1 /* C++ Parser.
2    Copyright (C) 2000-2014 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 "tree.h"
28 #include "print-tree.h"
29 #include "stringpool.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "cp-tree.h"
33 #include "intl.h"
34 #include "c-family/c-pragma.h"
35 #include "decl.h"
36 #include "flags.h"
37 #include "diagnostic-core.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "plugin.h"
43 #include "tree-pretty-print.h"
44 #include "parser.h"
45 #include "type-utils.h"
46 #include "omp-low.h"
47 
48 
49 /* The lexer.  */
50 
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52    and c-lex.c) and the C++ parser.  */
53 
54 static cp_token eof_token =
55 {
56   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
57 };
58 
59 /* The various kinds of non integral constant we encounter. */
60 typedef enum non_integral_constant {
61   NIC_NONE,
62   /* floating-point literal */
63   NIC_FLOAT,
64   /* %<this%> */
65   NIC_THIS,
66   /* %<__FUNCTION__%> */
67   NIC_FUNC_NAME,
68   /* %<__PRETTY_FUNCTION__%> */
69   NIC_PRETTY_FUNC,
70   /* %<__func__%> */
71   NIC_C99_FUNC,
72   /* "%<va_arg%> */
73   NIC_VA_ARG,
74   /* a cast */
75   NIC_CAST,
76   /* %<typeid%> operator */
77   NIC_TYPEID,
78   /* non-constant compound literals */
79   NIC_NCC,
80   /* a function call */
81   NIC_FUNC_CALL,
82   /* an increment */
83   NIC_INC,
84   /* an decrement */
85   NIC_DEC,
86   /* an array reference */
87   NIC_ARRAY_REF,
88   /* %<->%> */
89   NIC_ARROW,
90   /* %<.%> */
91   NIC_POINT,
92   /* the address of a label */
93   NIC_ADDR_LABEL,
94   /* %<*%> */
95   NIC_STAR,
96   /* %<&%> */
97   NIC_ADDR,
98   /* %<++%> */
99   NIC_PREINCREMENT,
100   /* %<--%> */
101   NIC_PREDECREMENT,
102   /* %<new%> */
103   NIC_NEW,
104   /* %<delete%> */
105   NIC_DEL,
106   /* calls to overloaded operators */
107   NIC_OVERLOADED,
108   /* an assignment */
109   NIC_ASSIGNMENT,
110   /* a comma operator */
111   NIC_COMMA,
112   /* a call to a constructor */
113   NIC_CONSTRUCTOR,
114   /* a transaction expression */
115   NIC_TRANSACTION
116 } non_integral_constant;
117 
118 /* The various kinds of errors about name-lookup failing. */
119 typedef enum name_lookup_error {
120   /* NULL */
121   NLE_NULL,
122   /* is not a type */
123   NLE_TYPE,
124   /* is not a class or namespace */
125   NLE_CXX98,
126   /* is not a class, namespace, or enumeration */
127   NLE_NOT_CXX98
128 } name_lookup_error;
129 
130 /* The various kinds of required token */
131 typedef enum required_token {
132   RT_NONE,
133   RT_SEMICOLON,  /* ';' */
134   RT_OPEN_PAREN, /* '(' */
135   RT_CLOSE_BRACE, /* '}' */
136   RT_OPEN_BRACE,  /* '{' */
137   RT_CLOSE_SQUARE, /* ']' */
138   RT_OPEN_SQUARE,  /* '[' */
139   RT_COMMA, /* ',' */
140   RT_SCOPE, /* '::' */
141   RT_LESS, /* '<' */
142   RT_GREATER, /* '>' */
143   RT_EQ, /* '=' */
144   RT_ELLIPSIS, /* '...' */
145   RT_MULT, /* '*' */
146   RT_COMPL, /* '~' */
147   RT_COLON, /* ':' */
148   RT_COLON_SCOPE, /* ':' or '::' */
149   RT_CLOSE_PAREN, /* ')' */
150   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151   RT_PRAGMA_EOL, /* end of line */
152   RT_NAME, /* identifier */
153 
154   /* The type is CPP_KEYWORD */
155   RT_NEW, /* new */
156   RT_DELETE, /* delete */
157   RT_RETURN, /* return */
158   RT_WHILE, /* while */
159   RT_EXTERN, /* extern */
160   RT_STATIC_ASSERT, /* static_assert */
161   RT_DECLTYPE, /* decltype */
162   RT_OPERATOR, /* operator */
163   RT_CLASS, /* class */
164   RT_TEMPLATE, /* template */
165   RT_NAMESPACE, /* namespace */
166   RT_USING, /* using */
167   RT_ASM, /* asm */
168   RT_TRY, /* try */
169   RT_CATCH, /* catch */
170   RT_THROW, /* throw */
171   RT_LABEL, /* __label__ */
172   RT_AT_TRY, /* @try */
173   RT_AT_SYNCHRONIZED, /* @synchronized */
174   RT_AT_THROW, /* @throw */
175 
176   RT_SELECT,  /* selection-statement */
177   RT_INTERATION, /* iteration-statement */
178   RT_JUMP, /* jump-statement */
179   RT_CLASS_KEY, /* class-key */
180   RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181   RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182   RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183   RT_TRANSACTION_CANCEL /* __transaction_cancel */
184 } required_token;
185 
186 /* Prototypes.  */
187 
188 static cp_lexer *cp_lexer_new_main
189   (void);
190 static cp_lexer *cp_lexer_new_from_tokens
191   (cp_token_cache *tokens);
192 static void cp_lexer_destroy
193   (cp_lexer *);
194 static int cp_lexer_saving_tokens
195   (const cp_lexer *);
196 static cp_token *cp_lexer_token_at
197   (cp_lexer *, cp_token_position);
198 static void cp_lexer_get_preprocessor_token
199   (cp_lexer *, cp_token *);
200 static inline cp_token *cp_lexer_peek_token
201   (cp_lexer *);
202 static cp_token *cp_lexer_peek_nth_token
203   (cp_lexer *, size_t);
204 static inline bool cp_lexer_next_token_is
205   (cp_lexer *, enum cpp_ttype);
206 static bool cp_lexer_next_token_is_not
207   (cp_lexer *, enum cpp_ttype);
208 static bool cp_lexer_next_token_is_keyword
209   (cp_lexer *, enum rid);
210 static cp_token *cp_lexer_consume_token
211   (cp_lexer *);
212 static void cp_lexer_purge_token
213   (cp_lexer *);
214 static void cp_lexer_purge_tokens_after
215   (cp_lexer *, cp_token_position);
216 static void cp_lexer_save_tokens
217   (cp_lexer *);
218 static void cp_lexer_commit_tokens
219   (cp_lexer *);
220 static void cp_lexer_rollback_tokens
221   (cp_lexer *);
222 static void cp_lexer_print_token
223   (FILE *, cp_token *);
224 static inline bool cp_lexer_debugging_p
225   (cp_lexer *);
226 static void cp_lexer_start_debugging
227   (cp_lexer *) ATTRIBUTE_UNUSED;
228 static void cp_lexer_stop_debugging
229   (cp_lexer *) ATTRIBUTE_UNUSED;
230 
231 static cp_token_cache *cp_token_cache_new
232   (cp_token *, cp_token *);
233 
234 static void cp_parser_initial_pragma
235   (cp_token *);
236 
237 static tree cp_literal_operator_id
238   (const char *);
239 
240 static void cp_parser_cilk_simd
241   (cp_parser *, cp_token *);
242 static bool cp_parser_omp_declare_reduction_exprs
243   (tree, cp_parser *);
244 static tree cp_parser_cilk_simd_vectorlength
245   (cp_parser *, tree, bool);
246 
247 /* Manifest constants.  */
248 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
249 #define CP_SAVED_TOKEN_STACK 5
250 
251 /* Variables.  */
252 
253 /* The stream to which debugging output should be written.  */
254 static FILE *cp_lexer_debug_stream;
255 
256 /* Nonzero if we are parsing an unevaluated operand: an operand to
257    sizeof, typeof, or alignof.  */
258 int cp_unevaluated_operand;
259 
260 /* Dump up to NUM tokens in BUFFER to FILE starting with token
261    START_TOKEN.  If START_TOKEN is NULL, the dump starts with the
262    first token in BUFFER.  If NUM is 0, dump all the tokens.  If
263    CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
264    highlighted by surrounding it in [[ ]].  */
265 
266 static void
cp_lexer_dump_tokens(FILE * file,vec<cp_token,va_gc> * buffer,cp_token * start_token,unsigned num,cp_token * curr_token)267 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
268 		      cp_token *start_token, unsigned num,
269 		      cp_token *curr_token)
270 {
271   unsigned i, nprinted;
272   cp_token *token;
273   bool do_print;
274 
275   fprintf (file, "%u tokens\n", vec_safe_length (buffer));
276 
277   if (buffer == NULL)
278     return;
279 
280   if (num == 0)
281     num = buffer->length ();
282 
283   if (start_token == NULL)
284     start_token = buffer->address ();
285 
286   if (start_token > buffer->address ())
287     {
288       cp_lexer_print_token (file, &(*buffer)[0]);
289       fprintf (file, " ... ");
290     }
291 
292   do_print = false;
293   nprinted = 0;
294   for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
295     {
296       if (token == start_token)
297 	do_print = true;
298 
299       if (!do_print)
300 	continue;
301 
302       nprinted++;
303       if (token == curr_token)
304 	fprintf (file, "[[");
305 
306       cp_lexer_print_token (file, token);
307 
308       if (token == curr_token)
309 	fprintf (file, "]]");
310 
311       switch (token->type)
312 	{
313 	  case CPP_SEMICOLON:
314 	  case CPP_OPEN_BRACE:
315 	  case CPP_CLOSE_BRACE:
316 	  case CPP_EOF:
317 	    fputc ('\n', file);
318 	    break;
319 
320 	  default:
321 	    fputc (' ', file);
322 	}
323     }
324 
325   if (i == num && i < buffer->length ())
326     {
327       fprintf (file, " ... ");
328       cp_lexer_print_token (file, &buffer->last ());
329     }
330 
331   fprintf (file, "\n");
332 }
333 
334 
335 /* Dump all tokens in BUFFER to stderr.  */
336 
337 void
cp_lexer_debug_tokens(vec<cp_token,va_gc> * buffer)338 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
339 {
340   cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
341 }
342 
343 DEBUG_FUNCTION void
debug(vec<cp_token,va_gc> & ref)344 debug (vec<cp_token, va_gc> &ref)
345 {
346   cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
347 }
348 
349 DEBUG_FUNCTION void
debug(vec<cp_token,va_gc> * ptr)350 debug (vec<cp_token, va_gc> *ptr)
351 {
352   if (ptr)
353     debug (*ptr);
354   else
355     fprintf (stderr, "<nil>\n");
356 }
357 
358 
359 /* Dump the cp_parser tree field T to FILE if T is non-NULL.  DESC is the
360    description for T.  */
361 
362 static void
cp_debug_print_tree_if_set(FILE * file,const char * desc,tree t)363 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
364 {
365   if (t)
366     {
367       fprintf (file, "%s: ", desc);
368       print_node_brief (file, "", t, 0);
369     }
370 }
371 
372 
373 /* Dump parser context C to FILE.  */
374 
375 static void
cp_debug_print_context(FILE * file,cp_parser_context * c)376 cp_debug_print_context (FILE *file, cp_parser_context *c)
377 {
378   const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
379   fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
380   print_node_brief (file, "", c->object_type, 0);
381   fprintf (file, "}\n");
382 }
383 
384 
385 /* Print the stack of parsing contexts to FILE starting with FIRST.  */
386 
387 static void
cp_debug_print_context_stack(FILE * file,cp_parser_context * first)388 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
389 {
390   unsigned i;
391   cp_parser_context *c;
392 
393   fprintf (file, "Parsing context stack:\n");
394   for (i = 0, c = first; c; c = c->next, i++)
395     {
396       fprintf (file, "\t#%u: ", i);
397       cp_debug_print_context (file, c);
398     }
399 }
400 
401 
402 /* Print the value of FLAG to FILE.  DESC is a string describing the flag.  */
403 
404 static void
cp_debug_print_flag(FILE * file,const char * desc,bool flag)405 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
406 {
407   if (flag)
408     fprintf (file, "%s: true\n", desc);
409 }
410 
411 
412 /* Print an unparsed function entry UF to FILE.  */
413 
414 static void
cp_debug_print_unparsed_function(FILE * file,cp_unparsed_functions_entry * uf)415 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
416 {
417   unsigned i;
418   cp_default_arg_entry *default_arg_fn;
419   tree fn;
420 
421   fprintf (file, "\tFunctions with default args:\n");
422   for (i = 0;
423        vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
424        i++)
425     {
426       fprintf (file, "\t\tClass type: ");
427       print_node_brief (file, "", default_arg_fn->class_type, 0);
428       fprintf (file, "\t\tDeclaration: ");
429       print_node_brief (file, "", default_arg_fn->decl, 0);
430       fprintf (file, "\n");
431     }
432 
433   fprintf (file, "\n\tFunctions with definitions that require "
434 	   "post-processing\n\t\t");
435   for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
436     {
437       print_node_brief (file, "", fn, 0);
438       fprintf (file, " ");
439     }
440   fprintf (file, "\n");
441 
442   fprintf (file, "\n\tNon-static data members with initializers that require "
443            "post-processing\n\t\t");
444   for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
445     {
446       print_node_brief (file, "", fn, 0);
447       fprintf (file, " ");
448     }
449   fprintf (file, "\n");
450 }
451 
452 
453 /* Print the stack of unparsed member functions S to FILE.  */
454 
455 static void
cp_debug_print_unparsed_queues(FILE * file,vec<cp_unparsed_functions_entry,va_gc> * s)456 cp_debug_print_unparsed_queues (FILE *file,
457 				vec<cp_unparsed_functions_entry, va_gc> *s)
458 {
459   unsigned i;
460   cp_unparsed_functions_entry *uf;
461 
462   fprintf (file, "Unparsed functions\n");
463   for (i = 0; vec_safe_iterate (s, i, &uf); i++)
464     {
465       fprintf (file, "#%u:\n", i);
466       cp_debug_print_unparsed_function (file, uf);
467     }
468 }
469 
470 
471 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
472    the given PARSER.  If FILE is NULL, the output is printed on stderr. */
473 
474 static void
cp_debug_parser_tokens(FILE * file,cp_parser * parser,int window_size)475 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
476 {
477   cp_token *next_token, *first_token, *start_token;
478 
479   if (file == NULL)
480     file = stderr;
481 
482   next_token = parser->lexer->next_token;
483   first_token = parser->lexer->buffer->address ();
484   start_token = (next_token > first_token + window_size / 2)
485 		? next_token - window_size / 2
486 		: first_token;
487   cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
488 			next_token);
489 }
490 
491 
492 /* Dump debugging information for the given PARSER.  If FILE is NULL,
493    the output is printed on stderr.  */
494 
495 void
cp_debug_parser(FILE * file,cp_parser * parser)496 cp_debug_parser (FILE *file, cp_parser *parser)
497 {
498   const size_t window_size = 20;
499   cp_token *token;
500   expanded_location eloc;
501 
502   if (file == NULL)
503     file = stderr;
504 
505   fprintf (file, "Parser state\n\n");
506   fprintf (file, "Number of tokens: %u\n",
507 	   vec_safe_length (parser->lexer->buffer));
508   cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
509   cp_debug_print_tree_if_set (file, "Object scope",
510 				     parser->object_scope);
511   cp_debug_print_tree_if_set (file, "Qualifying scope",
512 				     parser->qualifying_scope);
513   cp_debug_print_context_stack (file, parser->context);
514   cp_debug_print_flag (file, "Allow GNU extensions",
515 			      parser->allow_gnu_extensions_p);
516   cp_debug_print_flag (file, "'>' token is greater-than",
517 			      parser->greater_than_is_operator_p);
518   cp_debug_print_flag (file, "Default args allowed in current "
519 			      "parameter list", parser->default_arg_ok_p);
520   cp_debug_print_flag (file, "Parsing integral constant-expression",
521 			      parser->integral_constant_expression_p);
522   cp_debug_print_flag (file, "Allow non-constant expression in current "
523 			      "constant-expression",
524 			      parser->allow_non_integral_constant_expression_p);
525   cp_debug_print_flag (file, "Seen non-constant expression",
526 			      parser->non_integral_constant_expression_p);
527   cp_debug_print_flag (file, "Local names and 'this' forbidden in "
528 			      "current context",
529 			      parser->local_variables_forbidden_p);
530   cp_debug_print_flag (file, "In unbraced linkage specification",
531 			      parser->in_unbraced_linkage_specification_p);
532   cp_debug_print_flag (file, "Parsing a declarator",
533 			      parser->in_declarator_p);
534   cp_debug_print_flag (file, "In template argument list",
535 			      parser->in_template_argument_list_p);
536   cp_debug_print_flag (file, "Parsing an iteration statement",
537 			      parser->in_statement & IN_ITERATION_STMT);
538   cp_debug_print_flag (file, "Parsing a switch statement",
539 			      parser->in_statement & IN_SWITCH_STMT);
540   cp_debug_print_flag (file, "Parsing a structured OpenMP block",
541 			      parser->in_statement & IN_OMP_BLOCK);
542   cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
543 			      parser->in_statement & IN_CILK_SIMD_FOR);
544   cp_debug_print_flag (file, "Parsing a an OpenMP loop",
545 			      parser->in_statement & IN_OMP_FOR);
546   cp_debug_print_flag (file, "Parsing an if statement",
547 			      parser->in_statement & IN_IF_STMT);
548   cp_debug_print_flag (file, "Parsing a type-id in an expression "
549 			      "context", parser->in_type_id_in_expr_p);
550   cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
551 			      parser->implicit_extern_c);
552   cp_debug_print_flag (file, "String expressions should be translated "
553 			      "to execution character set",
554 			      parser->translate_strings_p);
555   cp_debug_print_flag (file, "Parsing function body outside of a "
556 			      "local class", parser->in_function_body);
557   cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
558 			      parser->colon_corrects_to_scope_p);
559   cp_debug_print_flag (file, "Colon doesn't start a class definition",
560 			      parser->colon_doesnt_start_class_def_p);
561   if (parser->type_definition_forbidden_message)
562     fprintf (file, "Error message for forbidden type definitions: %s\n",
563 	     parser->type_definition_forbidden_message);
564   cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
565   fprintf (file, "Number of class definitions in progress: %u\n",
566 	   parser->num_classes_being_defined);
567   fprintf (file, "Number of template parameter lists for the current "
568 	   "declaration: %u\n", parser->num_template_parameter_lists);
569   cp_debug_parser_tokens (file, parser, window_size);
570   token = parser->lexer->next_token;
571   fprintf (file, "Next token to parse:\n");
572   fprintf (file, "\tToken:  ");
573   cp_lexer_print_token (file, token);
574   eloc = expand_location (token->location);
575   fprintf (file, "\n\tFile:   %s\n", eloc.file);
576   fprintf (file, "\tLine:   %d\n", eloc.line);
577   fprintf (file, "\tColumn: %d\n", eloc.column);
578 }
579 
580 DEBUG_FUNCTION void
debug(cp_parser & ref)581 debug (cp_parser &ref)
582 {
583   cp_debug_parser (stderr, &ref);
584 }
585 
586 DEBUG_FUNCTION void
debug(cp_parser * ptr)587 debug (cp_parser *ptr)
588 {
589   if (ptr)
590     debug (*ptr);
591   else
592     fprintf (stderr, "<nil>\n");
593 }
594 
595 /* Allocate memory for a new lexer object and return it.  */
596 
597 static cp_lexer *
cp_lexer_alloc(void)598 cp_lexer_alloc (void)
599 {
600   cp_lexer *lexer;
601 
602   c_common_no_more_pch ();
603 
604   /* Allocate the memory.  */
605   lexer = ggc_alloc_cleared_cp_lexer ();
606 
607   /* Initially we are not debugging.  */
608   lexer->debugging_p = false;
609 
610   lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
611 
612   /* Create the buffer.  */
613   vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
614 
615   return lexer;
616 }
617 
618 
619 /* Create a new main C++ lexer, the lexer that gets tokens from the
620    preprocessor.  */
621 
622 static cp_lexer *
cp_lexer_new_main(void)623 cp_lexer_new_main (void)
624 {
625   cp_lexer *lexer;
626   cp_token token;
627 
628   /* It's possible that parsing the first pragma will load a PCH file,
629      which is a GC collection point.  So we have to do that before
630      allocating any memory.  */
631   cp_parser_initial_pragma (&token);
632 
633   lexer = cp_lexer_alloc ();
634 
635   /* Put the first token in the buffer.  */
636   lexer->buffer->quick_push (token);
637 
638   /* Get the remaining tokens from the preprocessor.  */
639   while (token.type != CPP_EOF)
640     {
641       cp_lexer_get_preprocessor_token (lexer, &token);
642       vec_safe_push (lexer->buffer, token);
643     }
644 
645   lexer->last_token = lexer->buffer->address ()
646                       + lexer->buffer->length ()
647 		      - 1;
648   lexer->next_token = lexer->buffer->length ()
649 		      ? lexer->buffer->address ()
650 		      : &eof_token;
651 
652   /* Subsequent preprocessor diagnostics should use compiler
653      diagnostic functions to get the compiler source location.  */
654   done_lexing = true;
655 
656   gcc_assert (!lexer->next_token->purged_p);
657   return lexer;
658 }
659 
660 /* Create a new lexer whose token stream is primed with the tokens in
661    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
662 
663 static cp_lexer *
cp_lexer_new_from_tokens(cp_token_cache * cache)664 cp_lexer_new_from_tokens (cp_token_cache *cache)
665 {
666   cp_token *first = cache->first;
667   cp_token *last = cache->last;
668   cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
669 
670   /* We do not own the buffer.  */
671   lexer->buffer = NULL;
672   lexer->next_token = first == last ? &eof_token : first;
673   lexer->last_token = last;
674 
675   lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
676 
677   /* Initially we are not debugging.  */
678   lexer->debugging_p = false;
679 
680   gcc_assert (!lexer->next_token->purged_p);
681   return lexer;
682 }
683 
684 /* Frees all resources associated with LEXER.  */
685 
686 static void
cp_lexer_destroy(cp_lexer * lexer)687 cp_lexer_destroy (cp_lexer *lexer)
688 {
689   vec_free (lexer->buffer);
690   lexer->saved_tokens.release ();
691   ggc_free (lexer);
692 }
693 
694 /* Returns nonzero if debugging information should be output.  */
695 
696 static inline bool
cp_lexer_debugging_p(cp_lexer * lexer)697 cp_lexer_debugging_p (cp_lexer *lexer)
698 {
699   return lexer->debugging_p;
700 }
701 
702 
703 static inline cp_token_position
cp_lexer_token_position(cp_lexer * lexer,bool previous_p)704 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
705 {
706   gcc_assert (!previous_p || lexer->next_token != &eof_token);
707 
708   return lexer->next_token - previous_p;
709 }
710 
711 static inline cp_token *
cp_lexer_token_at(cp_lexer *,cp_token_position pos)712 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
713 {
714   return pos;
715 }
716 
717 static inline void
cp_lexer_set_token_position(cp_lexer * lexer,cp_token_position pos)718 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
719 {
720   lexer->next_token = cp_lexer_token_at (lexer, pos);
721 }
722 
723 static inline cp_token_position
cp_lexer_previous_token_position(cp_lexer * lexer)724 cp_lexer_previous_token_position (cp_lexer *lexer)
725 {
726   if (lexer->next_token == &eof_token)
727     return lexer->last_token - 1;
728   else
729     return cp_lexer_token_position (lexer, true);
730 }
731 
732 static inline cp_token *
cp_lexer_previous_token(cp_lexer * lexer)733 cp_lexer_previous_token (cp_lexer *lexer)
734 {
735   cp_token_position tp = cp_lexer_previous_token_position (lexer);
736 
737   return cp_lexer_token_at (lexer, tp);
738 }
739 
740 /* nonzero if we are presently saving tokens.  */
741 
742 static inline int
cp_lexer_saving_tokens(const cp_lexer * lexer)743 cp_lexer_saving_tokens (const cp_lexer* lexer)
744 {
745   return lexer->saved_tokens.length () != 0;
746 }
747 
748 /* Store the next token from the preprocessor in *TOKEN.  Return true
749    if we reach EOF.  If LEXER is NULL, assume we are handling an
750    initial #pragma pch_preprocess, and thus want the lexer to return
751    processed strings.  */
752 
753 static void
cp_lexer_get_preprocessor_token(cp_lexer * lexer,cp_token * token)754 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
755 {
756   static int is_extern_c = 0;
757 
758    /* Get a new token from the preprocessor.  */
759   token->type
760     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
761 			lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
762   token->keyword = RID_MAX;
763   token->pragma_kind = PRAGMA_NONE;
764   token->purged_p = false;
765 
766   /* On some systems, some header files are surrounded by an
767      implicit extern "C" block.  Set a flag in the token if it
768      comes from such a header.  */
769   is_extern_c += pending_lang_change;
770   pending_lang_change = 0;
771   token->implicit_extern_c = is_extern_c > 0;
772 
773   /* Check to see if this token is a keyword.  */
774   if (token->type == CPP_NAME)
775     {
776       if (C_IS_RESERVED_WORD (token->u.value))
777 	{
778 	  /* Mark this token as a keyword.  */
779 	  token->type = CPP_KEYWORD;
780 	  /* Record which keyword.  */
781 	  token->keyword = C_RID_CODE (token->u.value);
782 	}
783       else
784 	{
785           if (warn_cxx0x_compat
786               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
787               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
788             {
789               /* Warn about the C++0x keyword (but still treat it as
790                  an identifier).  */
791               warning (OPT_Wc__0x_compat,
792                        "identifier %qE is a keyword in C++11",
793                        token->u.value);
794 
795               /* Clear out the C_RID_CODE so we don't warn about this
796                  particular identifier-turned-keyword again.  */
797               C_SET_RID_CODE (token->u.value, RID_MAX);
798             }
799 
800 	  token->ambiguous_p = false;
801 	  token->keyword = RID_MAX;
802 	}
803     }
804   else if (token->type == CPP_AT_NAME)
805     {
806       /* This only happens in Objective-C++; it must be a keyword.  */
807       token->type = CPP_KEYWORD;
808       switch (C_RID_CODE (token->u.value))
809 	{
810 	  /* Replace 'class' with '@class', 'private' with '@private',
811 	     etc.  This prevents confusion with the C++ keyword
812 	     'class', and makes the tokens consistent with other
813 	     Objective-C 'AT' keywords.  For example '@class' is
814 	     reported as RID_AT_CLASS which is consistent with
815 	     '@synchronized', which is reported as
816 	     RID_AT_SYNCHRONIZED.
817 	  */
818 	case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
819 	case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
820 	case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
821 	case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
822 	case RID_THROW:     token->keyword = RID_AT_THROW; break;
823 	case RID_TRY:       token->keyword = RID_AT_TRY; break;
824 	case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
825 	default:            token->keyword = C_RID_CODE (token->u.value);
826 	}
827     }
828   else if (token->type == CPP_PRAGMA)
829     {
830       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
831       token->pragma_kind = ((enum pragma_kind)
832 			    TREE_INT_CST_LOW (token->u.value));
833       token->u.value = NULL_TREE;
834     }
835 }
836 
837 /* Update the globals input_location and the input file stack from TOKEN.  */
838 static inline void
cp_lexer_set_source_position_from_token(cp_token * token)839 cp_lexer_set_source_position_from_token (cp_token *token)
840 {
841   if (token->type != CPP_EOF)
842     {
843       input_location = token->location;
844     }
845 }
846 
847 /* Update the globals input_location and the input file stack from LEXER.  */
848 static inline void
cp_lexer_set_source_position(cp_lexer * lexer)849 cp_lexer_set_source_position (cp_lexer *lexer)
850 {
851   cp_token *token = cp_lexer_peek_token (lexer);
852   cp_lexer_set_source_position_from_token (token);
853 }
854 
855 /* Return a pointer to the next token in the token stream, but do not
856    consume it.  */
857 
858 static inline cp_token *
cp_lexer_peek_token(cp_lexer * lexer)859 cp_lexer_peek_token (cp_lexer *lexer)
860 {
861   if (cp_lexer_debugging_p (lexer))
862     {
863       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
864       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
865       putc ('\n', cp_lexer_debug_stream);
866     }
867   return lexer->next_token;
868 }
869 
870 /* Return true if the next token has the indicated TYPE.  */
871 
872 static inline bool
cp_lexer_next_token_is(cp_lexer * lexer,enum cpp_ttype type)873 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
874 {
875   return cp_lexer_peek_token (lexer)->type == type;
876 }
877 
878 /* Return true if the next token does not have the indicated TYPE.  */
879 
880 static inline bool
cp_lexer_next_token_is_not(cp_lexer * lexer,enum cpp_ttype type)881 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
882 {
883   return !cp_lexer_next_token_is (lexer, type);
884 }
885 
886 /* Return true if the next token is the indicated KEYWORD.  */
887 
888 static inline bool
cp_lexer_next_token_is_keyword(cp_lexer * lexer,enum rid keyword)889 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
890 {
891   return cp_lexer_peek_token (lexer)->keyword == keyword;
892 }
893 
894 static inline bool
cp_lexer_nth_token_is_keyword(cp_lexer * lexer,size_t n,enum rid keyword)895 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
896 {
897   return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
898 }
899 
900 /* Return true if the next token is not the indicated KEYWORD.  */
901 
902 static inline bool
cp_lexer_next_token_is_not_keyword(cp_lexer * lexer,enum rid keyword)903 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
904 {
905   return cp_lexer_peek_token (lexer)->keyword != keyword;
906 }
907 
908 /* Return true if the next token is a keyword for a decl-specifier.  */
909 
910 static bool
cp_lexer_next_token_is_decl_specifier_keyword(cp_lexer * lexer)911 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
912 {
913   cp_token *token;
914 
915   token = cp_lexer_peek_token (lexer);
916   switch (token->keyword)
917     {
918       /* auto specifier: storage-class-specifier in C++,
919          simple-type-specifier in C++0x.  */
920     case RID_AUTO:
921       /* Storage classes.  */
922     case RID_REGISTER:
923     case RID_STATIC:
924     case RID_EXTERN:
925     case RID_MUTABLE:
926     case RID_THREAD:
927       /* Elaborated type specifiers.  */
928     case RID_ENUM:
929     case RID_CLASS:
930     case RID_STRUCT:
931     case RID_UNION:
932     case RID_TYPENAME:
933       /* Simple type specifiers.  */
934     case RID_CHAR:
935     case RID_CHAR16:
936     case RID_CHAR32:
937     case RID_WCHAR:
938     case RID_BOOL:
939     case RID_SHORT:
940     case RID_INT:
941     case RID_LONG:
942     case RID_INT128:
943     case RID_SIGNED:
944     case RID_UNSIGNED:
945     case RID_FLOAT:
946     case RID_DOUBLE:
947     case RID_VOID:
948       /* GNU extensions.  */
949     case RID_ATTRIBUTE:
950     case RID_TYPEOF:
951       /* C++0x extensions.  */
952     case RID_DECLTYPE:
953     case RID_UNDERLYING_TYPE:
954       return true;
955 
956     default:
957       return false;
958     }
959 }
960 
961 /* Returns TRUE iff the token T begins a decltype type.  */
962 
963 static bool
token_is_decltype(cp_token * t)964 token_is_decltype (cp_token *t)
965 {
966   return (t->keyword == RID_DECLTYPE
967 	  || t->type == CPP_DECLTYPE);
968 }
969 
970 /* Returns TRUE iff the next token begins a decltype type.  */
971 
972 static bool
cp_lexer_next_token_is_decltype(cp_lexer * lexer)973 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
974 {
975   cp_token *t = cp_lexer_peek_token (lexer);
976   return token_is_decltype (t);
977 }
978 
979 /* Return a pointer to the Nth token in the token stream.  If N is 1,
980    then this is precisely equivalent to cp_lexer_peek_token (except
981    that it is not inline).  One would like to disallow that case, but
982    there is one case (cp_parser_nth_token_starts_template_id) where
983    the caller passes a variable for N and it might be 1.  */
984 
985 static cp_token *
cp_lexer_peek_nth_token(cp_lexer * lexer,size_t n)986 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
987 {
988   cp_token *token;
989 
990   /* N is 1-based, not zero-based.  */
991   gcc_assert (n > 0);
992 
993   if (cp_lexer_debugging_p (lexer))
994     fprintf (cp_lexer_debug_stream,
995 	     "cp_lexer: peeking ahead %ld at token: ", (long)n);
996 
997   --n;
998   token = lexer->next_token;
999   gcc_assert (!n || token != &eof_token);
1000   while (n != 0)
1001     {
1002       ++token;
1003       if (token == lexer->last_token)
1004 	{
1005 	  token = &eof_token;
1006 	  break;
1007 	}
1008 
1009       if (!token->purged_p)
1010 	--n;
1011     }
1012 
1013   if (cp_lexer_debugging_p (lexer))
1014     {
1015       cp_lexer_print_token (cp_lexer_debug_stream, token);
1016       putc ('\n', cp_lexer_debug_stream);
1017     }
1018 
1019   return token;
1020 }
1021 
1022 /* Return the next token, and advance the lexer's next_token pointer
1023    to point to the next non-purged token.  */
1024 
1025 static cp_token *
cp_lexer_consume_token(cp_lexer * lexer)1026 cp_lexer_consume_token (cp_lexer* lexer)
1027 {
1028   cp_token *token = lexer->next_token;
1029 
1030   gcc_assert (token != &eof_token);
1031   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1032 
1033   do
1034     {
1035       lexer->next_token++;
1036       if (lexer->next_token == lexer->last_token)
1037 	{
1038 	  lexer->next_token = &eof_token;
1039 	  break;
1040 	}
1041 
1042     }
1043   while (lexer->next_token->purged_p);
1044 
1045   cp_lexer_set_source_position_from_token (token);
1046 
1047   /* Provide debugging output.  */
1048   if (cp_lexer_debugging_p (lexer))
1049     {
1050       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1051       cp_lexer_print_token (cp_lexer_debug_stream, token);
1052       putc ('\n', cp_lexer_debug_stream);
1053     }
1054 
1055   return token;
1056 }
1057 
1058 /* Permanently remove the next token from the token stream, and
1059    advance the next_token pointer to refer to the next non-purged
1060    token.  */
1061 
1062 static void
cp_lexer_purge_token(cp_lexer * lexer)1063 cp_lexer_purge_token (cp_lexer *lexer)
1064 {
1065   cp_token *tok = lexer->next_token;
1066 
1067   gcc_assert (tok != &eof_token);
1068   tok->purged_p = true;
1069   tok->location = UNKNOWN_LOCATION;
1070   tok->u.value = NULL_TREE;
1071   tok->keyword = RID_MAX;
1072 
1073   do
1074     {
1075       tok++;
1076       if (tok == lexer->last_token)
1077 	{
1078 	  tok = &eof_token;
1079 	  break;
1080 	}
1081     }
1082   while (tok->purged_p);
1083   lexer->next_token = tok;
1084 }
1085 
1086 /* Permanently remove all tokens after TOK, up to, but not
1087    including, the token that will be returned next by
1088    cp_lexer_peek_token.  */
1089 
1090 static void
cp_lexer_purge_tokens_after(cp_lexer * lexer,cp_token * tok)1091 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1092 {
1093   cp_token *peek = lexer->next_token;
1094 
1095   if (peek == &eof_token)
1096     peek = lexer->last_token;
1097 
1098   gcc_assert (tok < peek);
1099 
1100   for ( tok += 1; tok != peek; tok += 1)
1101     {
1102       tok->purged_p = true;
1103       tok->location = UNKNOWN_LOCATION;
1104       tok->u.value = NULL_TREE;
1105       tok->keyword = RID_MAX;
1106     }
1107 }
1108 
1109 /* Begin saving tokens.  All tokens consumed after this point will be
1110    preserved.  */
1111 
1112 static void
cp_lexer_save_tokens(cp_lexer * lexer)1113 cp_lexer_save_tokens (cp_lexer* lexer)
1114 {
1115   /* Provide debugging output.  */
1116   if (cp_lexer_debugging_p (lexer))
1117     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1118 
1119   lexer->saved_tokens.safe_push (lexer->next_token);
1120 }
1121 
1122 /* Commit to the portion of the token stream most recently saved.  */
1123 
1124 static void
cp_lexer_commit_tokens(cp_lexer * lexer)1125 cp_lexer_commit_tokens (cp_lexer* lexer)
1126 {
1127   /* Provide debugging output.  */
1128   if (cp_lexer_debugging_p (lexer))
1129     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1130 
1131   lexer->saved_tokens.pop ();
1132 }
1133 
1134 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1135    to the token stream.  Stop saving tokens.  */
1136 
1137 static void
cp_lexer_rollback_tokens(cp_lexer * lexer)1138 cp_lexer_rollback_tokens (cp_lexer* lexer)
1139 {
1140   /* Provide debugging output.  */
1141   if (cp_lexer_debugging_p (lexer))
1142     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1143 
1144   lexer->next_token = lexer->saved_tokens.pop ();
1145 }
1146 
1147 /* Print a representation of the TOKEN on the STREAM.  */
1148 
1149 static void
cp_lexer_print_token(FILE * stream,cp_token * token)1150 cp_lexer_print_token (FILE * stream, cp_token *token)
1151 {
1152   /* We don't use cpp_type2name here because the parser defines
1153      a few tokens of its own.  */
1154   static const char *const token_names[] = {
1155     /* cpplib-defined token types */
1156 #define OP(e, s) #e,
1157 #define TK(e, s) #e,
1158     TTYPE_TABLE
1159 #undef OP
1160 #undef TK
1161     /* C++ parser token types - see "Manifest constants", above.  */
1162     "KEYWORD",
1163     "TEMPLATE_ID",
1164     "NESTED_NAME_SPECIFIER",
1165   };
1166 
1167   /* For some tokens, print the associated data.  */
1168   switch (token->type)
1169     {
1170     case CPP_KEYWORD:
1171       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1172 	 For example, `struct' is mapped to an INTEGER_CST.  */
1173       if (!identifier_p (token->u.value))
1174 	break;
1175       /* else fall through */
1176     case CPP_NAME:
1177       fputs (IDENTIFIER_POINTER (token->u.value), stream);
1178       break;
1179 
1180     case CPP_STRING:
1181     case CPP_STRING16:
1182     case CPP_STRING32:
1183     case CPP_WSTRING:
1184     case CPP_UTF8STRING:
1185       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1186       break;
1187 
1188     case CPP_NUMBER:
1189       print_generic_expr (stream, token->u.value, 0);
1190       break;
1191 
1192     default:
1193       /* If we have a name for the token, print it out.  Otherwise, we
1194 	 simply give the numeric code.  */
1195       if (token->type < ARRAY_SIZE(token_names))
1196 	fputs (token_names[token->type], stream);
1197       else
1198 	fprintf (stream, "[%d]", token->type);
1199       break;
1200     }
1201 }
1202 
1203 DEBUG_FUNCTION void
debug(cp_token & ref)1204 debug (cp_token &ref)
1205 {
1206   cp_lexer_print_token (stderr, &ref);
1207   fprintf (stderr, "\n");
1208 }
1209 
1210 DEBUG_FUNCTION void
debug(cp_token * ptr)1211 debug (cp_token *ptr)
1212 {
1213   if (ptr)
1214     debug (*ptr);
1215   else
1216     fprintf (stderr, "<nil>\n");
1217 }
1218 
1219 
1220 /* Start emitting debugging information.  */
1221 
1222 static void
cp_lexer_start_debugging(cp_lexer * lexer)1223 cp_lexer_start_debugging (cp_lexer* lexer)
1224 {
1225   lexer->debugging_p = true;
1226   cp_lexer_debug_stream = stderr;
1227 }
1228 
1229 /* Stop emitting debugging information.  */
1230 
1231 static void
cp_lexer_stop_debugging(cp_lexer * lexer)1232 cp_lexer_stop_debugging (cp_lexer* lexer)
1233 {
1234   lexer->debugging_p = false;
1235   cp_lexer_debug_stream = NULL;
1236 }
1237 
1238 /* Create a new cp_token_cache, representing a range of tokens.  */
1239 
1240 static cp_token_cache *
cp_token_cache_new(cp_token * first,cp_token * last)1241 cp_token_cache_new (cp_token *first, cp_token *last)
1242 {
1243   cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1244   cache->first = first;
1245   cache->last = last;
1246   return cache;
1247 }
1248 
1249 /* Diagnose if #pragma omp declare simd isn't followed immediately
1250    by function declaration or definition.  */
1251 
1252 static inline void
cp_ensure_no_omp_declare_simd(cp_parser * parser)1253 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1254 {
1255   if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1256     {
1257       error ("%<#pragma omp declare simd%> not immediately followed by "
1258 	     "function declaration or definition");
1259       parser->omp_declare_simd = NULL;
1260     }
1261 }
1262 
1263 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1264    and put that into "omp declare simd" attribute.  */
1265 
1266 static inline void
cp_finalize_omp_declare_simd(cp_parser * parser,tree fndecl)1267 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1268 {
1269   if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1270     {
1271       if (fndecl == error_mark_node)
1272 	{
1273 	  parser->omp_declare_simd = NULL;
1274 	  return;
1275 	}
1276       if (TREE_CODE (fndecl) != FUNCTION_DECL)
1277 	{
1278 	  cp_ensure_no_omp_declare_simd (parser);
1279 	  return;
1280 	}
1281     }
1282 }
1283 
1284 /* Decl-specifiers.  */
1285 
1286 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1287 
1288 static void
clear_decl_specs(cp_decl_specifier_seq * decl_specs)1289 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1290 {
1291   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1292 }
1293 
1294 /* Declarators.  */
1295 
1296 /* Nothing other than the parser should be creating declarators;
1297    declarators are a semi-syntactic representation of C++ entities.
1298    Other parts of the front end that need to create entities (like
1299    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1300 
1301 static cp_declarator *make_call_declarator
1302   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1303 static cp_declarator *make_array_declarator
1304   (cp_declarator *, tree);
1305 static cp_declarator *make_pointer_declarator
1306   (cp_cv_quals, cp_declarator *, tree);
1307 static cp_declarator *make_reference_declarator
1308   (cp_cv_quals, cp_declarator *, bool, tree);
1309 static cp_parameter_declarator *make_parameter_declarator
1310   (cp_decl_specifier_seq *, cp_declarator *, tree);
1311 static cp_declarator *make_ptrmem_declarator
1312   (cp_cv_quals, tree, cp_declarator *, tree);
1313 
1314 /* An erroneous declarator.  */
1315 static cp_declarator *cp_error_declarator;
1316 
1317 /* The obstack on which declarators and related data structures are
1318    allocated.  */
1319 static struct obstack declarator_obstack;
1320 
1321 /* Alloc BYTES from the declarator memory pool.  */
1322 
1323 static inline void *
alloc_declarator(size_t bytes)1324 alloc_declarator (size_t bytes)
1325 {
1326   return obstack_alloc (&declarator_obstack, bytes);
1327 }
1328 
1329 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1330    common to all declarators.  */
1331 
1332 static cp_declarator *
make_declarator(cp_declarator_kind kind)1333 make_declarator (cp_declarator_kind kind)
1334 {
1335   cp_declarator *declarator;
1336 
1337   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1338   declarator->kind = kind;
1339   declarator->attributes = NULL_TREE;
1340   declarator->std_attributes = NULL_TREE;
1341   declarator->declarator = NULL;
1342   declarator->parameter_pack_p = false;
1343   declarator->id_loc = UNKNOWN_LOCATION;
1344 
1345   return declarator;
1346 }
1347 
1348 /* Make a declarator for a generalized identifier.  If
1349    QUALIFYING_SCOPE is non-NULL, the identifier is
1350    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1351    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1352    is, if any.   */
1353 
1354 static cp_declarator *
make_id_declarator(tree qualifying_scope,tree unqualified_name,special_function_kind sfk)1355 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1356 		    special_function_kind sfk)
1357 {
1358   cp_declarator *declarator;
1359 
1360   /* It is valid to write:
1361 
1362        class C { void f(); };
1363        typedef C D;
1364        void D::f();
1365 
1366      The standard is not clear about whether `typedef const C D' is
1367      legal; as of 2002-09-15 the committee is considering that
1368      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1369      well.  */
1370   if (qualifying_scope && TYPE_P (qualifying_scope))
1371     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1372 
1373   gcc_assert (identifier_p (unqualified_name)
1374 	      || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1375 	      || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1376 
1377   declarator = make_declarator (cdk_id);
1378   declarator->u.id.qualifying_scope = qualifying_scope;
1379   declarator->u.id.unqualified_name = unqualified_name;
1380   declarator->u.id.sfk = sfk;
1381 
1382   return declarator;
1383 }
1384 
1385 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1386    of modifiers such as const or volatile to apply to the pointer
1387    type, represented as identifiers.  ATTRIBUTES represent the attributes that
1388    appertain to the pointer or reference.  */
1389 
1390 cp_declarator *
make_pointer_declarator(cp_cv_quals cv_qualifiers,cp_declarator * target,tree attributes)1391 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1392 			 tree attributes)
1393 {
1394   cp_declarator *declarator;
1395 
1396   declarator = make_declarator (cdk_pointer);
1397   declarator->declarator = target;
1398   declarator->u.pointer.qualifiers = cv_qualifiers;
1399   declarator->u.pointer.class_type = NULL_TREE;
1400   if (target)
1401     {
1402       declarator->id_loc = target->id_loc;
1403       declarator->parameter_pack_p = target->parameter_pack_p;
1404       target->parameter_pack_p = false;
1405     }
1406   else
1407     declarator->parameter_pack_p = false;
1408 
1409   declarator->std_attributes = attributes;
1410 
1411   return declarator;
1412 }
1413 
1414 /* Like make_pointer_declarator -- but for references.  ATTRIBUTES
1415    represent the attributes that appertain to the pointer or
1416    reference.  */
1417 
1418 cp_declarator *
make_reference_declarator(cp_cv_quals cv_qualifiers,cp_declarator * target,bool rvalue_ref,tree attributes)1419 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1420 			   bool rvalue_ref, tree attributes)
1421 {
1422   cp_declarator *declarator;
1423 
1424   declarator = make_declarator (cdk_reference);
1425   declarator->declarator = target;
1426   declarator->u.reference.qualifiers = cv_qualifiers;
1427   declarator->u.reference.rvalue_ref = rvalue_ref;
1428   if (target)
1429     {
1430       declarator->id_loc = target->id_loc;
1431       declarator->parameter_pack_p = target->parameter_pack_p;
1432       target->parameter_pack_p = false;
1433     }
1434   else
1435     declarator->parameter_pack_p = false;
1436 
1437   declarator->std_attributes = attributes;
1438 
1439   return declarator;
1440 }
1441 
1442 /* Like make_pointer_declarator -- but for a pointer to a non-static
1443    member of CLASS_TYPE.  ATTRIBUTES represent the attributes that
1444    appertain to the pointer or reference.  */
1445 
1446 cp_declarator *
make_ptrmem_declarator(cp_cv_quals cv_qualifiers,tree class_type,cp_declarator * pointee,tree attributes)1447 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1448 			cp_declarator *pointee,
1449 			tree attributes)
1450 {
1451   cp_declarator *declarator;
1452 
1453   declarator = make_declarator (cdk_ptrmem);
1454   declarator->declarator = pointee;
1455   declarator->u.pointer.qualifiers = cv_qualifiers;
1456   declarator->u.pointer.class_type = class_type;
1457 
1458   if (pointee)
1459     {
1460       declarator->parameter_pack_p = pointee->parameter_pack_p;
1461       pointee->parameter_pack_p = false;
1462     }
1463   else
1464     declarator->parameter_pack_p = false;
1465 
1466   declarator->std_attributes = attributes;
1467 
1468   return declarator;
1469 }
1470 
1471 /* Make a declarator for the function given by TARGET, with the
1472    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1473    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1474    indicates what exceptions can be thrown.  */
1475 
1476 cp_declarator *
make_call_declarator(cp_declarator * target,tree parms,cp_cv_quals cv_qualifiers,cp_virt_specifiers virt_specifiers,cp_ref_qualifier ref_qualifier,tree exception_specification,tree late_return_type)1477 make_call_declarator (cp_declarator *target,
1478 		      tree parms,
1479 		      cp_cv_quals cv_qualifiers,
1480 		      cp_virt_specifiers virt_specifiers,
1481 		      cp_ref_qualifier ref_qualifier,
1482 		      tree exception_specification,
1483 		      tree late_return_type)
1484 {
1485   cp_declarator *declarator;
1486 
1487   declarator = make_declarator (cdk_function);
1488   declarator->declarator = target;
1489   declarator->u.function.parameters = parms;
1490   declarator->u.function.qualifiers = cv_qualifiers;
1491   declarator->u.function.virt_specifiers = virt_specifiers;
1492   declarator->u.function.ref_qualifier = ref_qualifier;
1493   declarator->u.function.exception_specification = exception_specification;
1494   declarator->u.function.late_return_type = late_return_type;
1495   if (target)
1496     {
1497       declarator->id_loc = target->id_loc;
1498       declarator->parameter_pack_p = target->parameter_pack_p;
1499       target->parameter_pack_p = false;
1500     }
1501   else
1502     declarator->parameter_pack_p = false;
1503 
1504   return declarator;
1505 }
1506 
1507 /* Make a declarator for an array of BOUNDS elements, each of which is
1508    defined by ELEMENT.  */
1509 
1510 cp_declarator *
make_array_declarator(cp_declarator * element,tree bounds)1511 make_array_declarator (cp_declarator *element, tree bounds)
1512 {
1513   cp_declarator *declarator;
1514 
1515   declarator = make_declarator (cdk_array);
1516   declarator->declarator = element;
1517   declarator->u.array.bounds = bounds;
1518   if (element)
1519     {
1520       declarator->id_loc = element->id_loc;
1521       declarator->parameter_pack_p = element->parameter_pack_p;
1522       element->parameter_pack_p = false;
1523     }
1524   else
1525     declarator->parameter_pack_p = false;
1526 
1527   return declarator;
1528 }
1529 
1530 /* Determine whether the declarator we've seen so far can be a
1531    parameter pack, when followed by an ellipsis.  */
1532 static bool
declarator_can_be_parameter_pack(cp_declarator * declarator)1533 declarator_can_be_parameter_pack (cp_declarator *declarator)
1534 {
1535   /* Search for a declarator name, or any other declarator that goes
1536      after the point where the ellipsis could appear in a parameter
1537      pack. If we find any of these, then this declarator can not be
1538      made into a parameter pack.  */
1539   bool found = false;
1540   while (declarator && !found)
1541     {
1542       switch ((int)declarator->kind)
1543 	{
1544 	case cdk_id:
1545 	case cdk_array:
1546 	  found = true;
1547 	  break;
1548 
1549 	case cdk_error:
1550 	  return true;
1551 
1552 	default:
1553 	  declarator = declarator->declarator;
1554 	  break;
1555 	}
1556     }
1557 
1558   return !found;
1559 }
1560 
1561 cp_parameter_declarator *no_parameters;
1562 
1563 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1564    DECLARATOR and DEFAULT_ARGUMENT.  */
1565 
1566 cp_parameter_declarator *
make_parameter_declarator(cp_decl_specifier_seq * decl_specifiers,cp_declarator * declarator,tree default_argument)1567 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1568 			   cp_declarator *declarator,
1569 			   tree default_argument)
1570 {
1571   cp_parameter_declarator *parameter;
1572 
1573   parameter = ((cp_parameter_declarator *)
1574 	       alloc_declarator (sizeof (cp_parameter_declarator)));
1575   parameter->next = NULL;
1576   if (decl_specifiers)
1577     parameter->decl_specifiers = *decl_specifiers;
1578   else
1579     clear_decl_specs (&parameter->decl_specifiers);
1580   parameter->declarator = declarator;
1581   parameter->default_argument = default_argument;
1582   parameter->ellipsis_p = false;
1583 
1584   return parameter;
1585 }
1586 
1587 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1588 
1589 static bool
function_declarator_p(const cp_declarator * declarator)1590 function_declarator_p (const cp_declarator *declarator)
1591 {
1592   while (declarator)
1593     {
1594       if (declarator->kind == cdk_function
1595 	  && declarator->declarator->kind == cdk_id)
1596 	return true;
1597       if (declarator->kind == cdk_id
1598 	  || declarator->kind == cdk_error)
1599 	return false;
1600       declarator = declarator->declarator;
1601     }
1602   return false;
1603 }
1604 
1605 /* The parser.  */
1606 
1607 /* Overview
1608    --------
1609 
1610    A cp_parser parses the token stream as specified by the C++
1611    grammar.  Its job is purely parsing, not semantic analysis.  For
1612    example, the parser breaks the token stream into declarators,
1613    expressions, statements, and other similar syntactic constructs.
1614    It does not check that the types of the expressions on either side
1615    of an assignment-statement are compatible, or that a function is
1616    not declared with a parameter of type `void'.
1617 
1618    The parser invokes routines elsewhere in the compiler to perform
1619    semantic analysis and to build up the abstract syntax tree for the
1620    code processed.
1621 
1622    The parser (and the template instantiation code, which is, in a
1623    way, a close relative of parsing) are the only parts of the
1624    compiler that should be calling push_scope and pop_scope, or
1625    related functions.  The parser (and template instantiation code)
1626    keeps track of what scope is presently active; everything else
1627    should simply honor that.  (The code that generates static
1628    initializers may also need to set the scope, in order to check
1629    access control correctly when emitting the initializers.)
1630 
1631    Methodology
1632    -----------
1633 
1634    The parser is of the standard recursive-descent variety.  Upcoming
1635    tokens in the token stream are examined in order to determine which
1636    production to use when parsing a non-terminal.  Some C++ constructs
1637    require arbitrary look ahead to disambiguate.  For example, it is
1638    impossible, in the general case, to tell whether a statement is an
1639    expression or declaration without scanning the entire statement.
1640    Therefore, the parser is capable of "parsing tentatively."  When the
1641    parser is not sure what construct comes next, it enters this mode.
1642    Then, while we attempt to parse the construct, the parser queues up
1643    error messages, rather than issuing them immediately, and saves the
1644    tokens it consumes.  If the construct is parsed successfully, the
1645    parser "commits", i.e., it issues any queued error messages and
1646    the tokens that were being preserved are permanently discarded.
1647    If, however, the construct is not parsed successfully, the parser
1648    rolls back its state completely so that it can resume parsing using
1649    a different alternative.
1650 
1651    Future Improvements
1652    -------------------
1653 
1654    The performance of the parser could probably be improved substantially.
1655    We could often eliminate the need to parse tentatively by looking ahead
1656    a little bit.  In some places, this approach might not entirely eliminate
1657    the need to parse tentatively, but it might still speed up the average
1658    case.  */
1659 
1660 /* Flags that are passed to some parsing functions.  These values can
1661    be bitwise-ored together.  */
1662 
1663 enum
1664 {
1665   /* No flags.  */
1666   CP_PARSER_FLAGS_NONE = 0x0,
1667   /* The construct is optional.  If it is not present, then no error
1668      should be issued.  */
1669   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1670   /* When parsing a type-specifier, treat user-defined type-names
1671      as non-type identifiers.  */
1672   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1673   /* When parsing a type-specifier, do not try to parse a class-specifier
1674      or enum-specifier.  */
1675   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1676   /* When parsing a decl-specifier-seq, only allow type-specifier or
1677      constexpr.  */
1678   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1679 };
1680 
1681 /* This type is used for parameters and variables which hold
1682    combinations of the above flags.  */
1683 typedef int cp_parser_flags;
1684 
1685 /* The different kinds of declarators we want to parse.  */
1686 
1687 typedef enum cp_parser_declarator_kind
1688 {
1689   /* We want an abstract declarator.  */
1690   CP_PARSER_DECLARATOR_ABSTRACT,
1691   /* We want a named declarator.  */
1692   CP_PARSER_DECLARATOR_NAMED,
1693   /* We don't mind, but the name must be an unqualified-id.  */
1694   CP_PARSER_DECLARATOR_EITHER
1695 } cp_parser_declarator_kind;
1696 
1697 /* The precedence values used to parse binary expressions.  The minimum value
1698    of PREC must be 1, because zero is reserved to quickly discriminate
1699    binary operators from other tokens.  */
1700 
1701 enum cp_parser_prec
1702 {
1703   PREC_NOT_OPERATOR,
1704   PREC_LOGICAL_OR_EXPRESSION,
1705   PREC_LOGICAL_AND_EXPRESSION,
1706   PREC_INCLUSIVE_OR_EXPRESSION,
1707   PREC_EXCLUSIVE_OR_EXPRESSION,
1708   PREC_AND_EXPRESSION,
1709   PREC_EQUALITY_EXPRESSION,
1710   PREC_RELATIONAL_EXPRESSION,
1711   PREC_SHIFT_EXPRESSION,
1712   PREC_ADDITIVE_EXPRESSION,
1713   PREC_MULTIPLICATIVE_EXPRESSION,
1714   PREC_PM_EXPRESSION,
1715   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1716 };
1717 
1718 /* A mapping from a token type to a corresponding tree node type, with a
1719    precedence value.  */
1720 
1721 typedef struct cp_parser_binary_operations_map_node
1722 {
1723   /* The token type.  */
1724   enum cpp_ttype token_type;
1725   /* The corresponding tree code.  */
1726   enum tree_code tree_type;
1727   /* The precedence of this operator.  */
1728   enum cp_parser_prec prec;
1729 } cp_parser_binary_operations_map_node;
1730 
1731 typedef struct cp_parser_expression_stack_entry
1732 {
1733   /* Left hand side of the binary operation we are currently
1734      parsing.  */
1735   tree lhs;
1736   /* Original tree code for left hand side, if it was a binary
1737      expression itself (used for -Wparentheses).  */
1738   enum tree_code lhs_type;
1739   /* Tree code for the binary operation we are parsing.  */
1740   enum tree_code tree_type;
1741   /* Precedence of the binary operation we are parsing.  */
1742   enum cp_parser_prec prec;
1743   /* Location of the binary operation we are parsing.  */
1744   location_t loc;
1745 } cp_parser_expression_stack_entry;
1746 
1747 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1748    entries because precedence levels on the stack are monotonically
1749    increasing.  */
1750 typedef struct cp_parser_expression_stack_entry
1751   cp_parser_expression_stack[NUM_PREC_VALUES];
1752 
1753 /* Prototypes.  */
1754 
1755 /* Constructors and destructors.  */
1756 
1757 static cp_parser_context *cp_parser_context_new
1758   (cp_parser_context *);
1759 
1760 /* Class variables.  */
1761 
1762 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1763 
1764 /* The operator-precedence table used by cp_parser_binary_expression.
1765    Transformed into an associative array (binops_by_token) by
1766    cp_parser_new.  */
1767 
1768 static const cp_parser_binary_operations_map_node binops[] = {
1769   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1770   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1771 
1772   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1773   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1774   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1775 
1776   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1777   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1778 
1779   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1780   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1781 
1782   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1783   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1784   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1785   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1786 
1787   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1788   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1789 
1790   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1791 
1792   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1793 
1794   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1795 
1796   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1797 
1798   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1799 };
1800 
1801 /* The same as binops, but initialized by cp_parser_new so that
1802    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1803    for speed.  */
1804 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1805 
1806 /* Constructors and destructors.  */
1807 
1808 /* Construct a new context.  The context below this one on the stack
1809    is given by NEXT.  */
1810 
1811 static cp_parser_context *
cp_parser_context_new(cp_parser_context * next)1812 cp_parser_context_new (cp_parser_context* next)
1813 {
1814   cp_parser_context *context;
1815 
1816   /* Allocate the storage.  */
1817   if (cp_parser_context_free_list != NULL)
1818     {
1819       /* Pull the first entry from the free list.  */
1820       context = cp_parser_context_free_list;
1821       cp_parser_context_free_list = context->next;
1822       memset (context, 0, sizeof (*context));
1823     }
1824   else
1825     context = ggc_alloc_cleared_cp_parser_context ();
1826 
1827   /* No errors have occurred yet in this context.  */
1828   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1829   /* If this is not the bottommost context, copy information that we
1830      need from the previous context.  */
1831   if (next)
1832     {
1833       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1834 	 expression, then we are parsing one in this context, too.  */
1835       context->object_type = next->object_type;
1836       /* Thread the stack.  */
1837       context->next = next;
1838     }
1839 
1840   return context;
1841 }
1842 
1843 /* Managing the unparsed function queues.  */
1844 
1845 #define unparsed_funs_with_default_args \
1846   parser->unparsed_queues->last ().funs_with_default_args
1847 #define unparsed_funs_with_definitions \
1848   parser->unparsed_queues->last ().funs_with_definitions
1849 #define unparsed_nsdmis \
1850   parser->unparsed_queues->last ().nsdmis
1851 
1852 static void
push_unparsed_function_queues(cp_parser * parser)1853 push_unparsed_function_queues (cp_parser *parser)
1854 {
1855   cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL};
1856   vec_safe_push (parser->unparsed_queues, e);
1857 }
1858 
1859 static void
pop_unparsed_function_queues(cp_parser * parser)1860 pop_unparsed_function_queues (cp_parser *parser)
1861 {
1862   release_tree_vector (unparsed_funs_with_definitions);
1863   parser->unparsed_queues->pop ();
1864 }
1865 
1866 /* Prototypes.  */
1867 
1868 /* Constructors and destructors.  */
1869 
1870 static cp_parser *cp_parser_new
1871   (void);
1872 
1873 /* Routines to parse various constructs.
1874 
1875    Those that return `tree' will return the error_mark_node (rather
1876    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1877    Sometimes, they will return an ordinary node if error-recovery was
1878    attempted, even though a parse error occurred.  So, to check
1879    whether or not a parse error occurred, you should always use
1880    cp_parser_error_occurred.  If the construct is optional (indicated
1881    either by an `_opt' in the name of the function that does the
1882    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1883    the construct is not present.  */
1884 
1885 /* Lexical conventions [gram.lex]  */
1886 
1887 static tree cp_parser_identifier
1888   (cp_parser *);
1889 static tree cp_parser_string_literal
1890   (cp_parser *, bool, bool);
1891 static tree cp_parser_userdef_char_literal
1892   (cp_parser *);
1893 static tree cp_parser_userdef_string_literal
1894   (tree);
1895 static tree cp_parser_userdef_numeric_literal
1896   (cp_parser *);
1897 
1898 /* Basic concepts [gram.basic]  */
1899 
1900 static bool cp_parser_translation_unit
1901   (cp_parser *);
1902 
1903 /* Expressions [gram.expr]  */
1904 
1905 static tree cp_parser_primary_expression
1906   (cp_parser *, bool, bool, bool, cp_id_kind *);
1907 static tree cp_parser_id_expression
1908   (cp_parser *, bool, bool, bool *, bool, bool);
1909 static tree cp_parser_unqualified_id
1910   (cp_parser *, bool, bool, bool, bool);
1911 static tree cp_parser_nested_name_specifier_opt
1912   (cp_parser *, bool, bool, bool, bool);
1913 static tree cp_parser_nested_name_specifier
1914   (cp_parser *, bool, bool, bool, bool);
1915 static tree cp_parser_qualifying_entity
1916   (cp_parser *, bool, bool, bool, bool, bool);
1917 static tree cp_parser_postfix_expression
1918   (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1919 static tree cp_parser_postfix_open_square_expression
1920   (cp_parser *, tree, bool, bool);
1921 static tree cp_parser_postfix_dot_deref_expression
1922   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1923 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1924   (cp_parser *, int, bool, bool, bool *);
1925 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1926 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1927 static void cp_parser_pseudo_destructor_name
1928   (cp_parser *, tree, tree *, tree *);
1929 static tree cp_parser_unary_expression
1930   (cp_parser *, bool, bool, cp_id_kind *);
1931 static enum tree_code cp_parser_unary_operator
1932   (cp_token *);
1933 static tree cp_parser_new_expression
1934   (cp_parser *);
1935 static vec<tree, va_gc> *cp_parser_new_placement
1936   (cp_parser *);
1937 static tree cp_parser_new_type_id
1938   (cp_parser *, tree *);
1939 static cp_declarator *cp_parser_new_declarator_opt
1940   (cp_parser *);
1941 static cp_declarator *cp_parser_direct_new_declarator
1942   (cp_parser *);
1943 static vec<tree, va_gc> *cp_parser_new_initializer
1944   (cp_parser *);
1945 static tree cp_parser_delete_expression
1946   (cp_parser *);
1947 static tree cp_parser_cast_expression
1948   (cp_parser *, bool, bool, bool, cp_id_kind *);
1949 static tree cp_parser_binary_expression
1950   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1951 static tree cp_parser_question_colon_clause
1952   (cp_parser *, tree);
1953 static tree cp_parser_assignment_expression
1954   (cp_parser *, bool, cp_id_kind *);
1955 static enum tree_code cp_parser_assignment_operator_opt
1956   (cp_parser *);
1957 static tree cp_parser_expression
1958   (cp_parser *, bool, cp_id_kind *);
1959 static tree cp_parser_expression
1960   (cp_parser *, bool, bool, cp_id_kind *);
1961 static tree cp_parser_constant_expression
1962   (cp_parser *, bool, bool *);
1963 static tree cp_parser_builtin_offsetof
1964   (cp_parser *);
1965 static tree cp_parser_lambda_expression
1966   (cp_parser *);
1967 static void cp_parser_lambda_introducer
1968   (cp_parser *, tree);
1969 static bool cp_parser_lambda_declarator_opt
1970   (cp_parser *, tree);
1971 static void cp_parser_lambda_body
1972   (cp_parser *, tree);
1973 
1974 /* Statements [gram.stmt.stmt]  */
1975 
1976 static void cp_parser_statement
1977   (cp_parser *, tree, bool, bool *);
1978 static void cp_parser_label_for_labeled_statement
1979 (cp_parser *, tree);
1980 static tree cp_parser_expression_statement
1981   (cp_parser *, tree);
1982 static tree cp_parser_compound_statement
1983   (cp_parser *, tree, bool, bool);
1984 static void cp_parser_statement_seq_opt
1985   (cp_parser *, tree);
1986 static tree cp_parser_selection_statement
1987   (cp_parser *, bool *);
1988 static tree cp_parser_condition
1989   (cp_parser *);
1990 static tree cp_parser_iteration_statement
1991   (cp_parser *, bool);
1992 static bool cp_parser_for_init_statement
1993   (cp_parser *, tree *decl);
1994 static tree cp_parser_for
1995   (cp_parser *, bool);
1996 static tree cp_parser_c_for
1997   (cp_parser *, tree, tree, bool);
1998 static tree cp_parser_range_for
1999   (cp_parser *, tree, tree, tree, bool);
2000 static void do_range_for_auto_deduction
2001   (tree, tree);
2002 static tree cp_parser_perform_range_for_lookup
2003   (tree, tree *, tree *);
2004 static tree cp_parser_range_for_member_function
2005   (tree, tree);
2006 static tree cp_parser_jump_statement
2007   (cp_parser *);
2008 static void cp_parser_declaration_statement
2009   (cp_parser *);
2010 
2011 static tree cp_parser_implicitly_scoped_statement
2012   (cp_parser *, bool *);
2013 static void cp_parser_already_scoped_statement
2014   (cp_parser *);
2015 
2016 /* Declarations [gram.dcl.dcl] */
2017 
2018 static void cp_parser_declaration_seq_opt
2019   (cp_parser *);
2020 static void cp_parser_declaration
2021   (cp_parser *);
2022 static void cp_parser_block_declaration
2023   (cp_parser *, bool);
2024 static void cp_parser_simple_declaration
2025   (cp_parser *, bool, tree *);
2026 static void cp_parser_decl_specifier_seq
2027   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2028 static tree cp_parser_storage_class_specifier_opt
2029   (cp_parser *);
2030 static tree cp_parser_function_specifier_opt
2031   (cp_parser *, cp_decl_specifier_seq *);
2032 static tree cp_parser_type_specifier
2033   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2034    int *, bool *);
2035 static tree cp_parser_simple_type_specifier
2036   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2037 static tree cp_parser_type_name
2038   (cp_parser *);
2039 static tree cp_parser_nonclass_name
2040   (cp_parser* parser);
2041 static tree cp_parser_elaborated_type_specifier
2042   (cp_parser *, bool, bool);
2043 static tree cp_parser_enum_specifier
2044   (cp_parser *);
2045 static void cp_parser_enumerator_list
2046   (cp_parser *, tree);
2047 static void cp_parser_enumerator_definition
2048   (cp_parser *, tree);
2049 static tree cp_parser_namespace_name
2050   (cp_parser *);
2051 static void cp_parser_namespace_definition
2052   (cp_parser *);
2053 static void cp_parser_namespace_body
2054   (cp_parser *);
2055 static tree cp_parser_qualified_namespace_specifier
2056   (cp_parser *);
2057 static void cp_parser_namespace_alias_definition
2058   (cp_parser *);
2059 static bool cp_parser_using_declaration
2060   (cp_parser *, bool);
2061 static void cp_parser_using_directive
2062   (cp_parser *);
2063 static tree cp_parser_alias_declaration
2064   (cp_parser *);
2065 static void cp_parser_asm_definition
2066   (cp_parser *);
2067 static void cp_parser_linkage_specification
2068   (cp_parser *);
2069 static void cp_parser_static_assert
2070   (cp_parser *, bool);
2071 static tree cp_parser_decltype
2072   (cp_parser *);
2073 
2074 /* Declarators [gram.dcl.decl] */
2075 
2076 static tree cp_parser_init_declarator
2077   (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2078 static cp_declarator *cp_parser_declarator
2079   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
2080 static cp_declarator *cp_parser_direct_declarator
2081   (cp_parser *, cp_parser_declarator_kind, int *, bool);
2082 static enum tree_code cp_parser_ptr_operator
2083   (cp_parser *, tree *, cp_cv_quals *, tree *);
2084 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2085   (cp_parser *);
2086 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2087   (cp_parser *);
2088 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2089   (cp_parser *);
2090 static tree cp_parser_late_return_type_opt
2091   (cp_parser *, cp_declarator *, cp_cv_quals);
2092 static tree cp_parser_declarator_id
2093   (cp_parser *, bool);
2094 static tree cp_parser_type_id
2095   (cp_parser *);
2096 static tree cp_parser_template_type_arg
2097   (cp_parser *);
2098 static tree cp_parser_trailing_type_id (cp_parser *);
2099 static tree cp_parser_type_id_1
2100   (cp_parser *, bool, bool);
2101 static void cp_parser_type_specifier_seq
2102   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2103 static tree cp_parser_parameter_declaration_clause
2104   (cp_parser *);
2105 static tree cp_parser_parameter_declaration_list
2106   (cp_parser *, bool *);
2107 static cp_parameter_declarator *cp_parser_parameter_declaration
2108   (cp_parser *, bool, bool *);
2109 static tree cp_parser_default_argument
2110   (cp_parser *, bool);
2111 static void cp_parser_function_body
2112   (cp_parser *, bool);
2113 static tree cp_parser_initializer
2114   (cp_parser *, bool *, bool *);
2115 static tree cp_parser_initializer_clause
2116   (cp_parser *, bool *);
2117 static tree cp_parser_braced_list
2118   (cp_parser*, bool*);
2119 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2120   (cp_parser *, bool *);
2121 
2122 static bool cp_parser_ctor_initializer_opt_and_function_body
2123   (cp_parser *, bool);
2124 
2125 static tree cp_parser_late_parsing_omp_declare_simd
2126   (cp_parser *, tree);
2127 
2128 static tree cp_parser_late_parsing_cilk_simd_fn_info
2129   (cp_parser *, tree);
2130 
2131 static tree synthesize_implicit_template_parm
2132   (cp_parser *);
2133 static tree finish_fully_implicit_template
2134   (cp_parser *, tree);
2135 
2136 /* Classes [gram.class] */
2137 
2138 static tree cp_parser_class_name
2139   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2140 static tree cp_parser_class_specifier
2141   (cp_parser *);
2142 static tree cp_parser_class_head
2143   (cp_parser *, bool *);
2144 static enum tag_types cp_parser_class_key
2145   (cp_parser *);
2146 static void cp_parser_member_specification_opt
2147   (cp_parser *);
2148 static void cp_parser_member_declaration
2149   (cp_parser *);
2150 static tree cp_parser_pure_specifier
2151   (cp_parser *);
2152 static tree cp_parser_constant_initializer
2153   (cp_parser *);
2154 
2155 /* Derived classes [gram.class.derived] */
2156 
2157 static tree cp_parser_base_clause
2158   (cp_parser *);
2159 static tree cp_parser_base_specifier
2160   (cp_parser *);
2161 
2162 /* Special member functions [gram.special] */
2163 
2164 static tree cp_parser_conversion_function_id
2165   (cp_parser *);
2166 static tree cp_parser_conversion_type_id
2167   (cp_parser *);
2168 static cp_declarator *cp_parser_conversion_declarator_opt
2169   (cp_parser *);
2170 static bool cp_parser_ctor_initializer_opt
2171   (cp_parser *);
2172 static void cp_parser_mem_initializer_list
2173   (cp_parser *);
2174 static tree cp_parser_mem_initializer
2175   (cp_parser *);
2176 static tree cp_parser_mem_initializer_id
2177   (cp_parser *);
2178 
2179 /* Overloading [gram.over] */
2180 
2181 static tree cp_parser_operator_function_id
2182   (cp_parser *);
2183 static tree cp_parser_operator
2184   (cp_parser *);
2185 
2186 /* Templates [gram.temp] */
2187 
2188 static void cp_parser_template_declaration
2189   (cp_parser *, bool);
2190 static tree cp_parser_template_parameter_list
2191   (cp_parser *);
2192 static tree cp_parser_template_parameter
2193   (cp_parser *, bool *, bool *);
2194 static tree cp_parser_type_parameter
2195   (cp_parser *, bool *);
2196 static tree cp_parser_template_id
2197   (cp_parser *, bool, bool, enum tag_types, bool);
2198 static tree cp_parser_template_name
2199   (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2200 static tree cp_parser_template_argument_list
2201   (cp_parser *);
2202 static tree cp_parser_template_argument
2203   (cp_parser *);
2204 static void cp_parser_explicit_instantiation
2205   (cp_parser *);
2206 static void cp_parser_explicit_specialization
2207   (cp_parser *);
2208 
2209 /* Exception handling [gram.exception] */
2210 
2211 static tree cp_parser_try_block
2212   (cp_parser *);
2213 static bool cp_parser_function_try_block
2214   (cp_parser *);
2215 static void cp_parser_handler_seq
2216   (cp_parser *);
2217 static void cp_parser_handler
2218   (cp_parser *);
2219 static tree cp_parser_exception_declaration
2220   (cp_parser *);
2221 static tree cp_parser_throw_expression
2222   (cp_parser *);
2223 static tree cp_parser_exception_specification_opt
2224   (cp_parser *);
2225 static tree cp_parser_type_id_list
2226   (cp_parser *);
2227 
2228 /* GNU Extensions */
2229 
2230 static tree cp_parser_asm_specification_opt
2231   (cp_parser *);
2232 static tree cp_parser_asm_operand_list
2233   (cp_parser *);
2234 static tree cp_parser_asm_clobber_list
2235   (cp_parser *);
2236 static tree cp_parser_asm_label_list
2237   (cp_parser *);
2238 static bool cp_next_tokens_can_be_attribute_p
2239   (cp_parser *);
2240 static bool cp_next_tokens_can_be_gnu_attribute_p
2241   (cp_parser *);
2242 static bool cp_next_tokens_can_be_std_attribute_p
2243   (cp_parser *);
2244 static bool cp_nth_tokens_can_be_std_attribute_p
2245   (cp_parser *, size_t);
2246 static bool cp_nth_tokens_can_be_gnu_attribute_p
2247   (cp_parser *, size_t);
2248 static bool cp_nth_tokens_can_be_attribute_p
2249   (cp_parser *, size_t);
2250 static tree cp_parser_attributes_opt
2251   (cp_parser *);
2252 static tree cp_parser_gnu_attributes_opt
2253   (cp_parser *);
2254 static tree cp_parser_gnu_attribute_list
2255   (cp_parser *);
2256 static tree cp_parser_std_attribute
2257   (cp_parser *);
2258 static tree cp_parser_std_attribute_spec
2259   (cp_parser *);
2260 static tree cp_parser_std_attribute_spec_seq
2261   (cp_parser *);
2262 static bool cp_parser_extension_opt
2263   (cp_parser *, int *);
2264 static void cp_parser_label_declaration
2265   (cp_parser *);
2266 
2267 /* Transactional Memory Extensions */
2268 
2269 static tree cp_parser_transaction
2270   (cp_parser *, enum rid);
2271 static tree cp_parser_transaction_expression
2272   (cp_parser *, enum rid);
2273 static bool cp_parser_function_transaction
2274   (cp_parser *, enum rid);
2275 static tree cp_parser_transaction_cancel
2276   (cp_parser *);
2277 
2278 enum pragma_context {
2279   pragma_external,
2280   pragma_member,
2281   pragma_objc_icode,
2282   pragma_stmt,
2283   pragma_compound
2284 };
2285 static bool cp_parser_pragma
2286   (cp_parser *, enum pragma_context);
2287 
2288 /* Objective-C++ Productions */
2289 
2290 static tree cp_parser_objc_message_receiver
2291   (cp_parser *);
2292 static tree cp_parser_objc_message_args
2293   (cp_parser *);
2294 static tree cp_parser_objc_message_expression
2295   (cp_parser *);
2296 static tree cp_parser_objc_encode_expression
2297   (cp_parser *);
2298 static tree cp_parser_objc_defs_expression
2299   (cp_parser *);
2300 static tree cp_parser_objc_protocol_expression
2301   (cp_parser *);
2302 static tree cp_parser_objc_selector_expression
2303   (cp_parser *);
2304 static tree cp_parser_objc_expression
2305   (cp_parser *);
2306 static bool cp_parser_objc_selector_p
2307   (enum cpp_ttype);
2308 static tree cp_parser_objc_selector
2309   (cp_parser *);
2310 static tree cp_parser_objc_protocol_refs_opt
2311   (cp_parser *);
2312 static void cp_parser_objc_declaration
2313   (cp_parser *, tree);
2314 static tree cp_parser_objc_statement
2315   (cp_parser *);
2316 static bool cp_parser_objc_valid_prefix_attributes
2317   (cp_parser *, tree *);
2318 static void cp_parser_objc_at_property_declaration
2319   (cp_parser *) ;
2320 static void cp_parser_objc_at_synthesize_declaration
2321   (cp_parser *) ;
2322 static void cp_parser_objc_at_dynamic_declaration
2323   (cp_parser *) ;
2324 static tree cp_parser_objc_struct_declaration
2325   (cp_parser *) ;
2326 
2327 /* Utility Routines */
2328 
2329 static tree cp_parser_lookup_name
2330   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2331 static tree cp_parser_lookup_name_simple
2332   (cp_parser *, tree, location_t);
2333 static tree cp_parser_maybe_treat_template_as_class
2334   (tree, bool);
2335 static bool cp_parser_check_declarator_template_parameters
2336   (cp_parser *, cp_declarator *, location_t);
2337 static bool cp_parser_check_template_parameters
2338   (cp_parser *, unsigned, location_t, cp_declarator *);
2339 static tree cp_parser_simple_cast_expression
2340   (cp_parser *);
2341 static tree cp_parser_global_scope_opt
2342   (cp_parser *, bool);
2343 static bool cp_parser_constructor_declarator_p
2344   (cp_parser *, bool);
2345 static tree cp_parser_function_definition_from_specifiers_and_declarator
2346   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2347 static tree cp_parser_function_definition_after_declarator
2348   (cp_parser *, bool);
2349 static void cp_parser_template_declaration_after_export
2350   (cp_parser *, bool);
2351 static void cp_parser_perform_template_parameter_access_checks
2352   (vec<deferred_access_check, va_gc> *);
2353 static tree cp_parser_single_declaration
2354   (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2355 static tree cp_parser_functional_cast
2356   (cp_parser *, tree);
2357 static tree cp_parser_save_member_function_body
2358   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2359 static tree cp_parser_save_nsdmi
2360   (cp_parser *);
2361 static tree cp_parser_enclosed_template_argument_list
2362   (cp_parser *);
2363 static void cp_parser_save_default_args
2364   (cp_parser *, tree);
2365 static void cp_parser_late_parsing_for_member
2366   (cp_parser *, tree);
2367 static tree cp_parser_late_parse_one_default_arg
2368   (cp_parser *, tree, tree, tree);
2369 static void cp_parser_late_parsing_nsdmi
2370   (cp_parser *, tree);
2371 static void cp_parser_late_parsing_default_args
2372   (cp_parser *, tree);
2373 static tree cp_parser_sizeof_operand
2374   (cp_parser *, enum rid);
2375 static tree cp_parser_trait_expr
2376   (cp_parser *, enum rid);
2377 static bool cp_parser_declares_only_class_p
2378   (cp_parser *);
2379 static void cp_parser_set_storage_class
2380   (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2381 static void cp_parser_set_decl_spec_type
2382   (cp_decl_specifier_seq *, tree, cp_token *, bool);
2383 static void set_and_check_decl_spec_loc
2384   (cp_decl_specifier_seq *decl_specs,
2385    cp_decl_spec ds, cp_token *);
2386 static bool cp_parser_friend_p
2387   (const cp_decl_specifier_seq *);
2388 static void cp_parser_required_error
2389   (cp_parser *, required_token, bool);
2390 static cp_token *cp_parser_require
2391   (cp_parser *, enum cpp_ttype, required_token);
2392 static cp_token *cp_parser_require_keyword
2393   (cp_parser *, enum rid, required_token);
2394 static bool cp_parser_token_starts_function_definition_p
2395   (cp_token *);
2396 static bool cp_parser_next_token_starts_class_definition_p
2397   (cp_parser *);
2398 static bool cp_parser_next_token_ends_template_argument_p
2399   (cp_parser *);
2400 static bool cp_parser_nth_token_starts_template_argument_list_p
2401   (cp_parser *, size_t);
2402 static enum tag_types cp_parser_token_is_class_key
2403   (cp_token *);
2404 static void cp_parser_check_class_key
2405   (enum tag_types, tree type);
2406 static void cp_parser_check_access_in_redeclaration
2407   (tree type, location_t location);
2408 static bool cp_parser_optional_template_keyword
2409   (cp_parser *);
2410 static void cp_parser_pre_parsed_nested_name_specifier
2411   (cp_parser *);
2412 static bool cp_parser_cache_group
2413   (cp_parser *, enum cpp_ttype, unsigned);
2414 static tree cp_parser_cache_defarg
2415   (cp_parser *parser, bool nsdmi);
2416 static void cp_parser_parse_tentatively
2417   (cp_parser *);
2418 static void cp_parser_commit_to_tentative_parse
2419   (cp_parser *);
2420 static void cp_parser_commit_to_topmost_tentative_parse
2421   (cp_parser *);
2422 static void cp_parser_abort_tentative_parse
2423   (cp_parser *);
2424 static bool cp_parser_parse_definitely
2425   (cp_parser *);
2426 static inline bool cp_parser_parsing_tentatively
2427   (cp_parser *);
2428 static bool cp_parser_uncommitted_to_tentative_parse_p
2429   (cp_parser *);
2430 static void cp_parser_error
2431   (cp_parser *, const char *);
2432 static void cp_parser_name_lookup_error
2433   (cp_parser *, tree, tree, name_lookup_error, location_t);
2434 static bool cp_parser_simulate_error
2435   (cp_parser *);
2436 static bool cp_parser_check_type_definition
2437   (cp_parser *);
2438 static void cp_parser_check_for_definition_in_return_type
2439   (cp_declarator *, tree, location_t type_location);
2440 static void cp_parser_check_for_invalid_template_id
2441   (cp_parser *, tree, enum tag_types, location_t location);
2442 static bool cp_parser_non_integral_constant_expression
2443   (cp_parser *, non_integral_constant);
2444 static void cp_parser_diagnose_invalid_type_name
2445   (cp_parser *, tree, tree, location_t);
2446 static bool cp_parser_parse_and_diagnose_invalid_type_name
2447   (cp_parser *);
2448 static int cp_parser_skip_to_closing_parenthesis
2449   (cp_parser *, bool, bool, bool);
2450 static void cp_parser_skip_to_end_of_statement
2451   (cp_parser *);
2452 static void cp_parser_consume_semicolon_at_end_of_statement
2453   (cp_parser *);
2454 static void cp_parser_skip_to_end_of_block_or_statement
2455   (cp_parser *);
2456 static bool cp_parser_skip_to_closing_brace
2457   (cp_parser *);
2458 static void cp_parser_skip_to_end_of_template_parameter_list
2459   (cp_parser *);
2460 static void cp_parser_skip_to_pragma_eol
2461   (cp_parser*, cp_token *);
2462 static bool cp_parser_error_occurred
2463   (cp_parser *);
2464 static bool cp_parser_allow_gnu_extensions_p
2465   (cp_parser *);
2466 static bool cp_parser_is_pure_string_literal
2467   (cp_token *);
2468 static bool cp_parser_is_string_literal
2469   (cp_token *);
2470 static bool cp_parser_is_keyword
2471   (cp_token *, enum rid);
2472 static tree cp_parser_make_typename_type
2473   (cp_parser *, tree, tree, location_t location);
2474 static cp_declarator * cp_parser_make_indirect_declarator
2475  (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2476 
2477 /* Returns nonzero if we are parsing tentatively.  */
2478 
2479 static inline bool
cp_parser_parsing_tentatively(cp_parser * parser)2480 cp_parser_parsing_tentatively (cp_parser* parser)
2481 {
2482   return parser->context->next != NULL;
2483 }
2484 
2485 /* Returns nonzero if TOKEN is a string literal.  */
2486 
2487 static bool
cp_parser_is_pure_string_literal(cp_token * token)2488 cp_parser_is_pure_string_literal (cp_token* token)
2489 {
2490   return (token->type == CPP_STRING ||
2491 	  token->type == CPP_STRING16 ||
2492 	  token->type == CPP_STRING32 ||
2493 	  token->type == CPP_WSTRING ||
2494 	  token->type == CPP_UTF8STRING);
2495 }
2496 
2497 /* Returns nonzero if TOKEN is a string literal
2498    of a user-defined string literal.  */
2499 
2500 static bool
cp_parser_is_string_literal(cp_token * token)2501 cp_parser_is_string_literal (cp_token* token)
2502 {
2503   return (cp_parser_is_pure_string_literal (token) ||
2504 	  token->type == CPP_STRING_USERDEF ||
2505 	  token->type == CPP_STRING16_USERDEF ||
2506 	  token->type == CPP_STRING32_USERDEF ||
2507 	  token->type == CPP_WSTRING_USERDEF ||
2508 	  token->type == CPP_UTF8STRING_USERDEF);
2509 }
2510 
2511 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2512 
2513 static bool
cp_parser_is_keyword(cp_token * token,enum rid keyword)2514 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2515 {
2516   return token->keyword == keyword;
2517 }
2518 
2519 /* If not parsing tentatively, issue a diagnostic of the form
2520       FILE:LINE: MESSAGE before TOKEN
2521    where TOKEN is the next token in the input stream.  MESSAGE
2522    (specified by the caller) is usually of the form "expected
2523    OTHER-TOKEN".  */
2524 
2525 static void
cp_parser_error(cp_parser * parser,const char * gmsgid)2526 cp_parser_error (cp_parser* parser, const char* gmsgid)
2527 {
2528   if (!cp_parser_simulate_error (parser))
2529     {
2530       cp_token *token = cp_lexer_peek_token (parser->lexer);
2531       /* This diagnostic makes more sense if it is tagged to the line
2532 	 of the token we just peeked at.  */
2533       cp_lexer_set_source_position_from_token (token);
2534 
2535       if (token->type == CPP_PRAGMA)
2536 	{
2537 	  error_at (token->location,
2538 		    "%<#pragma%> is not allowed here");
2539 	  cp_parser_skip_to_pragma_eol (parser, token);
2540 	  return;
2541 	}
2542 
2543       c_parse_error (gmsgid,
2544 		     /* Because c_parser_error does not understand
2545 			CPP_KEYWORD, keywords are treated like
2546 			identifiers.  */
2547 		     (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2548 		     token->u.value, token->flags);
2549     }
2550 }
2551 
2552 /* Issue an error about name-lookup failing.  NAME is the
2553    IDENTIFIER_NODE DECL is the result of
2554    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2555    the thing that we hoped to find.  */
2556 
2557 static void
cp_parser_name_lookup_error(cp_parser * parser,tree name,tree decl,name_lookup_error desired,location_t location)2558 cp_parser_name_lookup_error (cp_parser* parser,
2559 			     tree name,
2560 			     tree decl,
2561 			     name_lookup_error desired,
2562 			     location_t location)
2563 {
2564   /* If name lookup completely failed, tell the user that NAME was not
2565      declared.  */
2566   if (decl == error_mark_node)
2567     {
2568       if (parser->scope && parser->scope != global_namespace)
2569 	error_at (location, "%<%E::%E%> has not been declared",
2570 		  parser->scope, name);
2571       else if (parser->scope == global_namespace)
2572 	error_at (location, "%<::%E%> has not been declared", name);
2573       else if (parser->object_scope
2574 	       && !CLASS_TYPE_P (parser->object_scope))
2575 	error_at (location, "request for member %qE in non-class type %qT",
2576 		  name, parser->object_scope);
2577       else if (parser->object_scope)
2578 	error_at (location, "%<%T::%E%> has not been declared",
2579 		  parser->object_scope, name);
2580       else
2581 	error_at (location, "%qE has not been declared", name);
2582     }
2583   else if (parser->scope && parser->scope != global_namespace)
2584     {
2585       switch (desired)
2586 	{
2587 	  case NLE_TYPE:
2588 	    error_at (location, "%<%E::%E%> is not a type",
2589 	    			parser->scope, name);
2590 	    break;
2591 	  case NLE_CXX98:
2592 	    error_at (location, "%<%E::%E%> is not a class or namespace",
2593 	    			parser->scope, name);
2594 	    break;
2595 	  case NLE_NOT_CXX98:
2596 	    error_at (location,
2597 	    	      "%<%E::%E%> is not a class, namespace, or enumeration",
2598 		      parser->scope, name);
2599 	    break;
2600 	  default:
2601 	    gcc_unreachable ();
2602 
2603 	}
2604     }
2605   else if (parser->scope == global_namespace)
2606     {
2607       switch (desired)
2608 	{
2609 	  case NLE_TYPE:
2610 	    error_at (location, "%<::%E%> is not a type", name);
2611 	    break;
2612 	  case NLE_CXX98:
2613 	    error_at (location, "%<::%E%> is not a class or namespace", name);
2614 	    break;
2615 	  case NLE_NOT_CXX98:
2616 	    error_at (location,
2617 		      "%<::%E%> is not a class, namespace, or enumeration",
2618 		      name);
2619 	    break;
2620 	  default:
2621 	    gcc_unreachable ();
2622 	}
2623     }
2624   else
2625     {
2626       switch (desired)
2627 	{
2628 	  case NLE_TYPE:
2629 	    error_at (location, "%qE is not a type", name);
2630 	    break;
2631 	  case NLE_CXX98:
2632 	    error_at (location, "%qE is not a class or namespace", name);
2633 	    break;
2634 	  case NLE_NOT_CXX98:
2635 	    error_at (location,
2636 		      "%qE is not a class, namespace, or enumeration", name);
2637 	    break;
2638 	  default:
2639 	    gcc_unreachable ();
2640 	}
2641     }
2642 }
2643 
2644 /* If we are parsing tentatively, remember that an error has occurred
2645    during this tentative parse.  Returns true if the error was
2646    simulated; false if a message should be issued by the caller.  */
2647 
2648 static bool
cp_parser_simulate_error(cp_parser * parser)2649 cp_parser_simulate_error (cp_parser* parser)
2650 {
2651   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2652     {
2653       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2654       return true;
2655     }
2656   return false;
2657 }
2658 
2659 /* This function is called when a type is defined.  If type
2660    definitions are forbidden at this point, an error message is
2661    issued.  */
2662 
2663 static bool
cp_parser_check_type_definition(cp_parser * parser)2664 cp_parser_check_type_definition (cp_parser* parser)
2665 {
2666   /* If types are forbidden here, issue a message.  */
2667   if (parser->type_definition_forbidden_message)
2668     {
2669       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2670 	 in the message need to be interpreted.  */
2671       error (parser->type_definition_forbidden_message);
2672       return false;
2673     }
2674   return true;
2675 }
2676 
2677 /* This function is called when the DECLARATOR is processed.  The TYPE
2678    was a type defined in the decl-specifiers.  If it is invalid to
2679    define a type in the decl-specifiers for DECLARATOR, an error is
2680    issued. TYPE_LOCATION is the location of TYPE and is used
2681    for error reporting.  */
2682 
2683 static void
cp_parser_check_for_definition_in_return_type(cp_declarator * declarator,tree type,location_t type_location)2684 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2685 					       tree type, location_t type_location)
2686 {
2687   /* [dcl.fct] forbids type definitions in return types.
2688      Unfortunately, it's not easy to know whether or not we are
2689      processing a return type until after the fact.  */
2690   while (declarator
2691 	 && (declarator->kind == cdk_pointer
2692 	     || declarator->kind == cdk_reference
2693 	     || declarator->kind == cdk_ptrmem))
2694     declarator = declarator->declarator;
2695   if (declarator
2696       && declarator->kind == cdk_function)
2697     {
2698       error_at (type_location,
2699 		"new types may not be defined in a return type");
2700       inform (type_location,
2701 	      "(perhaps a semicolon is missing after the definition of %qT)",
2702 	      type);
2703     }
2704 }
2705 
2706 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2707    "<" in any valid C++ program.  If the next token is indeed "<",
2708    issue a message warning the user about what appears to be an
2709    invalid attempt to form a template-id. LOCATION is the location
2710    of the type-specifier (TYPE) */
2711 
2712 static void
cp_parser_check_for_invalid_template_id(cp_parser * parser,tree type,enum tag_types tag_type,location_t location)2713 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2714 					 tree type,
2715 					 enum tag_types tag_type,
2716 					 location_t location)
2717 {
2718   cp_token_position start = 0;
2719 
2720   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2721     {
2722       if (TYPE_P (type))
2723 	error_at (location, "%qT is not a template", type);
2724       else if (identifier_p (type))
2725 	{
2726 	  if (tag_type != none_type)
2727 	    error_at (location, "%qE is not a class template", type);
2728 	  else
2729 	    error_at (location, "%qE is not a template", type);
2730 	}
2731       else
2732 	error_at (location, "invalid template-id");
2733       /* Remember the location of the invalid "<".  */
2734       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2735 	start = cp_lexer_token_position (parser->lexer, true);
2736       /* Consume the "<".  */
2737       cp_lexer_consume_token (parser->lexer);
2738       /* Parse the template arguments.  */
2739       cp_parser_enclosed_template_argument_list (parser);
2740       /* Permanently remove the invalid template arguments so that
2741 	 this error message is not issued again.  */
2742       if (start)
2743 	cp_lexer_purge_tokens_after (parser->lexer, start);
2744     }
2745 }
2746 
2747 /* If parsing an integral constant-expression, issue an error message
2748    about the fact that THING appeared and return true.  Otherwise,
2749    return false.  In either case, set
2750    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2751 
2752 static bool
cp_parser_non_integral_constant_expression(cp_parser * parser,non_integral_constant thing)2753 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2754 					    non_integral_constant thing)
2755 {
2756   parser->non_integral_constant_expression_p = true;
2757   if (parser->integral_constant_expression_p)
2758     {
2759       if (!parser->allow_non_integral_constant_expression_p)
2760 	{
2761 	  const char *msg = NULL;
2762 	  switch (thing)
2763 	    {
2764   	      case NIC_FLOAT:
2765 		error ("floating-point literal "
2766 		       "cannot appear in a constant-expression");
2767 		return true;
2768 	      case NIC_CAST:
2769 		error ("a cast to a type other than an integral or "
2770 		       "enumeration type cannot appear in a "
2771 		       "constant-expression");
2772 		return true;
2773 	      case NIC_TYPEID:
2774 		error ("%<typeid%> operator "
2775 		       "cannot appear in a constant-expression");
2776 		return true;
2777 	      case NIC_NCC:
2778 		error ("non-constant compound literals "
2779 		       "cannot appear in a constant-expression");
2780 		return true;
2781 	      case NIC_FUNC_CALL:
2782 		error ("a function call "
2783 		       "cannot appear in a constant-expression");
2784 		return true;
2785 	      case NIC_INC:
2786 		error ("an increment "
2787 		       "cannot appear in a constant-expression");
2788 		return true;
2789 	      case NIC_DEC:
2790 		error ("an decrement "
2791 		       "cannot appear in a constant-expression");
2792 		return true;
2793 	      case NIC_ARRAY_REF:
2794 		error ("an array reference "
2795 		       "cannot appear in a constant-expression");
2796 		return true;
2797 	      case NIC_ADDR_LABEL:
2798 		error ("the address of a label "
2799 		       "cannot appear in a constant-expression");
2800 		return true;
2801 	      case NIC_OVERLOADED:
2802 		error ("calls to overloaded operators "
2803 		       "cannot appear in a constant-expression");
2804 		return true;
2805 	      case NIC_ASSIGNMENT:
2806 		error ("an assignment cannot appear in a constant-expression");
2807 		return true;
2808 	      case NIC_COMMA:
2809 		error ("a comma operator "
2810 		       "cannot appear in a constant-expression");
2811 		return true;
2812 	      case NIC_CONSTRUCTOR:
2813 		error ("a call to a constructor "
2814 		       "cannot appear in a constant-expression");
2815 		return true;
2816 	      case NIC_TRANSACTION:
2817 		error ("a transaction expression "
2818 		       "cannot appear in a constant-expression");
2819 		return true;
2820 	      case NIC_THIS:
2821 		msg = "this";
2822 		break;
2823 	      case NIC_FUNC_NAME:
2824 		msg = "__FUNCTION__";
2825 		break;
2826   	      case NIC_PRETTY_FUNC:
2827 		msg = "__PRETTY_FUNCTION__";
2828 		break;
2829 	      case NIC_C99_FUNC:
2830 		msg = "__func__";
2831 		break;
2832 	      case NIC_VA_ARG:
2833 		msg = "va_arg";
2834 		break;
2835 	      case NIC_ARROW:
2836 		msg = "->";
2837 		break;
2838 	      case NIC_POINT:
2839 		msg = ".";
2840 		break;
2841 	      case NIC_STAR:
2842 		msg = "*";
2843 		break;
2844 	      case NIC_ADDR:
2845 		msg = "&";
2846 		break;
2847 	      case NIC_PREINCREMENT:
2848 		msg = "++";
2849 		break;
2850 	      case NIC_PREDECREMENT:
2851 		msg = "--";
2852 		break;
2853 	      case NIC_NEW:
2854 		msg = "new";
2855 		break;
2856 	      case NIC_DEL:
2857 		msg = "delete";
2858 		break;
2859 	      default:
2860 		gcc_unreachable ();
2861 	    }
2862 	  if (msg)
2863 	    error ("%qs cannot appear in a constant-expression", msg);
2864 	  return true;
2865 	}
2866     }
2867   return false;
2868 }
2869 
2870 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2871    qualifying scope (or NULL, if none) for ID.  This function commits
2872    to the current active tentative parse, if any.  (Otherwise, the
2873    problematic construct might be encountered again later, resulting
2874    in duplicate error messages.) LOCATION is the location of ID.  */
2875 
2876 static void
cp_parser_diagnose_invalid_type_name(cp_parser * parser,tree scope,tree id,location_t location)2877 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2878 				      tree scope, tree id,
2879 				      location_t location)
2880 {
2881   tree decl, old_scope;
2882   cp_parser_commit_to_tentative_parse (parser);
2883   /* Try to lookup the identifier.  */
2884   old_scope = parser->scope;
2885   parser->scope = scope;
2886   decl = cp_parser_lookup_name_simple (parser, id, location);
2887   parser->scope = old_scope;
2888   /* If the lookup found a template-name, it means that the user forgot
2889   to specify an argument list. Emit a useful error message.  */
2890   if (TREE_CODE (decl) == TEMPLATE_DECL)
2891     error_at (location,
2892 	      "invalid use of template-name %qE without an argument list",
2893 	      decl);
2894   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2895     error_at (location, "invalid use of destructor %qD as a type", id);
2896   else if (TREE_CODE (decl) == TYPE_DECL)
2897     /* Something like 'unsigned A a;'  */
2898     error_at (location, "invalid combination of multiple type-specifiers");
2899   else if (!parser->scope)
2900     {
2901       /* Issue an error message.  */
2902       error_at (location, "%qE does not name a type", id);
2903       /* If we're in a template class, it's possible that the user was
2904 	 referring to a type from a base class.  For example:
2905 
2906 	   template <typename T> struct A { typedef T X; };
2907 	   template <typename T> struct B : public A<T> { X x; };
2908 
2909 	 The user should have said "typename A<T>::X".  */
2910       if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2911 	inform (location, "C++11 %<constexpr%> only available with "
2912 		"-std=c++11 or -std=gnu++11");
2913       else if (processing_template_decl && current_class_type
2914 	       && TYPE_BINFO (current_class_type))
2915 	{
2916 	  tree b;
2917 
2918 	  for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2919 	       b;
2920 	       b = TREE_CHAIN (b))
2921 	    {
2922 	      tree base_type = BINFO_TYPE (b);
2923 	      if (CLASS_TYPE_P (base_type)
2924 		  && dependent_type_p (base_type))
2925 		{
2926 		  tree field;
2927 		  /* Go from a particular instantiation of the
2928 		     template (which will have an empty TYPE_FIELDs),
2929 		     to the main version.  */
2930 		  base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2931 		  for (field = TYPE_FIELDS (base_type);
2932 		       field;
2933 		       field = DECL_CHAIN (field))
2934 		    if (TREE_CODE (field) == TYPE_DECL
2935 			&& DECL_NAME (field) == id)
2936 		      {
2937 			inform (location,
2938 				"(perhaps %<typename %T::%E%> was intended)",
2939 				BINFO_TYPE (b), id);
2940 			break;
2941 		      }
2942 		  if (field)
2943 		    break;
2944 		}
2945 	    }
2946 	}
2947     }
2948   /* Here we diagnose qualified-ids where the scope is actually correct,
2949      but the identifier does not resolve to a valid type name.  */
2950   else if (parser->scope != error_mark_node)
2951     {
2952       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2953 	{
2954 	  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2955 	    error_at (location_of (id),
2956 		      "%qE in namespace %qE does not name a template type",
2957 		      id, parser->scope);
2958 	  else
2959 	    error_at (location_of (id),
2960 		      "%qE in namespace %qE does not name a type",
2961 		      id, parser->scope);
2962 	}
2963       else if (CLASS_TYPE_P (parser->scope)
2964 	       && constructor_name_p (id, parser->scope))
2965 	{
2966 	  /* A<T>::A<T>() */
2967 	  error_at (location, "%<%T::%E%> names the constructor, not"
2968 		    " the type", parser->scope, id);
2969 	  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2970 	    error_at (location, "and %qT has no template constructors",
2971 		      parser->scope);
2972 	}
2973       else if (TYPE_P (parser->scope)
2974 	       && dependent_scope_p (parser->scope))
2975 	error_at (location, "need %<typename%> before %<%T::%E%> because "
2976 		  "%qT is a dependent scope",
2977 		  parser->scope, id, parser->scope);
2978       else if (TYPE_P (parser->scope))
2979 	{
2980 	  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2981 	    error_at (location_of (id),
2982 		      "%qE in %q#T does not name a template type",
2983 		      id, parser->scope);
2984 	  else
2985 	    error_at (location_of (id),
2986 		      "%qE in %q#T does not name a type",
2987 		      id, parser->scope);
2988 	}
2989       else
2990 	gcc_unreachable ();
2991     }
2992 }
2993 
2994 /* Check for a common situation where a type-name should be present,
2995    but is not, and issue a sensible error message.  Returns true if an
2996    invalid type-name was detected.
2997 
2998    The situation handled by this function are variable declarations of the
2999    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3000    Usually, `ID' should name a type, but if we got here it means that it
3001    does not. We try to emit the best possible error message depending on
3002    how exactly the id-expression looks like.  */
3003 
3004 static bool
cp_parser_parse_and_diagnose_invalid_type_name(cp_parser * parser)3005 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3006 {
3007   tree id;
3008   cp_token *token = cp_lexer_peek_token (parser->lexer);
3009 
3010   /* Avoid duplicate error about ambiguous lookup.  */
3011   if (token->type == CPP_NESTED_NAME_SPECIFIER)
3012     {
3013       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3014       if (next->type == CPP_NAME && next->ambiguous_p)
3015 	goto out;
3016     }
3017 
3018   cp_parser_parse_tentatively (parser);
3019   id = cp_parser_id_expression (parser,
3020 				/*template_keyword_p=*/false,
3021 				/*check_dependency_p=*/true,
3022 				/*template_p=*/NULL,
3023 				/*declarator_p=*/true,
3024 				/*optional_p=*/false);
3025   /* If the next token is a (, this is a function with no explicit return
3026      type, i.e. constructor, destructor or conversion op.  */
3027   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3028       || TREE_CODE (id) == TYPE_DECL)
3029     {
3030       cp_parser_abort_tentative_parse (parser);
3031       return false;
3032     }
3033   if (!cp_parser_parse_definitely (parser))
3034     return false;
3035 
3036   /* Emit a diagnostic for the invalid type.  */
3037   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
3038 					id, token->location);
3039  out:
3040   /* If we aren't in the middle of a declarator (i.e. in a
3041      parameter-declaration-clause), skip to the end of the declaration;
3042      there's no point in trying to process it.  */
3043   if (!parser->in_declarator_p)
3044     cp_parser_skip_to_end_of_block_or_statement (parser);
3045   return true;
3046 }
3047 
3048 /* Consume tokens up to, and including, the next non-nested closing `)'.
3049    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3050    are doing error recovery. Returns -1 if OR_COMMA is true and we
3051    found an unnested comma.  */
3052 
3053 static int
cp_parser_skip_to_closing_parenthesis(cp_parser * parser,bool recovering,bool or_comma,bool consume_paren)3054 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3055 				       bool recovering,
3056 				       bool or_comma,
3057 				       bool consume_paren)
3058 {
3059   unsigned paren_depth = 0;
3060   unsigned brace_depth = 0;
3061   unsigned square_depth = 0;
3062 
3063   if (recovering && !or_comma
3064       && cp_parser_uncommitted_to_tentative_parse_p (parser))
3065     return 0;
3066 
3067   while (true)
3068     {
3069       cp_token * token = cp_lexer_peek_token (parser->lexer);
3070 
3071       switch (token->type)
3072 	{
3073 	case CPP_EOF:
3074 	case CPP_PRAGMA_EOL:
3075 	  /* If we've run out of tokens, then there is no closing `)'.  */
3076 	  return 0;
3077 
3078         /* This is good for lambda expression capture-lists.  */
3079         case CPP_OPEN_SQUARE:
3080           ++square_depth;
3081           break;
3082         case CPP_CLOSE_SQUARE:
3083           if (!square_depth--)
3084             return 0;
3085           break;
3086 
3087 	case CPP_SEMICOLON:
3088 	  /* This matches the processing in skip_to_end_of_statement.  */
3089 	  if (!brace_depth)
3090 	    return 0;
3091 	  break;
3092 
3093 	case CPP_OPEN_BRACE:
3094 	  ++brace_depth;
3095 	  break;
3096 	case CPP_CLOSE_BRACE:
3097 	  if (!brace_depth--)
3098 	    return 0;
3099 	  break;
3100 
3101 	case CPP_COMMA:
3102 	  if (recovering && or_comma && !brace_depth && !paren_depth
3103 	      && !square_depth)
3104 	    return -1;
3105 	  break;
3106 
3107 	case CPP_OPEN_PAREN:
3108 	  if (!brace_depth)
3109 	    ++paren_depth;
3110 	  break;
3111 
3112 	case CPP_CLOSE_PAREN:
3113 	  if (!brace_depth && !paren_depth--)
3114 	    {
3115 	      if (consume_paren)
3116 		cp_lexer_consume_token (parser->lexer);
3117 	      return 1;
3118 	    }
3119 	  break;
3120 
3121 	default:
3122 	  break;
3123 	}
3124 
3125       /* Consume the token.  */
3126       cp_lexer_consume_token (parser->lexer);
3127     }
3128 }
3129 
3130 /* Consume tokens until we reach the end of the current statement.
3131    Normally, that will be just before consuming a `;'.  However, if a
3132    non-nested `}' comes first, then we stop before consuming that.  */
3133 
3134 static void
cp_parser_skip_to_end_of_statement(cp_parser * parser)3135 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3136 {
3137   unsigned nesting_depth = 0;
3138 
3139   /* Unwind generic function template scope if necessary.  */
3140   if (parser->fully_implicit_function_template_p)
3141     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3142 
3143   while (true)
3144     {
3145       cp_token *token = cp_lexer_peek_token (parser->lexer);
3146 
3147       switch (token->type)
3148 	{
3149 	case CPP_EOF:
3150 	case CPP_PRAGMA_EOL:
3151 	  /* If we've run out of tokens, stop.  */
3152 	  return;
3153 
3154 	case CPP_SEMICOLON:
3155 	  /* If the next token is a `;', we have reached the end of the
3156 	     statement.  */
3157 	  if (!nesting_depth)
3158 	    return;
3159 	  break;
3160 
3161 	case CPP_CLOSE_BRACE:
3162 	  /* If this is a non-nested '}', stop before consuming it.
3163 	     That way, when confronted with something like:
3164 
3165 	       { 3 + }
3166 
3167 	     we stop before consuming the closing '}', even though we
3168 	     have not yet reached a `;'.  */
3169 	  if (nesting_depth == 0)
3170 	    return;
3171 
3172 	  /* If it is the closing '}' for a block that we have
3173 	     scanned, stop -- but only after consuming the token.
3174 	     That way given:
3175 
3176 		void f g () { ... }
3177 		typedef int I;
3178 
3179 	     we will stop after the body of the erroneously declared
3180 	     function, but before consuming the following `typedef'
3181 	     declaration.  */
3182 	  if (--nesting_depth == 0)
3183 	    {
3184 	      cp_lexer_consume_token (parser->lexer);
3185 	      return;
3186 	    }
3187 
3188 	case CPP_OPEN_BRACE:
3189 	  ++nesting_depth;
3190 	  break;
3191 
3192 	default:
3193 	  break;
3194 	}
3195 
3196       /* Consume the token.  */
3197       cp_lexer_consume_token (parser->lexer);
3198     }
3199 }
3200 
3201 /* This function is called at the end of a statement or declaration.
3202    If the next token is a semicolon, it is consumed; otherwise, error
3203    recovery is attempted.  */
3204 
3205 static void
cp_parser_consume_semicolon_at_end_of_statement(cp_parser * parser)3206 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3207 {
3208   /* Look for the trailing `;'.  */
3209   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3210     {
3211       /* If there is additional (erroneous) input, skip to the end of
3212 	 the statement.  */
3213       cp_parser_skip_to_end_of_statement (parser);
3214       /* If the next token is now a `;', consume it.  */
3215       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3216 	cp_lexer_consume_token (parser->lexer);
3217     }
3218 }
3219 
3220 /* Skip tokens until we have consumed an entire block, or until we
3221    have consumed a non-nested `;'.  */
3222 
3223 static void
cp_parser_skip_to_end_of_block_or_statement(cp_parser * parser)3224 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3225 {
3226   int nesting_depth = 0;
3227 
3228   /* Unwind generic function template scope if necessary.  */
3229   if (parser->fully_implicit_function_template_p)
3230     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3231 
3232   while (nesting_depth >= 0)
3233     {
3234       cp_token *token = cp_lexer_peek_token (parser->lexer);
3235 
3236       switch (token->type)
3237 	{
3238 	case CPP_EOF:
3239 	case CPP_PRAGMA_EOL:
3240 	  /* If we've run out of tokens, stop.  */
3241 	  return;
3242 
3243 	case CPP_SEMICOLON:
3244 	  /* Stop if this is an unnested ';'. */
3245 	  if (!nesting_depth)
3246 	    nesting_depth = -1;
3247 	  break;
3248 
3249 	case CPP_CLOSE_BRACE:
3250 	  /* Stop if this is an unnested '}', or closes the outermost
3251 	     nesting level.  */
3252 	  nesting_depth--;
3253 	  if (nesting_depth < 0)
3254 	    return;
3255 	  if (!nesting_depth)
3256 	    nesting_depth = -1;
3257 	  break;
3258 
3259 	case CPP_OPEN_BRACE:
3260 	  /* Nest. */
3261 	  nesting_depth++;
3262 	  break;
3263 
3264 	default:
3265 	  break;
3266 	}
3267 
3268       /* Consume the token.  */
3269       cp_lexer_consume_token (parser->lexer);
3270     }
3271 }
3272 
3273 /* Skip tokens until a non-nested closing curly brace is the next
3274    token, or there are no more tokens. Return true in the first case,
3275    false otherwise.  */
3276 
3277 static bool
cp_parser_skip_to_closing_brace(cp_parser * parser)3278 cp_parser_skip_to_closing_brace (cp_parser *parser)
3279 {
3280   unsigned nesting_depth = 0;
3281 
3282   while (true)
3283     {
3284       cp_token *token = cp_lexer_peek_token (parser->lexer);
3285 
3286       switch (token->type)
3287 	{
3288 	case CPP_EOF:
3289 	case CPP_PRAGMA_EOL:
3290 	  /* If we've run out of tokens, stop.  */
3291 	  return false;
3292 
3293 	case CPP_CLOSE_BRACE:
3294 	  /* If the next token is a non-nested `}', then we have reached
3295 	     the end of the current block.  */
3296 	  if (nesting_depth-- == 0)
3297 	    return true;
3298 	  break;
3299 
3300 	case CPP_OPEN_BRACE:
3301 	  /* If it the next token is a `{', then we are entering a new
3302 	     block.  Consume the entire block.  */
3303 	  ++nesting_depth;
3304 	  break;
3305 
3306 	default:
3307 	  break;
3308 	}
3309 
3310       /* Consume the token.  */
3311       cp_lexer_consume_token (parser->lexer);
3312     }
3313 }
3314 
3315 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3316    parameter is the PRAGMA token, allowing us to purge the entire pragma
3317    sequence.  */
3318 
3319 static void
cp_parser_skip_to_pragma_eol(cp_parser * parser,cp_token * pragma_tok)3320 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3321 {
3322   cp_token *token;
3323 
3324   parser->lexer->in_pragma = false;
3325 
3326   do
3327     token = cp_lexer_consume_token (parser->lexer);
3328   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3329 
3330   /* Ensure that the pragma is not parsed again.  */
3331   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3332 }
3333 
3334 /* Require pragma end of line, resyncing with it as necessary.  The
3335    arguments are as for cp_parser_skip_to_pragma_eol.  */
3336 
3337 static void
cp_parser_require_pragma_eol(cp_parser * parser,cp_token * pragma_tok)3338 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3339 {
3340   parser->lexer->in_pragma = false;
3341   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3342     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3343 }
3344 
3345 /* This is a simple wrapper around make_typename_type. When the id is
3346    an unresolved identifier node, we can provide a superior diagnostic
3347    using cp_parser_diagnose_invalid_type_name.  */
3348 
3349 static tree
cp_parser_make_typename_type(cp_parser * parser,tree scope,tree id,location_t id_location)3350 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3351 			      tree id, location_t id_location)
3352 {
3353   tree result;
3354   if (identifier_p (id))
3355     {
3356       result = make_typename_type (scope, id, typename_type,
3357 				   /*complain=*/tf_none);
3358       if (result == error_mark_node)
3359 	cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3360       return result;
3361     }
3362   return make_typename_type (scope, id, typename_type, tf_error);
3363 }
3364 
3365 /* This is a wrapper around the
3366    make_{pointer,ptrmem,reference}_declarator functions that decides
3367    which one to call based on the CODE and CLASS_TYPE arguments. The
3368    CODE argument should be one of the values returned by
3369    cp_parser_ptr_operator.  ATTRIBUTES represent the attributes that
3370    appertain to the pointer or reference.  */
3371 
3372 static cp_declarator *
cp_parser_make_indirect_declarator(enum tree_code code,tree class_type,cp_cv_quals cv_qualifiers,cp_declarator * target,tree attributes)3373 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3374 				    cp_cv_quals cv_qualifiers,
3375 				    cp_declarator *target,
3376 				    tree attributes)
3377 {
3378   if (code == ERROR_MARK)
3379     return cp_error_declarator;
3380 
3381   if (code == INDIRECT_REF)
3382     if (class_type == NULL_TREE)
3383       return make_pointer_declarator (cv_qualifiers, target, attributes);
3384     else
3385       return make_ptrmem_declarator (cv_qualifiers, class_type,
3386 				     target, attributes);
3387   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3388     return make_reference_declarator (cv_qualifiers, target,
3389 				      false, attributes);
3390   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3391     return make_reference_declarator (cv_qualifiers, target,
3392 				      true, attributes);
3393   gcc_unreachable ();
3394 }
3395 
3396 /* Create a new C++ parser.  */
3397 
3398 static cp_parser *
cp_parser_new(void)3399 cp_parser_new (void)
3400 {
3401   cp_parser *parser;
3402   cp_lexer *lexer;
3403   unsigned i;
3404 
3405   /* cp_lexer_new_main is called before doing GC allocation because
3406      cp_lexer_new_main might load a PCH file.  */
3407   lexer = cp_lexer_new_main ();
3408 
3409   /* Initialize the binops_by_token so that we can get the tree
3410      directly from the token.  */
3411   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3412     binops_by_token[binops[i].token_type] = binops[i];
3413 
3414   parser = ggc_alloc_cleared_cp_parser ();
3415   parser->lexer = lexer;
3416   parser->context = cp_parser_context_new (NULL);
3417 
3418   /* For now, we always accept GNU extensions.  */
3419   parser->allow_gnu_extensions_p = 1;
3420 
3421   /* The `>' token is a greater-than operator, not the end of a
3422      template-id.  */
3423   parser->greater_than_is_operator_p = true;
3424 
3425   parser->default_arg_ok_p = true;
3426 
3427   /* We are not parsing a constant-expression.  */
3428   parser->integral_constant_expression_p = false;
3429   parser->allow_non_integral_constant_expression_p = false;
3430   parser->non_integral_constant_expression_p = false;
3431 
3432   /* Local variable names are not forbidden.  */
3433   parser->local_variables_forbidden_p = false;
3434 
3435   /* We are not processing an `extern "C"' declaration.  */
3436   parser->in_unbraced_linkage_specification_p = false;
3437 
3438   /* We are not processing a declarator.  */
3439   parser->in_declarator_p = false;
3440 
3441   /* We are not processing a template-argument-list.  */
3442   parser->in_template_argument_list_p = false;
3443 
3444   /* We are not in an iteration statement.  */
3445   parser->in_statement = 0;
3446 
3447   /* We are not in a switch statement.  */
3448   parser->in_switch_statement_p = false;
3449 
3450   /* We are not parsing a type-id inside an expression.  */
3451   parser->in_type_id_in_expr_p = false;
3452 
3453   /* Declarations aren't implicitly extern "C".  */
3454   parser->implicit_extern_c = false;
3455 
3456   /* String literals should be translated to the execution character set.  */
3457   parser->translate_strings_p = true;
3458 
3459   /* We are not parsing a function body.  */
3460   parser->in_function_body = false;
3461 
3462   /* We can correct until told otherwise.  */
3463   parser->colon_corrects_to_scope_p = true;
3464 
3465   /* The unparsed function queue is empty.  */
3466   push_unparsed_function_queues (parser);
3467 
3468   /* There are no classes being defined.  */
3469   parser->num_classes_being_defined = 0;
3470 
3471   /* No template parameters apply.  */
3472   parser->num_template_parameter_lists = 0;
3473 
3474   /* Not declaring an implicit function template.  */
3475   parser->auto_is_implicit_function_template_parm_p = false;
3476   parser->fully_implicit_function_template_p = false;
3477   parser->implicit_template_parms = 0;
3478   parser->implicit_template_scope = 0;
3479 
3480   return parser;
3481 }
3482 
3483 /* Create a cp_lexer structure which will emit the tokens in CACHE
3484    and push it onto the parser's lexer stack.  This is used for delayed
3485    parsing of in-class method bodies and default arguments, and should
3486    not be confused with tentative parsing.  */
3487 static void
cp_parser_push_lexer_for_tokens(cp_parser * parser,cp_token_cache * cache)3488 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3489 {
3490   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3491   lexer->next = parser->lexer;
3492   parser->lexer = lexer;
3493 
3494   /* Move the current source position to that of the first token in the
3495      new lexer.  */
3496   cp_lexer_set_source_position_from_token (lexer->next_token);
3497 }
3498 
3499 /* Pop the top lexer off the parser stack.  This is never used for the
3500    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3501 static void
cp_parser_pop_lexer(cp_parser * parser)3502 cp_parser_pop_lexer (cp_parser *parser)
3503 {
3504   cp_lexer *lexer = parser->lexer;
3505   parser->lexer = lexer->next;
3506   cp_lexer_destroy (lexer);
3507 
3508   /* Put the current source position back where it was before this
3509      lexer was pushed.  */
3510   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3511 }
3512 
3513 /* Lexical conventions [gram.lex]  */
3514 
3515 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3516    identifier.  */
3517 
3518 static tree
cp_parser_identifier(cp_parser * parser)3519 cp_parser_identifier (cp_parser* parser)
3520 {
3521   cp_token *token;
3522 
3523   /* Look for the identifier.  */
3524   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3525   /* Return the value.  */
3526   return token ? token->u.value : error_mark_node;
3527 }
3528 
3529 /* Parse a sequence of adjacent string constants.  Returns a
3530    TREE_STRING representing the combined, nul-terminated string
3531    constant.  If TRANSLATE is true, translate the string to the
3532    execution character set.  If WIDE_OK is true, a wide string is
3533    invalid here.
3534 
3535    C++98 [lex.string] says that if a narrow string literal token is
3536    adjacent to a wide string literal token, the behavior is undefined.
3537    However, C99 6.4.5p4 says that this results in a wide string literal.
3538    We follow C99 here, for consistency with the C front end.
3539 
3540    This code is largely lifted from lex_string() in c-lex.c.
3541 
3542    FUTURE: ObjC++ will need to handle @-strings here.  */
3543 static tree
cp_parser_string_literal(cp_parser * parser,bool translate,bool wide_ok)3544 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3545 {
3546   tree value;
3547   size_t count;
3548   struct obstack str_ob;
3549   cpp_string str, istr, *strs;
3550   cp_token *tok;
3551   enum cpp_ttype type, curr_type;
3552   int have_suffix_p = 0;
3553   tree string_tree;
3554   tree suffix_id = NULL_TREE;
3555   bool curr_tok_is_userdef_p = false;
3556 
3557   tok = cp_lexer_peek_token (parser->lexer);
3558   if (!cp_parser_is_string_literal (tok))
3559     {
3560       cp_parser_error (parser, "expected string-literal");
3561       return error_mark_node;
3562     }
3563 
3564   if (cpp_userdef_string_p (tok->type))
3565     {
3566       string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3567       curr_type = cpp_userdef_string_remove_type (tok->type);
3568       curr_tok_is_userdef_p = true;
3569     }
3570   else
3571     {
3572       string_tree = tok->u.value;
3573       curr_type = tok->type;
3574     }
3575   type = curr_type;
3576 
3577   /* Try to avoid the overhead of creating and destroying an obstack
3578      for the common case of just one string.  */
3579   if (!cp_parser_is_string_literal
3580       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3581     {
3582       cp_lexer_consume_token (parser->lexer);
3583 
3584       str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3585       str.len = TREE_STRING_LENGTH (string_tree);
3586       count = 1;
3587 
3588       if (curr_tok_is_userdef_p)
3589 	{
3590 	  suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3591 	  have_suffix_p = 1;
3592 	  curr_type = cpp_userdef_string_remove_type (tok->type);
3593 	}
3594       else
3595 	curr_type = tok->type;
3596 
3597       strs = &str;
3598     }
3599   else
3600     {
3601       gcc_obstack_init (&str_ob);
3602       count = 0;
3603 
3604       do
3605 	{
3606 	  cp_lexer_consume_token (parser->lexer);
3607 	  count++;
3608 	  str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3609 	  str.len = TREE_STRING_LENGTH (string_tree);
3610 
3611 	  if (curr_tok_is_userdef_p)
3612 	    {
3613 	      tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3614 	      if (have_suffix_p == 0)
3615 		{
3616 		  suffix_id = curr_suffix_id;
3617 		  have_suffix_p = 1;
3618 		}
3619 	      else if (have_suffix_p == 1
3620 		       && curr_suffix_id != suffix_id)
3621 		{
3622 		  error ("inconsistent user-defined literal suffixes"
3623 			 " %qD and %qD in string literal",
3624 			 suffix_id, curr_suffix_id);
3625 		  have_suffix_p = -1;
3626 		}
3627 	      curr_type = cpp_userdef_string_remove_type (tok->type);
3628 	    }
3629 	  else
3630 	    curr_type = tok->type;
3631 
3632 	  if (type != curr_type)
3633 	    {
3634 	      if (type == CPP_STRING)
3635 		type = curr_type;
3636 	      else if (curr_type != CPP_STRING)
3637 		error_at (tok->location,
3638 			  "unsupported non-standard concatenation "
3639 			  "of string literals");
3640 	    }
3641 
3642 	  obstack_grow (&str_ob, &str, sizeof (cpp_string));
3643 
3644 	  tok = cp_lexer_peek_token (parser->lexer);
3645 	  if (cpp_userdef_string_p (tok->type))
3646 	    {
3647 	      string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3648 	      curr_type = cpp_userdef_string_remove_type (tok->type);
3649 	      curr_tok_is_userdef_p = true;
3650 	    }
3651 	  else
3652 	    {
3653 	      string_tree = tok->u.value;
3654 	      curr_type = tok->type;
3655 	      curr_tok_is_userdef_p = false;
3656 	    }
3657 	}
3658       while (cp_parser_is_string_literal (tok));
3659 
3660       strs = (cpp_string *) obstack_finish (&str_ob);
3661     }
3662 
3663   if (type != CPP_STRING && !wide_ok)
3664     {
3665       cp_parser_error (parser, "a wide string is invalid in this context");
3666       type = CPP_STRING;
3667     }
3668 
3669   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3670       (parse_in, strs, count, &istr, type))
3671     {
3672       value = build_string (istr.len, (const char *)istr.text);
3673       free (CONST_CAST (unsigned char *, istr.text));
3674 
3675       switch (type)
3676 	{
3677 	default:
3678 	case CPP_STRING:
3679 	case CPP_UTF8STRING:
3680 	  TREE_TYPE (value) = char_array_type_node;
3681 	  break;
3682 	case CPP_STRING16:
3683 	  TREE_TYPE (value) = char16_array_type_node;
3684 	  break;
3685 	case CPP_STRING32:
3686 	  TREE_TYPE (value) = char32_array_type_node;
3687 	  break;
3688 	case CPP_WSTRING:
3689 	  TREE_TYPE (value) = wchar_array_type_node;
3690 	  break;
3691 	}
3692 
3693       value = fix_string_type (value);
3694 
3695       if (have_suffix_p)
3696 	{
3697 	  tree literal = build_userdef_literal (suffix_id, value,
3698 						OT_NONE, NULL_TREE);
3699 	  value = cp_parser_userdef_string_literal (literal);
3700 	}
3701     }
3702   else
3703     /* cpp_interpret_string has issued an error.  */
3704     value = error_mark_node;
3705 
3706   if (count > 1)
3707     obstack_free (&str_ob, 0);
3708 
3709   return value;
3710 }
3711 
3712 /* Look up a literal operator with the name and the exact arguments.  */
3713 
3714 static tree
lookup_literal_operator(tree name,vec<tree,va_gc> * args)3715 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3716 {
3717   tree decl, fns;
3718   decl = lookup_name (name);
3719   if (!decl || !is_overloaded_fn (decl))
3720     return error_mark_node;
3721 
3722   for (fns = decl; fns; fns = OVL_NEXT (fns))
3723     {
3724       unsigned int ix;
3725       bool found = true;
3726       tree fn = OVL_CURRENT (fns);
3727       tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3728       if (parmtypes != NULL_TREE)
3729 	{
3730 	  for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3731 	       ++ix, parmtypes = TREE_CHAIN (parmtypes))
3732 	    {
3733 	      tree tparm = TREE_VALUE (parmtypes);
3734 	      tree targ = TREE_TYPE ((*args)[ix]);
3735 	      bool ptr = TYPE_PTR_P (tparm);
3736 	      bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3737 	      if ((ptr || arr || !same_type_p (tparm, targ))
3738 		  && (!ptr || !arr
3739 		      || !same_type_p (TREE_TYPE (tparm),
3740 				       TREE_TYPE (targ))))
3741 		found = false;
3742 	    }
3743 	  if (found
3744 	      && ix == vec_safe_length (args)
3745 	      /* May be this should be sufficient_parms_p instead,
3746 		 depending on how exactly should user-defined literals
3747 		 work in presence of default arguments on the literal
3748 		 operator parameters.  */
3749 	      && parmtypes == void_list_node)
3750 	    return fn;
3751 	}
3752     }
3753 
3754   return error_mark_node;
3755 }
3756 
3757 /* Parse a user-defined char constant.  Returns a call to a user-defined
3758    literal operator taking the character as an argument.  */
3759 
3760 static tree
cp_parser_userdef_char_literal(cp_parser * parser)3761 cp_parser_userdef_char_literal (cp_parser *parser)
3762 {
3763   cp_token *token = cp_lexer_consume_token (parser->lexer);
3764   tree literal = token->u.value;
3765   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3766   tree value = USERDEF_LITERAL_VALUE (literal);
3767   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3768   tree decl, result;
3769 
3770   /* Build up a call to the user-defined operator  */
3771   /* Lookup the name we got back from the id-expression.  */
3772   vec<tree, va_gc> *args = make_tree_vector ();
3773   vec_safe_push (args, value);
3774   decl = lookup_literal_operator (name, args);
3775   if (!decl || decl == error_mark_node)
3776     {
3777       error ("unable to find character literal operator %qD with %qT argument",
3778 	     name, TREE_TYPE (value));
3779       release_tree_vector (args);
3780       return error_mark_node;
3781     }
3782   result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3783   release_tree_vector (args);
3784   if (result != error_mark_node)
3785     return result;
3786 
3787   error ("unable to find character literal operator %qD with %qT argument",
3788 	 name, TREE_TYPE (value));
3789   return error_mark_node;
3790 }
3791 
3792 /* A subroutine of cp_parser_userdef_numeric_literal to
3793    create a char... template parameter pack from a string node.  */
3794 
3795 static tree
make_char_string_pack(tree value)3796 make_char_string_pack (tree value)
3797 {
3798   tree charvec;
3799   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3800   const char *str = TREE_STRING_POINTER (value);
3801   int i, len = TREE_STRING_LENGTH (value) - 1;
3802   tree argvec = make_tree_vec (1);
3803 
3804   /* Fill in CHARVEC with all of the parameters.  */
3805   charvec = make_tree_vec (len);
3806   for (i = 0; i < len; ++i)
3807     TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3808 
3809   /* Build the argument packs.  */
3810   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3811   TREE_TYPE (argpack) = char_type_node;
3812 
3813   TREE_VEC_ELT (argvec, 0) = argpack;
3814 
3815   return argvec;
3816 }
3817 
3818 /* A subroutine of cp_parser_userdef_numeric_literal to
3819    create a char... template parameter pack from a string node.  */
3820 
3821 static tree
make_string_pack(tree value)3822 make_string_pack (tree value)
3823 {
3824   tree charvec;
3825   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3826   const unsigned char *str
3827     = (const unsigned char *) TREE_STRING_POINTER (value);
3828   int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3829   int len = TREE_STRING_LENGTH (value) / sz - 1;
3830   tree argvec = make_tree_vec (2);
3831 
3832   tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3833   str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3834 
3835   /* First template parm is character type.  */
3836   TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3837 
3838   /* Fill in CHARVEC with all of the parameters.  */
3839   charvec = make_tree_vec (len);
3840   for (int i = 0; i < len; ++i)
3841     TREE_VEC_ELT (charvec, i)
3842       = double_int_to_tree (str_char_type_node,
3843 			    double_int::from_buffer (str + i * sz, sz));
3844 
3845   /* Build the argument packs.  */
3846   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3847   TREE_TYPE (argpack) = str_char_type_node;
3848 
3849   TREE_VEC_ELT (argvec, 1) = argpack;
3850 
3851   return argvec;
3852 }
3853 
3854 /* Parse a user-defined numeric constant.  returns a call to a user-defined
3855    literal operator.  */
3856 
3857 static tree
cp_parser_userdef_numeric_literal(cp_parser * parser)3858 cp_parser_userdef_numeric_literal (cp_parser *parser)
3859 {
3860   cp_token *token = cp_lexer_consume_token (parser->lexer);
3861   tree literal = token->u.value;
3862   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3863   tree value = USERDEF_LITERAL_VALUE (literal);
3864   int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3865   tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3866   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3867   tree decl, result;
3868   vec<tree, va_gc> *args;
3869 
3870   /* Look for a literal operator taking the exact type of numeric argument
3871      as the literal value.  */
3872   args = make_tree_vector ();
3873   vec_safe_push (args, value);
3874   decl = lookup_literal_operator (name, args);
3875   if (decl && decl != error_mark_node)
3876     {
3877       result = finish_call_expr (decl, &args, false, true, tf_none);
3878       if (result != error_mark_node)
3879 	{
3880 	  if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3881 	    warning_at (token->location, OPT_Woverflow,
3882 		        "integer literal exceeds range of %qT type",
3883 		        long_long_unsigned_type_node);
3884 	  else
3885 	    {
3886 	      if (overflow > 0)
3887 		warning_at (token->location, OPT_Woverflow,
3888 			    "floating literal exceeds range of %qT type",
3889 			    long_double_type_node);
3890 	      else if (overflow < 0)
3891 		warning_at (token->location, OPT_Woverflow,
3892 			    "floating literal truncated to zero");
3893 	    }
3894 	  release_tree_vector (args);
3895 	  return result;
3896 	}
3897     }
3898   release_tree_vector (args);
3899 
3900   /* If the numeric argument didn't work, look for a raw literal
3901      operator taking a const char* argument consisting of the number
3902      in string format.  */
3903   args = make_tree_vector ();
3904   vec_safe_push (args, num_string);
3905   decl = lookup_literal_operator (name, args);
3906   if (decl && decl != error_mark_node)
3907     {
3908       result = finish_call_expr (decl, &args, false, true, tf_none);
3909       if (result != error_mark_node)
3910 	{
3911 	  release_tree_vector (args);
3912 	  return result;
3913 	}
3914     }
3915   release_tree_vector (args);
3916 
3917   /* If the raw literal didn't work, look for a non-type template
3918      function with parameter pack char....  Call the function with
3919      template parameter characters representing the number.  */
3920   args = make_tree_vector ();
3921   decl = lookup_literal_operator (name, args);
3922   if (decl && decl != error_mark_node)
3923     {
3924       tree tmpl_args = make_char_string_pack (num_string);
3925       decl = lookup_template_function (decl, tmpl_args);
3926       result = finish_call_expr (decl, &args, false, true, tf_none);
3927       if (result != error_mark_node)
3928 	{
3929 	  release_tree_vector (args);
3930 	  return result;
3931 	}
3932     }
3933   release_tree_vector (args);
3934 
3935   error ("unable to find numeric literal operator %qD", name);
3936   if (!cpp_get_options (parse_in)->ext_numeric_literals)
3937     inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
3938 	    "to enable more built-in suffixes");
3939   return error_mark_node;
3940 }
3941 
3942 /* Parse a user-defined string constant.  Returns a call to a user-defined
3943    literal operator taking a character pointer and the length of the string
3944    as arguments.  */
3945 
3946 static tree
cp_parser_userdef_string_literal(tree literal)3947 cp_parser_userdef_string_literal (tree literal)
3948 {
3949   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3950   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3951   tree value = USERDEF_LITERAL_VALUE (literal);
3952   int len = TREE_STRING_LENGTH (value)
3953 	/ TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3954   tree decl, result;
3955   vec<tree, va_gc> *args;
3956 
3957   /* Look for a template function with typename parameter CharT
3958      and parameter pack CharT...  Call the function with
3959      template parameter characters representing the string.  */
3960   args = make_tree_vector ();
3961   decl = lookup_literal_operator (name, args);
3962   if (decl && decl != error_mark_node)
3963     {
3964       tree tmpl_args = make_string_pack (value);
3965       decl = lookup_template_function (decl, tmpl_args);
3966       result = finish_call_expr (decl, &args, false, true, tf_none);
3967       if (result != error_mark_node)
3968 	{
3969 	  release_tree_vector (args);
3970 	  return result;
3971 	}
3972     }
3973   release_tree_vector (args);
3974 
3975   /* Build up a call to the user-defined operator  */
3976   /* Lookup the name we got back from the id-expression.  */
3977   args = make_tree_vector ();
3978   vec_safe_push (args, value);
3979   vec_safe_push (args, build_int_cst (size_type_node, len));
3980   decl = lookup_name (name);
3981   if (!decl || decl == error_mark_node)
3982     {
3983       error ("unable to find string literal operator %qD", name);
3984       release_tree_vector (args);
3985       return error_mark_node;
3986     }
3987   result = finish_call_expr (decl, &args, false, true, tf_none);
3988   release_tree_vector (args);
3989   if (result != error_mark_node)
3990     return result;
3991 
3992   error ("unable to find string literal operator %qD with %qT, %qT arguments",
3993 	 name, TREE_TYPE (value), size_type_node);
3994   return error_mark_node;
3995 }
3996 
3997 
3998 /* Basic concepts [gram.basic]  */
3999 
4000 /* Parse a translation-unit.
4001 
4002    translation-unit:
4003      declaration-seq [opt]
4004 
4005    Returns TRUE if all went well.  */
4006 
4007 static bool
cp_parser_translation_unit(cp_parser * parser)4008 cp_parser_translation_unit (cp_parser* parser)
4009 {
4010   /* The address of the first non-permanent object on the declarator
4011      obstack.  */
4012   static void *declarator_obstack_base;
4013 
4014   bool success;
4015 
4016   /* Create the declarator obstack, if necessary.  */
4017   if (!cp_error_declarator)
4018     {
4019       gcc_obstack_init (&declarator_obstack);
4020       /* Create the error declarator.  */
4021       cp_error_declarator = make_declarator (cdk_error);
4022       /* Create the empty parameter list.  */
4023       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4024       /* Remember where the base of the declarator obstack lies.  */
4025       declarator_obstack_base = obstack_next_free (&declarator_obstack);
4026     }
4027 
4028   cp_parser_declaration_seq_opt (parser);
4029 
4030   /* If there are no tokens left then all went well.  */
4031   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4032     {
4033       /* Get rid of the token array; we don't need it any more.  */
4034       cp_lexer_destroy (parser->lexer);
4035       parser->lexer = NULL;
4036 
4037       /* This file might have been a context that's implicitly extern
4038 	 "C".  If so, pop the lang context.  (Only relevant for PCH.) */
4039       if (parser->implicit_extern_c)
4040 	{
4041 	  pop_lang_context ();
4042 	  parser->implicit_extern_c = false;
4043 	}
4044 
4045       /* Finish up.  */
4046       finish_translation_unit ();
4047 
4048       success = true;
4049     }
4050   else
4051     {
4052       cp_parser_error (parser, "expected declaration");
4053       success = false;
4054     }
4055 
4056   /* Make sure the declarator obstack was fully cleaned up.  */
4057   gcc_assert (obstack_next_free (&declarator_obstack)
4058 	      == declarator_obstack_base);
4059 
4060   /* All went well.  */
4061   return success;
4062 }
4063 
4064 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4065    decltype context.  */
4066 
4067 static inline tsubst_flags_t
complain_flags(bool decltype_p)4068 complain_flags (bool decltype_p)
4069 {
4070   tsubst_flags_t complain = tf_warning_or_error;
4071   if (decltype_p)
4072     complain |= tf_decltype;
4073   return complain;
4074 }
4075 
4076 /* Expressions [gram.expr] */
4077 
4078 /* Parse a primary-expression.
4079 
4080    primary-expression:
4081      literal
4082      this
4083      ( expression )
4084      id-expression
4085 
4086    GNU Extensions:
4087 
4088    primary-expression:
4089      ( compound-statement )
4090      __builtin_va_arg ( assignment-expression , type-id )
4091      __builtin_offsetof ( type-id , offsetof-expression )
4092 
4093    C++ Extensions:
4094      __has_nothrow_assign ( type-id )
4095      __has_nothrow_constructor ( type-id )
4096      __has_nothrow_copy ( type-id )
4097      __has_trivial_assign ( type-id )
4098      __has_trivial_constructor ( type-id )
4099      __has_trivial_copy ( type-id )
4100      __has_trivial_destructor ( type-id )
4101      __has_virtual_destructor ( type-id )
4102      __is_abstract ( type-id )
4103      __is_base_of ( type-id , type-id )
4104      __is_class ( type-id )
4105      __is_convertible_to ( type-id , type-id )
4106      __is_empty ( type-id )
4107      __is_enum ( type-id )
4108      __is_final ( type-id )
4109      __is_literal_type ( type-id )
4110      __is_pod ( type-id )
4111      __is_polymorphic ( type-id )
4112      __is_std_layout ( type-id )
4113      __is_trivial ( type-id )
4114      __is_union ( type-id )
4115 
4116    Objective-C++ Extension:
4117 
4118    primary-expression:
4119      objc-expression
4120 
4121    literal:
4122      __null
4123 
4124    ADDRESS_P is true iff this expression was immediately preceded by
4125    "&" and therefore might denote a pointer-to-member.  CAST_P is true
4126    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
4127    true iff this expression is a template argument.
4128 
4129    Returns a representation of the expression.  Upon return, *IDK
4130    indicates what kind of id-expression (if any) was present.  */
4131 
4132 static tree
cp_parser_primary_expression(cp_parser * parser,bool address_p,bool cast_p,bool template_arg_p,bool decltype_p,cp_id_kind * idk)4133 cp_parser_primary_expression (cp_parser *parser,
4134 			      bool address_p,
4135 			      bool cast_p,
4136 			      bool template_arg_p,
4137 			      bool decltype_p,
4138 			      cp_id_kind *idk)
4139 {
4140   cp_token *token = NULL;
4141 
4142   /* Assume the primary expression is not an id-expression.  */
4143   *idk = CP_ID_KIND_NONE;
4144 
4145   /* Peek at the next token.  */
4146   token = cp_lexer_peek_token (parser->lexer);
4147   switch (token->type)
4148     {
4149       /* literal:
4150 	   integer-literal
4151 	   character-literal
4152 	   floating-literal
4153 	   string-literal
4154 	   boolean-literal
4155 	   pointer-literal
4156 	   user-defined-literal  */
4157     case CPP_CHAR:
4158     case CPP_CHAR16:
4159     case CPP_CHAR32:
4160     case CPP_WCHAR:
4161     case CPP_NUMBER:
4162       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4163 	return cp_parser_userdef_numeric_literal (parser);
4164       token = cp_lexer_consume_token (parser->lexer);
4165       if (TREE_CODE (token->u.value) == FIXED_CST)
4166 	{
4167 	  error_at (token->location,
4168 		    "fixed-point types not supported in C++");
4169 	  return error_mark_node;
4170 	}
4171       /* Floating-point literals are only allowed in an integral
4172 	 constant expression if they are cast to an integral or
4173 	 enumeration type.  */
4174       if (TREE_CODE (token->u.value) == REAL_CST
4175 	  && parser->integral_constant_expression_p
4176 	  && pedantic)
4177 	{
4178 	  /* CAST_P will be set even in invalid code like "int(2.7 +
4179 	     ...)".   Therefore, we have to check that the next token
4180 	     is sure to end the cast.  */
4181 	  if (cast_p)
4182 	    {
4183 	      cp_token *next_token;
4184 
4185 	      next_token = cp_lexer_peek_token (parser->lexer);
4186 	      if (/* The comma at the end of an
4187 		     enumerator-definition.  */
4188 		  next_token->type != CPP_COMMA
4189 		  /* The curly brace at the end of an enum-specifier.  */
4190 		  && next_token->type != CPP_CLOSE_BRACE
4191 		  /* The end of a statement.  */
4192 		  && next_token->type != CPP_SEMICOLON
4193 		  /* The end of the cast-expression.  */
4194 		  && next_token->type != CPP_CLOSE_PAREN
4195 		  /* The end of an array bound.  */
4196 		  && next_token->type != CPP_CLOSE_SQUARE
4197 		  /* The closing ">" in a template-argument-list.  */
4198 		  && (next_token->type != CPP_GREATER
4199 		      || parser->greater_than_is_operator_p)
4200 		  /* C++0x only: A ">>" treated like two ">" tokens,
4201                      in a template-argument-list.  */
4202 		  && (next_token->type != CPP_RSHIFT
4203                       || (cxx_dialect == cxx98)
4204 		      || parser->greater_than_is_operator_p))
4205 		cast_p = false;
4206 	    }
4207 
4208 	  /* If we are within a cast, then the constraint that the
4209 	     cast is to an integral or enumeration type will be
4210 	     checked at that point.  If we are not within a cast, then
4211 	     this code is invalid.  */
4212 	  if (!cast_p)
4213 	    cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4214 	}
4215       return token->u.value;
4216 
4217     case CPP_CHAR_USERDEF:
4218     case CPP_CHAR16_USERDEF:
4219     case CPP_CHAR32_USERDEF:
4220     case CPP_WCHAR_USERDEF:
4221       return cp_parser_userdef_char_literal (parser);
4222 
4223     case CPP_STRING:
4224     case CPP_STRING16:
4225     case CPP_STRING32:
4226     case CPP_WSTRING:
4227     case CPP_UTF8STRING:
4228     case CPP_STRING_USERDEF:
4229     case CPP_STRING16_USERDEF:
4230     case CPP_STRING32_USERDEF:
4231     case CPP_WSTRING_USERDEF:
4232     case CPP_UTF8STRING_USERDEF:
4233       /* ??? Should wide strings be allowed when parser->translate_strings_p
4234 	 is false (i.e. in attributes)?  If not, we can kill the third
4235 	 argument to cp_parser_string_literal.  */
4236       return cp_parser_string_literal (parser,
4237 				       parser->translate_strings_p,
4238 				       true);
4239 
4240     case CPP_OPEN_PAREN:
4241       {
4242 	tree expr;
4243 	bool saved_greater_than_is_operator_p;
4244 
4245 	/* Consume the `('.  */
4246 	cp_lexer_consume_token (parser->lexer);
4247 	/* Within a parenthesized expression, a `>' token is always
4248 	   the greater-than operator.  */
4249 	saved_greater_than_is_operator_p
4250 	  = parser->greater_than_is_operator_p;
4251 	parser->greater_than_is_operator_p = true;
4252 	/* If we see `( { ' then we are looking at the beginning of
4253 	   a GNU statement-expression.  */
4254 	if (cp_parser_allow_gnu_extensions_p (parser)
4255 	    && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4256 	  {
4257 	    /* Statement-expressions are not allowed by the standard.  */
4258 	    pedwarn (token->location, OPT_Wpedantic,
4259 		     "ISO C++ forbids braced-groups within expressions");
4260 
4261 	    /* And they're not allowed outside of a function-body; you
4262 	       cannot, for example, write:
4263 
4264 		 int i = ({ int j = 3; j + 1; });
4265 
4266 	       at class or namespace scope.  */
4267 	    if (!parser->in_function_body
4268 		|| parser->in_template_argument_list_p)
4269 	      {
4270 		error_at (token->location,
4271 			  "statement-expressions are not allowed outside "
4272 			  "functions nor in template-argument lists");
4273 		cp_parser_skip_to_end_of_block_or_statement (parser);
4274 		expr = error_mark_node;
4275 	      }
4276 	    else
4277 	      {
4278 		/* Start the statement-expression.  */
4279 		expr = begin_stmt_expr ();
4280 		/* Parse the compound-statement.  */
4281 		cp_parser_compound_statement (parser, expr, false, false);
4282 		/* Finish up.  */
4283 		expr = finish_stmt_expr (expr, false);
4284 	      }
4285 	  }
4286 	else
4287 	  {
4288 	    /* Parse the parenthesized expression.  */
4289 	    expr = cp_parser_expression (parser, cast_p, decltype_p, idk);
4290 	    /* Let the front end know that this expression was
4291 	       enclosed in parentheses. This matters in case, for
4292 	       example, the expression is of the form `A::B', since
4293 	       `&A::B' might be a pointer-to-member, but `&(A::B)' is
4294 	       not.  */
4295 	    expr = finish_parenthesized_expr (expr);
4296 	    /* DR 705: Wrapping an unqualified name in parentheses
4297 	       suppresses arg-dependent lookup.  We want to pass back
4298 	       CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4299 	       (c++/37862), but none of the others.  */
4300 	    if (*idk != CP_ID_KIND_QUALIFIED)
4301 	      *idk = CP_ID_KIND_NONE;
4302 	  }
4303 	/* The `>' token might be the end of a template-id or
4304 	   template-parameter-list now.  */
4305 	parser->greater_than_is_operator_p
4306 	  = saved_greater_than_is_operator_p;
4307 	/* Consume the `)'.  */
4308 	if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4309 	  cp_parser_skip_to_end_of_statement (parser);
4310 
4311 	return expr;
4312       }
4313 
4314     case CPP_OPEN_SQUARE:
4315       if (c_dialect_objc ())
4316         /* We have an Objective-C++ message. */
4317         return cp_parser_objc_expression (parser);
4318       {
4319 	tree lam = cp_parser_lambda_expression (parser);
4320 	/* Don't warn about a failed tentative parse.  */
4321 	if (cp_parser_error_occurred (parser))
4322 	  return error_mark_node;
4323 	maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4324 	return lam;
4325       }
4326 
4327     case CPP_OBJC_STRING:
4328       if (c_dialect_objc ())
4329 	/* We have an Objective-C++ string literal. */
4330         return cp_parser_objc_expression (parser);
4331       cp_parser_error (parser, "expected primary-expression");
4332       return error_mark_node;
4333 
4334     case CPP_KEYWORD:
4335       switch (token->keyword)
4336 	{
4337 	  /* These two are the boolean literals.  */
4338 	case RID_TRUE:
4339 	  cp_lexer_consume_token (parser->lexer);
4340 	  return boolean_true_node;
4341 	case RID_FALSE:
4342 	  cp_lexer_consume_token (parser->lexer);
4343 	  return boolean_false_node;
4344 
4345 	  /* The `__null' literal.  */
4346 	case RID_NULL:
4347 	  cp_lexer_consume_token (parser->lexer);
4348 	  return null_node;
4349 
4350 	  /* The `nullptr' literal.  */
4351 	case RID_NULLPTR:
4352 	  cp_lexer_consume_token (parser->lexer);
4353 	  return nullptr_node;
4354 
4355 	  /* Recognize the `this' keyword.  */
4356 	case RID_THIS:
4357 	  cp_lexer_consume_token (parser->lexer);
4358 	  if (parser->local_variables_forbidden_p)
4359 	    {
4360 	      error_at (token->location,
4361 			"%<this%> may not be used in this context");
4362 	      return error_mark_node;
4363 	    }
4364 	  /* Pointers cannot appear in constant-expressions.  */
4365 	  if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4366 	    return error_mark_node;
4367 	  return finish_this_expr ();
4368 
4369 	  /* The `operator' keyword can be the beginning of an
4370 	     id-expression.  */
4371 	case RID_OPERATOR:
4372 	  goto id_expression;
4373 
4374 	case RID_FUNCTION_NAME:
4375 	case RID_PRETTY_FUNCTION_NAME:
4376 	case RID_C99_FUNCTION_NAME:
4377 	  {
4378 	    non_integral_constant name;
4379 
4380 	    /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4381 	       __func__ are the names of variables -- but they are
4382 	       treated specially.  Therefore, they are handled here,
4383 	       rather than relying on the generic id-expression logic
4384 	       below.  Grammatically, these names are id-expressions.
4385 
4386 	       Consume the token.  */
4387 	    token = cp_lexer_consume_token (parser->lexer);
4388 
4389 	    switch (token->keyword)
4390 	      {
4391 	      case RID_FUNCTION_NAME:
4392 		name = NIC_FUNC_NAME;
4393 		break;
4394 	      case RID_PRETTY_FUNCTION_NAME:
4395 		name = NIC_PRETTY_FUNC;
4396 		break;
4397 	      case RID_C99_FUNCTION_NAME:
4398 		name = NIC_C99_FUNC;
4399 		break;
4400 	      default:
4401 		gcc_unreachable ();
4402 	      }
4403 
4404 	    if (cp_parser_non_integral_constant_expression (parser, name))
4405 	      return error_mark_node;
4406 
4407 	    /* Look up the name.  */
4408 	    return finish_fname (token->u.value);
4409 	  }
4410 
4411 	case RID_VA_ARG:
4412 	  {
4413 	    tree expression;
4414 	    tree type;
4415 	    source_location type_location;
4416 
4417 	    /* The `__builtin_va_arg' construct is used to handle
4418 	       `va_arg'.  Consume the `__builtin_va_arg' token.  */
4419 	    cp_lexer_consume_token (parser->lexer);
4420 	    /* Look for the opening `('.  */
4421 	    cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4422 	    /* Now, parse the assignment-expression.  */
4423 	    expression = cp_parser_assignment_expression (parser,
4424 							  /*cast_p=*/false, NULL);
4425 	    /* Look for the `,'.  */
4426 	    cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4427 	    type_location = cp_lexer_peek_token (parser->lexer)->location;
4428 	    /* Parse the type-id.  */
4429 	    type = cp_parser_type_id (parser);
4430 	    /* Look for the closing `)'.  */
4431 	    cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4432 	    /* Using `va_arg' in a constant-expression is not
4433 	       allowed.  */
4434 	    if (cp_parser_non_integral_constant_expression (parser,
4435 							    NIC_VA_ARG))
4436 	      return error_mark_node;
4437 	    return build_x_va_arg (type_location, expression, type);
4438 	  }
4439 
4440 	case RID_OFFSETOF:
4441 	  return cp_parser_builtin_offsetof (parser);
4442 
4443 	case RID_HAS_NOTHROW_ASSIGN:
4444 	case RID_HAS_NOTHROW_CONSTRUCTOR:
4445 	case RID_HAS_NOTHROW_COPY:
4446 	case RID_HAS_TRIVIAL_ASSIGN:
4447 	case RID_HAS_TRIVIAL_CONSTRUCTOR:
4448 	case RID_HAS_TRIVIAL_COPY:
4449 	case RID_HAS_TRIVIAL_DESTRUCTOR:
4450 	case RID_HAS_VIRTUAL_DESTRUCTOR:
4451 	case RID_IS_ABSTRACT:
4452 	case RID_IS_BASE_OF:
4453 	case RID_IS_CLASS:
4454 	case RID_IS_CONVERTIBLE_TO:
4455 	case RID_IS_EMPTY:
4456 	case RID_IS_ENUM:
4457 	case RID_IS_FINAL:
4458 	case RID_IS_LITERAL_TYPE:
4459 	case RID_IS_POD:
4460 	case RID_IS_POLYMORPHIC:
4461 	case RID_IS_STD_LAYOUT:
4462 	case RID_IS_TRIVIAL:
4463 	case RID_IS_UNION:
4464 	  return cp_parser_trait_expr (parser, token->keyword);
4465 
4466 	/* Objective-C++ expressions.  */
4467 	case RID_AT_ENCODE:
4468 	case RID_AT_PROTOCOL:
4469 	case RID_AT_SELECTOR:
4470 	  return cp_parser_objc_expression (parser);
4471 
4472 	case RID_TEMPLATE:
4473 	  if (parser->in_function_body
4474 	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4475 	      	  == CPP_LESS))
4476 	    {
4477 	      error_at (token->location,
4478 			"a template declaration cannot appear at block scope");
4479 	      cp_parser_skip_to_end_of_block_or_statement (parser);
4480 	      return error_mark_node;
4481 	    }
4482 	default:
4483 	  cp_parser_error (parser, "expected primary-expression");
4484 	  return error_mark_node;
4485 	}
4486 
4487       /* An id-expression can start with either an identifier, a
4488 	 `::' as the beginning of a qualified-id, or the "operator"
4489 	 keyword.  */
4490     case CPP_NAME:
4491     case CPP_SCOPE:
4492     case CPP_TEMPLATE_ID:
4493     case CPP_NESTED_NAME_SPECIFIER:
4494       {
4495 	tree id_expression;
4496 	tree decl;
4497 	const char *error_msg;
4498 	bool template_p;
4499 	bool done;
4500 	cp_token *id_expr_token;
4501 
4502       id_expression:
4503 	/* Parse the id-expression.  */
4504 	id_expression
4505 	  = cp_parser_id_expression (parser,
4506 				     /*template_keyword_p=*/false,
4507 				     /*check_dependency_p=*/true,
4508 				     &template_p,
4509 				     /*declarator_p=*/false,
4510 				     /*optional_p=*/false);
4511 	if (id_expression == error_mark_node)
4512 	  return error_mark_node;
4513 	id_expr_token = token;
4514 	token = cp_lexer_peek_token (parser->lexer);
4515 	done = (token->type != CPP_OPEN_SQUARE
4516 		&& token->type != CPP_OPEN_PAREN
4517 		&& token->type != CPP_DOT
4518 		&& token->type != CPP_DEREF
4519 		&& token->type != CPP_PLUS_PLUS
4520 		&& token->type != CPP_MINUS_MINUS);
4521 	/* If we have a template-id, then no further lookup is
4522 	   required.  If the template-id was for a template-class, we
4523 	   will sometimes have a TYPE_DECL at this point.  */
4524 	if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4525 		 || TREE_CODE (id_expression) == TYPE_DECL)
4526 	  decl = id_expression;
4527 	/* Look up the name.  */
4528 	else
4529 	  {
4530 	    tree ambiguous_decls;
4531 
4532 	    /* If we already know that this lookup is ambiguous, then
4533 	       we've already issued an error message; there's no reason
4534 	       to check again.  */
4535 	    if (id_expr_token->type == CPP_NAME
4536 		&& id_expr_token->ambiguous_p)
4537 	      {
4538 		cp_parser_simulate_error (parser);
4539 		return error_mark_node;
4540 	      }
4541 
4542 	    decl = cp_parser_lookup_name (parser, id_expression,
4543 					  none_type,
4544 					  template_p,
4545 					  /*is_namespace=*/false,
4546 					  /*check_dependency=*/true,
4547 					  &ambiguous_decls,
4548 					  id_expr_token->location);
4549 	    /* If the lookup was ambiguous, an error will already have
4550 	       been issued.  */
4551 	    if (ambiguous_decls)
4552 	      return error_mark_node;
4553 
4554 	    /* In Objective-C++, we may have an Objective-C 2.0
4555 	       dot-syntax for classes here.  */
4556 	    if (c_dialect_objc ()
4557 		&& cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4558 		&& TREE_CODE (decl) == TYPE_DECL
4559 		&& objc_is_class_name (decl))
4560 	      {
4561 		tree component;
4562 		cp_lexer_consume_token (parser->lexer);
4563 		component = cp_parser_identifier (parser);
4564 		if (component == error_mark_node)
4565 		  return error_mark_node;
4566 
4567 		return objc_build_class_component_ref (id_expression, component);
4568 	      }
4569 
4570 	    /* In Objective-C++, an instance variable (ivar) may be preferred
4571 	       to whatever cp_parser_lookup_name() found.  */
4572 	    decl = objc_lookup_ivar (decl, id_expression);
4573 
4574 	    /* If name lookup gives us a SCOPE_REF, then the
4575 	       qualifying scope was dependent.  */
4576 	    if (TREE_CODE (decl) == SCOPE_REF)
4577 	      {
4578 		/* At this point, we do not know if DECL is a valid
4579 		   integral constant expression.  We assume that it is
4580 		   in fact such an expression, so that code like:
4581 
4582 		      template <int N> struct A {
4583 			int a[B<N>::i];
4584 		      };
4585 
4586 		   is accepted.  At template-instantiation time, we
4587 		   will check that B<N>::i is actually a constant.  */
4588 		return decl;
4589 	      }
4590 	    /* Check to see if DECL is a local variable in a context
4591 	       where that is forbidden.  */
4592 	    if (parser->local_variables_forbidden_p
4593 		&& local_variable_p (decl))
4594 	      {
4595 		/* It might be that we only found DECL because we are
4596 		   trying to be generous with pre-ISO scoping rules.
4597 		   For example, consider:
4598 
4599 		     int i;
4600 		     void g() {
4601 		       for (int i = 0; i < 10; ++i) {}
4602 		       extern void f(int j = i);
4603 		     }
4604 
4605 		   Here, name look up will originally find the out
4606 		   of scope `i'.  We need to issue a warning message,
4607 		   but then use the global `i'.  */
4608 		decl = check_for_out_of_scope_variable (decl);
4609 		if (local_variable_p (decl))
4610 		  {
4611 		    error_at (id_expr_token->location,
4612 			      "local variable %qD may not appear in this context",
4613 			      decl);
4614 		    return error_mark_node;
4615 		  }
4616 	      }
4617 	  }
4618 
4619 	decl = (finish_id_expression
4620 		(id_expression, decl, parser->scope,
4621 		 idk,
4622 		 parser->integral_constant_expression_p,
4623 		 parser->allow_non_integral_constant_expression_p,
4624 		 &parser->non_integral_constant_expression_p,
4625 		 template_p, done, address_p,
4626 		 template_arg_p,
4627 		 &error_msg,
4628                  id_expr_token->location));
4629 	if (error_msg)
4630 	  cp_parser_error (parser, error_msg);
4631 	return decl;
4632       }
4633 
4634       /* Anything else is an error.  */
4635     default:
4636       cp_parser_error (parser, "expected primary-expression");
4637       return error_mark_node;
4638     }
4639 }
4640 
4641 static inline tree
cp_parser_primary_expression(cp_parser * parser,bool address_p,bool cast_p,bool template_arg_p,cp_id_kind * idk)4642 cp_parser_primary_expression (cp_parser *parser,
4643 			      bool address_p,
4644 			      bool cast_p,
4645 			      bool template_arg_p,
4646 			      cp_id_kind *idk)
4647 {
4648   return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4649 				       /*decltype*/false, idk);
4650 }
4651 
4652 /* Parse an id-expression.
4653 
4654    id-expression:
4655      unqualified-id
4656      qualified-id
4657 
4658    qualified-id:
4659      :: [opt] nested-name-specifier template [opt] unqualified-id
4660      :: identifier
4661      :: operator-function-id
4662      :: template-id
4663 
4664    Return a representation of the unqualified portion of the
4665    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4666    a `::' or nested-name-specifier.
4667 
4668    Often, if the id-expression was a qualified-id, the caller will
4669    want to make a SCOPE_REF to represent the qualified-id.  This
4670    function does not do this in order to avoid wastefully creating
4671    SCOPE_REFs when they are not required.
4672 
4673    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4674    `template' keyword.
4675 
4676    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4677    uninstantiated templates.
4678 
4679    If *TEMPLATE_P is non-NULL, it is set to true iff the
4680    `template' keyword is used to explicitly indicate that the entity
4681    named is a template.
4682 
4683    If DECLARATOR_P is true, the id-expression is appearing as part of
4684    a declarator, rather than as part of an expression.  */
4685 
4686 static tree
cp_parser_id_expression(cp_parser * parser,bool template_keyword_p,bool check_dependency_p,bool * template_p,bool declarator_p,bool optional_p)4687 cp_parser_id_expression (cp_parser *parser,
4688 			 bool template_keyword_p,
4689 			 bool check_dependency_p,
4690 			 bool *template_p,
4691 			 bool declarator_p,
4692 			 bool optional_p)
4693 {
4694   bool global_scope_p;
4695   bool nested_name_specifier_p;
4696 
4697   /* Assume the `template' keyword was not used.  */
4698   if (template_p)
4699     *template_p = template_keyword_p;
4700 
4701   /* Look for the optional `::' operator.  */
4702   global_scope_p
4703     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4704        != NULL_TREE);
4705   /* Look for the optional nested-name-specifier.  */
4706   nested_name_specifier_p
4707     = (cp_parser_nested_name_specifier_opt (parser,
4708 					    /*typename_keyword_p=*/false,
4709 					    check_dependency_p,
4710 					    /*type_p=*/false,
4711 					    declarator_p)
4712        != NULL_TREE);
4713   /* If there is a nested-name-specifier, then we are looking at
4714      the first qualified-id production.  */
4715   if (nested_name_specifier_p)
4716     {
4717       tree saved_scope;
4718       tree saved_object_scope;
4719       tree saved_qualifying_scope;
4720       tree unqualified_id;
4721       bool is_template;
4722 
4723       /* See if the next token is the `template' keyword.  */
4724       if (!template_p)
4725 	template_p = &is_template;
4726       *template_p = cp_parser_optional_template_keyword (parser);
4727       /* Name lookup we do during the processing of the
4728 	 unqualified-id might obliterate SCOPE.  */
4729       saved_scope = parser->scope;
4730       saved_object_scope = parser->object_scope;
4731       saved_qualifying_scope = parser->qualifying_scope;
4732       /* Process the final unqualified-id.  */
4733       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4734 						 check_dependency_p,
4735 						 declarator_p,
4736 						 /*optional_p=*/false);
4737       /* Restore the SAVED_SCOPE for our caller.  */
4738       parser->scope = saved_scope;
4739       parser->object_scope = saved_object_scope;
4740       parser->qualifying_scope = saved_qualifying_scope;
4741 
4742       return unqualified_id;
4743     }
4744   /* Otherwise, if we are in global scope, then we are looking at one
4745      of the other qualified-id productions.  */
4746   else if (global_scope_p)
4747     {
4748       cp_token *token;
4749       tree id;
4750 
4751       /* Peek at the next token.  */
4752       token = cp_lexer_peek_token (parser->lexer);
4753 
4754       /* If it's an identifier, and the next token is not a "<", then
4755 	 we can avoid the template-id case.  This is an optimization
4756 	 for this common case.  */
4757       if (token->type == CPP_NAME
4758 	  && !cp_parser_nth_token_starts_template_argument_list_p
4759 	       (parser, 2))
4760 	return cp_parser_identifier (parser);
4761 
4762       cp_parser_parse_tentatively (parser);
4763       /* Try a template-id.  */
4764       id = cp_parser_template_id (parser,
4765 				  /*template_keyword_p=*/false,
4766 				  /*check_dependency_p=*/true,
4767 				  none_type,
4768 				  declarator_p);
4769       /* If that worked, we're done.  */
4770       if (cp_parser_parse_definitely (parser))
4771 	return id;
4772 
4773       /* Peek at the next token.  (Changes in the token buffer may
4774 	 have invalidated the pointer obtained above.)  */
4775       token = cp_lexer_peek_token (parser->lexer);
4776 
4777       switch (token->type)
4778 	{
4779 	case CPP_NAME:
4780 	  return cp_parser_identifier (parser);
4781 
4782 	case CPP_KEYWORD:
4783 	  if (token->keyword == RID_OPERATOR)
4784 	    return cp_parser_operator_function_id (parser);
4785 	  /* Fall through.  */
4786 
4787 	default:
4788 	  cp_parser_error (parser, "expected id-expression");
4789 	  return error_mark_node;
4790 	}
4791     }
4792   else
4793     return cp_parser_unqualified_id (parser, template_keyword_p,
4794 				     /*check_dependency_p=*/true,
4795 				     declarator_p,
4796 				     optional_p);
4797 }
4798 
4799 /* Parse an unqualified-id.
4800 
4801    unqualified-id:
4802      identifier
4803      operator-function-id
4804      conversion-function-id
4805      ~ class-name
4806      template-id
4807 
4808    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4809    keyword, in a construct like `A::template ...'.
4810 
4811    Returns a representation of unqualified-id.  For the `identifier'
4812    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4813    production a BIT_NOT_EXPR is returned; the operand of the
4814    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4815    other productions, see the documentation accompanying the
4816    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4817    names are looked up in uninstantiated templates.  If DECLARATOR_P
4818    is true, the unqualified-id is appearing as part of a declarator,
4819    rather than as part of an expression.  */
4820 
4821 static tree
cp_parser_unqualified_id(cp_parser * parser,bool template_keyword_p,bool check_dependency_p,bool declarator_p,bool optional_p)4822 cp_parser_unqualified_id (cp_parser* parser,
4823 			  bool template_keyword_p,
4824 			  bool check_dependency_p,
4825 			  bool declarator_p,
4826 			  bool optional_p)
4827 {
4828   cp_token *token;
4829 
4830   /* Peek at the next token.  */
4831   token = cp_lexer_peek_token (parser->lexer);
4832 
4833   switch (token->type)
4834     {
4835     case CPP_NAME:
4836       {
4837 	tree id;
4838 
4839 	/* We don't know yet whether or not this will be a
4840 	   template-id.  */
4841 	cp_parser_parse_tentatively (parser);
4842 	/* Try a template-id.  */
4843 	id = cp_parser_template_id (parser, template_keyword_p,
4844 				    check_dependency_p,
4845 				    none_type,
4846 				    declarator_p);
4847 	/* If it worked, we're done.  */
4848 	if (cp_parser_parse_definitely (parser))
4849 	  return id;
4850 	/* Otherwise, it's an ordinary identifier.  */
4851 	return cp_parser_identifier (parser);
4852       }
4853 
4854     case CPP_TEMPLATE_ID:
4855       return cp_parser_template_id (parser, template_keyword_p,
4856 				    check_dependency_p,
4857 				    none_type,
4858 				    declarator_p);
4859 
4860     case CPP_COMPL:
4861       {
4862 	tree type_decl;
4863 	tree qualifying_scope;
4864 	tree object_scope;
4865 	tree scope;
4866 	bool done;
4867 
4868 	/* Consume the `~' token.  */
4869 	cp_lexer_consume_token (parser->lexer);
4870 	/* Parse the class-name.  The standard, as written, seems to
4871 	   say that:
4872 
4873 	     template <typename T> struct S { ~S (); };
4874 	     template <typename T> S<T>::~S() {}
4875 
4876 	   is invalid, since `~' must be followed by a class-name, but
4877 	   `S<T>' is dependent, and so not known to be a class.
4878 	   That's not right; we need to look in uninstantiated
4879 	   templates.  A further complication arises from:
4880 
4881 	     template <typename T> void f(T t) {
4882 	       t.T::~T();
4883 	     }
4884 
4885 	   Here, it is not possible to look up `T' in the scope of `T'
4886 	   itself.  We must look in both the current scope, and the
4887 	   scope of the containing complete expression.
4888 
4889 	   Yet another issue is:
4890 
4891 	     struct S {
4892 	       int S;
4893 	       ~S();
4894 	     };
4895 
4896 	     S::~S() {}
4897 
4898 	   The standard does not seem to say that the `S' in `~S'
4899 	   should refer to the type `S' and not the data member
4900 	   `S::S'.  */
4901 
4902 	/* DR 244 says that we look up the name after the "~" in the
4903 	   same scope as we looked up the qualifying name.  That idea
4904 	   isn't fully worked out; it's more complicated than that.  */
4905 	scope = parser->scope;
4906 	object_scope = parser->object_scope;
4907 	qualifying_scope = parser->qualifying_scope;
4908 
4909 	/* Check for invalid scopes.  */
4910 	if (scope == error_mark_node)
4911 	  {
4912 	    if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4913 	      cp_lexer_consume_token (parser->lexer);
4914 	    return error_mark_node;
4915 	  }
4916 	if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4917 	  {
4918 	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4919 	      error_at (token->location,
4920 			"scope %qT before %<~%> is not a class-name",
4921 			scope);
4922 	    cp_parser_simulate_error (parser);
4923 	    if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4924 	      cp_lexer_consume_token (parser->lexer);
4925 	    return error_mark_node;
4926 	  }
4927 	gcc_assert (!scope || TYPE_P (scope));
4928 
4929 	/* If the name is of the form "X::~X" it's OK even if X is a
4930 	   typedef.  */
4931 	token = cp_lexer_peek_token (parser->lexer);
4932 	if (scope
4933 	    && token->type == CPP_NAME
4934 	    && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4935 		!= CPP_LESS)
4936 	    && (token->u.value == TYPE_IDENTIFIER (scope)
4937 		|| (CLASS_TYPE_P (scope)
4938 		    && constructor_name_p (token->u.value, scope))))
4939 	  {
4940 	    cp_lexer_consume_token (parser->lexer);
4941 	    return build_nt (BIT_NOT_EXPR, scope);
4942 	  }
4943 
4944 	/* ~auto means the destructor of whatever the object is.  */
4945 	if (cp_parser_is_keyword (token, RID_AUTO))
4946 	  {
4947 	    if (cxx_dialect < cxx1y)
4948 	      pedwarn (input_location, 0,
4949 		       "%<~auto%> only available with "
4950 		       "-std=c++1y or -std=gnu++1y");
4951 	    cp_lexer_consume_token (parser->lexer);
4952 	    return build_nt (BIT_NOT_EXPR, make_auto ());
4953 	  }
4954 
4955 	/* If there was an explicit qualification (S::~T), first look
4956 	   in the scope given by the qualification (i.e., S).
4957 
4958 	   Note: in the calls to cp_parser_class_name below we pass
4959 	   typename_type so that lookup finds the injected-class-name
4960 	   rather than the constructor.  */
4961 	done = false;
4962 	type_decl = NULL_TREE;
4963 	if (scope)
4964 	  {
4965 	    cp_parser_parse_tentatively (parser);
4966 	    type_decl = cp_parser_class_name (parser,
4967 					      /*typename_keyword_p=*/false,
4968 					      /*template_keyword_p=*/false,
4969 					      typename_type,
4970 					      /*check_dependency=*/false,
4971 					      /*class_head_p=*/false,
4972 					      declarator_p);
4973 	    if (cp_parser_parse_definitely (parser))
4974 	      done = true;
4975 	  }
4976 	/* In "N::S::~S", look in "N" as well.  */
4977 	if (!done && scope && qualifying_scope)
4978 	  {
4979 	    cp_parser_parse_tentatively (parser);
4980 	    parser->scope = qualifying_scope;
4981 	    parser->object_scope = NULL_TREE;
4982 	    parser->qualifying_scope = NULL_TREE;
4983 	    type_decl
4984 	      = cp_parser_class_name (parser,
4985 				      /*typename_keyword_p=*/false,
4986 				      /*template_keyword_p=*/false,
4987 				      typename_type,
4988 				      /*check_dependency=*/false,
4989 				      /*class_head_p=*/false,
4990 				      declarator_p);
4991 	    if (cp_parser_parse_definitely (parser))
4992 	      done = true;
4993 	  }
4994 	/* In "p->S::~T", look in the scope given by "*p" as well.  */
4995 	else if (!done && object_scope)
4996 	  {
4997 	    cp_parser_parse_tentatively (parser);
4998 	    parser->scope = object_scope;
4999 	    parser->object_scope = NULL_TREE;
5000 	    parser->qualifying_scope = NULL_TREE;
5001 	    type_decl
5002 	      = cp_parser_class_name (parser,
5003 				      /*typename_keyword_p=*/false,
5004 				      /*template_keyword_p=*/false,
5005 				      typename_type,
5006 				      /*check_dependency=*/false,
5007 				      /*class_head_p=*/false,
5008 				      declarator_p);
5009 	    if (cp_parser_parse_definitely (parser))
5010 	      done = true;
5011 	  }
5012 	/* Look in the surrounding context.  */
5013 	if (!done)
5014 	  {
5015 	    parser->scope = NULL_TREE;
5016 	    parser->object_scope = NULL_TREE;
5017 	    parser->qualifying_scope = NULL_TREE;
5018 	    if (processing_template_decl)
5019 	      cp_parser_parse_tentatively (parser);
5020 	    type_decl
5021 	      = cp_parser_class_name (parser,
5022 				      /*typename_keyword_p=*/false,
5023 				      /*template_keyword_p=*/false,
5024 				      typename_type,
5025 				      /*check_dependency=*/false,
5026 				      /*class_head_p=*/false,
5027 				      declarator_p);
5028 	    if (processing_template_decl
5029 		&& ! cp_parser_parse_definitely (parser))
5030 	      {
5031 		/* We couldn't find a type with this name, so just accept
5032 		   it and check for a match at instantiation time.  */
5033 		type_decl = cp_parser_identifier (parser);
5034 		if (type_decl != error_mark_node)
5035 		  type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5036 		return type_decl;
5037 	      }
5038 	  }
5039 	/* If an error occurred, assume that the name of the
5040 	   destructor is the same as the name of the qualifying
5041 	   class.  That allows us to keep parsing after running
5042 	   into ill-formed destructor names.  */
5043 	if (type_decl == error_mark_node && scope)
5044 	  return build_nt (BIT_NOT_EXPR, scope);
5045 	else if (type_decl == error_mark_node)
5046 	  return error_mark_node;
5047 
5048 	/* Check that destructor name and scope match.  */
5049 	if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5050 	  {
5051 	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5052 	      error_at (token->location,
5053 			"declaration of %<~%T%> as member of %qT",
5054 			type_decl, scope);
5055 	    cp_parser_simulate_error (parser);
5056 	    return error_mark_node;
5057 	  }
5058 
5059 	/* [class.dtor]
5060 
5061 	   A typedef-name that names a class shall not be used as the
5062 	   identifier in the declarator for a destructor declaration.  */
5063 	if (declarator_p
5064 	    && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5065 	    && !DECL_SELF_REFERENCE_P (type_decl)
5066 	    && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5067 	  error_at (token->location,
5068 		    "typedef-name %qD used as destructor declarator",
5069 		    type_decl);
5070 
5071 	return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5072       }
5073 
5074     case CPP_KEYWORD:
5075       if (token->keyword == RID_OPERATOR)
5076 	{
5077 	  tree id;
5078 
5079 	  /* This could be a template-id, so we try that first.  */
5080 	  cp_parser_parse_tentatively (parser);
5081 	  /* Try a template-id.  */
5082 	  id = cp_parser_template_id (parser, template_keyword_p,
5083 				      /*check_dependency_p=*/true,
5084 				      none_type,
5085 				      declarator_p);
5086 	  /* If that worked, we're done.  */
5087 	  if (cp_parser_parse_definitely (parser))
5088 	    return id;
5089 	  /* We still don't know whether we're looking at an
5090 	     operator-function-id or a conversion-function-id.  */
5091 	  cp_parser_parse_tentatively (parser);
5092 	  /* Try an operator-function-id.  */
5093 	  id = cp_parser_operator_function_id (parser);
5094 	  /* If that didn't work, try a conversion-function-id.  */
5095 	  if (!cp_parser_parse_definitely (parser))
5096 	    id = cp_parser_conversion_function_id (parser);
5097 	  else if (UDLIT_OPER_P (id))
5098 	    {
5099 	      /* 17.6.3.3.5  */
5100 	      const char *name = UDLIT_OP_SUFFIX (id);
5101 	      if (name[0] != '_' && !in_system_header_at (input_location)
5102 		  && declarator_p)
5103 		warning (0, "literal operator suffixes not preceded by %<_%>"
5104 			    " are reserved for future standardization");
5105 	    }
5106 
5107 	  return id;
5108 	}
5109       /* Fall through.  */
5110 
5111     default:
5112       if (optional_p)
5113 	return NULL_TREE;
5114       cp_parser_error (parser, "expected unqualified-id");
5115       return error_mark_node;
5116     }
5117 }
5118 
5119 /* Parse an (optional) nested-name-specifier.
5120 
5121    nested-name-specifier: [C++98]
5122      class-or-namespace-name :: nested-name-specifier [opt]
5123      class-or-namespace-name :: template nested-name-specifier [opt]
5124 
5125    nested-name-specifier: [C++0x]
5126      type-name ::
5127      namespace-name ::
5128      nested-name-specifier identifier ::
5129      nested-name-specifier template [opt] simple-template-id ::
5130 
5131    PARSER->SCOPE should be set appropriately before this function is
5132    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5133    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
5134    in name lookups.
5135 
5136    Sets PARSER->SCOPE to the class (TYPE) or namespace
5137    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5138    it unchanged if there is no nested-name-specifier.  Returns the new
5139    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5140 
5141    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5142    part of a declaration and/or decl-specifier.  */
5143 
5144 static tree
cp_parser_nested_name_specifier_opt(cp_parser * parser,bool typename_keyword_p,bool check_dependency_p,bool type_p,bool is_declaration)5145 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5146 				     bool typename_keyword_p,
5147 				     bool check_dependency_p,
5148 				     bool type_p,
5149 				     bool is_declaration)
5150 {
5151   bool success = false;
5152   cp_token_position start = 0;
5153   cp_token *token;
5154 
5155   /* Remember where the nested-name-specifier starts.  */
5156   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5157     {
5158       start = cp_lexer_token_position (parser->lexer, false);
5159       push_deferring_access_checks (dk_deferred);
5160     }
5161 
5162   while (true)
5163     {
5164       tree new_scope;
5165       tree old_scope;
5166       tree saved_qualifying_scope;
5167       bool template_keyword_p;
5168 
5169       /* Spot cases that cannot be the beginning of a
5170 	 nested-name-specifier.  */
5171       token = cp_lexer_peek_token (parser->lexer);
5172 
5173       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5174 	 the already parsed nested-name-specifier.  */
5175       if (token->type == CPP_NESTED_NAME_SPECIFIER)
5176 	{
5177 	  /* Grab the nested-name-specifier and continue the loop.  */
5178 	  cp_parser_pre_parsed_nested_name_specifier (parser);
5179 	  /* If we originally encountered this nested-name-specifier
5180 	     with IS_DECLARATION set to false, we will not have
5181 	     resolved TYPENAME_TYPEs, so we must do so here.  */
5182 	  if (is_declaration
5183 	      && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5184 	    {
5185 	      new_scope = resolve_typename_type (parser->scope,
5186 						 /*only_current_p=*/false);
5187 	      if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5188 		parser->scope = new_scope;
5189 	    }
5190 	  success = true;
5191 	  continue;
5192 	}
5193 
5194       /* Spot cases that cannot be the beginning of a
5195 	 nested-name-specifier.  On the second and subsequent times
5196 	 through the loop, we look for the `template' keyword.  */
5197       if (success && token->keyword == RID_TEMPLATE)
5198 	;
5199       /* A template-id can start a nested-name-specifier.  */
5200       else if (token->type == CPP_TEMPLATE_ID)
5201 	;
5202       /* DR 743: decltype can be used in a nested-name-specifier.  */
5203       else if (token_is_decltype (token))
5204 	;
5205       else
5206 	{
5207 	  /* If the next token is not an identifier, then it is
5208 	     definitely not a type-name or namespace-name.  */
5209 	  if (token->type != CPP_NAME)
5210 	    break;
5211 	  /* If the following token is neither a `<' (to begin a
5212 	     template-id), nor a `::', then we are not looking at a
5213 	     nested-name-specifier.  */
5214 	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
5215 
5216 	  if (token->type == CPP_COLON
5217 	      && parser->colon_corrects_to_scope_p
5218 	      && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5219 	    {
5220 	      error_at (token->location,
5221 			"found %<:%> in nested-name-specifier, expected %<::%>");
5222 	      token->type = CPP_SCOPE;
5223 	    }
5224 
5225 	  if (token->type != CPP_SCOPE
5226 	      && !cp_parser_nth_token_starts_template_argument_list_p
5227 		  (parser, 2))
5228 	    break;
5229 	}
5230 
5231       /* The nested-name-specifier is optional, so we parse
5232 	 tentatively.  */
5233       cp_parser_parse_tentatively (parser);
5234 
5235       /* Look for the optional `template' keyword, if this isn't the
5236 	 first time through the loop.  */
5237       if (success)
5238 	template_keyword_p = cp_parser_optional_template_keyword (parser);
5239       else
5240 	template_keyword_p = false;
5241 
5242       /* Save the old scope since the name lookup we are about to do
5243 	 might destroy it.  */
5244       old_scope = parser->scope;
5245       saved_qualifying_scope = parser->qualifying_scope;
5246       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5247 	 look up names in "X<T>::I" in order to determine that "Y" is
5248 	 a template.  So, if we have a typename at this point, we make
5249 	 an effort to look through it.  */
5250       if (is_declaration
5251 	  && !typename_keyword_p
5252 	  && parser->scope
5253 	  && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5254 	parser->scope = resolve_typename_type (parser->scope,
5255 					       /*only_current_p=*/false);
5256       /* Parse the qualifying entity.  */
5257       new_scope
5258 	= cp_parser_qualifying_entity (parser,
5259                                        typename_keyword_p,
5260                                        template_keyword_p,
5261                                        check_dependency_p,
5262                                        type_p,
5263                                        is_declaration);
5264       /* Look for the `::' token.  */
5265       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5266 
5267       /* If we found what we wanted, we keep going; otherwise, we're
5268 	 done.  */
5269       if (!cp_parser_parse_definitely (parser))
5270 	{
5271 	  bool error_p = false;
5272 
5273 	  /* Restore the OLD_SCOPE since it was valid before the
5274 	     failed attempt at finding the last
5275 	     class-or-namespace-name.  */
5276 	  parser->scope = old_scope;
5277 	  parser->qualifying_scope = saved_qualifying_scope;
5278 
5279 	  /* If the next token is a decltype, and the one after that is a
5280 	     `::', then the decltype has failed to resolve to a class or
5281 	     enumeration type.  Give this error even when parsing
5282 	     tentatively since it can't possibly be valid--and we're going
5283 	     to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5284 	     won't get another chance.*/
5285 	  if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5286 	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5287 		  == CPP_SCOPE))
5288 	    {
5289 	      token = cp_lexer_consume_token (parser->lexer);
5290 	      error_at (token->location, "decltype evaluates to %qT, "
5291 			"which is not a class or enumeration type",
5292 			token->u.value);
5293 	      parser->scope = error_mark_node;
5294 	      error_p = true;
5295 	      /* As below.  */
5296 	      success = true;
5297 	      cp_lexer_consume_token (parser->lexer);
5298 	    }
5299 
5300 	  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5301 	    break;
5302 	  /* If the next token is an identifier, and the one after
5303 	     that is a `::', then any valid interpretation would have
5304 	     found a class-or-namespace-name.  */
5305 	  while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5306 		 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5307 		     == CPP_SCOPE)
5308 		 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5309 		     != CPP_COMPL))
5310 	    {
5311 	      token = cp_lexer_consume_token (parser->lexer);
5312 	      if (!error_p)
5313 		{
5314 		  if (!token->ambiguous_p)
5315 		    {
5316 		      tree decl;
5317 		      tree ambiguous_decls;
5318 
5319 		      decl = cp_parser_lookup_name (parser, token->u.value,
5320 						    none_type,
5321 						    /*is_template=*/false,
5322 						    /*is_namespace=*/false,
5323 						    /*check_dependency=*/true,
5324 						    &ambiguous_decls,
5325 						    token->location);
5326 		      if (TREE_CODE (decl) == TEMPLATE_DECL)
5327 			error_at (token->location,
5328 				  "%qD used without template parameters",
5329 				  decl);
5330 		      else if (ambiguous_decls)
5331 			{
5332 			  // cp_parser_lookup_name has the same diagnostic,
5333 			  // thus make sure to emit it at most once.
5334 			  if (cp_parser_uncommitted_to_tentative_parse_p
5335 			      (parser))
5336 			    {
5337 			      error_at (token->location,
5338 					"reference to %qD is ambiguous",
5339 					token->u.value);
5340 			      print_candidates (ambiguous_decls);
5341 			    }
5342 			  decl = error_mark_node;
5343 			}
5344 		      else
5345                         {
5346                           if (cxx_dialect != cxx98)
5347                             cp_parser_name_lookup_error
5348                             (parser, token->u.value, decl, NLE_NOT_CXX98,
5349 	  		     token->location);
5350 			  else
5351 			    cp_parser_name_lookup_error
5352 			    (parser, token->u.value, decl, NLE_CXX98,
5353 			     token->location);
5354                         }
5355 		    }
5356 		  parser->scope = error_mark_node;
5357 		  error_p = true;
5358 		  /* Treat this as a successful nested-name-specifier
5359 		     due to:
5360 
5361 		     [basic.lookup.qual]
5362 
5363 		     If the name found is not a class-name (clause
5364 		     _class_) or namespace-name (_namespace.def_), the
5365 		     program is ill-formed.  */
5366 		  success = true;
5367 		}
5368 	      cp_lexer_consume_token (parser->lexer);
5369 	    }
5370 	  break;
5371 	}
5372       /* We've found one valid nested-name-specifier.  */
5373       success = true;
5374       /* Name lookup always gives us a DECL.  */
5375       if (TREE_CODE (new_scope) == TYPE_DECL)
5376 	new_scope = TREE_TYPE (new_scope);
5377       /* Uses of "template" must be followed by actual templates.  */
5378       if (template_keyword_p
5379 	  && !(CLASS_TYPE_P (new_scope)
5380 	       && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5381 		    && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5382 		   || CLASSTYPE_IS_TEMPLATE (new_scope)))
5383 	  && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5384 	       && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5385 		   == TEMPLATE_ID_EXPR)))
5386 	permerror (input_location, TYPE_P (new_scope)
5387 		   ? G_("%qT is not a template")
5388 		   : G_("%qD is not a template"),
5389 		   new_scope);
5390       /* If it is a class scope, try to complete it; we are about to
5391 	 be looking up names inside the class.  */
5392       if (TYPE_P (new_scope)
5393 	  /* Since checking types for dependency can be expensive,
5394 	     avoid doing it if the type is already complete.  */
5395 	  && !COMPLETE_TYPE_P (new_scope)
5396 	  /* Do not try to complete dependent types.  */
5397 	  && !dependent_type_p (new_scope))
5398 	{
5399 	  new_scope = complete_type (new_scope);
5400 	  /* If it is a typedef to current class, use the current
5401 	     class instead, as the typedef won't have any names inside
5402 	     it yet.  */
5403 	  if (!COMPLETE_TYPE_P (new_scope)
5404 	      && currently_open_class (new_scope))
5405 	    new_scope = TYPE_MAIN_VARIANT (new_scope);
5406 	}
5407       /* Make sure we look in the right scope the next time through
5408 	 the loop.  */
5409       parser->scope = new_scope;
5410     }
5411 
5412   /* If parsing tentatively, replace the sequence of tokens that makes
5413      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5414      token.  That way, should we re-parse the token stream, we will
5415      not have to repeat the effort required to do the parse, nor will
5416      we issue duplicate error messages.  */
5417   if (success && start)
5418     {
5419       cp_token *token;
5420 
5421       token = cp_lexer_token_at (parser->lexer, start);
5422       /* Reset the contents of the START token.  */
5423       token->type = CPP_NESTED_NAME_SPECIFIER;
5424       /* Retrieve any deferred checks.  Do not pop this access checks yet
5425 	 so the memory will not be reclaimed during token replacing below.  */
5426       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5427       token->u.tree_check_value->value = parser->scope;
5428       token->u.tree_check_value->checks = get_deferred_access_checks ();
5429       token->u.tree_check_value->qualifying_scope =
5430 	parser->qualifying_scope;
5431       token->keyword = RID_MAX;
5432 
5433       /* Purge all subsequent tokens.  */
5434       cp_lexer_purge_tokens_after (parser->lexer, start);
5435     }
5436 
5437   if (start)
5438     pop_to_parent_deferring_access_checks ();
5439 
5440   return success ? parser->scope : NULL_TREE;
5441 }
5442 
5443 /* Parse a nested-name-specifier.  See
5444    cp_parser_nested_name_specifier_opt for details.  This function
5445    behaves identically, except that it will an issue an error if no
5446    nested-name-specifier is present.  */
5447 
5448 static tree
cp_parser_nested_name_specifier(cp_parser * parser,bool typename_keyword_p,bool check_dependency_p,bool type_p,bool is_declaration)5449 cp_parser_nested_name_specifier (cp_parser *parser,
5450 				 bool typename_keyword_p,
5451 				 bool check_dependency_p,
5452 				 bool type_p,
5453 				 bool is_declaration)
5454 {
5455   tree scope;
5456 
5457   /* Look for the nested-name-specifier.  */
5458   scope = cp_parser_nested_name_specifier_opt (parser,
5459 					       typename_keyword_p,
5460 					       check_dependency_p,
5461 					       type_p,
5462 					       is_declaration);
5463   /* If it was not present, issue an error message.  */
5464   if (!scope)
5465     {
5466       cp_parser_error (parser, "expected nested-name-specifier");
5467       parser->scope = NULL_TREE;
5468     }
5469 
5470   return scope;
5471 }
5472 
5473 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5474    this is either a class-name or a namespace-name (which corresponds
5475    to the class-or-namespace-name production in the grammar). For
5476    C++0x, it can also be a type-name that refers to an enumeration
5477    type or a simple-template-id.
5478 
5479    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5480    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5481    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5482    TYPE_P is TRUE iff the next name should be taken as a class-name,
5483    even the same name is declared to be another entity in the same
5484    scope.
5485 
5486    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5487    specified by the class-or-namespace-name.  If neither is found the
5488    ERROR_MARK_NODE is returned.  */
5489 
5490 static tree
cp_parser_qualifying_entity(cp_parser * parser,bool typename_keyword_p,bool template_keyword_p,bool check_dependency_p,bool type_p,bool is_declaration)5491 cp_parser_qualifying_entity (cp_parser *parser,
5492 			     bool typename_keyword_p,
5493 			     bool template_keyword_p,
5494 			     bool check_dependency_p,
5495 			     bool type_p,
5496 			     bool is_declaration)
5497 {
5498   tree saved_scope;
5499   tree saved_qualifying_scope;
5500   tree saved_object_scope;
5501   tree scope;
5502   bool only_class_p;
5503   bool successful_parse_p;
5504 
5505   /* DR 743: decltype can appear in a nested-name-specifier.  */
5506   if (cp_lexer_next_token_is_decltype (parser->lexer))
5507     {
5508       scope = cp_parser_decltype (parser);
5509       if (TREE_CODE (scope) != ENUMERAL_TYPE
5510 	  && !MAYBE_CLASS_TYPE_P (scope))
5511 	{
5512 	  cp_parser_simulate_error (parser);
5513 	  return error_mark_node;
5514 	}
5515       if (TYPE_NAME (scope))
5516 	scope = TYPE_NAME (scope);
5517       return scope;
5518     }
5519 
5520   /* Before we try to parse the class-name, we must save away the
5521      current PARSER->SCOPE since cp_parser_class_name will destroy
5522      it.  */
5523   saved_scope = parser->scope;
5524   saved_qualifying_scope = parser->qualifying_scope;
5525   saved_object_scope = parser->object_scope;
5526   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
5527      there is no need to look for a namespace-name.  */
5528   only_class_p = template_keyword_p
5529     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5530   if (!only_class_p)
5531     cp_parser_parse_tentatively (parser);
5532   scope = cp_parser_class_name (parser,
5533 				typename_keyword_p,
5534 				template_keyword_p,
5535 				type_p ? class_type : none_type,
5536 				check_dependency_p,
5537 				/*class_head_p=*/false,
5538 				is_declaration);
5539   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5540   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
5541   if (!only_class_p
5542       && cxx_dialect != cxx98
5543       && !successful_parse_p)
5544     {
5545       /* Restore the saved scope.  */
5546       parser->scope = saved_scope;
5547       parser->qualifying_scope = saved_qualifying_scope;
5548       parser->object_scope = saved_object_scope;
5549 
5550       /* Parse tentatively.  */
5551       cp_parser_parse_tentatively (parser);
5552 
5553       /* Parse a type-name  */
5554       scope = cp_parser_type_name (parser);
5555 
5556       /* "If the name found does not designate a namespace or a class,
5557 	 enumeration, or dependent type, the program is ill-formed."
5558 
5559          We cover classes and dependent types above and namespaces below,
5560          so this code is only looking for enums.  */
5561       if (!scope || TREE_CODE (scope) != TYPE_DECL
5562 	  || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5563 	cp_parser_simulate_error (parser);
5564 
5565       successful_parse_p = cp_parser_parse_definitely (parser);
5566     }
5567   /* If that didn't work, try for a namespace-name.  */
5568   if (!only_class_p && !successful_parse_p)
5569     {
5570       /* Restore the saved scope.  */
5571       parser->scope = saved_scope;
5572       parser->qualifying_scope = saved_qualifying_scope;
5573       parser->object_scope = saved_object_scope;
5574       /* If we are not looking at an identifier followed by the scope
5575 	 resolution operator, then this is not part of a
5576 	 nested-name-specifier.  (Note that this function is only used
5577 	 to parse the components of a nested-name-specifier.)  */
5578       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5579 	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5580 	return error_mark_node;
5581       scope = cp_parser_namespace_name (parser);
5582     }
5583 
5584   return scope;
5585 }
5586 
5587 /* Parse a postfix-expression.
5588 
5589    postfix-expression:
5590      primary-expression
5591      postfix-expression [ expression ]
5592      postfix-expression ( expression-list [opt] )
5593      simple-type-specifier ( expression-list [opt] )
5594      typename :: [opt] nested-name-specifier identifier
5595        ( expression-list [opt] )
5596      typename :: [opt] nested-name-specifier template [opt] template-id
5597        ( expression-list [opt] )
5598      postfix-expression . template [opt] id-expression
5599      postfix-expression -> template [opt] id-expression
5600      postfix-expression . pseudo-destructor-name
5601      postfix-expression -> pseudo-destructor-name
5602      postfix-expression ++
5603      postfix-expression --
5604      dynamic_cast < type-id > ( expression )
5605      static_cast < type-id > ( expression )
5606      reinterpret_cast < type-id > ( expression )
5607      const_cast < type-id > ( expression )
5608      typeid ( expression )
5609      typeid ( type-id )
5610 
5611    GNU Extension:
5612 
5613    postfix-expression:
5614      ( type-id ) { initializer-list , [opt] }
5615 
5616    This extension is a GNU version of the C99 compound-literal
5617    construct.  (The C99 grammar uses `type-name' instead of `type-id',
5618    but they are essentially the same concept.)
5619 
5620    If ADDRESS_P is true, the postfix expression is the operand of the
5621    `&' operator.  CAST_P is true if this expression is the target of a
5622    cast.
5623 
5624    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5625    class member access expressions [expr.ref].
5626 
5627    Returns a representation of the expression.  */
5628 
5629 static tree
cp_parser_postfix_expression(cp_parser * parser,bool address_p,bool cast_p,bool member_access_only_p,bool decltype_p,cp_id_kind * pidk_return)5630 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5631                               bool member_access_only_p, bool decltype_p,
5632 			      cp_id_kind * pidk_return)
5633 {
5634   cp_token *token;
5635   location_t loc;
5636   enum rid keyword;
5637   cp_id_kind idk = CP_ID_KIND_NONE;
5638   tree postfix_expression = NULL_TREE;
5639   bool is_member_access = false;
5640   int saved_in_statement = -1;
5641 
5642   /* Peek at the next token.  */
5643   token = cp_lexer_peek_token (parser->lexer);
5644   loc = token->location;
5645   /* Some of the productions are determined by keywords.  */
5646   keyword = token->keyword;
5647   switch (keyword)
5648     {
5649     case RID_DYNCAST:
5650     case RID_STATCAST:
5651     case RID_REINTCAST:
5652     case RID_CONSTCAST:
5653       {
5654 	tree type;
5655 	tree expression;
5656 	const char *saved_message;
5657 	bool saved_in_type_id_in_expr_p;
5658 
5659 	/* All of these can be handled in the same way from the point
5660 	   of view of parsing.  Begin by consuming the token
5661 	   identifying the cast.  */
5662 	cp_lexer_consume_token (parser->lexer);
5663 
5664 	/* New types cannot be defined in the cast.  */
5665 	saved_message = parser->type_definition_forbidden_message;
5666 	parser->type_definition_forbidden_message
5667 	  = G_("types may not be defined in casts");
5668 
5669 	/* Look for the opening `<'.  */
5670 	cp_parser_require (parser, CPP_LESS, RT_LESS);
5671 	/* Parse the type to which we are casting.  */
5672 	saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5673 	parser->in_type_id_in_expr_p = true;
5674 	type = cp_parser_type_id (parser);
5675 	parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5676 	/* Look for the closing `>'.  */
5677 	cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5678 	/* Restore the old message.  */
5679 	parser->type_definition_forbidden_message = saved_message;
5680 
5681 	bool saved_greater_than_is_operator_p
5682 	  = parser->greater_than_is_operator_p;
5683 	parser->greater_than_is_operator_p = true;
5684 
5685 	/* And the expression which is being cast.  */
5686 	cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5687 	expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5688 	cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5689 
5690 	parser->greater_than_is_operator_p
5691 	  = saved_greater_than_is_operator_p;
5692 
5693 	/* Only type conversions to integral or enumeration types
5694 	   can be used in constant-expressions.  */
5695 	if (!cast_valid_in_integral_constant_expression_p (type)
5696 	    && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5697 	  return error_mark_node;
5698 
5699 	switch (keyword)
5700 	  {
5701 	  case RID_DYNCAST:
5702 	    postfix_expression
5703 	      = build_dynamic_cast (type, expression, tf_warning_or_error);
5704 	    break;
5705 	  case RID_STATCAST:
5706 	    postfix_expression
5707 	      = build_static_cast (type, expression, tf_warning_or_error);
5708 	    break;
5709 	  case RID_REINTCAST:
5710 	    postfix_expression
5711 	      = build_reinterpret_cast (type, expression,
5712                                         tf_warning_or_error);
5713 	    break;
5714 	  case RID_CONSTCAST:
5715 	    postfix_expression
5716 	      = build_const_cast (type, expression, tf_warning_or_error);
5717 	    break;
5718 	  default:
5719 	    gcc_unreachable ();
5720 	  }
5721       }
5722       break;
5723 
5724     case RID_TYPEID:
5725       {
5726 	tree type;
5727 	const char *saved_message;
5728 	bool saved_in_type_id_in_expr_p;
5729 
5730 	/* Consume the `typeid' token.  */
5731 	cp_lexer_consume_token (parser->lexer);
5732 	/* Look for the `(' token.  */
5733 	cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5734 	/* Types cannot be defined in a `typeid' expression.  */
5735 	saved_message = parser->type_definition_forbidden_message;
5736 	parser->type_definition_forbidden_message
5737 	  = G_("types may not be defined in a %<typeid%> expression");
5738 	/* We can't be sure yet whether we're looking at a type-id or an
5739 	   expression.  */
5740 	cp_parser_parse_tentatively (parser);
5741 	/* Try a type-id first.  */
5742 	saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5743 	parser->in_type_id_in_expr_p = true;
5744 	type = cp_parser_type_id (parser);
5745 	parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5746 	/* Look for the `)' token.  Otherwise, we can't be sure that
5747 	   we're not looking at an expression: consider `typeid (int
5748 	   (3))', for example.  */
5749 	cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5750 	/* If all went well, simply lookup the type-id.  */
5751 	if (cp_parser_parse_definitely (parser))
5752 	  postfix_expression = get_typeid (type, tf_warning_or_error);
5753 	/* Otherwise, fall back to the expression variant.  */
5754 	else
5755 	  {
5756 	    tree expression;
5757 
5758 	    /* Look for an expression.  */
5759 	    expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5760 	    /* Compute its typeid.  */
5761 	    postfix_expression = build_typeid (expression, tf_warning_or_error);
5762 	    /* Look for the `)' token.  */
5763 	    cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5764 	  }
5765 	/* Restore the saved message.  */
5766 	parser->type_definition_forbidden_message = saved_message;
5767 	/* `typeid' may not appear in an integral constant expression.  */
5768 	if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5769 	  return error_mark_node;
5770       }
5771       break;
5772 
5773     case RID_TYPENAME:
5774       {
5775 	tree type;
5776 	/* The syntax permitted here is the same permitted for an
5777 	   elaborated-type-specifier.  */
5778 	type = cp_parser_elaborated_type_specifier (parser,
5779 						    /*is_friend=*/false,
5780 						    /*is_declaration=*/false);
5781 	postfix_expression = cp_parser_functional_cast (parser, type);
5782       }
5783       break;
5784 
5785     case RID_CILK_SPAWN:
5786       {
5787 	cp_lexer_consume_token (parser->lexer);
5788 	token = cp_lexer_peek_token (parser->lexer);
5789 	if (token->type == CPP_SEMICOLON)
5790 	  {
5791 	    error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5792 		      "an expression");
5793 	    postfix_expression = error_mark_node;
5794 	    break;
5795 	  }
5796 	else if (!current_function_decl)
5797 	  {
5798 	    error_at (token->location, "%<_Cilk_spawn%> may only be used "
5799 		      "inside a function");
5800 	    postfix_expression = error_mark_node;
5801 	    break;
5802 	  }
5803 	else
5804 	  {
5805 	    /* Consecutive _Cilk_spawns are not allowed in a statement.  */
5806 	    saved_in_statement = parser->in_statement;
5807 	    parser->in_statement |= IN_CILK_SPAWN;
5808 	  }
5809 	cfun->calls_cilk_spawn = 1;
5810 	postfix_expression =
5811 	  cp_parser_postfix_expression (parser, false, false,
5812 					false, false, &idk);
5813 	if (!flag_cilkplus)
5814 	  {
5815 	    error_at (token->location, "-fcilkplus must be enabled to use"
5816 		      " %<_Cilk_spawn%>");
5817 	    cfun->calls_cilk_spawn = 0;
5818 	  }
5819 	else if (saved_in_statement & IN_CILK_SPAWN)
5820 	  {
5821 	    error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5822 		      "are not permitted");
5823 	    postfix_expression = error_mark_node;
5824 	    cfun->calls_cilk_spawn = 0;
5825 	  }
5826 	else
5827 	  {
5828 	    postfix_expression = build_cilk_spawn (token->location,
5829 						   postfix_expression);
5830 	    if (postfix_expression != error_mark_node)
5831 	      SET_EXPR_LOCATION (postfix_expression, input_location);
5832 	    parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5833 	  }
5834 	break;
5835       }
5836 
5837     case RID_BUILTIN_SHUFFLE:
5838       {
5839 	vec<tree, va_gc> *vec;
5840 	unsigned int i;
5841 	tree p;
5842 
5843 	cp_lexer_consume_token (parser->lexer);
5844 	vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5845 		    /*cast_p=*/false, /*allow_expansion_p=*/true,
5846 		    /*non_constant_p=*/NULL);
5847 	if (vec == NULL)
5848 	  return error_mark_node;
5849 
5850 	FOR_EACH_VEC_ELT (*vec, i, p)
5851 	  mark_exp_read (p);
5852 
5853 	if (vec->length () == 2)
5854 	  return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5855 					 tf_warning_or_error);
5856 	else if (vec->length () == 3)
5857 	  return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5858 					 tf_warning_or_error);
5859 	else
5860 	{
5861 	  error_at (loc, "wrong number of arguments to "
5862 	      "%<__builtin_shuffle%>");
5863 	  return error_mark_node;
5864 	}
5865 	break;
5866       }
5867 
5868     default:
5869       {
5870 	tree type;
5871 
5872 	/* If the next thing is a simple-type-specifier, we may be
5873 	   looking at a functional cast.  We could also be looking at
5874 	   an id-expression.  So, we try the functional cast, and if
5875 	   that doesn't work we fall back to the primary-expression.  */
5876 	cp_parser_parse_tentatively (parser);
5877 	/* Look for the simple-type-specifier.  */
5878 	type = cp_parser_simple_type_specifier (parser,
5879 						/*decl_specs=*/NULL,
5880 						CP_PARSER_FLAGS_NONE);
5881 	/* Parse the cast itself.  */
5882 	if (!cp_parser_error_occurred (parser))
5883 	  postfix_expression
5884 	    = cp_parser_functional_cast (parser, type);
5885 	/* If that worked, we're done.  */
5886 	if (cp_parser_parse_definitely (parser))
5887 	  break;
5888 
5889 	/* If the functional-cast didn't work out, try a
5890 	   compound-literal.  */
5891 	if (cp_parser_allow_gnu_extensions_p (parser)
5892 	    && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5893 	  {
5894 	    tree initializer = NULL_TREE;
5895 	    bool compound_literal_p;
5896 
5897 	    cp_parser_parse_tentatively (parser);
5898 	    /* Consume the `('.  */
5899 	    cp_lexer_consume_token (parser->lexer);
5900 
5901 	    /* Avoid calling cp_parser_type_id pointlessly, see comment
5902 	       in cp_parser_cast_expression about c++/29234.  */
5903 	    cp_lexer_save_tokens (parser->lexer);
5904 
5905 	    compound_literal_p
5906 	      = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5907 							/*consume_paren=*/true)
5908 		 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5909 
5910 	    /* Roll back the tokens we skipped.  */
5911 	    cp_lexer_rollback_tokens (parser->lexer);
5912 
5913 	    if (!compound_literal_p)
5914 	      cp_parser_simulate_error (parser);
5915 	    else
5916 	      {
5917 		/* Parse the type.  */
5918 		bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5919 		parser->in_type_id_in_expr_p = true;
5920 		type = cp_parser_type_id (parser);
5921 		parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5922 		/* Look for the `)'.  */
5923 		cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5924 	      }
5925 
5926 	    /* If things aren't going well, there's no need to
5927 	       keep going.  */
5928 	    if (!cp_parser_error_occurred (parser))
5929 	      {
5930 		bool non_constant_p;
5931 		/* Parse the brace-enclosed initializer list.  */
5932 		initializer = cp_parser_braced_list (parser,
5933 						     &non_constant_p);
5934 	      }
5935 	    /* If that worked, we're definitely looking at a
5936 	       compound-literal expression.  */
5937 	    if (cp_parser_parse_definitely (parser))
5938 	      {
5939 		/* Warn the user that a compound literal is not
5940 		   allowed in standard C++.  */
5941 		pedwarn (input_location, OPT_Wpedantic,
5942 			 "ISO C++ forbids compound-literals");
5943 		/* For simplicity, we disallow compound literals in
5944 		   constant-expressions.  We could
5945 		   allow compound literals of integer type, whose
5946 		   initializer was a constant, in constant
5947 		   expressions.  Permitting that usage, as a further
5948 		   extension, would not change the meaning of any
5949 		   currently accepted programs.  (Of course, as
5950 		   compound literals are not part of ISO C++, the
5951 		   standard has nothing to say.)  */
5952 		if (cp_parser_non_integral_constant_expression (parser,
5953 								NIC_NCC))
5954 		  {
5955 		    postfix_expression = error_mark_node;
5956 		    break;
5957 		  }
5958 		/* Form the representation of the compound-literal.  */
5959 		postfix_expression
5960 		  = finish_compound_literal (type, initializer,
5961 					     tf_warning_or_error);
5962 		break;
5963 	      }
5964 	  }
5965 
5966 	/* It must be a primary-expression.  */
5967 	postfix_expression
5968 	  = cp_parser_primary_expression (parser, address_p, cast_p,
5969 					  /*template_arg_p=*/false,
5970 					  decltype_p,
5971 					  &idk);
5972       }
5973       break;
5974     }
5975 
5976   /* Note that we don't need to worry about calling build_cplus_new on a
5977      class-valued CALL_EXPR in decltype when it isn't the end of the
5978      postfix-expression; unary_complex_lvalue will take care of that for
5979      all these cases.  */
5980 
5981   /* Keep looping until the postfix-expression is complete.  */
5982   while (true)
5983     {
5984       if (idk == CP_ID_KIND_UNQUALIFIED
5985 	  && identifier_p (postfix_expression)
5986 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5987 	/* It is not a Koenig lookup function call.  */
5988 	postfix_expression
5989 	  = unqualified_name_lookup_error (postfix_expression);
5990 
5991       /* Peek at the next token.  */
5992       token = cp_lexer_peek_token (parser->lexer);
5993 
5994       switch (token->type)
5995 	{
5996 	case CPP_OPEN_SQUARE:
5997 	  if (cp_next_tokens_can_be_std_attribute_p (parser))
5998 	    {
5999 	      cp_parser_error (parser,
6000 			       "two consecutive %<[%> shall "
6001 			       "only introduce an attribute");
6002 	      return error_mark_node;
6003 	    }
6004 	  postfix_expression
6005 	    = cp_parser_postfix_open_square_expression (parser,
6006 							postfix_expression,
6007 							false,
6008 							decltype_p);
6009 	  idk = CP_ID_KIND_NONE;
6010           is_member_access = false;
6011 	  break;
6012 
6013 	case CPP_OPEN_PAREN:
6014 	  /* postfix-expression ( expression-list [opt] ) */
6015 	  {
6016 	    bool koenig_p;
6017 	    bool is_builtin_constant_p;
6018 	    bool saved_integral_constant_expression_p = false;
6019 	    bool saved_non_integral_constant_expression_p = false;
6020 	    tsubst_flags_t complain = complain_flags (decltype_p);
6021 	    vec<tree, va_gc> *args;
6022 
6023             is_member_access = false;
6024 
6025 	    is_builtin_constant_p
6026 	      = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6027 	    if (is_builtin_constant_p)
6028 	      {
6029 		/* The whole point of __builtin_constant_p is to allow
6030 		   non-constant expressions to appear as arguments.  */
6031 		saved_integral_constant_expression_p
6032 		  = parser->integral_constant_expression_p;
6033 		saved_non_integral_constant_expression_p
6034 		  = parser->non_integral_constant_expression_p;
6035 		parser->integral_constant_expression_p = false;
6036 	      }
6037 	    args = (cp_parser_parenthesized_expression_list
6038 		    (parser, non_attr,
6039 		     /*cast_p=*/false, /*allow_expansion_p=*/true,
6040 		     /*non_constant_p=*/NULL));
6041 	    if (is_builtin_constant_p)
6042 	      {
6043 		parser->integral_constant_expression_p
6044 		  = saved_integral_constant_expression_p;
6045 		parser->non_integral_constant_expression_p
6046 		  = saved_non_integral_constant_expression_p;
6047 	      }
6048 
6049 	    if (args == NULL)
6050 	      {
6051 		postfix_expression = error_mark_node;
6052 		break;
6053 	      }
6054 
6055 	    /* Function calls are not permitted in
6056 	       constant-expressions.  */
6057 	    if (! builtin_valid_in_constant_expr_p (postfix_expression)
6058 		&& cp_parser_non_integral_constant_expression (parser,
6059 							       NIC_FUNC_CALL))
6060 	      {
6061 		postfix_expression = error_mark_node;
6062 		release_tree_vector (args);
6063 		break;
6064 	      }
6065 
6066 	    koenig_p = false;
6067 	    if (idk == CP_ID_KIND_UNQUALIFIED
6068 		|| idk == CP_ID_KIND_TEMPLATE_ID)
6069 	      {
6070 		if (identifier_p (postfix_expression))
6071 		  {
6072 		    if (!args->is_empty ())
6073 		      {
6074 			koenig_p = true;
6075 			if (!any_type_dependent_arguments_p (args))
6076 			  postfix_expression
6077 			    = perform_koenig_lookup (postfix_expression, args,
6078 						     complain);
6079 		      }
6080 		    else
6081 		      postfix_expression
6082 			= unqualified_fn_lookup_error (postfix_expression);
6083 		  }
6084 		/* We do not perform argument-dependent lookup if
6085 		   normal lookup finds a non-function, in accordance
6086 		   with the expected resolution of DR 218.  */
6087 		else if (!args->is_empty ()
6088 			 && is_overloaded_fn (postfix_expression))
6089 		  {
6090 		    tree fn = get_first_fn (postfix_expression);
6091 		    fn = STRIP_TEMPLATE (fn);
6092 
6093 		    /* Do not do argument dependent lookup if regular
6094 		       lookup finds a member function or a block-scope
6095 		       function declaration.  [basic.lookup.argdep]/3  */
6096 		    if (!DECL_FUNCTION_MEMBER_P (fn)
6097 			&& !DECL_LOCAL_FUNCTION_P (fn))
6098 		      {
6099 			koenig_p = true;
6100 			if (!any_type_dependent_arguments_p (args))
6101 			  postfix_expression
6102 			    = perform_koenig_lookup (postfix_expression, args,
6103 						     complain);
6104 		      }
6105 		  }
6106 	      }
6107 
6108 	    if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6109 	      {
6110 		tree instance = TREE_OPERAND (postfix_expression, 0);
6111 		tree fn = TREE_OPERAND (postfix_expression, 1);
6112 
6113 		if (processing_template_decl
6114 		    && (type_dependent_expression_p (instance)
6115 			|| (!BASELINK_P (fn)
6116 			    && TREE_CODE (fn) != FIELD_DECL)
6117 			|| type_dependent_expression_p (fn)
6118 			|| any_type_dependent_arguments_p (args)))
6119 		  {
6120 		    postfix_expression
6121 		      = build_nt_call_vec (postfix_expression, args);
6122 		    release_tree_vector (args);
6123 		    break;
6124 		  }
6125 
6126 		if (BASELINK_P (fn))
6127 		  {
6128 		  postfix_expression
6129 		    = (build_new_method_call
6130 		       (instance, fn, &args, NULL_TREE,
6131 			(idk == CP_ID_KIND_QUALIFIED
6132 			 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6133 			 : LOOKUP_NORMAL),
6134 			/*fn_p=*/NULL,
6135 			complain));
6136 		  }
6137 		else
6138 		  postfix_expression
6139 		    = finish_call_expr (postfix_expression, &args,
6140 					/*disallow_virtual=*/false,
6141 					/*koenig_p=*/false,
6142 					complain);
6143 	      }
6144 	    else if (TREE_CODE (postfix_expression) == OFFSET_REF
6145 		     || TREE_CODE (postfix_expression) == MEMBER_REF
6146 		     || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6147 	      postfix_expression = (build_offset_ref_call_from_tree
6148 				    (postfix_expression, &args,
6149 				     complain));
6150 	    else if (idk == CP_ID_KIND_QUALIFIED)
6151 	      /* A call to a static class member, or a namespace-scope
6152 		 function.  */
6153 	      postfix_expression
6154 		= finish_call_expr (postfix_expression, &args,
6155 				    /*disallow_virtual=*/true,
6156 				    koenig_p,
6157 				    complain);
6158 	    else
6159 	      /* All other function calls.  */
6160 	      postfix_expression
6161 		= finish_call_expr (postfix_expression, &args,
6162 				    /*disallow_virtual=*/false,
6163 				    koenig_p,
6164 				    complain);
6165 
6166 	    /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
6167 	    idk = CP_ID_KIND_NONE;
6168 
6169 	    release_tree_vector (args);
6170 	  }
6171 	  break;
6172 
6173 	case CPP_DOT:
6174 	case CPP_DEREF:
6175 	  /* postfix-expression . template [opt] id-expression
6176 	     postfix-expression . pseudo-destructor-name
6177 	     postfix-expression -> template [opt] id-expression
6178 	     postfix-expression -> pseudo-destructor-name */
6179 
6180 	  /* Consume the `.' or `->' operator.  */
6181 	  cp_lexer_consume_token (parser->lexer);
6182 
6183 	  postfix_expression
6184 	    = cp_parser_postfix_dot_deref_expression (parser, token->type,
6185 						      postfix_expression,
6186 						      false, &idk, loc);
6187 
6188           is_member_access = true;
6189 	  break;
6190 
6191 	case CPP_PLUS_PLUS:
6192 	  /* postfix-expression ++  */
6193 	  /* Consume the `++' token.  */
6194 	  cp_lexer_consume_token (parser->lexer);
6195 	  /* Generate a representation for the complete expression.  */
6196 	  postfix_expression
6197 	    = finish_increment_expr (postfix_expression,
6198 				     POSTINCREMENT_EXPR);
6199 	  /* Increments may not appear in constant-expressions.  */
6200 	  if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6201 	    postfix_expression = error_mark_node;
6202 	  idk = CP_ID_KIND_NONE;
6203           is_member_access = false;
6204 	  break;
6205 
6206 	case CPP_MINUS_MINUS:
6207 	  /* postfix-expression -- */
6208 	  /* Consume the `--' token.  */
6209 	  cp_lexer_consume_token (parser->lexer);
6210 	  /* Generate a representation for the complete expression.  */
6211 	  postfix_expression
6212 	    = finish_increment_expr (postfix_expression,
6213 				     POSTDECREMENT_EXPR);
6214 	  /* Decrements may not appear in constant-expressions.  */
6215 	  if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6216 	    postfix_expression = error_mark_node;
6217 	  idk = CP_ID_KIND_NONE;
6218           is_member_access = false;
6219 	  break;
6220 
6221 	default:
6222 	  if (pidk_return != NULL)
6223 	    * pidk_return = idk;
6224           if (member_access_only_p)
6225             return is_member_access? postfix_expression : error_mark_node;
6226           else
6227             return postfix_expression;
6228 	}
6229     }
6230 
6231   /* We should never get here.  */
6232   gcc_unreachable ();
6233   return error_mark_node;
6234 }
6235 
6236 /* This function parses Cilk Plus array notations.  If a normal array expr. is
6237    parsed then the array index is passed back to the caller through *INIT_INDEX
6238    and the function returns a NULL_TREE.  If array notation expr. is parsed,
6239    then *INIT_INDEX is ignored by the caller and the function returns
6240    a tree of type ARRAY_NOTATION_REF.  If some error occurred it returns
6241    error_mark_node.  */
6242 
6243 static tree
cp_parser_array_notation(location_t loc,cp_parser * parser,tree * init_index,tree array_value)6244 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6245 			  tree array_value)
6246 {
6247   cp_token *token = NULL;
6248   tree length_index, stride = NULL_TREE, value_tree, array_type;
6249   if (!array_value || array_value == error_mark_node)
6250     {
6251       cp_parser_skip_to_end_of_statement (parser);
6252       return error_mark_node;
6253     }
6254 
6255   array_type = TREE_TYPE (array_value);
6256 
6257   bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6258   parser->colon_corrects_to_scope_p = false;
6259   token = cp_lexer_peek_token (parser->lexer);
6260 
6261   if (!token)
6262     {
6263       cp_parser_error (parser, "expected %<:%> or numeral");
6264       return error_mark_node;
6265     }
6266   else if (token->type == CPP_COLON)
6267     {
6268       /* Consume the ':'.  */
6269       cp_lexer_consume_token (parser->lexer);
6270 
6271       /* If we are here, then we have a case like this A[:].  */
6272       if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6273 	{
6274 	  cp_parser_error (parser, "expected %<]%>");
6275 	  cp_parser_skip_to_end_of_statement (parser);
6276 	  return error_mark_node;
6277 	}
6278       *init_index = NULL_TREE;
6279       stride = NULL_TREE;
6280       length_index = NULL_TREE;
6281     }
6282   else
6283     {
6284       /* If we are here, then there are three valid possibilities:
6285 	 1. ARRAY [ EXP ]
6286 	 2. ARRAY [ EXP : EXP ]
6287 	 3. ARRAY [ EXP : EXP : EXP ]  */
6288 
6289       *init_index = cp_parser_expression (parser, false, NULL);
6290       if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6291 	{
6292 	  /* This indicates that we have a normal array expression.  */
6293 	  parser->colon_corrects_to_scope_p = saved_colon_corrects;
6294 	  return NULL_TREE;
6295 	}
6296 
6297       /* Consume the ':'.  */
6298       cp_lexer_consume_token (parser->lexer);
6299       length_index = cp_parser_expression (parser, false, NULL);
6300       if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6301 	{
6302 	  cp_lexer_consume_token (parser->lexer);
6303 	  stride = cp_parser_expression (parser, false, NULL);
6304 	}
6305     }
6306   parser->colon_corrects_to_scope_p = saved_colon_corrects;
6307 
6308   if (*init_index == error_mark_node || length_index == error_mark_node
6309       || stride == error_mark_node || array_type == error_mark_node)
6310     {
6311       if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6312 	cp_lexer_consume_token (parser->lexer);
6313       return error_mark_node;
6314     }
6315   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6316 
6317   value_tree = build_array_notation_ref (loc, array_value, *init_index,
6318 					 length_index, stride, array_type);
6319   return value_tree;
6320 }
6321 
6322 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6323    by cp_parser_builtin_offsetof.  We're looking for
6324 
6325      postfix-expression [ expression ]
6326      postfix-expression [ braced-init-list ] (C++11)
6327 
6328    FOR_OFFSETOF is set if we're being called in that context, which
6329    changes how we deal with integer constant expressions.  */
6330 
6331 static tree
cp_parser_postfix_open_square_expression(cp_parser * parser,tree postfix_expression,bool for_offsetof,bool decltype_p)6332 cp_parser_postfix_open_square_expression (cp_parser *parser,
6333 					  tree postfix_expression,
6334 					  bool for_offsetof,
6335 					  bool decltype_p)
6336 {
6337   tree index = NULL_TREE;
6338   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6339   bool saved_greater_than_is_operator_p;
6340 
6341   /* Consume the `[' token.  */
6342   cp_lexer_consume_token (parser->lexer);
6343 
6344   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6345   parser->greater_than_is_operator_p = true;
6346 
6347   /* Parse the index expression.  */
6348   /* ??? For offsetof, there is a question of what to allow here.  If
6349      offsetof is not being used in an integral constant expression context,
6350      then we *could* get the right answer by computing the value at runtime.
6351      If we are in an integral constant expression context, then we might
6352      could accept any constant expression; hard to say without analysis.
6353      Rather than open the barn door too wide right away, allow only integer
6354      constant expressions here.  */
6355   if (for_offsetof)
6356     index = cp_parser_constant_expression (parser, false, NULL);
6357   else
6358     {
6359       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6360 	{
6361 	  bool expr_nonconst_p;
6362 	  cp_lexer_set_source_position (parser->lexer);
6363 	  maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6364 	  index = cp_parser_braced_list (parser, &expr_nonconst_p);
6365 	  if (flag_cilkplus
6366 	      && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6367 	    {
6368 	      error_at (cp_lexer_peek_token (parser->lexer)->location,
6369 			"braced list index is not allowed with array "
6370 			"notation");
6371 	      cp_parser_skip_to_end_of_statement (parser);
6372 	      return error_mark_node;
6373 	    }
6374 	}
6375       else if (flag_cilkplus)
6376 	{
6377 	  /* Here are have these two options:
6378 	     ARRAY[EXP : EXP]        - Array notation expr with default
6379 	     stride of 1.
6380 	     ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6381 	     stride.  */
6382 	  tree an_exp = cp_parser_array_notation (loc, parser, &index,
6383 						  postfix_expression);
6384 	  if (an_exp)
6385 	    return an_exp;
6386 	}
6387       else
6388 	index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6389     }
6390 
6391   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6392 
6393   /* Look for the closing `]'.  */
6394   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6395 
6396   /* Build the ARRAY_REF.  */
6397   postfix_expression = grok_array_decl (loc, postfix_expression,
6398 					index, decltype_p);
6399 
6400   /* When not doing offsetof, array references are not permitted in
6401      constant-expressions.  */
6402   if (!for_offsetof
6403       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6404     postfix_expression = error_mark_node;
6405 
6406   return postfix_expression;
6407 }
6408 
6409 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6410    by cp_parser_builtin_offsetof.  We're looking for
6411 
6412      postfix-expression . template [opt] id-expression
6413      postfix-expression . pseudo-destructor-name
6414      postfix-expression -> template [opt] id-expression
6415      postfix-expression -> pseudo-destructor-name
6416 
6417    FOR_OFFSETOF is set if we're being called in that context.  That sorta
6418    limits what of the above we'll actually accept, but nevermind.
6419    TOKEN_TYPE is the "." or "->" token, which will already have been
6420    removed from the stream.  */
6421 
6422 static tree
cp_parser_postfix_dot_deref_expression(cp_parser * parser,enum cpp_ttype token_type,tree postfix_expression,bool for_offsetof,cp_id_kind * idk,location_t location)6423 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6424 					enum cpp_ttype token_type,
6425 					tree postfix_expression,
6426 					bool for_offsetof, cp_id_kind *idk,
6427 					location_t location)
6428 {
6429   tree name;
6430   bool dependent_p;
6431   bool pseudo_destructor_p;
6432   tree scope = NULL_TREE;
6433 
6434   /* If this is a `->' operator, dereference the pointer.  */
6435   if (token_type == CPP_DEREF)
6436     postfix_expression = build_x_arrow (location, postfix_expression,
6437 					tf_warning_or_error);
6438   /* Check to see whether or not the expression is type-dependent.  */
6439   dependent_p = type_dependent_expression_p (postfix_expression);
6440   /* The identifier following the `->' or `.' is not qualified.  */
6441   parser->scope = NULL_TREE;
6442   parser->qualifying_scope = NULL_TREE;
6443   parser->object_scope = NULL_TREE;
6444   *idk = CP_ID_KIND_NONE;
6445 
6446   /* Enter the scope corresponding to the type of the object
6447      given by the POSTFIX_EXPRESSION.  */
6448   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6449     {
6450       scope = TREE_TYPE (postfix_expression);
6451       /* According to the standard, no expression should ever have
6452 	 reference type.  Unfortunately, we do not currently match
6453 	 the standard in this respect in that our internal representation
6454 	 of an expression may have reference type even when the standard
6455 	 says it does not.  Therefore, we have to manually obtain the
6456 	 underlying type here.  */
6457       scope = non_reference (scope);
6458       /* The type of the POSTFIX_EXPRESSION must be complete.  */
6459       if (scope == unknown_type_node)
6460 	{
6461 	  error_at (location, "%qE does not have class type",
6462 		    postfix_expression);
6463 	  scope = NULL_TREE;
6464 	}
6465       /* Unlike the object expression in other contexts, *this is not
6466 	 required to be of complete type for purposes of class member
6467 	 access (5.2.5) outside the member function body.  */
6468       else if (postfix_expression != current_class_ref
6469 	       && !(processing_template_decl && scope == current_class_type))
6470 	scope = complete_type_or_else (scope, NULL_TREE);
6471       /* Let the name lookup machinery know that we are processing a
6472 	 class member access expression.  */
6473       parser->context->object_type = scope;
6474       /* If something went wrong, we want to be able to discern that case,
6475 	 as opposed to the case where there was no SCOPE due to the type
6476 	 of expression being dependent.  */
6477       if (!scope)
6478 	scope = error_mark_node;
6479       /* If the SCOPE was erroneous, make the various semantic analysis
6480 	 functions exit quickly -- and without issuing additional error
6481 	 messages.  */
6482       if (scope == error_mark_node)
6483 	postfix_expression = error_mark_node;
6484     }
6485 
6486   /* Assume this expression is not a pseudo-destructor access.  */
6487   pseudo_destructor_p = false;
6488 
6489   /* If the SCOPE is a scalar type, then, if this is a valid program,
6490      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
6491      is type dependent, it can be pseudo-destructor-name or something else.
6492      Try to parse it as pseudo-destructor-name first.  */
6493   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6494     {
6495       tree s;
6496       tree type;
6497 
6498       cp_parser_parse_tentatively (parser);
6499       /* Parse the pseudo-destructor-name.  */
6500       s = NULL_TREE;
6501       cp_parser_pseudo_destructor_name (parser, postfix_expression,
6502 					&s, &type);
6503       if (dependent_p
6504 	  && (cp_parser_error_occurred (parser)
6505 	      || !SCALAR_TYPE_P (type)))
6506 	cp_parser_abort_tentative_parse (parser);
6507       else if (cp_parser_parse_definitely (parser))
6508 	{
6509 	  pseudo_destructor_p = true;
6510 	  postfix_expression
6511 	    = finish_pseudo_destructor_expr (postfix_expression,
6512 					     s, type, location);
6513 	}
6514     }
6515 
6516   if (!pseudo_destructor_p)
6517     {
6518       /* If the SCOPE is not a scalar type, we are looking at an
6519 	 ordinary class member access expression, rather than a
6520 	 pseudo-destructor-name.  */
6521       bool template_p;
6522       cp_token *token = cp_lexer_peek_token (parser->lexer);
6523       /* Parse the id-expression.  */
6524       name = (cp_parser_id_expression
6525 	      (parser,
6526 	       cp_parser_optional_template_keyword (parser),
6527 	       /*check_dependency_p=*/true,
6528 	       &template_p,
6529 	       /*declarator_p=*/false,
6530 	       /*optional_p=*/false));
6531       /* In general, build a SCOPE_REF if the member name is qualified.
6532 	 However, if the name was not dependent and has already been
6533 	 resolved; there is no need to build the SCOPE_REF.  For example;
6534 
6535 	     struct X { void f(); };
6536 	     template <typename T> void f(T* t) { t->X::f(); }
6537 
6538 	 Even though "t" is dependent, "X::f" is not and has been resolved
6539 	 to a BASELINK; there is no need to include scope information.  */
6540 
6541       /* But we do need to remember that there was an explicit scope for
6542 	 virtual function calls.  */
6543       if (parser->scope)
6544 	*idk = CP_ID_KIND_QUALIFIED;
6545 
6546       /* If the name is a template-id that names a type, we will get a
6547 	 TYPE_DECL here.  That is invalid code.  */
6548       if (TREE_CODE (name) == TYPE_DECL)
6549 	{
6550 	  error_at (token->location, "invalid use of %qD", name);
6551 	  postfix_expression = error_mark_node;
6552 	}
6553       else
6554 	{
6555 	  if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6556 	    {
6557 	      if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6558 		{
6559 		  error_at (token->location, "%<%D::%D%> is not a class member",
6560 			    parser->scope, name);
6561 		  postfix_expression = error_mark_node;
6562 		}
6563 	      else
6564 		name = build_qualified_name (/*type=*/NULL_TREE,
6565 					     parser->scope,
6566 					     name,
6567 					     template_p);
6568 	      parser->scope = NULL_TREE;
6569 	      parser->qualifying_scope = NULL_TREE;
6570 	      parser->object_scope = NULL_TREE;
6571 	    }
6572 	  if (parser->scope && name && BASELINK_P (name))
6573 	    adjust_result_of_qualified_name_lookup
6574 	      (name, parser->scope, scope);
6575 	  postfix_expression
6576 	    = finish_class_member_access_expr (postfix_expression, name,
6577 					       template_p,
6578 					       tf_warning_or_error);
6579 	}
6580     }
6581 
6582   /* We no longer need to look up names in the scope of the object on
6583      the left-hand side of the `.' or `->' operator.  */
6584   parser->context->object_type = NULL_TREE;
6585 
6586   /* Outside of offsetof, these operators may not appear in
6587      constant-expressions.  */
6588   if (!for_offsetof
6589       && (cp_parser_non_integral_constant_expression
6590 	  (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6591     postfix_expression = error_mark_node;
6592 
6593   return postfix_expression;
6594 }
6595 
6596 /* Parse a parenthesized expression-list.
6597 
6598    expression-list:
6599      assignment-expression
6600      expression-list, assignment-expression
6601 
6602    attribute-list:
6603      expression-list
6604      identifier
6605      identifier, expression-list
6606 
6607    CAST_P is true if this expression is the target of a cast.
6608 
6609    ALLOW_EXPANSION_P is true if this expression allows expansion of an
6610    argument pack.
6611 
6612    Returns a vector of trees.  Each element is a representation of an
6613    assignment-expression.  NULL is returned if the ( and or ) are
6614    missing.  An empty, but allocated, vector is returned on no
6615    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
6616    if we are parsing an attribute list for an attribute that wants a
6617    plain identifier argument, normal_attr for an attribute that wants
6618    an expression, or non_attr if we aren't parsing an attribute list.  If
6619    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6620    not all of the expressions in the list were constant.  */
6621 
6622 static vec<tree, va_gc> *
cp_parser_parenthesized_expression_list(cp_parser * parser,int is_attribute_list,bool cast_p,bool allow_expansion_p,bool * non_constant_p)6623 cp_parser_parenthesized_expression_list (cp_parser* parser,
6624 					 int is_attribute_list,
6625 					 bool cast_p,
6626                                          bool allow_expansion_p,
6627 					 bool *non_constant_p)
6628 {
6629   vec<tree, va_gc> *expression_list;
6630   bool fold_expr_p = is_attribute_list != non_attr;
6631   tree identifier = NULL_TREE;
6632   bool saved_greater_than_is_operator_p;
6633 
6634   /* Assume all the expressions will be constant.  */
6635   if (non_constant_p)
6636     *non_constant_p = false;
6637 
6638   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6639     return NULL;
6640 
6641   expression_list = make_tree_vector ();
6642 
6643   /* Within a parenthesized expression, a `>' token is always
6644      the greater-than operator.  */
6645   saved_greater_than_is_operator_p
6646     = parser->greater_than_is_operator_p;
6647   parser->greater_than_is_operator_p = true;
6648 
6649   /* Consume expressions until there are no more.  */
6650   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6651     while (true)
6652       {
6653 	tree expr;
6654 
6655 	/* At the beginning of attribute lists, check to see if the
6656 	   next token is an identifier.  */
6657 	if (is_attribute_list == id_attr
6658 	    && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6659 	  {
6660 	    cp_token *token;
6661 
6662 	    /* Consume the identifier.  */
6663 	    token = cp_lexer_consume_token (parser->lexer);
6664 	    /* Save the identifier.  */
6665 	    identifier = token->u.value;
6666 	  }
6667 	else
6668 	  {
6669 	    bool expr_non_constant_p;
6670 
6671 	    /* Parse the next assignment-expression.  */
6672 	    if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6673 	      {
6674 		/* A braced-init-list.  */
6675 		cp_lexer_set_source_position (parser->lexer);
6676 		maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6677 		expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6678 		if (non_constant_p && expr_non_constant_p)
6679 		  *non_constant_p = true;
6680 	      }
6681 	    else if (non_constant_p)
6682 	      {
6683 		expr = (cp_parser_constant_expression
6684 			(parser, /*allow_non_constant_p=*/true,
6685 			 &expr_non_constant_p));
6686 		if (expr_non_constant_p)
6687 		  *non_constant_p = true;
6688 	      }
6689 	    else
6690 	      expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6691 
6692 	    if (fold_expr_p)
6693 	      expr = fold_non_dependent_expr (expr);
6694 
6695             /* If we have an ellipsis, then this is an expression
6696 	       expansion.  */
6697             if (allow_expansion_p
6698                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6699               {
6700                 /* Consume the `...'.  */
6701                 cp_lexer_consume_token (parser->lexer);
6702 
6703                 /* Build the argument pack.  */
6704                 expr = make_pack_expansion (expr);
6705               }
6706 
6707 	     /* Add it to the list.  We add error_mark_node
6708 		expressions to the list, so that we can still tell if
6709 		the correct form for a parenthesized expression-list
6710 		is found. That gives better errors.  */
6711 	    vec_safe_push (expression_list, expr);
6712 
6713 	    if (expr == error_mark_node)
6714 	      goto skip_comma;
6715 	  }
6716 
6717 	/* After the first item, attribute lists look the same as
6718 	   expression lists.  */
6719 	is_attribute_list = non_attr;
6720 
6721       get_comma:;
6722 	/* If the next token isn't a `,', then we are done.  */
6723 	if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6724 	  break;
6725 
6726 	/* Otherwise, consume the `,' and keep going.  */
6727 	cp_lexer_consume_token (parser->lexer);
6728       }
6729 
6730   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6731     {
6732       int ending;
6733 
6734     skip_comma:;
6735       /* We try and resync to an unnested comma, as that will give the
6736 	 user better diagnostics.  */
6737       ending = cp_parser_skip_to_closing_parenthesis (parser,
6738 						      /*recovering=*/true,
6739 						      /*or_comma=*/true,
6740 						      /*consume_paren=*/true);
6741       if (ending < 0)
6742 	goto get_comma;
6743       if (!ending)
6744 	{
6745 	  parser->greater_than_is_operator_p
6746 	    = saved_greater_than_is_operator_p;
6747 	  return NULL;
6748 	}
6749     }
6750 
6751   parser->greater_than_is_operator_p
6752     = saved_greater_than_is_operator_p;
6753 
6754   if (identifier)
6755     vec_safe_insert (expression_list, 0, identifier);
6756 
6757   return expression_list;
6758 }
6759 
6760 /* Parse a pseudo-destructor-name.
6761 
6762    pseudo-destructor-name:
6763      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6764      :: [opt] nested-name-specifier template template-id :: ~ type-name
6765      :: [opt] nested-name-specifier [opt] ~ type-name
6766 
6767    If either of the first two productions is used, sets *SCOPE to the
6768    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
6769    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
6770    or ERROR_MARK_NODE if the parse fails.  */
6771 
6772 static void
cp_parser_pseudo_destructor_name(cp_parser * parser,tree object,tree * scope,tree * type)6773 cp_parser_pseudo_destructor_name (cp_parser* parser,
6774 				  tree object,
6775 				  tree* scope,
6776 				  tree* type)
6777 {
6778   bool nested_name_specifier_p;
6779 
6780   /* Handle ~auto.  */
6781   if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6782       && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6783       && !type_dependent_expression_p (object))
6784     {
6785       if (cxx_dialect < cxx1y)
6786 	pedwarn (input_location, 0,
6787 		 "%<~auto%> only available with "
6788 		 "-std=c++1y or -std=gnu++1y");
6789       cp_lexer_consume_token (parser->lexer);
6790       cp_lexer_consume_token (parser->lexer);
6791       *scope = NULL_TREE;
6792       *type = TREE_TYPE (object);
6793       return;
6794     }
6795 
6796   /* Assume that things will not work out.  */
6797   *type = error_mark_node;
6798 
6799   /* Look for the optional `::' operator.  */
6800   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6801   /* Look for the optional nested-name-specifier.  */
6802   nested_name_specifier_p
6803     = (cp_parser_nested_name_specifier_opt (parser,
6804 					    /*typename_keyword_p=*/false,
6805 					    /*check_dependency_p=*/true,
6806 					    /*type_p=*/false,
6807 					    /*is_declaration=*/false)
6808        != NULL_TREE);
6809   /* Now, if we saw a nested-name-specifier, we might be doing the
6810      second production.  */
6811   if (nested_name_specifier_p
6812       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6813     {
6814       /* Consume the `template' keyword.  */
6815       cp_lexer_consume_token (parser->lexer);
6816       /* Parse the template-id.  */
6817       cp_parser_template_id (parser,
6818 			     /*template_keyword_p=*/true,
6819 			     /*check_dependency_p=*/false,
6820 			     class_type,
6821 			     /*is_declaration=*/true);
6822       /* Look for the `::' token.  */
6823       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6824     }
6825   /* If the next token is not a `~', then there might be some
6826      additional qualification.  */
6827   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6828     {
6829       /* At this point, we're looking for "type-name :: ~".  The type-name
6830 	 must not be a class-name, since this is a pseudo-destructor.  So,
6831 	 it must be either an enum-name, or a typedef-name -- both of which
6832 	 are just identifiers.  So, we peek ahead to check that the "::"
6833 	 and "~" tokens are present; if they are not, then we can avoid
6834 	 calling type_name.  */
6835       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6836 	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6837 	  || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6838 	{
6839 	  cp_parser_error (parser, "non-scalar type");
6840 	  return;
6841 	}
6842 
6843       /* Look for the type-name.  */
6844       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6845       if (*scope == error_mark_node)
6846 	return;
6847 
6848       /* Look for the `::' token.  */
6849       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6850     }
6851   else
6852     *scope = NULL_TREE;
6853 
6854   /* Look for the `~'.  */
6855   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6856 
6857   /* Once we see the ~, this has to be a pseudo-destructor.  */
6858   if (!processing_template_decl && !cp_parser_error_occurred (parser))
6859     cp_parser_commit_to_topmost_tentative_parse (parser);
6860 
6861   /* Look for the type-name again.  We are not responsible for
6862      checking that it matches the first type-name.  */
6863   *type = TREE_TYPE (cp_parser_nonclass_name (parser));
6864 }
6865 
6866 /* Parse a unary-expression.
6867 
6868    unary-expression:
6869      postfix-expression
6870      ++ cast-expression
6871      -- cast-expression
6872      unary-operator cast-expression
6873      sizeof unary-expression
6874      sizeof ( type-id )
6875      alignof ( type-id )  [C++0x]
6876      new-expression
6877      delete-expression
6878 
6879    GNU Extensions:
6880 
6881    unary-expression:
6882      __extension__ cast-expression
6883      __alignof__ unary-expression
6884      __alignof__ ( type-id )
6885      alignof unary-expression  [C++0x]
6886      __real__ cast-expression
6887      __imag__ cast-expression
6888      && identifier
6889      sizeof ( type-id ) { initializer-list , [opt] }
6890      alignof ( type-id ) { initializer-list , [opt] } [C++0x]
6891      __alignof__ ( type-id ) { initializer-list , [opt] }
6892 
6893    ADDRESS_P is true iff the unary-expression is appearing as the
6894    operand of the `&' operator.   CAST_P is true if this expression is
6895    the target of a cast.
6896 
6897    Returns a representation of the expression.  */
6898 
6899 static tree
cp_parser_unary_expression(cp_parser * parser,bool address_p,bool cast_p,bool decltype_p,cp_id_kind * pidk)6900 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6901 			    bool decltype_p, cp_id_kind * pidk)
6902 {
6903   cp_token *token;
6904   enum tree_code unary_operator;
6905 
6906   /* Peek at the next token.  */
6907   token = cp_lexer_peek_token (parser->lexer);
6908   /* Some keywords give away the kind of expression.  */
6909   if (token->type == CPP_KEYWORD)
6910     {
6911       enum rid keyword = token->keyword;
6912 
6913       switch (keyword)
6914 	{
6915 	case RID_ALIGNOF:
6916 	case RID_SIZEOF:
6917 	  {
6918 	    tree operand, ret;
6919 	    enum tree_code op;
6920 	    location_t first_loc;
6921 
6922 	    op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6923 	    /* Consume the token.  */
6924 	    cp_lexer_consume_token (parser->lexer);
6925 	    first_loc = cp_lexer_peek_token (parser->lexer)->location;
6926 	    /* Parse the operand.  */
6927 	    operand = cp_parser_sizeof_operand (parser, keyword);
6928 
6929 	    if (TYPE_P (operand))
6930 	      ret = cxx_sizeof_or_alignof_type (operand, op, true);
6931 	    else
6932 	      {
6933 		/* ISO C++ defines alignof only with types, not with
6934 		   expressions. So pedwarn if alignof is used with a non-
6935 		   type expression. However, __alignof__ is ok.  */
6936 		if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6937 		  pedwarn (token->location, OPT_Wpedantic,
6938 			   "ISO C++ does not allow %<alignof%> "
6939 			   "with a non-type");
6940 
6941 		ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6942 	      }
6943 	    /* For SIZEOF_EXPR, just issue diagnostics, but keep
6944 	       SIZEOF_EXPR with the original operand.  */
6945 	    if (op == SIZEOF_EXPR && ret != error_mark_node)
6946 	      {
6947 		if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6948 		  {
6949 		    if (!processing_template_decl && TYPE_P (operand))
6950 		      {
6951 			ret = build_min (SIZEOF_EXPR, size_type_node,
6952 					 build1 (NOP_EXPR, operand,
6953 						 error_mark_node));
6954 			SIZEOF_EXPR_TYPE_P (ret) = 1;
6955 		      }
6956 		    else
6957 		      ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6958 		    TREE_SIDE_EFFECTS (ret) = 0;
6959 		    TREE_READONLY (ret) = 1;
6960 		  }
6961 		SET_EXPR_LOCATION (ret, first_loc);
6962 	      }
6963 	    return ret;
6964 	  }
6965 
6966 	case RID_NEW:
6967 	  return cp_parser_new_expression (parser);
6968 
6969 	case RID_DELETE:
6970 	  return cp_parser_delete_expression (parser);
6971 
6972 	case RID_EXTENSION:
6973 	  {
6974 	    /* The saved value of the PEDANTIC flag.  */
6975 	    int saved_pedantic;
6976 	    tree expr;
6977 
6978 	    /* Save away the PEDANTIC flag.  */
6979 	    cp_parser_extension_opt (parser, &saved_pedantic);
6980 	    /* Parse the cast-expression.  */
6981 	    expr = cp_parser_simple_cast_expression (parser);
6982 	    /* Restore the PEDANTIC flag.  */
6983 	    pedantic = saved_pedantic;
6984 
6985 	    return expr;
6986 	  }
6987 
6988 	case RID_REALPART:
6989 	case RID_IMAGPART:
6990 	  {
6991 	    tree expression;
6992 
6993 	    /* Consume the `__real__' or `__imag__' token.  */
6994 	    cp_lexer_consume_token (parser->lexer);
6995 	    /* Parse the cast-expression.  */
6996 	    expression = cp_parser_simple_cast_expression (parser);
6997 	    /* Create the complete representation.  */
6998 	    return build_x_unary_op (token->location,
6999 				     (keyword == RID_REALPART
7000 				      ? REALPART_EXPR : IMAGPART_EXPR),
7001 				     expression,
7002                                      tf_warning_or_error);
7003 	  }
7004 	  break;
7005 
7006 	case RID_TRANSACTION_ATOMIC:
7007 	case RID_TRANSACTION_RELAXED:
7008 	  return cp_parser_transaction_expression (parser, keyword);
7009 
7010 	case RID_NOEXCEPT:
7011 	  {
7012 	    tree expr;
7013 	    const char *saved_message;
7014 	    bool saved_integral_constant_expression_p;
7015 	    bool saved_non_integral_constant_expression_p;
7016 	    bool saved_greater_than_is_operator_p;
7017 
7018 	    cp_lexer_consume_token (parser->lexer);
7019 	    cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7020 
7021 	    saved_message = parser->type_definition_forbidden_message;
7022 	    parser->type_definition_forbidden_message
7023 	      = G_("types may not be defined in %<noexcept%> expressions");
7024 
7025 	    saved_integral_constant_expression_p
7026 	      = parser->integral_constant_expression_p;
7027 	    saved_non_integral_constant_expression_p
7028 	      = parser->non_integral_constant_expression_p;
7029 	    parser->integral_constant_expression_p = false;
7030 
7031 	    saved_greater_than_is_operator_p
7032 	      = parser->greater_than_is_operator_p;
7033 	    parser->greater_than_is_operator_p = true;
7034 
7035 	    ++cp_unevaluated_operand;
7036 	    ++c_inhibit_evaluation_warnings;
7037 	    expr = cp_parser_expression (parser, false, NULL);
7038 	    --c_inhibit_evaluation_warnings;
7039 	    --cp_unevaluated_operand;
7040 
7041 	    parser->greater_than_is_operator_p
7042 	      = saved_greater_than_is_operator_p;
7043 
7044 	    parser->integral_constant_expression_p
7045 	      = saved_integral_constant_expression_p;
7046 	    parser->non_integral_constant_expression_p
7047 	      = saved_non_integral_constant_expression_p;
7048 
7049 	    parser->type_definition_forbidden_message = saved_message;
7050 
7051 	    cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7052 	    return finish_noexcept_expr (expr, tf_warning_or_error);
7053 	  }
7054 
7055 	default:
7056 	  break;
7057 	}
7058     }
7059 
7060   /* Look for the `:: new' and `:: delete', which also signal the
7061      beginning of a new-expression, or delete-expression,
7062      respectively.  If the next token is `::', then it might be one of
7063      these.  */
7064   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7065     {
7066       enum rid keyword;
7067 
7068       /* See if the token after the `::' is one of the keywords in
7069 	 which we're interested.  */
7070       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7071       /* If it's `new', we have a new-expression.  */
7072       if (keyword == RID_NEW)
7073 	return cp_parser_new_expression (parser);
7074       /* Similarly, for `delete'.  */
7075       else if (keyword == RID_DELETE)
7076 	return cp_parser_delete_expression (parser);
7077     }
7078 
7079   /* Look for a unary operator.  */
7080   unary_operator = cp_parser_unary_operator (token);
7081   /* The `++' and `--' operators can be handled similarly, even though
7082      they are not technically unary-operators in the grammar.  */
7083   if (unary_operator == ERROR_MARK)
7084     {
7085       if (token->type == CPP_PLUS_PLUS)
7086 	unary_operator = PREINCREMENT_EXPR;
7087       else if (token->type == CPP_MINUS_MINUS)
7088 	unary_operator = PREDECREMENT_EXPR;
7089       /* Handle the GNU address-of-label extension.  */
7090       else if (cp_parser_allow_gnu_extensions_p (parser)
7091 	       && token->type == CPP_AND_AND)
7092 	{
7093 	  tree identifier;
7094 	  tree expression;
7095 	  location_t loc = token->location;
7096 
7097 	  /* Consume the '&&' token.  */
7098 	  cp_lexer_consume_token (parser->lexer);
7099 	  /* Look for the identifier.  */
7100 	  identifier = cp_parser_identifier (parser);
7101 	  /* Create an expression representing the address.  */
7102 	  expression = finish_label_address_expr (identifier, loc);
7103 	  if (cp_parser_non_integral_constant_expression (parser,
7104 							  NIC_ADDR_LABEL))
7105 	    expression = error_mark_node;
7106 	  return expression;
7107 	}
7108     }
7109   if (unary_operator != ERROR_MARK)
7110     {
7111       tree cast_expression;
7112       tree expression = error_mark_node;
7113       non_integral_constant non_constant_p = NIC_NONE;
7114       location_t loc = token->location;
7115       tsubst_flags_t complain = complain_flags (decltype_p);
7116 
7117       /* Consume the operator token.  */
7118       token = cp_lexer_consume_token (parser->lexer);
7119       /* Parse the cast-expression.  */
7120       cast_expression
7121 	= cp_parser_cast_expression (parser,
7122 				     unary_operator == ADDR_EXPR,
7123 				     /*cast_p=*/false,
7124 				     /*decltype*/false,
7125 				     pidk);
7126       /* Now, build an appropriate representation.  */
7127       switch (unary_operator)
7128 	{
7129 	case INDIRECT_REF:
7130 	  non_constant_p = NIC_STAR;
7131 	  expression = build_x_indirect_ref (loc, cast_expression,
7132 					     RO_UNARY_STAR,
7133                                              complain);
7134 	  break;
7135 
7136 	case ADDR_EXPR:
7137 	   non_constant_p = NIC_ADDR;
7138 	  /* Fall through.  */
7139 	case BIT_NOT_EXPR:
7140 	  expression = build_x_unary_op (loc, unary_operator,
7141 					 cast_expression,
7142                                          complain);
7143 	  break;
7144 
7145 	case PREINCREMENT_EXPR:
7146 	case PREDECREMENT_EXPR:
7147 	  non_constant_p = unary_operator == PREINCREMENT_EXPR
7148 			   ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7149 	  /* Fall through.  */
7150 	case UNARY_PLUS_EXPR:
7151 	case NEGATE_EXPR:
7152 	case TRUTH_NOT_EXPR:
7153 	  expression = finish_unary_op_expr (loc, unary_operator,
7154 					     cast_expression, complain);
7155 	  break;
7156 
7157 	default:
7158 	  gcc_unreachable ();
7159 	}
7160 
7161       if (non_constant_p != NIC_NONE
7162 	  && cp_parser_non_integral_constant_expression (parser,
7163 							 non_constant_p))
7164 	expression = error_mark_node;
7165 
7166       return expression;
7167     }
7168 
7169   return cp_parser_postfix_expression (parser, address_p, cast_p,
7170                                        /*member_access_only_p=*/false,
7171 				       decltype_p,
7172 				       pidk);
7173 }
7174 
7175 static inline tree
cp_parser_unary_expression(cp_parser * parser,bool address_p,bool cast_p,cp_id_kind * pidk)7176 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7177 			    cp_id_kind * pidk)
7178 {
7179   return cp_parser_unary_expression (parser, address_p, cast_p,
7180 				     /*decltype*/false, pidk);
7181 }
7182 
7183 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
7184    unary-operator, the corresponding tree code is returned.  */
7185 
7186 static enum tree_code
cp_parser_unary_operator(cp_token * token)7187 cp_parser_unary_operator (cp_token* token)
7188 {
7189   switch (token->type)
7190     {
7191     case CPP_MULT:
7192       return INDIRECT_REF;
7193 
7194     case CPP_AND:
7195       return ADDR_EXPR;
7196 
7197     case CPP_PLUS:
7198       return UNARY_PLUS_EXPR;
7199 
7200     case CPP_MINUS:
7201       return NEGATE_EXPR;
7202 
7203     case CPP_NOT:
7204       return TRUTH_NOT_EXPR;
7205 
7206     case CPP_COMPL:
7207       return BIT_NOT_EXPR;
7208 
7209     default:
7210       return ERROR_MARK;
7211     }
7212 }
7213 
7214 /* Parse a new-expression.
7215 
7216    new-expression:
7217      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7218      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7219 
7220    Returns a representation of the expression.  */
7221 
7222 static tree
cp_parser_new_expression(cp_parser * parser)7223 cp_parser_new_expression (cp_parser* parser)
7224 {
7225   bool global_scope_p;
7226   vec<tree, va_gc> *placement;
7227   tree type;
7228   vec<tree, va_gc> *initializer;
7229   tree nelts = NULL_TREE;
7230   tree ret;
7231 
7232   /* Look for the optional `::' operator.  */
7233   global_scope_p
7234     = (cp_parser_global_scope_opt (parser,
7235 				   /*current_scope_valid_p=*/false)
7236        != NULL_TREE);
7237   /* Look for the `new' operator.  */
7238   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7239   /* There's no easy way to tell a new-placement from the
7240      `( type-id )' construct.  */
7241   cp_parser_parse_tentatively (parser);
7242   /* Look for a new-placement.  */
7243   placement = cp_parser_new_placement (parser);
7244   /* If that didn't work out, there's no new-placement.  */
7245   if (!cp_parser_parse_definitely (parser))
7246     {
7247       if (placement != NULL)
7248 	release_tree_vector (placement);
7249       placement = NULL;
7250     }
7251 
7252   /* If the next token is a `(', then we have a parenthesized
7253      type-id.  */
7254   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7255     {
7256       cp_token *token;
7257       const char *saved_message = parser->type_definition_forbidden_message;
7258 
7259       /* Consume the `('.  */
7260       cp_lexer_consume_token (parser->lexer);
7261 
7262       /* Parse the type-id.  */
7263       parser->type_definition_forbidden_message
7264 	= G_("types may not be defined in a new-expression");
7265       type = cp_parser_type_id (parser);
7266       parser->type_definition_forbidden_message = saved_message;
7267 
7268       /* Look for the closing `)'.  */
7269       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7270       token = cp_lexer_peek_token (parser->lexer);
7271       /* There should not be a direct-new-declarator in this production,
7272 	 but GCC used to allowed this, so we check and emit a sensible error
7273 	 message for this case.  */
7274       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7275 	{
7276 	  error_at (token->location,
7277 		    "array bound forbidden after parenthesized type-id");
7278 	  inform (token->location,
7279 		  "try removing the parentheses around the type-id");
7280 	  cp_parser_direct_new_declarator (parser);
7281 	}
7282     }
7283   /* Otherwise, there must be a new-type-id.  */
7284   else
7285     type = cp_parser_new_type_id (parser, &nelts);
7286 
7287   /* If the next token is a `(' or '{', then we have a new-initializer.  */
7288   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7289       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7290     initializer = cp_parser_new_initializer (parser);
7291   else
7292     initializer = NULL;
7293 
7294   /* A new-expression may not appear in an integral constant
7295      expression.  */
7296   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7297     ret = error_mark_node;
7298   else
7299     {
7300       /* Create a representation of the new-expression.  */
7301       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7302 		       tf_warning_or_error);
7303     }
7304 
7305   if (placement != NULL)
7306     release_tree_vector (placement);
7307   if (initializer != NULL)
7308     release_tree_vector (initializer);
7309 
7310   return ret;
7311 }
7312 
7313 /* Parse a new-placement.
7314 
7315    new-placement:
7316      ( expression-list )
7317 
7318    Returns the same representation as for an expression-list.  */
7319 
7320 static vec<tree, va_gc> *
cp_parser_new_placement(cp_parser * parser)7321 cp_parser_new_placement (cp_parser* parser)
7322 {
7323   vec<tree, va_gc> *expression_list;
7324 
7325   /* Parse the expression-list.  */
7326   expression_list = (cp_parser_parenthesized_expression_list
7327 		     (parser, non_attr, /*cast_p=*/false,
7328 		      /*allow_expansion_p=*/true,
7329 		      /*non_constant_p=*/NULL));
7330 
7331   return expression_list;
7332 }
7333 
7334 /* Parse a new-type-id.
7335 
7336    new-type-id:
7337      type-specifier-seq new-declarator [opt]
7338 
7339    Returns the TYPE allocated.  If the new-type-id indicates an array
7340    type, *NELTS is set to the number of elements in the last array
7341    bound; the TYPE will not include the last array bound.  */
7342 
7343 static tree
cp_parser_new_type_id(cp_parser * parser,tree * nelts)7344 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7345 {
7346   cp_decl_specifier_seq type_specifier_seq;
7347   cp_declarator *new_declarator;
7348   cp_declarator *declarator;
7349   cp_declarator *outer_declarator;
7350   const char *saved_message;
7351 
7352   /* The type-specifier sequence must not contain type definitions.
7353      (It cannot contain declarations of new types either, but if they
7354      are not definitions we will catch that because they are not
7355      complete.)  */
7356   saved_message = parser->type_definition_forbidden_message;
7357   parser->type_definition_forbidden_message
7358     = G_("types may not be defined in a new-type-id");
7359   /* Parse the type-specifier-seq.  */
7360   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7361 				/*is_trailing_return=*/false,
7362 				&type_specifier_seq);
7363   /* Restore the old message.  */
7364   parser->type_definition_forbidden_message = saved_message;
7365 
7366   if (type_specifier_seq.type == error_mark_node)
7367     return error_mark_node;
7368 
7369   /* Parse the new-declarator.  */
7370   new_declarator = cp_parser_new_declarator_opt (parser);
7371 
7372   /* Determine the number of elements in the last array dimension, if
7373      any.  */
7374   *nelts = NULL_TREE;
7375   /* Skip down to the last array dimension.  */
7376   declarator = new_declarator;
7377   outer_declarator = NULL;
7378   while (declarator && (declarator->kind == cdk_pointer
7379 			|| declarator->kind == cdk_ptrmem))
7380     {
7381       outer_declarator = declarator;
7382       declarator = declarator->declarator;
7383     }
7384   while (declarator
7385 	 && declarator->kind == cdk_array
7386 	 && declarator->declarator
7387 	 && declarator->declarator->kind == cdk_array)
7388     {
7389       outer_declarator = declarator;
7390       declarator = declarator->declarator;
7391     }
7392 
7393   if (declarator && declarator->kind == cdk_array)
7394     {
7395       *nelts = declarator->u.array.bounds;
7396       if (*nelts == error_mark_node)
7397 	*nelts = integer_one_node;
7398 
7399       if (outer_declarator)
7400 	outer_declarator->declarator = declarator->declarator;
7401       else
7402 	new_declarator = NULL;
7403     }
7404 
7405   return groktypename (&type_specifier_seq, new_declarator, false);
7406 }
7407 
7408 /* Parse an (optional) new-declarator.
7409 
7410    new-declarator:
7411      ptr-operator new-declarator [opt]
7412      direct-new-declarator
7413 
7414    Returns the declarator.  */
7415 
7416 static cp_declarator *
cp_parser_new_declarator_opt(cp_parser * parser)7417 cp_parser_new_declarator_opt (cp_parser* parser)
7418 {
7419   enum tree_code code;
7420   tree type, std_attributes = NULL_TREE;
7421   cp_cv_quals cv_quals;
7422 
7423   /* We don't know if there's a ptr-operator next, or not.  */
7424   cp_parser_parse_tentatively (parser);
7425   /* Look for a ptr-operator.  */
7426   code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7427   /* If that worked, look for more new-declarators.  */
7428   if (cp_parser_parse_definitely (parser))
7429     {
7430       cp_declarator *declarator;
7431 
7432       /* Parse another optional declarator.  */
7433       declarator = cp_parser_new_declarator_opt (parser);
7434 
7435       declarator = cp_parser_make_indirect_declarator
7436 	(code, type, cv_quals, declarator, std_attributes);
7437 
7438       return declarator;
7439     }
7440 
7441   /* If the next token is a `[', there is a direct-new-declarator.  */
7442   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7443     return cp_parser_direct_new_declarator (parser);
7444 
7445   return NULL;
7446 }
7447 
7448 /* Parse a direct-new-declarator.
7449 
7450    direct-new-declarator:
7451      [ expression ]
7452      direct-new-declarator [constant-expression]
7453 
7454    */
7455 
7456 static cp_declarator *
cp_parser_direct_new_declarator(cp_parser * parser)7457 cp_parser_direct_new_declarator (cp_parser* parser)
7458 {
7459   cp_declarator *declarator = NULL;
7460 
7461   while (true)
7462     {
7463       tree expression;
7464       cp_token *token;
7465 
7466       /* Look for the opening `['.  */
7467       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7468 
7469       token = cp_lexer_peek_token (parser->lexer);
7470       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7471       /* The standard requires that the expression have integral
7472 	 type.  DR 74 adds enumeration types.  We believe that the
7473 	 real intent is that these expressions be handled like the
7474 	 expression in a `switch' condition, which also allows
7475 	 classes with a single conversion to integral or
7476 	 enumeration type.  */
7477       if (!processing_template_decl)
7478 	{
7479 	  expression
7480 	    = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7481 					  expression,
7482 					  /*complain=*/true);
7483 	  if (!expression)
7484 	    {
7485 	      error_at (token->location,
7486 			"expression in new-declarator must have integral "
7487 			"or enumeration type");
7488 	      expression = error_mark_node;
7489 	    }
7490 	}
7491 
7492       /* Look for the closing `]'.  */
7493       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7494 
7495       /* Add this bound to the declarator.  */
7496       declarator = make_array_declarator (declarator, expression);
7497 
7498       /* If the next token is not a `[', then there are no more
7499 	 bounds.  */
7500       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7501 	break;
7502     }
7503 
7504   return declarator;
7505 }
7506 
7507 /* Parse a new-initializer.
7508 
7509    new-initializer:
7510      ( expression-list [opt] )
7511      braced-init-list
7512 
7513    Returns a representation of the expression-list.  */
7514 
7515 static vec<tree, va_gc> *
cp_parser_new_initializer(cp_parser * parser)7516 cp_parser_new_initializer (cp_parser* parser)
7517 {
7518   vec<tree, va_gc> *expression_list;
7519 
7520   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7521     {
7522       tree t;
7523       bool expr_non_constant_p;
7524       cp_lexer_set_source_position (parser->lexer);
7525       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7526       t = cp_parser_braced_list (parser, &expr_non_constant_p);
7527       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7528       expression_list = make_tree_vector_single (t);
7529     }
7530   else
7531     expression_list = (cp_parser_parenthesized_expression_list
7532 		       (parser, non_attr, /*cast_p=*/false,
7533 			/*allow_expansion_p=*/true,
7534 			/*non_constant_p=*/NULL));
7535 
7536   return expression_list;
7537 }
7538 
7539 /* Parse a delete-expression.
7540 
7541    delete-expression:
7542      :: [opt] delete cast-expression
7543      :: [opt] delete [ ] cast-expression
7544 
7545    Returns a representation of the expression.  */
7546 
7547 static tree
cp_parser_delete_expression(cp_parser * parser)7548 cp_parser_delete_expression (cp_parser* parser)
7549 {
7550   bool global_scope_p;
7551   bool array_p;
7552   tree expression;
7553 
7554   /* Look for the optional `::' operator.  */
7555   global_scope_p
7556     = (cp_parser_global_scope_opt (parser,
7557 				   /*current_scope_valid_p=*/false)
7558        != NULL_TREE);
7559   /* Look for the `delete' keyword.  */
7560   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7561   /* See if the array syntax is in use.  */
7562   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7563     {
7564       /* Consume the `[' token.  */
7565       cp_lexer_consume_token (parser->lexer);
7566       /* Look for the `]' token.  */
7567       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7568       /* Remember that this is the `[]' construct.  */
7569       array_p = true;
7570     }
7571   else
7572     array_p = false;
7573 
7574   /* Parse the cast-expression.  */
7575   expression = cp_parser_simple_cast_expression (parser);
7576 
7577   /* A delete-expression may not appear in an integral constant
7578      expression.  */
7579   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7580     return error_mark_node;
7581 
7582   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7583 			tf_warning_or_error);
7584 }
7585 
7586 /* Returns true if TOKEN may start a cast-expression and false
7587    otherwise.  */
7588 
7589 static bool
cp_parser_tokens_start_cast_expression(cp_parser * parser)7590 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7591 {
7592   cp_token *token = cp_lexer_peek_token (parser->lexer);
7593   switch (token->type)
7594     {
7595     case CPP_COMMA:
7596     case CPP_SEMICOLON:
7597     case CPP_QUERY:
7598     case CPP_COLON:
7599     case CPP_CLOSE_SQUARE:
7600     case CPP_CLOSE_PAREN:
7601     case CPP_CLOSE_BRACE:
7602     case CPP_OPEN_BRACE:
7603     case CPP_DOT:
7604     case CPP_DOT_STAR:
7605     case CPP_DEREF:
7606     case CPP_DEREF_STAR:
7607     case CPP_DIV:
7608     case CPP_MOD:
7609     case CPP_LSHIFT:
7610     case CPP_RSHIFT:
7611     case CPP_LESS:
7612     case CPP_GREATER:
7613     case CPP_LESS_EQ:
7614     case CPP_GREATER_EQ:
7615     case CPP_EQ_EQ:
7616     case CPP_NOT_EQ:
7617     case CPP_EQ:
7618     case CPP_MULT_EQ:
7619     case CPP_DIV_EQ:
7620     case CPP_MOD_EQ:
7621     case CPP_PLUS_EQ:
7622     case CPP_MINUS_EQ:
7623     case CPP_RSHIFT_EQ:
7624     case CPP_LSHIFT_EQ:
7625     case CPP_AND_EQ:
7626     case CPP_XOR_EQ:
7627     case CPP_OR_EQ:
7628     case CPP_XOR:
7629     case CPP_OR:
7630     case CPP_OR_OR:
7631     case CPP_EOF:
7632       return false;
7633 
7634     case CPP_OPEN_PAREN:
7635       /* In ((type ()) () the last () isn't a valid cast-expression,
7636 	 so the whole must be parsed as postfix-expression.  */
7637       return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7638 	     != CPP_CLOSE_PAREN;
7639 
7640       /* '[' may start a primary-expression in obj-c++.  */
7641     case CPP_OPEN_SQUARE:
7642       return c_dialect_objc ();
7643 
7644     default:
7645       return true;
7646     }
7647 }
7648 
7649 /* Parse a cast-expression.
7650 
7651    cast-expression:
7652      unary-expression
7653      ( type-id ) cast-expression
7654 
7655    ADDRESS_P is true iff the unary-expression is appearing as the
7656    operand of the `&' operator.   CAST_P is true if this expression is
7657    the target of a cast.
7658 
7659    Returns a representation of the expression.  */
7660 
7661 static tree
cp_parser_cast_expression(cp_parser * parser,bool address_p,bool cast_p,bool decltype_p,cp_id_kind * pidk)7662 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7663 			   bool decltype_p, cp_id_kind * pidk)
7664 {
7665   /* If it's a `(', then we might be looking at a cast.  */
7666   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7667     {
7668       tree type = NULL_TREE;
7669       tree expr = NULL_TREE;
7670       bool cast_expression_p;
7671       const char *saved_message;
7672 
7673       /* There's no way to know yet whether or not this is a cast.
7674 	 For example, `(int (3))' is a unary-expression, while `(int)
7675 	 3' is a cast.  So, we resort to parsing tentatively.  */
7676       cp_parser_parse_tentatively (parser);
7677       /* Types may not be defined in a cast.  */
7678       saved_message = parser->type_definition_forbidden_message;
7679       parser->type_definition_forbidden_message
7680 	= G_("types may not be defined in casts");
7681       /* Consume the `('.  */
7682       cp_lexer_consume_token (parser->lexer);
7683       /* A very tricky bit is that `(struct S) { 3 }' is a
7684 	 compound-literal (which we permit in C++ as an extension).
7685 	 But, that construct is not a cast-expression -- it is a
7686 	 postfix-expression.  (The reason is that `(struct S) { 3 }.i'
7687 	 is legal; if the compound-literal were a cast-expression,
7688 	 you'd need an extra set of parentheses.)  But, if we parse
7689 	 the type-id, and it happens to be a class-specifier, then we
7690 	 will commit to the parse at that point, because we cannot
7691 	 undo the action that is done when creating a new class.  So,
7692 	 then we cannot back up and do a postfix-expression.
7693 	 Another tricky case is the following (c++/29234):
7694 
7695          struct S { void operator () (); };
7696 
7697          void foo ()
7698          {
7699            ( S()() );
7700          }
7701 
7702 	 As a type-id we parse the parenthesized S()() as a function
7703 	 returning a function, groktypename complains and we cannot
7704 	 back up in this case either.
7705 
7706 	 Therefore, we scan ahead to the closing `)', and check to see
7707 	 if the tokens after the `)' can start a cast-expression.  Otherwise
7708 	 we are dealing with an unary-expression, a postfix-expression
7709 	 or something else.
7710 
7711 	 Save tokens so that we can put them back.  */
7712       cp_lexer_save_tokens (parser->lexer);
7713 
7714       /* We may be looking at a cast-expression.  */
7715       cast_expression_p
7716 	= (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7717 						  /*consume_paren=*/true)
7718 	   && cp_parser_tokens_start_cast_expression (parser));
7719 
7720       /* Roll back the tokens we skipped.  */
7721       cp_lexer_rollback_tokens (parser->lexer);
7722       /* If we aren't looking at a cast-expression, simulate an error so
7723 	 that the call to cp_parser_parse_definitely below will fail.  */
7724       if (!cast_expression_p)
7725 	cp_parser_simulate_error (parser);
7726       else
7727 	{
7728 	  bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7729 	  parser->in_type_id_in_expr_p = true;
7730 	  /* Look for the type-id.  */
7731 	  type = cp_parser_type_id (parser);
7732 	  /* Look for the closing `)'.  */
7733 	  cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7734 	  parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7735 	}
7736 
7737       /* Restore the saved message.  */
7738       parser->type_definition_forbidden_message = saved_message;
7739 
7740       /* At this point this can only be either a cast or a
7741 	 parenthesized ctor such as `(T ())' that looks like a cast to
7742 	 function returning T.  */
7743       if (!cp_parser_error_occurred (parser))
7744 	{
7745 	  cp_parser_parse_definitely (parser);
7746 	  expr = cp_parser_cast_expression (parser,
7747 					    /*address_p=*/false,
7748 					    /*cast_p=*/true,
7749 					    /*decltype_p=*/false,
7750 					    pidk);
7751 
7752 	  /* Warn about old-style casts, if so requested.  */
7753 	  if (warn_old_style_cast
7754 	      && !in_system_header_at (input_location)
7755 	      && !VOID_TYPE_P (type)
7756 	      && current_lang_name != lang_name_c)
7757 	    warning (OPT_Wold_style_cast, "use of old-style cast");
7758 
7759 	  /* Only type conversions to integral or enumeration types
7760 	     can be used in constant-expressions.  */
7761 	  if (!cast_valid_in_integral_constant_expression_p (type)
7762 	      && cp_parser_non_integral_constant_expression (parser,
7763 							     NIC_CAST))
7764 	    return error_mark_node;
7765 
7766 	  /* Perform the cast.  */
7767 	  expr = build_c_cast (input_location, type, expr);
7768 	  return expr;
7769 	}
7770       else
7771         cp_parser_abort_tentative_parse (parser);
7772     }
7773 
7774   /* If we get here, then it's not a cast, so it must be a
7775      unary-expression.  */
7776   return cp_parser_unary_expression (parser, address_p, cast_p,
7777 				     decltype_p, pidk);
7778 }
7779 
7780 /* Parse a binary expression of the general form:
7781 
7782    pm-expression:
7783      cast-expression
7784      pm-expression .* cast-expression
7785      pm-expression ->* cast-expression
7786 
7787    multiplicative-expression:
7788      pm-expression
7789      multiplicative-expression * pm-expression
7790      multiplicative-expression / pm-expression
7791      multiplicative-expression % pm-expression
7792 
7793    additive-expression:
7794      multiplicative-expression
7795      additive-expression + multiplicative-expression
7796      additive-expression - multiplicative-expression
7797 
7798    shift-expression:
7799      additive-expression
7800      shift-expression << additive-expression
7801      shift-expression >> additive-expression
7802 
7803    relational-expression:
7804      shift-expression
7805      relational-expression < shift-expression
7806      relational-expression > shift-expression
7807      relational-expression <= shift-expression
7808      relational-expression >= shift-expression
7809 
7810   GNU Extension:
7811 
7812    relational-expression:
7813      relational-expression <? shift-expression
7814      relational-expression >? shift-expression
7815 
7816    equality-expression:
7817      relational-expression
7818      equality-expression == relational-expression
7819      equality-expression != relational-expression
7820 
7821    and-expression:
7822      equality-expression
7823      and-expression & equality-expression
7824 
7825    exclusive-or-expression:
7826      and-expression
7827      exclusive-or-expression ^ and-expression
7828 
7829    inclusive-or-expression:
7830      exclusive-or-expression
7831      inclusive-or-expression | exclusive-or-expression
7832 
7833    logical-and-expression:
7834      inclusive-or-expression
7835      logical-and-expression && inclusive-or-expression
7836 
7837    logical-or-expression:
7838      logical-and-expression
7839      logical-or-expression || logical-and-expression
7840 
7841    All these are implemented with a single function like:
7842 
7843    binary-expression:
7844      simple-cast-expression
7845      binary-expression <token> binary-expression
7846 
7847    CAST_P is true if this expression is the target of a cast.
7848 
7849    The binops_by_token map is used to get the tree codes for each <token> type.
7850    binary-expressions are associated according to a precedence table.  */
7851 
7852 #define TOKEN_PRECEDENCE(token)				     \
7853 (((token->type == CPP_GREATER				     \
7854    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7855   && !parser->greater_than_is_operator_p)		     \
7856  ? PREC_NOT_OPERATOR					     \
7857  : binops_by_token[token->type].prec)
7858 
7859 static tree
cp_parser_binary_expression(cp_parser * parser,bool cast_p,bool no_toplevel_fold_p,bool decltype_p,enum cp_parser_prec prec,cp_id_kind * pidk)7860 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7861 			     bool no_toplevel_fold_p,
7862 			     bool decltype_p,
7863 			     enum cp_parser_prec prec,
7864 			     cp_id_kind * pidk)
7865 {
7866   cp_parser_expression_stack stack;
7867   cp_parser_expression_stack_entry *sp = &stack[0];
7868   cp_parser_expression_stack_entry current;
7869   tree rhs;
7870   cp_token *token;
7871   enum tree_code rhs_type;
7872   enum cp_parser_prec new_prec, lookahead_prec;
7873   tree overload;
7874 
7875   /* Parse the first expression.  */
7876   current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7877 					   cast_p, decltype_p, pidk);
7878   current.lhs_type = ERROR_MARK;
7879   current.prec = prec;
7880 
7881   if (cp_parser_error_occurred (parser))
7882     return error_mark_node;
7883 
7884   for (;;)
7885     {
7886       /* Get an operator token.  */
7887       token = cp_lexer_peek_token (parser->lexer);
7888 
7889       if (warn_cxx0x_compat
7890           && token->type == CPP_RSHIFT
7891           && !parser->greater_than_is_operator_p)
7892         {
7893           if (warning_at (token->location, OPT_Wc__0x_compat,
7894 			  "%<>>%> operator is treated"
7895 			  " as two right angle brackets in C++11"))
7896 	    inform (token->location,
7897 		    "suggest parentheses around %<>>%> expression");
7898         }
7899 
7900       new_prec = TOKEN_PRECEDENCE (token);
7901 
7902       /* Popping an entry off the stack means we completed a subexpression:
7903 	 - either we found a token which is not an operator (`>' where it is not
7904 	   an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7905 	   will happen repeatedly;
7906 	 - or, we found an operator which has lower priority.  This is the case
7907 	   where the recursive descent *ascends*, as in `3 * 4 + 5' after
7908 	   parsing `3 * 4'.  */
7909       if (new_prec <= current.prec)
7910 	{
7911 	  if (sp == stack)
7912 	    break;
7913 	  else
7914 	    goto pop;
7915 	}
7916 
7917      get_rhs:
7918       current.tree_type = binops_by_token[token->type].tree_type;
7919       current.loc = token->location;
7920 
7921       /* We used the operator token.  */
7922       cp_lexer_consume_token (parser->lexer);
7923 
7924       /* For "false && x" or "true || x", x will never be executed;
7925 	 disable warnings while evaluating it.  */
7926       if (current.tree_type == TRUTH_ANDIF_EXPR)
7927 	c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7928       else if (current.tree_type == TRUTH_ORIF_EXPR)
7929 	c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7930 
7931       /* Extract another operand.  It may be the RHS of this expression
7932 	 or the LHS of a new, higher priority expression.  */
7933       rhs = cp_parser_simple_cast_expression (parser);
7934       rhs_type = ERROR_MARK;
7935 
7936       /* Get another operator token.  Look up its precedence to avoid
7937 	 building a useless (immediately popped) stack entry for common
7938 	 cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
7939       token = cp_lexer_peek_token (parser->lexer);
7940       lookahead_prec = TOKEN_PRECEDENCE (token);
7941       if (lookahead_prec > new_prec)
7942 	{
7943 	  /* ... and prepare to parse the RHS of the new, higher priority
7944 	     expression.  Since precedence levels on the stack are
7945 	     monotonically increasing, we do not have to care about
7946 	     stack overflows.  */
7947 	  *sp = current;
7948 	  ++sp;
7949 	  current.lhs = rhs;
7950 	  current.lhs_type = rhs_type;
7951 	  current.prec = new_prec;
7952 	  new_prec = lookahead_prec;
7953 	  goto get_rhs;
7954 
7955 	 pop:
7956 	  lookahead_prec = new_prec;
7957 	  /* If the stack is not empty, we have parsed into LHS the right side
7958 	     (`4' in the example above) of an expression we had suspended.
7959 	     We can use the information on the stack to recover the LHS (`3')
7960 	     from the stack together with the tree code (`MULT_EXPR'), and
7961 	     the precedence of the higher level subexpression
7962 	     (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
7963 	     which will be used to actually build the additive expression.  */
7964 	  rhs = current.lhs;
7965 	  rhs_type = current.lhs_type;
7966 	  --sp;
7967 	  current = *sp;
7968 	}
7969 
7970       /* Undo the disabling of warnings done above.  */
7971       if (current.tree_type == TRUTH_ANDIF_EXPR)
7972 	c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7973       else if (current.tree_type == TRUTH_ORIF_EXPR)
7974 	c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7975 
7976       overload = NULL;
7977       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7978 	 ERROR_MARK for everything that is not a binary expression.
7979 	 This makes warn_about_parentheses miss some warnings that
7980 	 involve unary operators.  For unary expressions we should
7981 	 pass the correct tree_code unless the unary expression was
7982 	 surrounded by parentheses.
7983       */
7984       if (no_toplevel_fold_p
7985 	  && lookahead_prec <= current.prec
7986 	  && sp == stack)
7987 	current.lhs = build2 (current.tree_type,
7988 			      TREE_CODE_CLASS (current.tree_type)
7989 			      == tcc_comparison
7990 			      ? boolean_type_node : TREE_TYPE (current.lhs),
7991 			      current.lhs, rhs);
7992       else
7993 	current.lhs = build_x_binary_op (current.loc, current.tree_type,
7994 					 current.lhs, current.lhs_type,
7995 					 rhs, rhs_type, &overload,
7996 					 complain_flags (decltype_p));
7997       current.lhs_type = current.tree_type;
7998       if (EXPR_P (current.lhs))
7999 	SET_EXPR_LOCATION (current.lhs, current.loc);
8000 
8001       /* If the binary operator required the use of an overloaded operator,
8002 	 then this expression cannot be an integral constant-expression.
8003 	 An overloaded operator can be used even if both operands are
8004 	 otherwise permissible in an integral constant-expression if at
8005 	 least one of the operands is of enumeration type.  */
8006 
8007       if (overload
8008 	  && cp_parser_non_integral_constant_expression (parser,
8009 							 NIC_OVERLOADED))
8010 	return error_mark_node;
8011     }
8012 
8013   return current.lhs;
8014 }
8015 
8016 static tree
cp_parser_binary_expression(cp_parser * parser,bool cast_p,bool no_toplevel_fold_p,enum cp_parser_prec prec,cp_id_kind * pidk)8017 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8018 			     bool no_toplevel_fold_p,
8019 			     enum cp_parser_prec prec,
8020 			     cp_id_kind * pidk)
8021 {
8022   return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8023 				      /*decltype*/false, prec, pidk);
8024 }
8025 
8026 /* Parse the `? expression : assignment-expression' part of a
8027    conditional-expression.  The LOGICAL_OR_EXPR is the
8028    logical-or-expression that started the conditional-expression.
8029    Returns a representation of the entire conditional-expression.
8030 
8031    This routine is used by cp_parser_assignment_expression.
8032 
8033      ? expression : assignment-expression
8034 
8035    GNU Extensions:
8036 
8037      ? : assignment-expression */
8038 
8039 static tree
cp_parser_question_colon_clause(cp_parser * parser,tree logical_or_expr)8040 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8041 {
8042   tree expr;
8043   tree assignment_expr;
8044   struct cp_token *token;
8045   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8046 
8047   /* Consume the `?' token.  */
8048   cp_lexer_consume_token (parser->lexer);
8049   token = cp_lexer_peek_token (parser->lexer);
8050   if (cp_parser_allow_gnu_extensions_p (parser)
8051       && token->type == CPP_COLON)
8052     {
8053       pedwarn (token->location, OPT_Wpedantic,
8054                "ISO C++ does not allow ?: with omitted middle operand");
8055       /* Implicit true clause.  */
8056       expr = NULL_TREE;
8057       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8058       warn_for_omitted_condop (token->location, logical_or_expr);
8059     }
8060   else
8061     {
8062       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8063       parser->colon_corrects_to_scope_p = false;
8064       /* Parse the expression.  */
8065       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8066       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8067       c_inhibit_evaluation_warnings +=
8068 	((logical_or_expr == truthvalue_true_node)
8069 	 - (logical_or_expr == truthvalue_false_node));
8070       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8071     }
8072 
8073   /* The next token should be a `:'.  */
8074   cp_parser_require (parser, CPP_COLON, RT_COLON);
8075   /* Parse the assignment-expression.  */
8076   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8077   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8078 
8079   /* Build the conditional-expression.  */
8080   return build_x_conditional_expr (loc, logical_or_expr,
8081 				   expr,
8082 				   assignment_expr,
8083                                    tf_warning_or_error);
8084 }
8085 
8086 /* Parse an assignment-expression.
8087 
8088    assignment-expression:
8089      conditional-expression
8090      logical-or-expression assignment-operator assignment_expression
8091      throw-expression
8092 
8093    CAST_P is true if this expression is the target of a cast.
8094    DECLTYPE_P is true if this expression is the operand of decltype.
8095 
8096    Returns a representation for the expression.  */
8097 
8098 static tree
cp_parser_assignment_expression(cp_parser * parser,bool cast_p,bool decltype_p,cp_id_kind * pidk)8099 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8100 				 bool decltype_p, cp_id_kind * pidk)
8101 {
8102   tree expr;
8103 
8104   /* If the next token is the `throw' keyword, then we're looking at
8105      a throw-expression.  */
8106   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8107     expr = cp_parser_throw_expression (parser);
8108   /* Otherwise, it must be that we are looking at a
8109      logical-or-expression.  */
8110   else
8111     {
8112       /* Parse the binary expressions (logical-or-expression).  */
8113       expr = cp_parser_binary_expression (parser, cast_p, false,
8114 					  decltype_p,
8115 					  PREC_NOT_OPERATOR, pidk);
8116       /* If the next token is a `?' then we're actually looking at a
8117 	 conditional-expression.  */
8118       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8119 	return cp_parser_question_colon_clause (parser, expr);
8120       else
8121 	{
8122 	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8123 
8124 	  /* If it's an assignment-operator, we're using the second
8125 	     production.  */
8126 	  enum tree_code assignment_operator
8127 	    = cp_parser_assignment_operator_opt (parser);
8128 	  if (assignment_operator != ERROR_MARK)
8129 	    {
8130 	      bool non_constant_p;
8131 	      location_t saved_input_location;
8132 
8133 	      /* Parse the right-hand side of the assignment.  */
8134 	      tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8135 
8136 	      if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8137 		maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8138 
8139 	      /* An assignment may not appear in a
8140 		 constant-expression.  */
8141 	      if (cp_parser_non_integral_constant_expression (parser,
8142 							      NIC_ASSIGNMENT))
8143 		return error_mark_node;
8144 	      /* Build the assignment expression.  Its default
8145 		 location is the location of the '=' token.  */
8146 	      saved_input_location = input_location;
8147 	      input_location = loc;
8148 	      expr = build_x_modify_expr (loc, expr,
8149 					  assignment_operator,
8150 					  rhs,
8151 					  complain_flags (decltype_p));
8152 	      input_location = saved_input_location;
8153 	    }
8154 	}
8155     }
8156 
8157   return expr;
8158 }
8159 
8160 static tree
cp_parser_assignment_expression(cp_parser * parser,bool cast_p,cp_id_kind * pidk)8161 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8162 				 cp_id_kind * pidk)
8163 {
8164   return cp_parser_assignment_expression (parser, cast_p,
8165 					  /*decltype*/false, pidk);
8166 }
8167 
8168 /* Parse an (optional) assignment-operator.
8169 
8170    assignment-operator: one of
8171      = *= /= %= += -= >>= <<= &= ^= |=
8172 
8173    GNU Extension:
8174 
8175    assignment-operator: one of
8176      <?= >?=
8177 
8178    If the next token is an assignment operator, the corresponding tree
8179    code is returned, and the token is consumed.  For example, for
8180    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
8181    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
8182    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
8183    operator, ERROR_MARK is returned.  */
8184 
8185 static enum tree_code
cp_parser_assignment_operator_opt(cp_parser * parser)8186 cp_parser_assignment_operator_opt (cp_parser* parser)
8187 {
8188   enum tree_code op;
8189   cp_token *token;
8190 
8191   /* Peek at the next token.  */
8192   token = cp_lexer_peek_token (parser->lexer);
8193 
8194   switch (token->type)
8195     {
8196     case CPP_EQ:
8197       op = NOP_EXPR;
8198       break;
8199 
8200     case CPP_MULT_EQ:
8201       op = MULT_EXPR;
8202       break;
8203 
8204     case CPP_DIV_EQ:
8205       op = TRUNC_DIV_EXPR;
8206       break;
8207 
8208     case CPP_MOD_EQ:
8209       op = TRUNC_MOD_EXPR;
8210       break;
8211 
8212     case CPP_PLUS_EQ:
8213       op = PLUS_EXPR;
8214       break;
8215 
8216     case CPP_MINUS_EQ:
8217       op = MINUS_EXPR;
8218       break;
8219 
8220     case CPP_RSHIFT_EQ:
8221       op = RSHIFT_EXPR;
8222       break;
8223 
8224     case CPP_LSHIFT_EQ:
8225       op = LSHIFT_EXPR;
8226       break;
8227 
8228     case CPP_AND_EQ:
8229       op = BIT_AND_EXPR;
8230       break;
8231 
8232     case CPP_XOR_EQ:
8233       op = BIT_XOR_EXPR;
8234       break;
8235 
8236     case CPP_OR_EQ:
8237       op = BIT_IOR_EXPR;
8238       break;
8239 
8240     default:
8241       /* Nothing else is an assignment operator.  */
8242       op = ERROR_MARK;
8243     }
8244 
8245   /* If it was an assignment operator, consume it.  */
8246   if (op != ERROR_MARK)
8247     cp_lexer_consume_token (parser->lexer);
8248 
8249   return op;
8250 }
8251 
8252 /* Parse an expression.
8253 
8254    expression:
8255      assignment-expression
8256      expression , assignment-expression
8257 
8258    CAST_P is true if this expression is the target of a cast.
8259    DECLTYPE_P is true if this expression is the immediate operand of decltype,
8260      except possibly parenthesized or on the RHS of a comma (N3276).
8261 
8262    Returns a representation of the expression.  */
8263 
8264 static tree
cp_parser_expression(cp_parser * parser,bool cast_p,bool decltype_p,cp_id_kind * pidk)8265 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
8266 		      cp_id_kind * pidk)
8267 {
8268   tree expression = NULL_TREE;
8269   location_t loc = UNKNOWN_LOCATION;
8270 
8271   while (true)
8272     {
8273       tree assignment_expression;
8274 
8275       /* Parse the next assignment-expression.  */
8276       assignment_expression
8277 	= cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
8278 
8279       /* We don't create a temporary for a call that is the immediate operand
8280 	 of decltype or on the RHS of a comma.  But when we see a comma, we
8281 	 need to create a temporary for a call on the LHS.  */
8282       if (decltype_p && !processing_template_decl
8283 	  && TREE_CODE (assignment_expression) == CALL_EXPR
8284 	  && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8285 	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8286 	assignment_expression
8287 	  = build_cplus_new (TREE_TYPE (assignment_expression),
8288 			     assignment_expression, tf_warning_or_error);
8289 
8290       /* If this is the first assignment-expression, we can just
8291 	 save it away.  */
8292       if (!expression)
8293 	expression = assignment_expression;
8294       else
8295 	expression = build_x_compound_expr (loc, expression,
8296 					    assignment_expression,
8297 					    complain_flags (decltype_p));
8298       /* If the next token is not a comma, then we are done with the
8299 	 expression.  */
8300       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8301 	break;
8302       /* Consume the `,'.  */
8303       loc = cp_lexer_peek_token (parser->lexer)->location;
8304       cp_lexer_consume_token (parser->lexer);
8305       /* A comma operator cannot appear in a constant-expression.  */
8306       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8307 	expression = error_mark_node;
8308     }
8309 
8310   return expression;
8311 }
8312 
8313 static inline tree
cp_parser_expression(cp_parser * parser,bool cast_p,cp_id_kind * pidk)8314 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
8315 {
8316   return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
8317 }
8318 
8319 /* Parse a constant-expression.
8320 
8321    constant-expression:
8322      conditional-expression
8323 
8324   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8325   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
8326   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
8327   is false, NON_CONSTANT_P should be NULL.  */
8328 
8329 static tree
cp_parser_constant_expression(cp_parser * parser,bool allow_non_constant_p,bool * non_constant_p)8330 cp_parser_constant_expression (cp_parser* parser,
8331 			       bool allow_non_constant_p,
8332 			       bool *non_constant_p)
8333 {
8334   bool saved_integral_constant_expression_p;
8335   bool saved_allow_non_integral_constant_expression_p;
8336   bool saved_non_integral_constant_expression_p;
8337   tree expression;
8338 
8339   /* It might seem that we could simply parse the
8340      conditional-expression, and then check to see if it were
8341      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
8342      one that the compiler can figure out is constant, possibly after
8343      doing some simplifications or optimizations.  The standard has a
8344      precise definition of constant-expression, and we must honor
8345      that, even though it is somewhat more restrictive.
8346 
8347      For example:
8348 
8349        int i[(2, 3)];
8350 
8351      is not a legal declaration, because `(2, 3)' is not a
8352      constant-expression.  The `,' operator is forbidden in a
8353      constant-expression.  However, GCC's constant-folding machinery
8354      will fold this operation to an INTEGER_CST for `3'.  */
8355 
8356   /* Save the old settings.  */
8357   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8358   saved_allow_non_integral_constant_expression_p
8359     = parser->allow_non_integral_constant_expression_p;
8360   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8361   /* We are now parsing a constant-expression.  */
8362   parser->integral_constant_expression_p = true;
8363   parser->allow_non_integral_constant_expression_p
8364     = (allow_non_constant_p || cxx_dialect >= cxx11);
8365   parser->non_integral_constant_expression_p = false;
8366   /* Although the grammar says "conditional-expression", we parse an
8367      "assignment-expression", which also permits "throw-expression"
8368      and the use of assignment operators.  In the case that
8369      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8370      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
8371      actually essential that we look for an assignment-expression.
8372      For example, cp_parser_initializer_clauses uses this function to
8373      determine whether a particular assignment-expression is in fact
8374      constant.  */
8375   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8376   /* Restore the old settings.  */
8377   parser->integral_constant_expression_p
8378     = saved_integral_constant_expression_p;
8379   parser->allow_non_integral_constant_expression_p
8380     = saved_allow_non_integral_constant_expression_p;
8381   if (cxx_dialect >= cxx11)
8382     {
8383       /* Require an rvalue constant expression here; that's what our
8384 	 callers expect.  Reference constant expressions are handled
8385 	 separately in e.g. cp_parser_template_argument.  */
8386       bool is_const = potential_rvalue_constant_expression (expression);
8387       parser->non_integral_constant_expression_p = !is_const;
8388       if (!is_const && !allow_non_constant_p)
8389 	require_potential_rvalue_constant_expression (expression);
8390     }
8391   if (allow_non_constant_p)
8392     *non_constant_p = parser->non_integral_constant_expression_p;
8393   parser->non_integral_constant_expression_p
8394     = saved_non_integral_constant_expression_p;
8395 
8396   return expression;
8397 }
8398 
8399 /* Parse __builtin_offsetof.
8400 
8401    offsetof-expression:
8402      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8403 
8404    offsetof-member-designator:
8405      id-expression
8406      | offsetof-member-designator "." id-expression
8407      | offsetof-member-designator "[" expression "]"
8408      | offsetof-member-designator "->" id-expression  */
8409 
8410 static tree
cp_parser_builtin_offsetof(cp_parser * parser)8411 cp_parser_builtin_offsetof (cp_parser *parser)
8412 {
8413   int save_ice_p, save_non_ice_p;
8414   tree type, expr;
8415   cp_id_kind dummy;
8416   cp_token *token;
8417 
8418   /* We're about to accept non-integral-constant things, but will
8419      definitely yield an integral constant expression.  Save and
8420      restore these values around our local parsing.  */
8421   save_ice_p = parser->integral_constant_expression_p;
8422   save_non_ice_p = parser->non_integral_constant_expression_p;
8423 
8424   /* Consume the "__builtin_offsetof" token.  */
8425   cp_lexer_consume_token (parser->lexer);
8426   /* Consume the opening `('.  */
8427   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8428   /* Parse the type-id.  */
8429   type = cp_parser_type_id (parser);
8430   /* Look for the `,'.  */
8431   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8432   token = cp_lexer_peek_token (parser->lexer);
8433 
8434   /* Build the (type *)null that begins the traditional offsetof macro.  */
8435   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8436                             tf_warning_or_error);
8437 
8438   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
8439   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8440 						 true, &dummy, token->location);
8441   while (true)
8442     {
8443       token = cp_lexer_peek_token (parser->lexer);
8444       switch (token->type)
8445 	{
8446 	case CPP_OPEN_SQUARE:
8447 	  /* offsetof-member-designator "[" expression "]" */
8448 	  expr = cp_parser_postfix_open_square_expression (parser, expr,
8449 							   true, false);
8450 	  break;
8451 
8452 	case CPP_DEREF:
8453 	  /* offsetof-member-designator "->" identifier */
8454 	  expr = grok_array_decl (token->location, expr,
8455 				  integer_zero_node, false);
8456 	  /* FALLTHRU */
8457 
8458 	case CPP_DOT:
8459 	  /* offsetof-member-designator "." identifier */
8460 	  cp_lexer_consume_token (parser->lexer);
8461 	  expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8462 							 expr, true, &dummy,
8463 							 token->location);
8464 	  break;
8465 
8466 	case CPP_CLOSE_PAREN:
8467 	  /* Consume the ")" token.  */
8468 	  cp_lexer_consume_token (parser->lexer);
8469 	  goto success;
8470 
8471 	default:
8472 	  /* Error.  We know the following require will fail, but
8473 	     that gives the proper error message.  */
8474 	  cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8475 	  cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8476 	  expr = error_mark_node;
8477 	  goto failure;
8478 	}
8479     }
8480 
8481  success:
8482   /* If we're processing a template, we can't finish the semantics yet.
8483      Otherwise we can fold the entire expression now.  */
8484   if (processing_template_decl)
8485     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8486   else
8487     expr = finish_offsetof (expr);
8488 
8489  failure:
8490   parser->integral_constant_expression_p = save_ice_p;
8491   parser->non_integral_constant_expression_p = save_non_ice_p;
8492 
8493   return expr;
8494 }
8495 
8496 /* Parse a trait expression.
8497 
8498    Returns a representation of the expression, the underlying type
8499    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
8500 
8501 static tree
cp_parser_trait_expr(cp_parser * parser,enum rid keyword)8502 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8503 {
8504   cp_trait_kind kind;
8505   tree type1, type2 = NULL_TREE;
8506   bool binary = false;
8507   cp_decl_specifier_seq decl_specs;
8508 
8509   switch (keyword)
8510     {
8511     case RID_HAS_NOTHROW_ASSIGN:
8512       kind = CPTK_HAS_NOTHROW_ASSIGN;
8513       break;
8514     case RID_HAS_NOTHROW_CONSTRUCTOR:
8515       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8516       break;
8517     case RID_HAS_NOTHROW_COPY:
8518       kind = CPTK_HAS_NOTHROW_COPY;
8519       break;
8520     case RID_HAS_TRIVIAL_ASSIGN:
8521       kind = CPTK_HAS_TRIVIAL_ASSIGN;
8522       break;
8523     case RID_HAS_TRIVIAL_CONSTRUCTOR:
8524       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8525       break;
8526     case RID_HAS_TRIVIAL_COPY:
8527       kind = CPTK_HAS_TRIVIAL_COPY;
8528       break;
8529     case RID_HAS_TRIVIAL_DESTRUCTOR:
8530       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8531       break;
8532     case RID_HAS_VIRTUAL_DESTRUCTOR:
8533       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8534       break;
8535     case RID_IS_ABSTRACT:
8536       kind = CPTK_IS_ABSTRACT;
8537       break;
8538     case RID_IS_BASE_OF:
8539       kind = CPTK_IS_BASE_OF;
8540       binary = true;
8541       break;
8542     case RID_IS_CLASS:
8543       kind = CPTK_IS_CLASS;
8544       break;
8545     case RID_IS_CONVERTIBLE_TO:
8546       kind = CPTK_IS_CONVERTIBLE_TO;
8547       binary = true;
8548       break;
8549     case RID_IS_EMPTY:
8550       kind = CPTK_IS_EMPTY;
8551       break;
8552     case RID_IS_ENUM:
8553       kind = CPTK_IS_ENUM;
8554       break;
8555     case RID_IS_FINAL:
8556       kind = CPTK_IS_FINAL;
8557       break;
8558     case RID_IS_LITERAL_TYPE:
8559       kind = CPTK_IS_LITERAL_TYPE;
8560       break;
8561     case RID_IS_POD:
8562       kind = CPTK_IS_POD;
8563       break;
8564     case RID_IS_POLYMORPHIC:
8565       kind = CPTK_IS_POLYMORPHIC;
8566       break;
8567     case RID_IS_STD_LAYOUT:
8568       kind = CPTK_IS_STD_LAYOUT;
8569       break;
8570     case RID_IS_TRIVIAL:
8571       kind = CPTK_IS_TRIVIAL;
8572       break;
8573     case RID_IS_UNION:
8574       kind = CPTK_IS_UNION;
8575       break;
8576     case RID_UNDERLYING_TYPE:
8577       kind = CPTK_UNDERLYING_TYPE;
8578       break;
8579     case RID_BASES:
8580       kind = CPTK_BASES;
8581       break;
8582     case RID_DIRECT_BASES:
8583       kind = CPTK_DIRECT_BASES;
8584       break;
8585     default:
8586       gcc_unreachable ();
8587     }
8588 
8589   /* Consume the token.  */
8590   cp_lexer_consume_token (parser->lexer);
8591 
8592   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8593 
8594   type1 = cp_parser_type_id (parser);
8595 
8596   if (type1 == error_mark_node)
8597     return error_mark_node;
8598 
8599   /* Build a trivial decl-specifier-seq.  */
8600   clear_decl_specs (&decl_specs);
8601   decl_specs.type = type1;
8602 
8603   /* Call grokdeclarator to figure out what type this is.  */
8604   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8605 			  /*initialized=*/0, /*attrlist=*/NULL);
8606 
8607   if (binary)
8608     {
8609       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8610 
8611       type2 = cp_parser_type_id (parser);
8612 
8613       if (type2 == error_mark_node)
8614 	return error_mark_node;
8615 
8616       /* Build a trivial decl-specifier-seq.  */
8617       clear_decl_specs (&decl_specs);
8618       decl_specs.type = type2;
8619 
8620       /* Call grokdeclarator to figure out what type this is.  */
8621       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8622 			      /*initialized=*/0, /*attrlist=*/NULL);
8623     }
8624 
8625   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8626 
8627   /* Complete the trait expression, which may mean either processing
8628      the trait expr now or saving it for template instantiation.  */
8629   switch(kind)
8630     {
8631     case CPTK_UNDERLYING_TYPE:
8632       return finish_underlying_type (type1);
8633     case CPTK_BASES:
8634       return finish_bases (type1, false);
8635     case CPTK_DIRECT_BASES:
8636       return finish_bases (type1, true);
8637     default:
8638       return finish_trait_expr (kind, type1, type2);
8639     }
8640 }
8641 
8642 /* Lambdas that appear in variable initializer or default argument scope
8643    get that in their mangling, so we need to record it.  We might as well
8644    use the count for function and namespace scopes as well.  */
8645 static GTY(()) tree lambda_scope;
8646 static GTY(()) int lambda_count;
8647 typedef struct GTY(()) tree_int
8648 {
8649   tree t;
8650   int i;
8651 } tree_int;
8652 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8653 
8654 static void
start_lambda_scope(tree decl)8655 start_lambda_scope (tree decl)
8656 {
8657   tree_int ti;
8658   gcc_assert (decl);
8659   /* Once we're inside a function, we ignore other scopes and just push
8660      the function again so that popping works properly.  */
8661   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8662     decl = current_function_decl;
8663   ti.t = lambda_scope;
8664   ti.i = lambda_count;
8665   vec_safe_push (lambda_scope_stack, ti);
8666   if (lambda_scope != decl)
8667     {
8668       /* Don't reset the count if we're still in the same function.  */
8669       lambda_scope = decl;
8670       lambda_count = 0;
8671     }
8672 }
8673 
8674 static void
record_lambda_scope(tree lambda)8675 record_lambda_scope (tree lambda)
8676 {
8677   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8678   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8679 }
8680 
8681 static void
finish_lambda_scope(void)8682 finish_lambda_scope (void)
8683 {
8684   tree_int *p = &lambda_scope_stack->last ();
8685   if (lambda_scope != p->t)
8686     {
8687       lambda_scope = p->t;
8688       lambda_count = p->i;
8689     }
8690   lambda_scope_stack->pop ();
8691 }
8692 
8693 /* Parse a lambda expression.
8694 
8695    lambda-expression:
8696      lambda-introducer lambda-declarator [opt] compound-statement
8697 
8698    Returns a representation of the expression.  */
8699 
8700 static tree
cp_parser_lambda_expression(cp_parser * parser)8701 cp_parser_lambda_expression (cp_parser* parser)
8702 {
8703   tree lambda_expr = build_lambda_expr ();
8704   tree type;
8705   bool ok = true;
8706 
8707   LAMBDA_EXPR_LOCATION (lambda_expr)
8708     = cp_lexer_peek_token (parser->lexer)->location;
8709 
8710   if (cp_unevaluated_operand)
8711     {
8712       error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8713 		"lambda-expression in unevaluated context");
8714       ok = false;
8715     }
8716 
8717   /* We may be in the middle of deferred access check.  Disable
8718      it now.  */
8719   push_deferring_access_checks (dk_no_deferred);
8720 
8721   cp_parser_lambda_introducer (parser, lambda_expr);
8722 
8723   type = begin_lambda_type (lambda_expr);
8724   if (type == error_mark_node)
8725     return error_mark_node;
8726 
8727   record_lambda_scope (lambda_expr);
8728 
8729   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
8730   determine_visibility (TYPE_NAME (type));
8731 
8732   /* Now that we've started the type, add the capture fields for any
8733      explicit captures.  */
8734   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8735 
8736   {
8737     /* Inside the class, surrounding template-parameter-lists do not apply.  */
8738     unsigned int saved_num_template_parameter_lists
8739         = parser->num_template_parameter_lists;
8740     unsigned char in_statement = parser->in_statement;
8741     bool in_switch_statement_p = parser->in_switch_statement_p;
8742     bool fully_implicit_function_template_p
8743         = parser->fully_implicit_function_template_p;
8744     tree implicit_template_parms = parser->implicit_template_parms;
8745     cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
8746     bool auto_is_implicit_function_template_parm_p
8747         = parser->auto_is_implicit_function_template_parm_p;
8748 
8749     parser->num_template_parameter_lists = 0;
8750     parser->in_statement = 0;
8751     parser->in_switch_statement_p = false;
8752     parser->fully_implicit_function_template_p = false;
8753     parser->implicit_template_parms = 0;
8754     parser->implicit_template_scope = 0;
8755     parser->auto_is_implicit_function_template_parm_p = false;
8756 
8757     /* By virtue of defining a local class, a lambda expression has access to
8758        the private variables of enclosing classes.  */
8759 
8760     ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
8761 
8762     if (ok)
8763       cp_parser_lambda_body (parser, lambda_expr);
8764     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8765       {
8766 	if (cp_parser_skip_to_closing_brace (parser))
8767 	  cp_lexer_consume_token (parser->lexer);
8768       }
8769 
8770     /* The capture list was built up in reverse order; fix that now.  */
8771     LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8772       = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8773 
8774     if (ok)
8775       maybe_add_lambda_conv_op (type);
8776 
8777     type = finish_struct (type, /*attributes=*/NULL_TREE);
8778 
8779     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8780     parser->in_statement = in_statement;
8781     parser->in_switch_statement_p = in_switch_statement_p;
8782     parser->fully_implicit_function_template_p
8783 	= fully_implicit_function_template_p;
8784     parser->implicit_template_parms = implicit_template_parms;
8785     parser->implicit_template_scope = implicit_template_scope;
8786     parser->auto_is_implicit_function_template_parm_p
8787 	= auto_is_implicit_function_template_parm_p;
8788   }
8789 
8790   pop_deferring_access_checks ();
8791 
8792   /* This field is only used during parsing of the lambda.  */
8793   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8794 
8795   /* This lambda shouldn't have any proxies left at this point.  */
8796   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8797   /* And now that we're done, push proxies for an enclosing lambda.  */
8798   insert_pending_capture_proxies ();
8799 
8800   if (ok)
8801     return build_lambda_object (lambda_expr);
8802   else
8803     return error_mark_node;
8804 }
8805 
8806 /* Parse the beginning of a lambda expression.
8807 
8808    lambda-introducer:
8809      [ lambda-capture [opt] ]
8810 
8811    LAMBDA_EXPR is the current representation of the lambda expression.  */
8812 
8813 static void
cp_parser_lambda_introducer(cp_parser * parser,tree lambda_expr)8814 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8815 {
8816   /* Need commas after the first capture.  */
8817   bool first = true;
8818 
8819   /* Eat the leading `['.  */
8820   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8821 
8822   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
8823   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8824       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8825     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8826   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8827     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8828 
8829   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8830     {
8831       cp_lexer_consume_token (parser->lexer);
8832       first = false;
8833     }
8834 
8835   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8836     {
8837       cp_token* capture_token;
8838       tree capture_id;
8839       tree capture_init_expr;
8840       cp_id_kind idk = CP_ID_KIND_NONE;
8841       bool explicit_init_p = false;
8842 
8843       enum capture_kind_type
8844       {
8845 	BY_COPY,
8846 	BY_REFERENCE
8847       };
8848       enum capture_kind_type capture_kind = BY_COPY;
8849 
8850       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8851 	{
8852 	  error ("expected end of capture-list");
8853 	  return;
8854 	}
8855 
8856       if (first)
8857 	first = false;
8858       else
8859 	cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8860 
8861       /* Possibly capture `this'.  */
8862       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8863 	{
8864 	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8865 	  if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8866 	    pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8867 		     "with by-copy capture default");
8868 	  cp_lexer_consume_token (parser->lexer);
8869 	  add_capture (lambda_expr,
8870 		       /*id=*/this_identifier,
8871 		       /*initializer=*/finish_this_expr(),
8872 		       /*by_reference_p=*/false,
8873 		       explicit_init_p);
8874 	  continue;
8875 	}
8876 
8877       /* Remember whether we want to capture as a reference or not.  */
8878       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8879 	{
8880 	  capture_kind = BY_REFERENCE;
8881 	  cp_lexer_consume_token (parser->lexer);
8882 	}
8883 
8884       /* Get the identifier.  */
8885       capture_token = cp_lexer_peek_token (parser->lexer);
8886       capture_id = cp_parser_identifier (parser);
8887 
8888       if (capture_id == error_mark_node)
8889 	/* Would be nice to have a cp_parser_skip_to_closing_x for general
8890            delimiters, but I modified this to stop on unnested ']' as well.  It
8891            was already changed to stop on unnested '}', so the
8892            "closing_parenthesis" name is no more misleading with my change.  */
8893 	{
8894 	  cp_parser_skip_to_closing_parenthesis (parser,
8895 						 /*recovering=*/true,
8896 						 /*or_comma=*/true,
8897 						 /*consume_paren=*/true);
8898 	  break;
8899 	}
8900 
8901       /* Find the initializer for this capture.  */
8902       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
8903 	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
8904 	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8905 	{
8906 	  bool direct, non_constant;
8907 	  /* An explicit initializer exists.  */
8908 	  if (cxx_dialect < cxx1y)
8909 	    pedwarn (input_location, 0,
8910 		     "lambda capture initializers "
8911 		     "only available with -std=c++1y or -std=gnu++1y");
8912 	  capture_init_expr = cp_parser_initializer (parser, &direct,
8913 						     &non_constant);
8914 	  explicit_init_p = true;
8915 	  if (capture_init_expr == NULL_TREE)
8916 	    {
8917 	      error ("empty initializer for lambda init-capture");
8918 	      capture_init_expr = error_mark_node;
8919 	    }
8920 	}
8921       else
8922 	{
8923 	  const char* error_msg;
8924 
8925 	  /* Turn the identifier into an id-expression.  */
8926 	  capture_init_expr
8927 	    = cp_parser_lookup_name_simple (parser, capture_id,
8928 					    capture_token->location);
8929 
8930 	  if (capture_init_expr == error_mark_node)
8931 	    {
8932 	      unqualified_name_lookup_error (capture_id);
8933 	      continue;
8934 	    }
8935 	  else if (DECL_P (capture_init_expr)
8936 		   && (!VAR_P (capture_init_expr)
8937 		       && TREE_CODE (capture_init_expr) != PARM_DECL))
8938 	    {
8939 	      error_at (capture_token->location,
8940 			"capture of non-variable %qD ",
8941 			capture_init_expr);
8942 	      inform (0, "%q+#D declared here", capture_init_expr);
8943 	      continue;
8944 	    }
8945 	  if (VAR_P (capture_init_expr)
8946 	      && decl_storage_duration (capture_init_expr) != dk_auto)
8947 	    {
8948 	      if (pedwarn (capture_token->location, 0, "capture of variable "
8949 			   "%qD with non-automatic storage duration",
8950 			   capture_init_expr))
8951 		inform (0, "%q+#D declared here", capture_init_expr);
8952 	      continue;
8953 	    }
8954 
8955 	  capture_init_expr
8956             = finish_id_expression
8957                 (capture_id,
8958 		 capture_init_expr,
8959                  parser->scope,
8960                  &idk,
8961                  /*integral_constant_expression_p=*/false,
8962                  /*allow_non_integral_constant_expression_p=*/false,
8963                  /*non_integral_constant_expression_p=*/NULL,
8964                  /*template_p=*/false,
8965                  /*done=*/true,
8966                  /*address_p=*/false,
8967                  /*template_arg_p=*/false,
8968                  &error_msg,
8969                  capture_token->location);
8970 
8971 	  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8972 	    {
8973 	      cp_lexer_consume_token (parser->lexer);
8974 	      capture_init_expr = make_pack_expansion (capture_init_expr);
8975 	    }
8976 	  else
8977 	    check_for_bare_parameter_packs (capture_init_expr);
8978 	}
8979 
8980       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8981 	  && !explicit_init_p)
8982 	{
8983 	  if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8984 	      && capture_kind == BY_COPY)
8985 	    pedwarn (capture_token->location, 0, "explicit by-copy capture "
8986 		     "of %qD redundant with by-copy capture default",
8987 		     capture_id);
8988 	  if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8989 	      && capture_kind == BY_REFERENCE)
8990 	    pedwarn (capture_token->location, 0, "explicit by-reference "
8991 		     "capture of %qD redundant with by-reference capture "
8992 		     "default", capture_id);
8993 	}
8994 
8995       add_capture (lambda_expr,
8996 		   capture_id,
8997 		   capture_init_expr,
8998 		   /*by_reference_p=*/capture_kind == BY_REFERENCE,
8999 		   explicit_init_p);
9000     }
9001 
9002   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9003 }
9004 
9005 /* Parse the (optional) middle of a lambda expression.
9006 
9007    lambda-declarator:
9008      < template-parameter-list [opt] >
9009      ( parameter-declaration-clause [opt] )
9010        attribute-specifier [opt]
9011        mutable [opt]
9012        exception-specification [opt]
9013        lambda-return-type-clause [opt]
9014 
9015    LAMBDA_EXPR is the current representation of the lambda expression.  */
9016 
9017 static bool
cp_parser_lambda_declarator_opt(cp_parser * parser,tree lambda_expr)9018 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9019 {
9020   /* 5.1.1.4 of the standard says:
9021        If a lambda-expression does not include a lambda-declarator, it is as if
9022        the lambda-declarator were ().
9023      This means an empty parameter list, no attributes, and no exception
9024      specification.  */
9025   tree param_list = void_list_node;
9026   tree attributes = NULL_TREE;
9027   tree exception_spec = NULL_TREE;
9028   tree template_param_list = NULL_TREE;
9029 
9030   /* The template-parameter-list is optional, but must begin with
9031      an opening angle if present.  */
9032   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9033     {
9034       if (cxx_dialect < cxx1y)
9035 	pedwarn (parser->lexer->next_token->location, 0,
9036 		 "lambda templates are only available with "
9037 		 "-std=c++1y or -std=gnu++1y");
9038 
9039       cp_lexer_consume_token (parser->lexer);
9040 
9041       template_param_list = cp_parser_template_parameter_list (parser);
9042 
9043       cp_parser_skip_to_end_of_template_parameter_list (parser);
9044 
9045       /* We just processed one more parameter list.  */
9046       ++parser->num_template_parameter_lists;
9047     }
9048 
9049   /* The parameter-declaration-clause is optional (unless
9050      template-parameter-list was given), but must begin with an
9051      opening parenthesis if present.  */
9052   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9053     {
9054       cp_lexer_consume_token (parser->lexer);
9055 
9056       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9057 
9058       /* Parse parameters.  */
9059       param_list = cp_parser_parameter_declaration_clause (parser);
9060 
9061       /* Default arguments shall not be specified in the
9062 	 parameter-declaration-clause of a lambda-declarator.  */
9063       for (tree t = param_list; t; t = TREE_CHAIN (t))
9064 	if (TREE_PURPOSE (t))
9065 	  pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9066 		   "default argument specified for lambda parameter");
9067 
9068       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9069 
9070       attributes = cp_parser_attributes_opt (parser);
9071 
9072       /* Parse optional `mutable' keyword.  */
9073       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9074         {
9075           cp_lexer_consume_token (parser->lexer);
9076           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9077         }
9078 
9079       /* Parse optional exception specification.  */
9080       exception_spec = cp_parser_exception_specification_opt (parser);
9081 
9082       /* Parse optional trailing return type.  */
9083       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9084         {
9085           cp_lexer_consume_token (parser->lexer);
9086           LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9087 	    = cp_parser_trailing_type_id (parser);
9088         }
9089 
9090       /* The function parameters must be in scope all the way until after the
9091          trailing-return-type in case of decltype.  */
9092       pop_bindings_and_leave_scope ();
9093     }
9094   else if (template_param_list != NULL_TREE) // generate diagnostic
9095     cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9096 
9097   /* Create the function call operator.
9098 
9099      Messing with declarators like this is no uglier than building up the
9100      FUNCTION_DECL by hand, and this is less likely to get out of sync with
9101      other code.  */
9102   {
9103     cp_decl_specifier_seq return_type_specs;
9104     cp_declarator* declarator;
9105     tree fco;
9106     int quals;
9107     void *p;
9108 
9109     clear_decl_specs (&return_type_specs);
9110     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9111       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9112     else
9113       /* Maybe we will deduce the return type later.  */
9114       return_type_specs.type = make_auto ();
9115 
9116     p = obstack_alloc (&declarator_obstack, 0);
9117 
9118     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9119 				     sfk_none);
9120 
9121     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9122 	     ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9123     declarator = make_call_declarator (declarator, param_list, quals,
9124 				       VIRT_SPEC_UNSPECIFIED,
9125                                        REF_QUAL_NONE,
9126 				       exception_spec,
9127                                        /*late_return_type=*/NULL_TREE);
9128     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9129 
9130     fco = grokmethod (&return_type_specs,
9131 		      declarator,
9132 		      attributes);
9133     if (fco != error_mark_node)
9134       {
9135 	DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9136 	DECL_ARTIFICIAL (fco) = 1;
9137 	/* Give the object parameter a different name.  */
9138 	DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9139       }
9140     if (template_param_list)
9141       {
9142 	fco = finish_member_template_decl (fco);
9143 	finish_template_decl (template_param_list);
9144 	--parser->num_template_parameter_lists;
9145       }
9146     else if (parser->fully_implicit_function_template_p)
9147       fco = finish_fully_implicit_template (parser, fco);
9148 
9149     finish_member_declaration (fco);
9150 
9151     obstack_free (&declarator_obstack, p);
9152 
9153     return (fco != error_mark_node);
9154   }
9155 }
9156 
9157 /* Parse the body of a lambda expression, which is simply
9158 
9159    compound-statement
9160 
9161    but which requires special handling.
9162    LAMBDA_EXPR is the current representation of the lambda expression.  */
9163 
9164 static void
cp_parser_lambda_body(cp_parser * parser,tree lambda_expr)9165 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9166 {
9167   bool nested = (current_function_decl != NULL_TREE);
9168   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9169   if (nested)
9170     push_function_context ();
9171   else
9172     /* Still increment function_depth so that we don't GC in the
9173        middle of an expression.  */
9174     ++function_depth;
9175   /* Clear this in case we're in the middle of a default argument.  */
9176   parser->local_variables_forbidden_p = false;
9177 
9178   /* Finish the function call operator
9179      - class_specifier
9180      + late_parsing_for_member
9181      + function_definition_after_declarator
9182      + ctor_initializer_opt_and_function_body  */
9183   {
9184     tree fco = lambda_function (lambda_expr);
9185     tree body;
9186     bool done = false;
9187     tree compound_stmt;
9188     tree cap;
9189 
9190     /* Let the front end know that we are going to be defining this
9191        function.  */
9192     start_preparsed_function (fco,
9193 			      NULL_TREE,
9194 			      SF_PRE_PARSED | SF_INCLASS_INLINE);
9195 
9196     start_lambda_scope (fco);
9197     body = begin_function_body ();
9198 
9199     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9200       goto out;
9201 
9202     /* Push the proxies for any explicit captures.  */
9203     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9204 	 cap = TREE_CHAIN (cap))
9205       build_capture_proxy (TREE_PURPOSE (cap));
9206 
9207     compound_stmt = begin_compound_stmt (0);
9208 
9209     /* 5.1.1.4 of the standard says:
9210          If a lambda-expression does not include a trailing-return-type, it
9211          is as if the trailing-return-type denotes the following type:
9212 	  * if the compound-statement is of the form
9213                { return attribute-specifier [opt] expression ; }
9214              the type of the returned expression after lvalue-to-rvalue
9215              conversion (_conv.lval_ 4.1), array-to-pointer conversion
9216              (_conv.array_ 4.2), and function-to-pointer conversion
9217              (_conv.func_ 4.3);
9218           * otherwise, void.  */
9219 
9220     /* In a lambda that has neither a lambda-return-type-clause
9221        nor a deducible form, errors should be reported for return statements
9222        in the body.  Since we used void as the placeholder return type, parsing
9223        the body as usual will give such desired behavior.  */
9224     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9225         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9226         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9227       {
9228 	tree expr = NULL_TREE;
9229 	cp_id_kind idk = CP_ID_KIND_NONE;
9230 
9231 	/* Parse tentatively in case there's more after the initial return
9232 	   statement.  */
9233 	cp_parser_parse_tentatively (parser);
9234 
9235 	cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9236 
9237 	expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
9238 
9239 	cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9240 	cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9241 
9242 	if (cp_parser_parse_definitely (parser))
9243 	  {
9244 	    if (!processing_template_decl)
9245 	      apply_deduced_return_type (fco, lambda_return_type (expr));
9246 
9247 	    /* Will get error here if type not deduced yet.  */
9248 	    finish_return_stmt (expr);
9249 
9250 	    done = true;
9251 	  }
9252       }
9253 
9254     if (!done)
9255       {
9256 	while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9257 	  cp_parser_label_declaration (parser);
9258 	cp_parser_statement_seq_opt (parser, NULL_TREE);
9259 	cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9260       }
9261 
9262     finish_compound_stmt (compound_stmt);
9263 
9264   out:
9265     finish_function_body (body);
9266     finish_lambda_scope ();
9267 
9268     /* Finish the function and generate code for it if necessary.  */
9269     tree fn = finish_function (/*inline*/2);
9270 
9271     /* Only expand if the call op is not a template.  */
9272     if (!DECL_TEMPLATE_INFO (fco))
9273       expand_or_defer_fn (fn);
9274   }
9275 
9276   parser->local_variables_forbidden_p = local_variables_forbidden_p;
9277   if (nested)
9278     pop_function_context();
9279   else
9280     --function_depth;
9281 }
9282 
9283 /* Statements [gram.stmt.stmt]  */
9284 
9285 /* Parse a statement.
9286 
9287    statement:
9288      labeled-statement
9289      expression-statement
9290      compound-statement
9291      selection-statement
9292      iteration-statement
9293      jump-statement
9294      declaration-statement
9295      try-block
9296 
9297   C++11:
9298 
9299   statement:
9300     labeled-statement
9301     attribute-specifier-seq (opt) expression-statement
9302     attribute-specifier-seq (opt) compound-statement
9303     attribute-specifier-seq (opt) selection-statement
9304     attribute-specifier-seq (opt) iteration-statement
9305     attribute-specifier-seq (opt) jump-statement
9306     declaration-statement
9307     attribute-specifier-seq (opt) try-block
9308 
9309   TM Extension:
9310 
9311    statement:
9312      atomic-statement
9313 
9314   IN_COMPOUND is true when the statement is nested inside a
9315   cp_parser_compound_statement; this matters for certain pragmas.
9316 
9317   If IF_P is not NULL, *IF_P is set to indicate whether the statement
9318   is a (possibly labeled) if statement which is not enclosed in braces
9319   and has an else clause.  This is used to implement -Wparentheses.  */
9320 
9321 static void
cp_parser_statement(cp_parser * parser,tree in_statement_expr,bool in_compound,bool * if_p)9322 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9323 		     bool in_compound, bool *if_p)
9324 {
9325   tree statement, std_attrs = NULL_TREE;
9326   cp_token *token;
9327   location_t statement_location, attrs_location;
9328 
9329  restart:
9330   if (if_p != NULL)
9331     *if_p = false;
9332   /* There is no statement yet.  */
9333   statement = NULL_TREE;
9334 
9335   cp_lexer_save_tokens (parser->lexer);
9336   attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9337   if (c_dialect_objc ())
9338     /* In obj-c++, seeing '[[' might be the either the beginning of
9339        c++11 attributes, or a nested objc-message-expression.  So
9340        let's parse the c++11 attributes tentatively.  */
9341     cp_parser_parse_tentatively (parser);
9342   std_attrs = cp_parser_std_attribute_spec_seq (parser);
9343   if (c_dialect_objc ())
9344     {
9345       if (!cp_parser_parse_definitely (parser))
9346 	std_attrs = NULL_TREE;
9347     }
9348 
9349   /* Peek at the next token.  */
9350   token = cp_lexer_peek_token (parser->lexer);
9351   /* Remember the location of the first token in the statement.  */
9352   statement_location = token->location;
9353   /* If this is a keyword, then that will often determine what kind of
9354      statement we have.  */
9355   if (token->type == CPP_KEYWORD)
9356     {
9357       enum rid keyword = token->keyword;
9358 
9359       switch (keyword)
9360 	{
9361 	case RID_CASE:
9362 	case RID_DEFAULT:
9363 	  /* Looks like a labeled-statement with a case label.
9364 	     Parse the label, and then use tail recursion to parse
9365 	     the statement.  */
9366 	  cp_parser_label_for_labeled_statement (parser, std_attrs);
9367 	  goto restart;
9368 
9369 	case RID_IF:
9370 	case RID_SWITCH:
9371 	  statement = cp_parser_selection_statement (parser, if_p);
9372 	  break;
9373 
9374 	case RID_WHILE:
9375 	case RID_DO:
9376 	case RID_FOR:
9377 	  statement = cp_parser_iteration_statement (parser, false);
9378 	  break;
9379 
9380 	case RID_BREAK:
9381 	case RID_CONTINUE:
9382 	case RID_RETURN:
9383 	case RID_GOTO:
9384 	  statement = cp_parser_jump_statement (parser);
9385 	  break;
9386 
9387 	case RID_CILK_SYNC:
9388 	  cp_lexer_consume_token (parser->lexer);
9389 	  if (flag_cilkplus)
9390 	    {
9391 	      tree sync_expr = build_cilk_sync ();
9392 	      SET_EXPR_LOCATION (sync_expr,
9393 				 token->location);
9394 	      statement = finish_expr_stmt (sync_expr);
9395 	    }
9396 	  else
9397 	    {
9398 	      error_at (token->location, "-fcilkplus must be enabled to use"
9399 			" %<_Cilk_sync%>");
9400 	      statement = error_mark_node;
9401 	    }
9402 	  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9403 	  break;
9404 
9405 	  /* Objective-C++ exception-handling constructs.  */
9406 	case RID_AT_TRY:
9407 	case RID_AT_CATCH:
9408 	case RID_AT_FINALLY:
9409 	case RID_AT_SYNCHRONIZED:
9410 	case RID_AT_THROW:
9411 	  statement = cp_parser_objc_statement (parser);
9412 	  break;
9413 
9414 	case RID_TRY:
9415 	  statement = cp_parser_try_block (parser);
9416 	  break;
9417 
9418 	case RID_NAMESPACE:
9419 	  /* This must be a namespace alias definition.  */
9420 	  cp_parser_declaration_statement (parser);
9421 	  return;
9422 
9423 	case RID_TRANSACTION_ATOMIC:
9424 	case RID_TRANSACTION_RELAXED:
9425 	  statement = cp_parser_transaction (parser, keyword);
9426 	  break;
9427 	case RID_TRANSACTION_CANCEL:
9428 	  statement = cp_parser_transaction_cancel (parser);
9429 	  break;
9430 
9431 	default:
9432 	  /* It might be a keyword like `int' that can start a
9433 	     declaration-statement.  */
9434 	  break;
9435 	}
9436     }
9437   else if (token->type == CPP_NAME)
9438     {
9439       /* If the next token is a `:', then we are looking at a
9440 	 labeled-statement.  */
9441       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9442       if (token->type == CPP_COLON)
9443 	{
9444 	  /* Looks like a labeled-statement with an ordinary label.
9445 	     Parse the label, and then use tail recursion to parse
9446 	     the statement.  */
9447 
9448 	  cp_parser_label_for_labeled_statement (parser, std_attrs);
9449 	  goto restart;
9450 	}
9451     }
9452   /* Anything that starts with a `{' must be a compound-statement.  */
9453   else if (token->type == CPP_OPEN_BRACE)
9454     statement = cp_parser_compound_statement (parser, NULL, false, false);
9455   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9456      a statement all its own.  */
9457   else if (token->type == CPP_PRAGMA)
9458     {
9459       /* Only certain OpenMP pragmas are attached to statements, and thus
9460 	 are considered statements themselves.  All others are not.  In
9461 	 the context of a compound, accept the pragma as a "statement" and
9462 	 return so that we can check for a close brace.  Otherwise we
9463 	 require a real statement and must go back and read one.  */
9464       if (in_compound)
9465 	cp_parser_pragma (parser, pragma_compound);
9466       else if (!cp_parser_pragma (parser, pragma_stmt))
9467 	goto restart;
9468       return;
9469     }
9470   else if (token->type == CPP_EOF)
9471     {
9472       cp_parser_error (parser, "expected statement");
9473       return;
9474     }
9475 
9476   /* Everything else must be a declaration-statement or an
9477      expression-statement.  Try for the declaration-statement
9478      first, unless we are looking at a `;', in which case we know that
9479      we have an expression-statement.  */
9480   if (!statement)
9481     {
9482       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9483 	{
9484 	  if (std_attrs != NULL_TREE)
9485 	    {
9486 	      /*  Attributes should be parsed as part of the the
9487 		  declaration, so let's un-parse them.  */
9488 	      cp_lexer_rollback_tokens (parser->lexer);
9489 	      std_attrs = NULL_TREE;
9490 	    }
9491 
9492 	  cp_parser_parse_tentatively (parser);
9493 	  /* Try to parse the declaration-statement.  */
9494 	  cp_parser_declaration_statement (parser);
9495 	  /* If that worked, we're done.  */
9496 	  if (cp_parser_parse_definitely (parser))
9497 	    return;
9498 	}
9499       /* Look for an expression-statement instead.  */
9500       statement = cp_parser_expression_statement (parser, in_statement_expr);
9501     }
9502 
9503   /* Set the line number for the statement.  */
9504   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9505     SET_EXPR_LOCATION (statement, statement_location);
9506 
9507   /* Note that for now, we don't do anything with c++11 statements
9508      parsed at this level.  */
9509   if (std_attrs != NULL_TREE)
9510     warning_at (attrs_location,
9511 		OPT_Wattributes,
9512 		"attributes at the beginning of statement are ignored");
9513 }
9514 
9515 /* Parse the label for a labeled-statement, i.e.
9516 
9517    identifier :
9518    case constant-expression :
9519    default :
9520 
9521    GNU Extension:
9522    case constant-expression ... constant-expression : statement
9523 
9524    When a label is parsed without errors, the label is added to the
9525    parse tree by the finish_* functions, so this function doesn't
9526    have to return the label.  */
9527 
9528 static void
cp_parser_label_for_labeled_statement(cp_parser * parser,tree attributes)9529 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9530 {
9531   cp_token *token;
9532   tree label = NULL_TREE;
9533   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9534 
9535   /* The next token should be an identifier.  */
9536   token = cp_lexer_peek_token (parser->lexer);
9537   if (token->type != CPP_NAME
9538       && token->type != CPP_KEYWORD)
9539     {
9540       cp_parser_error (parser, "expected labeled-statement");
9541       return;
9542     }
9543 
9544   parser->colon_corrects_to_scope_p = false;
9545   switch (token->keyword)
9546     {
9547     case RID_CASE:
9548       {
9549 	tree expr, expr_hi;
9550 	cp_token *ellipsis;
9551 
9552 	/* Consume the `case' token.  */
9553 	cp_lexer_consume_token (parser->lexer);
9554 	/* Parse the constant-expression.  */
9555 	expr = cp_parser_constant_expression (parser,
9556 					      /*allow_non_constant_p=*/false,
9557 					      NULL);
9558 
9559 	ellipsis = cp_lexer_peek_token (parser->lexer);
9560 	if (ellipsis->type == CPP_ELLIPSIS)
9561 	  {
9562 	    /* Consume the `...' token.  */
9563 	    cp_lexer_consume_token (parser->lexer);
9564 	    expr_hi =
9565 	      cp_parser_constant_expression (parser,
9566 					     /*allow_non_constant_p=*/false,
9567 					     NULL);
9568 	    /* We don't need to emit warnings here, as the common code
9569 	       will do this for us.  */
9570 	  }
9571 	else
9572 	  expr_hi = NULL_TREE;
9573 
9574 	if (parser->in_switch_statement_p)
9575 	  finish_case_label (token->location, expr, expr_hi);
9576 	else
9577 	  error_at (token->location,
9578 		    "case label %qE not within a switch statement",
9579 		    expr);
9580       }
9581       break;
9582 
9583     case RID_DEFAULT:
9584       /* Consume the `default' token.  */
9585       cp_lexer_consume_token (parser->lexer);
9586 
9587       if (parser->in_switch_statement_p)
9588 	finish_case_label (token->location, NULL_TREE, NULL_TREE);
9589       else
9590 	error_at (token->location, "case label not within a switch statement");
9591       break;
9592 
9593     default:
9594       /* Anything else must be an ordinary label.  */
9595       label = finish_label_stmt (cp_parser_identifier (parser));
9596       break;
9597     }
9598 
9599   /* Require the `:' token.  */
9600   cp_parser_require (parser, CPP_COLON, RT_COLON);
9601 
9602   /* An ordinary label may optionally be followed by attributes.
9603      However, this is only permitted if the attributes are then
9604      followed by a semicolon.  This is because, for backward
9605      compatibility, when parsing
9606        lab: __attribute__ ((unused)) int i;
9607      we want the attribute to attach to "i", not "lab".  */
9608   if (label != NULL_TREE
9609       && cp_next_tokens_can_be_gnu_attribute_p (parser))
9610     {
9611       tree attrs;
9612       cp_parser_parse_tentatively (parser);
9613       attrs = cp_parser_gnu_attributes_opt (parser);
9614       if (attrs == NULL_TREE
9615 	  || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9616 	cp_parser_abort_tentative_parse (parser);
9617       else if (!cp_parser_parse_definitely (parser))
9618 	;
9619       else
9620 	attributes = chainon (attributes, attrs);
9621     }
9622 
9623   if (attributes != NULL_TREE)
9624     cplus_decl_attributes (&label, attributes, 0);
9625 
9626   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9627 }
9628 
9629 /* Parse an expression-statement.
9630 
9631    expression-statement:
9632      expression [opt] ;
9633 
9634    Returns the new EXPR_STMT -- or NULL_TREE if the expression
9635    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9636    indicates whether this expression-statement is part of an
9637    expression statement.  */
9638 
9639 static tree
cp_parser_expression_statement(cp_parser * parser,tree in_statement_expr)9640 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9641 {
9642   tree statement = NULL_TREE;
9643   cp_token *token = cp_lexer_peek_token (parser->lexer);
9644 
9645   /* If the next token is a ';', then there is no expression
9646      statement.  */
9647   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9648     {
9649       statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9650       if (statement == error_mark_node
9651 	  && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9652 	{
9653 	  cp_parser_skip_to_end_of_block_or_statement (parser);
9654 	  return error_mark_node;
9655 	}
9656     }
9657 
9658   /* Give a helpful message for "A<T>::type t;" and the like.  */
9659   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9660       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9661     {
9662       if (TREE_CODE (statement) == SCOPE_REF)
9663 	error_at (token->location, "need %<typename%> before %qE because "
9664 		  "%qT is a dependent scope",
9665 		  statement, TREE_OPERAND (statement, 0));
9666       else if (is_overloaded_fn (statement)
9667 	       && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9668 	{
9669 	  /* A::A a; */
9670 	  tree fn = get_first_fn (statement);
9671 	  error_at (token->location,
9672 		    "%<%T::%D%> names the constructor, not the type",
9673 		    DECL_CONTEXT (fn), DECL_NAME (fn));
9674 	}
9675     }
9676 
9677   /* Consume the final `;'.  */
9678   cp_parser_consume_semicolon_at_end_of_statement (parser);
9679 
9680   if (in_statement_expr
9681       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9682     /* This is the final expression statement of a statement
9683        expression.  */
9684     statement = finish_stmt_expr_expr (statement, in_statement_expr);
9685   else if (statement)
9686     statement = finish_expr_stmt (statement);
9687 
9688   return statement;
9689 }
9690 
9691 /* Parse a compound-statement.
9692 
9693    compound-statement:
9694      { statement-seq [opt] }
9695 
9696    GNU extension:
9697 
9698    compound-statement:
9699      { label-declaration-seq [opt] statement-seq [opt] }
9700 
9701    label-declaration-seq:
9702      label-declaration
9703      label-declaration-seq label-declaration
9704 
9705    Returns a tree representing the statement.  */
9706 
9707 static tree
cp_parser_compound_statement(cp_parser * parser,tree in_statement_expr,bool in_try,bool function_body)9708 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9709 			      bool in_try, bool function_body)
9710 {
9711   tree compound_stmt;
9712 
9713   /* Consume the `{'.  */
9714   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9715     return error_mark_node;
9716   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9717       && !function_body)
9718     pedwarn (input_location, OPT_Wpedantic,
9719 	     "compound-statement in constexpr function");
9720   /* Begin the compound-statement.  */
9721   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9722   /* If the next keyword is `__label__' we have a label declaration.  */
9723   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9724     cp_parser_label_declaration (parser);
9725   /* Parse an (optional) statement-seq.  */
9726   cp_parser_statement_seq_opt (parser, in_statement_expr);
9727   /* Finish the compound-statement.  */
9728   finish_compound_stmt (compound_stmt);
9729   /* Consume the `}'.  */
9730   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9731 
9732   return compound_stmt;
9733 }
9734 
9735 /* Parse an (optional) statement-seq.
9736 
9737    statement-seq:
9738      statement
9739      statement-seq [opt] statement  */
9740 
9741 static void
cp_parser_statement_seq_opt(cp_parser * parser,tree in_statement_expr)9742 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9743 {
9744   /* Scan statements until there aren't any more.  */
9745   while (true)
9746     {
9747       cp_token *token = cp_lexer_peek_token (parser->lexer);
9748 
9749       /* If we are looking at a `}', then we have run out of
9750 	 statements; the same is true if we have reached the end
9751 	 of file, or have stumbled upon a stray '@end'.  */
9752       if (token->type == CPP_CLOSE_BRACE
9753 	  || token->type == CPP_EOF
9754 	  || token->type == CPP_PRAGMA_EOL
9755 	  || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9756 	break;
9757 
9758       /* If we are in a compound statement and find 'else' then
9759 	 something went wrong.  */
9760       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9761 	{
9762 	  if (parser->in_statement & IN_IF_STMT)
9763 	    break;
9764 	  else
9765 	    {
9766 	      token = cp_lexer_consume_token (parser->lexer);
9767 	      error_at (token->location, "%<else%> without a previous %<if%>");
9768 	    }
9769 	}
9770 
9771       /* Parse the statement.  */
9772       cp_parser_statement (parser, in_statement_expr, true, NULL);
9773     }
9774 }
9775 
9776 /* Parse a selection-statement.
9777 
9778    selection-statement:
9779      if ( condition ) statement
9780      if ( condition ) statement else statement
9781      switch ( condition ) statement
9782 
9783    Returns the new IF_STMT or SWITCH_STMT.
9784 
9785    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9786    is a (possibly labeled) if statement which is not enclosed in
9787    braces and has an else clause.  This is used to implement
9788    -Wparentheses.  */
9789 
9790 static tree
cp_parser_selection_statement(cp_parser * parser,bool * if_p)9791 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9792 {
9793   cp_token *token;
9794   enum rid keyword;
9795 
9796   if (if_p != NULL)
9797     *if_p = false;
9798 
9799   /* Peek at the next token.  */
9800   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9801 
9802   /* See what kind of keyword it is.  */
9803   keyword = token->keyword;
9804   switch (keyword)
9805     {
9806     case RID_IF:
9807     case RID_SWITCH:
9808       {
9809 	tree statement;
9810 	tree condition;
9811 
9812 	/* Look for the `('.  */
9813 	if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9814 	  {
9815 	    cp_parser_skip_to_end_of_statement (parser);
9816 	    return error_mark_node;
9817 	  }
9818 
9819 	/* Begin the selection-statement.  */
9820 	if (keyword == RID_IF)
9821 	  statement = begin_if_stmt ();
9822 	else
9823 	  statement = begin_switch_stmt ();
9824 
9825 	/* Parse the condition.  */
9826 	condition = cp_parser_condition (parser);
9827 	/* Look for the `)'.  */
9828 	if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9829 	  cp_parser_skip_to_closing_parenthesis (parser, true, false,
9830 						 /*consume_paren=*/true);
9831 
9832 	if (keyword == RID_IF)
9833 	  {
9834 	    bool nested_if;
9835 	    unsigned char in_statement;
9836 
9837 	    /* Add the condition.  */
9838 	    finish_if_stmt_cond (condition, statement);
9839 
9840 	    /* Parse the then-clause.  */
9841 	    in_statement = parser->in_statement;
9842 	    parser->in_statement |= IN_IF_STMT;
9843 	    if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9844 	      {
9845 	        location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9846 		add_stmt (build_empty_stmt (loc));
9847 		cp_lexer_consume_token (parser->lexer);
9848 	        if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9849 		  warning_at (loc, OPT_Wempty_body, "suggest braces around "
9850 			      "empty body in an %<if%> statement");
9851 		nested_if = false;
9852 	      }
9853 	    else
9854 	      cp_parser_implicitly_scoped_statement (parser, &nested_if);
9855 	    parser->in_statement = in_statement;
9856 
9857 	    finish_then_clause (statement);
9858 
9859 	    /* If the next token is `else', parse the else-clause.  */
9860 	    if (cp_lexer_next_token_is_keyword (parser->lexer,
9861 						RID_ELSE))
9862 	      {
9863 		/* Consume the `else' keyword.  */
9864 		cp_lexer_consume_token (parser->lexer);
9865 		begin_else_clause (statement);
9866 		/* Parse the else-clause.  */
9867 	        if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9868 	          {
9869 		    location_t loc;
9870 		    loc = cp_lexer_peek_token (parser->lexer)->location;
9871 		    warning_at (loc,
9872 				OPT_Wempty_body, "suggest braces around "
9873 			        "empty body in an %<else%> statement");
9874 		    add_stmt (build_empty_stmt (loc));
9875 		    cp_lexer_consume_token (parser->lexer);
9876 		  }
9877 		else
9878 		  cp_parser_implicitly_scoped_statement (parser, NULL);
9879 
9880 		finish_else_clause (statement);
9881 
9882 		/* If we are currently parsing a then-clause, then
9883 		   IF_P will not be NULL.  We set it to true to
9884 		   indicate that this if statement has an else clause.
9885 		   This may trigger the Wparentheses warning below
9886 		   when we get back up to the parent if statement.  */
9887 		if (if_p != NULL)
9888 		  *if_p = true;
9889 	      }
9890 	    else
9891 	      {
9892 		/* This if statement does not have an else clause.  If
9893 		   NESTED_IF is true, then the then-clause is an if
9894 		   statement which does have an else clause.  We warn
9895 		   about the potential ambiguity.  */
9896 		if (nested_if)
9897 		  warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9898 			      "suggest explicit braces to avoid ambiguous"
9899 			      " %<else%>");
9900 	      }
9901 
9902 	    /* Now we're all done with the if-statement.  */
9903 	    finish_if_stmt (statement);
9904 	  }
9905 	else
9906 	  {
9907 	    bool in_switch_statement_p;
9908 	    unsigned char in_statement;
9909 
9910 	    /* Add the condition.  */
9911 	    finish_switch_cond (condition, statement);
9912 
9913 	    /* Parse the body of the switch-statement.  */
9914 	    in_switch_statement_p = parser->in_switch_statement_p;
9915 	    in_statement = parser->in_statement;
9916 	    parser->in_switch_statement_p = true;
9917 	    parser->in_statement |= IN_SWITCH_STMT;
9918 	    cp_parser_implicitly_scoped_statement (parser, NULL);
9919 	    parser->in_switch_statement_p = in_switch_statement_p;
9920 	    parser->in_statement = in_statement;
9921 
9922 	    /* Now we're all done with the switch-statement.  */
9923 	    finish_switch_stmt (statement);
9924 	  }
9925 
9926 	return statement;
9927       }
9928       break;
9929 
9930     default:
9931       cp_parser_error (parser, "expected selection-statement");
9932       return error_mark_node;
9933     }
9934 }
9935 
9936 /* Parse a condition.
9937 
9938    condition:
9939      expression
9940      type-specifier-seq declarator = initializer-clause
9941      type-specifier-seq declarator braced-init-list
9942 
9943    GNU Extension:
9944 
9945    condition:
9946      type-specifier-seq declarator asm-specification [opt]
9947        attributes [opt] = assignment-expression
9948 
9949    Returns the expression that should be tested.  */
9950 
9951 static tree
cp_parser_condition(cp_parser * parser)9952 cp_parser_condition (cp_parser* parser)
9953 {
9954   cp_decl_specifier_seq type_specifiers;
9955   const char *saved_message;
9956   int declares_class_or_enum;
9957 
9958   /* Try the declaration first.  */
9959   cp_parser_parse_tentatively (parser);
9960   /* New types are not allowed in the type-specifier-seq for a
9961      condition.  */
9962   saved_message = parser->type_definition_forbidden_message;
9963   parser->type_definition_forbidden_message
9964     = G_("types may not be defined in conditions");
9965   /* Parse the type-specifier-seq.  */
9966   cp_parser_decl_specifier_seq (parser,
9967 				CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9968 				&type_specifiers,
9969 				&declares_class_or_enum);
9970   /* Restore the saved message.  */
9971   parser->type_definition_forbidden_message = saved_message;
9972   /* If all is well, we might be looking at a declaration.  */
9973   if (!cp_parser_error_occurred (parser))
9974     {
9975       tree decl;
9976       tree asm_specification;
9977       tree attributes;
9978       cp_declarator *declarator;
9979       tree initializer = NULL_TREE;
9980 
9981       /* Parse the declarator.  */
9982       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9983 					 /*ctor_dtor_or_conv_p=*/NULL,
9984 					 /*parenthesized_p=*/NULL,
9985 					 /*member_p=*/false);
9986       /* Parse the attributes.  */
9987       attributes = cp_parser_attributes_opt (parser);
9988       /* Parse the asm-specification.  */
9989       asm_specification = cp_parser_asm_specification_opt (parser);
9990       /* If the next token is not an `=' or '{', then we might still be
9991 	 looking at an expression.  For example:
9992 
9993 	   if (A(a).x)
9994 
9995 	 looks like a decl-specifier-seq and a declarator -- but then
9996 	 there is no `=', so this is an expression.  */
9997       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9998 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9999 	cp_parser_simulate_error (parser);
10000 
10001       /* If we did see an `=' or '{', then we are looking at a declaration
10002 	 for sure.  */
10003       if (cp_parser_parse_definitely (parser))
10004 	{
10005 	  tree pushed_scope;
10006 	  bool non_constant_p;
10007 	  bool flags = LOOKUP_ONLYCONVERTING;
10008 
10009 	  /* Create the declaration.  */
10010 	  decl = start_decl (declarator, &type_specifiers,
10011 			     /*initialized_p=*/true,
10012 			     attributes, /*prefix_attributes=*/NULL_TREE,
10013 			     &pushed_scope);
10014 
10015 	  /* Parse the initializer.  */
10016 	  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10017 	    {
10018 	      initializer = cp_parser_braced_list (parser, &non_constant_p);
10019 	      CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10020 	      flags = 0;
10021 	    }
10022 	  else
10023 	    {
10024 	      /* Consume the `='.  */
10025 	      cp_parser_require (parser, CPP_EQ, RT_EQ);
10026 	      initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10027 	    }
10028 	  if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10029 	    maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10030 
10031 	  /* Process the initializer.  */
10032 	  cp_finish_decl (decl,
10033 			  initializer, !non_constant_p,
10034 			  asm_specification,
10035 			  flags);
10036 
10037 	  if (pushed_scope)
10038 	    pop_scope (pushed_scope);
10039 
10040 	  return convert_from_reference (decl);
10041 	}
10042     }
10043   /* If we didn't even get past the declarator successfully, we are
10044      definitely not looking at a declaration.  */
10045   else
10046     cp_parser_abort_tentative_parse (parser);
10047 
10048   /* Otherwise, we are looking at an expression.  */
10049   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
10050 }
10051 
10052 /* Parses a for-statement or range-for-statement until the closing ')',
10053    not included. */
10054 
10055 static tree
cp_parser_for(cp_parser * parser,bool ivdep)10056 cp_parser_for (cp_parser *parser, bool ivdep)
10057 {
10058   tree init, scope, decl;
10059   bool is_range_for;
10060 
10061   /* Begin the for-statement.  */
10062   scope = begin_for_scope (&init);
10063 
10064   /* Parse the initialization.  */
10065   is_range_for = cp_parser_for_init_statement (parser, &decl);
10066 
10067   if (is_range_for)
10068     return cp_parser_range_for (parser, scope, init, decl, ivdep);
10069   else
10070     return cp_parser_c_for (parser, scope, init, ivdep);
10071 }
10072 
10073 static tree
cp_parser_c_for(cp_parser * parser,tree scope,tree init,bool ivdep)10074 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10075 {
10076   /* Normal for loop */
10077   tree condition = NULL_TREE;
10078   tree expression = NULL_TREE;
10079   tree stmt;
10080 
10081   stmt = begin_for_stmt (scope, init);
10082   /* The for-init-statement has already been parsed in
10083      cp_parser_for_init_statement, so no work is needed here.  */
10084   finish_for_init_stmt (stmt);
10085 
10086   /* If there's a condition, process it.  */
10087   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10088     condition = cp_parser_condition (parser);
10089   else if (ivdep)
10090     {
10091       cp_parser_error (parser, "missing loop condition in loop with "
10092 		       "%<GCC ivdep%> pragma");
10093       condition = error_mark_node;
10094     }
10095   finish_for_cond (condition, stmt, ivdep);
10096   /* Look for the `;'.  */
10097   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10098 
10099   /* If there's an expression, process it.  */
10100   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10101     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10102   finish_for_expr (expression, stmt);
10103 
10104   return stmt;
10105 }
10106 
10107 /* Tries to parse a range-based for-statement:
10108 
10109   range-based-for:
10110     decl-specifier-seq declarator : expression
10111 
10112   The decl-specifier-seq declarator and the `:' are already parsed by
10113   cp_parser_for_init_statement. If processing_template_decl it returns a
10114   newly created RANGE_FOR_STMT; if not, it is converted to a
10115   regular FOR_STMT.  */
10116 
10117 static tree
cp_parser_range_for(cp_parser * parser,tree scope,tree init,tree range_decl,bool ivdep)10118 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10119 		     bool ivdep)
10120 {
10121   tree stmt, range_expr;
10122 
10123   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10124     {
10125       bool expr_non_constant_p;
10126       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10127     }
10128   else
10129     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10130 
10131   /* If in template, STMT is converted to a normal for-statement
10132      at instantiation. If not, it is done just ahead. */
10133   if (processing_template_decl)
10134     {
10135       if (check_for_bare_parameter_packs (range_expr))
10136 	range_expr = error_mark_node;
10137       stmt = begin_range_for_stmt (scope, init);
10138       if (ivdep)
10139 	RANGE_FOR_IVDEP (stmt) = 1;
10140       finish_range_for_decl (stmt, range_decl, range_expr);
10141       if (!type_dependent_expression_p (range_expr)
10142 	  /* do_auto_deduction doesn't mess with template init-lists.  */
10143 	  && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10144 	do_range_for_auto_deduction (range_decl, range_expr);
10145     }
10146   else
10147     {
10148       stmt = begin_for_stmt (scope, init);
10149       stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10150     }
10151   return stmt;
10152 }
10153 
10154 /* Subroutine of cp_convert_range_for: given the initializer expression,
10155    builds up the range temporary.  */
10156 
10157 static tree
build_range_temp(tree range_expr)10158 build_range_temp (tree range_expr)
10159 {
10160   tree range_type, range_temp;
10161 
10162   /* Find out the type deduced by the declaration
10163      `auto &&__range = range_expr'.  */
10164   range_type = cp_build_reference_type (make_auto (), true);
10165   range_type = do_auto_deduction (range_type, range_expr,
10166 				  type_uses_auto (range_type));
10167 
10168   /* Create the __range variable.  */
10169   range_temp = build_decl (input_location, VAR_DECL,
10170 			   get_identifier ("__for_range"), range_type);
10171   TREE_USED (range_temp) = 1;
10172   DECL_ARTIFICIAL (range_temp) = 1;
10173 
10174   return range_temp;
10175 }
10176 
10177 /* Used by cp_parser_range_for in template context: we aren't going to
10178    do a full conversion yet, but we still need to resolve auto in the
10179    type of the for-range-declaration if present.  This is basically
10180    a shortcut version of cp_convert_range_for.  */
10181 
10182 static void
do_range_for_auto_deduction(tree decl,tree range_expr)10183 do_range_for_auto_deduction (tree decl, tree range_expr)
10184 {
10185   tree auto_node = type_uses_auto (TREE_TYPE (decl));
10186   if (auto_node)
10187     {
10188       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10189       range_temp = convert_from_reference (build_range_temp (range_expr));
10190       iter_type = (cp_parser_perform_range_for_lookup
10191 		   (range_temp, &begin_dummy, &end_dummy));
10192       if (iter_type)
10193 	{
10194 	  iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10195 				  iter_type);
10196 	  iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10197 					    tf_warning_or_error);
10198 	  TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10199 						iter_decl, auto_node);
10200 	}
10201     }
10202 }
10203 
10204 /* Converts a range-based for-statement into a normal
10205    for-statement, as per the definition.
10206 
10207       for (RANGE_DECL : RANGE_EXPR)
10208 	BLOCK
10209 
10210    should be equivalent to:
10211 
10212       {
10213 	auto &&__range = RANGE_EXPR;
10214 	for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10215 	      __begin != __end;
10216 	      ++__begin)
10217 	  {
10218 	      RANGE_DECL = *__begin;
10219 	      BLOCK
10220 	  }
10221       }
10222 
10223    If RANGE_EXPR is an array:
10224 	BEGIN_EXPR = __range
10225 	END_EXPR = __range + ARRAY_SIZE(__range)
10226    Else if RANGE_EXPR has a member 'begin' or 'end':
10227 	BEGIN_EXPR = __range.begin()
10228 	END_EXPR = __range.end()
10229    Else:
10230 	BEGIN_EXPR = begin(__range)
10231 	END_EXPR = end(__range);
10232 
10233    If __range has a member 'begin' but not 'end', or vice versa, we must
10234    still use the second alternative (it will surely fail, however).
10235    When calling begin()/end() in the third alternative we must use
10236    argument dependent lookup, but always considering 'std' as an associated
10237    namespace.  */
10238 
10239 tree
cp_convert_range_for(tree statement,tree range_decl,tree range_expr,bool ivdep)10240 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10241 		      bool ivdep)
10242 {
10243   tree begin, end;
10244   tree iter_type, begin_expr, end_expr;
10245   tree condition, expression;
10246 
10247   if (range_decl == error_mark_node || range_expr == error_mark_node)
10248     /* If an error happened previously do nothing or else a lot of
10249        unhelpful errors would be issued.  */
10250     begin_expr = end_expr = iter_type = error_mark_node;
10251   else
10252     {
10253       tree range_temp;
10254 
10255       if (TREE_CODE (range_expr) == VAR_DECL
10256 	  && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10257 	/* Can't bind a reference to an array of runtime bound.  */
10258 	range_temp = range_expr;
10259       else
10260 	{
10261 	  range_temp = build_range_temp (range_expr);
10262 	  pushdecl (range_temp);
10263 	  cp_finish_decl (range_temp, range_expr,
10264 			  /*is_constant_init*/false, NULL_TREE,
10265 			  LOOKUP_ONLYCONVERTING);
10266 	  range_temp = convert_from_reference (range_temp);
10267 	}
10268       iter_type = cp_parser_perform_range_for_lookup (range_temp,
10269 						      &begin_expr, &end_expr);
10270     }
10271 
10272   /* The new for initialization statement.  */
10273   begin = build_decl (input_location, VAR_DECL,
10274 		      get_identifier ("__for_begin"), iter_type);
10275   TREE_USED (begin) = 1;
10276   DECL_ARTIFICIAL (begin) = 1;
10277   pushdecl (begin);
10278   cp_finish_decl (begin, begin_expr,
10279 		  /*is_constant_init*/false, NULL_TREE,
10280 		  LOOKUP_ONLYCONVERTING);
10281 
10282   end = build_decl (input_location, VAR_DECL,
10283 		    get_identifier ("__for_end"), iter_type);
10284   TREE_USED (end) = 1;
10285   DECL_ARTIFICIAL (end) = 1;
10286   pushdecl (end);
10287   cp_finish_decl (end, end_expr,
10288 		  /*is_constant_init*/false, NULL_TREE,
10289 		  LOOKUP_ONLYCONVERTING);
10290 
10291   finish_for_init_stmt (statement);
10292 
10293   /* The new for condition.  */
10294   condition = build_x_binary_op (input_location, NE_EXPR,
10295 				 begin, ERROR_MARK,
10296 				 end, ERROR_MARK,
10297 				 NULL, tf_warning_or_error);
10298   finish_for_cond (condition, statement, ivdep);
10299 
10300   /* The new increment expression.  */
10301   expression = finish_unary_op_expr (input_location,
10302 				     PREINCREMENT_EXPR, begin,
10303 				     tf_warning_or_error);
10304   finish_for_expr (expression, statement);
10305 
10306   /* The declaration is initialized with *__begin inside the loop body.  */
10307   cp_finish_decl (range_decl,
10308 		  build_x_indirect_ref (input_location, begin, RO_NULL,
10309 					tf_warning_or_error),
10310 		  /*is_constant_init*/false, NULL_TREE,
10311 		  LOOKUP_ONLYCONVERTING);
10312 
10313   return statement;
10314 }
10315 
10316 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10317    We need to solve both at the same time because the method used
10318    depends on the existence of members begin or end.
10319    Returns the type deduced for the iterator expression.  */
10320 
10321 static tree
cp_parser_perform_range_for_lookup(tree range,tree * begin,tree * end)10322 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10323 {
10324   if (error_operand_p (range))
10325     {
10326       *begin = *end = error_mark_node;
10327       return error_mark_node;
10328     }
10329 
10330   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10331     {
10332       error ("range-based %<for%> expression of type %qT "
10333 	     "has incomplete type", TREE_TYPE (range));
10334       *begin = *end = error_mark_node;
10335       return error_mark_node;
10336     }
10337   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10338     {
10339       /* If RANGE is an array, we will use pointer arithmetic.  */
10340       *begin = range;
10341       *end = build_binary_op (input_location, PLUS_EXPR,
10342 			      range,
10343 			      array_type_nelts_top (TREE_TYPE (range)),
10344 			      0);
10345       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10346     }
10347   else
10348     {
10349       /* If it is not an array, we must do a bit of magic.  */
10350       tree id_begin, id_end;
10351       tree member_begin, member_end;
10352 
10353       *begin = *end = error_mark_node;
10354 
10355       id_begin = get_identifier ("begin");
10356       id_end = get_identifier ("end");
10357       member_begin = lookup_member (TREE_TYPE (range), id_begin,
10358 				    /*protect=*/2, /*want_type=*/false,
10359 				    tf_warning_or_error);
10360       member_end = lookup_member (TREE_TYPE (range), id_end,
10361 				  /*protect=*/2, /*want_type=*/false,
10362 				  tf_warning_or_error);
10363 
10364       if (member_begin != NULL_TREE || member_end != NULL_TREE)
10365 	{
10366 	  /* Use the member functions.  */
10367 	  if (member_begin != NULL_TREE)
10368 	    *begin = cp_parser_range_for_member_function (range, id_begin);
10369 	  else
10370 	    error ("range-based %<for%> expression of type %qT has an "
10371 		   "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10372 
10373 	  if (member_end != NULL_TREE)
10374 	    *end = cp_parser_range_for_member_function (range, id_end);
10375 	  else
10376 	    error ("range-based %<for%> expression of type %qT has a "
10377 		   "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10378 	}
10379       else
10380 	{
10381 	  /* Use global functions with ADL.  */
10382 	  vec<tree, va_gc> *vec;
10383 	  vec = make_tree_vector ();
10384 
10385 	  vec_safe_push (vec, range);
10386 
10387 	  member_begin = perform_koenig_lookup (id_begin, vec,
10388 						tf_warning_or_error);
10389 	  *begin = finish_call_expr (member_begin, &vec, false, true,
10390 				     tf_warning_or_error);
10391 	  member_end = perform_koenig_lookup (id_end, vec,
10392 					      tf_warning_or_error);
10393 	  *end = finish_call_expr (member_end, &vec, false, true,
10394 				   tf_warning_or_error);
10395 
10396 	  release_tree_vector (vec);
10397 	}
10398 
10399       /* Last common checks.  */
10400       if (*begin == error_mark_node || *end == error_mark_node)
10401 	{
10402 	  /* If one of the expressions is an error do no more checks.  */
10403 	  *begin = *end = error_mark_node;
10404 	  return error_mark_node;
10405 	}
10406       else if (type_dependent_expression_p (*begin)
10407 	       || type_dependent_expression_p (*end))
10408 	/* Can happen, when, eg, in a template context, Koenig lookup
10409 	   can't resolve begin/end (c++/58503).  */
10410 	return NULL_TREE;
10411       else
10412 	{
10413 	  tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10414 	  /* The unqualified type of the __begin and __end temporaries should
10415 	     be the same, as required by the multiple auto declaration.  */
10416 	  if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10417 	    error ("inconsistent begin/end types in range-based %<for%> "
10418 		   "statement: %qT and %qT",
10419 		   TREE_TYPE (*begin), TREE_TYPE (*end));
10420 	  return iter_type;
10421 	}
10422     }
10423 }
10424 
10425 /* Helper function for cp_parser_perform_range_for_lookup.
10426    Builds a tree for RANGE.IDENTIFIER().  */
10427 
10428 static tree
cp_parser_range_for_member_function(tree range,tree identifier)10429 cp_parser_range_for_member_function (tree range, tree identifier)
10430 {
10431   tree member, res;
10432   vec<tree, va_gc> *vec;
10433 
10434   member = finish_class_member_access_expr (range, identifier,
10435 					    false, tf_warning_or_error);
10436   if (member == error_mark_node)
10437     return error_mark_node;
10438 
10439   vec = make_tree_vector ();
10440   res = finish_call_expr (member, &vec,
10441 			  /*disallow_virtual=*/false,
10442 			  /*koenig_p=*/false,
10443 			  tf_warning_or_error);
10444   release_tree_vector (vec);
10445   return res;
10446 }
10447 
10448 /* Parse an iteration-statement.
10449 
10450    iteration-statement:
10451      while ( condition ) statement
10452      do statement while ( expression ) ;
10453      for ( for-init-statement condition [opt] ; expression [opt] )
10454        statement
10455 
10456    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
10457 
10458 static tree
cp_parser_iteration_statement(cp_parser * parser,bool ivdep)10459 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10460 {
10461   cp_token *token;
10462   enum rid keyword;
10463   tree statement;
10464   unsigned char in_statement;
10465 
10466   /* Peek at the next token.  */
10467   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10468   if (!token)
10469     return error_mark_node;
10470 
10471   /* Remember whether or not we are already within an iteration
10472      statement.  */
10473   in_statement = parser->in_statement;
10474 
10475   /* See what kind of keyword it is.  */
10476   keyword = token->keyword;
10477   switch (keyword)
10478     {
10479     case RID_WHILE:
10480       {
10481 	tree condition;
10482 
10483 	/* Begin the while-statement.  */
10484 	statement = begin_while_stmt ();
10485 	/* Look for the `('.  */
10486 	cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10487 	/* Parse the condition.  */
10488 	condition = cp_parser_condition (parser);
10489 	finish_while_stmt_cond (condition, statement, ivdep);
10490 	/* Look for the `)'.  */
10491 	cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10492 	/* Parse the dependent statement.  */
10493 	parser->in_statement = IN_ITERATION_STMT;
10494 	cp_parser_already_scoped_statement (parser);
10495 	parser->in_statement = in_statement;
10496 	/* We're done with the while-statement.  */
10497 	finish_while_stmt (statement);
10498       }
10499       break;
10500 
10501     case RID_DO:
10502       {
10503 	tree expression;
10504 
10505 	/* Begin the do-statement.  */
10506 	statement = begin_do_stmt ();
10507 	/* Parse the body of the do-statement.  */
10508 	parser->in_statement = IN_ITERATION_STMT;
10509 	cp_parser_implicitly_scoped_statement (parser, NULL);
10510 	parser->in_statement = in_statement;
10511 	finish_do_body (statement);
10512 	/* Look for the `while' keyword.  */
10513 	cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10514 	/* Look for the `('.  */
10515 	cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10516 	/* Parse the expression.  */
10517 	expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10518 	/* We're done with the do-statement.  */
10519 	finish_do_stmt (expression, statement, ivdep);
10520 	/* Look for the `)'.  */
10521 	cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10522 	/* Look for the `;'.  */
10523 	cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10524       }
10525       break;
10526 
10527     case RID_FOR:
10528       {
10529 	/* Look for the `('.  */
10530 	cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10531 
10532 	statement = cp_parser_for (parser, ivdep);
10533 
10534 	/* Look for the `)'.  */
10535 	cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10536 
10537 	/* Parse the body of the for-statement.  */
10538 	parser->in_statement = IN_ITERATION_STMT;
10539 	cp_parser_already_scoped_statement (parser);
10540 	parser->in_statement = in_statement;
10541 
10542 	/* We're done with the for-statement.  */
10543 	finish_for_stmt (statement);
10544       }
10545       break;
10546 
10547     default:
10548       cp_parser_error (parser, "expected iteration-statement");
10549       statement = error_mark_node;
10550       break;
10551     }
10552 
10553   return statement;
10554 }
10555 
10556 /* Parse a for-init-statement or the declarator of a range-based-for.
10557    Returns true if a range-based-for declaration is seen.
10558 
10559    for-init-statement:
10560      expression-statement
10561      simple-declaration  */
10562 
10563 static bool
cp_parser_for_init_statement(cp_parser * parser,tree * decl)10564 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10565 {
10566   /* If the next token is a `;', then we have an empty
10567      expression-statement.  Grammatically, this is also a
10568      simple-declaration, but an invalid one, because it does not
10569      declare anything.  Therefore, if we did not handle this case
10570      specially, we would issue an error message about an invalid
10571      declaration.  */
10572   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10573     {
10574       bool is_range_for = false;
10575       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10576 
10577       parser->colon_corrects_to_scope_p = false;
10578 
10579       /* We're going to speculatively look for a declaration, falling back
10580 	 to an expression, if necessary.  */
10581       cp_parser_parse_tentatively (parser);
10582       /* Parse the declaration.  */
10583       cp_parser_simple_declaration (parser,
10584 				    /*function_definition_allowed_p=*/false,
10585 				    decl);
10586       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10587       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10588 	{
10589 	  /* It is a range-for, consume the ':' */
10590 	  cp_lexer_consume_token (parser->lexer);
10591 	  is_range_for = true;
10592 	  if (cxx_dialect < cxx11)
10593 	    {
10594 	      error_at (cp_lexer_peek_token (parser->lexer)->location,
10595 			"range-based %<for%> loops are not allowed "
10596 			"in C++98 mode");
10597 	      *decl = error_mark_node;
10598 	    }
10599 	}
10600       else
10601 	  /* The ';' is not consumed yet because we told
10602 	     cp_parser_simple_declaration not to.  */
10603 	  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10604 
10605       if (cp_parser_parse_definitely (parser))
10606 	return is_range_for;
10607       /* If the tentative parse failed, then we shall need to look for an
10608 	 expression-statement.  */
10609     }
10610   /* If we are here, it is an expression-statement.  */
10611   cp_parser_expression_statement (parser, NULL_TREE);
10612   return false;
10613 }
10614 
10615 /* Parse a jump-statement.
10616 
10617    jump-statement:
10618      break ;
10619      continue ;
10620      return expression [opt] ;
10621      return braced-init-list ;
10622      goto identifier ;
10623 
10624    GNU extension:
10625 
10626    jump-statement:
10627      goto * expression ;
10628 
10629    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
10630 
10631 static tree
cp_parser_jump_statement(cp_parser * parser)10632 cp_parser_jump_statement (cp_parser* parser)
10633 {
10634   tree statement = error_mark_node;
10635   cp_token *token;
10636   enum rid keyword;
10637   unsigned char in_statement;
10638 
10639   /* Peek at the next token.  */
10640   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10641   if (!token)
10642     return error_mark_node;
10643 
10644   /* See what kind of keyword it is.  */
10645   keyword = token->keyword;
10646   switch (keyword)
10647     {
10648     case RID_BREAK:
10649       in_statement = parser->in_statement & ~IN_IF_STMT;
10650       switch (in_statement)
10651 	{
10652 	case 0:
10653 	  error_at (token->location, "break statement not within loop or switch");
10654 	  break;
10655 	default:
10656 	  gcc_assert ((in_statement & IN_SWITCH_STMT)
10657 		      || in_statement == IN_ITERATION_STMT);
10658 	  statement = finish_break_stmt ();
10659 	  if (in_statement == IN_ITERATION_STMT)
10660 	    break_maybe_infinite_loop ();
10661 	  break;
10662 	case IN_OMP_BLOCK:
10663 	  error_at (token->location, "invalid exit from OpenMP structured block");
10664 	  break;
10665 	case IN_OMP_FOR:
10666 	  error_at (token->location, "break statement used with OpenMP for loop");
10667 	  break;
10668 	case IN_CILK_SIMD_FOR:
10669 	  error_at (token->location, "break statement used with Cilk Plus for loop");
10670 	  break;
10671 	}
10672       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10673       break;
10674 
10675     case RID_CONTINUE:
10676       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10677 	{
10678 	case 0:
10679 	  error_at (token->location, "continue statement not within a loop");
10680 	  break;
10681 	case IN_CILK_SIMD_FOR:
10682 	  error_at (token->location,
10683 		    "continue statement within %<#pragma simd%> loop body");
10684 	  /* Fall through.  */
10685 	case IN_ITERATION_STMT:
10686 	case IN_OMP_FOR:
10687 	  statement = finish_continue_stmt ();
10688 	  break;
10689 	case IN_OMP_BLOCK:
10690 	  error_at (token->location, "invalid exit from OpenMP structured block");
10691 	  break;
10692 	default:
10693 	  gcc_unreachable ();
10694 	}
10695       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10696       break;
10697 
10698     case RID_RETURN:
10699       {
10700 	tree expr;
10701 	bool expr_non_constant_p;
10702 
10703 	if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10704 	  {
10705 	    cp_lexer_set_source_position (parser->lexer);
10706 	    maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10707 	    expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10708 	  }
10709 	else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10710 	  expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10711 	else
10712 	  /* If the next token is a `;', then there is no
10713 	     expression.  */
10714 	  expr = NULL_TREE;
10715 	/* Build the return-statement.  */
10716 	statement = finish_return_stmt (expr);
10717 	/* Look for the final `;'.  */
10718 	cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10719       }
10720       break;
10721 
10722     case RID_GOTO:
10723       /* Create the goto-statement.  */
10724       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10725 	{
10726 	  /* Issue a warning about this use of a GNU extension.  */
10727 	  pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10728 	  /* Consume the '*' token.  */
10729 	  cp_lexer_consume_token (parser->lexer);
10730 	  /* Parse the dependent expression.  */
10731 	  finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10732 	}
10733       else
10734 	finish_goto_stmt (cp_parser_identifier (parser));
10735       /* Look for the final `;'.  */
10736       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10737       break;
10738 
10739     default:
10740       cp_parser_error (parser, "expected jump-statement");
10741       break;
10742     }
10743 
10744   return statement;
10745 }
10746 
10747 /* Parse a declaration-statement.
10748 
10749    declaration-statement:
10750      block-declaration  */
10751 
10752 static void
cp_parser_declaration_statement(cp_parser * parser)10753 cp_parser_declaration_statement (cp_parser* parser)
10754 {
10755   void *p;
10756 
10757   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
10758   p = obstack_alloc (&declarator_obstack, 0);
10759 
10760  /* Parse the block-declaration.  */
10761   cp_parser_block_declaration (parser, /*statement_p=*/true);
10762 
10763   /* Free any declarators allocated.  */
10764   obstack_free (&declarator_obstack, p);
10765 }
10766 
10767 /* Some dependent statements (like `if (cond) statement'), are
10768    implicitly in their own scope.  In other words, if the statement is
10769    a single statement (as opposed to a compound-statement), it is
10770    none-the-less treated as if it were enclosed in braces.  Any
10771    declarations appearing in the dependent statement are out of scope
10772    after control passes that point.  This function parses a statement,
10773    but ensures that is in its own scope, even if it is not a
10774    compound-statement.
10775 
10776    If IF_P is not NULL, *IF_P is set to indicate whether the statement
10777    is a (possibly labeled) if statement which is not enclosed in
10778    braces and has an else clause.  This is used to implement
10779    -Wparentheses.
10780 
10781    Returns the new statement.  */
10782 
10783 static tree
cp_parser_implicitly_scoped_statement(cp_parser * parser,bool * if_p)10784 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10785 {
10786   tree statement;
10787 
10788   if (if_p != NULL)
10789     *if_p = false;
10790 
10791   /* Mark if () ; with a special NOP_EXPR.  */
10792   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10793     {
10794       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10795       cp_lexer_consume_token (parser->lexer);
10796       statement = add_stmt (build_empty_stmt (loc));
10797     }
10798   /* if a compound is opened, we simply parse the statement directly.  */
10799   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10800     statement = cp_parser_compound_statement (parser, NULL, false, false);
10801   /* If the token is not a `{', then we must take special action.  */
10802   else
10803     {
10804       /* Create a compound-statement.  */
10805       statement = begin_compound_stmt (0);
10806       /* Parse the dependent-statement.  */
10807       cp_parser_statement (parser, NULL_TREE, false, if_p);
10808       /* Finish the dummy compound-statement.  */
10809       finish_compound_stmt (statement);
10810     }
10811 
10812   /* Return the statement.  */
10813   return statement;
10814 }
10815 
10816 /* For some dependent statements (like `while (cond) statement'), we
10817    have already created a scope.  Therefore, even if the dependent
10818    statement is a compound-statement, we do not want to create another
10819    scope.  */
10820 
10821 static void
cp_parser_already_scoped_statement(cp_parser * parser)10822 cp_parser_already_scoped_statement (cp_parser* parser)
10823 {
10824   /* If the token is a `{', then we must take special action.  */
10825   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10826     cp_parser_statement (parser, NULL_TREE, false, NULL);
10827   else
10828     {
10829       /* Avoid calling cp_parser_compound_statement, so that we
10830 	 don't create a new scope.  Do everything else by hand.  */
10831       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10832       /* If the next keyword is `__label__' we have a label declaration.  */
10833       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10834 	cp_parser_label_declaration (parser);
10835       /* Parse an (optional) statement-seq.  */
10836       cp_parser_statement_seq_opt (parser, NULL_TREE);
10837       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10838     }
10839 }
10840 
10841 /* Declarations [gram.dcl.dcl] */
10842 
10843 /* Parse an optional declaration-sequence.
10844 
10845    declaration-seq:
10846      declaration
10847      declaration-seq declaration  */
10848 
10849 static void
cp_parser_declaration_seq_opt(cp_parser * parser)10850 cp_parser_declaration_seq_opt (cp_parser* parser)
10851 {
10852   while (true)
10853     {
10854       cp_token *token;
10855 
10856       token = cp_lexer_peek_token (parser->lexer);
10857 
10858       if (token->type == CPP_CLOSE_BRACE
10859 	  || token->type == CPP_EOF
10860 	  || token->type == CPP_PRAGMA_EOL)
10861 	break;
10862 
10863       if (token->type == CPP_SEMICOLON)
10864 	{
10865 	  /* A declaration consisting of a single semicolon is
10866 	     invalid.  Allow it unless we're being pedantic.  */
10867 	  cp_lexer_consume_token (parser->lexer);
10868 	  if (!in_system_header_at (input_location))
10869 	    pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10870 	  continue;
10871 	}
10872 
10873       /* If we're entering or exiting a region that's implicitly
10874 	 extern "C", modify the lang context appropriately.  */
10875       if (!parser->implicit_extern_c && token->implicit_extern_c)
10876 	{
10877 	  push_lang_context (lang_name_c);
10878 	  parser->implicit_extern_c = true;
10879 	}
10880       else if (parser->implicit_extern_c && !token->implicit_extern_c)
10881 	{
10882 	  pop_lang_context ();
10883 	  parser->implicit_extern_c = false;
10884 	}
10885 
10886       if (token->type == CPP_PRAGMA)
10887 	{
10888 	  /* A top-level declaration can consist solely of a #pragma.
10889 	     A nested declaration cannot, so this is done here and not
10890 	     in cp_parser_declaration.  (A #pragma at block scope is
10891 	     handled in cp_parser_statement.)  */
10892 	  cp_parser_pragma (parser, pragma_external);
10893 	  continue;
10894 	}
10895 
10896       /* Parse the declaration itself.  */
10897       cp_parser_declaration (parser);
10898     }
10899 }
10900 
10901 /* Parse a declaration.
10902 
10903    declaration:
10904      block-declaration
10905      function-definition
10906      template-declaration
10907      explicit-instantiation
10908      explicit-specialization
10909      linkage-specification
10910      namespace-definition
10911 
10912    GNU extension:
10913 
10914    declaration:
10915       __extension__ declaration */
10916 
10917 static void
cp_parser_declaration(cp_parser * parser)10918 cp_parser_declaration (cp_parser* parser)
10919 {
10920   cp_token token1;
10921   cp_token token2;
10922   int saved_pedantic;
10923   void *p;
10924   tree attributes = NULL_TREE;
10925 
10926   /* Check for the `__extension__' keyword.  */
10927   if (cp_parser_extension_opt (parser, &saved_pedantic))
10928     {
10929       /* Parse the qualified declaration.  */
10930       cp_parser_declaration (parser);
10931       /* Restore the PEDANTIC flag.  */
10932       pedantic = saved_pedantic;
10933 
10934       return;
10935     }
10936 
10937   /* Try to figure out what kind of declaration is present.  */
10938   token1 = *cp_lexer_peek_token (parser->lexer);
10939 
10940   if (token1.type != CPP_EOF)
10941     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10942   else
10943     {
10944       token2.type = CPP_EOF;
10945       token2.keyword = RID_MAX;
10946     }
10947 
10948   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
10949   p = obstack_alloc (&declarator_obstack, 0);
10950 
10951   /* If the next token is `extern' and the following token is a string
10952      literal, then we have a linkage specification.  */
10953   if (token1.keyword == RID_EXTERN
10954       && cp_parser_is_pure_string_literal (&token2))
10955     cp_parser_linkage_specification (parser);
10956   /* If the next token is `template', then we have either a template
10957      declaration, an explicit instantiation, or an explicit
10958      specialization.  */
10959   else if (token1.keyword == RID_TEMPLATE)
10960     {
10961       /* `template <>' indicates a template specialization.  */
10962       if (token2.type == CPP_LESS
10963 	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10964 	cp_parser_explicit_specialization (parser);
10965       /* `template <' indicates a template declaration.  */
10966       else if (token2.type == CPP_LESS)
10967 	cp_parser_template_declaration (parser, /*member_p=*/false);
10968       /* Anything else must be an explicit instantiation.  */
10969       else
10970 	cp_parser_explicit_instantiation (parser);
10971     }
10972   /* If the next token is `export', then we have a template
10973      declaration.  */
10974   else if (token1.keyword == RID_EXPORT)
10975     cp_parser_template_declaration (parser, /*member_p=*/false);
10976   /* If the next token is `extern', 'static' or 'inline' and the one
10977      after that is `template', we have a GNU extended explicit
10978      instantiation directive.  */
10979   else if (cp_parser_allow_gnu_extensions_p (parser)
10980 	   && (token1.keyword == RID_EXTERN
10981 	       || token1.keyword == RID_STATIC
10982 	       || token1.keyword == RID_INLINE)
10983 	   && token2.keyword == RID_TEMPLATE)
10984     cp_parser_explicit_instantiation (parser);
10985   /* If the next token is `namespace', check for a named or unnamed
10986      namespace definition.  */
10987   else if (token1.keyword == RID_NAMESPACE
10988 	   && (/* A named namespace definition.  */
10989 	       (token2.type == CPP_NAME
10990 		&& (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10991 		    != CPP_EQ))
10992 	       /* An unnamed namespace definition.  */
10993 	       || token2.type == CPP_OPEN_BRACE
10994 	       || token2.keyword == RID_ATTRIBUTE))
10995     cp_parser_namespace_definition (parser);
10996   /* An inline (associated) namespace definition.  */
10997   else if (token1.keyword == RID_INLINE
10998 	   && token2.keyword == RID_NAMESPACE)
10999     cp_parser_namespace_definition (parser);
11000   /* Objective-C++ declaration/definition.  */
11001   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11002     cp_parser_objc_declaration (parser, NULL_TREE);
11003   else if (c_dialect_objc ()
11004 	   && token1.keyword == RID_ATTRIBUTE
11005 	   && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11006     cp_parser_objc_declaration (parser, attributes);
11007   /* We must have either a block declaration or a function
11008      definition.  */
11009   else
11010     /* Try to parse a block-declaration, or a function-definition.  */
11011     cp_parser_block_declaration (parser, /*statement_p=*/false);
11012 
11013   /* Free any declarators allocated.  */
11014   obstack_free (&declarator_obstack, p);
11015 }
11016 
11017 /* Parse a block-declaration.
11018 
11019    block-declaration:
11020      simple-declaration
11021      asm-definition
11022      namespace-alias-definition
11023      using-declaration
11024      using-directive
11025 
11026    GNU Extension:
11027 
11028    block-declaration:
11029      __extension__ block-declaration
11030 
11031    C++0x Extension:
11032 
11033    block-declaration:
11034      static_assert-declaration
11035 
11036    If STATEMENT_P is TRUE, then this block-declaration is occurring as
11037    part of a declaration-statement.  */
11038 
11039 static void
cp_parser_block_declaration(cp_parser * parser,bool statement_p)11040 cp_parser_block_declaration (cp_parser *parser,
11041 			     bool      statement_p)
11042 {
11043   cp_token *token1;
11044   int saved_pedantic;
11045 
11046   /* Check for the `__extension__' keyword.  */
11047   if (cp_parser_extension_opt (parser, &saved_pedantic))
11048     {
11049       /* Parse the qualified declaration.  */
11050       cp_parser_block_declaration (parser, statement_p);
11051       /* Restore the PEDANTIC flag.  */
11052       pedantic = saved_pedantic;
11053 
11054       return;
11055     }
11056 
11057   /* Peek at the next token to figure out which kind of declaration is
11058      present.  */
11059   token1 = cp_lexer_peek_token (parser->lexer);
11060 
11061   /* If the next keyword is `asm', we have an asm-definition.  */
11062   if (token1->keyword == RID_ASM)
11063     {
11064       if (statement_p)
11065 	cp_parser_commit_to_tentative_parse (parser);
11066       cp_parser_asm_definition (parser);
11067     }
11068   /* If the next keyword is `namespace', we have a
11069      namespace-alias-definition.  */
11070   else if (token1->keyword == RID_NAMESPACE)
11071     cp_parser_namespace_alias_definition (parser);
11072   /* If the next keyword is `using', we have a
11073      using-declaration, a using-directive, or an alias-declaration.  */
11074   else if (token1->keyword == RID_USING)
11075     {
11076       cp_token *token2;
11077 
11078       if (statement_p)
11079 	cp_parser_commit_to_tentative_parse (parser);
11080       /* If the token after `using' is `namespace', then we have a
11081 	 using-directive.  */
11082       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11083       if (token2->keyword == RID_NAMESPACE)
11084 	cp_parser_using_directive (parser);
11085       /* If the second token after 'using' is '=', then we have an
11086 	 alias-declaration.  */
11087       else if (cxx_dialect >= cxx11
11088 	       && token2->type == CPP_NAME
11089 	       && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11090 		   || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11091 	cp_parser_alias_declaration (parser);
11092       /* Otherwise, it's a using-declaration.  */
11093       else
11094 	cp_parser_using_declaration (parser,
11095 				     /*access_declaration_p=*/false);
11096     }
11097   /* If the next keyword is `__label__' we have a misplaced label
11098      declaration.  */
11099   else if (token1->keyword == RID_LABEL)
11100     {
11101       cp_lexer_consume_token (parser->lexer);
11102       error_at (token1->location, "%<__label__%> not at the beginning of a block");
11103       cp_parser_skip_to_end_of_statement (parser);
11104       /* If the next token is now a `;', consume it.  */
11105       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11106 	cp_lexer_consume_token (parser->lexer);
11107     }
11108   /* If the next token is `static_assert' we have a static assertion.  */
11109   else if (token1->keyword == RID_STATIC_ASSERT)
11110     cp_parser_static_assert (parser, /*member_p=*/false);
11111   /* Anything else must be a simple-declaration.  */
11112   else
11113     cp_parser_simple_declaration (parser, !statement_p,
11114 				  /*maybe_range_for_decl*/NULL);
11115 }
11116 
11117 /* Parse a simple-declaration.
11118 
11119    simple-declaration:
11120      decl-specifier-seq [opt] init-declarator-list [opt] ;
11121 
11122    init-declarator-list:
11123      init-declarator
11124      init-declarator-list , init-declarator
11125 
11126    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11127    function-definition as a simple-declaration.
11128 
11129    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11130    parsed declaration if it is an uninitialized single declarator not followed
11131    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11132    if present, will not be consumed.  */
11133 
11134 static void
cp_parser_simple_declaration(cp_parser * parser,bool function_definition_allowed_p,tree * maybe_range_for_decl)11135 cp_parser_simple_declaration (cp_parser* parser,
11136 			      bool function_definition_allowed_p,
11137 			      tree *maybe_range_for_decl)
11138 {
11139   cp_decl_specifier_seq decl_specifiers;
11140   int declares_class_or_enum;
11141   bool saw_declarator;
11142 
11143   if (maybe_range_for_decl)
11144     *maybe_range_for_decl = NULL_TREE;
11145 
11146   /* Defer access checks until we know what is being declared; the
11147      checks for names appearing in the decl-specifier-seq should be
11148      done as if we were in the scope of the thing being declared.  */
11149   push_deferring_access_checks (dk_deferred);
11150 
11151   /* Parse the decl-specifier-seq.  We have to keep track of whether
11152      or not the decl-specifier-seq declares a named class or
11153      enumeration type, since that is the only case in which the
11154      init-declarator-list is allowed to be empty.
11155 
11156      [dcl.dcl]
11157 
11158      In a simple-declaration, the optional init-declarator-list can be
11159      omitted only when declaring a class or enumeration, that is when
11160      the decl-specifier-seq contains either a class-specifier, an
11161      elaborated-type-specifier, or an enum-specifier.  */
11162   cp_parser_decl_specifier_seq (parser,
11163 				CP_PARSER_FLAGS_OPTIONAL,
11164 				&decl_specifiers,
11165 				&declares_class_or_enum);
11166   /* We no longer need to defer access checks.  */
11167   stop_deferring_access_checks ();
11168 
11169   /* In a block scope, a valid declaration must always have a
11170      decl-specifier-seq.  By not trying to parse declarators, we can
11171      resolve the declaration/expression ambiguity more quickly.  */
11172   if (!function_definition_allowed_p
11173       && !decl_specifiers.any_specifiers_p)
11174     {
11175       cp_parser_error (parser, "expected declaration");
11176       goto done;
11177     }
11178 
11179   /* If the next two tokens are both identifiers, the code is
11180      erroneous. The usual cause of this situation is code like:
11181 
11182        T t;
11183 
11184      where "T" should name a type -- but does not.  */
11185   if (!decl_specifiers.any_type_specifiers_p
11186       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11187     {
11188       /* If parsing tentatively, we should commit; we really are
11189 	 looking at a declaration.  */
11190       cp_parser_commit_to_tentative_parse (parser);
11191       /* Give up.  */
11192       goto done;
11193     }
11194 
11195   /* If we have seen at least one decl-specifier, and the next token
11196      is not a parenthesis, then we must be looking at a declaration.
11197      (After "int (" we might be looking at a functional cast.)  */
11198   if (decl_specifiers.any_specifiers_p
11199       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11200       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11201       && !cp_parser_error_occurred (parser))
11202     cp_parser_commit_to_tentative_parse (parser);
11203 
11204   /* Keep going until we hit the `;' at the end of the simple
11205      declaration.  */
11206   saw_declarator = false;
11207   while (cp_lexer_next_token_is_not (parser->lexer,
11208 				     CPP_SEMICOLON))
11209     {
11210       cp_token *token;
11211       bool function_definition_p;
11212       tree decl;
11213 
11214       if (saw_declarator)
11215 	{
11216 	  /* If we are processing next declarator, coma is expected */
11217 	  token = cp_lexer_peek_token (parser->lexer);
11218 	  gcc_assert (token->type == CPP_COMMA);
11219 	  cp_lexer_consume_token (parser->lexer);
11220 	  if (maybe_range_for_decl)
11221 	    *maybe_range_for_decl = error_mark_node;
11222 	}
11223       else
11224 	saw_declarator = true;
11225 
11226       /* Parse the init-declarator.  */
11227       decl = cp_parser_init_declarator (parser, &decl_specifiers,
11228 					/*checks=*/NULL,
11229 					function_definition_allowed_p,
11230 					/*member_p=*/false,
11231 					declares_class_or_enum,
11232 					&function_definition_p,
11233 					maybe_range_for_decl);
11234       /* If an error occurred while parsing tentatively, exit quickly.
11235 	 (That usually happens when in the body of a function; each
11236 	 statement is treated as a declaration-statement until proven
11237 	 otherwise.)  */
11238       if (cp_parser_error_occurred (parser))
11239 	goto done;
11240       /* Handle function definitions specially.  */
11241       if (function_definition_p)
11242 	{
11243 	  /* If the next token is a `,', then we are probably
11244 	     processing something like:
11245 
11246 	       void f() {}, *p;
11247 
11248 	     which is erroneous.  */
11249 	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11250 	    {
11251 	      cp_token *token = cp_lexer_peek_token (parser->lexer);
11252 	      error_at (token->location,
11253 			"mixing"
11254 			" declarations and function-definitions is forbidden");
11255 	    }
11256 	  /* Otherwise, we're done with the list of declarators.  */
11257 	  else
11258 	    {
11259 	      pop_deferring_access_checks ();
11260 	      return;
11261 	    }
11262 	}
11263       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11264 	*maybe_range_for_decl = decl;
11265       /* The next token should be either a `,' or a `;'.  */
11266       token = cp_lexer_peek_token (parser->lexer);
11267       /* If it's a `,', there are more declarators to come.  */
11268       if (token->type == CPP_COMMA)
11269 	/* will be consumed next time around */;
11270       /* If it's a `;', we are done.  */
11271       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11272 	break;
11273       /* Anything else is an error.  */
11274       else
11275 	{
11276 	  /* If we have already issued an error message we don't need
11277 	     to issue another one.  */
11278 	  if (decl != error_mark_node
11279 	      || cp_parser_uncommitted_to_tentative_parse_p (parser))
11280 	    cp_parser_error (parser, "expected %<,%> or %<;%>");
11281 	  /* Skip tokens until we reach the end of the statement.  */
11282 	  cp_parser_skip_to_end_of_statement (parser);
11283 	  /* If the next token is now a `;', consume it.  */
11284 	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11285 	    cp_lexer_consume_token (parser->lexer);
11286 	  goto done;
11287 	}
11288       /* After the first time around, a function-definition is not
11289 	 allowed -- even if it was OK at first.  For example:
11290 
11291 	   int i, f() {}
11292 
11293 	 is not valid.  */
11294       function_definition_allowed_p = false;
11295     }
11296 
11297   /* Issue an error message if no declarators are present, and the
11298      decl-specifier-seq does not itself declare a class or
11299      enumeration: [dcl.dcl]/3.  */
11300   if (!saw_declarator)
11301     {
11302       if (cp_parser_declares_only_class_p (parser))
11303 	{
11304 	  if (!declares_class_or_enum
11305 	      && decl_specifiers.type
11306 	      && OVERLOAD_TYPE_P (decl_specifiers.type))
11307 	    /* Ensure an error is issued anyway when finish_decltype_type,
11308 	       called via cp_parser_decl_specifier_seq, returns a class or
11309 	       an enumeration (c++/51786).  */
11310 	    decl_specifiers.type = NULL_TREE;
11311 	  shadow_tag (&decl_specifiers);
11312 	}
11313       /* Perform any deferred access checks.  */
11314       perform_deferred_access_checks (tf_warning_or_error);
11315     }
11316 
11317   /* Consume the `;'.  */
11318   if (!maybe_range_for_decl)
11319       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11320 
11321  done:
11322   pop_deferring_access_checks ();
11323 }
11324 
11325 /* Parse a decl-specifier-seq.
11326 
11327    decl-specifier-seq:
11328      decl-specifier-seq [opt] decl-specifier
11329      decl-specifier attribute-specifier-seq [opt] (C++11)
11330 
11331    decl-specifier:
11332      storage-class-specifier
11333      type-specifier
11334      function-specifier
11335      friend
11336      typedef
11337 
11338    GNU Extension:
11339 
11340    decl-specifier:
11341      attributes
11342 
11343    Set *DECL_SPECS to a representation of the decl-specifier-seq.
11344 
11345    The parser flags FLAGS is used to control type-specifier parsing.
11346 
11347    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11348    flags:
11349 
11350      1: one of the decl-specifiers is an elaborated-type-specifier
11351 	(i.e., a type declaration)
11352      2: one of the decl-specifiers is an enum-specifier or a
11353 	class-specifier (i.e., a type definition)
11354 
11355    */
11356 
11357 static void
cp_parser_decl_specifier_seq(cp_parser * parser,cp_parser_flags flags,cp_decl_specifier_seq * decl_specs,int * declares_class_or_enum)11358 cp_parser_decl_specifier_seq (cp_parser* parser,
11359 			      cp_parser_flags flags,
11360 			      cp_decl_specifier_seq *decl_specs,
11361 			      int* declares_class_or_enum)
11362 {
11363   bool constructor_possible_p = !parser->in_declarator_p;
11364   bool found_decl_spec = false;
11365   cp_token *start_token = NULL;
11366   cp_decl_spec ds;
11367 
11368   /* Clear DECL_SPECS.  */
11369   clear_decl_specs (decl_specs);
11370 
11371   /* Assume no class or enumeration type is declared.  */
11372   *declares_class_or_enum = 0;
11373 
11374   /* Keep reading specifiers until there are no more to read.  */
11375   while (true)
11376     {
11377       bool constructor_p;
11378       cp_token *token;
11379       ds = ds_last;
11380 
11381       /* Peek at the next token.  */
11382       token = cp_lexer_peek_token (parser->lexer);
11383 
11384       /* Save the first token of the decl spec list for error
11385          reporting.  */
11386       if (!start_token)
11387 	start_token = token;
11388       /* Handle attributes.  */
11389       if (cp_next_tokens_can_be_attribute_p (parser))
11390 	{
11391 	  /* Parse the attributes.  */
11392 	  tree attrs = cp_parser_attributes_opt (parser);
11393 
11394 	  /* In a sequence of declaration specifiers, c++11 attributes
11395 	     appertain to the type that precede them. In that case
11396 	     [dcl.spec]/1 says:
11397 
11398 	         The attribute-specifier-seq affects the type only for
11399 		 the declaration it appears in, not other declarations
11400 		 involving the same type.
11401 
11402              But for now let's force the user to position the
11403              attribute either at the beginning of the declaration or
11404              after the declarator-id, which would clearly mean that it
11405              applies to the declarator.  */
11406 	  if (cxx11_attribute_p (attrs))
11407 	    {
11408 	      if (!found_decl_spec)
11409 		/* The c++11 attribute is at the beginning of the
11410 		   declaration.  It appertains to the entity being
11411 		   declared.  */;
11412 	      else
11413 		{
11414 		  if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11415 		    {
11416 		      /*  This is an attribute following a
11417 			  class-specifier.  */
11418 		      if (decl_specs->type_definition_p)
11419 			warn_misplaced_attr_for_class_type (token->location,
11420 							    decl_specs->type);
11421 		      attrs = NULL_TREE;
11422 		    }
11423 		  else
11424 		    {
11425 		      decl_specs->std_attributes
11426 			= chainon (decl_specs->std_attributes,
11427 				   attrs);
11428 		      if (decl_specs->locations[ds_std_attribute] == 0)
11429 			decl_specs->locations[ds_std_attribute] = token->location;
11430 		    }
11431 		  continue;
11432 		}
11433 	    }
11434 
11435 	    decl_specs->attributes
11436 	      = chainon (decl_specs->attributes,
11437 			 attrs);
11438 	  if (decl_specs->locations[ds_attribute] == 0)
11439 	    decl_specs->locations[ds_attribute] = token->location;
11440 	  continue;
11441 	}
11442       /* Assume we will find a decl-specifier keyword.  */
11443       found_decl_spec = true;
11444       /* If the next token is an appropriate keyword, we can simply
11445 	 add it to the list.  */
11446       switch (token->keyword)
11447 	{
11448 	  /* decl-specifier:
11449 	       friend
11450                constexpr */
11451 	case RID_FRIEND:
11452 	  if (!at_class_scope_p ())
11453 	    {
11454 	      error_at (token->location, "%<friend%> used outside of class");
11455 	      cp_lexer_purge_token (parser->lexer);
11456 	    }
11457 	  else
11458 	    {
11459 	      ds = ds_friend;
11460 	      /* Consume the token.  */
11461 	      cp_lexer_consume_token (parser->lexer);
11462 	    }
11463 	  break;
11464 
11465         case RID_CONSTEXPR:
11466 	  ds = ds_constexpr;
11467           cp_lexer_consume_token (parser->lexer);
11468           break;
11469 
11470 	  /* function-specifier:
11471 	       inline
11472 	       virtual
11473 	       explicit  */
11474 	case RID_INLINE:
11475 	case RID_VIRTUAL:
11476 	case RID_EXPLICIT:
11477 	  cp_parser_function_specifier_opt (parser, decl_specs);
11478 	  break;
11479 
11480 	  /* decl-specifier:
11481 	       typedef  */
11482 	case RID_TYPEDEF:
11483 	  ds = ds_typedef;
11484 	  /* Consume the token.  */
11485 	  cp_lexer_consume_token (parser->lexer);
11486 	  /* A constructor declarator cannot appear in a typedef.  */
11487 	  constructor_possible_p = false;
11488 	  /* The "typedef" keyword can only occur in a declaration; we
11489 	     may as well commit at this point.  */
11490 	  cp_parser_commit_to_tentative_parse (parser);
11491 
11492           if (decl_specs->storage_class != sc_none)
11493             decl_specs->conflicting_specifiers_p = true;
11494 	  break;
11495 
11496 	  /* storage-class-specifier:
11497 	       auto
11498 	       register
11499 	       static
11500 	       extern
11501 	       mutable
11502 
11503 	     GNU Extension:
11504 	       thread  */
11505 	case RID_AUTO:
11506           if (cxx_dialect == cxx98)
11507             {
11508 	      /* Consume the token.  */
11509 	      cp_lexer_consume_token (parser->lexer);
11510 
11511               /* Complain about `auto' as a storage specifier, if
11512                  we're complaining about C++0x compatibility.  */
11513               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11514 			  " changes meaning in C++11; please remove it");
11515 
11516               /* Set the storage class anyway.  */
11517               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11518 					   token);
11519             }
11520           else
11521 	    /* C++0x auto type-specifier.  */
11522 	    found_decl_spec = false;
11523           break;
11524 
11525 	case RID_REGISTER:
11526 	case RID_STATIC:
11527 	case RID_EXTERN:
11528 	case RID_MUTABLE:
11529 	  /* Consume the token.  */
11530 	  cp_lexer_consume_token (parser->lexer);
11531           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11532 				       token);
11533 	  break;
11534 	case RID_THREAD:
11535 	  /* Consume the token.  */
11536 	  ds = ds_thread;
11537 	  cp_lexer_consume_token (parser->lexer);
11538 	  break;
11539 
11540 	default:
11541 	  /* We did not yet find a decl-specifier yet.  */
11542 	  found_decl_spec = false;
11543 	  break;
11544 	}
11545 
11546       if (found_decl_spec
11547 	  && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11548 	  && token->keyword != RID_CONSTEXPR)
11549 	error ("decl-specifier invalid in condition");
11550 
11551       if (ds != ds_last)
11552 	set_and_check_decl_spec_loc (decl_specs, ds, token);
11553 
11554       /* Constructors are a special case.  The `S' in `S()' is not a
11555 	 decl-specifier; it is the beginning of the declarator.  */
11556       constructor_p
11557 	= (!found_decl_spec
11558 	   && constructor_possible_p
11559 	   && (cp_parser_constructor_declarator_p
11560 	       (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11561 
11562       /* If we don't have a DECL_SPEC yet, then we must be looking at
11563 	 a type-specifier.  */
11564       if (!found_decl_spec && !constructor_p)
11565 	{
11566 	  int decl_spec_declares_class_or_enum;
11567 	  bool is_cv_qualifier;
11568 	  tree type_spec;
11569 
11570 	  type_spec
11571 	    = cp_parser_type_specifier (parser, flags,
11572 					decl_specs,
11573 					/*is_declaration=*/true,
11574 					&decl_spec_declares_class_or_enum,
11575 					&is_cv_qualifier);
11576 	  *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11577 
11578 	  /* If this type-specifier referenced a user-defined type
11579 	     (a typedef, class-name, etc.), then we can't allow any
11580 	     more such type-specifiers henceforth.
11581 
11582 	     [dcl.spec]
11583 
11584 	     The longest sequence of decl-specifiers that could
11585 	     possibly be a type name is taken as the
11586 	     decl-specifier-seq of a declaration.  The sequence shall
11587 	     be self-consistent as described below.
11588 
11589 	     [dcl.type]
11590 
11591 	     As a general rule, at most one type-specifier is allowed
11592 	     in the complete decl-specifier-seq of a declaration.  The
11593 	     only exceptions are the following:
11594 
11595 	     -- const or volatile can be combined with any other
11596 		type-specifier.
11597 
11598 	     -- signed or unsigned can be combined with char, long,
11599 		short, or int.
11600 
11601 	     -- ..
11602 
11603 	     Example:
11604 
11605 	       typedef char* Pc;
11606 	       void g (const int Pc);
11607 
11608 	     Here, Pc is *not* part of the decl-specifier seq; it's
11609 	     the declarator.  Therefore, once we see a type-specifier
11610 	     (other than a cv-qualifier), we forbid any additional
11611 	     user-defined types.  We *do* still allow things like `int
11612 	     int' to be considered a decl-specifier-seq, and issue the
11613 	     error message later.  */
11614 	  if (type_spec && !is_cv_qualifier)
11615 	    flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11616 	  /* A constructor declarator cannot follow a type-specifier.  */
11617 	  if (type_spec)
11618 	    {
11619 	      constructor_possible_p = false;
11620 	      found_decl_spec = true;
11621 	      if (!is_cv_qualifier)
11622 		decl_specs->any_type_specifiers_p = true;
11623 	    }
11624 	}
11625 
11626       /* If we still do not have a DECL_SPEC, then there are no more
11627 	 decl-specifiers.  */
11628       if (!found_decl_spec)
11629 	break;
11630 
11631       decl_specs->any_specifiers_p = true;
11632       /* After we see one decl-specifier, further decl-specifiers are
11633 	 always optional.  */
11634       flags |= CP_PARSER_FLAGS_OPTIONAL;
11635     }
11636 
11637   /* Don't allow a friend specifier with a class definition.  */
11638   if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11639       && (*declares_class_or_enum & 2))
11640     error_at (decl_specs->locations[ds_friend],
11641 	      "class definition may not be declared a friend");
11642 }
11643 
11644 /* Parse an (optional) storage-class-specifier.
11645 
11646    storage-class-specifier:
11647      auto
11648      register
11649      static
11650      extern
11651      mutable
11652 
11653    GNU Extension:
11654 
11655    storage-class-specifier:
11656      thread
11657 
11658    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
11659 
11660 static tree
cp_parser_storage_class_specifier_opt(cp_parser * parser)11661 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11662 {
11663   switch (cp_lexer_peek_token (parser->lexer)->keyword)
11664     {
11665     case RID_AUTO:
11666       if (cxx_dialect != cxx98)
11667         return NULL_TREE;
11668       /* Fall through for C++98.  */
11669 
11670     case RID_REGISTER:
11671     case RID_STATIC:
11672     case RID_EXTERN:
11673     case RID_MUTABLE:
11674     case RID_THREAD:
11675       /* Consume the token.  */
11676       return cp_lexer_consume_token (parser->lexer)->u.value;
11677 
11678     default:
11679       return NULL_TREE;
11680     }
11681 }
11682 
11683 /* Parse an (optional) function-specifier.
11684 
11685    function-specifier:
11686      inline
11687      virtual
11688      explicit
11689 
11690    Returns an IDENTIFIER_NODE corresponding to the keyword used.
11691    Updates DECL_SPECS, if it is non-NULL.  */
11692 
11693 static tree
cp_parser_function_specifier_opt(cp_parser * parser,cp_decl_specifier_seq * decl_specs)11694 cp_parser_function_specifier_opt (cp_parser* parser,
11695 				  cp_decl_specifier_seq *decl_specs)
11696 {
11697   cp_token *token = cp_lexer_peek_token (parser->lexer);
11698   switch (token->keyword)
11699     {
11700     case RID_INLINE:
11701       set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11702       break;
11703 
11704     case RID_VIRTUAL:
11705       /* 14.5.2.3 [temp.mem]
11706 
11707 	 A member function template shall not be virtual.  */
11708       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11709 	error_at (token->location, "templates may not be %<virtual%>");
11710       else
11711 	set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11712       break;
11713 
11714     case RID_EXPLICIT:
11715       set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11716       break;
11717 
11718     default:
11719       return NULL_TREE;
11720     }
11721 
11722   /* Consume the token.  */
11723   return cp_lexer_consume_token (parser->lexer)->u.value;
11724 }
11725 
11726 /* Parse a linkage-specification.
11727 
11728    linkage-specification:
11729      extern string-literal { declaration-seq [opt] }
11730      extern string-literal declaration  */
11731 
11732 static void
cp_parser_linkage_specification(cp_parser * parser)11733 cp_parser_linkage_specification (cp_parser* parser)
11734 {
11735   tree linkage;
11736 
11737   /* Look for the `extern' keyword.  */
11738   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11739 
11740   /* Look for the string-literal.  */
11741   linkage = cp_parser_string_literal (parser, false, false);
11742 
11743   /* Transform the literal into an identifier.  If the literal is a
11744      wide-character string, or contains embedded NULs, then we can't
11745      handle it as the user wants.  */
11746   if (strlen (TREE_STRING_POINTER (linkage))
11747       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11748     {
11749       cp_parser_error (parser, "invalid linkage-specification");
11750       /* Assume C++ linkage.  */
11751       linkage = lang_name_cplusplus;
11752     }
11753   else
11754     linkage = get_identifier (TREE_STRING_POINTER (linkage));
11755 
11756   /* We're now using the new linkage.  */
11757   push_lang_context (linkage);
11758 
11759   /* If the next token is a `{', then we're using the first
11760      production.  */
11761   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11762     {
11763       cp_ensure_no_omp_declare_simd (parser);
11764 
11765       /* Consume the `{' token.  */
11766       cp_lexer_consume_token (parser->lexer);
11767       /* Parse the declarations.  */
11768       cp_parser_declaration_seq_opt (parser);
11769       /* Look for the closing `}'.  */
11770       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11771     }
11772   /* Otherwise, there's just one declaration.  */
11773   else
11774     {
11775       bool saved_in_unbraced_linkage_specification_p;
11776 
11777       saved_in_unbraced_linkage_specification_p
11778 	= parser->in_unbraced_linkage_specification_p;
11779       parser->in_unbraced_linkage_specification_p = true;
11780       cp_parser_declaration (parser);
11781       parser->in_unbraced_linkage_specification_p
11782 	= saved_in_unbraced_linkage_specification_p;
11783     }
11784 
11785   /* We're done with the linkage-specification.  */
11786   pop_lang_context ();
11787 }
11788 
11789 /* Parse a static_assert-declaration.
11790 
11791    static_assert-declaration:
11792      static_assert ( constant-expression , string-literal ) ;
11793 
11794    If MEMBER_P, this static_assert is a class member.  */
11795 
11796 static void
cp_parser_static_assert(cp_parser * parser,bool member_p)11797 cp_parser_static_assert(cp_parser *parser, bool member_p)
11798 {
11799   tree condition;
11800   tree message;
11801   cp_token *token;
11802   location_t saved_loc;
11803   bool dummy;
11804 
11805   /* Peek at the `static_assert' token so we can keep track of exactly
11806      where the static assertion started.  */
11807   token = cp_lexer_peek_token (parser->lexer);
11808   saved_loc = token->location;
11809 
11810   /* Look for the `static_assert' keyword.  */
11811   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11812                                   RT_STATIC_ASSERT))
11813     return;
11814 
11815   /*  We know we are in a static assertion; commit to any tentative
11816       parse.  */
11817   if (cp_parser_parsing_tentatively (parser))
11818     cp_parser_commit_to_tentative_parse (parser);
11819 
11820   /* Parse the `(' starting the static assertion condition.  */
11821   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11822 
11823   /* Parse the constant-expression.  Allow a non-constant expression
11824      here in order to give better diagnostics in finish_static_assert.  */
11825   condition =
11826     cp_parser_constant_expression (parser,
11827                                    /*allow_non_constant_p=*/true,
11828                                    /*non_constant_p=*/&dummy);
11829 
11830   /* Parse the separating `,'.  */
11831   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11832 
11833   /* Parse the string-literal message.  */
11834   message = cp_parser_string_literal (parser,
11835                                       /*translate=*/false,
11836                                       /*wide_ok=*/true);
11837 
11838   /* A `)' completes the static assertion.  */
11839   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11840     cp_parser_skip_to_closing_parenthesis (parser,
11841                                            /*recovering=*/true,
11842                                            /*or_comma=*/false,
11843 					   /*consume_paren=*/true);
11844 
11845   /* A semicolon terminates the declaration.  */
11846   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11847 
11848   /* Complete the static assertion, which may mean either processing
11849      the static assert now or saving it for template instantiation.  */
11850   finish_static_assert (condition, message, saved_loc, member_p);
11851 }
11852 
11853 /* Parse the expression in decltype ( expression ).  */
11854 
11855 static tree
cp_parser_decltype_expr(cp_parser * parser,bool & id_expression_or_member_access_p)11856 cp_parser_decltype_expr (cp_parser *parser,
11857 			 bool &id_expression_or_member_access_p)
11858 {
11859   cp_token *id_expr_start_token;
11860   tree expr;
11861 
11862   /* First, try parsing an id-expression.  */
11863   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11864   cp_parser_parse_tentatively (parser);
11865   expr = cp_parser_id_expression (parser,
11866                                   /*template_keyword_p=*/false,
11867                                   /*check_dependency_p=*/true,
11868                                   /*template_p=*/NULL,
11869                                   /*declarator_p=*/false,
11870                                   /*optional_p=*/false);
11871 
11872   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11873     {
11874       bool non_integral_constant_expression_p = false;
11875       tree id_expression = expr;
11876       cp_id_kind idk;
11877       const char *error_msg;
11878 
11879       if (identifier_p (expr))
11880 	/* Lookup the name we got back from the id-expression.  */
11881 	expr = cp_parser_lookup_name_simple (parser, expr,
11882 					     id_expr_start_token->location);
11883 
11884       if (expr
11885           && expr != error_mark_node
11886           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11887           && TREE_CODE (expr) != TYPE_DECL
11888 	  && (TREE_CODE (expr) != BIT_NOT_EXPR
11889 	      || !TYPE_P (TREE_OPERAND (expr, 0)))
11890           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11891         {
11892           /* Complete lookup of the id-expression.  */
11893           expr = (finish_id_expression
11894                   (id_expression, expr, parser->scope, &idk,
11895                    /*integral_constant_expression_p=*/false,
11896                    /*allow_non_integral_constant_expression_p=*/true,
11897                    &non_integral_constant_expression_p,
11898                    /*template_p=*/false,
11899                    /*done=*/true,
11900                    /*address_p=*/false,
11901                    /*template_arg_p=*/false,
11902                    &error_msg,
11903 		   id_expr_start_token->location));
11904 
11905           if (expr == error_mark_node)
11906             /* We found an id-expression, but it was something that we
11907                should not have found. This is an error, not something
11908                we can recover from, so note that we found an
11909                id-expression and we'll recover as gracefully as
11910                possible.  */
11911             id_expression_or_member_access_p = true;
11912         }
11913 
11914       if (expr
11915           && expr != error_mark_node
11916           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11917         /* We have an id-expression.  */
11918         id_expression_or_member_access_p = true;
11919     }
11920 
11921   if (!id_expression_or_member_access_p)
11922     {
11923       /* Abort the id-expression parse.  */
11924       cp_parser_abort_tentative_parse (parser);
11925 
11926       /* Parsing tentatively, again.  */
11927       cp_parser_parse_tentatively (parser);
11928 
11929       /* Parse a class member access.  */
11930       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11931                                            /*cast_p=*/false, /*decltype*/true,
11932                                            /*member_access_only_p=*/true, NULL);
11933 
11934       if (expr
11935           && expr != error_mark_node
11936           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11937         /* We have an id-expression.  */
11938         id_expression_or_member_access_p = true;
11939     }
11940 
11941   if (id_expression_or_member_access_p)
11942     /* We have parsed the complete id-expression or member access.  */
11943     cp_parser_parse_definitely (parser);
11944   else
11945     {
11946       /* Abort our attempt to parse an id-expression or member access
11947          expression.  */
11948       cp_parser_abort_tentative_parse (parser);
11949 
11950       /* Parse a full expression.  */
11951       expr = cp_parser_expression (parser, /*cast_p=*/false,
11952 				   /*decltype*/true, NULL);
11953     }
11954 
11955   return expr;
11956 }
11957 
11958 /* Parse a `decltype' type. Returns the type.
11959 
11960    simple-type-specifier:
11961      decltype ( expression )
11962    C++14 proposal:
11963      decltype ( auto )  */
11964 
11965 static tree
cp_parser_decltype(cp_parser * parser)11966 cp_parser_decltype (cp_parser *parser)
11967 {
11968   tree expr;
11969   bool id_expression_or_member_access_p = false;
11970   const char *saved_message;
11971   bool saved_integral_constant_expression_p;
11972   bool saved_non_integral_constant_expression_p;
11973   bool saved_greater_than_is_operator_p;
11974   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11975 
11976   if (start_token->type == CPP_DECLTYPE)
11977     {
11978       /* Already parsed.  */
11979       cp_lexer_consume_token (parser->lexer);
11980       return start_token->u.value;
11981     }
11982 
11983   /* Look for the `decltype' token.  */
11984   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11985     return error_mark_node;
11986 
11987   /* Parse the opening `('.  */
11988   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11989     return error_mark_node;
11990 
11991   /* decltype (auto) */
11992   if (cxx_dialect >= cxx1y
11993       && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
11994     {
11995       cp_lexer_consume_token (parser->lexer);
11996       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11997 	return error_mark_node;
11998       expr = make_decltype_auto ();
11999       AUTO_IS_DECLTYPE (expr) = true;
12000       goto rewrite;
12001     }
12002 
12003   /* Types cannot be defined in a `decltype' expression.  Save away the
12004      old message.  */
12005   saved_message = parser->type_definition_forbidden_message;
12006 
12007   /* And create the new one.  */
12008   parser->type_definition_forbidden_message
12009     = G_("types may not be defined in %<decltype%> expressions");
12010 
12011   /* The restrictions on constant-expressions do not apply inside
12012      decltype expressions.  */
12013   saved_integral_constant_expression_p
12014     = parser->integral_constant_expression_p;
12015   saved_non_integral_constant_expression_p
12016     = parser->non_integral_constant_expression_p;
12017   parser->integral_constant_expression_p = false;
12018 
12019   /* Within a parenthesized expression, a `>' token is always
12020      the greater-than operator.  */
12021   saved_greater_than_is_operator_p
12022     = parser->greater_than_is_operator_p;
12023   parser->greater_than_is_operator_p = true;
12024 
12025   /* Do not actually evaluate the expression.  */
12026   ++cp_unevaluated_operand;
12027 
12028   /* Do not warn about problems with the expression.  */
12029   ++c_inhibit_evaluation_warnings;
12030 
12031   expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12032 
12033   /* Go back to evaluating expressions.  */
12034   --cp_unevaluated_operand;
12035   --c_inhibit_evaluation_warnings;
12036 
12037   /* The `>' token might be the end of a template-id or
12038      template-parameter-list now.  */
12039   parser->greater_than_is_operator_p
12040     = saved_greater_than_is_operator_p;
12041 
12042   /* Restore the old message and the integral constant expression
12043      flags.  */
12044   parser->type_definition_forbidden_message = saved_message;
12045   parser->integral_constant_expression_p
12046     = saved_integral_constant_expression_p;
12047   parser->non_integral_constant_expression_p
12048     = saved_non_integral_constant_expression_p;
12049 
12050   /* Parse to the closing `)'.  */
12051   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12052     {
12053       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12054 					     /*consume_paren=*/true);
12055       return error_mark_node;
12056     }
12057 
12058   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12059 			       tf_warning_or_error);
12060 
12061  rewrite:
12062   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12063      it again.  */
12064   start_token->type = CPP_DECLTYPE;
12065   start_token->u.value = expr;
12066   start_token->keyword = RID_MAX;
12067   cp_lexer_purge_tokens_after (parser->lexer, start_token);
12068 
12069   return expr;
12070 }
12071 
12072 /* Special member functions [gram.special] */
12073 
12074 /* Parse a conversion-function-id.
12075 
12076    conversion-function-id:
12077      operator conversion-type-id
12078 
12079    Returns an IDENTIFIER_NODE representing the operator.  */
12080 
12081 static tree
cp_parser_conversion_function_id(cp_parser * parser)12082 cp_parser_conversion_function_id (cp_parser* parser)
12083 {
12084   tree type;
12085   tree saved_scope;
12086   tree saved_qualifying_scope;
12087   tree saved_object_scope;
12088   tree pushed_scope = NULL_TREE;
12089 
12090   /* Look for the `operator' token.  */
12091   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12092     return error_mark_node;
12093   /* When we parse the conversion-type-id, the current scope will be
12094      reset.  However, we need that information in able to look up the
12095      conversion function later, so we save it here.  */
12096   saved_scope = parser->scope;
12097   saved_qualifying_scope = parser->qualifying_scope;
12098   saved_object_scope = parser->object_scope;
12099   /* We must enter the scope of the class so that the names of
12100      entities declared within the class are available in the
12101      conversion-type-id.  For example, consider:
12102 
12103        struct S {
12104 	 typedef int I;
12105 	 operator I();
12106        };
12107 
12108        S::operator I() { ... }
12109 
12110      In order to see that `I' is a type-name in the definition, we
12111      must be in the scope of `S'.  */
12112   if (saved_scope)
12113     pushed_scope = push_scope (saved_scope);
12114   /* Parse the conversion-type-id.  */
12115   type = cp_parser_conversion_type_id (parser);
12116   /* Leave the scope of the class, if any.  */
12117   if (pushed_scope)
12118     pop_scope (pushed_scope);
12119   /* Restore the saved scope.  */
12120   parser->scope = saved_scope;
12121   parser->qualifying_scope = saved_qualifying_scope;
12122   parser->object_scope = saved_object_scope;
12123   /* If the TYPE is invalid, indicate failure.  */
12124   if (type == error_mark_node)
12125     return error_mark_node;
12126   return mangle_conv_op_name_for_type (type);
12127 }
12128 
12129 /* Parse a conversion-type-id:
12130 
12131    conversion-type-id:
12132      type-specifier-seq conversion-declarator [opt]
12133 
12134    Returns the TYPE specified.  */
12135 
12136 static tree
cp_parser_conversion_type_id(cp_parser * parser)12137 cp_parser_conversion_type_id (cp_parser* parser)
12138 {
12139   tree attributes;
12140   cp_decl_specifier_seq type_specifiers;
12141   cp_declarator *declarator;
12142   tree type_specified;
12143   const char *saved_message;
12144 
12145   /* Parse the attributes.  */
12146   attributes = cp_parser_attributes_opt (parser);
12147 
12148   saved_message = parser->type_definition_forbidden_message;
12149   parser->type_definition_forbidden_message
12150     = G_("types may not be defined in a conversion-type-id");
12151 
12152   /* Parse the type-specifiers.  */
12153   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12154 				/*is_trailing_return=*/false,
12155 				&type_specifiers);
12156 
12157   parser->type_definition_forbidden_message = saved_message;
12158 
12159   /* If that didn't work, stop.  */
12160   if (type_specifiers.type == error_mark_node)
12161     return error_mark_node;
12162   /* Parse the conversion-declarator.  */
12163   declarator = cp_parser_conversion_declarator_opt (parser);
12164 
12165   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
12166 				    /*initialized=*/0, &attributes);
12167   if (attributes)
12168     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12169 
12170   /* Don't give this error when parsing tentatively.  This happens to
12171      work because we always parse this definitively once.  */
12172   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12173       && type_uses_auto (type_specified))
12174     {
12175       if (cxx_dialect < cxx1y)
12176 	{
12177 	  error ("invalid use of %<auto%> in conversion operator");
12178 	  return error_mark_node;
12179 	}
12180       else if (template_parm_scope_p ())
12181 	warning (0, "use of %<auto%> in member template "
12182 		 "conversion operator can never be deduced");
12183     }
12184 
12185   return type_specified;
12186 }
12187 
12188 /* Parse an (optional) conversion-declarator.
12189 
12190    conversion-declarator:
12191      ptr-operator conversion-declarator [opt]
12192 
12193    */
12194 
12195 static cp_declarator *
cp_parser_conversion_declarator_opt(cp_parser * parser)12196 cp_parser_conversion_declarator_opt (cp_parser* parser)
12197 {
12198   enum tree_code code;
12199   tree class_type, std_attributes = NULL_TREE;
12200   cp_cv_quals cv_quals;
12201 
12202   /* We don't know if there's a ptr-operator next, or not.  */
12203   cp_parser_parse_tentatively (parser);
12204   /* Try the ptr-operator.  */
12205   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12206 				 &std_attributes);
12207   /* If it worked, look for more conversion-declarators.  */
12208   if (cp_parser_parse_definitely (parser))
12209     {
12210       cp_declarator *declarator;
12211 
12212       /* Parse another optional declarator.  */
12213       declarator = cp_parser_conversion_declarator_opt (parser);
12214 
12215       declarator = cp_parser_make_indirect_declarator
12216 	(code, class_type, cv_quals, declarator, std_attributes);
12217 
12218       return declarator;
12219    }
12220 
12221   return NULL;
12222 }
12223 
12224 /* Parse an (optional) ctor-initializer.
12225 
12226    ctor-initializer:
12227      : mem-initializer-list
12228 
12229    Returns TRUE iff the ctor-initializer was actually present.  */
12230 
12231 static bool
cp_parser_ctor_initializer_opt(cp_parser * parser)12232 cp_parser_ctor_initializer_opt (cp_parser* parser)
12233 {
12234   /* If the next token is not a `:', then there is no
12235      ctor-initializer.  */
12236   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12237     {
12238       /* Do default initialization of any bases and members.  */
12239       if (DECL_CONSTRUCTOR_P (current_function_decl))
12240 	finish_mem_initializers (NULL_TREE);
12241 
12242       return false;
12243     }
12244 
12245   /* Consume the `:' token.  */
12246   cp_lexer_consume_token (parser->lexer);
12247   /* And the mem-initializer-list.  */
12248   cp_parser_mem_initializer_list (parser);
12249 
12250   return true;
12251 }
12252 
12253 /* Parse a mem-initializer-list.
12254 
12255    mem-initializer-list:
12256      mem-initializer ... [opt]
12257      mem-initializer ... [opt] , mem-initializer-list  */
12258 
12259 static void
cp_parser_mem_initializer_list(cp_parser * parser)12260 cp_parser_mem_initializer_list (cp_parser* parser)
12261 {
12262   tree mem_initializer_list = NULL_TREE;
12263   tree target_ctor = error_mark_node;
12264   cp_token *token = cp_lexer_peek_token (parser->lexer);
12265 
12266   /* Let the semantic analysis code know that we are starting the
12267      mem-initializer-list.  */
12268   if (!DECL_CONSTRUCTOR_P (current_function_decl))
12269     error_at (token->location,
12270 	      "only constructors take member initializers");
12271 
12272   /* Loop through the list.  */
12273   while (true)
12274     {
12275       tree mem_initializer;
12276 
12277       token = cp_lexer_peek_token (parser->lexer);
12278       /* Parse the mem-initializer.  */
12279       mem_initializer = cp_parser_mem_initializer (parser);
12280       /* If the next token is a `...', we're expanding member initializers. */
12281       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12282         {
12283           /* Consume the `...'. */
12284           cp_lexer_consume_token (parser->lexer);
12285 
12286           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12287              can be expanded but members cannot. */
12288           if (mem_initializer != error_mark_node
12289               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12290             {
12291               error_at (token->location,
12292 			"cannot expand initializer for member %<%D%>",
12293 			TREE_PURPOSE (mem_initializer));
12294               mem_initializer = error_mark_node;
12295             }
12296 
12297           /* Construct the pack expansion type. */
12298           if (mem_initializer != error_mark_node)
12299             mem_initializer = make_pack_expansion (mem_initializer);
12300         }
12301       if (target_ctor != error_mark_node
12302 	  && mem_initializer != error_mark_node)
12303 	{
12304 	  error ("mem-initializer for %qD follows constructor delegation",
12305 		 TREE_PURPOSE (mem_initializer));
12306 	  mem_initializer = error_mark_node;
12307 	}
12308       /* Look for a target constructor. */
12309       if (mem_initializer != error_mark_node
12310 	  && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12311 	  && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12312 	{
12313 	  maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12314 	  if (mem_initializer_list)
12315 	    {
12316 	      error ("constructor delegation follows mem-initializer for %qD",
12317 		     TREE_PURPOSE (mem_initializer_list));
12318 	      mem_initializer = error_mark_node;
12319 	    }
12320 	  target_ctor = mem_initializer;
12321 	}
12322       /* Add it to the list, unless it was erroneous.  */
12323       if (mem_initializer != error_mark_node)
12324 	{
12325 	  TREE_CHAIN (mem_initializer) = mem_initializer_list;
12326 	  mem_initializer_list = mem_initializer;
12327 	}
12328       /* If the next token is not a `,', we're done.  */
12329       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12330 	break;
12331       /* Consume the `,' token.  */
12332       cp_lexer_consume_token (parser->lexer);
12333     }
12334 
12335   /* Perform semantic analysis.  */
12336   if (DECL_CONSTRUCTOR_P (current_function_decl))
12337     finish_mem_initializers (mem_initializer_list);
12338 }
12339 
12340 /* Parse a mem-initializer.
12341 
12342    mem-initializer:
12343      mem-initializer-id ( expression-list [opt] )
12344      mem-initializer-id braced-init-list
12345 
12346    GNU extension:
12347 
12348    mem-initializer:
12349      ( expression-list [opt] )
12350 
12351    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
12352    class) or FIELD_DECL (for a non-static data member) to initialize;
12353    the TREE_VALUE is the expression-list.  An empty initialization
12354    list is represented by void_list_node.  */
12355 
12356 static tree
cp_parser_mem_initializer(cp_parser * parser)12357 cp_parser_mem_initializer (cp_parser* parser)
12358 {
12359   tree mem_initializer_id;
12360   tree expression_list;
12361   tree member;
12362   cp_token *token = cp_lexer_peek_token (parser->lexer);
12363 
12364   /* Find out what is being initialized.  */
12365   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12366     {
12367       permerror (token->location,
12368 		 "anachronistic old-style base class initializer");
12369       mem_initializer_id = NULL_TREE;
12370     }
12371   else
12372     {
12373       mem_initializer_id = cp_parser_mem_initializer_id (parser);
12374       if (mem_initializer_id == error_mark_node)
12375 	return mem_initializer_id;
12376     }
12377   member = expand_member_init (mem_initializer_id);
12378   if (member && !DECL_P (member))
12379     in_base_initializer = 1;
12380 
12381   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12382     {
12383       bool expr_non_constant_p;
12384       cp_lexer_set_source_position (parser->lexer);
12385       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12386       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12387       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12388       expression_list = build_tree_list (NULL_TREE, expression_list);
12389     }
12390   else
12391     {
12392       vec<tree, va_gc> *vec;
12393       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12394 						     /*cast_p=*/false,
12395 						     /*allow_expansion_p=*/true,
12396 						     /*non_constant_p=*/NULL);
12397       if (vec == NULL)
12398 	return error_mark_node;
12399       expression_list = build_tree_list_vec (vec);
12400       release_tree_vector (vec);
12401     }
12402 
12403   if (expression_list == error_mark_node)
12404     return error_mark_node;
12405   if (!expression_list)
12406     expression_list = void_type_node;
12407 
12408   in_base_initializer = 0;
12409 
12410   return member ? build_tree_list (member, expression_list) : error_mark_node;
12411 }
12412 
12413 /* Parse a mem-initializer-id.
12414 
12415    mem-initializer-id:
12416      :: [opt] nested-name-specifier [opt] class-name
12417      identifier
12418 
12419    Returns a TYPE indicating the class to be initializer for the first
12420    production.  Returns an IDENTIFIER_NODE indicating the data member
12421    to be initialized for the second production.  */
12422 
12423 static tree
cp_parser_mem_initializer_id(cp_parser * parser)12424 cp_parser_mem_initializer_id (cp_parser* parser)
12425 {
12426   bool global_scope_p;
12427   bool nested_name_specifier_p;
12428   bool template_p = false;
12429   tree id;
12430 
12431   cp_token *token = cp_lexer_peek_token (parser->lexer);
12432 
12433   /* `typename' is not allowed in this context ([temp.res]).  */
12434   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12435     {
12436       error_at (token->location,
12437 		"keyword %<typename%> not allowed in this context (a qualified "
12438 		"member initializer is implicitly a type)");
12439       cp_lexer_consume_token (parser->lexer);
12440     }
12441   /* Look for the optional `::' operator.  */
12442   global_scope_p
12443     = (cp_parser_global_scope_opt (parser,
12444 				   /*current_scope_valid_p=*/false)
12445        != NULL_TREE);
12446   /* Look for the optional nested-name-specifier.  The simplest way to
12447      implement:
12448 
12449        [temp.res]
12450 
12451        The keyword `typename' is not permitted in a base-specifier or
12452        mem-initializer; in these contexts a qualified name that
12453        depends on a template-parameter is implicitly assumed to be a
12454        type name.
12455 
12456      is to assume that we have seen the `typename' keyword at this
12457      point.  */
12458   nested_name_specifier_p
12459     = (cp_parser_nested_name_specifier_opt (parser,
12460 					    /*typename_keyword_p=*/true,
12461 					    /*check_dependency_p=*/true,
12462 					    /*type_p=*/true,
12463 					    /*is_declaration=*/true)
12464        != NULL_TREE);
12465   if (nested_name_specifier_p)
12466     template_p = cp_parser_optional_template_keyword (parser);
12467   /* If there is a `::' operator or a nested-name-specifier, then we
12468      are definitely looking for a class-name.  */
12469   if (global_scope_p || nested_name_specifier_p)
12470     return cp_parser_class_name (parser,
12471 				 /*typename_keyword_p=*/true,
12472 				 /*template_keyword_p=*/template_p,
12473 				 typename_type,
12474 				 /*check_dependency_p=*/true,
12475 				 /*class_head_p=*/false,
12476 				 /*is_declaration=*/true);
12477   /* Otherwise, we could also be looking for an ordinary identifier.  */
12478   cp_parser_parse_tentatively (parser);
12479   /* Try a class-name.  */
12480   id = cp_parser_class_name (parser,
12481 			     /*typename_keyword_p=*/true,
12482 			     /*template_keyword_p=*/false,
12483 			     none_type,
12484 			     /*check_dependency_p=*/true,
12485 			     /*class_head_p=*/false,
12486 			     /*is_declaration=*/true);
12487   /* If we found one, we're done.  */
12488   if (cp_parser_parse_definitely (parser))
12489     return id;
12490   /* Otherwise, look for an ordinary identifier.  */
12491   return cp_parser_identifier (parser);
12492 }
12493 
12494 /* Overloading [gram.over] */
12495 
12496 /* Parse an operator-function-id.
12497 
12498    operator-function-id:
12499      operator operator
12500 
12501    Returns an IDENTIFIER_NODE for the operator which is a
12502    human-readable spelling of the identifier, e.g., `operator +'.  */
12503 
12504 static tree
cp_parser_operator_function_id(cp_parser * parser)12505 cp_parser_operator_function_id (cp_parser* parser)
12506 {
12507   /* Look for the `operator' keyword.  */
12508   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12509     return error_mark_node;
12510   /* And then the name of the operator itself.  */
12511   return cp_parser_operator (parser);
12512 }
12513 
12514 /* Return an identifier node for a user-defined literal operator.
12515    The suffix identifier is chained to the operator name identifier.  */
12516 
12517 static tree
cp_literal_operator_id(const char * name)12518 cp_literal_operator_id (const char* name)
12519 {
12520   tree identifier;
12521   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12522 			      + strlen (name) + 10);
12523   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12524   identifier = get_identifier (buffer);
12525 
12526   return identifier;
12527 }
12528 
12529 /* Parse an operator.
12530 
12531    operator:
12532      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12533      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12534      || ++ -- , ->* -> () []
12535 
12536    GNU Extensions:
12537 
12538    operator:
12539      <? >? <?= >?=
12540 
12541    Returns an IDENTIFIER_NODE for the operator which is a
12542    human-readable spelling of the identifier, e.g., `operator +'.  */
12543 
12544 static tree
cp_parser_operator(cp_parser * parser)12545 cp_parser_operator (cp_parser* parser)
12546 {
12547   tree id = NULL_TREE;
12548   cp_token *token;
12549   bool bad_encoding_prefix = false;
12550 
12551   /* Peek at the next token.  */
12552   token = cp_lexer_peek_token (parser->lexer);
12553   /* Figure out which operator we have.  */
12554   switch (token->type)
12555     {
12556     case CPP_KEYWORD:
12557       {
12558 	enum tree_code op;
12559 
12560 	/* The keyword should be either `new' or `delete'.  */
12561 	if (token->keyword == RID_NEW)
12562 	  op = NEW_EXPR;
12563 	else if (token->keyword == RID_DELETE)
12564 	  op = DELETE_EXPR;
12565 	else
12566 	  break;
12567 
12568 	/* Consume the `new' or `delete' token.  */
12569 	cp_lexer_consume_token (parser->lexer);
12570 
12571 	/* Peek at the next token.  */
12572 	token = cp_lexer_peek_token (parser->lexer);
12573 	/* If it's a `[' token then this is the array variant of the
12574 	   operator.  */
12575 	if (token->type == CPP_OPEN_SQUARE)
12576 	  {
12577 	    /* Consume the `[' token.  */
12578 	    cp_lexer_consume_token (parser->lexer);
12579 	    /* Look for the `]' token.  */
12580 	    cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12581 	    id = ansi_opname (op == NEW_EXPR
12582 			      ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12583 	  }
12584 	/* Otherwise, we have the non-array variant.  */
12585 	else
12586 	  id = ansi_opname (op);
12587 
12588 	return id;
12589       }
12590 
12591     case CPP_PLUS:
12592       id = ansi_opname (PLUS_EXPR);
12593       break;
12594 
12595     case CPP_MINUS:
12596       id = ansi_opname (MINUS_EXPR);
12597       break;
12598 
12599     case CPP_MULT:
12600       id = ansi_opname (MULT_EXPR);
12601       break;
12602 
12603     case CPP_DIV:
12604       id = ansi_opname (TRUNC_DIV_EXPR);
12605       break;
12606 
12607     case CPP_MOD:
12608       id = ansi_opname (TRUNC_MOD_EXPR);
12609       break;
12610 
12611     case CPP_XOR:
12612       id = ansi_opname (BIT_XOR_EXPR);
12613       break;
12614 
12615     case CPP_AND:
12616       id = ansi_opname (BIT_AND_EXPR);
12617       break;
12618 
12619     case CPP_OR:
12620       id = ansi_opname (BIT_IOR_EXPR);
12621       break;
12622 
12623     case CPP_COMPL:
12624       id = ansi_opname (BIT_NOT_EXPR);
12625       break;
12626 
12627     case CPP_NOT:
12628       id = ansi_opname (TRUTH_NOT_EXPR);
12629       break;
12630 
12631     case CPP_EQ:
12632       id = ansi_assopname (NOP_EXPR);
12633       break;
12634 
12635     case CPP_LESS:
12636       id = ansi_opname (LT_EXPR);
12637       break;
12638 
12639     case CPP_GREATER:
12640       id = ansi_opname (GT_EXPR);
12641       break;
12642 
12643     case CPP_PLUS_EQ:
12644       id = ansi_assopname (PLUS_EXPR);
12645       break;
12646 
12647     case CPP_MINUS_EQ:
12648       id = ansi_assopname (MINUS_EXPR);
12649       break;
12650 
12651     case CPP_MULT_EQ:
12652       id = ansi_assopname (MULT_EXPR);
12653       break;
12654 
12655     case CPP_DIV_EQ:
12656       id = ansi_assopname (TRUNC_DIV_EXPR);
12657       break;
12658 
12659     case CPP_MOD_EQ:
12660       id = ansi_assopname (TRUNC_MOD_EXPR);
12661       break;
12662 
12663     case CPP_XOR_EQ:
12664       id = ansi_assopname (BIT_XOR_EXPR);
12665       break;
12666 
12667     case CPP_AND_EQ:
12668       id = ansi_assopname (BIT_AND_EXPR);
12669       break;
12670 
12671     case CPP_OR_EQ:
12672       id = ansi_assopname (BIT_IOR_EXPR);
12673       break;
12674 
12675     case CPP_LSHIFT:
12676       id = ansi_opname (LSHIFT_EXPR);
12677       break;
12678 
12679     case CPP_RSHIFT:
12680       id = ansi_opname (RSHIFT_EXPR);
12681       break;
12682 
12683     case CPP_LSHIFT_EQ:
12684       id = ansi_assopname (LSHIFT_EXPR);
12685       break;
12686 
12687     case CPP_RSHIFT_EQ:
12688       id = ansi_assopname (RSHIFT_EXPR);
12689       break;
12690 
12691     case CPP_EQ_EQ:
12692       id = ansi_opname (EQ_EXPR);
12693       break;
12694 
12695     case CPP_NOT_EQ:
12696       id = ansi_opname (NE_EXPR);
12697       break;
12698 
12699     case CPP_LESS_EQ:
12700       id = ansi_opname (LE_EXPR);
12701       break;
12702 
12703     case CPP_GREATER_EQ:
12704       id = ansi_opname (GE_EXPR);
12705       break;
12706 
12707     case CPP_AND_AND:
12708       id = ansi_opname (TRUTH_ANDIF_EXPR);
12709       break;
12710 
12711     case CPP_OR_OR:
12712       id = ansi_opname (TRUTH_ORIF_EXPR);
12713       break;
12714 
12715     case CPP_PLUS_PLUS:
12716       id = ansi_opname (POSTINCREMENT_EXPR);
12717       break;
12718 
12719     case CPP_MINUS_MINUS:
12720       id = ansi_opname (PREDECREMENT_EXPR);
12721       break;
12722 
12723     case CPP_COMMA:
12724       id = ansi_opname (COMPOUND_EXPR);
12725       break;
12726 
12727     case CPP_DEREF_STAR:
12728       id = ansi_opname (MEMBER_REF);
12729       break;
12730 
12731     case CPP_DEREF:
12732       id = ansi_opname (COMPONENT_REF);
12733       break;
12734 
12735     case CPP_OPEN_PAREN:
12736       /* Consume the `('.  */
12737       cp_lexer_consume_token (parser->lexer);
12738       /* Look for the matching `)'.  */
12739       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12740       return ansi_opname (CALL_EXPR);
12741 
12742     case CPP_OPEN_SQUARE:
12743       /* Consume the `['.  */
12744       cp_lexer_consume_token (parser->lexer);
12745       /* Look for the matching `]'.  */
12746       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12747       return ansi_opname (ARRAY_REF);
12748 
12749     case CPP_WSTRING:
12750     case CPP_STRING16:
12751     case CPP_STRING32:
12752     case CPP_UTF8STRING:
12753      bad_encoding_prefix = true;
12754       /* Fall through.  */
12755 
12756     case CPP_STRING:
12757       if (cxx_dialect == cxx98)
12758 	maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12759       if (bad_encoding_prefix)
12760 	{
12761 	  error ("invalid encoding prefix in literal operator");
12762 	  return error_mark_node;
12763 	}
12764       if (TREE_STRING_LENGTH (token->u.value) > 2)
12765 	{
12766 	  error ("expected empty string after %<operator%> keyword");
12767 	  return error_mark_node;
12768 	}
12769       /* Consume the string.  */
12770       cp_lexer_consume_token (parser->lexer);
12771       /* Look for the suffix identifier.  */
12772       token = cp_lexer_peek_token (parser->lexer);
12773       if (token->type == CPP_NAME)
12774 	{
12775 	  id = cp_parser_identifier (parser);
12776 	  if (id != error_mark_node)
12777 	    {
12778 	      const char *name = IDENTIFIER_POINTER (id);
12779 	      return cp_literal_operator_id (name);
12780 	    }
12781 	}
12782       else if (token->type == CPP_KEYWORD)
12783 	{
12784 	  error ("unexpected keyword;"
12785 		 " remove space between quotes and suffix identifier");
12786 	  return error_mark_node;
12787 	}
12788       else
12789 	{
12790 	  error ("expected suffix identifier");
12791 	  return error_mark_node;
12792 	}
12793 
12794     case CPP_WSTRING_USERDEF:
12795     case CPP_STRING16_USERDEF:
12796     case CPP_STRING32_USERDEF:
12797     case CPP_UTF8STRING_USERDEF:
12798       bad_encoding_prefix = true;
12799       /* Fall through.  */
12800 
12801     case CPP_STRING_USERDEF:
12802       if (cxx_dialect == cxx98)
12803 	maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12804       if (bad_encoding_prefix)
12805 	{
12806 	  error ("invalid encoding prefix in literal operator");
12807 	  return error_mark_node;
12808 	}
12809       {
12810 	tree string_tree = USERDEF_LITERAL_VALUE (token->u.value);
12811 	if (TREE_STRING_LENGTH (string_tree) > 2)
12812 	  {
12813 	    error ("expected empty string after %<operator%> keyword");
12814 	    return error_mark_node;
12815 	  }
12816 	id = USERDEF_LITERAL_SUFFIX_ID (token->u.value);
12817 	/* Consume the user-defined string literal.  */
12818 	cp_lexer_consume_token (parser->lexer);
12819 	if (id != error_mark_node)
12820 	  {
12821 	    const char *name = IDENTIFIER_POINTER (id);
12822 	    return cp_literal_operator_id (name);
12823 	  }
12824 	else
12825 	  return error_mark_node;
12826       }
12827 
12828     default:
12829       /* Anything else is an error.  */
12830       break;
12831     }
12832 
12833   /* If we have selected an identifier, we need to consume the
12834      operator token.  */
12835   if (id)
12836     cp_lexer_consume_token (parser->lexer);
12837   /* Otherwise, no valid operator name was present.  */
12838   else
12839     {
12840       cp_parser_error (parser, "expected operator");
12841       id = error_mark_node;
12842     }
12843 
12844   return id;
12845 }
12846 
12847 /* Parse a template-declaration.
12848 
12849    template-declaration:
12850      export [opt] template < template-parameter-list > declaration
12851 
12852    If MEMBER_P is TRUE, this template-declaration occurs within a
12853    class-specifier.
12854 
12855    The grammar rule given by the standard isn't correct.  What
12856    is really meant is:
12857 
12858    template-declaration:
12859      export [opt] template-parameter-list-seq
12860        decl-specifier-seq [opt] init-declarator [opt] ;
12861      export [opt] template-parameter-list-seq
12862        function-definition
12863 
12864    template-parameter-list-seq:
12865      template-parameter-list-seq [opt]
12866      template < template-parameter-list >  */
12867 
12868 static void
cp_parser_template_declaration(cp_parser * parser,bool member_p)12869 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12870 {
12871   /* Check for `export'.  */
12872   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12873     {
12874       /* Consume the `export' token.  */
12875       cp_lexer_consume_token (parser->lexer);
12876       /* Warn that we do not support `export'.  */
12877       warning (0, "keyword %<export%> not implemented, and will be ignored");
12878     }
12879 
12880   cp_parser_template_declaration_after_export (parser, member_p);
12881 }
12882 
12883 /* Parse a template-parameter-list.
12884 
12885    template-parameter-list:
12886      template-parameter
12887      template-parameter-list , template-parameter
12888 
12889    Returns a TREE_LIST.  Each node represents a template parameter.
12890    The nodes are connected via their TREE_CHAINs.  */
12891 
12892 static tree
cp_parser_template_parameter_list(cp_parser * parser)12893 cp_parser_template_parameter_list (cp_parser* parser)
12894 {
12895   tree parameter_list = NULL_TREE;
12896 
12897   begin_template_parm_list ();
12898 
12899   /* The loop below parses the template parms.  We first need to know
12900      the total number of template parms to be able to compute proper
12901      canonical types of each dependent type. So after the loop, when
12902      we know the total number of template parms,
12903      end_template_parm_list computes the proper canonical types and
12904      fixes up the dependent types accordingly.  */
12905   while (true)
12906     {
12907       tree parameter;
12908       bool is_non_type;
12909       bool is_parameter_pack;
12910       location_t parm_loc;
12911 
12912       /* Parse the template-parameter.  */
12913       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12914       parameter = cp_parser_template_parameter (parser,
12915                                                 &is_non_type,
12916                                                 &is_parameter_pack);
12917       /* Add it to the list.  */
12918       if (parameter != error_mark_node)
12919 	parameter_list = process_template_parm (parameter_list,
12920 						parm_loc,
12921 						parameter,
12922 						is_non_type,
12923 						is_parameter_pack);
12924       else
12925        {
12926          tree err_parm = build_tree_list (parameter, parameter);
12927          parameter_list = chainon (parameter_list, err_parm);
12928        }
12929 
12930       /* If the next token is not a `,', we're done.  */
12931       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12932 	break;
12933       /* Otherwise, consume the `,' token.  */
12934       cp_lexer_consume_token (parser->lexer);
12935     }
12936 
12937   return end_template_parm_list (parameter_list);
12938 }
12939 
12940 /* Parse a template-parameter.
12941 
12942    template-parameter:
12943      type-parameter
12944      parameter-declaration
12945 
12946    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
12947    the parameter.  The TREE_PURPOSE is the default value, if any.
12948    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
12949    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
12950    set to true iff this parameter is a parameter pack. */
12951 
12952 static tree
cp_parser_template_parameter(cp_parser * parser,bool * is_non_type,bool * is_parameter_pack)12953 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12954                               bool *is_parameter_pack)
12955 {
12956   cp_token *token;
12957   cp_parameter_declarator *parameter_declarator;
12958   cp_declarator *id_declarator;
12959   tree parm;
12960 
12961   /* Assume it is a type parameter or a template parameter.  */
12962   *is_non_type = false;
12963   /* Assume it not a parameter pack. */
12964   *is_parameter_pack = false;
12965   /* Peek at the next token.  */
12966   token = cp_lexer_peek_token (parser->lexer);
12967   /* If it is `class' or `template', we have a type-parameter.  */
12968   if (token->keyword == RID_TEMPLATE)
12969     return cp_parser_type_parameter (parser, is_parameter_pack);
12970   /* If it is `class' or `typename' we do not know yet whether it is a
12971      type parameter or a non-type parameter.  Consider:
12972 
12973        template <typename T, typename T::X X> ...
12974 
12975      or:
12976 
12977        template <class C, class D*> ...
12978 
12979      Here, the first parameter is a type parameter, and the second is
12980      a non-type parameter.  We can tell by looking at the token after
12981      the identifier -- if it is a `,', `=', or `>' then we have a type
12982      parameter.  */
12983   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12984     {
12985       /* Peek at the token after `class' or `typename'.  */
12986       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12987       /* If it's an ellipsis, we have a template type parameter
12988          pack. */
12989       if (token->type == CPP_ELLIPSIS)
12990         return cp_parser_type_parameter (parser, is_parameter_pack);
12991       /* If it's an identifier, skip it.  */
12992       if (token->type == CPP_NAME)
12993 	token = cp_lexer_peek_nth_token (parser->lexer, 3);
12994       /* Now, see if the token looks like the end of a template
12995 	 parameter.  */
12996       if (token->type == CPP_COMMA
12997 	  || token->type == CPP_EQ
12998 	  || token->type == CPP_GREATER)
12999 	return cp_parser_type_parameter (parser, is_parameter_pack);
13000     }
13001 
13002   /* Otherwise, it is a non-type parameter.
13003 
13004      [temp.param]
13005 
13006      When parsing a default template-argument for a non-type
13007      template-parameter, the first non-nested `>' is taken as the end
13008      of the template parameter-list rather than a greater-than
13009      operator.  */
13010   *is_non_type = true;
13011   parameter_declarator
13012      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13013 					/*parenthesized_p=*/NULL);
13014 
13015   if (!parameter_declarator)
13016     return error_mark_node;
13017 
13018   /* If the parameter declaration is marked as a parameter pack, set
13019      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13020      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13021      grokdeclarator. */
13022   if (parameter_declarator->declarator
13023       && parameter_declarator->declarator->parameter_pack_p)
13024     {
13025       *is_parameter_pack = true;
13026       parameter_declarator->declarator->parameter_pack_p = false;
13027     }
13028 
13029   if (parameter_declarator->default_argument)
13030     {
13031       /* Can happen in some cases of erroneous input (c++/34892).  */
13032       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13033 	/* Consume the `...' for better error recovery.  */
13034 	cp_lexer_consume_token (parser->lexer);
13035     }
13036   /* If the next token is an ellipsis, and we don't already have it
13037      marked as a parameter pack, then we have a parameter pack (that
13038      has no declarator).  */
13039   else if (!*is_parameter_pack
13040 	   && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13041 	   && (declarator_can_be_parameter_pack
13042 	       (parameter_declarator->declarator)))
13043     {
13044       /* Consume the `...'.  */
13045       cp_lexer_consume_token (parser->lexer);
13046       maybe_warn_variadic_templates ();
13047 
13048       *is_parameter_pack = true;
13049     }
13050   /* We might end up with a pack expansion as the type of the non-type
13051      template parameter, in which case this is a non-type template
13052      parameter pack.  */
13053   else if (parameter_declarator->decl_specifiers.type
13054 	   && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13055     {
13056       *is_parameter_pack = true;
13057       parameter_declarator->decl_specifiers.type =
13058 	PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13059     }
13060 
13061   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13062     {
13063       /* Parameter packs cannot have default arguments.  However, a
13064 	 user may try to do so, so we'll parse them and give an
13065 	 appropriate diagnostic here.  */
13066 
13067       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13068 
13069       /* Find the name of the parameter pack.  */
13070       id_declarator = parameter_declarator->declarator;
13071       while (id_declarator && id_declarator->kind != cdk_id)
13072 	id_declarator = id_declarator->declarator;
13073 
13074       if (id_declarator && id_declarator->kind == cdk_id)
13075 	error_at (start_token->location,
13076 		  "template parameter pack %qD cannot have a default argument",
13077 		  id_declarator->u.id.unqualified_name);
13078       else
13079 	error_at (start_token->location,
13080 		  "template parameter pack cannot have a default argument");
13081 
13082       /* Parse the default argument, but throw away the result.  */
13083       cp_parser_default_argument (parser, /*template_parm_p=*/true);
13084     }
13085 
13086   parm = grokdeclarator (parameter_declarator->declarator,
13087 			 &parameter_declarator->decl_specifiers,
13088 			 TPARM, /*initialized=*/0,
13089 			 /*attrlist=*/NULL);
13090   if (parm == error_mark_node)
13091     return error_mark_node;
13092 
13093   return build_tree_list (parameter_declarator->default_argument, parm);
13094 }
13095 
13096 /* Parse a type-parameter.
13097 
13098    type-parameter:
13099      class identifier [opt]
13100      class identifier [opt] = type-id
13101      typename identifier [opt]
13102      typename identifier [opt] = type-id
13103      template < template-parameter-list > class identifier [opt]
13104      template < template-parameter-list > class identifier [opt]
13105        = id-expression
13106 
13107    GNU Extension (variadic templates):
13108 
13109    type-parameter:
13110      class ... identifier [opt]
13111      typename ... identifier [opt]
13112 
13113    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
13114    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
13115    the declaration of the parameter.
13116 
13117    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13118 
13119 static tree
cp_parser_type_parameter(cp_parser * parser,bool * is_parameter_pack)13120 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13121 {
13122   cp_token *token;
13123   tree parameter;
13124 
13125   /* Look for a keyword to tell us what kind of parameter this is.  */
13126   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13127   if (!token)
13128     return error_mark_node;
13129 
13130   switch (token->keyword)
13131     {
13132     case RID_CLASS:
13133     case RID_TYPENAME:
13134       {
13135 	tree identifier;
13136 	tree default_argument;
13137 
13138         /* If the next token is an ellipsis, we have a template
13139            argument pack. */
13140         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13141           {
13142             /* Consume the `...' token. */
13143             cp_lexer_consume_token (parser->lexer);
13144             maybe_warn_variadic_templates ();
13145 
13146             *is_parameter_pack = true;
13147           }
13148 
13149 	/* If the next token is an identifier, then it names the
13150 	   parameter.  */
13151 	if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13152 	  identifier = cp_parser_identifier (parser);
13153 	else
13154 	  identifier = NULL_TREE;
13155 
13156 	/* Create the parameter.  */
13157 	parameter = finish_template_type_parm (class_type_node, identifier);
13158 
13159 	/* If the next token is an `=', we have a default argument.  */
13160 	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13161 	  {
13162 	    /* Consume the `=' token.  */
13163 	    cp_lexer_consume_token (parser->lexer);
13164 	    /* Parse the default-argument.  */
13165 	    push_deferring_access_checks (dk_no_deferred);
13166 	    default_argument = cp_parser_type_id (parser);
13167 
13168             /* Template parameter packs cannot have default
13169                arguments. */
13170             if (*is_parameter_pack)
13171               {
13172                 if (identifier)
13173                   error_at (token->location,
13174 			    "template parameter pack %qD cannot have a "
13175 			    "default argument", identifier);
13176                 else
13177                   error_at (token->location,
13178 			    "template parameter packs cannot have "
13179 			    "default arguments");
13180                 default_argument = NULL_TREE;
13181               }
13182 	    pop_deferring_access_checks ();
13183 	  }
13184 	else
13185 	  default_argument = NULL_TREE;
13186 
13187 	/* Create the combined representation of the parameter and the
13188 	   default argument.  */
13189 	parameter = build_tree_list (default_argument, parameter);
13190       }
13191       break;
13192 
13193     case RID_TEMPLATE:
13194       {
13195 	tree identifier;
13196 	tree default_argument;
13197 
13198 	/* Look for the `<'.  */
13199 	cp_parser_require (parser, CPP_LESS, RT_LESS);
13200 	/* Parse the template-parameter-list.  */
13201 	cp_parser_template_parameter_list (parser);
13202 	/* Look for the `>'.  */
13203 	cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13204 	/* Look for the `class' keyword.  */
13205 	cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
13206         /* If the next token is an ellipsis, we have a template
13207            argument pack. */
13208         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13209           {
13210             /* Consume the `...' token. */
13211             cp_lexer_consume_token (parser->lexer);
13212             maybe_warn_variadic_templates ();
13213 
13214             *is_parameter_pack = true;
13215           }
13216 	/* If the next token is an `=', then there is a
13217 	   default-argument.  If the next token is a `>', we are at
13218 	   the end of the parameter-list.  If the next token is a `,',
13219 	   then we are at the end of this parameter.  */
13220 	if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13221 	    && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13222 	    && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13223 	  {
13224 	    identifier = cp_parser_identifier (parser);
13225 	    /* Treat invalid names as if the parameter were nameless.  */
13226 	    if (identifier == error_mark_node)
13227 	      identifier = NULL_TREE;
13228 	  }
13229 	else
13230 	  identifier = NULL_TREE;
13231 
13232 	/* Create the template parameter.  */
13233 	parameter = finish_template_template_parm (class_type_node,
13234 						   identifier);
13235 
13236 	/* If the next token is an `=', then there is a
13237 	   default-argument.  */
13238 	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13239 	  {
13240 	    bool is_template;
13241 
13242 	    /* Consume the `='.  */
13243 	    cp_lexer_consume_token (parser->lexer);
13244 	    /* Parse the id-expression.  */
13245 	    push_deferring_access_checks (dk_no_deferred);
13246 	    /* save token before parsing the id-expression, for error
13247 	       reporting */
13248 	    token = cp_lexer_peek_token (parser->lexer);
13249 	    default_argument
13250 	      = cp_parser_id_expression (parser,
13251 					 /*template_keyword_p=*/false,
13252 					 /*check_dependency_p=*/true,
13253 					 /*template_p=*/&is_template,
13254 					 /*declarator_p=*/false,
13255 					 /*optional_p=*/false);
13256 	    if (TREE_CODE (default_argument) == TYPE_DECL)
13257 	      /* If the id-expression was a template-id that refers to
13258 		 a template-class, we already have the declaration here,
13259 		 so no further lookup is needed.  */
13260 		 ;
13261 	    else
13262 	      /* Look up the name.  */
13263 	      default_argument
13264 		= cp_parser_lookup_name (parser, default_argument,
13265 					 none_type,
13266 					 /*is_template=*/is_template,
13267 					 /*is_namespace=*/false,
13268 					 /*check_dependency=*/true,
13269 					 /*ambiguous_decls=*/NULL,
13270 					 token->location);
13271 	    /* See if the default argument is valid.  */
13272 	    default_argument
13273 	      = check_template_template_default_arg (default_argument);
13274 
13275             /* Template parameter packs cannot have default
13276                arguments. */
13277             if (*is_parameter_pack)
13278               {
13279                 if (identifier)
13280                   error_at (token->location,
13281 			    "template parameter pack %qD cannot "
13282 			    "have a default argument",
13283 			    identifier);
13284                 else
13285                   error_at (token->location, "template parameter packs cannot "
13286 			    "have default arguments");
13287                 default_argument = NULL_TREE;
13288               }
13289 	    pop_deferring_access_checks ();
13290 	  }
13291 	else
13292 	  default_argument = NULL_TREE;
13293 
13294 	/* Create the combined representation of the parameter and the
13295 	   default argument.  */
13296 	parameter = build_tree_list (default_argument, parameter);
13297       }
13298       break;
13299 
13300     default:
13301       gcc_unreachable ();
13302       break;
13303     }
13304 
13305   return parameter;
13306 }
13307 
13308 /* Parse a template-id.
13309 
13310    template-id:
13311      template-name < template-argument-list [opt] >
13312 
13313    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13314    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
13315    returned.  Otherwise, if the template-name names a function, or set
13316    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
13317    names a class, returns a TYPE_DECL for the specialization.
13318 
13319    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13320    uninstantiated templates.  */
13321 
13322 static tree
cp_parser_template_id(cp_parser * parser,bool template_keyword_p,bool check_dependency_p,enum tag_types tag_type,bool is_declaration)13323 cp_parser_template_id (cp_parser *parser,
13324 		       bool template_keyword_p,
13325 		       bool check_dependency_p,
13326 		       enum tag_types tag_type,
13327 		       bool is_declaration)
13328 {
13329   int i;
13330   tree templ;
13331   tree arguments;
13332   tree template_id;
13333   cp_token_position start_of_id = 0;
13334   deferred_access_check *chk;
13335   vec<deferred_access_check, va_gc> *access_check;
13336   cp_token *next_token = NULL, *next_token_2 = NULL;
13337   bool is_identifier;
13338 
13339   /* If the next token corresponds to a template-id, there is no need
13340      to reparse it.  */
13341   next_token = cp_lexer_peek_token (parser->lexer);
13342   if (next_token->type == CPP_TEMPLATE_ID)
13343     {
13344       struct tree_check *check_value;
13345 
13346       /* Get the stored value.  */
13347       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13348       /* Perform any access checks that were deferred.  */
13349       access_check = check_value->checks;
13350       if (access_check)
13351 	{
13352 	  FOR_EACH_VEC_ELT (*access_check, i, chk)
13353 	    perform_or_defer_access_check (chk->binfo,
13354 					   chk->decl,
13355 					   chk->diag_decl,
13356 					   tf_warning_or_error);
13357 	}
13358       /* Return the stored value.  */
13359       return check_value->value;
13360     }
13361 
13362   /* Avoid performing name lookup if there is no possibility of
13363      finding a template-id.  */
13364   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13365       || (next_token->type == CPP_NAME
13366 	  && !cp_parser_nth_token_starts_template_argument_list_p
13367 	       (parser, 2)))
13368     {
13369       cp_parser_error (parser, "expected template-id");
13370       return error_mark_node;
13371     }
13372 
13373   /* Remember where the template-id starts.  */
13374   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13375     start_of_id = cp_lexer_token_position (parser->lexer, false);
13376 
13377   push_deferring_access_checks (dk_deferred);
13378 
13379   /* Parse the template-name.  */
13380   is_identifier = false;
13381   templ = cp_parser_template_name (parser, template_keyword_p,
13382 				   check_dependency_p,
13383 				   is_declaration,
13384 				   tag_type,
13385 				   &is_identifier);
13386   if (templ == error_mark_node || is_identifier)
13387     {
13388       pop_deferring_access_checks ();
13389       return templ;
13390     }
13391 
13392   /* If we find the sequence `[:' after a template-name, it's probably
13393      a digraph-typo for `< ::'. Substitute the tokens and check if we can
13394      parse correctly the argument list.  */
13395   next_token = cp_lexer_peek_token (parser->lexer);
13396   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13397   if (next_token->type == CPP_OPEN_SQUARE
13398       && next_token->flags & DIGRAPH
13399       && next_token_2->type == CPP_COLON
13400       && !(next_token_2->flags & PREV_WHITE))
13401     {
13402       cp_parser_parse_tentatively (parser);
13403       /* Change `:' into `::'.  */
13404       next_token_2->type = CPP_SCOPE;
13405       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13406 	 CPP_LESS.  */
13407       cp_lexer_consume_token (parser->lexer);
13408 
13409       /* Parse the arguments.  */
13410       arguments = cp_parser_enclosed_template_argument_list (parser);
13411       if (!cp_parser_parse_definitely (parser))
13412 	{
13413 	  /* If we couldn't parse an argument list, then we revert our changes
13414 	     and return simply an error. Maybe this is not a template-id
13415 	     after all.  */
13416 	  next_token_2->type = CPP_COLON;
13417 	  cp_parser_error (parser, "expected %<<%>");
13418 	  pop_deferring_access_checks ();
13419 	  return error_mark_node;
13420 	}
13421       /* Otherwise, emit an error about the invalid digraph, but continue
13422 	 parsing because we got our argument list.  */
13423       if (permerror (next_token->location,
13424 		     "%<<::%> cannot begin a template-argument list"))
13425 	{
13426 	  static bool hint = false;
13427 	  inform (next_token->location,
13428 		  "%<<:%> is an alternate spelling for %<[%>."
13429 		  " Insert whitespace between %<<%> and %<::%>");
13430 	  if (!hint && !flag_permissive)
13431 	    {
13432 	      inform (next_token->location, "(if you use %<-fpermissive%> "
13433 		      "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13434 		      "accept your code)");
13435 	      hint = true;
13436 	    }
13437 	}
13438     }
13439   else
13440     {
13441       /* Look for the `<' that starts the template-argument-list.  */
13442       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13443 	{
13444 	  pop_deferring_access_checks ();
13445 	  return error_mark_node;
13446 	}
13447       /* Parse the arguments.  */
13448       arguments = cp_parser_enclosed_template_argument_list (parser);
13449     }
13450 
13451   /* Build a representation of the specialization.  */
13452   if (identifier_p (templ))
13453     template_id = build_min_nt_loc (next_token->location,
13454 				    TEMPLATE_ID_EXPR,
13455 				    templ, arguments);
13456   else if (DECL_TYPE_TEMPLATE_P (templ)
13457 	   || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13458     {
13459       bool entering_scope;
13460       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13461 	 template (rather than some instantiation thereof) only if
13462 	 is not nested within some other construct.  For example, in
13463 	 "template <typename T> void f(T) { A<T>::", A<T> is just an
13464 	 instantiation of A.  */
13465       entering_scope = (template_parm_scope_p ()
13466 			&& cp_lexer_next_token_is (parser->lexer,
13467 						   CPP_SCOPE));
13468       template_id
13469 	= finish_template_type (templ, arguments, entering_scope);
13470     }
13471   else
13472     {
13473       /* If it's not a class-template or a template-template, it should be
13474 	 a function-template.  */
13475       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13476 		   || TREE_CODE (templ) == OVERLOAD
13477 		   || BASELINK_P (templ)));
13478 
13479       template_id = lookup_template_function (templ, arguments);
13480     }
13481 
13482   /* If parsing tentatively, replace the sequence of tokens that makes
13483      up the template-id with a CPP_TEMPLATE_ID token.  That way,
13484      should we re-parse the token stream, we will not have to repeat
13485      the effort required to do the parse, nor will we issue duplicate
13486      error messages about problems during instantiation of the
13487      template.  */
13488   if (start_of_id
13489       /* Don't do this if we had a parse error in a declarator; re-parsing
13490 	 might succeed if a name changes meaning (60361).  */
13491       && !(cp_parser_error_occurred (parser)
13492 	   && cp_parser_parsing_tentatively (parser)
13493 	   && parser->in_declarator_p))
13494     {
13495       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13496 
13497       /* Reset the contents of the START_OF_ID token.  */
13498       token->type = CPP_TEMPLATE_ID;
13499       /* Retrieve any deferred checks.  Do not pop this access checks yet
13500 	 so the memory will not be reclaimed during token replacing below.  */
13501       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
13502       token->u.tree_check_value->value = template_id;
13503       token->u.tree_check_value->checks = get_deferred_access_checks ();
13504       token->keyword = RID_MAX;
13505 
13506       /* Purge all subsequent tokens.  */
13507       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13508 
13509       /* ??? Can we actually assume that, if template_id ==
13510 	 error_mark_node, we will have issued a diagnostic to the
13511 	 user, as opposed to simply marking the tentative parse as
13512 	 failed?  */
13513       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13514 	error_at (token->location, "parse error in template argument list");
13515     }
13516 
13517   pop_to_parent_deferring_access_checks ();
13518   return template_id;
13519 }
13520 
13521 /* Parse a template-name.
13522 
13523    template-name:
13524      identifier
13525 
13526    The standard should actually say:
13527 
13528    template-name:
13529      identifier
13530      operator-function-id
13531 
13532    A defect report has been filed about this issue.
13533 
13534    A conversion-function-id cannot be a template name because they cannot
13535    be part of a template-id. In fact, looking at this code:
13536 
13537    a.operator K<int>()
13538 
13539    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13540    It is impossible to call a templated conversion-function-id with an
13541    explicit argument list, since the only allowed template parameter is
13542    the type to which it is converting.
13543 
13544    If TEMPLATE_KEYWORD_P is true, then we have just seen the
13545    `template' keyword, in a construction like:
13546 
13547      T::template f<3>()
13548 
13549    In that case `f' is taken to be a template-name, even though there
13550    is no way of knowing for sure.
13551 
13552    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13553    name refers to a set of overloaded functions, at least one of which
13554    is a template, or an IDENTIFIER_NODE with the name of the template,
13555    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
13556    names are looked up inside uninstantiated templates.  */
13557 
13558 static tree
cp_parser_template_name(cp_parser * parser,bool template_keyword_p,bool check_dependency_p,bool is_declaration,enum tag_types tag_type,bool * is_identifier)13559 cp_parser_template_name (cp_parser* parser,
13560 			 bool template_keyword_p,
13561 			 bool check_dependency_p,
13562 			 bool is_declaration,
13563 			 enum tag_types tag_type,
13564 			 bool *is_identifier)
13565 {
13566   tree identifier;
13567   tree decl;
13568   tree fns;
13569   cp_token *token = cp_lexer_peek_token (parser->lexer);
13570 
13571   /* If the next token is `operator', then we have either an
13572      operator-function-id or a conversion-function-id.  */
13573   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13574     {
13575       /* We don't know whether we're looking at an
13576 	 operator-function-id or a conversion-function-id.  */
13577       cp_parser_parse_tentatively (parser);
13578       /* Try an operator-function-id.  */
13579       identifier = cp_parser_operator_function_id (parser);
13580       /* If that didn't work, try a conversion-function-id.  */
13581       if (!cp_parser_parse_definitely (parser))
13582 	{
13583 	  cp_parser_error (parser, "expected template-name");
13584 	  return error_mark_node;
13585 	}
13586     }
13587   /* Look for the identifier.  */
13588   else
13589     identifier = cp_parser_identifier (parser);
13590 
13591   /* If we didn't find an identifier, we don't have a template-id.  */
13592   if (identifier == error_mark_node)
13593     return error_mark_node;
13594 
13595   /* If the name immediately followed the `template' keyword, then it
13596      is a template-name.  However, if the next token is not `<', then
13597      we do not treat it as a template-name, since it is not being used
13598      as part of a template-id.  This enables us to handle constructs
13599      like:
13600 
13601        template <typename T> struct S { S(); };
13602        template <typename T> S<T>::S();
13603 
13604      correctly.  We would treat `S' as a template -- if it were `S<T>'
13605      -- but we do not if there is no `<'.  */
13606 
13607   if (processing_template_decl
13608       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13609     {
13610       /* In a declaration, in a dependent context, we pretend that the
13611 	 "template" keyword was present in order to improve error
13612 	 recovery.  For example, given:
13613 
13614 	   template <typename T> void f(T::X<int>);
13615 
13616 	 we want to treat "X<int>" as a template-id.  */
13617       if (is_declaration
13618 	  && !template_keyword_p
13619 	  && parser->scope && TYPE_P (parser->scope)
13620 	  && check_dependency_p
13621 	  && dependent_scope_p (parser->scope)
13622 	  /* Do not do this for dtors (or ctors), since they never
13623 	     need the template keyword before their name.  */
13624 	  && !constructor_name_p (identifier, parser->scope))
13625 	{
13626 	  cp_token_position start = 0;
13627 
13628 	  /* Explain what went wrong.  */
13629 	  error_at (token->location, "non-template %qD used as template",
13630 		    identifier);
13631 	  inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13632 		  parser->scope, identifier);
13633 	  /* If parsing tentatively, find the location of the "<" token.  */
13634 	  if (cp_parser_simulate_error (parser))
13635 	    start = cp_lexer_token_position (parser->lexer, true);
13636 	  /* Parse the template arguments so that we can issue error
13637 	     messages about them.  */
13638 	  cp_lexer_consume_token (parser->lexer);
13639 	  cp_parser_enclosed_template_argument_list (parser);
13640 	  /* Skip tokens until we find a good place from which to
13641 	     continue parsing.  */
13642 	  cp_parser_skip_to_closing_parenthesis (parser,
13643 						 /*recovering=*/true,
13644 						 /*or_comma=*/true,
13645 						 /*consume_paren=*/false);
13646 	  /* If parsing tentatively, permanently remove the
13647 	     template argument list.  That will prevent duplicate
13648 	     error messages from being issued about the missing
13649 	     "template" keyword.  */
13650 	  if (start)
13651 	    cp_lexer_purge_tokens_after (parser->lexer, start);
13652 	  if (is_identifier)
13653 	    *is_identifier = true;
13654 	  return identifier;
13655 	}
13656 
13657       /* If the "template" keyword is present, then there is generally
13658 	 no point in doing name-lookup, so we just return IDENTIFIER.
13659 	 But, if the qualifying scope is non-dependent then we can
13660 	 (and must) do name-lookup normally.  */
13661       if (template_keyword_p
13662 	  && (!parser->scope
13663 	      || (TYPE_P (parser->scope)
13664 		  && dependent_type_p (parser->scope))))
13665 	return identifier;
13666     }
13667 
13668   /* Look up the name.  */
13669   decl = cp_parser_lookup_name (parser, identifier,
13670 				tag_type,
13671 				/*is_template=*/true,
13672 				/*is_namespace=*/false,
13673 				check_dependency_p,
13674 				/*ambiguous_decls=*/NULL,
13675 				token->location);
13676 
13677   /* If DECL is a template, then the name was a template-name.  */
13678   if (TREE_CODE (decl) == TEMPLATE_DECL)
13679     ;
13680   else
13681     {
13682       tree fn = NULL_TREE;
13683 
13684       /* The standard does not explicitly indicate whether a name that
13685 	 names a set of overloaded declarations, some of which are
13686 	 templates, is a template-name.  However, such a name should
13687 	 be a template-name; otherwise, there is no way to form a
13688 	 template-id for the overloaded templates.  */
13689       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13690       if (TREE_CODE (fns) == OVERLOAD)
13691 	for (fn = fns; fn; fn = OVL_NEXT (fn))
13692 	  if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13693 	    break;
13694 
13695       if (!fn)
13696 	{
13697 	  /* The name does not name a template.  */
13698 	  cp_parser_error (parser, "expected template-name");
13699 	  return error_mark_node;
13700 	}
13701     }
13702 
13703   /* If DECL is dependent, and refers to a function, then just return
13704      its name; we will look it up again during template instantiation.  */
13705   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13706     {
13707       tree scope = ovl_scope (decl);
13708       if (TYPE_P (scope) && dependent_type_p (scope))
13709 	return identifier;
13710     }
13711 
13712   return decl;
13713 }
13714 
13715 /* Parse a template-argument-list.
13716 
13717    template-argument-list:
13718      template-argument ... [opt]
13719      template-argument-list , template-argument ... [opt]
13720 
13721    Returns a TREE_VEC containing the arguments.  */
13722 
13723 static tree
cp_parser_template_argument_list(cp_parser * parser)13724 cp_parser_template_argument_list (cp_parser* parser)
13725 {
13726   tree fixed_args[10];
13727   unsigned n_args = 0;
13728   unsigned alloced = 10;
13729   tree *arg_ary = fixed_args;
13730   tree vec;
13731   bool saved_in_template_argument_list_p;
13732   bool saved_ice_p;
13733   bool saved_non_ice_p;
13734 
13735   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13736   parser->in_template_argument_list_p = true;
13737   /* Even if the template-id appears in an integral
13738      constant-expression, the contents of the argument list do
13739      not.  */
13740   saved_ice_p = parser->integral_constant_expression_p;
13741   parser->integral_constant_expression_p = false;
13742   saved_non_ice_p = parser->non_integral_constant_expression_p;
13743   parser->non_integral_constant_expression_p = false;
13744 
13745   /* Parse the arguments.  */
13746   do
13747     {
13748       tree argument;
13749 
13750       if (n_args)
13751 	/* Consume the comma.  */
13752 	cp_lexer_consume_token (parser->lexer);
13753 
13754       /* Parse the template-argument.  */
13755       argument = cp_parser_template_argument (parser);
13756 
13757       /* If the next token is an ellipsis, we're expanding a template
13758          argument pack. */
13759       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13760         {
13761 	  if (argument == error_mark_node)
13762 	    {
13763 	      cp_token *token = cp_lexer_peek_token (parser->lexer);
13764 	      error_at (token->location,
13765 			"expected parameter pack before %<...%>");
13766 	    }
13767           /* Consume the `...' token. */
13768           cp_lexer_consume_token (parser->lexer);
13769 
13770           /* Make the argument into a TYPE_PACK_EXPANSION or
13771              EXPR_PACK_EXPANSION. */
13772           argument = make_pack_expansion (argument);
13773         }
13774 
13775       if (n_args == alloced)
13776 	{
13777 	  alloced *= 2;
13778 
13779 	  if (arg_ary == fixed_args)
13780 	    {
13781 	      arg_ary = XNEWVEC (tree, alloced);
13782 	      memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13783 	    }
13784 	  else
13785 	    arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13786 	}
13787       arg_ary[n_args++] = argument;
13788     }
13789   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13790 
13791   vec = make_tree_vec (n_args);
13792 
13793   while (n_args--)
13794     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13795 
13796   if (arg_ary != fixed_args)
13797     free (arg_ary);
13798   parser->non_integral_constant_expression_p = saved_non_ice_p;
13799   parser->integral_constant_expression_p = saved_ice_p;
13800   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13801 #ifdef ENABLE_CHECKING
13802   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13803 #endif
13804   return vec;
13805 }
13806 
13807 /* Parse a template-argument.
13808 
13809    template-argument:
13810      assignment-expression
13811      type-id
13812      id-expression
13813 
13814    The representation is that of an assignment-expression, type-id, or
13815    id-expression -- except that the qualified id-expression is
13816    evaluated, so that the value returned is either a DECL or an
13817    OVERLOAD.
13818 
13819    Although the standard says "assignment-expression", it forbids
13820    throw-expressions or assignments in the template argument.
13821    Therefore, we use "conditional-expression" instead.  */
13822 
13823 static tree
cp_parser_template_argument(cp_parser * parser)13824 cp_parser_template_argument (cp_parser* parser)
13825 {
13826   tree argument;
13827   bool template_p;
13828   bool address_p;
13829   bool maybe_type_id = false;
13830   cp_token *token = NULL, *argument_start_token = NULL;
13831   location_t loc = 0;
13832   cp_id_kind idk;
13833 
13834   /* There's really no way to know what we're looking at, so we just
13835      try each alternative in order.
13836 
13837        [temp.arg]
13838 
13839        In a template-argument, an ambiguity between a type-id and an
13840        expression is resolved to a type-id, regardless of the form of
13841        the corresponding template-parameter.
13842 
13843      Therefore, we try a type-id first.  */
13844   cp_parser_parse_tentatively (parser);
13845   argument = cp_parser_template_type_arg (parser);
13846   /* If there was no error parsing the type-id but the next token is a
13847      '>>', our behavior depends on which dialect of C++ we're
13848      parsing. In C++98, we probably found a typo for '> >'. But there
13849      are type-id which are also valid expressions. For instance:
13850 
13851      struct X { int operator >> (int); };
13852      template <int V> struct Foo {};
13853      Foo<X () >> 5> r;
13854 
13855      Here 'X()' is a valid type-id of a function type, but the user just
13856      wanted to write the expression "X() >> 5". Thus, we remember that we
13857      found a valid type-id, but we still try to parse the argument as an
13858      expression to see what happens.
13859 
13860      In C++0x, the '>>' will be considered two separate '>'
13861      tokens.  */
13862   if (!cp_parser_error_occurred (parser)
13863       && cxx_dialect == cxx98
13864       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13865     {
13866       maybe_type_id = true;
13867       cp_parser_abort_tentative_parse (parser);
13868     }
13869   else
13870     {
13871       /* If the next token isn't a `,' or a `>', then this argument wasn't
13872       really finished. This means that the argument is not a valid
13873       type-id.  */
13874       if (!cp_parser_next_token_ends_template_argument_p (parser))
13875 	cp_parser_error (parser, "expected template-argument");
13876       /* If that worked, we're done.  */
13877       if (cp_parser_parse_definitely (parser))
13878 	return argument;
13879     }
13880   /* We're still not sure what the argument will be.  */
13881   cp_parser_parse_tentatively (parser);
13882   /* Try a template.  */
13883   argument_start_token = cp_lexer_peek_token (parser->lexer);
13884   argument = cp_parser_id_expression (parser,
13885 				      /*template_keyword_p=*/false,
13886 				      /*check_dependency_p=*/true,
13887 				      &template_p,
13888 				      /*declarator_p=*/false,
13889 				      /*optional_p=*/false);
13890   /* If the next token isn't a `,' or a `>', then this argument wasn't
13891      really finished.  */
13892   if (!cp_parser_next_token_ends_template_argument_p (parser))
13893     cp_parser_error (parser, "expected template-argument");
13894   if (!cp_parser_error_occurred (parser))
13895     {
13896       /* Figure out what is being referred to.  If the id-expression
13897 	 was for a class template specialization, then we will have a
13898 	 TYPE_DECL at this point.  There is no need to do name lookup
13899 	 at this point in that case.  */
13900       if (TREE_CODE (argument) != TYPE_DECL)
13901 	argument = cp_parser_lookup_name (parser, argument,
13902 					  none_type,
13903 					  /*is_template=*/template_p,
13904 					  /*is_namespace=*/false,
13905 					  /*check_dependency=*/true,
13906 					  /*ambiguous_decls=*/NULL,
13907 					  argument_start_token->location);
13908       if (TREE_CODE (argument) != TEMPLATE_DECL
13909 	  && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13910 	cp_parser_error (parser, "expected template-name");
13911     }
13912   if (cp_parser_parse_definitely (parser))
13913     return argument;
13914   /* It must be a non-type argument.  There permitted cases are given
13915      in [temp.arg.nontype]:
13916 
13917      -- an integral constant-expression of integral or enumeration
13918 	type; or
13919 
13920      -- the name of a non-type template-parameter; or
13921 
13922      -- the name of an object or function with external linkage...
13923 
13924      -- the address of an object or function with external linkage...
13925 
13926      -- a pointer to member...  */
13927   /* Look for a non-type template parameter.  */
13928   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13929     {
13930       cp_parser_parse_tentatively (parser);
13931       argument = cp_parser_primary_expression (parser,
13932 					       /*address_p=*/false,
13933 					       /*cast_p=*/false,
13934 					       /*template_arg_p=*/true,
13935 					       &idk);
13936       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13937 	  || !cp_parser_next_token_ends_template_argument_p (parser))
13938 	cp_parser_simulate_error (parser);
13939       if (cp_parser_parse_definitely (parser))
13940 	return argument;
13941     }
13942 
13943   /* If the next token is "&", the argument must be the address of an
13944      object or function with external linkage.  */
13945   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13946   if (address_p)
13947     {
13948       loc = cp_lexer_peek_token (parser->lexer)->location;
13949       cp_lexer_consume_token (parser->lexer);
13950     }
13951   /* See if we might have an id-expression.  */
13952   token = cp_lexer_peek_token (parser->lexer);
13953   if (token->type == CPP_NAME
13954       || token->keyword == RID_OPERATOR
13955       || token->type == CPP_SCOPE
13956       || token->type == CPP_TEMPLATE_ID
13957       || token->type == CPP_NESTED_NAME_SPECIFIER)
13958     {
13959       cp_parser_parse_tentatively (parser);
13960       argument = cp_parser_primary_expression (parser,
13961 					       address_p,
13962 					       /*cast_p=*/false,
13963 					       /*template_arg_p=*/true,
13964 					       &idk);
13965       if (cp_parser_error_occurred (parser)
13966 	  || !cp_parser_next_token_ends_template_argument_p (parser))
13967 	cp_parser_abort_tentative_parse (parser);
13968       else
13969 	{
13970 	  tree probe;
13971 
13972 	  if (INDIRECT_REF_P (argument))
13973 	    {
13974 	      /* Strip the dereference temporarily.  */
13975 	      gcc_assert (REFERENCE_REF_P (argument));
13976 	      argument = TREE_OPERAND (argument, 0);
13977 	    }
13978 
13979 	  /* If we're in a template, we represent a qualified-id referring
13980 	     to a static data member as a SCOPE_REF even if the scope isn't
13981 	     dependent so that we can check access control later.  */
13982 	  probe = argument;
13983 	  if (TREE_CODE (probe) == SCOPE_REF)
13984 	    probe = TREE_OPERAND (probe, 1);
13985 	  if (VAR_P (probe))
13986 	    {
13987 	      /* A variable without external linkage might still be a
13988 		 valid constant-expression, so no error is issued here
13989 		 if the external-linkage check fails.  */
13990 	      if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13991 		cp_parser_simulate_error (parser);
13992 	    }
13993 	  else if (is_overloaded_fn (argument))
13994 	    /* All overloaded functions are allowed; if the external
13995 	       linkage test does not pass, an error will be issued
13996 	       later.  */
13997 	    ;
13998 	  else if (address_p
13999 		   && (TREE_CODE (argument) == OFFSET_REF
14000 		       || TREE_CODE (argument) == SCOPE_REF))
14001 	    /* A pointer-to-member.  */
14002 	    ;
14003 	  else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14004 	    ;
14005 	  else
14006 	    cp_parser_simulate_error (parser);
14007 
14008 	  if (cp_parser_parse_definitely (parser))
14009 	    {
14010 	      if (address_p)
14011 		argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14012 					     tf_warning_or_error);
14013 	      else
14014 		argument = convert_from_reference (argument);
14015 	      return argument;
14016 	    }
14017 	}
14018     }
14019   /* If the argument started with "&", there are no other valid
14020      alternatives at this point.  */
14021   if (address_p)
14022     {
14023       cp_parser_error (parser, "invalid non-type template argument");
14024       return error_mark_node;
14025     }
14026 
14027   /* If the argument wasn't successfully parsed as a type-id followed
14028      by '>>', the argument can only be a constant expression now.
14029      Otherwise, we try parsing the constant-expression tentatively,
14030      because the argument could really be a type-id.  */
14031   if (maybe_type_id)
14032     cp_parser_parse_tentatively (parser);
14033   argument = cp_parser_constant_expression (parser,
14034 					    /*allow_non_constant_p=*/false,
14035 					    /*non_constant_p=*/NULL);
14036   if (!maybe_type_id)
14037     return argument;
14038   if (!cp_parser_next_token_ends_template_argument_p (parser))
14039     cp_parser_error (parser, "expected template-argument");
14040   if (cp_parser_parse_definitely (parser))
14041     return argument;
14042   /* We did our best to parse the argument as a non type-id, but that
14043      was the only alternative that matched (albeit with a '>' after
14044      it). We can assume it's just a typo from the user, and a
14045      diagnostic will then be issued.  */
14046   return cp_parser_template_type_arg (parser);
14047 }
14048 
14049 /* Parse an explicit-instantiation.
14050 
14051    explicit-instantiation:
14052      template declaration
14053 
14054    Although the standard says `declaration', what it really means is:
14055 
14056    explicit-instantiation:
14057      template decl-specifier-seq [opt] declarator [opt] ;
14058 
14059    Things like `template int S<int>::i = 5, int S<double>::j;' are not
14060    supposed to be allowed.  A defect report has been filed about this
14061    issue.
14062 
14063    GNU Extension:
14064 
14065    explicit-instantiation:
14066      storage-class-specifier template
14067        decl-specifier-seq [opt] declarator [opt] ;
14068      function-specifier template
14069        decl-specifier-seq [opt] declarator [opt] ;  */
14070 
14071 static void
cp_parser_explicit_instantiation(cp_parser * parser)14072 cp_parser_explicit_instantiation (cp_parser* parser)
14073 {
14074   int declares_class_or_enum;
14075   cp_decl_specifier_seq decl_specifiers;
14076   tree extension_specifier = NULL_TREE;
14077 
14078   timevar_push (TV_TEMPLATE_INST);
14079 
14080   /* Look for an (optional) storage-class-specifier or
14081      function-specifier.  */
14082   if (cp_parser_allow_gnu_extensions_p (parser))
14083     {
14084       extension_specifier
14085 	= cp_parser_storage_class_specifier_opt (parser);
14086       if (!extension_specifier)
14087 	extension_specifier
14088 	  = cp_parser_function_specifier_opt (parser,
14089 					      /*decl_specs=*/NULL);
14090     }
14091 
14092   /* Look for the `template' keyword.  */
14093   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14094   /* Let the front end know that we are processing an explicit
14095      instantiation.  */
14096   begin_explicit_instantiation ();
14097   /* [temp.explicit] says that we are supposed to ignore access
14098      control while processing explicit instantiation directives.  */
14099   push_deferring_access_checks (dk_no_check);
14100   /* Parse a decl-specifier-seq.  */
14101   cp_parser_decl_specifier_seq (parser,
14102 				CP_PARSER_FLAGS_OPTIONAL,
14103 				&decl_specifiers,
14104 				&declares_class_or_enum);
14105   /* If there was exactly one decl-specifier, and it declared a class,
14106      and there's no declarator, then we have an explicit type
14107      instantiation.  */
14108   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14109     {
14110       tree type;
14111 
14112       type = check_tag_decl (&decl_specifiers,
14113 			     /*explicit_type_instantiation_p=*/true);
14114       /* Turn access control back on for names used during
14115 	 template instantiation.  */
14116       pop_deferring_access_checks ();
14117       if (type)
14118 	do_type_instantiation (type, extension_specifier,
14119 			       /*complain=*/tf_error);
14120     }
14121   else
14122     {
14123       cp_declarator *declarator;
14124       tree decl;
14125 
14126       /* Parse the declarator.  */
14127       declarator
14128 	= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14129 				/*ctor_dtor_or_conv_p=*/NULL,
14130 				/*parenthesized_p=*/NULL,
14131 				/*member_p=*/false);
14132       if (declares_class_or_enum & 2)
14133 	cp_parser_check_for_definition_in_return_type (declarator,
14134 						       decl_specifiers.type,
14135 						       decl_specifiers.locations[ds_type_spec]);
14136       if (declarator != cp_error_declarator)
14137 	{
14138 	  if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14139 	    permerror (decl_specifiers.locations[ds_inline],
14140 		       "explicit instantiation shall not use"
14141 		       " %<inline%> specifier");
14142 	  if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14143 	    permerror (decl_specifiers.locations[ds_constexpr],
14144 		       "explicit instantiation shall not use"
14145 		       " %<constexpr%> specifier");
14146 
14147 	  decl = grokdeclarator (declarator, &decl_specifiers,
14148 				 NORMAL, 0, &decl_specifiers.attributes);
14149 	  /* Turn access control back on for names used during
14150 	     template instantiation.  */
14151 	  pop_deferring_access_checks ();
14152 	  /* Do the explicit instantiation.  */
14153 	  do_decl_instantiation (decl, extension_specifier);
14154 	}
14155       else
14156 	{
14157 	  pop_deferring_access_checks ();
14158 	  /* Skip the body of the explicit instantiation.  */
14159 	  cp_parser_skip_to_end_of_statement (parser);
14160 	}
14161     }
14162   /* We're done with the instantiation.  */
14163   end_explicit_instantiation ();
14164 
14165   cp_parser_consume_semicolon_at_end_of_statement (parser);
14166 
14167   timevar_pop (TV_TEMPLATE_INST);
14168 }
14169 
14170 /* Parse an explicit-specialization.
14171 
14172    explicit-specialization:
14173      template < > declaration
14174 
14175    Although the standard says `declaration', what it really means is:
14176 
14177    explicit-specialization:
14178      template <> decl-specifier [opt] init-declarator [opt] ;
14179      template <> function-definition
14180      template <> explicit-specialization
14181      template <> template-declaration  */
14182 
14183 static void
cp_parser_explicit_specialization(cp_parser * parser)14184 cp_parser_explicit_specialization (cp_parser* parser)
14185 {
14186   bool need_lang_pop;
14187   cp_token *token = cp_lexer_peek_token (parser->lexer);
14188 
14189   /* Look for the `template' keyword.  */
14190   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14191   /* Look for the `<'.  */
14192   cp_parser_require (parser, CPP_LESS, RT_LESS);
14193   /* Look for the `>'.  */
14194   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14195   /* We have processed another parameter list.  */
14196   ++parser->num_template_parameter_lists;
14197   /* [temp]
14198 
14199      A template ... explicit specialization ... shall not have C
14200      linkage.  */
14201   if (current_lang_name == lang_name_c)
14202     {
14203       error_at (token->location, "template specialization with C linkage");
14204       /* Give it C++ linkage to avoid confusing other parts of the
14205 	 front end.  */
14206       push_lang_context (lang_name_cplusplus);
14207       need_lang_pop = true;
14208     }
14209   else
14210     need_lang_pop = false;
14211   /* Let the front end know that we are beginning a specialization.  */
14212   if (!begin_specialization ())
14213     {
14214       end_specialization ();
14215       return;
14216     }
14217 
14218   /* If the next keyword is `template', we need to figure out whether
14219      or not we're looking a template-declaration.  */
14220   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14221     {
14222       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14223 	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14224 	cp_parser_template_declaration_after_export (parser,
14225 						     /*member_p=*/false);
14226       else
14227 	cp_parser_explicit_specialization (parser);
14228     }
14229   else
14230     /* Parse the dependent declaration.  */
14231     cp_parser_single_declaration (parser,
14232 				  /*checks=*/NULL,
14233 				  /*member_p=*/false,
14234 				  /*explicit_specialization_p=*/true,
14235 				  /*friend_p=*/NULL);
14236   /* We're done with the specialization.  */
14237   end_specialization ();
14238   /* For the erroneous case of a template with C linkage, we pushed an
14239      implicit C++ linkage scope; exit that scope now.  */
14240   if (need_lang_pop)
14241     pop_lang_context ();
14242   /* We're done with this parameter list.  */
14243   --parser->num_template_parameter_lists;
14244 }
14245 
14246 /* Parse a type-specifier.
14247 
14248    type-specifier:
14249      simple-type-specifier
14250      class-specifier
14251      enum-specifier
14252      elaborated-type-specifier
14253      cv-qualifier
14254 
14255    GNU Extension:
14256 
14257    type-specifier:
14258      __complex__
14259 
14260    Returns a representation of the type-specifier.  For a
14261    class-specifier, enum-specifier, or elaborated-type-specifier, a
14262    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14263 
14264    The parser flags FLAGS is used to control type-specifier parsing.
14265 
14266    If IS_DECLARATION is TRUE, then this type-specifier is appearing
14267    in a decl-specifier-seq.
14268 
14269    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14270    class-specifier, enum-specifier, or elaborated-type-specifier, then
14271    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
14272    if a type is declared; 2 if it is defined.  Otherwise, it is set to
14273    zero.
14274 
14275    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14276    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
14277    is set to FALSE.  */
14278 
14279 static tree
cp_parser_type_specifier(cp_parser * parser,cp_parser_flags flags,cp_decl_specifier_seq * decl_specs,bool is_declaration,int * declares_class_or_enum,bool * is_cv_qualifier)14280 cp_parser_type_specifier (cp_parser* parser,
14281 			  cp_parser_flags flags,
14282 			  cp_decl_specifier_seq *decl_specs,
14283 			  bool is_declaration,
14284 			  int* declares_class_or_enum,
14285 			  bool* is_cv_qualifier)
14286 {
14287   tree type_spec = NULL_TREE;
14288   cp_token *token;
14289   enum rid keyword;
14290   cp_decl_spec ds = ds_last;
14291 
14292   /* Assume this type-specifier does not declare a new type.  */
14293   if (declares_class_or_enum)
14294     *declares_class_or_enum = 0;
14295   /* And that it does not specify a cv-qualifier.  */
14296   if (is_cv_qualifier)
14297     *is_cv_qualifier = false;
14298   /* Peek at the next token.  */
14299   token = cp_lexer_peek_token (parser->lexer);
14300 
14301   /* If we're looking at a keyword, we can use that to guide the
14302      production we choose.  */
14303   keyword = token->keyword;
14304   switch (keyword)
14305     {
14306     case RID_ENUM:
14307       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14308 	goto elaborated_type_specifier;
14309 
14310       /* Look for the enum-specifier.  */
14311       type_spec = cp_parser_enum_specifier (parser);
14312       /* If that worked, we're done.  */
14313       if (type_spec)
14314 	{
14315 	  if (declares_class_or_enum)
14316 	    *declares_class_or_enum = 2;
14317 	  if (decl_specs)
14318 	    cp_parser_set_decl_spec_type (decl_specs,
14319 					  type_spec,
14320 					  token,
14321 					  /*type_definition_p=*/true);
14322 	  return type_spec;
14323 	}
14324       else
14325 	goto elaborated_type_specifier;
14326 
14327       /* Any of these indicate either a class-specifier, or an
14328 	 elaborated-type-specifier.  */
14329     case RID_CLASS:
14330     case RID_STRUCT:
14331     case RID_UNION:
14332       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14333 	goto elaborated_type_specifier;
14334 
14335       /* Parse tentatively so that we can back up if we don't find a
14336 	 class-specifier.  */
14337       cp_parser_parse_tentatively (parser);
14338       /* Look for the class-specifier.  */
14339       type_spec = cp_parser_class_specifier (parser);
14340       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14341       /* If that worked, we're done.  */
14342       if (cp_parser_parse_definitely (parser))
14343 	{
14344 	  if (declares_class_or_enum)
14345 	    *declares_class_or_enum = 2;
14346 	  if (decl_specs)
14347 	    cp_parser_set_decl_spec_type (decl_specs,
14348 					  type_spec,
14349 					  token,
14350 					  /*type_definition_p=*/true);
14351 	  return type_spec;
14352 	}
14353 
14354       /* Fall through.  */
14355     elaborated_type_specifier:
14356       /* We're declaring (not defining) a class or enum.  */
14357       if (declares_class_or_enum)
14358 	*declares_class_or_enum = 1;
14359 
14360       /* Fall through.  */
14361     case RID_TYPENAME:
14362       /* Look for an elaborated-type-specifier.  */
14363       type_spec
14364 	= (cp_parser_elaborated_type_specifier
14365 	   (parser,
14366 	    decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14367 	    is_declaration));
14368       if (decl_specs)
14369 	cp_parser_set_decl_spec_type (decl_specs,
14370 				      type_spec,
14371 				      token,
14372 				      /*type_definition_p=*/false);
14373       return type_spec;
14374 
14375     case RID_CONST:
14376       ds = ds_const;
14377       if (is_cv_qualifier)
14378 	*is_cv_qualifier = true;
14379       break;
14380 
14381     case RID_VOLATILE:
14382       ds = ds_volatile;
14383       if (is_cv_qualifier)
14384 	*is_cv_qualifier = true;
14385       break;
14386 
14387     case RID_RESTRICT:
14388       ds = ds_restrict;
14389       if (is_cv_qualifier)
14390 	*is_cv_qualifier = true;
14391       break;
14392 
14393     case RID_COMPLEX:
14394       /* The `__complex__' keyword is a GNU extension.  */
14395       ds = ds_complex;
14396       break;
14397 
14398     default:
14399       break;
14400     }
14401 
14402   /* Handle simple keywords.  */
14403   if (ds != ds_last)
14404     {
14405       if (decl_specs)
14406 	{
14407 	  set_and_check_decl_spec_loc (decl_specs, ds, token);
14408 	  decl_specs->any_specifiers_p = true;
14409 	}
14410       return cp_lexer_consume_token (parser->lexer)->u.value;
14411     }
14412 
14413   /* If we do not already have a type-specifier, assume we are looking
14414      at a simple-type-specifier.  */
14415   type_spec = cp_parser_simple_type_specifier (parser,
14416 					       decl_specs,
14417 					       flags);
14418 
14419   /* If we didn't find a type-specifier, and a type-specifier was not
14420      optional in this context, issue an error message.  */
14421   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14422     {
14423       cp_parser_error (parser, "expected type specifier");
14424       return error_mark_node;
14425     }
14426 
14427   return type_spec;
14428 }
14429 
14430 /* Parse a simple-type-specifier.
14431 
14432    simple-type-specifier:
14433      :: [opt] nested-name-specifier [opt] type-name
14434      :: [opt] nested-name-specifier template template-id
14435      char
14436      wchar_t
14437      bool
14438      short
14439      int
14440      long
14441      signed
14442      unsigned
14443      float
14444      double
14445      void
14446 
14447    C++0x Extension:
14448 
14449    simple-type-specifier:
14450      auto
14451      decltype ( expression )
14452      char16_t
14453      char32_t
14454      __underlying_type ( type-id )
14455 
14456    GNU Extension:
14457 
14458    simple-type-specifier:
14459      __int128
14460      __typeof__ unary-expression
14461      __typeof__ ( type-id )
14462      __typeof__ ( type-id ) { initializer-list , [opt] }
14463 
14464    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
14465    appropriately updated.  */
14466 
14467 static tree
cp_parser_simple_type_specifier(cp_parser * parser,cp_decl_specifier_seq * decl_specs,cp_parser_flags flags)14468 cp_parser_simple_type_specifier (cp_parser* parser,
14469 				 cp_decl_specifier_seq *decl_specs,
14470 				 cp_parser_flags flags)
14471 {
14472   tree type = NULL_TREE;
14473   cp_token *token;
14474 
14475   /* Peek at the next token.  */
14476   token = cp_lexer_peek_token (parser->lexer);
14477 
14478   /* If we're looking at a keyword, things are easy.  */
14479   switch (token->keyword)
14480     {
14481     case RID_CHAR:
14482       if (decl_specs)
14483 	decl_specs->explicit_char_p = true;
14484       type = char_type_node;
14485       break;
14486     case RID_CHAR16:
14487       type = char16_type_node;
14488       break;
14489     case RID_CHAR32:
14490       type = char32_type_node;
14491       break;
14492     case RID_WCHAR:
14493       type = wchar_type_node;
14494       break;
14495     case RID_BOOL:
14496       type = boolean_type_node;
14497       break;
14498     case RID_SHORT:
14499       set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14500       type = short_integer_type_node;
14501       break;
14502     case RID_INT:
14503       if (decl_specs)
14504 	decl_specs->explicit_int_p = true;
14505       type = integer_type_node;
14506       break;
14507     case RID_INT128:
14508       if (!int128_integer_type_node)
14509 	break;
14510       if (decl_specs)
14511         decl_specs->explicit_int128_p = true;
14512       type = int128_integer_type_node;
14513       break;
14514     case RID_LONG:
14515       if (decl_specs)
14516 	set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14517       type = long_integer_type_node;
14518       break;
14519     case RID_SIGNED:
14520       set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14521       type = integer_type_node;
14522       break;
14523     case RID_UNSIGNED:
14524       set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14525       type = unsigned_type_node;
14526       break;
14527     case RID_FLOAT:
14528       type = float_type_node;
14529       break;
14530     case RID_DOUBLE:
14531       type = double_type_node;
14532       break;
14533     case RID_VOID:
14534       type = void_type_node;
14535       break;
14536 
14537     case RID_AUTO:
14538       maybe_warn_cpp0x (CPP0X_AUTO);
14539       if (parser->auto_is_implicit_function_template_parm_p)
14540 	{
14541 	  type = synthesize_implicit_template_parm (parser);
14542 
14543 	  if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14544 	    {
14545 	      if (cxx_dialect < cxx1y)
14546 		pedwarn (location_of (type), 0,
14547 			 "use of %<auto%> in lambda parameter declaration "
14548 			 "only available with "
14549 			 "-std=c++1y or -std=gnu++1y");
14550 	    }
14551 	  else if (cxx_dialect < cxx1y)
14552 	    pedwarn (location_of (type), 0,
14553 		     "use of %<auto%> in parameter declaration "
14554 		     "only available with "
14555 		     "-std=c++1y or -std=gnu++1y");
14556 	  else
14557 	    pedwarn (location_of (type), OPT_Wpedantic,
14558 		     "ISO C++ forbids use of %<auto%> in parameter "
14559 		     "declaration");
14560 	}
14561       else
14562 	type = make_auto ();
14563       break;
14564 
14565     case RID_DECLTYPE:
14566       /* Since DR 743, decltype can either be a simple-type-specifier by
14567 	 itself or begin a nested-name-specifier.  Parsing it will replace
14568 	 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14569 	 handling below decide what to do.  */
14570       cp_parser_decltype (parser);
14571       cp_lexer_set_token_position (parser->lexer, token);
14572       break;
14573 
14574     case RID_TYPEOF:
14575       /* Consume the `typeof' token.  */
14576       cp_lexer_consume_token (parser->lexer);
14577       /* Parse the operand to `typeof'.  */
14578       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14579       /* If it is not already a TYPE, take its type.  */
14580       if (!TYPE_P (type))
14581 	type = finish_typeof (type);
14582 
14583       if (decl_specs)
14584 	cp_parser_set_decl_spec_type (decl_specs, type,
14585 				      token,
14586 				      /*type_definition_p=*/false);
14587 
14588       return type;
14589 
14590     case RID_UNDERLYING_TYPE:
14591       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14592       if (decl_specs)
14593 	cp_parser_set_decl_spec_type (decl_specs, type,
14594 				      token,
14595 				      /*type_definition_p=*/false);
14596 
14597       return type;
14598 
14599     case RID_BASES:
14600     case RID_DIRECT_BASES:
14601       type = cp_parser_trait_expr (parser, token->keyword);
14602       if (decl_specs)
14603        cp_parser_set_decl_spec_type (decl_specs, type,
14604                                      token,
14605                                      /*type_definition_p=*/false);
14606       return type;
14607     default:
14608       break;
14609     }
14610 
14611   /* If token is an already-parsed decltype not followed by ::,
14612      it's a simple-type-specifier.  */
14613   if (token->type == CPP_DECLTYPE
14614       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14615     {
14616       type = token->u.value;
14617       if (decl_specs)
14618 	cp_parser_set_decl_spec_type (decl_specs, type,
14619 				      token,
14620 				      /*type_definition_p=*/false);
14621       cp_lexer_consume_token (parser->lexer);
14622       return type;
14623     }
14624 
14625   /* If the type-specifier was for a built-in type, we're done.  */
14626   if (type)
14627     {
14628       /* Record the type.  */
14629       if (decl_specs
14630 	  && (token->keyword != RID_SIGNED
14631 	      && token->keyword != RID_UNSIGNED
14632 	      && token->keyword != RID_SHORT
14633 	      && token->keyword != RID_LONG))
14634 	cp_parser_set_decl_spec_type (decl_specs,
14635 				      type,
14636 				      token,
14637 				      /*type_definition_p=*/false);
14638       if (decl_specs)
14639 	decl_specs->any_specifiers_p = true;
14640 
14641       /* Consume the token.  */
14642       cp_lexer_consume_token (parser->lexer);
14643 
14644       /* There is no valid C++ program where a non-template type is
14645 	 followed by a "<".  That usually indicates that the user thought
14646 	 that the type was a template.  */
14647       cp_parser_check_for_invalid_template_id (parser, type, none_type,
14648 					       token->location);
14649 
14650       return TYPE_NAME (type);
14651     }
14652 
14653   /* The type-specifier must be a user-defined type.  */
14654   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14655     {
14656       bool qualified_p;
14657       bool global_p;
14658 
14659       /* Don't gobble tokens or issue error messages if this is an
14660 	 optional type-specifier.  */
14661       if (flags & CP_PARSER_FLAGS_OPTIONAL)
14662 	cp_parser_parse_tentatively (parser);
14663 
14664       /* Look for the optional `::' operator.  */
14665       global_p
14666 	= (cp_parser_global_scope_opt (parser,
14667 				       /*current_scope_valid_p=*/false)
14668 	   != NULL_TREE);
14669       /* Look for the nested-name specifier.  */
14670       qualified_p
14671 	= (cp_parser_nested_name_specifier_opt (parser,
14672 						/*typename_keyword_p=*/false,
14673 						/*check_dependency_p=*/true,
14674 						/*type_p=*/false,
14675 						/*is_declaration=*/false)
14676 	   != NULL_TREE);
14677       token = cp_lexer_peek_token (parser->lexer);
14678       /* If we have seen a nested-name-specifier, and the next token
14679 	 is `template', then we are using the template-id production.  */
14680       if (parser->scope
14681 	  && cp_parser_optional_template_keyword (parser))
14682 	{
14683 	  /* Look for the template-id.  */
14684 	  type = cp_parser_template_id (parser,
14685 					/*template_keyword_p=*/true,
14686 					/*check_dependency_p=*/true,
14687 					none_type,
14688 					/*is_declaration=*/false);
14689 	  /* If the template-id did not name a type, we are out of
14690 	     luck.  */
14691 	  if (TREE_CODE (type) != TYPE_DECL)
14692 	    {
14693 	      cp_parser_error (parser, "expected template-id for type");
14694 	      type = NULL_TREE;
14695 	    }
14696 	}
14697       /* Otherwise, look for a type-name.  */
14698       else
14699 	type = cp_parser_type_name (parser);
14700       /* Keep track of all name-lookups performed in class scopes.  */
14701       if (type
14702 	  && !global_p
14703 	  && !qualified_p
14704 	  && TREE_CODE (type) == TYPE_DECL
14705 	  && identifier_p (DECL_NAME (type)))
14706 	maybe_note_name_used_in_class (DECL_NAME (type), type);
14707       /* If it didn't work out, we don't have a TYPE.  */
14708       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14709 	  && !cp_parser_parse_definitely (parser))
14710 	type = NULL_TREE;
14711       if (type && decl_specs)
14712 	cp_parser_set_decl_spec_type (decl_specs, type,
14713 				      token,
14714 				      /*type_definition_p=*/false);
14715     }
14716 
14717   /* If we didn't get a type-name, issue an error message.  */
14718   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14719     {
14720       cp_parser_error (parser, "expected type-name");
14721       return error_mark_node;
14722     }
14723 
14724   if (type && type != error_mark_node)
14725     {
14726       /* See if TYPE is an Objective-C type, and if so, parse and
14727 	 accept any protocol references following it.  Do this before
14728 	 the cp_parser_check_for_invalid_template_id() call, because
14729 	 Objective-C types can be followed by '<...>' which would
14730 	 enclose protocol names rather than template arguments, and so
14731 	 everything is fine.  */
14732       if (c_dialect_objc () && !parser->scope
14733 	  && (objc_is_id (type) || objc_is_class_name (type)))
14734 	{
14735 	  tree protos = cp_parser_objc_protocol_refs_opt (parser);
14736 	  tree qual_type = objc_get_protocol_qualified_type (type, protos);
14737 
14738 	  /* Clobber the "unqualified" type previously entered into
14739 	     DECL_SPECS with the new, improved protocol-qualified version.  */
14740 	  if (decl_specs)
14741 	    decl_specs->type = qual_type;
14742 
14743 	  return qual_type;
14744 	}
14745 
14746       /* There is no valid C++ program where a non-template type is
14747 	 followed by a "<".  That usually indicates that the user
14748 	 thought that the type was a template.  */
14749       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14750 					       none_type,
14751 					       token->location);
14752     }
14753 
14754   return type;
14755 }
14756 
14757 /* Parse a type-name.
14758 
14759    type-name:
14760      class-name
14761      enum-name
14762      typedef-name
14763      simple-template-id [in c++0x]
14764 
14765    enum-name:
14766      identifier
14767 
14768    typedef-name:
14769      identifier
14770 
14771    Returns a TYPE_DECL for the type.  */
14772 
14773 static tree
cp_parser_type_name(cp_parser * parser)14774 cp_parser_type_name (cp_parser* parser)
14775 {
14776   tree type_decl;
14777 
14778   /* We can't know yet whether it is a class-name or not.  */
14779   cp_parser_parse_tentatively (parser);
14780   /* Try a class-name.  */
14781   type_decl = cp_parser_class_name (parser,
14782 				    /*typename_keyword_p=*/false,
14783 				    /*template_keyword_p=*/false,
14784 				    none_type,
14785 				    /*check_dependency_p=*/true,
14786 				    /*class_head_p=*/false,
14787 				    /*is_declaration=*/false);
14788   /* If it's not a class-name, keep looking.  */
14789   if (!cp_parser_parse_definitely (parser))
14790     {
14791       if (cxx_dialect < cxx11)
14792 	/* It must be a typedef-name or an enum-name.  */
14793 	return cp_parser_nonclass_name (parser);
14794 
14795       cp_parser_parse_tentatively (parser);
14796       /* It is either a simple-template-id representing an
14797 	 instantiation of an alias template...  */
14798       type_decl = cp_parser_template_id (parser,
14799 					 /*template_keyword_p=*/false,
14800 					 /*check_dependency_p=*/true,
14801 					 none_type,
14802 					 /*is_declaration=*/false);
14803       /* Note that this must be an instantiation of an alias template
14804 	 because [temp.names]/6 says:
14805 
14806 	     A template-id that names an alias template specialization
14807 	     is a type-name.
14808 
14809 	 Whereas [temp.names]/7 says:
14810 
14811 	     A simple-template-id that names a class template
14812 	     specialization is a class-name.  */
14813       if (type_decl != NULL_TREE
14814 	  && TREE_CODE (type_decl) == TYPE_DECL
14815 	  && TYPE_DECL_ALIAS_P (type_decl))
14816 	gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14817       else
14818 	cp_parser_simulate_error (parser);
14819 
14820       if (!cp_parser_parse_definitely (parser))
14821 	/* ... Or a typedef-name or an enum-name.  */
14822 	return cp_parser_nonclass_name (parser);
14823     }
14824 
14825   return type_decl;
14826 }
14827 
14828 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14829 
14830    enum-name:
14831      identifier
14832 
14833    typedef-name:
14834      identifier
14835 
14836    Returns a TYPE_DECL for the type.  */
14837 
14838 static tree
cp_parser_nonclass_name(cp_parser * parser)14839 cp_parser_nonclass_name (cp_parser* parser)
14840 {
14841   tree type_decl;
14842   tree identifier;
14843 
14844   cp_token *token = cp_lexer_peek_token (parser->lexer);
14845   identifier = cp_parser_identifier (parser);
14846   if (identifier == error_mark_node)
14847     return error_mark_node;
14848 
14849   /* Look up the type-name.  */
14850   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14851 
14852   type_decl = strip_using_decl (type_decl);
14853 
14854   if (TREE_CODE (type_decl) != TYPE_DECL
14855       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14856     {
14857       /* See if this is an Objective-C type.  */
14858       tree protos = cp_parser_objc_protocol_refs_opt (parser);
14859       tree type = objc_get_protocol_qualified_type (identifier, protos);
14860       if (type)
14861 	type_decl = TYPE_NAME (type);
14862     }
14863 
14864   /* Issue an error if we did not find a type-name.  */
14865   if (TREE_CODE (type_decl) != TYPE_DECL
14866       /* In Objective-C, we have the complication that class names are
14867 	 normally type names and start declarations (eg, the
14868 	 "NSObject" in "NSObject *object;"), but can be used in an
14869 	 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14870 	 is an expression.  So, a classname followed by a dot is not a
14871 	 valid type-name.  */
14872       || (objc_is_class_name (TREE_TYPE (type_decl))
14873 	  && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14874     {
14875       if (!cp_parser_simulate_error (parser))
14876 	cp_parser_name_lookup_error (parser, identifier, type_decl,
14877 				     NLE_TYPE, token->location);
14878       return error_mark_node;
14879     }
14880   /* Remember that the name was used in the definition of the
14881      current class so that we can check later to see if the
14882      meaning would have been different after the class was
14883      entirely defined.  */
14884   else if (type_decl != error_mark_node
14885 	   && !parser->scope)
14886     maybe_note_name_used_in_class (identifier, type_decl);
14887 
14888   return type_decl;
14889 }
14890 
14891 /* Parse an elaborated-type-specifier.  Note that the grammar given
14892    here incorporates the resolution to DR68.
14893 
14894    elaborated-type-specifier:
14895      class-key :: [opt] nested-name-specifier [opt] identifier
14896      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14897      enum-key :: [opt] nested-name-specifier [opt] identifier
14898      typename :: [opt] nested-name-specifier identifier
14899      typename :: [opt] nested-name-specifier template [opt]
14900        template-id
14901 
14902    GNU extension:
14903 
14904    elaborated-type-specifier:
14905      class-key attributes :: [opt] nested-name-specifier [opt] identifier
14906      class-key attributes :: [opt] nested-name-specifier [opt]
14907 	       template [opt] template-id
14908      enum attributes :: [opt] nested-name-specifier [opt] identifier
14909 
14910    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14911    declared `friend'.  If IS_DECLARATION is TRUE, then this
14912    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14913    something is being declared.
14914 
14915    Returns the TYPE specified.  */
14916 
14917 static tree
cp_parser_elaborated_type_specifier(cp_parser * parser,bool is_friend,bool is_declaration)14918 cp_parser_elaborated_type_specifier (cp_parser* parser,
14919 				     bool is_friend,
14920 				     bool is_declaration)
14921 {
14922   enum tag_types tag_type;
14923   tree identifier;
14924   tree type = NULL_TREE;
14925   tree attributes = NULL_TREE;
14926   tree globalscope;
14927   cp_token *token = NULL;
14928 
14929   /* See if we're looking at the `enum' keyword.  */
14930   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14931     {
14932       /* Consume the `enum' token.  */
14933       cp_lexer_consume_token (parser->lexer);
14934       /* Remember that it's an enumeration type.  */
14935       tag_type = enum_type;
14936       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14937 	 enums) is used here.  */
14938       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14939 	  || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14940 	{
14941 	    pedwarn (input_location, 0, "elaborated-type-specifier "
14942 		      "for a scoped enum must not use the %<%D%> keyword",
14943 		      cp_lexer_peek_token (parser->lexer)->u.value);
14944 	  /* Consume the `struct' or `class' and parse it anyway.  */
14945 	  cp_lexer_consume_token (parser->lexer);
14946 	}
14947       /* Parse the attributes.  */
14948       attributes = cp_parser_attributes_opt (parser);
14949     }
14950   /* Or, it might be `typename'.  */
14951   else if (cp_lexer_next_token_is_keyword (parser->lexer,
14952 					   RID_TYPENAME))
14953     {
14954       /* Consume the `typename' token.  */
14955       cp_lexer_consume_token (parser->lexer);
14956       /* Remember that it's a `typename' type.  */
14957       tag_type = typename_type;
14958     }
14959   /* Otherwise it must be a class-key.  */
14960   else
14961     {
14962       tag_type = cp_parser_class_key (parser);
14963       if (tag_type == none_type)
14964 	return error_mark_node;
14965       /* Parse the attributes.  */
14966       attributes = cp_parser_attributes_opt (parser);
14967     }
14968 
14969   /* Look for the `::' operator.  */
14970   globalscope =  cp_parser_global_scope_opt (parser,
14971 					     /*current_scope_valid_p=*/false);
14972   /* Look for the nested-name-specifier.  */
14973   if (tag_type == typename_type && !globalscope)
14974     {
14975       if (!cp_parser_nested_name_specifier (parser,
14976 					   /*typename_keyword_p=*/true,
14977 					   /*check_dependency_p=*/true,
14978 					   /*type_p=*/true,
14979 					    is_declaration))
14980 	return error_mark_node;
14981     }
14982   else
14983     /* Even though `typename' is not present, the proposed resolution
14984        to Core Issue 180 says that in `class A<T>::B', `B' should be
14985        considered a type-name, even if `A<T>' is dependent.  */
14986     cp_parser_nested_name_specifier_opt (parser,
14987 					 /*typename_keyword_p=*/true,
14988 					 /*check_dependency_p=*/true,
14989 					 /*type_p=*/true,
14990 					 is_declaration);
14991  /* For everything but enumeration types, consider a template-id.
14992     For an enumeration type, consider only a plain identifier.  */
14993   if (tag_type != enum_type)
14994     {
14995       bool template_p = false;
14996       tree decl;
14997 
14998       /* Allow the `template' keyword.  */
14999       template_p = cp_parser_optional_template_keyword (parser);
15000       /* If we didn't see `template', we don't know if there's a
15001 	 template-id or not.  */
15002       if (!template_p)
15003 	cp_parser_parse_tentatively (parser);
15004       /* Parse the template-id.  */
15005       token = cp_lexer_peek_token (parser->lexer);
15006       decl = cp_parser_template_id (parser, template_p,
15007 				    /*check_dependency_p=*/true,
15008 				    tag_type,
15009 				    is_declaration);
15010       /* If we didn't find a template-id, look for an ordinary
15011 	 identifier.  */
15012       if (!template_p && !cp_parser_parse_definitely (parser))
15013 	;
15014       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15015 	 in effect, then we must assume that, upon instantiation, the
15016 	 template will correspond to a class.  */
15017       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15018 	       && tag_type == typename_type)
15019 	type = make_typename_type (parser->scope, decl,
15020 				   typename_type,
15021 				   /*complain=*/tf_error);
15022       /* If the `typename' keyword is in effect and DECL is not a type
15023 	 decl, then type is non existent.   */
15024       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15025         ;
15026       else if (TREE_CODE (decl) == TYPE_DECL)
15027         type = check_elaborated_type_specifier (tag_type, decl,
15028 						/*allow_template_p=*/true);
15029       else if (decl == error_mark_node)
15030 	type = error_mark_node;
15031     }
15032 
15033   if (!type)
15034     {
15035       token = cp_lexer_peek_token (parser->lexer);
15036       identifier = cp_parser_identifier (parser);
15037 
15038       if (identifier == error_mark_node)
15039 	{
15040 	  parser->scope = NULL_TREE;
15041 	  return error_mark_node;
15042 	}
15043 
15044       /* For a `typename', we needn't call xref_tag.  */
15045       if (tag_type == typename_type
15046 	  && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15047 	return cp_parser_make_typename_type (parser, parser->scope,
15048 					     identifier,
15049 					     token->location);
15050 
15051       /* Template parameter lists apply only if we are not within a
15052 	 function parameter list.  */
15053       bool template_parm_lists_apply
15054 	  = parser->num_template_parameter_lists;
15055       if (template_parm_lists_apply)
15056 	for (cp_binding_level *s = current_binding_level;
15057 	     s && s->kind != sk_template_parms;
15058 	     s = s->level_chain)
15059 	  if (s->kind == sk_function_parms)
15060 	    template_parm_lists_apply = false;
15061 
15062       /* Look up a qualified name in the usual way.  */
15063       if (parser->scope)
15064 	{
15065 	  tree decl;
15066 	  tree ambiguous_decls;
15067 
15068 	  decl = cp_parser_lookup_name (parser, identifier,
15069 					tag_type,
15070 					/*is_template=*/false,
15071 					/*is_namespace=*/false,
15072 					/*check_dependency=*/true,
15073 					&ambiguous_decls,
15074 					token->location);
15075 
15076 	  /* If the lookup was ambiguous, an error will already have been
15077 	     issued.  */
15078 	  if (ambiguous_decls)
15079 	    return error_mark_node;
15080 
15081 	  /* If we are parsing friend declaration, DECL may be a
15082 	     TEMPLATE_DECL tree node here.  However, we need to check
15083 	     whether this TEMPLATE_DECL results in valid code.  Consider
15084 	     the following example:
15085 
15086 	       namespace N {
15087 		 template <class T> class C {};
15088 	       }
15089 	       class X {
15090 		 template <class T> friend class N::C; // #1, valid code
15091 	       };
15092 	       template <class T> class Y {
15093 		 friend class N::C;		       // #2, invalid code
15094 	       };
15095 
15096 	     For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15097 	     name lookup of `N::C'.  We see that friend declaration must
15098 	     be template for the code to be valid.  Note that
15099 	     processing_template_decl does not work here since it is
15100 	     always 1 for the above two cases.  */
15101 
15102 	  decl = (cp_parser_maybe_treat_template_as_class
15103 		  (decl, /*tag_name_p=*/is_friend
15104 			 && template_parm_lists_apply));
15105 
15106 	  if (TREE_CODE (decl) != TYPE_DECL)
15107 	    {
15108 	      cp_parser_diagnose_invalid_type_name (parser,
15109 						    parser->scope,
15110 						    identifier,
15111 						    token->location);
15112 	      return error_mark_node;
15113 	    }
15114 
15115 	  if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15116             {
15117               bool allow_template = (template_parm_lists_apply
15118 		                     || DECL_SELF_REFERENCE_P (decl));
15119               type = check_elaborated_type_specifier (tag_type, decl,
15120                                                       allow_template);
15121 
15122               if (type == error_mark_node)
15123                 return error_mark_node;
15124             }
15125 
15126           /* Forward declarations of nested types, such as
15127 
15128                class C1::C2;
15129                class C1::C2::C3;
15130 
15131              are invalid unless all components preceding the final '::'
15132              are complete.  If all enclosing types are complete, these
15133              declarations become merely pointless.
15134 
15135              Invalid forward declarations of nested types are errors
15136              caught elsewhere in parsing.  Those that are pointless arrive
15137              here.  */
15138 
15139           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15140               && !is_friend && !processing_explicit_instantiation)
15141             warning (0, "declaration %qD does not declare anything", decl);
15142 
15143 	  type = TREE_TYPE (decl);
15144 	}
15145       else
15146 	{
15147 	  /* An elaborated-type-specifier sometimes introduces a new type and
15148 	     sometimes names an existing type.  Normally, the rule is that it
15149 	     introduces a new type only if there is not an existing type of
15150 	     the same name already in scope.  For example, given:
15151 
15152 	       struct S {};
15153 	       void f() { struct S s; }
15154 
15155 	     the `struct S' in the body of `f' is the same `struct S' as in
15156 	     the global scope; the existing definition is used.  However, if
15157 	     there were no global declaration, this would introduce a new
15158 	     local class named `S'.
15159 
15160 	     An exception to this rule applies to the following code:
15161 
15162 	       namespace N { struct S; }
15163 
15164 	     Here, the elaborated-type-specifier names a new type
15165 	     unconditionally; even if there is already an `S' in the
15166 	     containing scope this declaration names a new type.
15167 	     This exception only applies if the elaborated-type-specifier
15168 	     forms the complete declaration:
15169 
15170 	       [class.name]
15171 
15172 	       A declaration consisting solely of `class-key identifier ;' is
15173 	       either a redeclaration of the name in the current scope or a
15174 	       forward declaration of the identifier as a class name.  It
15175 	       introduces the name into the current scope.
15176 
15177 	     We are in this situation precisely when the next token is a `;'.
15178 
15179 	     An exception to the exception is that a `friend' declaration does
15180 	     *not* name a new type; i.e., given:
15181 
15182 	       struct S { friend struct T; };
15183 
15184 	     `T' is not a new type in the scope of `S'.
15185 
15186 	     Also, `new struct S' or `sizeof (struct S)' never results in the
15187 	     definition of a new type; a new type can only be declared in a
15188 	     declaration context.  */
15189 
15190 	  tag_scope ts;
15191 	  bool template_p;
15192 
15193 	  if (is_friend)
15194 	    /* Friends have special name lookup rules.  */
15195 	    ts = ts_within_enclosing_non_class;
15196 	  else if (is_declaration
15197 		   && cp_lexer_next_token_is (parser->lexer,
15198 					      CPP_SEMICOLON))
15199 	    /* This is a `class-key identifier ;' */
15200 	    ts = ts_current;
15201 	  else
15202 	    ts = ts_global;
15203 
15204 	  template_p =
15205 	    (template_parm_lists_apply
15206 	     && (cp_parser_next_token_starts_class_definition_p (parser)
15207 		 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15208 	  /* An unqualified name was used to reference this type, so
15209 	     there were no qualifying templates.  */
15210 	  if (template_parm_lists_apply
15211 	      && !cp_parser_check_template_parameters (parser,
15212 						       /*num_templates=*/0,
15213 						       token->location,
15214 						       /*declarator=*/NULL))
15215 	    return error_mark_node;
15216 	  type = xref_tag (tag_type, identifier, ts, template_p);
15217 	}
15218     }
15219 
15220   if (type == error_mark_node)
15221     return error_mark_node;
15222 
15223   /* Allow attributes on forward declarations of classes.  */
15224   if (attributes)
15225     {
15226       if (TREE_CODE (type) == TYPENAME_TYPE)
15227 	warning (OPT_Wattributes,
15228 		 "attributes ignored on uninstantiated type");
15229       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15230 	       && ! processing_explicit_instantiation)
15231 	warning (OPT_Wattributes,
15232 		 "attributes ignored on template instantiation");
15233       else if (is_declaration && cp_parser_declares_only_class_p (parser))
15234 	cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15235       else
15236 	warning (OPT_Wattributes,
15237 		 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15238     }
15239 
15240   if (tag_type != enum_type)
15241     {
15242       /* Indicate whether this class was declared as a `class' or as a
15243 	 `struct'.  */
15244       if (TREE_CODE (type) == RECORD_TYPE)
15245 	CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15246       cp_parser_check_class_key (tag_type, type);
15247     }
15248 
15249   /* A "<" cannot follow an elaborated type specifier.  If that
15250      happens, the user was probably trying to form a template-id.  */
15251   cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15252 					   token->location);
15253 
15254   return type;
15255 }
15256 
15257 /* Parse an enum-specifier.
15258 
15259    enum-specifier:
15260      enum-head { enumerator-list [opt] }
15261      enum-head { enumerator-list , } [C++0x]
15262 
15263    enum-head:
15264      enum-key identifier [opt] enum-base [opt]
15265      enum-key nested-name-specifier identifier enum-base [opt]
15266 
15267    enum-key:
15268      enum
15269      enum class   [C++0x]
15270      enum struct  [C++0x]
15271 
15272    enum-base:   [C++0x]
15273      : type-specifier-seq
15274 
15275    opaque-enum-specifier:
15276      enum-key identifier enum-base [opt] ;
15277 
15278    GNU Extensions:
15279      enum-key attributes[opt] identifier [opt] enum-base [opt]
15280        { enumerator-list [opt] }attributes[opt]
15281      enum-key attributes[opt] identifier [opt] enum-base [opt]
15282        { enumerator-list, }attributes[opt] [C++0x]
15283 
15284    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15285    if the token stream isn't an enum-specifier after all.  */
15286 
15287 static tree
cp_parser_enum_specifier(cp_parser * parser)15288 cp_parser_enum_specifier (cp_parser* parser)
15289 {
15290   tree identifier;
15291   tree type = NULL_TREE;
15292   tree prev_scope;
15293   tree nested_name_specifier = NULL_TREE;
15294   tree attributes;
15295   bool scoped_enum_p = false;
15296   bool has_underlying_type = false;
15297   bool nested_being_defined = false;
15298   bool new_value_list = false;
15299   bool is_new_type = false;
15300   bool is_anonymous = false;
15301   tree underlying_type = NULL_TREE;
15302   cp_token *type_start_token = NULL;
15303   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15304 
15305   parser->colon_corrects_to_scope_p = false;
15306 
15307   /* Parse tentatively so that we can back up if we don't find a
15308      enum-specifier.  */
15309   cp_parser_parse_tentatively (parser);
15310 
15311   /* Caller guarantees that the current token is 'enum', an identifier
15312      possibly follows, and the token after that is an opening brace.
15313      If we don't have an identifier, fabricate an anonymous name for
15314      the enumeration being defined.  */
15315   cp_lexer_consume_token (parser->lexer);
15316 
15317   /* Parse the "class" or "struct", which indicates a scoped
15318      enumeration type in C++0x.  */
15319   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15320       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15321     {
15322       if (cxx_dialect < cxx11)
15323         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15324 
15325       /* Consume the `struct' or `class' token.  */
15326       cp_lexer_consume_token (parser->lexer);
15327 
15328       scoped_enum_p = true;
15329     }
15330 
15331   attributes = cp_parser_attributes_opt (parser);
15332 
15333   /* Clear the qualification.  */
15334   parser->scope = NULL_TREE;
15335   parser->qualifying_scope = NULL_TREE;
15336   parser->object_scope = NULL_TREE;
15337 
15338   /* Figure out in what scope the declaration is being placed.  */
15339   prev_scope = current_scope ();
15340 
15341   type_start_token = cp_lexer_peek_token (parser->lexer);
15342 
15343   push_deferring_access_checks (dk_no_check);
15344   nested_name_specifier
15345       = cp_parser_nested_name_specifier_opt (parser,
15346 					     /*typename_keyword_p=*/true,
15347 					     /*check_dependency_p=*/false,
15348 					     /*type_p=*/false,
15349 					     /*is_declaration=*/false);
15350 
15351   if (nested_name_specifier)
15352     {
15353       tree name;
15354 
15355       identifier = cp_parser_identifier (parser);
15356       name =  cp_parser_lookup_name (parser, identifier,
15357 				     enum_type,
15358 				     /*is_template=*/false,
15359 				     /*is_namespace=*/false,
15360 				     /*check_dependency=*/true,
15361 				     /*ambiguous_decls=*/NULL,
15362 				     input_location);
15363       if (name && name != error_mark_node)
15364 	{
15365 	  type = TREE_TYPE (name);
15366 	  if (TREE_CODE (type) == TYPENAME_TYPE)
15367 	    {
15368 	      /* Are template enums allowed in ISO? */
15369 	      if (template_parm_scope_p ())
15370 		pedwarn (type_start_token->location, OPT_Wpedantic,
15371 			 "%qD is an enumeration template", name);
15372 	      /* ignore a typename reference, for it will be solved by name
15373 	         in start_enum.  */
15374 	      type = NULL_TREE;
15375 	    }
15376 	}
15377       else if (nested_name_specifier == error_mark_node)
15378 	/* We already issued an error.  */;
15379       else
15380 	error_at (type_start_token->location,
15381 		  "%qD is not an enumerator-name", identifier);
15382     }
15383   else
15384     {
15385       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15386 	identifier = cp_parser_identifier (parser);
15387       else
15388 	{
15389 	  identifier = make_anon_name ();
15390 	  is_anonymous = true;
15391 	  if (scoped_enum_p)
15392 	    error_at (type_start_token->location,
15393 		      "anonymous scoped enum is not allowed");
15394 	}
15395     }
15396   pop_deferring_access_checks ();
15397 
15398   /* Check for the `:' that denotes a specified underlying type in C++0x.
15399      Note that a ':' could also indicate a bitfield width, however.  */
15400   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15401     {
15402       cp_decl_specifier_seq type_specifiers;
15403 
15404       /* Consume the `:'.  */
15405       cp_lexer_consume_token (parser->lexer);
15406 
15407       /* Parse the type-specifier-seq.  */
15408       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15409 				    /*is_trailing_return=*/false,
15410                                     &type_specifiers);
15411 
15412       /* At this point this is surely not elaborated type specifier.  */
15413       if (!cp_parser_parse_definitely (parser))
15414 	return NULL_TREE;
15415 
15416       if (cxx_dialect < cxx11)
15417         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15418 
15419       has_underlying_type = true;
15420 
15421       /* If that didn't work, stop.  */
15422       if (type_specifiers.type != error_mark_node)
15423         {
15424           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15425                                             /*initialized=*/0, NULL);
15426           if (underlying_type == error_mark_node
15427 	      || check_for_bare_parameter_packs (underlying_type))
15428             underlying_type = NULL_TREE;
15429         }
15430     }
15431 
15432   /* Look for the `{' but don't consume it yet.  */
15433   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15434     {
15435       if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15436 	{
15437 	  cp_parser_error (parser, "expected %<{%>");
15438 	  if (has_underlying_type)
15439 	    {
15440 	      type = NULL_TREE;
15441 	      goto out;
15442 	    }
15443 	}
15444       /* An opaque-enum-specifier must have a ';' here.  */
15445       if ((scoped_enum_p || underlying_type)
15446 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15447 	{
15448 	  cp_parser_error (parser, "expected %<;%> or %<{%>");
15449 	  if (has_underlying_type)
15450 	    {
15451 	      type = NULL_TREE;
15452 	      goto out;
15453 	    }
15454 	}
15455     }
15456 
15457   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15458     return NULL_TREE;
15459 
15460   if (nested_name_specifier)
15461     {
15462       if (CLASS_TYPE_P (nested_name_specifier))
15463 	{
15464 	  nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15465 	  TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15466 	  push_scope (nested_name_specifier);
15467 	}
15468       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15469 	{
15470 	  push_nested_namespace (nested_name_specifier);
15471 	}
15472     }
15473 
15474   /* Issue an error message if type-definitions are forbidden here.  */
15475   if (!cp_parser_check_type_definition (parser))
15476     type = error_mark_node;
15477   else
15478     /* Create the new type.  We do this before consuming the opening
15479        brace so the enum will be recorded as being on the line of its
15480        tag (or the 'enum' keyword, if there is no tag).  */
15481     type = start_enum (identifier, type, underlying_type,
15482 		       scoped_enum_p, &is_new_type);
15483 
15484   /* If the next token is not '{' it is an opaque-enum-specifier or an
15485      elaborated-type-specifier.  */
15486   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15487     {
15488       timevar_push (TV_PARSE_ENUM);
15489       if (nested_name_specifier
15490 	  && nested_name_specifier != error_mark_node)
15491 	{
15492 	  /* The following catches invalid code such as:
15493 	     enum class S<int>::E { A, B, C }; */
15494 	  if (!processing_specialization
15495 	      && CLASS_TYPE_P (nested_name_specifier)
15496 	      && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15497 	    error_at (type_start_token->location, "cannot add an enumerator "
15498 		      "list to a template instantiation");
15499 
15500 	  if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15501 	    {
15502 	      error_at (type_start_token->location,
15503 			"%<%T::%E%> has not been declared",
15504 			TYPE_CONTEXT (nested_name_specifier),
15505 			nested_name_specifier);
15506 	      type = error_mark_node;
15507 	    }
15508 	  /* If that scope does not contain the scope in which the
15509 	     class was originally declared, the program is invalid.  */
15510 	  else if (prev_scope && !is_ancestor (prev_scope,
15511 					       nested_name_specifier))
15512 	    {
15513 	      if (at_namespace_scope_p ())
15514 		error_at (type_start_token->location,
15515 			  "declaration of %qD in namespace %qD which does not "
15516 			  "enclose %qD",
15517 			  type, prev_scope, nested_name_specifier);
15518 	      else
15519 		error_at (type_start_token->location,
15520 			  "declaration of %qD in %qD which does not "
15521 			  "enclose %qD",
15522 			  type, prev_scope, nested_name_specifier);
15523 	      type = error_mark_node;
15524 	    }
15525 	}
15526 
15527       if (scoped_enum_p)
15528 	begin_scope (sk_scoped_enum, type);
15529 
15530       /* Consume the opening brace.  */
15531       cp_lexer_consume_token (parser->lexer);
15532 
15533       if (type == error_mark_node)
15534 	; /* Nothing to add */
15535       else if (OPAQUE_ENUM_P (type)
15536 	       || (cxx_dialect > cxx98 && processing_specialization))
15537 	{
15538 	  new_value_list = true;
15539 	  SET_OPAQUE_ENUM_P (type, false);
15540 	  DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15541 	}
15542       else
15543 	{
15544 	  error_at (type_start_token->location, "multiple definition of %q#T", type);
15545 	  error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15546 		    "previous definition here");
15547 	  type = error_mark_node;
15548 	}
15549 
15550       if (type == error_mark_node)
15551 	cp_parser_skip_to_end_of_block_or_statement (parser);
15552       /* If the next token is not '}', then there are some enumerators.  */
15553       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15554 	{
15555 	  if (is_anonymous && !scoped_enum_p)
15556 	    pedwarn (type_start_token->location, OPT_Wpedantic,
15557 		     "ISO C++ forbids empty anonymous enum");
15558 	}
15559       else
15560 	cp_parser_enumerator_list (parser, type);
15561 
15562       /* Consume the final '}'.  */
15563       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15564 
15565       if (scoped_enum_p)
15566 	finish_scope ();
15567       timevar_pop (TV_PARSE_ENUM);
15568     }
15569   else
15570     {
15571       /* If a ';' follows, then it is an opaque-enum-specifier
15572 	and additional restrictions apply.  */
15573       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15574 	{
15575 	  if (is_anonymous)
15576 	    error_at (type_start_token->location,
15577 		      "opaque-enum-specifier without name");
15578 	  else if (nested_name_specifier)
15579 	    error_at (type_start_token->location,
15580 		      "opaque-enum-specifier must use a simple identifier");
15581 	}
15582     }
15583 
15584   /* Look for trailing attributes to apply to this enumeration, and
15585      apply them if appropriate.  */
15586   if (cp_parser_allow_gnu_extensions_p (parser))
15587     {
15588       tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15589       trailing_attr = chainon (trailing_attr, attributes);
15590       cplus_decl_attributes (&type,
15591 			     trailing_attr,
15592 			     (int) ATTR_FLAG_TYPE_IN_PLACE);
15593     }
15594 
15595   /* Finish up the enumeration.  */
15596   if (type != error_mark_node)
15597     {
15598       if (new_value_list)
15599 	finish_enum_value_list (type);
15600       if (is_new_type)
15601 	finish_enum (type);
15602     }
15603 
15604   if (nested_name_specifier)
15605     {
15606       if (CLASS_TYPE_P (nested_name_specifier))
15607 	{
15608 	  TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15609 	  pop_scope (nested_name_specifier);
15610 	}
15611       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15612 	{
15613 	  pop_nested_namespace (nested_name_specifier);
15614 	}
15615     }
15616  out:
15617   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15618   return type;
15619 }
15620 
15621 /* Parse an enumerator-list.  The enumerators all have the indicated
15622    TYPE.
15623 
15624    enumerator-list:
15625      enumerator-definition
15626      enumerator-list , enumerator-definition  */
15627 
15628 static void
cp_parser_enumerator_list(cp_parser * parser,tree type)15629 cp_parser_enumerator_list (cp_parser* parser, tree type)
15630 {
15631   while (true)
15632     {
15633       /* Parse an enumerator-definition.  */
15634       cp_parser_enumerator_definition (parser, type);
15635 
15636       /* If the next token is not a ',', we've reached the end of
15637 	 the list.  */
15638       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15639 	break;
15640       /* Otherwise, consume the `,' and keep going.  */
15641       cp_lexer_consume_token (parser->lexer);
15642       /* If the next token is a `}', there is a trailing comma.  */
15643       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15644 	{
15645 	  if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
15646 	    pedwarn (input_location, OPT_Wpedantic,
15647                      "comma at end of enumerator list");
15648 	  break;
15649 	}
15650     }
15651 }
15652 
15653 /* Parse an enumerator-definition.  The enumerator has the indicated
15654    TYPE.
15655 
15656    enumerator-definition:
15657      enumerator
15658      enumerator = constant-expression
15659 
15660    enumerator:
15661      identifier  */
15662 
15663 static void
cp_parser_enumerator_definition(cp_parser * parser,tree type)15664 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15665 {
15666   tree identifier;
15667   tree value;
15668   location_t loc;
15669 
15670   /* Save the input location because we are interested in the location
15671      of the identifier and not the location of the explicit value.  */
15672   loc = cp_lexer_peek_token (parser->lexer)->location;
15673 
15674   /* Look for the identifier.  */
15675   identifier = cp_parser_identifier (parser);
15676   if (identifier == error_mark_node)
15677     return;
15678 
15679   /* If the next token is an '=', then there is an explicit value.  */
15680   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15681     {
15682       /* Consume the `=' token.  */
15683       cp_lexer_consume_token (parser->lexer);
15684       /* Parse the value.  */
15685       value = cp_parser_constant_expression (parser,
15686 					     /*allow_non_constant_p=*/false,
15687 					     NULL);
15688     }
15689   else
15690     value = NULL_TREE;
15691 
15692   /* If we are processing a template, make sure the initializer of the
15693      enumerator doesn't contain any bare template parameter pack.  */
15694   if (check_for_bare_parameter_packs (value))
15695     value = error_mark_node;
15696 
15697   /* integral_constant_value will pull out this expression, so make sure
15698      it's folded as appropriate.  */
15699   value = fold_non_dependent_expr (value);
15700 
15701   /* Create the enumerator.  */
15702   build_enumerator (identifier, value, type, loc);
15703 }
15704 
15705 /* Parse a namespace-name.
15706 
15707    namespace-name:
15708      original-namespace-name
15709      namespace-alias
15710 
15711    Returns the NAMESPACE_DECL for the namespace.  */
15712 
15713 static tree
cp_parser_namespace_name(cp_parser * parser)15714 cp_parser_namespace_name (cp_parser* parser)
15715 {
15716   tree identifier;
15717   tree namespace_decl;
15718 
15719   cp_token *token = cp_lexer_peek_token (parser->lexer);
15720 
15721   /* Get the name of the namespace.  */
15722   identifier = cp_parser_identifier (parser);
15723   if (identifier == error_mark_node)
15724     return error_mark_node;
15725 
15726   /* Look up the identifier in the currently active scope.  Look only
15727      for namespaces, due to:
15728 
15729        [basic.lookup.udir]
15730 
15731        When looking up a namespace-name in a using-directive or alias
15732        definition, only namespace names are considered.
15733 
15734      And:
15735 
15736        [basic.lookup.qual]
15737 
15738        During the lookup of a name preceding the :: scope resolution
15739        operator, object, function, and enumerator names are ignored.
15740 
15741      (Note that cp_parser_qualifying_entity only calls this
15742      function if the token after the name is the scope resolution
15743      operator.)  */
15744   namespace_decl = cp_parser_lookup_name (parser, identifier,
15745 					  none_type,
15746 					  /*is_template=*/false,
15747 					  /*is_namespace=*/true,
15748 					  /*check_dependency=*/true,
15749 					  /*ambiguous_decls=*/NULL,
15750 					  token->location);
15751   /* If it's not a namespace, issue an error.  */
15752   if (namespace_decl == error_mark_node
15753       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15754     {
15755       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15756 	error_at (token->location, "%qD is not a namespace-name", identifier);
15757       cp_parser_error (parser, "expected namespace-name");
15758       namespace_decl = error_mark_node;
15759     }
15760 
15761   return namespace_decl;
15762 }
15763 
15764 /* Parse a namespace-definition.
15765 
15766    namespace-definition:
15767      named-namespace-definition
15768      unnamed-namespace-definition
15769 
15770    named-namespace-definition:
15771      original-namespace-definition
15772      extension-namespace-definition
15773 
15774    original-namespace-definition:
15775      namespace identifier { namespace-body }
15776 
15777    extension-namespace-definition:
15778      namespace original-namespace-name { namespace-body }
15779 
15780    unnamed-namespace-definition:
15781      namespace { namespace-body } */
15782 
15783 static void
cp_parser_namespace_definition(cp_parser * parser)15784 cp_parser_namespace_definition (cp_parser* parser)
15785 {
15786   tree identifier, attribs;
15787   bool has_visibility;
15788   bool is_inline;
15789 
15790   cp_ensure_no_omp_declare_simd (parser);
15791   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15792     {
15793       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15794       is_inline = true;
15795       cp_lexer_consume_token (parser->lexer);
15796     }
15797   else
15798     is_inline = false;
15799 
15800   /* Look for the `namespace' keyword.  */
15801   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15802 
15803   /* Get the name of the namespace.  We do not attempt to distinguish
15804      between an original-namespace-definition and an
15805      extension-namespace-definition at this point.  The semantic
15806      analysis routines are responsible for that.  */
15807   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15808     identifier = cp_parser_identifier (parser);
15809   else
15810     identifier = NULL_TREE;
15811 
15812   /* Parse any specified attributes.  */
15813   attribs = cp_parser_attributes_opt (parser);
15814 
15815   /* Look for the `{' to start the namespace.  */
15816   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15817   /* Start the namespace.  */
15818   push_namespace (identifier);
15819 
15820   /* "inline namespace" is equivalent to a stub namespace definition
15821      followed by a strong using directive.  */
15822   if (is_inline)
15823     {
15824       tree name_space = current_namespace;
15825       /* Set up namespace association.  */
15826       DECL_NAMESPACE_ASSOCIATIONS (name_space)
15827 	= tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15828 		     DECL_NAMESPACE_ASSOCIATIONS (name_space));
15829       /* Import the contents of the inline namespace.  */
15830       pop_namespace ();
15831       do_using_directive (name_space);
15832       push_namespace (identifier);
15833     }
15834 
15835   has_visibility = handle_namespace_attrs (current_namespace, attribs);
15836 
15837   /* Parse the body of the namespace.  */
15838   cp_parser_namespace_body (parser);
15839 
15840   if (has_visibility)
15841     pop_visibility (1);
15842 
15843   /* Finish the namespace.  */
15844   pop_namespace ();
15845   /* Look for the final `}'.  */
15846   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15847 }
15848 
15849 /* Parse a namespace-body.
15850 
15851    namespace-body:
15852      declaration-seq [opt]  */
15853 
15854 static void
cp_parser_namespace_body(cp_parser * parser)15855 cp_parser_namespace_body (cp_parser* parser)
15856 {
15857   cp_parser_declaration_seq_opt (parser);
15858 }
15859 
15860 /* Parse a namespace-alias-definition.
15861 
15862    namespace-alias-definition:
15863      namespace identifier = qualified-namespace-specifier ;  */
15864 
15865 static void
cp_parser_namespace_alias_definition(cp_parser * parser)15866 cp_parser_namespace_alias_definition (cp_parser* parser)
15867 {
15868   tree identifier;
15869   tree namespace_specifier;
15870 
15871   cp_token *token = cp_lexer_peek_token (parser->lexer);
15872 
15873   /* Look for the `namespace' keyword.  */
15874   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15875   /* Look for the identifier.  */
15876   identifier = cp_parser_identifier (parser);
15877   if (identifier == error_mark_node)
15878     return;
15879   /* Look for the `=' token.  */
15880   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15881       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15882     {
15883       error_at (token->location, "%<namespace%> definition is not allowed here");
15884       /* Skip the definition.  */
15885       cp_lexer_consume_token (parser->lexer);
15886       if (cp_parser_skip_to_closing_brace (parser))
15887 	cp_lexer_consume_token (parser->lexer);
15888       return;
15889     }
15890   cp_parser_require (parser, CPP_EQ, RT_EQ);
15891   /* Look for the qualified-namespace-specifier.  */
15892   namespace_specifier
15893     = cp_parser_qualified_namespace_specifier (parser);
15894   /* Look for the `;' token.  */
15895   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15896 
15897   /* Register the alias in the symbol table.  */
15898   do_namespace_alias (identifier, namespace_specifier);
15899 }
15900 
15901 /* Parse a qualified-namespace-specifier.
15902 
15903    qualified-namespace-specifier:
15904      :: [opt] nested-name-specifier [opt] namespace-name
15905 
15906    Returns a NAMESPACE_DECL corresponding to the specified
15907    namespace.  */
15908 
15909 static tree
cp_parser_qualified_namespace_specifier(cp_parser * parser)15910 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15911 {
15912   /* Look for the optional `::'.  */
15913   cp_parser_global_scope_opt (parser,
15914 			      /*current_scope_valid_p=*/false);
15915 
15916   /* Look for the optional nested-name-specifier.  */
15917   cp_parser_nested_name_specifier_opt (parser,
15918 				       /*typename_keyword_p=*/false,
15919 				       /*check_dependency_p=*/true,
15920 				       /*type_p=*/false,
15921 				       /*is_declaration=*/true);
15922 
15923   return cp_parser_namespace_name (parser);
15924 }
15925 
15926 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15927    access declaration.
15928 
15929    using-declaration:
15930      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15931      using :: unqualified-id ;
15932 
15933    access-declaration:
15934      qualified-id ;
15935 
15936    */
15937 
15938 static bool
cp_parser_using_declaration(cp_parser * parser,bool access_declaration_p)15939 cp_parser_using_declaration (cp_parser* parser,
15940 			     bool access_declaration_p)
15941 {
15942   cp_token *token;
15943   bool typename_p = false;
15944   bool global_scope_p;
15945   tree decl;
15946   tree identifier;
15947   tree qscope;
15948   int oldcount = errorcount;
15949   cp_token *diag_token = NULL;
15950 
15951   if (access_declaration_p)
15952     {
15953       diag_token = cp_lexer_peek_token (parser->lexer);
15954       cp_parser_parse_tentatively (parser);
15955     }
15956   else
15957     {
15958       /* Look for the `using' keyword.  */
15959       cp_parser_require_keyword (parser, RID_USING, RT_USING);
15960 
15961       /* Peek at the next token.  */
15962       token = cp_lexer_peek_token (parser->lexer);
15963       /* See if it's `typename'.  */
15964       if (token->keyword == RID_TYPENAME)
15965 	{
15966 	  /* Remember that we've seen it.  */
15967 	  typename_p = true;
15968 	  /* Consume the `typename' token.  */
15969 	  cp_lexer_consume_token (parser->lexer);
15970 	}
15971     }
15972 
15973   /* Look for the optional global scope qualification.  */
15974   global_scope_p
15975     = (cp_parser_global_scope_opt (parser,
15976 				   /*current_scope_valid_p=*/false)
15977        != NULL_TREE);
15978 
15979   /* If we saw `typename', or didn't see `::', then there must be a
15980      nested-name-specifier present.  */
15981   if (typename_p || !global_scope_p)
15982     {
15983       qscope = cp_parser_nested_name_specifier (parser, typename_p,
15984 						/*check_dependency_p=*/true,
15985 						/*type_p=*/false,
15986 						/*is_declaration=*/true);
15987       if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
15988 	{
15989 	  cp_parser_skip_to_end_of_block_or_statement (parser);
15990 	  return false;
15991 	}
15992     }
15993   /* Otherwise, we could be in either of the two productions.  In that
15994      case, treat the nested-name-specifier as optional.  */
15995   else
15996     qscope = cp_parser_nested_name_specifier_opt (parser,
15997 						  /*typename_keyword_p=*/false,
15998 						  /*check_dependency_p=*/true,
15999 						  /*type_p=*/false,
16000 						  /*is_declaration=*/true);
16001   if (!qscope)
16002     qscope = global_namespace;
16003 
16004   if (access_declaration_p && cp_parser_error_occurred (parser))
16005     /* Something has already gone wrong; there's no need to parse
16006        further.  Since an error has occurred, the return value of
16007        cp_parser_parse_definitely will be false, as required.  */
16008     return cp_parser_parse_definitely (parser);
16009 
16010   token = cp_lexer_peek_token (parser->lexer);
16011   /* Parse the unqualified-id.  */
16012   identifier = cp_parser_unqualified_id (parser,
16013 					 /*template_keyword_p=*/false,
16014 					 /*check_dependency_p=*/true,
16015 					 /*declarator_p=*/true,
16016 					 /*optional_p=*/false);
16017 
16018   if (access_declaration_p)
16019     {
16020       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16021 	cp_parser_simulate_error (parser);
16022       if (!cp_parser_parse_definitely (parser))
16023 	return false;
16024     }
16025 
16026   /* The function we call to handle a using-declaration is different
16027      depending on what scope we are in.  */
16028   if (qscope == error_mark_node || identifier == error_mark_node)
16029     ;
16030   else if (!identifier_p (identifier)
16031 	   && TREE_CODE (identifier) != BIT_NOT_EXPR)
16032     /* [namespace.udecl]
16033 
16034        A using declaration shall not name a template-id.  */
16035     error_at (token->location,
16036 	      "a template-id may not appear in a using-declaration");
16037   else
16038     {
16039       if (at_class_scope_p ())
16040 	{
16041 	  /* Create the USING_DECL.  */
16042 	  decl = do_class_using_decl (parser->scope, identifier);
16043 
16044 	  if (decl && typename_p)
16045 	    USING_DECL_TYPENAME_P (decl) = 1;
16046 
16047 	  if (check_for_bare_parameter_packs (decl))
16048 	    {
16049 	      cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16050 	      return false;
16051 	    }
16052 	  else
16053 	    /* Add it to the list of members in this class.  */
16054 	    finish_member_declaration (decl);
16055 	}
16056       else
16057 	{
16058 	  decl = cp_parser_lookup_name_simple (parser,
16059 					       identifier,
16060 					       token->location);
16061 	  if (decl == error_mark_node)
16062 	    cp_parser_name_lookup_error (parser, identifier,
16063 					 decl, NLE_NULL,
16064 					 token->location);
16065 	  else if (check_for_bare_parameter_packs (decl))
16066 	    {
16067 	      cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16068 	      return false;
16069 	    }
16070 	  else if (!at_namespace_scope_p ())
16071 	    do_local_using_decl (decl, qscope, identifier);
16072 	  else
16073 	    do_toplevel_using_decl (decl, qscope, identifier);
16074 	}
16075     }
16076 
16077   /* Look for the final `;'.  */
16078   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16079 
16080   if (access_declaration_p && errorcount == oldcount)
16081     warning_at (diag_token->location, OPT_Wdeprecated,
16082 		"access declarations are deprecated "
16083 		"in favour of using-declarations; "
16084 		"suggestion: add the %<using%> keyword");
16085 
16086   return true;
16087 }
16088 
16089 /* Parse an alias-declaration.
16090 
16091    alias-declaration:
16092      using identifier attribute-specifier-seq [opt] = type-id  */
16093 
16094 static tree
cp_parser_alias_declaration(cp_parser * parser)16095 cp_parser_alias_declaration (cp_parser* parser)
16096 {
16097   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16098   location_t id_location;
16099   cp_declarator *declarator;
16100   cp_decl_specifier_seq decl_specs;
16101   bool member_p;
16102   const char *saved_message = NULL;
16103 
16104   /* Look for the `using' keyword.  */
16105   cp_token *using_token
16106     = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16107   if (using_token == NULL)
16108     return error_mark_node;
16109 
16110   id_location = cp_lexer_peek_token (parser->lexer)->location;
16111   id = cp_parser_identifier (parser);
16112   if (id == error_mark_node)
16113     return error_mark_node;
16114 
16115   cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16116   attributes = cp_parser_attributes_opt (parser);
16117   if (attributes == error_mark_node)
16118     return error_mark_node;
16119 
16120   cp_parser_require (parser, CPP_EQ, RT_EQ);
16121 
16122   if (cp_parser_error_occurred (parser))
16123     return error_mark_node;
16124 
16125   cp_parser_commit_to_tentative_parse (parser);
16126 
16127   /* Now we are going to parse the type-id of the declaration.  */
16128 
16129   /*
16130     [dcl.type]/3 says:
16131 
16132 	"A type-specifier-seq shall not define a class or enumeration
16133 	 unless it appears in the type-id of an alias-declaration (7.1.3) that
16134 	 is not the declaration of a template-declaration."
16135 
16136     In other words, if we currently are in an alias template, the
16137     type-id should not define a type.
16138 
16139     So let's set parser->type_definition_forbidden_message in that
16140     case; cp_parser_check_type_definition (called by
16141     cp_parser_class_specifier) will then emit an error if a type is
16142     defined in the type-id.  */
16143   if (parser->num_template_parameter_lists)
16144     {
16145       saved_message = parser->type_definition_forbidden_message;
16146       parser->type_definition_forbidden_message =
16147 	G_("types may not be defined in alias template declarations");
16148     }
16149 
16150   type = cp_parser_type_id (parser);
16151 
16152   /* Restore the error message if need be.  */
16153   if (parser->num_template_parameter_lists)
16154     parser->type_definition_forbidden_message = saved_message;
16155 
16156   if (type == error_mark_node)
16157     {
16158       cp_parser_skip_to_end_of_block_or_statement (parser);
16159       return error_mark_node;
16160     }
16161 
16162   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16163 
16164   if (cp_parser_error_occurred (parser))
16165     {
16166       cp_parser_skip_to_end_of_block_or_statement (parser);
16167       return error_mark_node;
16168     }
16169 
16170   /* A typedef-name can also be introduced by an alias-declaration. The
16171      identifier following the using keyword becomes a typedef-name. It has
16172      the same semantics as if it were introduced by the typedef
16173      specifier. In particular, it does not define a new type and it shall
16174      not appear in the type-id.  */
16175 
16176   clear_decl_specs (&decl_specs);
16177   decl_specs.type = type;
16178   if (attributes != NULL_TREE)
16179     {
16180       decl_specs.attributes = attributes;
16181       set_and_check_decl_spec_loc (&decl_specs,
16182 				   ds_attribute,
16183 				   attrs_token);
16184     }
16185   set_and_check_decl_spec_loc (&decl_specs,
16186 			       ds_typedef,
16187 			       using_token);
16188   set_and_check_decl_spec_loc (&decl_specs,
16189 			       ds_alias,
16190 			       using_token);
16191 
16192   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16193   declarator->id_loc = id_location;
16194 
16195   member_p = at_class_scope_p ();
16196   if (member_p)
16197     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16198 		      NULL_TREE, attributes);
16199   else
16200     decl = start_decl (declarator, &decl_specs, 0,
16201 		       attributes, NULL_TREE, &pushed_scope);
16202   if (decl == error_mark_node)
16203     return decl;
16204 
16205   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16206 
16207   if (pushed_scope)
16208     pop_scope (pushed_scope);
16209 
16210   /* If decl is a template, return its TEMPLATE_DECL so that it gets
16211      added into the symbol table; otherwise, return the TYPE_DECL.  */
16212   if (DECL_LANG_SPECIFIC (decl)
16213       && DECL_TEMPLATE_INFO (decl)
16214       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16215     {
16216       decl = DECL_TI_TEMPLATE (decl);
16217       if (member_p)
16218 	check_member_template (decl);
16219     }
16220 
16221   return decl;
16222 }
16223 
16224 /* Parse a using-directive.
16225 
16226    using-directive:
16227      using namespace :: [opt] nested-name-specifier [opt]
16228        namespace-name ;  */
16229 
16230 static void
cp_parser_using_directive(cp_parser * parser)16231 cp_parser_using_directive (cp_parser* parser)
16232 {
16233   tree namespace_decl;
16234   tree attribs;
16235 
16236   /* Look for the `using' keyword.  */
16237   cp_parser_require_keyword (parser, RID_USING, RT_USING);
16238   /* And the `namespace' keyword.  */
16239   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16240   /* Look for the optional `::' operator.  */
16241   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16242   /* And the optional nested-name-specifier.  */
16243   cp_parser_nested_name_specifier_opt (parser,
16244 				       /*typename_keyword_p=*/false,
16245 				       /*check_dependency_p=*/true,
16246 				       /*type_p=*/false,
16247 				       /*is_declaration=*/true);
16248   /* Get the namespace being used.  */
16249   namespace_decl = cp_parser_namespace_name (parser);
16250   /* And any specified attributes.  */
16251   attribs = cp_parser_attributes_opt (parser);
16252   /* Update the symbol table.  */
16253   parse_using_directive (namespace_decl, attribs);
16254   /* Look for the final `;'.  */
16255   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16256 }
16257 
16258 /* Parse an asm-definition.
16259 
16260    asm-definition:
16261      asm ( string-literal ) ;
16262 
16263    GNU Extension:
16264 
16265    asm-definition:
16266      asm volatile [opt] ( string-literal ) ;
16267      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16268      asm volatile [opt] ( string-literal : asm-operand-list [opt]
16269 			  : asm-operand-list [opt] ) ;
16270      asm volatile [opt] ( string-literal : asm-operand-list [opt]
16271 			  : asm-operand-list [opt]
16272 			  : asm-clobber-list [opt] ) ;
16273      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16274 			       : asm-clobber-list [opt]
16275 			       : asm-goto-list ) ;  */
16276 
16277 static void
cp_parser_asm_definition(cp_parser * parser)16278 cp_parser_asm_definition (cp_parser* parser)
16279 {
16280   tree string;
16281   tree outputs = NULL_TREE;
16282   tree inputs = NULL_TREE;
16283   tree clobbers = NULL_TREE;
16284   tree labels = NULL_TREE;
16285   tree asm_stmt;
16286   bool volatile_p = false;
16287   bool extended_p = false;
16288   bool invalid_inputs_p = false;
16289   bool invalid_outputs_p = false;
16290   bool goto_p = false;
16291   required_token missing = RT_NONE;
16292 
16293   /* Look for the `asm' keyword.  */
16294   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16295   /* See if the next token is `volatile'.  */
16296   if (cp_parser_allow_gnu_extensions_p (parser)
16297       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16298     {
16299       /* Remember that we saw the `volatile' keyword.  */
16300       volatile_p = true;
16301       /* Consume the token.  */
16302       cp_lexer_consume_token (parser->lexer);
16303     }
16304   if (cp_parser_allow_gnu_extensions_p (parser)
16305       && parser->in_function_body
16306       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16307     {
16308       /* Remember that we saw the `goto' keyword.  */
16309       goto_p = true;
16310       /* Consume the token.  */
16311       cp_lexer_consume_token (parser->lexer);
16312     }
16313   /* Look for the opening `('.  */
16314   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16315     return;
16316   /* Look for the string.  */
16317   string = cp_parser_string_literal (parser, false, false);
16318   if (string == error_mark_node)
16319     {
16320       cp_parser_skip_to_closing_parenthesis (parser, true, false,
16321 					     /*consume_paren=*/true);
16322       return;
16323     }
16324 
16325   /* If we're allowing GNU extensions, check for the extended assembly
16326      syntax.  Unfortunately, the `:' tokens need not be separated by
16327      a space in C, and so, for compatibility, we tolerate that here
16328      too.  Doing that means that we have to treat the `::' operator as
16329      two `:' tokens.  */
16330   if (cp_parser_allow_gnu_extensions_p (parser)
16331       && parser->in_function_body
16332       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16333 	  || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16334     {
16335       bool inputs_p = false;
16336       bool clobbers_p = false;
16337       bool labels_p = false;
16338 
16339       /* The extended syntax was used.  */
16340       extended_p = true;
16341 
16342       /* Look for outputs.  */
16343       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16344 	{
16345 	  /* Consume the `:'.  */
16346 	  cp_lexer_consume_token (parser->lexer);
16347 	  /* Parse the output-operands.  */
16348 	  if (cp_lexer_next_token_is_not (parser->lexer,
16349 					  CPP_COLON)
16350 	      && cp_lexer_next_token_is_not (parser->lexer,
16351 					     CPP_SCOPE)
16352 	      && cp_lexer_next_token_is_not (parser->lexer,
16353 					     CPP_CLOSE_PAREN)
16354 	      && !goto_p)
16355 	    outputs = cp_parser_asm_operand_list (parser);
16356 
16357 	    if (outputs == error_mark_node)
16358 	      invalid_outputs_p = true;
16359 	}
16360       /* If the next token is `::', there are no outputs, and the
16361 	 next token is the beginning of the inputs.  */
16362       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16363 	/* The inputs are coming next.  */
16364 	inputs_p = true;
16365 
16366       /* Look for inputs.  */
16367       if (inputs_p
16368 	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16369 	{
16370 	  /* Consume the `:' or `::'.  */
16371 	  cp_lexer_consume_token (parser->lexer);
16372 	  /* Parse the output-operands.  */
16373 	  if (cp_lexer_next_token_is_not (parser->lexer,
16374 					  CPP_COLON)
16375 	      && cp_lexer_next_token_is_not (parser->lexer,
16376 					     CPP_SCOPE)
16377 	      && cp_lexer_next_token_is_not (parser->lexer,
16378 					     CPP_CLOSE_PAREN))
16379 	    inputs = cp_parser_asm_operand_list (parser);
16380 
16381 	    if (inputs == error_mark_node)
16382 	      invalid_inputs_p = true;
16383 	}
16384       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16385 	/* The clobbers are coming next.  */
16386 	clobbers_p = true;
16387 
16388       /* Look for clobbers.  */
16389       if (clobbers_p
16390 	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16391 	{
16392 	  clobbers_p = true;
16393 	  /* Consume the `:' or `::'.  */
16394 	  cp_lexer_consume_token (parser->lexer);
16395 	  /* Parse the clobbers.  */
16396 	  if (cp_lexer_next_token_is_not (parser->lexer,
16397 					  CPP_COLON)
16398 	      && cp_lexer_next_token_is_not (parser->lexer,
16399 					     CPP_CLOSE_PAREN))
16400 	    clobbers = cp_parser_asm_clobber_list (parser);
16401 	}
16402       else if (goto_p
16403 	       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16404 	/* The labels are coming next.  */
16405 	labels_p = true;
16406 
16407       /* Look for labels.  */
16408       if (labels_p
16409 	  || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16410 	{
16411 	  labels_p = true;
16412 	  /* Consume the `:' or `::'.  */
16413 	  cp_lexer_consume_token (parser->lexer);
16414 	  /* Parse the labels.  */
16415 	  labels = cp_parser_asm_label_list (parser);
16416 	}
16417 
16418       if (goto_p && !labels_p)
16419 	missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16420     }
16421   else if (goto_p)
16422     missing = RT_COLON_SCOPE;
16423 
16424   /* Look for the closing `)'.  */
16425   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16426 			  missing ? missing : RT_CLOSE_PAREN))
16427     cp_parser_skip_to_closing_parenthesis (parser, true, false,
16428 					   /*consume_paren=*/true);
16429   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16430 
16431   if (!invalid_inputs_p && !invalid_outputs_p)
16432     {
16433       /* Create the ASM_EXPR.  */
16434       if (parser->in_function_body)
16435 	{
16436 	  asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16437 				      inputs, clobbers, labels);
16438 	  /* If the extended syntax was not used, mark the ASM_EXPR.  */
16439 	  if (!extended_p)
16440 	    {
16441 	      tree temp = asm_stmt;
16442 	      if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16443 		temp = TREE_OPERAND (temp, 0);
16444 
16445 	      ASM_INPUT_P (temp) = 1;
16446 	    }
16447 	}
16448       else
16449 	add_asm_node (string);
16450     }
16451 }
16452 
16453 /* Declarators [gram.dcl.decl] */
16454 
16455 /* Parse an init-declarator.
16456 
16457    init-declarator:
16458      declarator initializer [opt]
16459 
16460    GNU Extension:
16461 
16462    init-declarator:
16463      declarator asm-specification [opt] attributes [opt] initializer [opt]
16464 
16465    function-definition:
16466      decl-specifier-seq [opt] declarator ctor-initializer [opt]
16467        function-body
16468      decl-specifier-seq [opt] declarator function-try-block
16469 
16470    GNU Extension:
16471 
16472    function-definition:
16473      __extension__ function-definition
16474 
16475    TM Extension:
16476 
16477    function-definition:
16478      decl-specifier-seq [opt] declarator function-transaction-block
16479 
16480    The DECL_SPECIFIERS apply to this declarator.  Returns a
16481    representation of the entity declared.  If MEMBER_P is TRUE, then
16482    this declarator appears in a class scope.  The new DECL created by
16483    this declarator is returned.
16484 
16485    The CHECKS are access checks that should be performed once we know
16486    what entity is being declared (and, therefore, what classes have
16487    befriended it).
16488 
16489    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16490    for a function-definition here as well.  If the declarator is a
16491    declarator for a function-definition, *FUNCTION_DEFINITION_P will
16492    be TRUE upon return.  By that point, the function-definition will
16493    have been completely parsed.
16494 
16495    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16496    is FALSE.
16497 
16498    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16499    parsed declaration if it is an uninitialized single declarator not followed
16500    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16501    if present, will not be consumed.  If returned, this declarator will be
16502    created with SD_INITIALIZED but will not call cp_finish_decl.  */
16503 
16504 static tree
cp_parser_init_declarator(cp_parser * parser,cp_decl_specifier_seq * decl_specifiers,vec<deferred_access_check,va_gc> * checks,bool function_definition_allowed_p,bool member_p,int declares_class_or_enum,bool * function_definition_p,tree * maybe_range_for_decl)16505 cp_parser_init_declarator (cp_parser* parser,
16506 			   cp_decl_specifier_seq *decl_specifiers,
16507 			   vec<deferred_access_check, va_gc> *checks,
16508 			   bool function_definition_allowed_p,
16509 			   bool member_p,
16510 			   int declares_class_or_enum,
16511 			   bool* function_definition_p,
16512 			   tree* maybe_range_for_decl)
16513 {
16514   cp_token *token = NULL, *asm_spec_start_token = NULL,
16515            *attributes_start_token = NULL;
16516   cp_declarator *declarator;
16517   tree prefix_attributes;
16518   tree attributes = NULL;
16519   tree asm_specification;
16520   tree initializer;
16521   tree decl = NULL_TREE;
16522   tree scope;
16523   int is_initialized;
16524   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
16525      initialized with "= ..", CPP_OPEN_PAREN if initialized with
16526      "(...)".  */
16527   enum cpp_ttype initialization_kind;
16528   bool is_direct_init = false;
16529   bool is_non_constant_init;
16530   int ctor_dtor_or_conv_p;
16531   bool friend_p;
16532   tree pushed_scope = NULL_TREE;
16533   bool range_for_decl_p = false;
16534   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16535 
16536   /* Gather the attributes that were provided with the
16537      decl-specifiers.  */
16538   prefix_attributes = decl_specifiers->attributes;
16539 
16540   /* Assume that this is not the declarator for a function
16541      definition.  */
16542   if (function_definition_p)
16543     *function_definition_p = false;
16544 
16545   /* Default arguments are only permitted for function parameters.  */
16546   if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16547     parser->default_arg_ok_p = false;
16548 
16549   /* Defer access checks while parsing the declarator; we cannot know
16550      what names are accessible until we know what is being
16551      declared.  */
16552   resume_deferring_access_checks ();
16553 
16554   /* Parse the declarator.  */
16555   token = cp_lexer_peek_token (parser->lexer);
16556   declarator
16557     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16558 			    &ctor_dtor_or_conv_p,
16559 			    /*parenthesized_p=*/NULL,
16560 			    member_p);
16561   /* Gather up the deferred checks.  */
16562   stop_deferring_access_checks ();
16563 
16564   parser->default_arg_ok_p = saved_default_arg_ok_p;
16565 
16566   /* If the DECLARATOR was erroneous, there's no need to go
16567      further.  */
16568   if (declarator == cp_error_declarator)
16569     return error_mark_node;
16570 
16571   /* Check that the number of template-parameter-lists is OK.  */
16572   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16573 						       token->location))
16574     return error_mark_node;
16575 
16576   if (declares_class_or_enum & 2)
16577     cp_parser_check_for_definition_in_return_type (declarator,
16578 						   decl_specifiers->type,
16579 						   decl_specifiers->locations[ds_type_spec]);
16580 
16581   /* Figure out what scope the entity declared by the DECLARATOR is
16582      located in.  `grokdeclarator' sometimes changes the scope, so
16583      we compute it now.  */
16584   scope = get_scope_of_declarator (declarator);
16585 
16586   /* Perform any lookups in the declared type which were thought to be
16587      dependent, but are not in the scope of the declarator.  */
16588   decl_specifiers->type
16589     = maybe_update_decl_type (decl_specifiers->type, scope);
16590 
16591   /* If we're allowing GNU extensions, look for an
16592      asm-specification.  */
16593   if (cp_parser_allow_gnu_extensions_p (parser))
16594     {
16595       /* Look for an asm-specification.  */
16596       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16597       asm_specification = cp_parser_asm_specification_opt (parser);
16598     }
16599   else
16600     asm_specification = NULL_TREE;
16601 
16602   /* Look for attributes.  */
16603   attributes_start_token = cp_lexer_peek_token (parser->lexer);
16604   attributes = cp_parser_attributes_opt (parser);
16605 
16606   /* Peek at the next token.  */
16607   token = cp_lexer_peek_token (parser->lexer);
16608 
16609   if (function_declarator_p (declarator))
16610     {
16611       /* Check to see if the token indicates the start of a
16612 	 function-definition.  */
16613       if (cp_parser_token_starts_function_definition_p (token))
16614 	{
16615 	  if (!function_definition_allowed_p)
16616 	    {
16617 	      /* If a function-definition should not appear here, issue an
16618 		 error message.  */
16619 	      cp_parser_error (parser,
16620 			       "a function-definition is not allowed here");
16621 	      return error_mark_node;
16622 	    }
16623 
16624 	  location_t func_brace_location
16625 	    = cp_lexer_peek_token (parser->lexer)->location;
16626 
16627 	  /* Neither attributes nor an asm-specification are allowed
16628 	     on a function-definition.  */
16629 	  if (asm_specification)
16630 	    error_at (asm_spec_start_token->location,
16631 		      "an asm-specification is not allowed "
16632 		      "on a function-definition");
16633 	  if (attributes)
16634 	    error_at (attributes_start_token->location,
16635 		      "attributes are not allowed "
16636 		      "on a function-definition");
16637 	  /* This is a function-definition.  */
16638 	  *function_definition_p = true;
16639 
16640 	  /* Parse the function definition.  */
16641 	  if (member_p)
16642 	    decl = cp_parser_save_member_function_body (parser,
16643 							decl_specifiers,
16644 							declarator,
16645 							prefix_attributes);
16646 	  else
16647 	    decl =
16648 	      (cp_parser_function_definition_from_specifiers_and_declarator
16649 	       (parser, decl_specifiers, prefix_attributes, declarator));
16650 
16651 	  if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16652 	    {
16653 	      /* This is where the prologue starts...  */
16654 	      DECL_STRUCT_FUNCTION (decl)->function_start_locus
16655 		= func_brace_location;
16656 	    }
16657 
16658 	  return decl;
16659 	}
16660     }
16661 
16662   /* [dcl.dcl]
16663 
16664      Only in function declarations for constructors, destructors, and
16665      type conversions can the decl-specifier-seq be omitted.
16666 
16667      We explicitly postpone this check past the point where we handle
16668      function-definitions because we tolerate function-definitions
16669      that are missing their return types in some modes.  */
16670   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16671     {
16672       cp_parser_error (parser,
16673 		       "expected constructor, destructor, or type conversion");
16674       return error_mark_node;
16675     }
16676 
16677   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
16678   if (token->type == CPP_EQ
16679       || token->type == CPP_OPEN_PAREN
16680       || token->type == CPP_OPEN_BRACE)
16681     {
16682       is_initialized = SD_INITIALIZED;
16683       initialization_kind = token->type;
16684       if (maybe_range_for_decl)
16685 	*maybe_range_for_decl = error_mark_node;
16686 
16687       if (token->type == CPP_EQ
16688 	  && function_declarator_p (declarator))
16689 	{
16690 	  cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16691 	  if (t2->keyword == RID_DEFAULT)
16692 	    is_initialized = SD_DEFAULTED;
16693 	  else if (t2->keyword == RID_DELETE)
16694 	    is_initialized = SD_DELETED;
16695 	}
16696     }
16697   else
16698     {
16699       /* If the init-declarator isn't initialized and isn't followed by a
16700 	 `,' or `;', it's not a valid init-declarator.  */
16701       if (token->type != CPP_COMMA
16702 	  && token->type != CPP_SEMICOLON)
16703 	{
16704 	  if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16705 	    range_for_decl_p = true;
16706 	  else
16707 	    {
16708 	      cp_parser_error (parser, "expected initializer");
16709 	      return error_mark_node;
16710 	    }
16711 	}
16712       is_initialized = SD_UNINITIALIZED;
16713       initialization_kind = CPP_EOF;
16714     }
16715 
16716   /* Because start_decl has side-effects, we should only call it if we
16717      know we're going ahead.  By this point, we know that we cannot
16718      possibly be looking at any other construct.  */
16719   cp_parser_commit_to_tentative_parse (parser);
16720 
16721   /* If the decl specifiers were bad, issue an error now that we're
16722      sure this was intended to be a declarator.  Then continue
16723      declaring the variable(s), as int, to try to cut down on further
16724      errors.  */
16725   if (decl_specifiers->any_specifiers_p
16726       && decl_specifiers->type == error_mark_node)
16727     {
16728       cp_parser_error (parser, "invalid type in declaration");
16729       decl_specifiers->type = integer_type_node;
16730     }
16731 
16732   /* Check to see whether or not this declaration is a friend.  */
16733   friend_p = cp_parser_friend_p (decl_specifiers);
16734 
16735   /* Enter the newly declared entry in the symbol table.  If we're
16736      processing a declaration in a class-specifier, we wait until
16737      after processing the initializer.  */
16738   if (!member_p)
16739     {
16740       if (parser->in_unbraced_linkage_specification_p)
16741 	decl_specifiers->storage_class = sc_extern;
16742       decl = start_decl (declarator, decl_specifiers,
16743 			 range_for_decl_p? SD_INITIALIZED : is_initialized,
16744 			 attributes, prefix_attributes, &pushed_scope);
16745       cp_finalize_omp_declare_simd (parser, decl);
16746       /* Adjust location of decl if declarator->id_loc is more appropriate:
16747 	 set, and decl wasn't merged with another decl, in which case its
16748 	 location would be different from input_location, and more accurate.  */
16749       if (DECL_P (decl)
16750 	  && declarator->id_loc != UNKNOWN_LOCATION
16751 	  && DECL_SOURCE_LOCATION (decl) == input_location)
16752 	DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16753     }
16754   else if (scope)
16755     /* Enter the SCOPE.  That way unqualified names appearing in the
16756        initializer will be looked up in SCOPE.  */
16757     pushed_scope = push_scope (scope);
16758 
16759   /* Perform deferred access control checks, now that we know in which
16760      SCOPE the declared entity resides.  */
16761   if (!member_p && decl)
16762     {
16763       tree saved_current_function_decl = NULL_TREE;
16764 
16765       /* If the entity being declared is a function, pretend that we
16766 	 are in its scope.  If it is a `friend', it may have access to
16767 	 things that would not otherwise be accessible.  */
16768       if (TREE_CODE (decl) == FUNCTION_DECL)
16769 	{
16770 	  saved_current_function_decl = current_function_decl;
16771 	  current_function_decl = decl;
16772 	}
16773 
16774       /* Perform access checks for template parameters.  */
16775       cp_parser_perform_template_parameter_access_checks (checks);
16776 
16777       /* Perform the access control checks for the declarator and the
16778 	 decl-specifiers.  */
16779       perform_deferred_access_checks (tf_warning_or_error);
16780 
16781       /* Restore the saved value.  */
16782       if (TREE_CODE (decl) == FUNCTION_DECL)
16783 	current_function_decl = saved_current_function_decl;
16784     }
16785 
16786   /* Parse the initializer.  */
16787   initializer = NULL_TREE;
16788   is_direct_init = false;
16789   is_non_constant_init = true;
16790   if (is_initialized)
16791     {
16792       if (function_declarator_p (declarator))
16793 	{
16794 	  cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16795 	   if (initialization_kind == CPP_EQ)
16796 	     initializer = cp_parser_pure_specifier (parser);
16797 	   else
16798 	     {
16799 	       /* If the declaration was erroneous, we don't really
16800 		  know what the user intended, so just silently
16801 		  consume the initializer.  */
16802 	       if (decl != error_mark_node)
16803 		 error_at (initializer_start_token->location,
16804 			   "initializer provided for function");
16805 	       cp_parser_skip_to_closing_parenthesis (parser,
16806 						      /*recovering=*/true,
16807 						      /*or_comma=*/false,
16808 						      /*consume_paren=*/true);
16809 	     }
16810 	}
16811       else
16812 	{
16813 	  /* We want to record the extra mangling scope for in-class
16814 	     initializers of class members and initializers of static data
16815 	     member templates.  The former involves deferring
16816 	     parsing of the initializer until end of class as with default
16817 	     arguments.  So right here we only handle the latter.  */
16818 	  if (!member_p && processing_template_decl)
16819 	    start_lambda_scope (decl);
16820 	  initializer = cp_parser_initializer (parser,
16821 					       &is_direct_init,
16822 					       &is_non_constant_init);
16823 	  if (!member_p && processing_template_decl)
16824 	    finish_lambda_scope ();
16825 	  if (initializer == error_mark_node)
16826 	    cp_parser_skip_to_end_of_statement (parser);
16827 	}
16828     }
16829 
16830   /* The old parser allows attributes to appear after a parenthesized
16831      initializer.  Mark Mitchell proposed removing this functionality
16832      on the GCC mailing lists on 2002-08-13.  This parser accepts the
16833      attributes -- but ignores them.  */
16834   if (cp_parser_allow_gnu_extensions_p (parser)
16835       && initialization_kind == CPP_OPEN_PAREN)
16836     if (cp_parser_attributes_opt (parser))
16837       warning (OPT_Wattributes,
16838 	       "attributes after parenthesized initializer ignored");
16839 
16840   /* A non-template declaration involving a function parameter list containing
16841      an implicit template parameter will have been made into a template.  If it
16842      turns out that the resulting declaration is not an actual function then
16843      finish the template declaration here.  An error message will already have
16844      been issued.  */
16845   if (parser->fully_implicit_function_template_p)
16846     if (!function_declarator_p (declarator))
16847       {
16848 	if (pushed_scope)
16849 	  {
16850 	    pop_scope (pushed_scope);
16851 	    pushed_scope = 0;
16852 	  }
16853 	finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
16854       }
16855 
16856   /* For an in-class declaration, use `grokfield' to create the
16857      declaration.  */
16858   if (member_p)
16859     {
16860       if (pushed_scope)
16861 	{
16862 	  pop_scope (pushed_scope);
16863 	  pushed_scope = NULL_TREE;
16864 	}
16865       decl = grokfield (declarator, decl_specifiers,
16866 			initializer, !is_non_constant_init,
16867 			/*asmspec=*/NULL_TREE,
16868 			chainon (attributes, prefix_attributes));
16869       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
16870 	cp_parser_save_default_args (parser, decl);
16871       cp_finalize_omp_declare_simd (parser, decl);
16872     }
16873 
16874   /* Finish processing the declaration.  But, skip member
16875      declarations.  */
16876   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
16877     {
16878       cp_finish_decl (decl,
16879 		      initializer, !is_non_constant_init,
16880 		      asm_specification,
16881 		      /* If the initializer is in parentheses, then this is
16882 			 a direct-initialization, which means that an
16883 			 `explicit' constructor is OK.  Otherwise, an
16884 			 `explicit' constructor cannot be used.  */
16885 		      ((is_direct_init || !is_initialized)
16886 		       ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16887     }
16888   else if ((cxx_dialect != cxx98) && friend_p
16889 	   && decl && TREE_CODE (decl) == FUNCTION_DECL)
16890     /* Core issue #226 (C++0x only): A default template-argument
16891        shall not be specified in a friend class template
16892        declaration. */
16893     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16894                              /*is_partial=*/false, /*is_friend_decl=*/1);
16895 
16896   if (!friend_p && pushed_scope)
16897     pop_scope (pushed_scope);
16898 
16899   if (function_declarator_p (declarator)
16900       && parser->fully_implicit_function_template_p)
16901     {
16902       if (member_p)
16903 	decl = finish_fully_implicit_template (parser, decl);
16904       else
16905 	finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
16906     }
16907 
16908   return decl;
16909 }
16910 
16911 /* Parse a declarator.
16912 
16913    declarator:
16914      direct-declarator
16915      ptr-operator declarator
16916 
16917    abstract-declarator:
16918      ptr-operator abstract-declarator [opt]
16919      direct-abstract-declarator
16920 
16921    GNU Extensions:
16922 
16923    declarator:
16924      attributes [opt] direct-declarator
16925      attributes [opt] ptr-operator declarator
16926 
16927    abstract-declarator:
16928      attributes [opt] ptr-operator abstract-declarator [opt]
16929      attributes [opt] direct-abstract-declarator
16930 
16931    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16932    detect constructor, destructor or conversion operators. It is set
16933    to -1 if the declarator is a name, and +1 if it is a
16934    function. Otherwise it is set to zero. Usually you just want to
16935    test for >0, but internally the negative value is used.
16936 
16937    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16938    a decl-specifier-seq unless it declares a constructor, destructor,
16939    or conversion.  It might seem that we could check this condition in
16940    semantic analysis, rather than parsing, but that makes it difficult
16941    to handle something like `f()'.  We want to notice that there are
16942    no decl-specifiers, and therefore realize that this is an
16943    expression, not a declaration.)
16944 
16945    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16946    the declarator is a direct-declarator of the form "(...)".
16947 
16948    MEMBER_P is true iff this declarator is a member-declarator.  */
16949 
16950 static cp_declarator *
cp_parser_declarator(cp_parser * parser,cp_parser_declarator_kind dcl_kind,int * ctor_dtor_or_conv_p,bool * parenthesized_p,bool member_p)16951 cp_parser_declarator (cp_parser* parser,
16952 		      cp_parser_declarator_kind dcl_kind,
16953 		      int* ctor_dtor_or_conv_p,
16954 		      bool* parenthesized_p,
16955 		      bool member_p)
16956 {
16957   cp_declarator *declarator;
16958   enum tree_code code;
16959   cp_cv_quals cv_quals;
16960   tree class_type;
16961   tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16962 
16963   /* Assume this is not a constructor, destructor, or type-conversion
16964      operator.  */
16965   if (ctor_dtor_or_conv_p)
16966     *ctor_dtor_or_conv_p = 0;
16967 
16968   if (cp_parser_allow_gnu_extensions_p (parser))
16969     gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16970 
16971   /* Check for the ptr-operator production.  */
16972   cp_parser_parse_tentatively (parser);
16973   /* Parse the ptr-operator.  */
16974   code = cp_parser_ptr_operator (parser,
16975 				 &class_type,
16976 				 &cv_quals,
16977 				 &std_attributes);
16978 
16979   /* If that worked, then we have a ptr-operator.  */
16980   if (cp_parser_parse_definitely (parser))
16981     {
16982       /* If a ptr-operator was found, then this declarator was not
16983 	 parenthesized.  */
16984       if (parenthesized_p)
16985 	*parenthesized_p = true;
16986       /* The dependent declarator is optional if we are parsing an
16987 	 abstract-declarator.  */
16988       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16989 	cp_parser_parse_tentatively (parser);
16990 
16991       /* Parse the dependent declarator.  */
16992       declarator = cp_parser_declarator (parser, dcl_kind,
16993 					 /*ctor_dtor_or_conv_p=*/NULL,
16994 					 /*parenthesized_p=*/NULL,
16995 					 /*member_p=*/false);
16996 
16997       /* If we are parsing an abstract-declarator, we must handle the
16998 	 case where the dependent declarator is absent.  */
16999       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17000 	  && !cp_parser_parse_definitely (parser))
17001 	declarator = NULL;
17002 
17003       declarator = cp_parser_make_indirect_declarator
17004 	(code, class_type, cv_quals, declarator, std_attributes);
17005     }
17006   /* Everything else is a direct-declarator.  */
17007   else
17008     {
17009       if (parenthesized_p)
17010 	*parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17011 						   CPP_OPEN_PAREN);
17012       declarator = cp_parser_direct_declarator (parser, dcl_kind,
17013 						ctor_dtor_or_conv_p,
17014 						member_p);
17015     }
17016 
17017   if (gnu_attributes && declarator && declarator != cp_error_declarator)
17018     declarator->attributes = gnu_attributes;
17019   return declarator;
17020 }
17021 
17022 /* Parse a direct-declarator or direct-abstract-declarator.
17023 
17024    direct-declarator:
17025      declarator-id
17026      direct-declarator ( parameter-declaration-clause )
17027        cv-qualifier-seq [opt]
17028        ref-qualifier [opt]
17029        exception-specification [opt]
17030      direct-declarator [ constant-expression [opt] ]
17031      ( declarator )
17032 
17033    direct-abstract-declarator:
17034      direct-abstract-declarator [opt]
17035        ( parameter-declaration-clause )
17036        cv-qualifier-seq [opt]
17037        ref-qualifier [opt]
17038        exception-specification [opt]
17039      direct-abstract-declarator [opt] [ constant-expression [opt] ]
17040      ( abstract-declarator )
17041 
17042    Returns a representation of the declarator.  DCL_KIND is
17043    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17044    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
17045    we are parsing a direct-declarator.  It is
17046    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17047    of ambiguity we prefer an abstract declarator, as per
17048    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
17049    cp_parser_declarator.  */
17050 
17051 static cp_declarator *
cp_parser_direct_declarator(cp_parser * parser,cp_parser_declarator_kind dcl_kind,int * ctor_dtor_or_conv_p,bool member_p)17052 cp_parser_direct_declarator (cp_parser* parser,
17053 			     cp_parser_declarator_kind dcl_kind,
17054 			     int* ctor_dtor_or_conv_p,
17055 			     bool member_p)
17056 {
17057   cp_token *token;
17058   cp_declarator *declarator = NULL;
17059   tree scope = NULL_TREE;
17060   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17061   bool saved_in_declarator_p = parser->in_declarator_p;
17062   bool first = true;
17063   tree pushed_scope = NULL_TREE;
17064 
17065   while (true)
17066     {
17067       /* Peek at the next token.  */
17068       token = cp_lexer_peek_token (parser->lexer);
17069       if (token->type == CPP_OPEN_PAREN)
17070 	{
17071 	  /* This is either a parameter-declaration-clause, or a
17072 	     parenthesized declarator. When we know we are parsing a
17073 	     named declarator, it must be a parenthesized declarator
17074 	     if FIRST is true. For instance, `(int)' is a
17075 	     parameter-declaration-clause, with an omitted
17076 	     direct-abstract-declarator. But `((*))', is a
17077 	     parenthesized abstract declarator. Finally, when T is a
17078 	     template parameter `(T)' is a
17079 	     parameter-declaration-clause, and not a parenthesized
17080 	     named declarator.
17081 
17082 	     We first try and parse a parameter-declaration-clause,
17083 	     and then try a nested declarator (if FIRST is true).
17084 
17085 	     It is not an error for it not to be a
17086 	     parameter-declaration-clause, even when FIRST is
17087 	     false. Consider,
17088 
17089 	       int i (int);
17090 	       int i (3);
17091 
17092 	     The first is the declaration of a function while the
17093 	     second is the definition of a variable, including its
17094 	     initializer.
17095 
17096 	     Having seen only the parenthesis, we cannot know which of
17097 	     these two alternatives should be selected.  Even more
17098 	     complex are examples like:
17099 
17100 	       int i (int (a));
17101 	       int i (int (3));
17102 
17103 	     The former is a function-declaration; the latter is a
17104 	     variable initialization.
17105 
17106 	     Thus again, we try a parameter-declaration-clause, and if
17107 	     that fails, we back out and return.  */
17108 
17109 	  if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17110 	    {
17111 	      tree params;
17112 	      bool is_declarator = false;
17113 
17114 	      /* In a member-declarator, the only valid interpretation
17115 		 of a parenthesis is the start of a
17116 		 parameter-declaration-clause.  (It is invalid to
17117 		 initialize a static data member with a parenthesized
17118 		 initializer; only the "=" form of initialization is
17119 		 permitted.)  */
17120 	      if (!member_p)
17121 		cp_parser_parse_tentatively (parser);
17122 
17123 	      /* Consume the `('.  */
17124 	      cp_lexer_consume_token (parser->lexer);
17125 	      if (first)
17126 		{
17127 		  /* If this is going to be an abstract declarator, we're
17128 		     in a declarator and we can't have default args.  */
17129 		  parser->default_arg_ok_p = false;
17130 		  parser->in_declarator_p = true;
17131 		}
17132 
17133 	      begin_scope (sk_function_parms, NULL_TREE);
17134 
17135 	      /* Parse the parameter-declaration-clause.  */
17136 	      params = cp_parser_parameter_declaration_clause (parser);
17137 
17138 	      /* Consume the `)'.  */
17139 	      cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17140 
17141 	      /* If all went well, parse the cv-qualifier-seq,
17142 		 ref-qualifier and the exception-specification.  */
17143 	      if (member_p || cp_parser_parse_definitely (parser))
17144 		{
17145 		  cp_cv_quals cv_quals;
17146 		  cp_virt_specifiers virt_specifiers;
17147 		  cp_ref_qualifier ref_qual;
17148 		  tree exception_specification;
17149 		  tree late_return;
17150 		  tree attrs;
17151 		  bool memfn = (member_p || (pushed_scope
17152 					     && CLASS_TYPE_P (pushed_scope)));
17153 
17154 		  is_declarator = true;
17155 
17156 		  if (ctor_dtor_or_conv_p)
17157 		    *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17158 		  first = false;
17159 
17160 		  /* Parse the cv-qualifier-seq.  */
17161 		  cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17162 		  /* Parse the ref-qualifier. */
17163 		  ref_qual = cp_parser_ref_qualifier_opt (parser);
17164 		  /* And the exception-specification.  */
17165 		  exception_specification
17166 		    = cp_parser_exception_specification_opt (parser);
17167 
17168 		  attrs = cp_parser_std_attribute_spec_seq (parser);
17169 
17170 		  /* In here, we handle cases where attribute is used after
17171 		     the function declaration.  For example:
17172 		     void func (int x) __attribute__((vector(..)));  */
17173 		  if (flag_cilkplus
17174 		      && cp_next_tokens_can_be_gnu_attribute_p (parser))
17175 		    {
17176 		      cp_parser_parse_tentatively (parser);
17177 		      tree attr = cp_parser_gnu_attributes_opt (parser);
17178 		      if (cp_lexer_next_token_is_not (parser->lexer,
17179 						      CPP_SEMICOLON)
17180 			  && cp_lexer_next_token_is_not (parser->lexer,
17181 							 CPP_OPEN_BRACE))
17182 			cp_parser_abort_tentative_parse (parser);
17183 		      else if (!cp_parser_parse_definitely (parser))
17184 			;
17185 		      else
17186 			attrs = chainon (attr, attrs);
17187 		    }
17188 		  late_return = (cp_parser_late_return_type_opt
17189 				 (parser, declarator,
17190 				  memfn ? cv_quals : -1));
17191 
17192 
17193 		  /* Parse the virt-specifier-seq.  */
17194 		  virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17195 
17196 		  /* Create the function-declarator.  */
17197 		  declarator = make_call_declarator (declarator,
17198 						     params,
17199 						     cv_quals,
17200 						     virt_specifiers,
17201 						     ref_qual,
17202 						     exception_specification,
17203 						     late_return);
17204 		  declarator->std_attributes = attrs;
17205 		  /* Any subsequent parameter lists are to do with
17206 		     return type, so are not those of the declared
17207 		     function.  */
17208 		  parser->default_arg_ok_p = false;
17209 		}
17210 
17211 	      /* Remove the function parms from scope.  */
17212 	      pop_bindings_and_leave_scope ();
17213 
17214 	      if (is_declarator)
17215 		/* Repeat the main loop.  */
17216 		continue;
17217 	    }
17218 
17219 	  /* If this is the first, we can try a parenthesized
17220 	     declarator.  */
17221 	  if (first)
17222 	    {
17223 	      bool saved_in_type_id_in_expr_p;
17224 
17225 	      parser->default_arg_ok_p = saved_default_arg_ok_p;
17226 	      parser->in_declarator_p = saved_in_declarator_p;
17227 
17228 	      /* Consume the `('.  */
17229 	      cp_lexer_consume_token (parser->lexer);
17230 	      /* Parse the nested declarator.  */
17231 	      saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17232 	      parser->in_type_id_in_expr_p = true;
17233 	      declarator
17234 		= cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17235 					/*parenthesized_p=*/NULL,
17236 					member_p);
17237 	      parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17238 	      first = false;
17239 	      /* Expect a `)'.  */
17240 	      if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17241 		declarator = cp_error_declarator;
17242 	      if (declarator == cp_error_declarator)
17243 		break;
17244 
17245 	      goto handle_declarator;
17246 	    }
17247 	  /* Otherwise, we must be done.  */
17248 	  else
17249 	    break;
17250 	}
17251       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17252 	       && token->type == CPP_OPEN_SQUARE
17253 	       && !cp_next_tokens_can_be_attribute_p (parser))
17254 	{
17255 	  /* Parse an array-declarator.  */
17256 	  tree bounds, attrs;
17257 
17258 	  if (ctor_dtor_or_conv_p)
17259 	    *ctor_dtor_or_conv_p = 0;
17260 
17261 	  first = false;
17262 	  parser->default_arg_ok_p = false;
17263 	  parser->in_declarator_p = true;
17264 	  /* Consume the `['.  */
17265 	  cp_lexer_consume_token (parser->lexer);
17266 	  /* Peek at the next token.  */
17267 	  token = cp_lexer_peek_token (parser->lexer);
17268 	  /* If the next token is `]', then there is no
17269 	     constant-expression.  */
17270 	  if (token->type != CPP_CLOSE_SQUARE)
17271 	    {
17272 	      bool non_constant_p;
17273 	      bounds
17274 		= cp_parser_constant_expression (parser,
17275 						 /*allow_non_constant=*/true,
17276 						 &non_constant_p);
17277 	      if (!non_constant_p)
17278 		/* OK */;
17279 	      else if (error_operand_p (bounds))
17280 		/* Already gave an error.  */;
17281 	      else if (!parser->in_function_body
17282 		       || current_binding_level->kind == sk_function_parms)
17283 		{
17284 		  /* Normally, the array bound must be an integral constant
17285 		     expression.  However, as an extension, we allow VLAs
17286 		     in function scopes as long as they aren't part of a
17287 		     parameter declaration.  */
17288 		  cp_parser_error (parser,
17289 				   "array bound is not an integer constant");
17290 		  bounds = error_mark_node;
17291 		}
17292 	      else if (processing_template_decl
17293 		       && !type_dependent_expression_p (bounds))
17294 		{
17295 		  /* Remember this wasn't a constant-expression.  */
17296 		  bounds = build_nop (TREE_TYPE (bounds), bounds);
17297 		  TREE_SIDE_EFFECTS (bounds) = 1;
17298 		}
17299 	    }
17300 	  else
17301 	    bounds = NULL_TREE;
17302 	  /* Look for the closing `]'.  */
17303 	  if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17304 	    {
17305 	      declarator = cp_error_declarator;
17306 	      break;
17307 	    }
17308 
17309 	  attrs = cp_parser_std_attribute_spec_seq (parser);
17310 	  declarator = make_array_declarator (declarator, bounds);
17311 	  declarator->std_attributes = attrs;
17312 	}
17313       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17314 	{
17315 	  {
17316 	    tree qualifying_scope;
17317 	    tree unqualified_name;
17318 	    tree attrs;
17319 	    special_function_kind sfk;
17320 	    bool abstract_ok;
17321 	    bool pack_expansion_p = false;
17322 	    cp_token *declarator_id_start_token;
17323 
17324 	    /* Parse a declarator-id */
17325 	    abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17326 	    if (abstract_ok)
17327 	      {
17328 		cp_parser_parse_tentatively (parser);
17329 
17330 		/* If we see an ellipsis, we should be looking at a
17331 		   parameter pack. */
17332 		if (token->type == CPP_ELLIPSIS)
17333 		  {
17334 		    /* Consume the `...' */
17335 		    cp_lexer_consume_token (parser->lexer);
17336 
17337 		    pack_expansion_p = true;
17338 		  }
17339 	      }
17340 
17341 	    declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17342 	    unqualified_name
17343 	      = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17344 	    qualifying_scope = parser->scope;
17345 	    if (abstract_ok)
17346 	      {
17347 		bool okay = false;
17348 
17349 		if (!unqualified_name && pack_expansion_p)
17350 		  {
17351 		    /* Check whether an error occurred. */
17352 		    okay = !cp_parser_error_occurred (parser);
17353 
17354 		    /* We already consumed the ellipsis to mark a
17355 		       parameter pack, but we have no way to report it,
17356 		       so abort the tentative parse. We will be exiting
17357 		       immediately anyway. */
17358 		    cp_parser_abort_tentative_parse (parser);
17359 		  }
17360 		else
17361 		  okay = cp_parser_parse_definitely (parser);
17362 
17363 		if (!okay)
17364 		  unqualified_name = error_mark_node;
17365 		else if (unqualified_name
17366 			 && (qualifying_scope
17367 			     || (!identifier_p (unqualified_name))))
17368 		  {
17369 		    cp_parser_error (parser, "expected unqualified-id");
17370 		    unqualified_name = error_mark_node;
17371 		  }
17372 	      }
17373 
17374 	    if (!unqualified_name)
17375 	      return NULL;
17376 	    if (unqualified_name == error_mark_node)
17377 	      {
17378 		declarator = cp_error_declarator;
17379 		pack_expansion_p = false;
17380 		declarator->parameter_pack_p = false;
17381 		break;
17382 	      }
17383 
17384 	    attrs = cp_parser_std_attribute_spec_seq (parser);
17385 
17386 	    if (qualifying_scope && at_namespace_scope_p ()
17387 		&& TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17388 	      {
17389 		/* In the declaration of a member of a template class
17390 		   outside of the class itself, the SCOPE will sometimes
17391 		   be a TYPENAME_TYPE.  For example, given:
17392 
17393 		   template <typename T>
17394 		   int S<T>::R::i = 3;
17395 
17396 		   the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
17397 		   this context, we must resolve S<T>::R to an ordinary
17398 		   type, rather than a typename type.
17399 
17400 		   The reason we normally avoid resolving TYPENAME_TYPEs
17401 		   is that a specialization of `S' might render
17402 		   `S<T>::R' not a type.  However, if `S' is
17403 		   specialized, then this `i' will not be used, so there
17404 		   is no harm in resolving the types here.  */
17405 		tree type;
17406 
17407 		/* Resolve the TYPENAME_TYPE.  */
17408 		type = resolve_typename_type (qualifying_scope,
17409 					      /*only_current_p=*/false);
17410 		/* If that failed, the declarator is invalid.  */
17411 		if (TREE_CODE (type) == TYPENAME_TYPE)
17412 		  {
17413 		    if (typedef_variant_p (type))
17414 		      error_at (declarator_id_start_token->location,
17415 				"cannot define member of dependent typedef "
17416 				"%qT", type);
17417 		    else
17418 		      error_at (declarator_id_start_token->location,
17419 				"%<%T::%E%> is not a type",
17420 				TYPE_CONTEXT (qualifying_scope),
17421 				TYPE_IDENTIFIER (qualifying_scope));
17422 		  }
17423 		qualifying_scope = type;
17424 	      }
17425 
17426 	    sfk = sfk_none;
17427 
17428 	    if (unqualified_name)
17429 	      {
17430 		tree class_type;
17431 
17432 		if (qualifying_scope
17433 		    && CLASS_TYPE_P (qualifying_scope))
17434 		  class_type = qualifying_scope;
17435 		else
17436 		  class_type = current_class_type;
17437 
17438 		if (TREE_CODE (unqualified_name) == TYPE_DECL)
17439 		  {
17440 		    tree name_type = TREE_TYPE (unqualified_name);
17441 		    if (class_type && same_type_p (name_type, class_type))
17442 		      {
17443 			if (qualifying_scope
17444 			    && CLASSTYPE_USE_TEMPLATE (name_type))
17445 			  {
17446 			    error_at (declarator_id_start_token->location,
17447 				      "invalid use of constructor as a template");
17448 			    inform (declarator_id_start_token->location,
17449 				    "use %<%T::%D%> instead of %<%T::%D%> to "
17450 				    "name the constructor in a qualified name",
17451 				    class_type,
17452 				    DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17453 				    class_type, name_type);
17454 			    declarator = cp_error_declarator;
17455 			    break;
17456 			  }
17457 			else
17458 			  unqualified_name = constructor_name (class_type);
17459 		      }
17460 		    else
17461 		      {
17462 			/* We do not attempt to print the declarator
17463 			   here because we do not have enough
17464 			   information about its original syntactic
17465 			   form.  */
17466 			cp_parser_error (parser, "invalid declarator");
17467 			declarator = cp_error_declarator;
17468 			break;
17469 		      }
17470 		  }
17471 
17472 		if (class_type)
17473 		  {
17474 		    if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17475 		      sfk = sfk_destructor;
17476 		    else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17477 		      sfk = sfk_conversion;
17478 		    else if (/* There's no way to declare a constructor
17479 				for an anonymous type, even if the type
17480 				got a name for linkage purposes.  */
17481 			     !TYPE_WAS_ANONYMOUS (class_type)
17482 			     && constructor_name_p (unqualified_name,
17483 						    class_type))
17484 		      {
17485 			unqualified_name = constructor_name (class_type);
17486 			sfk = sfk_constructor;
17487 		      }
17488 		    else if (is_overloaded_fn (unqualified_name)
17489 			     && DECL_CONSTRUCTOR_P (get_first_fn
17490 						    (unqualified_name)))
17491 		      sfk = sfk_constructor;
17492 
17493 		    if (ctor_dtor_or_conv_p && sfk != sfk_none)
17494 		      *ctor_dtor_or_conv_p = -1;
17495 		  }
17496 	      }
17497 	    declarator = make_id_declarator (qualifying_scope,
17498 					     unqualified_name,
17499 					     sfk);
17500 	    declarator->std_attributes = attrs;
17501 	    declarator->id_loc = token->location;
17502 	    declarator->parameter_pack_p = pack_expansion_p;
17503 
17504 	    if (pack_expansion_p)
17505 	      maybe_warn_variadic_templates ();
17506 	  }
17507 
17508 	handle_declarator:;
17509 	  scope = get_scope_of_declarator (declarator);
17510 	  if (scope)
17511 	    {
17512 	      /* Any names that appear after the declarator-id for a
17513 		 member are looked up in the containing scope.  */
17514 	      if (at_function_scope_p ())
17515 		{
17516 		  /* But declarations with qualified-ids can't appear in a
17517 		     function.  */
17518 		  cp_parser_error (parser, "qualified-id in declaration");
17519 		  declarator = cp_error_declarator;
17520 		  break;
17521 		}
17522 	      pushed_scope = push_scope (scope);
17523 	    }
17524 	  parser->in_declarator_p = true;
17525 	  if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17526 	      || (declarator && declarator->kind == cdk_id))
17527 	    /* Default args are only allowed on function
17528 	       declarations.  */
17529 	    parser->default_arg_ok_p = saved_default_arg_ok_p;
17530 	  else
17531 	    parser->default_arg_ok_p = false;
17532 
17533 	  first = false;
17534 	}
17535       /* We're done.  */
17536       else
17537 	break;
17538     }
17539 
17540   /* For an abstract declarator, we might wind up with nothing at this
17541      point.  That's an error; the declarator is not optional.  */
17542   if (!declarator)
17543     cp_parser_error (parser, "expected declarator");
17544 
17545   /* If we entered a scope, we must exit it now.  */
17546   if (pushed_scope)
17547     pop_scope (pushed_scope);
17548 
17549   parser->default_arg_ok_p = saved_default_arg_ok_p;
17550   parser->in_declarator_p = saved_in_declarator_p;
17551 
17552   return declarator;
17553 }
17554 
17555 /* Parse a ptr-operator.
17556 
17557    ptr-operator:
17558      * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17559      * cv-qualifier-seq [opt]
17560      &
17561      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17562      nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17563 
17564    GNU Extension:
17565 
17566    ptr-operator:
17567      & cv-qualifier-seq [opt]
17568 
17569    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17570    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17571    an rvalue reference. In the case of a pointer-to-member, *TYPE is
17572    filled in with the TYPE containing the member.  *CV_QUALS is
17573    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17574    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
17575    Note that the tree codes returned by this function have nothing
17576    to do with the types of trees that will be eventually be created
17577    to represent the pointer or reference type being parsed. They are
17578    just constants with suggestive names. */
17579 static enum tree_code
cp_parser_ptr_operator(cp_parser * parser,tree * type,cp_cv_quals * cv_quals,tree * attributes)17580 cp_parser_ptr_operator (cp_parser* parser,
17581 			tree* type,
17582 			cp_cv_quals *cv_quals,
17583 			tree *attributes)
17584 {
17585   enum tree_code code = ERROR_MARK;
17586   cp_token *token;
17587   tree attrs = NULL_TREE;
17588 
17589   /* Assume that it's not a pointer-to-member.  */
17590   *type = NULL_TREE;
17591   /* And that there are no cv-qualifiers.  */
17592   *cv_quals = TYPE_UNQUALIFIED;
17593 
17594   /* Peek at the next token.  */
17595   token = cp_lexer_peek_token (parser->lexer);
17596 
17597   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
17598   if (token->type == CPP_MULT)
17599     code = INDIRECT_REF;
17600   else if (token->type == CPP_AND)
17601     code = ADDR_EXPR;
17602   else if ((cxx_dialect != cxx98) &&
17603 	   token->type == CPP_AND_AND) /* C++0x only */
17604     code = NON_LVALUE_EXPR;
17605 
17606   if (code != ERROR_MARK)
17607     {
17608       /* Consume the `*', `&' or `&&'.  */
17609       cp_lexer_consume_token (parser->lexer);
17610 
17611       /* A `*' can be followed by a cv-qualifier-seq, and so can a
17612 	 `&', if we are allowing GNU extensions.  (The only qualifier
17613 	 that can legally appear after `&' is `restrict', but that is
17614 	 enforced during semantic analysis.  */
17615       if (code == INDIRECT_REF
17616 	  || cp_parser_allow_gnu_extensions_p (parser))
17617 	*cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17618 
17619       attrs = cp_parser_std_attribute_spec_seq (parser);
17620       if (attributes != NULL)
17621 	*attributes = attrs;
17622     }
17623   else
17624     {
17625       /* Try the pointer-to-member case.  */
17626       cp_parser_parse_tentatively (parser);
17627       /* Look for the optional `::' operator.  */
17628       cp_parser_global_scope_opt (parser,
17629 				  /*current_scope_valid_p=*/false);
17630       /* Look for the nested-name specifier.  */
17631       token = cp_lexer_peek_token (parser->lexer);
17632       cp_parser_nested_name_specifier (parser,
17633 				       /*typename_keyword_p=*/false,
17634 				       /*check_dependency_p=*/true,
17635 				       /*type_p=*/false,
17636 				       /*is_declaration=*/false);
17637       /* If we found it, and the next token is a `*', then we are
17638 	 indeed looking at a pointer-to-member operator.  */
17639       if (!cp_parser_error_occurred (parser)
17640 	  && cp_parser_require (parser, CPP_MULT, RT_MULT))
17641 	{
17642 	  /* Indicate that the `*' operator was used.  */
17643 	  code = INDIRECT_REF;
17644 
17645 	  if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17646 	    error_at (token->location, "%qD is a namespace", parser->scope);
17647 	  else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17648 	    error_at (token->location, "cannot form pointer to member of "
17649 		      "non-class %q#T", parser->scope);
17650 	  else
17651 	    {
17652 	      /* The type of which the member is a member is given by the
17653 		 current SCOPE.  */
17654 	      *type = parser->scope;
17655 	      /* The next name will not be qualified.  */
17656 	      parser->scope = NULL_TREE;
17657 	      parser->qualifying_scope = NULL_TREE;
17658 	      parser->object_scope = NULL_TREE;
17659 	      /* Look for optional c++11 attributes.  */
17660 	      attrs = cp_parser_std_attribute_spec_seq (parser);
17661 	      if (attributes != NULL)
17662 		*attributes = attrs;
17663 	      /* Look for the optional cv-qualifier-seq.  */
17664 	      *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17665 	    }
17666 	}
17667       /* If that didn't work we don't have a ptr-operator.  */
17668       if (!cp_parser_parse_definitely (parser))
17669 	cp_parser_error (parser, "expected ptr-operator");
17670     }
17671 
17672   return code;
17673 }
17674 
17675 /* Parse an (optional) cv-qualifier-seq.
17676 
17677    cv-qualifier-seq:
17678      cv-qualifier cv-qualifier-seq [opt]
17679 
17680    cv-qualifier:
17681      const
17682      volatile
17683 
17684    GNU Extension:
17685 
17686    cv-qualifier:
17687      __restrict__
17688 
17689    Returns a bitmask representing the cv-qualifiers.  */
17690 
17691 static cp_cv_quals
cp_parser_cv_qualifier_seq_opt(cp_parser * parser)17692 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17693 {
17694   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17695 
17696   while (true)
17697     {
17698       cp_token *token;
17699       cp_cv_quals cv_qualifier;
17700 
17701       /* Peek at the next token.  */
17702       token = cp_lexer_peek_token (parser->lexer);
17703       /* See if it's a cv-qualifier.  */
17704       switch (token->keyword)
17705 	{
17706 	case RID_CONST:
17707 	  cv_qualifier = TYPE_QUAL_CONST;
17708 	  break;
17709 
17710 	case RID_VOLATILE:
17711 	  cv_qualifier = TYPE_QUAL_VOLATILE;
17712 	  break;
17713 
17714 	case RID_RESTRICT:
17715 	  cv_qualifier = TYPE_QUAL_RESTRICT;
17716 	  break;
17717 
17718 	default:
17719 	  cv_qualifier = TYPE_UNQUALIFIED;
17720 	  break;
17721 	}
17722 
17723       if (!cv_qualifier)
17724 	break;
17725 
17726       if (cv_quals & cv_qualifier)
17727 	{
17728 	  error_at (token->location, "duplicate cv-qualifier");
17729 	  cp_lexer_purge_token (parser->lexer);
17730 	}
17731       else
17732 	{
17733 	  cp_lexer_consume_token (parser->lexer);
17734 	  cv_quals |= cv_qualifier;
17735 	}
17736     }
17737 
17738   return cv_quals;
17739 }
17740 
17741 /* Parse an (optional) ref-qualifier
17742 
17743    ref-qualifier:
17744      &
17745      &&
17746 
17747    Returns cp_ref_qualifier representing ref-qualifier. */
17748 
17749 static cp_ref_qualifier
cp_parser_ref_qualifier_opt(cp_parser * parser)17750 cp_parser_ref_qualifier_opt (cp_parser* parser)
17751 {
17752   cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17753 
17754   /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532).  */
17755   if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
17756     return ref_qual;
17757 
17758   while (true)
17759     {
17760       cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17761       cp_token *token = cp_lexer_peek_token (parser->lexer);
17762 
17763       switch (token->type)
17764 	{
17765 	case CPP_AND:
17766 	  curr_ref_qual = REF_QUAL_LVALUE;
17767 	  break;
17768 
17769 	case CPP_AND_AND:
17770 	  curr_ref_qual = REF_QUAL_RVALUE;
17771 	  break;
17772 
17773 	default:
17774 	  curr_ref_qual = REF_QUAL_NONE;
17775 	  break;
17776 	}
17777 
17778       if (!curr_ref_qual)
17779 	break;
17780       else if (ref_qual)
17781 	{
17782 	  error_at (token->location, "multiple ref-qualifiers");
17783 	  cp_lexer_purge_token (parser->lexer);
17784 	}
17785       else
17786 	{
17787 	  ref_qual = curr_ref_qual;
17788 	  cp_lexer_consume_token (parser->lexer);
17789 	}
17790     }
17791 
17792   return ref_qual;
17793 }
17794 
17795 /* Parse an (optional) virt-specifier-seq.
17796 
17797    virt-specifier-seq:
17798      virt-specifier virt-specifier-seq [opt]
17799 
17800    virt-specifier:
17801      override
17802      final
17803 
17804    Returns a bitmask representing the virt-specifiers.  */
17805 
17806 static cp_virt_specifiers
cp_parser_virt_specifier_seq_opt(cp_parser * parser)17807 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
17808 {
17809   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17810 
17811   while (true)
17812     {
17813       cp_token *token;
17814       cp_virt_specifiers virt_specifier;
17815 
17816       /* Peek at the next token.  */
17817       token = cp_lexer_peek_token (parser->lexer);
17818       /* See if it's a virt-specifier-qualifier.  */
17819       if (token->type != CPP_NAME)
17820         break;
17821       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
17822         {
17823           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17824           virt_specifier = VIRT_SPEC_OVERRIDE;
17825         }
17826       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
17827         {
17828           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17829           virt_specifier = VIRT_SPEC_FINAL;
17830         }
17831       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
17832         {
17833           virt_specifier = VIRT_SPEC_FINAL;
17834         }
17835       else
17836 	break;
17837 
17838       if (virt_specifiers & virt_specifier)
17839 	{
17840 	  error_at (token->location, "duplicate virt-specifier");
17841 	  cp_lexer_purge_token (parser->lexer);
17842 	}
17843       else
17844 	{
17845 	  cp_lexer_consume_token (parser->lexer);
17846 	  virt_specifiers |= virt_specifier;
17847 	}
17848     }
17849   return virt_specifiers;
17850 }
17851 
17852 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17853    is in scope even though it isn't real.  */
17854 
17855 void
inject_this_parameter(tree ctype,cp_cv_quals quals)17856 inject_this_parameter (tree ctype, cp_cv_quals quals)
17857 {
17858   tree this_parm;
17859 
17860   if (current_class_ptr)
17861     {
17862       /* We don't clear this between NSDMIs.  Is it already what we want?  */
17863       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
17864       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
17865 	  && cp_type_quals (type) == quals)
17866 	return;
17867     }
17868 
17869   this_parm = build_this_parm (ctype, quals);
17870   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
17871   current_class_ptr = NULL_TREE;
17872   current_class_ref
17873     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
17874   current_class_ptr = this_parm;
17875 }
17876 
17877 /* Return true iff our current scope is a non-static data member
17878    initializer.  */
17879 
17880 bool
parsing_nsdmi(void)17881 parsing_nsdmi (void)
17882 {
17883   /* We recognize NSDMI context by the context-less 'this' pointer set up
17884      by the function above.  */
17885   if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
17886     return true;
17887   return false;
17888 }
17889 
17890 /* Parse a late-specified return type, if any.  This is not a separate
17891    non-terminal, but part of a function declarator, which looks like
17892 
17893    -> trailing-type-specifier-seq abstract-declarator(opt)
17894 
17895    Returns the type indicated by the type-id.
17896 
17897    In addition to this this parses any queued up omp declare simd
17898    clauses and Cilk Plus SIMD-enabled function's vector attributes.
17899 
17900    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17901    function.  */
17902 
17903 static tree
cp_parser_late_return_type_opt(cp_parser * parser,cp_declarator * declarator,cp_cv_quals quals)17904 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
17905 				cp_cv_quals quals)
17906 {
17907   cp_token *token;
17908   tree type = NULL_TREE;
17909   bool declare_simd_p = (parser->omp_declare_simd
17910 			 && declarator
17911 			 && declarator->kind == cdk_id);
17912 
17913   bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
17914 				&& declarator && declarator->kind == cdk_id);
17915 
17916   /* Peek at the next token.  */
17917   token = cp_lexer_peek_token (parser->lexer);
17918   /* A late-specified return type is indicated by an initial '->'. */
17919   if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
17920     return NULL_TREE;
17921 
17922   tree save_ccp = current_class_ptr;
17923   tree save_ccr = current_class_ref;
17924   if (quals >= 0)
17925     {
17926       /* DR 1207: 'this' is in scope in the trailing return type.  */
17927       inject_this_parameter (current_class_type, quals);
17928     }
17929 
17930   if (token->type == CPP_DEREF)
17931     {
17932       /* Consume the ->.  */
17933       cp_lexer_consume_token (parser->lexer);
17934 
17935       type = cp_parser_trailing_type_id (parser);
17936     }
17937 
17938   if (cilk_simd_fn_vector_p)
17939     declarator->std_attributes
17940       = cp_parser_late_parsing_cilk_simd_fn_info (parser,
17941 						  declarator->std_attributes);
17942   if (declare_simd_p)
17943     declarator->std_attributes
17944       = cp_parser_late_parsing_omp_declare_simd (parser,
17945 						 declarator->std_attributes);
17946 
17947   if (quals >= 0)
17948     {
17949       current_class_ptr = save_ccp;
17950       current_class_ref = save_ccr;
17951     }
17952 
17953   return type;
17954 }
17955 
17956 /* Parse a declarator-id.
17957 
17958    declarator-id:
17959      id-expression
17960      :: [opt] nested-name-specifier [opt] type-name
17961 
17962    In the `id-expression' case, the value returned is as for
17963    cp_parser_id_expression if the id-expression was an unqualified-id.
17964    If the id-expression was a qualified-id, then a SCOPE_REF is
17965    returned.  The first operand is the scope (either a NAMESPACE_DECL
17966    or TREE_TYPE), but the second is still just a representation of an
17967    unqualified-id.  */
17968 
17969 static tree
cp_parser_declarator_id(cp_parser * parser,bool optional_p)17970 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
17971 {
17972   tree id;
17973   /* The expression must be an id-expression.  Assume that qualified
17974      names are the names of types so that:
17975 
17976        template <class T>
17977        int S<T>::R::i = 3;
17978 
17979      will work; we must treat `S<T>::R' as the name of a type.
17980      Similarly, assume that qualified names are templates, where
17981      required, so that:
17982 
17983        template <class T>
17984        int S<T>::R<T>::i = 3;
17985 
17986      will work, too.  */
17987   id = cp_parser_id_expression (parser,
17988 				/*template_keyword_p=*/false,
17989 				/*check_dependency_p=*/false,
17990 				/*template_p=*/NULL,
17991 				/*declarator_p=*/true,
17992 				optional_p);
17993   if (id && BASELINK_P (id))
17994     id = BASELINK_FUNCTIONS (id);
17995   return id;
17996 }
17997 
17998 /* Parse a type-id.
17999 
18000    type-id:
18001      type-specifier-seq abstract-declarator [opt]
18002 
18003    Returns the TYPE specified.  */
18004 
18005 static tree
cp_parser_type_id_1(cp_parser * parser,bool is_template_arg,bool is_trailing_return)18006 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18007 		     bool is_trailing_return)
18008 {
18009   cp_decl_specifier_seq type_specifier_seq;
18010   cp_declarator *abstract_declarator;
18011 
18012   /* Parse the type-specifier-seq.  */
18013   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18014 				is_trailing_return,
18015 				&type_specifier_seq);
18016   if (type_specifier_seq.type == error_mark_node)
18017     return error_mark_node;
18018 
18019   /* There might or might not be an abstract declarator.  */
18020   cp_parser_parse_tentatively (parser);
18021   /* Look for the declarator.  */
18022   abstract_declarator
18023     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18024 			    /*parenthesized_p=*/NULL,
18025 			    /*member_p=*/false);
18026   /* Check to see if there really was a declarator.  */
18027   if (!cp_parser_parse_definitely (parser))
18028     abstract_declarator = NULL;
18029 
18030   if (type_specifier_seq.type
18031       /* None of the valid uses of 'auto' in C++14 involve the type-id
18032 	 nonterminal, but it is valid in a trailing-return-type.  */
18033       && !(cxx_dialect >= cxx1y && is_trailing_return)
18034       && type_uses_auto (type_specifier_seq.type))
18035     {
18036       /* A type-id with type 'auto' is only ok if the abstract declarator
18037 	 is a function declarator with a late-specified return type.  */
18038       if (abstract_declarator
18039 	  && abstract_declarator->kind == cdk_function
18040 	  && abstract_declarator->u.function.late_return_type)
18041 	/* OK */;
18042       else
18043 	{
18044 	  error ("invalid use of %<auto%>");
18045 	  return error_mark_node;
18046 	}
18047     }
18048 
18049   return groktypename (&type_specifier_seq, abstract_declarator,
18050 		       is_template_arg);
18051 }
18052 
cp_parser_type_id(cp_parser * parser)18053 static tree cp_parser_type_id (cp_parser *parser)
18054 {
18055   return cp_parser_type_id_1 (parser, false, false);
18056 }
18057 
cp_parser_template_type_arg(cp_parser * parser)18058 static tree cp_parser_template_type_arg (cp_parser *parser)
18059 {
18060   tree r;
18061   const char *saved_message = parser->type_definition_forbidden_message;
18062   parser->type_definition_forbidden_message
18063     = G_("types may not be defined in template arguments");
18064   r = cp_parser_type_id_1 (parser, true, false);
18065   parser->type_definition_forbidden_message = saved_message;
18066   if (cxx_dialect >= cxx1y && type_uses_auto (r))
18067     {
18068       error ("invalid use of %<auto%> in template argument");
18069       r = error_mark_node;
18070     }
18071   return r;
18072 }
18073 
cp_parser_trailing_type_id(cp_parser * parser)18074 static tree cp_parser_trailing_type_id (cp_parser *parser)
18075 {
18076   return cp_parser_type_id_1 (parser, false, true);
18077 }
18078 
18079 /* Parse a type-specifier-seq.
18080 
18081    type-specifier-seq:
18082      type-specifier type-specifier-seq [opt]
18083 
18084    GNU extension:
18085 
18086    type-specifier-seq:
18087      attributes type-specifier-seq [opt]
18088 
18089    If IS_DECLARATION is true, we are at the start of a "condition" or
18090    exception-declaration, so we might be followed by a declarator-id.
18091 
18092    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18093    i.e. we've just seen "->".
18094 
18095    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
18096 
18097 static void
cp_parser_type_specifier_seq(cp_parser * parser,bool is_declaration,bool is_trailing_return,cp_decl_specifier_seq * type_specifier_seq)18098 cp_parser_type_specifier_seq (cp_parser* parser,
18099 			      bool is_declaration,
18100 			      bool is_trailing_return,
18101 			      cp_decl_specifier_seq *type_specifier_seq)
18102 {
18103   bool seen_type_specifier = false;
18104   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18105   cp_token *start_token = NULL;
18106 
18107   /* Clear the TYPE_SPECIFIER_SEQ.  */
18108   clear_decl_specs (type_specifier_seq);
18109 
18110   /* In the context of a trailing return type, enum E { } is an
18111      elaborated-type-specifier followed by a function-body, not an
18112      enum-specifier.  */
18113   if (is_trailing_return)
18114     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18115 
18116   /* Parse the type-specifiers and attributes.  */
18117   while (true)
18118     {
18119       tree type_specifier;
18120       bool is_cv_qualifier;
18121 
18122       /* Check for attributes first.  */
18123       if (cp_next_tokens_can_be_attribute_p (parser))
18124 	{
18125 	  type_specifier_seq->attributes =
18126 	    chainon (type_specifier_seq->attributes,
18127 		     cp_parser_attributes_opt (parser));
18128 	  continue;
18129 	}
18130 
18131       /* record the token of the beginning of the type specifier seq,
18132          for error reporting purposes*/
18133      if (!start_token)
18134        start_token = cp_lexer_peek_token (parser->lexer);
18135 
18136       /* Look for the type-specifier.  */
18137       type_specifier = cp_parser_type_specifier (parser,
18138 						 flags,
18139 						 type_specifier_seq,
18140 						 /*is_declaration=*/false,
18141 						 NULL,
18142 						 &is_cv_qualifier);
18143       if (!type_specifier)
18144 	{
18145 	  /* If the first type-specifier could not be found, this is not a
18146 	     type-specifier-seq at all.  */
18147 	  if (!seen_type_specifier)
18148 	    {
18149 	      /* Set in_declarator_p to avoid skipping to the semicolon.  */
18150 	      int in_decl = parser->in_declarator_p;
18151 	      parser->in_declarator_p = true;
18152 
18153 	      if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18154 		  || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18155 		cp_parser_error (parser, "expected type-specifier");
18156 
18157 	      parser->in_declarator_p = in_decl;
18158 
18159 	      type_specifier_seq->type = error_mark_node;
18160 	      return;
18161 	    }
18162 	  /* If subsequent type-specifiers could not be found, the
18163 	     type-specifier-seq is complete.  */
18164 	  break;
18165 	}
18166 
18167       seen_type_specifier = true;
18168       /* The standard says that a condition can be:
18169 
18170 	    type-specifier-seq declarator = assignment-expression
18171 
18172 	 However, given:
18173 
18174 	   struct S {};
18175 	   if (int S = ...)
18176 
18177 	 we should treat the "S" as a declarator, not as a
18178 	 type-specifier.  The standard doesn't say that explicitly for
18179 	 type-specifier-seq, but it does say that for
18180 	 decl-specifier-seq in an ordinary declaration.  Perhaps it
18181 	 would be clearer just to allow a decl-specifier-seq here, and
18182 	 then add a semantic restriction that if any decl-specifiers
18183 	 that are not type-specifiers appear, the program is invalid.  */
18184       if (is_declaration && !is_cv_qualifier)
18185 	flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18186     }
18187 }
18188 
18189 /* Return whether the function currently being declared has an associated
18190    template parameter list.  */
18191 
18192 static bool
function_being_declared_is_template_p(cp_parser * parser)18193 function_being_declared_is_template_p (cp_parser* parser)
18194 {
18195   if (!current_template_parms || processing_template_parmlist)
18196     return false;
18197 
18198   if (parser->implicit_template_scope)
18199     return true;
18200 
18201   if (at_class_scope_p ()
18202       && TYPE_BEING_DEFINED (current_class_type))
18203     return parser->num_template_parameter_lists != 0;
18204 
18205   return ((int) parser->num_template_parameter_lists > template_class_depth
18206 	  (current_class_type));
18207 }
18208 
18209 /* Parse a parameter-declaration-clause.
18210 
18211    parameter-declaration-clause:
18212      parameter-declaration-list [opt] ... [opt]
18213      parameter-declaration-list , ...
18214 
18215    Returns a representation for the parameter declarations.  A return
18216    value of NULL indicates a parameter-declaration-clause consisting
18217    only of an ellipsis.  */
18218 
18219 static tree
cp_parser_parameter_declaration_clause(cp_parser * parser)18220 cp_parser_parameter_declaration_clause (cp_parser* parser)
18221 {
18222   tree parameters;
18223   cp_token *token;
18224   bool ellipsis_p;
18225   bool is_error;
18226 
18227   struct cleanup {
18228     cp_parser* parser;
18229     int auto_is_implicit_function_template_parm_p;
18230     ~cleanup() {
18231       parser->auto_is_implicit_function_template_parm_p
18232 	= auto_is_implicit_function_template_parm_p;
18233     }
18234   } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18235 
18236   (void) cleanup;
18237 
18238   if (!processing_specialization
18239       && !processing_template_parmlist
18240       && !processing_explicit_instantiation)
18241     if (!current_function_decl
18242 	|| (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18243       parser->auto_is_implicit_function_template_parm_p = true;
18244 
18245   /* Peek at the next token.  */
18246   token = cp_lexer_peek_token (parser->lexer);
18247   /* Check for trivial parameter-declaration-clauses.  */
18248   if (token->type == CPP_ELLIPSIS)
18249     {
18250       /* Consume the `...' token.  */
18251       cp_lexer_consume_token (parser->lexer);
18252       return NULL_TREE;
18253     }
18254   else if (token->type == CPP_CLOSE_PAREN)
18255     /* There are no parameters.  */
18256     {
18257 #ifndef NO_IMPLICIT_EXTERN_C
18258       if (in_system_header_at (input_location)
18259 	  && current_class_type == NULL
18260 	  && current_lang_name == lang_name_c)
18261 	return NULL_TREE;
18262       else
18263 #endif
18264 	return void_list_node;
18265     }
18266   /* Check for `(void)', too, which is a special case.  */
18267   else if (token->keyword == RID_VOID
18268 	   && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18269 	       == CPP_CLOSE_PAREN))
18270     {
18271       /* Consume the `void' token.  */
18272       cp_lexer_consume_token (parser->lexer);
18273       /* There are no parameters.  */
18274       return void_list_node;
18275     }
18276 
18277   /* Parse the parameter-declaration-list.  */
18278   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18279   /* If a parse error occurred while parsing the
18280      parameter-declaration-list, then the entire
18281      parameter-declaration-clause is erroneous.  */
18282   if (is_error)
18283     return NULL;
18284 
18285   /* Peek at the next token.  */
18286   token = cp_lexer_peek_token (parser->lexer);
18287   /* If it's a `,', the clause should terminate with an ellipsis.  */
18288   if (token->type == CPP_COMMA)
18289     {
18290       /* Consume the `,'.  */
18291       cp_lexer_consume_token (parser->lexer);
18292       /* Expect an ellipsis.  */
18293       ellipsis_p
18294 	= (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18295     }
18296   /* It might also be `...' if the optional trailing `,' was
18297      omitted.  */
18298   else if (token->type == CPP_ELLIPSIS)
18299     {
18300       /* Consume the `...' token.  */
18301       cp_lexer_consume_token (parser->lexer);
18302       /* And remember that we saw it.  */
18303       ellipsis_p = true;
18304     }
18305   else
18306     ellipsis_p = false;
18307 
18308   /* Finish the parameter list.  */
18309   if (!ellipsis_p)
18310     parameters = chainon (parameters, void_list_node);
18311 
18312   return parameters;
18313 }
18314 
18315 /* Parse a parameter-declaration-list.
18316 
18317    parameter-declaration-list:
18318      parameter-declaration
18319      parameter-declaration-list , parameter-declaration
18320 
18321    Returns a representation of the parameter-declaration-list, as for
18322    cp_parser_parameter_declaration_clause.  However, the
18323    `void_list_node' is never appended to the list.  Upon return,
18324    *IS_ERROR will be true iff an error occurred.  */
18325 
18326 static tree
cp_parser_parameter_declaration_list(cp_parser * parser,bool * is_error)18327 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18328 {
18329   tree parameters = NULL_TREE;
18330   tree *tail = &parameters;
18331   bool saved_in_unbraced_linkage_specification_p;
18332   int index = 0;
18333 
18334   /* Assume all will go well.  */
18335   *is_error = false;
18336   /* The special considerations that apply to a function within an
18337      unbraced linkage specifications do not apply to the parameters
18338      to the function.  */
18339   saved_in_unbraced_linkage_specification_p
18340     = parser->in_unbraced_linkage_specification_p;
18341   parser->in_unbraced_linkage_specification_p = false;
18342 
18343   /* Look for more parameters.  */
18344   while (true)
18345     {
18346       cp_parameter_declarator *parameter;
18347       tree decl = error_mark_node;
18348       bool parenthesized_p = false;
18349       int template_parm_idx = (function_being_declared_is_template_p (parser)?
18350 			       TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18351 						(current_template_parms)) : 0);
18352 
18353       /* Parse the parameter.  */
18354       parameter
18355 	= cp_parser_parameter_declaration (parser,
18356 					   /*template_parm_p=*/false,
18357 					   &parenthesized_p);
18358 
18359       /* We don't know yet if the enclosing context is deprecated, so wait
18360 	 and warn in grokparms if appropriate.  */
18361       deprecated_state = DEPRECATED_SUPPRESS;
18362 
18363       if (parameter)
18364 	{
18365 	  /* If a function parameter pack was specified and an implicit template
18366 	     parameter was introduced during cp_parser_parameter_declaration,
18367 	     change any implicit parameters introduced into packs.  */
18368 	  if (parser->implicit_template_parms
18369 	      && parameter->declarator
18370 	      && parameter->declarator->parameter_pack_p)
18371 	    {
18372 	      int latest_template_parm_idx = TREE_VEC_LENGTH
18373 		(INNERMOST_TEMPLATE_PARMS (current_template_parms));
18374 
18375 	      if (latest_template_parm_idx != template_parm_idx)
18376 		parameter->decl_specifiers.type = convert_generic_types_to_packs
18377 		  (parameter->decl_specifiers.type,
18378 		   template_parm_idx, latest_template_parm_idx);
18379 	    }
18380 
18381 	  decl = grokdeclarator (parameter->declarator,
18382 				 &parameter->decl_specifiers,
18383 				 PARM,
18384 				 parameter->default_argument != NULL_TREE,
18385 				 &parameter->decl_specifiers.attributes);
18386 	}
18387 
18388       deprecated_state = DEPRECATED_NORMAL;
18389 
18390       /* If a parse error occurred parsing the parameter declaration,
18391 	 then the entire parameter-declaration-list is erroneous.  */
18392       if (decl == error_mark_node)
18393 	{
18394 	  *is_error = true;
18395 	  parameters = error_mark_node;
18396 	  break;
18397 	}
18398 
18399       if (parameter->decl_specifiers.attributes)
18400 	cplus_decl_attributes (&decl,
18401 			       parameter->decl_specifiers.attributes,
18402 			       0);
18403       if (DECL_NAME (decl))
18404 	decl = pushdecl (decl);
18405 
18406       if (decl != error_mark_node)
18407 	{
18408 	  retrofit_lang_decl (decl);
18409 	  DECL_PARM_INDEX (decl) = ++index;
18410 	  DECL_PARM_LEVEL (decl) = function_parm_depth ();
18411 	}
18412 
18413       /* Add the new parameter to the list.  */
18414       *tail = build_tree_list (parameter->default_argument, decl);
18415       tail = &TREE_CHAIN (*tail);
18416 
18417       /* Peek at the next token.  */
18418       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18419 	  || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18420 	  /* These are for Objective-C++ */
18421 	  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18422 	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18423 	/* The parameter-declaration-list is complete.  */
18424 	break;
18425       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18426 	{
18427 	  cp_token *token;
18428 
18429 	  /* Peek at the next token.  */
18430 	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
18431 	  /* If it's an ellipsis, then the list is complete.  */
18432 	  if (token->type == CPP_ELLIPSIS)
18433 	    break;
18434 	  /* Otherwise, there must be more parameters.  Consume the
18435 	     `,'.  */
18436 	  cp_lexer_consume_token (parser->lexer);
18437 	  /* When parsing something like:
18438 
18439 		int i(float f, double d)
18440 
18441 	     we can tell after seeing the declaration for "f" that we
18442 	     are not looking at an initialization of a variable "i",
18443 	     but rather at the declaration of a function "i".
18444 
18445 	     Due to the fact that the parsing of template arguments
18446 	     (as specified to a template-id) requires backtracking we
18447 	     cannot use this technique when inside a template argument
18448 	     list.  */
18449 	  if (!parser->in_template_argument_list_p
18450 	      && !parser->in_type_id_in_expr_p
18451 	      && cp_parser_uncommitted_to_tentative_parse_p (parser)
18452 	      /* However, a parameter-declaration of the form
18453 		 "float(f)" (which is a valid declaration of a
18454 		 parameter "f") can also be interpreted as an
18455 		 expression (the conversion of "f" to "float").  */
18456 	      && !parenthesized_p)
18457 	    cp_parser_commit_to_tentative_parse (parser);
18458 	}
18459       else
18460 	{
18461 	  cp_parser_error (parser, "expected %<,%> or %<...%>");
18462 	  if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18463 	    cp_parser_skip_to_closing_parenthesis (parser,
18464 						   /*recovering=*/true,
18465 						   /*or_comma=*/false,
18466 						   /*consume_paren=*/false);
18467 	  break;
18468 	}
18469     }
18470 
18471   parser->in_unbraced_linkage_specification_p
18472     = saved_in_unbraced_linkage_specification_p;
18473 
18474   /* Reset implicit_template_scope if we are about to leave the function
18475      parameter list that introduced it.  Note that for out-of-line member
18476      definitions, there will be one or more class scopes before we get to
18477      the template parameter scope.  */
18478 
18479   if (cp_binding_level *its = parser->implicit_template_scope)
18480     if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18481       {
18482 	while (maybe_its->kind == sk_class)
18483 	  maybe_its = maybe_its->level_chain;
18484 	if (maybe_its == its)
18485 	  {
18486 	    parser->implicit_template_parms = 0;
18487 	    parser->implicit_template_scope = 0;
18488 	  }
18489       }
18490 
18491   return parameters;
18492 }
18493 
18494 /* Parse a parameter declaration.
18495 
18496    parameter-declaration:
18497      decl-specifier-seq ... [opt] declarator
18498      decl-specifier-seq declarator = assignment-expression
18499      decl-specifier-seq ... [opt] abstract-declarator [opt]
18500      decl-specifier-seq abstract-declarator [opt] = assignment-expression
18501 
18502    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18503    declares a template parameter.  (In that case, a non-nested `>'
18504    token encountered during the parsing of the assignment-expression
18505    is not interpreted as a greater-than operator.)
18506 
18507    Returns a representation of the parameter, or NULL if an error
18508    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18509    true iff the declarator is of the form "(p)".  */
18510 
18511 static cp_parameter_declarator *
cp_parser_parameter_declaration(cp_parser * parser,bool template_parm_p,bool * parenthesized_p)18512 cp_parser_parameter_declaration (cp_parser *parser,
18513 				 bool template_parm_p,
18514 				 bool *parenthesized_p)
18515 {
18516   int declares_class_or_enum;
18517   cp_decl_specifier_seq decl_specifiers;
18518   cp_declarator *declarator;
18519   tree default_argument;
18520   cp_token *token = NULL, *declarator_token_start = NULL;
18521   const char *saved_message;
18522 
18523   /* In a template parameter, `>' is not an operator.
18524 
18525      [temp.param]
18526 
18527      When parsing a default template-argument for a non-type
18528      template-parameter, the first non-nested `>' is taken as the end
18529      of the template parameter-list rather than a greater-than
18530      operator.  */
18531 
18532   /* Type definitions may not appear in parameter types.  */
18533   saved_message = parser->type_definition_forbidden_message;
18534   parser->type_definition_forbidden_message
18535     = G_("types may not be defined in parameter types");
18536 
18537   /* Parse the declaration-specifiers.  */
18538   cp_parser_decl_specifier_seq (parser,
18539 				CP_PARSER_FLAGS_NONE,
18540 				&decl_specifiers,
18541 				&declares_class_or_enum);
18542 
18543   /* Complain about missing 'typename' or other invalid type names.  */
18544   if (!decl_specifiers.any_type_specifiers_p
18545       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18546     decl_specifiers.type = error_mark_node;
18547 
18548   /* If an error occurred, there's no reason to attempt to parse the
18549      rest of the declaration.  */
18550   if (cp_parser_error_occurred (parser))
18551     {
18552       parser->type_definition_forbidden_message = saved_message;
18553       return NULL;
18554     }
18555 
18556   /* Peek at the next token.  */
18557   token = cp_lexer_peek_token (parser->lexer);
18558 
18559   /* If the next token is a `)', `,', `=', `>', or `...', then there
18560      is no declarator. However, when variadic templates are enabled,
18561      there may be a declarator following `...'.  */
18562   if (token->type == CPP_CLOSE_PAREN
18563       || token->type == CPP_COMMA
18564       || token->type == CPP_EQ
18565       || token->type == CPP_GREATER)
18566     {
18567       declarator = NULL;
18568       if (parenthesized_p)
18569 	*parenthesized_p = false;
18570     }
18571   /* Otherwise, there should be a declarator.  */
18572   else
18573     {
18574       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18575       parser->default_arg_ok_p = false;
18576 
18577       /* After seeing a decl-specifier-seq, if the next token is not a
18578 	 "(", there is no possibility that the code is a valid
18579 	 expression.  Therefore, if parsing tentatively, we commit at
18580 	 this point.  */
18581       if (!parser->in_template_argument_list_p
18582 	  /* In an expression context, having seen:
18583 
18584 	       (int((char ...
18585 
18586 	     we cannot be sure whether we are looking at a
18587 	     function-type (taking a "char" as a parameter) or a cast
18588 	     of some object of type "char" to "int".  */
18589 	  && !parser->in_type_id_in_expr_p
18590 	  && cp_parser_uncommitted_to_tentative_parse_p (parser)
18591 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18592 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18593 	cp_parser_commit_to_tentative_parse (parser);
18594       /* Parse the declarator.  */
18595       declarator_token_start = token;
18596       declarator = cp_parser_declarator (parser,
18597 					 CP_PARSER_DECLARATOR_EITHER,
18598 					 /*ctor_dtor_or_conv_p=*/NULL,
18599 					 parenthesized_p,
18600 					 /*member_p=*/false);
18601       parser->default_arg_ok_p = saved_default_arg_ok_p;
18602       /* After the declarator, allow more attributes.  */
18603       decl_specifiers.attributes
18604 	= chainon (decl_specifiers.attributes,
18605 		   cp_parser_attributes_opt (parser));
18606     }
18607 
18608   /* If the next token is an ellipsis, and we have not seen a
18609      declarator name, and the type of the declarator contains parameter
18610      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18611      a parameter pack expansion expression. Otherwise, leave the
18612      ellipsis for a C-style variadic function. */
18613   token = cp_lexer_peek_token (parser->lexer);
18614   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18615     {
18616       tree type = decl_specifiers.type;
18617 
18618       if (type && DECL_P (type))
18619         type = TREE_TYPE (type);
18620 
18621       if (type
18622 	  && TREE_CODE (type) != TYPE_PACK_EXPANSION
18623 	  && declarator_can_be_parameter_pack (declarator)
18624           && (!declarator || !declarator->parameter_pack_p)
18625           && uses_parameter_packs (type))
18626         {
18627 	  /* Consume the `...'. */
18628 	  cp_lexer_consume_token (parser->lexer);
18629 	  maybe_warn_variadic_templates ();
18630 
18631 	  /* Build a pack expansion type */
18632 	  if (declarator)
18633 	    declarator->parameter_pack_p = true;
18634 	  else
18635 	    decl_specifiers.type = make_pack_expansion (type);
18636 	}
18637     }
18638 
18639   /* The restriction on defining new types applies only to the type
18640      of the parameter, not to the default argument.  */
18641   parser->type_definition_forbidden_message = saved_message;
18642 
18643   /* If the next token is `=', then process a default argument.  */
18644   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18645     {
18646       token = cp_lexer_peek_token (parser->lexer);
18647       /* If we are defining a class, then the tokens that make up the
18648 	 default argument must be saved and processed later.  */
18649       if (!template_parm_p && at_class_scope_p ()
18650 	  && TYPE_BEING_DEFINED (current_class_type)
18651 	  && !LAMBDA_TYPE_P (current_class_type))
18652 	default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18653       /* Outside of a class definition, we can just parse the
18654 	 assignment-expression.  */
18655       else
18656 	default_argument
18657 	  = cp_parser_default_argument (parser, template_parm_p);
18658 
18659       if (!parser->default_arg_ok_p)
18660 	{
18661 	  if (flag_permissive)
18662 	    warning (0, "deprecated use of default argument for parameter of non-function");
18663 	  else
18664 	    {
18665 	      error_at (token->location,
18666 			"default arguments are only "
18667 			"permitted for function parameters");
18668 	      default_argument = NULL_TREE;
18669 	    }
18670 	}
18671       else if ((declarator && declarator->parameter_pack_p)
18672 	       || (decl_specifiers.type
18673 		   && PACK_EXPANSION_P (decl_specifiers.type)))
18674 	{
18675 	  /* Find the name of the parameter pack.  */
18676 	  cp_declarator *id_declarator = declarator;
18677 	  while (id_declarator && id_declarator->kind != cdk_id)
18678 	    id_declarator = id_declarator->declarator;
18679 
18680 	  if (id_declarator && id_declarator->kind == cdk_id)
18681 	    error_at (declarator_token_start->location,
18682 		      template_parm_p
18683 		      ? G_("template parameter pack %qD "
18684 			   "cannot have a default argument")
18685 		      : G_("parameter pack %qD cannot have "
18686 			   "a default argument"),
18687 		      id_declarator->u.id.unqualified_name);
18688 	  else
18689 	    error_at (declarator_token_start->location,
18690 		      template_parm_p
18691 		      ? G_("template parameter pack cannot have "
18692 			   "a default argument")
18693 		      : G_("parameter pack cannot have a "
18694 			   "default argument"));
18695 
18696 	  default_argument = NULL_TREE;
18697 	}
18698     }
18699   else
18700     default_argument = NULL_TREE;
18701 
18702   return make_parameter_declarator (&decl_specifiers,
18703 				    declarator,
18704 				    default_argument);
18705 }
18706 
18707 /* Parse a default argument and return it.
18708 
18709    TEMPLATE_PARM_P is true if this is a default argument for a
18710    non-type template parameter.  */
18711 static tree
cp_parser_default_argument(cp_parser * parser,bool template_parm_p)18712 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18713 {
18714   tree default_argument = NULL_TREE;
18715   bool saved_greater_than_is_operator_p;
18716   bool saved_local_variables_forbidden_p;
18717   bool non_constant_p, is_direct_init;
18718 
18719   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18720      set correctly.  */
18721   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18722   parser->greater_than_is_operator_p = !template_parm_p;
18723   /* Local variable names (and the `this' keyword) may not
18724      appear in a default argument.  */
18725   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18726   parser->local_variables_forbidden_p = true;
18727   /* Parse the assignment-expression.  */
18728   if (template_parm_p)
18729     push_deferring_access_checks (dk_no_deferred);
18730   tree saved_class_ptr = NULL_TREE;
18731   tree saved_class_ref = NULL_TREE;
18732   /* The "this" pointer is not valid in a default argument.  */
18733   if (cfun)
18734     {
18735       saved_class_ptr = current_class_ptr;
18736       cp_function_chain->x_current_class_ptr = NULL_TREE;
18737       saved_class_ref = current_class_ref;
18738       cp_function_chain->x_current_class_ref = NULL_TREE;
18739     }
18740   default_argument
18741     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18742   /* Restore the "this" pointer.  */
18743   if (cfun)
18744     {
18745       cp_function_chain->x_current_class_ptr = saved_class_ptr;
18746       cp_function_chain->x_current_class_ref = saved_class_ref;
18747     }
18748   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18749     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18750   if (template_parm_p)
18751     pop_deferring_access_checks ();
18752   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18753   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18754 
18755   return default_argument;
18756 }
18757 
18758 /* Parse a function-body.
18759 
18760    function-body:
18761      compound_statement  */
18762 
18763 static void
cp_parser_function_body(cp_parser * parser,bool in_function_try_block)18764 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18765 {
18766   cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18767 }
18768 
18769 /* Parse a ctor-initializer-opt followed by a function-body.  Return
18770    true if a ctor-initializer was present.  When IN_FUNCTION_TRY_BLOCK
18771    is true we are parsing a function-try-block.  */
18772 
18773 static bool
cp_parser_ctor_initializer_opt_and_function_body(cp_parser * parser,bool in_function_try_block)18774 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18775 						  bool in_function_try_block)
18776 {
18777   tree body, list;
18778   bool ctor_initializer_p;
18779   const bool check_body_p =
18780      DECL_CONSTRUCTOR_P (current_function_decl)
18781      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18782   tree last = NULL;
18783 
18784   /* Begin the function body.  */
18785   body = begin_function_body ();
18786   /* Parse the optional ctor-initializer.  */
18787   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
18788 
18789   /* If we're parsing a constexpr constructor definition, we need
18790      to check that the constructor body is indeed empty.  However,
18791      before we get to cp_parser_function_body lot of junk has been
18792      generated, so we can't just check that we have an empty block.
18793      Rather we take a snapshot of the outermost block, and check whether
18794      cp_parser_function_body changed its state.  */
18795   if (check_body_p)
18796     {
18797       list = cur_stmt_list;
18798       if (STATEMENT_LIST_TAIL (list))
18799 	last = STATEMENT_LIST_TAIL (list)->stmt;
18800     }
18801   /* Parse the function-body.  */
18802   cp_parser_function_body (parser, in_function_try_block);
18803   if (check_body_p)
18804     check_constexpr_ctor_body (last, list);
18805   /* Finish the function body.  */
18806   finish_function_body (body);
18807 
18808   return ctor_initializer_p;
18809 }
18810 
18811 /* Parse an initializer.
18812 
18813    initializer:
18814      = initializer-clause
18815      ( expression-list )
18816 
18817    Returns an expression representing the initializer.  If no
18818    initializer is present, NULL_TREE is returned.
18819 
18820    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
18821    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
18822    set to TRUE if there is no initializer present.  If there is an
18823    initializer, and it is not a constant-expression, *NON_CONSTANT_P
18824    is set to true; otherwise it is set to false.  */
18825 
18826 static tree
cp_parser_initializer(cp_parser * parser,bool * is_direct_init,bool * non_constant_p)18827 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
18828 		       bool* non_constant_p)
18829 {
18830   cp_token *token;
18831   tree init;
18832 
18833   /* Peek at the next token.  */
18834   token = cp_lexer_peek_token (parser->lexer);
18835 
18836   /* Let our caller know whether or not this initializer was
18837      parenthesized.  */
18838   *is_direct_init = (token->type != CPP_EQ);
18839   /* Assume that the initializer is constant.  */
18840   *non_constant_p = false;
18841 
18842   if (token->type == CPP_EQ)
18843     {
18844       /* Consume the `='.  */
18845       cp_lexer_consume_token (parser->lexer);
18846       /* Parse the initializer-clause.  */
18847       init = cp_parser_initializer_clause (parser, non_constant_p);
18848     }
18849   else if (token->type == CPP_OPEN_PAREN)
18850     {
18851       vec<tree, va_gc> *vec;
18852       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
18853 						     /*cast_p=*/false,
18854 						     /*allow_expansion_p=*/true,
18855 						     non_constant_p);
18856       if (vec == NULL)
18857 	return error_mark_node;
18858       init = build_tree_list_vec (vec);
18859       release_tree_vector (vec);
18860     }
18861   else if (token->type == CPP_OPEN_BRACE)
18862     {
18863       cp_lexer_set_source_position (parser->lexer);
18864       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18865       init = cp_parser_braced_list (parser, non_constant_p);
18866       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
18867     }
18868   else
18869     {
18870       /* Anything else is an error.  */
18871       cp_parser_error (parser, "expected initializer");
18872       init = error_mark_node;
18873     }
18874 
18875   return init;
18876 }
18877 
18878 /* Parse an initializer-clause.
18879 
18880    initializer-clause:
18881      assignment-expression
18882      braced-init-list
18883 
18884    Returns an expression representing the initializer.
18885 
18886    If the `assignment-expression' production is used the value
18887    returned is simply a representation for the expression.
18888 
18889    Otherwise, calls cp_parser_braced_list.  */
18890 
18891 static tree
cp_parser_initializer_clause(cp_parser * parser,bool * non_constant_p)18892 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
18893 {
18894   tree initializer;
18895 
18896   /* Assume the expression is constant.  */
18897   *non_constant_p = false;
18898 
18899   /* If it is not a `{', then we are looking at an
18900      assignment-expression.  */
18901   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
18902     {
18903       initializer
18904 	= cp_parser_constant_expression (parser,
18905 					/*allow_non_constant_p=*/true,
18906 					non_constant_p);
18907     }
18908   else
18909     initializer = cp_parser_braced_list (parser, non_constant_p);
18910 
18911   return initializer;
18912 }
18913 
18914 /* Parse a brace-enclosed initializer list.
18915 
18916    braced-init-list:
18917      { initializer-list , [opt] }
18918      { }
18919 
18920    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
18921    the elements of the initializer-list (or NULL, if the last
18922    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
18923    NULL_TREE.  There is no way to detect whether or not the optional
18924    trailing `,' was provided.  NON_CONSTANT_P is as for
18925    cp_parser_initializer.  */
18926 
18927 static tree
cp_parser_braced_list(cp_parser * parser,bool * non_constant_p)18928 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
18929 {
18930   tree initializer;
18931 
18932   /* Consume the `{' token.  */
18933   cp_lexer_consume_token (parser->lexer);
18934   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
18935   initializer = make_node (CONSTRUCTOR);
18936   /* If it's not a `}', then there is a non-trivial initializer.  */
18937   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
18938     {
18939       /* Parse the initializer list.  */
18940       CONSTRUCTOR_ELTS (initializer)
18941 	= cp_parser_initializer_list (parser, non_constant_p);
18942       /* A trailing `,' token is allowed.  */
18943       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18944 	cp_lexer_consume_token (parser->lexer);
18945     }
18946   else
18947     *non_constant_p = false;
18948   /* Now, there should be a trailing `}'.  */
18949   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18950   TREE_TYPE (initializer) = init_list_type_node;
18951   return initializer;
18952 }
18953 
18954 /* Parse an initializer-list.
18955 
18956    initializer-list:
18957      initializer-clause ... [opt]
18958      initializer-list , initializer-clause ... [opt]
18959 
18960    GNU Extension:
18961 
18962    initializer-list:
18963      designation initializer-clause ...[opt]
18964      initializer-list , designation initializer-clause ...[opt]
18965 
18966    designation:
18967      . identifier =
18968      identifier :
18969      [ constant-expression ] =
18970 
18971    Returns a vec of constructor_elt.  The VALUE of each elt is an expression
18972    for the initializer.  If the INDEX of the elt is non-NULL, it is the
18973    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
18974    as for cp_parser_initializer.  */
18975 
18976 static vec<constructor_elt, va_gc> *
cp_parser_initializer_list(cp_parser * parser,bool * non_constant_p)18977 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
18978 {
18979   vec<constructor_elt, va_gc> *v = NULL;
18980 
18981   /* Assume all of the expressions are constant.  */
18982   *non_constant_p = false;
18983 
18984   /* Parse the rest of the list.  */
18985   while (true)
18986     {
18987       cp_token *token;
18988       tree designator;
18989       tree initializer;
18990       bool clause_non_constant_p;
18991 
18992       /* If the next token is an identifier and the following one is a
18993 	 colon, we are looking at the GNU designated-initializer
18994 	 syntax.  */
18995       if (cp_parser_allow_gnu_extensions_p (parser)
18996 	  && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
18997 	  && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
18998 	{
18999 	  /* Warn the user that they are using an extension.  */
19000 	  pedwarn (input_location, OPT_Wpedantic,
19001 		   "ISO C++ does not allow designated initializers");
19002 	  /* Consume the identifier.  */
19003 	  designator = cp_lexer_consume_token (parser->lexer)->u.value;
19004 	  /* Consume the `:'.  */
19005 	  cp_lexer_consume_token (parser->lexer);
19006 	}
19007       /* Also handle the C99 syntax, '. id ='.  */
19008       else if (cp_parser_allow_gnu_extensions_p (parser)
19009 	       && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19010 	       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19011 	       && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19012 	{
19013 	  /* Warn the user that they are using an extension.  */
19014 	  pedwarn (input_location, OPT_Wpedantic,
19015 		   "ISO C++ does not allow C99 designated initializers");
19016 	  /* Consume the `.'.  */
19017 	  cp_lexer_consume_token (parser->lexer);
19018 	  /* Consume the identifier.  */
19019 	  designator = cp_lexer_consume_token (parser->lexer)->u.value;
19020 	  /* Consume the `='.  */
19021 	  cp_lexer_consume_token (parser->lexer);
19022 	}
19023       /* Also handle C99 array designators, '[ const ] ='.  */
19024       else if (cp_parser_allow_gnu_extensions_p (parser)
19025 	       && !c_dialect_objc ()
19026 	       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19027 	{
19028 	  /* In C++11, [ could start a lambda-introducer.  */
19029 	  bool non_const = false;
19030 
19031 	  cp_parser_parse_tentatively (parser);
19032 	  cp_lexer_consume_token (parser->lexer);
19033 	  designator = cp_parser_constant_expression (parser, true, &non_const);
19034 	  cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19035 	  cp_parser_require (parser, CPP_EQ, RT_EQ);
19036 	  if (!cp_parser_parse_definitely (parser))
19037 	    designator = NULL_TREE;
19038 	  else if (non_const)
19039 	    require_potential_rvalue_constant_expression (designator);
19040 	}
19041       else
19042 	designator = NULL_TREE;
19043 
19044       /* Parse the initializer.  */
19045       initializer = cp_parser_initializer_clause (parser,
19046 						  &clause_non_constant_p);
19047       /* If any clause is non-constant, so is the entire initializer.  */
19048       if (clause_non_constant_p)
19049 	*non_constant_p = true;
19050 
19051       /* If we have an ellipsis, this is an initializer pack
19052 	 expansion.  */
19053       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19054         {
19055           /* Consume the `...'.  */
19056           cp_lexer_consume_token (parser->lexer);
19057 
19058           /* Turn the initializer into an initializer expansion.  */
19059           initializer = make_pack_expansion (initializer);
19060         }
19061 
19062       /* Add it to the vector.  */
19063       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19064 
19065       /* If the next token is not a comma, we have reached the end of
19066 	 the list.  */
19067       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19068 	break;
19069 
19070       /* Peek at the next token.  */
19071       token = cp_lexer_peek_nth_token (parser->lexer, 2);
19072       /* If the next token is a `}', then we're still done.  An
19073 	 initializer-clause can have a trailing `,' after the
19074 	 initializer-list and before the closing `}'.  */
19075       if (token->type == CPP_CLOSE_BRACE)
19076 	break;
19077 
19078       /* Consume the `,' token.  */
19079       cp_lexer_consume_token (parser->lexer);
19080     }
19081 
19082   return v;
19083 }
19084 
19085 /* Classes [gram.class] */
19086 
19087 /* Parse a class-name.
19088 
19089    class-name:
19090      identifier
19091      template-id
19092 
19093    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19094    to indicate that names looked up in dependent types should be
19095    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
19096    keyword has been used to indicate that the name that appears next
19097    is a template.  TAG_TYPE indicates the explicit tag given before
19098    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
19099    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
19100    is the class being defined in a class-head.
19101 
19102    Returns the TYPE_DECL representing the class.  */
19103 
19104 static tree
cp_parser_class_name(cp_parser * parser,bool typename_keyword_p,bool template_keyword_p,enum tag_types tag_type,bool check_dependency_p,bool class_head_p,bool is_declaration)19105 cp_parser_class_name (cp_parser *parser,
19106 		      bool typename_keyword_p,
19107 		      bool template_keyword_p,
19108 		      enum tag_types tag_type,
19109 		      bool check_dependency_p,
19110 		      bool class_head_p,
19111 		      bool is_declaration)
19112 {
19113   tree decl;
19114   tree scope;
19115   bool typename_p;
19116   cp_token *token;
19117   tree identifier = NULL_TREE;
19118 
19119   /* All class-names start with an identifier.  */
19120   token = cp_lexer_peek_token (parser->lexer);
19121   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19122     {
19123       cp_parser_error (parser, "expected class-name");
19124       return error_mark_node;
19125     }
19126 
19127   /* PARSER->SCOPE can be cleared when parsing the template-arguments
19128      to a template-id, so we save it here.  */
19129   scope = parser->scope;
19130   if (scope == error_mark_node)
19131     return error_mark_node;
19132 
19133   /* Any name names a type if we're following the `typename' keyword
19134      in a qualified name where the enclosing scope is type-dependent.  */
19135   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19136 		&& dependent_type_p (scope));
19137   /* Handle the common case (an identifier, but not a template-id)
19138      efficiently.  */
19139   if (token->type == CPP_NAME
19140       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19141     {
19142       cp_token *identifier_token;
19143       bool ambiguous_p;
19144 
19145       /* Look for the identifier.  */
19146       identifier_token = cp_lexer_peek_token (parser->lexer);
19147       ambiguous_p = identifier_token->ambiguous_p;
19148       identifier = cp_parser_identifier (parser);
19149       /* If the next token isn't an identifier, we are certainly not
19150 	 looking at a class-name.  */
19151       if (identifier == error_mark_node)
19152 	decl = error_mark_node;
19153       /* If we know this is a type-name, there's no need to look it
19154 	 up.  */
19155       else if (typename_p)
19156 	decl = identifier;
19157       else
19158 	{
19159 	  tree ambiguous_decls;
19160 	  /* If we already know that this lookup is ambiguous, then
19161 	     we've already issued an error message; there's no reason
19162 	     to check again.  */
19163 	  if (ambiguous_p)
19164 	    {
19165 	      cp_parser_simulate_error (parser);
19166 	      return error_mark_node;
19167 	    }
19168 	  /* If the next token is a `::', then the name must be a type
19169 	     name.
19170 
19171 	     [basic.lookup.qual]
19172 
19173 	     During the lookup for a name preceding the :: scope
19174 	     resolution operator, object, function, and enumerator
19175 	     names are ignored.  */
19176 	  if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19177 	    tag_type = typename_type;
19178 	  /* Look up the name.  */
19179 	  decl = cp_parser_lookup_name (parser, identifier,
19180 					tag_type,
19181 					/*is_template=*/false,
19182 					/*is_namespace=*/false,
19183 					check_dependency_p,
19184 					&ambiguous_decls,
19185 					identifier_token->location);
19186 	  if (ambiguous_decls)
19187 	    {
19188 	      if (cp_parser_parsing_tentatively (parser))
19189 		cp_parser_simulate_error (parser);
19190 	      return error_mark_node;
19191 	    }
19192 	}
19193     }
19194   else
19195     {
19196       /* Try a template-id.  */
19197       decl = cp_parser_template_id (parser, template_keyword_p,
19198 				    check_dependency_p,
19199 				    tag_type,
19200 				    is_declaration);
19201       if (decl == error_mark_node)
19202 	return error_mark_node;
19203     }
19204 
19205   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19206 
19207   /* If this is a typename, create a TYPENAME_TYPE.  */
19208   if (typename_p && decl != error_mark_node)
19209     {
19210       decl = make_typename_type (scope, decl, typename_type,
19211 				 /*complain=*/tf_error);
19212       if (decl != error_mark_node)
19213 	decl = TYPE_NAME (decl);
19214     }
19215 
19216   decl = strip_using_decl (decl);
19217 
19218   /* Check to see that it is really the name of a class.  */
19219   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19220       && identifier_p (TREE_OPERAND (decl, 0))
19221       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19222     /* Situations like this:
19223 
19224 	 template <typename T> struct A {
19225 	   typename T::template X<int>::I i;
19226 	 };
19227 
19228        are problematic.  Is `T::template X<int>' a class-name?  The
19229        standard does not seem to be definitive, but there is no other
19230        valid interpretation of the following `::'.  Therefore, those
19231        names are considered class-names.  */
19232     {
19233       decl = make_typename_type (scope, decl, tag_type, tf_error);
19234       if (decl != error_mark_node)
19235 	decl = TYPE_NAME (decl);
19236     }
19237   else if (TREE_CODE (decl) != TYPE_DECL
19238 	   || TREE_TYPE (decl) == error_mark_node
19239 	   || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19240 	   /* In Objective-C 2.0, a classname followed by '.' starts a
19241 	      dot-syntax expression, and it's not a type-name.  */
19242 	   || (c_dialect_objc ()
19243 	       && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19244 	       && objc_is_class_name (decl)))
19245     decl = error_mark_node;
19246 
19247   if (decl == error_mark_node)
19248     cp_parser_error (parser, "expected class-name");
19249   else if (identifier && !parser->scope)
19250     maybe_note_name_used_in_class (identifier, decl);
19251 
19252   return decl;
19253 }
19254 
19255 /* Parse a class-specifier.
19256 
19257    class-specifier:
19258      class-head { member-specification [opt] }
19259 
19260    Returns the TREE_TYPE representing the class.  */
19261 
19262 static tree
cp_parser_class_specifier_1(cp_parser * parser)19263 cp_parser_class_specifier_1 (cp_parser* parser)
19264 {
19265   tree type;
19266   tree attributes = NULL_TREE;
19267   bool nested_name_specifier_p;
19268   unsigned saved_num_template_parameter_lists;
19269   bool saved_in_function_body;
19270   unsigned char in_statement;
19271   bool in_switch_statement_p;
19272   bool saved_in_unbraced_linkage_specification_p;
19273   tree old_scope = NULL_TREE;
19274   tree scope = NULL_TREE;
19275   cp_token *closing_brace;
19276 
19277   push_deferring_access_checks (dk_no_deferred);
19278 
19279   /* Parse the class-head.  */
19280   type = cp_parser_class_head (parser,
19281 			       &nested_name_specifier_p);
19282   /* If the class-head was a semantic disaster, skip the entire body
19283      of the class.  */
19284   if (!type)
19285     {
19286       cp_parser_skip_to_end_of_block_or_statement (parser);
19287       pop_deferring_access_checks ();
19288       return error_mark_node;
19289     }
19290 
19291   /* Look for the `{'.  */
19292   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19293     {
19294       pop_deferring_access_checks ();
19295       return error_mark_node;
19296     }
19297 
19298   cp_ensure_no_omp_declare_simd (parser);
19299 
19300   /* Issue an error message if type-definitions are forbidden here.  */
19301   cp_parser_check_type_definition (parser);
19302   /* Remember that we are defining one more class.  */
19303   ++parser->num_classes_being_defined;
19304   /* Inside the class, surrounding template-parameter-lists do not
19305      apply.  */
19306   saved_num_template_parameter_lists
19307     = parser->num_template_parameter_lists;
19308   parser->num_template_parameter_lists = 0;
19309   /* We are not in a function body.  */
19310   saved_in_function_body = parser->in_function_body;
19311   parser->in_function_body = false;
19312   /* Or in a loop.  */
19313   in_statement = parser->in_statement;
19314   parser->in_statement = 0;
19315   /* Or in a switch.  */
19316   in_switch_statement_p = parser->in_switch_statement_p;
19317   parser->in_switch_statement_p = false;
19318   /* We are not immediately inside an extern "lang" block.  */
19319   saved_in_unbraced_linkage_specification_p
19320     = parser->in_unbraced_linkage_specification_p;
19321   parser->in_unbraced_linkage_specification_p = false;
19322 
19323   /* Start the class.  */
19324   if (nested_name_specifier_p)
19325     {
19326       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19327       old_scope = push_inner_scope (scope);
19328     }
19329   type = begin_class_definition (type);
19330 
19331   if (type == error_mark_node)
19332     /* If the type is erroneous, skip the entire body of the class.  */
19333     cp_parser_skip_to_closing_brace (parser);
19334   else
19335     /* Parse the member-specification.  */
19336     cp_parser_member_specification_opt (parser);
19337 
19338   /* Look for the trailing `}'.  */
19339   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19340   /* Look for trailing attributes to apply to this class.  */
19341   if (cp_parser_allow_gnu_extensions_p (parser))
19342     attributes = cp_parser_gnu_attributes_opt (parser);
19343   if (type != error_mark_node)
19344     type = finish_struct (type, attributes);
19345   if (nested_name_specifier_p)
19346     pop_inner_scope (old_scope, scope);
19347 
19348   /* We've finished a type definition.  Check for the common syntax
19349      error of forgetting a semicolon after the definition.  We need to
19350      be careful, as we can't just check for not-a-semicolon and be done
19351      with it; the user might have typed:
19352 
19353      class X { } c = ...;
19354      class X { } *p = ...;
19355 
19356      and so forth.  Instead, enumerate all the possible tokens that
19357      might follow this production; if we don't see one of them, then
19358      complain and silently insert the semicolon.  */
19359   {
19360     cp_token *token = cp_lexer_peek_token (parser->lexer);
19361     bool want_semicolon = true;
19362 
19363     if (cp_next_tokens_can_be_std_attribute_p (parser))
19364       /* Don't try to parse c++11 attributes here.  As per the
19365 	 grammar, that should be a task for
19366 	 cp_parser_decl_specifier_seq.  */
19367       want_semicolon = false;
19368 
19369     switch (token->type)
19370       {
19371       case CPP_NAME:
19372       case CPP_SEMICOLON:
19373       case CPP_MULT:
19374       case CPP_AND:
19375       case CPP_OPEN_PAREN:
19376       case CPP_CLOSE_PAREN:
19377       case CPP_COMMA:
19378         want_semicolon = false;
19379         break;
19380 
19381         /* While it's legal for type qualifiers and storage class
19382            specifiers to follow type definitions in the grammar, only
19383            compiler testsuites contain code like that.  Assume that if
19384            we see such code, then what we're really seeing is a case
19385            like:
19386 
19387            class X { }
19388            const <type> var = ...;
19389 
19390            or
19391 
19392            class Y { }
19393            static <type> func (...) ...
19394 
19395            i.e. the qualifier or specifier applies to the next
19396            declaration.  To do so, however, we need to look ahead one
19397            more token to see if *that* token is a type specifier.
19398 
19399 	   This code could be improved to handle:
19400 
19401 	   class Z { }
19402 	   static const <type> var = ...;  */
19403       case CPP_KEYWORD:
19404 	if (keyword_is_decl_specifier (token->keyword))
19405 	  {
19406 	    cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19407 
19408 	    /* Handling user-defined types here would be nice, but very
19409 	       tricky.  */
19410 	    want_semicolon
19411 	      = (lookahead->type == CPP_KEYWORD
19412 		 && keyword_begins_type_specifier (lookahead->keyword));
19413 	  }
19414 	break;
19415       default:
19416 	break;
19417       }
19418 
19419     /* If we don't have a type, then something is very wrong and we
19420        shouldn't try to do anything clever.  Likewise for not seeing the
19421        closing brace.  */
19422     if (closing_brace && TYPE_P (type) && want_semicolon)
19423       {
19424 	cp_token_position prev
19425 	  = cp_lexer_previous_token_position (parser->lexer);
19426 	cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19427 	location_t loc = prev_token->location;
19428 
19429 	if (CLASSTYPE_DECLARED_CLASS (type))
19430 	  error_at (loc, "expected %<;%> after class definition");
19431 	else if (TREE_CODE (type) == RECORD_TYPE)
19432 	  error_at (loc, "expected %<;%> after struct definition");
19433 	else if (TREE_CODE (type) == UNION_TYPE)
19434 	  error_at (loc, "expected %<;%> after union definition");
19435 	else
19436 	  gcc_unreachable ();
19437 
19438 	/* Unget one token and smash it to look as though we encountered
19439 	   a semicolon in the input stream.  */
19440 	cp_lexer_set_token_position (parser->lexer, prev);
19441 	token = cp_lexer_peek_token (parser->lexer);
19442 	token->type = CPP_SEMICOLON;
19443 	token->keyword = RID_MAX;
19444       }
19445   }
19446 
19447   /* If this class is not itself within the scope of another class,
19448      then we need to parse the bodies of all of the queued function
19449      definitions.  Note that the queued functions defined in a class
19450      are not always processed immediately following the
19451      class-specifier for that class.  Consider:
19452 
19453        struct A {
19454 	 struct B { void f() { sizeof (A); } };
19455        };
19456 
19457      If `f' were processed before the processing of `A' were
19458      completed, there would be no way to compute the size of `A'.
19459      Note that the nesting we are interested in here is lexical --
19460      not the semantic nesting given by TYPE_CONTEXT.  In particular,
19461      for:
19462 
19463        struct A { struct B; };
19464        struct A::B { void f() { } };
19465 
19466      there is no need to delay the parsing of `A::B::f'.  */
19467   if (--parser->num_classes_being_defined == 0)
19468     {
19469       tree decl;
19470       tree class_type = NULL_TREE;
19471       tree pushed_scope = NULL_TREE;
19472       unsigned ix;
19473       cp_default_arg_entry *e;
19474       tree save_ccp, save_ccr;
19475 
19476       /* In a first pass, parse default arguments to the functions.
19477 	 Then, in a second pass, parse the bodies of the functions.
19478 	 This two-phased approach handles cases like:
19479 
19480 	    struct S {
19481 	      void f() { g(); }
19482 	      void g(int i = 3);
19483 	    };
19484 
19485 	 */
19486       FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19487 	{
19488 	  decl = e->decl;
19489 	  /* If there are default arguments that have not yet been processed,
19490 	     take care of them now.  */
19491 	  if (class_type != e->class_type)
19492 	    {
19493 	      if (pushed_scope)
19494 		pop_scope (pushed_scope);
19495 	      class_type = e->class_type;
19496 	      pushed_scope = push_scope (class_type);
19497 	    }
19498 	  /* Make sure that any template parameters are in scope.  */
19499 	  maybe_begin_member_template_processing (decl);
19500 	  /* Parse the default argument expressions.  */
19501 	  cp_parser_late_parsing_default_args (parser, decl);
19502 	  /* Remove any template parameters from the symbol table.  */
19503 	  maybe_end_member_template_processing ();
19504 	}
19505       vec_safe_truncate (unparsed_funs_with_default_args, 0);
19506       /* Now parse any NSDMIs.  */
19507       save_ccp = current_class_ptr;
19508       save_ccr = current_class_ref;
19509       FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19510 	{
19511 	  if (class_type != DECL_CONTEXT (decl))
19512 	    {
19513 	      if (pushed_scope)
19514 		pop_scope (pushed_scope);
19515 	      class_type = DECL_CONTEXT (decl);
19516 	      pushed_scope = push_scope (class_type);
19517 	    }
19518 	  inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19519 	  cp_parser_late_parsing_nsdmi (parser, decl);
19520 	}
19521       vec_safe_truncate (unparsed_nsdmis, 0);
19522       current_class_ptr = save_ccp;
19523       current_class_ref = save_ccr;
19524       if (pushed_scope)
19525 	pop_scope (pushed_scope);
19526       /* Now parse the body of the functions.  */
19527       if (flag_openmp)
19528 	{
19529 	  /* OpenMP UDRs need to be parsed before all other functions.  */
19530 	  FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19531 	    if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19532 	      cp_parser_late_parsing_for_member (parser, decl);
19533 	  FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19534 	    if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19535 	      cp_parser_late_parsing_for_member (parser, decl);
19536 	}
19537       else
19538 	FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19539 	  cp_parser_late_parsing_for_member (parser, decl);
19540       vec_safe_truncate (unparsed_funs_with_definitions, 0);
19541     }
19542 
19543   /* Put back any saved access checks.  */
19544   pop_deferring_access_checks ();
19545 
19546   /* Restore saved state.  */
19547   parser->in_switch_statement_p = in_switch_statement_p;
19548   parser->in_statement = in_statement;
19549   parser->in_function_body = saved_in_function_body;
19550   parser->num_template_parameter_lists
19551     = saved_num_template_parameter_lists;
19552   parser->in_unbraced_linkage_specification_p
19553     = saved_in_unbraced_linkage_specification_p;
19554 
19555   return type;
19556 }
19557 
19558 static tree
cp_parser_class_specifier(cp_parser * parser)19559 cp_parser_class_specifier (cp_parser* parser)
19560 {
19561   tree ret;
19562   timevar_push (TV_PARSE_STRUCT);
19563   ret = cp_parser_class_specifier_1 (parser);
19564   timevar_pop (TV_PARSE_STRUCT);
19565   return ret;
19566 }
19567 
19568 /* Parse a class-head.
19569 
19570    class-head:
19571      class-key identifier [opt] base-clause [opt]
19572      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19573      class-key nested-name-specifier [opt] template-id
19574        base-clause [opt]
19575 
19576    class-virt-specifier:
19577      final
19578 
19579    GNU Extensions:
19580      class-key attributes identifier [opt] base-clause [opt]
19581      class-key attributes nested-name-specifier identifier base-clause [opt]
19582      class-key attributes nested-name-specifier [opt] template-id
19583        base-clause [opt]
19584 
19585    Upon return BASES is initialized to the list of base classes (or
19586    NULL, if there are none) in the same form returned by
19587    cp_parser_base_clause.
19588 
19589    Returns the TYPE of the indicated class.  Sets
19590    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19591    involving a nested-name-specifier was used, and FALSE otherwise.
19592 
19593    Returns error_mark_node if this is not a class-head.
19594 
19595    Returns NULL_TREE if the class-head is syntactically valid, but
19596    semantically invalid in a way that means we should skip the entire
19597    body of the class.  */
19598 
19599 static tree
cp_parser_class_head(cp_parser * parser,bool * nested_name_specifier_p)19600 cp_parser_class_head (cp_parser* parser,
19601 		      bool* nested_name_specifier_p)
19602 {
19603   tree nested_name_specifier;
19604   enum tag_types class_key;
19605   tree id = NULL_TREE;
19606   tree type = NULL_TREE;
19607   tree attributes;
19608   tree bases;
19609   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19610   bool template_id_p = false;
19611   bool qualified_p = false;
19612   bool invalid_nested_name_p = false;
19613   bool invalid_explicit_specialization_p = false;
19614   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19615   tree pushed_scope = NULL_TREE;
19616   unsigned num_templates;
19617   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
19618   /* Assume no nested-name-specifier will be present.  */
19619   *nested_name_specifier_p = false;
19620   /* Assume no template parameter lists will be used in defining the
19621      type.  */
19622   num_templates = 0;
19623   parser->colon_corrects_to_scope_p = false;
19624 
19625   /* Look for the class-key.  */
19626   class_key = cp_parser_class_key (parser);
19627   if (class_key == none_type)
19628     return error_mark_node;
19629 
19630   /* Parse the attributes.  */
19631   attributes = cp_parser_attributes_opt (parser);
19632 
19633   /* If the next token is `::', that is invalid -- but sometimes
19634      people do try to write:
19635 
19636        struct ::S {};
19637 
19638      Handle this gracefully by accepting the extra qualifier, and then
19639      issuing an error about it later if this really is a
19640      class-head.  If it turns out just to be an elaborated type
19641      specifier, remain silent.  */
19642   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
19643     qualified_p = true;
19644 
19645   push_deferring_access_checks (dk_no_check);
19646 
19647   /* Determine the name of the class.  Begin by looking for an
19648      optional nested-name-specifier.  */
19649   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
19650   nested_name_specifier
19651     = cp_parser_nested_name_specifier_opt (parser,
19652 					   /*typename_keyword_p=*/false,
19653 					   /*check_dependency_p=*/false,
19654 					   /*type_p=*/true,
19655 					   /*is_declaration=*/false);
19656   /* If there was a nested-name-specifier, then there *must* be an
19657      identifier.  */
19658   if (nested_name_specifier)
19659     {
19660       type_start_token = cp_lexer_peek_token (parser->lexer);
19661       /* Although the grammar says `identifier', it really means
19662 	 `class-name' or `template-name'.  You are only allowed to
19663 	 define a class that has already been declared with this
19664 	 syntax.
19665 
19666 	 The proposed resolution for Core Issue 180 says that wherever
19667 	 you see `class T::X' you should treat `X' as a type-name.
19668 
19669 	 It is OK to define an inaccessible class; for example:
19670 
19671 	   class A { class B; };
19672 	   class A::B {};
19673 
19674 	 We do not know if we will see a class-name, or a
19675 	 template-name.  We look for a class-name first, in case the
19676 	 class-name is a template-id; if we looked for the
19677 	 template-name first we would stop after the template-name.  */
19678       cp_parser_parse_tentatively (parser);
19679       type = cp_parser_class_name (parser,
19680 				   /*typename_keyword_p=*/false,
19681 				   /*template_keyword_p=*/false,
19682 				   class_type,
19683 				   /*check_dependency_p=*/false,
19684 				   /*class_head_p=*/true,
19685 				   /*is_declaration=*/false);
19686       /* If that didn't work, ignore the nested-name-specifier.  */
19687       if (!cp_parser_parse_definitely (parser))
19688 	{
19689 	  invalid_nested_name_p = true;
19690 	  type_start_token = cp_lexer_peek_token (parser->lexer);
19691 	  id = cp_parser_identifier (parser);
19692 	  if (id == error_mark_node)
19693 	    id = NULL_TREE;
19694 	}
19695       /* If we could not find a corresponding TYPE, treat this
19696 	 declaration like an unqualified declaration.  */
19697       if (type == error_mark_node)
19698 	nested_name_specifier = NULL_TREE;
19699       /* Otherwise, count the number of templates used in TYPE and its
19700 	 containing scopes.  */
19701       else
19702 	{
19703 	  tree scope;
19704 
19705 	  for (scope = TREE_TYPE (type);
19706 	       scope && TREE_CODE (scope) != NAMESPACE_DECL;
19707 	       scope = get_containing_scope (scope))
19708 	    if (TYPE_P (scope)
19709 		&& CLASS_TYPE_P (scope)
19710 		&& CLASSTYPE_TEMPLATE_INFO (scope)
19711 		&& PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
19712 		&& (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
19713 		    || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
19714 	      ++num_templates;
19715 	}
19716     }
19717   /* Otherwise, the identifier is optional.  */
19718   else
19719     {
19720       /* We don't know whether what comes next is a template-id,
19721 	 an identifier, or nothing at all.  */
19722       cp_parser_parse_tentatively (parser);
19723       /* Check for a template-id.  */
19724       type_start_token = cp_lexer_peek_token (parser->lexer);
19725       id = cp_parser_template_id (parser,
19726 				  /*template_keyword_p=*/false,
19727 				  /*check_dependency_p=*/true,
19728 				  class_key,
19729 				  /*is_declaration=*/true);
19730       /* If that didn't work, it could still be an identifier.  */
19731       if (!cp_parser_parse_definitely (parser))
19732 	{
19733 	  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19734 	    {
19735 	      type_start_token = cp_lexer_peek_token (parser->lexer);
19736 	      id = cp_parser_identifier (parser);
19737 	    }
19738 	  else
19739 	    id = NULL_TREE;
19740 	}
19741       else
19742 	{
19743 	  template_id_p = true;
19744 	  ++num_templates;
19745 	}
19746     }
19747 
19748   pop_deferring_access_checks ();
19749 
19750   if (id)
19751     {
19752       cp_parser_check_for_invalid_template_id (parser, id,
19753 					       class_key,
19754                                                type_start_token->location);
19755     }
19756   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19757 
19758   /* If it's not a `:' or a `{' then we can't really be looking at a
19759      class-head, since a class-head only appears as part of a
19760      class-specifier.  We have to detect this situation before calling
19761      xref_tag, since that has irreversible side-effects.  */
19762   if (!cp_parser_next_token_starts_class_definition_p (parser))
19763     {
19764       cp_parser_error (parser, "expected %<{%> or %<:%>");
19765       type = error_mark_node;
19766       goto out;
19767     }
19768 
19769   /* At this point, we're going ahead with the class-specifier, even
19770      if some other problem occurs.  */
19771   cp_parser_commit_to_tentative_parse (parser);
19772   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
19773     {
19774       cp_parser_error (parser,
19775                        "cannot specify %<override%> for a class");
19776       type = error_mark_node;
19777       goto out;
19778     }
19779   /* Issue the error about the overly-qualified name now.  */
19780   if (qualified_p)
19781     {
19782       cp_parser_error (parser,
19783 		       "global qualification of class name is invalid");
19784       type = error_mark_node;
19785       goto out;
19786     }
19787   else if (invalid_nested_name_p)
19788     {
19789       cp_parser_error (parser,
19790 		       "qualified name does not name a class");
19791       type = error_mark_node;
19792       goto out;
19793     }
19794   else if (nested_name_specifier)
19795     {
19796       tree scope;
19797 
19798       /* Reject typedef-names in class heads.  */
19799       if (!DECL_IMPLICIT_TYPEDEF_P (type))
19800 	{
19801 	  error_at (type_start_token->location,
19802 		    "invalid class name in declaration of %qD",
19803 		    type);
19804 	  type = NULL_TREE;
19805 	  goto done;
19806 	}
19807 
19808       /* Figure out in what scope the declaration is being placed.  */
19809       scope = current_scope ();
19810       /* If that scope does not contain the scope in which the
19811 	 class was originally declared, the program is invalid.  */
19812       if (scope && !is_ancestor (scope, nested_name_specifier))
19813 	{
19814 	  if (at_namespace_scope_p ())
19815 	    error_at (type_start_token->location,
19816 		      "declaration of %qD in namespace %qD which does not "
19817 		      "enclose %qD",
19818 		      type, scope, nested_name_specifier);
19819 	  else
19820 	    error_at (type_start_token->location,
19821 		      "declaration of %qD in %qD which does not enclose %qD",
19822 		      type, scope, nested_name_specifier);
19823 	  type = NULL_TREE;
19824 	  goto done;
19825 	}
19826       /* [dcl.meaning]
19827 
19828 	 A declarator-id shall not be qualified except for the
19829 	 definition of a ... nested class outside of its class
19830 	 ... [or] the definition or explicit instantiation of a
19831 	 class member of a namespace outside of its namespace.  */
19832       if (scope == nested_name_specifier)
19833 	{
19834 	  permerror (nested_name_specifier_token_start->location,
19835 		     "extra qualification not allowed");
19836 	  nested_name_specifier = NULL_TREE;
19837 	  num_templates = 0;
19838 	}
19839     }
19840   /* An explicit-specialization must be preceded by "template <>".  If
19841      it is not, try to recover gracefully.  */
19842   if (at_namespace_scope_p ()
19843       && parser->num_template_parameter_lists == 0
19844       && template_id_p)
19845     {
19846       error_at (type_start_token->location,
19847 		"an explicit specialization must be preceded by %<template <>%>");
19848       invalid_explicit_specialization_p = true;
19849       /* Take the same action that would have been taken by
19850 	 cp_parser_explicit_specialization.  */
19851       ++parser->num_template_parameter_lists;
19852       begin_specialization ();
19853     }
19854   /* There must be no "return" statements between this point and the
19855      end of this function; set "type "to the correct return value and
19856      use "goto done;" to return.  */
19857   /* Make sure that the right number of template parameters were
19858      present.  */
19859   if (!cp_parser_check_template_parameters (parser, num_templates,
19860 					    type_start_token->location,
19861 					    /*declarator=*/NULL))
19862     {
19863       /* If something went wrong, there is no point in even trying to
19864 	 process the class-definition.  */
19865       type = NULL_TREE;
19866       goto done;
19867     }
19868 
19869   /* Look up the type.  */
19870   if (template_id_p)
19871     {
19872       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
19873 	  && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
19874 	      || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
19875 	{
19876 	  error_at (type_start_token->location,
19877 		    "function template %qD redeclared as a class template", id);
19878 	  type = error_mark_node;
19879 	}
19880       else
19881 	{
19882 	  type = TREE_TYPE (id);
19883 	  type = maybe_process_partial_specialization (type);
19884 	}
19885       if (nested_name_specifier)
19886 	pushed_scope = push_scope (nested_name_specifier);
19887     }
19888   else if (nested_name_specifier)
19889     {
19890       tree class_type;
19891 
19892       /* Given:
19893 
19894 	    template <typename T> struct S { struct T };
19895 	    template <typename T> struct S<T>::T { };
19896 
19897 	 we will get a TYPENAME_TYPE when processing the definition of
19898 	 `S::T'.  We need to resolve it to the actual type before we
19899 	 try to define it.  */
19900       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
19901 	{
19902 	  class_type = resolve_typename_type (TREE_TYPE (type),
19903 					      /*only_current_p=*/false);
19904 	  if (TREE_CODE (class_type) != TYPENAME_TYPE)
19905 	    type = TYPE_NAME (class_type);
19906 	  else
19907 	    {
19908 	      cp_parser_error (parser, "could not resolve typename type");
19909 	      type = error_mark_node;
19910 	    }
19911 	}
19912 
19913       if (maybe_process_partial_specialization (TREE_TYPE (type))
19914 	  == error_mark_node)
19915 	{
19916 	  type = NULL_TREE;
19917 	  goto done;
19918 	}
19919 
19920       class_type = current_class_type;
19921       /* Enter the scope indicated by the nested-name-specifier.  */
19922       pushed_scope = push_scope (nested_name_specifier);
19923       /* Get the canonical version of this type.  */
19924       type = TYPE_MAIN_DECL (TREE_TYPE (type));
19925       /* Call push_template_decl if it seems like we should be defining a
19926 	 template either from the template headers or the type we're
19927 	 defining, so that we diagnose both extra and missing headers.  */
19928       if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
19929 	   || (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type))
19930 	       && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE
19931 				      (TREE_TYPE (type)))))
19932 	  && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
19933 	{
19934 	  type = push_template_decl (type);
19935 	  if (type == error_mark_node)
19936 	    {
19937 	      type = NULL_TREE;
19938 	      goto done;
19939 	    }
19940 	}
19941 
19942       type = TREE_TYPE (type);
19943       *nested_name_specifier_p = true;
19944     }
19945   else      /* The name is not a nested name.  */
19946     {
19947       /* If the class was unnamed, create a dummy name.  */
19948       if (!id)
19949 	id = make_anon_name ();
19950       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
19951 		       parser->num_template_parameter_lists);
19952     }
19953 
19954   /* Indicate whether this class was declared as a `class' or as a
19955      `struct'.  */
19956   if (TREE_CODE (type) == RECORD_TYPE)
19957     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
19958   cp_parser_check_class_key (class_key, type);
19959 
19960   /* If this type was already complete, and we see another definition,
19961      that's an error.  */
19962   if (type != error_mark_node && COMPLETE_TYPE_P (type))
19963     {
19964       error_at (type_start_token->location, "redefinition of %q#T",
19965 		type);
19966       error_at (type_start_token->location, "previous definition of %q+#T",
19967 		type);
19968       type = NULL_TREE;
19969       goto done;
19970     }
19971   else if (type == error_mark_node)
19972     type = NULL_TREE;
19973 
19974   if (type)
19975     {
19976       /* Apply attributes now, before any use of the class as a template
19977 	 argument in its base list.  */
19978       cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
19979       fixup_attribute_variants (type);
19980     }
19981 
19982   /* We will have entered the scope containing the class; the names of
19983      base classes should be looked up in that context.  For example:
19984 
19985        struct A { struct B {}; struct C; };
19986        struct A::C : B {};
19987 
19988      is valid.  */
19989 
19990   /* Get the list of base-classes, if there is one.  */
19991   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19992     {
19993       /* PR59482: enter the class scope so that base-specifiers are looked
19994 	 up correctly.  */
19995       if (type)
19996 	pushclass (type);
19997       bases = cp_parser_base_clause (parser);
19998       /* PR59482: get out of the previously pushed class scope so that the
19999 	 subsequent pops pop the right thing.  */
20000       if (type)
20001 	popclass ();
20002     }
20003   else
20004     bases = NULL_TREE;
20005 
20006   /* If we're really defining a class, process the base classes.
20007      If they're invalid, fail.  */
20008   if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20009       && !xref_basetypes (type, bases))
20010     type = NULL_TREE;
20011 
20012  done:
20013   /* Leave the scope given by the nested-name-specifier.  We will
20014      enter the class scope itself while processing the members.  */
20015   if (pushed_scope)
20016     pop_scope (pushed_scope);
20017 
20018   if (invalid_explicit_specialization_p)
20019     {
20020       end_specialization ();
20021       --parser->num_template_parameter_lists;
20022     }
20023 
20024   if (type)
20025     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20026   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20027     CLASSTYPE_FINAL (type) = 1;
20028  out:
20029   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20030   return type;
20031 }
20032 
20033 /* Parse a class-key.
20034 
20035    class-key:
20036      class
20037      struct
20038      union
20039 
20040    Returns the kind of class-key specified, or none_type to indicate
20041    error.  */
20042 
20043 static enum tag_types
cp_parser_class_key(cp_parser * parser)20044 cp_parser_class_key (cp_parser* parser)
20045 {
20046   cp_token *token;
20047   enum tag_types tag_type;
20048 
20049   /* Look for the class-key.  */
20050   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20051   if (!token)
20052     return none_type;
20053 
20054   /* Check to see if the TOKEN is a class-key.  */
20055   tag_type = cp_parser_token_is_class_key (token);
20056   if (!tag_type)
20057     cp_parser_error (parser, "expected class-key");
20058   return tag_type;
20059 }
20060 
20061 /* Parse an (optional) member-specification.
20062 
20063    member-specification:
20064      member-declaration member-specification [opt]
20065      access-specifier : member-specification [opt]  */
20066 
20067 static void
cp_parser_member_specification_opt(cp_parser * parser)20068 cp_parser_member_specification_opt (cp_parser* parser)
20069 {
20070   while (true)
20071     {
20072       cp_token *token;
20073       enum rid keyword;
20074 
20075       /* Peek at the next token.  */
20076       token = cp_lexer_peek_token (parser->lexer);
20077       /* If it's a `}', or EOF then we've seen all the members.  */
20078       if (token->type == CPP_CLOSE_BRACE
20079 	  || token->type == CPP_EOF
20080 	  || token->type == CPP_PRAGMA_EOL)
20081 	break;
20082 
20083       /* See if this token is a keyword.  */
20084       keyword = token->keyword;
20085       switch (keyword)
20086 	{
20087 	case RID_PUBLIC:
20088 	case RID_PROTECTED:
20089 	case RID_PRIVATE:
20090 	  /* Consume the access-specifier.  */
20091 	  cp_lexer_consume_token (parser->lexer);
20092 	  /* Remember which access-specifier is active.  */
20093 	  current_access_specifier = token->u.value;
20094 	  /* Look for the `:'.  */
20095 	  cp_parser_require (parser, CPP_COLON, RT_COLON);
20096 	  break;
20097 
20098 	default:
20099 	  /* Accept #pragmas at class scope.  */
20100 	  if (token->type == CPP_PRAGMA)
20101 	    {
20102 	      cp_parser_pragma (parser, pragma_member);
20103 	      break;
20104 	    }
20105 
20106 	  /* Otherwise, the next construction must be a
20107 	     member-declaration.  */
20108 	  cp_parser_member_declaration (parser);
20109 	}
20110     }
20111 }
20112 
20113 /* Parse a member-declaration.
20114 
20115    member-declaration:
20116      decl-specifier-seq [opt] member-declarator-list [opt] ;
20117      function-definition ; [opt]
20118      :: [opt] nested-name-specifier template [opt] unqualified-id ;
20119      using-declaration
20120      template-declaration
20121      alias-declaration
20122 
20123    member-declarator-list:
20124      member-declarator
20125      member-declarator-list , member-declarator
20126 
20127    member-declarator:
20128      declarator pure-specifier [opt]
20129      declarator constant-initializer [opt]
20130      identifier [opt] : constant-expression
20131 
20132    GNU Extensions:
20133 
20134    member-declaration:
20135      __extension__ member-declaration
20136 
20137    member-declarator:
20138      declarator attributes [opt] pure-specifier [opt]
20139      declarator attributes [opt] constant-initializer [opt]
20140      identifier [opt] attributes [opt] : constant-expression
20141 
20142    C++0x Extensions:
20143 
20144    member-declaration:
20145      static_assert-declaration  */
20146 
20147 static void
cp_parser_member_declaration(cp_parser * parser)20148 cp_parser_member_declaration (cp_parser* parser)
20149 {
20150   cp_decl_specifier_seq decl_specifiers;
20151   tree prefix_attributes;
20152   tree decl;
20153   int declares_class_or_enum;
20154   bool friend_p;
20155   cp_token *token = NULL;
20156   cp_token *decl_spec_token_start = NULL;
20157   cp_token *initializer_token_start = NULL;
20158   int saved_pedantic;
20159   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20160 
20161   /* Check for the `__extension__' keyword.  */
20162   if (cp_parser_extension_opt (parser, &saved_pedantic))
20163     {
20164       /* Recurse.  */
20165       cp_parser_member_declaration (parser);
20166       /* Restore the old value of the PEDANTIC flag.  */
20167       pedantic = saved_pedantic;
20168 
20169       return;
20170     }
20171 
20172   /* Check for a template-declaration.  */
20173   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20174     {
20175       /* An explicit specialization here is an error condition, and we
20176 	 expect the specialization handler to detect and report this.  */
20177       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20178 	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20179 	cp_parser_explicit_specialization (parser);
20180       else
20181 	cp_parser_template_declaration (parser, /*member_p=*/true);
20182 
20183       return;
20184     }
20185 
20186   /* Check for a using-declaration.  */
20187   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20188     {
20189       if (cxx_dialect < cxx11)
20190 	{
20191 	  /* Parse the using-declaration.  */
20192 	  cp_parser_using_declaration (parser,
20193 				       /*access_declaration_p=*/false);
20194 	  return;
20195 	}
20196       else
20197 	{
20198 	  tree decl;
20199 	  bool alias_decl_expected;
20200 	  cp_parser_parse_tentatively (parser);
20201 	  decl = cp_parser_alias_declaration (parser);
20202 	  /* Note that if we actually see the '=' token after the
20203 	     identifier, cp_parser_alias_declaration commits the
20204 	     tentative parse.  In that case, we really expects an
20205 	     alias-declaration.  Otherwise, we expect a using
20206 	     declaration.  */
20207 	  alias_decl_expected =
20208 	    !cp_parser_uncommitted_to_tentative_parse_p (parser);
20209 	  cp_parser_parse_definitely (parser);
20210 
20211 	  if (alias_decl_expected)
20212 	    finish_member_declaration (decl);
20213 	  else
20214 	    cp_parser_using_declaration (parser,
20215 					 /*access_declaration_p=*/false);
20216 	  return;
20217 	}
20218     }
20219 
20220   /* Check for @defs.  */
20221   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20222     {
20223       tree ivar, member;
20224       tree ivar_chains = cp_parser_objc_defs_expression (parser);
20225       ivar = ivar_chains;
20226       while (ivar)
20227 	{
20228 	  member = ivar;
20229 	  ivar = TREE_CHAIN (member);
20230 	  TREE_CHAIN (member) = NULL_TREE;
20231 	  finish_member_declaration (member);
20232 	}
20233       return;
20234     }
20235 
20236   /* If the next token is `static_assert' we have a static assertion.  */
20237   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20238     {
20239       cp_parser_static_assert (parser, /*member_p=*/true);
20240       return;
20241     }
20242 
20243   parser->colon_corrects_to_scope_p = false;
20244 
20245   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20246       goto out;
20247 
20248   /* Parse the decl-specifier-seq.  */
20249   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20250   cp_parser_decl_specifier_seq (parser,
20251 				CP_PARSER_FLAGS_OPTIONAL,
20252 				&decl_specifiers,
20253 				&declares_class_or_enum);
20254   /* Check for an invalid type-name.  */
20255   if (!decl_specifiers.any_type_specifiers_p
20256       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20257     goto out;
20258   /* If there is no declarator, then the decl-specifier-seq should
20259      specify a type.  */
20260   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20261     {
20262       /* If there was no decl-specifier-seq, and the next token is a
20263 	 `;', then we have something like:
20264 
20265 	   struct S { ; };
20266 
20267 	 [class.mem]
20268 
20269 	 Each member-declaration shall declare at least one member
20270 	 name of the class.  */
20271       if (!decl_specifiers.any_specifiers_p)
20272 	{
20273 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
20274 	  if (!in_system_header_at (token->location))
20275 	    pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20276 	}
20277       else
20278 	{
20279 	  tree type;
20280 
20281 	  /* See if this declaration is a friend.  */
20282 	  friend_p = cp_parser_friend_p (&decl_specifiers);
20283 	  /* If there were decl-specifiers, check to see if there was
20284 	     a class-declaration.  */
20285 	  type = check_tag_decl (&decl_specifiers,
20286 				 /*explicit_type_instantiation_p=*/false);
20287 	  /* Nested classes have already been added to the class, but
20288 	     a `friend' needs to be explicitly registered.  */
20289 	  if (friend_p)
20290 	    {
20291 	      /* If the `friend' keyword was present, the friend must
20292 		 be introduced with a class-key.  */
20293 	       if (!declares_class_or_enum && cxx_dialect < cxx11)
20294 		 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20295 			  "in C++03 a class-key must be used "
20296 			  "when declaring a friend");
20297 	       /* In this case:
20298 
20299 		    template <typename T> struct A {
20300 		      friend struct A<T>::B;
20301 		    };
20302 
20303 		  A<T>::B will be represented by a TYPENAME_TYPE, and
20304 		  therefore not recognized by check_tag_decl.  */
20305 	       if (!type)
20306 		 {
20307 		   type = decl_specifiers.type;
20308 		   if (type && TREE_CODE (type) == TYPE_DECL)
20309 		     type = TREE_TYPE (type);
20310 		 }
20311 	       if (!type || !TYPE_P (type))
20312 		 error_at (decl_spec_token_start->location,
20313 			   "friend declaration does not name a class or "
20314 			   "function");
20315 	       else
20316 		 make_friend_class (current_class_type, type,
20317 				    /*complain=*/true);
20318 	    }
20319 	  /* If there is no TYPE, an error message will already have
20320 	     been issued.  */
20321 	  else if (!type || type == error_mark_node)
20322 	    ;
20323 	  /* An anonymous aggregate has to be handled specially; such
20324 	     a declaration really declares a data member (with a
20325 	     particular type), as opposed to a nested class.  */
20326 	  else if (ANON_AGGR_TYPE_P (type))
20327 	    {
20328 	      /* C++11 9.5/6.  */
20329 	      if (decl_specifiers.storage_class != sc_none)
20330 		error_at (decl_spec_token_start->location,
20331 			  "a storage class on an anonymous aggregate "
20332 			  "in class scope is not allowed");
20333 
20334 	      /* Remove constructors and such from TYPE, now that we
20335 		 know it is an anonymous aggregate.  */
20336 	      fixup_anonymous_aggr (type);
20337 	      /* And make the corresponding data member.  */
20338 	      decl = build_decl (decl_spec_token_start->location,
20339 				 FIELD_DECL, NULL_TREE, type);
20340 	      /* Add it to the class.  */
20341 	      finish_member_declaration (decl);
20342 	    }
20343 	  else
20344 	    cp_parser_check_access_in_redeclaration
20345 					      (TYPE_NAME (type),
20346 					       decl_spec_token_start->location);
20347 	}
20348     }
20349   else
20350     {
20351       bool assume_semicolon = false;
20352 
20353       /* Clear attributes from the decl_specifiers but keep them
20354 	 around as prefix attributes that apply them to the entity
20355 	 being declared.  */
20356       prefix_attributes = decl_specifiers.attributes;
20357       decl_specifiers.attributes = NULL_TREE;
20358 
20359       /* See if these declarations will be friends.  */
20360       friend_p = cp_parser_friend_p (&decl_specifiers);
20361 
20362       /* Keep going until we hit the `;' at the end of the
20363 	 declaration.  */
20364       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20365 	{
20366 	  tree attributes = NULL_TREE;
20367 	  tree first_attribute;
20368 
20369 	  /* Peek at the next token.  */
20370 	  token = cp_lexer_peek_token (parser->lexer);
20371 
20372 	  /* Check for a bitfield declaration.  */
20373 	  if (token->type == CPP_COLON
20374 	      || (token->type == CPP_NAME
20375 		  && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20376 		  == CPP_COLON))
20377 	    {
20378 	      tree identifier;
20379 	      tree width;
20380 
20381 	      /* Get the name of the bitfield.  Note that we cannot just
20382 		 check TOKEN here because it may have been invalidated by
20383 		 the call to cp_lexer_peek_nth_token above.  */
20384 	      if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20385 		identifier = cp_parser_identifier (parser);
20386 	      else
20387 		identifier = NULL_TREE;
20388 
20389 	      /* Consume the `:' token.  */
20390 	      cp_lexer_consume_token (parser->lexer);
20391 	      /* Get the width of the bitfield.  */
20392 	      width
20393 		= cp_parser_constant_expression (parser,
20394 						 /*allow_non_constant=*/false,
20395 						 NULL);
20396 
20397 	      /* Look for attributes that apply to the bitfield.  */
20398 	      attributes = cp_parser_attributes_opt (parser);
20399 	      /* Remember which attributes are prefix attributes and
20400 		 which are not.  */
20401 	      first_attribute = attributes;
20402 	      /* Combine the attributes.  */
20403 	      attributes = chainon (prefix_attributes, attributes);
20404 
20405 	      /* Create the bitfield declaration.  */
20406 	      decl = grokbitfield (identifier
20407 				   ? make_id_declarator (NULL_TREE,
20408 							 identifier,
20409 							 sfk_none)
20410 				   : NULL,
20411 				   &decl_specifiers,
20412 				   width,
20413 				   attributes);
20414 	    }
20415 	  else
20416 	    {
20417 	      cp_declarator *declarator;
20418 	      tree initializer;
20419 	      tree asm_specification;
20420 	      int ctor_dtor_or_conv_p;
20421 
20422 	      /* Parse the declarator.  */
20423 	      declarator
20424 		= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20425 					&ctor_dtor_or_conv_p,
20426 					/*parenthesized_p=*/NULL,
20427 					/*member_p=*/true);
20428 
20429 	      /* If something went wrong parsing the declarator, make sure
20430 		 that we at least consume some tokens.  */
20431 	      if (declarator == cp_error_declarator)
20432 		{
20433 		  /* Skip to the end of the statement.  */
20434 		  cp_parser_skip_to_end_of_statement (parser);
20435 		  /* If the next token is not a semicolon, that is
20436 		     probably because we just skipped over the body of
20437 		     a function.  So, we consume a semicolon if
20438 		     present, but do not issue an error message if it
20439 		     is not present.  */
20440 		  if (cp_lexer_next_token_is (parser->lexer,
20441 					      CPP_SEMICOLON))
20442 		    cp_lexer_consume_token (parser->lexer);
20443 		  goto out;
20444 		}
20445 
20446 	      if (declares_class_or_enum & 2)
20447 		cp_parser_check_for_definition_in_return_type
20448 					    (declarator, decl_specifiers.type,
20449 					     decl_specifiers.locations[ds_type_spec]);
20450 
20451 	      /* Look for an asm-specification.  */
20452 	      asm_specification = cp_parser_asm_specification_opt (parser);
20453 	      /* Look for attributes that apply to the declaration.  */
20454 	      attributes = cp_parser_attributes_opt (parser);
20455 	      /* Remember which attributes are prefix attributes and
20456 		 which are not.  */
20457 	      first_attribute = attributes;
20458 	      /* Combine the attributes.  */
20459 	      attributes = chainon (prefix_attributes, attributes);
20460 
20461 	      /* If it's an `=', then we have a constant-initializer or a
20462 		 pure-specifier.  It is not correct to parse the
20463 		 initializer before registering the member declaration
20464 		 since the member declaration should be in scope while
20465 		 its initializer is processed.  However, the rest of the
20466 		 front end does not yet provide an interface that allows
20467 		 us to handle this correctly.  */
20468 	      if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20469 		{
20470 		  /* In [class.mem]:
20471 
20472 		     A pure-specifier shall be used only in the declaration of
20473 		     a virtual function.
20474 
20475 		     A member-declarator can contain a constant-initializer
20476 		     only if it declares a static member of integral or
20477 		     enumeration type.
20478 
20479 		     Therefore, if the DECLARATOR is for a function, we look
20480 		     for a pure-specifier; otherwise, we look for a
20481 		     constant-initializer.  When we call `grokfield', it will
20482 		     perform more stringent semantics checks.  */
20483 		  initializer_token_start = cp_lexer_peek_token (parser->lexer);
20484 		  if (function_declarator_p (declarator)
20485 		      || (decl_specifiers.type
20486 			  && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20487 			  && declarator->kind == cdk_id
20488 			  && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20489 			      == FUNCTION_TYPE)))
20490 		    initializer = cp_parser_pure_specifier (parser);
20491 		  else if (decl_specifiers.storage_class != sc_static)
20492 		    initializer = cp_parser_save_nsdmi (parser);
20493 		  else if (cxx_dialect >= cxx11)
20494 		    {
20495 		      bool nonconst;
20496 		      /* Don't require a constant rvalue in C++11, since we
20497 			 might want a reference constant.  We'll enforce
20498 		         constancy later.  */
20499 		      cp_lexer_consume_token (parser->lexer);
20500 		      /* Parse the initializer.  */
20501 		      initializer = cp_parser_initializer_clause (parser,
20502 								  &nonconst);
20503 		    }
20504 		  else
20505 		    /* Parse the initializer.  */
20506 		    initializer = cp_parser_constant_initializer (parser);
20507 		}
20508 	      else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20509 		       && !function_declarator_p (declarator))
20510 		{
20511 		  bool x;
20512 		  if (decl_specifiers.storage_class != sc_static)
20513 		    initializer = cp_parser_save_nsdmi (parser);
20514 		  else
20515 		    initializer = cp_parser_initializer (parser, &x, &x);
20516 		}
20517 	      /* Otherwise, there is no initializer.  */
20518 	      else
20519 		initializer = NULL_TREE;
20520 
20521 	      /* See if we are probably looking at a function
20522 		 definition.  We are certainly not looking at a
20523 		 member-declarator.  Calling `grokfield' has
20524 		 side-effects, so we must not do it unless we are sure
20525 		 that we are looking at a member-declarator.  */
20526 	      if (cp_parser_token_starts_function_definition_p
20527 		  (cp_lexer_peek_token (parser->lexer)))
20528 		{
20529 		  /* The grammar does not allow a pure-specifier to be
20530 		     used when a member function is defined.  (It is
20531 		     possible that this fact is an oversight in the
20532 		     standard, since a pure function may be defined
20533 		     outside of the class-specifier.  */
20534 		  if (initializer && initializer_token_start)
20535 		    error_at (initializer_token_start->location,
20536 			      "pure-specifier on function-definition");
20537 		  decl = cp_parser_save_member_function_body (parser,
20538 							      &decl_specifiers,
20539 							      declarator,
20540 							      attributes);
20541 		  if (parser->fully_implicit_function_template_p)
20542 		    decl = finish_fully_implicit_template (parser, decl);
20543 		  /* If the member was not a friend, declare it here.  */
20544 		  if (!friend_p)
20545 		    finish_member_declaration (decl);
20546 		  /* Peek at the next token.  */
20547 		  token = cp_lexer_peek_token (parser->lexer);
20548 		  /* If the next token is a semicolon, consume it.  */
20549 		  if (token->type == CPP_SEMICOLON)
20550 		    cp_lexer_consume_token (parser->lexer);
20551 		  goto out;
20552 		}
20553 	      else
20554 		if (declarator->kind == cdk_function)
20555 		  declarator->id_loc = token->location;
20556 	      /* Create the declaration.  */
20557 	      decl = grokfield (declarator, &decl_specifiers,
20558 				initializer, /*init_const_expr_p=*/true,
20559 				asm_specification, attributes);
20560 	      if (parser->fully_implicit_function_template_p)
20561 		{
20562 		  if (friend_p)
20563 		    finish_fully_implicit_template (parser, 0);
20564 		  else
20565 		    decl = finish_fully_implicit_template (parser, decl);
20566 		}
20567 	    }
20568 
20569 	  cp_finalize_omp_declare_simd (parser, decl);
20570 
20571 	  /* Reset PREFIX_ATTRIBUTES.  */
20572 	  while (attributes && TREE_CHAIN (attributes) != first_attribute)
20573 	    attributes = TREE_CHAIN (attributes);
20574 	  if (attributes)
20575 	    TREE_CHAIN (attributes) = NULL_TREE;
20576 
20577 	  /* If there is any qualification still in effect, clear it
20578 	     now; we will be starting fresh with the next declarator.  */
20579 	  parser->scope = NULL_TREE;
20580 	  parser->qualifying_scope = NULL_TREE;
20581 	  parser->object_scope = NULL_TREE;
20582 	  /* If it's a `,', then there are more declarators.  */
20583 	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20584 	    {
20585 	      cp_lexer_consume_token (parser->lexer);
20586 	      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20587 		{
20588 		  cp_token *token = cp_lexer_previous_token (parser->lexer);
20589 		  error_at (token->location,
20590 			    "stray %<,%> at end of member declaration");
20591 		}
20592 	    }
20593 	  /* If the next token isn't a `;', then we have a parse error.  */
20594 	  else if (cp_lexer_next_token_is_not (parser->lexer,
20595 					       CPP_SEMICOLON))
20596 	    {
20597 	      /* The next token might be a ways away from where the
20598 		 actual semicolon is missing.  Find the previous token
20599 		 and use that for our error position.  */
20600 	      cp_token *token = cp_lexer_previous_token (parser->lexer);
20601 	      error_at (token->location,
20602 			"expected %<;%> at end of member declaration");
20603 
20604 	      /* Assume that the user meant to provide a semicolon.  If
20605 		 we were to cp_parser_skip_to_end_of_statement, we might
20606 		 skip to a semicolon inside a member function definition
20607 		 and issue nonsensical error messages.  */
20608 	      assume_semicolon = true;
20609 	    }
20610 
20611 	  if (decl)
20612 	    {
20613 	      /* Add DECL to the list of members.  */
20614 	      if (!friend_p)
20615 		finish_member_declaration (decl);
20616 
20617 	      if (TREE_CODE (decl) == FUNCTION_DECL)
20618 		cp_parser_save_default_args (parser, decl);
20619 	      else if (TREE_CODE (decl) == FIELD_DECL
20620 		       && !DECL_C_BIT_FIELD (decl)
20621 		       && DECL_INITIAL (decl))
20622 		/* Add DECL to the queue of NSDMI to be parsed later.  */
20623 		vec_safe_push (unparsed_nsdmis, decl);
20624 	    }
20625 
20626 	  if (assume_semicolon)
20627 	    goto out;
20628 	}
20629     }
20630 
20631   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20632  out:
20633   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20634 }
20635 
20636 /* Parse a pure-specifier.
20637 
20638    pure-specifier:
20639      = 0
20640 
20641    Returns INTEGER_ZERO_NODE if a pure specifier is found.
20642    Otherwise, ERROR_MARK_NODE is returned.  */
20643 
20644 static tree
cp_parser_pure_specifier(cp_parser * parser)20645 cp_parser_pure_specifier (cp_parser* parser)
20646 {
20647   cp_token *token;
20648 
20649   /* Look for the `=' token.  */
20650   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20651     return error_mark_node;
20652   /* Look for the `0' token.  */
20653   token = cp_lexer_peek_token (parser->lexer);
20654 
20655   if (token->type == CPP_EOF
20656       || token->type == CPP_PRAGMA_EOL)
20657     return error_mark_node;
20658 
20659   cp_lexer_consume_token (parser->lexer);
20660 
20661   /* Accept = default or = delete in c++0x mode.  */
20662   if (token->keyword == RID_DEFAULT
20663       || token->keyword == RID_DELETE)
20664     {
20665       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
20666       return token->u.value;
20667     }
20668 
20669   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
20670   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
20671     {
20672       cp_parser_error (parser,
20673 		       "invalid pure specifier (only %<= 0%> is allowed)");
20674       cp_parser_skip_to_end_of_statement (parser);
20675       return error_mark_node;
20676     }
20677   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20678     {
20679       error_at (token->location, "templates may not be %<virtual%>");
20680       return error_mark_node;
20681     }
20682 
20683   return integer_zero_node;
20684 }
20685 
20686 /* Parse a constant-initializer.
20687 
20688    constant-initializer:
20689      = constant-expression
20690 
20691    Returns a representation of the constant-expression.  */
20692 
20693 static tree
cp_parser_constant_initializer(cp_parser * parser)20694 cp_parser_constant_initializer (cp_parser* parser)
20695 {
20696   /* Look for the `=' token.  */
20697   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20698     return error_mark_node;
20699 
20700   /* It is invalid to write:
20701 
20702        struct S { static const int i = { 7 }; };
20703 
20704      */
20705   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20706     {
20707       cp_parser_error (parser,
20708 		       "a brace-enclosed initializer is not allowed here");
20709       /* Consume the opening brace.  */
20710       cp_lexer_consume_token (parser->lexer);
20711       /* Skip the initializer.  */
20712       cp_parser_skip_to_closing_brace (parser);
20713       /* Look for the trailing `}'.  */
20714       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20715 
20716       return error_mark_node;
20717     }
20718 
20719   return cp_parser_constant_expression (parser,
20720 					/*allow_non_constant=*/false,
20721 					NULL);
20722 }
20723 
20724 /* Derived classes [gram.class.derived] */
20725 
20726 /* Parse a base-clause.
20727 
20728    base-clause:
20729      : base-specifier-list
20730 
20731    base-specifier-list:
20732      base-specifier ... [opt]
20733      base-specifier-list , base-specifier ... [opt]
20734 
20735    Returns a TREE_LIST representing the base-classes, in the order in
20736    which they were declared.  The representation of each node is as
20737    described by cp_parser_base_specifier.
20738 
20739    In the case that no bases are specified, this function will return
20740    NULL_TREE, not ERROR_MARK_NODE.  */
20741 
20742 static tree
cp_parser_base_clause(cp_parser * parser)20743 cp_parser_base_clause (cp_parser* parser)
20744 {
20745   tree bases = NULL_TREE;
20746 
20747   /* Look for the `:' that begins the list.  */
20748   cp_parser_require (parser, CPP_COLON, RT_COLON);
20749 
20750   /* Scan the base-specifier-list.  */
20751   while (true)
20752     {
20753       cp_token *token;
20754       tree base;
20755       bool pack_expansion_p = false;
20756 
20757       /* Look for the base-specifier.  */
20758       base = cp_parser_base_specifier (parser);
20759       /* Look for the (optional) ellipsis. */
20760       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20761         {
20762           /* Consume the `...'. */
20763           cp_lexer_consume_token (parser->lexer);
20764 
20765           pack_expansion_p = true;
20766         }
20767 
20768       /* Add BASE to the front of the list.  */
20769       if (base && base != error_mark_node)
20770 	{
20771           if (pack_expansion_p)
20772             /* Make this a pack expansion type. */
20773             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
20774 
20775           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
20776             {
20777               TREE_CHAIN (base) = bases;
20778               bases = base;
20779             }
20780 	}
20781       /* Peek at the next token.  */
20782       token = cp_lexer_peek_token (parser->lexer);
20783       /* If it's not a comma, then the list is complete.  */
20784       if (token->type != CPP_COMMA)
20785 	break;
20786       /* Consume the `,'.  */
20787       cp_lexer_consume_token (parser->lexer);
20788     }
20789 
20790   /* PARSER->SCOPE may still be non-NULL at this point, if the last
20791      base class had a qualified name.  However, the next name that
20792      appears is certainly not qualified.  */
20793   parser->scope = NULL_TREE;
20794   parser->qualifying_scope = NULL_TREE;
20795   parser->object_scope = NULL_TREE;
20796 
20797   return nreverse (bases);
20798 }
20799 
20800 /* Parse a base-specifier.
20801 
20802    base-specifier:
20803      :: [opt] nested-name-specifier [opt] class-name
20804      virtual access-specifier [opt] :: [opt] nested-name-specifier
20805        [opt] class-name
20806      access-specifier virtual [opt] :: [opt] nested-name-specifier
20807        [opt] class-name
20808 
20809    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
20810    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
20811    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
20812    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
20813 
20814 static tree
cp_parser_base_specifier(cp_parser * parser)20815 cp_parser_base_specifier (cp_parser* parser)
20816 {
20817   cp_token *token;
20818   bool done = false;
20819   bool virtual_p = false;
20820   bool duplicate_virtual_error_issued_p = false;
20821   bool duplicate_access_error_issued_p = false;
20822   bool class_scope_p, template_p;
20823   tree access = access_default_node;
20824   tree type;
20825 
20826   /* Process the optional `virtual' and `access-specifier'.  */
20827   while (!done)
20828     {
20829       /* Peek at the next token.  */
20830       token = cp_lexer_peek_token (parser->lexer);
20831       /* Process `virtual'.  */
20832       switch (token->keyword)
20833 	{
20834 	case RID_VIRTUAL:
20835 	  /* If `virtual' appears more than once, issue an error.  */
20836 	  if (virtual_p && !duplicate_virtual_error_issued_p)
20837 	    {
20838 	      cp_parser_error (parser,
20839 			       "%<virtual%> specified more than once in base-specified");
20840 	      duplicate_virtual_error_issued_p = true;
20841 	    }
20842 
20843 	  virtual_p = true;
20844 
20845 	  /* Consume the `virtual' token.  */
20846 	  cp_lexer_consume_token (parser->lexer);
20847 
20848 	  break;
20849 
20850 	case RID_PUBLIC:
20851 	case RID_PROTECTED:
20852 	case RID_PRIVATE:
20853 	  /* If more than one access specifier appears, issue an
20854 	     error.  */
20855 	  if (access != access_default_node
20856 	      && !duplicate_access_error_issued_p)
20857 	    {
20858 	      cp_parser_error (parser,
20859 			       "more than one access specifier in base-specified");
20860 	      duplicate_access_error_issued_p = true;
20861 	    }
20862 
20863 	  access = ridpointers[(int) token->keyword];
20864 
20865 	  /* Consume the access-specifier.  */
20866 	  cp_lexer_consume_token (parser->lexer);
20867 
20868 	  break;
20869 
20870 	default:
20871 	  done = true;
20872 	  break;
20873 	}
20874     }
20875   /* It is not uncommon to see programs mechanically, erroneously, use
20876      the 'typename' keyword to denote (dependent) qualified types
20877      as base classes.  */
20878   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
20879     {
20880       token = cp_lexer_peek_token (parser->lexer);
20881       if (!processing_template_decl)
20882 	error_at (token->location,
20883 		  "keyword %<typename%> not allowed outside of templates");
20884       else
20885 	error_at (token->location,
20886 		  "keyword %<typename%> not allowed in this context "
20887 		  "(the base class is implicitly a type)");
20888       cp_lexer_consume_token (parser->lexer);
20889     }
20890 
20891   /* Look for the optional `::' operator.  */
20892   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
20893   /* Look for the nested-name-specifier.  The simplest way to
20894      implement:
20895 
20896        [temp.res]
20897 
20898        The keyword `typename' is not permitted in a base-specifier or
20899        mem-initializer; in these contexts a qualified name that
20900        depends on a template-parameter is implicitly assumed to be a
20901        type name.
20902 
20903      is to pretend that we have seen the `typename' keyword at this
20904      point.  */
20905   cp_parser_nested_name_specifier_opt (parser,
20906 				       /*typename_keyword_p=*/true,
20907 				       /*check_dependency_p=*/true,
20908 				       typename_type,
20909 				       /*is_declaration=*/true);
20910   /* If the base class is given by a qualified name, assume that names
20911      we see are type names or templates, as appropriate.  */
20912   class_scope_p = (parser->scope && TYPE_P (parser->scope));
20913   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
20914 
20915   if (!parser->scope
20916       && cp_lexer_next_token_is_decltype (parser->lexer))
20917     /* DR 950 allows decltype as a base-specifier.  */
20918     type = cp_parser_decltype (parser);
20919   else
20920     {
20921       /* Otherwise, look for the class-name.  */
20922       type = cp_parser_class_name (parser,
20923 				   class_scope_p,
20924 				   template_p,
20925 				   typename_type,
20926 				   /*check_dependency_p=*/true,
20927 				   /*class_head_p=*/false,
20928 				   /*is_declaration=*/true);
20929       type = TREE_TYPE (type);
20930     }
20931 
20932   if (type == error_mark_node)
20933     return error_mark_node;
20934 
20935   return finish_base_specifier (type, access, virtual_p);
20936 }
20937 
20938 /* Exception handling [gram.exception] */
20939 
20940 /* Parse an (optional) noexcept-specification.
20941 
20942    noexcept-specification:
20943      noexcept ( constant-expression ) [opt]
20944 
20945    If no noexcept-specification is present, returns NULL_TREE.
20946    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
20947    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
20948    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
20949    Otherwise, returns a noexcept specification unless RETURN_COND is true,
20950    in which case a boolean condition is returned instead.  */
20951 
20952 static tree
cp_parser_noexcept_specification_opt(cp_parser * parser,bool require_constexpr,bool * consumed_expr,bool return_cond)20953 cp_parser_noexcept_specification_opt (cp_parser* parser,
20954 				      bool require_constexpr,
20955 				      bool* consumed_expr,
20956 				      bool return_cond)
20957 {
20958   cp_token *token;
20959   const char *saved_message;
20960 
20961   /* Peek at the next token.  */
20962   token = cp_lexer_peek_token (parser->lexer);
20963 
20964   /* Is it a noexcept-specification?  */
20965   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
20966     {
20967       tree expr;
20968       cp_lexer_consume_token (parser->lexer);
20969 
20970       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
20971 	{
20972 	  cp_lexer_consume_token (parser->lexer);
20973 
20974 	  if (require_constexpr)
20975 	    {
20976 	      /* Types may not be defined in an exception-specification.  */
20977 	      saved_message = parser->type_definition_forbidden_message;
20978 	      parser->type_definition_forbidden_message
20979 	      = G_("types may not be defined in an exception-specification");
20980 
20981 	      expr = cp_parser_constant_expression (parser, false, NULL);
20982 
20983 	      /* Restore the saved message.  */
20984 	      parser->type_definition_forbidden_message = saved_message;
20985 	    }
20986 	  else
20987 	    {
20988 	      expr = cp_parser_expression (parser, false, NULL);
20989 	      *consumed_expr = true;
20990 	    }
20991 
20992 	  cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20993 	}
20994       else
20995 	{
20996 	  expr = boolean_true_node;
20997 	  if (!require_constexpr)
20998 	    *consumed_expr = false;
20999 	}
21000 
21001       /* We cannot build a noexcept-spec right away because this will check
21002 	 that expr is a constexpr.  */
21003       if (!return_cond)
21004 	return build_noexcept_spec (expr, tf_warning_or_error);
21005       else
21006 	return expr;
21007     }
21008   else
21009     return NULL_TREE;
21010 }
21011 
21012 /* Parse an (optional) exception-specification.
21013 
21014    exception-specification:
21015      throw ( type-id-list [opt] )
21016 
21017    Returns a TREE_LIST representing the exception-specification.  The
21018    TREE_VALUE of each node is a type.  */
21019 
21020 static tree
cp_parser_exception_specification_opt(cp_parser * parser)21021 cp_parser_exception_specification_opt (cp_parser* parser)
21022 {
21023   cp_token *token;
21024   tree type_id_list;
21025   const char *saved_message;
21026 
21027   /* Peek at the next token.  */
21028   token = cp_lexer_peek_token (parser->lexer);
21029 
21030   /* Is it a noexcept-specification?  */
21031   type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21032 						      false);
21033   if (type_id_list != NULL_TREE)
21034     return type_id_list;
21035 
21036   /* If it's not `throw', then there's no exception-specification.  */
21037   if (!cp_parser_is_keyword (token, RID_THROW))
21038     return NULL_TREE;
21039 
21040 #if 0
21041   /* Enable this once a lot of code has transitioned to noexcept?  */
21042   if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21043     warning (OPT_Wdeprecated, "dynamic exception specifications are "
21044 	     "deprecated in C++0x; use %<noexcept%> instead");
21045 #endif
21046 
21047   /* Consume the `throw'.  */
21048   cp_lexer_consume_token (parser->lexer);
21049 
21050   /* Look for the `('.  */
21051   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21052 
21053   /* Peek at the next token.  */
21054   token = cp_lexer_peek_token (parser->lexer);
21055   /* If it's not a `)', then there is a type-id-list.  */
21056   if (token->type != CPP_CLOSE_PAREN)
21057     {
21058       /* Types may not be defined in an exception-specification.  */
21059       saved_message = parser->type_definition_forbidden_message;
21060       parser->type_definition_forbidden_message
21061 	= G_("types may not be defined in an exception-specification");
21062       /* Parse the type-id-list.  */
21063       type_id_list = cp_parser_type_id_list (parser);
21064       /* Restore the saved message.  */
21065       parser->type_definition_forbidden_message = saved_message;
21066     }
21067   else
21068     type_id_list = empty_except_spec;
21069 
21070   /* Look for the `)'.  */
21071   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21072 
21073   return type_id_list;
21074 }
21075 
21076 /* Parse an (optional) type-id-list.
21077 
21078    type-id-list:
21079      type-id ... [opt]
21080      type-id-list , type-id ... [opt]
21081 
21082    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
21083    in the order that the types were presented.  */
21084 
21085 static tree
cp_parser_type_id_list(cp_parser * parser)21086 cp_parser_type_id_list (cp_parser* parser)
21087 {
21088   tree types = NULL_TREE;
21089 
21090   while (true)
21091     {
21092       cp_token *token;
21093       tree type;
21094 
21095       /* Get the next type-id.  */
21096       type = cp_parser_type_id (parser);
21097       /* Parse the optional ellipsis. */
21098       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21099         {
21100           /* Consume the `...'. */
21101           cp_lexer_consume_token (parser->lexer);
21102 
21103           /* Turn the type into a pack expansion expression. */
21104           type = make_pack_expansion (type);
21105         }
21106       /* Add it to the list.  */
21107       types = add_exception_specifier (types, type, /*complain=*/1);
21108       /* Peek at the next token.  */
21109       token = cp_lexer_peek_token (parser->lexer);
21110       /* If it is not a `,', we are done.  */
21111       if (token->type != CPP_COMMA)
21112 	break;
21113       /* Consume the `,'.  */
21114       cp_lexer_consume_token (parser->lexer);
21115     }
21116 
21117   return nreverse (types);
21118 }
21119 
21120 /* Parse a try-block.
21121 
21122    try-block:
21123      try compound-statement handler-seq  */
21124 
21125 static tree
cp_parser_try_block(cp_parser * parser)21126 cp_parser_try_block (cp_parser* parser)
21127 {
21128   tree try_block;
21129 
21130   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21131   try_block = begin_try_block ();
21132   cp_parser_compound_statement (parser, NULL, true, false);
21133   finish_try_block (try_block);
21134   cp_parser_handler_seq (parser);
21135   finish_handler_sequence (try_block);
21136 
21137   return try_block;
21138 }
21139 
21140 /* Parse a function-try-block.
21141 
21142    function-try-block:
21143      try ctor-initializer [opt] function-body handler-seq  */
21144 
21145 static bool
cp_parser_function_try_block(cp_parser * parser)21146 cp_parser_function_try_block (cp_parser* parser)
21147 {
21148   tree compound_stmt;
21149   tree try_block;
21150   bool ctor_initializer_p;
21151 
21152   /* Look for the `try' keyword.  */
21153   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21154     return false;
21155   /* Let the rest of the front end know where we are.  */
21156   try_block = begin_function_try_block (&compound_stmt);
21157   /* Parse the function-body.  */
21158   ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21159     (parser, /*in_function_try_block=*/true);
21160   /* We're done with the `try' part.  */
21161   finish_function_try_block (try_block);
21162   /* Parse the handlers.  */
21163   cp_parser_handler_seq (parser);
21164   /* We're done with the handlers.  */
21165   finish_function_handler_sequence (try_block, compound_stmt);
21166 
21167   return ctor_initializer_p;
21168 }
21169 
21170 /* Parse a handler-seq.
21171 
21172    handler-seq:
21173      handler handler-seq [opt]  */
21174 
21175 static void
cp_parser_handler_seq(cp_parser * parser)21176 cp_parser_handler_seq (cp_parser* parser)
21177 {
21178   while (true)
21179     {
21180       cp_token *token;
21181 
21182       /* Parse the handler.  */
21183       cp_parser_handler (parser);
21184       /* Peek at the next token.  */
21185       token = cp_lexer_peek_token (parser->lexer);
21186       /* If it's not `catch' then there are no more handlers.  */
21187       if (!cp_parser_is_keyword (token, RID_CATCH))
21188 	break;
21189     }
21190 }
21191 
21192 /* Parse a handler.
21193 
21194    handler:
21195      catch ( exception-declaration ) compound-statement  */
21196 
21197 static void
cp_parser_handler(cp_parser * parser)21198 cp_parser_handler (cp_parser* parser)
21199 {
21200   tree handler;
21201   tree declaration;
21202 
21203   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21204   handler = begin_handler ();
21205   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21206   declaration = cp_parser_exception_declaration (parser);
21207   finish_handler_parms (declaration, handler);
21208   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21209   cp_parser_compound_statement (parser, NULL, false, false);
21210   finish_handler (handler);
21211 }
21212 
21213 /* Parse an exception-declaration.
21214 
21215    exception-declaration:
21216      type-specifier-seq declarator
21217      type-specifier-seq abstract-declarator
21218      type-specifier-seq
21219      ...
21220 
21221    Returns a VAR_DECL for the declaration, or NULL_TREE if the
21222    ellipsis variant is used.  */
21223 
21224 static tree
cp_parser_exception_declaration(cp_parser * parser)21225 cp_parser_exception_declaration (cp_parser* parser)
21226 {
21227   cp_decl_specifier_seq type_specifiers;
21228   cp_declarator *declarator;
21229   const char *saved_message;
21230 
21231   /* If it's an ellipsis, it's easy to handle.  */
21232   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21233     {
21234       /* Consume the `...' token.  */
21235       cp_lexer_consume_token (parser->lexer);
21236       return NULL_TREE;
21237     }
21238 
21239   /* Types may not be defined in exception-declarations.  */
21240   saved_message = parser->type_definition_forbidden_message;
21241   parser->type_definition_forbidden_message
21242     = G_("types may not be defined in exception-declarations");
21243 
21244   /* Parse the type-specifier-seq.  */
21245   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21246 				/*is_trailing_return=*/false,
21247 				&type_specifiers);
21248   /* If it's a `)', then there is no declarator.  */
21249   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21250     declarator = NULL;
21251   else
21252     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21253 				       /*ctor_dtor_or_conv_p=*/NULL,
21254 				       /*parenthesized_p=*/NULL,
21255 				       /*member_p=*/false);
21256 
21257   /* Restore the saved message.  */
21258   parser->type_definition_forbidden_message = saved_message;
21259 
21260   if (!type_specifiers.any_specifiers_p)
21261     return error_mark_node;
21262 
21263   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21264 }
21265 
21266 /* Parse a throw-expression.
21267 
21268    throw-expression:
21269      throw assignment-expression [opt]
21270 
21271    Returns a THROW_EXPR representing the throw-expression.  */
21272 
21273 static tree
cp_parser_throw_expression(cp_parser * parser)21274 cp_parser_throw_expression (cp_parser* parser)
21275 {
21276   tree expression;
21277   cp_token* token;
21278 
21279   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21280   token = cp_lexer_peek_token (parser->lexer);
21281   /* Figure out whether or not there is an assignment-expression
21282      following the "throw" keyword.  */
21283   if (token->type == CPP_COMMA
21284       || token->type == CPP_SEMICOLON
21285       || token->type == CPP_CLOSE_PAREN
21286       || token->type == CPP_CLOSE_SQUARE
21287       || token->type == CPP_CLOSE_BRACE
21288       || token->type == CPP_COLON)
21289     expression = NULL_TREE;
21290   else
21291     expression = cp_parser_assignment_expression (parser,
21292 						  /*cast_p=*/false, NULL);
21293 
21294   return build_throw (expression);
21295 }
21296 
21297 /* GNU Extensions */
21298 
21299 /* Parse an (optional) asm-specification.
21300 
21301    asm-specification:
21302      asm ( string-literal )
21303 
21304    If the asm-specification is present, returns a STRING_CST
21305    corresponding to the string-literal.  Otherwise, returns
21306    NULL_TREE.  */
21307 
21308 static tree
cp_parser_asm_specification_opt(cp_parser * parser)21309 cp_parser_asm_specification_opt (cp_parser* parser)
21310 {
21311   cp_token *token;
21312   tree asm_specification;
21313 
21314   /* Peek at the next token.  */
21315   token = cp_lexer_peek_token (parser->lexer);
21316   /* If the next token isn't the `asm' keyword, then there's no
21317      asm-specification.  */
21318   if (!cp_parser_is_keyword (token, RID_ASM))
21319     return NULL_TREE;
21320 
21321   /* Consume the `asm' token.  */
21322   cp_lexer_consume_token (parser->lexer);
21323   /* Look for the `('.  */
21324   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21325 
21326   /* Look for the string-literal.  */
21327   asm_specification = cp_parser_string_literal (parser, false, false);
21328 
21329   /* Look for the `)'.  */
21330   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21331 
21332   return asm_specification;
21333 }
21334 
21335 /* Parse an asm-operand-list.
21336 
21337    asm-operand-list:
21338      asm-operand
21339      asm-operand-list , asm-operand
21340 
21341    asm-operand:
21342      string-literal ( expression )
21343      [ string-literal ] string-literal ( expression )
21344 
21345    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
21346    each node is the expression.  The TREE_PURPOSE is itself a
21347    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21348    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21349    is a STRING_CST for the string literal before the parenthesis. Returns
21350    ERROR_MARK_NODE if any of the operands are invalid.  */
21351 
21352 static tree
cp_parser_asm_operand_list(cp_parser * parser)21353 cp_parser_asm_operand_list (cp_parser* parser)
21354 {
21355   tree asm_operands = NULL_TREE;
21356   bool invalid_operands = false;
21357 
21358   while (true)
21359     {
21360       tree string_literal;
21361       tree expression;
21362       tree name;
21363 
21364       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21365 	{
21366 	  /* Consume the `[' token.  */
21367 	  cp_lexer_consume_token (parser->lexer);
21368 	  /* Read the operand name.  */
21369 	  name = cp_parser_identifier (parser);
21370 	  if (name != error_mark_node)
21371 	    name = build_string (IDENTIFIER_LENGTH (name),
21372 				 IDENTIFIER_POINTER (name));
21373 	  /* Look for the closing `]'.  */
21374 	  cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21375 	}
21376       else
21377 	name = NULL_TREE;
21378       /* Look for the string-literal.  */
21379       string_literal = cp_parser_string_literal (parser, false, false);
21380 
21381       /* Look for the `('.  */
21382       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21383       /* Parse the expression.  */
21384       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
21385       /* Look for the `)'.  */
21386       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21387 
21388       if (name == error_mark_node
21389 	  || string_literal == error_mark_node
21390 	  || expression == error_mark_node)
21391         invalid_operands = true;
21392 
21393       /* Add this operand to the list.  */
21394       asm_operands = tree_cons (build_tree_list (name, string_literal),
21395 				expression,
21396 				asm_operands);
21397       /* If the next token is not a `,', there are no more
21398 	 operands.  */
21399       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21400 	break;
21401       /* Consume the `,'.  */
21402       cp_lexer_consume_token (parser->lexer);
21403     }
21404 
21405   return invalid_operands ? error_mark_node : nreverse (asm_operands);
21406 }
21407 
21408 /* Parse an asm-clobber-list.
21409 
21410    asm-clobber-list:
21411      string-literal
21412      asm-clobber-list , string-literal
21413 
21414    Returns a TREE_LIST, indicating the clobbers in the order that they
21415    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
21416 
21417 static tree
cp_parser_asm_clobber_list(cp_parser * parser)21418 cp_parser_asm_clobber_list (cp_parser* parser)
21419 {
21420   tree clobbers = NULL_TREE;
21421 
21422   while (true)
21423     {
21424       tree string_literal;
21425 
21426       /* Look for the string literal.  */
21427       string_literal = cp_parser_string_literal (parser, false, false);
21428       /* Add it to the list.  */
21429       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21430       /* If the next token is not a `,', then the list is
21431 	 complete.  */
21432       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21433 	break;
21434       /* Consume the `,' token.  */
21435       cp_lexer_consume_token (parser->lexer);
21436     }
21437 
21438   return clobbers;
21439 }
21440 
21441 /* Parse an asm-label-list.
21442 
21443    asm-label-list:
21444      identifier
21445      asm-label-list , identifier
21446 
21447    Returns a TREE_LIST, indicating the labels in the order that they
21448    appeared.  The TREE_VALUE of each node is a label.  */
21449 
21450 static tree
cp_parser_asm_label_list(cp_parser * parser)21451 cp_parser_asm_label_list (cp_parser* parser)
21452 {
21453   tree labels = NULL_TREE;
21454 
21455   while (true)
21456     {
21457       tree identifier, label, name;
21458 
21459       /* Look for the identifier.  */
21460       identifier = cp_parser_identifier (parser);
21461       if (!error_operand_p (identifier))
21462         {
21463 	  label = lookup_label (identifier);
21464 	  if (TREE_CODE (label) == LABEL_DECL)
21465 	    {
21466 	      TREE_USED (label) = 1;
21467 	      check_goto (label);
21468 	      name = build_string (IDENTIFIER_LENGTH (identifier),
21469 				   IDENTIFIER_POINTER (identifier));
21470 	      labels = tree_cons (name, label, labels);
21471 	    }
21472 	}
21473       /* If the next token is not a `,', then the list is
21474 	 complete.  */
21475       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21476 	break;
21477       /* Consume the `,' token.  */
21478       cp_lexer_consume_token (parser->lexer);
21479     }
21480 
21481   return nreverse (labels);
21482 }
21483 
21484 /* Return TRUE iff the next tokens in the stream are possibly the
21485    beginning of a GNU extension attribute. */
21486 
21487 static bool
cp_next_tokens_can_be_gnu_attribute_p(cp_parser * parser)21488 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21489 {
21490   return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21491 }
21492 
21493 /* Return TRUE iff the next tokens in the stream are possibly the
21494    beginning of a standard C++-11 attribute specifier.  */
21495 
21496 static bool
cp_next_tokens_can_be_std_attribute_p(cp_parser * parser)21497 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21498 {
21499   return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21500 }
21501 
21502 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21503    beginning of a standard C++-11 attribute specifier.  */
21504 
21505 static bool
cp_nth_tokens_can_be_std_attribute_p(cp_parser * parser,size_t n)21506 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21507 {
21508   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21509 
21510   return (cxx_dialect >= cxx11
21511 	  && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21512 	      || (token->type == CPP_OPEN_SQUARE
21513 		  && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21514 		  && token->type == CPP_OPEN_SQUARE)));
21515 }
21516 
21517 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21518    beginning of a GNU extension attribute.  */
21519 
21520 static bool
cp_nth_tokens_can_be_gnu_attribute_p(cp_parser * parser,size_t n)21521 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21522 {
21523   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21524 
21525   return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21526 }
21527 
21528 /* Return true iff the next tokens can be the beginning of either a
21529    GNU attribute list, or a standard C++11 attribute sequence.  */
21530 
21531 static bool
cp_next_tokens_can_be_attribute_p(cp_parser * parser)21532 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21533 {
21534   return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21535 	  || cp_next_tokens_can_be_std_attribute_p (parser));
21536 }
21537 
21538 /* Return true iff the next Nth tokens can be the beginning of either
21539    a GNU attribute list, or a standard C++11 attribute sequence.  */
21540 
21541 static bool
cp_nth_tokens_can_be_attribute_p(cp_parser * parser,size_t n)21542 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21543 {
21544   return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21545 	  || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21546 }
21547 
21548 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21549    of GNU attributes, or return NULL.  */
21550 
21551 static tree
cp_parser_attributes_opt(cp_parser * parser)21552 cp_parser_attributes_opt (cp_parser *parser)
21553 {
21554   if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21555       return cp_parser_gnu_attributes_opt (parser);
21556   return cp_parser_std_attribute_spec_seq (parser);
21557 }
21558 
21559 #define CILK_SIMD_FN_CLAUSE_MASK				  \
21560 	((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH)	  \
21561 	 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR)	  \
21562 	 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM)	  \
21563 	 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK)	  \
21564 	 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
21565 
21566 /* Parses the Cilk Plus SIMD-enabled function's attribute.  Syntax:
21567    vector [(<clauses>)]  */
21568 
21569 static void
cp_parser_cilk_simd_fn_vector_attrs(cp_parser * parser,cp_token * v_token)21570 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
21571 {
21572   bool first_p = parser->cilk_simd_fn_info == NULL;
21573   cp_token *token = v_token;
21574   if (first_p)
21575     {
21576       parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
21577       parser->cilk_simd_fn_info->error_seen = false;
21578       parser->cilk_simd_fn_info->fndecl_seen = false;
21579       parser->cilk_simd_fn_info->tokens = vNULL;
21580     }
21581   int paren_scope = 0;
21582   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21583     {
21584       cp_lexer_consume_token (parser->lexer);
21585       v_token = cp_lexer_peek_token (parser->lexer);
21586       paren_scope++;
21587     }
21588   while (paren_scope > 0)
21589     {
21590       token = cp_lexer_peek_token (parser->lexer);
21591       if (token->type == CPP_OPEN_PAREN)
21592 	paren_scope++;
21593       else if (token->type == CPP_CLOSE_PAREN)
21594 	paren_scope--;
21595       /* Do not push the last ')'  */
21596       if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
21597 	cp_lexer_consume_token (parser->lexer);
21598     }
21599 
21600   token->type = CPP_PRAGMA_EOL;
21601   parser->lexer->next_token = token;
21602   cp_lexer_consume_token (parser->lexer);
21603 
21604   struct cp_token_cache *cp
21605     = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
21606   parser->cilk_simd_fn_info->tokens.safe_push (cp);
21607 }
21608 
21609 /* Parse an (optional) series of attributes.
21610 
21611    attributes:
21612      attributes attribute
21613 
21614    attribute:
21615      __attribute__ (( attribute-list [opt] ))
21616 
21617    The return value is as for cp_parser_gnu_attribute_list.  */
21618 
21619 static tree
cp_parser_gnu_attributes_opt(cp_parser * parser)21620 cp_parser_gnu_attributes_opt (cp_parser* parser)
21621 {
21622   tree attributes = NULL_TREE;
21623 
21624   while (true)
21625     {
21626       cp_token *token;
21627       tree attribute_list;
21628       bool ok = true;
21629 
21630       /* Peek at the next token.  */
21631       token = cp_lexer_peek_token (parser->lexer);
21632       /* If it's not `__attribute__', then we're done.  */
21633       if (token->keyword != RID_ATTRIBUTE)
21634 	break;
21635 
21636       /* Consume the `__attribute__' keyword.  */
21637       cp_lexer_consume_token (parser->lexer);
21638       /* Look for the two `(' tokens.  */
21639       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21640       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21641 
21642       /* Peek at the next token.  */
21643       token = cp_lexer_peek_token (parser->lexer);
21644       if (token->type != CPP_CLOSE_PAREN)
21645 	/* Parse the attribute-list.  */
21646 	attribute_list = cp_parser_gnu_attribute_list (parser);
21647       else
21648 	/* If the next token is a `)', then there is no attribute
21649 	   list.  */
21650 	attribute_list = NULL;
21651 
21652       /* Look for the two `)' tokens.  */
21653       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21654 	ok = false;
21655       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21656 	ok = false;
21657       if (!ok)
21658 	cp_parser_skip_to_end_of_statement (parser);
21659 
21660       /* Add these new attributes to the list.  */
21661       attributes = chainon (attributes, attribute_list);
21662     }
21663 
21664   return attributes;
21665 }
21666 
21667 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
21668    "__vector" or "__vector__."  */
21669 
21670 static inline bool
is_cilkplus_vector_p(tree name)21671 is_cilkplus_vector_p (tree name)
21672 {
21673   if (flag_cilkplus && is_attribute_p ("vector", name))
21674     return true;
21675   return false;
21676 }
21677 
21678 /* Parse a GNU attribute-list.
21679 
21680    attribute-list:
21681      attribute
21682      attribute-list , attribute
21683 
21684    attribute:
21685      identifier
21686      identifier ( identifier )
21687      identifier ( identifier , expression-list )
21688      identifier ( expression-list )
21689 
21690    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
21691    to an attribute.  The TREE_PURPOSE of each node is the identifier
21692    indicating which attribute is in use.  The TREE_VALUE represents
21693    the arguments, if any.  */
21694 
21695 static tree
cp_parser_gnu_attribute_list(cp_parser * parser)21696 cp_parser_gnu_attribute_list (cp_parser* parser)
21697 {
21698   tree attribute_list = NULL_TREE;
21699   bool save_translate_strings_p = parser->translate_strings_p;
21700 
21701   parser->translate_strings_p = false;
21702   while (true)
21703     {
21704       cp_token *token;
21705       tree identifier;
21706       tree attribute;
21707 
21708       /* Look for the identifier.  We also allow keywords here; for
21709 	 example `__attribute__ ((const))' is legal.  */
21710       token = cp_lexer_peek_token (parser->lexer);
21711       if (token->type == CPP_NAME
21712 	  || token->type == CPP_KEYWORD)
21713 	{
21714 	  tree arguments = NULL_TREE;
21715 
21716 	  /* Consume the token, but save it since we need it for the
21717 	     SIMD enabled function parsing.  */
21718 	  cp_token *id_token = cp_lexer_consume_token (parser->lexer);
21719 
21720 	  /* Save away the identifier that indicates which attribute
21721 	     this is.  */
21722 	  identifier = (token->type == CPP_KEYWORD)
21723 	    /* For keywords, use the canonical spelling, not the
21724 	       parsed identifier.  */
21725 	    ? ridpointers[(int) token->keyword]
21726 	    : id_token->u.value;
21727 
21728 	  attribute = build_tree_list (identifier, NULL_TREE);
21729 
21730 	  /* Peek at the next token.  */
21731 	  token = cp_lexer_peek_token (parser->lexer);
21732 	  /* If it's an `(', then parse the attribute arguments.  */
21733 	  if (token->type == CPP_OPEN_PAREN)
21734 	    {
21735 	      vec<tree, va_gc> *vec;
21736 	      int attr_flag = (attribute_takes_identifier_p (identifier)
21737 			       ? id_attr : normal_attr);
21738 	      if (is_cilkplus_vector_p (identifier))
21739 		{
21740 		  cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
21741 		  continue;
21742 		}
21743 	      else
21744 		vec = cp_parser_parenthesized_expression_list
21745 		  (parser, attr_flag, /*cast_p=*/false,
21746 		   /*allow_expansion_p=*/false,
21747 		   /*non_constant_p=*/NULL);
21748 	      if (vec == NULL)
21749 		arguments = error_mark_node;
21750 	      else
21751 		{
21752 		  arguments = build_tree_list_vec (vec);
21753 		  release_tree_vector (vec);
21754 		}
21755 	      /* Save the arguments away.  */
21756 	      TREE_VALUE (attribute) = arguments;
21757 	    }
21758 	  else if (is_cilkplus_vector_p (identifier))
21759 	    {
21760 	      cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
21761 	      continue;
21762 	    }
21763 
21764 	  if (arguments != error_mark_node)
21765 	    {
21766 	      /* Add this attribute to the list.  */
21767 	      TREE_CHAIN (attribute) = attribute_list;
21768 	      attribute_list = attribute;
21769 	    }
21770 
21771 	  token = cp_lexer_peek_token (parser->lexer);
21772 	}
21773       /* Now, look for more attributes.  If the next token isn't a
21774 	 `,', we're done.  */
21775       if (token->type != CPP_COMMA)
21776 	break;
21777 
21778       /* Consume the comma and keep going.  */
21779       cp_lexer_consume_token (parser->lexer);
21780     }
21781   parser->translate_strings_p = save_translate_strings_p;
21782 
21783   /* We built up the list in reverse order.  */
21784   return nreverse (attribute_list);
21785 }
21786 
21787 /*  Parse a standard C++11 attribute.
21788 
21789     The returned representation is a TREE_LIST which TREE_PURPOSE is
21790     the scoped name of the attribute, and the TREE_VALUE is its
21791     arguments list.
21792 
21793     Note that the scoped name of the attribute is itself a TREE_LIST
21794     which TREE_PURPOSE is the namespace of the attribute, and
21795     TREE_VALUE its name.  This is unlike a GNU attribute -- as parsed
21796     by cp_parser_gnu_attribute_list -- that doesn't have any namespace
21797     and which TREE_PURPOSE is directly the attribute name.
21798 
21799     Clients of the attribute code should use get_attribute_namespace
21800     and get_attribute_name to get the actual namespace and name of
21801     attributes, regardless of their being GNU or C++11 attributes.
21802 
21803     attribute:
21804       attribute-token attribute-argument-clause [opt]
21805 
21806     attribute-token:
21807       identifier
21808       attribute-scoped-token
21809 
21810     attribute-scoped-token:
21811       attribute-namespace :: identifier
21812 
21813     attribute-namespace:
21814       identifier
21815 
21816     attribute-argument-clause:
21817       ( balanced-token-seq )
21818 
21819     balanced-token-seq:
21820       balanced-token [opt]
21821       balanced-token-seq balanced-token
21822 
21823     balanced-token:
21824       ( balanced-token-seq )
21825       [ balanced-token-seq ]
21826       { balanced-token-seq }.  */
21827 
21828 static tree
cp_parser_std_attribute(cp_parser * parser)21829 cp_parser_std_attribute (cp_parser *parser)
21830 {
21831   tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
21832   cp_token *token;
21833 
21834   /* First, parse name of the the attribute, a.k.a
21835      attribute-token.  */
21836 
21837   token = cp_lexer_peek_token (parser->lexer);
21838   if (token->type == CPP_NAME)
21839     attr_id = token->u.value;
21840   else if (token->type == CPP_KEYWORD)
21841     attr_id = ridpointers[(int) token->keyword];
21842   else if (token->flags & NAMED_OP)
21843     attr_id = get_identifier (cpp_type2name (token->type, token->flags));
21844 
21845   if (attr_id == NULL_TREE)
21846     return NULL_TREE;
21847 
21848   cp_lexer_consume_token (parser->lexer);
21849 
21850   token = cp_lexer_peek_token (parser->lexer);
21851   if (token->type == CPP_SCOPE)
21852     {
21853       /* We are seeing a scoped attribute token.  */
21854 
21855       cp_lexer_consume_token (parser->lexer);
21856       attr_ns = attr_id;
21857 
21858       token = cp_lexer_consume_token (parser->lexer);
21859       if (token->type == CPP_NAME)
21860 	attr_id = token->u.value;
21861       else if (token->type == CPP_KEYWORD)
21862 	attr_id = ridpointers[(int) token->keyword];
21863       else
21864 	{
21865 	  error_at (token->location,
21866 		    "expected an identifier for the attribute name");
21867 	  return error_mark_node;
21868 	}
21869       attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
21870 				   NULL_TREE);
21871       token = cp_lexer_peek_token (parser->lexer);
21872     }
21873   else
21874     {
21875       attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
21876 				   NULL_TREE);
21877       /* C++11 noreturn attribute is equivalent to GNU's.  */
21878       if (is_attribute_p ("noreturn", attr_id))
21879 	TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21880       /* C++14 deprecated attribute is equivalent to GNU's.  */
21881       else if (cxx_dialect >= cxx1y && is_attribute_p ("deprecated", attr_id))
21882 	TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21883     }
21884 
21885   /* Now parse the optional argument clause of the attribute.  */
21886 
21887   if (token->type != CPP_OPEN_PAREN)
21888     return attribute;
21889 
21890   {
21891     vec<tree, va_gc> *vec;
21892     int attr_flag = normal_attr;
21893 
21894     if (attr_ns == get_identifier ("gnu")
21895 	&& attribute_takes_identifier_p (attr_id))
21896       /* A GNU attribute that takes an identifier in parameter.  */
21897       attr_flag = id_attr;
21898 
21899     vec = cp_parser_parenthesized_expression_list
21900       (parser, attr_flag, /*cast_p=*/false,
21901        /*allow_expansion_p=*/true,
21902        /*non_constant_p=*/NULL);
21903     if (vec == NULL)
21904       arguments = error_mark_node;
21905     else
21906       {
21907 	arguments = build_tree_list_vec (vec);
21908 	release_tree_vector (vec);
21909       }
21910 
21911     if (arguments == error_mark_node)
21912       attribute = error_mark_node;
21913     else
21914       TREE_VALUE (attribute) = arguments;
21915   }
21916 
21917   return attribute;
21918 }
21919 
21920 /* Parse a list of standard C++-11 attributes.
21921 
21922    attribute-list:
21923      attribute [opt]
21924      attribute-list , attribute[opt]
21925      attribute ...
21926      attribute-list , attribute ...
21927 */
21928 
21929 static tree
cp_parser_std_attribute_list(cp_parser * parser)21930 cp_parser_std_attribute_list (cp_parser *parser)
21931 {
21932   tree attributes = NULL_TREE, attribute = NULL_TREE;
21933   cp_token *token = NULL;
21934 
21935   while (true)
21936     {
21937       attribute = cp_parser_std_attribute (parser);
21938       if (attribute == error_mark_node)
21939 	break;
21940       if (attribute != NULL_TREE)
21941 	{
21942 	  TREE_CHAIN (attribute) = attributes;
21943 	  attributes = attribute;
21944 	}
21945       token = cp_lexer_peek_token (parser->lexer);
21946       if (token->type != CPP_COMMA)
21947 	break;
21948       cp_lexer_consume_token (parser->lexer);
21949     }
21950   attributes = nreverse (attributes);
21951   return attributes;
21952 }
21953 
21954 /* Parse a standard C++-11 attribute specifier.
21955 
21956    attribute-specifier:
21957      [ [ attribute-list ] ]
21958      alignment-specifier
21959 
21960    alignment-specifier:
21961      alignas ( type-id ... [opt] )
21962      alignas ( alignment-expression ... [opt] ).  */
21963 
21964 static tree
cp_parser_std_attribute_spec(cp_parser * parser)21965 cp_parser_std_attribute_spec (cp_parser *parser)
21966 {
21967   tree attributes = NULL_TREE;
21968   cp_token *token = cp_lexer_peek_token (parser->lexer);
21969 
21970   if (token->type == CPP_OPEN_SQUARE
21971       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
21972     {
21973       cp_lexer_consume_token (parser->lexer);
21974       cp_lexer_consume_token (parser->lexer);
21975 
21976       attributes = cp_parser_std_attribute_list (parser);
21977 
21978       if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
21979 	  || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21980 	cp_parser_skip_to_end_of_statement (parser);
21981       else
21982 	/* Warn about parsing c++11 attribute in non-c++1 mode, only
21983 	   when we are sure that we have actually parsed them.  */
21984 	maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21985     }
21986   else
21987     {
21988       tree alignas_expr;
21989 
21990       /* Look for an alignment-specifier.  */
21991 
21992       token = cp_lexer_peek_token (parser->lexer);
21993 
21994       if (token->type != CPP_KEYWORD
21995 	  || token->keyword != RID_ALIGNAS)
21996 	return NULL_TREE;
21997 
21998       cp_lexer_consume_token (parser->lexer);
21999       maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22000 
22001       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22002 	{
22003 	  cp_parser_error (parser, "expected %<(%>");
22004 	  return error_mark_node;
22005 	}
22006 
22007       cp_parser_parse_tentatively (parser);
22008       alignas_expr = cp_parser_type_id (parser);
22009 
22010       if (!cp_parser_parse_definitely (parser))
22011 	{
22012 	  gcc_assert (alignas_expr == error_mark_node
22013 		      || alignas_expr == NULL_TREE);
22014 
22015 	  alignas_expr =
22016 	    cp_parser_assignment_expression (parser, /*cast_p=*/false,
22017 					     /**cp_id_kind=*/NULL);
22018 	  if (alignas_expr == error_mark_node)
22019 	    cp_parser_skip_to_end_of_statement (parser);
22020 	  if (alignas_expr == NULL_TREE
22021 	      || alignas_expr == error_mark_node)
22022 	    return alignas_expr;
22023 	}
22024 
22025       if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22026 	{
22027 	  cp_parser_error (parser, "expected %<)%>");
22028 	  return error_mark_node;
22029 	}
22030 
22031       alignas_expr = cxx_alignas_expr (alignas_expr);
22032 
22033       /* Build the C++-11 representation of an 'aligned'
22034 	 attribute.  */
22035       attributes =
22036 	build_tree_list (build_tree_list (get_identifier ("gnu"),
22037 					  get_identifier ("aligned")),
22038 			 build_tree_list (NULL_TREE, alignas_expr));
22039     }
22040 
22041   return attributes;
22042 }
22043 
22044 /* Parse a standard C++-11 attribute-specifier-seq.
22045 
22046    attribute-specifier-seq:
22047      attribute-specifier-seq [opt] attribute-specifier
22048  */
22049 
22050 static tree
cp_parser_std_attribute_spec_seq(cp_parser * parser)22051 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22052 {
22053   tree attr_specs = NULL;
22054 
22055   while (true)
22056     {
22057       tree attr_spec = cp_parser_std_attribute_spec (parser);
22058       if (attr_spec == NULL_TREE)
22059 	break;
22060       if (attr_spec == error_mark_node)
22061 	return error_mark_node;
22062 
22063       TREE_CHAIN (attr_spec) = attr_specs;
22064       attr_specs = attr_spec;
22065     }
22066 
22067   attr_specs = nreverse (attr_specs);
22068   return attr_specs;
22069 }
22070 
22071 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
22072    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
22073    current value of the PEDANTIC flag, regardless of whether or not
22074    the `__extension__' keyword is present.  The caller is responsible
22075    for restoring the value of the PEDANTIC flag.  */
22076 
22077 static bool
cp_parser_extension_opt(cp_parser * parser,int * saved_pedantic)22078 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22079 {
22080   /* Save the old value of the PEDANTIC flag.  */
22081   *saved_pedantic = pedantic;
22082 
22083   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22084     {
22085       /* Consume the `__extension__' token.  */
22086       cp_lexer_consume_token (parser->lexer);
22087       /* We're not being pedantic while the `__extension__' keyword is
22088 	 in effect.  */
22089       pedantic = 0;
22090 
22091       return true;
22092     }
22093 
22094   return false;
22095 }
22096 
22097 /* Parse a label declaration.
22098 
22099    label-declaration:
22100      __label__ label-declarator-seq ;
22101 
22102    label-declarator-seq:
22103      identifier , label-declarator-seq
22104      identifier  */
22105 
22106 static void
cp_parser_label_declaration(cp_parser * parser)22107 cp_parser_label_declaration (cp_parser* parser)
22108 {
22109   /* Look for the `__label__' keyword.  */
22110   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22111 
22112   while (true)
22113     {
22114       tree identifier;
22115 
22116       /* Look for an identifier.  */
22117       identifier = cp_parser_identifier (parser);
22118       /* If we failed, stop.  */
22119       if (identifier == error_mark_node)
22120 	break;
22121       /* Declare it as a label.  */
22122       finish_label_decl (identifier);
22123       /* If the next token is a `;', stop.  */
22124       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22125 	break;
22126       /* Look for the `,' separating the label declarations.  */
22127       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22128     }
22129 
22130   /* Look for the final `;'.  */
22131   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22132 }
22133 
22134 /* Support Functions */
22135 
22136 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22137    NAME should have one of the representations used for an
22138    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22139    is returned.  If PARSER->SCOPE is a dependent type, then a
22140    SCOPE_REF is returned.
22141 
22142    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22143    returned; the name was already resolved when the TEMPLATE_ID_EXPR
22144    was formed.  Abstractly, such entities should not be passed to this
22145    function, because they do not need to be looked up, but it is
22146    simpler to check for this special case here, rather than at the
22147    call-sites.
22148 
22149    In cases not explicitly covered above, this function returns a
22150    DECL, OVERLOAD, or baselink representing the result of the lookup.
22151    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22152    is returned.
22153 
22154    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22155    (e.g., "struct") that was used.  In that case bindings that do not
22156    refer to types are ignored.
22157 
22158    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22159    ignored.
22160 
22161    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22162    are ignored.
22163 
22164    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22165    types.
22166 
22167    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22168    TREE_LIST of candidates if name-lookup results in an ambiguity, and
22169    NULL_TREE otherwise.  */
22170 
22171 static tree
cp_parser_lookup_name(cp_parser * parser,tree name,enum tag_types tag_type,bool is_template,bool is_namespace,bool check_dependency,tree * ambiguous_decls,location_t name_location)22172 cp_parser_lookup_name (cp_parser *parser, tree name,
22173 		       enum tag_types tag_type,
22174 		       bool is_template,
22175 		       bool is_namespace,
22176 		       bool check_dependency,
22177 		       tree *ambiguous_decls,
22178 		       location_t name_location)
22179 {
22180   tree decl;
22181   tree object_type = parser->context->object_type;
22182 
22183   /* Assume that the lookup will be unambiguous.  */
22184   if (ambiguous_decls)
22185     *ambiguous_decls = NULL_TREE;
22186 
22187   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22188      no longer valid.  Note that if we are parsing tentatively, and
22189      the parse fails, OBJECT_TYPE will be automatically restored.  */
22190   parser->context->object_type = NULL_TREE;
22191 
22192   if (name == error_mark_node)
22193     return error_mark_node;
22194 
22195   /* A template-id has already been resolved; there is no lookup to
22196      do.  */
22197   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22198     return name;
22199   if (BASELINK_P (name))
22200     {
22201       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22202 		  == TEMPLATE_ID_EXPR);
22203       return name;
22204     }
22205 
22206   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
22207      it should already have been checked to make sure that the name
22208      used matches the type being destroyed.  */
22209   if (TREE_CODE (name) == BIT_NOT_EXPR)
22210     {
22211       tree type;
22212 
22213       /* Figure out to which type this destructor applies.  */
22214       if (parser->scope)
22215 	type = parser->scope;
22216       else if (object_type)
22217 	type = object_type;
22218       else
22219 	type = current_class_type;
22220       /* If that's not a class type, there is no destructor.  */
22221       if (!type || !CLASS_TYPE_P (type))
22222 	return error_mark_node;
22223       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22224 	lazily_declare_fn (sfk_destructor, type);
22225       if (!CLASSTYPE_DESTRUCTORS (type))
22226 	  return error_mark_node;
22227       /* If it was a class type, return the destructor.  */
22228       return CLASSTYPE_DESTRUCTORS (type);
22229     }
22230 
22231   /* By this point, the NAME should be an ordinary identifier.  If
22232      the id-expression was a qualified name, the qualifying scope is
22233      stored in PARSER->SCOPE at this point.  */
22234   gcc_assert (identifier_p (name));
22235 
22236   /* Perform the lookup.  */
22237   if (parser->scope)
22238     {
22239       bool dependent_p;
22240 
22241       if (parser->scope == error_mark_node)
22242 	return error_mark_node;
22243 
22244       /* If the SCOPE is dependent, the lookup must be deferred until
22245 	 the template is instantiated -- unless we are explicitly
22246 	 looking up names in uninstantiated templates.  Even then, we
22247 	 cannot look up the name if the scope is not a class type; it
22248 	 might, for example, be a template type parameter.  */
22249       dependent_p = (TYPE_P (parser->scope)
22250 		     && dependent_scope_p (parser->scope));
22251       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22252 	  && dependent_p)
22253 	/* Defer lookup.  */
22254 	decl = error_mark_node;
22255       else
22256 	{
22257 	  tree pushed_scope = NULL_TREE;
22258 
22259 	  /* If PARSER->SCOPE is a dependent type, then it must be a
22260 	     class type, and we must not be checking dependencies;
22261 	     otherwise, we would have processed this lookup above.  So
22262 	     that PARSER->SCOPE is not considered a dependent base by
22263 	     lookup_member, we must enter the scope here.  */
22264 	  if (dependent_p)
22265 	    pushed_scope = push_scope (parser->scope);
22266 
22267 	  /* If the PARSER->SCOPE is a template specialization, it
22268 	     may be instantiated during name lookup.  In that case,
22269 	     errors may be issued.  Even if we rollback the current
22270 	     tentative parse, those errors are valid.  */
22271 	  decl = lookup_qualified_name (parser->scope, name,
22272 					tag_type != none_type,
22273 					/*complain=*/true);
22274 
22275 	  /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22276 	     lookup result and the nested-name-specifier nominates a class C:
22277 	       * if the name specified after the nested-name-specifier, when
22278 	       looked up in C, is the injected-class-name of C (Clause 9), or
22279 	       * if the name specified after the nested-name-specifier is the
22280 	       same as the identifier or the simple-template-id's template-
22281 	       name in the last component of the nested-name-specifier,
22282 	     the name is instead considered to name the constructor of
22283 	     class C. [ Note: for example, the constructor is not an
22284 	     acceptable lookup result in an elaborated-type-specifier so
22285 	     the constructor would not be used in place of the
22286 	     injected-class-name. --end note ] Such a constructor name
22287 	     shall be used only in the declarator-id of a declaration that
22288 	     names a constructor or in a using-declaration.  */
22289 	  if (tag_type == none_type
22290 	      && DECL_SELF_REFERENCE_P (decl)
22291 	      && same_type_p (DECL_CONTEXT (decl), parser->scope))
22292 	    decl = lookup_qualified_name (parser->scope, ctor_identifier,
22293 					  tag_type != none_type,
22294 					  /*complain=*/true);
22295 
22296 	  /* If we have a single function from a using decl, pull it out.  */
22297 	  if (TREE_CODE (decl) == OVERLOAD
22298 	      && !really_overloaded_fn (decl))
22299 	    decl = OVL_FUNCTION (decl);
22300 
22301 	  if (pushed_scope)
22302 	    pop_scope (pushed_scope);
22303 	}
22304 
22305       /* If the scope is a dependent type and either we deferred lookup or
22306 	 we did lookup but didn't find the name, rememeber the name.  */
22307       if (decl == error_mark_node && TYPE_P (parser->scope)
22308 	  && dependent_type_p (parser->scope))
22309 	{
22310 	  if (tag_type)
22311 	    {
22312 	      tree type;
22313 
22314 	      /* The resolution to Core Issue 180 says that `struct
22315 		 A::B' should be considered a type-name, even if `A'
22316 		 is dependent.  */
22317 	      type = make_typename_type (parser->scope, name, tag_type,
22318 					 /*complain=*/tf_error);
22319 	      if (type != error_mark_node)
22320 		decl = TYPE_NAME (type);
22321 	    }
22322 	  else if (is_template
22323 		   && (cp_parser_next_token_ends_template_argument_p (parser)
22324 		       || cp_lexer_next_token_is (parser->lexer,
22325 						  CPP_CLOSE_PAREN)))
22326 	    decl = make_unbound_class_template (parser->scope,
22327 						name, NULL_TREE,
22328 						/*complain=*/tf_error);
22329 	  else
22330 	    decl = build_qualified_name (/*type=*/NULL_TREE,
22331 					 parser->scope, name,
22332 					 is_template);
22333 	}
22334       parser->qualifying_scope = parser->scope;
22335       parser->object_scope = NULL_TREE;
22336     }
22337   else if (object_type)
22338     {
22339       /* Look up the name in the scope of the OBJECT_TYPE, unless the
22340 	 OBJECT_TYPE is not a class.  */
22341       if (CLASS_TYPE_P (object_type))
22342 	/* If the OBJECT_TYPE is a template specialization, it may
22343 	   be instantiated during name lookup.  In that case, errors
22344 	   may be issued.  Even if we rollback the current tentative
22345 	   parse, those errors are valid.  */
22346 	decl = lookup_member (object_type,
22347 			      name,
22348 			      /*protect=*/0,
22349 			      tag_type != none_type,
22350 			      tf_warning_or_error);
22351       else
22352 	decl = NULL_TREE;
22353 
22354       if (!decl)
22355 	/* Look it up in the enclosing context.  */
22356 	decl = lookup_name_real (name, tag_type != none_type,
22357 				 /*nonclass=*/0,
22358 				 /*block_p=*/true, is_namespace, 0);
22359       parser->object_scope = object_type;
22360       parser->qualifying_scope = NULL_TREE;
22361     }
22362   else
22363     {
22364       decl = lookup_name_real (name, tag_type != none_type,
22365 			       /*nonclass=*/0,
22366 			       /*block_p=*/true, is_namespace, 0);
22367       parser->qualifying_scope = NULL_TREE;
22368       parser->object_scope = NULL_TREE;
22369     }
22370 
22371   /* If the lookup failed, let our caller know.  */
22372   if (!decl || decl == error_mark_node)
22373     return error_mark_node;
22374 
22375   /* Pull out the template from an injected-class-name (or multiple).  */
22376   if (is_template)
22377     decl = maybe_get_template_decl_from_type_decl (decl);
22378 
22379   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
22380   if (TREE_CODE (decl) == TREE_LIST)
22381     {
22382       if (ambiguous_decls)
22383 	*ambiguous_decls = decl;
22384       /* The error message we have to print is too complicated for
22385 	 cp_parser_error, so we incorporate its actions directly.  */
22386       if (!cp_parser_simulate_error (parser))
22387 	{
22388 	  error_at (name_location, "reference to %qD is ambiguous",
22389 		    name);
22390 	  print_candidates (decl);
22391 	}
22392       return error_mark_node;
22393     }
22394 
22395   gcc_assert (DECL_P (decl)
22396 	      || TREE_CODE (decl) == OVERLOAD
22397 	      || TREE_CODE (decl) == SCOPE_REF
22398 	      || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22399 	      || BASELINK_P (decl));
22400 
22401   /* If we have resolved the name of a member declaration, check to
22402      see if the declaration is accessible.  When the name resolves to
22403      set of overloaded functions, accessibility is checked when
22404      overload resolution is done.
22405 
22406      During an explicit instantiation, access is not checked at all,
22407      as per [temp.explicit].  */
22408   if (DECL_P (decl))
22409     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22410 
22411   maybe_record_typedef_use (decl);
22412 
22413   return decl;
22414 }
22415 
22416 /* Like cp_parser_lookup_name, but for use in the typical case where
22417    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22418    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
22419 
22420 static tree
cp_parser_lookup_name_simple(cp_parser * parser,tree name,location_t location)22421 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22422 {
22423   return cp_parser_lookup_name (parser, name,
22424 				none_type,
22425 				/*is_template=*/false,
22426 				/*is_namespace=*/false,
22427 				/*check_dependency=*/true,
22428 				/*ambiguous_decls=*/NULL,
22429 				location);
22430 }
22431 
22432 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22433    the current context, return the TYPE_DECL.  If TAG_NAME_P is
22434    true, the DECL indicates the class being defined in a class-head,
22435    or declared in an elaborated-type-specifier.
22436 
22437    Otherwise, return DECL.  */
22438 
22439 static tree
cp_parser_maybe_treat_template_as_class(tree decl,bool tag_name_p)22440 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22441 {
22442   /* If the TEMPLATE_DECL is being declared as part of a class-head,
22443      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22444 
22445        struct A {
22446 	 template <typename T> struct B;
22447        };
22448 
22449        template <typename T> struct A::B {};
22450 
22451      Similarly, in an elaborated-type-specifier:
22452 
22453        namespace N { struct X{}; }
22454 
22455        struct A {
22456 	 template <typename T> friend struct N::X;
22457        };
22458 
22459      However, if the DECL refers to a class type, and we are in
22460      the scope of the class, then the name lookup automatically
22461      finds the TYPE_DECL created by build_self_reference rather
22462      than a TEMPLATE_DECL.  For example, in:
22463 
22464        template <class T> struct S {
22465 	 S s;
22466        };
22467 
22468      there is no need to handle such case.  */
22469 
22470   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22471     return DECL_TEMPLATE_RESULT (decl);
22472 
22473   return decl;
22474 }
22475 
22476 /* If too many, or too few, template-parameter lists apply to the
22477    declarator, issue an error message.  Returns TRUE if all went well,
22478    and FALSE otherwise.  */
22479 
22480 static bool
cp_parser_check_declarator_template_parameters(cp_parser * parser,cp_declarator * declarator,location_t declarator_location)22481 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22482 						cp_declarator *declarator,
22483 						location_t declarator_location)
22484 {
22485   switch (declarator->kind)
22486     {
22487     case cdk_id:
22488       {
22489 	unsigned num_templates = 0;
22490 	tree scope = declarator->u.id.qualifying_scope;
22491 
22492 	if (scope)
22493 	  num_templates = num_template_headers_for_class (scope);
22494 	else if (TREE_CODE (declarator->u.id.unqualified_name)
22495 		 == TEMPLATE_ID_EXPR)
22496 	  /* If the DECLARATOR has the form `X<y>' then it uses one
22497 	     additional level of template parameters.  */
22498 	  ++num_templates;
22499 
22500 	return cp_parser_check_template_parameters
22501 	  (parser, num_templates, declarator_location, declarator);
22502       }
22503 
22504     case cdk_function:
22505     case cdk_array:
22506     case cdk_pointer:
22507     case cdk_reference:
22508     case cdk_ptrmem:
22509       return (cp_parser_check_declarator_template_parameters
22510 	      (parser, declarator->declarator, declarator_location));
22511 
22512     case cdk_error:
22513       return true;
22514 
22515     default:
22516       gcc_unreachable ();
22517     }
22518   return false;
22519 }
22520 
22521 /* NUM_TEMPLATES were used in the current declaration.  If that is
22522    invalid, return FALSE and issue an error messages.  Otherwise,
22523    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
22524    declarator and we can print more accurate diagnostics.  */
22525 
22526 static bool
cp_parser_check_template_parameters(cp_parser * parser,unsigned num_templates,location_t location,cp_declarator * declarator)22527 cp_parser_check_template_parameters (cp_parser* parser,
22528 				     unsigned num_templates,
22529 				     location_t location,
22530 				     cp_declarator *declarator)
22531 {
22532   /* If there are the same number of template classes and parameter
22533      lists, that's OK.  */
22534   if (parser->num_template_parameter_lists == num_templates)
22535     return true;
22536   /* If there are more, but only one more, then we are referring to a
22537      member template.  That's OK too.  */
22538   if (parser->num_template_parameter_lists == num_templates + 1)
22539     return true;
22540   /* If there are more template classes than parameter lists, we have
22541      something like:
22542 
22543        template <class T> void S<T>::R<T>::f ();  */
22544   if (parser->num_template_parameter_lists < num_templates)
22545     {
22546       if (declarator && !current_function_decl)
22547 	error_at (location, "specializing member %<%T::%E%> "
22548 		  "requires %<template<>%> syntax",
22549 		  declarator->u.id.qualifying_scope,
22550 		  declarator->u.id.unqualified_name);
22551       else if (declarator)
22552 	error_at (location, "invalid declaration of %<%T::%E%>",
22553 		  declarator->u.id.qualifying_scope,
22554 		  declarator->u.id.unqualified_name);
22555       else
22556 	error_at (location, "too few template-parameter-lists");
22557       return false;
22558     }
22559   /* Otherwise, there are too many template parameter lists.  We have
22560      something like:
22561 
22562      template <class T> template <class U> void S::f();  */
22563   error_at (location, "too many template-parameter-lists");
22564   return false;
22565 }
22566 
22567 /* Parse an optional `::' token indicating that the following name is
22568    from the global namespace.  If so, PARSER->SCOPE is set to the
22569    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22570    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22571    Returns the new value of PARSER->SCOPE, if the `::' token is
22572    present, and NULL_TREE otherwise.  */
22573 
22574 static tree
cp_parser_global_scope_opt(cp_parser * parser,bool current_scope_valid_p)22575 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
22576 {
22577   cp_token *token;
22578 
22579   /* Peek at the next token.  */
22580   token = cp_lexer_peek_token (parser->lexer);
22581   /* If we're looking at a `::' token then we're starting from the
22582      global namespace, not our current location.  */
22583   if (token->type == CPP_SCOPE)
22584     {
22585       /* Consume the `::' token.  */
22586       cp_lexer_consume_token (parser->lexer);
22587       /* Set the SCOPE so that we know where to start the lookup.  */
22588       parser->scope = global_namespace;
22589       parser->qualifying_scope = global_namespace;
22590       parser->object_scope = NULL_TREE;
22591 
22592       return parser->scope;
22593     }
22594   else if (!current_scope_valid_p)
22595     {
22596       parser->scope = NULL_TREE;
22597       parser->qualifying_scope = NULL_TREE;
22598       parser->object_scope = NULL_TREE;
22599     }
22600 
22601   return NULL_TREE;
22602 }
22603 
22604 /* Returns TRUE if the upcoming token sequence is the start of a
22605    constructor declarator.  If FRIEND_P is true, the declarator is
22606    preceded by the `friend' specifier.  */
22607 
22608 static bool
cp_parser_constructor_declarator_p(cp_parser * parser,bool friend_p)22609 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
22610 {
22611   bool constructor_p;
22612   bool outside_class_specifier_p;
22613   tree nested_name_specifier;
22614   cp_token *next_token;
22615 
22616   /* The common case is that this is not a constructor declarator, so
22617      try to avoid doing lots of work if at all possible.  It's not
22618      valid declare a constructor at function scope.  */
22619   if (parser->in_function_body)
22620     return false;
22621   /* And only certain tokens can begin a constructor declarator.  */
22622   next_token = cp_lexer_peek_token (parser->lexer);
22623   if (next_token->type != CPP_NAME
22624       && next_token->type != CPP_SCOPE
22625       && next_token->type != CPP_NESTED_NAME_SPECIFIER
22626       && next_token->type != CPP_TEMPLATE_ID)
22627     return false;
22628 
22629   /* Parse tentatively; we are going to roll back all of the tokens
22630      consumed here.  */
22631   cp_parser_parse_tentatively (parser);
22632   /* Assume that we are looking at a constructor declarator.  */
22633   constructor_p = true;
22634 
22635   /* Look for the optional `::' operator.  */
22636   cp_parser_global_scope_opt (parser,
22637 			      /*current_scope_valid_p=*/false);
22638   /* Look for the nested-name-specifier.  */
22639   nested_name_specifier
22640     = (cp_parser_nested_name_specifier_opt (parser,
22641 					    /*typename_keyword_p=*/false,
22642 					    /*check_dependency_p=*/false,
22643 					    /*type_p=*/false,
22644 					    /*is_declaration=*/false));
22645 
22646   outside_class_specifier_p = (!at_class_scope_p ()
22647 			       || !TYPE_BEING_DEFINED (current_class_type)
22648 			       || friend_p);
22649 
22650   /* Outside of a class-specifier, there must be a
22651      nested-name-specifier.  */
22652   if (!nested_name_specifier && outside_class_specifier_p)
22653     constructor_p = false;
22654   else if (nested_name_specifier == error_mark_node)
22655     constructor_p = false;
22656 
22657   /* If we have a class scope, this is easy; DR 147 says that S::S always
22658      names the constructor, and no other qualified name could.  */
22659   if (constructor_p && nested_name_specifier
22660       && CLASS_TYPE_P (nested_name_specifier))
22661     {
22662       tree id = cp_parser_unqualified_id (parser,
22663 					  /*template_keyword_p=*/false,
22664 					  /*check_dependency_p=*/false,
22665 					  /*declarator_p=*/true,
22666 					  /*optional_p=*/false);
22667       if (is_overloaded_fn (id))
22668 	id = DECL_NAME (get_first_fn (id));
22669       if (!constructor_name_p (id, nested_name_specifier))
22670 	constructor_p = false;
22671     }
22672   /* If we still think that this might be a constructor-declarator,
22673      look for a class-name.  */
22674   else if (constructor_p)
22675     {
22676       /* If we have:
22677 
22678 	   template <typename T> struct S {
22679 	     S();
22680 	   };
22681 
22682 	 we must recognize that the nested `S' names a class.  */
22683       tree type_decl;
22684       type_decl = cp_parser_class_name (parser,
22685 					/*typename_keyword_p=*/false,
22686 					/*template_keyword_p=*/false,
22687 					none_type,
22688 					/*check_dependency_p=*/false,
22689 					/*class_head_p=*/false,
22690 					/*is_declaration=*/false);
22691       /* If there was no class-name, then this is not a constructor.
22692 	 Otherwise, if we are in a class-specifier and we aren't
22693 	 handling a friend declaration, check that its type matches
22694 	 current_class_type (c++/38313).  Note: error_mark_node
22695 	 is left alone for error recovery purposes.  */
22696       constructor_p = (!cp_parser_error_occurred (parser)
22697 		       && (outside_class_specifier_p
22698 			   || type_decl == error_mark_node
22699 			   || same_type_p (current_class_type,
22700 					   TREE_TYPE (type_decl))));
22701 
22702       /* If we're still considering a constructor, we have to see a `(',
22703 	 to begin the parameter-declaration-clause, followed by either a
22704 	 `)', an `...', or a decl-specifier.  We need to check for a
22705 	 type-specifier to avoid being fooled into thinking that:
22706 
22707 	   S (f) (int);
22708 
22709 	 is a constructor.  (It is actually a function named `f' that
22710 	 takes one parameter (of type `int') and returns a value of type
22711 	 `S'.  */
22712       if (constructor_p
22713 	  && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22714 	constructor_p = false;
22715 
22716       if (constructor_p
22717 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
22718 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
22719 	  /* A parameter declaration begins with a decl-specifier,
22720 	     which is either the "attribute" keyword, a storage class
22721 	     specifier, or (usually) a type-specifier.  */
22722 	  && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
22723 	{
22724 	  tree type;
22725 	  tree pushed_scope = NULL_TREE;
22726 	  unsigned saved_num_template_parameter_lists;
22727 
22728 	  /* Names appearing in the type-specifier should be looked up
22729 	     in the scope of the class.  */
22730 	  if (current_class_type)
22731 	    type = NULL_TREE;
22732 	  else
22733 	    {
22734 	      type = TREE_TYPE (type_decl);
22735 	      if (TREE_CODE (type) == TYPENAME_TYPE)
22736 		{
22737 		  type = resolve_typename_type (type,
22738 						/*only_current_p=*/false);
22739 		  if (TREE_CODE (type) == TYPENAME_TYPE)
22740 		    {
22741 		      cp_parser_abort_tentative_parse (parser);
22742 		      return false;
22743 		    }
22744 		}
22745 	      pushed_scope = push_scope (type);
22746 	    }
22747 
22748 	  /* Inside the constructor parameter list, surrounding
22749 	     template-parameter-lists do not apply.  */
22750 	  saved_num_template_parameter_lists
22751 	    = parser->num_template_parameter_lists;
22752 	  parser->num_template_parameter_lists = 0;
22753 
22754 	  /* Look for the type-specifier.  */
22755 	  cp_parser_type_specifier (parser,
22756 				    CP_PARSER_FLAGS_NONE,
22757 				    /*decl_specs=*/NULL,
22758 				    /*is_declarator=*/true,
22759 				    /*declares_class_or_enum=*/NULL,
22760 				    /*is_cv_qualifier=*/NULL);
22761 
22762 	  parser->num_template_parameter_lists
22763 	    = saved_num_template_parameter_lists;
22764 
22765 	  /* Leave the scope of the class.  */
22766 	  if (pushed_scope)
22767 	    pop_scope (pushed_scope);
22768 
22769 	  constructor_p = !cp_parser_error_occurred (parser);
22770 	}
22771     }
22772 
22773   /* We did not really want to consume any tokens.  */
22774   cp_parser_abort_tentative_parse (parser);
22775 
22776   return constructor_p;
22777 }
22778 
22779 /* Parse the definition of the function given by the DECL_SPECIFIERS,
22780    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
22781    they must be performed once we are in the scope of the function.
22782 
22783    Returns the function defined.  */
22784 
22785 static tree
cp_parser_function_definition_from_specifiers_and_declarator(cp_parser * parser,cp_decl_specifier_seq * decl_specifiers,tree attributes,const cp_declarator * declarator)22786 cp_parser_function_definition_from_specifiers_and_declarator
22787   (cp_parser* parser,
22788    cp_decl_specifier_seq *decl_specifiers,
22789    tree attributes,
22790    const cp_declarator *declarator)
22791 {
22792   tree fn;
22793   bool success_p;
22794 
22795   /* Begin the function-definition.  */
22796   success_p = start_function (decl_specifiers, declarator, attributes);
22797 
22798   /* The things we're about to see are not directly qualified by any
22799      template headers we've seen thus far.  */
22800   reset_specialization ();
22801 
22802   /* If there were names looked up in the decl-specifier-seq that we
22803      did not check, check them now.  We must wait until we are in the
22804      scope of the function to perform the checks, since the function
22805      might be a friend.  */
22806   perform_deferred_access_checks (tf_warning_or_error);
22807 
22808   if (success_p)
22809     {
22810       cp_finalize_omp_declare_simd (parser, current_function_decl);
22811       parser->omp_declare_simd = NULL;
22812     }
22813 
22814   if (!success_p)
22815     {
22816       /* Skip the entire function.  */
22817       cp_parser_skip_to_end_of_block_or_statement (parser);
22818       fn = error_mark_node;
22819     }
22820   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
22821     {
22822       /* Seen already, skip it.  An error message has already been output.  */
22823       cp_parser_skip_to_end_of_block_or_statement (parser);
22824       fn = current_function_decl;
22825       current_function_decl = NULL_TREE;
22826       /* If this is a function from a class, pop the nested class.  */
22827       if (current_class_name)
22828 	pop_nested_class ();
22829     }
22830   else
22831     {
22832       timevar_id_t tv;
22833       if (DECL_DECLARED_INLINE_P (current_function_decl))
22834         tv = TV_PARSE_INLINE;
22835       else
22836         tv = TV_PARSE_FUNC;
22837       timevar_push (tv);
22838       fn = cp_parser_function_definition_after_declarator (parser,
22839 							 /*inline_p=*/false);
22840       timevar_pop (tv);
22841     }
22842 
22843   return fn;
22844 }
22845 
22846 /* Parse the part of a function-definition that follows the
22847    declarator.  INLINE_P is TRUE iff this function is an inline
22848    function defined within a class-specifier.
22849 
22850    Returns the function defined.  */
22851 
22852 static tree
cp_parser_function_definition_after_declarator(cp_parser * parser,bool inline_p)22853 cp_parser_function_definition_after_declarator (cp_parser* parser,
22854 						bool inline_p)
22855 {
22856   tree fn;
22857   bool ctor_initializer_p = false;
22858   bool saved_in_unbraced_linkage_specification_p;
22859   bool saved_in_function_body;
22860   unsigned saved_num_template_parameter_lists;
22861   cp_token *token;
22862   bool fully_implicit_function_template_p
22863     = parser->fully_implicit_function_template_p;
22864   parser->fully_implicit_function_template_p = false;
22865   tree implicit_template_parms
22866     = parser->implicit_template_parms;
22867   parser->implicit_template_parms = 0;
22868   cp_binding_level* implicit_template_scope
22869     = parser->implicit_template_scope;
22870   parser->implicit_template_scope = 0;
22871 
22872   saved_in_function_body = parser->in_function_body;
22873   parser->in_function_body = true;
22874   /* If the next token is `return', then the code may be trying to
22875      make use of the "named return value" extension that G++ used to
22876      support.  */
22877   token = cp_lexer_peek_token (parser->lexer);
22878   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
22879     {
22880       /* Consume the `return' keyword.  */
22881       cp_lexer_consume_token (parser->lexer);
22882       /* Look for the identifier that indicates what value is to be
22883 	 returned.  */
22884       cp_parser_identifier (parser);
22885       /* Issue an error message.  */
22886       error_at (token->location,
22887 		"named return values are no longer supported");
22888       /* Skip tokens until we reach the start of the function body.  */
22889       while (true)
22890 	{
22891 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
22892 	  if (token->type == CPP_OPEN_BRACE
22893 	      || token->type == CPP_EOF
22894 	      || token->type == CPP_PRAGMA_EOL)
22895 	    break;
22896 	  cp_lexer_consume_token (parser->lexer);
22897 	}
22898     }
22899   /* The `extern' in `extern "C" void f () { ... }' does not apply to
22900      anything declared inside `f'.  */
22901   saved_in_unbraced_linkage_specification_p
22902     = parser->in_unbraced_linkage_specification_p;
22903   parser->in_unbraced_linkage_specification_p = false;
22904   /* Inside the function, surrounding template-parameter-lists do not
22905      apply.  */
22906   saved_num_template_parameter_lists
22907     = parser->num_template_parameter_lists;
22908   parser->num_template_parameter_lists = 0;
22909 
22910   start_lambda_scope (current_function_decl);
22911 
22912   /* If the next token is `try', `__transaction_atomic', or
22913      `__transaction_relaxed`, then we are looking at either function-try-block
22914      or function-transaction-block.  Note that all of these include the
22915      function-body.  */
22916   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
22917     ctor_initializer_p = cp_parser_function_transaction (parser,
22918 	RID_TRANSACTION_ATOMIC);
22919   else if (cp_lexer_next_token_is_keyword (parser->lexer,
22920       RID_TRANSACTION_RELAXED))
22921     ctor_initializer_p = cp_parser_function_transaction (parser,
22922 	RID_TRANSACTION_RELAXED);
22923   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
22924     ctor_initializer_p = cp_parser_function_try_block (parser);
22925   else
22926     ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
22927       (parser, /*in_function_try_block=*/false);
22928 
22929   finish_lambda_scope ();
22930 
22931   /* Finish the function.  */
22932   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
22933 			(inline_p ? 2 : 0));
22934   /* Generate code for it, if necessary.  */
22935   expand_or_defer_fn (fn);
22936   /* Restore the saved values.  */
22937   parser->in_unbraced_linkage_specification_p
22938     = saved_in_unbraced_linkage_specification_p;
22939   parser->num_template_parameter_lists
22940     = saved_num_template_parameter_lists;
22941   parser->in_function_body = saved_in_function_body;
22942 
22943   parser->fully_implicit_function_template_p
22944     = fully_implicit_function_template_p;
22945   parser->implicit_template_parms
22946     = implicit_template_parms;
22947   parser->implicit_template_scope
22948     = implicit_template_scope;
22949 
22950   if (parser->fully_implicit_function_template_p)
22951     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
22952 
22953   return fn;
22954 }
22955 
22956 /* Parse a template-declaration, assuming that the `export' (and
22957    `extern') keywords, if present, has already been scanned.  MEMBER_P
22958    is as for cp_parser_template_declaration.  */
22959 
22960 static void
cp_parser_template_declaration_after_export(cp_parser * parser,bool member_p)22961 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
22962 {
22963   tree decl = NULL_TREE;
22964   vec<deferred_access_check, va_gc> *checks;
22965   tree parameter_list;
22966   bool friend_p = false;
22967   bool need_lang_pop;
22968   cp_token *token;
22969 
22970   /* Look for the `template' keyword.  */
22971   token = cp_lexer_peek_token (parser->lexer);
22972   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
22973     return;
22974 
22975   /* And the `<'.  */
22976   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
22977     return;
22978   if (at_class_scope_p () && current_function_decl)
22979     {
22980       /* 14.5.2.2 [temp.mem]
22981 
22982          A local class shall not have member templates.  */
22983       error_at (token->location,
22984 		"invalid declaration of member template in local class");
22985       cp_parser_skip_to_end_of_block_or_statement (parser);
22986       return;
22987     }
22988   /* [temp]
22989 
22990      A template ... shall not have C linkage.  */
22991   if (current_lang_name == lang_name_c)
22992     {
22993       error_at (token->location, "template with C linkage");
22994       /* Give it C++ linkage to avoid confusing other parts of the
22995 	 front end.  */
22996       push_lang_context (lang_name_cplusplus);
22997       need_lang_pop = true;
22998     }
22999   else
23000     need_lang_pop = false;
23001 
23002   /* We cannot perform access checks on the template parameter
23003      declarations until we know what is being declared, just as we
23004      cannot check the decl-specifier list.  */
23005   push_deferring_access_checks (dk_deferred);
23006 
23007   /* If the next token is `>', then we have an invalid
23008      specialization.  Rather than complain about an invalid template
23009      parameter, issue an error message here.  */
23010   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23011     {
23012       cp_parser_error (parser, "invalid explicit specialization");
23013       begin_specialization ();
23014       parameter_list = NULL_TREE;
23015     }
23016   else
23017     {
23018       /* Parse the template parameters.  */
23019       parameter_list = cp_parser_template_parameter_list (parser);
23020     }
23021 
23022   /* Get the deferred access checks from the parameter list.  These
23023      will be checked once we know what is being declared, as for a
23024      member template the checks must be performed in the scope of the
23025      class containing the member.  */
23026   checks = get_deferred_access_checks ();
23027 
23028   /* Look for the `>'.  */
23029   cp_parser_skip_to_end_of_template_parameter_list (parser);
23030   /* We just processed one more parameter list.  */
23031   ++parser->num_template_parameter_lists;
23032   /* If the next token is `template', there are more template
23033      parameters.  */
23034   if (cp_lexer_next_token_is_keyword (parser->lexer,
23035 				      RID_TEMPLATE))
23036     cp_parser_template_declaration_after_export (parser, member_p);
23037   else if (cxx_dialect >= cxx11
23038 	   && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23039     decl = cp_parser_alias_declaration (parser);
23040   else
23041     {
23042       /* There are no access checks when parsing a template, as we do not
23043 	 know if a specialization will be a friend.  */
23044       push_deferring_access_checks (dk_no_check);
23045       token = cp_lexer_peek_token (parser->lexer);
23046       decl = cp_parser_single_declaration (parser,
23047 					   checks,
23048 					   member_p,
23049                                            /*explicit_specialization_p=*/false,
23050 					   &friend_p);
23051       pop_deferring_access_checks ();
23052 
23053       /* If this is a member template declaration, let the front
23054 	 end know.  */
23055       if (member_p && !friend_p && decl)
23056 	{
23057 	  if (TREE_CODE (decl) == TYPE_DECL)
23058 	    cp_parser_check_access_in_redeclaration (decl, token->location);
23059 
23060 	  decl = finish_member_template_decl (decl);
23061 	}
23062       else if (friend_p && decl
23063 	       && DECL_DECLARES_TYPE_P (decl))
23064 	make_friend_class (current_class_type, TREE_TYPE (decl),
23065 			   /*complain=*/true);
23066     }
23067   /* We are done with the current parameter list.  */
23068   --parser->num_template_parameter_lists;
23069 
23070   pop_deferring_access_checks ();
23071 
23072   /* Finish up.  */
23073   finish_template_decl (parameter_list);
23074 
23075   /* Check the template arguments for a literal operator template.  */
23076   if (decl
23077       && DECL_DECLARES_FUNCTION_P (decl)
23078       && UDLIT_OPER_P (DECL_NAME (decl)))
23079     {
23080       bool ok = true;
23081       if (parameter_list == NULL_TREE)
23082 	ok = false;
23083       else
23084 	{
23085 	  int num_parms = TREE_VEC_LENGTH (parameter_list);
23086 	  if (num_parms == 1)
23087 	    {
23088 	      tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23089 	      tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23090 	      if (TREE_TYPE (parm) != char_type_node
23091 		  || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23092 		ok = false;
23093 	    }
23094 	  else if (num_parms == 2 && cxx_dialect >= cxx1y)
23095 	    {
23096 	      tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23097 	      tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23098 	      tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23099 	      tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23100 	      if (TREE_TYPE (parm) != TREE_TYPE (type)
23101 		  || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23102 		ok = false;
23103 	    }
23104 	  else
23105 	    ok = false;
23106 	}
23107       if (!ok)
23108 	{
23109 	  if (cxx_dialect >= cxx1y)
23110 	    error ("literal operator template %qD has invalid parameter list."
23111 		   "  Expected non-type template argument pack <char...>"
23112 		   " or <typename CharT, CharT...>",
23113 		   decl);
23114 	  else
23115 	    error ("literal operator template %qD has invalid parameter list."
23116 		   "  Expected non-type template argument pack <char...>",
23117 		   decl);
23118 	}
23119     }
23120   /* Register member declarations.  */
23121   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23122     finish_member_declaration (decl);
23123   /* For the erroneous case of a template with C linkage, we pushed an
23124      implicit C++ linkage scope; exit that scope now.  */
23125   if (need_lang_pop)
23126     pop_lang_context ();
23127   /* If DECL is a function template, we must return to parse it later.
23128      (Even though there is no definition, there might be default
23129      arguments that need handling.)  */
23130   if (member_p && decl
23131       && DECL_DECLARES_FUNCTION_P (decl))
23132     vec_safe_push (unparsed_funs_with_definitions, decl);
23133 }
23134 
23135 /* Perform the deferred access checks from a template-parameter-list.
23136    CHECKS is a TREE_LIST of access checks, as returned by
23137    get_deferred_access_checks.  */
23138 
23139 static void
cp_parser_perform_template_parameter_access_checks(vec<deferred_access_check,va_gc> * checks)23140 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23141 {
23142   ++processing_template_parmlist;
23143   perform_access_checks (checks, tf_warning_or_error);
23144   --processing_template_parmlist;
23145 }
23146 
23147 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23148    `function-definition' sequence that follows a template header.
23149    If MEMBER_P is true, this declaration appears in a class scope.
23150 
23151    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
23152    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
23153 
23154 static tree
cp_parser_single_declaration(cp_parser * parser,vec<deferred_access_check,va_gc> * checks,bool member_p,bool explicit_specialization_p,bool * friend_p)23155 cp_parser_single_declaration (cp_parser* parser,
23156 			      vec<deferred_access_check, va_gc> *checks,
23157 			      bool member_p,
23158                               bool explicit_specialization_p,
23159 			      bool* friend_p)
23160 {
23161   int declares_class_or_enum;
23162   tree decl = NULL_TREE;
23163   cp_decl_specifier_seq decl_specifiers;
23164   bool function_definition_p = false;
23165   cp_token *decl_spec_token_start;
23166 
23167   /* This function is only used when processing a template
23168      declaration.  */
23169   gcc_assert (innermost_scope_kind () == sk_template_parms
23170 	      || innermost_scope_kind () == sk_template_spec);
23171 
23172   /* Defer access checks until we know what is being declared.  */
23173   push_deferring_access_checks (dk_deferred);
23174 
23175   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23176      alternative.  */
23177   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23178   cp_parser_decl_specifier_seq (parser,
23179 				CP_PARSER_FLAGS_OPTIONAL,
23180 				&decl_specifiers,
23181 				&declares_class_or_enum);
23182   if (friend_p)
23183     *friend_p = cp_parser_friend_p (&decl_specifiers);
23184 
23185   /* There are no template typedefs.  */
23186   if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23187     {
23188       error_at (decl_spec_token_start->location,
23189 		"template declaration of %<typedef%>");
23190       decl = error_mark_node;
23191     }
23192 
23193   /* Gather up the access checks that occurred the
23194      decl-specifier-seq.  */
23195   stop_deferring_access_checks ();
23196 
23197   /* Check for the declaration of a template class.  */
23198   if (declares_class_or_enum)
23199     {
23200       if (cp_parser_declares_only_class_p (parser))
23201 	{
23202 	  decl = shadow_tag (&decl_specifiers);
23203 
23204 	  /* In this case:
23205 
23206 	       struct C {
23207 		 friend template <typename T> struct A<T>::B;
23208 	       };
23209 
23210 	     A<T>::B will be represented by a TYPENAME_TYPE, and
23211 	     therefore not recognized by shadow_tag.  */
23212 	  if (friend_p && *friend_p
23213 	      && !decl
23214 	      && decl_specifiers.type
23215 	      && TYPE_P (decl_specifiers.type))
23216 	    decl = decl_specifiers.type;
23217 
23218 	  if (decl && decl != error_mark_node)
23219 	    decl = TYPE_NAME (decl);
23220 	  else
23221 	    decl = error_mark_node;
23222 
23223 	  /* Perform access checks for template parameters.  */
23224 	  cp_parser_perform_template_parameter_access_checks (checks);
23225 	}
23226     }
23227 
23228   /* Complain about missing 'typename' or other invalid type names.  */
23229   if (!decl_specifiers.any_type_specifiers_p
23230       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23231     {
23232       /* cp_parser_parse_and_diagnose_invalid_type_name calls
23233 	 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23234 	 the rest of this declaration.  */
23235       decl = error_mark_node;
23236       goto out;
23237     }
23238 
23239   /* If it's not a template class, try for a template function.  If
23240      the next token is a `;', then this declaration does not declare
23241      anything.  But, if there were errors in the decl-specifiers, then
23242      the error might well have come from an attempted class-specifier.
23243      In that case, there's no need to warn about a missing declarator.  */
23244   if (!decl
23245       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23246 	  || decl_specifiers.type != error_mark_node))
23247     {
23248       decl = cp_parser_init_declarator (parser,
23249 				        &decl_specifiers,
23250 				        checks,
23251 				        /*function_definition_allowed_p=*/true,
23252 				        member_p,
23253 				        declares_class_or_enum,
23254 				        &function_definition_p,
23255 					NULL);
23256 
23257     /* 7.1.1-1 [dcl.stc]
23258 
23259        A storage-class-specifier shall not be specified in an explicit
23260        specialization...  */
23261     if (decl
23262         && explicit_specialization_p
23263         && decl_specifiers.storage_class != sc_none)
23264       {
23265         error_at (decl_spec_token_start->location,
23266 		  "explicit template specialization cannot have a storage class");
23267         decl = error_mark_node;
23268       }
23269 
23270     if (decl && VAR_P (decl))
23271       check_template_variable (decl);
23272     }
23273 
23274   /* Look for a trailing `;' after the declaration.  */
23275   if (!function_definition_p
23276       && (decl == error_mark_node
23277 	  || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23278     cp_parser_skip_to_end_of_block_or_statement (parser);
23279 
23280  out:
23281   pop_deferring_access_checks ();
23282 
23283   /* Clear any current qualification; whatever comes next is the start
23284      of something new.  */
23285   parser->scope = NULL_TREE;
23286   parser->qualifying_scope = NULL_TREE;
23287   parser->object_scope = NULL_TREE;
23288 
23289   return decl;
23290 }
23291 
23292 /* Parse a cast-expression that is not the operand of a unary "&".  */
23293 
23294 static tree
cp_parser_simple_cast_expression(cp_parser * parser)23295 cp_parser_simple_cast_expression (cp_parser *parser)
23296 {
23297   return cp_parser_cast_expression (parser, /*address_p=*/false,
23298 				    /*cast_p=*/false, /*decltype*/false, NULL);
23299 }
23300 
23301 /* Parse a functional cast to TYPE.  Returns an expression
23302    representing the cast.  */
23303 
23304 static tree
cp_parser_functional_cast(cp_parser * parser,tree type)23305 cp_parser_functional_cast (cp_parser* parser, tree type)
23306 {
23307   vec<tree, va_gc> *vec;
23308   tree expression_list;
23309   tree cast;
23310   bool nonconst_p;
23311 
23312   if (!type)
23313     type = error_mark_node;
23314 
23315   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23316     {
23317       cp_lexer_set_source_position (parser->lexer);
23318       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23319       expression_list = cp_parser_braced_list (parser, &nonconst_p);
23320       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23321       if (TREE_CODE (type) == TYPE_DECL)
23322 	type = TREE_TYPE (type);
23323       return finish_compound_literal (type, expression_list,
23324 				      tf_warning_or_error);
23325     }
23326 
23327 
23328   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23329 						 /*cast_p=*/true,
23330 						 /*allow_expansion_p=*/true,
23331 						 /*non_constant_p=*/NULL);
23332   if (vec == NULL)
23333     expression_list = error_mark_node;
23334   else
23335     {
23336       expression_list = build_tree_list_vec (vec);
23337       release_tree_vector (vec);
23338     }
23339 
23340   cast = build_functional_cast (type, expression_list,
23341                                 tf_warning_or_error);
23342   /* [expr.const]/1: In an integral constant expression "only type
23343      conversions to integral or enumeration type can be used".  */
23344   if (TREE_CODE (type) == TYPE_DECL)
23345     type = TREE_TYPE (type);
23346   if (cast != error_mark_node
23347       && !cast_valid_in_integral_constant_expression_p (type)
23348       && cp_parser_non_integral_constant_expression (parser,
23349 						     NIC_CONSTRUCTOR))
23350     return error_mark_node;
23351   return cast;
23352 }
23353 
23354 /* Save the tokens that make up the body of a member function defined
23355    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
23356    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
23357    specifiers applied to the declaration.  Returns the FUNCTION_DECL
23358    for the member function.  */
23359 
23360 static tree
cp_parser_save_member_function_body(cp_parser * parser,cp_decl_specifier_seq * decl_specifiers,cp_declarator * declarator,tree attributes)23361 cp_parser_save_member_function_body (cp_parser* parser,
23362 				     cp_decl_specifier_seq *decl_specifiers,
23363 				     cp_declarator *declarator,
23364 				     tree attributes)
23365 {
23366   cp_token *first;
23367   cp_token *last;
23368   tree fn;
23369 
23370   /* Create the FUNCTION_DECL.  */
23371   fn = grokmethod (decl_specifiers, declarator, attributes);
23372   cp_finalize_omp_declare_simd (parser, fn);
23373   /* If something went badly wrong, bail out now.  */
23374   if (fn == error_mark_node)
23375     {
23376       /* If there's a function-body, skip it.  */
23377       if (cp_parser_token_starts_function_definition_p
23378 	  (cp_lexer_peek_token (parser->lexer)))
23379 	cp_parser_skip_to_end_of_block_or_statement (parser);
23380       return error_mark_node;
23381     }
23382 
23383   /* Remember it, if there default args to post process.  */
23384   cp_parser_save_default_args (parser, fn);
23385 
23386   /* Save away the tokens that make up the body of the
23387      function.  */
23388   first = parser->lexer->next_token;
23389   /* Handle function try blocks.  */
23390   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23391     cp_lexer_consume_token (parser->lexer);
23392   /* We can have braced-init-list mem-initializers before the fn body.  */
23393   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23394     {
23395       cp_lexer_consume_token (parser->lexer);
23396       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23397 	{
23398 	  /* cache_group will stop after an un-nested { } pair, too.  */
23399 	  if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23400 	    break;
23401 
23402 	  /* variadic mem-inits have ... after the ')'.  */
23403 	  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23404 	    cp_lexer_consume_token (parser->lexer);
23405 	}
23406     }
23407   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23408   /* Handle function try blocks.  */
23409   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23410     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23411   last = parser->lexer->next_token;
23412 
23413   /* Save away the inline definition; we will process it when the
23414      class is complete.  */
23415   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23416   DECL_PENDING_INLINE_P (fn) = 1;
23417 
23418   /* We need to know that this was defined in the class, so that
23419      friend templates are handled correctly.  */
23420   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23421 
23422   /* Add FN to the queue of functions to be parsed later.  */
23423   vec_safe_push (unparsed_funs_with_definitions, fn);
23424 
23425   return fn;
23426 }
23427 
23428 /* Save the tokens that make up the in-class initializer for a non-static
23429    data member.  Returns a DEFAULT_ARG.  */
23430 
23431 static tree
cp_parser_save_nsdmi(cp_parser * parser)23432 cp_parser_save_nsdmi (cp_parser* parser)
23433 {
23434   return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23435 }
23436 
23437 /* Parse a template-argument-list, as well as the trailing ">" (but
23438    not the opening "<").  See cp_parser_template_argument_list for the
23439    return value.  */
23440 
23441 static tree
cp_parser_enclosed_template_argument_list(cp_parser * parser)23442 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23443 {
23444   tree arguments;
23445   tree saved_scope;
23446   tree saved_qualifying_scope;
23447   tree saved_object_scope;
23448   bool saved_greater_than_is_operator_p;
23449   int saved_unevaluated_operand;
23450   int saved_inhibit_evaluation_warnings;
23451 
23452   /* [temp.names]
23453 
23454      When parsing a template-id, the first non-nested `>' is taken as
23455      the end of the template-argument-list rather than a greater-than
23456      operator.  */
23457   saved_greater_than_is_operator_p
23458     = parser->greater_than_is_operator_p;
23459   parser->greater_than_is_operator_p = false;
23460   /* Parsing the argument list may modify SCOPE, so we save it
23461      here.  */
23462   saved_scope = parser->scope;
23463   saved_qualifying_scope = parser->qualifying_scope;
23464   saved_object_scope = parser->object_scope;
23465   /* We need to evaluate the template arguments, even though this
23466      template-id may be nested within a "sizeof".  */
23467   saved_unevaluated_operand = cp_unevaluated_operand;
23468   cp_unevaluated_operand = 0;
23469   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23470   c_inhibit_evaluation_warnings = 0;
23471   /* Parse the template-argument-list itself.  */
23472   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23473       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23474     arguments = NULL_TREE;
23475   else
23476     arguments = cp_parser_template_argument_list (parser);
23477   /* Look for the `>' that ends the template-argument-list. If we find
23478      a '>>' instead, it's probably just a typo.  */
23479   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23480     {
23481       if (cxx_dialect != cxx98)
23482         {
23483           /* In C++0x, a `>>' in a template argument list or cast
23484              expression is considered to be two separate `>'
23485              tokens. So, change the current token to a `>', but don't
23486              consume it: it will be consumed later when the outer
23487              template argument list (or cast expression) is parsed.
23488              Note that this replacement of `>' for `>>' is necessary
23489              even if we are parsing tentatively: in the tentative
23490              case, after calling
23491              cp_parser_enclosed_template_argument_list we will always
23492              throw away all of the template arguments and the first
23493              closing `>', either because the template argument list
23494              was erroneous or because we are replacing those tokens
23495              with a CPP_TEMPLATE_ID token.  The second `>' (which will
23496              not have been thrown away) is needed either to close an
23497              outer template argument list or to complete a new-style
23498              cast.  */
23499 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
23500           token->type = CPP_GREATER;
23501         }
23502       else if (!saved_greater_than_is_operator_p)
23503 	{
23504 	  /* If we're in a nested template argument list, the '>>' has
23505 	    to be a typo for '> >'. We emit the error message, but we
23506 	    continue parsing and we push a '>' as next token, so that
23507 	    the argument list will be parsed correctly.  Note that the
23508 	    global source location is still on the token before the
23509 	    '>>', so we need to say explicitly where we want it.  */
23510 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
23511 	  error_at (token->location, "%<>>%> should be %<> >%> "
23512 		    "within a nested template argument list");
23513 
23514 	  token->type = CPP_GREATER;
23515 	}
23516       else
23517 	{
23518 	  /* If this is not a nested template argument list, the '>>'
23519 	    is a typo for '>'. Emit an error message and continue.
23520 	    Same deal about the token location, but here we can get it
23521 	    right by consuming the '>>' before issuing the diagnostic.  */
23522 	  cp_token *token = cp_lexer_consume_token (parser->lexer);
23523 	  error_at (token->location,
23524 		    "spurious %<>>%>, use %<>%> to terminate "
23525 		    "a template argument list");
23526 	}
23527     }
23528   else
23529     cp_parser_skip_to_end_of_template_parameter_list (parser);
23530   /* The `>' token might be a greater-than operator again now.  */
23531   parser->greater_than_is_operator_p
23532     = saved_greater_than_is_operator_p;
23533   /* Restore the SAVED_SCOPE.  */
23534   parser->scope = saved_scope;
23535   parser->qualifying_scope = saved_qualifying_scope;
23536   parser->object_scope = saved_object_scope;
23537   cp_unevaluated_operand = saved_unevaluated_operand;
23538   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23539 
23540   return arguments;
23541 }
23542 
23543 /* MEMBER_FUNCTION is a member function, or a friend.  If default
23544    arguments, or the body of the function have not yet been parsed,
23545    parse them now.  */
23546 
23547 static void
cp_parser_late_parsing_for_member(cp_parser * parser,tree member_function)23548 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23549 {
23550   timevar_push (TV_PARSE_INMETH);
23551   /* If this member is a template, get the underlying
23552      FUNCTION_DECL.  */
23553   if (DECL_FUNCTION_TEMPLATE_P (member_function))
23554     member_function = DECL_TEMPLATE_RESULT (member_function);
23555 
23556   /* There should not be any class definitions in progress at this
23557      point; the bodies of members are only parsed outside of all class
23558      definitions.  */
23559   gcc_assert (parser->num_classes_being_defined == 0);
23560   /* While we're parsing the member functions we might encounter more
23561      classes.  We want to handle them right away, but we don't want
23562      them getting mixed up with functions that are currently in the
23563      queue.  */
23564   push_unparsed_function_queues (parser);
23565 
23566   /* Make sure that any template parameters are in scope.  */
23567   maybe_begin_member_template_processing (member_function);
23568 
23569   /* If the body of the function has not yet been parsed, parse it
23570      now.  */
23571   if (DECL_PENDING_INLINE_P (member_function))
23572     {
23573       tree function_scope;
23574       cp_token_cache *tokens;
23575 
23576       /* The function is no longer pending; we are processing it.  */
23577       tokens = DECL_PENDING_INLINE_INFO (member_function);
23578       DECL_PENDING_INLINE_INFO (member_function) = NULL;
23579       DECL_PENDING_INLINE_P (member_function) = 0;
23580 
23581       /* If this is a local class, enter the scope of the containing
23582 	 function.  */
23583       function_scope = current_function_decl;
23584       if (function_scope)
23585 	push_function_context ();
23586 
23587       /* Push the body of the function onto the lexer stack.  */
23588       cp_parser_push_lexer_for_tokens (parser, tokens);
23589 
23590       /* Let the front end know that we going to be defining this
23591 	 function.  */
23592       start_preparsed_function (member_function, NULL_TREE,
23593 				SF_PRE_PARSED | SF_INCLASS_INLINE);
23594 
23595       /* Don't do access checking if it is a templated function.  */
23596       if (processing_template_decl)
23597 	push_deferring_access_checks (dk_no_check);
23598 
23599       /* #pragma omp declare reduction needs special parsing.  */
23600       if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
23601 	{
23602 	  parser->lexer->in_pragma = true;
23603 	  cp_parser_omp_declare_reduction_exprs (member_function, parser);
23604 	  finish_function (/*inline*/2);
23605 	  cp_check_omp_declare_reduction (member_function);
23606 	}
23607       else
23608 	/* Now, parse the body of the function.  */
23609 	cp_parser_function_definition_after_declarator (parser,
23610 							/*inline_p=*/true);
23611 
23612       if (processing_template_decl)
23613 	pop_deferring_access_checks ();
23614 
23615       /* Leave the scope of the containing function.  */
23616       if (function_scope)
23617 	pop_function_context ();
23618       cp_parser_pop_lexer (parser);
23619     }
23620 
23621   /* Remove any template parameters from the symbol table.  */
23622   maybe_end_member_template_processing ();
23623 
23624   /* Restore the queue.  */
23625   pop_unparsed_function_queues (parser);
23626   timevar_pop (TV_PARSE_INMETH);
23627 }
23628 
23629 /* If DECL contains any default args, remember it on the unparsed
23630    functions queue.  */
23631 
23632 static void
cp_parser_save_default_args(cp_parser * parser,tree decl)23633 cp_parser_save_default_args (cp_parser* parser, tree decl)
23634 {
23635   tree probe;
23636 
23637   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
23638        probe;
23639        probe = TREE_CHAIN (probe))
23640     if (TREE_PURPOSE (probe))
23641       {
23642 	cp_default_arg_entry entry = {current_class_type, decl};
23643 	vec_safe_push (unparsed_funs_with_default_args, entry);
23644 	break;
23645       }
23646 }
23647 
23648 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23649    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
23650    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
23651    from the parameter-type-list.  */
23652 
23653 static tree
cp_parser_late_parse_one_default_arg(cp_parser * parser,tree decl,tree default_arg,tree parmtype)23654 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
23655 				      tree default_arg, tree parmtype)
23656 {
23657   cp_token_cache *tokens;
23658   tree parsed_arg;
23659   bool dummy;
23660 
23661   if (default_arg == error_mark_node)
23662     return error_mark_node;
23663 
23664   /* Push the saved tokens for the default argument onto the parser's
23665      lexer stack.  */
23666   tokens = DEFARG_TOKENS (default_arg);
23667   cp_parser_push_lexer_for_tokens (parser, tokens);
23668 
23669   start_lambda_scope (decl);
23670 
23671   /* Parse the default argument.  */
23672   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
23673   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
23674     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23675 
23676   finish_lambda_scope ();
23677 
23678   if (parsed_arg == error_mark_node)
23679     cp_parser_skip_to_end_of_statement (parser);
23680 
23681   if (!processing_template_decl)
23682     {
23683       /* In a non-template class, check conversions now.  In a template,
23684 	 we'll wait and instantiate these as needed.  */
23685       if (TREE_CODE (decl) == PARM_DECL)
23686 	parsed_arg = check_default_argument (parmtype, parsed_arg,
23687 					     tf_warning_or_error);
23688       else
23689 	parsed_arg = digest_nsdmi_init (decl, parsed_arg);
23690     }
23691 
23692   /* If the token stream has not been completely used up, then
23693      there was extra junk after the end of the default
23694      argument.  */
23695   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23696     {
23697       if (TREE_CODE (decl) == PARM_DECL)
23698 	cp_parser_error (parser, "expected %<,%>");
23699       else
23700 	cp_parser_error (parser, "expected %<;%>");
23701     }
23702 
23703   /* Revert to the main lexer.  */
23704   cp_parser_pop_lexer (parser);
23705 
23706   return parsed_arg;
23707 }
23708 
23709 /* FIELD is a non-static data member with an initializer which we saved for
23710    later; parse it now.  */
23711 
23712 static void
cp_parser_late_parsing_nsdmi(cp_parser * parser,tree field)23713 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
23714 {
23715   tree def;
23716 
23717   maybe_begin_member_template_processing (field);
23718 
23719   push_unparsed_function_queues (parser);
23720   def = cp_parser_late_parse_one_default_arg (parser, field,
23721 					      DECL_INITIAL (field),
23722 					      NULL_TREE);
23723   pop_unparsed_function_queues (parser);
23724 
23725   maybe_end_member_template_processing ();
23726 
23727   DECL_INITIAL (field) = def;
23728 }
23729 
23730 /* FN is a FUNCTION_DECL which may contains a parameter with an
23731    unparsed DEFAULT_ARG.  Parse the default args now.  This function
23732    assumes that the current scope is the scope in which the default
23733    argument should be processed.  */
23734 
23735 static void
cp_parser_late_parsing_default_args(cp_parser * parser,tree fn)23736 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
23737 {
23738   bool saved_local_variables_forbidden_p;
23739   tree parm, parmdecl;
23740 
23741   /* While we're parsing the default args, we might (due to the
23742      statement expression extension) encounter more classes.  We want
23743      to handle them right away, but we don't want them getting mixed
23744      up with default args that are currently in the queue.  */
23745   push_unparsed_function_queues (parser);
23746 
23747   /* Local variable names (and the `this' keyword) may not appear
23748      in a default argument.  */
23749   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23750   parser->local_variables_forbidden_p = true;
23751 
23752   push_defarg_context (fn);
23753 
23754   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
23755 	 parmdecl = DECL_ARGUMENTS (fn);
23756        parm && parm != void_list_node;
23757        parm = TREE_CHAIN (parm),
23758 	 parmdecl = DECL_CHAIN (parmdecl))
23759     {
23760       tree default_arg = TREE_PURPOSE (parm);
23761       tree parsed_arg;
23762       vec<tree, va_gc> *insts;
23763       tree copy;
23764       unsigned ix;
23765 
23766       if (!default_arg)
23767 	continue;
23768 
23769       if (TREE_CODE (default_arg) != DEFAULT_ARG)
23770 	/* This can happen for a friend declaration for a function
23771 	   already declared with default arguments.  */
23772 	continue;
23773 
23774       parsed_arg
23775 	= cp_parser_late_parse_one_default_arg (parser, parmdecl,
23776 						default_arg,
23777 						TREE_VALUE (parm));
23778       if (parsed_arg == error_mark_node)
23779 	{
23780 	  continue;
23781 	}
23782 
23783       TREE_PURPOSE (parm) = parsed_arg;
23784 
23785       /* Update any instantiations we've already created.  */
23786       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
23787 	   vec_safe_iterate (insts, ix, &copy); ix++)
23788 	TREE_PURPOSE (copy) = parsed_arg;
23789     }
23790 
23791   pop_defarg_context ();
23792 
23793   /* Make sure no default arg is missing.  */
23794   check_default_args (fn);
23795 
23796   /* Restore the state of local_variables_forbidden_p.  */
23797   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
23798 
23799   /* Restore the queue.  */
23800   pop_unparsed_function_queues (parser);
23801 }
23802 
23803 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
23804 
23805      sizeof ... ( identifier )
23806 
23807    where the 'sizeof' token has already been consumed.  */
23808 
23809 static tree
cp_parser_sizeof_pack(cp_parser * parser)23810 cp_parser_sizeof_pack (cp_parser *parser)
23811 {
23812   /* Consume the `...'.  */
23813   cp_lexer_consume_token (parser->lexer);
23814   maybe_warn_variadic_templates ();
23815 
23816   bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
23817   if (paren)
23818     cp_lexer_consume_token (parser->lexer);
23819   else
23820     permerror (cp_lexer_peek_token (parser->lexer)->location,
23821 	       "%<sizeof...%> argument must be surrounded by parentheses");
23822 
23823   cp_token *token = cp_lexer_peek_token (parser->lexer);
23824   tree name = cp_parser_identifier (parser);
23825   if (name == error_mark_node)
23826     return error_mark_node;
23827   /* The name is not qualified.  */
23828   parser->scope = NULL_TREE;
23829   parser->qualifying_scope = NULL_TREE;
23830   parser->object_scope = NULL_TREE;
23831   tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
23832   if (expr == error_mark_node)
23833     cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
23834 				 token->location);
23835   if (TREE_CODE (expr) == TYPE_DECL)
23836     expr = TREE_TYPE (expr);
23837   else if (TREE_CODE (expr) == CONST_DECL)
23838     expr = DECL_INITIAL (expr);
23839   expr = make_pack_expansion (expr);
23840 
23841   if (paren)
23842     cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23843 
23844   return expr;
23845 }
23846 
23847 /* Parse the operand of `sizeof' (or a similar operator).  Returns
23848    either a TYPE or an expression, depending on the form of the
23849    input.  The KEYWORD indicates which kind of expression we have
23850    encountered.  */
23851 
23852 static tree
cp_parser_sizeof_operand(cp_parser * parser,enum rid keyword)23853 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
23854 {
23855   tree expr = NULL_TREE;
23856   const char *saved_message;
23857   char *tmp;
23858   bool saved_integral_constant_expression_p;
23859   bool saved_non_integral_constant_expression_p;
23860 
23861   /* If it's a `...', then we are computing the length of a parameter
23862      pack.  */
23863   if (keyword == RID_SIZEOF
23864       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23865     return cp_parser_sizeof_pack (parser);
23866 
23867   /* Types cannot be defined in a `sizeof' expression.  Save away the
23868      old message.  */
23869   saved_message = parser->type_definition_forbidden_message;
23870   /* And create the new one.  */
23871   tmp = concat ("types may not be defined in %<",
23872 		IDENTIFIER_POINTER (ridpointers[keyword]),
23873 		"%> expressions", NULL);
23874   parser->type_definition_forbidden_message = tmp;
23875 
23876   /* The restrictions on constant-expressions do not apply inside
23877      sizeof expressions.  */
23878   saved_integral_constant_expression_p
23879     = parser->integral_constant_expression_p;
23880   saved_non_integral_constant_expression_p
23881     = parser->non_integral_constant_expression_p;
23882   parser->integral_constant_expression_p = false;
23883 
23884   /* Do not actually evaluate the expression.  */
23885   ++cp_unevaluated_operand;
23886   ++c_inhibit_evaluation_warnings;
23887   /* If it's a `(', then we might be looking at the type-id
23888      construction.  */
23889   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23890     {
23891       tree type = NULL_TREE;
23892       bool compound_literal_p;
23893 
23894       /* We can't be sure yet whether we're looking at a type-id or an
23895 	 expression.  */
23896       cp_parser_parse_tentatively (parser);
23897       /* Consume the `('.  */
23898       cp_lexer_consume_token (parser->lexer);
23899       /* Note: as a GNU Extension, compound literals are considered
23900 	 postfix-expressions as they are in C99, so they are valid
23901 	 arguments to sizeof.  See comment in cp_parser_cast_expression
23902 	 for details.  */
23903       cp_lexer_save_tokens (parser->lexer);
23904       /* Skip tokens until the next token is a closing parenthesis.
23905 	 If we find the closing `)', and the next token is a `{', then
23906 	 we are looking at a compound-literal.  */
23907       compound_literal_p
23908 	= (cp_parser_skip_to_closing_parenthesis (parser, false, false,
23909 						  /*consume_paren=*/true)
23910 	   && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
23911       /* Roll back the tokens we skipped.  */
23912       cp_lexer_rollback_tokens (parser->lexer);
23913       /* If we were looking at a compound-literal, simulate an error
23914 	 so that the call to cp_parser_parse_definitely below will
23915 	 fail.  */
23916       if (compound_literal_p)
23917 	cp_parser_simulate_error (parser);
23918       else
23919 	{
23920 	  bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23921 	  parser->in_type_id_in_expr_p = true;
23922 	  /* Look for the type-id.  */
23923 	  type = cp_parser_type_id (parser);
23924 	  /* Look for the closing `)'.  */
23925 	  cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23926 	  parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23927 	}
23928 
23929       /* If all went well, then we're done.  */
23930       if (cp_parser_parse_definitely (parser))
23931 	{
23932 	  cp_decl_specifier_seq decl_specs;
23933 
23934 	  /* Build a trivial decl-specifier-seq.  */
23935 	  clear_decl_specs (&decl_specs);
23936 	  decl_specs.type = type;
23937 
23938 	  /* Call grokdeclarator to figure out what type this is.  */
23939 	  expr = grokdeclarator (NULL,
23940 				 &decl_specs,
23941 				 TYPENAME,
23942 				 /*initialized=*/0,
23943 				 /*attrlist=*/NULL);
23944 	}
23945     }
23946 
23947   /* If the type-id production did not work out, then we must be
23948      looking at the unary-expression production.  */
23949   if (!expr)
23950     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
23951 				       /*cast_p=*/false, NULL);
23952 
23953   /* Go back to evaluating expressions.  */
23954   --cp_unevaluated_operand;
23955   --c_inhibit_evaluation_warnings;
23956 
23957   /* Free the message we created.  */
23958   free (tmp);
23959   /* And restore the old one.  */
23960   parser->type_definition_forbidden_message = saved_message;
23961   parser->integral_constant_expression_p
23962     = saved_integral_constant_expression_p;
23963   parser->non_integral_constant_expression_p
23964     = saved_non_integral_constant_expression_p;
23965 
23966   return expr;
23967 }
23968 
23969 /* If the current declaration has no declarator, return true.  */
23970 
23971 static bool
cp_parser_declares_only_class_p(cp_parser * parser)23972 cp_parser_declares_only_class_p (cp_parser *parser)
23973 {
23974   /* If the next token is a `;' or a `,' then there is no
23975      declarator.  */
23976   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23977 	  || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
23978 }
23979 
23980 /* Update the DECL_SPECS to reflect the storage class indicated by
23981    KEYWORD.  */
23982 
23983 static void
cp_parser_set_storage_class(cp_parser * parser,cp_decl_specifier_seq * decl_specs,enum rid keyword,cp_token * token)23984 cp_parser_set_storage_class (cp_parser *parser,
23985 			     cp_decl_specifier_seq *decl_specs,
23986 			     enum rid keyword,
23987 			     cp_token *token)
23988 {
23989   cp_storage_class storage_class;
23990 
23991   if (parser->in_unbraced_linkage_specification_p)
23992     {
23993       error_at (token->location, "invalid use of %qD in linkage specification",
23994 		ridpointers[keyword]);
23995       return;
23996     }
23997   else if (decl_specs->storage_class != sc_none)
23998     {
23999       decl_specs->conflicting_specifiers_p = true;
24000       return;
24001     }
24002 
24003   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24004       && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24005       && decl_specs->gnu_thread_keyword_p)
24006     {
24007       pedwarn (decl_specs->locations[ds_thread], 0,
24008 		"%<__thread%> before %qD", ridpointers[keyword]);
24009     }
24010 
24011   switch (keyword)
24012     {
24013     case RID_AUTO:
24014       storage_class = sc_auto;
24015       break;
24016     case RID_REGISTER:
24017       storage_class = sc_register;
24018       break;
24019     case RID_STATIC:
24020       storage_class = sc_static;
24021       break;
24022     case RID_EXTERN:
24023       storage_class = sc_extern;
24024       break;
24025     case RID_MUTABLE:
24026       storage_class = sc_mutable;
24027       break;
24028     default:
24029       gcc_unreachable ();
24030     }
24031   decl_specs->storage_class = storage_class;
24032   set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24033 
24034   /* A storage class specifier cannot be applied alongside a typedef
24035      specifier. If there is a typedef specifier present then set
24036      conflicting_specifiers_p which will trigger an error later
24037      on in grokdeclarator. */
24038   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24039     decl_specs->conflicting_specifiers_p = true;
24040 }
24041 
24042 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
24043    is true, the type is a class or enum definition.  */
24044 
24045 static void
cp_parser_set_decl_spec_type(cp_decl_specifier_seq * decl_specs,tree type_spec,cp_token * token,bool type_definition_p)24046 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24047 			      tree type_spec,
24048 			      cp_token *token,
24049 			      bool type_definition_p)
24050 {
24051   decl_specs->any_specifiers_p = true;
24052 
24053   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24054      (with, for example, in "typedef int wchar_t;") we remember that
24055      this is what happened.  In system headers, we ignore these
24056      declarations so that G++ can work with system headers that are not
24057      C++-safe.  */
24058   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24059       && !type_definition_p
24060       && (type_spec == boolean_type_node
24061 	  || type_spec == char16_type_node
24062 	  || type_spec == char32_type_node
24063 	  || type_spec == wchar_type_node)
24064       && (decl_specs->type
24065 	  || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24066 	  || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24067 	  || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24068 	  || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24069     {
24070       decl_specs->redefined_builtin_type = type_spec;
24071       set_and_check_decl_spec_loc (decl_specs,
24072 				   ds_redefined_builtin_type_spec,
24073 				   token);
24074       if (!decl_specs->type)
24075 	{
24076 	  decl_specs->type = type_spec;
24077 	  decl_specs->type_definition_p = false;
24078 	  set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24079 	}
24080     }
24081   else if (decl_specs->type)
24082     decl_specs->multiple_types_p = true;
24083   else
24084     {
24085       decl_specs->type = type_spec;
24086       decl_specs->type_definition_p = type_definition_p;
24087       decl_specs->redefined_builtin_type = NULL_TREE;
24088       set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24089     }
24090 }
24091 
24092 /* True iff TOKEN is the GNU keyword __thread.  */
24093 
24094 static bool
token_is__thread(cp_token * token)24095 token_is__thread (cp_token *token)
24096 {
24097   gcc_assert (token->keyword == RID_THREAD);
24098   return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24099 }
24100 
24101 /* Set the location for a declarator specifier and check if it is
24102    duplicated.
24103 
24104    DECL_SPECS is the sequence of declarator specifiers onto which to
24105    set the location.
24106 
24107    DS is the single declarator specifier to set which location  is to
24108    be set onto the existing sequence of declarators.
24109 
24110    LOCATION is the location for the declarator specifier to
24111    consider.  */
24112 
24113 static void
set_and_check_decl_spec_loc(cp_decl_specifier_seq * decl_specs,cp_decl_spec ds,cp_token * token)24114 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24115 			     cp_decl_spec ds, cp_token *token)
24116 {
24117   gcc_assert (ds < ds_last);
24118 
24119   if (decl_specs == NULL)
24120     return;
24121 
24122   source_location location = token->location;
24123 
24124   if (decl_specs->locations[ds] == 0)
24125     {
24126       decl_specs->locations[ds] = location;
24127       if (ds == ds_thread)
24128 	decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24129     }
24130   else
24131     {
24132       if (ds == ds_long)
24133 	{
24134 	  if (decl_specs->locations[ds_long_long] != 0)
24135 	    error_at (location,
24136 		      "%<long long long%> is too long for GCC");
24137 	  else
24138 	    {
24139 	      decl_specs->locations[ds_long_long] = location;
24140 	      pedwarn_cxx98 (location,
24141 			     OPT_Wlong_long,
24142 			     "ISO C++ 1998 does not support %<long long%>");
24143 	    }
24144 	}
24145       else if (ds == ds_thread)
24146 	{
24147 	  bool gnu = token_is__thread (token);
24148 	  if (gnu != decl_specs->gnu_thread_keyword_p)
24149 	    error_at (location,
24150 		      "both %<__thread%> and %<thread_local%> specified");
24151 	  else
24152 	    error_at (location, "duplicate %qD", token->u.value);
24153 	}
24154       else
24155 	{
24156 	  static const char *const decl_spec_names[] = {
24157 	    "signed",
24158 	    "unsigned",
24159 	    "short",
24160 	    "long",
24161 	    "const",
24162 	    "volatile",
24163 	    "restrict",
24164 	    "inline",
24165 	    "virtual",
24166 	    "explicit",
24167 	    "friend",
24168 	    "typedef",
24169 	    "using",
24170             "constexpr",
24171 	    "__complex"
24172 	  };
24173 	  error_at (location,
24174 		    "duplicate %qs", decl_spec_names[ds]);
24175 	}
24176     }
24177 }
24178 
24179 /* Return true iff the declarator specifier DS is present in the
24180    sequence of declarator specifiers DECL_SPECS.  */
24181 
24182 bool
decl_spec_seq_has_spec_p(const cp_decl_specifier_seq * decl_specs,cp_decl_spec ds)24183 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24184 			  cp_decl_spec ds)
24185 {
24186   gcc_assert (ds < ds_last);
24187 
24188   if (decl_specs == NULL)
24189     return false;
24190 
24191   return decl_specs->locations[ds] != 0;
24192 }
24193 
24194 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24195    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
24196 
24197 static bool
cp_parser_friend_p(const cp_decl_specifier_seq * decl_specifiers)24198 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24199 {
24200   return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24201 }
24202 
24203 /* Issue an error message indicating that TOKEN_DESC was expected.
24204    If KEYWORD is true, it indicated this function is called by
24205    cp_parser_require_keword and the required token can only be
24206    a indicated keyword. */
24207 
24208 static void
cp_parser_required_error(cp_parser * parser,required_token token_desc,bool keyword)24209 cp_parser_required_error (cp_parser *parser,
24210 			  required_token token_desc,
24211 			  bool keyword)
24212 {
24213   switch (token_desc)
24214     {
24215       case RT_NEW:
24216 	cp_parser_error (parser, "expected %<new%>");
24217 	return;
24218       case RT_DELETE:
24219 	cp_parser_error (parser, "expected %<delete%>");
24220 	return;
24221       case RT_RETURN:
24222 	cp_parser_error (parser, "expected %<return%>");
24223 	return;
24224       case RT_WHILE:
24225 	cp_parser_error (parser, "expected %<while%>");
24226 	return;
24227       case RT_EXTERN:
24228 	cp_parser_error (parser, "expected %<extern%>");
24229 	return;
24230       case RT_STATIC_ASSERT:
24231 	cp_parser_error (parser, "expected %<static_assert%>");
24232 	return;
24233       case RT_DECLTYPE:
24234 	cp_parser_error (parser, "expected %<decltype%>");
24235 	return;
24236       case RT_OPERATOR:
24237 	cp_parser_error (parser, "expected %<operator%>");
24238 	return;
24239       case RT_CLASS:
24240 	cp_parser_error (parser, "expected %<class%>");
24241 	return;
24242       case RT_TEMPLATE:
24243 	cp_parser_error (parser, "expected %<template%>");
24244 	return;
24245       case RT_NAMESPACE:
24246 	cp_parser_error (parser, "expected %<namespace%>");
24247 	return;
24248       case RT_USING:
24249 	cp_parser_error (parser, "expected %<using%>");
24250 	return;
24251       case RT_ASM:
24252 	cp_parser_error (parser, "expected %<asm%>");
24253 	return;
24254       case RT_TRY:
24255 	cp_parser_error (parser, "expected %<try%>");
24256 	return;
24257       case RT_CATCH:
24258 	cp_parser_error (parser, "expected %<catch%>");
24259 	return;
24260       case RT_THROW:
24261 	cp_parser_error (parser, "expected %<throw%>");
24262 	return;
24263       case RT_LABEL:
24264 	cp_parser_error (parser, "expected %<__label__%>");
24265 	return;
24266       case RT_AT_TRY:
24267 	cp_parser_error (parser, "expected %<@try%>");
24268 	return;
24269       case RT_AT_SYNCHRONIZED:
24270 	cp_parser_error (parser, "expected %<@synchronized%>");
24271 	return;
24272       case RT_AT_THROW:
24273 	cp_parser_error (parser, "expected %<@throw%>");
24274 	return;
24275       case RT_TRANSACTION_ATOMIC:
24276 	cp_parser_error (parser, "expected %<__transaction_atomic%>");
24277 	return;
24278       case RT_TRANSACTION_RELAXED:
24279 	cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24280 	return;
24281       default:
24282 	break;
24283     }
24284   if (!keyword)
24285     {
24286       switch (token_desc)
24287         {
24288 	  case RT_SEMICOLON:
24289 	    cp_parser_error (parser, "expected %<;%>");
24290 	    return;
24291 	  case RT_OPEN_PAREN:
24292 	    cp_parser_error (parser, "expected %<(%>");
24293 	    return;
24294 	  case RT_CLOSE_BRACE:
24295 	    cp_parser_error (parser, "expected %<}%>");
24296 	    return;
24297 	  case RT_OPEN_BRACE:
24298 	    cp_parser_error (parser, "expected %<{%>");
24299 	    return;
24300 	  case RT_CLOSE_SQUARE:
24301 	    cp_parser_error (parser, "expected %<]%>");
24302 	    return;
24303 	  case RT_OPEN_SQUARE:
24304 	    cp_parser_error (parser, "expected %<[%>");
24305 	    return;
24306 	  case RT_COMMA:
24307 	    cp_parser_error (parser, "expected %<,%>");
24308 	    return;
24309 	  case RT_SCOPE:
24310 	    cp_parser_error (parser, "expected %<::%>");
24311 	    return;
24312 	  case RT_LESS:
24313 	    cp_parser_error (parser, "expected %<<%>");
24314 	    return;
24315 	  case RT_GREATER:
24316 	    cp_parser_error (parser, "expected %<>%>");
24317 	    return;
24318 	  case RT_EQ:
24319 	    cp_parser_error (parser, "expected %<=%>");
24320 	    return;
24321 	  case RT_ELLIPSIS:
24322 	    cp_parser_error (parser, "expected %<...%>");
24323 	    return;
24324 	  case RT_MULT:
24325 	    cp_parser_error (parser, "expected %<*%>");
24326 	    return;
24327 	  case RT_COMPL:
24328 	    cp_parser_error (parser, "expected %<~%>");
24329 	    return;
24330 	  case RT_COLON:
24331 	    cp_parser_error (parser, "expected %<:%>");
24332 	    return;
24333 	  case RT_COLON_SCOPE:
24334 	    cp_parser_error (parser, "expected %<:%> or %<::%>");
24335 	    return;
24336 	  case RT_CLOSE_PAREN:
24337 	    cp_parser_error (parser, "expected %<)%>");
24338 	    return;
24339 	  case RT_COMMA_CLOSE_PAREN:
24340 	    cp_parser_error (parser, "expected %<,%> or %<)%>");
24341 	    return;
24342 	  case RT_PRAGMA_EOL:
24343 	    cp_parser_error (parser, "expected end of line");
24344 	    return;
24345 	  case RT_NAME:
24346 	    cp_parser_error (parser, "expected identifier");
24347 	    return;
24348 	  case RT_SELECT:
24349 	    cp_parser_error (parser, "expected selection-statement");
24350 	    return;
24351 	  case RT_INTERATION:
24352 	    cp_parser_error (parser, "expected iteration-statement");
24353 	    return;
24354 	  case RT_JUMP:
24355 	    cp_parser_error (parser, "expected jump-statement");
24356 	    return;
24357 	  case RT_CLASS_KEY:
24358 	    cp_parser_error (parser, "expected class-key");
24359 	    return;
24360 	  case RT_CLASS_TYPENAME_TEMPLATE:
24361 	    cp_parser_error (parser,
24362 	  	 "expected %<class%>, %<typename%>, or %<template%>");
24363 	    return;
24364 	  default:
24365 	    gcc_unreachable ();
24366 	}
24367     }
24368   else
24369     gcc_unreachable ();
24370 }
24371 
24372 
24373 
24374 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
24375    issue an error message indicating that TOKEN_DESC was expected.
24376 
24377    Returns the token consumed, if the token had the appropriate type.
24378    Otherwise, returns NULL.  */
24379 
24380 static cp_token *
cp_parser_require(cp_parser * parser,enum cpp_ttype type,required_token token_desc)24381 cp_parser_require (cp_parser* parser,
24382 		   enum cpp_ttype type,
24383 		   required_token token_desc)
24384 {
24385   if (cp_lexer_next_token_is (parser->lexer, type))
24386     return cp_lexer_consume_token (parser->lexer);
24387   else
24388     {
24389       /* Output the MESSAGE -- unless we're parsing tentatively.  */
24390       if (!cp_parser_simulate_error (parser))
24391 	cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24392       return NULL;
24393     }
24394 }
24395 
24396 /* An error message is produced if the next token is not '>'.
24397    All further tokens are skipped until the desired token is
24398    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
24399 
24400 static void
cp_parser_skip_to_end_of_template_parameter_list(cp_parser * parser)24401 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24402 {
24403   /* Current level of '< ... >'.  */
24404   unsigned level = 0;
24405   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
24406   unsigned nesting_depth = 0;
24407 
24408   /* Are we ready, yet?  If not, issue error message.  */
24409   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24410     return;
24411 
24412   /* Skip tokens until the desired token is found.  */
24413   while (true)
24414     {
24415       /* Peek at the next token.  */
24416       switch (cp_lexer_peek_token (parser->lexer)->type)
24417 	{
24418 	case CPP_LESS:
24419 	  if (!nesting_depth)
24420 	    ++level;
24421 	  break;
24422 
24423         case CPP_RSHIFT:
24424           if (cxx_dialect == cxx98)
24425             /* C++0x views the `>>' operator as two `>' tokens, but
24426                C++98 does not. */
24427             break;
24428           else if (!nesting_depth && level-- == 0)
24429 	    {
24430               /* We've hit a `>>' where the first `>' closes the
24431                  template argument list, and the second `>' is
24432                  spurious.  Just consume the `>>' and stop; we've
24433                  already produced at least one error.  */
24434 	      cp_lexer_consume_token (parser->lexer);
24435 	      return;
24436 	    }
24437           /* Fall through for C++0x, so we handle the second `>' in
24438              the `>>'.  */
24439 
24440 	case CPP_GREATER:
24441 	  if (!nesting_depth && level-- == 0)
24442 	    {
24443 	      /* We've reached the token we want, consume it and stop.  */
24444 	      cp_lexer_consume_token (parser->lexer);
24445 	      return;
24446 	    }
24447 	  break;
24448 
24449 	case CPP_OPEN_PAREN:
24450 	case CPP_OPEN_SQUARE:
24451 	  ++nesting_depth;
24452 	  break;
24453 
24454 	case CPP_CLOSE_PAREN:
24455 	case CPP_CLOSE_SQUARE:
24456 	  if (nesting_depth-- == 0)
24457 	    return;
24458 	  break;
24459 
24460 	case CPP_EOF:
24461 	case CPP_PRAGMA_EOL:
24462 	case CPP_SEMICOLON:
24463 	case CPP_OPEN_BRACE:
24464 	case CPP_CLOSE_BRACE:
24465 	  /* The '>' was probably forgotten, don't look further.  */
24466 	  return;
24467 
24468 	default:
24469 	  break;
24470 	}
24471 
24472       /* Consume this token.  */
24473       cp_lexer_consume_token (parser->lexer);
24474     }
24475 }
24476 
24477 /* If the next token is the indicated keyword, consume it.  Otherwise,
24478    issue an error message indicating that TOKEN_DESC was expected.
24479 
24480    Returns the token consumed, if the token had the appropriate type.
24481    Otherwise, returns NULL.  */
24482 
24483 static cp_token *
cp_parser_require_keyword(cp_parser * parser,enum rid keyword,required_token token_desc)24484 cp_parser_require_keyword (cp_parser* parser,
24485 			   enum rid keyword,
24486 			   required_token token_desc)
24487 {
24488   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24489 
24490   if (token && token->keyword != keyword)
24491     {
24492       cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24493       return NULL;
24494     }
24495 
24496   return token;
24497 }
24498 
24499 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24500    function-definition.  */
24501 
24502 static bool
cp_parser_token_starts_function_definition_p(cp_token * token)24503 cp_parser_token_starts_function_definition_p (cp_token* token)
24504 {
24505   return (/* An ordinary function-body begins with an `{'.  */
24506 	  token->type == CPP_OPEN_BRACE
24507 	  /* A ctor-initializer begins with a `:'.  */
24508 	  || token->type == CPP_COLON
24509 	  /* A function-try-block begins with `try'.  */
24510 	  || token->keyword == RID_TRY
24511 	  /* A function-transaction-block begins with `__transaction_atomic'
24512 	     or `__transaction_relaxed'.  */
24513 	  || token->keyword == RID_TRANSACTION_ATOMIC
24514 	  || token->keyword == RID_TRANSACTION_RELAXED
24515 	  /* The named return value extension begins with `return'.  */
24516 	  || token->keyword == RID_RETURN);
24517 }
24518 
24519 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24520    definition.  */
24521 
24522 static bool
cp_parser_next_token_starts_class_definition_p(cp_parser * parser)24523 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24524 {
24525   cp_token *token;
24526 
24527   token = cp_lexer_peek_token (parser->lexer);
24528   return (token->type == CPP_OPEN_BRACE
24529 	  || (token->type == CPP_COLON
24530 	      && !parser->colon_doesnt_start_class_def_p));
24531 }
24532 
24533 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24534    C++0x) ending a template-argument.  */
24535 
24536 static bool
cp_parser_next_token_ends_template_argument_p(cp_parser * parser)24537 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24538 {
24539   cp_token *token;
24540 
24541   token = cp_lexer_peek_token (parser->lexer);
24542   return (token->type == CPP_COMMA
24543           || token->type == CPP_GREATER
24544           || token->type == CPP_ELLIPSIS
24545 	  || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24546 }
24547 
24548 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24549    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
24550 
24551 static bool
cp_parser_nth_token_starts_template_argument_list_p(cp_parser * parser,size_t n)24552 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24553 						     size_t n)
24554 {
24555   cp_token *token;
24556 
24557   token = cp_lexer_peek_nth_token (parser->lexer, n);
24558   if (token->type == CPP_LESS)
24559     return true;
24560   /* Check for the sequence `<::' in the original code. It would be lexed as
24561      `[:', where `[' is a digraph, and there is no whitespace before
24562      `:'.  */
24563   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24564     {
24565       cp_token *token2;
24566       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24567       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24568 	return true;
24569     }
24570   return false;
24571 }
24572 
24573 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24574    or none_type otherwise.  */
24575 
24576 static enum tag_types
cp_parser_token_is_class_key(cp_token * token)24577 cp_parser_token_is_class_key (cp_token* token)
24578 {
24579   switch (token->keyword)
24580     {
24581     case RID_CLASS:
24582       return class_type;
24583     case RID_STRUCT:
24584       return record_type;
24585     case RID_UNION:
24586       return union_type;
24587 
24588     default:
24589       return none_type;
24590     }
24591 }
24592 
24593 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
24594 
24595 static void
cp_parser_check_class_key(enum tag_types class_key,tree type)24596 cp_parser_check_class_key (enum tag_types class_key, tree type)
24597 {
24598   if (type == error_mark_node)
24599     return;
24600   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
24601     {
24602       if (permerror (input_location, "%qs tag used in naming %q#T",
24603 		     class_key == union_type ? "union"
24604 		     : class_key == record_type ? "struct" : "class",
24605 		     type))
24606 	inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
24607 		"%q#T was previously declared here", type);
24608     }
24609 }
24610 
24611 /* Issue an error message if DECL is redeclared with different
24612    access than its original declaration [class.access.spec/3].
24613    This applies to nested classes and nested class templates.
24614    [class.mem/1].  */
24615 
24616 static void
cp_parser_check_access_in_redeclaration(tree decl,location_t location)24617 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
24618 {
24619   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
24620     return;
24621 
24622   if ((TREE_PRIVATE (decl)
24623        != (current_access_specifier == access_private_node))
24624       || (TREE_PROTECTED (decl)
24625 	  != (current_access_specifier == access_protected_node)))
24626     error_at (location, "%qD redeclared with different access", decl);
24627 }
24628 
24629 /* Look for the `template' keyword, as a syntactic disambiguator.
24630    Return TRUE iff it is present, in which case it will be
24631    consumed.  */
24632 
24633 static bool
cp_parser_optional_template_keyword(cp_parser * parser)24634 cp_parser_optional_template_keyword (cp_parser *parser)
24635 {
24636   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24637     {
24638       /* In C++98 the `template' keyword can only be used within templates;
24639 	 outside templates the parser can always figure out what is a
24640 	 template and what is not.  In C++11,  per the resolution of DR 468,
24641 	 `template' is allowed in cases where it is not strictly necessary.  */
24642       if (!processing_template_decl
24643 	  && pedantic && cxx_dialect == cxx98)
24644 	{
24645 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
24646 	  pedwarn (token->location, OPT_Wpedantic,
24647 		   "in C++98 %<template%> (as a disambiguator) is only "
24648 		   "allowed within templates");
24649 	  /* If this part of the token stream is rescanned, the same
24650 	     error message would be generated.  So, we purge the token
24651 	     from the stream.  */
24652 	  cp_lexer_purge_token (parser->lexer);
24653 	  return false;
24654 	}
24655       else
24656 	{
24657 	  /* Consume the `template' keyword.  */
24658 	  cp_lexer_consume_token (parser->lexer);
24659 	  return true;
24660 	}
24661     }
24662   return false;
24663 }
24664 
24665 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
24666    set PARSER->SCOPE, and perform other related actions.  */
24667 
24668 static void
cp_parser_pre_parsed_nested_name_specifier(cp_parser * parser)24669 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
24670 {
24671   int i;
24672   struct tree_check *check_value;
24673   deferred_access_check *chk;
24674   vec<deferred_access_check, va_gc> *checks;
24675 
24676   /* Get the stored value.  */
24677   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
24678   /* Perform any access checks that were deferred.  */
24679   checks = check_value->checks;
24680   if (checks)
24681     {
24682       FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
24683 	perform_or_defer_access_check (chk->binfo,
24684 				       chk->decl,
24685 				       chk->diag_decl, tf_warning_or_error);
24686     }
24687   /* Set the scope from the stored value.  */
24688   parser->scope = check_value->value;
24689   parser->qualifying_scope = check_value->qualifying_scope;
24690   parser->object_scope = NULL_TREE;
24691 }
24692 
24693 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
24694    encounter the end of a block before what we were looking for.  */
24695 
24696 static bool
cp_parser_cache_group(cp_parser * parser,enum cpp_ttype end,unsigned depth)24697 cp_parser_cache_group (cp_parser *parser,
24698 		       enum cpp_ttype end,
24699 		       unsigned depth)
24700 {
24701   while (true)
24702     {
24703       cp_token *token = cp_lexer_peek_token (parser->lexer);
24704 
24705       /* Abort a parenthesized expression if we encounter a semicolon.  */
24706       if ((end == CPP_CLOSE_PAREN || depth == 0)
24707 	  && token->type == CPP_SEMICOLON)
24708 	return true;
24709       /* If we've reached the end of the file, stop.  */
24710       if (token->type == CPP_EOF
24711 	  || (end != CPP_PRAGMA_EOL
24712 	      && token->type == CPP_PRAGMA_EOL))
24713 	return true;
24714       if (token->type == CPP_CLOSE_BRACE && depth == 0)
24715 	/* We've hit the end of an enclosing block, so there's been some
24716 	   kind of syntax error.  */
24717 	return true;
24718 
24719       /* Consume the token.  */
24720       cp_lexer_consume_token (parser->lexer);
24721       /* See if it starts a new group.  */
24722       if (token->type == CPP_OPEN_BRACE)
24723 	{
24724 	  cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
24725 	  /* In theory this should probably check end == '}', but
24726 	     cp_parser_save_member_function_body needs it to exit
24727 	     after either '}' or ')' when called with ')'.  */
24728 	  if (depth == 0)
24729 	    return false;
24730 	}
24731       else if (token->type == CPP_OPEN_PAREN)
24732 	{
24733 	  cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
24734 	  if (depth == 0 && end == CPP_CLOSE_PAREN)
24735 	    return false;
24736 	}
24737       else if (token->type == CPP_PRAGMA)
24738 	cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
24739       else if (token->type == end)
24740 	return false;
24741     }
24742 }
24743 
24744 /* Like above, for caching a default argument or NSDMI.  Both of these are
24745    terminated by a non-nested comma, but it can be unclear whether or not a
24746    comma is nested in a template argument list unless we do more parsing.
24747    In order to handle this ambiguity, when we encounter a ',' after a '<'
24748    we try to parse what follows as a parameter-declaration-list (in the
24749    case of a default argument) or a member-declarator (in the case of an
24750    NSDMI).  If that succeeds, then we stop caching.  */
24751 
24752 static tree
cp_parser_cache_defarg(cp_parser * parser,bool nsdmi)24753 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
24754 {
24755   unsigned depth = 0;
24756   int maybe_template_id = 0;
24757   cp_token *first_token;
24758   cp_token *token;
24759   tree default_argument;
24760 
24761   /* Add tokens until we have processed the entire default
24762      argument.  We add the range [first_token, token).  */
24763   first_token = cp_lexer_peek_token (parser->lexer);
24764   if (first_token->type == CPP_OPEN_BRACE)
24765     {
24766       /* For list-initialization, this is straightforward.  */
24767       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24768       token = cp_lexer_peek_token (parser->lexer);
24769     }
24770   else while (true)
24771     {
24772       bool done = false;
24773 
24774       /* Peek at the next token.  */
24775       token = cp_lexer_peek_token (parser->lexer);
24776       /* What we do depends on what token we have.  */
24777       switch (token->type)
24778 	{
24779 	  /* In valid code, a default argument must be
24780 	     immediately followed by a `,' `)', or `...'.  */
24781 	case CPP_COMMA:
24782 	  if (depth == 0 && maybe_template_id)
24783 	    {
24784 	      /* If we've seen a '<', we might be in a
24785 		 template-argument-list.  Until Core issue 325 is
24786 		 resolved, we don't know how this situation ought
24787 		 to be handled, so try to DTRT.  We check whether
24788 		 what comes after the comma is a valid parameter
24789 		 declaration list.  If it is, then the comma ends
24790 		 the default argument; otherwise the default
24791 		 argument continues.  */
24792 	      bool error = false;
24793 
24794 	      /* Set ITALP so cp_parser_parameter_declaration_list
24795 		 doesn't decide to commit to this parse.  */
24796 	      bool saved_italp = parser->in_template_argument_list_p;
24797 	      parser->in_template_argument_list_p = true;
24798 
24799 	      cp_parser_parse_tentatively (parser);
24800 	      cp_lexer_consume_token (parser->lexer);
24801 
24802 	      if (nsdmi)
24803 		{
24804 		  int ctor_dtor_or_conv_p;
24805 		  cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24806 					&ctor_dtor_or_conv_p,
24807 					/*parenthesized_p=*/NULL,
24808 					/*member_p=*/true);
24809 		}
24810 	      else
24811 		{
24812 		  begin_scope (sk_function_parms, NULL_TREE);
24813 		  cp_parser_parameter_declaration_list (parser, &error);
24814 		  pop_bindings_and_leave_scope ();
24815 		}
24816 	      if (!cp_parser_error_occurred (parser) && !error)
24817 		done = true;
24818 	      cp_parser_abort_tentative_parse (parser);
24819 
24820 	      parser->in_template_argument_list_p = saved_italp;
24821 	      break;
24822 	    }
24823 	case CPP_CLOSE_PAREN:
24824 	case CPP_ELLIPSIS:
24825 	  /* If we run into a non-nested `;', `}', or `]',
24826 	     then the code is invalid -- but the default
24827 	     argument is certainly over.  */
24828 	case CPP_SEMICOLON:
24829 	case CPP_CLOSE_BRACE:
24830 	case CPP_CLOSE_SQUARE:
24831 	  if (depth == 0
24832 	      /* Handle correctly int n = sizeof ... ( p );  */
24833 	      && token->type != CPP_ELLIPSIS)
24834 	    done = true;
24835 	  /* Update DEPTH, if necessary.  */
24836 	  else if (token->type == CPP_CLOSE_PAREN
24837 		   || token->type == CPP_CLOSE_BRACE
24838 		   || token->type == CPP_CLOSE_SQUARE)
24839 	    --depth;
24840 	  break;
24841 
24842 	case CPP_OPEN_PAREN:
24843 	case CPP_OPEN_SQUARE:
24844 	case CPP_OPEN_BRACE:
24845 	  ++depth;
24846 	  break;
24847 
24848 	case CPP_LESS:
24849 	  if (depth == 0)
24850 	    /* This might be the comparison operator, or it might
24851 	       start a template argument list.  */
24852 	    ++maybe_template_id;
24853 	  break;
24854 
24855 	case CPP_RSHIFT:
24856 	  if (cxx_dialect == cxx98)
24857 	    break;
24858 	  /* Fall through for C++0x, which treats the `>>'
24859 	     operator like two `>' tokens in certain
24860 	     cases.  */
24861 
24862 	case CPP_GREATER:
24863 	  if (depth == 0)
24864 	    {
24865 	      /* This might be an operator, or it might close a
24866 		 template argument list.  But if a previous '<'
24867 		 started a template argument list, this will have
24868 		 closed it, so we can't be in one anymore.  */
24869 	      maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
24870 	      if (maybe_template_id < 0)
24871 		maybe_template_id = 0;
24872 	    }
24873 	  break;
24874 
24875 	  /* If we run out of tokens, issue an error message.  */
24876 	case CPP_EOF:
24877 	case CPP_PRAGMA_EOL:
24878 	  error_at (token->location, "file ends in default argument");
24879 	  done = true;
24880 	  break;
24881 
24882 	case CPP_NAME:
24883 	case CPP_SCOPE:
24884 	  /* In these cases, we should look for template-ids.
24885 	     For example, if the default argument is
24886 	     `X<int, double>()', we need to do name lookup to
24887 	     figure out whether or not `X' is a template; if
24888 	     so, the `,' does not end the default argument.
24889 
24890 	     That is not yet done.  */
24891 	  break;
24892 
24893 	default:
24894 	  break;
24895 	}
24896 
24897       /* If we've reached the end, stop.  */
24898       if (done)
24899 	break;
24900 
24901       /* Add the token to the token block.  */
24902       token = cp_lexer_consume_token (parser->lexer);
24903     }
24904 
24905   /* Create a DEFAULT_ARG to represent the unparsed default
24906      argument.  */
24907   default_argument = make_node (DEFAULT_ARG);
24908   DEFARG_TOKENS (default_argument)
24909     = cp_token_cache_new (first_token, token);
24910   DEFARG_INSTANTIATIONS (default_argument) = NULL;
24911 
24912   return default_argument;
24913 }
24914 
24915 /* Begin parsing tentatively.  We always save tokens while parsing
24916    tentatively so that if the tentative parsing fails we can restore the
24917    tokens.  */
24918 
24919 static void
cp_parser_parse_tentatively(cp_parser * parser)24920 cp_parser_parse_tentatively (cp_parser* parser)
24921 {
24922   /* Enter a new parsing context.  */
24923   parser->context = cp_parser_context_new (parser->context);
24924   /* Begin saving tokens.  */
24925   cp_lexer_save_tokens (parser->lexer);
24926   /* In order to avoid repetitive access control error messages,
24927      access checks are queued up until we are no longer parsing
24928      tentatively.  */
24929   push_deferring_access_checks (dk_deferred);
24930 }
24931 
24932 /* Commit to the currently active tentative parse.  */
24933 
24934 static void
cp_parser_commit_to_tentative_parse(cp_parser * parser)24935 cp_parser_commit_to_tentative_parse (cp_parser* parser)
24936 {
24937   cp_parser_context *context;
24938   cp_lexer *lexer;
24939 
24940   /* Mark all of the levels as committed.  */
24941   lexer = parser->lexer;
24942   for (context = parser->context; context->next; context = context->next)
24943     {
24944       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24945 	break;
24946       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24947       while (!cp_lexer_saving_tokens (lexer))
24948 	lexer = lexer->next;
24949       cp_lexer_commit_tokens (lexer);
24950     }
24951 }
24952 
24953 /* Commit to the topmost currently active tentative parse.
24954 
24955    Note that this function shouldn't be called when there are
24956    irreversible side-effects while in a tentative state.  For
24957    example, we shouldn't create a permanent entry in the symbol
24958    table, or issue an error message that might not apply if the
24959    tentative parse is aborted.  */
24960 
24961 static void
cp_parser_commit_to_topmost_tentative_parse(cp_parser * parser)24962 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
24963 {
24964   cp_parser_context *context = parser->context;
24965   cp_lexer *lexer = parser->lexer;
24966 
24967   if (context)
24968     {
24969       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24970 	return;
24971       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24972 
24973       while (!cp_lexer_saving_tokens (lexer))
24974 	lexer = lexer->next;
24975       cp_lexer_commit_tokens (lexer);
24976     }
24977 }
24978 
24979 /* Abort the currently active tentative parse.  All consumed tokens
24980    will be rolled back, and no diagnostics will be issued.  */
24981 
24982 static void
cp_parser_abort_tentative_parse(cp_parser * parser)24983 cp_parser_abort_tentative_parse (cp_parser* parser)
24984 {
24985   cp_parser_simulate_error (parser);
24986   /* Now, pretend that we want to see if the construct was
24987      successfully parsed.  */
24988   cp_parser_parse_definitely (parser);
24989 }
24990 
24991 /* Stop parsing tentatively.  If a parse error has occurred, restore the
24992    token stream.  Otherwise, commit to the tokens we have consumed.
24993    Returns true if no error occurred; false otherwise.  */
24994 
24995 static bool
cp_parser_parse_definitely(cp_parser * parser)24996 cp_parser_parse_definitely (cp_parser* parser)
24997 {
24998   bool error_occurred;
24999   cp_parser_context *context;
25000 
25001   /* Remember whether or not an error occurred, since we are about to
25002      destroy that information.  */
25003   error_occurred = cp_parser_error_occurred (parser);
25004   /* Remove the topmost context from the stack.  */
25005   context = parser->context;
25006   parser->context = context->next;
25007   /* If no parse errors occurred, commit to the tentative parse.  */
25008   if (!error_occurred)
25009     {
25010       /* Commit to the tokens read tentatively, unless that was
25011 	 already done.  */
25012       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25013 	cp_lexer_commit_tokens (parser->lexer);
25014 
25015       pop_to_parent_deferring_access_checks ();
25016     }
25017   /* Otherwise, if errors occurred, roll back our state so that things
25018      are just as they were before we began the tentative parse.  */
25019   else
25020     {
25021       cp_lexer_rollback_tokens (parser->lexer);
25022       pop_deferring_access_checks ();
25023     }
25024   /* Add the context to the front of the free list.  */
25025   context->next = cp_parser_context_free_list;
25026   cp_parser_context_free_list = context;
25027 
25028   return !error_occurred;
25029 }
25030 
25031 /* Returns true if we are parsing tentatively and are not committed to
25032    this tentative parse.  */
25033 
25034 static bool
cp_parser_uncommitted_to_tentative_parse_p(cp_parser * parser)25035 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25036 {
25037   return (cp_parser_parsing_tentatively (parser)
25038 	  && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25039 }
25040 
25041 /* Returns nonzero iff an error has occurred during the most recent
25042    tentative parse.  */
25043 
25044 static bool
cp_parser_error_occurred(cp_parser * parser)25045 cp_parser_error_occurred (cp_parser* parser)
25046 {
25047   return (cp_parser_parsing_tentatively (parser)
25048 	  && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25049 }
25050 
25051 /* Returns nonzero if GNU extensions are allowed.  */
25052 
25053 static bool
cp_parser_allow_gnu_extensions_p(cp_parser * parser)25054 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25055 {
25056   return parser->allow_gnu_extensions_p;
25057 }
25058 
25059 /* Objective-C++ Productions */
25060 
25061 
25062 /* Parse an Objective-C expression, which feeds into a primary-expression
25063    above.
25064 
25065    objc-expression:
25066      objc-message-expression
25067      objc-string-literal
25068      objc-encode-expression
25069      objc-protocol-expression
25070      objc-selector-expression
25071 
25072   Returns a tree representation of the expression.  */
25073 
25074 static tree
cp_parser_objc_expression(cp_parser * parser)25075 cp_parser_objc_expression (cp_parser* parser)
25076 {
25077   /* Try to figure out what kind of declaration is present.  */
25078   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25079 
25080   switch (kwd->type)
25081     {
25082     case CPP_OPEN_SQUARE:
25083       return cp_parser_objc_message_expression (parser);
25084 
25085     case CPP_OBJC_STRING:
25086       kwd = cp_lexer_consume_token (parser->lexer);
25087       return objc_build_string_object (kwd->u.value);
25088 
25089     case CPP_KEYWORD:
25090       switch (kwd->keyword)
25091 	{
25092 	case RID_AT_ENCODE:
25093 	  return cp_parser_objc_encode_expression (parser);
25094 
25095 	case RID_AT_PROTOCOL:
25096 	  return cp_parser_objc_protocol_expression (parser);
25097 
25098 	case RID_AT_SELECTOR:
25099 	  return cp_parser_objc_selector_expression (parser);
25100 
25101 	default:
25102 	  break;
25103 	}
25104     default:
25105       error_at (kwd->location,
25106 		"misplaced %<@%D%> Objective-C++ construct",
25107 		kwd->u.value);
25108       cp_parser_skip_to_end_of_block_or_statement (parser);
25109     }
25110 
25111   return error_mark_node;
25112 }
25113 
25114 /* Parse an Objective-C message expression.
25115 
25116    objc-message-expression:
25117      [ objc-message-receiver objc-message-args ]
25118 
25119    Returns a representation of an Objective-C message.  */
25120 
25121 static tree
cp_parser_objc_message_expression(cp_parser * parser)25122 cp_parser_objc_message_expression (cp_parser* parser)
25123 {
25124   tree receiver, messageargs;
25125 
25126   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
25127   receiver = cp_parser_objc_message_receiver (parser);
25128   messageargs = cp_parser_objc_message_args (parser);
25129   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25130 
25131   return objc_build_message_expr (receiver, messageargs);
25132 }
25133 
25134 /* Parse an objc-message-receiver.
25135 
25136    objc-message-receiver:
25137      expression
25138      simple-type-specifier
25139 
25140   Returns a representation of the type or expression.  */
25141 
25142 static tree
cp_parser_objc_message_receiver(cp_parser * parser)25143 cp_parser_objc_message_receiver (cp_parser* parser)
25144 {
25145   tree rcv;
25146 
25147   /* An Objective-C message receiver may be either (1) a type
25148      or (2) an expression.  */
25149   cp_parser_parse_tentatively (parser);
25150   rcv = cp_parser_expression (parser, false, NULL);
25151 
25152   if (cp_parser_parse_definitely (parser))
25153     return rcv;
25154 
25155   rcv = cp_parser_simple_type_specifier (parser,
25156 					 /*decl_specs=*/NULL,
25157 					 CP_PARSER_FLAGS_NONE);
25158 
25159   return objc_get_class_reference (rcv);
25160 }
25161 
25162 /* Parse the arguments and selectors comprising an Objective-C message.
25163 
25164    objc-message-args:
25165      objc-selector
25166      objc-selector-args
25167      objc-selector-args , objc-comma-args
25168 
25169    objc-selector-args:
25170      objc-selector [opt] : assignment-expression
25171      objc-selector-args objc-selector [opt] : assignment-expression
25172 
25173    objc-comma-args:
25174      assignment-expression
25175      objc-comma-args , assignment-expression
25176 
25177    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25178    selector arguments and TREE_VALUE containing a list of comma
25179    arguments.  */
25180 
25181 static tree
cp_parser_objc_message_args(cp_parser * parser)25182 cp_parser_objc_message_args (cp_parser* parser)
25183 {
25184   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25185   bool maybe_unary_selector_p = true;
25186   cp_token *token = cp_lexer_peek_token (parser->lexer);
25187 
25188   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25189     {
25190       tree selector = NULL_TREE, arg;
25191 
25192       if (token->type != CPP_COLON)
25193 	selector = cp_parser_objc_selector (parser);
25194 
25195       /* Detect if we have a unary selector.  */
25196       if (maybe_unary_selector_p
25197 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25198 	return build_tree_list (selector, NULL_TREE);
25199 
25200       maybe_unary_selector_p = false;
25201       cp_parser_require (parser, CPP_COLON, RT_COLON);
25202       arg = cp_parser_assignment_expression (parser, false, NULL);
25203 
25204       sel_args
25205 	= chainon (sel_args,
25206 		   build_tree_list (selector, arg));
25207 
25208       token = cp_lexer_peek_token (parser->lexer);
25209     }
25210 
25211   /* Handle non-selector arguments, if any. */
25212   while (token->type == CPP_COMMA)
25213     {
25214       tree arg;
25215 
25216       cp_lexer_consume_token (parser->lexer);
25217       arg = cp_parser_assignment_expression (parser, false, NULL);
25218 
25219       addl_args
25220 	= chainon (addl_args,
25221 		   build_tree_list (NULL_TREE, arg));
25222 
25223       token = cp_lexer_peek_token (parser->lexer);
25224     }
25225 
25226   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25227     {
25228       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25229       return build_tree_list (error_mark_node, error_mark_node);
25230     }
25231 
25232   return build_tree_list (sel_args, addl_args);
25233 }
25234 
25235 /* Parse an Objective-C encode expression.
25236 
25237    objc-encode-expression:
25238      @encode objc-typename
25239 
25240    Returns an encoded representation of the type argument.  */
25241 
25242 static tree
cp_parser_objc_encode_expression(cp_parser * parser)25243 cp_parser_objc_encode_expression (cp_parser* parser)
25244 {
25245   tree type;
25246   cp_token *token;
25247 
25248   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
25249   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25250   token = cp_lexer_peek_token (parser->lexer);
25251   type = complete_type (cp_parser_type_id (parser));
25252   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25253 
25254   if (!type)
25255     {
25256       error_at (token->location,
25257 		"%<@encode%> must specify a type as an argument");
25258       return error_mark_node;
25259     }
25260 
25261   /* This happens if we find @encode(T) (where T is a template
25262      typename or something dependent on a template typename) when
25263      parsing a template.  In that case, we can't compile it
25264      immediately, but we rather create an AT_ENCODE_EXPR which will
25265      need to be instantiated when the template is used.
25266   */
25267   if (dependent_type_p (type))
25268     {
25269       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25270       TREE_READONLY (value) = 1;
25271       return value;
25272     }
25273 
25274   return objc_build_encode_expr (type);
25275 }
25276 
25277 /* Parse an Objective-C @defs expression.  */
25278 
25279 static tree
cp_parser_objc_defs_expression(cp_parser * parser)25280 cp_parser_objc_defs_expression (cp_parser *parser)
25281 {
25282   tree name;
25283 
25284   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
25285   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25286   name = cp_parser_identifier (parser);
25287   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25288 
25289   return objc_get_class_ivars (name);
25290 }
25291 
25292 /* Parse an Objective-C protocol expression.
25293 
25294   objc-protocol-expression:
25295     @protocol ( identifier )
25296 
25297   Returns a representation of the protocol expression.  */
25298 
25299 static tree
cp_parser_objc_protocol_expression(cp_parser * parser)25300 cp_parser_objc_protocol_expression (cp_parser* parser)
25301 {
25302   tree proto;
25303 
25304   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
25305   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25306   proto = cp_parser_identifier (parser);
25307   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25308 
25309   return objc_build_protocol_expr (proto);
25310 }
25311 
25312 /* Parse an Objective-C selector expression.
25313 
25314    objc-selector-expression:
25315      @selector ( objc-method-signature )
25316 
25317    objc-method-signature:
25318      objc-selector
25319      objc-selector-seq
25320 
25321    objc-selector-seq:
25322      objc-selector :
25323      objc-selector-seq objc-selector :
25324 
25325   Returns a representation of the method selector.  */
25326 
25327 static tree
cp_parser_objc_selector_expression(cp_parser * parser)25328 cp_parser_objc_selector_expression (cp_parser* parser)
25329 {
25330   tree sel_seq = NULL_TREE;
25331   bool maybe_unary_selector_p = true;
25332   cp_token *token;
25333   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25334 
25335   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
25336   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25337   token = cp_lexer_peek_token (parser->lexer);
25338 
25339   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25340 	 || token->type == CPP_SCOPE)
25341     {
25342       tree selector = NULL_TREE;
25343 
25344       if (token->type != CPP_COLON
25345 	  || token->type == CPP_SCOPE)
25346 	selector = cp_parser_objc_selector (parser);
25347 
25348       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25349 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25350 	{
25351 	  /* Detect if we have a unary selector.  */
25352 	  if (maybe_unary_selector_p)
25353 	    {
25354 	      sel_seq = selector;
25355 	      goto finish_selector;
25356 	    }
25357 	  else
25358 	    {
25359 	      cp_parser_error (parser, "expected %<:%>");
25360 	    }
25361 	}
25362       maybe_unary_selector_p = false;
25363       token = cp_lexer_consume_token (parser->lexer);
25364 
25365       if (token->type == CPP_SCOPE)
25366 	{
25367 	  sel_seq
25368 	    = chainon (sel_seq,
25369 		       build_tree_list (selector, NULL_TREE));
25370 	  sel_seq
25371 	    = chainon (sel_seq,
25372 		       build_tree_list (NULL_TREE, NULL_TREE));
25373 	}
25374       else
25375 	sel_seq
25376 	  = chainon (sel_seq,
25377 		     build_tree_list (selector, NULL_TREE));
25378 
25379       token = cp_lexer_peek_token (parser->lexer);
25380     }
25381 
25382  finish_selector:
25383   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25384 
25385   return objc_build_selector_expr (loc, sel_seq);
25386 }
25387 
25388 /* Parse a list of identifiers.
25389 
25390    objc-identifier-list:
25391      identifier
25392      objc-identifier-list , identifier
25393 
25394    Returns a TREE_LIST of identifier nodes.  */
25395 
25396 static tree
cp_parser_objc_identifier_list(cp_parser * parser)25397 cp_parser_objc_identifier_list (cp_parser* parser)
25398 {
25399   tree identifier;
25400   tree list;
25401   cp_token *sep;
25402 
25403   identifier = cp_parser_identifier (parser);
25404   if (identifier == error_mark_node)
25405     return error_mark_node;
25406 
25407   list = build_tree_list (NULL_TREE, identifier);
25408   sep = cp_lexer_peek_token (parser->lexer);
25409 
25410   while (sep->type == CPP_COMMA)
25411     {
25412       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
25413       identifier = cp_parser_identifier (parser);
25414       if (identifier == error_mark_node)
25415 	return list;
25416 
25417       list = chainon (list, build_tree_list (NULL_TREE,
25418 					     identifier));
25419       sep = cp_lexer_peek_token (parser->lexer);
25420     }
25421 
25422   return list;
25423 }
25424 
25425 /* Parse an Objective-C alias declaration.
25426 
25427    objc-alias-declaration:
25428      @compatibility_alias identifier identifier ;
25429 
25430    This function registers the alias mapping with the Objective-C front end.
25431    It returns nothing.  */
25432 
25433 static void
cp_parser_objc_alias_declaration(cp_parser * parser)25434 cp_parser_objc_alias_declaration (cp_parser* parser)
25435 {
25436   tree alias, orig;
25437 
25438   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
25439   alias = cp_parser_identifier (parser);
25440   orig = cp_parser_identifier (parser);
25441   objc_declare_alias (alias, orig);
25442   cp_parser_consume_semicolon_at_end_of_statement (parser);
25443 }
25444 
25445 /* Parse an Objective-C class forward-declaration.
25446 
25447    objc-class-declaration:
25448      @class objc-identifier-list ;
25449 
25450    The function registers the forward declarations with the Objective-C
25451    front end.  It returns nothing.  */
25452 
25453 static void
cp_parser_objc_class_declaration(cp_parser * parser)25454 cp_parser_objc_class_declaration (cp_parser* parser)
25455 {
25456   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
25457   while (true)
25458     {
25459       tree id;
25460 
25461       id = cp_parser_identifier (parser);
25462       if (id == error_mark_node)
25463 	break;
25464 
25465       objc_declare_class (id);
25466 
25467       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25468 	cp_lexer_consume_token (parser->lexer);
25469       else
25470 	break;
25471     }
25472   cp_parser_consume_semicolon_at_end_of_statement (parser);
25473 }
25474 
25475 /* Parse a list of Objective-C protocol references.
25476 
25477    objc-protocol-refs-opt:
25478      objc-protocol-refs [opt]
25479 
25480    objc-protocol-refs:
25481      < objc-identifier-list >
25482 
25483    Returns a TREE_LIST of identifiers, if any.  */
25484 
25485 static tree
cp_parser_objc_protocol_refs_opt(cp_parser * parser)25486 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25487 {
25488   tree protorefs = NULL_TREE;
25489 
25490   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25491     {
25492       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
25493       protorefs = cp_parser_objc_identifier_list (parser);
25494       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25495     }
25496 
25497   return protorefs;
25498 }
25499 
25500 /* Parse a Objective-C visibility specification.  */
25501 
25502 static void
cp_parser_objc_visibility_spec(cp_parser * parser)25503 cp_parser_objc_visibility_spec (cp_parser* parser)
25504 {
25505   cp_token *vis = cp_lexer_peek_token (parser->lexer);
25506 
25507   switch (vis->keyword)
25508     {
25509     case RID_AT_PRIVATE:
25510       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25511       break;
25512     case RID_AT_PROTECTED:
25513       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25514       break;
25515     case RID_AT_PUBLIC:
25516       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25517       break;
25518     case RID_AT_PACKAGE:
25519       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25520       break;
25521     default:
25522       return;
25523     }
25524 
25525   /* Eat '@private'/'@protected'/'@public'.  */
25526   cp_lexer_consume_token (parser->lexer);
25527 }
25528 
25529 /* Parse an Objective-C method type.  Return 'true' if it is a class
25530    (+) method, and 'false' if it is an instance (-) method.  */
25531 
25532 static inline bool
cp_parser_objc_method_type(cp_parser * parser)25533 cp_parser_objc_method_type (cp_parser* parser)
25534 {
25535   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25536     return true;
25537   else
25538     return false;
25539 }
25540 
25541 /* Parse an Objective-C protocol qualifier.  */
25542 
25543 static tree
cp_parser_objc_protocol_qualifiers(cp_parser * parser)25544 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25545 {
25546   tree quals = NULL_TREE, node;
25547   cp_token *token = cp_lexer_peek_token (parser->lexer);
25548 
25549   node = token->u.value;
25550 
25551   while (node && identifier_p (node)
25552 	 && (node == ridpointers [(int) RID_IN]
25553 	     || node == ridpointers [(int) RID_OUT]
25554 	     || node == ridpointers [(int) RID_INOUT]
25555 	     || node == ridpointers [(int) RID_BYCOPY]
25556 	     || node == ridpointers [(int) RID_BYREF]
25557 	     || node == ridpointers [(int) RID_ONEWAY]))
25558     {
25559       quals = tree_cons (NULL_TREE, node, quals);
25560       cp_lexer_consume_token (parser->lexer);
25561       token = cp_lexer_peek_token (parser->lexer);
25562       node = token->u.value;
25563     }
25564 
25565   return quals;
25566 }
25567 
25568 /* Parse an Objective-C typename.  */
25569 
25570 static tree
cp_parser_objc_typename(cp_parser * parser)25571 cp_parser_objc_typename (cp_parser* parser)
25572 {
25573   tree type_name = NULL_TREE;
25574 
25575   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25576     {
25577       tree proto_quals, cp_type = NULL_TREE;
25578 
25579       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
25580       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
25581 
25582       /* An ObjC type name may consist of just protocol qualifiers, in which
25583 	 case the type shall default to 'id'.  */
25584       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25585 	{
25586 	  cp_type = cp_parser_type_id (parser);
25587 
25588 	  /* If the type could not be parsed, an error has already
25589 	     been produced.  For error recovery, behave as if it had
25590 	     not been specified, which will use the default type
25591 	     'id'.  */
25592 	  if (cp_type == error_mark_node)
25593 	    {
25594 	      cp_type = NULL_TREE;
25595 	      /* We need to skip to the closing parenthesis as
25596 		 cp_parser_type_id() does not seem to do it for
25597 		 us.  */
25598 	      cp_parser_skip_to_closing_parenthesis (parser,
25599 						     /*recovering=*/true,
25600 						     /*or_comma=*/false,
25601 						     /*consume_paren=*/false);
25602 	    }
25603 	}
25604 
25605       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25606       type_name = build_tree_list (proto_quals, cp_type);
25607     }
25608 
25609   return type_name;
25610 }
25611 
25612 /* Check to see if TYPE refers to an Objective-C selector name.  */
25613 
25614 static bool
cp_parser_objc_selector_p(enum cpp_ttype type)25615 cp_parser_objc_selector_p (enum cpp_ttype type)
25616 {
25617   return (type == CPP_NAME || type == CPP_KEYWORD
25618 	  || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
25619 	  || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
25620 	  || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
25621 	  || type == CPP_XOR || type == CPP_XOR_EQ);
25622 }
25623 
25624 /* Parse an Objective-C selector.  */
25625 
25626 static tree
cp_parser_objc_selector(cp_parser * parser)25627 cp_parser_objc_selector (cp_parser* parser)
25628 {
25629   cp_token *token = cp_lexer_consume_token (parser->lexer);
25630 
25631   if (!cp_parser_objc_selector_p (token->type))
25632     {
25633       error_at (token->location, "invalid Objective-C++ selector name");
25634       return error_mark_node;
25635     }
25636 
25637   /* C++ operator names are allowed to appear in ObjC selectors.  */
25638   switch (token->type)
25639     {
25640     case CPP_AND_AND: return get_identifier ("and");
25641     case CPP_AND_EQ: return get_identifier ("and_eq");
25642     case CPP_AND: return get_identifier ("bitand");
25643     case CPP_OR: return get_identifier ("bitor");
25644     case CPP_COMPL: return get_identifier ("compl");
25645     case CPP_NOT: return get_identifier ("not");
25646     case CPP_NOT_EQ: return get_identifier ("not_eq");
25647     case CPP_OR_OR: return get_identifier ("or");
25648     case CPP_OR_EQ: return get_identifier ("or_eq");
25649     case CPP_XOR: return get_identifier ("xor");
25650     case CPP_XOR_EQ: return get_identifier ("xor_eq");
25651     default: return token->u.value;
25652     }
25653 }
25654 
25655 /* Parse an Objective-C params list.  */
25656 
25657 static tree
cp_parser_objc_method_keyword_params(cp_parser * parser,tree * attributes)25658 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
25659 {
25660   tree params = NULL_TREE;
25661   bool maybe_unary_selector_p = true;
25662   cp_token *token = cp_lexer_peek_token (parser->lexer);
25663 
25664   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25665     {
25666       tree selector = NULL_TREE, type_name, identifier;
25667       tree parm_attr = NULL_TREE;
25668 
25669       if (token->keyword == RID_ATTRIBUTE)
25670 	break;
25671 
25672       if (token->type != CPP_COLON)
25673 	selector = cp_parser_objc_selector (parser);
25674 
25675       /* Detect if we have a unary selector.  */
25676       if (maybe_unary_selector_p
25677 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25678 	{
25679 	  params = selector; /* Might be followed by attributes.  */
25680 	  break;
25681 	}
25682 
25683       maybe_unary_selector_p = false;
25684       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25685 	{
25686 	  /* Something went quite wrong.  There should be a colon
25687 	     here, but there is not.  Stop parsing parameters.  */
25688 	  break;
25689 	}
25690       type_name = cp_parser_objc_typename (parser);
25691       /* New ObjC allows attributes on parameters too.  */
25692       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
25693 	parm_attr = cp_parser_attributes_opt (parser);
25694       identifier = cp_parser_identifier (parser);
25695 
25696       params
25697 	= chainon (params,
25698 		   objc_build_keyword_decl (selector,
25699 					    type_name,
25700 					    identifier,
25701 					    parm_attr));
25702 
25703       token = cp_lexer_peek_token (parser->lexer);
25704     }
25705 
25706   if (params == NULL_TREE)
25707     {
25708       cp_parser_error (parser, "objective-c++ method declaration is expected");
25709       return error_mark_node;
25710     }
25711 
25712   /* We allow tail attributes for the method.  */
25713   if (token->keyword == RID_ATTRIBUTE)
25714     {
25715       *attributes = cp_parser_attributes_opt (parser);
25716       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25717 	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25718 	return params;
25719       cp_parser_error (parser,
25720 		       "method attributes must be specified at the end");
25721       return error_mark_node;
25722     }
25723 
25724   if (params == NULL_TREE)
25725     {
25726       cp_parser_error (parser, "objective-c++ method declaration is expected");
25727       return error_mark_node;
25728     }
25729   return params;
25730 }
25731 
25732 /* Parse the non-keyword Objective-C params.  */
25733 
25734 static tree
cp_parser_objc_method_tail_params_opt(cp_parser * parser,bool * ellipsisp,tree * attributes)25735 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
25736 				       tree* attributes)
25737 {
25738   tree params = make_node (TREE_LIST);
25739   cp_token *token = cp_lexer_peek_token (parser->lexer);
25740   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
25741 
25742   while (token->type == CPP_COMMA)
25743     {
25744       cp_parameter_declarator *parmdecl;
25745       tree parm;
25746 
25747       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
25748       token = cp_lexer_peek_token (parser->lexer);
25749 
25750       if (token->type == CPP_ELLIPSIS)
25751 	{
25752 	  cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
25753 	  *ellipsisp = true;
25754 	  token = cp_lexer_peek_token (parser->lexer);
25755 	  break;
25756 	}
25757 
25758       /* TODO: parse attributes for tail parameters.  */
25759       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
25760       parm = grokdeclarator (parmdecl->declarator,
25761 			     &parmdecl->decl_specifiers,
25762 			     PARM, /*initialized=*/0,
25763 			     /*attrlist=*/NULL);
25764 
25765       chainon (params, build_tree_list (NULL_TREE, parm));
25766       token = cp_lexer_peek_token (parser->lexer);
25767     }
25768 
25769   /* We allow tail attributes for the method.  */
25770   if (token->keyword == RID_ATTRIBUTE)
25771     {
25772       if (*attributes == NULL_TREE)
25773 	{
25774 	  *attributes = cp_parser_attributes_opt (parser);
25775 	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25776 	      || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25777 	    return params;
25778 	}
25779       else
25780 	/* We have an error, but parse the attributes, so that we can
25781 	   carry on.  */
25782 	*attributes = cp_parser_attributes_opt (parser);
25783 
25784       cp_parser_error (parser,
25785 		       "method attributes must be specified at the end");
25786       return error_mark_node;
25787     }
25788 
25789   return params;
25790 }
25791 
25792 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
25793 
25794 static void
cp_parser_objc_interstitial_code(cp_parser * parser)25795 cp_parser_objc_interstitial_code (cp_parser* parser)
25796 {
25797   cp_token *token = cp_lexer_peek_token (parser->lexer);
25798 
25799   /* If the next token is `extern' and the following token is a string
25800      literal, then we have a linkage specification.  */
25801   if (token->keyword == RID_EXTERN
25802       && cp_parser_is_pure_string_literal
25803 	 (cp_lexer_peek_nth_token (parser->lexer, 2)))
25804     cp_parser_linkage_specification (parser);
25805   /* Handle #pragma, if any.  */
25806   else if (token->type == CPP_PRAGMA)
25807     cp_parser_pragma (parser, pragma_objc_icode);
25808   /* Allow stray semicolons.  */
25809   else if (token->type == CPP_SEMICOLON)
25810     cp_lexer_consume_token (parser->lexer);
25811   /* Mark methods as optional or required, when building protocols.  */
25812   else if (token->keyword == RID_AT_OPTIONAL)
25813     {
25814       cp_lexer_consume_token (parser->lexer);
25815       objc_set_method_opt (true);
25816     }
25817   else if (token->keyword == RID_AT_REQUIRED)
25818     {
25819       cp_lexer_consume_token (parser->lexer);
25820       objc_set_method_opt (false);
25821     }
25822   else if (token->keyword == RID_NAMESPACE)
25823     cp_parser_namespace_definition (parser);
25824   /* Other stray characters must generate errors.  */
25825   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
25826     {
25827       cp_lexer_consume_token (parser->lexer);
25828       error ("stray %qs between Objective-C++ methods",
25829 	     token->type == CPP_OPEN_BRACE ? "{" : "}");
25830     }
25831   /* Finally, try to parse a block-declaration, or a function-definition.  */
25832   else
25833     cp_parser_block_declaration (parser, /*statement_p=*/false);
25834 }
25835 
25836 /* Parse a method signature.  */
25837 
25838 static tree
cp_parser_objc_method_signature(cp_parser * parser,tree * attributes)25839 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
25840 {
25841   tree rettype, kwdparms, optparms;
25842   bool ellipsis = false;
25843   bool is_class_method;
25844 
25845   is_class_method = cp_parser_objc_method_type (parser);
25846   rettype = cp_parser_objc_typename (parser);
25847   *attributes = NULL_TREE;
25848   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
25849   if (kwdparms == error_mark_node)
25850     return error_mark_node;
25851   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
25852   if (optparms == error_mark_node)
25853     return error_mark_node;
25854 
25855   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
25856 }
25857 
25858 static bool
cp_parser_objc_method_maybe_bad_prefix_attributes(cp_parser * parser)25859 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
25860 {
25861   tree tattr;
25862   cp_lexer_save_tokens (parser->lexer);
25863   tattr = cp_parser_attributes_opt (parser);
25864   gcc_assert (tattr) ;
25865 
25866   /* If the attributes are followed by a method introducer, this is not allowed.
25867      Dump the attributes and flag the situation.  */
25868   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
25869       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
25870     return true;
25871 
25872   /* Otherwise, the attributes introduce some interstitial code, possibly so
25873      rewind to allow that check.  */
25874   cp_lexer_rollback_tokens (parser->lexer);
25875   return false;
25876 }
25877 
25878 /* Parse an Objective-C method prototype list.  */
25879 
25880 static void
cp_parser_objc_method_prototype_list(cp_parser * parser)25881 cp_parser_objc_method_prototype_list (cp_parser* parser)
25882 {
25883   cp_token *token = cp_lexer_peek_token (parser->lexer);
25884 
25885   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25886     {
25887       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25888 	{
25889 	  tree attributes, sig;
25890 	  bool is_class_method;
25891 	  if (token->type == CPP_PLUS)
25892 	    is_class_method = true;
25893 	  else
25894 	    is_class_method = false;
25895 	  sig = cp_parser_objc_method_signature (parser, &attributes);
25896 	  if (sig == error_mark_node)
25897 	    {
25898 	      cp_parser_skip_to_end_of_block_or_statement (parser);
25899 	      token = cp_lexer_peek_token (parser->lexer);
25900 	      continue;
25901 	    }
25902 	  objc_add_method_declaration (is_class_method, sig, attributes);
25903 	  cp_parser_consume_semicolon_at_end_of_statement (parser);
25904 	}
25905       else if (token->keyword == RID_AT_PROPERTY)
25906 	cp_parser_objc_at_property_declaration (parser);
25907       else if (token->keyword == RID_ATTRIBUTE
25908       	       && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25909 	warning_at (cp_lexer_peek_token (parser->lexer)->location,
25910 		    OPT_Wattributes,
25911 		    "prefix attributes are ignored for methods");
25912       else
25913 	/* Allow for interspersed non-ObjC++ code.  */
25914 	cp_parser_objc_interstitial_code (parser);
25915 
25916       token = cp_lexer_peek_token (parser->lexer);
25917     }
25918 
25919   if (token->type != CPP_EOF)
25920     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
25921   else
25922     cp_parser_error (parser, "expected %<@end%>");
25923 
25924   objc_finish_interface ();
25925 }
25926 
25927 /* Parse an Objective-C method definition list.  */
25928 
25929 static void
cp_parser_objc_method_definition_list(cp_parser * parser)25930 cp_parser_objc_method_definition_list (cp_parser* parser)
25931 {
25932   cp_token *token = cp_lexer_peek_token (parser->lexer);
25933 
25934   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25935     {
25936       tree meth;
25937 
25938       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25939 	{
25940 	  cp_token *ptk;
25941 	  tree sig, attribute;
25942 	  bool is_class_method;
25943 	  if (token->type == CPP_PLUS)
25944 	    is_class_method = true;
25945 	  else
25946 	    is_class_method = false;
25947 	  push_deferring_access_checks (dk_deferred);
25948 	  sig = cp_parser_objc_method_signature (parser, &attribute);
25949 	  if (sig == error_mark_node)
25950 	    {
25951 	      cp_parser_skip_to_end_of_block_or_statement (parser);
25952 	      token = cp_lexer_peek_token (parser->lexer);
25953 	      continue;
25954 	    }
25955 	  objc_start_method_definition (is_class_method, sig, attribute,
25956 					NULL_TREE);
25957 
25958 	  /* For historical reasons, we accept an optional semicolon.  */
25959 	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25960 	    cp_lexer_consume_token (parser->lexer);
25961 
25962 	  ptk = cp_lexer_peek_token (parser->lexer);
25963 	  if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
25964 		|| ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
25965 	    {
25966 	      perform_deferred_access_checks (tf_warning_or_error);
25967 	      stop_deferring_access_checks ();
25968 	      meth = cp_parser_function_definition_after_declarator (parser,
25969 								     false);
25970 	      pop_deferring_access_checks ();
25971 	      objc_finish_method_definition (meth);
25972 	    }
25973 	}
25974       /* The following case will be removed once @synthesize is
25975 	 completely implemented.  */
25976       else if (token->keyword == RID_AT_PROPERTY)
25977 	cp_parser_objc_at_property_declaration (parser);
25978       else if (token->keyword == RID_AT_SYNTHESIZE)
25979 	cp_parser_objc_at_synthesize_declaration (parser);
25980       else if (token->keyword == RID_AT_DYNAMIC)
25981 	cp_parser_objc_at_dynamic_declaration (parser);
25982       else if (token->keyword == RID_ATTRIBUTE
25983       	       && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25984 	warning_at (token->location, OPT_Wattributes,
25985 	       	    "prefix attributes are ignored for methods");
25986       else
25987 	/* Allow for interspersed non-ObjC++ code.  */
25988 	cp_parser_objc_interstitial_code (parser);
25989 
25990       token = cp_lexer_peek_token (parser->lexer);
25991     }
25992 
25993   if (token->type != CPP_EOF)
25994     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
25995   else
25996     cp_parser_error (parser, "expected %<@end%>");
25997 
25998   objc_finish_implementation ();
25999 }
26000 
26001 /* Parse Objective-C ivars.  */
26002 
26003 static void
cp_parser_objc_class_ivars(cp_parser * parser)26004 cp_parser_objc_class_ivars (cp_parser* parser)
26005 {
26006   cp_token *token = cp_lexer_peek_token (parser->lexer);
26007 
26008   if (token->type != CPP_OPEN_BRACE)
26009     return;	/* No ivars specified.  */
26010 
26011   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
26012   token = cp_lexer_peek_token (parser->lexer);
26013 
26014   while (token->type != CPP_CLOSE_BRACE
26015 	&& token->keyword != RID_AT_END && token->type != CPP_EOF)
26016     {
26017       cp_decl_specifier_seq declspecs;
26018       int decl_class_or_enum_p;
26019       tree prefix_attributes;
26020 
26021       cp_parser_objc_visibility_spec (parser);
26022 
26023       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26024 	break;
26025 
26026       cp_parser_decl_specifier_seq (parser,
26027 				    CP_PARSER_FLAGS_OPTIONAL,
26028 				    &declspecs,
26029 				    &decl_class_or_enum_p);
26030 
26031       /* auto, register, static, extern, mutable.  */
26032       if (declspecs.storage_class != sc_none)
26033 	{
26034 	  cp_parser_error (parser, "invalid type for instance variable");
26035 	  declspecs.storage_class = sc_none;
26036 	}
26037 
26038       /* thread_local.  */
26039       if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26040 	{
26041 	  cp_parser_error (parser, "invalid type for instance variable");
26042 	  declspecs.locations[ds_thread] = 0;
26043 	}
26044 
26045       /* typedef.  */
26046       if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26047 	{
26048 	  cp_parser_error (parser, "invalid type for instance variable");
26049 	  declspecs.locations[ds_typedef] = 0;
26050 	}
26051 
26052       prefix_attributes = declspecs.attributes;
26053       declspecs.attributes = NULL_TREE;
26054 
26055       /* Keep going until we hit the `;' at the end of the
26056 	 declaration.  */
26057       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26058 	{
26059 	  tree width = NULL_TREE, attributes, first_attribute, decl;
26060 	  cp_declarator *declarator = NULL;
26061 	  int ctor_dtor_or_conv_p;
26062 
26063 	  /* Check for a (possibly unnamed) bitfield declaration.  */
26064 	  token = cp_lexer_peek_token (parser->lexer);
26065 	  if (token->type == CPP_COLON)
26066 	    goto eat_colon;
26067 
26068 	  if (token->type == CPP_NAME
26069 	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26070 		  == CPP_COLON))
26071 	    {
26072 	      /* Get the name of the bitfield.  */
26073 	      declarator = make_id_declarator (NULL_TREE,
26074 					       cp_parser_identifier (parser),
26075 					       sfk_none);
26076 
26077 	     eat_colon:
26078 	      cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
26079 	      /* Get the width of the bitfield.  */
26080 	      width
26081 		= cp_parser_constant_expression (parser,
26082 						 /*allow_non_constant=*/false,
26083 						 NULL);
26084 	    }
26085 	  else
26086 	    {
26087 	      /* Parse the declarator.  */
26088 	      declarator
26089 		= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26090 					&ctor_dtor_or_conv_p,
26091 					/*parenthesized_p=*/NULL,
26092 					/*member_p=*/false);
26093 	    }
26094 
26095 	  /* Look for attributes that apply to the ivar.  */
26096 	  attributes = cp_parser_attributes_opt (parser);
26097 	  /* Remember which attributes are prefix attributes and
26098 	     which are not.  */
26099 	  first_attribute = attributes;
26100 	  /* Combine the attributes.  */
26101 	  attributes = chainon (prefix_attributes, attributes);
26102 
26103 	  if (width)
26104 	      /* Create the bitfield declaration.  */
26105 	      decl = grokbitfield (declarator, &declspecs,
26106 				   width,
26107 				   attributes);
26108 	  else
26109 	    decl = grokfield (declarator, &declspecs,
26110 			      NULL_TREE, /*init_const_expr_p=*/false,
26111 			      NULL_TREE, attributes);
26112 
26113 	  /* Add the instance variable.  */
26114 	  if (decl != error_mark_node && decl != NULL_TREE)
26115 	    objc_add_instance_variable (decl);
26116 
26117 	  /* Reset PREFIX_ATTRIBUTES.  */
26118 	  while (attributes && TREE_CHAIN (attributes) != first_attribute)
26119 	    attributes = TREE_CHAIN (attributes);
26120 	  if (attributes)
26121 	    TREE_CHAIN (attributes) = NULL_TREE;
26122 
26123 	  token = cp_lexer_peek_token (parser->lexer);
26124 
26125 	  if (token->type == CPP_COMMA)
26126 	    {
26127 	      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
26128 	      continue;
26129 	    }
26130 	  break;
26131 	}
26132 
26133       cp_parser_consume_semicolon_at_end_of_statement (parser);
26134       token = cp_lexer_peek_token (parser->lexer);
26135     }
26136 
26137   if (token->keyword == RID_AT_END)
26138     cp_parser_error (parser, "expected %<}%>");
26139 
26140   /* Do not consume the RID_AT_END, so it will be read again as terminating
26141      the @interface of @implementation.  */
26142   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26143     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
26144 
26145   /* For historical reasons, we accept an optional semicolon.  */
26146   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26147     cp_lexer_consume_token (parser->lexer);
26148 }
26149 
26150 /* Parse an Objective-C protocol declaration.  */
26151 
26152 static void
cp_parser_objc_protocol_declaration(cp_parser * parser,tree attributes)26153 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26154 {
26155   tree proto, protorefs;
26156   cp_token *tok;
26157 
26158   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
26159   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26160     {
26161       tok = cp_lexer_peek_token (parser->lexer);
26162       error_at (tok->location, "identifier expected after %<@protocol%>");
26163       cp_parser_consume_semicolon_at_end_of_statement (parser);
26164       return;
26165     }
26166 
26167   /* See if we have a forward declaration or a definition.  */
26168   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26169 
26170   /* Try a forward declaration first.  */
26171   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26172     {
26173       while (true)
26174 	{
26175 	  tree id;
26176 
26177 	  id = cp_parser_identifier (parser);
26178 	  if (id == error_mark_node)
26179 	    break;
26180 
26181 	  objc_declare_protocol (id, attributes);
26182 
26183 	  if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26184 	    cp_lexer_consume_token (parser->lexer);
26185 	  else
26186 	    break;
26187 	}
26188       cp_parser_consume_semicolon_at_end_of_statement (parser);
26189     }
26190 
26191   /* Ok, we got a full-fledged definition (or at least should).  */
26192   else
26193     {
26194       proto = cp_parser_identifier (parser);
26195       protorefs = cp_parser_objc_protocol_refs_opt (parser);
26196       objc_start_protocol (proto, protorefs, attributes);
26197       cp_parser_objc_method_prototype_list (parser);
26198     }
26199 }
26200 
26201 /* Parse an Objective-C superclass or category.  */
26202 
26203 static void
cp_parser_objc_superclass_or_category(cp_parser * parser,bool iface_p,tree * super,tree * categ,bool * is_class_extension)26204 cp_parser_objc_superclass_or_category (cp_parser *parser,
26205 				       bool iface_p,
26206 				       tree *super,
26207 				       tree *categ, bool *is_class_extension)
26208 {
26209   cp_token *next = cp_lexer_peek_token (parser->lexer);
26210 
26211   *super = *categ = NULL_TREE;
26212   *is_class_extension = false;
26213   if (next->type == CPP_COLON)
26214     {
26215       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
26216       *super = cp_parser_identifier (parser);
26217     }
26218   else if (next->type == CPP_OPEN_PAREN)
26219     {
26220       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
26221 
26222       /* If there is no category name, and this is an @interface, we
26223 	 have a class extension.  */
26224       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26225 	{
26226 	  *categ = NULL_TREE;
26227 	  *is_class_extension = true;
26228 	}
26229       else
26230 	*categ = cp_parser_identifier (parser);
26231 
26232       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26233     }
26234 }
26235 
26236 /* Parse an Objective-C class interface.  */
26237 
26238 static void
cp_parser_objc_class_interface(cp_parser * parser,tree attributes)26239 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26240 {
26241   tree name, super, categ, protos;
26242   bool is_class_extension;
26243 
26244   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
26245   name = cp_parser_identifier (parser);
26246   if (name == error_mark_node)
26247     {
26248       /* It's hard to recover because even if valid @interface stuff
26249 	 is to follow, we can't compile it (or validate it) if we
26250 	 don't even know which class it refers to.  Let's assume this
26251 	 was a stray '@interface' token in the stream and skip it.
26252       */
26253       return;
26254     }
26255   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26256 					 &is_class_extension);
26257   protos = cp_parser_objc_protocol_refs_opt (parser);
26258 
26259   /* We have either a class or a category on our hands.  */
26260   if (categ || is_class_extension)
26261     objc_start_category_interface (name, categ, protos, attributes);
26262   else
26263     {
26264       objc_start_class_interface (name, super, protos, attributes);
26265       /* Handle instance variable declarations, if any.  */
26266       cp_parser_objc_class_ivars (parser);
26267       objc_continue_interface ();
26268     }
26269 
26270   cp_parser_objc_method_prototype_list (parser);
26271 }
26272 
26273 /* Parse an Objective-C class implementation.  */
26274 
26275 static void
cp_parser_objc_class_implementation(cp_parser * parser)26276 cp_parser_objc_class_implementation (cp_parser* parser)
26277 {
26278   tree name, super, categ;
26279   bool is_class_extension;
26280 
26281   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
26282   name = cp_parser_identifier (parser);
26283   if (name == error_mark_node)
26284     {
26285       /* It's hard to recover because even if valid @implementation
26286 	 stuff is to follow, we can't compile it (or validate it) if
26287 	 we don't even know which class it refers to.  Let's assume
26288 	 this was a stray '@implementation' token in the stream and
26289 	 skip it.
26290       */
26291       return;
26292     }
26293   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26294 					 &is_class_extension);
26295 
26296   /* We have either a class or a category on our hands.  */
26297   if (categ)
26298     objc_start_category_implementation (name, categ);
26299   else
26300     {
26301       objc_start_class_implementation (name, super);
26302       /* Handle instance variable declarations, if any.  */
26303       cp_parser_objc_class_ivars (parser);
26304       objc_continue_implementation ();
26305     }
26306 
26307   cp_parser_objc_method_definition_list (parser);
26308 }
26309 
26310 /* Consume the @end token and finish off the implementation.  */
26311 
26312 static void
cp_parser_objc_end_implementation(cp_parser * parser)26313 cp_parser_objc_end_implementation (cp_parser* parser)
26314 {
26315   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
26316   objc_finish_implementation ();
26317 }
26318 
26319 /* Parse an Objective-C declaration.  */
26320 
26321 static void
cp_parser_objc_declaration(cp_parser * parser,tree attributes)26322 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26323 {
26324   /* Try to figure out what kind of declaration is present.  */
26325   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26326 
26327   if (attributes)
26328     switch (kwd->keyword)
26329       {
26330 	case RID_AT_ALIAS:
26331 	case RID_AT_CLASS:
26332 	case RID_AT_END:
26333 	  error_at (kwd->location, "attributes may not be specified before"
26334 	            " the %<@%D%> Objective-C++ keyword",
26335 		    kwd->u.value);
26336 	  attributes = NULL;
26337 	  break;
26338 	case RID_AT_IMPLEMENTATION:
26339 	  warning_at (kwd->location, OPT_Wattributes,
26340 		      "prefix attributes are ignored before %<@%D%>",
26341 		      kwd->u.value);
26342 	  attributes = NULL;
26343 	default:
26344 	  break;
26345       }
26346 
26347   switch (kwd->keyword)
26348     {
26349     case RID_AT_ALIAS:
26350       cp_parser_objc_alias_declaration (parser);
26351       break;
26352     case RID_AT_CLASS:
26353       cp_parser_objc_class_declaration (parser);
26354       break;
26355     case RID_AT_PROTOCOL:
26356       cp_parser_objc_protocol_declaration (parser, attributes);
26357       break;
26358     case RID_AT_INTERFACE:
26359       cp_parser_objc_class_interface (parser, attributes);
26360       break;
26361     case RID_AT_IMPLEMENTATION:
26362       cp_parser_objc_class_implementation (parser);
26363       break;
26364     case RID_AT_END:
26365       cp_parser_objc_end_implementation (parser);
26366       break;
26367     default:
26368       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26369 		kwd->u.value);
26370       cp_parser_skip_to_end_of_block_or_statement (parser);
26371     }
26372 }
26373 
26374 /* Parse an Objective-C try-catch-finally statement.
26375 
26376    objc-try-catch-finally-stmt:
26377      @try compound-statement objc-catch-clause-seq [opt]
26378        objc-finally-clause [opt]
26379 
26380    objc-catch-clause-seq:
26381      objc-catch-clause objc-catch-clause-seq [opt]
26382 
26383    objc-catch-clause:
26384      @catch ( objc-exception-declaration ) compound-statement
26385 
26386    objc-finally-clause:
26387      @finally compound-statement
26388 
26389    objc-exception-declaration:
26390      parameter-declaration
26391      '...'
26392 
26393    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26394 
26395    Returns NULL_TREE.
26396 
26397    PS: This function is identical to c_parser_objc_try_catch_finally_statement
26398    for C.  Keep them in sync.  */
26399 
26400 static tree
cp_parser_objc_try_catch_finally_statement(cp_parser * parser)26401 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26402 {
26403   location_t location;
26404   tree stmt;
26405 
26406   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26407   location = cp_lexer_peek_token (parser->lexer)->location;
26408   objc_maybe_warn_exceptions (location);
26409   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26410      node, lest it get absorbed into the surrounding block.  */
26411   stmt = push_stmt_list ();
26412   cp_parser_compound_statement (parser, NULL, false, false);
26413   objc_begin_try_stmt (location, pop_stmt_list (stmt));
26414 
26415   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26416     {
26417       cp_parameter_declarator *parm;
26418       tree parameter_declaration = error_mark_node;
26419       bool seen_open_paren = false;
26420 
26421       cp_lexer_consume_token (parser->lexer);
26422       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26423 	seen_open_paren = true;
26424       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26425 	{
26426 	  /* We have "@catch (...)" (where the '...' are literally
26427 	     what is in the code).  Skip the '...'.
26428 	     parameter_declaration is set to NULL_TREE, and
26429 	     objc_being_catch_clauses() knows that that means
26430 	     '...'.  */
26431 	  cp_lexer_consume_token (parser->lexer);
26432 	  parameter_declaration = NULL_TREE;
26433 	}
26434       else
26435 	{
26436 	  /* We have "@catch (NSException *exception)" or something
26437 	     like that.  Parse the parameter declaration.  */
26438 	  parm = cp_parser_parameter_declaration (parser, false, NULL);
26439 	  if (parm == NULL)
26440 	    parameter_declaration = error_mark_node;
26441 	  else
26442 	    parameter_declaration = grokdeclarator (parm->declarator,
26443 						    &parm->decl_specifiers,
26444 						    PARM, /*initialized=*/0,
26445 						    /*attrlist=*/NULL);
26446 	}
26447       if (seen_open_paren)
26448 	cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26449       else
26450 	{
26451 	  /* If there was no open parenthesis, we are recovering from
26452 	     an error, and we are trying to figure out what mistake
26453 	     the user has made.  */
26454 
26455 	  /* If there is an immediate closing parenthesis, the user
26456 	     probably forgot the opening one (ie, they typed "@catch
26457 	     NSException *e)".  Parse the closing parenthesis and keep
26458 	     going.  */
26459 	  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26460 	    cp_lexer_consume_token (parser->lexer);
26461 
26462 	  /* If these is no immediate closing parenthesis, the user
26463 	     probably doesn't know that parenthesis are required at
26464 	     all (ie, they typed "@catch NSException *e").  So, just
26465 	     forget about the closing parenthesis and keep going.  */
26466 	}
26467       objc_begin_catch_clause (parameter_declaration);
26468       cp_parser_compound_statement (parser, NULL, false, false);
26469       objc_finish_catch_clause ();
26470     }
26471   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26472     {
26473       cp_lexer_consume_token (parser->lexer);
26474       location = cp_lexer_peek_token (parser->lexer)->location;
26475       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26476 	 node, lest it get absorbed into the surrounding block.  */
26477       stmt = push_stmt_list ();
26478       cp_parser_compound_statement (parser, NULL, false, false);
26479       objc_build_finally_clause (location, pop_stmt_list (stmt));
26480     }
26481 
26482   return objc_finish_try_stmt ();
26483 }
26484 
26485 /* Parse an Objective-C synchronized statement.
26486 
26487    objc-synchronized-stmt:
26488      @synchronized ( expression ) compound-statement
26489 
26490    Returns NULL_TREE.  */
26491 
26492 static tree
cp_parser_objc_synchronized_statement(cp_parser * parser)26493 cp_parser_objc_synchronized_statement (cp_parser *parser)
26494 {
26495   location_t location;
26496   tree lock, stmt;
26497 
26498   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26499 
26500   location = cp_lexer_peek_token (parser->lexer)->location;
26501   objc_maybe_warn_exceptions (location);
26502   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26503   lock = cp_parser_expression (parser, false, NULL);
26504   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26505 
26506   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26507      node, lest it get absorbed into the surrounding block.  */
26508   stmt = push_stmt_list ();
26509   cp_parser_compound_statement (parser, NULL, false, false);
26510 
26511   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26512 }
26513 
26514 /* Parse an Objective-C throw statement.
26515 
26516    objc-throw-stmt:
26517      @throw assignment-expression [opt] ;
26518 
26519    Returns a constructed '@throw' statement.  */
26520 
26521 static tree
cp_parser_objc_throw_statement(cp_parser * parser)26522 cp_parser_objc_throw_statement (cp_parser *parser)
26523 {
26524   tree expr = NULL_TREE;
26525   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26526 
26527   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26528 
26529   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26530     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26531 
26532   cp_parser_consume_semicolon_at_end_of_statement (parser);
26533 
26534   return objc_build_throw_stmt (loc, expr);
26535 }
26536 
26537 /* Parse an Objective-C statement.  */
26538 
26539 static tree
cp_parser_objc_statement(cp_parser * parser)26540 cp_parser_objc_statement (cp_parser * parser)
26541 {
26542   /* Try to figure out what kind of declaration is present.  */
26543   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26544 
26545   switch (kwd->keyword)
26546     {
26547     case RID_AT_TRY:
26548       return cp_parser_objc_try_catch_finally_statement (parser);
26549     case RID_AT_SYNCHRONIZED:
26550       return cp_parser_objc_synchronized_statement (parser);
26551     case RID_AT_THROW:
26552       return cp_parser_objc_throw_statement (parser);
26553     default:
26554       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26555 	       kwd->u.value);
26556       cp_parser_skip_to_end_of_block_or_statement (parser);
26557     }
26558 
26559   return error_mark_node;
26560 }
26561 
26562 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26563    look ahead to see if an objc keyword follows the attributes.  This
26564    is to detect the use of prefix attributes on ObjC @interface and
26565    @protocol.  */
26566 
26567 static bool
cp_parser_objc_valid_prefix_attributes(cp_parser * parser,tree * attrib)26568 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
26569 {
26570   cp_lexer_save_tokens (parser->lexer);
26571   *attrib = cp_parser_attributes_opt (parser);
26572   gcc_assert (*attrib);
26573   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
26574     {
26575       cp_lexer_commit_tokens (parser->lexer);
26576       return true;
26577     }
26578   cp_lexer_rollback_tokens (parser->lexer);
26579   return false;
26580 }
26581 
26582 /* This routine is a minimal replacement for
26583    c_parser_struct_declaration () used when parsing the list of
26584    types/names or ObjC++ properties.  For example, when parsing the
26585    code
26586 
26587    @property (readonly) int a, b, c;
26588 
26589    this function is responsible for parsing "int a, int b, int c" and
26590    returning the declarations as CHAIN of DECLs.
26591 
26592    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
26593    similar parsing.  */
26594 static tree
cp_parser_objc_struct_declaration(cp_parser * parser)26595 cp_parser_objc_struct_declaration (cp_parser *parser)
26596 {
26597   tree decls = NULL_TREE;
26598   cp_decl_specifier_seq declspecs;
26599   int decl_class_or_enum_p;
26600   tree prefix_attributes;
26601 
26602   cp_parser_decl_specifier_seq (parser,
26603 				CP_PARSER_FLAGS_NONE,
26604 				&declspecs,
26605 				&decl_class_or_enum_p);
26606 
26607   if (declspecs.type == error_mark_node)
26608     return error_mark_node;
26609 
26610   /* auto, register, static, extern, mutable.  */
26611   if (declspecs.storage_class != sc_none)
26612     {
26613       cp_parser_error (parser, "invalid type for property");
26614       declspecs.storage_class = sc_none;
26615     }
26616 
26617   /* thread_local.  */
26618   if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26619     {
26620       cp_parser_error (parser, "invalid type for property");
26621       declspecs.locations[ds_thread] = 0;
26622     }
26623 
26624   /* typedef.  */
26625   if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26626     {
26627       cp_parser_error (parser, "invalid type for property");
26628       declspecs.locations[ds_typedef] = 0;
26629     }
26630 
26631   prefix_attributes = declspecs.attributes;
26632   declspecs.attributes = NULL_TREE;
26633 
26634   /* Keep going until we hit the `;' at the end of the declaration. */
26635   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26636     {
26637       tree attributes, first_attribute, decl;
26638       cp_declarator *declarator;
26639       cp_token *token;
26640 
26641       /* Parse the declarator.  */
26642       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26643 					 NULL, NULL, false);
26644 
26645       /* Look for attributes that apply to the ivar.  */
26646       attributes = cp_parser_attributes_opt (parser);
26647       /* Remember which attributes are prefix attributes and
26648 	 which are not.  */
26649       first_attribute = attributes;
26650       /* Combine the attributes.  */
26651       attributes = chainon (prefix_attributes, attributes);
26652 
26653       decl = grokfield (declarator, &declspecs,
26654 			NULL_TREE, /*init_const_expr_p=*/false,
26655 			NULL_TREE, attributes);
26656 
26657       if (decl == error_mark_node || decl == NULL_TREE)
26658 	return error_mark_node;
26659 
26660       /* Reset PREFIX_ATTRIBUTES.  */
26661       while (attributes && TREE_CHAIN (attributes) != first_attribute)
26662 	attributes = TREE_CHAIN (attributes);
26663       if (attributes)
26664 	TREE_CHAIN (attributes) = NULL_TREE;
26665 
26666       DECL_CHAIN (decl) = decls;
26667       decls = decl;
26668 
26669       token = cp_lexer_peek_token (parser->lexer);
26670       if (token->type == CPP_COMMA)
26671 	{
26672 	  cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
26673 	  continue;
26674 	}
26675       else
26676 	break;
26677     }
26678   return decls;
26679 }
26680 
26681 /* Parse an Objective-C @property declaration.  The syntax is:
26682 
26683    objc-property-declaration:
26684      '@property' objc-property-attributes[opt] struct-declaration ;
26685 
26686    objc-property-attributes:
26687     '(' objc-property-attribute-list ')'
26688 
26689    objc-property-attribute-list:
26690      objc-property-attribute
26691      objc-property-attribute-list, objc-property-attribute
26692 
26693    objc-property-attribute
26694      'getter' = identifier
26695      'setter' = identifier
26696      'readonly'
26697      'readwrite'
26698      'assign'
26699      'retain'
26700      'copy'
26701      'nonatomic'
26702 
26703   For example:
26704     @property NSString *name;
26705     @property (readonly) id object;
26706     @property (retain, nonatomic, getter=getTheName) id name;
26707     @property int a, b, c;
26708 
26709    PS: This function is identical to
26710    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
26711 static void
cp_parser_objc_at_property_declaration(cp_parser * parser)26712 cp_parser_objc_at_property_declaration (cp_parser *parser)
26713 {
26714   /* The following variables hold the attributes of the properties as
26715      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
26716      seen.  When we see an attribute, we set them to 'true' (if they
26717      are boolean properties) or to the identifier (if they have an
26718      argument, ie, for getter and setter).  Note that here we only
26719      parse the list of attributes, check the syntax and accumulate the
26720      attributes that we find.  objc_add_property_declaration() will
26721      then process the information.  */
26722   bool property_assign = false;
26723   bool property_copy = false;
26724   tree property_getter_ident = NULL_TREE;
26725   bool property_nonatomic = false;
26726   bool property_readonly = false;
26727   bool property_readwrite = false;
26728   bool property_retain = false;
26729   tree property_setter_ident = NULL_TREE;
26730 
26731   /* 'properties' is the list of properties that we read.  Usually a
26732      single one, but maybe more (eg, in "@property int a, b, c;" there
26733      are three).  */
26734   tree properties;
26735   location_t loc;
26736 
26737   loc = cp_lexer_peek_token (parser->lexer)->location;
26738 
26739   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
26740 
26741   /* Parse the optional attribute list...  */
26742   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26743     {
26744       /* Eat the '('.  */
26745       cp_lexer_consume_token (parser->lexer);
26746 
26747       while (true)
26748 	{
26749 	  bool syntax_error = false;
26750 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
26751       	  enum rid keyword;
26752 
26753 	  if (token->type != CPP_NAME)
26754 	    {
26755 	      cp_parser_error (parser, "expected identifier");
26756 	      break;
26757 	    }
26758 	  keyword = C_RID_CODE (token->u.value);
26759 	  cp_lexer_consume_token (parser->lexer);
26760 	  switch (keyword)
26761 	    {
26762 	    case RID_ASSIGN:    property_assign = true;    break;
26763 	    case RID_COPY:      property_copy = true;      break;
26764 	    case RID_NONATOMIC: property_nonatomic = true; break;
26765 	    case RID_READONLY:  property_readonly = true;  break;
26766 	    case RID_READWRITE: property_readwrite = true; break;
26767 	    case RID_RETAIN:    property_retain = true;    break;
26768 
26769 	    case RID_GETTER:
26770 	    case RID_SETTER:
26771 	      if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26772 		{
26773 		  if (keyword == RID_GETTER)
26774 		    cp_parser_error (parser,
26775 				     "missing %<=%> (after %<getter%> attribute)");
26776 		  else
26777 		    cp_parser_error (parser,
26778 				     "missing %<=%> (after %<setter%> attribute)");
26779 		  syntax_error = true;
26780 		  break;
26781 		}
26782 	      cp_lexer_consume_token (parser->lexer); /* eat the = */
26783 	      if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
26784 		{
26785 		  cp_parser_error (parser, "expected identifier");
26786 		  syntax_error = true;
26787 		  break;
26788 		}
26789 	      if (keyword == RID_SETTER)
26790 		{
26791 		  if (property_setter_ident != NULL_TREE)
26792 		    {
26793 		      cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
26794 		      cp_lexer_consume_token (parser->lexer);
26795 		    }
26796 		  else
26797 		    property_setter_ident = cp_parser_objc_selector (parser);
26798 		  if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26799 		    cp_parser_error (parser, "setter name must terminate with %<:%>");
26800 		  else
26801 		    cp_lexer_consume_token (parser->lexer);
26802 		}
26803 	      else
26804 		{
26805 		  if (property_getter_ident != NULL_TREE)
26806 		    {
26807 		      cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
26808 		      cp_lexer_consume_token (parser->lexer);
26809 		    }
26810 		  else
26811 		    property_getter_ident = cp_parser_objc_selector (parser);
26812 		}
26813 	      break;
26814 	    default:
26815 	      cp_parser_error (parser, "unknown property attribute");
26816 	      syntax_error = true;
26817 	      break;
26818 	    }
26819 
26820 	  if (syntax_error)
26821 	    break;
26822 
26823 	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26824 	    cp_lexer_consume_token (parser->lexer);
26825 	  else
26826 	    break;
26827 	}
26828 
26829       /* FIXME: "@property (setter, assign);" will generate a spurious
26830 	 "error: expected ‘)’ before ‘,’ token".  This is because
26831 	 cp_parser_require, unlike the C counterpart, will produce an
26832 	 error even if we are in error recovery.  */
26833       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26834 	{
26835 	  cp_parser_skip_to_closing_parenthesis (parser,
26836 						 /*recovering=*/true,
26837 						 /*or_comma=*/false,
26838 						 /*consume_paren=*/true);
26839 	}
26840     }
26841 
26842   /* ... and the property declaration(s).  */
26843   properties = cp_parser_objc_struct_declaration (parser);
26844 
26845   if (properties == error_mark_node)
26846     {
26847       cp_parser_skip_to_end_of_statement (parser);
26848       /* If the next token is now a `;', consume it.  */
26849       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26850 	cp_lexer_consume_token (parser->lexer);
26851       return;
26852     }
26853 
26854   if (properties == NULL_TREE)
26855     cp_parser_error (parser, "expected identifier");
26856   else
26857     {
26858       /* Comma-separated properties are chained together in
26859 	 reverse order; add them one by one.  */
26860       properties = nreverse (properties);
26861 
26862       for (; properties; properties = TREE_CHAIN (properties))
26863 	objc_add_property_declaration (loc, copy_node (properties),
26864 				       property_readonly, property_readwrite,
26865 				       property_assign, property_retain,
26866 				       property_copy, property_nonatomic,
26867 				       property_getter_ident, property_setter_ident);
26868     }
26869 
26870   cp_parser_consume_semicolon_at_end_of_statement (parser);
26871 }
26872 
26873 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
26874 
26875    objc-synthesize-declaration:
26876      @synthesize objc-synthesize-identifier-list ;
26877 
26878    objc-synthesize-identifier-list:
26879      objc-synthesize-identifier
26880      objc-synthesize-identifier-list, objc-synthesize-identifier
26881 
26882    objc-synthesize-identifier
26883      identifier
26884      identifier = identifier
26885 
26886   For example:
26887     @synthesize MyProperty;
26888     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
26889 
26890   PS: This function is identical to c_parser_objc_at_synthesize_declaration
26891   for C.  Keep them in sync.
26892 */
26893 static void
cp_parser_objc_at_synthesize_declaration(cp_parser * parser)26894 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
26895 {
26896   tree list = NULL_TREE;
26897   location_t loc;
26898   loc = cp_lexer_peek_token (parser->lexer)->location;
26899 
26900   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
26901   while (true)
26902     {
26903       tree property, ivar;
26904       property = cp_parser_identifier (parser);
26905       if (property == error_mark_node)
26906 	{
26907 	  cp_parser_consume_semicolon_at_end_of_statement (parser);
26908 	  return;
26909 	}
26910       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
26911 	{
26912 	  cp_lexer_consume_token (parser->lexer);
26913 	  ivar = cp_parser_identifier (parser);
26914 	  if (ivar == error_mark_node)
26915 	    {
26916 	      cp_parser_consume_semicolon_at_end_of_statement (parser);
26917 	      return;
26918 	    }
26919 	}
26920       else
26921 	ivar = NULL_TREE;
26922       list = chainon (list, build_tree_list (ivar, property));
26923       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26924 	cp_lexer_consume_token (parser->lexer);
26925       else
26926 	break;
26927     }
26928   cp_parser_consume_semicolon_at_end_of_statement (parser);
26929   objc_add_synthesize_declaration (loc, list);
26930 }
26931 
26932 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
26933 
26934    objc-dynamic-declaration:
26935      @dynamic identifier-list ;
26936 
26937    For example:
26938      @dynamic MyProperty;
26939      @dynamic MyProperty, AnotherProperty;
26940 
26941   PS: This function is identical to c_parser_objc_at_dynamic_declaration
26942   for C.  Keep them in sync.
26943 */
26944 static void
cp_parser_objc_at_dynamic_declaration(cp_parser * parser)26945 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
26946 {
26947   tree list = NULL_TREE;
26948   location_t loc;
26949   loc = cp_lexer_peek_token (parser->lexer)->location;
26950 
26951   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
26952   while (true)
26953     {
26954       tree property;
26955       property = cp_parser_identifier (parser);
26956       if (property == error_mark_node)
26957 	{
26958 	  cp_parser_consume_semicolon_at_end_of_statement (parser);
26959 	  return;
26960 	}
26961       list = chainon (list, build_tree_list (NULL, property));
26962       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26963 	cp_lexer_consume_token (parser->lexer);
26964       else
26965 	break;
26966     }
26967   cp_parser_consume_semicolon_at_end_of_statement (parser);
26968   objc_add_dynamic_declaration (loc, list);
26969 }
26970 
26971 
26972 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines.  */
26973 
26974 /* Returns name of the next clause.
26975    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
26976    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
26977    returned and the token is consumed.  */
26978 
26979 static pragma_omp_clause
cp_parser_omp_clause_name(cp_parser * parser)26980 cp_parser_omp_clause_name (cp_parser *parser)
26981 {
26982   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
26983 
26984   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
26985     result = PRAGMA_OMP_CLAUSE_IF;
26986   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
26987     result = PRAGMA_OMP_CLAUSE_DEFAULT;
26988   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
26989     result = PRAGMA_OMP_CLAUSE_PRIVATE;
26990   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26991     result = PRAGMA_OMP_CLAUSE_FOR;
26992   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26993     {
26994       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26995       const char *p = IDENTIFIER_POINTER (id);
26996 
26997       switch (p[0])
26998 	{
26999 	case 'a':
27000 	  if (!strcmp ("aligned", p))
27001 	    result = PRAGMA_OMP_CLAUSE_ALIGNED;
27002 	  break;
27003 	case 'c':
27004 	  if (!strcmp ("collapse", p))
27005 	    result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27006 	  else if (!strcmp ("copyin", p))
27007 	    result = PRAGMA_OMP_CLAUSE_COPYIN;
27008 	  else if (!strcmp ("copyprivate", p))
27009 	    result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27010 	  break;
27011 	case 'd':
27012 	  if (!strcmp ("depend", p))
27013 	    result = PRAGMA_OMP_CLAUSE_DEPEND;
27014 	  else if (!strcmp ("device", p))
27015 	    result = PRAGMA_OMP_CLAUSE_DEVICE;
27016 	  else if (!strcmp ("dist_schedule", p))
27017 	    result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27018 	  break;
27019 	case 'f':
27020 	  if (!strcmp ("final", p))
27021 	    result = PRAGMA_OMP_CLAUSE_FINAL;
27022 	  else if (!strcmp ("firstprivate", p))
27023 	    result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27024 	  else if (!strcmp ("from", p))
27025 	    result = PRAGMA_OMP_CLAUSE_FROM;
27026 	  break;
27027 	case 'i':
27028 	  if (!strcmp ("inbranch", p))
27029 	    result = PRAGMA_OMP_CLAUSE_INBRANCH;
27030 	  break;
27031 	case 'l':
27032 	  if (!strcmp ("lastprivate", p))
27033 	    result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27034 	  else if (!strcmp ("linear", p))
27035 	    result = PRAGMA_OMP_CLAUSE_LINEAR;
27036 	  break;
27037 	case 'm':
27038 	  if (!strcmp ("map", p))
27039 	    result = PRAGMA_OMP_CLAUSE_MAP;
27040 	  else if (!strcmp ("mergeable", p))
27041 	    result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27042 	  else if (flag_cilkplus && !strcmp ("mask", p))
27043 	    result = PRAGMA_CILK_CLAUSE_MASK;
27044 	  break;
27045 	case 'n':
27046 	  if (!strcmp ("notinbranch", p))
27047 	    result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27048 	  else if (!strcmp ("nowait", p))
27049 	    result = PRAGMA_OMP_CLAUSE_NOWAIT;
27050 	  else if (flag_cilkplus && !strcmp ("nomask", p))
27051 	    result = PRAGMA_CILK_CLAUSE_NOMASK;
27052 	  else if (!strcmp ("num_teams", p))
27053 	    result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27054 	  else if (!strcmp ("num_threads", p))
27055 	    result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27056 	  break;
27057 	case 'o':
27058 	  if (!strcmp ("ordered", p))
27059 	    result = PRAGMA_OMP_CLAUSE_ORDERED;
27060 	  break;
27061 	case 'p':
27062 	  if (!strcmp ("parallel", p))
27063 	    result = PRAGMA_OMP_CLAUSE_PARALLEL;
27064 	  else if (!strcmp ("proc_bind", p))
27065 	    result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27066 	  break;
27067 	case 'r':
27068 	  if (!strcmp ("reduction", p))
27069 	    result = PRAGMA_OMP_CLAUSE_REDUCTION;
27070 	  break;
27071 	case 's':
27072 	  if (!strcmp ("safelen", p))
27073 	    result = PRAGMA_OMP_CLAUSE_SAFELEN;
27074 	  else if (!strcmp ("schedule", p))
27075 	    result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27076 	  else if (!strcmp ("sections", p))
27077 	    result = PRAGMA_OMP_CLAUSE_SECTIONS;
27078 	  else if (!strcmp ("shared", p))
27079 	    result = PRAGMA_OMP_CLAUSE_SHARED;
27080 	  else if (!strcmp ("simdlen", p))
27081 	    result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27082 	  break;
27083 	case 't':
27084 	  if (!strcmp ("taskgroup", p))
27085 	    result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27086 	  else if (!strcmp ("thread_limit", p))
27087 	    result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27088 	  else if (!strcmp ("to", p))
27089 	    result = PRAGMA_OMP_CLAUSE_TO;
27090 	  break;
27091 	case 'u':
27092 	  if (!strcmp ("uniform", p))
27093 	    result = PRAGMA_OMP_CLAUSE_UNIFORM;
27094 	  else if (!strcmp ("untied", p))
27095 	    result = PRAGMA_OMP_CLAUSE_UNTIED;
27096 	  break;
27097 	case 'v':
27098 	  if (flag_cilkplus && !strcmp ("vectorlength", p))
27099 	    result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27100 	  break;
27101 	}
27102     }
27103 
27104   if (result != PRAGMA_OMP_CLAUSE_NONE)
27105     cp_lexer_consume_token (parser->lexer);
27106 
27107   return result;
27108 }
27109 
27110 /* Validate that a clause of the given type does not already exist.  */
27111 
27112 static void
check_no_duplicate_clause(tree clauses,enum omp_clause_code code,const char * name,location_t location)27113 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27114 			   const char *name, location_t location)
27115 {
27116   tree c;
27117 
27118   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27119     if (OMP_CLAUSE_CODE (c) == code)
27120       {
27121 	error_at (location, "too many %qs clauses", name);
27122 	break;
27123       }
27124 }
27125 
27126 /* OpenMP 2.5:
27127    variable-list:
27128      identifier
27129      variable-list , identifier
27130 
27131    In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27132    colon).  An opening parenthesis will have been consumed by the caller.
27133 
27134    If KIND is nonzero, create the appropriate node and install the decl
27135    in OMP_CLAUSE_DECL and add the node to the head of the list.
27136 
27137    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27138    return the list created.
27139 
27140    COLON can be NULL if only closing parenthesis should end the list,
27141    or pointer to bool which will receive false if the list is terminated
27142    by closing parenthesis or true if the list is terminated by colon.  */
27143 
27144 static tree
cp_parser_omp_var_list_no_open(cp_parser * parser,enum omp_clause_code kind,tree list,bool * colon)27145 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27146 				tree list, bool *colon)
27147 {
27148   cp_token *token;
27149   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27150   if (colon)
27151     {
27152       parser->colon_corrects_to_scope_p = false;
27153       *colon = false;
27154     }
27155   while (1)
27156     {
27157       tree name, decl;
27158 
27159       token = cp_lexer_peek_token (parser->lexer);
27160       name = cp_parser_id_expression (parser, /*template_p=*/false,
27161 				      /*check_dependency_p=*/true,
27162 				      /*template_p=*/NULL,
27163 				      /*declarator_p=*/false,
27164 				      /*optional_p=*/false);
27165       if (name == error_mark_node)
27166 	goto skip_comma;
27167 
27168       decl = cp_parser_lookup_name_simple (parser, name, token->location);
27169       if (decl == error_mark_node)
27170 	cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27171 				     token->location);
27172       else if (kind != 0)
27173 	{
27174 	  switch (kind)
27175 	    {
27176 	    case OMP_CLAUSE_MAP:
27177 	    case OMP_CLAUSE_FROM:
27178 	    case OMP_CLAUSE_TO:
27179 	    case OMP_CLAUSE_DEPEND:
27180 	      while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27181 		{
27182 		  tree low_bound = NULL_TREE, length = NULL_TREE;
27183 
27184 		  parser->colon_corrects_to_scope_p = false;
27185 		  cp_lexer_consume_token (parser->lexer);
27186 		  if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27187 		    low_bound = cp_parser_expression (parser, /*cast_p=*/false,
27188 						      NULL);
27189 		  if (!colon)
27190 		    parser->colon_corrects_to_scope_p
27191 		      = saved_colon_corrects_to_scope_p;
27192 		  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27193 		    length = integer_one_node;
27194 		  else
27195 		    {
27196 		      /* Look for `:'.  */
27197 		      if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27198 			goto skip_comma;
27199 		      if (!cp_lexer_next_token_is (parser->lexer,
27200 						   CPP_CLOSE_SQUARE))
27201 			length = cp_parser_expression (parser,
27202 						       /*cast_p=*/false,
27203 						       NULL);
27204 		    }
27205 		  /* Look for the closing `]'.  */
27206 		  if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27207 					  RT_CLOSE_SQUARE))
27208 		    goto skip_comma;
27209 		  decl = tree_cons (low_bound, length, decl);
27210 		}
27211 	      break;
27212 	    default:
27213 	      break;
27214 	    }
27215 
27216 	  tree u = build_omp_clause (token->location, kind);
27217 	  OMP_CLAUSE_DECL (u) = decl;
27218 	  OMP_CLAUSE_CHAIN (u) = list;
27219 	  list = u;
27220 	}
27221       else
27222 	list = tree_cons (decl, NULL_TREE, list);
27223 
27224     get_comma:
27225       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27226 	break;
27227       cp_lexer_consume_token (parser->lexer);
27228     }
27229 
27230   if (colon)
27231     parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27232 
27233   if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27234     {
27235       *colon = true;
27236       cp_parser_require (parser, CPP_COLON, RT_COLON);
27237       return list;
27238     }
27239 
27240   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27241     {
27242       int ending;
27243 
27244       /* Try to resync to an unnested comma.  Copied from
27245 	 cp_parser_parenthesized_expression_list.  */
27246     skip_comma:
27247       if (colon)
27248 	parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27249       ending = cp_parser_skip_to_closing_parenthesis (parser,
27250 						      /*recovering=*/true,
27251 						      /*or_comma=*/true,
27252 						      /*consume_paren=*/true);
27253       if (ending < 0)
27254 	goto get_comma;
27255     }
27256 
27257   return list;
27258 }
27259 
27260 /* Similarly, but expect leading and trailing parenthesis.  This is a very
27261    common case for omp clauses.  */
27262 
27263 static tree
cp_parser_omp_var_list(cp_parser * parser,enum omp_clause_code kind,tree list)27264 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27265 {
27266   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27267     return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27268   return list;
27269 }
27270 
27271 /* OpenMP 3.0:
27272    collapse ( constant-expression ) */
27273 
27274 static tree
cp_parser_omp_clause_collapse(cp_parser * parser,tree list,location_t location)27275 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27276 {
27277   tree c, num;
27278   location_t loc;
27279   HOST_WIDE_INT n;
27280 
27281   loc = cp_lexer_peek_token (parser->lexer)->location;
27282   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27283     return list;
27284 
27285   num = cp_parser_constant_expression (parser, false, NULL);
27286 
27287   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27288     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27289 					   /*or_comma=*/false,
27290 					   /*consume_paren=*/true);
27291 
27292   if (num == error_mark_node)
27293     return list;
27294   num = fold_non_dependent_expr (num);
27295   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27296       || !tree_fits_shwi_p (num)
27297       || (n = tree_to_shwi (num)) <= 0
27298       || (int) n != n)
27299     {
27300       error_at (loc, "collapse argument needs positive constant integer expression");
27301       return list;
27302     }
27303 
27304   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27305   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27306   OMP_CLAUSE_CHAIN (c) = list;
27307   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27308 
27309   return c;
27310 }
27311 
27312 /* OpenMP 2.5:
27313    default ( shared | none ) */
27314 
27315 static tree
cp_parser_omp_clause_default(cp_parser * parser,tree list,location_t location)27316 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27317 {
27318   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27319   tree c;
27320 
27321   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27322     return list;
27323   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27324     {
27325       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27326       const char *p = IDENTIFIER_POINTER (id);
27327 
27328       switch (p[0])
27329 	{
27330 	case 'n':
27331 	  if (strcmp ("none", p) != 0)
27332 	    goto invalid_kind;
27333 	  kind = OMP_CLAUSE_DEFAULT_NONE;
27334 	  break;
27335 
27336 	case 's':
27337 	  if (strcmp ("shared", p) != 0)
27338 	    goto invalid_kind;
27339 	  kind = OMP_CLAUSE_DEFAULT_SHARED;
27340 	  break;
27341 
27342 	default:
27343 	  goto invalid_kind;
27344 	}
27345 
27346       cp_lexer_consume_token (parser->lexer);
27347     }
27348   else
27349     {
27350     invalid_kind:
27351       cp_parser_error (parser, "expected %<none%> or %<shared%>");
27352     }
27353 
27354   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27355     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27356 					   /*or_comma=*/false,
27357 					   /*consume_paren=*/true);
27358 
27359   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27360     return list;
27361 
27362   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27363   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27364   OMP_CLAUSE_CHAIN (c) = list;
27365   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27366 
27367   return c;
27368 }
27369 
27370 /* OpenMP 3.1:
27371    final ( expression ) */
27372 
27373 static tree
cp_parser_omp_clause_final(cp_parser * parser,tree list,location_t location)27374 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27375 {
27376   tree t, c;
27377 
27378   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27379     return list;
27380 
27381   t = cp_parser_condition (parser);
27382 
27383   if (t == error_mark_node
27384       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27385     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27386 					   /*or_comma=*/false,
27387 					   /*consume_paren=*/true);
27388 
27389   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27390 
27391   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27392   OMP_CLAUSE_FINAL_EXPR (c) = t;
27393   OMP_CLAUSE_CHAIN (c) = list;
27394 
27395   return c;
27396 }
27397 
27398 /* OpenMP 2.5:
27399    if ( expression ) */
27400 
27401 static tree
cp_parser_omp_clause_if(cp_parser * parser,tree list,location_t location)27402 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27403 {
27404   tree t, c;
27405 
27406   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27407     return list;
27408 
27409   t = cp_parser_condition (parser);
27410 
27411   if (t == error_mark_node
27412       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27413     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27414 					   /*or_comma=*/false,
27415 					   /*consume_paren=*/true);
27416 
27417   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27418 
27419   c = build_omp_clause (location, OMP_CLAUSE_IF);
27420   OMP_CLAUSE_IF_EXPR (c) = t;
27421   OMP_CLAUSE_CHAIN (c) = list;
27422 
27423   return c;
27424 }
27425 
27426 /* OpenMP 3.1:
27427    mergeable */
27428 
27429 static tree
cp_parser_omp_clause_mergeable(cp_parser *,tree list,location_t location)27430 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27431 				tree list, location_t location)
27432 {
27433   tree c;
27434 
27435   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27436 			     location);
27437 
27438   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27439   OMP_CLAUSE_CHAIN (c) = list;
27440   return c;
27441 }
27442 
27443 /* OpenMP 2.5:
27444    nowait */
27445 
27446 static tree
cp_parser_omp_clause_nowait(cp_parser *,tree list,location_t location)27447 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27448 			     tree list, location_t location)
27449 {
27450   tree c;
27451 
27452   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27453 
27454   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27455   OMP_CLAUSE_CHAIN (c) = list;
27456   return c;
27457 }
27458 
27459 /* OpenMP 2.5:
27460    num_threads ( expression ) */
27461 
27462 static tree
cp_parser_omp_clause_num_threads(cp_parser * parser,tree list,location_t location)27463 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27464 				  location_t location)
27465 {
27466   tree t, c;
27467 
27468   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27469     return list;
27470 
27471   t = cp_parser_expression (parser, false, NULL);
27472 
27473   if (t == error_mark_node
27474       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27475     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27476 					   /*or_comma=*/false,
27477 					   /*consume_paren=*/true);
27478 
27479   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27480 			     "num_threads", location);
27481 
27482   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27483   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27484   OMP_CLAUSE_CHAIN (c) = list;
27485 
27486   return c;
27487 }
27488 
27489 /* OpenMP 2.5:
27490    ordered */
27491 
27492 static tree
cp_parser_omp_clause_ordered(cp_parser *,tree list,location_t location)27493 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27494 			      tree list, location_t location)
27495 {
27496   tree c;
27497 
27498   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27499 			     "ordered", location);
27500 
27501   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27502   OMP_CLAUSE_CHAIN (c) = list;
27503   return c;
27504 }
27505 
27506 /* OpenMP 2.5:
27507    reduction ( reduction-operator : variable-list )
27508 
27509    reduction-operator:
27510      One of: + * - & ^ | && ||
27511 
27512    OpenMP 3.1:
27513 
27514    reduction-operator:
27515      One of: + * - & ^ | && || min max
27516 
27517    OpenMP 4.0:
27518 
27519    reduction-operator:
27520      One of: + * - & ^ | && ||
27521      id-expression  */
27522 
27523 static tree
cp_parser_omp_clause_reduction(cp_parser * parser,tree list)27524 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27525 {
27526   enum tree_code code = ERROR_MARK;
27527   tree nlist, c, id = NULL_TREE;
27528 
27529   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27530     return list;
27531 
27532   switch (cp_lexer_peek_token (parser->lexer)->type)
27533     {
27534     case CPP_PLUS: code = PLUS_EXPR; break;
27535     case CPP_MULT: code = MULT_EXPR; break;
27536     case CPP_MINUS: code = MINUS_EXPR; break;
27537     case CPP_AND: code = BIT_AND_EXPR; break;
27538     case CPP_XOR: code = BIT_XOR_EXPR; break;
27539     case CPP_OR: code = BIT_IOR_EXPR; break;
27540     case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27541     case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27542     default: break;
27543     }
27544 
27545   if (code != ERROR_MARK)
27546     cp_lexer_consume_token (parser->lexer);
27547   else
27548     {
27549       bool saved_colon_corrects_to_scope_p;
27550       saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27551       parser->colon_corrects_to_scope_p = false;
27552       id = cp_parser_id_expression (parser, /*template_p=*/false,
27553 				    /*check_dependency_p=*/true,
27554 				    /*template_p=*/NULL,
27555 				    /*declarator_p=*/false,
27556 				    /*optional_p=*/false);
27557       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27558       if (identifier_p (id))
27559 	{
27560 	  const char *p = IDENTIFIER_POINTER (id);
27561 
27562 	  if (strcmp (p, "min") == 0)
27563 	    code = MIN_EXPR;
27564 	  else if (strcmp (p, "max") == 0)
27565 	    code = MAX_EXPR;
27566 	  else if (id == ansi_opname (PLUS_EXPR))
27567 	    code = PLUS_EXPR;
27568 	  else if (id == ansi_opname (MULT_EXPR))
27569 	    code = MULT_EXPR;
27570 	  else if (id == ansi_opname (MINUS_EXPR))
27571 	    code = MINUS_EXPR;
27572 	  else if (id == ansi_opname (BIT_AND_EXPR))
27573 	    code = BIT_AND_EXPR;
27574 	  else if (id == ansi_opname (BIT_IOR_EXPR))
27575 	    code = BIT_IOR_EXPR;
27576 	  else if (id == ansi_opname (BIT_XOR_EXPR))
27577 	    code = BIT_XOR_EXPR;
27578 	  else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
27579 	    code = TRUTH_ANDIF_EXPR;
27580 	  else if (id == ansi_opname (TRUTH_ORIF_EXPR))
27581 	    code = TRUTH_ORIF_EXPR;
27582 	  id = omp_reduction_id (code, id, NULL_TREE);
27583 	  tree scope = parser->scope;
27584 	  if (scope)
27585 	    id = build_qualified_name (NULL_TREE, scope, id, false);
27586 	  parser->scope = NULL_TREE;
27587 	  parser->qualifying_scope = NULL_TREE;
27588 	  parser->object_scope = NULL_TREE;
27589 	}
27590       else
27591 	{
27592 	  error ("invalid reduction-identifier");
27593 	 resync_fail:
27594 	  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27595 						 /*or_comma=*/false,
27596 						 /*consume_paren=*/true);
27597 	  return list;
27598 	}
27599     }
27600 
27601   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27602     goto resync_fail;
27603 
27604   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
27605 					  NULL);
27606   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27607     {
27608       OMP_CLAUSE_REDUCTION_CODE (c) = code;
27609       OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
27610     }
27611 
27612   return nlist;
27613 }
27614 
27615 /* OpenMP 2.5:
27616    schedule ( schedule-kind )
27617    schedule ( schedule-kind , expression )
27618 
27619    schedule-kind:
27620      static | dynamic | guided | runtime | auto  */
27621 
27622 static tree
cp_parser_omp_clause_schedule(cp_parser * parser,tree list,location_t location)27623 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
27624 {
27625   tree c, t;
27626 
27627   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27628     return list;
27629 
27630   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
27631 
27632   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27633     {
27634       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27635       const char *p = IDENTIFIER_POINTER (id);
27636 
27637       switch (p[0])
27638 	{
27639 	case 'd':
27640 	  if (strcmp ("dynamic", p) != 0)
27641 	    goto invalid_kind;
27642 	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
27643 	  break;
27644 
27645 	case 'g':
27646 	  if (strcmp ("guided", p) != 0)
27647 	    goto invalid_kind;
27648 	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
27649 	  break;
27650 
27651 	case 'r':
27652 	  if (strcmp ("runtime", p) != 0)
27653 	    goto invalid_kind;
27654 	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
27655 	  break;
27656 
27657 	default:
27658 	  goto invalid_kind;
27659 	}
27660     }
27661   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27662     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
27663   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
27664     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
27665   else
27666     goto invalid_kind;
27667   cp_lexer_consume_token (parser->lexer);
27668 
27669   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27670     {
27671       cp_token *token;
27672       cp_lexer_consume_token (parser->lexer);
27673 
27674       token = cp_lexer_peek_token (parser->lexer);
27675       t = cp_parser_assignment_expression (parser, false, NULL);
27676 
27677       if (t == error_mark_node)
27678 	goto resync_fail;
27679       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
27680 	error_at (token->location, "schedule %<runtime%> does not take "
27681 		  "a %<chunk_size%> parameter");
27682       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
27683 	error_at (token->location, "schedule %<auto%> does not take "
27684 		  "a %<chunk_size%> parameter");
27685       else
27686 	OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
27687 
27688       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27689 	goto resync_fail;
27690     }
27691   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27692     goto resync_fail;
27693 
27694   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
27695   OMP_CLAUSE_CHAIN (c) = list;
27696   return c;
27697 
27698  invalid_kind:
27699   cp_parser_error (parser, "invalid schedule kind");
27700  resync_fail:
27701   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27702 					 /*or_comma=*/false,
27703 					 /*consume_paren=*/true);
27704   return list;
27705 }
27706 
27707 /* OpenMP 3.0:
27708    untied */
27709 
27710 static tree
cp_parser_omp_clause_untied(cp_parser *,tree list,location_t location)27711 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
27712 			     tree list, location_t location)
27713 {
27714   tree c;
27715 
27716   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
27717 
27718   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
27719   OMP_CLAUSE_CHAIN (c) = list;
27720   return c;
27721 }
27722 
27723 /* OpenMP 4.0:
27724    inbranch
27725    notinbranch */
27726 
27727 static tree
cp_parser_omp_clause_branch(cp_parser *,enum omp_clause_code code,tree list,location_t location)27728 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
27729 			     tree list, location_t location)
27730 {
27731   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
27732   tree c = build_omp_clause (location, code);
27733   OMP_CLAUSE_CHAIN (c) = list;
27734   return c;
27735 }
27736 
27737 /* OpenMP 4.0:
27738    parallel
27739    for
27740    sections
27741    taskgroup */
27742 
27743 static tree
cp_parser_omp_clause_cancelkind(cp_parser *,enum omp_clause_code code,tree list,location_t location)27744 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
27745 				 enum omp_clause_code code,
27746 				 tree list, location_t location)
27747 {
27748   tree c = build_omp_clause (location, code);
27749   OMP_CLAUSE_CHAIN (c) = list;
27750   return c;
27751 }
27752 
27753 /* OpenMP 4.0:
27754    num_teams ( expression ) */
27755 
27756 static tree
cp_parser_omp_clause_num_teams(cp_parser * parser,tree list,location_t location)27757 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
27758 				location_t location)
27759 {
27760   tree t, c;
27761 
27762   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27763     return list;
27764 
27765   t = cp_parser_expression (parser, false, NULL);
27766 
27767   if (t == error_mark_node
27768       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27769     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27770 					   /*or_comma=*/false,
27771 					   /*consume_paren=*/true);
27772 
27773   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
27774 			     "num_teams", location);
27775 
27776   c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
27777   OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
27778   OMP_CLAUSE_CHAIN (c) = list;
27779 
27780   return c;
27781 }
27782 
27783 /* OpenMP 4.0:
27784    thread_limit ( expression ) */
27785 
27786 static tree
cp_parser_omp_clause_thread_limit(cp_parser * parser,tree list,location_t location)27787 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
27788 				   location_t location)
27789 {
27790   tree t, c;
27791 
27792   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27793     return list;
27794 
27795   t = cp_parser_expression (parser, false, NULL);
27796 
27797   if (t == error_mark_node
27798       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27799     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27800 					   /*or_comma=*/false,
27801 					   /*consume_paren=*/true);
27802 
27803   check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
27804 			     "thread_limit", location);
27805 
27806   c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
27807   OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
27808   OMP_CLAUSE_CHAIN (c) = list;
27809 
27810   return c;
27811 }
27812 
27813 /* OpenMP 4.0:
27814    aligned ( variable-list )
27815    aligned ( variable-list : constant-expression )  */
27816 
27817 static tree
cp_parser_omp_clause_aligned(cp_parser * parser,tree list)27818 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
27819 {
27820   tree nlist, c, alignment = NULL_TREE;
27821   bool colon;
27822 
27823   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27824     return list;
27825 
27826   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
27827 					  &colon);
27828 
27829   if (colon)
27830     {
27831       alignment = cp_parser_constant_expression (parser, false, NULL);
27832 
27833       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27834 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27835 					       /*or_comma=*/false,
27836 					       /*consume_paren=*/true);
27837 
27838       if (alignment == error_mark_node)
27839 	alignment = NULL_TREE;
27840     }
27841 
27842   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27843     OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
27844 
27845   return nlist;
27846 }
27847 
27848 /* OpenMP 4.0:
27849    linear ( variable-list )
27850    linear ( variable-list : expression )  */
27851 
27852 static tree
cp_parser_omp_clause_linear(cp_parser * parser,tree list,bool is_cilk_simd_fn)27853 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
27854 			     bool is_cilk_simd_fn)
27855 {
27856   tree nlist, c, step = integer_one_node;
27857   bool colon;
27858 
27859   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27860     return list;
27861 
27862   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
27863 					  &colon);
27864 
27865   if (colon)
27866     {
27867       step = cp_parser_expression (parser, false, NULL);
27868 
27869       if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
27870 	{
27871 	  sorry ("using parameters for %<linear%> step is not supported yet");
27872 	  step = integer_one_node;
27873 	}
27874       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27875 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27876 					       /*or_comma=*/false,
27877 					       /*consume_paren=*/true);
27878 
27879       if (step == error_mark_node)
27880 	return list;
27881     }
27882 
27883   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27884     OMP_CLAUSE_LINEAR_STEP (c) = step;
27885 
27886   return nlist;
27887 }
27888 
27889 /* OpenMP 4.0:
27890    safelen ( constant-expression )  */
27891 
27892 static tree
cp_parser_omp_clause_safelen(cp_parser * parser,tree list,location_t location)27893 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
27894 			      location_t location)
27895 {
27896   tree t, c;
27897 
27898   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27899     return list;
27900 
27901   t = cp_parser_constant_expression (parser, false, NULL);
27902 
27903   if (t == error_mark_node
27904       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27905     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27906 					   /*or_comma=*/false,
27907 					   /*consume_paren=*/true);
27908 
27909   check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
27910 
27911   c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
27912   OMP_CLAUSE_SAFELEN_EXPR (c) = t;
27913   OMP_CLAUSE_CHAIN (c) = list;
27914 
27915   return c;
27916 }
27917 
27918 /* OpenMP 4.0:
27919    simdlen ( constant-expression )  */
27920 
27921 static tree
cp_parser_omp_clause_simdlen(cp_parser * parser,tree list,location_t location)27922 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
27923 			      location_t location)
27924 {
27925   tree t, c;
27926 
27927   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27928     return list;
27929 
27930   t = cp_parser_constant_expression (parser, false, NULL);
27931 
27932   if (t == error_mark_node
27933       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27934     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27935 					   /*or_comma=*/false,
27936 					   /*consume_paren=*/true);
27937 
27938   check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
27939 
27940   c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
27941   OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
27942   OMP_CLAUSE_CHAIN (c) = list;
27943 
27944   return c;
27945 }
27946 
27947 /* OpenMP 4.0:
27948    depend ( depend-kind : variable-list )
27949 
27950    depend-kind:
27951      in | out | inout  */
27952 
27953 static tree
cp_parser_omp_clause_depend(cp_parser * parser,tree list)27954 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
27955 {
27956   tree nlist, c;
27957   enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
27958 
27959   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27960     return list;
27961 
27962   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27963     {
27964       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27965       const char *p = IDENTIFIER_POINTER (id);
27966 
27967       if (strcmp ("in", p) == 0)
27968 	kind = OMP_CLAUSE_DEPEND_IN;
27969       else if (strcmp ("inout", p) == 0)
27970 	kind = OMP_CLAUSE_DEPEND_INOUT;
27971       else if (strcmp ("out", p) == 0)
27972 	kind = OMP_CLAUSE_DEPEND_OUT;
27973       else
27974 	goto invalid_kind;
27975     }
27976   else
27977     goto invalid_kind;
27978 
27979   cp_lexer_consume_token (parser->lexer);
27980   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27981     goto resync_fail;
27982 
27983   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
27984 					  NULL);
27985 
27986   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27987     OMP_CLAUSE_DEPEND_KIND (c) = kind;
27988 
27989   return nlist;
27990 
27991  invalid_kind:
27992   cp_parser_error (parser, "invalid depend kind");
27993  resync_fail:
27994   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27995 					 /*or_comma=*/false,
27996 					 /*consume_paren=*/true);
27997   return list;
27998 }
27999 
28000 /* OpenMP 4.0:
28001    map ( map-kind : variable-list )
28002    map ( variable-list )
28003 
28004    map-kind:
28005      alloc | to | from | tofrom  */
28006 
28007 static tree
cp_parser_omp_clause_map(cp_parser * parser,tree list)28008 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28009 {
28010   tree nlist, c;
28011   enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28012 
28013   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28014     return list;
28015 
28016   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28017       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28018     {
28019       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28020       const char *p = IDENTIFIER_POINTER (id);
28021 
28022       if (strcmp ("alloc", p) == 0)
28023 	kind = OMP_CLAUSE_MAP_ALLOC;
28024       else if (strcmp ("to", p) == 0)
28025 	kind = OMP_CLAUSE_MAP_TO;
28026       else if (strcmp ("from", p) == 0)
28027 	kind = OMP_CLAUSE_MAP_FROM;
28028       else if (strcmp ("tofrom", p) == 0)
28029 	kind = OMP_CLAUSE_MAP_TOFROM;
28030       else
28031 	{
28032 	  cp_parser_error (parser, "invalid map kind");
28033 	  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28034 						 /*or_comma=*/false,
28035 						 /*consume_paren=*/true);
28036 	  return list;
28037 	}
28038       cp_lexer_consume_token (parser->lexer);
28039       cp_lexer_consume_token (parser->lexer);
28040     }
28041 
28042   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28043 					  NULL);
28044 
28045   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28046     OMP_CLAUSE_MAP_KIND (c) = kind;
28047 
28048   return nlist;
28049 }
28050 
28051 /* OpenMP 4.0:
28052    device ( expression ) */
28053 
28054 static tree
cp_parser_omp_clause_device(cp_parser * parser,tree list,location_t location)28055 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28056 			     location_t location)
28057 {
28058   tree t, c;
28059 
28060   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28061     return list;
28062 
28063   t = cp_parser_expression (parser, false, NULL);
28064 
28065   if (t == error_mark_node
28066       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28067     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28068 					   /*or_comma=*/false,
28069 					   /*consume_paren=*/true);
28070 
28071   check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28072 			     "device", location);
28073 
28074   c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28075   OMP_CLAUSE_DEVICE_ID (c) = t;
28076   OMP_CLAUSE_CHAIN (c) = list;
28077 
28078   return c;
28079 }
28080 
28081 /* OpenMP 4.0:
28082    dist_schedule ( static )
28083    dist_schedule ( static , expression )  */
28084 
28085 static tree
cp_parser_omp_clause_dist_schedule(cp_parser * parser,tree list,location_t location)28086 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28087 				    location_t location)
28088 {
28089   tree c, t;
28090 
28091   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28092     return list;
28093 
28094   c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28095 
28096   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28097     goto invalid_kind;
28098   cp_lexer_consume_token (parser->lexer);
28099 
28100   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28101     {
28102       cp_lexer_consume_token (parser->lexer);
28103 
28104       t = cp_parser_assignment_expression (parser, false, NULL);
28105 
28106       if (t == error_mark_node)
28107 	goto resync_fail;
28108       OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28109 
28110       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28111 	goto resync_fail;
28112     }
28113   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28114     goto resync_fail;
28115 
28116   check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28117 			     location);
28118   OMP_CLAUSE_CHAIN (c) = list;
28119   return c;
28120 
28121  invalid_kind:
28122   cp_parser_error (parser, "invalid dist_schedule kind");
28123  resync_fail:
28124   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28125 					 /*or_comma=*/false,
28126 					 /*consume_paren=*/true);
28127   return list;
28128 }
28129 
28130 /* OpenMP 4.0:
28131    proc_bind ( proc-bind-kind )
28132 
28133    proc-bind-kind:
28134      master | close | spread  */
28135 
28136 static tree
cp_parser_omp_clause_proc_bind(cp_parser * parser,tree list,location_t location)28137 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28138 				location_t location)
28139 {
28140   tree c;
28141   enum omp_clause_proc_bind_kind kind;
28142 
28143   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28144     return list;
28145 
28146   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28147     {
28148       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28149       const char *p = IDENTIFIER_POINTER (id);
28150 
28151       if (strcmp ("master", p) == 0)
28152 	kind = OMP_CLAUSE_PROC_BIND_MASTER;
28153       else if (strcmp ("close", p) == 0)
28154 	kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28155       else if (strcmp ("spread", p) == 0)
28156 	kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28157       else
28158 	goto invalid_kind;
28159     }
28160   else
28161     goto invalid_kind;
28162 
28163   cp_lexer_consume_token (parser->lexer);
28164   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28165     goto resync_fail;
28166 
28167   c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28168   check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28169 			     location);
28170   OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28171   OMP_CLAUSE_CHAIN (c) = list;
28172   return c;
28173 
28174  invalid_kind:
28175   cp_parser_error (parser, "invalid depend kind");
28176  resync_fail:
28177   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28178 					 /*or_comma=*/false,
28179 					 /*consume_paren=*/true);
28180   return list;
28181 }
28182 
28183 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
28184    is a bitmask in MASK.  Return the list of clauses found; the result
28185    of clause default goes in *pdefault.  */
28186 
28187 static tree
28188 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28189 			   const char *where, cp_token *pragma_tok,
28190 			   bool finish_p = true)
28191 {
28192   tree clauses = NULL;
28193   bool first = true;
28194   cp_token *token = NULL;
28195   bool cilk_simd_fn = false;
28196 
28197   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28198     {
28199       pragma_omp_clause c_kind;
28200       const char *c_name;
28201       tree prev = clauses;
28202 
28203       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28204 	cp_lexer_consume_token (parser->lexer);
28205 
28206       token = cp_lexer_peek_token (parser->lexer);
28207       c_kind = cp_parser_omp_clause_name (parser);
28208 
28209       switch (c_kind)
28210 	{
28211 	case PRAGMA_OMP_CLAUSE_COLLAPSE:
28212 	  clauses = cp_parser_omp_clause_collapse (parser, clauses,
28213 						   token->location);
28214 	  c_name = "collapse";
28215 	  break;
28216 	case PRAGMA_OMP_CLAUSE_COPYIN:
28217 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28218 	  c_name = "copyin";
28219 	  break;
28220 	case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28221 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28222 					    clauses);
28223 	  c_name = "copyprivate";
28224 	  break;
28225 	case PRAGMA_OMP_CLAUSE_DEFAULT:
28226 	  clauses = cp_parser_omp_clause_default (parser, clauses,
28227 						  token->location);
28228 	  c_name = "default";
28229 	  break;
28230 	case PRAGMA_OMP_CLAUSE_FINAL:
28231 	  clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28232 	  c_name = "final";
28233 	  break;
28234 	case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28235 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28236 					    clauses);
28237 	  c_name = "firstprivate";
28238 	  break;
28239 	case PRAGMA_OMP_CLAUSE_IF:
28240 	  clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28241 	  c_name = "if";
28242 	  break;
28243 	case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28244 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28245 					    clauses);
28246 	  c_name = "lastprivate";
28247 	  break;
28248 	case PRAGMA_OMP_CLAUSE_MERGEABLE:
28249 	  clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28250 						    token->location);
28251 	  c_name = "mergeable";
28252 	  break;
28253 	case PRAGMA_OMP_CLAUSE_NOWAIT:
28254 	  clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28255 	  c_name = "nowait";
28256 	  break;
28257 	case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28258 	  clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28259 						      token->location);
28260 	  c_name = "num_threads";
28261 	  break;
28262 	case PRAGMA_OMP_CLAUSE_ORDERED:
28263 	  clauses = cp_parser_omp_clause_ordered (parser, clauses,
28264 						  token->location);
28265 	  c_name = "ordered";
28266 	  break;
28267 	case PRAGMA_OMP_CLAUSE_PRIVATE:
28268 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28269 					    clauses);
28270 	  c_name = "private";
28271 	  break;
28272 	case PRAGMA_OMP_CLAUSE_REDUCTION:
28273 	  clauses = cp_parser_omp_clause_reduction (parser, clauses);
28274 	  c_name = "reduction";
28275 	  break;
28276 	case PRAGMA_OMP_CLAUSE_SCHEDULE:
28277 	  clauses = cp_parser_omp_clause_schedule (parser, clauses,
28278 						   token->location);
28279 	  c_name = "schedule";
28280 	  break;
28281 	case PRAGMA_OMP_CLAUSE_SHARED:
28282 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28283 					    clauses);
28284 	  c_name = "shared";
28285 	  break;
28286 	case PRAGMA_OMP_CLAUSE_UNTIED:
28287 	  clauses = cp_parser_omp_clause_untied (parser, clauses,
28288 						 token->location);
28289 	  c_name = "untied";
28290 	  break;
28291 	case PRAGMA_OMP_CLAUSE_INBRANCH:
28292 	case PRAGMA_CILK_CLAUSE_MASK:
28293 	  clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28294 						 clauses, token->location);
28295 	  c_name = "inbranch";
28296 	  break;
28297 	case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28298 	case PRAGMA_CILK_CLAUSE_NOMASK:
28299 	  clauses = cp_parser_omp_clause_branch (parser,
28300 						 OMP_CLAUSE_NOTINBRANCH,
28301 						 clauses, token->location);
28302 	  c_name = "notinbranch";
28303 	  break;
28304 	case PRAGMA_OMP_CLAUSE_PARALLEL:
28305 	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28306 						     clauses, token->location);
28307 	  c_name = "parallel";
28308 	  if (!first)
28309 	    {
28310 	     clause_not_first:
28311 	      error_at (token->location, "%qs must be the first clause of %qs",
28312 			c_name, where);
28313 	      clauses = prev;
28314 	    }
28315 	  break;
28316 	case PRAGMA_OMP_CLAUSE_FOR:
28317 	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28318 						     clauses, token->location);
28319 	  c_name = "for";
28320 	  if (!first)
28321 	    goto clause_not_first;
28322 	  break;
28323 	case PRAGMA_OMP_CLAUSE_SECTIONS:
28324 	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28325 						     clauses, token->location);
28326 	  c_name = "sections";
28327 	  if (!first)
28328 	    goto clause_not_first;
28329 	  break;
28330 	case PRAGMA_OMP_CLAUSE_TASKGROUP:
28331 	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28332 						     clauses, token->location);
28333 	  c_name = "taskgroup";
28334 	  if (!first)
28335 	    goto clause_not_first;
28336 	  break;
28337 	case PRAGMA_OMP_CLAUSE_TO:
28338 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28339 					    clauses);
28340 	  c_name = "to";
28341 	  break;
28342 	case PRAGMA_OMP_CLAUSE_FROM:
28343 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28344 					    clauses);
28345 	  c_name = "from";
28346 	  break;
28347 	case PRAGMA_OMP_CLAUSE_UNIFORM:
28348 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28349 					    clauses);
28350 	  c_name = "uniform";
28351 	  break;
28352 	case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28353 	  clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28354 						    token->location);
28355 	  c_name = "num_teams";
28356 	  break;
28357 	case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28358 	  clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28359 						       token->location);
28360 	  c_name = "thread_limit";
28361 	  break;
28362 	case PRAGMA_OMP_CLAUSE_ALIGNED:
28363 	  clauses = cp_parser_omp_clause_aligned (parser, clauses);
28364 	  c_name = "aligned";
28365 	  break;
28366 	case PRAGMA_OMP_CLAUSE_LINEAR:
28367 	  if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
28368 	    cilk_simd_fn = true;
28369 	  clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
28370 	  c_name = "linear";
28371 	  break;
28372 	case PRAGMA_OMP_CLAUSE_DEPEND:
28373 	  clauses = cp_parser_omp_clause_depend (parser, clauses);
28374 	  c_name = "depend";
28375 	  break;
28376 	case PRAGMA_OMP_CLAUSE_MAP:
28377 	  clauses = cp_parser_omp_clause_map (parser, clauses);
28378 	  c_name = "map";
28379 	  break;
28380 	case PRAGMA_OMP_CLAUSE_DEVICE:
28381 	  clauses = cp_parser_omp_clause_device (parser, clauses,
28382 						 token->location);
28383 	  c_name = "device";
28384 	  break;
28385 	case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28386 	  clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28387 							token->location);
28388 	  c_name = "dist_schedule";
28389 	  break;
28390 	case PRAGMA_OMP_CLAUSE_PROC_BIND:
28391 	  clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28392 						    token->location);
28393 	  c_name = "proc_bind";
28394 	  break;
28395 	case PRAGMA_OMP_CLAUSE_SAFELEN:
28396 	  clauses = cp_parser_omp_clause_safelen (parser, clauses,
28397 						  token->location);
28398 	  c_name = "safelen";
28399 	  break;
28400 	case PRAGMA_OMP_CLAUSE_SIMDLEN:
28401 	  clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28402 						  token->location);
28403 	  c_name = "simdlen";
28404 	  break;
28405 	case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
28406 	  clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
28407 	  c_name = "simdlen";
28408 	  break;
28409 	default:
28410 	  cp_parser_error (parser, "expected %<#pragma omp%> clause");
28411 	  goto saw_error;
28412 	}
28413 
28414       first = false;
28415 
28416       if (((mask >> c_kind) & 1) == 0)
28417 	{
28418 	  /* Remove the invalid clause(s) from the list to avoid
28419 	     confusing the rest of the compiler.  */
28420 	  clauses = prev;
28421 	  error_at (token->location, "%qs is not valid for %qs", c_name, where);
28422 	}
28423     }
28424  saw_error:
28425   /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28426      no reason to skip to the end.  */
28427   if (!(flag_cilkplus && pragma_tok == NULL))
28428     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28429   if (finish_p)
28430     return finish_omp_clauses (clauses);
28431   return clauses;
28432 }
28433 
28434 /* OpenMP 2.5:
28435    structured-block:
28436      statement
28437 
28438    In practice, we're also interested in adding the statement to an
28439    outer node.  So it is convenient if we work around the fact that
28440    cp_parser_statement calls add_stmt.  */
28441 
28442 static unsigned
cp_parser_begin_omp_structured_block(cp_parser * parser)28443 cp_parser_begin_omp_structured_block (cp_parser *parser)
28444 {
28445   unsigned save = parser->in_statement;
28446 
28447   /* Only move the values to IN_OMP_BLOCK if they weren't false.
28448      This preserves the "not within loop or switch" style error messages
28449      for nonsense cases like
28450 	void foo() {
28451 	#pragma omp single
28452 	  break;
28453 	}
28454   */
28455   if (parser->in_statement)
28456     parser->in_statement = IN_OMP_BLOCK;
28457 
28458   return save;
28459 }
28460 
28461 static void
cp_parser_end_omp_structured_block(cp_parser * parser,unsigned save)28462 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28463 {
28464   parser->in_statement = save;
28465 }
28466 
28467 static tree
cp_parser_omp_structured_block(cp_parser * parser)28468 cp_parser_omp_structured_block (cp_parser *parser)
28469 {
28470   tree stmt = begin_omp_structured_block ();
28471   unsigned int save = cp_parser_begin_omp_structured_block (parser);
28472 
28473   cp_parser_statement (parser, NULL_TREE, false, NULL);
28474 
28475   cp_parser_end_omp_structured_block (parser, save);
28476   return finish_omp_structured_block (stmt);
28477 }
28478 
28479 /* OpenMP 2.5:
28480    # pragma omp atomic new-line
28481      expression-stmt
28482 
28483    expression-stmt:
28484      x binop= expr | x++ | ++x | x-- | --x
28485    binop:
28486      +, *, -, /, &, ^, |, <<, >>
28487 
28488   where x is an lvalue expression with scalar type.
28489 
28490    OpenMP 3.1:
28491    # pragma omp atomic new-line
28492      update-stmt
28493 
28494    # pragma omp atomic read new-line
28495      read-stmt
28496 
28497    # pragma omp atomic write new-line
28498      write-stmt
28499 
28500    # pragma omp atomic update new-line
28501      update-stmt
28502 
28503    # pragma omp atomic capture new-line
28504      capture-stmt
28505 
28506    # pragma omp atomic capture new-line
28507      capture-block
28508 
28509    read-stmt:
28510      v = x
28511    write-stmt:
28512      x = expr
28513    update-stmt:
28514      expression-stmt | x = x binop expr
28515    capture-stmt:
28516      v = expression-stmt
28517    capture-block:
28518      { v = x; update-stmt; } | { update-stmt; v = x; }
28519 
28520    OpenMP 4.0:
28521    update-stmt:
28522      expression-stmt | x = x binop expr | x = expr binop x
28523    capture-stmt:
28524      v = update-stmt
28525    capture-block:
28526      { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28527 
28528   where x and v are lvalue expressions with scalar type.  */
28529 
28530 static void
cp_parser_omp_atomic(cp_parser * parser,cp_token * pragma_tok)28531 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28532 {
28533   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28534   tree rhs1 = NULL_TREE, orig_lhs;
28535   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28536   bool structured_block = false;
28537   bool seq_cst = false;
28538 
28539   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28540     {
28541       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28542       const char *p = IDENTIFIER_POINTER (id);
28543 
28544       if (!strcmp (p, "seq_cst"))
28545 	{
28546 	  seq_cst = true;
28547 	  cp_lexer_consume_token (parser->lexer);
28548 	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28549 	      && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
28550 	    cp_lexer_consume_token (parser->lexer);
28551 	}
28552     }
28553   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28554     {
28555       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28556       const char *p = IDENTIFIER_POINTER (id);
28557 
28558       if (!strcmp (p, "read"))
28559 	code = OMP_ATOMIC_READ;
28560       else if (!strcmp (p, "write"))
28561 	code = NOP_EXPR;
28562       else if (!strcmp (p, "update"))
28563 	code = OMP_ATOMIC;
28564       else if (!strcmp (p, "capture"))
28565 	code = OMP_ATOMIC_CAPTURE_NEW;
28566       else
28567 	p = NULL;
28568       if (p)
28569 	cp_lexer_consume_token (parser->lexer);
28570     }
28571   if (!seq_cst)
28572     {
28573       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28574 	  && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
28575 	cp_lexer_consume_token (parser->lexer);
28576 
28577       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28578 	{
28579 	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28580 	  const char *p = IDENTIFIER_POINTER (id);
28581 
28582 	  if (!strcmp (p, "seq_cst"))
28583 	    {
28584 	      seq_cst = true;
28585 	      cp_lexer_consume_token (parser->lexer);
28586 	    }
28587 	}
28588     }
28589   cp_parser_require_pragma_eol (parser, pragma_tok);
28590 
28591   switch (code)
28592     {
28593     case OMP_ATOMIC_READ:
28594     case NOP_EXPR: /* atomic write */
28595       v = cp_parser_unary_expression (parser, /*address_p=*/false,
28596 				      /*cast_p=*/false, NULL);
28597       if (v == error_mark_node)
28598 	goto saw_error;
28599       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28600 	goto saw_error;
28601       if (code == NOP_EXPR)
28602 	lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28603       else
28604 	lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28605 					  /*cast_p=*/false, NULL);
28606       if (lhs == error_mark_node)
28607 	goto saw_error;
28608       if (code == NOP_EXPR)
28609 	{
28610 	  /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28611 	     opcode.  */
28612 	  code = OMP_ATOMIC;
28613 	  rhs = lhs;
28614 	  lhs = v;
28615 	  v = NULL_TREE;
28616 	}
28617       goto done;
28618     case OMP_ATOMIC_CAPTURE_NEW:
28619       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28620 	{
28621 	  cp_lexer_consume_token (parser->lexer);
28622 	  structured_block = true;
28623 	}
28624       else
28625 	{
28626 	  v = cp_parser_unary_expression (parser, /*address_p=*/false,
28627 					  /*cast_p=*/false, NULL);
28628 	  if (v == error_mark_node)
28629 	    goto saw_error;
28630 	  if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28631 	    goto saw_error;
28632 	}
28633     default:
28634       break;
28635     }
28636 
28637 restart:
28638   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28639 				    /*cast_p=*/false, NULL);
28640   orig_lhs = lhs;
28641   switch (TREE_CODE (lhs))
28642     {
28643     case ERROR_MARK:
28644       goto saw_error;
28645 
28646     case POSTINCREMENT_EXPR:
28647       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28648 	code = OMP_ATOMIC_CAPTURE_OLD;
28649       /* FALLTHROUGH */
28650     case PREINCREMENT_EXPR:
28651       lhs = TREE_OPERAND (lhs, 0);
28652       opcode = PLUS_EXPR;
28653       rhs = integer_one_node;
28654       break;
28655 
28656     case POSTDECREMENT_EXPR:
28657       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28658 	code = OMP_ATOMIC_CAPTURE_OLD;
28659       /* FALLTHROUGH */
28660     case PREDECREMENT_EXPR:
28661       lhs = TREE_OPERAND (lhs, 0);
28662       opcode = MINUS_EXPR;
28663       rhs = integer_one_node;
28664       break;
28665 
28666     case COMPOUND_EXPR:
28667       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
28668 	 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
28669 	 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
28670 	 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
28671 	 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
28672 					     (TREE_OPERAND (lhs, 1), 0), 0)))
28673 	    == BOOLEAN_TYPE)
28674        /* Undo effects of boolean_increment for post {in,de}crement.  */
28675        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
28676       /* FALLTHRU */
28677     case MODIFY_EXPR:
28678       if (TREE_CODE (lhs) == MODIFY_EXPR
28679 	 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
28680 	{
28681 	  /* Undo effects of boolean_increment.  */
28682 	  if (integer_onep (TREE_OPERAND (lhs, 1)))
28683 	    {
28684 	      /* This is pre or post increment.  */
28685 	      rhs = TREE_OPERAND (lhs, 1);
28686 	      lhs = TREE_OPERAND (lhs, 0);
28687 	      opcode = NOP_EXPR;
28688 	      if (code == OMP_ATOMIC_CAPTURE_NEW
28689 		  && !structured_block
28690 		  && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
28691 		code = OMP_ATOMIC_CAPTURE_OLD;
28692 	      break;
28693 	    }
28694 	}
28695       /* FALLTHRU */
28696     default:
28697       switch (cp_lexer_peek_token (parser->lexer)->type)
28698 	{
28699 	case CPP_MULT_EQ:
28700 	  opcode = MULT_EXPR;
28701 	  break;
28702 	case CPP_DIV_EQ:
28703 	  opcode = TRUNC_DIV_EXPR;
28704 	  break;
28705 	case CPP_PLUS_EQ:
28706 	  opcode = PLUS_EXPR;
28707 	  break;
28708 	case CPP_MINUS_EQ:
28709 	  opcode = MINUS_EXPR;
28710 	  break;
28711 	case CPP_LSHIFT_EQ:
28712 	  opcode = LSHIFT_EXPR;
28713 	  break;
28714 	case CPP_RSHIFT_EQ:
28715 	  opcode = RSHIFT_EXPR;
28716 	  break;
28717 	case CPP_AND_EQ:
28718 	  opcode = BIT_AND_EXPR;
28719 	  break;
28720 	case CPP_OR_EQ:
28721 	  opcode = BIT_IOR_EXPR;
28722 	  break;
28723 	case CPP_XOR_EQ:
28724 	  opcode = BIT_XOR_EXPR;
28725 	  break;
28726 	case CPP_EQ:
28727 	  enum cp_parser_prec oprec;
28728 	  cp_token *token;
28729 	  cp_lexer_consume_token (parser->lexer);
28730 	  cp_parser_parse_tentatively (parser);
28731 	  rhs1 = cp_parser_simple_cast_expression (parser);
28732 	  if (rhs1 == error_mark_node)
28733 	    {
28734 	      cp_parser_abort_tentative_parse (parser);
28735 	      cp_parser_simple_cast_expression (parser);
28736 	      goto saw_error;
28737 	    }
28738 	  token = cp_lexer_peek_token (parser->lexer);
28739 	  if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
28740 	    {
28741 	      cp_parser_abort_tentative_parse (parser);
28742 	      cp_parser_parse_tentatively (parser);
28743 	      rhs = cp_parser_binary_expression (parser, false, true,
28744 						 PREC_NOT_OPERATOR, NULL);
28745 	      if (rhs == error_mark_node)
28746 		{
28747 		  cp_parser_abort_tentative_parse (parser);
28748 		  cp_parser_binary_expression (parser, false, true,
28749 					       PREC_NOT_OPERATOR, NULL);
28750 		  goto saw_error;
28751 		}
28752 	      switch (TREE_CODE (rhs))
28753 		{
28754 		case MULT_EXPR:
28755 		case TRUNC_DIV_EXPR:
28756 		case PLUS_EXPR:
28757 		case MINUS_EXPR:
28758 		case LSHIFT_EXPR:
28759 		case RSHIFT_EXPR:
28760 		case BIT_AND_EXPR:
28761 		case BIT_IOR_EXPR:
28762 		case BIT_XOR_EXPR:
28763 		  if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
28764 		    {
28765 		      if (cp_parser_parse_definitely (parser))
28766 			{
28767 			  opcode = TREE_CODE (rhs);
28768 			  rhs1 = TREE_OPERAND (rhs, 0);
28769 			  rhs = TREE_OPERAND (rhs, 1);
28770 			  goto stmt_done;
28771 			}
28772 		      else
28773 			goto saw_error;
28774 		    }
28775 		  break;
28776 		default:
28777 		  break;
28778 		}
28779 	      cp_parser_abort_tentative_parse (parser);
28780 	      if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
28781 		{
28782 		  rhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28783 		  if (rhs == error_mark_node)
28784 		    goto saw_error;
28785 		  opcode = NOP_EXPR;
28786 		  rhs1 = NULL_TREE;
28787 		  goto stmt_done;
28788 		}
28789 	      cp_parser_error (parser,
28790 			       "invalid form of %<#pragma omp atomic%>");
28791 	      goto saw_error;
28792 	    }
28793 	  if (!cp_parser_parse_definitely (parser))
28794 	    goto saw_error;
28795 	  switch (token->type)
28796 	    {
28797 	    case CPP_SEMICOLON:
28798 	      if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28799 		{
28800 		  code = OMP_ATOMIC_CAPTURE_OLD;
28801 		  v = lhs;
28802 		  lhs = NULL_TREE;
28803 		  lhs1 = rhs1;
28804 		  rhs1 = NULL_TREE;
28805 		  cp_lexer_consume_token (parser->lexer);
28806 		  goto restart;
28807 		}
28808 	      else if (structured_block)
28809 		{
28810 		  opcode = NOP_EXPR;
28811 		  rhs = rhs1;
28812 		  rhs1 = NULL_TREE;
28813 		  goto stmt_done;
28814 		}
28815 	      cp_parser_error (parser,
28816 			       "invalid form of %<#pragma omp atomic%>");
28817 	      goto saw_error;
28818 	    case CPP_MULT:
28819 	      opcode = MULT_EXPR;
28820 	      break;
28821 	    case CPP_DIV:
28822 	      opcode = TRUNC_DIV_EXPR;
28823 	      break;
28824 	    case CPP_PLUS:
28825 	      opcode = PLUS_EXPR;
28826 	      break;
28827 	    case CPP_MINUS:
28828 	      opcode = MINUS_EXPR;
28829 	      break;
28830 	    case CPP_LSHIFT:
28831 	      opcode = LSHIFT_EXPR;
28832 	      break;
28833 	    case CPP_RSHIFT:
28834 	      opcode = RSHIFT_EXPR;
28835 	      break;
28836 	    case CPP_AND:
28837 	      opcode = BIT_AND_EXPR;
28838 	      break;
28839 	    case CPP_OR:
28840 	      opcode = BIT_IOR_EXPR;
28841 	      break;
28842 	    case CPP_XOR:
28843 	      opcode = BIT_XOR_EXPR;
28844 	      break;
28845 	    default:
28846 	      cp_parser_error (parser,
28847 			       "invalid operator for %<#pragma omp atomic%>");
28848 	      goto saw_error;
28849 	    }
28850 	  oprec = TOKEN_PRECEDENCE (token);
28851 	  gcc_assert (oprec != PREC_NOT_OPERATOR);
28852 	  if (commutative_tree_code (opcode))
28853 	    oprec = (enum cp_parser_prec) (oprec - 1);
28854 	  cp_lexer_consume_token (parser->lexer);
28855 	  rhs = cp_parser_binary_expression (parser, false, false,
28856 					     oprec, NULL);
28857 	  if (rhs == error_mark_node)
28858 	    goto saw_error;
28859 	  goto stmt_done;
28860 	  /* FALLTHROUGH */
28861 	default:
28862 	  cp_parser_error (parser,
28863 			   "invalid operator for %<#pragma omp atomic%>");
28864 	  goto saw_error;
28865 	}
28866       cp_lexer_consume_token (parser->lexer);
28867 
28868       rhs = cp_parser_expression (parser, false, NULL);
28869       if (rhs == error_mark_node)
28870 	goto saw_error;
28871       break;
28872     }
28873 stmt_done:
28874   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28875     {
28876       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
28877 	goto saw_error;
28878       v = cp_parser_unary_expression (parser, /*address_p=*/false,
28879 				      /*cast_p=*/false, NULL);
28880       if (v == error_mark_node)
28881 	goto saw_error;
28882       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28883 	goto saw_error;
28884       lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
28885 					 /*cast_p=*/false, NULL);
28886       if (lhs1 == error_mark_node)
28887 	goto saw_error;
28888     }
28889   if (structured_block)
28890     {
28891       cp_parser_consume_semicolon_at_end_of_statement (parser);
28892       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
28893     }
28894 done:
28895   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
28896   if (!structured_block)
28897     cp_parser_consume_semicolon_at_end_of_statement (parser);
28898   return;
28899 
28900  saw_error:
28901   cp_parser_skip_to_end_of_block_or_statement (parser);
28902   if (structured_block)
28903     {
28904       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28905         cp_lexer_consume_token (parser->lexer);
28906       else if (code == OMP_ATOMIC_CAPTURE_NEW)
28907 	{
28908 	  cp_parser_skip_to_end_of_block_or_statement (parser);
28909 	  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28910 	    cp_lexer_consume_token (parser->lexer);
28911 	}
28912     }
28913 }
28914 
28915 
28916 /* OpenMP 2.5:
28917    # pragma omp barrier new-line  */
28918 
28919 static void
cp_parser_omp_barrier(cp_parser * parser,cp_token * pragma_tok)28920 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
28921 {
28922   cp_parser_require_pragma_eol (parser, pragma_tok);
28923   finish_omp_barrier ();
28924 }
28925 
28926 /* OpenMP 2.5:
28927    # pragma omp critical [(name)] new-line
28928      structured-block  */
28929 
28930 static tree
cp_parser_omp_critical(cp_parser * parser,cp_token * pragma_tok)28931 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
28932 {
28933   tree stmt, name = NULL;
28934 
28935   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28936     {
28937       cp_lexer_consume_token (parser->lexer);
28938 
28939       name = cp_parser_identifier (parser);
28940 
28941       if (name == error_mark_node
28942 	  || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28943 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28944 					       /*or_comma=*/false,
28945 					       /*consume_paren=*/true);
28946       if (name == error_mark_node)
28947 	name = NULL;
28948     }
28949   cp_parser_require_pragma_eol (parser, pragma_tok);
28950 
28951   stmt = cp_parser_omp_structured_block (parser);
28952   return c_finish_omp_critical (input_location, stmt, name);
28953 }
28954 
28955 /* OpenMP 2.5:
28956    # pragma omp flush flush-vars[opt] new-line
28957 
28958    flush-vars:
28959      ( variable-list ) */
28960 
28961 static void
cp_parser_omp_flush(cp_parser * parser,cp_token * pragma_tok)28962 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
28963 {
28964   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28965     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28966   cp_parser_require_pragma_eol (parser, pragma_tok);
28967 
28968   finish_omp_flush ();
28969 }
28970 
28971 /* Helper function, to parse omp for increment expression.  */
28972 
28973 static tree
cp_parser_omp_for_cond(cp_parser * parser,tree decl,enum tree_code code)28974 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
28975 {
28976   tree cond = cp_parser_binary_expression (parser, false, true,
28977 					   PREC_NOT_OPERATOR, NULL);
28978   if (cond == error_mark_node
28979       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28980     {
28981       cp_parser_skip_to_end_of_statement (parser);
28982       return error_mark_node;
28983     }
28984 
28985   switch (TREE_CODE (cond))
28986     {
28987     case GT_EXPR:
28988     case GE_EXPR:
28989     case LT_EXPR:
28990     case LE_EXPR:
28991       break;
28992     case NE_EXPR:
28993       if (code == CILK_SIMD)
28994 	break;
28995       /* Fall through: OpenMP disallows NE_EXPR.  */
28996     default:
28997       return error_mark_node;
28998     }
28999 
29000   /* If decl is an iterator, preserve LHS and RHS of the relational
29001      expr until finish_omp_for.  */
29002   if (decl
29003       && (type_dependent_expression_p (decl)
29004 	  || CLASS_TYPE_P (TREE_TYPE (decl))))
29005     return cond;
29006 
29007   return build_x_binary_op (input_location, TREE_CODE (cond),
29008 			    TREE_OPERAND (cond, 0), ERROR_MARK,
29009 			    TREE_OPERAND (cond, 1), ERROR_MARK,
29010 			    /*overload=*/NULL, tf_warning_or_error);
29011 }
29012 
29013 /* Helper function, to parse omp for increment expression.  */
29014 
29015 static tree
cp_parser_omp_for_incr(cp_parser * parser,tree decl)29016 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29017 {
29018   cp_token *token = cp_lexer_peek_token (parser->lexer);
29019   enum tree_code op;
29020   tree lhs, rhs;
29021   cp_id_kind idk;
29022   bool decl_first;
29023 
29024   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29025     {
29026       op = (token->type == CPP_PLUS_PLUS
29027 	    ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29028       cp_lexer_consume_token (parser->lexer);
29029       lhs = cp_parser_simple_cast_expression (parser);
29030       if (lhs != decl)
29031 	return error_mark_node;
29032       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29033     }
29034 
29035   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29036   if (lhs != decl)
29037     return error_mark_node;
29038 
29039   token = cp_lexer_peek_token (parser->lexer);
29040   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29041     {
29042       op = (token->type == CPP_PLUS_PLUS
29043 	    ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29044       cp_lexer_consume_token (parser->lexer);
29045       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29046     }
29047 
29048   op = cp_parser_assignment_operator_opt (parser);
29049   if (op == ERROR_MARK)
29050     return error_mark_node;
29051 
29052   if (op != NOP_EXPR)
29053     {
29054       rhs = cp_parser_assignment_expression (parser, false, NULL);
29055       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29056       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29057     }
29058 
29059   lhs = cp_parser_binary_expression (parser, false, false,
29060 				     PREC_ADDITIVE_EXPRESSION, NULL);
29061   token = cp_lexer_peek_token (parser->lexer);
29062   decl_first = lhs == decl;
29063   if (decl_first)
29064     lhs = NULL_TREE;
29065   if (token->type != CPP_PLUS
29066       && token->type != CPP_MINUS)
29067     return error_mark_node;
29068 
29069   do
29070     {
29071       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29072       cp_lexer_consume_token (parser->lexer);
29073       rhs = cp_parser_binary_expression (parser, false, false,
29074 					 PREC_ADDITIVE_EXPRESSION, NULL);
29075       token = cp_lexer_peek_token (parser->lexer);
29076       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29077 	{
29078 	  if (lhs == NULL_TREE)
29079 	    {
29080 	      if (op == PLUS_EXPR)
29081 		lhs = rhs;
29082 	      else
29083 		lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29084 					tf_warning_or_error);
29085 	    }
29086 	  else
29087 	    lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29088 				     ERROR_MARK, NULL, tf_warning_or_error);
29089 	}
29090     }
29091   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29092 
29093   if (!decl_first)
29094     {
29095       if (rhs != decl || op == MINUS_EXPR)
29096 	return error_mark_node;
29097       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29098     }
29099   else
29100     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29101 
29102   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29103 }
29104 
29105 /* Parse the initialization statement of either an OpenMP for loop or
29106    a Cilk Plus for loop.
29107 
29108    PARSING_OPENMP is true if parsing OpenMP, or false if parsing Cilk
29109    Plus.
29110 
29111    Return true if the resulting construct should have an
29112    OMP_CLAUSE_PRIVATE added to it.  */
29113 
29114 static bool
cp_parser_omp_for_loop_init(cp_parser * parser,bool parsing_openmp,tree & this_pre_body,vec<tree,va_gc> * for_block,tree & init,tree & decl,tree & real_decl)29115 cp_parser_omp_for_loop_init (cp_parser *parser,
29116 			     bool parsing_openmp,
29117 			     tree &this_pre_body,
29118 			     vec<tree, va_gc> *for_block,
29119 			     tree &init,
29120 			     tree &decl,
29121 			     tree &real_decl)
29122 {
29123   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29124     return false;
29125 
29126   bool add_private_clause = false;
29127 
29128   /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29129 
29130      init-expr:
29131      var = lb
29132      integer-type var = lb
29133      random-access-iterator-type var = lb
29134      pointer-type var = lb
29135   */
29136   cp_decl_specifier_seq type_specifiers;
29137 
29138   /* First, try to parse as an initialized declaration.  See
29139      cp_parser_condition, from whence the bulk of this is copied.  */
29140 
29141   cp_parser_parse_tentatively (parser);
29142   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29143 				/*is_trailing_return=*/false,
29144 				&type_specifiers);
29145   if (cp_parser_parse_definitely (parser))
29146     {
29147       /* If parsing a type specifier seq succeeded, then this
29148 	 MUST be a initialized declaration.  */
29149       tree asm_specification, attributes;
29150       cp_declarator *declarator;
29151 
29152       declarator = cp_parser_declarator (parser,
29153 					 CP_PARSER_DECLARATOR_NAMED,
29154 					 /*ctor_dtor_or_conv_p=*/NULL,
29155 					 /*parenthesized_p=*/NULL,
29156 					 /*member_p=*/false);
29157       attributes = cp_parser_attributes_opt (parser);
29158       asm_specification = cp_parser_asm_specification_opt (parser);
29159 
29160       if (declarator == cp_error_declarator)
29161 	cp_parser_skip_to_end_of_statement (parser);
29162 
29163       else
29164 	{
29165 	  tree pushed_scope, auto_node;
29166 
29167 	  decl = start_decl (declarator, &type_specifiers,
29168 			     SD_INITIALIZED, attributes,
29169 			     /*prefix_attributes=*/NULL_TREE,
29170 			     &pushed_scope);
29171 
29172 	  auto_node = type_uses_auto (TREE_TYPE (decl));
29173 	  if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29174 	    {
29175 	      if (cp_lexer_next_token_is (parser->lexer,
29176 					  CPP_OPEN_PAREN))
29177 		{
29178 		  if (parsing_openmp)
29179 		    error ("parenthesized initialization is not allowed in "
29180 			   "OpenMP %<for%> loop");
29181 		  else
29182 		    error ("parenthesized initialization is "
29183 			   "not allowed in for-loop");
29184 		}
29185 	      else
29186 		/* Trigger an error.  */
29187 		cp_parser_require (parser, CPP_EQ, RT_EQ);
29188 
29189 	      init = error_mark_node;
29190 	      cp_parser_skip_to_end_of_statement (parser);
29191 	    }
29192 	  else if (CLASS_TYPE_P (TREE_TYPE (decl))
29193 		   || type_dependent_expression_p (decl)
29194 		   || auto_node)
29195 	    {
29196 	      bool is_direct_init, is_non_constant_init;
29197 
29198 	      init = cp_parser_initializer (parser,
29199 					    &is_direct_init,
29200 					    &is_non_constant_init);
29201 
29202 	      if (auto_node)
29203 		{
29204 		  TREE_TYPE (decl)
29205 		    = do_auto_deduction (TREE_TYPE (decl), init,
29206 					 auto_node);
29207 
29208 		  if (!CLASS_TYPE_P (TREE_TYPE (decl))
29209 		      && !type_dependent_expression_p (decl))
29210 		    goto non_class;
29211 		}
29212 
29213 	      cp_finish_decl (decl, init, !is_non_constant_init,
29214 			      asm_specification,
29215 			      LOOKUP_ONLYCONVERTING);
29216 	      if (CLASS_TYPE_P (TREE_TYPE (decl)))
29217 		{
29218 		  vec_safe_push (for_block, this_pre_body);
29219 		  init = NULL_TREE;
29220 		}
29221 	      else
29222 		init = pop_stmt_list (this_pre_body);
29223 	      this_pre_body = NULL_TREE;
29224 	    }
29225 	  else
29226 	    {
29227 	      /* Consume '='.  */
29228 	      cp_lexer_consume_token (parser->lexer);
29229 	      init = cp_parser_assignment_expression (parser, false, NULL);
29230 
29231 	    non_class:
29232 	      if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29233 		init = error_mark_node;
29234 	      else
29235 		cp_finish_decl (decl, NULL_TREE,
29236 				/*init_const_expr_p=*/false,
29237 				asm_specification,
29238 				LOOKUP_ONLYCONVERTING);
29239 	    }
29240 
29241 	  if (pushed_scope)
29242 	    pop_scope (pushed_scope);
29243 	}
29244     }
29245   else
29246     {
29247       cp_id_kind idk;
29248       /* If parsing a type specifier sequence failed, then
29249 	 this MUST be a simple expression.  */
29250       cp_parser_parse_tentatively (parser);
29251       decl = cp_parser_primary_expression (parser, false, false,
29252 					   false, &idk);
29253       if (!cp_parser_error_occurred (parser)
29254 	  && decl
29255 	  && DECL_P (decl)
29256 	  && CLASS_TYPE_P (TREE_TYPE (decl)))
29257 	{
29258 	  tree rhs;
29259 
29260 	  cp_parser_parse_definitely (parser);
29261 	  cp_parser_require (parser, CPP_EQ, RT_EQ);
29262 	  rhs = cp_parser_assignment_expression (parser, false, NULL);
29263 	  finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29264 						 decl, NOP_EXPR,
29265 						 rhs,
29266 						 tf_warning_or_error));
29267 	  add_private_clause = true;
29268 	}
29269       else
29270 	{
29271 	  decl = NULL;
29272 	  cp_parser_abort_tentative_parse (parser);
29273 	  init = cp_parser_expression (parser, false, NULL);
29274 	  if (init)
29275 	    {
29276 	      if (TREE_CODE (init) == MODIFY_EXPR
29277 		  || TREE_CODE (init) == MODOP_EXPR)
29278 		real_decl = TREE_OPERAND (init, 0);
29279 	    }
29280 	}
29281     }
29282   return add_private_clause;
29283 }
29284 
29285 /* Parse the restricted form of the for statement allowed by OpenMP.  */
29286 
29287 static tree
cp_parser_omp_for_loop(cp_parser * parser,enum tree_code code,tree clauses,tree * cclauses)29288 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29289 			tree *cclauses)
29290 {
29291   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29292   tree real_decl, initv, condv, incrv, declv;
29293   tree this_pre_body, cl;
29294   location_t loc_first;
29295   bool collapse_err = false;
29296   int i, collapse = 1, nbraces = 0;
29297   vec<tree, va_gc> *for_block = make_tree_vector ();
29298 
29299   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29300     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29301       collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
29302 
29303   gcc_assert (collapse >= 1);
29304 
29305   declv = make_tree_vec (collapse);
29306   initv = make_tree_vec (collapse);
29307   condv = make_tree_vec (collapse);
29308   incrv = make_tree_vec (collapse);
29309 
29310   loc_first = cp_lexer_peek_token (parser->lexer)->location;
29311 
29312   for (i = 0; i < collapse; i++)
29313     {
29314       int bracecount = 0;
29315       bool add_private_clause = false;
29316       location_t loc;
29317 
29318       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29319 	{
29320 	  cp_parser_error (parser, "for statement expected");
29321 	  return NULL;
29322 	}
29323       loc = cp_lexer_consume_token (parser->lexer)->location;
29324 
29325       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29326 	return NULL;
29327 
29328       init = decl = real_decl = NULL;
29329       this_pre_body = push_stmt_list ();
29330 
29331       add_private_clause
29332 	|= cp_parser_omp_for_loop_init (parser,
29333 					/*parsing_openmp=*/code != CILK_SIMD,
29334 					this_pre_body, for_block,
29335 					init, decl, real_decl);
29336 
29337       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29338       if (this_pre_body)
29339 	{
29340 	  this_pre_body = pop_stmt_list (this_pre_body);
29341 	  if (pre_body)
29342 	    {
29343 	      tree t = pre_body;
29344 	      pre_body = push_stmt_list ();
29345 	      add_stmt (t);
29346 	      add_stmt (this_pre_body);
29347 	      pre_body = pop_stmt_list (pre_body);
29348 	    }
29349 	  else
29350 	    pre_body = this_pre_body;
29351 	}
29352 
29353       if (decl)
29354 	real_decl = decl;
29355       if (cclauses != NULL
29356 	  && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29357 	  && real_decl != NULL_TREE)
29358 	{
29359 	  tree *c;
29360 	  for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29361 	    if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29362 		&& OMP_CLAUSE_DECL (*c) == real_decl)
29363 	      {
29364 		error_at (loc, "iteration variable %qD"
29365 			  " should not be firstprivate", real_decl);
29366 		*c = OMP_CLAUSE_CHAIN (*c);
29367 	      }
29368 	    else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29369 		     && OMP_CLAUSE_DECL (*c) == real_decl)
29370 	      {
29371 		/* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29372 		   change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
29373 		tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29374 		OMP_CLAUSE_DECL (l) = real_decl;
29375 		CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29376 		if (code == OMP_SIMD)
29377 		  {
29378 		    OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29379 		    cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
29380 		  }
29381 		else
29382 		  {
29383 		    OMP_CLAUSE_CHAIN (l) = clauses;
29384 		    clauses = l;
29385 		  }
29386 		OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29387 		CP_OMP_CLAUSE_INFO (*c) = NULL;
29388 		add_private_clause = false;
29389 	      }
29390 	    else
29391 	      {
29392 		if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29393 		    && OMP_CLAUSE_DECL (*c) == real_decl)
29394 		  add_private_clause = false;
29395 		c = &OMP_CLAUSE_CHAIN (*c);
29396 	      }
29397 	}
29398 
29399       if (add_private_clause)
29400 	{
29401 	  tree c;
29402 	  for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29403 	    {
29404 	      if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29405 		   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29406 		  && OMP_CLAUSE_DECL (c) == decl)
29407 		break;
29408 	      else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29409 		       && OMP_CLAUSE_DECL (c) == decl)
29410 		error_at (loc, "iteration variable %qD "
29411 			  "should not be firstprivate",
29412 			  decl);
29413 	      else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29414 		       && OMP_CLAUSE_DECL (c) == decl)
29415 		error_at (loc, "iteration variable %qD should not be reduction",
29416 			  decl);
29417 	    }
29418 	  if (c == NULL)
29419 	    {
29420 	      c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29421 	      OMP_CLAUSE_DECL (c) = decl;
29422 	      c = finish_omp_clauses (c);
29423 	      if (c)
29424 		{
29425 		  OMP_CLAUSE_CHAIN (c) = clauses;
29426 		  clauses = c;
29427 		}
29428 	    }
29429 	}
29430 
29431       cond = NULL;
29432       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29433 	cond = cp_parser_omp_for_cond (parser, decl, code);
29434       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29435 
29436       incr = NULL;
29437       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29438 	{
29439 	  /* If decl is an iterator, preserve the operator on decl
29440 	     until finish_omp_for.  */
29441 	  if (real_decl
29442 	      && ((processing_template_decl
29443 		   && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29444 		  || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29445 	    incr = cp_parser_omp_for_incr (parser, real_decl);
29446 	  else
29447 	    incr = cp_parser_expression (parser, false, NULL);
29448 	  if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29449 	    SET_EXPR_LOCATION (incr, input_location);
29450 	}
29451 
29452       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29453 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29454 					       /*or_comma=*/false,
29455 					       /*consume_paren=*/true);
29456 
29457       TREE_VEC_ELT (declv, i) = decl;
29458       TREE_VEC_ELT (initv, i) = init;
29459       TREE_VEC_ELT (condv, i) = cond;
29460       TREE_VEC_ELT (incrv, i) = incr;
29461 
29462       if (i == collapse - 1)
29463 	break;
29464 
29465       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29466 	 in between the collapsed for loops to be still considered perfectly
29467 	 nested.  Hopefully the final version clarifies this.
29468 	 For now handle (multiple) {'s and empty statements.  */
29469       cp_parser_parse_tentatively (parser);
29470       do
29471 	{
29472 	  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29473 	    break;
29474 	  else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29475 	    {
29476 	      cp_lexer_consume_token (parser->lexer);
29477 	      bracecount++;
29478 	    }
29479 	  else if (bracecount
29480 		   && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29481 	    cp_lexer_consume_token (parser->lexer);
29482 	  else
29483 	    {
29484 	      loc = cp_lexer_peek_token (parser->lexer)->location;
29485 	      error_at (loc, "not enough collapsed for loops");
29486 	      collapse_err = true;
29487 	      cp_parser_abort_tentative_parse (parser);
29488 	      declv = NULL_TREE;
29489 	      break;
29490 	    }
29491 	}
29492       while (1);
29493 
29494       if (declv)
29495 	{
29496 	  cp_parser_parse_definitely (parser);
29497 	  nbraces += bracecount;
29498 	}
29499     }
29500 
29501   /* Note that we saved the original contents of this flag when we entered
29502      the structured block, and so we don't need to re-save it here.  */
29503   if (code == CILK_SIMD)
29504     parser->in_statement = IN_CILK_SIMD_FOR;
29505   else
29506     parser->in_statement = IN_OMP_FOR;
29507 
29508   /* Note that the grammar doesn't call for a structured block here,
29509      though the loop as a whole is a structured block.  */
29510   body = push_stmt_list ();
29511   cp_parser_statement (parser, NULL_TREE, false, NULL);
29512   body = pop_stmt_list (body);
29513 
29514   if (declv == NULL_TREE)
29515     ret = NULL_TREE;
29516   else
29517     ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
29518 			  pre_body, clauses);
29519 
29520   while (nbraces)
29521     {
29522       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29523 	{
29524 	  cp_lexer_consume_token (parser->lexer);
29525 	  nbraces--;
29526 	}
29527       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29528 	cp_lexer_consume_token (parser->lexer);
29529       else
29530 	{
29531 	  if (!collapse_err)
29532 	    {
29533 	      error_at (cp_lexer_peek_token (parser->lexer)->location,
29534 			"collapsed loops not perfectly nested");
29535 	    }
29536 	  collapse_err = true;
29537 	  cp_parser_statement_seq_opt (parser, NULL);
29538 	  if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29539 	    break;
29540 	}
29541     }
29542 
29543   while (!for_block->is_empty ())
29544     add_stmt (pop_stmt_list (for_block->pop ()));
29545   release_tree_vector (for_block);
29546 
29547   return ret;
29548 }
29549 
29550 /* Helper function for OpenMP parsing, split clauses and call
29551    finish_omp_clauses on each of the set of clauses afterwards.  */
29552 
29553 static void
cp_omp_split_clauses(location_t loc,enum tree_code code,omp_clause_mask mask,tree clauses,tree * cclauses)29554 cp_omp_split_clauses (location_t loc, enum tree_code code,
29555 		      omp_clause_mask mask, tree clauses, tree *cclauses)
29556 {
29557   int i;
29558   c_omp_split_clauses (loc, code, mask, clauses, cclauses);
29559   for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
29560     if (cclauses[i])
29561       cclauses[i] = finish_omp_clauses (cclauses[i]);
29562 }
29563 
29564 /* OpenMP 4.0:
29565    #pragma omp simd simd-clause[optseq] new-line
29566      for-loop  */
29567 
29568 #define OMP_SIMD_CLAUSE_MASK					\
29569 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN)	\
29570 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)	\
29571 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)	\
29572 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
29573 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
29574 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
29575 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29576 
29577 static tree
cp_parser_omp_simd(cp_parser * parser,cp_token * pragma_tok,char * p_name,omp_clause_mask mask,tree * cclauses)29578 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
29579 		    char *p_name, omp_clause_mask mask, tree *cclauses)
29580 {
29581   tree clauses, sb, ret;
29582   unsigned int save;
29583   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29584 
29585   strcat (p_name, " simd");
29586   mask |= OMP_SIMD_CLAUSE_MASK;
29587   mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
29588 
29589   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29590 				       cclauses == NULL);
29591   if (cclauses)
29592     {
29593       cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
29594       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
29595     }
29596 
29597   sb = begin_omp_structured_block ();
29598   save = cp_parser_begin_omp_structured_block (parser);
29599 
29600   ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
29601 
29602   cp_parser_end_omp_structured_block (parser, save);
29603   add_stmt (finish_omp_structured_block (sb));
29604 
29605   return ret;
29606 }
29607 
29608 /* OpenMP 2.5:
29609    #pragma omp for for-clause[optseq] new-line
29610      for-loop
29611 
29612    OpenMP 4.0:
29613    #pragma omp for simd for-simd-clause[optseq] new-line
29614      for-loop  */
29615 
29616 #define OMP_FOR_CLAUSE_MASK					\
29617 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
29618 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
29619 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
29620 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
29621 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED)	\
29622 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)	\
29623 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)	\
29624 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29625 
29626 static tree
cp_parser_omp_for(cp_parser * parser,cp_token * pragma_tok,char * p_name,omp_clause_mask mask,tree * cclauses)29627 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
29628 		   char *p_name, omp_clause_mask mask, tree *cclauses)
29629 {
29630   tree clauses, sb, ret;
29631   unsigned int save;
29632   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29633 
29634   strcat (p_name, " for");
29635   mask |= OMP_FOR_CLAUSE_MASK;
29636   if (cclauses)
29637     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29638 
29639   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29640     {
29641       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29642       const char *p = IDENTIFIER_POINTER (id);
29643 
29644       if (strcmp (p, "simd") == 0)
29645 	{
29646 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29647 	  if (cclauses == NULL)
29648 	    cclauses = cclauses_buf;
29649 
29650 	  cp_lexer_consume_token (parser->lexer);
29651 	  if (!flag_openmp)  /* flag_openmp_simd  */
29652 	    return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29653 				       cclauses);
29654 	  sb = begin_omp_structured_block ();
29655 	  save = cp_parser_begin_omp_structured_block (parser);
29656 	  ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29657 				    cclauses);
29658 	  cp_parser_end_omp_structured_block (parser, save);
29659 	  tree body = finish_omp_structured_block (sb);
29660 	  if (ret == NULL)
29661 	    return ret;
29662 	  ret = make_node (OMP_FOR);
29663 	  TREE_TYPE (ret) = void_type_node;
29664 	  OMP_FOR_BODY (ret) = body;
29665 	  OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29666 	  SET_EXPR_LOCATION (ret, loc);
29667 	  add_stmt (ret);
29668 	  return ret;
29669 	}
29670     }
29671   if (!flag_openmp)  /* flag_openmp_simd  */
29672     {
29673       cp_parser_require_pragma_eol (parser, pragma_tok);
29674       return NULL_TREE;
29675     }
29676 
29677   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29678 				       cclauses == NULL);
29679   if (cclauses)
29680     {
29681       cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
29682       clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29683     }
29684 
29685   sb = begin_omp_structured_block ();
29686   save = cp_parser_begin_omp_structured_block (parser);
29687 
29688   ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
29689 
29690   cp_parser_end_omp_structured_block (parser, save);
29691   add_stmt (finish_omp_structured_block (sb));
29692 
29693   return ret;
29694 }
29695 
29696 /* OpenMP 2.5:
29697    # pragma omp master new-line
29698      structured-block  */
29699 
29700 static tree
cp_parser_omp_master(cp_parser * parser,cp_token * pragma_tok)29701 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
29702 {
29703   cp_parser_require_pragma_eol (parser, pragma_tok);
29704   return c_finish_omp_master (input_location,
29705 			      cp_parser_omp_structured_block (parser));
29706 }
29707 
29708 /* OpenMP 2.5:
29709    # pragma omp ordered new-line
29710      structured-block  */
29711 
29712 static tree
cp_parser_omp_ordered(cp_parser * parser,cp_token * pragma_tok)29713 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
29714 {
29715   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29716   cp_parser_require_pragma_eol (parser, pragma_tok);
29717   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
29718 }
29719 
29720 /* OpenMP 2.5:
29721 
29722    section-scope:
29723      { section-sequence }
29724 
29725    section-sequence:
29726      section-directive[opt] structured-block
29727      section-sequence section-directive structured-block  */
29728 
29729 static tree
cp_parser_omp_sections_scope(cp_parser * parser)29730 cp_parser_omp_sections_scope (cp_parser *parser)
29731 {
29732   tree stmt, substmt;
29733   bool error_suppress = false;
29734   cp_token *tok;
29735 
29736   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
29737     return NULL_TREE;
29738 
29739   stmt = push_stmt_list ();
29740 
29741   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
29742     {
29743       substmt = cp_parser_omp_structured_block (parser);
29744       substmt = build1 (OMP_SECTION, void_type_node, substmt);
29745       add_stmt (substmt);
29746     }
29747 
29748   while (1)
29749     {
29750       tok = cp_lexer_peek_token (parser->lexer);
29751       if (tok->type == CPP_CLOSE_BRACE)
29752 	break;
29753       if (tok->type == CPP_EOF)
29754 	break;
29755 
29756       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
29757 	{
29758 	  cp_lexer_consume_token (parser->lexer);
29759 	  cp_parser_require_pragma_eol (parser, tok);
29760 	  error_suppress = false;
29761 	}
29762       else if (!error_suppress)
29763 	{
29764 	  cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
29765 	  error_suppress = true;
29766 	}
29767 
29768       substmt = cp_parser_omp_structured_block (parser);
29769       substmt = build1 (OMP_SECTION, void_type_node, substmt);
29770       add_stmt (substmt);
29771     }
29772   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29773 
29774   substmt = pop_stmt_list (stmt);
29775 
29776   stmt = make_node (OMP_SECTIONS);
29777   TREE_TYPE (stmt) = void_type_node;
29778   OMP_SECTIONS_BODY (stmt) = substmt;
29779 
29780   add_stmt (stmt);
29781   return stmt;
29782 }
29783 
29784 /* OpenMP 2.5:
29785    # pragma omp sections sections-clause[optseq] newline
29786      sections-scope  */
29787 
29788 #define OMP_SECTIONS_CLAUSE_MASK				\
29789 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
29790 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
29791 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
29792 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
29793 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29794 
29795 static tree
cp_parser_omp_sections(cp_parser * parser,cp_token * pragma_tok,char * p_name,omp_clause_mask mask,tree * cclauses)29796 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
29797 			char *p_name, omp_clause_mask mask, tree *cclauses)
29798 {
29799   tree clauses, ret;
29800   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29801 
29802   strcat (p_name, " sections");
29803   mask |= OMP_SECTIONS_CLAUSE_MASK;
29804   if (cclauses)
29805     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29806 
29807   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29808 				       cclauses == NULL);
29809   if (cclauses)
29810     {
29811       cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
29812       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
29813     }
29814 
29815   ret = cp_parser_omp_sections_scope (parser);
29816   if (ret)
29817     OMP_SECTIONS_CLAUSES (ret) = clauses;
29818 
29819   return ret;
29820 }
29821 
29822 /* OpenMP 2.5:
29823    # pragma omp parallel parallel-clause[optseq] new-line
29824      structured-block
29825    # pragma omp parallel for parallel-for-clause[optseq] new-line
29826      structured-block
29827    # pragma omp parallel sections parallel-sections-clause[optseq] new-line
29828      structured-block
29829 
29830    OpenMP 4.0:
29831    # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
29832      structured-block */
29833 
29834 #define OMP_PARALLEL_CLAUSE_MASK				\
29835 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
29836 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
29837 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
29838 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)	\
29839 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
29840 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN)	\
29841 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
29842 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)	\
29843 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
29844 
29845 static tree
cp_parser_omp_parallel(cp_parser * parser,cp_token * pragma_tok,char * p_name,omp_clause_mask mask,tree * cclauses)29846 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
29847 			char *p_name, omp_clause_mask mask, tree *cclauses)
29848 {
29849   tree stmt, clauses, block;
29850   unsigned int save;
29851   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29852 
29853   strcat (p_name, " parallel");
29854   mask |= OMP_PARALLEL_CLAUSE_MASK;
29855 
29856   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29857     {
29858       tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29859       if (cclauses == NULL)
29860 	cclauses = cclauses_buf;
29861 
29862       cp_lexer_consume_token (parser->lexer);
29863       if (!flag_openmp)  /* flag_openmp_simd  */
29864 	return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
29865       block = begin_omp_parallel ();
29866       save = cp_parser_begin_omp_structured_block (parser);
29867       tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
29868       cp_parser_end_omp_structured_block (parser, save);
29869       stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29870 				  block);
29871       if (ret == NULL_TREE)
29872 	return ret;
29873       OMP_PARALLEL_COMBINED (stmt) = 1;
29874       return stmt;
29875     }
29876   else if (cclauses)
29877     {
29878       error_at (loc, "expected %<for%> after %qs", p_name);
29879       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29880       return NULL_TREE;
29881     }
29882   else if (!flag_openmp)  /* flag_openmp_simd  */
29883     {
29884       cp_parser_require_pragma_eol (parser, pragma_tok);
29885       return NULL_TREE;
29886     }
29887   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29888     {
29889       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29890       const char *p = IDENTIFIER_POINTER (id);
29891       if (strcmp (p, "sections") == 0)
29892 	{
29893 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29894 	  cclauses = cclauses_buf;
29895 
29896 	  cp_lexer_consume_token (parser->lexer);
29897 	  block = begin_omp_parallel ();
29898 	  save = cp_parser_begin_omp_structured_block (parser);
29899 	  cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
29900 	  cp_parser_end_omp_structured_block (parser, save);
29901 	  stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29902 				      block);
29903 	  OMP_PARALLEL_COMBINED (stmt) = 1;
29904 	  return stmt;
29905 	}
29906     }
29907 
29908   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
29909 
29910   block = begin_omp_parallel ();
29911   save = cp_parser_begin_omp_structured_block (parser);
29912   cp_parser_statement (parser, NULL_TREE, false, NULL);
29913   cp_parser_end_omp_structured_block (parser, save);
29914   stmt = finish_omp_parallel (clauses, block);
29915   return stmt;
29916 }
29917 
29918 /* OpenMP 2.5:
29919    # pragma omp single single-clause[optseq] new-line
29920      structured-block  */
29921 
29922 #define OMP_SINGLE_CLAUSE_MASK					\
29923 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
29924 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
29925 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE)	\
29926 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29927 
29928 static tree
cp_parser_omp_single(cp_parser * parser,cp_token * pragma_tok)29929 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
29930 {
29931   tree stmt = make_node (OMP_SINGLE);
29932   TREE_TYPE (stmt) = void_type_node;
29933 
29934   OMP_SINGLE_CLAUSES (stmt)
29935     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
29936 				 "#pragma omp single", pragma_tok);
29937   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
29938 
29939   return add_stmt (stmt);
29940 }
29941 
29942 /* OpenMP 3.0:
29943    # pragma omp task task-clause[optseq] new-line
29944      structured-block  */
29945 
29946 #define OMP_TASK_CLAUSE_MASK					\
29947 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
29948 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)	\
29949 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)	\
29950 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
29951 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
29952 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
29953 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)	\
29954 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)	\
29955 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
29956 
29957 static tree
cp_parser_omp_task(cp_parser * parser,cp_token * pragma_tok)29958 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
29959 {
29960   tree clauses, block;
29961   unsigned int save;
29962 
29963   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
29964 				       "#pragma omp task", pragma_tok);
29965   block = begin_omp_task ();
29966   save = cp_parser_begin_omp_structured_block (parser);
29967   cp_parser_statement (parser, NULL_TREE, false, NULL);
29968   cp_parser_end_omp_structured_block (parser, save);
29969   return finish_omp_task (clauses, block);
29970 }
29971 
29972 /* OpenMP 3.0:
29973    # pragma omp taskwait new-line  */
29974 
29975 static void
cp_parser_omp_taskwait(cp_parser * parser,cp_token * pragma_tok)29976 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
29977 {
29978   cp_parser_require_pragma_eol (parser, pragma_tok);
29979   finish_omp_taskwait ();
29980 }
29981 
29982 /* OpenMP 3.1:
29983    # pragma omp taskyield new-line  */
29984 
29985 static void
cp_parser_omp_taskyield(cp_parser * parser,cp_token * pragma_tok)29986 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
29987 {
29988   cp_parser_require_pragma_eol (parser, pragma_tok);
29989   finish_omp_taskyield ();
29990 }
29991 
29992 /* OpenMP 4.0:
29993    # pragma omp taskgroup new-line
29994      structured-block  */
29995 
29996 static tree
cp_parser_omp_taskgroup(cp_parser * parser,cp_token * pragma_tok)29997 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
29998 {
29999   cp_parser_require_pragma_eol (parser, pragma_tok);
30000   return c_finish_omp_taskgroup (input_location,
30001 				 cp_parser_omp_structured_block (parser));
30002 }
30003 
30004 
30005 /* OpenMP 2.5:
30006    # pragma omp threadprivate (variable-list) */
30007 
30008 static void
cp_parser_omp_threadprivate(cp_parser * parser,cp_token * pragma_tok)30009 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30010 {
30011   tree vars;
30012 
30013   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30014   cp_parser_require_pragma_eol (parser, pragma_tok);
30015 
30016   finish_omp_threadprivate (vars);
30017 }
30018 
30019 /* OpenMP 4.0:
30020    # pragma omp cancel cancel-clause[optseq] new-line  */
30021 
30022 #define OMP_CANCEL_CLAUSE_MASK					\
30023 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)	\
30024 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)		\
30025 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)	\
30026 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP)	\
30027 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30028 
30029 static void
cp_parser_omp_cancel(cp_parser * parser,cp_token * pragma_tok)30030 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
30031 {
30032   tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
30033 					    "#pragma omp cancel", pragma_tok);
30034   finish_omp_cancel (clauses);
30035 }
30036 
30037 /* OpenMP 4.0:
30038    # pragma omp cancellation point cancelpt-clause[optseq] new-line  */
30039 
30040 #define OMP_CANCELLATION_POINT_CLAUSE_MASK			\
30041 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)	\
30042 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)		\
30043 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)	\
30044 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30045 
30046 static void
cp_parser_omp_cancellation_point(cp_parser * parser,cp_token * pragma_tok)30047 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30048 {
30049   tree clauses;
30050   bool point_seen = false;
30051 
30052   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30053     {
30054       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30055       const char *p = IDENTIFIER_POINTER (id);
30056 
30057       if (strcmp (p, "point") == 0)
30058 	{
30059 	  cp_lexer_consume_token (parser->lexer);
30060 	  point_seen = true;
30061 	}
30062     }
30063   if (!point_seen)
30064     {
30065       cp_parser_error (parser, "expected %<point%>");
30066       cp_parser_require_pragma_eol (parser, pragma_tok);
30067       return;
30068     }
30069 
30070   clauses = cp_parser_omp_all_clauses (parser,
30071 				       OMP_CANCELLATION_POINT_CLAUSE_MASK,
30072 				       "#pragma omp cancellation point",
30073 				       pragma_tok);
30074   finish_omp_cancellation_point (clauses);
30075 }
30076 
30077 /* OpenMP 4.0:
30078    #pragma omp distribute distribute-clause[optseq] new-line
30079      for-loop  */
30080 
30081 #define OMP_DISTRIBUTE_CLAUSE_MASK				\
30082 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
30083 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
30084 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30085 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30086 
30087 static tree
cp_parser_omp_distribute(cp_parser * parser,cp_token * pragma_tok,char * p_name,omp_clause_mask mask,tree * cclauses)30088 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30089 			  char *p_name, omp_clause_mask mask, tree *cclauses)
30090 {
30091   tree clauses, sb, ret;
30092   unsigned int save;
30093   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30094 
30095   strcat (p_name, " distribute");
30096   mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30097 
30098   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30099     {
30100       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30101       const char *p = IDENTIFIER_POINTER (id);
30102       bool simd = false;
30103       bool parallel = false;
30104 
30105       if (strcmp (p, "simd") == 0)
30106 	simd = true;
30107       else
30108 	parallel = strcmp (p, "parallel") == 0;
30109       if (parallel || simd)
30110 	{
30111 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30112 	  if (cclauses == NULL)
30113 	    cclauses = cclauses_buf;
30114 	  cp_lexer_consume_token (parser->lexer);
30115 	  if (!flag_openmp)  /* flag_openmp_simd  */
30116 	    {
30117 	      if (simd)
30118 		return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30119 					   cclauses);
30120 	      else
30121 		return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30122 					       cclauses);
30123 	    }
30124 	  sb = begin_omp_structured_block ();
30125 	  save = cp_parser_begin_omp_structured_block (parser);
30126 	  if (simd)
30127 	    ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30128 				      cclauses);
30129 	  else
30130 	    ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30131 					  cclauses);
30132 	  cp_parser_end_omp_structured_block (parser, save);
30133 	  tree body = finish_omp_structured_block (sb);
30134 	  if (ret == NULL)
30135 	    return ret;
30136 	  ret = make_node (OMP_DISTRIBUTE);
30137 	  TREE_TYPE (ret) = void_type_node;
30138 	  OMP_FOR_BODY (ret) = body;
30139 	  OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30140 	  SET_EXPR_LOCATION (ret, loc);
30141 	  add_stmt (ret);
30142 	  return ret;
30143 	}
30144     }
30145   if (!flag_openmp)  /* flag_openmp_simd  */
30146     {
30147       cp_parser_require_pragma_eol (parser, pragma_tok);
30148       return NULL_TREE;
30149     }
30150 
30151   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30152 				       cclauses == NULL);
30153   if (cclauses)
30154     {
30155       cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30156       clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30157     }
30158 
30159   sb = begin_omp_structured_block ();
30160   save = cp_parser_begin_omp_structured_block (parser);
30161 
30162   ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30163 
30164   cp_parser_end_omp_structured_block (parser, save);
30165   add_stmt (finish_omp_structured_block (sb));
30166 
30167   return ret;
30168 }
30169 
30170 /* OpenMP 4.0:
30171    # pragma omp teams teams-clause[optseq] new-line
30172      structured-block  */
30173 
30174 #define OMP_TEAMS_CLAUSE_MASK					\
30175 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
30176 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
30177 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
30178 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
30179 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)	\
30180 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT)	\
30181 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30182 
30183 static tree
cp_parser_omp_teams(cp_parser * parser,cp_token * pragma_tok,char * p_name,omp_clause_mask mask,tree * cclauses)30184 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30185 		     char *p_name, omp_clause_mask mask, tree *cclauses)
30186 {
30187   tree clauses, sb, ret;
30188   unsigned int save;
30189   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30190 
30191   strcat (p_name, " teams");
30192   mask |= OMP_TEAMS_CLAUSE_MASK;
30193 
30194   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30195     {
30196       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30197       const char *p = IDENTIFIER_POINTER (id);
30198       if (strcmp (p, "distribute") == 0)
30199 	{
30200 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30201 	  if (cclauses == NULL)
30202 	    cclauses = cclauses_buf;
30203 
30204 	  cp_lexer_consume_token (parser->lexer);
30205 	  if (!flag_openmp)  /* flag_openmp_simd  */
30206 	    return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30207 					     cclauses);
30208 	  sb = begin_omp_structured_block ();
30209 	  save = cp_parser_begin_omp_structured_block (parser);
30210 	  ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30211 					  cclauses);
30212 	  cp_parser_end_omp_structured_block (parser, save);
30213 	  tree body = finish_omp_structured_block (sb);
30214 	  if (ret == NULL)
30215 	    return ret;
30216 	  clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30217 	  ret = make_node (OMP_TEAMS);
30218 	  TREE_TYPE (ret) = void_type_node;
30219 	  OMP_TEAMS_CLAUSES (ret) = clauses;
30220 	  OMP_TEAMS_BODY (ret) = body;
30221 	  return add_stmt (ret);
30222 	}
30223     }
30224   if (!flag_openmp)  /* flag_openmp_simd  */
30225     {
30226       cp_parser_require_pragma_eol (parser, pragma_tok);
30227       return NULL_TREE;
30228     }
30229 
30230   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30231 				       cclauses == NULL);
30232   if (cclauses)
30233     {
30234       cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30235       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30236     }
30237 
30238   tree stmt = make_node (OMP_TEAMS);
30239   TREE_TYPE (stmt) = void_type_node;
30240   OMP_TEAMS_CLAUSES (stmt) = clauses;
30241   OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30242 
30243   return add_stmt (stmt);
30244 }
30245 
30246 /* OpenMP 4.0:
30247    # pragma omp target data target-data-clause[optseq] new-line
30248      structured-block  */
30249 
30250 #define OMP_TARGET_DATA_CLAUSE_MASK				\
30251 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
30252 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
30253 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30254 
30255 static tree
cp_parser_omp_target_data(cp_parser * parser,cp_token * pragma_tok)30256 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30257 {
30258   tree stmt = make_node (OMP_TARGET_DATA);
30259   TREE_TYPE (stmt) = void_type_node;
30260 
30261   OMP_TARGET_DATA_CLAUSES (stmt)
30262     = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30263 				 "#pragma omp target data", pragma_tok);
30264   keep_next_level (true);
30265   OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30266 
30267   SET_EXPR_LOCATION (stmt, pragma_tok->location);
30268   return add_stmt (stmt);
30269 }
30270 
30271 /* OpenMP 4.0:
30272    # pragma omp target update target-update-clause[optseq] new-line */
30273 
30274 #define OMP_TARGET_UPDATE_CLAUSE_MASK				\
30275 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM)		\
30276 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)		\
30277 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
30278 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30279 
30280 static bool
cp_parser_omp_target_update(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)30281 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30282 			     enum pragma_context context)
30283 {
30284   if (context == pragma_stmt)
30285     {
30286       error_at (pragma_tok->location,
30287 		"%<#pragma omp target update%> may only be "
30288 		"used in compound statements");
30289       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30290       return false;
30291     }
30292 
30293   tree clauses
30294     = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30295 				 "#pragma omp target update", pragma_tok);
30296   if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30297       && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30298     {
30299       error_at (pragma_tok->location,
30300 		"%<#pragma omp target update must contain at least one "
30301 		"%<from%> or %<to%> clauses");
30302       return false;
30303     }
30304 
30305   tree stmt = make_node (OMP_TARGET_UPDATE);
30306   TREE_TYPE (stmt) = void_type_node;
30307   OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30308   SET_EXPR_LOCATION (stmt, pragma_tok->location);
30309   add_stmt (stmt);
30310   return false;
30311 }
30312 
30313 /* OpenMP 4.0:
30314    # pragma omp target target-clause[optseq] new-line
30315      structured-block  */
30316 
30317 #define OMP_TARGET_CLAUSE_MASK					\
30318 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
30319 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
30320 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30321 
30322 static bool
cp_parser_omp_target(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)30323 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30324 		      enum pragma_context context)
30325 {
30326   if (context != pragma_stmt && context != pragma_compound)
30327     {
30328       cp_parser_error (parser, "expected declaration specifiers");
30329       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30330       return false;
30331     }
30332 
30333   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30334     {
30335       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30336       const char *p = IDENTIFIER_POINTER (id);
30337 
30338       if (strcmp (p, "teams") == 0)
30339 	{
30340 	  tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30341 	  char p_name[sizeof ("#pragma omp target teams distribute "
30342 			      "parallel for simd")];
30343 
30344 	  cp_lexer_consume_token (parser->lexer);
30345 	  strcpy (p_name, "#pragma omp target");
30346 	  if (!flag_openmp)  /* flag_openmp_simd  */
30347 	    return cp_parser_omp_teams (parser, pragma_tok, p_name,
30348 					OMP_TARGET_CLAUSE_MASK, cclauses);
30349 	  keep_next_level (true);
30350 	  tree sb = begin_omp_structured_block ();
30351 	  unsigned save = cp_parser_begin_omp_structured_block (parser);
30352 	  tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30353 					  OMP_TARGET_CLAUSE_MASK, cclauses);
30354 	  cp_parser_end_omp_structured_block (parser, save);
30355 	  tree body = finish_omp_structured_block (sb);
30356 	  if (ret == NULL)
30357 	    return ret;
30358 	  tree stmt = make_node (OMP_TARGET);
30359 	  TREE_TYPE (stmt) = void_type_node;
30360 	  OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30361 	  OMP_TARGET_BODY (stmt) = body;
30362 	  add_stmt (stmt);
30363 	  return true;
30364 	}
30365       else if (!flag_openmp)  /* flag_openmp_simd  */
30366 	{
30367 	  cp_parser_require_pragma_eol (parser, pragma_tok);
30368 	  return NULL_TREE;
30369 	}
30370       else if (strcmp (p, "data") == 0)
30371 	{
30372 	  cp_lexer_consume_token (parser->lexer);
30373 	  cp_parser_omp_target_data (parser, pragma_tok);
30374 	  return true;
30375 	}
30376       else if (strcmp (p, "update") == 0)
30377 	{
30378 	  cp_lexer_consume_token (parser->lexer);
30379 	  return cp_parser_omp_target_update (parser, pragma_tok, context);
30380 	}
30381     }
30382 
30383   tree stmt = make_node (OMP_TARGET);
30384   TREE_TYPE (stmt) = void_type_node;
30385 
30386   OMP_TARGET_CLAUSES (stmt)
30387     = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30388 				 "#pragma omp target", pragma_tok);
30389   keep_next_level (true);
30390   OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30391 
30392   SET_EXPR_LOCATION (stmt, pragma_tok->location);
30393   add_stmt (stmt);
30394   return true;
30395 }
30396 
30397 /* OpenMP 4.0:
30398    # pragma omp declare simd declare-simd-clauses[optseq] new-line  */
30399 
30400 #define OMP_DECLARE_SIMD_CLAUSE_MASK				\
30401 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)	\
30402 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)	\
30403 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)	\
30404 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)	\
30405 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH)	\
30406 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30407 
30408 static void
cp_parser_omp_declare_simd(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)30409 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30410 			    enum pragma_context context)
30411 {
30412   bool first_p = parser->omp_declare_simd == NULL;
30413   cp_omp_declare_simd_data data;
30414   if (first_p)
30415     {
30416       data.error_seen = false;
30417       data.fndecl_seen = false;
30418       data.tokens = vNULL;
30419       parser->omp_declare_simd = &data;
30420     }
30421   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30422 	 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30423     cp_lexer_consume_token (parser->lexer);
30424   if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30425     parser->omp_declare_simd->error_seen = true;
30426   cp_parser_require_pragma_eol (parser, pragma_tok);
30427   struct cp_token_cache *cp
30428     = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30429   parser->omp_declare_simd->tokens.safe_push (cp);
30430   if (first_p)
30431     {
30432       while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30433 	cp_parser_pragma (parser, context);
30434       switch (context)
30435 	{
30436 	case pragma_external:
30437 	  cp_parser_declaration (parser);
30438 	  break;
30439 	case pragma_member:
30440 	  cp_parser_member_declaration (parser);
30441 	  break;
30442 	case pragma_objc_icode:
30443 	  cp_parser_block_declaration (parser, /*statement_p=*/false);
30444 	  break;
30445 	default:
30446 	  cp_parser_declaration_statement (parser);
30447 	  break;
30448 	}
30449       if (parser->omp_declare_simd
30450 	  && !parser->omp_declare_simd->error_seen
30451 	  && !parser->omp_declare_simd->fndecl_seen)
30452 	error_at (pragma_tok->location,
30453 		  "%<#pragma omp declare simd%> not immediately followed by "
30454 		  "function declaration or definition");
30455       data.tokens.release ();
30456       parser->omp_declare_simd = NULL;
30457     }
30458 }
30459 
30460 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30461    This function is modelled similar to the late parsing of omp declare
30462    simd.  */
30463 
30464 static tree
cp_parser_late_parsing_cilk_simd_fn_info(cp_parser * parser,tree attrs)30465 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
30466 {
30467   struct cp_token_cache *ce;
30468   cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
30469   int ii = 0;
30470 
30471   if (parser->omp_declare_simd != NULL)
30472     {
30473       error ("%<#pragma omp declare simd%> cannot be used in the same function"
30474 	     " marked as a Cilk Plus SIMD-enabled function");
30475       XDELETE (parser->cilk_simd_fn_info);
30476       parser->cilk_simd_fn_info = NULL;
30477       return attrs;
30478     }
30479   if (!info->error_seen && info->fndecl_seen)
30480     {
30481       error ("vector attribute not immediately followed by a single function"
30482 	     " declaration or definition");
30483       info->error_seen = true;
30484     }
30485   if (info->error_seen)
30486     return attrs;
30487 
30488   FOR_EACH_VEC_ELT (info->tokens, ii, ce)
30489     {
30490       tree c, cl;
30491 
30492       cp_parser_push_lexer_for_tokens (parser, ce);
30493       parser->lexer->in_pragma = true;
30494       cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
30495 				      "SIMD-enabled functions attribute",
30496 				      NULL);
30497       cp_parser_pop_lexer (parser);
30498       if (cl)
30499 	cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30500 
30501       c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
30502       TREE_CHAIN (c) = attrs;
30503       attrs = c;
30504 
30505       c = build_tree_list (get_identifier ("omp declare simd"), cl);
30506       TREE_CHAIN (c) = attrs;
30507       if (processing_template_decl)
30508 	ATTR_IS_DEPENDENT (c) = 1;
30509       attrs = c;
30510     }
30511   info->fndecl_seen = true;
30512   XDELETE (parser->cilk_simd_fn_info);
30513   parser->cilk_simd_fn_info = NULL;
30514   return attrs;
30515 }
30516 
30517 /* Finalize #pragma omp declare simd clauses after direct declarator has
30518    been parsed, and put that into "omp declare simd" attribute.  */
30519 
30520 static tree
cp_parser_late_parsing_omp_declare_simd(cp_parser * parser,tree attrs)30521 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
30522 {
30523   struct cp_token_cache *ce;
30524   cp_omp_declare_simd_data *data = parser->omp_declare_simd;
30525   int i;
30526 
30527   if (!data->error_seen && data->fndecl_seen)
30528     {
30529       error ("%<#pragma omp declare simd%> not immediately followed by "
30530 	     "a single function declaration or definition");
30531       data->error_seen = true;
30532       return attrs;
30533     }
30534   if (data->error_seen)
30535     return attrs;
30536 
30537   FOR_EACH_VEC_ELT (data->tokens, i, ce)
30538     {
30539       tree c, cl;
30540 
30541       cp_parser_push_lexer_for_tokens (parser, ce);
30542       parser->lexer->in_pragma = true;
30543       gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
30544       cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
30545       cp_lexer_consume_token (parser->lexer);
30546       cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
30547 				      "#pragma omp declare simd", pragma_tok);
30548       cp_parser_pop_lexer (parser);
30549       if (cl)
30550 	cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30551       c = build_tree_list (get_identifier ("omp declare simd"), cl);
30552       TREE_CHAIN (c) = attrs;
30553       if (processing_template_decl)
30554 	ATTR_IS_DEPENDENT (c) = 1;
30555       attrs = c;
30556     }
30557 
30558   data->fndecl_seen = true;
30559   return attrs;
30560 }
30561 
30562 
30563 /* OpenMP 4.0:
30564    # pragma omp declare target new-line
30565    declarations and definitions
30566    # pragma omp end declare target new-line  */
30567 
30568 static void
cp_parser_omp_declare_target(cp_parser * parser,cp_token * pragma_tok)30569 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
30570 {
30571   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30572   scope_chain->omp_declare_target_attribute++;
30573 }
30574 
30575 static void
cp_parser_omp_end_declare_target(cp_parser * parser,cp_token * pragma_tok)30576 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
30577 {
30578   const char *p = "";
30579   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30580     {
30581       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30582       p = IDENTIFIER_POINTER (id);
30583     }
30584   if (strcmp (p, "declare") == 0)
30585     {
30586       cp_lexer_consume_token (parser->lexer);
30587       p = "";
30588       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30589 	{
30590 	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30591 	  p = IDENTIFIER_POINTER (id);
30592 	}
30593       if (strcmp (p, "target") == 0)
30594 	cp_lexer_consume_token (parser->lexer);
30595       else
30596 	{
30597 	  cp_parser_error (parser, "expected %<target%>");
30598 	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30599 	  return;
30600 	}
30601     }
30602   else
30603     {
30604       cp_parser_error (parser, "expected %<declare%>");
30605       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30606       return;
30607     }
30608   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30609   if (!scope_chain->omp_declare_target_attribute)
30610     error_at (pragma_tok->location,
30611 	      "%<#pragma omp end declare target%> without corresponding "
30612 	      "%<#pragma omp declare target%>");
30613   else
30614     scope_chain->omp_declare_target_attribute--;
30615 }
30616 
30617 /* Helper function of cp_parser_omp_declare_reduction.  Parse the combiner
30618    expression and optional initializer clause of
30619    #pragma omp declare reduction.  We store the expression(s) as
30620    either 3, 6 or 7 special statements inside of the artificial function's
30621    body.  The first two statements are DECL_EXPRs for the artificial
30622    OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
30623    expression that uses those variables.
30624    If there was any INITIALIZER clause, this is followed by further statements,
30625    the fourth and fifth statements are DECL_EXPRs for the artificial
30626    OMP_PRIV resp. OMP_ORIG variables.  If the INITIALIZER clause wasn't the
30627    constructor variant (first token after open paren is not omp_priv),
30628    then the sixth statement is a statement with the function call expression
30629    that uses the OMP_PRIV and optionally OMP_ORIG variable.
30630    Otherwise, the sixth statement is whatever statement cp_finish_decl emits
30631    to initialize the OMP_PRIV artificial variable and there is seventh
30632    statement, a DECL_EXPR of the OMP_PRIV statement again.  */
30633 
30634 static bool
cp_parser_omp_declare_reduction_exprs(tree fndecl,cp_parser * parser)30635 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
30636 {
30637   tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
30638   gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
30639   type = TREE_TYPE (type);
30640   tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
30641   DECL_ARTIFICIAL (omp_out) = 1;
30642   pushdecl (omp_out);
30643   add_decl_expr (omp_out);
30644   tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
30645   DECL_ARTIFICIAL (omp_in) = 1;
30646   pushdecl (omp_in);
30647   add_decl_expr (omp_in);
30648   tree combiner;
30649   tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
30650 
30651   keep_next_level (true);
30652   tree block = begin_omp_structured_block ();
30653   combiner = cp_parser_expression (parser, false, NULL);
30654   finish_expr_stmt (combiner);
30655   block = finish_omp_structured_block (block);
30656   add_stmt (block);
30657 
30658   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30659     return false;
30660 
30661   const char *p = "";
30662   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30663     {
30664       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30665       p = IDENTIFIER_POINTER (id);
30666     }
30667 
30668   if (strcmp (p, "initializer") == 0)
30669     {
30670       cp_lexer_consume_token (parser->lexer);
30671       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30672 	return false;
30673 
30674       p = "";
30675       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30676 	{
30677 	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30678 	  p = IDENTIFIER_POINTER (id);
30679 	}
30680 
30681       omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
30682       DECL_ARTIFICIAL (omp_priv) = 1;
30683       pushdecl (omp_priv);
30684       add_decl_expr (omp_priv);
30685       omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
30686       DECL_ARTIFICIAL (omp_orig) = 1;
30687       pushdecl (omp_orig);
30688       add_decl_expr (omp_orig);
30689 
30690       keep_next_level (true);
30691       block = begin_omp_structured_block ();
30692 
30693       bool ctor = false;
30694       if (strcmp (p, "omp_priv") == 0)
30695 	{
30696 	  bool is_direct_init, is_non_constant_init;
30697 	  ctor = true;
30698 	  cp_lexer_consume_token (parser->lexer);
30699 	  /* Reject initializer (omp_priv) and initializer (omp_priv ()).  */
30700 	  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
30701 	      || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30702 		  && cp_lexer_peek_nth_token (parser->lexer, 2)->type
30703 		     == CPP_CLOSE_PAREN
30704 		  && cp_lexer_peek_nth_token (parser->lexer, 3)->type
30705 		     == CPP_CLOSE_PAREN))
30706 	    {
30707 	      finish_omp_structured_block (block);
30708 	      error ("invalid initializer clause");
30709 	      return false;
30710 	    }
30711 	  initializer = cp_parser_initializer (parser, &is_direct_init,
30712 					       &is_non_constant_init);
30713 	  cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
30714 			  NULL_TREE, LOOKUP_ONLYCONVERTING);
30715 	}
30716       else
30717 	{
30718 	  cp_parser_parse_tentatively (parser);
30719 	  tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
30720 						  /*check_dependency_p=*/true,
30721 						  /*template_p=*/NULL,
30722 						  /*declarator_p=*/false,
30723 						  /*optional_p=*/false);
30724 	  vec<tree, va_gc> *args;
30725 	  if (fn_name == error_mark_node
30726 	      || cp_parser_error_occurred (parser)
30727 	      || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30728 	      || ((args = cp_parser_parenthesized_expression_list
30729 				(parser, non_attr, /*cast_p=*/false,
30730 				 /*allow_expansion_p=*/true,
30731 				 /*non_constant_p=*/NULL)),
30732 		  cp_parser_error_occurred (parser)))
30733 	    {
30734 	      finish_omp_structured_block (block);
30735 	      cp_parser_abort_tentative_parse (parser);
30736 	      cp_parser_error (parser, "expected id-expression (arguments)");
30737 	      return false;
30738 	    }
30739 	  unsigned int i;
30740 	  tree arg;
30741 	  FOR_EACH_VEC_SAFE_ELT (args, i, arg)
30742 	    if (arg == omp_priv
30743 		|| (TREE_CODE (arg) == ADDR_EXPR
30744 		    && TREE_OPERAND (arg, 0) == omp_priv))
30745 	      break;
30746 	  cp_parser_abort_tentative_parse (parser);
30747 	  if (arg == NULL_TREE)
30748 	    error ("one of the initializer call arguments should be %<omp_priv%>"
30749 		   " or %<&omp_priv%>");
30750 	  initializer = cp_parser_postfix_expression (parser, false, false, false,
30751 						      false, NULL);
30752 	  finish_expr_stmt (initializer);
30753 	}
30754 
30755       block = finish_omp_structured_block (block);
30756       cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
30757       finish_expr_stmt (block);
30758 
30759       if (ctor)
30760 	add_decl_expr (omp_orig);
30761 
30762       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30763 	return false;
30764     }
30765 
30766   if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
30767     cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
30768 
30769   return true;
30770 }
30771 
30772 /* OpenMP 4.0
30773    #pragma omp declare reduction (reduction-id : typename-list : expression) \
30774       initializer-clause[opt] new-line
30775 
30776    initializer-clause:
30777       initializer (omp_priv initializer)
30778       initializer (function-name (argument-list))  */
30779 
30780 static void
cp_parser_omp_declare_reduction(cp_parser * parser,cp_token * pragma_tok,enum pragma_context)30781 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
30782 				 enum pragma_context)
30783 {
30784   auto_vec<tree> types;
30785   enum tree_code reduc_code = ERROR_MARK;
30786   tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
30787   unsigned int i;
30788   cp_token *first_token;
30789   cp_token_cache *cp;
30790   int errs;
30791   void *p;
30792 
30793   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
30794   p = obstack_alloc (&declarator_obstack, 0);
30795 
30796   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30797     goto fail;
30798 
30799   switch (cp_lexer_peek_token (parser->lexer)->type)
30800     {
30801     case CPP_PLUS:
30802       reduc_code = PLUS_EXPR;
30803       break;
30804     case CPP_MULT:
30805       reduc_code = MULT_EXPR;
30806       break;
30807     case CPP_MINUS:
30808       reduc_code = MINUS_EXPR;
30809       break;
30810     case CPP_AND:
30811       reduc_code = BIT_AND_EXPR;
30812       break;
30813     case CPP_XOR:
30814       reduc_code = BIT_XOR_EXPR;
30815       break;
30816     case CPP_OR:
30817       reduc_code = BIT_IOR_EXPR;
30818       break;
30819     case CPP_AND_AND:
30820       reduc_code = TRUTH_ANDIF_EXPR;
30821       break;
30822     case CPP_OR_OR:
30823       reduc_code = TRUTH_ORIF_EXPR;
30824       break;
30825     case CPP_NAME:
30826       reduc_id = orig_reduc_id = cp_parser_identifier (parser);
30827       break;
30828     default:
30829       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
30830 			       "%<|%>, %<&&%>, %<||%> or identifier");
30831       goto fail;
30832     }
30833 
30834   if (reduc_code != ERROR_MARK)
30835     cp_lexer_consume_token (parser->lexer);
30836 
30837   reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
30838   if (reduc_id == error_mark_node)
30839     goto fail;
30840 
30841   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30842     goto fail;
30843 
30844   /* Types may not be defined in declare reduction type list.  */
30845   const char *saved_message;
30846   saved_message = parser->type_definition_forbidden_message;
30847   parser->type_definition_forbidden_message
30848     = G_("types may not be defined in declare reduction type list");
30849   bool saved_colon_corrects_to_scope_p;
30850   saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30851   parser->colon_corrects_to_scope_p = false;
30852   bool saved_colon_doesnt_start_class_def_p;
30853   saved_colon_doesnt_start_class_def_p
30854     = parser->colon_doesnt_start_class_def_p;
30855   parser->colon_doesnt_start_class_def_p = true;
30856 
30857   while (true)
30858     {
30859       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30860       type = cp_parser_type_id (parser);
30861       if (type == error_mark_node)
30862 	;
30863       else if (ARITHMETIC_TYPE_P (type)
30864 	       && (orig_reduc_id == NULL_TREE
30865 		   || (TREE_CODE (type) != COMPLEX_TYPE
30866 		       && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30867 				   "min") == 0
30868 			   || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30869 				      "max") == 0))))
30870 	error_at (loc, "predeclared arithmetic type %qT in "
30871 		       "%<#pragma omp declare reduction%>", type);
30872       else if (TREE_CODE (type) == FUNCTION_TYPE
30873 	       || TREE_CODE (type) == METHOD_TYPE
30874 	       || TREE_CODE (type) == ARRAY_TYPE)
30875 	error_at (loc, "function or array type %qT in "
30876 		       "%<#pragma omp declare reduction%>", type);
30877       else if (TREE_CODE (type) == REFERENCE_TYPE)
30878 	error_at (loc, "reference type %qT in "
30879 		       "%<#pragma omp declare reduction%>", type);
30880       else if (TYPE_QUALS_NO_ADDR_SPACE (type))
30881 	error_at (loc, "const, volatile or __restrict qualified type %qT in "
30882 		       "%<#pragma omp declare reduction%>", type);
30883       else
30884 	types.safe_push (type);
30885 
30886       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30887 	cp_lexer_consume_token (parser->lexer);
30888       else
30889 	break;
30890     }
30891 
30892   /* Restore the saved message.  */
30893   parser->type_definition_forbidden_message = saved_message;
30894   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30895   parser->colon_doesnt_start_class_def_p
30896     = saved_colon_doesnt_start_class_def_p;
30897 
30898   if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
30899       || types.is_empty ())
30900     {
30901      fail:
30902       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30903       goto done;
30904     }
30905 
30906   first_token = cp_lexer_peek_token (parser->lexer);
30907   cp = NULL;
30908   errs = errorcount;
30909   FOR_EACH_VEC_ELT (types, i, type)
30910     {
30911       tree fntype
30912 	= build_function_type_list (void_type_node,
30913 				    cp_build_reference_type (type, false),
30914 				    NULL_TREE);
30915       tree this_reduc_id = reduc_id;
30916       if (!dependent_type_p (type))
30917 	this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
30918       tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
30919       DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
30920       DECL_ARTIFICIAL (fndecl) = 1;
30921       DECL_EXTERNAL (fndecl) = 1;
30922       DECL_DECLARED_INLINE_P (fndecl) = 1;
30923       DECL_IGNORED_P (fndecl) = 1;
30924       DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
30925       DECL_ATTRIBUTES (fndecl)
30926 	= tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
30927 		     DECL_ATTRIBUTES (fndecl));
30928       if (processing_template_decl)
30929 	fndecl = push_template_decl (fndecl);
30930       bool block_scope = false;
30931       tree block = NULL_TREE;
30932       if (current_function_decl)
30933 	{
30934 	  block_scope = true;
30935 	  DECL_CONTEXT (fndecl) = global_namespace;
30936 	  if (!processing_template_decl)
30937 	    pushdecl (fndecl);
30938 	}
30939       else if (current_class_type)
30940 	{
30941 	  if (cp == NULL)
30942 	    {
30943 	      while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30944 		     && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30945 		cp_lexer_consume_token (parser->lexer);
30946 	      if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30947 		goto fail;
30948 	      cp = cp_token_cache_new (first_token,
30949 				       cp_lexer_peek_nth_token (parser->lexer,
30950 								2));
30951 	    }
30952 	  DECL_STATIC_FUNCTION_P (fndecl) = 1;
30953 	  finish_member_declaration (fndecl);
30954 	  DECL_PENDING_INLINE_INFO (fndecl) = cp;
30955 	  DECL_PENDING_INLINE_P (fndecl) = 1;
30956 	  vec_safe_push (unparsed_funs_with_definitions, fndecl);
30957 	  continue;
30958 	}
30959       else
30960 	{
30961 	  DECL_CONTEXT (fndecl) = current_namespace;
30962 	  pushdecl (fndecl);
30963 	}
30964       if (!block_scope)
30965 	start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
30966       else
30967 	block = begin_omp_structured_block ();
30968       if (cp)
30969 	{
30970 	  cp_parser_push_lexer_for_tokens (parser, cp);
30971 	  parser->lexer->in_pragma = true;
30972 	}
30973       if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
30974 	{
30975 	  if (!block_scope)
30976 	    finish_function (0);
30977 	  else
30978 	    DECL_CONTEXT (fndecl) = current_function_decl;
30979 	  if (cp)
30980 	    cp_parser_pop_lexer (parser);
30981 	  goto fail;
30982 	}
30983       if (cp)
30984 	cp_parser_pop_lexer (parser);
30985       if (!block_scope)
30986 	finish_function (0);
30987       else
30988 	{
30989 	  DECL_CONTEXT (fndecl) = current_function_decl;
30990 	  block = finish_omp_structured_block (block);
30991 	  if (TREE_CODE (block) == BIND_EXPR)
30992 	    DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
30993 	  else if (TREE_CODE (block) == STATEMENT_LIST)
30994 	    DECL_SAVED_TREE (fndecl) = block;
30995 	  if (processing_template_decl)
30996 	    add_decl_expr (fndecl);
30997 	}
30998       cp_check_omp_declare_reduction (fndecl);
30999       if (cp == NULL && types.length () > 1)
31000 	cp = cp_token_cache_new (first_token,
31001 				 cp_lexer_peek_nth_token (parser->lexer, 2));
31002       if (errs != errorcount)
31003 	break;
31004     }
31005 
31006   cp_parser_require_pragma_eol (parser, pragma_tok);
31007 
31008  done:
31009   /* Free any declarators allocated.  */
31010   obstack_free (&declarator_obstack, p);
31011 }
31012 
31013 /* OpenMP 4.0
31014    #pragma omp declare simd declare-simd-clauses[optseq] new-line
31015    #pragma omp declare reduction (reduction-id : typename-list : expression) \
31016       initializer-clause[opt] new-line
31017    #pragma omp declare target new-line  */
31018 
31019 static void
cp_parser_omp_declare(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)31020 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31021 		       enum pragma_context context)
31022 {
31023   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31024     {
31025       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31026       const char *p = IDENTIFIER_POINTER (id);
31027 
31028       if (strcmp (p, "simd") == 0)
31029 	{
31030 	  cp_lexer_consume_token (parser->lexer);
31031 	  cp_parser_omp_declare_simd (parser, pragma_tok,
31032 				      context);
31033 	  return;
31034 	}
31035       cp_ensure_no_omp_declare_simd (parser);
31036       if (strcmp (p, "reduction") == 0)
31037 	{
31038 	  cp_lexer_consume_token (parser->lexer);
31039 	  cp_parser_omp_declare_reduction (parser, pragma_tok,
31040 					   context);
31041 	  return;
31042 	}
31043       if (!flag_openmp)  /* flag_openmp_simd  */
31044 	{
31045 	  cp_parser_require_pragma_eol (parser, pragma_tok);
31046 	  return;
31047 	}
31048       if (strcmp (p, "target") == 0)
31049 	{
31050 	  cp_lexer_consume_token (parser->lexer);
31051 	  cp_parser_omp_declare_target (parser, pragma_tok);
31052 	  return;
31053 	}
31054     }
31055   cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31056 			   "or %<target%>");
31057   cp_parser_require_pragma_eol (parser, pragma_tok);
31058 }
31059 
31060 /* Main entry point to OpenMP statement pragmas.  */
31061 
31062 static void
cp_parser_omp_construct(cp_parser * parser,cp_token * pragma_tok)31063 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31064 {
31065   tree stmt;
31066   char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31067   omp_clause_mask mask (0);
31068 
31069   switch (pragma_tok->pragma_kind)
31070     {
31071     case PRAGMA_OMP_ATOMIC:
31072       cp_parser_omp_atomic (parser, pragma_tok);
31073       return;
31074     case PRAGMA_OMP_CRITICAL:
31075       stmt = cp_parser_omp_critical (parser, pragma_tok);
31076       break;
31077     case PRAGMA_OMP_DISTRIBUTE:
31078       strcpy (p_name, "#pragma omp");
31079       stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31080       break;
31081     case PRAGMA_OMP_FOR:
31082       strcpy (p_name, "#pragma omp");
31083       stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31084       break;
31085     case PRAGMA_OMP_MASTER:
31086       stmt = cp_parser_omp_master (parser, pragma_tok);
31087       break;
31088     case PRAGMA_OMP_ORDERED:
31089       stmt = cp_parser_omp_ordered (parser, pragma_tok);
31090       break;
31091     case PRAGMA_OMP_PARALLEL:
31092       strcpy (p_name, "#pragma omp");
31093       stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31094       break;
31095     case PRAGMA_OMP_SECTIONS:
31096       strcpy (p_name, "#pragma omp");
31097       stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31098       break;
31099     case PRAGMA_OMP_SIMD:
31100       strcpy (p_name, "#pragma omp");
31101       stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31102       break;
31103     case PRAGMA_OMP_SINGLE:
31104       stmt = cp_parser_omp_single (parser, pragma_tok);
31105       break;
31106     case PRAGMA_OMP_TASK:
31107       stmt = cp_parser_omp_task (parser, pragma_tok);
31108       break;
31109     case PRAGMA_OMP_TASKGROUP:
31110       stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31111       break;
31112     case PRAGMA_OMP_TEAMS:
31113       strcpy (p_name, "#pragma omp");
31114       stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31115       break;
31116     default:
31117       gcc_unreachable ();
31118     }
31119 
31120   if (stmt)
31121     SET_EXPR_LOCATION (stmt, pragma_tok->location);
31122 }
31123 
31124 /* Transactional Memory parsing routines.  */
31125 
31126 /* Parse a transaction attribute.
31127 
31128    txn-attribute:
31129 	attribute
31130 	[ [ identifier ] ]
31131 
31132    ??? Simplify this when C++0x bracket attributes are
31133    implemented properly.  */
31134 
31135 static tree
cp_parser_txn_attribute_opt(cp_parser * parser)31136 cp_parser_txn_attribute_opt (cp_parser *parser)
31137 {
31138   cp_token *token;
31139   tree attr_name, attr = NULL;
31140 
31141   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31142     return cp_parser_attributes_opt (parser);
31143 
31144   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31145     return NULL_TREE;
31146   cp_lexer_consume_token (parser->lexer);
31147   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31148     goto error1;
31149 
31150   token = cp_lexer_peek_token (parser->lexer);
31151   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31152     {
31153       token = cp_lexer_consume_token (parser->lexer);
31154 
31155       attr_name = (token->type == CPP_KEYWORD
31156 		   /* For keywords, use the canonical spelling,
31157 		      not the parsed identifier.  */
31158 		   ? ridpointers[(int) token->keyword]
31159 		   : token->u.value);
31160       attr = build_tree_list (attr_name, NULL_TREE);
31161     }
31162   else
31163     cp_parser_error (parser, "expected identifier");
31164 
31165   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31166  error1:
31167   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31168   return attr;
31169 }
31170 
31171 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31172 
31173    transaction-statement:
31174      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31175        compound-statement
31176      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31177 */
31178 
31179 static tree
cp_parser_transaction(cp_parser * parser,enum rid keyword)31180 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31181 {
31182   unsigned char old_in = parser->in_transaction;
31183   unsigned char this_in = 1, new_in;
31184   cp_token *token;
31185   tree stmt, attrs, noex;
31186 
31187   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31188       || keyword == RID_TRANSACTION_RELAXED);
31189   token = cp_parser_require_keyword (parser, keyword,
31190       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31191 	  : RT_TRANSACTION_RELAXED));
31192   gcc_assert (token != NULL);
31193 
31194   if (keyword == RID_TRANSACTION_RELAXED)
31195     this_in |= TM_STMT_ATTR_RELAXED;
31196   else
31197     {
31198       attrs = cp_parser_txn_attribute_opt (parser);
31199       if (attrs)
31200 	this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31201     }
31202 
31203   /* Parse a noexcept specification.  */
31204   noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31205 
31206   /* Keep track if we're in the lexical scope of an outer transaction.  */
31207   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31208 
31209   stmt = begin_transaction_stmt (token->location, NULL, this_in);
31210 
31211   parser->in_transaction = new_in;
31212   cp_parser_compound_statement (parser, NULL, false, false);
31213   parser->in_transaction = old_in;
31214 
31215   finish_transaction_stmt (stmt, NULL, this_in, noex);
31216 
31217   return stmt;
31218 }
31219 
31220 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31221 
31222    transaction-expression:
31223      __transaction_atomic txn-noexcept-spec[opt] ( expression )
31224      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31225 */
31226 
31227 static tree
cp_parser_transaction_expression(cp_parser * parser,enum rid keyword)31228 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31229 {
31230   unsigned char old_in = parser->in_transaction;
31231   unsigned char this_in = 1;
31232   cp_token *token;
31233   tree expr, noex;
31234   bool noex_expr;
31235 
31236   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31237       || keyword == RID_TRANSACTION_RELAXED);
31238 
31239   if (!flag_tm)
31240     error (keyword == RID_TRANSACTION_RELAXED
31241 	   ? G_("%<__transaction_relaxed%> without transactional memory "
31242 		"support enabled")
31243 	   : G_("%<__transaction_atomic%> without transactional memory "
31244 		"support enabled"));
31245 
31246   token = cp_parser_require_keyword (parser, keyword,
31247       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31248 	  : RT_TRANSACTION_RELAXED));
31249   gcc_assert (token != NULL);
31250 
31251   if (keyword == RID_TRANSACTION_RELAXED)
31252     this_in |= TM_STMT_ATTR_RELAXED;
31253 
31254   /* Set this early.  This might mean that we allow transaction_cancel in
31255      an expression that we find out later actually has to be a constexpr.
31256      However, we expect that cxx_constant_value will be able to deal with
31257      this; also, if the noexcept has no constexpr, then what we parse next
31258      really is a transaction's body.  */
31259   parser->in_transaction = this_in;
31260 
31261   /* Parse a noexcept specification.  */
31262   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31263 					       true);
31264 
31265   if (!noex || !noex_expr
31266       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31267     {
31268       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31269 
31270       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
31271       expr = finish_parenthesized_expr (expr);
31272 
31273       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31274     }
31275   else
31276     {
31277       /* The only expression that is available got parsed for the noexcept
31278          already.  noexcept is true then.  */
31279       expr = noex;
31280       noex = boolean_true_node;
31281     }
31282 
31283   expr = build_transaction_expr (token->location, expr, this_in, noex);
31284   parser->in_transaction = old_in;
31285 
31286   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31287     return error_mark_node;
31288 
31289   return (flag_tm ? expr : error_mark_node);
31290 }
31291 
31292 /* Parse a function-transaction-block.
31293 
31294    function-transaction-block:
31295      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31296 	 function-body
31297      __transaction_atomic txn-attribute[opt] function-try-block
31298      __transaction_relaxed ctor-initializer[opt] function-body
31299      __transaction_relaxed function-try-block
31300 */
31301 
31302 static bool
cp_parser_function_transaction(cp_parser * parser,enum rid keyword)31303 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31304 {
31305   unsigned char old_in = parser->in_transaction;
31306   unsigned char new_in = 1;
31307   tree compound_stmt, stmt, attrs;
31308   bool ctor_initializer_p;
31309   cp_token *token;
31310 
31311   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31312       || keyword == RID_TRANSACTION_RELAXED);
31313   token = cp_parser_require_keyword (parser, keyword,
31314       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31315 	  : RT_TRANSACTION_RELAXED));
31316   gcc_assert (token != NULL);
31317 
31318   if (keyword == RID_TRANSACTION_RELAXED)
31319     new_in |= TM_STMT_ATTR_RELAXED;
31320   else
31321     {
31322       attrs = cp_parser_txn_attribute_opt (parser);
31323       if (attrs)
31324 	new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31325     }
31326 
31327   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31328 
31329   parser->in_transaction = new_in;
31330 
31331   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31332     ctor_initializer_p = cp_parser_function_try_block (parser);
31333   else
31334     ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31335       (parser, /*in_function_try_block=*/false);
31336 
31337   parser->in_transaction = old_in;
31338 
31339   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31340 
31341   return ctor_initializer_p;
31342 }
31343 
31344 /* Parse a __transaction_cancel statement.
31345 
31346    cancel-statement:
31347      __transaction_cancel txn-attribute[opt] ;
31348      __transaction_cancel txn-attribute[opt] throw-expression ;
31349 
31350    ??? Cancel and throw is not yet implemented.  */
31351 
31352 static tree
cp_parser_transaction_cancel(cp_parser * parser)31353 cp_parser_transaction_cancel (cp_parser *parser)
31354 {
31355   cp_token *token;
31356   bool is_outer = false;
31357   tree stmt, attrs;
31358 
31359   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31360 				     RT_TRANSACTION_CANCEL);
31361   gcc_assert (token != NULL);
31362 
31363   attrs = cp_parser_txn_attribute_opt (parser);
31364   if (attrs)
31365     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31366 
31367   /* ??? Parse cancel-and-throw here.  */
31368 
31369   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31370 
31371   if (!flag_tm)
31372     {
31373       error_at (token->location, "%<__transaction_cancel%> without "
31374 		"transactional memory support enabled");
31375       return error_mark_node;
31376     }
31377   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31378     {
31379       error_at (token->location, "%<__transaction_cancel%> within a "
31380 		"%<__transaction_relaxed%>");
31381       return error_mark_node;
31382     }
31383   else if (is_outer)
31384     {
31385       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31386 	  && !is_tm_may_cancel_outer (current_function_decl))
31387 	{
31388 	  error_at (token->location, "outer %<__transaction_cancel%> not "
31389 		    "within outer %<__transaction_atomic%>");
31390 	  error_at (token->location,
31391 		    "  or a %<transaction_may_cancel_outer%> function");
31392 	  return error_mark_node;
31393 	}
31394     }
31395   else if (parser->in_transaction == 0)
31396     {
31397       error_at (token->location, "%<__transaction_cancel%> not within "
31398 		"%<__transaction_atomic%>");
31399       return error_mark_node;
31400     }
31401 
31402   stmt = build_tm_abort_call (token->location, is_outer);
31403   add_stmt (stmt);
31404 
31405   return stmt;
31406 }
31407 
31408 /* The parser.  */
31409 
31410 static GTY (()) cp_parser *the_parser;
31411 
31412 
31413 /* Special handling for the first token or line in the file.  The first
31414    thing in the file might be #pragma GCC pch_preprocess, which loads a
31415    PCH file, which is a GC collection point.  So we need to handle this
31416    first pragma without benefit of an existing lexer structure.
31417 
31418    Always returns one token to the caller in *FIRST_TOKEN.  This is
31419    either the true first token of the file, or the first token after
31420    the initial pragma.  */
31421 
31422 static void
cp_parser_initial_pragma(cp_token * first_token)31423 cp_parser_initial_pragma (cp_token *first_token)
31424 {
31425   tree name = NULL;
31426 
31427   cp_lexer_get_preprocessor_token (NULL, first_token);
31428   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31429     return;
31430 
31431   cp_lexer_get_preprocessor_token (NULL, first_token);
31432   if (first_token->type == CPP_STRING)
31433     {
31434       name = first_token->u.value;
31435 
31436       cp_lexer_get_preprocessor_token (NULL, first_token);
31437       if (first_token->type != CPP_PRAGMA_EOL)
31438 	error_at (first_token->location,
31439 		  "junk at end of %<#pragma GCC pch_preprocess%>");
31440     }
31441   else
31442     error_at (first_token->location, "expected string literal");
31443 
31444   /* Skip to the end of the pragma.  */
31445   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31446     cp_lexer_get_preprocessor_token (NULL, first_token);
31447 
31448   /* Now actually load the PCH file.  */
31449   if (name)
31450     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31451 
31452   /* Read one more token to return to our caller.  We have to do this
31453      after reading the PCH file in, since its pointers have to be
31454      live.  */
31455   cp_lexer_get_preprocessor_token (NULL, first_token);
31456 }
31457 
31458 /* Normal parsing of a pragma token.  Here we can (and must) use the
31459    regular lexer.  */
31460 
31461 static bool
cp_parser_pragma(cp_parser * parser,enum pragma_context context)31462 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
31463 {
31464   cp_token *pragma_tok;
31465   unsigned int id;
31466 
31467   pragma_tok = cp_lexer_consume_token (parser->lexer);
31468   gcc_assert (pragma_tok->type == CPP_PRAGMA);
31469   parser->lexer->in_pragma = true;
31470 
31471   id = pragma_tok->pragma_kind;
31472   if (id != PRAGMA_OMP_DECLARE_REDUCTION)
31473     cp_ensure_no_omp_declare_simd (parser);
31474   switch (id)
31475     {
31476     case PRAGMA_GCC_PCH_PREPROCESS:
31477       error_at (pragma_tok->location,
31478 		"%<#pragma GCC pch_preprocess%> must be first");
31479       break;
31480 
31481     case PRAGMA_OMP_BARRIER:
31482       switch (context)
31483 	{
31484 	case pragma_compound:
31485 	  cp_parser_omp_barrier (parser, pragma_tok);
31486 	  return false;
31487 	case pragma_stmt:
31488 	  error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
31489 		    "used in compound statements");
31490 	  break;
31491 	default:
31492 	  goto bad_stmt;
31493 	}
31494       break;
31495 
31496     case PRAGMA_OMP_FLUSH:
31497       switch (context)
31498 	{
31499 	case pragma_compound:
31500 	  cp_parser_omp_flush (parser, pragma_tok);
31501 	  return false;
31502 	case pragma_stmt:
31503 	  error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
31504 		    "used in compound statements");
31505 	  break;
31506 	default:
31507 	  goto bad_stmt;
31508 	}
31509       break;
31510 
31511     case PRAGMA_OMP_TASKWAIT:
31512       switch (context)
31513 	{
31514 	case pragma_compound:
31515 	  cp_parser_omp_taskwait (parser, pragma_tok);
31516 	  return false;
31517 	case pragma_stmt:
31518 	  error_at (pragma_tok->location,
31519 		    "%<#pragma omp taskwait%> may only be "
31520 		    "used in compound statements");
31521 	  break;
31522 	default:
31523 	  goto bad_stmt;
31524 	}
31525       break;
31526 
31527     case PRAGMA_OMP_TASKYIELD:
31528       switch (context)
31529 	{
31530 	case pragma_compound:
31531 	  cp_parser_omp_taskyield (parser, pragma_tok);
31532 	  return false;
31533 	case pragma_stmt:
31534 	  error_at (pragma_tok->location,
31535 		    "%<#pragma omp taskyield%> may only be "
31536 		    "used in compound statements");
31537 	  break;
31538 	default:
31539 	  goto bad_stmt;
31540 	}
31541       break;
31542 
31543     case PRAGMA_OMP_CANCEL:
31544       switch (context)
31545 	{
31546 	case pragma_compound:
31547 	  cp_parser_omp_cancel (parser, pragma_tok);
31548 	  return false;
31549 	case pragma_stmt:
31550 	  error_at (pragma_tok->location,
31551 		    "%<#pragma omp cancel%> may only be "
31552 		    "used in compound statements");
31553 	  break;
31554 	default:
31555 	  goto bad_stmt;
31556 	}
31557       break;
31558 
31559     case PRAGMA_OMP_CANCELLATION_POINT:
31560       switch (context)
31561 	{
31562 	case pragma_compound:
31563 	  cp_parser_omp_cancellation_point (parser, pragma_tok);
31564 	  return false;
31565 	case pragma_stmt:
31566 	  error_at (pragma_tok->location,
31567 		    "%<#pragma omp cancellation point%> may only be "
31568 		    "used in compound statements");
31569 	  break;
31570 	default:
31571 	  goto bad_stmt;
31572 	}
31573       break;
31574 
31575     case PRAGMA_OMP_THREADPRIVATE:
31576       cp_parser_omp_threadprivate (parser, pragma_tok);
31577       return false;
31578 
31579     case PRAGMA_OMP_DECLARE_REDUCTION:
31580       cp_parser_omp_declare (parser, pragma_tok, context);
31581       return false;
31582 
31583     case PRAGMA_OMP_ATOMIC:
31584     case PRAGMA_OMP_CRITICAL:
31585     case PRAGMA_OMP_DISTRIBUTE:
31586     case PRAGMA_OMP_FOR:
31587     case PRAGMA_OMP_MASTER:
31588     case PRAGMA_OMP_ORDERED:
31589     case PRAGMA_OMP_PARALLEL:
31590     case PRAGMA_OMP_SECTIONS:
31591     case PRAGMA_OMP_SIMD:
31592     case PRAGMA_OMP_SINGLE:
31593     case PRAGMA_OMP_TASK:
31594     case PRAGMA_OMP_TASKGROUP:
31595     case PRAGMA_OMP_TEAMS:
31596       if (context != pragma_stmt && context != pragma_compound)
31597 	goto bad_stmt;
31598       cp_parser_omp_construct (parser, pragma_tok);
31599       return true;
31600 
31601     case PRAGMA_OMP_TARGET:
31602       return cp_parser_omp_target (parser, pragma_tok, context);
31603 
31604     case PRAGMA_OMP_END_DECLARE_TARGET:
31605       cp_parser_omp_end_declare_target (parser, pragma_tok);
31606       return false;
31607 
31608     case PRAGMA_OMP_SECTION:
31609       error_at (pragma_tok->location,
31610 		"%<#pragma omp section%> may only be used in "
31611 		"%<#pragma omp sections%> construct");
31612       break;
31613 
31614     case PRAGMA_IVDEP:
31615       {
31616 	cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31617 	cp_token *tok;
31618 	tok = cp_lexer_peek_token (the_parser->lexer);
31619 	if (tok->type != CPP_KEYWORD
31620 	    || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
31621 		&& tok->keyword != RID_DO))
31622 	  {
31623 	    cp_parser_error (parser, "for, while or do statement expected");
31624 	    return false;
31625 	  }
31626 	cp_parser_iteration_statement (parser, true);
31627 	return true;
31628       }
31629 
31630     case PRAGMA_CILK_SIMD:
31631       if (context == pragma_external)
31632 	{
31633 	  error_at (pragma_tok->location,
31634 		    "%<#pragma simd%> must be inside a function");
31635 	  break;
31636 	}
31637       cp_parser_cilk_simd (parser, pragma_tok);
31638       return true;
31639 
31640     default:
31641       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
31642       c_invoke_pragma_handler (id);
31643       break;
31644 
31645     bad_stmt:
31646       cp_parser_error (parser, "expected declaration specifiers");
31647       break;
31648     }
31649 
31650   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31651   return false;
31652 }
31653 
31654 /* The interface the pragma parsers have to the lexer.  */
31655 
31656 enum cpp_ttype
pragma_lex(tree * value)31657 pragma_lex (tree *value)
31658 {
31659   cp_token *tok;
31660   enum cpp_ttype ret;
31661 
31662   tok = cp_lexer_peek_token (the_parser->lexer);
31663 
31664   ret = tok->type;
31665   *value = tok->u.value;
31666 
31667   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
31668     ret = CPP_EOF;
31669   else if (ret == CPP_STRING)
31670     *value = cp_parser_string_literal (the_parser, false, false);
31671   else
31672     {
31673       cp_lexer_consume_token (the_parser->lexer);
31674       if (ret == CPP_KEYWORD)
31675 	ret = CPP_NAME;
31676     }
31677 
31678   return ret;
31679 }
31680 
31681 
31682 /* External interface.  */
31683 
31684 /* Parse one entire translation unit.  */
31685 
31686 void
c_parse_file(void)31687 c_parse_file (void)
31688 {
31689   static bool already_called = false;
31690 
31691   if (already_called)
31692     {
31693       sorry ("inter-module optimizations not implemented for C++");
31694       return;
31695     }
31696   already_called = true;
31697 
31698   the_parser = cp_parser_new ();
31699   push_deferring_access_checks (flag_access_control
31700 				? dk_no_deferred : dk_no_check);
31701   cp_parser_translation_unit (the_parser);
31702   the_parser = NULL;
31703 }
31704 
31705 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
31706    vectorlength clause:
31707    Syntax:
31708    vectorlength ( constant-expression )  */
31709 
31710 static tree
cp_parser_cilk_simd_vectorlength(cp_parser * parser,tree clauses,bool is_simd_fn)31711 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
31712 				  bool is_simd_fn)
31713 {
31714   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31715   tree expr;
31716   /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
31717      safelen clause.  Thus, vectorlength is represented as OMP 4.0
31718      safelen.  For SIMD-enabled function it is represented by OMP 4.0
31719      simdlen.  */
31720   if (!is_simd_fn)
31721     check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
31722 			       loc);
31723   else
31724     check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
31725 			       loc);
31726 
31727   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31728     return error_mark_node;
31729 
31730   expr = cp_parser_constant_expression (parser, false, NULL);
31731   expr = maybe_constant_value (expr);
31732 
31733   /* If expr == error_mark_node, then don't emit any errors nor
31734      create a clause.  if any of the above functions returns
31735      error mark node then they would have emitted an error message.  */
31736   if (expr == error_mark_node)
31737     ;
31738   else if (!TREE_TYPE (expr)
31739 	   || !TREE_CONSTANT (expr)
31740 	   || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
31741     error_at (loc, "vectorlength must be an integer constant");
31742   else if (TREE_CONSTANT (expr)
31743 	   && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
31744     error_at (loc, "vectorlength must be a power of 2");
31745   else
31746     {
31747       tree c;
31748       if (!is_simd_fn)
31749 	{
31750 	  c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
31751 	  OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
31752 	  OMP_CLAUSE_CHAIN (c) = clauses;
31753 	  clauses = c;
31754 	}
31755       else
31756 	{
31757 	  c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
31758 	  OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
31759 	  OMP_CLAUSE_CHAIN (c) = clauses;
31760 	  clauses = c;
31761 	}
31762     }
31763 
31764   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31765     return error_mark_node;
31766   return clauses;
31767 }
31768 
31769 /* Handles the Cilk Plus #pragma simd linear clause.
31770    Syntax:
31771    linear ( simd-linear-variable-list )
31772 
31773    simd-linear-variable-list:
31774      simd-linear-variable
31775      simd-linear-variable-list , simd-linear-variable
31776 
31777    simd-linear-variable:
31778      id-expression
31779      id-expression : simd-linear-step
31780 
31781    simd-linear-step:
31782    conditional-expression */
31783 
31784 static tree
cp_parser_cilk_simd_linear(cp_parser * parser,tree clauses)31785 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
31786 {
31787   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31788 
31789   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31790     return clauses;
31791   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31792     {
31793       cp_parser_error (parser, "expected identifier");
31794       cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31795       return error_mark_node;
31796     }
31797 
31798   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31799   parser->colon_corrects_to_scope_p = false;
31800   while (1)
31801     {
31802       cp_token *token = cp_lexer_peek_token (parser->lexer);
31803       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31804 	{
31805 	  cp_parser_error (parser, "expected variable-name");
31806 	  clauses = error_mark_node;
31807 	  break;
31808 	}
31809 
31810       tree var_name = cp_parser_id_expression (parser, false, true, NULL,
31811 					       false, false);
31812       tree decl = cp_parser_lookup_name_simple (parser, var_name,
31813 						token->location);
31814       if (decl == error_mark_node)
31815 	{
31816 	  cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
31817 				       token->location);
31818 	  clauses = error_mark_node;
31819 	}
31820       else
31821 	{
31822 	  tree e = NULL_TREE;
31823 	  tree step_size = integer_one_node;
31824 
31825 	  /* If present, parse the linear step.  Otherwise, assume the default
31826 	     value of 1.  */
31827 	  if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
31828 	    {
31829 	      cp_lexer_consume_token (parser->lexer);
31830 
31831 	      e = cp_parser_assignment_expression (parser, false, NULL);
31832 	      e = maybe_constant_value (e);
31833 
31834 	      if (e == error_mark_node)
31835 		{
31836 		  /* If an error has occurred,  then the whole pragma is
31837 		     considered ill-formed.  Thus, no reason to keep
31838 		     parsing.  */
31839 		  clauses = error_mark_node;
31840 		  break;
31841 		}
31842 	      else if (type_dependent_expression_p (e)
31843 		       || value_dependent_expression_p (e)
31844 		       || (TREE_TYPE (e)
31845 			   && INTEGRAL_TYPE_P (TREE_TYPE (e))
31846 			   && (TREE_CONSTANT (e)
31847 			       || DECL_P (e))))
31848 		step_size = e;
31849 	      else
31850 		cp_parser_error (parser,
31851 				 "step size must be an integer constant "
31852 				 "expression or an integer variable");
31853 	    }
31854 
31855 	  /* Use the OMP_CLAUSE_LINEAR,  which has the same semantics.  */
31856 	  tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
31857 	  OMP_CLAUSE_DECL (l) = decl;
31858 	  OMP_CLAUSE_LINEAR_STEP (l) = step_size;
31859 	  OMP_CLAUSE_CHAIN (l) = clauses;
31860 	  clauses = l;
31861 	}
31862       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31863 	cp_lexer_consume_token (parser->lexer);
31864       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31865 	break;
31866       else
31867 	{
31868 	  error_at (cp_lexer_peek_token (parser->lexer)->location,
31869 		    "expected %<,%> or %<)%> after %qE", decl);
31870 	  clauses = error_mark_node;
31871 	  break;
31872 	}
31873     }
31874   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31875   cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31876   return clauses;
31877 }
31878 
31879 /* Returns the name of the next clause.  If the clause is not
31880    recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
31881    token is not consumed.  Otherwise, the appropriate enum from the
31882    pragma_simd_clause is returned and the token is consumed.  */
31883 
31884 static pragma_omp_clause
cp_parser_cilk_simd_clause_name(cp_parser * parser)31885 cp_parser_cilk_simd_clause_name (cp_parser *parser)
31886 {
31887   pragma_omp_clause clause_type;
31888   cp_token *token = cp_lexer_peek_token (parser->lexer);
31889 
31890   if (token->keyword == RID_PRIVATE)
31891     clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
31892   else if (!token->u.value || token->type != CPP_NAME)
31893     return PRAGMA_CILK_CLAUSE_NONE;
31894   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
31895     clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
31896   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
31897     clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
31898   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
31899     clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
31900   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
31901     clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
31902   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
31903     clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
31904   else
31905     return PRAGMA_CILK_CLAUSE_NONE;
31906 
31907   cp_lexer_consume_token (parser->lexer);
31908   return clause_type;
31909 }
31910 
31911 /* Parses all the #pragma simd clauses.  Returns a list of clauses found.  */
31912 
31913 static tree
cp_parser_cilk_simd_all_clauses(cp_parser * parser,cp_token * pragma_token)31914 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
31915 {
31916   tree clauses = NULL_TREE;
31917 
31918   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31919 	 && clauses != error_mark_node)
31920     {
31921       pragma_omp_clause c_kind;
31922       c_kind = cp_parser_cilk_simd_clause_name (parser);
31923       if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
31924 	clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
31925       else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
31926 	clauses = cp_parser_cilk_simd_linear (parser, clauses);
31927       else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
31928 	/* Use the OpenMP 4.0 equivalent function.  */
31929 	clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
31930       else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
31931 	/* Use the OpenMP 4.0 equivalent function.  */
31932 	clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
31933 					  clauses);
31934       else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
31935 	/* Use the OMP 4.0 equivalent function.  */
31936 	clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
31937 					  clauses);
31938       else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
31939 	/* Use the OMP 4.0 equivalent function.  */
31940 	clauses = cp_parser_omp_clause_reduction (parser, clauses);
31941       else
31942 	{
31943 	  clauses = error_mark_node;
31944 	  cp_parser_error (parser, "expected %<#pragma simd%> clause");
31945 	  break;
31946 	}
31947     }
31948 
31949   cp_parser_skip_to_pragma_eol (parser, pragma_token);
31950 
31951   if (clauses == error_mark_node)
31952     return error_mark_node;
31953   else
31954     return c_finish_cilk_clauses (clauses);
31955 }
31956 
31957 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops.  */
31958 
31959 static void
cp_parser_cilk_simd(cp_parser * parser,cp_token * pragma_token)31960 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
31961 {
31962   tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
31963 
31964   if (clauses == error_mark_node)
31965     return;
31966 
31967   if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
31968     {
31969       error_at (cp_lexer_peek_token (parser->lexer)->location,
31970 		"for statement expected");
31971       return;
31972     }
31973 
31974   tree sb = begin_omp_structured_block ();
31975   int save = cp_parser_begin_omp_structured_block (parser);
31976   tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
31977   if (ret)
31978     cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
31979   cp_parser_end_omp_structured_block (parser, save);
31980   add_stmt (finish_omp_structured_block (sb));
31981   return;
31982 }
31983 
31984 /* Create an identifier for a generic parameter type (a synthesized
31985    template parameter implied by `auto' or a concept identifier). */
31986 
31987 static GTY(()) int generic_parm_count;
31988 static tree
make_generic_type_name()31989 make_generic_type_name ()
31990 {
31991   char buf[32];
31992   sprintf (buf, "auto:%d", ++generic_parm_count);
31993   return get_identifier (buf);
31994 }
31995 
31996 /* Predicate that behaves as is_auto_or_concept but matches the parent
31997    node of the generic type rather than the generic type itself.  This
31998    allows for type transformation in add_implicit_template_parms.  */
31999 
32000 static inline bool
tree_type_is_auto_or_concept(const_tree t)32001 tree_type_is_auto_or_concept (const_tree t)
32002 {
32003   return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
32004 }
32005 
32006 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32007    (creating a new template parameter list if necessary).  Returns the newly
32008    created template type parm.  */
32009 
32010 tree
synthesize_implicit_template_parm(cp_parser * parser)32011 synthesize_implicit_template_parm  (cp_parser *parser)
32012 {
32013   gcc_assert (current_binding_level->kind == sk_function_parms);
32014 
32015   /* We are either continuing a function template that already contains implicit
32016      template parameters, creating a new fully-implicit function template, or
32017      extending an existing explicit function template with implicit template
32018      parameters.  */
32019 
32020   cp_binding_level *const entry_scope = current_binding_level;
32021 
32022   bool become_template = false;
32023   cp_binding_level *parent_scope = 0;
32024 
32025   if (parser->implicit_template_scope)
32026     {
32027       gcc_assert (parser->implicit_template_parms);
32028 
32029       current_binding_level = parser->implicit_template_scope;
32030     }
32031   else
32032     {
32033       /* Roll back to the existing template parameter scope (in the case of
32034 	 extending an explicit function template) or introduce a new template
32035 	 parameter scope ahead of the function parameter scope (or class scope
32036 	 in the case of out-of-line member definitions).  The function scope is
32037 	 added back after template parameter synthesis below.  */
32038 
32039       cp_binding_level *scope = entry_scope;
32040 
32041       while (scope->kind == sk_function_parms)
32042 	{
32043 	  parent_scope = scope;
32044 	  scope = scope->level_chain;
32045 	}
32046       if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
32047 	{
32048 	  /* If not defining a class, then any class scope is a scope level in
32049 	     an out-of-line member definition.  In this case simply wind back
32050 	     beyond the first such scope to inject the template parameter list.
32051 	     Otherwise wind back to the class being defined.  The latter can
32052 	     occur in class member friend declarations such as:
32053 
32054 	       class A {
32055 		 void foo (auto);
32056 	       };
32057 	       class B {
32058 		 friend void A::foo (auto);
32059 	       };
32060 
32061 	    The template parameter list synthesized for the friend declaration
32062 	    must be injected in the scope of 'B'.  This can also occur in
32063 	    erroneous cases such as:
32064 
32065 	       struct A {
32066 	         struct B {
32067 		   void foo (auto);
32068 		 };
32069 		 void B::foo (auto) {}
32070 	       };
32071 
32072 	    Here the attempted definition of 'B::foo' within 'A' is ill-formed
32073 	    but, nevertheless, the template parameter list synthesized for the
32074 	    declarator should be injected into the scope of 'A' as if the
32075 	    ill-formed template was specified explicitly.  */
32076 
32077 	  while (scope->kind == sk_class && !scope->defining_class_p)
32078 	    {
32079 	      parent_scope = scope;
32080 	      scope = scope->level_chain;
32081 	    }
32082 	}
32083 
32084       current_binding_level = scope;
32085 
32086       if (scope->kind != sk_template_parms
32087 	  || !function_being_declared_is_template_p (parser))
32088 	{
32089 	  /* Introduce a new template parameter list for implicit template
32090 	     parameters.  */
32091 
32092 	  become_template = true;
32093 
32094 	  parser->implicit_template_scope
32095 	      = begin_scope (sk_template_parms, NULL);
32096 
32097 	  ++processing_template_decl;
32098 
32099 	  parser->fully_implicit_function_template_p = true;
32100 	  ++parser->num_template_parameter_lists;
32101 	}
32102       else
32103 	{
32104 	  /* Synthesize implicit template parameters at the end of the explicit
32105 	     template parameter list.  */
32106 
32107 	  gcc_assert (current_template_parms);
32108 
32109 	  parser->implicit_template_scope = scope;
32110 
32111 	  tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32112 	  parser->implicit_template_parms
32113 	    = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32114 	}
32115     }
32116 
32117   /* Synthesize a new template parameter and track the current template
32118      parameter chain with implicit_template_parms.  */
32119 
32120   tree synth_id = make_generic_type_name ();
32121   tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32122 						    synth_id);
32123   tree new_parm
32124     = process_template_parm (parser->implicit_template_parms,
32125 			     input_location,
32126 			     build_tree_list (NULL_TREE, synth_tmpl_parm),
32127 			     /*non_type=*/false,
32128 			     /*param_pack=*/false);
32129 
32130 
32131   if (parser->implicit_template_parms)
32132     parser->implicit_template_parms
32133       = TREE_CHAIN (parser->implicit_template_parms);
32134   else
32135     parser->implicit_template_parms = new_parm;
32136 
32137   tree new_type = TREE_TYPE (getdecls ());
32138 
32139   /* If creating a fully implicit function template, start the new implicit
32140      template parameter list with this synthesized type, otherwise grow the
32141      current template parameter list.  */
32142 
32143   if (become_template)
32144     {
32145       parent_scope->level_chain = current_binding_level;
32146 
32147       tree new_parms = make_tree_vec (1);
32148       TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32149       current_template_parms = tree_cons (size_int (processing_template_decl),
32150 					  new_parms, current_template_parms);
32151     }
32152   else
32153     {
32154       tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32155       int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32156       new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
32157       TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32158     }
32159 
32160   current_binding_level = entry_scope;
32161 
32162   return new_type;
32163 }
32164 
32165 /* Finish the declaration of a fully implicit function template.  Such a
32166    template has no explicit template parameter list so has not been through the
32167    normal template head and tail processing.  synthesize_implicit_template_parm
32168    tries to do the head; this tries to do the tail.  MEMBER_DECL_OPT should be
32169    provided if the declaration is a class member such that its template
32170    declaration can be completed.  If MEMBER_DECL_OPT is provided the finished
32171    form is returned.  Otherwise NULL_TREE is returned. */
32172 
32173 tree
finish_fully_implicit_template(cp_parser * parser,tree member_decl_opt)32174 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32175 {
32176   gcc_assert (parser->fully_implicit_function_template_p);
32177 
32178   if (member_decl_opt && member_decl_opt != error_mark_node
32179       && DECL_VIRTUAL_P (member_decl_opt))
32180     {
32181       error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32182 		"implicit templates may not be %<virtual%>");
32183       DECL_VIRTUAL_P (member_decl_opt) = false;
32184     }
32185 
32186   if (member_decl_opt)
32187     member_decl_opt = finish_member_template_decl (member_decl_opt);
32188   end_template_decl ();
32189 
32190   parser->fully_implicit_function_template_p = false;
32191   --parser->num_template_parameter_lists;
32192 
32193   return member_decl_opt;
32194 }
32195 
32196 #include "gt-cp-parser.h"
32197