]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
a87675eb1b75c01a96bc8638ceb4f164132183c4
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "cp-tree.h"
25 #include "c-family/c-common.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "cgraph.h"
29 #include "print-tree.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "intl.h"
33 #include "decl.h"
34 #include "c-family/c-objc.h"
35 #include "plugin.h"
36 #include "tree-pretty-print.h"
37 #include "parser.h"
38 #include "omp-low.h"
39 #include "gomp-constants.h"
40 #include "c-family/c-indentation.h"
41 #include "context.h"
42
43 \f
44 /* The lexer. */
45
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
48
49 static cp_token eof_token =
50 {
51 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
52 };
53
54 /* The various kinds of non integral constant we encounter. */
55 enum non_integral_constant {
56 NIC_NONE,
57 /* floating-point literal */
58 NIC_FLOAT,
59 /* %<this%> */
60 NIC_THIS,
61 /* %<__FUNCTION__%> */
62 NIC_FUNC_NAME,
63 /* %<__PRETTY_FUNCTION__%> */
64 NIC_PRETTY_FUNC,
65 /* %<__func__%> */
66 NIC_C99_FUNC,
67 /* "%<va_arg%> */
68 NIC_VA_ARG,
69 /* a cast */
70 NIC_CAST,
71 /* %<typeid%> operator */
72 NIC_TYPEID,
73 /* non-constant compound literals */
74 NIC_NCC,
75 /* a function call */
76 NIC_FUNC_CALL,
77 /* an increment */
78 NIC_INC,
79 /* an decrement */
80 NIC_DEC,
81 /* an array reference */
82 NIC_ARRAY_REF,
83 /* %<->%> */
84 NIC_ARROW,
85 /* %<.%> */
86 NIC_POINT,
87 /* the address of a label */
88 NIC_ADDR_LABEL,
89 /* %<*%> */
90 NIC_STAR,
91 /* %<&%> */
92 NIC_ADDR,
93 /* %<++%> */
94 NIC_PREINCREMENT,
95 /* %<--%> */
96 NIC_PREDECREMENT,
97 /* %<new%> */
98 NIC_NEW,
99 /* %<delete%> */
100 NIC_DEL,
101 /* calls to overloaded operators */
102 NIC_OVERLOADED,
103 /* an assignment */
104 NIC_ASSIGNMENT,
105 /* a comma operator */
106 NIC_COMMA,
107 /* a call to a constructor */
108 NIC_CONSTRUCTOR,
109 /* a transaction expression */
110 NIC_TRANSACTION
111 };
112
113 /* The various kinds of errors about name-lookup failing. */
114 enum name_lookup_error {
115 /* NULL */
116 NLE_NULL,
117 /* is not a type */
118 NLE_TYPE,
119 /* is not a class or namespace */
120 NLE_CXX98,
121 /* is not a class, namespace, or enumeration */
122 NLE_NOT_CXX98
123 };
124
125 /* The various kinds of required token */
126 enum required_token {
127 RT_NONE,
128 RT_SEMICOLON, /* ';' */
129 RT_OPEN_PAREN, /* '(' */
130 RT_CLOSE_BRACE, /* '}' */
131 RT_OPEN_BRACE, /* '{' */
132 RT_CLOSE_SQUARE, /* ']' */
133 RT_OPEN_SQUARE, /* '[' */
134 RT_COMMA, /* ',' */
135 RT_SCOPE, /* '::' */
136 RT_LESS, /* '<' */
137 RT_GREATER, /* '>' */
138 RT_EQ, /* '=' */
139 RT_ELLIPSIS, /* '...' */
140 RT_MULT, /* '*' */
141 RT_COMPL, /* '~' */
142 RT_COLON, /* ':' */
143 RT_COLON_SCOPE, /* ':' or '::' */
144 RT_CLOSE_PAREN, /* ')' */
145 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
146 RT_PRAGMA_EOL, /* end of line */
147 RT_NAME, /* identifier */
148
149 /* The type is CPP_KEYWORD */
150 RT_NEW, /* new */
151 RT_DELETE, /* delete */
152 RT_RETURN, /* return */
153 RT_WHILE, /* while */
154 RT_EXTERN, /* extern */
155 RT_STATIC_ASSERT, /* static_assert */
156 RT_DECLTYPE, /* decltype */
157 RT_OPERATOR, /* operator */
158 RT_CLASS, /* class */
159 RT_TEMPLATE, /* template */
160 RT_NAMESPACE, /* namespace */
161 RT_USING, /* using */
162 RT_ASM, /* asm */
163 RT_TRY, /* try */
164 RT_CATCH, /* catch */
165 RT_THROW, /* throw */
166 RT_LABEL, /* __label__ */
167 RT_AT_TRY, /* @try */
168 RT_AT_SYNCHRONIZED, /* @synchronized */
169 RT_AT_THROW, /* @throw */
170
171 RT_SELECT, /* selection-statement */
172 RT_INTERATION, /* iteration-statement */
173 RT_JUMP, /* jump-statement */
174 RT_CLASS_KEY, /* class-key */
175 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
176 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
177 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
178 RT_TRANSACTION_CANCEL /* __transaction_cancel */
179 };
180
181 /* Prototypes. */
182
183 static cp_lexer *cp_lexer_new_main
184 (void);
185 static cp_lexer *cp_lexer_new_from_tokens
186 (cp_token_cache *tokens);
187 static void cp_lexer_destroy
188 (cp_lexer *);
189 static int cp_lexer_saving_tokens
190 (const cp_lexer *);
191 static cp_token *cp_lexer_token_at
192 (cp_lexer *, cp_token_position);
193 static void cp_lexer_get_preprocessor_token
194 (cp_lexer *, cp_token *);
195 static inline cp_token *cp_lexer_peek_token
196 (cp_lexer *);
197 static cp_token *cp_lexer_peek_nth_token
198 (cp_lexer *, size_t);
199 static inline bool cp_lexer_next_token_is
200 (cp_lexer *, enum cpp_ttype);
201 static bool cp_lexer_next_token_is_not
202 (cp_lexer *, enum cpp_ttype);
203 static bool cp_lexer_next_token_is_keyword
204 (cp_lexer *, enum rid);
205 static cp_token *cp_lexer_consume_token
206 (cp_lexer *);
207 static void cp_lexer_purge_token
208 (cp_lexer *);
209 static void cp_lexer_purge_tokens_after
210 (cp_lexer *, cp_token_position);
211 static void cp_lexer_save_tokens
212 (cp_lexer *);
213 static void cp_lexer_commit_tokens
214 (cp_lexer *);
215 static void cp_lexer_rollback_tokens
216 (cp_lexer *);
217 static void cp_lexer_print_token
218 (FILE *, cp_token *);
219 static inline bool cp_lexer_debugging_p
220 (cp_lexer *);
221 static void cp_lexer_start_debugging
222 (cp_lexer *) ATTRIBUTE_UNUSED;
223 static void cp_lexer_stop_debugging
224 (cp_lexer *) ATTRIBUTE_UNUSED;
225
226 static cp_token_cache *cp_token_cache_new
227 (cp_token *, cp_token *);
228
229 static void cp_parser_initial_pragma
230 (cp_token *);
231
232 static tree cp_literal_operator_id
233 (const char *);
234
235 static void cp_parser_cilk_simd
236 (cp_parser *, cp_token *);
237 static tree cp_parser_cilk_for
238 (cp_parser *, tree);
239 static bool cp_parser_omp_declare_reduction_exprs
240 (tree, cp_parser *);
241 static tree cp_parser_cilk_simd_vectorlength
242 (cp_parser *, tree, bool);
243 static void cp_finalize_oacc_routine
244 (cp_parser *, tree, bool, bool);
245
246 /* Manifest constants. */
247 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
248 #define CP_SAVED_TOKEN_STACK 5
249
250 /* Variables. */
251
252 /* The stream to which debugging output should be written. */
253 static FILE *cp_lexer_debug_stream;
254
255 /* Nonzero if we are parsing an unevaluated operand: an operand to
256 sizeof, typeof, or alignof. */
257 int cp_unevaluated_operand;
258
259 /* Dump up to NUM tokens in BUFFER to FILE starting with token
260 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
261 first token in BUFFER. If NUM is 0, dump all the tokens. If
262 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
263 highlighted by surrounding it in [[ ]]. */
264
265 static void
266 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
267 cp_token *start_token, unsigned num,
268 cp_token *curr_token)
269 {
270 unsigned i, nprinted;
271 cp_token *token;
272 bool do_print;
273
274 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
275
276 if (buffer == NULL)
277 return;
278
279 if (num == 0)
280 num = buffer->length ();
281
282 if (start_token == NULL)
283 start_token = buffer->address ();
284
285 if (start_token > buffer->address ())
286 {
287 cp_lexer_print_token (file, &(*buffer)[0]);
288 fprintf (file, " ... ");
289 }
290
291 do_print = false;
292 nprinted = 0;
293 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
294 {
295 if (token == start_token)
296 do_print = true;
297
298 if (!do_print)
299 continue;
300
301 nprinted++;
302 if (token == curr_token)
303 fprintf (file, "[[");
304
305 cp_lexer_print_token (file, token);
306
307 if (token == curr_token)
308 fprintf (file, "]]");
309
310 switch (token->type)
311 {
312 case CPP_SEMICOLON:
313 case CPP_OPEN_BRACE:
314 case CPP_CLOSE_BRACE:
315 case CPP_EOF:
316 fputc ('\n', file);
317 break;
318
319 default:
320 fputc (' ', file);
321 }
322 }
323
324 if (i == num && i < buffer->length ())
325 {
326 fprintf (file, " ... ");
327 cp_lexer_print_token (file, &buffer->last ());
328 }
329
330 fprintf (file, "\n");
331 }
332
333
334 /* Dump all tokens in BUFFER to stderr. */
335
336 void
337 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
338 {
339 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
340 }
341
342 DEBUG_FUNCTION void
343 debug (vec<cp_token, va_gc> &ref)
344 {
345 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
346 }
347
348 DEBUG_FUNCTION void
349 debug (vec<cp_token, va_gc> *ptr)
350 {
351 if (ptr)
352 debug (*ptr);
353 else
354 fprintf (stderr, "<nil>\n");
355 }
356
357
358 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
359 description for T. */
360
361 static void
362 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
363 {
364 if (t)
365 {
366 fprintf (file, "%s: ", desc);
367 print_node_brief (file, "", t, 0);
368 }
369 }
370
371
372 /* Dump parser context C to FILE. */
373
374 static void
375 cp_debug_print_context (FILE *file, cp_parser_context *c)
376 {
377 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
378 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
379 print_node_brief (file, "", c->object_type, 0);
380 fprintf (file, "}\n");
381 }
382
383
384 /* Print the stack of parsing contexts to FILE starting with FIRST. */
385
386 static void
387 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
388 {
389 unsigned i;
390 cp_parser_context *c;
391
392 fprintf (file, "Parsing context stack:\n");
393 for (i = 0, c = first; c; c = c->next, i++)
394 {
395 fprintf (file, "\t#%u: ", i);
396 cp_debug_print_context (file, c);
397 }
398 }
399
400
401 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
402
403 static void
404 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
405 {
406 if (flag)
407 fprintf (file, "%s: true\n", desc);
408 }
409
410
411 /* Print an unparsed function entry UF to FILE. */
412
413 static void
414 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
415 {
416 unsigned i;
417 cp_default_arg_entry *default_arg_fn;
418 tree fn;
419
420 fprintf (file, "\tFunctions with default args:\n");
421 for (i = 0;
422 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
423 i++)
424 {
425 fprintf (file, "\t\tClass type: ");
426 print_node_brief (file, "", default_arg_fn->class_type, 0);
427 fprintf (file, "\t\tDeclaration: ");
428 print_node_brief (file, "", default_arg_fn->decl, 0);
429 fprintf (file, "\n");
430 }
431
432 fprintf (file, "\n\tFunctions with definitions that require "
433 "post-processing\n\t\t");
434 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
435 {
436 print_node_brief (file, "", fn, 0);
437 fprintf (file, " ");
438 }
439 fprintf (file, "\n");
440
441 fprintf (file, "\n\tNon-static data members with initializers that require "
442 "post-processing\n\t\t");
443 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
444 {
445 print_node_brief (file, "", fn, 0);
446 fprintf (file, " ");
447 }
448 fprintf (file, "\n");
449 }
450
451
452 /* Print the stack of unparsed member functions S to FILE. */
453
454 static void
455 cp_debug_print_unparsed_queues (FILE *file,
456 vec<cp_unparsed_functions_entry, va_gc> *s)
457 {
458 unsigned i;
459 cp_unparsed_functions_entry *uf;
460
461 fprintf (file, "Unparsed functions\n");
462 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
463 {
464 fprintf (file, "#%u:\n", i);
465 cp_debug_print_unparsed_function (file, uf);
466 }
467 }
468
469
470 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
471 the given PARSER. If FILE is NULL, the output is printed on stderr. */
472
473 static void
474 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
475 {
476 cp_token *next_token, *first_token, *start_token;
477
478 if (file == NULL)
479 file = stderr;
480
481 next_token = parser->lexer->next_token;
482 first_token = parser->lexer->buffer->address ();
483 start_token = (next_token > first_token + window_size / 2)
484 ? next_token - window_size / 2
485 : first_token;
486 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
487 next_token);
488 }
489
490
491 /* Dump debugging information for the given PARSER. If FILE is NULL,
492 the output is printed on stderr. */
493
494 void
495 cp_debug_parser (FILE *file, cp_parser *parser)
496 {
497 const size_t window_size = 20;
498 cp_token *token;
499 expanded_location eloc;
500
501 if (file == NULL)
502 file = stderr;
503
504 fprintf (file, "Parser state\n\n");
505 fprintf (file, "Number of tokens: %u\n",
506 vec_safe_length (parser->lexer->buffer));
507 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
508 cp_debug_print_tree_if_set (file, "Object scope",
509 parser->object_scope);
510 cp_debug_print_tree_if_set (file, "Qualifying scope",
511 parser->qualifying_scope);
512 cp_debug_print_context_stack (file, parser->context);
513 cp_debug_print_flag (file, "Allow GNU extensions",
514 parser->allow_gnu_extensions_p);
515 cp_debug_print_flag (file, "'>' token is greater-than",
516 parser->greater_than_is_operator_p);
517 cp_debug_print_flag (file, "Default args allowed in current "
518 "parameter list", parser->default_arg_ok_p);
519 cp_debug_print_flag (file, "Parsing integral constant-expression",
520 parser->integral_constant_expression_p);
521 cp_debug_print_flag (file, "Allow non-constant expression in current "
522 "constant-expression",
523 parser->allow_non_integral_constant_expression_p);
524 cp_debug_print_flag (file, "Seen non-constant expression",
525 parser->non_integral_constant_expression_p);
526 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
527 "current context",
528 parser->local_variables_forbidden_p);
529 cp_debug_print_flag (file, "In unbraced linkage specification",
530 parser->in_unbraced_linkage_specification_p);
531 cp_debug_print_flag (file, "Parsing a declarator",
532 parser->in_declarator_p);
533 cp_debug_print_flag (file, "In template argument list",
534 parser->in_template_argument_list_p);
535 cp_debug_print_flag (file, "Parsing an iteration statement",
536 parser->in_statement & IN_ITERATION_STMT);
537 cp_debug_print_flag (file, "Parsing a switch statement",
538 parser->in_statement & IN_SWITCH_STMT);
539 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
540 parser->in_statement & IN_OMP_BLOCK);
541 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
542 parser->in_statement & IN_CILK_SIMD_FOR);
543 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
544 parser->in_statement & IN_OMP_FOR);
545 cp_debug_print_flag (file, "Parsing an if statement",
546 parser->in_statement & IN_IF_STMT);
547 cp_debug_print_flag (file, "Parsing a type-id in an expression "
548 "context", parser->in_type_id_in_expr_p);
549 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
550 parser->implicit_extern_c);
551 cp_debug_print_flag (file, "String expressions should be translated "
552 "to execution character set",
553 parser->translate_strings_p);
554 cp_debug_print_flag (file, "Parsing function body outside of a "
555 "local class", parser->in_function_body);
556 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
557 parser->colon_corrects_to_scope_p);
558 cp_debug_print_flag (file, "Colon doesn't start a class definition",
559 parser->colon_doesnt_start_class_def_p);
560 if (parser->type_definition_forbidden_message)
561 fprintf (file, "Error message for forbidden type definitions: %s\n",
562 parser->type_definition_forbidden_message);
563 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
564 fprintf (file, "Number of class definitions in progress: %u\n",
565 parser->num_classes_being_defined);
566 fprintf (file, "Number of template parameter lists for the current "
567 "declaration: %u\n", parser->num_template_parameter_lists);
568 cp_debug_parser_tokens (file, parser, window_size);
569 token = parser->lexer->next_token;
570 fprintf (file, "Next token to parse:\n");
571 fprintf (file, "\tToken: ");
572 cp_lexer_print_token (file, token);
573 eloc = expand_location (token->location);
574 fprintf (file, "\n\tFile: %s\n", eloc.file);
575 fprintf (file, "\tLine: %d\n", eloc.line);
576 fprintf (file, "\tColumn: %d\n", eloc.column);
577 }
578
579 DEBUG_FUNCTION void
580 debug (cp_parser &ref)
581 {
582 cp_debug_parser (stderr, &ref);
583 }
584
585 DEBUG_FUNCTION void
586 debug (cp_parser *ptr)
587 {
588 if (ptr)
589 debug (*ptr);
590 else
591 fprintf (stderr, "<nil>\n");
592 }
593
594 /* Allocate memory for a new lexer object and return it. */
595
596 static cp_lexer *
597 cp_lexer_alloc (void)
598 {
599 cp_lexer *lexer;
600
601 c_common_no_more_pch ();
602
603 /* Allocate the memory. */
604 lexer = ggc_cleared_alloc<cp_lexer> ();
605
606 /* Initially we are not debugging. */
607 lexer->debugging_p = false;
608
609 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
610
611 /* Create the buffer. */
612 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
613
614 return lexer;
615 }
616
617
618 /* Create a new main C++ lexer, the lexer that gets tokens from the
619 preprocessor. */
620
621 static cp_lexer *
622 cp_lexer_new_main (void)
623 {
624 cp_lexer *lexer;
625 cp_token token;
626
627 /* It's possible that parsing the first pragma will load a PCH file,
628 which is a GC collection point. So we have to do that before
629 allocating any memory. */
630 cp_parser_initial_pragma (&token);
631
632 lexer = cp_lexer_alloc ();
633
634 /* Put the first token in the buffer. */
635 lexer->buffer->quick_push (token);
636
637 /* Get the remaining tokens from the preprocessor. */
638 while (token.type != CPP_EOF)
639 {
640 cp_lexer_get_preprocessor_token (lexer, &token);
641 vec_safe_push (lexer->buffer, token);
642 }
643
644 lexer->last_token = lexer->buffer->address ()
645 + lexer->buffer->length ()
646 - 1;
647 lexer->next_token = lexer->buffer->length ()
648 ? lexer->buffer->address ()
649 : &eof_token;
650
651 /* Subsequent preprocessor diagnostics should use compiler
652 diagnostic functions to get the compiler source location. */
653 done_lexing = true;
654
655 gcc_assert (!lexer->next_token->purged_p);
656 return lexer;
657 }
658
659 /* Create a new lexer whose token stream is primed with the tokens in
660 CACHE. When these tokens are exhausted, no new tokens will be read. */
661
662 static cp_lexer *
663 cp_lexer_new_from_tokens (cp_token_cache *cache)
664 {
665 cp_token *first = cache->first;
666 cp_token *last = cache->last;
667 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
668
669 /* We do not own the buffer. */
670 lexer->buffer = NULL;
671 lexer->next_token = first == last ? &eof_token : first;
672 lexer->last_token = last;
673
674 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
675
676 /* Initially we are not debugging. */
677 lexer->debugging_p = false;
678
679 gcc_assert (!lexer->next_token->purged_p);
680 return lexer;
681 }
682
683 /* Frees all resources associated with LEXER. */
684
685 static void
686 cp_lexer_destroy (cp_lexer *lexer)
687 {
688 vec_free (lexer->buffer);
689 lexer->saved_tokens.release ();
690 ggc_free (lexer);
691 }
692
693 /* Returns nonzero if debugging information should be output. */
694
695 static inline bool
696 cp_lexer_debugging_p (cp_lexer *lexer)
697 {
698 return lexer->debugging_p;
699 }
700
701
702 static inline cp_token_position
703 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
704 {
705 gcc_assert (!previous_p || lexer->next_token != &eof_token);
706
707 return lexer->next_token - previous_p;
708 }
709
710 static inline cp_token *
711 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
712 {
713 return pos;
714 }
715
716 static inline void
717 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
718 {
719 lexer->next_token = cp_lexer_token_at (lexer, pos);
720 }
721
722 static inline cp_token_position
723 cp_lexer_previous_token_position (cp_lexer *lexer)
724 {
725 if (lexer->next_token == &eof_token)
726 return lexer->last_token - 1;
727 else
728 return cp_lexer_token_position (lexer, true);
729 }
730
731 static inline cp_token *
732 cp_lexer_previous_token (cp_lexer *lexer)
733 {
734 cp_token_position tp = cp_lexer_previous_token_position (lexer);
735
736 return cp_lexer_token_at (lexer, tp);
737 }
738
739 /* nonzero if we are presently saving tokens. */
740
741 static inline int
742 cp_lexer_saving_tokens (const cp_lexer* lexer)
743 {
744 return lexer->saved_tokens.length () != 0;
745 }
746
747 /* Store the next token from the preprocessor in *TOKEN. Return true
748 if we reach EOF. If LEXER is NULL, assume we are handling an
749 initial #pragma pch_preprocess, and thus want the lexer to return
750 processed strings. */
751
752 static void
753 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
754 {
755 static int is_extern_c = 0;
756
757 /* Get a new token from the preprocessor. */
758 token->type
759 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
760 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
761 token->keyword = RID_MAX;
762 token->pragma_kind = PRAGMA_NONE;
763 token->purged_p = false;
764 token->error_reported = 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_cxx11_compat
786 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
787 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
788 {
789 /* Warn about the C++0x keyword (but still treat it as
790 an identifier). */
791 warning (OPT_Wc__11_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->keyword = RID_MAX;
801 }
802 }
803 else if (token->type == CPP_AT_NAME)
804 {
805 /* This only happens in Objective-C++; it must be a keyword. */
806 token->type = CPP_KEYWORD;
807 switch (C_RID_CODE (token->u.value))
808 {
809 /* Replace 'class' with '@class', 'private' with '@private',
810 etc. This prevents confusion with the C++ keyword
811 'class', and makes the tokens consistent with other
812 Objective-C 'AT' keywords. For example '@class' is
813 reported as RID_AT_CLASS which is consistent with
814 '@synchronized', which is reported as
815 RID_AT_SYNCHRONIZED.
816 */
817 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
818 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
819 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
820 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
821 case RID_THROW: token->keyword = RID_AT_THROW; break;
822 case RID_TRY: token->keyword = RID_AT_TRY; break;
823 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
824 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; 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
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
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 *
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
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
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
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
895 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
896 {
897 return cp_lexer_peek_nth_token (lexer, n)->type == type;
898 }
899
900 static inline bool
901 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
902 {
903 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
904 }
905
906 /* Return true if the next token is not the indicated KEYWORD. */
907
908 static inline bool
909 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
910 {
911 return cp_lexer_peek_token (lexer)->keyword != keyword;
912 }
913
914 /* Return true if the next token is a keyword for a decl-specifier. */
915
916 static bool
917 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
918 {
919 cp_token *token;
920
921 token = cp_lexer_peek_token (lexer);
922 switch (token->keyword)
923 {
924 /* auto specifier: storage-class-specifier in C++,
925 simple-type-specifier in C++0x. */
926 case RID_AUTO:
927 /* Storage classes. */
928 case RID_REGISTER:
929 case RID_STATIC:
930 case RID_EXTERN:
931 case RID_MUTABLE:
932 case RID_THREAD:
933 /* Elaborated type specifiers. */
934 case RID_ENUM:
935 case RID_CLASS:
936 case RID_STRUCT:
937 case RID_UNION:
938 case RID_TYPENAME:
939 /* Simple type specifiers. */
940 case RID_CHAR:
941 case RID_CHAR16:
942 case RID_CHAR32:
943 case RID_WCHAR:
944 case RID_BOOL:
945 case RID_SHORT:
946 case RID_INT:
947 case RID_LONG:
948 case RID_SIGNED:
949 case RID_UNSIGNED:
950 case RID_FLOAT:
951 case RID_DOUBLE:
952 case RID_VOID:
953 /* GNU extensions. */
954 case RID_ATTRIBUTE:
955 case RID_TYPEOF:
956 /* C++0x extensions. */
957 case RID_DECLTYPE:
958 case RID_UNDERLYING_TYPE:
959 return true;
960
961 default:
962 if (token->keyword >= RID_FIRST_INT_N
963 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
964 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
965 return true;
966 return false;
967 }
968 }
969
970 /* Returns TRUE iff the token T begins a decltype type. */
971
972 static bool
973 token_is_decltype (cp_token *t)
974 {
975 return (t->keyword == RID_DECLTYPE
976 || t->type == CPP_DECLTYPE);
977 }
978
979 /* Returns TRUE iff the next token begins a decltype type. */
980
981 static bool
982 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
983 {
984 cp_token *t = cp_lexer_peek_token (lexer);
985 return token_is_decltype (t);
986 }
987
988 /* Return a pointer to the Nth token in the token stream. If N is 1,
989 then this is precisely equivalent to cp_lexer_peek_token (except
990 that it is not inline). One would like to disallow that case, but
991 there is one case (cp_parser_nth_token_starts_template_id) where
992 the caller passes a variable for N and it might be 1. */
993
994 static cp_token *
995 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
996 {
997 cp_token *token;
998
999 /* N is 1-based, not zero-based. */
1000 gcc_assert (n > 0);
1001
1002 if (cp_lexer_debugging_p (lexer))
1003 fprintf (cp_lexer_debug_stream,
1004 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1005
1006 --n;
1007 token = lexer->next_token;
1008 gcc_assert (!n || token != &eof_token);
1009 while (n != 0)
1010 {
1011 ++token;
1012 if (token == lexer->last_token)
1013 {
1014 token = &eof_token;
1015 break;
1016 }
1017
1018 if (!token->purged_p)
1019 --n;
1020 }
1021
1022 if (cp_lexer_debugging_p (lexer))
1023 {
1024 cp_lexer_print_token (cp_lexer_debug_stream, token);
1025 putc ('\n', cp_lexer_debug_stream);
1026 }
1027
1028 return token;
1029 }
1030
1031 /* Return the next token, and advance the lexer's next_token pointer
1032 to point to the next non-purged token. */
1033
1034 static cp_token *
1035 cp_lexer_consume_token (cp_lexer* lexer)
1036 {
1037 cp_token *token = lexer->next_token;
1038
1039 gcc_assert (token != &eof_token);
1040 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1041
1042 do
1043 {
1044 lexer->next_token++;
1045 if (lexer->next_token == lexer->last_token)
1046 {
1047 lexer->next_token = &eof_token;
1048 break;
1049 }
1050
1051 }
1052 while (lexer->next_token->purged_p);
1053
1054 cp_lexer_set_source_position_from_token (token);
1055
1056 /* Provide debugging output. */
1057 if (cp_lexer_debugging_p (lexer))
1058 {
1059 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1060 cp_lexer_print_token (cp_lexer_debug_stream, token);
1061 putc ('\n', cp_lexer_debug_stream);
1062 }
1063
1064 return token;
1065 }
1066
1067 /* Permanently remove the next token from the token stream, and
1068 advance the next_token pointer to refer to the next non-purged
1069 token. */
1070
1071 static void
1072 cp_lexer_purge_token (cp_lexer *lexer)
1073 {
1074 cp_token *tok = lexer->next_token;
1075
1076 gcc_assert (tok != &eof_token);
1077 tok->purged_p = true;
1078 tok->location = UNKNOWN_LOCATION;
1079 tok->u.value = NULL_TREE;
1080 tok->keyword = RID_MAX;
1081
1082 do
1083 {
1084 tok++;
1085 if (tok == lexer->last_token)
1086 {
1087 tok = &eof_token;
1088 break;
1089 }
1090 }
1091 while (tok->purged_p);
1092 lexer->next_token = tok;
1093 }
1094
1095 /* Permanently remove all tokens after TOK, up to, but not
1096 including, the token that will be returned next by
1097 cp_lexer_peek_token. */
1098
1099 static void
1100 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1101 {
1102 cp_token *peek = lexer->next_token;
1103
1104 if (peek == &eof_token)
1105 peek = lexer->last_token;
1106
1107 gcc_assert (tok < peek);
1108
1109 for ( tok += 1; tok != peek; tok += 1)
1110 {
1111 tok->purged_p = true;
1112 tok->location = UNKNOWN_LOCATION;
1113 tok->u.value = NULL_TREE;
1114 tok->keyword = RID_MAX;
1115 }
1116 }
1117
1118 /* Begin saving tokens. All tokens consumed after this point will be
1119 preserved. */
1120
1121 static void
1122 cp_lexer_save_tokens (cp_lexer* lexer)
1123 {
1124 /* Provide debugging output. */
1125 if (cp_lexer_debugging_p (lexer))
1126 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1127
1128 lexer->saved_tokens.safe_push (lexer->next_token);
1129 }
1130
1131 /* Commit to the portion of the token stream most recently saved. */
1132
1133 static void
1134 cp_lexer_commit_tokens (cp_lexer* lexer)
1135 {
1136 /* Provide debugging output. */
1137 if (cp_lexer_debugging_p (lexer))
1138 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1139
1140 lexer->saved_tokens.pop ();
1141 }
1142
1143 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1144 to the token stream. Stop saving tokens. */
1145
1146 static void
1147 cp_lexer_rollback_tokens (cp_lexer* lexer)
1148 {
1149 /* Provide debugging output. */
1150 if (cp_lexer_debugging_p (lexer))
1151 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1152
1153 lexer->next_token = lexer->saved_tokens.pop ();
1154 }
1155
1156 /* RAII wrapper around the above functions, with sanity checking. Creating
1157 a variable saves tokens, which are committed when the variable is
1158 destroyed unless they are explicitly rolled back by calling the rollback
1159 member function. */
1160
1161 struct saved_token_sentinel
1162 {
1163 cp_lexer *lexer;
1164 unsigned len;
1165 bool commit;
1166 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1167 {
1168 len = lexer->saved_tokens.length ();
1169 cp_lexer_save_tokens (lexer);
1170 }
1171 void rollback ()
1172 {
1173 cp_lexer_rollback_tokens (lexer);
1174 commit = false;
1175 }
1176 ~saved_token_sentinel()
1177 {
1178 if (commit)
1179 cp_lexer_commit_tokens (lexer);
1180 gcc_assert (lexer->saved_tokens.length () == len);
1181 }
1182 };
1183
1184 /* Print a representation of the TOKEN on the STREAM. */
1185
1186 static void
1187 cp_lexer_print_token (FILE * stream, cp_token *token)
1188 {
1189 /* We don't use cpp_type2name here because the parser defines
1190 a few tokens of its own. */
1191 static const char *const token_names[] = {
1192 /* cpplib-defined token types */
1193 #define OP(e, s) #e,
1194 #define TK(e, s) #e,
1195 TTYPE_TABLE
1196 #undef OP
1197 #undef TK
1198 /* C++ parser token types - see "Manifest constants", above. */
1199 "KEYWORD",
1200 "TEMPLATE_ID",
1201 "NESTED_NAME_SPECIFIER",
1202 };
1203
1204 /* For some tokens, print the associated data. */
1205 switch (token->type)
1206 {
1207 case CPP_KEYWORD:
1208 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1209 For example, `struct' is mapped to an INTEGER_CST. */
1210 if (!identifier_p (token->u.value))
1211 break;
1212 /* else fall through */
1213 case CPP_NAME:
1214 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1215 break;
1216
1217 case CPP_STRING:
1218 case CPP_STRING16:
1219 case CPP_STRING32:
1220 case CPP_WSTRING:
1221 case CPP_UTF8STRING:
1222 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1223 break;
1224
1225 case CPP_NUMBER:
1226 print_generic_expr (stream, token->u.value, 0);
1227 break;
1228
1229 default:
1230 /* If we have a name for the token, print it out. Otherwise, we
1231 simply give the numeric code. */
1232 if (token->type < ARRAY_SIZE(token_names))
1233 fputs (token_names[token->type], stream);
1234 else
1235 fprintf (stream, "[%d]", token->type);
1236 break;
1237 }
1238 }
1239
1240 DEBUG_FUNCTION void
1241 debug (cp_token &ref)
1242 {
1243 cp_lexer_print_token (stderr, &ref);
1244 fprintf (stderr, "\n");
1245 }
1246
1247 DEBUG_FUNCTION void
1248 debug (cp_token *ptr)
1249 {
1250 if (ptr)
1251 debug (*ptr);
1252 else
1253 fprintf (stderr, "<nil>\n");
1254 }
1255
1256
1257 /* Start emitting debugging information. */
1258
1259 static void
1260 cp_lexer_start_debugging (cp_lexer* lexer)
1261 {
1262 lexer->debugging_p = true;
1263 cp_lexer_debug_stream = stderr;
1264 }
1265
1266 /* Stop emitting debugging information. */
1267
1268 static void
1269 cp_lexer_stop_debugging (cp_lexer* lexer)
1270 {
1271 lexer->debugging_p = false;
1272 cp_lexer_debug_stream = NULL;
1273 }
1274
1275 /* Create a new cp_token_cache, representing a range of tokens. */
1276
1277 static cp_token_cache *
1278 cp_token_cache_new (cp_token *first, cp_token *last)
1279 {
1280 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1281 cache->first = first;
1282 cache->last = last;
1283 return cache;
1284 }
1285
1286 /* Diagnose if #pragma omp declare simd isn't followed immediately
1287 by function declaration or definition. */
1288
1289 static inline void
1290 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1291 {
1292 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1293 {
1294 error ("%<#pragma omp declare simd%> not immediately followed by "
1295 "function declaration or definition");
1296 parser->omp_declare_simd = NULL;
1297 }
1298 }
1299
1300 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1301 and put that into "omp declare simd" attribute. */
1302
1303 static inline void
1304 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1305 {
1306 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1307 {
1308 if (fndecl == error_mark_node)
1309 {
1310 parser->omp_declare_simd = NULL;
1311 return;
1312 }
1313 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1314 {
1315 cp_ensure_no_omp_declare_simd (parser);
1316 return;
1317 }
1318 }
1319 }
1320
1321 /* Diagnose if #pragma omp routine isn't followed immediately
1322 by function declaration or definition. */
1323
1324 static inline void
1325 cp_ensure_no_oacc_routine (cp_parser *parser)
1326 {
1327 cp_finalize_oacc_routine (parser, NULL_TREE, false, true);
1328 }
1329 \f
1330 /* Decl-specifiers. */
1331
1332 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1333
1334 static void
1335 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1336 {
1337 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1338 }
1339
1340 /* Declarators. */
1341
1342 /* Nothing other than the parser should be creating declarators;
1343 declarators are a semi-syntactic representation of C++ entities.
1344 Other parts of the front end that need to create entities (like
1345 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1346
1347 static cp_declarator *make_call_declarator
1348 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1349 static cp_declarator *make_array_declarator
1350 (cp_declarator *, tree);
1351 static cp_declarator *make_pointer_declarator
1352 (cp_cv_quals, cp_declarator *, tree);
1353 static cp_declarator *make_reference_declarator
1354 (cp_cv_quals, cp_declarator *, bool, tree);
1355 static cp_declarator *make_ptrmem_declarator
1356 (cp_cv_quals, tree, cp_declarator *, tree);
1357
1358 /* An erroneous declarator. */
1359 static cp_declarator *cp_error_declarator;
1360
1361 /* The obstack on which declarators and related data structures are
1362 allocated. */
1363 static struct obstack declarator_obstack;
1364
1365 /* Alloc BYTES from the declarator memory pool. */
1366
1367 static inline void *
1368 alloc_declarator (size_t bytes)
1369 {
1370 return obstack_alloc (&declarator_obstack, bytes);
1371 }
1372
1373 /* Allocate a declarator of the indicated KIND. Clear fields that are
1374 common to all declarators. */
1375
1376 static cp_declarator *
1377 make_declarator (cp_declarator_kind kind)
1378 {
1379 cp_declarator *declarator;
1380
1381 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1382 declarator->kind = kind;
1383 declarator->attributes = NULL_TREE;
1384 declarator->std_attributes = NULL_TREE;
1385 declarator->declarator = NULL;
1386 declarator->parameter_pack_p = false;
1387 declarator->id_loc = UNKNOWN_LOCATION;
1388
1389 return declarator;
1390 }
1391
1392 /* Make a declarator for a generalized identifier. If
1393 QUALIFYING_SCOPE is non-NULL, the identifier is
1394 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1395 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1396 is, if any. */
1397
1398 static cp_declarator *
1399 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1400 special_function_kind sfk)
1401 {
1402 cp_declarator *declarator;
1403
1404 /* It is valid to write:
1405
1406 class C { void f(); };
1407 typedef C D;
1408 void D::f();
1409
1410 The standard is not clear about whether `typedef const C D' is
1411 legal; as of 2002-09-15 the committee is considering that
1412 question. EDG 3.0 allows that syntax. Therefore, we do as
1413 well. */
1414 if (qualifying_scope && TYPE_P (qualifying_scope))
1415 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1416
1417 gcc_assert (identifier_p (unqualified_name)
1418 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1419 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1420
1421 declarator = make_declarator (cdk_id);
1422 declarator->u.id.qualifying_scope = qualifying_scope;
1423 declarator->u.id.unqualified_name = unqualified_name;
1424 declarator->u.id.sfk = sfk;
1425
1426 return declarator;
1427 }
1428
1429 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1430 of modifiers such as const or volatile to apply to the pointer
1431 type, represented as identifiers. ATTRIBUTES represent the attributes that
1432 appertain to the pointer or reference. */
1433
1434 cp_declarator *
1435 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1436 tree attributes)
1437 {
1438 cp_declarator *declarator;
1439
1440 declarator = make_declarator (cdk_pointer);
1441 declarator->declarator = target;
1442 declarator->u.pointer.qualifiers = cv_qualifiers;
1443 declarator->u.pointer.class_type = NULL_TREE;
1444 if (target)
1445 {
1446 declarator->id_loc = target->id_loc;
1447 declarator->parameter_pack_p = target->parameter_pack_p;
1448 target->parameter_pack_p = false;
1449 }
1450 else
1451 declarator->parameter_pack_p = false;
1452
1453 declarator->std_attributes = attributes;
1454
1455 return declarator;
1456 }
1457
1458 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1459 represent the attributes that appertain to the pointer or
1460 reference. */
1461
1462 cp_declarator *
1463 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1464 bool rvalue_ref, tree attributes)
1465 {
1466 cp_declarator *declarator;
1467
1468 declarator = make_declarator (cdk_reference);
1469 declarator->declarator = target;
1470 declarator->u.reference.qualifiers = cv_qualifiers;
1471 declarator->u.reference.rvalue_ref = rvalue_ref;
1472 if (target)
1473 {
1474 declarator->id_loc = target->id_loc;
1475 declarator->parameter_pack_p = target->parameter_pack_p;
1476 target->parameter_pack_p = false;
1477 }
1478 else
1479 declarator->parameter_pack_p = false;
1480
1481 declarator->std_attributes = attributes;
1482
1483 return declarator;
1484 }
1485
1486 /* Like make_pointer_declarator -- but for a pointer to a non-static
1487 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1488 appertain to the pointer or reference. */
1489
1490 cp_declarator *
1491 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1492 cp_declarator *pointee,
1493 tree attributes)
1494 {
1495 cp_declarator *declarator;
1496
1497 declarator = make_declarator (cdk_ptrmem);
1498 declarator->declarator = pointee;
1499 declarator->u.pointer.qualifiers = cv_qualifiers;
1500 declarator->u.pointer.class_type = class_type;
1501
1502 if (pointee)
1503 {
1504 declarator->parameter_pack_p = pointee->parameter_pack_p;
1505 pointee->parameter_pack_p = false;
1506 }
1507 else
1508 declarator->parameter_pack_p = false;
1509
1510 declarator->std_attributes = attributes;
1511
1512 return declarator;
1513 }
1514
1515 /* Make a declarator for the function given by TARGET, with the
1516 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1517 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1518 indicates what exceptions can be thrown. */
1519
1520 cp_declarator *
1521 make_call_declarator (cp_declarator *target,
1522 tree parms,
1523 cp_cv_quals cv_qualifiers,
1524 cp_virt_specifiers virt_specifiers,
1525 cp_ref_qualifier ref_qualifier,
1526 tree tx_qualifier,
1527 tree exception_specification,
1528 tree late_return_type,
1529 tree requires_clause)
1530 {
1531 cp_declarator *declarator;
1532
1533 declarator = make_declarator (cdk_function);
1534 declarator->declarator = target;
1535 declarator->u.function.parameters = parms;
1536 declarator->u.function.qualifiers = cv_qualifiers;
1537 declarator->u.function.virt_specifiers = virt_specifiers;
1538 declarator->u.function.ref_qualifier = ref_qualifier;
1539 declarator->u.function.tx_qualifier = tx_qualifier;
1540 declarator->u.function.exception_specification = exception_specification;
1541 declarator->u.function.late_return_type = late_return_type;
1542 declarator->u.function.requires_clause = requires_clause;
1543 if (target)
1544 {
1545 declarator->id_loc = target->id_loc;
1546 declarator->parameter_pack_p = target->parameter_pack_p;
1547 target->parameter_pack_p = false;
1548 }
1549 else
1550 declarator->parameter_pack_p = false;
1551
1552 return declarator;
1553 }
1554
1555 /* Make a declarator for an array of BOUNDS elements, each of which is
1556 defined by ELEMENT. */
1557
1558 cp_declarator *
1559 make_array_declarator (cp_declarator *element, tree bounds)
1560 {
1561 cp_declarator *declarator;
1562
1563 declarator = make_declarator (cdk_array);
1564 declarator->declarator = element;
1565 declarator->u.array.bounds = bounds;
1566 if (element)
1567 {
1568 declarator->id_loc = element->id_loc;
1569 declarator->parameter_pack_p = element->parameter_pack_p;
1570 element->parameter_pack_p = false;
1571 }
1572 else
1573 declarator->parameter_pack_p = false;
1574
1575 return declarator;
1576 }
1577
1578 /* Determine whether the declarator we've seen so far can be a
1579 parameter pack, when followed by an ellipsis. */
1580 static bool
1581 declarator_can_be_parameter_pack (cp_declarator *declarator)
1582 {
1583 if (declarator && declarator->parameter_pack_p)
1584 /* We already saw an ellipsis. */
1585 return false;
1586
1587 /* Search for a declarator name, or any other declarator that goes
1588 after the point where the ellipsis could appear in a parameter
1589 pack. If we find any of these, then this declarator can not be
1590 made into a parameter pack. */
1591 bool found = false;
1592 while (declarator && !found)
1593 {
1594 switch ((int)declarator->kind)
1595 {
1596 case cdk_id:
1597 case cdk_array:
1598 found = true;
1599 break;
1600
1601 case cdk_error:
1602 return true;
1603
1604 default:
1605 declarator = declarator->declarator;
1606 break;
1607 }
1608 }
1609
1610 return !found;
1611 }
1612
1613 cp_parameter_declarator *no_parameters;
1614
1615 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1616 DECLARATOR and DEFAULT_ARGUMENT. */
1617
1618 cp_parameter_declarator *
1619 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1620 cp_declarator *declarator,
1621 tree default_argument,
1622 bool template_parameter_pack_p = false)
1623 {
1624 cp_parameter_declarator *parameter;
1625
1626 parameter = ((cp_parameter_declarator *)
1627 alloc_declarator (sizeof (cp_parameter_declarator)));
1628 parameter->next = NULL;
1629 if (decl_specifiers)
1630 parameter->decl_specifiers = *decl_specifiers;
1631 else
1632 clear_decl_specs (&parameter->decl_specifiers);
1633 parameter->declarator = declarator;
1634 parameter->default_argument = default_argument;
1635 parameter->template_parameter_pack_p = template_parameter_pack_p;
1636
1637 return parameter;
1638 }
1639
1640 /* Returns true iff DECLARATOR is a declaration for a function. */
1641
1642 static bool
1643 function_declarator_p (const cp_declarator *declarator)
1644 {
1645 while (declarator)
1646 {
1647 if (declarator->kind == cdk_function
1648 && declarator->declarator->kind == cdk_id)
1649 return true;
1650 if (declarator->kind == cdk_id
1651 || declarator->kind == cdk_error)
1652 return false;
1653 declarator = declarator->declarator;
1654 }
1655 return false;
1656 }
1657
1658 /* The parser. */
1659
1660 /* Overview
1661 --------
1662
1663 A cp_parser parses the token stream as specified by the C++
1664 grammar. Its job is purely parsing, not semantic analysis. For
1665 example, the parser breaks the token stream into declarators,
1666 expressions, statements, and other similar syntactic constructs.
1667 It does not check that the types of the expressions on either side
1668 of an assignment-statement are compatible, or that a function is
1669 not declared with a parameter of type `void'.
1670
1671 The parser invokes routines elsewhere in the compiler to perform
1672 semantic analysis and to build up the abstract syntax tree for the
1673 code processed.
1674
1675 The parser (and the template instantiation code, which is, in a
1676 way, a close relative of parsing) are the only parts of the
1677 compiler that should be calling push_scope and pop_scope, or
1678 related functions. The parser (and template instantiation code)
1679 keeps track of what scope is presently active; everything else
1680 should simply honor that. (The code that generates static
1681 initializers may also need to set the scope, in order to check
1682 access control correctly when emitting the initializers.)
1683
1684 Methodology
1685 -----------
1686
1687 The parser is of the standard recursive-descent variety. Upcoming
1688 tokens in the token stream are examined in order to determine which
1689 production to use when parsing a non-terminal. Some C++ constructs
1690 require arbitrary look ahead to disambiguate. For example, it is
1691 impossible, in the general case, to tell whether a statement is an
1692 expression or declaration without scanning the entire statement.
1693 Therefore, the parser is capable of "parsing tentatively." When the
1694 parser is not sure what construct comes next, it enters this mode.
1695 Then, while we attempt to parse the construct, the parser queues up
1696 error messages, rather than issuing them immediately, and saves the
1697 tokens it consumes. If the construct is parsed successfully, the
1698 parser "commits", i.e., it issues any queued error messages and
1699 the tokens that were being preserved are permanently discarded.
1700 If, however, the construct is not parsed successfully, the parser
1701 rolls back its state completely so that it can resume parsing using
1702 a different alternative.
1703
1704 Future Improvements
1705 -------------------
1706
1707 The performance of the parser could probably be improved substantially.
1708 We could often eliminate the need to parse tentatively by looking ahead
1709 a little bit. In some places, this approach might not entirely eliminate
1710 the need to parse tentatively, but it might still speed up the average
1711 case. */
1712
1713 /* Flags that are passed to some parsing functions. These values can
1714 be bitwise-ored together. */
1715
1716 enum
1717 {
1718 /* No flags. */
1719 CP_PARSER_FLAGS_NONE = 0x0,
1720 /* The construct is optional. If it is not present, then no error
1721 should be issued. */
1722 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1723 /* When parsing a type-specifier, treat user-defined type-names
1724 as non-type identifiers. */
1725 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1726 /* When parsing a type-specifier, do not try to parse a class-specifier
1727 or enum-specifier. */
1728 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1729 /* When parsing a decl-specifier-seq, only allow type-specifier or
1730 constexpr. */
1731 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1732 };
1733
1734 /* This type is used for parameters and variables which hold
1735 combinations of the above flags. */
1736 typedef int cp_parser_flags;
1737
1738 /* The different kinds of declarators we want to parse. */
1739
1740 enum cp_parser_declarator_kind
1741 {
1742 /* We want an abstract declarator. */
1743 CP_PARSER_DECLARATOR_ABSTRACT,
1744 /* We want a named declarator. */
1745 CP_PARSER_DECLARATOR_NAMED,
1746 /* We don't mind, but the name must be an unqualified-id. */
1747 CP_PARSER_DECLARATOR_EITHER
1748 };
1749
1750 /* The precedence values used to parse binary expressions. The minimum value
1751 of PREC must be 1, because zero is reserved to quickly discriminate
1752 binary operators from other tokens. */
1753
1754 enum cp_parser_prec
1755 {
1756 PREC_NOT_OPERATOR,
1757 PREC_LOGICAL_OR_EXPRESSION,
1758 PREC_LOGICAL_AND_EXPRESSION,
1759 PREC_INCLUSIVE_OR_EXPRESSION,
1760 PREC_EXCLUSIVE_OR_EXPRESSION,
1761 PREC_AND_EXPRESSION,
1762 PREC_EQUALITY_EXPRESSION,
1763 PREC_RELATIONAL_EXPRESSION,
1764 PREC_SHIFT_EXPRESSION,
1765 PREC_ADDITIVE_EXPRESSION,
1766 PREC_MULTIPLICATIVE_EXPRESSION,
1767 PREC_PM_EXPRESSION,
1768 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1769 };
1770
1771 /* A mapping from a token type to a corresponding tree node type, with a
1772 precedence value. */
1773
1774 struct cp_parser_binary_operations_map_node
1775 {
1776 /* The token type. */
1777 enum cpp_ttype token_type;
1778 /* The corresponding tree code. */
1779 enum tree_code tree_type;
1780 /* The precedence of this operator. */
1781 enum cp_parser_prec prec;
1782 };
1783
1784 struct cp_parser_expression_stack_entry
1785 {
1786 /* Left hand side of the binary operation we are currently
1787 parsing. */
1788 tree lhs;
1789 /* Original tree code for left hand side, if it was a binary
1790 expression itself (used for -Wparentheses). */
1791 enum tree_code lhs_type;
1792 /* Tree code for the binary operation we are parsing. */
1793 enum tree_code tree_type;
1794 /* Precedence of the binary operation we are parsing. */
1795 enum cp_parser_prec prec;
1796 /* Location of the binary operation we are parsing. */
1797 location_t loc;
1798 };
1799
1800 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1801 entries because precedence levels on the stack are monotonically
1802 increasing. */
1803 typedef struct cp_parser_expression_stack_entry
1804 cp_parser_expression_stack[NUM_PREC_VALUES];
1805
1806 /* Prototypes. */
1807
1808 /* Constructors and destructors. */
1809
1810 static cp_parser_context *cp_parser_context_new
1811 (cp_parser_context *);
1812
1813 /* Class variables. */
1814
1815 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1816
1817 /* The operator-precedence table used by cp_parser_binary_expression.
1818 Transformed into an associative array (binops_by_token) by
1819 cp_parser_new. */
1820
1821 static const cp_parser_binary_operations_map_node binops[] = {
1822 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1823 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1824
1825 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1826 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1827 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1828
1829 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1830 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1831
1832 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1833 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1834
1835 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1836 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1837 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1838 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1839
1840 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1841 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1842
1843 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1844
1845 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1846
1847 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1848
1849 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1850
1851 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1852 };
1853
1854 /* The same as binops, but initialized by cp_parser_new so that
1855 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1856 for speed. */
1857 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1858
1859 /* Constructors and destructors. */
1860
1861 /* Construct a new context. The context below this one on the stack
1862 is given by NEXT. */
1863
1864 static cp_parser_context *
1865 cp_parser_context_new (cp_parser_context* next)
1866 {
1867 cp_parser_context *context;
1868
1869 /* Allocate the storage. */
1870 if (cp_parser_context_free_list != NULL)
1871 {
1872 /* Pull the first entry from the free list. */
1873 context = cp_parser_context_free_list;
1874 cp_parser_context_free_list = context->next;
1875 memset (context, 0, sizeof (*context));
1876 }
1877 else
1878 context = ggc_cleared_alloc<cp_parser_context> ();
1879
1880 /* No errors have occurred yet in this context. */
1881 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1882 /* If this is not the bottommost context, copy information that we
1883 need from the previous context. */
1884 if (next)
1885 {
1886 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1887 expression, then we are parsing one in this context, too. */
1888 context->object_type = next->object_type;
1889 /* Thread the stack. */
1890 context->next = next;
1891 }
1892
1893 return context;
1894 }
1895
1896 /* Managing the unparsed function queues. */
1897
1898 #define unparsed_funs_with_default_args \
1899 parser->unparsed_queues->last ().funs_with_default_args
1900 #define unparsed_funs_with_definitions \
1901 parser->unparsed_queues->last ().funs_with_definitions
1902 #define unparsed_nsdmis \
1903 parser->unparsed_queues->last ().nsdmis
1904 #define unparsed_classes \
1905 parser->unparsed_queues->last ().classes
1906
1907 static void
1908 push_unparsed_function_queues (cp_parser *parser)
1909 {
1910 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1911 vec_safe_push (parser->unparsed_queues, e);
1912 }
1913
1914 static void
1915 pop_unparsed_function_queues (cp_parser *parser)
1916 {
1917 release_tree_vector (unparsed_funs_with_definitions);
1918 parser->unparsed_queues->pop ();
1919 }
1920
1921 /* Prototypes. */
1922
1923 /* Constructors and destructors. */
1924
1925 static cp_parser *cp_parser_new
1926 (void);
1927
1928 /* Routines to parse various constructs.
1929
1930 Those that return `tree' will return the error_mark_node (rather
1931 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1932 Sometimes, they will return an ordinary node if error-recovery was
1933 attempted, even though a parse error occurred. So, to check
1934 whether or not a parse error occurred, you should always use
1935 cp_parser_error_occurred. If the construct is optional (indicated
1936 either by an `_opt' in the name of the function that does the
1937 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1938 the construct is not present. */
1939
1940 /* Lexical conventions [gram.lex] */
1941
1942 static tree cp_parser_identifier
1943 (cp_parser *);
1944 static tree cp_parser_string_literal
1945 (cp_parser *, bool, bool, bool);
1946 static tree cp_parser_userdef_char_literal
1947 (cp_parser *);
1948 static tree cp_parser_userdef_string_literal
1949 (tree);
1950 static tree cp_parser_userdef_numeric_literal
1951 (cp_parser *);
1952
1953 /* Basic concepts [gram.basic] */
1954
1955 static bool cp_parser_translation_unit
1956 (cp_parser *);
1957
1958 /* Expressions [gram.expr] */
1959
1960 static tree cp_parser_primary_expression
1961 (cp_parser *, bool, bool, bool, cp_id_kind *);
1962 static tree cp_parser_id_expression
1963 (cp_parser *, bool, bool, bool *, bool, bool);
1964 static tree cp_parser_unqualified_id
1965 (cp_parser *, bool, bool, bool, bool);
1966 static tree cp_parser_nested_name_specifier_opt
1967 (cp_parser *, bool, bool, bool, bool);
1968 static tree cp_parser_nested_name_specifier
1969 (cp_parser *, bool, bool, bool, bool);
1970 static tree cp_parser_qualifying_entity
1971 (cp_parser *, bool, bool, bool, bool, bool);
1972 static tree cp_parser_postfix_expression
1973 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1974 static tree cp_parser_postfix_open_square_expression
1975 (cp_parser *, tree, bool, bool);
1976 static tree cp_parser_postfix_dot_deref_expression
1977 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1978 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1979 (cp_parser *, int, bool, bool, bool *, bool = false);
1980 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1981 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1982 static void cp_parser_pseudo_destructor_name
1983 (cp_parser *, tree, tree *, tree *);
1984 static tree cp_parser_unary_expression
1985 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1986 static enum tree_code cp_parser_unary_operator
1987 (cp_token *);
1988 static tree cp_parser_new_expression
1989 (cp_parser *);
1990 static vec<tree, va_gc> *cp_parser_new_placement
1991 (cp_parser *);
1992 static tree cp_parser_new_type_id
1993 (cp_parser *, tree *);
1994 static cp_declarator *cp_parser_new_declarator_opt
1995 (cp_parser *);
1996 static cp_declarator *cp_parser_direct_new_declarator
1997 (cp_parser *);
1998 static vec<tree, va_gc> *cp_parser_new_initializer
1999 (cp_parser *);
2000 static tree cp_parser_delete_expression
2001 (cp_parser *);
2002 static tree cp_parser_cast_expression
2003 (cp_parser *, bool, bool, bool, cp_id_kind *);
2004 static tree cp_parser_binary_expression
2005 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2006 static tree cp_parser_question_colon_clause
2007 (cp_parser *, tree);
2008 static tree cp_parser_assignment_expression
2009 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2010 static enum tree_code cp_parser_assignment_operator_opt
2011 (cp_parser *);
2012 static tree cp_parser_expression
2013 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2014 static tree cp_parser_constant_expression
2015 (cp_parser *, bool = false, bool * = NULL);
2016 static tree cp_parser_builtin_offsetof
2017 (cp_parser *);
2018 static tree cp_parser_lambda_expression
2019 (cp_parser *);
2020 static void cp_parser_lambda_introducer
2021 (cp_parser *, tree);
2022 static bool cp_parser_lambda_declarator_opt
2023 (cp_parser *, tree);
2024 static void cp_parser_lambda_body
2025 (cp_parser *, tree);
2026
2027 /* Statements [gram.stmt.stmt] */
2028
2029 static void cp_parser_statement
2030 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2031 static void cp_parser_label_for_labeled_statement
2032 (cp_parser *, tree);
2033 static tree cp_parser_expression_statement
2034 (cp_parser *, tree);
2035 static tree cp_parser_compound_statement
2036 (cp_parser *, tree, int, bool);
2037 static void cp_parser_statement_seq_opt
2038 (cp_parser *, tree);
2039 static tree cp_parser_selection_statement
2040 (cp_parser *, bool *, vec<tree> *);
2041 static tree cp_parser_condition
2042 (cp_parser *);
2043 static tree cp_parser_iteration_statement
2044 (cp_parser *, bool);
2045 static bool cp_parser_for_init_statement
2046 (cp_parser *, tree *decl);
2047 static tree cp_parser_for
2048 (cp_parser *, bool);
2049 static tree cp_parser_c_for
2050 (cp_parser *, tree, tree, bool);
2051 static tree cp_parser_range_for
2052 (cp_parser *, tree, tree, tree, bool);
2053 static void do_range_for_auto_deduction
2054 (tree, tree);
2055 static tree cp_parser_perform_range_for_lookup
2056 (tree, tree *, tree *);
2057 static tree cp_parser_range_for_member_function
2058 (tree, tree);
2059 static tree cp_parser_jump_statement
2060 (cp_parser *);
2061 static void cp_parser_declaration_statement
2062 (cp_parser *);
2063
2064 static tree cp_parser_implicitly_scoped_statement
2065 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2066 static void cp_parser_already_scoped_statement
2067 (cp_parser *, const token_indent_info &);
2068
2069 /* Declarations [gram.dcl.dcl] */
2070
2071 static void cp_parser_declaration_seq_opt
2072 (cp_parser *);
2073 static void cp_parser_declaration
2074 (cp_parser *);
2075 static void cp_parser_block_declaration
2076 (cp_parser *, bool);
2077 static void cp_parser_simple_declaration
2078 (cp_parser *, bool, tree *);
2079 static void cp_parser_decl_specifier_seq
2080 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2081 static tree cp_parser_storage_class_specifier_opt
2082 (cp_parser *);
2083 static tree cp_parser_function_specifier_opt
2084 (cp_parser *, cp_decl_specifier_seq *);
2085 static tree cp_parser_type_specifier
2086 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2087 int *, bool *);
2088 static tree cp_parser_simple_type_specifier
2089 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2090 static tree cp_parser_type_name
2091 (cp_parser *, bool);
2092 static tree cp_parser_type_name
2093 (cp_parser *);
2094 static tree cp_parser_nonclass_name
2095 (cp_parser* parser);
2096 static tree cp_parser_elaborated_type_specifier
2097 (cp_parser *, bool, bool);
2098 static tree cp_parser_enum_specifier
2099 (cp_parser *);
2100 static void cp_parser_enumerator_list
2101 (cp_parser *, tree);
2102 static void cp_parser_enumerator_definition
2103 (cp_parser *, tree);
2104 static tree cp_parser_namespace_name
2105 (cp_parser *);
2106 static void cp_parser_namespace_definition
2107 (cp_parser *);
2108 static void cp_parser_namespace_body
2109 (cp_parser *);
2110 static tree cp_parser_qualified_namespace_specifier
2111 (cp_parser *);
2112 static void cp_parser_namespace_alias_definition
2113 (cp_parser *);
2114 static bool cp_parser_using_declaration
2115 (cp_parser *, bool);
2116 static void cp_parser_using_directive
2117 (cp_parser *);
2118 static tree cp_parser_alias_declaration
2119 (cp_parser *);
2120 static void cp_parser_asm_definition
2121 (cp_parser *);
2122 static void cp_parser_linkage_specification
2123 (cp_parser *);
2124 static void cp_parser_static_assert
2125 (cp_parser *, bool);
2126 static tree cp_parser_decltype
2127 (cp_parser *);
2128
2129 /* Declarators [gram.dcl.decl] */
2130
2131 static tree cp_parser_init_declarator
2132 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2133 bool, bool, int, bool *, tree *, bool, location_t *);
2134 static cp_declarator *cp_parser_declarator
2135 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2136 static cp_declarator *cp_parser_direct_declarator
2137 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2138 static enum tree_code cp_parser_ptr_operator
2139 (cp_parser *, tree *, cp_cv_quals *, tree *);
2140 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2141 (cp_parser *);
2142 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2143 (cp_parser *);
2144 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2145 (cp_parser *);
2146 static tree cp_parser_tx_qualifier_opt
2147 (cp_parser *);
2148 static tree cp_parser_late_return_type_opt
2149 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2150 static tree cp_parser_declarator_id
2151 (cp_parser *, bool);
2152 static tree cp_parser_type_id
2153 (cp_parser *);
2154 static tree cp_parser_template_type_arg
2155 (cp_parser *);
2156 static tree cp_parser_trailing_type_id (cp_parser *);
2157 static tree cp_parser_type_id_1
2158 (cp_parser *, bool, bool);
2159 static void cp_parser_type_specifier_seq
2160 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2161 static tree cp_parser_parameter_declaration_clause
2162 (cp_parser *);
2163 static tree cp_parser_parameter_declaration_list
2164 (cp_parser *, bool *);
2165 static cp_parameter_declarator *cp_parser_parameter_declaration
2166 (cp_parser *, bool, bool *);
2167 static tree cp_parser_default_argument
2168 (cp_parser *, bool);
2169 static void cp_parser_function_body
2170 (cp_parser *, bool);
2171 static tree cp_parser_initializer
2172 (cp_parser *, bool *, bool *);
2173 static tree cp_parser_initializer_clause
2174 (cp_parser *, bool *);
2175 static tree cp_parser_braced_list
2176 (cp_parser*, bool*);
2177 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2178 (cp_parser *, bool *);
2179
2180 static bool cp_parser_ctor_initializer_opt_and_function_body
2181 (cp_parser *, bool);
2182
2183 static tree cp_parser_late_parsing_omp_declare_simd
2184 (cp_parser *, tree);
2185
2186 static tree cp_parser_late_parsing_cilk_simd_fn_info
2187 (cp_parser *, tree);
2188
2189 static tree synthesize_implicit_template_parm
2190 (cp_parser *, tree);
2191 static tree finish_fully_implicit_template
2192 (cp_parser *, tree);
2193
2194 /* Classes [gram.class] */
2195
2196 static tree cp_parser_class_name
2197 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2198 static tree cp_parser_class_specifier
2199 (cp_parser *);
2200 static tree cp_parser_class_head
2201 (cp_parser *, bool *);
2202 static enum tag_types cp_parser_class_key
2203 (cp_parser *);
2204 static void cp_parser_type_parameter_key
2205 (cp_parser* parser);
2206 static void cp_parser_member_specification_opt
2207 (cp_parser *);
2208 static void cp_parser_member_declaration
2209 (cp_parser *);
2210 static tree cp_parser_pure_specifier
2211 (cp_parser *);
2212 static tree cp_parser_constant_initializer
2213 (cp_parser *);
2214
2215 /* Derived classes [gram.class.derived] */
2216
2217 static tree cp_parser_base_clause
2218 (cp_parser *);
2219 static tree cp_parser_base_specifier
2220 (cp_parser *);
2221
2222 /* Special member functions [gram.special] */
2223
2224 static tree cp_parser_conversion_function_id
2225 (cp_parser *);
2226 static tree cp_parser_conversion_type_id
2227 (cp_parser *);
2228 static cp_declarator *cp_parser_conversion_declarator_opt
2229 (cp_parser *);
2230 static bool cp_parser_ctor_initializer_opt
2231 (cp_parser *);
2232 static void cp_parser_mem_initializer_list
2233 (cp_parser *);
2234 static tree cp_parser_mem_initializer
2235 (cp_parser *);
2236 static tree cp_parser_mem_initializer_id
2237 (cp_parser *);
2238
2239 /* Overloading [gram.over] */
2240
2241 static tree cp_parser_operator_function_id
2242 (cp_parser *);
2243 static tree cp_parser_operator
2244 (cp_parser *);
2245
2246 /* Templates [gram.temp] */
2247
2248 static void cp_parser_template_declaration
2249 (cp_parser *, bool);
2250 static tree cp_parser_template_parameter_list
2251 (cp_parser *);
2252 static tree cp_parser_template_parameter
2253 (cp_parser *, bool *, bool *);
2254 static tree cp_parser_type_parameter
2255 (cp_parser *, bool *);
2256 static tree cp_parser_template_id
2257 (cp_parser *, bool, bool, enum tag_types, bool);
2258 static tree cp_parser_template_name
2259 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2260 static tree cp_parser_template_argument_list
2261 (cp_parser *);
2262 static tree cp_parser_template_argument
2263 (cp_parser *);
2264 static void cp_parser_explicit_instantiation
2265 (cp_parser *);
2266 static void cp_parser_explicit_specialization
2267 (cp_parser *);
2268
2269 /* Exception handling [gram.exception] */
2270
2271 static tree cp_parser_try_block
2272 (cp_parser *);
2273 static bool cp_parser_function_try_block
2274 (cp_parser *);
2275 static void cp_parser_handler_seq
2276 (cp_parser *);
2277 static void cp_parser_handler
2278 (cp_parser *);
2279 static tree cp_parser_exception_declaration
2280 (cp_parser *);
2281 static tree cp_parser_throw_expression
2282 (cp_parser *);
2283 static tree cp_parser_exception_specification_opt
2284 (cp_parser *);
2285 static tree cp_parser_type_id_list
2286 (cp_parser *);
2287
2288 /* GNU Extensions */
2289
2290 static tree cp_parser_asm_specification_opt
2291 (cp_parser *);
2292 static tree cp_parser_asm_operand_list
2293 (cp_parser *);
2294 static tree cp_parser_asm_clobber_list
2295 (cp_parser *);
2296 static tree cp_parser_asm_label_list
2297 (cp_parser *);
2298 static bool cp_next_tokens_can_be_attribute_p
2299 (cp_parser *);
2300 static bool cp_next_tokens_can_be_gnu_attribute_p
2301 (cp_parser *);
2302 static bool cp_next_tokens_can_be_std_attribute_p
2303 (cp_parser *);
2304 static bool cp_nth_tokens_can_be_std_attribute_p
2305 (cp_parser *, size_t);
2306 static bool cp_nth_tokens_can_be_gnu_attribute_p
2307 (cp_parser *, size_t);
2308 static bool cp_nth_tokens_can_be_attribute_p
2309 (cp_parser *, size_t);
2310 static tree cp_parser_attributes_opt
2311 (cp_parser *);
2312 static tree cp_parser_gnu_attributes_opt
2313 (cp_parser *);
2314 static tree cp_parser_gnu_attribute_list
2315 (cp_parser *);
2316 static tree cp_parser_std_attribute
2317 (cp_parser *);
2318 static tree cp_parser_std_attribute_spec
2319 (cp_parser *);
2320 static tree cp_parser_std_attribute_spec_seq
2321 (cp_parser *);
2322 static bool cp_parser_extension_opt
2323 (cp_parser *, int *);
2324 static void cp_parser_label_declaration
2325 (cp_parser *);
2326
2327 /* Concept Extensions */
2328
2329 static tree cp_parser_requires_clause
2330 (cp_parser *);
2331 static tree cp_parser_requires_clause_opt
2332 (cp_parser *);
2333 static tree cp_parser_requires_expression
2334 (cp_parser *);
2335 static tree cp_parser_requirement_parameter_list
2336 (cp_parser *);
2337 static tree cp_parser_requirement_body
2338 (cp_parser *);
2339 static tree cp_parser_requirement_list
2340 (cp_parser *);
2341 static tree cp_parser_requirement
2342 (cp_parser *);
2343 static tree cp_parser_simple_requirement
2344 (cp_parser *);
2345 static tree cp_parser_compound_requirement
2346 (cp_parser *);
2347 static tree cp_parser_type_requirement
2348 (cp_parser *);
2349 static tree cp_parser_nested_requirement
2350 (cp_parser *);
2351
2352 /* Transactional Memory Extensions */
2353
2354 static tree cp_parser_transaction
2355 (cp_parser *, cp_token *);
2356 static tree cp_parser_transaction_expression
2357 (cp_parser *, enum rid);
2358 static bool cp_parser_function_transaction
2359 (cp_parser *, enum rid);
2360 static tree cp_parser_transaction_cancel
2361 (cp_parser *);
2362
2363 enum pragma_context {
2364 pragma_external,
2365 pragma_member,
2366 pragma_objc_icode,
2367 pragma_stmt,
2368 pragma_compound
2369 };
2370 static bool cp_parser_pragma
2371 (cp_parser *, enum pragma_context);
2372
2373 /* Objective-C++ Productions */
2374
2375 static tree cp_parser_objc_message_receiver
2376 (cp_parser *);
2377 static tree cp_parser_objc_message_args
2378 (cp_parser *);
2379 static tree cp_parser_objc_message_expression
2380 (cp_parser *);
2381 static tree cp_parser_objc_encode_expression
2382 (cp_parser *);
2383 static tree cp_parser_objc_defs_expression
2384 (cp_parser *);
2385 static tree cp_parser_objc_protocol_expression
2386 (cp_parser *);
2387 static tree cp_parser_objc_selector_expression
2388 (cp_parser *);
2389 static tree cp_parser_objc_expression
2390 (cp_parser *);
2391 static bool cp_parser_objc_selector_p
2392 (enum cpp_ttype);
2393 static tree cp_parser_objc_selector
2394 (cp_parser *);
2395 static tree cp_parser_objc_protocol_refs_opt
2396 (cp_parser *);
2397 static void cp_parser_objc_declaration
2398 (cp_parser *, tree);
2399 static tree cp_parser_objc_statement
2400 (cp_parser *);
2401 static bool cp_parser_objc_valid_prefix_attributes
2402 (cp_parser *, tree *);
2403 static void cp_parser_objc_at_property_declaration
2404 (cp_parser *) ;
2405 static void cp_parser_objc_at_synthesize_declaration
2406 (cp_parser *) ;
2407 static void cp_parser_objc_at_dynamic_declaration
2408 (cp_parser *) ;
2409 static tree cp_parser_objc_struct_declaration
2410 (cp_parser *) ;
2411
2412 /* Utility Routines */
2413
2414 static tree cp_parser_lookup_name
2415 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2416 static tree cp_parser_lookup_name_simple
2417 (cp_parser *, tree, location_t);
2418 static tree cp_parser_maybe_treat_template_as_class
2419 (tree, bool);
2420 static bool cp_parser_check_declarator_template_parameters
2421 (cp_parser *, cp_declarator *, location_t);
2422 static bool cp_parser_check_template_parameters
2423 (cp_parser *, unsigned, location_t, cp_declarator *);
2424 static tree cp_parser_simple_cast_expression
2425 (cp_parser *);
2426 static tree cp_parser_global_scope_opt
2427 (cp_parser *, bool);
2428 static bool cp_parser_constructor_declarator_p
2429 (cp_parser *, bool);
2430 static tree cp_parser_function_definition_from_specifiers_and_declarator
2431 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2432 static tree cp_parser_function_definition_after_declarator
2433 (cp_parser *, bool);
2434 static bool cp_parser_template_declaration_after_export
2435 (cp_parser *, bool);
2436 static void cp_parser_perform_template_parameter_access_checks
2437 (vec<deferred_access_check, va_gc> *);
2438 static tree cp_parser_single_declaration
2439 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2440 static tree cp_parser_functional_cast
2441 (cp_parser *, tree);
2442 static tree cp_parser_save_member_function_body
2443 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree, bool);
2444 static tree cp_parser_save_nsdmi
2445 (cp_parser *);
2446 static tree cp_parser_enclosed_template_argument_list
2447 (cp_parser *);
2448 static void cp_parser_save_default_args
2449 (cp_parser *, tree);
2450 static void cp_parser_late_parsing_for_member
2451 (cp_parser *, tree);
2452 static tree cp_parser_late_parse_one_default_arg
2453 (cp_parser *, tree, tree, tree);
2454 static void cp_parser_late_parsing_nsdmi
2455 (cp_parser *, tree);
2456 static void cp_parser_late_parsing_default_args
2457 (cp_parser *, tree);
2458 static tree cp_parser_sizeof_operand
2459 (cp_parser *, enum rid);
2460 static tree cp_parser_trait_expr
2461 (cp_parser *, enum rid);
2462 static bool cp_parser_declares_only_class_p
2463 (cp_parser *);
2464 static void cp_parser_set_storage_class
2465 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2466 static void cp_parser_set_decl_spec_type
2467 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2468 static void set_and_check_decl_spec_loc
2469 (cp_decl_specifier_seq *decl_specs,
2470 cp_decl_spec ds, cp_token *);
2471 static bool cp_parser_friend_p
2472 (const cp_decl_specifier_seq *);
2473 static void cp_parser_required_error
2474 (cp_parser *, required_token, bool);
2475 static cp_token *cp_parser_require
2476 (cp_parser *, enum cpp_ttype, required_token);
2477 static cp_token *cp_parser_require_keyword
2478 (cp_parser *, enum rid, required_token);
2479 static bool cp_parser_token_starts_function_definition_p
2480 (cp_token *);
2481 static bool cp_parser_next_token_starts_class_definition_p
2482 (cp_parser *);
2483 static bool cp_parser_next_token_ends_template_argument_p
2484 (cp_parser *);
2485 static bool cp_parser_nth_token_starts_template_argument_list_p
2486 (cp_parser *, size_t);
2487 static enum tag_types cp_parser_token_is_class_key
2488 (cp_token *);
2489 static enum tag_types cp_parser_token_is_type_parameter_key
2490 (cp_token *);
2491 static void cp_parser_check_class_key
2492 (enum tag_types, tree type);
2493 static void cp_parser_check_access_in_redeclaration
2494 (tree type, location_t location);
2495 static bool cp_parser_optional_template_keyword
2496 (cp_parser *);
2497 static void cp_parser_pre_parsed_nested_name_specifier
2498 (cp_parser *);
2499 static bool cp_parser_cache_group
2500 (cp_parser *, enum cpp_ttype, unsigned);
2501 static tree cp_parser_cache_defarg
2502 (cp_parser *parser, bool nsdmi);
2503 static void cp_parser_parse_tentatively
2504 (cp_parser *);
2505 static void cp_parser_commit_to_tentative_parse
2506 (cp_parser *);
2507 static void cp_parser_commit_to_topmost_tentative_parse
2508 (cp_parser *);
2509 static void cp_parser_abort_tentative_parse
2510 (cp_parser *);
2511 static bool cp_parser_parse_definitely
2512 (cp_parser *);
2513 static inline bool cp_parser_parsing_tentatively
2514 (cp_parser *);
2515 static bool cp_parser_uncommitted_to_tentative_parse_p
2516 (cp_parser *);
2517 static void cp_parser_error
2518 (cp_parser *, const char *);
2519 static void cp_parser_name_lookup_error
2520 (cp_parser *, tree, tree, name_lookup_error, location_t);
2521 static bool cp_parser_simulate_error
2522 (cp_parser *);
2523 static bool cp_parser_check_type_definition
2524 (cp_parser *);
2525 static void cp_parser_check_for_definition_in_return_type
2526 (cp_declarator *, tree, location_t type_location);
2527 static void cp_parser_check_for_invalid_template_id
2528 (cp_parser *, tree, enum tag_types, location_t location);
2529 static bool cp_parser_non_integral_constant_expression
2530 (cp_parser *, non_integral_constant);
2531 static void cp_parser_diagnose_invalid_type_name
2532 (cp_parser *, tree, location_t);
2533 static bool cp_parser_parse_and_diagnose_invalid_type_name
2534 (cp_parser *);
2535 static int cp_parser_skip_to_closing_parenthesis
2536 (cp_parser *, bool, bool, bool);
2537 static void cp_parser_skip_to_end_of_statement
2538 (cp_parser *);
2539 static void cp_parser_consume_semicolon_at_end_of_statement
2540 (cp_parser *);
2541 static void cp_parser_skip_to_end_of_block_or_statement
2542 (cp_parser *);
2543 static bool cp_parser_skip_to_closing_brace
2544 (cp_parser *);
2545 static void cp_parser_skip_to_end_of_template_parameter_list
2546 (cp_parser *);
2547 static void cp_parser_skip_to_pragma_eol
2548 (cp_parser*, cp_token *);
2549 static bool cp_parser_error_occurred
2550 (cp_parser *);
2551 static bool cp_parser_allow_gnu_extensions_p
2552 (cp_parser *);
2553 static bool cp_parser_is_pure_string_literal
2554 (cp_token *);
2555 static bool cp_parser_is_string_literal
2556 (cp_token *);
2557 static bool cp_parser_is_keyword
2558 (cp_token *, enum rid);
2559 static tree cp_parser_make_typename_type
2560 (cp_parser *, tree, location_t location);
2561 static cp_declarator * cp_parser_make_indirect_declarator
2562 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2563 static bool cp_parser_compound_literal_p
2564 (cp_parser *);
2565 static bool cp_parser_array_designator_p
2566 (cp_parser *);
2567 static bool cp_parser_skip_to_closing_square_bracket
2568 (cp_parser *);
2569
2570 /* Concept-related syntactic transformations */
2571
2572 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2573 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2574
2575 // -------------------------------------------------------------------------- //
2576 // Unevaluated Operand Guard
2577 //
2578 // Implementation of an RAII helper for unevaluated operand parsing.
2579 cp_unevaluated::cp_unevaluated ()
2580 {
2581 ++cp_unevaluated_operand;
2582 ++c_inhibit_evaluation_warnings;
2583 }
2584
2585 cp_unevaluated::~cp_unevaluated ()
2586 {
2587 --c_inhibit_evaluation_warnings;
2588 --cp_unevaluated_operand;
2589 }
2590
2591 // -------------------------------------------------------------------------- //
2592 // Tentative Parsing
2593
2594 /* Returns nonzero if we are parsing tentatively. */
2595
2596 static inline bool
2597 cp_parser_parsing_tentatively (cp_parser* parser)
2598 {
2599 return parser->context->next != NULL;
2600 }
2601
2602 /* Returns nonzero if TOKEN is a string literal. */
2603
2604 static bool
2605 cp_parser_is_pure_string_literal (cp_token* token)
2606 {
2607 return (token->type == CPP_STRING ||
2608 token->type == CPP_STRING16 ||
2609 token->type == CPP_STRING32 ||
2610 token->type == CPP_WSTRING ||
2611 token->type == CPP_UTF8STRING);
2612 }
2613
2614 /* Returns nonzero if TOKEN is a string literal
2615 of a user-defined string literal. */
2616
2617 static bool
2618 cp_parser_is_string_literal (cp_token* token)
2619 {
2620 return (cp_parser_is_pure_string_literal (token) ||
2621 token->type == CPP_STRING_USERDEF ||
2622 token->type == CPP_STRING16_USERDEF ||
2623 token->type == CPP_STRING32_USERDEF ||
2624 token->type == CPP_WSTRING_USERDEF ||
2625 token->type == CPP_UTF8STRING_USERDEF);
2626 }
2627
2628 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2629
2630 static bool
2631 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2632 {
2633 return token->keyword == keyword;
2634 }
2635
2636 /* If not parsing tentatively, issue a diagnostic of the form
2637 FILE:LINE: MESSAGE before TOKEN
2638 where TOKEN is the next token in the input stream. MESSAGE
2639 (specified by the caller) is usually of the form "expected
2640 OTHER-TOKEN". */
2641
2642 static void
2643 cp_parser_error (cp_parser* parser, const char* gmsgid)
2644 {
2645 if (!cp_parser_simulate_error (parser))
2646 {
2647 cp_token *token = cp_lexer_peek_token (parser->lexer);
2648 /* This diagnostic makes more sense if it is tagged to the line
2649 of the token we just peeked at. */
2650 cp_lexer_set_source_position_from_token (token);
2651
2652 if (token->type == CPP_PRAGMA)
2653 {
2654 error_at (token->location,
2655 "%<#pragma%> is not allowed here");
2656 cp_parser_skip_to_pragma_eol (parser, token);
2657 return;
2658 }
2659
2660 c_parse_error (gmsgid,
2661 /* Because c_parser_error does not understand
2662 CPP_KEYWORD, keywords are treated like
2663 identifiers. */
2664 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2665 token->u.value, token->flags);
2666 }
2667 }
2668
2669 /* Issue an error about name-lookup failing. NAME is the
2670 IDENTIFIER_NODE DECL is the result of
2671 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2672 the thing that we hoped to find. */
2673
2674 static void
2675 cp_parser_name_lookup_error (cp_parser* parser,
2676 tree name,
2677 tree decl,
2678 name_lookup_error desired,
2679 location_t location)
2680 {
2681 /* If name lookup completely failed, tell the user that NAME was not
2682 declared. */
2683 if (decl == error_mark_node)
2684 {
2685 if (parser->scope && parser->scope != global_namespace)
2686 error_at (location, "%<%E::%E%> has not been declared",
2687 parser->scope, name);
2688 else if (parser->scope == global_namespace)
2689 error_at (location, "%<::%E%> has not been declared", name);
2690 else if (parser->object_scope
2691 && !CLASS_TYPE_P (parser->object_scope))
2692 error_at (location, "request for member %qE in non-class type %qT",
2693 name, parser->object_scope);
2694 else if (parser->object_scope)
2695 error_at (location, "%<%T::%E%> has not been declared",
2696 parser->object_scope, name);
2697 else
2698 error_at (location, "%qE has not been declared", name);
2699 }
2700 else if (parser->scope && parser->scope != global_namespace)
2701 {
2702 switch (desired)
2703 {
2704 case NLE_TYPE:
2705 error_at (location, "%<%E::%E%> is not a type",
2706 parser->scope, name);
2707 break;
2708 case NLE_CXX98:
2709 error_at (location, "%<%E::%E%> is not a class or namespace",
2710 parser->scope, name);
2711 break;
2712 case NLE_NOT_CXX98:
2713 error_at (location,
2714 "%<%E::%E%> is not a class, namespace, or enumeration",
2715 parser->scope, name);
2716 break;
2717 default:
2718 gcc_unreachable ();
2719
2720 }
2721 }
2722 else if (parser->scope == global_namespace)
2723 {
2724 switch (desired)
2725 {
2726 case NLE_TYPE:
2727 error_at (location, "%<::%E%> is not a type", name);
2728 break;
2729 case NLE_CXX98:
2730 error_at (location, "%<::%E%> is not a class or namespace", name);
2731 break;
2732 case NLE_NOT_CXX98:
2733 error_at (location,
2734 "%<::%E%> is not a class, namespace, or enumeration",
2735 name);
2736 break;
2737 default:
2738 gcc_unreachable ();
2739 }
2740 }
2741 else
2742 {
2743 switch (desired)
2744 {
2745 case NLE_TYPE:
2746 error_at (location, "%qE is not a type", name);
2747 break;
2748 case NLE_CXX98:
2749 error_at (location, "%qE is not a class or namespace", name);
2750 break;
2751 case NLE_NOT_CXX98:
2752 error_at (location,
2753 "%qE is not a class, namespace, or enumeration", name);
2754 break;
2755 default:
2756 gcc_unreachable ();
2757 }
2758 }
2759 }
2760
2761 /* If we are parsing tentatively, remember that an error has occurred
2762 during this tentative parse. Returns true if the error was
2763 simulated; false if a message should be issued by the caller. */
2764
2765 static bool
2766 cp_parser_simulate_error (cp_parser* parser)
2767 {
2768 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2769 {
2770 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2771 return true;
2772 }
2773 return false;
2774 }
2775
2776 /* This function is called when a type is defined. If type
2777 definitions are forbidden at this point, an error message is
2778 issued. */
2779
2780 static bool
2781 cp_parser_check_type_definition (cp_parser* parser)
2782 {
2783 /* If types are forbidden here, issue a message. */
2784 if (parser->type_definition_forbidden_message)
2785 {
2786 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2787 in the message need to be interpreted. */
2788 error (parser->type_definition_forbidden_message);
2789 return false;
2790 }
2791 return true;
2792 }
2793
2794 /* This function is called when the DECLARATOR is processed. The TYPE
2795 was a type defined in the decl-specifiers. If it is invalid to
2796 define a type in the decl-specifiers for DECLARATOR, an error is
2797 issued. TYPE_LOCATION is the location of TYPE and is used
2798 for error reporting. */
2799
2800 static void
2801 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2802 tree type, location_t type_location)
2803 {
2804 /* [dcl.fct] forbids type definitions in return types.
2805 Unfortunately, it's not easy to know whether or not we are
2806 processing a return type until after the fact. */
2807 while (declarator
2808 && (declarator->kind == cdk_pointer
2809 || declarator->kind == cdk_reference
2810 || declarator->kind == cdk_ptrmem))
2811 declarator = declarator->declarator;
2812 if (declarator
2813 && declarator->kind == cdk_function)
2814 {
2815 error_at (type_location,
2816 "new types may not be defined in a return type");
2817 inform (type_location,
2818 "(perhaps a semicolon is missing after the definition of %qT)",
2819 type);
2820 }
2821 }
2822
2823 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2824 "<" in any valid C++ program. If the next token is indeed "<",
2825 issue a message warning the user about what appears to be an
2826 invalid attempt to form a template-id. LOCATION is the location
2827 of the type-specifier (TYPE) */
2828
2829 static void
2830 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2831 tree type,
2832 enum tag_types tag_type,
2833 location_t location)
2834 {
2835 cp_token_position start = 0;
2836
2837 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2838 {
2839 if (TYPE_P (type))
2840 error_at (location, "%qT is not a template", type);
2841 else if (identifier_p (type))
2842 {
2843 if (tag_type != none_type)
2844 error_at (location, "%qE is not a class template", type);
2845 else
2846 error_at (location, "%qE is not a template", type);
2847 }
2848 else
2849 error_at (location, "invalid template-id");
2850 /* Remember the location of the invalid "<". */
2851 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2852 start = cp_lexer_token_position (parser->lexer, true);
2853 /* Consume the "<". */
2854 cp_lexer_consume_token (parser->lexer);
2855 /* Parse the template arguments. */
2856 cp_parser_enclosed_template_argument_list (parser);
2857 /* Permanently remove the invalid template arguments so that
2858 this error message is not issued again. */
2859 if (start)
2860 cp_lexer_purge_tokens_after (parser->lexer, start);
2861 }
2862 }
2863
2864 /* If parsing an integral constant-expression, issue an error message
2865 about the fact that THING appeared and return true. Otherwise,
2866 return false. In either case, set
2867 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2868
2869 static bool
2870 cp_parser_non_integral_constant_expression (cp_parser *parser,
2871 non_integral_constant thing)
2872 {
2873 parser->non_integral_constant_expression_p = true;
2874 if (parser->integral_constant_expression_p)
2875 {
2876 if (!parser->allow_non_integral_constant_expression_p)
2877 {
2878 const char *msg = NULL;
2879 switch (thing)
2880 {
2881 case NIC_FLOAT:
2882 error ("floating-point literal "
2883 "cannot appear in a constant-expression");
2884 return true;
2885 case NIC_CAST:
2886 error ("a cast to a type other than an integral or "
2887 "enumeration type cannot appear in a "
2888 "constant-expression");
2889 return true;
2890 case NIC_TYPEID:
2891 error ("%<typeid%> operator "
2892 "cannot appear in a constant-expression");
2893 return true;
2894 case NIC_NCC:
2895 error ("non-constant compound literals "
2896 "cannot appear in a constant-expression");
2897 return true;
2898 case NIC_FUNC_CALL:
2899 error ("a function call "
2900 "cannot appear in a constant-expression");
2901 return true;
2902 case NIC_INC:
2903 error ("an increment "
2904 "cannot appear in a constant-expression");
2905 return true;
2906 case NIC_DEC:
2907 error ("an decrement "
2908 "cannot appear in a constant-expression");
2909 return true;
2910 case NIC_ARRAY_REF:
2911 error ("an array reference "
2912 "cannot appear in a constant-expression");
2913 return true;
2914 case NIC_ADDR_LABEL:
2915 error ("the address of a label "
2916 "cannot appear in a constant-expression");
2917 return true;
2918 case NIC_OVERLOADED:
2919 error ("calls to overloaded operators "
2920 "cannot appear in a constant-expression");
2921 return true;
2922 case NIC_ASSIGNMENT:
2923 error ("an assignment cannot appear in a constant-expression");
2924 return true;
2925 case NIC_COMMA:
2926 error ("a comma operator "
2927 "cannot appear in a constant-expression");
2928 return true;
2929 case NIC_CONSTRUCTOR:
2930 error ("a call to a constructor "
2931 "cannot appear in a constant-expression");
2932 return true;
2933 case NIC_TRANSACTION:
2934 error ("a transaction expression "
2935 "cannot appear in a constant-expression");
2936 return true;
2937 case NIC_THIS:
2938 msg = "this";
2939 break;
2940 case NIC_FUNC_NAME:
2941 msg = "__FUNCTION__";
2942 break;
2943 case NIC_PRETTY_FUNC:
2944 msg = "__PRETTY_FUNCTION__";
2945 break;
2946 case NIC_C99_FUNC:
2947 msg = "__func__";
2948 break;
2949 case NIC_VA_ARG:
2950 msg = "va_arg";
2951 break;
2952 case NIC_ARROW:
2953 msg = "->";
2954 break;
2955 case NIC_POINT:
2956 msg = ".";
2957 break;
2958 case NIC_STAR:
2959 msg = "*";
2960 break;
2961 case NIC_ADDR:
2962 msg = "&";
2963 break;
2964 case NIC_PREINCREMENT:
2965 msg = "++";
2966 break;
2967 case NIC_PREDECREMENT:
2968 msg = "--";
2969 break;
2970 case NIC_NEW:
2971 msg = "new";
2972 break;
2973 case NIC_DEL:
2974 msg = "delete";
2975 break;
2976 default:
2977 gcc_unreachable ();
2978 }
2979 if (msg)
2980 error ("%qs cannot appear in a constant-expression", msg);
2981 return true;
2982 }
2983 }
2984 return false;
2985 }
2986
2987 /* Emit a diagnostic for an invalid type name. This function commits
2988 to the current active tentative parse, if any. (Otherwise, the
2989 problematic construct might be encountered again later, resulting
2990 in duplicate error messages.) LOCATION is the location of ID. */
2991
2992 static void
2993 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2994 location_t location)
2995 {
2996 tree decl, ambiguous_decls;
2997 cp_parser_commit_to_tentative_parse (parser);
2998 /* Try to lookup the identifier. */
2999 decl = cp_parser_lookup_name (parser, id, none_type,
3000 /*is_template=*/false,
3001 /*is_namespace=*/false,
3002 /*check_dependency=*/true,
3003 &ambiguous_decls, location);
3004 if (ambiguous_decls)
3005 /* If the lookup was ambiguous, an error will already have
3006 been issued. */
3007 return;
3008 /* If the lookup found a template-name, it means that the user forgot
3009 to specify an argument list. Emit a useful error message. */
3010 if (DECL_TYPE_TEMPLATE_P (decl))
3011 {
3012 error_at (location,
3013 "invalid use of template-name %qE without an argument list",
3014 decl);
3015 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3016 }
3017 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3018 error_at (location, "invalid use of destructor %qD as a type", id);
3019 else if (TREE_CODE (decl) == TYPE_DECL)
3020 /* Something like 'unsigned A a;' */
3021 error_at (location, "invalid combination of multiple type-specifiers");
3022 else if (!parser->scope)
3023 {
3024 /* Issue an error message. */
3025 error_at (location, "%qE does not name a type", id);
3026 /* If we're in a template class, it's possible that the user was
3027 referring to a type from a base class. For example:
3028
3029 template <typename T> struct A { typedef T X; };
3030 template <typename T> struct B : public A<T> { X x; };
3031
3032 The user should have said "typename A<T>::X". */
3033 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3034 inform (location, "C++11 %<constexpr%> only available with "
3035 "-std=c++11 or -std=gnu++11");
3036 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3037 inform (location, "C++11 %<noexcept%> only available with "
3038 "-std=c++11 or -std=gnu++11");
3039 else if (cxx_dialect < cxx11
3040 && TREE_CODE (id) == IDENTIFIER_NODE
3041 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3042 inform (location, "C++11 %<thread_local%> only available with "
3043 "-std=c++11 or -std=gnu++11");
3044 else if (processing_template_decl && current_class_type
3045 && TYPE_BINFO (current_class_type))
3046 {
3047 tree b;
3048
3049 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3050 b;
3051 b = TREE_CHAIN (b))
3052 {
3053 tree base_type = BINFO_TYPE (b);
3054 if (CLASS_TYPE_P (base_type)
3055 && dependent_type_p (base_type))
3056 {
3057 tree field;
3058 /* Go from a particular instantiation of the
3059 template (which will have an empty TYPE_FIELDs),
3060 to the main version. */
3061 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3062 for (field = TYPE_FIELDS (base_type);
3063 field;
3064 field = DECL_CHAIN (field))
3065 if (TREE_CODE (field) == TYPE_DECL
3066 && DECL_NAME (field) == id)
3067 {
3068 inform (location,
3069 "(perhaps %<typename %T::%E%> was intended)",
3070 BINFO_TYPE (b), id);
3071 break;
3072 }
3073 if (field)
3074 break;
3075 }
3076 }
3077 }
3078 }
3079 /* Here we diagnose qualified-ids where the scope is actually correct,
3080 but the identifier does not resolve to a valid type name. */
3081 else if (parser->scope != error_mark_node)
3082 {
3083 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3084 {
3085 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3086 error_at (location_of (id),
3087 "%qE in namespace %qE does not name a template type",
3088 id, parser->scope);
3089 else
3090 error_at (location_of (id),
3091 "%qE in namespace %qE does not name a type",
3092 id, parser->scope);
3093 if (DECL_P (decl))
3094 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3095 }
3096 else if (CLASS_TYPE_P (parser->scope)
3097 && constructor_name_p (id, parser->scope))
3098 {
3099 /* A<T>::A<T>() */
3100 error_at (location, "%<%T::%E%> names the constructor, not"
3101 " the type", parser->scope, id);
3102 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3103 error_at (location, "and %qT has no template constructors",
3104 parser->scope);
3105 }
3106 else if (TYPE_P (parser->scope)
3107 && dependent_scope_p (parser->scope))
3108 error_at (location, "need %<typename%> before %<%T::%E%> because "
3109 "%qT is a dependent scope",
3110 parser->scope, id, parser->scope);
3111 else if (TYPE_P (parser->scope))
3112 {
3113 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3114 error_at (location_of (id),
3115 "%qE in %q#T does not name a template type",
3116 id, parser->scope);
3117 else
3118 error_at (location_of (id),
3119 "%qE in %q#T does not name a type",
3120 id, parser->scope);
3121 if (DECL_P (decl))
3122 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3123 }
3124 else
3125 gcc_unreachable ();
3126 }
3127 }
3128
3129 /* Check for a common situation where a type-name should be present,
3130 but is not, and issue a sensible error message. Returns true if an
3131 invalid type-name was detected.
3132
3133 The situation handled by this function are variable declarations of the
3134 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3135 Usually, `ID' should name a type, but if we got here it means that it
3136 does not. We try to emit the best possible error message depending on
3137 how exactly the id-expression looks like. */
3138
3139 static bool
3140 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3141 {
3142 tree id;
3143 cp_token *token = cp_lexer_peek_token (parser->lexer);
3144
3145 /* Avoid duplicate error about ambiguous lookup. */
3146 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3147 {
3148 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3149 if (next->type == CPP_NAME && next->error_reported)
3150 goto out;
3151 }
3152
3153 cp_parser_parse_tentatively (parser);
3154 id = cp_parser_id_expression (parser,
3155 /*template_keyword_p=*/false,
3156 /*check_dependency_p=*/true,
3157 /*template_p=*/NULL,
3158 /*declarator_p=*/true,
3159 /*optional_p=*/false);
3160 /* If the next token is a (, this is a function with no explicit return
3161 type, i.e. constructor, destructor or conversion op. */
3162 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3163 || TREE_CODE (id) == TYPE_DECL)
3164 {
3165 cp_parser_abort_tentative_parse (parser);
3166 return false;
3167 }
3168 if (!cp_parser_parse_definitely (parser))
3169 return false;
3170
3171 /* Emit a diagnostic for the invalid type. */
3172 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3173 out:
3174 /* If we aren't in the middle of a declarator (i.e. in a
3175 parameter-declaration-clause), skip to the end of the declaration;
3176 there's no point in trying to process it. */
3177 if (!parser->in_declarator_p)
3178 cp_parser_skip_to_end_of_block_or_statement (parser);
3179 return true;
3180 }
3181
3182 /* Consume tokens up to, and including, the next non-nested closing `)'.
3183 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3184 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3185 found an unnested token of that type. */
3186
3187 static int
3188 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3189 bool recovering,
3190 cpp_ttype or_ttype,
3191 bool consume_paren)
3192 {
3193 unsigned paren_depth = 0;
3194 unsigned brace_depth = 0;
3195 unsigned square_depth = 0;
3196
3197 if (recovering && or_ttype == CPP_EOF
3198 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3199 return 0;
3200
3201 while (true)
3202 {
3203 cp_token * token = cp_lexer_peek_token (parser->lexer);
3204
3205 /* Have we found what we're looking for before the closing paren? */
3206 if (token->type == or_ttype && or_ttype != CPP_EOF
3207 && !brace_depth && !paren_depth && !square_depth)
3208 return -1;
3209
3210 switch (token->type)
3211 {
3212 case CPP_EOF:
3213 case CPP_PRAGMA_EOL:
3214 /* If we've run out of tokens, then there is no closing `)'. */
3215 return 0;
3216
3217 /* This is good for lambda expression capture-lists. */
3218 case CPP_OPEN_SQUARE:
3219 ++square_depth;
3220 break;
3221 case CPP_CLOSE_SQUARE:
3222 if (!square_depth--)
3223 return 0;
3224 break;
3225
3226 case CPP_SEMICOLON:
3227 /* This matches the processing in skip_to_end_of_statement. */
3228 if (!brace_depth)
3229 return 0;
3230 break;
3231
3232 case CPP_OPEN_BRACE:
3233 ++brace_depth;
3234 break;
3235 case CPP_CLOSE_BRACE:
3236 if (!brace_depth--)
3237 return 0;
3238 break;
3239
3240 case CPP_OPEN_PAREN:
3241 if (!brace_depth)
3242 ++paren_depth;
3243 break;
3244
3245 case CPP_CLOSE_PAREN:
3246 if (!brace_depth && !paren_depth--)
3247 {
3248 if (consume_paren)
3249 cp_lexer_consume_token (parser->lexer);
3250 return 1;
3251 }
3252 break;
3253
3254 default:
3255 break;
3256 }
3257
3258 /* Consume the token. */
3259 cp_lexer_consume_token (parser->lexer);
3260 }
3261 }
3262
3263 /* Consume tokens up to, and including, the next non-nested closing `)'.
3264 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3265 are doing error recovery. Returns -1 if OR_COMMA is true and we
3266 found an unnested token of that type. */
3267
3268 static int
3269 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3270 bool recovering,
3271 bool or_comma,
3272 bool consume_paren)
3273 {
3274 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3275 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3276 ttype, consume_paren);
3277 }
3278
3279 /* Consume tokens until we reach the end of the current statement.
3280 Normally, that will be just before consuming a `;'. However, if a
3281 non-nested `}' comes first, then we stop before consuming that. */
3282
3283 static void
3284 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3285 {
3286 unsigned nesting_depth = 0;
3287
3288 /* Unwind generic function template scope if necessary. */
3289 if (parser->fully_implicit_function_template_p)
3290 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3291
3292 while (true)
3293 {
3294 cp_token *token = cp_lexer_peek_token (parser->lexer);
3295
3296 switch (token->type)
3297 {
3298 case CPP_EOF:
3299 case CPP_PRAGMA_EOL:
3300 /* If we've run out of tokens, stop. */
3301 return;
3302
3303 case CPP_SEMICOLON:
3304 /* If the next token is a `;', we have reached the end of the
3305 statement. */
3306 if (!nesting_depth)
3307 return;
3308 break;
3309
3310 case CPP_CLOSE_BRACE:
3311 /* If this is a non-nested '}', stop before consuming it.
3312 That way, when confronted with something like:
3313
3314 { 3 + }
3315
3316 we stop before consuming the closing '}', even though we
3317 have not yet reached a `;'. */
3318 if (nesting_depth == 0)
3319 return;
3320
3321 /* If it is the closing '}' for a block that we have
3322 scanned, stop -- but only after consuming the token.
3323 That way given:
3324
3325 void f g () { ... }
3326 typedef int I;
3327
3328 we will stop after the body of the erroneously declared
3329 function, but before consuming the following `typedef'
3330 declaration. */
3331 if (--nesting_depth == 0)
3332 {
3333 cp_lexer_consume_token (parser->lexer);
3334 return;
3335 }
3336
3337 case CPP_OPEN_BRACE:
3338 ++nesting_depth;
3339 break;
3340
3341 default:
3342 break;
3343 }
3344
3345 /* Consume the token. */
3346 cp_lexer_consume_token (parser->lexer);
3347 }
3348 }
3349
3350 /* This function is called at the end of a statement or declaration.
3351 If the next token is a semicolon, it is consumed; otherwise, error
3352 recovery is attempted. */
3353
3354 static void
3355 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3356 {
3357 /* Look for the trailing `;'. */
3358 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3359 {
3360 /* If there is additional (erroneous) input, skip to the end of
3361 the statement. */
3362 cp_parser_skip_to_end_of_statement (parser);
3363 /* If the next token is now a `;', consume it. */
3364 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3365 cp_lexer_consume_token (parser->lexer);
3366 }
3367 }
3368
3369 /* Skip tokens until we have consumed an entire block, or until we
3370 have consumed a non-nested `;'. */
3371
3372 static void
3373 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3374 {
3375 int nesting_depth = 0;
3376
3377 /* Unwind generic function template scope if necessary. */
3378 if (parser->fully_implicit_function_template_p)
3379 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3380
3381 while (nesting_depth >= 0)
3382 {
3383 cp_token *token = cp_lexer_peek_token (parser->lexer);
3384
3385 switch (token->type)
3386 {
3387 case CPP_EOF:
3388 case CPP_PRAGMA_EOL:
3389 /* If we've run out of tokens, stop. */
3390 return;
3391
3392 case CPP_SEMICOLON:
3393 /* Stop if this is an unnested ';'. */
3394 if (!nesting_depth)
3395 nesting_depth = -1;
3396 break;
3397
3398 case CPP_CLOSE_BRACE:
3399 /* Stop if this is an unnested '}', or closes the outermost
3400 nesting level. */
3401 nesting_depth--;
3402 if (nesting_depth < 0)
3403 return;
3404 if (!nesting_depth)
3405 nesting_depth = -1;
3406 break;
3407
3408 case CPP_OPEN_BRACE:
3409 /* Nest. */
3410 nesting_depth++;
3411 break;
3412
3413 default:
3414 break;
3415 }
3416
3417 /* Consume the token. */
3418 cp_lexer_consume_token (parser->lexer);
3419 }
3420 }
3421
3422 /* Skip tokens until a non-nested closing curly brace is the next
3423 token, or there are no more tokens. Return true in the first case,
3424 false otherwise. */
3425
3426 static bool
3427 cp_parser_skip_to_closing_brace (cp_parser *parser)
3428 {
3429 unsigned nesting_depth = 0;
3430
3431 while (true)
3432 {
3433 cp_token *token = cp_lexer_peek_token (parser->lexer);
3434
3435 switch (token->type)
3436 {
3437 case CPP_EOF:
3438 case CPP_PRAGMA_EOL:
3439 /* If we've run out of tokens, stop. */
3440 return false;
3441
3442 case CPP_CLOSE_BRACE:
3443 /* If the next token is a non-nested `}', then we have reached
3444 the end of the current block. */
3445 if (nesting_depth-- == 0)
3446 return true;
3447 break;
3448
3449 case CPP_OPEN_BRACE:
3450 /* If it the next token is a `{', then we are entering a new
3451 block. Consume the entire block. */
3452 ++nesting_depth;
3453 break;
3454
3455 default:
3456 break;
3457 }
3458
3459 /* Consume the token. */
3460 cp_lexer_consume_token (parser->lexer);
3461 }
3462 }
3463
3464 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3465 parameter is the PRAGMA token, allowing us to purge the entire pragma
3466 sequence. */
3467
3468 static void
3469 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3470 {
3471 cp_token *token;
3472
3473 parser->lexer->in_pragma = false;
3474
3475 do
3476 token = cp_lexer_consume_token (parser->lexer);
3477 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3478
3479 /* Ensure that the pragma is not parsed again. */
3480 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3481 }
3482
3483 /* Require pragma end of line, resyncing with it as necessary. The
3484 arguments are as for cp_parser_skip_to_pragma_eol. */
3485
3486 static void
3487 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3488 {
3489 parser->lexer->in_pragma = false;
3490 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3491 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3492 }
3493
3494 /* This is a simple wrapper around make_typename_type. When the id is
3495 an unresolved identifier node, we can provide a superior diagnostic
3496 using cp_parser_diagnose_invalid_type_name. */
3497
3498 static tree
3499 cp_parser_make_typename_type (cp_parser *parser, tree id,
3500 location_t id_location)
3501 {
3502 tree result;
3503 if (identifier_p (id))
3504 {
3505 result = make_typename_type (parser->scope, id, typename_type,
3506 /*complain=*/tf_none);
3507 if (result == error_mark_node)
3508 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3509 return result;
3510 }
3511 return make_typename_type (parser->scope, id, typename_type, tf_error);
3512 }
3513
3514 /* This is a wrapper around the
3515 make_{pointer,ptrmem,reference}_declarator functions that decides
3516 which one to call based on the CODE and CLASS_TYPE arguments. The
3517 CODE argument should be one of the values returned by
3518 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3519 appertain to the pointer or reference. */
3520
3521 static cp_declarator *
3522 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3523 cp_cv_quals cv_qualifiers,
3524 cp_declarator *target,
3525 tree attributes)
3526 {
3527 if (code == ERROR_MARK)
3528 return cp_error_declarator;
3529
3530 if (code == INDIRECT_REF)
3531 if (class_type == NULL_TREE)
3532 return make_pointer_declarator (cv_qualifiers, target, attributes);
3533 else
3534 return make_ptrmem_declarator (cv_qualifiers, class_type,
3535 target, attributes);
3536 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3537 return make_reference_declarator (cv_qualifiers, target,
3538 false, attributes);
3539 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3540 return make_reference_declarator (cv_qualifiers, target,
3541 true, attributes);
3542 gcc_unreachable ();
3543 }
3544
3545 /* Create a new C++ parser. */
3546
3547 static cp_parser *
3548 cp_parser_new (void)
3549 {
3550 cp_parser *parser;
3551 cp_lexer *lexer;
3552 unsigned i;
3553
3554 /* cp_lexer_new_main is called before doing GC allocation because
3555 cp_lexer_new_main might load a PCH file. */
3556 lexer = cp_lexer_new_main ();
3557
3558 /* Initialize the binops_by_token so that we can get the tree
3559 directly from the token. */
3560 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3561 binops_by_token[binops[i].token_type] = binops[i];
3562
3563 parser = ggc_cleared_alloc<cp_parser> ();
3564 parser->lexer = lexer;
3565 parser->context = cp_parser_context_new (NULL);
3566
3567 /* For now, we always accept GNU extensions. */
3568 parser->allow_gnu_extensions_p = 1;
3569
3570 /* The `>' token is a greater-than operator, not the end of a
3571 template-id. */
3572 parser->greater_than_is_operator_p = true;
3573
3574 parser->default_arg_ok_p = true;
3575
3576 /* We are not parsing a constant-expression. */
3577 parser->integral_constant_expression_p = false;
3578 parser->allow_non_integral_constant_expression_p = false;
3579 parser->non_integral_constant_expression_p = false;
3580
3581 /* Local variable names are not forbidden. */
3582 parser->local_variables_forbidden_p = false;
3583
3584 /* We are not processing an `extern "C"' declaration. */
3585 parser->in_unbraced_linkage_specification_p = false;
3586
3587 /* We are not processing a declarator. */
3588 parser->in_declarator_p = false;
3589
3590 /* We are not processing a template-argument-list. */
3591 parser->in_template_argument_list_p = false;
3592
3593 /* We are not in an iteration statement. */
3594 parser->in_statement = 0;
3595
3596 /* We are not in a switch statement. */
3597 parser->in_switch_statement_p = false;
3598
3599 /* We are not parsing a type-id inside an expression. */
3600 parser->in_type_id_in_expr_p = false;
3601
3602 /* Declarations aren't implicitly extern "C". */
3603 parser->implicit_extern_c = false;
3604
3605 /* String literals should be translated to the execution character set. */
3606 parser->translate_strings_p = true;
3607
3608 /* We are not parsing a function body. */
3609 parser->in_function_body = false;
3610
3611 /* We can correct until told otherwise. */
3612 parser->colon_corrects_to_scope_p = true;
3613
3614 /* The unparsed function queue is empty. */
3615 push_unparsed_function_queues (parser);
3616
3617 /* There are no classes being defined. */
3618 parser->num_classes_being_defined = 0;
3619
3620 /* No template parameters apply. */
3621 parser->num_template_parameter_lists = 0;
3622
3623 /* Not declaring an implicit function template. */
3624 parser->auto_is_implicit_function_template_parm_p = false;
3625 parser->fully_implicit_function_template_p = false;
3626 parser->implicit_template_parms = 0;
3627 parser->implicit_template_scope = 0;
3628
3629 /* Active OpenACC routine clauses. */
3630 parser->oacc_routine = NULL;
3631
3632 /* Allow constrained-type-specifiers. */
3633 parser->prevent_constrained_type_specifiers = 0;
3634
3635 return parser;
3636 }
3637
3638 /* Create a cp_lexer structure which will emit the tokens in CACHE
3639 and push it onto the parser's lexer stack. This is used for delayed
3640 parsing of in-class method bodies and default arguments, and should
3641 not be confused with tentative parsing. */
3642 static void
3643 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3644 {
3645 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3646 lexer->next = parser->lexer;
3647 parser->lexer = lexer;
3648
3649 /* Move the current source position to that of the first token in the
3650 new lexer. */
3651 cp_lexer_set_source_position_from_token (lexer->next_token);
3652 }
3653
3654 /* Pop the top lexer off the parser stack. This is never used for the
3655 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3656 static void
3657 cp_parser_pop_lexer (cp_parser *parser)
3658 {
3659 cp_lexer *lexer = parser->lexer;
3660 parser->lexer = lexer->next;
3661 cp_lexer_destroy (lexer);
3662
3663 /* Put the current source position back where it was before this
3664 lexer was pushed. */
3665 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3666 }
3667
3668 /* Lexical conventions [gram.lex] */
3669
3670 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3671 identifier. */
3672
3673 static tree
3674 cp_parser_identifier (cp_parser* parser)
3675 {
3676 cp_token *token;
3677
3678 /* Look for the identifier. */
3679 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3680 /* Return the value. */
3681 return token ? token->u.value : error_mark_node;
3682 }
3683
3684 /* Parse a sequence of adjacent string constants. Returns a
3685 TREE_STRING representing the combined, nul-terminated string
3686 constant. If TRANSLATE is true, translate the string to the
3687 execution character set. If WIDE_OK is true, a wide string is
3688 invalid here.
3689
3690 C++98 [lex.string] says that if a narrow string literal token is
3691 adjacent to a wide string literal token, the behavior is undefined.
3692 However, C99 6.4.5p4 says that this results in a wide string literal.
3693 We follow C99 here, for consistency with the C front end.
3694
3695 This code is largely lifted from lex_string() in c-lex.c.
3696
3697 FUTURE: ObjC++ will need to handle @-strings here. */
3698 static tree
3699 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3700 bool lookup_udlit = true)
3701 {
3702 tree value;
3703 size_t count;
3704 struct obstack str_ob;
3705 cpp_string str, istr, *strs;
3706 cp_token *tok;
3707 enum cpp_ttype type, curr_type;
3708 int have_suffix_p = 0;
3709 tree string_tree;
3710 tree suffix_id = NULL_TREE;
3711 bool curr_tok_is_userdef_p = false;
3712
3713 tok = cp_lexer_peek_token (parser->lexer);
3714 if (!cp_parser_is_string_literal (tok))
3715 {
3716 cp_parser_error (parser, "expected string-literal");
3717 return error_mark_node;
3718 }
3719
3720 if (cpp_userdef_string_p (tok->type))
3721 {
3722 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3723 curr_type = cpp_userdef_string_remove_type (tok->type);
3724 curr_tok_is_userdef_p = true;
3725 }
3726 else
3727 {
3728 string_tree = tok->u.value;
3729 curr_type = tok->type;
3730 }
3731 type = curr_type;
3732
3733 /* Try to avoid the overhead of creating and destroying an obstack
3734 for the common case of just one string. */
3735 if (!cp_parser_is_string_literal
3736 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3737 {
3738 cp_lexer_consume_token (parser->lexer);
3739
3740 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3741 str.len = TREE_STRING_LENGTH (string_tree);
3742 count = 1;
3743
3744 if (curr_tok_is_userdef_p)
3745 {
3746 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3747 have_suffix_p = 1;
3748 curr_type = cpp_userdef_string_remove_type (tok->type);
3749 }
3750 else
3751 curr_type = tok->type;
3752
3753 strs = &str;
3754 }
3755 else
3756 {
3757 gcc_obstack_init (&str_ob);
3758 count = 0;
3759
3760 do
3761 {
3762 cp_lexer_consume_token (parser->lexer);
3763 count++;
3764 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3765 str.len = TREE_STRING_LENGTH (string_tree);
3766
3767 if (curr_tok_is_userdef_p)
3768 {
3769 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3770 if (have_suffix_p == 0)
3771 {
3772 suffix_id = curr_suffix_id;
3773 have_suffix_p = 1;
3774 }
3775 else if (have_suffix_p == 1
3776 && curr_suffix_id != suffix_id)
3777 {
3778 error ("inconsistent user-defined literal suffixes"
3779 " %qD and %qD in string literal",
3780 suffix_id, curr_suffix_id);
3781 have_suffix_p = -1;
3782 }
3783 curr_type = cpp_userdef_string_remove_type (tok->type);
3784 }
3785 else
3786 curr_type = tok->type;
3787
3788 if (type != curr_type)
3789 {
3790 if (type == CPP_STRING)
3791 type = curr_type;
3792 else if (curr_type != CPP_STRING)
3793 error_at (tok->location,
3794 "unsupported non-standard concatenation "
3795 "of string literals");
3796 }
3797
3798 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3799
3800 tok = cp_lexer_peek_token (parser->lexer);
3801 if (cpp_userdef_string_p (tok->type))
3802 {
3803 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3804 curr_type = cpp_userdef_string_remove_type (tok->type);
3805 curr_tok_is_userdef_p = true;
3806 }
3807 else
3808 {
3809 string_tree = tok->u.value;
3810 curr_type = tok->type;
3811 curr_tok_is_userdef_p = false;
3812 }
3813 }
3814 while (cp_parser_is_string_literal (tok));
3815
3816 strs = (cpp_string *) obstack_finish (&str_ob);
3817 }
3818
3819 if (type != CPP_STRING && !wide_ok)
3820 {
3821 cp_parser_error (parser, "a wide string is invalid in this context");
3822 type = CPP_STRING;
3823 }
3824
3825 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3826 (parse_in, strs, count, &istr, type))
3827 {
3828 value = build_string (istr.len, (const char *)istr.text);
3829 free (CONST_CAST (unsigned char *, istr.text));
3830
3831 switch (type)
3832 {
3833 default:
3834 case CPP_STRING:
3835 case CPP_UTF8STRING:
3836 TREE_TYPE (value) = char_array_type_node;
3837 break;
3838 case CPP_STRING16:
3839 TREE_TYPE (value) = char16_array_type_node;
3840 break;
3841 case CPP_STRING32:
3842 TREE_TYPE (value) = char32_array_type_node;
3843 break;
3844 case CPP_WSTRING:
3845 TREE_TYPE (value) = wchar_array_type_node;
3846 break;
3847 }
3848
3849 value = fix_string_type (value);
3850
3851 if (have_suffix_p)
3852 {
3853 tree literal = build_userdef_literal (suffix_id, value,
3854 OT_NONE, NULL_TREE);
3855 if (lookup_udlit)
3856 value = cp_parser_userdef_string_literal (literal);
3857 else
3858 value = literal;
3859 }
3860 }
3861 else
3862 /* cpp_interpret_string has issued an error. */
3863 value = error_mark_node;
3864
3865 if (count > 1)
3866 obstack_free (&str_ob, 0);
3867
3868 return value;
3869 }
3870
3871 /* Look up a literal operator with the name and the exact arguments. */
3872
3873 static tree
3874 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3875 {
3876 tree decl, fns;
3877 decl = lookup_name (name);
3878 if (!decl || !is_overloaded_fn (decl))
3879 return error_mark_node;
3880
3881 for (fns = decl; fns; fns = OVL_NEXT (fns))
3882 {
3883 unsigned int ix;
3884 bool found = true;
3885 tree fn = OVL_CURRENT (fns);
3886 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3887 if (parmtypes != NULL_TREE)
3888 {
3889 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3890 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3891 {
3892 tree tparm = TREE_VALUE (parmtypes);
3893 tree targ = TREE_TYPE ((*args)[ix]);
3894 bool ptr = TYPE_PTR_P (tparm);
3895 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3896 if ((ptr || arr || !same_type_p (tparm, targ))
3897 && (!ptr || !arr
3898 || !same_type_p (TREE_TYPE (tparm),
3899 TREE_TYPE (targ))))
3900 found = false;
3901 }
3902 if (found
3903 && ix == vec_safe_length (args)
3904 /* May be this should be sufficient_parms_p instead,
3905 depending on how exactly should user-defined literals
3906 work in presence of default arguments on the literal
3907 operator parameters. */
3908 && parmtypes == void_list_node)
3909 return decl;
3910 }
3911 }
3912
3913 return error_mark_node;
3914 }
3915
3916 /* Parse a user-defined char constant. Returns a call to a user-defined
3917 literal operator taking the character as an argument. */
3918
3919 static tree
3920 cp_parser_userdef_char_literal (cp_parser *parser)
3921 {
3922 cp_token *token = cp_lexer_consume_token (parser->lexer);
3923 tree literal = token->u.value;
3924 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3925 tree value = USERDEF_LITERAL_VALUE (literal);
3926 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3927 tree decl, result;
3928
3929 /* Build up a call to the user-defined operator */
3930 /* Lookup the name we got back from the id-expression. */
3931 vec<tree, va_gc> *args = make_tree_vector ();
3932 vec_safe_push (args, value);
3933 decl = lookup_literal_operator (name, args);
3934 if (!decl || decl == error_mark_node)
3935 {
3936 error ("unable to find character literal operator %qD with %qT argument",
3937 name, TREE_TYPE (value));
3938 release_tree_vector (args);
3939 return error_mark_node;
3940 }
3941 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3942 release_tree_vector (args);
3943 return result;
3944 }
3945
3946 /* A subroutine of cp_parser_userdef_numeric_literal to
3947 create a char... template parameter pack from a string node. */
3948
3949 static tree
3950 make_char_string_pack (tree value)
3951 {
3952 tree charvec;
3953 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3954 const char *str = TREE_STRING_POINTER (value);
3955 int i, len = TREE_STRING_LENGTH (value) - 1;
3956 tree argvec = make_tree_vec (1);
3957
3958 /* Fill in CHARVEC with all of the parameters. */
3959 charvec = make_tree_vec (len);
3960 for (i = 0; i < len; ++i)
3961 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3962
3963 /* Build the argument packs. */
3964 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3965 TREE_TYPE (argpack) = char_type_node;
3966
3967 TREE_VEC_ELT (argvec, 0) = argpack;
3968
3969 return argvec;
3970 }
3971
3972 /* A subroutine of cp_parser_userdef_numeric_literal to
3973 create a char... template parameter pack from a string node. */
3974
3975 static tree
3976 make_string_pack (tree value)
3977 {
3978 tree charvec;
3979 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3980 const unsigned char *str
3981 = (const unsigned char *) TREE_STRING_POINTER (value);
3982 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3983 int len = TREE_STRING_LENGTH (value) / sz - 1;
3984 tree argvec = make_tree_vec (2);
3985
3986 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3987 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3988
3989 /* First template parm is character type. */
3990 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3991
3992 /* Fill in CHARVEC with all of the parameters. */
3993 charvec = make_tree_vec (len);
3994 for (int i = 0; i < len; ++i)
3995 TREE_VEC_ELT (charvec, i)
3996 = double_int_to_tree (str_char_type_node,
3997 double_int::from_buffer (str + i * sz, sz));
3998
3999 /* Build the argument packs. */
4000 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4001 TREE_TYPE (argpack) = str_char_type_node;
4002
4003 TREE_VEC_ELT (argvec, 1) = argpack;
4004
4005 return argvec;
4006 }
4007
4008 /* Parse a user-defined numeric constant. returns a call to a user-defined
4009 literal operator. */
4010
4011 static tree
4012 cp_parser_userdef_numeric_literal (cp_parser *parser)
4013 {
4014 cp_token *token = cp_lexer_consume_token (parser->lexer);
4015 tree literal = token->u.value;
4016 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4017 tree value = USERDEF_LITERAL_VALUE (literal);
4018 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4019 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4020 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4021 tree decl, result;
4022 vec<tree, va_gc> *args;
4023
4024 /* Look for a literal operator taking the exact type of numeric argument
4025 as the literal value. */
4026 args = make_tree_vector ();
4027 vec_safe_push (args, value);
4028 decl = lookup_literal_operator (name, args);
4029 if (decl && decl != error_mark_node)
4030 {
4031 result = finish_call_expr (decl, &args, false, true,
4032 tf_warning_or_error);
4033
4034 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4035 {
4036 warning_at (token->location, OPT_Woverflow,
4037 "integer literal exceeds range of %qT type",
4038 long_long_unsigned_type_node);
4039 }
4040 else
4041 {
4042 if (overflow > 0)
4043 warning_at (token->location, OPT_Woverflow,
4044 "floating literal exceeds range of %qT type",
4045 long_double_type_node);
4046 else if (overflow < 0)
4047 warning_at (token->location, OPT_Woverflow,
4048 "floating literal truncated to zero");
4049 }
4050
4051 release_tree_vector (args);
4052 return result;
4053 }
4054 release_tree_vector (args);
4055
4056 /* If the numeric argument didn't work, look for a raw literal
4057 operator taking a const char* argument consisting of the number
4058 in string format. */
4059 args = make_tree_vector ();
4060 vec_safe_push (args, num_string);
4061 decl = lookup_literal_operator (name, args);
4062 if (decl && decl != error_mark_node)
4063 {
4064 result = finish_call_expr (decl, &args, false, true,
4065 tf_warning_or_error);
4066 release_tree_vector (args);
4067 return result;
4068 }
4069 release_tree_vector (args);
4070
4071 /* If the raw literal didn't work, look for a non-type template
4072 function with parameter pack char.... Call the function with
4073 template parameter characters representing the number. */
4074 args = make_tree_vector ();
4075 decl = lookup_literal_operator (name, args);
4076 if (decl && decl != error_mark_node)
4077 {
4078 tree tmpl_args = make_char_string_pack (num_string);
4079 decl = lookup_template_function (decl, tmpl_args);
4080 result = finish_call_expr (decl, &args, false, true,
4081 tf_warning_or_error);
4082 release_tree_vector (args);
4083 return result;
4084 }
4085
4086 release_tree_vector (args);
4087
4088 error ("unable to find numeric literal operator %qD", name);
4089 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4090 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4091 "to enable more built-in suffixes");
4092 return error_mark_node;
4093 }
4094
4095 /* Parse a user-defined string constant. Returns a call to a user-defined
4096 literal operator taking a character pointer and the length of the string
4097 as arguments. */
4098
4099 static tree
4100 cp_parser_userdef_string_literal (tree literal)
4101 {
4102 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4103 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4104 tree value = USERDEF_LITERAL_VALUE (literal);
4105 int len = TREE_STRING_LENGTH (value)
4106 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4107 tree decl, result;
4108 vec<tree, va_gc> *args;
4109
4110 /* Build up a call to the user-defined operator. */
4111 /* Lookup the name we got back from the id-expression. */
4112 args = make_tree_vector ();
4113 vec_safe_push (args, value);
4114 vec_safe_push (args, build_int_cst (size_type_node, len));
4115 decl = lookup_literal_operator (name, args);
4116
4117 if (decl && decl != error_mark_node)
4118 {
4119 result = finish_call_expr (decl, &args, false, true,
4120 tf_warning_or_error);
4121 release_tree_vector (args);
4122 return result;
4123 }
4124 release_tree_vector (args);
4125
4126 /* Look for a template function with typename parameter CharT
4127 and parameter pack CharT... Call the function with
4128 template parameter characters representing the string. */
4129 args = make_tree_vector ();
4130 decl = lookup_literal_operator (name, args);
4131 if (decl && decl != error_mark_node)
4132 {
4133 tree tmpl_args = make_string_pack (value);
4134 decl = lookup_template_function (decl, tmpl_args);
4135 result = finish_call_expr (decl, &args, false, true,
4136 tf_warning_or_error);
4137 release_tree_vector (args);
4138 return result;
4139 }
4140 release_tree_vector (args);
4141
4142 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4143 name, TREE_TYPE (value), size_type_node);
4144 return error_mark_node;
4145 }
4146
4147
4148 /* Basic concepts [gram.basic] */
4149
4150 /* Parse a translation-unit.
4151
4152 translation-unit:
4153 declaration-seq [opt]
4154
4155 Returns TRUE if all went well. */
4156
4157 static bool
4158 cp_parser_translation_unit (cp_parser* parser)
4159 {
4160 /* The address of the first non-permanent object on the declarator
4161 obstack. */
4162 static void *declarator_obstack_base;
4163
4164 bool success;
4165
4166 /* Create the declarator obstack, if necessary. */
4167 if (!cp_error_declarator)
4168 {
4169 gcc_obstack_init (&declarator_obstack);
4170 /* Create the error declarator. */
4171 cp_error_declarator = make_declarator (cdk_error);
4172 /* Create the empty parameter list. */
4173 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4174 /* Remember where the base of the declarator obstack lies. */
4175 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4176 }
4177
4178 cp_parser_declaration_seq_opt (parser);
4179
4180 /* If there are no tokens left then all went well. */
4181 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4182 {
4183 /* Get rid of the token array; we don't need it any more. */
4184 cp_lexer_destroy (parser->lexer);
4185 parser->lexer = NULL;
4186
4187 /* This file might have been a context that's implicitly extern
4188 "C". If so, pop the lang context. (Only relevant for PCH.) */
4189 if (parser->implicit_extern_c)
4190 {
4191 pop_lang_context ();
4192 parser->implicit_extern_c = false;
4193 }
4194
4195 /* Finish up. */
4196 finish_translation_unit ();
4197
4198 success = true;
4199 }
4200 else
4201 {
4202 cp_parser_error (parser, "expected declaration");
4203 success = false;
4204 }
4205
4206 /* Make sure the declarator obstack was fully cleaned up. */
4207 gcc_assert (obstack_next_free (&declarator_obstack)
4208 == declarator_obstack_base);
4209
4210 /* All went well. */
4211 return success;
4212 }
4213
4214 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4215 decltype context. */
4216
4217 static inline tsubst_flags_t
4218 complain_flags (bool decltype_p)
4219 {
4220 tsubst_flags_t complain = tf_warning_or_error;
4221 if (decltype_p)
4222 complain |= tf_decltype;
4223 return complain;
4224 }
4225
4226 /* We're about to parse a collection of statements. If we're currently
4227 parsing tentatively, set up a firewall so that any nested
4228 cp_parser_commit_to_tentative_parse won't affect the current context. */
4229
4230 static cp_token_position
4231 cp_parser_start_tentative_firewall (cp_parser *parser)
4232 {
4233 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4234 return 0;
4235
4236 cp_parser_parse_tentatively (parser);
4237 cp_parser_commit_to_topmost_tentative_parse (parser);
4238 return cp_lexer_token_position (parser->lexer, false);
4239 }
4240
4241 /* We've finished parsing the collection of statements. Wrap up the
4242 firewall and replace the relevant tokens with the parsed form. */
4243
4244 static void
4245 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4246 tree expr)
4247 {
4248 if (!start)
4249 return;
4250
4251 /* Finish the firewall level. */
4252 cp_parser_parse_definitely (parser);
4253 /* And remember the result of the parse for when we try again. */
4254 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4255 token->type = CPP_PREPARSED_EXPR;
4256 token->u.value = expr;
4257 token->keyword = RID_MAX;
4258 cp_lexer_purge_tokens_after (parser->lexer, start);
4259 }
4260
4261 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4262 enclosing parentheses. */
4263
4264 static tree
4265 cp_parser_statement_expr (cp_parser *parser)
4266 {
4267 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4268
4269 /* Consume the '('. */
4270 cp_lexer_consume_token (parser->lexer);
4271 /* Start the statement-expression. */
4272 tree expr = begin_stmt_expr ();
4273 /* Parse the compound-statement. */
4274 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4275 /* Finish up. */
4276 expr = finish_stmt_expr (expr, false);
4277 /* Consume the ')'. */
4278 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4279 cp_parser_skip_to_end_of_statement (parser);
4280
4281 cp_parser_end_tentative_firewall (parser, start, expr);
4282 return expr;
4283 }
4284
4285 /* Expressions [gram.expr] */
4286
4287 /* Parse a fold-operator.
4288
4289 fold-operator:
4290 - * / % ^ & | = < > << >>
4291 = -= *= /= %= ^= &= |= <<= >>=
4292 == != <= >= && || , .* ->*
4293
4294 This returns the tree code corresponding to the matched operator
4295 as an int. When the current token matches a compound assignment
4296 opertor, the resulting tree code is the negative value of the
4297 non-assignment operator. */
4298
4299 static int
4300 cp_parser_fold_operator (cp_token *token)
4301 {
4302 switch (token->type)
4303 {
4304 case CPP_PLUS: return PLUS_EXPR;
4305 case CPP_MINUS: return MINUS_EXPR;
4306 case CPP_MULT: return MULT_EXPR;
4307 case CPP_DIV: return TRUNC_DIV_EXPR;
4308 case CPP_MOD: return TRUNC_MOD_EXPR;
4309 case CPP_XOR: return BIT_XOR_EXPR;
4310 case CPP_AND: return BIT_AND_EXPR;
4311 case CPP_OR: return BIT_IOR_EXPR;
4312 case CPP_LSHIFT: return LSHIFT_EXPR;
4313 case CPP_RSHIFT: return RSHIFT_EXPR;
4314
4315 case CPP_EQ: return -NOP_EXPR;
4316 case CPP_PLUS_EQ: return -PLUS_EXPR;
4317 case CPP_MINUS_EQ: return -MINUS_EXPR;
4318 case CPP_MULT_EQ: return -MULT_EXPR;
4319 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4320 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4321 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4322 case CPP_AND_EQ: return -BIT_AND_EXPR;
4323 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4324 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4325 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4326
4327 case CPP_EQ_EQ: return EQ_EXPR;
4328 case CPP_NOT_EQ: return NE_EXPR;
4329 case CPP_LESS: return LT_EXPR;
4330 case CPP_GREATER: return GT_EXPR;
4331 case CPP_LESS_EQ: return LE_EXPR;
4332 case CPP_GREATER_EQ: return GE_EXPR;
4333
4334 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4335 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4336
4337 case CPP_COMMA: return COMPOUND_EXPR;
4338
4339 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4340 case CPP_DEREF_STAR: return MEMBER_REF;
4341
4342 default: return ERROR_MARK;
4343 }
4344 }
4345
4346 /* Returns true if CODE indicates a binary expression, which is not allowed in
4347 the LHS of a fold-expression. More codes will need to be added to use this
4348 function in other contexts. */
4349
4350 static bool
4351 is_binary_op (tree_code code)
4352 {
4353 switch (code)
4354 {
4355 case PLUS_EXPR:
4356 case POINTER_PLUS_EXPR:
4357 case MINUS_EXPR:
4358 case MULT_EXPR:
4359 case TRUNC_DIV_EXPR:
4360 case TRUNC_MOD_EXPR:
4361 case BIT_XOR_EXPR:
4362 case BIT_AND_EXPR:
4363 case BIT_IOR_EXPR:
4364 case LSHIFT_EXPR:
4365 case RSHIFT_EXPR:
4366
4367 case MODOP_EXPR:
4368
4369 case EQ_EXPR:
4370 case NE_EXPR:
4371 case LE_EXPR:
4372 case GE_EXPR:
4373 case LT_EXPR:
4374 case GT_EXPR:
4375
4376 case TRUTH_ANDIF_EXPR:
4377 case TRUTH_ORIF_EXPR:
4378
4379 case COMPOUND_EXPR:
4380
4381 case DOTSTAR_EXPR:
4382 case MEMBER_REF:
4383 return true;
4384
4385 default:
4386 return false;
4387 }
4388 }
4389
4390 /* If the next token is a suitable fold operator, consume it and return as
4391 the function above. */
4392
4393 static int
4394 cp_parser_fold_operator (cp_parser *parser)
4395 {
4396 cp_token* token = cp_lexer_peek_token (parser->lexer);
4397 int code = cp_parser_fold_operator (token);
4398 if (code != ERROR_MARK)
4399 cp_lexer_consume_token (parser->lexer);
4400 return code;
4401 }
4402
4403 /* Parse a fold-expression.
4404
4405 fold-expression:
4406 ( ... folding-operator cast-expression)
4407 ( cast-expression folding-operator ... )
4408 ( cast-expression folding operator ... folding-operator cast-expression)
4409
4410 Note that the '(' and ')' are matched in primary expression. */
4411
4412 static tree
4413 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4414 {
4415 cp_id_kind pidk;
4416
4417 // Left fold.
4418 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4419 {
4420 cp_lexer_consume_token (parser->lexer);
4421 int op = cp_parser_fold_operator (parser);
4422 if (op == ERROR_MARK)
4423 {
4424 cp_parser_error (parser, "expected binary operator");
4425 return error_mark_node;
4426 }
4427
4428 tree expr = cp_parser_cast_expression (parser, false, false,
4429 false, &pidk);
4430 if (expr == error_mark_node)
4431 return error_mark_node;
4432 return finish_left_unary_fold_expr (expr, op);
4433 }
4434
4435 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4436 int op = cp_parser_fold_operator (parser);
4437 if (op == ERROR_MARK)
4438 {
4439 cp_parser_error (parser, "expected binary operator");
4440 return error_mark_node;
4441 }
4442
4443 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4444 {
4445 cp_parser_error (parser, "expected ...");
4446 return error_mark_node;
4447 }
4448 cp_lexer_consume_token (parser->lexer);
4449
4450 /* The operands of a fold-expression are cast-expressions, so binary or
4451 conditional expressions are not allowed. We check this here to avoid
4452 tentative parsing. */
4453 if (is_binary_op (TREE_CODE (expr1)))
4454 error_at (location_of (expr1),
4455 "binary expression in operand of fold-expression");
4456 else if (TREE_CODE (expr1) == COND_EXPR)
4457 error_at (location_of (expr1),
4458 "conditional expression in operand of fold-expression");
4459
4460 // Right fold.
4461 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4462 return finish_right_unary_fold_expr (expr1, op);
4463
4464 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4465 {
4466 cp_parser_error (parser, "mismatched operator in fold-expression");
4467 return error_mark_node;
4468 }
4469 cp_lexer_consume_token (parser->lexer);
4470
4471 // Binary left or right fold.
4472 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4473 if (expr2 == error_mark_node)
4474 return error_mark_node;
4475 return finish_binary_fold_expr (expr1, expr2, op);
4476 }
4477
4478 /* Parse a primary-expression.
4479
4480 primary-expression:
4481 literal
4482 this
4483 ( expression )
4484 id-expression
4485 lambda-expression (C++11)
4486
4487 GNU Extensions:
4488
4489 primary-expression:
4490 ( compound-statement )
4491 __builtin_va_arg ( assignment-expression , type-id )
4492 __builtin_offsetof ( type-id , offsetof-expression )
4493
4494 C++ Extensions:
4495 __has_nothrow_assign ( type-id )
4496 __has_nothrow_constructor ( type-id )
4497 __has_nothrow_copy ( type-id )
4498 __has_trivial_assign ( type-id )
4499 __has_trivial_constructor ( type-id )
4500 __has_trivial_copy ( type-id )
4501 __has_trivial_destructor ( type-id )
4502 __has_virtual_destructor ( type-id )
4503 __is_abstract ( type-id )
4504 __is_base_of ( type-id , type-id )
4505 __is_class ( type-id )
4506 __is_empty ( type-id )
4507 __is_enum ( type-id )
4508 __is_final ( type-id )
4509 __is_literal_type ( type-id )
4510 __is_pod ( type-id )
4511 __is_polymorphic ( type-id )
4512 __is_std_layout ( type-id )
4513 __is_trivial ( type-id )
4514 __is_union ( type-id )
4515
4516 Objective-C++ Extension:
4517
4518 primary-expression:
4519 objc-expression
4520
4521 literal:
4522 __null
4523
4524 ADDRESS_P is true iff this expression was immediately preceded by
4525 "&" and therefore might denote a pointer-to-member. CAST_P is true
4526 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4527 true iff this expression is a template argument.
4528
4529 Returns a representation of the expression. Upon return, *IDK
4530 indicates what kind of id-expression (if any) was present. */
4531
4532 static tree
4533 cp_parser_primary_expression (cp_parser *parser,
4534 bool address_p,
4535 bool cast_p,
4536 bool template_arg_p,
4537 bool decltype_p,
4538 cp_id_kind *idk)
4539 {
4540 cp_token *token = NULL;
4541
4542 /* Assume the primary expression is not an id-expression. */
4543 *idk = CP_ID_KIND_NONE;
4544
4545 /* Peek at the next token. */
4546 token = cp_lexer_peek_token (parser->lexer);
4547 switch ((int) token->type)
4548 {
4549 /* literal:
4550 integer-literal
4551 character-literal
4552 floating-literal
4553 string-literal
4554 boolean-literal
4555 pointer-literal
4556 user-defined-literal */
4557 case CPP_CHAR:
4558 case CPP_CHAR16:
4559 case CPP_CHAR32:
4560 case CPP_WCHAR:
4561 case CPP_UTF8CHAR:
4562 case CPP_NUMBER:
4563 case CPP_PREPARSED_EXPR:
4564 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4565 return cp_parser_userdef_numeric_literal (parser);
4566 token = cp_lexer_consume_token (parser->lexer);
4567 if (TREE_CODE (token->u.value) == FIXED_CST)
4568 {
4569 error_at (token->location,
4570 "fixed-point types not supported in C++");
4571 return error_mark_node;
4572 }
4573 /* Floating-point literals are only allowed in an integral
4574 constant expression if they are cast to an integral or
4575 enumeration type. */
4576 if (TREE_CODE (token->u.value) == REAL_CST
4577 && parser->integral_constant_expression_p
4578 && pedantic)
4579 {
4580 /* CAST_P will be set even in invalid code like "int(2.7 +
4581 ...)". Therefore, we have to check that the next token
4582 is sure to end the cast. */
4583 if (cast_p)
4584 {
4585 cp_token *next_token;
4586
4587 next_token = cp_lexer_peek_token (parser->lexer);
4588 if (/* The comma at the end of an
4589 enumerator-definition. */
4590 next_token->type != CPP_COMMA
4591 /* The curly brace at the end of an enum-specifier. */
4592 && next_token->type != CPP_CLOSE_BRACE
4593 /* The end of a statement. */
4594 && next_token->type != CPP_SEMICOLON
4595 /* The end of the cast-expression. */
4596 && next_token->type != CPP_CLOSE_PAREN
4597 /* The end of an array bound. */
4598 && next_token->type != CPP_CLOSE_SQUARE
4599 /* The closing ">" in a template-argument-list. */
4600 && (next_token->type != CPP_GREATER
4601 || parser->greater_than_is_operator_p)
4602 /* C++0x only: A ">>" treated like two ">" tokens,
4603 in a template-argument-list. */
4604 && (next_token->type != CPP_RSHIFT
4605 || (cxx_dialect == cxx98)
4606 || parser->greater_than_is_operator_p))
4607 cast_p = false;
4608 }
4609
4610 /* If we are within a cast, then the constraint that the
4611 cast is to an integral or enumeration type will be
4612 checked at that point. If we are not within a cast, then
4613 this code is invalid. */
4614 if (!cast_p)
4615 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4616 }
4617 return token->u.value;
4618
4619 case CPP_CHAR_USERDEF:
4620 case CPP_CHAR16_USERDEF:
4621 case CPP_CHAR32_USERDEF:
4622 case CPP_WCHAR_USERDEF:
4623 case CPP_UTF8CHAR_USERDEF:
4624 return cp_parser_userdef_char_literal (parser);
4625
4626 case CPP_STRING:
4627 case CPP_STRING16:
4628 case CPP_STRING32:
4629 case CPP_WSTRING:
4630 case CPP_UTF8STRING:
4631 case CPP_STRING_USERDEF:
4632 case CPP_STRING16_USERDEF:
4633 case CPP_STRING32_USERDEF:
4634 case CPP_WSTRING_USERDEF:
4635 case CPP_UTF8STRING_USERDEF:
4636 /* ??? Should wide strings be allowed when parser->translate_strings_p
4637 is false (i.e. in attributes)? If not, we can kill the third
4638 argument to cp_parser_string_literal. */
4639 return cp_parser_string_literal (parser,
4640 parser->translate_strings_p,
4641 true);
4642
4643 case CPP_OPEN_PAREN:
4644 /* If we see `( { ' then we are looking at the beginning of
4645 a GNU statement-expression. */
4646 if (cp_parser_allow_gnu_extensions_p (parser)
4647 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4648 {
4649 /* Statement-expressions are not allowed by the standard. */
4650 pedwarn (token->location, OPT_Wpedantic,
4651 "ISO C++ forbids braced-groups within expressions");
4652
4653 /* And they're not allowed outside of a function-body; you
4654 cannot, for example, write:
4655
4656 int i = ({ int j = 3; j + 1; });
4657
4658 at class or namespace scope. */
4659 if (!parser->in_function_body
4660 || parser->in_template_argument_list_p)
4661 {
4662 error_at (token->location,
4663 "statement-expressions are not allowed outside "
4664 "functions nor in template-argument lists");
4665 cp_parser_skip_to_end_of_block_or_statement (parser);
4666 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4667 cp_lexer_consume_token (parser->lexer);
4668 return error_mark_node;
4669 }
4670 else
4671 return cp_parser_statement_expr (parser);
4672 }
4673 /* Otherwise it's a normal parenthesized expression. */
4674 {
4675 tree expr;
4676 bool saved_greater_than_is_operator_p;
4677
4678 /* Consume the `('. */
4679 cp_lexer_consume_token (parser->lexer);
4680 /* Within a parenthesized expression, a `>' token is always
4681 the greater-than operator. */
4682 saved_greater_than_is_operator_p
4683 = parser->greater_than_is_operator_p;
4684 parser->greater_than_is_operator_p = true;
4685
4686 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4687 /* Left fold expression. */
4688 expr = NULL_TREE;
4689 else
4690 /* Parse the parenthesized expression. */
4691 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4692
4693 token = cp_lexer_peek_token (parser->lexer);
4694 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4695 {
4696 expr = cp_parser_fold_expression (parser, expr);
4697 if (expr != error_mark_node
4698 && cxx_dialect < cxx1z
4699 && !in_system_header_at (input_location))
4700 pedwarn (input_location, 0, "fold-expressions only available "
4701 "with -std=c++1z or -std=gnu++1z");
4702 }
4703 else
4704 /* Let the front end know that this expression was
4705 enclosed in parentheses. This matters in case, for
4706 example, the expression is of the form `A::B', since
4707 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4708 not. */
4709 expr = finish_parenthesized_expr (expr);
4710
4711 /* DR 705: Wrapping an unqualified name in parentheses
4712 suppresses arg-dependent lookup. We want to pass back
4713 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4714 (c++/37862), but none of the others. */
4715 if (*idk != CP_ID_KIND_QUALIFIED)
4716 *idk = CP_ID_KIND_NONE;
4717
4718 /* The `>' token might be the end of a template-id or
4719 template-parameter-list now. */
4720 parser->greater_than_is_operator_p
4721 = saved_greater_than_is_operator_p;
4722 /* Consume the `)'. */
4723 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4724 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4725 cp_parser_skip_to_end_of_statement (parser);
4726
4727 return expr;
4728 }
4729
4730 case CPP_OPEN_SQUARE:
4731 {
4732 if (c_dialect_objc ())
4733 {
4734 /* We might have an Objective-C++ message. */
4735 cp_parser_parse_tentatively (parser);
4736 tree msg = cp_parser_objc_message_expression (parser);
4737 /* If that works out, we're done ... */
4738 if (cp_parser_parse_definitely (parser))
4739 return msg;
4740 /* ... else, fall though to see if it's a lambda. */
4741 }
4742 tree lam = cp_parser_lambda_expression (parser);
4743 /* Don't warn about a failed tentative parse. */
4744 if (cp_parser_error_occurred (parser))
4745 return error_mark_node;
4746 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4747 return lam;
4748 }
4749
4750 case CPP_OBJC_STRING:
4751 if (c_dialect_objc ())
4752 /* We have an Objective-C++ string literal. */
4753 return cp_parser_objc_expression (parser);
4754 cp_parser_error (parser, "expected primary-expression");
4755 return error_mark_node;
4756
4757 case CPP_KEYWORD:
4758 switch (token->keyword)
4759 {
4760 /* These two are the boolean literals. */
4761 case RID_TRUE:
4762 cp_lexer_consume_token (parser->lexer);
4763 return boolean_true_node;
4764 case RID_FALSE:
4765 cp_lexer_consume_token (parser->lexer);
4766 return boolean_false_node;
4767
4768 /* The `__null' literal. */
4769 case RID_NULL:
4770 cp_lexer_consume_token (parser->lexer);
4771 return null_node;
4772
4773 /* The `nullptr' literal. */
4774 case RID_NULLPTR:
4775 cp_lexer_consume_token (parser->lexer);
4776 return nullptr_node;
4777
4778 /* Recognize the `this' keyword. */
4779 case RID_THIS:
4780 cp_lexer_consume_token (parser->lexer);
4781 if (parser->local_variables_forbidden_p)
4782 {
4783 error_at (token->location,
4784 "%<this%> may not be used in this context");
4785 return error_mark_node;
4786 }
4787 /* Pointers cannot appear in constant-expressions. */
4788 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4789 return error_mark_node;
4790 return finish_this_expr ();
4791
4792 /* The `operator' keyword can be the beginning of an
4793 id-expression. */
4794 case RID_OPERATOR:
4795 goto id_expression;
4796
4797 case RID_FUNCTION_NAME:
4798 case RID_PRETTY_FUNCTION_NAME:
4799 case RID_C99_FUNCTION_NAME:
4800 {
4801 non_integral_constant name;
4802
4803 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4804 __func__ are the names of variables -- but they are
4805 treated specially. Therefore, they are handled here,
4806 rather than relying on the generic id-expression logic
4807 below. Grammatically, these names are id-expressions.
4808
4809 Consume the token. */
4810 token = cp_lexer_consume_token (parser->lexer);
4811
4812 switch (token->keyword)
4813 {
4814 case RID_FUNCTION_NAME:
4815 name = NIC_FUNC_NAME;
4816 break;
4817 case RID_PRETTY_FUNCTION_NAME:
4818 name = NIC_PRETTY_FUNC;
4819 break;
4820 case RID_C99_FUNCTION_NAME:
4821 name = NIC_C99_FUNC;
4822 break;
4823 default:
4824 gcc_unreachable ();
4825 }
4826
4827 if (cp_parser_non_integral_constant_expression (parser, name))
4828 return error_mark_node;
4829
4830 /* Look up the name. */
4831 return finish_fname (token->u.value);
4832 }
4833
4834 case RID_VA_ARG:
4835 {
4836 tree expression;
4837 tree type;
4838 source_location type_location;
4839
4840 /* The `__builtin_va_arg' construct is used to handle
4841 `va_arg'. Consume the `__builtin_va_arg' token. */
4842 cp_lexer_consume_token (parser->lexer);
4843 /* Look for the opening `('. */
4844 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4845 /* Now, parse the assignment-expression. */
4846 expression = cp_parser_assignment_expression (parser);
4847 /* Look for the `,'. */
4848 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4849 type_location = cp_lexer_peek_token (parser->lexer)->location;
4850 /* Parse the type-id. */
4851 type = cp_parser_type_id (parser);
4852 /* Look for the closing `)'. */
4853 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4854 /* Using `va_arg' in a constant-expression is not
4855 allowed. */
4856 if (cp_parser_non_integral_constant_expression (parser,
4857 NIC_VA_ARG))
4858 return error_mark_node;
4859 return build_x_va_arg (type_location, expression, type);
4860 }
4861
4862 case RID_OFFSETOF:
4863 return cp_parser_builtin_offsetof (parser);
4864
4865 case RID_HAS_NOTHROW_ASSIGN:
4866 case RID_HAS_NOTHROW_CONSTRUCTOR:
4867 case RID_HAS_NOTHROW_COPY:
4868 case RID_HAS_TRIVIAL_ASSIGN:
4869 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4870 case RID_HAS_TRIVIAL_COPY:
4871 case RID_HAS_TRIVIAL_DESTRUCTOR:
4872 case RID_HAS_VIRTUAL_DESTRUCTOR:
4873 case RID_IS_ABSTRACT:
4874 case RID_IS_BASE_OF:
4875 case RID_IS_CLASS:
4876 case RID_IS_EMPTY:
4877 case RID_IS_ENUM:
4878 case RID_IS_FINAL:
4879 case RID_IS_LITERAL_TYPE:
4880 case RID_IS_POD:
4881 case RID_IS_POLYMORPHIC:
4882 case RID_IS_SAME_AS:
4883 case RID_IS_STD_LAYOUT:
4884 case RID_IS_TRIVIAL:
4885 case RID_IS_TRIVIALLY_ASSIGNABLE:
4886 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4887 case RID_IS_TRIVIALLY_COPYABLE:
4888 case RID_IS_UNION:
4889 return cp_parser_trait_expr (parser, token->keyword);
4890
4891 // C++ concepts
4892 case RID_REQUIRES:
4893 return cp_parser_requires_expression (parser);
4894
4895 /* Objective-C++ expressions. */
4896 case RID_AT_ENCODE:
4897 case RID_AT_PROTOCOL:
4898 case RID_AT_SELECTOR:
4899 return cp_parser_objc_expression (parser);
4900
4901 case RID_TEMPLATE:
4902 if (parser->in_function_body
4903 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4904 == CPP_LESS))
4905 {
4906 error_at (token->location,
4907 "a template declaration cannot appear at block scope");
4908 cp_parser_skip_to_end_of_block_or_statement (parser);
4909 return error_mark_node;
4910 }
4911 default:
4912 cp_parser_error (parser, "expected primary-expression");
4913 return error_mark_node;
4914 }
4915
4916 /* An id-expression can start with either an identifier, a
4917 `::' as the beginning of a qualified-id, or the "operator"
4918 keyword. */
4919 case CPP_NAME:
4920 case CPP_SCOPE:
4921 case CPP_TEMPLATE_ID:
4922 case CPP_NESTED_NAME_SPECIFIER:
4923 {
4924 tree id_expression;
4925 tree decl;
4926 const char *error_msg;
4927 bool template_p;
4928 bool done;
4929 cp_token *id_expr_token;
4930
4931 id_expression:
4932 /* Parse the id-expression. */
4933 id_expression
4934 = cp_parser_id_expression (parser,
4935 /*template_keyword_p=*/false,
4936 /*check_dependency_p=*/true,
4937 &template_p,
4938 /*declarator_p=*/false,
4939 /*optional_p=*/false);
4940 if (id_expression == error_mark_node)
4941 return error_mark_node;
4942 id_expr_token = token;
4943 token = cp_lexer_peek_token (parser->lexer);
4944 done = (token->type != CPP_OPEN_SQUARE
4945 && token->type != CPP_OPEN_PAREN
4946 && token->type != CPP_DOT
4947 && token->type != CPP_DEREF
4948 && token->type != CPP_PLUS_PLUS
4949 && token->type != CPP_MINUS_MINUS);
4950 /* If we have a template-id, then no further lookup is
4951 required. If the template-id was for a template-class, we
4952 will sometimes have a TYPE_DECL at this point. */
4953 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4954 || TREE_CODE (id_expression) == TYPE_DECL)
4955 decl = id_expression;
4956 /* Look up the name. */
4957 else
4958 {
4959 tree ambiguous_decls;
4960
4961 /* If we already know that this lookup is ambiguous, then
4962 we've already issued an error message; there's no reason
4963 to check again. */
4964 if (id_expr_token->type == CPP_NAME
4965 && id_expr_token->error_reported)
4966 {
4967 cp_parser_simulate_error (parser);
4968 return error_mark_node;
4969 }
4970
4971 decl = cp_parser_lookup_name (parser, id_expression,
4972 none_type,
4973 template_p,
4974 /*is_namespace=*/false,
4975 /*check_dependency=*/true,
4976 &ambiguous_decls,
4977 id_expr_token->location);
4978 /* If the lookup was ambiguous, an error will already have
4979 been issued. */
4980 if (ambiguous_decls)
4981 return error_mark_node;
4982
4983 /* In Objective-C++, we may have an Objective-C 2.0
4984 dot-syntax for classes here. */
4985 if (c_dialect_objc ()
4986 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4987 && TREE_CODE (decl) == TYPE_DECL
4988 && objc_is_class_name (decl))
4989 {
4990 tree component;
4991 cp_lexer_consume_token (parser->lexer);
4992 component = cp_parser_identifier (parser);
4993 if (component == error_mark_node)
4994 return error_mark_node;
4995
4996 return objc_build_class_component_ref (id_expression, component);
4997 }
4998
4999 /* In Objective-C++, an instance variable (ivar) may be preferred
5000 to whatever cp_parser_lookup_name() found. */
5001 decl = objc_lookup_ivar (decl, id_expression);
5002
5003 /* If name lookup gives us a SCOPE_REF, then the
5004 qualifying scope was dependent. */
5005 if (TREE_CODE (decl) == SCOPE_REF)
5006 {
5007 /* At this point, we do not know if DECL is a valid
5008 integral constant expression. We assume that it is
5009 in fact such an expression, so that code like:
5010
5011 template <int N> struct A {
5012 int a[B<N>::i];
5013 };
5014
5015 is accepted. At template-instantiation time, we
5016 will check that B<N>::i is actually a constant. */
5017 return decl;
5018 }
5019 /* Check to see if DECL is a local variable in a context
5020 where that is forbidden. */
5021 if (parser->local_variables_forbidden_p
5022 && local_variable_p (decl))
5023 {
5024 /* It might be that we only found DECL because we are
5025 trying to be generous with pre-ISO scoping rules.
5026 For example, consider:
5027
5028 int i;
5029 void g() {
5030 for (int i = 0; i < 10; ++i) {}
5031 extern void f(int j = i);
5032 }
5033
5034 Here, name look up will originally find the out
5035 of scope `i'. We need to issue a warning message,
5036 but then use the global `i'. */
5037 decl = check_for_out_of_scope_variable (decl);
5038 if (local_variable_p (decl))
5039 {
5040 error_at (id_expr_token->location,
5041 "local variable %qD may not appear in this context",
5042 decl);
5043 return error_mark_node;
5044 }
5045 }
5046 }
5047
5048 decl = (finish_id_expression
5049 (id_expression, decl, parser->scope,
5050 idk,
5051 parser->integral_constant_expression_p,
5052 parser->allow_non_integral_constant_expression_p,
5053 &parser->non_integral_constant_expression_p,
5054 template_p, done, address_p,
5055 template_arg_p,
5056 &error_msg,
5057 id_expr_token->location));
5058 if (error_msg)
5059 cp_parser_error (parser, error_msg);
5060 return decl;
5061 }
5062
5063 /* Anything else is an error. */
5064 default:
5065 cp_parser_error (parser, "expected primary-expression");
5066 return error_mark_node;
5067 }
5068 }
5069
5070 static inline tree
5071 cp_parser_primary_expression (cp_parser *parser,
5072 bool address_p,
5073 bool cast_p,
5074 bool template_arg_p,
5075 cp_id_kind *idk)
5076 {
5077 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5078 /*decltype*/false, idk);
5079 }
5080
5081 /* Parse an id-expression.
5082
5083 id-expression:
5084 unqualified-id
5085 qualified-id
5086
5087 qualified-id:
5088 :: [opt] nested-name-specifier template [opt] unqualified-id
5089 :: identifier
5090 :: operator-function-id
5091 :: template-id
5092
5093 Return a representation of the unqualified portion of the
5094 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5095 a `::' or nested-name-specifier.
5096
5097 Often, if the id-expression was a qualified-id, the caller will
5098 want to make a SCOPE_REF to represent the qualified-id. This
5099 function does not do this in order to avoid wastefully creating
5100 SCOPE_REFs when they are not required.
5101
5102 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5103 `template' keyword.
5104
5105 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5106 uninstantiated templates.
5107
5108 If *TEMPLATE_P is non-NULL, it is set to true iff the
5109 `template' keyword is used to explicitly indicate that the entity
5110 named is a template.
5111
5112 If DECLARATOR_P is true, the id-expression is appearing as part of
5113 a declarator, rather than as part of an expression. */
5114
5115 static tree
5116 cp_parser_id_expression (cp_parser *parser,
5117 bool template_keyword_p,
5118 bool check_dependency_p,
5119 bool *template_p,
5120 bool declarator_p,
5121 bool optional_p)
5122 {
5123 bool global_scope_p;
5124 bool nested_name_specifier_p;
5125
5126 /* Assume the `template' keyword was not used. */
5127 if (template_p)
5128 *template_p = template_keyword_p;
5129
5130 /* Look for the optional `::' operator. */
5131 global_scope_p
5132 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5133 != NULL_TREE);
5134 /* Look for the optional nested-name-specifier. */
5135 nested_name_specifier_p
5136 = (cp_parser_nested_name_specifier_opt (parser,
5137 /*typename_keyword_p=*/false,
5138 check_dependency_p,
5139 /*type_p=*/false,
5140 declarator_p)
5141 != NULL_TREE);
5142 /* If there is a nested-name-specifier, then we are looking at
5143 the first qualified-id production. */
5144 if (nested_name_specifier_p)
5145 {
5146 tree saved_scope;
5147 tree saved_object_scope;
5148 tree saved_qualifying_scope;
5149 tree unqualified_id;
5150 bool is_template;
5151
5152 /* See if the next token is the `template' keyword. */
5153 if (!template_p)
5154 template_p = &is_template;
5155 *template_p = cp_parser_optional_template_keyword (parser);
5156 /* Name lookup we do during the processing of the
5157 unqualified-id might obliterate SCOPE. */
5158 saved_scope = parser->scope;
5159 saved_object_scope = parser->object_scope;
5160 saved_qualifying_scope = parser->qualifying_scope;
5161 /* Process the final unqualified-id. */
5162 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5163 check_dependency_p,
5164 declarator_p,
5165 /*optional_p=*/false);
5166 /* Restore the SAVED_SCOPE for our caller. */
5167 parser->scope = saved_scope;
5168 parser->object_scope = saved_object_scope;
5169 parser->qualifying_scope = saved_qualifying_scope;
5170
5171 return unqualified_id;
5172 }
5173 /* Otherwise, if we are in global scope, then we are looking at one
5174 of the other qualified-id productions. */
5175 else if (global_scope_p)
5176 {
5177 cp_token *token;
5178 tree id;
5179
5180 /* Peek at the next token. */
5181 token = cp_lexer_peek_token (parser->lexer);
5182
5183 /* If it's an identifier, and the next token is not a "<", then
5184 we can avoid the template-id case. This is an optimization
5185 for this common case. */
5186 if (token->type == CPP_NAME
5187 && !cp_parser_nth_token_starts_template_argument_list_p
5188 (parser, 2))
5189 return cp_parser_identifier (parser);
5190
5191 cp_parser_parse_tentatively (parser);
5192 /* Try a template-id. */
5193 id = cp_parser_template_id (parser,
5194 /*template_keyword_p=*/false,
5195 /*check_dependency_p=*/true,
5196 none_type,
5197 declarator_p);
5198 /* If that worked, we're done. */
5199 if (cp_parser_parse_definitely (parser))
5200 return id;
5201
5202 /* Peek at the next token. (Changes in the token buffer may
5203 have invalidated the pointer obtained above.) */
5204 token = cp_lexer_peek_token (parser->lexer);
5205
5206 switch (token->type)
5207 {
5208 case CPP_NAME:
5209 return cp_parser_identifier (parser);
5210
5211 case CPP_KEYWORD:
5212 if (token->keyword == RID_OPERATOR)
5213 return cp_parser_operator_function_id (parser);
5214 /* Fall through. */
5215
5216 default:
5217 cp_parser_error (parser, "expected id-expression");
5218 return error_mark_node;
5219 }
5220 }
5221 else
5222 return cp_parser_unqualified_id (parser, template_keyword_p,
5223 /*check_dependency_p=*/true,
5224 declarator_p,
5225 optional_p);
5226 }
5227
5228 /* Parse an unqualified-id.
5229
5230 unqualified-id:
5231 identifier
5232 operator-function-id
5233 conversion-function-id
5234 ~ class-name
5235 template-id
5236
5237 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5238 keyword, in a construct like `A::template ...'.
5239
5240 Returns a representation of unqualified-id. For the `identifier'
5241 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5242 production a BIT_NOT_EXPR is returned; the operand of the
5243 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5244 other productions, see the documentation accompanying the
5245 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5246 names are looked up in uninstantiated templates. If DECLARATOR_P
5247 is true, the unqualified-id is appearing as part of a declarator,
5248 rather than as part of an expression. */
5249
5250 static tree
5251 cp_parser_unqualified_id (cp_parser* parser,
5252 bool template_keyword_p,
5253 bool check_dependency_p,
5254 bool declarator_p,
5255 bool optional_p)
5256 {
5257 cp_token *token;
5258
5259 /* Peek at the next token. */
5260 token = cp_lexer_peek_token (parser->lexer);
5261
5262 switch ((int) token->type)
5263 {
5264 case CPP_NAME:
5265 {
5266 tree id;
5267
5268 /* We don't know yet whether or not this will be a
5269 template-id. */
5270 cp_parser_parse_tentatively (parser);
5271 /* Try a template-id. */
5272 id = cp_parser_template_id (parser, template_keyword_p,
5273 check_dependency_p,
5274 none_type,
5275 declarator_p);
5276 /* If it worked, we're done. */
5277 if (cp_parser_parse_definitely (parser))
5278 return id;
5279 /* Otherwise, it's an ordinary identifier. */
5280 return cp_parser_identifier (parser);
5281 }
5282
5283 case CPP_TEMPLATE_ID:
5284 return cp_parser_template_id (parser, template_keyword_p,
5285 check_dependency_p,
5286 none_type,
5287 declarator_p);
5288
5289 case CPP_COMPL:
5290 {
5291 tree type_decl;
5292 tree qualifying_scope;
5293 tree object_scope;
5294 tree scope;
5295 bool done;
5296
5297 /* Consume the `~' token. */
5298 cp_lexer_consume_token (parser->lexer);
5299 /* Parse the class-name. The standard, as written, seems to
5300 say that:
5301
5302 template <typename T> struct S { ~S (); };
5303 template <typename T> S<T>::~S() {}
5304
5305 is invalid, since `~' must be followed by a class-name, but
5306 `S<T>' is dependent, and so not known to be a class.
5307 That's not right; we need to look in uninstantiated
5308 templates. A further complication arises from:
5309
5310 template <typename T> void f(T t) {
5311 t.T::~T();
5312 }
5313
5314 Here, it is not possible to look up `T' in the scope of `T'
5315 itself. We must look in both the current scope, and the
5316 scope of the containing complete expression.
5317
5318 Yet another issue is:
5319
5320 struct S {
5321 int S;
5322 ~S();
5323 };
5324
5325 S::~S() {}
5326
5327 The standard does not seem to say that the `S' in `~S'
5328 should refer to the type `S' and not the data member
5329 `S::S'. */
5330
5331 /* DR 244 says that we look up the name after the "~" in the
5332 same scope as we looked up the qualifying name. That idea
5333 isn't fully worked out; it's more complicated than that. */
5334 scope = parser->scope;
5335 object_scope = parser->object_scope;
5336 qualifying_scope = parser->qualifying_scope;
5337
5338 /* Check for invalid scopes. */
5339 if (scope == error_mark_node)
5340 {
5341 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5342 cp_lexer_consume_token (parser->lexer);
5343 return error_mark_node;
5344 }
5345 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5346 {
5347 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5348 error_at (token->location,
5349 "scope %qT before %<~%> is not a class-name",
5350 scope);
5351 cp_parser_simulate_error (parser);
5352 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5353 cp_lexer_consume_token (parser->lexer);
5354 return error_mark_node;
5355 }
5356 gcc_assert (!scope || TYPE_P (scope));
5357
5358 /* If the name is of the form "X::~X" it's OK even if X is a
5359 typedef. */
5360 token = cp_lexer_peek_token (parser->lexer);
5361 if (scope
5362 && token->type == CPP_NAME
5363 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5364 != CPP_LESS)
5365 && (token->u.value == TYPE_IDENTIFIER (scope)
5366 || (CLASS_TYPE_P (scope)
5367 && constructor_name_p (token->u.value, scope))))
5368 {
5369 cp_lexer_consume_token (parser->lexer);
5370 return build_nt (BIT_NOT_EXPR, scope);
5371 }
5372
5373 /* ~auto means the destructor of whatever the object is. */
5374 if (cp_parser_is_keyword (token, RID_AUTO))
5375 {
5376 if (cxx_dialect < cxx14)
5377 pedwarn (input_location, 0,
5378 "%<~auto%> only available with "
5379 "-std=c++14 or -std=gnu++14");
5380 cp_lexer_consume_token (parser->lexer);
5381 return build_nt (BIT_NOT_EXPR, make_auto ());
5382 }
5383
5384 /* If there was an explicit qualification (S::~T), first look
5385 in the scope given by the qualification (i.e., S).
5386
5387 Note: in the calls to cp_parser_class_name below we pass
5388 typename_type so that lookup finds the injected-class-name
5389 rather than the constructor. */
5390 done = false;
5391 type_decl = NULL_TREE;
5392 if (scope)
5393 {
5394 cp_parser_parse_tentatively (parser);
5395 type_decl = cp_parser_class_name (parser,
5396 /*typename_keyword_p=*/false,
5397 /*template_keyword_p=*/false,
5398 typename_type,
5399 /*check_dependency=*/false,
5400 /*class_head_p=*/false,
5401 declarator_p);
5402 if (cp_parser_parse_definitely (parser))
5403 done = true;
5404 }
5405 /* In "N::S::~S", look in "N" as well. */
5406 if (!done && scope && qualifying_scope)
5407 {
5408 cp_parser_parse_tentatively (parser);
5409 parser->scope = qualifying_scope;
5410 parser->object_scope = NULL_TREE;
5411 parser->qualifying_scope = NULL_TREE;
5412 type_decl
5413 = cp_parser_class_name (parser,
5414 /*typename_keyword_p=*/false,
5415 /*template_keyword_p=*/false,
5416 typename_type,
5417 /*check_dependency=*/false,
5418 /*class_head_p=*/false,
5419 declarator_p);
5420 if (cp_parser_parse_definitely (parser))
5421 done = true;
5422 }
5423 /* In "p->S::~T", look in the scope given by "*p" as well. */
5424 else if (!done && object_scope)
5425 {
5426 cp_parser_parse_tentatively (parser);
5427 parser->scope = object_scope;
5428 parser->object_scope = NULL_TREE;
5429 parser->qualifying_scope = NULL_TREE;
5430 type_decl
5431 = cp_parser_class_name (parser,
5432 /*typename_keyword_p=*/false,
5433 /*template_keyword_p=*/false,
5434 typename_type,
5435 /*check_dependency=*/false,
5436 /*class_head_p=*/false,
5437 declarator_p);
5438 if (cp_parser_parse_definitely (parser))
5439 done = true;
5440 }
5441 /* Look in the surrounding context. */
5442 if (!done)
5443 {
5444 parser->scope = NULL_TREE;
5445 parser->object_scope = NULL_TREE;
5446 parser->qualifying_scope = NULL_TREE;
5447 if (processing_template_decl)
5448 cp_parser_parse_tentatively (parser);
5449 type_decl
5450 = cp_parser_class_name (parser,
5451 /*typename_keyword_p=*/false,
5452 /*template_keyword_p=*/false,
5453 typename_type,
5454 /*check_dependency=*/false,
5455 /*class_head_p=*/false,
5456 declarator_p);
5457 if (processing_template_decl
5458 && ! cp_parser_parse_definitely (parser))
5459 {
5460 /* We couldn't find a type with this name. If we're parsing
5461 tentatively, fail and try something else. */
5462 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5463 {
5464 cp_parser_simulate_error (parser);
5465 return error_mark_node;
5466 }
5467 /* Otherwise, accept it and check for a match at instantiation
5468 time. */
5469 type_decl = cp_parser_identifier (parser);
5470 if (type_decl != error_mark_node)
5471 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5472 return type_decl;
5473 }
5474 }
5475 /* If an error occurred, assume that the name of the
5476 destructor is the same as the name of the qualifying
5477 class. That allows us to keep parsing after running
5478 into ill-formed destructor names. */
5479 if (type_decl == error_mark_node && scope)
5480 return build_nt (BIT_NOT_EXPR, scope);
5481 else if (type_decl == error_mark_node)
5482 return error_mark_node;
5483
5484 /* Check that destructor name and scope match. */
5485 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5486 {
5487 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5488 error_at (token->location,
5489 "declaration of %<~%T%> as member of %qT",
5490 type_decl, scope);
5491 cp_parser_simulate_error (parser);
5492 return error_mark_node;
5493 }
5494
5495 /* [class.dtor]
5496
5497 A typedef-name that names a class shall not be used as the
5498 identifier in the declarator for a destructor declaration. */
5499 if (declarator_p
5500 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5501 && !DECL_SELF_REFERENCE_P (type_decl)
5502 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5503 error_at (token->location,
5504 "typedef-name %qD used as destructor declarator",
5505 type_decl);
5506
5507 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5508 }
5509
5510 case CPP_KEYWORD:
5511 if (token->keyword == RID_OPERATOR)
5512 {
5513 tree id;
5514
5515 /* This could be a template-id, so we try that first. */
5516 cp_parser_parse_tentatively (parser);
5517 /* Try a template-id. */
5518 id = cp_parser_template_id (parser, template_keyword_p,
5519 /*check_dependency_p=*/true,
5520 none_type,
5521 declarator_p);
5522 /* If that worked, we're done. */
5523 if (cp_parser_parse_definitely (parser))
5524 return id;
5525 /* We still don't know whether we're looking at an
5526 operator-function-id or a conversion-function-id. */
5527 cp_parser_parse_tentatively (parser);
5528 /* Try an operator-function-id. */
5529 id = cp_parser_operator_function_id (parser);
5530 /* If that didn't work, try a conversion-function-id. */
5531 if (!cp_parser_parse_definitely (parser))
5532 id = cp_parser_conversion_function_id (parser);
5533 else if (UDLIT_OPER_P (id))
5534 {
5535 /* 17.6.3.3.5 */
5536 const char *name = UDLIT_OP_SUFFIX (id);
5537 if (name[0] != '_' && !in_system_header_at (input_location)
5538 && declarator_p)
5539 warning (0, "literal operator suffixes not preceded by %<_%>"
5540 " are reserved for future standardization");
5541 }
5542
5543 return id;
5544 }
5545 /* Fall through. */
5546
5547 default:
5548 if (optional_p)
5549 return NULL_TREE;
5550 cp_parser_error (parser, "expected unqualified-id");
5551 return error_mark_node;
5552 }
5553 }
5554
5555 /* Parse an (optional) nested-name-specifier.
5556
5557 nested-name-specifier: [C++98]
5558 class-or-namespace-name :: nested-name-specifier [opt]
5559 class-or-namespace-name :: template nested-name-specifier [opt]
5560
5561 nested-name-specifier: [C++0x]
5562 type-name ::
5563 namespace-name ::
5564 nested-name-specifier identifier ::
5565 nested-name-specifier template [opt] simple-template-id ::
5566
5567 PARSER->SCOPE should be set appropriately before this function is
5568 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5569 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5570 in name lookups.
5571
5572 Sets PARSER->SCOPE to the class (TYPE) or namespace
5573 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5574 it unchanged if there is no nested-name-specifier. Returns the new
5575 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5576
5577 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5578 part of a declaration and/or decl-specifier. */
5579
5580 static tree
5581 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5582 bool typename_keyword_p,
5583 bool check_dependency_p,
5584 bool type_p,
5585 bool is_declaration)
5586 {
5587 bool success = false;
5588 cp_token_position start = 0;
5589 cp_token *token;
5590
5591 /* Remember where the nested-name-specifier starts. */
5592 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5593 {
5594 start = cp_lexer_token_position (parser->lexer, false);
5595 push_deferring_access_checks (dk_deferred);
5596 }
5597
5598 while (true)
5599 {
5600 tree new_scope;
5601 tree old_scope;
5602 tree saved_qualifying_scope;
5603 bool template_keyword_p;
5604
5605 /* Spot cases that cannot be the beginning of a
5606 nested-name-specifier. */
5607 token = cp_lexer_peek_token (parser->lexer);
5608
5609 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5610 the already parsed nested-name-specifier. */
5611 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5612 {
5613 /* Grab the nested-name-specifier and continue the loop. */
5614 cp_parser_pre_parsed_nested_name_specifier (parser);
5615 /* If we originally encountered this nested-name-specifier
5616 with IS_DECLARATION set to false, we will not have
5617 resolved TYPENAME_TYPEs, so we must do so here. */
5618 if (is_declaration
5619 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5620 {
5621 new_scope = resolve_typename_type (parser->scope,
5622 /*only_current_p=*/false);
5623 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5624 parser->scope = new_scope;
5625 }
5626 success = true;
5627 continue;
5628 }
5629
5630 /* Spot cases that cannot be the beginning of a
5631 nested-name-specifier. On the second and subsequent times
5632 through the loop, we look for the `template' keyword. */
5633 if (success && token->keyword == RID_TEMPLATE)
5634 ;
5635 /* A template-id can start a nested-name-specifier. */
5636 else if (token->type == CPP_TEMPLATE_ID)
5637 ;
5638 /* DR 743: decltype can be used in a nested-name-specifier. */
5639 else if (token_is_decltype (token))
5640 ;
5641 else
5642 {
5643 /* If the next token is not an identifier, then it is
5644 definitely not a type-name or namespace-name. */
5645 if (token->type != CPP_NAME)
5646 break;
5647 /* If the following token is neither a `<' (to begin a
5648 template-id), nor a `::', then we are not looking at a
5649 nested-name-specifier. */
5650 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5651
5652 if (token->type == CPP_COLON
5653 && parser->colon_corrects_to_scope_p
5654 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5655 {
5656 error_at (token->location,
5657 "found %<:%> in nested-name-specifier, expected %<::%>");
5658 token->type = CPP_SCOPE;
5659 }
5660
5661 if (token->type != CPP_SCOPE
5662 && !cp_parser_nth_token_starts_template_argument_list_p
5663 (parser, 2))
5664 break;
5665 }
5666
5667 /* The nested-name-specifier is optional, so we parse
5668 tentatively. */
5669 cp_parser_parse_tentatively (parser);
5670
5671 /* Look for the optional `template' keyword, if this isn't the
5672 first time through the loop. */
5673 if (success)
5674 template_keyword_p = cp_parser_optional_template_keyword (parser);
5675 else
5676 template_keyword_p = false;
5677
5678 /* Save the old scope since the name lookup we are about to do
5679 might destroy it. */
5680 old_scope = parser->scope;
5681 saved_qualifying_scope = parser->qualifying_scope;
5682 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5683 look up names in "X<T>::I" in order to determine that "Y" is
5684 a template. So, if we have a typename at this point, we make
5685 an effort to look through it. */
5686 if (is_declaration
5687 && !typename_keyword_p
5688 && parser->scope
5689 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5690 parser->scope = resolve_typename_type (parser->scope,
5691 /*only_current_p=*/false);
5692 /* Parse the qualifying entity. */
5693 new_scope
5694 = cp_parser_qualifying_entity (parser,
5695 typename_keyword_p,
5696 template_keyword_p,
5697 check_dependency_p,
5698 type_p,
5699 is_declaration);
5700 /* Look for the `::' token. */
5701 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5702
5703 /* If we found what we wanted, we keep going; otherwise, we're
5704 done. */
5705 if (!cp_parser_parse_definitely (parser))
5706 {
5707 bool error_p = false;
5708
5709 /* Restore the OLD_SCOPE since it was valid before the
5710 failed attempt at finding the last
5711 class-or-namespace-name. */
5712 parser->scope = old_scope;
5713 parser->qualifying_scope = saved_qualifying_scope;
5714
5715 /* If the next token is a decltype, and the one after that is a
5716 `::', then the decltype has failed to resolve to a class or
5717 enumeration type. Give this error even when parsing
5718 tentatively since it can't possibly be valid--and we're going
5719 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5720 won't get another chance.*/
5721 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5722 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5723 == CPP_SCOPE))
5724 {
5725 token = cp_lexer_consume_token (parser->lexer);
5726 error_at (token->location, "decltype evaluates to %qT, "
5727 "which is not a class or enumeration type",
5728 token->u.value);
5729 parser->scope = error_mark_node;
5730 error_p = true;
5731 /* As below. */
5732 success = true;
5733 cp_lexer_consume_token (parser->lexer);
5734 }
5735
5736 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5737 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5738 {
5739 /* If we have a non-type template-id followed by ::, it can't
5740 possibly be valid. */
5741 token = cp_lexer_peek_token (parser->lexer);
5742 tree tid = token->u.tree_check_value->value;
5743 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5744 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5745 {
5746 tree tmpl = NULL_TREE;
5747 if (is_overloaded_fn (tid))
5748 {
5749 tree fns = get_fns (tid);
5750 if (!OVL_CHAIN (fns))
5751 tmpl = OVL_CURRENT (fns);
5752 error_at (token->location, "function template-id %qD "
5753 "in nested-name-specifier", tid);
5754 }
5755 else
5756 {
5757 /* Variable template. */
5758 tmpl = TREE_OPERAND (tid, 0);
5759 gcc_assert (variable_template_p (tmpl));
5760 error_at (token->location, "variable template-id %qD "
5761 "in nested-name-specifier", tid);
5762 }
5763 if (tmpl)
5764 inform (DECL_SOURCE_LOCATION (tmpl),
5765 "%qD declared here", tmpl);
5766
5767 parser->scope = error_mark_node;
5768 error_p = true;
5769 /* As below. */
5770 success = true;
5771 cp_lexer_consume_token (parser->lexer);
5772 cp_lexer_consume_token (parser->lexer);
5773 }
5774 }
5775
5776 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5777 break;
5778 /* If the next token is an identifier, and the one after
5779 that is a `::', then any valid interpretation would have
5780 found a class-or-namespace-name. */
5781 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5782 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5783 == CPP_SCOPE)
5784 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5785 != CPP_COMPL))
5786 {
5787 token = cp_lexer_consume_token (parser->lexer);
5788 if (!error_p)
5789 {
5790 if (!token->error_reported)
5791 {
5792 tree decl;
5793 tree ambiguous_decls;
5794
5795 decl = cp_parser_lookup_name (parser, token->u.value,
5796 none_type,
5797 /*is_template=*/false,
5798 /*is_namespace=*/false,
5799 /*check_dependency=*/true,
5800 &ambiguous_decls,
5801 token->location);
5802 if (TREE_CODE (decl) == TEMPLATE_DECL)
5803 error_at (token->location,
5804 "%qD used without template parameters",
5805 decl);
5806 else if (ambiguous_decls)
5807 {
5808 // cp_parser_lookup_name has the same diagnostic,
5809 // thus make sure to emit it at most once.
5810 if (cp_parser_uncommitted_to_tentative_parse_p
5811 (parser))
5812 {
5813 error_at (token->location,
5814 "reference to %qD is ambiguous",
5815 token->u.value);
5816 print_candidates (ambiguous_decls);
5817 }
5818 decl = error_mark_node;
5819 }
5820 else
5821 {
5822 if (cxx_dialect != cxx98)
5823 cp_parser_name_lookup_error
5824 (parser, token->u.value, decl, NLE_NOT_CXX98,
5825 token->location);
5826 else
5827 cp_parser_name_lookup_error
5828 (parser, token->u.value, decl, NLE_CXX98,
5829 token->location);
5830 }
5831 }
5832 parser->scope = error_mark_node;
5833 error_p = true;
5834 /* Treat this as a successful nested-name-specifier
5835 due to:
5836
5837 [basic.lookup.qual]
5838
5839 If the name found is not a class-name (clause
5840 _class_) or namespace-name (_namespace.def_), the
5841 program is ill-formed. */
5842 success = true;
5843 }
5844 cp_lexer_consume_token (parser->lexer);
5845 }
5846 break;
5847 }
5848 /* We've found one valid nested-name-specifier. */
5849 success = true;
5850 /* Name lookup always gives us a DECL. */
5851 if (TREE_CODE (new_scope) == TYPE_DECL)
5852 new_scope = TREE_TYPE (new_scope);
5853 /* Uses of "template" must be followed by actual templates. */
5854 if (template_keyword_p
5855 && !(CLASS_TYPE_P (new_scope)
5856 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5857 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5858 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5859 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5860 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5861 == TEMPLATE_ID_EXPR)))
5862 permerror (input_location, TYPE_P (new_scope)
5863 ? G_("%qT is not a template")
5864 : G_("%qD is not a template"),
5865 new_scope);
5866 /* If it is a class scope, try to complete it; we are about to
5867 be looking up names inside the class. */
5868 if (TYPE_P (new_scope)
5869 /* Since checking types for dependency can be expensive,
5870 avoid doing it if the type is already complete. */
5871 && !COMPLETE_TYPE_P (new_scope)
5872 /* Do not try to complete dependent types. */
5873 && !dependent_type_p (new_scope))
5874 {
5875 new_scope = complete_type (new_scope);
5876 /* If it is a typedef to current class, use the current
5877 class instead, as the typedef won't have any names inside
5878 it yet. */
5879 if (!COMPLETE_TYPE_P (new_scope)
5880 && currently_open_class (new_scope))
5881 new_scope = TYPE_MAIN_VARIANT (new_scope);
5882 }
5883 /* Make sure we look in the right scope the next time through
5884 the loop. */
5885 parser->scope = new_scope;
5886 }
5887
5888 /* If parsing tentatively, replace the sequence of tokens that makes
5889 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5890 token. That way, should we re-parse the token stream, we will
5891 not have to repeat the effort required to do the parse, nor will
5892 we issue duplicate error messages. */
5893 if (success && start)
5894 {
5895 cp_token *token;
5896
5897 token = cp_lexer_token_at (parser->lexer, start);
5898 /* Reset the contents of the START token. */
5899 token->type = CPP_NESTED_NAME_SPECIFIER;
5900 /* Retrieve any deferred checks. Do not pop this access checks yet
5901 so the memory will not be reclaimed during token replacing below. */
5902 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5903 token->u.tree_check_value->value = parser->scope;
5904 token->u.tree_check_value->checks = get_deferred_access_checks ();
5905 token->u.tree_check_value->qualifying_scope =
5906 parser->qualifying_scope;
5907 token->keyword = RID_MAX;
5908
5909 /* Purge all subsequent tokens. */
5910 cp_lexer_purge_tokens_after (parser->lexer, start);
5911 }
5912
5913 if (start)
5914 pop_to_parent_deferring_access_checks ();
5915
5916 return success ? parser->scope : NULL_TREE;
5917 }
5918
5919 /* Parse a nested-name-specifier. See
5920 cp_parser_nested_name_specifier_opt for details. This function
5921 behaves identically, except that it will an issue an error if no
5922 nested-name-specifier is present. */
5923
5924 static tree
5925 cp_parser_nested_name_specifier (cp_parser *parser,
5926 bool typename_keyword_p,
5927 bool check_dependency_p,
5928 bool type_p,
5929 bool is_declaration)
5930 {
5931 tree scope;
5932
5933 /* Look for the nested-name-specifier. */
5934 scope = cp_parser_nested_name_specifier_opt (parser,
5935 typename_keyword_p,
5936 check_dependency_p,
5937 type_p,
5938 is_declaration);
5939 /* If it was not present, issue an error message. */
5940 if (!scope)
5941 {
5942 cp_parser_error (parser, "expected nested-name-specifier");
5943 parser->scope = NULL_TREE;
5944 }
5945
5946 return scope;
5947 }
5948
5949 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5950 this is either a class-name or a namespace-name (which corresponds
5951 to the class-or-namespace-name production in the grammar). For
5952 C++0x, it can also be a type-name that refers to an enumeration
5953 type or a simple-template-id.
5954
5955 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5956 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5957 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5958 TYPE_P is TRUE iff the next name should be taken as a class-name,
5959 even the same name is declared to be another entity in the same
5960 scope.
5961
5962 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5963 specified by the class-or-namespace-name. If neither is found the
5964 ERROR_MARK_NODE is returned. */
5965
5966 static tree
5967 cp_parser_qualifying_entity (cp_parser *parser,
5968 bool typename_keyword_p,
5969 bool template_keyword_p,
5970 bool check_dependency_p,
5971 bool type_p,
5972 bool is_declaration)
5973 {
5974 tree saved_scope;
5975 tree saved_qualifying_scope;
5976 tree saved_object_scope;
5977 tree scope;
5978 bool only_class_p;
5979 bool successful_parse_p;
5980
5981 /* DR 743: decltype can appear in a nested-name-specifier. */
5982 if (cp_lexer_next_token_is_decltype (parser->lexer))
5983 {
5984 scope = cp_parser_decltype (parser);
5985 if (TREE_CODE (scope) != ENUMERAL_TYPE
5986 && !MAYBE_CLASS_TYPE_P (scope))
5987 {
5988 cp_parser_simulate_error (parser);
5989 return error_mark_node;
5990 }
5991 if (TYPE_NAME (scope))
5992 scope = TYPE_NAME (scope);
5993 return scope;
5994 }
5995
5996 /* Before we try to parse the class-name, we must save away the
5997 current PARSER->SCOPE since cp_parser_class_name will destroy
5998 it. */
5999 saved_scope = parser->scope;
6000 saved_qualifying_scope = parser->qualifying_scope;
6001 saved_object_scope = parser->object_scope;
6002 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6003 there is no need to look for a namespace-name. */
6004 only_class_p = template_keyword_p
6005 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6006 if (!only_class_p)
6007 cp_parser_parse_tentatively (parser);
6008 scope = cp_parser_class_name (parser,
6009 typename_keyword_p,
6010 template_keyword_p,
6011 type_p ? class_type : none_type,
6012 check_dependency_p,
6013 /*class_head_p=*/false,
6014 is_declaration,
6015 /*enum_ok=*/cxx_dialect > cxx98);
6016 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6017 /* If that didn't work, try for a namespace-name. */
6018 if (!only_class_p && !successful_parse_p)
6019 {
6020 /* Restore the saved scope. */
6021 parser->scope = saved_scope;
6022 parser->qualifying_scope = saved_qualifying_scope;
6023 parser->object_scope = saved_object_scope;
6024 /* If we are not looking at an identifier followed by the scope
6025 resolution operator, then this is not part of a
6026 nested-name-specifier. (Note that this function is only used
6027 to parse the components of a nested-name-specifier.) */
6028 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6029 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6030 return error_mark_node;
6031 scope = cp_parser_namespace_name (parser);
6032 }
6033
6034 return scope;
6035 }
6036
6037 /* Return true if we are looking at a compound-literal, false otherwise. */
6038
6039 static bool
6040 cp_parser_compound_literal_p (cp_parser *parser)
6041 {
6042 /* Consume the `('. */
6043 cp_lexer_consume_token (parser->lexer);
6044
6045 cp_lexer_save_tokens (parser->lexer);
6046
6047 /* Skip tokens until the next token is a closing parenthesis.
6048 If we find the closing `)', and the next token is a `{', then
6049 we are looking at a compound-literal. */
6050 bool compound_literal_p
6051 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6052 /*consume_paren=*/true)
6053 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6054
6055 /* Roll back the tokens we skipped. */
6056 cp_lexer_rollback_tokens (parser->lexer);
6057
6058 return compound_literal_p;
6059 }
6060
6061 /* Parse a postfix-expression.
6062
6063 postfix-expression:
6064 primary-expression
6065 postfix-expression [ expression ]
6066 postfix-expression ( expression-list [opt] )
6067 simple-type-specifier ( expression-list [opt] )
6068 typename :: [opt] nested-name-specifier identifier
6069 ( expression-list [opt] )
6070 typename :: [opt] nested-name-specifier template [opt] template-id
6071 ( expression-list [opt] )
6072 postfix-expression . template [opt] id-expression
6073 postfix-expression -> template [opt] id-expression
6074 postfix-expression . pseudo-destructor-name
6075 postfix-expression -> pseudo-destructor-name
6076 postfix-expression ++
6077 postfix-expression --
6078 dynamic_cast < type-id > ( expression )
6079 static_cast < type-id > ( expression )
6080 reinterpret_cast < type-id > ( expression )
6081 const_cast < type-id > ( expression )
6082 typeid ( expression )
6083 typeid ( type-id )
6084
6085 GNU Extension:
6086
6087 postfix-expression:
6088 ( type-id ) { initializer-list , [opt] }
6089
6090 This extension is a GNU version of the C99 compound-literal
6091 construct. (The C99 grammar uses `type-name' instead of `type-id',
6092 but they are essentially the same concept.)
6093
6094 If ADDRESS_P is true, the postfix expression is the operand of the
6095 `&' operator. CAST_P is true if this expression is the target of a
6096 cast.
6097
6098 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6099 class member access expressions [expr.ref].
6100
6101 Returns a representation of the expression. */
6102
6103 static tree
6104 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6105 bool member_access_only_p, bool decltype_p,
6106 cp_id_kind * pidk_return)
6107 {
6108 cp_token *token;
6109 location_t loc;
6110 enum rid keyword;
6111 cp_id_kind idk = CP_ID_KIND_NONE;
6112 tree postfix_expression = NULL_TREE;
6113 bool is_member_access = false;
6114 int saved_in_statement = -1;
6115
6116 /* Peek at the next token. */
6117 token = cp_lexer_peek_token (parser->lexer);
6118 loc = token->location;
6119 /* Some of the productions are determined by keywords. */
6120 keyword = token->keyword;
6121 switch (keyword)
6122 {
6123 case RID_DYNCAST:
6124 case RID_STATCAST:
6125 case RID_REINTCAST:
6126 case RID_CONSTCAST:
6127 {
6128 tree type;
6129 tree expression;
6130 const char *saved_message;
6131 bool saved_in_type_id_in_expr_p;
6132
6133 /* All of these can be handled in the same way from the point
6134 of view of parsing. Begin by consuming the token
6135 identifying the cast. */
6136 cp_lexer_consume_token (parser->lexer);
6137
6138 /* New types cannot be defined in the cast. */
6139 saved_message = parser->type_definition_forbidden_message;
6140 parser->type_definition_forbidden_message
6141 = G_("types may not be defined in casts");
6142
6143 /* Look for the opening `<'. */
6144 cp_parser_require (parser, CPP_LESS, RT_LESS);
6145 /* Parse the type to which we are casting. */
6146 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6147 parser->in_type_id_in_expr_p = true;
6148 type = cp_parser_type_id (parser);
6149 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6150 /* Look for the closing `>'. */
6151 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6152 /* Restore the old message. */
6153 parser->type_definition_forbidden_message = saved_message;
6154
6155 bool saved_greater_than_is_operator_p
6156 = parser->greater_than_is_operator_p;
6157 parser->greater_than_is_operator_p = true;
6158
6159 /* And the expression which is being cast. */
6160 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6161 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6162 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6163
6164 parser->greater_than_is_operator_p
6165 = saved_greater_than_is_operator_p;
6166
6167 /* Only type conversions to integral or enumeration types
6168 can be used in constant-expressions. */
6169 if (!cast_valid_in_integral_constant_expression_p (type)
6170 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6171 return error_mark_node;
6172
6173 switch (keyword)
6174 {
6175 case RID_DYNCAST:
6176 postfix_expression
6177 = build_dynamic_cast (type, expression, tf_warning_or_error);
6178 break;
6179 case RID_STATCAST:
6180 postfix_expression
6181 = build_static_cast (type, expression, tf_warning_or_error);
6182 break;
6183 case RID_REINTCAST:
6184 postfix_expression
6185 = build_reinterpret_cast (type, expression,
6186 tf_warning_or_error);
6187 break;
6188 case RID_CONSTCAST:
6189 postfix_expression
6190 = build_const_cast (type, expression, tf_warning_or_error);
6191 break;
6192 default:
6193 gcc_unreachable ();
6194 }
6195 }
6196 break;
6197
6198 case RID_TYPEID:
6199 {
6200 tree type;
6201 const char *saved_message;
6202 bool saved_in_type_id_in_expr_p;
6203
6204 /* Consume the `typeid' token. */
6205 cp_lexer_consume_token (parser->lexer);
6206 /* Look for the `(' token. */
6207 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6208 /* Types cannot be defined in a `typeid' expression. */
6209 saved_message = parser->type_definition_forbidden_message;
6210 parser->type_definition_forbidden_message
6211 = G_("types may not be defined in a %<typeid%> expression");
6212 /* We can't be sure yet whether we're looking at a type-id or an
6213 expression. */
6214 cp_parser_parse_tentatively (parser);
6215 /* Try a type-id first. */
6216 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6217 parser->in_type_id_in_expr_p = true;
6218 type = cp_parser_type_id (parser);
6219 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6220 /* Look for the `)' token. Otherwise, we can't be sure that
6221 we're not looking at an expression: consider `typeid (int
6222 (3))', for example. */
6223 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6224 /* If all went well, simply lookup the type-id. */
6225 if (cp_parser_parse_definitely (parser))
6226 postfix_expression = get_typeid (type, tf_warning_or_error);
6227 /* Otherwise, fall back to the expression variant. */
6228 else
6229 {
6230 tree expression;
6231
6232 /* Look for an expression. */
6233 expression = cp_parser_expression (parser, & idk);
6234 /* Compute its typeid. */
6235 postfix_expression = build_typeid (expression, tf_warning_or_error);
6236 /* Look for the `)' token. */
6237 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6238 }
6239 /* Restore the saved message. */
6240 parser->type_definition_forbidden_message = saved_message;
6241 /* `typeid' may not appear in an integral constant expression. */
6242 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6243 return error_mark_node;
6244 }
6245 break;
6246
6247 case RID_TYPENAME:
6248 {
6249 tree type;
6250 /* The syntax permitted here is the same permitted for an
6251 elaborated-type-specifier. */
6252 ++parser->prevent_constrained_type_specifiers;
6253 type = cp_parser_elaborated_type_specifier (parser,
6254 /*is_friend=*/false,
6255 /*is_declaration=*/false);
6256 --parser->prevent_constrained_type_specifiers;
6257 postfix_expression = cp_parser_functional_cast (parser, type);
6258 }
6259 break;
6260
6261 case RID_CILK_SPAWN:
6262 {
6263 cp_lexer_consume_token (parser->lexer);
6264 token = cp_lexer_peek_token (parser->lexer);
6265 if (token->type == CPP_SEMICOLON)
6266 {
6267 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6268 "an expression");
6269 postfix_expression = error_mark_node;
6270 break;
6271 }
6272 else if (!current_function_decl)
6273 {
6274 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6275 "inside a function");
6276 postfix_expression = error_mark_node;
6277 break;
6278 }
6279 else
6280 {
6281 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6282 saved_in_statement = parser->in_statement;
6283 parser->in_statement |= IN_CILK_SPAWN;
6284 }
6285 cfun->calls_cilk_spawn = 1;
6286 postfix_expression =
6287 cp_parser_postfix_expression (parser, false, false,
6288 false, false, &idk);
6289 if (!flag_cilkplus)
6290 {
6291 error_at (token->location, "-fcilkplus must be enabled to use"
6292 " %<_Cilk_spawn%>");
6293 cfun->calls_cilk_spawn = 0;
6294 }
6295 else if (saved_in_statement & IN_CILK_SPAWN)
6296 {
6297 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6298 "are not permitted");
6299 postfix_expression = error_mark_node;
6300 cfun->calls_cilk_spawn = 0;
6301 }
6302 else
6303 {
6304 postfix_expression = build_cilk_spawn (token->location,
6305 postfix_expression);
6306 if (postfix_expression != error_mark_node)
6307 SET_EXPR_LOCATION (postfix_expression, input_location);
6308 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6309 }
6310 break;
6311 }
6312
6313 case RID_BUILTIN_SHUFFLE:
6314 {
6315 vec<tree, va_gc> *vec;
6316 unsigned int i;
6317 tree p;
6318
6319 cp_lexer_consume_token (parser->lexer);
6320 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6321 /*cast_p=*/false, /*allow_expansion_p=*/true,
6322 /*non_constant_p=*/NULL);
6323 if (vec == NULL)
6324 return error_mark_node;
6325
6326 FOR_EACH_VEC_ELT (*vec, i, p)
6327 mark_exp_read (p);
6328
6329 if (vec->length () == 2)
6330 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6331 tf_warning_or_error);
6332 else if (vec->length () == 3)
6333 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6334 tf_warning_or_error);
6335 else
6336 {
6337 error_at (loc, "wrong number of arguments to "
6338 "%<__builtin_shuffle%>");
6339 return error_mark_node;
6340 }
6341 break;
6342 }
6343
6344 default:
6345 {
6346 tree type;
6347
6348 /* If the next thing is a simple-type-specifier, we may be
6349 looking at a functional cast. We could also be looking at
6350 an id-expression. So, we try the functional cast, and if
6351 that doesn't work we fall back to the primary-expression. */
6352 cp_parser_parse_tentatively (parser);
6353 /* Look for the simple-type-specifier. */
6354 ++parser->prevent_constrained_type_specifiers;
6355 type = cp_parser_simple_type_specifier (parser,
6356 /*decl_specs=*/NULL,
6357 CP_PARSER_FLAGS_NONE);
6358 --parser->prevent_constrained_type_specifiers;
6359 /* Parse the cast itself. */
6360 if (!cp_parser_error_occurred (parser))
6361 postfix_expression
6362 = cp_parser_functional_cast (parser, type);
6363 /* If that worked, we're done. */
6364 if (cp_parser_parse_definitely (parser))
6365 break;
6366
6367 /* If the functional-cast didn't work out, try a
6368 compound-literal. */
6369 if (cp_parser_allow_gnu_extensions_p (parser)
6370 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6371 {
6372 tree initializer = NULL_TREE;
6373
6374 cp_parser_parse_tentatively (parser);
6375
6376 /* Avoid calling cp_parser_type_id pointlessly, see comment
6377 in cp_parser_cast_expression about c++/29234. */
6378 if (!cp_parser_compound_literal_p (parser))
6379 cp_parser_simulate_error (parser);
6380 else
6381 {
6382 /* Parse the type. */
6383 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6384 parser->in_type_id_in_expr_p = true;
6385 type = cp_parser_type_id (parser);
6386 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6387 /* Look for the `)'. */
6388 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6389 }
6390
6391 /* If things aren't going well, there's no need to
6392 keep going. */
6393 if (!cp_parser_error_occurred (parser))
6394 {
6395 bool non_constant_p;
6396 /* Parse the brace-enclosed initializer list. */
6397 initializer = cp_parser_braced_list (parser,
6398 &non_constant_p);
6399 }
6400 /* If that worked, we're definitely looking at a
6401 compound-literal expression. */
6402 if (cp_parser_parse_definitely (parser))
6403 {
6404 /* Warn the user that a compound literal is not
6405 allowed in standard C++. */
6406 pedwarn (input_location, OPT_Wpedantic,
6407 "ISO C++ forbids compound-literals");
6408 /* For simplicity, we disallow compound literals in
6409 constant-expressions. We could
6410 allow compound literals of integer type, whose
6411 initializer was a constant, in constant
6412 expressions. Permitting that usage, as a further
6413 extension, would not change the meaning of any
6414 currently accepted programs. (Of course, as
6415 compound literals are not part of ISO C++, the
6416 standard has nothing to say.) */
6417 if (cp_parser_non_integral_constant_expression (parser,
6418 NIC_NCC))
6419 {
6420 postfix_expression = error_mark_node;
6421 break;
6422 }
6423 /* Form the representation of the compound-literal. */
6424 postfix_expression
6425 = finish_compound_literal (type, initializer,
6426 tf_warning_or_error);
6427 break;
6428 }
6429 }
6430
6431 /* It must be a primary-expression. */
6432 postfix_expression
6433 = cp_parser_primary_expression (parser, address_p, cast_p,
6434 /*template_arg_p=*/false,
6435 decltype_p,
6436 &idk);
6437 }
6438 break;
6439 }
6440
6441 /* Note that we don't need to worry about calling build_cplus_new on a
6442 class-valued CALL_EXPR in decltype when it isn't the end of the
6443 postfix-expression; unary_complex_lvalue will take care of that for
6444 all these cases. */
6445
6446 /* Keep looping until the postfix-expression is complete. */
6447 while (true)
6448 {
6449 if (idk == CP_ID_KIND_UNQUALIFIED
6450 && identifier_p (postfix_expression)
6451 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6452 /* It is not a Koenig lookup function call. */
6453 postfix_expression
6454 = unqualified_name_lookup_error (postfix_expression);
6455
6456 /* Peek at the next token. */
6457 token = cp_lexer_peek_token (parser->lexer);
6458
6459 switch (token->type)
6460 {
6461 case CPP_OPEN_SQUARE:
6462 if (cp_next_tokens_can_be_std_attribute_p (parser))
6463 {
6464 cp_parser_error (parser,
6465 "two consecutive %<[%> shall "
6466 "only introduce an attribute");
6467 return error_mark_node;
6468 }
6469 postfix_expression
6470 = cp_parser_postfix_open_square_expression (parser,
6471 postfix_expression,
6472 false,
6473 decltype_p);
6474 idk = CP_ID_KIND_NONE;
6475 is_member_access = false;
6476 break;
6477
6478 case CPP_OPEN_PAREN:
6479 /* postfix-expression ( expression-list [opt] ) */
6480 {
6481 bool koenig_p;
6482 bool is_builtin_constant_p;
6483 bool saved_integral_constant_expression_p = false;
6484 bool saved_non_integral_constant_expression_p = false;
6485 tsubst_flags_t complain = complain_flags (decltype_p);
6486 vec<tree, va_gc> *args;
6487
6488 is_member_access = false;
6489
6490 is_builtin_constant_p
6491 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6492 if (is_builtin_constant_p)
6493 {
6494 /* The whole point of __builtin_constant_p is to allow
6495 non-constant expressions to appear as arguments. */
6496 saved_integral_constant_expression_p
6497 = parser->integral_constant_expression_p;
6498 saved_non_integral_constant_expression_p
6499 = parser->non_integral_constant_expression_p;
6500 parser->integral_constant_expression_p = false;
6501 }
6502 args = (cp_parser_parenthesized_expression_list
6503 (parser, non_attr,
6504 /*cast_p=*/false, /*allow_expansion_p=*/true,
6505 /*non_constant_p=*/NULL,
6506 /*want_literal_zero_p=*/warn_memset_transposed_args));
6507 if (is_builtin_constant_p)
6508 {
6509 parser->integral_constant_expression_p
6510 = saved_integral_constant_expression_p;
6511 parser->non_integral_constant_expression_p
6512 = saved_non_integral_constant_expression_p;
6513 }
6514
6515 if (args == NULL)
6516 {
6517 postfix_expression = error_mark_node;
6518 break;
6519 }
6520
6521 /* Function calls are not permitted in
6522 constant-expressions. */
6523 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6524 && cp_parser_non_integral_constant_expression (parser,
6525 NIC_FUNC_CALL))
6526 {
6527 postfix_expression = error_mark_node;
6528 release_tree_vector (args);
6529 break;
6530 }
6531
6532 koenig_p = false;
6533 if (idk == CP_ID_KIND_UNQUALIFIED
6534 || idk == CP_ID_KIND_TEMPLATE_ID)
6535 {
6536 if (identifier_p (postfix_expression))
6537 {
6538 if (!args->is_empty ())
6539 {
6540 koenig_p = true;
6541 if (!any_type_dependent_arguments_p (args))
6542 postfix_expression
6543 = perform_koenig_lookup (postfix_expression, args,
6544 complain);
6545 }
6546 else
6547 postfix_expression
6548 = unqualified_fn_lookup_error (postfix_expression);
6549 }
6550 /* We do not perform argument-dependent lookup if
6551 normal lookup finds a non-function, in accordance
6552 with the expected resolution of DR 218. */
6553 else if (!args->is_empty ()
6554 && is_overloaded_fn (postfix_expression))
6555 {
6556 tree fn = get_first_fn (postfix_expression);
6557 fn = STRIP_TEMPLATE (fn);
6558
6559 /* Do not do argument dependent lookup if regular
6560 lookup finds a member function or a block-scope
6561 function declaration. [basic.lookup.argdep]/3 */
6562 if (!DECL_FUNCTION_MEMBER_P (fn)
6563 && !DECL_LOCAL_FUNCTION_P (fn))
6564 {
6565 koenig_p = true;
6566 if (!any_type_dependent_arguments_p (args))
6567 postfix_expression
6568 = perform_koenig_lookup (postfix_expression, args,
6569 complain);
6570 }
6571 }
6572 }
6573
6574 if (warn_memset_transposed_args)
6575 {
6576 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6577 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6578 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6579 && vec_safe_length (args) == 3
6580 && integer_zerop ((*args)[2])
6581 && LITERAL_ZERO_P ((*args)[2])
6582 && !(integer_zerop ((*args)[1])
6583 && LITERAL_ZERO_P ((*args)[1])))
6584 warning (OPT_Wmemset_transposed_args,
6585 "%<memset%> used with constant zero length "
6586 "parameter; this could be due to transposed "
6587 "parameters");
6588
6589 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6590 to avoid leaking those into folder and middle-end. */
6591 unsigned int i;
6592 tree arg;
6593 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6594 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6595 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6596 }
6597
6598 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6599 {
6600 tree instance = TREE_OPERAND (postfix_expression, 0);
6601 tree fn = TREE_OPERAND (postfix_expression, 1);
6602
6603 if (processing_template_decl
6604 && (type_dependent_expression_p (instance)
6605 || (!BASELINK_P (fn)
6606 && TREE_CODE (fn) != FIELD_DECL)
6607 || type_dependent_expression_p (fn)
6608 || any_type_dependent_arguments_p (args)))
6609 {
6610 postfix_expression
6611 = build_nt_call_vec (postfix_expression, args);
6612 release_tree_vector (args);
6613 break;
6614 }
6615
6616 if (BASELINK_P (fn))
6617 {
6618 postfix_expression
6619 = (build_new_method_call
6620 (instance, fn, &args, NULL_TREE,
6621 (idk == CP_ID_KIND_QUALIFIED
6622 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6623 : LOOKUP_NORMAL),
6624 /*fn_p=*/NULL,
6625 complain));
6626 }
6627 else
6628 postfix_expression
6629 = finish_call_expr (postfix_expression, &args,
6630 /*disallow_virtual=*/false,
6631 /*koenig_p=*/false,
6632 complain);
6633 }
6634 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6635 || TREE_CODE (postfix_expression) == MEMBER_REF
6636 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6637 postfix_expression = (build_offset_ref_call_from_tree
6638 (postfix_expression, &args,
6639 complain));
6640 else if (idk == CP_ID_KIND_QUALIFIED)
6641 /* A call to a static class member, or a namespace-scope
6642 function. */
6643 postfix_expression
6644 = finish_call_expr (postfix_expression, &args,
6645 /*disallow_virtual=*/true,
6646 koenig_p,
6647 complain);
6648 else
6649 /* All other function calls. */
6650 postfix_expression
6651 = finish_call_expr (postfix_expression, &args,
6652 /*disallow_virtual=*/false,
6653 koenig_p,
6654 complain);
6655
6656 protected_set_expr_location (postfix_expression, token->location);
6657
6658 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6659 idk = CP_ID_KIND_NONE;
6660
6661 release_tree_vector (args);
6662 }
6663 break;
6664
6665 case CPP_DOT:
6666 case CPP_DEREF:
6667 /* postfix-expression . template [opt] id-expression
6668 postfix-expression . pseudo-destructor-name
6669 postfix-expression -> template [opt] id-expression
6670 postfix-expression -> pseudo-destructor-name */
6671
6672 /* Consume the `.' or `->' operator. */
6673 cp_lexer_consume_token (parser->lexer);
6674
6675 postfix_expression
6676 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6677 postfix_expression,
6678 false, &idk, loc);
6679
6680 is_member_access = true;
6681 break;
6682
6683 case CPP_PLUS_PLUS:
6684 /* postfix-expression ++ */
6685 /* Consume the `++' token. */
6686 cp_lexer_consume_token (parser->lexer);
6687 /* Generate a representation for the complete expression. */
6688 postfix_expression
6689 = finish_increment_expr (postfix_expression,
6690 POSTINCREMENT_EXPR);
6691 /* Increments may not appear in constant-expressions. */
6692 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6693 postfix_expression = error_mark_node;
6694 idk = CP_ID_KIND_NONE;
6695 is_member_access = false;
6696 break;
6697
6698 case CPP_MINUS_MINUS:
6699 /* postfix-expression -- */
6700 /* Consume the `--' token. */
6701 cp_lexer_consume_token (parser->lexer);
6702 /* Generate a representation for the complete expression. */
6703 postfix_expression
6704 = finish_increment_expr (postfix_expression,
6705 POSTDECREMENT_EXPR);
6706 /* Decrements may not appear in constant-expressions. */
6707 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6708 postfix_expression = error_mark_node;
6709 idk = CP_ID_KIND_NONE;
6710 is_member_access = false;
6711 break;
6712
6713 default:
6714 if (pidk_return != NULL)
6715 * pidk_return = idk;
6716 if (member_access_only_p)
6717 return is_member_access? postfix_expression : error_mark_node;
6718 else
6719 return postfix_expression;
6720 }
6721 }
6722
6723 /* We should never get here. */
6724 gcc_unreachable ();
6725 return error_mark_node;
6726 }
6727
6728 /* This function parses Cilk Plus array notations. If a normal array expr. is
6729 parsed then the array index is passed back to the caller through *INIT_INDEX
6730 and the function returns a NULL_TREE. If array notation expr. is parsed,
6731 then *INIT_INDEX is ignored by the caller and the function returns
6732 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6733 error_mark_node. */
6734
6735 static tree
6736 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6737 tree array_value)
6738 {
6739 cp_token *token = NULL;
6740 tree length_index, stride = NULL_TREE, value_tree, array_type;
6741 if (!array_value || array_value == error_mark_node)
6742 {
6743 cp_parser_skip_to_end_of_statement (parser);
6744 return error_mark_node;
6745 }
6746
6747 array_type = TREE_TYPE (array_value);
6748
6749 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6750 parser->colon_corrects_to_scope_p = false;
6751 token = cp_lexer_peek_token (parser->lexer);
6752
6753 if (!token)
6754 {
6755 cp_parser_error (parser, "expected %<:%> or numeral");
6756 return error_mark_node;
6757 }
6758 else if (token->type == CPP_COLON)
6759 {
6760 /* Consume the ':'. */
6761 cp_lexer_consume_token (parser->lexer);
6762
6763 /* If we are here, then we have a case like this A[:]. */
6764 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6765 {
6766 cp_parser_error (parser, "expected %<]%>");
6767 cp_parser_skip_to_end_of_statement (parser);
6768 return error_mark_node;
6769 }
6770 *init_index = NULL_TREE;
6771 stride = NULL_TREE;
6772 length_index = NULL_TREE;
6773 }
6774 else
6775 {
6776 /* If we are here, then there are three valid possibilities:
6777 1. ARRAY [ EXP ]
6778 2. ARRAY [ EXP : EXP ]
6779 3. ARRAY [ EXP : EXP : EXP ] */
6780
6781 *init_index = cp_parser_expression (parser);
6782 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6783 {
6784 /* This indicates that we have a normal array expression. */
6785 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6786 return NULL_TREE;
6787 }
6788
6789 /* Consume the ':'. */
6790 cp_lexer_consume_token (parser->lexer);
6791 length_index = cp_parser_expression (parser);
6792 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6793 {
6794 cp_lexer_consume_token (parser->lexer);
6795 stride = cp_parser_expression (parser);
6796 }
6797 }
6798 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6799
6800 if (*init_index == error_mark_node || length_index == error_mark_node
6801 || stride == error_mark_node || array_type == error_mark_node)
6802 {
6803 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6804 cp_lexer_consume_token (parser->lexer);
6805 return error_mark_node;
6806 }
6807 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6808
6809 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6810 length_index, stride, array_type);
6811 return value_tree;
6812 }
6813
6814 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6815 by cp_parser_builtin_offsetof. We're looking for
6816
6817 postfix-expression [ expression ]
6818 postfix-expression [ braced-init-list ] (C++11)
6819
6820 FOR_OFFSETOF is set if we're being called in that context, which
6821 changes how we deal with integer constant expressions. */
6822
6823 static tree
6824 cp_parser_postfix_open_square_expression (cp_parser *parser,
6825 tree postfix_expression,
6826 bool for_offsetof,
6827 bool decltype_p)
6828 {
6829 tree index = NULL_TREE;
6830 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6831 bool saved_greater_than_is_operator_p;
6832
6833 /* Consume the `[' token. */
6834 cp_lexer_consume_token (parser->lexer);
6835
6836 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6837 parser->greater_than_is_operator_p = true;
6838
6839 /* Parse the index expression. */
6840 /* ??? For offsetof, there is a question of what to allow here. If
6841 offsetof is not being used in an integral constant expression context,
6842 then we *could* get the right answer by computing the value at runtime.
6843 If we are in an integral constant expression context, then we might
6844 could accept any constant expression; hard to say without analysis.
6845 Rather than open the barn door too wide right away, allow only integer
6846 constant expressions here. */
6847 if (for_offsetof)
6848 index = cp_parser_constant_expression (parser);
6849 else
6850 {
6851 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6852 {
6853 bool expr_nonconst_p;
6854 cp_lexer_set_source_position (parser->lexer);
6855 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6856 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6857 if (flag_cilkplus
6858 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6859 {
6860 error_at (cp_lexer_peek_token (parser->lexer)->location,
6861 "braced list index is not allowed with array "
6862 "notation");
6863 cp_parser_skip_to_end_of_statement (parser);
6864 return error_mark_node;
6865 }
6866 }
6867 else if (flag_cilkplus)
6868 {
6869 /* Here are have these two options:
6870 ARRAY[EXP : EXP] - Array notation expr with default
6871 stride of 1.
6872 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6873 stride. */
6874 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6875 postfix_expression);
6876 if (an_exp)
6877 return an_exp;
6878 }
6879 else
6880 index = cp_parser_expression (parser);
6881 }
6882
6883 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6884
6885 /* Look for the closing `]'. */
6886 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6887
6888 /* Build the ARRAY_REF. */
6889 postfix_expression = grok_array_decl (loc, postfix_expression,
6890 index, decltype_p);
6891
6892 /* When not doing offsetof, array references are not permitted in
6893 constant-expressions. */
6894 if (!for_offsetof
6895 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6896 postfix_expression = error_mark_node;
6897
6898 return postfix_expression;
6899 }
6900
6901 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6902 by cp_parser_builtin_offsetof. We're looking for
6903
6904 postfix-expression . template [opt] id-expression
6905 postfix-expression . pseudo-destructor-name
6906 postfix-expression -> template [opt] id-expression
6907 postfix-expression -> pseudo-destructor-name
6908
6909 FOR_OFFSETOF is set if we're being called in that context. That sorta
6910 limits what of the above we'll actually accept, but nevermind.
6911 TOKEN_TYPE is the "." or "->" token, which will already have been
6912 removed from the stream. */
6913
6914 static tree
6915 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6916 enum cpp_ttype token_type,
6917 tree postfix_expression,
6918 bool for_offsetof, cp_id_kind *idk,
6919 location_t location)
6920 {
6921 tree name;
6922 bool dependent_p;
6923 bool pseudo_destructor_p;
6924 tree scope = NULL_TREE;
6925
6926 /* If this is a `->' operator, dereference the pointer. */
6927 if (token_type == CPP_DEREF)
6928 postfix_expression = build_x_arrow (location, postfix_expression,
6929 tf_warning_or_error);
6930 /* Check to see whether or not the expression is type-dependent. */
6931 dependent_p = type_dependent_expression_p (postfix_expression);
6932 /* The identifier following the `->' or `.' is not qualified. */
6933 parser->scope = NULL_TREE;
6934 parser->qualifying_scope = NULL_TREE;
6935 parser->object_scope = NULL_TREE;
6936 *idk = CP_ID_KIND_NONE;
6937
6938 /* Enter the scope corresponding to the type of the object
6939 given by the POSTFIX_EXPRESSION. */
6940 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6941 {
6942 scope = TREE_TYPE (postfix_expression);
6943 /* According to the standard, no expression should ever have
6944 reference type. Unfortunately, we do not currently match
6945 the standard in this respect in that our internal representation
6946 of an expression may have reference type even when the standard
6947 says it does not. Therefore, we have to manually obtain the
6948 underlying type here. */
6949 scope = non_reference (scope);
6950 /* The type of the POSTFIX_EXPRESSION must be complete. */
6951 if (scope == unknown_type_node)
6952 {
6953 error_at (location, "%qE does not have class type",
6954 postfix_expression);
6955 scope = NULL_TREE;
6956 }
6957 /* Unlike the object expression in other contexts, *this is not
6958 required to be of complete type for purposes of class member
6959 access (5.2.5) outside the member function body. */
6960 else if (postfix_expression != current_class_ref
6961 && !(processing_template_decl && scope == current_class_type))
6962 scope = complete_type_or_else (scope, NULL_TREE);
6963 /* Let the name lookup machinery know that we are processing a
6964 class member access expression. */
6965 parser->context->object_type = scope;
6966 /* If something went wrong, we want to be able to discern that case,
6967 as opposed to the case where there was no SCOPE due to the type
6968 of expression being dependent. */
6969 if (!scope)
6970 scope = error_mark_node;
6971 /* If the SCOPE was erroneous, make the various semantic analysis
6972 functions exit quickly -- and without issuing additional error
6973 messages. */
6974 if (scope == error_mark_node)
6975 postfix_expression = error_mark_node;
6976 }
6977
6978 /* Assume this expression is not a pseudo-destructor access. */
6979 pseudo_destructor_p = false;
6980
6981 /* If the SCOPE is a scalar type, then, if this is a valid program,
6982 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6983 is type dependent, it can be pseudo-destructor-name or something else.
6984 Try to parse it as pseudo-destructor-name first. */
6985 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6986 {
6987 tree s;
6988 tree type;
6989
6990 cp_parser_parse_tentatively (parser);
6991 /* Parse the pseudo-destructor-name. */
6992 s = NULL_TREE;
6993 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6994 &s, &type);
6995 if (dependent_p
6996 && (cp_parser_error_occurred (parser)
6997 || !SCALAR_TYPE_P (type)))
6998 cp_parser_abort_tentative_parse (parser);
6999 else if (cp_parser_parse_definitely (parser))
7000 {
7001 pseudo_destructor_p = true;
7002 postfix_expression
7003 = finish_pseudo_destructor_expr (postfix_expression,
7004 s, type, location);
7005 }
7006 }
7007
7008 if (!pseudo_destructor_p)
7009 {
7010 /* If the SCOPE is not a scalar type, we are looking at an
7011 ordinary class member access expression, rather than a
7012 pseudo-destructor-name. */
7013 bool template_p;
7014 cp_token *token = cp_lexer_peek_token (parser->lexer);
7015 /* Parse the id-expression. */
7016 name = (cp_parser_id_expression
7017 (parser,
7018 cp_parser_optional_template_keyword (parser),
7019 /*check_dependency_p=*/true,
7020 &template_p,
7021 /*declarator_p=*/false,
7022 /*optional_p=*/false));
7023 /* In general, build a SCOPE_REF if the member name is qualified.
7024 However, if the name was not dependent and has already been
7025 resolved; there is no need to build the SCOPE_REF. For example;
7026
7027 struct X { void f(); };
7028 template <typename T> void f(T* t) { t->X::f(); }
7029
7030 Even though "t" is dependent, "X::f" is not and has been resolved
7031 to a BASELINK; there is no need to include scope information. */
7032
7033 /* But we do need to remember that there was an explicit scope for
7034 virtual function calls. */
7035 if (parser->scope)
7036 *idk = CP_ID_KIND_QUALIFIED;
7037
7038 /* If the name is a template-id that names a type, we will get a
7039 TYPE_DECL here. That is invalid code. */
7040 if (TREE_CODE (name) == TYPE_DECL)
7041 {
7042 error_at (token->location, "invalid use of %qD", name);
7043 postfix_expression = error_mark_node;
7044 }
7045 else
7046 {
7047 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7048 {
7049 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7050 {
7051 error_at (token->location, "%<%D::%D%> is not a class member",
7052 parser->scope, name);
7053 postfix_expression = error_mark_node;
7054 }
7055 else
7056 name = build_qualified_name (/*type=*/NULL_TREE,
7057 parser->scope,
7058 name,
7059 template_p);
7060 parser->scope = NULL_TREE;
7061 parser->qualifying_scope = NULL_TREE;
7062 parser->object_scope = NULL_TREE;
7063 }
7064 if (parser->scope && name && BASELINK_P (name))
7065 adjust_result_of_qualified_name_lookup
7066 (name, parser->scope, scope);
7067 postfix_expression
7068 = finish_class_member_access_expr (postfix_expression, name,
7069 template_p,
7070 tf_warning_or_error);
7071 }
7072 }
7073
7074 /* We no longer need to look up names in the scope of the object on
7075 the left-hand side of the `.' or `->' operator. */
7076 parser->context->object_type = NULL_TREE;
7077
7078 /* Outside of offsetof, these operators may not appear in
7079 constant-expressions. */
7080 if (!for_offsetof
7081 && (cp_parser_non_integral_constant_expression
7082 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7083 postfix_expression = error_mark_node;
7084
7085 return postfix_expression;
7086 }
7087
7088 /* Cache of LITERAL_ZERO_P constants. */
7089
7090 static GTY(()) tree literal_zeros[itk_none];
7091
7092 /* Parse a parenthesized expression-list.
7093
7094 expression-list:
7095 assignment-expression
7096 expression-list, assignment-expression
7097
7098 attribute-list:
7099 expression-list
7100 identifier
7101 identifier, expression-list
7102
7103 CAST_P is true if this expression is the target of a cast.
7104
7105 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7106 argument pack.
7107
7108 Returns a vector of trees. Each element is a representation of an
7109 assignment-expression. NULL is returned if the ( and or ) are
7110 missing. An empty, but allocated, vector is returned on no
7111 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7112 if we are parsing an attribute list for an attribute that wants a
7113 plain identifier argument, normal_attr for an attribute that wants
7114 an expression, or non_attr if we aren't parsing an attribute list. If
7115 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7116 not all of the expressions in the list were constant.
7117 WANT_LITERAL_ZERO_P is true if the caller is interested in
7118 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
7119 immediately, this can be removed. */
7120
7121 static vec<tree, va_gc> *
7122 cp_parser_parenthesized_expression_list (cp_parser* parser,
7123 int is_attribute_list,
7124 bool cast_p,
7125 bool allow_expansion_p,
7126 bool *non_constant_p,
7127 bool want_literal_zero_p)
7128 {
7129 vec<tree, va_gc> *expression_list;
7130 bool fold_expr_p = is_attribute_list != non_attr;
7131 tree identifier = NULL_TREE;
7132 bool saved_greater_than_is_operator_p;
7133
7134 /* Assume all the expressions will be constant. */
7135 if (non_constant_p)
7136 *non_constant_p = false;
7137
7138 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7139 return NULL;
7140
7141 expression_list = make_tree_vector ();
7142
7143 /* Within a parenthesized expression, a `>' token is always
7144 the greater-than operator. */
7145 saved_greater_than_is_operator_p
7146 = parser->greater_than_is_operator_p;
7147 parser->greater_than_is_operator_p = true;
7148
7149 /* Consume expressions until there are no more. */
7150 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7151 while (true)
7152 {
7153 tree expr;
7154
7155 /* At the beginning of attribute lists, check to see if the
7156 next token is an identifier. */
7157 if (is_attribute_list == id_attr
7158 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7159 {
7160 cp_token *token;
7161
7162 /* Consume the identifier. */
7163 token = cp_lexer_consume_token (parser->lexer);
7164 /* Save the identifier. */
7165 identifier = token->u.value;
7166 }
7167 else
7168 {
7169 bool expr_non_constant_p;
7170
7171 /* Parse the next assignment-expression. */
7172 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7173 {
7174 /* A braced-init-list. */
7175 cp_lexer_set_source_position (parser->lexer);
7176 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7177 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7178 if (non_constant_p && expr_non_constant_p)
7179 *non_constant_p = true;
7180 }
7181 else if (non_constant_p)
7182 {
7183 expr = (cp_parser_constant_expression
7184 (parser, /*allow_non_constant_p=*/true,
7185 &expr_non_constant_p));
7186 if (expr_non_constant_p)
7187 *non_constant_p = true;
7188 }
7189 else
7190 {
7191 expr = NULL_TREE;
7192 cp_token *tok = cp_lexer_peek_token (parser->lexer);
7193 switch (tok->type)
7194 {
7195 case CPP_NUMBER:
7196 case CPP_CHAR:
7197 case CPP_WCHAR:
7198 case CPP_CHAR16:
7199 case CPP_CHAR32:
7200 case CPP_UTF8CHAR:
7201 /* If a parameter is literal zero alone, remember it
7202 for -Wmemset-transposed-args warning. */
7203 if (integer_zerop (tok->u.value)
7204 && !TREE_OVERFLOW (tok->u.value)
7205 && want_literal_zero_p
7206 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
7207 == CPP_COMMA
7208 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
7209 == CPP_CLOSE_PAREN))
7210 {
7211 unsigned int i;
7212 for (i = 0; i < itk_none; ++i)
7213 if (TREE_TYPE (tok->u.value) == integer_types[i])
7214 break;
7215 if (i < itk_none && literal_zeros[i])
7216 expr = literal_zeros[i];
7217 else
7218 {
7219 expr = copy_node (tok->u.value);
7220 LITERAL_ZERO_P (expr) = 1;
7221 if (i < itk_none)
7222 literal_zeros[i] = expr;
7223 }
7224 /* Consume the 0 token (or '\0', 0LL etc.). */
7225 cp_lexer_consume_token (parser->lexer);
7226 }
7227 break;
7228 default:
7229 break;
7230 }
7231 if (expr == NULL_TREE)
7232 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7233 cast_p);
7234 }
7235
7236 if (fold_expr_p)
7237 expr = instantiate_non_dependent_expr (expr);
7238
7239 /* If we have an ellipsis, then this is an expression
7240 expansion. */
7241 if (allow_expansion_p
7242 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7243 {
7244 /* Consume the `...'. */
7245 cp_lexer_consume_token (parser->lexer);
7246
7247 /* Build the argument pack. */
7248 expr = make_pack_expansion (expr);
7249 }
7250
7251 /* Add it to the list. We add error_mark_node
7252 expressions to the list, so that we can still tell if
7253 the correct form for a parenthesized expression-list
7254 is found. That gives better errors. */
7255 vec_safe_push (expression_list, expr);
7256
7257 if (expr == error_mark_node)
7258 goto skip_comma;
7259 }
7260
7261 /* After the first item, attribute lists look the same as
7262 expression lists. */
7263 is_attribute_list = non_attr;
7264
7265 get_comma:;
7266 /* If the next token isn't a `,', then we are done. */
7267 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7268 break;
7269
7270 /* Otherwise, consume the `,' and keep going. */
7271 cp_lexer_consume_token (parser->lexer);
7272 }
7273
7274 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7275 {
7276 int ending;
7277
7278 skip_comma:;
7279 /* We try and resync to an unnested comma, as that will give the
7280 user better diagnostics. */
7281 ending = cp_parser_skip_to_closing_parenthesis (parser,
7282 /*recovering=*/true,
7283 /*or_comma=*/true,
7284 /*consume_paren=*/true);
7285 if (ending < 0)
7286 goto get_comma;
7287 if (!ending)
7288 {
7289 parser->greater_than_is_operator_p
7290 = saved_greater_than_is_operator_p;
7291 return NULL;
7292 }
7293 }
7294
7295 parser->greater_than_is_operator_p
7296 = saved_greater_than_is_operator_p;
7297
7298 if (identifier)
7299 vec_safe_insert (expression_list, 0, identifier);
7300
7301 return expression_list;
7302 }
7303
7304 /* Parse a pseudo-destructor-name.
7305
7306 pseudo-destructor-name:
7307 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7308 :: [opt] nested-name-specifier template template-id :: ~ type-name
7309 :: [opt] nested-name-specifier [opt] ~ type-name
7310
7311 If either of the first two productions is used, sets *SCOPE to the
7312 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7313 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7314 or ERROR_MARK_NODE if the parse fails. */
7315
7316 static void
7317 cp_parser_pseudo_destructor_name (cp_parser* parser,
7318 tree object,
7319 tree* scope,
7320 tree* type)
7321 {
7322 bool nested_name_specifier_p;
7323
7324 /* Handle ~auto. */
7325 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7326 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7327 && !type_dependent_expression_p (object))
7328 {
7329 if (cxx_dialect < cxx14)
7330 pedwarn (input_location, 0,
7331 "%<~auto%> only available with "
7332 "-std=c++14 or -std=gnu++14");
7333 cp_lexer_consume_token (parser->lexer);
7334 cp_lexer_consume_token (parser->lexer);
7335 *scope = NULL_TREE;
7336 *type = TREE_TYPE (object);
7337 return;
7338 }
7339
7340 /* Assume that things will not work out. */
7341 *type = error_mark_node;
7342
7343 /* Look for the optional `::' operator. */
7344 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7345 /* Look for the optional nested-name-specifier. */
7346 nested_name_specifier_p
7347 = (cp_parser_nested_name_specifier_opt (parser,
7348 /*typename_keyword_p=*/false,
7349 /*check_dependency_p=*/true,
7350 /*type_p=*/false,
7351 /*is_declaration=*/false)
7352 != NULL_TREE);
7353 /* Now, if we saw a nested-name-specifier, we might be doing the
7354 second production. */
7355 if (nested_name_specifier_p
7356 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7357 {
7358 /* Consume the `template' keyword. */
7359 cp_lexer_consume_token (parser->lexer);
7360 /* Parse the template-id. */
7361 cp_parser_template_id (parser,
7362 /*template_keyword_p=*/true,
7363 /*check_dependency_p=*/false,
7364 class_type,
7365 /*is_declaration=*/true);
7366 /* Look for the `::' token. */
7367 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7368 }
7369 /* If the next token is not a `~', then there might be some
7370 additional qualification. */
7371 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7372 {
7373 /* At this point, we're looking for "type-name :: ~". The type-name
7374 must not be a class-name, since this is a pseudo-destructor. So,
7375 it must be either an enum-name, or a typedef-name -- both of which
7376 are just identifiers. So, we peek ahead to check that the "::"
7377 and "~" tokens are present; if they are not, then we can avoid
7378 calling type_name. */
7379 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7380 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7381 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7382 {
7383 cp_parser_error (parser, "non-scalar type");
7384 return;
7385 }
7386
7387 /* Look for the type-name. */
7388 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7389 if (*scope == error_mark_node)
7390 return;
7391
7392 /* Look for the `::' token. */
7393 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7394 }
7395 else
7396 *scope = NULL_TREE;
7397
7398 /* Look for the `~'. */
7399 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7400
7401 /* Once we see the ~, this has to be a pseudo-destructor. */
7402 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7403 cp_parser_commit_to_topmost_tentative_parse (parser);
7404
7405 /* Look for the type-name again. We are not responsible for
7406 checking that it matches the first type-name. */
7407 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7408 }
7409
7410 /* Parse a unary-expression.
7411
7412 unary-expression:
7413 postfix-expression
7414 ++ cast-expression
7415 -- cast-expression
7416 unary-operator cast-expression
7417 sizeof unary-expression
7418 sizeof ( type-id )
7419 alignof ( type-id ) [C++0x]
7420 new-expression
7421 delete-expression
7422
7423 GNU Extensions:
7424
7425 unary-expression:
7426 __extension__ cast-expression
7427 __alignof__ unary-expression
7428 __alignof__ ( type-id )
7429 alignof unary-expression [C++0x]
7430 __real__ cast-expression
7431 __imag__ cast-expression
7432 && identifier
7433 sizeof ( type-id ) { initializer-list , [opt] }
7434 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7435 __alignof__ ( type-id ) { initializer-list , [opt] }
7436
7437 ADDRESS_P is true iff the unary-expression is appearing as the
7438 operand of the `&' operator. CAST_P is true if this expression is
7439 the target of a cast.
7440
7441 Returns a representation of the expression. */
7442
7443 static tree
7444 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7445 bool address_p, bool cast_p, bool decltype_p)
7446 {
7447 cp_token *token;
7448 enum tree_code unary_operator;
7449
7450 /* Peek at the next token. */
7451 token = cp_lexer_peek_token (parser->lexer);
7452 /* Some keywords give away the kind of expression. */
7453 if (token->type == CPP_KEYWORD)
7454 {
7455 enum rid keyword = token->keyword;
7456
7457 switch (keyword)
7458 {
7459 case RID_ALIGNOF:
7460 case RID_SIZEOF:
7461 {
7462 tree operand, ret;
7463 enum tree_code op;
7464 location_t first_loc;
7465
7466 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7467 /* Consume the token. */
7468 cp_lexer_consume_token (parser->lexer);
7469 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7470 /* Parse the operand. */
7471 operand = cp_parser_sizeof_operand (parser, keyword);
7472
7473 if (TYPE_P (operand))
7474 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7475 else
7476 {
7477 /* ISO C++ defines alignof only with types, not with
7478 expressions. So pedwarn if alignof is used with a non-
7479 type expression. However, __alignof__ is ok. */
7480 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7481 pedwarn (token->location, OPT_Wpedantic,
7482 "ISO C++ does not allow %<alignof%> "
7483 "with a non-type");
7484
7485 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7486 }
7487 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7488 SIZEOF_EXPR with the original operand. */
7489 if (op == SIZEOF_EXPR && ret != error_mark_node)
7490 {
7491 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7492 {
7493 if (!processing_template_decl && TYPE_P (operand))
7494 {
7495 ret = build_min (SIZEOF_EXPR, size_type_node,
7496 build1 (NOP_EXPR, operand,
7497 error_mark_node));
7498 SIZEOF_EXPR_TYPE_P (ret) = 1;
7499 }
7500 else
7501 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7502 TREE_SIDE_EFFECTS (ret) = 0;
7503 TREE_READONLY (ret) = 1;
7504 }
7505 SET_EXPR_LOCATION (ret, first_loc);
7506 }
7507 return ret;
7508 }
7509
7510 case RID_NEW:
7511 return cp_parser_new_expression (parser);
7512
7513 case RID_DELETE:
7514 return cp_parser_delete_expression (parser);
7515
7516 case RID_EXTENSION:
7517 {
7518 /* The saved value of the PEDANTIC flag. */
7519 int saved_pedantic;
7520 tree expr;
7521
7522 /* Save away the PEDANTIC flag. */
7523 cp_parser_extension_opt (parser, &saved_pedantic);
7524 /* Parse the cast-expression. */
7525 expr = cp_parser_simple_cast_expression (parser);
7526 /* Restore the PEDANTIC flag. */
7527 pedantic = saved_pedantic;
7528
7529 return expr;
7530 }
7531
7532 case RID_REALPART:
7533 case RID_IMAGPART:
7534 {
7535 tree expression;
7536
7537 /* Consume the `__real__' or `__imag__' token. */
7538 cp_lexer_consume_token (parser->lexer);
7539 /* Parse the cast-expression. */
7540 expression = cp_parser_simple_cast_expression (parser);
7541 /* Create the complete representation. */
7542 return build_x_unary_op (token->location,
7543 (keyword == RID_REALPART
7544 ? REALPART_EXPR : IMAGPART_EXPR),
7545 expression,
7546 tf_warning_or_error);
7547 }
7548 break;
7549
7550 case RID_TRANSACTION_ATOMIC:
7551 case RID_TRANSACTION_RELAXED:
7552 return cp_parser_transaction_expression (parser, keyword);
7553
7554 case RID_NOEXCEPT:
7555 {
7556 tree expr;
7557 const char *saved_message;
7558 bool saved_integral_constant_expression_p;
7559 bool saved_non_integral_constant_expression_p;
7560 bool saved_greater_than_is_operator_p;
7561
7562 cp_lexer_consume_token (parser->lexer);
7563 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7564
7565 saved_message = parser->type_definition_forbidden_message;
7566 parser->type_definition_forbidden_message
7567 = G_("types may not be defined in %<noexcept%> expressions");
7568
7569 saved_integral_constant_expression_p
7570 = parser->integral_constant_expression_p;
7571 saved_non_integral_constant_expression_p
7572 = parser->non_integral_constant_expression_p;
7573 parser->integral_constant_expression_p = false;
7574
7575 saved_greater_than_is_operator_p
7576 = parser->greater_than_is_operator_p;
7577 parser->greater_than_is_operator_p = true;
7578
7579 ++cp_unevaluated_operand;
7580 ++c_inhibit_evaluation_warnings;
7581 ++cp_noexcept_operand;
7582 expr = cp_parser_expression (parser);
7583 --cp_noexcept_operand;
7584 --c_inhibit_evaluation_warnings;
7585 --cp_unevaluated_operand;
7586
7587 parser->greater_than_is_operator_p
7588 = saved_greater_than_is_operator_p;
7589
7590 parser->integral_constant_expression_p
7591 = saved_integral_constant_expression_p;
7592 parser->non_integral_constant_expression_p
7593 = saved_non_integral_constant_expression_p;
7594
7595 parser->type_definition_forbidden_message = saved_message;
7596
7597 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7598 return finish_noexcept_expr (expr, tf_warning_or_error);
7599 }
7600
7601 default:
7602 break;
7603 }
7604 }
7605
7606 /* Look for the `:: new' and `:: delete', which also signal the
7607 beginning of a new-expression, or delete-expression,
7608 respectively. If the next token is `::', then it might be one of
7609 these. */
7610 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7611 {
7612 enum rid keyword;
7613
7614 /* See if the token after the `::' is one of the keywords in
7615 which we're interested. */
7616 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7617 /* If it's `new', we have a new-expression. */
7618 if (keyword == RID_NEW)
7619 return cp_parser_new_expression (parser);
7620 /* Similarly, for `delete'. */
7621 else if (keyword == RID_DELETE)
7622 return cp_parser_delete_expression (parser);
7623 }
7624
7625 /* Look for a unary operator. */
7626 unary_operator = cp_parser_unary_operator (token);
7627 /* The `++' and `--' operators can be handled similarly, even though
7628 they are not technically unary-operators in the grammar. */
7629 if (unary_operator == ERROR_MARK)
7630 {
7631 if (token->type == CPP_PLUS_PLUS)
7632 unary_operator = PREINCREMENT_EXPR;
7633 else if (token->type == CPP_MINUS_MINUS)
7634 unary_operator = PREDECREMENT_EXPR;
7635 /* Handle the GNU address-of-label extension. */
7636 else if (cp_parser_allow_gnu_extensions_p (parser)
7637 && token->type == CPP_AND_AND)
7638 {
7639 tree identifier;
7640 tree expression;
7641 location_t loc = token->location;
7642
7643 /* Consume the '&&' token. */
7644 cp_lexer_consume_token (parser->lexer);
7645 /* Look for the identifier. */
7646 identifier = cp_parser_identifier (parser);
7647 /* Create an expression representing the address. */
7648 expression = finish_label_address_expr (identifier, loc);
7649 if (cp_parser_non_integral_constant_expression (parser,
7650 NIC_ADDR_LABEL))
7651 expression = error_mark_node;
7652 return expression;
7653 }
7654 }
7655 if (unary_operator != ERROR_MARK)
7656 {
7657 tree cast_expression;
7658 tree expression = error_mark_node;
7659 non_integral_constant non_constant_p = NIC_NONE;
7660 location_t loc = token->location;
7661 tsubst_flags_t complain = complain_flags (decltype_p);
7662
7663 /* Consume the operator token. */
7664 token = cp_lexer_consume_token (parser->lexer);
7665 /* Parse the cast-expression. */
7666 cast_expression
7667 = cp_parser_cast_expression (parser,
7668 unary_operator == ADDR_EXPR,
7669 /*cast_p=*/false,
7670 /*decltype*/false,
7671 pidk);
7672 /* Now, build an appropriate representation. */
7673 switch (unary_operator)
7674 {
7675 case INDIRECT_REF:
7676 non_constant_p = NIC_STAR;
7677 expression = build_x_indirect_ref (loc, cast_expression,
7678 RO_UNARY_STAR,
7679 complain);
7680 break;
7681
7682 case ADDR_EXPR:
7683 non_constant_p = NIC_ADDR;
7684 /* Fall through. */
7685 case BIT_NOT_EXPR:
7686 expression = build_x_unary_op (loc, unary_operator,
7687 cast_expression,
7688 complain);
7689 break;
7690
7691 case PREINCREMENT_EXPR:
7692 case PREDECREMENT_EXPR:
7693 non_constant_p = unary_operator == PREINCREMENT_EXPR
7694 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7695 /* Fall through. */
7696 case UNARY_PLUS_EXPR:
7697 case NEGATE_EXPR:
7698 case TRUTH_NOT_EXPR:
7699 expression = finish_unary_op_expr (loc, unary_operator,
7700 cast_expression, complain);
7701 break;
7702
7703 default:
7704 gcc_unreachable ();
7705 }
7706
7707 if (non_constant_p != NIC_NONE
7708 && cp_parser_non_integral_constant_expression (parser,
7709 non_constant_p))
7710 expression = error_mark_node;
7711
7712 return expression;
7713 }
7714
7715 return cp_parser_postfix_expression (parser, address_p, cast_p,
7716 /*member_access_only_p=*/false,
7717 decltype_p,
7718 pidk);
7719 }
7720
7721 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7722 unary-operator, the corresponding tree code is returned. */
7723
7724 static enum tree_code
7725 cp_parser_unary_operator (cp_token* token)
7726 {
7727 switch (token->type)
7728 {
7729 case CPP_MULT:
7730 return INDIRECT_REF;
7731
7732 case CPP_AND:
7733 return ADDR_EXPR;
7734
7735 case CPP_PLUS:
7736 return UNARY_PLUS_EXPR;
7737
7738 case CPP_MINUS:
7739 return NEGATE_EXPR;
7740
7741 case CPP_NOT:
7742 return TRUTH_NOT_EXPR;
7743
7744 case CPP_COMPL:
7745 return BIT_NOT_EXPR;
7746
7747 default:
7748 return ERROR_MARK;
7749 }
7750 }
7751
7752 /* Parse a new-expression.
7753
7754 new-expression:
7755 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7756 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7757
7758 Returns a representation of the expression. */
7759
7760 static tree
7761 cp_parser_new_expression (cp_parser* parser)
7762 {
7763 bool global_scope_p;
7764 vec<tree, va_gc> *placement;
7765 tree type;
7766 vec<tree, va_gc> *initializer;
7767 tree nelts = NULL_TREE;
7768 tree ret;
7769
7770 /* Look for the optional `::' operator. */
7771 global_scope_p
7772 = (cp_parser_global_scope_opt (parser,
7773 /*current_scope_valid_p=*/false)
7774 != NULL_TREE);
7775 /* Look for the `new' operator. */
7776 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7777 /* There's no easy way to tell a new-placement from the
7778 `( type-id )' construct. */
7779 cp_parser_parse_tentatively (parser);
7780 /* Look for a new-placement. */
7781 placement = cp_parser_new_placement (parser);
7782 /* If that didn't work out, there's no new-placement. */
7783 if (!cp_parser_parse_definitely (parser))
7784 {
7785 if (placement != NULL)
7786 release_tree_vector (placement);
7787 placement = NULL;
7788 }
7789
7790 /* If the next token is a `(', then we have a parenthesized
7791 type-id. */
7792 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7793 {
7794 cp_token *token;
7795 const char *saved_message = parser->type_definition_forbidden_message;
7796
7797 /* Consume the `('. */
7798 cp_lexer_consume_token (parser->lexer);
7799
7800 /* Parse the type-id. */
7801 parser->type_definition_forbidden_message
7802 = G_("types may not be defined in a new-expression");
7803 type = cp_parser_type_id (parser);
7804 parser->type_definition_forbidden_message = saved_message;
7805
7806 /* Look for the closing `)'. */
7807 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7808 token = cp_lexer_peek_token (parser->lexer);
7809 /* There should not be a direct-new-declarator in this production,
7810 but GCC used to allowed this, so we check and emit a sensible error
7811 message for this case. */
7812 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7813 {
7814 error_at (token->location,
7815 "array bound forbidden after parenthesized type-id");
7816 inform (token->location,
7817 "try removing the parentheses around the type-id");
7818 cp_parser_direct_new_declarator (parser);
7819 }
7820 }
7821 /* Otherwise, there must be a new-type-id. */
7822 else
7823 type = cp_parser_new_type_id (parser, &nelts);
7824
7825 /* If the next token is a `(' or '{', then we have a new-initializer. */
7826 cp_token *token = cp_lexer_peek_token (parser->lexer);
7827 if (token->type == CPP_OPEN_PAREN
7828 || token->type == CPP_OPEN_BRACE)
7829 initializer = cp_parser_new_initializer (parser);
7830 else
7831 initializer = NULL;
7832
7833 /* A new-expression may not appear in an integral constant
7834 expression. */
7835 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7836 ret = error_mark_node;
7837 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
7838 of a new-type-id or type-id of a new-expression, the new-expression shall
7839 contain a new-initializer of the form ( assignment-expression )".
7840 Additionally, consistently with the spirit of DR 1467, we want to accept
7841 'new auto { 2 }' too. */
7842 else if (type_uses_auto (type)
7843 && (vec_safe_length (initializer) != 1
7844 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
7845 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
7846 {
7847 error_at (token->location,
7848 "initialization of new-expression for type %<auto%> "
7849 "requires exactly one element");
7850 ret = error_mark_node;
7851 }
7852 else
7853 {
7854 /* Create a representation of the new-expression. */
7855 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7856 tf_warning_or_error);
7857 }
7858
7859 if (placement != NULL)
7860 release_tree_vector (placement);
7861 if (initializer != NULL)
7862 release_tree_vector (initializer);
7863
7864 return ret;
7865 }
7866
7867 /* Parse a new-placement.
7868
7869 new-placement:
7870 ( expression-list )
7871
7872 Returns the same representation as for an expression-list. */
7873
7874 static vec<tree, va_gc> *
7875 cp_parser_new_placement (cp_parser* parser)
7876 {
7877 vec<tree, va_gc> *expression_list;
7878
7879 /* Parse the expression-list. */
7880 expression_list = (cp_parser_parenthesized_expression_list
7881 (parser, non_attr, /*cast_p=*/false,
7882 /*allow_expansion_p=*/true,
7883 /*non_constant_p=*/NULL));
7884
7885 if (expression_list && expression_list->is_empty ())
7886 error ("expected expression-list or type-id");
7887
7888 return expression_list;
7889 }
7890
7891 /* Parse a new-type-id.
7892
7893 new-type-id:
7894 type-specifier-seq new-declarator [opt]
7895
7896 Returns the TYPE allocated. If the new-type-id indicates an array
7897 type, *NELTS is set to the number of elements in the last array
7898 bound; the TYPE will not include the last array bound. */
7899
7900 static tree
7901 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7902 {
7903 cp_decl_specifier_seq type_specifier_seq;
7904 cp_declarator *new_declarator;
7905 cp_declarator *declarator;
7906 cp_declarator *outer_declarator;
7907 const char *saved_message;
7908
7909 /* The type-specifier sequence must not contain type definitions.
7910 (It cannot contain declarations of new types either, but if they
7911 are not definitions we will catch that because they are not
7912 complete.) */
7913 saved_message = parser->type_definition_forbidden_message;
7914 parser->type_definition_forbidden_message
7915 = G_("types may not be defined in a new-type-id");
7916 /* Parse the type-specifier-seq. */
7917 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7918 /*is_trailing_return=*/false,
7919 &type_specifier_seq);
7920 /* Restore the old message. */
7921 parser->type_definition_forbidden_message = saved_message;
7922
7923 if (type_specifier_seq.type == error_mark_node)
7924 return error_mark_node;
7925
7926 /* Parse the new-declarator. */
7927 new_declarator = cp_parser_new_declarator_opt (parser);
7928
7929 /* Determine the number of elements in the last array dimension, if
7930 any. */
7931 *nelts = NULL_TREE;
7932 /* Skip down to the last array dimension. */
7933 declarator = new_declarator;
7934 outer_declarator = NULL;
7935 while (declarator && (declarator->kind == cdk_pointer
7936 || declarator->kind == cdk_ptrmem))
7937 {
7938 outer_declarator = declarator;
7939 declarator = declarator->declarator;
7940 }
7941 while (declarator
7942 && declarator->kind == cdk_array
7943 && declarator->declarator
7944 && declarator->declarator->kind == cdk_array)
7945 {
7946 outer_declarator = declarator;
7947 declarator = declarator->declarator;
7948 }
7949
7950 if (declarator && declarator->kind == cdk_array)
7951 {
7952 *nelts = declarator->u.array.bounds;
7953 if (*nelts == error_mark_node)
7954 *nelts = integer_one_node;
7955
7956 if (outer_declarator)
7957 outer_declarator->declarator = declarator->declarator;
7958 else
7959 new_declarator = NULL;
7960 }
7961
7962 return groktypename (&type_specifier_seq, new_declarator, false);
7963 }
7964
7965 /* Parse an (optional) new-declarator.
7966
7967 new-declarator:
7968 ptr-operator new-declarator [opt]
7969 direct-new-declarator
7970
7971 Returns the declarator. */
7972
7973 static cp_declarator *
7974 cp_parser_new_declarator_opt (cp_parser* parser)
7975 {
7976 enum tree_code code;
7977 tree type, std_attributes = NULL_TREE;
7978 cp_cv_quals cv_quals;
7979
7980 /* We don't know if there's a ptr-operator next, or not. */
7981 cp_parser_parse_tentatively (parser);
7982 /* Look for a ptr-operator. */
7983 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7984 /* If that worked, look for more new-declarators. */
7985 if (cp_parser_parse_definitely (parser))
7986 {
7987 cp_declarator *declarator;
7988
7989 /* Parse another optional declarator. */
7990 declarator = cp_parser_new_declarator_opt (parser);
7991
7992 declarator = cp_parser_make_indirect_declarator
7993 (code, type, cv_quals, declarator, std_attributes);
7994
7995 return declarator;
7996 }
7997
7998 /* If the next token is a `[', there is a direct-new-declarator. */
7999 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8000 return cp_parser_direct_new_declarator (parser);
8001
8002 return NULL;
8003 }
8004
8005 /* Parse a direct-new-declarator.
8006
8007 direct-new-declarator:
8008 [ expression ]
8009 direct-new-declarator [constant-expression]
8010
8011 */
8012
8013 static cp_declarator *
8014 cp_parser_direct_new_declarator (cp_parser* parser)
8015 {
8016 cp_declarator *declarator = NULL;
8017
8018 while (true)
8019 {
8020 tree expression;
8021 cp_token *token;
8022
8023 /* Look for the opening `['. */
8024 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8025
8026 token = cp_lexer_peek_token (parser->lexer);
8027 expression = cp_parser_expression (parser);
8028 /* The standard requires that the expression have integral
8029 type. DR 74 adds enumeration types. We believe that the
8030 real intent is that these expressions be handled like the
8031 expression in a `switch' condition, which also allows
8032 classes with a single conversion to integral or
8033 enumeration type. */
8034 if (!processing_template_decl)
8035 {
8036 expression
8037 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8038 expression,
8039 /*complain=*/true);
8040 if (!expression)
8041 {
8042 error_at (token->location,
8043 "expression in new-declarator must have integral "
8044 "or enumeration type");
8045 expression = error_mark_node;
8046 }
8047 }
8048
8049 /* Look for the closing `]'. */
8050 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8051
8052 /* Add this bound to the declarator. */
8053 declarator = make_array_declarator (declarator, expression);
8054
8055 /* If the next token is not a `[', then there are no more
8056 bounds. */
8057 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8058 break;
8059 }
8060
8061 return declarator;
8062 }
8063
8064 /* Parse a new-initializer.
8065
8066 new-initializer:
8067 ( expression-list [opt] )
8068 braced-init-list
8069
8070 Returns a representation of the expression-list. */
8071
8072 static vec<tree, va_gc> *
8073 cp_parser_new_initializer (cp_parser* parser)
8074 {
8075 vec<tree, va_gc> *expression_list;
8076
8077 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8078 {
8079 tree t;
8080 bool expr_non_constant_p;
8081 cp_lexer_set_source_position (parser->lexer);
8082 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8083 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8084 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8085 expression_list = make_tree_vector_single (t);
8086 }
8087 else
8088 expression_list = (cp_parser_parenthesized_expression_list
8089 (parser, non_attr, /*cast_p=*/false,
8090 /*allow_expansion_p=*/true,
8091 /*non_constant_p=*/NULL));
8092
8093 return expression_list;
8094 }
8095
8096 /* Parse a delete-expression.
8097
8098 delete-expression:
8099 :: [opt] delete cast-expression
8100 :: [opt] delete [ ] cast-expression
8101
8102 Returns a representation of the expression. */
8103
8104 static tree
8105 cp_parser_delete_expression (cp_parser* parser)
8106 {
8107 bool global_scope_p;
8108 bool array_p;
8109 tree expression;
8110
8111 /* Look for the optional `::' operator. */
8112 global_scope_p
8113 = (cp_parser_global_scope_opt (parser,
8114 /*current_scope_valid_p=*/false)
8115 != NULL_TREE);
8116 /* Look for the `delete' keyword. */
8117 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8118 /* See if the array syntax is in use. */
8119 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8120 {
8121 /* Consume the `[' token. */
8122 cp_lexer_consume_token (parser->lexer);
8123 /* Look for the `]' token. */
8124 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8125 /* Remember that this is the `[]' construct. */
8126 array_p = true;
8127 }
8128 else
8129 array_p = false;
8130
8131 /* Parse the cast-expression. */
8132 expression = cp_parser_simple_cast_expression (parser);
8133
8134 /* A delete-expression may not appear in an integral constant
8135 expression. */
8136 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8137 return error_mark_node;
8138
8139 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8140 tf_warning_or_error);
8141 }
8142
8143 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8144 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8145 0 otherwise. */
8146
8147 static int
8148 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8149 {
8150 cp_token *token = cp_lexer_peek_token (parser->lexer);
8151 switch (token->type)
8152 {
8153 case CPP_COMMA:
8154 case CPP_SEMICOLON:
8155 case CPP_QUERY:
8156 case CPP_COLON:
8157 case CPP_CLOSE_SQUARE:
8158 case CPP_CLOSE_PAREN:
8159 case CPP_CLOSE_BRACE:
8160 case CPP_OPEN_BRACE:
8161 case CPP_DOT:
8162 case CPP_DOT_STAR:
8163 case CPP_DEREF:
8164 case CPP_DEREF_STAR:
8165 case CPP_DIV:
8166 case CPP_MOD:
8167 case CPP_LSHIFT:
8168 case CPP_RSHIFT:
8169 case CPP_LESS:
8170 case CPP_GREATER:
8171 case CPP_LESS_EQ:
8172 case CPP_GREATER_EQ:
8173 case CPP_EQ_EQ:
8174 case CPP_NOT_EQ:
8175 case CPP_EQ:
8176 case CPP_MULT_EQ:
8177 case CPP_DIV_EQ:
8178 case CPP_MOD_EQ:
8179 case CPP_PLUS_EQ:
8180 case CPP_MINUS_EQ:
8181 case CPP_RSHIFT_EQ:
8182 case CPP_LSHIFT_EQ:
8183 case CPP_AND_EQ:
8184 case CPP_XOR_EQ:
8185 case CPP_OR_EQ:
8186 case CPP_XOR:
8187 case CPP_OR:
8188 case CPP_OR_OR:
8189 case CPP_EOF:
8190 case CPP_ELLIPSIS:
8191 return 0;
8192
8193 case CPP_OPEN_PAREN:
8194 /* In ((type ()) () the last () isn't a valid cast-expression,
8195 so the whole must be parsed as postfix-expression. */
8196 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8197 != CPP_CLOSE_PAREN;
8198
8199 case CPP_OPEN_SQUARE:
8200 /* '[' may start a primary-expression in obj-c++ and in C++11,
8201 as a lambda-expression, eg, '(void)[]{}'. */
8202 if (cxx_dialect >= cxx11)
8203 return -1;
8204 return c_dialect_objc ();
8205
8206 case CPP_PLUS_PLUS:
8207 case CPP_MINUS_MINUS:
8208 /* '++' and '--' may or may not start a cast-expression:
8209
8210 struct T { void operator++(int); };
8211 void f() { (T())++; }
8212
8213 vs
8214
8215 int a;
8216 (int)++a; */
8217 return -1;
8218
8219 default:
8220 return 1;
8221 }
8222 }
8223
8224 /* Parse a cast-expression.
8225
8226 cast-expression:
8227 unary-expression
8228 ( type-id ) cast-expression
8229
8230 ADDRESS_P is true iff the unary-expression is appearing as the
8231 operand of the `&' operator. CAST_P is true if this expression is
8232 the target of a cast.
8233
8234 Returns a representation of the expression. */
8235
8236 static tree
8237 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8238 bool decltype_p, cp_id_kind * pidk)
8239 {
8240 /* If it's a `(', then we might be looking at a cast. */
8241 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8242 {
8243 tree type = NULL_TREE;
8244 tree expr = NULL_TREE;
8245 int cast_expression = 0;
8246 const char *saved_message;
8247
8248 /* There's no way to know yet whether or not this is a cast.
8249 For example, `(int (3))' is a unary-expression, while `(int)
8250 3' is a cast. So, we resort to parsing tentatively. */
8251 cp_parser_parse_tentatively (parser);
8252 /* Types may not be defined in a cast. */
8253 saved_message = parser->type_definition_forbidden_message;
8254 parser->type_definition_forbidden_message
8255 = G_("types may not be defined in casts");
8256 /* Consume the `('. */
8257 cp_lexer_consume_token (parser->lexer);
8258 /* A very tricky bit is that `(struct S) { 3 }' is a
8259 compound-literal (which we permit in C++ as an extension).
8260 But, that construct is not a cast-expression -- it is a
8261 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8262 is legal; if the compound-literal were a cast-expression,
8263 you'd need an extra set of parentheses.) But, if we parse
8264 the type-id, and it happens to be a class-specifier, then we
8265 will commit to the parse at that point, because we cannot
8266 undo the action that is done when creating a new class. So,
8267 then we cannot back up and do a postfix-expression.
8268
8269 Another tricky case is the following (c++/29234):
8270
8271 struct S { void operator () (); };
8272
8273 void foo ()
8274 {
8275 ( S()() );
8276 }
8277
8278 As a type-id we parse the parenthesized S()() as a function
8279 returning a function, groktypename complains and we cannot
8280 back up in this case either.
8281
8282 Therefore, we scan ahead to the closing `)', and check to see
8283 if the tokens after the `)' can start a cast-expression. Otherwise
8284 we are dealing with an unary-expression, a postfix-expression
8285 or something else.
8286
8287 Yet another tricky case, in C++11, is the following (c++/54891):
8288
8289 (void)[]{};
8290
8291 The issue is that usually, besides the case of lambda-expressions,
8292 the parenthesized type-id cannot be followed by '[', and, eg, we
8293 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8294 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8295 we don't commit, we try a cast-expression, then an unary-expression.
8296
8297 Save tokens so that we can put them back. */
8298 cp_lexer_save_tokens (parser->lexer);
8299
8300 /* We may be looking at a cast-expression. */
8301 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8302 /*consume_paren=*/true))
8303 cast_expression
8304 = cp_parser_tokens_start_cast_expression (parser);
8305
8306 /* Roll back the tokens we skipped. */
8307 cp_lexer_rollback_tokens (parser->lexer);
8308 /* If we aren't looking at a cast-expression, simulate an error so
8309 that the call to cp_parser_error_occurred below returns true. */
8310 if (!cast_expression)
8311 cp_parser_simulate_error (parser);
8312 else
8313 {
8314 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8315 parser->in_type_id_in_expr_p = true;
8316 /* Look for the type-id. */
8317 type = cp_parser_type_id (parser);
8318 /* Look for the closing `)'. */
8319 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8320 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8321 }
8322
8323 /* Restore the saved message. */
8324 parser->type_definition_forbidden_message = saved_message;
8325
8326 /* At this point this can only be either a cast or a
8327 parenthesized ctor such as `(T ())' that looks like a cast to
8328 function returning T. */
8329 if (!cp_parser_error_occurred (parser))
8330 {
8331 /* Only commit if the cast-expression doesn't start with
8332 '++', '--', or '[' in C++11. */
8333 if (cast_expression > 0)
8334 cp_parser_commit_to_topmost_tentative_parse (parser);
8335
8336 expr = cp_parser_cast_expression (parser,
8337 /*address_p=*/false,
8338 /*cast_p=*/true,
8339 /*decltype_p=*/false,
8340 pidk);
8341
8342 if (cp_parser_parse_definitely (parser))
8343 {
8344 /* Warn about old-style casts, if so requested. */
8345 if (warn_old_style_cast
8346 && !in_system_header_at (input_location)
8347 && !VOID_TYPE_P (type)
8348 && current_lang_name != lang_name_c)
8349 warning (OPT_Wold_style_cast, "use of old-style cast");
8350
8351 /* Only type conversions to integral or enumeration types
8352 can be used in constant-expressions. */
8353 if (!cast_valid_in_integral_constant_expression_p (type)
8354 && cp_parser_non_integral_constant_expression (parser,
8355 NIC_CAST))
8356 return error_mark_node;
8357
8358 /* Perform the cast. */
8359 expr = build_c_cast (input_location, type, expr);
8360 return expr;
8361 }
8362 }
8363 else
8364 cp_parser_abort_tentative_parse (parser);
8365 }
8366
8367 /* If we get here, then it's not a cast, so it must be a
8368 unary-expression. */
8369 return cp_parser_unary_expression (parser, pidk, address_p,
8370 cast_p, decltype_p);
8371 }
8372
8373 /* Parse a binary expression of the general form:
8374
8375 pm-expression:
8376 cast-expression
8377 pm-expression .* cast-expression
8378 pm-expression ->* cast-expression
8379
8380 multiplicative-expression:
8381 pm-expression
8382 multiplicative-expression * pm-expression
8383 multiplicative-expression / pm-expression
8384 multiplicative-expression % pm-expression
8385
8386 additive-expression:
8387 multiplicative-expression
8388 additive-expression + multiplicative-expression
8389 additive-expression - multiplicative-expression
8390
8391 shift-expression:
8392 additive-expression
8393 shift-expression << additive-expression
8394 shift-expression >> additive-expression
8395
8396 relational-expression:
8397 shift-expression
8398 relational-expression < shift-expression
8399 relational-expression > shift-expression
8400 relational-expression <= shift-expression
8401 relational-expression >= shift-expression
8402
8403 GNU Extension:
8404
8405 relational-expression:
8406 relational-expression <? shift-expression
8407 relational-expression >? shift-expression
8408
8409 equality-expression:
8410 relational-expression
8411 equality-expression == relational-expression
8412 equality-expression != relational-expression
8413
8414 and-expression:
8415 equality-expression
8416 and-expression & equality-expression
8417
8418 exclusive-or-expression:
8419 and-expression
8420 exclusive-or-expression ^ and-expression
8421
8422 inclusive-or-expression:
8423 exclusive-or-expression
8424 inclusive-or-expression | exclusive-or-expression
8425
8426 logical-and-expression:
8427 inclusive-or-expression
8428 logical-and-expression && inclusive-or-expression
8429
8430 logical-or-expression:
8431 logical-and-expression
8432 logical-or-expression || logical-and-expression
8433
8434 All these are implemented with a single function like:
8435
8436 binary-expression:
8437 simple-cast-expression
8438 binary-expression <token> binary-expression
8439
8440 CAST_P is true if this expression is the target of a cast.
8441
8442 The binops_by_token map is used to get the tree codes for each <token> type.
8443 binary-expressions are associated according to a precedence table. */
8444
8445 #define TOKEN_PRECEDENCE(token) \
8446 (((token->type == CPP_GREATER \
8447 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8448 && !parser->greater_than_is_operator_p) \
8449 ? PREC_NOT_OPERATOR \
8450 : binops_by_token[token->type].prec)
8451
8452 static tree
8453 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8454 bool no_toplevel_fold_p,
8455 bool decltype_p,
8456 enum cp_parser_prec prec,
8457 cp_id_kind * pidk)
8458 {
8459 cp_parser_expression_stack stack;
8460 cp_parser_expression_stack_entry *sp = &stack[0];
8461 cp_parser_expression_stack_entry current;
8462 tree rhs;
8463 cp_token *token;
8464 enum tree_code rhs_type;
8465 enum cp_parser_prec new_prec, lookahead_prec;
8466 tree overload;
8467
8468 /* Parse the first expression. */
8469 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8470 ? TRUTH_NOT_EXPR : ERROR_MARK);
8471 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8472 cast_p, decltype_p, pidk);
8473 current.prec = prec;
8474
8475 if (cp_parser_error_occurred (parser))
8476 return error_mark_node;
8477
8478 for (;;)
8479 {
8480 /* Get an operator token. */
8481 token = cp_lexer_peek_token (parser->lexer);
8482
8483 if (warn_cxx11_compat
8484 && token->type == CPP_RSHIFT
8485 && !parser->greater_than_is_operator_p)
8486 {
8487 if (warning_at (token->location, OPT_Wc__11_compat,
8488 "%<>>%> operator is treated"
8489 " as two right angle brackets in C++11"))
8490 inform (token->location,
8491 "suggest parentheses around %<>>%> expression");
8492 }
8493
8494 new_prec = TOKEN_PRECEDENCE (token);
8495 if (new_prec != PREC_NOT_OPERATOR
8496 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8497 /* This is a fold-expression; handle it later. */
8498 new_prec = PREC_NOT_OPERATOR;
8499
8500 /* Popping an entry off the stack means we completed a subexpression:
8501 - either we found a token which is not an operator (`>' where it is not
8502 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8503 will happen repeatedly;
8504 - or, we found an operator which has lower priority. This is the case
8505 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8506 parsing `3 * 4'. */
8507 if (new_prec <= current.prec)
8508 {
8509 if (sp == stack)
8510 break;
8511 else
8512 goto pop;
8513 }
8514
8515 get_rhs:
8516 current.tree_type = binops_by_token[token->type].tree_type;
8517 current.loc = token->location;
8518
8519 /* We used the operator token. */
8520 cp_lexer_consume_token (parser->lexer);
8521
8522 /* For "false && x" or "true || x", x will never be executed;
8523 disable warnings while evaluating it. */
8524 if (current.tree_type == TRUTH_ANDIF_EXPR)
8525 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8526 else if (current.tree_type == TRUTH_ORIF_EXPR)
8527 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8528
8529 /* Extract another operand. It may be the RHS of this expression
8530 or the LHS of a new, higher priority expression. */
8531 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8532 ? TRUTH_NOT_EXPR : ERROR_MARK);
8533 rhs = cp_parser_simple_cast_expression (parser);
8534
8535 /* Get another operator token. Look up its precedence to avoid
8536 building a useless (immediately popped) stack entry for common
8537 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8538 token = cp_lexer_peek_token (parser->lexer);
8539 lookahead_prec = TOKEN_PRECEDENCE (token);
8540 if (lookahead_prec != PREC_NOT_OPERATOR
8541 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8542 lookahead_prec = PREC_NOT_OPERATOR;
8543 if (lookahead_prec > new_prec)
8544 {
8545 /* ... and prepare to parse the RHS of the new, higher priority
8546 expression. Since precedence levels on the stack are
8547 monotonically increasing, we do not have to care about
8548 stack overflows. */
8549 *sp = current;
8550 ++sp;
8551 current.lhs = rhs;
8552 current.lhs_type = rhs_type;
8553 current.prec = new_prec;
8554 new_prec = lookahead_prec;
8555 goto get_rhs;
8556
8557 pop:
8558 lookahead_prec = new_prec;
8559 /* If the stack is not empty, we have parsed into LHS the right side
8560 (`4' in the example above) of an expression we had suspended.
8561 We can use the information on the stack to recover the LHS (`3')
8562 from the stack together with the tree code (`MULT_EXPR'), and
8563 the precedence of the higher level subexpression
8564 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8565 which will be used to actually build the additive expression. */
8566 rhs = current.lhs;
8567 rhs_type = current.lhs_type;
8568 --sp;
8569 current = *sp;
8570 }
8571
8572 /* Undo the disabling of warnings done above. */
8573 if (current.tree_type == TRUTH_ANDIF_EXPR)
8574 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8575 else if (current.tree_type == TRUTH_ORIF_EXPR)
8576 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8577
8578 if (warn_logical_not_paren
8579 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8580 && current.lhs_type == TRUTH_NOT_EXPR
8581 /* Avoid warning for !!x == y. */
8582 && (TREE_CODE (current.lhs) != NE_EXPR
8583 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8584 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8585 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8586 /* Avoid warning for !b == y where b is boolean. */
8587 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8588 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8589 != BOOLEAN_TYPE))))
8590 /* Avoid warning for !!b == y where b is boolean. */
8591 && (!DECL_P (current.lhs)
8592 || TREE_TYPE (current.lhs) == NULL_TREE
8593 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8594 warn_logical_not_parentheses (current.loc, current.tree_type,
8595 maybe_constant_value (rhs));
8596
8597 overload = NULL;
8598 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8599 ERROR_MARK for everything that is not a binary expression.
8600 This makes warn_about_parentheses miss some warnings that
8601 involve unary operators. For unary expressions we should
8602 pass the correct tree_code unless the unary expression was
8603 surrounded by parentheses.
8604 */
8605 if (no_toplevel_fold_p
8606 && lookahead_prec <= current.prec
8607 && sp == stack)
8608 current.lhs = build2 (current.tree_type,
8609 TREE_CODE_CLASS (current.tree_type)
8610 == tcc_comparison
8611 ? boolean_type_node : TREE_TYPE (current.lhs),
8612 current.lhs, rhs);
8613 else
8614 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8615 current.lhs, current.lhs_type,
8616 rhs, rhs_type, &overload,
8617 complain_flags (decltype_p));
8618 current.lhs_type = current.tree_type;
8619 protected_set_expr_location (current.lhs, current.loc);
8620
8621 /* If the binary operator required the use of an overloaded operator,
8622 then this expression cannot be an integral constant-expression.
8623 An overloaded operator can be used even if both operands are
8624 otherwise permissible in an integral constant-expression if at
8625 least one of the operands is of enumeration type. */
8626
8627 if (overload
8628 && cp_parser_non_integral_constant_expression (parser,
8629 NIC_OVERLOADED))
8630 return error_mark_node;
8631 }
8632
8633 return current.lhs;
8634 }
8635
8636 static tree
8637 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8638 bool no_toplevel_fold_p,
8639 enum cp_parser_prec prec,
8640 cp_id_kind * pidk)
8641 {
8642 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8643 /*decltype*/false, prec, pidk);
8644 }
8645
8646 /* Parse the `? expression : assignment-expression' part of a
8647 conditional-expression. The LOGICAL_OR_EXPR is the
8648 logical-or-expression that started the conditional-expression.
8649 Returns a representation of the entire conditional-expression.
8650
8651 This routine is used by cp_parser_assignment_expression.
8652
8653 ? expression : assignment-expression
8654
8655 GNU Extensions:
8656
8657 ? : assignment-expression */
8658
8659 static tree
8660 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8661 {
8662 tree expr;
8663 tree assignment_expr;
8664 struct cp_token *token;
8665 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8666
8667 /* Consume the `?' token. */
8668 cp_lexer_consume_token (parser->lexer);
8669 token = cp_lexer_peek_token (parser->lexer);
8670 if (cp_parser_allow_gnu_extensions_p (parser)
8671 && token->type == CPP_COLON)
8672 {
8673 pedwarn (token->location, OPT_Wpedantic,
8674 "ISO C++ does not allow ?: with omitted middle operand");
8675 /* Implicit true clause. */
8676 expr = NULL_TREE;
8677 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8678 warn_for_omitted_condop (token->location, logical_or_expr);
8679 }
8680 else
8681 {
8682 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8683 parser->colon_corrects_to_scope_p = false;
8684 /* Parse the expression. */
8685 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8686 expr = cp_parser_expression (parser);
8687 c_inhibit_evaluation_warnings +=
8688 ((logical_or_expr == truthvalue_true_node)
8689 - (logical_or_expr == truthvalue_false_node));
8690 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8691 }
8692
8693 /* The next token should be a `:'. */
8694 cp_parser_require (parser, CPP_COLON, RT_COLON);
8695 /* Parse the assignment-expression. */
8696 assignment_expr = cp_parser_assignment_expression (parser);
8697 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8698
8699 /* Build the conditional-expression. */
8700 return build_x_conditional_expr (loc, logical_or_expr,
8701 expr,
8702 assignment_expr,
8703 tf_warning_or_error);
8704 }
8705
8706 /* Parse an assignment-expression.
8707
8708 assignment-expression:
8709 conditional-expression
8710 logical-or-expression assignment-operator assignment_expression
8711 throw-expression
8712
8713 CAST_P is true if this expression is the target of a cast.
8714 DECLTYPE_P is true if this expression is the operand of decltype.
8715
8716 Returns a representation for the expression. */
8717
8718 static tree
8719 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8720 bool cast_p, bool decltype_p)
8721 {
8722 tree expr;
8723
8724 /* If the next token is the `throw' keyword, then we're looking at
8725 a throw-expression. */
8726 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8727 expr = cp_parser_throw_expression (parser);
8728 /* Otherwise, it must be that we are looking at a
8729 logical-or-expression. */
8730 else
8731 {
8732 /* Parse the binary expressions (logical-or-expression). */
8733 expr = cp_parser_binary_expression (parser, cast_p, false,
8734 decltype_p,
8735 PREC_NOT_OPERATOR, pidk);
8736 /* If the next token is a `?' then we're actually looking at a
8737 conditional-expression. */
8738 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8739 return cp_parser_question_colon_clause (parser, expr);
8740 else
8741 {
8742 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8743
8744 /* If it's an assignment-operator, we're using the second
8745 production. */
8746 enum tree_code assignment_operator
8747 = cp_parser_assignment_operator_opt (parser);
8748 if (assignment_operator != ERROR_MARK)
8749 {
8750 bool non_constant_p;
8751 location_t saved_input_location;
8752
8753 /* Parse the right-hand side of the assignment. */
8754 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8755
8756 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8757 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8758
8759 /* An assignment may not appear in a
8760 constant-expression. */
8761 if (cp_parser_non_integral_constant_expression (parser,
8762 NIC_ASSIGNMENT))
8763 return error_mark_node;
8764 /* Build the assignment expression. Its default
8765 location is the location of the '=' token. */
8766 saved_input_location = input_location;
8767 input_location = loc;
8768 expr = build_x_modify_expr (loc, expr,
8769 assignment_operator,
8770 rhs,
8771 complain_flags (decltype_p));
8772 input_location = saved_input_location;
8773 }
8774 }
8775 }
8776
8777 return expr;
8778 }
8779
8780 /* Parse an (optional) assignment-operator.
8781
8782 assignment-operator: one of
8783 = *= /= %= += -= >>= <<= &= ^= |=
8784
8785 GNU Extension:
8786
8787 assignment-operator: one of
8788 <?= >?=
8789
8790 If the next token is an assignment operator, the corresponding tree
8791 code is returned, and the token is consumed. For example, for
8792 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8793 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8794 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8795 operator, ERROR_MARK is returned. */
8796
8797 static enum tree_code
8798 cp_parser_assignment_operator_opt (cp_parser* parser)
8799 {
8800 enum tree_code op;
8801 cp_token *token;
8802
8803 /* Peek at the next token. */
8804 token = cp_lexer_peek_token (parser->lexer);
8805
8806 switch (token->type)
8807 {
8808 case CPP_EQ:
8809 op = NOP_EXPR;
8810 break;
8811
8812 case CPP_MULT_EQ:
8813 op = MULT_EXPR;
8814 break;
8815
8816 case CPP_DIV_EQ:
8817 op = TRUNC_DIV_EXPR;
8818 break;
8819
8820 case CPP_MOD_EQ:
8821 op = TRUNC_MOD_EXPR;
8822 break;
8823
8824 case CPP_PLUS_EQ:
8825 op = PLUS_EXPR;
8826 break;
8827
8828 case CPP_MINUS_EQ:
8829 op = MINUS_EXPR;
8830 break;
8831
8832 case CPP_RSHIFT_EQ:
8833 op = RSHIFT_EXPR;
8834 break;
8835
8836 case CPP_LSHIFT_EQ:
8837 op = LSHIFT_EXPR;
8838 break;
8839
8840 case CPP_AND_EQ:
8841 op = BIT_AND_EXPR;
8842 break;
8843
8844 case CPP_XOR_EQ:
8845 op = BIT_XOR_EXPR;
8846 break;
8847
8848 case CPP_OR_EQ:
8849 op = BIT_IOR_EXPR;
8850 break;
8851
8852 default:
8853 /* Nothing else is an assignment operator. */
8854 op = ERROR_MARK;
8855 }
8856
8857 /* An operator followed by ... is a fold-expression, handled elsewhere. */
8858 if (op != ERROR_MARK
8859 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8860 op = ERROR_MARK;
8861
8862 /* If it was an assignment operator, consume it. */
8863 if (op != ERROR_MARK)
8864 cp_lexer_consume_token (parser->lexer);
8865
8866 return op;
8867 }
8868
8869 /* Parse an expression.
8870
8871 expression:
8872 assignment-expression
8873 expression , assignment-expression
8874
8875 CAST_P is true if this expression is the target of a cast.
8876 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8877 except possibly parenthesized or on the RHS of a comma (N3276).
8878
8879 Returns a representation of the expression. */
8880
8881 static tree
8882 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8883 bool cast_p, bool decltype_p)
8884 {
8885 tree expression = NULL_TREE;
8886 location_t loc = UNKNOWN_LOCATION;
8887
8888 while (true)
8889 {
8890 tree assignment_expression;
8891
8892 /* Parse the next assignment-expression. */
8893 assignment_expression
8894 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8895
8896 /* We don't create a temporary for a call that is the immediate operand
8897 of decltype or on the RHS of a comma. But when we see a comma, we
8898 need to create a temporary for a call on the LHS. */
8899 if (decltype_p && !processing_template_decl
8900 && TREE_CODE (assignment_expression) == CALL_EXPR
8901 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8902 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8903 assignment_expression
8904 = build_cplus_new (TREE_TYPE (assignment_expression),
8905 assignment_expression, tf_warning_or_error);
8906
8907 /* If this is the first assignment-expression, we can just
8908 save it away. */
8909 if (!expression)
8910 expression = assignment_expression;
8911 else
8912 expression = build_x_compound_expr (loc, expression,
8913 assignment_expression,
8914 complain_flags (decltype_p));
8915 /* If the next token is not a comma, or we're in a fold-expression, then
8916 we are done with the expression. */
8917 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8918 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8919 break;
8920 /* Consume the `,'. */
8921 loc = cp_lexer_peek_token (parser->lexer)->location;
8922 cp_lexer_consume_token (parser->lexer);
8923 /* A comma operator cannot appear in a constant-expression. */
8924 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8925 expression = error_mark_node;
8926 }
8927
8928 return expression;
8929 }
8930
8931 /* Parse a constant-expression.
8932
8933 constant-expression:
8934 conditional-expression
8935
8936 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8937 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8938 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8939 is false, NON_CONSTANT_P should be NULL. */
8940
8941 static tree
8942 cp_parser_constant_expression (cp_parser* parser,
8943 bool allow_non_constant_p,
8944 bool *non_constant_p)
8945 {
8946 bool saved_integral_constant_expression_p;
8947 bool saved_allow_non_integral_constant_expression_p;
8948 bool saved_non_integral_constant_expression_p;
8949 tree expression;
8950
8951 /* It might seem that we could simply parse the
8952 conditional-expression, and then check to see if it were
8953 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8954 one that the compiler can figure out is constant, possibly after
8955 doing some simplifications or optimizations. The standard has a
8956 precise definition of constant-expression, and we must honor
8957 that, even though it is somewhat more restrictive.
8958
8959 For example:
8960
8961 int i[(2, 3)];
8962
8963 is not a legal declaration, because `(2, 3)' is not a
8964 constant-expression. The `,' operator is forbidden in a
8965 constant-expression. However, GCC's constant-folding machinery
8966 will fold this operation to an INTEGER_CST for `3'. */
8967
8968 /* Save the old settings. */
8969 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8970 saved_allow_non_integral_constant_expression_p
8971 = parser->allow_non_integral_constant_expression_p;
8972 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8973 /* We are now parsing a constant-expression. */
8974 parser->integral_constant_expression_p = true;
8975 parser->allow_non_integral_constant_expression_p
8976 = (allow_non_constant_p || cxx_dialect >= cxx11);
8977 parser->non_integral_constant_expression_p = false;
8978 /* Although the grammar says "conditional-expression", we parse an
8979 "assignment-expression", which also permits "throw-expression"
8980 and the use of assignment operators. In the case that
8981 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8982 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8983 actually essential that we look for an assignment-expression.
8984 For example, cp_parser_initializer_clauses uses this function to
8985 determine whether a particular assignment-expression is in fact
8986 constant. */
8987 expression = cp_parser_assignment_expression (parser);
8988 /* Restore the old settings. */
8989 parser->integral_constant_expression_p
8990 = saved_integral_constant_expression_p;
8991 parser->allow_non_integral_constant_expression_p
8992 = saved_allow_non_integral_constant_expression_p;
8993 if (cxx_dialect >= cxx11)
8994 {
8995 /* Require an rvalue constant expression here; that's what our
8996 callers expect. Reference constant expressions are handled
8997 separately in e.g. cp_parser_template_argument. */
8998 bool is_const = potential_rvalue_constant_expression (expression);
8999 parser->non_integral_constant_expression_p = !is_const;
9000 if (!is_const && !allow_non_constant_p)
9001 require_potential_rvalue_constant_expression (expression);
9002 }
9003 if (allow_non_constant_p)
9004 *non_constant_p = parser->non_integral_constant_expression_p;
9005 parser->non_integral_constant_expression_p
9006 = saved_non_integral_constant_expression_p;
9007
9008 return expression;
9009 }
9010
9011 /* Parse __builtin_offsetof.
9012
9013 offsetof-expression:
9014 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9015
9016 offsetof-member-designator:
9017 id-expression
9018 | offsetof-member-designator "." id-expression
9019 | offsetof-member-designator "[" expression "]"
9020 | offsetof-member-designator "->" id-expression */
9021
9022 static tree
9023 cp_parser_builtin_offsetof (cp_parser *parser)
9024 {
9025 int save_ice_p, save_non_ice_p;
9026 tree type, expr;
9027 cp_id_kind dummy;
9028 cp_token *token;
9029
9030 /* We're about to accept non-integral-constant things, but will
9031 definitely yield an integral constant expression. Save and
9032 restore these values around our local parsing. */
9033 save_ice_p = parser->integral_constant_expression_p;
9034 save_non_ice_p = parser->non_integral_constant_expression_p;
9035
9036 /* Consume the "__builtin_offsetof" token. */
9037 cp_lexer_consume_token (parser->lexer);
9038 /* Consume the opening `('. */
9039 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9040 /* Parse the type-id. */
9041 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9042 type = cp_parser_type_id (parser);
9043 /* Look for the `,'. */
9044 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9045 token = cp_lexer_peek_token (parser->lexer);
9046
9047 /* Build the (type *)null that begins the traditional offsetof macro. */
9048 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
9049 tf_warning_or_error);
9050
9051 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9052 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
9053 true, &dummy, token->location);
9054 while (true)
9055 {
9056 token = cp_lexer_peek_token (parser->lexer);
9057 switch (token->type)
9058 {
9059 case CPP_OPEN_SQUARE:
9060 /* offsetof-member-designator "[" expression "]" */
9061 expr = cp_parser_postfix_open_square_expression (parser, expr,
9062 true, false);
9063 break;
9064
9065 case CPP_DEREF:
9066 /* offsetof-member-designator "->" identifier */
9067 expr = grok_array_decl (token->location, expr,
9068 integer_zero_node, false);
9069 /* FALLTHRU */
9070
9071 case CPP_DOT:
9072 /* offsetof-member-designator "." identifier */
9073 cp_lexer_consume_token (parser->lexer);
9074 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9075 expr, true, &dummy,
9076 token->location);
9077 break;
9078
9079 case CPP_CLOSE_PAREN:
9080 /* Consume the ")" token. */
9081 cp_lexer_consume_token (parser->lexer);
9082 goto success;
9083
9084 default:
9085 /* Error. We know the following require will fail, but
9086 that gives the proper error message. */
9087 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9088 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9089 expr = error_mark_node;
9090 goto failure;
9091 }
9092 }
9093
9094 success:
9095 expr = finish_offsetof (expr, loc);
9096
9097 failure:
9098 parser->integral_constant_expression_p = save_ice_p;
9099 parser->non_integral_constant_expression_p = save_non_ice_p;
9100
9101 return expr;
9102 }
9103
9104 /* Parse a trait expression.
9105
9106 Returns a representation of the expression, the underlying type
9107 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9108
9109 static tree
9110 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9111 {
9112 cp_trait_kind kind;
9113 tree type1, type2 = NULL_TREE;
9114 bool binary = false;
9115 bool variadic = false;
9116
9117 switch (keyword)
9118 {
9119 case RID_HAS_NOTHROW_ASSIGN:
9120 kind = CPTK_HAS_NOTHROW_ASSIGN;
9121 break;
9122 case RID_HAS_NOTHROW_CONSTRUCTOR:
9123 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9124 break;
9125 case RID_HAS_NOTHROW_COPY:
9126 kind = CPTK_HAS_NOTHROW_COPY;
9127 break;
9128 case RID_HAS_TRIVIAL_ASSIGN:
9129 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9130 break;
9131 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9132 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9133 break;
9134 case RID_HAS_TRIVIAL_COPY:
9135 kind = CPTK_HAS_TRIVIAL_COPY;
9136 break;
9137 case RID_HAS_TRIVIAL_DESTRUCTOR:
9138 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9139 break;
9140 case RID_HAS_VIRTUAL_DESTRUCTOR:
9141 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9142 break;
9143 case RID_IS_ABSTRACT:
9144 kind = CPTK_IS_ABSTRACT;
9145 break;
9146 case RID_IS_BASE_OF:
9147 kind = CPTK_IS_BASE_OF;
9148 binary = true;
9149 break;
9150 case RID_IS_CLASS:
9151 kind = CPTK_IS_CLASS;
9152 break;
9153 case RID_IS_EMPTY:
9154 kind = CPTK_IS_EMPTY;
9155 break;
9156 case RID_IS_ENUM:
9157 kind = CPTK_IS_ENUM;
9158 break;
9159 case RID_IS_FINAL:
9160 kind = CPTK_IS_FINAL;
9161 break;
9162 case RID_IS_LITERAL_TYPE:
9163 kind = CPTK_IS_LITERAL_TYPE;
9164 break;
9165 case RID_IS_POD:
9166 kind = CPTK_IS_POD;
9167 break;
9168 case RID_IS_POLYMORPHIC:
9169 kind = CPTK_IS_POLYMORPHIC;
9170 break;
9171 case RID_IS_SAME_AS:
9172 kind = CPTK_IS_SAME_AS;
9173 binary = true;
9174 break;
9175 case RID_IS_STD_LAYOUT:
9176 kind = CPTK_IS_STD_LAYOUT;
9177 break;
9178 case RID_IS_TRIVIAL:
9179 kind = CPTK_IS_TRIVIAL;
9180 break;
9181 case RID_IS_TRIVIALLY_ASSIGNABLE:
9182 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9183 binary = true;
9184 break;
9185 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9186 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9187 variadic = true;
9188 break;
9189 case RID_IS_TRIVIALLY_COPYABLE:
9190 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9191 break;
9192 case RID_IS_UNION:
9193 kind = CPTK_IS_UNION;
9194 break;
9195 case RID_UNDERLYING_TYPE:
9196 kind = CPTK_UNDERLYING_TYPE;
9197 break;
9198 case RID_BASES:
9199 kind = CPTK_BASES;
9200 break;
9201 case RID_DIRECT_BASES:
9202 kind = CPTK_DIRECT_BASES;
9203 break;
9204 default:
9205 gcc_unreachable ();
9206 }
9207
9208 /* Consume the token. */
9209 cp_lexer_consume_token (parser->lexer);
9210
9211 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9212
9213 type1 = cp_parser_type_id (parser);
9214
9215 if (type1 == error_mark_node)
9216 return error_mark_node;
9217
9218 if (binary)
9219 {
9220 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9221
9222 type2 = cp_parser_type_id (parser);
9223
9224 if (type2 == error_mark_node)
9225 return error_mark_node;
9226 }
9227 else if (variadic)
9228 {
9229 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9230 {
9231 cp_lexer_consume_token (parser->lexer);
9232 tree elt = cp_parser_type_id (parser);
9233 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9234 {
9235 cp_lexer_consume_token (parser->lexer);
9236 elt = make_pack_expansion (elt);
9237 }
9238 if (elt == error_mark_node)
9239 return error_mark_node;
9240 type2 = tree_cons (NULL_TREE, elt, type2);
9241 }
9242 }
9243
9244 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9245
9246 /* Complete the trait expression, which may mean either processing
9247 the trait expr now or saving it for template instantiation. */
9248 switch(kind)
9249 {
9250 case CPTK_UNDERLYING_TYPE:
9251 return finish_underlying_type (type1);
9252 case CPTK_BASES:
9253 return finish_bases (type1, false);
9254 case CPTK_DIRECT_BASES:
9255 return finish_bases (type1, true);
9256 default:
9257 return finish_trait_expr (kind, type1, type2);
9258 }
9259 }
9260
9261 /* Lambdas that appear in variable initializer or default argument scope
9262 get that in their mangling, so we need to record it. We might as well
9263 use the count for function and namespace scopes as well. */
9264 static GTY(()) tree lambda_scope;
9265 static GTY(()) int lambda_count;
9266 struct GTY(()) tree_int
9267 {
9268 tree t;
9269 int i;
9270 };
9271 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9272
9273 static void
9274 start_lambda_scope (tree decl)
9275 {
9276 tree_int ti;
9277 gcc_assert (decl);
9278 /* Once we're inside a function, we ignore other scopes and just push
9279 the function again so that popping works properly. */
9280 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9281 decl = current_function_decl;
9282 ti.t = lambda_scope;
9283 ti.i = lambda_count;
9284 vec_safe_push (lambda_scope_stack, ti);
9285 if (lambda_scope != decl)
9286 {
9287 /* Don't reset the count if we're still in the same function. */
9288 lambda_scope = decl;
9289 lambda_count = 0;
9290 }
9291 }
9292
9293 static void
9294 record_lambda_scope (tree lambda)
9295 {
9296 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9297 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9298 }
9299
9300 static void
9301 finish_lambda_scope (void)
9302 {
9303 tree_int *p = &lambda_scope_stack->last ();
9304 if (lambda_scope != p->t)
9305 {
9306 lambda_scope = p->t;
9307 lambda_count = p->i;
9308 }
9309 lambda_scope_stack->pop ();
9310 }
9311
9312 /* Parse a lambda expression.
9313
9314 lambda-expression:
9315 lambda-introducer lambda-declarator [opt] compound-statement
9316
9317 Returns a representation of the expression. */
9318
9319 static tree
9320 cp_parser_lambda_expression (cp_parser* parser)
9321 {
9322 tree lambda_expr = build_lambda_expr ();
9323 tree type;
9324 bool ok = true;
9325 cp_token *token = cp_lexer_peek_token (parser->lexer);
9326 cp_token_position start = 0;
9327
9328 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9329
9330 if (cp_unevaluated_operand)
9331 {
9332 if (!token->error_reported)
9333 {
9334 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9335 "lambda-expression in unevaluated context");
9336 token->error_reported = true;
9337 }
9338 ok = false;
9339 }
9340 else if (parser->in_template_argument_list_p)
9341 {
9342 if (!token->error_reported)
9343 {
9344 error_at (token->location, "lambda-expression in template-argument");
9345 token->error_reported = true;
9346 }
9347 ok = false;
9348 }
9349
9350 /* We may be in the middle of deferred access check. Disable
9351 it now. */
9352 push_deferring_access_checks (dk_no_deferred);
9353
9354 cp_parser_lambda_introducer (parser, lambda_expr);
9355
9356 type = begin_lambda_type (lambda_expr);
9357 if (type == error_mark_node)
9358 return error_mark_node;
9359
9360 record_lambda_scope (lambda_expr);
9361
9362 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9363 determine_visibility (TYPE_NAME (type));
9364
9365 /* Now that we've started the type, add the capture fields for any
9366 explicit captures. */
9367 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9368
9369 {
9370 /* Inside the class, surrounding template-parameter-lists do not apply. */
9371 unsigned int saved_num_template_parameter_lists
9372 = parser->num_template_parameter_lists;
9373 unsigned char in_statement = parser->in_statement;
9374 bool in_switch_statement_p = parser->in_switch_statement_p;
9375 bool fully_implicit_function_template_p
9376 = parser->fully_implicit_function_template_p;
9377 tree implicit_template_parms = parser->implicit_template_parms;
9378 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9379 bool auto_is_implicit_function_template_parm_p
9380 = parser->auto_is_implicit_function_template_parm_p;
9381
9382 parser->num_template_parameter_lists = 0;
9383 parser->in_statement = 0;
9384 parser->in_switch_statement_p = false;
9385 parser->fully_implicit_function_template_p = false;
9386 parser->implicit_template_parms = 0;
9387 parser->implicit_template_scope = 0;
9388 parser->auto_is_implicit_function_template_parm_p = false;
9389
9390 /* By virtue of defining a local class, a lambda expression has access to
9391 the private variables of enclosing classes. */
9392
9393 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9394
9395 if (ok)
9396 {
9397 if (!cp_parser_error_occurred (parser)
9398 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9399 && cp_parser_start_tentative_firewall (parser))
9400 start = token;
9401 cp_parser_lambda_body (parser, lambda_expr);
9402 }
9403 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9404 {
9405 if (cp_parser_skip_to_closing_brace (parser))
9406 cp_lexer_consume_token (parser->lexer);
9407 }
9408
9409 /* The capture list was built up in reverse order; fix that now. */
9410 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9411 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9412
9413 if (ok)
9414 maybe_add_lambda_conv_op (type);
9415
9416 type = finish_struct (type, /*attributes=*/NULL_TREE);
9417
9418 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9419 parser->in_statement = in_statement;
9420 parser->in_switch_statement_p = in_switch_statement_p;
9421 parser->fully_implicit_function_template_p
9422 = fully_implicit_function_template_p;
9423 parser->implicit_template_parms = implicit_template_parms;
9424 parser->implicit_template_scope = implicit_template_scope;
9425 parser->auto_is_implicit_function_template_parm_p
9426 = auto_is_implicit_function_template_parm_p;
9427 }
9428
9429 pop_deferring_access_checks ();
9430
9431 /* This field is only used during parsing of the lambda. */
9432 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9433
9434 /* This lambda shouldn't have any proxies left at this point. */
9435 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9436 /* And now that we're done, push proxies for an enclosing lambda. */
9437 insert_pending_capture_proxies ();
9438
9439 if (ok)
9440 lambda_expr = build_lambda_object (lambda_expr);
9441 else
9442 lambda_expr = error_mark_node;
9443
9444 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9445
9446 return lambda_expr;
9447 }
9448
9449 /* Parse the beginning of a lambda expression.
9450
9451 lambda-introducer:
9452 [ lambda-capture [opt] ]
9453
9454 LAMBDA_EXPR is the current representation of the lambda expression. */
9455
9456 static void
9457 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9458 {
9459 /* Need commas after the first capture. */
9460 bool first = true;
9461
9462 /* Eat the leading `['. */
9463 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9464
9465 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9466 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9467 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9468 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9469 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9470 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9471
9472 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9473 {
9474 cp_lexer_consume_token (parser->lexer);
9475 first = false;
9476 }
9477
9478 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9479 {
9480 cp_token* capture_token;
9481 tree capture_id;
9482 tree capture_init_expr;
9483 cp_id_kind idk = CP_ID_KIND_NONE;
9484 bool explicit_init_p = false;
9485
9486 enum capture_kind_type
9487 {
9488 BY_COPY,
9489 BY_REFERENCE
9490 };
9491 enum capture_kind_type capture_kind = BY_COPY;
9492
9493 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9494 {
9495 error ("expected end of capture-list");
9496 return;
9497 }
9498
9499 if (first)
9500 first = false;
9501 else
9502 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9503
9504 /* Possibly capture `this'. */
9505 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9506 {
9507 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9508 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9509 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9510 "with by-copy capture default");
9511 cp_lexer_consume_token (parser->lexer);
9512 add_capture (lambda_expr,
9513 /*id=*/this_identifier,
9514 /*initializer=*/finish_this_expr(),
9515 /*by_reference_p=*/false,
9516 explicit_init_p);
9517 continue;
9518 }
9519
9520 /* Remember whether we want to capture as a reference or not. */
9521 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9522 {
9523 capture_kind = BY_REFERENCE;
9524 cp_lexer_consume_token (parser->lexer);
9525 }
9526
9527 /* Get the identifier. */
9528 capture_token = cp_lexer_peek_token (parser->lexer);
9529 capture_id = cp_parser_identifier (parser);
9530
9531 if (capture_id == error_mark_node)
9532 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9533 delimiters, but I modified this to stop on unnested ']' as well. It
9534 was already changed to stop on unnested '}', so the
9535 "closing_parenthesis" name is no more misleading with my change. */
9536 {
9537 cp_parser_skip_to_closing_parenthesis (parser,
9538 /*recovering=*/true,
9539 /*or_comma=*/true,
9540 /*consume_paren=*/true);
9541 break;
9542 }
9543
9544 /* Find the initializer for this capture. */
9545 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9546 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9547 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9548 {
9549 bool direct, non_constant;
9550 /* An explicit initializer exists. */
9551 if (cxx_dialect < cxx14)
9552 pedwarn (input_location, 0,
9553 "lambda capture initializers "
9554 "only available with -std=c++14 or -std=gnu++14");
9555 capture_init_expr = cp_parser_initializer (parser, &direct,
9556 &non_constant);
9557 explicit_init_p = true;
9558 if (capture_init_expr == NULL_TREE)
9559 {
9560 error ("empty initializer for lambda init-capture");
9561 capture_init_expr = error_mark_node;
9562 }
9563 }
9564 else
9565 {
9566 const char* error_msg;
9567
9568 /* Turn the identifier into an id-expression. */
9569 capture_init_expr
9570 = cp_parser_lookup_name_simple (parser, capture_id,
9571 capture_token->location);
9572
9573 if (capture_init_expr == error_mark_node)
9574 {
9575 unqualified_name_lookup_error (capture_id);
9576 continue;
9577 }
9578 else if (DECL_P (capture_init_expr)
9579 && (!VAR_P (capture_init_expr)
9580 && TREE_CODE (capture_init_expr) != PARM_DECL))
9581 {
9582 error_at (capture_token->location,
9583 "capture of non-variable %qD ",
9584 capture_init_expr);
9585 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9586 "%q#D declared here", capture_init_expr);
9587 continue;
9588 }
9589 if (VAR_P (capture_init_expr)
9590 && decl_storage_duration (capture_init_expr) != dk_auto)
9591 {
9592 if (pedwarn (capture_token->location, 0, "capture of variable "
9593 "%qD with non-automatic storage duration",
9594 capture_init_expr))
9595 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9596 "%q#D declared here", capture_init_expr);
9597 continue;
9598 }
9599
9600 capture_init_expr
9601 = finish_id_expression
9602 (capture_id,
9603 capture_init_expr,
9604 parser->scope,
9605 &idk,
9606 /*integral_constant_expression_p=*/false,
9607 /*allow_non_integral_constant_expression_p=*/false,
9608 /*non_integral_constant_expression_p=*/NULL,
9609 /*template_p=*/false,
9610 /*done=*/true,
9611 /*address_p=*/false,
9612 /*template_arg_p=*/false,
9613 &error_msg,
9614 capture_token->location);
9615
9616 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9617 {
9618 cp_lexer_consume_token (parser->lexer);
9619 capture_init_expr = make_pack_expansion (capture_init_expr);
9620 }
9621 else
9622 check_for_bare_parameter_packs (capture_init_expr);
9623 }
9624
9625 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9626 && !explicit_init_p)
9627 {
9628 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9629 && capture_kind == BY_COPY)
9630 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9631 "of %qD redundant with by-copy capture default",
9632 capture_id);
9633 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9634 && capture_kind == BY_REFERENCE)
9635 pedwarn (capture_token->location, 0, "explicit by-reference "
9636 "capture of %qD redundant with by-reference capture "
9637 "default", capture_id);
9638 }
9639
9640 add_capture (lambda_expr,
9641 capture_id,
9642 capture_init_expr,
9643 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9644 explicit_init_p);
9645 }
9646
9647 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9648 }
9649
9650 /* Parse the (optional) middle of a lambda expression.
9651
9652 lambda-declarator:
9653 < template-parameter-list [opt] >
9654 ( parameter-declaration-clause [opt] )
9655 attribute-specifier [opt]
9656 mutable [opt]
9657 exception-specification [opt]
9658 lambda-return-type-clause [opt]
9659
9660 LAMBDA_EXPR is the current representation of the lambda expression. */
9661
9662 static bool
9663 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9664 {
9665 /* 5.1.1.4 of the standard says:
9666 If a lambda-expression does not include a lambda-declarator, it is as if
9667 the lambda-declarator were ().
9668 This means an empty parameter list, no attributes, and no exception
9669 specification. */
9670 tree param_list = void_list_node;
9671 tree attributes = NULL_TREE;
9672 tree exception_spec = NULL_TREE;
9673 tree template_param_list = NULL_TREE;
9674 tree tx_qual = NULL_TREE;
9675
9676 /* The template-parameter-list is optional, but must begin with
9677 an opening angle if present. */
9678 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9679 {
9680 if (cxx_dialect < cxx14)
9681 pedwarn (parser->lexer->next_token->location, 0,
9682 "lambda templates are only available with "
9683 "-std=c++14 or -std=gnu++14");
9684
9685 cp_lexer_consume_token (parser->lexer);
9686
9687 template_param_list = cp_parser_template_parameter_list (parser);
9688
9689 cp_parser_skip_to_end_of_template_parameter_list (parser);
9690
9691 /* We just processed one more parameter list. */
9692 ++parser->num_template_parameter_lists;
9693 }
9694
9695 /* The parameter-declaration-clause is optional (unless
9696 template-parameter-list was given), but must begin with an
9697 opening parenthesis if present. */
9698 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9699 {
9700 cp_lexer_consume_token (parser->lexer);
9701
9702 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9703
9704 /* Parse parameters. */
9705 param_list = cp_parser_parameter_declaration_clause (parser);
9706
9707 /* Default arguments shall not be specified in the
9708 parameter-declaration-clause of a lambda-declarator. */
9709 for (tree t = param_list; t; t = TREE_CHAIN (t))
9710 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9711 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9712 "default argument specified for lambda parameter");
9713
9714 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9715
9716 attributes = cp_parser_attributes_opt (parser);
9717
9718 /* Parse optional `mutable' keyword. */
9719 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9720 {
9721 cp_lexer_consume_token (parser->lexer);
9722 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9723 }
9724
9725 tx_qual = cp_parser_tx_qualifier_opt (parser);
9726
9727 /* Parse optional exception specification. */
9728 exception_spec = cp_parser_exception_specification_opt (parser);
9729
9730 /* Parse optional trailing return type. */
9731 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9732 {
9733 cp_lexer_consume_token (parser->lexer);
9734 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9735 = cp_parser_trailing_type_id (parser);
9736 }
9737
9738 /* The function parameters must be in scope all the way until after the
9739 trailing-return-type in case of decltype. */
9740 pop_bindings_and_leave_scope ();
9741 }
9742 else if (template_param_list != NULL_TREE) // generate diagnostic
9743 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9744
9745 /* Create the function call operator.
9746
9747 Messing with declarators like this is no uglier than building up the
9748 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9749 other code. */
9750 {
9751 cp_decl_specifier_seq return_type_specs;
9752 cp_declarator* declarator;
9753 tree fco;
9754 int quals;
9755 void *p;
9756
9757 clear_decl_specs (&return_type_specs);
9758 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9759 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9760 else
9761 /* Maybe we will deduce the return type later. */
9762 return_type_specs.type = make_auto ();
9763
9764 p = obstack_alloc (&declarator_obstack, 0);
9765
9766 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9767 sfk_none);
9768
9769 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9770 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9771 declarator = make_call_declarator (declarator, param_list, quals,
9772 VIRT_SPEC_UNSPECIFIED,
9773 REF_QUAL_NONE,
9774 tx_qual,
9775 exception_spec,
9776 /*late_return_type=*/NULL_TREE,
9777 /*requires_clause*/NULL_TREE);
9778 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9779
9780 fco = grokmethod (&return_type_specs,
9781 declarator,
9782 attributes);
9783 if (fco != error_mark_node)
9784 {
9785 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9786 DECL_ARTIFICIAL (fco) = 1;
9787 /* Give the object parameter a different name. */
9788 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9789 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9790 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9791 }
9792 if (template_param_list)
9793 {
9794 fco = finish_member_template_decl (fco);
9795 finish_template_decl (template_param_list);
9796 --parser->num_template_parameter_lists;
9797 }
9798 else if (parser->fully_implicit_function_template_p)
9799 fco = finish_fully_implicit_template (parser, fco);
9800
9801 finish_member_declaration (fco);
9802
9803 obstack_free (&declarator_obstack, p);
9804
9805 return (fco != error_mark_node);
9806 }
9807 }
9808
9809 /* Parse the body of a lambda expression, which is simply
9810
9811 compound-statement
9812
9813 but which requires special handling.
9814 LAMBDA_EXPR is the current representation of the lambda expression. */
9815
9816 static void
9817 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9818 {
9819 bool nested = (current_function_decl != NULL_TREE);
9820 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9821 if (nested)
9822 push_function_context ();
9823 else
9824 /* Still increment function_depth so that we don't GC in the
9825 middle of an expression. */
9826 ++function_depth;
9827 vec<tree> omp_privatization_save;
9828 save_omp_privatization_clauses (omp_privatization_save);
9829 /* Clear this in case we're in the middle of a default argument. */
9830 parser->local_variables_forbidden_p = false;
9831
9832 /* Finish the function call operator
9833 - class_specifier
9834 + late_parsing_for_member
9835 + function_definition_after_declarator
9836 + ctor_initializer_opt_and_function_body */
9837 {
9838 tree fco = lambda_function (lambda_expr);
9839 tree body;
9840 bool done = false;
9841 tree compound_stmt;
9842 tree cap;
9843
9844 /* Let the front end know that we are going to be defining this
9845 function. */
9846 start_preparsed_function (fco,
9847 NULL_TREE,
9848 SF_PRE_PARSED | SF_INCLASS_INLINE);
9849
9850 start_lambda_scope (fco);
9851 body = begin_function_body ();
9852
9853 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9854 goto out;
9855
9856 /* Push the proxies for any explicit captures. */
9857 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9858 cap = TREE_CHAIN (cap))
9859 build_capture_proxy (TREE_PURPOSE (cap));
9860
9861 compound_stmt = begin_compound_stmt (0);
9862
9863 /* 5.1.1.4 of the standard says:
9864 If a lambda-expression does not include a trailing-return-type, it
9865 is as if the trailing-return-type denotes the following type:
9866 * if the compound-statement is of the form
9867 { return attribute-specifier [opt] expression ; }
9868 the type of the returned expression after lvalue-to-rvalue
9869 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9870 (_conv.array_ 4.2), and function-to-pointer conversion
9871 (_conv.func_ 4.3);
9872 * otherwise, void. */
9873
9874 /* In a lambda that has neither a lambda-return-type-clause
9875 nor a deducible form, errors should be reported for return statements
9876 in the body. Since we used void as the placeholder return type, parsing
9877 the body as usual will give such desired behavior. */
9878 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9879 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9880 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9881 {
9882 tree expr = NULL_TREE;
9883 cp_id_kind idk = CP_ID_KIND_NONE;
9884
9885 /* Parse tentatively in case there's more after the initial return
9886 statement. */
9887 cp_parser_parse_tentatively (parser);
9888
9889 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9890
9891 expr = cp_parser_expression (parser, &idk);
9892
9893 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9894 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9895
9896 if (cp_parser_parse_definitely (parser))
9897 {
9898 if (!processing_template_decl)
9899 {
9900 tree type = lambda_return_type (expr);
9901 apply_deduced_return_type (fco, type);
9902 if (type == error_mark_node)
9903 expr = error_mark_node;
9904 }
9905
9906 /* Will get error here if type not deduced yet. */
9907 finish_return_stmt (expr);
9908
9909 done = true;
9910 }
9911 }
9912
9913 if (!done)
9914 {
9915 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9916 cp_parser_label_declaration (parser);
9917 cp_parser_statement_seq_opt (parser, NULL_TREE);
9918 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9919 }
9920
9921 finish_compound_stmt (compound_stmt);
9922
9923 out:
9924 finish_function_body (body);
9925 finish_lambda_scope ();
9926
9927 /* Finish the function and generate code for it if necessary. */
9928 tree fn = finish_function (/*inline*/2);
9929
9930 /* Only expand if the call op is not a template. */
9931 if (!DECL_TEMPLATE_INFO (fco))
9932 expand_or_defer_fn (fn);
9933 }
9934
9935 restore_omp_privatization_clauses (omp_privatization_save);
9936 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9937 if (nested)
9938 pop_function_context();
9939 else
9940 --function_depth;
9941 }
9942
9943 /* Statements [gram.stmt.stmt] */
9944
9945 /* Parse a statement.
9946
9947 statement:
9948 labeled-statement
9949 expression-statement
9950 compound-statement
9951 selection-statement
9952 iteration-statement
9953 jump-statement
9954 declaration-statement
9955 try-block
9956
9957 C++11:
9958
9959 statement:
9960 labeled-statement
9961 attribute-specifier-seq (opt) expression-statement
9962 attribute-specifier-seq (opt) compound-statement
9963 attribute-specifier-seq (opt) selection-statement
9964 attribute-specifier-seq (opt) iteration-statement
9965 attribute-specifier-seq (opt) jump-statement
9966 declaration-statement
9967 attribute-specifier-seq (opt) try-block
9968
9969 TM Extension:
9970
9971 statement:
9972 atomic-statement
9973
9974 IN_COMPOUND is true when the statement is nested inside a
9975 cp_parser_compound_statement; this matters for certain pragmas.
9976
9977 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9978 is a (possibly labeled) if statement which is not enclosed in braces
9979 and has an else clause. This is used to implement -Wparentheses.
9980
9981 CHAIN is a vector of if-else-if conditions. */
9982
9983 static void
9984 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9985 bool in_compound, bool *if_p, vec<tree> *chain)
9986 {
9987 tree statement, std_attrs = NULL_TREE;
9988 cp_token *token;
9989 location_t statement_location, attrs_location;
9990
9991 restart:
9992 if (if_p != NULL)
9993 *if_p = false;
9994 /* There is no statement yet. */
9995 statement = NULL_TREE;
9996
9997 saved_token_sentinel saved_tokens (parser->lexer);
9998 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9999 if (c_dialect_objc ())
10000 /* In obj-c++, seeing '[[' might be the either the beginning of
10001 c++11 attributes, or a nested objc-message-expression. So
10002 let's parse the c++11 attributes tentatively. */
10003 cp_parser_parse_tentatively (parser);
10004 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10005 if (c_dialect_objc ())
10006 {
10007 if (!cp_parser_parse_definitely (parser))
10008 std_attrs = NULL_TREE;
10009 }
10010
10011 /* Peek at the next token. */
10012 token = cp_lexer_peek_token (parser->lexer);
10013 /* Remember the location of the first token in the statement. */
10014 statement_location = token->location;
10015 /* If this is a keyword, then that will often determine what kind of
10016 statement we have. */
10017 if (token->type == CPP_KEYWORD)
10018 {
10019 enum rid keyword = token->keyword;
10020
10021 switch (keyword)
10022 {
10023 case RID_CASE:
10024 case RID_DEFAULT:
10025 /* Looks like a labeled-statement with a case label.
10026 Parse the label, and then use tail recursion to parse
10027 the statement. */
10028 cp_parser_label_for_labeled_statement (parser, std_attrs);
10029 goto restart;
10030
10031 case RID_IF:
10032 case RID_SWITCH:
10033 statement = cp_parser_selection_statement (parser, if_p, chain);
10034 break;
10035
10036 case RID_WHILE:
10037 case RID_DO:
10038 case RID_FOR:
10039 statement = cp_parser_iteration_statement (parser, false);
10040 break;
10041
10042 case RID_CILK_FOR:
10043 if (!flag_cilkplus)
10044 {
10045 error_at (cp_lexer_peek_token (parser->lexer)->location,
10046 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10047 cp_lexer_consume_token (parser->lexer);
10048 statement = error_mark_node;
10049 }
10050 else
10051 statement = cp_parser_cilk_for (parser, integer_zero_node);
10052 break;
10053
10054 case RID_BREAK:
10055 case RID_CONTINUE:
10056 case RID_RETURN:
10057 case RID_GOTO:
10058 statement = cp_parser_jump_statement (parser);
10059 break;
10060
10061 case RID_CILK_SYNC:
10062 cp_lexer_consume_token (parser->lexer);
10063 if (flag_cilkplus)
10064 {
10065 tree sync_expr = build_cilk_sync ();
10066 SET_EXPR_LOCATION (sync_expr,
10067 token->location);
10068 statement = finish_expr_stmt (sync_expr);
10069 }
10070 else
10071 {
10072 error_at (token->location, "-fcilkplus must be enabled to use"
10073 " %<_Cilk_sync%>");
10074 statement = error_mark_node;
10075 }
10076 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10077 break;
10078
10079 /* Objective-C++ exception-handling constructs. */
10080 case RID_AT_TRY:
10081 case RID_AT_CATCH:
10082 case RID_AT_FINALLY:
10083 case RID_AT_SYNCHRONIZED:
10084 case RID_AT_THROW:
10085 statement = cp_parser_objc_statement (parser);
10086 break;
10087
10088 case RID_TRY:
10089 statement = cp_parser_try_block (parser);
10090 break;
10091
10092 case RID_NAMESPACE:
10093 /* This must be a namespace alias definition. */
10094 cp_parser_declaration_statement (parser);
10095 return;
10096
10097 case RID_TRANSACTION_ATOMIC:
10098 case RID_TRANSACTION_RELAXED:
10099 case RID_SYNCHRONIZED:
10100 case RID_ATOMIC_NOEXCEPT:
10101 case RID_ATOMIC_CANCEL:
10102 statement = cp_parser_transaction (parser, token);
10103 break;
10104 case RID_TRANSACTION_CANCEL:
10105 statement = cp_parser_transaction_cancel (parser);
10106 break;
10107
10108 default:
10109 /* It might be a keyword like `int' that can start a
10110 declaration-statement. */
10111 break;
10112 }
10113 }
10114 else if (token->type == CPP_NAME)
10115 {
10116 /* If the next token is a `:', then we are looking at a
10117 labeled-statement. */
10118 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10119 if (token->type == CPP_COLON)
10120 {
10121 /* Looks like a labeled-statement with an ordinary label.
10122 Parse the label, and then use tail recursion to parse
10123 the statement. */
10124
10125 cp_parser_label_for_labeled_statement (parser, std_attrs);
10126 goto restart;
10127 }
10128 }
10129 /* Anything that starts with a `{' must be a compound-statement. */
10130 else if (token->type == CPP_OPEN_BRACE)
10131 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10132 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10133 a statement all its own. */
10134 else if (token->type == CPP_PRAGMA)
10135 {
10136 /* Only certain OpenMP pragmas are attached to statements, and thus
10137 are considered statements themselves. All others are not. In
10138 the context of a compound, accept the pragma as a "statement" and
10139 return so that we can check for a close brace. Otherwise we
10140 require a real statement and must go back and read one. */
10141 if (in_compound)
10142 cp_parser_pragma (parser, pragma_compound);
10143 else if (!cp_parser_pragma (parser, pragma_stmt))
10144 goto restart;
10145 return;
10146 }
10147 else if (token->type == CPP_EOF)
10148 {
10149 cp_parser_error (parser, "expected statement");
10150 return;
10151 }
10152
10153 /* Everything else must be a declaration-statement or an
10154 expression-statement. Try for the declaration-statement
10155 first, unless we are looking at a `;', in which case we know that
10156 we have an expression-statement. */
10157 if (!statement)
10158 {
10159 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10160 {
10161 if (std_attrs != NULL_TREE)
10162 {
10163 /* Attributes should be parsed as part of the the
10164 declaration, so let's un-parse them. */
10165 saved_tokens.rollback();
10166 std_attrs = NULL_TREE;
10167 }
10168
10169 cp_parser_parse_tentatively (parser);
10170 /* Try to parse the declaration-statement. */
10171 cp_parser_declaration_statement (parser);
10172 /* If that worked, we're done. */
10173 if (cp_parser_parse_definitely (parser))
10174 return;
10175 }
10176 /* Look for an expression-statement instead. */
10177 statement = cp_parser_expression_statement (parser, in_statement_expr);
10178 }
10179
10180 /* Set the line number for the statement. */
10181 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10182 SET_EXPR_LOCATION (statement, statement_location);
10183
10184 /* Note that for now, we don't do anything with c++11 statements
10185 parsed at this level. */
10186 if (std_attrs != NULL_TREE)
10187 warning_at (attrs_location,
10188 OPT_Wattributes,
10189 "attributes at the beginning of statement are ignored");
10190 }
10191
10192 /* Parse the label for a labeled-statement, i.e.
10193
10194 identifier :
10195 case constant-expression :
10196 default :
10197
10198 GNU Extension:
10199 case constant-expression ... constant-expression : statement
10200
10201 When a label is parsed without errors, the label is added to the
10202 parse tree by the finish_* functions, so this function doesn't
10203 have to return the label. */
10204
10205 static void
10206 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10207 {
10208 cp_token *token;
10209 tree label = NULL_TREE;
10210 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10211
10212 /* The next token should be an identifier. */
10213 token = cp_lexer_peek_token (parser->lexer);
10214 if (token->type != CPP_NAME
10215 && token->type != CPP_KEYWORD)
10216 {
10217 cp_parser_error (parser, "expected labeled-statement");
10218 return;
10219 }
10220
10221 parser->colon_corrects_to_scope_p = false;
10222 switch (token->keyword)
10223 {
10224 case RID_CASE:
10225 {
10226 tree expr, expr_hi;
10227 cp_token *ellipsis;
10228
10229 /* Consume the `case' token. */
10230 cp_lexer_consume_token (parser->lexer);
10231 /* Parse the constant-expression. */
10232 expr = cp_parser_constant_expression (parser);
10233 if (check_for_bare_parameter_packs (expr))
10234 expr = error_mark_node;
10235
10236 ellipsis = cp_lexer_peek_token (parser->lexer);
10237 if (ellipsis->type == CPP_ELLIPSIS)
10238 {
10239 /* Consume the `...' token. */
10240 cp_lexer_consume_token (parser->lexer);
10241 expr_hi = cp_parser_constant_expression (parser);
10242 if (check_for_bare_parameter_packs (expr_hi))
10243 expr_hi = error_mark_node;
10244
10245 /* We don't need to emit warnings here, as the common code
10246 will do this for us. */
10247 }
10248 else
10249 expr_hi = NULL_TREE;
10250
10251 if (parser->in_switch_statement_p)
10252 finish_case_label (token->location, expr, expr_hi);
10253 else
10254 error_at (token->location,
10255 "case label %qE not within a switch statement",
10256 expr);
10257 }
10258 break;
10259
10260 case RID_DEFAULT:
10261 /* Consume the `default' token. */
10262 cp_lexer_consume_token (parser->lexer);
10263
10264 if (parser->in_switch_statement_p)
10265 finish_case_label (token->location, NULL_TREE, NULL_TREE);
10266 else
10267 error_at (token->location, "case label not within a switch statement");
10268 break;
10269
10270 default:
10271 /* Anything else must be an ordinary label. */
10272 label = finish_label_stmt (cp_parser_identifier (parser));
10273 break;
10274 }
10275
10276 /* Require the `:' token. */
10277 cp_parser_require (parser, CPP_COLON, RT_COLON);
10278
10279 /* An ordinary label may optionally be followed by attributes.
10280 However, this is only permitted if the attributes are then
10281 followed by a semicolon. This is because, for backward
10282 compatibility, when parsing
10283 lab: __attribute__ ((unused)) int i;
10284 we want the attribute to attach to "i", not "lab". */
10285 if (label != NULL_TREE
10286 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10287 {
10288 tree attrs;
10289 cp_parser_parse_tentatively (parser);
10290 attrs = cp_parser_gnu_attributes_opt (parser);
10291 if (attrs == NULL_TREE
10292 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10293 cp_parser_abort_tentative_parse (parser);
10294 else if (!cp_parser_parse_definitely (parser))
10295 ;
10296 else
10297 attributes = chainon (attributes, attrs);
10298 }
10299
10300 if (attributes != NULL_TREE)
10301 cplus_decl_attributes (&label, attributes, 0);
10302
10303 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10304 }
10305
10306 /* Parse an expression-statement.
10307
10308 expression-statement:
10309 expression [opt] ;
10310
10311 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10312 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10313 indicates whether this expression-statement is part of an
10314 expression statement. */
10315
10316 static tree
10317 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10318 {
10319 tree statement = NULL_TREE;
10320 cp_token *token = cp_lexer_peek_token (parser->lexer);
10321
10322 /* If the next token is a ';', then there is no expression
10323 statement. */
10324 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10325 {
10326 statement = cp_parser_expression (parser);
10327 if (statement == error_mark_node
10328 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10329 {
10330 cp_parser_skip_to_end_of_block_or_statement (parser);
10331 return error_mark_node;
10332 }
10333 }
10334
10335 /* Give a helpful message for "A<T>::type t;" and the like. */
10336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10337 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10338 {
10339 if (TREE_CODE (statement) == SCOPE_REF)
10340 error_at (token->location, "need %<typename%> before %qE because "
10341 "%qT is a dependent scope",
10342 statement, TREE_OPERAND (statement, 0));
10343 else if (is_overloaded_fn (statement)
10344 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10345 {
10346 /* A::A a; */
10347 tree fn = get_first_fn (statement);
10348 error_at (token->location,
10349 "%<%T::%D%> names the constructor, not the type",
10350 DECL_CONTEXT (fn), DECL_NAME (fn));
10351 }
10352 }
10353
10354 /* Consume the final `;'. */
10355 cp_parser_consume_semicolon_at_end_of_statement (parser);
10356
10357 if (in_statement_expr
10358 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10359 /* This is the final expression statement of a statement
10360 expression. */
10361 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10362 else if (statement)
10363 statement = finish_expr_stmt (statement);
10364
10365 return statement;
10366 }
10367
10368 /* Parse a compound-statement.
10369
10370 compound-statement:
10371 { statement-seq [opt] }
10372
10373 GNU extension:
10374
10375 compound-statement:
10376 { label-declaration-seq [opt] statement-seq [opt] }
10377
10378 label-declaration-seq:
10379 label-declaration
10380 label-declaration-seq label-declaration
10381
10382 Returns a tree representing the statement. */
10383
10384 static tree
10385 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10386 int bcs_flags, bool function_body)
10387 {
10388 tree compound_stmt;
10389
10390 /* Consume the `{'. */
10391 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10392 return error_mark_node;
10393 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10394 && !function_body && cxx_dialect < cxx14)
10395 pedwarn (input_location, OPT_Wpedantic,
10396 "compound-statement in constexpr function");
10397 /* Begin the compound-statement. */
10398 compound_stmt = begin_compound_stmt (bcs_flags);
10399 /* If the next keyword is `__label__' we have a label declaration. */
10400 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10401 cp_parser_label_declaration (parser);
10402 /* Parse an (optional) statement-seq. */
10403 cp_parser_statement_seq_opt (parser, in_statement_expr);
10404 /* Finish the compound-statement. */
10405 finish_compound_stmt (compound_stmt);
10406 /* Consume the `}'. */
10407 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10408
10409 return compound_stmt;
10410 }
10411
10412 /* Parse an (optional) statement-seq.
10413
10414 statement-seq:
10415 statement
10416 statement-seq [opt] statement */
10417
10418 static void
10419 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10420 {
10421 /* Scan statements until there aren't any more. */
10422 while (true)
10423 {
10424 cp_token *token = cp_lexer_peek_token (parser->lexer);
10425
10426 /* If we are looking at a `}', then we have run out of
10427 statements; the same is true if we have reached the end
10428 of file, or have stumbled upon a stray '@end'. */
10429 if (token->type == CPP_CLOSE_BRACE
10430 || token->type == CPP_EOF
10431 || token->type == CPP_PRAGMA_EOL
10432 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10433 break;
10434
10435 /* If we are in a compound statement and find 'else' then
10436 something went wrong. */
10437 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10438 {
10439 if (parser->in_statement & IN_IF_STMT)
10440 break;
10441 else
10442 {
10443 token = cp_lexer_consume_token (parser->lexer);
10444 error_at (token->location, "%<else%> without a previous %<if%>");
10445 }
10446 }
10447
10448 /* Parse the statement. */
10449 cp_parser_statement (parser, in_statement_expr, true, NULL);
10450 }
10451 }
10452
10453 /* Parse a selection-statement.
10454
10455 selection-statement:
10456 if ( condition ) statement
10457 if ( condition ) statement else statement
10458 switch ( condition ) statement
10459
10460 Returns the new IF_STMT or SWITCH_STMT.
10461
10462 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10463 is a (possibly labeled) if statement which is not enclosed in
10464 braces and has an else clause. This is used to implement
10465 -Wparentheses.
10466
10467 CHAIN is a vector of if-else-if conditions. This is used to implement
10468 -Wduplicated-cond. */
10469
10470 static tree
10471 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
10472 vec<tree> *chain)
10473 {
10474 cp_token *token;
10475 enum rid keyword;
10476 token_indent_info guard_tinfo;
10477
10478 if (if_p != NULL)
10479 *if_p = false;
10480
10481 /* Peek at the next token. */
10482 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10483 guard_tinfo = get_token_indent_info (token);
10484
10485 /* See what kind of keyword it is. */
10486 keyword = token->keyword;
10487 switch (keyword)
10488 {
10489 case RID_IF:
10490 case RID_SWITCH:
10491 {
10492 tree statement;
10493 tree condition;
10494
10495 /* Look for the `('. */
10496 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10497 {
10498 cp_parser_skip_to_end_of_statement (parser);
10499 return error_mark_node;
10500 }
10501
10502 /* Begin the selection-statement. */
10503 if (keyword == RID_IF)
10504 statement = begin_if_stmt ();
10505 else
10506 statement = begin_switch_stmt ();
10507
10508 /* Parse the condition. */
10509 condition = cp_parser_condition (parser);
10510 /* Look for the `)'. */
10511 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10512 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10513 /*consume_paren=*/true);
10514
10515 if (keyword == RID_IF)
10516 {
10517 bool nested_if;
10518 unsigned char in_statement;
10519
10520 /* Add the condition. */
10521 finish_if_stmt_cond (condition, statement);
10522
10523 if (warn_duplicated_cond)
10524 warn_duplicated_cond_add_or_warn (token->location, condition,
10525 &chain);
10526
10527 /* Parse the then-clause. */
10528 in_statement = parser->in_statement;
10529 parser->in_statement |= IN_IF_STMT;
10530 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10531 guard_tinfo);
10532 parser->in_statement = in_statement;
10533
10534 finish_then_clause (statement);
10535
10536 /* If the next token is `else', parse the else-clause. */
10537 if (cp_lexer_next_token_is_keyword (parser->lexer,
10538 RID_ELSE))
10539 {
10540 guard_tinfo
10541 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
10542 /* Consume the `else' keyword. */
10543 cp_lexer_consume_token (parser->lexer);
10544 if (warn_duplicated_cond)
10545 {
10546 if (cp_lexer_next_token_is_keyword (parser->lexer,
10547 RID_IF)
10548 && chain == NULL)
10549 {
10550 /* We've got "if (COND) else if (COND2)". Start
10551 the condition chain and add COND as the first
10552 element. */
10553 chain = new vec<tree> ();
10554 if (!CONSTANT_CLASS_P (condition)
10555 && !TREE_SIDE_EFFECTS (condition))
10556 {
10557 /* Wrap it in a NOP_EXPR so that we can set the
10558 location of the condition. */
10559 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
10560 condition);
10561 SET_EXPR_LOCATION (e, token->location);
10562 chain->safe_push (e);
10563 }
10564 }
10565 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
10566 RID_IF))
10567 {
10568 /* This is if-else without subsequent if. Zap the
10569 condition chain; we would have already warned at
10570 this point. */
10571 delete chain;
10572 chain = NULL;
10573 }
10574 }
10575 begin_else_clause (statement);
10576 /* Parse the else-clause. */
10577 cp_parser_implicitly_scoped_statement (parser, NULL,
10578 guard_tinfo, chain);
10579
10580 finish_else_clause (statement);
10581
10582 /* If we are currently parsing a then-clause, then
10583 IF_P will not be NULL. We set it to true to
10584 indicate that this if statement has an else clause.
10585 This may trigger the Wparentheses warning below
10586 when we get back up to the parent if statement. */
10587 if (if_p != NULL)
10588 *if_p = true;
10589 }
10590 else
10591 {
10592 /* This if statement does not have an else clause. If
10593 NESTED_IF is true, then the then-clause is an if
10594 statement which does have an else clause. We warn
10595 about the potential ambiguity. */
10596 if (nested_if)
10597 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10598 "suggest explicit braces to avoid ambiguous"
10599 " %<else%>");
10600 if (warn_duplicated_cond)
10601 {
10602 /* We don't need the condition chain anymore. */
10603 delete chain;
10604 chain = NULL;
10605 }
10606 }
10607
10608 /* Now we're all done with the if-statement. */
10609 finish_if_stmt (statement);
10610 }
10611 else
10612 {
10613 bool in_switch_statement_p;
10614 unsigned char in_statement;
10615
10616 /* Add the condition. */
10617 finish_switch_cond (condition, statement);
10618
10619 /* Parse the body of the switch-statement. */
10620 in_switch_statement_p = parser->in_switch_statement_p;
10621 in_statement = parser->in_statement;
10622 parser->in_switch_statement_p = true;
10623 parser->in_statement |= IN_SWITCH_STMT;
10624 cp_parser_implicitly_scoped_statement (parser, NULL,
10625 guard_tinfo);
10626 parser->in_switch_statement_p = in_switch_statement_p;
10627 parser->in_statement = in_statement;
10628
10629 /* Now we're all done with the switch-statement. */
10630 finish_switch_stmt (statement);
10631 }
10632
10633 return statement;
10634 }
10635 break;
10636
10637 default:
10638 cp_parser_error (parser, "expected selection-statement");
10639 return error_mark_node;
10640 }
10641 }
10642
10643 /* Parse a condition.
10644
10645 condition:
10646 expression
10647 type-specifier-seq declarator = initializer-clause
10648 type-specifier-seq declarator braced-init-list
10649
10650 GNU Extension:
10651
10652 condition:
10653 type-specifier-seq declarator asm-specification [opt]
10654 attributes [opt] = assignment-expression
10655
10656 Returns the expression that should be tested. */
10657
10658 static tree
10659 cp_parser_condition (cp_parser* parser)
10660 {
10661 cp_decl_specifier_seq type_specifiers;
10662 const char *saved_message;
10663 int declares_class_or_enum;
10664
10665 /* Try the declaration first. */
10666 cp_parser_parse_tentatively (parser);
10667 /* New types are not allowed in the type-specifier-seq for a
10668 condition. */
10669 saved_message = parser->type_definition_forbidden_message;
10670 parser->type_definition_forbidden_message
10671 = G_("types may not be defined in conditions");
10672 /* Parse the type-specifier-seq. */
10673 cp_parser_decl_specifier_seq (parser,
10674 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10675 &type_specifiers,
10676 &declares_class_or_enum);
10677 /* Restore the saved message. */
10678 parser->type_definition_forbidden_message = saved_message;
10679 /* If all is well, we might be looking at a declaration. */
10680 if (!cp_parser_error_occurred (parser))
10681 {
10682 tree decl;
10683 tree asm_specification;
10684 tree attributes;
10685 cp_declarator *declarator;
10686 tree initializer = NULL_TREE;
10687
10688 /* Parse the declarator. */
10689 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10690 /*ctor_dtor_or_conv_p=*/NULL,
10691 /*parenthesized_p=*/NULL,
10692 /*member_p=*/false,
10693 /*friend_p=*/false);
10694 /* Parse the attributes. */
10695 attributes = cp_parser_attributes_opt (parser);
10696 /* Parse the asm-specification. */
10697 asm_specification = cp_parser_asm_specification_opt (parser);
10698 /* If the next token is not an `=' or '{', then we might still be
10699 looking at an expression. For example:
10700
10701 if (A(a).x)
10702
10703 looks like a decl-specifier-seq and a declarator -- but then
10704 there is no `=', so this is an expression. */
10705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10706 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10707 cp_parser_simulate_error (parser);
10708
10709 /* If we did see an `=' or '{', then we are looking at a declaration
10710 for sure. */
10711 if (cp_parser_parse_definitely (parser))
10712 {
10713 tree pushed_scope;
10714 bool non_constant_p;
10715 bool flags = LOOKUP_ONLYCONVERTING;
10716
10717 /* Create the declaration. */
10718 decl = start_decl (declarator, &type_specifiers,
10719 /*initialized_p=*/true,
10720 attributes, /*prefix_attributes=*/NULL_TREE,
10721 &pushed_scope);
10722
10723 /* Parse the initializer. */
10724 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10725 {
10726 initializer = cp_parser_braced_list (parser, &non_constant_p);
10727 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10728 flags = 0;
10729 }
10730 else
10731 {
10732 /* Consume the `='. */
10733 cp_parser_require (parser, CPP_EQ, RT_EQ);
10734 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10735 }
10736 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10737 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10738
10739 /* Process the initializer. */
10740 cp_finish_decl (decl,
10741 initializer, !non_constant_p,
10742 asm_specification,
10743 flags);
10744
10745 if (pushed_scope)
10746 pop_scope (pushed_scope);
10747
10748 return convert_from_reference (decl);
10749 }
10750 }
10751 /* If we didn't even get past the declarator successfully, we are
10752 definitely not looking at a declaration. */
10753 else
10754 cp_parser_abort_tentative_parse (parser);
10755
10756 /* Otherwise, we are looking at an expression. */
10757 return cp_parser_expression (parser);
10758 }
10759
10760 /* Parses a for-statement or range-for-statement until the closing ')',
10761 not included. */
10762
10763 static tree
10764 cp_parser_for (cp_parser *parser, bool ivdep)
10765 {
10766 tree init, scope, decl;
10767 bool is_range_for;
10768
10769 /* Begin the for-statement. */
10770 scope = begin_for_scope (&init);
10771
10772 /* Parse the initialization. */
10773 is_range_for = cp_parser_for_init_statement (parser, &decl);
10774
10775 if (is_range_for)
10776 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10777 else
10778 return cp_parser_c_for (parser, scope, init, ivdep);
10779 }
10780
10781 static tree
10782 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10783 {
10784 /* Normal for loop */
10785 tree condition = NULL_TREE;
10786 tree expression = NULL_TREE;
10787 tree stmt;
10788
10789 stmt = begin_for_stmt (scope, init);
10790 /* The for-init-statement has already been parsed in
10791 cp_parser_for_init_statement, so no work is needed here. */
10792 finish_for_init_stmt (stmt);
10793
10794 /* If there's a condition, process it. */
10795 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10796 condition = cp_parser_condition (parser);
10797 else if (ivdep)
10798 {
10799 cp_parser_error (parser, "missing loop condition in loop with "
10800 "%<GCC ivdep%> pragma");
10801 condition = error_mark_node;
10802 }
10803 finish_for_cond (condition, stmt, ivdep);
10804 /* Look for the `;'. */
10805 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10806
10807 /* If there's an expression, process it. */
10808 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10809 expression = cp_parser_expression (parser);
10810 finish_for_expr (expression, stmt);
10811
10812 return stmt;
10813 }
10814
10815 /* Tries to parse a range-based for-statement:
10816
10817 range-based-for:
10818 decl-specifier-seq declarator : expression
10819
10820 The decl-specifier-seq declarator and the `:' are already parsed by
10821 cp_parser_for_init_statement. If processing_template_decl it returns a
10822 newly created RANGE_FOR_STMT; if not, it is converted to a
10823 regular FOR_STMT. */
10824
10825 static tree
10826 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10827 bool ivdep)
10828 {
10829 tree stmt, range_expr;
10830
10831 /* Get the range declaration momentarily out of the way so that
10832 the range expression doesn't clash with it. */
10833 if (range_decl != error_mark_node)
10834 pop_binding (DECL_NAME (range_decl), range_decl);
10835
10836 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10837 {
10838 bool expr_non_constant_p;
10839 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10840 }
10841 else
10842 range_expr = cp_parser_expression (parser);
10843
10844 /* Put the range declaration back into scope. */
10845 if (range_decl != error_mark_node)
10846 push_binding (DECL_NAME (range_decl), range_decl, current_binding_level);
10847
10848 /* If in template, STMT is converted to a normal for-statement
10849 at instantiation. If not, it is done just ahead. */
10850 if (processing_template_decl)
10851 {
10852 if (check_for_bare_parameter_packs (range_expr))
10853 range_expr = error_mark_node;
10854 stmt = begin_range_for_stmt (scope, init);
10855 if (ivdep)
10856 RANGE_FOR_IVDEP (stmt) = 1;
10857 finish_range_for_decl (stmt, range_decl, range_expr);
10858 if (!type_dependent_expression_p (range_expr)
10859 /* do_auto_deduction doesn't mess with template init-lists. */
10860 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10861 do_range_for_auto_deduction (range_decl, range_expr);
10862 }
10863 else
10864 {
10865 stmt = begin_for_stmt (scope, init);
10866 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10867 }
10868 return stmt;
10869 }
10870
10871 /* Subroutine of cp_convert_range_for: given the initializer expression,
10872 builds up the range temporary. */
10873
10874 static tree
10875 build_range_temp (tree range_expr)
10876 {
10877 tree range_type, range_temp;
10878
10879 /* Find out the type deduced by the declaration
10880 `auto &&__range = range_expr'. */
10881 range_type = cp_build_reference_type (make_auto (), true);
10882 range_type = do_auto_deduction (range_type, range_expr,
10883 type_uses_auto (range_type));
10884
10885 /* Create the __range variable. */
10886 range_temp = build_decl (input_location, VAR_DECL,
10887 get_identifier ("__for_range"), range_type);
10888 TREE_USED (range_temp) = 1;
10889 DECL_ARTIFICIAL (range_temp) = 1;
10890
10891 return range_temp;
10892 }
10893
10894 /* Used by cp_parser_range_for in template context: we aren't going to
10895 do a full conversion yet, but we still need to resolve auto in the
10896 type of the for-range-declaration if present. This is basically
10897 a shortcut version of cp_convert_range_for. */
10898
10899 static void
10900 do_range_for_auto_deduction (tree decl, tree range_expr)
10901 {
10902 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10903 if (auto_node)
10904 {
10905 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10906 range_temp = convert_from_reference (build_range_temp (range_expr));
10907 iter_type = (cp_parser_perform_range_for_lookup
10908 (range_temp, &begin_dummy, &end_dummy));
10909 if (iter_type)
10910 {
10911 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10912 iter_type);
10913 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10914 tf_warning_or_error);
10915 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10916 iter_decl, auto_node);
10917 }
10918 }
10919 }
10920
10921 /* Converts a range-based for-statement into a normal
10922 for-statement, as per the definition.
10923
10924 for (RANGE_DECL : RANGE_EXPR)
10925 BLOCK
10926
10927 should be equivalent to:
10928
10929 {
10930 auto &&__range = RANGE_EXPR;
10931 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10932 __begin != __end;
10933 ++__begin)
10934 {
10935 RANGE_DECL = *__begin;
10936 BLOCK
10937 }
10938 }
10939
10940 If RANGE_EXPR is an array:
10941 BEGIN_EXPR = __range
10942 END_EXPR = __range + ARRAY_SIZE(__range)
10943 Else if RANGE_EXPR has a member 'begin' or 'end':
10944 BEGIN_EXPR = __range.begin()
10945 END_EXPR = __range.end()
10946 Else:
10947 BEGIN_EXPR = begin(__range)
10948 END_EXPR = end(__range);
10949
10950 If __range has a member 'begin' but not 'end', or vice versa, we must
10951 still use the second alternative (it will surely fail, however).
10952 When calling begin()/end() in the third alternative we must use
10953 argument dependent lookup, but always considering 'std' as an associated
10954 namespace. */
10955
10956 tree
10957 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10958 bool ivdep)
10959 {
10960 tree begin, end;
10961 tree iter_type, begin_expr, end_expr;
10962 tree condition, expression;
10963
10964 if (range_decl == error_mark_node || range_expr == error_mark_node)
10965 /* If an error happened previously do nothing or else a lot of
10966 unhelpful errors would be issued. */
10967 begin_expr = end_expr = iter_type = error_mark_node;
10968 else
10969 {
10970 tree range_temp;
10971
10972 if (VAR_P (range_expr)
10973 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10974 /* Can't bind a reference to an array of runtime bound. */
10975 range_temp = range_expr;
10976 else
10977 {
10978 range_temp = build_range_temp (range_expr);
10979 pushdecl (range_temp);
10980 cp_finish_decl (range_temp, range_expr,
10981 /*is_constant_init*/false, NULL_TREE,
10982 LOOKUP_ONLYCONVERTING);
10983 range_temp = convert_from_reference (range_temp);
10984 }
10985 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10986 &begin_expr, &end_expr);
10987 }
10988
10989 /* The new for initialization statement. */
10990 begin = build_decl (input_location, VAR_DECL,
10991 get_identifier ("__for_begin"), iter_type);
10992 TREE_USED (begin) = 1;
10993 DECL_ARTIFICIAL (begin) = 1;
10994 pushdecl (begin);
10995 cp_finish_decl (begin, begin_expr,
10996 /*is_constant_init*/false, NULL_TREE,
10997 LOOKUP_ONLYCONVERTING);
10998
10999 end = build_decl (input_location, VAR_DECL,
11000 get_identifier ("__for_end"), iter_type);
11001 TREE_USED (end) = 1;
11002 DECL_ARTIFICIAL (end) = 1;
11003 pushdecl (end);
11004 cp_finish_decl (end, end_expr,
11005 /*is_constant_init*/false, NULL_TREE,
11006 LOOKUP_ONLYCONVERTING);
11007
11008 finish_for_init_stmt (statement);
11009
11010 /* The new for condition. */
11011 condition = build_x_binary_op (input_location, NE_EXPR,
11012 begin, ERROR_MARK,
11013 end, ERROR_MARK,
11014 NULL, tf_warning_or_error);
11015 finish_for_cond (condition, statement, ivdep);
11016
11017 /* The new increment expression. */
11018 expression = finish_unary_op_expr (input_location,
11019 PREINCREMENT_EXPR, begin,
11020 tf_warning_or_error);
11021 finish_for_expr (expression, statement);
11022
11023 /* The declaration is initialized with *__begin inside the loop body. */
11024 cp_finish_decl (range_decl,
11025 build_x_indirect_ref (input_location, begin, RO_NULL,
11026 tf_warning_or_error),
11027 /*is_constant_init*/false, NULL_TREE,
11028 LOOKUP_ONLYCONVERTING);
11029
11030 return statement;
11031 }
11032
11033 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11034 We need to solve both at the same time because the method used
11035 depends on the existence of members begin or end.
11036 Returns the type deduced for the iterator expression. */
11037
11038 static tree
11039 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11040 {
11041 if (error_operand_p (range))
11042 {
11043 *begin = *end = error_mark_node;
11044 return error_mark_node;
11045 }
11046
11047 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11048 {
11049 error ("range-based %<for%> expression of type %qT "
11050 "has incomplete type", TREE_TYPE (range));
11051 *begin = *end = error_mark_node;
11052 return error_mark_node;
11053 }
11054 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11055 {
11056 /* If RANGE is an array, we will use pointer arithmetic. */
11057 *begin = range;
11058 *end = build_binary_op (input_location, PLUS_EXPR,
11059 range,
11060 array_type_nelts_top (TREE_TYPE (range)),
11061 0);
11062 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
11063 }
11064 else
11065 {
11066 /* If it is not an array, we must do a bit of magic. */
11067 tree id_begin, id_end;
11068 tree member_begin, member_end;
11069
11070 *begin = *end = error_mark_node;
11071
11072 id_begin = get_identifier ("begin");
11073 id_end = get_identifier ("end");
11074 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11075 /*protect=*/2, /*want_type=*/false,
11076 tf_warning_or_error);
11077 member_end = lookup_member (TREE_TYPE (range), id_end,
11078 /*protect=*/2, /*want_type=*/false,
11079 tf_warning_or_error);
11080
11081 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11082 {
11083 /* Use the member functions. */
11084 if (member_begin != NULL_TREE)
11085 *begin = cp_parser_range_for_member_function (range, id_begin);
11086 else
11087 error ("range-based %<for%> expression of type %qT has an "
11088 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11089
11090 if (member_end != NULL_TREE)
11091 *end = cp_parser_range_for_member_function (range, id_end);
11092 else
11093 error ("range-based %<for%> expression of type %qT has a "
11094 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11095 }
11096 else
11097 {
11098 /* Use global functions with ADL. */
11099 vec<tree, va_gc> *vec;
11100 vec = make_tree_vector ();
11101
11102 vec_safe_push (vec, range);
11103
11104 member_begin = perform_koenig_lookup (id_begin, vec,
11105 tf_warning_or_error);
11106 *begin = finish_call_expr (member_begin, &vec, false, true,
11107 tf_warning_or_error);
11108 member_end = perform_koenig_lookup (id_end, vec,
11109 tf_warning_or_error);
11110 *end = finish_call_expr (member_end, &vec, false, true,
11111 tf_warning_or_error);
11112
11113 release_tree_vector (vec);
11114 }
11115
11116 /* Last common checks. */
11117 if (*begin == error_mark_node || *end == error_mark_node)
11118 {
11119 /* If one of the expressions is an error do no more checks. */
11120 *begin = *end = error_mark_node;
11121 return error_mark_node;
11122 }
11123 else if (type_dependent_expression_p (*begin)
11124 || type_dependent_expression_p (*end))
11125 /* Can happen, when, eg, in a template context, Koenig lookup
11126 can't resolve begin/end (c++/58503). */
11127 return NULL_TREE;
11128 else
11129 {
11130 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11131 /* The unqualified type of the __begin and __end temporaries should
11132 be the same, as required by the multiple auto declaration. */
11133 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11134 error ("inconsistent begin/end types in range-based %<for%> "
11135 "statement: %qT and %qT",
11136 TREE_TYPE (*begin), TREE_TYPE (*end));
11137 return iter_type;
11138 }
11139 }
11140 }
11141
11142 /* Helper function for cp_parser_perform_range_for_lookup.
11143 Builds a tree for RANGE.IDENTIFIER(). */
11144
11145 static tree
11146 cp_parser_range_for_member_function (tree range, tree identifier)
11147 {
11148 tree member, res;
11149 vec<tree, va_gc> *vec;
11150
11151 member = finish_class_member_access_expr (range, identifier,
11152 false, tf_warning_or_error);
11153 if (member == error_mark_node)
11154 return error_mark_node;
11155
11156 vec = make_tree_vector ();
11157 res = finish_call_expr (member, &vec,
11158 /*disallow_virtual=*/false,
11159 /*koenig_p=*/false,
11160 tf_warning_or_error);
11161 release_tree_vector (vec);
11162 return res;
11163 }
11164
11165 /* Parse an iteration-statement.
11166
11167 iteration-statement:
11168 while ( condition ) statement
11169 do statement while ( expression ) ;
11170 for ( for-init-statement condition [opt] ; expression [opt] )
11171 statement
11172
11173 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11174
11175 static tree
11176 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
11177 {
11178 cp_token *token;
11179 enum rid keyword;
11180 tree statement;
11181 unsigned char in_statement;
11182 token_indent_info guard_tinfo;
11183
11184 /* Peek at the next token. */
11185 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11186 if (!token)
11187 return error_mark_node;
11188
11189 guard_tinfo = get_token_indent_info (token);
11190
11191 /* Remember whether or not we are already within an iteration
11192 statement. */
11193 in_statement = parser->in_statement;
11194
11195 /* See what kind of keyword it is. */
11196 keyword = token->keyword;
11197 switch (keyword)
11198 {
11199 case RID_WHILE:
11200 {
11201 tree condition;
11202
11203 /* Begin the while-statement. */
11204 statement = begin_while_stmt ();
11205 /* Look for the `('. */
11206 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11207 /* Parse the condition. */
11208 condition = cp_parser_condition (parser);
11209 finish_while_stmt_cond (condition, statement, ivdep);
11210 /* Look for the `)'. */
11211 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11212 /* Parse the dependent statement. */
11213 parser->in_statement = IN_ITERATION_STMT;
11214 cp_parser_already_scoped_statement (parser, guard_tinfo);
11215 parser->in_statement = in_statement;
11216 /* We're done with the while-statement. */
11217 finish_while_stmt (statement);
11218 }
11219 break;
11220
11221 case RID_DO:
11222 {
11223 tree expression;
11224
11225 /* Begin the do-statement. */
11226 statement = begin_do_stmt ();
11227 /* Parse the body of the do-statement. */
11228 parser->in_statement = IN_ITERATION_STMT;
11229 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11230 parser->in_statement = in_statement;
11231 finish_do_body (statement);
11232 /* Look for the `while' keyword. */
11233 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11234 /* Look for the `('. */
11235 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11236 /* Parse the expression. */
11237 expression = cp_parser_expression (parser);
11238 /* We're done with the do-statement. */
11239 finish_do_stmt (expression, statement, ivdep);
11240 /* Look for the `)'. */
11241 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11242 /* Look for the `;'. */
11243 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11244 }
11245 break;
11246
11247 case RID_FOR:
11248 {
11249 /* Look for the `('. */
11250 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11251
11252 statement = cp_parser_for (parser, ivdep);
11253
11254 /* Look for the `)'. */
11255 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11256
11257 /* Parse the body of the for-statement. */
11258 parser->in_statement = IN_ITERATION_STMT;
11259 cp_parser_already_scoped_statement (parser, guard_tinfo);
11260 parser->in_statement = in_statement;
11261
11262 /* We're done with the for-statement. */
11263 finish_for_stmt (statement);
11264 }
11265 break;
11266
11267 default:
11268 cp_parser_error (parser, "expected iteration-statement");
11269 statement = error_mark_node;
11270 break;
11271 }
11272
11273 return statement;
11274 }
11275
11276 /* Parse a for-init-statement or the declarator of a range-based-for.
11277 Returns true if a range-based-for declaration is seen.
11278
11279 for-init-statement:
11280 expression-statement
11281 simple-declaration */
11282
11283 static bool
11284 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
11285 {
11286 /* If the next token is a `;', then we have an empty
11287 expression-statement. Grammatically, this is also a
11288 simple-declaration, but an invalid one, because it does not
11289 declare anything. Therefore, if we did not handle this case
11290 specially, we would issue an error message about an invalid
11291 declaration. */
11292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11293 {
11294 bool is_range_for = false;
11295 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11296
11297 /* A colon is used in range-based for. */
11298 parser->colon_corrects_to_scope_p = false;
11299
11300 /* We're going to speculatively look for a declaration, falling back
11301 to an expression, if necessary. */
11302 cp_parser_parse_tentatively (parser);
11303 /* Parse the declaration. */
11304 cp_parser_simple_declaration (parser,
11305 /*function_definition_allowed_p=*/false,
11306 decl);
11307 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11308 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11309 {
11310 /* It is a range-for, consume the ':' */
11311 cp_lexer_consume_token (parser->lexer);
11312 is_range_for = true;
11313 if (cxx_dialect < cxx11)
11314 {
11315 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11316 "range-based %<for%> loops only available with "
11317 "-std=c++11 or -std=gnu++11");
11318 *decl = error_mark_node;
11319 }
11320 }
11321 else
11322 /* The ';' is not consumed yet because we told
11323 cp_parser_simple_declaration not to. */
11324 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11325
11326 if (cp_parser_parse_definitely (parser))
11327 return is_range_for;
11328 /* If the tentative parse failed, then we shall need to look for an
11329 expression-statement. */
11330 }
11331 /* If we are here, it is an expression-statement. */
11332 cp_parser_expression_statement (parser, NULL_TREE);
11333 return false;
11334 }
11335
11336 /* Parse a jump-statement.
11337
11338 jump-statement:
11339 break ;
11340 continue ;
11341 return expression [opt] ;
11342 return braced-init-list ;
11343 goto identifier ;
11344
11345 GNU extension:
11346
11347 jump-statement:
11348 goto * expression ;
11349
11350 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11351
11352 static tree
11353 cp_parser_jump_statement (cp_parser* parser)
11354 {
11355 tree statement = error_mark_node;
11356 cp_token *token;
11357 enum rid keyword;
11358 unsigned char in_statement;
11359
11360 /* Peek at the next token. */
11361 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11362 if (!token)
11363 return error_mark_node;
11364
11365 /* See what kind of keyword it is. */
11366 keyword = token->keyword;
11367 switch (keyword)
11368 {
11369 case RID_BREAK:
11370 in_statement = parser->in_statement & ~IN_IF_STMT;
11371 switch (in_statement)
11372 {
11373 case 0:
11374 error_at (token->location, "break statement not within loop or switch");
11375 break;
11376 default:
11377 gcc_assert ((in_statement & IN_SWITCH_STMT)
11378 || in_statement == IN_ITERATION_STMT);
11379 statement = finish_break_stmt ();
11380 if (in_statement == IN_ITERATION_STMT)
11381 break_maybe_infinite_loop ();
11382 break;
11383 case IN_OMP_BLOCK:
11384 error_at (token->location, "invalid exit from OpenMP structured block");
11385 break;
11386 case IN_OMP_FOR:
11387 error_at (token->location, "break statement used with OpenMP for loop");
11388 break;
11389 case IN_CILK_SIMD_FOR:
11390 error_at (token->location, "break statement used with Cilk Plus for loop");
11391 break;
11392 }
11393 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11394 break;
11395
11396 case RID_CONTINUE:
11397 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11398 {
11399 case 0:
11400 error_at (token->location, "continue statement not within a loop");
11401 break;
11402 case IN_CILK_SIMD_FOR:
11403 error_at (token->location,
11404 "continue statement within %<#pragma simd%> loop body");
11405 /* Fall through. */
11406 case IN_ITERATION_STMT:
11407 case IN_OMP_FOR:
11408 statement = finish_continue_stmt ();
11409 break;
11410 case IN_OMP_BLOCK:
11411 error_at (token->location, "invalid exit from OpenMP structured block");
11412 break;
11413 default:
11414 gcc_unreachable ();
11415 }
11416 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11417 break;
11418
11419 case RID_RETURN:
11420 {
11421 tree expr;
11422 bool expr_non_constant_p;
11423
11424 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11425 {
11426 cp_lexer_set_source_position (parser->lexer);
11427 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11428 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11429 }
11430 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11431 expr = cp_parser_expression (parser);
11432 else
11433 /* If the next token is a `;', then there is no
11434 expression. */
11435 expr = NULL_TREE;
11436 /* Build the return-statement. */
11437 statement = finish_return_stmt (expr);
11438 /* Look for the final `;'. */
11439 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11440 }
11441 break;
11442
11443 case RID_GOTO:
11444 if (parser->in_function_body
11445 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11446 {
11447 error ("%<goto%> in %<constexpr%> function");
11448 cp_function_chain->invalid_constexpr = true;
11449 }
11450
11451 /* Create the goto-statement. */
11452 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11453 {
11454 /* Issue a warning about this use of a GNU extension. */
11455 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11456 /* Consume the '*' token. */
11457 cp_lexer_consume_token (parser->lexer);
11458 /* Parse the dependent expression. */
11459 finish_goto_stmt (cp_parser_expression (parser));
11460 }
11461 else
11462 finish_goto_stmt (cp_parser_identifier (parser));
11463 /* Look for the final `;'. */
11464 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11465 break;
11466
11467 default:
11468 cp_parser_error (parser, "expected jump-statement");
11469 break;
11470 }
11471
11472 return statement;
11473 }
11474
11475 /* Parse a declaration-statement.
11476
11477 declaration-statement:
11478 block-declaration */
11479
11480 static void
11481 cp_parser_declaration_statement (cp_parser* parser)
11482 {
11483 void *p;
11484
11485 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11486 p = obstack_alloc (&declarator_obstack, 0);
11487
11488 /* Parse the block-declaration. */
11489 cp_parser_block_declaration (parser, /*statement_p=*/true);
11490
11491 /* Free any declarators allocated. */
11492 obstack_free (&declarator_obstack, p);
11493 }
11494
11495 /* Some dependent statements (like `if (cond) statement'), are
11496 implicitly in their own scope. In other words, if the statement is
11497 a single statement (as opposed to a compound-statement), it is
11498 none-the-less treated as if it were enclosed in braces. Any
11499 declarations appearing in the dependent statement are out of scope
11500 after control passes that point. This function parses a statement,
11501 but ensures that is in its own scope, even if it is not a
11502 compound-statement.
11503
11504 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11505 is a (possibly labeled) if statement which is not enclosed in
11506 braces and has an else clause. This is used to implement
11507 -Wparentheses.
11508
11509 CHAIN is a vector of if-else-if conditions. This is used to implement
11510 -Wduplicated-cond.
11511
11512 Returns the new statement. */
11513
11514 static tree
11515 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11516 const token_indent_info &guard_tinfo,
11517 vec<tree> *chain)
11518 {
11519 tree statement;
11520 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11521 token_indent_info body_tinfo
11522 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11523
11524 if (if_p != NULL)
11525 *if_p = false;
11526
11527 /* Mark if () ; with a special NOP_EXPR. */
11528 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11529 {
11530 cp_lexer_consume_token (parser->lexer);
11531 statement = add_stmt (build_empty_stmt (body_loc));
11532
11533 if (guard_tinfo.keyword == RID_IF
11534 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
11535 warning_at (body_loc, OPT_Wempty_body,
11536 "suggest braces around empty body in an %<if%> statement");
11537 else if (guard_tinfo.keyword == RID_ELSE)
11538 warning_at (body_loc, OPT_Wempty_body,
11539 "suggest braces around empty body in an %<else%> statement");
11540 }
11541 /* if a compound is opened, we simply parse the statement directly. */
11542 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11543 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11544 /* If the token is not a `{', then we must take special action. */
11545 else
11546 {
11547 /* Create a compound-statement. */
11548 statement = begin_compound_stmt (0);
11549 /* Parse the dependent-statement. */
11550 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
11551 /* Finish the dummy compound-statement. */
11552 finish_compound_stmt (statement);
11553 }
11554
11555 token_indent_info next_tinfo
11556 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11557 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11558
11559 /* Return the statement. */
11560 return statement;
11561 }
11562
11563 /* For some dependent statements (like `while (cond) statement'), we
11564 have already created a scope. Therefore, even if the dependent
11565 statement is a compound-statement, we do not want to create another
11566 scope. */
11567
11568 static void
11569 cp_parser_already_scoped_statement (cp_parser* parser,
11570 const token_indent_info &guard_tinfo)
11571 {
11572 /* If the token is a `{', then we must take special action. */
11573 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11574 {
11575 token_indent_info body_tinfo
11576 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11577
11578 cp_parser_statement (parser, NULL_TREE, false, NULL);
11579 token_indent_info next_tinfo
11580 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11581 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11582 }
11583 else
11584 {
11585 /* Avoid calling cp_parser_compound_statement, so that we
11586 don't create a new scope. Do everything else by hand. */
11587 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11588 /* If the next keyword is `__label__' we have a label declaration. */
11589 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11590 cp_parser_label_declaration (parser);
11591 /* Parse an (optional) statement-seq. */
11592 cp_parser_statement_seq_opt (parser, NULL_TREE);
11593 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11594 }
11595 }
11596
11597 /* Declarations [gram.dcl.dcl] */
11598
11599 /* Parse an optional declaration-sequence.
11600
11601 declaration-seq:
11602 declaration
11603 declaration-seq declaration */
11604
11605 static void
11606 cp_parser_declaration_seq_opt (cp_parser* parser)
11607 {
11608 while (true)
11609 {
11610 cp_token *token;
11611
11612 token = cp_lexer_peek_token (parser->lexer);
11613
11614 if (token->type == CPP_CLOSE_BRACE
11615 || token->type == CPP_EOF
11616 || token->type == CPP_PRAGMA_EOL)
11617 break;
11618
11619 if (token->type == CPP_SEMICOLON)
11620 {
11621 /* A declaration consisting of a single semicolon is
11622 invalid. Allow it unless we're being pedantic. */
11623 cp_lexer_consume_token (parser->lexer);
11624 if (!in_system_header_at (input_location))
11625 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11626 continue;
11627 }
11628
11629 /* If we're entering or exiting a region that's implicitly
11630 extern "C", modify the lang context appropriately. */
11631 if (!parser->implicit_extern_c && token->implicit_extern_c)
11632 {
11633 push_lang_context (lang_name_c);
11634 parser->implicit_extern_c = true;
11635 }
11636 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11637 {
11638 pop_lang_context ();
11639 parser->implicit_extern_c = false;
11640 }
11641
11642 if (token->type == CPP_PRAGMA)
11643 {
11644 /* A top-level declaration can consist solely of a #pragma.
11645 A nested declaration cannot, so this is done here and not
11646 in cp_parser_declaration. (A #pragma at block scope is
11647 handled in cp_parser_statement.) */
11648 cp_parser_pragma (parser, pragma_external);
11649 continue;
11650 }
11651
11652 /* Parse the declaration itself. */
11653 cp_parser_declaration (parser);
11654 }
11655 }
11656
11657 /* Parse a declaration.
11658
11659 declaration:
11660 block-declaration
11661 function-definition
11662 template-declaration
11663 explicit-instantiation
11664 explicit-specialization
11665 linkage-specification
11666 namespace-definition
11667
11668 GNU extension:
11669
11670 declaration:
11671 __extension__ declaration */
11672
11673 static void
11674 cp_parser_declaration (cp_parser* parser)
11675 {
11676 cp_token token1;
11677 cp_token token2;
11678 int saved_pedantic;
11679 void *p;
11680 tree attributes = NULL_TREE;
11681
11682 /* Check for the `__extension__' keyword. */
11683 if (cp_parser_extension_opt (parser, &saved_pedantic))
11684 {
11685 /* Parse the qualified declaration. */
11686 cp_parser_declaration (parser);
11687 /* Restore the PEDANTIC flag. */
11688 pedantic = saved_pedantic;
11689
11690 return;
11691 }
11692
11693 /* Try to figure out what kind of declaration is present. */
11694 token1 = *cp_lexer_peek_token (parser->lexer);
11695
11696 if (token1.type != CPP_EOF)
11697 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11698 else
11699 {
11700 token2.type = CPP_EOF;
11701 token2.keyword = RID_MAX;
11702 }
11703
11704 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11705 p = obstack_alloc (&declarator_obstack, 0);
11706
11707 /* If the next token is `extern' and the following token is a string
11708 literal, then we have a linkage specification. */
11709 if (token1.keyword == RID_EXTERN
11710 && cp_parser_is_pure_string_literal (&token2))
11711 cp_parser_linkage_specification (parser);
11712 /* If the next token is `template', then we have either a template
11713 declaration, an explicit instantiation, or an explicit
11714 specialization. */
11715 else if (token1.keyword == RID_TEMPLATE)
11716 {
11717 /* `template <>' indicates a template specialization. */
11718 if (token2.type == CPP_LESS
11719 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11720 cp_parser_explicit_specialization (parser);
11721 /* `template <' indicates a template declaration. */
11722 else if (token2.type == CPP_LESS)
11723 cp_parser_template_declaration (parser, /*member_p=*/false);
11724 /* Anything else must be an explicit instantiation. */
11725 else
11726 cp_parser_explicit_instantiation (parser);
11727 }
11728 /* If the next token is `export', then we have a template
11729 declaration. */
11730 else if (token1.keyword == RID_EXPORT)
11731 cp_parser_template_declaration (parser, /*member_p=*/false);
11732 /* If the next token is `extern', 'static' or 'inline' and the one
11733 after that is `template', we have a GNU extended explicit
11734 instantiation directive. */
11735 else if (cp_parser_allow_gnu_extensions_p (parser)
11736 && (token1.keyword == RID_EXTERN
11737 || token1.keyword == RID_STATIC
11738 || token1.keyword == RID_INLINE)
11739 && token2.keyword == RID_TEMPLATE)
11740 cp_parser_explicit_instantiation (parser);
11741 /* If the next token is `namespace', check for a named or unnamed
11742 namespace definition. */
11743 else if (token1.keyword == RID_NAMESPACE
11744 && (/* A named namespace definition. */
11745 (token2.type == CPP_NAME
11746 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11747 != CPP_EQ))
11748 || (token2.type == CPP_OPEN_SQUARE
11749 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
11750 == CPP_OPEN_SQUARE)
11751 /* An unnamed namespace definition. */
11752 || token2.type == CPP_OPEN_BRACE
11753 || token2.keyword == RID_ATTRIBUTE))
11754 cp_parser_namespace_definition (parser);
11755 /* An inline (associated) namespace definition. */
11756 else if (token1.keyword == RID_INLINE
11757 && token2.keyword == RID_NAMESPACE)
11758 cp_parser_namespace_definition (parser);
11759 /* Objective-C++ declaration/definition. */
11760 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11761 cp_parser_objc_declaration (parser, NULL_TREE);
11762 else if (c_dialect_objc ()
11763 && token1.keyword == RID_ATTRIBUTE
11764 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11765 cp_parser_objc_declaration (parser, attributes);
11766 /* At this point we may have a template declared by a concept
11767 introduction. */
11768 else if (flag_concepts
11769 && cp_parser_template_declaration_after_export (parser,
11770 /*member_p=*/false))
11771 /* We did. */;
11772 else
11773 /* Try to parse a block-declaration, or a function-definition. */
11774 cp_parser_block_declaration (parser, /*statement_p=*/false);
11775
11776 /* Free any declarators allocated. */
11777 obstack_free (&declarator_obstack, p);
11778 }
11779
11780 /* Parse a block-declaration.
11781
11782 block-declaration:
11783 simple-declaration
11784 asm-definition
11785 namespace-alias-definition
11786 using-declaration
11787 using-directive
11788
11789 GNU Extension:
11790
11791 block-declaration:
11792 __extension__ block-declaration
11793
11794 C++0x Extension:
11795
11796 block-declaration:
11797 static_assert-declaration
11798
11799 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11800 part of a declaration-statement. */
11801
11802 static void
11803 cp_parser_block_declaration (cp_parser *parser,
11804 bool statement_p)
11805 {
11806 cp_token *token1;
11807 int saved_pedantic;
11808
11809 /* Check for the `__extension__' keyword. */
11810 if (cp_parser_extension_opt (parser, &saved_pedantic))
11811 {
11812 /* Parse the qualified declaration. */
11813 cp_parser_block_declaration (parser, statement_p);
11814 /* Restore the PEDANTIC flag. */
11815 pedantic = saved_pedantic;
11816
11817 return;
11818 }
11819
11820 /* Peek at the next token to figure out which kind of declaration is
11821 present. */
11822 token1 = cp_lexer_peek_token (parser->lexer);
11823
11824 /* If the next keyword is `asm', we have an asm-definition. */
11825 if (token1->keyword == RID_ASM)
11826 {
11827 if (statement_p)
11828 cp_parser_commit_to_tentative_parse (parser);
11829 cp_parser_asm_definition (parser);
11830 }
11831 /* If the next keyword is `namespace', we have a
11832 namespace-alias-definition. */
11833 else if (token1->keyword == RID_NAMESPACE)
11834 cp_parser_namespace_alias_definition (parser);
11835 /* If the next keyword is `using', we have a
11836 using-declaration, a using-directive, or an alias-declaration. */
11837 else if (token1->keyword == RID_USING)
11838 {
11839 cp_token *token2;
11840
11841 if (statement_p)
11842 cp_parser_commit_to_tentative_parse (parser);
11843 /* If the token after `using' is `namespace', then we have a
11844 using-directive. */
11845 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11846 if (token2->keyword == RID_NAMESPACE)
11847 cp_parser_using_directive (parser);
11848 /* If the second token after 'using' is '=', then we have an
11849 alias-declaration. */
11850 else if (cxx_dialect >= cxx11
11851 && token2->type == CPP_NAME
11852 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11853 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11854 cp_parser_alias_declaration (parser);
11855 /* Otherwise, it's a using-declaration. */
11856 else
11857 cp_parser_using_declaration (parser,
11858 /*access_declaration_p=*/false);
11859 }
11860 /* If the next keyword is `__label__' we have a misplaced label
11861 declaration. */
11862 else if (token1->keyword == RID_LABEL)
11863 {
11864 cp_lexer_consume_token (parser->lexer);
11865 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11866 cp_parser_skip_to_end_of_statement (parser);
11867 /* If the next token is now a `;', consume it. */
11868 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11869 cp_lexer_consume_token (parser->lexer);
11870 }
11871 /* If the next token is `static_assert' we have a static assertion. */
11872 else if (token1->keyword == RID_STATIC_ASSERT)
11873 cp_parser_static_assert (parser, /*member_p=*/false);
11874 /* Anything else must be a simple-declaration. */
11875 else
11876 cp_parser_simple_declaration (parser, !statement_p,
11877 /*maybe_range_for_decl*/NULL);
11878 }
11879
11880 /* Parse a simple-declaration.
11881
11882 simple-declaration:
11883 decl-specifier-seq [opt] init-declarator-list [opt] ;
11884
11885 init-declarator-list:
11886 init-declarator
11887 init-declarator-list , init-declarator
11888
11889 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11890 function-definition as a simple-declaration.
11891
11892 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11893 parsed declaration if it is an uninitialized single declarator not followed
11894 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11895 if present, will not be consumed. */
11896
11897 static void
11898 cp_parser_simple_declaration (cp_parser* parser,
11899 bool function_definition_allowed_p,
11900 tree *maybe_range_for_decl)
11901 {
11902 cp_decl_specifier_seq decl_specifiers;
11903 int declares_class_or_enum;
11904 bool saw_declarator;
11905 location_t comma_loc = UNKNOWN_LOCATION;
11906 location_t init_loc = UNKNOWN_LOCATION;
11907 bool first = true;
11908
11909 if (maybe_range_for_decl)
11910 *maybe_range_for_decl = NULL_TREE;
11911
11912 /* Defer access checks until we know what is being declared; the
11913 checks for names appearing in the decl-specifier-seq should be
11914 done as if we were in the scope of the thing being declared. */
11915 push_deferring_access_checks (dk_deferred);
11916
11917 /* Parse the decl-specifier-seq. We have to keep track of whether
11918 or not the decl-specifier-seq declares a named class or
11919 enumeration type, since that is the only case in which the
11920 init-declarator-list is allowed to be empty.
11921
11922 [dcl.dcl]
11923
11924 In a simple-declaration, the optional init-declarator-list can be
11925 omitted only when declaring a class or enumeration, that is when
11926 the decl-specifier-seq contains either a class-specifier, an
11927 elaborated-type-specifier, or an enum-specifier. */
11928 cp_parser_decl_specifier_seq (parser,
11929 CP_PARSER_FLAGS_OPTIONAL,
11930 &decl_specifiers,
11931 &declares_class_or_enum);
11932 /* We no longer need to defer access checks. */
11933 stop_deferring_access_checks ();
11934
11935 /* In a block scope, a valid declaration must always have a
11936 decl-specifier-seq. By not trying to parse declarators, we can
11937 resolve the declaration/expression ambiguity more quickly. */
11938 if (!function_definition_allowed_p
11939 && !decl_specifiers.any_specifiers_p)
11940 {
11941 cp_parser_error (parser, "expected declaration");
11942 goto done;
11943 }
11944
11945 /* If the next two tokens are both identifiers, the code is
11946 erroneous. The usual cause of this situation is code like:
11947
11948 T t;
11949
11950 where "T" should name a type -- but does not. */
11951 if (!decl_specifiers.any_type_specifiers_p
11952 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11953 {
11954 /* If parsing tentatively, we should commit; we really are
11955 looking at a declaration. */
11956 cp_parser_commit_to_tentative_parse (parser);
11957 /* Give up. */
11958 goto done;
11959 }
11960
11961 /* If we have seen at least one decl-specifier, and the next token
11962 is not a parenthesis, then we must be looking at a declaration.
11963 (After "int (" we might be looking at a functional cast.) */
11964 if (decl_specifiers.any_specifiers_p
11965 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11966 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11967 && !cp_parser_error_occurred (parser))
11968 cp_parser_commit_to_tentative_parse (parser);
11969
11970 /* Keep going until we hit the `;' at the end of the simple
11971 declaration. */
11972 saw_declarator = false;
11973 while (cp_lexer_next_token_is_not (parser->lexer,
11974 CPP_SEMICOLON))
11975 {
11976 cp_token *token;
11977 bool function_definition_p;
11978 tree decl;
11979
11980 if (saw_declarator)
11981 {
11982 /* If we are processing next declarator, comma is expected */
11983 token = cp_lexer_peek_token (parser->lexer);
11984 gcc_assert (token->type == CPP_COMMA);
11985 cp_lexer_consume_token (parser->lexer);
11986 if (maybe_range_for_decl)
11987 {
11988 *maybe_range_for_decl = error_mark_node;
11989 if (comma_loc == UNKNOWN_LOCATION)
11990 comma_loc = token->location;
11991 }
11992 }
11993 else
11994 saw_declarator = true;
11995
11996 /* Parse the init-declarator. */
11997 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11998 /*checks=*/NULL,
11999 function_definition_allowed_p,
12000 /*member_p=*/false,
12001 declares_class_or_enum,
12002 &function_definition_p,
12003 maybe_range_for_decl,
12004 first,
12005 &init_loc);
12006 first = false;
12007
12008 /* If an error occurred while parsing tentatively, exit quickly.
12009 (That usually happens when in the body of a function; each
12010 statement is treated as a declaration-statement until proven
12011 otherwise.) */
12012 if (cp_parser_error_occurred (parser))
12013 goto done;
12014 /* Handle function definitions specially. */
12015 if (function_definition_p)
12016 {
12017 /* If the next token is a `,', then we are probably
12018 processing something like:
12019
12020 void f() {}, *p;
12021
12022 which is erroneous. */
12023 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12024 {
12025 cp_token *token = cp_lexer_peek_token (parser->lexer);
12026 error_at (token->location,
12027 "mixing"
12028 " declarations and function-definitions is forbidden");
12029 }
12030 /* Otherwise, we're done with the list of declarators. */
12031 else
12032 {
12033 pop_deferring_access_checks ();
12034 return;
12035 }
12036 }
12037 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12038 *maybe_range_for_decl = decl;
12039 /* The next token should be either a `,' or a `;'. */
12040 token = cp_lexer_peek_token (parser->lexer);
12041 /* If it's a `,', there are more declarators to come. */
12042 if (token->type == CPP_COMMA)
12043 /* will be consumed next time around */;
12044 /* If it's a `;', we are done. */
12045 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12046 break;
12047 /* Anything else is an error. */
12048 else
12049 {
12050 /* If we have already issued an error message we don't need
12051 to issue another one. */
12052 if ((decl != error_mark_node
12053 && DECL_INITIAL (decl) != error_mark_node)
12054 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12055 cp_parser_error (parser, "expected %<,%> or %<;%>");
12056 /* Skip tokens until we reach the end of the statement. */
12057 cp_parser_skip_to_end_of_statement (parser);
12058 /* If the next token is now a `;', consume it. */
12059 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12060 cp_lexer_consume_token (parser->lexer);
12061 goto done;
12062 }
12063 /* After the first time around, a function-definition is not
12064 allowed -- even if it was OK at first. For example:
12065
12066 int i, f() {}
12067
12068 is not valid. */
12069 function_definition_allowed_p = false;
12070 }
12071
12072 /* Issue an error message if no declarators are present, and the
12073 decl-specifier-seq does not itself declare a class or
12074 enumeration: [dcl.dcl]/3. */
12075 if (!saw_declarator)
12076 {
12077 if (cp_parser_declares_only_class_p (parser))
12078 {
12079 if (!declares_class_or_enum
12080 && decl_specifiers.type
12081 && OVERLOAD_TYPE_P (decl_specifiers.type))
12082 /* Ensure an error is issued anyway when finish_decltype_type,
12083 called via cp_parser_decl_specifier_seq, returns a class or
12084 an enumeration (c++/51786). */
12085 decl_specifiers.type = NULL_TREE;
12086 shadow_tag (&decl_specifiers);
12087 }
12088 /* Perform any deferred access checks. */
12089 perform_deferred_access_checks (tf_warning_or_error);
12090 }
12091
12092 /* Consume the `;'. */
12093 if (!maybe_range_for_decl)
12094 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12095 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12096 {
12097 if (init_loc != UNKNOWN_LOCATION)
12098 error_at (init_loc, "initializer in range-based %<for%> loop");
12099 if (comma_loc != UNKNOWN_LOCATION)
12100 error_at (comma_loc,
12101 "multiple declarations in range-based %<for%> loop");
12102 }
12103
12104 done:
12105 pop_deferring_access_checks ();
12106
12107 /* Reset any acc routine clauses. */
12108 parser->oacc_routine = NULL;
12109 }
12110
12111 /* Parse a decl-specifier-seq.
12112
12113 decl-specifier-seq:
12114 decl-specifier-seq [opt] decl-specifier
12115 decl-specifier attribute-specifier-seq [opt] (C++11)
12116
12117 decl-specifier:
12118 storage-class-specifier
12119 type-specifier
12120 function-specifier
12121 friend
12122 typedef
12123
12124 GNU Extension:
12125
12126 decl-specifier:
12127 attributes
12128
12129 Concepts Extension:
12130
12131 decl-specifier:
12132 concept
12133
12134 Set *DECL_SPECS to a representation of the decl-specifier-seq.
12135
12136 The parser flags FLAGS is used to control type-specifier parsing.
12137
12138 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12139 flags:
12140
12141 1: one of the decl-specifiers is an elaborated-type-specifier
12142 (i.e., a type declaration)
12143 2: one of the decl-specifiers is an enum-specifier or a
12144 class-specifier (i.e., a type definition)
12145
12146 */
12147
12148 static void
12149 cp_parser_decl_specifier_seq (cp_parser* parser,
12150 cp_parser_flags flags,
12151 cp_decl_specifier_seq *decl_specs,
12152 int* declares_class_or_enum)
12153 {
12154 bool constructor_possible_p = !parser->in_declarator_p;
12155 bool found_decl_spec = false;
12156 cp_token *start_token = NULL;
12157 cp_decl_spec ds;
12158
12159 /* Clear DECL_SPECS. */
12160 clear_decl_specs (decl_specs);
12161
12162 /* Assume no class or enumeration type is declared. */
12163 *declares_class_or_enum = 0;
12164
12165 /* Keep reading specifiers until there are no more to read. */
12166 while (true)
12167 {
12168 bool constructor_p;
12169 cp_token *token;
12170 ds = ds_last;
12171
12172 /* Peek at the next token. */
12173 token = cp_lexer_peek_token (parser->lexer);
12174
12175 /* Save the first token of the decl spec list for error
12176 reporting. */
12177 if (!start_token)
12178 start_token = token;
12179 /* Handle attributes. */
12180 if (cp_next_tokens_can_be_attribute_p (parser))
12181 {
12182 /* Parse the attributes. */
12183 tree attrs = cp_parser_attributes_opt (parser);
12184
12185 /* In a sequence of declaration specifiers, c++11 attributes
12186 appertain to the type that precede them. In that case
12187 [dcl.spec]/1 says:
12188
12189 The attribute-specifier-seq affects the type only for
12190 the declaration it appears in, not other declarations
12191 involving the same type.
12192
12193 But for now let's force the user to position the
12194 attribute either at the beginning of the declaration or
12195 after the declarator-id, which would clearly mean that it
12196 applies to the declarator. */
12197 if (cxx11_attribute_p (attrs))
12198 {
12199 if (!found_decl_spec)
12200 /* The c++11 attribute is at the beginning of the
12201 declaration. It appertains to the entity being
12202 declared. */;
12203 else
12204 {
12205 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
12206 {
12207 /* This is an attribute following a
12208 class-specifier. */
12209 if (decl_specs->type_definition_p)
12210 warn_misplaced_attr_for_class_type (token->location,
12211 decl_specs->type);
12212 attrs = NULL_TREE;
12213 }
12214 else
12215 {
12216 decl_specs->std_attributes
12217 = chainon (decl_specs->std_attributes,
12218 attrs);
12219 if (decl_specs->locations[ds_std_attribute] == 0)
12220 decl_specs->locations[ds_std_attribute] = token->location;
12221 }
12222 continue;
12223 }
12224 }
12225
12226 decl_specs->attributes
12227 = chainon (decl_specs->attributes,
12228 attrs);
12229 if (decl_specs->locations[ds_attribute] == 0)
12230 decl_specs->locations[ds_attribute] = token->location;
12231 continue;
12232 }
12233 /* Assume we will find a decl-specifier keyword. */
12234 found_decl_spec = true;
12235 /* If the next token is an appropriate keyword, we can simply
12236 add it to the list. */
12237 switch (token->keyword)
12238 {
12239 /* decl-specifier:
12240 friend
12241 constexpr */
12242 case RID_FRIEND:
12243 if (!at_class_scope_p ())
12244 {
12245 error_at (token->location, "%<friend%> used outside of class");
12246 cp_lexer_purge_token (parser->lexer);
12247 }
12248 else
12249 {
12250 ds = ds_friend;
12251 /* Consume the token. */
12252 cp_lexer_consume_token (parser->lexer);
12253 }
12254 break;
12255
12256 case RID_CONSTEXPR:
12257 ds = ds_constexpr;
12258 cp_lexer_consume_token (parser->lexer);
12259 break;
12260
12261 case RID_CONCEPT:
12262 ds = ds_concept;
12263 cp_lexer_consume_token (parser->lexer);
12264 break;
12265
12266 /* function-specifier:
12267 inline
12268 virtual
12269 explicit */
12270 case RID_INLINE:
12271 case RID_VIRTUAL:
12272 case RID_EXPLICIT:
12273 cp_parser_function_specifier_opt (parser, decl_specs);
12274 break;
12275
12276 /* decl-specifier:
12277 typedef */
12278 case RID_TYPEDEF:
12279 ds = ds_typedef;
12280 /* Consume the token. */
12281 cp_lexer_consume_token (parser->lexer);
12282 /* A constructor declarator cannot appear in a typedef. */
12283 constructor_possible_p = false;
12284 /* The "typedef" keyword can only occur in a declaration; we
12285 may as well commit at this point. */
12286 cp_parser_commit_to_tentative_parse (parser);
12287
12288 if (decl_specs->storage_class != sc_none)
12289 decl_specs->conflicting_specifiers_p = true;
12290 break;
12291
12292 /* storage-class-specifier:
12293 auto
12294 register
12295 static
12296 extern
12297 mutable
12298
12299 GNU Extension:
12300 thread */
12301 case RID_AUTO:
12302 if (cxx_dialect == cxx98)
12303 {
12304 /* Consume the token. */
12305 cp_lexer_consume_token (parser->lexer);
12306
12307 /* Complain about `auto' as a storage specifier, if
12308 we're complaining about C++0x compatibility. */
12309 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
12310 " changes meaning in C++11; please remove it");
12311
12312 /* Set the storage class anyway. */
12313 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
12314 token);
12315 }
12316 else
12317 /* C++0x auto type-specifier. */
12318 found_decl_spec = false;
12319 break;
12320
12321 case RID_REGISTER:
12322 case RID_STATIC:
12323 case RID_EXTERN:
12324 case RID_MUTABLE:
12325 /* Consume the token. */
12326 cp_lexer_consume_token (parser->lexer);
12327 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12328 token);
12329 break;
12330 case RID_THREAD:
12331 /* Consume the token. */
12332 ds = ds_thread;
12333 cp_lexer_consume_token (parser->lexer);
12334 break;
12335
12336 default:
12337 /* We did not yet find a decl-specifier yet. */
12338 found_decl_spec = false;
12339 break;
12340 }
12341
12342 if (found_decl_spec
12343 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12344 && token->keyword != RID_CONSTEXPR)
12345 error ("decl-specifier invalid in condition");
12346
12347 if (ds != ds_last)
12348 set_and_check_decl_spec_loc (decl_specs, ds, token);
12349
12350 /* Constructors are a special case. The `S' in `S()' is not a
12351 decl-specifier; it is the beginning of the declarator. */
12352 constructor_p
12353 = (!found_decl_spec
12354 && constructor_possible_p
12355 && (cp_parser_constructor_declarator_p
12356 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12357
12358 /* If we don't have a DECL_SPEC yet, then we must be looking at
12359 a type-specifier. */
12360 if (!found_decl_spec && !constructor_p)
12361 {
12362 int decl_spec_declares_class_or_enum;
12363 bool is_cv_qualifier;
12364 tree type_spec;
12365
12366 type_spec
12367 = cp_parser_type_specifier (parser, flags,
12368 decl_specs,
12369 /*is_declaration=*/true,
12370 &decl_spec_declares_class_or_enum,
12371 &is_cv_qualifier);
12372 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12373
12374 /* If this type-specifier referenced a user-defined type
12375 (a typedef, class-name, etc.), then we can't allow any
12376 more such type-specifiers henceforth.
12377
12378 [dcl.spec]
12379
12380 The longest sequence of decl-specifiers that could
12381 possibly be a type name is taken as the
12382 decl-specifier-seq of a declaration. The sequence shall
12383 be self-consistent as described below.
12384
12385 [dcl.type]
12386
12387 As a general rule, at most one type-specifier is allowed
12388 in the complete decl-specifier-seq of a declaration. The
12389 only exceptions are the following:
12390
12391 -- const or volatile can be combined with any other
12392 type-specifier.
12393
12394 -- signed or unsigned can be combined with char, long,
12395 short, or int.
12396
12397 -- ..
12398
12399 Example:
12400
12401 typedef char* Pc;
12402 void g (const int Pc);
12403
12404 Here, Pc is *not* part of the decl-specifier seq; it's
12405 the declarator. Therefore, once we see a type-specifier
12406 (other than a cv-qualifier), we forbid any additional
12407 user-defined types. We *do* still allow things like `int
12408 int' to be considered a decl-specifier-seq, and issue the
12409 error message later. */
12410 if (type_spec && !is_cv_qualifier)
12411 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12412 /* A constructor declarator cannot follow a type-specifier. */
12413 if (type_spec)
12414 {
12415 constructor_possible_p = false;
12416 found_decl_spec = true;
12417 if (!is_cv_qualifier)
12418 decl_specs->any_type_specifiers_p = true;
12419 }
12420 }
12421
12422 /* If we still do not have a DECL_SPEC, then there are no more
12423 decl-specifiers. */
12424 if (!found_decl_spec)
12425 break;
12426
12427 decl_specs->any_specifiers_p = true;
12428 /* After we see one decl-specifier, further decl-specifiers are
12429 always optional. */
12430 flags |= CP_PARSER_FLAGS_OPTIONAL;
12431 }
12432
12433 /* Don't allow a friend specifier with a class definition. */
12434 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12435 && (*declares_class_or_enum & 2))
12436 error_at (decl_specs->locations[ds_friend],
12437 "class definition may not be declared a friend");
12438 }
12439
12440 /* Parse an (optional) storage-class-specifier.
12441
12442 storage-class-specifier:
12443 auto
12444 register
12445 static
12446 extern
12447 mutable
12448
12449 GNU Extension:
12450
12451 storage-class-specifier:
12452 thread
12453
12454 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12455
12456 static tree
12457 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12458 {
12459 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12460 {
12461 case RID_AUTO:
12462 if (cxx_dialect != cxx98)
12463 return NULL_TREE;
12464 /* Fall through for C++98. */
12465
12466 case RID_REGISTER:
12467 case RID_STATIC:
12468 case RID_EXTERN:
12469 case RID_MUTABLE:
12470 case RID_THREAD:
12471 /* Consume the token. */
12472 return cp_lexer_consume_token (parser->lexer)->u.value;
12473
12474 default:
12475 return NULL_TREE;
12476 }
12477 }
12478
12479 /* Parse an (optional) function-specifier.
12480
12481 function-specifier:
12482 inline
12483 virtual
12484 explicit
12485
12486 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12487 Updates DECL_SPECS, if it is non-NULL. */
12488
12489 static tree
12490 cp_parser_function_specifier_opt (cp_parser* parser,
12491 cp_decl_specifier_seq *decl_specs)
12492 {
12493 cp_token *token = cp_lexer_peek_token (parser->lexer);
12494 switch (token->keyword)
12495 {
12496 case RID_INLINE:
12497 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12498 break;
12499
12500 case RID_VIRTUAL:
12501 /* 14.5.2.3 [temp.mem]
12502
12503 A member function template shall not be virtual. */
12504 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12505 error_at (token->location, "templates may not be %<virtual%>");
12506 else
12507 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12508 break;
12509
12510 case RID_EXPLICIT:
12511 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12512 break;
12513
12514 default:
12515 return NULL_TREE;
12516 }
12517
12518 /* Consume the token. */
12519 return cp_lexer_consume_token (parser->lexer)->u.value;
12520 }
12521
12522 /* Parse a linkage-specification.
12523
12524 linkage-specification:
12525 extern string-literal { declaration-seq [opt] }
12526 extern string-literal declaration */
12527
12528 static void
12529 cp_parser_linkage_specification (cp_parser* parser)
12530 {
12531 tree linkage;
12532
12533 /* Look for the `extern' keyword. */
12534 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12535
12536 /* Look for the string-literal. */
12537 linkage = cp_parser_string_literal (parser, false, false);
12538
12539 /* Transform the literal into an identifier. If the literal is a
12540 wide-character string, or contains embedded NULs, then we can't
12541 handle it as the user wants. */
12542 if (strlen (TREE_STRING_POINTER (linkage))
12543 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12544 {
12545 cp_parser_error (parser, "invalid linkage-specification");
12546 /* Assume C++ linkage. */
12547 linkage = lang_name_cplusplus;
12548 }
12549 else
12550 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12551
12552 /* We're now using the new linkage. */
12553 push_lang_context (linkage);
12554
12555 /* If the next token is a `{', then we're using the first
12556 production. */
12557 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12558 {
12559 cp_ensure_no_omp_declare_simd (parser);
12560 cp_ensure_no_oacc_routine (parser);
12561
12562 /* Consume the `{' token. */
12563 cp_lexer_consume_token (parser->lexer);
12564 /* Parse the declarations. */
12565 cp_parser_declaration_seq_opt (parser);
12566 /* Look for the closing `}'. */
12567 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12568 }
12569 /* Otherwise, there's just one declaration. */
12570 else
12571 {
12572 bool saved_in_unbraced_linkage_specification_p;
12573
12574 saved_in_unbraced_linkage_specification_p
12575 = parser->in_unbraced_linkage_specification_p;
12576 parser->in_unbraced_linkage_specification_p = true;
12577 cp_parser_declaration (parser);
12578 parser->in_unbraced_linkage_specification_p
12579 = saved_in_unbraced_linkage_specification_p;
12580 }
12581
12582 /* We're done with the linkage-specification. */
12583 pop_lang_context ();
12584 }
12585
12586 /* Parse a static_assert-declaration.
12587
12588 static_assert-declaration:
12589 static_assert ( constant-expression , string-literal ) ;
12590 static_assert ( constant-expression ) ; (C++1Z)
12591
12592 If MEMBER_P, this static_assert is a class member. */
12593
12594 static void
12595 cp_parser_static_assert(cp_parser *parser, bool member_p)
12596 {
12597 tree condition;
12598 tree message;
12599 cp_token *token;
12600 location_t saved_loc;
12601 bool dummy;
12602
12603 /* Peek at the `static_assert' token so we can keep track of exactly
12604 where the static assertion started. */
12605 token = cp_lexer_peek_token (parser->lexer);
12606 saved_loc = token->location;
12607
12608 /* Look for the `static_assert' keyword. */
12609 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12610 RT_STATIC_ASSERT))
12611 return;
12612
12613 /* We know we are in a static assertion; commit to any tentative
12614 parse. */
12615 if (cp_parser_parsing_tentatively (parser))
12616 cp_parser_commit_to_tentative_parse (parser);
12617
12618 /* Parse the `(' starting the static assertion condition. */
12619 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12620
12621 /* Parse the constant-expression. Allow a non-constant expression
12622 here in order to give better diagnostics in finish_static_assert. */
12623 condition =
12624 cp_parser_constant_expression (parser,
12625 /*allow_non_constant_p=*/true,
12626 /*non_constant_p=*/&dummy);
12627
12628 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12629 {
12630 if (cxx_dialect < cxx1z)
12631 pedwarn (input_location, OPT_Wpedantic,
12632 "static_assert without a message "
12633 "only available with -std=c++1z or -std=gnu++1z");
12634 /* Eat the ')' */
12635 cp_lexer_consume_token (parser->lexer);
12636 message = build_string (1, "");
12637 TREE_TYPE (message) = char_array_type_node;
12638 fix_string_type (message);
12639 }
12640 else
12641 {
12642 /* Parse the separating `,'. */
12643 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12644
12645 /* Parse the string-literal message. */
12646 message = cp_parser_string_literal (parser,
12647 /*translate=*/false,
12648 /*wide_ok=*/true);
12649
12650 /* A `)' completes the static assertion. */
12651 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12652 cp_parser_skip_to_closing_parenthesis (parser,
12653 /*recovering=*/true,
12654 /*or_comma=*/false,
12655 /*consume_paren=*/true);
12656 }
12657
12658 /* A semicolon terminates the declaration. */
12659 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12660
12661 /* Complete the static assertion, which may mean either processing
12662 the static assert now or saving it for template instantiation. */
12663 finish_static_assert (condition, message, saved_loc, member_p);
12664 }
12665
12666 /* Parse the expression in decltype ( expression ). */
12667
12668 static tree
12669 cp_parser_decltype_expr (cp_parser *parser,
12670 bool &id_expression_or_member_access_p)
12671 {
12672 cp_token *id_expr_start_token;
12673 tree expr;
12674
12675 /* First, try parsing an id-expression. */
12676 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12677 cp_parser_parse_tentatively (parser);
12678 expr = cp_parser_id_expression (parser,
12679 /*template_keyword_p=*/false,
12680 /*check_dependency_p=*/true,
12681 /*template_p=*/NULL,
12682 /*declarator_p=*/false,
12683 /*optional_p=*/false);
12684
12685 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12686 {
12687 bool non_integral_constant_expression_p = false;
12688 tree id_expression = expr;
12689 cp_id_kind idk;
12690 const char *error_msg;
12691
12692 if (identifier_p (expr))
12693 /* Lookup the name we got back from the id-expression. */
12694 expr = cp_parser_lookup_name_simple (parser, expr,
12695 id_expr_start_token->location);
12696
12697 if (expr
12698 && expr != error_mark_node
12699 && TREE_CODE (expr) != TYPE_DECL
12700 && (TREE_CODE (expr) != BIT_NOT_EXPR
12701 || !TYPE_P (TREE_OPERAND (expr, 0)))
12702 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12703 {
12704 /* Complete lookup of the id-expression. */
12705 expr = (finish_id_expression
12706 (id_expression, expr, parser->scope, &idk,
12707 /*integral_constant_expression_p=*/false,
12708 /*allow_non_integral_constant_expression_p=*/true,
12709 &non_integral_constant_expression_p,
12710 /*template_p=*/false,
12711 /*done=*/true,
12712 /*address_p=*/false,
12713 /*template_arg_p=*/false,
12714 &error_msg,
12715 id_expr_start_token->location));
12716
12717 if (expr == error_mark_node)
12718 /* We found an id-expression, but it was something that we
12719 should not have found. This is an error, not something
12720 we can recover from, so note that we found an
12721 id-expression and we'll recover as gracefully as
12722 possible. */
12723 id_expression_or_member_access_p = true;
12724 }
12725
12726 if (expr
12727 && expr != error_mark_node
12728 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12729 /* We have an id-expression. */
12730 id_expression_or_member_access_p = true;
12731 }
12732
12733 if (!id_expression_or_member_access_p)
12734 {
12735 /* Abort the id-expression parse. */
12736 cp_parser_abort_tentative_parse (parser);
12737
12738 /* Parsing tentatively, again. */
12739 cp_parser_parse_tentatively (parser);
12740
12741 /* Parse a class member access. */
12742 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12743 /*cast_p=*/false, /*decltype*/true,
12744 /*member_access_only_p=*/true, NULL);
12745
12746 if (expr
12747 && expr != error_mark_node
12748 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12749 /* We have an id-expression. */
12750 id_expression_or_member_access_p = true;
12751 }
12752
12753 if (id_expression_or_member_access_p)
12754 /* We have parsed the complete id-expression or member access. */
12755 cp_parser_parse_definitely (parser);
12756 else
12757 {
12758 /* Abort our attempt to parse an id-expression or member access
12759 expression. */
12760 cp_parser_abort_tentative_parse (parser);
12761
12762 /* Parse a full expression. */
12763 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12764 /*decltype_p=*/true);
12765 }
12766
12767 return expr;
12768 }
12769
12770 /* Parse a `decltype' type. Returns the type.
12771
12772 simple-type-specifier:
12773 decltype ( expression )
12774 C++14 proposal:
12775 decltype ( auto ) */
12776
12777 static tree
12778 cp_parser_decltype (cp_parser *parser)
12779 {
12780 tree expr;
12781 bool id_expression_or_member_access_p = false;
12782 const char *saved_message;
12783 bool saved_integral_constant_expression_p;
12784 bool saved_non_integral_constant_expression_p;
12785 bool saved_greater_than_is_operator_p;
12786 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12787
12788 if (start_token->type == CPP_DECLTYPE)
12789 {
12790 /* Already parsed. */
12791 cp_lexer_consume_token (parser->lexer);
12792 return start_token->u.value;
12793 }
12794
12795 /* Look for the `decltype' token. */
12796 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12797 return error_mark_node;
12798
12799 /* Parse the opening `('. */
12800 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12801 return error_mark_node;
12802
12803 /* decltype (auto) */
12804 if (cxx_dialect >= cxx14
12805 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12806 {
12807 cp_lexer_consume_token (parser->lexer);
12808 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12809 return error_mark_node;
12810 expr = make_decltype_auto ();
12811 AUTO_IS_DECLTYPE (expr) = true;
12812 goto rewrite;
12813 }
12814
12815 /* Types cannot be defined in a `decltype' expression. Save away the
12816 old message. */
12817 saved_message = parser->type_definition_forbidden_message;
12818
12819 /* And create the new one. */
12820 parser->type_definition_forbidden_message
12821 = G_("types may not be defined in %<decltype%> expressions");
12822
12823 /* The restrictions on constant-expressions do not apply inside
12824 decltype expressions. */
12825 saved_integral_constant_expression_p
12826 = parser->integral_constant_expression_p;
12827 saved_non_integral_constant_expression_p
12828 = parser->non_integral_constant_expression_p;
12829 parser->integral_constant_expression_p = false;
12830
12831 /* Within a parenthesized expression, a `>' token is always
12832 the greater-than operator. */
12833 saved_greater_than_is_operator_p
12834 = parser->greater_than_is_operator_p;
12835 parser->greater_than_is_operator_p = true;
12836
12837 /* Do not actually evaluate the expression. */
12838 ++cp_unevaluated_operand;
12839
12840 /* Do not warn about problems with the expression. */
12841 ++c_inhibit_evaluation_warnings;
12842
12843 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12844
12845 /* Go back to evaluating expressions. */
12846 --cp_unevaluated_operand;
12847 --c_inhibit_evaluation_warnings;
12848
12849 /* The `>' token might be the end of a template-id or
12850 template-parameter-list now. */
12851 parser->greater_than_is_operator_p
12852 = saved_greater_than_is_operator_p;
12853
12854 /* Restore the old message and the integral constant expression
12855 flags. */
12856 parser->type_definition_forbidden_message = saved_message;
12857 parser->integral_constant_expression_p
12858 = saved_integral_constant_expression_p;
12859 parser->non_integral_constant_expression_p
12860 = saved_non_integral_constant_expression_p;
12861
12862 /* Parse to the closing `)'. */
12863 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12864 {
12865 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12866 /*consume_paren=*/true);
12867 return error_mark_node;
12868 }
12869
12870 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12871 tf_warning_or_error);
12872
12873 rewrite:
12874 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12875 it again. */
12876 start_token->type = CPP_DECLTYPE;
12877 start_token->u.value = expr;
12878 start_token->keyword = RID_MAX;
12879 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12880
12881 return expr;
12882 }
12883
12884 /* Special member functions [gram.special] */
12885
12886 /* Parse a conversion-function-id.
12887
12888 conversion-function-id:
12889 operator conversion-type-id
12890
12891 Returns an IDENTIFIER_NODE representing the operator. */
12892
12893 static tree
12894 cp_parser_conversion_function_id (cp_parser* parser)
12895 {
12896 tree type;
12897 tree saved_scope;
12898 tree saved_qualifying_scope;
12899 tree saved_object_scope;
12900 tree pushed_scope = NULL_TREE;
12901
12902 /* Look for the `operator' token. */
12903 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12904 return error_mark_node;
12905 /* When we parse the conversion-type-id, the current scope will be
12906 reset. However, we need that information in able to look up the
12907 conversion function later, so we save it here. */
12908 saved_scope = parser->scope;
12909 saved_qualifying_scope = parser->qualifying_scope;
12910 saved_object_scope = parser->object_scope;
12911 /* We must enter the scope of the class so that the names of
12912 entities declared within the class are available in the
12913 conversion-type-id. For example, consider:
12914
12915 struct S {
12916 typedef int I;
12917 operator I();
12918 };
12919
12920 S::operator I() { ... }
12921
12922 In order to see that `I' is a type-name in the definition, we
12923 must be in the scope of `S'. */
12924 if (saved_scope)
12925 pushed_scope = push_scope (saved_scope);
12926 /* Parse the conversion-type-id. */
12927 type = cp_parser_conversion_type_id (parser);
12928 /* Leave the scope of the class, if any. */
12929 if (pushed_scope)
12930 pop_scope (pushed_scope);
12931 /* Restore the saved scope. */
12932 parser->scope = saved_scope;
12933 parser->qualifying_scope = saved_qualifying_scope;
12934 parser->object_scope = saved_object_scope;
12935 /* If the TYPE is invalid, indicate failure. */
12936 if (type == error_mark_node)
12937 return error_mark_node;
12938 return mangle_conv_op_name_for_type (type);
12939 }
12940
12941 /* Parse a conversion-type-id:
12942
12943 conversion-type-id:
12944 type-specifier-seq conversion-declarator [opt]
12945
12946 Returns the TYPE specified. */
12947
12948 static tree
12949 cp_parser_conversion_type_id (cp_parser* parser)
12950 {
12951 tree attributes;
12952 cp_decl_specifier_seq type_specifiers;
12953 cp_declarator *declarator;
12954 tree type_specified;
12955 const char *saved_message;
12956
12957 /* Parse the attributes. */
12958 attributes = cp_parser_attributes_opt (parser);
12959
12960 saved_message = parser->type_definition_forbidden_message;
12961 parser->type_definition_forbidden_message
12962 = G_("types may not be defined in a conversion-type-id");
12963
12964 /* Parse the type-specifiers. */
12965 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12966 /*is_trailing_return=*/false,
12967 &type_specifiers);
12968
12969 parser->type_definition_forbidden_message = saved_message;
12970
12971 /* If that didn't work, stop. */
12972 if (type_specifiers.type == error_mark_node)
12973 return error_mark_node;
12974 /* Parse the conversion-declarator. */
12975 declarator = cp_parser_conversion_declarator_opt (parser);
12976
12977 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12978 /*initialized=*/0, &attributes);
12979 if (attributes)
12980 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12981
12982 /* Don't give this error when parsing tentatively. This happens to
12983 work because we always parse this definitively once. */
12984 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12985 && type_uses_auto (type_specified))
12986 {
12987 if (cxx_dialect < cxx14)
12988 {
12989 error ("invalid use of %<auto%> in conversion operator");
12990 return error_mark_node;
12991 }
12992 else if (template_parm_scope_p ())
12993 warning (0, "use of %<auto%> in member template "
12994 "conversion operator can never be deduced");
12995 }
12996
12997 return type_specified;
12998 }
12999
13000 /* Parse an (optional) conversion-declarator.
13001
13002 conversion-declarator:
13003 ptr-operator conversion-declarator [opt]
13004
13005 */
13006
13007 static cp_declarator *
13008 cp_parser_conversion_declarator_opt (cp_parser* parser)
13009 {
13010 enum tree_code code;
13011 tree class_type, std_attributes = NULL_TREE;
13012 cp_cv_quals cv_quals;
13013
13014 /* We don't know if there's a ptr-operator next, or not. */
13015 cp_parser_parse_tentatively (parser);
13016 /* Try the ptr-operator. */
13017 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13018 &std_attributes);
13019 /* If it worked, look for more conversion-declarators. */
13020 if (cp_parser_parse_definitely (parser))
13021 {
13022 cp_declarator *declarator;
13023
13024 /* Parse another optional declarator. */
13025 declarator = cp_parser_conversion_declarator_opt (parser);
13026
13027 declarator = cp_parser_make_indirect_declarator
13028 (code, class_type, cv_quals, declarator, std_attributes);
13029
13030 return declarator;
13031 }
13032
13033 return NULL;
13034 }
13035
13036 /* Parse an (optional) ctor-initializer.
13037
13038 ctor-initializer:
13039 : mem-initializer-list
13040
13041 Returns TRUE iff the ctor-initializer was actually present. */
13042
13043 static bool
13044 cp_parser_ctor_initializer_opt (cp_parser* parser)
13045 {
13046 /* If the next token is not a `:', then there is no
13047 ctor-initializer. */
13048 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13049 {
13050 /* Do default initialization of any bases and members. */
13051 if (DECL_CONSTRUCTOR_P (current_function_decl))
13052 finish_mem_initializers (NULL_TREE);
13053
13054 return false;
13055 }
13056
13057 /* Consume the `:' token. */
13058 cp_lexer_consume_token (parser->lexer);
13059 /* And the mem-initializer-list. */
13060 cp_parser_mem_initializer_list (parser);
13061
13062 return true;
13063 }
13064
13065 /* Parse a mem-initializer-list.
13066
13067 mem-initializer-list:
13068 mem-initializer ... [opt]
13069 mem-initializer ... [opt] , mem-initializer-list */
13070
13071 static void
13072 cp_parser_mem_initializer_list (cp_parser* parser)
13073 {
13074 tree mem_initializer_list = NULL_TREE;
13075 tree target_ctor = error_mark_node;
13076 cp_token *token = cp_lexer_peek_token (parser->lexer);
13077
13078 /* Let the semantic analysis code know that we are starting the
13079 mem-initializer-list. */
13080 if (!DECL_CONSTRUCTOR_P (current_function_decl))
13081 error_at (token->location,
13082 "only constructors take member initializers");
13083
13084 /* Loop through the list. */
13085 while (true)
13086 {
13087 tree mem_initializer;
13088
13089 token = cp_lexer_peek_token (parser->lexer);
13090 /* Parse the mem-initializer. */
13091 mem_initializer = cp_parser_mem_initializer (parser);
13092 /* If the next token is a `...', we're expanding member initializers. */
13093 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13094 {
13095 /* Consume the `...'. */
13096 cp_lexer_consume_token (parser->lexer);
13097
13098 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13099 can be expanded but members cannot. */
13100 if (mem_initializer != error_mark_node
13101 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
13102 {
13103 error_at (token->location,
13104 "cannot expand initializer for member %<%D%>",
13105 TREE_PURPOSE (mem_initializer));
13106 mem_initializer = error_mark_node;
13107 }
13108
13109 /* Construct the pack expansion type. */
13110 if (mem_initializer != error_mark_node)
13111 mem_initializer = make_pack_expansion (mem_initializer);
13112 }
13113 if (target_ctor != error_mark_node
13114 && mem_initializer != error_mark_node)
13115 {
13116 error ("mem-initializer for %qD follows constructor delegation",
13117 TREE_PURPOSE (mem_initializer));
13118 mem_initializer = error_mark_node;
13119 }
13120 /* Look for a target constructor. */
13121 if (mem_initializer != error_mark_node
13122 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
13123 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
13124 {
13125 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
13126 if (mem_initializer_list)
13127 {
13128 error ("constructor delegation follows mem-initializer for %qD",
13129 TREE_PURPOSE (mem_initializer_list));
13130 mem_initializer = error_mark_node;
13131 }
13132 target_ctor = mem_initializer;
13133 }
13134 /* Add it to the list, unless it was erroneous. */
13135 if (mem_initializer != error_mark_node)
13136 {
13137 TREE_CHAIN (mem_initializer) = mem_initializer_list;
13138 mem_initializer_list = mem_initializer;
13139 }
13140 /* If the next token is not a `,', we're done. */
13141 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13142 break;
13143 /* Consume the `,' token. */
13144 cp_lexer_consume_token (parser->lexer);
13145 }
13146
13147 /* Perform semantic analysis. */
13148 if (DECL_CONSTRUCTOR_P (current_function_decl))
13149 finish_mem_initializers (mem_initializer_list);
13150 }
13151
13152 /* Parse a mem-initializer.
13153
13154 mem-initializer:
13155 mem-initializer-id ( expression-list [opt] )
13156 mem-initializer-id braced-init-list
13157
13158 GNU extension:
13159
13160 mem-initializer:
13161 ( expression-list [opt] )
13162
13163 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
13164 class) or FIELD_DECL (for a non-static data member) to initialize;
13165 the TREE_VALUE is the expression-list. An empty initialization
13166 list is represented by void_list_node. */
13167
13168 static tree
13169 cp_parser_mem_initializer (cp_parser* parser)
13170 {
13171 tree mem_initializer_id;
13172 tree expression_list;
13173 tree member;
13174 cp_token *token = cp_lexer_peek_token (parser->lexer);
13175
13176 /* Find out what is being initialized. */
13177 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13178 {
13179 permerror (token->location,
13180 "anachronistic old-style base class initializer");
13181 mem_initializer_id = NULL_TREE;
13182 }
13183 else
13184 {
13185 mem_initializer_id = cp_parser_mem_initializer_id (parser);
13186 if (mem_initializer_id == error_mark_node)
13187 return mem_initializer_id;
13188 }
13189 member = expand_member_init (mem_initializer_id);
13190 if (member && !DECL_P (member))
13191 in_base_initializer = 1;
13192
13193 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13194 {
13195 bool expr_non_constant_p;
13196 cp_lexer_set_source_position (parser->lexer);
13197 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13198 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
13199 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
13200 expression_list = build_tree_list (NULL_TREE, expression_list);
13201 }
13202 else
13203 {
13204 vec<tree, va_gc> *vec;
13205 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
13206 /*cast_p=*/false,
13207 /*allow_expansion_p=*/true,
13208 /*non_constant_p=*/NULL);
13209 if (vec == NULL)
13210 return error_mark_node;
13211 expression_list = build_tree_list_vec (vec);
13212 release_tree_vector (vec);
13213 }
13214
13215 if (expression_list == error_mark_node)
13216 return error_mark_node;
13217 if (!expression_list)
13218 expression_list = void_type_node;
13219
13220 in_base_initializer = 0;
13221
13222 return member ? build_tree_list (member, expression_list) : error_mark_node;
13223 }
13224
13225 /* Parse a mem-initializer-id.
13226
13227 mem-initializer-id:
13228 :: [opt] nested-name-specifier [opt] class-name
13229 decltype-specifier (C++11)
13230 identifier
13231
13232 Returns a TYPE indicating the class to be initialized for the first
13233 production (and the second in C++11). Returns an IDENTIFIER_NODE
13234 indicating the data member to be initialized for the last production. */
13235
13236 static tree
13237 cp_parser_mem_initializer_id (cp_parser* parser)
13238 {
13239 bool global_scope_p;
13240 bool nested_name_specifier_p;
13241 bool template_p = false;
13242 tree id;
13243
13244 cp_token *token = cp_lexer_peek_token (parser->lexer);
13245
13246 /* `typename' is not allowed in this context ([temp.res]). */
13247 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13248 {
13249 error_at (token->location,
13250 "keyword %<typename%> not allowed in this context (a qualified "
13251 "member initializer is implicitly a type)");
13252 cp_lexer_consume_token (parser->lexer);
13253 }
13254 /* Look for the optional `::' operator. */
13255 global_scope_p
13256 = (cp_parser_global_scope_opt (parser,
13257 /*current_scope_valid_p=*/false)
13258 != NULL_TREE);
13259 /* Look for the optional nested-name-specifier. The simplest way to
13260 implement:
13261
13262 [temp.res]
13263
13264 The keyword `typename' is not permitted in a base-specifier or
13265 mem-initializer; in these contexts a qualified name that
13266 depends on a template-parameter is implicitly assumed to be a
13267 type name.
13268
13269 is to assume that we have seen the `typename' keyword at this
13270 point. */
13271 nested_name_specifier_p
13272 = (cp_parser_nested_name_specifier_opt (parser,
13273 /*typename_keyword_p=*/true,
13274 /*check_dependency_p=*/true,
13275 /*type_p=*/true,
13276 /*is_declaration=*/true)
13277 != NULL_TREE);
13278 if (nested_name_specifier_p)
13279 template_p = cp_parser_optional_template_keyword (parser);
13280 /* If there is a `::' operator or a nested-name-specifier, then we
13281 are definitely looking for a class-name. */
13282 if (global_scope_p || nested_name_specifier_p)
13283 return cp_parser_class_name (parser,
13284 /*typename_keyword_p=*/true,
13285 /*template_keyword_p=*/template_p,
13286 typename_type,
13287 /*check_dependency_p=*/true,
13288 /*class_head_p=*/false,
13289 /*is_declaration=*/true);
13290 /* Otherwise, we could also be looking for an ordinary identifier. */
13291 cp_parser_parse_tentatively (parser);
13292 if (cp_lexer_next_token_is_decltype (parser->lexer))
13293 /* Try a decltype-specifier. */
13294 id = cp_parser_decltype (parser);
13295 else
13296 /* Otherwise, try a class-name. */
13297 id = cp_parser_class_name (parser,
13298 /*typename_keyword_p=*/true,
13299 /*template_keyword_p=*/false,
13300 none_type,
13301 /*check_dependency_p=*/true,
13302 /*class_head_p=*/false,
13303 /*is_declaration=*/true);
13304 /* If we found one, we're done. */
13305 if (cp_parser_parse_definitely (parser))
13306 return id;
13307 /* Otherwise, look for an ordinary identifier. */
13308 return cp_parser_identifier (parser);
13309 }
13310
13311 /* Overloading [gram.over] */
13312
13313 /* Parse an operator-function-id.
13314
13315 operator-function-id:
13316 operator operator
13317
13318 Returns an IDENTIFIER_NODE for the operator which is a
13319 human-readable spelling of the identifier, e.g., `operator +'. */
13320
13321 static tree
13322 cp_parser_operator_function_id (cp_parser* parser)
13323 {
13324 /* Look for the `operator' keyword. */
13325 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13326 return error_mark_node;
13327 /* And then the name of the operator itself. */
13328 return cp_parser_operator (parser);
13329 }
13330
13331 /* Return an identifier node for a user-defined literal operator.
13332 The suffix identifier is chained to the operator name identifier. */
13333
13334 static tree
13335 cp_literal_operator_id (const char* name)
13336 {
13337 tree identifier;
13338 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13339 + strlen (name) + 10);
13340 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13341 identifier = get_identifier (buffer);
13342
13343 return identifier;
13344 }
13345
13346 /* Parse an operator.
13347
13348 operator:
13349 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13350 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13351 || ++ -- , ->* -> () []
13352
13353 GNU Extensions:
13354
13355 operator:
13356 <? >? <?= >?=
13357
13358 Returns an IDENTIFIER_NODE for the operator which is a
13359 human-readable spelling of the identifier, e.g., `operator +'. */
13360
13361 static tree
13362 cp_parser_operator (cp_parser* parser)
13363 {
13364 tree id = NULL_TREE;
13365 cp_token *token;
13366 bool utf8 = false;
13367
13368 /* Peek at the next token. */
13369 token = cp_lexer_peek_token (parser->lexer);
13370 /* Figure out which operator we have. */
13371 switch (token->type)
13372 {
13373 case CPP_KEYWORD:
13374 {
13375 enum tree_code op;
13376
13377 /* The keyword should be either `new' or `delete'. */
13378 if (token->keyword == RID_NEW)
13379 op = NEW_EXPR;
13380 else if (token->keyword == RID_DELETE)
13381 op = DELETE_EXPR;
13382 else
13383 break;
13384
13385 /* Consume the `new' or `delete' token. */
13386 cp_lexer_consume_token (parser->lexer);
13387
13388 /* Peek at the next token. */
13389 token = cp_lexer_peek_token (parser->lexer);
13390 /* If it's a `[' token then this is the array variant of the
13391 operator. */
13392 if (token->type == CPP_OPEN_SQUARE)
13393 {
13394 /* Consume the `[' token. */
13395 cp_lexer_consume_token (parser->lexer);
13396 /* Look for the `]' token. */
13397 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13398 id = ansi_opname (op == NEW_EXPR
13399 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13400 }
13401 /* Otherwise, we have the non-array variant. */
13402 else
13403 id = ansi_opname (op);
13404
13405 return id;
13406 }
13407
13408 case CPP_PLUS:
13409 id = ansi_opname (PLUS_EXPR);
13410 break;
13411
13412 case CPP_MINUS:
13413 id = ansi_opname (MINUS_EXPR);
13414 break;
13415
13416 case CPP_MULT:
13417 id = ansi_opname (MULT_EXPR);
13418 break;
13419
13420 case CPP_DIV:
13421 id = ansi_opname (TRUNC_DIV_EXPR);
13422 break;
13423
13424 case CPP_MOD:
13425 id = ansi_opname (TRUNC_MOD_EXPR);
13426 break;
13427
13428 case CPP_XOR:
13429 id = ansi_opname (BIT_XOR_EXPR);
13430 break;
13431
13432 case CPP_AND:
13433 id = ansi_opname (BIT_AND_EXPR);
13434 break;
13435
13436 case CPP_OR:
13437 id = ansi_opname (BIT_IOR_EXPR);
13438 break;
13439
13440 case CPP_COMPL:
13441 id = ansi_opname (BIT_NOT_EXPR);
13442 break;
13443
13444 case CPP_NOT:
13445 id = ansi_opname (TRUTH_NOT_EXPR);
13446 break;
13447
13448 case CPP_EQ:
13449 id = ansi_assopname (NOP_EXPR);
13450 break;
13451
13452 case CPP_LESS:
13453 id = ansi_opname (LT_EXPR);
13454 break;
13455
13456 case CPP_GREATER:
13457 id = ansi_opname (GT_EXPR);
13458 break;
13459
13460 case CPP_PLUS_EQ:
13461 id = ansi_assopname (PLUS_EXPR);
13462 break;
13463
13464 case CPP_MINUS_EQ:
13465 id = ansi_assopname (MINUS_EXPR);
13466 break;
13467
13468 case CPP_MULT_EQ:
13469 id = ansi_assopname (MULT_EXPR);
13470 break;
13471
13472 case CPP_DIV_EQ:
13473 id = ansi_assopname (TRUNC_DIV_EXPR);
13474 break;
13475
13476 case CPP_MOD_EQ:
13477 id = ansi_assopname (TRUNC_MOD_EXPR);
13478 break;
13479
13480 case CPP_XOR_EQ:
13481 id = ansi_assopname (BIT_XOR_EXPR);
13482 break;
13483
13484 case CPP_AND_EQ:
13485 id = ansi_assopname (BIT_AND_EXPR);
13486 break;
13487
13488 case CPP_OR_EQ:
13489 id = ansi_assopname (BIT_IOR_EXPR);
13490 break;
13491
13492 case CPP_LSHIFT:
13493 id = ansi_opname (LSHIFT_EXPR);
13494 break;
13495
13496 case CPP_RSHIFT:
13497 id = ansi_opname (RSHIFT_EXPR);
13498 break;
13499
13500 case CPP_LSHIFT_EQ:
13501 id = ansi_assopname (LSHIFT_EXPR);
13502 break;
13503
13504 case CPP_RSHIFT_EQ:
13505 id = ansi_assopname (RSHIFT_EXPR);
13506 break;
13507
13508 case CPP_EQ_EQ:
13509 id = ansi_opname (EQ_EXPR);
13510 break;
13511
13512 case CPP_NOT_EQ:
13513 id = ansi_opname (NE_EXPR);
13514 break;
13515
13516 case CPP_LESS_EQ:
13517 id = ansi_opname (LE_EXPR);
13518 break;
13519
13520 case CPP_GREATER_EQ:
13521 id = ansi_opname (GE_EXPR);
13522 break;
13523
13524 case CPP_AND_AND:
13525 id = ansi_opname (TRUTH_ANDIF_EXPR);
13526 break;
13527
13528 case CPP_OR_OR:
13529 id = ansi_opname (TRUTH_ORIF_EXPR);
13530 break;
13531
13532 case CPP_PLUS_PLUS:
13533 id = ansi_opname (POSTINCREMENT_EXPR);
13534 break;
13535
13536 case CPP_MINUS_MINUS:
13537 id = ansi_opname (PREDECREMENT_EXPR);
13538 break;
13539
13540 case CPP_COMMA:
13541 id = ansi_opname (COMPOUND_EXPR);
13542 break;
13543
13544 case CPP_DEREF_STAR:
13545 id = ansi_opname (MEMBER_REF);
13546 break;
13547
13548 case CPP_DEREF:
13549 id = ansi_opname (COMPONENT_REF);
13550 break;
13551
13552 case CPP_OPEN_PAREN:
13553 /* Consume the `('. */
13554 cp_lexer_consume_token (parser->lexer);
13555 /* Look for the matching `)'. */
13556 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13557 return ansi_opname (CALL_EXPR);
13558
13559 case CPP_OPEN_SQUARE:
13560 /* Consume the `['. */
13561 cp_lexer_consume_token (parser->lexer);
13562 /* Look for the matching `]'. */
13563 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13564 return ansi_opname (ARRAY_REF);
13565
13566 case CPP_UTF8STRING:
13567 case CPP_UTF8STRING_USERDEF:
13568 utf8 = true;
13569 case CPP_STRING:
13570 case CPP_WSTRING:
13571 case CPP_STRING16:
13572 case CPP_STRING32:
13573 case CPP_STRING_USERDEF:
13574 case CPP_WSTRING_USERDEF:
13575 case CPP_STRING16_USERDEF:
13576 case CPP_STRING32_USERDEF:
13577 {
13578 tree str, string_tree;
13579 int sz, len;
13580
13581 if (cxx_dialect == cxx98)
13582 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13583
13584 /* Consume the string. */
13585 str = cp_parser_string_literal (parser, /*translate=*/true,
13586 /*wide_ok=*/true, /*lookup_udlit=*/false);
13587 if (str == error_mark_node)
13588 return error_mark_node;
13589 else if (TREE_CODE (str) == USERDEF_LITERAL)
13590 {
13591 string_tree = USERDEF_LITERAL_VALUE (str);
13592 id = USERDEF_LITERAL_SUFFIX_ID (str);
13593 }
13594 else
13595 {
13596 string_tree = str;
13597 /* Look for the suffix identifier. */
13598 token = cp_lexer_peek_token (parser->lexer);
13599 if (token->type == CPP_NAME)
13600 id = cp_parser_identifier (parser);
13601 else if (token->type == CPP_KEYWORD)
13602 {
13603 error ("unexpected keyword;"
13604 " remove space between quotes and suffix identifier");
13605 return error_mark_node;
13606 }
13607 else
13608 {
13609 error ("expected suffix identifier");
13610 return error_mark_node;
13611 }
13612 }
13613 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13614 (TREE_TYPE (TREE_TYPE (string_tree))));
13615 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13616 if (len != 0)
13617 {
13618 error ("expected empty string after %<operator%> keyword");
13619 return error_mark_node;
13620 }
13621 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13622 != char_type_node)
13623 {
13624 error ("invalid encoding prefix in literal operator");
13625 return error_mark_node;
13626 }
13627 if (id != error_mark_node)
13628 {
13629 const char *name = IDENTIFIER_POINTER (id);
13630 id = cp_literal_operator_id (name);
13631 }
13632 return id;
13633 }
13634
13635 default:
13636 /* Anything else is an error. */
13637 break;
13638 }
13639
13640 /* If we have selected an identifier, we need to consume the
13641 operator token. */
13642 if (id)
13643 cp_lexer_consume_token (parser->lexer);
13644 /* Otherwise, no valid operator name was present. */
13645 else
13646 {
13647 cp_parser_error (parser, "expected operator");
13648 id = error_mark_node;
13649 }
13650
13651 return id;
13652 }
13653
13654 /* Parse a template-declaration.
13655
13656 template-declaration:
13657 export [opt] template < template-parameter-list > declaration
13658
13659 If MEMBER_P is TRUE, this template-declaration occurs within a
13660 class-specifier.
13661
13662 The grammar rule given by the standard isn't correct. What
13663 is really meant is:
13664
13665 template-declaration:
13666 export [opt] template-parameter-list-seq
13667 decl-specifier-seq [opt] init-declarator [opt] ;
13668 export [opt] template-parameter-list-seq
13669 function-definition
13670
13671 template-parameter-list-seq:
13672 template-parameter-list-seq [opt]
13673 template < template-parameter-list >
13674
13675 Concept Extensions:
13676
13677 template-parameter-list-seq:
13678 template < template-parameter-list > requires-clause [opt]
13679
13680 requires-clause:
13681 requires logical-or-expression */
13682
13683 static void
13684 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13685 {
13686 /* Check for `export'. */
13687 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13688 {
13689 /* Consume the `export' token. */
13690 cp_lexer_consume_token (parser->lexer);
13691 /* Warn that we do not support `export'. */
13692 warning (0, "keyword %<export%> not implemented, and will be ignored");
13693 }
13694
13695 cp_parser_template_declaration_after_export (parser, member_p);
13696 }
13697
13698 /* Parse a template-parameter-list.
13699
13700 template-parameter-list:
13701 template-parameter
13702 template-parameter-list , template-parameter
13703
13704 Returns a TREE_LIST. Each node represents a template parameter.
13705 The nodes are connected via their TREE_CHAINs. */
13706
13707 static tree
13708 cp_parser_template_parameter_list (cp_parser* parser)
13709 {
13710 tree parameter_list = NULL_TREE;
13711
13712 begin_template_parm_list ();
13713
13714 /* The loop below parses the template parms. We first need to know
13715 the total number of template parms to be able to compute proper
13716 canonical types of each dependent type. So after the loop, when
13717 we know the total number of template parms,
13718 end_template_parm_list computes the proper canonical types and
13719 fixes up the dependent types accordingly. */
13720 while (true)
13721 {
13722 tree parameter;
13723 bool is_non_type;
13724 bool is_parameter_pack;
13725 location_t parm_loc;
13726
13727 /* Parse the template-parameter. */
13728 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13729 parameter = cp_parser_template_parameter (parser,
13730 &is_non_type,
13731 &is_parameter_pack);
13732 /* Add it to the list. */
13733 if (parameter != error_mark_node)
13734 parameter_list = process_template_parm (parameter_list,
13735 parm_loc,
13736 parameter,
13737 is_non_type,
13738 is_parameter_pack);
13739 else
13740 {
13741 tree err_parm = build_tree_list (parameter, parameter);
13742 parameter_list = chainon (parameter_list, err_parm);
13743 }
13744
13745 /* If the next token is not a `,', we're done. */
13746 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13747 break;
13748 /* Otherwise, consume the `,' token. */
13749 cp_lexer_consume_token (parser->lexer);
13750 }
13751
13752 return end_template_parm_list (parameter_list);
13753 }
13754
13755 /* Parse a introduction-list.
13756
13757 introduction-list:
13758 introduced-parameter
13759 introduction-list , introduced-parameter
13760
13761 introduced-parameter:
13762 ...[opt] identifier
13763
13764 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
13765 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
13766 WILDCARD_DECL will also have DECL_NAME set and token location in
13767 DECL_SOURCE_LOCATION. */
13768
13769 static tree
13770 cp_parser_introduction_list (cp_parser *parser)
13771 {
13772 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
13773
13774 while (true)
13775 {
13776 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
13777 if (is_pack)
13778 cp_lexer_consume_token (parser->lexer);
13779
13780 /* Build placeholder. */
13781 tree parm = build_nt (WILDCARD_DECL);
13782 DECL_SOURCE_LOCATION (parm)
13783 = cp_lexer_peek_token (parser->lexer)->location;
13784 DECL_NAME (parm) = cp_parser_identifier (parser);
13785 WILDCARD_PACK_P (parm) = is_pack;
13786 vec_safe_push (introduction_vec, parm);
13787
13788 /* If the next token is not a `,', we're done. */
13789 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13790 break;
13791 /* Otherwise, consume the `,' token. */
13792 cp_lexer_consume_token (parser->lexer);
13793 }
13794
13795 /* Convert the vec into a TREE_VEC. */
13796 tree introduction_list = make_tree_vec (introduction_vec->length ());
13797 unsigned int n;
13798 tree parm;
13799 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
13800 TREE_VEC_ELT (introduction_list, n) = parm;
13801
13802 release_tree_vector (introduction_vec);
13803 return introduction_list;
13804 }
13805
13806 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
13807 is an abstract declarator. */
13808
13809 static inline cp_declarator*
13810 get_id_declarator (cp_declarator *declarator)
13811 {
13812 cp_declarator *d = declarator;
13813 while (d && d->kind != cdk_id)
13814 d = d->declarator;
13815 return d;
13816 }
13817
13818 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
13819 is an abstract declarator. */
13820
13821 static inline tree
13822 get_unqualified_id (cp_declarator *declarator)
13823 {
13824 declarator = get_id_declarator (declarator);
13825 if (declarator)
13826 return declarator->u.id.unqualified_name;
13827 else
13828 return NULL_TREE;
13829 }
13830
13831 /* Returns true if DECL represents a constrained-parameter. */
13832
13833 static inline bool
13834 is_constrained_parameter (tree decl)
13835 {
13836 return (decl
13837 && TREE_CODE (decl) == TYPE_DECL
13838 && CONSTRAINED_PARM_CONCEPT (decl)
13839 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
13840 }
13841
13842 /* Returns true if PARM declares a constrained-parameter. */
13843
13844 static inline bool
13845 is_constrained_parameter (cp_parameter_declarator *parm)
13846 {
13847 return is_constrained_parameter (parm->decl_specifiers.type);
13848 }
13849
13850 /* Check that the type parameter is only a declarator-id, and that its
13851 type is not cv-qualified. */
13852
13853 bool
13854 cp_parser_check_constrained_type_parm (cp_parser *parser,
13855 cp_parameter_declarator *parm)
13856 {
13857 if (!parm->declarator)
13858 return true;
13859
13860 if (parm->declarator->kind != cdk_id)
13861 {
13862 cp_parser_error (parser, "invalid constrained type parameter");
13863 return false;
13864 }
13865
13866 /* Don't allow cv-qualified type parameters. */
13867 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
13868 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
13869 {
13870 cp_parser_error (parser, "cv-qualified type parameter");
13871 return false;
13872 }
13873
13874 return true;
13875 }
13876
13877 /* Finish parsing/processing a template type parameter and checking
13878 various restrictions. */
13879
13880 static inline tree
13881 cp_parser_constrained_type_template_parm (cp_parser *parser,
13882 tree id,
13883 cp_parameter_declarator* parmdecl)
13884 {
13885 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
13886 return finish_template_type_parm (class_type_node, id);
13887 else
13888 return error_mark_node;
13889 }
13890
13891 static tree
13892 finish_constrained_template_template_parm (tree proto, tree id)
13893 {
13894 /* FIXME: This should probably be copied, and we may need to adjust
13895 the template parameter depths. */
13896 tree saved_parms = current_template_parms;
13897 begin_template_parm_list ();
13898 current_template_parms = DECL_TEMPLATE_PARMS (proto);
13899 end_template_parm_list ();
13900
13901 tree parm = finish_template_template_parm (class_type_node, id);
13902 current_template_parms = saved_parms;
13903
13904 return parm;
13905 }
13906
13907 /* Finish parsing/processing a template template parameter by borrowing
13908 the template parameter list from the prototype parameter. */
13909
13910 static tree
13911 cp_parser_constrained_template_template_parm (cp_parser *parser,
13912 tree proto,
13913 tree id,
13914 cp_parameter_declarator *parmdecl)
13915 {
13916 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
13917 return error_mark_node;
13918 return finish_constrained_template_template_parm (proto, id);
13919 }
13920
13921 /* Create a new non-type template parameter from the given PARM
13922 declarator. */
13923
13924 static tree
13925 constrained_non_type_template_parm (bool *is_non_type,
13926 cp_parameter_declarator *parm)
13927 {
13928 *is_non_type = true;
13929 cp_declarator *decl = parm->declarator;
13930 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
13931 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
13932 return grokdeclarator (decl, specs, TPARM, 0, NULL);
13933 }
13934
13935 /* Build a constrained template parameter based on the PARMDECL
13936 declarator. The type of PARMDECL is the constrained type, which
13937 refers to the prototype template parameter that ultimately
13938 specifies the type of the declared parameter. */
13939
13940 static tree
13941 finish_constrained_parameter (cp_parser *parser,
13942 cp_parameter_declarator *parmdecl,
13943 bool *is_non_type,
13944 bool *is_parameter_pack)
13945 {
13946 tree decl = parmdecl->decl_specifiers.type;
13947 tree id = get_unqualified_id (parmdecl->declarator);
13948 tree def = parmdecl->default_argument;
13949 tree proto = DECL_INITIAL (decl);
13950
13951 /* A template parameter constrained by a variadic concept shall also
13952 be declared as a template parameter pack. */
13953 bool is_variadic = template_parameter_pack_p (proto);
13954 if (is_variadic && !*is_parameter_pack)
13955 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
13956
13957 /* Build the parameter. Return an error if the declarator was invalid. */
13958 tree parm;
13959 if (TREE_CODE (proto) == TYPE_DECL)
13960 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
13961 else if (TREE_CODE (proto) == TEMPLATE_DECL)
13962 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
13963 parmdecl);
13964 else
13965 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
13966 if (parm == error_mark_node)
13967 return error_mark_node;
13968
13969 /* Finish the parameter decl and create a node attaching the
13970 default argument and constraint. */
13971 parm = build_tree_list (def, parm);
13972 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
13973
13974 return parm;
13975 }
13976
13977 /* Returns true if the parsed type actually represents the declaration
13978 of a type template-parameter. */
13979
13980 static inline bool
13981 declares_constrained_type_template_parameter (tree type)
13982 {
13983 return (is_constrained_parameter (type)
13984 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
13985 }
13986
13987
13988 /* Returns true if the parsed type actually represents the declaration of
13989 a template template-parameter. */
13990
13991 static bool
13992 declares_constrained_template_template_parameter (tree type)
13993 {
13994 return (is_constrained_parameter (type)
13995 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
13996 }
13997
13998 /* Parse a default argument for a type template-parameter.
13999 Note that diagnostics are handled in cp_parser_template_parameter. */
14000
14001 static tree
14002 cp_parser_default_type_template_argument (cp_parser *parser)
14003 {
14004 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14005
14006 /* Consume the `=' token. */
14007 cp_lexer_consume_token (parser->lexer);
14008
14009 /* Parse the default-argument. */
14010 push_deferring_access_checks (dk_no_deferred);
14011 tree default_argument = cp_parser_type_id (parser);
14012 pop_deferring_access_checks ();
14013
14014 return default_argument;
14015 }
14016
14017 /* Parse a default argument for a template template-parameter. */
14018
14019 static tree
14020 cp_parser_default_template_template_argument (cp_parser *parser)
14021 {
14022 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14023
14024 bool is_template;
14025
14026 /* Consume the `='. */
14027 cp_lexer_consume_token (parser->lexer);
14028 /* Parse the id-expression. */
14029 push_deferring_access_checks (dk_no_deferred);
14030 /* save token before parsing the id-expression, for error
14031 reporting */
14032 const cp_token* token = cp_lexer_peek_token (parser->lexer);
14033 tree default_argument
14034 = cp_parser_id_expression (parser,
14035 /*template_keyword_p=*/false,
14036 /*check_dependency_p=*/true,
14037 /*template_p=*/&is_template,
14038 /*declarator_p=*/false,
14039 /*optional_p=*/false);
14040 if (TREE_CODE (default_argument) == TYPE_DECL)
14041 /* If the id-expression was a template-id that refers to
14042 a template-class, we already have the declaration here,
14043 so no further lookup is needed. */
14044 ;
14045 else
14046 /* Look up the name. */
14047 default_argument
14048 = cp_parser_lookup_name (parser, default_argument,
14049 none_type,
14050 /*is_template=*/is_template,
14051 /*is_namespace=*/false,
14052 /*check_dependency=*/true,
14053 /*ambiguous_decls=*/NULL,
14054 token->location);
14055 /* See if the default argument is valid. */
14056 default_argument = check_template_template_default_arg (default_argument);
14057 pop_deferring_access_checks ();
14058 return default_argument;
14059 }
14060
14061 /* Parse a template-parameter.
14062
14063 template-parameter:
14064 type-parameter
14065 parameter-declaration
14066
14067 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
14068 the parameter. The TREE_PURPOSE is the default value, if any.
14069 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
14070 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
14071 set to true iff this parameter is a parameter pack. */
14072
14073 static tree
14074 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
14075 bool *is_parameter_pack)
14076 {
14077 cp_token *token;
14078 cp_parameter_declarator *parameter_declarator;
14079 tree parm;
14080
14081 /* Assume it is a type parameter or a template parameter. */
14082 *is_non_type = false;
14083 /* Assume it not a parameter pack. */
14084 *is_parameter_pack = false;
14085 /* Peek at the next token. */
14086 token = cp_lexer_peek_token (parser->lexer);
14087 /* If it is `class' or `template', we have a type-parameter. */
14088 if (token->keyword == RID_TEMPLATE)
14089 return cp_parser_type_parameter (parser, is_parameter_pack);
14090 /* If it is `class' or `typename' we do not know yet whether it is a
14091 type parameter or a non-type parameter. Consider:
14092
14093 template <typename T, typename T::X X> ...
14094
14095 or:
14096
14097 template <class C, class D*> ...
14098
14099 Here, the first parameter is a type parameter, and the second is
14100 a non-type parameter. We can tell by looking at the token after
14101 the identifier -- if it is a `,', `=', or `>' then we have a type
14102 parameter. */
14103 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
14104 {
14105 /* Peek at the token after `class' or `typename'. */
14106 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14107 /* If it's an ellipsis, we have a template type parameter
14108 pack. */
14109 if (token->type == CPP_ELLIPSIS)
14110 return cp_parser_type_parameter (parser, is_parameter_pack);
14111 /* If it's an identifier, skip it. */
14112 if (token->type == CPP_NAME)
14113 token = cp_lexer_peek_nth_token (parser->lexer, 3);
14114 /* Now, see if the token looks like the end of a template
14115 parameter. */
14116 if (token->type == CPP_COMMA
14117 || token->type == CPP_EQ
14118 || token->type == CPP_GREATER)
14119 return cp_parser_type_parameter (parser, is_parameter_pack);
14120 }
14121
14122 /* Otherwise, it is a non-type parameter or a constrained parameter.
14123
14124 [temp.param]
14125
14126 When parsing a default template-argument for a non-type
14127 template-parameter, the first non-nested `>' is taken as the end
14128 of the template parameter-list rather than a greater-than
14129 operator. */
14130 parameter_declarator
14131 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
14132 /*parenthesized_p=*/NULL);
14133
14134 if (!parameter_declarator)
14135 return error_mark_node;
14136
14137 /* If the parameter declaration is marked as a parameter pack, set
14138 *IS_PARAMETER_PACK to notify the caller. */
14139 if (parameter_declarator->template_parameter_pack_p)
14140 *is_parameter_pack = true;
14141
14142 if (parameter_declarator->default_argument)
14143 {
14144 /* Can happen in some cases of erroneous input (c++/34892). */
14145 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14146 /* Consume the `...' for better error recovery. */
14147 cp_lexer_consume_token (parser->lexer);
14148 }
14149
14150 // The parameter may have been constrained.
14151 if (is_constrained_parameter (parameter_declarator))
14152 return finish_constrained_parameter (parser,
14153 parameter_declarator,
14154 is_non_type,
14155 is_parameter_pack);
14156
14157 // Now we're sure that the parameter is a non-type parameter.
14158 *is_non_type = true;
14159
14160 parm = grokdeclarator (parameter_declarator->declarator,
14161 &parameter_declarator->decl_specifiers,
14162 TPARM, /*initialized=*/0,
14163 /*attrlist=*/NULL);
14164 if (parm == error_mark_node)
14165 return error_mark_node;
14166
14167 return build_tree_list (parameter_declarator->default_argument, parm);
14168 }
14169
14170 /* Parse a type-parameter.
14171
14172 type-parameter:
14173 class identifier [opt]
14174 class identifier [opt] = type-id
14175 typename identifier [opt]
14176 typename identifier [opt] = type-id
14177 template < template-parameter-list > class identifier [opt]
14178 template < template-parameter-list > class identifier [opt]
14179 = id-expression
14180
14181 GNU Extension (variadic templates):
14182
14183 type-parameter:
14184 class ... identifier [opt]
14185 typename ... identifier [opt]
14186
14187 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
14188 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
14189 the declaration of the parameter.
14190
14191 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14192
14193 static tree
14194 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
14195 {
14196 cp_token *token;
14197 tree parameter;
14198
14199 /* Look for a keyword to tell us what kind of parameter this is. */
14200 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
14201 if (!token)
14202 return error_mark_node;
14203
14204 switch (token->keyword)
14205 {
14206 case RID_CLASS:
14207 case RID_TYPENAME:
14208 {
14209 tree identifier;
14210 tree default_argument;
14211
14212 /* If the next token is an ellipsis, we have a template
14213 argument pack. */
14214 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14215 {
14216 /* Consume the `...' token. */
14217 cp_lexer_consume_token (parser->lexer);
14218 maybe_warn_variadic_templates ();
14219
14220 *is_parameter_pack = true;
14221 }
14222
14223 /* If the next token is an identifier, then it names the
14224 parameter. */
14225 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14226 identifier = cp_parser_identifier (parser);
14227 else
14228 identifier = NULL_TREE;
14229
14230 /* Create the parameter. */
14231 parameter = finish_template_type_parm (class_type_node, identifier);
14232
14233 /* If the next token is an `=', we have a default argument. */
14234 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14235 {
14236 default_argument
14237 = cp_parser_default_type_template_argument (parser);
14238
14239 /* Template parameter packs cannot have default
14240 arguments. */
14241 if (*is_parameter_pack)
14242 {
14243 if (identifier)
14244 error_at (token->location,
14245 "template parameter pack %qD cannot have a "
14246 "default argument", identifier);
14247 else
14248 error_at (token->location,
14249 "template parameter packs cannot have "
14250 "default arguments");
14251 default_argument = NULL_TREE;
14252 }
14253 else if (check_for_bare_parameter_packs (default_argument))
14254 default_argument = error_mark_node;
14255 }
14256 else
14257 default_argument = NULL_TREE;
14258
14259 /* Create the combined representation of the parameter and the
14260 default argument. */
14261 parameter = build_tree_list (default_argument, parameter);
14262 }
14263 break;
14264
14265 case RID_TEMPLATE:
14266 {
14267 tree identifier;
14268 tree default_argument;
14269
14270 /* Look for the `<'. */
14271 cp_parser_require (parser, CPP_LESS, RT_LESS);
14272 /* Parse the template-parameter-list. */
14273 cp_parser_template_parameter_list (parser);
14274 /* Look for the `>'. */
14275 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14276
14277 // If template requirements are present, parse them.
14278 if (flag_concepts)
14279 {
14280 tree reqs = get_shorthand_constraints (current_template_parms);
14281 if (tree r = cp_parser_requires_clause_opt (parser))
14282 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
14283 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14284 }
14285
14286 /* Look for the `class' or 'typename' keywords. */
14287 cp_parser_type_parameter_key (parser);
14288 /* If the next token is an ellipsis, we have a template
14289 argument pack. */
14290 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14291 {
14292 /* Consume the `...' token. */
14293 cp_lexer_consume_token (parser->lexer);
14294 maybe_warn_variadic_templates ();
14295
14296 *is_parameter_pack = true;
14297 }
14298 /* If the next token is an `=', then there is a
14299 default-argument. If the next token is a `>', we are at
14300 the end of the parameter-list. If the next token is a `,',
14301 then we are at the end of this parameter. */
14302 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14303 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14304 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14305 {
14306 identifier = cp_parser_identifier (parser);
14307 /* Treat invalid names as if the parameter were nameless. */
14308 if (identifier == error_mark_node)
14309 identifier = NULL_TREE;
14310 }
14311 else
14312 identifier = NULL_TREE;
14313
14314 /* Create the template parameter. */
14315 parameter = finish_template_template_parm (class_type_node,
14316 identifier);
14317
14318 /* If the next token is an `=', then there is a
14319 default-argument. */
14320 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14321 {
14322 default_argument
14323 = cp_parser_default_template_template_argument (parser);
14324
14325 /* Template parameter packs cannot have default
14326 arguments. */
14327 if (*is_parameter_pack)
14328 {
14329 if (identifier)
14330 error_at (token->location,
14331 "template parameter pack %qD cannot "
14332 "have a default argument",
14333 identifier);
14334 else
14335 error_at (token->location, "template parameter packs cannot "
14336 "have default arguments");
14337 default_argument = NULL_TREE;
14338 }
14339 }
14340 else
14341 default_argument = NULL_TREE;
14342
14343 /* Create the combined representation of the parameter and the
14344 default argument. */
14345 parameter = build_tree_list (default_argument, parameter);
14346 }
14347 break;
14348
14349 default:
14350 gcc_unreachable ();
14351 break;
14352 }
14353
14354 return parameter;
14355 }
14356
14357 /* Parse a template-id.
14358
14359 template-id:
14360 template-name < template-argument-list [opt] >
14361
14362 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14363 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14364 returned. Otherwise, if the template-name names a function, or set
14365 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14366 names a class, returns a TYPE_DECL for the specialization.
14367
14368 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14369 uninstantiated templates. */
14370
14371 static tree
14372 cp_parser_template_id (cp_parser *parser,
14373 bool template_keyword_p,
14374 bool check_dependency_p,
14375 enum tag_types tag_type,
14376 bool is_declaration)
14377 {
14378 int i;
14379 tree templ;
14380 tree arguments;
14381 tree template_id;
14382 cp_token_position start_of_id = 0;
14383 deferred_access_check *chk;
14384 vec<deferred_access_check, va_gc> *access_check;
14385 cp_token *next_token = NULL, *next_token_2 = NULL;
14386 bool is_identifier;
14387
14388 /* If the next token corresponds to a template-id, there is no need
14389 to reparse it. */
14390 next_token = cp_lexer_peek_token (parser->lexer);
14391 if (next_token->type == CPP_TEMPLATE_ID)
14392 {
14393 struct tree_check *check_value;
14394
14395 /* Get the stored value. */
14396 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
14397 /* Perform any access checks that were deferred. */
14398 access_check = check_value->checks;
14399 if (access_check)
14400 {
14401 FOR_EACH_VEC_ELT (*access_check, i, chk)
14402 perform_or_defer_access_check (chk->binfo,
14403 chk->decl,
14404 chk->diag_decl,
14405 tf_warning_or_error);
14406 }
14407 /* Return the stored value. */
14408 return check_value->value;
14409 }
14410
14411 /* Avoid performing name lookup if there is no possibility of
14412 finding a template-id. */
14413 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
14414 || (next_token->type == CPP_NAME
14415 && !cp_parser_nth_token_starts_template_argument_list_p
14416 (parser, 2)))
14417 {
14418 cp_parser_error (parser, "expected template-id");
14419 return error_mark_node;
14420 }
14421
14422 /* Remember where the template-id starts. */
14423 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
14424 start_of_id = cp_lexer_token_position (parser->lexer, false);
14425
14426 push_deferring_access_checks (dk_deferred);
14427
14428 /* Parse the template-name. */
14429 is_identifier = false;
14430 templ = cp_parser_template_name (parser, template_keyword_p,
14431 check_dependency_p,
14432 is_declaration,
14433 tag_type,
14434 &is_identifier);
14435 if (templ == error_mark_node || is_identifier)
14436 {
14437 pop_deferring_access_checks ();
14438 return templ;
14439 }
14440
14441 /* If we find the sequence `[:' after a template-name, it's probably
14442 a digraph-typo for `< ::'. Substitute the tokens and check if we can
14443 parse correctly the argument list. */
14444 next_token = cp_lexer_peek_token (parser->lexer);
14445 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14446 if (next_token->type == CPP_OPEN_SQUARE
14447 && next_token->flags & DIGRAPH
14448 && next_token_2->type == CPP_COLON
14449 && !(next_token_2->flags & PREV_WHITE))
14450 {
14451 cp_parser_parse_tentatively (parser);
14452 /* Change `:' into `::'. */
14453 next_token_2->type = CPP_SCOPE;
14454 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14455 CPP_LESS. */
14456 cp_lexer_consume_token (parser->lexer);
14457
14458 /* Parse the arguments. */
14459 arguments = cp_parser_enclosed_template_argument_list (parser);
14460 if (!cp_parser_parse_definitely (parser))
14461 {
14462 /* If we couldn't parse an argument list, then we revert our changes
14463 and return simply an error. Maybe this is not a template-id
14464 after all. */
14465 next_token_2->type = CPP_COLON;
14466 cp_parser_error (parser, "expected %<<%>");
14467 pop_deferring_access_checks ();
14468 return error_mark_node;
14469 }
14470 /* Otherwise, emit an error about the invalid digraph, but continue
14471 parsing because we got our argument list. */
14472 if (permerror (next_token->location,
14473 "%<<::%> cannot begin a template-argument list"))
14474 {
14475 static bool hint = false;
14476 inform (next_token->location,
14477 "%<<:%> is an alternate spelling for %<[%>."
14478 " Insert whitespace between %<<%> and %<::%>");
14479 if (!hint && !flag_permissive)
14480 {
14481 inform (next_token->location, "(if you use %<-fpermissive%> "
14482 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14483 "accept your code)");
14484 hint = true;
14485 }
14486 }
14487 }
14488 else
14489 {
14490 /* Look for the `<' that starts the template-argument-list. */
14491 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
14492 {
14493 pop_deferring_access_checks ();
14494 return error_mark_node;
14495 }
14496 /* Parse the arguments. */
14497 arguments = cp_parser_enclosed_template_argument_list (parser);
14498 }
14499
14500 /* Build a representation of the specialization. */
14501 if (identifier_p (templ))
14502 template_id = build_min_nt_loc (next_token->location,
14503 TEMPLATE_ID_EXPR,
14504 templ, arguments);
14505 else if (DECL_TYPE_TEMPLATE_P (templ)
14506 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
14507 {
14508 bool entering_scope;
14509 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14510 template (rather than some instantiation thereof) only if
14511 is not nested within some other construct. For example, in
14512 "template <typename T> void f(T) { A<T>::", A<T> is just an
14513 instantiation of A. */
14514 entering_scope = (template_parm_scope_p ()
14515 && cp_lexer_next_token_is (parser->lexer,
14516 CPP_SCOPE));
14517 template_id
14518 = finish_template_type (templ, arguments, entering_scope);
14519 }
14520 /* A template-like identifier may be a partial concept id. */
14521 else if (flag_concepts
14522 && (template_id = (cp_parser_maybe_partial_concept_id
14523 (parser, templ, arguments))))
14524 return template_id;
14525 else if (variable_template_p (templ))
14526 {
14527 template_id = lookup_template_variable (templ, arguments);
14528 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14529 SET_EXPR_LOCATION (template_id, next_token->location);
14530 }
14531 else
14532 {
14533 /* If it's not a class-template or a template-template, it should be
14534 a function-template. */
14535 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
14536 || TREE_CODE (templ) == OVERLOAD
14537 || BASELINK_P (templ)));
14538
14539 template_id = lookup_template_function (templ, arguments);
14540 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14541 SET_EXPR_LOCATION (template_id, next_token->location);
14542 }
14543
14544 /* If parsing tentatively, replace the sequence of tokens that makes
14545 up the template-id with a CPP_TEMPLATE_ID token. That way,
14546 should we re-parse the token stream, we will not have to repeat
14547 the effort required to do the parse, nor will we issue duplicate
14548 error messages about problems during instantiation of the
14549 template. */
14550 if (start_of_id
14551 /* Don't do this if we had a parse error in a declarator; re-parsing
14552 might succeed if a name changes meaning (60361). */
14553 && !(cp_parser_error_occurred (parser)
14554 && cp_parser_parsing_tentatively (parser)
14555 && parser->in_declarator_p))
14556 {
14557 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
14558
14559 /* Reset the contents of the START_OF_ID token. */
14560 token->type = CPP_TEMPLATE_ID;
14561 /* Retrieve any deferred checks. Do not pop this access checks yet
14562 so the memory will not be reclaimed during token replacing below. */
14563 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14564 token->u.tree_check_value->value = template_id;
14565 token->u.tree_check_value->checks = get_deferred_access_checks ();
14566 token->keyword = RID_MAX;
14567
14568 /* Purge all subsequent tokens. */
14569 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
14570
14571 /* ??? Can we actually assume that, if template_id ==
14572 error_mark_node, we will have issued a diagnostic to the
14573 user, as opposed to simply marking the tentative parse as
14574 failed? */
14575 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
14576 error_at (token->location, "parse error in template argument list");
14577 }
14578
14579 pop_to_parent_deferring_access_checks ();
14580 return template_id;
14581 }
14582
14583 /* Parse a template-name.
14584
14585 template-name:
14586 identifier
14587
14588 The standard should actually say:
14589
14590 template-name:
14591 identifier
14592 operator-function-id
14593
14594 A defect report has been filed about this issue.
14595
14596 A conversion-function-id cannot be a template name because they cannot
14597 be part of a template-id. In fact, looking at this code:
14598
14599 a.operator K<int>()
14600
14601 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
14602 It is impossible to call a templated conversion-function-id with an
14603 explicit argument list, since the only allowed template parameter is
14604 the type to which it is converting.
14605
14606 If TEMPLATE_KEYWORD_P is true, then we have just seen the
14607 `template' keyword, in a construction like:
14608
14609 T::template f<3>()
14610
14611 In that case `f' is taken to be a template-name, even though there
14612 is no way of knowing for sure.
14613
14614 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
14615 name refers to a set of overloaded functions, at least one of which
14616 is a template, or an IDENTIFIER_NODE with the name of the template,
14617 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
14618 names are looked up inside uninstantiated templates. */
14619
14620 static tree
14621 cp_parser_template_name (cp_parser* parser,
14622 bool template_keyword_p,
14623 bool check_dependency_p,
14624 bool is_declaration,
14625 enum tag_types tag_type,
14626 bool *is_identifier)
14627 {
14628 tree identifier;
14629 tree decl;
14630 tree fns;
14631 cp_token *token = cp_lexer_peek_token (parser->lexer);
14632
14633 /* If the next token is `operator', then we have either an
14634 operator-function-id or a conversion-function-id. */
14635 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
14636 {
14637 /* We don't know whether we're looking at an
14638 operator-function-id or a conversion-function-id. */
14639 cp_parser_parse_tentatively (parser);
14640 /* Try an operator-function-id. */
14641 identifier = cp_parser_operator_function_id (parser);
14642 /* If that didn't work, try a conversion-function-id. */
14643 if (!cp_parser_parse_definitely (parser))
14644 {
14645 cp_parser_error (parser, "expected template-name");
14646 return error_mark_node;
14647 }
14648 }
14649 /* Look for the identifier. */
14650 else
14651 identifier = cp_parser_identifier (parser);
14652
14653 /* If we didn't find an identifier, we don't have a template-id. */
14654 if (identifier == error_mark_node)
14655 return error_mark_node;
14656
14657 /* If the name immediately followed the `template' keyword, then it
14658 is a template-name. However, if the next token is not `<', then
14659 we do not treat it as a template-name, since it is not being used
14660 as part of a template-id. This enables us to handle constructs
14661 like:
14662
14663 template <typename T> struct S { S(); };
14664 template <typename T> S<T>::S();
14665
14666 correctly. We would treat `S' as a template -- if it were `S<T>'
14667 -- but we do not if there is no `<'. */
14668
14669 if (processing_template_decl
14670 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
14671 {
14672 /* In a declaration, in a dependent context, we pretend that the
14673 "template" keyword was present in order to improve error
14674 recovery. For example, given:
14675
14676 template <typename T> void f(T::X<int>);
14677
14678 we want to treat "X<int>" as a template-id. */
14679 if (is_declaration
14680 && !template_keyword_p
14681 && parser->scope && TYPE_P (parser->scope)
14682 && check_dependency_p
14683 && dependent_scope_p (parser->scope)
14684 /* Do not do this for dtors (or ctors), since they never
14685 need the template keyword before their name. */
14686 && !constructor_name_p (identifier, parser->scope))
14687 {
14688 cp_token_position start = 0;
14689
14690 /* Explain what went wrong. */
14691 error_at (token->location, "non-template %qD used as template",
14692 identifier);
14693 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
14694 parser->scope, identifier);
14695 /* If parsing tentatively, find the location of the "<" token. */
14696 if (cp_parser_simulate_error (parser))
14697 start = cp_lexer_token_position (parser->lexer, true);
14698 /* Parse the template arguments so that we can issue error
14699 messages about them. */
14700 cp_lexer_consume_token (parser->lexer);
14701 cp_parser_enclosed_template_argument_list (parser);
14702 /* Skip tokens until we find a good place from which to
14703 continue parsing. */
14704 cp_parser_skip_to_closing_parenthesis (parser,
14705 /*recovering=*/true,
14706 /*or_comma=*/true,
14707 /*consume_paren=*/false);
14708 /* If parsing tentatively, permanently remove the
14709 template argument list. That will prevent duplicate
14710 error messages from being issued about the missing
14711 "template" keyword. */
14712 if (start)
14713 cp_lexer_purge_tokens_after (parser->lexer, start);
14714 if (is_identifier)
14715 *is_identifier = true;
14716 return identifier;
14717 }
14718
14719 /* If the "template" keyword is present, then there is generally
14720 no point in doing name-lookup, so we just return IDENTIFIER.
14721 But, if the qualifying scope is non-dependent then we can
14722 (and must) do name-lookup normally. */
14723 if (template_keyword_p
14724 && (!parser->scope
14725 || (TYPE_P (parser->scope)
14726 && dependent_type_p (parser->scope))))
14727 return identifier;
14728 }
14729
14730 /* Look up the name. */
14731 decl = cp_parser_lookup_name (parser, identifier,
14732 tag_type,
14733 /*is_template=*/true,
14734 /*is_namespace=*/false,
14735 check_dependency_p,
14736 /*ambiguous_decls=*/NULL,
14737 token->location);
14738
14739 decl = strip_using_decl (decl);
14740
14741 /* If DECL is a template, then the name was a template-name. */
14742 if (TREE_CODE (decl) == TEMPLATE_DECL)
14743 {
14744 if (TREE_DEPRECATED (decl)
14745 && deprecated_state != DEPRECATED_SUPPRESS)
14746 warn_deprecated_use (decl, NULL_TREE);
14747 }
14748 else
14749 {
14750 tree fn = NULL_TREE;
14751
14752 /* The standard does not explicitly indicate whether a name that
14753 names a set of overloaded declarations, some of which are
14754 templates, is a template-name. However, such a name should
14755 be a template-name; otherwise, there is no way to form a
14756 template-id for the overloaded templates. */
14757 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14758 if (TREE_CODE (fns) == OVERLOAD)
14759 for (fn = fns; fn; fn = OVL_NEXT (fn))
14760 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14761 break;
14762
14763 if (!fn)
14764 {
14765 /* The name does not name a template. */
14766 cp_parser_error (parser, "expected template-name");
14767 return error_mark_node;
14768 }
14769 }
14770
14771 /* If DECL is dependent, and refers to a function, then just return
14772 its name; we will look it up again during template instantiation. */
14773 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14774 {
14775 tree scope = ovl_scope (decl);
14776 if (TYPE_P (scope) && dependent_type_p (scope))
14777 return identifier;
14778 }
14779
14780 return decl;
14781 }
14782
14783 /* Parse a template-argument-list.
14784
14785 template-argument-list:
14786 template-argument ... [opt]
14787 template-argument-list , template-argument ... [opt]
14788
14789 Returns a TREE_VEC containing the arguments. */
14790
14791 static tree
14792 cp_parser_template_argument_list (cp_parser* parser)
14793 {
14794 tree fixed_args[10];
14795 unsigned n_args = 0;
14796 unsigned alloced = 10;
14797 tree *arg_ary = fixed_args;
14798 tree vec;
14799 bool saved_in_template_argument_list_p;
14800 bool saved_ice_p;
14801 bool saved_non_ice_p;
14802
14803 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14804 parser->in_template_argument_list_p = true;
14805 /* Even if the template-id appears in an integral
14806 constant-expression, the contents of the argument list do
14807 not. */
14808 saved_ice_p = parser->integral_constant_expression_p;
14809 parser->integral_constant_expression_p = false;
14810 saved_non_ice_p = parser->non_integral_constant_expression_p;
14811 parser->non_integral_constant_expression_p = false;
14812
14813 /* Parse the arguments. */
14814 do
14815 {
14816 tree argument;
14817
14818 if (n_args)
14819 /* Consume the comma. */
14820 cp_lexer_consume_token (parser->lexer);
14821
14822 /* Parse the template-argument. */
14823 argument = cp_parser_template_argument (parser);
14824
14825 /* If the next token is an ellipsis, we're expanding a template
14826 argument pack. */
14827 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14828 {
14829 if (argument == error_mark_node)
14830 {
14831 cp_token *token = cp_lexer_peek_token (parser->lexer);
14832 error_at (token->location,
14833 "expected parameter pack before %<...%>");
14834 }
14835 /* Consume the `...' token. */
14836 cp_lexer_consume_token (parser->lexer);
14837
14838 /* Make the argument into a TYPE_PACK_EXPANSION or
14839 EXPR_PACK_EXPANSION. */
14840 argument = make_pack_expansion (argument);
14841 }
14842
14843 if (n_args == alloced)
14844 {
14845 alloced *= 2;
14846
14847 if (arg_ary == fixed_args)
14848 {
14849 arg_ary = XNEWVEC (tree, alloced);
14850 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14851 }
14852 else
14853 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14854 }
14855 arg_ary[n_args++] = argument;
14856 }
14857 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14858
14859 vec = make_tree_vec (n_args);
14860
14861 while (n_args--)
14862 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14863
14864 if (arg_ary != fixed_args)
14865 free (arg_ary);
14866 parser->non_integral_constant_expression_p = saved_non_ice_p;
14867 parser->integral_constant_expression_p = saved_ice_p;
14868 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14869 if (CHECKING_P)
14870 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14871 return vec;
14872 }
14873
14874 /* Parse a template-argument.
14875
14876 template-argument:
14877 assignment-expression
14878 type-id
14879 id-expression
14880
14881 The representation is that of an assignment-expression, type-id, or
14882 id-expression -- except that the qualified id-expression is
14883 evaluated, so that the value returned is either a DECL or an
14884 OVERLOAD.
14885
14886 Although the standard says "assignment-expression", it forbids
14887 throw-expressions or assignments in the template argument.
14888 Therefore, we use "conditional-expression" instead. */
14889
14890 static tree
14891 cp_parser_template_argument (cp_parser* parser)
14892 {
14893 tree argument;
14894 bool template_p;
14895 bool address_p;
14896 bool maybe_type_id = false;
14897 cp_token *token = NULL, *argument_start_token = NULL;
14898 location_t loc = 0;
14899 cp_id_kind idk;
14900
14901 /* There's really no way to know what we're looking at, so we just
14902 try each alternative in order.
14903
14904 [temp.arg]
14905
14906 In a template-argument, an ambiguity between a type-id and an
14907 expression is resolved to a type-id, regardless of the form of
14908 the corresponding template-parameter.
14909
14910 Therefore, we try a type-id first. */
14911 cp_parser_parse_tentatively (parser);
14912 argument = cp_parser_template_type_arg (parser);
14913 /* If there was no error parsing the type-id but the next token is a
14914 '>>', our behavior depends on which dialect of C++ we're
14915 parsing. In C++98, we probably found a typo for '> >'. But there
14916 are type-id which are also valid expressions. For instance:
14917
14918 struct X { int operator >> (int); };
14919 template <int V> struct Foo {};
14920 Foo<X () >> 5> r;
14921
14922 Here 'X()' is a valid type-id of a function type, but the user just
14923 wanted to write the expression "X() >> 5". Thus, we remember that we
14924 found a valid type-id, but we still try to parse the argument as an
14925 expression to see what happens.
14926
14927 In C++0x, the '>>' will be considered two separate '>'
14928 tokens. */
14929 if (!cp_parser_error_occurred (parser)
14930 && cxx_dialect == cxx98
14931 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14932 {
14933 maybe_type_id = true;
14934 cp_parser_abort_tentative_parse (parser);
14935 }
14936 else
14937 {
14938 /* If the next token isn't a `,' or a `>', then this argument wasn't
14939 really finished. This means that the argument is not a valid
14940 type-id. */
14941 if (!cp_parser_next_token_ends_template_argument_p (parser))
14942 cp_parser_error (parser, "expected template-argument");
14943 /* If that worked, we're done. */
14944 if (cp_parser_parse_definitely (parser))
14945 return argument;
14946 }
14947 /* We're still not sure what the argument will be. */
14948 cp_parser_parse_tentatively (parser);
14949 /* Try a template. */
14950 argument_start_token = cp_lexer_peek_token (parser->lexer);
14951 argument = cp_parser_id_expression (parser,
14952 /*template_keyword_p=*/false,
14953 /*check_dependency_p=*/true,
14954 &template_p,
14955 /*declarator_p=*/false,
14956 /*optional_p=*/false);
14957 /* If the next token isn't a `,' or a `>', then this argument wasn't
14958 really finished. */
14959 if (!cp_parser_next_token_ends_template_argument_p (parser))
14960 cp_parser_error (parser, "expected template-argument");
14961 if (!cp_parser_error_occurred (parser))
14962 {
14963 /* Figure out what is being referred to. If the id-expression
14964 was for a class template specialization, then we will have a
14965 TYPE_DECL at this point. There is no need to do name lookup
14966 at this point in that case. */
14967 if (TREE_CODE (argument) != TYPE_DECL)
14968 argument = cp_parser_lookup_name (parser, argument,
14969 none_type,
14970 /*is_template=*/template_p,
14971 /*is_namespace=*/false,
14972 /*check_dependency=*/true,
14973 /*ambiguous_decls=*/NULL,
14974 argument_start_token->location);
14975 /* Handle a constrained-type-specifier for a non-type template
14976 parameter. */
14977 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
14978 argument = decl;
14979 else if (TREE_CODE (argument) != TEMPLATE_DECL
14980 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14981 cp_parser_error (parser, "expected template-name");
14982 }
14983 if (cp_parser_parse_definitely (parser))
14984 {
14985 if (TREE_DEPRECATED (argument))
14986 warn_deprecated_use (argument, NULL_TREE);
14987 return argument;
14988 }
14989 /* It must be a non-type argument. In C++17 any constant-expression is
14990 allowed. */
14991 if (cxx_dialect > cxx14)
14992 goto general_expr;
14993
14994 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
14995
14996 -- an integral constant-expression of integral or enumeration
14997 type; or
14998
14999 -- the name of a non-type template-parameter; or
15000
15001 -- the name of an object or function with external linkage...
15002
15003 -- the address of an object or function with external linkage...
15004
15005 -- a pointer to member... */
15006 /* Look for a non-type template parameter. */
15007 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15008 {
15009 cp_parser_parse_tentatively (parser);
15010 argument = cp_parser_primary_expression (parser,
15011 /*address_p=*/false,
15012 /*cast_p=*/false,
15013 /*template_arg_p=*/true,
15014 &idk);
15015 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
15016 || !cp_parser_next_token_ends_template_argument_p (parser))
15017 cp_parser_simulate_error (parser);
15018 if (cp_parser_parse_definitely (parser))
15019 return argument;
15020 }
15021
15022 /* If the next token is "&", the argument must be the address of an
15023 object or function with external linkage. */
15024 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
15025 if (address_p)
15026 {
15027 loc = cp_lexer_peek_token (parser->lexer)->location;
15028 cp_lexer_consume_token (parser->lexer);
15029 }
15030 /* See if we might have an id-expression. */
15031 token = cp_lexer_peek_token (parser->lexer);
15032 if (token->type == CPP_NAME
15033 || token->keyword == RID_OPERATOR
15034 || token->type == CPP_SCOPE
15035 || token->type == CPP_TEMPLATE_ID
15036 || token->type == CPP_NESTED_NAME_SPECIFIER)
15037 {
15038 cp_parser_parse_tentatively (parser);
15039 argument = cp_parser_primary_expression (parser,
15040 address_p,
15041 /*cast_p=*/false,
15042 /*template_arg_p=*/true,
15043 &idk);
15044 if (cp_parser_error_occurred (parser)
15045 || !cp_parser_next_token_ends_template_argument_p (parser))
15046 cp_parser_abort_tentative_parse (parser);
15047 else
15048 {
15049 tree probe;
15050
15051 if (INDIRECT_REF_P (argument))
15052 {
15053 /* Strip the dereference temporarily. */
15054 gcc_assert (REFERENCE_REF_P (argument));
15055 argument = TREE_OPERAND (argument, 0);
15056 }
15057
15058 /* If we're in a template, we represent a qualified-id referring
15059 to a static data member as a SCOPE_REF even if the scope isn't
15060 dependent so that we can check access control later. */
15061 probe = argument;
15062 if (TREE_CODE (probe) == SCOPE_REF)
15063 probe = TREE_OPERAND (probe, 1);
15064 if (VAR_P (probe))
15065 {
15066 /* A variable without external linkage might still be a
15067 valid constant-expression, so no error is issued here
15068 if the external-linkage check fails. */
15069 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
15070 cp_parser_simulate_error (parser);
15071 }
15072 else if (is_overloaded_fn (argument))
15073 /* All overloaded functions are allowed; if the external
15074 linkage test does not pass, an error will be issued
15075 later. */
15076 ;
15077 else if (address_p
15078 && (TREE_CODE (argument) == OFFSET_REF
15079 || TREE_CODE (argument) == SCOPE_REF))
15080 /* A pointer-to-member. */
15081 ;
15082 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
15083 ;
15084 else
15085 cp_parser_simulate_error (parser);
15086
15087 if (cp_parser_parse_definitely (parser))
15088 {
15089 if (address_p)
15090 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
15091 tf_warning_or_error);
15092 else
15093 argument = convert_from_reference (argument);
15094 return argument;
15095 }
15096 }
15097 }
15098 /* If the argument started with "&", there are no other valid
15099 alternatives at this point. */
15100 if (address_p)
15101 {
15102 cp_parser_error (parser, "invalid non-type template argument");
15103 return error_mark_node;
15104 }
15105
15106 general_expr:
15107 /* If the argument wasn't successfully parsed as a type-id followed
15108 by '>>', the argument can only be a constant expression now.
15109 Otherwise, we try parsing the constant-expression tentatively,
15110 because the argument could really be a type-id. */
15111 if (maybe_type_id)
15112 cp_parser_parse_tentatively (parser);
15113 argument = cp_parser_constant_expression (parser);
15114
15115 if (!maybe_type_id)
15116 return argument;
15117 if (!cp_parser_next_token_ends_template_argument_p (parser))
15118 cp_parser_error (parser, "expected template-argument");
15119 if (cp_parser_parse_definitely (parser))
15120 return argument;
15121 /* We did our best to parse the argument as a non type-id, but that
15122 was the only alternative that matched (albeit with a '>' after
15123 it). We can assume it's just a typo from the user, and a
15124 diagnostic will then be issued. */
15125 return cp_parser_template_type_arg (parser);
15126 }
15127
15128 /* Parse an explicit-instantiation.
15129
15130 explicit-instantiation:
15131 template declaration
15132
15133 Although the standard says `declaration', what it really means is:
15134
15135 explicit-instantiation:
15136 template decl-specifier-seq [opt] declarator [opt] ;
15137
15138 Things like `template int S<int>::i = 5, int S<double>::j;' are not
15139 supposed to be allowed. A defect report has been filed about this
15140 issue.
15141
15142 GNU Extension:
15143
15144 explicit-instantiation:
15145 storage-class-specifier template
15146 decl-specifier-seq [opt] declarator [opt] ;
15147 function-specifier template
15148 decl-specifier-seq [opt] declarator [opt] ; */
15149
15150 static void
15151 cp_parser_explicit_instantiation (cp_parser* parser)
15152 {
15153 int declares_class_or_enum;
15154 cp_decl_specifier_seq decl_specifiers;
15155 tree extension_specifier = NULL_TREE;
15156
15157 timevar_push (TV_TEMPLATE_INST);
15158
15159 /* Look for an (optional) storage-class-specifier or
15160 function-specifier. */
15161 if (cp_parser_allow_gnu_extensions_p (parser))
15162 {
15163 extension_specifier
15164 = cp_parser_storage_class_specifier_opt (parser);
15165 if (!extension_specifier)
15166 extension_specifier
15167 = cp_parser_function_specifier_opt (parser,
15168 /*decl_specs=*/NULL);
15169 }
15170
15171 /* Look for the `template' keyword. */
15172 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15173 /* Let the front end know that we are processing an explicit
15174 instantiation. */
15175 begin_explicit_instantiation ();
15176 /* [temp.explicit] says that we are supposed to ignore access
15177 control while processing explicit instantiation directives. */
15178 push_deferring_access_checks (dk_no_check);
15179 /* Parse a decl-specifier-seq. */
15180 cp_parser_decl_specifier_seq (parser,
15181 CP_PARSER_FLAGS_OPTIONAL,
15182 &decl_specifiers,
15183 &declares_class_or_enum);
15184 /* If there was exactly one decl-specifier, and it declared a class,
15185 and there's no declarator, then we have an explicit type
15186 instantiation. */
15187 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
15188 {
15189 tree type;
15190
15191 type = check_tag_decl (&decl_specifiers,
15192 /*explicit_type_instantiation_p=*/true);
15193 /* Turn access control back on for names used during
15194 template instantiation. */
15195 pop_deferring_access_checks ();
15196 if (type)
15197 do_type_instantiation (type, extension_specifier,
15198 /*complain=*/tf_error);
15199 }
15200 else
15201 {
15202 cp_declarator *declarator;
15203 tree decl;
15204
15205 /* Parse the declarator. */
15206 declarator
15207 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15208 /*ctor_dtor_or_conv_p=*/NULL,
15209 /*parenthesized_p=*/NULL,
15210 /*member_p=*/false,
15211 /*friend_p=*/false);
15212 if (declares_class_or_enum & 2)
15213 cp_parser_check_for_definition_in_return_type (declarator,
15214 decl_specifiers.type,
15215 decl_specifiers.locations[ds_type_spec]);
15216 if (declarator != cp_error_declarator)
15217 {
15218 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
15219 permerror (decl_specifiers.locations[ds_inline],
15220 "explicit instantiation shall not use"
15221 " %<inline%> specifier");
15222 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
15223 permerror (decl_specifiers.locations[ds_constexpr],
15224 "explicit instantiation shall not use"
15225 " %<constexpr%> specifier");
15226
15227 decl = grokdeclarator (declarator, &decl_specifiers,
15228 NORMAL, 0, &decl_specifiers.attributes);
15229 /* Turn access control back on for names used during
15230 template instantiation. */
15231 pop_deferring_access_checks ();
15232 /* Do the explicit instantiation. */
15233 do_decl_instantiation (decl, extension_specifier);
15234 }
15235 else
15236 {
15237 pop_deferring_access_checks ();
15238 /* Skip the body of the explicit instantiation. */
15239 cp_parser_skip_to_end_of_statement (parser);
15240 }
15241 }
15242 /* We're done with the instantiation. */
15243 end_explicit_instantiation ();
15244
15245 cp_parser_consume_semicolon_at_end_of_statement (parser);
15246
15247 timevar_pop (TV_TEMPLATE_INST);
15248 }
15249
15250 /* Parse an explicit-specialization.
15251
15252 explicit-specialization:
15253 template < > declaration
15254
15255 Although the standard says `declaration', what it really means is:
15256
15257 explicit-specialization:
15258 template <> decl-specifier [opt] init-declarator [opt] ;
15259 template <> function-definition
15260 template <> explicit-specialization
15261 template <> template-declaration */
15262
15263 static void
15264 cp_parser_explicit_specialization (cp_parser* parser)
15265 {
15266 bool need_lang_pop;
15267 cp_token *token = cp_lexer_peek_token (parser->lexer);
15268
15269 /* Look for the `template' keyword. */
15270 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15271 /* Look for the `<'. */
15272 cp_parser_require (parser, CPP_LESS, RT_LESS);
15273 /* Look for the `>'. */
15274 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15275 /* We have processed another parameter list. */
15276 ++parser->num_template_parameter_lists;
15277 /* [temp]
15278
15279 A template ... explicit specialization ... shall not have C
15280 linkage. */
15281 if (current_lang_name == lang_name_c)
15282 {
15283 error_at (token->location, "template specialization with C linkage");
15284 /* Give it C++ linkage to avoid confusing other parts of the
15285 front end. */
15286 push_lang_context (lang_name_cplusplus);
15287 need_lang_pop = true;
15288 }
15289 else
15290 need_lang_pop = false;
15291 /* Let the front end know that we are beginning a specialization. */
15292 if (!begin_specialization ())
15293 {
15294 end_specialization ();
15295 return;
15296 }
15297
15298 /* If the next keyword is `template', we need to figure out whether
15299 or not we're looking a template-declaration. */
15300 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15301 {
15302 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15303 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15304 cp_parser_template_declaration_after_export (parser,
15305 /*member_p=*/false);
15306 else
15307 cp_parser_explicit_specialization (parser);
15308 }
15309 else
15310 /* Parse the dependent declaration. */
15311 cp_parser_single_declaration (parser,
15312 /*checks=*/NULL,
15313 /*member_p=*/false,
15314 /*explicit_specialization_p=*/true,
15315 /*friend_p=*/NULL);
15316 /* We're done with the specialization. */
15317 end_specialization ();
15318 /* For the erroneous case of a template with C linkage, we pushed an
15319 implicit C++ linkage scope; exit that scope now. */
15320 if (need_lang_pop)
15321 pop_lang_context ();
15322 /* We're done with this parameter list. */
15323 --parser->num_template_parameter_lists;
15324 }
15325
15326 /* Parse a type-specifier.
15327
15328 type-specifier:
15329 simple-type-specifier
15330 class-specifier
15331 enum-specifier
15332 elaborated-type-specifier
15333 cv-qualifier
15334
15335 GNU Extension:
15336
15337 type-specifier:
15338 __complex__
15339
15340 Returns a representation of the type-specifier. For a
15341 class-specifier, enum-specifier, or elaborated-type-specifier, a
15342 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15343
15344 The parser flags FLAGS is used to control type-specifier parsing.
15345
15346 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15347 in a decl-specifier-seq.
15348
15349 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15350 class-specifier, enum-specifier, or elaborated-type-specifier, then
15351 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15352 if a type is declared; 2 if it is defined. Otherwise, it is set to
15353 zero.
15354
15355 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15356 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15357 is set to FALSE. */
15358
15359 static tree
15360 cp_parser_type_specifier (cp_parser* parser,
15361 cp_parser_flags flags,
15362 cp_decl_specifier_seq *decl_specs,
15363 bool is_declaration,
15364 int* declares_class_or_enum,
15365 bool* is_cv_qualifier)
15366 {
15367 tree type_spec = NULL_TREE;
15368 cp_token *token;
15369 enum rid keyword;
15370 cp_decl_spec ds = ds_last;
15371
15372 /* Assume this type-specifier does not declare a new type. */
15373 if (declares_class_or_enum)
15374 *declares_class_or_enum = 0;
15375 /* And that it does not specify a cv-qualifier. */
15376 if (is_cv_qualifier)
15377 *is_cv_qualifier = false;
15378 /* Peek at the next token. */
15379 token = cp_lexer_peek_token (parser->lexer);
15380
15381 /* If we're looking at a keyword, we can use that to guide the
15382 production we choose. */
15383 keyword = token->keyword;
15384 switch (keyword)
15385 {
15386 case RID_ENUM:
15387 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15388 goto elaborated_type_specifier;
15389
15390 /* Look for the enum-specifier. */
15391 type_spec = cp_parser_enum_specifier (parser);
15392 /* If that worked, we're done. */
15393 if (type_spec)
15394 {
15395 if (declares_class_or_enum)
15396 *declares_class_or_enum = 2;
15397 if (decl_specs)
15398 cp_parser_set_decl_spec_type (decl_specs,
15399 type_spec,
15400 token,
15401 /*type_definition_p=*/true);
15402 return type_spec;
15403 }
15404 else
15405 goto elaborated_type_specifier;
15406
15407 /* Any of these indicate either a class-specifier, or an
15408 elaborated-type-specifier. */
15409 case RID_CLASS:
15410 case RID_STRUCT:
15411 case RID_UNION:
15412 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15413 goto elaborated_type_specifier;
15414
15415 /* Parse tentatively so that we can back up if we don't find a
15416 class-specifier. */
15417 cp_parser_parse_tentatively (parser);
15418 /* Look for the class-specifier. */
15419 type_spec = cp_parser_class_specifier (parser);
15420 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
15421 /* If that worked, we're done. */
15422 if (cp_parser_parse_definitely (parser))
15423 {
15424 if (declares_class_or_enum)
15425 *declares_class_or_enum = 2;
15426 if (decl_specs)
15427 cp_parser_set_decl_spec_type (decl_specs,
15428 type_spec,
15429 token,
15430 /*type_definition_p=*/true);
15431 return type_spec;
15432 }
15433
15434 /* Fall through. */
15435 elaborated_type_specifier:
15436 /* We're declaring (not defining) a class or enum. */
15437 if (declares_class_or_enum)
15438 *declares_class_or_enum = 1;
15439
15440 /* Fall through. */
15441 case RID_TYPENAME:
15442 /* Look for an elaborated-type-specifier. */
15443 type_spec
15444 = (cp_parser_elaborated_type_specifier
15445 (parser,
15446 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
15447 is_declaration));
15448 if (decl_specs)
15449 cp_parser_set_decl_spec_type (decl_specs,
15450 type_spec,
15451 token,
15452 /*type_definition_p=*/false);
15453 return type_spec;
15454
15455 case RID_CONST:
15456 ds = ds_const;
15457 if (is_cv_qualifier)
15458 *is_cv_qualifier = true;
15459 break;
15460
15461 case RID_VOLATILE:
15462 ds = ds_volatile;
15463 if (is_cv_qualifier)
15464 *is_cv_qualifier = true;
15465 break;
15466
15467 case RID_RESTRICT:
15468 ds = ds_restrict;
15469 if (is_cv_qualifier)
15470 *is_cv_qualifier = true;
15471 break;
15472
15473 case RID_COMPLEX:
15474 /* The `__complex__' keyword is a GNU extension. */
15475 ds = ds_complex;
15476 break;
15477
15478 default:
15479 break;
15480 }
15481
15482 /* Handle simple keywords. */
15483 if (ds != ds_last)
15484 {
15485 if (decl_specs)
15486 {
15487 set_and_check_decl_spec_loc (decl_specs, ds, token);
15488 decl_specs->any_specifiers_p = true;
15489 }
15490 return cp_lexer_consume_token (parser->lexer)->u.value;
15491 }
15492
15493 /* If we do not already have a type-specifier, assume we are looking
15494 at a simple-type-specifier. */
15495 type_spec = cp_parser_simple_type_specifier (parser,
15496 decl_specs,
15497 flags);
15498
15499 /* If we didn't find a type-specifier, and a type-specifier was not
15500 optional in this context, issue an error message. */
15501 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15502 {
15503 cp_parser_error (parser, "expected type specifier");
15504 return error_mark_node;
15505 }
15506
15507 return type_spec;
15508 }
15509
15510 /* Parse a simple-type-specifier.
15511
15512 simple-type-specifier:
15513 :: [opt] nested-name-specifier [opt] type-name
15514 :: [opt] nested-name-specifier template template-id
15515 char
15516 wchar_t
15517 bool
15518 short
15519 int
15520 long
15521 signed
15522 unsigned
15523 float
15524 double
15525 void
15526
15527 C++0x Extension:
15528
15529 simple-type-specifier:
15530 auto
15531 decltype ( expression )
15532 char16_t
15533 char32_t
15534 __underlying_type ( type-id )
15535
15536 GNU Extension:
15537
15538 simple-type-specifier:
15539 __int128
15540 __typeof__ unary-expression
15541 __typeof__ ( type-id )
15542 __typeof__ ( type-id ) { initializer-list , [opt] }
15543
15544 Concepts Extension:
15545
15546 simple-type-specifier:
15547 constrained-type-specifier
15548
15549 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
15550 appropriately updated. */
15551
15552 static tree
15553 cp_parser_simple_type_specifier (cp_parser* parser,
15554 cp_decl_specifier_seq *decl_specs,
15555 cp_parser_flags flags)
15556 {
15557 tree type = NULL_TREE;
15558 cp_token *token;
15559 int idx;
15560
15561 /* Peek at the next token. */
15562 token = cp_lexer_peek_token (parser->lexer);
15563
15564 /* If we're looking at a keyword, things are easy. */
15565 switch (token->keyword)
15566 {
15567 case RID_CHAR:
15568 if (decl_specs)
15569 decl_specs->explicit_char_p = true;
15570 type = char_type_node;
15571 break;
15572 case RID_CHAR16:
15573 type = char16_type_node;
15574 break;
15575 case RID_CHAR32:
15576 type = char32_type_node;
15577 break;
15578 case RID_WCHAR:
15579 type = wchar_type_node;
15580 break;
15581 case RID_BOOL:
15582 type = boolean_type_node;
15583 break;
15584 case RID_SHORT:
15585 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
15586 type = short_integer_type_node;
15587 break;
15588 case RID_INT:
15589 if (decl_specs)
15590 decl_specs->explicit_int_p = true;
15591 type = integer_type_node;
15592 break;
15593 case RID_INT_N_0:
15594 case RID_INT_N_1:
15595 case RID_INT_N_2:
15596 case RID_INT_N_3:
15597 idx = token->keyword - RID_INT_N_0;
15598 if (! int_n_enabled_p [idx])
15599 break;
15600 if (decl_specs)
15601 {
15602 decl_specs->explicit_intN_p = true;
15603 decl_specs->int_n_idx = idx;
15604 }
15605 type = int_n_trees [idx].signed_type;
15606 break;
15607 case RID_LONG:
15608 if (decl_specs)
15609 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
15610 type = long_integer_type_node;
15611 break;
15612 case RID_SIGNED:
15613 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
15614 type = integer_type_node;
15615 break;
15616 case RID_UNSIGNED:
15617 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
15618 type = unsigned_type_node;
15619 break;
15620 case RID_FLOAT:
15621 type = float_type_node;
15622 break;
15623 case RID_DOUBLE:
15624 type = double_type_node;
15625 break;
15626 case RID_VOID:
15627 type = void_type_node;
15628 break;
15629
15630 case RID_AUTO:
15631 maybe_warn_cpp0x (CPP0X_AUTO);
15632 if (parser->auto_is_implicit_function_template_parm_p)
15633 {
15634 /* The 'auto' might be the placeholder return type for a function decl
15635 with trailing return type. */
15636 bool have_trailing_return_fn_decl = false;
15637 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15638 == CPP_OPEN_PAREN)
15639 {
15640 cp_parser_parse_tentatively (parser);
15641 cp_lexer_consume_token (parser->lexer);
15642 cp_lexer_consume_token (parser->lexer);
15643 if (cp_parser_skip_to_closing_parenthesis (parser,
15644 /*recovering*/false,
15645 /*or_comma*/false,
15646 /*consume_paren*/true))
15647 have_trailing_return_fn_decl
15648 = cp_lexer_next_token_is (parser->lexer, CPP_DEREF);
15649 cp_parser_abort_tentative_parse (parser);
15650 }
15651
15652 if (have_trailing_return_fn_decl)
15653 {
15654 type = make_auto ();
15655 break;
15656 }
15657
15658 if (cxx_dialect >= cxx14)
15659 {
15660 type = synthesize_implicit_template_parm (parser, NULL_TREE);
15661 type = TREE_TYPE (type);
15662 }
15663 else
15664 type = error_mark_node;
15665
15666 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
15667 {
15668 if (cxx_dialect < cxx14)
15669 error_at (token->location,
15670 "use of %<auto%> in lambda parameter declaration "
15671 "only available with "
15672 "-std=c++14 or -std=gnu++14");
15673 }
15674 else if (cxx_dialect < cxx14)
15675 error_at (token->location,
15676 "use of %<auto%> in parameter declaration "
15677 "only available with "
15678 "-std=c++14 or -std=gnu++14");
15679 else
15680 pedwarn (token->location, OPT_Wpedantic,
15681 "ISO C++ forbids use of %<auto%> in parameter "
15682 "declaration");
15683 }
15684 else
15685 type = make_auto ();
15686 break;
15687
15688 case RID_DECLTYPE:
15689 /* Since DR 743, decltype can either be a simple-type-specifier by
15690 itself or begin a nested-name-specifier. Parsing it will replace
15691 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
15692 handling below decide what to do. */
15693 cp_parser_decltype (parser);
15694 cp_lexer_set_token_position (parser->lexer, token);
15695 break;
15696
15697 case RID_TYPEOF:
15698 /* Consume the `typeof' token. */
15699 cp_lexer_consume_token (parser->lexer);
15700 /* Parse the operand to `typeof'. */
15701 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
15702 /* If it is not already a TYPE, take its type. */
15703 if (!TYPE_P (type))
15704 type = finish_typeof (type);
15705
15706 if (decl_specs)
15707 cp_parser_set_decl_spec_type (decl_specs, type,
15708 token,
15709 /*type_definition_p=*/false);
15710
15711 return type;
15712
15713 case RID_UNDERLYING_TYPE:
15714 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
15715 if (decl_specs)
15716 cp_parser_set_decl_spec_type (decl_specs, type,
15717 token,
15718 /*type_definition_p=*/false);
15719
15720 return type;
15721
15722 case RID_BASES:
15723 case RID_DIRECT_BASES:
15724 type = cp_parser_trait_expr (parser, token->keyword);
15725 if (decl_specs)
15726 cp_parser_set_decl_spec_type (decl_specs, type,
15727 token,
15728 /*type_definition_p=*/false);
15729 return type;
15730 default:
15731 break;
15732 }
15733
15734 /* If token is an already-parsed decltype not followed by ::,
15735 it's a simple-type-specifier. */
15736 if (token->type == CPP_DECLTYPE
15737 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
15738 {
15739 type = token->u.value;
15740 if (decl_specs)
15741 {
15742 cp_parser_set_decl_spec_type (decl_specs, type,
15743 token,
15744 /*type_definition_p=*/false);
15745 /* Remember that we are handling a decltype in order to
15746 implement the resolution of DR 1510 when the argument
15747 isn't instantiation dependent. */
15748 decl_specs->decltype_p = true;
15749 }
15750 cp_lexer_consume_token (parser->lexer);
15751 return type;
15752 }
15753
15754 /* If the type-specifier was for a built-in type, we're done. */
15755 if (type)
15756 {
15757 /* Record the type. */
15758 if (decl_specs
15759 && (token->keyword != RID_SIGNED
15760 && token->keyword != RID_UNSIGNED
15761 && token->keyword != RID_SHORT
15762 && token->keyword != RID_LONG))
15763 cp_parser_set_decl_spec_type (decl_specs,
15764 type,
15765 token,
15766 /*type_definition_p=*/false);
15767 if (decl_specs)
15768 decl_specs->any_specifiers_p = true;
15769
15770 /* Consume the token. */
15771 cp_lexer_consume_token (parser->lexer);
15772
15773 if (type == error_mark_node)
15774 return error_mark_node;
15775
15776 /* There is no valid C++ program where a non-template type is
15777 followed by a "<". That usually indicates that the user thought
15778 that the type was a template. */
15779 cp_parser_check_for_invalid_template_id (parser, type, none_type,
15780 token->location);
15781
15782 return TYPE_NAME (type);
15783 }
15784
15785 /* The type-specifier must be a user-defined type. */
15786 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15787 {
15788 bool qualified_p;
15789 bool global_p;
15790
15791 /* Don't gobble tokens or issue error messages if this is an
15792 optional type-specifier. */
15793 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15794 cp_parser_parse_tentatively (parser);
15795
15796 /* Look for the optional `::' operator. */
15797 global_p
15798 = (cp_parser_global_scope_opt (parser,
15799 /*current_scope_valid_p=*/false)
15800 != NULL_TREE);
15801 /* Look for the nested-name specifier. */
15802 qualified_p
15803 = (cp_parser_nested_name_specifier_opt (parser,
15804 /*typename_keyword_p=*/false,
15805 /*check_dependency_p=*/true,
15806 /*type_p=*/false,
15807 /*is_declaration=*/false)
15808 != NULL_TREE);
15809 token = cp_lexer_peek_token (parser->lexer);
15810 /* If we have seen a nested-name-specifier, and the next token
15811 is `template', then we are using the template-id production. */
15812 if (parser->scope
15813 && cp_parser_optional_template_keyword (parser))
15814 {
15815 /* Look for the template-id. */
15816 type = cp_parser_template_id (parser,
15817 /*template_keyword_p=*/true,
15818 /*check_dependency_p=*/true,
15819 none_type,
15820 /*is_declaration=*/false);
15821 /* If the template-id did not name a type, we are out of
15822 luck. */
15823 if (TREE_CODE (type) != TYPE_DECL)
15824 {
15825 cp_parser_error (parser, "expected template-id for type");
15826 type = NULL_TREE;
15827 }
15828 }
15829 /* Otherwise, look for a type-name. */
15830 else
15831 type = cp_parser_type_name (parser);
15832 /* Keep track of all name-lookups performed in class scopes. */
15833 if (type
15834 && !global_p
15835 && !qualified_p
15836 && TREE_CODE (type) == TYPE_DECL
15837 && identifier_p (DECL_NAME (type)))
15838 maybe_note_name_used_in_class (DECL_NAME (type), type);
15839 /* If it didn't work out, we don't have a TYPE. */
15840 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15841 && !cp_parser_parse_definitely (parser))
15842 type = NULL_TREE;
15843 if (type && decl_specs)
15844 cp_parser_set_decl_spec_type (decl_specs, type,
15845 token,
15846 /*type_definition_p=*/false);
15847 }
15848
15849 /* If we didn't get a type-name, issue an error message. */
15850 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15851 {
15852 cp_parser_error (parser, "expected type-name");
15853 return error_mark_node;
15854 }
15855
15856 if (type && type != error_mark_node)
15857 {
15858 /* See if TYPE is an Objective-C type, and if so, parse and
15859 accept any protocol references following it. Do this before
15860 the cp_parser_check_for_invalid_template_id() call, because
15861 Objective-C types can be followed by '<...>' which would
15862 enclose protocol names rather than template arguments, and so
15863 everything is fine. */
15864 if (c_dialect_objc () && !parser->scope
15865 && (objc_is_id (type) || objc_is_class_name (type)))
15866 {
15867 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15868 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15869
15870 /* Clobber the "unqualified" type previously entered into
15871 DECL_SPECS with the new, improved protocol-qualified version. */
15872 if (decl_specs)
15873 decl_specs->type = qual_type;
15874
15875 return qual_type;
15876 }
15877
15878 /* There is no valid C++ program where a non-template type is
15879 followed by a "<". That usually indicates that the user
15880 thought that the type was a template. */
15881 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15882 none_type,
15883 token->location);
15884 }
15885
15886 return type;
15887 }
15888
15889 /* Parse a type-name.
15890
15891 type-name:
15892 class-name
15893 enum-name
15894 typedef-name
15895 simple-template-id [in c++0x]
15896
15897 enum-name:
15898 identifier
15899
15900 typedef-name:
15901 identifier
15902
15903 Concepts:
15904
15905 type-name:
15906 concept-name
15907 partial-concept-id
15908
15909 concept-name:
15910 identifier
15911
15912 Returns a TYPE_DECL for the type. */
15913
15914 static tree
15915 cp_parser_type_name (cp_parser* parser)
15916 {
15917 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
15918 }
15919
15920 /* See above. */
15921 static tree
15922 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
15923 {
15924 tree type_decl;
15925
15926 /* We can't know yet whether it is a class-name or not. */
15927 cp_parser_parse_tentatively (parser);
15928 /* Try a class-name. */
15929 type_decl = cp_parser_class_name (parser,
15930 typename_keyword_p,
15931 /*template_keyword_p=*/false,
15932 none_type,
15933 /*check_dependency_p=*/true,
15934 /*class_head_p=*/false,
15935 /*is_declaration=*/false);
15936 /* If it's not a class-name, keep looking. */
15937 if (!cp_parser_parse_definitely (parser))
15938 {
15939 if (cxx_dialect < cxx11)
15940 /* It must be a typedef-name or an enum-name. */
15941 return cp_parser_nonclass_name (parser);
15942
15943 cp_parser_parse_tentatively (parser);
15944 /* It is either a simple-template-id representing an
15945 instantiation of an alias template... */
15946 type_decl = cp_parser_template_id (parser,
15947 /*template_keyword_p=*/false,
15948 /*check_dependency_p=*/true,
15949 none_type,
15950 /*is_declaration=*/false);
15951 /* Note that this must be an instantiation of an alias template
15952 because [temp.names]/6 says:
15953
15954 A template-id that names an alias template specialization
15955 is a type-name.
15956
15957 Whereas [temp.names]/7 says:
15958
15959 A simple-template-id that names a class template
15960 specialization is a class-name.
15961
15962 With concepts, this could also be a partial-concept-id that
15963 declares a non-type template parameter. */
15964 if (type_decl != NULL_TREE
15965 && TREE_CODE (type_decl) == TYPE_DECL
15966 && TYPE_DECL_ALIAS_P (type_decl))
15967 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15968 else if (is_constrained_parameter (type_decl))
15969 /* Don't do anything. */ ;
15970 else
15971 cp_parser_simulate_error (parser);
15972
15973 if (!cp_parser_parse_definitely (parser))
15974 /* ... Or a typedef-name or an enum-name. */
15975 return cp_parser_nonclass_name (parser);
15976 }
15977
15978 return type_decl;
15979 }
15980
15981 /* Check if DECL and ARGS can form a constrained-type-specifier.
15982 If ARGS is non-null, we try to form a concept check of the
15983 form DECL<?, ARGS> where ? is a wildcard that matches any
15984 kind of template argument. If ARGS is NULL, then we try to
15985 form a concept check of the form DECL<?>. */
15986
15987 static tree
15988 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
15989 tree decl, tree args)
15990 {
15991 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
15992
15993 /* If we a constrained-type-specifier cannot be deduced. */
15994 if (parser->prevent_constrained_type_specifiers)
15995 return NULL_TREE;
15996
15997 /* A constrained type specifier can only be found in an
15998 overload set or as a reference to a template declaration.
15999
16000 FIXME: This might be masking a bug. It's possible that
16001 that the deduction below is causing template specializations
16002 to be formed with the wildcard as an argument. */
16003 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
16004 return NULL_TREE;
16005
16006 /* Try to build a call expression that evaluates the
16007 concept. This can fail if the overload set refers
16008 only to non-templates. */
16009 tree placeholder = build_nt (WILDCARD_DECL);
16010 tree check = build_concept_check (decl, placeholder, args);
16011 if (check == error_mark_node)
16012 return NULL_TREE;
16013
16014 /* Deduce the checked constraint and the prototype parameter.
16015
16016 FIXME: In certain cases, failure to deduce should be a
16017 diagnosable error. */
16018 tree conc;
16019 tree proto;
16020 if (!deduce_constrained_parameter (check, conc, proto))
16021 return NULL_TREE;
16022
16023 /* In template parameter scope, this results in a constrained
16024 parameter. Return a descriptor of that parm. */
16025 if (processing_template_parmlist)
16026 return build_constrained_parameter (conc, proto, args);
16027
16028 /* In a parameter-declaration-clause, constrained-type
16029 specifiers result in invented template parameters. */
16030 if (parser->auto_is_implicit_function_template_parm_p)
16031 {
16032 tree x = build_constrained_parameter (conc, proto, args);
16033 return synthesize_implicit_template_parm (parser, x);
16034 }
16035 else
16036 {
16037 /* Otherwise, we're in a context where the constrained
16038 type name is deduced and the constraint applies
16039 after deduction. */
16040 return make_constrained_auto (conc, args);
16041 }
16042
16043 return NULL_TREE;
16044 }
16045
16046 /* If DECL refers to a concept, return a TYPE_DECL representing
16047 the result of using the constrained type specifier in the
16048 current context. DECL refers to a concept if
16049
16050 - it is an overload set containing a function concept taking a single
16051 type argument, or
16052
16053 - it is a variable concept taking a single type argument. */
16054
16055 static tree
16056 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
16057 {
16058 if (flag_concepts
16059 && (TREE_CODE (decl) == OVERLOAD
16060 || BASELINK_P (decl)
16061 || variable_concept_p (decl)))
16062 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
16063 else
16064 return NULL_TREE;
16065 }
16066
16067 /* Check if DECL and ARGS form a partial-concept-id. If so,
16068 assign ID to the resulting constrained placeholder.
16069
16070 Returns true if the partial-concept-id designates a placeholder
16071 and false otherwise. Note that *id is set to NULL_TREE in
16072 this case. */
16073
16074 static tree
16075 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
16076 {
16077 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
16078 }
16079
16080 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16081 or a concept-name.
16082
16083 enum-name:
16084 identifier
16085
16086 typedef-name:
16087 identifier
16088
16089 concept-name:
16090 identifier
16091
16092 Returns a TYPE_DECL for the type. */
16093
16094 static tree
16095 cp_parser_nonclass_name (cp_parser* parser)
16096 {
16097 tree type_decl;
16098 tree identifier;
16099
16100 cp_token *token = cp_lexer_peek_token (parser->lexer);
16101 identifier = cp_parser_identifier (parser);
16102 if (identifier == error_mark_node)
16103 return error_mark_node;
16104
16105 /* Look up the type-name. */
16106 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
16107
16108 type_decl = strip_using_decl (type_decl);
16109
16110 /* If we found an overload set, then it may refer to a concept-name. */
16111 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
16112 type_decl = decl;
16113
16114 if (TREE_CODE (type_decl) != TYPE_DECL
16115 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
16116 {
16117 /* See if this is an Objective-C type. */
16118 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16119 tree type = objc_get_protocol_qualified_type (identifier, protos);
16120 if (type)
16121 type_decl = TYPE_NAME (type);
16122 }
16123
16124 /* Issue an error if we did not find a type-name. */
16125 if (TREE_CODE (type_decl) != TYPE_DECL
16126 /* In Objective-C, we have the complication that class names are
16127 normally type names and start declarations (eg, the
16128 "NSObject" in "NSObject *object;"), but can be used in an
16129 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16130 is an expression. So, a classname followed by a dot is not a
16131 valid type-name. */
16132 || (objc_is_class_name (TREE_TYPE (type_decl))
16133 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
16134 {
16135 if (!cp_parser_simulate_error (parser))
16136 cp_parser_name_lookup_error (parser, identifier, type_decl,
16137 NLE_TYPE, token->location);
16138 return error_mark_node;
16139 }
16140 /* Remember that the name was used in the definition of the
16141 current class so that we can check later to see if the
16142 meaning would have been different after the class was
16143 entirely defined. */
16144 else if (type_decl != error_mark_node
16145 && !parser->scope)
16146 maybe_note_name_used_in_class (identifier, type_decl);
16147
16148 return type_decl;
16149 }
16150
16151 /* Parse an elaborated-type-specifier. Note that the grammar given
16152 here incorporates the resolution to DR68.
16153
16154 elaborated-type-specifier:
16155 class-key :: [opt] nested-name-specifier [opt] identifier
16156 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16157 enum-key :: [opt] nested-name-specifier [opt] identifier
16158 typename :: [opt] nested-name-specifier identifier
16159 typename :: [opt] nested-name-specifier template [opt]
16160 template-id
16161
16162 GNU extension:
16163
16164 elaborated-type-specifier:
16165 class-key attributes :: [opt] nested-name-specifier [opt] identifier
16166 class-key attributes :: [opt] nested-name-specifier [opt]
16167 template [opt] template-id
16168 enum attributes :: [opt] nested-name-specifier [opt] identifier
16169
16170 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16171 declared `friend'. If IS_DECLARATION is TRUE, then this
16172 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16173 something is being declared.
16174
16175 Returns the TYPE specified. */
16176
16177 static tree
16178 cp_parser_elaborated_type_specifier (cp_parser* parser,
16179 bool is_friend,
16180 bool is_declaration)
16181 {
16182 enum tag_types tag_type;
16183 tree identifier;
16184 tree type = NULL_TREE;
16185 tree attributes = NULL_TREE;
16186 tree globalscope;
16187 cp_token *token = NULL;
16188
16189 /* See if we're looking at the `enum' keyword. */
16190 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
16191 {
16192 /* Consume the `enum' token. */
16193 cp_lexer_consume_token (parser->lexer);
16194 /* Remember that it's an enumeration type. */
16195 tag_type = enum_type;
16196 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16197 enums) is used here. */
16198 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16199 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16200 {
16201 pedwarn (input_location, 0, "elaborated-type-specifier "
16202 "for a scoped enum must not use the %<%D%> keyword",
16203 cp_lexer_peek_token (parser->lexer)->u.value);
16204 /* Consume the `struct' or `class' and parse it anyway. */
16205 cp_lexer_consume_token (parser->lexer);
16206 }
16207 /* Parse the attributes. */
16208 attributes = cp_parser_attributes_opt (parser);
16209 }
16210 /* Or, it might be `typename'. */
16211 else if (cp_lexer_next_token_is_keyword (parser->lexer,
16212 RID_TYPENAME))
16213 {
16214 /* Consume the `typename' token. */
16215 cp_lexer_consume_token (parser->lexer);
16216 /* Remember that it's a `typename' type. */
16217 tag_type = typename_type;
16218 }
16219 /* Otherwise it must be a class-key. */
16220 else
16221 {
16222 tag_type = cp_parser_class_key (parser);
16223 if (tag_type == none_type)
16224 return error_mark_node;
16225 /* Parse the attributes. */
16226 attributes = cp_parser_attributes_opt (parser);
16227 }
16228
16229 /* Look for the `::' operator. */
16230 globalscope = cp_parser_global_scope_opt (parser,
16231 /*current_scope_valid_p=*/false);
16232 /* Look for the nested-name-specifier. */
16233 if (tag_type == typename_type && !globalscope)
16234 {
16235 if (!cp_parser_nested_name_specifier (parser,
16236 /*typename_keyword_p=*/true,
16237 /*check_dependency_p=*/true,
16238 /*type_p=*/true,
16239 is_declaration))
16240 return error_mark_node;
16241 }
16242 else
16243 /* Even though `typename' is not present, the proposed resolution
16244 to Core Issue 180 says that in `class A<T>::B', `B' should be
16245 considered a type-name, even if `A<T>' is dependent. */
16246 cp_parser_nested_name_specifier_opt (parser,
16247 /*typename_keyword_p=*/true,
16248 /*check_dependency_p=*/true,
16249 /*type_p=*/true,
16250 is_declaration);
16251 /* For everything but enumeration types, consider a template-id.
16252 For an enumeration type, consider only a plain identifier. */
16253 if (tag_type != enum_type)
16254 {
16255 bool template_p = false;
16256 tree decl;
16257
16258 /* Allow the `template' keyword. */
16259 template_p = cp_parser_optional_template_keyword (parser);
16260 /* If we didn't see `template', we don't know if there's a
16261 template-id or not. */
16262 if (!template_p)
16263 cp_parser_parse_tentatively (parser);
16264 /* Parse the template-id. */
16265 token = cp_lexer_peek_token (parser->lexer);
16266 decl = cp_parser_template_id (parser, template_p,
16267 /*check_dependency_p=*/true,
16268 tag_type,
16269 is_declaration);
16270 /* If we didn't find a template-id, look for an ordinary
16271 identifier. */
16272 if (!template_p && !cp_parser_parse_definitely (parser))
16273 ;
16274 /* We can get here when cp_parser_template_id, called by
16275 cp_parser_class_name with tag_type == none_type, succeeds
16276 and caches a BASELINK. Then, when called again here,
16277 instead of failing and returning an error_mark_node
16278 returns it (see template/typename17.C in C++11).
16279 ??? Could we diagnose this earlier? */
16280 else if (tag_type == typename_type && BASELINK_P (decl))
16281 {
16282 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
16283 type = error_mark_node;
16284 }
16285 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16286 in effect, then we must assume that, upon instantiation, the
16287 template will correspond to a class. */
16288 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16289 && tag_type == typename_type)
16290 type = make_typename_type (parser->scope, decl,
16291 typename_type,
16292 /*complain=*/tf_error);
16293 /* If the `typename' keyword is in effect and DECL is not a type
16294 decl, then type is non existent. */
16295 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16296 ;
16297 else if (TREE_CODE (decl) == TYPE_DECL)
16298 type = check_elaborated_type_specifier (tag_type, decl,
16299 /*allow_template_p=*/true);
16300 else if (decl == error_mark_node)
16301 type = error_mark_node;
16302 }
16303
16304 if (!type)
16305 {
16306 token = cp_lexer_peek_token (parser->lexer);
16307 identifier = cp_parser_identifier (parser);
16308
16309 if (identifier == error_mark_node)
16310 {
16311 parser->scope = NULL_TREE;
16312 return error_mark_node;
16313 }
16314
16315 /* For a `typename', we needn't call xref_tag. */
16316 if (tag_type == typename_type
16317 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16318 return cp_parser_make_typename_type (parser, identifier,
16319 token->location);
16320
16321 /* Template parameter lists apply only if we are not within a
16322 function parameter list. */
16323 bool template_parm_lists_apply
16324 = parser->num_template_parameter_lists;
16325 if (template_parm_lists_apply)
16326 for (cp_binding_level *s = current_binding_level;
16327 s && s->kind != sk_template_parms;
16328 s = s->level_chain)
16329 if (s->kind == sk_function_parms)
16330 template_parm_lists_apply = false;
16331
16332 /* Look up a qualified name in the usual way. */
16333 if (parser->scope)
16334 {
16335 tree decl;
16336 tree ambiguous_decls;
16337
16338 decl = cp_parser_lookup_name (parser, identifier,
16339 tag_type,
16340 /*is_template=*/false,
16341 /*is_namespace=*/false,
16342 /*check_dependency=*/true,
16343 &ambiguous_decls,
16344 token->location);
16345
16346 /* If the lookup was ambiguous, an error will already have been
16347 issued. */
16348 if (ambiguous_decls)
16349 return error_mark_node;
16350
16351 /* If we are parsing friend declaration, DECL may be a
16352 TEMPLATE_DECL tree node here. However, we need to check
16353 whether this TEMPLATE_DECL results in valid code. Consider
16354 the following example:
16355
16356 namespace N {
16357 template <class T> class C {};
16358 }
16359 class X {
16360 template <class T> friend class N::C; // #1, valid code
16361 };
16362 template <class T> class Y {
16363 friend class N::C; // #2, invalid code
16364 };
16365
16366 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16367 name lookup of `N::C'. We see that friend declaration must
16368 be template for the code to be valid. Note that
16369 processing_template_decl does not work here since it is
16370 always 1 for the above two cases. */
16371
16372 decl = (cp_parser_maybe_treat_template_as_class
16373 (decl, /*tag_name_p=*/is_friend
16374 && template_parm_lists_apply));
16375
16376 if (TREE_CODE (decl) != TYPE_DECL)
16377 {
16378 cp_parser_diagnose_invalid_type_name (parser,
16379 identifier,
16380 token->location);
16381 return error_mark_node;
16382 }
16383
16384 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
16385 {
16386 bool allow_template = (template_parm_lists_apply
16387 || DECL_SELF_REFERENCE_P (decl));
16388 type = check_elaborated_type_specifier (tag_type, decl,
16389 allow_template);
16390
16391 if (type == error_mark_node)
16392 return error_mark_node;
16393 }
16394
16395 /* Forward declarations of nested types, such as
16396
16397 class C1::C2;
16398 class C1::C2::C3;
16399
16400 are invalid unless all components preceding the final '::'
16401 are complete. If all enclosing types are complete, these
16402 declarations become merely pointless.
16403
16404 Invalid forward declarations of nested types are errors
16405 caught elsewhere in parsing. Those that are pointless arrive
16406 here. */
16407
16408 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16409 && !is_friend && !processing_explicit_instantiation)
16410 warning (0, "declaration %qD does not declare anything", decl);
16411
16412 type = TREE_TYPE (decl);
16413 }
16414 else
16415 {
16416 /* An elaborated-type-specifier sometimes introduces a new type and
16417 sometimes names an existing type. Normally, the rule is that it
16418 introduces a new type only if there is not an existing type of
16419 the same name already in scope. For example, given:
16420
16421 struct S {};
16422 void f() { struct S s; }
16423
16424 the `struct S' in the body of `f' is the same `struct S' as in
16425 the global scope; the existing definition is used. However, if
16426 there were no global declaration, this would introduce a new
16427 local class named `S'.
16428
16429 An exception to this rule applies to the following code:
16430
16431 namespace N { struct S; }
16432
16433 Here, the elaborated-type-specifier names a new type
16434 unconditionally; even if there is already an `S' in the
16435 containing scope this declaration names a new type.
16436 This exception only applies if the elaborated-type-specifier
16437 forms the complete declaration:
16438
16439 [class.name]
16440
16441 A declaration consisting solely of `class-key identifier ;' is
16442 either a redeclaration of the name in the current scope or a
16443 forward declaration of the identifier as a class name. It
16444 introduces the name into the current scope.
16445
16446 We are in this situation precisely when the next token is a `;'.
16447
16448 An exception to the exception is that a `friend' declaration does
16449 *not* name a new type; i.e., given:
16450
16451 struct S { friend struct T; };
16452
16453 `T' is not a new type in the scope of `S'.
16454
16455 Also, `new struct S' or `sizeof (struct S)' never results in the
16456 definition of a new type; a new type can only be declared in a
16457 declaration context. */
16458
16459 tag_scope ts;
16460 bool template_p;
16461
16462 if (is_friend)
16463 /* Friends have special name lookup rules. */
16464 ts = ts_within_enclosing_non_class;
16465 else if (is_declaration
16466 && cp_lexer_next_token_is (parser->lexer,
16467 CPP_SEMICOLON))
16468 /* This is a `class-key identifier ;' */
16469 ts = ts_current;
16470 else
16471 ts = ts_global;
16472
16473 template_p =
16474 (template_parm_lists_apply
16475 && (cp_parser_next_token_starts_class_definition_p (parser)
16476 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
16477 /* An unqualified name was used to reference this type, so
16478 there were no qualifying templates. */
16479 if (template_parm_lists_apply
16480 && !cp_parser_check_template_parameters (parser,
16481 /*num_templates=*/0,
16482 token->location,
16483 /*declarator=*/NULL))
16484 return error_mark_node;
16485 type = xref_tag (tag_type, identifier, ts, template_p);
16486 }
16487 }
16488
16489 if (type == error_mark_node)
16490 return error_mark_node;
16491
16492 /* Allow attributes on forward declarations of classes. */
16493 if (attributes)
16494 {
16495 if (TREE_CODE (type) == TYPENAME_TYPE)
16496 warning (OPT_Wattributes,
16497 "attributes ignored on uninstantiated type");
16498 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
16499 && ! processing_explicit_instantiation)
16500 warning (OPT_Wattributes,
16501 "attributes ignored on template instantiation");
16502 else if (is_declaration && cp_parser_declares_only_class_p (parser))
16503 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
16504 else
16505 warning (OPT_Wattributes,
16506 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16507 }
16508
16509 if (tag_type != enum_type)
16510 {
16511 /* Indicate whether this class was declared as a `class' or as a
16512 `struct'. */
16513 if (TREE_CODE (type) == RECORD_TYPE)
16514 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
16515 cp_parser_check_class_key (tag_type, type);
16516 }
16517
16518 /* A "<" cannot follow an elaborated type specifier. If that
16519 happens, the user was probably trying to form a template-id. */
16520 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
16521 token->location);
16522
16523 return type;
16524 }
16525
16526 /* Parse an enum-specifier.
16527
16528 enum-specifier:
16529 enum-head { enumerator-list [opt] }
16530 enum-head { enumerator-list , } [C++0x]
16531
16532 enum-head:
16533 enum-key identifier [opt] enum-base [opt]
16534 enum-key nested-name-specifier identifier enum-base [opt]
16535
16536 enum-key:
16537 enum
16538 enum class [C++0x]
16539 enum struct [C++0x]
16540
16541 enum-base: [C++0x]
16542 : type-specifier-seq
16543
16544 opaque-enum-specifier:
16545 enum-key identifier enum-base [opt] ;
16546
16547 GNU Extensions:
16548 enum-key attributes[opt] identifier [opt] enum-base [opt]
16549 { enumerator-list [opt] }attributes[opt]
16550 enum-key attributes[opt] identifier [opt] enum-base [opt]
16551 { enumerator-list, }attributes[opt] [C++0x]
16552
16553 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
16554 if the token stream isn't an enum-specifier after all. */
16555
16556 static tree
16557 cp_parser_enum_specifier (cp_parser* parser)
16558 {
16559 tree identifier;
16560 tree type = NULL_TREE;
16561 tree prev_scope;
16562 tree nested_name_specifier = NULL_TREE;
16563 tree attributes;
16564 bool scoped_enum_p = false;
16565 bool has_underlying_type = false;
16566 bool nested_being_defined = false;
16567 bool new_value_list = false;
16568 bool is_new_type = false;
16569 bool is_anonymous = false;
16570 tree underlying_type = NULL_TREE;
16571 cp_token *type_start_token = NULL;
16572 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
16573
16574 parser->colon_corrects_to_scope_p = false;
16575
16576 /* Parse tentatively so that we can back up if we don't find a
16577 enum-specifier. */
16578 cp_parser_parse_tentatively (parser);
16579
16580 /* Caller guarantees that the current token is 'enum', an identifier
16581 possibly follows, and the token after that is an opening brace.
16582 If we don't have an identifier, fabricate an anonymous name for
16583 the enumeration being defined. */
16584 cp_lexer_consume_token (parser->lexer);
16585
16586 /* Parse the "class" or "struct", which indicates a scoped
16587 enumeration type in C++0x. */
16588 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16589 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16590 {
16591 if (cxx_dialect < cxx11)
16592 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
16593
16594 /* Consume the `struct' or `class' token. */
16595 cp_lexer_consume_token (parser->lexer);
16596
16597 scoped_enum_p = true;
16598 }
16599
16600 attributes = cp_parser_attributes_opt (parser);
16601
16602 /* Clear the qualification. */
16603 parser->scope = NULL_TREE;
16604 parser->qualifying_scope = NULL_TREE;
16605 parser->object_scope = NULL_TREE;
16606
16607 /* Figure out in what scope the declaration is being placed. */
16608 prev_scope = current_scope ();
16609
16610 type_start_token = cp_lexer_peek_token (parser->lexer);
16611
16612 push_deferring_access_checks (dk_no_check);
16613 nested_name_specifier
16614 = cp_parser_nested_name_specifier_opt (parser,
16615 /*typename_keyword_p=*/true,
16616 /*check_dependency_p=*/false,
16617 /*type_p=*/false,
16618 /*is_declaration=*/false);
16619
16620 if (nested_name_specifier)
16621 {
16622 tree name;
16623
16624 identifier = cp_parser_identifier (parser);
16625 name = cp_parser_lookup_name (parser, identifier,
16626 enum_type,
16627 /*is_template=*/false,
16628 /*is_namespace=*/false,
16629 /*check_dependency=*/true,
16630 /*ambiguous_decls=*/NULL,
16631 input_location);
16632 if (name && name != error_mark_node)
16633 {
16634 type = TREE_TYPE (name);
16635 if (TREE_CODE (type) == TYPENAME_TYPE)
16636 {
16637 /* Are template enums allowed in ISO? */
16638 if (template_parm_scope_p ())
16639 pedwarn (type_start_token->location, OPT_Wpedantic,
16640 "%qD is an enumeration template", name);
16641 /* ignore a typename reference, for it will be solved by name
16642 in start_enum. */
16643 type = NULL_TREE;
16644 }
16645 }
16646 else if (nested_name_specifier == error_mark_node)
16647 /* We already issued an error. */;
16648 else
16649 {
16650 error_at (type_start_token->location,
16651 "%qD does not name an enumeration in %qT",
16652 identifier, nested_name_specifier);
16653 nested_name_specifier = error_mark_node;
16654 }
16655 }
16656 else
16657 {
16658 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16659 identifier = cp_parser_identifier (parser);
16660 else
16661 {
16662 identifier = make_anon_name ();
16663 is_anonymous = true;
16664 if (scoped_enum_p)
16665 error_at (type_start_token->location,
16666 "anonymous scoped enum is not allowed");
16667 }
16668 }
16669 pop_deferring_access_checks ();
16670
16671 /* Check for the `:' that denotes a specified underlying type in C++0x.
16672 Note that a ':' could also indicate a bitfield width, however. */
16673 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16674 {
16675 cp_decl_specifier_seq type_specifiers;
16676
16677 /* Consume the `:'. */
16678 cp_lexer_consume_token (parser->lexer);
16679
16680 /* Parse the type-specifier-seq. */
16681 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16682 /*is_trailing_return=*/false,
16683 &type_specifiers);
16684
16685 /* At this point this is surely not elaborated type specifier. */
16686 if (!cp_parser_parse_definitely (parser))
16687 return NULL_TREE;
16688
16689 if (cxx_dialect < cxx11)
16690 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
16691
16692 has_underlying_type = true;
16693
16694 /* If that didn't work, stop. */
16695 if (type_specifiers.type != error_mark_node)
16696 {
16697 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
16698 /*initialized=*/0, NULL);
16699 if (underlying_type == error_mark_node
16700 || check_for_bare_parameter_packs (underlying_type))
16701 underlying_type = NULL_TREE;
16702 }
16703 }
16704
16705 /* Look for the `{' but don't consume it yet. */
16706 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16707 {
16708 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
16709 {
16710 cp_parser_error (parser, "expected %<{%>");
16711 if (has_underlying_type)
16712 {
16713 type = NULL_TREE;
16714 goto out;
16715 }
16716 }
16717 /* An opaque-enum-specifier must have a ';' here. */
16718 if ((scoped_enum_p || underlying_type)
16719 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16720 {
16721 cp_parser_error (parser, "expected %<;%> or %<{%>");
16722 if (has_underlying_type)
16723 {
16724 type = NULL_TREE;
16725 goto out;
16726 }
16727 }
16728 }
16729
16730 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
16731 return NULL_TREE;
16732
16733 if (nested_name_specifier)
16734 {
16735 if (CLASS_TYPE_P (nested_name_specifier))
16736 {
16737 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
16738 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
16739 push_scope (nested_name_specifier);
16740 }
16741 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16742 {
16743 push_nested_namespace (nested_name_specifier);
16744 }
16745 }
16746
16747 /* Issue an error message if type-definitions are forbidden here. */
16748 if (!cp_parser_check_type_definition (parser))
16749 type = error_mark_node;
16750 else
16751 /* Create the new type. We do this before consuming the opening
16752 brace so the enum will be recorded as being on the line of its
16753 tag (or the 'enum' keyword, if there is no tag). */
16754 type = start_enum (identifier, type, underlying_type,
16755 scoped_enum_p, &is_new_type);
16756
16757 /* If the next token is not '{' it is an opaque-enum-specifier or an
16758 elaborated-type-specifier. */
16759 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16760 {
16761 timevar_push (TV_PARSE_ENUM);
16762 if (nested_name_specifier
16763 && nested_name_specifier != error_mark_node)
16764 {
16765 /* The following catches invalid code such as:
16766 enum class S<int>::E { A, B, C }; */
16767 if (!processing_specialization
16768 && CLASS_TYPE_P (nested_name_specifier)
16769 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
16770 error_at (type_start_token->location, "cannot add an enumerator "
16771 "list to a template instantiation");
16772
16773 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
16774 {
16775 error_at (type_start_token->location,
16776 "%<%T::%E%> has not been declared",
16777 TYPE_CONTEXT (nested_name_specifier),
16778 nested_name_specifier);
16779 type = error_mark_node;
16780 }
16781 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
16782 && !CLASS_TYPE_P (nested_name_specifier))
16783 {
16784 error_at (type_start_token->location, "nested name specifier "
16785 "%qT for enum declaration does not name a class "
16786 "or namespace", nested_name_specifier);
16787 type = error_mark_node;
16788 }
16789 /* If that scope does not contain the scope in which the
16790 class was originally declared, the program is invalid. */
16791 else if (prev_scope && !is_ancestor (prev_scope,
16792 nested_name_specifier))
16793 {
16794 if (at_namespace_scope_p ())
16795 error_at (type_start_token->location,
16796 "declaration of %qD in namespace %qD which does not "
16797 "enclose %qD",
16798 type, prev_scope, nested_name_specifier);
16799 else
16800 error_at (type_start_token->location,
16801 "declaration of %qD in %qD which does not "
16802 "enclose %qD",
16803 type, prev_scope, nested_name_specifier);
16804 type = error_mark_node;
16805 }
16806 }
16807
16808 if (scoped_enum_p)
16809 begin_scope (sk_scoped_enum, type);
16810
16811 /* Consume the opening brace. */
16812 cp_lexer_consume_token (parser->lexer);
16813
16814 if (type == error_mark_node)
16815 ; /* Nothing to add */
16816 else if (OPAQUE_ENUM_P (type)
16817 || (cxx_dialect > cxx98 && processing_specialization))
16818 {
16819 new_value_list = true;
16820 SET_OPAQUE_ENUM_P (type, false);
16821 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
16822 }
16823 else
16824 {
16825 error_at (type_start_token->location,
16826 "multiple definition of %q#T", type);
16827 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
16828 "previous definition here");
16829 type = error_mark_node;
16830 }
16831
16832 if (type == error_mark_node)
16833 cp_parser_skip_to_end_of_block_or_statement (parser);
16834 /* If the next token is not '}', then there are some enumerators. */
16835 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16836 {
16837 if (is_anonymous && !scoped_enum_p)
16838 pedwarn (type_start_token->location, OPT_Wpedantic,
16839 "ISO C++ forbids empty anonymous enum");
16840 }
16841 else
16842 cp_parser_enumerator_list (parser, type);
16843
16844 /* Consume the final '}'. */
16845 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16846
16847 if (scoped_enum_p)
16848 finish_scope ();
16849 timevar_pop (TV_PARSE_ENUM);
16850 }
16851 else
16852 {
16853 /* If a ';' follows, then it is an opaque-enum-specifier
16854 and additional restrictions apply. */
16855 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16856 {
16857 if (is_anonymous)
16858 error_at (type_start_token->location,
16859 "opaque-enum-specifier without name");
16860 else if (nested_name_specifier)
16861 error_at (type_start_token->location,
16862 "opaque-enum-specifier must use a simple identifier");
16863 }
16864 }
16865
16866 /* Look for trailing attributes to apply to this enumeration, and
16867 apply them if appropriate. */
16868 if (cp_parser_allow_gnu_extensions_p (parser))
16869 {
16870 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
16871 trailing_attr = chainon (trailing_attr, attributes);
16872 cplus_decl_attributes (&type,
16873 trailing_attr,
16874 (int) ATTR_FLAG_TYPE_IN_PLACE);
16875 }
16876
16877 /* Finish up the enumeration. */
16878 if (type != error_mark_node)
16879 {
16880 if (new_value_list)
16881 finish_enum_value_list (type);
16882 if (is_new_type)
16883 finish_enum (type);
16884 }
16885
16886 if (nested_name_specifier)
16887 {
16888 if (CLASS_TYPE_P (nested_name_specifier))
16889 {
16890 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16891 pop_scope (nested_name_specifier);
16892 }
16893 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16894 {
16895 pop_nested_namespace (nested_name_specifier);
16896 }
16897 }
16898 out:
16899 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16900 return type;
16901 }
16902
16903 /* Parse an enumerator-list. The enumerators all have the indicated
16904 TYPE.
16905
16906 enumerator-list:
16907 enumerator-definition
16908 enumerator-list , enumerator-definition */
16909
16910 static void
16911 cp_parser_enumerator_list (cp_parser* parser, tree type)
16912 {
16913 while (true)
16914 {
16915 /* Parse an enumerator-definition. */
16916 cp_parser_enumerator_definition (parser, type);
16917
16918 /* If the next token is not a ',', we've reached the end of
16919 the list. */
16920 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16921 break;
16922 /* Otherwise, consume the `,' and keep going. */
16923 cp_lexer_consume_token (parser->lexer);
16924 /* If the next token is a `}', there is a trailing comma. */
16925 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16926 {
16927 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16928 pedwarn (input_location, OPT_Wpedantic,
16929 "comma at end of enumerator list");
16930 break;
16931 }
16932 }
16933 }
16934
16935 /* Parse an enumerator-definition. The enumerator has the indicated
16936 TYPE.
16937
16938 enumerator-definition:
16939 enumerator
16940 enumerator = constant-expression
16941
16942 enumerator:
16943 identifier
16944
16945 GNU Extensions:
16946
16947 enumerator-definition:
16948 enumerator attributes [opt]
16949 enumerator attributes [opt] = constant-expression */
16950
16951 static void
16952 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16953 {
16954 tree identifier;
16955 tree value;
16956 location_t loc;
16957
16958 /* Save the input location because we are interested in the location
16959 of the identifier and not the location of the explicit value. */
16960 loc = cp_lexer_peek_token (parser->lexer)->location;
16961
16962 /* Look for the identifier. */
16963 identifier = cp_parser_identifier (parser);
16964 if (identifier == error_mark_node)
16965 return;
16966
16967 /* Parse any specified attributes. */
16968 tree attrs = cp_parser_attributes_opt (parser);
16969
16970 /* If the next token is an '=', then there is an explicit value. */
16971 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16972 {
16973 /* Consume the `=' token. */
16974 cp_lexer_consume_token (parser->lexer);
16975 /* Parse the value. */
16976 value = cp_parser_constant_expression (parser);
16977 }
16978 else
16979 value = NULL_TREE;
16980
16981 /* If we are processing a template, make sure the initializer of the
16982 enumerator doesn't contain any bare template parameter pack. */
16983 if (check_for_bare_parameter_packs (value))
16984 value = error_mark_node;
16985
16986 /* Create the enumerator. */
16987 build_enumerator (identifier, value, type, attrs, loc);
16988 }
16989
16990 /* Parse a namespace-name.
16991
16992 namespace-name:
16993 original-namespace-name
16994 namespace-alias
16995
16996 Returns the NAMESPACE_DECL for the namespace. */
16997
16998 static tree
16999 cp_parser_namespace_name (cp_parser* parser)
17000 {
17001 tree identifier;
17002 tree namespace_decl;
17003
17004 cp_token *token = cp_lexer_peek_token (parser->lexer);
17005
17006 /* Get the name of the namespace. */
17007 identifier = cp_parser_identifier (parser);
17008 if (identifier == error_mark_node)
17009 return error_mark_node;
17010
17011 /* Look up the identifier in the currently active scope. Look only
17012 for namespaces, due to:
17013
17014 [basic.lookup.udir]
17015
17016 When looking up a namespace-name in a using-directive or alias
17017 definition, only namespace names are considered.
17018
17019 And:
17020
17021 [basic.lookup.qual]
17022
17023 During the lookup of a name preceding the :: scope resolution
17024 operator, object, function, and enumerator names are ignored.
17025
17026 (Note that cp_parser_qualifying_entity only calls this
17027 function if the token after the name is the scope resolution
17028 operator.) */
17029 namespace_decl = cp_parser_lookup_name (parser, identifier,
17030 none_type,
17031 /*is_template=*/false,
17032 /*is_namespace=*/true,
17033 /*check_dependency=*/true,
17034 /*ambiguous_decls=*/NULL,
17035 token->location);
17036 /* If it's not a namespace, issue an error. */
17037 if (namespace_decl == error_mark_node
17038 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
17039 {
17040 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17041 error_at (token->location, "%qD is not a namespace-name", identifier);
17042 cp_parser_error (parser, "expected namespace-name");
17043 namespace_decl = error_mark_node;
17044 }
17045
17046 return namespace_decl;
17047 }
17048
17049 /* Parse a namespace-definition.
17050
17051 namespace-definition:
17052 named-namespace-definition
17053 unnamed-namespace-definition
17054
17055 named-namespace-definition:
17056 original-namespace-definition
17057 extension-namespace-definition
17058
17059 original-namespace-definition:
17060 namespace identifier { namespace-body }
17061
17062 extension-namespace-definition:
17063 namespace original-namespace-name { namespace-body }
17064
17065 unnamed-namespace-definition:
17066 namespace { namespace-body } */
17067
17068 static void
17069 cp_parser_namespace_definition (cp_parser* parser)
17070 {
17071 tree identifier, attribs;
17072 bool has_visibility;
17073 bool is_inline;
17074 cp_token* token;
17075 int nested_definition_count = 0;
17076
17077 cp_ensure_no_omp_declare_simd (parser);
17078 cp_ensure_no_oacc_routine (parser);
17079 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
17080 {
17081 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
17082 is_inline = true;
17083 cp_lexer_consume_token (parser->lexer);
17084 }
17085 else
17086 is_inline = false;
17087
17088 /* Look for the `namespace' keyword. */
17089 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17090
17091 /* Parse any specified attributes before the identifier. */
17092 attribs = cp_parser_attributes_opt (parser);
17093
17094 /* Get the name of the namespace. We do not attempt to distinguish
17095 between an original-namespace-definition and an
17096 extension-namespace-definition at this point. The semantic
17097 analysis routines are responsible for that. */
17098 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17099 identifier = cp_parser_identifier (parser);
17100 else
17101 identifier = NULL_TREE;
17102
17103 /* Parse any specified attributes after the identifier. */
17104 tree post_ident_attribs = cp_parser_attributes_opt (parser);
17105 if (post_ident_attribs)
17106 {
17107 if (attribs)
17108 attribs = chainon (attribs, post_ident_attribs);
17109 else
17110 attribs = post_ident_attribs;
17111 }
17112
17113 /* Start the namespace. */
17114 push_namespace (identifier);
17115
17116 /* Parse any nested namespace definition. */
17117 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17118 {
17119 if (attribs)
17120 error_at (token->location, "a nested namespace definition cannot have attributes");
17121 if (cxx_dialect < cxx1z)
17122 pedwarn (input_location, OPT_Wpedantic,
17123 "nested namespace definitions only available with "
17124 "-std=c++1z or -std=gnu++1z");
17125 if (is_inline)
17126 error_at (token->location, "a nested namespace definition cannot be inline");
17127 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17128 {
17129 cp_lexer_consume_token (parser->lexer);
17130 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17131 identifier = cp_parser_identifier (parser);
17132 else
17133 {
17134 cp_parser_error (parser, "nested identifier required");
17135 break;
17136 }
17137 ++nested_definition_count;
17138 push_namespace (identifier);
17139 }
17140 }
17141
17142 /* Look for the `{' to validate starting the namespace. */
17143 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
17144
17145 /* "inline namespace" is equivalent to a stub namespace definition
17146 followed by a strong using directive. */
17147 if (is_inline)
17148 {
17149 tree name_space = current_namespace;
17150 /* Set up namespace association. */
17151 DECL_NAMESPACE_ASSOCIATIONS (name_space)
17152 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
17153 DECL_NAMESPACE_ASSOCIATIONS (name_space));
17154 /* Import the contents of the inline namespace. */
17155 pop_namespace ();
17156 do_using_directive (name_space);
17157 push_namespace (identifier);
17158 }
17159
17160 has_visibility = handle_namespace_attrs (current_namespace, attribs);
17161
17162 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
17163
17164 /* Parse the body of the namespace. */
17165 cp_parser_namespace_body (parser);
17166
17167 if (has_visibility)
17168 pop_visibility (1);
17169
17170 /* Finish the nested namespace definitions. */
17171 while (nested_definition_count--)
17172 pop_namespace ();
17173
17174 /* Finish the namespace. */
17175 pop_namespace ();
17176 /* Look for the final `}'. */
17177 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17178 }
17179
17180 /* Parse a namespace-body.
17181
17182 namespace-body:
17183 declaration-seq [opt] */
17184
17185 static void
17186 cp_parser_namespace_body (cp_parser* parser)
17187 {
17188 cp_parser_declaration_seq_opt (parser);
17189 }
17190
17191 /* Parse a namespace-alias-definition.
17192
17193 namespace-alias-definition:
17194 namespace identifier = qualified-namespace-specifier ; */
17195
17196 static void
17197 cp_parser_namespace_alias_definition (cp_parser* parser)
17198 {
17199 tree identifier;
17200 tree namespace_specifier;
17201
17202 cp_token *token = cp_lexer_peek_token (parser->lexer);
17203
17204 /* Look for the `namespace' keyword. */
17205 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17206 /* Look for the identifier. */
17207 identifier = cp_parser_identifier (parser);
17208 if (identifier == error_mark_node)
17209 return;
17210 /* Look for the `=' token. */
17211 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
17212 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17213 {
17214 error_at (token->location, "%<namespace%> definition is not allowed here");
17215 /* Skip the definition. */
17216 cp_lexer_consume_token (parser->lexer);
17217 if (cp_parser_skip_to_closing_brace (parser))
17218 cp_lexer_consume_token (parser->lexer);
17219 return;
17220 }
17221 cp_parser_require (parser, CPP_EQ, RT_EQ);
17222 /* Look for the qualified-namespace-specifier. */
17223 namespace_specifier
17224 = cp_parser_qualified_namespace_specifier (parser);
17225 /* Look for the `;' token. */
17226 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17227
17228 /* Register the alias in the symbol table. */
17229 do_namespace_alias (identifier, namespace_specifier);
17230 }
17231
17232 /* Parse a qualified-namespace-specifier.
17233
17234 qualified-namespace-specifier:
17235 :: [opt] nested-name-specifier [opt] namespace-name
17236
17237 Returns a NAMESPACE_DECL corresponding to the specified
17238 namespace. */
17239
17240 static tree
17241 cp_parser_qualified_namespace_specifier (cp_parser* parser)
17242 {
17243 /* Look for the optional `::'. */
17244 cp_parser_global_scope_opt (parser,
17245 /*current_scope_valid_p=*/false);
17246
17247 /* Look for the optional nested-name-specifier. */
17248 cp_parser_nested_name_specifier_opt (parser,
17249 /*typename_keyword_p=*/false,
17250 /*check_dependency_p=*/true,
17251 /*type_p=*/false,
17252 /*is_declaration=*/true);
17253
17254 return cp_parser_namespace_name (parser);
17255 }
17256
17257 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17258 access declaration.
17259
17260 using-declaration:
17261 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17262 using :: unqualified-id ;
17263
17264 access-declaration:
17265 qualified-id ;
17266
17267 */
17268
17269 static bool
17270 cp_parser_using_declaration (cp_parser* parser,
17271 bool access_declaration_p)
17272 {
17273 cp_token *token;
17274 bool typename_p = false;
17275 bool global_scope_p;
17276 tree decl;
17277 tree identifier;
17278 tree qscope;
17279 int oldcount = errorcount;
17280 cp_token *diag_token = NULL;
17281
17282 if (access_declaration_p)
17283 {
17284 diag_token = cp_lexer_peek_token (parser->lexer);
17285 cp_parser_parse_tentatively (parser);
17286 }
17287 else
17288 {
17289 /* Look for the `using' keyword. */
17290 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17291
17292 /* Peek at the next token. */
17293 token = cp_lexer_peek_token (parser->lexer);
17294 /* See if it's `typename'. */
17295 if (token->keyword == RID_TYPENAME)
17296 {
17297 /* Remember that we've seen it. */
17298 typename_p = true;
17299 /* Consume the `typename' token. */
17300 cp_lexer_consume_token (parser->lexer);
17301 }
17302 }
17303
17304 /* Look for the optional global scope qualification. */
17305 global_scope_p
17306 = (cp_parser_global_scope_opt (parser,
17307 /*current_scope_valid_p=*/false)
17308 != NULL_TREE);
17309
17310 /* If we saw `typename', or didn't see `::', then there must be a
17311 nested-name-specifier present. */
17312 if (typename_p || !global_scope_p)
17313 {
17314 qscope = cp_parser_nested_name_specifier (parser, typename_p,
17315 /*check_dependency_p=*/true,
17316 /*type_p=*/false,
17317 /*is_declaration=*/true);
17318 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
17319 {
17320 cp_parser_skip_to_end_of_block_or_statement (parser);
17321 return false;
17322 }
17323 }
17324 /* Otherwise, we could be in either of the two productions. In that
17325 case, treat the nested-name-specifier as optional. */
17326 else
17327 qscope = cp_parser_nested_name_specifier_opt (parser,
17328 /*typename_keyword_p=*/false,
17329 /*check_dependency_p=*/true,
17330 /*type_p=*/false,
17331 /*is_declaration=*/true);
17332 if (!qscope)
17333 qscope = global_namespace;
17334 else if (UNSCOPED_ENUM_P (qscope))
17335 qscope = CP_TYPE_CONTEXT (qscope);
17336
17337 if (access_declaration_p && cp_parser_error_occurred (parser))
17338 /* Something has already gone wrong; there's no need to parse
17339 further. Since an error has occurred, the return value of
17340 cp_parser_parse_definitely will be false, as required. */
17341 return cp_parser_parse_definitely (parser);
17342
17343 token = cp_lexer_peek_token (parser->lexer);
17344 /* Parse the unqualified-id. */
17345 identifier = cp_parser_unqualified_id (parser,
17346 /*template_keyword_p=*/false,
17347 /*check_dependency_p=*/true,
17348 /*declarator_p=*/true,
17349 /*optional_p=*/false);
17350
17351 if (access_declaration_p)
17352 {
17353 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17354 cp_parser_simulate_error (parser);
17355 if (!cp_parser_parse_definitely (parser))
17356 return false;
17357 }
17358
17359 /* The function we call to handle a using-declaration is different
17360 depending on what scope we are in. */
17361 if (qscope == error_mark_node || identifier == error_mark_node)
17362 ;
17363 else if (!identifier_p (identifier)
17364 && TREE_CODE (identifier) != BIT_NOT_EXPR)
17365 /* [namespace.udecl]
17366
17367 A using declaration shall not name a template-id. */
17368 error_at (token->location,
17369 "a template-id may not appear in a using-declaration");
17370 else
17371 {
17372 if (at_class_scope_p ())
17373 {
17374 /* Create the USING_DECL. */
17375 decl = do_class_using_decl (parser->scope, identifier);
17376
17377 if (decl && typename_p)
17378 USING_DECL_TYPENAME_P (decl) = 1;
17379
17380 if (check_for_bare_parameter_packs (decl))
17381 {
17382 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17383 return false;
17384 }
17385 else
17386 /* Add it to the list of members in this class. */
17387 finish_member_declaration (decl);
17388 }
17389 else
17390 {
17391 decl = cp_parser_lookup_name_simple (parser,
17392 identifier,
17393 token->location);
17394 if (decl == error_mark_node)
17395 cp_parser_name_lookup_error (parser, identifier,
17396 decl, NLE_NULL,
17397 token->location);
17398 else if (check_for_bare_parameter_packs (decl))
17399 {
17400 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17401 return false;
17402 }
17403 else if (!at_namespace_scope_p ())
17404 do_local_using_decl (decl, qscope, identifier);
17405 else
17406 do_toplevel_using_decl (decl, qscope, identifier);
17407 }
17408 }
17409
17410 /* Look for the final `;'. */
17411 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17412
17413 if (access_declaration_p && errorcount == oldcount)
17414 warning_at (diag_token->location, OPT_Wdeprecated,
17415 "access declarations are deprecated "
17416 "in favour of using-declarations; "
17417 "suggestion: add the %<using%> keyword");
17418
17419 return true;
17420 }
17421
17422 /* Parse an alias-declaration.
17423
17424 alias-declaration:
17425 using identifier attribute-specifier-seq [opt] = type-id */
17426
17427 static tree
17428 cp_parser_alias_declaration (cp_parser* parser)
17429 {
17430 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
17431 location_t id_location;
17432 cp_declarator *declarator;
17433 cp_decl_specifier_seq decl_specs;
17434 bool member_p;
17435 const char *saved_message = NULL;
17436
17437 /* Look for the `using' keyword. */
17438 cp_token *using_token
17439 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
17440 if (using_token == NULL)
17441 return error_mark_node;
17442
17443 id_location = cp_lexer_peek_token (parser->lexer)->location;
17444 id = cp_parser_identifier (parser);
17445 if (id == error_mark_node)
17446 return error_mark_node;
17447
17448 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
17449 attributes = cp_parser_attributes_opt (parser);
17450 if (attributes == error_mark_node)
17451 return error_mark_node;
17452
17453 cp_parser_require (parser, CPP_EQ, RT_EQ);
17454
17455 if (cp_parser_error_occurred (parser))
17456 return error_mark_node;
17457
17458 cp_parser_commit_to_tentative_parse (parser);
17459
17460 /* Now we are going to parse the type-id of the declaration. */
17461
17462 /*
17463 [dcl.type]/3 says:
17464
17465 "A type-specifier-seq shall not define a class or enumeration
17466 unless it appears in the type-id of an alias-declaration (7.1.3) that
17467 is not the declaration of a template-declaration."
17468
17469 In other words, if we currently are in an alias template, the
17470 type-id should not define a type.
17471
17472 So let's set parser->type_definition_forbidden_message in that
17473 case; cp_parser_check_type_definition (called by
17474 cp_parser_class_specifier) will then emit an error if a type is
17475 defined in the type-id. */
17476 if (parser->num_template_parameter_lists)
17477 {
17478 saved_message = parser->type_definition_forbidden_message;
17479 parser->type_definition_forbidden_message =
17480 G_("types may not be defined in alias template declarations");
17481 }
17482
17483 type = cp_parser_type_id (parser);
17484
17485 /* Restore the error message if need be. */
17486 if (parser->num_template_parameter_lists)
17487 parser->type_definition_forbidden_message = saved_message;
17488
17489 if (type == error_mark_node
17490 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
17491 {
17492 cp_parser_skip_to_end_of_block_or_statement (parser);
17493 return error_mark_node;
17494 }
17495
17496 /* A typedef-name can also be introduced by an alias-declaration. The
17497 identifier following the using keyword becomes a typedef-name. It has
17498 the same semantics as if it were introduced by the typedef
17499 specifier. In particular, it does not define a new type and it shall
17500 not appear in the type-id. */
17501
17502 clear_decl_specs (&decl_specs);
17503 decl_specs.type = type;
17504 if (attributes != NULL_TREE)
17505 {
17506 decl_specs.attributes = attributes;
17507 set_and_check_decl_spec_loc (&decl_specs,
17508 ds_attribute,
17509 attrs_token);
17510 }
17511 set_and_check_decl_spec_loc (&decl_specs,
17512 ds_typedef,
17513 using_token);
17514 set_and_check_decl_spec_loc (&decl_specs,
17515 ds_alias,
17516 using_token);
17517
17518 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
17519 declarator->id_loc = id_location;
17520
17521 member_p = at_class_scope_p ();
17522 if (member_p)
17523 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
17524 NULL_TREE, attributes);
17525 else
17526 decl = start_decl (declarator, &decl_specs, 0,
17527 attributes, NULL_TREE, &pushed_scope);
17528 if (decl == error_mark_node)
17529 return decl;
17530
17531 // Attach constraints to the alias declaration.
17532 if (flag_concepts && current_template_parms)
17533 {
17534 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
17535 tree constr = build_constraints (reqs, NULL_TREE);
17536 set_constraints (decl, constr);
17537 }
17538
17539 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
17540
17541 if (pushed_scope)
17542 pop_scope (pushed_scope);
17543
17544 /* If decl is a template, return its TEMPLATE_DECL so that it gets
17545 added into the symbol table; otherwise, return the TYPE_DECL. */
17546 if (DECL_LANG_SPECIFIC (decl)
17547 && DECL_TEMPLATE_INFO (decl)
17548 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
17549 {
17550 decl = DECL_TI_TEMPLATE (decl);
17551 if (member_p)
17552 check_member_template (decl);
17553 }
17554
17555 return decl;
17556 }
17557
17558 /* Parse a using-directive.
17559
17560 using-directive:
17561 using namespace :: [opt] nested-name-specifier [opt]
17562 namespace-name ; */
17563
17564 static void
17565 cp_parser_using_directive (cp_parser* parser)
17566 {
17567 tree namespace_decl;
17568 tree attribs;
17569
17570 /* Look for the `using' keyword. */
17571 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17572 /* And the `namespace' keyword. */
17573 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17574 /* Look for the optional `::' operator. */
17575 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17576 /* And the optional nested-name-specifier. */
17577 cp_parser_nested_name_specifier_opt (parser,
17578 /*typename_keyword_p=*/false,
17579 /*check_dependency_p=*/true,
17580 /*type_p=*/false,
17581 /*is_declaration=*/true);
17582 /* Get the namespace being used. */
17583 namespace_decl = cp_parser_namespace_name (parser);
17584 /* And any specified attributes. */
17585 attribs = cp_parser_attributes_opt (parser);
17586 /* Update the symbol table. */
17587 parse_using_directive (namespace_decl, attribs);
17588 /* Look for the final `;'. */
17589 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17590 }
17591
17592 /* Parse an asm-definition.
17593
17594 asm-definition:
17595 asm ( string-literal ) ;
17596
17597 GNU Extension:
17598
17599 asm-definition:
17600 asm volatile [opt] ( string-literal ) ;
17601 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
17602 asm volatile [opt] ( string-literal : asm-operand-list [opt]
17603 : asm-operand-list [opt] ) ;
17604 asm volatile [opt] ( string-literal : asm-operand-list [opt]
17605 : asm-operand-list [opt]
17606 : asm-clobber-list [opt] ) ;
17607 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
17608 : asm-clobber-list [opt]
17609 : asm-goto-list ) ; */
17610
17611 static void
17612 cp_parser_asm_definition (cp_parser* parser)
17613 {
17614 tree string;
17615 tree outputs = NULL_TREE;
17616 tree inputs = NULL_TREE;
17617 tree clobbers = NULL_TREE;
17618 tree labels = NULL_TREE;
17619 tree asm_stmt;
17620 bool volatile_p = false;
17621 bool extended_p = false;
17622 bool invalid_inputs_p = false;
17623 bool invalid_outputs_p = false;
17624 bool goto_p = false;
17625 required_token missing = RT_NONE;
17626
17627 /* Look for the `asm' keyword. */
17628 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
17629
17630 if (parser->in_function_body
17631 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
17632 {
17633 error ("%<asm%> in %<constexpr%> function");
17634 cp_function_chain->invalid_constexpr = true;
17635 }
17636
17637 /* See if the next token is `volatile'. */
17638 if (cp_parser_allow_gnu_extensions_p (parser)
17639 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
17640 {
17641 /* Remember that we saw the `volatile' keyword. */
17642 volatile_p = true;
17643 /* Consume the token. */
17644 cp_lexer_consume_token (parser->lexer);
17645 }
17646 if (cp_parser_allow_gnu_extensions_p (parser)
17647 && parser->in_function_body
17648 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
17649 {
17650 /* Remember that we saw the `goto' keyword. */
17651 goto_p = true;
17652 /* Consume the token. */
17653 cp_lexer_consume_token (parser->lexer);
17654 }
17655 /* Look for the opening `('. */
17656 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
17657 return;
17658 /* Look for the string. */
17659 string = cp_parser_string_literal (parser, false, false);
17660 if (string == error_mark_node)
17661 {
17662 cp_parser_skip_to_closing_parenthesis (parser, true, false,
17663 /*consume_paren=*/true);
17664 return;
17665 }
17666
17667 /* If we're allowing GNU extensions, check for the extended assembly
17668 syntax. Unfortunately, the `:' tokens need not be separated by
17669 a space in C, and so, for compatibility, we tolerate that here
17670 too. Doing that means that we have to treat the `::' operator as
17671 two `:' tokens. */
17672 if (cp_parser_allow_gnu_extensions_p (parser)
17673 && parser->in_function_body
17674 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
17675 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
17676 {
17677 bool inputs_p = false;
17678 bool clobbers_p = false;
17679 bool labels_p = false;
17680
17681 /* The extended syntax was used. */
17682 extended_p = true;
17683
17684 /* Look for outputs. */
17685 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17686 {
17687 /* Consume the `:'. */
17688 cp_lexer_consume_token (parser->lexer);
17689 /* Parse the output-operands. */
17690 if (cp_lexer_next_token_is_not (parser->lexer,
17691 CPP_COLON)
17692 && cp_lexer_next_token_is_not (parser->lexer,
17693 CPP_SCOPE)
17694 && cp_lexer_next_token_is_not (parser->lexer,
17695 CPP_CLOSE_PAREN)
17696 && !goto_p)
17697 {
17698 outputs = cp_parser_asm_operand_list (parser);
17699 if (outputs == error_mark_node)
17700 invalid_outputs_p = true;
17701 }
17702 }
17703 /* If the next token is `::', there are no outputs, and the
17704 next token is the beginning of the inputs. */
17705 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17706 /* The inputs are coming next. */
17707 inputs_p = true;
17708
17709 /* Look for inputs. */
17710 if (inputs_p
17711 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17712 {
17713 /* Consume the `:' or `::'. */
17714 cp_lexer_consume_token (parser->lexer);
17715 /* Parse the output-operands. */
17716 if (cp_lexer_next_token_is_not (parser->lexer,
17717 CPP_COLON)
17718 && cp_lexer_next_token_is_not (parser->lexer,
17719 CPP_SCOPE)
17720 && cp_lexer_next_token_is_not (parser->lexer,
17721 CPP_CLOSE_PAREN))
17722 {
17723 inputs = cp_parser_asm_operand_list (parser);
17724 if (inputs == error_mark_node)
17725 invalid_inputs_p = true;
17726 }
17727 }
17728 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17729 /* The clobbers are coming next. */
17730 clobbers_p = true;
17731
17732 /* Look for clobbers. */
17733 if (clobbers_p
17734 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17735 {
17736 clobbers_p = true;
17737 /* Consume the `:' or `::'. */
17738 cp_lexer_consume_token (parser->lexer);
17739 /* Parse the clobbers. */
17740 if (cp_lexer_next_token_is_not (parser->lexer,
17741 CPP_COLON)
17742 && cp_lexer_next_token_is_not (parser->lexer,
17743 CPP_CLOSE_PAREN))
17744 clobbers = cp_parser_asm_clobber_list (parser);
17745 }
17746 else if (goto_p
17747 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17748 /* The labels are coming next. */
17749 labels_p = true;
17750
17751 /* Look for labels. */
17752 if (labels_p
17753 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
17754 {
17755 labels_p = true;
17756 /* Consume the `:' or `::'. */
17757 cp_lexer_consume_token (parser->lexer);
17758 /* Parse the labels. */
17759 labels = cp_parser_asm_label_list (parser);
17760 }
17761
17762 if (goto_p && !labels_p)
17763 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
17764 }
17765 else if (goto_p)
17766 missing = RT_COLON_SCOPE;
17767
17768 /* Look for the closing `)'. */
17769 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
17770 missing ? missing : RT_CLOSE_PAREN))
17771 cp_parser_skip_to_closing_parenthesis (parser, true, false,
17772 /*consume_paren=*/true);
17773 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17774
17775 if (!invalid_inputs_p && !invalid_outputs_p)
17776 {
17777 /* Create the ASM_EXPR. */
17778 if (parser->in_function_body)
17779 {
17780 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
17781 inputs, clobbers, labels);
17782 /* If the extended syntax was not used, mark the ASM_EXPR. */
17783 if (!extended_p)
17784 {
17785 tree temp = asm_stmt;
17786 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
17787 temp = TREE_OPERAND (temp, 0);
17788
17789 ASM_INPUT_P (temp) = 1;
17790 }
17791 }
17792 else
17793 symtab->finalize_toplevel_asm (string);
17794 }
17795 }
17796
17797 /* Declarators [gram.dcl.decl] */
17798
17799 /* Parse an init-declarator.
17800
17801 init-declarator:
17802 declarator initializer [opt]
17803
17804 GNU Extension:
17805
17806 init-declarator:
17807 declarator asm-specification [opt] attributes [opt] initializer [opt]
17808
17809 function-definition:
17810 decl-specifier-seq [opt] declarator ctor-initializer [opt]
17811 function-body
17812 decl-specifier-seq [opt] declarator function-try-block
17813
17814 GNU Extension:
17815
17816 function-definition:
17817 __extension__ function-definition
17818
17819 TM Extension:
17820
17821 function-definition:
17822 decl-specifier-seq [opt] declarator function-transaction-block
17823
17824 The DECL_SPECIFIERS apply to this declarator. Returns a
17825 representation of the entity declared. If MEMBER_P is TRUE, then
17826 this declarator appears in a class scope. The new DECL created by
17827 this declarator is returned.
17828
17829 The CHECKS are access checks that should be performed once we know
17830 what entity is being declared (and, therefore, what classes have
17831 befriended it).
17832
17833 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
17834 for a function-definition here as well. If the declarator is a
17835 declarator for a function-definition, *FUNCTION_DEFINITION_P will
17836 be TRUE upon return. By that point, the function-definition will
17837 have been completely parsed.
17838
17839 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
17840 is FALSE.
17841
17842 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
17843 parsed declaration if it is an uninitialized single declarator not followed
17844 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
17845 if present, will not be consumed. If returned, this declarator will be
17846 created with SD_INITIALIZED but will not call cp_finish_decl.
17847
17848 FIRST indicates if this is the first declarator in a declaration sequence.
17849
17850 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
17851 and there is an initializer, the pointed location_t is set to the
17852 location of the '=' or `(', or '{' in C++11 token introducing the
17853 initializer. */
17854
17855 static tree
17856 cp_parser_init_declarator (cp_parser* parser,
17857 cp_decl_specifier_seq *decl_specifiers,
17858 vec<deferred_access_check, va_gc> *checks,
17859 bool function_definition_allowed_p,
17860 bool member_p,
17861 int declares_class_or_enum,
17862 bool* function_definition_p,
17863 tree* maybe_range_for_decl,
17864 bool first,
17865 location_t* init_loc)
17866 {
17867 cp_token *token = NULL, *asm_spec_start_token = NULL,
17868 *attributes_start_token = NULL;
17869 cp_declarator *declarator;
17870 tree prefix_attributes;
17871 tree attributes = NULL;
17872 tree asm_specification;
17873 tree initializer;
17874 tree decl = NULL_TREE;
17875 tree scope;
17876 int is_initialized;
17877 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
17878 initialized with "= ..", CPP_OPEN_PAREN if initialized with
17879 "(...)". */
17880 enum cpp_ttype initialization_kind;
17881 bool is_direct_init = false;
17882 bool is_non_constant_init;
17883 int ctor_dtor_or_conv_p;
17884 bool friend_p = cp_parser_friend_p (decl_specifiers);
17885 tree pushed_scope = NULL_TREE;
17886 bool range_for_decl_p = false;
17887 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17888 location_t tmp_init_loc = UNKNOWN_LOCATION;
17889
17890 /* Gather the attributes that were provided with the
17891 decl-specifiers. */
17892 prefix_attributes = decl_specifiers->attributes;
17893
17894 /* Assume that this is not the declarator for a function
17895 definition. */
17896 if (function_definition_p)
17897 *function_definition_p = false;
17898
17899 /* Default arguments are only permitted for function parameters. */
17900 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
17901 parser->default_arg_ok_p = false;
17902
17903 /* Defer access checks while parsing the declarator; we cannot know
17904 what names are accessible until we know what is being
17905 declared. */
17906 resume_deferring_access_checks ();
17907
17908 /* Parse the declarator. */
17909 token = cp_lexer_peek_token (parser->lexer);
17910 declarator
17911 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17912 &ctor_dtor_or_conv_p,
17913 /*parenthesized_p=*/NULL,
17914 member_p, friend_p);
17915 /* Gather up the deferred checks. */
17916 stop_deferring_access_checks ();
17917
17918 parser->default_arg_ok_p = saved_default_arg_ok_p;
17919
17920 /* If the DECLARATOR was erroneous, there's no need to go
17921 further. */
17922 if (declarator == cp_error_declarator)
17923 return error_mark_node;
17924
17925 /* Check that the number of template-parameter-lists is OK. */
17926 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
17927 token->location))
17928 return error_mark_node;
17929
17930 if (declares_class_or_enum & 2)
17931 cp_parser_check_for_definition_in_return_type (declarator,
17932 decl_specifiers->type,
17933 decl_specifiers->locations[ds_type_spec]);
17934
17935 /* Figure out what scope the entity declared by the DECLARATOR is
17936 located in. `grokdeclarator' sometimes changes the scope, so
17937 we compute it now. */
17938 scope = get_scope_of_declarator (declarator);
17939
17940 /* Perform any lookups in the declared type which were thought to be
17941 dependent, but are not in the scope of the declarator. */
17942 decl_specifiers->type
17943 = maybe_update_decl_type (decl_specifiers->type, scope);
17944
17945 /* If we're allowing GNU extensions, look for an
17946 asm-specification. */
17947 if (cp_parser_allow_gnu_extensions_p (parser))
17948 {
17949 /* Look for an asm-specification. */
17950 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17951 asm_specification = cp_parser_asm_specification_opt (parser);
17952 }
17953 else
17954 asm_specification = NULL_TREE;
17955
17956 /* Look for attributes. */
17957 attributes_start_token = cp_lexer_peek_token (parser->lexer);
17958 attributes = cp_parser_attributes_opt (parser);
17959
17960 /* Peek at the next token. */
17961 token = cp_lexer_peek_token (parser->lexer);
17962
17963 bool bogus_implicit_tmpl = false;
17964
17965 if (function_declarator_p (declarator))
17966 {
17967 /* Check to see if the token indicates the start of a
17968 function-definition. */
17969 if (cp_parser_token_starts_function_definition_p (token))
17970 {
17971 if (!function_definition_allowed_p)
17972 {
17973 /* If a function-definition should not appear here, issue an
17974 error message. */
17975 cp_parser_error (parser,
17976 "a function-definition is not allowed here");
17977 return error_mark_node;
17978 }
17979
17980 location_t func_brace_location
17981 = cp_lexer_peek_token (parser->lexer)->location;
17982
17983 /* Neither attributes nor an asm-specification are allowed
17984 on a function-definition. */
17985 if (asm_specification)
17986 error_at (asm_spec_start_token->location,
17987 "an asm-specification is not allowed "
17988 "on a function-definition");
17989 if (attributes)
17990 error_at (attributes_start_token->location,
17991 "attributes are not allowed "
17992 "on a function-definition");
17993 /* This is a function-definition. */
17994 *function_definition_p = true;
17995
17996 /* Parse the function definition. */
17997 if (member_p)
17998 decl = cp_parser_save_member_function_body (parser,
17999 decl_specifiers,
18000 declarator,
18001 prefix_attributes,
18002 true);
18003 else
18004 decl =
18005 (cp_parser_function_definition_from_specifiers_and_declarator
18006 (parser, decl_specifiers, prefix_attributes, declarator));
18007
18008 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
18009 {
18010 /* This is where the prologue starts... */
18011 DECL_STRUCT_FUNCTION (decl)->function_start_locus
18012 = func_brace_location;
18013 }
18014
18015 return decl;
18016 }
18017 }
18018 else if (parser->fully_implicit_function_template_p)
18019 {
18020 /* A non-template declaration involving a function parameter list
18021 containing an implicit template parameter will be made into a
18022 template. If the resulting declaration is not going to be an
18023 actual function then finish the template scope here to prevent it.
18024 An error message will be issued once we have a decl to talk about.
18025
18026 FIXME probably we should do type deduction rather than create an
18027 implicit template, but the standard currently doesn't allow it. */
18028 bogus_implicit_tmpl = true;
18029 finish_fully_implicit_template (parser, NULL_TREE);
18030 }
18031
18032 /* [dcl.dcl]
18033
18034 Only in function declarations for constructors, destructors, and
18035 type conversions can the decl-specifier-seq be omitted.
18036
18037 We explicitly postpone this check past the point where we handle
18038 function-definitions because we tolerate function-definitions
18039 that are missing their return types in some modes. */
18040 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
18041 {
18042 cp_parser_error (parser,
18043 "expected constructor, destructor, or type conversion");
18044 return error_mark_node;
18045 }
18046
18047 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
18048 if (token->type == CPP_EQ
18049 || token->type == CPP_OPEN_PAREN
18050 || token->type == CPP_OPEN_BRACE)
18051 {
18052 is_initialized = SD_INITIALIZED;
18053 initialization_kind = token->type;
18054 if (maybe_range_for_decl)
18055 *maybe_range_for_decl = error_mark_node;
18056 tmp_init_loc = token->location;
18057 if (init_loc && *init_loc == UNKNOWN_LOCATION)
18058 *init_loc = tmp_init_loc;
18059
18060 if (token->type == CPP_EQ
18061 && function_declarator_p (declarator))
18062 {
18063 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
18064 if (t2->keyword == RID_DEFAULT)
18065 is_initialized = SD_DEFAULTED;
18066 else if (t2->keyword == RID_DELETE)
18067 is_initialized = SD_DELETED;
18068 }
18069 }
18070 else
18071 {
18072 /* If the init-declarator isn't initialized and isn't followed by a
18073 `,' or `;', it's not a valid init-declarator. */
18074 if (token->type != CPP_COMMA
18075 && token->type != CPP_SEMICOLON)
18076 {
18077 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
18078 range_for_decl_p = true;
18079 else
18080 {
18081 if (!maybe_range_for_decl)
18082 cp_parser_error (parser, "expected initializer");
18083 return error_mark_node;
18084 }
18085 }
18086 is_initialized = SD_UNINITIALIZED;
18087 initialization_kind = CPP_EOF;
18088 }
18089
18090 /* Because start_decl has side-effects, we should only call it if we
18091 know we're going ahead. By this point, we know that we cannot
18092 possibly be looking at any other construct. */
18093 cp_parser_commit_to_tentative_parse (parser);
18094
18095 /* Enter the newly declared entry in the symbol table. If we're
18096 processing a declaration in a class-specifier, we wait until
18097 after processing the initializer. */
18098 if (!member_p)
18099 {
18100 if (parser->in_unbraced_linkage_specification_p)
18101 decl_specifiers->storage_class = sc_extern;
18102 decl = start_decl (declarator, decl_specifiers,
18103 range_for_decl_p? SD_INITIALIZED : is_initialized,
18104 attributes, prefix_attributes, &pushed_scope);
18105 cp_finalize_omp_declare_simd (parser, decl);
18106 cp_finalize_oacc_routine (parser, decl, false, first);
18107 /* Adjust location of decl if declarator->id_loc is more appropriate:
18108 set, and decl wasn't merged with another decl, in which case its
18109 location would be different from input_location, and more accurate. */
18110 if (DECL_P (decl)
18111 && declarator->id_loc != UNKNOWN_LOCATION
18112 && DECL_SOURCE_LOCATION (decl) == input_location)
18113 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
18114 }
18115 else if (scope)
18116 /* Enter the SCOPE. That way unqualified names appearing in the
18117 initializer will be looked up in SCOPE. */
18118 pushed_scope = push_scope (scope);
18119
18120 /* Perform deferred access control checks, now that we know in which
18121 SCOPE the declared entity resides. */
18122 if (!member_p && decl)
18123 {
18124 tree saved_current_function_decl = NULL_TREE;
18125
18126 /* If the entity being declared is a function, pretend that we
18127 are in its scope. If it is a `friend', it may have access to
18128 things that would not otherwise be accessible. */
18129 if (TREE_CODE (decl) == FUNCTION_DECL)
18130 {
18131 saved_current_function_decl = current_function_decl;
18132 current_function_decl = decl;
18133 }
18134
18135 /* Perform access checks for template parameters. */
18136 cp_parser_perform_template_parameter_access_checks (checks);
18137
18138 /* Perform the access control checks for the declarator and the
18139 decl-specifiers. */
18140 perform_deferred_access_checks (tf_warning_or_error);
18141
18142 /* Restore the saved value. */
18143 if (TREE_CODE (decl) == FUNCTION_DECL)
18144 current_function_decl = saved_current_function_decl;
18145 }
18146
18147 /* Parse the initializer. */
18148 initializer = NULL_TREE;
18149 is_direct_init = false;
18150 is_non_constant_init = true;
18151 if (is_initialized)
18152 {
18153 if (function_declarator_p (declarator))
18154 {
18155 if (initialization_kind == CPP_EQ)
18156 initializer = cp_parser_pure_specifier (parser);
18157 else
18158 {
18159 /* If the declaration was erroneous, we don't really
18160 know what the user intended, so just silently
18161 consume the initializer. */
18162 if (decl != error_mark_node)
18163 error_at (tmp_init_loc, "initializer provided for function");
18164 cp_parser_skip_to_closing_parenthesis (parser,
18165 /*recovering=*/true,
18166 /*or_comma=*/false,
18167 /*consume_paren=*/true);
18168 }
18169 }
18170 else
18171 {
18172 /* We want to record the extra mangling scope for in-class
18173 initializers of class members and initializers of static data
18174 member templates. The former involves deferring
18175 parsing of the initializer until end of class as with default
18176 arguments. So right here we only handle the latter. */
18177 if (!member_p && processing_template_decl)
18178 start_lambda_scope (decl);
18179 initializer = cp_parser_initializer (parser,
18180 &is_direct_init,
18181 &is_non_constant_init);
18182 if (!member_p && processing_template_decl)
18183 finish_lambda_scope ();
18184 if (initializer == error_mark_node)
18185 cp_parser_skip_to_end_of_statement (parser);
18186 }
18187 }
18188
18189 /* The old parser allows attributes to appear after a parenthesized
18190 initializer. Mark Mitchell proposed removing this functionality
18191 on the GCC mailing lists on 2002-08-13. This parser accepts the
18192 attributes -- but ignores them. */
18193 if (cp_parser_allow_gnu_extensions_p (parser)
18194 && initialization_kind == CPP_OPEN_PAREN)
18195 if (cp_parser_attributes_opt (parser))
18196 warning (OPT_Wattributes,
18197 "attributes after parenthesized initializer ignored");
18198
18199 /* And now complain about a non-function implicit template. */
18200 if (bogus_implicit_tmpl && decl != error_mark_node)
18201 error_at (DECL_SOURCE_LOCATION (decl),
18202 "non-function %qD declared as implicit template", decl);
18203
18204 /* For an in-class declaration, use `grokfield' to create the
18205 declaration. */
18206 if (member_p)
18207 {
18208 if (pushed_scope)
18209 {
18210 pop_scope (pushed_scope);
18211 pushed_scope = NULL_TREE;
18212 }
18213 decl = grokfield (declarator, decl_specifiers,
18214 initializer, !is_non_constant_init,
18215 /*asmspec=*/NULL_TREE,
18216 chainon (attributes, prefix_attributes));
18217 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
18218 cp_parser_save_default_args (parser, decl);
18219 cp_finalize_omp_declare_simd (parser, decl);
18220 cp_finalize_oacc_routine (parser, decl, false, first);
18221 }
18222
18223 /* Finish processing the declaration. But, skip member
18224 declarations. */
18225 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
18226 {
18227 cp_finish_decl (decl,
18228 initializer, !is_non_constant_init,
18229 asm_specification,
18230 /* If the initializer is in parentheses, then this is
18231 a direct-initialization, which means that an
18232 `explicit' constructor is OK. Otherwise, an
18233 `explicit' constructor cannot be used. */
18234 ((is_direct_init || !is_initialized)
18235 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
18236 }
18237 else if ((cxx_dialect != cxx98) && friend_p
18238 && decl && TREE_CODE (decl) == FUNCTION_DECL)
18239 /* Core issue #226 (C++0x only): A default template-argument
18240 shall not be specified in a friend class template
18241 declaration. */
18242 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
18243 /*is_partial=*/false, /*is_friend_decl=*/1);
18244
18245 if (!friend_p && pushed_scope)
18246 pop_scope (pushed_scope);
18247
18248 if (function_declarator_p (declarator)
18249 && parser->fully_implicit_function_template_p)
18250 {
18251 if (member_p)
18252 decl = finish_fully_implicit_template (parser, decl);
18253 else
18254 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
18255 }
18256
18257 return decl;
18258 }
18259
18260 /* Parse a declarator.
18261
18262 declarator:
18263 direct-declarator
18264 ptr-operator declarator
18265
18266 abstract-declarator:
18267 ptr-operator abstract-declarator [opt]
18268 direct-abstract-declarator
18269
18270 GNU Extensions:
18271
18272 declarator:
18273 attributes [opt] direct-declarator
18274 attributes [opt] ptr-operator declarator
18275
18276 abstract-declarator:
18277 attributes [opt] ptr-operator abstract-declarator [opt]
18278 attributes [opt] direct-abstract-declarator
18279
18280 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18281 detect constructor, destructor or conversion operators. It is set
18282 to -1 if the declarator is a name, and +1 if it is a
18283 function. Otherwise it is set to zero. Usually you just want to
18284 test for >0, but internally the negative value is used.
18285
18286 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18287 a decl-specifier-seq unless it declares a constructor, destructor,
18288 or conversion. It might seem that we could check this condition in
18289 semantic analysis, rather than parsing, but that makes it difficult
18290 to handle something like `f()'. We want to notice that there are
18291 no decl-specifiers, and therefore realize that this is an
18292 expression, not a declaration.)
18293
18294 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18295 the declarator is a direct-declarator of the form "(...)".
18296
18297 MEMBER_P is true iff this declarator is a member-declarator.
18298
18299 FRIEND_P is true iff this declarator is a friend. */
18300
18301 static cp_declarator *
18302 cp_parser_declarator (cp_parser* parser,
18303 cp_parser_declarator_kind dcl_kind,
18304 int* ctor_dtor_or_conv_p,
18305 bool* parenthesized_p,
18306 bool member_p, bool friend_p)
18307 {
18308 cp_declarator *declarator;
18309 enum tree_code code;
18310 cp_cv_quals cv_quals;
18311 tree class_type;
18312 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
18313
18314 /* Assume this is not a constructor, destructor, or type-conversion
18315 operator. */
18316 if (ctor_dtor_or_conv_p)
18317 *ctor_dtor_or_conv_p = 0;
18318
18319 if (cp_parser_allow_gnu_extensions_p (parser))
18320 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
18321
18322 /* Check for the ptr-operator production. */
18323 cp_parser_parse_tentatively (parser);
18324 /* Parse the ptr-operator. */
18325 code = cp_parser_ptr_operator (parser,
18326 &class_type,
18327 &cv_quals,
18328 &std_attributes);
18329
18330 /* If that worked, then we have a ptr-operator. */
18331 if (cp_parser_parse_definitely (parser))
18332 {
18333 /* If a ptr-operator was found, then this declarator was not
18334 parenthesized. */
18335 if (parenthesized_p)
18336 *parenthesized_p = true;
18337 /* The dependent declarator is optional if we are parsing an
18338 abstract-declarator. */
18339 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18340 cp_parser_parse_tentatively (parser);
18341
18342 /* Parse the dependent declarator. */
18343 declarator = cp_parser_declarator (parser, dcl_kind,
18344 /*ctor_dtor_or_conv_p=*/NULL,
18345 /*parenthesized_p=*/NULL,
18346 /*member_p=*/false,
18347 friend_p);
18348
18349 /* If we are parsing an abstract-declarator, we must handle the
18350 case where the dependent declarator is absent. */
18351 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
18352 && !cp_parser_parse_definitely (parser))
18353 declarator = NULL;
18354
18355 declarator = cp_parser_make_indirect_declarator
18356 (code, class_type, cv_quals, declarator, std_attributes);
18357 }
18358 /* Everything else is a direct-declarator. */
18359 else
18360 {
18361 if (parenthesized_p)
18362 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
18363 CPP_OPEN_PAREN);
18364 declarator = cp_parser_direct_declarator (parser, dcl_kind,
18365 ctor_dtor_or_conv_p,
18366 member_p, friend_p);
18367 }
18368
18369 if (gnu_attributes && declarator && declarator != cp_error_declarator)
18370 declarator->attributes = gnu_attributes;
18371 return declarator;
18372 }
18373
18374 /* Parse a direct-declarator or direct-abstract-declarator.
18375
18376 direct-declarator:
18377 declarator-id
18378 direct-declarator ( parameter-declaration-clause )
18379 cv-qualifier-seq [opt]
18380 ref-qualifier [opt]
18381 exception-specification [opt]
18382 direct-declarator [ constant-expression [opt] ]
18383 ( declarator )
18384
18385 direct-abstract-declarator:
18386 direct-abstract-declarator [opt]
18387 ( parameter-declaration-clause )
18388 cv-qualifier-seq [opt]
18389 ref-qualifier [opt]
18390 exception-specification [opt]
18391 direct-abstract-declarator [opt] [ constant-expression [opt] ]
18392 ( abstract-declarator )
18393
18394 Returns a representation of the declarator. DCL_KIND is
18395 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18396 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
18397 we are parsing a direct-declarator. It is
18398 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18399 of ambiguity we prefer an abstract declarator, as per
18400 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18401 as for cp_parser_declarator. */
18402
18403 static cp_declarator *
18404 cp_parser_direct_declarator (cp_parser* parser,
18405 cp_parser_declarator_kind dcl_kind,
18406 int* ctor_dtor_or_conv_p,
18407 bool member_p, bool friend_p)
18408 {
18409 cp_token *token;
18410 cp_declarator *declarator = NULL;
18411 tree scope = NULL_TREE;
18412 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18413 bool saved_in_declarator_p = parser->in_declarator_p;
18414 bool first = true;
18415 tree pushed_scope = NULL_TREE;
18416
18417 while (true)
18418 {
18419 /* Peek at the next token. */
18420 token = cp_lexer_peek_token (parser->lexer);
18421 if (token->type == CPP_OPEN_PAREN)
18422 {
18423 /* This is either a parameter-declaration-clause, or a
18424 parenthesized declarator. When we know we are parsing a
18425 named declarator, it must be a parenthesized declarator
18426 if FIRST is true. For instance, `(int)' is a
18427 parameter-declaration-clause, with an omitted
18428 direct-abstract-declarator. But `((*))', is a
18429 parenthesized abstract declarator. Finally, when T is a
18430 template parameter `(T)' is a
18431 parameter-declaration-clause, and not a parenthesized
18432 named declarator.
18433
18434 We first try and parse a parameter-declaration-clause,
18435 and then try a nested declarator (if FIRST is true).
18436
18437 It is not an error for it not to be a
18438 parameter-declaration-clause, even when FIRST is
18439 false. Consider,
18440
18441 int i (int);
18442 int i (3);
18443
18444 The first is the declaration of a function while the
18445 second is the definition of a variable, including its
18446 initializer.
18447
18448 Having seen only the parenthesis, we cannot know which of
18449 these two alternatives should be selected. Even more
18450 complex are examples like:
18451
18452 int i (int (a));
18453 int i (int (3));
18454
18455 The former is a function-declaration; the latter is a
18456 variable initialization.
18457
18458 Thus again, we try a parameter-declaration-clause, and if
18459 that fails, we back out and return. */
18460
18461 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18462 {
18463 tree params;
18464 bool is_declarator = false;
18465
18466 /* In a member-declarator, the only valid interpretation
18467 of a parenthesis is the start of a
18468 parameter-declaration-clause. (It is invalid to
18469 initialize a static data member with a parenthesized
18470 initializer; only the "=" form of initialization is
18471 permitted.) */
18472 if (!member_p)
18473 cp_parser_parse_tentatively (parser);
18474
18475 /* Consume the `('. */
18476 cp_lexer_consume_token (parser->lexer);
18477 if (first)
18478 {
18479 /* If this is going to be an abstract declarator, we're
18480 in a declarator and we can't have default args. */
18481 parser->default_arg_ok_p = false;
18482 parser->in_declarator_p = true;
18483 }
18484
18485 begin_scope (sk_function_parms, NULL_TREE);
18486
18487 /* Parse the parameter-declaration-clause. */
18488 params = cp_parser_parameter_declaration_clause (parser);
18489
18490 /* Consume the `)'. */
18491 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18492
18493 /* If all went well, parse the cv-qualifier-seq,
18494 ref-qualifier and the exception-specification. */
18495 if (member_p || cp_parser_parse_definitely (parser))
18496 {
18497 cp_cv_quals cv_quals;
18498 cp_virt_specifiers virt_specifiers;
18499 cp_ref_qualifier ref_qual;
18500 tree exception_specification;
18501 tree late_return;
18502 tree attrs;
18503 bool memfn = (member_p || (pushed_scope
18504 && CLASS_TYPE_P (pushed_scope)));
18505
18506 is_declarator = true;
18507
18508 if (ctor_dtor_or_conv_p)
18509 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
18510 first = false;
18511
18512 /* Parse the cv-qualifier-seq. */
18513 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18514 /* Parse the ref-qualifier. */
18515 ref_qual = cp_parser_ref_qualifier_opt (parser);
18516 /* Parse the tx-qualifier. */
18517 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
18518 /* And the exception-specification. */
18519 exception_specification
18520 = cp_parser_exception_specification_opt (parser);
18521
18522 attrs = cp_parser_std_attribute_spec_seq (parser);
18523
18524 /* In here, we handle cases where attribute is used after
18525 the function declaration. For example:
18526 void func (int x) __attribute__((vector(..))); */
18527 if (flag_cilkplus
18528 && cp_next_tokens_can_be_gnu_attribute_p (parser))
18529 {
18530 cp_parser_parse_tentatively (parser);
18531 tree attr = cp_parser_gnu_attributes_opt (parser);
18532 if (cp_lexer_next_token_is_not (parser->lexer,
18533 CPP_SEMICOLON)
18534 && cp_lexer_next_token_is_not (parser->lexer,
18535 CPP_OPEN_BRACE))
18536 cp_parser_abort_tentative_parse (parser);
18537 else if (!cp_parser_parse_definitely (parser))
18538 ;
18539 else
18540 attrs = chainon (attr, attrs);
18541 }
18542 tree requires_clause = NULL_TREE;
18543 late_return = (cp_parser_late_return_type_opt
18544 (parser, declarator, requires_clause,
18545 memfn ? cv_quals : -1));
18546
18547 /* Parse the virt-specifier-seq. */
18548 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18549
18550 /* Create the function-declarator. */
18551 declarator = make_call_declarator (declarator,
18552 params,
18553 cv_quals,
18554 virt_specifiers,
18555 ref_qual,
18556 tx_qual,
18557 exception_specification,
18558 late_return,
18559 requires_clause);
18560 declarator->std_attributes = attrs;
18561 /* Any subsequent parameter lists are to do with
18562 return type, so are not those of the declared
18563 function. */
18564 parser->default_arg_ok_p = false;
18565 }
18566
18567 /* Remove the function parms from scope. */
18568 pop_bindings_and_leave_scope ();
18569
18570 if (is_declarator)
18571 /* Repeat the main loop. */
18572 continue;
18573 }
18574
18575 /* If this is the first, we can try a parenthesized
18576 declarator. */
18577 if (first)
18578 {
18579 bool saved_in_type_id_in_expr_p;
18580
18581 parser->default_arg_ok_p = saved_default_arg_ok_p;
18582 parser->in_declarator_p = saved_in_declarator_p;
18583
18584 /* Consume the `('. */
18585 cp_lexer_consume_token (parser->lexer);
18586 /* Parse the nested declarator. */
18587 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18588 parser->in_type_id_in_expr_p = true;
18589 declarator
18590 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
18591 /*parenthesized_p=*/NULL,
18592 member_p, friend_p);
18593 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18594 first = false;
18595 /* Expect a `)'. */
18596 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
18597 declarator = cp_error_declarator;
18598 if (declarator == cp_error_declarator)
18599 break;
18600
18601 goto handle_declarator;
18602 }
18603 /* Otherwise, we must be done. */
18604 else
18605 break;
18606 }
18607 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18608 && token->type == CPP_OPEN_SQUARE
18609 && !cp_next_tokens_can_be_attribute_p (parser))
18610 {
18611 /* Parse an array-declarator. */
18612 tree bounds, attrs;
18613
18614 if (ctor_dtor_or_conv_p)
18615 *ctor_dtor_or_conv_p = 0;
18616
18617 first = false;
18618 parser->default_arg_ok_p = false;
18619 parser->in_declarator_p = true;
18620 /* Consume the `['. */
18621 cp_lexer_consume_token (parser->lexer);
18622 /* Peek at the next token. */
18623 token = cp_lexer_peek_token (parser->lexer);
18624 /* If the next token is `]', then there is no
18625 constant-expression. */
18626 if (token->type != CPP_CLOSE_SQUARE)
18627 {
18628 bool non_constant_p;
18629 bounds
18630 = cp_parser_constant_expression (parser,
18631 /*allow_non_constant=*/true,
18632 &non_constant_p);
18633 if (!non_constant_p)
18634 /* OK */;
18635 else if (error_operand_p (bounds))
18636 /* Already gave an error. */;
18637 else if (!parser->in_function_body
18638 || current_binding_level->kind == sk_function_parms)
18639 {
18640 /* Normally, the array bound must be an integral constant
18641 expression. However, as an extension, we allow VLAs
18642 in function scopes as long as they aren't part of a
18643 parameter declaration. */
18644 cp_parser_error (parser,
18645 "array bound is not an integer constant");
18646 bounds = error_mark_node;
18647 }
18648 else if (processing_template_decl
18649 && !type_dependent_expression_p (bounds))
18650 {
18651 /* Remember this wasn't a constant-expression. */
18652 bounds = build_nop (TREE_TYPE (bounds), bounds);
18653 TREE_SIDE_EFFECTS (bounds) = 1;
18654 }
18655 }
18656 else
18657 bounds = NULL_TREE;
18658 /* Look for the closing `]'. */
18659 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
18660 {
18661 declarator = cp_error_declarator;
18662 break;
18663 }
18664
18665 attrs = cp_parser_std_attribute_spec_seq (parser);
18666 declarator = make_array_declarator (declarator, bounds);
18667 declarator->std_attributes = attrs;
18668 }
18669 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
18670 {
18671 {
18672 tree qualifying_scope;
18673 tree unqualified_name;
18674 tree attrs;
18675 special_function_kind sfk;
18676 bool abstract_ok;
18677 bool pack_expansion_p = false;
18678 cp_token *declarator_id_start_token;
18679
18680 /* Parse a declarator-id */
18681 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
18682 if (abstract_ok)
18683 {
18684 cp_parser_parse_tentatively (parser);
18685
18686 /* If we see an ellipsis, we should be looking at a
18687 parameter pack. */
18688 if (token->type == CPP_ELLIPSIS)
18689 {
18690 /* Consume the `...' */
18691 cp_lexer_consume_token (parser->lexer);
18692
18693 pack_expansion_p = true;
18694 }
18695 }
18696
18697 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
18698 unqualified_name
18699 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
18700 qualifying_scope = parser->scope;
18701 if (abstract_ok)
18702 {
18703 bool okay = false;
18704
18705 if (!unqualified_name && pack_expansion_p)
18706 {
18707 /* Check whether an error occurred. */
18708 okay = !cp_parser_error_occurred (parser);
18709
18710 /* We already consumed the ellipsis to mark a
18711 parameter pack, but we have no way to report it,
18712 so abort the tentative parse. We will be exiting
18713 immediately anyway. */
18714 cp_parser_abort_tentative_parse (parser);
18715 }
18716 else
18717 okay = cp_parser_parse_definitely (parser);
18718
18719 if (!okay)
18720 unqualified_name = error_mark_node;
18721 else if (unqualified_name
18722 && (qualifying_scope
18723 || (!identifier_p (unqualified_name))))
18724 {
18725 cp_parser_error (parser, "expected unqualified-id");
18726 unqualified_name = error_mark_node;
18727 }
18728 }
18729
18730 if (!unqualified_name)
18731 return NULL;
18732 if (unqualified_name == error_mark_node)
18733 {
18734 declarator = cp_error_declarator;
18735 pack_expansion_p = false;
18736 declarator->parameter_pack_p = false;
18737 break;
18738 }
18739
18740 attrs = cp_parser_std_attribute_spec_seq (parser);
18741
18742 if (qualifying_scope && at_namespace_scope_p ()
18743 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
18744 {
18745 /* In the declaration of a member of a template class
18746 outside of the class itself, the SCOPE will sometimes
18747 be a TYPENAME_TYPE. For example, given:
18748
18749 template <typename T>
18750 int S<T>::R::i = 3;
18751
18752 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
18753 this context, we must resolve S<T>::R to an ordinary
18754 type, rather than a typename type.
18755
18756 The reason we normally avoid resolving TYPENAME_TYPEs
18757 is that a specialization of `S' might render
18758 `S<T>::R' not a type. However, if `S' is
18759 specialized, then this `i' will not be used, so there
18760 is no harm in resolving the types here. */
18761 tree type;
18762
18763 /* Resolve the TYPENAME_TYPE. */
18764 type = resolve_typename_type (qualifying_scope,
18765 /*only_current_p=*/false);
18766 /* If that failed, the declarator is invalid. */
18767 if (TREE_CODE (type) == TYPENAME_TYPE)
18768 {
18769 if (typedef_variant_p (type))
18770 error_at (declarator_id_start_token->location,
18771 "cannot define member of dependent typedef "
18772 "%qT", type);
18773 else
18774 error_at (declarator_id_start_token->location,
18775 "%<%T::%E%> is not a type",
18776 TYPE_CONTEXT (qualifying_scope),
18777 TYPE_IDENTIFIER (qualifying_scope));
18778 }
18779 qualifying_scope = type;
18780 }
18781
18782 sfk = sfk_none;
18783
18784 if (unqualified_name)
18785 {
18786 tree class_type;
18787
18788 if (qualifying_scope
18789 && CLASS_TYPE_P (qualifying_scope))
18790 class_type = qualifying_scope;
18791 else
18792 class_type = current_class_type;
18793
18794 if (TREE_CODE (unqualified_name) == TYPE_DECL)
18795 {
18796 tree name_type = TREE_TYPE (unqualified_name);
18797 if (class_type && same_type_p (name_type, class_type))
18798 {
18799 if (qualifying_scope
18800 && CLASSTYPE_USE_TEMPLATE (name_type))
18801 {
18802 error_at (declarator_id_start_token->location,
18803 "invalid use of constructor as a template");
18804 inform (declarator_id_start_token->location,
18805 "use %<%T::%D%> instead of %<%T::%D%> to "
18806 "name the constructor in a qualified name",
18807 class_type,
18808 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
18809 class_type, name_type);
18810 declarator = cp_error_declarator;
18811 break;
18812 }
18813 else
18814 unqualified_name = constructor_name (class_type);
18815 }
18816 else
18817 {
18818 /* We do not attempt to print the declarator
18819 here because we do not have enough
18820 information about its original syntactic
18821 form. */
18822 cp_parser_error (parser, "invalid declarator");
18823 declarator = cp_error_declarator;
18824 break;
18825 }
18826 }
18827
18828 if (class_type)
18829 {
18830 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
18831 sfk = sfk_destructor;
18832 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
18833 sfk = sfk_conversion;
18834 else if (/* There's no way to declare a constructor
18835 for an anonymous type, even if the type
18836 got a name for linkage purposes. */
18837 !TYPE_WAS_ANONYMOUS (class_type)
18838 /* Handle correctly (c++/19200):
18839
18840 struct S {
18841 struct T{};
18842 friend void S(T);
18843 };
18844
18845 and also:
18846
18847 namespace N {
18848 void S();
18849 }
18850
18851 struct S {
18852 friend void N::S();
18853 }; */
18854 && !(friend_p
18855 && class_type != qualifying_scope)
18856 && constructor_name_p (unqualified_name,
18857 class_type))
18858 {
18859 unqualified_name = constructor_name (class_type);
18860 sfk = sfk_constructor;
18861 }
18862 else if (is_overloaded_fn (unqualified_name)
18863 && DECL_CONSTRUCTOR_P (get_first_fn
18864 (unqualified_name)))
18865 sfk = sfk_constructor;
18866
18867 if (ctor_dtor_or_conv_p && sfk != sfk_none)
18868 *ctor_dtor_or_conv_p = -1;
18869 }
18870 }
18871 declarator = make_id_declarator (qualifying_scope,
18872 unqualified_name,
18873 sfk);
18874 declarator->std_attributes = attrs;
18875 declarator->id_loc = token->location;
18876 declarator->parameter_pack_p = pack_expansion_p;
18877
18878 if (pack_expansion_p)
18879 maybe_warn_variadic_templates ();
18880 }
18881
18882 handle_declarator:;
18883 scope = get_scope_of_declarator (declarator);
18884 if (scope)
18885 {
18886 /* Any names that appear after the declarator-id for a
18887 member are looked up in the containing scope. */
18888 if (at_function_scope_p ())
18889 {
18890 /* But declarations with qualified-ids can't appear in a
18891 function. */
18892 cp_parser_error (parser, "qualified-id in declaration");
18893 declarator = cp_error_declarator;
18894 break;
18895 }
18896 pushed_scope = push_scope (scope);
18897 }
18898 parser->in_declarator_p = true;
18899 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
18900 || (declarator && declarator->kind == cdk_id))
18901 /* Default args are only allowed on function
18902 declarations. */
18903 parser->default_arg_ok_p = saved_default_arg_ok_p;
18904 else
18905 parser->default_arg_ok_p = false;
18906
18907 first = false;
18908 }
18909 /* We're done. */
18910 else
18911 break;
18912 }
18913
18914 /* For an abstract declarator, we might wind up with nothing at this
18915 point. That's an error; the declarator is not optional. */
18916 if (!declarator)
18917 cp_parser_error (parser, "expected declarator");
18918
18919 /* If we entered a scope, we must exit it now. */
18920 if (pushed_scope)
18921 pop_scope (pushed_scope);
18922
18923 parser->default_arg_ok_p = saved_default_arg_ok_p;
18924 parser->in_declarator_p = saved_in_declarator_p;
18925
18926 return declarator;
18927 }
18928
18929 /* Parse a ptr-operator.
18930
18931 ptr-operator:
18932 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18933 * cv-qualifier-seq [opt]
18934 &
18935 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
18936 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18937
18938 GNU Extension:
18939
18940 ptr-operator:
18941 & cv-qualifier-seq [opt]
18942
18943 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18944 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18945 an rvalue reference. In the case of a pointer-to-member, *TYPE is
18946 filled in with the TYPE containing the member. *CV_QUALS is
18947 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18948 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
18949 Note that the tree codes returned by this function have nothing
18950 to do with the types of trees that will be eventually be created
18951 to represent the pointer or reference type being parsed. They are
18952 just constants with suggestive names. */
18953 static enum tree_code
18954 cp_parser_ptr_operator (cp_parser* parser,
18955 tree* type,
18956 cp_cv_quals *cv_quals,
18957 tree *attributes)
18958 {
18959 enum tree_code code = ERROR_MARK;
18960 cp_token *token;
18961 tree attrs = NULL_TREE;
18962
18963 /* Assume that it's not a pointer-to-member. */
18964 *type = NULL_TREE;
18965 /* And that there are no cv-qualifiers. */
18966 *cv_quals = TYPE_UNQUALIFIED;
18967
18968 /* Peek at the next token. */
18969 token = cp_lexer_peek_token (parser->lexer);
18970
18971 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
18972 if (token->type == CPP_MULT)
18973 code = INDIRECT_REF;
18974 else if (token->type == CPP_AND)
18975 code = ADDR_EXPR;
18976 else if ((cxx_dialect != cxx98) &&
18977 token->type == CPP_AND_AND) /* C++0x only */
18978 code = NON_LVALUE_EXPR;
18979
18980 if (code != ERROR_MARK)
18981 {
18982 /* Consume the `*', `&' or `&&'. */
18983 cp_lexer_consume_token (parser->lexer);
18984
18985 /* A `*' can be followed by a cv-qualifier-seq, and so can a
18986 `&', if we are allowing GNU extensions. (The only qualifier
18987 that can legally appear after `&' is `restrict', but that is
18988 enforced during semantic analysis. */
18989 if (code == INDIRECT_REF
18990 || cp_parser_allow_gnu_extensions_p (parser))
18991 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18992
18993 attrs = cp_parser_std_attribute_spec_seq (parser);
18994 if (attributes != NULL)
18995 *attributes = attrs;
18996 }
18997 else
18998 {
18999 /* Try the pointer-to-member case. */
19000 cp_parser_parse_tentatively (parser);
19001 /* Look for the optional `::' operator. */
19002 cp_parser_global_scope_opt (parser,
19003 /*current_scope_valid_p=*/false);
19004 /* Look for the nested-name specifier. */
19005 token = cp_lexer_peek_token (parser->lexer);
19006 cp_parser_nested_name_specifier (parser,
19007 /*typename_keyword_p=*/false,
19008 /*check_dependency_p=*/true,
19009 /*type_p=*/false,
19010 /*is_declaration=*/false);
19011 /* If we found it, and the next token is a `*', then we are
19012 indeed looking at a pointer-to-member operator. */
19013 if (!cp_parser_error_occurred (parser)
19014 && cp_parser_require (parser, CPP_MULT, RT_MULT))
19015 {
19016 /* Indicate that the `*' operator was used. */
19017 code = INDIRECT_REF;
19018
19019 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
19020 error_at (token->location, "%qD is a namespace", parser->scope);
19021 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
19022 error_at (token->location, "cannot form pointer to member of "
19023 "non-class %q#T", parser->scope);
19024 else
19025 {
19026 /* The type of which the member is a member is given by the
19027 current SCOPE. */
19028 *type = parser->scope;
19029 /* The next name will not be qualified. */
19030 parser->scope = NULL_TREE;
19031 parser->qualifying_scope = NULL_TREE;
19032 parser->object_scope = NULL_TREE;
19033 /* Look for optional c++11 attributes. */
19034 attrs = cp_parser_std_attribute_spec_seq (parser);
19035 if (attributes != NULL)
19036 *attributes = attrs;
19037 /* Look for the optional cv-qualifier-seq. */
19038 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19039 }
19040 }
19041 /* If that didn't work we don't have a ptr-operator. */
19042 if (!cp_parser_parse_definitely (parser))
19043 cp_parser_error (parser, "expected ptr-operator");
19044 }
19045
19046 return code;
19047 }
19048
19049 /* Parse an (optional) cv-qualifier-seq.
19050
19051 cv-qualifier-seq:
19052 cv-qualifier cv-qualifier-seq [opt]
19053
19054 cv-qualifier:
19055 const
19056 volatile
19057
19058 GNU Extension:
19059
19060 cv-qualifier:
19061 __restrict__
19062
19063 Returns a bitmask representing the cv-qualifiers. */
19064
19065 static cp_cv_quals
19066 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
19067 {
19068 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
19069
19070 while (true)
19071 {
19072 cp_token *token;
19073 cp_cv_quals cv_qualifier;
19074
19075 /* Peek at the next token. */
19076 token = cp_lexer_peek_token (parser->lexer);
19077 /* See if it's a cv-qualifier. */
19078 switch (token->keyword)
19079 {
19080 case RID_CONST:
19081 cv_qualifier = TYPE_QUAL_CONST;
19082 break;
19083
19084 case RID_VOLATILE:
19085 cv_qualifier = TYPE_QUAL_VOLATILE;
19086 break;
19087
19088 case RID_RESTRICT:
19089 cv_qualifier = TYPE_QUAL_RESTRICT;
19090 break;
19091
19092 default:
19093 cv_qualifier = TYPE_UNQUALIFIED;
19094 break;
19095 }
19096
19097 if (!cv_qualifier)
19098 break;
19099
19100 if (cv_quals & cv_qualifier)
19101 {
19102 error_at (token->location, "duplicate cv-qualifier");
19103 cp_lexer_purge_token (parser->lexer);
19104 }
19105 else
19106 {
19107 cp_lexer_consume_token (parser->lexer);
19108 cv_quals |= cv_qualifier;
19109 }
19110 }
19111
19112 return cv_quals;
19113 }
19114
19115 /* Parse an (optional) ref-qualifier
19116
19117 ref-qualifier:
19118 &
19119 &&
19120
19121 Returns cp_ref_qualifier representing ref-qualifier. */
19122
19123 static cp_ref_qualifier
19124 cp_parser_ref_qualifier_opt (cp_parser* parser)
19125 {
19126 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
19127
19128 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
19129 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
19130 return ref_qual;
19131
19132 while (true)
19133 {
19134 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
19135 cp_token *token = cp_lexer_peek_token (parser->lexer);
19136
19137 switch (token->type)
19138 {
19139 case CPP_AND:
19140 curr_ref_qual = REF_QUAL_LVALUE;
19141 break;
19142
19143 case CPP_AND_AND:
19144 curr_ref_qual = REF_QUAL_RVALUE;
19145 break;
19146
19147 default:
19148 curr_ref_qual = REF_QUAL_NONE;
19149 break;
19150 }
19151
19152 if (!curr_ref_qual)
19153 break;
19154 else if (ref_qual)
19155 {
19156 error_at (token->location, "multiple ref-qualifiers");
19157 cp_lexer_purge_token (parser->lexer);
19158 }
19159 else
19160 {
19161 ref_qual = curr_ref_qual;
19162 cp_lexer_consume_token (parser->lexer);
19163 }
19164 }
19165
19166 return ref_qual;
19167 }
19168
19169 /* Parse an optional tx-qualifier.
19170
19171 tx-qualifier:
19172 transaction_safe
19173 transaction_safe_dynamic */
19174
19175 static tree
19176 cp_parser_tx_qualifier_opt (cp_parser *parser)
19177 {
19178 cp_token *token = cp_lexer_peek_token (parser->lexer);
19179 if (token->type == CPP_NAME)
19180 {
19181 tree name = token->u.value;
19182 const char *p = IDENTIFIER_POINTER (name);
19183 const int len = strlen ("transaction_safe");
19184 if (!strncmp (p, "transaction_safe", len))
19185 {
19186 p += len;
19187 if (*p == '\0'
19188 || !strcmp (p, "_dynamic"))
19189 {
19190 cp_lexer_consume_token (parser->lexer);
19191 if (!flag_tm)
19192 {
19193 error ("%E requires %<-fgnu-tm%>", name);
19194 return NULL_TREE;
19195 }
19196 else
19197 return name;
19198 }
19199 }
19200 }
19201 return NULL_TREE;
19202 }
19203
19204 /* Parse an (optional) virt-specifier-seq.
19205
19206 virt-specifier-seq:
19207 virt-specifier virt-specifier-seq [opt]
19208
19209 virt-specifier:
19210 override
19211 final
19212
19213 Returns a bitmask representing the virt-specifiers. */
19214
19215 static cp_virt_specifiers
19216 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
19217 {
19218 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19219
19220 while (true)
19221 {
19222 cp_token *token;
19223 cp_virt_specifiers virt_specifier;
19224
19225 /* Peek at the next token. */
19226 token = cp_lexer_peek_token (parser->lexer);
19227 /* See if it's a virt-specifier-qualifier. */
19228 if (token->type != CPP_NAME)
19229 break;
19230 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
19231 {
19232 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19233 virt_specifier = VIRT_SPEC_OVERRIDE;
19234 }
19235 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
19236 {
19237 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19238 virt_specifier = VIRT_SPEC_FINAL;
19239 }
19240 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
19241 {
19242 virt_specifier = VIRT_SPEC_FINAL;
19243 }
19244 else
19245 break;
19246
19247 if (virt_specifiers & virt_specifier)
19248 {
19249 error_at (token->location, "duplicate virt-specifier");
19250 cp_lexer_purge_token (parser->lexer);
19251 }
19252 else
19253 {
19254 cp_lexer_consume_token (parser->lexer);
19255 virt_specifiers |= virt_specifier;
19256 }
19257 }
19258 return virt_specifiers;
19259 }
19260
19261 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19262 is in scope even though it isn't real. */
19263
19264 void
19265 inject_this_parameter (tree ctype, cp_cv_quals quals)
19266 {
19267 tree this_parm;
19268
19269 if (current_class_ptr)
19270 {
19271 /* We don't clear this between NSDMIs. Is it already what we want? */
19272 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
19273 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
19274 && cp_type_quals (type) == quals)
19275 return;
19276 }
19277
19278 this_parm = build_this_parm (ctype, quals);
19279 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
19280 current_class_ptr = NULL_TREE;
19281 current_class_ref
19282 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
19283 current_class_ptr = this_parm;
19284 }
19285
19286 /* Return true iff our current scope is a non-static data member
19287 initializer. */
19288
19289 bool
19290 parsing_nsdmi (void)
19291 {
19292 /* We recognize NSDMI context by the context-less 'this' pointer set up
19293 by the function above. */
19294 if (current_class_ptr
19295 && TREE_CODE (current_class_ptr) == PARM_DECL
19296 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
19297 return true;
19298 return false;
19299 }
19300
19301 /* Parse a late-specified return type, if any. This is not a separate
19302 non-terminal, but part of a function declarator, which looks like
19303
19304 -> trailing-type-specifier-seq abstract-declarator(opt)
19305
19306 Returns the type indicated by the type-id.
19307
19308 In addition to this, parse any queued up omp declare simd
19309 clauses and Cilk Plus SIMD-enabled function's vector attributes.
19310
19311 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19312 function. */
19313
19314 static tree
19315 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
19316 tree& requires_clause, cp_cv_quals quals)
19317 {
19318 cp_token *token;
19319 tree type = NULL_TREE;
19320 bool declare_simd_p = (parser->omp_declare_simd
19321 && declarator
19322 && declarator->kind == cdk_id);
19323
19324 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
19325 && declarator && declarator->kind == cdk_id);
19326
19327 /* Peek at the next token. */
19328 token = cp_lexer_peek_token (parser->lexer);
19329 /* A late-specified return type is indicated by an initial '->'. */
19330 if (token->type != CPP_DEREF
19331 && token->keyword != RID_REQUIRES
19332 && !(declare_simd_p || cilk_simd_fn_vector_p))
19333 return NULL_TREE;
19334
19335 tree save_ccp = current_class_ptr;
19336 tree save_ccr = current_class_ref;
19337 if (quals >= 0)
19338 {
19339 /* DR 1207: 'this' is in scope in the trailing return type. */
19340 inject_this_parameter (current_class_type, quals);
19341 }
19342
19343 if (token->type == CPP_DEREF)
19344 {
19345 /* Consume the ->. */
19346 cp_lexer_consume_token (parser->lexer);
19347
19348 type = cp_parser_trailing_type_id (parser);
19349 }
19350
19351 /* Function declarations may be followed by a trailing
19352 requires-clause. */
19353 requires_clause = cp_parser_requires_clause_opt (parser);
19354
19355 if (cilk_simd_fn_vector_p)
19356 declarator->std_attributes
19357 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
19358 declarator->std_attributes);
19359 if (declare_simd_p)
19360 declarator->std_attributes
19361 = cp_parser_late_parsing_omp_declare_simd (parser,
19362 declarator->std_attributes);
19363
19364 if (quals >= 0)
19365 {
19366 current_class_ptr = save_ccp;
19367 current_class_ref = save_ccr;
19368 }
19369
19370 return type;
19371 }
19372
19373 /* Parse a declarator-id.
19374
19375 declarator-id:
19376 id-expression
19377 :: [opt] nested-name-specifier [opt] type-name
19378
19379 In the `id-expression' case, the value returned is as for
19380 cp_parser_id_expression if the id-expression was an unqualified-id.
19381 If the id-expression was a qualified-id, then a SCOPE_REF is
19382 returned. The first operand is the scope (either a NAMESPACE_DECL
19383 or TREE_TYPE), but the second is still just a representation of an
19384 unqualified-id. */
19385
19386 static tree
19387 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
19388 {
19389 tree id;
19390 /* The expression must be an id-expression. Assume that qualified
19391 names are the names of types so that:
19392
19393 template <class T>
19394 int S<T>::R::i = 3;
19395
19396 will work; we must treat `S<T>::R' as the name of a type.
19397 Similarly, assume that qualified names are templates, where
19398 required, so that:
19399
19400 template <class T>
19401 int S<T>::R<T>::i = 3;
19402
19403 will work, too. */
19404 id = cp_parser_id_expression (parser,
19405 /*template_keyword_p=*/false,
19406 /*check_dependency_p=*/false,
19407 /*template_p=*/NULL,
19408 /*declarator_p=*/true,
19409 optional_p);
19410 if (id && BASELINK_P (id))
19411 id = BASELINK_FUNCTIONS (id);
19412 return id;
19413 }
19414
19415 /* Parse a type-id.
19416
19417 type-id:
19418 type-specifier-seq abstract-declarator [opt]
19419
19420 Returns the TYPE specified. */
19421
19422 static tree
19423 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
19424 bool is_trailing_return)
19425 {
19426 cp_decl_specifier_seq type_specifier_seq;
19427 cp_declarator *abstract_declarator;
19428
19429 /* Parse the type-specifier-seq. */
19430 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
19431 is_trailing_return,
19432 &type_specifier_seq);
19433 if (type_specifier_seq.type == error_mark_node)
19434 return error_mark_node;
19435
19436 /* There might or might not be an abstract declarator. */
19437 cp_parser_parse_tentatively (parser);
19438 /* Look for the declarator. */
19439 abstract_declarator
19440 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
19441 /*parenthesized_p=*/NULL,
19442 /*member_p=*/false,
19443 /*friend_p=*/false);
19444 /* Check to see if there really was a declarator. */
19445 if (!cp_parser_parse_definitely (parser))
19446 abstract_declarator = NULL;
19447
19448 if (type_specifier_seq.type
19449 /* The concepts TS allows 'auto' as a type-id. */
19450 && !flag_concepts
19451 /* None of the valid uses of 'auto' in C++14 involve the type-id
19452 nonterminal, but it is valid in a trailing-return-type. */
19453 && !(cxx_dialect >= cxx14 && is_trailing_return)
19454 && type_uses_auto (type_specifier_seq.type))
19455 {
19456 /* A type-id with type 'auto' is only ok if the abstract declarator
19457 is a function declarator with a late-specified return type.
19458
19459 A type-id with 'auto' is also valid in a trailing-return-type
19460 in a compound-requirement. */
19461 if (abstract_declarator
19462 && abstract_declarator->kind == cdk_function
19463 && abstract_declarator->u.function.late_return_type)
19464 /* OK */;
19465 else if (parser->in_result_type_constraint_p)
19466 /* OK */;
19467 else
19468 {
19469 error ("invalid use of %<auto%>");
19470 return error_mark_node;
19471 }
19472 }
19473
19474 return groktypename (&type_specifier_seq, abstract_declarator,
19475 is_template_arg);
19476 }
19477
19478 static tree
19479 cp_parser_type_id (cp_parser *parser)
19480 {
19481 return cp_parser_type_id_1 (parser, false, false);
19482 }
19483
19484 static tree
19485 cp_parser_template_type_arg (cp_parser *parser)
19486 {
19487 tree r;
19488 const char *saved_message = parser->type_definition_forbidden_message;
19489 parser->type_definition_forbidden_message
19490 = G_("types may not be defined in template arguments");
19491 r = cp_parser_type_id_1 (parser, true, false);
19492 parser->type_definition_forbidden_message = saved_message;
19493 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
19494 {
19495 error ("invalid use of %<auto%> in template argument");
19496 r = error_mark_node;
19497 }
19498 return r;
19499 }
19500
19501 static tree
19502 cp_parser_trailing_type_id (cp_parser *parser)
19503 {
19504 return cp_parser_type_id_1 (parser, false, true);
19505 }
19506
19507 /* Parse a type-specifier-seq.
19508
19509 type-specifier-seq:
19510 type-specifier type-specifier-seq [opt]
19511
19512 GNU extension:
19513
19514 type-specifier-seq:
19515 attributes type-specifier-seq [opt]
19516
19517 If IS_DECLARATION is true, we are at the start of a "condition" or
19518 exception-declaration, so we might be followed by a declarator-id.
19519
19520 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
19521 i.e. we've just seen "->".
19522
19523 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
19524
19525 static void
19526 cp_parser_type_specifier_seq (cp_parser* parser,
19527 bool is_declaration,
19528 bool is_trailing_return,
19529 cp_decl_specifier_seq *type_specifier_seq)
19530 {
19531 bool seen_type_specifier = false;
19532 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
19533 cp_token *start_token = NULL;
19534
19535 /* Clear the TYPE_SPECIFIER_SEQ. */
19536 clear_decl_specs (type_specifier_seq);
19537
19538 /* In the context of a trailing return type, enum E { } is an
19539 elaborated-type-specifier followed by a function-body, not an
19540 enum-specifier. */
19541 if (is_trailing_return)
19542 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
19543
19544 /* Parse the type-specifiers and attributes. */
19545 while (true)
19546 {
19547 tree type_specifier;
19548 bool is_cv_qualifier;
19549
19550 /* Check for attributes first. */
19551 if (cp_next_tokens_can_be_attribute_p (parser))
19552 {
19553 type_specifier_seq->attributes =
19554 chainon (type_specifier_seq->attributes,
19555 cp_parser_attributes_opt (parser));
19556 continue;
19557 }
19558
19559 /* record the token of the beginning of the type specifier seq,
19560 for error reporting purposes*/
19561 if (!start_token)
19562 start_token = cp_lexer_peek_token (parser->lexer);
19563
19564 /* Look for the type-specifier. */
19565 type_specifier = cp_parser_type_specifier (parser,
19566 flags,
19567 type_specifier_seq,
19568 /*is_declaration=*/false,
19569 NULL,
19570 &is_cv_qualifier);
19571 if (!type_specifier)
19572 {
19573 /* If the first type-specifier could not be found, this is not a
19574 type-specifier-seq at all. */
19575 if (!seen_type_specifier)
19576 {
19577 /* Set in_declarator_p to avoid skipping to the semicolon. */
19578 int in_decl = parser->in_declarator_p;
19579 parser->in_declarator_p = true;
19580
19581 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
19582 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
19583 cp_parser_error (parser, "expected type-specifier");
19584
19585 parser->in_declarator_p = in_decl;
19586
19587 type_specifier_seq->type = error_mark_node;
19588 return;
19589 }
19590 /* If subsequent type-specifiers could not be found, the
19591 type-specifier-seq is complete. */
19592 break;
19593 }
19594
19595 seen_type_specifier = true;
19596 /* The standard says that a condition can be:
19597
19598 type-specifier-seq declarator = assignment-expression
19599
19600 However, given:
19601
19602 struct S {};
19603 if (int S = ...)
19604
19605 we should treat the "S" as a declarator, not as a
19606 type-specifier. The standard doesn't say that explicitly for
19607 type-specifier-seq, but it does say that for
19608 decl-specifier-seq in an ordinary declaration. Perhaps it
19609 would be clearer just to allow a decl-specifier-seq here, and
19610 then add a semantic restriction that if any decl-specifiers
19611 that are not type-specifiers appear, the program is invalid. */
19612 if (is_declaration && !is_cv_qualifier)
19613 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
19614 }
19615 }
19616
19617 /* Return whether the function currently being declared has an associated
19618 template parameter list. */
19619
19620 static bool
19621 function_being_declared_is_template_p (cp_parser* parser)
19622 {
19623 if (!current_template_parms || processing_template_parmlist)
19624 return false;
19625
19626 if (parser->implicit_template_scope)
19627 return true;
19628
19629 if (at_class_scope_p ()
19630 && TYPE_BEING_DEFINED (current_class_type))
19631 return parser->num_template_parameter_lists != 0;
19632
19633 return ((int) parser->num_template_parameter_lists > template_class_depth
19634 (current_class_type));
19635 }
19636
19637 /* Parse a parameter-declaration-clause.
19638
19639 parameter-declaration-clause:
19640 parameter-declaration-list [opt] ... [opt]
19641 parameter-declaration-list , ...
19642
19643 Returns a representation for the parameter declarations. A return
19644 value of NULL indicates a parameter-declaration-clause consisting
19645 only of an ellipsis. */
19646
19647 static tree
19648 cp_parser_parameter_declaration_clause (cp_parser* parser)
19649 {
19650 tree parameters;
19651 cp_token *token;
19652 bool ellipsis_p;
19653 bool is_error;
19654
19655 struct cleanup {
19656 cp_parser* parser;
19657 int auto_is_implicit_function_template_parm_p;
19658 ~cleanup() {
19659 parser->auto_is_implicit_function_template_parm_p
19660 = auto_is_implicit_function_template_parm_p;
19661 }
19662 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
19663
19664 (void) cleanup;
19665
19666 if (!processing_specialization
19667 && !processing_template_parmlist
19668 && !processing_explicit_instantiation)
19669 if (!current_function_decl
19670 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
19671 parser->auto_is_implicit_function_template_parm_p = true;
19672
19673 /* Peek at the next token. */
19674 token = cp_lexer_peek_token (parser->lexer);
19675 /* Check for trivial parameter-declaration-clauses. */
19676 if (token->type == CPP_ELLIPSIS)
19677 {
19678 /* Consume the `...' token. */
19679 cp_lexer_consume_token (parser->lexer);
19680 return NULL_TREE;
19681 }
19682 else if (token->type == CPP_CLOSE_PAREN)
19683 /* There are no parameters. */
19684 {
19685 #ifndef NO_IMPLICIT_EXTERN_C
19686 if (in_system_header_at (input_location)
19687 && current_class_type == NULL
19688 && current_lang_name == lang_name_c)
19689 return NULL_TREE;
19690 else
19691 #endif
19692 return void_list_node;
19693 }
19694 /* Check for `(void)', too, which is a special case. */
19695 else if (token->keyword == RID_VOID
19696 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19697 == CPP_CLOSE_PAREN))
19698 {
19699 /* Consume the `void' token. */
19700 cp_lexer_consume_token (parser->lexer);
19701 /* There are no parameters. */
19702 return void_list_node;
19703 }
19704
19705 /* Parse the parameter-declaration-list. */
19706 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
19707 /* If a parse error occurred while parsing the
19708 parameter-declaration-list, then the entire
19709 parameter-declaration-clause is erroneous. */
19710 if (is_error)
19711 return NULL;
19712
19713 /* Peek at the next token. */
19714 token = cp_lexer_peek_token (parser->lexer);
19715 /* If it's a `,', the clause should terminate with an ellipsis. */
19716 if (token->type == CPP_COMMA)
19717 {
19718 /* Consume the `,'. */
19719 cp_lexer_consume_token (parser->lexer);
19720 /* Expect an ellipsis. */
19721 ellipsis_p
19722 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
19723 }
19724 /* It might also be `...' if the optional trailing `,' was
19725 omitted. */
19726 else if (token->type == CPP_ELLIPSIS)
19727 {
19728 /* Consume the `...' token. */
19729 cp_lexer_consume_token (parser->lexer);
19730 /* And remember that we saw it. */
19731 ellipsis_p = true;
19732 }
19733 else
19734 ellipsis_p = false;
19735
19736 /* Finish the parameter list. */
19737 if (!ellipsis_p)
19738 parameters = chainon (parameters, void_list_node);
19739
19740 return parameters;
19741 }
19742
19743 /* Parse a parameter-declaration-list.
19744
19745 parameter-declaration-list:
19746 parameter-declaration
19747 parameter-declaration-list , parameter-declaration
19748
19749 Returns a representation of the parameter-declaration-list, as for
19750 cp_parser_parameter_declaration_clause. However, the
19751 `void_list_node' is never appended to the list. Upon return,
19752 *IS_ERROR will be true iff an error occurred. */
19753
19754 static tree
19755 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
19756 {
19757 tree parameters = NULL_TREE;
19758 tree *tail = &parameters;
19759 bool saved_in_unbraced_linkage_specification_p;
19760 int index = 0;
19761
19762 /* Assume all will go well. */
19763 *is_error = false;
19764 /* The special considerations that apply to a function within an
19765 unbraced linkage specifications do not apply to the parameters
19766 to the function. */
19767 saved_in_unbraced_linkage_specification_p
19768 = parser->in_unbraced_linkage_specification_p;
19769 parser->in_unbraced_linkage_specification_p = false;
19770
19771 /* Look for more parameters. */
19772 while (true)
19773 {
19774 cp_parameter_declarator *parameter;
19775 tree decl = error_mark_node;
19776 bool parenthesized_p = false;
19777 int template_parm_idx = (function_being_declared_is_template_p (parser)?
19778 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
19779 (current_template_parms)) : 0);
19780
19781 /* Parse the parameter. */
19782 parameter
19783 = cp_parser_parameter_declaration (parser,
19784 /*template_parm_p=*/false,
19785 &parenthesized_p);
19786
19787 /* We don't know yet if the enclosing context is deprecated, so wait
19788 and warn in grokparms if appropriate. */
19789 deprecated_state = DEPRECATED_SUPPRESS;
19790
19791 if (parameter)
19792 {
19793 /* If a function parameter pack was specified and an implicit template
19794 parameter was introduced during cp_parser_parameter_declaration,
19795 change any implicit parameters introduced into packs. */
19796 if (parser->implicit_template_parms
19797 && parameter->declarator
19798 && parameter->declarator->parameter_pack_p)
19799 {
19800 int latest_template_parm_idx = TREE_VEC_LENGTH
19801 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
19802
19803 if (latest_template_parm_idx != template_parm_idx)
19804 parameter->decl_specifiers.type = convert_generic_types_to_packs
19805 (parameter->decl_specifiers.type,
19806 template_parm_idx, latest_template_parm_idx);
19807 }
19808
19809 decl = grokdeclarator (parameter->declarator,
19810 &parameter->decl_specifiers,
19811 PARM,
19812 parameter->default_argument != NULL_TREE,
19813 &parameter->decl_specifiers.attributes);
19814 }
19815
19816 deprecated_state = DEPRECATED_NORMAL;
19817
19818 /* If a parse error occurred parsing the parameter declaration,
19819 then the entire parameter-declaration-list is erroneous. */
19820 if (decl == error_mark_node)
19821 {
19822 *is_error = true;
19823 parameters = error_mark_node;
19824 break;
19825 }
19826
19827 if (parameter->decl_specifiers.attributes)
19828 cplus_decl_attributes (&decl,
19829 parameter->decl_specifiers.attributes,
19830 0);
19831 if (DECL_NAME (decl))
19832 decl = pushdecl (decl);
19833
19834 if (decl != error_mark_node)
19835 {
19836 retrofit_lang_decl (decl);
19837 DECL_PARM_INDEX (decl) = ++index;
19838 DECL_PARM_LEVEL (decl) = function_parm_depth ();
19839 }
19840
19841 /* Add the new parameter to the list. */
19842 *tail = build_tree_list (parameter->default_argument, decl);
19843 tail = &TREE_CHAIN (*tail);
19844
19845 /* Peek at the next token. */
19846 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
19847 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
19848 /* These are for Objective-C++ */
19849 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19850 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19851 /* The parameter-declaration-list is complete. */
19852 break;
19853 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19854 {
19855 cp_token *token;
19856
19857 /* Peek at the next token. */
19858 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19859 /* If it's an ellipsis, then the list is complete. */
19860 if (token->type == CPP_ELLIPSIS)
19861 break;
19862 /* Otherwise, there must be more parameters. Consume the
19863 `,'. */
19864 cp_lexer_consume_token (parser->lexer);
19865 /* When parsing something like:
19866
19867 int i(float f, double d)
19868
19869 we can tell after seeing the declaration for "f" that we
19870 are not looking at an initialization of a variable "i",
19871 but rather at the declaration of a function "i".
19872
19873 Due to the fact that the parsing of template arguments
19874 (as specified to a template-id) requires backtracking we
19875 cannot use this technique when inside a template argument
19876 list. */
19877 if (!parser->in_template_argument_list_p
19878 && !parser->in_type_id_in_expr_p
19879 && cp_parser_uncommitted_to_tentative_parse_p (parser)
19880 /* However, a parameter-declaration of the form
19881 "float(f)" (which is a valid declaration of a
19882 parameter "f") can also be interpreted as an
19883 expression (the conversion of "f" to "float"). */
19884 && !parenthesized_p)
19885 cp_parser_commit_to_tentative_parse (parser);
19886 }
19887 else
19888 {
19889 cp_parser_error (parser, "expected %<,%> or %<...%>");
19890 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19891 cp_parser_skip_to_closing_parenthesis (parser,
19892 /*recovering=*/true,
19893 /*or_comma=*/false,
19894 /*consume_paren=*/false);
19895 break;
19896 }
19897 }
19898
19899 parser->in_unbraced_linkage_specification_p
19900 = saved_in_unbraced_linkage_specification_p;
19901
19902 /* Reset implicit_template_scope if we are about to leave the function
19903 parameter list that introduced it. Note that for out-of-line member
19904 definitions, there will be one or more class scopes before we get to
19905 the template parameter scope. */
19906
19907 if (cp_binding_level *its = parser->implicit_template_scope)
19908 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
19909 {
19910 while (maybe_its->kind == sk_class)
19911 maybe_its = maybe_its->level_chain;
19912 if (maybe_its == its)
19913 {
19914 parser->implicit_template_parms = 0;
19915 parser->implicit_template_scope = 0;
19916 }
19917 }
19918
19919 return parameters;
19920 }
19921
19922 /* Parse a parameter declaration.
19923
19924 parameter-declaration:
19925 decl-specifier-seq ... [opt] declarator
19926 decl-specifier-seq declarator = assignment-expression
19927 decl-specifier-seq ... [opt] abstract-declarator [opt]
19928 decl-specifier-seq abstract-declarator [opt] = assignment-expression
19929
19930 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
19931 declares a template parameter. (In that case, a non-nested `>'
19932 token encountered during the parsing of the assignment-expression
19933 is not interpreted as a greater-than operator.)
19934
19935 Returns a representation of the parameter, or NULL if an error
19936 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
19937 true iff the declarator is of the form "(p)". */
19938
19939 static cp_parameter_declarator *
19940 cp_parser_parameter_declaration (cp_parser *parser,
19941 bool template_parm_p,
19942 bool *parenthesized_p)
19943 {
19944 int declares_class_or_enum;
19945 cp_decl_specifier_seq decl_specifiers;
19946 cp_declarator *declarator;
19947 tree default_argument;
19948 cp_token *token = NULL, *declarator_token_start = NULL;
19949 const char *saved_message;
19950 bool template_parameter_pack_p = false;
19951
19952 /* In a template parameter, `>' is not an operator.
19953
19954 [temp.param]
19955
19956 When parsing a default template-argument for a non-type
19957 template-parameter, the first non-nested `>' is taken as the end
19958 of the template parameter-list rather than a greater-than
19959 operator. */
19960
19961 /* Type definitions may not appear in parameter types. */
19962 saved_message = parser->type_definition_forbidden_message;
19963 parser->type_definition_forbidden_message
19964 = G_("types may not be defined in parameter types");
19965
19966 /* Parse the declaration-specifiers. */
19967 cp_parser_decl_specifier_seq (parser,
19968 CP_PARSER_FLAGS_NONE,
19969 &decl_specifiers,
19970 &declares_class_or_enum);
19971
19972 /* Complain about missing 'typename' or other invalid type names. */
19973 if (!decl_specifiers.any_type_specifiers_p
19974 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19975 decl_specifiers.type = error_mark_node;
19976
19977 /* If an error occurred, there's no reason to attempt to parse the
19978 rest of the declaration. */
19979 if (cp_parser_error_occurred (parser))
19980 {
19981 parser->type_definition_forbidden_message = saved_message;
19982 return NULL;
19983 }
19984
19985 /* Peek at the next token. */
19986 token = cp_lexer_peek_token (parser->lexer);
19987
19988 /* If the next token is a `)', `,', `=', `>', or `...', then there
19989 is no declarator. However, when variadic templates are enabled,
19990 there may be a declarator following `...'. */
19991 if (token->type == CPP_CLOSE_PAREN
19992 || token->type == CPP_COMMA
19993 || token->type == CPP_EQ
19994 || token->type == CPP_GREATER)
19995 {
19996 declarator = NULL;
19997 if (parenthesized_p)
19998 *parenthesized_p = false;
19999 }
20000 /* Otherwise, there should be a declarator. */
20001 else
20002 {
20003 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20004 parser->default_arg_ok_p = false;
20005
20006 /* After seeing a decl-specifier-seq, if the next token is not a
20007 "(", there is no possibility that the code is a valid
20008 expression. Therefore, if parsing tentatively, we commit at
20009 this point. */
20010 if (!parser->in_template_argument_list_p
20011 /* In an expression context, having seen:
20012
20013 (int((char ...
20014
20015 we cannot be sure whether we are looking at a
20016 function-type (taking a "char" as a parameter) or a cast
20017 of some object of type "char" to "int". */
20018 && !parser->in_type_id_in_expr_p
20019 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20020 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20021 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
20022 cp_parser_commit_to_tentative_parse (parser);
20023 /* Parse the declarator. */
20024 declarator_token_start = token;
20025 declarator = cp_parser_declarator (parser,
20026 CP_PARSER_DECLARATOR_EITHER,
20027 /*ctor_dtor_or_conv_p=*/NULL,
20028 parenthesized_p,
20029 /*member_p=*/false,
20030 /*friend_p=*/false);
20031 parser->default_arg_ok_p = saved_default_arg_ok_p;
20032 /* After the declarator, allow more attributes. */
20033 decl_specifiers.attributes
20034 = chainon (decl_specifiers.attributes,
20035 cp_parser_attributes_opt (parser));
20036
20037 /* If the declarator is a template parameter pack, remember that and
20038 clear the flag in the declarator itself so we don't get errors
20039 from grokdeclarator. */
20040 if (template_parm_p && declarator && declarator->parameter_pack_p)
20041 {
20042 declarator->parameter_pack_p = false;
20043 template_parameter_pack_p = true;
20044 }
20045 }
20046
20047 /* If the next token is an ellipsis, and we have not seen a declarator
20048 name, and if either the type of the declarator contains parameter
20049 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20050 for, eg, abbreviated integral type names), then we actually have a
20051 parameter pack expansion expression. Otherwise, leave the ellipsis
20052 for a C-style variadic function. */
20053 token = cp_lexer_peek_token (parser->lexer);
20054 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20055 {
20056 tree type = decl_specifiers.type;
20057
20058 if (type && DECL_P (type))
20059 type = TREE_TYPE (type);
20060
20061 if (((type
20062 && TREE_CODE (type) != TYPE_PACK_EXPANSION
20063 && (template_parm_p || uses_parameter_packs (type)))
20064 || (!type && template_parm_p))
20065 && declarator_can_be_parameter_pack (declarator))
20066 {
20067 /* Consume the `...'. */
20068 cp_lexer_consume_token (parser->lexer);
20069 maybe_warn_variadic_templates ();
20070
20071 /* Build a pack expansion type */
20072 if (template_parm_p)
20073 template_parameter_pack_p = true;
20074 else if (declarator)
20075 declarator->parameter_pack_p = true;
20076 else
20077 decl_specifiers.type = make_pack_expansion (type);
20078 }
20079 }
20080
20081 /* The restriction on defining new types applies only to the type
20082 of the parameter, not to the default argument. */
20083 parser->type_definition_forbidden_message = saved_message;
20084
20085 /* If the next token is `=', then process a default argument. */
20086 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20087 {
20088 tree type = decl_specifiers.type;
20089 token = cp_lexer_peek_token (parser->lexer);
20090 /* If we are defining a class, then the tokens that make up the
20091 default argument must be saved and processed later. */
20092 if (!template_parm_p && at_class_scope_p ()
20093 && TYPE_BEING_DEFINED (current_class_type)
20094 && !LAMBDA_TYPE_P (current_class_type))
20095 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
20096
20097 // A constrained-type-specifier may declare a type template-parameter.
20098 else if (declares_constrained_type_template_parameter (type))
20099 default_argument
20100 = cp_parser_default_type_template_argument (parser);
20101
20102 // A constrained-type-specifier may declare a template-template-parameter.
20103 else if (declares_constrained_template_template_parameter (type))
20104 default_argument
20105 = cp_parser_default_template_template_argument (parser);
20106
20107 /* Outside of a class definition, we can just parse the
20108 assignment-expression. */
20109 else
20110 default_argument
20111 = cp_parser_default_argument (parser, template_parm_p);
20112
20113 if (!parser->default_arg_ok_p)
20114 {
20115 permerror (token->location,
20116 "default arguments are only "
20117 "permitted for function parameters");
20118 }
20119 else if ((declarator && declarator->parameter_pack_p)
20120 || template_parameter_pack_p
20121 || (decl_specifiers.type
20122 && PACK_EXPANSION_P (decl_specifiers.type)))
20123 {
20124 /* Find the name of the parameter pack. */
20125 cp_declarator *id_declarator = declarator;
20126 while (id_declarator && id_declarator->kind != cdk_id)
20127 id_declarator = id_declarator->declarator;
20128
20129 if (id_declarator && id_declarator->kind == cdk_id)
20130 error_at (declarator_token_start->location,
20131 template_parm_p
20132 ? G_("template parameter pack %qD "
20133 "cannot have a default argument")
20134 : G_("parameter pack %qD cannot have "
20135 "a default argument"),
20136 id_declarator->u.id.unqualified_name);
20137 else
20138 error_at (declarator_token_start->location,
20139 template_parm_p
20140 ? G_("template parameter pack cannot have "
20141 "a default argument")
20142 : G_("parameter pack cannot have a "
20143 "default argument"));
20144
20145 default_argument = NULL_TREE;
20146 }
20147 }
20148 else
20149 default_argument = NULL_TREE;
20150
20151 return make_parameter_declarator (&decl_specifiers,
20152 declarator,
20153 default_argument,
20154 template_parameter_pack_p);
20155 }
20156
20157 /* Parse a default argument and return it.
20158
20159 TEMPLATE_PARM_P is true if this is a default argument for a
20160 non-type template parameter. */
20161 static tree
20162 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
20163 {
20164 tree default_argument = NULL_TREE;
20165 bool saved_greater_than_is_operator_p;
20166 bool saved_local_variables_forbidden_p;
20167 bool non_constant_p, is_direct_init;
20168
20169 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20170 set correctly. */
20171 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
20172 parser->greater_than_is_operator_p = !template_parm_p;
20173 /* Local variable names (and the `this' keyword) may not
20174 appear in a default argument. */
20175 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20176 parser->local_variables_forbidden_p = true;
20177 /* Parse the assignment-expression. */
20178 if (template_parm_p)
20179 push_deferring_access_checks (dk_no_deferred);
20180 tree saved_class_ptr = NULL_TREE;
20181 tree saved_class_ref = NULL_TREE;
20182 /* The "this" pointer is not valid in a default argument. */
20183 if (cfun)
20184 {
20185 saved_class_ptr = current_class_ptr;
20186 cp_function_chain->x_current_class_ptr = NULL_TREE;
20187 saved_class_ref = current_class_ref;
20188 cp_function_chain->x_current_class_ref = NULL_TREE;
20189 }
20190 default_argument
20191 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
20192 /* Restore the "this" pointer. */
20193 if (cfun)
20194 {
20195 cp_function_chain->x_current_class_ptr = saved_class_ptr;
20196 cp_function_chain->x_current_class_ref = saved_class_ref;
20197 }
20198 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
20199 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20200 if (template_parm_p)
20201 pop_deferring_access_checks ();
20202 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
20203 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20204
20205 return default_argument;
20206 }
20207
20208 /* Parse a function-body.
20209
20210 function-body:
20211 compound_statement */
20212
20213 static void
20214 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
20215 {
20216 cp_parser_compound_statement (parser, NULL, (in_function_try_block
20217 ? BCS_TRY_BLOCK : BCS_NORMAL),
20218 true);
20219 }
20220
20221 /* Parse a ctor-initializer-opt followed by a function-body. Return
20222 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
20223 is true we are parsing a function-try-block. */
20224
20225 static bool
20226 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
20227 bool in_function_try_block)
20228 {
20229 tree body, list;
20230 bool ctor_initializer_p;
20231 const bool check_body_p =
20232 DECL_CONSTRUCTOR_P (current_function_decl)
20233 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
20234 tree last = NULL;
20235
20236 /* Begin the function body. */
20237 body = begin_function_body ();
20238 /* Parse the optional ctor-initializer. */
20239 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
20240
20241 /* If we're parsing a constexpr constructor definition, we need
20242 to check that the constructor body is indeed empty. However,
20243 before we get to cp_parser_function_body lot of junk has been
20244 generated, so we can't just check that we have an empty block.
20245 Rather we take a snapshot of the outermost block, and check whether
20246 cp_parser_function_body changed its state. */
20247 if (check_body_p)
20248 {
20249 list = cur_stmt_list;
20250 if (STATEMENT_LIST_TAIL (list))
20251 last = STATEMENT_LIST_TAIL (list)->stmt;
20252 }
20253 /* Parse the function-body. */
20254 cp_parser_function_body (parser, in_function_try_block);
20255 if (check_body_p)
20256 check_constexpr_ctor_body (last, list, /*complain=*/true);
20257 /* Finish the function body. */
20258 finish_function_body (body);
20259
20260 return ctor_initializer_p;
20261 }
20262
20263 /* Parse an initializer.
20264
20265 initializer:
20266 = initializer-clause
20267 ( expression-list )
20268
20269 Returns an expression representing the initializer. If no
20270 initializer is present, NULL_TREE is returned.
20271
20272 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20273 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
20274 set to TRUE if there is no initializer present. If there is an
20275 initializer, and it is not a constant-expression, *NON_CONSTANT_P
20276 is set to true; otherwise it is set to false. */
20277
20278 static tree
20279 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
20280 bool* non_constant_p)
20281 {
20282 cp_token *token;
20283 tree init;
20284
20285 /* Peek at the next token. */
20286 token = cp_lexer_peek_token (parser->lexer);
20287
20288 /* Let our caller know whether or not this initializer was
20289 parenthesized. */
20290 *is_direct_init = (token->type != CPP_EQ);
20291 /* Assume that the initializer is constant. */
20292 *non_constant_p = false;
20293
20294 if (token->type == CPP_EQ)
20295 {
20296 /* Consume the `='. */
20297 cp_lexer_consume_token (parser->lexer);
20298 /* Parse the initializer-clause. */
20299 init = cp_parser_initializer_clause (parser, non_constant_p);
20300 }
20301 else if (token->type == CPP_OPEN_PAREN)
20302 {
20303 vec<tree, va_gc> *vec;
20304 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20305 /*cast_p=*/false,
20306 /*allow_expansion_p=*/true,
20307 non_constant_p);
20308 if (vec == NULL)
20309 return error_mark_node;
20310 init = build_tree_list_vec (vec);
20311 release_tree_vector (vec);
20312 }
20313 else if (token->type == CPP_OPEN_BRACE)
20314 {
20315 cp_lexer_set_source_position (parser->lexer);
20316 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20317 init = cp_parser_braced_list (parser, non_constant_p);
20318 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
20319 }
20320 else
20321 {
20322 /* Anything else is an error. */
20323 cp_parser_error (parser, "expected initializer");
20324 init = error_mark_node;
20325 }
20326
20327 return init;
20328 }
20329
20330 /* Parse an initializer-clause.
20331
20332 initializer-clause:
20333 assignment-expression
20334 braced-init-list
20335
20336 Returns an expression representing the initializer.
20337
20338 If the `assignment-expression' production is used the value
20339 returned is simply a representation for the expression.
20340
20341 Otherwise, calls cp_parser_braced_list. */
20342
20343 static tree
20344 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
20345 {
20346 tree initializer;
20347
20348 /* Assume the expression is constant. */
20349 *non_constant_p = false;
20350
20351 /* If it is not a `{', then we are looking at an
20352 assignment-expression. */
20353 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
20354 {
20355 initializer
20356 = cp_parser_constant_expression (parser,
20357 /*allow_non_constant_p=*/true,
20358 non_constant_p);
20359 }
20360 else
20361 initializer = cp_parser_braced_list (parser, non_constant_p);
20362
20363 return initializer;
20364 }
20365
20366 /* Parse a brace-enclosed initializer list.
20367
20368 braced-init-list:
20369 { initializer-list , [opt] }
20370 { }
20371
20372 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
20373 the elements of the initializer-list (or NULL, if the last
20374 production is used). The TREE_TYPE for the CONSTRUCTOR will be
20375 NULL_TREE. There is no way to detect whether or not the optional
20376 trailing `,' was provided. NON_CONSTANT_P is as for
20377 cp_parser_initializer. */
20378
20379 static tree
20380 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
20381 {
20382 tree initializer;
20383
20384 /* Consume the `{' token. */
20385 cp_lexer_consume_token (parser->lexer);
20386 /* Create a CONSTRUCTOR to represent the braced-initializer. */
20387 initializer = make_node (CONSTRUCTOR);
20388 /* If it's not a `}', then there is a non-trivial initializer. */
20389 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
20390 {
20391 /* Parse the initializer list. */
20392 CONSTRUCTOR_ELTS (initializer)
20393 = cp_parser_initializer_list (parser, non_constant_p);
20394 /* A trailing `,' token is allowed. */
20395 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20396 cp_lexer_consume_token (parser->lexer);
20397 }
20398 else
20399 *non_constant_p = false;
20400 /* Now, there should be a trailing `}'. */
20401 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20402 TREE_TYPE (initializer) = init_list_type_node;
20403 return initializer;
20404 }
20405
20406 /* Consume tokens up to, and including, the next non-nested closing `]'.
20407 Returns true iff we found a closing `]'. */
20408
20409 static bool
20410 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
20411 {
20412 unsigned square_depth = 0;
20413
20414 while (true)
20415 {
20416 cp_token * token = cp_lexer_peek_token (parser->lexer);
20417
20418 switch (token->type)
20419 {
20420 case CPP_EOF:
20421 case CPP_PRAGMA_EOL:
20422 /* If we've run out of tokens, then there is no closing `]'. */
20423 return false;
20424
20425 case CPP_OPEN_SQUARE:
20426 ++square_depth;
20427 break;
20428
20429 case CPP_CLOSE_SQUARE:
20430 if (!square_depth--)
20431 {
20432 cp_lexer_consume_token (parser->lexer);
20433 return true;
20434 }
20435 break;
20436
20437 default:
20438 break;
20439 }
20440
20441 /* Consume the token. */
20442 cp_lexer_consume_token (parser->lexer);
20443 }
20444 }
20445
20446 /* Return true if we are looking at an array-designator, false otherwise. */
20447
20448 static bool
20449 cp_parser_array_designator_p (cp_parser *parser)
20450 {
20451 /* Consume the `['. */
20452 cp_lexer_consume_token (parser->lexer);
20453
20454 cp_lexer_save_tokens (parser->lexer);
20455
20456 /* Skip tokens until the next token is a closing square bracket.
20457 If we find the closing `]', and the next token is a `=', then
20458 we are looking at an array designator. */
20459 bool array_designator_p
20460 = (cp_parser_skip_to_closing_square_bracket (parser)
20461 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
20462
20463 /* Roll back the tokens we skipped. */
20464 cp_lexer_rollback_tokens (parser->lexer);
20465
20466 return array_designator_p;
20467 }
20468
20469 /* Parse an initializer-list.
20470
20471 initializer-list:
20472 initializer-clause ... [opt]
20473 initializer-list , initializer-clause ... [opt]
20474
20475 GNU Extension:
20476
20477 initializer-list:
20478 designation initializer-clause ...[opt]
20479 initializer-list , designation initializer-clause ...[opt]
20480
20481 designation:
20482 . identifier =
20483 identifier :
20484 [ constant-expression ] =
20485
20486 Returns a vec of constructor_elt. The VALUE of each elt is an expression
20487 for the initializer. If the INDEX of the elt is non-NULL, it is the
20488 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
20489 as for cp_parser_initializer. */
20490
20491 static vec<constructor_elt, va_gc> *
20492 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
20493 {
20494 vec<constructor_elt, va_gc> *v = NULL;
20495
20496 /* Assume all of the expressions are constant. */
20497 *non_constant_p = false;
20498
20499 /* Parse the rest of the list. */
20500 while (true)
20501 {
20502 cp_token *token;
20503 tree designator;
20504 tree initializer;
20505 bool clause_non_constant_p;
20506
20507 /* If the next token is an identifier and the following one is a
20508 colon, we are looking at the GNU designated-initializer
20509 syntax. */
20510 if (cp_parser_allow_gnu_extensions_p (parser)
20511 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
20512 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
20513 {
20514 /* Warn the user that they are using an extension. */
20515 pedwarn (input_location, OPT_Wpedantic,
20516 "ISO C++ does not allow designated initializers");
20517 /* Consume the identifier. */
20518 designator = cp_lexer_consume_token (parser->lexer)->u.value;
20519 /* Consume the `:'. */
20520 cp_lexer_consume_token (parser->lexer);
20521 }
20522 /* Also handle the C99 syntax, '. id ='. */
20523 else if (cp_parser_allow_gnu_extensions_p (parser)
20524 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
20525 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
20526 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
20527 {
20528 /* Warn the user that they are using an extension. */
20529 pedwarn (input_location, OPT_Wpedantic,
20530 "ISO C++ does not allow C99 designated initializers");
20531 /* Consume the `.'. */
20532 cp_lexer_consume_token (parser->lexer);
20533 /* Consume the identifier. */
20534 designator = cp_lexer_consume_token (parser->lexer)->u.value;
20535 /* Consume the `='. */
20536 cp_lexer_consume_token (parser->lexer);
20537 }
20538 /* Also handle C99 array designators, '[ const ] ='. */
20539 else if (cp_parser_allow_gnu_extensions_p (parser)
20540 && !c_dialect_objc ()
20541 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20542 {
20543 /* In C++11, [ could start a lambda-introducer. */
20544 bool non_const = false;
20545
20546 cp_parser_parse_tentatively (parser);
20547
20548 if (!cp_parser_array_designator_p (parser))
20549 {
20550 cp_parser_simulate_error (parser);
20551 designator = NULL_TREE;
20552 }
20553 else
20554 {
20555 designator = cp_parser_constant_expression (parser, true,
20556 &non_const);
20557 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20558 cp_parser_require (parser, CPP_EQ, RT_EQ);
20559 }
20560
20561 if (!cp_parser_parse_definitely (parser))
20562 designator = NULL_TREE;
20563 else if (non_const)
20564 require_potential_rvalue_constant_expression (designator);
20565 }
20566 else
20567 designator = NULL_TREE;
20568
20569 /* Parse the initializer. */
20570 initializer = cp_parser_initializer_clause (parser,
20571 &clause_non_constant_p);
20572 /* If any clause is non-constant, so is the entire initializer. */
20573 if (clause_non_constant_p)
20574 *non_constant_p = true;
20575
20576 /* If we have an ellipsis, this is an initializer pack
20577 expansion. */
20578 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20579 {
20580 /* Consume the `...'. */
20581 cp_lexer_consume_token (parser->lexer);
20582
20583 /* Turn the initializer into an initializer expansion. */
20584 initializer = make_pack_expansion (initializer);
20585 }
20586
20587 /* Add it to the vector. */
20588 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
20589
20590 /* If the next token is not a comma, we have reached the end of
20591 the list. */
20592 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20593 break;
20594
20595 /* Peek at the next token. */
20596 token = cp_lexer_peek_nth_token (parser->lexer, 2);
20597 /* If the next token is a `}', then we're still done. An
20598 initializer-clause can have a trailing `,' after the
20599 initializer-list and before the closing `}'. */
20600 if (token->type == CPP_CLOSE_BRACE)
20601 break;
20602
20603 /* Consume the `,' token. */
20604 cp_lexer_consume_token (parser->lexer);
20605 }
20606
20607 return v;
20608 }
20609
20610 /* Classes [gram.class] */
20611
20612 /* Parse a class-name.
20613
20614 class-name:
20615 identifier
20616 template-id
20617
20618 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
20619 to indicate that names looked up in dependent types should be
20620 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
20621 keyword has been used to indicate that the name that appears next
20622 is a template. TAG_TYPE indicates the explicit tag given before
20623 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
20624 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
20625 is the class being defined in a class-head. If ENUM_OK is TRUE,
20626 enum-names are also accepted.
20627
20628 Returns the TYPE_DECL representing the class. */
20629
20630 static tree
20631 cp_parser_class_name (cp_parser *parser,
20632 bool typename_keyword_p,
20633 bool template_keyword_p,
20634 enum tag_types tag_type,
20635 bool check_dependency_p,
20636 bool class_head_p,
20637 bool is_declaration,
20638 bool enum_ok)
20639 {
20640 tree decl;
20641 tree scope;
20642 bool typename_p;
20643 cp_token *token;
20644 tree identifier = NULL_TREE;
20645
20646 /* All class-names start with an identifier. */
20647 token = cp_lexer_peek_token (parser->lexer);
20648 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
20649 {
20650 cp_parser_error (parser, "expected class-name");
20651 return error_mark_node;
20652 }
20653
20654 /* PARSER->SCOPE can be cleared when parsing the template-arguments
20655 to a template-id, so we save it here. */
20656 scope = parser->scope;
20657 if (scope == error_mark_node)
20658 return error_mark_node;
20659
20660 /* Any name names a type if we're following the `typename' keyword
20661 in a qualified name where the enclosing scope is type-dependent. */
20662 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
20663 && dependent_type_p (scope));
20664 /* Handle the common case (an identifier, but not a template-id)
20665 efficiently. */
20666 if (token->type == CPP_NAME
20667 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
20668 {
20669 cp_token *identifier_token;
20670 bool ambiguous_p;
20671
20672 /* Look for the identifier. */
20673 identifier_token = cp_lexer_peek_token (parser->lexer);
20674 ambiguous_p = identifier_token->error_reported;
20675 identifier = cp_parser_identifier (parser);
20676 /* If the next token isn't an identifier, we are certainly not
20677 looking at a class-name. */
20678 if (identifier == error_mark_node)
20679 decl = error_mark_node;
20680 /* If we know this is a type-name, there's no need to look it
20681 up. */
20682 else if (typename_p)
20683 decl = identifier;
20684 else
20685 {
20686 tree ambiguous_decls;
20687 /* If we already know that this lookup is ambiguous, then
20688 we've already issued an error message; there's no reason
20689 to check again. */
20690 if (ambiguous_p)
20691 {
20692 cp_parser_simulate_error (parser);
20693 return error_mark_node;
20694 }
20695 /* If the next token is a `::', then the name must be a type
20696 name.
20697
20698 [basic.lookup.qual]
20699
20700 During the lookup for a name preceding the :: scope
20701 resolution operator, object, function, and enumerator
20702 names are ignored. */
20703 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20704 tag_type = typename_type;
20705 /* Look up the name. */
20706 decl = cp_parser_lookup_name (parser, identifier,
20707 tag_type,
20708 /*is_template=*/false,
20709 /*is_namespace=*/false,
20710 check_dependency_p,
20711 &ambiguous_decls,
20712 identifier_token->location);
20713 if (ambiguous_decls)
20714 {
20715 if (cp_parser_parsing_tentatively (parser))
20716 cp_parser_simulate_error (parser);
20717 return error_mark_node;
20718 }
20719 }
20720 }
20721 else
20722 {
20723 /* Try a template-id. */
20724 decl = cp_parser_template_id (parser, template_keyword_p,
20725 check_dependency_p,
20726 tag_type,
20727 is_declaration);
20728 if (decl == error_mark_node)
20729 return error_mark_node;
20730 }
20731
20732 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
20733
20734 /* If this is a typename, create a TYPENAME_TYPE. */
20735 if (typename_p && decl != error_mark_node)
20736 {
20737 decl = make_typename_type (scope, decl, typename_type,
20738 /*complain=*/tf_error);
20739 if (decl != error_mark_node)
20740 decl = TYPE_NAME (decl);
20741 }
20742
20743 decl = strip_using_decl (decl);
20744
20745 /* Check to see that it is really the name of a class. */
20746 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
20747 && identifier_p (TREE_OPERAND (decl, 0))
20748 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20749 /* Situations like this:
20750
20751 template <typename T> struct A {
20752 typename T::template X<int>::I i;
20753 };
20754
20755 are problematic. Is `T::template X<int>' a class-name? The
20756 standard does not seem to be definitive, but there is no other
20757 valid interpretation of the following `::'. Therefore, those
20758 names are considered class-names. */
20759 {
20760 decl = make_typename_type (scope, decl, tag_type, tf_error);
20761 if (decl != error_mark_node)
20762 decl = TYPE_NAME (decl);
20763 }
20764 else if (TREE_CODE (decl) != TYPE_DECL
20765 || TREE_TYPE (decl) == error_mark_node
20766 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
20767 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
20768 /* In Objective-C 2.0, a classname followed by '.' starts a
20769 dot-syntax expression, and it's not a type-name. */
20770 || (c_dialect_objc ()
20771 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
20772 && objc_is_class_name (decl)))
20773 decl = error_mark_node;
20774
20775 if (decl == error_mark_node)
20776 cp_parser_error (parser, "expected class-name");
20777 else if (identifier && !parser->scope)
20778 maybe_note_name_used_in_class (identifier, decl);
20779
20780 return decl;
20781 }
20782
20783 /* Parse a class-specifier.
20784
20785 class-specifier:
20786 class-head { member-specification [opt] }
20787
20788 Returns the TREE_TYPE representing the class. */
20789
20790 static tree
20791 cp_parser_class_specifier_1 (cp_parser* parser)
20792 {
20793 tree type;
20794 tree attributes = NULL_TREE;
20795 bool nested_name_specifier_p;
20796 unsigned saved_num_template_parameter_lists;
20797 bool saved_in_function_body;
20798 unsigned char in_statement;
20799 bool in_switch_statement_p;
20800 bool saved_in_unbraced_linkage_specification_p;
20801 tree old_scope = NULL_TREE;
20802 tree scope = NULL_TREE;
20803 cp_token *closing_brace;
20804
20805 push_deferring_access_checks (dk_no_deferred);
20806
20807 /* Parse the class-head. */
20808 type = cp_parser_class_head (parser,
20809 &nested_name_specifier_p);
20810 /* If the class-head was a semantic disaster, skip the entire body
20811 of the class. */
20812 if (!type)
20813 {
20814 cp_parser_skip_to_end_of_block_or_statement (parser);
20815 pop_deferring_access_checks ();
20816 return error_mark_node;
20817 }
20818
20819 /* Look for the `{'. */
20820 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
20821 {
20822 pop_deferring_access_checks ();
20823 return error_mark_node;
20824 }
20825
20826 cp_ensure_no_omp_declare_simd (parser);
20827 cp_ensure_no_oacc_routine (parser);
20828
20829 /* Issue an error message if type-definitions are forbidden here. */
20830 cp_parser_check_type_definition (parser);
20831 /* Remember that we are defining one more class. */
20832 ++parser->num_classes_being_defined;
20833 /* Inside the class, surrounding template-parameter-lists do not
20834 apply. */
20835 saved_num_template_parameter_lists
20836 = parser->num_template_parameter_lists;
20837 parser->num_template_parameter_lists = 0;
20838 /* We are not in a function body. */
20839 saved_in_function_body = parser->in_function_body;
20840 parser->in_function_body = false;
20841 /* Or in a loop. */
20842 in_statement = parser->in_statement;
20843 parser->in_statement = 0;
20844 /* Or in a switch. */
20845 in_switch_statement_p = parser->in_switch_statement_p;
20846 parser->in_switch_statement_p = false;
20847 /* We are not immediately inside an extern "lang" block. */
20848 saved_in_unbraced_linkage_specification_p
20849 = parser->in_unbraced_linkage_specification_p;
20850 parser->in_unbraced_linkage_specification_p = false;
20851
20852 // Associate constraints with the type.
20853 if (flag_concepts)
20854 type = associate_classtype_constraints (type);
20855
20856 /* Start the class. */
20857 if (nested_name_specifier_p)
20858 {
20859 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
20860 old_scope = push_inner_scope (scope);
20861 }
20862 type = begin_class_definition (type);
20863
20864 if (type == error_mark_node)
20865 /* If the type is erroneous, skip the entire body of the class. */
20866 cp_parser_skip_to_closing_brace (parser);
20867 else
20868 /* Parse the member-specification. */
20869 cp_parser_member_specification_opt (parser);
20870
20871 /* Look for the trailing `}'. */
20872 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20873 /* Look for trailing attributes to apply to this class. */
20874 if (cp_parser_allow_gnu_extensions_p (parser))
20875 attributes = cp_parser_gnu_attributes_opt (parser);
20876 if (type != error_mark_node)
20877 type = finish_struct (type, attributes);
20878 if (nested_name_specifier_p)
20879 pop_inner_scope (old_scope, scope);
20880
20881 /* We've finished a type definition. Check for the common syntax
20882 error of forgetting a semicolon after the definition. We need to
20883 be careful, as we can't just check for not-a-semicolon and be done
20884 with it; the user might have typed:
20885
20886 class X { } c = ...;
20887 class X { } *p = ...;
20888
20889 and so forth. Instead, enumerate all the possible tokens that
20890 might follow this production; if we don't see one of them, then
20891 complain and silently insert the semicolon. */
20892 {
20893 cp_token *token = cp_lexer_peek_token (parser->lexer);
20894 bool want_semicolon = true;
20895
20896 if (cp_next_tokens_can_be_std_attribute_p (parser))
20897 /* Don't try to parse c++11 attributes here. As per the
20898 grammar, that should be a task for
20899 cp_parser_decl_specifier_seq. */
20900 want_semicolon = false;
20901
20902 switch (token->type)
20903 {
20904 case CPP_NAME:
20905 case CPP_SEMICOLON:
20906 case CPP_MULT:
20907 case CPP_AND:
20908 case CPP_OPEN_PAREN:
20909 case CPP_CLOSE_PAREN:
20910 case CPP_COMMA:
20911 want_semicolon = false;
20912 break;
20913
20914 /* While it's legal for type qualifiers and storage class
20915 specifiers to follow type definitions in the grammar, only
20916 compiler testsuites contain code like that. Assume that if
20917 we see such code, then what we're really seeing is a case
20918 like:
20919
20920 class X { }
20921 const <type> var = ...;
20922
20923 or
20924
20925 class Y { }
20926 static <type> func (...) ...
20927
20928 i.e. the qualifier or specifier applies to the next
20929 declaration. To do so, however, we need to look ahead one
20930 more token to see if *that* token is a type specifier.
20931
20932 This code could be improved to handle:
20933
20934 class Z { }
20935 static const <type> var = ...; */
20936 case CPP_KEYWORD:
20937 if (keyword_is_decl_specifier (token->keyword))
20938 {
20939 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
20940
20941 /* Handling user-defined types here would be nice, but very
20942 tricky. */
20943 want_semicolon
20944 = (lookahead->type == CPP_KEYWORD
20945 && keyword_begins_type_specifier (lookahead->keyword));
20946 }
20947 break;
20948 default:
20949 break;
20950 }
20951
20952 /* If we don't have a type, then something is very wrong and we
20953 shouldn't try to do anything clever. Likewise for not seeing the
20954 closing brace. */
20955 if (closing_brace && TYPE_P (type) && want_semicolon)
20956 {
20957 cp_token_position prev
20958 = cp_lexer_previous_token_position (parser->lexer);
20959 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
20960 location_t loc = prev_token->location;
20961
20962 if (CLASSTYPE_DECLARED_CLASS (type))
20963 error_at (loc, "expected %<;%> after class definition");
20964 else if (TREE_CODE (type) == RECORD_TYPE)
20965 error_at (loc, "expected %<;%> after struct definition");
20966 else if (TREE_CODE (type) == UNION_TYPE)
20967 error_at (loc, "expected %<;%> after union definition");
20968 else
20969 gcc_unreachable ();
20970
20971 /* Unget one token and smash it to look as though we encountered
20972 a semicolon in the input stream. */
20973 cp_lexer_set_token_position (parser->lexer, prev);
20974 token = cp_lexer_peek_token (parser->lexer);
20975 token->type = CPP_SEMICOLON;
20976 token->keyword = RID_MAX;
20977 }
20978 }
20979
20980 /* If this class is not itself within the scope of another class,
20981 then we need to parse the bodies of all of the queued function
20982 definitions. Note that the queued functions defined in a class
20983 are not always processed immediately following the
20984 class-specifier for that class. Consider:
20985
20986 struct A {
20987 struct B { void f() { sizeof (A); } };
20988 };
20989
20990 If `f' were processed before the processing of `A' were
20991 completed, there would be no way to compute the size of `A'.
20992 Note that the nesting we are interested in here is lexical --
20993 not the semantic nesting given by TYPE_CONTEXT. In particular,
20994 for:
20995
20996 struct A { struct B; };
20997 struct A::B { void f() { } };
20998
20999 there is no need to delay the parsing of `A::B::f'. */
21000 if (--parser->num_classes_being_defined == 0)
21001 {
21002 tree decl;
21003 tree class_type = NULL_TREE;
21004 tree pushed_scope = NULL_TREE;
21005 unsigned ix;
21006 cp_default_arg_entry *e;
21007 tree save_ccp, save_ccr;
21008
21009 /* In a first pass, parse default arguments to the functions.
21010 Then, in a second pass, parse the bodies of the functions.
21011 This two-phased approach handles cases like:
21012
21013 struct S {
21014 void f() { g(); }
21015 void g(int i = 3);
21016 };
21017
21018 */
21019 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
21020 {
21021 decl = e->decl;
21022 /* If there are default arguments that have not yet been processed,
21023 take care of them now. */
21024 if (class_type != e->class_type)
21025 {
21026 if (pushed_scope)
21027 pop_scope (pushed_scope);
21028 class_type = e->class_type;
21029 pushed_scope = push_scope (class_type);
21030 }
21031 /* Make sure that any template parameters are in scope. */
21032 maybe_begin_member_template_processing (decl);
21033 /* Parse the default argument expressions. */
21034 cp_parser_late_parsing_default_args (parser, decl);
21035 /* Remove any template parameters from the symbol table. */
21036 maybe_end_member_template_processing ();
21037 }
21038 vec_safe_truncate (unparsed_funs_with_default_args, 0);
21039 /* Now parse any NSDMIs. */
21040 save_ccp = current_class_ptr;
21041 save_ccr = current_class_ref;
21042 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
21043 {
21044 if (class_type != DECL_CONTEXT (decl))
21045 {
21046 if (pushed_scope)
21047 pop_scope (pushed_scope);
21048 class_type = DECL_CONTEXT (decl);
21049 pushed_scope = push_scope (class_type);
21050 }
21051 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
21052 cp_parser_late_parsing_nsdmi (parser, decl);
21053 }
21054 vec_safe_truncate (unparsed_nsdmis, 0);
21055 current_class_ptr = save_ccp;
21056 current_class_ref = save_ccr;
21057 if (pushed_scope)
21058 pop_scope (pushed_scope);
21059
21060 /* Now do some post-NSDMI bookkeeping. */
21061 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
21062 after_nsdmi_defaulted_late_checks (class_type);
21063 vec_safe_truncate (unparsed_classes, 0);
21064 after_nsdmi_defaulted_late_checks (type);
21065
21066 /* Now parse the body of the functions. */
21067 if (flag_openmp)
21068 {
21069 /* OpenMP UDRs need to be parsed before all other functions. */
21070 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21071 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
21072 cp_parser_late_parsing_for_member (parser, decl);
21073 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21074 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
21075 cp_parser_late_parsing_for_member (parser, decl);
21076 }
21077 else
21078 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21079 cp_parser_late_parsing_for_member (parser, decl);
21080 vec_safe_truncate (unparsed_funs_with_definitions, 0);
21081 }
21082 else
21083 vec_safe_push (unparsed_classes, type);
21084
21085 /* Put back any saved access checks. */
21086 pop_deferring_access_checks ();
21087
21088 /* Restore saved state. */
21089 parser->in_switch_statement_p = in_switch_statement_p;
21090 parser->in_statement = in_statement;
21091 parser->in_function_body = saved_in_function_body;
21092 parser->num_template_parameter_lists
21093 = saved_num_template_parameter_lists;
21094 parser->in_unbraced_linkage_specification_p
21095 = saved_in_unbraced_linkage_specification_p;
21096
21097 return type;
21098 }
21099
21100 static tree
21101 cp_parser_class_specifier (cp_parser* parser)
21102 {
21103 tree ret;
21104 timevar_push (TV_PARSE_STRUCT);
21105 ret = cp_parser_class_specifier_1 (parser);
21106 timevar_pop (TV_PARSE_STRUCT);
21107 return ret;
21108 }
21109
21110 /* Parse a class-head.
21111
21112 class-head:
21113 class-key identifier [opt] base-clause [opt]
21114 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21115 class-key nested-name-specifier [opt] template-id
21116 base-clause [opt]
21117
21118 class-virt-specifier:
21119 final
21120
21121 GNU Extensions:
21122 class-key attributes identifier [opt] base-clause [opt]
21123 class-key attributes nested-name-specifier identifier base-clause [opt]
21124 class-key attributes nested-name-specifier [opt] template-id
21125 base-clause [opt]
21126
21127 Upon return BASES is initialized to the list of base classes (or
21128 NULL, if there are none) in the same form returned by
21129 cp_parser_base_clause.
21130
21131 Returns the TYPE of the indicated class. Sets
21132 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21133 involving a nested-name-specifier was used, and FALSE otherwise.
21134
21135 Returns error_mark_node if this is not a class-head.
21136
21137 Returns NULL_TREE if the class-head is syntactically valid, but
21138 semantically invalid in a way that means we should skip the entire
21139 body of the class. */
21140
21141 static tree
21142 cp_parser_class_head (cp_parser* parser,
21143 bool* nested_name_specifier_p)
21144 {
21145 tree nested_name_specifier;
21146 enum tag_types class_key;
21147 tree id = NULL_TREE;
21148 tree type = NULL_TREE;
21149 tree attributes;
21150 tree bases;
21151 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21152 bool template_id_p = false;
21153 bool qualified_p = false;
21154 bool invalid_nested_name_p = false;
21155 bool invalid_explicit_specialization_p = false;
21156 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21157 tree pushed_scope = NULL_TREE;
21158 unsigned num_templates;
21159 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
21160 /* Assume no nested-name-specifier will be present. */
21161 *nested_name_specifier_p = false;
21162 /* Assume no template parameter lists will be used in defining the
21163 type. */
21164 num_templates = 0;
21165 parser->colon_corrects_to_scope_p = false;
21166
21167 /* Look for the class-key. */
21168 class_key = cp_parser_class_key (parser);
21169 if (class_key == none_type)
21170 return error_mark_node;
21171
21172 /* Parse the attributes. */
21173 attributes = cp_parser_attributes_opt (parser);
21174
21175 /* If the next token is `::', that is invalid -- but sometimes
21176 people do try to write:
21177
21178 struct ::S {};
21179
21180 Handle this gracefully by accepting the extra qualifier, and then
21181 issuing an error about it later if this really is a
21182 class-head. If it turns out just to be an elaborated type
21183 specifier, remain silent. */
21184 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
21185 qualified_p = true;
21186
21187 push_deferring_access_checks (dk_no_check);
21188
21189 /* Determine the name of the class. Begin by looking for an
21190 optional nested-name-specifier. */
21191 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
21192 nested_name_specifier
21193 = cp_parser_nested_name_specifier_opt (parser,
21194 /*typename_keyword_p=*/false,
21195 /*check_dependency_p=*/false,
21196 /*type_p=*/true,
21197 /*is_declaration=*/false);
21198 /* If there was a nested-name-specifier, then there *must* be an
21199 identifier. */
21200 if (nested_name_specifier)
21201 {
21202 type_start_token = cp_lexer_peek_token (parser->lexer);
21203 /* Although the grammar says `identifier', it really means
21204 `class-name' or `template-name'. You are only allowed to
21205 define a class that has already been declared with this
21206 syntax.
21207
21208 The proposed resolution for Core Issue 180 says that wherever
21209 you see `class T::X' you should treat `X' as a type-name.
21210
21211 It is OK to define an inaccessible class; for example:
21212
21213 class A { class B; };
21214 class A::B {};
21215
21216 We do not know if we will see a class-name, or a
21217 template-name. We look for a class-name first, in case the
21218 class-name is a template-id; if we looked for the
21219 template-name first we would stop after the template-name. */
21220 cp_parser_parse_tentatively (parser);
21221 type = cp_parser_class_name (parser,
21222 /*typename_keyword_p=*/false,
21223 /*template_keyword_p=*/false,
21224 class_type,
21225 /*check_dependency_p=*/false,
21226 /*class_head_p=*/true,
21227 /*is_declaration=*/false);
21228 /* If that didn't work, ignore the nested-name-specifier. */
21229 if (!cp_parser_parse_definitely (parser))
21230 {
21231 invalid_nested_name_p = true;
21232 type_start_token = cp_lexer_peek_token (parser->lexer);
21233 id = cp_parser_identifier (parser);
21234 if (id == error_mark_node)
21235 id = NULL_TREE;
21236 }
21237 /* If we could not find a corresponding TYPE, treat this
21238 declaration like an unqualified declaration. */
21239 if (type == error_mark_node)
21240 nested_name_specifier = NULL_TREE;
21241 /* Otherwise, count the number of templates used in TYPE and its
21242 containing scopes. */
21243 else
21244 {
21245 tree scope;
21246
21247 for (scope = TREE_TYPE (type);
21248 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21249 scope = get_containing_scope (scope))
21250 if (TYPE_P (scope)
21251 && CLASS_TYPE_P (scope)
21252 && CLASSTYPE_TEMPLATE_INFO (scope)
21253 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
21254 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
21255 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
21256 ++num_templates;
21257 }
21258 }
21259 /* Otherwise, the identifier is optional. */
21260 else
21261 {
21262 /* We don't know whether what comes next is a template-id,
21263 an identifier, or nothing at all. */
21264 cp_parser_parse_tentatively (parser);
21265 /* Check for a template-id. */
21266 type_start_token = cp_lexer_peek_token (parser->lexer);
21267 id = cp_parser_template_id (parser,
21268 /*template_keyword_p=*/false,
21269 /*check_dependency_p=*/true,
21270 class_key,
21271 /*is_declaration=*/true);
21272 /* If that didn't work, it could still be an identifier. */
21273 if (!cp_parser_parse_definitely (parser))
21274 {
21275 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21276 {
21277 type_start_token = cp_lexer_peek_token (parser->lexer);
21278 id = cp_parser_identifier (parser);
21279 }
21280 else
21281 id = NULL_TREE;
21282 }
21283 else
21284 {
21285 template_id_p = true;
21286 ++num_templates;
21287 }
21288 }
21289
21290 pop_deferring_access_checks ();
21291
21292 if (id)
21293 {
21294 cp_parser_check_for_invalid_template_id (parser, id,
21295 class_key,
21296 type_start_token->location);
21297 }
21298 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21299
21300 /* If it's not a `:' or a `{' then we can't really be looking at a
21301 class-head, since a class-head only appears as part of a
21302 class-specifier. We have to detect this situation before calling
21303 xref_tag, since that has irreversible side-effects. */
21304 if (!cp_parser_next_token_starts_class_definition_p (parser))
21305 {
21306 cp_parser_error (parser, "expected %<{%> or %<:%>");
21307 type = error_mark_node;
21308 goto out;
21309 }
21310
21311 /* At this point, we're going ahead with the class-specifier, even
21312 if some other problem occurs. */
21313 cp_parser_commit_to_tentative_parse (parser);
21314 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
21315 {
21316 cp_parser_error (parser,
21317 "cannot specify %<override%> for a class");
21318 type = error_mark_node;
21319 goto out;
21320 }
21321 /* Issue the error about the overly-qualified name now. */
21322 if (qualified_p)
21323 {
21324 cp_parser_error (parser,
21325 "global qualification of class name is invalid");
21326 type = error_mark_node;
21327 goto out;
21328 }
21329 else if (invalid_nested_name_p)
21330 {
21331 cp_parser_error (parser,
21332 "qualified name does not name a class");
21333 type = error_mark_node;
21334 goto out;
21335 }
21336 else if (nested_name_specifier)
21337 {
21338 tree scope;
21339
21340 /* Reject typedef-names in class heads. */
21341 if (!DECL_IMPLICIT_TYPEDEF_P (type))
21342 {
21343 error_at (type_start_token->location,
21344 "invalid class name in declaration of %qD",
21345 type);
21346 type = NULL_TREE;
21347 goto done;
21348 }
21349
21350 /* Figure out in what scope the declaration is being placed. */
21351 scope = current_scope ();
21352 /* If that scope does not contain the scope in which the
21353 class was originally declared, the program is invalid. */
21354 if (scope && !is_ancestor (scope, nested_name_specifier))
21355 {
21356 if (at_namespace_scope_p ())
21357 error_at (type_start_token->location,
21358 "declaration of %qD in namespace %qD which does not "
21359 "enclose %qD",
21360 type, scope, nested_name_specifier);
21361 else
21362 error_at (type_start_token->location,
21363 "declaration of %qD in %qD which does not enclose %qD",
21364 type, scope, nested_name_specifier);
21365 type = NULL_TREE;
21366 goto done;
21367 }
21368 /* [dcl.meaning]
21369
21370 A declarator-id shall not be qualified except for the
21371 definition of a ... nested class outside of its class
21372 ... [or] the definition or explicit instantiation of a
21373 class member of a namespace outside of its namespace. */
21374 if (scope == nested_name_specifier)
21375 {
21376 permerror (nested_name_specifier_token_start->location,
21377 "extra qualification not allowed");
21378 nested_name_specifier = NULL_TREE;
21379 num_templates = 0;
21380 }
21381 }
21382 /* An explicit-specialization must be preceded by "template <>". If
21383 it is not, try to recover gracefully. */
21384 if (at_namespace_scope_p ()
21385 && parser->num_template_parameter_lists == 0
21386 && template_id_p)
21387 {
21388 error_at (type_start_token->location,
21389 "an explicit specialization must be preceded by %<template <>%>");
21390 invalid_explicit_specialization_p = true;
21391 /* Take the same action that would have been taken by
21392 cp_parser_explicit_specialization. */
21393 ++parser->num_template_parameter_lists;
21394 begin_specialization ();
21395 }
21396 /* There must be no "return" statements between this point and the
21397 end of this function; set "type "to the correct return value and
21398 use "goto done;" to return. */
21399 /* Make sure that the right number of template parameters were
21400 present. */
21401 if (!cp_parser_check_template_parameters (parser, num_templates,
21402 type_start_token->location,
21403 /*declarator=*/NULL))
21404 {
21405 /* If something went wrong, there is no point in even trying to
21406 process the class-definition. */
21407 type = NULL_TREE;
21408 goto done;
21409 }
21410
21411 /* Look up the type. */
21412 if (template_id_p)
21413 {
21414 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
21415 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
21416 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
21417 {
21418 error_at (type_start_token->location,
21419 "function template %qD redeclared as a class template", id);
21420 type = error_mark_node;
21421 }
21422 else
21423 {
21424 type = TREE_TYPE (id);
21425 type = maybe_process_partial_specialization (type);
21426 }
21427 if (nested_name_specifier)
21428 pushed_scope = push_scope (nested_name_specifier);
21429 }
21430 else if (nested_name_specifier)
21431 {
21432 tree class_type;
21433
21434 /* Given:
21435
21436 template <typename T> struct S { struct T };
21437 template <typename T> struct S<T>::T { };
21438
21439 we will get a TYPENAME_TYPE when processing the definition of
21440 `S::T'. We need to resolve it to the actual type before we
21441 try to define it. */
21442 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
21443 {
21444 class_type = resolve_typename_type (TREE_TYPE (type),
21445 /*only_current_p=*/false);
21446 if (TREE_CODE (class_type) != TYPENAME_TYPE)
21447 type = TYPE_NAME (class_type);
21448 else
21449 {
21450 cp_parser_error (parser, "could not resolve typename type");
21451 type = error_mark_node;
21452 }
21453 }
21454
21455 if (maybe_process_partial_specialization (TREE_TYPE (type))
21456 == error_mark_node)
21457 {
21458 type = NULL_TREE;
21459 goto done;
21460 }
21461
21462 class_type = current_class_type;
21463 /* Enter the scope indicated by the nested-name-specifier. */
21464 pushed_scope = push_scope (nested_name_specifier);
21465 /* Get the canonical version of this type. */
21466 type = TYPE_MAIN_DECL (TREE_TYPE (type));
21467 /* Call push_template_decl if it seems like we should be defining a
21468 template either from the template headers or the type we're
21469 defining, so that we diagnose both extra and missing headers. */
21470 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21471 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
21472 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
21473 {
21474 type = push_template_decl (type);
21475 if (type == error_mark_node)
21476 {
21477 type = NULL_TREE;
21478 goto done;
21479 }
21480 }
21481
21482 type = TREE_TYPE (type);
21483 *nested_name_specifier_p = true;
21484 }
21485 else /* The name is not a nested name. */
21486 {
21487 /* If the class was unnamed, create a dummy name. */
21488 if (!id)
21489 id = make_anon_name ();
21490 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
21491 parser->num_template_parameter_lists);
21492 }
21493
21494 /* Indicate whether this class was declared as a `class' or as a
21495 `struct'. */
21496 if (TREE_CODE (type) == RECORD_TYPE)
21497 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
21498 cp_parser_check_class_key (class_key, type);
21499
21500 /* If this type was already complete, and we see another definition,
21501 that's an error. */
21502 if (type != error_mark_node && COMPLETE_TYPE_P (type))
21503 {
21504 error_at (type_start_token->location, "redefinition of %q#T",
21505 type);
21506 error_at (type_start_token->location, "previous definition of %q+#T",
21507 type);
21508 type = NULL_TREE;
21509 goto done;
21510 }
21511 else if (type == error_mark_node)
21512 type = NULL_TREE;
21513
21514 if (type)
21515 {
21516 /* Apply attributes now, before any use of the class as a template
21517 argument in its base list. */
21518 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
21519 fixup_attribute_variants (type);
21520 }
21521
21522 /* We will have entered the scope containing the class; the names of
21523 base classes should be looked up in that context. For example:
21524
21525 struct A { struct B {}; struct C; };
21526 struct A::C : B {};
21527
21528 is valid. */
21529
21530 /* Get the list of base-classes, if there is one. */
21531 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21532 {
21533 /* PR59482: enter the class scope so that base-specifiers are looked
21534 up correctly. */
21535 if (type)
21536 pushclass (type);
21537 bases = cp_parser_base_clause (parser);
21538 /* PR59482: get out of the previously pushed class scope so that the
21539 subsequent pops pop the right thing. */
21540 if (type)
21541 popclass ();
21542 }
21543 else
21544 bases = NULL_TREE;
21545
21546 /* If we're really defining a class, process the base classes.
21547 If they're invalid, fail. */
21548 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21549 && !xref_basetypes (type, bases))
21550 type = NULL_TREE;
21551
21552 done:
21553 /* Leave the scope given by the nested-name-specifier. We will
21554 enter the class scope itself while processing the members. */
21555 if (pushed_scope)
21556 pop_scope (pushed_scope);
21557
21558 if (invalid_explicit_specialization_p)
21559 {
21560 end_specialization ();
21561 --parser->num_template_parameter_lists;
21562 }
21563
21564 if (type)
21565 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21566 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
21567 CLASSTYPE_FINAL (type) = 1;
21568 out:
21569 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21570 return type;
21571 }
21572
21573 /* Parse a class-key.
21574
21575 class-key:
21576 class
21577 struct
21578 union
21579
21580 Returns the kind of class-key specified, or none_type to indicate
21581 error. */
21582
21583 static enum tag_types
21584 cp_parser_class_key (cp_parser* parser)
21585 {
21586 cp_token *token;
21587 enum tag_types tag_type;
21588
21589 /* Look for the class-key. */
21590 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
21591 if (!token)
21592 return none_type;
21593
21594 /* Check to see if the TOKEN is a class-key. */
21595 tag_type = cp_parser_token_is_class_key (token);
21596 if (!tag_type)
21597 cp_parser_error (parser, "expected class-key");
21598 return tag_type;
21599 }
21600
21601 /* Parse a type-parameter-key.
21602
21603 type-parameter-key:
21604 class
21605 typename
21606 */
21607
21608 static void
21609 cp_parser_type_parameter_key (cp_parser* parser)
21610 {
21611 /* Look for the type-parameter-key. */
21612 enum tag_types tag_type = none_type;
21613 cp_token *token = cp_lexer_peek_token (parser->lexer);
21614 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
21615 {
21616 cp_lexer_consume_token (parser->lexer);
21617 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
21618 /* typename is not allowed in a template template parameter
21619 by the standard until C++1Z. */
21620 pedwarn (token->location, OPT_Wpedantic,
21621 "ISO C++ forbids typename key in template template parameter;"
21622 " use -std=c++1z or -std=gnu++1z");
21623 }
21624 else
21625 cp_parser_error (parser, "expected %<class%> or %<typename%>");
21626
21627 return;
21628 }
21629
21630 /* Parse an (optional) member-specification.
21631
21632 member-specification:
21633 member-declaration member-specification [opt]
21634 access-specifier : member-specification [opt] */
21635
21636 static void
21637 cp_parser_member_specification_opt (cp_parser* parser)
21638 {
21639 while (true)
21640 {
21641 cp_token *token;
21642 enum rid keyword;
21643
21644 /* Peek at the next token. */
21645 token = cp_lexer_peek_token (parser->lexer);
21646 /* If it's a `}', or EOF then we've seen all the members. */
21647 if (token->type == CPP_CLOSE_BRACE
21648 || token->type == CPP_EOF
21649 || token->type == CPP_PRAGMA_EOL)
21650 break;
21651
21652 /* See if this token is a keyword. */
21653 keyword = token->keyword;
21654 switch (keyword)
21655 {
21656 case RID_PUBLIC:
21657 case RID_PROTECTED:
21658 case RID_PRIVATE:
21659 /* Consume the access-specifier. */
21660 cp_lexer_consume_token (parser->lexer);
21661 /* Remember which access-specifier is active. */
21662 current_access_specifier = token->u.value;
21663 /* Look for the `:'. */
21664 cp_parser_require (parser, CPP_COLON, RT_COLON);
21665 break;
21666
21667 default:
21668 /* Accept #pragmas at class scope. */
21669 if (token->type == CPP_PRAGMA)
21670 {
21671 cp_parser_pragma (parser, pragma_member);
21672 break;
21673 }
21674
21675 /* Otherwise, the next construction must be a
21676 member-declaration. */
21677 cp_parser_member_declaration (parser);
21678 }
21679 }
21680 }
21681
21682 /* Parse a member-declaration.
21683
21684 member-declaration:
21685 decl-specifier-seq [opt] member-declarator-list [opt] ;
21686 function-definition ; [opt]
21687 :: [opt] nested-name-specifier template [opt] unqualified-id ;
21688 using-declaration
21689 template-declaration
21690 alias-declaration
21691
21692 member-declarator-list:
21693 member-declarator
21694 member-declarator-list , member-declarator
21695
21696 member-declarator:
21697 declarator pure-specifier [opt]
21698 declarator constant-initializer [opt]
21699 identifier [opt] : constant-expression
21700
21701 GNU Extensions:
21702
21703 member-declaration:
21704 __extension__ member-declaration
21705
21706 member-declarator:
21707 declarator attributes [opt] pure-specifier [opt]
21708 declarator attributes [opt] constant-initializer [opt]
21709 identifier [opt] attributes [opt] : constant-expression
21710
21711 C++0x Extensions:
21712
21713 member-declaration:
21714 static_assert-declaration */
21715
21716 static void
21717 cp_parser_member_declaration (cp_parser* parser)
21718 {
21719 cp_decl_specifier_seq decl_specifiers;
21720 tree prefix_attributes;
21721 tree decl;
21722 int declares_class_or_enum;
21723 bool friend_p;
21724 cp_token *token = NULL;
21725 cp_token *decl_spec_token_start = NULL;
21726 cp_token *initializer_token_start = NULL;
21727 int saved_pedantic;
21728 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21729
21730 /* Check for the `__extension__' keyword. */
21731 if (cp_parser_extension_opt (parser, &saved_pedantic))
21732 {
21733 /* Recurse. */
21734 cp_parser_member_declaration (parser);
21735 /* Restore the old value of the PEDANTIC flag. */
21736 pedantic = saved_pedantic;
21737
21738 return;
21739 }
21740
21741 /* Check for a template-declaration. */
21742 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21743 {
21744 /* An explicit specialization here is an error condition, and we
21745 expect the specialization handler to detect and report this. */
21746 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
21747 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
21748 cp_parser_explicit_specialization (parser);
21749 else
21750 cp_parser_template_declaration (parser, /*member_p=*/true);
21751
21752 return;
21753 }
21754 /* Check for a template introduction. */
21755 else if (cp_parser_template_declaration_after_export (parser, true))
21756 return;
21757
21758 /* Check for a using-declaration. */
21759 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21760 {
21761 if (cxx_dialect < cxx11)
21762 {
21763 /* Parse the using-declaration. */
21764 cp_parser_using_declaration (parser,
21765 /*access_declaration_p=*/false);
21766 return;
21767 }
21768 else
21769 {
21770 tree decl;
21771 bool alias_decl_expected;
21772 cp_parser_parse_tentatively (parser);
21773 decl = cp_parser_alias_declaration (parser);
21774 /* Note that if we actually see the '=' token after the
21775 identifier, cp_parser_alias_declaration commits the
21776 tentative parse. In that case, we really expect an
21777 alias-declaration. Otherwise, we expect a using
21778 declaration. */
21779 alias_decl_expected =
21780 !cp_parser_uncommitted_to_tentative_parse_p (parser);
21781 cp_parser_parse_definitely (parser);
21782
21783 if (alias_decl_expected)
21784 finish_member_declaration (decl);
21785 else
21786 cp_parser_using_declaration (parser,
21787 /*access_declaration_p=*/false);
21788 return;
21789 }
21790 }
21791
21792 /* Check for @defs. */
21793 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
21794 {
21795 tree ivar, member;
21796 tree ivar_chains = cp_parser_objc_defs_expression (parser);
21797 ivar = ivar_chains;
21798 while (ivar)
21799 {
21800 member = ivar;
21801 ivar = TREE_CHAIN (member);
21802 TREE_CHAIN (member) = NULL_TREE;
21803 finish_member_declaration (member);
21804 }
21805 return;
21806 }
21807
21808 /* If the next token is `static_assert' we have a static assertion. */
21809 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
21810 {
21811 cp_parser_static_assert (parser, /*member_p=*/true);
21812 return;
21813 }
21814
21815 parser->colon_corrects_to_scope_p = false;
21816
21817 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
21818 goto out;
21819
21820 /* Parse the decl-specifier-seq. */
21821 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21822 cp_parser_decl_specifier_seq (parser,
21823 CP_PARSER_FLAGS_OPTIONAL,
21824 &decl_specifiers,
21825 &declares_class_or_enum);
21826 /* Check for an invalid type-name. */
21827 if (!decl_specifiers.any_type_specifiers_p
21828 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21829 goto out;
21830 /* If there is no declarator, then the decl-specifier-seq should
21831 specify a type. */
21832 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21833 {
21834 /* If there was no decl-specifier-seq, and the next token is a
21835 `;', then we have something like:
21836
21837 struct S { ; };
21838
21839 [class.mem]
21840
21841 Each member-declaration shall declare at least one member
21842 name of the class. */
21843 if (!decl_specifiers.any_specifiers_p)
21844 {
21845 cp_token *token = cp_lexer_peek_token (parser->lexer);
21846 if (!in_system_header_at (token->location))
21847 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
21848 }
21849 else
21850 {
21851 tree type;
21852
21853 /* See if this declaration is a friend. */
21854 friend_p = cp_parser_friend_p (&decl_specifiers);
21855 /* If there were decl-specifiers, check to see if there was
21856 a class-declaration. */
21857 type = check_tag_decl (&decl_specifiers,
21858 /*explicit_type_instantiation_p=*/false);
21859 /* Nested classes have already been added to the class, but
21860 a `friend' needs to be explicitly registered. */
21861 if (friend_p)
21862 {
21863 /* If the `friend' keyword was present, the friend must
21864 be introduced with a class-key. */
21865 if (!declares_class_or_enum && cxx_dialect < cxx11)
21866 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
21867 "in C++03 a class-key must be used "
21868 "when declaring a friend");
21869 /* In this case:
21870
21871 template <typename T> struct A {
21872 friend struct A<T>::B;
21873 };
21874
21875 A<T>::B will be represented by a TYPENAME_TYPE, and
21876 therefore not recognized by check_tag_decl. */
21877 if (!type)
21878 {
21879 type = decl_specifiers.type;
21880 if (type && TREE_CODE (type) == TYPE_DECL)
21881 type = TREE_TYPE (type);
21882 }
21883 if (!type || !TYPE_P (type))
21884 error_at (decl_spec_token_start->location,
21885 "friend declaration does not name a class or "
21886 "function");
21887 else
21888 make_friend_class (current_class_type, type,
21889 /*complain=*/true);
21890 }
21891 /* If there is no TYPE, an error message will already have
21892 been issued. */
21893 else if (!type || type == error_mark_node)
21894 ;
21895 /* An anonymous aggregate has to be handled specially; such
21896 a declaration really declares a data member (with a
21897 particular type), as opposed to a nested class. */
21898 else if (ANON_AGGR_TYPE_P (type))
21899 {
21900 /* C++11 9.5/6. */
21901 if (decl_specifiers.storage_class != sc_none)
21902 error_at (decl_spec_token_start->location,
21903 "a storage class on an anonymous aggregate "
21904 "in class scope is not allowed");
21905
21906 /* Remove constructors and such from TYPE, now that we
21907 know it is an anonymous aggregate. */
21908 fixup_anonymous_aggr (type);
21909 /* And make the corresponding data member. */
21910 decl = build_decl (decl_spec_token_start->location,
21911 FIELD_DECL, NULL_TREE, type);
21912 /* Add it to the class. */
21913 finish_member_declaration (decl);
21914 }
21915 else
21916 cp_parser_check_access_in_redeclaration
21917 (TYPE_NAME (type),
21918 decl_spec_token_start->location);
21919 }
21920 }
21921 else
21922 {
21923 bool assume_semicolon = false;
21924 bool first = true;
21925
21926 /* Clear attributes from the decl_specifiers but keep them
21927 around as prefix attributes that apply them to the entity
21928 being declared. */
21929 prefix_attributes = decl_specifiers.attributes;
21930 decl_specifiers.attributes = NULL_TREE;
21931
21932 /* See if these declarations will be friends. */
21933 friend_p = cp_parser_friend_p (&decl_specifiers);
21934
21935 /* Keep going until we hit the `;' at the end of the
21936 declaration. */
21937 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21938 {
21939 tree attributes = NULL_TREE;
21940 tree first_attribute;
21941
21942 /* Peek at the next token. */
21943 token = cp_lexer_peek_token (parser->lexer);
21944
21945 /* Check for a bitfield declaration. */
21946 if (token->type == CPP_COLON
21947 || (token->type == CPP_NAME
21948 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
21949 == CPP_COLON))
21950 {
21951 tree identifier;
21952 tree width;
21953
21954 /* Get the name of the bitfield. Note that we cannot just
21955 check TOKEN here because it may have been invalidated by
21956 the call to cp_lexer_peek_nth_token above. */
21957 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
21958 identifier = cp_parser_identifier (parser);
21959 else
21960 identifier = NULL_TREE;
21961
21962 /* Consume the `:' token. */
21963 cp_lexer_consume_token (parser->lexer);
21964 /* Get the width of the bitfield. */
21965 width
21966 = cp_parser_constant_expression (parser);
21967
21968 /* Look for attributes that apply to the bitfield. */
21969 attributes = cp_parser_attributes_opt (parser);
21970 /* Remember which attributes are prefix attributes and
21971 which are not. */
21972 first_attribute = attributes;
21973 /* Combine the attributes. */
21974 attributes = chainon (prefix_attributes, attributes);
21975
21976 /* Create the bitfield declaration. */
21977 decl = grokbitfield (identifier
21978 ? make_id_declarator (NULL_TREE,
21979 identifier,
21980 sfk_none)
21981 : NULL,
21982 &decl_specifiers,
21983 width,
21984 attributes);
21985 }
21986 else
21987 {
21988 cp_declarator *declarator;
21989 tree initializer;
21990 tree asm_specification;
21991 int ctor_dtor_or_conv_p;
21992
21993 /* Parse the declarator. */
21994 declarator
21995 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21996 &ctor_dtor_or_conv_p,
21997 /*parenthesized_p=*/NULL,
21998 /*member_p=*/true,
21999 friend_p);
22000
22001 /* If something went wrong parsing the declarator, make sure
22002 that we at least consume some tokens. */
22003 if (declarator == cp_error_declarator)
22004 {
22005 /* Skip to the end of the statement. */
22006 cp_parser_skip_to_end_of_statement (parser);
22007 /* If the next token is not a semicolon, that is
22008 probably because we just skipped over the body of
22009 a function. So, we consume a semicolon if
22010 present, but do not issue an error message if it
22011 is not present. */
22012 if (cp_lexer_next_token_is (parser->lexer,
22013 CPP_SEMICOLON))
22014 cp_lexer_consume_token (parser->lexer);
22015 goto out;
22016 }
22017
22018 if (declares_class_or_enum & 2)
22019 cp_parser_check_for_definition_in_return_type
22020 (declarator, decl_specifiers.type,
22021 decl_specifiers.locations[ds_type_spec]);
22022
22023 /* Look for an asm-specification. */
22024 asm_specification = cp_parser_asm_specification_opt (parser);
22025 /* Look for attributes that apply to the declaration. */
22026 attributes = cp_parser_attributes_opt (parser);
22027 /* Remember which attributes are prefix attributes and
22028 which are not. */
22029 first_attribute = attributes;
22030 /* Combine the attributes. */
22031 attributes = chainon (prefix_attributes, attributes);
22032
22033 /* If it's an `=', then we have a constant-initializer or a
22034 pure-specifier. It is not correct to parse the
22035 initializer before registering the member declaration
22036 since the member declaration should be in scope while
22037 its initializer is processed. However, the rest of the
22038 front end does not yet provide an interface that allows
22039 us to handle this correctly. */
22040 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22041 {
22042 /* In [class.mem]:
22043
22044 A pure-specifier shall be used only in the declaration of
22045 a virtual function.
22046
22047 A member-declarator can contain a constant-initializer
22048 only if it declares a static member of integral or
22049 enumeration type.
22050
22051 Therefore, if the DECLARATOR is for a function, we look
22052 for a pure-specifier; otherwise, we look for a
22053 constant-initializer. When we call `grokfield', it will
22054 perform more stringent semantics checks. */
22055 initializer_token_start = cp_lexer_peek_token (parser->lexer);
22056 if (function_declarator_p (declarator)
22057 || (decl_specifiers.type
22058 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
22059 && declarator->kind == cdk_id
22060 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
22061 == FUNCTION_TYPE)))
22062 initializer = cp_parser_pure_specifier (parser);
22063 else if (decl_specifiers.storage_class != sc_static)
22064 initializer = cp_parser_save_nsdmi (parser);
22065 else if (cxx_dialect >= cxx11)
22066 {
22067 bool nonconst;
22068 /* Don't require a constant rvalue in C++11, since we
22069 might want a reference constant. We'll enforce
22070 constancy later. */
22071 cp_lexer_consume_token (parser->lexer);
22072 /* Parse the initializer. */
22073 initializer = cp_parser_initializer_clause (parser,
22074 &nonconst);
22075 }
22076 else
22077 /* Parse the initializer. */
22078 initializer = cp_parser_constant_initializer (parser);
22079 }
22080 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22081 && !function_declarator_p (declarator))
22082 {
22083 bool x;
22084 if (decl_specifiers.storage_class != sc_static)
22085 initializer = cp_parser_save_nsdmi (parser);
22086 else
22087 initializer = cp_parser_initializer (parser, &x, &x);
22088 }
22089 /* Otherwise, there is no initializer. */
22090 else
22091 initializer = NULL_TREE;
22092
22093 /* See if we are probably looking at a function
22094 definition. We are certainly not looking at a
22095 member-declarator. Calling `grokfield' has
22096 side-effects, so we must not do it unless we are sure
22097 that we are looking at a member-declarator. */
22098 if (cp_parser_token_starts_function_definition_p
22099 (cp_lexer_peek_token (parser->lexer)))
22100 {
22101 /* The grammar does not allow a pure-specifier to be
22102 used when a member function is defined. (It is
22103 possible that this fact is an oversight in the
22104 standard, since a pure function may be defined
22105 outside of the class-specifier. */
22106 if (initializer && initializer_token_start)
22107 error_at (initializer_token_start->location,
22108 "pure-specifier on function-definition");
22109 decl = cp_parser_save_member_function_body (parser,
22110 &decl_specifiers,
22111 declarator,
22112 attributes,
22113 first);
22114 first = false;
22115
22116 if (parser->fully_implicit_function_template_p)
22117 decl = finish_fully_implicit_template (parser, decl);
22118 /* If the member was not a friend, declare it here. */
22119 if (!friend_p)
22120 finish_member_declaration (decl);
22121 /* Peek at the next token. */
22122 token = cp_lexer_peek_token (parser->lexer);
22123 /* If the next token is a semicolon, consume it. */
22124 if (token->type == CPP_SEMICOLON)
22125 cp_lexer_consume_token (parser->lexer);
22126 goto out;
22127 }
22128 else
22129 if (declarator->kind == cdk_function)
22130 declarator->id_loc = token->location;
22131 /* Create the declaration. */
22132 decl = grokfield (declarator, &decl_specifiers,
22133 initializer, /*init_const_expr_p=*/true,
22134 asm_specification, attributes);
22135 if (parser->fully_implicit_function_template_p)
22136 {
22137 if (friend_p)
22138 finish_fully_implicit_template (parser, 0);
22139 else
22140 decl = finish_fully_implicit_template (parser, decl);
22141 }
22142 }
22143
22144 cp_finalize_omp_declare_simd (parser, decl);
22145 cp_finalize_oacc_routine (parser, decl, false, first);
22146 first = false;
22147
22148 /* Reset PREFIX_ATTRIBUTES. */
22149 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22150 attributes = TREE_CHAIN (attributes);
22151 if (attributes)
22152 TREE_CHAIN (attributes) = NULL_TREE;
22153
22154 /* If there is any qualification still in effect, clear it
22155 now; we will be starting fresh with the next declarator. */
22156 parser->scope = NULL_TREE;
22157 parser->qualifying_scope = NULL_TREE;
22158 parser->object_scope = NULL_TREE;
22159 /* If it's a `,', then there are more declarators. */
22160 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22161 {
22162 cp_lexer_consume_token (parser->lexer);
22163 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22164 {
22165 cp_token *token = cp_lexer_previous_token (parser->lexer);
22166 error_at (token->location,
22167 "stray %<,%> at end of member declaration");
22168 }
22169 }
22170 /* If the next token isn't a `;', then we have a parse error. */
22171 else if (cp_lexer_next_token_is_not (parser->lexer,
22172 CPP_SEMICOLON))
22173 {
22174 /* The next token might be a ways away from where the
22175 actual semicolon is missing. Find the previous token
22176 and use that for our error position. */
22177 cp_token *token = cp_lexer_previous_token (parser->lexer);
22178 error_at (token->location,
22179 "expected %<;%> at end of member declaration");
22180
22181 /* Assume that the user meant to provide a semicolon. If
22182 we were to cp_parser_skip_to_end_of_statement, we might
22183 skip to a semicolon inside a member function definition
22184 and issue nonsensical error messages. */
22185 assume_semicolon = true;
22186 }
22187
22188 if (decl)
22189 {
22190 /* Add DECL to the list of members. */
22191 if (!friend_p
22192 /* Explicitly include, eg, NSDMIs, for better error
22193 recovery (c++/58650). */
22194 || !DECL_DECLARES_FUNCTION_P (decl))
22195 finish_member_declaration (decl);
22196
22197 if (TREE_CODE (decl) == FUNCTION_DECL)
22198 cp_parser_save_default_args (parser, decl);
22199 else if (TREE_CODE (decl) == FIELD_DECL
22200 && !DECL_C_BIT_FIELD (decl)
22201 && DECL_INITIAL (decl))
22202 /* Add DECL to the queue of NSDMI to be parsed later. */
22203 vec_safe_push (unparsed_nsdmis, decl);
22204 }
22205
22206 if (assume_semicolon)
22207 goto out;
22208 }
22209
22210 /* Reset any OpenACC routine clauses. */
22211 parser->oacc_routine = NULL;
22212 }
22213
22214 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22215 out:
22216 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22217 }
22218
22219 /* Parse a pure-specifier.
22220
22221 pure-specifier:
22222 = 0
22223
22224 Returns INTEGER_ZERO_NODE if a pure specifier is found.
22225 Otherwise, ERROR_MARK_NODE is returned. */
22226
22227 static tree
22228 cp_parser_pure_specifier (cp_parser* parser)
22229 {
22230 cp_token *token;
22231
22232 /* Look for the `=' token. */
22233 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22234 return error_mark_node;
22235 /* Look for the `0' token. */
22236 token = cp_lexer_peek_token (parser->lexer);
22237
22238 if (token->type == CPP_EOF
22239 || token->type == CPP_PRAGMA_EOL)
22240 return error_mark_node;
22241
22242 cp_lexer_consume_token (parser->lexer);
22243
22244 /* Accept = default or = delete in c++0x mode. */
22245 if (token->keyword == RID_DEFAULT
22246 || token->keyword == RID_DELETE)
22247 {
22248 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
22249 return token->u.value;
22250 }
22251
22252 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
22253 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
22254 {
22255 cp_parser_error (parser,
22256 "invalid pure specifier (only %<= 0%> is allowed)");
22257 cp_parser_skip_to_end_of_statement (parser);
22258 return error_mark_node;
22259 }
22260 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22261 {
22262 error_at (token->location, "templates may not be %<virtual%>");
22263 return error_mark_node;
22264 }
22265
22266 return integer_zero_node;
22267 }
22268
22269 /* Parse a constant-initializer.
22270
22271 constant-initializer:
22272 = constant-expression
22273
22274 Returns a representation of the constant-expression. */
22275
22276 static tree
22277 cp_parser_constant_initializer (cp_parser* parser)
22278 {
22279 /* Look for the `=' token. */
22280 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22281 return error_mark_node;
22282
22283 /* It is invalid to write:
22284
22285 struct S { static const int i = { 7 }; };
22286
22287 */
22288 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22289 {
22290 cp_parser_error (parser,
22291 "a brace-enclosed initializer is not allowed here");
22292 /* Consume the opening brace. */
22293 cp_lexer_consume_token (parser->lexer);
22294 /* Skip the initializer. */
22295 cp_parser_skip_to_closing_brace (parser);
22296 /* Look for the trailing `}'. */
22297 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22298
22299 return error_mark_node;
22300 }
22301
22302 return cp_parser_constant_expression (parser);
22303 }
22304
22305 /* Derived classes [gram.class.derived] */
22306
22307 /* Parse a base-clause.
22308
22309 base-clause:
22310 : base-specifier-list
22311
22312 base-specifier-list:
22313 base-specifier ... [opt]
22314 base-specifier-list , base-specifier ... [opt]
22315
22316 Returns a TREE_LIST representing the base-classes, in the order in
22317 which they were declared. The representation of each node is as
22318 described by cp_parser_base_specifier.
22319
22320 In the case that no bases are specified, this function will return
22321 NULL_TREE, not ERROR_MARK_NODE. */
22322
22323 static tree
22324 cp_parser_base_clause (cp_parser* parser)
22325 {
22326 tree bases = NULL_TREE;
22327
22328 /* Look for the `:' that begins the list. */
22329 cp_parser_require (parser, CPP_COLON, RT_COLON);
22330
22331 /* Scan the base-specifier-list. */
22332 while (true)
22333 {
22334 cp_token *token;
22335 tree base;
22336 bool pack_expansion_p = false;
22337
22338 /* Look for the base-specifier. */
22339 base = cp_parser_base_specifier (parser);
22340 /* Look for the (optional) ellipsis. */
22341 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22342 {
22343 /* Consume the `...'. */
22344 cp_lexer_consume_token (parser->lexer);
22345
22346 pack_expansion_p = true;
22347 }
22348
22349 /* Add BASE to the front of the list. */
22350 if (base && base != error_mark_node)
22351 {
22352 if (pack_expansion_p)
22353 /* Make this a pack expansion type. */
22354 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
22355
22356 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
22357 {
22358 TREE_CHAIN (base) = bases;
22359 bases = base;
22360 }
22361 }
22362 /* Peek at the next token. */
22363 token = cp_lexer_peek_token (parser->lexer);
22364 /* If it's not a comma, then the list is complete. */
22365 if (token->type != CPP_COMMA)
22366 break;
22367 /* Consume the `,'. */
22368 cp_lexer_consume_token (parser->lexer);
22369 }
22370
22371 /* PARSER->SCOPE may still be non-NULL at this point, if the last
22372 base class had a qualified name. However, the next name that
22373 appears is certainly not qualified. */
22374 parser->scope = NULL_TREE;
22375 parser->qualifying_scope = NULL_TREE;
22376 parser->object_scope = NULL_TREE;
22377
22378 return nreverse (bases);
22379 }
22380
22381 /* Parse a base-specifier.
22382
22383 base-specifier:
22384 :: [opt] nested-name-specifier [opt] class-name
22385 virtual access-specifier [opt] :: [opt] nested-name-specifier
22386 [opt] class-name
22387 access-specifier virtual [opt] :: [opt] nested-name-specifier
22388 [opt] class-name
22389
22390 Returns a TREE_LIST. The TREE_PURPOSE will be one of
22391 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22392 indicate the specifiers provided. The TREE_VALUE will be a TYPE
22393 (or the ERROR_MARK_NODE) indicating the type that was specified. */
22394
22395 static tree
22396 cp_parser_base_specifier (cp_parser* parser)
22397 {
22398 cp_token *token;
22399 bool done = false;
22400 bool virtual_p = false;
22401 bool duplicate_virtual_error_issued_p = false;
22402 bool duplicate_access_error_issued_p = false;
22403 bool class_scope_p, template_p;
22404 tree access = access_default_node;
22405 tree type;
22406
22407 /* Process the optional `virtual' and `access-specifier'. */
22408 while (!done)
22409 {
22410 /* Peek at the next token. */
22411 token = cp_lexer_peek_token (parser->lexer);
22412 /* Process `virtual'. */
22413 switch (token->keyword)
22414 {
22415 case RID_VIRTUAL:
22416 /* If `virtual' appears more than once, issue an error. */
22417 if (virtual_p && !duplicate_virtual_error_issued_p)
22418 {
22419 cp_parser_error (parser,
22420 "%<virtual%> specified more than once in base-specified");
22421 duplicate_virtual_error_issued_p = true;
22422 }
22423
22424 virtual_p = true;
22425
22426 /* Consume the `virtual' token. */
22427 cp_lexer_consume_token (parser->lexer);
22428
22429 break;
22430
22431 case RID_PUBLIC:
22432 case RID_PROTECTED:
22433 case RID_PRIVATE:
22434 /* If more than one access specifier appears, issue an
22435 error. */
22436 if (access != access_default_node
22437 && !duplicate_access_error_issued_p)
22438 {
22439 cp_parser_error (parser,
22440 "more than one access specifier in base-specified");
22441 duplicate_access_error_issued_p = true;
22442 }
22443
22444 access = ridpointers[(int) token->keyword];
22445
22446 /* Consume the access-specifier. */
22447 cp_lexer_consume_token (parser->lexer);
22448
22449 break;
22450
22451 default:
22452 done = true;
22453 break;
22454 }
22455 }
22456 /* It is not uncommon to see programs mechanically, erroneously, use
22457 the 'typename' keyword to denote (dependent) qualified types
22458 as base classes. */
22459 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
22460 {
22461 token = cp_lexer_peek_token (parser->lexer);
22462 if (!processing_template_decl)
22463 error_at (token->location,
22464 "keyword %<typename%> not allowed outside of templates");
22465 else
22466 error_at (token->location,
22467 "keyword %<typename%> not allowed in this context "
22468 "(the base class is implicitly a type)");
22469 cp_lexer_consume_token (parser->lexer);
22470 }
22471
22472 /* Look for the optional `::' operator. */
22473 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22474 /* Look for the nested-name-specifier. The simplest way to
22475 implement:
22476
22477 [temp.res]
22478
22479 The keyword `typename' is not permitted in a base-specifier or
22480 mem-initializer; in these contexts a qualified name that
22481 depends on a template-parameter is implicitly assumed to be a
22482 type name.
22483
22484 is to pretend that we have seen the `typename' keyword at this
22485 point. */
22486 cp_parser_nested_name_specifier_opt (parser,
22487 /*typename_keyword_p=*/true,
22488 /*check_dependency_p=*/true,
22489 typename_type,
22490 /*is_declaration=*/true);
22491 /* If the base class is given by a qualified name, assume that names
22492 we see are type names or templates, as appropriate. */
22493 class_scope_p = (parser->scope && TYPE_P (parser->scope));
22494 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
22495
22496 if (!parser->scope
22497 && cp_lexer_next_token_is_decltype (parser->lexer))
22498 /* DR 950 allows decltype as a base-specifier. */
22499 type = cp_parser_decltype (parser);
22500 else
22501 {
22502 /* Otherwise, look for the class-name. */
22503 type = cp_parser_class_name (parser,
22504 class_scope_p,
22505 template_p,
22506 typename_type,
22507 /*check_dependency_p=*/true,
22508 /*class_head_p=*/false,
22509 /*is_declaration=*/true);
22510 type = TREE_TYPE (type);
22511 }
22512
22513 if (type == error_mark_node)
22514 return error_mark_node;
22515
22516 return finish_base_specifier (type, access, virtual_p);
22517 }
22518
22519 /* Exception handling [gram.exception] */
22520
22521 /* Parse an (optional) noexcept-specification.
22522
22523 noexcept-specification:
22524 noexcept ( constant-expression ) [opt]
22525
22526 If no noexcept-specification is present, returns NULL_TREE.
22527 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
22528 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
22529 there are no parentheses. CONSUMED_EXPR will be set accordingly.
22530 Otherwise, returns a noexcept specification unless RETURN_COND is true,
22531 in which case a boolean condition is returned instead. */
22532
22533 static tree
22534 cp_parser_noexcept_specification_opt (cp_parser* parser,
22535 bool require_constexpr,
22536 bool* consumed_expr,
22537 bool return_cond)
22538 {
22539 cp_token *token;
22540 const char *saved_message;
22541
22542 /* Peek at the next token. */
22543 token = cp_lexer_peek_token (parser->lexer);
22544
22545 /* Is it a noexcept-specification? */
22546 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
22547 {
22548 tree expr;
22549 cp_lexer_consume_token (parser->lexer);
22550
22551 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
22552 {
22553 cp_lexer_consume_token (parser->lexer);
22554
22555 if (require_constexpr)
22556 {
22557 /* Types may not be defined in an exception-specification. */
22558 saved_message = parser->type_definition_forbidden_message;
22559 parser->type_definition_forbidden_message
22560 = G_("types may not be defined in an exception-specification");
22561
22562 expr = cp_parser_constant_expression (parser);
22563
22564 /* Restore the saved message. */
22565 parser->type_definition_forbidden_message = saved_message;
22566 }
22567 else
22568 {
22569 expr = cp_parser_expression (parser);
22570 *consumed_expr = true;
22571 }
22572
22573 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22574 }
22575 else
22576 {
22577 expr = boolean_true_node;
22578 if (!require_constexpr)
22579 *consumed_expr = false;
22580 }
22581
22582 /* We cannot build a noexcept-spec right away because this will check
22583 that expr is a constexpr. */
22584 if (!return_cond)
22585 return build_noexcept_spec (expr, tf_warning_or_error);
22586 else
22587 return expr;
22588 }
22589 else
22590 return NULL_TREE;
22591 }
22592
22593 /* Parse an (optional) exception-specification.
22594
22595 exception-specification:
22596 throw ( type-id-list [opt] )
22597
22598 Returns a TREE_LIST representing the exception-specification. The
22599 TREE_VALUE of each node is a type. */
22600
22601 static tree
22602 cp_parser_exception_specification_opt (cp_parser* parser)
22603 {
22604 cp_token *token;
22605 tree type_id_list;
22606 const char *saved_message;
22607
22608 /* Peek at the next token. */
22609 token = cp_lexer_peek_token (parser->lexer);
22610
22611 /* Is it a noexcept-specification? */
22612 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
22613 false);
22614 if (type_id_list != NULL_TREE)
22615 return type_id_list;
22616
22617 /* If it's not `throw', then there's no exception-specification. */
22618 if (!cp_parser_is_keyword (token, RID_THROW))
22619 return NULL_TREE;
22620
22621 #if 0
22622 /* Enable this once a lot of code has transitioned to noexcept? */
22623 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
22624 warning (OPT_Wdeprecated, "dynamic exception specifications are "
22625 "deprecated in C++0x; use %<noexcept%> instead");
22626 #endif
22627
22628 /* Consume the `throw'. */
22629 cp_lexer_consume_token (parser->lexer);
22630
22631 /* Look for the `('. */
22632 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22633
22634 /* Peek at the next token. */
22635 token = cp_lexer_peek_token (parser->lexer);
22636 /* If it's not a `)', then there is a type-id-list. */
22637 if (token->type != CPP_CLOSE_PAREN)
22638 {
22639 /* Types may not be defined in an exception-specification. */
22640 saved_message = parser->type_definition_forbidden_message;
22641 parser->type_definition_forbidden_message
22642 = G_("types may not be defined in an exception-specification");
22643 /* Parse the type-id-list. */
22644 type_id_list = cp_parser_type_id_list (parser);
22645 /* Restore the saved message. */
22646 parser->type_definition_forbidden_message = saved_message;
22647 }
22648 else
22649 type_id_list = empty_except_spec;
22650
22651 /* Look for the `)'. */
22652 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22653
22654 return type_id_list;
22655 }
22656
22657 /* Parse an (optional) type-id-list.
22658
22659 type-id-list:
22660 type-id ... [opt]
22661 type-id-list , type-id ... [opt]
22662
22663 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
22664 in the order that the types were presented. */
22665
22666 static tree
22667 cp_parser_type_id_list (cp_parser* parser)
22668 {
22669 tree types = NULL_TREE;
22670
22671 while (true)
22672 {
22673 cp_token *token;
22674 tree type;
22675
22676 /* Get the next type-id. */
22677 type = cp_parser_type_id (parser);
22678 /* Parse the optional ellipsis. */
22679 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22680 {
22681 /* Consume the `...'. */
22682 cp_lexer_consume_token (parser->lexer);
22683
22684 /* Turn the type into a pack expansion expression. */
22685 type = make_pack_expansion (type);
22686 }
22687 /* Add it to the list. */
22688 types = add_exception_specifier (types, type, /*complain=*/1);
22689 /* Peek at the next token. */
22690 token = cp_lexer_peek_token (parser->lexer);
22691 /* If it is not a `,', we are done. */
22692 if (token->type != CPP_COMMA)
22693 break;
22694 /* Consume the `,'. */
22695 cp_lexer_consume_token (parser->lexer);
22696 }
22697
22698 return nreverse (types);
22699 }
22700
22701 /* Parse a try-block.
22702
22703 try-block:
22704 try compound-statement handler-seq */
22705
22706 static tree
22707 cp_parser_try_block (cp_parser* parser)
22708 {
22709 tree try_block;
22710
22711 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
22712 if (parser->in_function_body
22713 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
22714 error ("%<try%> in %<constexpr%> function");
22715
22716 try_block = begin_try_block ();
22717 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
22718 finish_try_block (try_block);
22719 cp_parser_handler_seq (parser);
22720 finish_handler_sequence (try_block);
22721
22722 return try_block;
22723 }
22724
22725 /* Parse a function-try-block.
22726
22727 function-try-block:
22728 try ctor-initializer [opt] function-body handler-seq */
22729
22730 static bool
22731 cp_parser_function_try_block (cp_parser* parser)
22732 {
22733 tree compound_stmt;
22734 tree try_block;
22735 bool ctor_initializer_p;
22736
22737 /* Look for the `try' keyword. */
22738 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
22739 return false;
22740 /* Let the rest of the front end know where we are. */
22741 try_block = begin_function_try_block (&compound_stmt);
22742 /* Parse the function-body. */
22743 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
22744 (parser, /*in_function_try_block=*/true);
22745 /* We're done with the `try' part. */
22746 finish_function_try_block (try_block);
22747 /* Parse the handlers. */
22748 cp_parser_handler_seq (parser);
22749 /* We're done with the handlers. */
22750 finish_function_handler_sequence (try_block, compound_stmt);
22751
22752 return ctor_initializer_p;
22753 }
22754
22755 /* Parse a handler-seq.
22756
22757 handler-seq:
22758 handler handler-seq [opt] */
22759
22760 static void
22761 cp_parser_handler_seq (cp_parser* parser)
22762 {
22763 while (true)
22764 {
22765 cp_token *token;
22766
22767 /* Parse the handler. */
22768 cp_parser_handler (parser);
22769 /* Peek at the next token. */
22770 token = cp_lexer_peek_token (parser->lexer);
22771 /* If it's not `catch' then there are no more handlers. */
22772 if (!cp_parser_is_keyword (token, RID_CATCH))
22773 break;
22774 }
22775 }
22776
22777 /* Parse a handler.
22778
22779 handler:
22780 catch ( exception-declaration ) compound-statement */
22781
22782 static void
22783 cp_parser_handler (cp_parser* parser)
22784 {
22785 tree handler;
22786 tree declaration;
22787
22788 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
22789 handler = begin_handler ();
22790 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22791 declaration = cp_parser_exception_declaration (parser);
22792 finish_handler_parms (declaration, handler);
22793 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22794 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
22795 finish_handler (handler);
22796 }
22797
22798 /* Parse an exception-declaration.
22799
22800 exception-declaration:
22801 type-specifier-seq declarator
22802 type-specifier-seq abstract-declarator
22803 type-specifier-seq
22804 ...
22805
22806 Returns a VAR_DECL for the declaration, or NULL_TREE if the
22807 ellipsis variant is used. */
22808
22809 static tree
22810 cp_parser_exception_declaration (cp_parser* parser)
22811 {
22812 cp_decl_specifier_seq type_specifiers;
22813 cp_declarator *declarator;
22814 const char *saved_message;
22815
22816 /* If it's an ellipsis, it's easy to handle. */
22817 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22818 {
22819 /* Consume the `...' token. */
22820 cp_lexer_consume_token (parser->lexer);
22821 return NULL_TREE;
22822 }
22823
22824 /* Types may not be defined in exception-declarations. */
22825 saved_message = parser->type_definition_forbidden_message;
22826 parser->type_definition_forbidden_message
22827 = G_("types may not be defined in exception-declarations");
22828
22829 /* Parse the type-specifier-seq. */
22830 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22831 /*is_trailing_return=*/false,
22832 &type_specifiers);
22833 /* If it's a `)', then there is no declarator. */
22834 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22835 declarator = NULL;
22836 else
22837 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
22838 /*ctor_dtor_or_conv_p=*/NULL,
22839 /*parenthesized_p=*/NULL,
22840 /*member_p=*/false,
22841 /*friend_p=*/false);
22842
22843 /* Restore the saved message. */
22844 parser->type_definition_forbidden_message = saved_message;
22845
22846 if (!type_specifiers.any_specifiers_p)
22847 return error_mark_node;
22848
22849 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
22850 }
22851
22852 /* Parse a throw-expression.
22853
22854 throw-expression:
22855 throw assignment-expression [opt]
22856
22857 Returns a THROW_EXPR representing the throw-expression. */
22858
22859 static tree
22860 cp_parser_throw_expression (cp_parser* parser)
22861 {
22862 tree expression;
22863 cp_token* token;
22864
22865 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
22866 token = cp_lexer_peek_token (parser->lexer);
22867 /* Figure out whether or not there is an assignment-expression
22868 following the "throw" keyword. */
22869 if (token->type == CPP_COMMA
22870 || token->type == CPP_SEMICOLON
22871 || token->type == CPP_CLOSE_PAREN
22872 || token->type == CPP_CLOSE_SQUARE
22873 || token->type == CPP_CLOSE_BRACE
22874 || token->type == CPP_COLON)
22875 expression = NULL_TREE;
22876 else
22877 expression = cp_parser_assignment_expression (parser);
22878
22879 return build_throw (expression);
22880 }
22881
22882 /* GNU Extensions */
22883
22884 /* Parse an (optional) asm-specification.
22885
22886 asm-specification:
22887 asm ( string-literal )
22888
22889 If the asm-specification is present, returns a STRING_CST
22890 corresponding to the string-literal. Otherwise, returns
22891 NULL_TREE. */
22892
22893 static tree
22894 cp_parser_asm_specification_opt (cp_parser* parser)
22895 {
22896 cp_token *token;
22897 tree asm_specification;
22898
22899 /* Peek at the next token. */
22900 token = cp_lexer_peek_token (parser->lexer);
22901 /* If the next token isn't the `asm' keyword, then there's no
22902 asm-specification. */
22903 if (!cp_parser_is_keyword (token, RID_ASM))
22904 return NULL_TREE;
22905
22906 /* Consume the `asm' token. */
22907 cp_lexer_consume_token (parser->lexer);
22908 /* Look for the `('. */
22909 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22910
22911 /* Look for the string-literal. */
22912 asm_specification = cp_parser_string_literal (parser, false, false);
22913
22914 /* Look for the `)'. */
22915 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22916
22917 return asm_specification;
22918 }
22919
22920 /* Parse an asm-operand-list.
22921
22922 asm-operand-list:
22923 asm-operand
22924 asm-operand-list , asm-operand
22925
22926 asm-operand:
22927 string-literal ( expression )
22928 [ string-literal ] string-literal ( expression )
22929
22930 Returns a TREE_LIST representing the operands. The TREE_VALUE of
22931 each node is the expression. The TREE_PURPOSE is itself a
22932 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
22933 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
22934 is a STRING_CST for the string literal before the parenthesis. Returns
22935 ERROR_MARK_NODE if any of the operands are invalid. */
22936
22937 static tree
22938 cp_parser_asm_operand_list (cp_parser* parser)
22939 {
22940 tree asm_operands = NULL_TREE;
22941 bool invalid_operands = false;
22942
22943 while (true)
22944 {
22945 tree string_literal;
22946 tree expression;
22947 tree name;
22948
22949 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22950 {
22951 /* Consume the `[' token. */
22952 cp_lexer_consume_token (parser->lexer);
22953 /* Read the operand name. */
22954 name = cp_parser_identifier (parser);
22955 if (name != error_mark_node)
22956 name = build_string (IDENTIFIER_LENGTH (name),
22957 IDENTIFIER_POINTER (name));
22958 /* Look for the closing `]'. */
22959 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22960 }
22961 else
22962 name = NULL_TREE;
22963 /* Look for the string-literal. */
22964 string_literal = cp_parser_string_literal (parser, false, false);
22965
22966 /* Look for the `('. */
22967 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22968 /* Parse the expression. */
22969 expression = cp_parser_expression (parser);
22970 /* Look for the `)'. */
22971 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22972
22973 if (name == error_mark_node
22974 || string_literal == error_mark_node
22975 || expression == error_mark_node)
22976 invalid_operands = true;
22977
22978 /* Add this operand to the list. */
22979 asm_operands = tree_cons (build_tree_list (name, string_literal),
22980 expression,
22981 asm_operands);
22982 /* If the next token is not a `,', there are no more
22983 operands. */
22984 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22985 break;
22986 /* Consume the `,'. */
22987 cp_lexer_consume_token (parser->lexer);
22988 }
22989
22990 return invalid_operands ? error_mark_node : nreverse (asm_operands);
22991 }
22992
22993 /* Parse an asm-clobber-list.
22994
22995 asm-clobber-list:
22996 string-literal
22997 asm-clobber-list , string-literal
22998
22999 Returns a TREE_LIST, indicating the clobbers in the order that they
23000 appeared. The TREE_VALUE of each node is a STRING_CST. */
23001
23002 static tree
23003 cp_parser_asm_clobber_list (cp_parser* parser)
23004 {
23005 tree clobbers = NULL_TREE;
23006
23007 while (true)
23008 {
23009 tree string_literal;
23010
23011 /* Look for the string literal. */
23012 string_literal = cp_parser_string_literal (parser, false, false);
23013 /* Add it to the list. */
23014 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
23015 /* If the next token is not a `,', then the list is
23016 complete. */
23017 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23018 break;
23019 /* Consume the `,' token. */
23020 cp_lexer_consume_token (parser->lexer);
23021 }
23022
23023 return clobbers;
23024 }
23025
23026 /* Parse an asm-label-list.
23027
23028 asm-label-list:
23029 identifier
23030 asm-label-list , identifier
23031
23032 Returns a TREE_LIST, indicating the labels in the order that they
23033 appeared. The TREE_VALUE of each node is a label. */
23034
23035 static tree
23036 cp_parser_asm_label_list (cp_parser* parser)
23037 {
23038 tree labels = NULL_TREE;
23039
23040 while (true)
23041 {
23042 tree identifier, label, name;
23043
23044 /* Look for the identifier. */
23045 identifier = cp_parser_identifier (parser);
23046 if (!error_operand_p (identifier))
23047 {
23048 label = lookup_label (identifier);
23049 if (TREE_CODE (label) == LABEL_DECL)
23050 {
23051 TREE_USED (label) = 1;
23052 check_goto (label);
23053 name = build_string (IDENTIFIER_LENGTH (identifier),
23054 IDENTIFIER_POINTER (identifier));
23055 labels = tree_cons (name, label, labels);
23056 }
23057 }
23058 /* If the next token is not a `,', then the list is
23059 complete. */
23060 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23061 break;
23062 /* Consume the `,' token. */
23063 cp_lexer_consume_token (parser->lexer);
23064 }
23065
23066 return nreverse (labels);
23067 }
23068
23069 /* Return TRUE iff the next tokens in the stream are possibly the
23070 beginning of a GNU extension attribute. */
23071
23072 static bool
23073 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
23074 {
23075 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
23076 }
23077
23078 /* Return TRUE iff the next tokens in the stream are possibly the
23079 beginning of a standard C++-11 attribute specifier. */
23080
23081 static bool
23082 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
23083 {
23084 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
23085 }
23086
23087 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23088 beginning of a standard C++-11 attribute specifier. */
23089
23090 static bool
23091 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
23092 {
23093 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23094
23095 return (cxx_dialect >= cxx11
23096 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
23097 || (token->type == CPP_OPEN_SQUARE
23098 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
23099 && token->type == CPP_OPEN_SQUARE)));
23100 }
23101
23102 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23103 beginning of a GNU extension attribute. */
23104
23105 static bool
23106 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
23107 {
23108 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23109
23110 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
23111 }
23112
23113 /* Return true iff the next tokens can be the beginning of either a
23114 GNU attribute list, or a standard C++11 attribute sequence. */
23115
23116 static bool
23117 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
23118 {
23119 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
23120 || cp_next_tokens_can_be_std_attribute_p (parser));
23121 }
23122
23123 /* Return true iff the next Nth tokens can be the beginning of either
23124 a GNU attribute list, or a standard C++11 attribute sequence. */
23125
23126 static bool
23127 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
23128 {
23129 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
23130 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
23131 }
23132
23133 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23134 of GNU attributes, or return NULL. */
23135
23136 static tree
23137 cp_parser_attributes_opt (cp_parser *parser)
23138 {
23139 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
23140 return cp_parser_gnu_attributes_opt (parser);
23141 return cp_parser_std_attribute_spec_seq (parser);
23142 }
23143
23144 #define CILK_SIMD_FN_CLAUSE_MASK \
23145 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
23146 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
23147 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
23148 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
23149 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23150
23151 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
23152 vector [(<clauses>)] */
23153
23154 static void
23155 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
23156 {
23157 bool first_p = parser->cilk_simd_fn_info == NULL;
23158 cp_token *token = v_token;
23159 if (first_p)
23160 {
23161 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
23162 parser->cilk_simd_fn_info->error_seen = false;
23163 parser->cilk_simd_fn_info->fndecl_seen = false;
23164 parser->cilk_simd_fn_info->tokens = vNULL;
23165 }
23166 int paren_scope = 0;
23167 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23168 {
23169 cp_lexer_consume_token (parser->lexer);
23170 v_token = cp_lexer_peek_token (parser->lexer);
23171 paren_scope++;
23172 }
23173 while (paren_scope > 0)
23174 {
23175 token = cp_lexer_peek_token (parser->lexer);
23176 if (token->type == CPP_OPEN_PAREN)
23177 paren_scope++;
23178 else if (token->type == CPP_CLOSE_PAREN)
23179 paren_scope--;
23180 /* Do not push the last ')' */
23181 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
23182 cp_lexer_consume_token (parser->lexer);
23183 }
23184
23185 token->type = CPP_PRAGMA_EOL;
23186 parser->lexer->next_token = token;
23187 cp_lexer_consume_token (parser->lexer);
23188
23189 struct cp_token_cache *cp
23190 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
23191 parser->cilk_simd_fn_info->tokens.safe_push (cp);
23192 }
23193
23194 /* Parse an (optional) series of attributes.
23195
23196 attributes:
23197 attributes attribute
23198
23199 attribute:
23200 __attribute__ (( attribute-list [opt] ))
23201
23202 The return value is as for cp_parser_gnu_attribute_list. */
23203
23204 static tree
23205 cp_parser_gnu_attributes_opt (cp_parser* parser)
23206 {
23207 tree attributes = NULL_TREE;
23208
23209 while (true)
23210 {
23211 cp_token *token;
23212 tree attribute_list;
23213 bool ok = true;
23214
23215 /* Peek at the next token. */
23216 token = cp_lexer_peek_token (parser->lexer);
23217 /* If it's not `__attribute__', then we're done. */
23218 if (token->keyword != RID_ATTRIBUTE)
23219 break;
23220
23221 /* Consume the `__attribute__' keyword. */
23222 cp_lexer_consume_token (parser->lexer);
23223 /* Look for the two `(' tokens. */
23224 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23225 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23226
23227 /* Peek at the next token. */
23228 token = cp_lexer_peek_token (parser->lexer);
23229 if (token->type != CPP_CLOSE_PAREN)
23230 /* Parse the attribute-list. */
23231 attribute_list = cp_parser_gnu_attribute_list (parser);
23232 else
23233 /* If the next token is a `)', then there is no attribute
23234 list. */
23235 attribute_list = NULL;
23236
23237 /* Look for the two `)' tokens. */
23238 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23239 ok = false;
23240 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23241 ok = false;
23242 if (!ok)
23243 cp_parser_skip_to_end_of_statement (parser);
23244
23245 /* Add these new attributes to the list. */
23246 attributes = chainon (attributes, attribute_list);
23247 }
23248
23249 return attributes;
23250 }
23251
23252 /* Parse a GNU attribute-list.
23253
23254 attribute-list:
23255 attribute
23256 attribute-list , attribute
23257
23258 attribute:
23259 identifier
23260 identifier ( identifier )
23261 identifier ( identifier , expression-list )
23262 identifier ( expression-list )
23263
23264 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
23265 to an attribute. The TREE_PURPOSE of each node is the identifier
23266 indicating which attribute is in use. The TREE_VALUE represents
23267 the arguments, if any. */
23268
23269 static tree
23270 cp_parser_gnu_attribute_list (cp_parser* parser)
23271 {
23272 tree attribute_list = NULL_TREE;
23273 bool save_translate_strings_p = parser->translate_strings_p;
23274
23275 parser->translate_strings_p = false;
23276 while (true)
23277 {
23278 cp_token *token;
23279 tree identifier;
23280 tree attribute;
23281
23282 /* Look for the identifier. We also allow keywords here; for
23283 example `__attribute__ ((const))' is legal. */
23284 token = cp_lexer_peek_token (parser->lexer);
23285 if (token->type == CPP_NAME
23286 || token->type == CPP_KEYWORD)
23287 {
23288 tree arguments = NULL_TREE;
23289
23290 /* Consume the token, but save it since we need it for the
23291 SIMD enabled function parsing. */
23292 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
23293
23294 /* Save away the identifier that indicates which attribute
23295 this is. */
23296 identifier = (token->type == CPP_KEYWORD)
23297 /* For keywords, use the canonical spelling, not the
23298 parsed identifier. */
23299 ? ridpointers[(int) token->keyword]
23300 : id_token->u.value;
23301
23302 attribute = build_tree_list (identifier, NULL_TREE);
23303
23304 /* Peek at the next token. */
23305 token = cp_lexer_peek_token (parser->lexer);
23306 /* If it's an `(', then parse the attribute arguments. */
23307 if (token->type == CPP_OPEN_PAREN)
23308 {
23309 vec<tree, va_gc> *vec;
23310 int attr_flag = (attribute_takes_identifier_p (identifier)
23311 ? id_attr : normal_attr);
23312 if (is_cilkplus_vector_p (identifier))
23313 {
23314 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23315 continue;
23316 }
23317 else
23318 vec = cp_parser_parenthesized_expression_list
23319 (parser, attr_flag, /*cast_p=*/false,
23320 /*allow_expansion_p=*/false,
23321 /*non_constant_p=*/NULL);
23322 if (vec == NULL)
23323 arguments = error_mark_node;
23324 else
23325 {
23326 arguments = build_tree_list_vec (vec);
23327 release_tree_vector (vec);
23328 }
23329 /* Save the arguments away. */
23330 TREE_VALUE (attribute) = arguments;
23331 }
23332 else if (is_cilkplus_vector_p (identifier))
23333 {
23334 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23335 continue;
23336 }
23337
23338 if (arguments != error_mark_node)
23339 {
23340 /* Add this attribute to the list. */
23341 TREE_CHAIN (attribute) = attribute_list;
23342 attribute_list = attribute;
23343 }
23344
23345 token = cp_lexer_peek_token (parser->lexer);
23346 }
23347 /* Now, look for more attributes. If the next token isn't a
23348 `,', we're done. */
23349 if (token->type != CPP_COMMA)
23350 break;
23351
23352 /* Consume the comma and keep going. */
23353 cp_lexer_consume_token (parser->lexer);
23354 }
23355 parser->translate_strings_p = save_translate_strings_p;
23356
23357 /* We built up the list in reverse order. */
23358 return nreverse (attribute_list);
23359 }
23360
23361 /* Parse a standard C++11 attribute.
23362
23363 The returned representation is a TREE_LIST which TREE_PURPOSE is
23364 the scoped name of the attribute, and the TREE_VALUE is its
23365 arguments list.
23366
23367 Note that the scoped name of the attribute is itself a TREE_LIST
23368 which TREE_PURPOSE is the namespace of the attribute, and
23369 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
23370 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23371 and which TREE_PURPOSE is directly the attribute name.
23372
23373 Clients of the attribute code should use get_attribute_namespace
23374 and get_attribute_name to get the actual namespace and name of
23375 attributes, regardless of their being GNU or C++11 attributes.
23376
23377 attribute:
23378 attribute-token attribute-argument-clause [opt]
23379
23380 attribute-token:
23381 identifier
23382 attribute-scoped-token
23383
23384 attribute-scoped-token:
23385 attribute-namespace :: identifier
23386
23387 attribute-namespace:
23388 identifier
23389
23390 attribute-argument-clause:
23391 ( balanced-token-seq )
23392
23393 balanced-token-seq:
23394 balanced-token [opt]
23395 balanced-token-seq balanced-token
23396
23397 balanced-token:
23398 ( balanced-token-seq )
23399 [ balanced-token-seq ]
23400 { balanced-token-seq }. */
23401
23402 static tree
23403 cp_parser_std_attribute (cp_parser *parser)
23404 {
23405 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
23406 cp_token *token;
23407
23408 /* First, parse name of the attribute, a.k.a attribute-token. */
23409
23410 token = cp_lexer_peek_token (parser->lexer);
23411 if (token->type == CPP_NAME)
23412 attr_id = token->u.value;
23413 else if (token->type == CPP_KEYWORD)
23414 attr_id = ridpointers[(int) token->keyword];
23415 else if (token->flags & NAMED_OP)
23416 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
23417
23418 if (attr_id == NULL_TREE)
23419 return NULL_TREE;
23420
23421 cp_lexer_consume_token (parser->lexer);
23422
23423 token = cp_lexer_peek_token (parser->lexer);
23424 if (token->type == CPP_SCOPE)
23425 {
23426 /* We are seeing a scoped attribute token. */
23427
23428 cp_lexer_consume_token (parser->lexer);
23429 attr_ns = attr_id;
23430
23431 token = cp_lexer_consume_token (parser->lexer);
23432 if (token->type == CPP_NAME)
23433 attr_id = token->u.value;
23434 else if (token->type == CPP_KEYWORD)
23435 attr_id = ridpointers[(int) token->keyword];
23436 else
23437 {
23438 error_at (token->location,
23439 "expected an identifier for the attribute name");
23440 return error_mark_node;
23441 }
23442 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
23443 NULL_TREE);
23444 token = cp_lexer_peek_token (parser->lexer);
23445 }
23446 else
23447 {
23448 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
23449 NULL_TREE);
23450 /* C++11 noreturn attribute is equivalent to GNU's. */
23451 if (is_attribute_p ("noreturn", attr_id))
23452 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23453 /* C++14 deprecated attribute is equivalent to GNU's. */
23454 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
23455 {
23456 if (cxx_dialect == cxx11)
23457 pedwarn (token->location, OPT_Wpedantic,
23458 "%<deprecated%> is a C++14 feature;"
23459 " use %<gnu::deprecated%>");
23460 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23461 }
23462 /* Transactional Memory TS optimize_for_synchronized attribute is
23463 equivalent to GNU transaction_callable. */
23464 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
23465 TREE_PURPOSE (attribute)
23466 = get_identifier ("transaction_callable");
23467 /* Transactional Memory attributes are GNU attributes. */
23468 else if (tm_attr_to_mask (attr_id))
23469 TREE_PURPOSE (attribute) = attr_id;
23470 }
23471
23472 /* Now parse the optional argument clause of the attribute. */
23473
23474 if (token->type != CPP_OPEN_PAREN)
23475 return attribute;
23476
23477 {
23478 vec<tree, va_gc> *vec;
23479 int attr_flag = normal_attr;
23480
23481 if (attr_ns == get_identifier ("gnu")
23482 && attribute_takes_identifier_p (attr_id))
23483 /* A GNU attribute that takes an identifier in parameter. */
23484 attr_flag = id_attr;
23485
23486 vec = cp_parser_parenthesized_expression_list
23487 (parser, attr_flag, /*cast_p=*/false,
23488 /*allow_expansion_p=*/true,
23489 /*non_constant_p=*/NULL);
23490 if (vec == NULL)
23491 arguments = error_mark_node;
23492 else
23493 {
23494 arguments = build_tree_list_vec (vec);
23495 release_tree_vector (vec);
23496 }
23497
23498 if (arguments == error_mark_node)
23499 attribute = error_mark_node;
23500 else
23501 TREE_VALUE (attribute) = arguments;
23502 }
23503
23504 return attribute;
23505 }
23506
23507 /* Check that the attribute ATTRIBUTE appears at most once in the
23508 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
23509 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
23510 isn't implemented yet in GCC. */
23511
23512 static void
23513 cp_parser_check_std_attribute (tree attributes, tree attribute)
23514 {
23515 if (attributes)
23516 {
23517 tree name = get_attribute_name (attribute);
23518 if (is_attribute_p ("noreturn", name)
23519 && lookup_attribute ("noreturn", attributes))
23520 error ("attribute noreturn can appear at most once "
23521 "in an attribute-list");
23522 else if (is_attribute_p ("deprecated", name)
23523 && lookup_attribute ("deprecated", attributes))
23524 error ("attribute deprecated can appear at most once "
23525 "in an attribute-list");
23526 }
23527 }
23528
23529 /* Parse a list of standard C++-11 attributes.
23530
23531 attribute-list:
23532 attribute [opt]
23533 attribute-list , attribute[opt]
23534 attribute ...
23535 attribute-list , attribute ...
23536 */
23537
23538 static tree
23539 cp_parser_std_attribute_list (cp_parser *parser)
23540 {
23541 tree attributes = NULL_TREE, attribute = NULL_TREE;
23542 cp_token *token = NULL;
23543
23544 while (true)
23545 {
23546 attribute = cp_parser_std_attribute (parser);
23547 if (attribute == error_mark_node)
23548 break;
23549 if (attribute != NULL_TREE)
23550 {
23551 cp_parser_check_std_attribute (attributes, attribute);
23552 TREE_CHAIN (attribute) = attributes;
23553 attributes = attribute;
23554 }
23555 token = cp_lexer_peek_token (parser->lexer);
23556 if (token->type == CPP_ELLIPSIS)
23557 {
23558 cp_lexer_consume_token (parser->lexer);
23559 TREE_VALUE (attribute)
23560 = make_pack_expansion (TREE_VALUE (attribute));
23561 token = cp_lexer_peek_token (parser->lexer);
23562 }
23563 if (token->type != CPP_COMMA)
23564 break;
23565 cp_lexer_consume_token (parser->lexer);
23566 }
23567 attributes = nreverse (attributes);
23568 return attributes;
23569 }
23570
23571 /* Parse a standard C++-11 attribute specifier.
23572
23573 attribute-specifier:
23574 [ [ attribute-list ] ]
23575 alignment-specifier
23576
23577 alignment-specifier:
23578 alignas ( type-id ... [opt] )
23579 alignas ( alignment-expression ... [opt] ). */
23580
23581 static tree
23582 cp_parser_std_attribute_spec (cp_parser *parser)
23583 {
23584 tree attributes = NULL_TREE;
23585 cp_token *token = cp_lexer_peek_token (parser->lexer);
23586
23587 if (token->type == CPP_OPEN_SQUARE
23588 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
23589 {
23590 cp_lexer_consume_token (parser->lexer);
23591 cp_lexer_consume_token (parser->lexer);
23592
23593 attributes = cp_parser_std_attribute_list (parser);
23594
23595 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
23596 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
23597 cp_parser_skip_to_end_of_statement (parser);
23598 else
23599 /* Warn about parsing c++11 attribute in non-c++1 mode, only
23600 when we are sure that we have actually parsed them. */
23601 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
23602 }
23603 else
23604 {
23605 tree alignas_expr;
23606
23607 /* Look for an alignment-specifier. */
23608
23609 token = cp_lexer_peek_token (parser->lexer);
23610
23611 if (token->type != CPP_KEYWORD
23612 || token->keyword != RID_ALIGNAS)
23613 return NULL_TREE;
23614
23615 cp_lexer_consume_token (parser->lexer);
23616 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
23617
23618 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
23619 {
23620 cp_parser_error (parser, "expected %<(%>");
23621 return error_mark_node;
23622 }
23623
23624 cp_parser_parse_tentatively (parser);
23625 alignas_expr = cp_parser_type_id (parser);
23626
23627 if (!cp_parser_parse_definitely (parser))
23628 {
23629 gcc_assert (alignas_expr == error_mark_node
23630 || alignas_expr == NULL_TREE);
23631
23632 alignas_expr =
23633 cp_parser_assignment_expression (parser);
23634 if (alignas_expr == error_mark_node)
23635 cp_parser_skip_to_end_of_statement (parser);
23636 if (alignas_expr == NULL_TREE
23637 || alignas_expr == error_mark_node)
23638 return alignas_expr;
23639 }
23640
23641 alignas_expr = cxx_alignas_expr (alignas_expr);
23642 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
23643
23644 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23645 {
23646 cp_lexer_consume_token (parser->lexer);
23647 alignas_expr = make_pack_expansion (alignas_expr);
23648 }
23649
23650 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
23651 {
23652 cp_parser_error (parser, "expected %<)%>");
23653 return error_mark_node;
23654 }
23655
23656 /* Build the C++-11 representation of an 'aligned'
23657 attribute. */
23658 attributes =
23659 build_tree_list (build_tree_list (get_identifier ("gnu"),
23660 get_identifier ("aligned")),
23661 alignas_expr);
23662 }
23663
23664 return attributes;
23665 }
23666
23667 /* Parse a standard C++-11 attribute-specifier-seq.
23668
23669 attribute-specifier-seq:
23670 attribute-specifier-seq [opt] attribute-specifier
23671 */
23672
23673 static tree
23674 cp_parser_std_attribute_spec_seq (cp_parser *parser)
23675 {
23676 tree attr_specs = NULL;
23677
23678 while (true)
23679 {
23680 tree attr_spec = cp_parser_std_attribute_spec (parser);
23681 if (attr_spec == NULL_TREE)
23682 break;
23683 if (attr_spec == error_mark_node)
23684 return error_mark_node;
23685
23686 TREE_CHAIN (attr_spec) = attr_specs;
23687 attr_specs = attr_spec;
23688 }
23689
23690 attr_specs = nreverse (attr_specs);
23691 return attr_specs;
23692 }
23693
23694 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
23695 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
23696 current value of the PEDANTIC flag, regardless of whether or not
23697 the `__extension__' keyword is present. The caller is responsible
23698 for restoring the value of the PEDANTIC flag. */
23699
23700 static bool
23701 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
23702 {
23703 /* Save the old value of the PEDANTIC flag. */
23704 *saved_pedantic = pedantic;
23705
23706 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
23707 {
23708 /* Consume the `__extension__' token. */
23709 cp_lexer_consume_token (parser->lexer);
23710 /* We're not being pedantic while the `__extension__' keyword is
23711 in effect. */
23712 pedantic = 0;
23713
23714 return true;
23715 }
23716
23717 return false;
23718 }
23719
23720 /* Parse a label declaration.
23721
23722 label-declaration:
23723 __label__ label-declarator-seq ;
23724
23725 label-declarator-seq:
23726 identifier , label-declarator-seq
23727 identifier */
23728
23729 static void
23730 cp_parser_label_declaration (cp_parser* parser)
23731 {
23732 /* Look for the `__label__' keyword. */
23733 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
23734
23735 while (true)
23736 {
23737 tree identifier;
23738
23739 /* Look for an identifier. */
23740 identifier = cp_parser_identifier (parser);
23741 /* If we failed, stop. */
23742 if (identifier == error_mark_node)
23743 break;
23744 /* Declare it as a label. */
23745 finish_label_decl (identifier);
23746 /* If the next token is a `;', stop. */
23747 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23748 break;
23749 /* Look for the `,' separating the label declarations. */
23750 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
23751 }
23752
23753 /* Look for the final `;'. */
23754 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23755 }
23756
23757 // -------------------------------------------------------------------------- //
23758 // Requires Clause
23759
23760 // Parse a requires clause.
23761 //
23762 // requires-clause:
23763 // 'requires' logical-or-expression
23764 //
23765 // The required logical-or-expression must be a constant expression. Note
23766 // that we don't check that the expression is constepxr here. We defer until
23767 // we analyze constraints and then, we only check atomic constraints.
23768 static tree
23769 cp_parser_requires_clause (cp_parser *parser)
23770 {
23771 // Parse the requires clause so that it is not automatically folded.
23772 ++processing_template_decl;
23773 tree expr = cp_parser_binary_expression (parser, false, false,
23774 PREC_NOT_OPERATOR, NULL);
23775 --processing_template_decl;
23776 return expr;
23777 }
23778
23779 // Optionally parse a requires clause:
23780 static tree
23781 cp_parser_requires_clause_opt (cp_parser *parser)
23782 {
23783 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
23784 return NULL_TREE;
23785 cp_lexer_consume_token (parser->lexer);
23786 return cp_parser_requires_clause (parser);
23787 }
23788
23789
23790 /*---------------------------------------------------------------------------
23791 Requires expressions
23792 ---------------------------------------------------------------------------*/
23793
23794 /* Parse a requires expression
23795
23796 requirement-expression:
23797 'requires' requirement-parameter-list [opt] requirement-body */
23798 static tree
23799 cp_parser_requires_expression (cp_parser *parser)
23800 {
23801 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
23802 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
23803
23804 /* A requires-expression shall appear only within a concept
23805 definition or a requires-clause.
23806
23807 TODO: Implement this diagnostic correctly. */
23808 if (!processing_template_decl)
23809 {
23810 error_at (loc, "a requires expression cannot appear outside a template");
23811 cp_parser_skip_to_end_of_statement (parser);
23812 return error_mark_node;
23813 }
23814
23815 tree parms, reqs;
23816 {
23817 /* Local parameters are delared as variables within the scope
23818 of the expression. They are not visible past the end of
23819 the expression. Expressions within the requires-expression
23820 are unevaluated. */
23821 struct scope_sentinel
23822 {
23823 scope_sentinel ()
23824 {
23825 ++cp_unevaluated_operand;
23826 begin_scope (sk_block, NULL_TREE);
23827 }
23828
23829 ~scope_sentinel ()
23830 {
23831 pop_bindings_and_leave_scope ();
23832 --cp_unevaluated_operand;
23833 }
23834 } s;
23835
23836 /* Parse the optional parameter list. */
23837 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23838 {
23839 parms = cp_parser_requirement_parameter_list (parser);
23840 if (parms == error_mark_node)
23841 return error_mark_node;
23842 }
23843 else
23844 parms = NULL_TREE;
23845
23846 /* Parse the requirement body. */
23847 reqs = cp_parser_requirement_body (parser);
23848 if (reqs == error_mark_node)
23849 return error_mark_node;
23850 }
23851
23852 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
23853 the parm chain. */
23854 grokparms (parms, &parms);
23855 return finish_requires_expr (parms, reqs);
23856 }
23857
23858 /* Parse a parameterized requirement.
23859
23860 requirement-parameter-list:
23861 '(' parameter-declaration-clause ')' */
23862 static tree
23863 cp_parser_requirement_parameter_list (cp_parser *parser)
23864 {
23865 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23866 return error_mark_node;
23867
23868 tree parms = cp_parser_parameter_declaration_clause (parser);
23869
23870 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23871 return error_mark_node;
23872
23873 return parms;
23874 }
23875
23876 /* Parse the body of a requirement.
23877
23878 requirement-body:
23879 '{' requirement-list '}' */
23880 static tree
23881 cp_parser_requirement_body (cp_parser *parser)
23882 {
23883 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
23884 return error_mark_node;
23885
23886 tree reqs = cp_parser_requirement_list (parser);
23887
23888 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
23889 return error_mark_node;
23890
23891 return reqs;
23892 }
23893
23894 /* Parse a list of requirements.
23895
23896 requirement-list:
23897 requirement
23898 requirement-list ';' requirement[opt] */
23899 static tree
23900 cp_parser_requirement_list (cp_parser *parser)
23901 {
23902 tree result = NULL_TREE;
23903 while (true)
23904 {
23905 tree req = cp_parser_requirement (parser);
23906 if (req == error_mark_node)
23907 return error_mark_node;
23908
23909 result = tree_cons (NULL_TREE, req, result);
23910
23911 /* If we see a semi-colon, consume it. */
23912 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23913 cp_lexer_consume_token (parser->lexer);
23914
23915 /* Stop processing at the end of the list. */
23916 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23917 break;
23918 }
23919
23920 /* Reverse the order of requirements so they are analyzed in
23921 declaration order. */
23922 return nreverse (result);
23923 }
23924
23925 /* Parse a syntactic requirement or type requirement.
23926
23927 requirement:
23928 simple-requirement
23929 compound-requirement
23930 type-requirement
23931 nested-requirement */
23932 static tree
23933 cp_parser_requirement (cp_parser *parser)
23934 {
23935 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23936 return cp_parser_compound_requirement (parser);
23937 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23938 return cp_parser_type_requirement (parser);
23939 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
23940 return cp_parser_nested_requirement (parser);
23941 else
23942 return cp_parser_simple_requirement (parser);
23943 }
23944
23945 /* Parse a simple requirement.
23946
23947 simple-requirement:
23948 expression ';' */
23949 static tree
23950 cp_parser_simple_requirement (cp_parser *parser)
23951 {
23952 tree expr = cp_parser_expression (parser, NULL, false, false);
23953 if (!expr || expr == error_mark_node)
23954 return error_mark_node;
23955
23956 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
23957 return error_mark_node;
23958
23959 return finish_simple_requirement (expr);
23960 }
23961
23962 /* Parse a type requirement
23963
23964 type-requirement
23965 nested-name-specifier [opt] required-type-name ';'
23966
23967 required-type-name:
23968 type-name
23969 'template' [opt] simple-template-id */
23970 static tree
23971 cp_parser_type_requirement (cp_parser *parser)
23972 {
23973 cp_lexer_consume_token (parser->lexer);
23974
23975 // Save the scope before parsing name specifiers.
23976 tree saved_scope = parser->scope;
23977 tree saved_object_scope = parser->object_scope;
23978 tree saved_qualifying_scope = parser->qualifying_scope;
23979 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
23980 cp_parser_nested_name_specifier_opt (parser,
23981 /*typename_keyword_p=*/true,
23982 /*check_dependency_p=*/false,
23983 /*type_p=*/true,
23984 /*is_declaration=*/false);
23985
23986 tree type;
23987 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23988 {
23989 cp_lexer_consume_token (parser->lexer);
23990 type = cp_parser_template_id (parser,
23991 /*template_keyword_p=*/true,
23992 /*check_dependency=*/false,
23993 /*tag_type=*/none_type,
23994 /*is_declaration=*/false);
23995 type = make_typename_type (parser->scope, type, typename_type,
23996 /*complain=*/tf_error);
23997 }
23998 else
23999 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
24000
24001 if (TREE_CODE (type) == TYPE_DECL)
24002 type = TREE_TYPE (type);
24003
24004 parser->scope = saved_scope;
24005 parser->object_scope = saved_object_scope;
24006 parser->qualifying_scope = saved_qualifying_scope;
24007
24008 if (type == error_mark_node)
24009 cp_parser_skip_to_end_of_statement (parser);
24010
24011 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24012 return error_mark_node;
24013 if (type == error_mark_node)
24014 return error_mark_node;
24015
24016 return finish_type_requirement (type);
24017 }
24018
24019 /* Parse a compound requirement
24020
24021 compound-requirement:
24022 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
24023 static tree
24024 cp_parser_compound_requirement (cp_parser *parser)
24025 {
24026 /* Parse an expression enclosed in '{ }'s. */
24027 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24028 return error_mark_node;
24029
24030 tree expr = cp_parser_expression (parser, NULL, false, false);
24031 if (!expr || expr == error_mark_node)
24032 return error_mark_node;
24033
24034 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24035 return error_mark_node;
24036
24037 /* Parse the optional noexcept. */
24038 bool noexcept_p = false;
24039 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
24040 {
24041 cp_lexer_consume_token (parser->lexer);
24042 noexcept_p = true;
24043 }
24044
24045 /* Parse the optional trailing return type. */
24046 tree type = NULL_TREE;
24047 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
24048 {
24049 cp_lexer_consume_token (parser->lexer);
24050 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
24051 parser->in_result_type_constraint_p = true;
24052 type = cp_parser_trailing_type_id (parser);
24053 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
24054 if (type == error_mark_node)
24055 return error_mark_node;
24056 }
24057
24058 return finish_compound_requirement (expr, type, noexcept_p);
24059 }
24060
24061 /* Parse a nested requirement. This is the same as a requires clause.
24062
24063 nested-requirement:
24064 requires-clause */
24065 static tree
24066 cp_parser_nested_requirement (cp_parser *parser)
24067 {
24068 cp_lexer_consume_token (parser->lexer);
24069 tree req = cp_parser_requires_clause (parser);
24070 if (req == error_mark_node)
24071 return error_mark_node;
24072 return finish_nested_requirement (req);
24073 }
24074
24075 /* Support Functions */
24076
24077 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24078 NAME should have one of the representations used for an
24079 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24080 is returned. If PARSER->SCOPE is a dependent type, then a
24081 SCOPE_REF is returned.
24082
24083 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24084 returned; the name was already resolved when the TEMPLATE_ID_EXPR
24085 was formed. Abstractly, such entities should not be passed to this
24086 function, because they do not need to be looked up, but it is
24087 simpler to check for this special case here, rather than at the
24088 call-sites.
24089
24090 In cases not explicitly covered above, this function returns a
24091 DECL, OVERLOAD, or baselink representing the result of the lookup.
24092 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24093 is returned.
24094
24095 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24096 (e.g., "struct") that was used. In that case bindings that do not
24097 refer to types are ignored.
24098
24099 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24100 ignored.
24101
24102 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24103 are ignored.
24104
24105 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24106 types.
24107
24108 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24109 TREE_LIST of candidates if name-lookup results in an ambiguity, and
24110 NULL_TREE otherwise. */
24111
24112 static tree
24113 cp_parser_lookup_name (cp_parser *parser, tree name,
24114 enum tag_types tag_type,
24115 bool is_template,
24116 bool is_namespace,
24117 bool check_dependency,
24118 tree *ambiguous_decls,
24119 location_t name_location)
24120 {
24121 tree decl;
24122 tree object_type = parser->context->object_type;
24123
24124 /* Assume that the lookup will be unambiguous. */
24125 if (ambiguous_decls)
24126 *ambiguous_decls = NULL_TREE;
24127
24128 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24129 no longer valid. Note that if we are parsing tentatively, and
24130 the parse fails, OBJECT_TYPE will be automatically restored. */
24131 parser->context->object_type = NULL_TREE;
24132
24133 if (name == error_mark_node)
24134 return error_mark_node;
24135
24136 /* A template-id has already been resolved; there is no lookup to
24137 do. */
24138 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
24139 return name;
24140 if (BASELINK_P (name))
24141 {
24142 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
24143 == TEMPLATE_ID_EXPR);
24144 return name;
24145 }
24146
24147 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
24148 it should already have been checked to make sure that the name
24149 used matches the type being destroyed. */
24150 if (TREE_CODE (name) == BIT_NOT_EXPR)
24151 {
24152 tree type;
24153
24154 /* Figure out to which type this destructor applies. */
24155 if (parser->scope)
24156 type = parser->scope;
24157 else if (object_type)
24158 type = object_type;
24159 else
24160 type = current_class_type;
24161 /* If that's not a class type, there is no destructor. */
24162 if (!type || !CLASS_TYPE_P (type))
24163 return error_mark_node;
24164 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
24165 lazily_declare_fn (sfk_destructor, type);
24166 if (!CLASSTYPE_DESTRUCTORS (type))
24167 return error_mark_node;
24168 /* If it was a class type, return the destructor. */
24169 return CLASSTYPE_DESTRUCTORS (type);
24170 }
24171
24172 /* By this point, the NAME should be an ordinary identifier. If
24173 the id-expression was a qualified name, the qualifying scope is
24174 stored in PARSER->SCOPE at this point. */
24175 gcc_assert (identifier_p (name));
24176
24177 /* Perform the lookup. */
24178 if (parser->scope)
24179 {
24180 bool dependent_p;
24181
24182 if (parser->scope == error_mark_node)
24183 return error_mark_node;
24184
24185 /* If the SCOPE is dependent, the lookup must be deferred until
24186 the template is instantiated -- unless we are explicitly
24187 looking up names in uninstantiated templates. Even then, we
24188 cannot look up the name if the scope is not a class type; it
24189 might, for example, be a template type parameter. */
24190 dependent_p = (TYPE_P (parser->scope)
24191 && dependent_scope_p (parser->scope));
24192 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
24193 && dependent_p)
24194 /* Defer lookup. */
24195 decl = error_mark_node;
24196 else
24197 {
24198 tree pushed_scope = NULL_TREE;
24199
24200 /* If PARSER->SCOPE is a dependent type, then it must be a
24201 class type, and we must not be checking dependencies;
24202 otherwise, we would have processed this lookup above. So
24203 that PARSER->SCOPE is not considered a dependent base by
24204 lookup_member, we must enter the scope here. */
24205 if (dependent_p)
24206 pushed_scope = push_scope (parser->scope);
24207
24208 /* If the PARSER->SCOPE is a template specialization, it
24209 may be instantiated during name lookup. In that case,
24210 errors may be issued. Even if we rollback the current
24211 tentative parse, those errors are valid. */
24212 decl = lookup_qualified_name (parser->scope, name,
24213 tag_type != none_type,
24214 /*complain=*/true);
24215
24216 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24217 lookup result and the nested-name-specifier nominates a class C:
24218 * if the name specified after the nested-name-specifier, when
24219 looked up in C, is the injected-class-name of C (Clause 9), or
24220 * if the name specified after the nested-name-specifier is the
24221 same as the identifier or the simple-template-id's template-
24222 name in the last component of the nested-name-specifier,
24223 the name is instead considered to name the constructor of
24224 class C. [ Note: for example, the constructor is not an
24225 acceptable lookup result in an elaborated-type-specifier so
24226 the constructor would not be used in place of the
24227 injected-class-name. --end note ] Such a constructor name
24228 shall be used only in the declarator-id of a declaration that
24229 names a constructor or in a using-declaration. */
24230 if (tag_type == none_type
24231 && DECL_SELF_REFERENCE_P (decl)
24232 && same_type_p (DECL_CONTEXT (decl), parser->scope))
24233 decl = lookup_qualified_name (parser->scope, ctor_identifier,
24234 tag_type != none_type,
24235 /*complain=*/true);
24236
24237 /* If we have a single function from a using decl, pull it out. */
24238 if (TREE_CODE (decl) == OVERLOAD
24239 && !really_overloaded_fn (decl))
24240 decl = OVL_FUNCTION (decl);
24241
24242 if (pushed_scope)
24243 pop_scope (pushed_scope);
24244 }
24245
24246 /* If the scope is a dependent type and either we deferred lookup or
24247 we did lookup but didn't find the name, rememeber the name. */
24248 if (decl == error_mark_node && TYPE_P (parser->scope)
24249 && dependent_type_p (parser->scope))
24250 {
24251 if (tag_type)
24252 {
24253 tree type;
24254
24255 /* The resolution to Core Issue 180 says that `struct
24256 A::B' should be considered a type-name, even if `A'
24257 is dependent. */
24258 type = make_typename_type (parser->scope, name, tag_type,
24259 /*complain=*/tf_error);
24260 if (type != error_mark_node)
24261 decl = TYPE_NAME (type);
24262 }
24263 else if (is_template
24264 && (cp_parser_next_token_ends_template_argument_p (parser)
24265 || cp_lexer_next_token_is (parser->lexer,
24266 CPP_CLOSE_PAREN)))
24267 decl = make_unbound_class_template (parser->scope,
24268 name, NULL_TREE,
24269 /*complain=*/tf_error);
24270 else
24271 decl = build_qualified_name (/*type=*/NULL_TREE,
24272 parser->scope, name,
24273 is_template);
24274 }
24275 parser->qualifying_scope = parser->scope;
24276 parser->object_scope = NULL_TREE;
24277 }
24278 else if (object_type)
24279 {
24280 /* Look up the name in the scope of the OBJECT_TYPE, unless the
24281 OBJECT_TYPE is not a class. */
24282 if (CLASS_TYPE_P (object_type))
24283 /* If the OBJECT_TYPE is a template specialization, it may
24284 be instantiated during name lookup. In that case, errors
24285 may be issued. Even if we rollback the current tentative
24286 parse, those errors are valid. */
24287 decl = lookup_member (object_type,
24288 name,
24289 /*protect=*/0,
24290 tag_type != none_type,
24291 tf_warning_or_error);
24292 else
24293 decl = NULL_TREE;
24294
24295 if (!decl)
24296 /* Look it up in the enclosing context. */
24297 decl = lookup_name_real (name, tag_type != none_type,
24298 /*nonclass=*/0,
24299 /*block_p=*/true, is_namespace, 0);
24300 parser->object_scope = object_type;
24301 parser->qualifying_scope = NULL_TREE;
24302 }
24303 else
24304 {
24305 decl = lookup_name_real (name, tag_type != none_type,
24306 /*nonclass=*/0,
24307 /*block_p=*/true, is_namespace, 0);
24308 parser->qualifying_scope = NULL_TREE;
24309 parser->object_scope = NULL_TREE;
24310 }
24311
24312 /* If the lookup failed, let our caller know. */
24313 if (!decl || decl == error_mark_node)
24314 return error_mark_node;
24315
24316 /* Pull out the template from an injected-class-name (or multiple). */
24317 if (is_template)
24318 decl = maybe_get_template_decl_from_type_decl (decl);
24319
24320 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
24321 if (TREE_CODE (decl) == TREE_LIST)
24322 {
24323 if (ambiguous_decls)
24324 *ambiguous_decls = decl;
24325 /* The error message we have to print is too complicated for
24326 cp_parser_error, so we incorporate its actions directly. */
24327 if (!cp_parser_simulate_error (parser))
24328 {
24329 error_at (name_location, "reference to %qD is ambiguous",
24330 name);
24331 print_candidates (decl);
24332 }
24333 return error_mark_node;
24334 }
24335
24336 gcc_assert (DECL_P (decl)
24337 || TREE_CODE (decl) == OVERLOAD
24338 || TREE_CODE (decl) == SCOPE_REF
24339 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
24340 || BASELINK_P (decl));
24341
24342 /* If we have resolved the name of a member declaration, check to
24343 see if the declaration is accessible. When the name resolves to
24344 set of overloaded functions, accessibility is checked when
24345 overload resolution is done.
24346
24347 During an explicit instantiation, access is not checked at all,
24348 as per [temp.explicit]. */
24349 if (DECL_P (decl))
24350 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
24351
24352 maybe_record_typedef_use (decl);
24353
24354 return decl;
24355 }
24356
24357 /* Like cp_parser_lookup_name, but for use in the typical case where
24358 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
24359 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
24360
24361 static tree
24362 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
24363 {
24364 return cp_parser_lookup_name (parser, name,
24365 none_type,
24366 /*is_template=*/false,
24367 /*is_namespace=*/false,
24368 /*check_dependency=*/true,
24369 /*ambiguous_decls=*/NULL,
24370 location);
24371 }
24372
24373 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24374 the current context, return the TYPE_DECL. If TAG_NAME_P is
24375 true, the DECL indicates the class being defined in a class-head,
24376 or declared in an elaborated-type-specifier.
24377
24378 Otherwise, return DECL. */
24379
24380 static tree
24381 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
24382 {
24383 /* If the TEMPLATE_DECL is being declared as part of a class-head,
24384 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24385
24386 struct A {
24387 template <typename T> struct B;
24388 };
24389
24390 template <typename T> struct A::B {};
24391
24392 Similarly, in an elaborated-type-specifier:
24393
24394 namespace N { struct X{}; }
24395
24396 struct A {
24397 template <typename T> friend struct N::X;
24398 };
24399
24400 However, if the DECL refers to a class type, and we are in
24401 the scope of the class, then the name lookup automatically
24402 finds the TYPE_DECL created by build_self_reference rather
24403 than a TEMPLATE_DECL. For example, in:
24404
24405 template <class T> struct S {
24406 S s;
24407 };
24408
24409 there is no need to handle such case. */
24410
24411 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
24412 return DECL_TEMPLATE_RESULT (decl);
24413
24414 return decl;
24415 }
24416
24417 /* If too many, or too few, template-parameter lists apply to the
24418 declarator, issue an error message. Returns TRUE if all went well,
24419 and FALSE otherwise. */
24420
24421 static bool
24422 cp_parser_check_declarator_template_parameters (cp_parser* parser,
24423 cp_declarator *declarator,
24424 location_t declarator_location)
24425 {
24426 switch (declarator->kind)
24427 {
24428 case cdk_id:
24429 {
24430 unsigned num_templates = 0;
24431 tree scope = declarator->u.id.qualifying_scope;
24432
24433 if (scope)
24434 num_templates = num_template_headers_for_class (scope);
24435 else if (TREE_CODE (declarator->u.id.unqualified_name)
24436 == TEMPLATE_ID_EXPR)
24437 /* If the DECLARATOR has the form `X<y>' then it uses one
24438 additional level of template parameters. */
24439 ++num_templates;
24440
24441 return cp_parser_check_template_parameters
24442 (parser, num_templates, declarator_location, declarator);
24443 }
24444
24445 case cdk_function:
24446 case cdk_array:
24447 case cdk_pointer:
24448 case cdk_reference:
24449 case cdk_ptrmem:
24450 return (cp_parser_check_declarator_template_parameters
24451 (parser, declarator->declarator, declarator_location));
24452
24453 case cdk_error:
24454 return true;
24455
24456 default:
24457 gcc_unreachable ();
24458 }
24459 return false;
24460 }
24461
24462 /* NUM_TEMPLATES were used in the current declaration. If that is
24463 invalid, return FALSE and issue an error messages. Otherwise,
24464 return TRUE. If DECLARATOR is non-NULL, then we are checking a
24465 declarator and we can print more accurate diagnostics. */
24466
24467 static bool
24468 cp_parser_check_template_parameters (cp_parser* parser,
24469 unsigned num_templates,
24470 location_t location,
24471 cp_declarator *declarator)
24472 {
24473 /* If there are the same number of template classes and parameter
24474 lists, that's OK. */
24475 if (parser->num_template_parameter_lists == num_templates)
24476 return true;
24477 /* If there are more, but only one more, then we are referring to a
24478 member template. That's OK too. */
24479 if (parser->num_template_parameter_lists == num_templates + 1)
24480 return true;
24481 /* If there are more template classes than parameter lists, we have
24482 something like:
24483
24484 template <class T> void S<T>::R<T>::f (); */
24485 if (parser->num_template_parameter_lists < num_templates)
24486 {
24487 if (declarator && !current_function_decl)
24488 error_at (location, "specializing member %<%T::%E%> "
24489 "requires %<template<>%> syntax",
24490 declarator->u.id.qualifying_scope,
24491 declarator->u.id.unqualified_name);
24492 else if (declarator)
24493 error_at (location, "invalid declaration of %<%T::%E%>",
24494 declarator->u.id.qualifying_scope,
24495 declarator->u.id.unqualified_name);
24496 else
24497 error_at (location, "too few template-parameter-lists");
24498 return false;
24499 }
24500 /* Otherwise, there are too many template parameter lists. We have
24501 something like:
24502
24503 template <class T> template <class U> void S::f(); */
24504 error_at (location, "too many template-parameter-lists");
24505 return false;
24506 }
24507
24508 /* Parse an optional `::' token indicating that the following name is
24509 from the global namespace. If so, PARSER->SCOPE is set to the
24510 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
24511 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
24512 Returns the new value of PARSER->SCOPE, if the `::' token is
24513 present, and NULL_TREE otherwise. */
24514
24515 static tree
24516 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
24517 {
24518 cp_token *token;
24519
24520 /* Peek at the next token. */
24521 token = cp_lexer_peek_token (parser->lexer);
24522 /* If we're looking at a `::' token then we're starting from the
24523 global namespace, not our current location. */
24524 if (token->type == CPP_SCOPE)
24525 {
24526 /* Consume the `::' token. */
24527 cp_lexer_consume_token (parser->lexer);
24528 /* Set the SCOPE so that we know where to start the lookup. */
24529 parser->scope = global_namespace;
24530 parser->qualifying_scope = global_namespace;
24531 parser->object_scope = NULL_TREE;
24532
24533 return parser->scope;
24534 }
24535 else if (!current_scope_valid_p)
24536 {
24537 parser->scope = NULL_TREE;
24538 parser->qualifying_scope = NULL_TREE;
24539 parser->object_scope = NULL_TREE;
24540 }
24541
24542 return NULL_TREE;
24543 }
24544
24545 /* Returns TRUE if the upcoming token sequence is the start of a
24546 constructor declarator. If FRIEND_P is true, the declarator is
24547 preceded by the `friend' specifier. */
24548
24549 static bool
24550 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
24551 {
24552 bool constructor_p;
24553 bool outside_class_specifier_p;
24554 tree nested_name_specifier;
24555 cp_token *next_token;
24556
24557 /* The common case is that this is not a constructor declarator, so
24558 try to avoid doing lots of work if at all possible. It's not
24559 valid declare a constructor at function scope. */
24560 if (parser->in_function_body)
24561 return false;
24562 /* And only certain tokens can begin a constructor declarator. */
24563 next_token = cp_lexer_peek_token (parser->lexer);
24564 if (next_token->type != CPP_NAME
24565 && next_token->type != CPP_SCOPE
24566 && next_token->type != CPP_NESTED_NAME_SPECIFIER
24567 && next_token->type != CPP_TEMPLATE_ID)
24568 return false;
24569
24570 /* Parse tentatively; we are going to roll back all of the tokens
24571 consumed here. */
24572 cp_parser_parse_tentatively (parser);
24573 /* Assume that we are looking at a constructor declarator. */
24574 constructor_p = true;
24575
24576 /* Look for the optional `::' operator. */
24577 cp_parser_global_scope_opt (parser,
24578 /*current_scope_valid_p=*/false);
24579 /* Look for the nested-name-specifier. */
24580 nested_name_specifier
24581 = (cp_parser_nested_name_specifier_opt (parser,
24582 /*typename_keyword_p=*/false,
24583 /*check_dependency_p=*/false,
24584 /*type_p=*/false,
24585 /*is_declaration=*/false));
24586
24587 outside_class_specifier_p = (!at_class_scope_p ()
24588 || !TYPE_BEING_DEFINED (current_class_type)
24589 || friend_p);
24590
24591 /* Outside of a class-specifier, there must be a
24592 nested-name-specifier. */
24593 if (!nested_name_specifier && outside_class_specifier_p)
24594 constructor_p = false;
24595 else if (nested_name_specifier == error_mark_node)
24596 constructor_p = false;
24597
24598 /* If we have a class scope, this is easy; DR 147 says that S::S always
24599 names the constructor, and no other qualified name could. */
24600 if (constructor_p && nested_name_specifier
24601 && CLASS_TYPE_P (nested_name_specifier))
24602 {
24603 tree id = cp_parser_unqualified_id (parser,
24604 /*template_keyword_p=*/false,
24605 /*check_dependency_p=*/false,
24606 /*declarator_p=*/true,
24607 /*optional_p=*/false);
24608 if (is_overloaded_fn (id))
24609 id = DECL_NAME (get_first_fn (id));
24610 if (!constructor_name_p (id, nested_name_specifier))
24611 constructor_p = false;
24612 }
24613 /* If we still think that this might be a constructor-declarator,
24614 look for a class-name. */
24615 else if (constructor_p)
24616 {
24617 /* If we have:
24618
24619 template <typename T> struct S {
24620 S();
24621 };
24622
24623 we must recognize that the nested `S' names a class. */
24624 tree type_decl;
24625 type_decl = cp_parser_class_name (parser,
24626 /*typename_keyword_p=*/false,
24627 /*template_keyword_p=*/false,
24628 none_type,
24629 /*check_dependency_p=*/false,
24630 /*class_head_p=*/false,
24631 /*is_declaration=*/false);
24632 /* If there was no class-name, then this is not a constructor.
24633 Otherwise, if we are in a class-specifier and we aren't
24634 handling a friend declaration, check that its type matches
24635 current_class_type (c++/38313). Note: error_mark_node
24636 is left alone for error recovery purposes. */
24637 constructor_p = (!cp_parser_error_occurred (parser)
24638 && (outside_class_specifier_p
24639 || type_decl == error_mark_node
24640 || same_type_p (current_class_type,
24641 TREE_TYPE (type_decl))));
24642
24643 /* If we're still considering a constructor, we have to see a `(',
24644 to begin the parameter-declaration-clause, followed by either a
24645 `)', an `...', or a decl-specifier. We need to check for a
24646 type-specifier to avoid being fooled into thinking that:
24647
24648 S (f) (int);
24649
24650 is a constructor. (It is actually a function named `f' that
24651 takes one parameter (of type `int') and returns a value of type
24652 `S'. */
24653 if (constructor_p
24654 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24655 constructor_p = false;
24656
24657 if (constructor_p
24658 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
24659 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
24660 /* A parameter declaration begins with a decl-specifier,
24661 which is either the "attribute" keyword, a storage class
24662 specifier, or (usually) a type-specifier. */
24663 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
24664 {
24665 tree type;
24666 tree pushed_scope = NULL_TREE;
24667 unsigned saved_num_template_parameter_lists;
24668
24669 /* Names appearing in the type-specifier should be looked up
24670 in the scope of the class. */
24671 if (current_class_type)
24672 type = NULL_TREE;
24673 else
24674 {
24675 type = TREE_TYPE (type_decl);
24676 if (TREE_CODE (type) == TYPENAME_TYPE)
24677 {
24678 type = resolve_typename_type (type,
24679 /*only_current_p=*/false);
24680 if (TREE_CODE (type) == TYPENAME_TYPE)
24681 {
24682 cp_parser_abort_tentative_parse (parser);
24683 return false;
24684 }
24685 }
24686 pushed_scope = push_scope (type);
24687 }
24688
24689 /* Inside the constructor parameter list, surrounding
24690 template-parameter-lists do not apply. */
24691 saved_num_template_parameter_lists
24692 = parser->num_template_parameter_lists;
24693 parser->num_template_parameter_lists = 0;
24694
24695 /* Look for the type-specifier. */
24696 cp_parser_type_specifier (parser,
24697 CP_PARSER_FLAGS_NONE,
24698 /*decl_specs=*/NULL,
24699 /*is_declarator=*/true,
24700 /*declares_class_or_enum=*/NULL,
24701 /*is_cv_qualifier=*/NULL);
24702
24703 parser->num_template_parameter_lists
24704 = saved_num_template_parameter_lists;
24705
24706 /* Leave the scope of the class. */
24707 if (pushed_scope)
24708 pop_scope (pushed_scope);
24709
24710 constructor_p = !cp_parser_error_occurred (parser);
24711 }
24712 }
24713
24714 /* We did not really want to consume any tokens. */
24715 cp_parser_abort_tentative_parse (parser);
24716
24717 return constructor_p;
24718 }
24719
24720 /* Parse the definition of the function given by the DECL_SPECIFIERS,
24721 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
24722 they must be performed once we are in the scope of the function.
24723
24724 Returns the function defined. */
24725
24726 static tree
24727 cp_parser_function_definition_from_specifiers_and_declarator
24728 (cp_parser* parser,
24729 cp_decl_specifier_seq *decl_specifiers,
24730 tree attributes,
24731 const cp_declarator *declarator)
24732 {
24733 tree fn;
24734 bool success_p;
24735
24736 /* Begin the function-definition. */
24737 success_p = start_function (decl_specifiers, declarator, attributes);
24738
24739 /* The things we're about to see are not directly qualified by any
24740 template headers we've seen thus far. */
24741 reset_specialization ();
24742
24743 /* If there were names looked up in the decl-specifier-seq that we
24744 did not check, check them now. We must wait until we are in the
24745 scope of the function to perform the checks, since the function
24746 might be a friend. */
24747 perform_deferred_access_checks (tf_warning_or_error);
24748
24749 if (success_p)
24750 {
24751 cp_finalize_omp_declare_simd (parser, current_function_decl);
24752 parser->omp_declare_simd = NULL;
24753 cp_finalize_oacc_routine (parser, current_function_decl, true, true);
24754 parser->oacc_routine = NULL;
24755 }
24756
24757 if (!success_p)
24758 {
24759 /* Skip the entire function. */
24760 cp_parser_skip_to_end_of_block_or_statement (parser);
24761 fn = error_mark_node;
24762 }
24763 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
24764 {
24765 /* Seen already, skip it. An error message has already been output. */
24766 cp_parser_skip_to_end_of_block_or_statement (parser);
24767 fn = current_function_decl;
24768 current_function_decl = NULL_TREE;
24769 /* If this is a function from a class, pop the nested class. */
24770 if (current_class_name)
24771 pop_nested_class ();
24772 }
24773 else
24774 {
24775 timevar_id_t tv;
24776 if (DECL_DECLARED_INLINE_P (current_function_decl))
24777 tv = TV_PARSE_INLINE;
24778 else
24779 tv = TV_PARSE_FUNC;
24780 timevar_push (tv);
24781 fn = cp_parser_function_definition_after_declarator (parser,
24782 /*inline_p=*/false);
24783 timevar_pop (tv);
24784 }
24785
24786 return fn;
24787 }
24788
24789 /* Parse the part of a function-definition that follows the
24790 declarator. INLINE_P is TRUE iff this function is an inline
24791 function defined within a class-specifier.
24792
24793 Returns the function defined. */
24794
24795 static tree
24796 cp_parser_function_definition_after_declarator (cp_parser* parser,
24797 bool inline_p)
24798 {
24799 tree fn;
24800 bool ctor_initializer_p = false;
24801 bool saved_in_unbraced_linkage_specification_p;
24802 bool saved_in_function_body;
24803 unsigned saved_num_template_parameter_lists;
24804 cp_token *token;
24805 bool fully_implicit_function_template_p
24806 = parser->fully_implicit_function_template_p;
24807 parser->fully_implicit_function_template_p = false;
24808 tree implicit_template_parms
24809 = parser->implicit_template_parms;
24810 parser->implicit_template_parms = 0;
24811 cp_binding_level* implicit_template_scope
24812 = parser->implicit_template_scope;
24813 parser->implicit_template_scope = 0;
24814
24815 saved_in_function_body = parser->in_function_body;
24816 parser->in_function_body = true;
24817 /* If the next token is `return', then the code may be trying to
24818 make use of the "named return value" extension that G++ used to
24819 support. */
24820 token = cp_lexer_peek_token (parser->lexer);
24821 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
24822 {
24823 /* Consume the `return' keyword. */
24824 cp_lexer_consume_token (parser->lexer);
24825 /* Look for the identifier that indicates what value is to be
24826 returned. */
24827 cp_parser_identifier (parser);
24828 /* Issue an error message. */
24829 error_at (token->location,
24830 "named return values are no longer supported");
24831 /* Skip tokens until we reach the start of the function body. */
24832 while (true)
24833 {
24834 cp_token *token = cp_lexer_peek_token (parser->lexer);
24835 if (token->type == CPP_OPEN_BRACE
24836 || token->type == CPP_EOF
24837 || token->type == CPP_PRAGMA_EOL)
24838 break;
24839 cp_lexer_consume_token (parser->lexer);
24840 }
24841 }
24842 /* The `extern' in `extern "C" void f () { ... }' does not apply to
24843 anything declared inside `f'. */
24844 saved_in_unbraced_linkage_specification_p
24845 = parser->in_unbraced_linkage_specification_p;
24846 parser->in_unbraced_linkage_specification_p = false;
24847 /* Inside the function, surrounding template-parameter-lists do not
24848 apply. */
24849 saved_num_template_parameter_lists
24850 = parser->num_template_parameter_lists;
24851 parser->num_template_parameter_lists = 0;
24852
24853 start_lambda_scope (current_function_decl);
24854
24855 /* If the next token is `try', `__transaction_atomic', or
24856 `__transaction_relaxed`, then we are looking at either function-try-block
24857 or function-transaction-block. Note that all of these include the
24858 function-body. */
24859 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
24860 ctor_initializer_p = cp_parser_function_transaction (parser,
24861 RID_TRANSACTION_ATOMIC);
24862 else if (cp_lexer_next_token_is_keyword (parser->lexer,
24863 RID_TRANSACTION_RELAXED))
24864 ctor_initializer_p = cp_parser_function_transaction (parser,
24865 RID_TRANSACTION_RELAXED);
24866 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
24867 ctor_initializer_p = cp_parser_function_try_block (parser);
24868 else
24869 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24870 (parser, /*in_function_try_block=*/false);
24871
24872 finish_lambda_scope ();
24873
24874 /* Finish the function. */
24875 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
24876 (inline_p ? 2 : 0));
24877 /* Generate code for it, if necessary. */
24878 expand_or_defer_fn (fn);
24879 /* Restore the saved values. */
24880 parser->in_unbraced_linkage_specification_p
24881 = saved_in_unbraced_linkage_specification_p;
24882 parser->num_template_parameter_lists
24883 = saved_num_template_parameter_lists;
24884 parser->in_function_body = saved_in_function_body;
24885
24886 parser->fully_implicit_function_template_p
24887 = fully_implicit_function_template_p;
24888 parser->implicit_template_parms
24889 = implicit_template_parms;
24890 parser->implicit_template_scope
24891 = implicit_template_scope;
24892
24893 if (parser->fully_implicit_function_template_p)
24894 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
24895
24896 return fn;
24897 }
24898
24899 /* Parse a template-declaration body (following argument list). */
24900
24901 static void
24902 cp_parser_template_declaration_after_parameters (cp_parser* parser,
24903 tree parameter_list,
24904 bool member_p)
24905 {
24906 tree decl = NULL_TREE;
24907 bool friend_p = false;
24908
24909 /* We just processed one more parameter list. */
24910 ++parser->num_template_parameter_lists;
24911
24912 /* Get the deferred access checks from the parameter list. These
24913 will be checked once we know what is being declared, as for a
24914 member template the checks must be performed in the scope of the
24915 class containing the member. */
24916 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
24917
24918 /* Tentatively parse for a new template parameter list, which can either be
24919 the template keyword or a template introduction. */
24920 if (cp_parser_template_declaration_after_export (parser, member_p))
24921 /* OK */;
24922 else if (cxx_dialect >= cxx11
24923 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24924 decl = cp_parser_alias_declaration (parser);
24925 else
24926 {
24927 /* There are no access checks when parsing a template, as we do not
24928 know if a specialization will be a friend. */
24929 push_deferring_access_checks (dk_no_check);
24930 cp_token *token = cp_lexer_peek_token (parser->lexer);
24931 decl = cp_parser_single_declaration (parser,
24932 checks,
24933 member_p,
24934 /*explicit_specialization_p=*/false,
24935 &friend_p);
24936 pop_deferring_access_checks ();
24937
24938 /* If this is a member template declaration, let the front
24939 end know. */
24940 if (member_p && !friend_p && decl)
24941 {
24942 if (TREE_CODE (decl) == TYPE_DECL)
24943 cp_parser_check_access_in_redeclaration (decl, token->location);
24944
24945 decl = finish_member_template_decl (decl);
24946 }
24947 else if (friend_p && decl
24948 && DECL_DECLARES_TYPE_P (decl))
24949 make_friend_class (current_class_type, TREE_TYPE (decl),
24950 /*complain=*/true);
24951 }
24952 /* We are done with the current parameter list. */
24953 --parser->num_template_parameter_lists;
24954
24955 pop_deferring_access_checks ();
24956
24957 /* Finish up. */
24958 finish_template_decl (parameter_list);
24959
24960 /* Check the template arguments for a literal operator template. */
24961 if (decl
24962 && DECL_DECLARES_FUNCTION_P (decl)
24963 && UDLIT_OPER_P (DECL_NAME (decl)))
24964 {
24965 bool ok = true;
24966 if (parameter_list == NULL_TREE)
24967 ok = false;
24968 else
24969 {
24970 int num_parms = TREE_VEC_LENGTH (parameter_list);
24971 if (num_parms == 1)
24972 {
24973 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
24974 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
24975 if (TREE_TYPE (parm) != char_type_node
24976 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
24977 ok = false;
24978 }
24979 else if (num_parms == 2 && cxx_dialect >= cxx14)
24980 {
24981 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
24982 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
24983 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
24984 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
24985 if (TREE_TYPE (parm) != TREE_TYPE (type)
24986 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
24987 ok = false;
24988 }
24989 else
24990 ok = false;
24991 }
24992 if (!ok)
24993 {
24994 if (cxx_dialect >= cxx14)
24995 error ("literal operator template %qD has invalid parameter list."
24996 " Expected non-type template argument pack <char...>"
24997 " or <typename CharT, CharT...>",
24998 decl);
24999 else
25000 error ("literal operator template %qD has invalid parameter list."
25001 " Expected non-type template argument pack <char...>",
25002 decl);
25003 }
25004 }
25005
25006 /* Register member declarations. */
25007 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
25008 finish_member_declaration (decl);
25009 /* If DECL is a function template, we must return to parse it later.
25010 (Even though there is no definition, there might be default
25011 arguments that need handling.) */
25012 if (member_p && decl
25013 && DECL_DECLARES_FUNCTION_P (decl))
25014 vec_safe_push (unparsed_funs_with_definitions, decl);
25015 }
25016
25017 /* Parse a template introduction header for a template-declaration. Returns
25018 false if tentative parse fails. */
25019
25020 static bool
25021 cp_parser_template_introduction (cp_parser* parser, bool member_p)
25022 {
25023 cp_parser_parse_tentatively (parser);
25024
25025 tree saved_scope = parser->scope;
25026 tree saved_object_scope = parser->object_scope;
25027 tree saved_qualifying_scope = parser->qualifying_scope;
25028
25029 /* Look for the optional `::' operator. */
25030 cp_parser_global_scope_opt (parser,
25031 /*current_scope_valid_p=*/false);
25032 /* Look for the nested-name-specifier. */
25033 cp_parser_nested_name_specifier_opt (parser,
25034 /*typename_keyword_p=*/false,
25035 /*check_dependency_p=*/true,
25036 /*type_p=*/false,
25037 /*is_declaration=*/false);
25038
25039 cp_token *token = cp_lexer_peek_token (parser->lexer);
25040 tree concept_name = cp_parser_identifier (parser);
25041
25042 /* Look up the concept for which we will be matching
25043 template parameters. */
25044 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
25045 token->location);
25046 parser->scope = saved_scope;
25047 parser->object_scope = saved_object_scope;
25048 parser->qualifying_scope = saved_qualifying_scope;
25049
25050 if (concept_name == error_mark_node)
25051 cp_parser_simulate_error (parser);
25052
25053 /* Look for opening brace for introduction. */
25054 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
25055
25056 if (!cp_parser_parse_definitely (parser))
25057 return false;
25058
25059 push_deferring_access_checks (dk_deferred);
25060
25061 /* Build vector of placeholder parameters and grab
25062 matching identifiers. */
25063 tree introduction_list = cp_parser_introduction_list (parser);
25064
25065 /* The introduction-list shall not be empty. */
25066 int nargs = TREE_VEC_LENGTH (introduction_list);
25067 if (nargs == 0)
25068 {
25069 error ("empty introduction-list");
25070 return true;
25071 }
25072
25073 /* Look for closing brace for introduction. */
25074 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25075 return true;
25076
25077 if (tmpl_decl == error_mark_node)
25078 {
25079 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
25080 token->location);
25081 return true;
25082 }
25083
25084 /* Build and associate the constraint. */
25085 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
25086 if (parms && parms != error_mark_node)
25087 {
25088 cp_parser_template_declaration_after_parameters (parser, parms,
25089 member_p);
25090 return true;
25091 }
25092
25093 error_at (token->location, "no matching concept for template-introduction");
25094 return true;
25095 }
25096
25097 /* Parse a normal template-declaration following the template keyword. */
25098
25099 static void
25100 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
25101 {
25102 tree parameter_list;
25103 bool need_lang_pop;
25104 location_t location = input_location;
25105
25106 /* Look for the `<' token. */
25107 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
25108 return;
25109 if (at_class_scope_p () && current_function_decl)
25110 {
25111 /* 14.5.2.2 [temp.mem]
25112
25113 A local class shall not have member templates. */
25114 error_at (location,
25115 "invalid declaration of member template in local class");
25116 cp_parser_skip_to_end_of_block_or_statement (parser);
25117 return;
25118 }
25119 /* [temp]
25120
25121 A template ... shall not have C linkage. */
25122 if (current_lang_name == lang_name_c)
25123 {
25124 error_at (location, "template with C linkage");
25125 /* Give it C++ linkage to avoid confusing other parts of the
25126 front end. */
25127 push_lang_context (lang_name_cplusplus);
25128 need_lang_pop = true;
25129 }
25130 else
25131 need_lang_pop = false;
25132
25133 /* We cannot perform access checks on the template parameter
25134 declarations until we know what is being declared, just as we
25135 cannot check the decl-specifier list. */
25136 push_deferring_access_checks (dk_deferred);
25137
25138 /* If the next token is `>', then we have an invalid
25139 specialization. Rather than complain about an invalid template
25140 parameter, issue an error message here. */
25141 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
25142 {
25143 cp_parser_error (parser, "invalid explicit specialization");
25144 begin_specialization ();
25145 parameter_list = NULL_TREE;
25146 }
25147 else
25148 {
25149 /* Parse the template parameters. */
25150 parameter_list = cp_parser_template_parameter_list (parser);
25151 }
25152
25153 /* Look for the `>'. */
25154 cp_parser_skip_to_end_of_template_parameter_list (parser);
25155
25156 /* Manage template requirements */
25157 if (flag_concepts)
25158 {
25159 tree reqs = get_shorthand_constraints (current_template_parms);
25160 if (tree r = cp_parser_requires_clause_opt (parser))
25161 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
25162 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
25163 }
25164
25165 cp_parser_template_declaration_after_parameters (parser, parameter_list,
25166 member_p);
25167
25168 /* For the erroneous case of a template with C linkage, we pushed an
25169 implicit C++ linkage scope; exit that scope now. */
25170 if (need_lang_pop)
25171 pop_lang_context ();
25172 }
25173
25174 /* Parse a template-declaration, assuming that the `export' (and
25175 `extern') keywords, if present, has already been scanned. MEMBER_P
25176 is as for cp_parser_template_declaration. */
25177
25178 static bool
25179 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
25180 {
25181 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25182 {
25183 cp_lexer_consume_token (parser->lexer);
25184 cp_parser_explicit_template_declaration (parser, member_p);
25185 return true;
25186 }
25187 else if (flag_concepts)
25188 return cp_parser_template_introduction (parser, member_p);
25189
25190 return false;
25191 }
25192
25193 /* Perform the deferred access checks from a template-parameter-list.
25194 CHECKS is a TREE_LIST of access checks, as returned by
25195 get_deferred_access_checks. */
25196
25197 static void
25198 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
25199 {
25200 ++processing_template_parmlist;
25201 perform_access_checks (checks, tf_warning_or_error);
25202 --processing_template_parmlist;
25203 }
25204
25205 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25206 `function-definition' sequence that follows a template header.
25207 If MEMBER_P is true, this declaration appears in a class scope.
25208
25209 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
25210 *FRIEND_P is set to TRUE iff the declaration is a friend. */
25211
25212 static tree
25213 cp_parser_single_declaration (cp_parser* parser,
25214 vec<deferred_access_check, va_gc> *checks,
25215 bool member_p,
25216 bool explicit_specialization_p,
25217 bool* friend_p)
25218 {
25219 int declares_class_or_enum;
25220 tree decl = NULL_TREE;
25221 cp_decl_specifier_seq decl_specifiers;
25222 bool function_definition_p = false;
25223 cp_token *decl_spec_token_start;
25224
25225 /* This function is only used when processing a template
25226 declaration. */
25227 gcc_assert (innermost_scope_kind () == sk_template_parms
25228 || innermost_scope_kind () == sk_template_spec);
25229
25230 /* Defer access checks until we know what is being declared. */
25231 push_deferring_access_checks (dk_deferred);
25232
25233 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25234 alternative. */
25235 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25236 cp_parser_decl_specifier_seq (parser,
25237 CP_PARSER_FLAGS_OPTIONAL,
25238 &decl_specifiers,
25239 &declares_class_or_enum);
25240 if (friend_p)
25241 *friend_p = cp_parser_friend_p (&decl_specifiers);
25242
25243 /* There are no template typedefs. */
25244 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25245 {
25246 error_at (decl_spec_token_start->location,
25247 "template declaration of %<typedef%>");
25248 decl = error_mark_node;
25249 }
25250
25251 /* Gather up the access checks that occurred the
25252 decl-specifier-seq. */
25253 stop_deferring_access_checks ();
25254
25255 /* Check for the declaration of a template class. */
25256 if (declares_class_or_enum)
25257 {
25258 if (cp_parser_declares_only_class_p (parser))
25259 {
25260 // If this is a declaration, but not a definition, associate
25261 // any constraints with the type declaration. Constraints
25262 // are associated with definitions in cp_parser_class_specifier.
25263 if (declares_class_or_enum == 1)
25264 associate_classtype_constraints (decl_specifiers.type);
25265
25266 decl = shadow_tag (&decl_specifiers);
25267
25268 /* In this case:
25269
25270 struct C {
25271 friend template <typename T> struct A<T>::B;
25272 };
25273
25274 A<T>::B will be represented by a TYPENAME_TYPE, and
25275 therefore not recognized by shadow_tag. */
25276 if (friend_p && *friend_p
25277 && !decl
25278 && decl_specifiers.type
25279 && TYPE_P (decl_specifiers.type))
25280 decl = decl_specifiers.type;
25281
25282 if (decl && decl != error_mark_node)
25283 decl = TYPE_NAME (decl);
25284 else
25285 decl = error_mark_node;
25286
25287 /* Perform access checks for template parameters. */
25288 cp_parser_perform_template_parameter_access_checks (checks);
25289 }
25290 }
25291
25292 /* Complain about missing 'typename' or other invalid type names. */
25293 if (!decl_specifiers.any_type_specifiers_p
25294 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25295 {
25296 /* cp_parser_parse_and_diagnose_invalid_type_name calls
25297 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
25298 the rest of this declaration. */
25299 decl = error_mark_node;
25300 goto out;
25301 }
25302
25303 /* If it's not a template class, try for a template function. If
25304 the next token is a `;', then this declaration does not declare
25305 anything. But, if there were errors in the decl-specifiers, then
25306 the error might well have come from an attempted class-specifier.
25307 In that case, there's no need to warn about a missing declarator. */
25308 if (!decl
25309 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
25310 || decl_specifiers.type != error_mark_node))
25311 {
25312 decl = cp_parser_init_declarator (parser,
25313 &decl_specifiers,
25314 checks,
25315 /*function_definition_allowed_p=*/true,
25316 member_p,
25317 declares_class_or_enum,
25318 &function_definition_p,
25319 NULL, true, NULL);
25320
25321 /* 7.1.1-1 [dcl.stc]
25322
25323 A storage-class-specifier shall not be specified in an explicit
25324 specialization... */
25325 if (decl
25326 && explicit_specialization_p
25327 && decl_specifiers.storage_class != sc_none)
25328 {
25329 error_at (decl_spec_token_start->location,
25330 "explicit template specialization cannot have a storage class");
25331 decl = error_mark_node;
25332 }
25333
25334 if (decl && VAR_P (decl))
25335 check_template_variable (decl);
25336 }
25337
25338 /* Look for a trailing `;' after the declaration. */
25339 if (!function_definition_p
25340 && (decl == error_mark_node
25341 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
25342 cp_parser_skip_to_end_of_block_or_statement (parser);
25343
25344 out:
25345 pop_deferring_access_checks ();
25346
25347 /* Clear any current qualification; whatever comes next is the start
25348 of something new. */
25349 parser->scope = NULL_TREE;
25350 parser->qualifying_scope = NULL_TREE;
25351 parser->object_scope = NULL_TREE;
25352
25353 return decl;
25354 }
25355
25356 /* Parse a cast-expression that is not the operand of a unary "&". */
25357
25358 static tree
25359 cp_parser_simple_cast_expression (cp_parser *parser)
25360 {
25361 return cp_parser_cast_expression (parser, /*address_p=*/false,
25362 /*cast_p=*/false, /*decltype*/false, NULL);
25363 }
25364
25365 /* Parse a functional cast to TYPE. Returns an expression
25366 representing the cast. */
25367
25368 static tree
25369 cp_parser_functional_cast (cp_parser* parser, tree type)
25370 {
25371 vec<tree, va_gc> *vec;
25372 tree expression_list;
25373 tree cast;
25374 bool nonconst_p;
25375
25376 if (!type)
25377 type = error_mark_node;
25378
25379 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25380 {
25381 cp_lexer_set_source_position (parser->lexer);
25382 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25383 expression_list = cp_parser_braced_list (parser, &nonconst_p);
25384 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
25385 if (TREE_CODE (type) == TYPE_DECL)
25386 type = TREE_TYPE (type);
25387 return finish_compound_literal (type, expression_list,
25388 tf_warning_or_error);
25389 }
25390
25391
25392 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25393 /*cast_p=*/true,
25394 /*allow_expansion_p=*/true,
25395 /*non_constant_p=*/NULL);
25396 if (vec == NULL)
25397 expression_list = error_mark_node;
25398 else
25399 {
25400 expression_list = build_tree_list_vec (vec);
25401 release_tree_vector (vec);
25402 }
25403
25404 cast = build_functional_cast (type, expression_list,
25405 tf_warning_or_error);
25406 /* [expr.const]/1: In an integral constant expression "only type
25407 conversions to integral or enumeration type can be used". */
25408 if (TREE_CODE (type) == TYPE_DECL)
25409 type = TREE_TYPE (type);
25410 if (cast != error_mark_node
25411 && !cast_valid_in_integral_constant_expression_p (type)
25412 && cp_parser_non_integral_constant_expression (parser,
25413 NIC_CONSTRUCTOR))
25414 return error_mark_node;
25415 return cast;
25416 }
25417
25418 /* Save the tokens that make up the body of a member function defined
25419 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
25420 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
25421 specifiers applied to the declaration. FIRST_DECL indicates if
25422 DECLARATOR is the first declarator in a declaration sequence. Returns
25423 the FUNCTION_DECL for the member function. */
25424
25425 static tree
25426 cp_parser_save_member_function_body (cp_parser* parser,
25427 cp_decl_specifier_seq *decl_specifiers,
25428 cp_declarator *declarator,
25429 tree attributes, bool first_decl)
25430 {
25431 cp_token *first;
25432 cp_token *last;
25433 tree fn;
25434
25435 /* Create the FUNCTION_DECL. */
25436 fn = grokmethod (decl_specifiers, declarator, attributes);
25437 cp_finalize_omp_declare_simd (parser, fn);
25438 cp_finalize_oacc_routine (parser, fn, true, first_decl);
25439 /* If something went badly wrong, bail out now. */
25440 if (fn == error_mark_node)
25441 {
25442 /* If there's a function-body, skip it. */
25443 if (cp_parser_token_starts_function_definition_p
25444 (cp_lexer_peek_token (parser->lexer)))
25445 cp_parser_skip_to_end_of_block_or_statement (parser);
25446 return error_mark_node;
25447 }
25448
25449 /* Remember it, if there default args to post process. */
25450 cp_parser_save_default_args (parser, fn);
25451
25452 /* Save away the tokens that make up the body of the
25453 function. */
25454 first = parser->lexer->next_token;
25455 /* Handle function try blocks. */
25456 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25457 cp_lexer_consume_token (parser->lexer);
25458 /* We can have braced-init-list mem-initializers before the fn body. */
25459 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
25460 {
25461 cp_lexer_consume_token (parser->lexer);
25462 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25463 {
25464 /* cache_group will stop after an un-nested { } pair, too. */
25465 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
25466 break;
25467
25468 /* variadic mem-inits have ... after the ')'. */
25469 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25470 cp_lexer_consume_token (parser->lexer);
25471 }
25472 }
25473 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25474 /* Handle function try blocks. */
25475 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
25476 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25477 last = parser->lexer->next_token;
25478
25479 /* Save away the inline definition; we will process it when the
25480 class is complete. */
25481 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
25482 DECL_PENDING_INLINE_P (fn) = 1;
25483
25484 /* We need to know that this was defined in the class, so that
25485 friend templates are handled correctly. */
25486 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
25487
25488 /* Add FN to the queue of functions to be parsed later. */
25489 vec_safe_push (unparsed_funs_with_definitions, fn);
25490
25491 return fn;
25492 }
25493
25494 /* Save the tokens that make up the in-class initializer for a non-static
25495 data member. Returns a DEFAULT_ARG. */
25496
25497 static tree
25498 cp_parser_save_nsdmi (cp_parser* parser)
25499 {
25500 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
25501 }
25502
25503 /* Parse a template-argument-list, as well as the trailing ">" (but
25504 not the opening "<"). See cp_parser_template_argument_list for the
25505 return value. */
25506
25507 static tree
25508 cp_parser_enclosed_template_argument_list (cp_parser* parser)
25509 {
25510 tree arguments;
25511 tree saved_scope;
25512 tree saved_qualifying_scope;
25513 tree saved_object_scope;
25514 bool saved_greater_than_is_operator_p;
25515 int saved_unevaluated_operand;
25516 int saved_inhibit_evaluation_warnings;
25517
25518 /* [temp.names]
25519
25520 When parsing a template-id, the first non-nested `>' is taken as
25521 the end of the template-argument-list rather than a greater-than
25522 operator. */
25523 saved_greater_than_is_operator_p
25524 = parser->greater_than_is_operator_p;
25525 parser->greater_than_is_operator_p = false;
25526 /* Parsing the argument list may modify SCOPE, so we save it
25527 here. */
25528 saved_scope = parser->scope;
25529 saved_qualifying_scope = parser->qualifying_scope;
25530 saved_object_scope = parser->object_scope;
25531 /* We need to evaluate the template arguments, even though this
25532 template-id may be nested within a "sizeof". */
25533 saved_unevaluated_operand = cp_unevaluated_operand;
25534 cp_unevaluated_operand = 0;
25535 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25536 c_inhibit_evaluation_warnings = 0;
25537 /* Parse the template-argument-list itself. */
25538 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
25539 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
25540 arguments = NULL_TREE;
25541 else
25542 arguments = cp_parser_template_argument_list (parser);
25543 /* Look for the `>' that ends the template-argument-list. If we find
25544 a '>>' instead, it's probably just a typo. */
25545 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
25546 {
25547 if (cxx_dialect != cxx98)
25548 {
25549 /* In C++0x, a `>>' in a template argument list or cast
25550 expression is considered to be two separate `>'
25551 tokens. So, change the current token to a `>', but don't
25552 consume it: it will be consumed later when the outer
25553 template argument list (or cast expression) is parsed.
25554 Note that this replacement of `>' for `>>' is necessary
25555 even if we are parsing tentatively: in the tentative
25556 case, after calling
25557 cp_parser_enclosed_template_argument_list we will always
25558 throw away all of the template arguments and the first
25559 closing `>', either because the template argument list
25560 was erroneous or because we are replacing those tokens
25561 with a CPP_TEMPLATE_ID token. The second `>' (which will
25562 not have been thrown away) is needed either to close an
25563 outer template argument list or to complete a new-style
25564 cast. */
25565 cp_token *token = cp_lexer_peek_token (parser->lexer);
25566 token->type = CPP_GREATER;
25567 }
25568 else if (!saved_greater_than_is_operator_p)
25569 {
25570 /* If we're in a nested template argument list, the '>>' has
25571 to be a typo for '> >'. We emit the error message, but we
25572 continue parsing and we push a '>' as next token, so that
25573 the argument list will be parsed correctly. Note that the
25574 global source location is still on the token before the
25575 '>>', so we need to say explicitly where we want it. */
25576 cp_token *token = cp_lexer_peek_token (parser->lexer);
25577 error_at (token->location, "%<>>%> should be %<> >%> "
25578 "within a nested template argument list");
25579
25580 token->type = CPP_GREATER;
25581 }
25582 else
25583 {
25584 /* If this is not a nested template argument list, the '>>'
25585 is a typo for '>'. Emit an error message and continue.
25586 Same deal about the token location, but here we can get it
25587 right by consuming the '>>' before issuing the diagnostic. */
25588 cp_token *token = cp_lexer_consume_token (parser->lexer);
25589 error_at (token->location,
25590 "spurious %<>>%>, use %<>%> to terminate "
25591 "a template argument list");
25592 }
25593 }
25594 else
25595 cp_parser_skip_to_end_of_template_parameter_list (parser);
25596 /* The `>' token might be a greater-than operator again now. */
25597 parser->greater_than_is_operator_p
25598 = saved_greater_than_is_operator_p;
25599 /* Restore the SAVED_SCOPE. */
25600 parser->scope = saved_scope;
25601 parser->qualifying_scope = saved_qualifying_scope;
25602 parser->object_scope = saved_object_scope;
25603 cp_unevaluated_operand = saved_unevaluated_operand;
25604 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25605
25606 return arguments;
25607 }
25608
25609 /* MEMBER_FUNCTION is a member function, or a friend. If default
25610 arguments, or the body of the function have not yet been parsed,
25611 parse them now. */
25612
25613 static void
25614 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
25615 {
25616 timevar_push (TV_PARSE_INMETH);
25617 /* If this member is a template, get the underlying
25618 FUNCTION_DECL. */
25619 if (DECL_FUNCTION_TEMPLATE_P (member_function))
25620 member_function = DECL_TEMPLATE_RESULT (member_function);
25621
25622 /* There should not be any class definitions in progress at this
25623 point; the bodies of members are only parsed outside of all class
25624 definitions. */
25625 gcc_assert (parser->num_classes_being_defined == 0);
25626 /* While we're parsing the member functions we might encounter more
25627 classes. We want to handle them right away, but we don't want
25628 them getting mixed up with functions that are currently in the
25629 queue. */
25630 push_unparsed_function_queues (parser);
25631
25632 /* Make sure that any template parameters are in scope. */
25633 maybe_begin_member_template_processing (member_function);
25634
25635 /* If the body of the function has not yet been parsed, parse it
25636 now. */
25637 if (DECL_PENDING_INLINE_P (member_function))
25638 {
25639 tree function_scope;
25640 cp_token_cache *tokens;
25641
25642 /* The function is no longer pending; we are processing it. */
25643 tokens = DECL_PENDING_INLINE_INFO (member_function);
25644 DECL_PENDING_INLINE_INFO (member_function) = NULL;
25645 DECL_PENDING_INLINE_P (member_function) = 0;
25646
25647 /* If this is a local class, enter the scope of the containing
25648 function. */
25649 function_scope = current_function_decl;
25650 if (function_scope)
25651 push_function_context ();
25652
25653 /* Push the body of the function onto the lexer stack. */
25654 cp_parser_push_lexer_for_tokens (parser, tokens);
25655
25656 /* Let the front end know that we going to be defining this
25657 function. */
25658 start_preparsed_function (member_function, NULL_TREE,
25659 SF_PRE_PARSED | SF_INCLASS_INLINE);
25660
25661 /* Don't do access checking if it is a templated function. */
25662 if (processing_template_decl)
25663 push_deferring_access_checks (dk_no_check);
25664
25665 /* #pragma omp declare reduction needs special parsing. */
25666 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
25667 {
25668 parser->lexer->in_pragma = true;
25669 cp_parser_omp_declare_reduction_exprs (member_function, parser);
25670 finish_function (/*inline*/2);
25671 cp_check_omp_declare_reduction (member_function);
25672 }
25673 else
25674 /* Now, parse the body of the function. */
25675 cp_parser_function_definition_after_declarator (parser,
25676 /*inline_p=*/true);
25677
25678 if (processing_template_decl)
25679 pop_deferring_access_checks ();
25680
25681 /* Leave the scope of the containing function. */
25682 if (function_scope)
25683 pop_function_context ();
25684 cp_parser_pop_lexer (parser);
25685 }
25686
25687 /* Remove any template parameters from the symbol table. */
25688 maybe_end_member_template_processing ();
25689
25690 /* Restore the queue. */
25691 pop_unparsed_function_queues (parser);
25692 timevar_pop (TV_PARSE_INMETH);
25693 }
25694
25695 /* If DECL contains any default args, remember it on the unparsed
25696 functions queue. */
25697
25698 static void
25699 cp_parser_save_default_args (cp_parser* parser, tree decl)
25700 {
25701 tree probe;
25702
25703 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
25704 probe;
25705 probe = TREE_CHAIN (probe))
25706 if (TREE_PURPOSE (probe))
25707 {
25708 cp_default_arg_entry entry = {current_class_type, decl};
25709 vec_safe_push (unparsed_funs_with_default_args, entry);
25710 break;
25711 }
25712 }
25713
25714 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
25715 which is either a FIELD_DECL or PARM_DECL. Parse it and return
25716 the result. For a PARM_DECL, PARMTYPE is the corresponding type
25717 from the parameter-type-list. */
25718
25719 static tree
25720 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
25721 tree default_arg, tree parmtype)
25722 {
25723 cp_token_cache *tokens;
25724 tree parsed_arg;
25725 bool dummy;
25726
25727 if (default_arg == error_mark_node)
25728 return error_mark_node;
25729
25730 /* Push the saved tokens for the default argument onto the parser's
25731 lexer stack. */
25732 tokens = DEFARG_TOKENS (default_arg);
25733 cp_parser_push_lexer_for_tokens (parser, tokens);
25734
25735 start_lambda_scope (decl);
25736
25737 /* Parse the default argument. */
25738 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
25739 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
25740 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25741
25742 finish_lambda_scope ();
25743
25744 if (parsed_arg == error_mark_node)
25745 cp_parser_skip_to_end_of_statement (parser);
25746
25747 if (!processing_template_decl)
25748 {
25749 /* In a non-template class, check conversions now. In a template,
25750 we'll wait and instantiate these as needed. */
25751 if (TREE_CODE (decl) == PARM_DECL)
25752 parsed_arg = check_default_argument (parmtype, parsed_arg,
25753 tf_warning_or_error);
25754 else
25755 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
25756 }
25757
25758 /* If the token stream has not been completely used up, then
25759 there was extra junk after the end of the default
25760 argument. */
25761 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
25762 {
25763 if (TREE_CODE (decl) == PARM_DECL)
25764 cp_parser_error (parser, "expected %<,%>");
25765 else
25766 cp_parser_error (parser, "expected %<;%>");
25767 }
25768
25769 /* Revert to the main lexer. */
25770 cp_parser_pop_lexer (parser);
25771
25772 return parsed_arg;
25773 }
25774
25775 /* FIELD is a non-static data member with an initializer which we saved for
25776 later; parse it now. */
25777
25778 static void
25779 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
25780 {
25781 tree def;
25782
25783 maybe_begin_member_template_processing (field);
25784
25785 push_unparsed_function_queues (parser);
25786 def = cp_parser_late_parse_one_default_arg (parser, field,
25787 DECL_INITIAL (field),
25788 NULL_TREE);
25789 pop_unparsed_function_queues (parser);
25790
25791 maybe_end_member_template_processing ();
25792
25793 DECL_INITIAL (field) = def;
25794 }
25795
25796 /* FN is a FUNCTION_DECL which may contains a parameter with an
25797 unparsed DEFAULT_ARG. Parse the default args now. This function
25798 assumes that the current scope is the scope in which the default
25799 argument should be processed. */
25800
25801 static void
25802 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
25803 {
25804 bool saved_local_variables_forbidden_p;
25805 tree parm, parmdecl;
25806
25807 /* While we're parsing the default args, we might (due to the
25808 statement expression extension) encounter more classes. We want
25809 to handle them right away, but we don't want them getting mixed
25810 up with default args that are currently in the queue. */
25811 push_unparsed_function_queues (parser);
25812
25813 /* Local variable names (and the `this' keyword) may not appear
25814 in a default argument. */
25815 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
25816 parser->local_variables_forbidden_p = true;
25817
25818 push_defarg_context (fn);
25819
25820 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
25821 parmdecl = DECL_ARGUMENTS (fn);
25822 parm && parm != void_list_node;
25823 parm = TREE_CHAIN (parm),
25824 parmdecl = DECL_CHAIN (parmdecl))
25825 {
25826 tree default_arg = TREE_PURPOSE (parm);
25827 tree parsed_arg;
25828 vec<tree, va_gc> *insts;
25829 tree copy;
25830 unsigned ix;
25831
25832 if (!default_arg)
25833 continue;
25834
25835 if (TREE_CODE (default_arg) != DEFAULT_ARG)
25836 /* This can happen for a friend declaration for a function
25837 already declared with default arguments. */
25838 continue;
25839
25840 parsed_arg
25841 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
25842 default_arg,
25843 TREE_VALUE (parm));
25844 if (parsed_arg == error_mark_node)
25845 {
25846 continue;
25847 }
25848
25849 TREE_PURPOSE (parm) = parsed_arg;
25850
25851 /* Update any instantiations we've already created. */
25852 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
25853 vec_safe_iterate (insts, ix, &copy); ix++)
25854 TREE_PURPOSE (copy) = parsed_arg;
25855 }
25856
25857 pop_defarg_context ();
25858
25859 /* Make sure no default arg is missing. */
25860 check_default_args (fn);
25861
25862 /* Restore the state of local_variables_forbidden_p. */
25863 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
25864
25865 /* Restore the queue. */
25866 pop_unparsed_function_queues (parser);
25867 }
25868
25869 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
25870
25871 sizeof ... ( identifier )
25872
25873 where the 'sizeof' token has already been consumed. */
25874
25875 static tree
25876 cp_parser_sizeof_pack (cp_parser *parser)
25877 {
25878 /* Consume the `...'. */
25879 cp_lexer_consume_token (parser->lexer);
25880 maybe_warn_variadic_templates ();
25881
25882 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
25883 if (paren)
25884 cp_lexer_consume_token (parser->lexer);
25885 else
25886 permerror (cp_lexer_peek_token (parser->lexer)->location,
25887 "%<sizeof...%> argument must be surrounded by parentheses");
25888
25889 cp_token *token = cp_lexer_peek_token (parser->lexer);
25890 tree name = cp_parser_identifier (parser);
25891 if (name == error_mark_node)
25892 return error_mark_node;
25893 /* The name is not qualified. */
25894 parser->scope = NULL_TREE;
25895 parser->qualifying_scope = NULL_TREE;
25896 parser->object_scope = NULL_TREE;
25897 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
25898 if (expr == error_mark_node)
25899 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
25900 token->location);
25901 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
25902 expr = TREE_TYPE (expr);
25903 else if (TREE_CODE (expr) == CONST_DECL)
25904 expr = DECL_INITIAL (expr);
25905 expr = make_pack_expansion (expr);
25906
25907 if (paren)
25908 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25909
25910 return expr;
25911 }
25912
25913 /* Parse the operand of `sizeof' (or a similar operator). Returns
25914 either a TYPE or an expression, depending on the form of the
25915 input. The KEYWORD indicates which kind of expression we have
25916 encountered. */
25917
25918 static tree
25919 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
25920 {
25921 tree expr = NULL_TREE;
25922 const char *saved_message;
25923 char *tmp;
25924 bool saved_integral_constant_expression_p;
25925 bool saved_non_integral_constant_expression_p;
25926
25927 /* If it's a `...', then we are computing the length of a parameter
25928 pack. */
25929 if (keyword == RID_SIZEOF
25930 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25931 return cp_parser_sizeof_pack (parser);
25932
25933 /* Types cannot be defined in a `sizeof' expression. Save away the
25934 old message. */
25935 saved_message = parser->type_definition_forbidden_message;
25936 /* And create the new one. */
25937 tmp = concat ("types may not be defined in %<",
25938 IDENTIFIER_POINTER (ridpointers[keyword]),
25939 "%> expressions", NULL);
25940 parser->type_definition_forbidden_message = tmp;
25941
25942 /* The restrictions on constant-expressions do not apply inside
25943 sizeof expressions. */
25944 saved_integral_constant_expression_p
25945 = parser->integral_constant_expression_p;
25946 saved_non_integral_constant_expression_p
25947 = parser->non_integral_constant_expression_p;
25948 parser->integral_constant_expression_p = false;
25949
25950 /* Do not actually evaluate the expression. */
25951 ++cp_unevaluated_operand;
25952 ++c_inhibit_evaluation_warnings;
25953 /* If it's a `(', then we might be looking at the type-id
25954 construction. */
25955 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25956 {
25957 tree type = NULL_TREE;
25958
25959 /* We can't be sure yet whether we're looking at a type-id or an
25960 expression. */
25961 cp_parser_parse_tentatively (parser);
25962 /* Note: as a GNU Extension, compound literals are considered
25963 postfix-expressions as they are in C99, so they are valid
25964 arguments to sizeof. See comment in cp_parser_cast_expression
25965 for details. */
25966 if (cp_parser_compound_literal_p (parser))
25967 cp_parser_simulate_error (parser);
25968 else
25969 {
25970 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
25971 parser->in_type_id_in_expr_p = true;
25972 /* Look for the type-id. */
25973 type = cp_parser_type_id (parser);
25974 /* Look for the closing `)'. */
25975 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25976 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
25977 }
25978
25979 /* If all went well, then we're done. */
25980 if (cp_parser_parse_definitely (parser))
25981 {
25982 cp_decl_specifier_seq decl_specs;
25983
25984 /* Build a trivial decl-specifier-seq. */
25985 clear_decl_specs (&decl_specs);
25986 decl_specs.type = type;
25987
25988 /* Call grokdeclarator to figure out what type this is. */
25989 expr = grokdeclarator (NULL,
25990 &decl_specs,
25991 TYPENAME,
25992 /*initialized=*/0,
25993 /*attrlist=*/NULL);
25994 }
25995 }
25996
25997 /* If the type-id production did not work out, then we must be
25998 looking at the unary-expression production. */
25999 if (!expr)
26000 expr = cp_parser_unary_expression (parser);
26001
26002 /* Go back to evaluating expressions. */
26003 --cp_unevaluated_operand;
26004 --c_inhibit_evaluation_warnings;
26005
26006 /* Free the message we created. */
26007 free (tmp);
26008 /* And restore the old one. */
26009 parser->type_definition_forbidden_message = saved_message;
26010 parser->integral_constant_expression_p
26011 = saved_integral_constant_expression_p;
26012 parser->non_integral_constant_expression_p
26013 = saved_non_integral_constant_expression_p;
26014
26015 return expr;
26016 }
26017
26018 /* If the current declaration has no declarator, return true. */
26019
26020 static bool
26021 cp_parser_declares_only_class_p (cp_parser *parser)
26022 {
26023 /* If the next token is a `;' or a `,' then there is no
26024 declarator. */
26025 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26026 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
26027 }
26028
26029 /* Update the DECL_SPECS to reflect the storage class indicated by
26030 KEYWORD. */
26031
26032 static void
26033 cp_parser_set_storage_class (cp_parser *parser,
26034 cp_decl_specifier_seq *decl_specs,
26035 enum rid keyword,
26036 cp_token *token)
26037 {
26038 cp_storage_class storage_class;
26039
26040 if (parser->in_unbraced_linkage_specification_p)
26041 {
26042 error_at (token->location, "invalid use of %qD in linkage specification",
26043 ridpointers[keyword]);
26044 return;
26045 }
26046 else if (decl_specs->storage_class != sc_none)
26047 {
26048 decl_specs->conflicting_specifiers_p = true;
26049 return;
26050 }
26051
26052 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
26053 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
26054 && decl_specs->gnu_thread_keyword_p)
26055 {
26056 pedwarn (decl_specs->locations[ds_thread], 0,
26057 "%<__thread%> before %qD", ridpointers[keyword]);
26058 }
26059
26060 switch (keyword)
26061 {
26062 case RID_AUTO:
26063 storage_class = sc_auto;
26064 break;
26065 case RID_REGISTER:
26066 storage_class = sc_register;
26067 break;
26068 case RID_STATIC:
26069 storage_class = sc_static;
26070 break;
26071 case RID_EXTERN:
26072 storage_class = sc_extern;
26073 break;
26074 case RID_MUTABLE:
26075 storage_class = sc_mutable;
26076 break;
26077 default:
26078 gcc_unreachable ();
26079 }
26080 decl_specs->storage_class = storage_class;
26081 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
26082
26083 /* A storage class specifier cannot be applied alongside a typedef
26084 specifier. If there is a typedef specifier present then set
26085 conflicting_specifiers_p which will trigger an error later
26086 on in grokdeclarator. */
26087 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
26088 decl_specs->conflicting_specifiers_p = true;
26089 }
26090
26091 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
26092 is true, the type is a class or enum definition. */
26093
26094 static void
26095 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
26096 tree type_spec,
26097 cp_token *token,
26098 bool type_definition_p)
26099 {
26100 decl_specs->any_specifiers_p = true;
26101
26102 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
26103 (with, for example, in "typedef int wchar_t;") we remember that
26104 this is what happened. In system headers, we ignore these
26105 declarations so that G++ can work with system headers that are not
26106 C++-safe. */
26107 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
26108 && !type_definition_p
26109 && (type_spec == boolean_type_node
26110 || type_spec == char16_type_node
26111 || type_spec == char32_type_node
26112 || type_spec == wchar_type_node)
26113 && (decl_specs->type
26114 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
26115 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
26116 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
26117 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
26118 {
26119 decl_specs->redefined_builtin_type = type_spec;
26120 set_and_check_decl_spec_loc (decl_specs,
26121 ds_redefined_builtin_type_spec,
26122 token);
26123 if (!decl_specs->type)
26124 {
26125 decl_specs->type = type_spec;
26126 decl_specs->type_definition_p = false;
26127 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
26128 }
26129 }
26130 else if (decl_specs->type)
26131 decl_specs->multiple_types_p = true;
26132 else
26133 {
26134 decl_specs->type = type_spec;
26135 decl_specs->type_definition_p = type_definition_p;
26136 decl_specs->redefined_builtin_type = NULL_TREE;
26137 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
26138 }
26139 }
26140
26141 /* True iff TOKEN is the GNU keyword __thread. */
26142
26143 static bool
26144 token_is__thread (cp_token *token)
26145 {
26146 gcc_assert (token->keyword == RID_THREAD);
26147 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
26148 }
26149
26150 /* Set the location for a declarator specifier and check if it is
26151 duplicated.
26152
26153 DECL_SPECS is the sequence of declarator specifiers onto which to
26154 set the location.
26155
26156 DS is the single declarator specifier to set which location is to
26157 be set onto the existing sequence of declarators.
26158
26159 LOCATION is the location for the declarator specifier to
26160 consider. */
26161
26162 static void
26163 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
26164 cp_decl_spec ds, cp_token *token)
26165 {
26166 gcc_assert (ds < ds_last);
26167
26168 if (decl_specs == NULL)
26169 return;
26170
26171 source_location location = token->location;
26172
26173 if (decl_specs->locations[ds] == 0)
26174 {
26175 decl_specs->locations[ds] = location;
26176 if (ds == ds_thread)
26177 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
26178 }
26179 else
26180 {
26181 if (ds == ds_long)
26182 {
26183 if (decl_specs->locations[ds_long_long] != 0)
26184 error_at (location,
26185 "%<long long long%> is too long for GCC");
26186 else
26187 {
26188 decl_specs->locations[ds_long_long] = location;
26189 pedwarn_cxx98 (location,
26190 OPT_Wlong_long,
26191 "ISO C++ 1998 does not support %<long long%>");
26192 }
26193 }
26194 else if (ds == ds_thread)
26195 {
26196 bool gnu = token_is__thread (token);
26197 if (gnu != decl_specs->gnu_thread_keyword_p)
26198 error_at (location,
26199 "both %<__thread%> and %<thread_local%> specified");
26200 else
26201 error_at (location, "duplicate %qD", token->u.value);
26202 }
26203 else
26204 {
26205 static const char *const decl_spec_names[] = {
26206 "signed",
26207 "unsigned",
26208 "short",
26209 "long",
26210 "const",
26211 "volatile",
26212 "restrict",
26213 "inline",
26214 "virtual",
26215 "explicit",
26216 "friend",
26217 "typedef",
26218 "using",
26219 "constexpr",
26220 "__complex"
26221 };
26222 error_at (location,
26223 "duplicate %qs", decl_spec_names[ds]);
26224 }
26225 }
26226 }
26227
26228 /* Return true iff the declarator specifier DS is present in the
26229 sequence of declarator specifiers DECL_SPECS. */
26230
26231 bool
26232 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
26233 cp_decl_spec ds)
26234 {
26235 gcc_assert (ds < ds_last);
26236
26237 if (decl_specs == NULL)
26238 return false;
26239
26240 return decl_specs->locations[ds] != 0;
26241 }
26242
26243 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
26244 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
26245
26246 static bool
26247 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
26248 {
26249 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
26250 }
26251
26252 /* Issue an error message indicating that TOKEN_DESC was expected.
26253 If KEYWORD is true, it indicated this function is called by
26254 cp_parser_require_keword and the required token can only be
26255 a indicated keyword. */
26256
26257 static void
26258 cp_parser_required_error (cp_parser *parser,
26259 required_token token_desc,
26260 bool keyword)
26261 {
26262 switch (token_desc)
26263 {
26264 case RT_NEW:
26265 cp_parser_error (parser, "expected %<new%>");
26266 return;
26267 case RT_DELETE:
26268 cp_parser_error (parser, "expected %<delete%>");
26269 return;
26270 case RT_RETURN:
26271 cp_parser_error (parser, "expected %<return%>");
26272 return;
26273 case RT_WHILE:
26274 cp_parser_error (parser, "expected %<while%>");
26275 return;
26276 case RT_EXTERN:
26277 cp_parser_error (parser, "expected %<extern%>");
26278 return;
26279 case RT_STATIC_ASSERT:
26280 cp_parser_error (parser, "expected %<static_assert%>");
26281 return;
26282 case RT_DECLTYPE:
26283 cp_parser_error (parser, "expected %<decltype%>");
26284 return;
26285 case RT_OPERATOR:
26286 cp_parser_error (parser, "expected %<operator%>");
26287 return;
26288 case RT_CLASS:
26289 cp_parser_error (parser, "expected %<class%>");
26290 return;
26291 case RT_TEMPLATE:
26292 cp_parser_error (parser, "expected %<template%>");
26293 return;
26294 case RT_NAMESPACE:
26295 cp_parser_error (parser, "expected %<namespace%>");
26296 return;
26297 case RT_USING:
26298 cp_parser_error (parser, "expected %<using%>");
26299 return;
26300 case RT_ASM:
26301 cp_parser_error (parser, "expected %<asm%>");
26302 return;
26303 case RT_TRY:
26304 cp_parser_error (parser, "expected %<try%>");
26305 return;
26306 case RT_CATCH:
26307 cp_parser_error (parser, "expected %<catch%>");
26308 return;
26309 case RT_THROW:
26310 cp_parser_error (parser, "expected %<throw%>");
26311 return;
26312 case RT_LABEL:
26313 cp_parser_error (parser, "expected %<__label__%>");
26314 return;
26315 case RT_AT_TRY:
26316 cp_parser_error (parser, "expected %<@try%>");
26317 return;
26318 case RT_AT_SYNCHRONIZED:
26319 cp_parser_error (parser, "expected %<@synchronized%>");
26320 return;
26321 case RT_AT_THROW:
26322 cp_parser_error (parser, "expected %<@throw%>");
26323 return;
26324 case RT_TRANSACTION_ATOMIC:
26325 cp_parser_error (parser, "expected %<__transaction_atomic%>");
26326 return;
26327 case RT_TRANSACTION_RELAXED:
26328 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
26329 return;
26330 default:
26331 break;
26332 }
26333 if (!keyword)
26334 {
26335 switch (token_desc)
26336 {
26337 case RT_SEMICOLON:
26338 cp_parser_error (parser, "expected %<;%>");
26339 return;
26340 case RT_OPEN_PAREN:
26341 cp_parser_error (parser, "expected %<(%>");
26342 return;
26343 case RT_CLOSE_BRACE:
26344 cp_parser_error (parser, "expected %<}%>");
26345 return;
26346 case RT_OPEN_BRACE:
26347 cp_parser_error (parser, "expected %<{%>");
26348 return;
26349 case RT_CLOSE_SQUARE:
26350 cp_parser_error (parser, "expected %<]%>");
26351 return;
26352 case RT_OPEN_SQUARE:
26353 cp_parser_error (parser, "expected %<[%>");
26354 return;
26355 case RT_COMMA:
26356 cp_parser_error (parser, "expected %<,%>");
26357 return;
26358 case RT_SCOPE:
26359 cp_parser_error (parser, "expected %<::%>");
26360 return;
26361 case RT_LESS:
26362 cp_parser_error (parser, "expected %<<%>");
26363 return;
26364 case RT_GREATER:
26365 cp_parser_error (parser, "expected %<>%>");
26366 return;
26367 case RT_EQ:
26368 cp_parser_error (parser, "expected %<=%>");
26369 return;
26370 case RT_ELLIPSIS:
26371 cp_parser_error (parser, "expected %<...%>");
26372 return;
26373 case RT_MULT:
26374 cp_parser_error (parser, "expected %<*%>");
26375 return;
26376 case RT_COMPL:
26377 cp_parser_error (parser, "expected %<~%>");
26378 return;
26379 case RT_COLON:
26380 cp_parser_error (parser, "expected %<:%>");
26381 return;
26382 case RT_COLON_SCOPE:
26383 cp_parser_error (parser, "expected %<:%> or %<::%>");
26384 return;
26385 case RT_CLOSE_PAREN:
26386 cp_parser_error (parser, "expected %<)%>");
26387 return;
26388 case RT_COMMA_CLOSE_PAREN:
26389 cp_parser_error (parser, "expected %<,%> or %<)%>");
26390 return;
26391 case RT_PRAGMA_EOL:
26392 cp_parser_error (parser, "expected end of line");
26393 return;
26394 case RT_NAME:
26395 cp_parser_error (parser, "expected identifier");
26396 return;
26397 case RT_SELECT:
26398 cp_parser_error (parser, "expected selection-statement");
26399 return;
26400 case RT_INTERATION:
26401 cp_parser_error (parser, "expected iteration-statement");
26402 return;
26403 case RT_JUMP:
26404 cp_parser_error (parser, "expected jump-statement");
26405 return;
26406 case RT_CLASS_KEY:
26407 cp_parser_error (parser, "expected class-key");
26408 return;
26409 case RT_CLASS_TYPENAME_TEMPLATE:
26410 cp_parser_error (parser,
26411 "expected %<class%>, %<typename%>, or %<template%>");
26412 return;
26413 default:
26414 gcc_unreachable ();
26415 }
26416 }
26417 else
26418 gcc_unreachable ();
26419 }
26420
26421
26422
26423 /* If the next token is of the indicated TYPE, consume it. Otherwise,
26424 issue an error message indicating that TOKEN_DESC was expected.
26425
26426 Returns the token consumed, if the token had the appropriate type.
26427 Otherwise, returns NULL. */
26428
26429 static cp_token *
26430 cp_parser_require (cp_parser* parser,
26431 enum cpp_ttype type,
26432 required_token token_desc)
26433 {
26434 if (cp_lexer_next_token_is (parser->lexer, type))
26435 return cp_lexer_consume_token (parser->lexer);
26436 else
26437 {
26438 /* Output the MESSAGE -- unless we're parsing tentatively. */
26439 if (!cp_parser_simulate_error (parser))
26440 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
26441 return NULL;
26442 }
26443 }
26444
26445 /* An error message is produced if the next token is not '>'.
26446 All further tokens are skipped until the desired token is
26447 found or '{', '}', ';' or an unbalanced ')' or ']'. */
26448
26449 static void
26450 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
26451 {
26452 /* Current level of '< ... >'. */
26453 unsigned level = 0;
26454 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
26455 unsigned nesting_depth = 0;
26456
26457 /* Are we ready, yet? If not, issue error message. */
26458 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
26459 return;
26460
26461 /* Skip tokens until the desired token is found. */
26462 while (true)
26463 {
26464 /* Peek at the next token. */
26465 switch (cp_lexer_peek_token (parser->lexer)->type)
26466 {
26467 case CPP_LESS:
26468 if (!nesting_depth)
26469 ++level;
26470 break;
26471
26472 case CPP_RSHIFT:
26473 if (cxx_dialect == cxx98)
26474 /* C++0x views the `>>' operator as two `>' tokens, but
26475 C++98 does not. */
26476 break;
26477 else if (!nesting_depth && level-- == 0)
26478 {
26479 /* We've hit a `>>' where the first `>' closes the
26480 template argument list, and the second `>' is
26481 spurious. Just consume the `>>' and stop; we've
26482 already produced at least one error. */
26483 cp_lexer_consume_token (parser->lexer);
26484 return;
26485 }
26486 /* Fall through for C++0x, so we handle the second `>' in
26487 the `>>'. */
26488
26489 case CPP_GREATER:
26490 if (!nesting_depth && level-- == 0)
26491 {
26492 /* We've reached the token we want, consume it and stop. */
26493 cp_lexer_consume_token (parser->lexer);
26494 return;
26495 }
26496 break;
26497
26498 case CPP_OPEN_PAREN:
26499 case CPP_OPEN_SQUARE:
26500 ++nesting_depth;
26501 break;
26502
26503 case CPP_CLOSE_PAREN:
26504 case CPP_CLOSE_SQUARE:
26505 if (nesting_depth-- == 0)
26506 return;
26507 break;
26508
26509 case CPP_EOF:
26510 case CPP_PRAGMA_EOL:
26511 case CPP_SEMICOLON:
26512 case CPP_OPEN_BRACE:
26513 case CPP_CLOSE_BRACE:
26514 /* The '>' was probably forgotten, don't look further. */
26515 return;
26516
26517 default:
26518 break;
26519 }
26520
26521 /* Consume this token. */
26522 cp_lexer_consume_token (parser->lexer);
26523 }
26524 }
26525
26526 /* If the next token is the indicated keyword, consume it. Otherwise,
26527 issue an error message indicating that TOKEN_DESC was expected.
26528
26529 Returns the token consumed, if the token had the appropriate type.
26530 Otherwise, returns NULL. */
26531
26532 static cp_token *
26533 cp_parser_require_keyword (cp_parser* parser,
26534 enum rid keyword,
26535 required_token token_desc)
26536 {
26537 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
26538
26539 if (token && token->keyword != keyword)
26540 {
26541 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
26542 return NULL;
26543 }
26544
26545 return token;
26546 }
26547
26548 /* Returns TRUE iff TOKEN is a token that can begin the body of a
26549 function-definition. */
26550
26551 static bool
26552 cp_parser_token_starts_function_definition_p (cp_token* token)
26553 {
26554 return (/* An ordinary function-body begins with an `{'. */
26555 token->type == CPP_OPEN_BRACE
26556 /* A ctor-initializer begins with a `:'. */
26557 || token->type == CPP_COLON
26558 /* A function-try-block begins with `try'. */
26559 || token->keyword == RID_TRY
26560 /* A function-transaction-block begins with `__transaction_atomic'
26561 or `__transaction_relaxed'. */
26562 || token->keyword == RID_TRANSACTION_ATOMIC
26563 || token->keyword == RID_TRANSACTION_RELAXED
26564 /* The named return value extension begins with `return'. */
26565 || token->keyword == RID_RETURN);
26566 }
26567
26568 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
26569 definition. */
26570
26571 static bool
26572 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
26573 {
26574 cp_token *token;
26575
26576 token = cp_lexer_peek_token (parser->lexer);
26577 return (token->type == CPP_OPEN_BRACE
26578 || (token->type == CPP_COLON
26579 && !parser->colon_doesnt_start_class_def_p));
26580 }
26581
26582 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
26583 C++0x) ending a template-argument. */
26584
26585 static bool
26586 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
26587 {
26588 cp_token *token;
26589
26590 token = cp_lexer_peek_token (parser->lexer);
26591 return (token->type == CPP_COMMA
26592 || token->type == CPP_GREATER
26593 || token->type == CPP_ELLIPSIS
26594 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
26595 }
26596
26597 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
26598 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
26599
26600 static bool
26601 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
26602 size_t n)
26603 {
26604 cp_token *token;
26605
26606 token = cp_lexer_peek_nth_token (parser->lexer, n);
26607 if (token->type == CPP_LESS)
26608 return true;
26609 /* Check for the sequence `<::' in the original code. It would be lexed as
26610 `[:', where `[' is a digraph, and there is no whitespace before
26611 `:'. */
26612 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
26613 {
26614 cp_token *token2;
26615 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
26616 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
26617 return true;
26618 }
26619 return false;
26620 }
26621
26622 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
26623 or none_type otherwise. */
26624
26625 static enum tag_types
26626 cp_parser_token_is_class_key (cp_token* token)
26627 {
26628 switch (token->keyword)
26629 {
26630 case RID_CLASS:
26631 return class_type;
26632 case RID_STRUCT:
26633 return record_type;
26634 case RID_UNION:
26635 return union_type;
26636
26637 default:
26638 return none_type;
26639 }
26640 }
26641
26642 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
26643 or none_type otherwise or if the token is null. */
26644
26645 static enum tag_types
26646 cp_parser_token_is_type_parameter_key (cp_token* token)
26647 {
26648 if (!token)
26649 return none_type;
26650
26651 switch (token->keyword)
26652 {
26653 case RID_CLASS:
26654 return class_type;
26655 case RID_TYPENAME:
26656 return typename_type;
26657
26658 default:
26659 return none_type;
26660 }
26661 }
26662
26663 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
26664
26665 static void
26666 cp_parser_check_class_key (enum tag_types class_key, tree type)
26667 {
26668 if (type == error_mark_node)
26669 return;
26670 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
26671 {
26672 if (permerror (input_location, "%qs tag used in naming %q#T",
26673 class_key == union_type ? "union"
26674 : class_key == record_type ? "struct" : "class",
26675 type))
26676 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
26677 "%q#T was previously declared here", type);
26678 }
26679 }
26680
26681 /* Issue an error message if DECL is redeclared with different
26682 access than its original declaration [class.access.spec/3].
26683 This applies to nested classes and nested class templates.
26684 [class.mem/1]. */
26685
26686 static void
26687 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
26688 {
26689 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
26690 return;
26691
26692 if ((TREE_PRIVATE (decl)
26693 != (current_access_specifier == access_private_node))
26694 || (TREE_PROTECTED (decl)
26695 != (current_access_specifier == access_protected_node)))
26696 error_at (location, "%qD redeclared with different access", decl);
26697 }
26698
26699 /* Look for the `template' keyword, as a syntactic disambiguator.
26700 Return TRUE iff it is present, in which case it will be
26701 consumed. */
26702
26703 static bool
26704 cp_parser_optional_template_keyword (cp_parser *parser)
26705 {
26706 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26707 {
26708 /* In C++98 the `template' keyword can only be used within templates;
26709 outside templates the parser can always figure out what is a
26710 template and what is not. In C++11, per the resolution of DR 468,
26711 `template' is allowed in cases where it is not strictly necessary. */
26712 if (!processing_template_decl
26713 && pedantic && cxx_dialect == cxx98)
26714 {
26715 cp_token *token = cp_lexer_peek_token (parser->lexer);
26716 pedwarn (token->location, OPT_Wpedantic,
26717 "in C++98 %<template%> (as a disambiguator) is only "
26718 "allowed within templates");
26719 /* If this part of the token stream is rescanned, the same
26720 error message would be generated. So, we purge the token
26721 from the stream. */
26722 cp_lexer_purge_token (parser->lexer);
26723 return false;
26724 }
26725 else
26726 {
26727 /* Consume the `template' keyword. */
26728 cp_lexer_consume_token (parser->lexer);
26729 return true;
26730 }
26731 }
26732 return false;
26733 }
26734
26735 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
26736 set PARSER->SCOPE, and perform other related actions. */
26737
26738 static void
26739 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
26740 {
26741 int i;
26742 struct tree_check *check_value;
26743 deferred_access_check *chk;
26744 vec<deferred_access_check, va_gc> *checks;
26745
26746 /* Get the stored value. */
26747 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
26748 /* Perform any access checks that were deferred. */
26749 checks = check_value->checks;
26750 if (checks)
26751 {
26752 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
26753 perform_or_defer_access_check (chk->binfo,
26754 chk->decl,
26755 chk->diag_decl, tf_warning_or_error);
26756 }
26757 /* Set the scope from the stored value. */
26758 parser->scope = check_value->value;
26759 parser->qualifying_scope = check_value->qualifying_scope;
26760 parser->object_scope = NULL_TREE;
26761 }
26762
26763 /* Consume tokens up through a non-nested END token. Returns TRUE if we
26764 encounter the end of a block before what we were looking for. */
26765
26766 static bool
26767 cp_parser_cache_group (cp_parser *parser,
26768 enum cpp_ttype end,
26769 unsigned depth)
26770 {
26771 while (true)
26772 {
26773 cp_token *token = cp_lexer_peek_token (parser->lexer);
26774
26775 /* Abort a parenthesized expression if we encounter a semicolon. */
26776 if ((end == CPP_CLOSE_PAREN || depth == 0)
26777 && token->type == CPP_SEMICOLON)
26778 return true;
26779 /* If we've reached the end of the file, stop. */
26780 if (token->type == CPP_EOF
26781 || (end != CPP_PRAGMA_EOL
26782 && token->type == CPP_PRAGMA_EOL))
26783 return true;
26784 if (token->type == CPP_CLOSE_BRACE && depth == 0)
26785 /* We've hit the end of an enclosing block, so there's been some
26786 kind of syntax error. */
26787 return true;
26788
26789 /* Consume the token. */
26790 cp_lexer_consume_token (parser->lexer);
26791 /* See if it starts a new group. */
26792 if (token->type == CPP_OPEN_BRACE)
26793 {
26794 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
26795 /* In theory this should probably check end == '}', but
26796 cp_parser_save_member_function_body needs it to exit
26797 after either '}' or ')' when called with ')'. */
26798 if (depth == 0)
26799 return false;
26800 }
26801 else if (token->type == CPP_OPEN_PAREN)
26802 {
26803 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
26804 if (depth == 0 && end == CPP_CLOSE_PAREN)
26805 return false;
26806 }
26807 else if (token->type == CPP_PRAGMA)
26808 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
26809 else if (token->type == end)
26810 return false;
26811 }
26812 }
26813
26814 /* Like above, for caching a default argument or NSDMI. Both of these are
26815 terminated by a non-nested comma, but it can be unclear whether or not a
26816 comma is nested in a template argument list unless we do more parsing.
26817 In order to handle this ambiguity, when we encounter a ',' after a '<'
26818 we try to parse what follows as a parameter-declaration-list (in the
26819 case of a default argument) or a member-declarator (in the case of an
26820 NSDMI). If that succeeds, then we stop caching. */
26821
26822 static tree
26823 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
26824 {
26825 unsigned depth = 0;
26826 int maybe_template_id = 0;
26827 cp_token *first_token;
26828 cp_token *token;
26829 tree default_argument;
26830
26831 /* Add tokens until we have processed the entire default
26832 argument. We add the range [first_token, token). */
26833 first_token = cp_lexer_peek_token (parser->lexer);
26834 if (first_token->type == CPP_OPEN_BRACE)
26835 {
26836 /* For list-initialization, this is straightforward. */
26837 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26838 token = cp_lexer_peek_token (parser->lexer);
26839 }
26840 else while (true)
26841 {
26842 bool done = false;
26843
26844 /* Peek at the next token. */
26845 token = cp_lexer_peek_token (parser->lexer);
26846 /* What we do depends on what token we have. */
26847 switch (token->type)
26848 {
26849 /* In valid code, a default argument must be
26850 immediately followed by a `,' `)', or `...'. */
26851 case CPP_COMMA:
26852 if (depth == 0 && maybe_template_id)
26853 {
26854 /* If we've seen a '<', we might be in a
26855 template-argument-list. Until Core issue 325 is
26856 resolved, we don't know how this situation ought
26857 to be handled, so try to DTRT. We check whether
26858 what comes after the comma is a valid parameter
26859 declaration list. If it is, then the comma ends
26860 the default argument; otherwise the default
26861 argument continues. */
26862 bool error = false;
26863 cp_token *peek;
26864
26865 /* Set ITALP so cp_parser_parameter_declaration_list
26866 doesn't decide to commit to this parse. */
26867 bool saved_italp = parser->in_template_argument_list_p;
26868 parser->in_template_argument_list_p = true;
26869
26870 cp_parser_parse_tentatively (parser);
26871
26872 if (nsdmi)
26873 {
26874 /* Parse declarators until we reach a non-comma or
26875 somthing that cannot be an initializer.
26876 Just checking whether we're looking at a single
26877 declarator is insufficient. Consider:
26878 int var = tuple<T,U>::x;
26879 The template parameter 'U' looks exactly like a
26880 declarator. */
26881 do
26882 {
26883 int ctor_dtor_or_conv_p;
26884 cp_lexer_consume_token (parser->lexer);
26885 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26886 &ctor_dtor_or_conv_p,
26887 /*parenthesized_p=*/NULL,
26888 /*member_p=*/true,
26889 /*friend_p=*/false);
26890 peek = cp_lexer_peek_token (parser->lexer);
26891 if (cp_parser_error_occurred (parser))
26892 break;
26893 }
26894 while (peek->type == CPP_COMMA);
26895 /* If we met an '=' or ';' then the original comma
26896 was the end of the NSDMI. Otherwise assume
26897 we're still in the NSDMI. */
26898 error = (peek->type != CPP_EQ
26899 && peek->type != CPP_SEMICOLON);
26900 }
26901 else
26902 {
26903 cp_lexer_consume_token (parser->lexer);
26904 begin_scope (sk_function_parms, NULL_TREE);
26905 cp_parser_parameter_declaration_list (parser, &error);
26906 pop_bindings_and_leave_scope ();
26907 }
26908 if (!cp_parser_error_occurred (parser) && !error)
26909 done = true;
26910 cp_parser_abort_tentative_parse (parser);
26911
26912 parser->in_template_argument_list_p = saved_italp;
26913 break;
26914 }
26915 case CPP_CLOSE_PAREN:
26916 case CPP_ELLIPSIS:
26917 /* If we run into a non-nested `;', `}', or `]',
26918 then the code is invalid -- but the default
26919 argument is certainly over. */
26920 case CPP_SEMICOLON:
26921 case CPP_CLOSE_BRACE:
26922 case CPP_CLOSE_SQUARE:
26923 if (depth == 0
26924 /* Handle correctly int n = sizeof ... ( p ); */
26925 && token->type != CPP_ELLIPSIS)
26926 done = true;
26927 /* Update DEPTH, if necessary. */
26928 else if (token->type == CPP_CLOSE_PAREN
26929 || token->type == CPP_CLOSE_BRACE
26930 || token->type == CPP_CLOSE_SQUARE)
26931 --depth;
26932 break;
26933
26934 case CPP_OPEN_PAREN:
26935 case CPP_OPEN_SQUARE:
26936 case CPP_OPEN_BRACE:
26937 ++depth;
26938 break;
26939
26940 case CPP_LESS:
26941 if (depth == 0)
26942 /* This might be the comparison operator, or it might
26943 start a template argument list. */
26944 ++maybe_template_id;
26945 break;
26946
26947 case CPP_RSHIFT:
26948 if (cxx_dialect == cxx98)
26949 break;
26950 /* Fall through for C++0x, which treats the `>>'
26951 operator like two `>' tokens in certain
26952 cases. */
26953
26954 case CPP_GREATER:
26955 if (depth == 0)
26956 {
26957 /* This might be an operator, or it might close a
26958 template argument list. But if a previous '<'
26959 started a template argument list, this will have
26960 closed it, so we can't be in one anymore. */
26961 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
26962 if (maybe_template_id < 0)
26963 maybe_template_id = 0;
26964 }
26965 break;
26966
26967 /* If we run out of tokens, issue an error message. */
26968 case CPP_EOF:
26969 case CPP_PRAGMA_EOL:
26970 error_at (token->location, "file ends in default argument");
26971 done = true;
26972 break;
26973
26974 case CPP_NAME:
26975 case CPP_SCOPE:
26976 /* In these cases, we should look for template-ids.
26977 For example, if the default argument is
26978 `X<int, double>()', we need to do name lookup to
26979 figure out whether or not `X' is a template; if
26980 so, the `,' does not end the default argument.
26981
26982 That is not yet done. */
26983 break;
26984
26985 default:
26986 break;
26987 }
26988
26989 /* If we've reached the end, stop. */
26990 if (done)
26991 break;
26992
26993 /* Add the token to the token block. */
26994 token = cp_lexer_consume_token (parser->lexer);
26995 }
26996
26997 /* Create a DEFAULT_ARG to represent the unparsed default
26998 argument. */
26999 default_argument = make_node (DEFAULT_ARG);
27000 DEFARG_TOKENS (default_argument)
27001 = cp_token_cache_new (first_token, token);
27002 DEFARG_INSTANTIATIONS (default_argument) = NULL;
27003
27004 return default_argument;
27005 }
27006
27007 /* Begin parsing tentatively. We always save tokens while parsing
27008 tentatively so that if the tentative parsing fails we can restore the
27009 tokens. */
27010
27011 static void
27012 cp_parser_parse_tentatively (cp_parser* parser)
27013 {
27014 /* Enter a new parsing context. */
27015 parser->context = cp_parser_context_new (parser->context);
27016 /* Begin saving tokens. */
27017 cp_lexer_save_tokens (parser->lexer);
27018 /* In order to avoid repetitive access control error messages,
27019 access checks are queued up until we are no longer parsing
27020 tentatively. */
27021 push_deferring_access_checks (dk_deferred);
27022 }
27023
27024 /* Commit to the currently active tentative parse. */
27025
27026 static void
27027 cp_parser_commit_to_tentative_parse (cp_parser* parser)
27028 {
27029 cp_parser_context *context;
27030 cp_lexer *lexer;
27031
27032 /* Mark all of the levels as committed. */
27033 lexer = parser->lexer;
27034 for (context = parser->context; context->next; context = context->next)
27035 {
27036 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27037 break;
27038 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27039 while (!cp_lexer_saving_tokens (lexer))
27040 lexer = lexer->next;
27041 cp_lexer_commit_tokens (lexer);
27042 }
27043 }
27044
27045 /* Commit to the topmost currently active tentative parse.
27046
27047 Note that this function shouldn't be called when there are
27048 irreversible side-effects while in a tentative state. For
27049 example, we shouldn't create a permanent entry in the symbol
27050 table, or issue an error message that might not apply if the
27051 tentative parse is aborted. */
27052
27053 static void
27054 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
27055 {
27056 cp_parser_context *context = parser->context;
27057 cp_lexer *lexer = parser->lexer;
27058
27059 if (context)
27060 {
27061 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27062 return;
27063 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27064
27065 while (!cp_lexer_saving_tokens (lexer))
27066 lexer = lexer->next;
27067 cp_lexer_commit_tokens (lexer);
27068 }
27069 }
27070
27071 /* Abort the currently active tentative parse. All consumed tokens
27072 will be rolled back, and no diagnostics will be issued. */
27073
27074 static void
27075 cp_parser_abort_tentative_parse (cp_parser* parser)
27076 {
27077 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
27078 || errorcount > 0);
27079 cp_parser_simulate_error (parser);
27080 /* Now, pretend that we want to see if the construct was
27081 successfully parsed. */
27082 cp_parser_parse_definitely (parser);
27083 }
27084
27085 /* Stop parsing tentatively. If a parse error has occurred, restore the
27086 token stream. Otherwise, commit to the tokens we have consumed.
27087 Returns true if no error occurred; false otherwise. */
27088
27089 static bool
27090 cp_parser_parse_definitely (cp_parser* parser)
27091 {
27092 bool error_occurred;
27093 cp_parser_context *context;
27094
27095 /* Remember whether or not an error occurred, since we are about to
27096 destroy that information. */
27097 error_occurred = cp_parser_error_occurred (parser);
27098 /* Remove the topmost context from the stack. */
27099 context = parser->context;
27100 parser->context = context->next;
27101 /* If no parse errors occurred, commit to the tentative parse. */
27102 if (!error_occurred)
27103 {
27104 /* Commit to the tokens read tentatively, unless that was
27105 already done. */
27106 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
27107 cp_lexer_commit_tokens (parser->lexer);
27108
27109 pop_to_parent_deferring_access_checks ();
27110 }
27111 /* Otherwise, if errors occurred, roll back our state so that things
27112 are just as they were before we began the tentative parse. */
27113 else
27114 {
27115 cp_lexer_rollback_tokens (parser->lexer);
27116 pop_deferring_access_checks ();
27117 }
27118 /* Add the context to the front of the free list. */
27119 context->next = cp_parser_context_free_list;
27120 cp_parser_context_free_list = context;
27121
27122 return !error_occurred;
27123 }
27124
27125 /* Returns true if we are parsing tentatively and are not committed to
27126 this tentative parse. */
27127
27128 static bool
27129 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
27130 {
27131 return (cp_parser_parsing_tentatively (parser)
27132 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
27133 }
27134
27135 /* Returns nonzero iff an error has occurred during the most recent
27136 tentative parse. */
27137
27138 static bool
27139 cp_parser_error_occurred (cp_parser* parser)
27140 {
27141 return (cp_parser_parsing_tentatively (parser)
27142 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
27143 }
27144
27145 /* Returns nonzero if GNU extensions are allowed. */
27146
27147 static bool
27148 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
27149 {
27150 return parser->allow_gnu_extensions_p;
27151 }
27152 \f
27153 /* Objective-C++ Productions */
27154
27155
27156 /* Parse an Objective-C expression, which feeds into a primary-expression
27157 above.
27158
27159 objc-expression:
27160 objc-message-expression
27161 objc-string-literal
27162 objc-encode-expression
27163 objc-protocol-expression
27164 objc-selector-expression
27165
27166 Returns a tree representation of the expression. */
27167
27168 static tree
27169 cp_parser_objc_expression (cp_parser* parser)
27170 {
27171 /* Try to figure out what kind of declaration is present. */
27172 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27173
27174 switch (kwd->type)
27175 {
27176 case CPP_OPEN_SQUARE:
27177 return cp_parser_objc_message_expression (parser);
27178
27179 case CPP_OBJC_STRING:
27180 kwd = cp_lexer_consume_token (parser->lexer);
27181 return objc_build_string_object (kwd->u.value);
27182
27183 case CPP_KEYWORD:
27184 switch (kwd->keyword)
27185 {
27186 case RID_AT_ENCODE:
27187 return cp_parser_objc_encode_expression (parser);
27188
27189 case RID_AT_PROTOCOL:
27190 return cp_parser_objc_protocol_expression (parser);
27191
27192 case RID_AT_SELECTOR:
27193 return cp_parser_objc_selector_expression (parser);
27194
27195 default:
27196 break;
27197 }
27198 default:
27199 error_at (kwd->location,
27200 "misplaced %<@%D%> Objective-C++ construct",
27201 kwd->u.value);
27202 cp_parser_skip_to_end_of_block_or_statement (parser);
27203 }
27204
27205 return error_mark_node;
27206 }
27207
27208 /* Parse an Objective-C message expression.
27209
27210 objc-message-expression:
27211 [ objc-message-receiver objc-message-args ]
27212
27213 Returns a representation of an Objective-C message. */
27214
27215 static tree
27216 cp_parser_objc_message_expression (cp_parser* parser)
27217 {
27218 tree receiver, messageargs;
27219
27220 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
27221 receiver = cp_parser_objc_message_receiver (parser);
27222 messageargs = cp_parser_objc_message_args (parser);
27223 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27224
27225 return objc_build_message_expr (receiver, messageargs);
27226 }
27227
27228 /* Parse an objc-message-receiver.
27229
27230 objc-message-receiver:
27231 expression
27232 simple-type-specifier
27233
27234 Returns a representation of the type or expression. */
27235
27236 static tree
27237 cp_parser_objc_message_receiver (cp_parser* parser)
27238 {
27239 tree rcv;
27240
27241 /* An Objective-C message receiver may be either (1) a type
27242 or (2) an expression. */
27243 cp_parser_parse_tentatively (parser);
27244 rcv = cp_parser_expression (parser);
27245
27246 /* If that worked out, fine. */
27247 if (cp_parser_parse_definitely (parser))
27248 return rcv;
27249
27250 cp_parser_parse_tentatively (parser);
27251 rcv = cp_parser_simple_type_specifier (parser,
27252 /*decl_specs=*/NULL,
27253 CP_PARSER_FLAGS_NONE);
27254
27255 if (cp_parser_parse_definitely (parser))
27256 return objc_get_class_reference (rcv);
27257
27258 cp_parser_error (parser, "objective-c++ message receiver expected");
27259 return error_mark_node;
27260 }
27261
27262 /* Parse the arguments and selectors comprising an Objective-C message.
27263
27264 objc-message-args:
27265 objc-selector
27266 objc-selector-args
27267 objc-selector-args , objc-comma-args
27268
27269 objc-selector-args:
27270 objc-selector [opt] : assignment-expression
27271 objc-selector-args objc-selector [opt] : assignment-expression
27272
27273 objc-comma-args:
27274 assignment-expression
27275 objc-comma-args , assignment-expression
27276
27277 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
27278 selector arguments and TREE_VALUE containing a list of comma
27279 arguments. */
27280
27281 static tree
27282 cp_parser_objc_message_args (cp_parser* parser)
27283 {
27284 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
27285 bool maybe_unary_selector_p = true;
27286 cp_token *token = cp_lexer_peek_token (parser->lexer);
27287
27288 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27289 {
27290 tree selector = NULL_TREE, arg;
27291
27292 if (token->type != CPP_COLON)
27293 selector = cp_parser_objc_selector (parser);
27294
27295 /* Detect if we have a unary selector. */
27296 if (maybe_unary_selector_p
27297 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27298 return build_tree_list (selector, NULL_TREE);
27299
27300 maybe_unary_selector_p = false;
27301 cp_parser_require (parser, CPP_COLON, RT_COLON);
27302 arg = cp_parser_assignment_expression (parser);
27303
27304 sel_args
27305 = chainon (sel_args,
27306 build_tree_list (selector, arg));
27307
27308 token = cp_lexer_peek_token (parser->lexer);
27309 }
27310
27311 /* Handle non-selector arguments, if any. */
27312 while (token->type == CPP_COMMA)
27313 {
27314 tree arg;
27315
27316 cp_lexer_consume_token (parser->lexer);
27317 arg = cp_parser_assignment_expression (parser);
27318
27319 addl_args
27320 = chainon (addl_args,
27321 build_tree_list (NULL_TREE, arg));
27322
27323 token = cp_lexer_peek_token (parser->lexer);
27324 }
27325
27326 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
27327 {
27328 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
27329 return build_tree_list (error_mark_node, error_mark_node);
27330 }
27331
27332 return build_tree_list (sel_args, addl_args);
27333 }
27334
27335 /* Parse an Objective-C encode expression.
27336
27337 objc-encode-expression:
27338 @encode objc-typename
27339
27340 Returns an encoded representation of the type argument. */
27341
27342 static tree
27343 cp_parser_objc_encode_expression (cp_parser* parser)
27344 {
27345 tree type;
27346 cp_token *token;
27347
27348 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
27349 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27350 token = cp_lexer_peek_token (parser->lexer);
27351 type = complete_type (cp_parser_type_id (parser));
27352 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27353
27354 if (!type)
27355 {
27356 error_at (token->location,
27357 "%<@encode%> must specify a type as an argument");
27358 return error_mark_node;
27359 }
27360
27361 /* This happens if we find @encode(T) (where T is a template
27362 typename or something dependent on a template typename) when
27363 parsing a template. In that case, we can't compile it
27364 immediately, but we rather create an AT_ENCODE_EXPR which will
27365 need to be instantiated when the template is used.
27366 */
27367 if (dependent_type_p (type))
27368 {
27369 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
27370 TREE_READONLY (value) = 1;
27371 return value;
27372 }
27373
27374 return objc_build_encode_expr (type);
27375 }
27376
27377 /* Parse an Objective-C @defs expression. */
27378
27379 static tree
27380 cp_parser_objc_defs_expression (cp_parser *parser)
27381 {
27382 tree name;
27383
27384 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
27385 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27386 name = cp_parser_identifier (parser);
27387 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27388
27389 return objc_get_class_ivars (name);
27390 }
27391
27392 /* Parse an Objective-C protocol expression.
27393
27394 objc-protocol-expression:
27395 @protocol ( identifier )
27396
27397 Returns a representation of the protocol expression. */
27398
27399 static tree
27400 cp_parser_objc_protocol_expression (cp_parser* parser)
27401 {
27402 tree proto;
27403
27404 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
27405 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27406 proto = cp_parser_identifier (parser);
27407 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27408
27409 return objc_build_protocol_expr (proto);
27410 }
27411
27412 /* Parse an Objective-C selector expression.
27413
27414 objc-selector-expression:
27415 @selector ( objc-method-signature )
27416
27417 objc-method-signature:
27418 objc-selector
27419 objc-selector-seq
27420
27421 objc-selector-seq:
27422 objc-selector :
27423 objc-selector-seq objc-selector :
27424
27425 Returns a representation of the method selector. */
27426
27427 static tree
27428 cp_parser_objc_selector_expression (cp_parser* parser)
27429 {
27430 tree sel_seq = NULL_TREE;
27431 bool maybe_unary_selector_p = true;
27432 cp_token *token;
27433 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27434
27435 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
27436 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27437 token = cp_lexer_peek_token (parser->lexer);
27438
27439 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
27440 || token->type == CPP_SCOPE)
27441 {
27442 tree selector = NULL_TREE;
27443
27444 if (token->type != CPP_COLON
27445 || token->type == CPP_SCOPE)
27446 selector = cp_parser_objc_selector (parser);
27447
27448 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
27449 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
27450 {
27451 /* Detect if we have a unary selector. */
27452 if (maybe_unary_selector_p)
27453 {
27454 sel_seq = selector;
27455 goto finish_selector;
27456 }
27457 else
27458 {
27459 cp_parser_error (parser, "expected %<:%>");
27460 }
27461 }
27462 maybe_unary_selector_p = false;
27463 token = cp_lexer_consume_token (parser->lexer);
27464
27465 if (token->type == CPP_SCOPE)
27466 {
27467 sel_seq
27468 = chainon (sel_seq,
27469 build_tree_list (selector, NULL_TREE));
27470 sel_seq
27471 = chainon (sel_seq,
27472 build_tree_list (NULL_TREE, NULL_TREE));
27473 }
27474 else
27475 sel_seq
27476 = chainon (sel_seq,
27477 build_tree_list (selector, NULL_TREE));
27478
27479 token = cp_lexer_peek_token (parser->lexer);
27480 }
27481
27482 finish_selector:
27483 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27484
27485 return objc_build_selector_expr (loc, sel_seq);
27486 }
27487
27488 /* Parse a list of identifiers.
27489
27490 objc-identifier-list:
27491 identifier
27492 objc-identifier-list , identifier
27493
27494 Returns a TREE_LIST of identifier nodes. */
27495
27496 static tree
27497 cp_parser_objc_identifier_list (cp_parser* parser)
27498 {
27499 tree identifier;
27500 tree list;
27501 cp_token *sep;
27502
27503 identifier = cp_parser_identifier (parser);
27504 if (identifier == error_mark_node)
27505 return error_mark_node;
27506
27507 list = build_tree_list (NULL_TREE, identifier);
27508 sep = cp_lexer_peek_token (parser->lexer);
27509
27510 while (sep->type == CPP_COMMA)
27511 {
27512 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27513 identifier = cp_parser_identifier (parser);
27514 if (identifier == error_mark_node)
27515 return list;
27516
27517 list = chainon (list, build_tree_list (NULL_TREE,
27518 identifier));
27519 sep = cp_lexer_peek_token (parser->lexer);
27520 }
27521
27522 return list;
27523 }
27524
27525 /* Parse an Objective-C alias declaration.
27526
27527 objc-alias-declaration:
27528 @compatibility_alias identifier identifier ;
27529
27530 This function registers the alias mapping with the Objective-C front end.
27531 It returns nothing. */
27532
27533 static void
27534 cp_parser_objc_alias_declaration (cp_parser* parser)
27535 {
27536 tree alias, orig;
27537
27538 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
27539 alias = cp_parser_identifier (parser);
27540 orig = cp_parser_identifier (parser);
27541 objc_declare_alias (alias, orig);
27542 cp_parser_consume_semicolon_at_end_of_statement (parser);
27543 }
27544
27545 /* Parse an Objective-C class forward-declaration.
27546
27547 objc-class-declaration:
27548 @class objc-identifier-list ;
27549
27550 The function registers the forward declarations with the Objective-C
27551 front end. It returns nothing. */
27552
27553 static void
27554 cp_parser_objc_class_declaration (cp_parser* parser)
27555 {
27556 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
27557 while (true)
27558 {
27559 tree id;
27560
27561 id = cp_parser_identifier (parser);
27562 if (id == error_mark_node)
27563 break;
27564
27565 objc_declare_class (id);
27566
27567 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27568 cp_lexer_consume_token (parser->lexer);
27569 else
27570 break;
27571 }
27572 cp_parser_consume_semicolon_at_end_of_statement (parser);
27573 }
27574
27575 /* Parse a list of Objective-C protocol references.
27576
27577 objc-protocol-refs-opt:
27578 objc-protocol-refs [opt]
27579
27580 objc-protocol-refs:
27581 < objc-identifier-list >
27582
27583 Returns a TREE_LIST of identifiers, if any. */
27584
27585 static tree
27586 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
27587 {
27588 tree protorefs = NULL_TREE;
27589
27590 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
27591 {
27592 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
27593 protorefs = cp_parser_objc_identifier_list (parser);
27594 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
27595 }
27596
27597 return protorefs;
27598 }
27599
27600 /* Parse a Objective-C visibility specification. */
27601
27602 static void
27603 cp_parser_objc_visibility_spec (cp_parser* parser)
27604 {
27605 cp_token *vis = cp_lexer_peek_token (parser->lexer);
27606
27607 switch (vis->keyword)
27608 {
27609 case RID_AT_PRIVATE:
27610 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
27611 break;
27612 case RID_AT_PROTECTED:
27613 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
27614 break;
27615 case RID_AT_PUBLIC:
27616 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
27617 break;
27618 case RID_AT_PACKAGE:
27619 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
27620 break;
27621 default:
27622 return;
27623 }
27624
27625 /* Eat '@private'/'@protected'/'@public'. */
27626 cp_lexer_consume_token (parser->lexer);
27627 }
27628
27629 /* Parse an Objective-C method type. Return 'true' if it is a class
27630 (+) method, and 'false' if it is an instance (-) method. */
27631
27632 static inline bool
27633 cp_parser_objc_method_type (cp_parser* parser)
27634 {
27635 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
27636 return true;
27637 else
27638 return false;
27639 }
27640
27641 /* Parse an Objective-C protocol qualifier. */
27642
27643 static tree
27644 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
27645 {
27646 tree quals = NULL_TREE, node;
27647 cp_token *token = cp_lexer_peek_token (parser->lexer);
27648
27649 node = token->u.value;
27650
27651 while (node && identifier_p (node)
27652 && (node == ridpointers [(int) RID_IN]
27653 || node == ridpointers [(int) RID_OUT]
27654 || node == ridpointers [(int) RID_INOUT]
27655 || node == ridpointers [(int) RID_BYCOPY]
27656 || node == ridpointers [(int) RID_BYREF]
27657 || node == ridpointers [(int) RID_ONEWAY]))
27658 {
27659 quals = tree_cons (NULL_TREE, node, quals);
27660 cp_lexer_consume_token (parser->lexer);
27661 token = cp_lexer_peek_token (parser->lexer);
27662 node = token->u.value;
27663 }
27664
27665 return quals;
27666 }
27667
27668 /* Parse an Objective-C typename. */
27669
27670 static tree
27671 cp_parser_objc_typename (cp_parser* parser)
27672 {
27673 tree type_name = NULL_TREE;
27674
27675 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27676 {
27677 tree proto_quals, cp_type = NULL_TREE;
27678
27679 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
27680 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
27681
27682 /* An ObjC type name may consist of just protocol qualifiers, in which
27683 case the type shall default to 'id'. */
27684 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27685 {
27686 cp_type = cp_parser_type_id (parser);
27687
27688 /* If the type could not be parsed, an error has already
27689 been produced. For error recovery, behave as if it had
27690 not been specified, which will use the default type
27691 'id'. */
27692 if (cp_type == error_mark_node)
27693 {
27694 cp_type = NULL_TREE;
27695 /* We need to skip to the closing parenthesis as
27696 cp_parser_type_id() does not seem to do it for
27697 us. */
27698 cp_parser_skip_to_closing_parenthesis (parser,
27699 /*recovering=*/true,
27700 /*or_comma=*/false,
27701 /*consume_paren=*/false);
27702 }
27703 }
27704
27705 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27706 type_name = build_tree_list (proto_quals, cp_type);
27707 }
27708
27709 return type_name;
27710 }
27711
27712 /* Check to see if TYPE refers to an Objective-C selector name. */
27713
27714 static bool
27715 cp_parser_objc_selector_p (enum cpp_ttype type)
27716 {
27717 return (type == CPP_NAME || type == CPP_KEYWORD
27718 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
27719 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
27720 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
27721 || type == CPP_XOR || type == CPP_XOR_EQ);
27722 }
27723
27724 /* Parse an Objective-C selector. */
27725
27726 static tree
27727 cp_parser_objc_selector (cp_parser* parser)
27728 {
27729 cp_token *token = cp_lexer_consume_token (parser->lexer);
27730
27731 if (!cp_parser_objc_selector_p (token->type))
27732 {
27733 error_at (token->location, "invalid Objective-C++ selector name");
27734 return error_mark_node;
27735 }
27736
27737 /* C++ operator names are allowed to appear in ObjC selectors. */
27738 switch (token->type)
27739 {
27740 case CPP_AND_AND: return get_identifier ("and");
27741 case CPP_AND_EQ: return get_identifier ("and_eq");
27742 case CPP_AND: return get_identifier ("bitand");
27743 case CPP_OR: return get_identifier ("bitor");
27744 case CPP_COMPL: return get_identifier ("compl");
27745 case CPP_NOT: return get_identifier ("not");
27746 case CPP_NOT_EQ: return get_identifier ("not_eq");
27747 case CPP_OR_OR: return get_identifier ("or");
27748 case CPP_OR_EQ: return get_identifier ("or_eq");
27749 case CPP_XOR: return get_identifier ("xor");
27750 case CPP_XOR_EQ: return get_identifier ("xor_eq");
27751 default: return token->u.value;
27752 }
27753 }
27754
27755 /* Parse an Objective-C params list. */
27756
27757 static tree
27758 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
27759 {
27760 tree params = NULL_TREE;
27761 bool maybe_unary_selector_p = true;
27762 cp_token *token = cp_lexer_peek_token (parser->lexer);
27763
27764 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27765 {
27766 tree selector = NULL_TREE, type_name, identifier;
27767 tree parm_attr = NULL_TREE;
27768
27769 if (token->keyword == RID_ATTRIBUTE)
27770 break;
27771
27772 if (token->type != CPP_COLON)
27773 selector = cp_parser_objc_selector (parser);
27774
27775 /* Detect if we have a unary selector. */
27776 if (maybe_unary_selector_p
27777 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27778 {
27779 params = selector; /* Might be followed by attributes. */
27780 break;
27781 }
27782
27783 maybe_unary_selector_p = false;
27784 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27785 {
27786 /* Something went quite wrong. There should be a colon
27787 here, but there is not. Stop parsing parameters. */
27788 break;
27789 }
27790 type_name = cp_parser_objc_typename (parser);
27791 /* New ObjC allows attributes on parameters too. */
27792 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27793 parm_attr = cp_parser_attributes_opt (parser);
27794 identifier = cp_parser_identifier (parser);
27795
27796 params
27797 = chainon (params,
27798 objc_build_keyword_decl (selector,
27799 type_name,
27800 identifier,
27801 parm_attr));
27802
27803 token = cp_lexer_peek_token (parser->lexer);
27804 }
27805
27806 if (params == NULL_TREE)
27807 {
27808 cp_parser_error (parser, "objective-c++ method declaration is expected");
27809 return error_mark_node;
27810 }
27811
27812 /* We allow tail attributes for the method. */
27813 if (token->keyword == RID_ATTRIBUTE)
27814 {
27815 *attributes = cp_parser_attributes_opt (parser);
27816 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27817 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27818 return params;
27819 cp_parser_error (parser,
27820 "method attributes must be specified at the end");
27821 return error_mark_node;
27822 }
27823
27824 if (params == NULL_TREE)
27825 {
27826 cp_parser_error (parser, "objective-c++ method declaration is expected");
27827 return error_mark_node;
27828 }
27829 return params;
27830 }
27831
27832 /* Parse the non-keyword Objective-C params. */
27833
27834 static tree
27835 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
27836 tree* attributes)
27837 {
27838 tree params = make_node (TREE_LIST);
27839 cp_token *token = cp_lexer_peek_token (parser->lexer);
27840 *ellipsisp = false; /* Initially, assume no ellipsis. */
27841
27842 while (token->type == CPP_COMMA)
27843 {
27844 cp_parameter_declarator *parmdecl;
27845 tree parm;
27846
27847 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27848 token = cp_lexer_peek_token (parser->lexer);
27849
27850 if (token->type == CPP_ELLIPSIS)
27851 {
27852 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
27853 *ellipsisp = true;
27854 token = cp_lexer_peek_token (parser->lexer);
27855 break;
27856 }
27857
27858 /* TODO: parse attributes for tail parameters. */
27859 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
27860 parm = grokdeclarator (parmdecl->declarator,
27861 &parmdecl->decl_specifiers,
27862 PARM, /*initialized=*/0,
27863 /*attrlist=*/NULL);
27864
27865 chainon (params, build_tree_list (NULL_TREE, parm));
27866 token = cp_lexer_peek_token (parser->lexer);
27867 }
27868
27869 /* We allow tail attributes for the method. */
27870 if (token->keyword == RID_ATTRIBUTE)
27871 {
27872 if (*attributes == NULL_TREE)
27873 {
27874 *attributes = cp_parser_attributes_opt (parser);
27875 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27876 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27877 return params;
27878 }
27879 else
27880 /* We have an error, but parse the attributes, so that we can
27881 carry on. */
27882 *attributes = cp_parser_attributes_opt (parser);
27883
27884 cp_parser_error (parser,
27885 "method attributes must be specified at the end");
27886 return error_mark_node;
27887 }
27888
27889 return params;
27890 }
27891
27892 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
27893
27894 static void
27895 cp_parser_objc_interstitial_code (cp_parser* parser)
27896 {
27897 cp_token *token = cp_lexer_peek_token (parser->lexer);
27898
27899 /* If the next token is `extern' and the following token is a string
27900 literal, then we have a linkage specification. */
27901 if (token->keyword == RID_EXTERN
27902 && cp_parser_is_pure_string_literal
27903 (cp_lexer_peek_nth_token (parser->lexer, 2)))
27904 cp_parser_linkage_specification (parser);
27905 /* Handle #pragma, if any. */
27906 else if (token->type == CPP_PRAGMA)
27907 cp_parser_pragma (parser, pragma_objc_icode);
27908 /* Allow stray semicolons. */
27909 else if (token->type == CPP_SEMICOLON)
27910 cp_lexer_consume_token (parser->lexer);
27911 /* Mark methods as optional or required, when building protocols. */
27912 else if (token->keyword == RID_AT_OPTIONAL)
27913 {
27914 cp_lexer_consume_token (parser->lexer);
27915 objc_set_method_opt (true);
27916 }
27917 else if (token->keyword == RID_AT_REQUIRED)
27918 {
27919 cp_lexer_consume_token (parser->lexer);
27920 objc_set_method_opt (false);
27921 }
27922 else if (token->keyword == RID_NAMESPACE)
27923 cp_parser_namespace_definition (parser);
27924 /* Other stray characters must generate errors. */
27925 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
27926 {
27927 cp_lexer_consume_token (parser->lexer);
27928 error ("stray %qs between Objective-C++ methods",
27929 token->type == CPP_OPEN_BRACE ? "{" : "}");
27930 }
27931 /* Finally, try to parse a block-declaration, or a function-definition. */
27932 else
27933 cp_parser_block_declaration (parser, /*statement_p=*/false);
27934 }
27935
27936 /* Parse a method signature. */
27937
27938 static tree
27939 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
27940 {
27941 tree rettype, kwdparms, optparms;
27942 bool ellipsis = false;
27943 bool is_class_method;
27944
27945 is_class_method = cp_parser_objc_method_type (parser);
27946 rettype = cp_parser_objc_typename (parser);
27947 *attributes = NULL_TREE;
27948 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
27949 if (kwdparms == error_mark_node)
27950 return error_mark_node;
27951 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
27952 if (optparms == error_mark_node)
27953 return error_mark_node;
27954
27955 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
27956 }
27957
27958 static bool
27959 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
27960 {
27961 tree tattr;
27962 cp_lexer_save_tokens (parser->lexer);
27963 tattr = cp_parser_attributes_opt (parser);
27964 gcc_assert (tattr) ;
27965
27966 /* If the attributes are followed by a method introducer, this is not allowed.
27967 Dump the attributes and flag the situation. */
27968 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
27969 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
27970 return true;
27971
27972 /* Otherwise, the attributes introduce some interstitial code, possibly so
27973 rewind to allow that check. */
27974 cp_lexer_rollback_tokens (parser->lexer);
27975 return false;
27976 }
27977
27978 /* Parse an Objective-C method prototype list. */
27979
27980 static void
27981 cp_parser_objc_method_prototype_list (cp_parser* parser)
27982 {
27983 cp_token *token = cp_lexer_peek_token (parser->lexer);
27984
27985 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
27986 {
27987 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
27988 {
27989 tree attributes, sig;
27990 bool is_class_method;
27991 if (token->type == CPP_PLUS)
27992 is_class_method = true;
27993 else
27994 is_class_method = false;
27995 sig = cp_parser_objc_method_signature (parser, &attributes);
27996 if (sig == error_mark_node)
27997 {
27998 cp_parser_skip_to_end_of_block_or_statement (parser);
27999 token = cp_lexer_peek_token (parser->lexer);
28000 continue;
28001 }
28002 objc_add_method_declaration (is_class_method, sig, attributes);
28003 cp_parser_consume_semicolon_at_end_of_statement (parser);
28004 }
28005 else if (token->keyword == RID_AT_PROPERTY)
28006 cp_parser_objc_at_property_declaration (parser);
28007 else if (token->keyword == RID_ATTRIBUTE
28008 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28009 warning_at (cp_lexer_peek_token (parser->lexer)->location,
28010 OPT_Wattributes,
28011 "prefix attributes are ignored for methods");
28012 else
28013 /* Allow for interspersed non-ObjC++ code. */
28014 cp_parser_objc_interstitial_code (parser);
28015
28016 token = cp_lexer_peek_token (parser->lexer);
28017 }
28018
28019 if (token->type != CPP_EOF)
28020 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28021 else
28022 cp_parser_error (parser, "expected %<@end%>");
28023
28024 objc_finish_interface ();
28025 }
28026
28027 /* Parse an Objective-C method definition list. */
28028
28029 static void
28030 cp_parser_objc_method_definition_list (cp_parser* parser)
28031 {
28032 cp_token *token = cp_lexer_peek_token (parser->lexer);
28033
28034 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28035 {
28036 tree meth;
28037
28038 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28039 {
28040 cp_token *ptk;
28041 tree sig, attribute;
28042 bool is_class_method;
28043 if (token->type == CPP_PLUS)
28044 is_class_method = true;
28045 else
28046 is_class_method = false;
28047 push_deferring_access_checks (dk_deferred);
28048 sig = cp_parser_objc_method_signature (parser, &attribute);
28049 if (sig == error_mark_node)
28050 {
28051 cp_parser_skip_to_end_of_block_or_statement (parser);
28052 token = cp_lexer_peek_token (parser->lexer);
28053 continue;
28054 }
28055 objc_start_method_definition (is_class_method, sig, attribute,
28056 NULL_TREE);
28057
28058 /* For historical reasons, we accept an optional semicolon. */
28059 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28060 cp_lexer_consume_token (parser->lexer);
28061
28062 ptk = cp_lexer_peek_token (parser->lexer);
28063 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
28064 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
28065 {
28066 perform_deferred_access_checks (tf_warning_or_error);
28067 stop_deferring_access_checks ();
28068 meth = cp_parser_function_definition_after_declarator (parser,
28069 false);
28070 pop_deferring_access_checks ();
28071 objc_finish_method_definition (meth);
28072 }
28073 }
28074 /* The following case will be removed once @synthesize is
28075 completely implemented. */
28076 else if (token->keyword == RID_AT_PROPERTY)
28077 cp_parser_objc_at_property_declaration (parser);
28078 else if (token->keyword == RID_AT_SYNTHESIZE)
28079 cp_parser_objc_at_synthesize_declaration (parser);
28080 else if (token->keyword == RID_AT_DYNAMIC)
28081 cp_parser_objc_at_dynamic_declaration (parser);
28082 else if (token->keyword == RID_ATTRIBUTE
28083 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28084 warning_at (token->location, OPT_Wattributes,
28085 "prefix attributes are ignored for methods");
28086 else
28087 /* Allow for interspersed non-ObjC++ code. */
28088 cp_parser_objc_interstitial_code (parser);
28089
28090 token = cp_lexer_peek_token (parser->lexer);
28091 }
28092
28093 if (token->type != CPP_EOF)
28094 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28095 else
28096 cp_parser_error (parser, "expected %<@end%>");
28097
28098 objc_finish_implementation ();
28099 }
28100
28101 /* Parse Objective-C ivars. */
28102
28103 static void
28104 cp_parser_objc_class_ivars (cp_parser* parser)
28105 {
28106 cp_token *token = cp_lexer_peek_token (parser->lexer);
28107
28108 if (token->type != CPP_OPEN_BRACE)
28109 return; /* No ivars specified. */
28110
28111 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
28112 token = cp_lexer_peek_token (parser->lexer);
28113
28114 while (token->type != CPP_CLOSE_BRACE
28115 && token->keyword != RID_AT_END && token->type != CPP_EOF)
28116 {
28117 cp_decl_specifier_seq declspecs;
28118 int decl_class_or_enum_p;
28119 tree prefix_attributes;
28120
28121 cp_parser_objc_visibility_spec (parser);
28122
28123 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28124 break;
28125
28126 cp_parser_decl_specifier_seq (parser,
28127 CP_PARSER_FLAGS_OPTIONAL,
28128 &declspecs,
28129 &decl_class_or_enum_p);
28130
28131 /* auto, register, static, extern, mutable. */
28132 if (declspecs.storage_class != sc_none)
28133 {
28134 cp_parser_error (parser, "invalid type for instance variable");
28135 declspecs.storage_class = sc_none;
28136 }
28137
28138 /* thread_local. */
28139 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28140 {
28141 cp_parser_error (parser, "invalid type for instance variable");
28142 declspecs.locations[ds_thread] = 0;
28143 }
28144
28145 /* typedef. */
28146 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28147 {
28148 cp_parser_error (parser, "invalid type for instance variable");
28149 declspecs.locations[ds_typedef] = 0;
28150 }
28151
28152 prefix_attributes = declspecs.attributes;
28153 declspecs.attributes = NULL_TREE;
28154
28155 /* Keep going until we hit the `;' at the end of the
28156 declaration. */
28157 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28158 {
28159 tree width = NULL_TREE, attributes, first_attribute, decl;
28160 cp_declarator *declarator = NULL;
28161 int ctor_dtor_or_conv_p;
28162
28163 /* Check for a (possibly unnamed) bitfield declaration. */
28164 token = cp_lexer_peek_token (parser->lexer);
28165 if (token->type == CPP_COLON)
28166 goto eat_colon;
28167
28168 if (token->type == CPP_NAME
28169 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
28170 == CPP_COLON))
28171 {
28172 /* Get the name of the bitfield. */
28173 declarator = make_id_declarator (NULL_TREE,
28174 cp_parser_identifier (parser),
28175 sfk_none);
28176
28177 eat_colon:
28178 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28179 /* Get the width of the bitfield. */
28180 width
28181 = cp_parser_constant_expression (parser);
28182 }
28183 else
28184 {
28185 /* Parse the declarator. */
28186 declarator
28187 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28188 &ctor_dtor_or_conv_p,
28189 /*parenthesized_p=*/NULL,
28190 /*member_p=*/false,
28191 /*friend_p=*/false);
28192 }
28193
28194 /* Look for attributes that apply to the ivar. */
28195 attributes = cp_parser_attributes_opt (parser);
28196 /* Remember which attributes are prefix attributes and
28197 which are not. */
28198 first_attribute = attributes;
28199 /* Combine the attributes. */
28200 attributes = chainon (prefix_attributes, attributes);
28201
28202 if (width)
28203 /* Create the bitfield declaration. */
28204 decl = grokbitfield (declarator, &declspecs,
28205 width,
28206 attributes);
28207 else
28208 decl = grokfield (declarator, &declspecs,
28209 NULL_TREE, /*init_const_expr_p=*/false,
28210 NULL_TREE, attributes);
28211
28212 /* Add the instance variable. */
28213 if (decl != error_mark_node && decl != NULL_TREE)
28214 objc_add_instance_variable (decl);
28215
28216 /* Reset PREFIX_ATTRIBUTES. */
28217 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28218 attributes = TREE_CHAIN (attributes);
28219 if (attributes)
28220 TREE_CHAIN (attributes) = NULL_TREE;
28221
28222 token = cp_lexer_peek_token (parser->lexer);
28223
28224 if (token->type == CPP_COMMA)
28225 {
28226 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28227 continue;
28228 }
28229 break;
28230 }
28231
28232 cp_parser_consume_semicolon_at_end_of_statement (parser);
28233 token = cp_lexer_peek_token (parser->lexer);
28234 }
28235
28236 if (token->keyword == RID_AT_END)
28237 cp_parser_error (parser, "expected %<}%>");
28238
28239 /* Do not consume the RID_AT_END, so it will be read again as terminating
28240 the @interface of @implementation. */
28241 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
28242 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
28243
28244 /* For historical reasons, we accept an optional semicolon. */
28245 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28246 cp_lexer_consume_token (parser->lexer);
28247 }
28248
28249 /* Parse an Objective-C protocol declaration. */
28250
28251 static void
28252 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
28253 {
28254 tree proto, protorefs;
28255 cp_token *tok;
28256
28257 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28258 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
28259 {
28260 tok = cp_lexer_peek_token (parser->lexer);
28261 error_at (tok->location, "identifier expected after %<@protocol%>");
28262 cp_parser_consume_semicolon_at_end_of_statement (parser);
28263 return;
28264 }
28265
28266 /* See if we have a forward declaration or a definition. */
28267 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
28268
28269 /* Try a forward declaration first. */
28270 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
28271 {
28272 while (true)
28273 {
28274 tree id;
28275
28276 id = cp_parser_identifier (parser);
28277 if (id == error_mark_node)
28278 break;
28279
28280 objc_declare_protocol (id, attributes);
28281
28282 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28283 cp_lexer_consume_token (parser->lexer);
28284 else
28285 break;
28286 }
28287 cp_parser_consume_semicolon_at_end_of_statement (parser);
28288 }
28289
28290 /* Ok, we got a full-fledged definition (or at least should). */
28291 else
28292 {
28293 proto = cp_parser_identifier (parser);
28294 protorefs = cp_parser_objc_protocol_refs_opt (parser);
28295 objc_start_protocol (proto, protorefs, attributes);
28296 cp_parser_objc_method_prototype_list (parser);
28297 }
28298 }
28299
28300 /* Parse an Objective-C superclass or category. */
28301
28302 static void
28303 cp_parser_objc_superclass_or_category (cp_parser *parser,
28304 bool iface_p,
28305 tree *super,
28306 tree *categ, bool *is_class_extension)
28307 {
28308 cp_token *next = cp_lexer_peek_token (parser->lexer);
28309
28310 *super = *categ = NULL_TREE;
28311 *is_class_extension = false;
28312 if (next->type == CPP_COLON)
28313 {
28314 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28315 *super = cp_parser_identifier (parser);
28316 }
28317 else if (next->type == CPP_OPEN_PAREN)
28318 {
28319 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28320
28321 /* If there is no category name, and this is an @interface, we
28322 have a class extension. */
28323 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28324 {
28325 *categ = NULL_TREE;
28326 *is_class_extension = true;
28327 }
28328 else
28329 *categ = cp_parser_identifier (parser);
28330
28331 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28332 }
28333 }
28334
28335 /* Parse an Objective-C class interface. */
28336
28337 static void
28338 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
28339 {
28340 tree name, super, categ, protos;
28341 bool is_class_extension;
28342
28343 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
28344 name = cp_parser_identifier (parser);
28345 if (name == error_mark_node)
28346 {
28347 /* It's hard to recover because even if valid @interface stuff
28348 is to follow, we can't compile it (or validate it) if we
28349 don't even know which class it refers to. Let's assume this
28350 was a stray '@interface' token in the stream and skip it.
28351 */
28352 return;
28353 }
28354 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
28355 &is_class_extension);
28356 protos = cp_parser_objc_protocol_refs_opt (parser);
28357
28358 /* We have either a class or a category on our hands. */
28359 if (categ || is_class_extension)
28360 objc_start_category_interface (name, categ, protos, attributes);
28361 else
28362 {
28363 objc_start_class_interface (name, super, protos, attributes);
28364 /* Handle instance variable declarations, if any. */
28365 cp_parser_objc_class_ivars (parser);
28366 objc_continue_interface ();
28367 }
28368
28369 cp_parser_objc_method_prototype_list (parser);
28370 }
28371
28372 /* Parse an Objective-C class implementation. */
28373
28374 static void
28375 cp_parser_objc_class_implementation (cp_parser* parser)
28376 {
28377 tree name, super, categ;
28378 bool is_class_extension;
28379
28380 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
28381 name = cp_parser_identifier (parser);
28382 if (name == error_mark_node)
28383 {
28384 /* It's hard to recover because even if valid @implementation
28385 stuff is to follow, we can't compile it (or validate it) if
28386 we don't even know which class it refers to. Let's assume
28387 this was a stray '@implementation' token in the stream and
28388 skip it.
28389 */
28390 return;
28391 }
28392 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
28393 &is_class_extension);
28394
28395 /* We have either a class or a category on our hands. */
28396 if (categ)
28397 objc_start_category_implementation (name, categ);
28398 else
28399 {
28400 objc_start_class_implementation (name, super);
28401 /* Handle instance variable declarations, if any. */
28402 cp_parser_objc_class_ivars (parser);
28403 objc_continue_implementation ();
28404 }
28405
28406 cp_parser_objc_method_definition_list (parser);
28407 }
28408
28409 /* Consume the @end token and finish off the implementation. */
28410
28411 static void
28412 cp_parser_objc_end_implementation (cp_parser* parser)
28413 {
28414 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28415 objc_finish_implementation ();
28416 }
28417
28418 /* Parse an Objective-C declaration. */
28419
28420 static void
28421 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
28422 {
28423 /* Try to figure out what kind of declaration is present. */
28424 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28425
28426 if (attributes)
28427 switch (kwd->keyword)
28428 {
28429 case RID_AT_ALIAS:
28430 case RID_AT_CLASS:
28431 case RID_AT_END:
28432 error_at (kwd->location, "attributes may not be specified before"
28433 " the %<@%D%> Objective-C++ keyword",
28434 kwd->u.value);
28435 attributes = NULL;
28436 break;
28437 case RID_AT_IMPLEMENTATION:
28438 warning_at (kwd->location, OPT_Wattributes,
28439 "prefix attributes are ignored before %<@%D%>",
28440 kwd->u.value);
28441 attributes = NULL;
28442 default:
28443 break;
28444 }
28445
28446 switch (kwd->keyword)
28447 {
28448 case RID_AT_ALIAS:
28449 cp_parser_objc_alias_declaration (parser);
28450 break;
28451 case RID_AT_CLASS:
28452 cp_parser_objc_class_declaration (parser);
28453 break;
28454 case RID_AT_PROTOCOL:
28455 cp_parser_objc_protocol_declaration (parser, attributes);
28456 break;
28457 case RID_AT_INTERFACE:
28458 cp_parser_objc_class_interface (parser, attributes);
28459 break;
28460 case RID_AT_IMPLEMENTATION:
28461 cp_parser_objc_class_implementation (parser);
28462 break;
28463 case RID_AT_END:
28464 cp_parser_objc_end_implementation (parser);
28465 break;
28466 default:
28467 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
28468 kwd->u.value);
28469 cp_parser_skip_to_end_of_block_or_statement (parser);
28470 }
28471 }
28472
28473 /* Parse an Objective-C try-catch-finally statement.
28474
28475 objc-try-catch-finally-stmt:
28476 @try compound-statement objc-catch-clause-seq [opt]
28477 objc-finally-clause [opt]
28478
28479 objc-catch-clause-seq:
28480 objc-catch-clause objc-catch-clause-seq [opt]
28481
28482 objc-catch-clause:
28483 @catch ( objc-exception-declaration ) compound-statement
28484
28485 objc-finally-clause:
28486 @finally compound-statement
28487
28488 objc-exception-declaration:
28489 parameter-declaration
28490 '...'
28491
28492 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
28493
28494 Returns NULL_TREE.
28495
28496 PS: This function is identical to c_parser_objc_try_catch_finally_statement
28497 for C. Keep them in sync. */
28498
28499 static tree
28500 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
28501 {
28502 location_t location;
28503 tree stmt;
28504
28505 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
28506 location = cp_lexer_peek_token (parser->lexer)->location;
28507 objc_maybe_warn_exceptions (location);
28508 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
28509 node, lest it get absorbed into the surrounding block. */
28510 stmt = push_stmt_list ();
28511 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28512 objc_begin_try_stmt (location, pop_stmt_list (stmt));
28513
28514 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
28515 {
28516 cp_parameter_declarator *parm;
28517 tree parameter_declaration = error_mark_node;
28518 bool seen_open_paren = false;
28519
28520 cp_lexer_consume_token (parser->lexer);
28521 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28522 seen_open_paren = true;
28523 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28524 {
28525 /* We have "@catch (...)" (where the '...' are literally
28526 what is in the code). Skip the '...'.
28527 parameter_declaration is set to NULL_TREE, and
28528 objc_being_catch_clauses() knows that that means
28529 '...'. */
28530 cp_lexer_consume_token (parser->lexer);
28531 parameter_declaration = NULL_TREE;
28532 }
28533 else
28534 {
28535 /* We have "@catch (NSException *exception)" or something
28536 like that. Parse the parameter declaration. */
28537 parm = cp_parser_parameter_declaration (parser, false, NULL);
28538 if (parm == NULL)
28539 parameter_declaration = error_mark_node;
28540 else
28541 parameter_declaration = grokdeclarator (parm->declarator,
28542 &parm->decl_specifiers,
28543 PARM, /*initialized=*/0,
28544 /*attrlist=*/NULL);
28545 }
28546 if (seen_open_paren)
28547 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28548 else
28549 {
28550 /* If there was no open parenthesis, we are recovering from
28551 an error, and we are trying to figure out what mistake
28552 the user has made. */
28553
28554 /* If there is an immediate closing parenthesis, the user
28555 probably forgot the opening one (ie, they typed "@catch
28556 NSException *e)". Parse the closing parenthesis and keep
28557 going. */
28558 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28559 cp_lexer_consume_token (parser->lexer);
28560
28561 /* If these is no immediate closing parenthesis, the user
28562 probably doesn't know that parenthesis are required at
28563 all (ie, they typed "@catch NSException *e"). So, just
28564 forget about the closing parenthesis and keep going. */
28565 }
28566 objc_begin_catch_clause (parameter_declaration);
28567 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28568 objc_finish_catch_clause ();
28569 }
28570 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
28571 {
28572 cp_lexer_consume_token (parser->lexer);
28573 location = cp_lexer_peek_token (parser->lexer)->location;
28574 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
28575 node, lest it get absorbed into the surrounding block. */
28576 stmt = push_stmt_list ();
28577 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28578 objc_build_finally_clause (location, pop_stmt_list (stmt));
28579 }
28580
28581 return objc_finish_try_stmt ();
28582 }
28583
28584 /* Parse an Objective-C synchronized statement.
28585
28586 objc-synchronized-stmt:
28587 @synchronized ( expression ) compound-statement
28588
28589 Returns NULL_TREE. */
28590
28591 static tree
28592 cp_parser_objc_synchronized_statement (cp_parser *parser)
28593 {
28594 location_t location;
28595 tree lock, stmt;
28596
28597 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
28598
28599 location = cp_lexer_peek_token (parser->lexer)->location;
28600 objc_maybe_warn_exceptions (location);
28601 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28602 lock = cp_parser_expression (parser);
28603 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28604
28605 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
28606 node, lest it get absorbed into the surrounding block. */
28607 stmt = push_stmt_list ();
28608 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28609
28610 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
28611 }
28612
28613 /* Parse an Objective-C throw statement.
28614
28615 objc-throw-stmt:
28616 @throw assignment-expression [opt] ;
28617
28618 Returns a constructed '@throw' statement. */
28619
28620 static tree
28621 cp_parser_objc_throw_statement (cp_parser *parser)
28622 {
28623 tree expr = NULL_TREE;
28624 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28625
28626 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
28627
28628 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28629 expr = cp_parser_expression (parser);
28630
28631 cp_parser_consume_semicolon_at_end_of_statement (parser);
28632
28633 return objc_build_throw_stmt (loc, expr);
28634 }
28635
28636 /* Parse an Objective-C statement. */
28637
28638 static tree
28639 cp_parser_objc_statement (cp_parser * parser)
28640 {
28641 /* Try to figure out what kind of declaration is present. */
28642 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28643
28644 switch (kwd->keyword)
28645 {
28646 case RID_AT_TRY:
28647 return cp_parser_objc_try_catch_finally_statement (parser);
28648 case RID_AT_SYNCHRONIZED:
28649 return cp_parser_objc_synchronized_statement (parser);
28650 case RID_AT_THROW:
28651 return cp_parser_objc_throw_statement (parser);
28652 default:
28653 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
28654 kwd->u.value);
28655 cp_parser_skip_to_end_of_block_or_statement (parser);
28656 }
28657
28658 return error_mark_node;
28659 }
28660
28661 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
28662 look ahead to see if an objc keyword follows the attributes. This
28663 is to detect the use of prefix attributes on ObjC @interface and
28664 @protocol. */
28665
28666 static bool
28667 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
28668 {
28669 cp_lexer_save_tokens (parser->lexer);
28670 *attrib = cp_parser_attributes_opt (parser);
28671 gcc_assert (*attrib);
28672 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
28673 {
28674 cp_lexer_commit_tokens (parser->lexer);
28675 return true;
28676 }
28677 cp_lexer_rollback_tokens (parser->lexer);
28678 return false;
28679 }
28680
28681 /* This routine is a minimal replacement for
28682 c_parser_struct_declaration () used when parsing the list of
28683 types/names or ObjC++ properties. For example, when parsing the
28684 code
28685
28686 @property (readonly) int a, b, c;
28687
28688 this function is responsible for parsing "int a, int b, int c" and
28689 returning the declarations as CHAIN of DECLs.
28690
28691 TODO: Share this code with cp_parser_objc_class_ivars. It's very
28692 similar parsing. */
28693 static tree
28694 cp_parser_objc_struct_declaration (cp_parser *parser)
28695 {
28696 tree decls = NULL_TREE;
28697 cp_decl_specifier_seq declspecs;
28698 int decl_class_or_enum_p;
28699 tree prefix_attributes;
28700
28701 cp_parser_decl_specifier_seq (parser,
28702 CP_PARSER_FLAGS_NONE,
28703 &declspecs,
28704 &decl_class_or_enum_p);
28705
28706 if (declspecs.type == error_mark_node)
28707 return error_mark_node;
28708
28709 /* auto, register, static, extern, mutable. */
28710 if (declspecs.storage_class != sc_none)
28711 {
28712 cp_parser_error (parser, "invalid type for property");
28713 declspecs.storage_class = sc_none;
28714 }
28715
28716 /* thread_local. */
28717 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28718 {
28719 cp_parser_error (parser, "invalid type for property");
28720 declspecs.locations[ds_thread] = 0;
28721 }
28722
28723 /* typedef. */
28724 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28725 {
28726 cp_parser_error (parser, "invalid type for property");
28727 declspecs.locations[ds_typedef] = 0;
28728 }
28729
28730 prefix_attributes = declspecs.attributes;
28731 declspecs.attributes = NULL_TREE;
28732
28733 /* Keep going until we hit the `;' at the end of the declaration. */
28734 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28735 {
28736 tree attributes, first_attribute, decl;
28737 cp_declarator *declarator;
28738 cp_token *token;
28739
28740 /* Parse the declarator. */
28741 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28742 NULL, NULL, false, false);
28743
28744 /* Look for attributes that apply to the ivar. */
28745 attributes = cp_parser_attributes_opt (parser);
28746 /* Remember which attributes are prefix attributes and
28747 which are not. */
28748 first_attribute = attributes;
28749 /* Combine the attributes. */
28750 attributes = chainon (prefix_attributes, attributes);
28751
28752 decl = grokfield (declarator, &declspecs,
28753 NULL_TREE, /*init_const_expr_p=*/false,
28754 NULL_TREE, attributes);
28755
28756 if (decl == error_mark_node || decl == NULL_TREE)
28757 return error_mark_node;
28758
28759 /* Reset PREFIX_ATTRIBUTES. */
28760 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28761 attributes = TREE_CHAIN (attributes);
28762 if (attributes)
28763 TREE_CHAIN (attributes) = NULL_TREE;
28764
28765 DECL_CHAIN (decl) = decls;
28766 decls = decl;
28767
28768 token = cp_lexer_peek_token (parser->lexer);
28769 if (token->type == CPP_COMMA)
28770 {
28771 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28772 continue;
28773 }
28774 else
28775 break;
28776 }
28777 return decls;
28778 }
28779
28780 /* Parse an Objective-C @property declaration. The syntax is:
28781
28782 objc-property-declaration:
28783 '@property' objc-property-attributes[opt] struct-declaration ;
28784
28785 objc-property-attributes:
28786 '(' objc-property-attribute-list ')'
28787
28788 objc-property-attribute-list:
28789 objc-property-attribute
28790 objc-property-attribute-list, objc-property-attribute
28791
28792 objc-property-attribute
28793 'getter' = identifier
28794 'setter' = identifier
28795 'readonly'
28796 'readwrite'
28797 'assign'
28798 'retain'
28799 'copy'
28800 'nonatomic'
28801
28802 For example:
28803 @property NSString *name;
28804 @property (readonly) id object;
28805 @property (retain, nonatomic, getter=getTheName) id name;
28806 @property int a, b, c;
28807
28808 PS: This function is identical to
28809 c_parser_objc_at_property_declaration for C. Keep them in sync. */
28810 static void
28811 cp_parser_objc_at_property_declaration (cp_parser *parser)
28812 {
28813 /* The following variables hold the attributes of the properties as
28814 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
28815 seen. When we see an attribute, we set them to 'true' (if they
28816 are boolean properties) or to the identifier (if they have an
28817 argument, ie, for getter and setter). Note that here we only
28818 parse the list of attributes, check the syntax and accumulate the
28819 attributes that we find. objc_add_property_declaration() will
28820 then process the information. */
28821 bool property_assign = false;
28822 bool property_copy = false;
28823 tree property_getter_ident = NULL_TREE;
28824 bool property_nonatomic = false;
28825 bool property_readonly = false;
28826 bool property_readwrite = false;
28827 bool property_retain = false;
28828 tree property_setter_ident = NULL_TREE;
28829
28830 /* 'properties' is the list of properties that we read. Usually a
28831 single one, but maybe more (eg, in "@property int a, b, c;" there
28832 are three). */
28833 tree properties;
28834 location_t loc;
28835
28836 loc = cp_lexer_peek_token (parser->lexer)->location;
28837
28838 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
28839
28840 /* Parse the optional attribute list... */
28841 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28842 {
28843 /* Eat the '('. */
28844 cp_lexer_consume_token (parser->lexer);
28845
28846 while (true)
28847 {
28848 bool syntax_error = false;
28849 cp_token *token = cp_lexer_peek_token (parser->lexer);
28850 enum rid keyword;
28851
28852 if (token->type != CPP_NAME)
28853 {
28854 cp_parser_error (parser, "expected identifier");
28855 break;
28856 }
28857 keyword = C_RID_CODE (token->u.value);
28858 cp_lexer_consume_token (parser->lexer);
28859 switch (keyword)
28860 {
28861 case RID_ASSIGN: property_assign = true; break;
28862 case RID_COPY: property_copy = true; break;
28863 case RID_NONATOMIC: property_nonatomic = true; break;
28864 case RID_READONLY: property_readonly = true; break;
28865 case RID_READWRITE: property_readwrite = true; break;
28866 case RID_RETAIN: property_retain = true; break;
28867
28868 case RID_GETTER:
28869 case RID_SETTER:
28870 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
28871 {
28872 if (keyword == RID_GETTER)
28873 cp_parser_error (parser,
28874 "missing %<=%> (after %<getter%> attribute)");
28875 else
28876 cp_parser_error (parser,
28877 "missing %<=%> (after %<setter%> attribute)");
28878 syntax_error = true;
28879 break;
28880 }
28881 cp_lexer_consume_token (parser->lexer); /* eat the = */
28882 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
28883 {
28884 cp_parser_error (parser, "expected identifier");
28885 syntax_error = true;
28886 break;
28887 }
28888 if (keyword == RID_SETTER)
28889 {
28890 if (property_setter_ident != NULL_TREE)
28891 {
28892 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
28893 cp_lexer_consume_token (parser->lexer);
28894 }
28895 else
28896 property_setter_ident = cp_parser_objc_selector (parser);
28897 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28898 cp_parser_error (parser, "setter name must terminate with %<:%>");
28899 else
28900 cp_lexer_consume_token (parser->lexer);
28901 }
28902 else
28903 {
28904 if (property_getter_ident != NULL_TREE)
28905 {
28906 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
28907 cp_lexer_consume_token (parser->lexer);
28908 }
28909 else
28910 property_getter_ident = cp_parser_objc_selector (parser);
28911 }
28912 break;
28913 default:
28914 cp_parser_error (parser, "unknown property attribute");
28915 syntax_error = true;
28916 break;
28917 }
28918
28919 if (syntax_error)
28920 break;
28921
28922 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28923 cp_lexer_consume_token (parser->lexer);
28924 else
28925 break;
28926 }
28927
28928 /* FIXME: "@property (setter, assign);" will generate a spurious
28929 "error: expected ‘)’ before ‘,’ token". This is because
28930 cp_parser_require, unlike the C counterpart, will produce an
28931 error even if we are in error recovery. */
28932 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28933 {
28934 cp_parser_skip_to_closing_parenthesis (parser,
28935 /*recovering=*/true,
28936 /*or_comma=*/false,
28937 /*consume_paren=*/true);
28938 }
28939 }
28940
28941 /* ... and the property declaration(s). */
28942 properties = cp_parser_objc_struct_declaration (parser);
28943
28944 if (properties == error_mark_node)
28945 {
28946 cp_parser_skip_to_end_of_statement (parser);
28947 /* If the next token is now a `;', consume it. */
28948 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28949 cp_lexer_consume_token (parser->lexer);
28950 return;
28951 }
28952
28953 if (properties == NULL_TREE)
28954 cp_parser_error (parser, "expected identifier");
28955 else
28956 {
28957 /* Comma-separated properties are chained together in
28958 reverse order; add them one by one. */
28959 properties = nreverse (properties);
28960
28961 for (; properties; properties = TREE_CHAIN (properties))
28962 objc_add_property_declaration (loc, copy_node (properties),
28963 property_readonly, property_readwrite,
28964 property_assign, property_retain,
28965 property_copy, property_nonatomic,
28966 property_getter_ident, property_setter_ident);
28967 }
28968
28969 cp_parser_consume_semicolon_at_end_of_statement (parser);
28970 }
28971
28972 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
28973
28974 objc-synthesize-declaration:
28975 @synthesize objc-synthesize-identifier-list ;
28976
28977 objc-synthesize-identifier-list:
28978 objc-synthesize-identifier
28979 objc-synthesize-identifier-list, objc-synthesize-identifier
28980
28981 objc-synthesize-identifier
28982 identifier
28983 identifier = identifier
28984
28985 For example:
28986 @synthesize MyProperty;
28987 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
28988
28989 PS: This function is identical to c_parser_objc_at_synthesize_declaration
28990 for C. Keep them in sync.
28991 */
28992 static void
28993 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
28994 {
28995 tree list = NULL_TREE;
28996 location_t loc;
28997 loc = cp_lexer_peek_token (parser->lexer)->location;
28998
28999 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
29000 while (true)
29001 {
29002 tree property, ivar;
29003 property = cp_parser_identifier (parser);
29004 if (property == error_mark_node)
29005 {
29006 cp_parser_consume_semicolon_at_end_of_statement (parser);
29007 return;
29008 }
29009 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
29010 {
29011 cp_lexer_consume_token (parser->lexer);
29012 ivar = cp_parser_identifier (parser);
29013 if (ivar == error_mark_node)
29014 {
29015 cp_parser_consume_semicolon_at_end_of_statement (parser);
29016 return;
29017 }
29018 }
29019 else
29020 ivar = NULL_TREE;
29021 list = chainon (list, build_tree_list (ivar, property));
29022 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29023 cp_lexer_consume_token (parser->lexer);
29024 else
29025 break;
29026 }
29027 cp_parser_consume_semicolon_at_end_of_statement (parser);
29028 objc_add_synthesize_declaration (loc, list);
29029 }
29030
29031 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
29032
29033 objc-dynamic-declaration:
29034 @dynamic identifier-list ;
29035
29036 For example:
29037 @dynamic MyProperty;
29038 @dynamic MyProperty, AnotherProperty;
29039
29040 PS: This function is identical to c_parser_objc_at_dynamic_declaration
29041 for C. Keep them in sync.
29042 */
29043 static void
29044 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
29045 {
29046 tree list = NULL_TREE;
29047 location_t loc;
29048 loc = cp_lexer_peek_token (parser->lexer)->location;
29049
29050 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
29051 while (true)
29052 {
29053 tree property;
29054 property = cp_parser_identifier (parser);
29055 if (property == error_mark_node)
29056 {
29057 cp_parser_consume_semicolon_at_end_of_statement (parser);
29058 return;
29059 }
29060 list = chainon (list, build_tree_list (NULL, property));
29061 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29062 cp_lexer_consume_token (parser->lexer);
29063 else
29064 break;
29065 }
29066 cp_parser_consume_semicolon_at_end_of_statement (parser);
29067 objc_add_dynamic_declaration (loc, list);
29068 }
29069
29070 \f
29071 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
29072
29073 /* Returns name of the next clause.
29074 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
29075 the token is not consumed. Otherwise appropriate pragma_omp_clause is
29076 returned and the token is consumed. */
29077
29078 static pragma_omp_clause
29079 cp_parser_omp_clause_name (cp_parser *parser)
29080 {
29081 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
29082
29083 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29084 result = PRAGMA_OACC_CLAUSE_AUTO;
29085 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
29086 result = PRAGMA_OMP_CLAUSE_IF;
29087 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
29088 result = PRAGMA_OMP_CLAUSE_DEFAULT;
29089 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
29090 result = PRAGMA_OACC_CLAUSE_DELETE;
29091 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
29092 result = PRAGMA_OMP_CLAUSE_PRIVATE;
29093 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29094 result = PRAGMA_OMP_CLAUSE_FOR;
29095 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29096 {
29097 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29098 const char *p = IDENTIFIER_POINTER (id);
29099
29100 switch (p[0])
29101 {
29102 case 'a':
29103 if (!strcmp ("aligned", p))
29104 result = PRAGMA_OMP_CLAUSE_ALIGNED;
29105 else if (!strcmp ("async", p))
29106 result = PRAGMA_OACC_CLAUSE_ASYNC;
29107 break;
29108 case 'c':
29109 if (!strcmp ("collapse", p))
29110 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
29111 else if (!strcmp ("copy", p))
29112 result = PRAGMA_OACC_CLAUSE_COPY;
29113 else if (!strcmp ("copyin", p))
29114 result = PRAGMA_OMP_CLAUSE_COPYIN;
29115 else if (!strcmp ("copyout", p))
29116 result = PRAGMA_OACC_CLAUSE_COPYOUT;
29117 else if (!strcmp ("copyprivate", p))
29118 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
29119 else if (!strcmp ("create", p))
29120 result = PRAGMA_OACC_CLAUSE_CREATE;
29121 break;
29122 case 'd':
29123 if (!strcmp ("defaultmap", p))
29124 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
29125 else if (!strcmp ("depend", p))
29126 result = PRAGMA_OMP_CLAUSE_DEPEND;
29127 else if (!strcmp ("device", p))
29128 result = PRAGMA_OMP_CLAUSE_DEVICE;
29129 else if (!strcmp ("deviceptr", p))
29130 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
29131 else if (!strcmp ("dist_schedule", p))
29132 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
29133 break;
29134 case 'f':
29135 if (!strcmp ("final", p))
29136 result = PRAGMA_OMP_CLAUSE_FINAL;
29137 else if (!strcmp ("firstprivate", p))
29138 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
29139 else if (!strcmp ("from", p))
29140 result = PRAGMA_OMP_CLAUSE_FROM;
29141 break;
29142 case 'g':
29143 if (!strcmp ("gang", p))
29144 result = PRAGMA_OACC_CLAUSE_GANG;
29145 else if (!strcmp ("grainsize", p))
29146 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
29147 break;
29148 case 'h':
29149 if (!strcmp ("hint", p))
29150 result = PRAGMA_OMP_CLAUSE_HINT;
29151 else if (!strcmp ("host", p))
29152 result = PRAGMA_OACC_CLAUSE_HOST;
29153 break;
29154 case 'i':
29155 if (!strcmp ("inbranch", p))
29156 result = PRAGMA_OMP_CLAUSE_INBRANCH;
29157 else if (!strcmp ("independent", p))
29158 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
29159 else if (!strcmp ("is_device_ptr", p))
29160 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
29161 break;
29162 case 'l':
29163 if (!strcmp ("lastprivate", p))
29164 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
29165 else if (!strcmp ("linear", p))
29166 result = PRAGMA_OMP_CLAUSE_LINEAR;
29167 else if (!strcmp ("link", p))
29168 result = PRAGMA_OMP_CLAUSE_LINK;
29169 break;
29170 case 'm':
29171 if (!strcmp ("map", p))
29172 result = PRAGMA_OMP_CLAUSE_MAP;
29173 else if (!strcmp ("mergeable", p))
29174 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
29175 else if (flag_cilkplus && !strcmp ("mask", p))
29176 result = PRAGMA_CILK_CLAUSE_MASK;
29177 break;
29178 case 'n':
29179 if (!strcmp ("nogroup", p))
29180 result = PRAGMA_OMP_CLAUSE_NOGROUP;
29181 else if (!strcmp ("notinbranch", p))
29182 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
29183 else if (!strcmp ("nowait", p))
29184 result = PRAGMA_OMP_CLAUSE_NOWAIT;
29185 else if (flag_cilkplus && !strcmp ("nomask", p))
29186 result = PRAGMA_CILK_CLAUSE_NOMASK;
29187 else if (!strcmp ("num_gangs", p))
29188 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
29189 else if (!strcmp ("num_tasks", p))
29190 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
29191 else if (!strcmp ("num_teams", p))
29192 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
29193 else if (!strcmp ("num_threads", p))
29194 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
29195 else if (!strcmp ("num_workers", p))
29196 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
29197 break;
29198 case 'o':
29199 if (!strcmp ("ordered", p))
29200 result = PRAGMA_OMP_CLAUSE_ORDERED;
29201 break;
29202 case 'p':
29203 if (!strcmp ("parallel", p))
29204 result = PRAGMA_OMP_CLAUSE_PARALLEL;
29205 else if (!strcmp ("present", p))
29206 result = PRAGMA_OACC_CLAUSE_PRESENT;
29207 else if (!strcmp ("present_or_copy", p)
29208 || !strcmp ("pcopy", p))
29209 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
29210 else if (!strcmp ("present_or_copyin", p)
29211 || !strcmp ("pcopyin", p))
29212 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
29213 else if (!strcmp ("present_or_copyout", p)
29214 || !strcmp ("pcopyout", p))
29215 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
29216 else if (!strcmp ("present_or_create", p)
29217 || !strcmp ("pcreate", p))
29218 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
29219 else if (!strcmp ("priority", p))
29220 result = PRAGMA_OMP_CLAUSE_PRIORITY;
29221 else if (!strcmp ("proc_bind", p))
29222 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
29223 break;
29224 case 'r':
29225 if (!strcmp ("reduction", p))
29226 result = PRAGMA_OMP_CLAUSE_REDUCTION;
29227 break;
29228 case 's':
29229 if (!strcmp ("safelen", p))
29230 result = PRAGMA_OMP_CLAUSE_SAFELEN;
29231 else if (!strcmp ("schedule", p))
29232 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
29233 else if (!strcmp ("sections", p))
29234 result = PRAGMA_OMP_CLAUSE_SECTIONS;
29235 else if (!strcmp ("self", p))
29236 result = PRAGMA_OACC_CLAUSE_SELF;
29237 else if (!strcmp ("seq", p))
29238 result = PRAGMA_OACC_CLAUSE_SEQ;
29239 else if (!strcmp ("shared", p))
29240 result = PRAGMA_OMP_CLAUSE_SHARED;
29241 else if (!strcmp ("simd", p))
29242 result = PRAGMA_OMP_CLAUSE_SIMD;
29243 else if (!strcmp ("simdlen", p))
29244 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
29245 break;
29246 case 't':
29247 if (!strcmp ("taskgroup", p))
29248 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
29249 else if (!strcmp ("thread_limit", p))
29250 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
29251 else if (!strcmp ("threads", p))
29252 result = PRAGMA_OMP_CLAUSE_THREADS;
29253 else if (!strcmp ("tile", p))
29254 result = PRAGMA_OACC_CLAUSE_TILE;
29255 else if (!strcmp ("to", p))
29256 result = PRAGMA_OMP_CLAUSE_TO;
29257 break;
29258 case 'u':
29259 if (!strcmp ("uniform", p))
29260 result = PRAGMA_OMP_CLAUSE_UNIFORM;
29261 else if (!strcmp ("untied", p))
29262 result = PRAGMA_OMP_CLAUSE_UNTIED;
29263 else if (!strcmp ("use_device_ptr", p))
29264 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
29265 break;
29266 case 'v':
29267 if (!strcmp ("vector", p))
29268 result = PRAGMA_OACC_CLAUSE_VECTOR;
29269 else if (!strcmp ("vector_length", p))
29270 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
29271 else if (flag_cilkplus && !strcmp ("vectorlength", p))
29272 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
29273 break;
29274 case 'w':
29275 if (!strcmp ("wait", p))
29276 result = PRAGMA_OACC_CLAUSE_WAIT;
29277 else if (!strcmp ("worker", p))
29278 result = PRAGMA_OACC_CLAUSE_WORKER;
29279 break;
29280 }
29281 }
29282
29283 if (result != PRAGMA_OMP_CLAUSE_NONE)
29284 cp_lexer_consume_token (parser->lexer);
29285
29286 return result;
29287 }
29288
29289 /* Validate that a clause of the given type does not already exist. */
29290
29291 static void
29292 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
29293 const char *name, location_t location)
29294 {
29295 tree c;
29296
29297 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29298 if (OMP_CLAUSE_CODE (c) == code)
29299 {
29300 error_at (location, "too many %qs clauses", name);
29301 break;
29302 }
29303 }
29304
29305 /* OpenMP 2.5:
29306 variable-list:
29307 identifier
29308 variable-list , identifier
29309
29310 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
29311 colon). An opening parenthesis will have been consumed by the caller.
29312
29313 If KIND is nonzero, create the appropriate node and install the decl
29314 in OMP_CLAUSE_DECL and add the node to the head of the list.
29315
29316 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
29317 return the list created.
29318
29319 COLON can be NULL if only closing parenthesis should end the list,
29320 or pointer to bool which will receive false if the list is terminated
29321 by closing parenthesis or true if the list is terminated by colon. */
29322
29323 static tree
29324 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
29325 tree list, bool *colon)
29326 {
29327 cp_token *token;
29328 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
29329 if (colon)
29330 {
29331 parser->colon_corrects_to_scope_p = false;
29332 *colon = false;
29333 }
29334 while (1)
29335 {
29336 tree name, decl;
29337
29338 token = cp_lexer_peek_token (parser->lexer);
29339 if (kind != 0
29340 && current_class_ptr
29341 && cp_parser_is_keyword (token, RID_THIS))
29342 {
29343 decl = finish_this_expr ();
29344 if (TREE_CODE (decl) == NON_LVALUE_EXPR
29345 || CONVERT_EXPR_P (decl))
29346 decl = TREE_OPERAND (decl, 0);
29347 cp_lexer_consume_token (parser->lexer);
29348 }
29349 else
29350 {
29351 name = cp_parser_id_expression (parser, /*template_p=*/false,
29352 /*check_dependency_p=*/true,
29353 /*template_p=*/NULL,
29354 /*declarator_p=*/false,
29355 /*optional_p=*/false);
29356 if (name == error_mark_node)
29357 goto skip_comma;
29358
29359 decl = cp_parser_lookup_name_simple (parser, name, token->location);
29360 if (decl == error_mark_node)
29361 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
29362 token->location);
29363 }
29364 if (decl == error_mark_node)
29365 ;
29366 else if (kind != 0)
29367 {
29368 switch (kind)
29369 {
29370 case OMP_CLAUSE__CACHE_:
29371 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
29372 {
29373 error_at (token->location, "expected %<[%>");
29374 decl = error_mark_node;
29375 break;
29376 }
29377 /* FALLTHROUGH. */
29378 case OMP_CLAUSE_MAP:
29379 case OMP_CLAUSE_FROM:
29380 case OMP_CLAUSE_TO:
29381 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
29382 {
29383 location_t loc
29384 = cp_lexer_peek_token (parser->lexer)->location;
29385 cp_id_kind idk = CP_ID_KIND_NONE;
29386 cp_lexer_consume_token (parser->lexer);
29387 decl
29388 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
29389 decl, false,
29390 &idk, loc);
29391 }
29392 /* FALLTHROUGH. */
29393 case OMP_CLAUSE_DEPEND:
29394 case OMP_CLAUSE_REDUCTION:
29395 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
29396 {
29397 tree low_bound = NULL_TREE, length = NULL_TREE;
29398
29399 parser->colon_corrects_to_scope_p = false;
29400 cp_lexer_consume_token (parser->lexer);
29401 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29402 low_bound = cp_parser_expression (parser);
29403 if (!colon)
29404 parser->colon_corrects_to_scope_p
29405 = saved_colon_corrects_to_scope_p;
29406 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
29407 length = integer_one_node;
29408 else
29409 {
29410 /* Look for `:'. */
29411 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29412 goto skip_comma;
29413 if (!cp_lexer_next_token_is (parser->lexer,
29414 CPP_CLOSE_SQUARE))
29415 length = cp_parser_expression (parser);
29416 }
29417 /* Look for the closing `]'. */
29418 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
29419 RT_CLOSE_SQUARE))
29420 goto skip_comma;
29421
29422 if (kind == OMP_CLAUSE__CACHE_)
29423 {
29424 if (TREE_CODE (low_bound) != INTEGER_CST
29425 && !TREE_READONLY (low_bound))
29426 {
29427 error_at (token->location,
29428 "%qD is not a constant", low_bound);
29429 decl = error_mark_node;
29430 }
29431
29432 if (TREE_CODE (length) != INTEGER_CST
29433 && !TREE_READONLY (length))
29434 {
29435 error_at (token->location,
29436 "%qD is not a constant", length);
29437 decl = error_mark_node;
29438 }
29439 }
29440
29441 decl = tree_cons (low_bound, length, decl);
29442 }
29443 break;
29444 default:
29445 break;
29446 }
29447
29448 tree u = build_omp_clause (token->location, kind);
29449 OMP_CLAUSE_DECL (u) = decl;
29450 OMP_CLAUSE_CHAIN (u) = list;
29451 list = u;
29452 }
29453 else
29454 list = tree_cons (decl, NULL_TREE, list);
29455
29456 get_comma:
29457 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29458 break;
29459 cp_lexer_consume_token (parser->lexer);
29460 }
29461
29462 if (colon)
29463 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
29464
29465 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29466 {
29467 *colon = true;
29468 cp_parser_require (parser, CPP_COLON, RT_COLON);
29469 return list;
29470 }
29471
29472 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29473 {
29474 int ending;
29475
29476 /* Try to resync to an unnested comma. Copied from
29477 cp_parser_parenthesized_expression_list. */
29478 skip_comma:
29479 if (colon)
29480 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
29481 ending = cp_parser_skip_to_closing_parenthesis (parser,
29482 /*recovering=*/true,
29483 /*or_comma=*/true,
29484 /*consume_paren=*/true);
29485 if (ending < 0)
29486 goto get_comma;
29487 }
29488
29489 return list;
29490 }
29491
29492 /* Similarly, but expect leading and trailing parenthesis. This is a very
29493 common case for omp clauses. */
29494
29495 static tree
29496 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
29497 {
29498 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29499 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
29500 return list;
29501 }
29502
29503 /* OpenACC 2.0:
29504 copy ( variable-list )
29505 copyin ( variable-list )
29506 copyout ( variable-list )
29507 create ( variable-list )
29508 delete ( variable-list )
29509 present ( variable-list )
29510 present_or_copy ( variable-list )
29511 pcopy ( variable-list )
29512 present_or_copyin ( variable-list )
29513 pcopyin ( variable-list )
29514 present_or_copyout ( variable-list )
29515 pcopyout ( variable-list )
29516 present_or_create ( variable-list )
29517 pcreate ( variable-list ) */
29518
29519 static tree
29520 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
29521 tree list)
29522 {
29523 enum gomp_map_kind kind;
29524 switch (c_kind)
29525 {
29526 case PRAGMA_OACC_CLAUSE_COPY:
29527 kind = GOMP_MAP_FORCE_TOFROM;
29528 break;
29529 case PRAGMA_OACC_CLAUSE_COPYIN:
29530 kind = GOMP_MAP_FORCE_TO;
29531 break;
29532 case PRAGMA_OACC_CLAUSE_COPYOUT:
29533 kind = GOMP_MAP_FORCE_FROM;
29534 break;
29535 case PRAGMA_OACC_CLAUSE_CREATE:
29536 kind = GOMP_MAP_FORCE_ALLOC;
29537 break;
29538 case PRAGMA_OACC_CLAUSE_DELETE:
29539 kind = GOMP_MAP_FORCE_DEALLOC;
29540 break;
29541 case PRAGMA_OACC_CLAUSE_DEVICE:
29542 kind = GOMP_MAP_FORCE_TO;
29543 break;
29544 case PRAGMA_OACC_CLAUSE_HOST:
29545 case PRAGMA_OACC_CLAUSE_SELF:
29546 kind = GOMP_MAP_FORCE_FROM;
29547 break;
29548 case PRAGMA_OACC_CLAUSE_PRESENT:
29549 kind = GOMP_MAP_FORCE_PRESENT;
29550 break;
29551 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29552 kind = GOMP_MAP_TOFROM;
29553 break;
29554 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29555 kind = GOMP_MAP_TO;
29556 break;
29557 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29558 kind = GOMP_MAP_FROM;
29559 break;
29560 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29561 kind = GOMP_MAP_ALLOC;
29562 break;
29563 default:
29564 gcc_unreachable ();
29565 }
29566 tree nl, c;
29567 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
29568
29569 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
29570 OMP_CLAUSE_SET_MAP_KIND (c, kind);
29571
29572 return nl;
29573 }
29574
29575 /* OpenACC 2.0:
29576 deviceptr ( variable-list ) */
29577
29578 static tree
29579 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
29580 {
29581 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29582 tree vars, t;
29583
29584 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
29585 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
29586 variable-list must only allow for pointer variables. */
29587 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29588 for (t = vars; t; t = TREE_CHAIN (t))
29589 {
29590 tree v = TREE_PURPOSE (t);
29591
29592 /* FIXME diagnostics: Ideally we should keep individual
29593 locations for all the variables in the var list to make the
29594 following errors more precise. Perhaps
29595 c_parser_omp_var_list_parens should construct a list of
29596 locations to go along with the var list. */
29597
29598 if (!VAR_P (v))
29599 error_at (loc, "%qD is not a variable", v);
29600 else if (TREE_TYPE (v) == error_mark_node)
29601 ;
29602 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
29603 error_at (loc, "%qD is not a pointer variable", v);
29604
29605 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
29606 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
29607 OMP_CLAUSE_DECL (u) = v;
29608 OMP_CLAUSE_CHAIN (u) = list;
29609 list = u;
29610 }
29611
29612 return list;
29613 }
29614
29615 /* OpenACC 2.0:
29616 auto
29617 independent
29618 nohost
29619 seq */
29620
29621 static tree
29622 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
29623 enum omp_clause_code code,
29624 tree list, location_t location)
29625 {
29626 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
29627 tree c = build_omp_clause (location, code);
29628 OMP_CLAUSE_CHAIN (c) = list;
29629 return c;
29630 }
29631
29632 /* OpenACC:
29633 num_gangs ( expression )
29634 num_workers ( expression )
29635 vector_length ( expression ) */
29636
29637 static tree
29638 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
29639 const char *str, tree list)
29640 {
29641 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29642
29643 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29644 return list;
29645
29646 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
29647
29648 if (t == error_mark_node
29649 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29650 {
29651 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29652 /*or_comma=*/false,
29653 /*consume_paren=*/true);
29654 return list;
29655 }
29656
29657 check_no_duplicate_clause (list, code, str, loc);
29658
29659 tree c = build_omp_clause (loc, code);
29660 OMP_CLAUSE_OPERAND (c, 0) = t;
29661 OMP_CLAUSE_CHAIN (c) = list;
29662 return c;
29663 }
29664
29665 /* OpenACC:
29666
29667 gang [( gang-arg-list )]
29668 worker [( [num:] int-expr )]
29669 vector [( [length:] int-expr )]
29670
29671 where gang-arg is one of:
29672
29673 [num:] int-expr
29674 static: size-expr
29675
29676 and size-expr may be:
29677
29678 *
29679 int-expr
29680 */
29681
29682 static tree
29683 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
29684 const char *str, tree list)
29685 {
29686 const char *id = "num";
29687 cp_lexer *lexer = parser->lexer;
29688 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
29689 location_t loc = cp_lexer_peek_token (lexer)->location;
29690
29691 if (kind == OMP_CLAUSE_VECTOR)
29692 id = "length";
29693
29694 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
29695 {
29696 cp_lexer_consume_token (lexer);
29697
29698 do
29699 {
29700 cp_token *next = cp_lexer_peek_token (lexer);
29701 int idx = 0;
29702
29703 /* Gang static argument. */
29704 if (kind == OMP_CLAUSE_GANG
29705 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
29706 {
29707 cp_lexer_consume_token (lexer);
29708
29709 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29710 goto cleanup_error;
29711
29712 idx = 1;
29713 if (ops[idx] != NULL)
29714 {
29715 cp_parser_error (parser, "too many %<static%> arguments");
29716 goto cleanup_error;
29717 }
29718
29719 /* Check for the '*' argument. */
29720 if (cp_lexer_next_token_is (lexer, CPP_MULT)
29721 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
29722 || cp_lexer_nth_token_is (parser->lexer, 2,
29723 CPP_CLOSE_PAREN)))
29724 {
29725 cp_lexer_consume_token (lexer);
29726 ops[idx] = integer_minus_one_node;
29727
29728 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
29729 {
29730 cp_lexer_consume_token (lexer);
29731 continue;
29732 }
29733 else break;
29734 }
29735 }
29736 /* Worker num: argument and vector length: arguments. */
29737 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
29738 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
29739 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
29740 {
29741 cp_lexer_consume_token (lexer); /* id */
29742 cp_lexer_consume_token (lexer); /* ':' */
29743 }
29744
29745 /* Now collect the actual argument. */
29746 if (ops[idx] != NULL_TREE)
29747 {
29748 cp_parser_error (parser, "unexpected argument");
29749 goto cleanup_error;
29750 }
29751
29752 tree expr = cp_parser_assignment_expression (parser, NULL, false,
29753 false);
29754 if (expr == error_mark_node)
29755 goto cleanup_error;
29756
29757 mark_exp_read (expr);
29758 ops[idx] = expr;
29759
29760 if (kind == OMP_CLAUSE_GANG
29761 && cp_lexer_next_token_is (lexer, CPP_COMMA))
29762 {
29763 cp_lexer_consume_token (lexer);
29764 continue;
29765 }
29766 break;
29767 }
29768 while (1);
29769
29770 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29771 goto cleanup_error;
29772 }
29773
29774 check_no_duplicate_clause (list, kind, str, loc);
29775
29776 c = build_omp_clause (loc, kind);
29777
29778 if (ops[1])
29779 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
29780
29781 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
29782 OMP_CLAUSE_CHAIN (c) = list;
29783
29784 return c;
29785
29786 cleanup_error:
29787 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
29788 return list;
29789 }
29790
29791 /* OpenACC 2.0:
29792 tile ( size-expr-list ) */
29793
29794 static tree
29795 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
29796 {
29797 tree c, expr = error_mark_node;
29798 tree tile = NULL_TREE;
29799
29800 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
29801
29802 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29803 return list;
29804
29805 do
29806 {
29807 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
29808 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
29809 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
29810 {
29811 cp_lexer_consume_token (parser->lexer);
29812 expr = integer_minus_one_node;
29813 }
29814 else
29815 expr = cp_parser_assignment_expression (parser, NULL, false, false);
29816
29817 if (expr == error_mark_node)
29818 return list;
29819
29820 tile = tree_cons (NULL_TREE, expr, tile);
29821
29822 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29823 cp_lexer_consume_token (parser->lexer);
29824 }
29825 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
29826
29827 /* Consume the trailing ')'. */
29828 cp_lexer_consume_token (parser->lexer);
29829
29830 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
29831 tile = nreverse (tile);
29832 OMP_CLAUSE_TILE_LIST (c) = tile;
29833 OMP_CLAUSE_CHAIN (c) = list;
29834 return c;
29835 }
29836
29837 /* OpenACC 2.0
29838 Parse wait clause or directive parameters. */
29839
29840 static tree
29841 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
29842 {
29843 vec<tree, va_gc> *args;
29844 tree t, args_tree;
29845
29846 args = cp_parser_parenthesized_expression_list (parser, non_attr,
29847 /*cast_p=*/false,
29848 /*allow_expansion_p=*/true,
29849 /*non_constant_p=*/NULL);
29850
29851 if (args == NULL || args->length () == 0)
29852 {
29853 cp_parser_error (parser, "expected integer expression before ')'");
29854 if (args != NULL)
29855 release_tree_vector (args);
29856 return list;
29857 }
29858
29859 args_tree = build_tree_list_vec (args);
29860
29861 release_tree_vector (args);
29862
29863 for (t = args_tree; t; t = TREE_CHAIN (t))
29864 {
29865 tree targ = TREE_VALUE (t);
29866
29867 if (targ != error_mark_node)
29868 {
29869 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
29870 error ("%<wait%> expression must be integral");
29871 else
29872 {
29873 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
29874
29875 mark_rvalue_use (targ);
29876 OMP_CLAUSE_DECL (c) = targ;
29877 OMP_CLAUSE_CHAIN (c) = list;
29878 list = c;
29879 }
29880 }
29881 }
29882
29883 return list;
29884 }
29885
29886 /* OpenACC:
29887 wait ( int-expr-list ) */
29888
29889 static tree
29890 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
29891 {
29892 location_t location = cp_lexer_peek_token (parser->lexer)->location;
29893
29894 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
29895 return list;
29896
29897 list = cp_parser_oacc_wait_list (parser, location, list);
29898
29899 return list;
29900 }
29901
29902 /* OpenMP 3.0:
29903 collapse ( constant-expression ) */
29904
29905 static tree
29906 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
29907 {
29908 tree c, num;
29909 location_t loc;
29910 HOST_WIDE_INT n;
29911
29912 loc = cp_lexer_peek_token (parser->lexer)->location;
29913 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29914 return list;
29915
29916 num = cp_parser_constant_expression (parser);
29917
29918 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29919 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29920 /*or_comma=*/false,
29921 /*consume_paren=*/true);
29922
29923 if (num == error_mark_node)
29924 return list;
29925 num = fold_non_dependent_expr (num);
29926 if (!tree_fits_shwi_p (num)
29927 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
29928 || (n = tree_to_shwi (num)) <= 0
29929 || (int) n != n)
29930 {
29931 error_at (loc, "collapse argument needs positive constant integer expression");
29932 return list;
29933 }
29934
29935 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
29936 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
29937 OMP_CLAUSE_CHAIN (c) = list;
29938 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
29939
29940 return c;
29941 }
29942
29943 /* OpenMP 2.5:
29944 default ( shared | none )
29945
29946 OpenACC 2.0
29947 default (none) */
29948
29949 static tree
29950 cp_parser_omp_clause_default (cp_parser *parser, tree list,
29951 location_t location, bool is_oacc)
29952 {
29953 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
29954 tree c;
29955
29956 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29957 return list;
29958 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29959 {
29960 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29961 const char *p = IDENTIFIER_POINTER (id);
29962
29963 switch (p[0])
29964 {
29965 case 'n':
29966 if (strcmp ("none", p) != 0)
29967 goto invalid_kind;
29968 kind = OMP_CLAUSE_DEFAULT_NONE;
29969 break;
29970
29971 case 's':
29972 if (strcmp ("shared", p) != 0 || is_oacc)
29973 goto invalid_kind;
29974 kind = OMP_CLAUSE_DEFAULT_SHARED;
29975 break;
29976
29977 default:
29978 goto invalid_kind;
29979 }
29980
29981 cp_lexer_consume_token (parser->lexer);
29982 }
29983 else
29984 {
29985 invalid_kind:
29986 if (is_oacc)
29987 cp_parser_error (parser, "expected %<none%>");
29988 else
29989 cp_parser_error (parser, "expected %<none%> or %<shared%>");
29990 }
29991
29992 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29993 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29994 /*or_comma=*/false,
29995 /*consume_paren=*/true);
29996
29997 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
29998 return list;
29999
30000 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
30001 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
30002 OMP_CLAUSE_CHAIN (c) = list;
30003 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
30004
30005 return c;
30006 }
30007
30008 /* OpenMP 3.1:
30009 final ( expression ) */
30010
30011 static tree
30012 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
30013 {
30014 tree t, c;
30015
30016 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30017 return list;
30018
30019 t = cp_parser_condition (parser);
30020
30021 if (t == error_mark_node
30022 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30023 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30024 /*or_comma=*/false,
30025 /*consume_paren=*/true);
30026
30027 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
30028
30029 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
30030 OMP_CLAUSE_FINAL_EXPR (c) = t;
30031 OMP_CLAUSE_CHAIN (c) = list;
30032
30033 return c;
30034 }
30035
30036 /* OpenMP 2.5:
30037 if ( expression )
30038
30039 OpenMP 4.5:
30040 if ( directive-name-modifier : expression )
30041
30042 directive-name-modifier:
30043 parallel | task | taskloop | target data | target | target update
30044 | target enter data | target exit data */
30045
30046 static tree
30047 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
30048 bool is_omp)
30049 {
30050 tree t, c;
30051 enum tree_code if_modifier = ERROR_MARK;
30052
30053 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30054 return list;
30055
30056 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30057 {
30058 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30059 const char *p = IDENTIFIER_POINTER (id);
30060 int n = 2;
30061
30062 if (strcmp ("parallel", p) == 0)
30063 if_modifier = OMP_PARALLEL;
30064 else if (strcmp ("task", p) == 0)
30065 if_modifier = OMP_TASK;
30066 else if (strcmp ("taskloop", p) == 0)
30067 if_modifier = OMP_TASKLOOP;
30068 else if (strcmp ("target", p) == 0)
30069 {
30070 if_modifier = OMP_TARGET;
30071 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
30072 {
30073 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
30074 p = IDENTIFIER_POINTER (id);
30075 if (strcmp ("data", p) == 0)
30076 if_modifier = OMP_TARGET_DATA;
30077 else if (strcmp ("update", p) == 0)
30078 if_modifier = OMP_TARGET_UPDATE;
30079 else if (strcmp ("enter", p) == 0)
30080 if_modifier = OMP_TARGET_ENTER_DATA;
30081 else if (strcmp ("exit", p) == 0)
30082 if_modifier = OMP_TARGET_EXIT_DATA;
30083 if (if_modifier != OMP_TARGET)
30084 n = 3;
30085 else
30086 {
30087 location_t loc
30088 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
30089 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
30090 "or %<exit%>");
30091 if_modifier = ERROR_MARK;
30092 }
30093 if (if_modifier == OMP_TARGET_ENTER_DATA
30094 || if_modifier == OMP_TARGET_EXIT_DATA)
30095 {
30096 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
30097 {
30098 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
30099 p = IDENTIFIER_POINTER (id);
30100 if (strcmp ("data", p) == 0)
30101 n = 4;
30102 }
30103 if (n != 4)
30104 {
30105 location_t loc
30106 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
30107 error_at (loc, "expected %<data%>");
30108 if_modifier = ERROR_MARK;
30109 }
30110 }
30111 }
30112 }
30113 if (if_modifier != ERROR_MARK)
30114 {
30115 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
30116 {
30117 while (n-- > 0)
30118 cp_lexer_consume_token (parser->lexer);
30119 }
30120 else
30121 {
30122 if (n > 2)
30123 {
30124 location_t loc
30125 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
30126 error_at (loc, "expected %<:%>");
30127 }
30128 if_modifier = ERROR_MARK;
30129 }
30130 }
30131 }
30132
30133 t = cp_parser_condition (parser);
30134
30135 if (t == error_mark_node
30136 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30137 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30138 /*or_comma=*/false,
30139 /*consume_paren=*/true);
30140
30141 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
30142 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
30143 {
30144 if (if_modifier != ERROR_MARK
30145 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30146 {
30147 const char *p = NULL;
30148 switch (if_modifier)
30149 {
30150 case OMP_PARALLEL: p = "parallel"; break;
30151 case OMP_TASK: p = "task"; break;
30152 case OMP_TASKLOOP: p = "taskloop"; break;
30153 case OMP_TARGET_DATA: p = "target data"; break;
30154 case OMP_TARGET: p = "target"; break;
30155 case OMP_TARGET_UPDATE: p = "target update"; break;
30156 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
30157 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
30158 default: gcc_unreachable ();
30159 }
30160 error_at (location, "too many %<if%> clauses with %qs modifier",
30161 p);
30162 return list;
30163 }
30164 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30165 {
30166 if (!is_omp)
30167 error_at (location, "too many %<if%> clauses");
30168 else
30169 error_at (location, "too many %<if%> clauses without modifier");
30170 return list;
30171 }
30172 else if (if_modifier == ERROR_MARK
30173 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
30174 {
30175 error_at (location, "if any %<if%> clause has modifier, then all "
30176 "%<if%> clauses have to use modifier");
30177 return list;
30178 }
30179 }
30180
30181 c = build_omp_clause (location, OMP_CLAUSE_IF);
30182 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
30183 OMP_CLAUSE_IF_EXPR (c) = t;
30184 OMP_CLAUSE_CHAIN (c) = list;
30185
30186 return c;
30187 }
30188
30189 /* OpenMP 3.1:
30190 mergeable */
30191
30192 static tree
30193 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
30194 tree list, location_t location)
30195 {
30196 tree c;
30197
30198 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
30199 location);
30200
30201 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
30202 OMP_CLAUSE_CHAIN (c) = list;
30203 return c;
30204 }
30205
30206 /* OpenMP 2.5:
30207 nowait */
30208
30209 static tree
30210 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
30211 tree list, location_t location)
30212 {
30213 tree c;
30214
30215 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
30216
30217 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
30218 OMP_CLAUSE_CHAIN (c) = list;
30219 return c;
30220 }
30221
30222 /* OpenMP 2.5:
30223 num_threads ( expression ) */
30224
30225 static tree
30226 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
30227 location_t location)
30228 {
30229 tree t, c;
30230
30231 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30232 return list;
30233
30234 t = cp_parser_expression (parser);
30235
30236 if (t == error_mark_node
30237 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30238 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30239 /*or_comma=*/false,
30240 /*consume_paren=*/true);
30241
30242 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
30243 "num_threads", location);
30244
30245 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
30246 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
30247 OMP_CLAUSE_CHAIN (c) = list;
30248
30249 return c;
30250 }
30251
30252 /* OpenMP 4.5:
30253 num_tasks ( expression ) */
30254
30255 static tree
30256 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
30257 location_t location)
30258 {
30259 tree t, c;
30260
30261 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30262 return list;
30263
30264 t = cp_parser_expression (parser);
30265
30266 if (t == error_mark_node
30267 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30268 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30269 /*or_comma=*/false,
30270 /*consume_paren=*/true);
30271
30272 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
30273 "num_tasks", location);
30274
30275 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
30276 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
30277 OMP_CLAUSE_CHAIN (c) = list;
30278
30279 return c;
30280 }
30281
30282 /* OpenMP 4.5:
30283 grainsize ( expression ) */
30284
30285 static tree
30286 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
30287 location_t location)
30288 {
30289 tree t, c;
30290
30291 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30292 return list;
30293
30294 t = cp_parser_expression (parser);
30295
30296 if (t == error_mark_node
30297 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30298 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30299 /*or_comma=*/false,
30300 /*consume_paren=*/true);
30301
30302 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
30303 "grainsize", location);
30304
30305 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
30306 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
30307 OMP_CLAUSE_CHAIN (c) = list;
30308
30309 return c;
30310 }
30311
30312 /* OpenMP 4.5:
30313 priority ( expression ) */
30314
30315 static tree
30316 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
30317 location_t location)
30318 {
30319 tree t, c;
30320
30321 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30322 return list;
30323
30324 t = cp_parser_expression (parser);
30325
30326 if (t == error_mark_node
30327 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30328 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30329 /*or_comma=*/false,
30330 /*consume_paren=*/true);
30331
30332 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
30333 "priority", location);
30334
30335 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
30336 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
30337 OMP_CLAUSE_CHAIN (c) = list;
30338
30339 return c;
30340 }
30341
30342 /* OpenMP 4.5:
30343 hint ( expression ) */
30344
30345 static tree
30346 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
30347 location_t location)
30348 {
30349 tree t, c;
30350
30351 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30352 return list;
30353
30354 t = cp_parser_expression (parser);
30355
30356 if (t == error_mark_node
30357 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30358 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30359 /*or_comma=*/false,
30360 /*consume_paren=*/true);
30361
30362 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
30363
30364 c = build_omp_clause (location, OMP_CLAUSE_HINT);
30365 OMP_CLAUSE_HINT_EXPR (c) = t;
30366 OMP_CLAUSE_CHAIN (c) = list;
30367
30368 return c;
30369 }
30370
30371 /* OpenMP 4.5:
30372 defaultmap ( tofrom : scalar ) */
30373
30374 static tree
30375 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
30376 location_t location)
30377 {
30378 tree c, id;
30379 const char *p;
30380
30381 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30382 return list;
30383
30384 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30385 {
30386 cp_parser_error (parser, "expected %<tofrom%>");
30387 goto out_err;
30388 }
30389 id = cp_lexer_peek_token (parser->lexer)->u.value;
30390 p = IDENTIFIER_POINTER (id);
30391 if (strcmp (p, "tofrom") != 0)
30392 {
30393 cp_parser_error (parser, "expected %<tofrom%>");
30394 goto out_err;
30395 }
30396 cp_lexer_consume_token (parser->lexer);
30397 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30398 goto out_err;
30399
30400 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30401 {
30402 cp_parser_error (parser, "expected %<scalar%>");
30403 goto out_err;
30404 }
30405 id = cp_lexer_peek_token (parser->lexer)->u.value;
30406 p = IDENTIFIER_POINTER (id);
30407 if (strcmp (p, "scalar") != 0)
30408 {
30409 cp_parser_error (parser, "expected %<scalar%>");
30410 goto out_err;
30411 }
30412 cp_lexer_consume_token (parser->lexer);
30413 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30414 goto out_err;
30415
30416 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
30417 location);
30418
30419 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
30420 OMP_CLAUSE_CHAIN (c) = list;
30421 return c;
30422
30423 out_err:
30424 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30425 /*or_comma=*/false,
30426 /*consume_paren=*/true);
30427 return list;
30428 }
30429
30430 /* OpenMP 2.5:
30431 ordered
30432
30433 OpenMP 4.5:
30434 ordered ( constant-expression ) */
30435
30436 static tree
30437 cp_parser_omp_clause_ordered (cp_parser *parser,
30438 tree list, location_t location)
30439 {
30440 tree c, num = NULL_TREE;
30441 HOST_WIDE_INT n;
30442
30443 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
30444 "ordered", location);
30445
30446 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30447 {
30448 cp_lexer_consume_token (parser->lexer);
30449
30450 num = cp_parser_constant_expression (parser);
30451
30452 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30453 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30454 /*or_comma=*/false,
30455 /*consume_paren=*/true);
30456
30457 if (num == error_mark_node)
30458 return list;
30459 num = fold_non_dependent_expr (num);
30460 if (!tree_fits_shwi_p (num)
30461 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30462 || (n = tree_to_shwi (num)) <= 0
30463 || (int) n != n)
30464 {
30465 error_at (location,
30466 "ordered argument needs positive constant integer "
30467 "expression");
30468 return list;
30469 }
30470 }
30471
30472 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
30473 OMP_CLAUSE_ORDERED_EXPR (c) = num;
30474 OMP_CLAUSE_CHAIN (c) = list;
30475 return c;
30476 }
30477
30478 /* OpenMP 2.5:
30479 reduction ( reduction-operator : variable-list )
30480
30481 reduction-operator:
30482 One of: + * - & ^ | && ||
30483
30484 OpenMP 3.1:
30485
30486 reduction-operator:
30487 One of: + * - & ^ | && || min max
30488
30489 OpenMP 4.0:
30490
30491 reduction-operator:
30492 One of: + * - & ^ | && ||
30493 id-expression */
30494
30495 static tree
30496 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
30497 {
30498 enum tree_code code = ERROR_MARK;
30499 tree nlist, c, id = NULL_TREE;
30500
30501 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30502 return list;
30503
30504 switch (cp_lexer_peek_token (parser->lexer)->type)
30505 {
30506 case CPP_PLUS: code = PLUS_EXPR; break;
30507 case CPP_MULT: code = MULT_EXPR; break;
30508 case CPP_MINUS: code = MINUS_EXPR; break;
30509 case CPP_AND: code = BIT_AND_EXPR; break;
30510 case CPP_XOR: code = BIT_XOR_EXPR; break;
30511 case CPP_OR: code = BIT_IOR_EXPR; break;
30512 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
30513 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
30514 default: break;
30515 }
30516
30517 if (code != ERROR_MARK)
30518 cp_lexer_consume_token (parser->lexer);
30519 else
30520 {
30521 bool saved_colon_corrects_to_scope_p;
30522 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30523 parser->colon_corrects_to_scope_p = false;
30524 id = cp_parser_id_expression (parser, /*template_p=*/false,
30525 /*check_dependency_p=*/true,
30526 /*template_p=*/NULL,
30527 /*declarator_p=*/false,
30528 /*optional_p=*/false);
30529 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30530 if (identifier_p (id))
30531 {
30532 const char *p = IDENTIFIER_POINTER (id);
30533
30534 if (strcmp (p, "min") == 0)
30535 code = MIN_EXPR;
30536 else if (strcmp (p, "max") == 0)
30537 code = MAX_EXPR;
30538 else if (id == ansi_opname (PLUS_EXPR))
30539 code = PLUS_EXPR;
30540 else if (id == ansi_opname (MULT_EXPR))
30541 code = MULT_EXPR;
30542 else if (id == ansi_opname (MINUS_EXPR))
30543 code = MINUS_EXPR;
30544 else if (id == ansi_opname (BIT_AND_EXPR))
30545 code = BIT_AND_EXPR;
30546 else if (id == ansi_opname (BIT_IOR_EXPR))
30547 code = BIT_IOR_EXPR;
30548 else if (id == ansi_opname (BIT_XOR_EXPR))
30549 code = BIT_XOR_EXPR;
30550 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
30551 code = TRUTH_ANDIF_EXPR;
30552 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
30553 code = TRUTH_ORIF_EXPR;
30554 id = omp_reduction_id (code, id, NULL_TREE);
30555 tree scope = parser->scope;
30556 if (scope)
30557 id = build_qualified_name (NULL_TREE, scope, id, false);
30558 parser->scope = NULL_TREE;
30559 parser->qualifying_scope = NULL_TREE;
30560 parser->object_scope = NULL_TREE;
30561 }
30562 else
30563 {
30564 error ("invalid reduction-identifier");
30565 resync_fail:
30566 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30567 /*or_comma=*/false,
30568 /*consume_paren=*/true);
30569 return list;
30570 }
30571 }
30572
30573 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30574 goto resync_fail;
30575
30576 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
30577 NULL);
30578 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
30579 {
30580 OMP_CLAUSE_REDUCTION_CODE (c) = code;
30581 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
30582 }
30583
30584 return nlist;
30585 }
30586
30587 /* OpenMP 2.5:
30588 schedule ( schedule-kind )
30589 schedule ( schedule-kind , expression )
30590
30591 schedule-kind:
30592 static | dynamic | guided | runtime | auto
30593
30594 OpenMP 4.5:
30595 schedule ( schedule-modifier : schedule-kind )
30596 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
30597
30598 schedule-modifier:
30599 simd
30600 monotonic
30601 nonmonotonic */
30602
30603 static tree
30604 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
30605 {
30606 tree c, t;
30607 int modifiers = 0, nmodifiers = 0;
30608
30609 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30610 return list;
30611
30612 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
30613
30614 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30615 {
30616 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30617 const char *p = IDENTIFIER_POINTER (id);
30618 if (strcmp ("simd", p) == 0)
30619 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
30620 else if (strcmp ("monotonic", p) == 0)
30621 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
30622 else if (strcmp ("nonmonotonic", p) == 0)
30623 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
30624 else
30625 break;
30626 cp_lexer_consume_token (parser->lexer);
30627 if (nmodifiers++ == 0
30628 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30629 cp_lexer_consume_token (parser->lexer);
30630 else
30631 {
30632 cp_parser_require (parser, CPP_COLON, RT_COLON);
30633 break;
30634 }
30635 }
30636
30637 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30638 {
30639 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30640 const char *p = IDENTIFIER_POINTER (id);
30641
30642 switch (p[0])
30643 {
30644 case 'd':
30645 if (strcmp ("dynamic", p) != 0)
30646 goto invalid_kind;
30647 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
30648 break;
30649
30650 case 'g':
30651 if (strcmp ("guided", p) != 0)
30652 goto invalid_kind;
30653 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
30654 break;
30655
30656 case 'r':
30657 if (strcmp ("runtime", p) != 0)
30658 goto invalid_kind;
30659 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
30660 break;
30661
30662 default:
30663 goto invalid_kind;
30664 }
30665 }
30666 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
30667 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
30668 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30669 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
30670 else
30671 goto invalid_kind;
30672 cp_lexer_consume_token (parser->lexer);
30673
30674 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
30675 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
30676 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
30677 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
30678 {
30679 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
30680 "specified");
30681 modifiers = 0;
30682 }
30683
30684 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30685 {
30686 cp_token *token;
30687 cp_lexer_consume_token (parser->lexer);
30688
30689 token = cp_lexer_peek_token (parser->lexer);
30690 t = cp_parser_assignment_expression (parser);
30691
30692 if (t == error_mark_node)
30693 goto resync_fail;
30694 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
30695 error_at (token->location, "schedule %<runtime%> does not take "
30696 "a %<chunk_size%> parameter");
30697 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
30698 error_at (token->location, "schedule %<auto%> does not take "
30699 "a %<chunk_size%> parameter");
30700 else
30701 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
30702
30703 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30704 goto resync_fail;
30705 }
30706 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
30707 goto resync_fail;
30708
30709 OMP_CLAUSE_SCHEDULE_KIND (c)
30710 = (enum omp_clause_schedule_kind)
30711 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
30712
30713 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
30714 OMP_CLAUSE_CHAIN (c) = list;
30715 return c;
30716
30717 invalid_kind:
30718 cp_parser_error (parser, "invalid schedule kind");
30719 resync_fail:
30720 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30721 /*or_comma=*/false,
30722 /*consume_paren=*/true);
30723 return list;
30724 }
30725
30726 /* OpenMP 3.0:
30727 untied */
30728
30729 static tree
30730 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
30731 tree list, location_t location)
30732 {
30733 tree c;
30734
30735 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
30736
30737 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
30738 OMP_CLAUSE_CHAIN (c) = list;
30739 return c;
30740 }
30741
30742 /* OpenMP 4.0:
30743 inbranch
30744 notinbranch */
30745
30746 static tree
30747 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
30748 tree list, location_t location)
30749 {
30750 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30751 tree c = build_omp_clause (location, code);
30752 OMP_CLAUSE_CHAIN (c) = list;
30753 return c;
30754 }
30755
30756 /* OpenMP 4.0:
30757 parallel
30758 for
30759 sections
30760 taskgroup */
30761
30762 static tree
30763 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
30764 enum omp_clause_code code,
30765 tree list, location_t location)
30766 {
30767 tree c = build_omp_clause (location, code);
30768 OMP_CLAUSE_CHAIN (c) = list;
30769 return c;
30770 }
30771
30772 /* OpenMP 4.5:
30773 nogroup */
30774
30775 static tree
30776 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
30777 tree list, location_t location)
30778 {
30779 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
30780 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
30781 OMP_CLAUSE_CHAIN (c) = list;
30782 return c;
30783 }
30784
30785 /* OpenMP 4.5:
30786 simd
30787 threads */
30788
30789 static tree
30790 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
30791 enum omp_clause_code code,
30792 tree list, location_t location)
30793 {
30794 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30795 tree c = build_omp_clause (location, code);
30796 OMP_CLAUSE_CHAIN (c) = list;
30797 return c;
30798 }
30799
30800 /* OpenMP 4.0:
30801 num_teams ( expression ) */
30802
30803 static tree
30804 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
30805 location_t location)
30806 {
30807 tree t, c;
30808
30809 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30810 return list;
30811
30812 t = cp_parser_expression (parser);
30813
30814 if (t == error_mark_node
30815 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30816 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30817 /*or_comma=*/false,
30818 /*consume_paren=*/true);
30819
30820 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
30821 "num_teams", location);
30822
30823 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
30824 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
30825 OMP_CLAUSE_CHAIN (c) = list;
30826
30827 return c;
30828 }
30829
30830 /* OpenMP 4.0:
30831 thread_limit ( expression ) */
30832
30833 static tree
30834 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
30835 location_t location)
30836 {
30837 tree t, c;
30838
30839 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30840 return list;
30841
30842 t = cp_parser_expression (parser);
30843
30844 if (t == error_mark_node
30845 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30846 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30847 /*or_comma=*/false,
30848 /*consume_paren=*/true);
30849
30850 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
30851 "thread_limit", location);
30852
30853 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
30854 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
30855 OMP_CLAUSE_CHAIN (c) = list;
30856
30857 return c;
30858 }
30859
30860 /* OpenMP 4.0:
30861 aligned ( variable-list )
30862 aligned ( variable-list : constant-expression ) */
30863
30864 static tree
30865 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
30866 {
30867 tree nlist, c, alignment = NULL_TREE;
30868 bool colon;
30869
30870 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30871 return list;
30872
30873 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
30874 &colon);
30875
30876 if (colon)
30877 {
30878 alignment = cp_parser_constant_expression (parser);
30879
30880 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30881 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30882 /*or_comma=*/false,
30883 /*consume_paren=*/true);
30884
30885 if (alignment == error_mark_node)
30886 alignment = NULL_TREE;
30887 }
30888
30889 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
30890 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
30891
30892 return nlist;
30893 }
30894
30895 /* OpenMP 4.0:
30896 linear ( variable-list )
30897 linear ( variable-list : expression )
30898
30899 OpenMP 4.5:
30900 linear ( modifier ( variable-list ) )
30901 linear ( modifier ( variable-list ) : expression ) */
30902
30903 static tree
30904 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
30905 bool is_cilk_simd_fn, bool declare_simd)
30906 {
30907 tree nlist, c, step = integer_one_node;
30908 bool colon;
30909 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
30910
30911 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30912 return list;
30913
30914 if (!is_cilk_simd_fn
30915 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30916 {
30917 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30918 const char *p = IDENTIFIER_POINTER (id);
30919
30920 if (strcmp ("ref", p) == 0)
30921 kind = OMP_CLAUSE_LINEAR_REF;
30922 else if (strcmp ("val", p) == 0)
30923 kind = OMP_CLAUSE_LINEAR_VAL;
30924 else if (strcmp ("uval", p) == 0)
30925 kind = OMP_CLAUSE_LINEAR_UVAL;
30926 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
30927 cp_lexer_consume_token (parser->lexer);
30928 else
30929 kind = OMP_CLAUSE_LINEAR_DEFAULT;
30930 }
30931
30932 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
30933 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
30934 &colon);
30935 else
30936 {
30937 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
30938 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
30939 if (colon)
30940 cp_parser_require (parser, CPP_COLON, RT_COLON);
30941 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30942 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30943 /*or_comma=*/false,
30944 /*consume_paren=*/true);
30945 }
30946
30947 if (colon)
30948 {
30949 step = NULL_TREE;
30950 if (declare_simd
30951 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
30952 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
30953 {
30954 cp_token *token = cp_lexer_peek_token (parser->lexer);
30955 cp_parser_parse_tentatively (parser);
30956 step = cp_parser_id_expression (parser, /*template_p=*/false,
30957 /*check_dependency_p=*/true,
30958 /*template_p=*/NULL,
30959 /*declarator_p=*/false,
30960 /*optional_p=*/false);
30961 if (step != error_mark_node)
30962 step = cp_parser_lookup_name_simple (parser, step, token->location);
30963 if (step == error_mark_node)
30964 {
30965 step = NULL_TREE;
30966 cp_parser_abort_tentative_parse (parser);
30967 }
30968 else if (!cp_parser_parse_definitely (parser))
30969 step = NULL_TREE;
30970 }
30971 if (!step)
30972 step = cp_parser_expression (parser);
30973
30974 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
30975 {
30976 sorry ("using parameters for %<linear%> step is not supported yet");
30977 step = integer_one_node;
30978 }
30979 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30980 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30981 /*or_comma=*/false,
30982 /*consume_paren=*/true);
30983
30984 if (step == error_mark_node)
30985 return list;
30986 }
30987
30988 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
30989 {
30990 OMP_CLAUSE_LINEAR_STEP (c) = step;
30991 OMP_CLAUSE_LINEAR_KIND (c) = kind;
30992 }
30993
30994 return nlist;
30995 }
30996
30997 /* OpenMP 4.0:
30998 safelen ( constant-expression ) */
30999
31000 static tree
31001 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
31002 location_t location)
31003 {
31004 tree t, c;
31005
31006 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31007 return list;
31008
31009 t = cp_parser_constant_expression (parser);
31010
31011 if (t == error_mark_node
31012 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31013 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31014 /*or_comma=*/false,
31015 /*consume_paren=*/true);
31016
31017 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
31018
31019 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
31020 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
31021 OMP_CLAUSE_CHAIN (c) = list;
31022
31023 return c;
31024 }
31025
31026 /* OpenMP 4.0:
31027 simdlen ( constant-expression ) */
31028
31029 static tree
31030 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
31031 location_t location)
31032 {
31033 tree t, c;
31034
31035 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31036 return list;
31037
31038 t = cp_parser_constant_expression (parser);
31039
31040 if (t == error_mark_node
31041 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31042 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31043 /*or_comma=*/false,
31044 /*consume_paren=*/true);
31045
31046 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
31047
31048 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
31049 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
31050 OMP_CLAUSE_CHAIN (c) = list;
31051
31052 return c;
31053 }
31054
31055 /* OpenMP 4.5:
31056 vec:
31057 identifier [+/- integer]
31058 vec , identifier [+/- integer]
31059 */
31060
31061 static tree
31062 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
31063 tree list)
31064 {
31065 tree vec = NULL;
31066
31067 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31068 {
31069 cp_parser_error (parser, "expected identifier");
31070 return list;
31071 }
31072
31073 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31074 {
31075 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
31076 tree t, identifier = cp_parser_identifier (parser);
31077 tree addend = NULL;
31078
31079 if (identifier == error_mark_node)
31080 t = error_mark_node;
31081 else
31082 {
31083 t = cp_parser_lookup_name_simple
31084 (parser, identifier,
31085 cp_lexer_peek_token (parser->lexer)->location);
31086 if (t == error_mark_node)
31087 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
31088 id_loc);
31089 }
31090
31091 bool neg = false;
31092 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31093 neg = true;
31094 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
31095 {
31096 addend = integer_zero_node;
31097 goto add_to_vector;
31098 }
31099 cp_lexer_consume_token (parser->lexer);
31100
31101 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
31102 {
31103 cp_parser_error (parser, "expected integer");
31104 return list;
31105 }
31106
31107 addend = cp_lexer_peek_token (parser->lexer)->u.value;
31108 if (TREE_CODE (addend) != INTEGER_CST)
31109 {
31110 cp_parser_error (parser, "expected integer");
31111 return list;
31112 }
31113 cp_lexer_consume_token (parser->lexer);
31114
31115 add_to_vector:
31116 if (t != error_mark_node)
31117 {
31118 vec = tree_cons (addend, t, vec);
31119 if (neg)
31120 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
31121 }
31122
31123 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31124 break;
31125
31126 cp_lexer_consume_token (parser->lexer);
31127 }
31128
31129 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
31130 {
31131 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
31132 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
31133 OMP_CLAUSE_DECL (u) = nreverse (vec);
31134 OMP_CLAUSE_CHAIN (u) = list;
31135 return u;
31136 }
31137 return list;
31138 }
31139
31140 /* OpenMP 4.0:
31141 depend ( depend-kind : variable-list )
31142
31143 depend-kind:
31144 in | out | inout
31145
31146 OpenMP 4.5:
31147 depend ( source )
31148
31149 depend ( sink : vec ) */
31150
31151 static tree
31152 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
31153 {
31154 tree nlist, c;
31155 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
31156
31157 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31158 return list;
31159
31160 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31161 {
31162 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31163 const char *p = IDENTIFIER_POINTER (id);
31164
31165 if (strcmp ("in", p) == 0)
31166 kind = OMP_CLAUSE_DEPEND_IN;
31167 else if (strcmp ("inout", p) == 0)
31168 kind = OMP_CLAUSE_DEPEND_INOUT;
31169 else if (strcmp ("out", p) == 0)
31170 kind = OMP_CLAUSE_DEPEND_OUT;
31171 else if (strcmp ("source", p) == 0)
31172 kind = OMP_CLAUSE_DEPEND_SOURCE;
31173 else if (strcmp ("sink", p) == 0)
31174 kind = OMP_CLAUSE_DEPEND_SINK;
31175 else
31176 goto invalid_kind;
31177 }
31178 else
31179 goto invalid_kind;
31180
31181 cp_lexer_consume_token (parser->lexer);
31182
31183 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
31184 {
31185 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
31186 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31187 OMP_CLAUSE_DECL (c) = NULL_TREE;
31188 OMP_CLAUSE_CHAIN (c) = list;
31189 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31190 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31191 /*or_comma=*/false,
31192 /*consume_paren=*/true);
31193 return c;
31194 }
31195
31196 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31197 goto resync_fail;
31198
31199 if (kind == OMP_CLAUSE_DEPEND_SINK)
31200 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
31201 else
31202 {
31203 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
31204 list, NULL);
31205
31206 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31207 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31208 }
31209 return nlist;
31210
31211 invalid_kind:
31212 cp_parser_error (parser, "invalid depend kind");
31213 resync_fail:
31214 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31215 /*or_comma=*/false,
31216 /*consume_paren=*/true);
31217 return list;
31218 }
31219
31220 /* OpenMP 4.0:
31221 map ( map-kind : variable-list )
31222 map ( variable-list )
31223
31224 map-kind:
31225 alloc | to | from | tofrom
31226
31227 OpenMP 4.5:
31228 map-kind:
31229 alloc | to | from | tofrom | release | delete
31230
31231 map ( always [,] map-kind: variable-list ) */
31232
31233 static tree
31234 cp_parser_omp_clause_map (cp_parser *parser, tree list)
31235 {
31236 tree nlist, c;
31237 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
31238 bool always = false;
31239
31240 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31241 return list;
31242
31243 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31244 {
31245 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31246 const char *p = IDENTIFIER_POINTER (id);
31247
31248 if (strcmp ("always", p) == 0)
31249 {
31250 int nth = 2;
31251 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
31252 nth++;
31253 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
31254 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
31255 == RID_DELETE))
31256 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
31257 == CPP_COLON))
31258 {
31259 always = true;
31260 cp_lexer_consume_token (parser->lexer);
31261 if (nth == 3)
31262 cp_lexer_consume_token (parser->lexer);
31263 }
31264 }
31265 }
31266
31267 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31268 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31269 {
31270 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31271 const char *p = IDENTIFIER_POINTER (id);
31272
31273 if (strcmp ("alloc", p) == 0)
31274 kind = GOMP_MAP_ALLOC;
31275 else if (strcmp ("to", p) == 0)
31276 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
31277 else if (strcmp ("from", p) == 0)
31278 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
31279 else if (strcmp ("tofrom", p) == 0)
31280 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
31281 else if (strcmp ("release", p) == 0)
31282 kind = GOMP_MAP_RELEASE;
31283 else
31284 {
31285 cp_parser_error (parser, "invalid map kind");
31286 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31287 /*or_comma=*/false,
31288 /*consume_paren=*/true);
31289 return list;
31290 }
31291 cp_lexer_consume_token (parser->lexer);
31292 cp_lexer_consume_token (parser->lexer);
31293 }
31294 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
31295 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31296 {
31297 kind = GOMP_MAP_DELETE;
31298 cp_lexer_consume_token (parser->lexer);
31299 cp_lexer_consume_token (parser->lexer);
31300 }
31301
31302 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
31303 NULL);
31304
31305 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31306 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31307
31308 return nlist;
31309 }
31310
31311 /* OpenMP 4.0:
31312 device ( expression ) */
31313
31314 static tree
31315 cp_parser_omp_clause_device (cp_parser *parser, tree list,
31316 location_t location)
31317 {
31318 tree t, c;
31319
31320 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31321 return list;
31322
31323 t = cp_parser_expression (parser);
31324
31325 if (t == error_mark_node
31326 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31327 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31328 /*or_comma=*/false,
31329 /*consume_paren=*/true);
31330
31331 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
31332 "device", location);
31333
31334 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
31335 OMP_CLAUSE_DEVICE_ID (c) = t;
31336 OMP_CLAUSE_CHAIN (c) = list;
31337
31338 return c;
31339 }
31340
31341 /* OpenMP 4.0:
31342 dist_schedule ( static )
31343 dist_schedule ( static , expression ) */
31344
31345 static tree
31346 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
31347 location_t location)
31348 {
31349 tree c, t;
31350
31351 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31352 return list;
31353
31354 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
31355
31356 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31357 goto invalid_kind;
31358 cp_lexer_consume_token (parser->lexer);
31359
31360 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31361 {
31362 cp_lexer_consume_token (parser->lexer);
31363
31364 t = cp_parser_assignment_expression (parser);
31365
31366 if (t == error_mark_node)
31367 goto resync_fail;
31368 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
31369
31370 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31371 goto resync_fail;
31372 }
31373 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31374 goto resync_fail;
31375
31376 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
31377 location);
31378 OMP_CLAUSE_CHAIN (c) = list;
31379 return c;
31380
31381 invalid_kind:
31382 cp_parser_error (parser, "invalid dist_schedule kind");
31383 resync_fail:
31384 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31385 /*or_comma=*/false,
31386 /*consume_paren=*/true);
31387 return list;
31388 }
31389
31390 /* OpenMP 4.0:
31391 proc_bind ( proc-bind-kind )
31392
31393 proc-bind-kind:
31394 master | close | spread */
31395
31396 static tree
31397 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
31398 location_t location)
31399 {
31400 tree c;
31401 enum omp_clause_proc_bind_kind kind;
31402
31403 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31404 return list;
31405
31406 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31407 {
31408 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31409 const char *p = IDENTIFIER_POINTER (id);
31410
31411 if (strcmp ("master", p) == 0)
31412 kind = OMP_CLAUSE_PROC_BIND_MASTER;
31413 else if (strcmp ("close", p) == 0)
31414 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
31415 else if (strcmp ("spread", p) == 0)
31416 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
31417 else
31418 goto invalid_kind;
31419 }
31420 else
31421 goto invalid_kind;
31422
31423 cp_lexer_consume_token (parser->lexer);
31424 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31425 goto resync_fail;
31426
31427 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
31428 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
31429 location);
31430 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
31431 OMP_CLAUSE_CHAIN (c) = list;
31432 return c;
31433
31434 invalid_kind:
31435 cp_parser_error (parser, "invalid depend kind");
31436 resync_fail:
31437 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31438 /*or_comma=*/false,
31439 /*consume_paren=*/true);
31440 return list;
31441 }
31442
31443 /* OpenACC:
31444 async [( int-expr )] */
31445
31446 static tree
31447 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
31448 {
31449 tree c, t;
31450 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31451
31452 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
31453
31454 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31455 {
31456 cp_lexer_consume_token (parser->lexer);
31457
31458 t = cp_parser_expression (parser);
31459 if (t == error_mark_node
31460 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31461 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31462 /*or_comma=*/false,
31463 /*consume_paren=*/true);
31464 }
31465
31466 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
31467
31468 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
31469 OMP_CLAUSE_ASYNC_EXPR (c) = t;
31470 OMP_CLAUSE_CHAIN (c) = list;
31471 list = c;
31472
31473 return list;
31474 }
31475
31476 /* Parse all OpenACC clauses. The set clauses allowed by the directive
31477 is a bitmask in MASK. Return the list of clauses found. */
31478
31479 static tree
31480 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
31481 const char *where, cp_token *pragma_tok,
31482 bool finish_p = true)
31483 {
31484 tree clauses = NULL;
31485 bool first = true;
31486
31487 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31488 {
31489 location_t here;
31490 pragma_omp_clause c_kind;
31491 omp_clause_code code;
31492 const char *c_name;
31493 tree prev = clauses;
31494
31495 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31496 cp_lexer_consume_token (parser->lexer);
31497
31498 here = cp_lexer_peek_token (parser->lexer)->location;
31499 c_kind = cp_parser_omp_clause_name (parser);
31500
31501 switch (c_kind)
31502 {
31503 case PRAGMA_OACC_CLAUSE_ASYNC:
31504 clauses = cp_parser_oacc_clause_async (parser, clauses);
31505 c_name = "async";
31506 break;
31507 case PRAGMA_OACC_CLAUSE_AUTO:
31508 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
31509 clauses, here);
31510 c_name = "auto";
31511 break;
31512 case PRAGMA_OACC_CLAUSE_COLLAPSE:
31513 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
31514 c_name = "collapse";
31515 break;
31516 case PRAGMA_OACC_CLAUSE_COPY:
31517 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31518 c_name = "copy";
31519 break;
31520 case PRAGMA_OACC_CLAUSE_COPYIN:
31521 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31522 c_name = "copyin";
31523 break;
31524 case PRAGMA_OACC_CLAUSE_COPYOUT:
31525 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31526 c_name = "copyout";
31527 break;
31528 case PRAGMA_OACC_CLAUSE_CREATE:
31529 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31530 c_name = "create";
31531 break;
31532 case PRAGMA_OACC_CLAUSE_DELETE:
31533 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31534 c_name = "delete";
31535 break;
31536 case PRAGMA_OMP_CLAUSE_DEFAULT:
31537 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
31538 c_name = "default";
31539 break;
31540 case PRAGMA_OACC_CLAUSE_DEVICE:
31541 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31542 c_name = "device";
31543 break;
31544 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
31545 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
31546 c_name = "deviceptr";
31547 break;
31548 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
31549 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
31550 clauses);
31551 c_name = "firstprivate";
31552 break;
31553 case PRAGMA_OACC_CLAUSE_GANG:
31554 c_name = "gang";
31555 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
31556 c_name, clauses);
31557 break;
31558 case PRAGMA_OACC_CLAUSE_HOST:
31559 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31560 c_name = "host";
31561 break;
31562 case PRAGMA_OACC_CLAUSE_IF:
31563 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
31564 c_name = "if";
31565 break;
31566 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
31567 clauses = cp_parser_oacc_simple_clause (parser,
31568 OMP_CLAUSE_INDEPENDENT,
31569 clauses, here);
31570 c_name = "independent";
31571 break;
31572 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
31573 code = OMP_CLAUSE_NUM_GANGS;
31574 c_name = "num_gangs";
31575 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
31576 clauses);
31577 break;
31578 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
31579 c_name = "num_workers";
31580 code = OMP_CLAUSE_NUM_WORKERS;
31581 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
31582 clauses);
31583 break;
31584 case PRAGMA_OACC_CLAUSE_PRESENT:
31585 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31586 c_name = "present";
31587 break;
31588 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31589 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31590 c_name = "present_or_copy";
31591 break;
31592 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31593 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31594 c_name = "present_or_copyin";
31595 break;
31596 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31597 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31598 c_name = "present_or_copyout";
31599 break;
31600 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31601 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31602 c_name = "present_or_create";
31603 break;
31604 case PRAGMA_OACC_CLAUSE_PRIVATE:
31605 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
31606 clauses);
31607 c_name = "private";
31608 break;
31609 case PRAGMA_OACC_CLAUSE_REDUCTION:
31610 clauses = cp_parser_omp_clause_reduction (parser, clauses);
31611 c_name = "reduction";
31612 break;
31613 case PRAGMA_OACC_CLAUSE_SELF:
31614 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
31615 c_name = "self";
31616 break;
31617 case PRAGMA_OACC_CLAUSE_SEQ:
31618 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
31619 clauses, here);
31620 c_name = "seq";
31621 break;
31622 case PRAGMA_OACC_CLAUSE_TILE:
31623 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
31624 c_name = "tile";
31625 break;
31626 case PRAGMA_OACC_CLAUSE_VECTOR:
31627 c_name = "vector";
31628 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
31629 c_name, clauses);
31630 break;
31631 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
31632 c_name = "vector_length";
31633 code = OMP_CLAUSE_VECTOR_LENGTH;
31634 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
31635 clauses);
31636 break;
31637 case PRAGMA_OACC_CLAUSE_WAIT:
31638 clauses = cp_parser_oacc_clause_wait (parser, clauses);
31639 c_name = "wait";
31640 break;
31641 case PRAGMA_OACC_CLAUSE_WORKER:
31642 c_name = "worker";
31643 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
31644 c_name, clauses);
31645 break;
31646 default:
31647 cp_parser_error (parser, "expected %<#pragma acc%> clause");
31648 goto saw_error;
31649 }
31650
31651 first = false;
31652
31653 if (((mask >> c_kind) & 1) == 0)
31654 {
31655 /* Remove the invalid clause(s) from the list to avoid
31656 confusing the rest of the compiler. */
31657 clauses = prev;
31658 error_at (here, "%qs is not valid for %qs", c_name, where);
31659 }
31660 }
31661
31662 saw_error:
31663 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31664
31665 if (finish_p)
31666 return finish_omp_clauses (clauses, false);
31667
31668 return clauses;
31669 }
31670
31671 /* Parse all OpenMP clauses. The set clauses allowed by the directive
31672 is a bitmask in MASK. Return the list of clauses found; the result
31673 of clause default goes in *pdefault. */
31674
31675 static tree
31676 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
31677 const char *where, cp_token *pragma_tok,
31678 bool finish_p = true)
31679 {
31680 tree clauses = NULL;
31681 bool first = true;
31682 cp_token *token = NULL;
31683
31684 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31685 {
31686 pragma_omp_clause c_kind;
31687 const char *c_name;
31688 tree prev = clauses;
31689
31690 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31691 cp_lexer_consume_token (parser->lexer);
31692
31693 token = cp_lexer_peek_token (parser->lexer);
31694 c_kind = cp_parser_omp_clause_name (parser);
31695
31696 switch (c_kind)
31697 {
31698 case PRAGMA_OMP_CLAUSE_COLLAPSE:
31699 clauses = cp_parser_omp_clause_collapse (parser, clauses,
31700 token->location);
31701 c_name = "collapse";
31702 break;
31703 case PRAGMA_OMP_CLAUSE_COPYIN:
31704 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
31705 c_name = "copyin";
31706 break;
31707 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
31708 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
31709 clauses);
31710 c_name = "copyprivate";
31711 break;
31712 case PRAGMA_OMP_CLAUSE_DEFAULT:
31713 clauses = cp_parser_omp_clause_default (parser, clauses,
31714 token->location, false);
31715 c_name = "default";
31716 break;
31717 case PRAGMA_OMP_CLAUSE_FINAL:
31718 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
31719 c_name = "final";
31720 break;
31721 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
31722 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
31723 clauses);
31724 c_name = "firstprivate";
31725 break;
31726 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
31727 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
31728 token->location);
31729 c_name = "grainsize";
31730 break;
31731 case PRAGMA_OMP_CLAUSE_HINT:
31732 clauses = cp_parser_omp_clause_hint (parser, clauses,
31733 token->location);
31734 c_name = "hint";
31735 break;
31736 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
31737 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
31738 token->location);
31739 c_name = "defaultmap";
31740 break;
31741 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
31742 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
31743 clauses);
31744 c_name = "use_device_ptr";
31745 break;
31746 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
31747 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
31748 clauses);
31749 c_name = "is_device_ptr";
31750 break;
31751 case PRAGMA_OMP_CLAUSE_IF:
31752 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
31753 true);
31754 c_name = "if";
31755 break;
31756 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
31757 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
31758 clauses);
31759 c_name = "lastprivate";
31760 break;
31761 case PRAGMA_OMP_CLAUSE_MERGEABLE:
31762 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
31763 token->location);
31764 c_name = "mergeable";
31765 break;
31766 case PRAGMA_OMP_CLAUSE_NOWAIT:
31767 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
31768 c_name = "nowait";
31769 break;
31770 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
31771 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
31772 token->location);
31773 c_name = "num_tasks";
31774 break;
31775 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
31776 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
31777 token->location);
31778 c_name = "num_threads";
31779 break;
31780 case PRAGMA_OMP_CLAUSE_ORDERED:
31781 clauses = cp_parser_omp_clause_ordered (parser, clauses,
31782 token->location);
31783 c_name = "ordered";
31784 break;
31785 case PRAGMA_OMP_CLAUSE_PRIORITY:
31786 clauses = cp_parser_omp_clause_priority (parser, clauses,
31787 token->location);
31788 c_name = "priority";
31789 break;
31790 case PRAGMA_OMP_CLAUSE_PRIVATE:
31791 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
31792 clauses);
31793 c_name = "private";
31794 break;
31795 case PRAGMA_OMP_CLAUSE_REDUCTION:
31796 clauses = cp_parser_omp_clause_reduction (parser, clauses);
31797 c_name = "reduction";
31798 break;
31799 case PRAGMA_OMP_CLAUSE_SCHEDULE:
31800 clauses = cp_parser_omp_clause_schedule (parser, clauses,
31801 token->location);
31802 c_name = "schedule";
31803 break;
31804 case PRAGMA_OMP_CLAUSE_SHARED:
31805 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
31806 clauses);
31807 c_name = "shared";
31808 break;
31809 case PRAGMA_OMP_CLAUSE_UNTIED:
31810 clauses = cp_parser_omp_clause_untied (parser, clauses,
31811 token->location);
31812 c_name = "untied";
31813 break;
31814 case PRAGMA_OMP_CLAUSE_INBRANCH:
31815 case PRAGMA_CILK_CLAUSE_MASK:
31816 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
31817 clauses, token->location);
31818 c_name = "inbranch";
31819 break;
31820 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
31821 case PRAGMA_CILK_CLAUSE_NOMASK:
31822 clauses = cp_parser_omp_clause_branch (parser,
31823 OMP_CLAUSE_NOTINBRANCH,
31824 clauses, token->location);
31825 c_name = "notinbranch";
31826 break;
31827 case PRAGMA_OMP_CLAUSE_PARALLEL:
31828 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
31829 clauses, token->location);
31830 c_name = "parallel";
31831 if (!first)
31832 {
31833 clause_not_first:
31834 error_at (token->location, "%qs must be the first clause of %qs",
31835 c_name, where);
31836 clauses = prev;
31837 }
31838 break;
31839 case PRAGMA_OMP_CLAUSE_FOR:
31840 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
31841 clauses, token->location);
31842 c_name = "for";
31843 if (!first)
31844 goto clause_not_first;
31845 break;
31846 case PRAGMA_OMP_CLAUSE_SECTIONS:
31847 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
31848 clauses, token->location);
31849 c_name = "sections";
31850 if (!first)
31851 goto clause_not_first;
31852 break;
31853 case PRAGMA_OMP_CLAUSE_TASKGROUP:
31854 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
31855 clauses, token->location);
31856 c_name = "taskgroup";
31857 if (!first)
31858 goto clause_not_first;
31859 break;
31860 case PRAGMA_OMP_CLAUSE_LINK:
31861 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
31862 c_name = "to";
31863 break;
31864 case PRAGMA_OMP_CLAUSE_TO:
31865 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
31866 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
31867 clauses);
31868 else
31869 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
31870 c_name = "to";
31871 break;
31872 case PRAGMA_OMP_CLAUSE_FROM:
31873 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
31874 c_name = "from";
31875 break;
31876 case PRAGMA_OMP_CLAUSE_UNIFORM:
31877 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
31878 clauses);
31879 c_name = "uniform";
31880 break;
31881 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
31882 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
31883 token->location);
31884 c_name = "num_teams";
31885 break;
31886 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
31887 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
31888 token->location);
31889 c_name = "thread_limit";
31890 break;
31891 case PRAGMA_OMP_CLAUSE_ALIGNED:
31892 clauses = cp_parser_omp_clause_aligned (parser, clauses);
31893 c_name = "aligned";
31894 break;
31895 case PRAGMA_OMP_CLAUSE_LINEAR:
31896 {
31897 bool cilk_simd_fn = false, declare_simd = false;
31898 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
31899 cilk_simd_fn = true;
31900 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
31901 declare_simd = true;
31902 clauses = cp_parser_omp_clause_linear (parser, clauses,
31903 cilk_simd_fn, declare_simd);
31904 }
31905 c_name = "linear";
31906 break;
31907 case PRAGMA_OMP_CLAUSE_DEPEND:
31908 clauses = cp_parser_omp_clause_depend (parser, clauses,
31909 token->location);
31910 c_name = "depend";
31911 break;
31912 case PRAGMA_OMP_CLAUSE_MAP:
31913 clauses = cp_parser_omp_clause_map (parser, clauses);
31914 c_name = "map";
31915 break;
31916 case PRAGMA_OMP_CLAUSE_DEVICE:
31917 clauses = cp_parser_omp_clause_device (parser, clauses,
31918 token->location);
31919 c_name = "device";
31920 break;
31921 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
31922 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
31923 token->location);
31924 c_name = "dist_schedule";
31925 break;
31926 case PRAGMA_OMP_CLAUSE_PROC_BIND:
31927 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
31928 token->location);
31929 c_name = "proc_bind";
31930 break;
31931 case PRAGMA_OMP_CLAUSE_SAFELEN:
31932 clauses = cp_parser_omp_clause_safelen (parser, clauses,
31933 token->location);
31934 c_name = "safelen";
31935 break;
31936 case PRAGMA_OMP_CLAUSE_SIMDLEN:
31937 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
31938 token->location);
31939 c_name = "simdlen";
31940 break;
31941 case PRAGMA_OMP_CLAUSE_NOGROUP:
31942 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
31943 token->location);
31944 c_name = "nogroup";
31945 break;
31946 case PRAGMA_OMP_CLAUSE_THREADS:
31947 clauses
31948 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
31949 clauses, token->location);
31950 c_name = "threads";
31951 break;
31952 case PRAGMA_OMP_CLAUSE_SIMD:
31953 clauses
31954 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
31955 clauses, token->location);
31956 c_name = "simd";
31957 break;
31958 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
31959 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
31960 c_name = "simdlen";
31961 break;
31962 default:
31963 cp_parser_error (parser, "expected %<#pragma omp%> clause");
31964 goto saw_error;
31965 }
31966
31967 first = false;
31968
31969 if (((mask >> c_kind) & 1) == 0)
31970 {
31971 /* Remove the invalid clause(s) from the list to avoid
31972 confusing the rest of the compiler. */
31973 clauses = prev;
31974 error_at (token->location, "%qs is not valid for %qs", c_name, where);
31975 }
31976 }
31977 saw_error:
31978 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
31979 no reason to skip to the end. */
31980 if (!(flag_cilkplus && pragma_tok == NULL))
31981 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31982 if (finish_p)
31983 {
31984 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
31985 return finish_omp_clauses (clauses, false, true);
31986 else
31987 return finish_omp_clauses (clauses, true);
31988 }
31989 return clauses;
31990 }
31991
31992 /* OpenMP 2.5:
31993 structured-block:
31994 statement
31995
31996 In practice, we're also interested in adding the statement to an
31997 outer node. So it is convenient if we work around the fact that
31998 cp_parser_statement calls add_stmt. */
31999
32000 static unsigned
32001 cp_parser_begin_omp_structured_block (cp_parser *parser)
32002 {
32003 unsigned save = parser->in_statement;
32004
32005 /* Only move the values to IN_OMP_BLOCK if they weren't false.
32006 This preserves the "not within loop or switch" style error messages
32007 for nonsense cases like
32008 void foo() {
32009 #pragma omp single
32010 break;
32011 }
32012 */
32013 if (parser->in_statement)
32014 parser->in_statement = IN_OMP_BLOCK;
32015
32016 return save;
32017 }
32018
32019 static void
32020 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
32021 {
32022 parser->in_statement = save;
32023 }
32024
32025 static tree
32026 cp_parser_omp_structured_block (cp_parser *parser)
32027 {
32028 tree stmt = begin_omp_structured_block ();
32029 unsigned int save = cp_parser_begin_omp_structured_block (parser);
32030
32031 cp_parser_statement (parser, NULL_TREE, false, NULL);
32032
32033 cp_parser_end_omp_structured_block (parser, save);
32034 return finish_omp_structured_block (stmt);
32035 }
32036
32037 /* OpenMP 2.5:
32038 # pragma omp atomic new-line
32039 expression-stmt
32040
32041 expression-stmt:
32042 x binop= expr | x++ | ++x | x-- | --x
32043 binop:
32044 +, *, -, /, &, ^, |, <<, >>
32045
32046 where x is an lvalue expression with scalar type.
32047
32048 OpenMP 3.1:
32049 # pragma omp atomic new-line
32050 update-stmt
32051
32052 # pragma omp atomic read new-line
32053 read-stmt
32054
32055 # pragma omp atomic write new-line
32056 write-stmt
32057
32058 # pragma omp atomic update new-line
32059 update-stmt
32060
32061 # pragma omp atomic capture new-line
32062 capture-stmt
32063
32064 # pragma omp atomic capture new-line
32065 capture-block
32066
32067 read-stmt:
32068 v = x
32069 write-stmt:
32070 x = expr
32071 update-stmt:
32072 expression-stmt | x = x binop expr
32073 capture-stmt:
32074 v = expression-stmt
32075 capture-block:
32076 { v = x; update-stmt; } | { update-stmt; v = x; }
32077
32078 OpenMP 4.0:
32079 update-stmt:
32080 expression-stmt | x = x binop expr | x = expr binop x
32081 capture-stmt:
32082 v = update-stmt
32083 capture-block:
32084 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
32085
32086 where x and v are lvalue expressions with scalar type. */
32087
32088 static void
32089 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
32090 {
32091 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
32092 tree rhs1 = NULL_TREE, orig_lhs;
32093 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
32094 bool structured_block = false;
32095 bool seq_cst = false;
32096
32097 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32098 {
32099 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32100 const char *p = IDENTIFIER_POINTER (id);
32101
32102 if (!strcmp (p, "seq_cst"))
32103 {
32104 seq_cst = true;
32105 cp_lexer_consume_token (parser->lexer);
32106 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32107 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32108 cp_lexer_consume_token (parser->lexer);
32109 }
32110 }
32111 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32112 {
32113 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32114 const char *p = IDENTIFIER_POINTER (id);
32115
32116 if (!strcmp (p, "read"))
32117 code = OMP_ATOMIC_READ;
32118 else if (!strcmp (p, "write"))
32119 code = NOP_EXPR;
32120 else if (!strcmp (p, "update"))
32121 code = OMP_ATOMIC;
32122 else if (!strcmp (p, "capture"))
32123 code = OMP_ATOMIC_CAPTURE_NEW;
32124 else
32125 p = NULL;
32126 if (p)
32127 cp_lexer_consume_token (parser->lexer);
32128 }
32129 if (!seq_cst)
32130 {
32131 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32132 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32133 cp_lexer_consume_token (parser->lexer);
32134
32135 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32136 {
32137 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32138 const char *p = IDENTIFIER_POINTER (id);
32139
32140 if (!strcmp (p, "seq_cst"))
32141 {
32142 seq_cst = true;
32143 cp_lexer_consume_token (parser->lexer);
32144 }
32145 }
32146 }
32147 cp_parser_require_pragma_eol (parser, pragma_tok);
32148
32149 switch (code)
32150 {
32151 case OMP_ATOMIC_READ:
32152 case NOP_EXPR: /* atomic write */
32153 v = cp_parser_unary_expression (parser);
32154 if (v == error_mark_node)
32155 goto saw_error;
32156 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32157 goto saw_error;
32158 if (code == NOP_EXPR)
32159 lhs = cp_parser_expression (parser);
32160 else
32161 lhs = cp_parser_unary_expression (parser);
32162 if (lhs == error_mark_node)
32163 goto saw_error;
32164 if (code == NOP_EXPR)
32165 {
32166 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
32167 opcode. */
32168 code = OMP_ATOMIC;
32169 rhs = lhs;
32170 lhs = v;
32171 v = NULL_TREE;
32172 }
32173 goto done;
32174 case OMP_ATOMIC_CAPTURE_NEW:
32175 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32176 {
32177 cp_lexer_consume_token (parser->lexer);
32178 structured_block = true;
32179 }
32180 else
32181 {
32182 v = cp_parser_unary_expression (parser);
32183 if (v == error_mark_node)
32184 goto saw_error;
32185 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32186 goto saw_error;
32187 }
32188 default:
32189 break;
32190 }
32191
32192 restart:
32193 lhs = cp_parser_unary_expression (parser);
32194 orig_lhs = lhs;
32195 switch (TREE_CODE (lhs))
32196 {
32197 case ERROR_MARK:
32198 goto saw_error;
32199
32200 case POSTINCREMENT_EXPR:
32201 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32202 code = OMP_ATOMIC_CAPTURE_OLD;
32203 /* FALLTHROUGH */
32204 case PREINCREMENT_EXPR:
32205 lhs = TREE_OPERAND (lhs, 0);
32206 opcode = PLUS_EXPR;
32207 rhs = integer_one_node;
32208 break;
32209
32210 case POSTDECREMENT_EXPR:
32211 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32212 code = OMP_ATOMIC_CAPTURE_OLD;
32213 /* FALLTHROUGH */
32214 case PREDECREMENT_EXPR:
32215 lhs = TREE_OPERAND (lhs, 0);
32216 opcode = MINUS_EXPR;
32217 rhs = integer_one_node;
32218 break;
32219
32220 case COMPOUND_EXPR:
32221 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
32222 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
32223 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
32224 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
32225 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
32226 (TREE_OPERAND (lhs, 1), 0), 0)))
32227 == BOOLEAN_TYPE)
32228 /* Undo effects of boolean_increment for post {in,de}crement. */
32229 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
32230 /* FALLTHRU */
32231 case MODIFY_EXPR:
32232 if (TREE_CODE (lhs) == MODIFY_EXPR
32233 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
32234 {
32235 /* Undo effects of boolean_increment. */
32236 if (integer_onep (TREE_OPERAND (lhs, 1)))
32237 {
32238 /* This is pre or post increment. */
32239 rhs = TREE_OPERAND (lhs, 1);
32240 lhs = TREE_OPERAND (lhs, 0);
32241 opcode = NOP_EXPR;
32242 if (code == OMP_ATOMIC_CAPTURE_NEW
32243 && !structured_block
32244 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
32245 code = OMP_ATOMIC_CAPTURE_OLD;
32246 break;
32247 }
32248 }
32249 /* FALLTHRU */
32250 default:
32251 switch (cp_lexer_peek_token (parser->lexer)->type)
32252 {
32253 case CPP_MULT_EQ:
32254 opcode = MULT_EXPR;
32255 break;
32256 case CPP_DIV_EQ:
32257 opcode = TRUNC_DIV_EXPR;
32258 break;
32259 case CPP_PLUS_EQ:
32260 opcode = PLUS_EXPR;
32261 break;
32262 case CPP_MINUS_EQ:
32263 opcode = MINUS_EXPR;
32264 break;
32265 case CPP_LSHIFT_EQ:
32266 opcode = LSHIFT_EXPR;
32267 break;
32268 case CPP_RSHIFT_EQ:
32269 opcode = RSHIFT_EXPR;
32270 break;
32271 case CPP_AND_EQ:
32272 opcode = BIT_AND_EXPR;
32273 break;
32274 case CPP_OR_EQ:
32275 opcode = BIT_IOR_EXPR;
32276 break;
32277 case CPP_XOR_EQ:
32278 opcode = BIT_XOR_EXPR;
32279 break;
32280 case CPP_EQ:
32281 enum cp_parser_prec oprec;
32282 cp_token *token;
32283 cp_lexer_consume_token (parser->lexer);
32284 cp_parser_parse_tentatively (parser);
32285 rhs1 = cp_parser_simple_cast_expression (parser);
32286 if (rhs1 == error_mark_node)
32287 {
32288 cp_parser_abort_tentative_parse (parser);
32289 cp_parser_simple_cast_expression (parser);
32290 goto saw_error;
32291 }
32292 token = cp_lexer_peek_token (parser->lexer);
32293 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
32294 {
32295 cp_parser_abort_tentative_parse (parser);
32296 cp_parser_parse_tentatively (parser);
32297 rhs = cp_parser_binary_expression (parser, false, true,
32298 PREC_NOT_OPERATOR, NULL);
32299 if (rhs == error_mark_node)
32300 {
32301 cp_parser_abort_tentative_parse (parser);
32302 cp_parser_binary_expression (parser, false, true,
32303 PREC_NOT_OPERATOR, NULL);
32304 goto saw_error;
32305 }
32306 switch (TREE_CODE (rhs))
32307 {
32308 case MULT_EXPR:
32309 case TRUNC_DIV_EXPR:
32310 case RDIV_EXPR:
32311 case PLUS_EXPR:
32312 case MINUS_EXPR:
32313 case LSHIFT_EXPR:
32314 case RSHIFT_EXPR:
32315 case BIT_AND_EXPR:
32316 case BIT_IOR_EXPR:
32317 case BIT_XOR_EXPR:
32318 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
32319 {
32320 if (cp_parser_parse_definitely (parser))
32321 {
32322 opcode = TREE_CODE (rhs);
32323 rhs1 = TREE_OPERAND (rhs, 0);
32324 rhs = TREE_OPERAND (rhs, 1);
32325 goto stmt_done;
32326 }
32327 else
32328 goto saw_error;
32329 }
32330 break;
32331 default:
32332 break;
32333 }
32334 cp_parser_abort_tentative_parse (parser);
32335 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
32336 {
32337 rhs = cp_parser_expression (parser);
32338 if (rhs == error_mark_node)
32339 goto saw_error;
32340 opcode = NOP_EXPR;
32341 rhs1 = NULL_TREE;
32342 goto stmt_done;
32343 }
32344 cp_parser_error (parser,
32345 "invalid form of %<#pragma omp atomic%>");
32346 goto saw_error;
32347 }
32348 if (!cp_parser_parse_definitely (parser))
32349 goto saw_error;
32350 switch (token->type)
32351 {
32352 case CPP_SEMICOLON:
32353 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
32354 {
32355 code = OMP_ATOMIC_CAPTURE_OLD;
32356 v = lhs;
32357 lhs = NULL_TREE;
32358 lhs1 = rhs1;
32359 rhs1 = NULL_TREE;
32360 cp_lexer_consume_token (parser->lexer);
32361 goto restart;
32362 }
32363 else if (structured_block)
32364 {
32365 opcode = NOP_EXPR;
32366 rhs = rhs1;
32367 rhs1 = NULL_TREE;
32368 goto stmt_done;
32369 }
32370 cp_parser_error (parser,
32371 "invalid form of %<#pragma omp atomic%>");
32372 goto saw_error;
32373 case CPP_MULT:
32374 opcode = MULT_EXPR;
32375 break;
32376 case CPP_DIV:
32377 opcode = TRUNC_DIV_EXPR;
32378 break;
32379 case CPP_PLUS:
32380 opcode = PLUS_EXPR;
32381 break;
32382 case CPP_MINUS:
32383 opcode = MINUS_EXPR;
32384 break;
32385 case CPP_LSHIFT:
32386 opcode = LSHIFT_EXPR;
32387 break;
32388 case CPP_RSHIFT:
32389 opcode = RSHIFT_EXPR;
32390 break;
32391 case CPP_AND:
32392 opcode = BIT_AND_EXPR;
32393 break;
32394 case CPP_OR:
32395 opcode = BIT_IOR_EXPR;
32396 break;
32397 case CPP_XOR:
32398 opcode = BIT_XOR_EXPR;
32399 break;
32400 default:
32401 cp_parser_error (parser,
32402 "invalid operator for %<#pragma omp atomic%>");
32403 goto saw_error;
32404 }
32405 oprec = TOKEN_PRECEDENCE (token);
32406 gcc_assert (oprec != PREC_NOT_OPERATOR);
32407 if (commutative_tree_code (opcode))
32408 oprec = (enum cp_parser_prec) (oprec - 1);
32409 cp_lexer_consume_token (parser->lexer);
32410 rhs = cp_parser_binary_expression (parser, false, false,
32411 oprec, NULL);
32412 if (rhs == error_mark_node)
32413 goto saw_error;
32414 goto stmt_done;
32415 /* FALLTHROUGH */
32416 default:
32417 cp_parser_error (parser,
32418 "invalid operator for %<#pragma omp atomic%>");
32419 goto saw_error;
32420 }
32421 cp_lexer_consume_token (parser->lexer);
32422
32423 rhs = cp_parser_expression (parser);
32424 if (rhs == error_mark_node)
32425 goto saw_error;
32426 break;
32427 }
32428 stmt_done:
32429 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
32430 {
32431 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
32432 goto saw_error;
32433 v = cp_parser_unary_expression (parser);
32434 if (v == error_mark_node)
32435 goto saw_error;
32436 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32437 goto saw_error;
32438 lhs1 = cp_parser_unary_expression (parser);
32439 if (lhs1 == error_mark_node)
32440 goto saw_error;
32441 }
32442 if (structured_block)
32443 {
32444 cp_parser_consume_semicolon_at_end_of_statement (parser);
32445 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
32446 }
32447 done:
32448 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
32449 if (!structured_block)
32450 cp_parser_consume_semicolon_at_end_of_statement (parser);
32451 return;
32452
32453 saw_error:
32454 cp_parser_skip_to_end_of_block_or_statement (parser);
32455 if (structured_block)
32456 {
32457 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
32458 cp_lexer_consume_token (parser->lexer);
32459 else if (code == OMP_ATOMIC_CAPTURE_NEW)
32460 {
32461 cp_parser_skip_to_end_of_block_or_statement (parser);
32462 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
32463 cp_lexer_consume_token (parser->lexer);
32464 }
32465 }
32466 }
32467
32468
32469 /* OpenMP 2.5:
32470 # pragma omp barrier new-line */
32471
32472 static void
32473 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
32474 {
32475 cp_parser_require_pragma_eol (parser, pragma_tok);
32476 finish_omp_barrier ();
32477 }
32478
32479 /* OpenMP 2.5:
32480 # pragma omp critical [(name)] new-line
32481 structured-block
32482
32483 OpenMP 4.5:
32484 # pragma omp critical [(name) [hint(expression)]] new-line
32485 structured-block */
32486
32487 #define OMP_CRITICAL_CLAUSE_MASK \
32488 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
32489
32490 static tree
32491 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
32492 {
32493 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
32494
32495 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32496 {
32497 cp_lexer_consume_token (parser->lexer);
32498
32499 name = cp_parser_identifier (parser);
32500
32501 if (name == error_mark_node
32502 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32503 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32504 /*or_comma=*/false,
32505 /*consume_paren=*/true);
32506 if (name == error_mark_node)
32507 name = NULL;
32508
32509 clauses = cp_parser_omp_all_clauses (parser,
32510 OMP_CRITICAL_CLAUSE_MASK,
32511 "#pragma omp critical", pragma_tok);
32512 }
32513 else
32514 cp_parser_require_pragma_eol (parser, pragma_tok);
32515
32516 stmt = cp_parser_omp_structured_block (parser);
32517 return c_finish_omp_critical (input_location, stmt, name, clauses);
32518 }
32519
32520 /* OpenMP 2.5:
32521 # pragma omp flush flush-vars[opt] new-line
32522
32523 flush-vars:
32524 ( variable-list ) */
32525
32526 static void
32527 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
32528 {
32529 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32530 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32531 cp_parser_require_pragma_eol (parser, pragma_tok);
32532
32533 finish_omp_flush ();
32534 }
32535
32536 /* Helper function, to parse omp for increment expression. */
32537
32538 static tree
32539 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
32540 {
32541 tree cond = cp_parser_binary_expression (parser, false, true,
32542 PREC_NOT_OPERATOR, NULL);
32543 if (cond == error_mark_node
32544 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
32545 {
32546 cp_parser_skip_to_end_of_statement (parser);
32547 return error_mark_node;
32548 }
32549
32550 switch (TREE_CODE (cond))
32551 {
32552 case GT_EXPR:
32553 case GE_EXPR:
32554 case LT_EXPR:
32555 case LE_EXPR:
32556 break;
32557 case NE_EXPR:
32558 if (code == CILK_SIMD || code == CILK_FOR)
32559 break;
32560 /* Fall through: OpenMP disallows NE_EXPR. */
32561 default:
32562 return error_mark_node;
32563 }
32564
32565 /* If decl is an iterator, preserve LHS and RHS of the relational
32566 expr until finish_omp_for. */
32567 if (decl
32568 && (type_dependent_expression_p (decl)
32569 || CLASS_TYPE_P (TREE_TYPE (decl))))
32570 return cond;
32571
32572 return build_x_binary_op (input_location, TREE_CODE (cond),
32573 TREE_OPERAND (cond, 0), ERROR_MARK,
32574 TREE_OPERAND (cond, 1), ERROR_MARK,
32575 /*overload=*/NULL, tf_warning_or_error);
32576 }
32577
32578 /* Helper function, to parse omp for increment expression. */
32579
32580 static tree
32581 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
32582 {
32583 cp_token *token = cp_lexer_peek_token (parser->lexer);
32584 enum tree_code op;
32585 tree lhs, rhs;
32586 cp_id_kind idk;
32587 bool decl_first;
32588
32589 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
32590 {
32591 op = (token->type == CPP_PLUS_PLUS
32592 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
32593 cp_lexer_consume_token (parser->lexer);
32594 lhs = cp_parser_simple_cast_expression (parser);
32595 if (lhs != decl
32596 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
32597 return error_mark_node;
32598 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
32599 }
32600
32601 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
32602 if (lhs != decl
32603 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
32604 return error_mark_node;
32605
32606 token = cp_lexer_peek_token (parser->lexer);
32607 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
32608 {
32609 op = (token->type == CPP_PLUS_PLUS
32610 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
32611 cp_lexer_consume_token (parser->lexer);
32612 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
32613 }
32614
32615 op = cp_parser_assignment_operator_opt (parser);
32616 if (op == ERROR_MARK)
32617 return error_mark_node;
32618
32619 if (op != NOP_EXPR)
32620 {
32621 rhs = cp_parser_assignment_expression (parser);
32622 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
32623 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
32624 }
32625
32626 lhs = cp_parser_binary_expression (parser, false, false,
32627 PREC_ADDITIVE_EXPRESSION, NULL);
32628 token = cp_lexer_peek_token (parser->lexer);
32629 decl_first = (lhs == decl
32630 || (processing_template_decl && cp_tree_equal (lhs, decl)));
32631 if (decl_first)
32632 lhs = NULL_TREE;
32633 if (token->type != CPP_PLUS
32634 && token->type != CPP_MINUS)
32635 return error_mark_node;
32636
32637 do
32638 {
32639 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
32640 cp_lexer_consume_token (parser->lexer);
32641 rhs = cp_parser_binary_expression (parser, false, false,
32642 PREC_ADDITIVE_EXPRESSION, NULL);
32643 token = cp_lexer_peek_token (parser->lexer);
32644 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
32645 {
32646 if (lhs == NULL_TREE)
32647 {
32648 if (op == PLUS_EXPR)
32649 lhs = rhs;
32650 else
32651 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
32652 tf_warning_or_error);
32653 }
32654 else
32655 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
32656 ERROR_MARK, NULL, tf_warning_or_error);
32657 }
32658 }
32659 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
32660
32661 if (!decl_first)
32662 {
32663 if ((rhs != decl
32664 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
32665 || op == MINUS_EXPR)
32666 return error_mark_node;
32667 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
32668 }
32669 else
32670 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
32671
32672 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
32673 }
32674
32675 /* Parse the initialization statement of either an OpenMP for loop or
32676 a Cilk Plus for loop.
32677
32678 Return true if the resulting construct should have an
32679 OMP_CLAUSE_PRIVATE added to it. */
32680
32681 static tree
32682 cp_parser_omp_for_loop_init (cp_parser *parser,
32683 enum tree_code code,
32684 tree &this_pre_body,
32685 vec<tree, va_gc> *for_block,
32686 tree &init,
32687 tree &orig_init,
32688 tree &decl,
32689 tree &real_decl)
32690 {
32691 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
32692 return NULL_TREE;
32693
32694 tree add_private_clause = NULL_TREE;
32695
32696 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
32697
32698 init-expr:
32699 var = lb
32700 integer-type var = lb
32701 random-access-iterator-type var = lb
32702 pointer-type var = lb
32703 */
32704 cp_decl_specifier_seq type_specifiers;
32705
32706 /* First, try to parse as an initialized declaration. See
32707 cp_parser_condition, from whence the bulk of this is copied. */
32708
32709 cp_parser_parse_tentatively (parser);
32710 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
32711 /*is_trailing_return=*/false,
32712 &type_specifiers);
32713 if (cp_parser_parse_definitely (parser))
32714 {
32715 /* If parsing a type specifier seq succeeded, then this
32716 MUST be a initialized declaration. */
32717 tree asm_specification, attributes;
32718 cp_declarator *declarator;
32719
32720 declarator = cp_parser_declarator (parser,
32721 CP_PARSER_DECLARATOR_NAMED,
32722 /*ctor_dtor_or_conv_p=*/NULL,
32723 /*parenthesized_p=*/NULL,
32724 /*member_p=*/false,
32725 /*friend_p=*/false);
32726 attributes = cp_parser_attributes_opt (parser);
32727 asm_specification = cp_parser_asm_specification_opt (parser);
32728
32729 if (declarator == cp_error_declarator)
32730 cp_parser_skip_to_end_of_statement (parser);
32731
32732 else
32733 {
32734 tree pushed_scope, auto_node;
32735
32736 decl = start_decl (declarator, &type_specifiers,
32737 SD_INITIALIZED, attributes,
32738 /*prefix_attributes=*/NULL_TREE,
32739 &pushed_scope);
32740
32741 auto_node = type_uses_auto (TREE_TYPE (decl));
32742 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
32743 {
32744 if (cp_lexer_next_token_is (parser->lexer,
32745 CPP_OPEN_PAREN))
32746 {
32747 if (code != CILK_SIMD && code != CILK_FOR)
32748 error ("parenthesized initialization is not allowed in "
32749 "OpenMP %<for%> loop");
32750 else
32751 error ("parenthesized initialization is "
32752 "not allowed in for-loop");
32753 }
32754 else
32755 /* Trigger an error. */
32756 cp_parser_require (parser, CPP_EQ, RT_EQ);
32757
32758 init = error_mark_node;
32759 cp_parser_skip_to_end_of_statement (parser);
32760 }
32761 else if (CLASS_TYPE_P (TREE_TYPE (decl))
32762 || type_dependent_expression_p (decl)
32763 || auto_node)
32764 {
32765 bool is_direct_init, is_non_constant_init;
32766
32767 init = cp_parser_initializer (parser,
32768 &is_direct_init,
32769 &is_non_constant_init);
32770
32771 if (auto_node)
32772 {
32773 TREE_TYPE (decl)
32774 = do_auto_deduction (TREE_TYPE (decl), init,
32775 auto_node);
32776
32777 if (!CLASS_TYPE_P (TREE_TYPE (decl))
32778 && !type_dependent_expression_p (decl))
32779 goto non_class;
32780 }
32781
32782 cp_finish_decl (decl, init, !is_non_constant_init,
32783 asm_specification,
32784 LOOKUP_ONLYCONVERTING);
32785 orig_init = init;
32786 if (CLASS_TYPE_P (TREE_TYPE (decl)))
32787 {
32788 vec_safe_push (for_block, this_pre_body);
32789 init = NULL_TREE;
32790 }
32791 else
32792 init = pop_stmt_list (this_pre_body);
32793 this_pre_body = NULL_TREE;
32794 }
32795 else
32796 {
32797 /* Consume '='. */
32798 cp_lexer_consume_token (parser->lexer);
32799 init = cp_parser_assignment_expression (parser);
32800
32801 non_class:
32802 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
32803 init = error_mark_node;
32804 else
32805 cp_finish_decl (decl, NULL_TREE,
32806 /*init_const_expr_p=*/false,
32807 asm_specification,
32808 LOOKUP_ONLYCONVERTING);
32809 }
32810
32811 if (pushed_scope)
32812 pop_scope (pushed_scope);
32813 }
32814 }
32815 else
32816 {
32817 cp_id_kind idk;
32818 /* If parsing a type specifier sequence failed, then
32819 this MUST be a simple expression. */
32820 if (code == CILK_FOR)
32821 error ("%<_Cilk_for%> allows expression instead of declaration only "
32822 "in C, not in C++");
32823 cp_parser_parse_tentatively (parser);
32824 decl = cp_parser_primary_expression (parser, false, false,
32825 false, &idk);
32826 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
32827 if (!cp_parser_error_occurred (parser)
32828 && decl
32829 && (TREE_CODE (decl) == COMPONENT_REF
32830 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
32831 {
32832 cp_parser_abort_tentative_parse (parser);
32833 cp_parser_parse_tentatively (parser);
32834 cp_token *token = cp_lexer_peek_token (parser->lexer);
32835 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
32836 /*check_dependency_p=*/true,
32837 /*template_p=*/NULL,
32838 /*declarator_p=*/false,
32839 /*optional_p=*/false);
32840 if (name != error_mark_node
32841 && last_tok == cp_lexer_peek_token (parser->lexer))
32842 {
32843 decl = cp_parser_lookup_name_simple (parser, name,
32844 token->location);
32845 if (TREE_CODE (decl) == FIELD_DECL)
32846 add_private_clause = omp_privatize_field (decl, false);
32847 }
32848 cp_parser_abort_tentative_parse (parser);
32849 cp_parser_parse_tentatively (parser);
32850 decl = cp_parser_primary_expression (parser, false, false,
32851 false, &idk);
32852 }
32853 if (!cp_parser_error_occurred (parser)
32854 && decl
32855 && DECL_P (decl)
32856 && CLASS_TYPE_P (TREE_TYPE (decl)))
32857 {
32858 tree rhs;
32859
32860 cp_parser_parse_definitely (parser);
32861 cp_parser_require (parser, CPP_EQ, RT_EQ);
32862 rhs = cp_parser_assignment_expression (parser);
32863 orig_init = rhs;
32864 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
32865 decl, NOP_EXPR,
32866 rhs,
32867 tf_warning_or_error));
32868 if (!add_private_clause)
32869 add_private_clause = decl;
32870 }
32871 else
32872 {
32873 decl = NULL;
32874 cp_parser_abort_tentative_parse (parser);
32875 init = cp_parser_expression (parser);
32876 if (init)
32877 {
32878 if (TREE_CODE (init) == MODIFY_EXPR
32879 || TREE_CODE (init) == MODOP_EXPR)
32880 real_decl = TREE_OPERAND (init, 0);
32881 }
32882 }
32883 }
32884 return add_private_clause;
32885 }
32886
32887 /* Parse the restricted form of the for statement allowed by OpenMP. */
32888
32889 static tree
32890 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
32891 tree *cclauses)
32892 {
32893 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
32894 tree real_decl, initv, condv, incrv, declv;
32895 tree this_pre_body, cl, ordered_cl = NULL_TREE;
32896 location_t loc_first;
32897 bool collapse_err = false;
32898 int i, collapse = 1, ordered = 0, count, nbraces = 0;
32899 vec<tree, va_gc> *for_block = make_tree_vector ();
32900 auto_vec<tree, 4> orig_inits;
32901
32902 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
32903 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
32904 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
32905 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
32906 && OMP_CLAUSE_ORDERED_EXPR (cl))
32907 {
32908 ordered_cl = cl;
32909 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
32910 }
32911
32912 if (ordered && ordered < collapse)
32913 {
32914 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
32915 "%<ordered%> clause parameter is less than %<collapse%>");
32916 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
32917 = build_int_cst (NULL_TREE, collapse);
32918 ordered = collapse;
32919 }
32920 if (ordered)
32921 {
32922 for (tree *pc = &clauses; *pc; )
32923 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
32924 {
32925 error_at (OMP_CLAUSE_LOCATION (*pc),
32926 "%<linear%> clause may not be specified together "
32927 "with %<ordered%> clause with a parameter");
32928 *pc = OMP_CLAUSE_CHAIN (*pc);
32929 }
32930 else
32931 pc = &OMP_CLAUSE_CHAIN (*pc);
32932 }
32933
32934 gcc_assert (collapse >= 1 && ordered >= 0);
32935 count = ordered ? ordered : collapse;
32936
32937 declv = make_tree_vec (count);
32938 initv = make_tree_vec (count);
32939 condv = make_tree_vec (count);
32940 incrv = make_tree_vec (count);
32941
32942 loc_first = cp_lexer_peek_token (parser->lexer)->location;
32943
32944 for (i = 0; i < count; i++)
32945 {
32946 int bracecount = 0;
32947 tree add_private_clause = NULL_TREE;
32948 location_t loc;
32949
32950 if (code != CILK_FOR
32951 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
32952 {
32953 cp_parser_error (parser, "for statement expected");
32954 return NULL;
32955 }
32956 if (code == CILK_FOR
32957 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
32958 {
32959 cp_parser_error (parser, "_Cilk_for statement expected");
32960 return NULL;
32961 }
32962 loc = cp_lexer_consume_token (parser->lexer)->location;
32963
32964 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32965 return NULL;
32966
32967 init = orig_init = decl = real_decl = NULL;
32968 this_pre_body = push_stmt_list ();
32969
32970 add_private_clause
32971 = cp_parser_omp_for_loop_init (parser, code,
32972 this_pre_body, for_block,
32973 init, orig_init, decl, real_decl);
32974
32975 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
32976 if (this_pre_body)
32977 {
32978 this_pre_body = pop_stmt_list (this_pre_body);
32979 if (pre_body)
32980 {
32981 tree t = pre_body;
32982 pre_body = push_stmt_list ();
32983 add_stmt (t);
32984 add_stmt (this_pre_body);
32985 pre_body = pop_stmt_list (pre_body);
32986 }
32987 else
32988 pre_body = this_pre_body;
32989 }
32990
32991 if (decl)
32992 real_decl = decl;
32993 if (cclauses != NULL
32994 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
32995 && real_decl != NULL_TREE)
32996 {
32997 tree *c;
32998 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
32999 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
33000 && OMP_CLAUSE_DECL (*c) == real_decl)
33001 {
33002 error_at (loc, "iteration variable %qD"
33003 " should not be firstprivate", real_decl);
33004 *c = OMP_CLAUSE_CHAIN (*c);
33005 }
33006 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
33007 && OMP_CLAUSE_DECL (*c) == real_decl)
33008 {
33009 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
33010 tree l = *c;
33011 *c = OMP_CLAUSE_CHAIN (*c);
33012 if (code == OMP_SIMD)
33013 {
33014 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33015 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
33016 }
33017 else
33018 {
33019 OMP_CLAUSE_CHAIN (l) = clauses;
33020 clauses = l;
33021 }
33022 add_private_clause = NULL_TREE;
33023 }
33024 else
33025 {
33026 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
33027 && OMP_CLAUSE_DECL (*c) == real_decl)
33028 add_private_clause = NULL_TREE;
33029 c = &OMP_CLAUSE_CHAIN (*c);
33030 }
33031 }
33032
33033 if (add_private_clause)
33034 {
33035 tree c;
33036 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
33037 {
33038 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
33039 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
33040 && OMP_CLAUSE_DECL (c) == decl)
33041 break;
33042 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
33043 && OMP_CLAUSE_DECL (c) == decl)
33044 error_at (loc, "iteration variable %qD "
33045 "should not be firstprivate",
33046 decl);
33047 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
33048 && OMP_CLAUSE_DECL (c) == decl)
33049 error_at (loc, "iteration variable %qD should not be reduction",
33050 decl);
33051 }
33052 if (c == NULL)
33053 {
33054 if (code != OMP_SIMD)
33055 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
33056 else if (collapse == 1)
33057 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33058 else
33059 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
33060 OMP_CLAUSE_DECL (c) = add_private_clause;
33061 c = finish_omp_clauses (c, true);
33062 if (c)
33063 {
33064 OMP_CLAUSE_CHAIN (c) = clauses;
33065 clauses = c;
33066 /* For linear, signal that we need to fill up
33067 the so far unknown linear step. */
33068 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
33069 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
33070 }
33071 }
33072 }
33073
33074 cond = NULL;
33075 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33076 cond = cp_parser_omp_for_cond (parser, decl, code);
33077 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33078
33079 incr = NULL;
33080 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
33081 {
33082 /* If decl is an iterator, preserve the operator on decl
33083 until finish_omp_for. */
33084 if (real_decl
33085 && ((processing_template_decl
33086 && (TREE_TYPE (real_decl) == NULL_TREE
33087 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
33088 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
33089 incr = cp_parser_omp_for_incr (parser, real_decl);
33090 else
33091 incr = cp_parser_expression (parser);
33092 if (!EXPR_HAS_LOCATION (incr))
33093 protected_set_expr_location (incr, input_location);
33094 }
33095
33096 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33097 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33098 /*or_comma=*/false,
33099 /*consume_paren=*/true);
33100
33101 TREE_VEC_ELT (declv, i) = decl;
33102 TREE_VEC_ELT (initv, i) = init;
33103 TREE_VEC_ELT (condv, i) = cond;
33104 TREE_VEC_ELT (incrv, i) = incr;
33105 if (orig_init)
33106 {
33107 orig_inits.safe_grow_cleared (i + 1);
33108 orig_inits[i] = orig_init;
33109 }
33110
33111 if (i == count - 1)
33112 break;
33113
33114 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
33115 in between the collapsed for loops to be still considered perfectly
33116 nested. Hopefully the final version clarifies this.
33117 For now handle (multiple) {'s and empty statements. */
33118 cp_parser_parse_tentatively (parser);
33119 do
33120 {
33121 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33122 break;
33123 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33124 {
33125 cp_lexer_consume_token (parser->lexer);
33126 bracecount++;
33127 }
33128 else if (bracecount
33129 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33130 cp_lexer_consume_token (parser->lexer);
33131 else
33132 {
33133 loc = cp_lexer_peek_token (parser->lexer)->location;
33134 error_at (loc, "not enough collapsed for loops");
33135 collapse_err = true;
33136 cp_parser_abort_tentative_parse (parser);
33137 declv = NULL_TREE;
33138 break;
33139 }
33140 }
33141 while (1);
33142
33143 if (declv)
33144 {
33145 cp_parser_parse_definitely (parser);
33146 nbraces += bracecount;
33147 }
33148 }
33149
33150 /* Note that we saved the original contents of this flag when we entered
33151 the structured block, and so we don't need to re-save it here. */
33152 if (code == CILK_SIMD || code == CILK_FOR)
33153 parser->in_statement = IN_CILK_SIMD_FOR;
33154 else
33155 parser->in_statement = IN_OMP_FOR;
33156
33157 /* Note that the grammar doesn't call for a structured block here,
33158 though the loop as a whole is a structured block. */
33159 body = push_stmt_list ();
33160 cp_parser_statement (parser, NULL_TREE, false, NULL);
33161 body = pop_stmt_list (body);
33162
33163 if (declv == NULL_TREE)
33164 ret = NULL_TREE;
33165 else
33166 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
33167 body, pre_body, &orig_inits, clauses);
33168
33169 while (nbraces)
33170 {
33171 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33172 {
33173 cp_lexer_consume_token (parser->lexer);
33174 nbraces--;
33175 }
33176 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33177 cp_lexer_consume_token (parser->lexer);
33178 else
33179 {
33180 if (!collapse_err)
33181 {
33182 error_at (cp_lexer_peek_token (parser->lexer)->location,
33183 "collapsed loops not perfectly nested");
33184 }
33185 collapse_err = true;
33186 cp_parser_statement_seq_opt (parser, NULL);
33187 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33188 break;
33189 }
33190 }
33191
33192 while (!for_block->is_empty ())
33193 add_stmt (pop_stmt_list (for_block->pop ()));
33194 release_tree_vector (for_block);
33195
33196 return ret;
33197 }
33198
33199 /* Helper function for OpenMP parsing, split clauses and call
33200 finish_omp_clauses on each of the set of clauses afterwards. */
33201
33202 static void
33203 cp_omp_split_clauses (location_t loc, enum tree_code code,
33204 omp_clause_mask mask, tree clauses, tree *cclauses)
33205 {
33206 int i;
33207 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
33208 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
33209 if (cclauses[i])
33210 cclauses[i] = finish_omp_clauses (cclauses[i], true);
33211 }
33212
33213 /* OpenMP 4.0:
33214 #pragma omp simd simd-clause[optseq] new-line
33215 for-loop */
33216
33217 #define OMP_SIMD_CLAUSE_MASK \
33218 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
33219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
33220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
33222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33226
33227 static tree
33228 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
33229 char *p_name, omp_clause_mask mask, tree *cclauses)
33230 {
33231 tree clauses, sb, ret;
33232 unsigned int save;
33233 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33234
33235 strcat (p_name, " simd");
33236 mask |= OMP_SIMD_CLAUSE_MASK;
33237
33238 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33239 cclauses == NULL);
33240 if (cclauses)
33241 {
33242 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
33243 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
33244 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
33245 OMP_CLAUSE_ORDERED);
33246 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
33247 {
33248 error_at (OMP_CLAUSE_LOCATION (c),
33249 "%<ordered%> clause with parameter may not be specified "
33250 "on %qs construct", p_name);
33251 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
33252 }
33253 }
33254
33255 sb = begin_omp_structured_block ();
33256 save = cp_parser_begin_omp_structured_block (parser);
33257
33258 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
33259
33260 cp_parser_end_omp_structured_block (parser, save);
33261 add_stmt (finish_omp_structured_block (sb));
33262
33263 return ret;
33264 }
33265
33266 /* OpenMP 2.5:
33267 #pragma omp for for-clause[optseq] new-line
33268 for-loop
33269
33270 OpenMP 4.0:
33271 #pragma omp for simd for-simd-clause[optseq] new-line
33272 for-loop */
33273
33274 #define OMP_FOR_CLAUSE_MASK \
33275 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
33281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
33282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
33283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33284
33285 static tree
33286 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
33287 char *p_name, omp_clause_mask mask, tree *cclauses)
33288 {
33289 tree clauses, sb, ret;
33290 unsigned int save;
33291 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33292
33293 strcat (p_name, " for");
33294 mask |= OMP_FOR_CLAUSE_MASK;
33295 if (cclauses)
33296 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
33297 /* Composite distribute parallel for{, simd} disallows ordered clause. */
33298 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33299 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
33300
33301 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33302 {
33303 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33304 const char *p = IDENTIFIER_POINTER (id);
33305
33306 if (strcmp (p, "simd") == 0)
33307 {
33308 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33309 if (cclauses == NULL)
33310 cclauses = cclauses_buf;
33311
33312 cp_lexer_consume_token (parser->lexer);
33313 if (!flag_openmp) /* flag_openmp_simd */
33314 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33315 cclauses);
33316 sb = begin_omp_structured_block ();
33317 save = cp_parser_begin_omp_structured_block (parser);
33318 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33319 cclauses);
33320 cp_parser_end_omp_structured_block (parser, save);
33321 tree body = finish_omp_structured_block (sb);
33322 if (ret == NULL)
33323 return ret;
33324 ret = make_node (OMP_FOR);
33325 TREE_TYPE (ret) = void_type_node;
33326 OMP_FOR_BODY (ret) = body;
33327 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33328 SET_EXPR_LOCATION (ret, loc);
33329 add_stmt (ret);
33330 return ret;
33331 }
33332 }
33333 if (!flag_openmp) /* flag_openmp_simd */
33334 {
33335 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33336 return NULL_TREE;
33337 }
33338
33339 /* Composite distribute parallel for disallows linear clause. */
33340 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33341 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
33342
33343 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33344 cclauses == NULL);
33345 if (cclauses)
33346 {
33347 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
33348 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33349 }
33350
33351 sb = begin_omp_structured_block ();
33352 save = cp_parser_begin_omp_structured_block (parser);
33353
33354 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
33355
33356 cp_parser_end_omp_structured_block (parser, save);
33357 add_stmt (finish_omp_structured_block (sb));
33358
33359 return ret;
33360 }
33361
33362 /* OpenMP 2.5:
33363 # pragma omp master new-line
33364 structured-block */
33365
33366 static tree
33367 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
33368 {
33369 cp_parser_require_pragma_eol (parser, pragma_tok);
33370 return c_finish_omp_master (input_location,
33371 cp_parser_omp_structured_block (parser));
33372 }
33373
33374 /* OpenMP 2.5:
33375 # pragma omp ordered new-line
33376 structured-block
33377
33378 OpenMP 4.5:
33379 # pragma omp ordered ordered-clauses new-line
33380 structured-block */
33381
33382 #define OMP_ORDERED_CLAUSE_MASK \
33383 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
33384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
33385
33386 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
33387 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
33388
33389 static bool
33390 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
33391 enum pragma_context context)
33392 {
33393 location_t loc = pragma_tok->location;
33394
33395 if (context != pragma_stmt && context != pragma_compound)
33396 {
33397 cp_parser_error (parser, "expected declaration specifiers");
33398 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33399 return false;
33400 }
33401
33402 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33403 {
33404 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33405 const char *p = IDENTIFIER_POINTER (id);
33406
33407 if (strcmp (p, "depend") == 0)
33408 {
33409 if (context == pragma_stmt)
33410 {
33411 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
33412 "%<depend%> clause may only be used in compound "
33413 "statements");
33414 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33415 return false;
33416 }
33417 tree clauses
33418 = cp_parser_omp_all_clauses (parser,
33419 OMP_ORDERED_DEPEND_CLAUSE_MASK,
33420 "#pragma omp ordered", pragma_tok);
33421 c_finish_omp_ordered (loc, clauses, NULL_TREE);
33422 return false;
33423 }
33424 }
33425
33426 tree clauses
33427 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
33428 "#pragma omp ordered", pragma_tok);
33429 c_finish_omp_ordered (loc, clauses,
33430 cp_parser_omp_structured_block (parser));
33431 return true;
33432 }
33433
33434 /* OpenMP 2.5:
33435
33436 section-scope:
33437 { section-sequence }
33438
33439 section-sequence:
33440 section-directive[opt] structured-block
33441 section-sequence section-directive structured-block */
33442
33443 static tree
33444 cp_parser_omp_sections_scope (cp_parser *parser)
33445 {
33446 tree stmt, substmt;
33447 bool error_suppress = false;
33448 cp_token *tok;
33449
33450 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
33451 return NULL_TREE;
33452
33453 stmt = push_stmt_list ();
33454
33455 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
33456 {
33457 substmt = cp_parser_omp_structured_block (parser);
33458 substmt = build1 (OMP_SECTION, void_type_node, substmt);
33459 add_stmt (substmt);
33460 }
33461
33462 while (1)
33463 {
33464 tok = cp_lexer_peek_token (parser->lexer);
33465 if (tok->type == CPP_CLOSE_BRACE)
33466 break;
33467 if (tok->type == CPP_EOF)
33468 break;
33469
33470 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
33471 {
33472 cp_lexer_consume_token (parser->lexer);
33473 cp_parser_require_pragma_eol (parser, tok);
33474 error_suppress = false;
33475 }
33476 else if (!error_suppress)
33477 {
33478 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
33479 error_suppress = true;
33480 }
33481
33482 substmt = cp_parser_omp_structured_block (parser);
33483 substmt = build1 (OMP_SECTION, void_type_node, substmt);
33484 add_stmt (substmt);
33485 }
33486 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33487
33488 substmt = pop_stmt_list (stmt);
33489
33490 stmt = make_node (OMP_SECTIONS);
33491 TREE_TYPE (stmt) = void_type_node;
33492 OMP_SECTIONS_BODY (stmt) = substmt;
33493
33494 add_stmt (stmt);
33495 return stmt;
33496 }
33497
33498 /* OpenMP 2.5:
33499 # pragma omp sections sections-clause[optseq] newline
33500 sections-scope */
33501
33502 #define OMP_SECTIONS_CLAUSE_MASK \
33503 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
33508
33509 static tree
33510 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
33511 char *p_name, omp_clause_mask mask, tree *cclauses)
33512 {
33513 tree clauses, ret;
33514 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33515
33516 strcat (p_name, " sections");
33517 mask |= OMP_SECTIONS_CLAUSE_MASK;
33518 if (cclauses)
33519 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
33520
33521 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33522 cclauses == NULL);
33523 if (cclauses)
33524 {
33525 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
33526 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
33527 }
33528
33529 ret = cp_parser_omp_sections_scope (parser);
33530 if (ret)
33531 OMP_SECTIONS_CLAUSES (ret) = clauses;
33532
33533 return ret;
33534 }
33535
33536 /* OpenMP 2.5:
33537 # pragma omp parallel parallel-clause[optseq] new-line
33538 structured-block
33539 # pragma omp parallel for parallel-for-clause[optseq] new-line
33540 structured-block
33541 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
33542 structured-block
33543
33544 OpenMP 4.0:
33545 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
33546 structured-block */
33547
33548 #define OMP_PARALLEL_CLAUSE_MASK \
33549 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
33550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
33553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
33554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
33555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
33557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
33558
33559 static tree
33560 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
33561 char *p_name, omp_clause_mask mask, tree *cclauses)
33562 {
33563 tree stmt, clauses, block;
33564 unsigned int save;
33565 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33566
33567 strcat (p_name, " parallel");
33568 mask |= OMP_PARALLEL_CLAUSE_MASK;
33569 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
33570 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
33571 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
33572 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
33573
33574 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33575 {
33576 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33577 if (cclauses == NULL)
33578 cclauses = cclauses_buf;
33579
33580 cp_lexer_consume_token (parser->lexer);
33581 if (!flag_openmp) /* flag_openmp_simd */
33582 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
33583 block = begin_omp_parallel ();
33584 save = cp_parser_begin_omp_structured_block (parser);
33585 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
33586 cp_parser_end_omp_structured_block (parser, save);
33587 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
33588 block);
33589 if (ret == NULL_TREE)
33590 return ret;
33591 OMP_PARALLEL_COMBINED (stmt) = 1;
33592 return stmt;
33593 }
33594 /* When combined with distribute, parallel has to be followed by for.
33595 #pragma omp target parallel is allowed though. */
33596 else if (cclauses
33597 && (mask & (OMP_CLAUSE_MASK_1
33598 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33599 {
33600 error_at (loc, "expected %<for%> after %qs", p_name);
33601 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33602 return NULL_TREE;
33603 }
33604 else if (!flag_openmp) /* flag_openmp_simd */
33605 {
33606 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33607 return NULL_TREE;
33608 }
33609 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33610 {
33611 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33612 const char *p = IDENTIFIER_POINTER (id);
33613 if (strcmp (p, "sections") == 0)
33614 {
33615 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33616 cclauses = cclauses_buf;
33617
33618 cp_lexer_consume_token (parser->lexer);
33619 block = begin_omp_parallel ();
33620 save = cp_parser_begin_omp_structured_block (parser);
33621 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
33622 cp_parser_end_omp_structured_block (parser, save);
33623 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
33624 block);
33625 OMP_PARALLEL_COMBINED (stmt) = 1;
33626 return stmt;
33627 }
33628 }
33629
33630 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
33631 if (cclauses)
33632 {
33633 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
33634 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
33635 }
33636
33637 block = begin_omp_parallel ();
33638 save = cp_parser_begin_omp_structured_block (parser);
33639 cp_parser_statement (parser, NULL_TREE, false, NULL);
33640 cp_parser_end_omp_structured_block (parser, save);
33641 stmt = finish_omp_parallel (clauses, block);
33642 return stmt;
33643 }
33644
33645 /* OpenMP 2.5:
33646 # pragma omp single single-clause[optseq] new-line
33647 structured-block */
33648
33649 #define OMP_SINGLE_CLAUSE_MASK \
33650 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
33653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
33654
33655 static tree
33656 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
33657 {
33658 tree stmt = make_node (OMP_SINGLE);
33659 TREE_TYPE (stmt) = void_type_node;
33660
33661 OMP_SINGLE_CLAUSES (stmt)
33662 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
33663 "#pragma omp single", pragma_tok);
33664 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
33665
33666 return add_stmt (stmt);
33667 }
33668
33669 /* OpenMP 3.0:
33670 # pragma omp task task-clause[optseq] new-line
33671 structured-block */
33672
33673 #define OMP_TASK_CLAUSE_MASK \
33674 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
33675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
33676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
33677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
33680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
33681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
33682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
33683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
33684
33685 static tree
33686 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
33687 {
33688 tree clauses, block;
33689 unsigned int save;
33690
33691 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
33692 "#pragma omp task", pragma_tok);
33693 block = begin_omp_task ();
33694 save = cp_parser_begin_omp_structured_block (parser);
33695 cp_parser_statement (parser, NULL_TREE, false, NULL);
33696 cp_parser_end_omp_structured_block (parser, save);
33697 return finish_omp_task (clauses, block);
33698 }
33699
33700 /* OpenMP 3.0:
33701 # pragma omp taskwait new-line */
33702
33703 static void
33704 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
33705 {
33706 cp_parser_require_pragma_eol (parser, pragma_tok);
33707 finish_omp_taskwait ();
33708 }
33709
33710 /* OpenMP 3.1:
33711 # pragma omp taskyield new-line */
33712
33713 static void
33714 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
33715 {
33716 cp_parser_require_pragma_eol (parser, pragma_tok);
33717 finish_omp_taskyield ();
33718 }
33719
33720 /* OpenMP 4.0:
33721 # pragma omp taskgroup new-line
33722 structured-block */
33723
33724 static tree
33725 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
33726 {
33727 cp_parser_require_pragma_eol (parser, pragma_tok);
33728 return c_finish_omp_taskgroup (input_location,
33729 cp_parser_omp_structured_block (parser));
33730 }
33731
33732
33733 /* OpenMP 2.5:
33734 # pragma omp threadprivate (variable-list) */
33735
33736 static void
33737 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
33738 {
33739 tree vars;
33740
33741 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33742 cp_parser_require_pragma_eol (parser, pragma_tok);
33743
33744 finish_omp_threadprivate (vars);
33745 }
33746
33747 /* OpenMP 4.0:
33748 # pragma omp cancel cancel-clause[optseq] new-line */
33749
33750 #define OMP_CANCEL_CLAUSE_MASK \
33751 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
33752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
33753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
33754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
33755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
33756
33757 static void
33758 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
33759 {
33760 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
33761 "#pragma omp cancel", pragma_tok);
33762 finish_omp_cancel (clauses);
33763 }
33764
33765 /* OpenMP 4.0:
33766 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
33767
33768 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
33769 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
33770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
33771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
33772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
33773
33774 static void
33775 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
33776 {
33777 tree clauses;
33778 bool point_seen = false;
33779
33780 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33781 {
33782 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33783 const char *p = IDENTIFIER_POINTER (id);
33784
33785 if (strcmp (p, "point") == 0)
33786 {
33787 cp_lexer_consume_token (parser->lexer);
33788 point_seen = true;
33789 }
33790 }
33791 if (!point_seen)
33792 {
33793 cp_parser_error (parser, "expected %<point%>");
33794 cp_parser_require_pragma_eol (parser, pragma_tok);
33795 return;
33796 }
33797
33798 clauses = cp_parser_omp_all_clauses (parser,
33799 OMP_CANCELLATION_POINT_CLAUSE_MASK,
33800 "#pragma omp cancellation point",
33801 pragma_tok);
33802 finish_omp_cancellation_point (clauses);
33803 }
33804
33805 /* OpenMP 4.0:
33806 #pragma omp distribute distribute-clause[optseq] new-line
33807 for-loop */
33808
33809 #define OMP_DISTRIBUTE_CLAUSE_MASK \
33810 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
33814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33815
33816 static tree
33817 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
33818 char *p_name, omp_clause_mask mask, tree *cclauses)
33819 {
33820 tree clauses, sb, ret;
33821 unsigned int save;
33822 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33823
33824 strcat (p_name, " distribute");
33825 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
33826
33827 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33828 {
33829 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33830 const char *p = IDENTIFIER_POINTER (id);
33831 bool simd = false;
33832 bool parallel = false;
33833
33834 if (strcmp (p, "simd") == 0)
33835 simd = true;
33836 else
33837 parallel = strcmp (p, "parallel") == 0;
33838 if (parallel || simd)
33839 {
33840 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33841 if (cclauses == NULL)
33842 cclauses = cclauses_buf;
33843 cp_lexer_consume_token (parser->lexer);
33844 if (!flag_openmp) /* flag_openmp_simd */
33845 {
33846 if (simd)
33847 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33848 cclauses);
33849 else
33850 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
33851 cclauses);
33852 }
33853 sb = begin_omp_structured_block ();
33854 save = cp_parser_begin_omp_structured_block (parser);
33855 if (simd)
33856 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33857 cclauses);
33858 else
33859 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
33860 cclauses);
33861 cp_parser_end_omp_structured_block (parser, save);
33862 tree body = finish_omp_structured_block (sb);
33863 if (ret == NULL)
33864 return ret;
33865 ret = make_node (OMP_DISTRIBUTE);
33866 TREE_TYPE (ret) = void_type_node;
33867 OMP_FOR_BODY (ret) = body;
33868 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
33869 SET_EXPR_LOCATION (ret, loc);
33870 add_stmt (ret);
33871 return ret;
33872 }
33873 }
33874 if (!flag_openmp) /* flag_openmp_simd */
33875 {
33876 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33877 return NULL_TREE;
33878 }
33879
33880 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33881 cclauses == NULL);
33882 if (cclauses)
33883 {
33884 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
33885 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
33886 }
33887
33888 sb = begin_omp_structured_block ();
33889 save = cp_parser_begin_omp_structured_block (parser);
33890
33891 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
33892
33893 cp_parser_end_omp_structured_block (parser, save);
33894 add_stmt (finish_omp_structured_block (sb));
33895
33896 return ret;
33897 }
33898
33899 /* OpenMP 4.0:
33900 # pragma omp teams teams-clause[optseq] new-line
33901 structured-block */
33902
33903 #define OMP_TEAMS_CLAUSE_MASK \
33904 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
33907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
33909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
33910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
33911
33912 static tree
33913 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
33914 char *p_name, omp_clause_mask mask, tree *cclauses)
33915 {
33916 tree clauses, sb, ret;
33917 unsigned int save;
33918 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33919
33920 strcat (p_name, " teams");
33921 mask |= OMP_TEAMS_CLAUSE_MASK;
33922
33923 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33924 {
33925 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33926 const char *p = IDENTIFIER_POINTER (id);
33927 if (strcmp (p, "distribute") == 0)
33928 {
33929 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33930 if (cclauses == NULL)
33931 cclauses = cclauses_buf;
33932
33933 cp_lexer_consume_token (parser->lexer);
33934 if (!flag_openmp) /* flag_openmp_simd */
33935 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
33936 cclauses);
33937 sb = begin_omp_structured_block ();
33938 save = cp_parser_begin_omp_structured_block (parser);
33939 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
33940 cclauses);
33941 cp_parser_end_omp_structured_block (parser, save);
33942 tree body = finish_omp_structured_block (sb);
33943 if (ret == NULL)
33944 return ret;
33945 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
33946 ret = make_node (OMP_TEAMS);
33947 TREE_TYPE (ret) = void_type_node;
33948 OMP_TEAMS_CLAUSES (ret) = clauses;
33949 OMP_TEAMS_BODY (ret) = body;
33950 OMP_TEAMS_COMBINED (ret) = 1;
33951 return add_stmt (ret);
33952 }
33953 }
33954 if (!flag_openmp) /* flag_openmp_simd */
33955 {
33956 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33957 return NULL_TREE;
33958 }
33959
33960 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33961 cclauses == NULL);
33962 if (cclauses)
33963 {
33964 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
33965 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
33966 }
33967
33968 tree stmt = make_node (OMP_TEAMS);
33969 TREE_TYPE (stmt) = void_type_node;
33970 OMP_TEAMS_CLAUSES (stmt) = clauses;
33971 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
33972
33973 return add_stmt (stmt);
33974 }
33975
33976 /* OpenMP 4.0:
33977 # pragma omp target data target-data-clause[optseq] new-line
33978 structured-block */
33979
33980 #define OMP_TARGET_DATA_CLAUSE_MASK \
33981 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
33982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
33983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
33984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
33985
33986 static tree
33987 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
33988 {
33989 tree clauses
33990 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
33991 "#pragma omp target data", pragma_tok);
33992 int map_seen = 0;
33993 for (tree *pc = &clauses; *pc;)
33994 {
33995 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
33996 switch (OMP_CLAUSE_MAP_KIND (*pc))
33997 {
33998 case GOMP_MAP_TO:
33999 case GOMP_MAP_ALWAYS_TO:
34000 case GOMP_MAP_FROM:
34001 case GOMP_MAP_ALWAYS_FROM:
34002 case GOMP_MAP_TOFROM:
34003 case GOMP_MAP_ALWAYS_TOFROM:
34004 case GOMP_MAP_ALLOC:
34005 map_seen = 3;
34006 break;
34007 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34008 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34009 case GOMP_MAP_ALWAYS_POINTER:
34010 break;
34011 default:
34012 map_seen |= 1;
34013 error_at (OMP_CLAUSE_LOCATION (*pc),
34014 "%<#pragma omp target data%> with map-type other "
34015 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34016 "on %<map%> clause");
34017 *pc = OMP_CLAUSE_CHAIN (*pc);
34018 continue;
34019 }
34020 pc = &OMP_CLAUSE_CHAIN (*pc);
34021 }
34022
34023 if (map_seen != 3)
34024 {
34025 if (map_seen == 0)
34026 error_at (pragma_tok->location,
34027 "%<#pragma omp target data%> must contain at least "
34028 "one %<map%> clause");
34029 return NULL_TREE;
34030 }
34031
34032 tree stmt = make_node (OMP_TARGET_DATA);
34033 TREE_TYPE (stmt) = void_type_node;
34034 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
34035
34036 keep_next_level (true);
34037 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
34038
34039 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34040 return add_stmt (stmt);
34041 }
34042
34043 /* OpenMP 4.5:
34044 # pragma omp target enter data target-enter-data-clause[optseq] new-line
34045 structured-block */
34046
34047 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
34048 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34053
34054 static tree
34055 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
34056 enum pragma_context context)
34057 {
34058 bool data_seen = false;
34059 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34060 {
34061 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34062 const char *p = IDENTIFIER_POINTER (id);
34063
34064 if (strcmp (p, "data") == 0)
34065 {
34066 cp_lexer_consume_token (parser->lexer);
34067 data_seen = true;
34068 }
34069 }
34070 if (!data_seen)
34071 {
34072 cp_parser_error (parser, "expected %<data%>");
34073 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34074 return NULL_TREE;
34075 }
34076
34077 if (context == pragma_stmt)
34078 {
34079 error_at (pragma_tok->location,
34080 "%<#pragma omp target enter data%> may only be "
34081 "used in compound statements");
34082 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34083 return NULL_TREE;
34084 }
34085
34086 tree clauses
34087 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
34088 "#pragma omp target enter data", pragma_tok);
34089 int map_seen = 0;
34090 for (tree *pc = &clauses; *pc;)
34091 {
34092 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34093 switch (OMP_CLAUSE_MAP_KIND (*pc))
34094 {
34095 case GOMP_MAP_TO:
34096 case GOMP_MAP_ALWAYS_TO:
34097 case GOMP_MAP_ALLOC:
34098 map_seen = 3;
34099 break;
34100 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34101 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34102 case GOMP_MAP_ALWAYS_POINTER:
34103 break;
34104 default:
34105 map_seen |= 1;
34106 error_at (OMP_CLAUSE_LOCATION (*pc),
34107 "%<#pragma omp target enter data%> with map-type other "
34108 "than %<to%> or %<alloc%> on %<map%> clause");
34109 *pc = OMP_CLAUSE_CHAIN (*pc);
34110 continue;
34111 }
34112 pc = &OMP_CLAUSE_CHAIN (*pc);
34113 }
34114
34115 if (map_seen != 3)
34116 {
34117 if (map_seen == 0)
34118 error_at (pragma_tok->location,
34119 "%<#pragma omp target enter data%> must contain at least "
34120 "one %<map%> clause");
34121 return NULL_TREE;
34122 }
34123
34124 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
34125 TREE_TYPE (stmt) = void_type_node;
34126 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
34127 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34128 return add_stmt (stmt);
34129 }
34130
34131 /* OpenMP 4.5:
34132 # pragma omp target exit data target-enter-data-clause[optseq] new-line
34133 structured-block */
34134
34135 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
34136 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34141
34142 static tree
34143 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
34144 enum pragma_context context)
34145 {
34146 bool data_seen = false;
34147 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34148 {
34149 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34150 const char *p = IDENTIFIER_POINTER (id);
34151
34152 if (strcmp (p, "data") == 0)
34153 {
34154 cp_lexer_consume_token (parser->lexer);
34155 data_seen = true;
34156 }
34157 }
34158 if (!data_seen)
34159 {
34160 cp_parser_error (parser, "expected %<data%>");
34161 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34162 return NULL_TREE;
34163 }
34164
34165 if (context == pragma_stmt)
34166 {
34167 error_at (pragma_tok->location,
34168 "%<#pragma omp target exit data%> may only be "
34169 "used in compound statements");
34170 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34171 return NULL_TREE;
34172 }
34173
34174 tree clauses
34175 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
34176 "#pragma omp target exit data", pragma_tok);
34177 int map_seen = 0;
34178 for (tree *pc = &clauses; *pc;)
34179 {
34180 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34181 switch (OMP_CLAUSE_MAP_KIND (*pc))
34182 {
34183 case GOMP_MAP_FROM:
34184 case GOMP_MAP_ALWAYS_FROM:
34185 case GOMP_MAP_RELEASE:
34186 case GOMP_MAP_DELETE:
34187 map_seen = 3;
34188 break;
34189 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34190 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34191 case GOMP_MAP_ALWAYS_POINTER:
34192 break;
34193 default:
34194 map_seen |= 1;
34195 error_at (OMP_CLAUSE_LOCATION (*pc),
34196 "%<#pragma omp target exit data%> with map-type other "
34197 "than %<from%>, %<release%> or %<delete%> on %<map%>"
34198 " clause");
34199 *pc = OMP_CLAUSE_CHAIN (*pc);
34200 continue;
34201 }
34202 pc = &OMP_CLAUSE_CHAIN (*pc);
34203 }
34204
34205 if (map_seen != 3)
34206 {
34207 if (map_seen == 0)
34208 error_at (pragma_tok->location,
34209 "%<#pragma omp target exit data%> must contain at least "
34210 "one %<map%> clause");
34211 return NULL_TREE;
34212 }
34213
34214 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
34215 TREE_TYPE (stmt) = void_type_node;
34216 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
34217 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34218 return add_stmt (stmt);
34219 }
34220
34221 /* OpenMP 4.0:
34222 # pragma omp target update target-update-clause[optseq] new-line */
34223
34224 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
34225 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
34226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
34227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34231
34232 static bool
34233 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
34234 enum pragma_context context)
34235 {
34236 if (context == pragma_stmt)
34237 {
34238 error_at (pragma_tok->location,
34239 "%<#pragma omp target update%> may only be "
34240 "used in compound statements");
34241 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34242 return false;
34243 }
34244
34245 tree clauses
34246 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
34247 "#pragma omp target update", pragma_tok);
34248 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
34249 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
34250 {
34251 error_at (pragma_tok->location,
34252 "%<#pragma omp target update%> must contain at least one "
34253 "%<from%> or %<to%> clauses");
34254 return false;
34255 }
34256
34257 tree stmt = make_node (OMP_TARGET_UPDATE);
34258 TREE_TYPE (stmt) = void_type_node;
34259 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
34260 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34261 add_stmt (stmt);
34262 return false;
34263 }
34264
34265 /* OpenMP 4.0:
34266 # pragma omp target target-clause[optseq] new-line
34267 structured-block */
34268
34269 #define OMP_TARGET_CLAUSE_MASK \
34270 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
34278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
34279
34280 static bool
34281 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
34282 enum pragma_context context)
34283 {
34284 tree *pc = NULL, stmt;
34285
34286 if (context != pragma_stmt && context != pragma_compound)
34287 {
34288 cp_parser_error (parser, "expected declaration specifiers");
34289 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34290 return false;
34291 }
34292
34293 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34294 {
34295 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34296 const char *p = IDENTIFIER_POINTER (id);
34297 enum tree_code ccode = ERROR_MARK;
34298
34299 if (strcmp (p, "teams") == 0)
34300 ccode = OMP_TEAMS;
34301 else if (strcmp (p, "parallel") == 0)
34302 ccode = OMP_PARALLEL;
34303 else if (strcmp (p, "simd") == 0)
34304 ccode = OMP_SIMD;
34305 if (ccode != ERROR_MARK)
34306 {
34307 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
34308 char p_name[sizeof ("#pragma omp target teams distribute "
34309 "parallel for simd")];
34310
34311 cp_lexer_consume_token (parser->lexer);
34312 strcpy (p_name, "#pragma omp target");
34313 if (!flag_openmp) /* flag_openmp_simd */
34314 {
34315 tree stmt;
34316 switch (ccode)
34317 {
34318 case OMP_TEAMS:
34319 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
34320 OMP_TARGET_CLAUSE_MASK,
34321 cclauses);
34322 break;
34323 case OMP_PARALLEL:
34324 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34325 OMP_TARGET_CLAUSE_MASK,
34326 cclauses);
34327 break;
34328 case OMP_SIMD:
34329 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
34330 OMP_TARGET_CLAUSE_MASK,
34331 cclauses);
34332 break;
34333 default:
34334 gcc_unreachable ();
34335 }
34336 return stmt != NULL_TREE;
34337 }
34338 keep_next_level (true);
34339 tree sb = begin_omp_structured_block (), ret;
34340 unsigned save = cp_parser_begin_omp_structured_block (parser);
34341 switch (ccode)
34342 {
34343 case OMP_TEAMS:
34344 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
34345 OMP_TARGET_CLAUSE_MASK, cclauses);
34346 break;
34347 case OMP_PARALLEL:
34348 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34349 OMP_TARGET_CLAUSE_MASK, cclauses);
34350 break;
34351 case OMP_SIMD:
34352 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
34353 OMP_TARGET_CLAUSE_MASK, cclauses);
34354 break;
34355 default:
34356 gcc_unreachable ();
34357 }
34358 cp_parser_end_omp_structured_block (parser, save);
34359 tree body = finish_omp_structured_block (sb);
34360 if (ret == NULL_TREE)
34361 return false;
34362 if (ccode == OMP_TEAMS && !processing_template_decl)
34363 {
34364 /* For combined target teams, ensure the num_teams and
34365 thread_limit clause expressions are evaluated on the host,
34366 before entering the target construct. */
34367 tree c;
34368 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34369 c; c = OMP_CLAUSE_CHAIN (c))
34370 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
34371 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
34372 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
34373 {
34374 tree expr = OMP_CLAUSE_OPERAND (c, 0);
34375 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
34376 if (expr == error_mark_node)
34377 continue;
34378 tree tmp = TARGET_EXPR_SLOT (expr);
34379 add_stmt (expr);
34380 OMP_CLAUSE_OPERAND (c, 0) = expr;
34381 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
34382 OMP_CLAUSE_FIRSTPRIVATE);
34383 OMP_CLAUSE_DECL (tc) = tmp;
34384 OMP_CLAUSE_CHAIN (tc)
34385 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
34386 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
34387 }
34388 }
34389 tree stmt = make_node (OMP_TARGET);
34390 TREE_TYPE (stmt) = void_type_node;
34391 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
34392 OMP_TARGET_BODY (stmt) = body;
34393 OMP_TARGET_COMBINED (stmt) = 1;
34394 add_stmt (stmt);
34395 pc = &OMP_TARGET_CLAUSES (stmt);
34396 goto check_clauses;
34397 }
34398 else if (!flag_openmp) /* flag_openmp_simd */
34399 {
34400 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34401 return false;
34402 }
34403 else if (strcmp (p, "data") == 0)
34404 {
34405 cp_lexer_consume_token (parser->lexer);
34406 cp_parser_omp_target_data (parser, pragma_tok);
34407 return true;
34408 }
34409 else if (strcmp (p, "enter") == 0)
34410 {
34411 cp_lexer_consume_token (parser->lexer);
34412 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
34413 return false;
34414 }
34415 else if (strcmp (p, "exit") == 0)
34416 {
34417 cp_lexer_consume_token (parser->lexer);
34418 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
34419 return false;
34420 }
34421 else if (strcmp (p, "update") == 0)
34422 {
34423 cp_lexer_consume_token (parser->lexer);
34424 return cp_parser_omp_target_update (parser, pragma_tok, context);
34425 }
34426 }
34427
34428 stmt = make_node (OMP_TARGET);
34429 TREE_TYPE (stmt) = void_type_node;
34430
34431 OMP_TARGET_CLAUSES (stmt)
34432 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
34433 "#pragma omp target", pragma_tok);
34434 pc = &OMP_TARGET_CLAUSES (stmt);
34435 keep_next_level (true);
34436 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
34437
34438 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34439 add_stmt (stmt);
34440
34441 check_clauses:
34442 while (*pc)
34443 {
34444 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34445 switch (OMP_CLAUSE_MAP_KIND (*pc))
34446 {
34447 case GOMP_MAP_TO:
34448 case GOMP_MAP_ALWAYS_TO:
34449 case GOMP_MAP_FROM:
34450 case GOMP_MAP_ALWAYS_FROM:
34451 case GOMP_MAP_TOFROM:
34452 case GOMP_MAP_ALWAYS_TOFROM:
34453 case GOMP_MAP_ALLOC:
34454 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34455 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34456 case GOMP_MAP_ALWAYS_POINTER:
34457 break;
34458 default:
34459 error_at (OMP_CLAUSE_LOCATION (*pc),
34460 "%<#pragma omp target%> with map-type other "
34461 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34462 "on %<map%> clause");
34463 *pc = OMP_CLAUSE_CHAIN (*pc);
34464 continue;
34465 }
34466 pc = &OMP_CLAUSE_CHAIN (*pc);
34467 }
34468 return true;
34469 }
34470
34471 /* OpenACC 2.0:
34472 # pragma acc cache (variable-list) new-line
34473 */
34474
34475 static tree
34476 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
34477 {
34478 tree stmt, clauses;
34479
34480 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
34481 clauses = finish_omp_clauses (clauses, false);
34482
34483 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
34484
34485 stmt = make_node (OACC_CACHE);
34486 TREE_TYPE (stmt) = void_type_node;
34487 OACC_CACHE_CLAUSES (stmt) = clauses;
34488 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34489 add_stmt (stmt);
34490
34491 return stmt;
34492 }
34493
34494 /* OpenACC 2.0:
34495 # pragma acc data oacc-data-clause[optseq] new-line
34496 structured-block */
34497
34498 #define OACC_DATA_CLAUSE_MASK \
34499 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
34500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
34504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
34506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
34507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
34509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
34510
34511 static tree
34512 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
34513 {
34514 tree stmt, clauses, block;
34515 unsigned int save;
34516
34517 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
34518 "#pragma acc data", pragma_tok);
34519
34520 block = begin_omp_parallel ();
34521 save = cp_parser_begin_omp_structured_block (parser);
34522 cp_parser_statement (parser, NULL_TREE, false, NULL);
34523 cp_parser_end_omp_structured_block (parser, save);
34524 stmt = finish_oacc_data (clauses, block);
34525 return stmt;
34526 }
34527
34528 /* OpenACC 2.0:
34529 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
34530
34531 or
34532
34533 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
34534
34535 LOC is the location of the #pragma token.
34536 */
34537
34538 #define OACC_ENTER_DATA_CLAUSE_MASK \
34539 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
34545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
34546
34547 #define OACC_EXIT_DATA_CLAUSE_MASK \
34548 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
34552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
34553
34554 static tree
34555 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
34556 bool enter)
34557 {
34558 tree stmt, clauses;
34559
34560 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
34561 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34562 {
34563 cp_parser_error (parser, enter
34564 ? "expected %<data%> in %<#pragma acc enter data%>"
34565 : "expected %<data%> in %<#pragma acc exit data%>");
34566 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34567 return NULL_TREE;
34568 }
34569
34570 const char *p =
34571 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
34572 if (strcmp (p, "data") != 0)
34573 {
34574 cp_parser_error (parser, "invalid pragma");
34575 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34576 return NULL_TREE;
34577 }
34578
34579 cp_lexer_consume_token (parser->lexer);
34580
34581 if (enter)
34582 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
34583 "#pragma acc enter data", pragma_tok);
34584 else
34585 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
34586 "#pragma acc exit data", pragma_tok);
34587
34588 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
34589 {
34590 error_at (pragma_tok->location,
34591 "%<#pragma acc enter data%> has no data movement clause");
34592 return NULL_TREE;
34593 }
34594
34595 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
34596 TREE_TYPE (stmt) = void_type_node;
34597 OMP_STANDALONE_CLAUSES (stmt) = clauses;
34598 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34599 add_stmt (stmt);
34600 return stmt;
34601 }
34602
34603 /* OpenACC 2.0:
34604 # pragma acc loop oacc-loop-clause[optseq] new-line
34605 structured-block */
34606
34607 #define OACC_LOOP_CLAUSE_MASK \
34608 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
34609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
34610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
34611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
34612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
34613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
34614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
34615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
34616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
34617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
34618
34619 static tree
34620 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
34621 omp_clause_mask mask, tree *cclauses)
34622 {
34623 strcat (p_name, " loop");
34624 mask |= OACC_LOOP_CLAUSE_MASK;
34625
34626 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
34627 cclauses == NULL);
34628 if (cclauses)
34629 {
34630 clauses = c_oacc_split_loop_clauses (clauses, cclauses);
34631 if (*cclauses)
34632 finish_omp_clauses (*cclauses, false);
34633 if (clauses)
34634 finish_omp_clauses (clauses, false);
34635 }
34636
34637 tree block = begin_omp_structured_block ();
34638 int save = cp_parser_begin_omp_structured_block (parser);
34639 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
34640 cp_parser_end_omp_structured_block (parser, save);
34641 add_stmt (finish_omp_structured_block (block));
34642
34643 return stmt;
34644 }
34645
34646 /* OpenACC 2.0:
34647 # pragma acc kernels oacc-kernels-clause[optseq] new-line
34648 structured-block
34649
34650 or
34651
34652 # pragma acc parallel oacc-parallel-clause[optseq] new-line
34653 structured-block
34654 */
34655
34656 #define OACC_KERNELS_CLAUSE_MASK \
34657 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
34659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
34663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
34664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
34666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
34667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
34669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
34670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
34671
34672 #define OACC_PARALLEL_CLAUSE_MASK \
34673 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
34675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
34676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
34677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
34678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
34679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
34680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
34681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
34683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
34684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
34685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
34686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
34687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
34688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
34689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
34690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
34691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
34692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
34693
34694 static tree
34695 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
34696 char *p_name)
34697 {
34698 omp_clause_mask mask;
34699 enum tree_code code;
34700 switch (pragma_tok->pragma_kind)
34701 {
34702 case PRAGMA_OACC_KERNELS:
34703 strcat (p_name, " kernels");
34704 mask = OACC_KERNELS_CLAUSE_MASK;
34705 code = OACC_KERNELS;
34706 break;
34707 case PRAGMA_OACC_PARALLEL:
34708 strcat (p_name, " parallel");
34709 mask = OACC_PARALLEL_CLAUSE_MASK;
34710 code = OACC_PARALLEL;
34711 break;
34712 default:
34713 gcc_unreachable ();
34714 }
34715
34716 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34717 {
34718 const char *p
34719 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
34720 if (strcmp (p, "loop") == 0)
34721 {
34722 cp_lexer_consume_token (parser->lexer);
34723 mask |= OACC_LOOP_CLAUSE_MASK;
34724
34725 tree block = begin_omp_parallel ();
34726 tree clauses;
34727 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses);
34728 return finish_omp_construct (code, block, clauses);
34729 }
34730 }
34731
34732 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
34733
34734 tree block = begin_omp_parallel ();
34735 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34736 cp_parser_statement (parser, NULL_TREE, false, NULL);
34737 cp_parser_end_omp_structured_block (parser, save);
34738 return finish_omp_construct (code, block, clauses);
34739 }
34740
34741 /* OpenACC 2.0:
34742 # pragma acc update oacc-update-clause[optseq] new-line
34743 */
34744
34745 #define OACC_UPDATE_CLAUSE_MASK \
34746 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
34747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
34748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
34749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
34750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
34751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
34752
34753 static tree
34754 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
34755 {
34756 tree stmt, clauses;
34757
34758 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
34759 "#pragma acc update", pragma_tok);
34760
34761 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
34762 {
34763 error_at (pragma_tok->location,
34764 "%<#pragma acc update%> must contain at least one "
34765 "%<device%> or %<host%> or %<self%> clause");
34766 return NULL_TREE;
34767 }
34768
34769 stmt = make_node (OACC_UPDATE);
34770 TREE_TYPE (stmt) = void_type_node;
34771 OACC_UPDATE_CLAUSES (stmt) = clauses;
34772 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34773 add_stmt (stmt);
34774 return stmt;
34775 }
34776
34777 /* OpenACC 2.0:
34778 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
34779
34780 LOC is the location of the #pragma token.
34781 */
34782
34783 #define OACC_WAIT_CLAUSE_MASK \
34784 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
34785
34786 static tree
34787 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
34788 {
34789 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
34790 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34791
34792 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34793 list = cp_parser_oacc_wait_list (parser, loc, list);
34794
34795 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
34796 "#pragma acc wait", pragma_tok);
34797
34798 stmt = c_finish_oacc_wait (loc, list, clauses);
34799
34800 return stmt;
34801 }
34802
34803 /* OpenMP 4.0:
34804 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
34805
34806 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
34807 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
34811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
34812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
34813
34814 static void
34815 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
34816 enum pragma_context context)
34817 {
34818 bool first_p = parser->omp_declare_simd == NULL;
34819 cp_omp_declare_simd_data data;
34820 if (first_p)
34821 {
34822 data.error_seen = false;
34823 data.fndecl_seen = false;
34824 data.tokens = vNULL;
34825 parser->omp_declare_simd = &data;
34826 }
34827 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
34828 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
34829 cp_lexer_consume_token (parser->lexer);
34830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34831 parser->omp_declare_simd->error_seen = true;
34832 cp_parser_require_pragma_eol (parser, pragma_tok);
34833 struct cp_token_cache *cp
34834 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
34835 parser->omp_declare_simd->tokens.safe_push (cp);
34836 if (first_p)
34837 {
34838 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
34839 cp_parser_pragma (parser, context);
34840 switch (context)
34841 {
34842 case pragma_external:
34843 cp_parser_declaration (parser);
34844 break;
34845 case pragma_member:
34846 cp_parser_member_declaration (parser);
34847 break;
34848 case pragma_objc_icode:
34849 cp_parser_block_declaration (parser, /*statement_p=*/false);
34850 break;
34851 default:
34852 cp_parser_declaration_statement (parser);
34853 break;
34854 }
34855 if (parser->omp_declare_simd
34856 && !parser->omp_declare_simd->error_seen
34857 && !parser->omp_declare_simd->fndecl_seen)
34858 error_at (pragma_tok->location,
34859 "%<#pragma omp declare simd%> not immediately followed by "
34860 "function declaration or definition");
34861 data.tokens.release ();
34862 parser->omp_declare_simd = NULL;
34863 }
34864 }
34865
34866 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
34867 This function is modelled similar to the late parsing of omp declare
34868 simd. */
34869
34870 static tree
34871 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
34872 {
34873 struct cp_token_cache *ce;
34874 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
34875 int ii = 0;
34876
34877 if (parser->omp_declare_simd != NULL)
34878 {
34879 error ("%<#pragma omp declare simd%> cannot be used in the same function"
34880 " marked as a Cilk Plus SIMD-enabled function");
34881 XDELETE (parser->cilk_simd_fn_info);
34882 parser->cilk_simd_fn_info = NULL;
34883 return attrs;
34884 }
34885 if (!info->error_seen && info->fndecl_seen)
34886 {
34887 error ("vector attribute not immediately followed by a single function"
34888 " declaration or definition");
34889 info->error_seen = true;
34890 }
34891 if (info->error_seen)
34892 return attrs;
34893
34894 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
34895 {
34896 tree c, cl;
34897
34898 cp_parser_push_lexer_for_tokens (parser, ce);
34899 parser->lexer->in_pragma = true;
34900 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
34901 "SIMD-enabled functions attribute",
34902 NULL);
34903 cp_parser_pop_lexer (parser);
34904 if (cl)
34905 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
34906
34907 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
34908 TREE_CHAIN (c) = attrs;
34909 attrs = c;
34910
34911 c = build_tree_list (get_identifier ("omp declare simd"), cl);
34912 TREE_CHAIN (c) = attrs;
34913 if (processing_template_decl)
34914 ATTR_IS_DEPENDENT (c) = 1;
34915 attrs = c;
34916 }
34917 info->fndecl_seen = true;
34918 XDELETE (parser->cilk_simd_fn_info);
34919 parser->cilk_simd_fn_info = NULL;
34920 return attrs;
34921 }
34922
34923 /* Finalize #pragma omp declare simd clauses after direct declarator has
34924 been parsed, and put that into "omp declare simd" attribute. */
34925
34926 static tree
34927 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
34928 {
34929 struct cp_token_cache *ce;
34930 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
34931 int i;
34932
34933 if (!data->error_seen && data->fndecl_seen)
34934 {
34935 error ("%<#pragma omp declare simd%> not immediately followed by "
34936 "a single function declaration or definition");
34937 data->error_seen = true;
34938 return attrs;
34939 }
34940 if (data->error_seen)
34941 return attrs;
34942
34943 FOR_EACH_VEC_ELT (data->tokens, i, ce)
34944 {
34945 tree c, cl;
34946
34947 cp_parser_push_lexer_for_tokens (parser, ce);
34948 parser->lexer->in_pragma = true;
34949 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
34950 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
34951 cp_lexer_consume_token (parser->lexer);
34952 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
34953 "#pragma omp declare simd", pragma_tok);
34954 cp_parser_pop_lexer (parser);
34955 if (cl)
34956 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
34957 c = build_tree_list (get_identifier ("omp declare simd"), cl);
34958 TREE_CHAIN (c) = attrs;
34959 if (processing_template_decl)
34960 ATTR_IS_DEPENDENT (c) = 1;
34961 attrs = c;
34962 }
34963
34964 data->fndecl_seen = true;
34965 return attrs;
34966 }
34967
34968
34969 /* OpenMP 4.0:
34970 # pragma omp declare target new-line
34971 declarations and definitions
34972 # pragma omp end declare target new-line
34973
34974 OpenMP 4.5:
34975 # pragma omp declare target ( extended-list ) new-line
34976
34977 # pragma omp declare target declare-target-clauses[seq] new-line */
34978
34979 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
34980 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
34981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
34982
34983 static void
34984 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
34985 {
34986 tree clauses = NULL_TREE;
34987 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34988 clauses
34989 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
34990 "#pragma omp declare target", pragma_tok);
34991 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34992 {
34993 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34994 clauses);
34995 clauses = finish_omp_clauses (clauses, true);
34996 cp_parser_require_pragma_eol (parser, pragma_tok);
34997 }
34998 else
34999 {
35000 cp_parser_require_pragma_eol (parser, pragma_tok);
35001 scope_chain->omp_declare_target_attribute++;
35002 return;
35003 }
35004 if (scope_chain->omp_declare_target_attribute)
35005 error_at (pragma_tok->location,
35006 "%<#pragma omp declare target%> with clauses in between "
35007 "%<#pragma omp declare target%> without clauses and "
35008 "%<#pragma omp end declare target%>");
35009 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
35010 {
35011 tree t = OMP_CLAUSE_DECL (c), id;
35012 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
35013 tree at2 = lookup_attribute ("omp declare target link",
35014 DECL_ATTRIBUTES (t));
35015 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
35016 {
35017 id = get_identifier ("omp declare target link");
35018 std::swap (at1, at2);
35019 }
35020 else
35021 id = get_identifier ("omp declare target");
35022 if (at2)
35023 {
35024 error_at (OMP_CLAUSE_LOCATION (c),
35025 "%qD specified both in declare target %<link%> and %<to%>"
35026 " clauses", t);
35027 continue;
35028 }
35029 if (!at1)
35030 {
35031 symtab_node *node = symtab_node::get (t);
35032 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
35033 if (node != NULL)
35034 {
35035 node->offloadable = 1;
35036 #ifdef ENABLE_OFFLOADING
35037 g->have_offload = true;
35038 if (is_a <varpool_node *> (node))
35039 {
35040 vec_safe_push (offload_vars, t);
35041 node->force_output = 1;
35042 }
35043 #endif
35044 }
35045 }
35046 }
35047 }
35048
35049 static void
35050 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
35051 {
35052 const char *p = "";
35053 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35054 {
35055 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35056 p = IDENTIFIER_POINTER (id);
35057 }
35058 if (strcmp (p, "declare") == 0)
35059 {
35060 cp_lexer_consume_token (parser->lexer);
35061 p = "";
35062 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35063 {
35064 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35065 p = IDENTIFIER_POINTER (id);
35066 }
35067 if (strcmp (p, "target") == 0)
35068 cp_lexer_consume_token (parser->lexer);
35069 else
35070 {
35071 cp_parser_error (parser, "expected %<target%>");
35072 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35073 return;
35074 }
35075 }
35076 else
35077 {
35078 cp_parser_error (parser, "expected %<declare%>");
35079 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35080 return;
35081 }
35082 cp_parser_require_pragma_eol (parser, pragma_tok);
35083 if (!scope_chain->omp_declare_target_attribute)
35084 error_at (pragma_tok->location,
35085 "%<#pragma omp end declare target%> without corresponding "
35086 "%<#pragma omp declare target%>");
35087 else
35088 scope_chain->omp_declare_target_attribute--;
35089 }
35090
35091 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
35092 expression and optional initializer clause of
35093 #pragma omp declare reduction. We store the expression(s) as
35094 either 3, 6 or 7 special statements inside of the artificial function's
35095 body. The first two statements are DECL_EXPRs for the artificial
35096 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
35097 expression that uses those variables.
35098 If there was any INITIALIZER clause, this is followed by further statements,
35099 the fourth and fifth statements are DECL_EXPRs for the artificial
35100 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
35101 constructor variant (first token after open paren is not omp_priv),
35102 then the sixth statement is a statement with the function call expression
35103 that uses the OMP_PRIV and optionally OMP_ORIG variable.
35104 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
35105 to initialize the OMP_PRIV artificial variable and there is seventh
35106 statement, a DECL_EXPR of the OMP_PRIV statement again. */
35107
35108 static bool
35109 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
35110 {
35111 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
35112 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
35113 type = TREE_TYPE (type);
35114 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
35115 DECL_ARTIFICIAL (omp_out) = 1;
35116 pushdecl (omp_out);
35117 add_decl_expr (omp_out);
35118 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
35119 DECL_ARTIFICIAL (omp_in) = 1;
35120 pushdecl (omp_in);
35121 add_decl_expr (omp_in);
35122 tree combiner;
35123 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
35124
35125 keep_next_level (true);
35126 tree block = begin_omp_structured_block ();
35127 combiner = cp_parser_expression (parser);
35128 finish_expr_stmt (combiner);
35129 block = finish_omp_structured_block (block);
35130 add_stmt (block);
35131
35132 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35133 return false;
35134
35135 const char *p = "";
35136 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35137 {
35138 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35139 p = IDENTIFIER_POINTER (id);
35140 }
35141
35142 if (strcmp (p, "initializer") == 0)
35143 {
35144 cp_lexer_consume_token (parser->lexer);
35145 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35146 return false;
35147
35148 p = "";
35149 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35150 {
35151 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35152 p = IDENTIFIER_POINTER (id);
35153 }
35154
35155 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
35156 DECL_ARTIFICIAL (omp_priv) = 1;
35157 pushdecl (omp_priv);
35158 add_decl_expr (omp_priv);
35159 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
35160 DECL_ARTIFICIAL (omp_orig) = 1;
35161 pushdecl (omp_orig);
35162 add_decl_expr (omp_orig);
35163
35164 keep_next_level (true);
35165 block = begin_omp_structured_block ();
35166
35167 bool ctor = false;
35168 if (strcmp (p, "omp_priv") == 0)
35169 {
35170 bool is_direct_init, is_non_constant_init;
35171 ctor = true;
35172 cp_lexer_consume_token (parser->lexer);
35173 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
35174 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
35175 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
35176 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
35177 == CPP_CLOSE_PAREN
35178 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
35179 == CPP_CLOSE_PAREN))
35180 {
35181 finish_omp_structured_block (block);
35182 error ("invalid initializer clause");
35183 return false;
35184 }
35185 initializer = cp_parser_initializer (parser, &is_direct_init,
35186 &is_non_constant_init);
35187 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
35188 NULL_TREE, LOOKUP_ONLYCONVERTING);
35189 }
35190 else
35191 {
35192 cp_parser_parse_tentatively (parser);
35193 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
35194 /*check_dependency_p=*/true,
35195 /*template_p=*/NULL,
35196 /*declarator_p=*/false,
35197 /*optional_p=*/false);
35198 vec<tree, va_gc> *args;
35199 if (fn_name == error_mark_node
35200 || cp_parser_error_occurred (parser)
35201 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
35202 || ((args = cp_parser_parenthesized_expression_list
35203 (parser, non_attr, /*cast_p=*/false,
35204 /*allow_expansion_p=*/true,
35205 /*non_constant_p=*/NULL)),
35206 cp_parser_error_occurred (parser)))
35207 {
35208 finish_omp_structured_block (block);
35209 cp_parser_abort_tentative_parse (parser);
35210 cp_parser_error (parser, "expected id-expression (arguments)");
35211 return false;
35212 }
35213 unsigned int i;
35214 tree arg;
35215 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
35216 if (arg == omp_priv
35217 || (TREE_CODE (arg) == ADDR_EXPR
35218 && TREE_OPERAND (arg, 0) == omp_priv))
35219 break;
35220 cp_parser_abort_tentative_parse (parser);
35221 if (arg == NULL_TREE)
35222 error ("one of the initializer call arguments should be %<omp_priv%>"
35223 " or %<&omp_priv%>");
35224 initializer = cp_parser_postfix_expression (parser, false, false, false,
35225 false, NULL);
35226 finish_expr_stmt (initializer);
35227 }
35228
35229 block = finish_omp_structured_block (block);
35230 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
35231 add_stmt (block);
35232
35233 if (ctor)
35234 add_decl_expr (omp_orig);
35235
35236 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35237 return false;
35238 }
35239
35240 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
35241 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
35242
35243 return true;
35244 }
35245
35246 /* OpenMP 4.0
35247 #pragma omp declare reduction (reduction-id : typename-list : expression) \
35248 initializer-clause[opt] new-line
35249
35250 initializer-clause:
35251 initializer (omp_priv initializer)
35252 initializer (function-name (argument-list)) */
35253
35254 static void
35255 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
35256 enum pragma_context)
35257 {
35258 auto_vec<tree> types;
35259 enum tree_code reduc_code = ERROR_MARK;
35260 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
35261 unsigned int i;
35262 cp_token *first_token;
35263 cp_token_cache *cp;
35264 int errs;
35265 void *p;
35266
35267 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
35268 p = obstack_alloc (&declarator_obstack, 0);
35269
35270 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35271 goto fail;
35272
35273 switch (cp_lexer_peek_token (parser->lexer)->type)
35274 {
35275 case CPP_PLUS:
35276 reduc_code = PLUS_EXPR;
35277 break;
35278 case CPP_MULT:
35279 reduc_code = MULT_EXPR;
35280 break;
35281 case CPP_MINUS:
35282 reduc_code = MINUS_EXPR;
35283 break;
35284 case CPP_AND:
35285 reduc_code = BIT_AND_EXPR;
35286 break;
35287 case CPP_XOR:
35288 reduc_code = BIT_XOR_EXPR;
35289 break;
35290 case CPP_OR:
35291 reduc_code = BIT_IOR_EXPR;
35292 break;
35293 case CPP_AND_AND:
35294 reduc_code = TRUTH_ANDIF_EXPR;
35295 break;
35296 case CPP_OR_OR:
35297 reduc_code = TRUTH_ORIF_EXPR;
35298 break;
35299 case CPP_NAME:
35300 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
35301 break;
35302 default:
35303 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
35304 "%<|%>, %<&&%>, %<||%> or identifier");
35305 goto fail;
35306 }
35307
35308 if (reduc_code != ERROR_MARK)
35309 cp_lexer_consume_token (parser->lexer);
35310
35311 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
35312 if (reduc_id == error_mark_node)
35313 goto fail;
35314
35315 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35316 goto fail;
35317
35318 /* Types may not be defined in declare reduction type list. */
35319 const char *saved_message;
35320 saved_message = parser->type_definition_forbidden_message;
35321 parser->type_definition_forbidden_message
35322 = G_("types may not be defined in declare reduction type list");
35323 bool saved_colon_corrects_to_scope_p;
35324 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
35325 parser->colon_corrects_to_scope_p = false;
35326 bool saved_colon_doesnt_start_class_def_p;
35327 saved_colon_doesnt_start_class_def_p
35328 = parser->colon_doesnt_start_class_def_p;
35329 parser->colon_doesnt_start_class_def_p = true;
35330
35331 while (true)
35332 {
35333 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35334 type = cp_parser_type_id (parser);
35335 if (type == error_mark_node)
35336 ;
35337 else if (ARITHMETIC_TYPE_P (type)
35338 && (orig_reduc_id == NULL_TREE
35339 || (TREE_CODE (type) != COMPLEX_TYPE
35340 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
35341 "min") == 0
35342 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
35343 "max") == 0))))
35344 error_at (loc, "predeclared arithmetic type %qT in "
35345 "%<#pragma omp declare reduction%>", type);
35346 else if (TREE_CODE (type) == FUNCTION_TYPE
35347 || TREE_CODE (type) == METHOD_TYPE
35348 || TREE_CODE (type) == ARRAY_TYPE)
35349 error_at (loc, "function or array type %qT in "
35350 "%<#pragma omp declare reduction%>", type);
35351 else if (TREE_CODE (type) == REFERENCE_TYPE)
35352 error_at (loc, "reference type %qT in "
35353 "%<#pragma omp declare reduction%>", type);
35354 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
35355 error_at (loc, "const, volatile or __restrict qualified type %qT in "
35356 "%<#pragma omp declare reduction%>", type);
35357 else
35358 types.safe_push (type);
35359
35360 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35361 cp_lexer_consume_token (parser->lexer);
35362 else
35363 break;
35364 }
35365
35366 /* Restore the saved message. */
35367 parser->type_definition_forbidden_message = saved_message;
35368 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
35369 parser->colon_doesnt_start_class_def_p
35370 = saved_colon_doesnt_start_class_def_p;
35371
35372 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
35373 || types.is_empty ())
35374 {
35375 fail:
35376 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35377 goto done;
35378 }
35379
35380 first_token = cp_lexer_peek_token (parser->lexer);
35381 cp = NULL;
35382 errs = errorcount;
35383 FOR_EACH_VEC_ELT (types, i, type)
35384 {
35385 tree fntype
35386 = build_function_type_list (void_type_node,
35387 cp_build_reference_type (type, false),
35388 NULL_TREE);
35389 tree this_reduc_id = reduc_id;
35390 if (!dependent_type_p (type))
35391 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
35392 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
35393 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
35394 DECL_ARTIFICIAL (fndecl) = 1;
35395 DECL_EXTERNAL (fndecl) = 1;
35396 DECL_DECLARED_INLINE_P (fndecl) = 1;
35397 DECL_IGNORED_P (fndecl) = 1;
35398 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
35399 DECL_ATTRIBUTES (fndecl)
35400 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
35401 DECL_ATTRIBUTES (fndecl));
35402 if (processing_template_decl)
35403 fndecl = push_template_decl (fndecl);
35404 bool block_scope = false;
35405 tree block = NULL_TREE;
35406 if (current_function_decl)
35407 {
35408 block_scope = true;
35409 DECL_CONTEXT (fndecl) = global_namespace;
35410 if (!processing_template_decl)
35411 pushdecl (fndecl);
35412 }
35413 else if (current_class_type)
35414 {
35415 if (cp == NULL)
35416 {
35417 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35418 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35419 cp_lexer_consume_token (parser->lexer);
35420 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35421 goto fail;
35422 cp = cp_token_cache_new (first_token,
35423 cp_lexer_peek_nth_token (parser->lexer,
35424 2));
35425 }
35426 DECL_STATIC_FUNCTION_P (fndecl) = 1;
35427 finish_member_declaration (fndecl);
35428 DECL_PENDING_INLINE_INFO (fndecl) = cp;
35429 DECL_PENDING_INLINE_P (fndecl) = 1;
35430 vec_safe_push (unparsed_funs_with_definitions, fndecl);
35431 continue;
35432 }
35433 else
35434 {
35435 DECL_CONTEXT (fndecl) = current_namespace;
35436 pushdecl (fndecl);
35437 }
35438 if (!block_scope)
35439 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
35440 else
35441 block = begin_omp_structured_block ();
35442 if (cp)
35443 {
35444 cp_parser_push_lexer_for_tokens (parser, cp);
35445 parser->lexer->in_pragma = true;
35446 }
35447 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
35448 {
35449 if (!block_scope)
35450 finish_function (0);
35451 else
35452 DECL_CONTEXT (fndecl) = current_function_decl;
35453 if (cp)
35454 cp_parser_pop_lexer (parser);
35455 goto fail;
35456 }
35457 if (cp)
35458 cp_parser_pop_lexer (parser);
35459 if (!block_scope)
35460 finish_function (0);
35461 else
35462 {
35463 DECL_CONTEXT (fndecl) = current_function_decl;
35464 block = finish_omp_structured_block (block);
35465 if (TREE_CODE (block) == BIND_EXPR)
35466 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
35467 else if (TREE_CODE (block) == STATEMENT_LIST)
35468 DECL_SAVED_TREE (fndecl) = block;
35469 if (processing_template_decl)
35470 add_decl_expr (fndecl);
35471 }
35472 cp_check_omp_declare_reduction (fndecl);
35473 if (cp == NULL && types.length () > 1)
35474 cp = cp_token_cache_new (first_token,
35475 cp_lexer_peek_nth_token (parser->lexer, 2));
35476 if (errs != errorcount)
35477 break;
35478 }
35479
35480 cp_parser_require_pragma_eol (parser, pragma_tok);
35481
35482 done:
35483 /* Free any declarators allocated. */
35484 obstack_free (&declarator_obstack, p);
35485 }
35486
35487 /* OpenMP 4.0
35488 #pragma omp declare simd declare-simd-clauses[optseq] new-line
35489 #pragma omp declare reduction (reduction-id : typename-list : expression) \
35490 initializer-clause[opt] new-line
35491 #pragma omp declare target new-line */
35492
35493 static void
35494 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
35495 enum pragma_context context)
35496 {
35497 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35498 {
35499 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35500 const char *p = IDENTIFIER_POINTER (id);
35501
35502 if (strcmp (p, "simd") == 0)
35503 {
35504 cp_lexer_consume_token (parser->lexer);
35505 cp_parser_omp_declare_simd (parser, pragma_tok,
35506 context);
35507 return;
35508 }
35509 cp_ensure_no_omp_declare_simd (parser);
35510 if (strcmp (p, "reduction") == 0)
35511 {
35512 cp_lexer_consume_token (parser->lexer);
35513 cp_parser_omp_declare_reduction (parser, pragma_tok,
35514 context);
35515 return;
35516 }
35517 if (!flag_openmp) /* flag_openmp_simd */
35518 {
35519 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35520 return;
35521 }
35522 if (strcmp (p, "target") == 0)
35523 {
35524 cp_lexer_consume_token (parser->lexer);
35525 cp_parser_omp_declare_target (parser, pragma_tok);
35526 return;
35527 }
35528 }
35529 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
35530 "or %<target%>");
35531 cp_parser_require_pragma_eol (parser, pragma_tok);
35532 }
35533
35534 /* OpenMP 4.5:
35535 #pragma omp taskloop taskloop-clause[optseq] new-line
35536 for-loop
35537
35538 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
35539 for-loop */
35540
35541 #define OMP_TASKLOOP_CLAUSE_MASK \
35542 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
35548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
35549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
35550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
35555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35556
35557 static tree
35558 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
35559 char *p_name, omp_clause_mask mask, tree *cclauses)
35560 {
35561 tree clauses, sb, ret;
35562 unsigned int save;
35563 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35564
35565 strcat (p_name, " taskloop");
35566 mask |= OMP_TASKLOOP_CLAUSE_MASK;
35567
35568 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35569 {
35570 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35571 const char *p = IDENTIFIER_POINTER (id);
35572
35573 if (strcmp (p, "simd") == 0)
35574 {
35575 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35576 if (cclauses == NULL)
35577 cclauses = cclauses_buf;
35578
35579 cp_lexer_consume_token (parser->lexer);
35580 if (!flag_openmp) /* flag_openmp_simd */
35581 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35582 cclauses);
35583 sb = begin_omp_structured_block ();
35584 save = cp_parser_begin_omp_structured_block (parser);
35585 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35586 cclauses);
35587 cp_parser_end_omp_structured_block (parser, save);
35588 tree body = finish_omp_structured_block (sb);
35589 if (ret == NULL)
35590 return ret;
35591 ret = make_node (OMP_TASKLOOP);
35592 TREE_TYPE (ret) = void_type_node;
35593 OMP_FOR_BODY (ret) = body;
35594 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
35595 SET_EXPR_LOCATION (ret, loc);
35596 add_stmt (ret);
35597 return ret;
35598 }
35599 }
35600 if (!flag_openmp) /* flag_openmp_simd */
35601 {
35602 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35603 return NULL_TREE;
35604 }
35605
35606 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35607 cclauses == NULL);
35608 if (cclauses)
35609 {
35610 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
35611 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
35612 }
35613
35614 sb = begin_omp_structured_block ();
35615 save = cp_parser_begin_omp_structured_block (parser);
35616
35617 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses);
35618
35619 cp_parser_end_omp_structured_block (parser, save);
35620 add_stmt (finish_omp_structured_block (sb));
35621
35622 return ret;
35623 }
35624
35625
35626 /* OpenACC 2.0:
35627 # pragma acc routine oacc-routine-clause[optseq] new-line
35628 function-definition
35629
35630 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
35631 */
35632
35633 #define OACC_ROUTINE_CLAUSE_MASK \
35634 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
35635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
35636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
35637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
35638
35639 /* Finalize #pragma acc routine clauses after direct declarator has
35640 been parsed, and put that into "omp declare target" attribute. */
35641
35642 static void
35643 cp_parser_finish_oacc_routine (cp_parser *ARG_UNUSED (parser), tree fndecl,
35644 tree clauses, bool named, bool is_defn,
35645 bool first)
35646 {
35647 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE (clauses));
35648
35649 if (named && fndecl && is_overloaded_fn (fndecl)
35650 && (TREE_CODE (fndecl) != FUNCTION_DECL
35651 || DECL_FUNCTION_TEMPLATE_P (fndecl)))
35652 {
35653 error_at (loc, "%<#pragma acc routine%> names a set of overloads");
35654 return;
35655 }
35656
35657 if (!fndecl || TREE_CODE (fndecl) != FUNCTION_DECL
35658 || (!named && !first))
35659 {
35660 error_at (loc, "%<#pragma acc routine%> %s",
35661 named ? "does not refer to a function"
35662 : "not followed by single function");
35663 return;
35664 }
35665
35666 /* Perhaps we should use the same rule as declarations in different
35667 namespaces? */
35668 if (named && !DECL_NAMESPACE_SCOPE_P (fndecl))
35669 {
35670 error_at (loc, "%<#pragma acc routine%> does not refer to a"
35671 " namespace scope function");
35672 return;
35673 }
35674
35675 if (get_oacc_fn_attrib (fndecl))
35676 error_at (loc, "%<#pragma acc routine%> already applied to %D", fndecl);
35677
35678 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
35679 error_at (OMP_CLAUSE_LOCATION (TREE_PURPOSE (clauses)),
35680 "%<#pragma acc routine%> must be applied before %s",
35681 TREE_USED (fndecl) ? "use" : "definition");
35682
35683 /* Process for function attrib */
35684 tree dims = build_oacc_routine_dims (TREE_VALUE (clauses));
35685 replace_oacc_fn_attrib (fndecl, dims);
35686
35687 /* Also attach as a declare. */
35688 DECL_ATTRIBUTES (fndecl)
35689 = tree_cons (get_identifier ("omp declare target"),
35690 clauses, DECL_ATTRIBUTES (fndecl));
35691 }
35692
35693 /* Parse the OpenACC routine pragma. This has an optional '( name )'
35694 component, which must resolve to a declared namespace-scope
35695 function. The clauses are either processed directly (for a named
35696 function), or defered until the immediatley following declaration
35697 is parsed. */
35698
35699 static void
35700 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
35701 enum pragma_context context)
35702 {
35703 tree decl = NULL_TREE;
35704 /* Create a dummy claue, to record location. */
35705 tree c_head = build_omp_clause (pragma_tok->location, OMP_CLAUSE_SEQ);
35706
35707 if (context != pragma_external)
35708 cp_parser_error (parser, "%<#pragma acc routine%> not at file scope");
35709
35710 /* Look for optional '( name )'. */
35711 if (cp_lexer_next_token_is (parser->lexer,CPP_OPEN_PAREN))
35712 {
35713 cp_lexer_consume_token (parser->lexer);
35714 cp_token *token = cp_lexer_peek_token (parser->lexer);
35715
35716 /* We parse the name as an id-expression. If it resolves to
35717 anything other than a non-overloaded function at namespace
35718 scope, it's an error. */
35719 tree id = cp_parser_id_expression (parser,
35720 /*template_keyword_p=*/false,
35721 /*check_dependency_p=*/false,
35722 /*template_p=*/NULL,
35723 /*declarator_p=*/false,
35724 /*optional_p=*/false);
35725 decl = cp_parser_lookup_name_simple (parser, id, token->location);
35726 if (id != error_mark_node && decl == error_mark_node)
35727 cp_parser_name_lookup_error (parser, id, decl, NLE_NULL,
35728 token->location);
35729
35730 if (decl == error_mark_node
35731 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35732 {
35733 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35734 return;
35735 }
35736 }
35737
35738 /* Build a chain of clauses. */
35739 parser->lexer->in_pragma = true;
35740 tree clauses = NULL_TREE;
35741 clauses = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
35742 "#pragma acc routine",
35743 cp_lexer_peek_token (parser->lexer));
35744
35745 /* Force clauses to be non-null, by attaching context to it. */
35746 clauses = tree_cons (c_head, clauses, NULL_TREE);
35747
35748 if (decl)
35749 cp_parser_finish_oacc_routine (parser, decl, clauses, true, false, 0);
35750 else
35751 parser->oacc_routine = clauses;
35752 }
35753
35754 /* Apply any saved OpenACC routine clauses to a just-parsed
35755 declaration. */
35756
35757 static void
35758 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn,
35759 bool first)
35760 {
35761 if (parser->oacc_routine)
35762 cp_parser_finish_oacc_routine (parser, fndecl, parser->oacc_routine,
35763 false, is_defn, first);
35764 }
35765
35766 /* Main entry point to OpenMP statement pragmas. */
35767
35768 static void
35769 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
35770 {
35771 tree stmt;
35772 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
35773 omp_clause_mask mask (0);
35774
35775 switch (pragma_tok->pragma_kind)
35776 {
35777 case PRAGMA_OACC_ATOMIC:
35778 cp_parser_omp_atomic (parser, pragma_tok);
35779 return;
35780 case PRAGMA_OACC_CACHE:
35781 stmt = cp_parser_oacc_cache (parser, pragma_tok);
35782 break;
35783 case PRAGMA_OACC_DATA:
35784 stmt = cp_parser_oacc_data (parser, pragma_tok);
35785 break;
35786 case PRAGMA_OACC_ENTER_DATA:
35787 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
35788 break;
35789 case PRAGMA_OACC_EXIT_DATA:
35790 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
35791 break;
35792 case PRAGMA_OACC_KERNELS:
35793 case PRAGMA_OACC_PARALLEL:
35794 strcpy (p_name, "#pragma acc");
35795 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name);
35796 break;
35797 case PRAGMA_OACC_LOOP:
35798 strcpy (p_name, "#pragma acc");
35799 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL);
35800 break;
35801 case PRAGMA_OACC_UPDATE:
35802 stmt = cp_parser_oacc_update (parser, pragma_tok);
35803 break;
35804 case PRAGMA_OACC_WAIT:
35805 stmt = cp_parser_oacc_wait (parser, pragma_tok);
35806 break;
35807 case PRAGMA_OMP_ATOMIC:
35808 cp_parser_omp_atomic (parser, pragma_tok);
35809 return;
35810 case PRAGMA_OMP_CRITICAL:
35811 stmt = cp_parser_omp_critical (parser, pragma_tok);
35812 break;
35813 case PRAGMA_OMP_DISTRIBUTE:
35814 strcpy (p_name, "#pragma omp");
35815 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
35816 break;
35817 case PRAGMA_OMP_FOR:
35818 strcpy (p_name, "#pragma omp");
35819 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
35820 break;
35821 case PRAGMA_OMP_MASTER:
35822 stmt = cp_parser_omp_master (parser, pragma_tok);
35823 break;
35824 case PRAGMA_OMP_PARALLEL:
35825 strcpy (p_name, "#pragma omp");
35826 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
35827 break;
35828 case PRAGMA_OMP_SECTIONS:
35829 strcpy (p_name, "#pragma omp");
35830 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
35831 break;
35832 case PRAGMA_OMP_SIMD:
35833 strcpy (p_name, "#pragma omp");
35834 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
35835 break;
35836 case PRAGMA_OMP_SINGLE:
35837 stmt = cp_parser_omp_single (parser, pragma_tok);
35838 break;
35839 case PRAGMA_OMP_TASK:
35840 stmt = cp_parser_omp_task (parser, pragma_tok);
35841 break;
35842 case PRAGMA_OMP_TASKGROUP:
35843 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
35844 break;
35845 case PRAGMA_OMP_TASKLOOP:
35846 strcpy (p_name, "#pragma omp");
35847 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL);
35848 break;
35849 case PRAGMA_OMP_TEAMS:
35850 strcpy (p_name, "#pragma omp");
35851 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
35852 break;
35853 default:
35854 gcc_unreachable ();
35855 }
35856
35857 protected_set_expr_location (stmt, pragma_tok->location);
35858 }
35859 \f
35860 /* Transactional Memory parsing routines. */
35861
35862 /* Parse a transaction attribute.
35863
35864 txn-attribute:
35865 attribute
35866 [ [ identifier ] ]
35867
35868 We use this instead of cp_parser_attributes_opt for transactions to avoid
35869 the pedwarn in C++98 mode. */
35870
35871 static tree
35872 cp_parser_txn_attribute_opt (cp_parser *parser)
35873 {
35874 cp_token *token;
35875 tree attr_name, attr = NULL;
35876
35877 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
35878 return cp_parser_attributes_opt (parser);
35879
35880 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
35881 return NULL_TREE;
35882 cp_lexer_consume_token (parser->lexer);
35883 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
35884 goto error1;
35885
35886 token = cp_lexer_peek_token (parser->lexer);
35887 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
35888 {
35889 token = cp_lexer_consume_token (parser->lexer);
35890
35891 attr_name = (token->type == CPP_KEYWORD
35892 /* For keywords, use the canonical spelling,
35893 not the parsed identifier. */
35894 ? ridpointers[(int) token->keyword]
35895 : token->u.value);
35896 attr = build_tree_list (attr_name, NULL_TREE);
35897 }
35898 else
35899 cp_parser_error (parser, "expected identifier");
35900
35901 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
35902 error1:
35903 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
35904 return attr;
35905 }
35906
35907 /* Parse a __transaction_atomic or __transaction_relaxed statement.
35908
35909 transaction-statement:
35910 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
35911 compound-statement
35912 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
35913 */
35914
35915 static tree
35916 cp_parser_transaction (cp_parser *parser, cp_token *token)
35917 {
35918 unsigned char old_in = parser->in_transaction;
35919 unsigned char this_in = 1, new_in;
35920 enum rid keyword = token->keyword;
35921 tree stmt, attrs, noex;
35922
35923 cp_lexer_consume_token (parser->lexer);
35924
35925 if (keyword == RID_TRANSACTION_RELAXED
35926 || keyword == RID_SYNCHRONIZED)
35927 this_in |= TM_STMT_ATTR_RELAXED;
35928 else
35929 {
35930 attrs = cp_parser_txn_attribute_opt (parser);
35931 if (attrs)
35932 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
35933 }
35934
35935 /* Parse a noexcept specification. */
35936 if (keyword == RID_ATOMIC_NOEXCEPT)
35937 noex = boolean_true_node;
35938 else if (keyword == RID_ATOMIC_CANCEL)
35939 {
35940 /* cancel-and-throw is unimplemented. */
35941 sorry ("atomic_cancel");
35942 noex = NULL_TREE;
35943 }
35944 else
35945 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
35946
35947 /* Keep track if we're in the lexical scope of an outer transaction. */
35948 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
35949
35950 stmt = begin_transaction_stmt (token->location, NULL, this_in);
35951
35952 parser->in_transaction = new_in;
35953 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
35954 parser->in_transaction = old_in;
35955
35956 finish_transaction_stmt (stmt, NULL, this_in, noex);
35957
35958 return stmt;
35959 }
35960
35961 /* Parse a __transaction_atomic or __transaction_relaxed expression.
35962
35963 transaction-expression:
35964 __transaction_atomic txn-noexcept-spec[opt] ( expression )
35965 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
35966 */
35967
35968 static tree
35969 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
35970 {
35971 unsigned char old_in = parser->in_transaction;
35972 unsigned char this_in = 1;
35973 cp_token *token;
35974 tree expr, noex;
35975 bool noex_expr;
35976
35977 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
35978 || keyword == RID_TRANSACTION_RELAXED);
35979
35980 if (!flag_tm)
35981 error (keyword == RID_TRANSACTION_RELAXED
35982 ? G_("%<__transaction_relaxed%> without transactional memory "
35983 "support enabled")
35984 : G_("%<__transaction_atomic%> without transactional memory "
35985 "support enabled"));
35986
35987 token = cp_parser_require_keyword (parser, keyword,
35988 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
35989 : RT_TRANSACTION_RELAXED));
35990 gcc_assert (token != NULL);
35991
35992 if (keyword == RID_TRANSACTION_RELAXED)
35993 this_in |= TM_STMT_ATTR_RELAXED;
35994
35995 /* Set this early. This might mean that we allow transaction_cancel in
35996 an expression that we find out later actually has to be a constexpr.
35997 However, we expect that cxx_constant_value will be able to deal with
35998 this; also, if the noexcept has no constexpr, then what we parse next
35999 really is a transaction's body. */
36000 parser->in_transaction = this_in;
36001
36002 /* Parse a noexcept specification. */
36003 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
36004 true);
36005
36006 if (!noex || !noex_expr
36007 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36008 {
36009 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
36010
36011 expr = cp_parser_expression (parser);
36012 expr = finish_parenthesized_expr (expr);
36013
36014 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
36015 }
36016 else
36017 {
36018 /* The only expression that is available got parsed for the noexcept
36019 already. noexcept is true then. */
36020 expr = noex;
36021 noex = boolean_true_node;
36022 }
36023
36024 expr = build_transaction_expr (token->location, expr, this_in, noex);
36025 parser->in_transaction = old_in;
36026
36027 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
36028 return error_mark_node;
36029
36030 return (flag_tm ? expr : error_mark_node);
36031 }
36032
36033 /* Parse a function-transaction-block.
36034
36035 function-transaction-block:
36036 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
36037 function-body
36038 __transaction_atomic txn-attribute[opt] function-try-block
36039 __transaction_relaxed ctor-initializer[opt] function-body
36040 __transaction_relaxed function-try-block
36041 */
36042
36043 static bool
36044 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
36045 {
36046 unsigned char old_in = parser->in_transaction;
36047 unsigned char new_in = 1;
36048 tree compound_stmt, stmt, attrs;
36049 bool ctor_initializer_p;
36050 cp_token *token;
36051
36052 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36053 || keyword == RID_TRANSACTION_RELAXED);
36054 token = cp_parser_require_keyword (parser, keyword,
36055 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
36056 : RT_TRANSACTION_RELAXED));
36057 gcc_assert (token != NULL);
36058
36059 if (keyword == RID_TRANSACTION_RELAXED)
36060 new_in |= TM_STMT_ATTR_RELAXED;
36061 else
36062 {
36063 attrs = cp_parser_txn_attribute_opt (parser);
36064 if (attrs)
36065 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36066 }
36067
36068 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
36069
36070 parser->in_transaction = new_in;
36071
36072 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
36073 ctor_initializer_p = cp_parser_function_try_block (parser);
36074 else
36075 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
36076 (parser, /*in_function_try_block=*/false);
36077
36078 parser->in_transaction = old_in;
36079
36080 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
36081
36082 return ctor_initializer_p;
36083 }
36084
36085 /* Parse a __transaction_cancel statement.
36086
36087 cancel-statement:
36088 __transaction_cancel txn-attribute[opt] ;
36089 __transaction_cancel txn-attribute[opt] throw-expression ;
36090
36091 ??? Cancel and throw is not yet implemented. */
36092
36093 static tree
36094 cp_parser_transaction_cancel (cp_parser *parser)
36095 {
36096 cp_token *token;
36097 bool is_outer = false;
36098 tree stmt, attrs;
36099
36100 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
36101 RT_TRANSACTION_CANCEL);
36102 gcc_assert (token != NULL);
36103
36104 attrs = cp_parser_txn_attribute_opt (parser);
36105 if (attrs)
36106 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
36107
36108 /* ??? Parse cancel-and-throw here. */
36109
36110 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36111
36112 if (!flag_tm)
36113 {
36114 error_at (token->location, "%<__transaction_cancel%> without "
36115 "transactional memory support enabled");
36116 return error_mark_node;
36117 }
36118 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
36119 {
36120 error_at (token->location, "%<__transaction_cancel%> within a "
36121 "%<__transaction_relaxed%>");
36122 return error_mark_node;
36123 }
36124 else if (is_outer)
36125 {
36126 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
36127 && !is_tm_may_cancel_outer (current_function_decl))
36128 {
36129 error_at (token->location, "outer %<__transaction_cancel%> not "
36130 "within outer %<__transaction_atomic%>");
36131 error_at (token->location,
36132 " or a %<transaction_may_cancel_outer%> function");
36133 return error_mark_node;
36134 }
36135 }
36136 else if (parser->in_transaction == 0)
36137 {
36138 error_at (token->location, "%<__transaction_cancel%> not within "
36139 "%<__transaction_atomic%>");
36140 return error_mark_node;
36141 }
36142
36143 stmt = build_tm_abort_call (token->location, is_outer);
36144 add_stmt (stmt);
36145
36146 return stmt;
36147 }
36148 \f
36149 /* The parser. */
36150
36151 static GTY (()) cp_parser *the_parser;
36152
36153 \f
36154 /* Special handling for the first token or line in the file. The first
36155 thing in the file might be #pragma GCC pch_preprocess, which loads a
36156 PCH file, which is a GC collection point. So we need to handle this
36157 first pragma without benefit of an existing lexer structure.
36158
36159 Always returns one token to the caller in *FIRST_TOKEN. This is
36160 either the true first token of the file, or the first token after
36161 the initial pragma. */
36162
36163 static void
36164 cp_parser_initial_pragma (cp_token *first_token)
36165 {
36166 tree name = NULL;
36167
36168 cp_lexer_get_preprocessor_token (NULL, first_token);
36169 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
36170 return;
36171
36172 cp_lexer_get_preprocessor_token (NULL, first_token);
36173 if (first_token->type == CPP_STRING)
36174 {
36175 name = first_token->u.value;
36176
36177 cp_lexer_get_preprocessor_token (NULL, first_token);
36178 if (first_token->type != CPP_PRAGMA_EOL)
36179 error_at (first_token->location,
36180 "junk at end of %<#pragma GCC pch_preprocess%>");
36181 }
36182 else
36183 error_at (first_token->location, "expected string literal");
36184
36185 /* Skip to the end of the pragma. */
36186 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
36187 cp_lexer_get_preprocessor_token (NULL, first_token);
36188
36189 /* Now actually load the PCH file. */
36190 if (name)
36191 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
36192
36193 /* Read one more token to return to our caller. We have to do this
36194 after reading the PCH file in, since its pointers have to be
36195 live. */
36196 cp_lexer_get_preprocessor_token (NULL, first_token);
36197 }
36198
36199 /* Parses the grainsize pragma for the _Cilk_for statement.
36200 Syntax:
36201 #pragma cilk grainsize = <VALUE>. */
36202
36203 static void
36204 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
36205 {
36206 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
36207 {
36208 tree exp = cp_parser_binary_expression (parser, false, false,
36209 PREC_NOT_OPERATOR, NULL);
36210 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36211 if (!exp || exp == error_mark_node)
36212 {
36213 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
36214 return;
36215 }
36216
36217 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
36218 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
36219 cp_parser_cilk_for (parser, exp);
36220 else
36221 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
36222 "%<#pragma cilk grainsize%> is not followed by "
36223 "%<_Cilk_for%>");
36224 return;
36225 }
36226 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36227 }
36228
36229 /* Normal parsing of a pragma token. Here we can (and must) use the
36230 regular lexer. */
36231
36232 static bool
36233 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
36234 {
36235 cp_token *pragma_tok;
36236 unsigned int id;
36237 tree stmt;
36238 bool ret;
36239
36240 pragma_tok = cp_lexer_consume_token (parser->lexer);
36241 gcc_assert (pragma_tok->type == CPP_PRAGMA);
36242 parser->lexer->in_pragma = true;
36243
36244 id = pragma_tok->pragma_kind;
36245 if (id != PRAGMA_OMP_DECLARE_REDUCTION && id != PRAGMA_OACC_ROUTINE)
36246 cp_ensure_no_omp_declare_simd (parser);
36247 cp_ensure_no_oacc_routine (parser);
36248 switch (id)
36249 {
36250 case PRAGMA_GCC_PCH_PREPROCESS:
36251 error_at (pragma_tok->location,
36252 "%<#pragma GCC pch_preprocess%> must be first");
36253 break;
36254
36255 case PRAGMA_OMP_BARRIER:
36256 switch (context)
36257 {
36258 case pragma_compound:
36259 cp_parser_omp_barrier (parser, pragma_tok);
36260 return false;
36261 case pragma_stmt:
36262 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
36263 "used in compound statements");
36264 break;
36265 default:
36266 goto bad_stmt;
36267 }
36268 break;
36269
36270 case PRAGMA_OMP_FLUSH:
36271 switch (context)
36272 {
36273 case pragma_compound:
36274 cp_parser_omp_flush (parser, pragma_tok);
36275 return false;
36276 case pragma_stmt:
36277 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
36278 "used in compound statements");
36279 break;
36280 default:
36281 goto bad_stmt;
36282 }
36283 break;
36284
36285 case PRAGMA_OMP_TASKWAIT:
36286 switch (context)
36287 {
36288 case pragma_compound:
36289 cp_parser_omp_taskwait (parser, pragma_tok);
36290 return false;
36291 case pragma_stmt:
36292 error_at (pragma_tok->location,
36293 "%<#pragma omp taskwait%> may only be "
36294 "used in compound statements");
36295 break;
36296 default:
36297 goto bad_stmt;
36298 }
36299 break;
36300
36301 case PRAGMA_OMP_TASKYIELD:
36302 switch (context)
36303 {
36304 case pragma_compound:
36305 cp_parser_omp_taskyield (parser, pragma_tok);
36306 return false;
36307 case pragma_stmt:
36308 error_at (pragma_tok->location,
36309 "%<#pragma omp taskyield%> may only be "
36310 "used in compound statements");
36311 break;
36312 default:
36313 goto bad_stmt;
36314 }
36315 break;
36316
36317 case PRAGMA_OMP_CANCEL:
36318 switch (context)
36319 {
36320 case pragma_compound:
36321 cp_parser_omp_cancel (parser, pragma_tok);
36322 return false;
36323 case pragma_stmt:
36324 error_at (pragma_tok->location,
36325 "%<#pragma omp cancel%> may only be "
36326 "used in compound statements");
36327 break;
36328 default:
36329 goto bad_stmt;
36330 }
36331 break;
36332
36333 case PRAGMA_OMP_CANCELLATION_POINT:
36334 switch (context)
36335 {
36336 case pragma_compound:
36337 cp_parser_omp_cancellation_point (parser, pragma_tok);
36338 return false;
36339 case pragma_stmt:
36340 error_at (pragma_tok->location,
36341 "%<#pragma omp cancellation point%> may only be "
36342 "used in compound statements");
36343 break;
36344 default:
36345 goto bad_stmt;
36346 }
36347 break;
36348
36349 case PRAGMA_OMP_THREADPRIVATE:
36350 cp_parser_omp_threadprivate (parser, pragma_tok);
36351 return false;
36352
36353 case PRAGMA_OMP_DECLARE_REDUCTION:
36354 cp_parser_omp_declare (parser, pragma_tok, context);
36355 return false;
36356
36357 case PRAGMA_OACC_ROUTINE:
36358 cp_parser_oacc_routine (parser, pragma_tok, context);
36359 return false;
36360
36361 case PRAGMA_OACC_ATOMIC:
36362 case PRAGMA_OACC_CACHE:
36363 case PRAGMA_OACC_DATA:
36364 case PRAGMA_OACC_ENTER_DATA:
36365 case PRAGMA_OACC_EXIT_DATA:
36366 case PRAGMA_OACC_KERNELS:
36367 case PRAGMA_OACC_PARALLEL:
36368 case PRAGMA_OACC_LOOP:
36369 case PRAGMA_OACC_UPDATE:
36370 case PRAGMA_OACC_WAIT:
36371 case PRAGMA_OMP_ATOMIC:
36372 case PRAGMA_OMP_CRITICAL:
36373 case PRAGMA_OMP_DISTRIBUTE:
36374 case PRAGMA_OMP_FOR:
36375 case PRAGMA_OMP_MASTER:
36376 case PRAGMA_OMP_PARALLEL:
36377 case PRAGMA_OMP_SECTIONS:
36378 case PRAGMA_OMP_SIMD:
36379 case PRAGMA_OMP_SINGLE:
36380 case PRAGMA_OMP_TASK:
36381 case PRAGMA_OMP_TASKGROUP:
36382 case PRAGMA_OMP_TASKLOOP:
36383 case PRAGMA_OMP_TEAMS:
36384 if (context != pragma_stmt && context != pragma_compound)
36385 goto bad_stmt;
36386 stmt = push_omp_privatization_clauses (false);
36387 cp_parser_omp_construct (parser, pragma_tok);
36388 pop_omp_privatization_clauses (stmt);
36389 return true;
36390
36391 case PRAGMA_OMP_ORDERED:
36392 stmt = push_omp_privatization_clauses (false);
36393 ret = cp_parser_omp_ordered (parser, pragma_tok, context);
36394 pop_omp_privatization_clauses (stmt);
36395 return ret;
36396
36397 case PRAGMA_OMP_TARGET:
36398 stmt = push_omp_privatization_clauses (false);
36399 ret = cp_parser_omp_target (parser, pragma_tok, context);
36400 pop_omp_privatization_clauses (stmt);
36401 return ret;
36402
36403 case PRAGMA_OMP_END_DECLARE_TARGET:
36404 cp_parser_omp_end_declare_target (parser, pragma_tok);
36405 return false;
36406
36407 case PRAGMA_OMP_SECTION:
36408 error_at (pragma_tok->location,
36409 "%<#pragma omp section%> may only be used in "
36410 "%<#pragma omp sections%> construct");
36411 break;
36412
36413 case PRAGMA_IVDEP:
36414 {
36415 if (context == pragma_external)
36416 {
36417 error_at (pragma_tok->location,
36418 "%<#pragma GCC ivdep%> must be inside a function");
36419 break;
36420 }
36421 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36422 cp_token *tok;
36423 tok = cp_lexer_peek_token (the_parser->lexer);
36424 if (tok->type != CPP_KEYWORD
36425 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
36426 && tok->keyword != RID_DO))
36427 {
36428 cp_parser_error (parser, "for, while or do statement expected");
36429 return false;
36430 }
36431 cp_parser_iteration_statement (parser, true);
36432 return true;
36433 }
36434
36435 case PRAGMA_CILK_SIMD:
36436 if (context == pragma_external)
36437 {
36438 error_at (pragma_tok->location,
36439 "%<#pragma simd%> must be inside a function");
36440 break;
36441 }
36442 stmt = push_omp_privatization_clauses (false);
36443 cp_parser_cilk_simd (parser, pragma_tok);
36444 pop_omp_privatization_clauses (stmt);
36445 return true;
36446
36447 case PRAGMA_CILK_GRAINSIZE:
36448 if (context == pragma_external)
36449 {
36450 error_at (pragma_tok->location,
36451 "%<#pragma cilk grainsize%> must be inside a function");
36452 break;
36453 }
36454
36455 /* Ignore the pragma if Cilk Plus is not enabled. */
36456 if (flag_cilkplus)
36457 {
36458 cp_parser_cilk_grainsize (parser, pragma_tok);
36459 return true;
36460 }
36461 else
36462 {
36463 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
36464 "%<#pragma cilk grainsize%>");
36465 break;
36466 }
36467
36468 default:
36469 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
36470 c_invoke_pragma_handler (id);
36471 break;
36472
36473 bad_stmt:
36474 cp_parser_error (parser, "expected declaration specifiers");
36475 break;
36476 }
36477
36478 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36479 return false;
36480 }
36481
36482 /* The interface the pragma parsers have to the lexer. */
36483
36484 enum cpp_ttype
36485 pragma_lex (tree *value, location_t *loc)
36486 {
36487 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
36488 enum cpp_ttype ret = tok->type;
36489
36490 *value = tok->u.value;
36491 if (loc)
36492 *loc = tok->location;
36493
36494 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
36495 ret = CPP_EOF;
36496 else if (ret == CPP_STRING)
36497 *value = cp_parser_string_literal (the_parser, false, false);
36498 else
36499 {
36500 if (ret == CPP_KEYWORD)
36501 ret = CPP_NAME;
36502 cp_lexer_consume_token (the_parser->lexer);
36503 }
36504
36505 return ret;
36506 }
36507
36508 \f
36509 /* External interface. */
36510
36511 /* Parse one entire translation unit. */
36512
36513 void
36514 c_parse_file (void)
36515 {
36516 static bool already_called = false;
36517
36518 if (already_called)
36519 fatal_error (input_location,
36520 "inter-module optimizations not implemented for C++");
36521 already_called = true;
36522
36523 the_parser = cp_parser_new ();
36524 push_deferring_access_checks (flag_access_control
36525 ? dk_no_deferred : dk_no_check);
36526 cp_parser_translation_unit (the_parser);
36527 the_parser = NULL;
36528 }
36529
36530 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
36531 vectorlength clause:
36532 Syntax:
36533 vectorlength ( constant-expression ) */
36534
36535 static tree
36536 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
36537 bool is_simd_fn)
36538 {
36539 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36540 tree expr;
36541 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
36542 safelen clause. Thus, vectorlength is represented as OMP 4.0
36543 safelen. For SIMD-enabled function it is represented by OMP 4.0
36544 simdlen. */
36545 if (!is_simd_fn)
36546 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
36547 loc);
36548 else
36549 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
36550 loc);
36551
36552 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36553 return error_mark_node;
36554
36555 expr = cp_parser_constant_expression (parser);
36556 expr = maybe_constant_value (expr);
36557
36558 /* If expr == error_mark_node, then don't emit any errors nor
36559 create a clause. if any of the above functions returns
36560 error mark node then they would have emitted an error message. */
36561 if (expr == error_mark_node)
36562 ;
36563 else if (!TREE_TYPE (expr)
36564 || !TREE_CONSTANT (expr)
36565 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
36566 error_at (loc, "vectorlength must be an integer constant");
36567 else if (TREE_CONSTANT (expr)
36568 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
36569 error_at (loc, "vectorlength must be a power of 2");
36570 else
36571 {
36572 tree c;
36573 if (!is_simd_fn)
36574 {
36575 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
36576 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
36577 OMP_CLAUSE_CHAIN (c) = clauses;
36578 clauses = c;
36579 }
36580 else
36581 {
36582 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
36583 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
36584 OMP_CLAUSE_CHAIN (c) = clauses;
36585 clauses = c;
36586 }
36587 }
36588
36589 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36590 return error_mark_node;
36591 return clauses;
36592 }
36593
36594 /* Handles the Cilk Plus #pragma simd linear clause.
36595 Syntax:
36596 linear ( simd-linear-variable-list )
36597
36598 simd-linear-variable-list:
36599 simd-linear-variable
36600 simd-linear-variable-list , simd-linear-variable
36601
36602 simd-linear-variable:
36603 id-expression
36604 id-expression : simd-linear-step
36605
36606 simd-linear-step:
36607 conditional-expression */
36608
36609 static tree
36610 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
36611 {
36612 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36613
36614 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36615 return clauses;
36616 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36617 {
36618 cp_parser_error (parser, "expected identifier");
36619 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
36620 return error_mark_node;
36621 }
36622
36623 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36624 parser->colon_corrects_to_scope_p = false;
36625 while (1)
36626 {
36627 cp_token *token = cp_lexer_peek_token (parser->lexer);
36628 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36629 {
36630 cp_parser_error (parser, "expected variable-name");
36631 clauses = error_mark_node;
36632 break;
36633 }
36634
36635 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
36636 false, false);
36637 tree decl = cp_parser_lookup_name_simple (parser, var_name,
36638 token->location);
36639 if (decl == error_mark_node)
36640 {
36641 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
36642 token->location);
36643 clauses = error_mark_node;
36644 }
36645 else
36646 {
36647 tree e = NULL_TREE;
36648 tree step_size = integer_one_node;
36649
36650 /* If present, parse the linear step. Otherwise, assume the default
36651 value of 1. */
36652 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
36653 {
36654 cp_lexer_consume_token (parser->lexer);
36655
36656 e = cp_parser_assignment_expression (parser);
36657 e = maybe_constant_value (e);
36658
36659 if (e == error_mark_node)
36660 {
36661 /* If an error has occurred, then the whole pragma is
36662 considered ill-formed. Thus, no reason to keep
36663 parsing. */
36664 clauses = error_mark_node;
36665 break;
36666 }
36667 else if (type_dependent_expression_p (e)
36668 || value_dependent_expression_p (e)
36669 || (TREE_TYPE (e)
36670 && INTEGRAL_TYPE_P (TREE_TYPE (e))
36671 && (TREE_CONSTANT (e)
36672 || DECL_P (e))))
36673 step_size = e;
36674 else
36675 cp_parser_error (parser,
36676 "step size must be an integer constant "
36677 "expression or an integer variable");
36678 }
36679
36680 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
36681 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
36682 OMP_CLAUSE_DECL (l) = decl;
36683 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
36684 OMP_CLAUSE_CHAIN (l) = clauses;
36685 clauses = l;
36686 }
36687 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36688 cp_lexer_consume_token (parser->lexer);
36689 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36690 break;
36691 else
36692 {
36693 error_at (cp_lexer_peek_token (parser->lexer)->location,
36694 "expected %<,%> or %<)%> after %qE", decl);
36695 clauses = error_mark_node;
36696 break;
36697 }
36698 }
36699 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36700 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
36701 return clauses;
36702 }
36703
36704 /* Returns the name of the next clause. If the clause is not
36705 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
36706 token is not consumed. Otherwise, the appropriate enum from the
36707 pragma_simd_clause is returned and the token is consumed. */
36708
36709 static pragma_omp_clause
36710 cp_parser_cilk_simd_clause_name (cp_parser *parser)
36711 {
36712 pragma_omp_clause clause_type;
36713 cp_token *token = cp_lexer_peek_token (parser->lexer);
36714
36715 if (token->keyword == RID_PRIVATE)
36716 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
36717 else if (!token->u.value || token->type != CPP_NAME)
36718 return PRAGMA_CILK_CLAUSE_NONE;
36719 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
36720 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
36721 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
36722 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
36723 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
36724 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
36725 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
36726 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
36727 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
36728 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
36729 else
36730 return PRAGMA_CILK_CLAUSE_NONE;
36731
36732 cp_lexer_consume_token (parser->lexer);
36733 return clause_type;
36734 }
36735
36736 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
36737
36738 static tree
36739 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
36740 {
36741 tree clauses = NULL_TREE;
36742
36743 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36744 && clauses != error_mark_node)
36745 {
36746 pragma_omp_clause c_kind;
36747 c_kind = cp_parser_cilk_simd_clause_name (parser);
36748 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
36749 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
36750 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
36751 clauses = cp_parser_cilk_simd_linear (parser, clauses);
36752 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
36753 /* Use the OpenMP 4.0 equivalent function. */
36754 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
36755 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
36756 /* Use the OpenMP 4.0 equivalent function. */
36757 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
36758 clauses);
36759 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
36760 /* Use the OMP 4.0 equivalent function. */
36761 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
36762 clauses);
36763 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
36764 /* Use the OMP 4.0 equivalent function. */
36765 clauses = cp_parser_omp_clause_reduction (parser, clauses);
36766 else
36767 {
36768 clauses = error_mark_node;
36769 cp_parser_error (parser, "expected %<#pragma simd%> clause");
36770 break;
36771 }
36772 }
36773
36774 cp_parser_skip_to_pragma_eol (parser, pragma_token);
36775
36776 if (clauses == error_mark_node)
36777 return error_mark_node;
36778 else
36779 return c_finish_cilk_clauses (clauses);
36780 }
36781
36782 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
36783
36784 static void
36785 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
36786 {
36787 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
36788
36789 if (clauses == error_mark_node)
36790 return;
36791
36792 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
36793 {
36794 error_at (cp_lexer_peek_token (parser->lexer)->location,
36795 "for statement expected");
36796 return;
36797 }
36798
36799 tree sb = begin_omp_structured_block ();
36800 int save = cp_parser_begin_omp_structured_block (parser);
36801 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
36802 if (ret)
36803 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
36804 cp_parser_end_omp_structured_block (parser, save);
36805 add_stmt (finish_omp_structured_block (sb));
36806 }
36807
36808 /* Main entry-point for parsing Cilk Plus _Cilk_for
36809 loops. The return value is error_mark_node
36810 when errors happen and CILK_FOR tree on success. */
36811
36812 static tree
36813 cp_parser_cilk_for (cp_parser *parser, tree grain)
36814 {
36815 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
36816 gcc_unreachable ();
36817
36818 tree sb = begin_omp_structured_block ();
36819 int save = cp_parser_begin_omp_structured_block (parser);
36820
36821 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
36822 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
36823 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
36824 clauses = finish_omp_clauses (clauses, false);
36825
36826 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
36827 if (ret)
36828 cpp_validate_cilk_plus_loop (ret);
36829 else
36830 ret = error_mark_node;
36831
36832 cp_parser_end_omp_structured_block (parser, save);
36833 add_stmt (finish_omp_structured_block (sb));
36834 return ret;
36835 }
36836
36837 /* Create an identifier for a generic parameter type (a synthesized
36838 template parameter implied by `auto' or a concept identifier). */
36839
36840 static GTY(()) int generic_parm_count;
36841 static tree
36842 make_generic_type_name ()
36843 {
36844 char buf[32];
36845 sprintf (buf, "auto:%d", ++generic_parm_count);
36846 return get_identifier (buf);
36847 }
36848
36849 /* Predicate that behaves as is_auto_or_concept but matches the parent
36850 node of the generic type rather than the generic type itself. This
36851 allows for type transformation in add_implicit_template_parms. */
36852
36853 static inline bool
36854 tree_type_is_auto_or_concept (const_tree t)
36855 {
36856 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
36857 }
36858
36859 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
36860 (creating a new template parameter list if necessary). Returns the newly
36861 created template type parm. */
36862
36863 static tree
36864 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
36865 {
36866 gcc_assert (current_binding_level->kind == sk_function_parms);
36867
36868 /* Before committing to modifying any scope, if we're in an
36869 implicit template scope, and we're trying to synthesize a
36870 constrained parameter, try to find a previous parameter with
36871 the same name. This is the same-type rule for abbreviated
36872 function templates. */
36873 if (parser->implicit_template_scope && constr)
36874 {
36875 tree t = parser->implicit_template_parms;
36876 while (t)
36877 {
36878 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
36879 {
36880 tree d = TREE_VALUE (t);
36881 if (TREE_CODE (d) == PARM_DECL)
36882 /* Return the TEMPLATE_PARM_INDEX. */
36883 d = DECL_INITIAL (d);
36884 return d;
36885 }
36886 t = TREE_CHAIN (t);
36887 }
36888 }
36889
36890 /* We are either continuing a function template that already contains implicit
36891 template parameters, creating a new fully-implicit function template, or
36892 extending an existing explicit function template with implicit template
36893 parameters. */
36894
36895 cp_binding_level *const entry_scope = current_binding_level;
36896
36897 bool become_template = false;
36898 cp_binding_level *parent_scope = 0;
36899
36900 if (parser->implicit_template_scope)
36901 {
36902 gcc_assert (parser->implicit_template_parms);
36903
36904 current_binding_level = parser->implicit_template_scope;
36905 }
36906 else
36907 {
36908 /* Roll back to the existing template parameter scope (in the case of
36909 extending an explicit function template) or introduce a new template
36910 parameter scope ahead of the function parameter scope (or class scope
36911 in the case of out-of-line member definitions). The function scope is
36912 added back after template parameter synthesis below. */
36913
36914 cp_binding_level *scope = entry_scope;
36915
36916 while (scope->kind == sk_function_parms)
36917 {
36918 parent_scope = scope;
36919 scope = scope->level_chain;
36920 }
36921 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
36922 {
36923 /* If not defining a class, then any class scope is a scope level in
36924 an out-of-line member definition. In this case simply wind back
36925 beyond the first such scope to inject the template parameter list.
36926 Otherwise wind back to the class being defined. The latter can
36927 occur in class member friend declarations such as:
36928
36929 class A {
36930 void foo (auto);
36931 };
36932 class B {
36933 friend void A::foo (auto);
36934 };
36935
36936 The template parameter list synthesized for the friend declaration
36937 must be injected in the scope of 'B'. This can also occur in
36938 erroneous cases such as:
36939
36940 struct A {
36941 struct B {
36942 void foo (auto);
36943 };
36944 void B::foo (auto) {}
36945 };
36946
36947 Here the attempted definition of 'B::foo' within 'A' is ill-formed
36948 but, nevertheless, the template parameter list synthesized for the
36949 declarator should be injected into the scope of 'A' as if the
36950 ill-formed template was specified explicitly. */
36951
36952 while (scope->kind == sk_class && !scope->defining_class_p)
36953 {
36954 parent_scope = scope;
36955 scope = scope->level_chain;
36956 }
36957 }
36958
36959 current_binding_level = scope;
36960
36961 if (scope->kind != sk_template_parms
36962 || !function_being_declared_is_template_p (parser))
36963 {
36964 /* Introduce a new template parameter list for implicit template
36965 parameters. */
36966
36967 become_template = true;
36968
36969 parser->implicit_template_scope
36970 = begin_scope (sk_template_parms, NULL);
36971
36972 ++processing_template_decl;
36973
36974 parser->fully_implicit_function_template_p = true;
36975 ++parser->num_template_parameter_lists;
36976 }
36977 else
36978 {
36979 /* Synthesize implicit template parameters at the end of the explicit
36980 template parameter list. */
36981
36982 gcc_assert (current_template_parms);
36983
36984 parser->implicit_template_scope = scope;
36985
36986 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
36987 parser->implicit_template_parms
36988 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
36989 }
36990 }
36991
36992 /* Synthesize a new template parameter and track the current template
36993 parameter chain with implicit_template_parms. */
36994
36995 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
36996 tree synth_id = make_generic_type_name ();
36997 tree synth_tmpl_parm;
36998 bool non_type = false;
36999
37000 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
37001 synth_tmpl_parm
37002 = finish_template_type_parm (class_type_node, synth_id);
37003 else if (TREE_CODE (proto) == TEMPLATE_DECL)
37004 synth_tmpl_parm
37005 = finish_constrained_template_template_parm (proto, synth_id);
37006 else
37007 {
37008 synth_tmpl_parm = copy_decl (proto);
37009 DECL_NAME (synth_tmpl_parm) = synth_id;
37010 non_type = true;
37011 }
37012
37013 // Attach the constraint to the parm before processing.
37014 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
37015 TREE_TYPE (node) = constr;
37016 tree new_parm
37017 = process_template_parm (parser->implicit_template_parms,
37018 input_location,
37019 node,
37020 /*non_type=*/non_type,
37021 /*param_pack=*/false);
37022
37023 // Chain the new parameter to the list of implicit parameters.
37024 if (parser->implicit_template_parms)
37025 parser->implicit_template_parms
37026 = TREE_CHAIN (parser->implicit_template_parms);
37027 else
37028 parser->implicit_template_parms = new_parm;
37029
37030 tree new_decl = getdecls ();
37031 if (non_type)
37032 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
37033 new_decl = DECL_INITIAL (new_decl);
37034
37035 /* If creating a fully implicit function template, start the new implicit
37036 template parameter list with this synthesized type, otherwise grow the
37037 current template parameter list. */
37038
37039 if (become_template)
37040 {
37041 parent_scope->level_chain = current_binding_level;
37042
37043 tree new_parms = make_tree_vec (1);
37044 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
37045 current_template_parms = tree_cons (size_int (processing_template_decl),
37046 new_parms, current_template_parms);
37047 }
37048 else
37049 {
37050 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
37051 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
37052 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
37053 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
37054 }
37055
37056 // If the new parameter was constrained, we need to add that to the
37057 // constraints in the template parameter list.
37058 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
37059 {
37060 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
37061 reqs = conjoin_constraints (reqs, req);
37062 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
37063 }
37064
37065 current_binding_level = entry_scope;
37066
37067 return new_decl;
37068 }
37069
37070 /* Finish the declaration of a fully implicit function template. Such a
37071 template has no explicit template parameter list so has not been through the
37072 normal template head and tail processing. synthesize_implicit_template_parm
37073 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
37074 provided if the declaration is a class member such that its template
37075 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
37076 form is returned. Otherwise NULL_TREE is returned. */
37077
37078 static tree
37079 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
37080 {
37081 gcc_assert (parser->fully_implicit_function_template_p);
37082
37083 if (member_decl_opt && member_decl_opt != error_mark_node
37084 && DECL_VIRTUAL_P (member_decl_opt))
37085 {
37086 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
37087 "implicit templates may not be %<virtual%>");
37088 DECL_VIRTUAL_P (member_decl_opt) = false;
37089 }
37090
37091 if (member_decl_opt)
37092 member_decl_opt = finish_member_template_decl (member_decl_opt);
37093 end_template_decl ();
37094
37095 parser->fully_implicit_function_template_p = false;
37096 --parser->num_template_parameter_lists;
37097
37098 return member_decl_opt;
37099 }
37100
37101 #include "gt-cp-parser.h"