Come up with is_empty for hash_{table,map,set}.
[gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
47 #include "memmodel.h"
48
49 \f
50 /* The lexer. */
51
52 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
53 and c-lex.c) and the C++ parser. */
54
55 static cp_token eof_token =
56 {
57 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
58 };
59
60 /* The various kinds of non integral constant we encounter. */
61 enum non_integral_constant {
62 NIC_NONE,
63 /* floating-point literal */
64 NIC_FLOAT,
65 /* %<this%> */
66 NIC_THIS,
67 /* %<__FUNCTION__%> */
68 NIC_FUNC_NAME,
69 /* %<__PRETTY_FUNCTION__%> */
70 NIC_PRETTY_FUNC,
71 /* %<__func__%> */
72 NIC_C99_FUNC,
73 /* "%<va_arg%> */
74 NIC_VA_ARG,
75 /* a cast */
76 NIC_CAST,
77 /* %<typeid%> operator */
78 NIC_TYPEID,
79 /* non-constant compound literals */
80 NIC_NCC,
81 /* a function call */
82 NIC_FUNC_CALL,
83 /* an increment */
84 NIC_INC,
85 /* an decrement */
86 NIC_DEC,
87 /* an array reference */
88 NIC_ARRAY_REF,
89 /* %<->%> */
90 NIC_ARROW,
91 /* %<.%> */
92 NIC_POINT,
93 /* the address of a label */
94 NIC_ADDR_LABEL,
95 /* %<*%> */
96 NIC_STAR,
97 /* %<&%> */
98 NIC_ADDR,
99 /* %<++%> */
100 NIC_PREINCREMENT,
101 /* %<--%> */
102 NIC_PREDECREMENT,
103 /* %<new%> */
104 NIC_NEW,
105 /* %<delete%> */
106 NIC_DEL,
107 /* calls to overloaded operators */
108 NIC_OVERLOADED,
109 /* an assignment */
110 NIC_ASSIGNMENT,
111 /* a comma operator */
112 NIC_COMMA,
113 /* a call to a constructor */
114 NIC_CONSTRUCTOR,
115 /* a transaction expression */
116 NIC_TRANSACTION
117 };
118
119 /* The various kinds of errors about name-lookup failing. */
120 enum name_lookup_error {
121 /* NULL */
122 NLE_NULL,
123 /* is not a type */
124 NLE_TYPE,
125 /* is not a class or namespace */
126 NLE_CXX98,
127 /* is not a class, namespace, or enumeration */
128 NLE_NOT_CXX98
129 };
130
131 /* The various kinds of required token */
132 enum required_token {
133 RT_NONE,
134 RT_SEMICOLON, /* ';' */
135 RT_OPEN_PAREN, /* '(' */
136 RT_CLOSE_BRACE, /* '}' */
137 RT_OPEN_BRACE, /* '{' */
138 RT_CLOSE_SQUARE, /* ']' */
139 RT_OPEN_SQUARE, /* '[' */
140 RT_COMMA, /* ',' */
141 RT_SCOPE, /* '::' */
142 RT_LESS, /* '<' */
143 RT_GREATER, /* '>' */
144 RT_EQ, /* '=' */
145 RT_ELLIPSIS, /* '...' */
146 RT_MULT, /* '*' */
147 RT_COMPL, /* '~' */
148 RT_COLON, /* ':' */
149 RT_COLON_SCOPE, /* ':' or '::' */
150 RT_CLOSE_PAREN, /* ')' */
151 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
152 RT_PRAGMA_EOL, /* end of line */
153 RT_NAME, /* identifier */
154
155 /* The type is CPP_KEYWORD */
156 RT_NEW, /* new */
157 RT_DELETE, /* delete */
158 RT_RETURN, /* return */
159 RT_WHILE, /* while */
160 RT_EXTERN, /* extern */
161 RT_STATIC_ASSERT, /* static_assert */
162 RT_DECLTYPE, /* decltype */
163 RT_OPERATOR, /* operator */
164 RT_CLASS, /* class */
165 RT_TEMPLATE, /* template */
166 RT_NAMESPACE, /* namespace */
167 RT_USING, /* using */
168 RT_ASM, /* asm */
169 RT_TRY, /* try */
170 RT_CATCH, /* catch */
171 RT_THROW, /* throw */
172 RT_LABEL, /* __label__ */
173 RT_AT_TRY, /* @try */
174 RT_AT_SYNCHRONIZED, /* @synchronized */
175 RT_AT_THROW, /* @throw */
176
177 RT_SELECT, /* selection-statement */
178 RT_ITERATION, /* iteration-statement */
179 RT_JUMP, /* jump-statement */
180 RT_CLASS_KEY, /* class-key */
181 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
182 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
183 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
184 RT_TRANSACTION_CANCEL /* __transaction_cancel */
185 };
186
187 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
188 reverting it on destruction. */
189
190 class type_id_in_expr_sentinel
191 {
192 cp_parser *parser;
193 bool saved;
194 public:
195 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
196 : parser (parser),
197 saved (parser->in_type_id_in_expr_p)
198 { parser->in_type_id_in_expr_p = set; }
199 ~type_id_in_expr_sentinel ()
200 { parser->in_type_id_in_expr_p = saved; }
201 };
202
203 /* Prototypes. */
204
205 static cp_lexer *cp_lexer_new_main
206 (void);
207 static cp_lexer *cp_lexer_new_from_tokens
208 (cp_token_cache *tokens);
209 static void cp_lexer_destroy
210 (cp_lexer *);
211 static int cp_lexer_saving_tokens
212 (const cp_lexer *);
213 static cp_token *cp_lexer_token_at
214 (cp_lexer *, cp_token_position);
215 static void cp_lexer_get_preprocessor_token
216 (cp_lexer *, cp_token *);
217 static inline cp_token *cp_lexer_peek_token
218 (cp_lexer *);
219 static cp_token *cp_lexer_peek_nth_token
220 (cp_lexer *, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer *, enum rid);
227 static cp_token *cp_lexer_consume_token
228 (cp_lexer *);
229 static void cp_lexer_purge_token
230 (cp_lexer *);
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer *, cp_token_position);
233 static void cp_lexer_save_tokens
234 (cp_lexer *);
235 static void cp_lexer_commit_tokens
236 (cp_lexer *);
237 static void cp_lexer_rollback_tokens
238 (cp_lexer *);
239 static void cp_lexer_print_token
240 (FILE *, cp_token *);
241 static inline bool cp_lexer_debugging_p
242 (cp_lexer *);
243 static void cp_lexer_start_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
245 static void cp_lexer_stop_debugging
246 (cp_lexer *) ATTRIBUTE_UNUSED;
247
248 static cp_token_cache *cp_token_cache_new
249 (cp_token *, cp_token *);
250
251 static void cp_parser_initial_pragma
252 (cp_token *);
253
254 static bool cp_parser_omp_declare_reduction_exprs
255 (tree, cp_parser *);
256 static void cp_finalize_oacc_routine
257 (cp_parser *, tree, bool);
258
259 /* Manifest constants. */
260 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
261 #define CP_SAVED_TOKEN_STACK 5
262
263 /* Variables. */
264
265 /* The stream to which debugging output should be written. */
266 static FILE *cp_lexer_debug_stream;
267
268 /* Nonzero if we are parsing an unevaluated operand: an operand to
269 sizeof, typeof, or alignof. */
270 int cp_unevaluated_operand;
271
272 /* Dump up to NUM tokens in BUFFER to FILE starting with token
273 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
274 first token in BUFFER. If NUM is 0, dump all the tokens. If
275 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
276 highlighted by surrounding it in [[ ]]. */
277
278 static void
279 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
280 cp_token *start_token, unsigned num,
281 cp_token *curr_token)
282 {
283 unsigned i, nprinted;
284 cp_token *token;
285 bool do_print;
286
287 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
288
289 if (buffer == NULL)
290 return;
291
292 if (num == 0)
293 num = buffer->length ();
294
295 if (start_token == NULL)
296 start_token = buffer->address ();
297
298 if (start_token > buffer->address ())
299 {
300 cp_lexer_print_token (file, &(*buffer)[0]);
301 fprintf (file, " ... ");
302 }
303
304 do_print = false;
305 nprinted = 0;
306 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
307 {
308 if (token == start_token)
309 do_print = true;
310
311 if (!do_print)
312 continue;
313
314 nprinted++;
315 if (token == curr_token)
316 fprintf (file, "[[");
317
318 cp_lexer_print_token (file, token);
319
320 if (token == curr_token)
321 fprintf (file, "]]");
322
323 switch (token->type)
324 {
325 case CPP_SEMICOLON:
326 case CPP_OPEN_BRACE:
327 case CPP_CLOSE_BRACE:
328 case CPP_EOF:
329 fputc ('\n', file);
330 break;
331
332 default:
333 fputc (' ', file);
334 }
335 }
336
337 if (i == num && i < buffer->length ())
338 {
339 fprintf (file, " ... ");
340 cp_lexer_print_token (file, &buffer->last ());
341 }
342
343 fprintf (file, "\n");
344 }
345
346
347 /* Dump all tokens in BUFFER to stderr. */
348
349 void
350 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
351 {
352 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
353 }
354
355 DEBUG_FUNCTION void
356 debug (vec<cp_token, va_gc> &ref)
357 {
358 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
359 }
360
361 DEBUG_FUNCTION void
362 debug (vec<cp_token, va_gc> *ptr)
363 {
364 if (ptr)
365 debug (*ptr);
366 else
367 fprintf (stderr, "<nil>\n");
368 }
369
370
371 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
372 description for T. */
373
374 static void
375 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
376 {
377 if (t)
378 {
379 fprintf (file, "%s: ", desc);
380 print_node_brief (file, "", t, 0);
381 }
382 }
383
384
385 /* Dump parser context C to FILE. */
386
387 static void
388 cp_debug_print_context (FILE *file, cp_parser_context *c)
389 {
390 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
391 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
392 print_node_brief (file, "", c->object_type, 0);
393 fprintf (file, "}\n");
394 }
395
396
397 /* Print the stack of parsing contexts to FILE starting with FIRST. */
398
399 static void
400 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
401 {
402 unsigned i;
403 cp_parser_context *c;
404
405 fprintf (file, "Parsing context stack:\n");
406 for (i = 0, c = first; c; c = c->next, i++)
407 {
408 fprintf (file, "\t#%u: ", i);
409 cp_debug_print_context (file, c);
410 }
411 }
412
413
414 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
415
416 static void
417 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
418 {
419 if (flag)
420 fprintf (file, "%s: true\n", desc);
421 }
422
423
424 /* Print an unparsed function entry UF to FILE. */
425
426 static void
427 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
428 {
429 unsigned i;
430 cp_default_arg_entry *default_arg_fn;
431 tree fn;
432
433 fprintf (file, "\tFunctions with default args:\n");
434 for (i = 0;
435 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
436 i++)
437 {
438 fprintf (file, "\t\tClass type: ");
439 print_node_brief (file, "", default_arg_fn->class_type, 0);
440 fprintf (file, "\t\tDeclaration: ");
441 print_node_brief (file, "", default_arg_fn->decl, 0);
442 fprintf (file, "\n");
443 }
444
445 fprintf (file, "\n\tFunctions with definitions that require "
446 "post-processing\n\t\t");
447 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
448 {
449 print_node_brief (file, "", fn, 0);
450 fprintf (file, " ");
451 }
452 fprintf (file, "\n");
453
454 fprintf (file, "\n\tNon-static data members with initializers that require "
455 "post-processing\n\t\t");
456 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
457 {
458 print_node_brief (file, "", fn, 0);
459 fprintf (file, " ");
460 }
461 fprintf (file, "\n");
462 }
463
464
465 /* Print the stack of unparsed member functions S to FILE. */
466
467 static void
468 cp_debug_print_unparsed_queues (FILE *file,
469 vec<cp_unparsed_functions_entry, va_gc> *s)
470 {
471 unsigned i;
472 cp_unparsed_functions_entry *uf;
473
474 fprintf (file, "Unparsed functions\n");
475 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
476 {
477 fprintf (file, "#%u:\n", i);
478 cp_debug_print_unparsed_function (file, uf);
479 }
480 }
481
482
483 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
484 the given PARSER. If FILE is NULL, the output is printed on stderr. */
485
486 static void
487 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
488 {
489 cp_token *next_token, *first_token, *start_token;
490
491 if (file == NULL)
492 file = stderr;
493
494 next_token = parser->lexer->next_token;
495 first_token = parser->lexer->buffer->address ();
496 start_token = (next_token > first_token + window_size / 2)
497 ? next_token - window_size / 2
498 : first_token;
499 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
500 next_token);
501 }
502
503
504 /* Dump debugging information for the given PARSER. If FILE is NULL,
505 the output is printed on stderr. */
506
507 void
508 cp_debug_parser (FILE *file, cp_parser *parser)
509 {
510 const size_t window_size = 20;
511 cp_token *token;
512 expanded_location eloc;
513
514 if (file == NULL)
515 file = stderr;
516
517 fprintf (file, "Parser state\n\n");
518 fprintf (file, "Number of tokens: %u\n",
519 vec_safe_length (parser->lexer->buffer));
520 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
521 cp_debug_print_tree_if_set (file, "Object scope",
522 parser->object_scope);
523 cp_debug_print_tree_if_set (file, "Qualifying scope",
524 parser->qualifying_scope);
525 cp_debug_print_context_stack (file, parser->context);
526 cp_debug_print_flag (file, "Allow GNU extensions",
527 parser->allow_gnu_extensions_p);
528 cp_debug_print_flag (file, "'>' token is greater-than",
529 parser->greater_than_is_operator_p);
530 cp_debug_print_flag (file, "Default args allowed in current "
531 "parameter list", parser->default_arg_ok_p);
532 cp_debug_print_flag (file, "Parsing integral constant-expression",
533 parser->integral_constant_expression_p);
534 cp_debug_print_flag (file, "Allow non-constant expression in current "
535 "constant-expression",
536 parser->allow_non_integral_constant_expression_p);
537 cp_debug_print_flag (file, "Seen non-constant expression",
538 parser->non_integral_constant_expression_p);
539 cp_debug_print_flag (file, "Local names forbidden in current context",
540 (parser->local_variables_forbidden_p
541 & LOCAL_VARS_FORBIDDEN));
542 cp_debug_print_flag (file, "'this' forbidden in current context",
543 (parser->local_variables_forbidden_p
544 & THIS_FORBIDDEN));
545 cp_debug_print_flag (file, "In unbraced linkage specification",
546 parser->in_unbraced_linkage_specification_p);
547 cp_debug_print_flag (file, "Parsing a declarator",
548 parser->in_declarator_p);
549 cp_debug_print_flag (file, "In template argument list",
550 parser->in_template_argument_list_p);
551 cp_debug_print_flag (file, "Parsing an iteration statement",
552 parser->in_statement & IN_ITERATION_STMT);
553 cp_debug_print_flag (file, "Parsing a switch statement",
554 parser->in_statement & IN_SWITCH_STMT);
555 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
556 parser->in_statement & IN_OMP_BLOCK);
557 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
558 parser->in_statement & IN_OMP_FOR);
559 cp_debug_print_flag (file, "Parsing an if statement",
560 parser->in_statement & IN_IF_STMT);
561 cp_debug_print_flag (file, "Parsing a type-id in an expression "
562 "context", parser->in_type_id_in_expr_p);
563 cp_debug_print_flag (file, "String expressions should be translated "
564 "to execution character set",
565 parser->translate_strings_p);
566 cp_debug_print_flag (file, "Parsing function body outside of a "
567 "local class", parser->in_function_body);
568 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
569 parser->colon_corrects_to_scope_p);
570 cp_debug_print_flag (file, "Colon doesn't start a class definition",
571 parser->colon_doesnt_start_class_def_p);
572 if (parser->type_definition_forbidden_message)
573 fprintf (file, "Error message for forbidden type definitions: %s %s\n",
574 parser->type_definition_forbidden_message,
575 parser->type_definition_forbidden_message_arg
576 ? parser->type_definition_forbidden_message_arg : "<none>");
577 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
578 fprintf (file, "Number of class definitions in progress: %u\n",
579 parser->num_classes_being_defined);
580 fprintf (file, "Number of template parameter lists for the current "
581 "declaration: %u\n", parser->num_template_parameter_lists);
582 cp_debug_parser_tokens (file, parser, window_size);
583 token = parser->lexer->next_token;
584 fprintf (file, "Next token to parse:\n");
585 fprintf (file, "\tToken: ");
586 cp_lexer_print_token (file, token);
587 eloc = expand_location (token->location);
588 fprintf (file, "\n\tFile: %s\n", eloc.file);
589 fprintf (file, "\tLine: %d\n", eloc.line);
590 fprintf (file, "\tColumn: %d\n", eloc.column);
591 }
592
593 DEBUG_FUNCTION void
594 debug (cp_parser &ref)
595 {
596 cp_debug_parser (stderr, &ref);
597 }
598
599 DEBUG_FUNCTION void
600 debug (cp_parser *ptr)
601 {
602 if (ptr)
603 debug (*ptr);
604 else
605 fprintf (stderr, "<nil>\n");
606 }
607
608 /* Allocate memory for a new lexer object and return it. */
609
610 static cp_lexer *
611 cp_lexer_alloc (void)
612 {
613 cp_lexer *lexer;
614
615 c_common_no_more_pch ();
616
617 /* Allocate the memory. */
618 lexer = ggc_cleared_alloc<cp_lexer> ();
619
620 /* Initially we are not debugging. */
621 lexer->debugging_p = false;
622
623 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
624
625 /* Create the buffer. */
626 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
627
628 return lexer;
629 }
630
631
632 /* Create a new main C++ lexer, the lexer that gets tokens from the
633 preprocessor. */
634
635 static cp_lexer *
636 cp_lexer_new_main (void)
637 {
638 cp_lexer *lexer;
639 cp_token token;
640
641 /* It's possible that parsing the first pragma will load a PCH file,
642 which is a GC collection point. So we have to do that before
643 allocating any memory. */
644 cp_parser_initial_pragma (&token);
645
646 lexer = cp_lexer_alloc ();
647
648 /* Put the first token in the buffer. */
649 lexer->buffer->quick_push (token);
650
651 /* Get the remaining tokens from the preprocessor. */
652 while (token.type != CPP_EOF)
653 {
654 cp_lexer_get_preprocessor_token (lexer, &token);
655 vec_safe_push (lexer->buffer, token);
656 }
657
658 lexer->last_token = lexer->buffer->address ()
659 + lexer->buffer->length ()
660 - 1;
661 lexer->next_token = lexer->buffer->length ()
662 ? lexer->buffer->address ()
663 : &eof_token;
664
665 /* Subsequent preprocessor diagnostics should use compiler
666 diagnostic functions to get the compiler source location. */
667 done_lexing = true;
668
669 gcc_assert (!lexer->next_token->purged_p);
670 return lexer;
671 }
672
673 /* Create a new lexer whose token stream is primed with the tokens in
674 CACHE. When these tokens are exhausted, no new tokens will be read. */
675
676 static cp_lexer *
677 cp_lexer_new_from_tokens (cp_token_cache *cache)
678 {
679 cp_token *first = cache->first;
680 cp_token *last = cache->last;
681 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
682
683 /* We do not own the buffer. */
684 lexer->buffer = NULL;
685 lexer->next_token = first == last ? &eof_token : first;
686 lexer->last_token = last;
687
688 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
689
690 /* Initially we are not debugging. */
691 lexer->debugging_p = false;
692
693 gcc_assert (!lexer->next_token->purged_p);
694 return lexer;
695 }
696
697 /* Frees all resources associated with LEXER. */
698
699 static void
700 cp_lexer_destroy (cp_lexer *lexer)
701 {
702 vec_free (lexer->buffer);
703 lexer->saved_tokens.release ();
704 ggc_free (lexer);
705 }
706
707 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
708 be used. The point of this flag is to help the compiler to fold away calls
709 to cp_lexer_debugging_p within this source file at compile time, when the
710 lexer is not being debugged. */
711
712 #define LEXER_DEBUGGING_ENABLED_P false
713
714 /* Returns nonzero if debugging information should be output. */
715
716 static inline bool
717 cp_lexer_debugging_p (cp_lexer *lexer)
718 {
719 if (!LEXER_DEBUGGING_ENABLED_P)
720 return false;
721
722 return lexer->debugging_p;
723 }
724
725
726 static inline cp_token_position
727 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
728 {
729 gcc_assert (!previous_p || lexer->next_token != &eof_token);
730
731 return lexer->next_token - previous_p;
732 }
733
734 static inline cp_token *
735 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
736 {
737 return pos;
738 }
739
740 static inline void
741 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
742 {
743 lexer->next_token = cp_lexer_token_at (lexer, pos);
744 }
745
746 static inline cp_token_position
747 cp_lexer_previous_token_position (cp_lexer *lexer)
748 {
749 if (lexer->next_token == &eof_token)
750 return lexer->last_token - 1;
751 else
752 return cp_lexer_token_position (lexer, true);
753 }
754
755 static inline cp_token *
756 cp_lexer_previous_token (cp_lexer *lexer)
757 {
758 cp_token_position tp = cp_lexer_previous_token_position (lexer);
759
760 /* Skip past purged tokens. */
761 while (tp->purged_p)
762 {
763 gcc_assert (tp != vec_safe_address (lexer->buffer));
764 tp--;
765 }
766
767 return cp_lexer_token_at (lexer, tp);
768 }
769
770 /* nonzero if we are presently saving tokens. */
771
772 static inline int
773 cp_lexer_saving_tokens (const cp_lexer* lexer)
774 {
775 return lexer->saved_tokens.length () != 0;
776 }
777
778 /* Store the next token from the preprocessor in *TOKEN. Return true
779 if we reach EOF. If LEXER is NULL, assume we are handling an
780 initial #pragma pch_preprocess, and thus want the lexer to return
781 processed strings. */
782
783 static void
784 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
785 {
786 static int is_extern_c = 0;
787
788 /* Get a new token from the preprocessor. */
789 token->type
790 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
791 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
792 token->keyword = RID_MAX;
793 token->purged_p = false;
794 token->error_reported = false;
795
796 /* On some systems, some header files are surrounded by an
797 implicit extern "C" block. Set a flag in the token if it
798 comes from such a header. */
799 is_extern_c += pending_lang_change;
800 pending_lang_change = 0;
801 token->implicit_extern_c = is_extern_c > 0;
802
803 /* Check to see if this token is a keyword. */
804 if (token->type == CPP_NAME)
805 {
806 if (IDENTIFIER_KEYWORD_P (token->u.value))
807 {
808 /* Mark this token as a keyword. */
809 token->type = CPP_KEYWORD;
810 /* Record which keyword. */
811 token->keyword = C_RID_CODE (token->u.value);
812 }
813 else
814 {
815 if (warn_cxx11_compat
816 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
817 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
818 {
819 /* Warn about the C++0x keyword (but still treat it as
820 an identifier). */
821 warning (OPT_Wc__11_compat,
822 "identifier %qE is a keyword in C++11",
823 token->u.value);
824
825 /* Clear out the C_RID_CODE so we don't warn about this
826 particular identifier-turned-keyword again. */
827 C_SET_RID_CODE (token->u.value, RID_MAX);
828 }
829
830 token->keyword = RID_MAX;
831 }
832 }
833 else if (token->type == CPP_AT_NAME)
834 {
835 /* This only happens in Objective-C++; it must be a keyword. */
836 token->type = CPP_KEYWORD;
837 switch (C_RID_CODE (token->u.value))
838 {
839 /* Replace 'class' with '@class', 'private' with '@private',
840 etc. This prevents confusion with the C++ keyword
841 'class', and makes the tokens consistent with other
842 Objective-C 'AT' keywords. For example '@class' is
843 reported as RID_AT_CLASS which is consistent with
844 '@synchronized', which is reported as
845 RID_AT_SYNCHRONIZED.
846 */
847 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
848 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
849 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
850 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
851 case RID_THROW: token->keyword = RID_AT_THROW; break;
852 case RID_TRY: token->keyword = RID_AT_TRY; break;
853 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
854 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
855 default: token->keyword = C_RID_CODE (token->u.value);
856 }
857 }
858 }
859
860 /* Update the globals input_location and the input file stack from TOKEN. */
861 static inline void
862 cp_lexer_set_source_position_from_token (cp_token *token)
863 {
864 if (token->type != CPP_EOF)
865 {
866 input_location = token->location;
867 }
868 }
869
870 /* Update the globals input_location and the input file stack from LEXER. */
871 static inline void
872 cp_lexer_set_source_position (cp_lexer *lexer)
873 {
874 cp_token *token = cp_lexer_peek_token (lexer);
875 cp_lexer_set_source_position_from_token (token);
876 }
877
878 /* Return a pointer to the next token in the token stream, but do not
879 consume it. */
880
881 static inline cp_token *
882 cp_lexer_peek_token (cp_lexer *lexer)
883 {
884 if (cp_lexer_debugging_p (lexer))
885 {
886 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
887 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
888 putc ('\n', cp_lexer_debug_stream);
889 }
890 return lexer->next_token;
891 }
892
893 /* Return true if the next token has the indicated TYPE. */
894
895 static inline bool
896 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
897 {
898 return cp_lexer_peek_token (lexer)->type == type;
899 }
900
901 /* Return true if the next token does not have the indicated TYPE. */
902
903 static inline bool
904 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
905 {
906 return !cp_lexer_next_token_is (lexer, type);
907 }
908
909 /* Return true if the next token is the indicated KEYWORD. */
910
911 static inline bool
912 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
913 {
914 return cp_lexer_peek_token (lexer)->keyword == keyword;
915 }
916
917 static inline bool
918 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
919 {
920 return cp_lexer_peek_nth_token (lexer, n)->type == type;
921 }
922
923 static inline bool
924 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
925 {
926 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
927 }
928
929 /* Return true if KEYWORD can start a decl-specifier. */
930
931 bool
932 cp_keyword_starts_decl_specifier_p (enum rid keyword)
933 {
934 switch (keyword)
935 {
936 /* auto specifier: storage-class-specifier in C++,
937 simple-type-specifier in C++0x. */
938 case RID_AUTO:
939 /* Storage classes. */
940 case RID_REGISTER:
941 case RID_STATIC:
942 case RID_EXTERN:
943 case RID_MUTABLE:
944 case RID_THREAD:
945 /* Elaborated type specifiers. */
946 case RID_ENUM:
947 case RID_CLASS:
948 case RID_STRUCT:
949 case RID_UNION:
950 case RID_TYPENAME:
951 /* Simple type specifiers. */
952 case RID_CHAR:
953 case RID_CHAR8:
954 case RID_CHAR16:
955 case RID_CHAR32:
956 case RID_WCHAR:
957 case RID_BOOL:
958 case RID_SHORT:
959 case RID_INT:
960 case RID_LONG:
961 case RID_SIGNED:
962 case RID_UNSIGNED:
963 case RID_FLOAT:
964 case RID_DOUBLE:
965 case RID_VOID:
966 /* GNU extensions. */
967 case RID_ATTRIBUTE:
968 case RID_TYPEOF:
969 /* C++0x extensions. */
970 case RID_DECLTYPE:
971 case RID_UNDERLYING_TYPE:
972 case RID_CONSTEXPR:
973 return true;
974
975 default:
976 if (keyword >= RID_FIRST_INT_N
977 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
978 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
979 return true;
980 return false;
981 }
982 }
983
984 /* Return true if the next token is a keyword for a decl-specifier. */
985
986 static bool
987 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
988 {
989 cp_token *token;
990
991 token = cp_lexer_peek_token (lexer);
992 return cp_keyword_starts_decl_specifier_p (token->keyword);
993 }
994
995 /* Returns TRUE iff the token T begins a decltype type. */
996
997 static bool
998 token_is_decltype (cp_token *t)
999 {
1000 return (t->keyword == RID_DECLTYPE
1001 || t->type == CPP_DECLTYPE);
1002 }
1003
1004 /* Returns TRUE iff the next token begins a decltype type. */
1005
1006 static bool
1007 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1008 {
1009 cp_token *t = cp_lexer_peek_token (lexer);
1010 return token_is_decltype (t);
1011 }
1012
1013 /* Called when processing a token with tree_check_value; perform or defer the
1014 associated checks and return the value. */
1015
1016 static tree
1017 saved_checks_value (struct tree_check *check_value)
1018 {
1019 /* Perform any access checks that were deferred. */
1020 vec<deferred_access_check, va_gc> *checks;
1021 deferred_access_check *chk;
1022 checks = check_value->checks;
1023 if (checks)
1024 {
1025 int i;
1026 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1027 perform_or_defer_access_check (chk->binfo,
1028 chk->decl,
1029 chk->diag_decl, tf_warning_or_error);
1030 }
1031 /* Return the stored value. */
1032 return check_value->value;
1033 }
1034
1035 /* Return a pointer to the Nth token in the token stream. If N is 1,
1036 then this is precisely equivalent to cp_lexer_peek_token (except
1037 that it is not inline). One would like to disallow that case, but
1038 there is one case (cp_parser_nth_token_starts_template_id) where
1039 the caller passes a variable for N and it might be 1. */
1040
1041 static cp_token *
1042 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1043 {
1044 cp_token *token;
1045
1046 /* N is 1-based, not zero-based. */
1047 gcc_assert (n > 0);
1048
1049 if (cp_lexer_debugging_p (lexer))
1050 fprintf (cp_lexer_debug_stream,
1051 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1052
1053 --n;
1054 token = lexer->next_token;
1055 gcc_assert (!n || token != &eof_token);
1056 while (n != 0)
1057 {
1058 ++token;
1059 if (token == lexer->last_token)
1060 {
1061 token = &eof_token;
1062 break;
1063 }
1064
1065 if (!token->purged_p)
1066 --n;
1067 }
1068
1069 if (cp_lexer_debugging_p (lexer))
1070 {
1071 cp_lexer_print_token (cp_lexer_debug_stream, token);
1072 putc ('\n', cp_lexer_debug_stream);
1073 }
1074
1075 return token;
1076 }
1077
1078 /* Return the next token, and advance the lexer's next_token pointer
1079 to point to the next non-purged token. */
1080
1081 static cp_token *
1082 cp_lexer_consume_token (cp_lexer* lexer)
1083 {
1084 cp_token *token = lexer->next_token;
1085
1086 gcc_assert (token != &eof_token);
1087 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1088
1089 do
1090 {
1091 lexer->next_token++;
1092 if (lexer->next_token == lexer->last_token)
1093 {
1094 lexer->next_token = &eof_token;
1095 break;
1096 }
1097
1098 }
1099 while (lexer->next_token->purged_p);
1100
1101 cp_lexer_set_source_position_from_token (token);
1102
1103 /* Provide debugging output. */
1104 if (cp_lexer_debugging_p (lexer))
1105 {
1106 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1107 cp_lexer_print_token (cp_lexer_debug_stream, token);
1108 putc ('\n', cp_lexer_debug_stream);
1109 }
1110
1111 return token;
1112 }
1113
1114 /* Permanently remove the next token from the token stream, and
1115 advance the next_token pointer to refer to the next non-purged
1116 token. */
1117
1118 static void
1119 cp_lexer_purge_token (cp_lexer *lexer)
1120 {
1121 cp_token *tok = lexer->next_token;
1122
1123 gcc_assert (tok != &eof_token);
1124 tok->purged_p = true;
1125 tok->location = UNKNOWN_LOCATION;
1126 tok->u.value = NULL_TREE;
1127 tok->keyword = RID_MAX;
1128
1129 do
1130 {
1131 tok++;
1132 if (tok == lexer->last_token)
1133 {
1134 tok = &eof_token;
1135 break;
1136 }
1137 }
1138 while (tok->purged_p);
1139 lexer->next_token = tok;
1140 }
1141
1142 /* Permanently remove all tokens after TOK, up to, but not
1143 including, the token that will be returned next by
1144 cp_lexer_peek_token. */
1145
1146 static void
1147 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1148 {
1149 cp_token *peek = lexer->next_token;
1150
1151 if (peek == &eof_token)
1152 peek = lexer->last_token;
1153
1154 gcc_assert (tok < peek);
1155
1156 for ( tok += 1; tok != peek; tok += 1)
1157 {
1158 tok->purged_p = true;
1159 tok->location = UNKNOWN_LOCATION;
1160 tok->u.value = NULL_TREE;
1161 tok->keyword = RID_MAX;
1162 }
1163 }
1164
1165 /* Begin saving tokens. All tokens consumed after this point will be
1166 preserved. */
1167
1168 static void
1169 cp_lexer_save_tokens (cp_lexer* lexer)
1170 {
1171 /* Provide debugging output. */
1172 if (cp_lexer_debugging_p (lexer))
1173 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1174
1175 lexer->saved_tokens.safe_push (lexer->next_token);
1176 }
1177
1178 /* Commit to the portion of the token stream most recently saved. */
1179
1180 static void
1181 cp_lexer_commit_tokens (cp_lexer* lexer)
1182 {
1183 /* Provide debugging output. */
1184 if (cp_lexer_debugging_p (lexer))
1185 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1186
1187 lexer->saved_tokens.pop ();
1188 }
1189
1190 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1191 to the token stream. Stop saving tokens. */
1192
1193 static void
1194 cp_lexer_rollback_tokens (cp_lexer* lexer)
1195 {
1196 /* Provide debugging output. */
1197 if (cp_lexer_debugging_p (lexer))
1198 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1199
1200 lexer->next_token = lexer->saved_tokens.pop ();
1201 }
1202
1203 /* RAII wrapper around the above functions, with sanity checking. Creating
1204 a variable saves tokens, which are committed when the variable is
1205 destroyed unless they are explicitly rolled back by calling the rollback
1206 member function. */
1207
1208 struct saved_token_sentinel
1209 {
1210 cp_lexer *lexer;
1211 unsigned len;
1212 bool commit;
1213 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1214 {
1215 len = lexer->saved_tokens.length ();
1216 cp_lexer_save_tokens (lexer);
1217 }
1218 void rollback ()
1219 {
1220 cp_lexer_rollback_tokens (lexer);
1221 commit = false;
1222 }
1223 ~saved_token_sentinel()
1224 {
1225 if (commit)
1226 cp_lexer_commit_tokens (lexer);
1227 gcc_assert (lexer->saved_tokens.length () == len);
1228 }
1229 };
1230
1231 /* Print a representation of the TOKEN on the STREAM. */
1232
1233 static void
1234 cp_lexer_print_token (FILE * stream, cp_token *token)
1235 {
1236 /* We don't use cpp_type2name here because the parser defines
1237 a few tokens of its own. */
1238 static const char *const token_names[] = {
1239 /* cpplib-defined token types */
1240 #define OP(e, s) #e,
1241 #define TK(e, s) #e,
1242 TTYPE_TABLE
1243 #undef OP
1244 #undef TK
1245 /* C++ parser token types - see "Manifest constants", above. */
1246 "KEYWORD",
1247 "TEMPLATE_ID",
1248 "NESTED_NAME_SPECIFIER",
1249 };
1250
1251 /* For some tokens, print the associated data. */
1252 switch (token->type)
1253 {
1254 case CPP_KEYWORD:
1255 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1256 For example, `struct' is mapped to an INTEGER_CST. */
1257 if (!identifier_p (token->u.value))
1258 break;
1259 /* fall through */
1260 case CPP_NAME:
1261 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1262 break;
1263
1264 case CPP_STRING:
1265 case CPP_STRING16:
1266 case CPP_STRING32:
1267 case CPP_WSTRING:
1268 case CPP_UTF8STRING:
1269 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1270 break;
1271
1272 case CPP_NUMBER:
1273 print_generic_expr (stream, token->u.value);
1274 break;
1275
1276 default:
1277 /* If we have a name for the token, print it out. Otherwise, we
1278 simply give the numeric code. */
1279 if (token->type < ARRAY_SIZE(token_names))
1280 fputs (token_names[token->type], stream);
1281 else
1282 fprintf (stream, "[%d]", token->type);
1283 break;
1284 }
1285 }
1286
1287 DEBUG_FUNCTION void
1288 debug (cp_token &ref)
1289 {
1290 cp_lexer_print_token (stderr, &ref);
1291 fprintf (stderr, "\n");
1292 }
1293
1294 DEBUG_FUNCTION void
1295 debug (cp_token *ptr)
1296 {
1297 if (ptr)
1298 debug (*ptr);
1299 else
1300 fprintf (stderr, "<nil>\n");
1301 }
1302
1303
1304 /* Start emitting debugging information. */
1305
1306 static void
1307 cp_lexer_start_debugging (cp_lexer* lexer)
1308 {
1309 if (!LEXER_DEBUGGING_ENABLED_P)
1310 fatal_error (input_location,
1311 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1312
1313 lexer->debugging_p = true;
1314 cp_lexer_debug_stream = stderr;
1315 }
1316
1317 /* Stop emitting debugging information. */
1318
1319 static void
1320 cp_lexer_stop_debugging (cp_lexer* lexer)
1321 {
1322 if (!LEXER_DEBUGGING_ENABLED_P)
1323 fatal_error (input_location,
1324 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1325
1326 lexer->debugging_p = false;
1327 cp_lexer_debug_stream = NULL;
1328 }
1329
1330 /* Create a new cp_token_cache, representing a range of tokens. */
1331
1332 static cp_token_cache *
1333 cp_token_cache_new (cp_token *first, cp_token *last)
1334 {
1335 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1336 cache->first = first;
1337 cache->last = last;
1338 return cache;
1339 }
1340
1341 /* Diagnose if #pragma omp declare simd isn't followed immediately
1342 by function declaration or definition. */
1343
1344 static inline void
1345 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1346 {
1347 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1348 {
1349 error ("%<#pragma omp declare simd%> not immediately followed by "
1350 "function declaration or definition");
1351 parser->omp_declare_simd = NULL;
1352 }
1353 }
1354
1355 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1356 and put that into "omp declare simd" attribute. */
1357
1358 static inline void
1359 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1360 {
1361 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1362 {
1363 if (fndecl == error_mark_node)
1364 {
1365 parser->omp_declare_simd = NULL;
1366 return;
1367 }
1368 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1369 {
1370 cp_ensure_no_omp_declare_simd (parser);
1371 return;
1372 }
1373 }
1374 }
1375
1376 /* Diagnose if #pragma acc routine isn't followed immediately by function
1377 declaration or definition. */
1378
1379 static inline void
1380 cp_ensure_no_oacc_routine (cp_parser *parser)
1381 {
1382 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1383 {
1384 error_at (parser->oacc_routine->loc,
1385 "%<#pragma acc routine%> not immediately followed by "
1386 "function declaration or definition");
1387 parser->oacc_routine = NULL;
1388 }
1389 }
1390 \f
1391 /* Decl-specifiers. */
1392
1393 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1394
1395 static void
1396 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1397 {
1398 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1399 }
1400
1401 /* Declarators. */
1402
1403 /* Nothing other than the parser should be creating declarators;
1404 declarators are a semi-syntactic representation of C++ entities.
1405 Other parts of the front end that need to create entities (like
1406 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1407
1408 static cp_declarator *make_call_declarator
1409 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1410 static cp_declarator *make_array_declarator
1411 (cp_declarator *, tree);
1412 static cp_declarator *make_pointer_declarator
1413 (cp_cv_quals, cp_declarator *, tree);
1414 static cp_declarator *make_reference_declarator
1415 (cp_cv_quals, cp_declarator *, bool, tree);
1416 static cp_declarator *make_ptrmem_declarator
1417 (cp_cv_quals, tree, cp_declarator *, tree);
1418
1419 /* An erroneous declarator. */
1420 static cp_declarator *cp_error_declarator;
1421
1422 /* The obstack on which declarators and related data structures are
1423 allocated. */
1424 static struct obstack declarator_obstack;
1425
1426 /* Alloc BYTES from the declarator memory pool. */
1427
1428 static inline void *
1429 alloc_declarator (size_t bytes)
1430 {
1431 return obstack_alloc (&declarator_obstack, bytes);
1432 }
1433
1434 /* Allocate a declarator of the indicated KIND. Clear fields that are
1435 common to all declarators. */
1436
1437 static cp_declarator *
1438 make_declarator (cp_declarator_kind kind)
1439 {
1440 cp_declarator *declarator;
1441
1442 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1443 declarator->kind = kind;
1444 declarator->parenthesized = UNKNOWN_LOCATION;
1445 declarator->attributes = NULL_TREE;
1446 declarator->std_attributes = NULL_TREE;
1447 declarator->declarator = NULL;
1448 declarator->parameter_pack_p = false;
1449 declarator->id_loc = UNKNOWN_LOCATION;
1450
1451 return declarator;
1452 }
1453
1454 /* Make a declarator for a generalized identifier. If
1455 QUALIFYING_SCOPE is non-NULL, the identifier is
1456 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1457 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1458 is, if any. */
1459
1460 static cp_declarator *
1461 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1462 special_function_kind sfk, location_t id_location)
1463 {
1464 cp_declarator *declarator;
1465
1466 /* It is valid to write:
1467
1468 class C { void f(); };
1469 typedef C D;
1470 void D::f();
1471
1472 The standard is not clear about whether `typedef const C D' is
1473 legal; as of 2002-09-15 the committee is considering that
1474 question. EDG 3.0 allows that syntax. Therefore, we do as
1475 well. */
1476 if (qualifying_scope && TYPE_P (qualifying_scope))
1477 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1478
1479 gcc_assert (identifier_p (unqualified_name)
1480 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1481 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1482
1483 declarator = make_declarator (cdk_id);
1484 declarator->u.id.qualifying_scope = qualifying_scope;
1485 declarator->u.id.unqualified_name = unqualified_name;
1486 declarator->u.id.sfk = sfk;
1487 declarator->id_loc = id_location;
1488
1489 return declarator;
1490 }
1491
1492 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1493 of modifiers such as const or volatile to apply to the pointer
1494 type, represented as identifiers. ATTRIBUTES represent the attributes that
1495 appertain to the pointer or reference. */
1496
1497 cp_declarator *
1498 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1499 tree attributes)
1500 {
1501 cp_declarator *declarator;
1502
1503 declarator = make_declarator (cdk_pointer);
1504 declarator->declarator = target;
1505 declarator->u.pointer.qualifiers = cv_qualifiers;
1506 declarator->u.pointer.class_type = NULL_TREE;
1507 if (target)
1508 {
1509 declarator->id_loc = target->id_loc;
1510 declarator->parameter_pack_p = target->parameter_pack_p;
1511 target->parameter_pack_p = false;
1512 }
1513 else
1514 declarator->parameter_pack_p = false;
1515
1516 declarator->std_attributes = attributes;
1517
1518 return declarator;
1519 }
1520
1521 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1522 represent the attributes that appertain to the pointer or
1523 reference. */
1524
1525 cp_declarator *
1526 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1527 bool rvalue_ref, tree attributes)
1528 {
1529 cp_declarator *declarator;
1530
1531 declarator = make_declarator (cdk_reference);
1532 declarator->declarator = target;
1533 declarator->u.reference.qualifiers = cv_qualifiers;
1534 declarator->u.reference.rvalue_ref = rvalue_ref;
1535 if (target)
1536 {
1537 declarator->id_loc = target->id_loc;
1538 declarator->parameter_pack_p = target->parameter_pack_p;
1539 target->parameter_pack_p = false;
1540 }
1541 else
1542 declarator->parameter_pack_p = false;
1543
1544 declarator->std_attributes = attributes;
1545
1546 return declarator;
1547 }
1548
1549 /* Like make_pointer_declarator -- but for a pointer to a non-static
1550 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1551 appertain to the pointer or reference. */
1552
1553 cp_declarator *
1554 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1555 cp_declarator *pointee,
1556 tree attributes)
1557 {
1558 cp_declarator *declarator;
1559
1560 declarator = make_declarator (cdk_ptrmem);
1561 declarator->declarator = pointee;
1562 declarator->u.pointer.qualifiers = cv_qualifiers;
1563 declarator->u.pointer.class_type = class_type;
1564
1565 if (pointee)
1566 {
1567 declarator->parameter_pack_p = pointee->parameter_pack_p;
1568 pointee->parameter_pack_p = false;
1569 }
1570 else
1571 declarator->parameter_pack_p = false;
1572
1573 declarator->std_attributes = attributes;
1574
1575 return declarator;
1576 }
1577
1578 /* Make a declarator for the function given by TARGET, with the
1579 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1580 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1581 indicates what exceptions can be thrown. */
1582
1583 cp_declarator *
1584 make_call_declarator (cp_declarator *target,
1585 tree parms,
1586 cp_cv_quals cv_qualifiers,
1587 cp_virt_specifiers virt_specifiers,
1588 cp_ref_qualifier ref_qualifier,
1589 tree tx_qualifier,
1590 tree exception_specification,
1591 tree late_return_type,
1592 tree requires_clause)
1593 {
1594 cp_declarator *declarator;
1595
1596 declarator = make_declarator (cdk_function);
1597 declarator->declarator = target;
1598 declarator->u.function.parameters = parms;
1599 declarator->u.function.qualifiers = cv_qualifiers;
1600 declarator->u.function.virt_specifiers = virt_specifiers;
1601 declarator->u.function.ref_qualifier = ref_qualifier;
1602 declarator->u.function.tx_qualifier = tx_qualifier;
1603 declarator->u.function.exception_specification = exception_specification;
1604 declarator->u.function.late_return_type = late_return_type;
1605 declarator->u.function.requires_clause = requires_clause;
1606 if (target)
1607 {
1608 declarator->id_loc = target->id_loc;
1609 declarator->parameter_pack_p = target->parameter_pack_p;
1610 target->parameter_pack_p = false;
1611 }
1612 else
1613 declarator->parameter_pack_p = false;
1614
1615 return declarator;
1616 }
1617
1618 /* Make a declarator for an array of BOUNDS elements, each of which is
1619 defined by ELEMENT. */
1620
1621 cp_declarator *
1622 make_array_declarator (cp_declarator *element, tree bounds)
1623 {
1624 cp_declarator *declarator;
1625
1626 declarator = make_declarator (cdk_array);
1627 declarator->declarator = element;
1628 declarator->u.array.bounds = bounds;
1629 if (element)
1630 {
1631 declarator->id_loc = element->id_loc;
1632 declarator->parameter_pack_p = element->parameter_pack_p;
1633 element->parameter_pack_p = false;
1634 }
1635 else
1636 declarator->parameter_pack_p = false;
1637
1638 return declarator;
1639 }
1640
1641 /* Determine whether the declarator we've seen so far can be a
1642 parameter pack, when followed by an ellipsis. */
1643 static bool
1644 declarator_can_be_parameter_pack (cp_declarator *declarator)
1645 {
1646 if (declarator && declarator->parameter_pack_p)
1647 /* We already saw an ellipsis. */
1648 return false;
1649
1650 /* Search for a declarator name, or any other declarator that goes
1651 after the point where the ellipsis could appear in a parameter
1652 pack. If we find any of these, then this declarator cannot be
1653 made into a parameter pack. */
1654 bool found = false;
1655 while (declarator && !found)
1656 {
1657 switch ((int)declarator->kind)
1658 {
1659 case cdk_id:
1660 case cdk_array:
1661 case cdk_decomp:
1662 found = true;
1663 break;
1664
1665 case cdk_error:
1666 return true;
1667
1668 default:
1669 declarator = declarator->declarator;
1670 break;
1671 }
1672 }
1673
1674 return !found;
1675 }
1676
1677 cp_parameter_declarator *no_parameters;
1678
1679 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1680 DECLARATOR and DEFAULT_ARGUMENT. */
1681
1682 cp_parameter_declarator *
1683 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1684 cp_declarator *declarator,
1685 tree default_argument,
1686 location_t loc,
1687 bool template_parameter_pack_p = false)
1688 {
1689 cp_parameter_declarator *parameter;
1690
1691 parameter = ((cp_parameter_declarator *)
1692 alloc_declarator (sizeof (cp_parameter_declarator)));
1693 parameter->next = NULL;
1694 if (decl_specifiers)
1695 parameter->decl_specifiers = *decl_specifiers;
1696 else
1697 clear_decl_specs (&parameter->decl_specifiers);
1698 parameter->declarator = declarator;
1699 parameter->default_argument = default_argument;
1700 parameter->template_parameter_pack_p = template_parameter_pack_p;
1701 parameter->loc = loc;
1702
1703 return parameter;
1704 }
1705
1706 /* Returns true iff DECLARATOR is a declaration for a function. */
1707
1708 static bool
1709 function_declarator_p (const cp_declarator *declarator)
1710 {
1711 while (declarator)
1712 {
1713 if (declarator->kind == cdk_function
1714 && declarator->declarator->kind == cdk_id)
1715 return true;
1716 if (declarator->kind == cdk_id
1717 || declarator->kind == cdk_decomp
1718 || declarator->kind == cdk_error)
1719 return false;
1720 declarator = declarator->declarator;
1721 }
1722 return false;
1723 }
1724
1725 /* The parser. */
1726
1727 /* Overview
1728 --------
1729
1730 A cp_parser parses the token stream as specified by the C++
1731 grammar. Its job is purely parsing, not semantic analysis. For
1732 example, the parser breaks the token stream into declarators,
1733 expressions, statements, and other similar syntactic constructs.
1734 It does not check that the types of the expressions on either side
1735 of an assignment-statement are compatible, or that a function is
1736 not declared with a parameter of type `void'.
1737
1738 The parser invokes routines elsewhere in the compiler to perform
1739 semantic analysis and to build up the abstract syntax tree for the
1740 code processed.
1741
1742 The parser (and the template instantiation code, which is, in a
1743 way, a close relative of parsing) are the only parts of the
1744 compiler that should be calling push_scope and pop_scope, or
1745 related functions. The parser (and template instantiation code)
1746 keeps track of what scope is presently active; everything else
1747 should simply honor that. (The code that generates static
1748 initializers may also need to set the scope, in order to check
1749 access control correctly when emitting the initializers.)
1750
1751 Methodology
1752 -----------
1753
1754 The parser is of the standard recursive-descent variety. Upcoming
1755 tokens in the token stream are examined in order to determine which
1756 production to use when parsing a non-terminal. Some C++ constructs
1757 require arbitrary look ahead to disambiguate. For example, it is
1758 impossible, in the general case, to tell whether a statement is an
1759 expression or declaration without scanning the entire statement.
1760 Therefore, the parser is capable of "parsing tentatively." When the
1761 parser is not sure what construct comes next, it enters this mode.
1762 Then, while we attempt to parse the construct, the parser queues up
1763 error messages, rather than issuing them immediately, and saves the
1764 tokens it consumes. If the construct is parsed successfully, the
1765 parser "commits", i.e., it issues any queued error messages and
1766 the tokens that were being preserved are permanently discarded.
1767 If, however, the construct is not parsed successfully, the parser
1768 rolls back its state completely so that it can resume parsing using
1769 a different alternative.
1770
1771 Future Improvements
1772 -------------------
1773
1774 The performance of the parser could probably be improved substantially.
1775 We could often eliminate the need to parse tentatively by looking ahead
1776 a little bit. In some places, this approach might not entirely eliminate
1777 the need to parse tentatively, but it might still speed up the average
1778 case. */
1779
1780 /* Flags that are passed to some parsing functions. These values can
1781 be bitwise-ored together. */
1782
1783 enum
1784 {
1785 /* No flags. */
1786 CP_PARSER_FLAGS_NONE = 0x0,
1787 /* The construct is optional. If it is not present, then no error
1788 should be issued. */
1789 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1790 /* When parsing a type-specifier, treat user-defined type-names
1791 as non-type identifiers. */
1792 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1793 /* When parsing a type-specifier, do not try to parse a class-specifier
1794 or enum-specifier. */
1795 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1796 /* When parsing a decl-specifier-seq, only allow type-specifier or
1797 constexpr. */
1798 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1799 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1800 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1801 /* When parsing a decl-specifier-seq, allow missing typename. */
1802 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20
1803 };
1804
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags;
1808
1809 /* The different kinds of declarators we want to parse. */
1810
1811 enum cp_parser_declarator_kind
1812 {
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1819 };
1820
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1824
1825 enum cp_parser_prec
1826 {
1827 PREC_NOT_OPERATOR,
1828 PREC_LOGICAL_OR_EXPRESSION,
1829 PREC_LOGICAL_AND_EXPRESSION,
1830 PREC_INCLUSIVE_OR_EXPRESSION,
1831 PREC_EXCLUSIVE_OR_EXPRESSION,
1832 PREC_AND_EXPRESSION,
1833 PREC_EQUALITY_EXPRESSION,
1834 PREC_RELATIONAL_EXPRESSION,
1835 PREC_SHIFT_EXPRESSION,
1836 PREC_ADDITIVE_EXPRESSION,
1837 PREC_MULTIPLICATIVE_EXPRESSION,
1838 PREC_PM_EXPRESSION,
1839 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1840 };
1841
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1844
1845 struct cp_parser_binary_operations_map_node
1846 {
1847 /* The token type. */
1848 enum cpp_ttype token_type;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec;
1853 };
1854
1855 struct cp_parser_expression_stack_entry
1856 {
1857 /* Left hand side of the binary operation we are currently
1858 parsing. */
1859 cp_expr lhs;
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec;
1867 /* Location of the binary operation we are parsing. */
1868 location_t loc;
1869 };
1870
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1873 increasing. */
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack[NUM_PREC_VALUES];
1876
1877 /* Prototypes. */
1878
1879 /* Constructors and destructors. */
1880
1881 static cp_parser_context *cp_parser_context_new
1882 (cp_parser_context *);
1883
1884 /* Class variables. */
1885
1886 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1887
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1890 cp_parser_new. */
1891
1892 static const cp_parser_binary_operations_map_node binops[] = {
1893 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1894 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1895
1896 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1899
1900 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1902
1903 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1905
1906 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1908 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1910
1911 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1912 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1913
1914 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1915
1916 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1917
1918 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1919
1920 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1921
1922 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1923 };
1924
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1927 for speed. */
1928 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1929
1930 /* Constructors and destructors. */
1931
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1934
1935 static cp_parser_context *
1936 cp_parser_context_new (cp_parser_context* next)
1937 {
1938 cp_parser_context *context;
1939
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list != NULL)
1942 {
1943 /* Pull the first entry from the free list. */
1944 context = cp_parser_context_free_list;
1945 cp_parser_context_free_list = context->next;
1946 memset (context, 0, sizeof (*context));
1947 }
1948 else
1949 context = ggc_cleared_alloc<cp_parser_context> ();
1950
1951 /* No errors have occurred yet in this context. */
1952 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1955 if (next)
1956 {
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context->object_type = next->object_type;
1960 /* Thread the stack. */
1961 context->next = next;
1962 }
1963
1964 return context;
1965 }
1966
1967 /* Managing the unparsed function queues. */
1968
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1977
1978 static void
1979 push_unparsed_function_queues (cp_parser *parser)
1980 {
1981 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1982 vec_safe_push (parser->unparsed_queues, e);
1983 }
1984
1985 static void
1986 pop_unparsed_function_queues (cp_parser *parser)
1987 {
1988 release_tree_vector (unparsed_funs_with_definitions);
1989 parser->unparsed_queues->pop ();
1990 }
1991
1992 /* Prototypes. */
1993
1994 /* Constructors and destructors. */
1995
1996 static cp_parser *cp_parser_new
1997 (void);
1998
1999 /* Routines to parse various constructs.
2000
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2010
2011 /* Lexical conventions [gram.lex] */
2012
2013 static cp_expr cp_parser_identifier
2014 (cp_parser *);
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser *, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2018 (cp_parser *);
2019 static tree cp_parser_userdef_string_literal
2020 (tree);
2021 static cp_expr cp_parser_userdef_numeric_literal
2022 (cp_parser *);
2023
2024 /* Basic concepts [gram.basic] */
2025
2026 static void cp_parser_translation_unit (cp_parser *);
2027
2028 /* Expressions [gram.expr] */
2029
2030 static cp_expr cp_parser_primary_expression
2031 (cp_parser *, bool, bool, bool, cp_id_kind *);
2032 static cp_expr cp_parser_id_expression
2033 (cp_parser *, bool, bool, bool *, bool, bool);
2034 static cp_expr cp_parser_unqualified_id
2035 (cp_parser *, bool, bool, bool, bool);
2036 static tree cp_parser_nested_name_specifier_opt
2037 (cp_parser *, bool, bool, bool, bool, bool = false);
2038 static tree cp_parser_nested_name_specifier
2039 (cp_parser *, bool, bool, bool, bool);
2040 static tree cp_parser_qualifying_entity
2041 (cp_parser *, bool, bool, bool, bool, bool);
2042 static cp_expr cp_parser_postfix_expression
2043 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2044 static tree cp_parser_postfix_open_square_expression
2045 (cp_parser *, tree, bool, bool);
2046 static tree cp_parser_postfix_dot_deref_expression
2047 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2048 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2049 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2050 bool = false);
2051 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2052 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2053 static void cp_parser_pseudo_destructor_name
2054 (cp_parser *, tree, tree *, tree *);
2055 static cp_expr cp_parser_unary_expression
2056 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2057 static enum tree_code cp_parser_unary_operator
2058 (cp_token *);
2059 static tree cp_parser_has_attribute_expression
2060 (cp_parser *);
2061 static tree cp_parser_new_expression
2062 (cp_parser *);
2063 static vec<tree, va_gc> *cp_parser_new_placement
2064 (cp_parser *);
2065 static tree cp_parser_new_type_id
2066 (cp_parser *, tree *);
2067 static cp_declarator *cp_parser_new_declarator_opt
2068 (cp_parser *);
2069 static cp_declarator *cp_parser_direct_new_declarator
2070 (cp_parser *);
2071 static vec<tree, va_gc> *cp_parser_new_initializer
2072 (cp_parser *);
2073 static tree cp_parser_delete_expression
2074 (cp_parser *);
2075 static cp_expr cp_parser_cast_expression
2076 (cp_parser *, bool, bool, bool, cp_id_kind *);
2077 static cp_expr cp_parser_binary_expression
2078 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2079 static tree cp_parser_question_colon_clause
2080 (cp_parser *, cp_expr);
2081 static cp_expr cp_parser_assignment_expression
2082 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2083 static enum tree_code cp_parser_assignment_operator_opt
2084 (cp_parser *);
2085 static cp_expr cp_parser_expression
2086 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2087 static cp_expr cp_parser_constant_expression
2088 (cp_parser *, bool = false, bool * = NULL, bool = false);
2089 static cp_expr cp_parser_builtin_offsetof
2090 (cp_parser *);
2091 static cp_expr cp_parser_lambda_expression
2092 (cp_parser *);
2093 static void cp_parser_lambda_introducer
2094 (cp_parser *, tree);
2095 static bool cp_parser_lambda_declarator_opt
2096 (cp_parser *, tree);
2097 static void cp_parser_lambda_body
2098 (cp_parser *, tree);
2099
2100 /* Statements [gram.stmt.stmt] */
2101
2102 static void cp_parser_statement
2103 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2104 static void cp_parser_label_for_labeled_statement
2105 (cp_parser *, tree);
2106 static tree cp_parser_expression_statement
2107 (cp_parser *, tree);
2108 static tree cp_parser_compound_statement
2109 (cp_parser *, tree, int, bool);
2110 static void cp_parser_statement_seq_opt
2111 (cp_parser *, tree);
2112 static tree cp_parser_selection_statement
2113 (cp_parser *, bool *, vec<tree> *);
2114 static tree cp_parser_condition
2115 (cp_parser *);
2116 static tree cp_parser_iteration_statement
2117 (cp_parser *, bool *, bool, unsigned short);
2118 static bool cp_parser_init_statement
2119 (cp_parser *, tree *decl);
2120 static tree cp_parser_for
2121 (cp_parser *, bool, unsigned short);
2122 static tree cp_parser_c_for
2123 (cp_parser *, tree, tree, bool, unsigned short);
2124 static tree cp_parser_range_for
2125 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2126 static void do_range_for_auto_deduction
2127 (tree, tree);
2128 static tree cp_parser_perform_range_for_lookup
2129 (tree, tree *, tree *);
2130 static tree cp_parser_range_for_member_function
2131 (tree, tree);
2132 static tree cp_parser_jump_statement
2133 (cp_parser *);
2134 static void cp_parser_declaration_statement
2135 (cp_parser *);
2136
2137 static tree cp_parser_implicitly_scoped_statement
2138 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2139 static void cp_parser_already_scoped_statement
2140 (cp_parser *, bool *, const token_indent_info &);
2141
2142 /* Declarations [gram.dcl.dcl] */
2143
2144 static void cp_parser_declaration_seq_opt
2145 (cp_parser *);
2146 static void cp_parser_declaration
2147 (cp_parser *);
2148 static void cp_parser_toplevel_declaration
2149 (cp_parser *);
2150 static void cp_parser_block_declaration
2151 (cp_parser *, bool);
2152 static void cp_parser_simple_declaration
2153 (cp_parser *, bool, tree *);
2154 static void cp_parser_decl_specifier_seq
2155 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2156 static tree cp_parser_storage_class_specifier_opt
2157 (cp_parser *);
2158 static tree cp_parser_function_specifier_opt
2159 (cp_parser *, cp_decl_specifier_seq *);
2160 static tree cp_parser_type_specifier
2161 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2162 int *, bool *);
2163 static tree cp_parser_simple_type_specifier
2164 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2165 static tree cp_parser_type_name
2166 (cp_parser *, bool);
2167 static tree cp_parser_nonclass_name
2168 (cp_parser* parser);
2169 static tree cp_parser_elaborated_type_specifier
2170 (cp_parser *, bool, bool);
2171 static tree cp_parser_enum_specifier
2172 (cp_parser *);
2173 static void cp_parser_enumerator_list
2174 (cp_parser *, tree);
2175 static void cp_parser_enumerator_definition
2176 (cp_parser *, tree);
2177 static tree cp_parser_namespace_name
2178 (cp_parser *);
2179 static void cp_parser_namespace_definition
2180 (cp_parser *);
2181 static void cp_parser_namespace_body
2182 (cp_parser *);
2183 static tree cp_parser_qualified_namespace_specifier
2184 (cp_parser *);
2185 static void cp_parser_namespace_alias_definition
2186 (cp_parser *);
2187 static bool cp_parser_using_declaration
2188 (cp_parser *, bool);
2189 static void cp_parser_using_directive
2190 (cp_parser *);
2191 static tree cp_parser_alias_declaration
2192 (cp_parser *);
2193 static void cp_parser_asm_definition
2194 (cp_parser *);
2195 static void cp_parser_linkage_specification
2196 (cp_parser *);
2197 static void cp_parser_static_assert
2198 (cp_parser *, bool);
2199 static tree cp_parser_decltype
2200 (cp_parser *);
2201 static tree cp_parser_decomposition_declaration
2202 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2203
2204 /* Declarators [gram.dcl.decl] */
2205
2206 static tree cp_parser_init_declarator
2207 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2208 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2209 location_t *, tree *);
2210 static cp_declarator *cp_parser_declarator
2211 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2212 bool, bool, bool);
2213 static cp_declarator *cp_parser_direct_declarator
2214 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2215 bool);
2216 static enum tree_code cp_parser_ptr_operator
2217 (cp_parser *, tree *, cp_cv_quals *, tree *);
2218 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2219 (cp_parser *);
2220 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2221 (cp_parser *);
2222 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2223 (cp_parser *);
2224 static tree cp_parser_tx_qualifier_opt
2225 (cp_parser *);
2226 static tree cp_parser_late_return_type_opt
2227 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2228 static tree cp_parser_declarator_id
2229 (cp_parser *, bool);
2230 static tree cp_parser_type_id
2231 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2232 static tree cp_parser_template_type_arg
2233 (cp_parser *);
2234 static tree cp_parser_trailing_type_id (cp_parser *);
2235 static tree cp_parser_type_id_1
2236 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2237 static void cp_parser_type_specifier_seq
2238 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2239 static tree cp_parser_parameter_declaration_clause
2240 (cp_parser *, cp_parser_flags);
2241 static tree cp_parser_parameter_declaration_list
2242 (cp_parser *, cp_parser_flags);
2243 static cp_parameter_declarator *cp_parser_parameter_declaration
2244 (cp_parser *, cp_parser_flags, bool, bool *);
2245 static tree cp_parser_default_argument
2246 (cp_parser *, bool);
2247 static void cp_parser_function_body
2248 (cp_parser *, bool);
2249 static tree cp_parser_initializer
2250 (cp_parser *, bool *, bool *, bool = false);
2251 static cp_expr cp_parser_initializer_clause
2252 (cp_parser *, bool *);
2253 static cp_expr cp_parser_braced_list
2254 (cp_parser*, bool*);
2255 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2256 (cp_parser *, bool *, bool *);
2257
2258 static void cp_parser_ctor_initializer_opt_and_function_body
2259 (cp_parser *, bool);
2260
2261 static tree cp_parser_late_parsing_omp_declare_simd
2262 (cp_parser *, tree);
2263
2264 static tree cp_parser_late_parsing_oacc_routine
2265 (cp_parser *, tree);
2266
2267 static tree synthesize_implicit_template_parm
2268 (cp_parser *, tree);
2269 static tree finish_fully_implicit_template
2270 (cp_parser *, tree);
2271 static void abort_fully_implicit_template
2272 (cp_parser *);
2273
2274 /* Classes [gram.class] */
2275
2276 static tree cp_parser_class_name
2277 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2278 static tree cp_parser_class_specifier
2279 (cp_parser *);
2280 static tree cp_parser_class_head
2281 (cp_parser *, bool *);
2282 static enum tag_types cp_parser_class_key
2283 (cp_parser *);
2284 static void cp_parser_type_parameter_key
2285 (cp_parser* parser);
2286 static void cp_parser_member_specification_opt
2287 (cp_parser *);
2288 static void cp_parser_member_declaration
2289 (cp_parser *);
2290 static tree cp_parser_pure_specifier
2291 (cp_parser *);
2292 static tree cp_parser_constant_initializer
2293 (cp_parser *);
2294
2295 /* Derived classes [gram.class.derived] */
2296
2297 static tree cp_parser_base_clause
2298 (cp_parser *);
2299 static tree cp_parser_base_specifier
2300 (cp_parser *);
2301
2302 /* Special member functions [gram.special] */
2303
2304 static tree cp_parser_conversion_function_id
2305 (cp_parser *);
2306 static tree cp_parser_conversion_type_id
2307 (cp_parser *);
2308 static cp_declarator *cp_parser_conversion_declarator_opt
2309 (cp_parser *);
2310 static void cp_parser_ctor_initializer_opt
2311 (cp_parser *);
2312 static void cp_parser_mem_initializer_list
2313 (cp_parser *);
2314 static tree cp_parser_mem_initializer
2315 (cp_parser *);
2316 static tree cp_parser_mem_initializer_id
2317 (cp_parser *);
2318
2319 /* Overloading [gram.over] */
2320
2321 static cp_expr cp_parser_operator_function_id
2322 (cp_parser *);
2323 static cp_expr cp_parser_operator
2324 (cp_parser *, location_t);
2325
2326 /* Templates [gram.temp] */
2327
2328 static void cp_parser_template_declaration
2329 (cp_parser *, bool);
2330 static tree cp_parser_template_parameter_list
2331 (cp_parser *);
2332 static tree cp_parser_template_parameter
2333 (cp_parser *, bool *, bool *);
2334 static tree cp_parser_type_parameter
2335 (cp_parser *, bool *);
2336 static tree cp_parser_template_id
2337 (cp_parser *, bool, bool, enum tag_types, bool);
2338 static tree cp_parser_template_name
2339 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2340 static tree cp_parser_template_argument_list
2341 (cp_parser *);
2342 static tree cp_parser_template_argument
2343 (cp_parser *);
2344 static void cp_parser_explicit_instantiation
2345 (cp_parser *);
2346 static void cp_parser_explicit_specialization
2347 (cp_parser *);
2348
2349 /* Exception handling [gram.exception] */
2350
2351 static tree cp_parser_try_block
2352 (cp_parser *);
2353 static void cp_parser_function_try_block
2354 (cp_parser *);
2355 static void cp_parser_handler_seq
2356 (cp_parser *);
2357 static void cp_parser_handler
2358 (cp_parser *);
2359 static tree cp_parser_exception_declaration
2360 (cp_parser *);
2361 static tree cp_parser_throw_expression
2362 (cp_parser *);
2363 static tree cp_parser_exception_specification_opt
2364 (cp_parser *);
2365 static tree cp_parser_type_id_list
2366 (cp_parser *);
2367
2368 /* GNU Extensions */
2369
2370 static tree cp_parser_asm_specification_opt
2371 (cp_parser *);
2372 static tree cp_parser_asm_operand_list
2373 (cp_parser *);
2374 static tree cp_parser_asm_clobber_list
2375 (cp_parser *);
2376 static tree cp_parser_asm_label_list
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_attribute_p
2379 (cp_parser *);
2380 static bool cp_next_tokens_can_be_gnu_attribute_p
2381 (cp_parser *);
2382 static bool cp_next_tokens_can_be_std_attribute_p
2383 (cp_parser *);
2384 static bool cp_nth_tokens_can_be_std_attribute_p
2385 (cp_parser *, size_t);
2386 static bool cp_nth_tokens_can_be_gnu_attribute_p
2387 (cp_parser *, size_t);
2388 static bool cp_nth_tokens_can_be_attribute_p
2389 (cp_parser *, size_t);
2390 static tree cp_parser_attributes_opt
2391 (cp_parser *);
2392 static tree cp_parser_gnu_attributes_opt
2393 (cp_parser *);
2394 static tree cp_parser_gnu_attribute_list
2395 (cp_parser *, bool = false);
2396 static tree cp_parser_std_attribute
2397 (cp_parser *, tree);
2398 static tree cp_parser_std_attribute_spec
2399 (cp_parser *);
2400 static tree cp_parser_std_attribute_spec_seq
2401 (cp_parser *);
2402 static size_t cp_parser_skip_attributes_opt
2403 (cp_parser *, size_t);
2404 static bool cp_parser_extension_opt
2405 (cp_parser *, int *);
2406 static void cp_parser_label_declaration
2407 (cp_parser *);
2408
2409 /* Concept Extensions */
2410
2411 static tree cp_parser_requires_clause
2412 (cp_parser *);
2413 static tree cp_parser_requires_clause_opt
2414 (cp_parser *);
2415 static tree cp_parser_requires_expression
2416 (cp_parser *);
2417 static tree cp_parser_requirement_parameter_list
2418 (cp_parser *);
2419 static tree cp_parser_requirement_body
2420 (cp_parser *);
2421 static tree cp_parser_requirement_list
2422 (cp_parser *);
2423 static tree cp_parser_requirement
2424 (cp_parser *);
2425 static tree cp_parser_simple_requirement
2426 (cp_parser *);
2427 static tree cp_parser_compound_requirement
2428 (cp_parser *);
2429 static tree cp_parser_type_requirement
2430 (cp_parser *);
2431 static tree cp_parser_nested_requirement
2432 (cp_parser *);
2433
2434 /* Transactional Memory Extensions */
2435
2436 static tree cp_parser_transaction
2437 (cp_parser *, cp_token *);
2438 static tree cp_parser_transaction_expression
2439 (cp_parser *, enum rid);
2440 static void cp_parser_function_transaction
2441 (cp_parser *, enum rid);
2442 static tree cp_parser_transaction_cancel
2443 (cp_parser *);
2444
2445 enum pragma_context {
2446 pragma_external,
2447 pragma_member,
2448 pragma_objc_icode,
2449 pragma_stmt,
2450 pragma_compound
2451 };
2452 static bool cp_parser_pragma
2453 (cp_parser *, enum pragma_context, bool *);
2454
2455 /* Objective-C++ Productions */
2456
2457 static tree cp_parser_objc_message_receiver
2458 (cp_parser *);
2459 static tree cp_parser_objc_message_args
2460 (cp_parser *);
2461 static tree cp_parser_objc_message_expression
2462 (cp_parser *);
2463 static cp_expr cp_parser_objc_encode_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_defs_expression
2466 (cp_parser *);
2467 static tree cp_parser_objc_protocol_expression
2468 (cp_parser *);
2469 static tree cp_parser_objc_selector_expression
2470 (cp_parser *);
2471 static cp_expr cp_parser_objc_expression
2472 (cp_parser *);
2473 static bool cp_parser_objc_selector_p
2474 (enum cpp_ttype);
2475 static tree cp_parser_objc_selector
2476 (cp_parser *);
2477 static tree cp_parser_objc_protocol_refs_opt
2478 (cp_parser *);
2479 static void cp_parser_objc_declaration
2480 (cp_parser *, tree);
2481 static tree cp_parser_objc_statement
2482 (cp_parser *);
2483 static bool cp_parser_objc_valid_prefix_attributes
2484 (cp_parser *, tree *);
2485 static void cp_parser_objc_at_property_declaration
2486 (cp_parser *) ;
2487 static void cp_parser_objc_at_synthesize_declaration
2488 (cp_parser *) ;
2489 static void cp_parser_objc_at_dynamic_declaration
2490 (cp_parser *) ;
2491 static tree cp_parser_objc_struct_declaration
2492 (cp_parser *) ;
2493
2494 /* Utility Routines */
2495
2496 static cp_expr cp_parser_lookup_name
2497 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2498 static tree cp_parser_lookup_name_simple
2499 (cp_parser *, tree, location_t);
2500 static tree cp_parser_maybe_treat_template_as_class
2501 (tree, bool);
2502 static bool cp_parser_check_declarator_template_parameters
2503 (cp_parser *, cp_declarator *, location_t);
2504 static bool cp_parser_check_template_parameters
2505 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2506 static cp_expr cp_parser_simple_cast_expression
2507 (cp_parser *);
2508 static tree cp_parser_global_scope_opt
2509 (cp_parser *, bool);
2510 static bool cp_parser_constructor_declarator_p
2511 (cp_parser *, cp_parser_flags, bool);
2512 static tree cp_parser_function_definition_from_specifiers_and_declarator
2513 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2514 static tree cp_parser_function_definition_after_declarator
2515 (cp_parser *, bool);
2516 static bool cp_parser_template_declaration_after_export
2517 (cp_parser *, bool);
2518 static void cp_parser_perform_template_parameter_access_checks
2519 (vec<deferred_access_check, va_gc> *);
2520 static tree cp_parser_single_declaration
2521 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2522 static cp_expr cp_parser_functional_cast
2523 (cp_parser *, tree);
2524 static tree cp_parser_save_member_function_body
2525 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2526 static tree cp_parser_save_nsdmi
2527 (cp_parser *);
2528 static tree cp_parser_enclosed_template_argument_list
2529 (cp_parser *);
2530 static void cp_parser_save_default_args
2531 (cp_parser *, tree);
2532 static void cp_parser_late_parsing_for_member
2533 (cp_parser *, tree);
2534 static tree cp_parser_late_parse_one_default_arg
2535 (cp_parser *, tree, tree, tree);
2536 static void cp_parser_late_parsing_nsdmi
2537 (cp_parser *, tree);
2538 static void cp_parser_late_parsing_default_args
2539 (cp_parser *, tree);
2540 static tree cp_parser_sizeof_operand
2541 (cp_parser *, enum rid);
2542 static cp_expr cp_parser_trait_expr
2543 (cp_parser *, enum rid);
2544 static bool cp_parser_declares_only_class_p
2545 (cp_parser *);
2546 static void cp_parser_set_storage_class
2547 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2548 static void cp_parser_set_decl_spec_type
2549 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2550 static void set_and_check_decl_spec_loc
2551 (cp_decl_specifier_seq *decl_specs,
2552 cp_decl_spec ds, cp_token *);
2553 static bool cp_parser_friend_p
2554 (const cp_decl_specifier_seq *);
2555 static void cp_parser_required_error
2556 (cp_parser *, required_token, bool, location_t);
2557 static cp_token *cp_parser_require
2558 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2559 static cp_token *cp_parser_require_keyword
2560 (cp_parser *, enum rid, required_token);
2561 static bool cp_parser_token_starts_function_definition_p
2562 (cp_token *);
2563 static bool cp_parser_next_token_starts_class_definition_p
2564 (cp_parser *);
2565 static bool cp_parser_next_token_ends_template_argument_p
2566 (cp_parser *);
2567 static bool cp_parser_nth_token_starts_template_argument_list_p
2568 (cp_parser *, size_t);
2569 static enum tag_types cp_parser_token_is_class_key
2570 (cp_token *);
2571 static enum tag_types cp_parser_token_is_type_parameter_key
2572 (cp_token *);
2573 static void cp_parser_check_class_key
2574 (enum tag_types, tree type);
2575 static void cp_parser_check_access_in_redeclaration
2576 (tree type, location_t location);
2577 static bool cp_parser_optional_template_keyword
2578 (cp_parser *);
2579 static void cp_parser_pre_parsed_nested_name_specifier
2580 (cp_parser *);
2581 static bool cp_parser_cache_group
2582 (cp_parser *, enum cpp_ttype, unsigned);
2583 static tree cp_parser_cache_defarg
2584 (cp_parser *parser, bool nsdmi);
2585 static void cp_parser_parse_tentatively
2586 (cp_parser *);
2587 static void cp_parser_commit_to_tentative_parse
2588 (cp_parser *);
2589 static void cp_parser_commit_to_topmost_tentative_parse
2590 (cp_parser *);
2591 static void cp_parser_abort_tentative_parse
2592 (cp_parser *);
2593 static bool cp_parser_parse_definitely
2594 (cp_parser *);
2595 static inline bool cp_parser_parsing_tentatively
2596 (cp_parser *);
2597 static bool cp_parser_uncommitted_to_tentative_parse_p
2598 (cp_parser *);
2599 static void cp_parser_error
2600 (cp_parser *, const char *);
2601 static void cp_parser_name_lookup_error
2602 (cp_parser *, tree, tree, name_lookup_error, location_t);
2603 static bool cp_parser_simulate_error
2604 (cp_parser *);
2605 static bool cp_parser_check_type_definition
2606 (cp_parser *);
2607 static void cp_parser_check_for_definition_in_return_type
2608 (cp_declarator *, tree, location_t type_location);
2609 static void cp_parser_check_for_invalid_template_id
2610 (cp_parser *, tree, enum tag_types, location_t location);
2611 static bool cp_parser_non_integral_constant_expression
2612 (cp_parser *, non_integral_constant);
2613 static void cp_parser_diagnose_invalid_type_name
2614 (cp_parser *, tree, location_t);
2615 static bool cp_parser_parse_and_diagnose_invalid_type_name
2616 (cp_parser *);
2617 static int cp_parser_skip_to_closing_parenthesis
2618 (cp_parser *, bool, bool, bool);
2619 static void cp_parser_skip_to_end_of_statement
2620 (cp_parser *);
2621 static void cp_parser_consume_semicolon_at_end_of_statement
2622 (cp_parser *);
2623 static void cp_parser_skip_to_end_of_block_or_statement
2624 (cp_parser *);
2625 static bool cp_parser_skip_to_closing_brace
2626 (cp_parser *);
2627 static void cp_parser_skip_to_end_of_template_parameter_list
2628 (cp_parser *);
2629 static void cp_parser_skip_to_pragma_eol
2630 (cp_parser*, cp_token *);
2631 static bool cp_parser_error_occurred
2632 (cp_parser *);
2633 static bool cp_parser_allow_gnu_extensions_p
2634 (cp_parser *);
2635 static bool cp_parser_is_pure_string_literal
2636 (cp_token *);
2637 static bool cp_parser_is_string_literal
2638 (cp_token *);
2639 static bool cp_parser_is_keyword
2640 (cp_token *, enum rid);
2641 static tree cp_parser_make_typename_type
2642 (cp_parser *, tree, location_t location);
2643 static cp_declarator * cp_parser_make_indirect_declarator
2644 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2645 static bool cp_parser_compound_literal_p
2646 (cp_parser *);
2647 static bool cp_parser_array_designator_p
2648 (cp_parser *);
2649 static bool cp_parser_init_statement_p
2650 (cp_parser *);
2651 static bool cp_parser_skip_to_closing_square_bracket
2652 (cp_parser *);
2653
2654 /* Concept-related syntactic transformations */
2655
2656 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2657 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2658
2659 // -------------------------------------------------------------------------- //
2660 // Unevaluated Operand Guard
2661 //
2662 // Implementation of an RAII helper for unevaluated operand parsing.
2663 cp_unevaluated::cp_unevaluated ()
2664 {
2665 ++cp_unevaluated_operand;
2666 ++c_inhibit_evaluation_warnings;
2667 }
2668
2669 cp_unevaluated::~cp_unevaluated ()
2670 {
2671 --c_inhibit_evaluation_warnings;
2672 --cp_unevaluated_operand;
2673 }
2674
2675 // -------------------------------------------------------------------------- //
2676 // Tentative Parsing
2677
2678 /* Returns nonzero if we are parsing tentatively. */
2679
2680 static inline bool
2681 cp_parser_parsing_tentatively (cp_parser* parser)
2682 {
2683 return parser->context->next != NULL;
2684 }
2685
2686 /* Returns nonzero if TOKEN is a string literal. */
2687
2688 static bool
2689 cp_parser_is_pure_string_literal (cp_token* token)
2690 {
2691 return (token->type == CPP_STRING ||
2692 token->type == CPP_STRING16 ||
2693 token->type == CPP_STRING32 ||
2694 token->type == CPP_WSTRING ||
2695 token->type == CPP_UTF8STRING);
2696 }
2697
2698 /* Returns nonzero if TOKEN is a string literal
2699 of a user-defined string literal. */
2700
2701 static bool
2702 cp_parser_is_string_literal (cp_token* token)
2703 {
2704 return (cp_parser_is_pure_string_literal (token) ||
2705 token->type == CPP_STRING_USERDEF ||
2706 token->type == CPP_STRING16_USERDEF ||
2707 token->type == CPP_STRING32_USERDEF ||
2708 token->type == CPP_WSTRING_USERDEF ||
2709 token->type == CPP_UTF8STRING_USERDEF);
2710 }
2711
2712 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2713
2714 static bool
2715 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2716 {
2717 return token->keyword == keyword;
2718 }
2719
2720 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2721 PRAGMA_NONE. */
2722
2723 static enum pragma_kind
2724 cp_parser_pragma_kind (cp_token *token)
2725 {
2726 if (token->type != CPP_PRAGMA)
2727 return PRAGMA_NONE;
2728 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2729 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2730 }
2731
2732 /* Helper function for cp_parser_error.
2733 Having peeked a token of kind TOK1_KIND that might signify
2734 a conflict marker, peek successor tokens to determine
2735 if we actually do have a conflict marker.
2736 Specifically, we consider a run of 7 '<', '=' or '>' characters
2737 at the start of a line as a conflict marker.
2738 These come through the lexer as three pairs and a single,
2739 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2740 If it returns true, *OUT_LOC is written to with the location/range
2741 of the marker. */
2742
2743 static bool
2744 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2745 location_t *out_loc)
2746 {
2747 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2748 if (token2->type != tok1_kind)
2749 return false;
2750 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2751 if (token3->type != tok1_kind)
2752 return false;
2753 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2754 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2755 return false;
2756
2757 /* It must be at the start of the line. */
2758 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2759 if (LOCATION_COLUMN (start_loc) != 1)
2760 return false;
2761
2762 /* We have a conflict marker. Construct a location of the form:
2763 <<<<<<<
2764 ^~~~~~~
2765 with start == caret, finishing at the end of the marker. */
2766 location_t finish_loc = get_finish (token4->location);
2767 *out_loc = make_location (start_loc, start_loc, finish_loc);
2768
2769 return true;
2770 }
2771
2772 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2773 RT_CLOSE_PAREN. */
2774
2775 static const char *
2776 get_matching_symbol (required_token token_desc)
2777 {
2778 switch (token_desc)
2779 {
2780 default:
2781 gcc_unreachable ();
2782 return "";
2783 case RT_CLOSE_BRACE:
2784 return "{";
2785 case RT_CLOSE_PAREN:
2786 return "(";
2787 }
2788 }
2789
2790 /* Attempt to convert TOKEN_DESC from a required_token to an
2791 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2792
2793 static enum cpp_ttype
2794 get_required_cpp_ttype (required_token token_desc)
2795 {
2796 switch (token_desc)
2797 {
2798 case RT_SEMICOLON:
2799 return CPP_SEMICOLON;
2800 case RT_OPEN_PAREN:
2801 return CPP_OPEN_PAREN;
2802 case RT_CLOSE_BRACE:
2803 return CPP_CLOSE_BRACE;
2804 case RT_OPEN_BRACE:
2805 return CPP_OPEN_BRACE;
2806 case RT_CLOSE_SQUARE:
2807 return CPP_CLOSE_SQUARE;
2808 case RT_OPEN_SQUARE:
2809 return CPP_OPEN_SQUARE;
2810 case RT_COMMA:
2811 return CPP_COMMA;
2812 case RT_COLON:
2813 return CPP_COLON;
2814 case RT_CLOSE_PAREN:
2815 return CPP_CLOSE_PAREN;
2816
2817 default:
2818 /* Use CPP_EOF as a "no completions possible" code. */
2819 return CPP_EOF;
2820 }
2821 }
2822
2823
2824 /* Subroutine of cp_parser_error and cp_parser_required_error.
2825
2826 Issue a diagnostic of the form
2827 FILE:LINE: MESSAGE before TOKEN
2828 where TOKEN is the next token in the input stream. MESSAGE
2829 (specified by the caller) is usually of the form "expected
2830 OTHER-TOKEN".
2831
2832 This bypasses the check for tentative passing, and potentially
2833 adds material needed by cp_parser_required_error.
2834
2835 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2836 suggesting insertion of the missing token.
2837
2838 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2839 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2840 location. */
2841
2842 static void
2843 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2844 required_token missing_token_desc,
2845 location_t matching_location)
2846 {
2847 cp_token *token = cp_lexer_peek_token (parser->lexer);
2848 /* This diagnostic makes more sense if it is tagged to the line
2849 of the token we just peeked at. */
2850 cp_lexer_set_source_position_from_token (token);
2851
2852 if (token->type == CPP_PRAGMA)
2853 {
2854 error_at (token->location,
2855 "%<#pragma%> is not allowed here");
2856 cp_parser_skip_to_pragma_eol (parser, token);
2857 return;
2858 }
2859
2860 /* If this is actually a conflict marker, report it as such. */
2861 if (token->type == CPP_LSHIFT
2862 || token->type == CPP_RSHIFT
2863 || token->type == CPP_EQ_EQ)
2864 {
2865 location_t loc;
2866 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2867 {
2868 error_at (loc, "version control conflict marker in file");
2869 expanded_location token_exploc = expand_location (token->location);
2870 /* Consume tokens until the end of the source line. */
2871 while (1)
2872 {
2873 cp_lexer_consume_token (parser->lexer);
2874 cp_token *next = cp_lexer_peek_token (parser->lexer);
2875 if (next == NULL)
2876 break;
2877 expanded_location next_exploc = expand_location (next->location);
2878 if (next_exploc.file != token_exploc.file)
2879 break;
2880 if (next_exploc.line != token_exploc.line)
2881 break;
2882 }
2883 return;
2884 }
2885 }
2886
2887 gcc_rich_location richloc (input_location);
2888
2889 bool added_matching_location = false;
2890
2891 if (missing_token_desc != RT_NONE)
2892 {
2893 /* Potentially supply a fix-it hint, suggesting to add the
2894 missing token immediately after the *previous* token.
2895 This may move the primary location within richloc. */
2896 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2897 location_t prev_token_loc
2898 = cp_lexer_previous_token (parser->lexer)->location;
2899 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2900
2901 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2902 Attempt to consolidate diagnostics by printing it as a
2903 secondary range within the main diagnostic. */
2904 if (matching_location != UNKNOWN_LOCATION)
2905 added_matching_location
2906 = richloc.add_location_if_nearby (matching_location);
2907 }
2908
2909 /* Actually emit the error. */
2910 c_parse_error (gmsgid,
2911 /* Because c_parser_error does not understand
2912 CPP_KEYWORD, keywords are treated like
2913 identifiers. */
2914 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2915 token->u.value, token->flags, &richloc);
2916
2917 if (missing_token_desc != RT_NONE)
2918 {
2919 /* If we weren't able to consolidate matching_location, then
2920 print it as a secondary diagnostic. */
2921 if (matching_location != UNKNOWN_LOCATION
2922 && !added_matching_location)
2923 inform (matching_location, "to match this %qs",
2924 get_matching_symbol (missing_token_desc));
2925 }
2926 }
2927
2928 /* If not parsing tentatively, issue a diagnostic of the form
2929 FILE:LINE: MESSAGE before TOKEN
2930 where TOKEN is the next token in the input stream. MESSAGE
2931 (specified by the caller) is usually of the form "expected
2932 OTHER-TOKEN". */
2933
2934 static void
2935 cp_parser_error (cp_parser* parser, const char* gmsgid)
2936 {
2937 if (!cp_parser_simulate_error (parser))
2938 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2939 }
2940
2941 /* Issue an error about name-lookup failing. NAME is the
2942 IDENTIFIER_NODE DECL is the result of
2943 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2944 the thing that we hoped to find. */
2945
2946 static void
2947 cp_parser_name_lookup_error (cp_parser* parser,
2948 tree name,
2949 tree decl,
2950 name_lookup_error desired,
2951 location_t location)
2952 {
2953 /* If name lookup completely failed, tell the user that NAME was not
2954 declared. */
2955 if (decl == error_mark_node)
2956 {
2957 if (parser->scope && parser->scope != global_namespace)
2958 error_at (location, "%<%E::%E%> has not been declared",
2959 parser->scope, name);
2960 else if (parser->scope == global_namespace)
2961 error_at (location, "%<::%E%> has not been declared", name);
2962 else if (parser->object_scope
2963 && !CLASS_TYPE_P (parser->object_scope))
2964 error_at (location, "request for member %qE in non-class type %qT",
2965 name, parser->object_scope);
2966 else if (parser->object_scope)
2967 error_at (location, "%<%T::%E%> has not been declared",
2968 parser->object_scope, name);
2969 else
2970 error_at (location, "%qE has not been declared", name);
2971 }
2972 else if (parser->scope && parser->scope != global_namespace)
2973 {
2974 switch (desired)
2975 {
2976 case NLE_TYPE:
2977 error_at (location, "%<%E::%E%> is not a type",
2978 parser->scope, name);
2979 break;
2980 case NLE_CXX98:
2981 error_at (location, "%<%E::%E%> is not a class or namespace",
2982 parser->scope, name);
2983 break;
2984 case NLE_NOT_CXX98:
2985 error_at (location,
2986 "%<%E::%E%> is not a class, namespace, or enumeration",
2987 parser->scope, name);
2988 break;
2989 default:
2990 gcc_unreachable ();
2991
2992 }
2993 }
2994 else if (parser->scope == global_namespace)
2995 {
2996 switch (desired)
2997 {
2998 case NLE_TYPE:
2999 error_at (location, "%<::%E%> is not a type", name);
3000 break;
3001 case NLE_CXX98:
3002 error_at (location, "%<::%E%> is not a class or namespace", name);
3003 break;
3004 case NLE_NOT_CXX98:
3005 error_at (location,
3006 "%<::%E%> is not a class, namespace, or enumeration",
3007 name);
3008 break;
3009 default:
3010 gcc_unreachable ();
3011 }
3012 }
3013 else
3014 {
3015 switch (desired)
3016 {
3017 case NLE_TYPE:
3018 error_at (location, "%qE is not a type", name);
3019 break;
3020 case NLE_CXX98:
3021 error_at (location, "%qE is not a class or namespace", name);
3022 break;
3023 case NLE_NOT_CXX98:
3024 error_at (location,
3025 "%qE is not a class, namespace, or enumeration", name);
3026 break;
3027 default:
3028 gcc_unreachable ();
3029 }
3030 }
3031 }
3032
3033 /* If we are parsing tentatively, remember that an error has occurred
3034 during this tentative parse. Returns true if the error was
3035 simulated; false if a message should be issued by the caller. */
3036
3037 static bool
3038 cp_parser_simulate_error (cp_parser* parser)
3039 {
3040 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3041 {
3042 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3043 return true;
3044 }
3045 return false;
3046 }
3047
3048 /* This function is called when a type is defined. If type
3049 definitions are forbidden at this point, an error message is
3050 issued. */
3051
3052 static bool
3053 cp_parser_check_type_definition (cp_parser* parser)
3054 {
3055 /* If types are forbidden here, issue a message. */
3056 if (parser->type_definition_forbidden_message)
3057 {
3058 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3059 or %qs in the message need to be interpreted. */
3060 error (parser->type_definition_forbidden_message,
3061 parser->type_definition_forbidden_message_arg);
3062 return false;
3063 }
3064 return true;
3065 }
3066
3067 /* This function is called when the DECLARATOR is processed. The TYPE
3068 was a type defined in the decl-specifiers. If it is invalid to
3069 define a type in the decl-specifiers for DECLARATOR, an error is
3070 issued. TYPE_LOCATION is the location of TYPE and is used
3071 for error reporting. */
3072
3073 static void
3074 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3075 tree type, location_t type_location)
3076 {
3077 /* [dcl.fct] forbids type definitions in return types.
3078 Unfortunately, it's not easy to know whether or not we are
3079 processing a return type until after the fact. */
3080 while (declarator
3081 && (declarator->kind == cdk_pointer
3082 || declarator->kind == cdk_reference
3083 || declarator->kind == cdk_ptrmem))
3084 declarator = declarator->declarator;
3085 if (declarator
3086 && declarator->kind == cdk_function)
3087 {
3088 error_at (type_location,
3089 "new types may not be defined in a return type");
3090 inform (type_location,
3091 "(perhaps a semicolon is missing after the definition of %qT)",
3092 type);
3093 }
3094 }
3095
3096 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3097 "<" in any valid C++ program. If the next token is indeed "<",
3098 issue a message warning the user about what appears to be an
3099 invalid attempt to form a template-id. LOCATION is the location
3100 of the type-specifier (TYPE) */
3101
3102 static void
3103 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3104 tree type,
3105 enum tag_types tag_type,
3106 location_t location)
3107 {
3108 cp_token_position start = 0;
3109
3110 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3111 {
3112 if (TREE_CODE (type) == TYPE_DECL)
3113 type = TREE_TYPE (type);
3114 if (TYPE_P (type) && !template_placeholder_p (type))
3115 error_at (location, "%qT is not a template", type);
3116 else if (identifier_p (type))
3117 {
3118 if (tag_type != none_type)
3119 error_at (location, "%qE is not a class template", type);
3120 else
3121 error_at (location, "%qE is not a template", type);
3122 }
3123 else
3124 error_at (location, "invalid template-id");
3125 /* Remember the location of the invalid "<". */
3126 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3127 start = cp_lexer_token_position (parser->lexer, true);
3128 /* Consume the "<". */
3129 cp_lexer_consume_token (parser->lexer);
3130 /* Parse the template arguments. */
3131 cp_parser_enclosed_template_argument_list (parser);
3132 /* Permanently remove the invalid template arguments so that
3133 this error message is not issued again. */
3134 if (start)
3135 cp_lexer_purge_tokens_after (parser->lexer, start);
3136 }
3137 }
3138
3139 /* If parsing an integral constant-expression, issue an error message
3140 about the fact that THING appeared and return true. Otherwise,
3141 return false. In either case, set
3142 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3143
3144 static bool
3145 cp_parser_non_integral_constant_expression (cp_parser *parser,
3146 non_integral_constant thing)
3147 {
3148 parser->non_integral_constant_expression_p = true;
3149 if (parser->integral_constant_expression_p)
3150 {
3151 if (!parser->allow_non_integral_constant_expression_p)
3152 {
3153 const char *msg = NULL;
3154 switch (thing)
3155 {
3156 case NIC_FLOAT:
3157 pedwarn (input_location, OPT_Wpedantic,
3158 "ISO C++ forbids using a floating-point literal "
3159 "in a constant-expression");
3160 return true;
3161 case NIC_CAST:
3162 error ("a cast to a type other than an integral or "
3163 "enumeration type cannot appear in a "
3164 "constant-expression");
3165 return true;
3166 case NIC_TYPEID:
3167 error ("%<typeid%> operator "
3168 "cannot appear in a constant-expression");
3169 return true;
3170 case NIC_NCC:
3171 error ("non-constant compound literals "
3172 "cannot appear in a constant-expression");
3173 return true;
3174 case NIC_FUNC_CALL:
3175 error ("a function call "
3176 "cannot appear in a constant-expression");
3177 return true;
3178 case NIC_INC:
3179 error ("an increment "
3180 "cannot appear in a constant-expression");
3181 return true;
3182 case NIC_DEC:
3183 error ("an decrement "
3184 "cannot appear in a constant-expression");
3185 return true;
3186 case NIC_ARRAY_REF:
3187 error ("an array reference "
3188 "cannot appear in a constant-expression");
3189 return true;
3190 case NIC_ADDR_LABEL:
3191 error ("the address of a label "
3192 "cannot appear in a constant-expression");
3193 return true;
3194 case NIC_OVERLOADED:
3195 error ("calls to overloaded operators "
3196 "cannot appear in a constant-expression");
3197 return true;
3198 case NIC_ASSIGNMENT:
3199 error ("an assignment cannot appear in a constant-expression");
3200 return true;
3201 case NIC_COMMA:
3202 error ("a comma operator "
3203 "cannot appear in a constant-expression");
3204 return true;
3205 case NIC_CONSTRUCTOR:
3206 error ("a call to a constructor "
3207 "cannot appear in a constant-expression");
3208 return true;
3209 case NIC_TRANSACTION:
3210 error ("a transaction expression "
3211 "cannot appear in a constant-expression");
3212 return true;
3213 case NIC_THIS:
3214 msg = "this";
3215 break;
3216 case NIC_FUNC_NAME:
3217 msg = "__FUNCTION__";
3218 break;
3219 case NIC_PRETTY_FUNC:
3220 msg = "__PRETTY_FUNCTION__";
3221 break;
3222 case NIC_C99_FUNC:
3223 msg = "__func__";
3224 break;
3225 case NIC_VA_ARG:
3226 msg = "va_arg";
3227 break;
3228 case NIC_ARROW:
3229 msg = "->";
3230 break;
3231 case NIC_POINT:
3232 msg = ".";
3233 break;
3234 case NIC_STAR:
3235 msg = "*";
3236 break;
3237 case NIC_ADDR:
3238 msg = "&";
3239 break;
3240 case NIC_PREINCREMENT:
3241 msg = "++";
3242 break;
3243 case NIC_PREDECREMENT:
3244 msg = "--";
3245 break;
3246 case NIC_NEW:
3247 msg = "new";
3248 break;
3249 case NIC_DEL:
3250 msg = "delete";
3251 break;
3252 default:
3253 gcc_unreachable ();
3254 }
3255 if (msg)
3256 error ("%qs cannot appear in a constant-expression", msg);
3257 return true;
3258 }
3259 }
3260 return false;
3261 }
3262
3263 /* Emit a diagnostic for an invalid type name. This function commits
3264 to the current active tentative parse, if any. (Otherwise, the
3265 problematic construct might be encountered again later, resulting
3266 in duplicate error messages.) LOCATION is the location of ID. */
3267
3268 static void
3269 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3270 location_t location)
3271 {
3272 tree decl, ambiguous_decls;
3273 cp_parser_commit_to_tentative_parse (parser);
3274 /* Try to lookup the identifier. */
3275 decl = cp_parser_lookup_name (parser, id, none_type,
3276 /*is_template=*/false,
3277 /*is_namespace=*/false,
3278 /*check_dependency=*/true,
3279 &ambiguous_decls, location);
3280 if (ambiguous_decls)
3281 /* If the lookup was ambiguous, an error will already have
3282 been issued. */
3283 return;
3284 /* If the lookup found a template-name, it means that the user forgot
3285 to specify an argument list. Emit a useful error message. */
3286 if (DECL_TYPE_TEMPLATE_P (decl))
3287 {
3288 auto_diagnostic_group d;
3289 error_at (location,
3290 "invalid use of template-name %qE without an argument list",
3291 decl);
3292 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3293 inform (location, "class template argument deduction is only available "
3294 "with %<-std=c++17%> or %<-std=gnu++17%>");
3295 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3296 }
3297 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3298 error_at (location, "invalid use of destructor %qD as a type", id);
3299 else if (TREE_CODE (decl) == TYPE_DECL)
3300 /* Something like 'unsigned A a;' */
3301 error_at (location, "invalid combination of multiple type-specifiers");
3302 else if (!parser->scope)
3303 {
3304 /* Issue an error message. */
3305 auto_diagnostic_group d;
3306 name_hint hint;
3307 if (TREE_CODE (id) == IDENTIFIER_NODE)
3308 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3309 if (const char *suggestion = hint.suggestion ())
3310 {
3311 gcc_rich_location richloc (location);
3312 richloc.add_fixit_replace (suggestion);
3313 error_at (&richloc,
3314 "%qE does not name a type; did you mean %qs?",
3315 id, suggestion);
3316 }
3317 else
3318 error_at (location, "%qE does not name a type", id);
3319 /* If we're in a template class, it's possible that the user was
3320 referring to a type from a base class. For example:
3321
3322 template <typename T> struct A { typedef T X; };
3323 template <typename T> struct B : public A<T> { X x; };
3324
3325 The user should have said "typename A<T>::X". */
3326 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3327 inform (location, "C++11 %<constexpr%> only available with "
3328 "%<-std=c++11%> or %<-std=gnu++11%>");
3329 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3330 inform (location, "C++11 %<noexcept%> only available with "
3331 "%<-std=c++11%> or %<-std=gnu++11%>");
3332 else if (cxx_dialect < cxx11
3333 && TREE_CODE (id) == IDENTIFIER_NODE
3334 && id_equal (id, "thread_local"))
3335 inform (location, "C++11 %<thread_local%> only available with "
3336 "%<-std=c++11%> or %<-std=gnu++11%>");
3337 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3338 inform (location, "%<concept%> only available with %<-fconcepts%>");
3339 else if (processing_template_decl && current_class_type
3340 && TYPE_BINFO (current_class_type))
3341 {
3342 tree b;
3343
3344 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3345 b;
3346 b = TREE_CHAIN (b))
3347 {
3348 tree base_type = BINFO_TYPE (b);
3349 if (CLASS_TYPE_P (base_type)
3350 && dependent_type_p (base_type))
3351 {
3352 tree field;
3353 /* Go from a particular instantiation of the
3354 template (which will have an empty TYPE_FIELDs),
3355 to the main version. */
3356 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3357 for (field = TYPE_FIELDS (base_type);
3358 field;
3359 field = DECL_CHAIN (field))
3360 if (TREE_CODE (field) == TYPE_DECL
3361 && DECL_NAME (field) == id)
3362 {
3363 inform (location,
3364 "(perhaps %<typename %T::%E%> was intended)",
3365 BINFO_TYPE (b), id);
3366 break;
3367 }
3368 if (field)
3369 break;
3370 }
3371 }
3372 }
3373 }
3374 /* Here we diagnose qualified-ids where the scope is actually correct,
3375 but the identifier does not resolve to a valid type name. */
3376 else if (parser->scope != error_mark_node)
3377 {
3378 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3379 {
3380 auto_diagnostic_group d;
3381 name_hint hint;
3382 if (decl == error_mark_node)
3383 hint = suggest_alternative_in_explicit_scope (location, id,
3384 parser->scope);
3385 const char *suggestion = hint.suggestion ();
3386 gcc_rich_location richloc (location_of (id));
3387 if (suggestion)
3388 richloc.add_fixit_replace (suggestion);
3389 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3390 {
3391 if (suggestion)
3392 error_at (&richloc,
3393 "%qE in namespace %qE does not name a template"
3394 " type; did you mean %qs?",
3395 id, parser->scope, suggestion);
3396 else
3397 error_at (&richloc,
3398 "%qE in namespace %qE does not name a template type",
3399 id, parser->scope);
3400 }
3401 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3402 {
3403 if (suggestion)
3404 error_at (&richloc,
3405 "%qE in namespace %qE does not name a template"
3406 " type; did you mean %qs?",
3407 TREE_OPERAND (id, 0), parser->scope, suggestion);
3408 else
3409 error_at (&richloc,
3410 "%qE in namespace %qE does not name a template"
3411 " type",
3412 TREE_OPERAND (id, 0), parser->scope);
3413 }
3414 else
3415 {
3416 if (suggestion)
3417 error_at (&richloc,
3418 "%qE in namespace %qE does not name a type"
3419 "; did you mean %qs?",
3420 id, parser->scope, suggestion);
3421 else
3422 error_at (&richloc,
3423 "%qE in namespace %qE does not name a type",
3424 id, parser->scope);
3425 }
3426 if (DECL_P (decl))
3427 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3428 }
3429 else if (CLASS_TYPE_P (parser->scope)
3430 && constructor_name_p (id, parser->scope))
3431 {
3432 /* A<T>::A<T>() */
3433 auto_diagnostic_group d;
3434 error_at (location, "%<%T::%E%> names the constructor, not"
3435 " the type", parser->scope, id);
3436 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3437 error_at (location, "and %qT has no template constructors",
3438 parser->scope);
3439 }
3440 else if (TYPE_P (parser->scope)
3441 && dependent_scope_p (parser->scope))
3442 {
3443 gcc_rich_location richloc (location);
3444 richloc.add_fixit_insert_before ("typename ");
3445 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3446 error_at (&richloc,
3447 "need %<typename%> before %<%T::%D::%E%> because "
3448 "%<%T::%D%> is a dependent scope",
3449 TYPE_CONTEXT (parser->scope),
3450 TYPENAME_TYPE_FULLNAME (parser->scope),
3451 id,
3452 TYPE_CONTEXT (parser->scope),
3453 TYPENAME_TYPE_FULLNAME (parser->scope));
3454 else
3455 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3456 "%qT is a dependent scope",
3457 parser->scope, id, parser->scope);
3458 }
3459 else if (TYPE_P (parser->scope))
3460 {
3461 auto_diagnostic_group d;
3462 if (!COMPLETE_TYPE_P (parser->scope))
3463 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3464 parser->scope);
3465 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3466 error_at (location_of (id),
3467 "%qE in %q#T does not name a template type",
3468 id, parser->scope);
3469 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3470 error_at (location_of (id),
3471 "%qE in %q#T does not name a template type",
3472 TREE_OPERAND (id, 0), parser->scope);
3473 else
3474 error_at (location_of (id),
3475 "%qE in %q#T does not name a type",
3476 id, parser->scope);
3477 if (DECL_P (decl))
3478 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3479 }
3480 else
3481 gcc_unreachable ();
3482 }
3483 }
3484
3485 /* Check for a common situation where a type-name should be present,
3486 but is not, and issue a sensible error message. Returns true if an
3487 invalid type-name was detected.
3488
3489 The situation handled by this function are variable declarations of the
3490 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3491 Usually, `ID' should name a type, but if we got here it means that it
3492 does not. We try to emit the best possible error message depending on
3493 how exactly the id-expression looks like. */
3494
3495 static bool
3496 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3497 {
3498 tree id;
3499 cp_token *token = cp_lexer_peek_token (parser->lexer);
3500
3501 /* Avoid duplicate error about ambiguous lookup. */
3502 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3503 {
3504 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3505 if (next->type == CPP_NAME && next->error_reported)
3506 goto out;
3507 }
3508
3509 cp_parser_parse_tentatively (parser);
3510 id = cp_parser_id_expression (parser,
3511 /*template_keyword_p=*/false,
3512 /*check_dependency_p=*/true,
3513 /*template_p=*/NULL,
3514 /*declarator_p=*/false,
3515 /*optional_p=*/false);
3516 /* If the next token is a (, this is a function with no explicit return
3517 type, i.e. constructor, destructor or conversion op. */
3518 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3519 || TREE_CODE (id) == TYPE_DECL)
3520 {
3521 cp_parser_abort_tentative_parse (parser);
3522 return false;
3523 }
3524 if (!cp_parser_parse_definitely (parser))
3525 return false;
3526
3527 /* Emit a diagnostic for the invalid type. */
3528 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3529 out:
3530 /* If we aren't in the middle of a declarator (i.e. in a
3531 parameter-declaration-clause), skip to the end of the declaration;
3532 there's no point in trying to process it. */
3533 if (!parser->in_declarator_p)
3534 cp_parser_skip_to_end_of_block_or_statement (parser);
3535 return true;
3536 }
3537
3538 /* Consume tokens up to, and including, the next non-nested closing `)'.
3539 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3540 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3541 found an unnested token of that type. */
3542
3543 static int
3544 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3545 bool recovering,
3546 cpp_ttype or_ttype,
3547 bool consume_paren)
3548 {
3549 unsigned paren_depth = 0;
3550 unsigned brace_depth = 0;
3551 unsigned square_depth = 0;
3552 unsigned condop_depth = 0;
3553
3554 if (recovering && or_ttype == CPP_EOF
3555 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3556 return 0;
3557
3558 while (true)
3559 {
3560 cp_token * token = cp_lexer_peek_token (parser->lexer);
3561
3562 /* Have we found what we're looking for before the closing paren? */
3563 if (token->type == or_ttype && or_ttype != CPP_EOF
3564 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3565 return -1;
3566
3567 switch (token->type)
3568 {
3569 case CPP_PRAGMA_EOL:
3570 if (!parser->lexer->in_pragma)
3571 break;
3572 /* FALLTHRU */
3573 case CPP_EOF:
3574 /* If we've run out of tokens, then there is no closing `)'. */
3575 return 0;
3576
3577 /* This is good for lambda expression capture-lists. */
3578 case CPP_OPEN_SQUARE:
3579 ++square_depth;
3580 break;
3581 case CPP_CLOSE_SQUARE:
3582 if (!square_depth--)
3583 return 0;
3584 break;
3585
3586 case CPP_SEMICOLON:
3587 /* This matches the processing in skip_to_end_of_statement. */
3588 if (!brace_depth)
3589 return 0;
3590 break;
3591
3592 case CPP_OPEN_BRACE:
3593 ++brace_depth;
3594 break;
3595 case CPP_CLOSE_BRACE:
3596 if (!brace_depth--)
3597 return 0;
3598 break;
3599
3600 case CPP_OPEN_PAREN:
3601 if (!brace_depth)
3602 ++paren_depth;
3603 break;
3604
3605 case CPP_CLOSE_PAREN:
3606 if (!brace_depth && !paren_depth--)
3607 {
3608 if (consume_paren)
3609 cp_lexer_consume_token (parser->lexer);
3610 return 1;
3611 }
3612 break;
3613
3614 case CPP_QUERY:
3615 if (!brace_depth && !paren_depth && !square_depth)
3616 ++condop_depth;
3617 break;
3618
3619 case CPP_COLON:
3620 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3621 condop_depth--;
3622 break;
3623
3624 default:
3625 break;
3626 }
3627
3628 /* Consume the token. */
3629 cp_lexer_consume_token (parser->lexer);
3630 }
3631 }
3632
3633 /* Consume tokens up to, and including, the next non-nested closing `)'.
3634 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3635 are doing error recovery. Returns -1 if OR_COMMA is true and we
3636 found an unnested token of that type. */
3637
3638 static int
3639 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3640 bool recovering,
3641 bool or_comma,
3642 bool consume_paren)
3643 {
3644 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3645 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3646 ttype, consume_paren);
3647 }
3648
3649 /* Consume tokens until we reach the end of the current statement.
3650 Normally, that will be just before consuming a `;'. However, if a
3651 non-nested `}' comes first, then we stop before consuming that. */
3652
3653 static void
3654 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3655 {
3656 unsigned nesting_depth = 0;
3657
3658 /* Unwind generic function template scope if necessary. */
3659 if (parser->fully_implicit_function_template_p)
3660 abort_fully_implicit_template (parser);
3661
3662 while (true)
3663 {
3664 cp_token *token = cp_lexer_peek_token (parser->lexer);
3665
3666 switch (token->type)
3667 {
3668 case CPP_PRAGMA_EOL:
3669 if (!parser->lexer->in_pragma)
3670 break;
3671 /* FALLTHRU */
3672 case CPP_EOF:
3673 /* If we've run out of tokens, stop. */
3674 return;
3675
3676 case CPP_SEMICOLON:
3677 /* If the next token is a `;', we have reached the end of the
3678 statement. */
3679 if (!nesting_depth)
3680 return;
3681 break;
3682
3683 case CPP_CLOSE_BRACE:
3684 /* If this is a non-nested '}', stop before consuming it.
3685 That way, when confronted with something like:
3686
3687 { 3 + }
3688
3689 we stop before consuming the closing '}', even though we
3690 have not yet reached a `;'. */
3691 if (nesting_depth == 0)
3692 return;
3693
3694 /* If it is the closing '}' for a block that we have
3695 scanned, stop -- but only after consuming the token.
3696 That way given:
3697
3698 void f g () { ... }
3699 typedef int I;
3700
3701 we will stop after the body of the erroneously declared
3702 function, but before consuming the following `typedef'
3703 declaration. */
3704 if (--nesting_depth == 0)
3705 {
3706 cp_lexer_consume_token (parser->lexer);
3707 return;
3708 }
3709 break;
3710
3711 case CPP_OPEN_BRACE:
3712 ++nesting_depth;
3713 break;
3714
3715 default:
3716 break;
3717 }
3718
3719 /* Consume the token. */
3720 cp_lexer_consume_token (parser->lexer);
3721 }
3722 }
3723
3724 /* This function is called at the end of a statement or declaration.
3725 If the next token is a semicolon, it is consumed; otherwise, error
3726 recovery is attempted. */
3727
3728 static void
3729 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3730 {
3731 /* Look for the trailing `;'. */
3732 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3733 {
3734 /* If there is additional (erroneous) input, skip to the end of
3735 the statement. */
3736 cp_parser_skip_to_end_of_statement (parser);
3737 /* If the next token is now a `;', consume it. */
3738 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3739 cp_lexer_consume_token (parser->lexer);
3740 }
3741 }
3742
3743 /* Skip tokens until we have consumed an entire block, or until we
3744 have consumed a non-nested `;'. */
3745
3746 static void
3747 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3748 {
3749 int nesting_depth = 0;
3750
3751 /* Unwind generic function template scope if necessary. */
3752 if (parser->fully_implicit_function_template_p)
3753 abort_fully_implicit_template (parser);
3754
3755 while (nesting_depth >= 0)
3756 {
3757 cp_token *token = cp_lexer_peek_token (parser->lexer);
3758
3759 switch (token->type)
3760 {
3761 case CPP_PRAGMA_EOL:
3762 if (!parser->lexer->in_pragma)
3763 break;
3764 /* FALLTHRU */
3765 case CPP_EOF:
3766 /* If we've run out of tokens, stop. */
3767 return;
3768
3769 case CPP_SEMICOLON:
3770 /* Stop if this is an unnested ';'. */
3771 if (!nesting_depth)
3772 nesting_depth = -1;
3773 break;
3774
3775 case CPP_CLOSE_BRACE:
3776 /* Stop if this is an unnested '}', or closes the outermost
3777 nesting level. */
3778 nesting_depth--;
3779 if (nesting_depth < 0)
3780 return;
3781 if (!nesting_depth)
3782 nesting_depth = -1;
3783 break;
3784
3785 case CPP_OPEN_BRACE:
3786 /* Nest. */
3787 nesting_depth++;
3788 break;
3789
3790 default:
3791 break;
3792 }
3793
3794 /* Consume the token. */
3795 cp_lexer_consume_token (parser->lexer);
3796 }
3797 }
3798
3799 /* Skip tokens until a non-nested closing curly brace is the next
3800 token, or there are no more tokens. Return true in the first case,
3801 false otherwise. */
3802
3803 static bool
3804 cp_parser_skip_to_closing_brace (cp_parser *parser)
3805 {
3806 unsigned nesting_depth = 0;
3807
3808 while (true)
3809 {
3810 cp_token *token = cp_lexer_peek_token (parser->lexer);
3811
3812 switch (token->type)
3813 {
3814 case CPP_PRAGMA_EOL:
3815 if (!parser->lexer->in_pragma)
3816 break;
3817 /* FALLTHRU */
3818 case CPP_EOF:
3819 /* If we've run out of tokens, stop. */
3820 return false;
3821
3822 case CPP_CLOSE_BRACE:
3823 /* If the next token is a non-nested `}', then we have reached
3824 the end of the current block. */
3825 if (nesting_depth-- == 0)
3826 return true;
3827 break;
3828
3829 case CPP_OPEN_BRACE:
3830 /* If it the next token is a `{', then we are entering a new
3831 block. Consume the entire block. */
3832 ++nesting_depth;
3833 break;
3834
3835 default:
3836 break;
3837 }
3838
3839 /* Consume the token. */
3840 cp_lexer_consume_token (parser->lexer);
3841 }
3842 }
3843
3844 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3845 parameter is the PRAGMA token, allowing us to purge the entire pragma
3846 sequence. */
3847
3848 static void
3849 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3850 {
3851 cp_token *token;
3852
3853 parser->lexer->in_pragma = false;
3854
3855 do
3856 token = cp_lexer_consume_token (parser->lexer);
3857 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3858
3859 /* Ensure that the pragma is not parsed again. */
3860 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3861 }
3862
3863 /* Require pragma end of line, resyncing with it as necessary. The
3864 arguments are as for cp_parser_skip_to_pragma_eol. */
3865
3866 static void
3867 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3868 {
3869 parser->lexer->in_pragma = false;
3870 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3871 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3872 }
3873
3874 /* This is a simple wrapper around make_typename_type. When the id is
3875 an unresolved identifier node, we can provide a superior diagnostic
3876 using cp_parser_diagnose_invalid_type_name. */
3877
3878 static tree
3879 cp_parser_make_typename_type (cp_parser *parser, tree id,
3880 location_t id_location)
3881 {
3882 tree result;
3883 if (identifier_p (id))
3884 {
3885 result = make_typename_type (parser->scope, id, typename_type,
3886 /*complain=*/tf_none);
3887 if (result == error_mark_node)
3888 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3889 return result;
3890 }
3891 return make_typename_type (parser->scope, id, typename_type, tf_error);
3892 }
3893
3894 /* This is a wrapper around the
3895 make_{pointer,ptrmem,reference}_declarator functions that decides
3896 which one to call based on the CODE and CLASS_TYPE arguments. The
3897 CODE argument should be one of the values returned by
3898 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3899 appertain to the pointer or reference. */
3900
3901 static cp_declarator *
3902 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3903 cp_cv_quals cv_qualifiers,
3904 cp_declarator *target,
3905 tree attributes)
3906 {
3907 if (code == ERROR_MARK || target == cp_error_declarator)
3908 return cp_error_declarator;
3909
3910 if (code == INDIRECT_REF)
3911 if (class_type == NULL_TREE)
3912 return make_pointer_declarator (cv_qualifiers, target, attributes);
3913 else
3914 return make_ptrmem_declarator (cv_qualifiers, class_type,
3915 target, attributes);
3916 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3917 return make_reference_declarator (cv_qualifiers, target,
3918 false, attributes);
3919 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3920 return make_reference_declarator (cv_qualifiers, target,
3921 true, attributes);
3922 gcc_unreachable ();
3923 }
3924
3925 /* Create a new C++ parser. */
3926
3927 static cp_parser *
3928 cp_parser_new (void)
3929 {
3930 cp_parser *parser;
3931 cp_lexer *lexer;
3932 unsigned i;
3933
3934 /* cp_lexer_new_main is called before doing GC allocation because
3935 cp_lexer_new_main might load a PCH file. */
3936 lexer = cp_lexer_new_main ();
3937
3938 /* Initialize the binops_by_token so that we can get the tree
3939 directly from the token. */
3940 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3941 binops_by_token[binops[i].token_type] = binops[i];
3942
3943 parser = ggc_cleared_alloc<cp_parser> ();
3944 parser->lexer = lexer;
3945 parser->context = cp_parser_context_new (NULL);
3946
3947 /* For now, we always accept GNU extensions. */
3948 parser->allow_gnu_extensions_p = 1;
3949
3950 /* The `>' token is a greater-than operator, not the end of a
3951 template-id. */
3952 parser->greater_than_is_operator_p = true;
3953
3954 parser->default_arg_ok_p = true;
3955
3956 /* We are not parsing a constant-expression. */
3957 parser->integral_constant_expression_p = false;
3958 parser->allow_non_integral_constant_expression_p = false;
3959 parser->non_integral_constant_expression_p = false;
3960
3961 /* Local variable names are not forbidden. */
3962 parser->local_variables_forbidden_p = 0;
3963
3964 /* We are not processing an `extern "C"' declaration. */
3965 parser->in_unbraced_linkage_specification_p = false;
3966
3967 /* We are not processing a declarator. */
3968 parser->in_declarator_p = false;
3969
3970 /* We are not processing a template-argument-list. */
3971 parser->in_template_argument_list_p = false;
3972
3973 /* We are not in an iteration statement. */
3974 parser->in_statement = 0;
3975
3976 /* We are not in a switch statement. */
3977 parser->in_switch_statement_p = false;
3978
3979 /* We are not parsing a type-id inside an expression. */
3980 parser->in_type_id_in_expr_p = false;
3981
3982 /* String literals should be translated to the execution character set. */
3983 parser->translate_strings_p = true;
3984
3985 /* We are not parsing a function body. */
3986 parser->in_function_body = false;
3987
3988 /* We can correct until told otherwise. */
3989 parser->colon_corrects_to_scope_p = true;
3990
3991 /* The unparsed function queue is empty. */
3992 push_unparsed_function_queues (parser);
3993
3994 /* There are no classes being defined. */
3995 parser->num_classes_being_defined = 0;
3996
3997 /* No template parameters apply. */
3998 parser->num_template_parameter_lists = 0;
3999
4000 /* Special parsing data structures. */
4001 parser->omp_declare_simd = NULL;
4002 parser->oacc_routine = NULL;
4003
4004 /* Not declaring an implicit function template. */
4005 parser->auto_is_implicit_function_template_parm_p = false;
4006 parser->fully_implicit_function_template_p = false;
4007 parser->implicit_template_parms = 0;
4008 parser->implicit_template_scope = 0;
4009
4010 /* Allow constrained-type-specifiers. */
4011 parser->prevent_constrained_type_specifiers = 0;
4012
4013 /* We haven't yet seen an 'extern "C"'. */
4014 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4015
4016 return parser;
4017 }
4018
4019 /* Create a cp_lexer structure which will emit the tokens in CACHE
4020 and push it onto the parser's lexer stack. This is used for delayed
4021 parsing of in-class method bodies and default arguments, and should
4022 not be confused with tentative parsing. */
4023 static void
4024 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4025 {
4026 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4027 lexer->next = parser->lexer;
4028 parser->lexer = lexer;
4029
4030 /* Move the current source position to that of the first token in the
4031 new lexer. */
4032 cp_lexer_set_source_position_from_token (lexer->next_token);
4033 }
4034
4035 /* Pop the top lexer off the parser stack. This is never used for the
4036 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4037 static void
4038 cp_parser_pop_lexer (cp_parser *parser)
4039 {
4040 cp_lexer *lexer = parser->lexer;
4041 parser->lexer = lexer->next;
4042 cp_lexer_destroy (lexer);
4043
4044 /* Put the current source position back where it was before this
4045 lexer was pushed. */
4046 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4047 }
4048
4049 /* Lexical conventions [gram.lex] */
4050
4051 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4052 identifier. */
4053
4054 static cp_expr
4055 cp_parser_identifier (cp_parser* parser)
4056 {
4057 cp_token *token;
4058
4059 /* Look for the identifier. */
4060 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4061 /* Return the value. */
4062 if (token)
4063 return cp_expr (token->u.value, token->location);
4064 else
4065 return error_mark_node;
4066 }
4067
4068 /* Parse a sequence of adjacent string constants. Returns a
4069 TREE_STRING representing the combined, nul-terminated string
4070 constant. If TRANSLATE is true, translate the string to the
4071 execution character set. If WIDE_OK is true, a wide string is
4072 invalid here.
4073
4074 C++98 [lex.string] says that if a narrow string literal token is
4075 adjacent to a wide string literal token, the behavior is undefined.
4076 However, C99 6.4.5p4 says that this results in a wide string literal.
4077 We follow C99 here, for consistency with the C front end.
4078
4079 This code is largely lifted from lex_string() in c-lex.c.
4080
4081 FUTURE: ObjC++ will need to handle @-strings here. */
4082 static cp_expr
4083 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4084 bool lookup_udlit = true)
4085 {
4086 tree value;
4087 size_t count;
4088 struct obstack str_ob;
4089 struct obstack loc_ob;
4090 cpp_string str, istr, *strs;
4091 cp_token *tok;
4092 enum cpp_ttype type, curr_type;
4093 int have_suffix_p = 0;
4094 tree string_tree;
4095 tree suffix_id = NULL_TREE;
4096 bool curr_tok_is_userdef_p = false;
4097
4098 tok = cp_lexer_peek_token (parser->lexer);
4099 if (!cp_parser_is_string_literal (tok))
4100 {
4101 cp_parser_error (parser, "expected string-literal");
4102 return error_mark_node;
4103 }
4104
4105 location_t loc = tok->location;
4106
4107 if (cpp_userdef_string_p (tok->type))
4108 {
4109 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4110 curr_type = cpp_userdef_string_remove_type (tok->type);
4111 curr_tok_is_userdef_p = true;
4112 }
4113 else
4114 {
4115 string_tree = tok->u.value;
4116 curr_type = tok->type;
4117 }
4118 type = curr_type;
4119
4120 /* Try to avoid the overhead of creating and destroying an obstack
4121 for the common case of just one string. */
4122 if (!cp_parser_is_string_literal
4123 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4124 {
4125 cp_lexer_consume_token (parser->lexer);
4126
4127 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4128 str.len = TREE_STRING_LENGTH (string_tree);
4129 count = 1;
4130
4131 if (curr_tok_is_userdef_p)
4132 {
4133 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4134 have_suffix_p = 1;
4135 curr_type = cpp_userdef_string_remove_type (tok->type);
4136 }
4137 else
4138 curr_type = tok->type;
4139
4140 strs = &str;
4141 }
4142 else
4143 {
4144 location_t last_tok_loc = tok->location;
4145 gcc_obstack_init (&str_ob);
4146 gcc_obstack_init (&loc_ob);
4147 count = 0;
4148
4149 do
4150 {
4151 cp_lexer_consume_token (parser->lexer);
4152 count++;
4153 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4154 str.len = TREE_STRING_LENGTH (string_tree);
4155
4156 if (curr_tok_is_userdef_p)
4157 {
4158 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4159 if (have_suffix_p == 0)
4160 {
4161 suffix_id = curr_suffix_id;
4162 have_suffix_p = 1;
4163 }
4164 else if (have_suffix_p == 1
4165 && curr_suffix_id != suffix_id)
4166 {
4167 error ("inconsistent user-defined literal suffixes"
4168 " %qD and %qD in string literal",
4169 suffix_id, curr_suffix_id);
4170 have_suffix_p = -1;
4171 }
4172 curr_type = cpp_userdef_string_remove_type (tok->type);
4173 }
4174 else
4175 curr_type = tok->type;
4176
4177 if (type != curr_type)
4178 {
4179 if (type == CPP_STRING)
4180 type = curr_type;
4181 else if (curr_type != CPP_STRING)
4182 {
4183 rich_location rich_loc (line_table, tok->location);
4184 rich_loc.add_range (last_tok_loc);
4185 error_at (&rich_loc,
4186 "unsupported non-standard concatenation "
4187 "of string literals");
4188 }
4189 }
4190
4191 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4192 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4193
4194 last_tok_loc = tok->location;
4195
4196 tok = cp_lexer_peek_token (parser->lexer);
4197 if (cpp_userdef_string_p (tok->type))
4198 {
4199 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4200 curr_type = cpp_userdef_string_remove_type (tok->type);
4201 curr_tok_is_userdef_p = true;
4202 }
4203 else
4204 {
4205 string_tree = tok->u.value;
4206 curr_type = tok->type;
4207 curr_tok_is_userdef_p = false;
4208 }
4209 }
4210 while (cp_parser_is_string_literal (tok));
4211
4212 /* A string literal built by concatenation has its caret=start at
4213 the start of the initial string, and its finish at the finish of
4214 the final string literal. */
4215 loc = make_location (loc, loc, get_finish (last_tok_loc));
4216
4217 strs = (cpp_string *) obstack_finish (&str_ob);
4218 }
4219
4220 if (type != CPP_STRING && !wide_ok)
4221 {
4222 cp_parser_error (parser, "a wide string is invalid in this context");
4223 type = CPP_STRING;
4224 }
4225
4226 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4227 (parse_in, strs, count, &istr, type))
4228 {
4229 value = build_string (istr.len, (const char *)istr.text);
4230 free (CONST_CAST (unsigned char *, istr.text));
4231 if (count > 1)
4232 {
4233 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4234 gcc_assert (g_string_concat_db);
4235 g_string_concat_db->record_string_concatenation (count, locs);
4236 }
4237
4238 switch (type)
4239 {
4240 default:
4241 case CPP_STRING:
4242 TREE_TYPE (value) = char_array_type_node;
4243 break;
4244 case CPP_UTF8STRING:
4245 if (flag_char8_t)
4246 TREE_TYPE (value) = char8_array_type_node;
4247 else
4248 TREE_TYPE (value) = char_array_type_node;
4249 break;
4250 case CPP_STRING16:
4251 TREE_TYPE (value) = char16_array_type_node;
4252 break;
4253 case CPP_STRING32:
4254 TREE_TYPE (value) = char32_array_type_node;
4255 break;
4256 case CPP_WSTRING:
4257 TREE_TYPE (value) = wchar_array_type_node;
4258 break;
4259 }
4260
4261 value = fix_string_type (value);
4262
4263 if (have_suffix_p)
4264 {
4265 tree literal = build_userdef_literal (suffix_id, value,
4266 OT_NONE, NULL_TREE);
4267 if (lookup_udlit)
4268 value = cp_parser_userdef_string_literal (literal);
4269 else
4270 value = literal;
4271 }
4272 }
4273 else
4274 /* cpp_interpret_string has issued an error. */
4275 value = error_mark_node;
4276
4277 if (count > 1)
4278 {
4279 obstack_free (&str_ob, 0);
4280 obstack_free (&loc_ob, 0);
4281 }
4282
4283 return cp_expr (value, loc);
4284 }
4285
4286 /* Look up a literal operator with the name and the exact arguments. */
4287
4288 static tree
4289 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4290 {
4291 tree decl = lookup_name (name);
4292 if (!decl || !is_overloaded_fn (decl))
4293 return error_mark_node;
4294
4295 for (lkp_iterator iter (decl); iter; ++iter)
4296 {
4297 tree fn = *iter;
4298
4299 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4300 {
4301 unsigned int ix;
4302 bool found = true;
4303
4304 for (ix = 0;
4305 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4306 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4307 {
4308 tree tparm = TREE_VALUE (parmtypes);
4309 tree targ = TREE_TYPE ((*args)[ix]);
4310 bool ptr = TYPE_PTR_P (tparm);
4311 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4312 if ((ptr || arr || !same_type_p (tparm, targ))
4313 && (!ptr || !arr
4314 || !same_type_p (TREE_TYPE (tparm),
4315 TREE_TYPE (targ))))
4316 found = false;
4317 }
4318
4319 if (found
4320 && ix == vec_safe_length (args)
4321 /* May be this should be sufficient_parms_p instead,
4322 depending on how exactly should user-defined literals
4323 work in presence of default arguments on the literal
4324 operator parameters. */
4325 && parmtypes == void_list_node)
4326 return decl;
4327 }
4328 }
4329
4330 return error_mark_node;
4331 }
4332
4333 /* Parse a user-defined char constant. Returns a call to a user-defined
4334 literal operator taking the character as an argument. */
4335
4336 static cp_expr
4337 cp_parser_userdef_char_literal (cp_parser *parser)
4338 {
4339 cp_token *token = cp_lexer_consume_token (parser->lexer);
4340 tree literal = token->u.value;
4341 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4342 tree value = USERDEF_LITERAL_VALUE (literal);
4343 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4344 tree decl, result;
4345
4346 /* Build up a call to the user-defined operator */
4347 /* Lookup the name we got back from the id-expression. */
4348 vec<tree, va_gc> *args = make_tree_vector ();
4349 vec_safe_push (args, value);
4350 decl = lookup_literal_operator (name, args);
4351 if (!decl || decl == error_mark_node)
4352 {
4353 error ("unable to find character literal operator %qD with %qT argument",
4354 name, TREE_TYPE (value));
4355 release_tree_vector (args);
4356 return error_mark_node;
4357 }
4358 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4359 release_tree_vector (args);
4360 return result;
4361 }
4362
4363 /* A subroutine of cp_parser_userdef_numeric_literal to
4364 create a char... template parameter pack from a string node. */
4365
4366 static tree
4367 make_char_string_pack (tree value)
4368 {
4369 tree charvec;
4370 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4371 const char *str = TREE_STRING_POINTER (value);
4372 int i, len = TREE_STRING_LENGTH (value) - 1;
4373 tree argvec = make_tree_vec (1);
4374
4375 /* Fill in CHARVEC with all of the parameters. */
4376 charvec = make_tree_vec (len);
4377 for (i = 0; i < len; ++i)
4378 {
4379 unsigned char s[3] = { '\'', str[i], '\'' };
4380 cpp_string in = { 3, s };
4381 cpp_string out = { 0, 0 };
4382 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4383 return NULL_TREE;
4384 gcc_assert (out.len == 2);
4385 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4386 out.text[0]);
4387 }
4388
4389 /* Build the argument packs. */
4390 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4391
4392 TREE_VEC_ELT (argvec, 0) = argpack;
4393
4394 return argvec;
4395 }
4396
4397 /* A subroutine of cp_parser_userdef_numeric_literal to
4398 create a char... template parameter pack from a string node. */
4399
4400 static tree
4401 make_string_pack (tree value)
4402 {
4403 tree charvec;
4404 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4405 const unsigned char *str
4406 = (const unsigned char *) TREE_STRING_POINTER (value);
4407 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4408 int len = TREE_STRING_LENGTH (value) / sz - 1;
4409 tree argvec = make_tree_vec (2);
4410
4411 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4412 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4413
4414 /* First template parm is character type. */
4415 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4416
4417 /* Fill in CHARVEC with all of the parameters. */
4418 charvec = make_tree_vec (len);
4419 for (int i = 0; i < len; ++i)
4420 TREE_VEC_ELT (charvec, i)
4421 = double_int_to_tree (str_char_type_node,
4422 double_int::from_buffer (str + i * sz, sz));
4423
4424 /* Build the argument packs. */
4425 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4426
4427 TREE_VEC_ELT (argvec, 1) = argpack;
4428
4429 return argvec;
4430 }
4431
4432 /* Parse a user-defined numeric constant. returns a call to a user-defined
4433 literal operator. */
4434
4435 static cp_expr
4436 cp_parser_userdef_numeric_literal (cp_parser *parser)
4437 {
4438 cp_token *token = cp_lexer_consume_token (parser->lexer);
4439 tree literal = token->u.value;
4440 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4441 tree value = USERDEF_LITERAL_VALUE (literal);
4442 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4443 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4444 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4445 tree decl, result;
4446 vec<tree, va_gc> *args;
4447
4448 /* Look for a literal operator taking the exact type of numeric argument
4449 as the literal value. */
4450 args = make_tree_vector ();
4451 vec_safe_push (args, value);
4452 decl = lookup_literal_operator (name, args);
4453 if (decl && decl != error_mark_node)
4454 {
4455 result = finish_call_expr (decl, &args, false, true,
4456 tf_warning_or_error);
4457
4458 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4459 {
4460 warning_at (token->location, OPT_Woverflow,
4461 "integer literal exceeds range of %qT type",
4462 long_long_unsigned_type_node);
4463 }
4464 else
4465 {
4466 if (overflow > 0)
4467 warning_at (token->location, OPT_Woverflow,
4468 "floating literal exceeds range of %qT type",
4469 long_double_type_node);
4470 else if (overflow < 0)
4471 warning_at (token->location, OPT_Woverflow,
4472 "floating literal truncated to zero");
4473 }
4474
4475 release_tree_vector (args);
4476 return result;
4477 }
4478 release_tree_vector (args);
4479
4480 /* If the numeric argument didn't work, look for a raw literal
4481 operator taking a const char* argument consisting of the number
4482 in string format. */
4483 args = make_tree_vector ();
4484 vec_safe_push (args, num_string);
4485 decl = lookup_literal_operator (name, args);
4486 if (decl && decl != error_mark_node)
4487 {
4488 result = finish_call_expr (decl, &args, false, true,
4489 tf_warning_or_error);
4490 release_tree_vector (args);
4491 return result;
4492 }
4493 release_tree_vector (args);
4494
4495 /* If the raw literal didn't work, look for a non-type template
4496 function with parameter pack char.... Call the function with
4497 template parameter characters representing the number. */
4498 args = make_tree_vector ();
4499 decl = lookup_literal_operator (name, args);
4500 if (decl && decl != error_mark_node)
4501 {
4502 tree tmpl_args = make_char_string_pack (num_string);
4503 if (tmpl_args == NULL_TREE)
4504 {
4505 error ("failed to translate literal to execution character set %qT",
4506 num_string);
4507 return error_mark_node;
4508 }
4509 decl = lookup_template_function (decl, tmpl_args);
4510 result = finish_call_expr (decl, &args, false, true,
4511 tf_warning_or_error);
4512 release_tree_vector (args);
4513 return result;
4514 }
4515
4516 release_tree_vector (args);
4517
4518 /* In C++14 the standard library defines complex number suffixes that
4519 conflict with GNU extensions. Prefer them if <complex> is #included. */
4520 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4521 bool i14 = (cxx_dialect > cxx11
4522 && (id_equal (suffix_id, "i")
4523 || id_equal (suffix_id, "if")
4524 || id_equal (suffix_id, "il")));
4525 diagnostic_t kind = DK_ERROR;
4526 int opt = 0;
4527
4528 if (i14 && ext)
4529 {
4530 tree cxlit = lookup_qualified_name (std_node,
4531 get_identifier ("complex_literals"),
4532 0, false, false);
4533 if (cxlit == error_mark_node)
4534 {
4535 /* No <complex>, so pedwarn and use GNU semantics. */
4536 kind = DK_PEDWARN;
4537 opt = OPT_Wpedantic;
4538 }
4539 }
4540
4541 bool complained
4542 = emit_diagnostic (kind, input_location, opt,
4543 "unable to find numeric literal operator %qD", name);
4544
4545 if (!complained)
4546 /* Don't inform either. */;
4547 else if (i14)
4548 {
4549 inform (token->location, "add %<using namespace std::complex_literals%> "
4550 "(from <complex>) to enable the C++14 user-defined literal "
4551 "suffixes");
4552 if (ext)
4553 inform (token->location, "or use %<j%> instead of %<i%> for the "
4554 "GNU built-in suffix");
4555 }
4556 else if (!ext)
4557 inform (token->location, "use %<-fext-numeric-literals%> "
4558 "to enable more built-in suffixes");
4559
4560 if (kind == DK_ERROR)
4561 value = error_mark_node;
4562 else
4563 {
4564 /* Use the built-in semantics. */
4565 tree type;
4566 if (id_equal (suffix_id, "i"))
4567 {
4568 if (TREE_CODE (value) == INTEGER_CST)
4569 type = integer_type_node;
4570 else
4571 type = double_type_node;
4572 }
4573 else if (id_equal (suffix_id, "if"))
4574 type = float_type_node;
4575 else /* if (id_equal (suffix_id, "il")) */
4576 type = long_double_type_node;
4577
4578 value = build_complex (build_complex_type (type),
4579 fold_convert (type, integer_zero_node),
4580 fold_convert (type, value));
4581 }
4582
4583 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4584 /* Avoid repeated diagnostics. */
4585 token->u.value = value;
4586 return value;
4587 }
4588
4589 /* Parse a user-defined string constant. Returns a call to a user-defined
4590 literal operator taking a character pointer and the length of the string
4591 as arguments. */
4592
4593 static tree
4594 cp_parser_userdef_string_literal (tree literal)
4595 {
4596 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4597 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4598 tree value = USERDEF_LITERAL_VALUE (literal);
4599 int len = TREE_STRING_LENGTH (value)
4600 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4601 tree decl;
4602
4603 /* Build up a call to the user-defined operator. */
4604 /* Lookup the name we got back from the id-expression. */
4605 releasing_vec rargs;
4606 vec<tree, va_gc> *&args = rargs.get_ref();
4607 vec_safe_push (args, value);
4608 vec_safe_push (args, build_int_cst (size_type_node, len));
4609 decl = lookup_literal_operator (name, args);
4610
4611 if (decl && decl != error_mark_node)
4612 return finish_call_expr (decl, &args, false, true,
4613 tf_warning_or_error);
4614
4615 /* Look for a suitable template function, either (C++20) with a single
4616 parameter of class type, or (N3599) with typename parameter CharT and
4617 parameter pack CharT... */
4618 args->truncate (0);
4619 decl = lookup_literal_operator (name, args);
4620 if (decl && decl != error_mark_node)
4621 {
4622 /* Use resolve_nondeduced_context to try to choose one form of template
4623 or the other. */
4624 tree tmpl_args = make_tree_vec (1);
4625 TREE_VEC_ELT (tmpl_args, 0) = value;
4626 decl = lookup_template_function (decl, tmpl_args);
4627 tree res = resolve_nondeduced_context (decl, tf_none);
4628 if (DECL_P (res))
4629 decl = res;
4630 else
4631 {
4632 TREE_OPERAND (decl, 1) = make_string_pack (value);
4633 res = resolve_nondeduced_context (decl, tf_none);
4634 if (DECL_P (res))
4635 decl = res;
4636 }
4637 if (!DECL_P (decl) && cxx_dialect > cxx17)
4638 TREE_OPERAND (decl, 1) = tmpl_args;
4639 return finish_call_expr (decl, &args, false, true,
4640 tf_warning_or_error);
4641 }
4642
4643 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4644 name, TREE_TYPE (value), size_type_node);
4645 return error_mark_node;
4646 }
4647
4648
4649 /* Basic concepts [gram.basic] */
4650
4651 /* Parse a translation-unit.
4652
4653 translation-unit:
4654 declaration-seq [opt] */
4655
4656 static void
4657 cp_parser_translation_unit (cp_parser* parser)
4658 {
4659 gcc_checking_assert (!cp_error_declarator);
4660
4661 /* Create the declarator obstack. */
4662 gcc_obstack_init (&declarator_obstack);
4663 /* Create the error declarator. */
4664 cp_error_declarator = make_declarator (cdk_error);
4665 /* Create the empty parameter list. */
4666 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4667 UNKNOWN_LOCATION);
4668 /* Remember where the base of the declarator obstack lies. */
4669 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4670
4671 bool implicit_extern_c = false;
4672
4673 for (;;)
4674 {
4675 cp_token *token = cp_lexer_peek_token (parser->lexer);
4676
4677 /* If we're entering or exiting a region that's implicitly
4678 extern "C", modify the lang context appropriately. */
4679 if (implicit_extern_c
4680 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4681 {
4682 implicit_extern_c = !implicit_extern_c;
4683 if (implicit_extern_c)
4684 push_lang_context (lang_name_c);
4685 else
4686 pop_lang_context ();
4687 }
4688
4689 if (token->type == CPP_EOF)
4690 break;
4691
4692 if (token->type == CPP_CLOSE_BRACE)
4693 {
4694 cp_parser_error (parser, "expected declaration");
4695 cp_lexer_consume_token (parser->lexer);
4696 /* If the next token is now a `;', consume it. */
4697 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4698 cp_lexer_consume_token (parser->lexer);
4699 }
4700 else
4701 cp_parser_toplevel_declaration (parser);
4702 }
4703
4704 /* Get rid of the token array; we don't need it any more. */
4705 cp_lexer_destroy (parser->lexer);
4706 parser->lexer = NULL;
4707
4708 /* The EOF should have reset this. */
4709 gcc_checking_assert (!implicit_extern_c);
4710
4711 /* Make sure the declarator obstack was fully cleaned up. */
4712 gcc_assert (obstack_next_free (&declarator_obstack)
4713 == declarator_obstack_base);
4714 }
4715
4716 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4717 decltype context. */
4718
4719 static inline tsubst_flags_t
4720 complain_flags (bool decltype_p)
4721 {
4722 tsubst_flags_t complain = tf_warning_or_error;
4723 if (decltype_p)
4724 complain |= tf_decltype;
4725 return complain;
4726 }
4727
4728 /* We're about to parse a collection of statements. If we're currently
4729 parsing tentatively, set up a firewall so that any nested
4730 cp_parser_commit_to_tentative_parse won't affect the current context. */
4731
4732 static cp_token_position
4733 cp_parser_start_tentative_firewall (cp_parser *parser)
4734 {
4735 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4736 return 0;
4737
4738 cp_parser_parse_tentatively (parser);
4739 cp_parser_commit_to_topmost_tentative_parse (parser);
4740 return cp_lexer_token_position (parser->lexer, false);
4741 }
4742
4743 /* We've finished parsing the collection of statements. Wrap up the
4744 firewall and replace the relevant tokens with the parsed form. */
4745
4746 static void
4747 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4748 tree expr)
4749 {
4750 if (!start)
4751 return;
4752
4753 /* Finish the firewall level. */
4754 cp_parser_parse_definitely (parser);
4755 /* And remember the result of the parse for when we try again. */
4756 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4757 token->type = CPP_PREPARSED_EXPR;
4758 token->u.value = expr;
4759 token->keyword = RID_MAX;
4760 cp_lexer_purge_tokens_after (parser->lexer, start);
4761 }
4762
4763 /* Like the above functions, but let the user modify the tokens. Used by
4764 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4765 later parses, so it makes sense to localize the effects of
4766 cp_parser_commit_to_tentative_parse. */
4767
4768 struct tentative_firewall
4769 {
4770 cp_parser *parser;
4771 bool set;
4772
4773 tentative_firewall (cp_parser *p): parser(p)
4774 {
4775 /* If we're currently parsing tentatively, start a committed level as a
4776 firewall and then an inner tentative parse. */
4777 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4778 {
4779 cp_parser_parse_tentatively (parser);
4780 cp_parser_commit_to_topmost_tentative_parse (parser);
4781 cp_parser_parse_tentatively (parser);
4782 }
4783 }
4784
4785 ~tentative_firewall()
4786 {
4787 if (set)
4788 {
4789 /* Finish the inner tentative parse and the firewall, propagating any
4790 uncommitted error state to the outer tentative parse. */
4791 bool err = cp_parser_error_occurred (parser);
4792 cp_parser_parse_definitely (parser);
4793 cp_parser_parse_definitely (parser);
4794 if (err)
4795 cp_parser_simulate_error (parser);
4796 }
4797 }
4798 };
4799
4800 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4801 This class is for tracking such a matching pair of symbols.
4802 In particular, it tracks the location of the first token,
4803 so that if the second token is missing, we can highlight the
4804 location of the first token when notifying the user about the
4805 problem. */
4806
4807 template <typename traits_t>
4808 class token_pair
4809 {
4810 public:
4811 /* token_pair's ctor. */
4812 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4813
4814 /* If the next token is the opening symbol for this pair, consume it and
4815 return true.
4816 Otherwise, issue an error and return false.
4817 In either case, record the location of the opening token. */
4818
4819 bool require_open (cp_parser *parser)
4820 {
4821 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4822 return cp_parser_require (parser, traits_t::open_token_type,
4823 traits_t::required_token_open);
4824 }
4825
4826 /* Consume the next token from PARSER, recording its location as
4827 that of the opening token within the pair. */
4828
4829 cp_token * consume_open (cp_parser *parser)
4830 {
4831 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4832 gcc_assert (tok->type == traits_t::open_token_type);
4833 m_open_loc = tok->location;
4834 return tok;
4835 }
4836
4837 /* If the next token is the closing symbol for this pair, consume it
4838 and return it.
4839 Otherwise, issue an error, highlighting the location of the
4840 corresponding opening token, and return NULL. */
4841
4842 cp_token *require_close (cp_parser *parser) const
4843 {
4844 return cp_parser_require (parser, traits_t::close_token_type,
4845 traits_t::required_token_close,
4846 m_open_loc);
4847 }
4848
4849 private:
4850 location_t m_open_loc;
4851 };
4852
4853 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4854
4855 struct matching_paren_traits
4856 {
4857 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4858 static const enum required_token required_token_open = RT_OPEN_PAREN;
4859 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4860 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4861 };
4862
4863 /* "matching_parens" is a token_pair<T> class for tracking matching
4864 pairs of parentheses. */
4865
4866 typedef token_pair<matching_paren_traits> matching_parens;
4867
4868 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4869
4870 struct matching_brace_traits
4871 {
4872 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4873 static const enum required_token required_token_open = RT_OPEN_BRACE;
4874 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4875 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4876 };
4877
4878 /* "matching_braces" is a token_pair<T> class for tracking matching
4879 pairs of braces. */
4880
4881 typedef token_pair<matching_brace_traits> matching_braces;
4882
4883
4884 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4885 enclosing parentheses. */
4886
4887 static cp_expr
4888 cp_parser_statement_expr (cp_parser *parser)
4889 {
4890 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4891
4892 /* Consume the '('. */
4893 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4894 matching_parens parens;
4895 parens.consume_open (parser);
4896 /* Start the statement-expression. */
4897 tree expr = begin_stmt_expr ();
4898 /* Parse the compound-statement. */
4899 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4900 /* Finish up. */
4901 expr = finish_stmt_expr (expr, false);
4902 /* Consume the ')'. */
4903 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4904 if (!parens.require_close (parser))
4905 cp_parser_skip_to_end_of_statement (parser);
4906
4907 cp_parser_end_tentative_firewall (parser, start, expr);
4908 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4909 return cp_expr (expr, combined_loc);
4910 }
4911
4912 /* Expressions [gram.expr] */
4913
4914 /* Parse a fold-operator.
4915
4916 fold-operator:
4917 - * / % ^ & | = < > << >>
4918 = -= *= /= %= ^= &= |= <<= >>=
4919 == != <= >= && || , .* ->*
4920
4921 This returns the tree code corresponding to the matched operator
4922 as an int. When the current token matches a compound assignment
4923 opertor, the resulting tree code is the negative value of the
4924 non-assignment operator. */
4925
4926 static int
4927 cp_parser_fold_operator (cp_token *token)
4928 {
4929 switch (token->type)
4930 {
4931 case CPP_PLUS: return PLUS_EXPR;
4932 case CPP_MINUS: return MINUS_EXPR;
4933 case CPP_MULT: return MULT_EXPR;
4934 case CPP_DIV: return TRUNC_DIV_EXPR;
4935 case CPP_MOD: return TRUNC_MOD_EXPR;
4936 case CPP_XOR: return BIT_XOR_EXPR;
4937 case CPP_AND: return BIT_AND_EXPR;
4938 case CPP_OR: return BIT_IOR_EXPR;
4939 case CPP_LSHIFT: return LSHIFT_EXPR;
4940 case CPP_RSHIFT: return RSHIFT_EXPR;
4941
4942 case CPP_EQ: return -NOP_EXPR;
4943 case CPP_PLUS_EQ: return -PLUS_EXPR;
4944 case CPP_MINUS_EQ: return -MINUS_EXPR;
4945 case CPP_MULT_EQ: return -MULT_EXPR;
4946 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4947 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4948 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4949 case CPP_AND_EQ: return -BIT_AND_EXPR;
4950 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4951 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4952 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4953
4954 case CPP_EQ_EQ: return EQ_EXPR;
4955 case CPP_NOT_EQ: return NE_EXPR;
4956 case CPP_LESS: return LT_EXPR;
4957 case CPP_GREATER: return GT_EXPR;
4958 case CPP_LESS_EQ: return LE_EXPR;
4959 case CPP_GREATER_EQ: return GE_EXPR;
4960
4961 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4962 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4963
4964 case CPP_COMMA: return COMPOUND_EXPR;
4965
4966 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4967 case CPP_DEREF_STAR: return MEMBER_REF;
4968
4969 default: return ERROR_MARK;
4970 }
4971 }
4972
4973 /* Returns true if CODE indicates a binary expression, which is not allowed in
4974 the LHS of a fold-expression. More codes will need to be added to use this
4975 function in other contexts. */
4976
4977 static bool
4978 is_binary_op (tree_code code)
4979 {
4980 switch (code)
4981 {
4982 case PLUS_EXPR:
4983 case POINTER_PLUS_EXPR:
4984 case MINUS_EXPR:
4985 case MULT_EXPR:
4986 case TRUNC_DIV_EXPR:
4987 case TRUNC_MOD_EXPR:
4988 case BIT_XOR_EXPR:
4989 case BIT_AND_EXPR:
4990 case BIT_IOR_EXPR:
4991 case LSHIFT_EXPR:
4992 case RSHIFT_EXPR:
4993
4994 case MODOP_EXPR:
4995
4996 case EQ_EXPR:
4997 case NE_EXPR:
4998 case LE_EXPR:
4999 case GE_EXPR:
5000 case LT_EXPR:
5001 case GT_EXPR:
5002
5003 case TRUTH_ANDIF_EXPR:
5004 case TRUTH_ORIF_EXPR:
5005
5006 case COMPOUND_EXPR:
5007
5008 case DOTSTAR_EXPR:
5009 case MEMBER_REF:
5010 return true;
5011
5012 default:
5013 return false;
5014 }
5015 }
5016
5017 /* If the next token is a suitable fold operator, consume it and return as
5018 the function above. */
5019
5020 static int
5021 cp_parser_fold_operator (cp_parser *parser)
5022 {
5023 cp_token* token = cp_lexer_peek_token (parser->lexer);
5024 int code = cp_parser_fold_operator (token);
5025 if (code != ERROR_MARK)
5026 cp_lexer_consume_token (parser->lexer);
5027 return code;
5028 }
5029
5030 /* Parse a fold-expression.
5031
5032 fold-expression:
5033 ( ... folding-operator cast-expression)
5034 ( cast-expression folding-operator ... )
5035 ( cast-expression folding operator ... folding-operator cast-expression)
5036
5037 Note that the '(' and ')' are matched in primary expression. */
5038
5039 static cp_expr
5040 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5041 {
5042 cp_id_kind pidk;
5043
5044 // Left fold.
5045 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5046 {
5047 cp_lexer_consume_token (parser->lexer);
5048 int op = cp_parser_fold_operator (parser);
5049 if (op == ERROR_MARK)
5050 {
5051 cp_parser_error (parser, "expected binary operator");
5052 return error_mark_node;
5053 }
5054
5055 tree expr = cp_parser_cast_expression (parser, false, false,
5056 false, &pidk);
5057 if (expr == error_mark_node)
5058 return error_mark_node;
5059 return finish_left_unary_fold_expr (expr, op);
5060 }
5061
5062 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5063 int op = cp_parser_fold_operator (parser);
5064 if (op == ERROR_MARK)
5065 {
5066 cp_parser_error (parser, "expected binary operator");
5067 return error_mark_node;
5068 }
5069
5070 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5071 {
5072 cp_parser_error (parser, "expected ...");
5073 return error_mark_node;
5074 }
5075 cp_lexer_consume_token (parser->lexer);
5076
5077 /* The operands of a fold-expression are cast-expressions, so binary or
5078 conditional expressions are not allowed. We check this here to avoid
5079 tentative parsing. */
5080 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5081 /* OK, the expression was parenthesized. */;
5082 else if (is_binary_op (TREE_CODE (expr1)))
5083 error_at (location_of (expr1),
5084 "binary expression in operand of fold-expression");
5085 else if (TREE_CODE (expr1) == COND_EXPR
5086 || (REFERENCE_REF_P (expr1)
5087 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5088 error_at (location_of (expr1),
5089 "conditional expression in operand of fold-expression");
5090
5091 // Right fold.
5092 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5093 return finish_right_unary_fold_expr (expr1, op);
5094
5095 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5096 {
5097 cp_parser_error (parser, "mismatched operator in fold-expression");
5098 return error_mark_node;
5099 }
5100 cp_lexer_consume_token (parser->lexer);
5101
5102 // Binary left or right fold.
5103 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5104 if (expr2 == error_mark_node)
5105 return error_mark_node;
5106 return finish_binary_fold_expr (expr1, expr2, op);
5107 }
5108
5109 /* Parse a primary-expression.
5110
5111 primary-expression:
5112 literal
5113 this
5114 ( expression )
5115 id-expression
5116 lambda-expression (C++11)
5117
5118 GNU Extensions:
5119
5120 primary-expression:
5121 ( compound-statement )
5122 __builtin_va_arg ( assignment-expression , type-id )
5123 __builtin_offsetof ( type-id , offsetof-expression )
5124
5125 C++ Extensions:
5126 __has_nothrow_assign ( type-id )
5127 __has_nothrow_constructor ( type-id )
5128 __has_nothrow_copy ( type-id )
5129 __has_trivial_assign ( type-id )
5130 __has_trivial_constructor ( type-id )
5131 __has_trivial_copy ( type-id )
5132 __has_trivial_destructor ( type-id )
5133 __has_virtual_destructor ( type-id )
5134 __is_abstract ( type-id )
5135 __is_base_of ( type-id , type-id )
5136 __is_class ( type-id )
5137 __is_empty ( type-id )
5138 __is_enum ( type-id )
5139 __is_final ( type-id )
5140 __is_literal_type ( type-id )
5141 __is_pod ( type-id )
5142 __is_polymorphic ( type-id )
5143 __is_std_layout ( type-id )
5144 __is_trivial ( type-id )
5145 __is_union ( type-id )
5146
5147 Objective-C++ Extension:
5148
5149 primary-expression:
5150 objc-expression
5151
5152 literal:
5153 __null
5154
5155 ADDRESS_P is true iff this expression was immediately preceded by
5156 "&" and therefore might denote a pointer-to-member. CAST_P is true
5157 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5158 true iff this expression is a template argument.
5159
5160 Returns a representation of the expression. Upon return, *IDK
5161 indicates what kind of id-expression (if any) was present. */
5162
5163 static cp_expr
5164 cp_parser_primary_expression (cp_parser *parser,
5165 bool address_p,
5166 bool cast_p,
5167 bool template_arg_p,
5168 bool decltype_p,
5169 cp_id_kind *idk)
5170 {
5171 cp_token *token = NULL;
5172
5173 /* Assume the primary expression is not an id-expression. */
5174 *idk = CP_ID_KIND_NONE;
5175
5176 /* Peek at the next token. */
5177 token = cp_lexer_peek_token (parser->lexer);
5178 switch ((int) token->type)
5179 {
5180 /* literal:
5181 integer-literal
5182 character-literal
5183 floating-literal
5184 string-literal
5185 boolean-literal
5186 pointer-literal
5187 user-defined-literal */
5188 case CPP_CHAR:
5189 case CPP_CHAR16:
5190 case CPP_CHAR32:
5191 case CPP_WCHAR:
5192 case CPP_UTF8CHAR:
5193 case CPP_NUMBER:
5194 case CPP_PREPARSED_EXPR:
5195 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5196 return cp_parser_userdef_numeric_literal (parser);
5197 token = cp_lexer_consume_token (parser->lexer);
5198 if (TREE_CODE (token->u.value) == FIXED_CST)
5199 {
5200 error_at (token->location,
5201 "fixed-point types not supported in C++");
5202 return error_mark_node;
5203 }
5204 /* Floating-point literals are only allowed in an integral
5205 constant expression if they are cast to an integral or
5206 enumeration type. */
5207 if (TREE_CODE (token->u.value) == REAL_CST
5208 && parser->integral_constant_expression_p
5209 && pedantic)
5210 {
5211 /* CAST_P will be set even in invalid code like "int(2.7 +
5212 ...)". Therefore, we have to check that the next token
5213 is sure to end the cast. */
5214 if (cast_p)
5215 {
5216 cp_token *next_token;
5217
5218 next_token = cp_lexer_peek_token (parser->lexer);
5219 if (/* The comma at the end of an
5220 enumerator-definition. */
5221 next_token->type != CPP_COMMA
5222 /* The curly brace at the end of an enum-specifier. */
5223 && next_token->type != CPP_CLOSE_BRACE
5224 /* The end of a statement. */
5225 && next_token->type != CPP_SEMICOLON
5226 /* The end of the cast-expression. */
5227 && next_token->type != CPP_CLOSE_PAREN
5228 /* The end of an array bound. */
5229 && next_token->type != CPP_CLOSE_SQUARE
5230 /* The closing ">" in a template-argument-list. */
5231 && (next_token->type != CPP_GREATER
5232 || parser->greater_than_is_operator_p)
5233 /* C++0x only: A ">>" treated like two ">" tokens,
5234 in a template-argument-list. */
5235 && (next_token->type != CPP_RSHIFT
5236 || (cxx_dialect == cxx98)
5237 || parser->greater_than_is_operator_p))
5238 cast_p = false;
5239 }
5240
5241 /* If we are within a cast, then the constraint that the
5242 cast is to an integral or enumeration type will be
5243 checked at that point. If we are not within a cast, then
5244 this code is invalid. */
5245 if (!cast_p)
5246 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5247 }
5248 return (cp_expr (token->u.value, token->location)
5249 .maybe_add_location_wrapper ());
5250
5251 case CPP_CHAR_USERDEF:
5252 case CPP_CHAR16_USERDEF:
5253 case CPP_CHAR32_USERDEF:
5254 case CPP_WCHAR_USERDEF:
5255 case CPP_UTF8CHAR_USERDEF:
5256 return cp_parser_userdef_char_literal (parser);
5257
5258 case CPP_STRING:
5259 case CPP_STRING16:
5260 case CPP_STRING32:
5261 case CPP_WSTRING:
5262 case CPP_UTF8STRING:
5263 case CPP_STRING_USERDEF:
5264 case CPP_STRING16_USERDEF:
5265 case CPP_STRING32_USERDEF:
5266 case CPP_WSTRING_USERDEF:
5267 case CPP_UTF8STRING_USERDEF:
5268 /* ??? Should wide strings be allowed when parser->translate_strings_p
5269 is false (i.e. in attributes)? If not, we can kill the third
5270 argument to cp_parser_string_literal. */
5271 return (cp_parser_string_literal (parser,
5272 parser->translate_strings_p,
5273 true)
5274 .maybe_add_location_wrapper ());
5275
5276 case CPP_OPEN_PAREN:
5277 /* If we see `( { ' then we are looking at the beginning of
5278 a GNU statement-expression. */
5279 if (cp_parser_allow_gnu_extensions_p (parser)
5280 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5281 {
5282 /* Statement-expressions are not allowed by the standard. */
5283 pedwarn (token->location, OPT_Wpedantic,
5284 "ISO C++ forbids braced-groups within expressions");
5285
5286 /* And they're not allowed outside of a function-body; you
5287 cannot, for example, write:
5288
5289 int i = ({ int j = 3; j + 1; });
5290
5291 at class or namespace scope. */
5292 if (!parser->in_function_body
5293 || parser->in_template_argument_list_p)
5294 {
5295 error_at (token->location,
5296 "statement-expressions are not allowed outside "
5297 "functions nor in template-argument lists");
5298 cp_parser_skip_to_end_of_block_or_statement (parser);
5299 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5300 cp_lexer_consume_token (parser->lexer);
5301 return error_mark_node;
5302 }
5303 else
5304 return cp_parser_statement_expr (parser);
5305 }
5306 /* Otherwise it's a normal parenthesized expression. */
5307 {
5308 cp_expr expr;
5309 bool saved_greater_than_is_operator_p;
5310
5311 location_t open_paren_loc = token->location;
5312
5313 /* Consume the `('. */
5314 matching_parens parens;
5315 parens.consume_open (parser);
5316 /* Within a parenthesized expression, a `>' token is always
5317 the greater-than operator. */
5318 saved_greater_than_is_operator_p
5319 = parser->greater_than_is_operator_p;
5320 parser->greater_than_is_operator_p = true;
5321
5322 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5323 /* Left fold expression. */
5324 expr = NULL_TREE;
5325 else
5326 /* Parse the parenthesized expression. */
5327 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5328
5329 token = cp_lexer_peek_token (parser->lexer);
5330 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5331 {
5332 expr = cp_parser_fold_expression (parser, expr);
5333 if (expr != error_mark_node
5334 && cxx_dialect < cxx17
5335 && !in_system_header_at (input_location))
5336 pedwarn (input_location, 0, "fold-expressions only available "
5337 "with %<-std=c++17%> or %<-std=gnu++17%>");
5338 }
5339 else
5340 /* Let the front end know that this expression was
5341 enclosed in parentheses. This matters in case, for
5342 example, the expression is of the form `A::B', since
5343 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5344 not. */
5345 expr = finish_parenthesized_expr (expr);
5346
5347 /* DR 705: Wrapping an unqualified name in parentheses
5348 suppresses arg-dependent lookup. We want to pass back
5349 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5350 (c++/37862), but none of the others. */
5351 if (*idk != CP_ID_KIND_QUALIFIED)
5352 *idk = CP_ID_KIND_NONE;
5353
5354 /* The `>' token might be the end of a template-id or
5355 template-parameter-list now. */
5356 parser->greater_than_is_operator_p
5357 = saved_greater_than_is_operator_p;
5358
5359 /* Consume the `)'. */
5360 token = cp_lexer_peek_token (parser->lexer);
5361 location_t close_paren_loc = token->location;
5362 expr.set_range (open_paren_loc, close_paren_loc);
5363 if (!parens.require_close (parser)
5364 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5365 cp_parser_skip_to_end_of_statement (parser);
5366
5367 return expr;
5368 }
5369
5370 case CPP_OPEN_SQUARE:
5371 {
5372 if (c_dialect_objc ())
5373 {
5374 /* We might have an Objective-C++ message. */
5375 cp_parser_parse_tentatively (parser);
5376 tree msg = cp_parser_objc_message_expression (parser);
5377 /* If that works out, we're done ... */
5378 if (cp_parser_parse_definitely (parser))
5379 return msg;
5380 /* ... else, fall though to see if it's a lambda. */
5381 }
5382 cp_expr lam = cp_parser_lambda_expression (parser);
5383 /* Don't warn about a failed tentative parse. */
5384 if (cp_parser_error_occurred (parser))
5385 return error_mark_node;
5386 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5387 return lam;
5388 }
5389
5390 case CPP_OBJC_STRING:
5391 if (c_dialect_objc ())
5392 /* We have an Objective-C++ string literal. */
5393 return cp_parser_objc_expression (parser);
5394 cp_parser_error (parser, "expected primary-expression");
5395 return error_mark_node;
5396
5397 case CPP_KEYWORD:
5398 switch (token->keyword)
5399 {
5400 /* These two are the boolean literals. */
5401 case RID_TRUE:
5402 cp_lexer_consume_token (parser->lexer);
5403 return cp_expr (boolean_true_node, token->location);
5404 case RID_FALSE:
5405 cp_lexer_consume_token (parser->lexer);
5406 return cp_expr (boolean_false_node, token->location);
5407
5408 /* The `__null' literal. */
5409 case RID_NULL:
5410 cp_lexer_consume_token (parser->lexer);
5411 return cp_expr (null_node, token->location);
5412
5413 /* The `nullptr' literal. */
5414 case RID_NULLPTR:
5415 cp_lexer_consume_token (parser->lexer);
5416 return cp_expr (nullptr_node, token->location);
5417
5418 /* Recognize the `this' keyword. */
5419 case RID_THIS:
5420 cp_lexer_consume_token (parser->lexer);
5421 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5422 {
5423 error_at (token->location,
5424 "%<this%> may not be used in this context");
5425 return error_mark_node;
5426 }
5427 /* Pointers cannot appear in constant-expressions. */
5428 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5429 return error_mark_node;
5430 return cp_expr (finish_this_expr (), token->location);
5431
5432 /* The `operator' keyword can be the beginning of an
5433 id-expression. */
5434 case RID_OPERATOR:
5435 goto id_expression;
5436
5437 case RID_FUNCTION_NAME:
5438 case RID_PRETTY_FUNCTION_NAME:
5439 case RID_C99_FUNCTION_NAME:
5440 {
5441 non_integral_constant name;
5442
5443 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5444 __func__ are the names of variables -- but they are
5445 treated specially. Therefore, they are handled here,
5446 rather than relying on the generic id-expression logic
5447 below. Grammatically, these names are id-expressions.
5448
5449 Consume the token. */
5450 token = cp_lexer_consume_token (parser->lexer);
5451
5452 switch (token->keyword)
5453 {
5454 case RID_FUNCTION_NAME:
5455 name = NIC_FUNC_NAME;
5456 break;
5457 case RID_PRETTY_FUNCTION_NAME:
5458 name = NIC_PRETTY_FUNC;
5459 break;
5460 case RID_C99_FUNCTION_NAME:
5461 name = NIC_C99_FUNC;
5462 break;
5463 default:
5464 gcc_unreachable ();
5465 }
5466
5467 if (cp_parser_non_integral_constant_expression (parser, name))
5468 return error_mark_node;
5469
5470 /* Look up the name. */
5471 return finish_fname (token->u.value);
5472 }
5473
5474 case RID_VA_ARG:
5475 {
5476 tree expression;
5477 tree type;
5478 location_t type_location;
5479 location_t start_loc
5480 = cp_lexer_peek_token (parser->lexer)->location;
5481 /* The `__builtin_va_arg' construct is used to handle
5482 `va_arg'. Consume the `__builtin_va_arg' token. */
5483 cp_lexer_consume_token (parser->lexer);
5484 /* Look for the opening `('. */
5485 matching_parens parens;
5486 parens.require_open (parser);
5487 /* Now, parse the assignment-expression. */
5488 expression = cp_parser_assignment_expression (parser);
5489 /* Look for the `,'. */
5490 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5491 type_location = cp_lexer_peek_token (parser->lexer)->location;
5492 /* Parse the type-id. */
5493 {
5494 type_id_in_expr_sentinel s (parser);
5495 type = cp_parser_type_id (parser);
5496 }
5497 /* Look for the closing `)'. */
5498 location_t finish_loc
5499 = cp_lexer_peek_token (parser->lexer)->location;
5500 parens.require_close (parser);
5501 /* Using `va_arg' in a constant-expression is not
5502 allowed. */
5503 if (cp_parser_non_integral_constant_expression (parser,
5504 NIC_VA_ARG))
5505 return error_mark_node;
5506 /* Construct a location of the form:
5507 __builtin_va_arg (v, int)
5508 ~~~~~~~~~~~~~~~~~~~~~^~~~
5509 with the caret at the type, ranging from the start of the
5510 "__builtin_va_arg" token to the close paren. */
5511 location_t combined_loc
5512 = make_location (type_location, start_loc, finish_loc);
5513 return build_x_va_arg (combined_loc, expression, type);
5514 }
5515
5516 case RID_OFFSETOF:
5517 return cp_parser_builtin_offsetof (parser);
5518
5519 case RID_HAS_NOTHROW_ASSIGN:
5520 case RID_HAS_NOTHROW_CONSTRUCTOR:
5521 case RID_HAS_NOTHROW_COPY:
5522 case RID_HAS_TRIVIAL_ASSIGN:
5523 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5524 case RID_HAS_TRIVIAL_COPY:
5525 case RID_HAS_TRIVIAL_DESTRUCTOR:
5526 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5527 case RID_HAS_VIRTUAL_DESTRUCTOR:
5528 case RID_IS_ABSTRACT:
5529 case RID_IS_AGGREGATE:
5530 case RID_IS_BASE_OF:
5531 case RID_IS_CLASS:
5532 case RID_IS_EMPTY:
5533 case RID_IS_ENUM:
5534 case RID_IS_FINAL:
5535 case RID_IS_LITERAL_TYPE:
5536 case RID_IS_POD:
5537 case RID_IS_POLYMORPHIC:
5538 case RID_IS_SAME_AS:
5539 case RID_IS_STD_LAYOUT:
5540 case RID_IS_TRIVIAL:
5541 case RID_IS_TRIVIALLY_ASSIGNABLE:
5542 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5543 case RID_IS_TRIVIALLY_COPYABLE:
5544 case RID_IS_UNION:
5545 case RID_IS_ASSIGNABLE:
5546 case RID_IS_CONSTRUCTIBLE:
5547 return cp_parser_trait_expr (parser, token->keyword);
5548
5549 // C++ concepts
5550 case RID_REQUIRES:
5551 return cp_parser_requires_expression (parser);
5552
5553 /* Objective-C++ expressions. */
5554 case RID_AT_ENCODE:
5555 case RID_AT_PROTOCOL:
5556 case RID_AT_SELECTOR:
5557 return cp_parser_objc_expression (parser);
5558
5559 case RID_TEMPLATE:
5560 if (parser->in_function_body
5561 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5562 == CPP_LESS))
5563 {
5564 error_at (token->location,
5565 "a template declaration cannot appear at block scope");
5566 cp_parser_skip_to_end_of_block_or_statement (parser);
5567 return error_mark_node;
5568 }
5569 /* FALLTHRU */
5570 default:
5571 cp_parser_error (parser, "expected primary-expression");
5572 return error_mark_node;
5573 }
5574
5575 /* An id-expression can start with either an identifier, a
5576 `::' as the beginning of a qualified-id, or the "operator"
5577 keyword. */
5578 case CPP_NAME:
5579 case CPP_SCOPE:
5580 case CPP_TEMPLATE_ID:
5581 case CPP_NESTED_NAME_SPECIFIER:
5582 {
5583 id_expression:
5584 cp_expr id_expression;
5585 cp_expr decl;
5586 const char *error_msg;
5587 bool template_p;
5588 bool done;
5589 cp_token *id_expr_token;
5590
5591 /* Parse the id-expression. */
5592 id_expression
5593 = cp_parser_id_expression (parser,
5594 /*template_keyword_p=*/false,
5595 /*check_dependency_p=*/true,
5596 &template_p,
5597 /*declarator_p=*/false,
5598 /*optional_p=*/false);
5599 if (id_expression == error_mark_node)
5600 return error_mark_node;
5601 id_expr_token = token;
5602 token = cp_lexer_peek_token (parser->lexer);
5603 done = (token->type != CPP_OPEN_SQUARE
5604 && token->type != CPP_OPEN_PAREN
5605 && token->type != CPP_DOT
5606 && token->type != CPP_DEREF
5607 && token->type != CPP_PLUS_PLUS
5608 && token->type != CPP_MINUS_MINUS);
5609 /* If we have a template-id, then no further lookup is
5610 required. If the template-id was for a template-class, we
5611 will sometimes have a TYPE_DECL at this point. */
5612 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5613 || TREE_CODE (id_expression) == TYPE_DECL)
5614 decl = id_expression;
5615 /* Look up the name. */
5616 else
5617 {
5618 tree ambiguous_decls;
5619
5620 /* If we already know that this lookup is ambiguous, then
5621 we've already issued an error message; there's no reason
5622 to check again. */
5623 if (id_expr_token->type == CPP_NAME
5624 && id_expr_token->error_reported)
5625 {
5626 cp_parser_simulate_error (parser);
5627 return error_mark_node;
5628 }
5629
5630 decl = cp_parser_lookup_name (parser, id_expression,
5631 none_type,
5632 template_p,
5633 /*is_namespace=*/false,
5634 /*check_dependency=*/true,
5635 &ambiguous_decls,
5636 id_expression.get_location ());
5637 /* If the lookup was ambiguous, an error will already have
5638 been issued. */
5639 if (ambiguous_decls)
5640 return error_mark_node;
5641
5642 /* In Objective-C++, we may have an Objective-C 2.0
5643 dot-syntax for classes here. */
5644 if (c_dialect_objc ()
5645 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5646 && TREE_CODE (decl) == TYPE_DECL
5647 && objc_is_class_name (decl))
5648 {
5649 tree component;
5650 cp_lexer_consume_token (parser->lexer);
5651 component = cp_parser_identifier (parser);
5652 if (component == error_mark_node)
5653 return error_mark_node;
5654
5655 tree result = objc_build_class_component_ref (id_expression,
5656 component);
5657 /* Build a location of the form:
5658 expr.component
5659 ~~~~~^~~~~~~~~
5660 with caret at the start of the component name (at
5661 input_location), ranging from the start of the id_expression
5662 to the end of the component name. */
5663 location_t combined_loc
5664 = make_location (input_location, id_expression.get_start (),
5665 get_finish (input_location));
5666 protected_set_expr_location (result, combined_loc);
5667 return result;
5668 }
5669
5670 /* In Objective-C++, an instance variable (ivar) may be preferred
5671 to whatever cp_parser_lookup_name() found.
5672 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5673 rest of c-family, we have to do a little extra work to preserve
5674 any location information in cp_expr "decl". Given that
5675 objc_lookup_ivar is implemented in "c-family" and "objc", we
5676 have a trip through the pure "tree" type, rather than cp_expr.
5677 Naively copying it back to "decl" would implicitly give the
5678 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5679 store an EXPR_LOCATION. Hence we only update "decl" (and
5680 hence its location_t) if we get back a different tree node. */
5681 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5682 id_expression);
5683 if (decl_tree != decl.get_value ())
5684 decl = cp_expr (decl_tree);
5685
5686 /* If name lookup gives us a SCOPE_REF, then the
5687 qualifying scope was dependent. */
5688 if (TREE_CODE (decl) == SCOPE_REF)
5689 {
5690 /* At this point, we do not know if DECL is a valid
5691 integral constant expression. We assume that it is
5692 in fact such an expression, so that code like:
5693
5694 template <int N> struct A {
5695 int a[B<N>::i];
5696 };
5697
5698 is accepted. At template-instantiation time, we
5699 will check that B<N>::i is actually a constant. */
5700 return decl;
5701 }
5702 /* Check to see if DECL is a local variable in a context
5703 where that is forbidden. */
5704 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
5705 && local_variable_p (decl))
5706 {
5707 error_at (id_expression.get_location (),
5708 "local variable %qD may not appear in this context",
5709 decl.get_value ());
5710 return error_mark_node;
5711 }
5712 }
5713
5714 decl = (finish_id_expression
5715 (id_expression, decl, parser->scope,
5716 idk,
5717 parser->integral_constant_expression_p,
5718 parser->allow_non_integral_constant_expression_p,
5719 &parser->non_integral_constant_expression_p,
5720 template_p, done, address_p,
5721 template_arg_p,
5722 &error_msg,
5723 id_expression.get_location ()));
5724 if (error_msg)
5725 cp_parser_error (parser, error_msg);
5726 /* Build a location for an id-expression of the form:
5727 ::ns::id
5728 ~~~~~~^~
5729 or:
5730 id
5731 ^~
5732 i.e. from the start of the first token to the end of the final
5733 token, with the caret at the start of the unqualified-id. */
5734 location_t caret_loc = get_pure_location (id_expression.get_location ());
5735 location_t start_loc = get_start (id_expr_token->location);
5736 location_t finish_loc = get_finish (id_expression.get_location ());
5737 location_t combined_loc
5738 = make_location (caret_loc, start_loc, finish_loc);
5739
5740 decl.set_location (combined_loc);
5741 return decl;
5742 }
5743
5744 /* Anything else is an error. */
5745 default:
5746 cp_parser_error (parser, "expected primary-expression");
5747 return error_mark_node;
5748 }
5749 }
5750
5751 static inline cp_expr
5752 cp_parser_primary_expression (cp_parser *parser,
5753 bool address_p,
5754 bool cast_p,
5755 bool template_arg_p,
5756 cp_id_kind *idk)
5757 {
5758 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5759 /*decltype*/false, idk);
5760 }
5761
5762 /* Parse an id-expression.
5763
5764 id-expression:
5765 unqualified-id
5766 qualified-id
5767
5768 qualified-id:
5769 :: [opt] nested-name-specifier template [opt] unqualified-id
5770 :: identifier
5771 :: operator-function-id
5772 :: template-id
5773
5774 Return a representation of the unqualified portion of the
5775 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5776 a `::' or nested-name-specifier.
5777
5778 Often, if the id-expression was a qualified-id, the caller will
5779 want to make a SCOPE_REF to represent the qualified-id. This
5780 function does not do this in order to avoid wastefully creating
5781 SCOPE_REFs when they are not required.
5782
5783 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5784 `template' keyword.
5785
5786 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5787 uninstantiated templates.
5788
5789 If *TEMPLATE_P is non-NULL, it is set to true iff the
5790 `template' keyword is used to explicitly indicate that the entity
5791 named is a template.
5792
5793 If DECLARATOR_P is true, the id-expression is appearing as part of
5794 a declarator, rather than as part of an expression. */
5795
5796 static cp_expr
5797 cp_parser_id_expression (cp_parser *parser,
5798 bool template_keyword_p,
5799 bool check_dependency_p,
5800 bool *template_p,
5801 bool declarator_p,
5802 bool optional_p)
5803 {
5804 bool global_scope_p;
5805 bool nested_name_specifier_p;
5806
5807 /* Assume the `template' keyword was not used. */
5808 if (template_p)
5809 *template_p = template_keyword_p;
5810
5811 /* Look for the optional `::' operator. */
5812 global_scope_p
5813 = (!template_keyword_p
5814 && (cp_parser_global_scope_opt (parser,
5815 /*current_scope_valid_p=*/false)
5816 != NULL_TREE));
5817
5818 /* Look for the optional nested-name-specifier. */
5819 nested_name_specifier_p
5820 = (cp_parser_nested_name_specifier_opt (parser,
5821 /*typename_keyword_p=*/false,
5822 check_dependency_p,
5823 /*type_p=*/false,
5824 declarator_p,
5825 template_keyword_p)
5826 != NULL_TREE);
5827
5828 /* If there is a nested-name-specifier, then we are looking at
5829 the first qualified-id production. */
5830 if (nested_name_specifier_p)
5831 {
5832 tree saved_scope;
5833 tree saved_object_scope;
5834 tree saved_qualifying_scope;
5835 cp_expr unqualified_id;
5836 bool is_template;
5837
5838 /* See if the next token is the `template' keyword. */
5839 if (!template_p)
5840 template_p = &is_template;
5841 *template_p = cp_parser_optional_template_keyword (parser);
5842 /* Name lookup we do during the processing of the
5843 unqualified-id might obliterate SCOPE. */
5844 saved_scope = parser->scope;
5845 saved_object_scope = parser->object_scope;
5846 saved_qualifying_scope = parser->qualifying_scope;
5847 /* Process the final unqualified-id. */
5848 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5849 check_dependency_p,
5850 declarator_p,
5851 /*optional_p=*/false);
5852 /* Restore the SAVED_SCOPE for our caller. */
5853 parser->scope = saved_scope;
5854 parser->object_scope = saved_object_scope;
5855 parser->qualifying_scope = saved_qualifying_scope;
5856
5857 return unqualified_id;
5858 }
5859 /* Otherwise, if we are in global scope, then we are looking at one
5860 of the other qualified-id productions. */
5861 else if (global_scope_p)
5862 {
5863 cp_token *token;
5864 tree id;
5865
5866 /* Peek at the next token. */
5867 token = cp_lexer_peek_token (parser->lexer);
5868
5869 /* If it's an identifier, and the next token is not a "<", then
5870 we can avoid the template-id case. This is an optimization
5871 for this common case. */
5872 if (token->type == CPP_NAME
5873 && !cp_parser_nth_token_starts_template_argument_list_p
5874 (parser, 2))
5875 return cp_parser_identifier (parser);
5876
5877 cp_parser_parse_tentatively (parser);
5878 /* Try a template-id. */
5879 id = cp_parser_template_id (parser,
5880 /*template_keyword_p=*/false,
5881 /*check_dependency_p=*/true,
5882 none_type,
5883 declarator_p);
5884 /* If that worked, we're done. */
5885 if (cp_parser_parse_definitely (parser))
5886 return id;
5887
5888 /* Peek at the next token. (Changes in the token buffer may
5889 have invalidated the pointer obtained above.) */
5890 token = cp_lexer_peek_token (parser->lexer);
5891
5892 switch (token->type)
5893 {
5894 case CPP_NAME:
5895 return cp_parser_identifier (parser);
5896
5897 case CPP_KEYWORD:
5898 if (token->keyword == RID_OPERATOR)
5899 return cp_parser_operator_function_id (parser);
5900 /* Fall through. */
5901
5902 default:
5903 cp_parser_error (parser, "expected id-expression");
5904 return error_mark_node;
5905 }
5906 }
5907 else
5908 return cp_parser_unqualified_id (parser, template_keyword_p,
5909 /*check_dependency_p=*/true,
5910 declarator_p,
5911 optional_p);
5912 }
5913
5914 /* Parse an unqualified-id.
5915
5916 unqualified-id:
5917 identifier
5918 operator-function-id
5919 conversion-function-id
5920 ~ class-name
5921 template-id
5922
5923 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5924 keyword, in a construct like `A::template ...'.
5925
5926 Returns a representation of unqualified-id. For the `identifier'
5927 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5928 production a BIT_NOT_EXPR is returned; the operand of the
5929 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5930 other productions, see the documentation accompanying the
5931 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5932 names are looked up in uninstantiated templates. If DECLARATOR_P
5933 is true, the unqualified-id is appearing as part of a declarator,
5934 rather than as part of an expression. */
5935
5936 static cp_expr
5937 cp_parser_unqualified_id (cp_parser* parser,
5938 bool template_keyword_p,
5939 bool check_dependency_p,
5940 bool declarator_p,
5941 bool optional_p)
5942 {
5943 cp_token *token;
5944
5945 /* Peek at the next token. */
5946 token = cp_lexer_peek_token (parser->lexer);
5947
5948 switch ((int) token->type)
5949 {
5950 case CPP_NAME:
5951 {
5952 tree id;
5953
5954 /* We don't know yet whether or not this will be a
5955 template-id. */
5956 cp_parser_parse_tentatively (parser);
5957 /* Try a template-id. */
5958 id = cp_parser_template_id (parser, template_keyword_p,
5959 check_dependency_p,
5960 none_type,
5961 declarator_p);
5962 /* If it worked, we're done. */
5963 if (cp_parser_parse_definitely (parser))
5964 return id;
5965 /* Otherwise, it's an ordinary identifier. */
5966 return cp_parser_identifier (parser);
5967 }
5968
5969 case CPP_TEMPLATE_ID:
5970 return cp_parser_template_id (parser, template_keyword_p,
5971 check_dependency_p,
5972 none_type,
5973 declarator_p);
5974
5975 case CPP_COMPL:
5976 {
5977 tree type_decl;
5978 tree qualifying_scope;
5979 tree object_scope;
5980 tree scope;
5981 bool done;
5982 location_t tilde_loc = token->location;
5983
5984 /* Consume the `~' token. */
5985 cp_lexer_consume_token (parser->lexer);
5986 /* Parse the class-name. The standard, as written, seems to
5987 say that:
5988
5989 template <typename T> struct S { ~S (); };
5990 template <typename T> S<T>::~S() {}
5991
5992 is invalid, since `~' must be followed by a class-name, but
5993 `S<T>' is dependent, and so not known to be a class.
5994 That's not right; we need to look in uninstantiated
5995 templates. A further complication arises from:
5996
5997 template <typename T> void f(T t) {
5998 t.T::~T();
5999 }
6000
6001 Here, it is not possible to look up `T' in the scope of `T'
6002 itself. We must look in both the current scope, and the
6003 scope of the containing complete expression.
6004
6005 Yet another issue is:
6006
6007 struct S {
6008 int S;
6009 ~S();
6010 };
6011
6012 S::~S() {}
6013
6014 The standard does not seem to say that the `S' in `~S'
6015 should refer to the type `S' and not the data member
6016 `S::S'. */
6017
6018 /* DR 244 says that we look up the name after the "~" in the
6019 same scope as we looked up the qualifying name. That idea
6020 isn't fully worked out; it's more complicated than that. */
6021 scope = parser->scope;
6022 object_scope = parser->object_scope;
6023 qualifying_scope = parser->qualifying_scope;
6024
6025 /* Check for invalid scopes. */
6026 if (scope == error_mark_node)
6027 {
6028 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6029 cp_lexer_consume_token (parser->lexer);
6030 return error_mark_node;
6031 }
6032 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6033 {
6034 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6035 error_at (token->location,
6036 "scope %qT before %<~%> is not a class-name",
6037 scope);
6038 cp_parser_simulate_error (parser);
6039 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6040 cp_lexer_consume_token (parser->lexer);
6041 return error_mark_node;
6042 }
6043 gcc_assert (!scope || TYPE_P (scope));
6044
6045 token = cp_lexer_peek_token (parser->lexer);
6046
6047 /* Create a location with caret == start at the tilde,
6048 finishing at the end of the peeked token, e.g:
6049 ~token
6050 ^~~~~~. */
6051 location_t loc
6052 = make_location (tilde_loc, tilde_loc, token->location);
6053
6054 /* If the name is of the form "X::~X" it's OK even if X is a
6055 typedef. */
6056
6057 if (scope
6058 && token->type == CPP_NAME
6059 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6060 != CPP_LESS)
6061 && (token->u.value == TYPE_IDENTIFIER (scope)
6062 || (CLASS_TYPE_P (scope)
6063 && constructor_name_p (token->u.value, scope))))
6064 {
6065 cp_lexer_consume_token (parser->lexer);
6066 return cp_expr (build_nt (BIT_NOT_EXPR, scope), loc);
6067 }
6068
6069 /* ~auto means the destructor of whatever the object is. */
6070 if (cp_parser_is_keyword (token, RID_AUTO))
6071 {
6072 if (cxx_dialect < cxx14)
6073 pedwarn (loc, 0,
6074 "%<~auto%> only available with "
6075 "%<-std=c++14%> or %<-std=gnu++14%>");
6076 cp_lexer_consume_token (parser->lexer);
6077 return cp_expr (build_nt (BIT_NOT_EXPR, make_auto (), loc));
6078 }
6079
6080 /* If there was an explicit qualification (S::~T), first look
6081 in the scope given by the qualification (i.e., S).
6082
6083 Note: in the calls to cp_parser_class_name below we pass
6084 typename_type so that lookup finds the injected-class-name
6085 rather than the constructor. */
6086 done = false;
6087 type_decl = NULL_TREE;
6088 if (scope)
6089 {
6090 cp_parser_parse_tentatively (parser);
6091 type_decl = cp_parser_class_name (parser,
6092 /*typename_keyword_p=*/false,
6093 /*template_keyword_p=*/false,
6094 typename_type,
6095 /*check_dependency=*/false,
6096 /*class_head_p=*/false,
6097 declarator_p);
6098 if (cp_parser_parse_definitely (parser))
6099 done = true;
6100 }
6101 /* In "N::S::~S", look in "N" as well. */
6102 if (!done && scope && qualifying_scope)
6103 {
6104 cp_parser_parse_tentatively (parser);
6105 parser->scope = qualifying_scope;
6106 parser->object_scope = NULL_TREE;
6107 parser->qualifying_scope = NULL_TREE;
6108 type_decl
6109 = cp_parser_class_name (parser,
6110 /*typename_keyword_p=*/false,
6111 /*template_keyword_p=*/false,
6112 typename_type,
6113 /*check_dependency=*/false,
6114 /*class_head_p=*/false,
6115 declarator_p);
6116 if (cp_parser_parse_definitely (parser))
6117 done = true;
6118 }
6119 /* In "p->S::~T", look in the scope given by "*p" as well. */
6120 else if (!done && object_scope)
6121 {
6122 cp_parser_parse_tentatively (parser);
6123 parser->scope = object_scope;
6124 parser->object_scope = NULL_TREE;
6125 parser->qualifying_scope = NULL_TREE;
6126 type_decl
6127 = cp_parser_class_name (parser,
6128 /*typename_keyword_p=*/false,
6129 /*template_keyword_p=*/false,
6130 typename_type,
6131 /*check_dependency=*/false,
6132 /*class_head_p=*/false,
6133 declarator_p);
6134 if (cp_parser_parse_definitely (parser))
6135 done = true;
6136 }
6137 /* Look in the surrounding context. */
6138 if (!done)
6139 {
6140 parser->scope = NULL_TREE;
6141 parser->object_scope = NULL_TREE;
6142 parser->qualifying_scope = NULL_TREE;
6143 if (processing_template_decl)
6144 cp_parser_parse_tentatively (parser);
6145 type_decl
6146 = cp_parser_class_name (parser,
6147 /*typename_keyword_p=*/false,
6148 /*template_keyword_p=*/false,
6149 typename_type,
6150 /*check_dependency=*/false,
6151 /*class_head_p=*/false,
6152 declarator_p);
6153 if (processing_template_decl
6154 && ! cp_parser_parse_definitely (parser))
6155 {
6156 /* We couldn't find a type with this name. If we're parsing
6157 tentatively, fail and try something else. */
6158 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6159 {
6160 cp_parser_simulate_error (parser);
6161 return error_mark_node;
6162 }
6163 /* Otherwise, accept it and check for a match at instantiation
6164 time. */
6165 type_decl = cp_parser_identifier (parser);
6166 if (type_decl != error_mark_node)
6167 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6168 return cp_expr (type_decl, loc);
6169 }
6170 }
6171 /* If an error occurred, assume that the name of the
6172 destructor is the same as the name of the qualifying
6173 class. That allows us to keep parsing after running
6174 into ill-formed destructor names. */
6175 if (type_decl == error_mark_node && scope)
6176 return build_nt (BIT_NOT_EXPR, scope);
6177 else if (type_decl == error_mark_node)
6178 return error_mark_node;
6179
6180 /* Check that destructor name and scope match. */
6181 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6182 {
6183 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6184 error_at (loc,
6185 "declaration of %<~%T%> as member of %qT",
6186 type_decl, scope);
6187 cp_parser_simulate_error (parser);
6188 return error_mark_node;
6189 }
6190
6191 /* [class.dtor]
6192
6193 A typedef-name that names a class shall not be used as the
6194 identifier in the declarator for a destructor declaration. */
6195 if (declarator_p
6196 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6197 && !DECL_SELF_REFERENCE_P (type_decl)
6198 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6199 error_at (loc,
6200 "typedef-name %qD used as destructor declarator",
6201 type_decl);
6202
6203 return cp_expr (build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl), loc));
6204 }
6205
6206 case CPP_KEYWORD:
6207 if (token->keyword == RID_OPERATOR)
6208 {
6209 cp_expr id;
6210
6211 /* This could be a template-id, so we try that first. */
6212 cp_parser_parse_tentatively (parser);
6213 /* Try a template-id. */
6214 id = cp_parser_template_id (parser, template_keyword_p,
6215 /*check_dependency_p=*/true,
6216 none_type,
6217 declarator_p);
6218 /* If that worked, we're done. */
6219 if (cp_parser_parse_definitely (parser))
6220 return id;
6221 /* We still don't know whether we're looking at an
6222 operator-function-id or a conversion-function-id. */
6223 cp_parser_parse_tentatively (parser);
6224 /* Try an operator-function-id. */
6225 id = cp_parser_operator_function_id (parser);
6226 /* If that didn't work, try a conversion-function-id. */
6227 if (!cp_parser_parse_definitely (parser))
6228 id = cp_parser_conversion_function_id (parser);
6229
6230 return id;
6231 }
6232 /* Fall through. */
6233
6234 default:
6235 if (optional_p)
6236 return NULL_TREE;
6237 cp_parser_error (parser, "expected unqualified-id");
6238 return error_mark_node;
6239 }
6240 }
6241
6242 /* Parse an (optional) nested-name-specifier.
6243
6244 nested-name-specifier: [C++98]
6245 class-or-namespace-name :: nested-name-specifier [opt]
6246 class-or-namespace-name :: template nested-name-specifier [opt]
6247
6248 nested-name-specifier: [C++0x]
6249 type-name ::
6250 namespace-name ::
6251 nested-name-specifier identifier ::
6252 nested-name-specifier template [opt] simple-template-id ::
6253
6254 PARSER->SCOPE should be set appropriately before this function is
6255 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6256 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6257 in name lookups.
6258
6259 Sets PARSER->SCOPE to the class (TYPE) or namespace
6260 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6261 it unchanged if there is no nested-name-specifier. Returns the new
6262 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6263
6264 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6265 part of a declaration and/or decl-specifier. */
6266
6267 static tree
6268 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6269 bool typename_keyword_p,
6270 bool check_dependency_p,
6271 bool type_p,
6272 bool is_declaration,
6273 bool template_keyword_p /* = false */)
6274 {
6275 bool success = false;
6276 cp_token_position start = 0;
6277 cp_token *token;
6278
6279 /* Remember where the nested-name-specifier starts. */
6280 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6281 {
6282 start = cp_lexer_token_position (parser->lexer, false);
6283 push_deferring_access_checks (dk_deferred);
6284 }
6285
6286 while (true)
6287 {
6288 tree new_scope;
6289 tree old_scope;
6290 tree saved_qualifying_scope;
6291
6292 /* Spot cases that cannot be the beginning of a
6293 nested-name-specifier. */
6294 token = cp_lexer_peek_token (parser->lexer);
6295
6296 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6297 the already parsed nested-name-specifier. */
6298 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6299 {
6300 /* Grab the nested-name-specifier and continue the loop. */
6301 cp_parser_pre_parsed_nested_name_specifier (parser);
6302 /* If we originally encountered this nested-name-specifier
6303 with IS_DECLARATION set to false, we will not have
6304 resolved TYPENAME_TYPEs, so we must do so here. */
6305 if (is_declaration
6306 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6307 {
6308 new_scope = resolve_typename_type (parser->scope,
6309 /*only_current_p=*/false);
6310 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6311 parser->scope = new_scope;
6312 }
6313 success = true;
6314 continue;
6315 }
6316
6317 /* Spot cases that cannot be the beginning of a
6318 nested-name-specifier. On the second and subsequent times
6319 through the loop, we look for the `template' keyword. */
6320 if (success && token->keyword == RID_TEMPLATE)
6321 ;
6322 /* A template-id can start a nested-name-specifier. */
6323 else if (token->type == CPP_TEMPLATE_ID)
6324 ;
6325 /* DR 743: decltype can be used in a nested-name-specifier. */
6326 else if (token_is_decltype (token))
6327 ;
6328 else
6329 {
6330 /* If the next token is not an identifier, then it is
6331 definitely not a type-name or namespace-name. */
6332 if (token->type != CPP_NAME)
6333 break;
6334 /* If the following token is neither a `<' (to begin a
6335 template-id), nor a `::', then we are not looking at a
6336 nested-name-specifier. */
6337 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6338
6339 if (token->type == CPP_COLON
6340 && parser->colon_corrects_to_scope_p
6341 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6342 {
6343 gcc_rich_location richloc (token->location);
6344 richloc.add_fixit_replace ("::");
6345 error_at (&richloc,
6346 "found %<:%> in nested-name-specifier, "
6347 "expected %<::%>");
6348 token->type = CPP_SCOPE;
6349 }
6350
6351 if (token->type != CPP_SCOPE
6352 && !cp_parser_nth_token_starts_template_argument_list_p
6353 (parser, 2))
6354 break;
6355 }
6356
6357 /* The nested-name-specifier is optional, so we parse
6358 tentatively. */
6359 cp_parser_parse_tentatively (parser);
6360
6361 /* Look for the optional `template' keyword, if this isn't the
6362 first time through the loop. */
6363 if (success)
6364 template_keyword_p = cp_parser_optional_template_keyword (parser);
6365
6366 /* Save the old scope since the name lookup we are about to do
6367 might destroy it. */
6368 old_scope = parser->scope;
6369 saved_qualifying_scope = parser->qualifying_scope;
6370 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6371 look up names in "X<T>::I" in order to determine that "Y" is
6372 a template. So, if we have a typename at this point, we make
6373 an effort to look through it. */
6374 if (is_declaration
6375 && !typename_keyword_p
6376 && parser->scope
6377 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6378 parser->scope = resolve_typename_type (parser->scope,
6379 /*only_current_p=*/false);
6380 /* Parse the qualifying entity. */
6381 new_scope
6382 = cp_parser_qualifying_entity (parser,
6383 typename_keyword_p,
6384 template_keyword_p,
6385 check_dependency_p,
6386 type_p,
6387 is_declaration);
6388 /* Look for the `::' token. */
6389 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6390
6391 /* If we found what we wanted, we keep going; otherwise, we're
6392 done. */
6393 if (!cp_parser_parse_definitely (parser))
6394 {
6395 bool error_p = false;
6396
6397 /* Restore the OLD_SCOPE since it was valid before the
6398 failed attempt at finding the last
6399 class-or-namespace-name. */
6400 parser->scope = old_scope;
6401 parser->qualifying_scope = saved_qualifying_scope;
6402
6403 /* If the next token is a decltype, and the one after that is a
6404 `::', then the decltype has failed to resolve to a class or
6405 enumeration type. Give this error even when parsing
6406 tentatively since it can't possibly be valid--and we're going
6407 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6408 won't get another chance.*/
6409 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6410 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6411 == CPP_SCOPE))
6412 {
6413 token = cp_lexer_consume_token (parser->lexer);
6414 error_at (token->location, "decltype evaluates to %qT, "
6415 "which is not a class or enumeration type",
6416 token->u.tree_check_value->value);
6417 parser->scope = error_mark_node;
6418 error_p = true;
6419 /* As below. */
6420 success = true;
6421 cp_lexer_consume_token (parser->lexer);
6422 }
6423
6424 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6425 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6426 {
6427 /* If we have a non-type template-id followed by ::, it can't
6428 possibly be valid. */
6429 token = cp_lexer_peek_token (parser->lexer);
6430 tree tid = token->u.tree_check_value->value;
6431 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6432 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6433 {
6434 tree tmpl = NULL_TREE;
6435 if (is_overloaded_fn (tid))
6436 {
6437 tree fns = get_fns (tid);
6438 if (OVL_SINGLE_P (fns))
6439 tmpl = OVL_FIRST (fns);
6440 error_at (token->location, "function template-id %qD "
6441 "in nested-name-specifier", tid);
6442 }
6443 else
6444 {
6445 /* Variable template. */
6446 tmpl = TREE_OPERAND (tid, 0);
6447 gcc_assert (variable_template_p (tmpl));
6448 error_at (token->location, "variable template-id %qD "
6449 "in nested-name-specifier", tid);
6450 }
6451 if (tmpl)
6452 inform (DECL_SOURCE_LOCATION (tmpl),
6453 "%qD declared here", tmpl);
6454
6455 parser->scope = error_mark_node;
6456 error_p = true;
6457 /* As below. */
6458 success = true;
6459 cp_lexer_consume_token (parser->lexer);
6460 cp_lexer_consume_token (parser->lexer);
6461 }
6462 }
6463
6464 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6465 break;
6466 /* If the next token is an identifier, and the one after
6467 that is a `::', then any valid interpretation would have
6468 found a class-or-namespace-name. */
6469 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6470 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6471 == CPP_SCOPE)
6472 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6473 != CPP_COMPL))
6474 {
6475 token = cp_lexer_consume_token (parser->lexer);
6476 if (!error_p)
6477 {
6478 if (!token->error_reported)
6479 {
6480 tree decl;
6481 tree ambiguous_decls;
6482
6483 decl = cp_parser_lookup_name (parser, token->u.value,
6484 none_type,
6485 /*is_template=*/false,
6486 /*is_namespace=*/false,
6487 /*check_dependency=*/true,
6488 &ambiguous_decls,
6489 token->location);
6490 if (TREE_CODE (decl) == TEMPLATE_DECL)
6491 error_at (token->location,
6492 "%qD used without template arguments",
6493 decl);
6494 else if (ambiguous_decls)
6495 {
6496 // cp_parser_lookup_name has the same diagnostic,
6497 // thus make sure to emit it at most once.
6498 if (cp_parser_uncommitted_to_tentative_parse_p
6499 (parser))
6500 {
6501 error_at (token->location,
6502 "reference to %qD is ambiguous",
6503 token->u.value);
6504 print_candidates (ambiguous_decls);
6505 }
6506 decl = error_mark_node;
6507 }
6508 else
6509 {
6510 if (cxx_dialect != cxx98)
6511 cp_parser_name_lookup_error
6512 (parser, token->u.value, decl, NLE_NOT_CXX98,
6513 token->location);
6514 else
6515 cp_parser_name_lookup_error
6516 (parser, token->u.value, decl, NLE_CXX98,
6517 token->location);
6518 }
6519 }
6520 parser->scope = error_mark_node;
6521 error_p = true;
6522 /* Treat this as a successful nested-name-specifier
6523 due to:
6524
6525 [basic.lookup.qual]
6526
6527 If the name found is not a class-name (clause
6528 _class_) or namespace-name (_namespace.def_), the
6529 program is ill-formed. */
6530 success = true;
6531 }
6532 cp_lexer_consume_token (parser->lexer);
6533 }
6534 break;
6535 }
6536 /* We've found one valid nested-name-specifier. */
6537 success = true;
6538 /* Name lookup always gives us a DECL. */
6539 if (TREE_CODE (new_scope) == TYPE_DECL)
6540 new_scope = TREE_TYPE (new_scope);
6541 /* Uses of "template" must be followed by actual templates. */
6542 if (template_keyword_p
6543 && !(CLASS_TYPE_P (new_scope)
6544 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6545 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6546 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6547 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6548 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6549 == TEMPLATE_ID_EXPR)))
6550 permerror (input_location, TYPE_P (new_scope)
6551 ? G_("%qT is not a template")
6552 : G_("%qD is not a template"),
6553 new_scope);
6554 /* If it is a class scope, try to complete it; we are about to
6555 be looking up names inside the class. */
6556 if (TYPE_P (new_scope)
6557 /* Since checking types for dependency can be expensive,
6558 avoid doing it if the type is already complete. */
6559 && !COMPLETE_TYPE_P (new_scope)
6560 /* Do not try to complete dependent types. */
6561 && !dependent_type_p (new_scope))
6562 {
6563 new_scope = complete_type (new_scope);
6564 /* If it is a typedef to current class, use the current
6565 class instead, as the typedef won't have any names inside
6566 it yet. */
6567 if (!COMPLETE_TYPE_P (new_scope)
6568 && currently_open_class (new_scope))
6569 new_scope = TYPE_MAIN_VARIANT (new_scope);
6570 }
6571 /* Make sure we look in the right scope the next time through
6572 the loop. */
6573 parser->scope = new_scope;
6574 }
6575
6576 /* If parsing tentatively, replace the sequence of tokens that makes
6577 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6578 token. That way, should we re-parse the token stream, we will
6579 not have to repeat the effort required to do the parse, nor will
6580 we issue duplicate error messages. */
6581 if (success && start)
6582 {
6583 cp_token *token;
6584
6585 token = cp_lexer_token_at (parser->lexer, start);
6586 /* Reset the contents of the START token. */
6587 token->type = CPP_NESTED_NAME_SPECIFIER;
6588 /* Retrieve any deferred checks. Do not pop this access checks yet
6589 so the memory will not be reclaimed during token replacing below. */
6590 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6591 token->u.tree_check_value->value = parser->scope;
6592 token->u.tree_check_value->checks = get_deferred_access_checks ();
6593 token->u.tree_check_value->qualifying_scope =
6594 parser->qualifying_scope;
6595 token->keyword = RID_MAX;
6596
6597 /* Purge all subsequent tokens. */
6598 cp_lexer_purge_tokens_after (parser->lexer, start);
6599 }
6600
6601 if (start)
6602 pop_to_parent_deferring_access_checks ();
6603
6604 return success ? parser->scope : NULL_TREE;
6605 }
6606
6607 /* Parse a nested-name-specifier. See
6608 cp_parser_nested_name_specifier_opt for details. This function
6609 behaves identically, except that it will an issue an error if no
6610 nested-name-specifier is present. */
6611
6612 static tree
6613 cp_parser_nested_name_specifier (cp_parser *parser,
6614 bool typename_keyword_p,
6615 bool check_dependency_p,
6616 bool type_p,
6617 bool is_declaration)
6618 {
6619 tree scope;
6620
6621 /* Look for the nested-name-specifier. */
6622 scope = cp_parser_nested_name_specifier_opt (parser,
6623 typename_keyword_p,
6624 check_dependency_p,
6625 type_p,
6626 is_declaration);
6627 /* If it was not present, issue an error message. */
6628 if (!scope)
6629 {
6630 cp_parser_error (parser, "expected nested-name-specifier");
6631 parser->scope = NULL_TREE;
6632 }
6633
6634 return scope;
6635 }
6636
6637 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6638 this is either a class-name or a namespace-name (which corresponds
6639 to the class-or-namespace-name production in the grammar). For
6640 C++0x, it can also be a type-name that refers to an enumeration
6641 type or a simple-template-id.
6642
6643 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6644 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6645 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6646 TYPE_P is TRUE iff the next name should be taken as a class-name,
6647 even the same name is declared to be another entity in the same
6648 scope.
6649
6650 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6651 specified by the class-or-namespace-name. If neither is found the
6652 ERROR_MARK_NODE is returned. */
6653
6654 static tree
6655 cp_parser_qualifying_entity (cp_parser *parser,
6656 bool typename_keyword_p,
6657 bool template_keyword_p,
6658 bool check_dependency_p,
6659 bool type_p,
6660 bool is_declaration)
6661 {
6662 tree saved_scope;
6663 tree saved_qualifying_scope;
6664 tree saved_object_scope;
6665 tree scope;
6666 bool only_class_p;
6667 bool successful_parse_p;
6668
6669 /* DR 743: decltype can appear in a nested-name-specifier. */
6670 if (cp_lexer_next_token_is_decltype (parser->lexer))
6671 {
6672 scope = cp_parser_decltype (parser);
6673 if (TREE_CODE (scope) != ENUMERAL_TYPE
6674 && !MAYBE_CLASS_TYPE_P (scope))
6675 {
6676 cp_parser_simulate_error (parser);
6677 return error_mark_node;
6678 }
6679 if (TYPE_NAME (scope))
6680 scope = TYPE_NAME (scope);
6681 return scope;
6682 }
6683
6684 /* Before we try to parse the class-name, we must save away the
6685 current PARSER->SCOPE since cp_parser_class_name will destroy
6686 it. */
6687 saved_scope = parser->scope;
6688 saved_qualifying_scope = parser->qualifying_scope;
6689 saved_object_scope = parser->object_scope;
6690 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6691 there is no need to look for a namespace-name. */
6692 only_class_p = template_keyword_p
6693 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6694 if (!only_class_p)
6695 cp_parser_parse_tentatively (parser);
6696 scope = cp_parser_class_name (parser,
6697 typename_keyword_p,
6698 template_keyword_p,
6699 type_p ? class_type : none_type,
6700 check_dependency_p,
6701 /*class_head_p=*/false,
6702 is_declaration,
6703 /*enum_ok=*/cxx_dialect > cxx98);
6704 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6705 /* If that didn't work, try for a namespace-name. */
6706 if (!only_class_p && !successful_parse_p)
6707 {
6708 /* Restore the saved scope. */
6709 parser->scope = saved_scope;
6710 parser->qualifying_scope = saved_qualifying_scope;
6711 parser->object_scope = saved_object_scope;
6712 /* If we are not looking at an identifier followed by the scope
6713 resolution operator, then this is not part of a
6714 nested-name-specifier. (Note that this function is only used
6715 to parse the components of a nested-name-specifier.) */
6716 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6717 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6718 return error_mark_node;
6719 scope = cp_parser_namespace_name (parser);
6720 }
6721
6722 return scope;
6723 }
6724
6725 /* Return true if we are looking at a compound-literal, false otherwise. */
6726
6727 static bool
6728 cp_parser_compound_literal_p (cp_parser *parser)
6729 {
6730 cp_lexer_save_tokens (parser->lexer);
6731
6732 /* Skip tokens until the next token is a closing parenthesis.
6733 If we find the closing `)', and the next token is a `{', then
6734 we are looking at a compound-literal. */
6735 bool compound_literal_p
6736 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6737 /*consume_paren=*/true)
6738 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6739
6740 /* Roll back the tokens we skipped. */
6741 cp_lexer_rollback_tokens (parser->lexer);
6742
6743 return compound_literal_p;
6744 }
6745
6746 /* Return true if EXPR is the integer constant zero or a complex constant
6747 of zero, without any folding, but ignoring location wrappers. */
6748
6749 bool
6750 literal_integer_zerop (const_tree expr)
6751 {
6752 return (location_wrapper_p (expr)
6753 && integer_zerop (TREE_OPERAND (expr, 0)));
6754 }
6755
6756 /* Parse a postfix-expression.
6757
6758 postfix-expression:
6759 primary-expression
6760 postfix-expression [ expression ]
6761 postfix-expression ( expression-list [opt] )
6762 simple-type-specifier ( expression-list [opt] )
6763 typename :: [opt] nested-name-specifier identifier
6764 ( expression-list [opt] )
6765 typename :: [opt] nested-name-specifier template [opt] template-id
6766 ( expression-list [opt] )
6767 postfix-expression . template [opt] id-expression
6768 postfix-expression -> template [opt] id-expression
6769 postfix-expression . pseudo-destructor-name
6770 postfix-expression -> pseudo-destructor-name
6771 postfix-expression ++
6772 postfix-expression --
6773 dynamic_cast < type-id > ( expression )
6774 static_cast < type-id > ( expression )
6775 reinterpret_cast < type-id > ( expression )
6776 const_cast < type-id > ( expression )
6777 typeid ( expression )
6778 typeid ( type-id )
6779
6780 GNU Extension:
6781
6782 postfix-expression:
6783 ( type-id ) { initializer-list , [opt] }
6784
6785 This extension is a GNU version of the C99 compound-literal
6786 construct. (The C99 grammar uses `type-name' instead of `type-id',
6787 but they are essentially the same concept.)
6788
6789 If ADDRESS_P is true, the postfix expression is the operand of the
6790 `&' operator. CAST_P is true if this expression is the target of a
6791 cast.
6792
6793 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6794 class member access expressions [expr.ref].
6795
6796 Returns a representation of the expression. */
6797
6798 static cp_expr
6799 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6800 bool member_access_only_p, bool decltype_p,
6801 cp_id_kind * pidk_return)
6802 {
6803 cp_token *token;
6804 location_t loc;
6805 enum rid keyword;
6806 cp_id_kind idk = CP_ID_KIND_NONE;
6807 cp_expr postfix_expression = NULL_TREE;
6808 bool is_member_access = false;
6809
6810 /* Peek at the next token. */
6811 token = cp_lexer_peek_token (parser->lexer);
6812 loc = token->location;
6813 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6814
6815 /* Some of the productions are determined by keywords. */
6816 keyword = token->keyword;
6817 switch (keyword)
6818 {
6819 case RID_DYNCAST:
6820 case RID_STATCAST:
6821 case RID_REINTCAST:
6822 case RID_CONSTCAST:
6823 {
6824 tree type;
6825 cp_expr expression;
6826 const char *saved_message;
6827 bool saved_in_type_id_in_expr_p;
6828
6829 /* All of these can be handled in the same way from the point
6830 of view of parsing. Begin by consuming the token
6831 identifying the cast. */
6832 cp_lexer_consume_token (parser->lexer);
6833
6834 /* New types cannot be defined in the cast. */
6835 saved_message = parser->type_definition_forbidden_message;
6836 parser->type_definition_forbidden_message
6837 = G_("types may not be defined in casts");
6838
6839 /* Look for the opening `<'. */
6840 cp_parser_require (parser, CPP_LESS, RT_LESS);
6841 /* Parse the type to which we are casting. */
6842 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6843 parser->in_type_id_in_expr_p = true;
6844 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
6845 NULL);
6846 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6847 /* Look for the closing `>'. */
6848 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6849 /* Restore the old message. */
6850 parser->type_definition_forbidden_message = saved_message;
6851
6852 bool saved_greater_than_is_operator_p
6853 = parser->greater_than_is_operator_p;
6854 parser->greater_than_is_operator_p = true;
6855
6856 /* And the expression which is being cast. */
6857 matching_parens parens;
6858 parens.require_open (parser);
6859 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6860 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6861 RT_CLOSE_PAREN);
6862 location_t end_loc = close_paren ?
6863 close_paren->location : UNKNOWN_LOCATION;
6864
6865 parser->greater_than_is_operator_p
6866 = saved_greater_than_is_operator_p;
6867
6868 /* Only type conversions to integral or enumeration types
6869 can be used in constant-expressions. */
6870 if (!cast_valid_in_integral_constant_expression_p (type)
6871 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6872 {
6873 postfix_expression = error_mark_node;
6874 break;
6875 }
6876
6877 switch (keyword)
6878 {
6879 case RID_DYNCAST:
6880 postfix_expression
6881 = build_dynamic_cast (type, expression, tf_warning_or_error);
6882 break;
6883 case RID_STATCAST:
6884 postfix_expression
6885 = build_static_cast (type, expression, tf_warning_or_error);
6886 break;
6887 case RID_REINTCAST:
6888 postfix_expression
6889 = build_reinterpret_cast (type, expression,
6890 tf_warning_or_error);
6891 break;
6892 case RID_CONSTCAST:
6893 postfix_expression
6894 = build_const_cast (type, expression, tf_warning_or_error);
6895 break;
6896 default:
6897 gcc_unreachable ();
6898 }
6899
6900 /* Construct a location e.g. :
6901 reinterpret_cast <int *> (expr)
6902 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6903 ranging from the start of the "*_cast" token to the final closing
6904 paren, with the caret at the start. */
6905 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6906 postfix_expression.set_location (cp_cast_loc);
6907 }
6908 break;
6909
6910 case RID_TYPEID:
6911 {
6912 tree type;
6913 const char *saved_message;
6914 bool saved_in_type_id_in_expr_p;
6915
6916 /* Consume the `typeid' token. */
6917 cp_lexer_consume_token (parser->lexer);
6918 /* Look for the `(' token. */
6919 matching_parens parens;
6920 parens.require_open (parser);
6921 /* Types cannot be defined in a `typeid' expression. */
6922 saved_message = parser->type_definition_forbidden_message;
6923 parser->type_definition_forbidden_message
6924 = G_("types may not be defined in a %<typeid%> expression");
6925 /* We can't be sure yet whether we're looking at a type-id or an
6926 expression. */
6927 cp_parser_parse_tentatively (parser);
6928 /* Try a type-id first. */
6929 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6930 parser->in_type_id_in_expr_p = true;
6931 type = cp_parser_type_id (parser);
6932 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6933 /* Look for the `)' token. Otherwise, we can't be sure that
6934 we're not looking at an expression: consider `typeid (int
6935 (3))', for example. */
6936 cp_token *close_paren = parens.require_close (parser);
6937 /* If all went well, simply lookup the type-id. */
6938 if (cp_parser_parse_definitely (parser))
6939 postfix_expression = get_typeid (type, tf_warning_or_error);
6940 /* Otherwise, fall back to the expression variant. */
6941 else
6942 {
6943 tree expression;
6944
6945 /* Look for an expression. */
6946 expression = cp_parser_expression (parser, & idk);
6947 /* Compute its typeid. */
6948 postfix_expression = build_typeid (expression, tf_warning_or_error);
6949 /* Look for the `)' token. */
6950 close_paren = parens.require_close (parser);
6951 }
6952 /* Restore the saved message. */
6953 parser->type_definition_forbidden_message = saved_message;
6954 /* `typeid' may not appear in an integral constant expression. */
6955 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6956 postfix_expression = error_mark_node;
6957
6958 /* Construct a location e.g. :
6959 typeid (expr)
6960 ^~~~~~~~~~~~~
6961 ranging from the start of the "typeid" token to the final closing
6962 paren, with the caret at the start. */
6963 if (close_paren)
6964 {
6965 location_t typeid_loc
6966 = make_location (start_loc, start_loc, close_paren->location);
6967 postfix_expression.set_location (typeid_loc);
6968 postfix_expression.maybe_add_location_wrapper ();
6969 }
6970 }
6971 break;
6972
6973 case RID_TYPENAME:
6974 {
6975 tree type;
6976 /* The syntax permitted here is the same permitted for an
6977 elaborated-type-specifier. */
6978 ++parser->prevent_constrained_type_specifiers;
6979 type = cp_parser_elaborated_type_specifier (parser,
6980 /*is_friend=*/false,
6981 /*is_declaration=*/false);
6982 --parser->prevent_constrained_type_specifiers;
6983 postfix_expression = cp_parser_functional_cast (parser, type);
6984 }
6985 break;
6986
6987 case RID_ADDRESSOF:
6988 case RID_BUILTIN_SHUFFLE:
6989 case RID_BUILTIN_LAUNDER:
6990 {
6991 vec<tree, va_gc> *vec;
6992 unsigned int i;
6993 tree p;
6994
6995 cp_lexer_consume_token (parser->lexer);
6996 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6997 /*cast_p=*/false, /*allow_expansion_p=*/true,
6998 /*non_constant_p=*/NULL);
6999 if (vec == NULL)
7000 {
7001 postfix_expression = error_mark_node;
7002 break;
7003 }
7004
7005 FOR_EACH_VEC_ELT (*vec, i, p)
7006 mark_exp_read (p);
7007
7008 switch (keyword)
7009 {
7010 case RID_ADDRESSOF:
7011 if (vec->length () == 1)
7012 postfix_expression
7013 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7014 else
7015 {
7016 error_at (loc, "wrong number of arguments to "
7017 "%<__builtin_addressof%>");
7018 postfix_expression = error_mark_node;
7019 }
7020 break;
7021
7022 case RID_BUILTIN_LAUNDER:
7023 if (vec->length () == 1)
7024 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7025 tf_warning_or_error);
7026 else
7027 {
7028 error_at (loc, "wrong number of arguments to "
7029 "%<__builtin_launder%>");
7030 postfix_expression = error_mark_node;
7031 }
7032 break;
7033
7034 case RID_BUILTIN_SHUFFLE:
7035 if (vec->length () == 2)
7036 postfix_expression
7037 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7038 (*vec)[1], tf_warning_or_error);
7039 else if (vec->length () == 3)
7040 postfix_expression
7041 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7042 (*vec)[2], tf_warning_or_error);
7043 else
7044 {
7045 error_at (loc, "wrong number of arguments to "
7046 "%<__builtin_shuffle%>");
7047 postfix_expression = error_mark_node;
7048 }
7049 break;
7050
7051 default:
7052 gcc_unreachable ();
7053 }
7054 break;
7055 }
7056
7057 case RID_BUILTIN_CONVERTVECTOR:
7058 {
7059 tree expression;
7060 tree type;
7061 /* Consume the `__builtin_convertvector' token. */
7062 cp_lexer_consume_token (parser->lexer);
7063 /* Look for the opening `('. */
7064 matching_parens parens;
7065 parens.require_open (parser);
7066 /* Now, parse the assignment-expression. */
7067 expression = cp_parser_assignment_expression (parser);
7068 /* Look for the `,'. */
7069 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7070 location_t type_location
7071 = cp_lexer_peek_token (parser->lexer)->location;
7072 /* Parse the type-id. */
7073 {
7074 type_id_in_expr_sentinel s (parser);
7075 type = cp_parser_type_id (parser);
7076 }
7077 /* Look for the closing `)'. */
7078 parens.require_close (parser);
7079 return cp_build_vec_convert (expression, type_location, type,
7080 tf_warning_or_error);
7081 }
7082
7083 default:
7084 {
7085 tree type;
7086
7087 /* If the next thing is a simple-type-specifier, we may be
7088 looking at a functional cast. We could also be looking at
7089 an id-expression. So, we try the functional cast, and if
7090 that doesn't work we fall back to the primary-expression. */
7091 cp_parser_parse_tentatively (parser);
7092 /* Look for the simple-type-specifier. */
7093 ++parser->prevent_constrained_type_specifiers;
7094 type = cp_parser_simple_type_specifier (parser,
7095 /*decl_specs=*/NULL,
7096 CP_PARSER_FLAGS_NONE);
7097 --parser->prevent_constrained_type_specifiers;
7098 /* Parse the cast itself. */
7099 if (!cp_parser_error_occurred (parser))
7100 postfix_expression
7101 = cp_parser_functional_cast (parser, type);
7102 /* If that worked, we're done. */
7103 if (cp_parser_parse_definitely (parser))
7104 break;
7105
7106 /* If the functional-cast didn't work out, try a
7107 compound-literal. */
7108 if (cp_parser_allow_gnu_extensions_p (parser)
7109 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7110 {
7111 cp_expr initializer = NULL_TREE;
7112
7113 cp_parser_parse_tentatively (parser);
7114
7115 matching_parens parens;
7116 parens.consume_open (parser);
7117
7118 /* Avoid calling cp_parser_type_id pointlessly, see comment
7119 in cp_parser_cast_expression about c++/29234. */
7120 if (!cp_parser_compound_literal_p (parser))
7121 cp_parser_simulate_error (parser);
7122 else
7123 {
7124 /* Parse the type. */
7125 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7126 parser->in_type_id_in_expr_p = true;
7127 type = cp_parser_type_id (parser);
7128 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7129 parens.require_close (parser);
7130 }
7131
7132 /* If things aren't going well, there's no need to
7133 keep going. */
7134 if (!cp_parser_error_occurred (parser))
7135 {
7136 bool non_constant_p;
7137 /* Parse the brace-enclosed initializer list. */
7138 initializer = cp_parser_braced_list (parser,
7139 &non_constant_p);
7140 }
7141 /* If that worked, we're definitely looking at a
7142 compound-literal expression. */
7143 if (cp_parser_parse_definitely (parser))
7144 {
7145 /* Warn the user that a compound literal is not
7146 allowed in standard C++. */
7147 pedwarn (input_location, OPT_Wpedantic,
7148 "ISO C++ forbids compound-literals");
7149 /* For simplicity, we disallow compound literals in
7150 constant-expressions. We could
7151 allow compound literals of integer type, whose
7152 initializer was a constant, in constant
7153 expressions. Permitting that usage, as a further
7154 extension, would not change the meaning of any
7155 currently accepted programs. (Of course, as
7156 compound literals are not part of ISO C++, the
7157 standard has nothing to say.) */
7158 if (cp_parser_non_integral_constant_expression (parser,
7159 NIC_NCC))
7160 {
7161 postfix_expression = error_mark_node;
7162 break;
7163 }
7164 /* Form the representation of the compound-literal. */
7165 postfix_expression
7166 = finish_compound_literal (type, initializer,
7167 tf_warning_or_error, fcl_c99);
7168 postfix_expression.set_location (initializer.get_location ());
7169 break;
7170 }
7171 }
7172
7173 /* It must be a primary-expression. */
7174 postfix_expression
7175 = cp_parser_primary_expression (parser, address_p, cast_p,
7176 /*template_arg_p=*/false,
7177 decltype_p,
7178 &idk);
7179 }
7180 break;
7181 }
7182
7183 /* Note that we don't need to worry about calling build_cplus_new on a
7184 class-valued CALL_EXPR in decltype when it isn't the end of the
7185 postfix-expression; unary_complex_lvalue will take care of that for
7186 all these cases. */
7187
7188 /* Keep looping until the postfix-expression is complete. */
7189 while (true)
7190 {
7191 if (idk == CP_ID_KIND_UNQUALIFIED
7192 && identifier_p (postfix_expression)
7193 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7194 /* It is not a Koenig lookup function call. */
7195 postfix_expression
7196 = unqualified_name_lookup_error (postfix_expression);
7197
7198 /* Peek at the next token. */
7199 token = cp_lexer_peek_token (parser->lexer);
7200
7201 switch (token->type)
7202 {
7203 case CPP_OPEN_SQUARE:
7204 if (cp_next_tokens_can_be_std_attribute_p (parser))
7205 {
7206 cp_parser_error (parser,
7207 "two consecutive %<[%> shall "
7208 "only introduce an attribute");
7209 return error_mark_node;
7210 }
7211 postfix_expression
7212 = cp_parser_postfix_open_square_expression (parser,
7213 postfix_expression,
7214 false,
7215 decltype_p);
7216 postfix_expression.set_range (start_loc,
7217 postfix_expression.get_location ());
7218
7219 idk = CP_ID_KIND_NONE;
7220 is_member_access = false;
7221 break;
7222
7223 case CPP_OPEN_PAREN:
7224 /* postfix-expression ( expression-list [opt] ) */
7225 {
7226 bool koenig_p;
7227 bool is_builtin_constant_p;
7228 bool saved_integral_constant_expression_p = false;
7229 bool saved_non_integral_constant_expression_p = false;
7230 tsubst_flags_t complain = complain_flags (decltype_p);
7231 vec<tree, va_gc> *args;
7232 location_t close_paren_loc = UNKNOWN_LOCATION;
7233
7234 is_member_access = false;
7235
7236 tree stripped_expression
7237 = tree_strip_any_location_wrapper (postfix_expression);
7238 is_builtin_constant_p
7239 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7240 if (is_builtin_constant_p)
7241 {
7242 /* The whole point of __builtin_constant_p is to allow
7243 non-constant expressions to appear as arguments. */
7244 saved_integral_constant_expression_p
7245 = parser->integral_constant_expression_p;
7246 saved_non_integral_constant_expression_p
7247 = parser->non_integral_constant_expression_p;
7248 parser->integral_constant_expression_p = false;
7249 }
7250 args = (cp_parser_parenthesized_expression_list
7251 (parser, non_attr,
7252 /*cast_p=*/false, /*allow_expansion_p=*/true,
7253 /*non_constant_p=*/NULL,
7254 /*close_paren_loc=*/&close_paren_loc,
7255 /*wrap_locations_p=*/true));
7256 if (is_builtin_constant_p)
7257 {
7258 parser->integral_constant_expression_p
7259 = saved_integral_constant_expression_p;
7260 parser->non_integral_constant_expression_p
7261 = saved_non_integral_constant_expression_p;
7262 }
7263
7264 if (args == NULL)
7265 {
7266 postfix_expression = error_mark_node;
7267 break;
7268 }
7269
7270 /* Function calls are not permitted in
7271 constant-expressions. */
7272 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7273 && cp_parser_non_integral_constant_expression (parser,
7274 NIC_FUNC_CALL))
7275 {
7276 postfix_expression = error_mark_node;
7277 release_tree_vector (args);
7278 break;
7279 }
7280
7281 koenig_p = false;
7282 if (idk == CP_ID_KIND_UNQUALIFIED
7283 || idk == CP_ID_KIND_TEMPLATE_ID)
7284 {
7285 if (identifier_p (postfix_expression)
7286 /* In C++2A, we may need to perform ADL for a template
7287 name. */
7288 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7289 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7290 {
7291 if (!args->is_empty ())
7292 {
7293 koenig_p = true;
7294 if (!any_type_dependent_arguments_p (args))
7295 postfix_expression
7296 = perform_koenig_lookup (postfix_expression, args,
7297 complain);
7298 }
7299 else
7300 postfix_expression
7301 = unqualified_fn_lookup_error (postfix_expression);
7302 }
7303 /* We do not perform argument-dependent lookup if
7304 normal lookup finds a non-function, in accordance
7305 with the expected resolution of DR 218. */
7306 else if (!args->is_empty ()
7307 && is_overloaded_fn (postfix_expression))
7308 {
7309 /* We only need to look at the first function,
7310 because all the fns share the attribute we're
7311 concerned with (all member fns or all local
7312 fns). */
7313 tree fn = get_first_fn (postfix_expression);
7314 fn = STRIP_TEMPLATE (fn);
7315
7316 /* Do not do argument dependent lookup if regular
7317 lookup finds a member function or a block-scope
7318 function declaration. [basic.lookup.argdep]/3 */
7319 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7320 || DECL_FUNCTION_MEMBER_P (fn)
7321 || DECL_LOCAL_FUNCTION_P (fn)))
7322 {
7323 koenig_p = true;
7324 if (!any_type_dependent_arguments_p (args))
7325 postfix_expression
7326 = perform_koenig_lookup (postfix_expression, args,
7327 complain);
7328 }
7329 }
7330 }
7331
7332 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7333 {
7334 tree instance = TREE_OPERAND (postfix_expression, 0);
7335 tree fn = TREE_OPERAND (postfix_expression, 1);
7336
7337 if (processing_template_decl
7338 && (type_dependent_object_expression_p (instance)
7339 || (!BASELINK_P (fn)
7340 && TREE_CODE (fn) != FIELD_DECL)
7341 || type_dependent_expression_p (fn)
7342 || any_type_dependent_arguments_p (args)))
7343 {
7344 maybe_generic_this_capture (instance, fn);
7345 postfix_expression
7346 = build_min_nt_call_vec (postfix_expression, args);
7347 release_tree_vector (args);
7348 break;
7349 }
7350
7351 if (BASELINK_P (fn))
7352 {
7353 postfix_expression
7354 = (build_new_method_call
7355 (instance, fn, &args, NULL_TREE,
7356 (idk == CP_ID_KIND_QUALIFIED
7357 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7358 : LOOKUP_NORMAL),
7359 /*fn_p=*/NULL,
7360 complain));
7361 }
7362 else
7363 postfix_expression
7364 = finish_call_expr (postfix_expression, &args,
7365 /*disallow_virtual=*/false,
7366 /*koenig_p=*/false,
7367 complain);
7368 }
7369 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7370 || TREE_CODE (postfix_expression) == MEMBER_REF
7371 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7372 postfix_expression = (build_offset_ref_call_from_tree
7373 (postfix_expression, &args,
7374 complain));
7375 else if (idk == CP_ID_KIND_QUALIFIED)
7376 /* A call to a static class member, or a namespace-scope
7377 function. */
7378 postfix_expression
7379 = finish_call_expr (postfix_expression, &args,
7380 /*disallow_virtual=*/true,
7381 koenig_p,
7382 complain);
7383 else
7384 /* All other function calls. */
7385 postfix_expression
7386 = finish_call_expr (postfix_expression, &args,
7387 /*disallow_virtual=*/false,
7388 koenig_p,
7389 complain);
7390
7391 if (close_paren_loc != UNKNOWN_LOCATION)
7392 {
7393 location_t combined_loc = make_location (token->location,
7394 start_loc,
7395 close_paren_loc);
7396 postfix_expression.set_location (combined_loc);
7397 }
7398
7399 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7400 idk = CP_ID_KIND_NONE;
7401
7402 release_tree_vector (args);
7403 }
7404 break;
7405
7406 case CPP_DOT:
7407 case CPP_DEREF:
7408 /* postfix-expression . template [opt] id-expression
7409 postfix-expression . pseudo-destructor-name
7410 postfix-expression -> template [opt] id-expression
7411 postfix-expression -> pseudo-destructor-name */
7412
7413 /* Consume the `.' or `->' operator. */
7414 cp_lexer_consume_token (parser->lexer);
7415
7416 postfix_expression
7417 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7418 postfix_expression,
7419 false, &idk, loc);
7420
7421 is_member_access = true;
7422 break;
7423
7424 case CPP_PLUS_PLUS:
7425 /* postfix-expression ++ */
7426 /* Consume the `++' token. */
7427 cp_lexer_consume_token (parser->lexer);
7428 /* Generate a representation for the complete expression. */
7429 postfix_expression
7430 = finish_increment_expr (postfix_expression,
7431 POSTINCREMENT_EXPR);
7432 /* Increments may not appear in constant-expressions. */
7433 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7434 postfix_expression = error_mark_node;
7435 idk = CP_ID_KIND_NONE;
7436 is_member_access = false;
7437 break;
7438
7439 case CPP_MINUS_MINUS:
7440 /* postfix-expression -- */
7441 /* Consume the `--' token. */
7442 cp_lexer_consume_token (parser->lexer);
7443 /* Generate a representation for the complete expression. */
7444 postfix_expression
7445 = finish_increment_expr (postfix_expression,
7446 POSTDECREMENT_EXPR);
7447 /* Decrements may not appear in constant-expressions. */
7448 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7449 postfix_expression = error_mark_node;
7450 idk = CP_ID_KIND_NONE;
7451 is_member_access = false;
7452 break;
7453
7454 default:
7455 if (pidk_return != NULL)
7456 * pidk_return = idk;
7457 if (member_access_only_p)
7458 return is_member_access
7459 ? postfix_expression
7460 : cp_expr (error_mark_node);
7461 else
7462 return postfix_expression;
7463 }
7464 }
7465
7466 /* We should never get here. */
7467 gcc_unreachable ();
7468 return error_mark_node;
7469 }
7470
7471 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7472 by cp_parser_builtin_offsetof. We're looking for
7473
7474 postfix-expression [ expression ]
7475 postfix-expression [ braced-init-list ] (C++11)
7476
7477 FOR_OFFSETOF is set if we're being called in that context, which
7478 changes how we deal with integer constant expressions. */
7479
7480 static tree
7481 cp_parser_postfix_open_square_expression (cp_parser *parser,
7482 tree postfix_expression,
7483 bool for_offsetof,
7484 bool decltype_p)
7485 {
7486 tree index = NULL_TREE;
7487 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7488 bool saved_greater_than_is_operator_p;
7489
7490 /* Consume the `[' token. */
7491 cp_lexer_consume_token (parser->lexer);
7492
7493 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7494 parser->greater_than_is_operator_p = true;
7495
7496 /* Parse the index expression. */
7497 /* ??? For offsetof, there is a question of what to allow here. If
7498 offsetof is not being used in an integral constant expression context,
7499 then we *could* get the right answer by computing the value at runtime.
7500 If we are in an integral constant expression context, then we might
7501 could accept any constant expression; hard to say without analysis.
7502 Rather than open the barn door too wide right away, allow only integer
7503 constant expressions here. */
7504 if (for_offsetof)
7505 index = cp_parser_constant_expression (parser);
7506 else
7507 {
7508 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7509 {
7510 bool expr_nonconst_p;
7511 cp_lexer_set_source_position (parser->lexer);
7512 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7513 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7514 }
7515 else
7516 index = cp_parser_expression (parser);
7517 }
7518
7519 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7520
7521 /* Look for the closing `]'. */
7522 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7523
7524 /* Build the ARRAY_REF. */
7525 postfix_expression = grok_array_decl (loc, postfix_expression,
7526 index, decltype_p);
7527
7528 /* When not doing offsetof, array references are not permitted in
7529 constant-expressions. */
7530 if (!for_offsetof
7531 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7532 postfix_expression = error_mark_node;
7533
7534 return postfix_expression;
7535 }
7536
7537 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7538 dereference of incomplete type, returns true if error_mark_node should
7539 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7540 and *DEPENDENT_P. */
7541
7542 bool
7543 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7544 bool *dependent_p)
7545 {
7546 /* In a template, be permissive by treating an object expression
7547 of incomplete type as dependent (after a pedwarn). */
7548 diagnostic_t kind = (processing_template_decl
7549 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7550
7551 switch (TREE_CODE (*postfix_expression))
7552 {
7553 case CAST_EXPR:
7554 case REINTERPRET_CAST_EXPR:
7555 case CONST_CAST_EXPR:
7556 case STATIC_CAST_EXPR:
7557 case DYNAMIC_CAST_EXPR:
7558 case IMPLICIT_CONV_EXPR:
7559 case VIEW_CONVERT_EXPR:
7560 case NON_LVALUE_EXPR:
7561 kind = DK_ERROR;
7562 break;
7563 case OVERLOAD:
7564 /* Don't emit any diagnostic for OVERLOADs. */
7565 kind = DK_IGNORED;
7566 break;
7567 default:
7568 /* Avoid clobbering e.g. DECLs. */
7569 if (!EXPR_P (*postfix_expression))
7570 kind = DK_ERROR;
7571 break;
7572 }
7573
7574 if (kind == DK_IGNORED)
7575 return false;
7576
7577 location_t exploc = location_of (*postfix_expression);
7578 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7579 if (!MAYBE_CLASS_TYPE_P (*scope))
7580 return true;
7581 if (kind == DK_ERROR)
7582 *scope = *postfix_expression = error_mark_node;
7583 else if (processing_template_decl)
7584 {
7585 *dependent_p = true;
7586 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7587 }
7588 return false;
7589 }
7590
7591 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7592 by cp_parser_builtin_offsetof. We're looking for
7593
7594 postfix-expression . template [opt] id-expression
7595 postfix-expression . pseudo-destructor-name
7596 postfix-expression -> template [opt] id-expression
7597 postfix-expression -> pseudo-destructor-name
7598
7599 FOR_OFFSETOF is set if we're being called in that context. That sorta
7600 limits what of the above we'll actually accept, but nevermind.
7601 TOKEN_TYPE is the "." or "->" token, which will already have been
7602 removed from the stream. */
7603
7604 static tree
7605 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7606 enum cpp_ttype token_type,
7607 cp_expr postfix_expression,
7608 bool for_offsetof, cp_id_kind *idk,
7609 location_t location)
7610 {
7611 tree name;
7612 bool dependent_p;
7613 bool pseudo_destructor_p;
7614 tree scope = NULL_TREE;
7615 location_t start_loc = postfix_expression.get_start ();
7616
7617 /* If this is a `->' operator, dereference the pointer. */
7618 if (token_type == CPP_DEREF)
7619 postfix_expression = build_x_arrow (location, postfix_expression,
7620 tf_warning_or_error);
7621 /* Check to see whether or not the expression is type-dependent and
7622 not the current instantiation. */
7623 dependent_p = type_dependent_object_expression_p (postfix_expression);
7624 /* The identifier following the `->' or `.' is not qualified. */
7625 parser->scope = NULL_TREE;
7626 parser->qualifying_scope = NULL_TREE;
7627 parser->object_scope = NULL_TREE;
7628 *idk = CP_ID_KIND_NONE;
7629
7630 /* Enter the scope corresponding to the type of the object
7631 given by the POSTFIX_EXPRESSION. */
7632 if (!dependent_p)
7633 {
7634 scope = TREE_TYPE (postfix_expression);
7635 /* According to the standard, no expression should ever have
7636 reference type. Unfortunately, we do not currently match
7637 the standard in this respect in that our internal representation
7638 of an expression may have reference type even when the standard
7639 says it does not. Therefore, we have to manually obtain the
7640 underlying type here. */
7641 scope = non_reference (scope);
7642 /* The type of the POSTFIX_EXPRESSION must be complete. */
7643 /* Unlike the object expression in other contexts, *this is not
7644 required to be of complete type for purposes of class member
7645 access (5.2.5) outside the member function body. */
7646 if (postfix_expression != current_class_ref
7647 && scope != error_mark_node
7648 && !currently_open_class (scope))
7649 {
7650 scope = complete_type (scope);
7651 if (!COMPLETE_TYPE_P (scope)
7652 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7653 &dependent_p))
7654 return error_mark_node;
7655 }
7656
7657 if (!dependent_p)
7658 {
7659 /* Let the name lookup machinery know that we are processing a
7660 class member access expression. */
7661 parser->context->object_type = scope;
7662 /* If something went wrong, we want to be able to discern that case,
7663 as opposed to the case where there was no SCOPE due to the type
7664 of expression being dependent. */
7665 if (!scope)
7666 scope = error_mark_node;
7667 /* If the SCOPE was erroneous, make the various semantic analysis
7668 functions exit quickly -- and without issuing additional error
7669 messages. */
7670 if (scope == error_mark_node)
7671 postfix_expression = error_mark_node;
7672 }
7673 }
7674
7675 if (dependent_p)
7676 /* Tell cp_parser_lookup_name that there was an object, even though it's
7677 type-dependent. */
7678 parser->context->object_type = unknown_type_node;
7679
7680 /* Assume this expression is not a pseudo-destructor access. */
7681 pseudo_destructor_p = false;
7682
7683 /* If the SCOPE is a scalar type, then, if this is a valid program,
7684 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7685 is type dependent, it can be pseudo-destructor-name or something else.
7686 Try to parse it as pseudo-destructor-name first. */
7687 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7688 {
7689 tree s;
7690 tree type;
7691
7692 cp_parser_parse_tentatively (parser);
7693 /* Parse the pseudo-destructor-name. */
7694 s = NULL_TREE;
7695 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7696 &s, &type);
7697 if (dependent_p
7698 && (cp_parser_error_occurred (parser)
7699 || !SCALAR_TYPE_P (type)))
7700 cp_parser_abort_tentative_parse (parser);
7701 else if (cp_parser_parse_definitely (parser))
7702 {
7703 pseudo_destructor_p = true;
7704 postfix_expression
7705 = finish_pseudo_destructor_expr (postfix_expression,
7706 s, type, location);
7707 }
7708 }
7709
7710 if (!pseudo_destructor_p)
7711 {
7712 /* If the SCOPE is not a scalar type, we are looking at an
7713 ordinary class member access expression, rather than a
7714 pseudo-destructor-name. */
7715 bool template_p;
7716 cp_token *token = cp_lexer_peek_token (parser->lexer);
7717 /* Parse the id-expression. */
7718 name = (cp_parser_id_expression
7719 (parser,
7720 cp_parser_optional_template_keyword (parser),
7721 /*check_dependency_p=*/true,
7722 &template_p,
7723 /*declarator_p=*/false,
7724 /*optional_p=*/false));
7725 /* In general, build a SCOPE_REF if the member name is qualified.
7726 However, if the name was not dependent and has already been
7727 resolved; there is no need to build the SCOPE_REF. For example;
7728
7729 struct X { void f(); };
7730 template <typename T> void f(T* t) { t->X::f(); }
7731
7732 Even though "t" is dependent, "X::f" is not and has been resolved
7733 to a BASELINK; there is no need to include scope information. */
7734
7735 /* But we do need to remember that there was an explicit scope for
7736 virtual function calls. */
7737 if (parser->scope)
7738 *idk = CP_ID_KIND_QUALIFIED;
7739
7740 /* If the name is a template-id that names a type, we will get a
7741 TYPE_DECL here. That is invalid code. */
7742 if (TREE_CODE (name) == TYPE_DECL)
7743 {
7744 error_at (token->location, "invalid use of %qD", name);
7745 postfix_expression = error_mark_node;
7746 }
7747 else
7748 {
7749 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7750 {
7751 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7752 {
7753 error_at (token->location, "%<%D::%D%> is not a class member",
7754 parser->scope, name);
7755 postfix_expression = error_mark_node;
7756 }
7757 else
7758 name = build_qualified_name (/*type=*/NULL_TREE,
7759 parser->scope,
7760 name,
7761 template_p);
7762 parser->scope = NULL_TREE;
7763 parser->qualifying_scope = NULL_TREE;
7764 parser->object_scope = NULL_TREE;
7765 }
7766 if (parser->scope && name && BASELINK_P (name))
7767 adjust_result_of_qualified_name_lookup
7768 (name, parser->scope, scope);
7769 postfix_expression
7770 = finish_class_member_access_expr (postfix_expression, name,
7771 template_p,
7772 tf_warning_or_error);
7773 /* Build a location e.g.:
7774 ptr->access_expr
7775 ~~~^~~~~~~~~~~~~
7776 where the caret is at the deref token, ranging from
7777 the start of postfix_expression to the end of the access expr. */
7778 location_t end_loc
7779 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7780 location_t combined_loc
7781 = make_location (input_location, start_loc, end_loc);
7782 protected_set_expr_location (postfix_expression, combined_loc);
7783 }
7784 }
7785
7786 /* We no longer need to look up names in the scope of the object on
7787 the left-hand side of the `.' or `->' operator. */
7788 parser->context->object_type = NULL_TREE;
7789
7790 /* Outside of offsetof, these operators may not appear in
7791 constant-expressions. */
7792 if (!for_offsetof
7793 && (cp_parser_non_integral_constant_expression
7794 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7795 postfix_expression = error_mark_node;
7796
7797 return postfix_expression;
7798 }
7799
7800 /* Parse a parenthesized expression-list.
7801
7802 expression-list:
7803 assignment-expression
7804 expression-list, assignment-expression
7805
7806 attribute-list:
7807 expression-list
7808 identifier
7809 identifier, expression-list
7810
7811 CAST_P is true if this expression is the target of a cast.
7812
7813 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7814 argument pack.
7815
7816 WRAP_LOCATIONS_P is true if expressions within this list for which
7817 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7818 their source locations.
7819
7820 Returns a vector of trees. Each element is a representation of an
7821 assignment-expression. NULL is returned if the ( and or ) are
7822 missing. An empty, but allocated, vector is returned on no
7823 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7824 if we are parsing an attribute list for an attribute that wants a
7825 plain identifier argument, normal_attr for an attribute that wants
7826 an expression, or non_attr if we aren't parsing an attribute list. If
7827 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7828 not all of the expressions in the list were constant.
7829 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7830 will be written to with the location of the closing parenthesis. If
7831 an error occurs, it may or may not be written to. */
7832
7833 static vec<tree, va_gc> *
7834 cp_parser_parenthesized_expression_list (cp_parser* parser,
7835 int is_attribute_list,
7836 bool cast_p,
7837 bool allow_expansion_p,
7838 bool *non_constant_p,
7839 location_t *close_paren_loc,
7840 bool wrap_locations_p)
7841 {
7842 vec<tree, va_gc> *expression_list;
7843 bool fold_expr_p = is_attribute_list != non_attr;
7844 tree identifier = NULL_TREE;
7845 bool saved_greater_than_is_operator_p;
7846
7847 /* Assume all the expressions will be constant. */
7848 if (non_constant_p)
7849 *non_constant_p = false;
7850
7851 matching_parens parens;
7852 if (!parens.require_open (parser))
7853 return NULL;
7854
7855 expression_list = make_tree_vector ();
7856
7857 /* Within a parenthesized expression, a `>' token is always
7858 the greater-than operator. */
7859 saved_greater_than_is_operator_p
7860 = parser->greater_than_is_operator_p;
7861 parser->greater_than_is_operator_p = true;
7862
7863 cp_expr expr (NULL_TREE);
7864
7865 /* Consume expressions until there are no more. */
7866 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7867 while (true)
7868 {
7869 /* At the beginning of attribute lists, check to see if the
7870 next token is an identifier. */
7871 if (is_attribute_list == id_attr
7872 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7873 {
7874 cp_token *token;
7875
7876 /* Consume the identifier. */
7877 token = cp_lexer_consume_token (parser->lexer);
7878 /* Save the identifier. */
7879 identifier = token->u.value;
7880 }
7881 else
7882 {
7883 bool expr_non_constant_p;
7884
7885 /* Parse the next assignment-expression. */
7886 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7887 {
7888 /* A braced-init-list. */
7889 cp_lexer_set_source_position (parser->lexer);
7890 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7891 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7892 if (non_constant_p && expr_non_constant_p)
7893 *non_constant_p = true;
7894 }
7895 else if (non_constant_p)
7896 {
7897 expr = (cp_parser_constant_expression
7898 (parser, /*allow_non_constant_p=*/true,
7899 &expr_non_constant_p));
7900 if (expr_non_constant_p)
7901 *non_constant_p = true;
7902 }
7903 else
7904 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7905 cast_p);
7906
7907 if (fold_expr_p)
7908 expr = instantiate_non_dependent_expr (expr);
7909
7910 /* If we have an ellipsis, then this is an expression
7911 expansion. */
7912 if (allow_expansion_p
7913 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7914 {
7915 /* Consume the `...'. */
7916 cp_lexer_consume_token (parser->lexer);
7917
7918 /* Build the argument pack. */
7919 expr = make_pack_expansion (expr);
7920 }
7921
7922 if (wrap_locations_p)
7923 expr.maybe_add_location_wrapper ();
7924
7925 /* Add it to the list. We add error_mark_node
7926 expressions to the list, so that we can still tell if
7927 the correct form for a parenthesized expression-list
7928 is found. That gives better errors. */
7929 vec_safe_push (expression_list, expr.get_value ());
7930
7931 if (expr == error_mark_node)
7932 goto skip_comma;
7933 }
7934
7935 /* After the first item, attribute lists look the same as
7936 expression lists. */
7937 is_attribute_list = non_attr;
7938
7939 get_comma:;
7940 /* If the next token isn't a `,', then we are done. */
7941 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7942 break;
7943
7944 /* Otherwise, consume the `,' and keep going. */
7945 cp_lexer_consume_token (parser->lexer);
7946 }
7947
7948 if (close_paren_loc)
7949 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7950
7951 if (!parens.require_close (parser))
7952 {
7953 int ending;
7954
7955 skip_comma:;
7956 /* We try and resync to an unnested comma, as that will give the
7957 user better diagnostics. */
7958 ending = cp_parser_skip_to_closing_parenthesis (parser,
7959 /*recovering=*/true,
7960 /*or_comma=*/true,
7961 /*consume_paren=*/true);
7962 if (ending < 0)
7963 goto get_comma;
7964 if (!ending)
7965 {
7966 parser->greater_than_is_operator_p
7967 = saved_greater_than_is_operator_p;
7968 return NULL;
7969 }
7970 }
7971
7972 parser->greater_than_is_operator_p
7973 = saved_greater_than_is_operator_p;
7974
7975 if (identifier)
7976 vec_safe_insert (expression_list, 0, identifier);
7977
7978 return expression_list;
7979 }
7980
7981 /* Parse a pseudo-destructor-name.
7982
7983 pseudo-destructor-name:
7984 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7985 :: [opt] nested-name-specifier template template-id :: ~ type-name
7986 :: [opt] nested-name-specifier [opt] ~ type-name
7987
7988 If either of the first two productions is used, sets *SCOPE to the
7989 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7990 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7991 or ERROR_MARK_NODE if the parse fails. */
7992
7993 static void
7994 cp_parser_pseudo_destructor_name (cp_parser* parser,
7995 tree object,
7996 tree* scope,
7997 tree* type)
7998 {
7999 bool nested_name_specifier_p;
8000
8001 /* Handle ~auto. */
8002 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8003 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8004 && !type_dependent_expression_p (object))
8005 {
8006 if (cxx_dialect < cxx14)
8007 pedwarn (input_location, 0,
8008 "%<~auto%> only available with "
8009 "%<-std=c++14%> or %<-std=gnu++14%>");
8010 cp_lexer_consume_token (parser->lexer);
8011 cp_lexer_consume_token (parser->lexer);
8012 *scope = NULL_TREE;
8013 *type = TREE_TYPE (object);
8014 return;
8015 }
8016
8017 /* Assume that things will not work out. */
8018 *type = error_mark_node;
8019
8020 /* Look for the optional `::' operator. */
8021 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8022 /* Look for the optional nested-name-specifier. */
8023 nested_name_specifier_p
8024 = (cp_parser_nested_name_specifier_opt (parser,
8025 /*typename_keyword_p=*/false,
8026 /*check_dependency_p=*/true,
8027 /*type_p=*/false,
8028 /*is_declaration=*/false)
8029 != NULL_TREE);
8030 /* Now, if we saw a nested-name-specifier, we might be doing the
8031 second production. */
8032 if (nested_name_specifier_p
8033 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8034 {
8035 /* Consume the `template' keyword. */
8036 cp_lexer_consume_token (parser->lexer);
8037 /* Parse the template-id. */
8038 cp_parser_template_id (parser,
8039 /*template_keyword_p=*/true,
8040 /*check_dependency_p=*/false,
8041 class_type,
8042 /*is_declaration=*/true);
8043 /* Look for the `::' token. */
8044 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8045 }
8046 /* If the next token is not a `~', then there might be some
8047 additional qualification. */
8048 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8049 {
8050 /* At this point, we're looking for "type-name :: ~". The type-name
8051 must not be a class-name, since this is a pseudo-destructor. So,
8052 it must be either an enum-name, or a typedef-name -- both of which
8053 are just identifiers. So, we peek ahead to check that the "::"
8054 and "~" tokens are present; if they are not, then we can avoid
8055 calling type_name. */
8056 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8057 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8058 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8059 {
8060 cp_parser_error (parser, "non-scalar type");
8061 return;
8062 }
8063
8064 /* Look for the type-name. */
8065 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8066 if (*scope == error_mark_node)
8067 return;
8068
8069 /* Look for the `::' token. */
8070 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8071 }
8072 else
8073 *scope = NULL_TREE;
8074
8075 /* Look for the `~'. */
8076 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8077
8078 /* Once we see the ~, this has to be a pseudo-destructor. */
8079 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8080 cp_parser_commit_to_topmost_tentative_parse (parser);
8081
8082 /* Look for the type-name again. We are not responsible for
8083 checking that it matches the first type-name. */
8084 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8085 }
8086
8087 /* Parse a unary-expression.
8088
8089 unary-expression:
8090 postfix-expression
8091 ++ cast-expression
8092 -- cast-expression
8093 unary-operator cast-expression
8094 sizeof unary-expression
8095 sizeof ( type-id )
8096 alignof ( type-id ) [C++0x]
8097 new-expression
8098 delete-expression
8099
8100 GNU Extensions:
8101
8102 unary-expression:
8103 __extension__ cast-expression
8104 __alignof__ unary-expression
8105 __alignof__ ( type-id )
8106 alignof unary-expression [C++0x]
8107 __real__ cast-expression
8108 __imag__ cast-expression
8109 && identifier
8110 sizeof ( type-id ) { initializer-list , [opt] }
8111 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8112 __alignof__ ( type-id ) { initializer-list , [opt] }
8113
8114 ADDRESS_P is true iff the unary-expression is appearing as the
8115 operand of the `&' operator. CAST_P is true if this expression is
8116 the target of a cast.
8117
8118 Returns a representation of the expression. */
8119
8120 static cp_expr
8121 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8122 bool address_p, bool cast_p, bool decltype_p)
8123 {
8124 cp_token *token;
8125 enum tree_code unary_operator;
8126
8127 /* Peek at the next token. */
8128 token = cp_lexer_peek_token (parser->lexer);
8129 /* Some keywords give away the kind of expression. */
8130 if (token->type == CPP_KEYWORD)
8131 {
8132 enum rid keyword = token->keyword;
8133
8134 switch (keyword)
8135 {
8136 case RID_ALIGNOF:
8137 case RID_SIZEOF:
8138 {
8139 tree operand, ret;
8140 enum tree_code op;
8141 location_t start_loc = token->location;
8142
8143 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8144 bool std_alignof = id_equal (token->u.value, "alignof");
8145
8146 /* Consume the token. */
8147 cp_lexer_consume_token (parser->lexer);
8148 /* Parse the operand. */
8149 operand = cp_parser_sizeof_operand (parser, keyword);
8150
8151 if (TYPE_P (operand))
8152 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8153 true);
8154 else
8155 {
8156 /* ISO C++ defines alignof only with types, not with
8157 expressions. So pedwarn if alignof is used with a non-
8158 type expression. However, __alignof__ is ok. */
8159 if (std_alignof)
8160 pedwarn (token->location, OPT_Wpedantic,
8161 "ISO C++ does not allow %<alignof%> "
8162 "with a non-type");
8163
8164 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8165 }
8166 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8167 SIZEOF_EXPR with the original operand. */
8168 if (op == SIZEOF_EXPR && ret != error_mark_node)
8169 {
8170 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8171 {
8172 if (!processing_template_decl && TYPE_P (operand))
8173 {
8174 ret = build_min (SIZEOF_EXPR, size_type_node,
8175 build1 (NOP_EXPR, operand,
8176 error_mark_node));
8177 SIZEOF_EXPR_TYPE_P (ret) = 1;
8178 }
8179 else
8180 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8181 TREE_SIDE_EFFECTS (ret) = 0;
8182 TREE_READONLY (ret) = 1;
8183 }
8184 }
8185
8186 /* Construct a location e.g. :
8187 alignof (expr)
8188 ^~~~~~~~~~~~~~
8189 with start == caret at the start of the "alignof"/"sizeof"
8190 token, with the endpoint at the final closing paren. */
8191 location_t finish_loc
8192 = cp_lexer_previous_token (parser->lexer)->location;
8193 location_t compound_loc
8194 = make_location (start_loc, start_loc, finish_loc);
8195
8196 cp_expr ret_expr (ret);
8197 ret_expr.set_location (compound_loc);
8198 ret_expr = ret_expr.maybe_add_location_wrapper ();
8199 return ret_expr;
8200 }
8201
8202 case RID_BUILTIN_HAS_ATTRIBUTE:
8203 return cp_parser_has_attribute_expression (parser);
8204
8205 case RID_NEW:
8206 return cp_parser_new_expression (parser);
8207
8208 case RID_DELETE:
8209 return cp_parser_delete_expression (parser);
8210
8211 case RID_EXTENSION:
8212 {
8213 /* The saved value of the PEDANTIC flag. */
8214 int saved_pedantic;
8215 tree expr;
8216
8217 /* Save away the PEDANTIC flag. */
8218 cp_parser_extension_opt (parser, &saved_pedantic);
8219 /* Parse the cast-expression. */
8220 expr = cp_parser_simple_cast_expression (parser);
8221 /* Restore the PEDANTIC flag. */
8222 pedantic = saved_pedantic;
8223
8224 return expr;
8225 }
8226
8227 case RID_REALPART:
8228 case RID_IMAGPART:
8229 {
8230 tree expression;
8231
8232 /* Consume the `__real__' or `__imag__' token. */
8233 cp_lexer_consume_token (parser->lexer);
8234 /* Parse the cast-expression. */
8235 expression = cp_parser_simple_cast_expression (parser);
8236 /* Create the complete representation. */
8237 return build_x_unary_op (token->location,
8238 (keyword == RID_REALPART
8239 ? REALPART_EXPR : IMAGPART_EXPR),
8240 expression,
8241 tf_warning_or_error);
8242 }
8243 break;
8244
8245 case RID_TRANSACTION_ATOMIC:
8246 case RID_TRANSACTION_RELAXED:
8247 return cp_parser_transaction_expression (parser, keyword);
8248
8249 case RID_NOEXCEPT:
8250 {
8251 tree expr;
8252 const char *saved_message;
8253 bool saved_integral_constant_expression_p;
8254 bool saved_non_integral_constant_expression_p;
8255 bool saved_greater_than_is_operator_p;
8256
8257 location_t start_loc = token->location;
8258
8259 cp_lexer_consume_token (parser->lexer);
8260 matching_parens parens;
8261 parens.require_open (parser);
8262
8263 saved_message = parser->type_definition_forbidden_message;
8264 parser->type_definition_forbidden_message
8265 = G_("types may not be defined in %<noexcept%> expressions");
8266
8267 saved_integral_constant_expression_p
8268 = parser->integral_constant_expression_p;
8269 saved_non_integral_constant_expression_p
8270 = parser->non_integral_constant_expression_p;
8271 parser->integral_constant_expression_p = false;
8272
8273 saved_greater_than_is_operator_p
8274 = parser->greater_than_is_operator_p;
8275 parser->greater_than_is_operator_p = true;
8276
8277 ++cp_unevaluated_operand;
8278 ++c_inhibit_evaluation_warnings;
8279 ++cp_noexcept_operand;
8280 expr = cp_parser_expression (parser);
8281 --cp_noexcept_operand;
8282 --c_inhibit_evaluation_warnings;
8283 --cp_unevaluated_operand;
8284
8285 parser->greater_than_is_operator_p
8286 = saved_greater_than_is_operator_p;
8287
8288 parser->integral_constant_expression_p
8289 = saved_integral_constant_expression_p;
8290 parser->non_integral_constant_expression_p
8291 = saved_non_integral_constant_expression_p;
8292
8293 parser->type_definition_forbidden_message = saved_message;
8294
8295 location_t finish_loc
8296 = cp_lexer_peek_token (parser->lexer)->location;
8297 parens.require_close (parser);
8298
8299 /* Construct a location of the form:
8300 noexcept (expr)
8301 ^~~~~~~~~~~~~~~
8302 with start == caret, finishing at the close-paren. */
8303 location_t noexcept_loc
8304 = make_location (start_loc, start_loc, finish_loc);
8305
8306 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8307 noexcept_loc);
8308 }
8309
8310 default:
8311 break;
8312 }
8313 }
8314
8315 /* Look for the `:: new' and `:: delete', which also signal the
8316 beginning of a new-expression, or delete-expression,
8317 respectively. If the next token is `::', then it might be one of
8318 these. */
8319 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8320 {
8321 enum rid keyword;
8322
8323 /* See if the token after the `::' is one of the keywords in
8324 which we're interested. */
8325 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8326 /* If it's `new', we have a new-expression. */
8327 if (keyword == RID_NEW)
8328 return cp_parser_new_expression (parser);
8329 /* Similarly, for `delete'. */
8330 else if (keyword == RID_DELETE)
8331 return cp_parser_delete_expression (parser);
8332 }
8333
8334 /* Look for a unary operator. */
8335 unary_operator = cp_parser_unary_operator (token);
8336 /* The `++' and `--' operators can be handled similarly, even though
8337 they are not technically unary-operators in the grammar. */
8338 if (unary_operator == ERROR_MARK)
8339 {
8340 if (token->type == CPP_PLUS_PLUS)
8341 unary_operator = PREINCREMENT_EXPR;
8342 else if (token->type == CPP_MINUS_MINUS)
8343 unary_operator = PREDECREMENT_EXPR;
8344 /* Handle the GNU address-of-label extension. */
8345 else if (cp_parser_allow_gnu_extensions_p (parser)
8346 && token->type == CPP_AND_AND)
8347 {
8348 tree identifier;
8349 tree expression;
8350 location_t start_loc = token->location;
8351
8352 /* Consume the '&&' token. */
8353 cp_lexer_consume_token (parser->lexer);
8354 /* Look for the identifier. */
8355 location_t finish_loc
8356 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8357 identifier = cp_parser_identifier (parser);
8358 /* Construct a location of the form:
8359 &&label
8360 ^~~~~~~
8361 with caret==start at the "&&", finish at the end of the label. */
8362 location_t combined_loc
8363 = make_location (start_loc, start_loc, finish_loc);
8364 /* Create an expression representing the address. */
8365 expression = finish_label_address_expr (identifier, combined_loc);
8366 if (cp_parser_non_integral_constant_expression (parser,
8367 NIC_ADDR_LABEL))
8368 expression = error_mark_node;
8369 return expression;
8370 }
8371 }
8372 if (unary_operator != ERROR_MARK)
8373 {
8374 cp_expr cast_expression;
8375 cp_expr expression = error_mark_node;
8376 non_integral_constant non_constant_p = NIC_NONE;
8377 location_t loc = token->location;
8378 tsubst_flags_t complain = complain_flags (decltype_p);
8379
8380 /* Consume the operator token. */
8381 token = cp_lexer_consume_token (parser->lexer);
8382 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8383
8384 /* Parse the cast-expression. */
8385 cast_expression
8386 = cp_parser_cast_expression (parser,
8387 unary_operator == ADDR_EXPR,
8388 /*cast_p=*/false,
8389 /*decltype*/false,
8390 pidk);
8391
8392 /* Make a location:
8393 OP_TOKEN CAST_EXPRESSION
8394 ^~~~~~~~~~~~~~~~~~~~~~~~~
8395 with start==caret at the operator token, and
8396 extending to the end of the cast_expression. */
8397 loc = make_location (loc, loc, cast_expression.get_finish ());
8398
8399 /* Now, build an appropriate representation. */
8400 switch (unary_operator)
8401 {
8402 case INDIRECT_REF:
8403 non_constant_p = NIC_STAR;
8404 expression = build_x_indirect_ref (loc, cast_expression,
8405 RO_UNARY_STAR,
8406 complain);
8407 /* TODO: build_x_indirect_ref does not always honor the
8408 location, so ensure it is set. */
8409 expression.set_location (loc);
8410 break;
8411
8412 case ADDR_EXPR:
8413 non_constant_p = NIC_ADDR;
8414 /* Fall through. */
8415 case BIT_NOT_EXPR:
8416 expression = build_x_unary_op (loc, unary_operator,
8417 cast_expression,
8418 complain);
8419 /* TODO: build_x_unary_op does not always honor the location,
8420 so ensure it is set. */
8421 expression.set_location (loc);
8422 break;
8423
8424 case PREINCREMENT_EXPR:
8425 case PREDECREMENT_EXPR:
8426 non_constant_p = unary_operator == PREINCREMENT_EXPR
8427 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8428 /* Fall through. */
8429 case NEGATE_EXPR:
8430 /* Immediately fold negation of a constant, unless the constant is 0
8431 (since -0 == 0) or it would overflow. */
8432 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8433 {
8434 tree stripped_expr
8435 = tree_strip_any_location_wrapper (cast_expression);
8436 if (CONSTANT_CLASS_P (stripped_expr)
8437 && !integer_zerop (stripped_expr)
8438 && !TREE_OVERFLOW (stripped_expr))
8439 {
8440 tree folded = fold_build1 (unary_operator,
8441 TREE_TYPE (stripped_expr),
8442 stripped_expr);
8443 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8444 {
8445 expression = maybe_wrap_with_location (folded, loc);
8446 break;
8447 }
8448 }
8449 }
8450 /* Fall through. */
8451 case UNARY_PLUS_EXPR:
8452 case TRUTH_NOT_EXPR:
8453 expression = finish_unary_op_expr (loc, unary_operator,
8454 cast_expression, complain);
8455 break;
8456
8457 default:
8458 gcc_unreachable ();
8459 }
8460
8461 if (non_constant_p != NIC_NONE
8462 && cp_parser_non_integral_constant_expression (parser,
8463 non_constant_p))
8464 expression = error_mark_node;
8465
8466 return expression;
8467 }
8468
8469 return cp_parser_postfix_expression (parser, address_p, cast_p,
8470 /*member_access_only_p=*/false,
8471 decltype_p,
8472 pidk);
8473 }
8474
8475 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8476 unary-operator, the corresponding tree code is returned. */
8477
8478 static enum tree_code
8479 cp_parser_unary_operator (cp_token* token)
8480 {
8481 switch (token->type)
8482 {
8483 case CPP_MULT:
8484 return INDIRECT_REF;
8485
8486 case CPP_AND:
8487 return ADDR_EXPR;
8488
8489 case CPP_PLUS:
8490 return UNARY_PLUS_EXPR;
8491
8492 case CPP_MINUS:
8493 return NEGATE_EXPR;
8494
8495 case CPP_NOT:
8496 return TRUTH_NOT_EXPR;
8497
8498 case CPP_COMPL:
8499 return BIT_NOT_EXPR;
8500
8501 default:
8502 return ERROR_MARK;
8503 }
8504 }
8505
8506 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8507 Returns a representation of the expression. */
8508
8509 static tree
8510 cp_parser_has_attribute_expression (cp_parser *parser)
8511 {
8512 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8513
8514 /* Consume the __builtin_has_attribute token. */
8515 cp_lexer_consume_token (parser->lexer);
8516
8517 matching_parens parens;
8518 if (!parens.require_open (parser))
8519 return error_mark_node;
8520
8521 /* Types cannot be defined in a `sizeof' expression. Save away the
8522 old message. */
8523 const char *saved_message = parser->type_definition_forbidden_message;
8524 const char *saved_message_arg
8525 = parser->type_definition_forbidden_message_arg;
8526 parser->type_definition_forbidden_message
8527 = G_("types may not be defined in %qs expressions");
8528 parser->type_definition_forbidden_message_arg
8529 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
8530
8531 /* The restrictions on constant-expressions do not apply inside
8532 sizeof expressions. */
8533 bool saved_integral_constant_expression_p
8534 = parser->integral_constant_expression_p;
8535 bool saved_non_integral_constant_expression_p
8536 = parser->non_integral_constant_expression_p;
8537 parser->integral_constant_expression_p = false;
8538
8539 /* Do not actually evaluate the expression. */
8540 ++cp_unevaluated_operand;
8541 ++c_inhibit_evaluation_warnings;
8542
8543 tree oper = NULL_TREE;
8544
8545 /* We can't be sure yet whether we're looking at a type-id or an
8546 expression. */
8547 cp_parser_parse_tentatively (parser);
8548
8549 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8550 parser->in_type_id_in_expr_p = true;
8551 /* Look for the type-id. */
8552 oper = cp_parser_type_id (parser);
8553 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8554
8555 cp_parser_parse_definitely (parser);
8556
8557 /* If the type-id production did not work out, then we must be
8558 looking at an expression. */
8559 if (!oper || oper == error_mark_node)
8560 oper = cp_parser_assignment_expression (parser);
8561
8562 STRIP_ANY_LOCATION_WRAPPER (oper);
8563
8564 /* Go back to evaluating expressions. */
8565 --cp_unevaluated_operand;
8566 --c_inhibit_evaluation_warnings;
8567
8568 /* And restore the old one. */
8569 parser->type_definition_forbidden_message = saved_message;
8570 parser->type_definition_forbidden_message_arg = saved_message_arg;
8571 parser->integral_constant_expression_p
8572 = saved_integral_constant_expression_p;
8573 parser->non_integral_constant_expression_p
8574 = saved_non_integral_constant_expression_p;
8575
8576 /* Consume the comma if it's there. */
8577 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
8578 {
8579 cp_parser_skip_to_closing_parenthesis (parser, false, false,
8580 /*consume_paren=*/true);
8581 return error_mark_node;
8582 }
8583
8584 /* Parse the attribute specification. */
8585 bool ret = false;
8586 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
8587 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
8588 {
8589 if (oper != error_mark_node)
8590 {
8591 /* Fold constant expressions used in attributes first. */
8592 cp_check_const_attributes (attr);
8593
8594 /* Finally, see if OPER has been declared with ATTR. */
8595 ret = has_attribute (atloc, oper, attr, default_conversion);
8596 }
8597
8598 parens.require_close (parser);
8599 }
8600 else
8601 {
8602 error_at (atloc, "expected identifier");
8603 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8604 }
8605
8606 /* Construct a location e.g. :
8607 __builtin_has_attribute (oper, attr)
8608 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8609 with start == caret at the start of the built-in token,
8610 and with the endpoint at the final closing paren. */
8611 location_t finish_loc
8612 = cp_lexer_previous_token (parser->lexer)->location;
8613 location_t compound_loc
8614 = make_location (start_loc, start_loc, finish_loc);
8615
8616 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
8617 ret_expr.set_location (compound_loc);
8618 ret_expr = ret_expr.maybe_add_location_wrapper ();
8619 return ret_expr;
8620 }
8621
8622 /* Parse a new-expression.
8623
8624 new-expression:
8625 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8626 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8627
8628 Returns a representation of the expression. */
8629
8630 static tree
8631 cp_parser_new_expression (cp_parser* parser)
8632 {
8633 bool global_scope_p;
8634 vec<tree, va_gc> *placement;
8635 tree type;
8636 vec<tree, va_gc> *initializer;
8637 tree nelts = NULL_TREE;
8638 tree ret;
8639
8640 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8641
8642 /* Look for the optional `::' operator. */
8643 global_scope_p
8644 = (cp_parser_global_scope_opt (parser,
8645 /*current_scope_valid_p=*/false)
8646 != NULL_TREE);
8647 /* Look for the `new' operator. */
8648 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8649 /* There's no easy way to tell a new-placement from the
8650 `( type-id )' construct. */
8651 cp_parser_parse_tentatively (parser);
8652 /* Look for a new-placement. */
8653 placement = cp_parser_new_placement (parser);
8654 /* If that didn't work out, there's no new-placement. */
8655 if (!cp_parser_parse_definitely (parser))
8656 {
8657 if (placement != NULL)
8658 release_tree_vector (placement);
8659 placement = NULL;
8660 }
8661
8662 /* If the next token is a `(', then we have a parenthesized
8663 type-id. */
8664 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8665 {
8666 cp_token *token;
8667 const char *saved_message = parser->type_definition_forbidden_message;
8668
8669 /* Consume the `('. */
8670 matching_parens parens;
8671 parens.consume_open (parser);
8672
8673 /* Parse the type-id. */
8674 parser->type_definition_forbidden_message
8675 = G_("types may not be defined in a new-expression");
8676 {
8677 type_id_in_expr_sentinel s (parser);
8678 type = cp_parser_type_id (parser);
8679 }
8680 parser->type_definition_forbidden_message = saved_message;
8681
8682 /* Look for the closing `)'. */
8683 parens.require_close (parser);
8684 token = cp_lexer_peek_token (parser->lexer);
8685 /* There should not be a direct-new-declarator in this production,
8686 but GCC used to allowed this, so we check and emit a sensible error
8687 message for this case. */
8688 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8689 {
8690 error_at (token->location,
8691 "array bound forbidden after parenthesized type-id");
8692 inform (token->location,
8693 "try removing the parentheses around the type-id");
8694 cp_parser_direct_new_declarator (parser);
8695 }
8696 }
8697 /* Otherwise, there must be a new-type-id. */
8698 else
8699 type = cp_parser_new_type_id (parser, &nelts);
8700
8701 /* If the next token is a `(' or '{', then we have a new-initializer. */
8702 cp_token *token = cp_lexer_peek_token (parser->lexer);
8703 if (token->type == CPP_OPEN_PAREN
8704 || token->type == CPP_OPEN_BRACE)
8705 initializer = cp_parser_new_initializer (parser);
8706 else
8707 initializer = NULL;
8708
8709 /* A new-expression may not appear in an integral constant
8710 expression. */
8711 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8712 ret = error_mark_node;
8713 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8714 of a new-type-id or type-id of a new-expression, the new-expression shall
8715 contain a new-initializer of the form ( assignment-expression )".
8716 Additionally, consistently with the spirit of DR 1467, we want to accept
8717 'new auto { 2 }' too. */
8718 else if ((ret = type_uses_auto (type))
8719 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8720 && (vec_safe_length (initializer) != 1
8721 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8722 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8723 {
8724 error_at (token->location,
8725 "initialization of new-expression for type %<auto%> "
8726 "requires exactly one element");
8727 ret = error_mark_node;
8728 }
8729 else
8730 {
8731 /* Construct a location e.g.:
8732 ptr = new int[100]
8733 ^~~~~~~~~~~~
8734 with caret == start at the start of the "new" token, and the end
8735 at the end of the final token we consumed. */
8736 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8737 location_t end_loc = get_finish (end_tok->location);
8738 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8739
8740 /* Create a representation of the new-expression. */
8741 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8742 tf_warning_or_error);
8743 protected_set_expr_location (ret, combined_loc);
8744 }
8745
8746 if (placement != NULL)
8747 release_tree_vector (placement);
8748 if (initializer != NULL)
8749 release_tree_vector (initializer);
8750
8751 return ret;
8752 }
8753
8754 /* Parse a new-placement.
8755
8756 new-placement:
8757 ( expression-list )
8758
8759 Returns the same representation as for an expression-list. */
8760
8761 static vec<tree, va_gc> *
8762 cp_parser_new_placement (cp_parser* parser)
8763 {
8764 vec<tree, va_gc> *expression_list;
8765
8766 /* Parse the expression-list. */
8767 expression_list = (cp_parser_parenthesized_expression_list
8768 (parser, non_attr, /*cast_p=*/false,
8769 /*allow_expansion_p=*/true,
8770 /*non_constant_p=*/NULL));
8771
8772 if (expression_list && expression_list->is_empty ())
8773 error ("expected expression-list or type-id");
8774
8775 return expression_list;
8776 }
8777
8778 /* Parse a new-type-id.
8779
8780 new-type-id:
8781 type-specifier-seq new-declarator [opt]
8782
8783 Returns the TYPE allocated. If the new-type-id indicates an array
8784 type, *NELTS is set to the number of elements in the last array
8785 bound; the TYPE will not include the last array bound. */
8786
8787 static tree
8788 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8789 {
8790 cp_decl_specifier_seq type_specifier_seq;
8791 cp_declarator *new_declarator;
8792 cp_declarator *declarator;
8793 cp_declarator *outer_declarator;
8794 const char *saved_message;
8795
8796 /* The type-specifier sequence must not contain type definitions.
8797 (It cannot contain declarations of new types either, but if they
8798 are not definitions we will catch that because they are not
8799 complete.) */
8800 saved_message = parser->type_definition_forbidden_message;
8801 parser->type_definition_forbidden_message
8802 = G_("types may not be defined in a new-type-id");
8803 /* Parse the type-specifier-seq. */
8804 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
8805 /*is_declaration=*/false,
8806 /*is_trailing_return=*/false,
8807 &type_specifier_seq);
8808 /* Restore the old message. */
8809 parser->type_definition_forbidden_message = saved_message;
8810
8811 if (type_specifier_seq.type == error_mark_node)
8812 return error_mark_node;
8813
8814 /* Parse the new-declarator. */
8815 new_declarator = cp_parser_new_declarator_opt (parser);
8816
8817 /* Determine the number of elements in the last array dimension, if
8818 any. */
8819 *nelts = NULL_TREE;
8820 /* Skip down to the last array dimension. */
8821 declarator = new_declarator;
8822 outer_declarator = NULL;
8823 while (declarator && (declarator->kind == cdk_pointer
8824 || declarator->kind == cdk_ptrmem))
8825 {
8826 outer_declarator = declarator;
8827 declarator = declarator->declarator;
8828 }
8829 while (declarator
8830 && declarator->kind == cdk_array
8831 && declarator->declarator
8832 && declarator->declarator->kind == cdk_array)
8833 {
8834 outer_declarator = declarator;
8835 declarator = declarator->declarator;
8836 }
8837
8838 if (declarator && declarator->kind == cdk_array)
8839 {
8840 *nelts = declarator->u.array.bounds;
8841 if (*nelts == error_mark_node)
8842 *nelts = integer_one_node;
8843
8844 if (outer_declarator)
8845 outer_declarator->declarator = declarator->declarator;
8846 else
8847 new_declarator = NULL;
8848 }
8849
8850 return groktypename (&type_specifier_seq, new_declarator, false);
8851 }
8852
8853 /* Parse an (optional) new-declarator.
8854
8855 new-declarator:
8856 ptr-operator new-declarator [opt]
8857 direct-new-declarator
8858
8859 Returns the declarator. */
8860
8861 static cp_declarator *
8862 cp_parser_new_declarator_opt (cp_parser* parser)
8863 {
8864 enum tree_code code;
8865 tree type, std_attributes = NULL_TREE;
8866 cp_cv_quals cv_quals;
8867
8868 /* We don't know if there's a ptr-operator next, or not. */
8869 cp_parser_parse_tentatively (parser);
8870 /* Look for a ptr-operator. */
8871 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8872 /* If that worked, look for more new-declarators. */
8873 if (cp_parser_parse_definitely (parser))
8874 {
8875 cp_declarator *declarator;
8876
8877 /* Parse another optional declarator. */
8878 declarator = cp_parser_new_declarator_opt (parser);
8879
8880 declarator = cp_parser_make_indirect_declarator
8881 (code, type, cv_quals, declarator, std_attributes);
8882
8883 return declarator;
8884 }
8885
8886 /* If the next token is a `[', there is a direct-new-declarator. */
8887 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8888 return cp_parser_direct_new_declarator (parser);
8889
8890 return NULL;
8891 }
8892
8893 /* Parse a direct-new-declarator.
8894
8895 direct-new-declarator:
8896 [ expression ]
8897 direct-new-declarator [constant-expression]
8898
8899 */
8900
8901 static cp_declarator *
8902 cp_parser_direct_new_declarator (cp_parser* parser)
8903 {
8904 cp_declarator *declarator = NULL;
8905
8906 while (true)
8907 {
8908 tree expression;
8909 cp_token *token;
8910
8911 /* Look for the opening `['. */
8912 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8913
8914 token = cp_lexer_peek_token (parser->lexer);
8915 expression = cp_parser_expression (parser);
8916 /* The standard requires that the expression have integral
8917 type. DR 74 adds enumeration types. We believe that the
8918 real intent is that these expressions be handled like the
8919 expression in a `switch' condition, which also allows
8920 classes with a single conversion to integral or
8921 enumeration type. */
8922 if (!processing_template_decl)
8923 {
8924 expression
8925 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8926 expression,
8927 /*complain=*/true);
8928 if (!expression)
8929 {
8930 error_at (token->location,
8931 "expression in new-declarator must have integral "
8932 "or enumeration type");
8933 expression = error_mark_node;
8934 }
8935 }
8936
8937 /* Look for the closing `]'. */
8938 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8939
8940 /* Add this bound to the declarator. */
8941 declarator = make_array_declarator (declarator, expression);
8942
8943 /* If the next token is not a `[', then there are no more
8944 bounds. */
8945 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8946 break;
8947 }
8948
8949 return declarator;
8950 }
8951
8952 /* Parse a new-initializer.
8953
8954 new-initializer:
8955 ( expression-list [opt] )
8956 braced-init-list
8957
8958 Returns a representation of the expression-list. */
8959
8960 static vec<tree, va_gc> *
8961 cp_parser_new_initializer (cp_parser* parser)
8962 {
8963 vec<tree, va_gc> *expression_list;
8964
8965 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8966 {
8967 tree t;
8968 bool expr_non_constant_p;
8969 cp_lexer_set_source_position (parser->lexer);
8970 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8971 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8972 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8973 expression_list = make_tree_vector_single (t);
8974 }
8975 else
8976 expression_list = (cp_parser_parenthesized_expression_list
8977 (parser, non_attr, /*cast_p=*/false,
8978 /*allow_expansion_p=*/true,
8979 /*non_constant_p=*/NULL));
8980
8981 return expression_list;
8982 }
8983
8984 /* Parse a delete-expression.
8985
8986 delete-expression:
8987 :: [opt] delete cast-expression
8988 :: [opt] delete [ ] cast-expression
8989
8990 Returns a representation of the expression. */
8991
8992 static tree
8993 cp_parser_delete_expression (cp_parser* parser)
8994 {
8995 bool global_scope_p;
8996 bool array_p;
8997 tree expression;
8998
8999 /* Look for the optional `::' operator. */
9000 global_scope_p
9001 = (cp_parser_global_scope_opt (parser,
9002 /*current_scope_valid_p=*/false)
9003 != NULL_TREE);
9004 /* Look for the `delete' keyword. */
9005 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9006 /* See if the array syntax is in use. */
9007 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9008 {
9009 /* Consume the `[' token. */
9010 cp_lexer_consume_token (parser->lexer);
9011 /* Look for the `]' token. */
9012 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9013 /* Remember that this is the `[]' construct. */
9014 array_p = true;
9015 }
9016 else
9017 array_p = false;
9018
9019 /* Parse the cast-expression. */
9020 expression = cp_parser_simple_cast_expression (parser);
9021
9022 /* A delete-expression may not appear in an integral constant
9023 expression. */
9024 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9025 return error_mark_node;
9026
9027 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
9028 tf_warning_or_error);
9029 }
9030
9031 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9032 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9033 0 otherwise. */
9034
9035 static int
9036 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9037 {
9038 cp_token *token = cp_lexer_peek_token (parser->lexer);
9039 switch (token->type)
9040 {
9041 case CPP_COMMA:
9042 case CPP_SEMICOLON:
9043 case CPP_QUERY:
9044 case CPP_COLON:
9045 case CPP_CLOSE_SQUARE:
9046 case CPP_CLOSE_PAREN:
9047 case CPP_CLOSE_BRACE:
9048 case CPP_OPEN_BRACE:
9049 case CPP_DOT:
9050 case CPP_DOT_STAR:
9051 case CPP_DEREF:
9052 case CPP_DEREF_STAR:
9053 case CPP_DIV:
9054 case CPP_MOD:
9055 case CPP_LSHIFT:
9056 case CPP_RSHIFT:
9057 case CPP_LESS:
9058 case CPP_GREATER:
9059 case CPP_LESS_EQ:
9060 case CPP_GREATER_EQ:
9061 case CPP_EQ_EQ:
9062 case CPP_NOT_EQ:
9063 case CPP_EQ:
9064 case CPP_MULT_EQ:
9065 case CPP_DIV_EQ:
9066 case CPP_MOD_EQ:
9067 case CPP_PLUS_EQ:
9068 case CPP_MINUS_EQ:
9069 case CPP_RSHIFT_EQ:
9070 case CPP_LSHIFT_EQ:
9071 case CPP_AND_EQ:
9072 case CPP_XOR_EQ:
9073 case CPP_OR_EQ:
9074 case CPP_XOR:
9075 case CPP_OR:
9076 case CPP_OR_OR:
9077 case CPP_EOF:
9078 case CPP_ELLIPSIS:
9079 return 0;
9080
9081 case CPP_OPEN_PAREN:
9082 /* In ((type ()) () the last () isn't a valid cast-expression,
9083 so the whole must be parsed as postfix-expression. */
9084 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9085 != CPP_CLOSE_PAREN;
9086
9087 case CPP_OPEN_SQUARE:
9088 /* '[' may start a primary-expression in obj-c++ and in C++11,
9089 as a lambda-expression, eg, '(void)[]{}'. */
9090 if (cxx_dialect >= cxx11)
9091 return -1;
9092 return c_dialect_objc ();
9093
9094 case CPP_PLUS_PLUS:
9095 case CPP_MINUS_MINUS:
9096 /* '++' and '--' may or may not start a cast-expression:
9097
9098 struct T { void operator++(int); };
9099 void f() { (T())++; }
9100
9101 vs
9102
9103 int a;
9104 (int)++a; */
9105 return -1;
9106
9107 default:
9108 return 1;
9109 }
9110 }
9111
9112 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9113 in the order: const_cast, static_cast, reinterpret_cast.
9114
9115 Don't suggest dynamic_cast.
9116
9117 Return the first legal cast kind found, or NULL otherwise. */
9118
9119 static const char *
9120 get_cast_suggestion (tree dst_type, tree orig_expr)
9121 {
9122 tree trial;
9123
9124 /* Reuse the parser logic by attempting to build the various kinds of
9125 cast, with "complain" disabled.
9126 Identify the first such cast that is valid. */
9127
9128 /* Don't attempt to run such logic within template processing. */
9129 if (processing_template_decl)
9130 return NULL;
9131
9132 /* First try const_cast. */
9133 trial = build_const_cast (dst_type, orig_expr, tf_none);
9134 if (trial != error_mark_node)
9135 return "const_cast";
9136
9137 /* If that fails, try static_cast. */
9138 trial = build_static_cast (dst_type, orig_expr, tf_none);
9139 if (trial != error_mark_node)
9140 return "static_cast";
9141
9142 /* Finally, try reinterpret_cast. */
9143 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
9144 if (trial != error_mark_node)
9145 return "reinterpret_cast";
9146
9147 /* No such cast possible. */
9148 return NULL;
9149 }
9150
9151 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9152 suggesting how to convert a C-style cast of the form:
9153
9154 (DST_TYPE)ORIG_EXPR
9155
9156 to a C++-style cast.
9157
9158 The primary range of RICHLOC is asssumed to be that of the original
9159 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9160 of the parens in the C-style cast. */
9161
9162 static void
9163 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9164 location_t close_paren_loc, tree orig_expr,
9165 tree dst_type)
9166 {
9167 /* This function is non-trivial, so bail out now if the warning isn't
9168 going to be emitted. */
9169 if (!warn_old_style_cast)
9170 return;
9171
9172 /* Try to find a legal C++ cast, trying them in order:
9173 const_cast, static_cast, reinterpret_cast. */
9174 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9175 if (!cast_suggestion)
9176 return;
9177
9178 /* Replace the open paren with "CAST_SUGGESTION<". */
9179 pretty_printer pp;
9180 pp_printf (&pp, "%s<", cast_suggestion);
9181 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9182
9183 /* Replace the close paren with "> (". */
9184 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9185
9186 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9187 rich_loc->add_fixit_insert_after (")");
9188 }
9189
9190
9191 /* Parse a cast-expression.
9192
9193 cast-expression:
9194 unary-expression
9195 ( type-id ) cast-expression
9196
9197 ADDRESS_P is true iff the unary-expression is appearing as the
9198 operand of the `&' operator. CAST_P is true if this expression is
9199 the target of a cast.
9200
9201 Returns a representation of the expression. */
9202
9203 static cp_expr
9204 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9205 bool decltype_p, cp_id_kind * pidk)
9206 {
9207 /* If it's a `(', then we might be looking at a cast. */
9208 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9209 {
9210 tree type = NULL_TREE;
9211 cp_expr expr (NULL_TREE);
9212 int cast_expression = 0;
9213 const char *saved_message;
9214
9215 /* There's no way to know yet whether or not this is a cast.
9216 For example, `(int (3))' is a unary-expression, while `(int)
9217 3' is a cast. So, we resort to parsing tentatively. */
9218 cp_parser_parse_tentatively (parser);
9219 /* Types may not be defined in a cast. */
9220 saved_message = parser->type_definition_forbidden_message;
9221 parser->type_definition_forbidden_message
9222 = G_("types may not be defined in casts");
9223 /* Consume the `('. */
9224 matching_parens parens;
9225 cp_token *open_paren = parens.consume_open (parser);
9226 location_t open_paren_loc = open_paren->location;
9227 location_t close_paren_loc = UNKNOWN_LOCATION;
9228
9229 /* A very tricky bit is that `(struct S) { 3 }' is a
9230 compound-literal (which we permit in C++ as an extension).
9231 But, that construct is not a cast-expression -- it is a
9232 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9233 is legal; if the compound-literal were a cast-expression,
9234 you'd need an extra set of parentheses.) But, if we parse
9235 the type-id, and it happens to be a class-specifier, then we
9236 will commit to the parse at that point, because we cannot
9237 undo the action that is done when creating a new class. So,
9238 then we cannot back up and do a postfix-expression.
9239
9240 Another tricky case is the following (c++/29234):
9241
9242 struct S { void operator () (); };
9243
9244 void foo ()
9245 {
9246 ( S()() );
9247 }
9248
9249 As a type-id we parse the parenthesized S()() as a function
9250 returning a function, groktypename complains and we cannot
9251 back up in this case either.
9252
9253 Therefore, we scan ahead to the closing `)', and check to see
9254 if the tokens after the `)' can start a cast-expression. Otherwise
9255 we are dealing with an unary-expression, a postfix-expression
9256 or something else.
9257
9258 Yet another tricky case, in C++11, is the following (c++/54891):
9259
9260 (void)[]{};
9261
9262 The issue is that usually, besides the case of lambda-expressions,
9263 the parenthesized type-id cannot be followed by '[', and, eg, we
9264 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9265 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9266 we don't commit, we try a cast-expression, then an unary-expression.
9267
9268 Save tokens so that we can put them back. */
9269 cp_lexer_save_tokens (parser->lexer);
9270
9271 /* We may be looking at a cast-expression. */
9272 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9273 /*consume_paren=*/true))
9274 cast_expression
9275 = cp_parser_tokens_start_cast_expression (parser);
9276
9277 /* Roll back the tokens we skipped. */
9278 cp_lexer_rollback_tokens (parser->lexer);
9279 /* If we aren't looking at a cast-expression, simulate an error so
9280 that the call to cp_parser_error_occurred below returns true. */
9281 if (!cast_expression)
9282 cp_parser_simulate_error (parser);
9283 else
9284 {
9285 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9286 parser->in_type_id_in_expr_p = true;
9287 /* Look for the type-id. */
9288 type = cp_parser_type_id (parser);
9289 /* Look for the closing `)'. */
9290 cp_token *close_paren = parens.require_close (parser);
9291 if (close_paren)
9292 close_paren_loc = close_paren->location;
9293 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9294 }
9295
9296 /* Restore the saved message. */
9297 parser->type_definition_forbidden_message = saved_message;
9298
9299 /* At this point this can only be either a cast or a
9300 parenthesized ctor such as `(T ())' that looks like a cast to
9301 function returning T. */
9302 if (!cp_parser_error_occurred (parser))
9303 {
9304 /* Only commit if the cast-expression doesn't start with
9305 '++', '--', or '[' in C++11. */
9306 if (cast_expression > 0)
9307 cp_parser_commit_to_topmost_tentative_parse (parser);
9308
9309 expr = cp_parser_cast_expression (parser,
9310 /*address_p=*/false,
9311 /*cast_p=*/true,
9312 /*decltype_p=*/false,
9313 pidk);
9314
9315 if (cp_parser_parse_definitely (parser))
9316 {
9317 /* Warn about old-style casts, if so requested. */
9318 if (warn_old_style_cast
9319 && !in_system_header_at (input_location)
9320 && !VOID_TYPE_P (type)
9321 && current_lang_name != lang_name_c)
9322 {
9323 gcc_rich_location rich_loc (input_location);
9324 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9325 expr, type);
9326 warning_at (&rich_loc, OPT_Wold_style_cast,
9327 "use of old-style cast to %q#T", type);
9328 }
9329
9330 /* Only type conversions to integral or enumeration types
9331 can be used in constant-expressions. */
9332 if (!cast_valid_in_integral_constant_expression_p (type)
9333 && cp_parser_non_integral_constant_expression (parser,
9334 NIC_CAST))
9335 return error_mark_node;
9336
9337 /* Perform the cast. */
9338 /* Make a location:
9339 (TYPE) EXPR
9340 ^~~~~~~~~~~
9341 with start==caret at the open paren, extending to the
9342 end of "expr". */
9343 location_t cast_loc = make_location (open_paren_loc,
9344 open_paren_loc,
9345 expr.get_finish ());
9346 expr = build_c_cast (cast_loc, type, expr);
9347 return expr;
9348 }
9349 }
9350 else
9351 cp_parser_abort_tentative_parse (parser);
9352 }
9353
9354 /* If we get here, then it's not a cast, so it must be a
9355 unary-expression. */
9356 return cp_parser_unary_expression (parser, pidk, address_p,
9357 cast_p, decltype_p);
9358 }
9359
9360 /* Parse a binary expression of the general form:
9361
9362 pm-expression:
9363 cast-expression
9364 pm-expression .* cast-expression
9365 pm-expression ->* cast-expression
9366
9367 multiplicative-expression:
9368 pm-expression
9369 multiplicative-expression * pm-expression
9370 multiplicative-expression / pm-expression
9371 multiplicative-expression % pm-expression
9372
9373 additive-expression:
9374 multiplicative-expression
9375 additive-expression + multiplicative-expression
9376 additive-expression - multiplicative-expression
9377
9378 shift-expression:
9379 additive-expression
9380 shift-expression << additive-expression
9381 shift-expression >> additive-expression
9382
9383 relational-expression:
9384 shift-expression
9385 relational-expression < shift-expression
9386 relational-expression > shift-expression
9387 relational-expression <= shift-expression
9388 relational-expression >= shift-expression
9389
9390 GNU Extension:
9391
9392 relational-expression:
9393 relational-expression <? shift-expression
9394 relational-expression >? shift-expression
9395
9396 equality-expression:
9397 relational-expression
9398 equality-expression == relational-expression
9399 equality-expression != relational-expression
9400
9401 and-expression:
9402 equality-expression
9403 and-expression & equality-expression
9404
9405 exclusive-or-expression:
9406 and-expression
9407 exclusive-or-expression ^ and-expression
9408
9409 inclusive-or-expression:
9410 exclusive-or-expression
9411 inclusive-or-expression | exclusive-or-expression
9412
9413 logical-and-expression:
9414 inclusive-or-expression
9415 logical-and-expression && inclusive-or-expression
9416
9417 logical-or-expression:
9418 logical-and-expression
9419 logical-or-expression || logical-and-expression
9420
9421 All these are implemented with a single function like:
9422
9423 binary-expression:
9424 simple-cast-expression
9425 binary-expression <token> binary-expression
9426
9427 CAST_P is true if this expression is the target of a cast.
9428
9429 The binops_by_token map is used to get the tree codes for each <token> type.
9430 binary-expressions are associated according to a precedence table. */
9431
9432 #define TOKEN_PRECEDENCE(token) \
9433 (((token->type == CPP_GREATER \
9434 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9435 && !parser->greater_than_is_operator_p) \
9436 ? PREC_NOT_OPERATOR \
9437 : binops_by_token[token->type].prec)
9438
9439 static cp_expr
9440 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9441 bool no_toplevel_fold_p,
9442 bool decltype_p,
9443 enum cp_parser_prec prec,
9444 cp_id_kind * pidk)
9445 {
9446 cp_parser_expression_stack stack;
9447 cp_parser_expression_stack_entry *sp = &stack[0];
9448 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
9449 cp_parser_expression_stack_entry current;
9450 cp_expr rhs;
9451 cp_token *token;
9452 enum tree_code rhs_type;
9453 enum cp_parser_prec new_prec, lookahead_prec;
9454 tree overload;
9455
9456 /* Parse the first expression. */
9457 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9458 ? TRUTH_NOT_EXPR : ERROR_MARK);
9459 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9460 cast_p, decltype_p, pidk);
9461 current.prec = prec;
9462
9463 if (cp_parser_error_occurred (parser))
9464 return error_mark_node;
9465
9466 for (;;)
9467 {
9468 /* Get an operator token. */
9469 token = cp_lexer_peek_token (parser->lexer);
9470
9471 if (warn_cxx11_compat
9472 && token->type == CPP_RSHIFT
9473 && !parser->greater_than_is_operator_p)
9474 {
9475 if (warning_at (token->location, OPT_Wc__11_compat,
9476 "%<>>%> operator is treated"
9477 " as two right angle brackets in C++11"))
9478 inform (token->location,
9479 "suggest parentheses around %<>>%> expression");
9480 }
9481
9482 new_prec = TOKEN_PRECEDENCE (token);
9483 if (new_prec != PREC_NOT_OPERATOR
9484 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9485 /* This is a fold-expression; handle it later. */
9486 new_prec = PREC_NOT_OPERATOR;
9487
9488 /* Popping an entry off the stack means we completed a subexpression:
9489 - either we found a token which is not an operator (`>' where it is not
9490 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9491 will happen repeatedly;
9492 - or, we found an operator which has lower priority. This is the case
9493 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9494 parsing `3 * 4'. */
9495 if (new_prec <= current.prec)
9496 {
9497 if (sp == stack)
9498 break;
9499 else
9500 goto pop;
9501 }
9502
9503 get_rhs:
9504 current.tree_type = binops_by_token[token->type].tree_type;
9505 current.loc = token->location;
9506
9507 /* We used the operator token. */
9508 cp_lexer_consume_token (parser->lexer);
9509
9510 /* For "false && x" or "true || x", x will never be executed;
9511 disable warnings while evaluating it. */
9512 if ((current.tree_type == TRUTH_ANDIF_EXPR
9513 && cp_fully_fold (current.lhs) == truthvalue_false_node)
9514 || (current.tree_type == TRUTH_ORIF_EXPR
9515 && cp_fully_fold (current.lhs) == truthvalue_true_node))
9516 {
9517 disable_warnings_sp = sp;
9518 ++c_inhibit_evaluation_warnings;
9519 }
9520
9521 /* Extract another operand. It may be the RHS of this expression
9522 or the LHS of a new, higher priority expression. */
9523 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9524 ? TRUTH_NOT_EXPR : ERROR_MARK);
9525 rhs = cp_parser_simple_cast_expression (parser);
9526
9527 /* Get another operator token. Look up its precedence to avoid
9528 building a useless (immediately popped) stack entry for common
9529 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9530 token = cp_lexer_peek_token (parser->lexer);
9531 lookahead_prec = TOKEN_PRECEDENCE (token);
9532 if (lookahead_prec != PREC_NOT_OPERATOR
9533 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9534 lookahead_prec = PREC_NOT_OPERATOR;
9535 if (lookahead_prec > new_prec)
9536 {
9537 /* ... and prepare to parse the RHS of the new, higher priority
9538 expression. Since precedence levels on the stack are
9539 monotonically increasing, we do not have to care about
9540 stack overflows. */
9541 *sp = current;
9542 ++sp;
9543 current.lhs = rhs;
9544 current.lhs_type = rhs_type;
9545 current.prec = new_prec;
9546 new_prec = lookahead_prec;
9547 goto get_rhs;
9548
9549 pop:
9550 lookahead_prec = new_prec;
9551 /* If the stack is not empty, we have parsed into LHS the right side
9552 (`4' in the example above) of an expression we had suspended.
9553 We can use the information on the stack to recover the LHS (`3')
9554 from the stack together with the tree code (`MULT_EXPR'), and
9555 the precedence of the higher level subexpression
9556 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9557 which will be used to actually build the additive expression. */
9558 rhs = current.lhs;
9559 rhs_type = current.lhs_type;
9560 --sp;
9561 current = *sp;
9562 }
9563
9564 /* Undo the disabling of warnings done above. */
9565 if (sp == disable_warnings_sp)
9566 {
9567 disable_warnings_sp = NULL;
9568 --c_inhibit_evaluation_warnings;
9569 }
9570
9571 if (warn_logical_not_paren
9572 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9573 && current.lhs_type == TRUTH_NOT_EXPR
9574 /* Avoid warning for !!x == y. */
9575 && (TREE_CODE (current.lhs) != NE_EXPR
9576 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9577 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9578 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9579 /* Avoid warning for !b == y where b is boolean. */
9580 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9581 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9582 != BOOLEAN_TYPE))))
9583 /* Avoid warning for !!b == y where b is boolean. */
9584 && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
9585 || TREE_TYPE (current.lhs) == NULL_TREE
9586 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9587 warn_logical_not_parentheses (current.loc, current.tree_type,
9588 current.lhs, maybe_constant_value (rhs));
9589
9590 overload = NULL;
9591
9592 location_t combined_loc = make_location (current.loc,
9593 current.lhs.get_start (),
9594 rhs.get_finish ());
9595
9596 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9597 ERROR_MARK for everything that is not a binary expression.
9598 This makes warn_about_parentheses miss some warnings that
9599 involve unary operators. For unary expressions we should
9600 pass the correct tree_code unless the unary expression was
9601 surrounded by parentheses.
9602 */
9603 if (no_toplevel_fold_p
9604 && lookahead_prec <= current.prec
9605 && sp == stack)
9606 {
9607 if (current.lhs == error_mark_node || rhs == error_mark_node)
9608 current.lhs = error_mark_node;
9609 else
9610 {
9611 current.lhs
9612 = build_min (current.tree_type,
9613 TREE_CODE_CLASS (current.tree_type)
9614 == tcc_comparison
9615 ? boolean_type_node : TREE_TYPE (current.lhs),
9616 current.lhs.get_value (), rhs.get_value ());
9617 SET_EXPR_LOCATION (current.lhs, combined_loc);
9618 }
9619 }
9620 else
9621 {
9622 op_location_t op_loc (current.loc, combined_loc);
9623 current.lhs = build_x_binary_op (op_loc, current.tree_type,
9624 current.lhs, current.lhs_type,
9625 rhs, rhs_type, &overload,
9626 complain_flags (decltype_p));
9627 /* TODO: build_x_binary_op doesn't always honor the location. */
9628 current.lhs.set_location (combined_loc);
9629 }
9630 current.lhs_type = current.tree_type;
9631
9632 /* If the binary operator required the use of an overloaded operator,
9633 then this expression cannot be an integral constant-expression.
9634 An overloaded operator can be used even if both operands are
9635 otherwise permissible in an integral constant-expression if at
9636 least one of the operands is of enumeration type. */
9637
9638 if (overload
9639 && cp_parser_non_integral_constant_expression (parser,
9640 NIC_OVERLOADED))
9641 return error_mark_node;
9642 }
9643
9644 return current.lhs;
9645 }
9646
9647 static cp_expr
9648 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9649 bool no_toplevel_fold_p,
9650 enum cp_parser_prec prec,
9651 cp_id_kind * pidk)
9652 {
9653 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9654 /*decltype*/false, prec, pidk);
9655 }
9656
9657 /* Parse the `? expression : assignment-expression' part of a
9658 conditional-expression. The LOGICAL_OR_EXPR is the
9659 logical-or-expression that started the conditional-expression.
9660 Returns a representation of the entire conditional-expression.
9661
9662 This routine is used by cp_parser_assignment_expression.
9663
9664 ? expression : assignment-expression
9665
9666 GNU Extensions:
9667
9668 ? : assignment-expression */
9669
9670 static tree
9671 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9672 {
9673 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9674 cp_expr assignment_expr;
9675 struct cp_token *token;
9676 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9677
9678 /* Consume the `?' token. */
9679 cp_lexer_consume_token (parser->lexer);
9680 token = cp_lexer_peek_token (parser->lexer);
9681 if (cp_parser_allow_gnu_extensions_p (parser)
9682 && token->type == CPP_COLON)
9683 {
9684 pedwarn (token->location, OPT_Wpedantic,
9685 "ISO C++ does not allow ?: with omitted middle operand");
9686 /* Implicit true clause. */
9687 expr = NULL_TREE;
9688 c_inhibit_evaluation_warnings +=
9689 folded_logical_or_expr == truthvalue_true_node;
9690 warn_for_omitted_condop (token->location, logical_or_expr);
9691 }
9692 else
9693 {
9694 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9695 parser->colon_corrects_to_scope_p = false;
9696 /* Parse the expression. */
9697 c_inhibit_evaluation_warnings +=
9698 folded_logical_or_expr == truthvalue_false_node;
9699 expr = cp_parser_expression (parser);
9700 c_inhibit_evaluation_warnings +=
9701 ((folded_logical_or_expr == truthvalue_true_node)
9702 - (folded_logical_or_expr == truthvalue_false_node));
9703 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9704 }
9705
9706 /* The next token should be a `:'. */
9707 cp_parser_require (parser, CPP_COLON, RT_COLON);
9708 /* Parse the assignment-expression. */
9709 assignment_expr = cp_parser_assignment_expression (parser);
9710 c_inhibit_evaluation_warnings -=
9711 folded_logical_or_expr == truthvalue_true_node;
9712
9713 /* Make a location:
9714 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9715 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9716 with the caret at the "?", ranging from the start of
9717 the logical_or_expr to the end of the assignment_expr. */
9718 loc = make_location (loc,
9719 logical_or_expr.get_start (),
9720 assignment_expr.get_finish ());
9721
9722 /* Build the conditional-expression. */
9723 return build_x_conditional_expr (loc, logical_or_expr,
9724 expr,
9725 assignment_expr,
9726 tf_warning_or_error);
9727 }
9728
9729 /* Parse an assignment-expression.
9730
9731 assignment-expression:
9732 conditional-expression
9733 logical-or-expression assignment-operator assignment_expression
9734 throw-expression
9735
9736 CAST_P is true if this expression is the target of a cast.
9737 DECLTYPE_P is true if this expression is the operand of decltype.
9738
9739 Returns a representation for the expression. */
9740
9741 static cp_expr
9742 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9743 bool cast_p, bool decltype_p)
9744 {
9745 cp_expr expr;
9746
9747 /* If the next token is the `throw' keyword, then we're looking at
9748 a throw-expression. */
9749 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9750 expr = cp_parser_throw_expression (parser);
9751 /* Otherwise, it must be that we are looking at a
9752 logical-or-expression. */
9753 else
9754 {
9755 /* Parse the binary expressions (logical-or-expression). */
9756 expr = cp_parser_binary_expression (parser, cast_p, false,
9757 decltype_p,
9758 PREC_NOT_OPERATOR, pidk);
9759 /* If the next token is a `?' then we're actually looking at a
9760 conditional-expression. */
9761 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9762 return cp_parser_question_colon_clause (parser, expr);
9763 else
9764 {
9765 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9766
9767 /* If it's an assignment-operator, we're using the second
9768 production. */
9769 enum tree_code assignment_operator
9770 = cp_parser_assignment_operator_opt (parser);
9771 if (assignment_operator != ERROR_MARK)
9772 {
9773 bool non_constant_p;
9774
9775 /* Parse the right-hand side of the assignment. */
9776 cp_expr rhs = cp_parser_initializer_clause (parser,
9777 &non_constant_p);
9778
9779 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9780 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9781
9782 /* An assignment may not appear in a
9783 constant-expression. */
9784 if (cp_parser_non_integral_constant_expression (parser,
9785 NIC_ASSIGNMENT))
9786 return error_mark_node;
9787 /* Build the assignment expression. Its default
9788 location:
9789 LHS = RHS
9790 ~~~~^~~~~
9791 is the location of the '=' token as the
9792 caret, ranging from the start of the lhs to the
9793 end of the rhs. */
9794 loc = make_location (loc,
9795 expr.get_start (),
9796 rhs.get_finish ());
9797 expr = build_x_modify_expr (loc, expr,
9798 assignment_operator,
9799 rhs,
9800 complain_flags (decltype_p));
9801 /* TODO: build_x_modify_expr doesn't honor the location,
9802 so we must set it here. */
9803 expr.set_location (loc);
9804 }
9805 }
9806 }
9807
9808 return expr;
9809 }
9810
9811 /* Parse an (optional) assignment-operator.
9812
9813 assignment-operator: one of
9814 = *= /= %= += -= >>= <<= &= ^= |=
9815
9816 GNU Extension:
9817
9818 assignment-operator: one of
9819 <?= >?=
9820
9821 If the next token is an assignment operator, the corresponding tree
9822 code is returned, and the token is consumed. For example, for
9823 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9824 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9825 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9826 operator, ERROR_MARK is returned. */
9827
9828 static enum tree_code
9829 cp_parser_assignment_operator_opt (cp_parser* parser)
9830 {
9831 enum tree_code op;
9832 cp_token *token;
9833
9834 /* Peek at the next token. */
9835 token = cp_lexer_peek_token (parser->lexer);
9836
9837 switch (token->type)
9838 {
9839 case CPP_EQ:
9840 op = NOP_EXPR;
9841 break;
9842
9843 case CPP_MULT_EQ:
9844 op = MULT_EXPR;
9845 break;
9846
9847 case CPP_DIV_EQ:
9848 op = TRUNC_DIV_EXPR;
9849 break;
9850
9851 case CPP_MOD_EQ:
9852 op = TRUNC_MOD_EXPR;
9853 break;
9854
9855 case CPP_PLUS_EQ:
9856 op = PLUS_EXPR;
9857 break;
9858
9859 case CPP_MINUS_EQ:
9860 op = MINUS_EXPR;
9861 break;
9862
9863 case CPP_RSHIFT_EQ:
9864 op = RSHIFT_EXPR;
9865 break;
9866
9867 case CPP_LSHIFT_EQ:
9868 op = LSHIFT_EXPR;
9869 break;
9870
9871 case CPP_AND_EQ:
9872 op = BIT_AND_EXPR;
9873 break;
9874
9875 case CPP_XOR_EQ:
9876 op = BIT_XOR_EXPR;
9877 break;
9878
9879 case CPP_OR_EQ:
9880 op = BIT_IOR_EXPR;
9881 break;
9882
9883 default:
9884 /* Nothing else is an assignment operator. */
9885 op = ERROR_MARK;
9886 }
9887
9888 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9889 if (op != ERROR_MARK
9890 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9891 op = ERROR_MARK;
9892
9893 /* If it was an assignment operator, consume it. */
9894 if (op != ERROR_MARK)
9895 cp_lexer_consume_token (parser->lexer);
9896
9897 return op;
9898 }
9899
9900 /* Parse an expression.
9901
9902 expression:
9903 assignment-expression
9904 expression , assignment-expression
9905
9906 CAST_P is true if this expression is the target of a cast.
9907 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9908 except possibly parenthesized or on the RHS of a comma (N3276).
9909
9910 Returns a representation of the expression. */
9911
9912 static cp_expr
9913 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9914 bool cast_p, bool decltype_p)
9915 {
9916 cp_expr expression = NULL_TREE;
9917 location_t loc = UNKNOWN_LOCATION;
9918
9919 while (true)
9920 {
9921 cp_expr assignment_expression;
9922
9923 /* Parse the next assignment-expression. */
9924 assignment_expression
9925 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9926
9927 /* We don't create a temporary for a call that is the immediate operand
9928 of decltype or on the RHS of a comma. But when we see a comma, we
9929 need to create a temporary for a call on the LHS. */
9930 if (decltype_p && !processing_template_decl
9931 && TREE_CODE (assignment_expression) == CALL_EXPR
9932 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9933 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9934 assignment_expression
9935 = build_cplus_new (TREE_TYPE (assignment_expression),
9936 assignment_expression, tf_warning_or_error);
9937
9938 /* If this is the first assignment-expression, we can just
9939 save it away. */
9940 if (!expression)
9941 expression = assignment_expression;
9942 else
9943 {
9944 /* Create a location with caret at the comma, ranging
9945 from the start of the LHS to the end of the RHS. */
9946 loc = make_location (loc,
9947 expression.get_start (),
9948 assignment_expression.get_finish ());
9949 expression = build_x_compound_expr (loc, expression,
9950 assignment_expression,
9951 complain_flags (decltype_p));
9952 expression.set_location (loc);
9953 }
9954 /* If the next token is not a comma, or we're in a fold-expression, then
9955 we are done with the expression. */
9956 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9957 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9958 break;
9959 /* Consume the `,'. */
9960 loc = cp_lexer_peek_token (parser->lexer)->location;
9961 cp_lexer_consume_token (parser->lexer);
9962 /* A comma operator cannot appear in a constant-expression. */
9963 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9964 expression = error_mark_node;
9965 }
9966
9967 return expression;
9968 }
9969
9970 /* Parse a constant-expression.
9971
9972 constant-expression:
9973 conditional-expression
9974
9975 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9976 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9977 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9978 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9979 only parse a conditional-expression, otherwise parse an
9980 assignment-expression. See below for rationale. */
9981
9982 static cp_expr
9983 cp_parser_constant_expression (cp_parser* parser,
9984 bool allow_non_constant_p,
9985 bool *non_constant_p,
9986 bool strict_p)
9987 {
9988 bool saved_integral_constant_expression_p;
9989 bool saved_allow_non_integral_constant_expression_p;
9990 bool saved_non_integral_constant_expression_p;
9991 cp_expr expression;
9992
9993 /* It might seem that we could simply parse the
9994 conditional-expression, and then check to see if it were
9995 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9996 one that the compiler can figure out is constant, possibly after
9997 doing some simplifications or optimizations. The standard has a
9998 precise definition of constant-expression, and we must honor
9999 that, even though it is somewhat more restrictive.
10000
10001 For example:
10002
10003 int i[(2, 3)];
10004
10005 is not a legal declaration, because `(2, 3)' is not a
10006 constant-expression. The `,' operator is forbidden in a
10007 constant-expression. However, GCC's constant-folding machinery
10008 will fold this operation to an INTEGER_CST for `3'. */
10009
10010 /* Save the old settings. */
10011 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10012 saved_allow_non_integral_constant_expression_p
10013 = parser->allow_non_integral_constant_expression_p;
10014 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10015 /* We are now parsing a constant-expression. */
10016 parser->integral_constant_expression_p = true;
10017 parser->allow_non_integral_constant_expression_p
10018 = (allow_non_constant_p || cxx_dialect >= cxx11);
10019 parser->non_integral_constant_expression_p = false;
10020 /* Although the grammar says "conditional-expression", when not STRICT_P,
10021 we parse an "assignment-expression", which also permits
10022 "throw-expression" and the use of assignment operators. In the case
10023 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10024 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10025 actually essential that we look for an assignment-expression.
10026 For example, cp_parser_initializer_clauses uses this function to
10027 determine whether a particular assignment-expression is in fact
10028 constant. */
10029 if (strict_p)
10030 {
10031 /* Parse the binary expressions (logical-or-expression). */
10032 expression = cp_parser_binary_expression (parser, false, false, false,
10033 PREC_NOT_OPERATOR, NULL);
10034 /* If the next token is a `?' then we're actually looking at
10035 a conditional-expression; otherwise we're done. */
10036 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10037 expression = cp_parser_question_colon_clause (parser, expression);
10038 }
10039 else
10040 expression = cp_parser_assignment_expression (parser);
10041 /* Restore the old settings. */
10042 parser->integral_constant_expression_p
10043 = saved_integral_constant_expression_p;
10044 parser->allow_non_integral_constant_expression_p
10045 = saved_allow_non_integral_constant_expression_p;
10046 if (cxx_dialect >= cxx11)
10047 {
10048 /* Require an rvalue constant expression here; that's what our
10049 callers expect. Reference constant expressions are handled
10050 separately in e.g. cp_parser_template_argument. */
10051 tree decay = expression;
10052 if (TREE_TYPE (expression)
10053 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10054 decay = build_address (expression);
10055 bool is_const = potential_rvalue_constant_expression (decay);
10056 parser->non_integral_constant_expression_p = !is_const;
10057 if (!is_const && !allow_non_constant_p)
10058 require_potential_rvalue_constant_expression (decay);
10059 }
10060 if (allow_non_constant_p)
10061 *non_constant_p = parser->non_integral_constant_expression_p;
10062 parser->non_integral_constant_expression_p
10063 = saved_non_integral_constant_expression_p;
10064
10065 return expression;
10066 }
10067
10068 /* Parse __builtin_offsetof.
10069
10070 offsetof-expression:
10071 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10072
10073 offsetof-member-designator:
10074 id-expression
10075 | offsetof-member-designator "." id-expression
10076 | offsetof-member-designator "[" expression "]"
10077 | offsetof-member-designator "->" id-expression */
10078
10079 static cp_expr
10080 cp_parser_builtin_offsetof (cp_parser *parser)
10081 {
10082 int save_ice_p, save_non_ice_p;
10083 tree type;
10084 cp_expr expr;
10085 cp_id_kind dummy;
10086 cp_token *token;
10087 location_t finish_loc;
10088
10089 /* We're about to accept non-integral-constant things, but will
10090 definitely yield an integral constant expression. Save and
10091 restore these values around our local parsing. */
10092 save_ice_p = parser->integral_constant_expression_p;
10093 save_non_ice_p = parser->non_integral_constant_expression_p;
10094
10095 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10096
10097 /* Consume the "__builtin_offsetof" token. */
10098 cp_lexer_consume_token (parser->lexer);
10099 /* Consume the opening `('. */
10100 matching_parens parens;
10101 parens.require_open (parser);
10102 /* Parse the type-id. */
10103 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10104 {
10105 const char *saved_message = parser->type_definition_forbidden_message;
10106 parser->type_definition_forbidden_message
10107 = G_("types may not be defined within %<__builtin_offsetof%>");
10108 type = cp_parser_type_id (parser);
10109 parser->type_definition_forbidden_message = saved_message;
10110 }
10111 /* Look for the `,'. */
10112 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10113 token = cp_lexer_peek_token (parser->lexer);
10114
10115 /* Build the (type *)null that begins the traditional offsetof macro. */
10116 tree object_ptr
10117 = build_static_cast (build_pointer_type (type), null_pointer_node,
10118 tf_warning_or_error);
10119
10120 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10121 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10122 true, &dummy, token->location);
10123 while (true)
10124 {
10125 token = cp_lexer_peek_token (parser->lexer);
10126 switch (token->type)
10127 {
10128 case CPP_OPEN_SQUARE:
10129 /* offsetof-member-designator "[" expression "]" */
10130 expr = cp_parser_postfix_open_square_expression (parser, expr,
10131 true, false);
10132 break;
10133
10134 case CPP_DEREF:
10135 /* offsetof-member-designator "->" identifier */
10136 expr = grok_array_decl (token->location, expr,
10137 integer_zero_node, false);
10138 /* FALLTHRU */
10139
10140 case CPP_DOT:
10141 /* offsetof-member-designator "." identifier */
10142 cp_lexer_consume_token (parser->lexer);
10143 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10144 expr, true, &dummy,
10145 token->location);
10146 break;
10147
10148 case CPP_CLOSE_PAREN:
10149 /* Consume the ")" token. */
10150 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10151 cp_lexer_consume_token (parser->lexer);
10152 goto success;
10153
10154 default:
10155 /* Error. We know the following require will fail, but
10156 that gives the proper error message. */
10157 parens.require_close (parser);
10158 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10159 expr = error_mark_node;
10160 goto failure;
10161 }
10162 }
10163
10164 success:
10165 /* Make a location of the form:
10166 __builtin_offsetof (struct s, f)
10167 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10168 with caret at the type-id, ranging from the start of the
10169 "_builtin_offsetof" token to the close paren. */
10170 loc = make_location (loc, start_loc, finish_loc);
10171 /* The result will be an INTEGER_CST, so we need to explicitly
10172 preserve the location. */
10173 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10174
10175 failure:
10176 parser->integral_constant_expression_p = save_ice_p;
10177 parser->non_integral_constant_expression_p = save_non_ice_p;
10178
10179 expr = expr.maybe_add_location_wrapper ();
10180 return expr;
10181 }
10182
10183 /* Parse a trait expression.
10184
10185 Returns a representation of the expression, the underlying type
10186 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10187
10188 static cp_expr
10189 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10190 {
10191 cp_trait_kind kind;
10192 tree type1, type2 = NULL_TREE;
10193 bool binary = false;
10194 bool variadic = false;
10195
10196 switch (keyword)
10197 {
10198 case RID_HAS_NOTHROW_ASSIGN:
10199 kind = CPTK_HAS_NOTHROW_ASSIGN;
10200 break;
10201 case RID_HAS_NOTHROW_CONSTRUCTOR:
10202 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10203 break;
10204 case RID_HAS_NOTHROW_COPY:
10205 kind = CPTK_HAS_NOTHROW_COPY;
10206 break;
10207 case RID_HAS_TRIVIAL_ASSIGN:
10208 kind = CPTK_HAS_TRIVIAL_ASSIGN;
10209 break;
10210 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10211 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10212 break;
10213 case RID_HAS_TRIVIAL_COPY:
10214 kind = CPTK_HAS_TRIVIAL_COPY;
10215 break;
10216 case RID_HAS_TRIVIAL_DESTRUCTOR:
10217 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10218 break;
10219 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10220 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10221 break;
10222 case RID_HAS_VIRTUAL_DESTRUCTOR:
10223 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10224 break;
10225 case RID_IS_ABSTRACT:
10226 kind = CPTK_IS_ABSTRACT;
10227 break;
10228 case RID_IS_AGGREGATE:
10229 kind = CPTK_IS_AGGREGATE;
10230 break;
10231 case RID_IS_BASE_OF:
10232 kind = CPTK_IS_BASE_OF;
10233 binary = true;
10234 break;
10235 case RID_IS_CLASS:
10236 kind = CPTK_IS_CLASS;
10237 break;
10238 case RID_IS_EMPTY:
10239 kind = CPTK_IS_EMPTY;
10240 break;
10241 case RID_IS_ENUM:
10242 kind = CPTK_IS_ENUM;
10243 break;
10244 case RID_IS_FINAL:
10245 kind = CPTK_IS_FINAL;
10246 break;
10247 case RID_IS_LITERAL_TYPE:
10248 kind = CPTK_IS_LITERAL_TYPE;
10249 break;
10250 case RID_IS_POD:
10251 kind = CPTK_IS_POD;
10252 break;
10253 case RID_IS_POLYMORPHIC:
10254 kind = CPTK_IS_POLYMORPHIC;
10255 break;
10256 case RID_IS_SAME_AS:
10257 kind = CPTK_IS_SAME_AS;
10258 binary = true;
10259 break;
10260 case RID_IS_STD_LAYOUT:
10261 kind = CPTK_IS_STD_LAYOUT;
10262 break;
10263 case RID_IS_TRIVIAL:
10264 kind = CPTK_IS_TRIVIAL;
10265 break;
10266 case RID_IS_TRIVIALLY_ASSIGNABLE:
10267 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10268 binary = true;
10269 break;
10270 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10271 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10272 variadic = true;
10273 break;
10274 case RID_IS_TRIVIALLY_COPYABLE:
10275 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10276 break;
10277 case RID_IS_UNION:
10278 kind = CPTK_IS_UNION;
10279 break;
10280 case RID_UNDERLYING_TYPE:
10281 kind = CPTK_UNDERLYING_TYPE;
10282 break;
10283 case RID_BASES:
10284 kind = CPTK_BASES;
10285 break;
10286 case RID_DIRECT_BASES:
10287 kind = CPTK_DIRECT_BASES;
10288 break;
10289 case RID_IS_ASSIGNABLE:
10290 kind = CPTK_IS_ASSIGNABLE;
10291 binary = true;
10292 break;
10293 case RID_IS_CONSTRUCTIBLE:
10294 kind = CPTK_IS_CONSTRUCTIBLE;
10295 variadic = true;
10296 break;
10297 default:
10298 gcc_unreachable ();
10299 }
10300
10301 /* Get location of initial token. */
10302 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10303
10304 /* Consume the token. */
10305 cp_lexer_consume_token (parser->lexer);
10306
10307 matching_parens parens;
10308 parens.require_open (parser);
10309
10310 {
10311 type_id_in_expr_sentinel s (parser);
10312 type1 = cp_parser_type_id (parser);
10313 }
10314
10315 if (type1 == error_mark_node)
10316 return error_mark_node;
10317
10318 if (binary)
10319 {
10320 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10321
10322 {
10323 type_id_in_expr_sentinel s (parser);
10324 type2 = cp_parser_type_id (parser);
10325 }
10326
10327 if (type2 == error_mark_node)
10328 return error_mark_node;
10329 }
10330 else if (variadic)
10331 {
10332 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10333 {
10334 cp_lexer_consume_token (parser->lexer);
10335 tree elt = cp_parser_type_id (parser);
10336 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10337 {
10338 cp_lexer_consume_token (parser->lexer);
10339 elt = make_pack_expansion (elt);
10340 }
10341 if (elt == error_mark_node)
10342 return error_mark_node;
10343 type2 = tree_cons (NULL_TREE, elt, type2);
10344 }
10345 }
10346
10347 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10348 parens.require_close (parser);
10349
10350 /* Construct a location of the form:
10351 __is_trivially_copyable(_Tp)
10352 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10353 with start == caret, finishing at the close-paren. */
10354 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10355
10356 /* Complete the trait expression, which may mean either processing
10357 the trait expr now or saving it for template instantiation. */
10358 switch (kind)
10359 {
10360 case CPTK_UNDERLYING_TYPE:
10361 return cp_expr (finish_underlying_type (type1), trait_loc);
10362 case CPTK_BASES:
10363 return cp_expr (finish_bases (type1, false), trait_loc);
10364 case CPTK_DIRECT_BASES:
10365 return cp_expr (finish_bases (type1, true), trait_loc);
10366 default:
10367 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10368 }
10369 }
10370
10371 /* Parse a lambda expression.
10372
10373 lambda-expression:
10374 lambda-introducer lambda-declarator [opt] compound-statement
10375
10376 Returns a representation of the expression. */
10377
10378 static cp_expr
10379 cp_parser_lambda_expression (cp_parser* parser)
10380 {
10381 tree lambda_expr = build_lambda_expr ();
10382 tree type;
10383 bool ok = true;
10384 cp_token *token = cp_lexer_peek_token (parser->lexer);
10385 cp_token_position start = 0;
10386
10387 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10388
10389 if (cxx_dialect >= cxx2a)
10390 /* C++20 allows lambdas in unevaluated context. */;
10391 else if (cp_unevaluated_operand)
10392 {
10393 if (!token->error_reported)
10394 {
10395 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10396 "lambda-expression in unevaluated context"
10397 " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10398 token->error_reported = true;
10399 }
10400 ok = false;
10401 }
10402 else if (parser->in_template_argument_list_p || processing_template_parmlist)
10403 {
10404 if (!token->error_reported)
10405 {
10406 error_at (token->location, "lambda-expression in template-argument"
10407 " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10408 token->error_reported = true;
10409 }
10410 ok = false;
10411 }
10412
10413 /* We may be in the middle of deferred access check. Disable
10414 it now. */
10415 push_deferring_access_checks (dk_no_deferred);
10416
10417 cp_parser_lambda_introducer (parser, lambda_expr);
10418 if (cp_parser_error_occurred (parser))
10419 return error_mark_node;
10420
10421 type = begin_lambda_type (lambda_expr);
10422 if (type == error_mark_node)
10423 return error_mark_node;
10424
10425 record_lambda_scope (lambda_expr);
10426
10427 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10428 determine_visibility (TYPE_NAME (type));
10429
10430 /* Now that we've started the type, add the capture fields for any
10431 explicit captures. */
10432 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10433
10434 {
10435 /* Inside the class, surrounding template-parameter-lists do not apply. */
10436 unsigned int saved_num_template_parameter_lists
10437 = parser->num_template_parameter_lists;
10438 unsigned char in_statement = parser->in_statement;
10439 bool in_switch_statement_p = parser->in_switch_statement_p;
10440 bool fully_implicit_function_template_p
10441 = parser->fully_implicit_function_template_p;
10442 tree implicit_template_parms = parser->implicit_template_parms;
10443 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10444 bool auto_is_implicit_function_template_parm_p
10445 = parser->auto_is_implicit_function_template_parm_p;
10446
10447 parser->num_template_parameter_lists = 0;
10448 parser->in_statement = 0;
10449 parser->in_switch_statement_p = false;
10450 parser->fully_implicit_function_template_p = false;
10451 parser->implicit_template_parms = 0;
10452 parser->implicit_template_scope = 0;
10453 parser->auto_is_implicit_function_template_parm_p = false;
10454
10455 /* By virtue of defining a local class, a lambda expression has access to
10456 the private variables of enclosing classes. */
10457
10458 if (cp_parser_start_tentative_firewall (parser))
10459 start = token;
10460
10461 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10462
10463 if (ok && cp_parser_error_occurred (parser))
10464 ok = false;
10465
10466 if (ok)
10467 {
10468 cp_parser_lambda_body (parser, lambda_expr);
10469 }
10470 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10471 {
10472 if (cp_parser_skip_to_closing_brace (parser))
10473 cp_lexer_consume_token (parser->lexer);
10474 }
10475
10476 /* The capture list was built up in reverse order; fix that now. */
10477 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10478 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10479
10480 if (ok)
10481 maybe_add_lambda_conv_op (type);
10482
10483 type = finish_struct (type, /*attributes=*/NULL_TREE);
10484
10485 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10486 parser->in_statement = in_statement;
10487 parser->in_switch_statement_p = in_switch_statement_p;
10488 parser->fully_implicit_function_template_p
10489 = fully_implicit_function_template_p;
10490 parser->implicit_template_parms = implicit_template_parms;
10491 parser->implicit_template_scope = implicit_template_scope;
10492 parser->auto_is_implicit_function_template_parm_p
10493 = auto_is_implicit_function_template_parm_p;
10494 }
10495
10496 /* This field is only used during parsing of the lambda. */
10497 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10498
10499 /* This lambda shouldn't have any proxies left at this point. */
10500 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10501 /* And now that we're done, push proxies for an enclosing lambda. */
10502 insert_pending_capture_proxies ();
10503
10504 /* Update the lambda expression to a range. */
10505 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10506 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10507 token->location,
10508 end_tok->location);
10509
10510 if (ok)
10511 lambda_expr = build_lambda_object (lambda_expr);
10512 else
10513 lambda_expr = error_mark_node;
10514
10515 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10516
10517 pop_deferring_access_checks ();
10518
10519 return lambda_expr;
10520 }
10521
10522 /* Parse the beginning of a lambda expression.
10523
10524 lambda-introducer:
10525 [ lambda-capture [opt] ]
10526
10527 LAMBDA_EXPR is the current representation of the lambda expression. */
10528
10529 static void
10530 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10531 {
10532 /* Need commas after the first capture. */
10533 bool first = true;
10534
10535 /* Eat the leading `['. */
10536 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10537
10538 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10539 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10540 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10541 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10542 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10543 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10544
10545 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10546 {
10547 cp_lexer_consume_token (parser->lexer);
10548 first = false;
10549
10550 if (!(at_function_scope_p () || parsing_nsdmi ()))
10551 error ("non-local lambda expression cannot have a capture-default");
10552 }
10553
10554 hash_set<tree, true> ids;
10555 tree first_capture_id = NULL_TREE;
10556 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10557 {
10558 cp_token* capture_token;
10559 tree capture_id;
10560 tree capture_init_expr;
10561 cp_id_kind idk = CP_ID_KIND_NONE;
10562 bool explicit_init_p = false;
10563
10564 enum capture_kind_type
10565 {
10566 BY_COPY,
10567 BY_REFERENCE
10568 };
10569 enum capture_kind_type capture_kind = BY_COPY;
10570
10571 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10572 {
10573 error ("expected end of capture-list");
10574 return;
10575 }
10576
10577 if (first)
10578 first = false;
10579 else
10580 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10581
10582 /* Possibly capture `this'. */
10583 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10584 {
10585 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10586 if (cxx_dialect < cxx2a
10587 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10588 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10589 "with by-copy capture default");
10590 cp_lexer_consume_token (parser->lexer);
10591 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10592 pedwarn (input_location, 0,
10593 "already captured %qD in lambda expression",
10594 this_identifier);
10595 else
10596 add_capture (lambda_expr, /*id=*/this_identifier,
10597 /*initializer=*/finish_this_expr (),
10598 /*by_reference_p=*/true, explicit_init_p);
10599 continue;
10600 }
10601
10602 /* Possibly capture `*this'. */
10603 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10604 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10605 {
10606 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10607 if (cxx_dialect < cxx17)
10608 pedwarn (loc, 0, "%<*this%> capture only available with "
10609 "%<-std=c++17%> or %<-std=gnu++17%>");
10610 cp_lexer_consume_token (parser->lexer);
10611 cp_lexer_consume_token (parser->lexer);
10612 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10613 pedwarn (input_location, 0,
10614 "already captured %qD in lambda expression",
10615 this_identifier);
10616 else
10617 add_capture (lambda_expr, /*id=*/this_identifier,
10618 /*initializer=*/finish_this_expr (),
10619 /*by_reference_p=*/false, explicit_init_p);
10620 continue;
10621 }
10622
10623 bool init_pack_expansion = false;
10624 location_t ellipsis_loc = UNKNOWN_LOCATION;
10625 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10626 {
10627 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
10628 if (cxx_dialect < cxx2a)
10629 pedwarn (ellipsis_loc, 0, "pack init-capture only available with "
10630 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10631 cp_lexer_consume_token (parser->lexer);
10632 init_pack_expansion = true;
10633 }
10634
10635 /* Remember whether we want to capture as a reference or not. */
10636 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10637 {
10638 capture_kind = BY_REFERENCE;
10639 cp_lexer_consume_token (parser->lexer);
10640 }
10641
10642 /* Get the identifier. */
10643 capture_token = cp_lexer_peek_token (parser->lexer);
10644 capture_id = cp_parser_identifier (parser);
10645
10646 if (capture_id == error_mark_node)
10647 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10648 delimiters, but I modified this to stop on unnested ']' as well. It
10649 was already changed to stop on unnested '}', so the
10650 "closing_parenthesis" name is no more misleading with my change. */
10651 {
10652 cp_parser_skip_to_closing_parenthesis (parser,
10653 /*recovering=*/true,
10654 /*or_comma=*/true,
10655 /*consume_paren=*/true);
10656 break;
10657 }
10658
10659 /* Find the initializer for this capture. */
10660 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10661 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10662 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10663 {
10664 bool direct, non_constant;
10665 /* An explicit initializer exists. */
10666 if (cxx_dialect < cxx14)
10667 pedwarn (input_location, 0,
10668 "lambda capture initializers "
10669 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
10670 capture_init_expr = cp_parser_initializer (parser, &direct,
10671 &non_constant, true);
10672 explicit_init_p = true;
10673 if (capture_init_expr == NULL_TREE)
10674 {
10675 error ("empty initializer for lambda init-capture");
10676 capture_init_expr = error_mark_node;
10677 }
10678 if (init_pack_expansion)
10679 capture_init_expr = make_pack_expansion (capture_init_expr);
10680 }
10681 else
10682 {
10683 const char* error_msg;
10684
10685 /* Turn the identifier into an id-expression. */
10686 capture_init_expr
10687 = cp_parser_lookup_name_simple (parser, capture_id,
10688 capture_token->location);
10689
10690 if (capture_init_expr == error_mark_node)
10691 {
10692 unqualified_name_lookup_error (capture_id);
10693 continue;
10694 }
10695 else if (!VAR_P (capture_init_expr)
10696 && TREE_CODE (capture_init_expr) != PARM_DECL)
10697 {
10698 error_at (capture_token->location,
10699 "capture of non-variable %qE",
10700 capture_init_expr);
10701 if (DECL_P (capture_init_expr))
10702 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10703 "%q#D declared here", capture_init_expr);
10704 continue;
10705 }
10706 if (VAR_P (capture_init_expr)
10707 && decl_storage_duration (capture_init_expr) != dk_auto)
10708 {
10709 if (pedwarn (capture_token->location, 0, "capture of variable "
10710 "%qD with non-automatic storage duration",
10711 capture_init_expr))
10712 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10713 "%q#D declared here", capture_init_expr);
10714 continue;
10715 }
10716
10717 capture_init_expr
10718 = finish_id_expression
10719 (capture_id,
10720 capture_init_expr,
10721 parser->scope,
10722 &idk,
10723 /*integral_constant_expression_p=*/false,
10724 /*allow_non_integral_constant_expression_p=*/false,
10725 /*non_integral_constant_expression_p=*/NULL,
10726 /*template_p=*/false,
10727 /*done=*/true,
10728 /*address_p=*/false,
10729 /*template_arg_p=*/false,
10730 &error_msg,
10731 capture_token->location);
10732
10733 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10734 {
10735 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10736 cp_lexer_consume_token (parser->lexer);
10737 capture_init_expr = make_pack_expansion (capture_init_expr);
10738 if (init_pack_expansion)
10739 {
10740 /* If what follows is an initializer, the second '...' is
10741 invalid. But for cases like [...xs...], the first one
10742 is invalid. */
10743 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10744 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10745 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10746 ellipsis_loc = loc;
10747 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
10748 continue;
10749 }
10750 }
10751 }
10752
10753 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10754 && !explicit_init_p)
10755 {
10756 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10757 && capture_kind == BY_COPY)
10758 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10759 "of %qD redundant with by-copy capture default",
10760 capture_id);
10761 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10762 && capture_kind == BY_REFERENCE)
10763 pedwarn (capture_token->location, 0, "explicit by-reference "
10764 "capture of %qD redundant with by-reference capture "
10765 "default", capture_id);
10766 }
10767
10768 /* Check for duplicates.
10769 Optimize for the zero or one explicit captures cases and only create
10770 the hash_set after adding second capture. */
10771 bool found = false;
10772 if (!ids.is_empty ())
10773 found = ids.add (capture_id);
10774 else if (first_capture_id == NULL_TREE)
10775 first_capture_id = capture_id;
10776 else if (capture_id == first_capture_id)
10777 found = true;
10778 else
10779 {
10780 ids.add (first_capture_id);
10781 ids.add (capture_id);
10782 }
10783 if (found)
10784 pedwarn (input_location, 0,
10785 "already captured %qD in lambda expression", capture_id);
10786 else
10787 add_capture (lambda_expr, capture_id, capture_init_expr,
10788 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10789 explicit_init_p);
10790
10791 /* If there is any qualification still in effect, clear it
10792 now; we will be starting fresh with the next capture. */
10793 parser->scope = NULL_TREE;
10794 parser->qualifying_scope = NULL_TREE;
10795 parser->object_scope = NULL_TREE;
10796 }
10797
10798 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10799 }
10800
10801 /* Parse the (optional) middle of a lambda expression.
10802
10803 lambda-declarator:
10804 < template-parameter-list [opt] >
10805 ( parameter-declaration-clause [opt] )
10806 attribute-specifier [opt]
10807 decl-specifier-seq [opt]
10808 exception-specification [opt]
10809 lambda-return-type-clause [opt]
10810
10811 LAMBDA_EXPR is the current representation of the lambda expression. */
10812
10813 static bool
10814 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10815 {
10816 /* 5.1.1.4 of the standard says:
10817 If a lambda-expression does not include a lambda-declarator, it is as if
10818 the lambda-declarator were ().
10819 This means an empty parameter list, no attributes, and no exception
10820 specification. */
10821 tree param_list = void_list_node;
10822 tree std_attrs = NULL_TREE;
10823 tree gnu_attrs = NULL_TREE;
10824 tree exception_spec = NULL_TREE;
10825 tree template_param_list = NULL_TREE;
10826 tree tx_qual = NULL_TREE;
10827 tree return_type = NULL_TREE;
10828 cp_decl_specifier_seq lambda_specs;
10829 clear_decl_specs (&lambda_specs);
10830
10831 /* The template-parameter-list is optional, but must begin with
10832 an opening angle if present. */
10833 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10834 {
10835 if (cxx_dialect < cxx14)
10836 pedwarn (parser->lexer->next_token->location, 0,
10837 "lambda templates are only available with "
10838 "%<-std=c++14%> or %<-std=gnu++14%>");
10839 else if (cxx_dialect < cxx2a)
10840 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10841 "lambda templates are only available with "
10842 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10843
10844 cp_lexer_consume_token (parser->lexer);
10845
10846 template_param_list = cp_parser_template_parameter_list (parser);
10847
10848 cp_parser_skip_to_end_of_template_parameter_list (parser);
10849
10850 /* We just processed one more parameter list. */
10851 ++parser->num_template_parameter_lists;
10852 }
10853
10854 /* The parameter-declaration-clause is optional (unless
10855 template-parameter-list was given), but must begin with an
10856 opening parenthesis if present. */
10857 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10858 {
10859 matching_parens parens;
10860 parens.consume_open (parser);
10861
10862 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10863
10864 /* Parse parameters. */
10865 param_list
10866 = cp_parser_parameter_declaration_clause
10867 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
10868
10869 /* Default arguments shall not be specified in the
10870 parameter-declaration-clause of a lambda-declarator. */
10871 if (cxx_dialect < cxx14)
10872 for (tree t = param_list; t; t = TREE_CHAIN (t))
10873 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10874 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10875 "default argument specified for lambda parameter");
10876
10877 parens.require_close (parser);
10878
10879 /* In the decl-specifier-seq of the lambda-declarator, each
10880 decl-specifier shall either be mutable or constexpr. */
10881 int declares_class_or_enum;
10882 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
10883 && !cp_next_tokens_can_be_gnu_attribute_p (parser))
10884 cp_parser_decl_specifier_seq (parser,
10885 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10886 &lambda_specs, &declares_class_or_enum);
10887 if (lambda_specs.storage_class == sc_mutable)
10888 {
10889 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10890 if (lambda_specs.conflicting_specifiers_p)
10891 error_at (lambda_specs.locations[ds_storage_class],
10892 "duplicate %<mutable%>");
10893 }
10894
10895 tx_qual = cp_parser_tx_qualifier_opt (parser);
10896
10897 /* Parse optional exception specification. */
10898 exception_spec = cp_parser_exception_specification_opt (parser);
10899
10900 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10901
10902 /* Parse optional trailing return type. */
10903 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10904 {
10905 cp_lexer_consume_token (parser->lexer);
10906 return_type = cp_parser_trailing_type_id (parser);
10907 }
10908
10909 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
10910 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
10911
10912 /* The function parameters must be in scope all the way until after the
10913 trailing-return-type in case of decltype. */
10914 pop_bindings_and_leave_scope ();
10915 }
10916 else if (template_param_list != NULL_TREE) // generate diagnostic
10917 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10918
10919 /* Create the function call operator.
10920
10921 Messing with declarators like this is no uglier than building up the
10922 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10923 other code. */
10924 {
10925 cp_decl_specifier_seq return_type_specs;
10926 cp_declarator* declarator;
10927 tree fco;
10928 int quals;
10929 void *p;
10930
10931 clear_decl_specs (&return_type_specs);
10932 return_type_specs.type = make_auto ();
10933
10934 if (lambda_specs.locations[ds_constexpr])
10935 {
10936 if (cxx_dialect >= cxx17)
10937 return_type_specs.locations[ds_constexpr]
10938 = lambda_specs.locations[ds_constexpr];
10939 else
10940 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10941 "lambda only available with %<-std=c++17%> or "
10942 "%<-std=gnu++17%>");
10943 }
10944
10945 p = obstack_alloc (&declarator_obstack, 0);
10946
10947 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
10948 LAMBDA_EXPR_LOCATION (lambda_expr));
10949
10950 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10951 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10952 declarator = make_call_declarator (declarator, param_list, quals,
10953 VIRT_SPEC_UNSPECIFIED,
10954 REF_QUAL_NONE,
10955 tx_qual,
10956 exception_spec,
10957 return_type,
10958 /*requires_clause*/NULL_TREE);
10959 declarator->std_attributes = std_attrs;
10960
10961 fco = grokmethod (&return_type_specs,
10962 declarator,
10963 gnu_attrs);
10964 if (fco != error_mark_node)
10965 {
10966 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10967 DECL_ARTIFICIAL (fco) = 1;
10968 /* Give the object parameter a different name. */
10969 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10970 DECL_LAMBDA_FUNCTION (fco) = 1;
10971 }
10972 if (template_param_list)
10973 {
10974 fco = finish_member_template_decl (fco);
10975 finish_template_decl (template_param_list);
10976 --parser->num_template_parameter_lists;
10977 }
10978 else if (parser->fully_implicit_function_template_p)
10979 fco = finish_fully_implicit_template (parser, fco);
10980
10981 finish_member_declaration (fco);
10982
10983 obstack_free (&declarator_obstack, p);
10984
10985 return (fco != error_mark_node);
10986 }
10987 }
10988
10989 /* Parse the body of a lambda expression, which is simply
10990
10991 compound-statement
10992
10993 but which requires special handling.
10994 LAMBDA_EXPR is the current representation of the lambda expression. */
10995
10996 static void
10997 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10998 {
10999 bool nested = (current_function_decl != NULL_TREE);
11000 unsigned char local_variables_forbidden_p
11001 = parser->local_variables_forbidden_p;
11002 bool in_function_body = parser->in_function_body;
11003
11004 /* The body of a lambda-expression is not a subexpression of the enclosing
11005 expression. */
11006 cp_evaluated ev;
11007
11008 if (nested)
11009 push_function_context ();
11010 else
11011 /* Still increment function_depth so that we don't GC in the
11012 middle of an expression. */
11013 ++function_depth;
11014
11015 vec<tree> omp_privatization_save;
11016 save_omp_privatization_clauses (omp_privatization_save);
11017 /* Clear this in case we're in the middle of a default argument. */
11018 parser->local_variables_forbidden_p = 0;
11019 parser->in_function_body = true;
11020
11021 {
11022 local_specialization_stack s (lss_copy);
11023 tree fco = lambda_function (lambda_expr);
11024 tree body = start_lambda_function (fco, lambda_expr);
11025 matching_braces braces;
11026
11027 if (braces.require_open (parser))
11028 {
11029 tree compound_stmt = begin_compound_stmt (0);
11030
11031 /* Originally C++11 required us to peek for 'return expr'; and
11032 process it specially here to deduce the return type. N3638
11033 removed the need for that. */
11034
11035 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11036 cp_parser_label_declaration (parser);
11037 cp_parser_statement_seq_opt (parser, NULL_TREE);
11038 braces.require_close (parser);
11039
11040 finish_compound_stmt (compound_stmt);
11041 }
11042
11043 finish_lambda_function (body);
11044 }
11045
11046 restore_omp_privatization_clauses (omp_privatization_save);
11047 parser->local_variables_forbidden_p = local_variables_forbidden_p;
11048 parser->in_function_body = in_function_body;
11049 if (nested)
11050 pop_function_context();
11051 else
11052 --function_depth;
11053 }
11054
11055 /* Statements [gram.stmt.stmt] */
11056
11057 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11058
11059 static void
11060 add_debug_begin_stmt (location_t loc)
11061 {
11062 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11063 return;
11064 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11065 /* A concept is never expanded normally. */
11066 return;
11067
11068 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11069 SET_EXPR_LOCATION (stmt, loc);
11070 add_stmt (stmt);
11071 }
11072
11073 /* Parse a statement.
11074
11075 statement:
11076 labeled-statement
11077 expression-statement
11078 compound-statement
11079 selection-statement
11080 iteration-statement
11081 jump-statement
11082 declaration-statement
11083 try-block
11084
11085 C++11:
11086
11087 statement:
11088 labeled-statement
11089 attribute-specifier-seq (opt) expression-statement
11090 attribute-specifier-seq (opt) compound-statement
11091 attribute-specifier-seq (opt) selection-statement
11092 attribute-specifier-seq (opt) iteration-statement
11093 attribute-specifier-seq (opt) jump-statement
11094 declaration-statement
11095 attribute-specifier-seq (opt) try-block
11096
11097 init-statement:
11098 expression-statement
11099 simple-declaration
11100
11101 TM Extension:
11102
11103 statement:
11104 atomic-statement
11105
11106 IN_COMPOUND is true when the statement is nested inside a
11107 cp_parser_compound_statement; this matters for certain pragmas.
11108
11109 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11110 is a (possibly labeled) if statement which is not enclosed in braces
11111 and has an else clause. This is used to implement -Wparentheses.
11112
11113 CHAIN is a vector of if-else-if conditions. */
11114
11115 static void
11116 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
11117 bool in_compound, bool *if_p, vec<tree> *chain,
11118 location_t *loc_after_labels)
11119 {
11120 tree statement, std_attrs = NULL_TREE;
11121 cp_token *token;
11122 location_t statement_location, attrs_loc;
11123
11124 restart:
11125 if (if_p != NULL)
11126 *if_p = false;
11127 /* There is no statement yet. */
11128 statement = NULL_TREE;
11129
11130 saved_token_sentinel saved_tokens (parser->lexer);
11131 attrs_loc = cp_lexer_peek_token (parser->lexer)->location;
11132 if (c_dialect_objc ())
11133 /* In obj-c++, seeing '[[' might be the either the beginning of
11134 c++11 attributes, or a nested objc-message-expression. So
11135 let's parse the c++11 attributes tentatively. */
11136 cp_parser_parse_tentatively (parser);
11137 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11138 if (std_attrs)
11139 {
11140 location_t end_loc
11141 = cp_lexer_previous_token (parser->lexer)->location;
11142 attrs_loc = make_location (attrs_loc, attrs_loc, end_loc);
11143 }
11144 if (c_dialect_objc ())
11145 {
11146 if (!cp_parser_parse_definitely (parser))
11147 std_attrs = NULL_TREE;
11148 }
11149
11150 /* Peek at the next token. */
11151 token = cp_lexer_peek_token (parser->lexer);
11152 /* Remember the location of the first token in the statement. */
11153 cp_token *statement_token = token;
11154 statement_location = token->location;
11155 add_debug_begin_stmt (statement_location);
11156 /* If this is a keyword, then that will often determine what kind of
11157 statement we have. */
11158 if (token->type == CPP_KEYWORD)
11159 {
11160 enum rid keyword = token->keyword;
11161
11162 switch (keyword)
11163 {
11164 case RID_CASE:
11165 case RID_DEFAULT:
11166 /* Looks like a labeled-statement with a case label.
11167 Parse the label, and then use tail recursion to parse
11168 the statement. */
11169 cp_parser_label_for_labeled_statement (parser, std_attrs);
11170 in_compound = false;
11171 goto restart;
11172
11173 case RID_IF:
11174 case RID_SWITCH:
11175 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11176 statement = cp_parser_selection_statement (parser, if_p, chain);
11177 break;
11178
11179 case RID_WHILE:
11180 case RID_DO:
11181 case RID_FOR:
11182 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11183 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
11184 break;
11185
11186 case RID_BREAK:
11187 case RID_CONTINUE:
11188 case RID_RETURN:
11189 case RID_GOTO:
11190 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11191 statement = cp_parser_jump_statement (parser);
11192 break;
11193
11194 /* Objective-C++ exception-handling constructs. */
11195 case RID_AT_TRY:
11196 case RID_AT_CATCH:
11197 case RID_AT_FINALLY:
11198 case RID_AT_SYNCHRONIZED:
11199 case RID_AT_THROW:
11200 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11201 statement = cp_parser_objc_statement (parser);
11202 break;
11203
11204 case RID_TRY:
11205 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11206 statement = cp_parser_try_block (parser);
11207 break;
11208
11209 case RID_NAMESPACE:
11210 /* This must be a namespace alias definition. */
11211 if (std_attrs != NULL_TREE)
11212 {
11213 /* Attributes should be parsed as part of the the
11214 declaration, so let's un-parse them. */
11215 saved_tokens.rollback();
11216 std_attrs = NULL_TREE;
11217 }
11218 cp_parser_declaration_statement (parser);
11219 return;
11220
11221 case RID_TRANSACTION_ATOMIC:
11222 case RID_TRANSACTION_RELAXED:
11223 case RID_SYNCHRONIZED:
11224 case RID_ATOMIC_NOEXCEPT:
11225 case RID_ATOMIC_CANCEL:
11226 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11227 statement = cp_parser_transaction (parser, token);
11228 break;
11229 case RID_TRANSACTION_CANCEL:
11230 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11231 statement = cp_parser_transaction_cancel (parser);
11232 break;
11233
11234 default:
11235 /* It might be a keyword like `int' that can start a
11236 declaration-statement. */
11237 break;
11238 }
11239 }
11240 else if (token->type == CPP_NAME)
11241 {
11242 /* If the next token is a `:', then we are looking at a
11243 labeled-statement. */
11244 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11245 if (token->type == CPP_COLON)
11246 {
11247 /* Looks like a labeled-statement with an ordinary label.
11248 Parse the label, and then use tail recursion to parse
11249 the statement. */
11250
11251 cp_parser_label_for_labeled_statement (parser, std_attrs);
11252 in_compound = false;
11253 goto restart;
11254 }
11255 }
11256 /* Anything that starts with a `{' must be a compound-statement. */
11257 else if (token->type == CPP_OPEN_BRACE)
11258 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11259 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11260 a statement all its own. */
11261 else if (token->type == CPP_PRAGMA)
11262 {
11263 /* Only certain OpenMP pragmas are attached to statements, and thus
11264 are considered statements themselves. All others are not. In
11265 the context of a compound, accept the pragma as a "statement" and
11266 return so that we can check for a close brace. Otherwise we
11267 require a real statement and must go back and read one. */
11268 if (in_compound)
11269 cp_parser_pragma (parser, pragma_compound, if_p);
11270 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11271 goto restart;
11272 return;
11273 }
11274 else if (token->type == CPP_EOF)
11275 {
11276 cp_parser_error (parser, "expected statement");
11277 return;
11278 }
11279
11280 /* Everything else must be a declaration-statement or an
11281 expression-statement. Try for the declaration-statement
11282 first, unless we are looking at a `;', in which case we know that
11283 we have an expression-statement. */
11284 if (!statement)
11285 {
11286 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11287 {
11288 if (std_attrs != NULL_TREE)
11289 /* Attributes should be parsed as part of the declaration,
11290 so let's un-parse them. */
11291 saved_tokens.rollback();
11292
11293 cp_parser_parse_tentatively (parser);
11294 /* Try to parse the declaration-statement. */
11295 cp_parser_declaration_statement (parser);
11296 /* If that worked, we're done. */
11297 if (cp_parser_parse_definitely (parser))
11298 return;
11299 /* It didn't work, restore the post-attribute position. */
11300 if (std_attrs)
11301 cp_lexer_set_token_position (parser->lexer, statement_token);
11302 }
11303 /* All preceding labels have been parsed at this point. */
11304 if (loc_after_labels != NULL)
11305 *loc_after_labels = statement_location;
11306
11307 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11308
11309 /* Look for an expression-statement instead. */
11310 statement = cp_parser_expression_statement (parser, in_statement_expr);
11311
11312 /* Handle [[fallthrough]];. */
11313 if (attribute_fallthrough_p (std_attrs))
11314 {
11315 /* The next token after the fallthrough attribute is ';'. */
11316 if (statement == NULL_TREE)
11317 {
11318 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11319 statement = build_call_expr_internal_loc (statement_location,
11320 IFN_FALLTHROUGH,
11321 void_type_node, 0);
11322 finish_expr_stmt (statement);
11323 }
11324 else
11325 warning_at (statement_location, OPT_Wattributes,
11326 "%<fallthrough%> attribute not followed by %<;%>");
11327 std_attrs = NULL_TREE;
11328 }
11329 }
11330
11331 /* Set the line number for the statement. */
11332 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11333 SET_EXPR_LOCATION (statement, statement_location);
11334
11335 /* Allow "[[fallthrough]];", but warn otherwise. */
11336 if (std_attrs != NULL_TREE)
11337 warning_at (attrs_loc,
11338 OPT_Wattributes,
11339 "attributes at the beginning of statement are ignored");
11340 }
11341
11342 /* Append ATTR to attribute list ATTRS. */
11343
11344 static tree
11345 attr_chainon (tree attrs, tree attr)
11346 {
11347 if (attrs == error_mark_node)
11348 return error_mark_node;
11349 if (attr == error_mark_node)
11350 return error_mark_node;
11351 return chainon (attrs, attr);
11352 }
11353
11354 /* Parse the label for a labeled-statement, i.e.
11355
11356 identifier :
11357 case constant-expression :
11358 default :
11359
11360 GNU Extension:
11361 case constant-expression ... constant-expression : statement
11362
11363 When a label is parsed without errors, the label is added to the
11364 parse tree by the finish_* functions, so this function doesn't
11365 have to return the label. */
11366
11367 static void
11368 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11369 {
11370 cp_token *token;
11371 tree label = NULL_TREE;
11372 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11373
11374 /* The next token should be an identifier. */
11375 token = cp_lexer_peek_token (parser->lexer);
11376 if (token->type != CPP_NAME
11377 && token->type != CPP_KEYWORD)
11378 {
11379 cp_parser_error (parser, "expected labeled-statement");
11380 return;
11381 }
11382
11383 /* Remember whether this case or a user-defined label is allowed to fall
11384 through to. */
11385 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11386
11387 parser->colon_corrects_to_scope_p = false;
11388 switch (token->keyword)
11389 {
11390 case RID_CASE:
11391 {
11392 tree expr, expr_hi;
11393 cp_token *ellipsis;
11394
11395 /* Consume the `case' token. */
11396 cp_lexer_consume_token (parser->lexer);
11397 /* Parse the constant-expression. */
11398 expr = cp_parser_constant_expression (parser);
11399 if (check_for_bare_parameter_packs (expr))
11400 expr = error_mark_node;
11401
11402 ellipsis = cp_lexer_peek_token (parser->lexer);
11403 if (ellipsis->type == CPP_ELLIPSIS)
11404 {
11405 /* Consume the `...' token. */
11406 cp_lexer_consume_token (parser->lexer);
11407 expr_hi = cp_parser_constant_expression (parser);
11408 if (check_for_bare_parameter_packs (expr_hi))
11409 expr_hi = error_mark_node;
11410
11411 /* We don't need to emit warnings here, as the common code
11412 will do this for us. */
11413 }
11414 else
11415 expr_hi = NULL_TREE;
11416
11417 if (parser->in_switch_statement_p)
11418 {
11419 tree l = finish_case_label (token->location, expr, expr_hi);
11420 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11421 {
11422 label = CASE_LABEL (l);
11423 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11424 }
11425 }
11426 else
11427 error_at (token->location,
11428 "case label %qE not within a switch statement",
11429 expr);
11430 }
11431 break;
11432
11433 case RID_DEFAULT:
11434 /* Consume the `default' token. */
11435 cp_lexer_consume_token (parser->lexer);
11436
11437 if (parser->in_switch_statement_p)
11438 {
11439 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11440 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11441 {
11442 label = CASE_LABEL (l);
11443 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11444 }
11445 }
11446 else
11447 error_at (token->location, "case label not within a switch statement");
11448 break;
11449
11450 default:
11451 /* Anything else must be an ordinary label. */
11452 label = finish_label_stmt (cp_parser_identifier (parser));
11453 if (label && TREE_CODE (label) == LABEL_DECL)
11454 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11455 break;
11456 }
11457
11458 /* Require the `:' token. */
11459 cp_parser_require (parser, CPP_COLON, RT_COLON);
11460
11461 /* An ordinary label may optionally be followed by attributes.
11462 However, this is only permitted if the attributes are then
11463 followed by a semicolon. This is because, for backward
11464 compatibility, when parsing
11465 lab: __attribute__ ((unused)) int i;
11466 we want the attribute to attach to "i", not "lab". */
11467 if (label != NULL_TREE
11468 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11469 {
11470 tree attrs;
11471 cp_parser_parse_tentatively (parser);
11472 attrs = cp_parser_gnu_attributes_opt (parser);
11473 if (attrs == NULL_TREE
11474 /* And fallthrough always binds to the expression-statement. */
11475 || attribute_fallthrough_p (attrs)
11476 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11477 cp_parser_abort_tentative_parse (parser);
11478 else if (!cp_parser_parse_definitely (parser))
11479 ;
11480 else
11481 attributes = attr_chainon (attributes, attrs);
11482 }
11483
11484 if (attributes != NULL_TREE)
11485 cplus_decl_attributes (&label, attributes, 0);
11486
11487 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11488 }
11489
11490 /* Parse an expression-statement.
11491
11492 expression-statement:
11493 expression [opt] ;
11494
11495 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11496 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11497 indicates whether this expression-statement is part of an
11498 expression statement. */
11499
11500 static tree
11501 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11502 {
11503 tree statement = NULL_TREE;
11504 cp_token *token = cp_lexer_peek_token (parser->lexer);
11505 location_t loc = token->location;
11506
11507 /* There might be attribute fallthrough. */
11508 tree attr = cp_parser_gnu_attributes_opt (parser);
11509
11510 /* If the next token is a ';', then there is no expression
11511 statement. */
11512 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11513 {
11514 statement = cp_parser_expression (parser);
11515 if (statement == error_mark_node
11516 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11517 {
11518 cp_parser_skip_to_end_of_block_or_statement (parser);
11519 return error_mark_node;
11520 }
11521 }
11522
11523 /* Handle [[fallthrough]];. */
11524 if (attribute_fallthrough_p (attr))
11525 {
11526 /* The next token after the fallthrough attribute is ';'. */
11527 if (statement == NULL_TREE)
11528 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11529 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11530 void_type_node, 0);
11531 else
11532 warning_at (loc, OPT_Wattributes,
11533 "%<fallthrough%> attribute not followed by %<;%>");
11534 attr = NULL_TREE;
11535 }
11536
11537 /* Allow "[[fallthrough]];", but warn otherwise. */
11538 if (attr != NULL_TREE)
11539 warning_at (loc, OPT_Wattributes,
11540 "attributes at the beginning of statement are ignored");
11541
11542 /* Give a helpful message for "A<T>::type t;" and the like. */
11543 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11544 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11545 {
11546 if (TREE_CODE (statement) == SCOPE_REF)
11547 error_at (token->location, "need %<typename%> before %qE because "
11548 "%qT is a dependent scope",
11549 statement, TREE_OPERAND (statement, 0));
11550 else if (is_overloaded_fn (statement)
11551 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11552 {
11553 /* A::A a; */
11554 tree fn = get_first_fn (statement);
11555 error_at (token->location,
11556 "%<%T::%D%> names the constructor, not the type",
11557 DECL_CONTEXT (fn), DECL_NAME (fn));
11558 }
11559 }
11560
11561 /* Consume the final `;'. */
11562 cp_parser_consume_semicolon_at_end_of_statement (parser);
11563
11564 if (in_statement_expr
11565 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11566 /* This is the final expression statement of a statement
11567 expression. */
11568 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11569 else if (statement)
11570 statement = finish_expr_stmt (statement);
11571
11572 return statement;
11573 }
11574
11575 /* Parse a compound-statement.
11576
11577 compound-statement:
11578 { statement-seq [opt] }
11579
11580 GNU extension:
11581
11582 compound-statement:
11583 { label-declaration-seq [opt] statement-seq [opt] }
11584
11585 label-declaration-seq:
11586 label-declaration
11587 label-declaration-seq label-declaration
11588
11589 Returns a tree representing the statement. */
11590
11591 static tree
11592 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11593 int bcs_flags, bool function_body)
11594 {
11595 tree compound_stmt;
11596 matching_braces braces;
11597
11598 /* Consume the `{'. */
11599 if (!braces.require_open (parser))
11600 return error_mark_node;
11601 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11602 && !function_body && cxx_dialect < cxx14)
11603 pedwarn (input_location, OPT_Wpedantic,
11604 "compound-statement in %<constexpr%> function");
11605 /* Begin the compound-statement. */
11606 compound_stmt = begin_compound_stmt (bcs_flags);
11607 /* If the next keyword is `__label__' we have a label declaration. */
11608 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11609 cp_parser_label_declaration (parser);
11610 /* Parse an (optional) statement-seq. */
11611 cp_parser_statement_seq_opt (parser, in_statement_expr);
11612 /* Finish the compound-statement. */
11613 finish_compound_stmt (compound_stmt);
11614 /* Consume the `}'. */
11615 braces.require_close (parser);
11616
11617 return compound_stmt;
11618 }
11619
11620 /* Parse an (optional) statement-seq.
11621
11622 statement-seq:
11623 statement
11624 statement-seq [opt] statement */
11625
11626 static void
11627 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11628 {
11629 /* Scan statements until there aren't any more. */
11630 while (true)
11631 {
11632 cp_token *token = cp_lexer_peek_token (parser->lexer);
11633
11634 /* If we are looking at a `}', then we have run out of
11635 statements; the same is true if we have reached the end
11636 of file, or have stumbled upon a stray '@end'. */
11637 if (token->type == CPP_CLOSE_BRACE
11638 || token->type == CPP_EOF
11639 || token->type == CPP_PRAGMA_EOL
11640 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11641 break;
11642
11643 /* If we are in a compound statement and find 'else' then
11644 something went wrong. */
11645 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11646 {
11647 if (parser->in_statement & IN_IF_STMT)
11648 break;
11649 else
11650 {
11651 token = cp_lexer_consume_token (parser->lexer);
11652 error_at (token->location, "%<else%> without a previous %<if%>");
11653 }
11654 }
11655
11656 /* Parse the statement. */
11657 cp_parser_statement (parser, in_statement_expr, true, NULL);
11658 }
11659 }
11660
11661 /* Return true if this is the C++20 version of range-based-for with
11662 init-statement. */
11663
11664 static bool
11665 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11666 {
11667 bool r = false;
11668
11669 /* Save tokens so that we can put them back. */
11670 cp_lexer_save_tokens (parser->lexer);
11671
11672 /* There has to be an unnested ; followed by an unnested :. */
11673 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11674 /*recovering=*/false,
11675 CPP_SEMICOLON,
11676 /*consume_paren=*/false) != -1)
11677 goto out;
11678
11679 /* We found the semicolon, eat it now. */
11680 cp_lexer_consume_token (parser->lexer);
11681
11682 /* Now look for ':' that is not nested in () or {}. */
11683 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11684 /*recovering=*/false,
11685 CPP_COLON,
11686 /*consume_paren=*/false) == -1);
11687
11688 out:
11689 /* Roll back the tokens we skipped. */
11690 cp_lexer_rollback_tokens (parser->lexer);
11691
11692 return r;
11693 }
11694
11695 /* Return true if we're looking at (init; cond), false otherwise. */
11696
11697 static bool
11698 cp_parser_init_statement_p (cp_parser *parser)
11699 {
11700 /* Save tokens so that we can put them back. */
11701 cp_lexer_save_tokens (parser->lexer);
11702
11703 /* Look for ';' that is not nested in () or {}. */
11704 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11705 /*recovering=*/false,
11706 CPP_SEMICOLON,
11707 /*consume_paren=*/false);
11708
11709 /* Roll back the tokens we skipped. */
11710 cp_lexer_rollback_tokens (parser->lexer);
11711
11712 return ret == -1;
11713 }
11714
11715 /* Parse a selection-statement.
11716
11717 selection-statement:
11718 if ( init-statement [opt] condition ) statement
11719 if ( init-statement [opt] condition ) statement else statement
11720 switch ( init-statement [opt] condition ) statement
11721
11722 Returns the new IF_STMT or SWITCH_STMT.
11723
11724 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11725 is a (possibly labeled) if statement which is not enclosed in
11726 braces and has an else clause. This is used to implement
11727 -Wparentheses.
11728
11729 CHAIN is a vector of if-else-if conditions. This is used to implement
11730 -Wduplicated-cond. */
11731
11732 static tree
11733 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11734 vec<tree> *chain)
11735 {
11736 cp_token *token;
11737 enum rid keyword;
11738 token_indent_info guard_tinfo;
11739
11740 if (if_p != NULL)
11741 *if_p = false;
11742
11743 /* Peek at the next token. */
11744 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11745 guard_tinfo = get_token_indent_info (token);
11746
11747 /* See what kind of keyword it is. */
11748 keyword = token->keyword;
11749 switch (keyword)
11750 {
11751 case RID_IF:
11752 case RID_SWITCH:
11753 {
11754 tree statement;
11755 tree condition;
11756
11757 bool cx = false;
11758 if (keyword == RID_IF
11759 && cp_lexer_next_token_is_keyword (parser->lexer,
11760 RID_CONSTEXPR))
11761 {
11762 cx = true;
11763 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11764 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11765 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11766 "with %<-std=c++17%> or %<-std=gnu++17%>");
11767 }
11768
11769 /* Look for the `('. */
11770 matching_parens parens;
11771 if (!parens.require_open (parser))
11772 {
11773 cp_parser_skip_to_end_of_statement (parser);
11774 return error_mark_node;
11775 }
11776
11777 /* Begin the selection-statement. */
11778 if (keyword == RID_IF)
11779 {
11780 statement = begin_if_stmt ();
11781 IF_STMT_CONSTEXPR_P (statement) = cx;
11782 }
11783 else
11784 statement = begin_switch_stmt ();
11785
11786 /* Parse the optional init-statement. */
11787 if (cp_parser_init_statement_p (parser))
11788 {
11789 tree decl;
11790 if (cxx_dialect < cxx17)
11791 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11792 "init-statement in selection statements only available "
11793 "with %<-std=c++17%> or %<-std=gnu++17%>");
11794 cp_parser_init_statement (parser, &decl);
11795 }
11796
11797 /* Parse the condition. */
11798 condition = cp_parser_condition (parser);
11799 /* Look for the `)'. */
11800 if (!parens.require_close (parser))
11801 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11802 /*consume_paren=*/true);
11803
11804 if (keyword == RID_IF)
11805 {
11806 bool nested_if;
11807 unsigned char in_statement;
11808
11809 /* Add the condition. */
11810 condition = finish_if_stmt_cond (condition, statement);
11811
11812 if (warn_duplicated_cond)
11813 warn_duplicated_cond_add_or_warn (token->location, condition,
11814 &chain);
11815
11816 /* Parse the then-clause. */
11817 in_statement = parser->in_statement;
11818 parser->in_statement |= IN_IF_STMT;
11819
11820 /* Outside a template, the non-selected branch of a constexpr
11821 if is a 'discarded statement', i.e. unevaluated. */
11822 bool was_discarded = in_discarded_stmt;
11823 bool discard_then = (cx && !processing_template_decl
11824 && integer_zerop (condition));
11825 if (discard_then)
11826 {
11827 in_discarded_stmt = true;
11828 ++c_inhibit_evaluation_warnings;
11829 }
11830
11831 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11832 guard_tinfo);
11833
11834 parser->in_statement = in_statement;
11835
11836 finish_then_clause (statement);
11837
11838 if (discard_then)
11839 {
11840 THEN_CLAUSE (statement) = NULL_TREE;
11841 in_discarded_stmt = was_discarded;
11842 --c_inhibit_evaluation_warnings;
11843 }
11844
11845 /* If the next token is `else', parse the else-clause. */
11846 if (cp_lexer_next_token_is_keyword (parser->lexer,
11847 RID_ELSE))
11848 {
11849 bool discard_else = (cx && !processing_template_decl
11850 && integer_nonzerop (condition));
11851 if (discard_else)
11852 {
11853 in_discarded_stmt = true;
11854 ++c_inhibit_evaluation_warnings;
11855 }
11856
11857 guard_tinfo
11858 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11859 /* Consume the `else' keyword. */
11860 cp_lexer_consume_token (parser->lexer);
11861 if (warn_duplicated_cond)
11862 {
11863 if (cp_lexer_next_token_is_keyword (parser->lexer,
11864 RID_IF)
11865 && chain == NULL)
11866 {
11867 /* We've got "if (COND) else if (COND2)". Start
11868 the condition chain and add COND as the first
11869 element. */
11870 chain = new vec<tree> ();
11871 if (!CONSTANT_CLASS_P (condition)
11872 && !TREE_SIDE_EFFECTS (condition))
11873 {
11874 /* Wrap it in a NOP_EXPR so that we can set the
11875 location of the condition. */
11876 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11877 condition);
11878 SET_EXPR_LOCATION (e, token->location);
11879 chain->safe_push (e);
11880 }
11881 }
11882 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11883 RID_IF))
11884 {
11885 /* This is if-else without subsequent if. Zap the
11886 condition chain; we would have already warned at
11887 this point. */
11888 delete chain;
11889 chain = NULL;
11890 }
11891 }
11892 begin_else_clause (statement);
11893 /* Parse the else-clause. */
11894 cp_parser_implicitly_scoped_statement (parser, NULL,
11895 guard_tinfo, chain);
11896
11897 finish_else_clause (statement);
11898
11899 /* If we are currently parsing a then-clause, then
11900 IF_P will not be NULL. We set it to true to
11901 indicate that this if statement has an else clause.
11902 This may trigger the Wparentheses warning below
11903 when we get back up to the parent if statement. */
11904 if (if_p != NULL)
11905 *if_p = true;
11906
11907 if (discard_else)
11908 {
11909 ELSE_CLAUSE (statement) = NULL_TREE;
11910 in_discarded_stmt = was_discarded;
11911 --c_inhibit_evaluation_warnings;
11912 }
11913 }
11914 else
11915 {
11916 /* This if statement does not have an else clause. If
11917 NESTED_IF is true, then the then-clause has an if
11918 statement which does have an else clause. We warn
11919 about the potential ambiguity. */
11920 if (nested_if)
11921 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11922 "suggest explicit braces to avoid ambiguous"
11923 " %<else%>");
11924 if (warn_duplicated_cond)
11925 {
11926 /* We don't need the condition chain anymore. */
11927 delete chain;
11928 chain = NULL;
11929 }
11930 }
11931
11932 /* Now we're all done with the if-statement. */
11933 finish_if_stmt (statement);
11934 }
11935 else
11936 {
11937 bool in_switch_statement_p;
11938 unsigned char in_statement;
11939
11940 /* Add the condition. */
11941 finish_switch_cond (condition, statement);
11942
11943 /* Parse the body of the switch-statement. */
11944 in_switch_statement_p = parser->in_switch_statement_p;
11945 in_statement = parser->in_statement;
11946 parser->in_switch_statement_p = true;
11947 parser->in_statement |= IN_SWITCH_STMT;
11948 cp_parser_implicitly_scoped_statement (parser, if_p,
11949 guard_tinfo);
11950 parser->in_switch_statement_p = in_switch_statement_p;
11951 parser->in_statement = in_statement;
11952
11953 /* Now we're all done with the switch-statement. */
11954 finish_switch_stmt (statement);
11955 }
11956
11957 return statement;
11958 }
11959 break;
11960
11961 default:
11962 cp_parser_error (parser, "expected selection-statement");
11963 return error_mark_node;
11964 }
11965 }
11966
11967 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11968 If we have seen at least one decl-specifier, and the next token
11969 is not a parenthesis, then we must be looking at a declaration.
11970 (After "int (" we might be looking at a functional cast.) */
11971
11972 static void
11973 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11974 bool any_specifiers_p)
11975 {
11976 if (any_specifiers_p
11977 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11978 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11979 && !cp_parser_error_occurred (parser))
11980 cp_parser_commit_to_tentative_parse (parser);
11981 }
11982
11983 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11984 The declarator shall not specify a function or an array. Returns
11985 TRUE if the declarator is valid, FALSE otherwise. */
11986
11987 static bool
11988 cp_parser_check_condition_declarator (cp_parser* parser,
11989 cp_declarator *declarator,
11990 location_t loc)
11991 {
11992 if (declarator == cp_error_declarator
11993 || function_declarator_p (declarator)
11994 || declarator->kind == cdk_array)
11995 {
11996 if (declarator == cp_error_declarator)
11997 /* Already complained. */;
11998 else if (declarator->kind == cdk_array)
11999 error_at (loc, "condition declares an array");
12000 else
12001 error_at (loc, "condition declares a function");
12002 if (parser->fully_implicit_function_template_p)
12003 abort_fully_implicit_template (parser);
12004 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
12005 /*or_comma=*/false,
12006 /*consume_paren=*/false);
12007 return false;
12008 }
12009 else
12010 return true;
12011 }
12012
12013 /* Parse a condition.
12014
12015 condition:
12016 expression
12017 type-specifier-seq declarator = initializer-clause
12018 type-specifier-seq declarator braced-init-list
12019
12020 GNU Extension:
12021
12022 condition:
12023 type-specifier-seq declarator asm-specification [opt]
12024 attributes [opt] = assignment-expression
12025
12026 Returns the expression that should be tested. */
12027
12028 static tree
12029 cp_parser_condition (cp_parser* parser)
12030 {
12031 cp_decl_specifier_seq type_specifiers;
12032 const char *saved_message;
12033 int declares_class_or_enum;
12034
12035 /* Try the declaration first. */
12036 cp_parser_parse_tentatively (parser);
12037 /* New types are not allowed in the type-specifier-seq for a
12038 condition. */
12039 saved_message = parser->type_definition_forbidden_message;
12040 parser->type_definition_forbidden_message
12041 = G_("types may not be defined in conditions");
12042 /* Parse the type-specifier-seq. */
12043 cp_parser_decl_specifier_seq (parser,
12044 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
12045 &type_specifiers,
12046 &declares_class_or_enum);
12047 /* Restore the saved message. */
12048 parser->type_definition_forbidden_message = saved_message;
12049
12050 cp_parser_maybe_commit_to_declaration (parser,
12051 type_specifiers.any_specifiers_p);
12052
12053 /* If all is well, we might be looking at a declaration. */
12054 if (!cp_parser_error_occurred (parser))
12055 {
12056 tree decl;
12057 tree asm_specification;
12058 tree attributes;
12059 cp_declarator *declarator;
12060 tree initializer = NULL_TREE;
12061 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12062
12063 /* Parse the declarator. */
12064 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12065 CP_PARSER_FLAGS_NONE,
12066 /*ctor_dtor_or_conv_p=*/NULL,
12067 /*parenthesized_p=*/NULL,
12068 /*member_p=*/false,
12069 /*friend_p=*/false,
12070 /*static_p=*/false);
12071 /* Parse the attributes. */
12072 attributes = cp_parser_attributes_opt (parser);
12073 /* Parse the asm-specification. */
12074 asm_specification = cp_parser_asm_specification_opt (parser);
12075 /* If the next token is not an `=' or '{', then we might still be
12076 looking at an expression. For example:
12077
12078 if (A(a).x)
12079
12080 looks like a decl-specifier-seq and a declarator -- but then
12081 there is no `=', so this is an expression. */
12082 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12083 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12084 cp_parser_simulate_error (parser);
12085
12086 /* If we did see an `=' or '{', then we are looking at a declaration
12087 for sure. */
12088 if (cp_parser_parse_definitely (parser))
12089 {
12090 tree pushed_scope;
12091 bool non_constant_p = false;
12092 int flags = LOOKUP_ONLYCONVERTING;
12093
12094 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
12095 return error_mark_node;
12096
12097 /* Create the declaration. */
12098 decl = start_decl (declarator, &type_specifiers,
12099 /*initialized_p=*/true,
12100 attributes, /*prefix_attributes=*/NULL_TREE,
12101 &pushed_scope);
12102
12103 /* Parse the initializer. */
12104 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12105 {
12106 initializer = cp_parser_braced_list (parser, &non_constant_p);
12107 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
12108 flags = 0;
12109 }
12110 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12111 {
12112 /* Consume the `='. */
12113 cp_lexer_consume_token (parser->lexer);
12114 initializer = cp_parser_initializer_clause (parser,
12115 &non_constant_p);
12116 }
12117 else
12118 {
12119 cp_parser_error (parser, "expected initializer");
12120 initializer = error_mark_node;
12121 }
12122 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
12123 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12124
12125 /* Process the initializer. */
12126 cp_finish_decl (decl,
12127 initializer, !non_constant_p,
12128 asm_specification,
12129 flags);
12130
12131 if (pushed_scope)
12132 pop_scope (pushed_scope);
12133
12134 return convert_from_reference (decl);
12135 }
12136 }
12137 /* If we didn't even get past the declarator successfully, we are
12138 definitely not looking at a declaration. */
12139 else
12140 cp_parser_abort_tentative_parse (parser);
12141
12142 /* Otherwise, we are looking at an expression. */
12143 return cp_parser_expression (parser);
12144 }
12145
12146 /* Parses a for-statement or range-for-statement until the closing ')',
12147 not included. */
12148
12149 static tree
12150 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
12151 {
12152 tree init, scope, decl;
12153 bool is_range_for;
12154
12155 /* Begin the for-statement. */
12156 scope = begin_for_scope (&init);
12157
12158 /* Parse the initialization. */
12159 is_range_for = cp_parser_init_statement (parser, &decl);
12160
12161 if (is_range_for)
12162 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
12163 false);
12164 else
12165 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
12166 }
12167
12168 static tree
12169 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
12170 unsigned short unroll)
12171 {
12172 /* Normal for loop */
12173 tree condition = NULL_TREE;
12174 tree expression = NULL_TREE;
12175 tree stmt;
12176
12177 stmt = begin_for_stmt (scope, init);
12178 /* The init-statement has already been parsed in
12179 cp_parser_init_statement, so no work is needed here. */
12180 finish_init_stmt (stmt);
12181
12182 /* If there's a condition, process it. */
12183 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12184 condition = cp_parser_condition (parser);
12185 else if (ivdep)
12186 {
12187 cp_parser_error (parser, "missing loop condition in loop with "
12188 "%<GCC ivdep%> pragma");
12189 condition = error_mark_node;
12190 }
12191 else if (unroll)
12192 {
12193 cp_parser_error (parser, "missing loop condition in loop with "
12194 "%<GCC unroll%> pragma");
12195 condition = error_mark_node;
12196 }
12197 finish_for_cond (condition, stmt, ivdep, unroll);
12198 /* Look for the `;'. */
12199 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12200
12201 /* If there's an expression, process it. */
12202 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
12203 expression = cp_parser_expression (parser);
12204 finish_for_expr (expression, stmt);
12205
12206 return stmt;
12207 }
12208
12209 /* Tries to parse a range-based for-statement:
12210
12211 range-based-for:
12212 decl-specifier-seq declarator : expression
12213
12214 The decl-specifier-seq declarator and the `:' are already parsed by
12215 cp_parser_init_statement. If processing_template_decl it returns a
12216 newly created RANGE_FOR_STMT; if not, it is converted to a
12217 regular FOR_STMT. */
12218
12219 static tree
12220 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
12221 bool ivdep, unsigned short unroll, bool is_omp)
12222 {
12223 tree stmt, range_expr;
12224 auto_vec <cxx_binding *, 16> bindings;
12225 auto_vec <tree, 16> names;
12226 tree decomp_first_name = NULL_TREE;
12227 unsigned int decomp_cnt = 0;
12228
12229 /* Get the range declaration momentarily out of the way so that
12230 the range expression doesn't clash with it. */
12231 if (range_decl != error_mark_node)
12232 {
12233 if (DECL_HAS_VALUE_EXPR_P (range_decl))
12234 {
12235 tree v = DECL_VALUE_EXPR (range_decl);
12236 /* For decomposition declaration get all of the corresponding
12237 declarations out of the way. */
12238 if (TREE_CODE (v) == ARRAY_REF
12239 && VAR_P (TREE_OPERAND (v, 0))
12240 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
12241 {
12242 tree d = range_decl;
12243 range_decl = TREE_OPERAND (v, 0);
12244 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
12245 decomp_first_name = d;
12246 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
12247 {
12248 tree name = DECL_NAME (d);
12249 names.safe_push (name);
12250 bindings.safe_push (IDENTIFIER_BINDING (name));
12251 IDENTIFIER_BINDING (name)
12252 = IDENTIFIER_BINDING (name)->previous;
12253 }
12254 }
12255 }
12256 if (names.is_empty ())
12257 {
12258 tree name = DECL_NAME (range_decl);
12259 names.safe_push (name);
12260 bindings.safe_push (IDENTIFIER_BINDING (name));
12261 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
12262 }
12263 }
12264
12265 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12266 {
12267 bool expr_non_constant_p;
12268 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12269 }
12270 else
12271 range_expr = cp_parser_expression (parser);
12272
12273 /* Put the range declaration(s) back into scope. */
12274 for (unsigned int i = 0; i < names.length (); i++)
12275 {
12276 cxx_binding *binding = bindings[i];
12277 binding->previous = IDENTIFIER_BINDING (names[i]);
12278 IDENTIFIER_BINDING (names[i]) = binding;
12279 }
12280
12281 /* finish_omp_for has its own code for the following, so just
12282 return the range_expr instead. */
12283 if (is_omp)
12284 return range_expr;
12285
12286 /* If in template, STMT is converted to a normal for-statement
12287 at instantiation. If not, it is done just ahead. */
12288 if (processing_template_decl)
12289 {
12290 if (check_for_bare_parameter_packs (range_expr))
12291 range_expr = error_mark_node;
12292 stmt = begin_range_for_stmt (scope, init);
12293 if (ivdep)
12294 RANGE_FOR_IVDEP (stmt) = 1;
12295 if (unroll)
12296 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12297 finish_range_for_decl (stmt, range_decl, range_expr);
12298 if (!type_dependent_expression_p (range_expr)
12299 /* do_auto_deduction doesn't mess with template init-lists. */
12300 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12301 do_range_for_auto_deduction (range_decl, range_expr);
12302 }
12303 else
12304 {
12305 stmt = begin_for_stmt (scope, init);
12306 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12307 decomp_first_name, decomp_cnt, ivdep,
12308 unroll);
12309 }
12310 return stmt;
12311 }
12312
12313 /* Subroutine of cp_convert_range_for: given the initializer expression,
12314 builds up the range temporary. */
12315
12316 static tree
12317 build_range_temp (tree range_expr)
12318 {
12319 tree range_type, range_temp;
12320
12321 /* Find out the type deduced by the declaration
12322 `auto &&__range = range_expr'. */
12323 range_type = cp_build_reference_type (make_auto (), true);
12324 range_type = do_auto_deduction (range_type, range_expr,
12325 type_uses_auto (range_type));
12326
12327 /* Create the __range variable. */
12328 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12329 range_type);
12330 TREE_USED (range_temp) = 1;
12331 DECL_ARTIFICIAL (range_temp) = 1;
12332
12333 return range_temp;
12334 }
12335
12336 /* Used by cp_parser_range_for in template context: we aren't going to
12337 do a full conversion yet, but we still need to resolve auto in the
12338 type of the for-range-declaration if present. This is basically
12339 a shortcut version of cp_convert_range_for. */
12340
12341 static void
12342 do_range_for_auto_deduction (tree decl, tree range_expr)
12343 {
12344 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12345 if (auto_node)
12346 {
12347 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12348 range_temp = convert_from_reference (build_range_temp (range_expr));
12349 iter_type = (cp_parser_perform_range_for_lookup
12350 (range_temp, &begin_dummy, &end_dummy));
12351 if (iter_type)
12352 {
12353 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12354 iter_type);
12355 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12356 RO_UNARY_STAR,
12357 tf_warning_or_error);
12358 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12359 iter_decl, auto_node);
12360 }
12361 }
12362 }
12363
12364 /* Converts a range-based for-statement into a normal
12365 for-statement, as per the definition.
12366
12367 for (RANGE_DECL : RANGE_EXPR)
12368 BLOCK
12369
12370 should be equivalent to:
12371
12372 {
12373 auto &&__range = RANGE_EXPR;
12374 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12375 __begin != __end;
12376 ++__begin)
12377 {
12378 RANGE_DECL = *__begin;
12379 BLOCK
12380 }
12381 }
12382
12383 If RANGE_EXPR is an array:
12384 BEGIN_EXPR = __range
12385 END_EXPR = __range + ARRAY_SIZE(__range)
12386 Else if RANGE_EXPR has a member 'begin' or 'end':
12387 BEGIN_EXPR = __range.begin()
12388 END_EXPR = __range.end()
12389 Else:
12390 BEGIN_EXPR = begin(__range)
12391 END_EXPR = end(__range);
12392
12393 If __range has a member 'begin' but not 'end', or vice versa, we must
12394 still use the second alternative (it will surely fail, however).
12395 When calling begin()/end() in the third alternative we must use
12396 argument dependent lookup, but always considering 'std' as an associated
12397 namespace. */
12398
12399 tree
12400 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12401 tree decomp_first_name, unsigned int decomp_cnt,
12402 bool ivdep, unsigned short unroll)
12403 {
12404 tree begin, end;
12405 tree iter_type, begin_expr, end_expr;
12406 tree condition, expression;
12407
12408 range_expr = mark_lvalue_use (range_expr);
12409
12410 if (range_decl == error_mark_node || range_expr == error_mark_node)
12411 /* If an error happened previously do nothing or else a lot of
12412 unhelpful errors would be issued. */
12413 begin_expr = end_expr = iter_type = error_mark_node;
12414 else
12415 {
12416 tree range_temp;
12417
12418 if (VAR_P (range_expr)
12419 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12420 /* Can't bind a reference to an array of runtime bound. */
12421 range_temp = range_expr;
12422 else
12423 {
12424 range_temp = build_range_temp (range_expr);
12425 pushdecl (range_temp);
12426 cp_finish_decl (range_temp, range_expr,
12427 /*is_constant_init*/false, NULL_TREE,
12428 LOOKUP_ONLYCONVERTING);
12429 range_temp = convert_from_reference (range_temp);
12430 }
12431 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12432 &begin_expr, &end_expr);
12433 }
12434
12435 /* The new for initialization statement. */
12436 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12437 iter_type);
12438 TREE_USED (begin) = 1;
12439 DECL_ARTIFICIAL (begin) = 1;
12440 pushdecl (begin);
12441 cp_finish_decl (begin, begin_expr,
12442 /*is_constant_init*/false, NULL_TREE,
12443 LOOKUP_ONLYCONVERTING);
12444
12445 if (cxx_dialect >= cxx17)
12446 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12447 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12448 TREE_USED (end) = 1;
12449 DECL_ARTIFICIAL (end) = 1;
12450 pushdecl (end);
12451 cp_finish_decl (end, end_expr,
12452 /*is_constant_init*/false, NULL_TREE,
12453 LOOKUP_ONLYCONVERTING);
12454
12455 finish_init_stmt (statement);
12456
12457 /* The new for condition. */
12458 condition = build_x_binary_op (input_location, NE_EXPR,
12459 begin, ERROR_MARK,
12460 end, ERROR_MARK,
12461 NULL, tf_warning_or_error);
12462 finish_for_cond (condition, statement, ivdep, unroll);
12463
12464 /* The new increment expression. */
12465 expression = finish_unary_op_expr (input_location,
12466 PREINCREMENT_EXPR, begin,
12467 tf_warning_or_error);
12468 finish_for_expr (expression, statement);
12469
12470 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12471 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12472
12473 /* The declaration is initialized with *__begin inside the loop body. */
12474 cp_finish_decl (range_decl,
12475 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12476 tf_warning_or_error),
12477 /*is_constant_init*/false, NULL_TREE,
12478 LOOKUP_ONLYCONVERTING);
12479 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12480 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12481
12482 return statement;
12483 }
12484
12485 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12486 We need to solve both at the same time because the method used
12487 depends on the existence of members begin or end.
12488 Returns the type deduced for the iterator expression. */
12489
12490 static tree
12491 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12492 {
12493 if (error_operand_p (range))
12494 {
12495 *begin = *end = error_mark_node;
12496 return error_mark_node;
12497 }
12498
12499 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12500 {
12501 error ("range-based %<for%> expression of type %qT "
12502 "has incomplete type", TREE_TYPE (range));
12503 *begin = *end = error_mark_node;
12504 return error_mark_node;
12505 }
12506 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12507 {
12508 /* If RANGE is an array, we will use pointer arithmetic. */
12509 *begin = decay_conversion (range, tf_warning_or_error);
12510 *end = build_binary_op (input_location, PLUS_EXPR,
12511 range,
12512 array_type_nelts_top (TREE_TYPE (range)),
12513 false);
12514 return TREE_TYPE (*begin);
12515 }
12516 else
12517 {
12518 /* If it is not an array, we must do a bit of magic. */
12519 tree id_begin, id_end;
12520 tree member_begin, member_end;
12521
12522 *begin = *end = error_mark_node;
12523
12524 id_begin = get_identifier ("begin");
12525 id_end = get_identifier ("end");
12526 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12527 /*protect=*/2, /*want_type=*/false,
12528 tf_warning_or_error);
12529 member_end = lookup_member (TREE_TYPE (range), id_end,
12530 /*protect=*/2, /*want_type=*/false,
12531 tf_warning_or_error);
12532
12533 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12534 {
12535 /* Use the member functions. */
12536 *begin = cp_parser_range_for_member_function (range, id_begin);
12537 *end = cp_parser_range_for_member_function (range, id_end);
12538 }
12539 else
12540 {
12541 /* Use global functions with ADL. */
12542 vec<tree, va_gc> *vec;
12543 vec = make_tree_vector ();
12544
12545 vec_safe_push (vec, range);
12546
12547 member_begin = perform_koenig_lookup (id_begin, vec,
12548 tf_warning_or_error);
12549 *begin = finish_call_expr (member_begin, &vec, false, true,
12550 tf_warning_or_error);
12551 member_end = perform_koenig_lookup (id_end, vec,
12552 tf_warning_or_error);
12553 *end = finish_call_expr (member_end, &vec, false, true,
12554 tf_warning_or_error);
12555
12556 release_tree_vector (vec);
12557 }
12558
12559 /* Last common checks. */
12560 if (*begin == error_mark_node || *end == error_mark_node)
12561 {
12562 /* If one of the expressions is an error do no more checks. */
12563 *begin = *end = error_mark_node;
12564 return error_mark_node;
12565 }
12566 else if (type_dependent_expression_p (*begin)
12567 || type_dependent_expression_p (*end))
12568 /* Can happen, when, eg, in a template context, Koenig lookup
12569 can't resolve begin/end (c++/58503). */
12570 return NULL_TREE;
12571 else
12572 {
12573 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12574 /* The unqualified type of the __begin and __end temporaries should
12575 be the same, as required by the multiple auto declaration. */
12576 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12577 {
12578 if (cxx_dialect >= cxx17
12579 && (build_x_binary_op (input_location, NE_EXPR,
12580 *begin, ERROR_MARK,
12581 *end, ERROR_MARK,
12582 NULL, tf_none)
12583 != error_mark_node))
12584 /* P0184R0 allows __begin and __end to have different types,
12585 but make sure they are comparable so we can give a better
12586 diagnostic. */;
12587 else
12588 error ("inconsistent begin/end types in range-based %<for%> "
12589 "statement: %qT and %qT",
12590 TREE_TYPE (*begin), TREE_TYPE (*end));
12591 }
12592 return iter_type;
12593 }
12594 }
12595 }
12596
12597 /* Helper function for cp_parser_perform_range_for_lookup.
12598 Builds a tree for RANGE.IDENTIFIER(). */
12599
12600 static tree
12601 cp_parser_range_for_member_function (tree range, tree identifier)
12602 {
12603 tree member, res;
12604 vec<tree, va_gc> *vec;
12605
12606 member = finish_class_member_access_expr (range, identifier,
12607 false, tf_warning_or_error);
12608 if (member == error_mark_node)
12609 return error_mark_node;
12610
12611 vec = make_tree_vector ();
12612 res = finish_call_expr (member, &vec,
12613 /*disallow_virtual=*/false,
12614 /*koenig_p=*/false,
12615 tf_warning_or_error);
12616 release_tree_vector (vec);
12617 return res;
12618 }
12619
12620 /* Parse an iteration-statement.
12621
12622 iteration-statement:
12623 while ( condition ) statement
12624 do statement while ( expression ) ;
12625 for ( init-statement condition [opt] ; expression [opt] )
12626 statement
12627
12628 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12629
12630 static tree
12631 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12632 unsigned short unroll)
12633 {
12634 cp_token *token;
12635 enum rid keyword;
12636 tree statement;
12637 unsigned char in_statement;
12638 token_indent_info guard_tinfo;
12639
12640 /* Peek at the next token. */
12641 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12642 if (!token)
12643 return error_mark_node;
12644
12645 guard_tinfo = get_token_indent_info (token);
12646
12647 /* Remember whether or not we are already within an iteration
12648 statement. */
12649 in_statement = parser->in_statement;
12650
12651 /* See what kind of keyword it is. */
12652 keyword = token->keyword;
12653 switch (keyword)
12654 {
12655 case RID_WHILE:
12656 {
12657 tree condition;
12658
12659 /* Begin the while-statement. */
12660 statement = begin_while_stmt ();
12661 /* Look for the `('. */
12662 matching_parens parens;
12663 parens.require_open (parser);
12664 /* Parse the condition. */
12665 condition = cp_parser_condition (parser);
12666 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12667 /* Look for the `)'. */
12668 parens.require_close (parser);
12669 /* Parse the dependent statement. */
12670 parser->in_statement = IN_ITERATION_STMT;
12671 bool prev = note_iteration_stmt_body_start ();
12672 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12673 note_iteration_stmt_body_end (prev);
12674 parser->in_statement = in_statement;
12675 /* We're done with the while-statement. */
12676 finish_while_stmt (statement);
12677 }
12678 break;
12679
12680 case RID_DO:
12681 {
12682 tree expression;
12683
12684 /* Begin the do-statement. */
12685 statement = begin_do_stmt ();
12686 /* Parse the body of the do-statement. */
12687 parser->in_statement = IN_ITERATION_STMT;
12688 bool prev = note_iteration_stmt_body_start ();
12689 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12690 note_iteration_stmt_body_end (prev);
12691 parser->in_statement = in_statement;
12692 finish_do_body (statement);
12693 /* Look for the `while' keyword. */
12694 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12695 /* Look for the `('. */
12696 matching_parens parens;
12697 parens.require_open (parser);
12698 /* Parse the expression. */
12699 expression = cp_parser_expression (parser);
12700 /* We're done with the do-statement. */
12701 finish_do_stmt (expression, statement, ivdep, unroll);
12702 /* Look for the `)'. */
12703 parens.require_close (parser);
12704 /* Look for the `;'. */
12705 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12706 }
12707 break;
12708
12709 case RID_FOR:
12710 {
12711 /* Look for the `('. */
12712 matching_parens parens;
12713 parens.require_open (parser);
12714
12715 statement = cp_parser_for (parser, ivdep, unroll);
12716
12717 /* Look for the `)'. */
12718 parens.require_close (parser);
12719
12720 /* Parse the body of the for-statement. */
12721 parser->in_statement = IN_ITERATION_STMT;
12722 bool prev = note_iteration_stmt_body_start ();
12723 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12724 note_iteration_stmt_body_end (prev);
12725 parser->in_statement = in_statement;
12726
12727 /* We're done with the for-statement. */
12728 finish_for_stmt (statement);
12729 }
12730 break;
12731
12732 default:
12733 cp_parser_error (parser, "expected iteration-statement");
12734 statement = error_mark_node;
12735 break;
12736 }
12737
12738 return statement;
12739 }
12740
12741 /* Parse a init-statement or the declarator of a range-based-for.
12742 Returns true if a range-based-for declaration is seen.
12743
12744 init-statement:
12745 expression-statement
12746 simple-declaration */
12747
12748 static bool
12749 cp_parser_init_statement (cp_parser *parser, tree *decl)
12750 {
12751 /* If the next token is a `;', then we have an empty
12752 expression-statement. Grammatically, this is also a
12753 simple-declaration, but an invalid one, because it does not
12754 declare anything. Therefore, if we did not handle this case
12755 specially, we would issue an error message about an invalid
12756 declaration. */
12757 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12758 {
12759 bool is_range_for = false;
12760 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12761
12762 /* Try to parse the init-statement. */
12763 if (cp_parser_range_based_for_with_init_p (parser))
12764 {
12765 tree dummy;
12766 cp_parser_parse_tentatively (parser);
12767 /* Parse the declaration. */
12768 cp_parser_simple_declaration (parser,
12769 /*function_definition_allowed_p=*/false,
12770 &dummy);
12771 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12772 if (!cp_parser_parse_definitely (parser))
12773 /* That didn't work, try to parse it as an expression-statement. */
12774 cp_parser_expression_statement (parser, NULL_TREE);
12775
12776 if (cxx_dialect < cxx2a)
12777 {
12778 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12779 "range-based %<for%> loops with initializer only "
12780 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
12781 *decl = error_mark_node;
12782 }
12783 }
12784
12785 /* A colon is used in range-based for. */
12786 parser->colon_corrects_to_scope_p = false;
12787
12788 /* We're going to speculatively look for a declaration, falling back
12789 to an expression, if necessary. */
12790 cp_parser_parse_tentatively (parser);
12791 /* Parse the declaration. */
12792 cp_parser_simple_declaration (parser,
12793 /*function_definition_allowed_p=*/false,
12794 decl);
12795 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12796 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12797 {
12798 /* It is a range-for, consume the ':'. */
12799 cp_lexer_consume_token (parser->lexer);
12800 is_range_for = true;
12801 if (cxx_dialect < cxx11)
12802 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12803 "range-based %<for%> loops only available with "
12804 "%<-std=c++11%> or %<-std=gnu++11%>");
12805 }
12806 else
12807 /* The ';' is not consumed yet because we told
12808 cp_parser_simple_declaration not to. */
12809 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12810
12811 if (cp_parser_parse_definitely (parser))
12812 return is_range_for;
12813 /* If the tentative parse failed, then we shall need to look for an
12814 expression-statement. */
12815 }
12816 /* If we are here, it is an expression-statement. */
12817 cp_parser_expression_statement (parser, NULL_TREE);
12818 return false;
12819 }
12820
12821 /* Parse a jump-statement.
12822
12823 jump-statement:
12824 break ;
12825 continue ;
12826 return expression [opt] ;
12827 return braced-init-list ;
12828 goto identifier ;
12829
12830 GNU extension:
12831
12832 jump-statement:
12833 goto * expression ;
12834
12835 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12836
12837 static tree
12838 cp_parser_jump_statement (cp_parser* parser)
12839 {
12840 tree statement = error_mark_node;
12841 cp_token *token;
12842 enum rid keyword;
12843 unsigned char in_statement;
12844
12845 /* Peek at the next token. */
12846 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12847 if (!token)
12848 return error_mark_node;
12849
12850 /* See what kind of keyword it is. */
12851 keyword = token->keyword;
12852 switch (keyword)
12853 {
12854 case RID_BREAK:
12855 in_statement = parser->in_statement & ~IN_IF_STMT;
12856 switch (in_statement)
12857 {
12858 case 0:
12859 error_at (token->location, "break statement not within loop or switch");
12860 break;
12861 default:
12862 gcc_assert ((in_statement & IN_SWITCH_STMT)
12863 || in_statement == IN_ITERATION_STMT);
12864 statement = finish_break_stmt ();
12865 if (in_statement == IN_ITERATION_STMT)
12866 break_maybe_infinite_loop ();
12867 break;
12868 case IN_OMP_BLOCK:
12869 error_at (token->location, "invalid exit from OpenMP structured block");
12870 break;
12871 case IN_OMP_FOR:
12872 error_at (token->location, "break statement used with OpenMP for loop");
12873 break;
12874 }
12875 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12876 break;
12877
12878 case RID_CONTINUE:
12879 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12880 {
12881 case 0:
12882 error_at (token->location, "continue statement not within a loop");
12883 break;
12884 /* Fall through. */
12885 case IN_ITERATION_STMT:
12886 case IN_OMP_FOR:
12887 statement = finish_continue_stmt ();
12888 break;
12889 case IN_OMP_BLOCK:
12890 error_at (token->location, "invalid exit from OpenMP structured block");
12891 break;
12892 default:
12893 gcc_unreachable ();
12894 }
12895 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12896 break;
12897
12898 case RID_RETURN:
12899 {
12900 tree expr;
12901 bool expr_non_constant_p;
12902
12903 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12904 {
12905 cp_lexer_set_source_position (parser->lexer);
12906 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12907 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12908 }
12909 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12910 expr = cp_parser_expression (parser);
12911 else
12912 /* If the next token is a `;', then there is no
12913 expression. */
12914 expr = NULL_TREE;
12915 /* Build the return-statement. */
12916 if (current_function_auto_return_pattern && in_discarded_stmt)
12917 /* Don't deduce from a discarded return statement. */;
12918 else
12919 statement = finish_return_stmt (expr);
12920 /* Look for the final `;'. */
12921 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12922 }
12923 break;
12924
12925 case RID_GOTO:
12926 if (parser->in_function_body
12927 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12928 {
12929 error ("%<goto%> in %<constexpr%> function");
12930 cp_function_chain->invalid_constexpr = true;
12931 }
12932
12933 /* Create the goto-statement. */
12934 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12935 {
12936 /* Issue a warning about this use of a GNU extension. */
12937 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12938 /* Consume the '*' token. */
12939 cp_lexer_consume_token (parser->lexer);
12940 /* Parse the dependent expression. */
12941 finish_goto_stmt (cp_parser_expression (parser));
12942 }
12943 else
12944 finish_goto_stmt (cp_parser_identifier (parser));
12945 /* Look for the final `;'. */
12946 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12947 break;
12948
12949 default:
12950 cp_parser_error (parser, "expected jump-statement");
12951 break;
12952 }
12953
12954 return statement;
12955 }
12956
12957 /* Parse a declaration-statement.
12958
12959 declaration-statement:
12960 block-declaration */
12961
12962 static void
12963 cp_parser_declaration_statement (cp_parser* parser)
12964 {
12965 void *p;
12966
12967 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12968 p = obstack_alloc (&declarator_obstack, 0);
12969
12970 /* Parse the block-declaration. */
12971 cp_parser_block_declaration (parser, /*statement_p=*/true);
12972
12973 /* Free any declarators allocated. */
12974 obstack_free (&declarator_obstack, p);
12975 }
12976
12977 /* Some dependent statements (like `if (cond) statement'), are
12978 implicitly in their own scope. In other words, if the statement is
12979 a single statement (as opposed to a compound-statement), it is
12980 none-the-less treated as if it were enclosed in braces. Any
12981 declarations appearing in the dependent statement are out of scope
12982 after control passes that point. This function parses a statement,
12983 but ensures that is in its own scope, even if it is not a
12984 compound-statement.
12985
12986 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12987 is a (possibly labeled) if statement which is not enclosed in
12988 braces and has an else clause. This is used to implement
12989 -Wparentheses.
12990
12991 CHAIN is a vector of if-else-if conditions. This is used to implement
12992 -Wduplicated-cond.
12993
12994 Returns the new statement. */
12995
12996 static tree
12997 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12998 const token_indent_info &guard_tinfo,
12999 vec<tree> *chain)
13000 {
13001 tree statement;
13002 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
13003 location_t body_loc_after_labels = UNKNOWN_LOCATION;
13004 token_indent_info body_tinfo
13005 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13006
13007 if (if_p != NULL)
13008 *if_p = false;
13009
13010 /* Mark if () ; with a special NOP_EXPR. */
13011 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13012 {
13013 cp_lexer_consume_token (parser->lexer);
13014 statement = add_stmt (build_empty_stmt (body_loc));
13015
13016 if (guard_tinfo.keyword == RID_IF
13017 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
13018 warning_at (body_loc, OPT_Wempty_body,
13019 "suggest braces around empty body in an %<if%> statement");
13020 else if (guard_tinfo.keyword == RID_ELSE)
13021 warning_at (body_loc, OPT_Wempty_body,
13022 "suggest braces around empty body in an %<else%> statement");
13023 }
13024 /* if a compound is opened, we simply parse the statement directly. */
13025 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13026 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
13027 /* If the token is not a `{', then we must take special action. */
13028 else
13029 {
13030 /* Create a compound-statement. */
13031 statement = begin_compound_stmt (0);
13032 /* Parse the dependent-statement. */
13033 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
13034 &body_loc_after_labels);
13035 /* Finish the dummy compound-statement. */
13036 finish_compound_stmt (statement);
13037 }
13038
13039 token_indent_info next_tinfo
13040 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13041 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13042
13043 if (body_loc_after_labels != UNKNOWN_LOCATION
13044 && next_tinfo.type != CPP_SEMICOLON)
13045 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
13046 guard_tinfo.location, guard_tinfo.keyword);
13047
13048 /* Return the statement. */
13049 return statement;
13050 }
13051
13052 /* For some dependent statements (like `while (cond) statement'), we
13053 have already created a scope. Therefore, even if the dependent
13054 statement is a compound-statement, we do not want to create another
13055 scope. */
13056
13057 static void
13058 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
13059 const token_indent_info &guard_tinfo)
13060 {
13061 /* If the token is a `{', then we must take special action. */
13062 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13063 {
13064 token_indent_info body_tinfo
13065 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13066 location_t loc_after_labels = UNKNOWN_LOCATION;
13067
13068 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
13069 &loc_after_labels);
13070 token_indent_info next_tinfo
13071 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13072 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13073
13074 if (loc_after_labels != UNKNOWN_LOCATION
13075 && next_tinfo.type != CPP_SEMICOLON)
13076 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
13077 guard_tinfo.location,
13078 guard_tinfo.keyword);
13079 }
13080 else
13081 {
13082 /* Avoid calling cp_parser_compound_statement, so that we
13083 don't create a new scope. Do everything else by hand. */
13084 matching_braces braces;
13085 braces.require_open (parser);
13086 /* If the next keyword is `__label__' we have a label declaration. */
13087 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13088 cp_parser_label_declaration (parser);
13089 /* Parse an (optional) statement-seq. */
13090 cp_parser_statement_seq_opt (parser, NULL_TREE);
13091 braces.require_close (parser);
13092 }
13093 }
13094
13095 /* Declarations [gram.dcl.dcl] */
13096
13097 /* Parse an optional declaration-sequence.
13098
13099 declaration-seq:
13100 declaration
13101 declaration-seq declaration */
13102
13103 static void
13104 cp_parser_declaration_seq_opt (cp_parser* parser)
13105 {
13106 while (true)
13107 {
13108 cp_token *token = cp_lexer_peek_token (parser->lexer);
13109
13110 if (token->type == CPP_CLOSE_BRACE
13111 || token->type == CPP_EOF)
13112 break;
13113 else
13114 cp_parser_toplevel_declaration (parser);
13115 }
13116 }
13117
13118 /* Parse a declaration.
13119
13120 declaration:
13121 block-declaration
13122 function-definition
13123 template-declaration
13124 explicit-instantiation
13125 explicit-specialization
13126 linkage-specification
13127 namespace-definition
13128
13129 C++17:
13130 deduction-guide
13131
13132 GNU extension:
13133
13134 declaration:
13135 __extension__ declaration */
13136
13137 static void
13138 cp_parser_declaration (cp_parser* parser)
13139 {
13140 cp_token token1;
13141 cp_token token2;
13142 int saved_pedantic;
13143 void *p;
13144 tree attributes = NULL_TREE;
13145
13146 /* Check for the `__extension__' keyword. */
13147 if (cp_parser_extension_opt (parser, &saved_pedantic))
13148 {
13149 /* Parse the qualified declaration. */
13150 cp_parser_declaration (parser);
13151 /* Restore the PEDANTIC flag. */
13152 pedantic = saved_pedantic;
13153
13154 return;
13155 }
13156
13157 /* Try to figure out what kind of declaration is present. */
13158 token1 = *cp_lexer_peek_token (parser->lexer);
13159
13160 if (token1.type != CPP_EOF)
13161 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
13162 else
13163 {
13164 token2.type = CPP_EOF;
13165 token2.keyword = RID_MAX;
13166 }
13167
13168 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13169 p = obstack_alloc (&declarator_obstack, 0);
13170
13171 /* If the next token is `extern' and the following token is a string
13172 literal, then we have a linkage specification. */
13173 if (token1.keyword == RID_EXTERN
13174 && cp_parser_is_pure_string_literal (&token2))
13175 cp_parser_linkage_specification (parser);
13176 /* If the next token is `template', then we have either a template
13177 declaration, an explicit instantiation, or an explicit
13178 specialization. */
13179 else if (token1.keyword == RID_TEMPLATE)
13180 {
13181 /* `template <>' indicates a template specialization. */
13182 if (token2.type == CPP_LESS
13183 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13184 cp_parser_explicit_specialization (parser);
13185 /* `template <' indicates a template declaration. */
13186 else if (token2.type == CPP_LESS)
13187 cp_parser_template_declaration (parser, /*member_p=*/false);
13188 /* Anything else must be an explicit instantiation. */
13189 else
13190 cp_parser_explicit_instantiation (parser);
13191 }
13192 /* If the next token is `export', then we have a template
13193 declaration. */
13194 else if (token1.keyword == RID_EXPORT)
13195 cp_parser_template_declaration (parser, /*member_p=*/false);
13196 /* If the next token is `extern', 'static' or 'inline' and the one
13197 after that is `template', we have a GNU extended explicit
13198 instantiation directive. */
13199 else if (cp_parser_allow_gnu_extensions_p (parser)
13200 && (token1.keyword == RID_EXTERN
13201 || token1.keyword == RID_STATIC
13202 || token1.keyword == RID_INLINE)
13203 && token2.keyword == RID_TEMPLATE)
13204 cp_parser_explicit_instantiation (parser);
13205 /* If the next token is `namespace', check for a named or unnamed
13206 namespace definition. */
13207 else if (token1.keyword == RID_NAMESPACE
13208 && (/* A named namespace definition. */
13209 (token2.type == CPP_NAME
13210 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
13211 != CPP_EQ))
13212 || (token2.type == CPP_OPEN_SQUARE
13213 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
13214 == CPP_OPEN_SQUARE)
13215 /* An unnamed namespace definition. */
13216 || token2.type == CPP_OPEN_BRACE
13217 || token2.keyword == RID_ATTRIBUTE))
13218 cp_parser_namespace_definition (parser);
13219 /* An inline (associated) namespace definition. */
13220 else if (token1.keyword == RID_INLINE
13221 && token2.keyword == RID_NAMESPACE)
13222 cp_parser_namespace_definition (parser);
13223 /* Objective-C++ declaration/definition. */
13224 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
13225 cp_parser_objc_declaration (parser, NULL_TREE);
13226 else if (c_dialect_objc ()
13227 && token1.keyword == RID_ATTRIBUTE
13228 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
13229 cp_parser_objc_declaration (parser, attributes);
13230 /* At this point we may have a template declared by a concept
13231 introduction. */
13232 else if (flag_concepts
13233 && cp_parser_template_declaration_after_export (parser,
13234 /*member_p=*/false))
13235 /* We did. */;
13236 else
13237 /* Try to parse a block-declaration, or a function-definition. */
13238 cp_parser_block_declaration (parser, /*statement_p=*/false);
13239
13240 /* Free any declarators allocated. */
13241 obstack_free (&declarator_obstack, p);
13242 }
13243
13244 /* Parse a namespace-scope declaration. */
13245
13246 static void
13247 cp_parser_toplevel_declaration (cp_parser* parser)
13248 {
13249 cp_token *token = cp_lexer_peek_token (parser->lexer);
13250
13251 if (token->type == CPP_PRAGMA)
13252 /* A top-level declaration can consist solely of a #pragma. A
13253 nested declaration cannot, so this is done here and not in
13254 cp_parser_declaration. (A #pragma at block scope is
13255 handled in cp_parser_statement.) */
13256 cp_parser_pragma (parser, pragma_external, NULL);
13257 else if (token->type == CPP_SEMICOLON)
13258 {
13259 /* A declaration consisting of a single semicolon is
13260 invalid. Allow it unless we're being pedantic. */
13261 cp_lexer_consume_token (parser->lexer);
13262 if (!in_system_header_at (input_location))
13263 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
13264 }
13265 else
13266 /* Parse the declaration itself. */
13267 cp_parser_declaration (parser);
13268 }
13269
13270 /* Parse a block-declaration.
13271
13272 block-declaration:
13273 simple-declaration
13274 asm-definition
13275 namespace-alias-definition
13276 using-declaration
13277 using-directive
13278
13279 GNU Extension:
13280
13281 block-declaration:
13282 __extension__ block-declaration
13283
13284 C++0x Extension:
13285
13286 block-declaration:
13287 static_assert-declaration
13288
13289 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13290 part of a declaration-statement. */
13291
13292 static void
13293 cp_parser_block_declaration (cp_parser *parser,
13294 bool statement_p)
13295 {
13296 cp_token *token1;
13297 int saved_pedantic;
13298
13299 /* Check for the `__extension__' keyword. */
13300 if (cp_parser_extension_opt (parser, &saved_pedantic))
13301 {
13302 /* Parse the qualified declaration. */
13303 cp_parser_block_declaration (parser, statement_p);
13304 /* Restore the PEDANTIC flag. */
13305 pedantic = saved_pedantic;
13306
13307 return;
13308 }
13309
13310 /* Peek at the next token to figure out which kind of declaration is
13311 present. */
13312 token1 = cp_lexer_peek_token (parser->lexer);
13313
13314 /* If the next keyword is `asm', we have an asm-definition. */
13315 if (token1->keyword == RID_ASM)
13316 {
13317 if (statement_p)
13318 cp_parser_commit_to_tentative_parse (parser);
13319 cp_parser_asm_definition (parser);
13320 }
13321 /* If the next keyword is `namespace', we have a
13322 namespace-alias-definition. */
13323 else if (token1->keyword == RID_NAMESPACE)
13324 cp_parser_namespace_alias_definition (parser);
13325 /* If the next keyword is `using', we have a
13326 using-declaration, a using-directive, or an alias-declaration. */
13327 else if (token1->keyword == RID_USING)
13328 {
13329 cp_token *token2;
13330
13331 if (statement_p)
13332 cp_parser_commit_to_tentative_parse (parser);
13333 /* If the token after `using' is `namespace', then we have a
13334 using-directive. */
13335 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13336 if (token2->keyword == RID_NAMESPACE)
13337 cp_parser_using_directive (parser);
13338 /* If the second token after 'using' is '=', then we have an
13339 alias-declaration. */
13340 else if (cxx_dialect >= cxx11
13341 && token2->type == CPP_NAME
13342 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13343 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13344 cp_parser_alias_declaration (parser);
13345 /* Otherwise, it's a using-declaration. */
13346 else
13347 cp_parser_using_declaration (parser,
13348 /*access_declaration_p=*/false);
13349 }
13350 /* If the next keyword is `__label__' we have a misplaced label
13351 declaration. */
13352 else if (token1->keyword == RID_LABEL)
13353 {
13354 cp_lexer_consume_token (parser->lexer);
13355 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13356 cp_parser_skip_to_end_of_statement (parser);
13357 /* If the next token is now a `;', consume it. */
13358 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13359 cp_lexer_consume_token (parser->lexer);
13360 }
13361 /* If the next token is `static_assert' we have a static assertion. */
13362 else if (token1->keyword == RID_STATIC_ASSERT)
13363 cp_parser_static_assert (parser, /*member_p=*/false);
13364 /* Anything else must be a simple-declaration. */
13365 else
13366 cp_parser_simple_declaration (parser, !statement_p,
13367 /*maybe_range_for_decl*/NULL);
13368 }
13369
13370 /* Parse a simple-declaration.
13371
13372 simple-declaration:
13373 decl-specifier-seq [opt] init-declarator-list [opt] ;
13374 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13375 brace-or-equal-initializer ;
13376
13377 init-declarator-list:
13378 init-declarator
13379 init-declarator-list , init-declarator
13380
13381 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13382 function-definition as a simple-declaration.
13383
13384 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13385 parsed declaration if it is an uninitialized single declarator not followed
13386 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13387 if present, will not be consumed. */
13388
13389 static void
13390 cp_parser_simple_declaration (cp_parser* parser,
13391 bool function_definition_allowed_p,
13392 tree *maybe_range_for_decl)
13393 {
13394 cp_decl_specifier_seq decl_specifiers;
13395 int declares_class_or_enum;
13396 bool saw_declarator;
13397 location_t comma_loc = UNKNOWN_LOCATION;
13398 location_t init_loc = UNKNOWN_LOCATION;
13399
13400 if (maybe_range_for_decl)
13401 *maybe_range_for_decl = NULL_TREE;
13402
13403 /* Defer access checks until we know what is being declared; the
13404 checks for names appearing in the decl-specifier-seq should be
13405 done as if we were in the scope of the thing being declared. */
13406 push_deferring_access_checks (dk_deferred);
13407
13408 /* Parse the decl-specifier-seq. We have to keep track of whether
13409 or not the decl-specifier-seq declares a named class or
13410 enumeration type, since that is the only case in which the
13411 init-declarator-list is allowed to be empty.
13412
13413 [dcl.dcl]
13414
13415 In a simple-declaration, the optional init-declarator-list can be
13416 omitted only when declaring a class or enumeration, that is when
13417 the decl-specifier-seq contains either a class-specifier, an
13418 elaborated-type-specifier, or an enum-specifier. */
13419 cp_parser_decl_specifier_seq (parser,
13420 CP_PARSER_FLAGS_OPTIONAL,
13421 &decl_specifiers,
13422 &declares_class_or_enum);
13423 /* We no longer need to defer access checks. */
13424 stop_deferring_access_checks ();
13425
13426 /* In a block scope, a valid declaration must always have a
13427 decl-specifier-seq. By not trying to parse declarators, we can
13428 resolve the declaration/expression ambiguity more quickly. */
13429 if (!function_definition_allowed_p
13430 && !decl_specifiers.any_specifiers_p)
13431 {
13432 cp_parser_error (parser, "expected declaration");
13433 goto done;
13434 }
13435
13436 /* If the next two tokens are both identifiers, the code is
13437 erroneous. The usual cause of this situation is code like:
13438
13439 T t;
13440
13441 where "T" should name a type -- but does not. */
13442 if (!decl_specifiers.any_type_specifiers_p
13443 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13444 {
13445 /* If parsing tentatively, we should commit; we really are
13446 looking at a declaration. */
13447 cp_parser_commit_to_tentative_parse (parser);
13448 /* Give up. */
13449 goto done;
13450 }
13451
13452 cp_parser_maybe_commit_to_declaration (parser,
13453 decl_specifiers.any_specifiers_p);
13454
13455 /* Look for C++17 decomposition declaration. */
13456 for (size_t n = 1; ; n++)
13457 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13458 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13459 continue;
13460 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13461 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13462 && decl_specifiers.any_specifiers_p)
13463 {
13464 tree decl
13465 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13466 maybe_range_for_decl,
13467 &init_loc);
13468
13469 /* The next token should be either a `,' or a `;'. */
13470 cp_token *token = cp_lexer_peek_token (parser->lexer);
13471 /* If it's a `;', we are done. */
13472 if (token->type == CPP_SEMICOLON)
13473 goto finish;
13474 else if (maybe_range_for_decl)
13475 {
13476 if (*maybe_range_for_decl == NULL_TREE)
13477 *maybe_range_for_decl = error_mark_node;
13478 goto finish;
13479 }
13480 /* Anything else is an error. */
13481 else
13482 {
13483 /* If we have already issued an error message we don't need
13484 to issue another one. */
13485 if ((decl != error_mark_node
13486 && DECL_INITIAL (decl) != error_mark_node)
13487 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13488 cp_parser_error (parser, "expected %<,%> or %<;%>");
13489 /* Skip tokens until we reach the end of the statement. */
13490 cp_parser_skip_to_end_of_statement (parser);
13491 /* If the next token is now a `;', consume it. */
13492 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13493 cp_lexer_consume_token (parser->lexer);
13494 goto done;
13495 }
13496 }
13497 else
13498 break;
13499
13500 tree last_type;
13501 bool auto_specifier_p;
13502 /* NULL_TREE if both variable and function declaration are allowed,
13503 error_mark_node if function declaration are not allowed and
13504 a FUNCTION_DECL that should be diagnosed if it is followed by
13505 variable declarations. */
13506 tree auto_function_declaration;
13507
13508 last_type = NULL_TREE;
13509 auto_specifier_p
13510 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13511 auto_function_declaration = NULL_TREE;
13512
13513 /* Keep going until we hit the `;' at the end of the simple
13514 declaration. */
13515 saw_declarator = false;
13516 while (cp_lexer_next_token_is_not (parser->lexer,
13517 CPP_SEMICOLON))
13518 {
13519 cp_token *token;
13520 bool function_definition_p;
13521 tree decl;
13522 tree auto_result = NULL_TREE;
13523
13524 if (saw_declarator)
13525 {
13526 /* If we are processing next declarator, comma is expected */
13527 token = cp_lexer_peek_token (parser->lexer);
13528 gcc_assert (token->type == CPP_COMMA);
13529 cp_lexer_consume_token (parser->lexer);
13530 if (maybe_range_for_decl)
13531 {
13532 *maybe_range_for_decl = error_mark_node;
13533 if (comma_loc == UNKNOWN_LOCATION)
13534 comma_loc = token->location;
13535 }
13536 }
13537 else
13538 saw_declarator = true;
13539
13540 /* Parse the init-declarator. */
13541 decl = cp_parser_init_declarator (parser,
13542 CP_PARSER_FLAGS_NONE,
13543 &decl_specifiers,
13544 /*checks=*/NULL,
13545 function_definition_allowed_p,
13546 /*member_p=*/false,
13547 declares_class_or_enum,
13548 &function_definition_p,
13549 maybe_range_for_decl,
13550 &init_loc,
13551 &auto_result);
13552 /* If an error occurred while parsing tentatively, exit quickly.
13553 (That usually happens when in the body of a function; each
13554 statement is treated as a declaration-statement until proven
13555 otherwise.) */
13556 if (cp_parser_error_occurred (parser))
13557 goto done;
13558
13559 if (auto_specifier_p && cxx_dialect >= cxx14)
13560 {
13561 /* If the init-declarator-list contains more than one
13562 init-declarator, they shall all form declarations of
13563 variables. */
13564 if (auto_function_declaration == NULL_TREE)
13565 auto_function_declaration
13566 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13567 else if (TREE_CODE (decl) == FUNCTION_DECL
13568 || auto_function_declaration != error_mark_node)
13569 {
13570 error_at (decl_specifiers.locations[ds_type_spec],
13571 "non-variable %qD in declaration with more than one "
13572 "declarator with placeholder type",
13573 TREE_CODE (decl) == FUNCTION_DECL
13574 ? decl : auto_function_declaration);
13575 auto_function_declaration = error_mark_node;
13576 }
13577 }
13578
13579 if (auto_result
13580 && (!processing_template_decl || !type_uses_auto (auto_result)))
13581 {
13582 if (last_type
13583 && last_type != error_mark_node
13584 && !same_type_p (auto_result, last_type))
13585 {
13586 /* If the list of declarators contains more than one declarator,
13587 the type of each declared variable is determined as described
13588 above. If the type deduced for the template parameter U is not
13589 the same in each deduction, the program is ill-formed. */
13590 error_at (decl_specifiers.locations[ds_type_spec],
13591 "inconsistent deduction for %qT: %qT and then %qT",
13592 decl_specifiers.type, last_type, auto_result);
13593 last_type = error_mark_node;
13594 }
13595 else
13596 last_type = auto_result;
13597 }
13598
13599 /* Handle function definitions specially. */
13600 if (function_definition_p)
13601 {
13602 /* If the next token is a `,', then we are probably
13603 processing something like:
13604
13605 void f() {}, *p;
13606
13607 which is erroneous. */
13608 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13609 {
13610 cp_token *token = cp_lexer_peek_token (parser->lexer);
13611 error_at (token->location,
13612 "mixing"
13613 " declarations and function-definitions is forbidden");
13614 }
13615 /* Otherwise, we're done with the list of declarators. */
13616 else
13617 {
13618 pop_deferring_access_checks ();
13619 return;
13620 }
13621 }
13622 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13623 *maybe_range_for_decl = decl;
13624 /* The next token should be either a `,' or a `;'. */
13625 token = cp_lexer_peek_token (parser->lexer);
13626 /* If it's a `,', there are more declarators to come. */
13627 if (token->type == CPP_COMMA)
13628 /* will be consumed next time around */;
13629 /* If it's a `;', we are done. */
13630 else if (token->type == CPP_SEMICOLON)
13631 break;
13632 else if (maybe_range_for_decl)
13633 {
13634 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13635 permerror (decl_specifiers.locations[ds_type_spec],
13636 "types may not be defined in a for-range-declaration");
13637 break;
13638 }
13639 /* Anything else is an error. */
13640 else
13641 {
13642 /* If we have already issued an error message we don't need
13643 to issue another one. */
13644 if ((decl != error_mark_node
13645 && DECL_INITIAL (decl) != error_mark_node)
13646 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13647 cp_parser_error (parser, "expected %<,%> or %<;%>");
13648 /* Skip tokens until we reach the end of the statement. */
13649 cp_parser_skip_to_end_of_statement (parser);
13650 /* If the next token is now a `;', consume it. */
13651 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13652 cp_lexer_consume_token (parser->lexer);
13653 goto done;
13654 }
13655 /* After the first time around, a function-definition is not
13656 allowed -- even if it was OK at first. For example:
13657
13658 int i, f() {}
13659
13660 is not valid. */
13661 function_definition_allowed_p = false;
13662 }
13663
13664 /* Issue an error message if no declarators are present, and the
13665 decl-specifier-seq does not itself declare a class or
13666 enumeration: [dcl.dcl]/3. */
13667 if (!saw_declarator)
13668 {
13669 if (cp_parser_declares_only_class_p (parser))
13670 {
13671 if (!declares_class_or_enum
13672 && decl_specifiers.type
13673 && OVERLOAD_TYPE_P (decl_specifiers.type))
13674 /* Ensure an error is issued anyway when finish_decltype_type,
13675 called via cp_parser_decl_specifier_seq, returns a class or
13676 an enumeration (c++/51786). */
13677 decl_specifiers.type = NULL_TREE;
13678 shadow_tag (&decl_specifiers);
13679 }
13680 /* Perform any deferred access checks. */
13681 perform_deferred_access_checks (tf_warning_or_error);
13682 }
13683
13684 /* Consume the `;'. */
13685 finish:
13686 if (!maybe_range_for_decl)
13687 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13688 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13689 {
13690 if (init_loc != UNKNOWN_LOCATION)
13691 error_at (init_loc, "initializer in range-based %<for%> loop");
13692 if (comma_loc != UNKNOWN_LOCATION)
13693 error_at (comma_loc,
13694 "multiple declarations in range-based %<for%> loop");
13695 }
13696
13697 done:
13698 pop_deferring_access_checks ();
13699 }
13700
13701 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13702 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13703 initializer ; */
13704
13705 static tree
13706 cp_parser_decomposition_declaration (cp_parser *parser,
13707 cp_decl_specifier_seq *decl_specifiers,
13708 tree *maybe_range_for_decl,
13709 location_t *init_loc)
13710 {
13711 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13712 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13713 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13714
13715 /* Parse the identifier-list. */
13716 auto_vec<cp_expr, 10> v;
13717 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13718 while (true)
13719 {
13720 cp_expr e = cp_parser_identifier (parser);
13721 if (e.get_value () == error_mark_node)
13722 break;
13723 v.safe_push (e);
13724 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13725 break;
13726 cp_lexer_consume_token (parser->lexer);
13727 }
13728
13729 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13730 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13731 {
13732 end_loc = UNKNOWN_LOCATION;
13733 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13734 false);
13735 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13736 cp_lexer_consume_token (parser->lexer);
13737 else
13738 {
13739 cp_parser_skip_to_end_of_statement (parser);
13740 return error_mark_node;
13741 }
13742 }
13743
13744 if (cxx_dialect < cxx17)
13745 pedwarn (loc, 0, "structured bindings only available with "
13746 "%<-std=c++17%> or %<-std=gnu++17%>");
13747
13748 tree pushed_scope;
13749 cp_declarator *declarator = make_declarator (cdk_decomp);
13750 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13751 declarator->id_loc = loc;
13752 if (ref_qual != REF_QUAL_NONE)
13753 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13754 ref_qual == REF_QUAL_RVALUE,
13755 NULL_TREE);
13756 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13757 NULL_TREE, decl_specifiers->attributes,
13758 &pushed_scope);
13759 tree orig_decl = decl;
13760
13761 unsigned int i;
13762 cp_expr e;
13763 cp_decl_specifier_seq decl_specs;
13764 clear_decl_specs (&decl_specs);
13765 decl_specs.type = make_auto ();
13766 tree prev = decl;
13767 FOR_EACH_VEC_ELT (v, i, e)
13768 {
13769 if (i == 0)
13770 declarator = make_id_declarator (NULL_TREE, e.get_value (),
13771 sfk_none, e.get_location ());
13772 else
13773 {
13774 declarator->u.id.unqualified_name = e.get_value ();
13775 declarator->id_loc = e.get_location ();
13776 }
13777 tree elt_pushed_scope;
13778 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13779 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13780 if (decl2 == error_mark_node)
13781 decl = error_mark_node;
13782 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13783 {
13784 /* Ensure we've diagnosed redeclaration if we aren't creating
13785 a new VAR_DECL. */
13786 gcc_assert (errorcount);
13787 decl = error_mark_node;
13788 }
13789 else
13790 prev = decl2;
13791 if (elt_pushed_scope)
13792 pop_scope (elt_pushed_scope);
13793 }
13794
13795 if (v.is_empty ())
13796 {
13797 error_at (loc, "empty structured binding declaration");
13798 decl = error_mark_node;
13799 }
13800
13801 if (maybe_range_for_decl == NULL
13802 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13803 {
13804 bool non_constant_p = false, is_direct_init = false;
13805 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13806 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13807 &non_constant_p);
13808 if (initializer == NULL_TREE
13809 || (TREE_CODE (initializer) == TREE_LIST
13810 && TREE_CHAIN (initializer))
13811 || (is_direct_init
13812 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13813 && CONSTRUCTOR_NELTS (initializer) != 1))
13814 {
13815 error_at (loc, "invalid initializer for structured binding "
13816 "declaration");
13817 initializer = error_mark_node;
13818 }
13819
13820 if (decl != error_mark_node)
13821 {
13822 cp_maybe_mangle_decomp (decl, prev, v.length ());
13823 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13824 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13825 cp_finish_decomp (decl, prev, v.length ());
13826 }
13827 }
13828 else if (decl != error_mark_node)
13829 {
13830 *maybe_range_for_decl = prev;
13831 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13832 the underlying DECL. */
13833 cp_finish_decomp (decl, prev, v.length ());
13834 }
13835
13836 if (pushed_scope)
13837 pop_scope (pushed_scope);
13838
13839 if (decl == error_mark_node && DECL_P (orig_decl))
13840 {
13841 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13842 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13843 }
13844
13845 return decl;
13846 }
13847
13848 /* Parse a decl-specifier-seq.
13849
13850 decl-specifier-seq:
13851 decl-specifier-seq [opt] decl-specifier
13852 decl-specifier attribute-specifier-seq [opt] (C++11)
13853
13854 decl-specifier:
13855 storage-class-specifier
13856 type-specifier
13857 function-specifier
13858 friend
13859 typedef
13860
13861 GNU Extension:
13862
13863 decl-specifier:
13864 attributes
13865
13866 Concepts Extension:
13867
13868 decl-specifier:
13869 concept
13870
13871 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13872
13873 The parser flags FLAGS is used to control type-specifier parsing.
13874
13875 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13876 flags:
13877
13878 1: one of the decl-specifiers is an elaborated-type-specifier
13879 (i.e., a type declaration)
13880 2: one of the decl-specifiers is an enum-specifier or a
13881 class-specifier (i.e., a type definition)
13882
13883 */
13884
13885 static void
13886 cp_parser_decl_specifier_seq (cp_parser* parser,
13887 cp_parser_flags flags,
13888 cp_decl_specifier_seq *decl_specs,
13889 int* declares_class_or_enum)
13890 {
13891 bool constructor_possible_p = !parser->in_declarator_p;
13892 bool found_decl_spec = false;
13893 cp_token *start_token = NULL;
13894 cp_decl_spec ds;
13895
13896 /* Clear DECL_SPECS. */
13897 clear_decl_specs (decl_specs);
13898
13899 /* Assume no class or enumeration type is declared. */
13900 *declares_class_or_enum = 0;
13901
13902 /* Keep reading specifiers until there are no more to read. */
13903 while (true)
13904 {
13905 bool constructor_p;
13906 cp_token *token;
13907 ds = ds_last;
13908
13909 /* Peek at the next token. */
13910 token = cp_lexer_peek_token (parser->lexer);
13911
13912 /* Save the first token of the decl spec list for error
13913 reporting. */
13914 if (!start_token)
13915 start_token = token;
13916 /* Handle attributes. */
13917 if (cp_next_tokens_can_be_attribute_p (parser))
13918 {
13919 /* Parse the attributes. */
13920 tree attrs = cp_parser_attributes_opt (parser);
13921
13922 /* In a sequence of declaration specifiers, c++11 attributes
13923 appertain to the type that precede them. In that case
13924 [dcl.spec]/1 says:
13925
13926 The attribute-specifier-seq affects the type only for
13927 the declaration it appears in, not other declarations
13928 involving the same type.
13929
13930 But for now let's force the user to position the
13931 attribute either at the beginning of the declaration or
13932 after the declarator-id, which would clearly mean that it
13933 applies to the declarator. */
13934 if (cxx11_attribute_p (attrs))
13935 {
13936 if (!found_decl_spec)
13937 /* The c++11 attribute is at the beginning of the
13938 declaration. It appertains to the entity being
13939 declared. */;
13940 else
13941 {
13942 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13943 {
13944 /* This is an attribute following a
13945 class-specifier. */
13946 if (decl_specs->type_definition_p)
13947 warn_misplaced_attr_for_class_type (token->location,
13948 decl_specs->type);
13949 attrs = NULL_TREE;
13950 }
13951 else
13952 {
13953 decl_specs->std_attributes
13954 = attr_chainon (decl_specs->std_attributes, attrs);
13955 if (decl_specs->locations[ds_std_attribute] == 0)
13956 decl_specs->locations[ds_std_attribute] = token->location;
13957 }
13958 continue;
13959 }
13960 }
13961
13962 decl_specs->attributes
13963 = attr_chainon (decl_specs->attributes, attrs);
13964 if (decl_specs->locations[ds_attribute] == 0)
13965 decl_specs->locations[ds_attribute] = token->location;
13966 continue;
13967 }
13968 /* Assume we will find a decl-specifier keyword. */
13969 found_decl_spec = true;
13970 /* If the next token is an appropriate keyword, we can simply
13971 add it to the list. */
13972 switch (token->keyword)
13973 {
13974 /* decl-specifier:
13975 friend
13976 constexpr */
13977 case RID_FRIEND:
13978 if (!at_class_scope_p ())
13979 {
13980 gcc_rich_location richloc (token->location);
13981 richloc.add_fixit_remove ();
13982 error_at (&richloc, "%<friend%> used outside of class");
13983 cp_lexer_purge_token (parser->lexer);
13984 }
13985 else
13986 {
13987 ds = ds_friend;
13988 /* Consume the token. */
13989 cp_lexer_consume_token (parser->lexer);
13990 }
13991 break;
13992
13993 case RID_CONSTEXPR:
13994 ds = ds_constexpr;
13995 cp_lexer_consume_token (parser->lexer);
13996 break;
13997
13998 case RID_CONCEPT:
13999 ds = ds_concept;
14000 cp_lexer_consume_token (parser->lexer);
14001 /* In C++20 a concept definition is just 'concept name = expr;'
14002 Support that syntax by pretending we've seen 'bool'. */
14003 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14004 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
14005 {
14006 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
14007 token, /*type_definition*/false);
14008 decl_specs->any_type_specifiers_p = true;
14009 }
14010 break;
14011
14012 /* function-specifier:
14013 inline
14014 virtual
14015 explicit */
14016 case RID_INLINE:
14017 case RID_VIRTUAL:
14018 case RID_EXPLICIT:
14019 cp_parser_function_specifier_opt (parser, decl_specs);
14020 break;
14021
14022 /* decl-specifier:
14023 typedef */
14024 case RID_TYPEDEF:
14025 ds = ds_typedef;
14026 /* Consume the token. */
14027 cp_lexer_consume_token (parser->lexer);
14028 /* A constructor declarator cannot appear in a typedef. */
14029 constructor_possible_p = false;
14030 /* The "typedef" keyword can only occur in a declaration; we
14031 may as well commit at this point. */
14032 cp_parser_commit_to_tentative_parse (parser);
14033
14034 if (decl_specs->storage_class != sc_none)
14035 decl_specs->conflicting_specifiers_p = true;
14036 break;
14037
14038 /* storage-class-specifier:
14039 auto
14040 register
14041 static
14042 extern
14043 mutable
14044
14045 GNU Extension:
14046 thread */
14047 case RID_AUTO:
14048 if (cxx_dialect == cxx98)
14049 {
14050 /* Consume the token. */
14051 cp_lexer_consume_token (parser->lexer);
14052
14053 /* Complain about `auto' as a storage specifier, if
14054 we're complaining about C++0x compatibility. */
14055 gcc_rich_location richloc (token->location);
14056 richloc.add_fixit_remove ();
14057 warning_at (&richloc, OPT_Wc__11_compat,
14058 "%<auto%> changes meaning in C++11; "
14059 "please remove it");
14060
14061 /* Set the storage class anyway. */
14062 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
14063 token);
14064 }
14065 else
14066 /* C++0x auto type-specifier. */
14067 found_decl_spec = false;
14068 break;
14069
14070 case RID_REGISTER:
14071 case RID_STATIC:
14072 case RID_EXTERN:
14073 case RID_MUTABLE:
14074 /* Consume the token. */
14075 cp_lexer_consume_token (parser->lexer);
14076 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
14077 token);
14078 break;
14079 case RID_THREAD:
14080 /* Consume the token. */
14081 ds = ds_thread;
14082 cp_lexer_consume_token (parser->lexer);
14083 break;
14084
14085 default:
14086 /* We did not yet find a decl-specifier yet. */
14087 found_decl_spec = false;
14088 break;
14089 }
14090
14091 if (found_decl_spec
14092 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
14093 && token->keyword != RID_CONSTEXPR)
14094 error ("decl-specifier invalid in condition");
14095
14096 if (found_decl_spec
14097 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14098 && token->keyword != RID_MUTABLE
14099 && token->keyword != RID_CONSTEXPR)
14100 error_at (token->location, "%qD invalid in lambda",
14101 ridpointers[token->keyword]);
14102
14103 if (ds != ds_last)
14104 set_and_check_decl_spec_loc (decl_specs, ds, token);
14105
14106 /* Constructors are a special case. The `S' in `S()' is not a
14107 decl-specifier; it is the beginning of the declarator. */
14108 constructor_p
14109 = (!found_decl_spec
14110 && constructor_possible_p
14111 && (cp_parser_constructor_declarator_p
14112 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
14113 ds_friend))));
14114
14115 /* If we don't have a DECL_SPEC yet, then we must be looking at
14116 a type-specifier. */
14117 if (!found_decl_spec && !constructor_p)
14118 {
14119 int decl_spec_declares_class_or_enum;
14120 bool is_cv_qualifier;
14121 tree type_spec;
14122
14123 type_spec
14124 = cp_parser_type_specifier (parser, flags,
14125 decl_specs,
14126 /*is_declaration=*/true,
14127 &decl_spec_declares_class_or_enum,
14128 &is_cv_qualifier);
14129 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
14130
14131 /* If this type-specifier referenced a user-defined type
14132 (a typedef, class-name, etc.), then we can't allow any
14133 more such type-specifiers henceforth.
14134
14135 [dcl.spec]
14136
14137 The longest sequence of decl-specifiers that could
14138 possibly be a type name is taken as the
14139 decl-specifier-seq of a declaration. The sequence shall
14140 be self-consistent as described below.
14141
14142 [dcl.type]
14143
14144 As a general rule, at most one type-specifier is allowed
14145 in the complete decl-specifier-seq of a declaration. The
14146 only exceptions are the following:
14147
14148 -- const or volatile can be combined with any other
14149 type-specifier.
14150
14151 -- signed or unsigned can be combined with char, long,
14152 short, or int.
14153
14154 -- ..
14155
14156 Example:
14157
14158 typedef char* Pc;
14159 void g (const int Pc);
14160
14161 Here, Pc is *not* part of the decl-specifier seq; it's
14162 the declarator. Therefore, once we see a type-specifier
14163 (other than a cv-qualifier), we forbid any additional
14164 user-defined types. We *do* still allow things like `int
14165 int' to be considered a decl-specifier-seq, and issue the
14166 error message later. */
14167 if (type_spec && !is_cv_qualifier)
14168 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14169 /* A constructor declarator cannot follow a type-specifier. */
14170 if (type_spec)
14171 {
14172 constructor_possible_p = false;
14173 found_decl_spec = true;
14174 if (!is_cv_qualifier)
14175 decl_specs->any_type_specifiers_p = true;
14176
14177 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
14178 error_at (token->location, "type-specifier invalid in lambda");
14179 }
14180 }
14181
14182 /* If we still do not have a DECL_SPEC, then there are no more
14183 decl-specifiers. */
14184 if (!found_decl_spec)
14185 break;
14186
14187 decl_specs->any_specifiers_p = true;
14188 /* After we see one decl-specifier, further decl-specifiers are
14189 always optional. */
14190 flags |= CP_PARSER_FLAGS_OPTIONAL;
14191 }
14192
14193 /* Don't allow a friend specifier with a class definition. */
14194 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
14195 && (*declares_class_or_enum & 2))
14196 error_at (decl_specs->locations[ds_friend],
14197 "class definition may not be declared a friend");
14198 }
14199
14200 /* Parse an (optional) storage-class-specifier.
14201
14202 storage-class-specifier:
14203 auto
14204 register
14205 static
14206 extern
14207 mutable
14208
14209 GNU Extension:
14210
14211 storage-class-specifier:
14212 thread
14213
14214 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14215
14216 static tree
14217 cp_parser_storage_class_specifier_opt (cp_parser* parser)
14218 {
14219 switch (cp_lexer_peek_token (parser->lexer)->keyword)
14220 {
14221 case RID_AUTO:
14222 if (cxx_dialect != cxx98)
14223 return NULL_TREE;
14224 /* Fall through for C++98. */
14225 gcc_fallthrough ();
14226
14227 case RID_REGISTER:
14228 case RID_STATIC:
14229 case RID_EXTERN:
14230 case RID_MUTABLE:
14231 case RID_THREAD:
14232 /* Consume the token. */
14233 return cp_lexer_consume_token (parser->lexer)->u.value;
14234
14235 default:
14236 return NULL_TREE;
14237 }
14238 }
14239
14240 /* Parse an (optional) function-specifier.
14241
14242 function-specifier:
14243 inline
14244 virtual
14245 explicit
14246
14247 C++2A Extension:
14248 explicit(constant-expression)
14249
14250 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14251 Updates DECL_SPECS, if it is non-NULL. */
14252
14253 static tree
14254 cp_parser_function_specifier_opt (cp_parser* parser,
14255 cp_decl_specifier_seq *decl_specs)
14256 {
14257 cp_token *token = cp_lexer_peek_token (parser->lexer);
14258 switch (token->keyword)
14259 {
14260 case RID_INLINE:
14261 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
14262 break;
14263
14264 case RID_VIRTUAL:
14265 /* 14.5.2.3 [temp.mem]
14266
14267 A member function template shall not be virtual. */
14268 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14269 && current_class_type)
14270 error_at (token->location, "templates may not be %<virtual%>");
14271 else
14272 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
14273 break;
14274
14275 case RID_EXPLICIT:
14276 {
14277 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
14278 /* If we see '(', it's C++20 explicit(bool). */
14279 tree expr;
14280 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14281 {
14282 matching_parens parens;
14283 parens.consume_open (parser);
14284
14285 /* New types are not allowed in an explicit-specifier. */
14286 const char *saved_message
14287 = parser->type_definition_forbidden_message;
14288 parser->type_definition_forbidden_message
14289 = G_("types may not be defined in explicit-specifier");
14290
14291 if (cxx_dialect < cxx2a)
14292 pedwarn (token->location, 0,
14293 "%<explicit(bool)%> only available with %<-std=c++2a%> "
14294 "or %<-std=gnu++2a%>");
14295
14296 /* Parse the constant-expression. */
14297 expr = cp_parser_constant_expression (parser);
14298
14299 /* Restore the saved message. */
14300 parser->type_definition_forbidden_message = saved_message;
14301 parens.require_close (parser);
14302 }
14303 else
14304 /* The explicit-specifier explicit without a constant-expression is
14305 equivalent to the explicit-specifier explicit(true). */
14306 expr = boolean_true_node;
14307
14308 /* [dcl.fct.spec]
14309 "the constant-expression, if supplied, shall be a contextually
14310 converted constant expression of type bool." */
14311 expr = build_explicit_specifier (expr, tf_warning_or_error);
14312 /* We could evaluate it -- mark the decl as appropriate. */
14313 if (expr == boolean_true_node)
14314 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14315 else if (expr == boolean_false_node)
14316 /* Don't mark the decl as explicit. */;
14317 else if (decl_specs)
14318 /* The expression was value-dependent. Remember it so that we can
14319 substitute it later. */
14320 decl_specs->explicit_specifier = expr;
14321 return id;
14322 }
14323
14324 default:
14325 return NULL_TREE;
14326 }
14327
14328 /* Consume the token. */
14329 return cp_lexer_consume_token (parser->lexer)->u.value;
14330 }
14331
14332 /* Parse a linkage-specification.
14333
14334 linkage-specification:
14335 extern string-literal { declaration-seq [opt] }
14336 extern string-literal declaration */
14337
14338 static void
14339 cp_parser_linkage_specification (cp_parser* parser)
14340 {
14341 tree linkage;
14342
14343 /* Look for the `extern' keyword. */
14344 cp_token *extern_token
14345 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14346
14347 /* Look for the string-literal. */
14348 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14349 linkage = cp_parser_string_literal (parser, false, false);
14350
14351 /* Transform the literal into an identifier. If the literal is a
14352 wide-character string, or contains embedded NULs, then we can't
14353 handle it as the user wants. */
14354 if (strlen (TREE_STRING_POINTER (linkage))
14355 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14356 {
14357 cp_parser_error (parser, "invalid linkage-specification");
14358 /* Assume C++ linkage. */
14359 linkage = lang_name_cplusplus;
14360 }
14361 else
14362 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14363
14364 /* We're now using the new linkage. */
14365 push_lang_context (linkage);
14366
14367 /* Preserve the location of the the innermost linkage specification,
14368 tracking the locations of nested specifications via a local. */
14369 location_t saved_location
14370 = parser->innermost_linkage_specification_location;
14371 /* Construct a location ranging from the start of the "extern" to
14372 the end of the string-literal, with the caret at the start, e.g.:
14373 extern "C" {
14374 ^~~~~~~~~~
14375 */
14376 parser->innermost_linkage_specification_location
14377 = make_location (extern_token->location,
14378 extern_token->location,
14379 get_finish (string_token->location));
14380
14381 /* If the next token is a `{', then we're using the first
14382 production. */
14383 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14384 {
14385 cp_ensure_no_omp_declare_simd (parser);
14386 cp_ensure_no_oacc_routine (parser);
14387
14388 /* Consume the `{' token. */
14389 matching_braces braces;
14390 braces.consume_open (parser);
14391 /* Parse the declarations. */
14392 cp_parser_declaration_seq_opt (parser);
14393 /* Look for the closing `}'. */
14394 braces.require_close (parser);
14395 }
14396 /* Otherwise, there's just one declaration. */
14397 else
14398 {
14399 bool saved_in_unbraced_linkage_specification_p;
14400
14401 saved_in_unbraced_linkage_specification_p
14402 = parser->in_unbraced_linkage_specification_p;
14403 parser->in_unbraced_linkage_specification_p = true;
14404 cp_parser_declaration (parser);
14405 parser->in_unbraced_linkage_specification_p
14406 = saved_in_unbraced_linkage_specification_p;
14407 }
14408
14409 /* We're done with the linkage-specification. */
14410 pop_lang_context ();
14411
14412 /* Restore location of parent linkage specification, if any. */
14413 parser->innermost_linkage_specification_location = saved_location;
14414 }
14415
14416 /* Parse a static_assert-declaration.
14417
14418 static_assert-declaration:
14419 static_assert ( constant-expression , string-literal ) ;
14420 static_assert ( constant-expression ) ; (C++17)
14421
14422 If MEMBER_P, this static_assert is a class member. */
14423
14424 static void
14425 cp_parser_static_assert(cp_parser *parser, bool member_p)
14426 {
14427 cp_expr condition;
14428 location_t token_loc;
14429 tree message;
14430 bool dummy;
14431
14432 /* Peek at the `static_assert' token so we can keep track of exactly
14433 where the static assertion started. */
14434 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14435
14436 /* Look for the `static_assert' keyword. */
14437 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14438 RT_STATIC_ASSERT))
14439 return;
14440
14441 /* We know we are in a static assertion; commit to any tentative
14442 parse. */
14443 if (cp_parser_parsing_tentatively (parser))
14444 cp_parser_commit_to_tentative_parse (parser);
14445
14446 /* Parse the `(' starting the static assertion condition. */
14447 matching_parens parens;
14448 parens.require_open (parser);
14449
14450 /* Parse the constant-expression. Allow a non-constant expression
14451 here in order to give better diagnostics in finish_static_assert. */
14452 condition =
14453 cp_parser_constant_expression (parser,
14454 /*allow_non_constant_p=*/true,
14455 /*non_constant_p=*/&dummy);
14456
14457 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14458 {
14459 if (cxx_dialect < cxx17)
14460 pedwarn (input_location, OPT_Wpedantic,
14461 "static_assert without a message "
14462 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
14463 /* Eat the ')' */
14464 cp_lexer_consume_token (parser->lexer);
14465 message = build_string (1, "");
14466 TREE_TYPE (message) = char_array_type_node;
14467 fix_string_type (message);
14468 }
14469 else
14470 {
14471 /* Parse the separating `,'. */
14472 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14473
14474 /* Parse the string-literal message. */
14475 message = cp_parser_string_literal (parser,
14476 /*translate=*/false,
14477 /*wide_ok=*/true);
14478
14479 /* A `)' completes the static assertion. */
14480 if (!parens.require_close (parser))
14481 cp_parser_skip_to_closing_parenthesis (parser,
14482 /*recovering=*/true,
14483 /*or_comma=*/false,
14484 /*consume_paren=*/true);
14485 }
14486
14487 /* A semicolon terminates the declaration. */
14488 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14489
14490 /* Get the location for the static assertion. Use that of the
14491 condition if available, otherwise, use that of the "static_assert"
14492 token. */
14493 location_t assert_loc = condition.get_location ();
14494 if (assert_loc == UNKNOWN_LOCATION)
14495 assert_loc = token_loc;
14496
14497 /* Complete the static assertion, which may mean either processing
14498 the static assert now or saving it for template instantiation. */
14499 finish_static_assert (condition, message, assert_loc, member_p);
14500 }
14501
14502 /* Parse the expression in decltype ( expression ). */
14503
14504 static tree
14505 cp_parser_decltype_expr (cp_parser *parser,
14506 bool &id_expression_or_member_access_p)
14507 {
14508 cp_token *id_expr_start_token;
14509 tree expr;
14510
14511 /* Since we're going to preserve any side-effects from this parse, set up a
14512 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14513 in the expression. */
14514 tentative_firewall firewall (parser);
14515
14516 /* First, try parsing an id-expression. */
14517 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14518 cp_parser_parse_tentatively (parser);
14519 expr = cp_parser_id_expression (parser,
14520 /*template_keyword_p=*/false,
14521 /*check_dependency_p=*/true,
14522 /*template_p=*/NULL,
14523 /*declarator_p=*/false,
14524 /*optional_p=*/false);
14525
14526 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14527 {
14528 bool non_integral_constant_expression_p = false;
14529 tree id_expression = expr;
14530 cp_id_kind idk;
14531 const char *error_msg;
14532
14533 if (identifier_p (expr))
14534 /* Lookup the name we got back from the id-expression. */
14535 expr = cp_parser_lookup_name_simple (parser, expr,
14536 id_expr_start_token->location);
14537
14538 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14539 /* A template without args is not a complete id-expression. */
14540 expr = error_mark_node;
14541
14542 if (expr
14543 && expr != error_mark_node
14544 && TREE_CODE (expr) != TYPE_DECL
14545 && (TREE_CODE (expr) != BIT_NOT_EXPR
14546 || !TYPE_P (TREE_OPERAND (expr, 0)))
14547 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14548 {
14549 /* Complete lookup of the id-expression. */
14550 expr = (finish_id_expression
14551 (id_expression, expr, parser->scope, &idk,
14552 /*integral_constant_expression_p=*/false,
14553 /*allow_non_integral_constant_expression_p=*/true,
14554 &non_integral_constant_expression_p,
14555 /*template_p=*/false,
14556 /*done=*/true,
14557 /*address_p=*/false,
14558 /*template_arg_p=*/false,
14559 &error_msg,
14560 id_expr_start_token->location));
14561
14562 if (expr == error_mark_node)
14563 /* We found an id-expression, but it was something that we
14564 should not have found. This is an error, not something
14565 we can recover from, so note that we found an
14566 id-expression and we'll recover as gracefully as
14567 possible. */
14568 id_expression_or_member_access_p = true;
14569 }
14570
14571 if (expr
14572 && expr != error_mark_node
14573 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14574 /* We have an id-expression. */
14575 id_expression_or_member_access_p = true;
14576 }
14577
14578 if (!id_expression_or_member_access_p)
14579 {
14580 /* Abort the id-expression parse. */
14581 cp_parser_abort_tentative_parse (parser);
14582
14583 /* Parsing tentatively, again. */
14584 cp_parser_parse_tentatively (parser);
14585
14586 /* Parse a class member access. */
14587 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14588 /*cast_p=*/false, /*decltype*/true,
14589 /*member_access_only_p=*/true, NULL);
14590
14591 if (expr
14592 && expr != error_mark_node
14593 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14594 /* We have an id-expression. */
14595 id_expression_or_member_access_p = true;
14596 }
14597
14598 if (id_expression_or_member_access_p)
14599 /* We have parsed the complete id-expression or member access. */
14600 cp_parser_parse_definitely (parser);
14601 else
14602 {
14603 /* Abort our attempt to parse an id-expression or member access
14604 expression. */
14605 cp_parser_abort_tentative_parse (parser);
14606
14607 /* Commit to the tentative_firewall so we get syntax errors. */
14608 cp_parser_commit_to_tentative_parse (parser);
14609
14610 /* Parse a full expression. */
14611 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14612 /*decltype_p=*/true);
14613 }
14614
14615 return expr;
14616 }
14617
14618 /* Parse a `decltype' type. Returns the type.
14619
14620 simple-type-specifier:
14621 decltype ( expression )
14622 C++14 proposal:
14623 decltype ( auto ) */
14624
14625 static tree
14626 cp_parser_decltype (cp_parser *parser)
14627 {
14628 bool id_expression_or_member_access_p = false;
14629 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14630
14631 if (start_token->type == CPP_DECLTYPE)
14632 {
14633 /* Already parsed. */
14634 cp_lexer_consume_token (parser->lexer);
14635 return saved_checks_value (start_token->u.tree_check_value);
14636 }
14637
14638 /* Look for the `decltype' token. */
14639 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14640 return error_mark_node;
14641
14642 /* Parse the opening `('. */
14643 matching_parens parens;
14644 if (!parens.require_open (parser))
14645 return error_mark_node;
14646
14647 push_deferring_access_checks (dk_deferred);
14648
14649 tree expr = NULL_TREE;
14650
14651 if (cxx_dialect >= cxx14
14652 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14653 /* decltype (auto) */
14654 cp_lexer_consume_token (parser->lexer);
14655 else
14656 {
14657 /* decltype (expression) */
14658
14659 /* Types cannot be defined in a `decltype' expression. Save away the
14660 old message and set the new one. */
14661 const char *saved_message = parser->type_definition_forbidden_message;
14662 parser->type_definition_forbidden_message
14663 = G_("types may not be defined in %<decltype%> expressions");
14664
14665 /* The restrictions on constant-expressions do not apply inside
14666 decltype expressions. */
14667 bool saved_integral_constant_expression_p
14668 = parser->integral_constant_expression_p;
14669 bool saved_non_integral_constant_expression_p
14670 = parser->non_integral_constant_expression_p;
14671 parser->integral_constant_expression_p = false;
14672
14673 /* Within a parenthesized expression, a `>' token is always
14674 the greater-than operator. */
14675 bool saved_greater_than_is_operator_p
14676 = parser->greater_than_is_operator_p;
14677 parser->greater_than_is_operator_p = true;
14678
14679 /* Do not actually evaluate the expression. */
14680 ++cp_unevaluated_operand;
14681
14682 /* Do not warn about problems with the expression. */
14683 ++c_inhibit_evaluation_warnings;
14684
14685 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14686 STRIP_ANY_LOCATION_WRAPPER (expr);
14687
14688 /* Go back to evaluating expressions. */
14689 --cp_unevaluated_operand;
14690 --c_inhibit_evaluation_warnings;
14691
14692 /* The `>' token might be the end of a template-id or
14693 template-parameter-list now. */
14694 parser->greater_than_is_operator_p
14695 = saved_greater_than_is_operator_p;
14696
14697 /* Restore the old message and the integral constant expression
14698 flags. */
14699 parser->type_definition_forbidden_message = saved_message;
14700 parser->integral_constant_expression_p
14701 = saved_integral_constant_expression_p;
14702 parser->non_integral_constant_expression_p
14703 = saved_non_integral_constant_expression_p;
14704 }
14705
14706 /* Parse to the closing `)'. */
14707 if (!parens.require_close (parser))
14708 {
14709 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14710 /*consume_paren=*/true);
14711 pop_deferring_access_checks ();
14712 return error_mark_node;
14713 }
14714
14715 if (!expr)
14716 {
14717 /* Build auto. */
14718 expr = make_decltype_auto ();
14719 AUTO_IS_DECLTYPE (expr) = true;
14720 }
14721 else
14722 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14723 tf_warning_or_error);
14724
14725 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14726 it again. */
14727 start_token->type = CPP_DECLTYPE;
14728 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14729 start_token->u.tree_check_value->value = expr;
14730 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14731 start_token->keyword = RID_MAX;
14732 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14733
14734 pop_to_parent_deferring_access_checks ();
14735
14736 return expr;
14737 }
14738
14739 /* Special member functions [gram.special] */
14740
14741 /* Parse a conversion-function-id.
14742
14743 conversion-function-id:
14744 operator conversion-type-id
14745
14746 Returns an IDENTIFIER_NODE representing the operator. */
14747
14748 static tree
14749 cp_parser_conversion_function_id (cp_parser* parser)
14750 {
14751 tree type;
14752 tree saved_scope;
14753 tree saved_qualifying_scope;
14754 tree saved_object_scope;
14755 tree pushed_scope = NULL_TREE;
14756
14757 /* Look for the `operator' token. */
14758 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14759 return error_mark_node;
14760 /* When we parse the conversion-type-id, the current scope will be
14761 reset. However, we need that information in able to look up the
14762 conversion function later, so we save it here. */
14763 saved_scope = parser->scope;
14764 saved_qualifying_scope = parser->qualifying_scope;
14765 saved_object_scope = parser->object_scope;
14766 /* We must enter the scope of the class so that the names of
14767 entities declared within the class are available in the
14768 conversion-type-id. For example, consider:
14769
14770 struct S {
14771 typedef int I;
14772 operator I();
14773 };
14774
14775 S::operator I() { ... }
14776
14777 In order to see that `I' is a type-name in the definition, we
14778 must be in the scope of `S'. */
14779 if (saved_scope)
14780 pushed_scope = push_scope (saved_scope);
14781 /* Parse the conversion-type-id. */
14782 type = cp_parser_conversion_type_id (parser);
14783 /* Leave the scope of the class, if any. */
14784 if (pushed_scope)
14785 pop_scope (pushed_scope);
14786 /* Restore the saved scope. */
14787 parser->scope = saved_scope;
14788 parser->qualifying_scope = saved_qualifying_scope;
14789 parser->object_scope = saved_object_scope;
14790 /* If the TYPE is invalid, indicate failure. */
14791 if (type == error_mark_node)
14792 return error_mark_node;
14793 return make_conv_op_name (type);
14794 }
14795
14796 /* Parse a conversion-type-id:
14797
14798 conversion-type-id:
14799 type-specifier-seq conversion-declarator [opt]
14800
14801 Returns the TYPE specified. */
14802
14803 static tree
14804 cp_parser_conversion_type_id (cp_parser* parser)
14805 {
14806 tree attributes;
14807 cp_decl_specifier_seq type_specifiers;
14808 cp_declarator *declarator;
14809 tree type_specified;
14810 const char *saved_message;
14811
14812 /* Parse the attributes. */
14813 attributes = cp_parser_attributes_opt (parser);
14814
14815 saved_message = parser->type_definition_forbidden_message;
14816 parser->type_definition_forbidden_message
14817 = G_("types may not be defined in a conversion-type-id");
14818
14819 /* Parse the type-specifiers. */
14820 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
14821 /*is_declaration=*/false,
14822 /*is_trailing_return=*/false,
14823 &type_specifiers);
14824
14825 parser->type_definition_forbidden_message = saved_message;
14826
14827 /* If that didn't work, stop. */
14828 if (type_specifiers.type == error_mark_node)
14829 return error_mark_node;
14830 /* Parse the conversion-declarator. */
14831 declarator = cp_parser_conversion_declarator_opt (parser);
14832
14833 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14834 /*initialized=*/0, &attributes);
14835 if (attributes)
14836 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14837
14838 /* Don't give this error when parsing tentatively. This happens to
14839 work because we always parse this definitively once. */
14840 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14841 && type_uses_auto (type_specified))
14842 {
14843 if (cxx_dialect < cxx14)
14844 {
14845 error ("invalid use of %<auto%> in conversion operator");
14846 return error_mark_node;
14847 }
14848 else if (template_parm_scope_p ())
14849 warning (0, "use of %<auto%> in member template "
14850 "conversion operator can never be deduced");
14851 }
14852
14853 return type_specified;
14854 }
14855
14856 /* Parse an (optional) conversion-declarator.
14857
14858 conversion-declarator:
14859 ptr-operator conversion-declarator [opt]
14860
14861 */
14862
14863 static cp_declarator *
14864 cp_parser_conversion_declarator_opt (cp_parser* parser)
14865 {
14866 enum tree_code code;
14867 tree class_type, std_attributes = NULL_TREE;
14868 cp_cv_quals cv_quals;
14869
14870 /* We don't know if there's a ptr-operator next, or not. */
14871 cp_parser_parse_tentatively (parser);
14872 /* Try the ptr-operator. */
14873 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14874 &std_attributes);
14875 /* If it worked, look for more conversion-declarators. */
14876 if (cp_parser_parse_definitely (parser))
14877 {
14878 cp_declarator *declarator;
14879
14880 /* Parse another optional declarator. */
14881 declarator = cp_parser_conversion_declarator_opt (parser);
14882
14883 declarator = cp_parser_make_indirect_declarator
14884 (code, class_type, cv_quals, declarator, std_attributes);
14885
14886 return declarator;
14887 }
14888
14889 return NULL;
14890 }
14891
14892 /* Parse an (optional) ctor-initializer.
14893
14894 ctor-initializer:
14895 : mem-initializer-list */
14896
14897 static void
14898 cp_parser_ctor_initializer_opt (cp_parser* parser)
14899 {
14900 /* If the next token is not a `:', then there is no
14901 ctor-initializer. */
14902 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14903 {
14904 /* Do default initialization of any bases and members. */
14905 if (DECL_CONSTRUCTOR_P (current_function_decl))
14906 finish_mem_initializers (NULL_TREE);
14907 return;
14908 }
14909
14910 /* Consume the `:' token. */
14911 cp_lexer_consume_token (parser->lexer);
14912 /* And the mem-initializer-list. */
14913 cp_parser_mem_initializer_list (parser);
14914 }
14915
14916 /* Parse a mem-initializer-list.
14917
14918 mem-initializer-list:
14919 mem-initializer ... [opt]
14920 mem-initializer ... [opt] , mem-initializer-list */
14921
14922 static void
14923 cp_parser_mem_initializer_list (cp_parser* parser)
14924 {
14925 tree mem_initializer_list = NULL_TREE;
14926 tree target_ctor = error_mark_node;
14927 cp_token *token = cp_lexer_peek_token (parser->lexer);
14928
14929 /* Let the semantic analysis code know that we are starting the
14930 mem-initializer-list. */
14931 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14932 error_at (token->location,
14933 "only constructors take member initializers");
14934
14935 /* Loop through the list. */
14936 while (true)
14937 {
14938 tree mem_initializer;
14939
14940 token = cp_lexer_peek_token (parser->lexer);
14941 /* Parse the mem-initializer. */
14942 mem_initializer = cp_parser_mem_initializer (parser);
14943 /* If the next token is a `...', we're expanding member initializers. */
14944 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14945 if (ellipsis
14946 || (mem_initializer != error_mark_node
14947 && check_for_bare_parameter_packs (TREE_PURPOSE
14948 (mem_initializer))))
14949 {
14950 /* Consume the `...'. */
14951 if (ellipsis)
14952 cp_lexer_consume_token (parser->lexer);
14953
14954 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14955 can be expanded but members cannot. */
14956 if (mem_initializer != error_mark_node
14957 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14958 {
14959 error_at (token->location,
14960 "cannot expand initializer for member %qD",
14961 TREE_PURPOSE (mem_initializer));
14962 mem_initializer = error_mark_node;
14963 }
14964
14965 /* Construct the pack expansion type. */
14966 if (mem_initializer != error_mark_node)
14967 mem_initializer = make_pack_expansion (mem_initializer);
14968 }
14969 if (target_ctor != error_mark_node
14970 && mem_initializer != error_mark_node)
14971 {
14972 error ("mem-initializer for %qD follows constructor delegation",
14973 TREE_PURPOSE (mem_initializer));
14974 mem_initializer = error_mark_node;
14975 }
14976 /* Look for a target constructor. */
14977 if (mem_initializer != error_mark_node
14978 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14979 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14980 {
14981 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14982 if (mem_initializer_list)
14983 {
14984 error ("constructor delegation follows mem-initializer for %qD",
14985 TREE_PURPOSE (mem_initializer_list));
14986 mem_initializer = error_mark_node;
14987 }
14988 target_ctor = mem_initializer;
14989 }
14990 /* Add it to the list, unless it was erroneous. */
14991 if (mem_initializer != error_mark_node)
14992 {
14993 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14994 mem_initializer_list = mem_initializer;
14995 }
14996 /* If the next token is not a `,', we're done. */
14997 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14998 break;
14999 /* Consume the `,' token. */
15000 cp_lexer_consume_token (parser->lexer);
15001 }
15002
15003 /* Perform semantic analysis. */
15004 if (DECL_CONSTRUCTOR_P (current_function_decl))
15005 finish_mem_initializers (mem_initializer_list);
15006 }
15007
15008 /* Parse a mem-initializer.
15009
15010 mem-initializer:
15011 mem-initializer-id ( expression-list [opt] )
15012 mem-initializer-id braced-init-list
15013
15014 GNU extension:
15015
15016 mem-initializer:
15017 ( expression-list [opt] )
15018
15019 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
15020 class) or FIELD_DECL (for a non-static data member) to initialize;
15021 the TREE_VALUE is the expression-list. An empty initialization
15022 list is represented by void_list_node. */
15023
15024 static tree
15025 cp_parser_mem_initializer (cp_parser* parser)
15026 {
15027 tree mem_initializer_id;
15028 tree expression_list;
15029 tree member;
15030 cp_token *token = cp_lexer_peek_token (parser->lexer);
15031
15032 /* Find out what is being initialized. */
15033 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15034 {
15035 permerror (token->location,
15036 "anachronistic old-style base class initializer");
15037 mem_initializer_id = NULL_TREE;
15038 }
15039 else
15040 {
15041 mem_initializer_id = cp_parser_mem_initializer_id (parser);
15042 if (mem_initializer_id == error_mark_node)
15043 return mem_initializer_id;
15044 }
15045 member = expand_member_init (mem_initializer_id);
15046 if (member && !DECL_P (member))
15047 in_base_initializer = 1;
15048
15049 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15050 {
15051 bool expr_non_constant_p;
15052 cp_lexer_set_source_position (parser->lexer);
15053 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15054 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
15055 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
15056 expression_list = build_tree_list (NULL_TREE, expression_list);
15057 }
15058 else
15059 {
15060 vec<tree, va_gc> *vec;
15061 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15062 /*cast_p=*/false,
15063 /*allow_expansion_p=*/true,
15064 /*non_constant_p=*/NULL,
15065 /*close_paren_loc=*/NULL,
15066 /*wrap_locations_p=*/true);
15067 if (vec == NULL)
15068 return error_mark_node;
15069 expression_list = build_tree_list_vec (vec);
15070 release_tree_vector (vec);
15071 }
15072
15073 if (expression_list == error_mark_node)
15074 return error_mark_node;
15075 if (!expression_list)
15076 expression_list = void_type_node;
15077
15078 in_base_initializer = 0;
15079
15080 return member ? build_tree_list (member, expression_list) : error_mark_node;
15081 }
15082
15083 /* Parse a mem-initializer-id.
15084
15085 mem-initializer-id:
15086 :: [opt] nested-name-specifier [opt] class-name
15087 decltype-specifier (C++11)
15088 identifier
15089
15090 Returns a TYPE indicating the class to be initialized for the first
15091 production (and the second in C++11). Returns an IDENTIFIER_NODE
15092 indicating the data member to be initialized for the last production. */
15093
15094 static tree
15095 cp_parser_mem_initializer_id (cp_parser* parser)
15096 {
15097 bool global_scope_p;
15098 bool nested_name_specifier_p;
15099 bool template_p = false;
15100 tree id;
15101
15102 cp_token *token = cp_lexer_peek_token (parser->lexer);
15103
15104 /* `typename' is not allowed in this context ([temp.res]). */
15105 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15106 {
15107 error_at (token->location,
15108 "keyword %<typename%> not allowed in this context (a qualified "
15109 "member initializer is implicitly a type)");
15110 cp_lexer_consume_token (parser->lexer);
15111 }
15112 /* Look for the optional `::' operator. */
15113 global_scope_p
15114 = (cp_parser_global_scope_opt (parser,
15115 /*current_scope_valid_p=*/false)
15116 != NULL_TREE);
15117 /* Look for the optional nested-name-specifier. The simplest way to
15118 implement:
15119
15120 [temp.res]
15121
15122 The keyword `typename' is not permitted in a base-specifier or
15123 mem-initializer; in these contexts a qualified name that
15124 depends on a template-parameter is implicitly assumed to be a
15125 type name.
15126
15127 is to assume that we have seen the `typename' keyword at this
15128 point. */
15129 nested_name_specifier_p
15130 = (cp_parser_nested_name_specifier_opt (parser,
15131 /*typename_keyword_p=*/true,
15132 /*check_dependency_p=*/true,
15133 /*type_p=*/true,
15134 /*is_declaration=*/true)
15135 != NULL_TREE);
15136 if (nested_name_specifier_p)
15137 template_p = cp_parser_optional_template_keyword (parser);
15138 /* If there is a `::' operator or a nested-name-specifier, then we
15139 are definitely looking for a class-name. */
15140 if (global_scope_p || nested_name_specifier_p)
15141 return cp_parser_class_name (parser,
15142 /*typename_keyword_p=*/true,
15143 /*template_keyword_p=*/template_p,
15144 typename_type,
15145 /*check_dependency_p=*/true,
15146 /*class_head_p=*/false,
15147 /*is_declaration=*/true);
15148 /* Otherwise, we could also be looking for an ordinary identifier. */
15149 cp_parser_parse_tentatively (parser);
15150 if (cp_lexer_next_token_is_decltype (parser->lexer))
15151 /* Try a decltype-specifier. */
15152 id = cp_parser_decltype (parser);
15153 else
15154 /* Otherwise, try a class-name. */
15155 id = cp_parser_class_name (parser,
15156 /*typename_keyword_p=*/true,
15157 /*template_keyword_p=*/false,
15158 none_type,
15159 /*check_dependency_p=*/true,
15160 /*class_head_p=*/false,
15161 /*is_declaration=*/true);
15162 /* If we found one, we're done. */
15163 if (cp_parser_parse_definitely (parser))
15164 return id;
15165 /* Otherwise, look for an ordinary identifier. */
15166 return cp_parser_identifier (parser);
15167 }
15168
15169 /* Overloading [gram.over] */
15170
15171 /* Parse an operator-function-id.
15172
15173 operator-function-id:
15174 operator operator
15175
15176 Returns an IDENTIFIER_NODE for the operator which is a
15177 human-readable spelling of the identifier, e.g., `operator +'. */
15178
15179 static cp_expr
15180 cp_parser_operator_function_id (cp_parser* parser)
15181 {
15182 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
15183 /* Look for the `operator' keyword. */
15184 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15185 return error_mark_node;
15186 /* And then the name of the operator itself. */
15187 return cp_parser_operator (parser, start_loc);
15188 }
15189
15190 /* Return an identifier node for a user-defined literal operator.
15191 The suffix identifier is chained to the operator name identifier. */
15192
15193 tree
15194 cp_literal_operator_id (const char* name)
15195 {
15196 tree identifier;
15197 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
15198 + strlen (name) + 10);
15199 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
15200 identifier = get_identifier (buffer);
15201
15202 return identifier;
15203 }
15204
15205 /* Parse an operator.
15206
15207 operator:
15208 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15209 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15210 || ++ -- , ->* -> () []
15211
15212 GNU Extensions:
15213
15214 operator:
15215 <? >? <?= >?=
15216
15217 Returns an IDENTIFIER_NODE for the operator which is a
15218 human-readable spelling of the identifier, e.g., `operator +'. */
15219
15220 static cp_expr
15221 cp_parser_operator (cp_parser* parser, location_t start_loc)
15222 {
15223 tree id = NULL_TREE;
15224 cp_token *token;
15225 bool utf8 = false;
15226
15227 /* Peek at the next token. */
15228 token = cp_lexer_peek_token (parser->lexer);
15229
15230 location_t end_loc = token->location;
15231
15232 /* Figure out which operator we have. */
15233 enum tree_code op = ERROR_MARK;
15234 bool assop = false;
15235 bool consumed = false;
15236 switch (token->type)
15237 {
15238 case CPP_KEYWORD:
15239 {
15240 /* The keyword should be either `new' or `delete'. */
15241 if (token->keyword == RID_NEW)
15242 op = NEW_EXPR;
15243 else if (token->keyword == RID_DELETE)
15244 op = DELETE_EXPR;
15245 else
15246 break;
15247
15248 /* Consume the `new' or `delete' token. */
15249 end_loc = cp_lexer_consume_token (parser->lexer)->location;
15250
15251 /* Peek at the next token. */
15252 token = cp_lexer_peek_token (parser->lexer);
15253 /* If it's a `[' token then this is the array variant of the
15254 operator. */
15255 if (token->type == CPP_OPEN_SQUARE)
15256 {
15257 /* Consume the `[' token. */
15258 cp_lexer_consume_token (parser->lexer);
15259 /* Look for the `]' token. */
15260 if (cp_token *close_token
15261 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15262 end_loc = close_token->location;
15263 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
15264 }
15265 consumed = true;
15266 break;
15267 }
15268
15269 case CPP_PLUS:
15270 op = PLUS_EXPR;
15271 break;
15272
15273 case CPP_MINUS:
15274 op = MINUS_EXPR;
15275 break;
15276
15277 case CPP_MULT:
15278 op = MULT_EXPR;
15279 break;
15280
15281 case CPP_DIV:
15282 op = TRUNC_DIV_EXPR;
15283 break;
15284
15285 case CPP_MOD:
15286 op = TRUNC_MOD_EXPR;
15287 break;
15288
15289 case CPP_XOR:
15290 op = BIT_XOR_EXPR;
15291 break;
15292
15293 case CPP_AND:
15294 op = BIT_AND_EXPR;
15295 break;
15296
15297 case CPP_OR:
15298 op = BIT_IOR_EXPR;
15299 break;
15300
15301 case CPP_COMPL:
15302 op = BIT_NOT_EXPR;
15303 break;
15304
15305 case CPP_NOT:
15306 op = TRUTH_NOT_EXPR;
15307 break;
15308
15309 case CPP_EQ:
15310 assop = true;
15311 op = NOP_EXPR;
15312 break;
15313
15314 case CPP_LESS:
15315 op = LT_EXPR;
15316 break;
15317
15318 case CPP_GREATER:
15319 op = GT_EXPR;
15320 break;
15321
15322 case CPP_PLUS_EQ:
15323 assop = true;
15324 op = PLUS_EXPR;
15325 break;
15326
15327 case CPP_MINUS_EQ:
15328 assop = true;
15329 op = MINUS_EXPR;
15330 break;
15331
15332 case CPP_MULT_EQ:
15333 assop = true;
15334 op = MULT_EXPR;
15335 break;
15336
15337 case CPP_DIV_EQ:
15338 assop = true;
15339 op = TRUNC_DIV_EXPR;
15340 break;
15341
15342 case CPP_MOD_EQ:
15343 assop = true;
15344 op = TRUNC_MOD_EXPR;
15345 break;
15346
15347 case CPP_XOR_EQ:
15348 assop = true;
15349 op = BIT_XOR_EXPR;
15350 break;
15351
15352 case CPP_AND_EQ:
15353 assop = true;
15354 op = BIT_AND_EXPR;
15355 break;
15356
15357 case CPP_OR_EQ:
15358 assop = true;
15359 op = BIT_IOR_EXPR;
15360 break;
15361
15362 case CPP_LSHIFT:
15363 op = LSHIFT_EXPR;
15364 break;
15365
15366 case CPP_RSHIFT:
15367 op = RSHIFT_EXPR;
15368 break;
15369
15370 case CPP_LSHIFT_EQ:
15371 assop = true;
15372 op = LSHIFT_EXPR;
15373 break;
15374
15375 case CPP_RSHIFT_EQ:
15376 assop = true;
15377 op = RSHIFT_EXPR;
15378 break;
15379
15380 case CPP_EQ_EQ:
15381 op = EQ_EXPR;
15382 break;
15383
15384 case CPP_NOT_EQ:
15385 op = NE_EXPR;
15386 break;
15387
15388 case CPP_LESS_EQ:
15389 op = LE_EXPR;
15390 break;
15391
15392 case CPP_GREATER_EQ:
15393 op = GE_EXPR;
15394 break;
15395
15396 case CPP_AND_AND:
15397 op = TRUTH_ANDIF_EXPR;
15398 break;
15399
15400 case CPP_OR_OR:
15401 op = TRUTH_ORIF_EXPR;
15402 break;
15403
15404 case CPP_PLUS_PLUS:
15405 op = POSTINCREMENT_EXPR;
15406 break;
15407
15408 case CPP_MINUS_MINUS:
15409 op = PREDECREMENT_EXPR;
15410 break;
15411
15412 case CPP_COMMA:
15413 op = COMPOUND_EXPR;
15414 break;
15415
15416 case CPP_DEREF_STAR:
15417 op = MEMBER_REF;
15418 break;
15419
15420 case CPP_DEREF:
15421 op = COMPONENT_REF;
15422 break;
15423
15424 case CPP_OPEN_PAREN:
15425 {
15426 /* Consume the `('. */
15427 matching_parens parens;
15428 parens.consume_open (parser);
15429 /* Look for the matching `)'. */
15430 token = parens.require_close (parser);
15431 if (token)
15432 end_loc = token->location;
15433 op = CALL_EXPR;
15434 consumed = true;
15435 break;
15436 }
15437
15438 case CPP_OPEN_SQUARE:
15439 /* Consume the `['. */
15440 cp_lexer_consume_token (parser->lexer);
15441 /* Look for the matching `]'. */
15442 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15443 if (token)
15444 end_loc = token->location;
15445 op = ARRAY_REF;
15446 consumed = true;
15447 break;
15448
15449 case CPP_UTF8STRING:
15450 case CPP_UTF8STRING_USERDEF:
15451 utf8 = true;
15452 /* FALLTHRU */
15453 case CPP_STRING:
15454 case CPP_WSTRING:
15455 case CPP_STRING16:
15456 case CPP_STRING32:
15457 case CPP_STRING_USERDEF:
15458 case CPP_WSTRING_USERDEF:
15459 case CPP_STRING16_USERDEF:
15460 case CPP_STRING32_USERDEF:
15461 {
15462 cp_expr str;
15463 tree string_tree;
15464 int sz, len;
15465
15466 if (cxx_dialect == cxx98)
15467 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15468
15469 /* Consume the string. */
15470 str = cp_parser_string_literal (parser, /*translate=*/true,
15471 /*wide_ok=*/true, /*lookup_udlit=*/false);
15472 if (str == error_mark_node)
15473 return error_mark_node;
15474 else if (TREE_CODE (str) == USERDEF_LITERAL)
15475 {
15476 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
15477 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
15478 end_loc = str.get_location ();
15479 }
15480 else
15481 {
15482 string_tree = str;
15483 /* Look for the suffix identifier. */
15484 token = cp_lexer_peek_token (parser->lexer);
15485 if (token->type == CPP_NAME)
15486 {
15487 id = cp_parser_identifier (parser);
15488 end_loc = token->location;
15489 }
15490 else if (token->type == CPP_KEYWORD)
15491 {
15492 error ("unexpected keyword;"
15493 " remove space between quotes and suffix identifier");
15494 return error_mark_node;
15495 }
15496 else
15497 {
15498 error ("expected suffix identifier");
15499 return error_mark_node;
15500 }
15501 }
15502 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15503 (TREE_TYPE (TREE_TYPE (string_tree))));
15504 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15505 if (len != 0)
15506 {
15507 error ("expected empty string after %<operator%> keyword");
15508 return error_mark_node;
15509 }
15510 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15511 != char_type_node)
15512 {
15513 error ("invalid encoding prefix in literal operator");
15514 return error_mark_node;
15515 }
15516 if (id != error_mark_node)
15517 {
15518 const char *name = IDENTIFIER_POINTER (id);
15519 id = cp_literal_operator_id (name);
15520 }
15521 /* Generate a location of the form:
15522 "" _suffix_identifier
15523 ^~~~~~~~~~~~~~~~~~~~~
15524 with caret == start at the start token, finish at the end of the
15525 suffix identifier. */
15526 location_t finish_loc
15527 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15528 location_t combined_loc
15529 = make_location (start_loc, start_loc, finish_loc);
15530 return cp_expr (id, combined_loc);
15531 }
15532
15533 default:
15534 /* Anything else is an error. */
15535 break;
15536 }
15537
15538 /* If we have selected an identifier, we need to consume the
15539 operator token. */
15540 if (op != ERROR_MARK)
15541 {
15542 id = ovl_op_identifier (assop, op);
15543 if (!consumed)
15544 cp_lexer_consume_token (parser->lexer);
15545 }
15546 /* Otherwise, no valid operator name was present. */
15547 else
15548 {
15549 cp_parser_error (parser, "expected operator");
15550 id = error_mark_node;
15551 }
15552
15553 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15554 return cp_expr (id, start_loc);
15555 }
15556
15557 /* Parse a template-declaration.
15558
15559 template-declaration:
15560 export [opt] template < template-parameter-list > declaration
15561
15562 If MEMBER_P is TRUE, this template-declaration occurs within a
15563 class-specifier.
15564
15565 The grammar rule given by the standard isn't correct. What
15566 is really meant is:
15567
15568 template-declaration:
15569 export [opt] template-parameter-list-seq
15570 decl-specifier-seq [opt] init-declarator [opt] ;
15571 export [opt] template-parameter-list-seq
15572 function-definition
15573
15574 template-parameter-list-seq:
15575 template-parameter-list-seq [opt]
15576 template < template-parameter-list >
15577
15578 Concept Extensions:
15579
15580 template-parameter-list-seq:
15581 template < template-parameter-list > requires-clause [opt]
15582
15583 requires-clause:
15584 requires logical-or-expression */
15585
15586 static void
15587 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15588 {
15589 /* Check for `export'. */
15590 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15591 {
15592 /* Consume the `export' token. */
15593 cp_lexer_consume_token (parser->lexer);
15594 /* Warn that we do not support `export'. */
15595 warning (0, "keyword %<export%> not implemented, and will be ignored");
15596 }
15597
15598 cp_parser_template_declaration_after_export (parser, member_p);
15599 }
15600
15601 /* Parse a template-parameter-list.
15602
15603 template-parameter-list:
15604 template-parameter
15605 template-parameter-list , template-parameter
15606
15607 Returns a TREE_LIST. Each node represents a template parameter.
15608 The nodes are connected via their TREE_CHAINs. */
15609
15610 static tree
15611 cp_parser_template_parameter_list (cp_parser* parser)
15612 {
15613 tree parameter_list = NULL_TREE;
15614
15615 /* Don't create wrapper nodes within a template-parameter-list,
15616 since we don't want to have different types based on the
15617 spelling location of constants and decls within them. */
15618 auto_suppress_location_wrappers sentinel;
15619
15620 begin_template_parm_list ();
15621
15622 /* The loop below parses the template parms. We first need to know
15623 the total number of template parms to be able to compute proper
15624 canonical types of each dependent type. So after the loop, when
15625 we know the total number of template parms,
15626 end_template_parm_list computes the proper canonical types and
15627 fixes up the dependent types accordingly. */
15628 while (true)
15629 {
15630 tree parameter;
15631 bool is_non_type;
15632 bool is_parameter_pack;
15633 location_t parm_loc;
15634
15635 /* Parse the template-parameter. */
15636 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15637 parameter = cp_parser_template_parameter (parser,
15638 &is_non_type,
15639 &is_parameter_pack);
15640 /* Add it to the list. */
15641 if (parameter != error_mark_node)
15642 parameter_list = process_template_parm (parameter_list,
15643 parm_loc,
15644 parameter,
15645 is_non_type,
15646 is_parameter_pack);
15647 else
15648 {
15649 tree err_parm = build_tree_list (parameter, parameter);
15650 parameter_list = chainon (parameter_list, err_parm);
15651 }
15652
15653 /* If the next token is not a `,', we're done. */
15654 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15655 break;
15656 /* Otherwise, consume the `,' token. */
15657 cp_lexer_consume_token (parser->lexer);
15658 }
15659
15660 return end_template_parm_list (parameter_list);
15661 }
15662
15663 /* Parse a introduction-list.
15664
15665 introduction-list:
15666 introduced-parameter
15667 introduction-list , introduced-parameter
15668
15669 introduced-parameter:
15670 ...[opt] identifier
15671
15672 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15673 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15674 WILDCARD_DECL will also have DECL_NAME set and token location in
15675 DECL_SOURCE_LOCATION. */
15676
15677 static tree
15678 cp_parser_introduction_list (cp_parser *parser)
15679 {
15680 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15681
15682 while (true)
15683 {
15684 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15685 if (is_pack)
15686 cp_lexer_consume_token (parser->lexer);
15687
15688 tree identifier = cp_parser_identifier (parser);
15689 if (identifier == error_mark_node)
15690 break;
15691
15692 /* Build placeholder. */
15693 tree parm = build_nt (WILDCARD_DECL);
15694 DECL_SOURCE_LOCATION (parm)
15695 = cp_lexer_peek_token (parser->lexer)->location;
15696 DECL_NAME (parm) = identifier;
15697 WILDCARD_PACK_P (parm) = is_pack;
15698 vec_safe_push (introduction_vec, parm);
15699
15700 /* If the next token is not a `,', we're done. */
15701 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15702 break;
15703 /* Otherwise, consume the `,' token. */
15704 cp_lexer_consume_token (parser->lexer);
15705 }
15706
15707 /* Convert the vec into a TREE_VEC. */
15708 tree introduction_list = make_tree_vec (introduction_vec->length ());
15709 unsigned int n;
15710 tree parm;
15711 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15712 TREE_VEC_ELT (introduction_list, n) = parm;
15713
15714 release_tree_vector (introduction_vec);
15715 return introduction_list;
15716 }
15717
15718 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15719 is an abstract declarator. */
15720
15721 static inline cp_declarator*
15722 get_id_declarator (cp_declarator *declarator)
15723 {
15724 cp_declarator *d = declarator;
15725 while (d && d->kind != cdk_id)
15726 d = d->declarator;
15727 return d;
15728 }
15729
15730 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15731 is an abstract declarator. */
15732
15733 static inline tree
15734 get_unqualified_id (cp_declarator *declarator)
15735 {
15736 declarator = get_id_declarator (declarator);
15737 if (declarator)
15738 return declarator->u.id.unqualified_name;
15739 else
15740 return NULL_TREE;
15741 }
15742
15743 /* Returns true if DECL represents a constrained-parameter. */
15744
15745 static inline bool
15746 is_constrained_parameter (tree decl)
15747 {
15748 return (decl
15749 && TREE_CODE (decl) == TYPE_DECL
15750 && CONSTRAINED_PARM_CONCEPT (decl)
15751 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15752 }
15753
15754 /* Returns true if PARM declares a constrained-parameter. */
15755
15756 static inline bool
15757 is_constrained_parameter (cp_parameter_declarator *parm)
15758 {
15759 return is_constrained_parameter (parm->decl_specifiers.type);
15760 }
15761
15762 /* Check that the type parameter is only a declarator-id, and that its
15763 type is not cv-qualified. */
15764
15765 bool
15766 cp_parser_check_constrained_type_parm (cp_parser *parser,
15767 cp_parameter_declarator *parm)
15768 {
15769 if (!parm->declarator)
15770 return true;
15771
15772 if (parm->declarator->kind != cdk_id)
15773 {
15774 cp_parser_error (parser, "invalid constrained type parameter");
15775 return false;
15776 }
15777
15778 /* Don't allow cv-qualified type parameters. */
15779 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15780 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15781 {
15782 cp_parser_error (parser, "cv-qualified type parameter");
15783 return false;
15784 }
15785
15786 return true;
15787 }
15788
15789 /* Finish parsing/processing a template type parameter and checking
15790 various restrictions. */
15791
15792 static inline tree
15793 cp_parser_constrained_type_template_parm (cp_parser *parser,
15794 tree id,
15795 cp_parameter_declarator* parmdecl)
15796 {
15797 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15798 return finish_template_type_parm (class_type_node, id);
15799 else
15800 return error_mark_node;
15801 }
15802
15803 static tree
15804 finish_constrained_template_template_parm (tree proto, tree id)
15805 {
15806 /* FIXME: This should probably be copied, and we may need to adjust
15807 the template parameter depths. */
15808 tree saved_parms = current_template_parms;
15809 begin_template_parm_list ();
15810 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15811 end_template_parm_list ();
15812
15813 tree parm = finish_template_template_parm (class_type_node, id);
15814 current_template_parms = saved_parms;
15815
15816 return parm;
15817 }
15818
15819 /* Finish parsing/processing a template template parameter by borrowing
15820 the template parameter list from the prototype parameter. */
15821
15822 static tree
15823 cp_parser_constrained_template_template_parm (cp_parser *parser,
15824 tree proto,
15825 tree id,
15826 cp_parameter_declarator *parmdecl)
15827 {
15828 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15829 return error_mark_node;
15830 return finish_constrained_template_template_parm (proto, id);
15831 }
15832
15833 /* Create a new non-type template parameter from the given PARM
15834 declarator. */
15835
15836 static tree
15837 constrained_non_type_template_parm (bool *is_non_type,
15838 cp_parameter_declarator *parm)
15839 {
15840 *is_non_type = true;
15841 cp_declarator *decl = parm->declarator;
15842 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15843 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15844 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15845 }
15846
15847 /* Build a constrained template parameter based on the PARMDECL
15848 declarator. The type of PARMDECL is the constrained type, which
15849 refers to the prototype template parameter that ultimately
15850 specifies the type of the declared parameter. */
15851
15852 static tree
15853 finish_constrained_parameter (cp_parser *parser,
15854 cp_parameter_declarator *parmdecl,
15855 bool *is_non_type,
15856 bool *is_parameter_pack)
15857 {
15858 tree decl = parmdecl->decl_specifiers.type;
15859 tree id = get_unqualified_id (parmdecl->declarator);
15860 tree def = parmdecl->default_argument;
15861 tree proto = DECL_INITIAL (decl);
15862
15863 /* A template parameter constrained by a variadic concept shall also
15864 be declared as a template parameter pack. */
15865 bool is_variadic = template_parameter_pack_p (proto);
15866 if (is_variadic && !*is_parameter_pack)
15867 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15868
15869 /* Build the parameter. Return an error if the declarator was invalid. */
15870 tree parm;
15871 if (TREE_CODE (proto) == TYPE_DECL)
15872 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15873 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15874 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15875 parmdecl);
15876 else
15877 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15878 if (parm == error_mark_node)
15879 return error_mark_node;
15880
15881 /* Finish the parameter decl and create a node attaching the
15882 default argument and constraint. */
15883 parm = build_tree_list (def, parm);
15884 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15885
15886 return parm;
15887 }
15888
15889 /* Returns true if the parsed type actually represents the declaration
15890 of a type template-parameter. */
15891
15892 static inline bool
15893 declares_constrained_type_template_parameter (tree type)
15894 {
15895 return (is_constrained_parameter (type)
15896 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15897 }
15898
15899
15900 /* Returns true if the parsed type actually represents the declaration of
15901 a template template-parameter. */
15902
15903 static bool
15904 declares_constrained_template_template_parameter (tree type)
15905 {
15906 return (is_constrained_parameter (type)
15907 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15908 }
15909
15910 /* Parse a default argument for a type template-parameter.
15911 Note that diagnostics are handled in cp_parser_template_parameter. */
15912
15913 static tree
15914 cp_parser_default_type_template_argument (cp_parser *parser)
15915 {
15916 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15917
15918 /* Consume the `=' token. */
15919 cp_lexer_consume_token (parser->lexer);
15920
15921 cp_token *token = cp_lexer_peek_token (parser->lexer);
15922
15923 /* Parse the default-argument. */
15924 push_deferring_access_checks (dk_no_deferred);
15925 tree default_argument = cp_parser_type_id (parser,
15926 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15927 NULL);
15928 pop_deferring_access_checks ();
15929
15930 if (flag_concepts && type_uses_auto (default_argument))
15931 {
15932 error_at (token->location,
15933 "invalid use of %<auto%> in default template argument");
15934 return error_mark_node;
15935 }
15936
15937 return default_argument;
15938 }
15939
15940 /* Parse a default argument for a template template-parameter. */
15941
15942 static tree
15943 cp_parser_default_template_template_argument (cp_parser *parser)
15944 {
15945 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15946
15947 bool is_template;
15948
15949 /* Consume the `='. */
15950 cp_lexer_consume_token (parser->lexer);
15951 /* Parse the id-expression. */
15952 push_deferring_access_checks (dk_no_deferred);
15953 /* save token before parsing the id-expression, for error
15954 reporting */
15955 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15956 tree default_argument
15957 = cp_parser_id_expression (parser,
15958 /*template_keyword_p=*/false,
15959 /*check_dependency_p=*/true,
15960 /*template_p=*/&is_template,
15961 /*declarator_p=*/false,
15962 /*optional_p=*/false);
15963 if (TREE_CODE (default_argument) == TYPE_DECL)
15964 /* If the id-expression was a template-id that refers to
15965 a template-class, we already have the declaration here,
15966 so no further lookup is needed. */
15967 ;
15968 else
15969 /* Look up the name. */
15970 default_argument
15971 = cp_parser_lookup_name (parser, default_argument,
15972 none_type,
15973 /*is_template=*/is_template,
15974 /*is_namespace=*/false,
15975 /*check_dependency=*/true,
15976 /*ambiguous_decls=*/NULL,
15977 token->location);
15978 /* See if the default argument is valid. */
15979 default_argument = check_template_template_default_arg (default_argument);
15980 pop_deferring_access_checks ();
15981 return default_argument;
15982 }
15983
15984 /* Parse a template-parameter.
15985
15986 template-parameter:
15987 type-parameter
15988 parameter-declaration
15989
15990 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15991 the parameter. The TREE_PURPOSE is the default value, if any.
15992 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15993 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15994 set to true iff this parameter is a parameter pack. */
15995
15996 static tree
15997 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15998 bool *is_parameter_pack)
15999 {
16000 cp_token *token;
16001 cp_parameter_declarator *parameter_declarator;
16002 tree parm;
16003
16004 /* Assume it is a type parameter or a template parameter. */
16005 *is_non_type = false;
16006 /* Assume it not a parameter pack. */
16007 *is_parameter_pack = false;
16008 /* Peek at the next token. */
16009 token = cp_lexer_peek_token (parser->lexer);
16010 /* If it is `template', we have a type-parameter. */
16011 if (token->keyword == RID_TEMPLATE)
16012 return cp_parser_type_parameter (parser, is_parameter_pack);
16013 /* If it is `class' or `typename' we do not know yet whether it is a
16014 type parameter or a non-type parameter. Consider:
16015
16016 template <typename T, typename T::X X> ...
16017
16018 or:
16019
16020 template <class C, class D*> ...
16021
16022 Here, the first parameter is a type parameter, and the second is
16023 a non-type parameter. We can tell by looking at the token after
16024 the identifier -- if it is a `,', `=', or `>' then we have a type
16025 parameter. */
16026 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
16027 {
16028 /* Peek at the token after `class' or `typename'. */
16029 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16030 /* If it's an ellipsis, we have a template type parameter
16031 pack. */
16032 if (token->type == CPP_ELLIPSIS)
16033 return cp_parser_type_parameter (parser, is_parameter_pack);
16034 /* If it's an identifier, skip it. */
16035 if (token->type == CPP_NAME)
16036 token = cp_lexer_peek_nth_token (parser->lexer, 3);
16037 /* Now, see if the token looks like the end of a template
16038 parameter. */
16039 if (token->type == CPP_COMMA
16040 || token->type == CPP_EQ
16041 || token->type == CPP_GREATER)
16042 return cp_parser_type_parameter (parser, is_parameter_pack);
16043 }
16044
16045 /* Otherwise, it is a non-type parameter or a constrained parameter.
16046
16047 [temp.param]
16048
16049 When parsing a default template-argument for a non-type
16050 template-parameter, the first non-nested `>' is taken as the end
16051 of the template parameter-list rather than a greater-than
16052 operator. */
16053 parameter_declarator
16054 = cp_parser_parameter_declaration (parser,
16055 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16056 /*template_parm_p=*/true,
16057 /*parenthesized_p=*/NULL);
16058
16059 if (!parameter_declarator)
16060 return error_mark_node;
16061
16062 /* If the parameter declaration is marked as a parameter pack, set
16063 *IS_PARAMETER_PACK to notify the caller. */
16064 if (parameter_declarator->template_parameter_pack_p)
16065 *is_parameter_pack = true;
16066
16067 if (parameter_declarator->default_argument)
16068 {
16069 /* Can happen in some cases of erroneous input (c++/34892). */
16070 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16071 /* Consume the `...' for better error recovery. */
16072 cp_lexer_consume_token (parser->lexer);
16073 }
16074
16075 // The parameter may have been constrained.
16076 if (is_constrained_parameter (parameter_declarator))
16077 return finish_constrained_parameter (parser,
16078 parameter_declarator,
16079 is_non_type,
16080 is_parameter_pack);
16081
16082 // Now we're sure that the parameter is a non-type parameter.
16083 *is_non_type = true;
16084
16085 parm = grokdeclarator (parameter_declarator->declarator,
16086 &parameter_declarator->decl_specifiers,
16087 TPARM, /*initialized=*/0,
16088 /*attrlist=*/NULL);
16089 if (parm == error_mark_node)
16090 return error_mark_node;
16091
16092 return build_tree_list (parameter_declarator->default_argument, parm);
16093 }
16094
16095 /* Parse a type-parameter.
16096
16097 type-parameter:
16098 class identifier [opt]
16099 class identifier [opt] = type-id
16100 typename identifier [opt]
16101 typename identifier [opt] = type-id
16102 template < template-parameter-list > class identifier [opt]
16103 template < template-parameter-list > class identifier [opt]
16104 = id-expression
16105
16106 GNU Extension (variadic templates):
16107
16108 type-parameter:
16109 class ... identifier [opt]
16110 typename ... identifier [opt]
16111
16112 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16113 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16114 the declaration of the parameter.
16115
16116 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16117
16118 static tree
16119 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
16120 {
16121 cp_token *token;
16122 tree parameter;
16123
16124 /* Look for a keyword to tell us what kind of parameter this is. */
16125 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
16126 if (!token)
16127 return error_mark_node;
16128
16129 switch (token->keyword)
16130 {
16131 case RID_CLASS:
16132 case RID_TYPENAME:
16133 {
16134 tree identifier;
16135 tree default_argument;
16136
16137 /* If the next token is an ellipsis, we have a template
16138 argument pack. */
16139 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16140 {
16141 /* Consume the `...' token. */
16142 cp_lexer_consume_token (parser->lexer);
16143 maybe_warn_variadic_templates ();
16144
16145 *is_parameter_pack = true;
16146 }
16147
16148 /* If the next token is an identifier, then it names the
16149 parameter. */
16150 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16151 identifier = cp_parser_identifier (parser);
16152 else
16153 identifier = NULL_TREE;
16154
16155 /* Create the parameter. */
16156 parameter = finish_template_type_parm (class_type_node, identifier);
16157
16158 /* If the next token is an `=', we have a default argument. */
16159 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16160 {
16161 default_argument
16162 = cp_parser_default_type_template_argument (parser);
16163
16164 /* Template parameter packs cannot have default
16165 arguments. */
16166 if (*is_parameter_pack)
16167 {
16168 if (identifier)
16169 error_at (token->location,
16170 "template parameter pack %qD cannot have a "
16171 "default argument", identifier);
16172 else
16173 error_at (token->location,
16174 "template parameter packs cannot have "
16175 "default arguments");
16176 default_argument = NULL_TREE;
16177 }
16178 else if (check_for_bare_parameter_packs (default_argument))
16179 default_argument = error_mark_node;
16180 }
16181 else
16182 default_argument = NULL_TREE;
16183
16184 /* Create the combined representation of the parameter and the
16185 default argument. */
16186 parameter = build_tree_list (default_argument, parameter);
16187 }
16188 break;
16189
16190 case RID_TEMPLATE:
16191 {
16192 tree identifier;
16193 tree default_argument;
16194
16195 /* Look for the `<'. */
16196 cp_parser_require (parser, CPP_LESS, RT_LESS);
16197 /* Parse the template-parameter-list. */
16198 cp_parser_template_parameter_list (parser);
16199 /* Look for the `>'. */
16200 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16201
16202 // If template requirements are present, parse them.
16203 if (flag_concepts)
16204 {
16205 tree reqs = get_shorthand_constraints (current_template_parms);
16206 if (tree r = cp_parser_requires_clause_opt (parser))
16207 reqs = conjoin_constraints (reqs, normalize_expression (r));
16208 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
16209 }
16210
16211 /* Look for the `class' or 'typename' keywords. */
16212 cp_parser_type_parameter_key (parser);
16213 /* If the next token is an ellipsis, we have a template
16214 argument pack. */
16215 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16216 {
16217 /* Consume the `...' token. */
16218 cp_lexer_consume_token (parser->lexer);
16219 maybe_warn_variadic_templates ();
16220
16221 *is_parameter_pack = true;
16222 }
16223 /* If the next token is an `=', then there is a
16224 default-argument. If the next token is a `>', we are at
16225 the end of the parameter-list. If the next token is a `,',
16226 then we are at the end of this parameter. */
16227 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16228 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
16229 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16230 {
16231 identifier = cp_parser_identifier (parser);
16232 /* Treat invalid names as if the parameter were nameless. */
16233 if (identifier == error_mark_node)
16234 identifier = NULL_TREE;
16235 }
16236 else
16237 identifier = NULL_TREE;
16238
16239 /* Create the template parameter. */
16240 parameter = finish_template_template_parm (class_type_node,
16241 identifier);
16242
16243 /* If the next token is an `=', then there is a
16244 default-argument. */
16245 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16246 {
16247 default_argument
16248 = cp_parser_default_template_template_argument (parser);
16249
16250 /* Template parameter packs cannot have default
16251 arguments. */
16252 if (*is_parameter_pack)
16253 {
16254 if (identifier)
16255 error_at (token->location,
16256 "template parameter pack %qD cannot "
16257 "have a default argument",
16258 identifier);
16259 else
16260 error_at (token->location, "template parameter packs cannot "
16261 "have default arguments");
16262 default_argument = NULL_TREE;
16263 }
16264 }
16265 else
16266 default_argument = NULL_TREE;
16267
16268 /* Create the combined representation of the parameter and the
16269 default argument. */
16270 parameter = build_tree_list (default_argument, parameter);
16271 }
16272 break;
16273
16274 default:
16275 gcc_unreachable ();
16276 break;
16277 }
16278
16279 return parameter;
16280 }
16281
16282 /* Parse a template-id.
16283
16284 template-id:
16285 template-name < template-argument-list [opt] >
16286
16287 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16288 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16289 returned. Otherwise, if the template-name names a function, or set
16290 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16291 names a class, returns a TYPE_DECL for the specialization.
16292
16293 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16294 uninstantiated templates. */
16295
16296 static tree
16297 cp_parser_template_id (cp_parser *parser,
16298 bool template_keyword_p,
16299 bool check_dependency_p,
16300 enum tag_types tag_type,
16301 bool is_declaration)
16302 {
16303 tree templ;
16304 tree arguments;
16305 tree template_id;
16306 cp_token_position start_of_id = 0;
16307 cp_token *next_token = NULL, *next_token_2 = NULL;
16308 bool is_identifier;
16309
16310 /* If the next token corresponds to a template-id, there is no need
16311 to reparse it. */
16312 cp_token *token = cp_lexer_peek_token (parser->lexer);
16313 if (token->type == CPP_TEMPLATE_ID)
16314 {
16315 cp_lexer_consume_token (parser->lexer);
16316 return saved_checks_value (token->u.tree_check_value);
16317 }
16318
16319 /* Avoid performing name lookup if there is no possibility of
16320 finding a template-id. */
16321 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16322 || (token->type == CPP_NAME
16323 && !cp_parser_nth_token_starts_template_argument_list_p
16324 (parser, 2)))
16325 {
16326 cp_parser_error (parser, "expected template-id");
16327 return error_mark_node;
16328 }
16329
16330 /* Remember where the template-id starts. */
16331 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16332 start_of_id = cp_lexer_token_position (parser->lexer, false);
16333
16334 push_deferring_access_checks (dk_deferred);
16335
16336 /* Parse the template-name. */
16337 is_identifier = false;
16338 templ = cp_parser_template_name (parser, template_keyword_p,
16339 check_dependency_p,
16340 is_declaration,
16341 tag_type,
16342 &is_identifier);
16343
16344 /* Push any access checks inside the firewall we're about to create. */
16345 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
16346 pop_deferring_access_checks ();
16347 if (templ == error_mark_node || is_identifier)
16348 return templ;
16349
16350 /* Since we're going to preserve any side-effects from this parse, set up a
16351 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16352 in the template arguments. */
16353 tentative_firewall firewall (parser);
16354 reopen_deferring_access_checks (checks);
16355
16356 /* If we find the sequence `[:' after a template-name, it's probably
16357 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16358 parse correctly the argument list. */
16359 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16360 == CPP_OPEN_SQUARE)
16361 && next_token->flags & DIGRAPH
16362 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16363 == CPP_COLON)
16364 && !(next_token_2->flags & PREV_WHITE))
16365 {
16366 cp_parser_parse_tentatively (parser);
16367 /* Change `:' into `::'. */
16368 next_token_2->type = CPP_SCOPE;
16369 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16370 CPP_LESS. */
16371 cp_lexer_consume_token (parser->lexer);
16372
16373 /* Parse the arguments. */
16374 arguments = cp_parser_enclosed_template_argument_list (parser);
16375 if (!cp_parser_parse_definitely (parser))
16376 {
16377 /* If we couldn't parse an argument list, then we revert our changes
16378 and return simply an error. Maybe this is not a template-id
16379 after all. */
16380 next_token_2->type = CPP_COLON;
16381 cp_parser_error (parser, "expected %<<%>");
16382 pop_deferring_access_checks ();
16383 return error_mark_node;
16384 }
16385 /* Otherwise, emit an error about the invalid digraph, but continue
16386 parsing because we got our argument list. */
16387 if (permerror (next_token->location,
16388 "%<<::%> cannot begin a template-argument list"))
16389 {
16390 static bool hint = false;
16391 inform (next_token->location,
16392 "%<<:%> is an alternate spelling for %<[%>."
16393 " Insert whitespace between %<<%> and %<::%>");
16394 if (!hint && !flag_permissive)
16395 {
16396 inform (next_token->location, "(if you use %<-fpermissive%> "
16397 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16398 "accept your code)");
16399 hint = true;
16400 }
16401 }
16402 }
16403 else
16404 {
16405 /* Look for the `<' that starts the template-argument-list. */
16406 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16407 {
16408 pop_deferring_access_checks ();
16409 return error_mark_node;
16410 }
16411 /* Parse the arguments. */
16412 arguments = cp_parser_enclosed_template_argument_list (parser);
16413
16414 if ((cxx_dialect > cxx17)
16415 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16416 && !template_keyword_p
16417 && (cp_parser_error_occurred (parser)
16418 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16419 {
16420 /* This didn't go well. */
16421 if (TREE_CODE (templ) == FUNCTION_DECL)
16422 {
16423 /* C++2A says that "function-name < a;" is now ill-formed. */
16424 if (cp_parser_error_occurred (parser))
16425 {
16426 error_at (token->location, "invalid template-argument-list");
16427 inform (token->location, "function name as the left hand "
16428 "operand of %<<%> is ill-formed in C++2a; wrap the "
16429 "function name in %<()%>");
16430 }
16431 else
16432 /* We expect "f<targs>" to be followed by "(args)". */
16433 error_at (cp_lexer_peek_token (parser->lexer)->location,
16434 "expected %<(%> after template-argument-list");
16435 if (start_of_id)
16436 /* Purge all subsequent tokens. */
16437 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16438 }
16439 else
16440 cp_parser_simulate_error (parser);
16441 pop_deferring_access_checks ();
16442 return error_mark_node;
16443 }
16444 }
16445
16446 /* Set the location to be of the form:
16447 template-name < template-argument-list [opt] >
16448 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16449 with caret == start at the start of the template-name,
16450 ranging until the closing '>'. */
16451 location_t finish_loc
16452 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
16453 location_t combined_loc
16454 = make_location (token->location, token->location, finish_loc);
16455
16456 /* Check for concepts autos where they don't belong. We could
16457 identify types in some cases of idnetifier TEMPL, looking ahead
16458 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16459 types. We reject them in functions, but if what we have is an
16460 identifier, even with none_type we can't conclude it's NOT a
16461 type, we have to wait for template substitution. */
16462 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16463 template_id = error_mark_node;
16464 /* Build a representation of the specialization. */
16465 else if (identifier_p (templ))
16466 template_id = build_min_nt_loc (combined_loc,
16467 TEMPLATE_ID_EXPR,
16468 templ, arguments);
16469 else if (DECL_TYPE_TEMPLATE_P (templ)
16470 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16471 {
16472 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16473 template (rather than some instantiation thereof) only if
16474 is not nested within some other construct. For example, in
16475 "template <typename T> void f(T) { A<T>::", A<T> is just an
16476 instantiation of A. */
16477 bool entering_scope
16478 = (template_parm_scope_p ()
16479 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16480 template_id
16481 = finish_template_type (templ, arguments, entering_scope);
16482 }
16483 /* A template-like identifier may be a partial concept id. */
16484 else if (flag_concepts
16485 && (template_id = (cp_parser_maybe_partial_concept_id
16486 (parser, templ, arguments))))
16487 return template_id;
16488 else if (variable_template_p (templ))
16489 {
16490 template_id = lookup_template_variable (templ, arguments);
16491 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16492 SET_EXPR_LOCATION (template_id, combined_loc);
16493 }
16494 else
16495 {
16496 /* If it's not a class-template or a template-template, it should be
16497 a function-template. */
16498 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16499 || TREE_CODE (templ) == OVERLOAD
16500 || TREE_CODE (templ) == FUNCTION_DECL
16501 || BASELINK_P (templ)));
16502
16503 template_id = lookup_template_function (templ, arguments);
16504 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16505 SET_EXPR_LOCATION (template_id, combined_loc);
16506 }
16507
16508 /* If parsing tentatively, replace the sequence of tokens that makes
16509 up the template-id with a CPP_TEMPLATE_ID token. That way,
16510 should we re-parse the token stream, we will not have to repeat
16511 the effort required to do the parse, nor will we issue duplicate
16512 error messages about problems during instantiation of the
16513 template. */
16514 if (start_of_id
16515 /* Don't do this if we had a parse error in a declarator; re-parsing
16516 might succeed if a name changes meaning (60361). */
16517 && !(cp_parser_error_occurred (parser)
16518 && cp_parser_parsing_tentatively (parser)
16519 && parser->in_declarator_p))
16520 {
16521 /* Reset the contents of the START_OF_ID token. */
16522 token->type = CPP_TEMPLATE_ID;
16523 token->location = combined_loc;
16524
16525 /* Retrieve any deferred checks. Do not pop this access checks yet
16526 so the memory will not be reclaimed during token replacing below. */
16527 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16528 token->u.tree_check_value->value = template_id;
16529 token->u.tree_check_value->checks = get_deferred_access_checks ();
16530 token->keyword = RID_MAX;
16531
16532 /* Purge all subsequent tokens. */
16533 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16534
16535 /* ??? Can we actually assume that, if template_id ==
16536 error_mark_node, we will have issued a diagnostic to the
16537 user, as opposed to simply marking the tentative parse as
16538 failed? */
16539 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16540 error_at (token->location, "parse error in template argument list");
16541 }
16542
16543 pop_to_parent_deferring_access_checks ();
16544 return template_id;
16545 }
16546
16547 /* Parse a template-name.
16548
16549 template-name:
16550 identifier
16551
16552 The standard should actually say:
16553
16554 template-name:
16555 identifier
16556 operator-function-id
16557
16558 A defect report has been filed about this issue.
16559
16560 A conversion-function-id cannot be a template name because they cannot
16561 be part of a template-id. In fact, looking at this code:
16562
16563 a.operator K<int>()
16564
16565 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16566 It is impossible to call a templated conversion-function-id with an
16567 explicit argument list, since the only allowed template parameter is
16568 the type to which it is converting.
16569
16570 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16571 `template' keyword, in a construction like:
16572
16573 T::template f<3>()
16574
16575 In that case `f' is taken to be a template-name, even though there
16576 is no way of knowing for sure.
16577
16578 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16579 name refers to a set of overloaded functions, at least one of which
16580 is a template, or an IDENTIFIER_NODE with the name of the template,
16581 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16582 names are looked up inside uninstantiated templates. */
16583
16584 static tree
16585 cp_parser_template_name (cp_parser* parser,
16586 bool template_keyword_p,
16587 bool check_dependency_p,
16588 bool is_declaration,
16589 enum tag_types tag_type,
16590 bool *is_identifier)
16591 {
16592 tree identifier;
16593 tree decl;
16594 cp_token *token = cp_lexer_peek_token (parser->lexer);
16595
16596 /* If the next token is `operator', then we have either an
16597 operator-function-id or a conversion-function-id. */
16598 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16599 {
16600 /* We don't know whether we're looking at an
16601 operator-function-id or a conversion-function-id. */
16602 cp_parser_parse_tentatively (parser);
16603 /* Try an operator-function-id. */
16604 identifier = cp_parser_operator_function_id (parser);
16605 /* If that didn't work, try a conversion-function-id. */
16606 if (!cp_parser_parse_definitely (parser))
16607 {
16608 cp_parser_error (parser, "expected template-name");
16609 return error_mark_node;
16610 }
16611 }
16612 /* Look for the identifier. */
16613 else
16614 identifier = cp_parser_identifier (parser);
16615
16616 /* If we didn't find an identifier, we don't have a template-id. */
16617 if (identifier == error_mark_node)
16618 return error_mark_node;
16619
16620 /* If the name immediately followed the `template' keyword, then it
16621 is a template-name. However, if the next token is not `<', then
16622 we do not treat it as a template-name, since it is not being used
16623 as part of a template-id. This enables us to handle constructs
16624 like:
16625
16626 template <typename T> struct S { S(); };
16627 template <typename T> S<T>::S();
16628
16629 correctly. We would treat `S' as a template -- if it were `S<T>'
16630 -- but we do not if there is no `<'. */
16631
16632 if (processing_template_decl
16633 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16634 {
16635 /* In a declaration, in a dependent context, we pretend that the
16636 "template" keyword was present in order to improve error
16637 recovery. For example, given:
16638
16639 template <typename T> void f(T::X<int>);
16640
16641 we want to treat "X<int>" as a template-id. */
16642 if (is_declaration
16643 && !template_keyword_p
16644 && parser->scope && TYPE_P (parser->scope)
16645 && check_dependency_p
16646 && dependent_scope_p (parser->scope)
16647 /* Do not do this for dtors (or ctors), since they never
16648 need the template keyword before their name. */
16649 && !constructor_name_p (identifier, parser->scope))
16650 {
16651 cp_token_position start = 0;
16652
16653 /* Explain what went wrong. */
16654 error_at (token->location, "non-template %qD used as template",
16655 identifier);
16656 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16657 parser->scope, identifier);
16658 /* If parsing tentatively, find the location of the "<" token. */
16659 if (cp_parser_simulate_error (parser))
16660 start = cp_lexer_token_position (parser->lexer, true);
16661 /* Parse the template arguments so that we can issue error
16662 messages about them. */
16663 cp_lexer_consume_token (parser->lexer);
16664 cp_parser_enclosed_template_argument_list (parser);
16665 /* Skip tokens until we find a good place from which to
16666 continue parsing. */
16667 cp_parser_skip_to_closing_parenthesis (parser,
16668 /*recovering=*/true,
16669 /*or_comma=*/true,
16670 /*consume_paren=*/false);
16671 /* If parsing tentatively, permanently remove the
16672 template argument list. That will prevent duplicate
16673 error messages from being issued about the missing
16674 "template" keyword. */
16675 if (start)
16676 cp_lexer_purge_tokens_after (parser->lexer, start);
16677 if (is_identifier)
16678 *is_identifier = true;
16679 parser->context->object_type = NULL_TREE;
16680 return identifier;
16681 }
16682
16683 /* If the "template" keyword is present, then there is generally
16684 no point in doing name-lookup, so we just return IDENTIFIER.
16685 But, if the qualifying scope is non-dependent then we can
16686 (and must) do name-lookup normally. */
16687 if (template_keyword_p)
16688 {
16689 tree scope = (parser->scope ? parser->scope
16690 : parser->context->object_type);
16691 if (scope && TYPE_P (scope)
16692 && (!CLASS_TYPE_P (scope)
16693 || (check_dependency_p && dependent_type_p (scope))))
16694 {
16695 /* We're optimizing away the call to cp_parser_lookup_name, but
16696 we still need to do this. */
16697 parser->context->object_type = NULL_TREE;
16698 return identifier;
16699 }
16700 }
16701 }
16702
16703 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16704 const bool scoped_p = ((parser->scope ? parser->scope
16705 : parser->context->object_type) != NULL_TREE);
16706
16707 /* Look up the name. */
16708 decl = cp_parser_lookup_name (parser, identifier,
16709 tag_type,
16710 /*is_template=*/true,
16711 /*is_namespace=*/false,
16712 check_dependency_p,
16713 /*ambiguous_decls=*/NULL,
16714 token->location);
16715
16716 decl = strip_using_decl (decl);
16717
16718 /* If DECL is a template, then the name was a template-name. */
16719 if (TREE_CODE (decl) == TEMPLATE_DECL)
16720 {
16721 if (TREE_DEPRECATED (decl)
16722 && deprecated_state != DEPRECATED_SUPPRESS)
16723 warn_deprecated_use (decl, NULL_TREE);
16724 }
16725 else
16726 {
16727 /* The standard does not explicitly indicate whether a name that
16728 names a set of overloaded declarations, some of which are
16729 templates, is a template-name. However, such a name should
16730 be a template-name; otherwise, there is no way to form a
16731 template-id for the overloaded templates. */
16732 bool found = false;
16733
16734 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16735 !found && iter; ++iter)
16736 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16737 found = true;
16738
16739 if (!found
16740 && (cxx_dialect > cxx17)
16741 && !scoped_p
16742 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
16743 && tag_type == none_type)
16744 {
16745 /* [temp.names] says "A name is also considered to refer to a template
16746 if it is an unqualified-id followed by a < and name lookup finds
16747 either one or more functions or finds nothing." */
16748
16749 /* The "more functions" case. Just use the OVERLOAD as normally.
16750 We don't use is_overloaded_fn here to avoid considering
16751 BASELINKs. */
16752 if (TREE_CODE (decl) == OVERLOAD
16753 /* Name lookup found one function. */
16754 || TREE_CODE (decl) == FUNCTION_DECL)
16755 found = true;
16756 /* Name lookup found nothing. */
16757 else if (decl == error_mark_node)
16758 return identifier;
16759 }
16760
16761 if (!found)
16762 {
16763 /* The name does not name a template. */
16764 cp_parser_error (parser, "expected template-name");
16765 return error_mark_node;
16766 }
16767 }
16768
16769 return decl;
16770 }
16771
16772 /* Parse a template-argument-list.
16773
16774 template-argument-list:
16775 template-argument ... [opt]
16776 template-argument-list , template-argument ... [opt]
16777
16778 Returns a TREE_VEC containing the arguments. */
16779
16780 static tree
16781 cp_parser_template_argument_list (cp_parser* parser)
16782 {
16783 tree fixed_args[10];
16784 unsigned n_args = 0;
16785 unsigned alloced = 10;
16786 tree *arg_ary = fixed_args;
16787 tree vec;
16788 bool saved_in_template_argument_list_p;
16789 bool saved_ice_p;
16790 bool saved_non_ice_p;
16791
16792 /* Don't create location wrapper nodes within a template-argument-list. */
16793 auto_suppress_location_wrappers sentinel;
16794
16795 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16796 parser->in_template_argument_list_p = true;
16797 /* Even if the template-id appears in an integral
16798 constant-expression, the contents of the argument list do
16799 not. */
16800 saved_ice_p = parser->integral_constant_expression_p;
16801 parser->integral_constant_expression_p = false;
16802 saved_non_ice_p = parser->non_integral_constant_expression_p;
16803 parser->non_integral_constant_expression_p = false;
16804
16805 /* Parse the arguments. */
16806 do
16807 {
16808 tree argument;
16809
16810 if (n_args)
16811 /* Consume the comma. */
16812 cp_lexer_consume_token (parser->lexer);
16813
16814 /* Parse the template-argument. */
16815 argument = cp_parser_template_argument (parser);
16816
16817 /* If the next token is an ellipsis, we're expanding a template
16818 argument pack. */
16819 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16820 {
16821 if (argument == error_mark_node)
16822 {
16823 cp_token *token = cp_lexer_peek_token (parser->lexer);
16824 error_at (token->location,
16825 "expected parameter pack before %<...%>");
16826 }
16827 /* Consume the `...' token. */
16828 cp_lexer_consume_token (parser->lexer);
16829
16830 /* Make the argument into a TYPE_PACK_EXPANSION or
16831 EXPR_PACK_EXPANSION. */
16832 argument = make_pack_expansion (argument);
16833 }
16834
16835 if (n_args == alloced)
16836 {
16837 alloced *= 2;
16838
16839 if (arg_ary == fixed_args)
16840 {
16841 arg_ary = XNEWVEC (tree, alloced);
16842 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16843 }
16844 else
16845 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16846 }
16847 arg_ary[n_args++] = argument;
16848 }
16849 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16850
16851 vec = make_tree_vec (n_args);
16852
16853 while (n_args--)
16854 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16855
16856 if (arg_ary != fixed_args)
16857 free (arg_ary);
16858 parser->non_integral_constant_expression_p = saved_non_ice_p;
16859 parser->integral_constant_expression_p = saved_ice_p;
16860 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16861 if (CHECKING_P)
16862 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16863 return vec;
16864 }
16865
16866 /* Parse a template-argument.
16867
16868 template-argument:
16869 assignment-expression
16870 type-id
16871 id-expression
16872
16873 The representation is that of an assignment-expression, type-id, or
16874 id-expression -- except that the qualified id-expression is
16875 evaluated, so that the value returned is either a DECL or an
16876 OVERLOAD.
16877
16878 Although the standard says "assignment-expression", it forbids
16879 throw-expressions or assignments in the template argument.
16880 Therefore, we use "conditional-expression" instead. */
16881
16882 static tree
16883 cp_parser_template_argument (cp_parser* parser)
16884 {
16885 tree argument;
16886 bool template_p;
16887 bool address_p;
16888 bool maybe_type_id = false;
16889 cp_token *token = NULL, *argument_start_token = NULL;
16890 location_t loc = 0;
16891 cp_id_kind idk;
16892
16893 /* There's really no way to know what we're looking at, so we just
16894 try each alternative in order.
16895
16896 [temp.arg]
16897
16898 In a template-argument, an ambiguity between a type-id and an
16899 expression is resolved to a type-id, regardless of the form of
16900 the corresponding template-parameter.
16901
16902 Therefore, we try a type-id first. */
16903 cp_parser_parse_tentatively (parser);
16904 argument = cp_parser_template_type_arg (parser);
16905 /* If there was no error parsing the type-id but the next token is a
16906 '>>', our behavior depends on which dialect of C++ we're
16907 parsing. In C++98, we probably found a typo for '> >'. But there
16908 are type-id which are also valid expressions. For instance:
16909
16910 struct X { int operator >> (int); };
16911 template <int V> struct Foo {};
16912 Foo<X () >> 5> r;
16913
16914 Here 'X()' is a valid type-id of a function type, but the user just
16915 wanted to write the expression "X() >> 5". Thus, we remember that we
16916 found a valid type-id, but we still try to parse the argument as an
16917 expression to see what happens.
16918
16919 In C++0x, the '>>' will be considered two separate '>'
16920 tokens. */
16921 if (!cp_parser_error_occurred (parser)
16922 && cxx_dialect == cxx98
16923 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16924 {
16925 maybe_type_id = true;
16926 cp_parser_abort_tentative_parse (parser);
16927 }
16928 else
16929 {
16930 /* If the next token isn't a `,' or a `>', then this argument wasn't
16931 really finished. This means that the argument is not a valid
16932 type-id. */
16933 if (!cp_parser_next_token_ends_template_argument_p (parser))
16934 cp_parser_error (parser, "expected template-argument");
16935 /* If that worked, we're done. */
16936 if (cp_parser_parse_definitely (parser))
16937 return argument;
16938 }
16939 /* We're still not sure what the argument will be. */
16940 cp_parser_parse_tentatively (parser);
16941 /* Try a template. */
16942 argument_start_token = cp_lexer_peek_token (parser->lexer);
16943 argument = cp_parser_id_expression (parser,
16944 /*template_keyword_p=*/false,
16945 /*check_dependency_p=*/true,
16946 &template_p,
16947 /*declarator_p=*/false,
16948 /*optional_p=*/false);
16949 /* If the next token isn't a `,' or a `>', then this argument wasn't
16950 really finished. */
16951 if (!cp_parser_next_token_ends_template_argument_p (parser))
16952 cp_parser_error (parser, "expected template-argument");
16953 if (!cp_parser_error_occurred (parser))
16954 {
16955 /* Figure out what is being referred to. If the id-expression
16956 was for a class template specialization, then we will have a
16957 TYPE_DECL at this point. There is no need to do name lookup
16958 at this point in that case. */
16959 if (TREE_CODE (argument) != TYPE_DECL)
16960 argument = cp_parser_lookup_name (parser, argument,
16961 none_type,
16962 /*is_template=*/template_p,
16963 /*is_namespace=*/false,
16964 /*check_dependency=*/true,
16965 /*ambiguous_decls=*/NULL,
16966 argument_start_token->location);
16967 /* Handle a constrained-type-specifier for a non-type template
16968 parameter. */
16969 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16970 argument = decl;
16971 else if (TREE_CODE (argument) != TEMPLATE_DECL
16972 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16973 cp_parser_error (parser, "expected template-name");
16974 }
16975 if (cp_parser_parse_definitely (parser))
16976 {
16977 if (TREE_DEPRECATED (argument))
16978 warn_deprecated_use (argument, NULL_TREE);
16979 return argument;
16980 }
16981 /* It must be a non-type argument. In C++17 any constant-expression is
16982 allowed. */
16983 if (cxx_dialect > cxx14)
16984 goto general_expr;
16985
16986 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16987
16988 -- an integral constant-expression of integral or enumeration
16989 type; or
16990
16991 -- the name of a non-type template-parameter; or
16992
16993 -- the name of an object or function with external linkage...
16994
16995 -- the address of an object or function with external linkage...
16996
16997 -- a pointer to member... */
16998 /* Look for a non-type template parameter. */
16999 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17000 {
17001 cp_parser_parse_tentatively (parser);
17002 argument = cp_parser_primary_expression (parser,
17003 /*address_p=*/false,
17004 /*cast_p=*/false,
17005 /*template_arg_p=*/true,
17006 &idk);
17007 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
17008 || !cp_parser_next_token_ends_template_argument_p (parser))
17009 cp_parser_simulate_error (parser);
17010 if (cp_parser_parse_definitely (parser))
17011 return argument;
17012 }
17013
17014 /* If the next token is "&", the argument must be the address of an
17015 object or function with external linkage. */
17016 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
17017 if (address_p)
17018 {
17019 loc = cp_lexer_peek_token (parser->lexer)->location;
17020 cp_lexer_consume_token (parser->lexer);
17021 }
17022 /* See if we might have an id-expression. */
17023 token = cp_lexer_peek_token (parser->lexer);
17024 if (token->type == CPP_NAME
17025 || token->keyword == RID_OPERATOR
17026 || token->type == CPP_SCOPE
17027 || token->type == CPP_TEMPLATE_ID
17028 || token->type == CPP_NESTED_NAME_SPECIFIER)
17029 {
17030 cp_parser_parse_tentatively (parser);
17031 argument = cp_parser_primary_expression (parser,
17032 address_p,
17033 /*cast_p=*/false,
17034 /*template_arg_p=*/true,
17035 &idk);
17036 if (cp_parser_error_occurred (parser)
17037 || !cp_parser_next_token_ends_template_argument_p (parser))
17038 cp_parser_abort_tentative_parse (parser);
17039 else
17040 {
17041 tree probe;
17042
17043 if (INDIRECT_REF_P (argument))
17044 {
17045 /* Strip the dereference temporarily. */
17046 gcc_assert (REFERENCE_REF_P (argument));
17047 argument = TREE_OPERAND (argument, 0);
17048 }
17049
17050 /* If we're in a template, we represent a qualified-id referring
17051 to a static data member as a SCOPE_REF even if the scope isn't
17052 dependent so that we can check access control later. */
17053 probe = argument;
17054 if (TREE_CODE (probe) == SCOPE_REF)
17055 probe = TREE_OPERAND (probe, 1);
17056 if (VAR_P (probe))
17057 {
17058 /* A variable without external linkage might still be a
17059 valid constant-expression, so no error is issued here
17060 if the external-linkage check fails. */
17061 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
17062 cp_parser_simulate_error (parser);
17063 }
17064 else if (is_overloaded_fn (argument))
17065 /* All overloaded functions are allowed; if the external
17066 linkage test does not pass, an error will be issued
17067 later. */
17068 ;
17069 else if (address_p
17070 && (TREE_CODE (argument) == OFFSET_REF
17071 || TREE_CODE (argument) == SCOPE_REF))
17072 /* A pointer-to-member. */
17073 ;
17074 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
17075 ;
17076 else
17077 cp_parser_simulate_error (parser);
17078
17079 if (cp_parser_parse_definitely (parser))
17080 {
17081 if (address_p)
17082 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
17083 tf_warning_or_error);
17084 else
17085 argument = convert_from_reference (argument);
17086 return argument;
17087 }
17088 }
17089 }
17090 /* If the argument started with "&", there are no other valid
17091 alternatives at this point. */
17092 if (address_p)
17093 {
17094 cp_parser_error (parser, "invalid non-type template argument");
17095 return error_mark_node;
17096 }
17097
17098 general_expr:
17099 /* If the argument wasn't successfully parsed as a type-id followed
17100 by '>>', the argument can only be a constant expression now.
17101 Otherwise, we try parsing the constant-expression tentatively,
17102 because the argument could really be a type-id. */
17103 if (maybe_type_id)
17104 cp_parser_parse_tentatively (parser);
17105
17106 if (cxx_dialect <= cxx14)
17107 argument = cp_parser_constant_expression (parser);
17108 else
17109 {
17110 /* In C++20, we can encounter a braced-init-list. */
17111 if (cxx_dialect >= cxx2a
17112 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17113 {
17114 bool expr_non_constant_p;
17115 return cp_parser_braced_list (parser, &expr_non_constant_p);
17116 }
17117
17118 /* With C++17 generalized non-type template arguments we need to handle
17119 lvalue constant expressions, too. */
17120 argument = cp_parser_assignment_expression (parser);
17121 require_potential_constant_expression (argument);
17122 }
17123
17124 if (!maybe_type_id)
17125 return argument;
17126 if (!cp_parser_next_token_ends_template_argument_p (parser))
17127 cp_parser_error (parser, "expected template-argument");
17128 if (cp_parser_parse_definitely (parser))
17129 return argument;
17130 /* We did our best to parse the argument as a non type-id, but that
17131 was the only alternative that matched (albeit with a '>' after
17132 it). We can assume it's just a typo from the user, and a
17133 diagnostic will then be issued. */
17134 return cp_parser_template_type_arg (parser);
17135 }
17136
17137 /* Parse an explicit-instantiation.
17138
17139 explicit-instantiation:
17140 template declaration
17141
17142 Although the standard says `declaration', what it really means is:
17143
17144 explicit-instantiation:
17145 template decl-specifier-seq [opt] declarator [opt] ;
17146
17147 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17148 supposed to be allowed. A defect report has been filed about this
17149 issue.
17150
17151 GNU Extension:
17152
17153 explicit-instantiation:
17154 storage-class-specifier template
17155 decl-specifier-seq [opt] declarator [opt] ;
17156 function-specifier template
17157 decl-specifier-seq [opt] declarator [opt] ; */
17158
17159 static void
17160 cp_parser_explicit_instantiation (cp_parser* parser)
17161 {
17162 int declares_class_or_enum;
17163 cp_decl_specifier_seq decl_specifiers;
17164 tree extension_specifier = NULL_TREE;
17165
17166 timevar_push (TV_TEMPLATE_INST);
17167
17168 /* Look for an (optional) storage-class-specifier or
17169 function-specifier. */
17170 if (cp_parser_allow_gnu_extensions_p (parser))
17171 {
17172 extension_specifier
17173 = cp_parser_storage_class_specifier_opt (parser);
17174 if (!extension_specifier)
17175 extension_specifier
17176 = cp_parser_function_specifier_opt (parser,
17177 /*decl_specs=*/NULL);
17178 }
17179
17180 /* Look for the `template' keyword. */
17181 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17182 /* Let the front end know that we are processing an explicit
17183 instantiation. */
17184 begin_explicit_instantiation ();
17185 /* [temp.explicit] says that we are supposed to ignore access
17186 control while processing explicit instantiation directives. */
17187 push_deferring_access_checks (dk_no_check);
17188 /* Parse a decl-specifier-seq. */
17189 cp_parser_decl_specifier_seq (parser,
17190 CP_PARSER_FLAGS_OPTIONAL,
17191 &decl_specifiers,
17192 &declares_class_or_enum);
17193 /* If there was exactly one decl-specifier, and it declared a class,
17194 and there's no declarator, then we have an explicit type
17195 instantiation. */
17196 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
17197 {
17198 tree type;
17199
17200 type = check_tag_decl (&decl_specifiers,
17201 /*explicit_type_instantiation_p=*/true);
17202 /* Turn access control back on for names used during
17203 template instantiation. */
17204 pop_deferring_access_checks ();
17205 if (type)
17206 do_type_instantiation (type, extension_specifier,
17207 /*complain=*/tf_error);
17208 }
17209 else
17210 {
17211 cp_declarator *declarator;
17212 tree decl;
17213
17214 /* Parse the declarator. */
17215 declarator
17216 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17217 CP_PARSER_FLAGS_NONE,
17218 /*ctor_dtor_or_conv_p=*/NULL,
17219 /*parenthesized_p=*/NULL,
17220 /*member_p=*/false,
17221 /*friend_p=*/false,
17222 /*static_p=*/false);
17223 if (declares_class_or_enum & 2)
17224 cp_parser_check_for_definition_in_return_type (declarator,
17225 decl_specifiers.type,
17226 decl_specifiers.locations[ds_type_spec]);
17227 if (declarator != cp_error_declarator)
17228 {
17229 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
17230 permerror (decl_specifiers.locations[ds_inline],
17231 "explicit instantiation shall not use"
17232 " %<inline%> specifier");
17233 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
17234 permerror (decl_specifiers.locations[ds_constexpr],
17235 "explicit instantiation shall not use"
17236 " %<constexpr%> specifier");
17237
17238 decl = grokdeclarator (declarator, &decl_specifiers,
17239 NORMAL, 0, &decl_specifiers.attributes);
17240 /* Turn access control back on for names used during
17241 template instantiation. */
17242 pop_deferring_access_checks ();
17243 /* Do the explicit instantiation. */
17244 do_decl_instantiation (decl, extension_specifier);
17245 }
17246 else
17247 {
17248 pop_deferring_access_checks ();
17249 /* Skip the body of the explicit instantiation. */
17250 cp_parser_skip_to_end_of_statement (parser);
17251 }
17252 }
17253 /* We're done with the instantiation. */
17254 end_explicit_instantiation ();
17255
17256 cp_parser_consume_semicolon_at_end_of_statement (parser);
17257
17258 timevar_pop (TV_TEMPLATE_INST);
17259 }
17260
17261 /* Parse an explicit-specialization.
17262
17263 explicit-specialization:
17264 template < > declaration
17265
17266 Although the standard says `declaration', what it really means is:
17267
17268 explicit-specialization:
17269 template <> decl-specifier [opt] init-declarator [opt] ;
17270 template <> function-definition
17271 template <> explicit-specialization
17272 template <> template-declaration */
17273
17274 static void
17275 cp_parser_explicit_specialization (cp_parser* parser)
17276 {
17277 bool need_lang_pop;
17278 cp_token *token = cp_lexer_peek_token (parser->lexer);
17279
17280 /* Look for the `template' keyword. */
17281 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17282 /* Look for the `<'. */
17283 cp_parser_require (parser, CPP_LESS, RT_LESS);
17284 /* Look for the `>'. */
17285 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17286 /* We have processed another parameter list. */
17287 ++parser->num_template_parameter_lists;
17288 /* [temp]
17289
17290 A template ... explicit specialization ... shall not have C
17291 linkage. */
17292 if (current_lang_name == lang_name_c)
17293 {
17294 error_at (token->location, "template specialization with C linkage");
17295 maybe_show_extern_c_location ();
17296 /* Give it C++ linkage to avoid confusing other parts of the
17297 front end. */
17298 push_lang_context (lang_name_cplusplus);
17299 need_lang_pop = true;
17300 }
17301 else
17302 need_lang_pop = false;
17303 /* Let the front end know that we are beginning a specialization. */
17304 if (!begin_specialization ())
17305 {
17306 end_specialization ();
17307 return;
17308 }
17309
17310 /* If the next keyword is `template', we need to figure out whether
17311 or not we're looking a template-declaration. */
17312 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17313 {
17314 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17315 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
17316 cp_parser_template_declaration_after_export (parser,
17317 /*member_p=*/false);
17318 else
17319 cp_parser_explicit_specialization (parser);
17320 }
17321 else
17322 /* Parse the dependent declaration. */
17323 cp_parser_single_declaration (parser,
17324 /*checks=*/NULL,
17325 /*member_p=*/false,
17326 /*explicit_specialization_p=*/true,
17327 /*friend_p=*/NULL);
17328 /* We're done with the specialization. */
17329 end_specialization ();
17330 /* For the erroneous case of a template with C linkage, we pushed an
17331 implicit C++ linkage scope; exit that scope now. */
17332 if (need_lang_pop)
17333 pop_lang_context ();
17334 /* We're done with this parameter list. */
17335 --parser->num_template_parameter_lists;
17336 }
17337
17338 /* Parse a type-specifier.
17339
17340 type-specifier:
17341 simple-type-specifier
17342 class-specifier
17343 enum-specifier
17344 elaborated-type-specifier
17345 cv-qualifier
17346
17347 GNU Extension:
17348
17349 type-specifier:
17350 __complex__
17351
17352 Returns a representation of the type-specifier. For a
17353 class-specifier, enum-specifier, or elaborated-type-specifier, a
17354 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17355
17356 The parser flags FLAGS is used to control type-specifier parsing.
17357
17358 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17359 in a decl-specifier-seq.
17360
17361 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17362 class-specifier, enum-specifier, or elaborated-type-specifier, then
17363 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17364 if a type is declared; 2 if it is defined. Otherwise, it is set to
17365 zero.
17366
17367 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17368 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17369 is set to FALSE. */
17370
17371 static tree
17372 cp_parser_type_specifier (cp_parser* parser,
17373 cp_parser_flags flags,
17374 cp_decl_specifier_seq *decl_specs,
17375 bool is_declaration,
17376 int* declares_class_or_enum,
17377 bool* is_cv_qualifier)
17378 {
17379 tree type_spec = NULL_TREE;
17380 cp_token *token;
17381 enum rid keyword;
17382 cp_decl_spec ds = ds_last;
17383
17384 /* Assume this type-specifier does not declare a new type. */
17385 if (declares_class_or_enum)
17386 *declares_class_or_enum = 0;
17387 /* And that it does not specify a cv-qualifier. */
17388 if (is_cv_qualifier)
17389 *is_cv_qualifier = false;
17390 /* Peek at the next token. */
17391 token = cp_lexer_peek_token (parser->lexer);
17392
17393 /* If we're looking at a keyword, we can use that to guide the
17394 production we choose. */
17395 keyword = token->keyword;
17396 switch (keyword)
17397 {
17398 case RID_ENUM:
17399 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17400 goto elaborated_type_specifier;
17401
17402 /* Look for the enum-specifier. */
17403 type_spec = cp_parser_enum_specifier (parser);
17404 /* If that worked, we're done. */
17405 if (type_spec)
17406 {
17407 if (declares_class_or_enum)
17408 *declares_class_or_enum = 2;
17409 if (decl_specs)
17410 cp_parser_set_decl_spec_type (decl_specs,
17411 type_spec,
17412 token,
17413 /*type_definition_p=*/true);
17414 return type_spec;
17415 }
17416 else
17417 goto elaborated_type_specifier;
17418
17419 /* Any of these indicate either a class-specifier, or an
17420 elaborated-type-specifier. */
17421 case RID_CLASS:
17422 case RID_STRUCT:
17423 case RID_UNION:
17424 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17425 goto elaborated_type_specifier;
17426
17427 /* Parse tentatively so that we can back up if we don't find a
17428 class-specifier. */
17429 cp_parser_parse_tentatively (parser);
17430 /* Look for the class-specifier. */
17431 type_spec = cp_parser_class_specifier (parser);
17432 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17433 /* If that worked, we're done. */
17434 if (cp_parser_parse_definitely (parser))
17435 {
17436 if (declares_class_or_enum)
17437 *declares_class_or_enum = 2;
17438 if (decl_specs)
17439 cp_parser_set_decl_spec_type (decl_specs,
17440 type_spec,
17441 token,
17442 /*type_definition_p=*/true);
17443 return type_spec;
17444 }
17445
17446 /* Fall through. */
17447 elaborated_type_specifier:
17448 /* We're declaring (not defining) a class or enum. */
17449 if (declares_class_or_enum)
17450 *declares_class_or_enum = 1;
17451
17452 /* Fall through. */
17453 case RID_TYPENAME:
17454 /* Look for an elaborated-type-specifier. */
17455 type_spec
17456 = (cp_parser_elaborated_type_specifier
17457 (parser,
17458 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17459 is_declaration));
17460 if (decl_specs)
17461 cp_parser_set_decl_spec_type (decl_specs,
17462 type_spec,
17463 token,
17464 /*type_definition_p=*/false);
17465 return type_spec;
17466
17467 case RID_CONST:
17468 ds = ds_const;
17469 if (is_cv_qualifier)
17470 *is_cv_qualifier = true;
17471 break;
17472
17473 case RID_VOLATILE:
17474 ds = ds_volatile;
17475 if (is_cv_qualifier)
17476 *is_cv_qualifier = true;
17477 break;
17478
17479 case RID_RESTRICT:
17480 ds = ds_restrict;
17481 if (is_cv_qualifier)
17482 *is_cv_qualifier = true;
17483 break;
17484
17485 case RID_COMPLEX:
17486 /* The `__complex__' keyword is a GNU extension. */
17487 ds = ds_complex;
17488 break;
17489
17490 default:
17491 break;
17492 }
17493
17494 /* Handle simple keywords. */
17495 if (ds != ds_last)
17496 {
17497 if (decl_specs)
17498 {
17499 set_and_check_decl_spec_loc (decl_specs, ds, token);
17500 decl_specs->any_specifiers_p = true;
17501 }
17502 return cp_lexer_consume_token (parser->lexer)->u.value;
17503 }
17504
17505 /* If we do not already have a type-specifier, assume we are looking
17506 at a simple-type-specifier. */
17507 type_spec = cp_parser_simple_type_specifier (parser,
17508 decl_specs,
17509 flags);
17510
17511 /* If we didn't find a type-specifier, and a type-specifier was not
17512 optional in this context, issue an error message. */
17513 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17514 {
17515 cp_parser_error (parser, "expected type specifier");
17516 return error_mark_node;
17517 }
17518
17519 return type_spec;
17520 }
17521
17522 /* Parse a simple-type-specifier.
17523
17524 simple-type-specifier:
17525 :: [opt] nested-name-specifier [opt] type-name
17526 :: [opt] nested-name-specifier template template-id
17527 char
17528 wchar_t
17529 bool
17530 short
17531 int
17532 long
17533 signed
17534 unsigned
17535 float
17536 double
17537 void
17538
17539 C++11 Extension:
17540
17541 simple-type-specifier:
17542 auto
17543 decltype ( expression )
17544 char16_t
17545 char32_t
17546 __underlying_type ( type-id )
17547
17548 C++17 extension:
17549
17550 nested-name-specifier(opt) template-name
17551
17552 GNU Extension:
17553
17554 simple-type-specifier:
17555 __int128
17556 __typeof__ unary-expression
17557 __typeof__ ( type-id )
17558 __typeof__ ( type-id ) { initializer-list , [opt] }
17559
17560 Concepts Extension:
17561
17562 simple-type-specifier:
17563 constrained-type-specifier
17564
17565 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17566 appropriately updated. */
17567
17568 static tree
17569 cp_parser_simple_type_specifier (cp_parser* parser,
17570 cp_decl_specifier_seq *decl_specs,
17571 cp_parser_flags flags)
17572 {
17573 tree type = NULL_TREE;
17574 cp_token *token;
17575 int idx;
17576
17577 /* Peek at the next token. */
17578 token = cp_lexer_peek_token (parser->lexer);
17579
17580 /* If we're looking at a keyword, things are easy. */
17581 switch (token->keyword)
17582 {
17583 case RID_CHAR:
17584 if (decl_specs)
17585 decl_specs->explicit_char_p = true;
17586 type = char_type_node;
17587 break;
17588 case RID_CHAR8:
17589 type = char8_type_node;
17590 break;
17591 case RID_CHAR16:
17592 type = char16_type_node;
17593 break;
17594 case RID_CHAR32:
17595 type = char32_type_node;
17596 break;
17597 case RID_WCHAR:
17598 type = wchar_type_node;
17599 break;
17600 case RID_BOOL:
17601 type = boolean_type_node;
17602 break;
17603 case RID_SHORT:
17604 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17605 type = short_integer_type_node;
17606 break;
17607 case RID_INT:
17608 if (decl_specs)
17609 decl_specs->explicit_int_p = true;
17610 type = integer_type_node;
17611 break;
17612 case RID_INT_N_0:
17613 case RID_INT_N_1:
17614 case RID_INT_N_2:
17615 case RID_INT_N_3:
17616 idx = token->keyword - RID_INT_N_0;
17617 if (! int_n_enabled_p [idx])
17618 break;
17619 if (decl_specs)
17620 {
17621 decl_specs->explicit_intN_p = true;
17622 decl_specs->int_n_idx = idx;
17623 }
17624 type = int_n_trees [idx].signed_type;
17625 break;
17626 case RID_LONG:
17627 if (decl_specs)
17628 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17629 type = long_integer_type_node;
17630 break;
17631 case RID_SIGNED:
17632 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17633 type = integer_type_node;
17634 break;
17635 case RID_UNSIGNED:
17636 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17637 type = unsigned_type_node;
17638 break;
17639 case RID_FLOAT:
17640 type = float_type_node;
17641 break;
17642 case RID_DOUBLE:
17643 type = double_type_node;
17644 break;
17645 case RID_VOID:
17646 type = void_type_node;
17647 break;
17648
17649 case RID_AUTO:
17650 maybe_warn_cpp0x (CPP0X_AUTO);
17651 if (parser->auto_is_implicit_function_template_parm_p)
17652 {
17653 /* The 'auto' might be the placeholder return type for a function decl
17654 with trailing return type. */
17655 bool have_trailing_return_fn_decl = false;
17656
17657 cp_parser_parse_tentatively (parser);
17658 cp_lexer_consume_token (parser->lexer);
17659 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17660 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17661 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17662 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17663 {
17664 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17665 {
17666 cp_lexer_consume_token (parser->lexer);
17667 cp_parser_skip_to_closing_parenthesis (parser,
17668 /*recovering*/false,
17669 /*or_comma*/false,
17670 /*consume_paren*/true);
17671 continue;
17672 }
17673
17674 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17675 {
17676 have_trailing_return_fn_decl = true;
17677 break;
17678 }
17679
17680 cp_lexer_consume_token (parser->lexer);
17681 }
17682 cp_parser_abort_tentative_parse (parser);
17683
17684 if (have_trailing_return_fn_decl)
17685 {
17686 type = make_auto ();
17687 break;
17688 }
17689
17690 if (cxx_dialect >= cxx14)
17691 {
17692 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17693 type = TREE_TYPE (type);
17694 }
17695 else
17696 type = error_mark_node;
17697
17698 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17699 {
17700 if (cxx_dialect < cxx14)
17701 error_at (token->location,
17702 "use of %<auto%> in lambda parameter declaration "
17703 "only available with "
17704 "%<-std=c++14%> or %<-std=gnu++14%>");
17705 }
17706 else if (cxx_dialect < cxx14)
17707 error_at (token->location,
17708 "use of %<auto%> in parameter declaration "
17709 "only available with "
17710 "%<-std=c++14%> or %<-std=gnu++14%>");
17711 else if (!flag_concepts)
17712 pedwarn (token->location, 0,
17713 "use of %<auto%> in parameter declaration "
17714 "only available with %<-fconcepts%>");
17715 }
17716 else
17717 type = make_auto ();
17718 break;
17719
17720 case RID_DECLTYPE:
17721 /* Since DR 743, decltype can either be a simple-type-specifier by
17722 itself or begin a nested-name-specifier. Parsing it will replace
17723 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17724 handling below decide what to do. */
17725 cp_parser_decltype (parser);
17726 cp_lexer_set_token_position (parser->lexer, token);
17727 break;
17728
17729 case RID_TYPEOF:
17730 /* Consume the `typeof' token. */
17731 cp_lexer_consume_token (parser->lexer);
17732 /* Parse the operand to `typeof'. */
17733 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17734 /* If it is not already a TYPE, take its type. */
17735 if (!TYPE_P (type))
17736 type = finish_typeof (type);
17737
17738 if (decl_specs)
17739 cp_parser_set_decl_spec_type (decl_specs, type,
17740 token,
17741 /*type_definition_p=*/false);
17742
17743 return type;
17744
17745 case RID_UNDERLYING_TYPE:
17746 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17747 if (decl_specs)
17748 cp_parser_set_decl_spec_type (decl_specs, type,
17749 token,
17750 /*type_definition_p=*/false);
17751
17752 return type;
17753
17754 case RID_BASES:
17755 case RID_DIRECT_BASES:
17756 type = cp_parser_trait_expr (parser, token->keyword);
17757 if (decl_specs)
17758 cp_parser_set_decl_spec_type (decl_specs, type,
17759 token,
17760 /*type_definition_p=*/false);
17761 return type;
17762 default:
17763 break;
17764 }
17765
17766 /* If token is an already-parsed decltype not followed by ::,
17767 it's a simple-type-specifier. */
17768 if (token->type == CPP_DECLTYPE
17769 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17770 {
17771 type = saved_checks_value (token->u.tree_check_value);
17772 if (decl_specs)
17773 {
17774 cp_parser_set_decl_spec_type (decl_specs, type,
17775 token,
17776 /*type_definition_p=*/false);
17777 /* Remember that we are handling a decltype in order to
17778 implement the resolution of DR 1510 when the argument
17779 isn't instantiation dependent. */
17780 decl_specs->decltype_p = true;
17781 }
17782 cp_lexer_consume_token (parser->lexer);
17783 return type;
17784 }
17785
17786 /* If the type-specifier was for a built-in type, we're done. */
17787 if (type)
17788 {
17789 /* Record the type. */
17790 if (decl_specs
17791 && (token->keyword != RID_SIGNED
17792 && token->keyword != RID_UNSIGNED
17793 && token->keyword != RID_SHORT
17794 && token->keyword != RID_LONG))
17795 cp_parser_set_decl_spec_type (decl_specs,
17796 type,
17797 token,
17798 /*type_definition_p=*/false);
17799 if (decl_specs)
17800 decl_specs->any_specifiers_p = true;
17801
17802 /* Consume the token. */
17803 cp_lexer_consume_token (parser->lexer);
17804
17805 if (type == error_mark_node)
17806 return error_mark_node;
17807
17808 /* There is no valid C++ program where a non-template type is
17809 followed by a "<". That usually indicates that the user thought
17810 that the type was a template. */
17811 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17812 token->location);
17813
17814 return TYPE_NAME (type);
17815 }
17816
17817 /* The type-specifier must be a user-defined type. */
17818 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17819 {
17820 bool qualified_p;
17821 bool global_p;
17822 const bool typename_p = (cxx_dialect >= cxx2a
17823 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
17824
17825 /* Don't gobble tokens or issue error messages if this is an
17826 optional type-specifier. */
17827 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17828 cp_parser_parse_tentatively (parser);
17829
17830 token = cp_lexer_peek_token (parser->lexer);
17831
17832 /* Look for the optional `::' operator. */
17833 global_p
17834 = (cp_parser_global_scope_opt (parser,
17835 /*current_scope_valid_p=*/false)
17836 != NULL_TREE);
17837 /* Look for the nested-name specifier. */
17838 qualified_p
17839 = (cp_parser_nested_name_specifier_opt (parser,
17840 /*typename_keyword_p=*/false,
17841 /*check_dependency_p=*/true,
17842 /*type_p=*/false,
17843 /*is_declaration=*/false)
17844 != NULL_TREE);
17845 /* If we have seen a nested-name-specifier, and the next token
17846 is `template', then we are using the template-id production. */
17847 if (parser->scope
17848 && cp_parser_optional_template_keyword (parser))
17849 {
17850 /* Look for the template-id. */
17851 type = cp_parser_template_id (parser,
17852 /*template_keyword_p=*/true,
17853 /*check_dependency_p=*/true,
17854 none_type,
17855 /*is_declaration=*/false);
17856 /* If the template-id did not name a type, we are out of
17857 luck. */
17858 if (TREE_CODE (type) != TYPE_DECL)
17859 {
17860 /* ...unless we pretend we have seen 'typename'. */
17861 if (typename_p)
17862 type = cp_parser_make_typename_type (parser, type,
17863 token->location);
17864 else
17865 {
17866 cp_parser_error (parser, "expected template-id for type");
17867 type = NULL_TREE;
17868 }
17869 }
17870 }
17871 /* Otherwise, look for a type-name. */
17872 else
17873 type = cp_parser_type_name (parser, (qualified_p && typename_p));
17874
17875 /* Keep track of all name-lookups performed in class scopes. */
17876 if (type
17877 && !global_p
17878 && !qualified_p
17879 && TREE_CODE (type) == TYPE_DECL
17880 && identifier_p (DECL_NAME (type)))
17881 maybe_note_name_used_in_class (DECL_NAME (type), type);
17882 /* If it didn't work out, we don't have a TYPE. */
17883 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17884 && !cp_parser_parse_definitely (parser))
17885 type = NULL_TREE;
17886 if (!type && cxx_dialect >= cxx17)
17887 {
17888 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17889 cp_parser_parse_tentatively (parser);
17890
17891 cp_parser_global_scope_opt (parser,
17892 /*current_scope_valid_p=*/false);
17893 cp_parser_nested_name_specifier_opt (parser,
17894 /*typename_keyword_p=*/false,
17895 /*check_dependency_p=*/true,
17896 /*type_p=*/false,
17897 /*is_declaration=*/false);
17898 tree name = cp_parser_identifier (parser);
17899 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17900 && parser->scope != error_mark_node)
17901 {
17902 tree tmpl = cp_parser_lookup_name (parser, name,
17903 none_type,
17904 /*is_template=*/false,
17905 /*is_namespace=*/false,
17906 /*check_dependency=*/true,
17907 /*ambiguous_decls=*/NULL,
17908 token->location);
17909 if (tmpl && tmpl != error_mark_node
17910 && (DECL_CLASS_TEMPLATE_P (tmpl)
17911 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17912 type = make_template_placeholder (tmpl);
17913 else
17914 {
17915 type = error_mark_node;
17916 if (!cp_parser_simulate_error (parser))
17917 cp_parser_name_lookup_error (parser, name, tmpl,
17918 NLE_TYPE, token->location);
17919 }
17920 }
17921 else
17922 type = error_mark_node;
17923
17924 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17925 && !cp_parser_parse_definitely (parser))
17926 type = NULL_TREE;
17927 }
17928 if (type && decl_specs)
17929 cp_parser_set_decl_spec_type (decl_specs, type,
17930 token,
17931 /*type_definition_p=*/false);
17932 }
17933
17934 /* If we didn't get a type-name, issue an error message. */
17935 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17936 {
17937 cp_parser_error (parser, "expected type-name");
17938 return error_mark_node;
17939 }
17940
17941 if (type && type != error_mark_node)
17942 {
17943 /* See if TYPE is an Objective-C type, and if so, parse and
17944 accept any protocol references following it. Do this before
17945 the cp_parser_check_for_invalid_template_id() call, because
17946 Objective-C types can be followed by '<...>' which would
17947 enclose protocol names rather than template arguments, and so
17948 everything is fine. */
17949 if (c_dialect_objc () && !parser->scope
17950 && (objc_is_id (type) || objc_is_class_name (type)))
17951 {
17952 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17953 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17954
17955 /* Clobber the "unqualified" type previously entered into
17956 DECL_SPECS with the new, improved protocol-qualified version. */
17957 if (decl_specs)
17958 decl_specs->type = qual_type;
17959
17960 return qual_type;
17961 }
17962
17963 /* There is no valid C++ program where a non-template type is
17964 followed by a "<". That usually indicates that the user
17965 thought that the type was a template. */
17966 cp_parser_check_for_invalid_template_id (parser, type,
17967 none_type,
17968 token->location);
17969 }
17970
17971 return type;
17972 }
17973
17974 /* Parse a type-name.
17975
17976 type-name:
17977 class-name
17978 enum-name
17979 typedef-name
17980 simple-template-id [in c++0x]
17981
17982 enum-name:
17983 identifier
17984
17985 typedef-name:
17986 identifier
17987
17988 Concepts:
17989
17990 type-name:
17991 concept-name
17992 partial-concept-id
17993
17994 concept-name:
17995 identifier
17996
17997 Returns a TYPE_DECL for the type. */
17998
17999 static tree
18000 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
18001 {
18002 tree type_decl;
18003
18004 /* We can't know yet whether it is a class-name or not. */
18005 cp_parser_parse_tentatively (parser);
18006 /* Try a class-name. */
18007 type_decl = cp_parser_class_name (parser,
18008 typename_keyword_p,
18009 /*template_keyword_p=*/false,
18010 none_type,
18011 /*check_dependency_p=*/true,
18012 /*class_head_p=*/false,
18013 /*is_declaration=*/false);
18014 /* If it's not a class-name, keep looking. */
18015 if (!cp_parser_parse_definitely (parser))
18016 {
18017 if (cxx_dialect < cxx11)
18018 /* It must be a typedef-name or an enum-name. */
18019 return cp_parser_nonclass_name (parser);
18020
18021 cp_parser_parse_tentatively (parser);
18022 /* It is either a simple-template-id representing an
18023 instantiation of an alias template... */
18024 type_decl = cp_parser_template_id (parser,
18025 /*template_keyword_p=*/false,
18026 /*check_dependency_p=*/true,
18027 none_type,
18028 /*is_declaration=*/false);
18029 /* Note that this must be an instantiation of an alias template
18030 because [temp.names]/6 says:
18031
18032 A template-id that names an alias template specialization
18033 is a type-name.
18034
18035 Whereas [temp.names]/7 says:
18036
18037 A simple-template-id that names a class template
18038 specialization is a class-name.
18039
18040 With concepts, this could also be a partial-concept-id that
18041 declares a non-type template parameter. */
18042 if (type_decl != NULL_TREE
18043 && TREE_CODE (type_decl) == TYPE_DECL
18044 && TYPE_DECL_ALIAS_P (type_decl))
18045 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
18046 else if (is_constrained_parameter (type_decl))
18047 /* Don't do anything. */ ;
18048 else
18049 cp_parser_simulate_error (parser);
18050
18051 if (!cp_parser_parse_definitely (parser))
18052 /* ... Or a typedef-name or an enum-name. */
18053 return cp_parser_nonclass_name (parser);
18054 }
18055
18056 return type_decl;
18057 }
18058
18059 /* Check if DECL and ARGS can form a constrained-type-specifier.
18060 If ARGS is non-null, we try to form a concept check of the
18061 form DECL<?, ARGS> where ? is a wildcard that matches any
18062 kind of template argument. If ARGS is NULL, then we try to
18063 form a concept check of the form DECL<?>. */
18064
18065 static tree
18066 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
18067 tree decl, tree args)
18068 {
18069 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
18070
18071 /* If we a constrained-type-specifier cannot be deduced. */
18072 if (parser->prevent_constrained_type_specifiers)
18073 return NULL_TREE;
18074
18075 /* A constrained type specifier can only be found in an
18076 overload set or as a reference to a template declaration.
18077
18078 FIXME: This might be masking a bug. It's possible that
18079 that the deduction below is causing template specializations
18080 to be formed with the wildcard as an argument. */
18081 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
18082 return NULL_TREE;
18083
18084 /* Try to build a call expression that evaluates the
18085 concept. This can fail if the overload set refers
18086 only to non-templates. */
18087 tree placeholder = build_nt (WILDCARD_DECL);
18088 tree check = build_concept_check (decl, placeholder, args);
18089 if (check == error_mark_node)
18090 return NULL_TREE;
18091
18092 /* Deduce the checked constraint and the prototype parameter.
18093
18094 FIXME: In certain cases, failure to deduce should be a
18095 diagnosable error. */
18096 tree conc;
18097 tree proto;
18098 if (!deduce_constrained_parameter (check, conc, proto))
18099 return NULL_TREE;
18100
18101 /* In template parameter scope, this results in a constrained
18102 parameter. Return a descriptor of that parm. */
18103 if (processing_template_parmlist)
18104 return build_constrained_parameter (conc, proto, args);
18105
18106 /* In a parameter-declaration-clause, constrained-type
18107 specifiers result in invented template parameters. */
18108 if (parser->auto_is_implicit_function_template_parm_p)
18109 {
18110 tree x = build_constrained_parameter (conc, proto, args);
18111 return synthesize_implicit_template_parm (parser, x);
18112 }
18113 else
18114 {
18115 /* Otherwise, we're in a context where the constrained
18116 type name is deduced and the constraint applies
18117 after deduction. */
18118 return make_constrained_auto (conc, args);
18119 }
18120
18121 return NULL_TREE;
18122 }
18123
18124 /* If DECL refers to a concept, return a TYPE_DECL representing
18125 the result of using the constrained type specifier in the
18126 current context. DECL refers to a concept if
18127
18128 - it is an overload set containing a function concept taking a single
18129 type argument, or
18130
18131 - it is a variable concept taking a single type argument. */
18132
18133 static tree
18134 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
18135 {
18136 if (flag_concepts
18137 && (TREE_CODE (decl) == OVERLOAD
18138 || BASELINK_P (decl)
18139 || variable_concept_p (decl)))
18140 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
18141 else
18142 return NULL_TREE;
18143 }
18144
18145 /* Check if DECL and ARGS form a partial-concept-id. If so,
18146 assign ID to the resulting constrained placeholder.
18147
18148 Returns true if the partial-concept-id designates a placeholder
18149 and false otherwise. Note that *id is set to NULL_TREE in
18150 this case. */
18151
18152 static tree
18153 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
18154 {
18155 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
18156 }
18157
18158 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18159 or a concept-name.
18160
18161 enum-name:
18162 identifier
18163
18164 typedef-name:
18165 identifier
18166
18167 concept-name:
18168 identifier
18169
18170 Returns a TYPE_DECL for the type. */
18171
18172 static tree
18173 cp_parser_nonclass_name (cp_parser* parser)
18174 {
18175 tree type_decl;
18176 tree identifier;
18177
18178 cp_token *token = cp_lexer_peek_token (parser->lexer);
18179 identifier = cp_parser_identifier (parser);
18180 if (identifier == error_mark_node)
18181 return error_mark_node;
18182
18183 /* Look up the type-name. */
18184 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
18185
18186 type_decl = strip_using_decl (type_decl);
18187
18188 /* If we found an overload set, then it may refer to a concept-name. */
18189 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
18190 type_decl = decl;
18191
18192 if (TREE_CODE (type_decl) != TYPE_DECL
18193 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
18194 {
18195 /* See if this is an Objective-C type. */
18196 tree protos = cp_parser_objc_protocol_refs_opt (parser);
18197 tree type = objc_get_protocol_qualified_type (identifier, protos);
18198 if (type)
18199 type_decl = TYPE_NAME (type);
18200 }
18201
18202 /* Issue an error if we did not find a type-name. */
18203 if (TREE_CODE (type_decl) != TYPE_DECL
18204 /* In Objective-C, we have the complication that class names are
18205 normally type names and start declarations (eg, the
18206 "NSObject" in "NSObject *object;"), but can be used in an
18207 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18208 is an expression. So, a classname followed by a dot is not a
18209 valid type-name. */
18210 || (objc_is_class_name (TREE_TYPE (type_decl))
18211 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
18212 {
18213 if (!cp_parser_simulate_error (parser))
18214 cp_parser_name_lookup_error (parser, identifier, type_decl,
18215 NLE_TYPE, token->location);
18216 return error_mark_node;
18217 }
18218 /* Remember that the name was used in the definition of the
18219 current class so that we can check later to see if the
18220 meaning would have been different after the class was
18221 entirely defined. */
18222 else if (type_decl != error_mark_node
18223 && !parser->scope)
18224 maybe_note_name_used_in_class (identifier, type_decl);
18225
18226 return type_decl;
18227 }
18228
18229 /* Parse an elaborated-type-specifier. Note that the grammar given
18230 here incorporates the resolution to DR68.
18231
18232 elaborated-type-specifier:
18233 class-key :: [opt] nested-name-specifier [opt] identifier
18234 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18235 enum-key :: [opt] nested-name-specifier [opt] identifier
18236 typename :: [opt] nested-name-specifier identifier
18237 typename :: [opt] nested-name-specifier template [opt]
18238 template-id
18239
18240 GNU extension:
18241
18242 elaborated-type-specifier:
18243 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18244 class-key attributes :: [opt] nested-name-specifier [opt]
18245 template [opt] template-id
18246 enum attributes :: [opt] nested-name-specifier [opt] identifier
18247
18248 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18249 declared `friend'. If IS_DECLARATION is TRUE, then this
18250 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18251 something is being declared.
18252
18253 Returns the TYPE specified. */
18254
18255 static tree
18256 cp_parser_elaborated_type_specifier (cp_parser* parser,
18257 bool is_friend,
18258 bool is_declaration)
18259 {
18260 enum tag_types tag_type;
18261 tree identifier;
18262 tree type = NULL_TREE;
18263 tree attributes = NULL_TREE;
18264 tree globalscope;
18265 cp_token *token = NULL;
18266
18267 /* See if we're looking at the `enum' keyword. */
18268 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
18269 {
18270 /* Consume the `enum' token. */
18271 cp_lexer_consume_token (parser->lexer);
18272 /* Remember that it's an enumeration type. */
18273 tag_type = enum_type;
18274 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18275 enums) is used here. */
18276 cp_token *token = cp_lexer_peek_token (parser->lexer);
18277 if (cp_parser_is_keyword (token, RID_CLASS)
18278 || cp_parser_is_keyword (token, RID_STRUCT))
18279 {
18280 gcc_rich_location richloc (token->location);
18281 richloc.add_range (input_location);
18282 richloc.add_fixit_remove ();
18283 pedwarn (&richloc, 0, "elaborated-type-specifier for "
18284 "a scoped enum must not use the %qD keyword",
18285 token->u.value);
18286 /* Consume the `struct' or `class' and parse it anyway. */
18287 cp_lexer_consume_token (parser->lexer);
18288 }
18289 /* Parse the attributes. */
18290 attributes = cp_parser_attributes_opt (parser);
18291 }
18292 /* Or, it might be `typename'. */
18293 else if (cp_lexer_next_token_is_keyword (parser->lexer,
18294 RID_TYPENAME))
18295 {
18296 /* Consume the `typename' token. */
18297 cp_lexer_consume_token (parser->lexer);
18298 /* Remember that it's a `typename' type. */
18299 tag_type = typename_type;
18300 }
18301 /* Otherwise it must be a class-key. */
18302 else
18303 {
18304 tag_type = cp_parser_class_key (parser);
18305 if (tag_type == none_type)
18306 return error_mark_node;
18307 /* Parse the attributes. */
18308 attributes = cp_parser_attributes_opt (parser);
18309 }
18310
18311 /* Look for the `::' operator. */
18312 globalscope = cp_parser_global_scope_opt (parser,
18313 /*current_scope_valid_p=*/false);
18314 /* Look for the nested-name-specifier. */
18315 tree nested_name_specifier;
18316 if (tag_type == typename_type && !globalscope)
18317 {
18318 nested_name_specifier
18319 = cp_parser_nested_name_specifier (parser,
18320 /*typename_keyword_p=*/true,
18321 /*check_dependency_p=*/true,
18322 /*type_p=*/true,
18323 is_declaration);
18324 if (!nested_name_specifier)
18325 return error_mark_node;
18326 }
18327 else
18328 /* Even though `typename' is not present, the proposed resolution
18329 to Core Issue 180 says that in `class A<T>::B', `B' should be
18330 considered a type-name, even if `A<T>' is dependent. */
18331 nested_name_specifier
18332 = cp_parser_nested_name_specifier_opt (parser,
18333 /*typename_keyword_p=*/true,
18334 /*check_dependency_p=*/true,
18335 /*type_p=*/true,
18336 is_declaration);
18337 /* For everything but enumeration types, consider a template-id.
18338 For an enumeration type, consider only a plain identifier. */
18339 if (tag_type != enum_type)
18340 {
18341 bool template_p = false;
18342 tree decl;
18343
18344 /* Allow the `template' keyword. */
18345 template_p = cp_parser_optional_template_keyword (parser);
18346 /* If we didn't see `template', we don't know if there's a
18347 template-id or not. */
18348 if (!template_p)
18349 cp_parser_parse_tentatively (parser);
18350 /* The `template' keyword must follow a nested-name-specifier. */
18351 else if (!nested_name_specifier)
18352 {
18353 cp_parser_error (parser, "%<template%> must follow a nested-"
18354 "name-specifier");
18355 return error_mark_node;
18356 }
18357
18358 /* Parse the template-id. */
18359 token = cp_lexer_peek_token (parser->lexer);
18360 decl = cp_parser_template_id (parser, template_p,
18361 /*check_dependency_p=*/true,
18362 tag_type,
18363 is_declaration);
18364 /* If we didn't find a template-id, look for an ordinary
18365 identifier. */
18366 if (!template_p && !cp_parser_parse_definitely (parser))
18367 ;
18368 /* We can get here when cp_parser_template_id, called by
18369 cp_parser_class_name with tag_type == none_type, succeeds
18370 and caches a BASELINK. Then, when called again here,
18371 instead of failing and returning an error_mark_node
18372 returns it (see template/typename17.C in C++11).
18373 ??? Could we diagnose this earlier? */
18374 else if (tag_type == typename_type && BASELINK_P (decl))
18375 {
18376 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18377 type = error_mark_node;
18378 }
18379 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18380 in effect, then we must assume that, upon instantiation, the
18381 template will correspond to a class. */
18382 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18383 && tag_type == typename_type)
18384 type = make_typename_type (parser->scope, decl,
18385 typename_type,
18386 /*complain=*/tf_error);
18387 /* If the `typename' keyword is in effect and DECL is not a type
18388 decl, then type is non existent. */
18389 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18390 ;
18391 else if (TREE_CODE (decl) == TYPE_DECL)
18392 {
18393 type = check_elaborated_type_specifier (tag_type, decl,
18394 /*allow_template_p=*/true);
18395
18396 /* If the next token is a semicolon, this must be a specialization,
18397 instantiation, or friend declaration. Check the scope while we
18398 still know whether or not we had a nested-name-specifier. */
18399 if (type != error_mark_node
18400 && !nested_name_specifier && !is_friend
18401 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18402 check_unqualified_spec_or_inst (type, token->location);
18403 }
18404 else if (decl == error_mark_node)
18405 type = error_mark_node;
18406 }
18407
18408 if (!type)
18409 {
18410 token = cp_lexer_peek_token (parser->lexer);
18411 identifier = cp_parser_identifier (parser);
18412
18413 if (identifier == error_mark_node)
18414 {
18415 parser->scope = NULL_TREE;
18416 return error_mark_node;
18417 }
18418
18419 /* For a `typename', we needn't call xref_tag. */
18420 if (tag_type == typename_type
18421 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18422 return cp_parser_make_typename_type (parser, identifier,
18423 token->location);
18424
18425 /* Template parameter lists apply only if we are not within a
18426 function parameter list. */
18427 bool template_parm_lists_apply
18428 = parser->num_template_parameter_lists;
18429 if (template_parm_lists_apply)
18430 for (cp_binding_level *s = current_binding_level;
18431 s && s->kind != sk_template_parms;
18432 s = s->level_chain)
18433 if (s->kind == sk_function_parms)
18434 template_parm_lists_apply = false;
18435
18436 /* Look up a qualified name in the usual way. */
18437 if (parser->scope)
18438 {
18439 tree decl;
18440 tree ambiguous_decls;
18441
18442 decl = cp_parser_lookup_name (parser, identifier,
18443 tag_type,
18444 /*is_template=*/false,
18445 /*is_namespace=*/false,
18446 /*check_dependency=*/true,
18447 &ambiguous_decls,
18448 token->location);
18449
18450 /* If the lookup was ambiguous, an error will already have been
18451 issued. */
18452 if (ambiguous_decls)
18453 return error_mark_node;
18454
18455 /* If we are parsing friend declaration, DECL may be a
18456 TEMPLATE_DECL tree node here. However, we need to check
18457 whether this TEMPLATE_DECL results in valid code. Consider
18458 the following example:
18459
18460 namespace N {
18461 template <class T> class C {};
18462 }
18463 class X {
18464 template <class T> friend class N::C; // #1, valid code
18465 };
18466 template <class T> class Y {
18467 friend class N::C; // #2, invalid code
18468 };
18469
18470 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18471 name lookup of `N::C'. We see that friend declaration must
18472 be template for the code to be valid. Note that
18473 processing_template_decl does not work here since it is
18474 always 1 for the above two cases. */
18475
18476 decl = (cp_parser_maybe_treat_template_as_class
18477 (decl, /*tag_name_p=*/is_friend
18478 && template_parm_lists_apply));
18479
18480 if (TREE_CODE (decl) != TYPE_DECL)
18481 {
18482 cp_parser_diagnose_invalid_type_name (parser,
18483 identifier,
18484 token->location);
18485 return error_mark_node;
18486 }
18487
18488 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18489 {
18490 bool allow_template = (template_parm_lists_apply
18491 || DECL_SELF_REFERENCE_P (decl));
18492 type = check_elaborated_type_specifier (tag_type, decl,
18493 allow_template);
18494
18495 if (type == error_mark_node)
18496 return error_mark_node;
18497 }
18498
18499 /* Forward declarations of nested types, such as
18500
18501 class C1::C2;
18502 class C1::C2::C3;
18503
18504 are invalid unless all components preceding the final '::'
18505 are complete. If all enclosing types are complete, these
18506 declarations become merely pointless.
18507
18508 Invalid forward declarations of nested types are errors
18509 caught elsewhere in parsing. Those that are pointless arrive
18510 here. */
18511
18512 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18513 && !is_friend && !processing_explicit_instantiation)
18514 warning (0, "declaration %qD does not declare anything", decl);
18515
18516 type = TREE_TYPE (decl);
18517 }
18518 else
18519 {
18520 /* An elaborated-type-specifier sometimes introduces a new type and
18521 sometimes names an existing type. Normally, the rule is that it
18522 introduces a new type only if there is not an existing type of
18523 the same name already in scope. For example, given:
18524
18525 struct S {};
18526 void f() { struct S s; }
18527
18528 the `struct S' in the body of `f' is the same `struct S' as in
18529 the global scope; the existing definition is used. However, if
18530 there were no global declaration, this would introduce a new
18531 local class named `S'.
18532
18533 An exception to this rule applies to the following code:
18534
18535 namespace N { struct S; }
18536
18537 Here, the elaborated-type-specifier names a new type
18538 unconditionally; even if there is already an `S' in the
18539 containing scope this declaration names a new type.
18540 This exception only applies if the elaborated-type-specifier
18541 forms the complete declaration:
18542
18543 [class.name]
18544
18545 A declaration consisting solely of `class-key identifier ;' is
18546 either a redeclaration of the name in the current scope or a
18547 forward declaration of the identifier as a class name. It
18548 introduces the name into the current scope.
18549
18550 We are in this situation precisely when the next token is a `;'.
18551
18552 An exception to the exception is that a `friend' declaration does
18553 *not* name a new type; i.e., given:
18554
18555 struct S { friend struct T; };
18556
18557 `T' is not a new type in the scope of `S'.
18558
18559 Also, `new struct S' or `sizeof (struct S)' never results in the
18560 definition of a new type; a new type can only be declared in a
18561 declaration context. */
18562
18563 tag_scope ts;
18564 bool template_p;
18565
18566 if (is_friend)
18567 /* Friends have special name lookup rules. */
18568 ts = ts_within_enclosing_non_class;
18569 else if (is_declaration
18570 && cp_lexer_next_token_is (parser->lexer,
18571 CPP_SEMICOLON))
18572 /* This is a `class-key identifier ;' */
18573 ts = ts_current;
18574 else
18575 ts = ts_global;
18576
18577 template_p =
18578 (template_parm_lists_apply
18579 && (cp_parser_next_token_starts_class_definition_p (parser)
18580 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18581 /* An unqualified name was used to reference this type, so
18582 there were no qualifying templates. */
18583 if (template_parm_lists_apply
18584 && !cp_parser_check_template_parameters (parser,
18585 /*num_templates=*/0,
18586 /*template_id*/false,
18587 token->location,
18588 /*declarator=*/NULL))
18589 return error_mark_node;
18590 type = xref_tag (tag_type, identifier, ts, template_p);
18591 }
18592 }
18593
18594 if (type == error_mark_node)
18595 return error_mark_node;
18596
18597 /* Allow attributes on forward declarations of classes. */
18598 if (attributes)
18599 {
18600 if (TREE_CODE (type) == TYPENAME_TYPE)
18601 warning (OPT_Wattributes,
18602 "attributes ignored on uninstantiated type");
18603 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18604 && ! processing_explicit_instantiation)
18605 warning (OPT_Wattributes,
18606 "attributes ignored on template instantiation");
18607 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18608 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18609 else
18610 warning (OPT_Wattributes,
18611 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18612 }
18613
18614 if (tag_type != enum_type)
18615 {
18616 /* Indicate whether this class was declared as a `class' or as a
18617 `struct'. */
18618 if (CLASS_TYPE_P (type))
18619 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18620 cp_parser_check_class_key (tag_type, type);
18621 }
18622
18623 /* A "<" cannot follow an elaborated type specifier. If that
18624 happens, the user was probably trying to form a template-id. */
18625 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18626 token->location);
18627
18628 return type;
18629 }
18630
18631 /* Parse an enum-specifier.
18632
18633 enum-specifier:
18634 enum-head { enumerator-list [opt] }
18635 enum-head { enumerator-list , } [C++0x]
18636
18637 enum-head:
18638 enum-key identifier [opt] enum-base [opt]
18639 enum-key nested-name-specifier identifier enum-base [opt]
18640
18641 enum-key:
18642 enum
18643 enum class [C++0x]
18644 enum struct [C++0x]
18645
18646 enum-base: [C++0x]
18647 : type-specifier-seq
18648
18649 opaque-enum-specifier:
18650 enum-key identifier enum-base [opt] ;
18651
18652 GNU Extensions:
18653 enum-key attributes[opt] identifier [opt] enum-base [opt]
18654 { enumerator-list [opt] }attributes[opt]
18655 enum-key attributes[opt] identifier [opt] enum-base [opt]
18656 { enumerator-list, }attributes[opt] [C++0x]
18657
18658 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18659 if the token stream isn't an enum-specifier after all. */
18660
18661 static tree
18662 cp_parser_enum_specifier (cp_parser* parser)
18663 {
18664 tree identifier;
18665 tree type = NULL_TREE;
18666 tree prev_scope;
18667 tree nested_name_specifier = NULL_TREE;
18668 tree attributes;
18669 bool scoped_enum_p = false;
18670 bool has_underlying_type = false;
18671 bool nested_being_defined = false;
18672 bool new_value_list = false;
18673 bool is_new_type = false;
18674 bool is_unnamed = false;
18675 tree underlying_type = NULL_TREE;
18676 cp_token *type_start_token = NULL;
18677 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18678
18679 parser->colon_corrects_to_scope_p = false;
18680
18681 /* Parse tentatively so that we can back up if we don't find a
18682 enum-specifier. */
18683 cp_parser_parse_tentatively (parser);
18684
18685 /* Caller guarantees that the current token is 'enum', an identifier
18686 possibly follows, and the token after that is an opening brace.
18687 If we don't have an identifier, fabricate an anonymous name for
18688 the enumeration being defined. */
18689 cp_lexer_consume_token (parser->lexer);
18690
18691 /* Parse the "class" or "struct", which indicates a scoped
18692 enumeration type in C++0x. */
18693 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18694 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18695 {
18696 if (cxx_dialect < cxx11)
18697 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18698
18699 /* Consume the `struct' or `class' token. */
18700 cp_lexer_consume_token (parser->lexer);
18701
18702 scoped_enum_p = true;
18703 }
18704
18705 attributes = cp_parser_attributes_opt (parser);
18706
18707 /* Clear the qualification. */
18708 parser->scope = NULL_TREE;
18709 parser->qualifying_scope = NULL_TREE;
18710 parser->object_scope = NULL_TREE;
18711
18712 /* Figure out in what scope the declaration is being placed. */
18713 prev_scope = current_scope ();
18714
18715 type_start_token = cp_lexer_peek_token (parser->lexer);
18716
18717 push_deferring_access_checks (dk_no_check);
18718 nested_name_specifier
18719 = cp_parser_nested_name_specifier_opt (parser,
18720 /*typename_keyword_p=*/true,
18721 /*check_dependency_p=*/false,
18722 /*type_p=*/false,
18723 /*is_declaration=*/false);
18724
18725 if (nested_name_specifier)
18726 {
18727 tree name;
18728
18729 identifier = cp_parser_identifier (parser);
18730 name = cp_parser_lookup_name (parser, identifier,
18731 enum_type,
18732 /*is_template=*/false,
18733 /*is_namespace=*/false,
18734 /*check_dependency=*/true,
18735 /*ambiguous_decls=*/NULL,
18736 input_location);
18737 if (name && name != error_mark_node)
18738 {
18739 type = TREE_TYPE (name);
18740 if (TREE_CODE (type) == TYPENAME_TYPE)
18741 {
18742 /* Are template enums allowed in ISO? */
18743 if (template_parm_scope_p ())
18744 pedwarn (type_start_token->location, OPT_Wpedantic,
18745 "%qD is an enumeration template", name);
18746 /* ignore a typename reference, for it will be solved by name
18747 in start_enum. */
18748 type = NULL_TREE;
18749 }
18750 }
18751 else if (nested_name_specifier == error_mark_node)
18752 /* We already issued an error. */;
18753 else
18754 {
18755 error_at (type_start_token->location,
18756 "%qD does not name an enumeration in %qT",
18757 identifier, nested_name_specifier);
18758 nested_name_specifier = error_mark_node;
18759 }
18760 }
18761 else
18762 {
18763 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18764 identifier = cp_parser_identifier (parser);
18765 else
18766 {
18767 identifier = make_anon_name ();
18768 is_unnamed = true;
18769 if (scoped_enum_p)
18770 error_at (type_start_token->location,
18771 "unnamed scoped enum is not allowed");
18772 }
18773 }
18774 pop_deferring_access_checks ();
18775
18776 /* Check for the `:' that denotes a specified underlying type in C++0x.
18777 Note that a ':' could also indicate a bitfield width, however. */
18778 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18779 {
18780 cp_decl_specifier_seq type_specifiers;
18781
18782 /* Consume the `:'. */
18783 cp_lexer_consume_token (parser->lexer);
18784
18785 /* Parse the type-specifier-seq. */
18786 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
18787 /*is_declaration=*/false,
18788 /*is_trailing_return=*/false,
18789 &type_specifiers);
18790
18791 /* At this point this is surely not elaborated type specifier. */
18792 if (!cp_parser_parse_definitely (parser))
18793 return NULL_TREE;
18794
18795 if (cxx_dialect < cxx11)
18796 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18797
18798 has_underlying_type = true;
18799
18800 /* If that didn't work, stop. */
18801 if (type_specifiers.type != error_mark_node)
18802 {
18803 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18804 /*initialized=*/0, NULL);
18805 if (underlying_type == error_mark_node
18806 || check_for_bare_parameter_packs (underlying_type))
18807 underlying_type = NULL_TREE;
18808 }
18809 }
18810
18811 /* Look for the `{' but don't consume it yet. */
18812 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18813 {
18814 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18815 {
18816 cp_parser_error (parser, "expected %<{%>");
18817 if (has_underlying_type)
18818 {
18819 type = NULL_TREE;
18820 goto out;
18821 }
18822 }
18823 /* An opaque-enum-specifier must have a ';' here. */
18824 if ((scoped_enum_p || underlying_type)
18825 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18826 {
18827 cp_parser_error (parser, "expected %<;%> or %<{%>");
18828 if (has_underlying_type)
18829 {
18830 type = NULL_TREE;
18831 goto out;
18832 }
18833 }
18834 }
18835
18836 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18837 return NULL_TREE;
18838
18839 if (nested_name_specifier)
18840 {
18841 if (CLASS_TYPE_P (nested_name_specifier))
18842 {
18843 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18844 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18845 push_scope (nested_name_specifier);
18846 }
18847 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18848 {
18849 push_nested_namespace (nested_name_specifier);
18850 }
18851 }
18852
18853 /* Issue an error message if type-definitions are forbidden here. */
18854 if (!cp_parser_check_type_definition (parser))
18855 type = error_mark_node;
18856 else
18857 /* Create the new type. We do this before consuming the opening
18858 brace so the enum will be recorded as being on the line of its
18859 tag (or the 'enum' keyword, if there is no tag). */
18860 type = start_enum (identifier, type, underlying_type,
18861 attributes, scoped_enum_p, &is_new_type);
18862
18863 /* If the next token is not '{' it is an opaque-enum-specifier or an
18864 elaborated-type-specifier. */
18865 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18866 {
18867 timevar_push (TV_PARSE_ENUM);
18868 if (nested_name_specifier
18869 && nested_name_specifier != error_mark_node)
18870 {
18871 /* The following catches invalid code such as:
18872 enum class S<int>::E { A, B, C }; */
18873 if (!processing_specialization
18874 && CLASS_TYPE_P (nested_name_specifier)
18875 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18876 error_at (type_start_token->location, "cannot add an enumerator "
18877 "list to a template instantiation");
18878
18879 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18880 {
18881 error_at (type_start_token->location,
18882 "%<%T::%E%> has not been declared",
18883 TYPE_CONTEXT (nested_name_specifier),
18884 nested_name_specifier);
18885 type = error_mark_node;
18886 }
18887 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18888 && !CLASS_TYPE_P (nested_name_specifier))
18889 {
18890 error_at (type_start_token->location, "nested name specifier "
18891 "%qT for enum declaration does not name a class "
18892 "or namespace", nested_name_specifier);
18893 type = error_mark_node;
18894 }
18895 /* If that scope does not contain the scope in which the
18896 class was originally declared, the program is invalid. */
18897 else if (prev_scope && !is_ancestor (prev_scope,
18898 nested_name_specifier))
18899 {
18900 if (at_namespace_scope_p ())
18901 error_at (type_start_token->location,
18902 "declaration of %qD in namespace %qD which does not "
18903 "enclose %qD",
18904 type, prev_scope, nested_name_specifier);
18905 else
18906 error_at (type_start_token->location,
18907 "declaration of %qD in %qD which does not "
18908 "enclose %qD",
18909 type, prev_scope, nested_name_specifier);
18910 type = error_mark_node;
18911 }
18912 /* If that scope is the scope where the declaration is being placed
18913 the program is invalid. */
18914 else if (CLASS_TYPE_P (nested_name_specifier)
18915 && CLASS_TYPE_P (prev_scope)
18916 && same_type_p (nested_name_specifier, prev_scope))
18917 {
18918 permerror (type_start_token->location,
18919 "extra qualification not allowed");
18920 nested_name_specifier = NULL_TREE;
18921 }
18922 }
18923
18924 if (scoped_enum_p)
18925 begin_scope (sk_scoped_enum, type);
18926
18927 /* Consume the opening brace. */
18928 matching_braces braces;
18929 braces.consume_open (parser);
18930
18931 if (type == error_mark_node)
18932 ; /* Nothing to add */
18933 else if (OPAQUE_ENUM_P (type)
18934 || (cxx_dialect > cxx98 && processing_specialization))
18935 {
18936 new_value_list = true;
18937 SET_OPAQUE_ENUM_P (type, false);
18938 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18939 }
18940 else
18941 {
18942 error_at (type_start_token->location,
18943 "multiple definition of %q#T", type);
18944 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18945 "previous definition here");
18946 type = error_mark_node;
18947 }
18948
18949 if (type == error_mark_node)
18950 cp_parser_skip_to_end_of_block_or_statement (parser);
18951 /* If the next token is not '}', then there are some enumerators. */
18952 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18953 {
18954 if (is_unnamed && !scoped_enum_p)
18955 pedwarn (type_start_token->location, OPT_Wpedantic,
18956 "ISO C++ forbids empty unnamed enum");
18957 }
18958 else
18959 cp_parser_enumerator_list (parser, type);
18960
18961 /* Consume the final '}'. */
18962 braces.require_close (parser);
18963
18964 if (scoped_enum_p)
18965 finish_scope ();
18966 timevar_pop (TV_PARSE_ENUM);
18967 }
18968 else
18969 {
18970 /* If a ';' follows, then it is an opaque-enum-specifier
18971 and additional restrictions apply. */
18972 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18973 {
18974 if (is_unnamed)
18975 error_at (type_start_token->location,
18976 "opaque-enum-specifier without name");
18977 else if (nested_name_specifier)
18978 error_at (type_start_token->location,
18979 "opaque-enum-specifier must use a simple identifier");
18980 }
18981 }
18982
18983 /* Look for trailing attributes to apply to this enumeration, and
18984 apply them if appropriate. */
18985 if (cp_parser_allow_gnu_extensions_p (parser))
18986 {
18987 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18988 cplus_decl_attributes (&type,
18989 trailing_attr,
18990 (int) ATTR_FLAG_TYPE_IN_PLACE);
18991 }
18992
18993 /* Finish up the enumeration. */
18994 if (type != error_mark_node)
18995 {
18996 if (new_value_list)
18997 finish_enum_value_list (type);
18998 if (is_new_type)
18999 finish_enum (type);
19000 }
19001
19002 if (nested_name_specifier)
19003 {
19004 if (CLASS_TYPE_P (nested_name_specifier))
19005 {
19006 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
19007 pop_scope (nested_name_specifier);
19008 }
19009 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
19010 {
19011 pop_nested_namespace (nested_name_specifier);
19012 }
19013 }
19014 out:
19015 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19016 return type;
19017 }
19018
19019 /* Parse an enumerator-list. The enumerators all have the indicated
19020 TYPE.
19021
19022 enumerator-list:
19023 enumerator-definition
19024 enumerator-list , enumerator-definition */
19025
19026 static void
19027 cp_parser_enumerator_list (cp_parser* parser, tree type)
19028 {
19029 while (true)
19030 {
19031 /* Parse an enumerator-definition. */
19032 cp_parser_enumerator_definition (parser, type);
19033
19034 /* If the next token is not a ',', we've reached the end of
19035 the list. */
19036 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19037 break;
19038 /* Otherwise, consume the `,' and keep going. */
19039 cp_lexer_consume_token (parser->lexer);
19040 /* If the next token is a `}', there is a trailing comma. */
19041 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19042 {
19043 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
19044 pedwarn (input_location, OPT_Wpedantic,
19045 "comma at end of enumerator list");
19046 break;
19047 }
19048 }
19049 }
19050
19051 /* Parse an enumerator-definition. The enumerator has the indicated
19052 TYPE.
19053
19054 enumerator-definition:
19055 enumerator
19056 enumerator = constant-expression
19057
19058 enumerator:
19059 identifier
19060
19061 GNU Extensions:
19062
19063 enumerator-definition:
19064 enumerator attributes [opt]
19065 enumerator attributes [opt] = constant-expression */
19066
19067 static void
19068 cp_parser_enumerator_definition (cp_parser* parser, tree type)
19069 {
19070 tree identifier;
19071 tree value;
19072 location_t loc;
19073
19074 /* Save the input location because we are interested in the location
19075 of the identifier and not the location of the explicit value. */
19076 loc = cp_lexer_peek_token (parser->lexer)->location;
19077
19078 /* Look for the identifier. */
19079 identifier = cp_parser_identifier (parser);
19080 if (identifier == error_mark_node)
19081 return;
19082
19083 /* Parse any specified attributes. */
19084 tree attrs = cp_parser_attributes_opt (parser);
19085
19086 /* If the next token is an '=', then there is an explicit value. */
19087 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19088 {
19089 /* Consume the `=' token. */
19090 cp_lexer_consume_token (parser->lexer);
19091 /* Parse the value. */
19092 value = cp_parser_constant_expression (parser);
19093 }
19094 else
19095 value = NULL_TREE;
19096
19097 /* If we are processing a template, make sure the initializer of the
19098 enumerator doesn't contain any bare template parameter pack. */
19099 if (check_for_bare_parameter_packs (value))
19100 value = error_mark_node;
19101
19102 /* Create the enumerator. */
19103 build_enumerator (identifier, value, type, attrs, loc);
19104 }
19105
19106 /* Parse a namespace-name.
19107
19108 namespace-name:
19109 original-namespace-name
19110 namespace-alias
19111
19112 Returns the NAMESPACE_DECL for the namespace. */
19113
19114 static tree
19115 cp_parser_namespace_name (cp_parser* parser)
19116 {
19117 tree identifier;
19118 tree namespace_decl;
19119
19120 cp_token *token = cp_lexer_peek_token (parser->lexer);
19121
19122 /* Get the name of the namespace. */
19123 identifier = cp_parser_identifier (parser);
19124 if (identifier == error_mark_node)
19125 return error_mark_node;
19126
19127 /* Look up the identifier in the currently active scope. Look only
19128 for namespaces, due to:
19129
19130 [basic.lookup.udir]
19131
19132 When looking up a namespace-name in a using-directive or alias
19133 definition, only namespace names are considered.
19134
19135 And:
19136
19137 [basic.lookup.qual]
19138
19139 During the lookup of a name preceding the :: scope resolution
19140 operator, object, function, and enumerator names are ignored.
19141
19142 (Note that cp_parser_qualifying_entity only calls this
19143 function if the token after the name is the scope resolution
19144 operator.) */
19145 namespace_decl = cp_parser_lookup_name (parser, identifier,
19146 none_type,
19147 /*is_template=*/false,
19148 /*is_namespace=*/true,
19149 /*check_dependency=*/true,
19150 /*ambiguous_decls=*/NULL,
19151 token->location);
19152 /* If it's not a namespace, issue an error. */
19153 if (namespace_decl == error_mark_node
19154 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
19155 {
19156 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19157 {
19158 auto_diagnostic_group d;
19159 name_hint hint;
19160 if (namespace_decl == error_mark_node
19161 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
19162 hint = suggest_alternative_in_explicit_scope (token->location,
19163 identifier,
19164 parser->scope);
19165 if (const char *suggestion = hint.suggestion ())
19166 {
19167 gcc_rich_location richloc (token->location);
19168 richloc.add_fixit_replace (suggestion);
19169 error_at (&richloc,
19170 "%qD is not a namespace-name; did you mean %qs?",
19171 identifier, suggestion);
19172 }
19173 else
19174 error_at (token->location, "%qD is not a namespace-name",
19175 identifier);
19176 }
19177 else
19178 cp_parser_error (parser, "expected namespace-name");
19179 namespace_decl = error_mark_node;
19180 }
19181
19182 return namespace_decl;
19183 }
19184
19185 /* Parse a namespace-definition.
19186
19187 namespace-definition:
19188 named-namespace-definition
19189 unnamed-namespace-definition
19190
19191 named-namespace-definition:
19192 original-namespace-definition
19193 extension-namespace-definition
19194
19195 original-namespace-definition:
19196 namespace identifier { namespace-body }
19197
19198 extension-namespace-definition:
19199 namespace original-namespace-name { namespace-body }
19200
19201 unnamed-namespace-definition:
19202 namespace { namespace-body } */
19203
19204 static void
19205 cp_parser_namespace_definition (cp_parser* parser)
19206 {
19207 tree identifier;
19208 int nested_definition_count = 0;
19209
19210 cp_ensure_no_omp_declare_simd (parser);
19211 cp_ensure_no_oacc_routine (parser);
19212
19213 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
19214 const bool topmost_inline_p = is_inline;
19215
19216 if (is_inline)
19217 {
19218 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
19219 cp_lexer_consume_token (parser->lexer);
19220 }
19221
19222 /* Look for the `namespace' keyword. */
19223 cp_token* token
19224 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19225
19226 /* Parse any specified attributes before the identifier. */
19227 tree attribs = cp_parser_attributes_opt (parser);
19228
19229 for (;;)
19230 {
19231 identifier = NULL_TREE;
19232
19233 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
19234 RID_INLINE);
19235 if (nested_inline_p && nested_definition_count != 0)
19236 {
19237 if (cxx_dialect < cxx2a)
19238 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
19239 OPT_Wpedantic, "nested inline namespace definitions only "
19240 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
19241 cp_lexer_consume_token (parser->lexer);
19242 }
19243
19244 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19245 {
19246 identifier = cp_parser_identifier (parser);
19247
19248 if (cp_next_tokens_can_be_std_attribute_p (parser))
19249 pedwarn (input_location, OPT_Wpedantic,
19250 "standard attributes on namespaces must precede "
19251 "the namespace name");
19252
19253 /* Parse any attributes specified after the identifier. */
19254 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
19255 }
19256
19257 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19258 {
19259 /* Don't forget that the innermost namespace might have been
19260 marked as inline. Use |= because we cannot overwrite
19261 IS_INLINE in case the outermost namespace is inline, but
19262 there are no nested inlines. */
19263 is_inline |= nested_inline_p;
19264 break;
19265 }
19266
19267 if (!nested_definition_count && cxx_dialect < cxx17)
19268 pedwarn (input_location, OPT_Wpedantic,
19269 "nested namespace definitions only available with "
19270 "%<-std=c++17%> or %<-std=gnu++17%>");
19271
19272 /* Nested namespace names can create new namespaces (unlike
19273 other qualified-ids). */
19274 if (int count = (identifier
19275 ? push_namespace (identifier, nested_inline_p)
19276 : 0))
19277 nested_definition_count += count;
19278 else
19279 cp_parser_error (parser, "nested namespace name required");
19280 cp_lexer_consume_token (parser->lexer);
19281 }
19282
19283 if (nested_definition_count && !identifier)
19284 cp_parser_error (parser, "namespace name required");
19285
19286 if (nested_definition_count && attribs)
19287 error_at (token->location,
19288 "a nested namespace definition cannot have attributes");
19289 if (nested_definition_count && topmost_inline_p)
19290 error_at (token->location,
19291 "a nested namespace definition cannot be inline");
19292
19293 /* Start the namespace. */
19294 nested_definition_count += push_namespace (identifier, is_inline);
19295
19296 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
19297
19298 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
19299
19300 /* Look for the `{' to validate starting the namespace. */
19301 matching_braces braces;
19302 if (braces.require_open (parser))
19303 {
19304 /* Parse the body of the namespace. */
19305 cp_parser_namespace_body (parser);
19306
19307 /* Look for the final `}'. */
19308 braces.require_close (parser);
19309 }
19310
19311 if (has_visibility)
19312 pop_visibility (1);
19313
19314 /* Pop the nested namespace definitions. */
19315 while (nested_definition_count--)
19316 pop_namespace ();
19317 }
19318
19319 /* Parse a namespace-body.
19320
19321 namespace-body:
19322 declaration-seq [opt] */
19323
19324 static void
19325 cp_parser_namespace_body (cp_parser* parser)
19326 {
19327 cp_parser_declaration_seq_opt (parser);
19328 }
19329
19330 /* Parse a namespace-alias-definition.
19331
19332 namespace-alias-definition:
19333 namespace identifier = qualified-namespace-specifier ; */
19334
19335 static void
19336 cp_parser_namespace_alias_definition (cp_parser* parser)
19337 {
19338 tree identifier;
19339 tree namespace_specifier;
19340
19341 cp_token *token = cp_lexer_peek_token (parser->lexer);
19342
19343 /* Look for the `namespace' keyword. */
19344 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19345 /* Look for the identifier. */
19346 identifier = cp_parser_identifier (parser);
19347 if (identifier == error_mark_node)
19348 return;
19349 /* Look for the `=' token. */
19350 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
19351 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19352 {
19353 error_at (token->location, "%<namespace%> definition is not allowed here");
19354 /* Skip the definition. */
19355 cp_lexer_consume_token (parser->lexer);
19356 if (cp_parser_skip_to_closing_brace (parser))
19357 cp_lexer_consume_token (parser->lexer);
19358 return;
19359 }
19360 cp_parser_require (parser, CPP_EQ, RT_EQ);
19361 /* Look for the qualified-namespace-specifier. */
19362 namespace_specifier
19363 = cp_parser_qualified_namespace_specifier (parser);
19364 /* Look for the `;' token. */
19365 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19366
19367 /* Register the alias in the symbol table. */
19368 do_namespace_alias (identifier, namespace_specifier);
19369 }
19370
19371 /* Parse a qualified-namespace-specifier.
19372
19373 qualified-namespace-specifier:
19374 :: [opt] nested-name-specifier [opt] namespace-name
19375
19376 Returns a NAMESPACE_DECL corresponding to the specified
19377 namespace. */
19378
19379 static tree
19380 cp_parser_qualified_namespace_specifier (cp_parser* parser)
19381 {
19382 /* Look for the optional `::'. */
19383 cp_parser_global_scope_opt (parser,
19384 /*current_scope_valid_p=*/false);
19385
19386 /* Look for the optional nested-name-specifier. */
19387 cp_parser_nested_name_specifier_opt (parser,
19388 /*typename_keyword_p=*/false,
19389 /*check_dependency_p=*/true,
19390 /*type_p=*/false,
19391 /*is_declaration=*/true);
19392
19393 return cp_parser_namespace_name (parser);
19394 }
19395
19396 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19397 access declaration.
19398
19399 using-declaration:
19400 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19401 using :: unqualified-id ;
19402
19403 access-declaration:
19404 qualified-id ;
19405
19406 */
19407
19408 static bool
19409 cp_parser_using_declaration (cp_parser* parser,
19410 bool access_declaration_p)
19411 {
19412 cp_token *token;
19413 bool typename_p = false;
19414 bool global_scope_p;
19415 tree decl;
19416 tree identifier;
19417 tree qscope;
19418 int oldcount = errorcount;
19419 cp_token *diag_token = NULL;
19420
19421 if (access_declaration_p)
19422 {
19423 diag_token = cp_lexer_peek_token (parser->lexer);
19424 cp_parser_parse_tentatively (parser);
19425 }
19426 else
19427 {
19428 /* Look for the `using' keyword. */
19429 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19430
19431 again:
19432 /* Peek at the next token. */
19433 token = cp_lexer_peek_token (parser->lexer);
19434 /* See if it's `typename'. */
19435 if (token->keyword == RID_TYPENAME)
19436 {
19437 /* Remember that we've seen it. */
19438 typename_p = true;
19439 /* Consume the `typename' token. */
19440 cp_lexer_consume_token (parser->lexer);
19441 }
19442 }
19443
19444 /* Look for the optional global scope qualification. */
19445 global_scope_p
19446 = (cp_parser_global_scope_opt (parser,
19447 /*current_scope_valid_p=*/false)
19448 != NULL_TREE);
19449
19450 /* If we saw `typename', or didn't see `::', then there must be a
19451 nested-name-specifier present. */
19452 if (typename_p || !global_scope_p)
19453 {
19454 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19455 /*check_dependency_p=*/true,
19456 /*type_p=*/false,
19457 /*is_declaration=*/true);
19458 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19459 {
19460 cp_parser_skip_to_end_of_block_or_statement (parser);
19461 return false;
19462 }
19463 }
19464 /* Otherwise, we could be in either of the two productions. In that
19465 case, treat the nested-name-specifier as optional. */
19466 else
19467 qscope = cp_parser_nested_name_specifier_opt (parser,
19468 /*typename_keyword_p=*/false,
19469 /*check_dependency_p=*/true,
19470 /*type_p=*/false,
19471 /*is_declaration=*/true);
19472 if (!qscope)
19473 qscope = global_namespace;
19474 else if (UNSCOPED_ENUM_P (qscope)
19475 && !TYPE_FUNCTION_SCOPE_P (qscope))
19476 qscope = CP_TYPE_CONTEXT (qscope);
19477
19478 if (access_declaration_p && cp_parser_error_occurred (parser))
19479 /* Something has already gone wrong; there's no need to parse
19480 further. Since an error has occurred, the return value of
19481 cp_parser_parse_definitely will be false, as required. */
19482 return cp_parser_parse_definitely (parser);
19483
19484 token = cp_lexer_peek_token (parser->lexer);
19485 /* Parse the unqualified-id. */
19486 identifier = cp_parser_unqualified_id (parser,
19487 /*template_keyword_p=*/false,
19488 /*check_dependency_p=*/true,
19489 /*declarator_p=*/true,
19490 /*optional_p=*/false);
19491
19492 if (access_declaration_p)
19493 {
19494 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19495 cp_parser_simulate_error (parser);
19496 if (!cp_parser_parse_definitely (parser))
19497 return false;
19498 }
19499 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19500 {
19501 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19502 if (cxx_dialect < cxx17
19503 && !in_system_header_at (ell->location))
19504 pedwarn (ell->location, 0,
19505 "pack expansion in using-declaration only available "
19506 "with %<-std=c++17%> or %<-std=gnu++17%>");
19507 qscope = make_pack_expansion (qscope);
19508 }
19509
19510 /* The function we call to handle a using-declaration is different
19511 depending on what scope we are in. */
19512 if (qscope == error_mark_node || identifier == error_mark_node)
19513 ;
19514 else if (!identifier_p (identifier)
19515 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19516 /* [namespace.udecl]
19517
19518 A using declaration shall not name a template-id. */
19519 error_at (token->location,
19520 "a template-id may not appear in a using-declaration");
19521 else
19522 {
19523 if (at_class_scope_p ())
19524 {
19525 /* Create the USING_DECL. */
19526 decl = do_class_using_decl (qscope, identifier);
19527
19528 if (decl && typename_p)
19529 USING_DECL_TYPENAME_P (decl) = 1;
19530
19531 if (check_for_bare_parameter_packs (decl))
19532 {
19533 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19534 return false;
19535 }
19536 else
19537 /* Add it to the list of members in this class. */
19538 finish_member_declaration (decl);
19539 }
19540 else
19541 {
19542 decl = cp_parser_lookup_name_simple (parser,
19543 identifier,
19544 token->location);
19545 if (decl == error_mark_node)
19546 cp_parser_name_lookup_error (parser, identifier,
19547 decl, NLE_NULL,
19548 token->location);
19549 else if (check_for_bare_parameter_packs (decl))
19550 {
19551 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19552 return false;
19553 }
19554 else if (!at_namespace_scope_p ())
19555 finish_local_using_decl (decl, qscope, identifier);
19556 else
19557 finish_namespace_using_decl (decl, qscope, identifier);
19558 }
19559 }
19560
19561 if (!access_declaration_p
19562 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19563 {
19564 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19565 if (cxx_dialect < cxx17)
19566 pedwarn (comma->location, 0,
19567 "comma-separated list in using-declaration only available "
19568 "with %<-std=c++17%> or %<-std=gnu++17%>");
19569 goto again;
19570 }
19571
19572 /* Look for the final `;'. */
19573 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19574
19575 if (access_declaration_p && errorcount == oldcount)
19576 warning_at (diag_token->location, OPT_Wdeprecated,
19577 "access declarations are deprecated "
19578 "in favour of using-declarations; "
19579 "suggestion: add the %<using%> keyword");
19580
19581 return true;
19582 }
19583
19584 /* Parse an alias-declaration.
19585
19586 alias-declaration:
19587 using identifier attribute-specifier-seq [opt] = type-id */
19588
19589 static tree
19590 cp_parser_alias_declaration (cp_parser* parser)
19591 {
19592 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19593 location_t id_location, type_location;
19594 cp_declarator *declarator;
19595 cp_decl_specifier_seq decl_specs;
19596 bool member_p;
19597 const char *saved_message = NULL;
19598
19599 /* Look for the `using' keyword. */
19600 cp_token *using_token
19601 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19602 if (using_token == NULL)
19603 return error_mark_node;
19604
19605 id_location = cp_lexer_peek_token (parser->lexer)->location;
19606 id = cp_parser_identifier (parser);
19607 if (id == error_mark_node)
19608 return error_mark_node;
19609
19610 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19611 attributes = cp_parser_attributes_opt (parser);
19612 if (attributes == error_mark_node)
19613 return error_mark_node;
19614
19615 cp_parser_require (parser, CPP_EQ, RT_EQ);
19616
19617 if (cp_parser_error_occurred (parser))
19618 return error_mark_node;
19619
19620 cp_parser_commit_to_tentative_parse (parser);
19621
19622 /* Now we are going to parse the type-id of the declaration. */
19623
19624 /*
19625 [dcl.type]/3 says:
19626
19627 "A type-specifier-seq shall not define a class or enumeration
19628 unless it appears in the type-id of an alias-declaration (7.1.3) that
19629 is not the declaration of a template-declaration."
19630
19631 In other words, if we currently are in an alias template, the
19632 type-id should not define a type.
19633
19634 So let's set parser->type_definition_forbidden_message in that
19635 case; cp_parser_check_type_definition (called by
19636 cp_parser_class_specifier) will then emit an error if a type is
19637 defined in the type-id. */
19638 if (parser->num_template_parameter_lists)
19639 {
19640 saved_message = parser->type_definition_forbidden_message;
19641 parser->type_definition_forbidden_message =
19642 G_("types may not be defined in alias template declarations");
19643 }
19644
19645 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
19646 &type_location);
19647
19648 /* Restore the error message if need be. */
19649 if (parser->num_template_parameter_lists)
19650 parser->type_definition_forbidden_message = saved_message;
19651
19652 if (type == error_mark_node
19653 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19654 {
19655 cp_parser_skip_to_end_of_block_or_statement (parser);
19656 return error_mark_node;
19657 }
19658
19659 /* A typedef-name can also be introduced by an alias-declaration. The
19660 identifier following the using keyword becomes a typedef-name. It has
19661 the same semantics as if it were introduced by the typedef
19662 specifier. In particular, it does not define a new type and it shall
19663 not appear in the type-id. */
19664
19665 clear_decl_specs (&decl_specs);
19666 decl_specs.type = type;
19667 if (attributes != NULL_TREE)
19668 {
19669 decl_specs.attributes = attributes;
19670 set_and_check_decl_spec_loc (&decl_specs,
19671 ds_attribute,
19672 attrs_token);
19673 }
19674 set_and_check_decl_spec_loc (&decl_specs,
19675 ds_typedef,
19676 using_token);
19677 set_and_check_decl_spec_loc (&decl_specs,
19678 ds_alias,
19679 using_token);
19680 decl_specs.locations[ds_type_spec] = type_location;
19681
19682 if (parser->num_template_parameter_lists
19683 && !cp_parser_check_template_parameters (parser,
19684 /*num_templates=*/0,
19685 /*template_id*/false,
19686 id_location,
19687 /*declarator=*/NULL))
19688 return error_mark_node;
19689
19690 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
19691
19692 member_p = at_class_scope_p ();
19693 if (member_p)
19694 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19695 NULL_TREE, attributes);
19696 else
19697 decl = start_decl (declarator, &decl_specs, 0,
19698 attributes, NULL_TREE, &pushed_scope);
19699 if (decl == error_mark_node)
19700 return decl;
19701
19702 // Attach constraints to the alias declaration.
19703 if (flag_concepts && current_template_parms)
19704 {
19705 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19706 tree constr = build_constraints (reqs, NULL_TREE);
19707 set_constraints (decl, constr);
19708 }
19709
19710 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19711
19712 if (pushed_scope)
19713 pop_scope (pushed_scope);
19714
19715 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19716 added into the symbol table; otherwise, return the TYPE_DECL. */
19717 if (DECL_LANG_SPECIFIC (decl)
19718 && DECL_TEMPLATE_INFO (decl)
19719 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19720 {
19721 decl = DECL_TI_TEMPLATE (decl);
19722 if (member_p)
19723 check_member_template (decl);
19724 }
19725
19726 return decl;
19727 }
19728
19729 /* Parse a using-directive.
19730
19731 using-directive:
19732 using namespace :: [opt] nested-name-specifier [opt]
19733 namespace-name ; */
19734
19735 static void
19736 cp_parser_using_directive (cp_parser* parser)
19737 {
19738 tree namespace_decl;
19739 tree attribs;
19740
19741 /* Look for the `using' keyword. */
19742 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19743 /* And the `namespace' keyword. */
19744 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19745 /* Look for the optional `::' operator. */
19746 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19747 /* And the optional nested-name-specifier. */
19748 cp_parser_nested_name_specifier_opt (parser,
19749 /*typename_keyword_p=*/false,
19750 /*check_dependency_p=*/true,
19751 /*type_p=*/false,
19752 /*is_declaration=*/true);
19753 /* Get the namespace being used. */
19754 namespace_decl = cp_parser_namespace_name (parser);
19755 /* And any specified attributes. */
19756 attribs = cp_parser_attributes_opt (parser);
19757
19758 /* Update the symbol table. */
19759 if (namespace_bindings_p ())
19760 finish_namespace_using_directive (namespace_decl, attribs);
19761 else
19762 finish_local_using_directive (namespace_decl, attribs);
19763
19764 /* Look for the final `;'. */
19765 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19766 }
19767
19768 /* Parse an asm-definition.
19769
19770 asm-qualifier:
19771 volatile
19772 inline
19773 goto
19774
19775 asm-qualifier-list:
19776 asm-qualifier
19777 asm-qualifier-list asm-qualifier
19778
19779 asm-definition:
19780 asm ( string-literal ) ;
19781
19782 GNU Extension:
19783
19784 asm-definition:
19785 asm asm-qualifier-list [opt] ( string-literal ) ;
19786 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19787 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19788 : asm-operand-list [opt] ) ;
19789 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19790 : asm-operand-list [opt]
19791 : asm-clobber-list [opt] ) ;
19792 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19793 : asm-clobber-list [opt]
19794 : asm-goto-list ) ;
19795
19796 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19797 contains goto, and is the only allowed form in that case. No duplicates are
19798 allowed in an asm-qualifier-list. */
19799
19800 static void
19801 cp_parser_asm_definition (cp_parser* parser)
19802 {
19803 tree string;
19804 tree outputs = NULL_TREE;
19805 tree inputs = NULL_TREE;
19806 tree clobbers = NULL_TREE;
19807 tree labels = NULL_TREE;
19808 tree asm_stmt;
19809 bool extended_p = false;
19810 bool invalid_inputs_p = false;
19811 bool invalid_outputs_p = false;
19812 required_token missing = RT_NONE;
19813
19814 /* Look for the `asm' keyword. */
19815 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19816
19817 if (parser->in_function_body
19818 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19819 {
19820 error ("%<asm%> in %<constexpr%> function");
19821 cp_function_chain->invalid_constexpr = true;
19822 }
19823
19824 /* Handle the asm-qualifier-list. */
19825 location_t volatile_loc = UNKNOWN_LOCATION;
19826 location_t inline_loc = UNKNOWN_LOCATION;
19827 location_t goto_loc = UNKNOWN_LOCATION;
19828 location_t first_loc = UNKNOWN_LOCATION;
19829
19830 if (cp_parser_allow_gnu_extensions_p (parser))
19831 for (;;)
19832 {
19833 cp_token *token = cp_lexer_peek_token (parser->lexer);
19834 location_t loc = token->location;
19835 switch (cp_lexer_peek_token (parser->lexer)->keyword)
19836 {
19837 case RID_VOLATILE:
19838 if (volatile_loc)
19839 {
19840 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19841 inform (volatile_loc, "first seen here");
19842 }
19843 else
19844 {
19845 if (!parser->in_function_body)
19846 warning_at (loc, 0, "asm qualifier %qT ignored outside of "
19847 "function body", token->u.value);
19848 volatile_loc = loc;
19849 }
19850 cp_lexer_consume_token (parser->lexer);
19851 continue;
19852
19853 case RID_INLINE:
19854 if (inline_loc)
19855 {
19856 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19857 inform (inline_loc, "first seen here");
19858 }
19859 else
19860 inline_loc = loc;
19861 if (!first_loc)
19862 first_loc = loc;
19863 cp_lexer_consume_token (parser->lexer);
19864 continue;
19865
19866 case RID_GOTO:
19867 if (goto_loc)
19868 {
19869 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19870 inform (goto_loc, "first seen here");
19871 }
19872 else
19873 goto_loc = loc;
19874 if (!first_loc)
19875 first_loc = loc;
19876 cp_lexer_consume_token (parser->lexer);
19877 continue;
19878
19879 case RID_CONST:
19880 case RID_RESTRICT:
19881 error_at (loc, "%qT is not an asm qualifier", token->u.value);
19882 cp_lexer_consume_token (parser->lexer);
19883 continue;
19884
19885 default:
19886 break;
19887 }
19888 break;
19889 }
19890
19891 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
19892 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
19893 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
19894
19895 if (!parser->in_function_body && (inline_p || goto_p))
19896 {
19897 error_at (first_loc, "asm qualifier outside of function body");
19898 inline_p = goto_p = false;
19899 }
19900
19901 /* Look for the opening `('. */
19902 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19903 return;
19904 /* Look for the string. */
19905 string = cp_parser_string_literal (parser, false, false);
19906 if (string == error_mark_node)
19907 {
19908 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19909 /*consume_paren=*/true);
19910 return;
19911 }
19912
19913 /* If we're allowing GNU extensions, check for the extended assembly
19914 syntax. Unfortunately, the `:' tokens need not be separated by
19915 a space in C, and so, for compatibility, we tolerate that here
19916 too. Doing that means that we have to treat the `::' operator as
19917 two `:' tokens. */
19918 if (cp_parser_allow_gnu_extensions_p (parser)
19919 && parser->in_function_body
19920 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19921 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19922 {
19923 bool inputs_p = false;
19924 bool clobbers_p = false;
19925 bool labels_p = false;
19926
19927 /* The extended syntax was used. */
19928 extended_p = true;
19929
19930 /* Look for outputs. */
19931 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19932 {
19933 /* Consume the `:'. */
19934 cp_lexer_consume_token (parser->lexer);
19935 /* Parse the output-operands. */
19936 if (cp_lexer_next_token_is_not (parser->lexer,
19937 CPP_COLON)
19938 && cp_lexer_next_token_is_not (parser->lexer,
19939 CPP_SCOPE)
19940 && cp_lexer_next_token_is_not (parser->lexer,
19941 CPP_CLOSE_PAREN)
19942 && !goto_p)
19943 {
19944 outputs = cp_parser_asm_operand_list (parser);
19945 if (outputs == error_mark_node)
19946 invalid_outputs_p = true;
19947 }
19948 }
19949 /* If the next token is `::', there are no outputs, and the
19950 next token is the beginning of the inputs. */
19951 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19952 /* The inputs are coming next. */
19953 inputs_p = true;
19954
19955 /* Look for inputs. */
19956 if (inputs_p
19957 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19958 {
19959 /* Consume the `:' or `::'. */
19960 cp_lexer_consume_token (parser->lexer);
19961 /* Parse the output-operands. */
19962 if (cp_lexer_next_token_is_not (parser->lexer,
19963 CPP_COLON)
19964 && cp_lexer_next_token_is_not (parser->lexer,
19965 CPP_SCOPE)
19966 && cp_lexer_next_token_is_not (parser->lexer,
19967 CPP_CLOSE_PAREN))
19968 {
19969 inputs = cp_parser_asm_operand_list (parser);
19970 if (inputs == error_mark_node)
19971 invalid_inputs_p = true;
19972 }
19973 }
19974 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19975 /* The clobbers are coming next. */
19976 clobbers_p = true;
19977
19978 /* Look for clobbers. */
19979 if (clobbers_p
19980 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19981 {
19982 clobbers_p = true;
19983 /* Consume the `:' or `::'. */
19984 cp_lexer_consume_token (parser->lexer);
19985 /* Parse the clobbers. */
19986 if (cp_lexer_next_token_is_not (parser->lexer,
19987 CPP_COLON)
19988 && cp_lexer_next_token_is_not (parser->lexer,
19989 CPP_CLOSE_PAREN))
19990 clobbers = cp_parser_asm_clobber_list (parser);
19991 }
19992 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19993 /* The labels are coming next. */
19994 labels_p = true;
19995
19996 /* Look for labels. */
19997 if (labels_p
19998 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19999 {
20000 labels_p = true;
20001 /* Consume the `:' or `::'. */
20002 cp_lexer_consume_token (parser->lexer);
20003 /* Parse the labels. */
20004 labels = cp_parser_asm_label_list (parser);
20005 }
20006
20007 if (goto_p && !labels_p)
20008 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
20009 }
20010 else if (goto_p)
20011 missing = RT_COLON_SCOPE;
20012
20013 /* Look for the closing `)'. */
20014 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
20015 missing ? missing : RT_CLOSE_PAREN))
20016 cp_parser_skip_to_closing_parenthesis (parser, true, false,
20017 /*consume_paren=*/true);
20018 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20019
20020 if (!invalid_inputs_p && !invalid_outputs_p)
20021 {
20022 /* Create the ASM_EXPR. */
20023 if (parser->in_function_body)
20024 {
20025 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
20026 inputs, clobbers, labels, inline_p);
20027 /* If the extended syntax was not used, mark the ASM_EXPR. */
20028 if (!extended_p)
20029 {
20030 tree temp = asm_stmt;
20031 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
20032 temp = TREE_OPERAND (temp, 0);
20033
20034 ASM_INPUT_P (temp) = 1;
20035 }
20036 }
20037 else
20038 symtab->finalize_toplevel_asm (string);
20039 }
20040 }
20041
20042 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
20043 type that comes from the decl-specifier-seq. */
20044
20045 static tree
20046 strip_declarator_types (tree type, cp_declarator *declarator)
20047 {
20048 for (cp_declarator *d = declarator; d;)
20049 switch (d->kind)
20050 {
20051 case cdk_id:
20052 case cdk_decomp:
20053 case cdk_error:
20054 d = NULL;
20055 break;
20056
20057 default:
20058 if (TYPE_PTRMEMFUNC_P (type))
20059 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
20060 type = TREE_TYPE (type);
20061 d = d->declarator;
20062 break;
20063 }
20064
20065 return type;
20066 }
20067
20068 /* Declarators [gram.dcl.decl] */
20069
20070 /* Parse an init-declarator.
20071
20072 init-declarator:
20073 declarator initializer [opt]
20074
20075 GNU Extension:
20076
20077 init-declarator:
20078 declarator asm-specification [opt] attributes [opt] initializer [opt]
20079
20080 function-definition:
20081 decl-specifier-seq [opt] declarator ctor-initializer [opt]
20082 function-body
20083 decl-specifier-seq [opt] declarator function-try-block
20084
20085 GNU Extension:
20086
20087 function-definition:
20088 __extension__ function-definition
20089
20090 TM Extension:
20091
20092 function-definition:
20093 decl-specifier-seq [opt] declarator function-transaction-block
20094
20095 The parser flags FLAGS is used to control type-specifier parsing.
20096
20097 The DECL_SPECIFIERS apply to this declarator. Returns a
20098 representation of the entity declared. If MEMBER_P is TRUE, then
20099 this declarator appears in a class scope. The new DECL created by
20100 this declarator is returned.
20101
20102 The CHECKS are access checks that should be performed once we know
20103 what entity is being declared (and, therefore, what classes have
20104 befriended it).
20105
20106 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20107 for a function-definition here as well. If the declarator is a
20108 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20109 be TRUE upon return. By that point, the function-definition will
20110 have been completely parsed.
20111
20112 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20113 is FALSE.
20114
20115 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20116 parsed declaration if it is an uninitialized single declarator not followed
20117 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20118 if present, will not be consumed. If returned, this declarator will be
20119 created with SD_INITIALIZED but will not call cp_finish_decl.
20120
20121 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20122 and there is an initializer, the pointed location_t is set to the
20123 location of the '=' or `(', or '{' in C++11 token introducing the
20124 initializer. */
20125
20126 static tree
20127 cp_parser_init_declarator (cp_parser* parser,
20128 cp_parser_flags flags,
20129 cp_decl_specifier_seq *decl_specifiers,
20130 vec<deferred_access_check, va_gc> *checks,
20131 bool function_definition_allowed_p,
20132 bool member_p,
20133 int declares_class_or_enum,
20134 bool* function_definition_p,
20135 tree* maybe_range_for_decl,
20136 location_t* init_loc,
20137 tree* auto_result)
20138 {
20139 cp_token *token = NULL, *asm_spec_start_token = NULL,
20140 *attributes_start_token = NULL;
20141 cp_declarator *declarator;
20142 tree prefix_attributes;
20143 tree attributes = NULL;
20144 tree asm_specification;
20145 tree initializer;
20146 tree decl = NULL_TREE;
20147 tree scope;
20148 int is_initialized;
20149 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20150 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20151 "(...)". */
20152 enum cpp_ttype initialization_kind;
20153 bool is_direct_init = false;
20154 bool is_non_constant_init;
20155 int ctor_dtor_or_conv_p;
20156 bool friend_p = cp_parser_friend_p (decl_specifiers);
20157 tree pushed_scope = NULL_TREE;
20158 bool range_for_decl_p = false;
20159 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20160 location_t tmp_init_loc = UNKNOWN_LOCATION;
20161
20162 /* Gather the attributes that were provided with the
20163 decl-specifiers. */
20164 prefix_attributes = decl_specifiers->attributes;
20165
20166 /* Assume that this is not the declarator for a function
20167 definition. */
20168 if (function_definition_p)
20169 *function_definition_p = false;
20170
20171 /* Default arguments are only permitted for function parameters. */
20172 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
20173 parser->default_arg_ok_p = false;
20174
20175 /* Defer access checks while parsing the declarator; we cannot know
20176 what names are accessible until we know what is being
20177 declared. */
20178 resume_deferring_access_checks ();
20179
20180 token = cp_lexer_peek_token (parser->lexer);
20181
20182 /* Parse the declarator. */
20183 declarator
20184 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20185 flags, &ctor_dtor_or_conv_p,
20186 /*parenthesized_p=*/NULL,
20187 member_p, friend_p, /*static_p=*/false);
20188 /* Gather up the deferred checks. */
20189 stop_deferring_access_checks ();
20190
20191 parser->default_arg_ok_p = saved_default_arg_ok_p;
20192
20193 /* If the DECLARATOR was erroneous, there's no need to go
20194 further. */
20195 if (declarator == cp_error_declarator)
20196 return error_mark_node;
20197
20198 /* Check that the number of template-parameter-lists is OK. */
20199 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
20200 token->location))
20201 return error_mark_node;
20202
20203 if (declares_class_or_enum & 2)
20204 cp_parser_check_for_definition_in_return_type (declarator,
20205 decl_specifiers->type,
20206 decl_specifiers->locations[ds_type_spec]);
20207
20208 /* Figure out what scope the entity declared by the DECLARATOR is
20209 located in. `grokdeclarator' sometimes changes the scope, so
20210 we compute it now. */
20211 scope = get_scope_of_declarator (declarator);
20212
20213 /* Perform any lookups in the declared type which were thought to be
20214 dependent, but are not in the scope of the declarator. */
20215 decl_specifiers->type
20216 = maybe_update_decl_type (decl_specifiers->type, scope);
20217
20218 /* If we're allowing GNU extensions, look for an
20219 asm-specification. */
20220 if (cp_parser_allow_gnu_extensions_p (parser))
20221 {
20222 /* Look for an asm-specification. */
20223 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
20224 asm_specification = cp_parser_asm_specification_opt (parser);
20225 }
20226 else
20227 asm_specification = NULL_TREE;
20228
20229 /* Look for attributes. */
20230 attributes_start_token = cp_lexer_peek_token (parser->lexer);
20231 attributes = cp_parser_attributes_opt (parser);
20232
20233 /* Peek at the next token. */
20234 token = cp_lexer_peek_token (parser->lexer);
20235
20236 bool bogus_implicit_tmpl = false;
20237
20238 if (function_declarator_p (declarator))
20239 {
20240 /* Handle C++17 deduction guides. */
20241 if (!decl_specifiers->type
20242 && ctor_dtor_or_conv_p <= 0
20243 && cxx_dialect >= cxx17)
20244 {
20245 cp_declarator *id = get_id_declarator (declarator);
20246 tree name = id->u.id.unqualified_name;
20247 parser->scope = id->u.id.qualifying_scope;
20248 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
20249 if (tmpl
20250 && (DECL_CLASS_TEMPLATE_P (tmpl)
20251 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
20252 {
20253 id->u.id.unqualified_name = dguide_name (tmpl);
20254 id->u.id.sfk = sfk_deduction_guide;
20255 ctor_dtor_or_conv_p = 1;
20256 }
20257 }
20258
20259 /* Check to see if the token indicates the start of a
20260 function-definition. */
20261 if (cp_parser_token_starts_function_definition_p (token))
20262 {
20263 if (!function_definition_allowed_p)
20264 {
20265 /* If a function-definition should not appear here, issue an
20266 error message. */
20267 cp_parser_error (parser,
20268 "a function-definition is not allowed here");
20269 return error_mark_node;
20270 }
20271
20272 location_t func_brace_location
20273 = cp_lexer_peek_token (parser->lexer)->location;
20274
20275 /* Neither attributes nor an asm-specification are allowed
20276 on a function-definition. */
20277 if (asm_specification)
20278 error_at (asm_spec_start_token->location,
20279 "an asm-specification is not allowed "
20280 "on a function-definition");
20281 if (attributes)
20282 error_at (attributes_start_token->location,
20283 "attributes are not allowed "
20284 "on a function-definition");
20285 /* This is a function-definition. */
20286 *function_definition_p = true;
20287
20288 /* Parse the function definition. */
20289 if (member_p)
20290 decl = cp_parser_save_member_function_body (parser,
20291 decl_specifiers,
20292 declarator,
20293 prefix_attributes);
20294 else
20295 decl =
20296 (cp_parser_function_definition_from_specifiers_and_declarator
20297 (parser, decl_specifiers, prefix_attributes, declarator));
20298
20299 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
20300 {
20301 /* This is where the prologue starts... */
20302 DECL_STRUCT_FUNCTION (decl)->function_start_locus
20303 = func_brace_location;
20304 }
20305
20306 return decl;
20307 }
20308 }
20309 else if (parser->fully_implicit_function_template_p)
20310 {
20311 /* A non-template declaration involving a function parameter list
20312 containing an implicit template parameter will be made into a
20313 template. If the resulting declaration is not going to be an
20314 actual function then finish the template scope here to prevent it.
20315 An error message will be issued once we have a decl to talk about.
20316
20317 FIXME probably we should do type deduction rather than create an
20318 implicit template, but the standard currently doesn't allow it. */
20319 bogus_implicit_tmpl = true;
20320 finish_fully_implicit_template (parser, NULL_TREE);
20321 }
20322
20323 /* [dcl.dcl]
20324
20325 Only in function declarations for constructors, destructors, type
20326 conversions, and deduction guides can the decl-specifier-seq be omitted.
20327
20328 We explicitly postpone this check past the point where we handle
20329 function-definitions because we tolerate function-definitions
20330 that are missing their return types in some modes. */
20331 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
20332 {
20333 cp_parser_error (parser,
20334 "expected constructor, destructor, or type conversion");
20335 return error_mark_node;
20336 }
20337
20338 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20339 if (token->type == CPP_EQ
20340 || token->type == CPP_OPEN_PAREN
20341 || token->type == CPP_OPEN_BRACE)
20342 {
20343 is_initialized = SD_INITIALIZED;
20344 initialization_kind = token->type;
20345 if (maybe_range_for_decl)
20346 *maybe_range_for_decl = error_mark_node;
20347 tmp_init_loc = token->location;
20348 if (init_loc && *init_loc == UNKNOWN_LOCATION)
20349 *init_loc = tmp_init_loc;
20350
20351 if (token->type == CPP_EQ
20352 && function_declarator_p (declarator))
20353 {
20354 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
20355 if (t2->keyword == RID_DEFAULT)
20356 is_initialized = SD_DEFAULTED;
20357 else if (t2->keyword == RID_DELETE)
20358 is_initialized = SD_DELETED;
20359 }
20360 }
20361 else
20362 {
20363 /* If the init-declarator isn't initialized and isn't followed by a
20364 `,' or `;', it's not a valid init-declarator. */
20365 if (token->type != CPP_COMMA
20366 && token->type != CPP_SEMICOLON)
20367 {
20368 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
20369 range_for_decl_p = true;
20370 else
20371 {
20372 if (!maybe_range_for_decl)
20373 cp_parser_error (parser, "expected initializer");
20374 return error_mark_node;
20375 }
20376 }
20377 is_initialized = SD_UNINITIALIZED;
20378 initialization_kind = CPP_EOF;
20379 }
20380
20381 /* Because start_decl has side-effects, we should only call it if we
20382 know we're going ahead. By this point, we know that we cannot
20383 possibly be looking at any other construct. */
20384 cp_parser_commit_to_tentative_parse (parser);
20385
20386 /* Enter the newly declared entry in the symbol table. If we're
20387 processing a declaration in a class-specifier, we wait until
20388 after processing the initializer. */
20389 if (!member_p)
20390 {
20391 if (parser->in_unbraced_linkage_specification_p)
20392 decl_specifiers->storage_class = sc_extern;
20393 decl = start_decl (declarator, decl_specifiers,
20394 range_for_decl_p? SD_INITIALIZED : is_initialized,
20395 attributes, prefix_attributes, &pushed_scope);
20396 cp_finalize_omp_declare_simd (parser, decl);
20397 cp_finalize_oacc_routine (parser, decl, false);
20398 /* Adjust location of decl if declarator->id_loc is more appropriate:
20399 set, and decl wasn't merged with another decl, in which case its
20400 location would be different from input_location, and more accurate. */
20401 if (DECL_P (decl)
20402 && declarator->id_loc != UNKNOWN_LOCATION
20403 && DECL_SOURCE_LOCATION (decl) == input_location)
20404 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
20405 }
20406 else if (scope)
20407 /* Enter the SCOPE. That way unqualified names appearing in the
20408 initializer will be looked up in SCOPE. */
20409 pushed_scope = push_scope (scope);
20410
20411 /* Perform deferred access control checks, now that we know in which
20412 SCOPE the declared entity resides. */
20413 if (!member_p && decl)
20414 {
20415 tree saved_current_function_decl = NULL_TREE;
20416
20417 /* If the entity being declared is a function, pretend that we
20418 are in its scope. If it is a `friend', it may have access to
20419 things that would not otherwise be accessible. */
20420 if (TREE_CODE (decl) == FUNCTION_DECL)
20421 {
20422 saved_current_function_decl = current_function_decl;
20423 current_function_decl = decl;
20424 }
20425
20426 /* Perform access checks for template parameters. */
20427 cp_parser_perform_template_parameter_access_checks (checks);
20428
20429 /* Perform the access control checks for the declarator and the
20430 decl-specifiers. */
20431 perform_deferred_access_checks (tf_warning_or_error);
20432
20433 /* Restore the saved value. */
20434 if (TREE_CODE (decl) == FUNCTION_DECL)
20435 current_function_decl = saved_current_function_decl;
20436 }
20437
20438 /* Parse the initializer. */
20439 initializer = NULL_TREE;
20440 is_direct_init = false;
20441 is_non_constant_init = true;
20442 if (is_initialized)
20443 {
20444 if (function_declarator_p (declarator))
20445 {
20446 if (initialization_kind == CPP_EQ)
20447 initializer = cp_parser_pure_specifier (parser);
20448 else
20449 {
20450 /* If the declaration was erroneous, we don't really
20451 know what the user intended, so just silently
20452 consume the initializer. */
20453 if (decl != error_mark_node)
20454 error_at (tmp_init_loc, "initializer provided for function");
20455 cp_parser_skip_to_closing_parenthesis (parser,
20456 /*recovering=*/true,
20457 /*or_comma=*/false,
20458 /*consume_paren=*/true);
20459 }
20460 }
20461 else
20462 {
20463 /* We want to record the extra mangling scope for in-class
20464 initializers of class members and initializers of static data
20465 member templates. The former involves deferring
20466 parsing of the initializer until end of class as with default
20467 arguments. So right here we only handle the latter. */
20468 if (!member_p && processing_template_decl && decl != error_mark_node)
20469 start_lambda_scope (decl);
20470 initializer = cp_parser_initializer (parser,
20471 &is_direct_init,
20472 &is_non_constant_init);
20473 if (!member_p && processing_template_decl && decl != error_mark_node)
20474 finish_lambda_scope ();
20475 if (initializer == error_mark_node)
20476 cp_parser_skip_to_end_of_statement (parser);
20477 }
20478 }
20479
20480 /* The old parser allows attributes to appear after a parenthesized
20481 initializer. Mark Mitchell proposed removing this functionality
20482 on the GCC mailing lists on 2002-08-13. This parser accepts the
20483 attributes -- but ignores them. Made a permerror in GCC 8. */
20484 if (cp_parser_allow_gnu_extensions_p (parser)
20485 && initialization_kind == CPP_OPEN_PAREN
20486 && cp_parser_attributes_opt (parser)
20487 && permerror (input_location,
20488 "attributes after parenthesized initializer ignored"))
20489 {
20490 static bool hint;
20491 if (flag_permissive && !hint)
20492 {
20493 hint = true;
20494 inform (input_location,
20495 "this flexibility is deprecated and will be removed");
20496 }
20497 }
20498
20499 /* And now complain about a non-function implicit template. */
20500 if (bogus_implicit_tmpl && decl != error_mark_node)
20501 error_at (DECL_SOURCE_LOCATION (decl),
20502 "non-function %qD declared as implicit template", decl);
20503
20504 /* For an in-class declaration, use `grokfield' to create the
20505 declaration. */
20506 if (member_p)
20507 {
20508 if (pushed_scope)
20509 {
20510 pop_scope (pushed_scope);
20511 pushed_scope = NULL_TREE;
20512 }
20513 decl = grokfield (declarator, decl_specifiers,
20514 initializer, !is_non_constant_init,
20515 /*asmspec=*/NULL_TREE,
20516 attr_chainon (attributes, prefix_attributes));
20517 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20518 cp_parser_save_default_args (parser, decl);
20519 cp_finalize_omp_declare_simd (parser, decl);
20520 cp_finalize_oacc_routine (parser, decl, false);
20521 }
20522
20523 /* Finish processing the declaration. But, skip member
20524 declarations. */
20525 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20526 {
20527 cp_finish_decl (decl,
20528 initializer, !is_non_constant_init,
20529 asm_specification,
20530 /* If the initializer is in parentheses, then this is
20531 a direct-initialization, which means that an
20532 `explicit' constructor is OK. Otherwise, an
20533 `explicit' constructor cannot be used. */
20534 ((is_direct_init || !is_initialized)
20535 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20536 }
20537 else if ((cxx_dialect != cxx98) && friend_p
20538 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20539 /* Core issue #226 (C++0x only): A default template-argument
20540 shall not be specified in a friend class template
20541 declaration. */
20542 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20543 /*is_partial=*/false, /*is_friend_decl=*/1);
20544
20545 if (!friend_p && pushed_scope)
20546 pop_scope (pushed_scope);
20547
20548 if (function_declarator_p (declarator)
20549 && parser->fully_implicit_function_template_p)
20550 {
20551 if (member_p)
20552 decl = finish_fully_implicit_template (parser, decl);
20553 else
20554 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20555 }
20556
20557 if (auto_result && is_initialized && decl_specifiers->type
20558 && type_uses_auto (decl_specifiers->type))
20559 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20560
20561 return decl;
20562 }
20563
20564 /* Parse a declarator.
20565
20566 declarator:
20567 direct-declarator
20568 ptr-operator declarator
20569
20570 abstract-declarator:
20571 ptr-operator abstract-declarator [opt]
20572 direct-abstract-declarator
20573
20574 GNU Extensions:
20575
20576 declarator:
20577 attributes [opt] direct-declarator
20578 attributes [opt] ptr-operator declarator
20579
20580 abstract-declarator:
20581 attributes [opt] ptr-operator abstract-declarator [opt]
20582 attributes [opt] direct-abstract-declarator
20583
20584 The parser flags FLAGS is used to control type-specifier parsing.
20585
20586 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20587 detect constructors, destructors, deduction guides, or conversion operators.
20588 It is set to -1 if the declarator is a name, and +1 if it is a
20589 function. Otherwise it is set to zero. Usually you just want to
20590 test for >0, but internally the negative value is used.
20591
20592 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20593 a decl-specifier-seq unless it declares a constructor, destructor,
20594 or conversion. It might seem that we could check this condition in
20595 semantic analysis, rather than parsing, but that makes it difficult
20596 to handle something like `f()'. We want to notice that there are
20597 no decl-specifiers, and therefore realize that this is an
20598 expression, not a declaration.)
20599
20600 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20601 the declarator is a direct-declarator of the form "(...)".
20602
20603 MEMBER_P is true iff this declarator is a member-declarator.
20604
20605 FRIEND_P is true iff this declarator is a friend.
20606
20607 STATIC_P is true iff the keyword static was seen. */
20608
20609 static cp_declarator *
20610 cp_parser_declarator (cp_parser* parser,
20611 cp_parser_declarator_kind dcl_kind,
20612 cp_parser_flags flags,
20613 int* ctor_dtor_or_conv_p,
20614 bool* parenthesized_p,
20615 bool member_p, bool friend_p, bool static_p)
20616 {
20617 cp_declarator *declarator;
20618 enum tree_code code;
20619 cp_cv_quals cv_quals;
20620 tree class_type;
20621 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20622
20623 /* Assume this is not a constructor, destructor, or type-conversion
20624 operator. */
20625 if (ctor_dtor_or_conv_p)
20626 *ctor_dtor_or_conv_p = 0;
20627
20628 if (cp_parser_allow_gnu_extensions_p (parser))
20629 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20630
20631 /* Check for the ptr-operator production. */
20632 cp_parser_parse_tentatively (parser);
20633 /* Parse the ptr-operator. */
20634 code = cp_parser_ptr_operator (parser,
20635 &class_type,
20636 &cv_quals,
20637 &std_attributes);
20638
20639 /* If that worked, then we have a ptr-operator. */
20640 if (cp_parser_parse_definitely (parser))
20641 {
20642 /* If a ptr-operator was found, then this declarator was not
20643 parenthesized. */
20644 if (parenthesized_p)
20645 *parenthesized_p = true;
20646 /* The dependent declarator is optional if we are parsing an
20647 abstract-declarator. */
20648 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20649 cp_parser_parse_tentatively (parser);
20650
20651 /* Parse the dependent declarator. */
20652 declarator = cp_parser_declarator (parser, dcl_kind,
20653 CP_PARSER_FLAGS_NONE,
20654 /*ctor_dtor_or_conv_p=*/NULL,
20655 /*parenthesized_p=*/NULL,
20656 /*member_p=*/false,
20657 friend_p, /*static_p=*/false);
20658
20659 /* If we are parsing an abstract-declarator, we must handle the
20660 case where the dependent declarator is absent. */
20661 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20662 && !cp_parser_parse_definitely (parser))
20663 declarator = NULL;
20664
20665 declarator = cp_parser_make_indirect_declarator
20666 (code, class_type, cv_quals, declarator, std_attributes);
20667 }
20668 /* Everything else is a direct-declarator. */
20669 else
20670 {
20671 if (parenthesized_p)
20672 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20673 CPP_OPEN_PAREN);
20674 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20675 flags, ctor_dtor_or_conv_p,
20676 member_p, friend_p, static_p);
20677 }
20678
20679 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20680 declarator->attributes = gnu_attributes;
20681 return declarator;
20682 }
20683
20684 /* Parse a direct-declarator or direct-abstract-declarator.
20685
20686 direct-declarator:
20687 declarator-id
20688 direct-declarator ( parameter-declaration-clause )
20689 cv-qualifier-seq [opt]
20690 ref-qualifier [opt]
20691 exception-specification [opt]
20692 direct-declarator [ constant-expression [opt] ]
20693 ( declarator )
20694
20695 direct-abstract-declarator:
20696 direct-abstract-declarator [opt]
20697 ( parameter-declaration-clause )
20698 cv-qualifier-seq [opt]
20699 ref-qualifier [opt]
20700 exception-specification [opt]
20701 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20702 ( abstract-declarator )
20703
20704 Returns a representation of the declarator. DCL_KIND is
20705 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20706 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20707 we are parsing a direct-declarator. It is
20708 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20709 of ambiguity we prefer an abstract declarator, as per
20710 [dcl.ambig.res].
20711 The parser flags FLAGS is used to control type-specifier parsing.
20712 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
20713 as for cp_parser_declarator. */
20714
20715 static cp_declarator *
20716 cp_parser_direct_declarator (cp_parser* parser,
20717 cp_parser_declarator_kind dcl_kind,
20718 cp_parser_flags flags,
20719 int* ctor_dtor_or_conv_p,
20720 bool member_p, bool friend_p, bool static_p)
20721 {
20722 cp_token *token;
20723 cp_declarator *declarator = NULL;
20724 tree scope = NULL_TREE;
20725 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20726 bool saved_in_declarator_p = parser->in_declarator_p;
20727 bool first = true;
20728 tree pushed_scope = NULL_TREE;
20729 cp_token *open_paren = NULL, *close_paren = NULL;
20730
20731 while (true)
20732 {
20733 /* Peek at the next token. */
20734 token = cp_lexer_peek_token (parser->lexer);
20735 if (token->type == CPP_OPEN_PAREN)
20736 {
20737 /* This is either a parameter-declaration-clause, or a
20738 parenthesized declarator. When we know we are parsing a
20739 named declarator, it must be a parenthesized declarator
20740 if FIRST is true. For instance, `(int)' is a
20741 parameter-declaration-clause, with an omitted
20742 direct-abstract-declarator. But `((*))', is a
20743 parenthesized abstract declarator. Finally, when T is a
20744 template parameter `(T)' is a
20745 parameter-declaration-clause, and not a parenthesized
20746 named declarator.
20747
20748 We first try and parse a parameter-declaration-clause,
20749 and then try a nested declarator (if FIRST is true).
20750
20751 It is not an error for it not to be a
20752 parameter-declaration-clause, even when FIRST is
20753 false. Consider,
20754
20755 int i (int);
20756 int i (3);
20757
20758 The first is the declaration of a function while the
20759 second is the definition of a variable, including its
20760 initializer.
20761
20762 Having seen only the parenthesis, we cannot know which of
20763 these two alternatives should be selected. Even more
20764 complex are examples like:
20765
20766 int i (int (a));
20767 int i (int (3));
20768
20769 The former is a function-declaration; the latter is a
20770 variable initialization.
20771
20772 Thus again, we try a parameter-declaration-clause, and if
20773 that fails, we back out and return. */
20774
20775 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20776 {
20777 tree params;
20778 bool is_declarator = false;
20779
20780 open_paren = NULL;
20781
20782 /* In a member-declarator, the only valid interpretation
20783 of a parenthesis is the start of a
20784 parameter-declaration-clause. (It is invalid to
20785 initialize a static data member with a parenthesized
20786 initializer; only the "=" form of initialization is
20787 permitted.) */
20788 if (!member_p)
20789 cp_parser_parse_tentatively (parser);
20790
20791 /* Consume the `('. */
20792 matching_parens parens;
20793 parens.consume_open (parser);
20794 if (first)
20795 {
20796 /* If this is going to be an abstract declarator, we're
20797 in a declarator and we can't have default args. */
20798 parser->default_arg_ok_p = false;
20799 parser->in_declarator_p = true;
20800 }
20801
20802 begin_scope (sk_function_parms, NULL_TREE);
20803
20804 /* Parse the parameter-declaration-clause. */
20805 params
20806 = cp_parser_parameter_declaration_clause (parser, flags);
20807
20808 /* Consume the `)'. */
20809 parens.require_close (parser);
20810
20811 /* If all went well, parse the cv-qualifier-seq,
20812 ref-qualifier and the exception-specification. */
20813 if (member_p || cp_parser_parse_definitely (parser))
20814 {
20815 cp_cv_quals cv_quals;
20816 cp_virt_specifiers virt_specifiers;
20817 cp_ref_qualifier ref_qual;
20818 tree exception_specification;
20819 tree late_return;
20820 tree attrs;
20821 bool memfn = (member_p || (pushed_scope
20822 && CLASS_TYPE_P (pushed_scope)));
20823 unsigned char local_variables_forbidden_p
20824 = parser->local_variables_forbidden_p;
20825 /* 'this' is not allowed in static member functions. */
20826 if (static_p || friend_p)
20827 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
20828
20829 is_declarator = true;
20830
20831 if (ctor_dtor_or_conv_p)
20832 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20833 first = false;
20834
20835 /* Parse the cv-qualifier-seq. */
20836 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20837 /* Parse the ref-qualifier. */
20838 ref_qual = cp_parser_ref_qualifier_opt (parser);
20839 /* Parse the tx-qualifier. */
20840 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20841 /* And the exception-specification. */
20842 exception_specification
20843 = cp_parser_exception_specification_opt (parser);
20844
20845 attrs = cp_parser_std_attribute_spec_seq (parser);
20846
20847 /* In here, we handle cases where attribute is used after
20848 the function declaration. For example:
20849 void func (int x) __attribute__((vector(..))); */
20850 tree gnu_attrs = NULL_TREE;
20851 tree requires_clause = NULL_TREE;
20852 late_return = (cp_parser_late_return_type_opt
20853 (parser, declarator, requires_clause,
20854 memfn ? cv_quals : -1));
20855
20856 /* Parse the virt-specifier-seq. */
20857 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20858
20859 /* Create the function-declarator. */
20860 declarator = make_call_declarator (declarator,
20861 params,
20862 cv_quals,
20863 virt_specifiers,
20864 ref_qual,
20865 tx_qual,
20866 exception_specification,
20867 late_return,
20868 requires_clause);
20869 declarator->std_attributes = attrs;
20870 declarator->attributes = gnu_attrs;
20871 /* Any subsequent parameter lists are to do with
20872 return type, so are not those of the declared
20873 function. */
20874 parser->default_arg_ok_p = false;
20875
20876 /* Restore the state of local_variables_forbidden_p. */
20877 parser->local_variables_forbidden_p
20878 = local_variables_forbidden_p;
20879 }
20880
20881 /* Remove the function parms from scope. */
20882 pop_bindings_and_leave_scope ();
20883
20884 if (is_declarator)
20885 /* Repeat the main loop. */
20886 continue;
20887 }
20888
20889 /* If this is the first, we can try a parenthesized
20890 declarator. */
20891 if (first)
20892 {
20893 bool saved_in_type_id_in_expr_p;
20894
20895 parser->default_arg_ok_p = saved_default_arg_ok_p;
20896 parser->in_declarator_p = saved_in_declarator_p;
20897
20898 open_paren = token;
20899 /* Consume the `('. */
20900 matching_parens parens;
20901 parens.consume_open (parser);
20902 /* Parse the nested declarator. */
20903 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20904 parser->in_type_id_in_expr_p = true;
20905 declarator
20906 = cp_parser_declarator (parser, dcl_kind, flags,
20907 ctor_dtor_or_conv_p,
20908 /*parenthesized_p=*/NULL,
20909 member_p, friend_p,
20910 /*static_p=*/false);
20911 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20912 first = false;
20913 /* Expect a `)'. */
20914 close_paren = cp_lexer_peek_token (parser->lexer);
20915 if (!parens.require_close (parser))
20916 declarator = cp_error_declarator;
20917 if (declarator == cp_error_declarator)
20918 break;
20919
20920 goto handle_declarator;
20921 }
20922 /* Otherwise, we must be done. */
20923 else
20924 break;
20925 }
20926 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20927 && token->type == CPP_OPEN_SQUARE
20928 && !cp_next_tokens_can_be_attribute_p (parser))
20929 {
20930 /* Parse an array-declarator. */
20931 tree bounds, attrs;
20932
20933 if (ctor_dtor_or_conv_p)
20934 *ctor_dtor_or_conv_p = 0;
20935
20936 open_paren = NULL;
20937 first = false;
20938 parser->default_arg_ok_p = false;
20939 parser->in_declarator_p = true;
20940 /* Consume the `['. */
20941 cp_lexer_consume_token (parser->lexer);
20942 /* Peek at the next token. */
20943 token = cp_lexer_peek_token (parser->lexer);
20944 /* If the next token is `]', then there is no
20945 constant-expression. */
20946 if (token->type != CPP_CLOSE_SQUARE)
20947 {
20948 bool non_constant_p;
20949 bounds
20950 = cp_parser_constant_expression (parser,
20951 /*allow_non_constant=*/true,
20952 &non_constant_p);
20953 if (!non_constant_p)
20954 /* OK */;
20955 else if (error_operand_p (bounds))
20956 /* Already gave an error. */;
20957 else if (!parser->in_function_body
20958 || current_binding_level->kind == sk_function_parms)
20959 {
20960 /* Normally, the array bound must be an integral constant
20961 expression. However, as an extension, we allow VLAs
20962 in function scopes as long as they aren't part of a
20963 parameter declaration. */
20964 cp_parser_error (parser,
20965 "array bound is not an integer constant");
20966 bounds = error_mark_node;
20967 }
20968 else if (processing_template_decl
20969 && !type_dependent_expression_p (bounds))
20970 {
20971 /* Remember this wasn't a constant-expression. */
20972 bounds = build_nop (TREE_TYPE (bounds), bounds);
20973 TREE_SIDE_EFFECTS (bounds) = 1;
20974 }
20975 }
20976 else
20977 bounds = NULL_TREE;
20978 /* Look for the closing `]'. */
20979 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20980 {
20981 declarator = cp_error_declarator;
20982 break;
20983 }
20984
20985 attrs = cp_parser_std_attribute_spec_seq (parser);
20986 declarator = make_array_declarator (declarator, bounds);
20987 declarator->std_attributes = attrs;
20988 }
20989 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20990 {
20991 {
20992 tree qualifying_scope;
20993 tree unqualified_name;
20994 tree attrs;
20995 special_function_kind sfk;
20996 bool abstract_ok;
20997 bool pack_expansion_p = false;
20998 cp_token *declarator_id_start_token;
20999
21000 /* Parse a declarator-id */
21001 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
21002 if (abstract_ok)
21003 {
21004 cp_parser_parse_tentatively (parser);
21005
21006 /* If we see an ellipsis, we should be looking at a
21007 parameter pack. */
21008 if (token->type == CPP_ELLIPSIS)
21009 {
21010 /* Consume the `...' */
21011 cp_lexer_consume_token (parser->lexer);
21012
21013 pack_expansion_p = true;
21014 }
21015 }
21016
21017 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
21018 unqualified_name
21019 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
21020 qualifying_scope = parser->scope;
21021 if (abstract_ok)
21022 {
21023 bool okay = false;
21024
21025 if (!unqualified_name && pack_expansion_p)
21026 {
21027 /* Check whether an error occurred. */
21028 okay = !cp_parser_error_occurred (parser);
21029
21030 /* We already consumed the ellipsis to mark a
21031 parameter pack, but we have no way to report it,
21032 so abort the tentative parse. We will be exiting
21033 immediately anyway. */
21034 cp_parser_abort_tentative_parse (parser);
21035 }
21036 else
21037 okay = cp_parser_parse_definitely (parser);
21038
21039 if (!okay)
21040 unqualified_name = error_mark_node;
21041 else if (unqualified_name
21042 && (qualifying_scope
21043 || (!identifier_p (unqualified_name))))
21044 {
21045 cp_parser_error (parser, "expected unqualified-id");
21046 unqualified_name = error_mark_node;
21047 }
21048 }
21049
21050 if (!unqualified_name)
21051 return NULL;
21052 if (unqualified_name == error_mark_node)
21053 {
21054 declarator = cp_error_declarator;
21055 pack_expansion_p = false;
21056 declarator->parameter_pack_p = false;
21057 break;
21058 }
21059
21060 attrs = cp_parser_std_attribute_spec_seq (parser);
21061
21062 if (qualifying_scope && at_namespace_scope_p ()
21063 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
21064 {
21065 /* In the declaration of a member of a template class
21066 outside of the class itself, the SCOPE will sometimes
21067 be a TYPENAME_TYPE. For example, given:
21068
21069 template <typename T>
21070 int S<T>::R::i = 3;
21071
21072 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
21073 this context, we must resolve S<T>::R to an ordinary
21074 type, rather than a typename type.
21075
21076 The reason we normally avoid resolving TYPENAME_TYPEs
21077 is that a specialization of `S' might render
21078 `S<T>::R' not a type. However, if `S' is
21079 specialized, then this `i' will not be used, so there
21080 is no harm in resolving the types here. */
21081 tree type;
21082
21083 /* Resolve the TYPENAME_TYPE. */
21084 type = resolve_typename_type (qualifying_scope,
21085 /*only_current_p=*/false);
21086 /* If that failed, the declarator is invalid. */
21087 if (TREE_CODE (type) == TYPENAME_TYPE)
21088 {
21089 if (typedef_variant_p (type))
21090 error_at (declarator_id_start_token->location,
21091 "cannot define member of dependent typedef "
21092 "%qT", type);
21093 else
21094 error_at (declarator_id_start_token->location,
21095 "%<%T::%E%> is not a type",
21096 TYPE_CONTEXT (qualifying_scope),
21097 TYPE_IDENTIFIER (qualifying_scope));
21098 }
21099 qualifying_scope = type;
21100 }
21101
21102 sfk = sfk_none;
21103
21104 if (unqualified_name)
21105 {
21106 tree class_type;
21107
21108 if (qualifying_scope
21109 && CLASS_TYPE_P (qualifying_scope))
21110 class_type = qualifying_scope;
21111 else
21112 class_type = current_class_type;
21113
21114 if (TREE_CODE (unqualified_name) == TYPE_DECL)
21115 {
21116 tree name_type = TREE_TYPE (unqualified_name);
21117
21118 if (!class_type || !same_type_p (name_type, class_type))
21119 {
21120 /* We do not attempt to print the declarator
21121 here because we do not have enough
21122 information about its original syntactic
21123 form. */
21124 cp_parser_error (parser, "invalid declarator");
21125 declarator = cp_error_declarator;
21126 break;
21127 }
21128 else if (qualifying_scope
21129 && CLASSTYPE_USE_TEMPLATE (name_type))
21130 {
21131 error_at (declarator_id_start_token->location,
21132 "invalid use of constructor as a template");
21133 inform (declarator_id_start_token->location,
21134 "use %<%T::%D%> instead of %<%T::%D%> to "
21135 "name the constructor in a qualified name",
21136 class_type,
21137 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
21138 class_type, name_type);
21139 declarator = cp_error_declarator;
21140 break;
21141 }
21142 unqualified_name = constructor_name (class_type);
21143 }
21144
21145 if (class_type)
21146 {
21147 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
21148 sfk = sfk_destructor;
21149 else if (identifier_p (unqualified_name)
21150 && IDENTIFIER_CONV_OP_P (unqualified_name))
21151 sfk = sfk_conversion;
21152 else if (/* There's no way to declare a constructor
21153 for an unnamed type, even if the type
21154 got a name for linkage purposes. */
21155 !TYPE_WAS_UNNAMED (class_type)
21156 /* Handle correctly (c++/19200):
21157
21158 struct S {
21159 struct T{};
21160 friend void S(T);
21161 };
21162
21163 and also:
21164
21165 namespace N {
21166 void S();
21167 }
21168
21169 struct S {
21170 friend void N::S();
21171 }; */
21172 && (!friend_p || class_type == qualifying_scope)
21173 && constructor_name_p (unqualified_name,
21174 class_type))
21175 sfk = sfk_constructor;
21176 else if (is_overloaded_fn (unqualified_name)
21177 && DECL_CONSTRUCTOR_P (get_first_fn
21178 (unqualified_name)))
21179 sfk = sfk_constructor;
21180
21181 if (ctor_dtor_or_conv_p && sfk != sfk_none)
21182 *ctor_dtor_or_conv_p = -1;
21183 }
21184 }
21185 declarator = make_id_declarator (qualifying_scope,
21186 unqualified_name,
21187 sfk, token->location);
21188 declarator->std_attributes = attrs;
21189 declarator->parameter_pack_p = pack_expansion_p;
21190
21191 if (pack_expansion_p)
21192 maybe_warn_variadic_templates ();
21193
21194 /* We're looking for this case in [temp.res]:
21195 A qualified-id is assumed to name a type if [...]
21196 - it is a decl-specifier of the decl-specifier-seq of a
21197 parameter-declaration in a declarator of a function or
21198 function template declaration, ... */
21199 if (cxx_dialect >= cxx2a
21200 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
21201 && declarator->kind == cdk_id
21202 && !at_class_scope_p ()
21203 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21204 {
21205 /* ...whose declarator-id is qualified. If it isn't, never
21206 assume the parameters to refer to types. */
21207 if (qualifying_scope == NULL_TREE)
21208 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21209 else
21210 {
21211 /* Now we have something like
21212 template <typename T> int C::x(S::p);
21213 which can be a function template declaration or a
21214 variable template definition. If name lookup for
21215 the declarator-id C::x finds one or more function
21216 templates, assume S::p to name a type. Otherwise,
21217 don't. */
21218 tree decl
21219 = cp_parser_lookup_name_simple (parser, unqualified_name,
21220 token->location);
21221 if (!is_overloaded_fn (decl)
21222 /* Allow
21223 template<typename T>
21224 A<T>::A(T::type) { } */
21225 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
21226 && constructor_name_p (unqualified_name,
21227 qualifying_scope)))
21228 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21229 }
21230 }
21231 }
21232
21233 handle_declarator:;
21234 scope = get_scope_of_declarator (declarator);
21235 if (scope)
21236 {
21237 /* Any names that appear after the declarator-id for a
21238 member are looked up in the containing scope. */
21239 if (at_function_scope_p ())
21240 {
21241 /* But declarations with qualified-ids can't appear in a
21242 function. */
21243 cp_parser_error (parser, "qualified-id in declaration");
21244 declarator = cp_error_declarator;
21245 break;
21246 }
21247 pushed_scope = push_scope (scope);
21248 }
21249 parser->in_declarator_p = true;
21250 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
21251 || (declarator && declarator->kind == cdk_id))
21252 /* Default args are only allowed on function
21253 declarations. */
21254 parser->default_arg_ok_p = saved_default_arg_ok_p;
21255 else
21256 parser->default_arg_ok_p = false;
21257
21258 first = false;
21259 }
21260 /* We're done. */
21261 else
21262 break;
21263 }
21264
21265 /* For an abstract declarator, we might wind up with nothing at this
21266 point. That's an error; the declarator is not optional. */
21267 if (!declarator)
21268 cp_parser_error (parser, "expected declarator");
21269 else if (open_paren)
21270 {
21271 /* Record overly parenthesized declarator so we can give a
21272 diagnostic about confusing decl/expr disambiguation. */
21273 if (declarator->kind == cdk_array)
21274 {
21275 /* If the open and close parens are on different lines, this
21276 is probably a formatting thing, so ignore. */
21277 expanded_location open = expand_location (open_paren->location);
21278 expanded_location close = expand_location (close_paren->location);
21279 if (open.line != close.line || open.file != close.file)
21280 open_paren = NULL;
21281 }
21282 if (open_paren)
21283 declarator->parenthesized = open_paren->location;
21284 }
21285
21286 /* If we entered a scope, we must exit it now. */
21287 if (pushed_scope)
21288 pop_scope (pushed_scope);
21289
21290 parser->default_arg_ok_p = saved_default_arg_ok_p;
21291 parser->in_declarator_p = saved_in_declarator_p;
21292
21293 return declarator;
21294 }
21295
21296 /* Parse a ptr-operator.
21297
21298 ptr-operator:
21299 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21300 * cv-qualifier-seq [opt]
21301 &
21302 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21303 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21304
21305 GNU Extension:
21306
21307 ptr-operator:
21308 & cv-qualifier-seq [opt]
21309
21310 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21311 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21312 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21313 filled in with the TYPE containing the member. *CV_QUALS is
21314 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21315 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21316 Note that the tree codes returned by this function have nothing
21317 to do with the types of trees that will be eventually be created
21318 to represent the pointer or reference type being parsed. They are
21319 just constants with suggestive names. */
21320 static enum tree_code
21321 cp_parser_ptr_operator (cp_parser* parser,
21322 tree* type,
21323 cp_cv_quals *cv_quals,
21324 tree *attributes)
21325 {
21326 enum tree_code code = ERROR_MARK;
21327 cp_token *token;
21328 tree attrs = NULL_TREE;
21329
21330 /* Assume that it's not a pointer-to-member. */
21331 *type = NULL_TREE;
21332 /* And that there are no cv-qualifiers. */
21333 *cv_quals = TYPE_UNQUALIFIED;
21334
21335 /* Peek at the next token. */
21336 token = cp_lexer_peek_token (parser->lexer);
21337
21338 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21339 if (token->type == CPP_MULT)
21340 code = INDIRECT_REF;
21341 else if (token->type == CPP_AND)
21342 code = ADDR_EXPR;
21343 else if ((cxx_dialect != cxx98) &&
21344 token->type == CPP_AND_AND) /* C++0x only */
21345 code = NON_LVALUE_EXPR;
21346
21347 if (code != ERROR_MARK)
21348 {
21349 /* Consume the `*', `&' or `&&'. */
21350 cp_lexer_consume_token (parser->lexer);
21351
21352 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21353 `&', if we are allowing GNU extensions. (The only qualifier
21354 that can legally appear after `&' is `restrict', but that is
21355 enforced during semantic analysis. */
21356 if (code == INDIRECT_REF
21357 || cp_parser_allow_gnu_extensions_p (parser))
21358 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21359
21360 attrs = cp_parser_std_attribute_spec_seq (parser);
21361 if (attributes != NULL)
21362 *attributes = attrs;
21363 }
21364 else
21365 {
21366 /* Try the pointer-to-member case. */
21367 cp_parser_parse_tentatively (parser);
21368 /* Look for the optional `::' operator. */
21369 cp_parser_global_scope_opt (parser,
21370 /*current_scope_valid_p=*/false);
21371 /* Look for the nested-name specifier. */
21372 token = cp_lexer_peek_token (parser->lexer);
21373 cp_parser_nested_name_specifier (parser,
21374 /*typename_keyword_p=*/false,
21375 /*check_dependency_p=*/true,
21376 /*type_p=*/false,
21377 /*is_declaration=*/false);
21378 /* If we found it, and the next token is a `*', then we are
21379 indeed looking at a pointer-to-member operator. */
21380 if (!cp_parser_error_occurred (parser)
21381 && cp_parser_require (parser, CPP_MULT, RT_MULT))
21382 {
21383 /* Indicate that the `*' operator was used. */
21384 code = INDIRECT_REF;
21385
21386 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
21387 error_at (token->location, "%qD is a namespace", parser->scope);
21388 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
21389 error_at (token->location, "cannot form pointer to member of "
21390 "non-class %q#T", parser->scope);
21391 else
21392 {
21393 /* The type of which the member is a member is given by the
21394 current SCOPE. */
21395 *type = parser->scope;
21396 /* The next name will not be qualified. */
21397 parser->scope = NULL_TREE;
21398 parser->qualifying_scope = NULL_TREE;
21399 parser->object_scope = NULL_TREE;
21400 /* Look for optional c++11 attributes. */
21401 attrs = cp_parser_std_attribute_spec_seq (parser);
21402 if (attributes != NULL)
21403 *attributes = attrs;
21404 /* Look for the optional cv-qualifier-seq. */
21405 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21406 }
21407 }
21408 /* If that didn't work we don't have a ptr-operator. */
21409 if (!cp_parser_parse_definitely (parser))
21410 cp_parser_error (parser, "expected ptr-operator");
21411 }
21412
21413 return code;
21414 }
21415
21416 /* Parse an (optional) cv-qualifier-seq.
21417
21418 cv-qualifier-seq:
21419 cv-qualifier cv-qualifier-seq [opt]
21420
21421 cv-qualifier:
21422 const
21423 volatile
21424
21425 GNU Extension:
21426
21427 cv-qualifier:
21428 __restrict__
21429
21430 Returns a bitmask representing the cv-qualifiers. */
21431
21432 static cp_cv_quals
21433 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
21434 {
21435 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
21436
21437 while (true)
21438 {
21439 cp_token *token;
21440 cp_cv_quals cv_qualifier;
21441
21442 /* Peek at the next token. */
21443 token = cp_lexer_peek_token (parser->lexer);
21444 /* See if it's a cv-qualifier. */
21445 switch (token->keyword)
21446 {
21447 case RID_CONST:
21448 cv_qualifier = TYPE_QUAL_CONST;
21449 break;
21450
21451 case RID_VOLATILE:
21452 cv_qualifier = TYPE_QUAL_VOLATILE;
21453 break;
21454
21455 case RID_RESTRICT:
21456 cv_qualifier = TYPE_QUAL_RESTRICT;
21457 break;
21458
21459 default:
21460 cv_qualifier = TYPE_UNQUALIFIED;
21461 break;
21462 }
21463
21464 if (!cv_qualifier)
21465 break;
21466
21467 if (cv_quals & cv_qualifier)
21468 {
21469 gcc_rich_location richloc (token->location);
21470 richloc.add_fixit_remove ();
21471 error_at (&richloc, "duplicate cv-qualifier");
21472 cp_lexer_purge_token (parser->lexer);
21473 }
21474 else
21475 {
21476 cp_lexer_consume_token (parser->lexer);
21477 cv_quals |= cv_qualifier;
21478 }
21479 }
21480
21481 return cv_quals;
21482 }
21483
21484 /* Parse an (optional) ref-qualifier
21485
21486 ref-qualifier:
21487 &
21488 &&
21489
21490 Returns cp_ref_qualifier representing ref-qualifier. */
21491
21492 static cp_ref_qualifier
21493 cp_parser_ref_qualifier_opt (cp_parser* parser)
21494 {
21495 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
21496
21497 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21498 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
21499 return ref_qual;
21500
21501 while (true)
21502 {
21503 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
21504 cp_token *token = cp_lexer_peek_token (parser->lexer);
21505
21506 switch (token->type)
21507 {
21508 case CPP_AND:
21509 curr_ref_qual = REF_QUAL_LVALUE;
21510 break;
21511
21512 case CPP_AND_AND:
21513 curr_ref_qual = REF_QUAL_RVALUE;
21514 break;
21515
21516 default:
21517 curr_ref_qual = REF_QUAL_NONE;
21518 break;
21519 }
21520
21521 if (!curr_ref_qual)
21522 break;
21523 else if (ref_qual)
21524 {
21525 error_at (token->location, "multiple ref-qualifiers");
21526 cp_lexer_purge_token (parser->lexer);
21527 }
21528 else
21529 {
21530 ref_qual = curr_ref_qual;
21531 cp_lexer_consume_token (parser->lexer);
21532 }
21533 }
21534
21535 return ref_qual;
21536 }
21537
21538 /* Parse an optional tx-qualifier.
21539
21540 tx-qualifier:
21541 transaction_safe
21542 transaction_safe_dynamic */
21543
21544 static tree
21545 cp_parser_tx_qualifier_opt (cp_parser *parser)
21546 {
21547 cp_token *token = cp_lexer_peek_token (parser->lexer);
21548 if (token->type == CPP_NAME)
21549 {
21550 tree name = token->u.value;
21551 const char *p = IDENTIFIER_POINTER (name);
21552 const int len = strlen ("transaction_safe");
21553 if (!strncmp (p, "transaction_safe", len))
21554 {
21555 p += len;
21556 if (*p == '\0'
21557 || !strcmp (p, "_dynamic"))
21558 {
21559 cp_lexer_consume_token (parser->lexer);
21560 if (!flag_tm)
21561 {
21562 error ("%qE requires %<-fgnu-tm%>", name);
21563 return NULL_TREE;
21564 }
21565 else
21566 return name;
21567 }
21568 }
21569 }
21570 return NULL_TREE;
21571 }
21572
21573 /* Parse an (optional) virt-specifier-seq.
21574
21575 virt-specifier-seq:
21576 virt-specifier virt-specifier-seq [opt]
21577
21578 virt-specifier:
21579 override
21580 final
21581
21582 Returns a bitmask representing the virt-specifiers. */
21583
21584 static cp_virt_specifiers
21585 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21586 {
21587 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21588
21589 while (true)
21590 {
21591 cp_token *token;
21592 cp_virt_specifiers virt_specifier;
21593
21594 /* Peek at the next token. */
21595 token = cp_lexer_peek_token (parser->lexer);
21596 /* See if it's a virt-specifier-qualifier. */
21597 if (token->type != CPP_NAME)
21598 break;
21599 if (id_equal (token->u.value, "override"))
21600 {
21601 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21602 virt_specifier = VIRT_SPEC_OVERRIDE;
21603 }
21604 else if (id_equal (token->u.value, "final"))
21605 {
21606 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21607 virt_specifier = VIRT_SPEC_FINAL;
21608 }
21609 else if (id_equal (token->u.value, "__final"))
21610 {
21611 virt_specifier = VIRT_SPEC_FINAL;
21612 }
21613 else
21614 break;
21615
21616 if (virt_specifiers & virt_specifier)
21617 {
21618 gcc_rich_location richloc (token->location);
21619 richloc.add_fixit_remove ();
21620 error_at (&richloc, "duplicate virt-specifier");
21621 cp_lexer_purge_token (parser->lexer);
21622 }
21623 else
21624 {
21625 cp_lexer_consume_token (parser->lexer);
21626 virt_specifiers |= virt_specifier;
21627 }
21628 }
21629 return virt_specifiers;
21630 }
21631
21632 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21633 is in scope even though it isn't real. */
21634
21635 void
21636 inject_this_parameter (tree ctype, cp_cv_quals quals)
21637 {
21638 tree this_parm;
21639
21640 if (current_class_ptr)
21641 {
21642 /* We don't clear this between NSDMIs. Is it already what we want? */
21643 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21644 if (DECL_P (current_class_ptr)
21645 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21646 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21647 && cp_type_quals (type) == quals)
21648 return;
21649 }
21650
21651 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21652 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21653 current_class_ptr = NULL_TREE;
21654 current_class_ref
21655 = cp_build_fold_indirect_ref (this_parm);
21656 current_class_ptr = this_parm;
21657 }
21658
21659 /* Return true iff our current scope is a non-static data member
21660 initializer. */
21661
21662 bool
21663 parsing_nsdmi (void)
21664 {
21665 /* We recognize NSDMI context by the context-less 'this' pointer set up
21666 by the function above. */
21667 if (current_class_ptr
21668 && TREE_CODE (current_class_ptr) == PARM_DECL
21669 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21670 return true;
21671 return false;
21672 }
21673
21674 /* Parse a late-specified return type, if any. This is not a separate
21675 non-terminal, but part of a function declarator, which looks like
21676
21677 -> trailing-type-specifier-seq abstract-declarator(opt)
21678
21679 Returns the type indicated by the type-id.
21680
21681 In addition to this, parse any queued up #pragma omp declare simd
21682 clauses, and #pragma acc routine clauses.
21683
21684 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21685 function. */
21686
21687 static tree
21688 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21689 tree& requires_clause, cp_cv_quals quals)
21690 {
21691 cp_token *token;
21692 tree type = NULL_TREE;
21693 bool declare_simd_p = (parser->omp_declare_simd
21694 && declarator
21695 && declarator->kind == cdk_id);
21696
21697 bool oacc_routine_p = (parser->oacc_routine
21698 && declarator
21699 && declarator->kind == cdk_id);
21700
21701 /* Peek at the next token. */
21702 token = cp_lexer_peek_token (parser->lexer);
21703 /* A late-specified return type is indicated by an initial '->'. */
21704 if (token->type != CPP_DEREF
21705 && token->keyword != RID_REQUIRES
21706 && !(token->type == CPP_NAME
21707 && token->u.value == ridpointers[RID_REQUIRES])
21708 && !(declare_simd_p || oacc_routine_p))
21709 return NULL_TREE;
21710
21711 tree save_ccp = current_class_ptr;
21712 tree save_ccr = current_class_ref;
21713 if (quals >= 0)
21714 {
21715 /* DR 1207: 'this' is in scope in the trailing return type. */
21716 inject_this_parameter (current_class_type, quals);
21717 }
21718
21719 if (token->type == CPP_DEREF)
21720 {
21721 /* Consume the ->. */
21722 cp_lexer_consume_token (parser->lexer);
21723
21724 type = cp_parser_trailing_type_id (parser);
21725 }
21726
21727 /* Function declarations may be followed by a trailing
21728 requires-clause. */
21729 requires_clause = cp_parser_requires_clause_opt (parser);
21730
21731 if (declare_simd_p)
21732 declarator->attributes
21733 = cp_parser_late_parsing_omp_declare_simd (parser,
21734 declarator->attributes);
21735 if (oacc_routine_p)
21736 declarator->attributes
21737 = cp_parser_late_parsing_oacc_routine (parser,
21738 declarator->attributes);
21739
21740 if (quals >= 0)
21741 {
21742 current_class_ptr = save_ccp;
21743 current_class_ref = save_ccr;
21744 }
21745
21746 return type;
21747 }
21748
21749 /* Parse a declarator-id.
21750
21751 declarator-id:
21752 id-expression
21753 :: [opt] nested-name-specifier [opt] type-name
21754
21755 In the `id-expression' case, the value returned is as for
21756 cp_parser_id_expression if the id-expression was an unqualified-id.
21757 If the id-expression was a qualified-id, then a SCOPE_REF is
21758 returned. The first operand is the scope (either a NAMESPACE_DECL
21759 or TREE_TYPE), but the second is still just a representation of an
21760 unqualified-id. */
21761
21762 static tree
21763 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21764 {
21765 tree id;
21766 /* The expression must be an id-expression. Assume that qualified
21767 names are the names of types so that:
21768
21769 template <class T>
21770 int S<T>::R::i = 3;
21771
21772 will work; we must treat `S<T>::R' as the name of a type.
21773 Similarly, assume that qualified names are templates, where
21774 required, so that:
21775
21776 template <class T>
21777 int S<T>::R<T>::i = 3;
21778
21779 will work, too. */
21780 id = cp_parser_id_expression (parser,
21781 /*template_keyword_p=*/false,
21782 /*check_dependency_p=*/false,
21783 /*template_p=*/NULL,
21784 /*declarator_p=*/true,
21785 optional_p);
21786 if (id && BASELINK_P (id))
21787 id = BASELINK_FUNCTIONS (id);
21788 return id;
21789 }
21790
21791 /* Parse a type-id.
21792
21793 type-id:
21794 type-specifier-seq abstract-declarator [opt]
21795
21796 The parser flags FLAGS is used to control type-specifier parsing.
21797
21798 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
21799
21800 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21801 i.e. we've just seen "->".
21802
21803 Returns the TYPE specified. */
21804
21805 static tree
21806 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
21807 bool is_template_arg, bool is_trailing_return,
21808 location_t *type_location)
21809 {
21810 cp_decl_specifier_seq type_specifier_seq;
21811 cp_declarator *abstract_declarator;
21812
21813 /* Parse the type-specifier-seq. */
21814 cp_parser_type_specifier_seq (parser, flags,
21815 /*is_declaration=*/false,
21816 is_trailing_return,
21817 &type_specifier_seq);
21818 if (type_location)
21819 *type_location = type_specifier_seq.locations[ds_type_spec];
21820
21821 if (is_template_arg && type_specifier_seq.type
21822 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21823 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21824 /* A bare template name as a template argument is a template template
21825 argument, not a placeholder, so fail parsing it as a type argument. */
21826 {
21827 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21828 cp_parser_simulate_error (parser);
21829 return error_mark_node;
21830 }
21831 if (type_specifier_seq.type == error_mark_node)
21832 return error_mark_node;
21833
21834 /* There might or might not be an abstract declarator. */
21835 cp_parser_parse_tentatively (parser);
21836 /* Look for the declarator. */
21837 abstract_declarator
21838 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
21839 CP_PARSER_FLAGS_NONE, NULL,
21840 /*parenthesized_p=*/NULL,
21841 /*member_p=*/false,
21842 /*friend_p=*/false,
21843 /*static_p=*/false);
21844 /* Check to see if there really was a declarator. */
21845 if (!cp_parser_parse_definitely (parser))
21846 abstract_declarator = NULL;
21847
21848 if (type_specifier_seq.type
21849 /* The concepts TS allows 'auto' as a type-id. */
21850 && (!flag_concepts || parser->in_type_id_in_expr_p)
21851 /* None of the valid uses of 'auto' in C++14 involve the type-id
21852 nonterminal, but it is valid in a trailing-return-type. */
21853 && !(cxx_dialect >= cxx14 && is_trailing_return))
21854 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21855 {
21856 /* A type-id with type 'auto' is only ok if the abstract declarator
21857 is a function declarator with a late-specified return type.
21858
21859 A type-id with 'auto' is also valid in a trailing-return-type
21860 in a compound-requirement. */
21861 if (abstract_declarator
21862 && abstract_declarator->kind == cdk_function
21863 && abstract_declarator->u.function.late_return_type)
21864 /* OK */;
21865 else if (parser->in_result_type_constraint_p)
21866 /* OK */;
21867 else
21868 {
21869 location_t loc = type_specifier_seq.locations[ds_type_spec];
21870 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21871 {
21872 error_at (loc, "missing template arguments after %qT",
21873 auto_node);
21874 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21875 tmpl);
21876 }
21877 else
21878 error_at (loc, "invalid use of %qT", auto_node);
21879 return error_mark_node;
21880 }
21881 }
21882
21883 return groktypename (&type_specifier_seq, abstract_declarator,
21884 is_template_arg);
21885 }
21886
21887 /* Wrapper for cp_parser_type_id_1. */
21888
21889 static tree
21890 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
21891 location_t *type_location)
21892 {
21893 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
21894 }
21895
21896 /* Wrapper for cp_parser_type_id_1. */
21897
21898 static tree
21899 cp_parser_template_type_arg (cp_parser *parser)
21900 {
21901 tree r;
21902 const char *saved_message = parser->type_definition_forbidden_message;
21903 parser->type_definition_forbidden_message
21904 = G_("types may not be defined in template arguments");
21905 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
21906 parser->type_definition_forbidden_message = saved_message;
21907 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21908 {
21909 error ("invalid use of %<auto%> in template argument");
21910 r = error_mark_node;
21911 }
21912 return r;
21913 }
21914
21915 /* Wrapper for cp_parser_type_id_1. */
21916
21917 static tree
21918 cp_parser_trailing_type_id (cp_parser *parser)
21919 {
21920 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
21921 false, true, NULL);
21922 }
21923
21924 /* Parse a type-specifier-seq.
21925
21926 type-specifier-seq:
21927 type-specifier type-specifier-seq [opt]
21928
21929 GNU extension:
21930
21931 type-specifier-seq:
21932 attributes type-specifier-seq [opt]
21933
21934 The parser flags FLAGS is used to control type-specifier parsing.
21935
21936 If IS_DECLARATION is true, we are at the start of a "condition" or
21937 exception-declaration, so we might be followed by a declarator-id.
21938
21939 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21940 i.e. we've just seen "->".
21941
21942 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21943
21944 static void
21945 cp_parser_type_specifier_seq (cp_parser* parser,
21946 cp_parser_flags flags,
21947 bool is_declaration,
21948 bool is_trailing_return,
21949 cp_decl_specifier_seq *type_specifier_seq)
21950 {
21951 bool seen_type_specifier = false;
21952 cp_token *start_token = NULL;
21953
21954 /* Clear the TYPE_SPECIFIER_SEQ. */
21955 clear_decl_specs (type_specifier_seq);
21956
21957 flags |= CP_PARSER_FLAGS_OPTIONAL;
21958 /* In the context of a trailing return type, enum E { } is an
21959 elaborated-type-specifier followed by a function-body, not an
21960 enum-specifier. */
21961 if (is_trailing_return)
21962 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21963
21964 /* Parse the type-specifiers and attributes. */
21965 while (true)
21966 {
21967 tree type_specifier;
21968 bool is_cv_qualifier;
21969
21970 /* Check for attributes first. */
21971 if (cp_next_tokens_can_be_attribute_p (parser))
21972 {
21973 type_specifier_seq->attributes
21974 = attr_chainon (type_specifier_seq->attributes,
21975 cp_parser_attributes_opt (parser));
21976 continue;
21977 }
21978
21979 /* record the token of the beginning of the type specifier seq,
21980 for error reporting purposes*/
21981 if (!start_token)
21982 start_token = cp_lexer_peek_token (parser->lexer);
21983
21984 /* Look for the type-specifier. */
21985 type_specifier = cp_parser_type_specifier (parser,
21986 flags,
21987 type_specifier_seq,
21988 /*is_declaration=*/false,
21989 NULL,
21990 &is_cv_qualifier);
21991 if (!type_specifier)
21992 {
21993 /* If the first type-specifier could not be found, this is not a
21994 type-specifier-seq at all. */
21995 if (!seen_type_specifier)
21996 {
21997 /* Set in_declarator_p to avoid skipping to the semicolon. */
21998 int in_decl = parser->in_declarator_p;
21999 parser->in_declarator_p = true;
22000
22001 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
22002 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
22003 cp_parser_error (parser, "expected type-specifier");
22004
22005 parser->in_declarator_p = in_decl;
22006
22007 type_specifier_seq->type = error_mark_node;
22008 return;
22009 }
22010 /* If subsequent type-specifiers could not be found, the
22011 type-specifier-seq is complete. */
22012 break;
22013 }
22014
22015 seen_type_specifier = true;
22016 /* The standard says that a condition can be:
22017
22018 type-specifier-seq declarator = assignment-expression
22019
22020 However, given:
22021
22022 struct S {};
22023 if (int S = ...)
22024
22025 we should treat the "S" as a declarator, not as a
22026 type-specifier. The standard doesn't say that explicitly for
22027 type-specifier-seq, but it does say that for
22028 decl-specifier-seq in an ordinary declaration. Perhaps it
22029 would be clearer just to allow a decl-specifier-seq here, and
22030 then add a semantic restriction that if any decl-specifiers
22031 that are not type-specifiers appear, the program is invalid. */
22032 if (is_declaration && !is_cv_qualifier)
22033 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
22034 }
22035 }
22036
22037 /* Return whether the function currently being declared has an associated
22038 template parameter list. */
22039
22040 static bool
22041 function_being_declared_is_template_p (cp_parser* parser)
22042 {
22043 if (!current_template_parms || processing_template_parmlist)
22044 return false;
22045
22046 if (parser->implicit_template_scope)
22047 return true;
22048
22049 if (at_class_scope_p ()
22050 && TYPE_BEING_DEFINED (current_class_type))
22051 return parser->num_template_parameter_lists != 0;
22052
22053 return ((int) parser->num_template_parameter_lists > template_class_depth
22054 (current_class_type));
22055 }
22056
22057 /* Parse a parameter-declaration-clause.
22058
22059 parameter-declaration-clause:
22060 parameter-declaration-list [opt] ... [opt]
22061 parameter-declaration-list , ...
22062
22063 The parser flags FLAGS is used to control type-specifier parsing.
22064
22065 Returns a representation for the parameter declarations. A return
22066 value of NULL indicates a parameter-declaration-clause consisting
22067 only of an ellipsis. */
22068
22069 static tree
22070 cp_parser_parameter_declaration_clause (cp_parser* parser,
22071 cp_parser_flags flags)
22072 {
22073 tree parameters;
22074 cp_token *token;
22075 bool ellipsis_p;
22076
22077 temp_override<bool> cleanup
22078 (parser->auto_is_implicit_function_template_parm_p);
22079
22080 if (!processing_specialization
22081 && !processing_template_parmlist
22082 && !processing_explicit_instantiation
22083 /* default_arg_ok_p tracks whether this is a parameter-clause for an
22084 actual function or a random abstract declarator. */
22085 && parser->default_arg_ok_p)
22086 if (!current_function_decl
22087 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
22088 parser->auto_is_implicit_function_template_parm_p = true;
22089
22090 /* Peek at the next token. */
22091 token = cp_lexer_peek_token (parser->lexer);
22092 /* Check for trivial parameter-declaration-clauses. */
22093 if (token->type == CPP_ELLIPSIS)
22094 {
22095 /* Consume the `...' token. */
22096 cp_lexer_consume_token (parser->lexer);
22097 return NULL_TREE;
22098 }
22099 else if (token->type == CPP_CLOSE_PAREN)
22100 /* There are no parameters. */
22101 return void_list_node;
22102 /* Check for `(void)', too, which is a special case. */
22103 else if (token->keyword == RID_VOID
22104 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22105 == CPP_CLOSE_PAREN))
22106 {
22107 /* Consume the `void' token. */
22108 cp_lexer_consume_token (parser->lexer);
22109 /* There are no parameters. */
22110 return void_list_node;
22111 }
22112
22113 /* Parse the parameter-declaration-list. */
22114 parameters = cp_parser_parameter_declaration_list (parser, flags);
22115 /* If a parse error occurred while parsing the
22116 parameter-declaration-list, then the entire
22117 parameter-declaration-clause is erroneous. */
22118 if (parameters == error_mark_node)
22119 return NULL_TREE;
22120
22121 /* Peek at the next token. */
22122 token = cp_lexer_peek_token (parser->lexer);
22123 /* If it's a `,', the clause should terminate with an ellipsis. */
22124 if (token->type == CPP_COMMA)
22125 {
22126 /* Consume the `,'. */
22127 cp_lexer_consume_token (parser->lexer);
22128 /* Expect an ellipsis. */
22129 ellipsis_p
22130 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
22131 }
22132 /* It might also be `...' if the optional trailing `,' was
22133 omitted. */
22134 else if (token->type == CPP_ELLIPSIS)
22135 {
22136 /* Consume the `...' token. */
22137 cp_lexer_consume_token (parser->lexer);
22138 /* And remember that we saw it. */
22139 ellipsis_p = true;
22140 }
22141 else
22142 ellipsis_p = false;
22143
22144 /* Finish the parameter list. */
22145 if (!ellipsis_p)
22146 parameters = chainon (parameters, void_list_node);
22147
22148 return parameters;
22149 }
22150
22151 /* Parse a parameter-declaration-list.
22152
22153 parameter-declaration-list:
22154 parameter-declaration
22155 parameter-declaration-list , parameter-declaration
22156
22157 The parser flags FLAGS is used to control type-specifier parsing.
22158
22159 Returns a representation of the parameter-declaration-list, as for
22160 cp_parser_parameter_declaration_clause. However, the
22161 `void_list_node' is never appended to the list. */
22162
22163 static tree
22164 cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
22165 {
22166 tree parameters = NULL_TREE;
22167 tree *tail = &parameters;
22168 bool saved_in_unbraced_linkage_specification_p;
22169 int index = 0;
22170
22171 /* The special considerations that apply to a function within an
22172 unbraced linkage specifications do not apply to the parameters
22173 to the function. */
22174 saved_in_unbraced_linkage_specification_p
22175 = parser->in_unbraced_linkage_specification_p;
22176 parser->in_unbraced_linkage_specification_p = false;
22177
22178 /* Look for more parameters. */
22179 while (true)
22180 {
22181 cp_parameter_declarator *parameter;
22182 tree decl = error_mark_node;
22183 bool parenthesized_p = false;
22184
22185 /* Parse the parameter. */
22186 parameter
22187 = cp_parser_parameter_declaration (parser, flags,
22188 /*template_parm_p=*/false,
22189 &parenthesized_p);
22190
22191 /* We don't know yet if the enclosing context is deprecated, so wait
22192 and warn in grokparms if appropriate. */
22193 deprecated_state = DEPRECATED_SUPPRESS;
22194
22195 if (parameter)
22196 {
22197 decl = grokdeclarator (parameter->declarator,
22198 &parameter->decl_specifiers,
22199 PARM,
22200 parameter->default_argument != NULL_TREE,
22201 &parameter->decl_specifiers.attributes);
22202 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
22203 DECL_SOURCE_LOCATION (decl) = parameter->loc;
22204 }
22205
22206 deprecated_state = DEPRECATED_NORMAL;
22207
22208 /* If a parse error occurred parsing the parameter declaration,
22209 then the entire parameter-declaration-list is erroneous. */
22210 if (decl == error_mark_node)
22211 {
22212 parameters = error_mark_node;
22213 break;
22214 }
22215
22216 if (parameter->decl_specifiers.attributes)
22217 cplus_decl_attributes (&decl,
22218 parameter->decl_specifiers.attributes,
22219 0);
22220 if (DECL_NAME (decl))
22221 decl = pushdecl (decl);
22222
22223 if (decl != error_mark_node)
22224 {
22225 retrofit_lang_decl (decl);
22226 DECL_PARM_INDEX (decl) = ++index;
22227 DECL_PARM_LEVEL (decl) = function_parm_depth ();
22228 }
22229
22230 /* Add the new parameter to the list. */
22231 *tail = build_tree_list (parameter->default_argument, decl);
22232 tail = &TREE_CHAIN (*tail);
22233
22234 /* Peek at the next token. */
22235 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
22236 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
22237 /* These are for Objective-C++ */
22238 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22239 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22240 /* The parameter-declaration-list is complete. */
22241 break;
22242 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22243 {
22244 cp_token *token;
22245
22246 /* Peek at the next token. */
22247 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22248 /* If it's an ellipsis, then the list is complete. */
22249 if (token->type == CPP_ELLIPSIS)
22250 break;
22251 /* Otherwise, there must be more parameters. Consume the
22252 `,'. */
22253 cp_lexer_consume_token (parser->lexer);
22254 /* When parsing something like:
22255
22256 int i(float f, double d)
22257
22258 we can tell after seeing the declaration for "f" that we
22259 are not looking at an initialization of a variable "i",
22260 but rather at the declaration of a function "i".
22261
22262 Due to the fact that the parsing of template arguments
22263 (as specified to a template-id) requires backtracking we
22264 cannot use this technique when inside a template argument
22265 list. */
22266 if (!parser->in_template_argument_list_p
22267 && !parser->in_type_id_in_expr_p
22268 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22269 /* However, a parameter-declaration of the form
22270 "float(f)" (which is a valid declaration of a
22271 parameter "f") can also be interpreted as an
22272 expression (the conversion of "f" to "float"). */
22273 && !parenthesized_p)
22274 cp_parser_commit_to_tentative_parse (parser);
22275 }
22276 else
22277 {
22278 cp_parser_error (parser, "expected %<,%> or %<...%>");
22279 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
22280 cp_parser_skip_to_closing_parenthesis (parser,
22281 /*recovering=*/true,
22282 /*or_comma=*/false,
22283 /*consume_paren=*/false);
22284 break;
22285 }
22286 }
22287
22288 parser->in_unbraced_linkage_specification_p
22289 = saved_in_unbraced_linkage_specification_p;
22290
22291 /* Reset implicit_template_scope if we are about to leave the function
22292 parameter list that introduced it. Note that for out-of-line member
22293 definitions, there will be one or more class scopes before we get to
22294 the template parameter scope. */
22295
22296 if (cp_binding_level *its = parser->implicit_template_scope)
22297 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
22298 {
22299 while (maybe_its->kind == sk_class)
22300 maybe_its = maybe_its->level_chain;
22301 if (maybe_its == its)
22302 {
22303 parser->implicit_template_parms = 0;
22304 parser->implicit_template_scope = 0;
22305 }
22306 }
22307
22308 return parameters;
22309 }
22310
22311 /* Parse a parameter declaration.
22312
22313 parameter-declaration:
22314 decl-specifier-seq ... [opt] declarator
22315 decl-specifier-seq declarator = assignment-expression
22316 decl-specifier-seq ... [opt] abstract-declarator [opt]
22317 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22318
22319 The parser flags FLAGS is used to control type-specifier parsing.
22320
22321 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22322 declares a template parameter. (In that case, a non-nested `>'
22323 token encountered during the parsing of the assignment-expression
22324 is not interpreted as a greater-than operator.)
22325
22326 Returns a representation of the parameter, or NULL if an error
22327 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22328 true iff the declarator is of the form "(p)". */
22329
22330 static cp_parameter_declarator *
22331 cp_parser_parameter_declaration (cp_parser *parser,
22332 cp_parser_flags flags,
22333 bool template_parm_p,
22334 bool *parenthesized_p)
22335 {
22336 int declares_class_or_enum;
22337 cp_decl_specifier_seq decl_specifiers;
22338 cp_declarator *declarator;
22339 tree default_argument;
22340 cp_token *token = NULL, *declarator_token_start = NULL;
22341 const char *saved_message;
22342 bool template_parameter_pack_p = false;
22343
22344 /* In a template parameter, `>' is not an operator.
22345
22346 [temp.param]
22347
22348 When parsing a default template-argument for a non-type
22349 template-parameter, the first non-nested `>' is taken as the end
22350 of the template parameter-list rather than a greater-than
22351 operator. */
22352
22353 /* Type definitions may not appear in parameter types. */
22354 saved_message = parser->type_definition_forbidden_message;
22355 parser->type_definition_forbidden_message
22356 = G_("types may not be defined in parameter types");
22357
22358 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
22359 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22360 (current_template_parms)) : 0);
22361
22362 /* Parse the declaration-specifiers. */
22363 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22364 cp_parser_decl_specifier_seq (parser,
22365 flags,
22366 &decl_specifiers,
22367 &declares_class_or_enum);
22368
22369 /* Complain about missing 'typename' or other invalid type names. */
22370 if (!decl_specifiers.any_type_specifiers_p
22371 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22372 decl_specifiers.type = error_mark_node;
22373
22374 /* If an error occurred, there's no reason to attempt to parse the
22375 rest of the declaration. */
22376 if (cp_parser_error_occurred (parser))
22377 {
22378 parser->type_definition_forbidden_message = saved_message;
22379 return NULL;
22380 }
22381
22382 /* Peek at the next token. */
22383 token = cp_lexer_peek_token (parser->lexer);
22384
22385 /* If the next token is a `)', `,', `=', `>', or `...', then there
22386 is no declarator. However, when variadic templates are enabled,
22387 there may be a declarator following `...'. */
22388 if (token->type == CPP_CLOSE_PAREN
22389 || token->type == CPP_COMMA
22390 || token->type == CPP_EQ
22391 || token->type == CPP_GREATER)
22392 {
22393 declarator = NULL;
22394 if (parenthesized_p)
22395 *parenthesized_p = false;
22396 }
22397 /* Otherwise, there should be a declarator. */
22398 else
22399 {
22400 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22401 parser->default_arg_ok_p = false;
22402
22403 /* After seeing a decl-specifier-seq, if the next token is not a
22404 "(", there is no possibility that the code is a valid
22405 expression. Therefore, if parsing tentatively, we commit at
22406 this point. */
22407 if (!parser->in_template_argument_list_p
22408 /* In an expression context, having seen:
22409
22410 (int((char ...
22411
22412 we cannot be sure whether we are looking at a
22413 function-type (taking a "char" as a parameter) or a cast
22414 of some object of type "char" to "int". */
22415 && !parser->in_type_id_in_expr_p
22416 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22417 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22418 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
22419 cp_parser_commit_to_tentative_parse (parser);
22420 /* Parse the declarator. */
22421 declarator_token_start = token;
22422 declarator = cp_parser_declarator (parser,
22423 CP_PARSER_DECLARATOR_EITHER,
22424 CP_PARSER_FLAGS_NONE,
22425 /*ctor_dtor_or_conv_p=*/NULL,
22426 parenthesized_p,
22427 /*member_p=*/false,
22428 /*friend_p=*/false,
22429 /*static_p=*/false);
22430 parser->default_arg_ok_p = saved_default_arg_ok_p;
22431 /* After the declarator, allow more attributes. */
22432 decl_specifiers.attributes
22433 = attr_chainon (decl_specifiers.attributes,
22434 cp_parser_attributes_opt (parser));
22435
22436 /* If the declarator is a template parameter pack, remember that and
22437 clear the flag in the declarator itself so we don't get errors
22438 from grokdeclarator. */
22439 if (template_parm_p && declarator && declarator->parameter_pack_p)
22440 {
22441 declarator->parameter_pack_p = false;
22442 template_parameter_pack_p = true;
22443 }
22444 }
22445
22446 /* If the next token is an ellipsis, and we have not seen a declarator
22447 name, and if either the type of the declarator contains parameter
22448 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22449 for, eg, abbreviated integral type names), then we actually have a
22450 parameter pack expansion expression. Otherwise, leave the ellipsis
22451 for a C-style variadic function. */
22452 token = cp_lexer_peek_token (parser->lexer);
22453
22454 /* If a function parameter pack was specified and an implicit template
22455 parameter was introduced during cp_parser_parameter_declaration,
22456 change any implicit parameters introduced into packs. */
22457 if (parser->implicit_template_parms
22458 && ((token->type == CPP_ELLIPSIS
22459 && declarator_can_be_parameter_pack (declarator))
22460 || (declarator && declarator->parameter_pack_p)))
22461 {
22462 int latest_template_parm_idx = TREE_VEC_LENGTH
22463 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
22464
22465 if (latest_template_parm_idx != template_parm_idx)
22466 decl_specifiers.type = convert_generic_types_to_packs
22467 (decl_specifiers.type,
22468 template_parm_idx, latest_template_parm_idx);
22469 }
22470
22471 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22472 {
22473 tree type = decl_specifiers.type;
22474
22475 if (type && DECL_P (type))
22476 type = TREE_TYPE (type);
22477
22478 if (((type
22479 && TREE_CODE (type) != TYPE_PACK_EXPANSION
22480 && (template_parm_p || uses_parameter_packs (type)))
22481 || (!type && template_parm_p))
22482 && declarator_can_be_parameter_pack (declarator))
22483 {
22484 /* Consume the `...'. */
22485 cp_lexer_consume_token (parser->lexer);
22486 maybe_warn_variadic_templates ();
22487
22488 /* Build a pack expansion type */
22489 if (template_parm_p)
22490 template_parameter_pack_p = true;
22491 else if (declarator)
22492 declarator->parameter_pack_p = true;
22493 else
22494 decl_specifiers.type = make_pack_expansion (type);
22495 }
22496 }
22497
22498 /* The restriction on defining new types applies only to the type
22499 of the parameter, not to the default argument. */
22500 parser->type_definition_forbidden_message = saved_message;
22501
22502 /* If the next token is `=', then process a default argument. */
22503 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22504 {
22505 tree type = decl_specifiers.type;
22506 token = cp_lexer_peek_token (parser->lexer);
22507 /* If we are defining a class, then the tokens that make up the
22508 default argument must be saved and processed later. */
22509 if (!template_parm_p && at_class_scope_p ()
22510 && TYPE_BEING_DEFINED (current_class_type)
22511 && !LAMBDA_TYPE_P (current_class_type))
22512 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
22513
22514 // A constrained-type-specifier may declare a type template-parameter.
22515 else if (declares_constrained_type_template_parameter (type))
22516 default_argument
22517 = cp_parser_default_type_template_argument (parser);
22518
22519 // A constrained-type-specifier may declare a template-template-parameter.
22520 else if (declares_constrained_template_template_parameter (type))
22521 default_argument
22522 = cp_parser_default_template_template_argument (parser);
22523
22524 /* Outside of a class definition, we can just parse the
22525 assignment-expression. */
22526 else
22527 default_argument
22528 = cp_parser_default_argument (parser, template_parm_p);
22529
22530 if (!parser->default_arg_ok_p)
22531 {
22532 permerror (token->location,
22533 "default arguments are only "
22534 "permitted for function parameters");
22535 }
22536 else if ((declarator && declarator->parameter_pack_p)
22537 || template_parameter_pack_p
22538 || (decl_specifiers.type
22539 && PACK_EXPANSION_P (decl_specifiers.type)))
22540 {
22541 /* Find the name of the parameter pack. */
22542 cp_declarator *id_declarator = declarator;
22543 while (id_declarator && id_declarator->kind != cdk_id)
22544 id_declarator = id_declarator->declarator;
22545
22546 if (id_declarator && id_declarator->kind == cdk_id)
22547 error_at (declarator_token_start->location,
22548 template_parm_p
22549 ? G_("template parameter pack %qD "
22550 "cannot have a default argument")
22551 : G_("parameter pack %qD cannot have "
22552 "a default argument"),
22553 id_declarator->u.id.unqualified_name);
22554 else
22555 error_at (declarator_token_start->location,
22556 template_parm_p
22557 ? G_("template parameter pack cannot have "
22558 "a default argument")
22559 : G_("parameter pack cannot have a "
22560 "default argument"));
22561
22562 default_argument = NULL_TREE;
22563 }
22564 }
22565 else
22566 default_argument = NULL_TREE;
22567
22568 if (default_argument)
22569 STRIP_ANY_LOCATION_WRAPPER (default_argument);
22570
22571 /* Generate a location for the parameter, ranging from the start of the
22572 initial token to the end of the final token (using input_location for
22573 the latter, set up by cp_lexer_set_source_position_from_token when
22574 consuming tokens).
22575
22576 If we have a identifier, then use it for the caret location, e.g.
22577
22578 extern int callee (int one, int (*two)(int, int), float three);
22579 ~~~~~~^~~~~~~~~~~~~~
22580
22581 otherwise, reuse the start location for the caret location e.g.:
22582
22583 extern int callee (int one, int (*)(int, int), float three);
22584 ^~~~~~~~~~~~~~~~~
22585
22586 */
22587 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22588 ? declarator->id_loc
22589 : decl_spec_token_start->location);
22590 location_t param_loc = make_location (caret_loc,
22591 decl_spec_token_start->location,
22592 input_location);
22593
22594 return make_parameter_declarator (&decl_specifiers,
22595 declarator,
22596 default_argument,
22597 param_loc,
22598 template_parameter_pack_p);
22599 }
22600
22601 /* Parse a default argument and return it.
22602
22603 TEMPLATE_PARM_P is true if this is a default argument for a
22604 non-type template parameter. */
22605 static tree
22606 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22607 {
22608 tree default_argument = NULL_TREE;
22609 bool saved_greater_than_is_operator_p;
22610 unsigned char saved_local_variables_forbidden_p;
22611 bool non_constant_p, is_direct_init;
22612
22613 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22614 set correctly. */
22615 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22616 parser->greater_than_is_operator_p = !template_parm_p;
22617 /* Local variable names (and the `this' keyword) may not
22618 appear in a default argument. */
22619 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22620 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
22621 /* Parse the assignment-expression. */
22622 if (template_parm_p)
22623 push_deferring_access_checks (dk_no_deferred);
22624 tree saved_class_ptr = NULL_TREE;
22625 tree saved_class_ref = NULL_TREE;
22626 /* The "this" pointer is not valid in a default argument. */
22627 if (cfun)
22628 {
22629 saved_class_ptr = current_class_ptr;
22630 cp_function_chain->x_current_class_ptr = NULL_TREE;
22631 saved_class_ref = current_class_ref;
22632 cp_function_chain->x_current_class_ref = NULL_TREE;
22633 }
22634 default_argument
22635 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22636 /* Restore the "this" pointer. */
22637 if (cfun)
22638 {
22639 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22640 cp_function_chain->x_current_class_ref = saved_class_ref;
22641 }
22642 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22643 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22644 if (template_parm_p)
22645 pop_deferring_access_checks ();
22646 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22647 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22648
22649 return default_argument;
22650 }
22651
22652 /* Parse a function-body.
22653
22654 function-body:
22655 compound_statement */
22656
22657 static void
22658 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22659 {
22660 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22661 ? BCS_TRY_BLOCK : BCS_NORMAL),
22662 true);
22663 }
22664
22665 /* Parse a ctor-initializer-opt followed by a function-body. Return
22666 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22667 is true we are parsing a function-try-block. */
22668
22669 static void
22670 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22671 bool in_function_try_block)
22672 {
22673 tree body, list;
22674 const bool check_body_p
22675 = (DECL_CONSTRUCTOR_P (current_function_decl)
22676 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
22677 tree last = NULL;
22678
22679 if (in_function_try_block
22680 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
22681 && cxx_dialect < cxx2a)
22682 {
22683 if (DECL_CONSTRUCTOR_P (current_function_decl))
22684 pedwarn (input_location, 0,
22685 "function-try-block body of %<constexpr%> constructor only "
22686 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22687 else
22688 pedwarn (input_location, 0,
22689 "function-try-block body of %<constexpr%> function only "
22690 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22691 }
22692
22693 /* Begin the function body. */
22694 body = begin_function_body ();
22695 /* Parse the optional ctor-initializer. */
22696 cp_parser_ctor_initializer_opt (parser);
22697
22698 /* If we're parsing a constexpr constructor definition, we need
22699 to check that the constructor body is indeed empty. However,
22700 before we get to cp_parser_function_body lot of junk has been
22701 generated, so we can't just check that we have an empty block.
22702 Rather we take a snapshot of the outermost block, and check whether
22703 cp_parser_function_body changed its state. */
22704 if (check_body_p)
22705 {
22706 list = cur_stmt_list;
22707 if (STATEMENT_LIST_TAIL (list))
22708 last = STATEMENT_LIST_TAIL (list)->stmt;
22709 }
22710 /* Parse the function-body. */
22711 cp_parser_function_body (parser, in_function_try_block);
22712 if (check_body_p)
22713 check_constexpr_ctor_body (last, list, /*complain=*/true);
22714 /* Finish the function body. */
22715 finish_function_body (body);
22716 }
22717
22718 /* Parse an initializer.
22719
22720 initializer:
22721 = initializer-clause
22722 ( expression-list )
22723
22724 Returns an expression representing the initializer. If no
22725 initializer is present, NULL_TREE is returned.
22726
22727 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22728 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22729 set to TRUE if there is no initializer present. If there is an
22730 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22731 is set to true; otherwise it is set to false. */
22732
22733 static tree
22734 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22735 bool* non_constant_p, bool subexpression_p)
22736 {
22737 cp_token *token;
22738 tree init;
22739
22740 /* Peek at the next token. */
22741 token = cp_lexer_peek_token (parser->lexer);
22742
22743 /* Let our caller know whether or not this initializer was
22744 parenthesized. */
22745 *is_direct_init = (token->type != CPP_EQ);
22746 /* Assume that the initializer is constant. */
22747 *non_constant_p = false;
22748
22749 if (token->type == CPP_EQ)
22750 {
22751 /* Consume the `='. */
22752 cp_lexer_consume_token (parser->lexer);
22753 /* Parse the initializer-clause. */
22754 init = cp_parser_initializer_clause (parser, non_constant_p);
22755 }
22756 else if (token->type == CPP_OPEN_PAREN)
22757 {
22758 vec<tree, va_gc> *vec;
22759 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22760 /*cast_p=*/false,
22761 /*allow_expansion_p=*/true,
22762 non_constant_p);
22763 if (vec == NULL)
22764 return error_mark_node;
22765 init = build_tree_list_vec (vec);
22766 release_tree_vector (vec);
22767 }
22768 else if (token->type == CPP_OPEN_BRACE)
22769 {
22770 cp_lexer_set_source_position (parser->lexer);
22771 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22772 init = cp_parser_braced_list (parser, non_constant_p);
22773 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22774 }
22775 else
22776 {
22777 /* Anything else is an error. */
22778 cp_parser_error (parser, "expected initializer");
22779 init = error_mark_node;
22780 }
22781
22782 if (!subexpression_p && check_for_bare_parameter_packs (init))
22783 init = error_mark_node;
22784
22785 return init;
22786 }
22787
22788 /* Parse an initializer-clause.
22789
22790 initializer-clause:
22791 assignment-expression
22792 braced-init-list
22793
22794 Returns an expression representing the initializer.
22795
22796 If the `assignment-expression' production is used the value
22797 returned is simply a representation for the expression.
22798
22799 Otherwise, calls cp_parser_braced_list. */
22800
22801 static cp_expr
22802 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22803 {
22804 cp_expr initializer;
22805
22806 /* Assume the expression is constant. */
22807 *non_constant_p = false;
22808
22809 /* If it is not a `{', then we are looking at an
22810 assignment-expression. */
22811 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22812 {
22813 initializer
22814 = cp_parser_constant_expression (parser,
22815 /*allow_non_constant_p=*/true,
22816 non_constant_p);
22817 }
22818 else
22819 initializer = cp_parser_braced_list (parser, non_constant_p);
22820
22821 return initializer;
22822 }
22823
22824 /* Parse a brace-enclosed initializer list.
22825
22826 braced-init-list:
22827 { initializer-list , [opt] }
22828 { designated-initializer-list , [opt] }
22829 { }
22830
22831 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22832 the elements of the initializer-list (or NULL, if the last
22833 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22834 NULL_TREE. There is no way to detect whether or not the optional
22835 trailing `,' was provided. NON_CONSTANT_P is as for
22836 cp_parser_initializer. */
22837
22838 static cp_expr
22839 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22840 {
22841 tree initializer;
22842 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22843
22844 /* Consume the `{' token. */
22845 matching_braces braces;
22846 braces.require_open (parser);
22847 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22848 initializer = make_node (CONSTRUCTOR);
22849 /* If it's not a `}', then there is a non-trivial initializer. */
22850 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22851 {
22852 bool designated;
22853 /* Parse the initializer list. */
22854 CONSTRUCTOR_ELTS (initializer)
22855 = cp_parser_initializer_list (parser, non_constant_p, &designated);
22856 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
22857 /* A trailing `,' token is allowed. */
22858 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22859 cp_lexer_consume_token (parser->lexer);
22860 }
22861 else
22862 *non_constant_p = false;
22863 /* Now, there should be a trailing `}'. */
22864 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22865 braces.require_close (parser);
22866 TREE_TYPE (initializer) = init_list_type_node;
22867
22868 cp_expr result (initializer);
22869 /* Build a location of the form:
22870 { ... }
22871 ^~~~~~~
22872 with caret==start at the open brace, finish at the close brace. */
22873 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22874 result.set_location (combined_loc);
22875 return result;
22876 }
22877
22878 /* Consume tokens up to, and including, the next non-nested closing `]'.
22879 Returns true iff we found a closing `]'. */
22880
22881 static bool
22882 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22883 {
22884 unsigned square_depth = 0;
22885
22886 while (true)
22887 {
22888 cp_token * token = cp_lexer_peek_token (parser->lexer);
22889
22890 switch (token->type)
22891 {
22892 case CPP_PRAGMA_EOL:
22893 if (!parser->lexer->in_pragma)
22894 break;
22895 /* FALLTHRU */
22896 case CPP_EOF:
22897 /* If we've run out of tokens, then there is no closing `]'. */
22898 return false;
22899
22900 case CPP_OPEN_SQUARE:
22901 ++square_depth;
22902 break;
22903
22904 case CPP_CLOSE_SQUARE:
22905 if (!square_depth--)
22906 {
22907 cp_lexer_consume_token (parser->lexer);
22908 return true;
22909 }
22910 break;
22911
22912 default:
22913 break;
22914 }
22915
22916 /* Consume the token. */
22917 cp_lexer_consume_token (parser->lexer);
22918 }
22919 }
22920
22921 /* Return true if we are looking at an array-designator, false otherwise. */
22922
22923 static bool
22924 cp_parser_array_designator_p (cp_parser *parser)
22925 {
22926 /* Consume the `['. */
22927 cp_lexer_consume_token (parser->lexer);
22928
22929 cp_lexer_save_tokens (parser->lexer);
22930
22931 /* Skip tokens until the next token is a closing square bracket.
22932 If we find the closing `]', and the next token is a `=', then
22933 we are looking at an array designator. */
22934 bool array_designator_p
22935 = (cp_parser_skip_to_closing_square_bracket (parser)
22936 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22937
22938 /* Roll back the tokens we skipped. */
22939 cp_lexer_rollback_tokens (parser->lexer);
22940
22941 return array_designator_p;
22942 }
22943
22944 /* Parse an initializer-list.
22945
22946 initializer-list:
22947 initializer-clause ... [opt]
22948 initializer-list , initializer-clause ... [opt]
22949
22950 C++2A Extension:
22951
22952 designated-initializer-list:
22953 designated-initializer-clause
22954 designated-initializer-list , designated-initializer-clause
22955
22956 designated-initializer-clause:
22957 designator brace-or-equal-initializer
22958
22959 designator:
22960 . identifier
22961
22962 GNU Extension:
22963
22964 initializer-list:
22965 designation initializer-clause ...[opt]
22966 initializer-list , designation initializer-clause ...[opt]
22967
22968 designation:
22969 . identifier =
22970 identifier :
22971 [ constant-expression ] =
22972
22973 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22974 for the initializer. If the INDEX of the elt is non-NULL, it is the
22975 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22976 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
22977 are any designators. */
22978
22979 static vec<constructor_elt, va_gc> *
22980 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
22981 bool *designated)
22982 {
22983 vec<constructor_elt, va_gc> *v = NULL;
22984 bool first_p = true;
22985 tree first_designator = NULL_TREE;
22986
22987 /* Assume all of the expressions are constant. */
22988 *non_constant_p = false;
22989
22990 /* Parse the rest of the list. */
22991 while (true)
22992 {
22993 cp_token *token;
22994 tree designator;
22995 tree initializer;
22996 bool clause_non_constant_p;
22997 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22998
22999 /* Handle the C++2A syntax, '. id ='. */
23000 if ((cxx_dialect >= cxx2a
23001 || cp_parser_allow_gnu_extensions_p (parser))
23002 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
23003 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
23004 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
23005 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
23006 == CPP_OPEN_BRACE)))
23007 {
23008 if (cxx_dialect < cxx2a)
23009 pedwarn (loc, OPT_Wpedantic,
23010 "C++ designated initializers only available with "
23011 "%<-std=c++2a%> or %<-std=gnu++2a%>");
23012 /* Consume the `.'. */
23013 cp_lexer_consume_token (parser->lexer);
23014 /* Consume the identifier. */
23015 designator = cp_lexer_consume_token (parser->lexer)->u.value;
23016 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23017 /* Consume the `='. */
23018 cp_lexer_consume_token (parser->lexer);
23019 }
23020 /* Also, if the next token is an identifier and the following one is a
23021 colon, we are looking at the GNU designated-initializer
23022 syntax. */
23023 else if (cp_parser_allow_gnu_extensions_p (parser)
23024 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
23025 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23026 == CPP_COLON))
23027 {
23028 /* Warn the user that they are using an extension. */
23029 pedwarn (loc, OPT_Wpedantic,
23030 "ISO C++ does not allow GNU designated initializers");
23031 /* Consume the identifier. */
23032 designator = cp_lexer_consume_token (parser->lexer)->u.value;
23033 /* Consume the `:'. */
23034 cp_lexer_consume_token (parser->lexer);
23035 }
23036 /* Also handle C99 array designators, '[ const ] ='. */
23037 else if (cp_parser_allow_gnu_extensions_p (parser)
23038 && !c_dialect_objc ()
23039 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23040 {
23041 /* In C++11, [ could start a lambda-introducer. */
23042 bool non_const = false;
23043
23044 cp_parser_parse_tentatively (parser);
23045
23046 if (!cp_parser_array_designator_p (parser))
23047 {
23048 cp_parser_simulate_error (parser);
23049 designator = NULL_TREE;
23050 }
23051 else
23052 {
23053 designator = cp_parser_constant_expression (parser, true,
23054 &non_const);
23055 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23056 cp_parser_require (parser, CPP_EQ, RT_EQ);
23057 }
23058
23059 if (!cp_parser_parse_definitely (parser))
23060 designator = NULL_TREE;
23061 else if (non_const
23062 && (!require_potential_rvalue_constant_expression
23063 (designator)))
23064 designator = NULL_TREE;
23065 if (designator)
23066 /* Warn the user that they are using an extension. */
23067 pedwarn (loc, OPT_Wpedantic,
23068 "ISO C++ does not allow C99 designated initializers");
23069 }
23070 else
23071 designator = NULL_TREE;
23072
23073 if (first_p)
23074 {
23075 first_designator = designator;
23076 first_p = false;
23077 }
23078 else if (cxx_dialect >= cxx2a
23079 && first_designator != error_mark_node
23080 && (!first_designator != !designator))
23081 {
23082 error_at (loc, "either all initializer clauses should be designated "
23083 "or none of them should be");
23084 first_designator = error_mark_node;
23085 }
23086 else if (cxx_dialect < cxx2a && !first_designator)
23087 first_designator = designator;
23088
23089 /* Parse the initializer. */
23090 initializer = cp_parser_initializer_clause (parser,
23091 &clause_non_constant_p);
23092 /* If any clause is non-constant, so is the entire initializer. */
23093 if (clause_non_constant_p)
23094 *non_constant_p = true;
23095
23096 /* If we have an ellipsis, this is an initializer pack
23097 expansion. */
23098 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23099 {
23100 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23101
23102 /* Consume the `...'. */
23103 cp_lexer_consume_token (parser->lexer);
23104
23105 if (designator && cxx_dialect >= cxx2a)
23106 error_at (loc,
23107 "%<...%> not allowed in designated initializer list");
23108
23109 /* Turn the initializer into an initializer expansion. */
23110 initializer = make_pack_expansion (initializer);
23111 }
23112
23113 /* Add it to the vector. */
23114 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
23115
23116 /* If the next token is not a comma, we have reached the end of
23117 the list. */
23118 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23119 break;
23120
23121 /* Peek at the next token. */
23122 token = cp_lexer_peek_nth_token (parser->lexer, 2);
23123 /* If the next token is a `}', then we're still done. An
23124 initializer-clause can have a trailing `,' after the
23125 initializer-list and before the closing `}'. */
23126 if (token->type == CPP_CLOSE_BRACE)
23127 break;
23128
23129 /* Consume the `,' token. */
23130 cp_lexer_consume_token (parser->lexer);
23131 }
23132
23133 /* The same identifier shall not appear in multiple designators
23134 of a designated-initializer-list. */
23135 if (first_designator)
23136 {
23137 unsigned int i;
23138 tree designator, val;
23139 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23140 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23141 {
23142 if (IDENTIFIER_MARKED (designator))
23143 {
23144 error_at (cp_expr_loc_or_loc (val, input_location),
23145 "%<.%s%> designator used multiple times in "
23146 "the same initializer list",
23147 IDENTIFIER_POINTER (designator));
23148 (*v)[i].index = error_mark_node;
23149 }
23150 else
23151 IDENTIFIER_MARKED (designator) = 1;
23152 }
23153 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23154 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23155 IDENTIFIER_MARKED (designator) = 0;
23156 }
23157
23158 *designated = first_designator != NULL_TREE;
23159 return v;
23160 }
23161
23162 /* Classes [gram.class] */
23163
23164 /* Parse a class-name.
23165
23166 class-name:
23167 identifier
23168 template-id
23169
23170 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23171 to indicate that names looked up in dependent types should be
23172 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23173 keyword has been used to indicate that the name that appears next
23174 is a template. TAG_TYPE indicates the explicit tag given before
23175 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23176 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23177 is the class being defined in a class-head. If ENUM_OK is TRUE,
23178 enum-names are also accepted.
23179
23180 Returns the TYPE_DECL representing the class. */
23181
23182 static tree
23183 cp_parser_class_name (cp_parser *parser,
23184 bool typename_keyword_p,
23185 bool template_keyword_p,
23186 enum tag_types tag_type,
23187 bool check_dependency_p,
23188 bool class_head_p,
23189 bool is_declaration,
23190 bool enum_ok)
23191 {
23192 tree decl;
23193 tree scope;
23194 bool typename_p;
23195 cp_token *token;
23196 tree identifier = NULL_TREE;
23197
23198 /* All class-names start with an identifier. */
23199 token = cp_lexer_peek_token (parser->lexer);
23200 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
23201 {
23202 cp_parser_error (parser, "expected class-name");
23203 return error_mark_node;
23204 }
23205
23206 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23207 to a template-id, so we save it here. */
23208 scope = parser->scope;
23209 if (scope == error_mark_node)
23210 return error_mark_node;
23211
23212 /* Any name names a type if we're following the `typename' keyword
23213 in a qualified name where the enclosing scope is type-dependent. */
23214 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
23215 && dependent_type_p (scope));
23216 /* Handle the common case (an identifier, but not a template-id)
23217 efficiently. */
23218 if (token->type == CPP_NAME
23219 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
23220 {
23221 cp_token *identifier_token;
23222 bool ambiguous_p;
23223
23224 /* Look for the identifier. */
23225 identifier_token = cp_lexer_peek_token (parser->lexer);
23226 ambiguous_p = identifier_token->error_reported;
23227 identifier = cp_parser_identifier (parser);
23228 /* If the next token isn't an identifier, we are certainly not
23229 looking at a class-name. */
23230 if (identifier == error_mark_node)
23231 decl = error_mark_node;
23232 /* If we know this is a type-name, there's no need to look it
23233 up. */
23234 else if (typename_p)
23235 decl = identifier;
23236 else
23237 {
23238 tree ambiguous_decls;
23239 /* If we already know that this lookup is ambiguous, then
23240 we've already issued an error message; there's no reason
23241 to check again. */
23242 if (ambiguous_p)
23243 {
23244 cp_parser_simulate_error (parser);
23245 return error_mark_node;
23246 }
23247 /* If the next token is a `::', then the name must be a type
23248 name.
23249
23250 [basic.lookup.qual]
23251
23252 During the lookup for a name preceding the :: scope
23253 resolution operator, object, function, and enumerator
23254 names are ignored. */
23255 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23256 tag_type = scope_type;
23257 /* Look up the name. */
23258 decl = cp_parser_lookup_name (parser, identifier,
23259 tag_type,
23260 /*is_template=*/false,
23261 /*is_namespace=*/false,
23262 check_dependency_p,
23263 &ambiguous_decls,
23264 identifier_token->location);
23265 if (ambiguous_decls)
23266 {
23267 if (cp_parser_parsing_tentatively (parser))
23268 cp_parser_simulate_error (parser);
23269 return error_mark_node;
23270 }
23271 }
23272 }
23273 else
23274 {
23275 /* Try a template-id. */
23276 decl = cp_parser_template_id (parser, template_keyword_p,
23277 check_dependency_p,
23278 tag_type,
23279 is_declaration);
23280 if (decl == error_mark_node)
23281 return error_mark_node;
23282 }
23283
23284 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
23285
23286 /* If this is a typename, create a TYPENAME_TYPE. */
23287 if (typename_p
23288 && decl != error_mark_node
23289 && !is_overloaded_fn (decl))
23290 {
23291 decl = make_typename_type (scope, decl, typename_type,
23292 /*complain=*/tf_error);
23293 if (decl != error_mark_node)
23294 decl = TYPE_NAME (decl);
23295 }
23296
23297 decl = strip_using_decl (decl);
23298
23299 /* Check to see that it is really the name of a class. */
23300 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
23301 && identifier_p (TREE_OPERAND (decl, 0))
23302 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23303 /* Situations like this:
23304
23305 template <typename T> struct A {
23306 typename T::template X<int>::I i;
23307 };
23308
23309 are problematic. Is `T::template X<int>' a class-name? The
23310 standard does not seem to be definitive, but there is no other
23311 valid interpretation of the following `::'. Therefore, those
23312 names are considered class-names. */
23313 {
23314 decl = make_typename_type (scope, decl, tag_type, tf_error);
23315 if (decl != error_mark_node)
23316 decl = TYPE_NAME (decl);
23317 }
23318 else if (TREE_CODE (decl) != TYPE_DECL
23319 || TREE_TYPE (decl) == error_mark_node
23320 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
23321 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
23322 /* In Objective-C 2.0, a classname followed by '.' starts a
23323 dot-syntax expression, and it's not a type-name. */
23324 || (c_dialect_objc ()
23325 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
23326 && objc_is_class_name (decl)))
23327 decl = error_mark_node;
23328
23329 if (decl == error_mark_node)
23330 cp_parser_error (parser, "expected class-name");
23331 else if (identifier && !parser->scope)
23332 maybe_note_name_used_in_class (identifier, decl);
23333
23334 return decl;
23335 }
23336
23337 /* Parse a class-specifier.
23338
23339 class-specifier:
23340 class-head { member-specification [opt] }
23341
23342 Returns the TREE_TYPE representing the class. */
23343
23344 static tree
23345 cp_parser_class_specifier_1 (cp_parser* parser)
23346 {
23347 tree type;
23348 tree attributes = NULL_TREE;
23349 bool nested_name_specifier_p;
23350 unsigned saved_num_template_parameter_lists;
23351 bool saved_in_function_body;
23352 unsigned char in_statement;
23353 bool in_switch_statement_p;
23354 bool saved_in_unbraced_linkage_specification_p;
23355 tree old_scope = NULL_TREE;
23356 tree scope = NULL_TREE;
23357 cp_token *closing_brace;
23358
23359 push_deferring_access_checks (dk_no_deferred);
23360
23361 /* Parse the class-head. */
23362 type = cp_parser_class_head (parser,
23363 &nested_name_specifier_p);
23364 /* If the class-head was a semantic disaster, skip the entire body
23365 of the class. */
23366 if (!type)
23367 {
23368 cp_parser_skip_to_end_of_block_or_statement (parser);
23369 pop_deferring_access_checks ();
23370 return error_mark_node;
23371 }
23372
23373 /* Look for the `{'. */
23374 matching_braces braces;
23375 if (!braces.require_open (parser))
23376 {
23377 pop_deferring_access_checks ();
23378 return error_mark_node;
23379 }
23380
23381 cp_ensure_no_omp_declare_simd (parser);
23382 cp_ensure_no_oacc_routine (parser);
23383
23384 /* Issue an error message if type-definitions are forbidden here. */
23385 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
23386 /* Remember that we are defining one more class. */
23387 ++parser->num_classes_being_defined;
23388 /* Inside the class, surrounding template-parameter-lists do not
23389 apply. */
23390 saved_num_template_parameter_lists
23391 = parser->num_template_parameter_lists;
23392 parser->num_template_parameter_lists = 0;
23393 /* We are not in a function body. */
23394 saved_in_function_body = parser->in_function_body;
23395 parser->in_function_body = false;
23396 /* Or in a loop. */
23397 in_statement = parser->in_statement;
23398 parser->in_statement = 0;
23399 /* Or in a switch. */
23400 in_switch_statement_p = parser->in_switch_statement_p;
23401 parser->in_switch_statement_p = false;
23402 /* We are not immediately inside an extern "lang" block. */
23403 saved_in_unbraced_linkage_specification_p
23404 = parser->in_unbraced_linkage_specification_p;
23405 parser->in_unbraced_linkage_specification_p = false;
23406
23407 // Associate constraints with the type.
23408 if (flag_concepts)
23409 type = associate_classtype_constraints (type);
23410
23411 /* Start the class. */
23412 if (nested_name_specifier_p)
23413 {
23414 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
23415 old_scope = push_inner_scope (scope);
23416 }
23417 type = begin_class_definition (type);
23418
23419 if (type == error_mark_node)
23420 /* If the type is erroneous, skip the entire body of the class. */
23421 cp_parser_skip_to_closing_brace (parser);
23422 else
23423 /* Parse the member-specification. */
23424 cp_parser_member_specification_opt (parser);
23425
23426 /* Look for the trailing `}'. */
23427 closing_brace = braces.require_close (parser);
23428 /* Look for trailing attributes to apply to this class. */
23429 if (cp_parser_allow_gnu_extensions_p (parser))
23430 attributes = cp_parser_gnu_attributes_opt (parser);
23431 if (type != error_mark_node)
23432 type = finish_struct (type, attributes);
23433 if (nested_name_specifier_p)
23434 pop_inner_scope (old_scope, scope);
23435
23436 /* We've finished a type definition. Check for the common syntax
23437 error of forgetting a semicolon after the definition. We need to
23438 be careful, as we can't just check for not-a-semicolon and be done
23439 with it; the user might have typed:
23440
23441 class X { } c = ...;
23442 class X { } *p = ...;
23443
23444 and so forth. Instead, enumerate all the possible tokens that
23445 might follow this production; if we don't see one of them, then
23446 complain and silently insert the semicolon. */
23447 {
23448 cp_token *token = cp_lexer_peek_token (parser->lexer);
23449 bool want_semicolon = true;
23450
23451 if (cp_next_tokens_can_be_std_attribute_p (parser))
23452 /* Don't try to parse c++11 attributes here. As per the
23453 grammar, that should be a task for
23454 cp_parser_decl_specifier_seq. */
23455 want_semicolon = false;
23456
23457 switch (token->type)
23458 {
23459 case CPP_NAME:
23460 case CPP_SEMICOLON:
23461 case CPP_MULT:
23462 case CPP_AND:
23463 case CPP_OPEN_PAREN:
23464 case CPP_CLOSE_PAREN:
23465 case CPP_COMMA:
23466 want_semicolon = false;
23467 break;
23468
23469 /* While it's legal for type qualifiers and storage class
23470 specifiers to follow type definitions in the grammar, only
23471 compiler testsuites contain code like that. Assume that if
23472 we see such code, then what we're really seeing is a case
23473 like:
23474
23475 class X { }
23476 const <type> var = ...;
23477
23478 or
23479
23480 class Y { }
23481 static <type> func (...) ...
23482
23483 i.e. the qualifier or specifier applies to the next
23484 declaration. To do so, however, we need to look ahead one
23485 more token to see if *that* token is a type specifier.
23486
23487 This code could be improved to handle:
23488
23489 class Z { }
23490 static const <type> var = ...; */
23491 case CPP_KEYWORD:
23492 if (keyword_is_decl_specifier (token->keyword))
23493 {
23494 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
23495
23496 /* Handling user-defined types here would be nice, but very
23497 tricky. */
23498 want_semicolon
23499 = (lookahead->type == CPP_KEYWORD
23500 && keyword_begins_type_specifier (lookahead->keyword));
23501 }
23502 break;
23503 default:
23504 break;
23505 }
23506
23507 /* If we don't have a type, then something is very wrong and we
23508 shouldn't try to do anything clever. Likewise for not seeing the
23509 closing brace. */
23510 if (closing_brace && TYPE_P (type) && want_semicolon)
23511 {
23512 /* Locate the closing brace. */
23513 cp_token_position prev
23514 = cp_lexer_previous_token_position (parser->lexer);
23515 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
23516 location_t loc = prev_token->location;
23517
23518 /* We want to suggest insertion of a ';' immediately *after* the
23519 closing brace, so, if we can, offset the location by 1 column. */
23520 location_t next_loc = loc;
23521 if (!linemap_location_from_macro_expansion_p (line_table, loc))
23522 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
23523
23524 rich_location richloc (line_table, next_loc);
23525
23526 /* If we successfully offset the location, suggest the fix-it. */
23527 if (next_loc != loc)
23528 richloc.add_fixit_insert_before (next_loc, ";");
23529
23530 if (CLASSTYPE_DECLARED_CLASS (type))
23531 error_at (&richloc,
23532 "expected %<;%> after class definition");
23533 else if (TREE_CODE (type) == RECORD_TYPE)
23534 error_at (&richloc,
23535 "expected %<;%> after struct definition");
23536 else if (TREE_CODE (type) == UNION_TYPE)
23537 error_at (&richloc,
23538 "expected %<;%> after union definition");
23539 else
23540 gcc_unreachable ();
23541
23542 /* Unget one token and smash it to look as though we encountered
23543 a semicolon in the input stream. */
23544 cp_lexer_set_token_position (parser->lexer, prev);
23545 token = cp_lexer_peek_token (parser->lexer);
23546 token->type = CPP_SEMICOLON;
23547 token->keyword = RID_MAX;
23548 }
23549 }
23550
23551 /* If this class is not itself within the scope of another class,
23552 then we need to parse the bodies of all of the queued function
23553 definitions. Note that the queued functions defined in a class
23554 are not always processed immediately following the
23555 class-specifier for that class. Consider:
23556
23557 struct A {
23558 struct B { void f() { sizeof (A); } };
23559 };
23560
23561 If `f' were processed before the processing of `A' were
23562 completed, there would be no way to compute the size of `A'.
23563 Note that the nesting we are interested in here is lexical --
23564 not the semantic nesting given by TYPE_CONTEXT. In particular,
23565 for:
23566
23567 struct A { struct B; };
23568 struct A::B { void f() { } };
23569
23570 there is no need to delay the parsing of `A::B::f'. */
23571 if (--parser->num_classes_being_defined == 0)
23572 {
23573 tree decl;
23574 tree class_type = NULL_TREE;
23575 tree pushed_scope = NULL_TREE;
23576 unsigned ix;
23577 cp_default_arg_entry *e;
23578 tree save_ccp, save_ccr;
23579
23580 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
23581 {
23582 /* Skip default arguments, NSDMIs, etc, in order to improve
23583 error recovery (c++/71169, c++/71832). */
23584 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23585 vec_safe_truncate (unparsed_nsdmis, 0);
23586 vec_safe_truncate (unparsed_classes, 0);
23587 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23588 }
23589
23590 /* In a first pass, parse default arguments to the functions.
23591 Then, in a second pass, parse the bodies of the functions.
23592 This two-phased approach handles cases like:
23593
23594 struct S {
23595 void f() { g(); }
23596 void g(int i = 3);
23597 };
23598
23599 */
23600 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
23601 {
23602 decl = e->decl;
23603 /* If there are default arguments that have not yet been processed,
23604 take care of them now. */
23605 if (class_type != e->class_type)
23606 {
23607 if (pushed_scope)
23608 pop_scope (pushed_scope);
23609 class_type = e->class_type;
23610 pushed_scope = push_scope (class_type);
23611 }
23612 /* Make sure that any template parameters are in scope. */
23613 maybe_begin_member_template_processing (decl);
23614 /* Parse the default argument expressions. */
23615 cp_parser_late_parsing_default_args (parser, decl);
23616 /* Remove any template parameters from the symbol table. */
23617 maybe_end_member_template_processing ();
23618 }
23619 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23620 /* Now parse any NSDMIs. */
23621 save_ccp = current_class_ptr;
23622 save_ccr = current_class_ref;
23623 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23624 {
23625 if (class_type != DECL_CONTEXT (decl))
23626 {
23627 if (pushed_scope)
23628 pop_scope (pushed_scope);
23629 class_type = DECL_CONTEXT (decl);
23630 pushed_scope = push_scope (class_type);
23631 }
23632 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23633 cp_parser_late_parsing_nsdmi (parser, decl);
23634 }
23635 vec_safe_truncate (unparsed_nsdmis, 0);
23636 current_class_ptr = save_ccp;
23637 current_class_ref = save_ccr;
23638 if (pushed_scope)
23639 pop_scope (pushed_scope);
23640
23641 /* Now do some post-NSDMI bookkeeping. */
23642 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23643 after_nsdmi_defaulted_late_checks (class_type);
23644 vec_safe_truncate (unparsed_classes, 0);
23645 after_nsdmi_defaulted_late_checks (type);
23646
23647 /* Now parse the body of the functions. */
23648 if (flag_openmp)
23649 {
23650 /* OpenMP UDRs need to be parsed before all other functions. */
23651 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23652 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23653 cp_parser_late_parsing_for_member (parser, decl);
23654 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23655 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23656 cp_parser_late_parsing_for_member (parser, decl);
23657 }
23658 else
23659 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23660 cp_parser_late_parsing_for_member (parser, decl);
23661 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23662 }
23663 else
23664 vec_safe_push (unparsed_classes, type);
23665
23666 /* Put back any saved access checks. */
23667 pop_deferring_access_checks ();
23668
23669 /* Restore saved state. */
23670 parser->in_switch_statement_p = in_switch_statement_p;
23671 parser->in_statement = in_statement;
23672 parser->in_function_body = saved_in_function_body;
23673 parser->num_template_parameter_lists
23674 = saved_num_template_parameter_lists;
23675 parser->in_unbraced_linkage_specification_p
23676 = saved_in_unbraced_linkage_specification_p;
23677
23678 return type;
23679 }
23680
23681 static tree
23682 cp_parser_class_specifier (cp_parser* parser)
23683 {
23684 tree ret;
23685 timevar_push (TV_PARSE_STRUCT);
23686 ret = cp_parser_class_specifier_1 (parser);
23687 timevar_pop (TV_PARSE_STRUCT);
23688 return ret;
23689 }
23690
23691 /* Parse a class-head.
23692
23693 class-head:
23694 class-key identifier [opt] base-clause [opt]
23695 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23696 class-key nested-name-specifier [opt] template-id
23697 base-clause [opt]
23698
23699 class-virt-specifier:
23700 final
23701
23702 GNU Extensions:
23703 class-key attributes identifier [opt] base-clause [opt]
23704 class-key attributes nested-name-specifier identifier base-clause [opt]
23705 class-key attributes nested-name-specifier [opt] template-id
23706 base-clause [opt]
23707
23708 Upon return BASES is initialized to the list of base classes (or
23709 NULL, if there are none) in the same form returned by
23710 cp_parser_base_clause.
23711
23712 Returns the TYPE of the indicated class. Sets
23713 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23714 involving a nested-name-specifier was used, and FALSE otherwise.
23715
23716 Returns error_mark_node if this is not a class-head.
23717
23718 Returns NULL_TREE if the class-head is syntactically valid, but
23719 semantically invalid in a way that means we should skip the entire
23720 body of the class. */
23721
23722 static tree
23723 cp_parser_class_head (cp_parser* parser,
23724 bool* nested_name_specifier_p)
23725 {
23726 tree nested_name_specifier;
23727 enum tag_types class_key;
23728 tree id = NULL_TREE;
23729 tree type = NULL_TREE;
23730 tree attributes;
23731 tree bases;
23732 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23733 bool template_id_p = false;
23734 bool qualified_p = false;
23735 bool invalid_nested_name_p = false;
23736 bool invalid_explicit_specialization_p = false;
23737 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23738 tree pushed_scope = NULL_TREE;
23739 unsigned num_templates;
23740 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23741 /* Assume no nested-name-specifier will be present. */
23742 *nested_name_specifier_p = false;
23743 /* Assume no template parameter lists will be used in defining the
23744 type. */
23745 num_templates = 0;
23746 parser->colon_corrects_to_scope_p = false;
23747
23748 /* Look for the class-key. */
23749 class_key = cp_parser_class_key (parser);
23750 if (class_key == none_type)
23751 return error_mark_node;
23752
23753 location_t class_head_start_location = input_location;
23754
23755 /* Parse the attributes. */
23756 attributes = cp_parser_attributes_opt (parser);
23757
23758 /* If the next token is `::', that is invalid -- but sometimes
23759 people do try to write:
23760
23761 struct ::S {};
23762
23763 Handle this gracefully by accepting the extra qualifier, and then
23764 issuing an error about it later if this really is a
23765 class-head. If it turns out just to be an elaborated type
23766 specifier, remain silent. */
23767 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23768 qualified_p = true;
23769
23770 push_deferring_access_checks (dk_no_check);
23771
23772 /* Determine the name of the class. Begin by looking for an
23773 optional nested-name-specifier. */
23774 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23775 nested_name_specifier
23776 = cp_parser_nested_name_specifier_opt (parser,
23777 /*typename_keyword_p=*/false,
23778 /*check_dependency_p=*/false,
23779 /*type_p=*/true,
23780 /*is_declaration=*/false);
23781 /* If there was a nested-name-specifier, then there *must* be an
23782 identifier. */
23783
23784 cp_token *bad_template_keyword = NULL;
23785
23786 if (nested_name_specifier)
23787 {
23788 type_start_token = cp_lexer_peek_token (parser->lexer);
23789 /* Although the grammar says `identifier', it really means
23790 `class-name' or `template-name'. You are only allowed to
23791 define a class that has already been declared with this
23792 syntax.
23793
23794 The proposed resolution for Core Issue 180 says that wherever
23795 you see `class T::X' you should treat `X' as a type-name.
23796
23797 It is OK to define an inaccessible class; for example:
23798
23799 class A { class B; };
23800 class A::B {};
23801
23802 We do not know if we will see a class-name, or a
23803 template-name. We look for a class-name first, in case the
23804 class-name is a template-id; if we looked for the
23805 template-name first we would stop after the template-name. */
23806 cp_parser_parse_tentatively (parser);
23807 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23808 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23809 type = cp_parser_class_name (parser,
23810 /*typename_keyword_p=*/false,
23811 /*template_keyword_p=*/false,
23812 class_type,
23813 /*check_dependency_p=*/false,
23814 /*class_head_p=*/true,
23815 /*is_declaration=*/false);
23816 /* If that didn't work, ignore the nested-name-specifier. */
23817 if (!cp_parser_parse_definitely (parser))
23818 {
23819 invalid_nested_name_p = true;
23820 type_start_token = cp_lexer_peek_token (parser->lexer);
23821 id = cp_parser_identifier (parser);
23822 if (id == error_mark_node)
23823 id = NULL_TREE;
23824 }
23825 /* If we could not find a corresponding TYPE, treat this
23826 declaration like an unqualified declaration. */
23827 if (type == error_mark_node)
23828 nested_name_specifier = NULL_TREE;
23829 /* Otherwise, count the number of templates used in TYPE and its
23830 containing scopes. */
23831 else
23832 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23833 }
23834 /* Otherwise, the identifier is optional. */
23835 else
23836 {
23837 /* We don't know whether what comes next is a template-id,
23838 an identifier, or nothing at all. */
23839 cp_parser_parse_tentatively (parser);
23840 /* Check for a template-id. */
23841 type_start_token = cp_lexer_peek_token (parser->lexer);
23842 id = cp_parser_template_id (parser,
23843 /*template_keyword_p=*/false,
23844 /*check_dependency_p=*/true,
23845 class_key,
23846 /*is_declaration=*/true);
23847 /* If that didn't work, it could still be an identifier. */
23848 if (!cp_parser_parse_definitely (parser))
23849 {
23850 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23851 {
23852 type_start_token = cp_lexer_peek_token (parser->lexer);
23853 id = cp_parser_identifier (parser);
23854 }
23855 else
23856 id = NULL_TREE;
23857 }
23858 else
23859 {
23860 template_id_p = true;
23861 ++num_templates;
23862 }
23863 }
23864
23865 pop_deferring_access_checks ();
23866
23867 if (id)
23868 {
23869 cp_parser_check_for_invalid_template_id (parser, id,
23870 class_key,
23871 type_start_token->location);
23872 }
23873 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23874
23875 /* If it's not a `:' or a `{' then we can't really be looking at a
23876 class-head, since a class-head only appears as part of a
23877 class-specifier. We have to detect this situation before calling
23878 xref_tag, since that has irreversible side-effects. */
23879 if (!cp_parser_next_token_starts_class_definition_p (parser))
23880 {
23881 cp_parser_error (parser, "expected %<{%> or %<:%>");
23882 type = error_mark_node;
23883 goto out;
23884 }
23885
23886 /* At this point, we're going ahead with the class-specifier, even
23887 if some other problem occurs. */
23888 cp_parser_commit_to_tentative_parse (parser);
23889 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23890 {
23891 cp_parser_error (parser,
23892 "cannot specify %<override%> for a class");
23893 type = error_mark_node;
23894 goto out;
23895 }
23896 /* Issue the error about the overly-qualified name now. */
23897 if (qualified_p)
23898 {
23899 cp_parser_error (parser,
23900 "global qualification of class name is invalid");
23901 type = error_mark_node;
23902 goto out;
23903 }
23904 else if (invalid_nested_name_p)
23905 {
23906 cp_parser_error (parser,
23907 "qualified name does not name a class");
23908 type = error_mark_node;
23909 goto out;
23910 }
23911 else if (nested_name_specifier)
23912 {
23913 tree scope;
23914
23915 if (bad_template_keyword)
23916 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23917 keyword template shall not appear at the top level. */
23918 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23919 "keyword %<template%> not allowed in class-head-name");
23920
23921 /* Reject typedef-names in class heads. */
23922 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23923 {
23924 error_at (type_start_token->location,
23925 "invalid class name in declaration of %qD",
23926 type);
23927 type = NULL_TREE;
23928 goto done;
23929 }
23930
23931 /* Figure out in what scope the declaration is being placed. */
23932 scope = current_scope ();
23933 /* If that scope does not contain the scope in which the
23934 class was originally declared, the program is invalid. */
23935 if (scope && !is_ancestor (scope, nested_name_specifier))
23936 {
23937 if (at_namespace_scope_p ())
23938 error_at (type_start_token->location,
23939 "declaration of %qD in namespace %qD which does not "
23940 "enclose %qD",
23941 type, scope, nested_name_specifier);
23942 else
23943 error_at (type_start_token->location,
23944 "declaration of %qD in %qD which does not enclose %qD",
23945 type, scope, nested_name_specifier);
23946 type = NULL_TREE;
23947 goto done;
23948 }
23949 /* [dcl.meaning]
23950
23951 A declarator-id shall not be qualified except for the
23952 definition of a ... nested class outside of its class
23953 ... [or] the definition or explicit instantiation of a
23954 class member of a namespace outside of its namespace. */
23955 if (scope == nested_name_specifier)
23956 {
23957 permerror (nested_name_specifier_token_start->location,
23958 "extra qualification not allowed");
23959 nested_name_specifier = NULL_TREE;
23960 num_templates = 0;
23961 }
23962 }
23963 /* An explicit-specialization must be preceded by "template <>". If
23964 it is not, try to recover gracefully. */
23965 if (at_namespace_scope_p ()
23966 && parser->num_template_parameter_lists == 0
23967 && !processing_template_parmlist
23968 && template_id_p)
23969 {
23970 /* Build a location of this form:
23971 struct typename <ARGS>
23972 ^~~~~~~~~~~~~~~~~~~~~~
23973 with caret==start at the start token, and
23974 finishing at the end of the type. */
23975 location_t reported_loc
23976 = make_location (class_head_start_location,
23977 class_head_start_location,
23978 get_finish (type_start_token->location));
23979 rich_location richloc (line_table, reported_loc);
23980 richloc.add_fixit_insert_before (class_head_start_location,
23981 "template <> ");
23982 error_at (&richloc,
23983 "an explicit specialization must be preceded by"
23984 " %<template <>%>");
23985 invalid_explicit_specialization_p = true;
23986 /* Take the same action that would have been taken by
23987 cp_parser_explicit_specialization. */
23988 ++parser->num_template_parameter_lists;
23989 begin_specialization ();
23990 }
23991 /* There must be no "return" statements between this point and the
23992 end of this function; set "type "to the correct return value and
23993 use "goto done;" to return. */
23994 /* Make sure that the right number of template parameters were
23995 present. */
23996 if (!cp_parser_check_template_parameters (parser, num_templates,
23997 template_id_p,
23998 type_start_token->location,
23999 /*declarator=*/NULL))
24000 {
24001 /* If something went wrong, there is no point in even trying to
24002 process the class-definition. */
24003 type = NULL_TREE;
24004 goto done;
24005 }
24006
24007 /* Look up the type. */
24008 if (template_id_p)
24009 {
24010 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
24011 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
24012 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
24013 {
24014 error_at (type_start_token->location,
24015 "function template %qD redeclared as a class template", id);
24016 type = error_mark_node;
24017 }
24018 else
24019 {
24020 type = TREE_TYPE (id);
24021 type = maybe_process_partial_specialization (type);
24022
24023 /* Check the scope while we still know whether or not we had a
24024 nested-name-specifier. */
24025 if (type != error_mark_node)
24026 check_unqualified_spec_or_inst (type, type_start_token->location);
24027 }
24028 if (nested_name_specifier)
24029 pushed_scope = push_scope (nested_name_specifier);
24030 }
24031 else if (nested_name_specifier)
24032 {
24033 tree class_type;
24034
24035 /* Given:
24036
24037 template <typename T> struct S { struct T };
24038 template <typename T> struct S<T>::T { };
24039
24040 we will get a TYPENAME_TYPE when processing the definition of
24041 `S::T'. We need to resolve it to the actual type before we
24042 try to define it. */
24043 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
24044 {
24045 class_type = resolve_typename_type (TREE_TYPE (type),
24046 /*only_current_p=*/false);
24047 if (TREE_CODE (class_type) != TYPENAME_TYPE)
24048 type = TYPE_NAME (class_type);
24049 else
24050 {
24051 cp_parser_error (parser, "could not resolve typename type");
24052 type = error_mark_node;
24053 }
24054 }
24055
24056 if (maybe_process_partial_specialization (TREE_TYPE (type))
24057 == error_mark_node)
24058 {
24059 type = NULL_TREE;
24060 goto done;
24061 }
24062
24063 class_type = current_class_type;
24064 /* Enter the scope indicated by the nested-name-specifier. */
24065 pushed_scope = push_scope (nested_name_specifier);
24066 /* Get the canonical version of this type. */
24067 type = TYPE_MAIN_DECL (TREE_TYPE (type));
24068 /* Call push_template_decl if it seems like we should be defining a
24069 template either from the template headers or the type we're
24070 defining, so that we diagnose both extra and missing headers. */
24071 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
24072 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
24073 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
24074 {
24075 type = push_template_decl (type);
24076 if (type == error_mark_node)
24077 {
24078 type = NULL_TREE;
24079 goto done;
24080 }
24081 }
24082
24083 type = TREE_TYPE (type);
24084 *nested_name_specifier_p = true;
24085 }
24086 else /* The name is not a nested name. */
24087 {
24088 /* If the class was unnamed, create a dummy name. */
24089 if (!id)
24090 id = make_anon_name ();
24091 tag_scope tag_scope = (parser->in_type_id_in_expr_p
24092 ? ts_within_enclosing_non_class
24093 : ts_current);
24094 type = xref_tag (class_key, id, tag_scope,
24095 parser->num_template_parameter_lists);
24096 }
24097
24098 /* Indicate whether this class was declared as a `class' or as a
24099 `struct'. */
24100 if (TREE_CODE (type) == RECORD_TYPE)
24101 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
24102 cp_parser_check_class_key (class_key, type);
24103
24104 /* If this type was already complete, and we see another definition,
24105 that's an error. Likewise if the type is already being defined:
24106 this can happen, eg, when it's defined from within an expression
24107 (c++/84605). */
24108 if (type != error_mark_node
24109 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
24110 {
24111 error_at (type_start_token->location, "redefinition of %q#T",
24112 type);
24113 inform (location_of (type), "previous definition of %q#T",
24114 type);
24115 type = NULL_TREE;
24116 goto done;
24117 }
24118 else if (type == error_mark_node)
24119 type = NULL_TREE;
24120
24121 if (type)
24122 {
24123 /* Apply attributes now, before any use of the class as a template
24124 argument in its base list. */
24125 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
24126 fixup_attribute_variants (type);
24127 }
24128
24129 /* We will have entered the scope containing the class; the names of
24130 base classes should be looked up in that context. For example:
24131
24132 struct A { struct B {}; struct C; };
24133 struct A::C : B {};
24134
24135 is valid. */
24136
24137 /* Get the list of base-classes, if there is one. */
24138 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24139 {
24140 /* PR59482: enter the class scope so that base-specifiers are looked
24141 up correctly. */
24142 if (type)
24143 pushclass (type);
24144 bases = cp_parser_base_clause (parser);
24145 /* PR59482: get out of the previously pushed class scope so that the
24146 subsequent pops pop the right thing. */
24147 if (type)
24148 popclass ();
24149 }
24150 else
24151 bases = NULL_TREE;
24152
24153 /* If we're really defining a class, process the base classes.
24154 If they're invalid, fail. */
24155 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24156 xref_basetypes (type, bases);
24157
24158 done:
24159 /* Leave the scope given by the nested-name-specifier. We will
24160 enter the class scope itself while processing the members. */
24161 if (pushed_scope)
24162 pop_scope (pushed_scope);
24163
24164 if (invalid_explicit_specialization_p)
24165 {
24166 end_specialization ();
24167 --parser->num_template_parameter_lists;
24168 }
24169
24170 if (type)
24171 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
24172 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
24173 CLASSTYPE_FINAL (type) = 1;
24174 out:
24175 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24176 return type;
24177 }
24178
24179 /* Parse a class-key.
24180
24181 class-key:
24182 class
24183 struct
24184 union
24185
24186 Returns the kind of class-key specified, or none_type to indicate
24187 error. */
24188
24189 static enum tag_types
24190 cp_parser_class_key (cp_parser* parser)
24191 {
24192 cp_token *token;
24193 enum tag_types tag_type;
24194
24195 /* Look for the class-key. */
24196 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
24197 if (!token)
24198 return none_type;
24199
24200 /* Check to see if the TOKEN is a class-key. */
24201 tag_type = cp_parser_token_is_class_key (token);
24202 if (!tag_type)
24203 cp_parser_error (parser, "expected class-key");
24204 return tag_type;
24205 }
24206
24207 /* Parse a type-parameter-key.
24208
24209 type-parameter-key:
24210 class
24211 typename
24212 */
24213
24214 static void
24215 cp_parser_type_parameter_key (cp_parser* parser)
24216 {
24217 /* Look for the type-parameter-key. */
24218 enum tag_types tag_type = none_type;
24219 cp_token *token = cp_lexer_peek_token (parser->lexer);
24220 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
24221 {
24222 cp_lexer_consume_token (parser->lexer);
24223 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
24224 /* typename is not allowed in a template template parameter
24225 by the standard until C++17. */
24226 pedwarn (token->location, OPT_Wpedantic,
24227 "ISO C++ forbids typename key in template template parameter;"
24228 " use %<-std=c++17%> or %<-std=gnu++17%>");
24229 }
24230 else
24231 cp_parser_error (parser, "expected %<class%> or %<typename%>");
24232
24233 return;
24234 }
24235
24236 /* Parse an (optional) member-specification.
24237
24238 member-specification:
24239 member-declaration member-specification [opt]
24240 access-specifier : member-specification [opt] */
24241
24242 static void
24243 cp_parser_member_specification_opt (cp_parser* parser)
24244 {
24245 while (true)
24246 {
24247 cp_token *token;
24248 enum rid keyword;
24249
24250 /* Peek at the next token. */
24251 token = cp_lexer_peek_token (parser->lexer);
24252 /* If it's a `}', or EOF then we've seen all the members. */
24253 if (token->type == CPP_CLOSE_BRACE
24254 || token->type == CPP_EOF
24255 || token->type == CPP_PRAGMA_EOL)
24256 break;
24257
24258 /* See if this token is a keyword. */
24259 keyword = token->keyword;
24260 switch (keyword)
24261 {
24262 case RID_PUBLIC:
24263 case RID_PROTECTED:
24264 case RID_PRIVATE:
24265 /* Consume the access-specifier. */
24266 cp_lexer_consume_token (parser->lexer);
24267 /* Remember which access-specifier is active. */
24268 current_access_specifier = token->u.value;
24269 /* Look for the `:'. */
24270 cp_parser_require (parser, CPP_COLON, RT_COLON);
24271 break;
24272
24273 default:
24274 /* Accept #pragmas at class scope. */
24275 if (token->type == CPP_PRAGMA)
24276 {
24277 cp_parser_pragma (parser, pragma_member, NULL);
24278 break;
24279 }
24280
24281 /* Otherwise, the next construction must be a
24282 member-declaration. */
24283 cp_parser_member_declaration (parser);
24284 }
24285 }
24286 }
24287
24288 /* Parse a member-declaration.
24289
24290 member-declaration:
24291 decl-specifier-seq [opt] member-declarator-list [opt] ;
24292 function-definition ; [opt]
24293 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24294 using-declaration
24295 template-declaration
24296 alias-declaration
24297
24298 member-declarator-list:
24299 member-declarator
24300 member-declarator-list , member-declarator
24301
24302 member-declarator:
24303 declarator pure-specifier [opt]
24304 declarator constant-initializer [opt]
24305 identifier [opt] : constant-expression
24306
24307 GNU Extensions:
24308
24309 member-declaration:
24310 __extension__ member-declaration
24311
24312 member-declarator:
24313 declarator attributes [opt] pure-specifier [opt]
24314 declarator attributes [opt] constant-initializer [opt]
24315 identifier [opt] attributes [opt] : constant-expression
24316
24317 C++0x Extensions:
24318
24319 member-declaration:
24320 static_assert-declaration */
24321
24322 static void
24323 cp_parser_member_declaration (cp_parser* parser)
24324 {
24325 cp_decl_specifier_seq decl_specifiers;
24326 tree prefix_attributes;
24327 tree decl;
24328 int declares_class_or_enum;
24329 bool friend_p;
24330 cp_token *token = NULL;
24331 cp_token *decl_spec_token_start = NULL;
24332 cp_token *initializer_token_start = NULL;
24333 int saved_pedantic;
24334 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24335
24336 /* Check for the `__extension__' keyword. */
24337 if (cp_parser_extension_opt (parser, &saved_pedantic))
24338 {
24339 /* Recurse. */
24340 cp_parser_member_declaration (parser);
24341 /* Restore the old value of the PEDANTIC flag. */
24342 pedantic = saved_pedantic;
24343
24344 return;
24345 }
24346
24347 /* Check for a template-declaration. */
24348 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24349 {
24350 /* An explicit specialization here is an error condition, and we
24351 expect the specialization handler to detect and report this. */
24352 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
24353 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
24354 cp_parser_explicit_specialization (parser);
24355 else
24356 cp_parser_template_declaration (parser, /*member_p=*/true);
24357
24358 return;
24359 }
24360 /* Check for a template introduction. */
24361 else if (cp_parser_template_declaration_after_export (parser, true))
24362 return;
24363
24364 /* Check for a using-declaration. */
24365 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24366 {
24367 if (cxx_dialect < cxx11)
24368 {
24369 /* Parse the using-declaration. */
24370 cp_parser_using_declaration (parser,
24371 /*access_declaration_p=*/false);
24372 return;
24373 }
24374 else
24375 {
24376 tree decl;
24377 bool alias_decl_expected;
24378 cp_parser_parse_tentatively (parser);
24379 decl = cp_parser_alias_declaration (parser);
24380 /* Note that if we actually see the '=' token after the
24381 identifier, cp_parser_alias_declaration commits the
24382 tentative parse. In that case, we really expect an
24383 alias-declaration. Otherwise, we expect a using
24384 declaration. */
24385 alias_decl_expected =
24386 !cp_parser_uncommitted_to_tentative_parse_p (parser);
24387 cp_parser_parse_definitely (parser);
24388
24389 if (alias_decl_expected)
24390 finish_member_declaration (decl);
24391 else
24392 cp_parser_using_declaration (parser,
24393 /*access_declaration_p=*/false);
24394 return;
24395 }
24396 }
24397
24398 /* Check for @defs. */
24399 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
24400 {
24401 tree ivar, member;
24402 tree ivar_chains = cp_parser_objc_defs_expression (parser);
24403 ivar = ivar_chains;
24404 while (ivar)
24405 {
24406 member = ivar;
24407 ivar = TREE_CHAIN (member);
24408 TREE_CHAIN (member) = NULL_TREE;
24409 finish_member_declaration (member);
24410 }
24411 return;
24412 }
24413
24414 /* If the next token is `static_assert' we have a static assertion. */
24415 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
24416 {
24417 cp_parser_static_assert (parser, /*member_p=*/true);
24418 return;
24419 }
24420
24421 parser->colon_corrects_to_scope_p = false;
24422
24423 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
24424 goto out;
24425
24426 /* Parse the decl-specifier-seq. */
24427 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24428 cp_parser_decl_specifier_seq (parser,
24429 (CP_PARSER_FLAGS_OPTIONAL
24430 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
24431 &decl_specifiers,
24432 &declares_class_or_enum);
24433 /* Check for an invalid type-name. */
24434 if (!decl_specifiers.any_type_specifiers_p
24435 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24436 goto out;
24437 /* If there is no declarator, then the decl-specifier-seq should
24438 specify a type. */
24439 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24440 {
24441 /* If there was no decl-specifier-seq, and the next token is a
24442 `;', then we have something like:
24443
24444 struct S { ; };
24445
24446 [class.mem]
24447
24448 Each member-declaration shall declare at least one member
24449 name of the class. */
24450 if (!decl_specifiers.any_specifiers_p)
24451 {
24452 cp_token *token = cp_lexer_peek_token (parser->lexer);
24453 if (!in_system_header_at (token->location))
24454 {
24455 gcc_rich_location richloc (token->location);
24456 richloc.add_fixit_remove ();
24457 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
24458 }
24459 }
24460 else
24461 {
24462 tree type;
24463
24464 /* See if this declaration is a friend. */
24465 friend_p = cp_parser_friend_p (&decl_specifiers);
24466 /* If there were decl-specifiers, check to see if there was
24467 a class-declaration. */
24468 type = check_tag_decl (&decl_specifiers,
24469 /*explicit_type_instantiation_p=*/false);
24470 /* Nested classes have already been added to the class, but
24471 a `friend' needs to be explicitly registered. */
24472 if (friend_p)
24473 {
24474 /* If the `friend' keyword was present, the friend must
24475 be introduced with a class-key. */
24476 if (!declares_class_or_enum && cxx_dialect < cxx11)
24477 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
24478 "in C++03 a class-key must be used "
24479 "when declaring a friend");
24480 /* In this case:
24481
24482 template <typename T> struct A {
24483 friend struct A<T>::B;
24484 };
24485
24486 A<T>::B will be represented by a TYPENAME_TYPE, and
24487 therefore not recognized by check_tag_decl. */
24488 if (!type)
24489 {
24490 type = decl_specifiers.type;
24491 if (type && TREE_CODE (type) == TYPE_DECL)
24492 type = TREE_TYPE (type);
24493 }
24494 if (!type || !TYPE_P (type))
24495 error_at (decl_spec_token_start->location,
24496 "friend declaration does not name a class or "
24497 "function");
24498 else
24499 make_friend_class (current_class_type, type,
24500 /*complain=*/true);
24501 }
24502 /* If there is no TYPE, an error message will already have
24503 been issued. */
24504 else if (!type || type == error_mark_node)
24505 ;
24506 /* An anonymous aggregate has to be handled specially; such
24507 a declaration really declares a data member (with a
24508 particular type), as opposed to a nested class. */
24509 else if (ANON_AGGR_TYPE_P (type))
24510 {
24511 /* C++11 9.5/6. */
24512 if (decl_specifiers.storage_class != sc_none)
24513 error_at (decl_spec_token_start->location,
24514 "a storage class on an anonymous aggregate "
24515 "in class scope is not allowed");
24516
24517 /* Remove constructors and such from TYPE, now that we
24518 know it is an anonymous aggregate. */
24519 fixup_anonymous_aggr (type);
24520 /* And make the corresponding data member. */
24521 decl = build_decl (decl_spec_token_start->location,
24522 FIELD_DECL, NULL_TREE, type);
24523 /* Add it to the class. */
24524 finish_member_declaration (decl);
24525 }
24526 else
24527 cp_parser_check_access_in_redeclaration
24528 (TYPE_NAME (type),
24529 decl_spec_token_start->location);
24530 }
24531 }
24532 else
24533 {
24534 bool assume_semicolon = false;
24535
24536 /* Clear attributes from the decl_specifiers but keep them
24537 around as prefix attributes that apply them to the entity
24538 being declared. */
24539 prefix_attributes = decl_specifiers.attributes;
24540 decl_specifiers.attributes = NULL_TREE;
24541
24542 /* See if these declarations will be friends. */
24543 friend_p = cp_parser_friend_p (&decl_specifiers);
24544
24545 /* Keep going until we hit the `;' at the end of the
24546 declaration. */
24547 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24548 {
24549 tree attributes = NULL_TREE;
24550 tree first_attribute;
24551 tree initializer;
24552 bool named_bitfld = false;
24553
24554 /* Peek at the next token. */
24555 token = cp_lexer_peek_token (parser->lexer);
24556
24557 /* The following code wants to know early if it is a bit-field
24558 or some other declaration. Attributes can appear before
24559 the `:' token. Skip over them without consuming any tokens
24560 to peek if they are followed by `:'. */
24561 if (cp_next_tokens_can_be_attribute_p (parser)
24562 || (token->type == CPP_NAME
24563 && cp_nth_tokens_can_be_attribute_p (parser, 2)
24564 && (named_bitfld = true)))
24565 {
24566 size_t n
24567 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
24568 token = cp_lexer_peek_nth_token (parser->lexer, n);
24569 }
24570
24571 /* Check for a bitfield declaration. */
24572 if (token->type == CPP_COLON
24573 || (token->type == CPP_NAME
24574 && token == cp_lexer_peek_token (parser->lexer)
24575 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
24576 && (named_bitfld = true)))
24577 {
24578 tree identifier;
24579 tree width;
24580 tree late_attributes = NULL_TREE;
24581 location_t id_location
24582 = cp_lexer_peek_token (parser->lexer)->location;
24583
24584 if (named_bitfld)
24585 identifier = cp_parser_identifier (parser);
24586 else
24587 identifier = NULL_TREE;
24588
24589 /* Look for attributes that apply to the bitfield. */
24590 attributes = cp_parser_attributes_opt (parser);
24591
24592 /* Consume the `:' token. */
24593 cp_lexer_consume_token (parser->lexer);
24594
24595 /* Get the width of the bitfield. */
24596 width = cp_parser_constant_expression (parser, false, NULL,
24597 cxx_dialect >= cxx11);
24598
24599 /* In C++2A and as extension for C++11 and above we allow
24600 default member initializers for bit-fields. */
24601 initializer = NULL_TREE;
24602 if (cxx_dialect >= cxx11
24603 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
24604 || cp_lexer_next_token_is (parser->lexer,
24605 CPP_OPEN_BRACE)))
24606 {
24607 location_t loc
24608 = cp_lexer_peek_token (parser->lexer)->location;
24609 if (cxx_dialect < cxx2a
24610 && !in_system_header_at (loc)
24611 && identifier != NULL_TREE)
24612 pedwarn (loc, 0,
24613 "default member initializers for bit-fields "
24614 "only available with %<-std=c++2a%> or "
24615 "%<-std=gnu++2a%>");
24616
24617 initializer = cp_parser_save_nsdmi (parser);
24618 if (identifier == NULL_TREE)
24619 {
24620 error_at (loc, "default member initializer for "
24621 "unnamed bit-field");
24622 initializer = NULL_TREE;
24623 }
24624 }
24625 else
24626 {
24627 /* Look for attributes that apply to the bitfield after
24628 the `:' token and width. This is where GCC used to
24629 parse attributes in the past, pedwarn if there is
24630 a std attribute. */
24631 if (cp_next_tokens_can_be_std_attribute_p (parser))
24632 pedwarn (input_location, OPT_Wpedantic,
24633 "ISO C++ allows bit-field attributes only "
24634 "before the %<:%> token");
24635
24636 late_attributes = cp_parser_attributes_opt (parser);
24637 }
24638
24639 attributes = attr_chainon (attributes, late_attributes);
24640
24641 /* Remember which attributes are prefix attributes and
24642 which are not. */
24643 first_attribute = attributes;
24644 /* Combine the attributes. */
24645 attributes = attr_chainon (prefix_attributes, attributes);
24646
24647 /* Create the bitfield declaration. */
24648 decl = grokbitfield (identifier
24649 ? make_id_declarator (NULL_TREE,
24650 identifier,
24651 sfk_none,
24652 id_location)
24653 : NULL,
24654 &decl_specifiers,
24655 width, initializer,
24656 attributes);
24657 }
24658 else
24659 {
24660 cp_declarator *declarator;
24661 tree asm_specification;
24662 int ctor_dtor_or_conv_p;
24663 bool static_p = (decl_specifiers.storage_class == sc_static);
24664
24665 /* Parse the declarator. */
24666 declarator
24667 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24668 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24669 &ctor_dtor_or_conv_p,
24670 /*parenthesized_p=*/NULL,
24671 /*member_p=*/true,
24672 friend_p, static_p);
24673
24674 /* If something went wrong parsing the declarator, make sure
24675 that we at least consume some tokens. */
24676 if (declarator == cp_error_declarator)
24677 {
24678 /* Skip to the end of the statement. */
24679 cp_parser_skip_to_end_of_statement (parser);
24680 /* If the next token is not a semicolon, that is
24681 probably because we just skipped over the body of
24682 a function. So, we consume a semicolon if
24683 present, but do not issue an error message if it
24684 is not present. */
24685 if (cp_lexer_next_token_is (parser->lexer,
24686 CPP_SEMICOLON))
24687 cp_lexer_consume_token (parser->lexer);
24688 goto out;
24689 }
24690
24691 if (declares_class_or_enum & 2)
24692 cp_parser_check_for_definition_in_return_type
24693 (declarator, decl_specifiers.type,
24694 decl_specifiers.locations[ds_type_spec]);
24695
24696 /* Look for an asm-specification. */
24697 asm_specification = cp_parser_asm_specification_opt (parser);
24698 /* Look for attributes that apply to the declaration. */
24699 attributes = cp_parser_attributes_opt (parser);
24700 /* Remember which attributes are prefix attributes and
24701 which are not. */
24702 first_attribute = attributes;
24703 /* Combine the attributes. */
24704 attributes = attr_chainon (prefix_attributes, attributes);
24705
24706 /* If it's an `=', then we have a constant-initializer or a
24707 pure-specifier. It is not correct to parse the
24708 initializer before registering the member declaration
24709 since the member declaration should be in scope while
24710 its initializer is processed. However, the rest of the
24711 front end does not yet provide an interface that allows
24712 us to handle this correctly. */
24713 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24714 {
24715 /* In [class.mem]:
24716
24717 A pure-specifier shall be used only in the declaration of
24718 a virtual function.
24719
24720 A member-declarator can contain a constant-initializer
24721 only if it declares a static member of integral or
24722 enumeration type.
24723
24724 Therefore, if the DECLARATOR is for a function, we look
24725 for a pure-specifier; otherwise, we look for a
24726 constant-initializer. When we call `grokfield', it will
24727 perform more stringent semantics checks. */
24728 initializer_token_start = cp_lexer_peek_token (parser->lexer);
24729 if (function_declarator_p (declarator)
24730 || (decl_specifiers.type
24731 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24732 && declarator->kind == cdk_id
24733 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24734 == FUNCTION_TYPE)))
24735 initializer = cp_parser_pure_specifier (parser);
24736 else if (decl_specifiers.storage_class != sc_static)
24737 initializer = cp_parser_save_nsdmi (parser);
24738 else if (cxx_dialect >= cxx11)
24739 {
24740 bool nonconst;
24741 /* Don't require a constant rvalue in C++11, since we
24742 might want a reference constant. We'll enforce
24743 constancy later. */
24744 cp_lexer_consume_token (parser->lexer);
24745 /* Parse the initializer. */
24746 initializer = cp_parser_initializer_clause (parser,
24747 &nonconst);
24748 }
24749 else
24750 /* Parse the initializer. */
24751 initializer = cp_parser_constant_initializer (parser);
24752 }
24753 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24754 && !function_declarator_p (declarator))
24755 {
24756 bool x;
24757 if (decl_specifiers.storage_class != sc_static)
24758 initializer = cp_parser_save_nsdmi (parser);
24759 else
24760 initializer = cp_parser_initializer (parser, &x, &x);
24761 }
24762 /* Otherwise, there is no initializer. */
24763 else
24764 initializer = NULL_TREE;
24765
24766 /* See if we are probably looking at a function
24767 definition. We are certainly not looking at a
24768 member-declarator. Calling `grokfield' has
24769 side-effects, so we must not do it unless we are sure
24770 that we are looking at a member-declarator. */
24771 if (cp_parser_token_starts_function_definition_p
24772 (cp_lexer_peek_token (parser->lexer)))
24773 {
24774 /* The grammar does not allow a pure-specifier to be
24775 used when a member function is defined. (It is
24776 possible that this fact is an oversight in the
24777 standard, since a pure function may be defined
24778 outside of the class-specifier. */
24779 if (initializer && initializer_token_start)
24780 error_at (initializer_token_start->location,
24781 "pure-specifier on function-definition");
24782 decl = cp_parser_save_member_function_body (parser,
24783 &decl_specifiers,
24784 declarator,
24785 attributes);
24786 if (parser->fully_implicit_function_template_p)
24787 decl = finish_fully_implicit_template (parser, decl);
24788 /* If the member was not a friend, declare it here. */
24789 if (!friend_p)
24790 finish_member_declaration (decl);
24791 /* Peek at the next token. */
24792 token = cp_lexer_peek_token (parser->lexer);
24793 /* If the next token is a semicolon, consume it. */
24794 if (token->type == CPP_SEMICOLON)
24795 {
24796 location_t semicolon_loc
24797 = cp_lexer_consume_token (parser->lexer)->location;
24798 gcc_rich_location richloc (semicolon_loc);
24799 richloc.add_fixit_remove ();
24800 warning_at (&richloc, OPT_Wextra_semi,
24801 "extra %<;%> after in-class "
24802 "function definition");
24803 }
24804 goto out;
24805 }
24806 else
24807 if (declarator->kind == cdk_function)
24808 declarator->id_loc = token->location;
24809 /* Create the declaration. */
24810 decl = grokfield (declarator, &decl_specifiers,
24811 initializer, /*init_const_expr_p=*/true,
24812 asm_specification, attributes);
24813 if (parser->fully_implicit_function_template_p)
24814 {
24815 if (friend_p)
24816 finish_fully_implicit_template (parser, 0);
24817 else
24818 decl = finish_fully_implicit_template (parser, decl);
24819 }
24820 }
24821
24822 cp_finalize_omp_declare_simd (parser, decl);
24823 cp_finalize_oacc_routine (parser, decl, false);
24824
24825 /* Reset PREFIX_ATTRIBUTES. */
24826 if (attributes != error_mark_node)
24827 {
24828 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24829 attributes = TREE_CHAIN (attributes);
24830 if (attributes)
24831 TREE_CHAIN (attributes) = NULL_TREE;
24832 }
24833
24834 /* If there is any qualification still in effect, clear it
24835 now; we will be starting fresh with the next declarator. */
24836 parser->scope = NULL_TREE;
24837 parser->qualifying_scope = NULL_TREE;
24838 parser->object_scope = NULL_TREE;
24839 /* If it's a `,', then there are more declarators. */
24840 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24841 {
24842 cp_lexer_consume_token (parser->lexer);
24843 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24844 {
24845 cp_token *token = cp_lexer_previous_token (parser->lexer);
24846 gcc_rich_location richloc (token->location);
24847 richloc.add_fixit_remove ();
24848 error_at (&richloc, "stray %<,%> at end of "
24849 "member declaration");
24850 }
24851 }
24852 /* If the next token isn't a `;', then we have a parse error. */
24853 else if (cp_lexer_next_token_is_not (parser->lexer,
24854 CPP_SEMICOLON))
24855 {
24856 /* The next token might be a ways away from where the
24857 actual semicolon is missing. Find the previous token
24858 and use that for our error position. */
24859 cp_token *token = cp_lexer_previous_token (parser->lexer);
24860 gcc_rich_location richloc (token->location);
24861 richloc.add_fixit_insert_after (";");
24862 error_at (&richloc, "expected %<;%> at end of "
24863 "member declaration");
24864
24865 /* Assume that the user meant to provide a semicolon. If
24866 we were to cp_parser_skip_to_end_of_statement, we might
24867 skip to a semicolon inside a member function definition
24868 and issue nonsensical error messages. */
24869 assume_semicolon = true;
24870 }
24871
24872 if (decl)
24873 {
24874 /* Add DECL to the list of members. */
24875 if (!friend_p
24876 /* Explicitly include, eg, NSDMIs, for better error
24877 recovery (c++/58650). */
24878 || !DECL_DECLARES_FUNCTION_P (decl))
24879 finish_member_declaration (decl);
24880
24881 if (TREE_CODE (decl) == FUNCTION_DECL)
24882 cp_parser_save_default_args (parser, decl);
24883 else if (TREE_CODE (decl) == FIELD_DECL
24884 && DECL_INITIAL (decl))
24885 /* Add DECL to the queue of NSDMI to be parsed later. */
24886 vec_safe_push (unparsed_nsdmis, decl);
24887 }
24888
24889 if (assume_semicolon)
24890 goto out;
24891 }
24892 }
24893
24894 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24895 out:
24896 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24897 }
24898
24899 /* Parse a pure-specifier.
24900
24901 pure-specifier:
24902 = 0
24903
24904 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24905 Otherwise, ERROR_MARK_NODE is returned. */
24906
24907 static tree
24908 cp_parser_pure_specifier (cp_parser* parser)
24909 {
24910 cp_token *token;
24911
24912 /* Look for the `=' token. */
24913 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24914 return error_mark_node;
24915 /* Look for the `0' token. */
24916 token = cp_lexer_peek_token (parser->lexer);
24917
24918 if (token->type == CPP_EOF
24919 || token->type == CPP_PRAGMA_EOL)
24920 return error_mark_node;
24921
24922 cp_lexer_consume_token (parser->lexer);
24923
24924 /* Accept = default or = delete in c++0x mode. */
24925 if (token->keyword == RID_DEFAULT
24926 || token->keyword == RID_DELETE)
24927 {
24928 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24929 return token->u.value;
24930 }
24931
24932 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24933 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24934 {
24935 cp_parser_error (parser,
24936 "invalid pure specifier (only %<= 0%> is allowed)");
24937 cp_parser_skip_to_end_of_statement (parser);
24938 return error_mark_node;
24939 }
24940 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24941 {
24942 error_at (token->location, "templates may not be %<virtual%>");
24943 return error_mark_node;
24944 }
24945
24946 return integer_zero_node;
24947 }
24948
24949 /* Parse a constant-initializer.
24950
24951 constant-initializer:
24952 = constant-expression
24953
24954 Returns a representation of the constant-expression. */
24955
24956 static tree
24957 cp_parser_constant_initializer (cp_parser* parser)
24958 {
24959 /* Look for the `=' token. */
24960 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24961 return error_mark_node;
24962
24963 /* It is invalid to write:
24964
24965 struct S { static const int i = { 7 }; };
24966
24967 */
24968 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24969 {
24970 cp_parser_error (parser,
24971 "a brace-enclosed initializer is not allowed here");
24972 /* Consume the opening brace. */
24973 matching_braces braces;
24974 braces.consume_open (parser);
24975 /* Skip the initializer. */
24976 cp_parser_skip_to_closing_brace (parser);
24977 /* Look for the trailing `}'. */
24978 braces.require_close (parser);
24979
24980 return error_mark_node;
24981 }
24982
24983 return cp_parser_constant_expression (parser);
24984 }
24985
24986 /* Derived classes [gram.class.derived] */
24987
24988 /* Parse a base-clause.
24989
24990 base-clause:
24991 : base-specifier-list
24992
24993 base-specifier-list:
24994 base-specifier ... [opt]
24995 base-specifier-list , base-specifier ... [opt]
24996
24997 Returns a TREE_LIST representing the base-classes, in the order in
24998 which they were declared. The representation of each node is as
24999 described by cp_parser_base_specifier.
25000
25001 In the case that no bases are specified, this function will return
25002 NULL_TREE, not ERROR_MARK_NODE. */
25003
25004 static tree
25005 cp_parser_base_clause (cp_parser* parser)
25006 {
25007 tree bases = NULL_TREE;
25008
25009 /* Look for the `:' that begins the list. */
25010 cp_parser_require (parser, CPP_COLON, RT_COLON);
25011
25012 /* Scan the base-specifier-list. */
25013 while (true)
25014 {
25015 cp_token *token;
25016 tree base;
25017 bool pack_expansion_p = false;
25018
25019 /* Look for the base-specifier. */
25020 base = cp_parser_base_specifier (parser);
25021 /* Look for the (optional) ellipsis. */
25022 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25023 {
25024 /* Consume the `...'. */
25025 cp_lexer_consume_token (parser->lexer);
25026
25027 pack_expansion_p = true;
25028 }
25029
25030 /* Add BASE to the front of the list. */
25031 if (base && base != error_mark_node)
25032 {
25033 if (pack_expansion_p)
25034 /* Make this a pack expansion type. */
25035 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
25036
25037 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
25038 {
25039 TREE_CHAIN (base) = bases;
25040 bases = base;
25041 }
25042 }
25043 /* Peek at the next token. */
25044 token = cp_lexer_peek_token (parser->lexer);
25045 /* If it's not a comma, then the list is complete. */
25046 if (token->type != CPP_COMMA)
25047 break;
25048 /* Consume the `,'. */
25049 cp_lexer_consume_token (parser->lexer);
25050 }
25051
25052 /* PARSER->SCOPE may still be non-NULL at this point, if the last
25053 base class had a qualified name. However, the next name that
25054 appears is certainly not qualified. */
25055 parser->scope = NULL_TREE;
25056 parser->qualifying_scope = NULL_TREE;
25057 parser->object_scope = NULL_TREE;
25058
25059 return nreverse (bases);
25060 }
25061
25062 /* Parse a base-specifier.
25063
25064 base-specifier:
25065 :: [opt] nested-name-specifier [opt] class-name
25066 virtual access-specifier [opt] :: [opt] nested-name-specifier
25067 [opt] class-name
25068 access-specifier virtual [opt] :: [opt] nested-name-specifier
25069 [opt] class-name
25070
25071 Returns a TREE_LIST. The TREE_PURPOSE will be one of
25072 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
25073 indicate the specifiers provided. The TREE_VALUE will be a TYPE
25074 (or the ERROR_MARK_NODE) indicating the type that was specified. */
25075
25076 static tree
25077 cp_parser_base_specifier (cp_parser* parser)
25078 {
25079 cp_token *token;
25080 bool done = false;
25081 bool virtual_p = false;
25082 bool duplicate_virtual_error_issued_p = false;
25083 bool duplicate_access_error_issued_p = false;
25084 bool class_scope_p, template_p;
25085 tree access = access_default_node;
25086 tree type;
25087
25088 /* Process the optional `virtual' and `access-specifier'. */
25089 while (!done)
25090 {
25091 /* Peek at the next token. */
25092 token = cp_lexer_peek_token (parser->lexer);
25093 /* Process `virtual'. */
25094 switch (token->keyword)
25095 {
25096 case RID_VIRTUAL:
25097 /* If `virtual' appears more than once, issue an error. */
25098 if (virtual_p && !duplicate_virtual_error_issued_p)
25099 {
25100 cp_parser_error (parser,
25101 "%<virtual%> specified more than once in base-specifier");
25102 duplicate_virtual_error_issued_p = true;
25103 }
25104
25105 virtual_p = true;
25106
25107 /* Consume the `virtual' token. */
25108 cp_lexer_consume_token (parser->lexer);
25109
25110 break;
25111
25112 case RID_PUBLIC:
25113 case RID_PROTECTED:
25114 case RID_PRIVATE:
25115 /* If more than one access specifier appears, issue an
25116 error. */
25117 if (access != access_default_node
25118 && !duplicate_access_error_issued_p)
25119 {
25120 cp_parser_error (parser,
25121 "more than one access specifier in base-specifier");
25122 duplicate_access_error_issued_p = true;
25123 }
25124
25125 access = ridpointers[(int) token->keyword];
25126
25127 /* Consume the access-specifier. */
25128 cp_lexer_consume_token (parser->lexer);
25129
25130 break;
25131
25132 default:
25133 done = true;
25134 break;
25135 }
25136 }
25137 /* It is not uncommon to see programs mechanically, erroneously, use
25138 the 'typename' keyword to denote (dependent) qualified types
25139 as base classes. */
25140 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25141 {
25142 token = cp_lexer_peek_token (parser->lexer);
25143 if (!processing_template_decl)
25144 error_at (token->location,
25145 "keyword %<typename%> not allowed outside of templates");
25146 else
25147 error_at (token->location,
25148 "keyword %<typename%> not allowed in this context "
25149 "(the base class is implicitly a type)");
25150 cp_lexer_consume_token (parser->lexer);
25151 }
25152
25153 /* Look for the optional `::' operator. */
25154 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
25155 /* Look for the nested-name-specifier. The simplest way to
25156 implement:
25157
25158 [temp.res]
25159
25160 The keyword `typename' is not permitted in a base-specifier or
25161 mem-initializer; in these contexts a qualified name that
25162 depends on a template-parameter is implicitly assumed to be a
25163 type name.
25164
25165 is to pretend that we have seen the `typename' keyword at this
25166 point. */
25167 cp_parser_nested_name_specifier_opt (parser,
25168 /*typename_keyword_p=*/true,
25169 /*check_dependency_p=*/true,
25170 /*type_p=*/true,
25171 /*is_declaration=*/true);
25172 /* If the base class is given by a qualified name, assume that names
25173 we see are type names or templates, as appropriate. */
25174 class_scope_p = (parser->scope && TYPE_P (parser->scope));
25175 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
25176
25177 if (!parser->scope
25178 && cp_lexer_next_token_is_decltype (parser->lexer))
25179 /* DR 950 allows decltype as a base-specifier. */
25180 type = cp_parser_decltype (parser);
25181 else
25182 {
25183 /* Otherwise, look for the class-name. */
25184 type = cp_parser_class_name (parser,
25185 class_scope_p,
25186 template_p,
25187 typename_type,
25188 /*check_dependency_p=*/true,
25189 /*class_head_p=*/false,
25190 /*is_declaration=*/true);
25191 type = TREE_TYPE (type);
25192 }
25193
25194 if (type == error_mark_node)
25195 return error_mark_node;
25196
25197 return finish_base_specifier (type, access, virtual_p);
25198 }
25199
25200 /* Exception handling [gram.exception] */
25201
25202 /* Parse an (optional) noexcept-specification.
25203
25204 noexcept-specification:
25205 noexcept ( constant-expression ) [opt]
25206
25207 If no noexcept-specification is present, returns NULL_TREE.
25208 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25209 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25210 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25211 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25212 in which case a boolean condition is returned instead. */
25213
25214 static tree
25215 cp_parser_noexcept_specification_opt (cp_parser* parser,
25216 bool require_constexpr,
25217 bool* consumed_expr,
25218 bool return_cond)
25219 {
25220 cp_token *token;
25221 const char *saved_message;
25222
25223 /* Peek at the next token. */
25224 token = cp_lexer_peek_token (parser->lexer);
25225
25226 /* Is it a noexcept-specification? */
25227 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
25228 {
25229 tree expr;
25230 cp_lexer_consume_token (parser->lexer);
25231
25232 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
25233 {
25234 matching_parens parens;
25235 parens.consume_open (parser);
25236
25237 tree save_ccp = current_class_ptr;
25238 tree save_ccr = current_class_ref;
25239
25240 if (current_class_type)
25241 inject_this_parameter (current_class_type, TYPE_UNQUALIFIED);
25242
25243 if (require_constexpr)
25244 {
25245 /* Types may not be defined in an exception-specification. */
25246 saved_message = parser->type_definition_forbidden_message;
25247 parser->type_definition_forbidden_message
25248 = G_("types may not be defined in an exception-specification");
25249
25250 bool non_constant_p;
25251 expr
25252 = cp_parser_constant_expression (parser,
25253 /*allow_non_constant=*/true,
25254 &non_constant_p);
25255 if (non_constant_p
25256 && !require_potential_rvalue_constant_expression (expr))
25257 {
25258 expr = NULL_TREE;
25259 return_cond = true;
25260 }
25261
25262 /* Restore the saved message. */
25263 parser->type_definition_forbidden_message = saved_message;
25264 }
25265 else
25266 {
25267 expr = cp_parser_expression (parser);
25268 *consumed_expr = true;
25269 }
25270
25271 parens.require_close (parser);
25272
25273 current_class_ptr = save_ccp;
25274 current_class_ref = save_ccr;
25275 }
25276 else
25277 {
25278 expr = boolean_true_node;
25279 if (!require_constexpr)
25280 *consumed_expr = false;
25281 }
25282
25283 /* We cannot build a noexcept-spec right away because this will check
25284 that expr is a constexpr. */
25285 if (!return_cond)
25286 return build_noexcept_spec (expr, tf_warning_or_error);
25287 else
25288 return expr;
25289 }
25290 else
25291 return NULL_TREE;
25292 }
25293
25294 /* Parse an (optional) exception-specification.
25295
25296 exception-specification:
25297 throw ( type-id-list [opt] )
25298
25299 Returns a TREE_LIST representing the exception-specification. The
25300 TREE_VALUE of each node is a type. */
25301
25302 static tree
25303 cp_parser_exception_specification_opt (cp_parser* parser)
25304 {
25305 cp_token *token;
25306 tree type_id_list;
25307 const char *saved_message;
25308
25309 /* Peek at the next token. */
25310 token = cp_lexer_peek_token (parser->lexer);
25311
25312 /* Is it a noexcept-specification? */
25313 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
25314 false);
25315 if (type_id_list != NULL_TREE)
25316 return type_id_list;
25317
25318 /* If it's not `throw', then there's no exception-specification. */
25319 if (!cp_parser_is_keyword (token, RID_THROW))
25320 return NULL_TREE;
25321
25322 location_t loc = token->location;
25323
25324 /* Consume the `throw'. */
25325 cp_lexer_consume_token (parser->lexer);
25326
25327 /* Look for the `('. */
25328 matching_parens parens;
25329 parens.require_open (parser);
25330
25331 /* Peek at the next token. */
25332 token = cp_lexer_peek_token (parser->lexer);
25333 /* If it's not a `)', then there is a type-id-list. */
25334 if (token->type != CPP_CLOSE_PAREN)
25335 {
25336 /* Types may not be defined in an exception-specification. */
25337 saved_message = parser->type_definition_forbidden_message;
25338 parser->type_definition_forbidden_message
25339 = G_("types may not be defined in an exception-specification");
25340 /* Parse the type-id-list. */
25341 type_id_list = cp_parser_type_id_list (parser);
25342 /* Restore the saved message. */
25343 parser->type_definition_forbidden_message = saved_message;
25344
25345 if (cxx_dialect >= cxx17)
25346 {
25347 error_at (loc, "ISO C++17 does not allow dynamic exception "
25348 "specifications");
25349 type_id_list = NULL_TREE;
25350 }
25351 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
25352 warning_at (loc, OPT_Wdeprecated,
25353 "dynamic exception specifications are deprecated in "
25354 "C++11");
25355 }
25356 /* In C++17, throw() is equivalent to noexcept (true). throw()
25357 is deprecated in C++11 and above as well, but is still widely used,
25358 so don't warn about it yet. */
25359 else if (cxx_dialect >= cxx17)
25360 type_id_list = noexcept_true_spec;
25361 else
25362 type_id_list = empty_except_spec;
25363
25364 /* Look for the `)'. */
25365 parens.require_close (parser);
25366
25367 return type_id_list;
25368 }
25369
25370 /* Parse an (optional) type-id-list.
25371
25372 type-id-list:
25373 type-id ... [opt]
25374 type-id-list , type-id ... [opt]
25375
25376 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
25377 in the order that the types were presented. */
25378
25379 static tree
25380 cp_parser_type_id_list (cp_parser* parser)
25381 {
25382 tree types = NULL_TREE;
25383
25384 while (true)
25385 {
25386 cp_token *token;
25387 tree type;
25388
25389 token = cp_lexer_peek_token (parser->lexer);
25390
25391 /* Get the next type-id. */
25392 type = cp_parser_type_id (parser);
25393 /* Check for invalid 'auto'. */
25394 if (flag_concepts && type_uses_auto (type))
25395 {
25396 error_at (token->location,
25397 "invalid use of %<auto%> in exception-specification");
25398 type = error_mark_node;
25399 }
25400 /* Parse the optional ellipsis. */
25401 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25402 {
25403 /* Consume the `...'. */
25404 cp_lexer_consume_token (parser->lexer);
25405
25406 /* Turn the type into a pack expansion expression. */
25407 type = make_pack_expansion (type);
25408 }
25409 /* Add it to the list. */
25410 types = add_exception_specifier (types, type, /*complain=*/1);
25411 /* Peek at the next token. */
25412 token = cp_lexer_peek_token (parser->lexer);
25413 /* If it is not a `,', we are done. */
25414 if (token->type != CPP_COMMA)
25415 break;
25416 /* Consume the `,'. */
25417 cp_lexer_consume_token (parser->lexer);
25418 }
25419
25420 return nreverse (types);
25421 }
25422
25423 /* Parse a try-block.
25424
25425 try-block:
25426 try compound-statement handler-seq */
25427
25428 static tree
25429 cp_parser_try_block (cp_parser* parser)
25430 {
25431 tree try_block;
25432
25433 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
25434 if (parser->in_function_body
25435 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25436 && cxx_dialect < cxx2a)
25437 pedwarn (input_location, 0,
25438 "%<try%> in %<constexpr%> function only "
25439 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
25440
25441 try_block = begin_try_block ();
25442 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
25443 finish_try_block (try_block);
25444 cp_parser_handler_seq (parser);
25445 finish_handler_sequence (try_block);
25446
25447 return try_block;
25448 }
25449
25450 /* Parse a function-try-block.
25451
25452 function-try-block:
25453 try ctor-initializer [opt] function-body handler-seq */
25454
25455 static void
25456 cp_parser_function_try_block (cp_parser* parser)
25457 {
25458 tree compound_stmt;
25459 tree try_block;
25460
25461 /* Look for the `try' keyword. */
25462 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
25463 return;
25464 /* Let the rest of the front end know where we are. */
25465 try_block = begin_function_try_block (&compound_stmt);
25466 /* Parse the function-body. */
25467 cp_parser_ctor_initializer_opt_and_function_body
25468 (parser, /*in_function_try_block=*/true);
25469 /* We're done with the `try' part. */
25470 finish_function_try_block (try_block);
25471 /* Parse the handlers. */
25472 cp_parser_handler_seq (parser);
25473 /* We're done with the handlers. */
25474 finish_function_handler_sequence (try_block, compound_stmt);
25475 }
25476
25477 /* Parse a handler-seq.
25478
25479 handler-seq:
25480 handler handler-seq [opt] */
25481
25482 static void
25483 cp_parser_handler_seq (cp_parser* parser)
25484 {
25485 while (true)
25486 {
25487 cp_token *token;
25488
25489 /* Parse the handler. */
25490 cp_parser_handler (parser);
25491 /* Peek at the next token. */
25492 token = cp_lexer_peek_token (parser->lexer);
25493 /* If it's not `catch' then there are no more handlers. */
25494 if (!cp_parser_is_keyword (token, RID_CATCH))
25495 break;
25496 }
25497 }
25498
25499 /* Parse a handler.
25500
25501 handler:
25502 catch ( exception-declaration ) compound-statement */
25503
25504 static void
25505 cp_parser_handler (cp_parser* parser)
25506 {
25507 tree handler;
25508 tree declaration;
25509
25510 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
25511 handler = begin_handler ();
25512 matching_parens parens;
25513 parens.require_open (parser);
25514 declaration = cp_parser_exception_declaration (parser);
25515 finish_handler_parms (declaration, handler);
25516 parens.require_close (parser);
25517 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
25518 finish_handler (handler);
25519 }
25520
25521 /* Parse an exception-declaration.
25522
25523 exception-declaration:
25524 type-specifier-seq declarator
25525 type-specifier-seq abstract-declarator
25526 type-specifier-seq
25527 ...
25528
25529 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25530 ellipsis variant is used. */
25531
25532 static tree
25533 cp_parser_exception_declaration (cp_parser* parser)
25534 {
25535 cp_decl_specifier_seq type_specifiers;
25536 cp_declarator *declarator;
25537 const char *saved_message;
25538
25539 /* If it's an ellipsis, it's easy to handle. */
25540 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25541 {
25542 /* Consume the `...' token. */
25543 cp_lexer_consume_token (parser->lexer);
25544 return NULL_TREE;
25545 }
25546
25547 /* Types may not be defined in exception-declarations. */
25548 saved_message = parser->type_definition_forbidden_message;
25549 parser->type_definition_forbidden_message
25550 = G_("types may not be defined in exception-declarations");
25551
25552 /* Parse the type-specifier-seq. */
25553 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
25554 /*is_declaration=*/true,
25555 /*is_trailing_return=*/false,
25556 &type_specifiers);
25557 /* If it's a `)', then there is no declarator. */
25558 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25559 declarator = NULL;
25560 else
25561 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
25562 CP_PARSER_FLAGS_NONE,
25563 /*ctor_dtor_or_conv_p=*/NULL,
25564 /*parenthesized_p=*/NULL,
25565 /*member_p=*/false,
25566 /*friend_p=*/false,
25567 /*static_p=*/false);
25568
25569 /* Restore the saved message. */
25570 parser->type_definition_forbidden_message = saved_message;
25571
25572 if (!type_specifiers.any_specifiers_p)
25573 return error_mark_node;
25574
25575 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
25576 }
25577
25578 /* Parse a throw-expression.
25579
25580 throw-expression:
25581 throw assignment-expression [opt]
25582
25583 Returns a THROW_EXPR representing the throw-expression. */
25584
25585 static tree
25586 cp_parser_throw_expression (cp_parser* parser)
25587 {
25588 tree expression;
25589 cp_token* token;
25590
25591 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
25592 token = cp_lexer_peek_token (parser->lexer);
25593 /* Figure out whether or not there is an assignment-expression
25594 following the "throw" keyword. */
25595 if (token->type == CPP_COMMA
25596 || token->type == CPP_SEMICOLON
25597 || token->type == CPP_CLOSE_PAREN
25598 || token->type == CPP_CLOSE_SQUARE
25599 || token->type == CPP_CLOSE_BRACE
25600 || token->type == CPP_COLON)
25601 expression = NULL_TREE;
25602 else
25603 expression = cp_parser_assignment_expression (parser);
25604
25605 return build_throw (expression);
25606 }
25607
25608 /* GNU Extensions */
25609
25610 /* Parse an (optional) asm-specification.
25611
25612 asm-specification:
25613 asm ( string-literal )
25614
25615 If the asm-specification is present, returns a STRING_CST
25616 corresponding to the string-literal. Otherwise, returns
25617 NULL_TREE. */
25618
25619 static tree
25620 cp_parser_asm_specification_opt (cp_parser* parser)
25621 {
25622 cp_token *token;
25623 tree asm_specification;
25624
25625 /* Peek at the next token. */
25626 token = cp_lexer_peek_token (parser->lexer);
25627 /* If the next token isn't the `asm' keyword, then there's no
25628 asm-specification. */
25629 if (!cp_parser_is_keyword (token, RID_ASM))
25630 return NULL_TREE;
25631
25632 /* Consume the `asm' token. */
25633 cp_lexer_consume_token (parser->lexer);
25634 /* Look for the `('. */
25635 matching_parens parens;
25636 parens.require_open (parser);
25637
25638 /* Look for the string-literal. */
25639 asm_specification = cp_parser_string_literal (parser, false, false);
25640
25641 /* Look for the `)'. */
25642 parens.require_close (parser);
25643
25644 return asm_specification;
25645 }
25646
25647 /* Parse an asm-operand-list.
25648
25649 asm-operand-list:
25650 asm-operand
25651 asm-operand-list , asm-operand
25652
25653 asm-operand:
25654 string-literal ( expression )
25655 [ string-literal ] string-literal ( expression )
25656
25657 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25658 each node is the expression. The TREE_PURPOSE is itself a
25659 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25660 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25661 is a STRING_CST for the string literal before the parenthesis. Returns
25662 ERROR_MARK_NODE if any of the operands are invalid. */
25663
25664 static tree
25665 cp_parser_asm_operand_list (cp_parser* parser)
25666 {
25667 tree asm_operands = NULL_TREE;
25668 bool invalid_operands = false;
25669
25670 while (true)
25671 {
25672 tree string_literal;
25673 tree expression;
25674 tree name;
25675
25676 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25677 {
25678 /* Consume the `[' token. */
25679 cp_lexer_consume_token (parser->lexer);
25680 /* Read the operand name. */
25681 name = cp_parser_identifier (parser);
25682 if (name != error_mark_node)
25683 name = build_string (IDENTIFIER_LENGTH (name),
25684 IDENTIFIER_POINTER (name));
25685 /* Look for the closing `]'. */
25686 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25687 }
25688 else
25689 name = NULL_TREE;
25690 /* Look for the string-literal. */
25691 string_literal = cp_parser_string_literal (parser, false, false);
25692
25693 /* Look for the `('. */
25694 matching_parens parens;
25695 parens.require_open (parser);
25696 /* Parse the expression. */
25697 expression = cp_parser_expression (parser);
25698 /* Look for the `)'. */
25699 parens.require_close (parser);
25700
25701 if (name == error_mark_node
25702 || string_literal == error_mark_node
25703 || expression == error_mark_node)
25704 invalid_operands = true;
25705
25706 /* Add this operand to the list. */
25707 asm_operands = tree_cons (build_tree_list (name, string_literal),
25708 expression,
25709 asm_operands);
25710 /* If the next token is not a `,', there are no more
25711 operands. */
25712 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25713 break;
25714 /* Consume the `,'. */
25715 cp_lexer_consume_token (parser->lexer);
25716 }
25717
25718 return invalid_operands ? error_mark_node : nreverse (asm_operands);
25719 }
25720
25721 /* Parse an asm-clobber-list.
25722
25723 asm-clobber-list:
25724 string-literal
25725 asm-clobber-list , string-literal
25726
25727 Returns a TREE_LIST, indicating the clobbers in the order that they
25728 appeared. The TREE_VALUE of each node is a STRING_CST. */
25729
25730 static tree
25731 cp_parser_asm_clobber_list (cp_parser* parser)
25732 {
25733 tree clobbers = NULL_TREE;
25734
25735 while (true)
25736 {
25737 tree string_literal;
25738
25739 /* Look for the string literal. */
25740 string_literal = cp_parser_string_literal (parser, false, false);
25741 /* Add it to the list. */
25742 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25743 /* If the next token is not a `,', then the list is
25744 complete. */
25745 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25746 break;
25747 /* Consume the `,' token. */
25748 cp_lexer_consume_token (parser->lexer);
25749 }
25750
25751 return clobbers;
25752 }
25753
25754 /* Parse an asm-label-list.
25755
25756 asm-label-list:
25757 identifier
25758 asm-label-list , identifier
25759
25760 Returns a TREE_LIST, indicating the labels in the order that they
25761 appeared. The TREE_VALUE of each node is a label. */
25762
25763 static tree
25764 cp_parser_asm_label_list (cp_parser* parser)
25765 {
25766 tree labels = NULL_TREE;
25767
25768 while (true)
25769 {
25770 tree identifier, label, name;
25771
25772 /* Look for the identifier. */
25773 identifier = cp_parser_identifier (parser);
25774 if (!error_operand_p (identifier))
25775 {
25776 label = lookup_label (identifier);
25777 if (TREE_CODE (label) == LABEL_DECL)
25778 {
25779 TREE_USED (label) = 1;
25780 check_goto (label);
25781 name = build_string (IDENTIFIER_LENGTH (identifier),
25782 IDENTIFIER_POINTER (identifier));
25783 labels = tree_cons (name, label, labels);
25784 }
25785 }
25786 /* If the next token is not a `,', then the list is
25787 complete. */
25788 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25789 break;
25790 /* Consume the `,' token. */
25791 cp_lexer_consume_token (parser->lexer);
25792 }
25793
25794 return nreverse (labels);
25795 }
25796
25797 /* Return TRUE iff the next tokens in the stream are possibly the
25798 beginning of a GNU extension attribute. */
25799
25800 static bool
25801 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25802 {
25803 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25804 }
25805
25806 /* Return TRUE iff the next tokens in the stream are possibly the
25807 beginning of a standard C++-11 attribute specifier. */
25808
25809 static bool
25810 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25811 {
25812 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25813 }
25814
25815 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25816 beginning of a standard C++-11 attribute specifier. */
25817
25818 static bool
25819 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25820 {
25821 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25822
25823 return (cxx_dialect >= cxx11
25824 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25825 || (token->type == CPP_OPEN_SQUARE
25826 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25827 && token->type == CPP_OPEN_SQUARE)));
25828 }
25829
25830 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25831 beginning of a GNU extension attribute. */
25832
25833 static bool
25834 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25835 {
25836 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25837
25838 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25839 }
25840
25841 /* Return true iff the next tokens can be the beginning of either a
25842 GNU attribute list, or a standard C++11 attribute sequence. */
25843
25844 static bool
25845 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25846 {
25847 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25848 || cp_next_tokens_can_be_std_attribute_p (parser));
25849 }
25850
25851 /* Return true iff the next Nth tokens can be the beginning of either
25852 a GNU attribute list, or a standard C++11 attribute sequence. */
25853
25854 static bool
25855 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25856 {
25857 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25858 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25859 }
25860
25861 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25862 of GNU attributes, or return NULL. */
25863
25864 static tree
25865 cp_parser_attributes_opt (cp_parser *parser)
25866 {
25867 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25868 return cp_parser_gnu_attributes_opt (parser);
25869 return cp_parser_std_attribute_spec_seq (parser);
25870 }
25871
25872 /* Parse an (optional) series of attributes.
25873
25874 attributes:
25875 attributes attribute
25876
25877 attribute:
25878 __attribute__ (( attribute-list [opt] ))
25879
25880 The return value is as for cp_parser_gnu_attribute_list. */
25881
25882 static tree
25883 cp_parser_gnu_attributes_opt (cp_parser* parser)
25884 {
25885 tree attributes = NULL_TREE;
25886
25887 temp_override<bool> cleanup
25888 (parser->auto_is_implicit_function_template_parm_p, false);
25889
25890 while (true)
25891 {
25892 cp_token *token;
25893 tree attribute_list;
25894 bool ok = true;
25895
25896 /* Peek at the next token. */
25897 token = cp_lexer_peek_token (parser->lexer);
25898 /* If it's not `__attribute__', then we're done. */
25899 if (token->keyword != RID_ATTRIBUTE)
25900 break;
25901
25902 /* Consume the `__attribute__' keyword. */
25903 cp_lexer_consume_token (parser->lexer);
25904 /* Look for the two `(' tokens. */
25905 matching_parens outer_parens;
25906 if (!outer_parens.require_open (parser))
25907 ok = false;
25908 matching_parens inner_parens;
25909 if (!inner_parens.require_open (parser))
25910 ok = false;
25911
25912 /* Peek at the next token. */
25913 token = cp_lexer_peek_token (parser->lexer);
25914 if (token->type != CPP_CLOSE_PAREN)
25915 /* Parse the attribute-list. */
25916 attribute_list = cp_parser_gnu_attribute_list (parser);
25917 else
25918 /* If the next token is a `)', then there is no attribute
25919 list. */
25920 attribute_list = NULL;
25921
25922 /* Look for the two `)' tokens. */
25923 if (!inner_parens.require_close (parser))
25924 ok = false;
25925 if (!outer_parens.require_close (parser))
25926 ok = false;
25927 if (!ok)
25928 cp_parser_skip_to_end_of_statement (parser);
25929
25930 /* Add these new attributes to the list. */
25931 attributes = attr_chainon (attributes, attribute_list);
25932 }
25933
25934 return attributes;
25935 }
25936
25937 /* Parse a GNU attribute-list.
25938
25939 attribute-list:
25940 attribute
25941 attribute-list , attribute
25942
25943 attribute:
25944 identifier
25945 identifier ( identifier )
25946 identifier ( identifier , expression-list )
25947 identifier ( expression-list )
25948
25949 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25950 to an attribute. The TREE_PURPOSE of each node is the identifier
25951 indicating which attribute is in use. The TREE_VALUE represents
25952 the arguments, if any. */
25953
25954 static tree
25955 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
25956 {
25957 tree attribute_list = NULL_TREE;
25958 bool save_translate_strings_p = parser->translate_strings_p;
25959
25960 /* Don't create wrapper nodes within attributes: the
25961 handlers don't know how to handle them. */
25962 auto_suppress_location_wrappers sentinel;
25963
25964 parser->translate_strings_p = false;
25965 while (true)
25966 {
25967 cp_token *token;
25968 tree identifier;
25969 tree attribute;
25970
25971 /* Look for the identifier. We also allow keywords here; for
25972 example `__attribute__ ((const))' is legal. */
25973 token = cp_lexer_peek_token (parser->lexer);
25974 if (token->type == CPP_NAME
25975 || token->type == CPP_KEYWORD)
25976 {
25977 tree arguments = NULL_TREE;
25978
25979 /* Consume the token, but save it since we need it for the
25980 SIMD enabled function parsing. */
25981 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25982
25983 /* Save away the identifier that indicates which attribute
25984 this is. */
25985 identifier = (token->type == CPP_KEYWORD)
25986 /* For keywords, use the canonical spelling, not the
25987 parsed identifier. */
25988 ? ridpointers[(int) token->keyword]
25989 : id_token->u.value;
25990
25991 identifier = canonicalize_attr_name (identifier);
25992 attribute = build_tree_list (identifier, NULL_TREE);
25993
25994 /* Peek at the next token. */
25995 token = cp_lexer_peek_token (parser->lexer);
25996 /* If it's an `(', then parse the attribute arguments. */
25997 if (token->type == CPP_OPEN_PAREN)
25998 {
25999 vec<tree, va_gc> *vec;
26000 int attr_flag = (attribute_takes_identifier_p (identifier)
26001 ? id_attr : normal_attr);
26002 vec = cp_parser_parenthesized_expression_list
26003 (parser, attr_flag, /*cast_p=*/false,
26004 /*allow_expansion_p=*/false,
26005 /*non_constant_p=*/NULL);
26006 if (vec == NULL)
26007 arguments = error_mark_node;
26008 else
26009 {
26010 arguments = build_tree_list_vec (vec);
26011 release_tree_vector (vec);
26012 }
26013 /* Save the arguments away. */
26014 TREE_VALUE (attribute) = arguments;
26015 }
26016
26017 if (arguments != error_mark_node)
26018 {
26019 /* Add this attribute to the list. */
26020 TREE_CHAIN (attribute) = attribute_list;
26021 attribute_list = attribute;
26022 }
26023
26024 token = cp_lexer_peek_token (parser->lexer);
26025 }
26026 /* Unless EXACTLY_ONE is set look for more attributes.
26027 If the next token isn't a `,', we're done. */
26028 if (exactly_one || token->type != CPP_COMMA)
26029 break;
26030
26031 /* Consume the comma and keep going. */
26032 cp_lexer_consume_token (parser->lexer);
26033 }
26034 parser->translate_strings_p = save_translate_strings_p;
26035
26036 /* We built up the list in reverse order. */
26037 return nreverse (attribute_list);
26038 }
26039
26040 /* Parse a standard C++11 attribute.
26041
26042 The returned representation is a TREE_LIST which TREE_PURPOSE is
26043 the scoped name of the attribute, and the TREE_VALUE is its
26044 arguments list.
26045
26046 Note that the scoped name of the attribute is itself a TREE_LIST
26047 which TREE_PURPOSE is the namespace of the attribute, and
26048 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
26049 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
26050 and which TREE_PURPOSE is directly the attribute name.
26051
26052 Clients of the attribute code should use get_attribute_namespace
26053 and get_attribute_name to get the actual namespace and name of
26054 attributes, regardless of their being GNU or C++11 attributes.
26055
26056 attribute:
26057 attribute-token attribute-argument-clause [opt]
26058
26059 attribute-token:
26060 identifier
26061 attribute-scoped-token
26062
26063 attribute-scoped-token:
26064 attribute-namespace :: identifier
26065
26066 attribute-namespace:
26067 identifier
26068
26069 attribute-argument-clause:
26070 ( balanced-token-seq )
26071
26072 balanced-token-seq:
26073 balanced-token [opt]
26074 balanced-token-seq balanced-token
26075
26076 balanced-token:
26077 ( balanced-token-seq )
26078 [ balanced-token-seq ]
26079 { balanced-token-seq }. */
26080
26081 static tree
26082 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
26083 {
26084 tree attribute, attr_id = NULL_TREE, arguments;
26085 cp_token *token;
26086
26087 temp_override<bool> cleanup
26088 (parser->auto_is_implicit_function_template_parm_p, false);
26089
26090 /* First, parse name of the attribute, a.k.a attribute-token. */
26091
26092 token = cp_lexer_peek_token (parser->lexer);
26093 if (token->type == CPP_NAME)
26094 attr_id = token->u.value;
26095 else if (token->type == CPP_KEYWORD)
26096 attr_id = ridpointers[(int) token->keyword];
26097 else if (token->flags & NAMED_OP)
26098 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26099
26100 if (attr_id == NULL_TREE)
26101 return NULL_TREE;
26102
26103 cp_lexer_consume_token (parser->lexer);
26104
26105 token = cp_lexer_peek_token (parser->lexer);
26106 if (token->type == CPP_SCOPE)
26107 {
26108 /* We are seeing a scoped attribute token. */
26109
26110 cp_lexer_consume_token (parser->lexer);
26111 if (attr_ns)
26112 error_at (token->location, "attribute using prefix used together "
26113 "with scoped attribute token");
26114 attr_ns = attr_id;
26115
26116 token = cp_lexer_consume_token (parser->lexer);
26117 if (token->type == CPP_NAME)
26118 attr_id = token->u.value;
26119 else if (token->type == CPP_KEYWORD)
26120 attr_id = ridpointers[(int) token->keyword];
26121 else if (token->flags & NAMED_OP)
26122 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26123 else
26124 {
26125 error_at (token->location,
26126 "expected an identifier for the attribute name");
26127 return error_mark_node;
26128 }
26129
26130 attr_ns = canonicalize_attr_name (attr_ns);
26131 attr_id = canonicalize_attr_name (attr_id);
26132 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26133 NULL_TREE);
26134 token = cp_lexer_peek_token (parser->lexer);
26135 }
26136 else if (attr_ns)
26137 {
26138 attr_ns = canonicalize_attr_name (attr_ns);
26139 attr_id = canonicalize_attr_name (attr_id);
26140 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26141 NULL_TREE);
26142 }
26143 else
26144 {
26145 attr_id = canonicalize_attr_name (attr_id);
26146 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
26147 NULL_TREE);
26148 /* C++11 noreturn attribute is equivalent to GNU's. */
26149 if (is_attribute_p ("noreturn", attr_id))
26150 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26151 /* C++14 deprecated attribute is equivalent to GNU's. */
26152 else if (is_attribute_p ("deprecated", attr_id))
26153 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26154 /* C++17 fallthrough attribute is equivalent to GNU's. */
26155 else if (is_attribute_p ("fallthrough", attr_id))
26156 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26157 /* Transactional Memory TS optimize_for_synchronized attribute is
26158 equivalent to GNU transaction_callable. */
26159 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
26160 TREE_PURPOSE (attribute)
26161 = get_identifier ("transaction_callable");
26162 /* Transactional Memory attributes are GNU attributes. */
26163 else if (tm_attr_to_mask (attr_id))
26164 TREE_PURPOSE (attribute) = attr_id;
26165 }
26166
26167 /* Now parse the optional argument clause of the attribute. */
26168
26169 if (token->type != CPP_OPEN_PAREN)
26170 return attribute;
26171
26172 {
26173 vec<tree, va_gc> *vec;
26174 int attr_flag = normal_attr;
26175
26176 if (attr_ns == gnu_identifier
26177 && attribute_takes_identifier_p (attr_id))
26178 /* A GNU attribute that takes an identifier in parameter. */
26179 attr_flag = id_attr;
26180
26181 vec = cp_parser_parenthesized_expression_list
26182 (parser, attr_flag, /*cast_p=*/false,
26183 /*allow_expansion_p=*/true,
26184 /*non_constant_p=*/NULL);
26185 if (vec == NULL)
26186 arguments = error_mark_node;
26187 else
26188 {
26189 arguments = build_tree_list_vec (vec);
26190 release_tree_vector (vec);
26191 }
26192
26193 if (arguments == error_mark_node)
26194 attribute = error_mark_node;
26195 else
26196 TREE_VALUE (attribute) = arguments;
26197 }
26198
26199 return attribute;
26200 }
26201
26202 /* Check that the attribute ATTRIBUTE appears at most once in the
26203 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
26204 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
26205 isn't implemented yet in GCC. */
26206
26207 static void
26208 cp_parser_check_std_attribute (tree attributes, tree attribute)
26209 {
26210 if (attributes)
26211 {
26212 tree name = get_attribute_name (attribute);
26213 if (is_attribute_p ("noreturn", name)
26214 && lookup_attribute ("noreturn", attributes))
26215 error ("attribute %<noreturn%> can appear at most once "
26216 "in an attribute-list");
26217 else if (is_attribute_p ("deprecated", name)
26218 && lookup_attribute ("deprecated", attributes))
26219 error ("attribute %<deprecated%> can appear at most once "
26220 "in an attribute-list");
26221 }
26222 }
26223
26224 /* Parse a list of standard C++-11 attributes.
26225
26226 attribute-list:
26227 attribute [opt]
26228 attribute-list , attribute[opt]
26229 attribute ...
26230 attribute-list , attribute ...
26231 */
26232
26233 static tree
26234 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
26235 {
26236 tree attributes = NULL_TREE, attribute = NULL_TREE;
26237 cp_token *token = NULL;
26238
26239 while (true)
26240 {
26241 attribute = cp_parser_std_attribute (parser, attr_ns);
26242 if (attribute == error_mark_node)
26243 break;
26244 if (attribute != NULL_TREE)
26245 {
26246 cp_parser_check_std_attribute (attributes, attribute);
26247 TREE_CHAIN (attribute) = attributes;
26248 attributes = attribute;
26249 }
26250 token = cp_lexer_peek_token (parser->lexer);
26251 if (token->type == CPP_ELLIPSIS)
26252 {
26253 cp_lexer_consume_token (parser->lexer);
26254 if (attribute == NULL_TREE)
26255 error_at (token->location,
26256 "expected attribute before %<...%>");
26257 else
26258 {
26259 tree pack = make_pack_expansion (TREE_VALUE (attribute));
26260 if (pack == error_mark_node)
26261 return error_mark_node;
26262 TREE_VALUE (attribute) = pack;
26263 }
26264 token = cp_lexer_peek_token (parser->lexer);
26265 }
26266 if (token->type != CPP_COMMA)
26267 break;
26268 cp_lexer_consume_token (parser->lexer);
26269 }
26270 attributes = nreverse (attributes);
26271 return attributes;
26272 }
26273
26274 /* Parse a standard C++-11 attribute specifier.
26275
26276 attribute-specifier:
26277 [ [ attribute-using-prefix [opt] attribute-list ] ]
26278 alignment-specifier
26279
26280 attribute-using-prefix:
26281 using attribute-namespace :
26282
26283 alignment-specifier:
26284 alignas ( type-id ... [opt] )
26285 alignas ( alignment-expression ... [opt] ). */
26286
26287 static tree
26288 cp_parser_std_attribute_spec (cp_parser *parser)
26289 {
26290 tree attributes = NULL_TREE;
26291 cp_token *token = cp_lexer_peek_token (parser->lexer);
26292
26293 if (token->type == CPP_OPEN_SQUARE
26294 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
26295 {
26296 tree attr_ns = NULL_TREE;
26297
26298 cp_lexer_consume_token (parser->lexer);
26299 cp_lexer_consume_token (parser->lexer);
26300
26301 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26302 {
26303 token = cp_lexer_peek_nth_token (parser->lexer, 2);
26304 if (token->type == CPP_NAME)
26305 attr_ns = token->u.value;
26306 else if (token->type == CPP_KEYWORD)
26307 attr_ns = ridpointers[(int) token->keyword];
26308 else if (token->flags & NAMED_OP)
26309 attr_ns = get_identifier (cpp_type2name (token->type,
26310 token->flags));
26311 if (attr_ns
26312 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
26313 {
26314 if (cxx_dialect < cxx17
26315 && !in_system_header_at (input_location))
26316 pedwarn (input_location, 0,
26317 "attribute using prefix only available "
26318 "with %<-std=c++17%> or %<-std=gnu++17%>");
26319
26320 cp_lexer_consume_token (parser->lexer);
26321 cp_lexer_consume_token (parser->lexer);
26322 cp_lexer_consume_token (parser->lexer);
26323 }
26324 else
26325 attr_ns = NULL_TREE;
26326 }
26327
26328 attributes = cp_parser_std_attribute_list (parser, attr_ns);
26329
26330 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
26331 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
26332 cp_parser_skip_to_end_of_statement (parser);
26333 else
26334 /* Warn about parsing c++11 attribute in non-c++11 mode, only
26335 when we are sure that we have actually parsed them. */
26336 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26337 }
26338 else
26339 {
26340 tree alignas_expr;
26341
26342 /* Look for an alignment-specifier. */
26343
26344 token = cp_lexer_peek_token (parser->lexer);
26345
26346 if (token->type != CPP_KEYWORD
26347 || token->keyword != RID_ALIGNAS)
26348 return NULL_TREE;
26349
26350 cp_lexer_consume_token (parser->lexer);
26351 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26352
26353 matching_parens parens;
26354 if (!parens.require_open (parser))
26355 return error_mark_node;
26356
26357 cp_parser_parse_tentatively (parser);
26358 alignas_expr = cp_parser_type_id (parser);
26359
26360 if (!cp_parser_parse_definitely (parser))
26361 {
26362 alignas_expr = cp_parser_assignment_expression (parser);
26363 if (alignas_expr == error_mark_node)
26364 cp_parser_skip_to_end_of_statement (parser);
26365 if (alignas_expr == NULL_TREE
26366 || alignas_expr == error_mark_node)
26367 return alignas_expr;
26368 }
26369
26370 alignas_expr = cxx_alignas_expr (alignas_expr);
26371 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
26372
26373 /* Handle alignas (pack...). */
26374 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26375 {
26376 cp_lexer_consume_token (parser->lexer);
26377 alignas_expr = make_pack_expansion (alignas_expr);
26378 }
26379
26380 /* Something went wrong, so don't build the attribute. */
26381 if (alignas_expr == error_mark_node)
26382 return error_mark_node;
26383
26384 if (!parens.require_close (parser))
26385 return error_mark_node;
26386
26387 /* Build the C++-11 representation of an 'aligned'
26388 attribute. */
26389 attributes
26390 = build_tree_list (build_tree_list (gnu_identifier,
26391 aligned_identifier), alignas_expr);
26392 }
26393
26394 return attributes;
26395 }
26396
26397 /* Parse a standard C++-11 attribute-specifier-seq.
26398
26399 attribute-specifier-seq:
26400 attribute-specifier-seq [opt] attribute-specifier
26401 */
26402
26403 static tree
26404 cp_parser_std_attribute_spec_seq (cp_parser *parser)
26405 {
26406 tree attr_specs = NULL_TREE;
26407 tree attr_last = NULL_TREE;
26408
26409 /* Don't create wrapper nodes within attributes: the
26410 handlers don't know how to handle them. */
26411 auto_suppress_location_wrappers sentinel;
26412
26413 while (true)
26414 {
26415 tree attr_spec = cp_parser_std_attribute_spec (parser);
26416 if (attr_spec == NULL_TREE)
26417 break;
26418 if (attr_spec == error_mark_node)
26419 return error_mark_node;
26420
26421 if (attr_last)
26422 TREE_CHAIN (attr_last) = attr_spec;
26423 else
26424 attr_specs = attr_last = attr_spec;
26425 attr_last = tree_last (attr_last);
26426 }
26427
26428 return attr_specs;
26429 }
26430
26431 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26432 return index of the first token after balanced-token, or N on failure. */
26433
26434 static size_t
26435 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
26436 {
26437 size_t orig_n = n;
26438 int nparens = 0, nbraces = 0, nsquares = 0;
26439 do
26440 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
26441 {
26442 case CPP_PRAGMA_EOL:
26443 if (!parser->lexer->in_pragma)
26444 break;
26445 /* FALLTHRU */
26446 case CPP_EOF:
26447 /* Ran out of tokens. */
26448 return orig_n;
26449 case CPP_OPEN_PAREN:
26450 ++nparens;
26451 break;
26452 case CPP_OPEN_BRACE:
26453 ++nbraces;
26454 break;
26455 case CPP_OPEN_SQUARE:
26456 ++nsquares;
26457 break;
26458 case CPP_CLOSE_PAREN:
26459 --nparens;
26460 break;
26461 case CPP_CLOSE_BRACE:
26462 --nbraces;
26463 break;
26464 case CPP_CLOSE_SQUARE:
26465 --nsquares;
26466 break;
26467 default:
26468 break;
26469 }
26470 while (nparens || nbraces || nsquares);
26471 return n;
26472 }
26473
26474 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26475 return index of the first token after the GNU attribute tokens, or N on
26476 failure. */
26477
26478 static size_t
26479 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
26480 {
26481 while (true)
26482 {
26483 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
26484 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
26485 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
26486 break;
26487
26488 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
26489 if (n2 == n + 2)
26490 break;
26491 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
26492 break;
26493 n = n2 + 1;
26494 }
26495 return n;
26496 }
26497
26498 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26499 next token), return index of the first token after the standard C++11
26500 attribute tokens, or N on failure. */
26501
26502 static size_t
26503 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
26504 {
26505 while (true)
26506 {
26507 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
26508 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
26509 {
26510 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26511 if (n2 == n + 1)
26512 break;
26513 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
26514 break;
26515 n = n2 + 1;
26516 }
26517 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
26518 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
26519 {
26520 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26521 if (n2 == n + 1)
26522 break;
26523 n = n2;
26524 }
26525 else
26526 break;
26527 }
26528 return n;
26529 }
26530
26531 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26532 as the next token), return index of the first token after the attribute
26533 tokens, or N on failure. */
26534
26535 static size_t
26536 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
26537 {
26538 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
26539 return cp_parser_skip_gnu_attributes_opt (parser, n);
26540 return cp_parser_skip_std_attribute_spec_seq (parser, n);
26541 }
26542
26543 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26544 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26545 current value of the PEDANTIC flag, regardless of whether or not
26546 the `__extension__' keyword is present. The caller is responsible
26547 for restoring the value of the PEDANTIC flag. */
26548
26549 static bool
26550 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
26551 {
26552 /* Save the old value of the PEDANTIC flag. */
26553 *saved_pedantic = pedantic;
26554
26555 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
26556 {
26557 /* Consume the `__extension__' token. */
26558 cp_lexer_consume_token (parser->lexer);
26559 /* We're not being pedantic while the `__extension__' keyword is
26560 in effect. */
26561 pedantic = 0;
26562
26563 return true;
26564 }
26565
26566 return false;
26567 }
26568
26569 /* Parse a label declaration.
26570
26571 label-declaration:
26572 __label__ label-declarator-seq ;
26573
26574 label-declarator-seq:
26575 identifier , label-declarator-seq
26576 identifier */
26577
26578 static void
26579 cp_parser_label_declaration (cp_parser* parser)
26580 {
26581 /* Look for the `__label__' keyword. */
26582 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
26583
26584 while (true)
26585 {
26586 tree identifier;
26587
26588 /* Look for an identifier. */
26589 identifier = cp_parser_identifier (parser);
26590 /* If we failed, stop. */
26591 if (identifier == error_mark_node)
26592 break;
26593 /* Declare it as a label. */
26594 finish_label_decl (identifier);
26595 /* If the next token is a `;', stop. */
26596 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26597 break;
26598 /* Look for the `,' separating the label declarations. */
26599 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
26600 }
26601
26602 /* Look for the final `;'. */
26603 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26604 }
26605
26606 // -------------------------------------------------------------------------- //
26607 // Requires Clause
26608
26609 // Parse a requires clause.
26610 //
26611 // requires-clause:
26612 // 'requires' logical-or-expression
26613 //
26614 // The required logical-or-expression must be a constant expression. Note
26615 // that we don't check that the expression is constepxr here. We defer until
26616 // we analyze constraints and then, we only check atomic constraints.
26617 static tree
26618 cp_parser_requires_clause (cp_parser *parser)
26619 {
26620 // Parse the requires clause so that it is not automatically folded.
26621 ++processing_template_decl;
26622 tree expr = cp_parser_binary_expression (parser, false, false,
26623 PREC_NOT_OPERATOR, NULL);
26624 if (check_for_bare_parameter_packs (expr))
26625 expr = error_mark_node;
26626 --processing_template_decl;
26627 return expr;
26628 }
26629
26630 // Optionally parse a requires clause:
26631 static tree
26632 cp_parser_requires_clause_opt (cp_parser *parser)
26633 {
26634 cp_token *tok = cp_lexer_peek_token (parser->lexer);
26635 if (tok->keyword != RID_REQUIRES)
26636 {
26637 if (!flag_concepts && tok->type == CPP_NAME
26638 && tok->u.value == ridpointers[RID_REQUIRES])
26639 {
26640 error_at (cp_lexer_peek_token (parser->lexer)->location,
26641 "%<requires%> only available with %<-fconcepts%>");
26642 /* Parse and discard the requires-clause. */
26643 cp_lexer_consume_token (parser->lexer);
26644 cp_parser_requires_clause (parser);
26645 }
26646 return NULL_TREE;
26647 }
26648 cp_lexer_consume_token (parser->lexer);
26649 return cp_parser_requires_clause (parser);
26650 }
26651
26652
26653 /*---------------------------------------------------------------------------
26654 Requires expressions
26655 ---------------------------------------------------------------------------*/
26656
26657 /* Parse a requires expression
26658
26659 requirement-expression:
26660 'requires' requirement-parameter-list [opt] requirement-body */
26661 static tree
26662 cp_parser_requires_expression (cp_parser *parser)
26663 {
26664 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26665 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26666
26667 /* A requires-expression shall appear only within a concept
26668 definition or a requires-clause.
26669
26670 TODO: Implement this diagnostic correctly. */
26671 if (!processing_template_decl)
26672 {
26673 error_at (loc, "a requires expression cannot appear outside a template");
26674 cp_parser_skip_to_end_of_statement (parser);
26675 return error_mark_node;
26676 }
26677
26678 tree parms, reqs;
26679 {
26680 /* Local parameters are delared as variables within the scope
26681 of the expression. They are not visible past the end of
26682 the expression. Expressions within the requires-expression
26683 are unevaluated. */
26684 struct scope_sentinel
26685 {
26686 scope_sentinel ()
26687 {
26688 ++cp_unevaluated_operand;
26689 begin_scope (sk_block, NULL_TREE);
26690 }
26691
26692 ~scope_sentinel ()
26693 {
26694 pop_bindings_and_leave_scope ();
26695 --cp_unevaluated_operand;
26696 }
26697 } s;
26698
26699 /* Parse the optional parameter list. */
26700 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26701 {
26702 parms = cp_parser_requirement_parameter_list (parser);
26703 if (parms == error_mark_node)
26704 return error_mark_node;
26705 }
26706 else
26707 parms = NULL_TREE;
26708
26709 /* Parse the requirement body. */
26710 reqs = cp_parser_requirement_body (parser);
26711 if (reqs == error_mark_node)
26712 return error_mark_node;
26713 }
26714
26715 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26716 the parm chain. */
26717 grokparms (parms, &parms);
26718 return finish_requires_expr (parms, reqs);
26719 }
26720
26721 /* Parse a parameterized requirement.
26722
26723 requirement-parameter-list:
26724 '(' parameter-declaration-clause ')' */
26725 static tree
26726 cp_parser_requirement_parameter_list (cp_parser *parser)
26727 {
26728 matching_parens parens;
26729 if (!parens.require_open (parser))
26730 return error_mark_node;
26731
26732 tree parms
26733 = cp_parser_parameter_declaration_clause (parser, CP_PARSER_FLAGS_NONE);
26734
26735 if (!parens.require_close (parser))
26736 return error_mark_node;
26737
26738 return parms;
26739 }
26740
26741 /* Parse the body of a requirement.
26742
26743 requirement-body:
26744 '{' requirement-list '}' */
26745 static tree
26746 cp_parser_requirement_body (cp_parser *parser)
26747 {
26748 matching_braces braces;
26749 if (!braces.require_open (parser))
26750 return error_mark_node;
26751
26752 tree reqs = cp_parser_requirement_list (parser);
26753
26754 if (!braces.require_close (parser))
26755 return error_mark_node;
26756
26757 return reqs;
26758 }
26759
26760 /* Parse a list of requirements.
26761
26762 requirement-list:
26763 requirement
26764 requirement-list ';' requirement[opt] */
26765 static tree
26766 cp_parser_requirement_list (cp_parser *parser)
26767 {
26768 tree result = NULL_TREE;
26769 while (true)
26770 {
26771 tree req = cp_parser_requirement (parser);
26772 if (req == error_mark_node)
26773 return error_mark_node;
26774
26775 result = tree_cons (NULL_TREE, req, result);
26776
26777 /* If we see a semi-colon, consume it. */
26778 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26779 cp_lexer_consume_token (parser->lexer);
26780
26781 /* Stop processing at the end of the list. */
26782 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26783 break;
26784 }
26785
26786 /* Reverse the order of requirements so they are analyzed in
26787 declaration order. */
26788 return nreverse (result);
26789 }
26790
26791 /* Parse a syntactic requirement or type requirement.
26792
26793 requirement:
26794 simple-requirement
26795 compound-requirement
26796 type-requirement
26797 nested-requirement */
26798 static tree
26799 cp_parser_requirement (cp_parser *parser)
26800 {
26801 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26802 return cp_parser_compound_requirement (parser);
26803 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26804 return cp_parser_type_requirement (parser);
26805 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26806 return cp_parser_nested_requirement (parser);
26807 else
26808 return cp_parser_simple_requirement (parser);
26809 }
26810
26811 /* Parse a simple requirement.
26812
26813 simple-requirement:
26814 expression ';' */
26815 static tree
26816 cp_parser_simple_requirement (cp_parser *parser)
26817 {
26818 tree expr = cp_parser_expression (parser, NULL, false, false);
26819 if (!expr || expr == error_mark_node)
26820 return error_mark_node;
26821
26822 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26823 return error_mark_node;
26824
26825 return finish_simple_requirement (expr);
26826 }
26827
26828 /* Parse a type requirement
26829
26830 type-requirement
26831 nested-name-specifier [opt] required-type-name ';'
26832
26833 required-type-name:
26834 type-name
26835 'template' [opt] simple-template-id */
26836 static tree
26837 cp_parser_type_requirement (cp_parser *parser)
26838 {
26839 cp_lexer_consume_token (parser->lexer);
26840
26841 // Save the scope before parsing name specifiers.
26842 tree saved_scope = parser->scope;
26843 tree saved_object_scope = parser->object_scope;
26844 tree saved_qualifying_scope = parser->qualifying_scope;
26845 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26846 cp_parser_nested_name_specifier_opt (parser,
26847 /*typename_keyword_p=*/true,
26848 /*check_dependency_p=*/false,
26849 /*type_p=*/true,
26850 /*is_declaration=*/false);
26851
26852 tree type;
26853 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26854 {
26855 cp_lexer_consume_token (parser->lexer);
26856 type = cp_parser_template_id (parser,
26857 /*template_keyword_p=*/true,
26858 /*check_dependency=*/false,
26859 /*tag_type=*/none_type,
26860 /*is_declaration=*/false);
26861 type = make_typename_type (parser->scope, type, typename_type,
26862 /*complain=*/tf_error);
26863 }
26864 else
26865 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26866
26867 if (TREE_CODE (type) == TYPE_DECL)
26868 type = TREE_TYPE (type);
26869
26870 parser->scope = saved_scope;
26871 parser->object_scope = saved_object_scope;
26872 parser->qualifying_scope = saved_qualifying_scope;
26873
26874 if (type == error_mark_node)
26875 cp_parser_skip_to_end_of_statement (parser);
26876
26877 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26878 return error_mark_node;
26879 if (type == error_mark_node)
26880 return error_mark_node;
26881
26882 return finish_type_requirement (type);
26883 }
26884
26885 /* Parse a compound requirement
26886
26887 compound-requirement:
26888 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26889 static tree
26890 cp_parser_compound_requirement (cp_parser *parser)
26891 {
26892 /* Parse an expression enclosed in '{ }'s. */
26893 matching_braces braces;
26894 if (!braces.require_open (parser))
26895 return error_mark_node;
26896
26897 tree expr = cp_parser_expression (parser, NULL, false, false);
26898 if (!expr || expr == error_mark_node)
26899 return error_mark_node;
26900
26901 if (!braces.require_close (parser))
26902 return error_mark_node;
26903
26904 /* Parse the optional noexcept. */
26905 bool noexcept_p = false;
26906 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26907 {
26908 cp_lexer_consume_token (parser->lexer);
26909 noexcept_p = true;
26910 }
26911
26912 /* Parse the optional trailing return type. */
26913 tree type = NULL_TREE;
26914 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26915 {
26916 cp_lexer_consume_token (parser->lexer);
26917 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26918 parser->in_result_type_constraint_p = true;
26919 type = cp_parser_trailing_type_id (parser);
26920 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26921 if (type == error_mark_node)
26922 return error_mark_node;
26923 }
26924
26925 return finish_compound_requirement (expr, type, noexcept_p);
26926 }
26927
26928 /* Parse a nested requirement. This is the same as a requires clause.
26929
26930 nested-requirement:
26931 requires-clause */
26932 static tree
26933 cp_parser_nested_requirement (cp_parser *parser)
26934 {
26935 cp_lexer_consume_token (parser->lexer);
26936 tree req = cp_parser_requires_clause (parser);
26937 if (req == error_mark_node)
26938 return error_mark_node;
26939 return finish_nested_requirement (req);
26940 }
26941
26942 /* Support Functions */
26943
26944 /* Return the appropriate prefer_type argument for lookup_name_real based on
26945 tag_type and template_mem_access. */
26946
26947 static inline int
26948 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26949 {
26950 /* DR 141: When looking in the current enclosing context for a template-name
26951 after -> or ., only consider class templates. */
26952 if (template_mem_access)
26953 return 2;
26954 switch (tag_type)
26955 {
26956 case none_type: return 0; // No preference.
26957 case scope_type: return 1; // Type or namespace.
26958 default: return 2; // Type only.
26959 }
26960 }
26961
26962 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26963 NAME should have one of the representations used for an
26964 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26965 is returned. If PARSER->SCOPE is a dependent type, then a
26966 SCOPE_REF is returned.
26967
26968 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26969 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26970 was formed. Abstractly, such entities should not be passed to this
26971 function, because they do not need to be looked up, but it is
26972 simpler to check for this special case here, rather than at the
26973 call-sites.
26974
26975 In cases not explicitly covered above, this function returns a
26976 DECL, OVERLOAD, or baselink representing the result of the lookup.
26977 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26978 is returned.
26979
26980 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26981 (e.g., "struct") that was used. In that case bindings that do not
26982 refer to types are ignored.
26983
26984 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26985 ignored.
26986
26987 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26988 are ignored.
26989
26990 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26991 types.
26992
26993 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26994 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26995 NULL_TREE otherwise. */
26996
26997 static cp_expr
26998 cp_parser_lookup_name (cp_parser *parser, tree name,
26999 enum tag_types tag_type,
27000 bool is_template,
27001 bool is_namespace,
27002 bool check_dependency,
27003 tree *ambiguous_decls,
27004 location_t name_location)
27005 {
27006 tree decl;
27007 tree object_type = parser->context->object_type;
27008
27009 /* Assume that the lookup will be unambiguous. */
27010 if (ambiguous_decls)
27011 *ambiguous_decls = NULL_TREE;
27012
27013 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
27014 no longer valid. Note that if we are parsing tentatively, and
27015 the parse fails, OBJECT_TYPE will be automatically restored. */
27016 parser->context->object_type = NULL_TREE;
27017
27018 if (name == error_mark_node)
27019 return error_mark_node;
27020
27021 /* A template-id has already been resolved; there is no lookup to
27022 do. */
27023 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
27024 return name;
27025 if (BASELINK_P (name))
27026 {
27027 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
27028 == TEMPLATE_ID_EXPR);
27029 return name;
27030 }
27031
27032 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
27033 it should already have been checked to make sure that the name
27034 used matches the type being destroyed. */
27035 if (TREE_CODE (name) == BIT_NOT_EXPR)
27036 {
27037 tree type;
27038
27039 /* Figure out to which type this destructor applies. */
27040 if (parser->scope)
27041 type = parser->scope;
27042 else if (object_type)
27043 type = object_type;
27044 else
27045 type = current_class_type;
27046 /* If that's not a class type, there is no destructor. */
27047 if (!type || !CLASS_TYPE_P (type))
27048 return error_mark_node;
27049
27050 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
27051 lazily_declare_fn (sfk_destructor, type);
27052
27053 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
27054 return dtor;
27055
27056 return error_mark_node;
27057 }
27058
27059 /* By this point, the NAME should be an ordinary identifier. If
27060 the id-expression was a qualified name, the qualifying scope is
27061 stored in PARSER->SCOPE at this point. */
27062 gcc_assert (identifier_p (name));
27063
27064 /* Perform the lookup. */
27065 if (parser->scope)
27066 {
27067 bool dependent_p;
27068
27069 if (parser->scope == error_mark_node)
27070 return error_mark_node;
27071
27072 /* If the SCOPE is dependent, the lookup must be deferred until
27073 the template is instantiated -- unless we are explicitly
27074 looking up names in uninstantiated templates. Even then, we
27075 cannot look up the name if the scope is not a class type; it
27076 might, for example, be a template type parameter. */
27077 dependent_p = (TYPE_P (parser->scope)
27078 && dependent_scope_p (parser->scope));
27079 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
27080 && dependent_p)
27081 /* Defer lookup. */
27082 decl = error_mark_node;
27083 else
27084 {
27085 tree pushed_scope = NULL_TREE;
27086
27087 /* If PARSER->SCOPE is a dependent type, then it must be a
27088 class type, and we must not be checking dependencies;
27089 otherwise, we would have processed this lookup above. So
27090 that PARSER->SCOPE is not considered a dependent base by
27091 lookup_member, we must enter the scope here. */
27092 if (dependent_p)
27093 pushed_scope = push_scope (parser->scope);
27094
27095 /* If the PARSER->SCOPE is a template specialization, it
27096 may be instantiated during name lookup. In that case,
27097 errors may be issued. Even if we rollback the current
27098 tentative parse, those errors are valid. */
27099 decl = lookup_qualified_name (parser->scope, name,
27100 prefer_type_arg (tag_type),
27101 /*complain=*/true);
27102
27103 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
27104 lookup result and the nested-name-specifier nominates a class C:
27105 * if the name specified after the nested-name-specifier, when
27106 looked up in C, is the injected-class-name of C (Clause 9), or
27107 * if the name specified after the nested-name-specifier is the
27108 same as the identifier or the simple-template-id's template-
27109 name in the last component of the nested-name-specifier,
27110 the name is instead considered to name the constructor of
27111 class C. [ Note: for example, the constructor is not an
27112 acceptable lookup result in an elaborated-type-specifier so
27113 the constructor would not be used in place of the
27114 injected-class-name. --end note ] Such a constructor name
27115 shall be used only in the declarator-id of a declaration that
27116 names a constructor or in a using-declaration. */
27117 if (tag_type == none_type
27118 && DECL_SELF_REFERENCE_P (decl)
27119 && same_type_p (DECL_CONTEXT (decl), parser->scope))
27120 decl = lookup_qualified_name (parser->scope, ctor_identifier,
27121 prefer_type_arg (tag_type),
27122 /*complain=*/true);
27123
27124 /* If we have a single function from a using decl, pull it out. */
27125 if (TREE_CODE (decl) == OVERLOAD
27126 && !really_overloaded_fn (decl))
27127 decl = OVL_FUNCTION (decl);
27128
27129 if (pushed_scope)
27130 pop_scope (pushed_scope);
27131 }
27132
27133 /* If the scope is a dependent type and either we deferred lookup or
27134 we did lookup but didn't find the name, rememeber the name. */
27135 if (decl == error_mark_node && TYPE_P (parser->scope)
27136 && dependent_type_p (parser->scope))
27137 {
27138 if (tag_type)
27139 {
27140 tree type;
27141
27142 /* The resolution to Core Issue 180 says that `struct
27143 A::B' should be considered a type-name, even if `A'
27144 is dependent. */
27145 type = make_typename_type (parser->scope, name, tag_type,
27146 /*complain=*/tf_error);
27147 if (type != error_mark_node)
27148 decl = TYPE_NAME (type);
27149 }
27150 else if (is_template
27151 && (cp_parser_next_token_ends_template_argument_p (parser)
27152 || cp_lexer_next_token_is (parser->lexer,
27153 CPP_CLOSE_PAREN)))
27154 decl = make_unbound_class_template (parser->scope,
27155 name, NULL_TREE,
27156 /*complain=*/tf_error);
27157 else
27158 decl = build_qualified_name (/*type=*/NULL_TREE,
27159 parser->scope, name,
27160 is_template);
27161 }
27162 parser->qualifying_scope = parser->scope;
27163 parser->object_scope = NULL_TREE;
27164 }
27165 else if (object_type)
27166 {
27167 /* Look up the name in the scope of the OBJECT_TYPE, unless the
27168 OBJECT_TYPE is not a class. */
27169 if (CLASS_TYPE_P (object_type))
27170 /* If the OBJECT_TYPE is a template specialization, it may
27171 be instantiated during name lookup. In that case, errors
27172 may be issued. Even if we rollback the current tentative
27173 parse, those errors are valid. */
27174 decl = lookup_member (object_type,
27175 name,
27176 /*protect=*/0,
27177 prefer_type_arg (tag_type),
27178 tf_warning_or_error);
27179 else
27180 decl = NULL_TREE;
27181
27182 if (!decl)
27183 /* Look it up in the enclosing context. DR 141: When looking for a
27184 template-name after -> or ., only consider class templates. */
27185 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
27186 /*nonclass=*/0,
27187 /*block_p=*/true, is_namespace, 0);
27188 if (object_type == unknown_type_node)
27189 /* The object is type-dependent, so we can't look anything up; we used
27190 this to get the DR 141 behavior. */
27191 object_type = NULL_TREE;
27192 parser->object_scope = object_type;
27193 parser->qualifying_scope = NULL_TREE;
27194 }
27195 else
27196 {
27197 decl = lookup_name_real (name, prefer_type_arg (tag_type),
27198 /*nonclass=*/0,
27199 /*block_p=*/true, is_namespace, 0);
27200 parser->qualifying_scope = NULL_TREE;
27201 parser->object_scope = NULL_TREE;
27202 }
27203
27204 /* If the lookup failed, let our caller know. */
27205 if (!decl || decl == error_mark_node)
27206 return error_mark_node;
27207
27208 /* Pull out the template from an injected-class-name (or multiple). */
27209 if (is_template)
27210 decl = maybe_get_template_decl_from_type_decl (decl);
27211
27212 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
27213 if (TREE_CODE (decl) == TREE_LIST)
27214 {
27215 if (ambiguous_decls)
27216 *ambiguous_decls = decl;
27217 /* The error message we have to print is too complicated for
27218 cp_parser_error, so we incorporate its actions directly. */
27219 if (!cp_parser_simulate_error (parser))
27220 {
27221 error_at (name_location, "reference to %qD is ambiguous",
27222 name);
27223 print_candidates (decl);
27224 }
27225 return error_mark_node;
27226 }
27227
27228 gcc_assert (DECL_P (decl)
27229 || TREE_CODE (decl) == OVERLOAD
27230 || TREE_CODE (decl) == SCOPE_REF
27231 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
27232 || BASELINK_P (decl));
27233
27234 /* If we have resolved the name of a member declaration, check to
27235 see if the declaration is accessible. When the name resolves to
27236 set of overloaded functions, accessibility is checked when
27237 overload resolution is done.
27238
27239 During an explicit instantiation, access is not checked at all,
27240 as per [temp.explicit]. */
27241 if (DECL_P (decl))
27242 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
27243
27244 maybe_record_typedef_use (decl);
27245
27246 return cp_expr (decl, name_location);
27247 }
27248
27249 /* Like cp_parser_lookup_name, but for use in the typical case where
27250 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
27251 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
27252
27253 static tree
27254 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
27255 {
27256 return cp_parser_lookup_name (parser, name,
27257 none_type,
27258 /*is_template=*/false,
27259 /*is_namespace=*/false,
27260 /*check_dependency=*/true,
27261 /*ambiguous_decls=*/NULL,
27262 location);
27263 }
27264
27265 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
27266 the current context, return the TYPE_DECL. If TAG_NAME_P is
27267 true, the DECL indicates the class being defined in a class-head,
27268 or declared in an elaborated-type-specifier.
27269
27270 Otherwise, return DECL. */
27271
27272 static tree
27273 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
27274 {
27275 /* If the TEMPLATE_DECL is being declared as part of a class-head,
27276 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
27277
27278 struct A {
27279 template <typename T> struct B;
27280 };
27281
27282 template <typename T> struct A::B {};
27283
27284 Similarly, in an elaborated-type-specifier:
27285
27286 namespace N { struct X{}; }
27287
27288 struct A {
27289 template <typename T> friend struct N::X;
27290 };
27291
27292 However, if the DECL refers to a class type, and we are in
27293 the scope of the class, then the name lookup automatically
27294 finds the TYPE_DECL created by build_self_reference rather
27295 than a TEMPLATE_DECL. For example, in:
27296
27297 template <class T> struct S {
27298 S s;
27299 };
27300
27301 there is no need to handle such case. */
27302
27303 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
27304 return DECL_TEMPLATE_RESULT (decl);
27305
27306 return decl;
27307 }
27308
27309 /* If too many, or too few, template-parameter lists apply to the
27310 declarator, issue an error message. Returns TRUE if all went well,
27311 and FALSE otherwise. */
27312
27313 static bool
27314 cp_parser_check_declarator_template_parameters (cp_parser* parser,
27315 cp_declarator *declarator,
27316 location_t declarator_location)
27317 {
27318 switch (declarator->kind)
27319 {
27320 case cdk_id:
27321 {
27322 unsigned num_templates = 0;
27323 tree scope = declarator->u.id.qualifying_scope;
27324 bool template_id_p = false;
27325
27326 if (scope)
27327 num_templates = num_template_headers_for_class (scope);
27328 else if (TREE_CODE (declarator->u.id.unqualified_name)
27329 == TEMPLATE_ID_EXPR)
27330 {
27331 /* If the DECLARATOR has the form `X<y>' then it uses one
27332 additional level of template parameters. */
27333 ++num_templates;
27334 template_id_p = true;
27335 }
27336
27337 return cp_parser_check_template_parameters
27338 (parser, num_templates, template_id_p, declarator_location,
27339 declarator);
27340 }
27341
27342 case cdk_function:
27343 case cdk_array:
27344 case cdk_pointer:
27345 case cdk_reference:
27346 case cdk_ptrmem:
27347 return (cp_parser_check_declarator_template_parameters
27348 (parser, declarator->declarator, declarator_location));
27349
27350 case cdk_decomp:
27351 case cdk_error:
27352 return true;
27353
27354 default:
27355 gcc_unreachable ();
27356 }
27357 return false;
27358 }
27359
27360 /* NUM_TEMPLATES were used in the current declaration. If that is
27361 invalid, return FALSE and issue an error messages. Otherwise,
27362 return TRUE. If DECLARATOR is non-NULL, then we are checking a
27363 declarator and we can print more accurate diagnostics. */
27364
27365 static bool
27366 cp_parser_check_template_parameters (cp_parser* parser,
27367 unsigned num_templates,
27368 bool template_id_p,
27369 location_t location,
27370 cp_declarator *declarator)
27371 {
27372 /* If there are the same number of template classes and parameter
27373 lists, that's OK. */
27374 if (parser->num_template_parameter_lists == num_templates)
27375 return true;
27376 /* If there are more, but only one more, and the name ends in an identifier,
27377 then we are declaring a primary template. That's OK too. */
27378 if (!template_id_p
27379 && parser->num_template_parameter_lists == num_templates + 1)
27380 return true;
27381 /* If there are more template classes than parameter lists, we have
27382 something like:
27383
27384 template <class T> void S<T>::R<T>::f (); */
27385 if (parser->num_template_parameter_lists < num_templates)
27386 {
27387 if (declarator && !current_function_decl)
27388 error_at (location, "specializing member %<%T::%E%> "
27389 "requires %<template<>%> syntax",
27390 declarator->u.id.qualifying_scope,
27391 declarator->u.id.unqualified_name);
27392 else if (declarator)
27393 error_at (location, "invalid declaration of %<%T::%E%>",
27394 declarator->u.id.qualifying_scope,
27395 declarator->u.id.unqualified_name);
27396 else
27397 error_at (location, "too few template-parameter-lists");
27398 return false;
27399 }
27400 /* Otherwise, there are too many template parameter lists. We have
27401 something like:
27402
27403 template <class T> template <class U> void S::f(); */
27404 error_at (location, "too many template-parameter-lists");
27405 return false;
27406 }
27407
27408 /* Parse an optional `::' token indicating that the following name is
27409 from the global namespace. If so, PARSER->SCOPE is set to the
27410 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
27411 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
27412 Returns the new value of PARSER->SCOPE, if the `::' token is
27413 present, and NULL_TREE otherwise. */
27414
27415 static tree
27416 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
27417 {
27418 cp_token *token;
27419
27420 /* Peek at the next token. */
27421 token = cp_lexer_peek_token (parser->lexer);
27422 /* If we're looking at a `::' token then we're starting from the
27423 global namespace, not our current location. */
27424 if (token->type == CPP_SCOPE)
27425 {
27426 /* Consume the `::' token. */
27427 cp_lexer_consume_token (parser->lexer);
27428 /* Set the SCOPE so that we know where to start the lookup. */
27429 parser->scope = global_namespace;
27430 parser->qualifying_scope = global_namespace;
27431 parser->object_scope = NULL_TREE;
27432
27433 return parser->scope;
27434 }
27435 else if (!current_scope_valid_p)
27436 {
27437 parser->scope = NULL_TREE;
27438 parser->qualifying_scope = NULL_TREE;
27439 parser->object_scope = NULL_TREE;
27440 }
27441
27442 return NULL_TREE;
27443 }
27444
27445 /* Returns TRUE if the upcoming token sequence is the start of a
27446 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
27447 declarator is preceded by the `friend' specifier. The parser flags FLAGS
27448 is used to control type-specifier parsing. */
27449
27450 static bool
27451 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
27452 bool friend_p)
27453 {
27454 bool constructor_p;
27455 bool outside_class_specifier_p;
27456 tree nested_name_specifier;
27457 cp_token *next_token;
27458
27459 /* The common case is that this is not a constructor declarator, so
27460 try to avoid doing lots of work if at all possible. It's not
27461 valid declare a constructor at function scope. */
27462 if (parser->in_function_body)
27463 return false;
27464 /* And only certain tokens can begin a constructor declarator. */
27465 next_token = cp_lexer_peek_token (parser->lexer);
27466 if (next_token->type != CPP_NAME
27467 && next_token->type != CPP_SCOPE
27468 && next_token->type != CPP_NESTED_NAME_SPECIFIER
27469 && next_token->type != CPP_TEMPLATE_ID)
27470 return false;
27471
27472 /* Parse tentatively; we are going to roll back all of the tokens
27473 consumed here. */
27474 cp_parser_parse_tentatively (parser);
27475 /* Assume that we are looking at a constructor declarator. */
27476 constructor_p = true;
27477
27478 /* Look for the optional `::' operator. */
27479 cp_parser_global_scope_opt (parser,
27480 /*current_scope_valid_p=*/false);
27481 /* Look for the nested-name-specifier. */
27482 nested_name_specifier
27483 = (cp_parser_nested_name_specifier_opt (parser,
27484 /*typename_keyword_p=*/false,
27485 /*check_dependency_p=*/false,
27486 /*type_p=*/false,
27487 /*is_declaration=*/false));
27488
27489 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
27490 if (nested_name_specifier
27491 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
27492 {
27493 tree s = resolve_typename_type (nested_name_specifier,
27494 /*only_current_p=*/false);
27495 if (TREE_CODE (s) != TYPENAME_TYPE)
27496 nested_name_specifier = s;
27497 }
27498
27499 outside_class_specifier_p = (!at_class_scope_p ()
27500 || !TYPE_BEING_DEFINED (current_class_type)
27501 || friend_p);
27502
27503 /* Outside of a class-specifier, there must be a
27504 nested-name-specifier. Except in C++17 mode, where we
27505 might be declaring a guiding declaration. */
27506 if (!nested_name_specifier && outside_class_specifier_p
27507 && cxx_dialect < cxx17)
27508 constructor_p = false;
27509 else if (nested_name_specifier == error_mark_node)
27510 constructor_p = false;
27511
27512 /* If we have a class scope, this is easy; DR 147 says that S::S always
27513 names the constructor, and no other qualified name could. */
27514 if (constructor_p && nested_name_specifier
27515 && CLASS_TYPE_P (nested_name_specifier))
27516 {
27517 tree id = cp_parser_unqualified_id (parser,
27518 /*template_keyword_p=*/false,
27519 /*check_dependency_p=*/false,
27520 /*declarator_p=*/true,
27521 /*optional_p=*/false);
27522 if (is_overloaded_fn (id))
27523 id = DECL_NAME (get_first_fn (id));
27524 if (!constructor_name_p (id, nested_name_specifier))
27525 constructor_p = false;
27526 }
27527 /* If we still think that this might be a constructor-declarator,
27528 look for a class-name. */
27529 else if (constructor_p)
27530 {
27531 /* If we have:
27532
27533 template <typename T> struct S {
27534 S();
27535 };
27536
27537 we must recognize that the nested `S' names a class. */
27538 if (cxx_dialect >= cxx17)
27539 cp_parser_parse_tentatively (parser);
27540
27541 tree type_decl;
27542 type_decl = cp_parser_class_name (parser,
27543 /*typename_keyword_p=*/false,
27544 /*template_keyword_p=*/false,
27545 none_type,
27546 /*check_dependency_p=*/false,
27547 /*class_head_p=*/false,
27548 /*is_declaration=*/false);
27549
27550 if (cxx_dialect >= cxx17
27551 && !cp_parser_parse_definitely (parser))
27552 {
27553 type_decl = NULL_TREE;
27554 tree tmpl = cp_parser_template_name (parser,
27555 /*template_keyword*/false,
27556 /*check_dependency_p*/false,
27557 /*is_declaration*/false,
27558 none_type,
27559 /*is_identifier*/NULL);
27560 if (DECL_CLASS_TEMPLATE_P (tmpl)
27561 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27562 /* It's a deduction guide, return true. */;
27563 else
27564 cp_parser_simulate_error (parser);
27565 }
27566
27567 /* If there was no class-name, then this is not a constructor.
27568 Otherwise, if we are in a class-specifier and we aren't
27569 handling a friend declaration, check that its type matches
27570 current_class_type (c++/38313). Note: error_mark_node
27571 is left alone for error recovery purposes. */
27572 constructor_p = (!cp_parser_error_occurred (parser)
27573 && (outside_class_specifier_p
27574 || type_decl == NULL_TREE
27575 || type_decl == error_mark_node
27576 || same_type_p (current_class_type,
27577 TREE_TYPE (type_decl))));
27578
27579 /* If we're still considering a constructor, we have to see a `(',
27580 to begin the parameter-declaration-clause, followed by either a
27581 `)', an `...', or a decl-specifier. We need to check for a
27582 type-specifier to avoid being fooled into thinking that:
27583
27584 S (f) (int);
27585
27586 is a constructor. (It is actually a function named `f' that
27587 takes one parameter (of type `int') and returns a value of type
27588 `S'. */
27589 if (constructor_p
27590 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27591 constructor_p = false;
27592
27593 if (constructor_p
27594 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
27595 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
27596 /* A parameter declaration begins with a decl-specifier,
27597 which is either the "attribute" keyword, a storage class
27598 specifier, or (usually) a type-specifier. */
27599 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
27600 {
27601 tree type;
27602 tree pushed_scope = NULL_TREE;
27603 unsigned saved_num_template_parameter_lists;
27604
27605 /* Names appearing in the type-specifier should be looked up
27606 in the scope of the class. */
27607 if (current_class_type)
27608 type = NULL_TREE;
27609 else if (type_decl)
27610 {
27611 type = TREE_TYPE (type_decl);
27612 if (TREE_CODE (type) == TYPENAME_TYPE)
27613 {
27614 type = resolve_typename_type (type,
27615 /*only_current_p=*/false);
27616 if (TREE_CODE (type) == TYPENAME_TYPE)
27617 {
27618 cp_parser_abort_tentative_parse (parser);
27619 return false;
27620 }
27621 }
27622 pushed_scope = push_scope (type);
27623 }
27624
27625 /* Inside the constructor parameter list, surrounding
27626 template-parameter-lists do not apply. */
27627 saved_num_template_parameter_lists
27628 = parser->num_template_parameter_lists;
27629 parser->num_template_parameter_lists = 0;
27630
27631 /* Look for the type-specifier. It's not optional, but its typename
27632 might be. */
27633 cp_parser_type_specifier (parser,
27634 (flags & ~CP_PARSER_FLAGS_OPTIONAL),
27635 /*decl_specs=*/NULL,
27636 /*is_declarator=*/true,
27637 /*declares_class_or_enum=*/NULL,
27638 /*is_cv_qualifier=*/NULL);
27639
27640 parser->num_template_parameter_lists
27641 = saved_num_template_parameter_lists;
27642
27643 /* Leave the scope of the class. */
27644 if (pushed_scope)
27645 pop_scope (pushed_scope);
27646
27647 constructor_p = !cp_parser_error_occurred (parser);
27648 }
27649 }
27650
27651 /* We did not really want to consume any tokens. */
27652 cp_parser_abort_tentative_parse (parser);
27653
27654 return constructor_p;
27655 }
27656
27657 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27658 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27659 they must be performed once we are in the scope of the function.
27660
27661 Returns the function defined. */
27662
27663 static tree
27664 cp_parser_function_definition_from_specifiers_and_declarator
27665 (cp_parser* parser,
27666 cp_decl_specifier_seq *decl_specifiers,
27667 tree attributes,
27668 const cp_declarator *declarator)
27669 {
27670 tree fn;
27671 bool success_p;
27672
27673 /* Begin the function-definition. */
27674 success_p = start_function (decl_specifiers, declarator, attributes);
27675
27676 /* The things we're about to see are not directly qualified by any
27677 template headers we've seen thus far. */
27678 reset_specialization ();
27679
27680 /* If there were names looked up in the decl-specifier-seq that we
27681 did not check, check them now. We must wait until we are in the
27682 scope of the function to perform the checks, since the function
27683 might be a friend. */
27684 perform_deferred_access_checks (tf_warning_or_error);
27685
27686 if (success_p)
27687 {
27688 cp_finalize_omp_declare_simd (parser, current_function_decl);
27689 parser->omp_declare_simd = NULL;
27690 cp_finalize_oacc_routine (parser, current_function_decl, true);
27691 parser->oacc_routine = NULL;
27692 }
27693
27694 if (!success_p)
27695 {
27696 /* Skip the entire function. */
27697 cp_parser_skip_to_end_of_block_or_statement (parser);
27698 fn = error_mark_node;
27699 }
27700 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27701 {
27702 /* Seen already, skip it. An error message has already been output. */
27703 cp_parser_skip_to_end_of_block_or_statement (parser);
27704 fn = current_function_decl;
27705 current_function_decl = NULL_TREE;
27706 /* If this is a function from a class, pop the nested class. */
27707 if (current_class_name)
27708 pop_nested_class ();
27709 }
27710 else
27711 {
27712 timevar_id_t tv;
27713 if (DECL_DECLARED_INLINE_P (current_function_decl))
27714 tv = TV_PARSE_INLINE;
27715 else
27716 tv = TV_PARSE_FUNC;
27717 timevar_push (tv);
27718 fn = cp_parser_function_definition_after_declarator (parser,
27719 /*inline_p=*/false);
27720 timevar_pop (tv);
27721 }
27722
27723 return fn;
27724 }
27725
27726 /* Parse the part of a function-definition that follows the
27727 declarator. INLINE_P is TRUE iff this function is an inline
27728 function defined within a class-specifier.
27729
27730 Returns the function defined. */
27731
27732 static tree
27733 cp_parser_function_definition_after_declarator (cp_parser* parser,
27734 bool inline_p)
27735 {
27736 tree fn;
27737 bool saved_in_unbraced_linkage_specification_p;
27738 bool saved_in_function_body;
27739 unsigned saved_num_template_parameter_lists;
27740 cp_token *token;
27741 bool fully_implicit_function_template_p
27742 = parser->fully_implicit_function_template_p;
27743 parser->fully_implicit_function_template_p = false;
27744 tree implicit_template_parms
27745 = parser->implicit_template_parms;
27746 parser->implicit_template_parms = 0;
27747 cp_binding_level* implicit_template_scope
27748 = parser->implicit_template_scope;
27749 parser->implicit_template_scope = 0;
27750
27751 saved_in_function_body = parser->in_function_body;
27752 parser->in_function_body = true;
27753 /* If the next token is `return', then the code may be trying to
27754 make use of the "named return value" extension that G++ used to
27755 support. */
27756 token = cp_lexer_peek_token (parser->lexer);
27757 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27758 {
27759 /* Consume the `return' keyword. */
27760 cp_lexer_consume_token (parser->lexer);
27761 /* Look for the identifier that indicates what value is to be
27762 returned. */
27763 cp_parser_identifier (parser);
27764 /* Issue an error message. */
27765 error_at (token->location,
27766 "named return values are no longer supported");
27767 /* Skip tokens until we reach the start of the function body. */
27768 while (true)
27769 {
27770 cp_token *token = cp_lexer_peek_token (parser->lexer);
27771 if (token->type == CPP_OPEN_BRACE
27772 || token->type == CPP_EOF
27773 || token->type == CPP_PRAGMA_EOL)
27774 break;
27775 cp_lexer_consume_token (parser->lexer);
27776 }
27777 }
27778 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27779 anything declared inside `f'. */
27780 saved_in_unbraced_linkage_specification_p
27781 = parser->in_unbraced_linkage_specification_p;
27782 parser->in_unbraced_linkage_specification_p = false;
27783 /* Inside the function, surrounding template-parameter-lists do not
27784 apply. */
27785 saved_num_template_parameter_lists
27786 = parser->num_template_parameter_lists;
27787 parser->num_template_parameter_lists = 0;
27788
27789 /* If the next token is `try', `__transaction_atomic', or
27790 `__transaction_relaxed`, then we are looking at either function-try-block
27791 or function-transaction-block. Note that all of these include the
27792 function-body. */
27793 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
27794 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
27795 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27796 RID_TRANSACTION_RELAXED))
27797 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
27798 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27799 cp_parser_function_try_block (parser);
27800 else
27801 cp_parser_ctor_initializer_opt_and_function_body
27802 (parser, /*in_function_try_block=*/false);
27803
27804 /* Finish the function. */
27805 fn = finish_function (inline_p);
27806 /* Generate code for it, if necessary. */
27807 expand_or_defer_fn (fn);
27808 /* Restore the saved values. */
27809 parser->in_unbraced_linkage_specification_p
27810 = saved_in_unbraced_linkage_specification_p;
27811 parser->num_template_parameter_lists
27812 = saved_num_template_parameter_lists;
27813 parser->in_function_body = saved_in_function_body;
27814
27815 parser->fully_implicit_function_template_p
27816 = fully_implicit_function_template_p;
27817 parser->implicit_template_parms
27818 = implicit_template_parms;
27819 parser->implicit_template_scope
27820 = implicit_template_scope;
27821
27822 if (parser->fully_implicit_function_template_p)
27823 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27824
27825 return fn;
27826 }
27827
27828 /* Parse a template-declaration body (following argument list). */
27829
27830 static void
27831 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27832 tree parameter_list,
27833 bool member_p)
27834 {
27835 tree decl = NULL_TREE;
27836 bool friend_p = false;
27837
27838 /* We just processed one more parameter list. */
27839 ++parser->num_template_parameter_lists;
27840
27841 /* Get the deferred access checks from the parameter list. These
27842 will be checked once we know what is being declared, as for a
27843 member template the checks must be performed in the scope of the
27844 class containing the member. */
27845 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27846
27847 /* Tentatively parse for a new template parameter list, which can either be
27848 the template keyword or a template introduction. */
27849 if (cp_parser_template_declaration_after_export (parser, member_p))
27850 /* OK */;
27851 else if (cxx_dialect >= cxx11
27852 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27853 decl = cp_parser_alias_declaration (parser);
27854 else
27855 {
27856 /* There are no access checks when parsing a template, as we do not
27857 know if a specialization will be a friend. */
27858 push_deferring_access_checks (dk_no_check);
27859 cp_token *token = cp_lexer_peek_token (parser->lexer);
27860 decl = cp_parser_single_declaration (parser,
27861 checks,
27862 member_p,
27863 /*explicit_specialization_p=*/false,
27864 &friend_p);
27865 pop_deferring_access_checks ();
27866
27867 /* If this is a member template declaration, let the front
27868 end know. */
27869 if (member_p && !friend_p && decl)
27870 {
27871 if (TREE_CODE (decl) == TYPE_DECL)
27872 cp_parser_check_access_in_redeclaration (decl, token->location);
27873
27874 decl = finish_member_template_decl (decl);
27875 }
27876 else if (friend_p && decl
27877 && DECL_DECLARES_TYPE_P (decl))
27878 make_friend_class (current_class_type, TREE_TYPE (decl),
27879 /*complain=*/true);
27880 }
27881 /* We are done with the current parameter list. */
27882 --parser->num_template_parameter_lists;
27883
27884 pop_deferring_access_checks ();
27885
27886 /* Finish up. */
27887 finish_template_decl (parameter_list);
27888
27889 /* Check the template arguments for a literal operator template. */
27890 if (decl
27891 && DECL_DECLARES_FUNCTION_P (decl)
27892 && UDLIT_OPER_P (DECL_NAME (decl)))
27893 {
27894 bool ok = true;
27895 if (parameter_list == NULL_TREE)
27896 ok = false;
27897 else
27898 {
27899 int num_parms = TREE_VEC_LENGTH (parameter_list);
27900 if (num_parms == 1)
27901 {
27902 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27903 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27904 if (CLASS_TYPE_P (TREE_TYPE (parm)))
27905 /* OK, C++20 string literal operator template. We don't need
27906 to warn in lower dialects here because we will have already
27907 warned about the template parameter. */;
27908 else if (TREE_TYPE (parm) != char_type_node
27909 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27910 ok = false;
27911 }
27912 else if (num_parms == 2 && cxx_dialect >= cxx14)
27913 {
27914 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27915 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27916 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27917 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27918 if (parm == error_mark_node
27919 || TREE_TYPE (parm) != TREE_TYPE (type)
27920 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27921 ok = false;
27922 else
27923 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27924 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
27925 "ISO C++ did not adopt string literal operator templa"
27926 "tes taking an argument pack of characters");
27927 }
27928 else
27929 ok = false;
27930 }
27931 if (!ok)
27932 {
27933 if (cxx_dialect > cxx17)
27934 error ("literal operator template %qD has invalid parameter list;"
27935 " Expected non-type template parameter pack <char...> "
27936 "or single non-type parameter of class type",
27937 decl);
27938 else
27939 error ("literal operator template %qD has invalid parameter list."
27940 " Expected non-type template parameter pack <char...>",
27941 decl);
27942 }
27943 }
27944
27945 /* Register member declarations. */
27946 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27947 finish_member_declaration (decl);
27948 /* If DECL is a function template, we must return to parse it later.
27949 (Even though there is no definition, there might be default
27950 arguments that need handling.) */
27951 if (member_p && decl
27952 && DECL_DECLARES_FUNCTION_P (decl))
27953 vec_safe_push (unparsed_funs_with_definitions, decl);
27954 }
27955
27956 /* Parse a template introduction header for a template-declaration. Returns
27957 false if tentative parse fails. */
27958
27959 static bool
27960 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27961 {
27962 cp_parser_parse_tentatively (parser);
27963
27964 tree saved_scope = parser->scope;
27965 tree saved_object_scope = parser->object_scope;
27966 tree saved_qualifying_scope = parser->qualifying_scope;
27967
27968 /* Look for the optional `::' operator. */
27969 cp_parser_global_scope_opt (parser,
27970 /*current_scope_valid_p=*/false);
27971 /* Look for the nested-name-specifier. */
27972 cp_parser_nested_name_specifier_opt (parser,
27973 /*typename_keyword_p=*/false,
27974 /*check_dependency_p=*/true,
27975 /*type_p=*/false,
27976 /*is_declaration=*/false);
27977
27978 cp_token *token = cp_lexer_peek_token (parser->lexer);
27979 tree concept_name = cp_parser_identifier (parser);
27980
27981 /* Look up the concept for which we will be matching
27982 template parameters. */
27983 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27984 token->location);
27985 parser->scope = saved_scope;
27986 parser->object_scope = saved_object_scope;
27987 parser->qualifying_scope = saved_qualifying_scope;
27988
27989 if (concept_name == error_mark_node)
27990 cp_parser_simulate_error (parser);
27991
27992 /* Look for opening brace for introduction. */
27993 matching_braces braces;
27994 braces.require_open (parser);
27995
27996 if (!cp_parser_parse_definitely (parser))
27997 return false;
27998
27999 push_deferring_access_checks (dk_deferred);
28000
28001 /* Build vector of placeholder parameters and grab
28002 matching identifiers. */
28003 tree introduction_list = cp_parser_introduction_list (parser);
28004
28005 /* Look for closing brace for introduction. */
28006 if (!braces.require_close (parser))
28007 return true;
28008
28009 /* The introduction-list shall not be empty. */
28010 int nargs = TREE_VEC_LENGTH (introduction_list);
28011 if (nargs == 0)
28012 {
28013 /* In cp_parser_introduction_list we have already issued an error. */
28014 return true;
28015 }
28016
28017 if (tmpl_decl == error_mark_node)
28018 {
28019 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
28020 token->location);
28021 return true;
28022 }
28023
28024 /* Build and associate the constraint. */
28025 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
28026 if (parms && parms != error_mark_node)
28027 {
28028 cp_parser_template_declaration_after_parameters (parser, parms,
28029 member_p);
28030 return true;
28031 }
28032
28033 error_at (token->location, "no matching concept for template-introduction");
28034 return true;
28035 }
28036
28037 /* Parse a normal template-declaration following the template keyword. */
28038
28039 static void
28040 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
28041 {
28042 tree parameter_list;
28043 bool need_lang_pop;
28044 location_t location = input_location;
28045
28046 /* Look for the `<' token. */
28047 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
28048 return;
28049 if (at_class_scope_p () && current_function_decl)
28050 {
28051 /* 14.5.2.2 [temp.mem]
28052
28053 A local class shall not have member templates. */
28054 error_at (location,
28055 "invalid declaration of member template in local class");
28056 cp_parser_skip_to_end_of_block_or_statement (parser);
28057 return;
28058 }
28059 /* [temp]
28060
28061 A template ... shall not have C linkage. */
28062 if (current_lang_name == lang_name_c)
28063 {
28064 error_at (location, "template with C linkage");
28065 maybe_show_extern_c_location ();
28066 /* Give it C++ linkage to avoid confusing other parts of the
28067 front end. */
28068 push_lang_context (lang_name_cplusplus);
28069 need_lang_pop = true;
28070 }
28071 else
28072 need_lang_pop = false;
28073
28074 /* We cannot perform access checks on the template parameter
28075 declarations until we know what is being declared, just as we
28076 cannot check the decl-specifier list. */
28077 push_deferring_access_checks (dk_deferred);
28078
28079 /* If the next token is `>', then we have an invalid
28080 specialization. Rather than complain about an invalid template
28081 parameter, issue an error message here. */
28082 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
28083 {
28084 cp_parser_error (parser, "invalid explicit specialization");
28085 begin_specialization ();
28086 parameter_list = NULL_TREE;
28087 }
28088 else
28089 {
28090 /* Parse the template parameters. */
28091 parameter_list = cp_parser_template_parameter_list (parser);
28092 }
28093
28094 /* Look for the `>'. */
28095 cp_parser_skip_to_end_of_template_parameter_list (parser);
28096
28097 /* Manage template requirements */
28098 if (flag_concepts)
28099 {
28100 tree reqs = get_shorthand_constraints (current_template_parms);
28101 if (tree r = cp_parser_requires_clause_opt (parser))
28102 reqs = conjoin_constraints (reqs, normalize_expression (r));
28103 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
28104 }
28105
28106 cp_parser_template_declaration_after_parameters (parser, parameter_list,
28107 member_p);
28108
28109 /* For the erroneous case of a template with C linkage, we pushed an
28110 implicit C++ linkage scope; exit that scope now. */
28111 if (need_lang_pop)
28112 pop_lang_context ();
28113 }
28114
28115 /* Parse a template-declaration, assuming that the `export' (and
28116 `extern') keywords, if present, has already been scanned. MEMBER_P
28117 is as for cp_parser_template_declaration. */
28118
28119 static bool
28120 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
28121 {
28122 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28123 {
28124 cp_lexer_consume_token (parser->lexer);
28125 cp_parser_explicit_template_declaration (parser, member_p);
28126 return true;
28127 }
28128 else if (flag_concepts)
28129 return cp_parser_template_introduction (parser, member_p);
28130
28131 return false;
28132 }
28133
28134 /* Perform the deferred access checks from a template-parameter-list.
28135 CHECKS is a TREE_LIST of access checks, as returned by
28136 get_deferred_access_checks. */
28137
28138 static void
28139 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
28140 {
28141 ++processing_template_parmlist;
28142 perform_access_checks (checks, tf_warning_or_error);
28143 --processing_template_parmlist;
28144 }
28145
28146 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
28147 `function-definition' sequence that follows a template header.
28148 If MEMBER_P is true, this declaration appears in a class scope.
28149
28150 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
28151 *FRIEND_P is set to TRUE iff the declaration is a friend. */
28152
28153 static tree
28154 cp_parser_single_declaration (cp_parser* parser,
28155 vec<deferred_access_check, va_gc> *checks,
28156 bool member_p,
28157 bool explicit_specialization_p,
28158 bool* friend_p)
28159 {
28160 int declares_class_or_enum;
28161 tree decl = NULL_TREE;
28162 cp_decl_specifier_seq decl_specifiers;
28163 bool function_definition_p = false;
28164 cp_token *decl_spec_token_start;
28165
28166 /* This function is only used when processing a template
28167 declaration. */
28168 gcc_assert (innermost_scope_kind () == sk_template_parms
28169 || innermost_scope_kind () == sk_template_spec);
28170
28171 /* Defer access checks until we know what is being declared. */
28172 push_deferring_access_checks (dk_deferred);
28173
28174 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
28175 alternative. */
28176 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
28177 cp_parser_decl_specifier_seq (parser,
28178 (CP_PARSER_FLAGS_OPTIONAL
28179 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
28180 &decl_specifiers,
28181 &declares_class_or_enum);
28182 if (friend_p)
28183 *friend_p = cp_parser_friend_p (&decl_specifiers);
28184
28185 /* There are no template typedefs. */
28186 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
28187 {
28188 error_at (decl_spec_token_start->location,
28189 "template declaration of %<typedef%>");
28190 decl = error_mark_node;
28191 }
28192
28193 /* Gather up the access checks that occurred the
28194 decl-specifier-seq. */
28195 stop_deferring_access_checks ();
28196
28197 /* Check for the declaration of a template class. */
28198 if (declares_class_or_enum)
28199 {
28200 if (cp_parser_declares_only_class_p (parser)
28201 || (declares_class_or_enum & 2))
28202 {
28203 // If this is a declaration, but not a definition, associate
28204 // any constraints with the type declaration. Constraints
28205 // are associated with definitions in cp_parser_class_specifier.
28206 if (declares_class_or_enum == 1)
28207 associate_classtype_constraints (decl_specifiers.type);
28208
28209 decl = shadow_tag (&decl_specifiers);
28210
28211 /* In this case:
28212
28213 struct C {
28214 friend template <typename T> struct A<T>::B;
28215 };
28216
28217 A<T>::B will be represented by a TYPENAME_TYPE, and
28218 therefore not recognized by shadow_tag. */
28219 if (friend_p && *friend_p
28220 && !decl
28221 && decl_specifiers.type
28222 && TYPE_P (decl_specifiers.type))
28223 decl = decl_specifiers.type;
28224
28225 if (decl && decl != error_mark_node)
28226 decl = TYPE_NAME (decl);
28227 else
28228 decl = error_mark_node;
28229
28230 /* Perform access checks for template parameters. */
28231 cp_parser_perform_template_parameter_access_checks (checks);
28232
28233 /* Give a helpful diagnostic for
28234 template <class T> struct A { } a;
28235 if we aren't already recovering from an error. */
28236 if (!cp_parser_declares_only_class_p (parser)
28237 && !seen_error ())
28238 {
28239 error_at (cp_lexer_peek_token (parser->lexer)->location,
28240 "a class template declaration must not declare "
28241 "anything else");
28242 cp_parser_skip_to_end_of_block_or_statement (parser);
28243 goto out;
28244 }
28245 }
28246 }
28247
28248 /* Complain about missing 'typename' or other invalid type names. */
28249 if (!decl_specifiers.any_type_specifiers_p
28250 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
28251 {
28252 /* cp_parser_parse_and_diagnose_invalid_type_name calls
28253 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
28254 the rest of this declaration. */
28255 decl = error_mark_node;
28256 goto out;
28257 }
28258
28259 /* If it's not a template class, try for a template function. If
28260 the next token is a `;', then this declaration does not declare
28261 anything. But, if there were errors in the decl-specifiers, then
28262 the error might well have come from an attempted class-specifier.
28263 In that case, there's no need to warn about a missing declarator. */
28264 if (!decl
28265 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
28266 || decl_specifiers.type != error_mark_node))
28267 {
28268 decl = cp_parser_init_declarator (parser,
28269 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
28270 &decl_specifiers,
28271 checks,
28272 /*function_definition_allowed_p=*/true,
28273 member_p,
28274 declares_class_or_enum,
28275 &function_definition_p,
28276 NULL, NULL, NULL);
28277
28278 /* 7.1.1-1 [dcl.stc]
28279
28280 A storage-class-specifier shall not be specified in an explicit
28281 specialization... */
28282 if (decl
28283 && explicit_specialization_p
28284 && decl_specifiers.storage_class != sc_none)
28285 {
28286 error_at (decl_spec_token_start->location,
28287 "explicit template specialization cannot have a storage class");
28288 decl = error_mark_node;
28289 }
28290
28291 if (decl && VAR_P (decl))
28292 check_template_variable (decl);
28293 }
28294
28295 /* Look for a trailing `;' after the declaration. */
28296 if (!function_definition_p
28297 && (decl == error_mark_node
28298 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
28299 cp_parser_skip_to_end_of_block_or_statement (parser);
28300
28301 out:
28302 pop_deferring_access_checks ();
28303
28304 /* Clear any current qualification; whatever comes next is the start
28305 of something new. */
28306 parser->scope = NULL_TREE;
28307 parser->qualifying_scope = NULL_TREE;
28308 parser->object_scope = NULL_TREE;
28309
28310 return decl;
28311 }
28312
28313 /* Parse a cast-expression that is not the operand of a unary "&". */
28314
28315 static cp_expr
28316 cp_parser_simple_cast_expression (cp_parser *parser)
28317 {
28318 return cp_parser_cast_expression (parser, /*address_p=*/false,
28319 /*cast_p=*/false, /*decltype*/false, NULL);
28320 }
28321
28322 /* Parse a functional cast to TYPE. Returns an expression
28323 representing the cast. */
28324
28325 static cp_expr
28326 cp_parser_functional_cast (cp_parser* parser, tree type)
28327 {
28328 vec<tree, va_gc> *vec;
28329 tree expression_list;
28330 cp_expr cast;
28331 bool nonconst_p;
28332
28333 location_t start_loc = input_location;
28334
28335 if (!type)
28336 type = error_mark_node;
28337
28338 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28339 {
28340 cp_lexer_set_source_position (parser->lexer);
28341 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28342 expression_list = cp_parser_braced_list (parser, &nonconst_p);
28343 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
28344 if (TREE_CODE (type) == TYPE_DECL)
28345 type = TREE_TYPE (type);
28346
28347 cast = finish_compound_literal (type, expression_list,
28348 tf_warning_or_error, fcl_functional);
28349 /* Create a location of the form:
28350 type_name{i, f}
28351 ^~~~~~~~~~~~~~~
28352 with caret == start at the start of the type name,
28353 finishing at the closing brace. */
28354 location_t finish_loc
28355 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28356 location_t combined_loc = make_location (start_loc, start_loc,
28357 finish_loc);
28358 cast.set_location (combined_loc);
28359 return cast;
28360 }
28361
28362
28363 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
28364 /*cast_p=*/true,
28365 /*allow_expansion_p=*/true,
28366 /*non_constant_p=*/NULL);
28367 if (vec == NULL)
28368 expression_list = error_mark_node;
28369 else
28370 {
28371 expression_list = build_tree_list_vec (vec);
28372 release_tree_vector (vec);
28373 }
28374
28375 cast = build_functional_cast (type, expression_list,
28376 tf_warning_or_error);
28377 /* [expr.const]/1: In an integral constant expression "only type
28378 conversions to integral or enumeration type can be used". */
28379 if (TREE_CODE (type) == TYPE_DECL)
28380 type = TREE_TYPE (type);
28381 if (cast != error_mark_node
28382 && !cast_valid_in_integral_constant_expression_p (type)
28383 && cp_parser_non_integral_constant_expression (parser,
28384 NIC_CONSTRUCTOR))
28385 return error_mark_node;
28386
28387 /* Create a location of the form:
28388 float(i)
28389 ^~~~~~~~
28390 with caret == start at the start of the type name,
28391 finishing at the closing paren. */
28392 location_t finish_loc
28393 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28394 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
28395 cast.set_location (combined_loc);
28396 return cast;
28397 }
28398
28399 /* Save the tokens that make up the body of a member function defined
28400 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
28401 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
28402 specifiers applied to the declaration. Returns the FUNCTION_DECL
28403 for the member function. */
28404
28405 static tree
28406 cp_parser_save_member_function_body (cp_parser* parser,
28407 cp_decl_specifier_seq *decl_specifiers,
28408 cp_declarator *declarator,
28409 tree attributes)
28410 {
28411 cp_token *first;
28412 cp_token *last;
28413 tree fn;
28414 bool function_try_block = false;
28415
28416 /* Create the FUNCTION_DECL. */
28417 fn = grokmethod (decl_specifiers, declarator, attributes);
28418 cp_finalize_omp_declare_simd (parser, fn);
28419 cp_finalize_oacc_routine (parser, fn, true);
28420 /* If something went badly wrong, bail out now. */
28421 if (fn == error_mark_node)
28422 {
28423 /* If there's a function-body, skip it. */
28424 if (cp_parser_token_starts_function_definition_p
28425 (cp_lexer_peek_token (parser->lexer)))
28426 cp_parser_skip_to_end_of_block_or_statement (parser);
28427 return error_mark_node;
28428 }
28429
28430 /* Remember it, if there default args to post process. */
28431 cp_parser_save_default_args (parser, fn);
28432
28433 /* Save away the tokens that make up the body of the
28434 function. */
28435 first = parser->lexer->next_token;
28436
28437 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
28438 cp_lexer_consume_token (parser->lexer);
28439 else if (cp_lexer_next_token_is_keyword (parser->lexer,
28440 RID_TRANSACTION_ATOMIC))
28441 {
28442 cp_lexer_consume_token (parser->lexer);
28443 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
28444 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
28445 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
28446 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
28447 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28448 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
28449 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
28450 {
28451 cp_lexer_consume_token (parser->lexer);
28452 cp_lexer_consume_token (parser->lexer);
28453 cp_lexer_consume_token (parser->lexer);
28454 cp_lexer_consume_token (parser->lexer);
28455 cp_lexer_consume_token (parser->lexer);
28456 }
28457 else
28458 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
28459 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
28460 {
28461 cp_lexer_consume_token (parser->lexer);
28462 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28463 break;
28464 }
28465 }
28466
28467 /* Handle function try blocks. */
28468 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28469 {
28470 cp_lexer_consume_token (parser->lexer);
28471 function_try_block = true;
28472 }
28473 /* We can have braced-init-list mem-initializers before the fn body. */
28474 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28475 {
28476 cp_lexer_consume_token (parser->lexer);
28477 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
28478 {
28479 /* cache_group will stop after an un-nested { } pair, too. */
28480 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28481 break;
28482
28483 /* variadic mem-inits have ... after the ')'. */
28484 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28485 cp_lexer_consume_token (parser->lexer);
28486 }
28487 }
28488 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28489 /* Handle function try blocks. */
28490 if (function_try_block)
28491 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
28492 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28493 last = parser->lexer->next_token;
28494
28495 /* Save away the inline definition; we will process it when the
28496 class is complete. */
28497 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
28498 DECL_PENDING_INLINE_P (fn) = 1;
28499
28500 /* We need to know that this was defined in the class, so that
28501 friend templates are handled correctly. */
28502 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
28503
28504 /* Add FN to the queue of functions to be parsed later. */
28505 vec_safe_push (unparsed_funs_with_definitions, fn);
28506
28507 return fn;
28508 }
28509
28510 /* Save the tokens that make up the in-class initializer for a non-static
28511 data member. Returns a DEFAULT_ARG. */
28512
28513 static tree
28514 cp_parser_save_nsdmi (cp_parser* parser)
28515 {
28516 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
28517 }
28518
28519 /* Parse a template-argument-list, as well as the trailing ">" (but
28520 not the opening "<"). See cp_parser_template_argument_list for the
28521 return value. */
28522
28523 static tree
28524 cp_parser_enclosed_template_argument_list (cp_parser* parser)
28525 {
28526 tree arguments;
28527 tree saved_scope;
28528 tree saved_qualifying_scope;
28529 tree saved_object_scope;
28530 bool saved_greater_than_is_operator_p;
28531
28532 /* [temp.names]
28533
28534 When parsing a template-id, the first non-nested `>' is taken as
28535 the end of the template-argument-list rather than a greater-than
28536 operator. */
28537 saved_greater_than_is_operator_p
28538 = parser->greater_than_is_operator_p;
28539 parser->greater_than_is_operator_p = false;
28540 /* Parsing the argument list may modify SCOPE, so we save it
28541 here. */
28542 saved_scope = parser->scope;
28543 saved_qualifying_scope = parser->qualifying_scope;
28544 saved_object_scope = parser->object_scope;
28545 /* We need to evaluate the template arguments, even though this
28546 template-id may be nested within a "sizeof". */
28547 cp_evaluated ev;
28548 /* Parse the template-argument-list itself. */
28549 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
28550 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28551 arguments = NULL_TREE;
28552 else
28553 arguments = cp_parser_template_argument_list (parser);
28554 /* Look for the `>' that ends the template-argument-list. If we find
28555 a '>>' instead, it's probably just a typo. */
28556 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28557 {
28558 if (cxx_dialect != cxx98)
28559 {
28560 /* In C++0x, a `>>' in a template argument list or cast
28561 expression is considered to be two separate `>'
28562 tokens. So, change the current token to a `>', but don't
28563 consume it: it will be consumed later when the outer
28564 template argument list (or cast expression) is parsed.
28565 Note that this replacement of `>' for `>>' is necessary
28566 even if we are parsing tentatively: in the tentative
28567 case, after calling
28568 cp_parser_enclosed_template_argument_list we will always
28569 throw away all of the template arguments and the first
28570 closing `>', either because the template argument list
28571 was erroneous or because we are replacing those tokens
28572 with a CPP_TEMPLATE_ID token. The second `>' (which will
28573 not have been thrown away) is needed either to close an
28574 outer template argument list or to complete a new-style
28575 cast. */
28576 cp_token *token = cp_lexer_peek_token (parser->lexer);
28577 token->type = CPP_GREATER;
28578 }
28579 else if (!saved_greater_than_is_operator_p)
28580 {
28581 /* If we're in a nested template argument list, the '>>' has
28582 to be a typo for '> >'. We emit the error message, but we
28583 continue parsing and we push a '>' as next token, so that
28584 the argument list will be parsed correctly. Note that the
28585 global source location is still on the token before the
28586 '>>', so we need to say explicitly where we want it. */
28587 cp_token *token = cp_lexer_peek_token (parser->lexer);
28588 gcc_rich_location richloc (token->location);
28589 richloc.add_fixit_replace ("> >");
28590 error_at (&richloc, "%<>>%> should be %<> >%> "
28591 "within a nested template argument list");
28592
28593 token->type = CPP_GREATER;
28594 }
28595 else
28596 {
28597 /* If this is not a nested template argument list, the '>>'
28598 is a typo for '>'. Emit an error message and continue.
28599 Same deal about the token location, but here we can get it
28600 right by consuming the '>>' before issuing the diagnostic. */
28601 cp_token *token = cp_lexer_consume_token (parser->lexer);
28602 error_at (token->location,
28603 "spurious %<>>%>, use %<>%> to terminate "
28604 "a template argument list");
28605 }
28606 }
28607 else
28608 cp_parser_skip_to_end_of_template_parameter_list (parser);
28609 /* The `>' token might be a greater-than operator again now. */
28610 parser->greater_than_is_operator_p
28611 = saved_greater_than_is_operator_p;
28612 /* Restore the SAVED_SCOPE. */
28613 parser->scope = saved_scope;
28614 parser->qualifying_scope = saved_qualifying_scope;
28615 parser->object_scope = saved_object_scope;
28616
28617 return arguments;
28618 }
28619
28620 /* MEMBER_FUNCTION is a member function, or a friend. If default
28621 arguments, or the body of the function have not yet been parsed,
28622 parse them now. */
28623
28624 static void
28625 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
28626 {
28627 timevar_push (TV_PARSE_INMETH);
28628 /* If this member is a template, get the underlying
28629 FUNCTION_DECL. */
28630 if (DECL_FUNCTION_TEMPLATE_P (member_function))
28631 member_function = DECL_TEMPLATE_RESULT (member_function);
28632
28633 /* There should not be any class definitions in progress at this
28634 point; the bodies of members are only parsed outside of all class
28635 definitions. */
28636 gcc_assert (parser->num_classes_being_defined == 0);
28637 /* While we're parsing the member functions we might encounter more
28638 classes. We want to handle them right away, but we don't want
28639 them getting mixed up with functions that are currently in the
28640 queue. */
28641 push_unparsed_function_queues (parser);
28642
28643 /* Make sure that any template parameters are in scope. */
28644 maybe_begin_member_template_processing (member_function);
28645
28646 /* If the body of the function has not yet been parsed, parse it
28647 now. */
28648 if (DECL_PENDING_INLINE_P (member_function))
28649 {
28650 tree function_scope;
28651 cp_token_cache *tokens;
28652
28653 /* The function is no longer pending; we are processing it. */
28654 tokens = DECL_PENDING_INLINE_INFO (member_function);
28655 DECL_PENDING_INLINE_INFO (member_function) = NULL;
28656 DECL_PENDING_INLINE_P (member_function) = 0;
28657
28658 /* If this is a local class, enter the scope of the containing
28659 function. */
28660 function_scope = current_function_decl;
28661 if (function_scope)
28662 push_function_context ();
28663
28664 /* Push the body of the function onto the lexer stack. */
28665 cp_parser_push_lexer_for_tokens (parser, tokens);
28666
28667 /* Let the front end know that we going to be defining this
28668 function. */
28669 start_preparsed_function (member_function, NULL_TREE,
28670 SF_PRE_PARSED | SF_INCLASS_INLINE);
28671
28672 /* Don't do access checking if it is a templated function. */
28673 if (processing_template_decl)
28674 push_deferring_access_checks (dk_no_check);
28675
28676 /* #pragma omp declare reduction needs special parsing. */
28677 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
28678 {
28679 parser->lexer->in_pragma = true;
28680 cp_parser_omp_declare_reduction_exprs (member_function, parser);
28681 finish_function (/*inline_p=*/true);
28682 cp_check_omp_declare_reduction (member_function);
28683 }
28684 else
28685 /* Now, parse the body of the function. */
28686 cp_parser_function_definition_after_declarator (parser,
28687 /*inline_p=*/true);
28688
28689 if (processing_template_decl)
28690 pop_deferring_access_checks ();
28691
28692 /* Leave the scope of the containing function. */
28693 if (function_scope)
28694 pop_function_context ();
28695 cp_parser_pop_lexer (parser);
28696 }
28697
28698 /* Remove any template parameters from the symbol table. */
28699 maybe_end_member_template_processing ();
28700
28701 /* Restore the queue. */
28702 pop_unparsed_function_queues (parser);
28703 timevar_pop (TV_PARSE_INMETH);
28704 }
28705
28706 /* If DECL contains any default args, remember it on the unparsed
28707 functions queue. */
28708
28709 static void
28710 cp_parser_save_default_args (cp_parser* parser, tree decl)
28711 {
28712 tree probe;
28713
28714 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28715 probe;
28716 probe = TREE_CHAIN (probe))
28717 if (TREE_PURPOSE (probe))
28718 {
28719 cp_default_arg_entry entry = {current_class_type, decl};
28720 vec_safe_push (unparsed_funs_with_default_args, entry);
28721 break;
28722 }
28723 }
28724
28725 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28726 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28727 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28728 from the parameter-type-list. */
28729
28730 static tree
28731 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28732 tree default_arg, tree parmtype)
28733 {
28734 cp_token_cache *tokens;
28735 tree parsed_arg;
28736 bool dummy;
28737
28738 if (default_arg == error_mark_node)
28739 return error_mark_node;
28740
28741 /* Push the saved tokens for the default argument onto the parser's
28742 lexer stack. */
28743 tokens = DEFARG_TOKENS (default_arg);
28744 cp_parser_push_lexer_for_tokens (parser, tokens);
28745
28746 start_lambda_scope (decl);
28747
28748 /* Parse the default argument. */
28749 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28750 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28751 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28752
28753 finish_lambda_scope ();
28754
28755 if (parsed_arg == error_mark_node)
28756 cp_parser_skip_to_end_of_statement (parser);
28757
28758 if (!processing_template_decl)
28759 {
28760 /* In a non-template class, check conversions now. In a template,
28761 we'll wait and instantiate these as needed. */
28762 if (TREE_CODE (decl) == PARM_DECL)
28763 parsed_arg = check_default_argument (parmtype, parsed_arg,
28764 tf_warning_or_error);
28765 else if (maybe_reject_flexarray_init (decl, parsed_arg))
28766 parsed_arg = error_mark_node;
28767 else
28768 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28769 }
28770
28771 /* If the token stream has not been completely used up, then
28772 there was extra junk after the end of the default
28773 argument. */
28774 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28775 {
28776 if (TREE_CODE (decl) == PARM_DECL)
28777 cp_parser_error (parser, "expected %<,%>");
28778 else
28779 cp_parser_error (parser, "expected %<;%>");
28780 }
28781
28782 /* Revert to the main lexer. */
28783 cp_parser_pop_lexer (parser);
28784
28785 return parsed_arg;
28786 }
28787
28788 /* FIELD is a non-static data member with an initializer which we saved for
28789 later; parse it now. */
28790
28791 static void
28792 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
28793 {
28794 tree def;
28795
28796 maybe_begin_member_template_processing (field);
28797
28798 push_unparsed_function_queues (parser);
28799 def = cp_parser_late_parse_one_default_arg (parser, field,
28800 DECL_INITIAL (field),
28801 NULL_TREE);
28802 pop_unparsed_function_queues (parser);
28803
28804 maybe_end_member_template_processing ();
28805
28806 DECL_INITIAL (field) = def;
28807 }
28808
28809 /* FN is a FUNCTION_DECL which may contains a parameter with an
28810 unparsed DEFAULT_ARG. Parse the default args now. This function
28811 assumes that the current scope is the scope in which the default
28812 argument should be processed. */
28813
28814 static void
28815 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
28816 {
28817 unsigned char saved_local_variables_forbidden_p;
28818 tree parm, parmdecl;
28819
28820 /* While we're parsing the default args, we might (due to the
28821 statement expression extension) encounter more classes. We want
28822 to handle them right away, but we don't want them getting mixed
28823 up with default args that are currently in the queue. */
28824 push_unparsed_function_queues (parser);
28825
28826 /* Local variable names (and the `this' keyword) may not appear
28827 in a default argument. */
28828 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28829 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
28830
28831 push_defarg_context (fn);
28832
28833 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28834 parmdecl = DECL_ARGUMENTS (fn);
28835 parm && parm != void_list_node;
28836 parm = TREE_CHAIN (parm),
28837 parmdecl = DECL_CHAIN (parmdecl))
28838 {
28839 tree default_arg = TREE_PURPOSE (parm);
28840 tree parsed_arg;
28841 vec<tree, va_gc> *insts;
28842 tree copy;
28843 unsigned ix;
28844
28845 if (!default_arg)
28846 continue;
28847
28848 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28849 /* This can happen for a friend declaration for a function
28850 already declared with default arguments. */
28851 continue;
28852
28853 parsed_arg
28854 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28855 default_arg,
28856 TREE_VALUE (parm));
28857 TREE_PURPOSE (parm) = parsed_arg;
28858
28859 /* Update any instantiations we've already created. */
28860 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28861 vec_safe_iterate (insts, ix, &copy); ix++)
28862 TREE_PURPOSE (copy) = parsed_arg;
28863 }
28864
28865 pop_defarg_context ();
28866
28867 /* Make sure no default arg is missing. */
28868 check_default_args (fn);
28869
28870 /* Restore the state of local_variables_forbidden_p. */
28871 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28872
28873 /* Restore the queue. */
28874 pop_unparsed_function_queues (parser);
28875 }
28876
28877 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28878
28879 sizeof ... ( identifier )
28880
28881 where the 'sizeof' token has already been consumed. */
28882
28883 static tree
28884 cp_parser_sizeof_pack (cp_parser *parser)
28885 {
28886 /* Consume the `...'. */
28887 cp_lexer_consume_token (parser->lexer);
28888 maybe_warn_variadic_templates ();
28889
28890 matching_parens parens;
28891 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28892 if (paren)
28893 parens.consume_open (parser);
28894 else
28895 permerror (cp_lexer_peek_token (parser->lexer)->location,
28896 "%<sizeof...%> argument must be surrounded by parentheses");
28897
28898 cp_token *token = cp_lexer_peek_token (parser->lexer);
28899 tree name = cp_parser_identifier (parser);
28900 if (name == error_mark_node)
28901 return error_mark_node;
28902 /* The name is not qualified. */
28903 parser->scope = NULL_TREE;
28904 parser->qualifying_scope = NULL_TREE;
28905 parser->object_scope = NULL_TREE;
28906 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28907 if (expr == error_mark_node)
28908 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28909 token->location);
28910 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28911 expr = TREE_TYPE (expr);
28912 else if (TREE_CODE (expr) == CONST_DECL)
28913 expr = DECL_INITIAL (expr);
28914 expr = make_pack_expansion (expr);
28915 PACK_EXPANSION_SIZEOF_P (expr) = true;
28916
28917 if (paren)
28918 parens.require_close (parser);
28919
28920 return expr;
28921 }
28922
28923 /* Parse the operand of `sizeof' (or a similar operator). Returns
28924 either a TYPE or an expression, depending on the form of the
28925 input. The KEYWORD indicates which kind of expression we have
28926 encountered. */
28927
28928 static tree
28929 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28930 {
28931 tree expr = NULL_TREE;
28932 const char *saved_message;
28933 const char *saved_message_arg;
28934 bool saved_integral_constant_expression_p;
28935 bool saved_non_integral_constant_expression_p;
28936
28937 /* If it's a `...', then we are computing the length of a parameter
28938 pack. */
28939 if (keyword == RID_SIZEOF
28940 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28941 return cp_parser_sizeof_pack (parser);
28942
28943 /* Types cannot be defined in a `sizeof' expression. Save away the
28944 old message. */
28945 saved_message = parser->type_definition_forbidden_message;
28946 saved_message_arg = parser->type_definition_forbidden_message_arg;
28947 parser->type_definition_forbidden_message
28948 = G_("types may not be defined in %qs expressions");
28949 parser->type_definition_forbidden_message_arg
28950 = IDENTIFIER_POINTER (ridpointers[keyword]);
28951
28952 /* The restrictions on constant-expressions do not apply inside
28953 sizeof expressions. */
28954 saved_integral_constant_expression_p
28955 = parser->integral_constant_expression_p;
28956 saved_non_integral_constant_expression_p
28957 = parser->non_integral_constant_expression_p;
28958 parser->integral_constant_expression_p = false;
28959
28960 /* Do not actually evaluate the expression. */
28961 ++cp_unevaluated_operand;
28962 ++c_inhibit_evaluation_warnings;
28963 /* If it's a `(', then we might be looking at the type-id
28964 construction. */
28965 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28966 {
28967 tree type = NULL_TREE;
28968
28969 /* We can't be sure yet whether we're looking at a type-id or an
28970 expression. */
28971 cp_parser_parse_tentatively (parser);
28972
28973 matching_parens parens;
28974 parens.consume_open (parser);
28975
28976 /* Note: as a GNU Extension, compound literals are considered
28977 postfix-expressions as they are in C99, so they are valid
28978 arguments to sizeof. See comment in cp_parser_cast_expression
28979 for details. */
28980 if (cp_parser_compound_literal_p (parser))
28981 cp_parser_simulate_error (parser);
28982 else
28983 {
28984 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28985 parser->in_type_id_in_expr_p = true;
28986 /* Look for the type-id. */
28987 type = cp_parser_type_id (parser);
28988 /* Look for the closing `)'. */
28989 parens.require_close (parser);
28990 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28991 }
28992
28993 /* If all went well, then we're done. */
28994 if (cp_parser_parse_definitely (parser))
28995 expr = type;
28996 }
28997
28998 /* If the type-id production did not work out, then we must be
28999 looking at the unary-expression production. */
29000 if (!expr)
29001 expr = cp_parser_unary_expression (parser);
29002
29003 /* Go back to evaluating expressions. */
29004 --cp_unevaluated_operand;
29005 --c_inhibit_evaluation_warnings;
29006
29007 /* And restore the old one. */
29008 parser->type_definition_forbidden_message = saved_message;
29009 parser->type_definition_forbidden_message_arg = saved_message_arg;
29010 parser->integral_constant_expression_p
29011 = saved_integral_constant_expression_p;
29012 parser->non_integral_constant_expression_p
29013 = saved_non_integral_constant_expression_p;
29014
29015 return expr;
29016 }
29017
29018 /* If the current declaration has no declarator, return true. */
29019
29020 static bool
29021 cp_parser_declares_only_class_p (cp_parser *parser)
29022 {
29023 /* If the next token is a `;' or a `,' then there is no
29024 declarator. */
29025 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29026 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
29027 }
29028
29029 /* Update the DECL_SPECS to reflect the storage class indicated by
29030 KEYWORD. */
29031
29032 static void
29033 cp_parser_set_storage_class (cp_parser *parser,
29034 cp_decl_specifier_seq *decl_specs,
29035 enum rid keyword,
29036 cp_token *token)
29037 {
29038 cp_storage_class storage_class;
29039
29040 if (parser->in_unbraced_linkage_specification_p)
29041 {
29042 error_at (token->location, "invalid use of %qD in linkage specification",
29043 ridpointers[keyword]);
29044 return;
29045 }
29046 else if (decl_specs->storage_class != sc_none)
29047 {
29048 decl_specs->conflicting_specifiers_p = true;
29049 return;
29050 }
29051
29052 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
29053 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
29054 && decl_specs->gnu_thread_keyword_p)
29055 {
29056 pedwarn (decl_specs->locations[ds_thread], 0,
29057 "%<__thread%> before %qD", ridpointers[keyword]);
29058 }
29059
29060 switch (keyword)
29061 {
29062 case RID_AUTO:
29063 storage_class = sc_auto;
29064 break;
29065 case RID_REGISTER:
29066 storage_class = sc_register;
29067 break;
29068 case RID_STATIC:
29069 storage_class = sc_static;
29070 break;
29071 case RID_EXTERN:
29072 storage_class = sc_extern;
29073 break;
29074 case RID_MUTABLE:
29075 storage_class = sc_mutable;
29076 break;
29077 default:
29078 gcc_unreachable ();
29079 }
29080 decl_specs->storage_class = storage_class;
29081 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
29082
29083 /* A storage class specifier cannot be applied alongside a typedef
29084 specifier. If there is a typedef specifier present then set
29085 conflicting_specifiers_p which will trigger an error later
29086 on in grokdeclarator. */
29087 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
29088 decl_specs->conflicting_specifiers_p = true;
29089 }
29090
29091 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
29092 is true, the type is a class or enum definition. */
29093
29094 static void
29095 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
29096 tree type_spec,
29097 cp_token *token,
29098 bool type_definition_p)
29099 {
29100 decl_specs->any_specifiers_p = true;
29101
29102 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
29103 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
29104 this is what happened. In system headers, we ignore these
29105 declarations so that G++ can work with system headers that are not
29106 C++-safe. */
29107 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
29108 && !type_definition_p
29109 && (type_spec == boolean_type_node
29110 || type_spec == char8_type_node
29111 || type_spec == char16_type_node
29112 || type_spec == char32_type_node
29113 || type_spec == wchar_type_node)
29114 && (decl_specs->type
29115 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
29116 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
29117 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
29118 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
29119 {
29120 decl_specs->redefined_builtin_type = type_spec;
29121 set_and_check_decl_spec_loc (decl_specs,
29122 ds_redefined_builtin_type_spec,
29123 token);
29124 if (!decl_specs->type)
29125 {
29126 decl_specs->type = type_spec;
29127 decl_specs->type_definition_p = false;
29128 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
29129 }
29130 }
29131 else if (decl_specs->type)
29132 decl_specs->multiple_types_p = true;
29133 else
29134 {
29135 decl_specs->type = type_spec;
29136 decl_specs->type_definition_p = type_definition_p;
29137 decl_specs->redefined_builtin_type = NULL_TREE;
29138 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
29139 }
29140 }
29141
29142 /* True iff TOKEN is the GNU keyword __thread. */
29143
29144 static bool
29145 token_is__thread (cp_token *token)
29146 {
29147 gcc_assert (token->keyword == RID_THREAD);
29148 return id_equal (token->u.value, "__thread");
29149 }
29150
29151 /* Set the location for a declarator specifier and check if it is
29152 duplicated.
29153
29154 DECL_SPECS is the sequence of declarator specifiers onto which to
29155 set the location.
29156
29157 DS is the single declarator specifier to set which location is to
29158 be set onto the existing sequence of declarators.
29159
29160 LOCATION is the location for the declarator specifier to
29161 consider. */
29162
29163 static void
29164 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
29165 cp_decl_spec ds, cp_token *token)
29166 {
29167 gcc_assert (ds < ds_last);
29168
29169 if (decl_specs == NULL)
29170 return;
29171
29172 location_t location = token->location;
29173
29174 if (decl_specs->locations[ds] == 0)
29175 {
29176 decl_specs->locations[ds] = location;
29177 if (ds == ds_thread)
29178 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
29179 }
29180 else
29181 {
29182 if (ds == ds_long)
29183 {
29184 if (decl_specs->locations[ds_long_long] != 0)
29185 error_at (location,
29186 "%<long long long%> is too long for GCC");
29187 else
29188 {
29189 decl_specs->locations[ds_long_long] = location;
29190 pedwarn_cxx98 (location,
29191 OPT_Wlong_long,
29192 "ISO C++ 1998 does not support %<long long%>");
29193 }
29194 }
29195 else if (ds == ds_thread)
29196 {
29197 bool gnu = token_is__thread (token);
29198 gcc_rich_location richloc (location);
29199 if (gnu != decl_specs->gnu_thread_keyword_p)
29200 {
29201 richloc.add_range (decl_specs->locations[ds_thread]);
29202 error_at (&richloc,
29203 "both %<__thread%> and %<thread_local%> specified");
29204 }
29205 else
29206 {
29207 richloc.add_fixit_remove ();
29208 error_at (&richloc, "duplicate %qD", token->u.value);
29209 }
29210 }
29211 else
29212 {
29213 static const char *const decl_spec_names[] = {
29214 "signed",
29215 "unsigned",
29216 "short",
29217 "long",
29218 "const",
29219 "volatile",
29220 "restrict",
29221 "inline",
29222 "virtual",
29223 "explicit",
29224 "friend",
29225 "typedef",
29226 "using",
29227 "constexpr",
29228 "__complex"
29229 };
29230 gcc_rich_location richloc (location);
29231 richloc.add_fixit_remove ();
29232 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
29233 }
29234 }
29235 }
29236
29237 /* Return true iff the declarator specifier DS is present in the
29238 sequence of declarator specifiers DECL_SPECS. */
29239
29240 bool
29241 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
29242 cp_decl_spec ds)
29243 {
29244 gcc_assert (ds < ds_last);
29245
29246 if (decl_specs == NULL)
29247 return false;
29248
29249 return decl_specs->locations[ds] != 0;
29250 }
29251
29252 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
29253 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
29254
29255 static bool
29256 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
29257 {
29258 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
29259 }
29260
29261 /* Issue an error message indicating that TOKEN_DESC was expected.
29262 If KEYWORD is true, it indicated this function is called by
29263 cp_parser_require_keword and the required token can only be
29264 a indicated keyword.
29265
29266 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29267 within any error as the location of an "opening" token matching
29268 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29269 RT_CLOSE_PAREN). */
29270
29271 static void
29272 cp_parser_required_error (cp_parser *parser,
29273 required_token token_desc,
29274 bool keyword,
29275 location_t matching_location)
29276 {
29277 if (cp_parser_simulate_error (parser))
29278 return;
29279
29280 const char *gmsgid = NULL;
29281 switch (token_desc)
29282 {
29283 case RT_NEW:
29284 gmsgid = G_("expected %<new%>");
29285 break;
29286 case RT_DELETE:
29287 gmsgid = G_("expected %<delete%>");
29288 break;
29289 case RT_RETURN:
29290 gmsgid = G_("expected %<return%>");
29291 break;
29292 case RT_WHILE:
29293 gmsgid = G_("expected %<while%>");
29294 break;
29295 case RT_EXTERN:
29296 gmsgid = G_("expected %<extern%>");
29297 break;
29298 case RT_STATIC_ASSERT:
29299 gmsgid = G_("expected %<static_assert%>");
29300 break;
29301 case RT_DECLTYPE:
29302 gmsgid = G_("expected %<decltype%>");
29303 break;
29304 case RT_OPERATOR:
29305 gmsgid = G_("expected %<operator%>");
29306 break;
29307 case RT_CLASS:
29308 gmsgid = G_("expected %<class%>");
29309 break;
29310 case RT_TEMPLATE:
29311 gmsgid = G_("expected %<template%>");
29312 break;
29313 case RT_NAMESPACE:
29314 gmsgid = G_("expected %<namespace%>");
29315 break;
29316 case RT_USING:
29317 gmsgid = G_("expected %<using%>");
29318 break;
29319 case RT_ASM:
29320 gmsgid = G_("expected %<asm%>");
29321 break;
29322 case RT_TRY:
29323 gmsgid = G_("expected %<try%>");
29324 break;
29325 case RT_CATCH:
29326 gmsgid = G_("expected %<catch%>");
29327 break;
29328 case RT_THROW:
29329 gmsgid = G_("expected %<throw%>");
29330 break;
29331 case RT_LABEL:
29332 gmsgid = G_("expected %<__label__%>");
29333 break;
29334 case RT_AT_TRY:
29335 gmsgid = G_("expected %<@try%>");
29336 break;
29337 case RT_AT_SYNCHRONIZED:
29338 gmsgid = G_("expected %<@synchronized%>");
29339 break;
29340 case RT_AT_THROW:
29341 gmsgid = G_("expected %<@throw%>");
29342 break;
29343 case RT_TRANSACTION_ATOMIC:
29344 gmsgid = G_("expected %<__transaction_atomic%>");
29345 break;
29346 case RT_TRANSACTION_RELAXED:
29347 gmsgid = G_("expected %<__transaction_relaxed%>");
29348 break;
29349 default:
29350 break;
29351 }
29352
29353 if (!gmsgid && !keyword)
29354 {
29355 switch (token_desc)
29356 {
29357 case RT_SEMICOLON:
29358 gmsgid = G_("expected %<;%>");
29359 break;
29360 case RT_OPEN_PAREN:
29361 gmsgid = G_("expected %<(%>");
29362 break;
29363 case RT_CLOSE_BRACE:
29364 gmsgid = G_("expected %<}%>");
29365 break;
29366 case RT_OPEN_BRACE:
29367 gmsgid = G_("expected %<{%>");
29368 break;
29369 case RT_CLOSE_SQUARE:
29370 gmsgid = G_("expected %<]%>");
29371 break;
29372 case RT_OPEN_SQUARE:
29373 gmsgid = G_("expected %<[%>");
29374 break;
29375 case RT_COMMA:
29376 gmsgid = G_("expected %<,%>");
29377 break;
29378 case RT_SCOPE:
29379 gmsgid = G_("expected %<::%>");
29380 break;
29381 case RT_LESS:
29382 gmsgid = G_("expected %<<%>");
29383 break;
29384 case RT_GREATER:
29385 gmsgid = G_("expected %<>%>");
29386 break;
29387 case RT_EQ:
29388 gmsgid = G_("expected %<=%>");
29389 break;
29390 case RT_ELLIPSIS:
29391 gmsgid = G_("expected %<...%>");
29392 break;
29393 case RT_MULT:
29394 gmsgid = G_("expected %<*%>");
29395 break;
29396 case RT_COMPL:
29397 gmsgid = G_("expected %<~%>");
29398 break;
29399 case RT_COLON:
29400 gmsgid = G_("expected %<:%>");
29401 break;
29402 case RT_COLON_SCOPE:
29403 gmsgid = G_("expected %<:%> or %<::%>");
29404 break;
29405 case RT_CLOSE_PAREN:
29406 gmsgid = G_("expected %<)%>");
29407 break;
29408 case RT_COMMA_CLOSE_PAREN:
29409 gmsgid = G_("expected %<,%> or %<)%>");
29410 break;
29411 case RT_PRAGMA_EOL:
29412 gmsgid = G_("expected end of line");
29413 break;
29414 case RT_NAME:
29415 gmsgid = G_("expected identifier");
29416 break;
29417 case RT_SELECT:
29418 gmsgid = G_("expected selection-statement");
29419 break;
29420 case RT_ITERATION:
29421 gmsgid = G_("expected iteration-statement");
29422 break;
29423 case RT_JUMP:
29424 gmsgid = G_("expected jump-statement");
29425 break;
29426 case RT_CLASS_KEY:
29427 gmsgid = G_("expected class-key");
29428 break;
29429 case RT_CLASS_TYPENAME_TEMPLATE:
29430 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
29431 break;
29432 default:
29433 gcc_unreachable ();
29434 }
29435 }
29436
29437 if (gmsgid)
29438 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
29439 }
29440
29441
29442 /* If the next token is of the indicated TYPE, consume it. Otherwise,
29443 issue an error message indicating that TOKEN_DESC was expected.
29444
29445 Returns the token consumed, if the token had the appropriate type.
29446 Otherwise, returns NULL.
29447
29448 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29449 within any error as the location of an "opening" token matching
29450 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29451 RT_CLOSE_PAREN). */
29452
29453 static cp_token *
29454 cp_parser_require (cp_parser* parser,
29455 enum cpp_ttype type,
29456 required_token token_desc,
29457 location_t matching_location)
29458 {
29459 if (cp_lexer_next_token_is (parser->lexer, type))
29460 return cp_lexer_consume_token (parser->lexer);
29461 else
29462 {
29463 /* Output the MESSAGE -- unless we're parsing tentatively. */
29464 if (!cp_parser_simulate_error (parser))
29465 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
29466 matching_location);
29467 return NULL;
29468 }
29469 }
29470
29471 /* An error message is produced if the next token is not '>'.
29472 All further tokens are skipped until the desired token is
29473 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29474
29475 static void
29476 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
29477 {
29478 /* Current level of '< ... >'. */
29479 unsigned level = 0;
29480 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29481 unsigned nesting_depth = 0;
29482
29483 /* Are we ready, yet? If not, issue error message. */
29484 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
29485 return;
29486
29487 /* Skip tokens until the desired token is found. */
29488 while (true)
29489 {
29490 /* Peek at the next token. */
29491 switch (cp_lexer_peek_token (parser->lexer)->type)
29492 {
29493 case CPP_LESS:
29494 if (!nesting_depth)
29495 ++level;
29496 break;
29497
29498 case CPP_RSHIFT:
29499 if (cxx_dialect == cxx98)
29500 /* C++0x views the `>>' operator as two `>' tokens, but
29501 C++98 does not. */
29502 break;
29503 else if (!nesting_depth && level-- == 0)
29504 {
29505 /* We've hit a `>>' where the first `>' closes the
29506 template argument list, and the second `>' is
29507 spurious. Just consume the `>>' and stop; we've
29508 already produced at least one error. */
29509 cp_lexer_consume_token (parser->lexer);
29510 return;
29511 }
29512 /* Fall through for C++0x, so we handle the second `>' in
29513 the `>>'. */
29514 gcc_fallthrough ();
29515
29516 case CPP_GREATER:
29517 if (!nesting_depth && level-- == 0)
29518 {
29519 /* We've reached the token we want, consume it and stop. */
29520 cp_lexer_consume_token (parser->lexer);
29521 return;
29522 }
29523 break;
29524
29525 case CPP_OPEN_PAREN:
29526 case CPP_OPEN_SQUARE:
29527 ++nesting_depth;
29528 break;
29529
29530 case CPP_CLOSE_PAREN:
29531 case CPP_CLOSE_SQUARE:
29532 if (nesting_depth-- == 0)
29533 return;
29534 break;
29535
29536 case CPP_EOF:
29537 case CPP_PRAGMA_EOL:
29538 case CPP_SEMICOLON:
29539 case CPP_OPEN_BRACE:
29540 case CPP_CLOSE_BRACE:
29541 /* The '>' was probably forgotten, don't look further. */
29542 return;
29543
29544 default:
29545 break;
29546 }
29547
29548 /* Consume this token. */
29549 cp_lexer_consume_token (parser->lexer);
29550 }
29551 }
29552
29553 /* If the next token is the indicated keyword, consume it. Otherwise,
29554 issue an error message indicating that TOKEN_DESC was expected.
29555
29556 Returns the token consumed, if the token had the appropriate type.
29557 Otherwise, returns NULL. */
29558
29559 static cp_token *
29560 cp_parser_require_keyword (cp_parser* parser,
29561 enum rid keyword,
29562 required_token token_desc)
29563 {
29564 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
29565
29566 if (token && token->keyword != keyword)
29567 {
29568 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
29569 UNKNOWN_LOCATION);
29570 return NULL;
29571 }
29572
29573 return token;
29574 }
29575
29576 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29577 function-definition. */
29578
29579 static bool
29580 cp_parser_token_starts_function_definition_p (cp_token* token)
29581 {
29582 return (/* An ordinary function-body begins with an `{'. */
29583 token->type == CPP_OPEN_BRACE
29584 /* A ctor-initializer begins with a `:'. */
29585 || token->type == CPP_COLON
29586 /* A function-try-block begins with `try'. */
29587 || token->keyword == RID_TRY
29588 /* A function-transaction-block begins with `__transaction_atomic'
29589 or `__transaction_relaxed'. */
29590 || token->keyword == RID_TRANSACTION_ATOMIC
29591 || token->keyword == RID_TRANSACTION_RELAXED
29592 /* The named return value extension begins with `return'. */
29593 || token->keyword == RID_RETURN);
29594 }
29595
29596 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29597 definition. */
29598
29599 static bool
29600 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
29601 {
29602 cp_token *token;
29603
29604 token = cp_lexer_peek_token (parser->lexer);
29605 return (token->type == CPP_OPEN_BRACE
29606 || (token->type == CPP_COLON
29607 && !parser->colon_doesnt_start_class_def_p));
29608 }
29609
29610 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29611 C++0x) ending a template-argument. */
29612
29613 static bool
29614 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
29615 {
29616 cp_token *token;
29617
29618 token = cp_lexer_peek_token (parser->lexer);
29619 return (token->type == CPP_COMMA
29620 || token->type == CPP_GREATER
29621 || token->type == CPP_ELLIPSIS
29622 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
29623 }
29624
29625 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29626 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29627
29628 static bool
29629 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
29630 size_t n)
29631 {
29632 cp_token *token;
29633
29634 token = cp_lexer_peek_nth_token (parser->lexer, n);
29635 if (token->type == CPP_LESS)
29636 return true;
29637 /* Check for the sequence `<::' in the original code. It would be lexed as
29638 `[:', where `[' is a digraph, and there is no whitespace before
29639 `:'. */
29640 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
29641 {
29642 cp_token *token2;
29643 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
29644 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
29645 return true;
29646 }
29647 return false;
29648 }
29649
29650 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29651 or none_type otherwise. */
29652
29653 static enum tag_types
29654 cp_parser_token_is_class_key (cp_token* token)
29655 {
29656 switch (token->keyword)
29657 {
29658 case RID_CLASS:
29659 return class_type;
29660 case RID_STRUCT:
29661 return record_type;
29662 case RID_UNION:
29663 return union_type;
29664
29665 default:
29666 return none_type;
29667 }
29668 }
29669
29670 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29671 or none_type otherwise or if the token is null. */
29672
29673 static enum tag_types
29674 cp_parser_token_is_type_parameter_key (cp_token* token)
29675 {
29676 if (!token)
29677 return none_type;
29678
29679 switch (token->keyword)
29680 {
29681 case RID_CLASS:
29682 return class_type;
29683 case RID_TYPENAME:
29684 return typename_type;
29685
29686 default:
29687 return none_type;
29688 }
29689 }
29690
29691 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29692
29693 static void
29694 cp_parser_check_class_key (enum tag_types class_key, tree type)
29695 {
29696 if (type == error_mark_node)
29697 return;
29698 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29699 {
29700 if (permerror (input_location, "%qs tag used in naming %q#T",
29701 class_key == union_type ? "union"
29702 : class_key == record_type ? "struct" : "class",
29703 type))
29704 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29705 "%q#T was previously declared here", type);
29706 }
29707 }
29708
29709 /* Issue an error message if DECL is redeclared with different
29710 access than its original declaration [class.access.spec/3].
29711 This applies to nested classes, nested class templates and
29712 enumerations [class.mem/1]. */
29713
29714 static void
29715 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29716 {
29717 if (!decl
29718 || (!CLASS_TYPE_P (TREE_TYPE (decl))
29719 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29720 return;
29721
29722 if ((TREE_PRIVATE (decl)
29723 != (current_access_specifier == access_private_node))
29724 || (TREE_PROTECTED (decl)
29725 != (current_access_specifier == access_protected_node)))
29726 error_at (location, "%qD redeclared with different access", decl);
29727 }
29728
29729 /* Look for the `template' keyword, as a syntactic disambiguator.
29730 Return TRUE iff it is present, in which case it will be
29731 consumed. */
29732
29733 static bool
29734 cp_parser_optional_template_keyword (cp_parser *parser)
29735 {
29736 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29737 {
29738 /* In C++98 the `template' keyword can only be used within templates;
29739 outside templates the parser can always figure out what is a
29740 template and what is not. In C++11, per the resolution of DR 468,
29741 `template' is allowed in cases where it is not strictly necessary. */
29742 if (!processing_template_decl
29743 && pedantic && cxx_dialect == cxx98)
29744 {
29745 cp_token *token = cp_lexer_peek_token (parser->lexer);
29746 pedwarn (token->location, OPT_Wpedantic,
29747 "in C++98 %<template%> (as a disambiguator) is only "
29748 "allowed within templates");
29749 /* If this part of the token stream is rescanned, the same
29750 error message would be generated. So, we purge the token
29751 from the stream. */
29752 cp_lexer_purge_token (parser->lexer);
29753 return false;
29754 }
29755 else
29756 {
29757 /* Consume the `template' keyword. */
29758 cp_lexer_consume_token (parser->lexer);
29759 return true;
29760 }
29761 }
29762 return false;
29763 }
29764
29765 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29766 set PARSER->SCOPE, and perform other related actions. */
29767
29768 static void
29769 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29770 {
29771 struct tree_check *check_value;
29772
29773 /* Get the stored value. */
29774 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
29775 /* Set the scope from the stored value. */
29776 parser->scope = saved_checks_value (check_value);
29777 parser->qualifying_scope = check_value->qualifying_scope;
29778 parser->object_scope = NULL_TREE;
29779 }
29780
29781 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29782 encounter the end of a block before what we were looking for. */
29783
29784 static bool
29785 cp_parser_cache_group (cp_parser *parser,
29786 enum cpp_ttype end,
29787 unsigned depth)
29788 {
29789 while (true)
29790 {
29791 cp_token *token = cp_lexer_peek_token (parser->lexer);
29792
29793 /* Abort a parenthesized expression if we encounter a semicolon. */
29794 if ((end == CPP_CLOSE_PAREN || depth == 0)
29795 && token->type == CPP_SEMICOLON)
29796 return true;
29797 /* If we've reached the end of the file, stop. */
29798 if (token->type == CPP_EOF
29799 || (end != CPP_PRAGMA_EOL
29800 && token->type == CPP_PRAGMA_EOL))
29801 return true;
29802 if (token->type == CPP_CLOSE_BRACE && depth == 0)
29803 /* We've hit the end of an enclosing block, so there's been some
29804 kind of syntax error. */
29805 return true;
29806
29807 /* Consume the token. */
29808 cp_lexer_consume_token (parser->lexer);
29809 /* See if it starts a new group. */
29810 if (token->type == CPP_OPEN_BRACE)
29811 {
29812 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29813 /* In theory this should probably check end == '}', but
29814 cp_parser_save_member_function_body needs it to exit
29815 after either '}' or ')' when called with ')'. */
29816 if (depth == 0)
29817 return false;
29818 }
29819 else if (token->type == CPP_OPEN_PAREN)
29820 {
29821 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29822 if (depth == 0 && end == CPP_CLOSE_PAREN)
29823 return false;
29824 }
29825 else if (token->type == CPP_PRAGMA)
29826 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29827 else if (token->type == end)
29828 return false;
29829 }
29830 }
29831
29832 /* Like above, for caching a default argument or NSDMI. Both of these are
29833 terminated by a non-nested comma, but it can be unclear whether or not a
29834 comma is nested in a template argument list unless we do more parsing.
29835 In order to handle this ambiguity, when we encounter a ',' after a '<'
29836 we try to parse what follows as a parameter-declaration-list (in the
29837 case of a default argument) or a member-declarator (in the case of an
29838 NSDMI). If that succeeds, then we stop caching. */
29839
29840 static tree
29841 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29842 {
29843 unsigned depth = 0;
29844 int maybe_template_id = 0;
29845 cp_token *first_token;
29846 cp_token *token;
29847 tree default_argument;
29848
29849 /* Add tokens until we have processed the entire default
29850 argument. We add the range [first_token, token). */
29851 first_token = cp_lexer_peek_token (parser->lexer);
29852 if (first_token->type == CPP_OPEN_BRACE)
29853 {
29854 /* For list-initialization, this is straightforward. */
29855 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29856 token = cp_lexer_peek_token (parser->lexer);
29857 }
29858 else while (true)
29859 {
29860 bool done = false;
29861
29862 /* Peek at the next token. */
29863 token = cp_lexer_peek_token (parser->lexer);
29864 /* What we do depends on what token we have. */
29865 switch (token->type)
29866 {
29867 /* In valid code, a default argument must be
29868 immediately followed by a `,' `)', or `...'. */
29869 case CPP_COMMA:
29870 if (depth == 0 && maybe_template_id)
29871 {
29872 /* If we've seen a '<', we might be in a
29873 template-argument-list. Until Core issue 325 is
29874 resolved, we don't know how this situation ought
29875 to be handled, so try to DTRT. We check whether
29876 what comes after the comma is a valid parameter
29877 declaration list. If it is, then the comma ends
29878 the default argument; otherwise the default
29879 argument continues. */
29880 bool error = false;
29881 cp_token *peek;
29882
29883 /* Set ITALP so cp_parser_parameter_declaration_list
29884 doesn't decide to commit to this parse. */
29885 bool saved_italp = parser->in_template_argument_list_p;
29886 parser->in_template_argument_list_p = true;
29887
29888 cp_parser_parse_tentatively (parser);
29889
29890 if (nsdmi)
29891 {
29892 /* Parse declarators until we reach a non-comma or
29893 somthing that cannot be an initializer.
29894 Just checking whether we're looking at a single
29895 declarator is insufficient. Consider:
29896 int var = tuple<T,U>::x;
29897 The template parameter 'U' looks exactly like a
29898 declarator. */
29899 do
29900 {
29901 int ctor_dtor_or_conv_p;
29902 cp_lexer_consume_token (parser->lexer);
29903 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29904 CP_PARSER_FLAGS_NONE,
29905 &ctor_dtor_or_conv_p,
29906 /*parenthesized_p=*/NULL,
29907 /*member_p=*/true,
29908 /*friend_p=*/false,
29909 /*static_p=*/false);
29910 peek = cp_lexer_peek_token (parser->lexer);
29911 if (cp_parser_error_occurred (parser))
29912 break;
29913 }
29914 while (peek->type == CPP_COMMA);
29915 /* If we met an '=' or ';' then the original comma
29916 was the end of the NSDMI. Otherwise assume
29917 we're still in the NSDMI. */
29918 error = (peek->type != CPP_EQ
29919 && peek->type != CPP_SEMICOLON);
29920 }
29921 else
29922 {
29923 cp_lexer_consume_token (parser->lexer);
29924 begin_scope (sk_function_parms, NULL_TREE);
29925 tree t = cp_parser_parameter_declaration_list
29926 (parser, CP_PARSER_FLAGS_NONE);
29927 if (t == error_mark_node)
29928 error = true;
29929 pop_bindings_and_leave_scope ();
29930 }
29931 if (!cp_parser_error_occurred (parser) && !error)
29932 done = true;
29933 cp_parser_abort_tentative_parse (parser);
29934
29935 parser->in_template_argument_list_p = saved_italp;
29936 break;
29937 }
29938 /* FALLTHRU */
29939 case CPP_CLOSE_PAREN:
29940 case CPP_ELLIPSIS:
29941 /* If we run into a non-nested `;', `}', or `]',
29942 then the code is invalid -- but the default
29943 argument is certainly over. */
29944 case CPP_SEMICOLON:
29945 case CPP_CLOSE_BRACE:
29946 case CPP_CLOSE_SQUARE:
29947 if (depth == 0
29948 /* Handle correctly int n = sizeof ... ( p ); */
29949 && token->type != CPP_ELLIPSIS)
29950 done = true;
29951 /* Update DEPTH, if necessary. */
29952 else if (token->type == CPP_CLOSE_PAREN
29953 || token->type == CPP_CLOSE_BRACE
29954 || token->type == CPP_CLOSE_SQUARE)
29955 --depth;
29956 break;
29957
29958 case CPP_OPEN_PAREN:
29959 case CPP_OPEN_SQUARE:
29960 case CPP_OPEN_BRACE:
29961 ++depth;
29962 break;
29963
29964 case CPP_LESS:
29965 if (depth == 0)
29966 /* This might be the comparison operator, or it might
29967 start a template argument list. */
29968 ++maybe_template_id;
29969 break;
29970
29971 case CPP_RSHIFT:
29972 if (cxx_dialect == cxx98)
29973 break;
29974 /* Fall through for C++0x, which treats the `>>'
29975 operator like two `>' tokens in certain
29976 cases. */
29977 gcc_fallthrough ();
29978
29979 case CPP_GREATER:
29980 if (depth == 0)
29981 {
29982 /* This might be an operator, or it might close a
29983 template argument list. But if a previous '<'
29984 started a template argument list, this will have
29985 closed it, so we can't be in one anymore. */
29986 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29987 if (maybe_template_id < 0)
29988 maybe_template_id = 0;
29989 }
29990 break;
29991
29992 /* If we run out of tokens, issue an error message. */
29993 case CPP_EOF:
29994 case CPP_PRAGMA_EOL:
29995 error_at (token->location, "file ends in default argument");
29996 return error_mark_node;
29997
29998 case CPP_NAME:
29999 case CPP_SCOPE:
30000 /* In these cases, we should look for template-ids.
30001 For example, if the default argument is
30002 `X<int, double>()', we need to do name lookup to
30003 figure out whether or not `X' is a template; if
30004 so, the `,' does not end the default argument.
30005
30006 That is not yet done. */
30007 break;
30008
30009 default:
30010 break;
30011 }
30012
30013 /* If we've reached the end, stop. */
30014 if (done)
30015 break;
30016
30017 /* Add the token to the token block. */
30018 token = cp_lexer_consume_token (parser->lexer);
30019 }
30020
30021 /* Create a DEFAULT_ARG to represent the unparsed default
30022 argument. */
30023 default_argument = make_node (DEFAULT_ARG);
30024 DEFARG_TOKENS (default_argument)
30025 = cp_token_cache_new (first_token, token);
30026 DEFARG_INSTANTIATIONS (default_argument) = NULL;
30027
30028 return default_argument;
30029 }
30030
30031 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
30032
30033 location_t
30034 defarg_location (tree default_argument)
30035 {
30036 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
30037 location_t start = tokens->first->location;
30038 location_t end = tokens->last->location;
30039 return make_location (start, start, end);
30040 }
30041
30042 /* Begin parsing tentatively. We always save tokens while parsing
30043 tentatively so that if the tentative parsing fails we can restore the
30044 tokens. */
30045
30046 static void
30047 cp_parser_parse_tentatively (cp_parser* parser)
30048 {
30049 /* Enter a new parsing context. */
30050 parser->context = cp_parser_context_new (parser->context);
30051 /* Begin saving tokens. */
30052 cp_lexer_save_tokens (parser->lexer);
30053 /* In order to avoid repetitive access control error messages,
30054 access checks are queued up until we are no longer parsing
30055 tentatively. */
30056 push_deferring_access_checks (dk_deferred);
30057 }
30058
30059 /* Commit to the currently active tentative parse. */
30060
30061 static void
30062 cp_parser_commit_to_tentative_parse (cp_parser* parser)
30063 {
30064 cp_parser_context *context;
30065 cp_lexer *lexer;
30066
30067 /* Mark all of the levels as committed. */
30068 lexer = parser->lexer;
30069 for (context = parser->context; context->next; context = context->next)
30070 {
30071 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
30072 break;
30073 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
30074 while (!cp_lexer_saving_tokens (lexer))
30075 lexer = lexer->next;
30076 cp_lexer_commit_tokens (lexer);
30077 }
30078 }
30079
30080 /* Commit to the topmost currently active tentative parse.
30081
30082 Note that this function shouldn't be called when there are
30083 irreversible side-effects while in a tentative state. For
30084 example, we shouldn't create a permanent entry in the symbol
30085 table, or issue an error message that might not apply if the
30086 tentative parse is aborted. */
30087
30088 static void
30089 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
30090 {
30091 cp_parser_context *context = parser->context;
30092 cp_lexer *lexer = parser->lexer;
30093
30094 if (context)
30095 {
30096 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
30097 return;
30098 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
30099
30100 while (!cp_lexer_saving_tokens (lexer))
30101 lexer = lexer->next;
30102 cp_lexer_commit_tokens (lexer);
30103 }
30104 }
30105
30106 /* Abort the currently active tentative parse. All consumed tokens
30107 will be rolled back, and no diagnostics will be issued. */
30108
30109 static void
30110 cp_parser_abort_tentative_parse (cp_parser* parser)
30111 {
30112 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
30113 || errorcount > 0);
30114 cp_parser_simulate_error (parser);
30115 /* Now, pretend that we want to see if the construct was
30116 successfully parsed. */
30117 cp_parser_parse_definitely (parser);
30118 }
30119
30120 /* Stop parsing tentatively. If a parse error has occurred, restore the
30121 token stream. Otherwise, commit to the tokens we have consumed.
30122 Returns true if no error occurred; false otherwise. */
30123
30124 static bool
30125 cp_parser_parse_definitely (cp_parser* parser)
30126 {
30127 bool error_occurred;
30128 cp_parser_context *context;
30129
30130 /* Remember whether or not an error occurred, since we are about to
30131 destroy that information. */
30132 error_occurred = cp_parser_error_occurred (parser);
30133 /* Remove the topmost context from the stack. */
30134 context = parser->context;
30135 parser->context = context->next;
30136 /* If no parse errors occurred, commit to the tentative parse. */
30137 if (!error_occurred)
30138 {
30139 /* Commit to the tokens read tentatively, unless that was
30140 already done. */
30141 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
30142 cp_lexer_commit_tokens (parser->lexer);
30143
30144 pop_to_parent_deferring_access_checks ();
30145 }
30146 /* Otherwise, if errors occurred, roll back our state so that things
30147 are just as they were before we began the tentative parse. */
30148 else
30149 {
30150 cp_lexer_rollback_tokens (parser->lexer);
30151 pop_deferring_access_checks ();
30152 }
30153 /* Add the context to the front of the free list. */
30154 context->next = cp_parser_context_free_list;
30155 cp_parser_context_free_list = context;
30156
30157 return !error_occurred;
30158 }
30159
30160 /* Returns true if we are parsing tentatively and are not committed to
30161 this tentative parse. */
30162
30163 static bool
30164 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
30165 {
30166 return (cp_parser_parsing_tentatively (parser)
30167 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
30168 }
30169
30170 /* Returns nonzero iff an error has occurred during the most recent
30171 tentative parse. */
30172
30173 static bool
30174 cp_parser_error_occurred (cp_parser* parser)
30175 {
30176 return (cp_parser_parsing_tentatively (parser)
30177 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
30178 }
30179
30180 /* Returns nonzero if GNU extensions are allowed. */
30181
30182 static bool
30183 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
30184 {
30185 return parser->allow_gnu_extensions_p;
30186 }
30187 \f
30188 /* Objective-C++ Productions */
30189
30190
30191 /* Parse an Objective-C expression, which feeds into a primary-expression
30192 above.
30193
30194 objc-expression:
30195 objc-message-expression
30196 objc-string-literal
30197 objc-encode-expression
30198 objc-protocol-expression
30199 objc-selector-expression
30200
30201 Returns a tree representation of the expression. */
30202
30203 static cp_expr
30204 cp_parser_objc_expression (cp_parser* parser)
30205 {
30206 /* Try to figure out what kind of declaration is present. */
30207 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30208
30209 switch (kwd->type)
30210 {
30211 case CPP_OPEN_SQUARE:
30212 return cp_parser_objc_message_expression (parser);
30213
30214 case CPP_OBJC_STRING:
30215 kwd = cp_lexer_consume_token (parser->lexer);
30216 return objc_build_string_object (kwd->u.value);
30217
30218 case CPP_KEYWORD:
30219 switch (kwd->keyword)
30220 {
30221 case RID_AT_ENCODE:
30222 return cp_parser_objc_encode_expression (parser);
30223
30224 case RID_AT_PROTOCOL:
30225 return cp_parser_objc_protocol_expression (parser);
30226
30227 case RID_AT_SELECTOR:
30228 return cp_parser_objc_selector_expression (parser);
30229
30230 default:
30231 break;
30232 }
30233 /* FALLTHRU */
30234 default:
30235 error_at (kwd->location,
30236 "misplaced %<@%D%> Objective-C++ construct",
30237 kwd->u.value);
30238 cp_parser_skip_to_end_of_block_or_statement (parser);
30239 }
30240
30241 return error_mark_node;
30242 }
30243
30244 /* Parse an Objective-C message expression.
30245
30246 objc-message-expression:
30247 [ objc-message-receiver objc-message-args ]
30248
30249 Returns a representation of an Objective-C message. */
30250
30251 static tree
30252 cp_parser_objc_message_expression (cp_parser* parser)
30253 {
30254 tree receiver, messageargs;
30255
30256 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30257 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
30258 receiver = cp_parser_objc_message_receiver (parser);
30259 messageargs = cp_parser_objc_message_args (parser);
30260 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
30261 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
30262
30263 tree result = objc_build_message_expr (receiver, messageargs);
30264
30265 /* Construct a location e.g.
30266 [self func1:5]
30267 ^~~~~~~~~~~~~~
30268 ranging from the '[' to the ']', with the caret at the start. */
30269 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
30270 protected_set_expr_location (result, combined_loc);
30271
30272 return result;
30273 }
30274
30275 /* Parse an objc-message-receiver.
30276
30277 objc-message-receiver:
30278 expression
30279 simple-type-specifier
30280
30281 Returns a representation of the type or expression. */
30282
30283 static tree
30284 cp_parser_objc_message_receiver (cp_parser* parser)
30285 {
30286 tree rcv;
30287
30288 /* An Objective-C message receiver may be either (1) a type
30289 or (2) an expression. */
30290 cp_parser_parse_tentatively (parser);
30291 rcv = cp_parser_expression (parser);
30292
30293 /* If that worked out, fine. */
30294 if (cp_parser_parse_definitely (parser))
30295 return rcv;
30296
30297 cp_parser_parse_tentatively (parser);
30298 rcv = cp_parser_simple_type_specifier (parser,
30299 /*decl_specs=*/NULL,
30300 CP_PARSER_FLAGS_NONE);
30301
30302 if (cp_parser_parse_definitely (parser))
30303 return objc_get_class_reference (rcv);
30304
30305 cp_parser_error (parser, "objective-c++ message receiver expected");
30306 return error_mark_node;
30307 }
30308
30309 /* Parse the arguments and selectors comprising an Objective-C message.
30310
30311 objc-message-args:
30312 objc-selector
30313 objc-selector-args
30314 objc-selector-args , objc-comma-args
30315
30316 objc-selector-args:
30317 objc-selector [opt] : assignment-expression
30318 objc-selector-args objc-selector [opt] : assignment-expression
30319
30320 objc-comma-args:
30321 assignment-expression
30322 objc-comma-args , assignment-expression
30323
30324 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
30325 selector arguments and TREE_VALUE containing a list of comma
30326 arguments. */
30327
30328 static tree
30329 cp_parser_objc_message_args (cp_parser* parser)
30330 {
30331 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
30332 bool maybe_unary_selector_p = true;
30333 cp_token *token = cp_lexer_peek_token (parser->lexer);
30334
30335 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30336 {
30337 tree selector = NULL_TREE, arg;
30338
30339 if (token->type != CPP_COLON)
30340 selector = cp_parser_objc_selector (parser);
30341
30342 /* Detect if we have a unary selector. */
30343 if (maybe_unary_selector_p
30344 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30345 return build_tree_list (selector, NULL_TREE);
30346
30347 maybe_unary_selector_p = false;
30348 cp_parser_require (parser, CPP_COLON, RT_COLON);
30349 arg = cp_parser_assignment_expression (parser);
30350
30351 sel_args
30352 = chainon (sel_args,
30353 build_tree_list (selector, arg));
30354
30355 token = cp_lexer_peek_token (parser->lexer);
30356 }
30357
30358 /* Handle non-selector arguments, if any. */
30359 while (token->type == CPP_COMMA)
30360 {
30361 tree arg;
30362
30363 cp_lexer_consume_token (parser->lexer);
30364 arg = cp_parser_assignment_expression (parser);
30365
30366 addl_args
30367 = chainon (addl_args,
30368 build_tree_list (NULL_TREE, arg));
30369
30370 token = cp_lexer_peek_token (parser->lexer);
30371 }
30372
30373 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
30374 {
30375 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
30376 return build_tree_list (error_mark_node, error_mark_node);
30377 }
30378
30379 return build_tree_list (sel_args, addl_args);
30380 }
30381
30382 /* Parse an Objective-C encode expression.
30383
30384 objc-encode-expression:
30385 @encode objc-typename
30386
30387 Returns an encoded representation of the type argument. */
30388
30389 static cp_expr
30390 cp_parser_objc_encode_expression (cp_parser* parser)
30391 {
30392 tree type;
30393 cp_token *token;
30394 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30395
30396 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
30397 matching_parens parens;
30398 parens.require_open (parser);
30399 token = cp_lexer_peek_token (parser->lexer);
30400 type = complete_type (cp_parser_type_id (parser));
30401 parens.require_close (parser);
30402
30403 if (!type)
30404 {
30405 error_at (token->location,
30406 "%<@encode%> must specify a type as an argument");
30407 return error_mark_node;
30408 }
30409
30410 /* This happens if we find @encode(T) (where T is a template
30411 typename or something dependent on a template typename) when
30412 parsing a template. In that case, we can't compile it
30413 immediately, but we rather create an AT_ENCODE_EXPR which will
30414 need to be instantiated when the template is used.
30415 */
30416 if (dependent_type_p (type))
30417 {
30418 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
30419 TREE_READONLY (value) = 1;
30420 return value;
30421 }
30422
30423
30424 /* Build a location of the form:
30425 @encode(int)
30426 ^~~~~~~~~~~~
30427 with caret==start at the @ token, finishing at the close paren. */
30428 location_t combined_loc
30429 = make_location (start_loc, start_loc,
30430 cp_lexer_previous_token (parser->lexer)->location);
30431
30432 return cp_expr (objc_build_encode_expr (type), combined_loc);
30433 }
30434
30435 /* Parse an Objective-C @defs expression. */
30436
30437 static tree
30438 cp_parser_objc_defs_expression (cp_parser *parser)
30439 {
30440 tree name;
30441
30442 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
30443 matching_parens parens;
30444 parens.require_open (parser);
30445 name = cp_parser_identifier (parser);
30446 parens.require_close (parser);
30447
30448 return objc_get_class_ivars (name);
30449 }
30450
30451 /* Parse an Objective-C protocol expression.
30452
30453 objc-protocol-expression:
30454 @protocol ( identifier )
30455
30456 Returns a representation of the protocol expression. */
30457
30458 static tree
30459 cp_parser_objc_protocol_expression (cp_parser* parser)
30460 {
30461 tree proto;
30462 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30463
30464 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30465 matching_parens parens;
30466 parens.require_open (parser);
30467 proto = cp_parser_identifier (parser);
30468 parens.require_close (parser);
30469
30470 /* Build a location of the form:
30471 @protocol(prot)
30472 ^~~~~~~~~~~~~~~
30473 with caret==start at the @ token, finishing at the close paren. */
30474 location_t combined_loc
30475 = make_location (start_loc, start_loc,
30476 cp_lexer_previous_token (parser->lexer)->location);
30477 tree result = objc_build_protocol_expr (proto);
30478 protected_set_expr_location (result, combined_loc);
30479 return result;
30480 }
30481
30482 /* Parse an Objective-C selector expression.
30483
30484 objc-selector-expression:
30485 @selector ( objc-method-signature )
30486
30487 objc-method-signature:
30488 objc-selector
30489 objc-selector-seq
30490
30491 objc-selector-seq:
30492 objc-selector :
30493 objc-selector-seq objc-selector :
30494
30495 Returns a representation of the method selector. */
30496
30497 static tree
30498 cp_parser_objc_selector_expression (cp_parser* parser)
30499 {
30500 tree sel_seq = NULL_TREE;
30501 bool maybe_unary_selector_p = true;
30502 cp_token *token;
30503 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30504
30505 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
30506 matching_parens parens;
30507 parens.require_open (parser);
30508 token = cp_lexer_peek_token (parser->lexer);
30509
30510 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
30511 || token->type == CPP_SCOPE)
30512 {
30513 tree selector = NULL_TREE;
30514
30515 if (token->type != CPP_COLON
30516 || token->type == CPP_SCOPE)
30517 selector = cp_parser_objc_selector (parser);
30518
30519 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
30520 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
30521 {
30522 /* Detect if we have a unary selector. */
30523 if (maybe_unary_selector_p)
30524 {
30525 sel_seq = selector;
30526 goto finish_selector;
30527 }
30528 else
30529 {
30530 cp_parser_error (parser, "expected %<:%>");
30531 }
30532 }
30533 maybe_unary_selector_p = false;
30534 token = cp_lexer_consume_token (parser->lexer);
30535
30536 if (token->type == CPP_SCOPE)
30537 {
30538 sel_seq
30539 = chainon (sel_seq,
30540 build_tree_list (selector, NULL_TREE));
30541 sel_seq
30542 = chainon (sel_seq,
30543 build_tree_list (NULL_TREE, NULL_TREE));
30544 }
30545 else
30546 sel_seq
30547 = chainon (sel_seq,
30548 build_tree_list (selector, NULL_TREE));
30549
30550 token = cp_lexer_peek_token (parser->lexer);
30551 }
30552
30553 finish_selector:
30554 parens.require_close (parser);
30555
30556
30557 /* Build a location of the form:
30558 @selector(func)
30559 ^~~~~~~~~~~~~~~
30560 with caret==start at the @ token, finishing at the close paren. */
30561 location_t combined_loc
30562 = make_location (loc, loc,
30563 cp_lexer_previous_token (parser->lexer)->location);
30564 tree result = objc_build_selector_expr (combined_loc, sel_seq);
30565 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30566 protected_set_expr_location (result, combined_loc);
30567 return result;
30568 }
30569
30570 /* Parse a list of identifiers.
30571
30572 objc-identifier-list:
30573 identifier
30574 objc-identifier-list , identifier
30575
30576 Returns a TREE_LIST of identifier nodes. */
30577
30578 static tree
30579 cp_parser_objc_identifier_list (cp_parser* parser)
30580 {
30581 tree identifier;
30582 tree list;
30583 cp_token *sep;
30584
30585 identifier = cp_parser_identifier (parser);
30586 if (identifier == error_mark_node)
30587 return error_mark_node;
30588
30589 list = build_tree_list (NULL_TREE, identifier);
30590 sep = cp_lexer_peek_token (parser->lexer);
30591
30592 while (sep->type == CPP_COMMA)
30593 {
30594 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30595 identifier = cp_parser_identifier (parser);
30596 if (identifier == error_mark_node)
30597 return list;
30598
30599 list = chainon (list, build_tree_list (NULL_TREE,
30600 identifier));
30601 sep = cp_lexer_peek_token (parser->lexer);
30602 }
30603
30604 return list;
30605 }
30606
30607 /* Parse an Objective-C alias declaration.
30608
30609 objc-alias-declaration:
30610 @compatibility_alias identifier identifier ;
30611
30612 This function registers the alias mapping with the Objective-C front end.
30613 It returns nothing. */
30614
30615 static void
30616 cp_parser_objc_alias_declaration (cp_parser* parser)
30617 {
30618 tree alias, orig;
30619
30620 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
30621 alias = cp_parser_identifier (parser);
30622 orig = cp_parser_identifier (parser);
30623 objc_declare_alias (alias, orig);
30624 cp_parser_consume_semicolon_at_end_of_statement (parser);
30625 }
30626
30627 /* Parse an Objective-C class forward-declaration.
30628
30629 objc-class-declaration:
30630 @class objc-identifier-list ;
30631
30632 The function registers the forward declarations with the Objective-C
30633 front end. It returns nothing. */
30634
30635 static void
30636 cp_parser_objc_class_declaration (cp_parser* parser)
30637 {
30638 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
30639 while (true)
30640 {
30641 tree id;
30642
30643 id = cp_parser_identifier (parser);
30644 if (id == error_mark_node)
30645 break;
30646
30647 objc_declare_class (id);
30648
30649 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30650 cp_lexer_consume_token (parser->lexer);
30651 else
30652 break;
30653 }
30654 cp_parser_consume_semicolon_at_end_of_statement (parser);
30655 }
30656
30657 /* Parse a list of Objective-C protocol references.
30658
30659 objc-protocol-refs-opt:
30660 objc-protocol-refs [opt]
30661
30662 objc-protocol-refs:
30663 < objc-identifier-list >
30664
30665 Returns a TREE_LIST of identifiers, if any. */
30666
30667 static tree
30668 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
30669 {
30670 tree protorefs = NULL_TREE;
30671
30672 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
30673 {
30674 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
30675 protorefs = cp_parser_objc_identifier_list (parser);
30676 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
30677 }
30678
30679 return protorefs;
30680 }
30681
30682 /* Parse a Objective-C visibility specification. */
30683
30684 static void
30685 cp_parser_objc_visibility_spec (cp_parser* parser)
30686 {
30687 cp_token *vis = cp_lexer_peek_token (parser->lexer);
30688
30689 switch (vis->keyword)
30690 {
30691 case RID_AT_PRIVATE:
30692 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30693 break;
30694 case RID_AT_PROTECTED:
30695 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30696 break;
30697 case RID_AT_PUBLIC:
30698 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30699 break;
30700 case RID_AT_PACKAGE:
30701 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30702 break;
30703 default:
30704 return;
30705 }
30706
30707 /* Eat '@private'/'@protected'/'@public'. */
30708 cp_lexer_consume_token (parser->lexer);
30709 }
30710
30711 /* Parse an Objective-C method type. Return 'true' if it is a class
30712 (+) method, and 'false' if it is an instance (-) method. */
30713
30714 static inline bool
30715 cp_parser_objc_method_type (cp_parser* parser)
30716 {
30717 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30718 return true;
30719 else
30720 return false;
30721 }
30722
30723 /* Parse an Objective-C protocol qualifier. */
30724
30725 static tree
30726 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30727 {
30728 tree quals = NULL_TREE, node;
30729 cp_token *token = cp_lexer_peek_token (parser->lexer);
30730
30731 node = token->u.value;
30732
30733 while (node && identifier_p (node)
30734 && (node == ridpointers [(int) RID_IN]
30735 || node == ridpointers [(int) RID_OUT]
30736 || node == ridpointers [(int) RID_INOUT]
30737 || node == ridpointers [(int) RID_BYCOPY]
30738 || node == ridpointers [(int) RID_BYREF]
30739 || node == ridpointers [(int) RID_ONEWAY]))
30740 {
30741 quals = tree_cons (NULL_TREE, node, quals);
30742 cp_lexer_consume_token (parser->lexer);
30743 token = cp_lexer_peek_token (parser->lexer);
30744 node = token->u.value;
30745 }
30746
30747 return quals;
30748 }
30749
30750 /* Parse an Objective-C typename. */
30751
30752 static tree
30753 cp_parser_objc_typename (cp_parser* parser)
30754 {
30755 tree type_name = NULL_TREE;
30756
30757 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30758 {
30759 tree proto_quals, cp_type = NULL_TREE;
30760
30761 matching_parens parens;
30762 parens.consume_open (parser); /* Eat '('. */
30763 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30764
30765 /* An ObjC type name may consist of just protocol qualifiers, in which
30766 case the type shall default to 'id'. */
30767 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30768 {
30769 cp_type = cp_parser_type_id (parser);
30770
30771 /* If the type could not be parsed, an error has already
30772 been produced. For error recovery, behave as if it had
30773 not been specified, which will use the default type
30774 'id'. */
30775 if (cp_type == error_mark_node)
30776 {
30777 cp_type = NULL_TREE;
30778 /* We need to skip to the closing parenthesis as
30779 cp_parser_type_id() does not seem to do it for
30780 us. */
30781 cp_parser_skip_to_closing_parenthesis (parser,
30782 /*recovering=*/true,
30783 /*or_comma=*/false,
30784 /*consume_paren=*/false);
30785 }
30786 }
30787
30788 parens.require_close (parser);
30789 type_name = build_tree_list (proto_quals, cp_type);
30790 }
30791
30792 return type_name;
30793 }
30794
30795 /* Check to see if TYPE refers to an Objective-C selector name. */
30796
30797 static bool
30798 cp_parser_objc_selector_p (enum cpp_ttype type)
30799 {
30800 return (type == CPP_NAME || type == CPP_KEYWORD
30801 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
30802 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
30803 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
30804 || type == CPP_XOR || type == CPP_XOR_EQ);
30805 }
30806
30807 /* Parse an Objective-C selector. */
30808
30809 static tree
30810 cp_parser_objc_selector (cp_parser* parser)
30811 {
30812 cp_token *token = cp_lexer_consume_token (parser->lexer);
30813
30814 if (!cp_parser_objc_selector_p (token->type))
30815 {
30816 error_at (token->location, "invalid Objective-C++ selector name");
30817 return error_mark_node;
30818 }
30819
30820 /* C++ operator names are allowed to appear in ObjC selectors. */
30821 switch (token->type)
30822 {
30823 case CPP_AND_AND: return get_identifier ("and");
30824 case CPP_AND_EQ: return get_identifier ("and_eq");
30825 case CPP_AND: return get_identifier ("bitand");
30826 case CPP_OR: return get_identifier ("bitor");
30827 case CPP_COMPL: return get_identifier ("compl");
30828 case CPP_NOT: return get_identifier ("not");
30829 case CPP_NOT_EQ: return get_identifier ("not_eq");
30830 case CPP_OR_OR: return get_identifier ("or");
30831 case CPP_OR_EQ: return get_identifier ("or_eq");
30832 case CPP_XOR: return get_identifier ("xor");
30833 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30834 default: return token->u.value;
30835 }
30836 }
30837
30838 /* Parse an Objective-C params list. */
30839
30840 static tree
30841 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30842 {
30843 tree params = NULL_TREE;
30844 bool maybe_unary_selector_p = true;
30845 cp_token *token = cp_lexer_peek_token (parser->lexer);
30846
30847 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30848 {
30849 tree selector = NULL_TREE, type_name, identifier;
30850 tree parm_attr = NULL_TREE;
30851
30852 if (token->keyword == RID_ATTRIBUTE)
30853 break;
30854
30855 if (token->type != CPP_COLON)
30856 selector = cp_parser_objc_selector (parser);
30857
30858 /* Detect if we have a unary selector. */
30859 if (maybe_unary_selector_p
30860 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30861 {
30862 params = selector; /* Might be followed by attributes. */
30863 break;
30864 }
30865
30866 maybe_unary_selector_p = false;
30867 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30868 {
30869 /* Something went quite wrong. There should be a colon
30870 here, but there is not. Stop parsing parameters. */
30871 break;
30872 }
30873 type_name = cp_parser_objc_typename (parser);
30874 /* New ObjC allows attributes on parameters too. */
30875 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30876 parm_attr = cp_parser_attributes_opt (parser);
30877 identifier = cp_parser_identifier (parser);
30878
30879 params
30880 = chainon (params,
30881 objc_build_keyword_decl (selector,
30882 type_name,
30883 identifier,
30884 parm_attr));
30885
30886 token = cp_lexer_peek_token (parser->lexer);
30887 }
30888
30889 if (params == NULL_TREE)
30890 {
30891 cp_parser_error (parser, "objective-c++ method declaration is expected");
30892 return error_mark_node;
30893 }
30894
30895 /* We allow tail attributes for the method. */
30896 if (token->keyword == RID_ATTRIBUTE)
30897 {
30898 *attributes = cp_parser_attributes_opt (parser);
30899 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30900 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30901 return params;
30902 cp_parser_error (parser,
30903 "method attributes must be specified at the end");
30904 return error_mark_node;
30905 }
30906
30907 if (params == NULL_TREE)
30908 {
30909 cp_parser_error (parser, "objective-c++ method declaration is expected");
30910 return error_mark_node;
30911 }
30912 return params;
30913 }
30914
30915 /* Parse the non-keyword Objective-C params. */
30916
30917 static tree
30918 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30919 tree* attributes)
30920 {
30921 tree params = make_node (TREE_LIST);
30922 cp_token *token = cp_lexer_peek_token (parser->lexer);
30923 *ellipsisp = false; /* Initially, assume no ellipsis. */
30924
30925 while (token->type == CPP_COMMA)
30926 {
30927 cp_parameter_declarator *parmdecl;
30928 tree parm;
30929
30930 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30931 token = cp_lexer_peek_token (parser->lexer);
30932
30933 if (token->type == CPP_ELLIPSIS)
30934 {
30935 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30936 *ellipsisp = true;
30937 token = cp_lexer_peek_token (parser->lexer);
30938 break;
30939 }
30940
30941 /* TODO: parse attributes for tail parameters. */
30942 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
30943 false, NULL);
30944 parm = grokdeclarator (parmdecl->declarator,
30945 &parmdecl->decl_specifiers,
30946 PARM, /*initialized=*/0,
30947 /*attrlist=*/NULL);
30948
30949 chainon (params, build_tree_list (NULL_TREE, parm));
30950 token = cp_lexer_peek_token (parser->lexer);
30951 }
30952
30953 /* We allow tail attributes for the method. */
30954 if (token->keyword == RID_ATTRIBUTE)
30955 {
30956 if (*attributes == NULL_TREE)
30957 {
30958 *attributes = cp_parser_attributes_opt (parser);
30959 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30960 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30961 return params;
30962 }
30963 else
30964 /* We have an error, but parse the attributes, so that we can
30965 carry on. */
30966 *attributes = cp_parser_attributes_opt (parser);
30967
30968 cp_parser_error (parser,
30969 "method attributes must be specified at the end");
30970 return error_mark_node;
30971 }
30972
30973 return params;
30974 }
30975
30976 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30977
30978 static void
30979 cp_parser_objc_interstitial_code (cp_parser* parser)
30980 {
30981 cp_token *token = cp_lexer_peek_token (parser->lexer);
30982
30983 /* If the next token is `extern' and the following token is a string
30984 literal, then we have a linkage specification. */
30985 if (token->keyword == RID_EXTERN
30986 && cp_parser_is_pure_string_literal
30987 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30988 cp_parser_linkage_specification (parser);
30989 /* Handle #pragma, if any. */
30990 else if (token->type == CPP_PRAGMA)
30991 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30992 /* Allow stray semicolons. */
30993 else if (token->type == CPP_SEMICOLON)
30994 cp_lexer_consume_token (parser->lexer);
30995 /* Mark methods as optional or required, when building protocols. */
30996 else if (token->keyword == RID_AT_OPTIONAL)
30997 {
30998 cp_lexer_consume_token (parser->lexer);
30999 objc_set_method_opt (true);
31000 }
31001 else if (token->keyword == RID_AT_REQUIRED)
31002 {
31003 cp_lexer_consume_token (parser->lexer);
31004 objc_set_method_opt (false);
31005 }
31006 else if (token->keyword == RID_NAMESPACE)
31007 cp_parser_namespace_definition (parser);
31008 /* Other stray characters must generate errors. */
31009 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
31010 {
31011 cp_lexer_consume_token (parser->lexer);
31012 error ("stray %qs between Objective-C++ methods",
31013 token->type == CPP_OPEN_BRACE ? "{" : "}");
31014 }
31015 /* Finally, try to parse a block-declaration, or a function-definition. */
31016 else
31017 cp_parser_block_declaration (parser, /*statement_p=*/false);
31018 }
31019
31020 /* Parse a method signature. */
31021
31022 static tree
31023 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
31024 {
31025 tree rettype, kwdparms, optparms;
31026 bool ellipsis = false;
31027 bool is_class_method;
31028
31029 is_class_method = cp_parser_objc_method_type (parser);
31030 rettype = cp_parser_objc_typename (parser);
31031 *attributes = NULL_TREE;
31032 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
31033 if (kwdparms == error_mark_node)
31034 return error_mark_node;
31035 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
31036 if (optparms == error_mark_node)
31037 return error_mark_node;
31038
31039 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
31040 }
31041
31042 static bool
31043 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
31044 {
31045 tree tattr;
31046 cp_lexer_save_tokens (parser->lexer);
31047 tattr = cp_parser_attributes_opt (parser);
31048 gcc_assert (tattr) ;
31049
31050 /* If the attributes are followed by a method introducer, this is not allowed.
31051 Dump the attributes and flag the situation. */
31052 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
31053 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31054 return true;
31055
31056 /* Otherwise, the attributes introduce some interstitial code, possibly so
31057 rewind to allow that check. */
31058 cp_lexer_rollback_tokens (parser->lexer);
31059 return false;
31060 }
31061
31062 /* Parse an Objective-C method prototype list. */
31063
31064 static void
31065 cp_parser_objc_method_prototype_list (cp_parser* parser)
31066 {
31067 cp_token *token = cp_lexer_peek_token (parser->lexer);
31068
31069 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
31070 {
31071 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
31072 {
31073 tree attributes, sig;
31074 bool is_class_method;
31075 if (token->type == CPP_PLUS)
31076 is_class_method = true;
31077 else
31078 is_class_method = false;
31079 sig = cp_parser_objc_method_signature (parser, &attributes);
31080 if (sig == error_mark_node)
31081 {
31082 cp_parser_skip_to_end_of_block_or_statement (parser);
31083 token = cp_lexer_peek_token (parser->lexer);
31084 continue;
31085 }
31086 objc_add_method_declaration (is_class_method, sig, attributes);
31087 cp_parser_consume_semicolon_at_end_of_statement (parser);
31088 }
31089 else if (token->keyword == RID_AT_PROPERTY)
31090 cp_parser_objc_at_property_declaration (parser);
31091 else if (token->keyword == RID_ATTRIBUTE
31092 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
31093 warning_at (cp_lexer_peek_token (parser->lexer)->location,
31094 OPT_Wattributes,
31095 "prefix attributes are ignored for methods");
31096 else
31097 /* Allow for interspersed non-ObjC++ code. */
31098 cp_parser_objc_interstitial_code (parser);
31099
31100 token = cp_lexer_peek_token (parser->lexer);
31101 }
31102
31103 if (token->type != CPP_EOF)
31104 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31105 else
31106 cp_parser_error (parser, "expected %<@end%>");
31107
31108 objc_finish_interface ();
31109 }
31110
31111 /* Parse an Objective-C method definition list. */
31112
31113 static void
31114 cp_parser_objc_method_definition_list (cp_parser* parser)
31115 {
31116 cp_token *token = cp_lexer_peek_token (parser->lexer);
31117
31118 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
31119 {
31120 tree meth;
31121
31122 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
31123 {
31124 cp_token *ptk;
31125 tree sig, attribute;
31126 bool is_class_method;
31127 if (token->type == CPP_PLUS)
31128 is_class_method = true;
31129 else
31130 is_class_method = false;
31131 push_deferring_access_checks (dk_deferred);
31132 sig = cp_parser_objc_method_signature (parser, &attribute);
31133 if (sig == error_mark_node)
31134 {
31135 cp_parser_skip_to_end_of_block_or_statement (parser);
31136 token = cp_lexer_peek_token (parser->lexer);
31137 continue;
31138 }
31139 objc_start_method_definition (is_class_method, sig, attribute,
31140 NULL_TREE);
31141
31142 /* For historical reasons, we accept an optional semicolon. */
31143 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31144 cp_lexer_consume_token (parser->lexer);
31145
31146 ptk = cp_lexer_peek_token (parser->lexer);
31147 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
31148 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
31149 {
31150 perform_deferred_access_checks (tf_warning_or_error);
31151 stop_deferring_access_checks ();
31152 meth = cp_parser_function_definition_after_declarator (parser,
31153 false);
31154 pop_deferring_access_checks ();
31155 objc_finish_method_definition (meth);
31156 }
31157 }
31158 /* The following case will be removed once @synthesize is
31159 completely implemented. */
31160 else if (token->keyword == RID_AT_PROPERTY)
31161 cp_parser_objc_at_property_declaration (parser);
31162 else if (token->keyword == RID_AT_SYNTHESIZE)
31163 cp_parser_objc_at_synthesize_declaration (parser);
31164 else if (token->keyword == RID_AT_DYNAMIC)
31165 cp_parser_objc_at_dynamic_declaration (parser);
31166 else if (token->keyword == RID_ATTRIBUTE
31167 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
31168 warning_at (token->location, OPT_Wattributes,
31169 "prefix attributes are ignored for methods");
31170 else
31171 /* Allow for interspersed non-ObjC++ code. */
31172 cp_parser_objc_interstitial_code (parser);
31173
31174 token = cp_lexer_peek_token (parser->lexer);
31175 }
31176
31177 if (token->type != CPP_EOF)
31178 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31179 else
31180 cp_parser_error (parser, "expected %<@end%>");
31181
31182 objc_finish_implementation ();
31183 }
31184
31185 /* Parse Objective-C ivars. */
31186
31187 static void
31188 cp_parser_objc_class_ivars (cp_parser* parser)
31189 {
31190 cp_token *token = cp_lexer_peek_token (parser->lexer);
31191
31192 if (token->type != CPP_OPEN_BRACE)
31193 return; /* No ivars specified. */
31194
31195 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
31196 token = cp_lexer_peek_token (parser->lexer);
31197
31198 while (token->type != CPP_CLOSE_BRACE
31199 && token->keyword != RID_AT_END && token->type != CPP_EOF)
31200 {
31201 cp_decl_specifier_seq declspecs;
31202 int decl_class_or_enum_p;
31203 tree prefix_attributes;
31204
31205 cp_parser_objc_visibility_spec (parser);
31206
31207 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31208 break;
31209
31210 cp_parser_decl_specifier_seq (parser,
31211 CP_PARSER_FLAGS_OPTIONAL,
31212 &declspecs,
31213 &decl_class_or_enum_p);
31214
31215 /* auto, register, static, extern, mutable. */
31216 if (declspecs.storage_class != sc_none)
31217 {
31218 cp_parser_error (parser, "invalid type for instance variable");
31219 declspecs.storage_class = sc_none;
31220 }
31221
31222 /* thread_local. */
31223 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31224 {
31225 cp_parser_error (parser, "invalid type for instance variable");
31226 declspecs.locations[ds_thread] = 0;
31227 }
31228
31229 /* typedef. */
31230 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31231 {
31232 cp_parser_error (parser, "invalid type for instance variable");
31233 declspecs.locations[ds_typedef] = 0;
31234 }
31235
31236 prefix_attributes = declspecs.attributes;
31237 declspecs.attributes = NULL_TREE;
31238
31239 /* Keep going until we hit the `;' at the end of the
31240 declaration. */
31241 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31242 {
31243 tree width = NULL_TREE, attributes, first_attribute, decl;
31244 cp_declarator *declarator = NULL;
31245 int ctor_dtor_or_conv_p;
31246
31247 /* Check for a (possibly unnamed) bitfield declaration. */
31248 token = cp_lexer_peek_token (parser->lexer);
31249 if (token->type == CPP_COLON)
31250 goto eat_colon;
31251
31252 if (token->type == CPP_NAME
31253 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
31254 == CPP_COLON))
31255 {
31256 /* Get the name of the bitfield. */
31257 declarator = make_id_declarator (NULL_TREE,
31258 cp_parser_identifier (parser),
31259 sfk_none, token->location);
31260
31261 eat_colon:
31262 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31263 /* Get the width of the bitfield. */
31264 width
31265 = cp_parser_constant_expression (parser);
31266 }
31267 else
31268 {
31269 /* Parse the declarator. */
31270 declarator
31271 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31272 CP_PARSER_FLAGS_NONE,
31273 &ctor_dtor_or_conv_p,
31274 /*parenthesized_p=*/NULL,
31275 /*member_p=*/false,
31276 /*friend_p=*/false,
31277 /*static_p=*/false);
31278 }
31279
31280 /* Look for attributes that apply to the ivar. */
31281 attributes = cp_parser_attributes_opt (parser);
31282 /* Remember which attributes are prefix attributes and
31283 which are not. */
31284 first_attribute = attributes;
31285 /* Combine the attributes. */
31286 attributes = attr_chainon (prefix_attributes, attributes);
31287
31288 if (width)
31289 /* Create the bitfield declaration. */
31290 decl = grokbitfield (declarator, &declspecs,
31291 width, NULL_TREE, attributes);
31292 else
31293 decl = grokfield (declarator, &declspecs,
31294 NULL_TREE, /*init_const_expr_p=*/false,
31295 NULL_TREE, attributes);
31296
31297 /* Add the instance variable. */
31298 if (decl != error_mark_node && decl != NULL_TREE)
31299 objc_add_instance_variable (decl);
31300
31301 /* Reset PREFIX_ATTRIBUTES. */
31302 if (attributes != error_mark_node)
31303 {
31304 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31305 attributes = TREE_CHAIN (attributes);
31306 if (attributes)
31307 TREE_CHAIN (attributes) = NULL_TREE;
31308 }
31309
31310 token = cp_lexer_peek_token (parser->lexer);
31311
31312 if (token->type == CPP_COMMA)
31313 {
31314 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31315 continue;
31316 }
31317 break;
31318 }
31319
31320 cp_parser_consume_semicolon_at_end_of_statement (parser);
31321 token = cp_lexer_peek_token (parser->lexer);
31322 }
31323
31324 if (token->keyword == RID_AT_END)
31325 cp_parser_error (parser, "expected %<}%>");
31326
31327 /* Do not consume the RID_AT_END, so it will be read again as terminating
31328 the @interface of @implementation. */
31329 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
31330 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
31331
31332 /* For historical reasons, we accept an optional semicolon. */
31333 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31334 cp_lexer_consume_token (parser->lexer);
31335 }
31336
31337 /* Parse an Objective-C protocol declaration. */
31338
31339 static void
31340 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
31341 {
31342 tree proto, protorefs;
31343 cp_token *tok;
31344
31345 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
31346 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31347 {
31348 tok = cp_lexer_peek_token (parser->lexer);
31349 error_at (tok->location, "identifier expected after %<@protocol%>");
31350 cp_parser_consume_semicolon_at_end_of_statement (parser);
31351 return;
31352 }
31353
31354 /* See if we have a forward declaration or a definition. */
31355 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
31356
31357 /* Try a forward declaration first. */
31358 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
31359 {
31360 while (true)
31361 {
31362 tree id;
31363
31364 id = cp_parser_identifier (parser);
31365 if (id == error_mark_node)
31366 break;
31367
31368 objc_declare_protocol (id, attributes);
31369
31370 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31371 cp_lexer_consume_token (parser->lexer);
31372 else
31373 break;
31374 }
31375 cp_parser_consume_semicolon_at_end_of_statement (parser);
31376 }
31377
31378 /* Ok, we got a full-fledged definition (or at least should). */
31379 else
31380 {
31381 proto = cp_parser_identifier (parser);
31382 protorefs = cp_parser_objc_protocol_refs_opt (parser);
31383 objc_start_protocol (proto, protorefs, attributes);
31384 cp_parser_objc_method_prototype_list (parser);
31385 }
31386 }
31387
31388 /* Parse an Objective-C superclass or category. */
31389
31390 static void
31391 cp_parser_objc_superclass_or_category (cp_parser *parser,
31392 bool iface_p,
31393 tree *super,
31394 tree *categ, bool *is_class_extension)
31395 {
31396 cp_token *next = cp_lexer_peek_token (parser->lexer);
31397
31398 *super = *categ = NULL_TREE;
31399 *is_class_extension = false;
31400 if (next->type == CPP_COLON)
31401 {
31402 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31403 *super = cp_parser_identifier (parser);
31404 }
31405 else if (next->type == CPP_OPEN_PAREN)
31406 {
31407 matching_parens parens;
31408 parens.consume_open (parser); /* Eat '('. */
31409
31410 /* If there is no category name, and this is an @interface, we
31411 have a class extension. */
31412 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31413 {
31414 *categ = NULL_TREE;
31415 *is_class_extension = true;
31416 }
31417 else
31418 *categ = cp_parser_identifier (parser);
31419
31420 parens.require_close (parser);
31421 }
31422 }
31423
31424 /* Parse an Objective-C class interface. */
31425
31426 static void
31427 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
31428 {
31429 tree name, super, categ, protos;
31430 bool is_class_extension;
31431
31432 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
31433 name = cp_parser_identifier (parser);
31434 if (name == error_mark_node)
31435 {
31436 /* It's hard to recover because even if valid @interface stuff
31437 is to follow, we can't compile it (or validate it) if we
31438 don't even know which class it refers to. Let's assume this
31439 was a stray '@interface' token in the stream and skip it.
31440 */
31441 return;
31442 }
31443 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
31444 &is_class_extension);
31445 protos = cp_parser_objc_protocol_refs_opt (parser);
31446
31447 /* We have either a class or a category on our hands. */
31448 if (categ || is_class_extension)
31449 objc_start_category_interface (name, categ, protos, attributes);
31450 else
31451 {
31452 objc_start_class_interface (name, super, protos, attributes);
31453 /* Handle instance variable declarations, if any. */
31454 cp_parser_objc_class_ivars (parser);
31455 objc_continue_interface ();
31456 }
31457
31458 cp_parser_objc_method_prototype_list (parser);
31459 }
31460
31461 /* Parse an Objective-C class implementation. */
31462
31463 static void
31464 cp_parser_objc_class_implementation (cp_parser* parser)
31465 {
31466 tree name, super, categ;
31467 bool is_class_extension;
31468
31469 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
31470 name = cp_parser_identifier (parser);
31471 if (name == error_mark_node)
31472 {
31473 /* It's hard to recover because even if valid @implementation
31474 stuff is to follow, we can't compile it (or validate it) if
31475 we don't even know which class it refers to. Let's assume
31476 this was a stray '@implementation' token in the stream and
31477 skip it.
31478 */
31479 return;
31480 }
31481 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
31482 &is_class_extension);
31483
31484 /* We have either a class or a category on our hands. */
31485 if (categ)
31486 objc_start_category_implementation (name, categ);
31487 else
31488 {
31489 objc_start_class_implementation (name, super);
31490 /* Handle instance variable declarations, if any. */
31491 cp_parser_objc_class_ivars (parser);
31492 objc_continue_implementation ();
31493 }
31494
31495 cp_parser_objc_method_definition_list (parser);
31496 }
31497
31498 /* Consume the @end token and finish off the implementation. */
31499
31500 static void
31501 cp_parser_objc_end_implementation (cp_parser* parser)
31502 {
31503 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31504 objc_finish_implementation ();
31505 }
31506
31507 /* Parse an Objective-C declaration. */
31508
31509 static void
31510 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
31511 {
31512 /* Try to figure out what kind of declaration is present. */
31513 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31514
31515 if (attributes)
31516 switch (kwd->keyword)
31517 {
31518 case RID_AT_ALIAS:
31519 case RID_AT_CLASS:
31520 case RID_AT_END:
31521 error_at (kwd->location, "attributes may not be specified before"
31522 " the %<@%D%> Objective-C++ keyword",
31523 kwd->u.value);
31524 attributes = NULL;
31525 break;
31526 case RID_AT_IMPLEMENTATION:
31527 warning_at (kwd->location, OPT_Wattributes,
31528 "prefix attributes are ignored before %<@%D%>",
31529 kwd->u.value);
31530 attributes = NULL;
31531 default:
31532 break;
31533 }
31534
31535 switch (kwd->keyword)
31536 {
31537 case RID_AT_ALIAS:
31538 cp_parser_objc_alias_declaration (parser);
31539 break;
31540 case RID_AT_CLASS:
31541 cp_parser_objc_class_declaration (parser);
31542 break;
31543 case RID_AT_PROTOCOL:
31544 cp_parser_objc_protocol_declaration (parser, attributes);
31545 break;
31546 case RID_AT_INTERFACE:
31547 cp_parser_objc_class_interface (parser, attributes);
31548 break;
31549 case RID_AT_IMPLEMENTATION:
31550 cp_parser_objc_class_implementation (parser);
31551 break;
31552 case RID_AT_END:
31553 cp_parser_objc_end_implementation (parser);
31554 break;
31555 default:
31556 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31557 kwd->u.value);
31558 cp_parser_skip_to_end_of_block_or_statement (parser);
31559 }
31560 }
31561
31562 /* Parse an Objective-C try-catch-finally statement.
31563
31564 objc-try-catch-finally-stmt:
31565 @try compound-statement objc-catch-clause-seq [opt]
31566 objc-finally-clause [opt]
31567
31568 objc-catch-clause-seq:
31569 objc-catch-clause objc-catch-clause-seq [opt]
31570
31571 objc-catch-clause:
31572 @catch ( objc-exception-declaration ) compound-statement
31573
31574 objc-finally-clause:
31575 @finally compound-statement
31576
31577 objc-exception-declaration:
31578 parameter-declaration
31579 '...'
31580
31581 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31582
31583 Returns NULL_TREE.
31584
31585 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31586 for C. Keep them in sync. */
31587
31588 static tree
31589 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
31590 {
31591 location_t location;
31592 tree stmt;
31593
31594 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
31595 location = cp_lexer_peek_token (parser->lexer)->location;
31596 objc_maybe_warn_exceptions (location);
31597 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31598 node, lest it get absorbed into the surrounding block. */
31599 stmt = push_stmt_list ();
31600 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31601 objc_begin_try_stmt (location, pop_stmt_list (stmt));
31602
31603 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
31604 {
31605 cp_parameter_declarator *parm;
31606 tree parameter_declaration = error_mark_node;
31607 bool seen_open_paren = false;
31608 matching_parens parens;
31609
31610 cp_lexer_consume_token (parser->lexer);
31611 if (parens.require_open (parser))
31612 seen_open_paren = true;
31613 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31614 {
31615 /* We have "@catch (...)" (where the '...' are literally
31616 what is in the code). Skip the '...'.
31617 parameter_declaration is set to NULL_TREE, and
31618 objc_being_catch_clauses() knows that that means
31619 '...'. */
31620 cp_lexer_consume_token (parser->lexer);
31621 parameter_declaration = NULL_TREE;
31622 }
31623 else
31624 {
31625 /* We have "@catch (NSException *exception)" or something
31626 like that. Parse the parameter declaration. */
31627 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
31628 false, NULL);
31629 if (parm == NULL)
31630 parameter_declaration = error_mark_node;
31631 else
31632 parameter_declaration = grokdeclarator (parm->declarator,
31633 &parm->decl_specifiers,
31634 PARM, /*initialized=*/0,
31635 /*attrlist=*/NULL);
31636 }
31637 if (seen_open_paren)
31638 parens.require_close (parser);
31639 else
31640 {
31641 /* If there was no open parenthesis, we are recovering from
31642 an error, and we are trying to figure out what mistake
31643 the user has made. */
31644
31645 /* If there is an immediate closing parenthesis, the user
31646 probably forgot the opening one (ie, they typed "@catch
31647 NSException *e)". Parse the closing parenthesis and keep
31648 going. */
31649 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31650 cp_lexer_consume_token (parser->lexer);
31651
31652 /* If these is no immediate closing parenthesis, the user
31653 probably doesn't know that parenthesis are required at
31654 all (ie, they typed "@catch NSException *e"). So, just
31655 forget about the closing parenthesis and keep going. */
31656 }
31657 objc_begin_catch_clause (parameter_declaration);
31658 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31659 objc_finish_catch_clause ();
31660 }
31661 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
31662 {
31663 cp_lexer_consume_token (parser->lexer);
31664 location = cp_lexer_peek_token (parser->lexer)->location;
31665 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31666 node, lest it get absorbed into the surrounding block. */
31667 stmt = push_stmt_list ();
31668 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31669 objc_build_finally_clause (location, pop_stmt_list (stmt));
31670 }
31671
31672 return objc_finish_try_stmt ();
31673 }
31674
31675 /* Parse an Objective-C synchronized statement.
31676
31677 objc-synchronized-stmt:
31678 @synchronized ( expression ) compound-statement
31679
31680 Returns NULL_TREE. */
31681
31682 static tree
31683 cp_parser_objc_synchronized_statement (cp_parser *parser)
31684 {
31685 location_t location;
31686 tree lock, stmt;
31687
31688 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
31689
31690 location = cp_lexer_peek_token (parser->lexer)->location;
31691 objc_maybe_warn_exceptions (location);
31692 matching_parens parens;
31693 parens.require_open (parser);
31694 lock = cp_parser_expression (parser);
31695 parens.require_close (parser);
31696
31697 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31698 node, lest it get absorbed into the surrounding block. */
31699 stmt = push_stmt_list ();
31700 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31701
31702 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31703 }
31704
31705 /* Parse an Objective-C throw statement.
31706
31707 objc-throw-stmt:
31708 @throw assignment-expression [opt] ;
31709
31710 Returns a constructed '@throw' statement. */
31711
31712 static tree
31713 cp_parser_objc_throw_statement (cp_parser *parser)
31714 {
31715 tree expr = NULL_TREE;
31716 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31717
31718 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31719
31720 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31721 expr = cp_parser_expression (parser);
31722
31723 cp_parser_consume_semicolon_at_end_of_statement (parser);
31724
31725 return objc_build_throw_stmt (loc, expr);
31726 }
31727
31728 /* Parse an Objective-C statement. */
31729
31730 static tree
31731 cp_parser_objc_statement (cp_parser * parser)
31732 {
31733 /* Try to figure out what kind of declaration is present. */
31734 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31735
31736 switch (kwd->keyword)
31737 {
31738 case RID_AT_TRY:
31739 return cp_parser_objc_try_catch_finally_statement (parser);
31740 case RID_AT_SYNCHRONIZED:
31741 return cp_parser_objc_synchronized_statement (parser);
31742 case RID_AT_THROW:
31743 return cp_parser_objc_throw_statement (parser);
31744 default:
31745 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31746 kwd->u.value);
31747 cp_parser_skip_to_end_of_block_or_statement (parser);
31748 }
31749
31750 return error_mark_node;
31751 }
31752
31753 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31754 look ahead to see if an objc keyword follows the attributes. This
31755 is to detect the use of prefix attributes on ObjC @interface and
31756 @protocol. */
31757
31758 static bool
31759 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31760 {
31761 cp_lexer_save_tokens (parser->lexer);
31762 *attrib = cp_parser_attributes_opt (parser);
31763 gcc_assert (*attrib);
31764 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31765 {
31766 cp_lexer_commit_tokens (parser->lexer);
31767 return true;
31768 }
31769 cp_lexer_rollback_tokens (parser->lexer);
31770 return false;
31771 }
31772
31773 /* This routine is a minimal replacement for
31774 c_parser_struct_declaration () used when parsing the list of
31775 types/names or ObjC++ properties. For example, when parsing the
31776 code
31777
31778 @property (readonly) int a, b, c;
31779
31780 this function is responsible for parsing "int a, int b, int c" and
31781 returning the declarations as CHAIN of DECLs.
31782
31783 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31784 similar parsing. */
31785 static tree
31786 cp_parser_objc_struct_declaration (cp_parser *parser)
31787 {
31788 tree decls = NULL_TREE;
31789 cp_decl_specifier_seq declspecs;
31790 int decl_class_or_enum_p;
31791 tree prefix_attributes;
31792
31793 cp_parser_decl_specifier_seq (parser,
31794 CP_PARSER_FLAGS_NONE,
31795 &declspecs,
31796 &decl_class_or_enum_p);
31797
31798 if (declspecs.type == error_mark_node)
31799 return error_mark_node;
31800
31801 /* auto, register, static, extern, mutable. */
31802 if (declspecs.storage_class != sc_none)
31803 {
31804 cp_parser_error (parser, "invalid type for property");
31805 declspecs.storage_class = sc_none;
31806 }
31807
31808 /* thread_local. */
31809 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31810 {
31811 cp_parser_error (parser, "invalid type for property");
31812 declspecs.locations[ds_thread] = 0;
31813 }
31814
31815 /* typedef. */
31816 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31817 {
31818 cp_parser_error (parser, "invalid type for property");
31819 declspecs.locations[ds_typedef] = 0;
31820 }
31821
31822 prefix_attributes = declspecs.attributes;
31823 declspecs.attributes = NULL_TREE;
31824
31825 /* Keep going until we hit the `;' at the end of the declaration. */
31826 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31827 {
31828 tree attributes, first_attribute, decl;
31829 cp_declarator *declarator;
31830 cp_token *token;
31831
31832 /* Parse the declarator. */
31833 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31834 CP_PARSER_FLAGS_NONE,
31835 NULL, NULL, false, false, false);
31836
31837 /* Look for attributes that apply to the ivar. */
31838 attributes = cp_parser_attributes_opt (parser);
31839 /* Remember which attributes are prefix attributes and
31840 which are not. */
31841 first_attribute = attributes;
31842 /* Combine the attributes. */
31843 attributes = attr_chainon (prefix_attributes, attributes);
31844
31845 decl = grokfield (declarator, &declspecs,
31846 NULL_TREE, /*init_const_expr_p=*/false,
31847 NULL_TREE, attributes);
31848
31849 if (decl == error_mark_node || decl == NULL_TREE)
31850 return error_mark_node;
31851
31852 /* Reset PREFIX_ATTRIBUTES. */
31853 if (attributes != error_mark_node)
31854 {
31855 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31856 attributes = TREE_CHAIN (attributes);
31857 if (attributes)
31858 TREE_CHAIN (attributes) = NULL_TREE;
31859 }
31860
31861 DECL_CHAIN (decl) = decls;
31862 decls = decl;
31863
31864 token = cp_lexer_peek_token (parser->lexer);
31865 if (token->type == CPP_COMMA)
31866 {
31867 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31868 continue;
31869 }
31870 else
31871 break;
31872 }
31873 return decls;
31874 }
31875
31876 /* Parse an Objective-C @property declaration. The syntax is:
31877
31878 objc-property-declaration:
31879 '@property' objc-property-attributes[opt] struct-declaration ;
31880
31881 objc-property-attributes:
31882 '(' objc-property-attribute-list ')'
31883
31884 objc-property-attribute-list:
31885 objc-property-attribute
31886 objc-property-attribute-list, objc-property-attribute
31887
31888 objc-property-attribute
31889 'getter' = identifier
31890 'setter' = identifier
31891 'readonly'
31892 'readwrite'
31893 'assign'
31894 'retain'
31895 'copy'
31896 'nonatomic'
31897
31898 For example:
31899 @property NSString *name;
31900 @property (readonly) id object;
31901 @property (retain, nonatomic, getter=getTheName) id name;
31902 @property int a, b, c;
31903
31904 PS: This function is identical to
31905 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31906 static void
31907 cp_parser_objc_at_property_declaration (cp_parser *parser)
31908 {
31909 /* The following variables hold the attributes of the properties as
31910 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31911 seen. When we see an attribute, we set them to 'true' (if they
31912 are boolean properties) or to the identifier (if they have an
31913 argument, ie, for getter and setter). Note that here we only
31914 parse the list of attributes, check the syntax and accumulate the
31915 attributes that we find. objc_add_property_declaration() will
31916 then process the information. */
31917 bool property_assign = false;
31918 bool property_copy = false;
31919 tree property_getter_ident = NULL_TREE;
31920 bool property_nonatomic = false;
31921 bool property_readonly = false;
31922 bool property_readwrite = false;
31923 bool property_retain = false;
31924 tree property_setter_ident = NULL_TREE;
31925
31926 /* 'properties' is the list of properties that we read. Usually a
31927 single one, but maybe more (eg, in "@property int a, b, c;" there
31928 are three). */
31929 tree properties;
31930 location_t loc;
31931
31932 loc = cp_lexer_peek_token (parser->lexer)->location;
31933
31934 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31935
31936 /* Parse the optional attribute list... */
31937 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31938 {
31939 /* Eat the '('. */
31940 matching_parens parens;
31941 parens.consume_open (parser);
31942
31943 while (true)
31944 {
31945 bool syntax_error = false;
31946 cp_token *token = cp_lexer_peek_token (parser->lexer);
31947 enum rid keyword;
31948
31949 if (token->type != CPP_NAME)
31950 {
31951 cp_parser_error (parser, "expected identifier");
31952 break;
31953 }
31954 keyword = C_RID_CODE (token->u.value);
31955 cp_lexer_consume_token (parser->lexer);
31956 switch (keyword)
31957 {
31958 case RID_ASSIGN: property_assign = true; break;
31959 case RID_COPY: property_copy = true; break;
31960 case RID_NONATOMIC: property_nonatomic = true; break;
31961 case RID_READONLY: property_readonly = true; break;
31962 case RID_READWRITE: property_readwrite = true; break;
31963 case RID_RETAIN: property_retain = true; break;
31964
31965 case RID_GETTER:
31966 case RID_SETTER:
31967 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31968 {
31969 if (keyword == RID_GETTER)
31970 cp_parser_error (parser,
31971 "missing %<=%> (after %<getter%> attribute)");
31972 else
31973 cp_parser_error (parser,
31974 "missing %<=%> (after %<setter%> attribute)");
31975 syntax_error = true;
31976 break;
31977 }
31978 cp_lexer_consume_token (parser->lexer); /* eat the = */
31979 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31980 {
31981 cp_parser_error (parser, "expected identifier");
31982 syntax_error = true;
31983 break;
31984 }
31985 if (keyword == RID_SETTER)
31986 {
31987 if (property_setter_ident != NULL_TREE)
31988 {
31989 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31990 cp_lexer_consume_token (parser->lexer);
31991 }
31992 else
31993 property_setter_ident = cp_parser_objc_selector (parser);
31994 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31995 cp_parser_error (parser, "setter name must terminate with %<:%>");
31996 else
31997 cp_lexer_consume_token (parser->lexer);
31998 }
31999 else
32000 {
32001 if (property_getter_ident != NULL_TREE)
32002 {
32003 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
32004 cp_lexer_consume_token (parser->lexer);
32005 }
32006 else
32007 property_getter_ident = cp_parser_objc_selector (parser);
32008 }
32009 break;
32010 default:
32011 cp_parser_error (parser, "unknown property attribute");
32012 syntax_error = true;
32013 break;
32014 }
32015
32016 if (syntax_error)
32017 break;
32018
32019 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32020 cp_lexer_consume_token (parser->lexer);
32021 else
32022 break;
32023 }
32024
32025 /* FIXME: "@property (setter, assign);" will generate a spurious
32026 "error: expected ‘)’ before ‘,’ token". This is because
32027 cp_parser_require, unlike the C counterpart, will produce an
32028 error even if we are in error recovery. */
32029 if (!parens.require_close (parser))
32030 {
32031 cp_parser_skip_to_closing_parenthesis (parser,
32032 /*recovering=*/true,
32033 /*or_comma=*/false,
32034 /*consume_paren=*/true);
32035 }
32036 }
32037
32038 /* ... and the property declaration(s). */
32039 properties = cp_parser_objc_struct_declaration (parser);
32040
32041 if (properties == error_mark_node)
32042 {
32043 cp_parser_skip_to_end_of_statement (parser);
32044 /* If the next token is now a `;', consume it. */
32045 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
32046 cp_lexer_consume_token (parser->lexer);
32047 return;
32048 }
32049
32050 if (properties == NULL_TREE)
32051 cp_parser_error (parser, "expected identifier");
32052 else
32053 {
32054 /* Comma-separated properties are chained together in
32055 reverse order; add them one by one. */
32056 properties = nreverse (properties);
32057
32058 for (; properties; properties = TREE_CHAIN (properties))
32059 objc_add_property_declaration (loc, copy_node (properties),
32060 property_readonly, property_readwrite,
32061 property_assign, property_retain,
32062 property_copy, property_nonatomic,
32063 property_getter_ident, property_setter_ident);
32064 }
32065
32066 cp_parser_consume_semicolon_at_end_of_statement (parser);
32067 }
32068
32069 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
32070
32071 objc-synthesize-declaration:
32072 @synthesize objc-synthesize-identifier-list ;
32073
32074 objc-synthesize-identifier-list:
32075 objc-synthesize-identifier
32076 objc-synthesize-identifier-list, objc-synthesize-identifier
32077
32078 objc-synthesize-identifier
32079 identifier
32080 identifier = identifier
32081
32082 For example:
32083 @synthesize MyProperty;
32084 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
32085
32086 PS: This function is identical to c_parser_objc_at_synthesize_declaration
32087 for C. Keep them in sync.
32088 */
32089 static void
32090 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
32091 {
32092 tree list = NULL_TREE;
32093 location_t loc;
32094 loc = cp_lexer_peek_token (parser->lexer)->location;
32095
32096 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
32097 while (true)
32098 {
32099 tree property, ivar;
32100 property = cp_parser_identifier (parser);
32101 if (property == error_mark_node)
32102 {
32103 cp_parser_consume_semicolon_at_end_of_statement (parser);
32104 return;
32105 }
32106 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
32107 {
32108 cp_lexer_consume_token (parser->lexer);
32109 ivar = cp_parser_identifier (parser);
32110 if (ivar == error_mark_node)
32111 {
32112 cp_parser_consume_semicolon_at_end_of_statement (parser);
32113 return;
32114 }
32115 }
32116 else
32117 ivar = NULL_TREE;
32118 list = chainon (list, build_tree_list (ivar, property));
32119 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32120 cp_lexer_consume_token (parser->lexer);
32121 else
32122 break;
32123 }
32124 cp_parser_consume_semicolon_at_end_of_statement (parser);
32125 objc_add_synthesize_declaration (loc, list);
32126 }
32127
32128 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
32129
32130 objc-dynamic-declaration:
32131 @dynamic identifier-list ;
32132
32133 For example:
32134 @dynamic MyProperty;
32135 @dynamic MyProperty, AnotherProperty;
32136
32137 PS: This function is identical to c_parser_objc_at_dynamic_declaration
32138 for C. Keep them in sync.
32139 */
32140 static void
32141 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
32142 {
32143 tree list = NULL_TREE;
32144 location_t loc;
32145 loc = cp_lexer_peek_token (parser->lexer)->location;
32146
32147 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
32148 while (true)
32149 {
32150 tree property;
32151 property = cp_parser_identifier (parser);
32152 if (property == error_mark_node)
32153 {
32154 cp_parser_consume_semicolon_at_end_of_statement (parser);
32155 return;
32156 }
32157 list = chainon (list, build_tree_list (NULL, property));
32158 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32159 cp_lexer_consume_token (parser->lexer);
32160 else
32161 break;
32162 }
32163 cp_parser_consume_semicolon_at_end_of_statement (parser);
32164 objc_add_dynamic_declaration (loc, list);
32165 }
32166
32167 \f
32168 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
32169
32170 /* Returns name of the next clause.
32171 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
32172 the token is not consumed. Otherwise appropriate pragma_omp_clause is
32173 returned and the token is consumed. */
32174
32175 static pragma_omp_clause
32176 cp_parser_omp_clause_name (cp_parser *parser)
32177 {
32178 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
32179
32180 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32181 result = PRAGMA_OACC_CLAUSE_AUTO;
32182 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
32183 result = PRAGMA_OMP_CLAUSE_IF;
32184 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
32185 result = PRAGMA_OMP_CLAUSE_DEFAULT;
32186 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
32187 result = PRAGMA_OACC_CLAUSE_DELETE;
32188 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
32189 result = PRAGMA_OMP_CLAUSE_PRIVATE;
32190 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
32191 result = PRAGMA_OMP_CLAUSE_FOR;
32192 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32193 {
32194 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32195 const char *p = IDENTIFIER_POINTER (id);
32196
32197 switch (p[0])
32198 {
32199 case 'a':
32200 if (!strcmp ("aligned", p))
32201 result = PRAGMA_OMP_CLAUSE_ALIGNED;
32202 else if (!strcmp ("async", p))
32203 result = PRAGMA_OACC_CLAUSE_ASYNC;
32204 break;
32205 case 'c':
32206 if (!strcmp ("collapse", p))
32207 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
32208 else if (!strcmp ("copy", p))
32209 result = PRAGMA_OACC_CLAUSE_COPY;
32210 else if (!strcmp ("copyin", p))
32211 result = PRAGMA_OMP_CLAUSE_COPYIN;
32212 else if (!strcmp ("copyout", p))
32213 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32214 else if (!strcmp ("copyprivate", p))
32215 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
32216 else if (!strcmp ("create", p))
32217 result = PRAGMA_OACC_CLAUSE_CREATE;
32218 break;
32219 case 'd':
32220 if (!strcmp ("defaultmap", p))
32221 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
32222 else if (!strcmp ("depend", p))
32223 result = PRAGMA_OMP_CLAUSE_DEPEND;
32224 else if (!strcmp ("device", p))
32225 result = PRAGMA_OMP_CLAUSE_DEVICE;
32226 else if (!strcmp ("deviceptr", p))
32227 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
32228 else if (!strcmp ("device_resident", p))
32229 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
32230 else if (!strcmp ("dist_schedule", p))
32231 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
32232 break;
32233 case 'f':
32234 if (!strcmp ("final", p))
32235 result = PRAGMA_OMP_CLAUSE_FINAL;
32236 else if (!strcmp ("finalize", p))
32237 result = PRAGMA_OACC_CLAUSE_FINALIZE;
32238 else if (!strcmp ("firstprivate", p))
32239 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
32240 else if (!strcmp ("from", p))
32241 result = PRAGMA_OMP_CLAUSE_FROM;
32242 break;
32243 case 'g':
32244 if (!strcmp ("gang", p))
32245 result = PRAGMA_OACC_CLAUSE_GANG;
32246 else if (!strcmp ("grainsize", p))
32247 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
32248 break;
32249 case 'h':
32250 if (!strcmp ("hint", p))
32251 result = PRAGMA_OMP_CLAUSE_HINT;
32252 else if (!strcmp ("host", p))
32253 result = PRAGMA_OACC_CLAUSE_HOST;
32254 break;
32255 case 'i':
32256 if (!strcmp ("if_present", p))
32257 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
32258 else if (!strcmp ("in_reduction", p))
32259 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
32260 else if (!strcmp ("inbranch", p))
32261 result = PRAGMA_OMP_CLAUSE_INBRANCH;
32262 else if (!strcmp ("independent", p))
32263 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
32264 else if (!strcmp ("is_device_ptr", p))
32265 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
32266 break;
32267 case 'l':
32268 if (!strcmp ("lastprivate", p))
32269 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
32270 else if (!strcmp ("linear", p))
32271 result = PRAGMA_OMP_CLAUSE_LINEAR;
32272 else if (!strcmp ("link", p))
32273 result = PRAGMA_OMP_CLAUSE_LINK;
32274 break;
32275 case 'm':
32276 if (!strcmp ("map", p))
32277 result = PRAGMA_OMP_CLAUSE_MAP;
32278 else if (!strcmp ("mergeable", p))
32279 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
32280 break;
32281 case 'n':
32282 if (!strcmp ("nogroup", p))
32283 result = PRAGMA_OMP_CLAUSE_NOGROUP;
32284 else if (!strcmp ("nontemporal", p))
32285 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
32286 else if (!strcmp ("notinbranch", p))
32287 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
32288 else if (!strcmp ("nowait", p))
32289 result = PRAGMA_OMP_CLAUSE_NOWAIT;
32290 else if (!strcmp ("num_gangs", p))
32291 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
32292 else if (!strcmp ("num_tasks", p))
32293 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
32294 else if (!strcmp ("num_teams", p))
32295 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
32296 else if (!strcmp ("num_threads", p))
32297 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
32298 else if (!strcmp ("num_workers", p))
32299 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
32300 break;
32301 case 'o':
32302 if (!strcmp ("ordered", p))
32303 result = PRAGMA_OMP_CLAUSE_ORDERED;
32304 break;
32305 case 'p':
32306 if (!strcmp ("parallel", p))
32307 result = PRAGMA_OMP_CLAUSE_PARALLEL;
32308 else if (!strcmp ("present", p))
32309 result = PRAGMA_OACC_CLAUSE_PRESENT;
32310 else if (!strcmp ("present_or_copy", p)
32311 || !strcmp ("pcopy", p))
32312 result = PRAGMA_OACC_CLAUSE_COPY;
32313 else if (!strcmp ("present_or_copyin", p)
32314 || !strcmp ("pcopyin", p))
32315 result = PRAGMA_OACC_CLAUSE_COPYIN;
32316 else if (!strcmp ("present_or_copyout", p)
32317 || !strcmp ("pcopyout", p))
32318 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32319 else if (!strcmp ("present_or_create", p)
32320 || !strcmp ("pcreate", p))
32321 result = PRAGMA_OACC_CLAUSE_CREATE;
32322 else if (!strcmp ("priority", p))
32323 result = PRAGMA_OMP_CLAUSE_PRIORITY;
32324 else if (!strcmp ("proc_bind", p))
32325 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
32326 break;
32327 case 'r':
32328 if (!strcmp ("reduction", p))
32329 result = PRAGMA_OMP_CLAUSE_REDUCTION;
32330 break;
32331 case 's':
32332 if (!strcmp ("safelen", p))
32333 result = PRAGMA_OMP_CLAUSE_SAFELEN;
32334 else if (!strcmp ("schedule", p))
32335 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
32336 else if (!strcmp ("sections", p))
32337 result = PRAGMA_OMP_CLAUSE_SECTIONS;
32338 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
32339 result = PRAGMA_OACC_CLAUSE_HOST;
32340 else if (!strcmp ("seq", p))
32341 result = PRAGMA_OACC_CLAUSE_SEQ;
32342 else if (!strcmp ("shared", p))
32343 result = PRAGMA_OMP_CLAUSE_SHARED;
32344 else if (!strcmp ("simd", p))
32345 result = PRAGMA_OMP_CLAUSE_SIMD;
32346 else if (!strcmp ("simdlen", p))
32347 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
32348 break;
32349 case 't':
32350 if (!strcmp ("task_reduction", p))
32351 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
32352 else if (!strcmp ("taskgroup", p))
32353 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
32354 else if (!strcmp ("thread_limit", p))
32355 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
32356 else if (!strcmp ("threads", p))
32357 result = PRAGMA_OMP_CLAUSE_THREADS;
32358 else if (!strcmp ("tile", p))
32359 result = PRAGMA_OACC_CLAUSE_TILE;
32360 else if (!strcmp ("to", p))
32361 result = PRAGMA_OMP_CLAUSE_TO;
32362 break;
32363 case 'u':
32364 if (!strcmp ("uniform", p))
32365 result = PRAGMA_OMP_CLAUSE_UNIFORM;
32366 else if (!strcmp ("untied", p))
32367 result = PRAGMA_OMP_CLAUSE_UNTIED;
32368 else if (!strcmp ("use_device", p))
32369 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
32370 else if (!strcmp ("use_device_ptr", p))
32371 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
32372 break;
32373 case 'v':
32374 if (!strcmp ("vector", p))
32375 result = PRAGMA_OACC_CLAUSE_VECTOR;
32376 else if (!strcmp ("vector_length", p))
32377 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
32378 break;
32379 case 'w':
32380 if (!strcmp ("wait", p))
32381 result = PRAGMA_OACC_CLAUSE_WAIT;
32382 else if (!strcmp ("worker", p))
32383 result = PRAGMA_OACC_CLAUSE_WORKER;
32384 break;
32385 }
32386 }
32387
32388 if (result != PRAGMA_OMP_CLAUSE_NONE)
32389 cp_lexer_consume_token (parser->lexer);
32390
32391 return result;
32392 }
32393
32394 /* Validate that a clause of the given type does not already exist. */
32395
32396 static void
32397 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
32398 const char *name, location_t location)
32399 {
32400 tree c;
32401
32402 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
32403 if (OMP_CLAUSE_CODE (c) == code)
32404 {
32405 error_at (location, "too many %qs clauses", name);
32406 break;
32407 }
32408 }
32409
32410 /* OpenMP 2.5:
32411 variable-list:
32412 identifier
32413 variable-list , identifier
32414
32415 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
32416 colon). An opening parenthesis will have been consumed by the caller.
32417
32418 If KIND is nonzero, create the appropriate node and install the decl
32419 in OMP_CLAUSE_DECL and add the node to the head of the list.
32420
32421 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
32422 return the list created.
32423
32424 COLON can be NULL if only closing parenthesis should end the list,
32425 or pointer to bool which will receive false if the list is terminated
32426 by closing parenthesis or true if the list is terminated by colon. */
32427
32428 static tree
32429 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
32430 tree list, bool *colon)
32431 {
32432 cp_token *token;
32433 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32434 if (colon)
32435 {
32436 parser->colon_corrects_to_scope_p = false;
32437 *colon = false;
32438 }
32439 while (1)
32440 {
32441 tree name, decl;
32442
32443 if (kind == OMP_CLAUSE_DEPEND)
32444 cp_parser_parse_tentatively (parser);
32445 token = cp_lexer_peek_token (parser->lexer);
32446 if (kind != 0
32447 && current_class_ptr
32448 && cp_parser_is_keyword (token, RID_THIS))
32449 {
32450 decl = finish_this_expr ();
32451 if (TREE_CODE (decl) == NON_LVALUE_EXPR
32452 || CONVERT_EXPR_P (decl))
32453 decl = TREE_OPERAND (decl, 0);
32454 cp_lexer_consume_token (parser->lexer);
32455 }
32456 else
32457 {
32458 name = cp_parser_id_expression (parser, /*template_p=*/false,
32459 /*check_dependency_p=*/true,
32460 /*template_p=*/NULL,
32461 /*declarator_p=*/false,
32462 /*optional_p=*/false);
32463 if (name == error_mark_node)
32464 {
32465 if (kind == OMP_CLAUSE_DEPEND
32466 && cp_parser_simulate_error (parser))
32467 goto depend_lvalue;
32468 goto skip_comma;
32469 }
32470
32471 if (identifier_p (name))
32472 decl = cp_parser_lookup_name_simple (parser, name, token->location);
32473 else
32474 decl = name;
32475 if (decl == error_mark_node)
32476 {
32477 if (kind == OMP_CLAUSE_DEPEND
32478 && cp_parser_simulate_error (parser))
32479 goto depend_lvalue;
32480 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
32481 token->location);
32482 }
32483 }
32484 if (decl == error_mark_node)
32485 ;
32486 else if (kind != 0)
32487 {
32488 switch (kind)
32489 {
32490 case OMP_CLAUSE__CACHE_:
32491 /* The OpenACC cache directive explicitly only allows "array
32492 elements or subarrays". */
32493 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
32494 {
32495 error_at (token->location, "expected %<[%>");
32496 decl = error_mark_node;
32497 break;
32498 }
32499 /* FALLTHROUGH. */
32500 case OMP_CLAUSE_MAP:
32501 case OMP_CLAUSE_FROM:
32502 case OMP_CLAUSE_TO:
32503 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
32504 {
32505 location_t loc
32506 = cp_lexer_peek_token (parser->lexer)->location;
32507 cp_id_kind idk = CP_ID_KIND_NONE;
32508 cp_lexer_consume_token (parser->lexer);
32509 decl = convert_from_reference (decl);
32510 decl
32511 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
32512 decl, false,
32513 &idk, loc);
32514 }
32515 /* FALLTHROUGH. */
32516 case OMP_CLAUSE_DEPEND:
32517 case OMP_CLAUSE_REDUCTION:
32518 case OMP_CLAUSE_IN_REDUCTION:
32519 case OMP_CLAUSE_TASK_REDUCTION:
32520 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
32521 {
32522 tree low_bound = NULL_TREE, length = NULL_TREE;
32523
32524 parser->colon_corrects_to_scope_p = false;
32525 cp_lexer_consume_token (parser->lexer);
32526 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32527 low_bound = cp_parser_expression (parser);
32528 if (!colon)
32529 parser->colon_corrects_to_scope_p
32530 = saved_colon_corrects_to_scope_p;
32531 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
32532 length = integer_one_node;
32533 else
32534 {
32535 /* Look for `:'. */
32536 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32537 {
32538 if (kind == OMP_CLAUSE_DEPEND
32539 && cp_parser_simulate_error (parser))
32540 goto depend_lvalue;
32541 goto skip_comma;
32542 }
32543 if (kind == OMP_CLAUSE_DEPEND)
32544 cp_parser_commit_to_tentative_parse (parser);
32545 if (!cp_lexer_next_token_is (parser->lexer,
32546 CPP_CLOSE_SQUARE))
32547 length = cp_parser_expression (parser);
32548 }
32549 /* Look for the closing `]'. */
32550 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
32551 RT_CLOSE_SQUARE))
32552 {
32553 if (kind == OMP_CLAUSE_DEPEND
32554 && cp_parser_simulate_error (parser))
32555 goto depend_lvalue;
32556 goto skip_comma;
32557 }
32558
32559 decl = tree_cons (low_bound, length, decl);
32560 }
32561 break;
32562 default:
32563 break;
32564 }
32565
32566 if (kind == OMP_CLAUSE_DEPEND)
32567 {
32568 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
32569 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32570 && cp_parser_simulate_error (parser))
32571 {
32572 depend_lvalue:
32573 cp_parser_abort_tentative_parse (parser);
32574 decl = cp_parser_assignment_expression (parser, NULL,
32575 false, false);
32576 }
32577 else
32578 cp_parser_parse_definitely (parser);
32579 }
32580
32581 tree u = build_omp_clause (token->location, kind);
32582 OMP_CLAUSE_DECL (u) = decl;
32583 OMP_CLAUSE_CHAIN (u) = list;
32584 list = u;
32585 }
32586 else
32587 list = tree_cons (decl, NULL_TREE, list);
32588
32589 get_comma:
32590 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32591 break;
32592 cp_lexer_consume_token (parser->lexer);
32593 }
32594
32595 if (colon)
32596 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32597
32598 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32599 {
32600 *colon = true;
32601 cp_parser_require (parser, CPP_COLON, RT_COLON);
32602 return list;
32603 }
32604
32605 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32606 {
32607 int ending;
32608
32609 /* Try to resync to an unnested comma. Copied from
32610 cp_parser_parenthesized_expression_list. */
32611 skip_comma:
32612 if (colon)
32613 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32614 ending = cp_parser_skip_to_closing_parenthesis (parser,
32615 /*recovering=*/true,
32616 /*or_comma=*/true,
32617 /*consume_paren=*/true);
32618 if (ending < 0)
32619 goto get_comma;
32620 }
32621
32622 return list;
32623 }
32624
32625 /* Similarly, but expect leading and trailing parenthesis. This is a very
32626 common case for omp clauses. */
32627
32628 static tree
32629 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
32630 {
32631 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32632 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
32633 return list;
32634 }
32635
32636 /* OpenACC 2.0:
32637 copy ( variable-list )
32638 copyin ( variable-list )
32639 copyout ( variable-list )
32640 create ( variable-list )
32641 delete ( variable-list )
32642 present ( variable-list ) */
32643
32644 static tree
32645 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
32646 tree list)
32647 {
32648 enum gomp_map_kind kind;
32649 switch (c_kind)
32650 {
32651 case PRAGMA_OACC_CLAUSE_COPY:
32652 kind = GOMP_MAP_TOFROM;
32653 break;
32654 case PRAGMA_OACC_CLAUSE_COPYIN:
32655 kind = GOMP_MAP_TO;
32656 break;
32657 case PRAGMA_OACC_CLAUSE_COPYOUT:
32658 kind = GOMP_MAP_FROM;
32659 break;
32660 case PRAGMA_OACC_CLAUSE_CREATE:
32661 kind = GOMP_MAP_ALLOC;
32662 break;
32663 case PRAGMA_OACC_CLAUSE_DELETE:
32664 kind = GOMP_MAP_RELEASE;
32665 break;
32666 case PRAGMA_OACC_CLAUSE_DEVICE:
32667 kind = GOMP_MAP_FORCE_TO;
32668 break;
32669 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32670 kind = GOMP_MAP_DEVICE_RESIDENT;
32671 break;
32672 case PRAGMA_OACC_CLAUSE_HOST:
32673 kind = GOMP_MAP_FORCE_FROM;
32674 break;
32675 case PRAGMA_OACC_CLAUSE_LINK:
32676 kind = GOMP_MAP_LINK;
32677 break;
32678 case PRAGMA_OACC_CLAUSE_PRESENT:
32679 kind = GOMP_MAP_FORCE_PRESENT;
32680 break;
32681 default:
32682 gcc_unreachable ();
32683 }
32684 tree nl, c;
32685 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
32686
32687 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
32688 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32689
32690 return nl;
32691 }
32692
32693 /* OpenACC 2.0:
32694 deviceptr ( variable-list ) */
32695
32696 static tree
32697 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
32698 {
32699 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32700 tree vars, t;
32701
32702 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32703 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32704 variable-list must only allow for pointer variables. */
32705 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32706 for (t = vars; t; t = TREE_CHAIN (t))
32707 {
32708 tree v = TREE_PURPOSE (t);
32709 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
32710 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
32711 OMP_CLAUSE_DECL (u) = v;
32712 OMP_CLAUSE_CHAIN (u) = list;
32713 list = u;
32714 }
32715
32716 return list;
32717 }
32718
32719 /* OpenACC 2.5:
32720 auto
32721 finalize
32722 independent
32723 nohost
32724 seq */
32725
32726 static tree
32727 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
32728 tree list)
32729 {
32730 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
32731
32732 tree c = build_omp_clause (loc, code);
32733 OMP_CLAUSE_CHAIN (c) = list;
32734
32735 return c;
32736 }
32737
32738 /* OpenACC:
32739 num_gangs ( expression )
32740 num_workers ( expression )
32741 vector_length ( expression ) */
32742
32743 static tree
32744 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32745 const char *str, tree list)
32746 {
32747 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32748
32749 matching_parens parens;
32750 if (!parens.require_open (parser))
32751 return list;
32752
32753 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32754
32755 if (t == error_mark_node
32756 || !parens.require_close (parser))
32757 {
32758 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32759 /*or_comma=*/false,
32760 /*consume_paren=*/true);
32761 return list;
32762 }
32763
32764 check_no_duplicate_clause (list, code, str, loc);
32765
32766 tree c = build_omp_clause (loc, code);
32767 OMP_CLAUSE_OPERAND (c, 0) = t;
32768 OMP_CLAUSE_CHAIN (c) = list;
32769 return c;
32770 }
32771
32772 /* OpenACC:
32773
32774 gang [( gang-arg-list )]
32775 worker [( [num:] int-expr )]
32776 vector [( [length:] int-expr )]
32777
32778 where gang-arg is one of:
32779
32780 [num:] int-expr
32781 static: size-expr
32782
32783 and size-expr may be:
32784
32785 *
32786 int-expr
32787 */
32788
32789 static tree
32790 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
32791 omp_clause_code kind,
32792 const char *str, tree list)
32793 {
32794 const char *id = "num";
32795 cp_lexer *lexer = parser->lexer;
32796 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
32797
32798 if (kind == OMP_CLAUSE_VECTOR)
32799 id = "length";
32800
32801 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
32802 {
32803 matching_parens parens;
32804 parens.consume_open (parser);
32805
32806 do
32807 {
32808 cp_token *next = cp_lexer_peek_token (lexer);
32809 int idx = 0;
32810
32811 /* Gang static argument. */
32812 if (kind == OMP_CLAUSE_GANG
32813 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
32814 {
32815 cp_lexer_consume_token (lexer);
32816
32817 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32818 goto cleanup_error;
32819
32820 idx = 1;
32821 if (ops[idx] != NULL)
32822 {
32823 cp_parser_error (parser, "too many %<static%> arguments");
32824 goto cleanup_error;
32825 }
32826
32827 /* Check for the '*' argument. */
32828 if (cp_lexer_next_token_is (lexer, CPP_MULT)
32829 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32830 || cp_lexer_nth_token_is (parser->lexer, 2,
32831 CPP_CLOSE_PAREN)))
32832 {
32833 cp_lexer_consume_token (lexer);
32834 ops[idx] = integer_minus_one_node;
32835
32836 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
32837 {
32838 cp_lexer_consume_token (lexer);
32839 continue;
32840 }
32841 else break;
32842 }
32843 }
32844 /* Worker num: argument and vector length: arguments. */
32845 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
32846 && id_equal (next->u.value, id)
32847 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
32848 {
32849 cp_lexer_consume_token (lexer); /* id */
32850 cp_lexer_consume_token (lexer); /* ':' */
32851 }
32852
32853 /* Now collect the actual argument. */
32854 if (ops[idx] != NULL_TREE)
32855 {
32856 cp_parser_error (parser, "unexpected argument");
32857 goto cleanup_error;
32858 }
32859
32860 tree expr = cp_parser_assignment_expression (parser, NULL, false,
32861 false);
32862 if (expr == error_mark_node)
32863 goto cleanup_error;
32864
32865 mark_exp_read (expr);
32866 ops[idx] = expr;
32867
32868 if (kind == OMP_CLAUSE_GANG
32869 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32870 {
32871 cp_lexer_consume_token (lexer);
32872 continue;
32873 }
32874 break;
32875 }
32876 while (1);
32877
32878 if (!parens.require_close (parser))
32879 goto cleanup_error;
32880 }
32881
32882 check_no_duplicate_clause (list, kind, str, loc);
32883
32884 c = build_omp_clause (loc, kind);
32885
32886 if (ops[1])
32887 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32888
32889 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32890 OMP_CLAUSE_CHAIN (c) = list;
32891
32892 return c;
32893
32894 cleanup_error:
32895 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32896 return list;
32897 }
32898
32899 /* OpenACC 2.0:
32900 tile ( size-expr-list ) */
32901
32902 static tree
32903 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32904 {
32905 tree c, expr = error_mark_node;
32906 tree tile = NULL_TREE;
32907
32908 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32909 so, but the spec authors never considered such a case and have
32910 differing opinions on what it might mean, including 'not
32911 allowed'.) */
32912 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32913 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32914 clause_loc);
32915
32916 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32917 return list;
32918
32919 do
32920 {
32921 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32922 return list;
32923
32924 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32925 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32926 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32927 {
32928 cp_lexer_consume_token (parser->lexer);
32929 expr = integer_zero_node;
32930 }
32931 else
32932 expr = cp_parser_constant_expression (parser);
32933
32934 tile = tree_cons (NULL_TREE, expr, tile);
32935 }
32936 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32937
32938 /* Consume the trailing ')'. */
32939 cp_lexer_consume_token (parser->lexer);
32940
32941 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32942 tile = nreverse (tile);
32943 OMP_CLAUSE_TILE_LIST (c) = tile;
32944 OMP_CLAUSE_CHAIN (c) = list;
32945 return c;
32946 }
32947
32948 /* OpenACC 2.0
32949 Parse wait clause or directive parameters. */
32950
32951 static tree
32952 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32953 {
32954 vec<tree, va_gc> *args;
32955 tree t, args_tree;
32956
32957 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32958 /*cast_p=*/false,
32959 /*allow_expansion_p=*/true,
32960 /*non_constant_p=*/NULL);
32961
32962 if (args == NULL || args->length () == 0)
32963 {
32964 if (args != NULL)
32965 {
32966 cp_parser_error (parser, "expected integer expression list");
32967 release_tree_vector (args);
32968 }
32969 return list;
32970 }
32971
32972 args_tree = build_tree_list_vec (args);
32973
32974 release_tree_vector (args);
32975
32976 for (t = args_tree; t; t = TREE_CHAIN (t))
32977 {
32978 tree targ = TREE_VALUE (t);
32979
32980 if (targ != error_mark_node)
32981 {
32982 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32983 error ("%<wait%> expression must be integral");
32984 else
32985 {
32986 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32987
32988 targ = mark_rvalue_use (targ);
32989 OMP_CLAUSE_DECL (c) = targ;
32990 OMP_CLAUSE_CHAIN (c) = list;
32991 list = c;
32992 }
32993 }
32994 }
32995
32996 return list;
32997 }
32998
32999 /* OpenACC:
33000 wait [( int-expr-list )] */
33001
33002 static tree
33003 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
33004 {
33005 location_t location = cp_lexer_peek_token (parser->lexer)->location;
33006
33007 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33008 list = cp_parser_oacc_wait_list (parser, location, list);
33009 else
33010 {
33011 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
33012
33013 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33014 OMP_CLAUSE_CHAIN (c) = list;
33015 list = c;
33016 }
33017
33018 return list;
33019 }
33020
33021 /* OpenMP 3.0:
33022 collapse ( constant-expression ) */
33023
33024 static tree
33025 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
33026 {
33027 tree c, num;
33028 location_t loc;
33029 HOST_WIDE_INT n;
33030
33031 loc = cp_lexer_peek_token (parser->lexer)->location;
33032 matching_parens parens;
33033 if (!parens.require_open (parser))
33034 return list;
33035
33036 num = cp_parser_constant_expression (parser);
33037
33038 if (!parens.require_close (parser))
33039 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33040 /*or_comma=*/false,
33041 /*consume_paren=*/true);
33042
33043 if (num == error_mark_node)
33044 return list;
33045 num = fold_non_dependent_expr (num);
33046 if (!tree_fits_shwi_p (num)
33047 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33048 || (n = tree_to_shwi (num)) <= 0
33049 || (int) n != n)
33050 {
33051 error_at (loc, "collapse argument needs positive constant integer expression");
33052 return list;
33053 }
33054
33055 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
33056 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
33057 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
33058 OMP_CLAUSE_CHAIN (c) = list;
33059 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
33060
33061 return c;
33062 }
33063
33064 /* OpenMP 2.5:
33065 default ( none | shared )
33066
33067 OpenACC:
33068 default ( none | present ) */
33069
33070 static tree
33071 cp_parser_omp_clause_default (cp_parser *parser, tree list,
33072 location_t location, bool is_oacc)
33073 {
33074 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
33075 tree c;
33076
33077 matching_parens parens;
33078 if (!parens.require_open (parser))
33079 return list;
33080 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33081 {
33082 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33083 const char *p = IDENTIFIER_POINTER (id);
33084
33085 switch (p[0])
33086 {
33087 case 'n':
33088 if (strcmp ("none", p) != 0)
33089 goto invalid_kind;
33090 kind = OMP_CLAUSE_DEFAULT_NONE;
33091 break;
33092
33093 case 'p':
33094 if (strcmp ("present", p) != 0 || !is_oacc)
33095 goto invalid_kind;
33096 kind = OMP_CLAUSE_DEFAULT_PRESENT;
33097 break;
33098
33099 case 's':
33100 if (strcmp ("shared", p) != 0 || is_oacc)
33101 goto invalid_kind;
33102 kind = OMP_CLAUSE_DEFAULT_SHARED;
33103 break;
33104
33105 default:
33106 goto invalid_kind;
33107 }
33108
33109 cp_lexer_consume_token (parser->lexer);
33110 }
33111 else
33112 {
33113 invalid_kind:
33114 if (is_oacc)
33115 cp_parser_error (parser, "expected %<none%> or %<present%>");
33116 else
33117 cp_parser_error (parser, "expected %<none%> or %<shared%>");
33118 }
33119
33120 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
33121 || !parens.require_close (parser))
33122 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33123 /*or_comma=*/false,
33124 /*consume_paren=*/true);
33125
33126 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
33127 return list;
33128
33129 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
33130 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
33131 OMP_CLAUSE_CHAIN (c) = list;
33132 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
33133
33134 return c;
33135 }
33136
33137 /* OpenMP 3.1:
33138 final ( expression ) */
33139
33140 static tree
33141 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
33142 {
33143 tree t, c;
33144
33145 matching_parens parens;
33146 if (!parens.require_open (parser))
33147 return list;
33148
33149 t = cp_parser_assignment_expression (parser);
33150
33151 if (t == error_mark_node
33152 || !parens.require_close (parser))
33153 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33154 /*or_comma=*/false,
33155 /*consume_paren=*/true);
33156
33157 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
33158
33159 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
33160 OMP_CLAUSE_FINAL_EXPR (c) = t;
33161 OMP_CLAUSE_CHAIN (c) = list;
33162
33163 return c;
33164 }
33165
33166 /* OpenMP 2.5:
33167 if ( expression )
33168
33169 OpenMP 4.5:
33170 if ( directive-name-modifier : expression )
33171
33172 directive-name-modifier:
33173 parallel | task | taskloop | target data | target | target update
33174 | target enter data | target exit data
33175
33176 OpenMP 5.0:
33177 directive-name-modifier:
33178 ... | simd | cancel */
33179
33180 static tree
33181 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
33182 bool is_omp)
33183 {
33184 tree t, c;
33185 enum tree_code if_modifier = ERROR_MARK;
33186
33187 matching_parens parens;
33188 if (!parens.require_open (parser))
33189 return list;
33190
33191 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33192 {
33193 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33194 const char *p = IDENTIFIER_POINTER (id);
33195 int n = 2;
33196
33197 if (strcmp ("cancel", p) == 0)
33198 if_modifier = VOID_CST;
33199 else if (strcmp ("parallel", p) == 0)
33200 if_modifier = OMP_PARALLEL;
33201 else if (strcmp ("simd", p) == 0)
33202 if_modifier = OMP_SIMD;
33203 else if (strcmp ("task", p) == 0)
33204 if_modifier = OMP_TASK;
33205 else if (strcmp ("taskloop", p) == 0)
33206 if_modifier = OMP_TASKLOOP;
33207 else if (strcmp ("target", p) == 0)
33208 {
33209 if_modifier = OMP_TARGET;
33210 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
33211 {
33212 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
33213 p = IDENTIFIER_POINTER (id);
33214 if (strcmp ("data", p) == 0)
33215 if_modifier = OMP_TARGET_DATA;
33216 else if (strcmp ("update", p) == 0)
33217 if_modifier = OMP_TARGET_UPDATE;
33218 else if (strcmp ("enter", p) == 0)
33219 if_modifier = OMP_TARGET_ENTER_DATA;
33220 else if (strcmp ("exit", p) == 0)
33221 if_modifier = OMP_TARGET_EXIT_DATA;
33222 if (if_modifier != OMP_TARGET)
33223 n = 3;
33224 else
33225 {
33226 location_t loc
33227 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
33228 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
33229 "or %<exit%>");
33230 if_modifier = ERROR_MARK;
33231 }
33232 if (if_modifier == OMP_TARGET_ENTER_DATA
33233 || if_modifier == OMP_TARGET_EXIT_DATA)
33234 {
33235 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
33236 {
33237 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
33238 p = IDENTIFIER_POINTER (id);
33239 if (strcmp ("data", p) == 0)
33240 n = 4;
33241 }
33242 if (n != 4)
33243 {
33244 location_t loc
33245 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
33246 error_at (loc, "expected %<data%>");
33247 if_modifier = ERROR_MARK;
33248 }
33249 }
33250 }
33251 }
33252 if (if_modifier != ERROR_MARK)
33253 {
33254 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
33255 {
33256 while (n-- > 0)
33257 cp_lexer_consume_token (parser->lexer);
33258 }
33259 else
33260 {
33261 if (n > 2)
33262 {
33263 location_t loc
33264 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
33265 error_at (loc, "expected %<:%>");
33266 }
33267 if_modifier = ERROR_MARK;
33268 }
33269 }
33270 }
33271
33272 t = cp_parser_assignment_expression (parser);
33273
33274 if (t == error_mark_node
33275 || !parens.require_close (parser))
33276 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33277 /*or_comma=*/false,
33278 /*consume_paren=*/true);
33279
33280 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33281 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
33282 {
33283 if (if_modifier != ERROR_MARK
33284 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33285 {
33286 const char *p = NULL;
33287 switch (if_modifier)
33288 {
33289 case VOID_CST: p = "cancel"; break;
33290 case OMP_PARALLEL: p = "parallel"; break;
33291 case OMP_SIMD: p = "simd"; break;
33292 case OMP_TASK: p = "task"; break;
33293 case OMP_TASKLOOP: p = "taskloop"; break;
33294 case OMP_TARGET_DATA: p = "target data"; break;
33295 case OMP_TARGET: p = "target"; break;
33296 case OMP_TARGET_UPDATE: p = "target update"; break;
33297 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
33298 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
33299 default: gcc_unreachable ();
33300 }
33301 error_at (location, "too many %<if%> clauses with %qs modifier",
33302 p);
33303 return list;
33304 }
33305 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33306 {
33307 if (!is_omp)
33308 error_at (location, "too many %<if%> clauses");
33309 else
33310 error_at (location, "too many %<if%> clauses without modifier");
33311 return list;
33312 }
33313 else if (if_modifier == ERROR_MARK
33314 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
33315 {
33316 error_at (location, "if any %<if%> clause has modifier, then all "
33317 "%<if%> clauses have to use modifier");
33318 return list;
33319 }
33320 }
33321
33322 c = build_omp_clause (location, OMP_CLAUSE_IF);
33323 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
33324 OMP_CLAUSE_IF_EXPR (c) = t;
33325 OMP_CLAUSE_CHAIN (c) = list;
33326
33327 return c;
33328 }
33329
33330 /* OpenMP 3.1:
33331 mergeable */
33332
33333 static tree
33334 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
33335 tree list, location_t location)
33336 {
33337 tree c;
33338
33339 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
33340 location);
33341
33342 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
33343 OMP_CLAUSE_CHAIN (c) = list;
33344 return c;
33345 }
33346
33347 /* OpenMP 2.5:
33348 nowait */
33349
33350 static tree
33351 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
33352 tree list, location_t location)
33353 {
33354 tree c;
33355
33356 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
33357
33358 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
33359 OMP_CLAUSE_CHAIN (c) = list;
33360 return c;
33361 }
33362
33363 /* OpenMP 2.5:
33364 num_threads ( expression ) */
33365
33366 static tree
33367 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
33368 location_t location)
33369 {
33370 tree t, c;
33371
33372 matching_parens parens;
33373 if (!parens.require_open (parser))
33374 return list;
33375
33376 t = cp_parser_assignment_expression (parser);
33377
33378 if (t == error_mark_node
33379 || !parens.require_close (parser))
33380 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33381 /*or_comma=*/false,
33382 /*consume_paren=*/true);
33383
33384 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
33385 "num_threads", location);
33386
33387 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
33388 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
33389 OMP_CLAUSE_CHAIN (c) = list;
33390
33391 return c;
33392 }
33393
33394 /* OpenMP 4.5:
33395 num_tasks ( expression ) */
33396
33397 static tree
33398 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
33399 location_t location)
33400 {
33401 tree t, c;
33402
33403 matching_parens parens;
33404 if (!parens.require_open (parser))
33405 return list;
33406
33407 t = cp_parser_assignment_expression (parser);
33408
33409 if (t == error_mark_node
33410 || !parens.require_close (parser))
33411 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33412 /*or_comma=*/false,
33413 /*consume_paren=*/true);
33414
33415 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
33416 "num_tasks", location);
33417
33418 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
33419 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
33420 OMP_CLAUSE_CHAIN (c) = list;
33421
33422 return c;
33423 }
33424
33425 /* OpenMP 4.5:
33426 grainsize ( expression ) */
33427
33428 static tree
33429 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
33430 location_t location)
33431 {
33432 tree t, c;
33433
33434 matching_parens parens;
33435 if (!parens.require_open (parser))
33436 return list;
33437
33438 t = cp_parser_assignment_expression (parser);
33439
33440 if (t == error_mark_node
33441 || !parens.require_close (parser))
33442 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33443 /*or_comma=*/false,
33444 /*consume_paren=*/true);
33445
33446 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
33447 "grainsize", location);
33448
33449 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
33450 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
33451 OMP_CLAUSE_CHAIN (c) = list;
33452
33453 return c;
33454 }
33455
33456 /* OpenMP 4.5:
33457 priority ( expression ) */
33458
33459 static tree
33460 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
33461 location_t location)
33462 {
33463 tree t, c;
33464
33465 matching_parens parens;
33466 if (!parens.require_open (parser))
33467 return list;
33468
33469 t = cp_parser_assignment_expression (parser);
33470
33471 if (t == error_mark_node
33472 || !parens.require_close (parser))
33473 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33474 /*or_comma=*/false,
33475 /*consume_paren=*/true);
33476
33477 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
33478 "priority", location);
33479
33480 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
33481 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
33482 OMP_CLAUSE_CHAIN (c) = list;
33483
33484 return c;
33485 }
33486
33487 /* OpenMP 4.5:
33488 hint ( expression ) */
33489
33490 static tree
33491 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
33492 {
33493 tree t, c;
33494
33495 matching_parens parens;
33496 if (!parens.require_open (parser))
33497 return list;
33498
33499 t = cp_parser_assignment_expression (parser);
33500
33501 if (t == error_mark_node
33502 || !parens.require_close (parser))
33503 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33504 /*or_comma=*/false,
33505 /*consume_paren=*/true);
33506
33507 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
33508
33509 c = build_omp_clause (location, OMP_CLAUSE_HINT);
33510 OMP_CLAUSE_HINT_EXPR (c) = t;
33511 OMP_CLAUSE_CHAIN (c) = list;
33512
33513 return c;
33514 }
33515
33516 /* OpenMP 4.5:
33517 defaultmap ( tofrom : scalar )
33518
33519 OpenMP 5.0:
33520 defaultmap ( implicit-behavior [ : variable-category ] ) */
33521
33522 static tree
33523 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
33524 location_t location)
33525 {
33526 tree c, id;
33527 const char *p;
33528 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33529 enum omp_clause_defaultmap_kind category
33530 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
33531
33532 matching_parens parens;
33533 if (!parens.require_open (parser))
33534 return list;
33535
33536 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
33537 p = "default";
33538 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33539 {
33540 invalid_behavior:
33541 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
33542 "%<tofrom%>, %<firstprivate%>, %<none%> "
33543 "or %<default%>");
33544 goto out_err;
33545 }
33546 else
33547 {
33548 id = cp_lexer_peek_token (parser->lexer)->u.value;
33549 p = IDENTIFIER_POINTER (id);
33550 }
33551
33552 switch (p[0])
33553 {
33554 case 'a':
33555 if (strcmp ("alloc", p) == 0)
33556 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
33557 else
33558 goto invalid_behavior;
33559 break;
33560
33561 case 'd':
33562 if (strcmp ("default", p) == 0)
33563 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33564 else
33565 goto invalid_behavior;
33566 break;
33567
33568 case 'f':
33569 if (strcmp ("firstprivate", p) == 0)
33570 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
33571 else if (strcmp ("from", p) == 0)
33572 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
33573 else
33574 goto invalid_behavior;
33575 break;
33576
33577 case 'n':
33578 if (strcmp ("none", p) == 0)
33579 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
33580 else
33581 goto invalid_behavior;
33582 break;
33583
33584 case 't':
33585 if (strcmp ("tofrom", p) == 0)
33586 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
33587 else if (strcmp ("to", p) == 0)
33588 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
33589 else
33590 goto invalid_behavior;
33591 break;
33592
33593 default:
33594 goto invalid_behavior;
33595 }
33596 cp_lexer_consume_token (parser->lexer);
33597
33598 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33599 {
33600 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33601 goto out_err;
33602
33603 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33604 {
33605 invalid_category:
33606 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
33607 "%<pointer%>");
33608 goto out_err;
33609 }
33610 id = cp_lexer_peek_token (parser->lexer)->u.value;
33611 p = IDENTIFIER_POINTER (id);
33612
33613 switch (p[0])
33614 {
33615 case 'a':
33616 if (strcmp ("aggregate", p) == 0)
33617 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
33618 else
33619 goto invalid_category;
33620 break;
33621
33622 case 'p':
33623 if (strcmp ("pointer", p) == 0)
33624 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
33625 else
33626 goto invalid_category;
33627 break;
33628
33629 case 's':
33630 if (strcmp ("scalar", p) == 0)
33631 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
33632 else
33633 goto invalid_category;
33634 break;
33635
33636 default:
33637 goto invalid_category;
33638 }
33639
33640 cp_lexer_consume_token (parser->lexer);
33641 }
33642 if (!parens.require_close (parser))
33643 goto out_err;
33644
33645 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33646 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
33647 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33648 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
33649 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
33650 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
33651 {
33652 enum omp_clause_defaultmap_kind cat = category;
33653 location_t loc = OMP_CLAUSE_LOCATION (c);
33654 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
33655 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
33656 p = NULL;
33657 switch (cat)
33658 {
33659 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
33660 p = NULL;
33661 break;
33662 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
33663 p = "aggregate";
33664 break;
33665 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
33666 p = "pointer";
33667 break;
33668 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
33669 p = "scalar";
33670 break;
33671 default:
33672 gcc_unreachable ();
33673 }
33674 if (p)
33675 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
33676 p);
33677 else
33678 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
33679 "category");
33680 break;
33681 }
33682
33683 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
33684 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
33685 OMP_CLAUSE_CHAIN (c) = list;
33686 return c;
33687
33688 out_err:
33689 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33690 /*or_comma=*/false,
33691 /*consume_paren=*/true);
33692 return list;
33693 }
33694
33695 /* OpenMP 2.5:
33696 ordered
33697
33698 OpenMP 4.5:
33699 ordered ( constant-expression ) */
33700
33701 static tree
33702 cp_parser_omp_clause_ordered (cp_parser *parser,
33703 tree list, location_t location)
33704 {
33705 tree c, num = NULL_TREE;
33706 HOST_WIDE_INT n;
33707
33708 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
33709 "ordered", location);
33710
33711 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33712 {
33713 matching_parens parens;
33714 parens.consume_open (parser);
33715
33716 num = cp_parser_constant_expression (parser);
33717
33718 if (!parens.require_close (parser))
33719 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33720 /*or_comma=*/false,
33721 /*consume_paren=*/true);
33722
33723 if (num == error_mark_node)
33724 return list;
33725 num = fold_non_dependent_expr (num);
33726 if (!tree_fits_shwi_p (num)
33727 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33728 || (n = tree_to_shwi (num)) <= 0
33729 || (int) n != n)
33730 {
33731 error_at (location,
33732 "ordered argument needs positive constant integer "
33733 "expression");
33734 return list;
33735 }
33736 }
33737
33738 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
33739 OMP_CLAUSE_ORDERED_EXPR (c) = num;
33740 OMP_CLAUSE_CHAIN (c) = list;
33741 return c;
33742 }
33743
33744 /* OpenMP 2.5:
33745 reduction ( reduction-operator : variable-list )
33746
33747 reduction-operator:
33748 One of: + * - & ^ | && ||
33749
33750 OpenMP 3.1:
33751
33752 reduction-operator:
33753 One of: + * - & ^ | && || min max
33754
33755 OpenMP 4.0:
33756
33757 reduction-operator:
33758 One of: + * - & ^ | && ||
33759 id-expression
33760
33761 OpenMP 5.0:
33762 reduction ( reduction-modifier, reduction-operator : variable-list )
33763 in_reduction ( reduction-operator : variable-list )
33764 task_reduction ( reduction-operator : variable-list ) */
33765
33766 static tree
33767 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
33768 bool is_omp, tree list)
33769 {
33770 enum tree_code code = ERROR_MARK;
33771 tree nlist, c, id = NULL_TREE;
33772 bool task = false;
33773 bool inscan = false;
33774
33775 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33776 return list;
33777
33778 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
33779 {
33780 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
33781 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33782 {
33783 cp_lexer_consume_token (parser->lexer);
33784 cp_lexer_consume_token (parser->lexer);
33785 }
33786 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33787 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33788 {
33789 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33790 const char *p = IDENTIFIER_POINTER (id);
33791 if (strcmp (p, "task") == 0)
33792 task = true;
33793 else if (strcmp (p, "inscan") == 0)
33794 {
33795 inscan = true;
33796 sorry ("%<inscan%> modifier on %<reduction%> clause "
33797 "not supported yet");
33798 }
33799 if (task || inscan)
33800 {
33801 cp_lexer_consume_token (parser->lexer);
33802 cp_lexer_consume_token (parser->lexer);
33803 }
33804 }
33805 }
33806
33807 switch (cp_lexer_peek_token (parser->lexer)->type)
33808 {
33809 case CPP_PLUS: code = PLUS_EXPR; break;
33810 case CPP_MULT: code = MULT_EXPR; break;
33811 case CPP_MINUS: code = MINUS_EXPR; break;
33812 case CPP_AND: code = BIT_AND_EXPR; break;
33813 case CPP_XOR: code = BIT_XOR_EXPR; break;
33814 case CPP_OR: code = BIT_IOR_EXPR; break;
33815 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
33816 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
33817 default: break;
33818 }
33819
33820 if (code != ERROR_MARK)
33821 cp_lexer_consume_token (parser->lexer);
33822 else
33823 {
33824 bool saved_colon_corrects_to_scope_p;
33825 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33826 parser->colon_corrects_to_scope_p = false;
33827 id = cp_parser_id_expression (parser, /*template_p=*/false,
33828 /*check_dependency_p=*/true,
33829 /*template_p=*/NULL,
33830 /*declarator_p=*/false,
33831 /*optional_p=*/false);
33832 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33833 if (identifier_p (id))
33834 {
33835 const char *p = IDENTIFIER_POINTER (id);
33836
33837 if (strcmp (p, "min") == 0)
33838 code = MIN_EXPR;
33839 else if (strcmp (p, "max") == 0)
33840 code = MAX_EXPR;
33841 else if (id == ovl_op_identifier (false, PLUS_EXPR))
33842 code = PLUS_EXPR;
33843 else if (id == ovl_op_identifier (false, MULT_EXPR))
33844 code = MULT_EXPR;
33845 else if (id == ovl_op_identifier (false, MINUS_EXPR))
33846 code = MINUS_EXPR;
33847 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
33848 code = BIT_AND_EXPR;
33849 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
33850 code = BIT_IOR_EXPR;
33851 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
33852 code = BIT_XOR_EXPR;
33853 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
33854 code = TRUTH_ANDIF_EXPR;
33855 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
33856 code = TRUTH_ORIF_EXPR;
33857 id = omp_reduction_id (code, id, NULL_TREE);
33858 tree scope = parser->scope;
33859 if (scope)
33860 id = build_qualified_name (NULL_TREE, scope, id, false);
33861 parser->scope = NULL_TREE;
33862 parser->qualifying_scope = NULL_TREE;
33863 parser->object_scope = NULL_TREE;
33864 }
33865 else
33866 {
33867 error ("invalid reduction-identifier");
33868 resync_fail:
33869 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33870 /*or_comma=*/false,
33871 /*consume_paren=*/true);
33872 return list;
33873 }
33874 }
33875
33876 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33877 goto resync_fail;
33878
33879 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
33880 NULL);
33881 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33882 {
33883 OMP_CLAUSE_REDUCTION_CODE (c) = code;
33884 if (task)
33885 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
33886 else if (inscan)
33887 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
33888 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
33889 }
33890
33891 return nlist;
33892 }
33893
33894 /* OpenMP 2.5:
33895 schedule ( schedule-kind )
33896 schedule ( schedule-kind , expression )
33897
33898 schedule-kind:
33899 static | dynamic | guided | runtime | auto
33900
33901 OpenMP 4.5:
33902 schedule ( schedule-modifier : schedule-kind )
33903 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33904
33905 schedule-modifier:
33906 simd
33907 monotonic
33908 nonmonotonic */
33909
33910 static tree
33911 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
33912 {
33913 tree c, t;
33914 int modifiers = 0, nmodifiers = 0;
33915
33916 matching_parens parens;
33917 if (!parens.require_open (parser))
33918 return list;
33919
33920 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
33921
33922 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33923 {
33924 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33925 const char *p = IDENTIFIER_POINTER (id);
33926 if (strcmp ("simd", p) == 0)
33927 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
33928 else if (strcmp ("monotonic", p) == 0)
33929 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
33930 else if (strcmp ("nonmonotonic", p) == 0)
33931 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
33932 else
33933 break;
33934 cp_lexer_consume_token (parser->lexer);
33935 if (nmodifiers++ == 0
33936 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33937 cp_lexer_consume_token (parser->lexer);
33938 else
33939 {
33940 cp_parser_require (parser, CPP_COLON, RT_COLON);
33941 break;
33942 }
33943 }
33944
33945 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33946 {
33947 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33948 const char *p = IDENTIFIER_POINTER (id);
33949
33950 switch (p[0])
33951 {
33952 case 'd':
33953 if (strcmp ("dynamic", p) != 0)
33954 goto invalid_kind;
33955 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
33956 break;
33957
33958 case 'g':
33959 if (strcmp ("guided", p) != 0)
33960 goto invalid_kind;
33961 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
33962 break;
33963
33964 case 'r':
33965 if (strcmp ("runtime", p) != 0)
33966 goto invalid_kind;
33967 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
33968 break;
33969
33970 default:
33971 goto invalid_kind;
33972 }
33973 }
33974 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33975 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
33976 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33977 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
33978 else
33979 goto invalid_kind;
33980 cp_lexer_consume_token (parser->lexer);
33981
33982 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
33983 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33984 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33985 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33986 {
33987 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33988 "specified");
33989 modifiers = 0;
33990 }
33991
33992 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33993 {
33994 cp_token *token;
33995 cp_lexer_consume_token (parser->lexer);
33996
33997 token = cp_lexer_peek_token (parser->lexer);
33998 t = cp_parser_assignment_expression (parser);
33999
34000 if (t == error_mark_node)
34001 goto resync_fail;
34002 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
34003 error_at (token->location, "schedule %<runtime%> does not take "
34004 "a %<chunk_size%> parameter");
34005 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
34006 error_at (token->location, "schedule %<auto%> does not take "
34007 "a %<chunk_size%> parameter");
34008 else
34009 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
34010
34011 if (!parens.require_close (parser))
34012 goto resync_fail;
34013 }
34014 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34015 goto resync_fail;
34016
34017 OMP_CLAUSE_SCHEDULE_KIND (c)
34018 = (enum omp_clause_schedule_kind)
34019 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
34020
34021 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
34022 OMP_CLAUSE_CHAIN (c) = list;
34023 return c;
34024
34025 invalid_kind:
34026 cp_parser_error (parser, "invalid schedule kind");
34027 resync_fail:
34028 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34029 /*or_comma=*/false,
34030 /*consume_paren=*/true);
34031 return list;
34032 }
34033
34034 /* OpenMP 3.0:
34035 untied */
34036
34037 static tree
34038 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
34039 tree list, location_t location)
34040 {
34041 tree c;
34042
34043 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
34044
34045 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
34046 OMP_CLAUSE_CHAIN (c) = list;
34047 return c;
34048 }
34049
34050 /* OpenMP 4.0:
34051 inbranch
34052 notinbranch */
34053
34054 static tree
34055 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
34056 tree list, location_t location)
34057 {
34058 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
34059 tree c = build_omp_clause (location, code);
34060 OMP_CLAUSE_CHAIN (c) = list;
34061 return c;
34062 }
34063
34064 /* OpenMP 4.0:
34065 parallel
34066 for
34067 sections
34068 taskgroup */
34069
34070 static tree
34071 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
34072 enum omp_clause_code code,
34073 tree list, location_t location)
34074 {
34075 tree c = build_omp_clause (location, code);
34076 OMP_CLAUSE_CHAIN (c) = list;
34077 return c;
34078 }
34079
34080 /* OpenMP 4.5:
34081 nogroup */
34082
34083 static tree
34084 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
34085 tree list, location_t location)
34086 {
34087 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
34088 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
34089 OMP_CLAUSE_CHAIN (c) = list;
34090 return c;
34091 }
34092
34093 /* OpenMP 4.5:
34094 simd
34095 threads */
34096
34097 static tree
34098 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
34099 enum omp_clause_code code,
34100 tree list, location_t location)
34101 {
34102 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
34103 tree c = build_omp_clause (location, code);
34104 OMP_CLAUSE_CHAIN (c) = list;
34105 return c;
34106 }
34107
34108 /* OpenMP 4.0:
34109 num_teams ( expression ) */
34110
34111 static tree
34112 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
34113 location_t location)
34114 {
34115 tree t, c;
34116
34117 matching_parens parens;
34118 if (!parens.require_open (parser))
34119 return list;
34120
34121 t = cp_parser_assignment_expression (parser);
34122
34123 if (t == error_mark_node
34124 || !parens.require_close (parser))
34125 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34126 /*or_comma=*/false,
34127 /*consume_paren=*/true);
34128
34129 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
34130 "num_teams", location);
34131
34132 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
34133 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
34134 OMP_CLAUSE_CHAIN (c) = list;
34135
34136 return c;
34137 }
34138
34139 /* OpenMP 4.0:
34140 thread_limit ( expression ) */
34141
34142 static tree
34143 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
34144 location_t location)
34145 {
34146 tree t, c;
34147
34148 matching_parens parens;
34149 if (!parens.require_open (parser))
34150 return list;
34151
34152 t = cp_parser_assignment_expression (parser);
34153
34154 if (t == error_mark_node
34155 || !parens.require_close (parser))
34156 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34157 /*or_comma=*/false,
34158 /*consume_paren=*/true);
34159
34160 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
34161 "thread_limit", location);
34162
34163 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
34164 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
34165 OMP_CLAUSE_CHAIN (c) = list;
34166
34167 return c;
34168 }
34169
34170 /* OpenMP 4.0:
34171 aligned ( variable-list )
34172 aligned ( variable-list : constant-expression ) */
34173
34174 static tree
34175 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
34176 {
34177 tree nlist, c, alignment = NULL_TREE;
34178 bool colon;
34179
34180 matching_parens parens;
34181 if (!parens.require_open (parser))
34182 return list;
34183
34184 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
34185 &colon);
34186
34187 if (colon)
34188 {
34189 alignment = cp_parser_constant_expression (parser);
34190
34191 if (!parens.require_close (parser))
34192 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34193 /*or_comma=*/false,
34194 /*consume_paren=*/true);
34195
34196 if (alignment == error_mark_node)
34197 alignment = NULL_TREE;
34198 }
34199
34200 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34201 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
34202
34203 return nlist;
34204 }
34205
34206 /* OpenMP 2.5:
34207 lastprivate ( variable-list )
34208
34209 OpenMP 5.0:
34210 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
34211
34212 static tree
34213 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
34214 {
34215 bool conditional = false;
34216
34217 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34218 return list;
34219
34220 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34221 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
34222 {
34223 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34224 const char *p = IDENTIFIER_POINTER (id);
34225
34226 if (strcmp ("conditional", p) == 0)
34227 {
34228 conditional = true;
34229 cp_lexer_consume_token (parser->lexer);
34230 cp_lexer_consume_token (parser->lexer);
34231 }
34232 }
34233
34234 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
34235 list, NULL);
34236
34237 if (conditional)
34238 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34239 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
34240 return nlist;
34241 }
34242
34243 /* OpenMP 4.0:
34244 linear ( variable-list )
34245 linear ( variable-list : expression )
34246
34247 OpenMP 4.5:
34248 linear ( modifier ( variable-list ) )
34249 linear ( modifier ( variable-list ) : expression ) */
34250
34251 static tree
34252 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
34253 bool declare_simd)
34254 {
34255 tree nlist, c, step = integer_one_node;
34256 bool colon;
34257 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
34258
34259 matching_parens parens;
34260 if (!parens.require_open (parser))
34261 return list;
34262
34263 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34264 {
34265 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34266 const char *p = IDENTIFIER_POINTER (id);
34267
34268 if (strcmp ("ref", p) == 0)
34269 kind = OMP_CLAUSE_LINEAR_REF;
34270 else if (strcmp ("val", p) == 0)
34271 kind = OMP_CLAUSE_LINEAR_VAL;
34272 else if (strcmp ("uval", p) == 0)
34273 kind = OMP_CLAUSE_LINEAR_UVAL;
34274 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
34275 cp_lexer_consume_token (parser->lexer);
34276 else
34277 kind = OMP_CLAUSE_LINEAR_DEFAULT;
34278 }
34279
34280 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
34281 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
34282 &colon);
34283 else
34284 {
34285 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
34286 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
34287 if (colon)
34288 cp_parser_require (parser, CPP_COLON, RT_COLON);
34289 else if (!parens.require_close (parser))
34290 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34291 /*or_comma=*/false,
34292 /*consume_paren=*/true);
34293 }
34294
34295 if (colon)
34296 {
34297 step = NULL_TREE;
34298 if (declare_simd
34299 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34300 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
34301 {
34302 cp_token *token = cp_lexer_peek_token (parser->lexer);
34303 cp_parser_parse_tentatively (parser);
34304 step = cp_parser_id_expression (parser, /*template_p=*/false,
34305 /*check_dependency_p=*/true,
34306 /*template_p=*/NULL,
34307 /*declarator_p=*/false,
34308 /*optional_p=*/false);
34309 if (step != error_mark_node)
34310 step = cp_parser_lookup_name_simple (parser, step, token->location);
34311 if (step == error_mark_node)
34312 {
34313 step = NULL_TREE;
34314 cp_parser_abort_tentative_parse (parser);
34315 }
34316 else if (!cp_parser_parse_definitely (parser))
34317 step = NULL_TREE;
34318 }
34319 if (!step)
34320 step = cp_parser_assignment_expression (parser);
34321
34322 if (!parens.require_close (parser))
34323 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34324 /*or_comma=*/false,
34325 /*consume_paren=*/true);
34326
34327 if (step == error_mark_node)
34328 return list;
34329 }
34330
34331 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34332 {
34333 OMP_CLAUSE_LINEAR_STEP (c) = step;
34334 OMP_CLAUSE_LINEAR_KIND (c) = kind;
34335 }
34336
34337 return nlist;
34338 }
34339
34340 /* OpenMP 4.0:
34341 safelen ( constant-expression ) */
34342
34343 static tree
34344 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
34345 location_t location)
34346 {
34347 tree t, c;
34348
34349 matching_parens parens;
34350 if (!parens.require_open (parser))
34351 return list;
34352
34353 t = cp_parser_constant_expression (parser);
34354
34355 if (t == error_mark_node
34356 || !parens.require_close (parser))
34357 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34358 /*or_comma=*/false,
34359 /*consume_paren=*/true);
34360
34361 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
34362
34363 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
34364 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
34365 OMP_CLAUSE_CHAIN (c) = list;
34366
34367 return c;
34368 }
34369
34370 /* OpenMP 4.0:
34371 simdlen ( constant-expression ) */
34372
34373 static tree
34374 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
34375 location_t location)
34376 {
34377 tree t, c;
34378
34379 matching_parens parens;
34380 if (!parens.require_open (parser))
34381 return list;
34382
34383 t = cp_parser_constant_expression (parser);
34384
34385 if (t == error_mark_node
34386 || !parens.require_close (parser))
34387 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34388 /*or_comma=*/false,
34389 /*consume_paren=*/true);
34390
34391 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
34392
34393 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
34394 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
34395 OMP_CLAUSE_CHAIN (c) = list;
34396
34397 return c;
34398 }
34399
34400 /* OpenMP 4.5:
34401 vec:
34402 identifier [+/- integer]
34403 vec , identifier [+/- integer]
34404 */
34405
34406 static tree
34407 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
34408 tree list)
34409 {
34410 tree vec = NULL;
34411
34412 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34413 {
34414 cp_parser_error (parser, "expected identifier");
34415 return list;
34416 }
34417
34418 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34419 {
34420 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
34421 tree t, identifier = cp_parser_identifier (parser);
34422 tree addend = NULL;
34423
34424 if (identifier == error_mark_node)
34425 t = error_mark_node;
34426 else
34427 {
34428 t = cp_parser_lookup_name_simple
34429 (parser, identifier,
34430 cp_lexer_peek_token (parser->lexer)->location);
34431 if (t == error_mark_node)
34432 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
34433 id_loc);
34434 }
34435
34436 bool neg = false;
34437 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
34438 neg = true;
34439 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
34440 {
34441 addend = integer_zero_node;
34442 goto add_to_vector;
34443 }
34444 cp_lexer_consume_token (parser->lexer);
34445
34446 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
34447 {
34448 cp_parser_error (parser, "expected integer");
34449 return list;
34450 }
34451
34452 addend = cp_lexer_peek_token (parser->lexer)->u.value;
34453 if (TREE_CODE (addend) != INTEGER_CST)
34454 {
34455 cp_parser_error (parser, "expected integer");
34456 return list;
34457 }
34458 cp_lexer_consume_token (parser->lexer);
34459
34460 add_to_vector:
34461 if (t != error_mark_node)
34462 {
34463 vec = tree_cons (addend, t, vec);
34464 if (neg)
34465 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
34466 }
34467
34468 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
34469 break;
34470
34471 cp_lexer_consume_token (parser->lexer);
34472 }
34473
34474 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
34475 {
34476 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
34477 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
34478 OMP_CLAUSE_DECL (u) = nreverse (vec);
34479 OMP_CLAUSE_CHAIN (u) = list;
34480 return u;
34481 }
34482 return list;
34483 }
34484
34485 /* OpenMP 5.0:
34486 iterators ( iterators-definition )
34487
34488 iterators-definition:
34489 iterator-specifier
34490 iterator-specifier , iterators-definition
34491
34492 iterator-specifier:
34493 identifier = range-specification
34494 iterator-type identifier = range-specification
34495
34496 range-specification:
34497 begin : end
34498 begin : end : step */
34499
34500 static tree
34501 cp_parser_omp_iterators (cp_parser *parser)
34502 {
34503 tree ret = NULL_TREE, *last = &ret;
34504 cp_lexer_consume_token (parser->lexer);
34505
34506 matching_parens parens;
34507 if (!parens.require_open (parser))
34508 return error_mark_node;
34509
34510 bool saved_colon_corrects_to_scope_p
34511 = parser->colon_corrects_to_scope_p;
34512 bool saved_colon_doesnt_start_class_def_p
34513 = parser->colon_doesnt_start_class_def_p;
34514
34515 do
34516 {
34517 tree iter_type;
34518 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34519 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
34520 iter_type = integer_type_node;
34521 else
34522 {
34523 const char *saved_message
34524 = parser->type_definition_forbidden_message;
34525 parser->type_definition_forbidden_message
34526 = G_("types may not be defined in iterator type");
34527
34528 iter_type = cp_parser_type_id (parser);
34529
34530 parser->type_definition_forbidden_message = saved_message;
34531 }
34532
34533 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34534 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34535 {
34536 cp_parser_error (parser, "expected identifier");
34537 break;
34538 }
34539
34540 tree id = cp_parser_identifier (parser);
34541 if (id == error_mark_node)
34542 break;
34543
34544 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34545 break;
34546
34547 parser->colon_corrects_to_scope_p = false;
34548 parser->colon_doesnt_start_class_def_p = true;
34549 tree begin = cp_parser_assignment_expression (parser);
34550
34551 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34552 break;
34553
34554 tree end = cp_parser_assignment_expression (parser);
34555
34556 tree step = integer_one_node;
34557 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34558 {
34559 cp_lexer_consume_token (parser->lexer);
34560 step = cp_parser_assignment_expression (parser);
34561 }
34562
34563 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
34564 DECL_ARTIFICIAL (iter_var) = 1;
34565 DECL_CONTEXT (iter_var) = current_function_decl;
34566 pushdecl (iter_var);
34567
34568 *last = make_tree_vec (6);
34569 TREE_VEC_ELT (*last, 0) = iter_var;
34570 TREE_VEC_ELT (*last, 1) = begin;
34571 TREE_VEC_ELT (*last, 2) = end;
34572 TREE_VEC_ELT (*last, 3) = step;
34573 last = &TREE_CHAIN (*last);
34574
34575 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34576 {
34577 cp_lexer_consume_token (parser->lexer);
34578 continue;
34579 }
34580 break;
34581 }
34582 while (1);
34583
34584 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34585 parser->colon_doesnt_start_class_def_p
34586 = saved_colon_doesnt_start_class_def_p;
34587
34588 if (!parens.require_close (parser))
34589 cp_parser_skip_to_closing_parenthesis (parser,
34590 /*recovering=*/true,
34591 /*or_comma=*/false,
34592 /*consume_paren=*/true);
34593
34594 return ret ? ret : error_mark_node;
34595 }
34596
34597 /* OpenMP 4.0:
34598 depend ( depend-kind : variable-list )
34599
34600 depend-kind:
34601 in | out | inout
34602
34603 OpenMP 4.5:
34604 depend ( source )
34605
34606 depend ( sink : vec )
34607
34608 OpenMP 5.0:
34609 depend ( depend-modifier , depend-kind: variable-list )
34610
34611 depend-kind:
34612 in | out | inout | mutexinoutset | depobj
34613
34614 depend-modifier:
34615 iterator ( iterators-definition ) */
34616
34617 static tree
34618 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
34619 {
34620 tree nlist, c, iterators = NULL_TREE;
34621 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
34622
34623 matching_parens parens;
34624 if (!parens.require_open (parser))
34625 return list;
34626
34627 do
34628 {
34629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34630 goto invalid_kind;
34631
34632 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34633 const char *p = IDENTIFIER_POINTER (id);
34634
34635 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
34636 {
34637 begin_scope (sk_omp, NULL);
34638 iterators = cp_parser_omp_iterators (parser);
34639 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
34640 continue;
34641 }
34642 if (strcmp ("in", p) == 0)
34643 kind = OMP_CLAUSE_DEPEND_IN;
34644 else if (strcmp ("inout", p) == 0)
34645 kind = OMP_CLAUSE_DEPEND_INOUT;
34646 else if (strcmp ("mutexinoutset", p) == 0)
34647 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
34648 else if (strcmp ("out", p) == 0)
34649 kind = OMP_CLAUSE_DEPEND_OUT;
34650 else if (strcmp ("depobj", p) == 0)
34651 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
34652 else if (strcmp ("sink", p) == 0)
34653 kind = OMP_CLAUSE_DEPEND_SINK;
34654 else if (strcmp ("source", p) == 0)
34655 kind = OMP_CLAUSE_DEPEND_SOURCE;
34656 else
34657 goto invalid_kind;
34658 break;
34659 }
34660 while (1);
34661
34662 cp_lexer_consume_token (parser->lexer);
34663
34664 if (iterators
34665 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
34666 {
34667 poplevel (0, 1, 0);
34668 error_at (loc, "%<iterator%> modifier incompatible with %qs",
34669 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
34670 iterators = NULL_TREE;
34671 }
34672
34673 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
34674 {
34675 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
34676 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34677 OMP_CLAUSE_DECL (c) = NULL_TREE;
34678 OMP_CLAUSE_CHAIN (c) = list;
34679 if (!parens.require_close (parser))
34680 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34681 /*or_comma=*/false,
34682 /*consume_paren=*/true);
34683 return c;
34684 }
34685
34686 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34687 goto resync_fail;
34688
34689 if (kind == OMP_CLAUSE_DEPEND_SINK)
34690 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
34691 else
34692 {
34693 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
34694 list, NULL);
34695
34696 if (iterators)
34697 {
34698 tree block = poplevel (1, 1, 0);
34699 if (iterators == error_mark_node)
34700 iterators = NULL_TREE;
34701 else
34702 TREE_VEC_ELT (iterators, 5) = block;
34703 }
34704
34705 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34706 {
34707 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34708 if (iterators)
34709 OMP_CLAUSE_DECL (c)
34710 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
34711 }
34712 }
34713 return nlist;
34714
34715 invalid_kind:
34716 cp_parser_error (parser, "invalid depend kind");
34717 resync_fail:
34718 if (iterators)
34719 poplevel (0, 1, 0);
34720 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34721 /*or_comma=*/false,
34722 /*consume_paren=*/true);
34723 return list;
34724 }
34725
34726 /* OpenMP 4.0:
34727 map ( map-kind : variable-list )
34728 map ( variable-list )
34729
34730 map-kind:
34731 alloc | to | from | tofrom
34732
34733 OpenMP 4.5:
34734 map-kind:
34735 alloc | to | from | tofrom | release | delete
34736
34737 map ( always [,] map-kind: variable-list ) */
34738
34739 static tree
34740 cp_parser_omp_clause_map (cp_parser *parser, tree list)
34741 {
34742 tree nlist, c;
34743 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
34744 bool always = false;
34745
34746 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34747 return list;
34748
34749 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34750 {
34751 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34752 const char *p = IDENTIFIER_POINTER (id);
34753
34754 if (strcmp ("always", p) == 0)
34755 {
34756 int nth = 2;
34757 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
34758 nth++;
34759 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
34760 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
34761 == RID_DELETE))
34762 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
34763 == CPP_COLON))
34764 {
34765 always = true;
34766 cp_lexer_consume_token (parser->lexer);
34767 if (nth == 3)
34768 cp_lexer_consume_token (parser->lexer);
34769 }
34770 }
34771 }
34772
34773 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34774 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34775 {
34776 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34777 const char *p = IDENTIFIER_POINTER (id);
34778
34779 if (strcmp ("alloc", p) == 0)
34780 kind = GOMP_MAP_ALLOC;
34781 else if (strcmp ("to", p) == 0)
34782 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
34783 else if (strcmp ("from", p) == 0)
34784 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
34785 else if (strcmp ("tofrom", p) == 0)
34786 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
34787 else if (strcmp ("release", p) == 0)
34788 kind = GOMP_MAP_RELEASE;
34789 else
34790 {
34791 cp_parser_error (parser, "invalid map kind");
34792 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34793 /*or_comma=*/false,
34794 /*consume_paren=*/true);
34795 return list;
34796 }
34797 cp_lexer_consume_token (parser->lexer);
34798 cp_lexer_consume_token (parser->lexer);
34799 }
34800 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
34801 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34802 {
34803 kind = GOMP_MAP_DELETE;
34804 cp_lexer_consume_token (parser->lexer);
34805 cp_lexer_consume_token (parser->lexer);
34806 }
34807
34808 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
34809 NULL);
34810
34811 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34812 OMP_CLAUSE_SET_MAP_KIND (c, kind);
34813
34814 return nlist;
34815 }
34816
34817 /* OpenMP 4.0:
34818 device ( expression ) */
34819
34820 static tree
34821 cp_parser_omp_clause_device (cp_parser *parser, tree list,
34822 location_t location)
34823 {
34824 tree t, c;
34825
34826 matching_parens parens;
34827 if (!parens.require_open (parser))
34828 return list;
34829
34830 t = cp_parser_assignment_expression (parser);
34831
34832 if (t == error_mark_node
34833 || !parens.require_close (parser))
34834 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34835 /*or_comma=*/false,
34836 /*consume_paren=*/true);
34837
34838 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
34839 "device", location);
34840
34841 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
34842 OMP_CLAUSE_DEVICE_ID (c) = t;
34843 OMP_CLAUSE_CHAIN (c) = list;
34844
34845 return c;
34846 }
34847
34848 /* OpenMP 4.0:
34849 dist_schedule ( static )
34850 dist_schedule ( static , expression ) */
34851
34852 static tree
34853 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
34854 location_t location)
34855 {
34856 tree c, t;
34857
34858 matching_parens parens;
34859 if (!parens.require_open (parser))
34860 return list;
34861
34862 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
34863
34864 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34865 goto invalid_kind;
34866 cp_lexer_consume_token (parser->lexer);
34867
34868 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34869 {
34870 cp_lexer_consume_token (parser->lexer);
34871
34872 t = cp_parser_assignment_expression (parser);
34873
34874 if (t == error_mark_node)
34875 goto resync_fail;
34876 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
34877
34878 if (!parens.require_close (parser))
34879 goto resync_fail;
34880 }
34881 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34882 goto resync_fail;
34883
34884 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
34885 location);
34886 OMP_CLAUSE_CHAIN (c) = list;
34887 return c;
34888
34889 invalid_kind:
34890 cp_parser_error (parser, "invalid dist_schedule kind");
34891 resync_fail:
34892 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34893 /*or_comma=*/false,
34894 /*consume_paren=*/true);
34895 return list;
34896 }
34897
34898 /* OpenMP 4.0:
34899 proc_bind ( proc-bind-kind )
34900
34901 proc-bind-kind:
34902 master | close | spread */
34903
34904 static tree
34905 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
34906 location_t location)
34907 {
34908 tree c;
34909 enum omp_clause_proc_bind_kind kind;
34910
34911 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34912 return list;
34913
34914 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34915 {
34916 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34917 const char *p = IDENTIFIER_POINTER (id);
34918
34919 if (strcmp ("master", p) == 0)
34920 kind = OMP_CLAUSE_PROC_BIND_MASTER;
34921 else if (strcmp ("close", p) == 0)
34922 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
34923 else if (strcmp ("spread", p) == 0)
34924 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
34925 else
34926 goto invalid_kind;
34927 }
34928 else
34929 goto invalid_kind;
34930
34931 cp_lexer_consume_token (parser->lexer);
34932 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34933 goto resync_fail;
34934
34935 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
34936 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
34937 location);
34938 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
34939 OMP_CLAUSE_CHAIN (c) = list;
34940 return c;
34941
34942 invalid_kind:
34943 cp_parser_error (parser, "invalid depend kind");
34944 resync_fail:
34945 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34946 /*or_comma=*/false,
34947 /*consume_paren=*/true);
34948 return list;
34949 }
34950
34951 /* OpenACC:
34952 async [( int-expr )] */
34953
34954 static tree
34955 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
34956 {
34957 tree c, t;
34958 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34959
34960 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
34961
34962 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34963 {
34964 matching_parens parens;
34965 parens.consume_open (parser);
34966
34967 t = cp_parser_expression (parser);
34968 if (t == error_mark_node
34969 || !parens.require_close (parser))
34970 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34971 /*or_comma=*/false,
34972 /*consume_paren=*/true);
34973 }
34974
34975 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
34976
34977 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
34978 OMP_CLAUSE_ASYNC_EXPR (c) = t;
34979 OMP_CLAUSE_CHAIN (c) = list;
34980 list = c;
34981
34982 return list;
34983 }
34984
34985 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34986 is a bitmask in MASK. Return the list of clauses found. */
34987
34988 static tree
34989 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
34990 const char *where, cp_token *pragma_tok,
34991 bool finish_p = true)
34992 {
34993 tree clauses = NULL;
34994 bool first = true;
34995
34996 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34997 {
34998 location_t here;
34999 pragma_omp_clause c_kind;
35000 omp_clause_code code;
35001 const char *c_name;
35002 tree prev = clauses;
35003
35004 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35005 cp_lexer_consume_token (parser->lexer);
35006
35007 here = cp_lexer_peek_token (parser->lexer)->location;
35008 c_kind = cp_parser_omp_clause_name (parser);
35009
35010 switch (c_kind)
35011 {
35012 case PRAGMA_OACC_CLAUSE_ASYNC:
35013 clauses = cp_parser_oacc_clause_async (parser, clauses);
35014 c_name = "async";
35015 break;
35016 case PRAGMA_OACC_CLAUSE_AUTO:
35017 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
35018 clauses);
35019 c_name = "auto";
35020 break;
35021 case PRAGMA_OACC_CLAUSE_COLLAPSE:
35022 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
35023 c_name = "collapse";
35024 break;
35025 case PRAGMA_OACC_CLAUSE_COPY:
35026 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35027 c_name = "copy";
35028 break;
35029 case PRAGMA_OACC_CLAUSE_COPYIN:
35030 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35031 c_name = "copyin";
35032 break;
35033 case PRAGMA_OACC_CLAUSE_COPYOUT:
35034 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35035 c_name = "copyout";
35036 break;
35037 case PRAGMA_OACC_CLAUSE_CREATE:
35038 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35039 c_name = "create";
35040 break;
35041 case PRAGMA_OACC_CLAUSE_DELETE:
35042 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35043 c_name = "delete";
35044 break;
35045 case PRAGMA_OMP_CLAUSE_DEFAULT:
35046 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
35047 c_name = "default";
35048 break;
35049 case PRAGMA_OACC_CLAUSE_DEVICE:
35050 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35051 c_name = "device";
35052 break;
35053 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
35054 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
35055 c_name = "deviceptr";
35056 break;
35057 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
35058 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35059 c_name = "device_resident";
35060 break;
35061 case PRAGMA_OACC_CLAUSE_FINALIZE:
35062 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
35063 clauses);
35064 c_name = "finalize";
35065 break;
35066 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
35067 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
35068 clauses);
35069 c_name = "firstprivate";
35070 break;
35071 case PRAGMA_OACC_CLAUSE_GANG:
35072 c_name = "gang";
35073 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
35074 c_name, clauses);
35075 break;
35076 case PRAGMA_OACC_CLAUSE_HOST:
35077 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35078 c_name = "host";
35079 break;
35080 case PRAGMA_OACC_CLAUSE_IF:
35081 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
35082 c_name = "if";
35083 break;
35084 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
35085 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
35086 clauses);
35087 c_name = "if_present";
35088 break;
35089 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
35090 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
35091 clauses);
35092 c_name = "independent";
35093 break;
35094 case PRAGMA_OACC_CLAUSE_LINK:
35095 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35096 c_name = "link";
35097 break;
35098 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
35099 code = OMP_CLAUSE_NUM_GANGS;
35100 c_name = "num_gangs";
35101 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35102 clauses);
35103 break;
35104 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
35105 c_name = "num_workers";
35106 code = OMP_CLAUSE_NUM_WORKERS;
35107 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35108 clauses);
35109 break;
35110 case PRAGMA_OACC_CLAUSE_PRESENT:
35111 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35112 c_name = "present";
35113 break;
35114 case PRAGMA_OACC_CLAUSE_PRIVATE:
35115 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
35116 clauses);
35117 c_name = "private";
35118 break;
35119 case PRAGMA_OACC_CLAUSE_REDUCTION:
35120 clauses
35121 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
35122 false, clauses);
35123 c_name = "reduction";
35124 break;
35125 case PRAGMA_OACC_CLAUSE_SEQ:
35126 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
35127 clauses);
35128 c_name = "seq";
35129 break;
35130 case PRAGMA_OACC_CLAUSE_TILE:
35131 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
35132 c_name = "tile";
35133 break;
35134 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
35135 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
35136 clauses);
35137 c_name = "use_device";
35138 break;
35139 case PRAGMA_OACC_CLAUSE_VECTOR:
35140 c_name = "vector";
35141 clauses = cp_parser_oacc_shape_clause (parser, here,
35142 OMP_CLAUSE_VECTOR,
35143 c_name, clauses);
35144 break;
35145 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
35146 c_name = "vector_length";
35147 code = OMP_CLAUSE_VECTOR_LENGTH;
35148 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35149 clauses);
35150 break;
35151 case PRAGMA_OACC_CLAUSE_WAIT:
35152 clauses = cp_parser_oacc_clause_wait (parser, clauses);
35153 c_name = "wait";
35154 break;
35155 case PRAGMA_OACC_CLAUSE_WORKER:
35156 c_name = "worker";
35157 clauses = cp_parser_oacc_shape_clause (parser, here,
35158 OMP_CLAUSE_WORKER,
35159 c_name, clauses);
35160 break;
35161 default:
35162 cp_parser_error (parser, "expected %<#pragma acc%> clause");
35163 goto saw_error;
35164 }
35165
35166 first = false;
35167
35168 if (((mask >> c_kind) & 1) == 0)
35169 {
35170 /* Remove the invalid clause(s) from the list to avoid
35171 confusing the rest of the compiler. */
35172 clauses = prev;
35173 error_at (here, "%qs is not valid for %qs", c_name, where);
35174 }
35175 }
35176
35177 saw_error:
35178 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35179
35180 if (finish_p)
35181 return finish_omp_clauses (clauses, C_ORT_ACC);
35182
35183 return clauses;
35184 }
35185
35186 /* Parse all OpenMP clauses. The set clauses allowed by the directive
35187 is a bitmask in MASK. Return the list of clauses found; the result
35188 of clause default goes in *pdefault. */
35189
35190 static tree
35191 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
35192 const char *where, cp_token *pragma_tok,
35193 bool finish_p = true)
35194 {
35195 tree clauses = NULL;
35196 bool first = true;
35197 cp_token *token = NULL;
35198
35199 /* Don't create location wrapper nodes within OpenMP clauses. */
35200 auto_suppress_location_wrappers sentinel;
35201
35202 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35203 {
35204 pragma_omp_clause c_kind;
35205 const char *c_name;
35206 tree prev = clauses;
35207
35208 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35209 cp_lexer_consume_token (parser->lexer);
35210
35211 token = cp_lexer_peek_token (parser->lexer);
35212 c_kind = cp_parser_omp_clause_name (parser);
35213
35214 switch (c_kind)
35215 {
35216 case PRAGMA_OMP_CLAUSE_COLLAPSE:
35217 clauses = cp_parser_omp_clause_collapse (parser, clauses,
35218 token->location);
35219 c_name = "collapse";
35220 break;
35221 case PRAGMA_OMP_CLAUSE_COPYIN:
35222 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
35223 c_name = "copyin";
35224 break;
35225 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
35226 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
35227 clauses);
35228 c_name = "copyprivate";
35229 break;
35230 case PRAGMA_OMP_CLAUSE_DEFAULT:
35231 clauses = cp_parser_omp_clause_default (parser, clauses,
35232 token->location, false);
35233 c_name = "default";
35234 break;
35235 case PRAGMA_OMP_CLAUSE_FINAL:
35236 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
35237 c_name = "final";
35238 break;
35239 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
35240 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
35241 clauses);
35242 c_name = "firstprivate";
35243 break;
35244 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
35245 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
35246 token->location);
35247 c_name = "grainsize";
35248 break;
35249 case PRAGMA_OMP_CLAUSE_HINT:
35250 clauses = cp_parser_omp_clause_hint (parser, clauses,
35251 token->location);
35252 c_name = "hint";
35253 break;
35254 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
35255 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
35256 token->location);
35257 c_name = "defaultmap";
35258 break;
35259 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
35260 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
35261 clauses);
35262 c_name = "use_device_ptr";
35263 break;
35264 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
35265 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
35266 clauses);
35267 c_name = "is_device_ptr";
35268 break;
35269 case PRAGMA_OMP_CLAUSE_IF:
35270 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
35271 true);
35272 c_name = "if";
35273 break;
35274 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
35275 clauses
35276 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
35277 true, clauses);
35278 c_name = "in_reduction";
35279 break;
35280 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
35281 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
35282 c_name = "lastprivate";
35283 break;
35284 case PRAGMA_OMP_CLAUSE_MERGEABLE:
35285 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
35286 token->location);
35287 c_name = "mergeable";
35288 break;
35289 case PRAGMA_OMP_CLAUSE_NOWAIT:
35290 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
35291 c_name = "nowait";
35292 break;
35293 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
35294 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
35295 token->location);
35296 c_name = "num_tasks";
35297 break;
35298 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
35299 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
35300 token->location);
35301 c_name = "num_threads";
35302 break;
35303 case PRAGMA_OMP_CLAUSE_ORDERED:
35304 clauses = cp_parser_omp_clause_ordered (parser, clauses,
35305 token->location);
35306 c_name = "ordered";
35307 break;
35308 case PRAGMA_OMP_CLAUSE_PRIORITY:
35309 clauses = cp_parser_omp_clause_priority (parser, clauses,
35310 token->location);
35311 c_name = "priority";
35312 break;
35313 case PRAGMA_OMP_CLAUSE_PRIVATE:
35314 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
35315 clauses);
35316 c_name = "private";
35317 break;
35318 case PRAGMA_OMP_CLAUSE_REDUCTION:
35319 clauses
35320 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
35321 true, clauses);
35322 c_name = "reduction";
35323 break;
35324 case PRAGMA_OMP_CLAUSE_SCHEDULE:
35325 clauses = cp_parser_omp_clause_schedule (parser, clauses,
35326 token->location);
35327 c_name = "schedule";
35328 break;
35329 case PRAGMA_OMP_CLAUSE_SHARED:
35330 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
35331 clauses);
35332 c_name = "shared";
35333 break;
35334 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
35335 clauses
35336 = cp_parser_omp_clause_reduction (parser,
35337 OMP_CLAUSE_TASK_REDUCTION,
35338 true, clauses);
35339 c_name = "task_reduction";
35340 break;
35341 case PRAGMA_OMP_CLAUSE_UNTIED:
35342 clauses = cp_parser_omp_clause_untied (parser, clauses,
35343 token->location);
35344 c_name = "untied";
35345 break;
35346 case PRAGMA_OMP_CLAUSE_INBRANCH:
35347 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
35348 clauses, token->location);
35349 c_name = "inbranch";
35350 break;
35351 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
35352 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
35353 clauses);
35354 c_name = "nontemporal";
35355 break;
35356 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
35357 clauses = cp_parser_omp_clause_branch (parser,
35358 OMP_CLAUSE_NOTINBRANCH,
35359 clauses, token->location);
35360 c_name = "notinbranch";
35361 break;
35362 case PRAGMA_OMP_CLAUSE_PARALLEL:
35363 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
35364 clauses, token->location);
35365 c_name = "parallel";
35366 if (!first)
35367 {
35368 clause_not_first:
35369 error_at (token->location, "%qs must be the first clause of %qs",
35370 c_name, where);
35371 clauses = prev;
35372 }
35373 break;
35374 case PRAGMA_OMP_CLAUSE_FOR:
35375 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
35376 clauses, token->location);
35377 c_name = "for";
35378 if (!first)
35379 goto clause_not_first;
35380 break;
35381 case PRAGMA_OMP_CLAUSE_SECTIONS:
35382 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
35383 clauses, token->location);
35384 c_name = "sections";
35385 if (!first)
35386 goto clause_not_first;
35387 break;
35388 case PRAGMA_OMP_CLAUSE_TASKGROUP:
35389 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
35390 clauses, token->location);
35391 c_name = "taskgroup";
35392 if (!first)
35393 goto clause_not_first;
35394 break;
35395 case PRAGMA_OMP_CLAUSE_LINK:
35396 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
35397 c_name = "to";
35398 break;
35399 case PRAGMA_OMP_CLAUSE_TO:
35400 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
35401 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35402 clauses);
35403 else
35404 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
35405 c_name = "to";
35406 break;
35407 case PRAGMA_OMP_CLAUSE_FROM:
35408 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
35409 c_name = "from";
35410 break;
35411 case PRAGMA_OMP_CLAUSE_UNIFORM:
35412 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
35413 clauses);
35414 c_name = "uniform";
35415 break;
35416 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
35417 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
35418 token->location);
35419 c_name = "num_teams";
35420 break;
35421 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
35422 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
35423 token->location);
35424 c_name = "thread_limit";
35425 break;
35426 case PRAGMA_OMP_CLAUSE_ALIGNED:
35427 clauses = cp_parser_omp_clause_aligned (parser, clauses);
35428 c_name = "aligned";
35429 break;
35430 case PRAGMA_OMP_CLAUSE_LINEAR:
35431 {
35432 bool declare_simd = false;
35433 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
35434 declare_simd = true;
35435 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
35436 }
35437 c_name = "linear";
35438 break;
35439 case PRAGMA_OMP_CLAUSE_DEPEND:
35440 clauses = cp_parser_omp_clause_depend (parser, clauses,
35441 token->location);
35442 c_name = "depend";
35443 break;
35444 case PRAGMA_OMP_CLAUSE_MAP:
35445 clauses = cp_parser_omp_clause_map (parser, clauses);
35446 c_name = "map";
35447 break;
35448 case PRAGMA_OMP_CLAUSE_DEVICE:
35449 clauses = cp_parser_omp_clause_device (parser, clauses,
35450 token->location);
35451 c_name = "device";
35452 break;
35453 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
35454 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
35455 token->location);
35456 c_name = "dist_schedule";
35457 break;
35458 case PRAGMA_OMP_CLAUSE_PROC_BIND:
35459 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
35460 token->location);
35461 c_name = "proc_bind";
35462 break;
35463 case PRAGMA_OMP_CLAUSE_SAFELEN:
35464 clauses = cp_parser_omp_clause_safelen (parser, clauses,
35465 token->location);
35466 c_name = "safelen";
35467 break;
35468 case PRAGMA_OMP_CLAUSE_SIMDLEN:
35469 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
35470 token->location);
35471 c_name = "simdlen";
35472 break;
35473 case PRAGMA_OMP_CLAUSE_NOGROUP:
35474 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
35475 token->location);
35476 c_name = "nogroup";
35477 break;
35478 case PRAGMA_OMP_CLAUSE_THREADS:
35479 clauses
35480 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
35481 clauses, token->location);
35482 c_name = "threads";
35483 break;
35484 case PRAGMA_OMP_CLAUSE_SIMD:
35485 clauses
35486 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
35487 clauses, token->location);
35488 c_name = "simd";
35489 break;
35490 default:
35491 cp_parser_error (parser, "expected %<#pragma omp%> clause");
35492 goto saw_error;
35493 }
35494
35495 first = false;
35496
35497 if (((mask >> c_kind) & 1) == 0)
35498 {
35499 /* Remove the invalid clause(s) from the list to avoid
35500 confusing the rest of the compiler. */
35501 clauses = prev;
35502 error_at (token->location, "%qs is not valid for %qs", c_name, where);
35503 }
35504 }
35505 saw_error:
35506 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35507 if (finish_p)
35508 {
35509 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
35510 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
35511 else
35512 return finish_omp_clauses (clauses, C_ORT_OMP);
35513 }
35514 return clauses;
35515 }
35516
35517 /* OpenMP 2.5:
35518 structured-block:
35519 statement
35520
35521 In practice, we're also interested in adding the statement to an
35522 outer node. So it is convenient if we work around the fact that
35523 cp_parser_statement calls add_stmt. */
35524
35525 static unsigned
35526 cp_parser_begin_omp_structured_block (cp_parser *parser)
35527 {
35528 unsigned save = parser->in_statement;
35529
35530 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35531 This preserves the "not within loop or switch" style error messages
35532 for nonsense cases like
35533 void foo() {
35534 #pragma omp single
35535 break;
35536 }
35537 */
35538 if (parser->in_statement)
35539 parser->in_statement = IN_OMP_BLOCK;
35540
35541 return save;
35542 }
35543
35544 static void
35545 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
35546 {
35547 parser->in_statement = save;
35548 }
35549
35550 static tree
35551 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
35552 {
35553 tree stmt = begin_omp_structured_block ();
35554 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35555
35556 cp_parser_statement (parser, NULL_TREE, false, if_p);
35557
35558 cp_parser_end_omp_structured_block (parser, save);
35559 return finish_omp_structured_block (stmt);
35560 }
35561
35562 /* OpenMP 2.5:
35563 # pragma omp atomic new-line
35564 expression-stmt
35565
35566 expression-stmt:
35567 x binop= expr | x++ | ++x | x-- | --x
35568 binop:
35569 +, *, -, /, &, ^, |, <<, >>
35570
35571 where x is an lvalue expression with scalar type.
35572
35573 OpenMP 3.1:
35574 # pragma omp atomic new-line
35575 update-stmt
35576
35577 # pragma omp atomic read new-line
35578 read-stmt
35579
35580 # pragma omp atomic write new-line
35581 write-stmt
35582
35583 # pragma omp atomic update new-line
35584 update-stmt
35585
35586 # pragma omp atomic capture new-line
35587 capture-stmt
35588
35589 # pragma omp atomic capture new-line
35590 capture-block
35591
35592 read-stmt:
35593 v = x
35594 write-stmt:
35595 x = expr
35596 update-stmt:
35597 expression-stmt | x = x binop expr
35598 capture-stmt:
35599 v = expression-stmt
35600 capture-block:
35601 { v = x; update-stmt; } | { update-stmt; v = x; }
35602
35603 OpenMP 4.0:
35604 update-stmt:
35605 expression-stmt | x = x binop expr | x = expr binop x
35606 capture-stmt:
35607 v = update-stmt
35608 capture-block:
35609 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35610
35611 where x and v are lvalue expressions with scalar type. */
35612
35613 static void
35614 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
35615 {
35616 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
35617 tree rhs1 = NULL_TREE, orig_lhs;
35618 location_t loc = pragma_tok->location;
35619 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
35620 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
35621 bool structured_block = false;
35622 bool first = true;
35623 tree clauses = NULL_TREE;
35624
35625 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35626 {
35627 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35628 cp_lexer_consume_token (parser->lexer);
35629
35630 first = false;
35631
35632 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35633 {
35634 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35635 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
35636 const char *p = IDENTIFIER_POINTER (id);
35637 enum tree_code new_code = ERROR_MARK;
35638 enum omp_memory_order new_memory_order
35639 = OMP_MEMORY_ORDER_UNSPECIFIED;
35640
35641 if (!strcmp (p, "read"))
35642 new_code = OMP_ATOMIC_READ;
35643 else if (!strcmp (p, "write"))
35644 new_code = NOP_EXPR;
35645 else if (!strcmp (p, "update"))
35646 new_code = OMP_ATOMIC;
35647 else if (!strcmp (p, "capture"))
35648 new_code = OMP_ATOMIC_CAPTURE_NEW;
35649 else if (!strcmp (p, "seq_cst"))
35650 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35651 else if (!strcmp (p, "acq_rel"))
35652 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35653 else if (!strcmp (p, "release"))
35654 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
35655 else if (!strcmp (p, "acquire"))
35656 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35657 else if (!strcmp (p, "relaxed"))
35658 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
35659 else if (!strcmp (p, "hint"))
35660 {
35661 cp_lexer_consume_token (parser->lexer);
35662 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
35663 continue;
35664 }
35665 else
35666 {
35667 p = NULL;
35668 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
35669 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35670 "%<release%>, %<relaxed%> or %<hint%> clause");
35671 }
35672 if (p)
35673 {
35674 if (new_code != ERROR_MARK)
35675 {
35676 if (code != ERROR_MARK)
35677 error_at (cloc, "too many atomic clauses");
35678 else
35679 code = new_code;
35680 }
35681 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35682 {
35683 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35684 error_at (cloc, "too many memory order clauses");
35685 else
35686 memory_order = new_memory_order;
35687 }
35688 cp_lexer_consume_token (parser->lexer);
35689 continue;
35690 }
35691 }
35692 break;
35693 }
35694 cp_parser_require_pragma_eol (parser, pragma_tok);
35695
35696 if (code == ERROR_MARK)
35697 code = OMP_ATOMIC;
35698 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
35699 {
35700 omp_requires_mask
35701 = (enum omp_requires) (omp_requires_mask
35702 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
35703 switch ((enum omp_memory_order)
35704 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
35705 {
35706 case OMP_MEMORY_ORDER_UNSPECIFIED:
35707 case OMP_MEMORY_ORDER_RELAXED:
35708 memory_order = OMP_MEMORY_ORDER_RELAXED;
35709 break;
35710 case OMP_MEMORY_ORDER_SEQ_CST:
35711 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35712 break;
35713 case OMP_MEMORY_ORDER_ACQ_REL:
35714 switch (code)
35715 {
35716 case OMP_ATOMIC_READ:
35717 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35718 break;
35719 case NOP_EXPR: /* atomic write */
35720 case OMP_ATOMIC:
35721 memory_order = OMP_MEMORY_ORDER_RELEASE;
35722 break;
35723 default:
35724 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35725 break;
35726 }
35727 break;
35728 default:
35729 gcc_unreachable ();
35730 }
35731 }
35732 else
35733 switch (code)
35734 {
35735 case OMP_ATOMIC_READ:
35736 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35737 || memory_order == OMP_MEMORY_ORDER_RELEASE)
35738 {
35739 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
35740 "%<acq_rel%> or %<release%> clauses");
35741 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35742 }
35743 break;
35744 case NOP_EXPR: /* atomic write */
35745 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35746 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35747 {
35748 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
35749 "%<acq_rel%> or %<acquire%> clauses");
35750 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35751 }
35752 break;
35753 case OMP_ATOMIC:
35754 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35755 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35756 {
35757 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
35758 "%<acq_rel%> or %<acquire%> clauses");
35759 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35760 }
35761 break;
35762 default:
35763 break;
35764 }
35765
35766 switch (code)
35767 {
35768 case OMP_ATOMIC_READ:
35769 case NOP_EXPR: /* atomic write */
35770 v = cp_parser_unary_expression (parser);
35771 if (v == error_mark_node)
35772 goto saw_error;
35773 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35774 goto saw_error;
35775 if (code == NOP_EXPR)
35776 lhs = cp_parser_expression (parser);
35777 else
35778 lhs = cp_parser_unary_expression (parser);
35779 if (lhs == error_mark_node)
35780 goto saw_error;
35781 if (code == NOP_EXPR)
35782 {
35783 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35784 opcode. */
35785 code = OMP_ATOMIC;
35786 rhs = lhs;
35787 lhs = v;
35788 v = NULL_TREE;
35789 }
35790 goto done;
35791 case OMP_ATOMIC_CAPTURE_NEW:
35792 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35793 {
35794 cp_lexer_consume_token (parser->lexer);
35795 structured_block = true;
35796 }
35797 else
35798 {
35799 v = cp_parser_unary_expression (parser);
35800 if (v == error_mark_node)
35801 goto saw_error;
35802 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35803 goto saw_error;
35804 }
35805 default:
35806 break;
35807 }
35808
35809 restart:
35810 lhs = cp_parser_unary_expression (parser);
35811 orig_lhs = lhs;
35812 switch (TREE_CODE (lhs))
35813 {
35814 case ERROR_MARK:
35815 goto saw_error;
35816
35817 case POSTINCREMENT_EXPR:
35818 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35819 code = OMP_ATOMIC_CAPTURE_OLD;
35820 /* FALLTHROUGH */
35821 case PREINCREMENT_EXPR:
35822 lhs = TREE_OPERAND (lhs, 0);
35823 opcode = PLUS_EXPR;
35824 rhs = integer_one_node;
35825 break;
35826
35827 case POSTDECREMENT_EXPR:
35828 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35829 code = OMP_ATOMIC_CAPTURE_OLD;
35830 /* FALLTHROUGH */
35831 case PREDECREMENT_EXPR:
35832 lhs = TREE_OPERAND (lhs, 0);
35833 opcode = MINUS_EXPR;
35834 rhs = integer_one_node;
35835 break;
35836
35837 case COMPOUND_EXPR:
35838 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
35839 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
35840 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
35841 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
35842 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35843 (TREE_OPERAND (lhs, 1), 0), 0)))
35844 == BOOLEAN_TYPE)
35845 /* Undo effects of boolean_increment for post {in,de}crement. */
35846 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
35847 /* FALLTHRU */
35848 case MODIFY_EXPR:
35849 if (TREE_CODE (lhs) == MODIFY_EXPR
35850 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
35851 {
35852 /* Undo effects of boolean_increment. */
35853 if (integer_onep (TREE_OPERAND (lhs, 1)))
35854 {
35855 /* This is pre or post increment. */
35856 rhs = TREE_OPERAND (lhs, 1);
35857 lhs = TREE_OPERAND (lhs, 0);
35858 opcode = NOP_EXPR;
35859 if (code == OMP_ATOMIC_CAPTURE_NEW
35860 && !structured_block
35861 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
35862 code = OMP_ATOMIC_CAPTURE_OLD;
35863 break;
35864 }
35865 }
35866 /* FALLTHRU */
35867 default:
35868 switch (cp_lexer_peek_token (parser->lexer)->type)
35869 {
35870 case CPP_MULT_EQ:
35871 opcode = MULT_EXPR;
35872 break;
35873 case CPP_DIV_EQ:
35874 opcode = TRUNC_DIV_EXPR;
35875 break;
35876 case CPP_PLUS_EQ:
35877 opcode = PLUS_EXPR;
35878 break;
35879 case CPP_MINUS_EQ:
35880 opcode = MINUS_EXPR;
35881 break;
35882 case CPP_LSHIFT_EQ:
35883 opcode = LSHIFT_EXPR;
35884 break;
35885 case CPP_RSHIFT_EQ:
35886 opcode = RSHIFT_EXPR;
35887 break;
35888 case CPP_AND_EQ:
35889 opcode = BIT_AND_EXPR;
35890 break;
35891 case CPP_OR_EQ:
35892 opcode = BIT_IOR_EXPR;
35893 break;
35894 case CPP_XOR_EQ:
35895 opcode = BIT_XOR_EXPR;
35896 break;
35897 case CPP_EQ:
35898 enum cp_parser_prec oprec;
35899 cp_token *token;
35900 cp_lexer_consume_token (parser->lexer);
35901 cp_parser_parse_tentatively (parser);
35902 rhs1 = cp_parser_simple_cast_expression (parser);
35903 if (rhs1 == error_mark_node)
35904 {
35905 cp_parser_abort_tentative_parse (parser);
35906 cp_parser_simple_cast_expression (parser);
35907 goto saw_error;
35908 }
35909 token = cp_lexer_peek_token (parser->lexer);
35910 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
35911 {
35912 cp_parser_abort_tentative_parse (parser);
35913 cp_parser_parse_tentatively (parser);
35914 rhs = cp_parser_binary_expression (parser, false, true,
35915 PREC_NOT_OPERATOR, NULL);
35916 if (rhs == error_mark_node)
35917 {
35918 cp_parser_abort_tentative_parse (parser);
35919 cp_parser_binary_expression (parser, false, true,
35920 PREC_NOT_OPERATOR, NULL);
35921 goto saw_error;
35922 }
35923 switch (TREE_CODE (rhs))
35924 {
35925 case MULT_EXPR:
35926 case TRUNC_DIV_EXPR:
35927 case RDIV_EXPR:
35928 case PLUS_EXPR:
35929 case MINUS_EXPR:
35930 case LSHIFT_EXPR:
35931 case RSHIFT_EXPR:
35932 case BIT_AND_EXPR:
35933 case BIT_IOR_EXPR:
35934 case BIT_XOR_EXPR:
35935 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
35936 {
35937 if (cp_parser_parse_definitely (parser))
35938 {
35939 opcode = TREE_CODE (rhs);
35940 rhs1 = TREE_OPERAND (rhs, 0);
35941 rhs = TREE_OPERAND (rhs, 1);
35942 goto stmt_done;
35943 }
35944 else
35945 goto saw_error;
35946 }
35947 break;
35948 default:
35949 break;
35950 }
35951 cp_parser_abort_tentative_parse (parser);
35952 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
35953 {
35954 rhs = cp_parser_expression (parser);
35955 if (rhs == error_mark_node)
35956 goto saw_error;
35957 opcode = NOP_EXPR;
35958 rhs1 = NULL_TREE;
35959 goto stmt_done;
35960 }
35961 cp_parser_error (parser,
35962 "invalid form of %<#pragma omp atomic%>");
35963 goto saw_error;
35964 }
35965 if (!cp_parser_parse_definitely (parser))
35966 goto saw_error;
35967 switch (token->type)
35968 {
35969 case CPP_SEMICOLON:
35970 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35971 {
35972 code = OMP_ATOMIC_CAPTURE_OLD;
35973 v = lhs;
35974 lhs = NULL_TREE;
35975 lhs1 = rhs1;
35976 rhs1 = NULL_TREE;
35977 cp_lexer_consume_token (parser->lexer);
35978 goto restart;
35979 }
35980 else if (structured_block)
35981 {
35982 opcode = NOP_EXPR;
35983 rhs = rhs1;
35984 rhs1 = NULL_TREE;
35985 goto stmt_done;
35986 }
35987 cp_parser_error (parser,
35988 "invalid form of %<#pragma omp atomic%>");
35989 goto saw_error;
35990 case CPP_MULT:
35991 opcode = MULT_EXPR;
35992 break;
35993 case CPP_DIV:
35994 opcode = TRUNC_DIV_EXPR;
35995 break;
35996 case CPP_PLUS:
35997 opcode = PLUS_EXPR;
35998 break;
35999 case CPP_MINUS:
36000 opcode = MINUS_EXPR;
36001 break;
36002 case CPP_LSHIFT:
36003 opcode = LSHIFT_EXPR;
36004 break;
36005 case CPP_RSHIFT:
36006 opcode = RSHIFT_EXPR;
36007 break;
36008 case CPP_AND:
36009 opcode = BIT_AND_EXPR;
36010 break;
36011 case CPP_OR:
36012 opcode = BIT_IOR_EXPR;
36013 break;
36014 case CPP_XOR:
36015 opcode = BIT_XOR_EXPR;
36016 break;
36017 default:
36018 cp_parser_error (parser,
36019 "invalid operator for %<#pragma omp atomic%>");
36020 goto saw_error;
36021 }
36022 oprec = TOKEN_PRECEDENCE (token);
36023 gcc_assert (oprec != PREC_NOT_OPERATOR);
36024 if (commutative_tree_code (opcode))
36025 oprec = (enum cp_parser_prec) (oprec - 1);
36026 cp_lexer_consume_token (parser->lexer);
36027 rhs = cp_parser_binary_expression (parser, false, false,
36028 oprec, NULL);
36029 if (rhs == error_mark_node)
36030 goto saw_error;
36031 goto stmt_done;
36032 /* FALLTHROUGH */
36033 default:
36034 cp_parser_error (parser,
36035 "invalid operator for %<#pragma omp atomic%>");
36036 goto saw_error;
36037 }
36038 cp_lexer_consume_token (parser->lexer);
36039
36040 rhs = cp_parser_expression (parser);
36041 if (rhs == error_mark_node)
36042 goto saw_error;
36043 break;
36044 }
36045 stmt_done:
36046 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
36047 {
36048 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
36049 goto saw_error;
36050 v = cp_parser_unary_expression (parser);
36051 if (v == error_mark_node)
36052 goto saw_error;
36053 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
36054 goto saw_error;
36055 lhs1 = cp_parser_unary_expression (parser);
36056 if (lhs1 == error_mark_node)
36057 goto saw_error;
36058 }
36059 if (structured_block)
36060 {
36061 cp_parser_consume_semicolon_at_end_of_statement (parser);
36062 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
36063 }
36064 done:
36065 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36066 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
36067 rhs1, clauses, memory_order);
36068 if (!structured_block)
36069 cp_parser_consume_semicolon_at_end_of_statement (parser);
36070 return;
36071
36072 saw_error:
36073 cp_parser_skip_to_end_of_block_or_statement (parser);
36074 if (structured_block)
36075 {
36076 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36077 cp_lexer_consume_token (parser->lexer);
36078 else if (code == OMP_ATOMIC_CAPTURE_NEW)
36079 {
36080 cp_parser_skip_to_end_of_block_or_statement (parser);
36081 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36082 cp_lexer_consume_token (parser->lexer);
36083 }
36084 }
36085 }
36086
36087
36088 /* OpenMP 2.5:
36089 # pragma omp barrier new-line */
36090
36091 static void
36092 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
36093 {
36094 cp_parser_require_pragma_eol (parser, pragma_tok);
36095 finish_omp_barrier ();
36096 }
36097
36098 /* OpenMP 2.5:
36099 # pragma omp critical [(name)] new-line
36100 structured-block
36101
36102 OpenMP 4.5:
36103 # pragma omp critical [(name) [hint(expression)]] new-line
36104 structured-block */
36105
36106 #define OMP_CRITICAL_CLAUSE_MASK \
36107 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
36108
36109 static tree
36110 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36111 {
36112 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
36113
36114 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36115 {
36116 matching_parens parens;
36117 parens.consume_open (parser);
36118
36119 name = cp_parser_identifier (parser);
36120
36121 if (name == error_mark_node
36122 || !parens.require_close (parser))
36123 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36124 /*or_comma=*/false,
36125 /*consume_paren=*/true);
36126 if (name == error_mark_node)
36127 name = NULL;
36128
36129 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
36130 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
36131 cp_lexer_consume_token (parser->lexer);
36132
36133 clauses = cp_parser_omp_all_clauses (parser,
36134 OMP_CRITICAL_CLAUSE_MASK,
36135 "#pragma omp critical", pragma_tok);
36136 }
36137 else
36138 cp_parser_require_pragma_eol (parser, pragma_tok);
36139
36140 stmt = cp_parser_omp_structured_block (parser, if_p);
36141 return c_finish_omp_critical (input_location, stmt, name, clauses);
36142 }
36143
36144 /* OpenMP 5.0:
36145 # pragma omp depobj ( depobj ) depobj-clause new-line
36146
36147 depobj-clause:
36148 depend (dependence-type : locator)
36149 destroy
36150 update (dependence-type)
36151
36152 dependence-type:
36153 in
36154 out
36155 inout
36156 mutexinout */
36157
36158 static void
36159 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
36160 {
36161 location_t loc = pragma_tok->location;
36162 matching_parens parens;
36163 if (!parens.require_open (parser))
36164 {
36165 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36166 return;
36167 }
36168
36169 tree depobj = cp_parser_assignment_expression (parser);
36170
36171 if (!parens.require_close (parser))
36172 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36173 /*or_comma=*/false,
36174 /*consume_paren=*/true);
36175
36176 tree clause = NULL_TREE;
36177 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
36178 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
36179 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36180 {
36181 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36182 const char *p = IDENTIFIER_POINTER (id);
36183
36184 cp_lexer_consume_token (parser->lexer);
36185 if (!strcmp ("depend", p))
36186 {
36187 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
36188 if (clause)
36189 clause = finish_omp_clauses (clause, C_ORT_OMP);
36190 if (!clause)
36191 clause = error_mark_node;
36192 }
36193 else if (!strcmp ("destroy", p))
36194 kind = OMP_CLAUSE_DEPEND_LAST;
36195 else if (!strcmp ("update", p))
36196 {
36197 matching_parens c_parens;
36198 if (c_parens.require_open (parser))
36199 {
36200 location_t c2_loc
36201 = cp_lexer_peek_token (parser->lexer)->location;
36202 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36203 {
36204 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
36205 const char *p2 = IDENTIFIER_POINTER (id2);
36206
36207 cp_lexer_consume_token (parser->lexer);
36208 if (!strcmp ("in", p2))
36209 kind = OMP_CLAUSE_DEPEND_IN;
36210 else if (!strcmp ("out", p2))
36211 kind = OMP_CLAUSE_DEPEND_OUT;
36212 else if (!strcmp ("inout", p2))
36213 kind = OMP_CLAUSE_DEPEND_INOUT;
36214 else if (!strcmp ("mutexinoutset", p2))
36215 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
36216 }
36217 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
36218 {
36219 clause = error_mark_node;
36220 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
36221 "%<mutexinoutset%>");
36222 }
36223 if (!c_parens.require_close (parser))
36224 cp_parser_skip_to_closing_parenthesis (parser,
36225 /*recovering=*/true,
36226 /*or_comma=*/false,
36227 /*consume_paren=*/true);
36228 }
36229 else
36230 clause = error_mark_node;
36231 }
36232 }
36233 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
36234 {
36235 clause = error_mark_node;
36236 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
36237 }
36238 cp_parser_require_pragma_eol (parser, pragma_tok);
36239
36240 finish_omp_depobj (loc, depobj, kind, clause);
36241 }
36242
36243
36244 /* OpenMP 2.5:
36245 # pragma omp flush flush-vars[opt] new-line
36246
36247 flush-vars:
36248 ( variable-list )
36249
36250 OpenMP 5.0:
36251 # pragma omp flush memory-order-clause new-line */
36252
36253 static void
36254 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
36255 {
36256 enum memmodel mo = MEMMODEL_LAST;
36257 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36258 {
36259 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36260 const char *p = IDENTIFIER_POINTER (id);
36261 if (!strcmp (p, "acq_rel"))
36262 mo = MEMMODEL_ACQ_REL;
36263 else if (!strcmp (p, "release"))
36264 mo = MEMMODEL_RELEASE;
36265 else if (!strcmp (p, "acquire"))
36266 mo = MEMMODEL_ACQUIRE;
36267 else
36268 error_at (cp_lexer_peek_token (parser->lexer)->location,
36269 "expected %<acq_rel%>, %<release%> or %<acquire%>");
36270 cp_lexer_consume_token (parser->lexer);
36271 }
36272 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36273 {
36274 if (mo != MEMMODEL_LAST)
36275 error_at (cp_lexer_peek_token (parser->lexer)->location,
36276 "%<flush%> list specified together with memory order "
36277 "clause");
36278 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36279 }
36280 cp_parser_require_pragma_eol (parser, pragma_tok);
36281
36282 finish_omp_flush (mo);
36283 }
36284
36285 /* Helper function, to parse omp for increment expression. */
36286
36287 static tree
36288 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
36289 {
36290 tree cond = cp_parser_binary_expression (parser, false, true,
36291 PREC_NOT_OPERATOR, NULL);
36292 if (cond == error_mark_node
36293 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36294 {
36295 cp_parser_skip_to_end_of_statement (parser);
36296 return error_mark_node;
36297 }
36298
36299 switch (TREE_CODE (cond))
36300 {
36301 case GT_EXPR:
36302 case GE_EXPR:
36303 case LT_EXPR:
36304 case LE_EXPR:
36305 break;
36306 case NE_EXPR:
36307 if (code != OACC_LOOP)
36308 break;
36309 gcc_fallthrough ();
36310 default:
36311 return error_mark_node;
36312 }
36313
36314 /* If decl is an iterator, preserve LHS and RHS of the relational
36315 expr until finish_omp_for. */
36316 if (decl
36317 && (type_dependent_expression_p (decl)
36318 || CLASS_TYPE_P (TREE_TYPE (decl))))
36319 return cond;
36320
36321 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
36322 TREE_CODE (cond),
36323 TREE_OPERAND (cond, 0), ERROR_MARK,
36324 TREE_OPERAND (cond, 1), ERROR_MARK,
36325 /*overload=*/NULL, tf_warning_or_error);
36326 }
36327
36328 /* Helper function, to parse omp for increment expression. */
36329
36330 static tree
36331 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
36332 {
36333 cp_token *token = cp_lexer_peek_token (parser->lexer);
36334 enum tree_code op;
36335 tree lhs, rhs;
36336 cp_id_kind idk;
36337 bool decl_first;
36338
36339 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36340 {
36341 op = (token->type == CPP_PLUS_PLUS
36342 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
36343 cp_lexer_consume_token (parser->lexer);
36344 lhs = cp_parser_simple_cast_expression (parser);
36345 if (lhs != decl
36346 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36347 return error_mark_node;
36348 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36349 }
36350
36351 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
36352 if (lhs != decl
36353 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36354 return error_mark_node;
36355
36356 token = cp_lexer_peek_token (parser->lexer);
36357 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36358 {
36359 op = (token->type == CPP_PLUS_PLUS
36360 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
36361 cp_lexer_consume_token (parser->lexer);
36362 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36363 }
36364
36365 op = cp_parser_assignment_operator_opt (parser);
36366 if (op == ERROR_MARK)
36367 return error_mark_node;
36368
36369 if (op != NOP_EXPR)
36370 {
36371 rhs = cp_parser_assignment_expression (parser);
36372 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
36373 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36374 }
36375
36376 lhs = cp_parser_binary_expression (parser, false, false,
36377 PREC_ADDITIVE_EXPRESSION, NULL);
36378 token = cp_lexer_peek_token (parser->lexer);
36379 decl_first = (lhs == decl
36380 || (processing_template_decl && cp_tree_equal (lhs, decl)));
36381 if (decl_first)
36382 lhs = NULL_TREE;
36383 if (token->type != CPP_PLUS
36384 && token->type != CPP_MINUS)
36385 return error_mark_node;
36386
36387 do
36388 {
36389 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
36390 cp_lexer_consume_token (parser->lexer);
36391 rhs = cp_parser_binary_expression (parser, false, false,
36392 PREC_ADDITIVE_EXPRESSION, NULL);
36393 token = cp_lexer_peek_token (parser->lexer);
36394 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
36395 {
36396 if (lhs == NULL_TREE)
36397 {
36398 if (op == PLUS_EXPR)
36399 lhs = rhs;
36400 else
36401 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
36402 tf_warning_or_error);
36403 }
36404 else
36405 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
36406 ERROR_MARK, NULL, tf_warning_or_error);
36407 }
36408 }
36409 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
36410
36411 if (!decl_first)
36412 {
36413 if ((rhs != decl
36414 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
36415 || op == MINUS_EXPR)
36416 return error_mark_node;
36417 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
36418 }
36419 else
36420 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
36421
36422 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36423 }
36424
36425 /* Parse the initialization statement of an OpenMP for loop.
36426
36427 Return true if the resulting construct should have an
36428 OMP_CLAUSE_PRIVATE added to it. */
36429
36430 static tree
36431 cp_parser_omp_for_loop_init (cp_parser *parser,
36432 tree &this_pre_body,
36433 vec<tree, va_gc> *&for_block,
36434 tree &init,
36435 tree &orig_init,
36436 tree &decl,
36437 tree &real_decl)
36438 {
36439 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36440 return NULL_TREE;
36441
36442 tree add_private_clause = NULL_TREE;
36443
36444 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
36445
36446 init-expr:
36447 var = lb
36448 integer-type var = lb
36449 random-access-iterator-type var = lb
36450 pointer-type var = lb
36451 */
36452 cp_decl_specifier_seq type_specifiers;
36453
36454 /* First, try to parse as an initialized declaration. See
36455 cp_parser_condition, from whence the bulk of this is copied. */
36456
36457 cp_parser_parse_tentatively (parser);
36458 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
36459 /*is_declaration=*/true,
36460 /*is_trailing_return=*/false,
36461 &type_specifiers);
36462 if (cp_parser_parse_definitely (parser))
36463 {
36464 /* If parsing a type specifier seq succeeded, then this
36465 MUST be a initialized declaration. */
36466 tree asm_specification, attributes;
36467 cp_declarator *declarator;
36468
36469 declarator = cp_parser_declarator (parser,
36470 CP_PARSER_DECLARATOR_NAMED,
36471 CP_PARSER_FLAGS_NONE,
36472 /*ctor_dtor_or_conv_p=*/NULL,
36473 /*parenthesized_p=*/NULL,
36474 /*member_p=*/false,
36475 /*friend_p=*/false,
36476 /*static_p=*/false);
36477 attributes = cp_parser_attributes_opt (parser);
36478 asm_specification = cp_parser_asm_specification_opt (parser);
36479
36480 if (declarator == cp_error_declarator)
36481 cp_parser_skip_to_end_of_statement (parser);
36482
36483 else
36484 {
36485 tree pushed_scope, auto_node;
36486
36487 decl = start_decl (declarator, &type_specifiers,
36488 SD_INITIALIZED, attributes,
36489 /*prefix_attributes=*/NULL_TREE,
36490 &pushed_scope);
36491
36492 auto_node = type_uses_auto (TREE_TYPE (decl));
36493 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36494 {
36495 if (cp_lexer_next_token_is (parser->lexer,
36496 CPP_OPEN_PAREN))
36497 error ("parenthesized initialization is not allowed in "
36498 "OpenMP %<for%> loop");
36499 else
36500 /* Trigger an error. */
36501 cp_parser_require (parser, CPP_EQ, RT_EQ);
36502
36503 init = error_mark_node;
36504 cp_parser_skip_to_end_of_statement (parser);
36505 }
36506 else if (CLASS_TYPE_P (TREE_TYPE (decl))
36507 || type_dependent_expression_p (decl)
36508 || auto_node)
36509 {
36510 bool is_direct_init, is_non_constant_init;
36511
36512 init = cp_parser_initializer (parser,
36513 &is_direct_init,
36514 &is_non_constant_init);
36515
36516 if (auto_node)
36517 {
36518 TREE_TYPE (decl)
36519 = do_auto_deduction (TREE_TYPE (decl), init,
36520 auto_node);
36521
36522 if (!CLASS_TYPE_P (TREE_TYPE (decl))
36523 && !type_dependent_expression_p (decl))
36524 goto non_class;
36525 }
36526
36527 cp_finish_decl (decl, init, !is_non_constant_init,
36528 asm_specification,
36529 LOOKUP_ONLYCONVERTING);
36530 orig_init = init;
36531 if (CLASS_TYPE_P (TREE_TYPE (decl)))
36532 {
36533 vec_safe_push (for_block, this_pre_body);
36534 init = NULL_TREE;
36535 }
36536 else
36537 {
36538 init = pop_stmt_list (this_pre_body);
36539 if (init && TREE_CODE (init) == STATEMENT_LIST)
36540 {
36541 tree_stmt_iterator i = tsi_start (init);
36542 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36543 while (!tsi_end_p (i))
36544 {
36545 tree t = tsi_stmt (i);
36546 if (TREE_CODE (t) == DECL_EXPR
36547 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
36548 {
36549 tsi_delink (&i);
36550 vec_safe_push (for_block, t);
36551 continue;
36552 }
36553 break;
36554 }
36555 if (tsi_one_before_end_p (i))
36556 {
36557 tree t = tsi_stmt (i);
36558 tsi_delink (&i);
36559 free_stmt_list (init);
36560 init = t;
36561 }
36562 }
36563 }
36564 this_pre_body = NULL_TREE;
36565 }
36566 else
36567 {
36568 /* Consume '='. */
36569 cp_lexer_consume_token (parser->lexer);
36570 init = cp_parser_assignment_expression (parser);
36571
36572 non_class:
36573 if (TYPE_REF_P (TREE_TYPE (decl)))
36574 init = error_mark_node;
36575 else
36576 cp_finish_decl (decl, NULL_TREE,
36577 /*init_const_expr_p=*/false,
36578 asm_specification,
36579 LOOKUP_ONLYCONVERTING);
36580 }
36581
36582 if (pushed_scope)
36583 pop_scope (pushed_scope);
36584 }
36585 }
36586 else
36587 {
36588 cp_id_kind idk;
36589 /* If parsing a type specifier sequence failed, then
36590 this MUST be a simple expression. */
36591 cp_parser_parse_tentatively (parser);
36592 decl = cp_parser_primary_expression (parser, false, false,
36593 false, &idk);
36594 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
36595 if (!cp_parser_error_occurred (parser)
36596 && decl
36597 && (TREE_CODE (decl) == COMPONENT_REF
36598 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
36599 {
36600 cp_parser_abort_tentative_parse (parser);
36601 cp_parser_parse_tentatively (parser);
36602 cp_token *token = cp_lexer_peek_token (parser->lexer);
36603 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
36604 /*check_dependency_p=*/true,
36605 /*template_p=*/NULL,
36606 /*declarator_p=*/false,
36607 /*optional_p=*/false);
36608 if (name != error_mark_node
36609 && last_tok == cp_lexer_peek_token (parser->lexer))
36610 {
36611 decl = cp_parser_lookup_name_simple (parser, name,
36612 token->location);
36613 if (TREE_CODE (decl) == FIELD_DECL)
36614 add_private_clause = omp_privatize_field (decl, false);
36615 }
36616 cp_parser_abort_tentative_parse (parser);
36617 cp_parser_parse_tentatively (parser);
36618 decl = cp_parser_primary_expression (parser, false, false,
36619 false, &idk);
36620 }
36621 if (!cp_parser_error_occurred (parser)
36622 && decl
36623 && DECL_P (decl)
36624 && CLASS_TYPE_P (TREE_TYPE (decl)))
36625 {
36626 tree rhs;
36627
36628 cp_parser_parse_definitely (parser);
36629 cp_parser_require (parser, CPP_EQ, RT_EQ);
36630 rhs = cp_parser_assignment_expression (parser);
36631 orig_init = rhs;
36632 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
36633 decl, NOP_EXPR,
36634 rhs,
36635 tf_warning_or_error));
36636 if (!add_private_clause)
36637 add_private_clause = decl;
36638 }
36639 else
36640 {
36641 decl = NULL;
36642 cp_parser_abort_tentative_parse (parser);
36643 init = cp_parser_expression (parser);
36644 if (init)
36645 {
36646 if (TREE_CODE (init) == MODIFY_EXPR
36647 || TREE_CODE (init) == MODOP_EXPR)
36648 real_decl = TREE_OPERAND (init, 0);
36649 }
36650 }
36651 }
36652 return add_private_clause;
36653 }
36654
36655 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36656
36657 void
36658 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
36659 tree &decl, tree &orig_decl, tree &init,
36660 tree &orig_init, tree &cond, tree &incr)
36661 {
36662 tree begin, end, range_temp_decl = NULL_TREE;
36663 tree iter_type, begin_expr, end_expr;
36664
36665 if (processing_template_decl)
36666 {
36667 if (check_for_bare_parameter_packs (init))
36668 init = error_mark_node;
36669 if (!type_dependent_expression_p (init)
36670 /* do_auto_deduction doesn't mess with template init-lists. */
36671 && !BRACE_ENCLOSED_INITIALIZER_P (init))
36672 {
36673 tree d = decl;
36674 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
36675 {
36676 tree v = DECL_VALUE_EXPR (decl);
36677 if (TREE_CODE (v) == ARRAY_REF
36678 && VAR_P (TREE_OPERAND (v, 0))
36679 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36680 d = TREE_OPERAND (v, 0);
36681 }
36682 do_range_for_auto_deduction (d, init);
36683 }
36684 cond = global_namespace;
36685 incr = NULL_TREE;
36686 orig_init = init;
36687 if (this_pre_body)
36688 this_pre_body = pop_stmt_list (this_pre_body);
36689 return;
36690 }
36691
36692 init = mark_lvalue_use (init);
36693
36694 if (decl == error_mark_node || init == error_mark_node)
36695 /* If an error happened previously do nothing or else a lot of
36696 unhelpful errors would be issued. */
36697 begin_expr = end_expr = iter_type = error_mark_node;
36698 else
36699 {
36700 tree range_temp;
36701
36702 if (VAR_P (init)
36703 && array_of_runtime_bound_p (TREE_TYPE (init)))
36704 /* Can't bind a reference to an array of runtime bound. */
36705 range_temp = init;
36706 else
36707 {
36708 range_temp = build_range_temp (init);
36709 DECL_NAME (range_temp) = NULL_TREE;
36710 pushdecl (range_temp);
36711 cp_finish_decl (range_temp, init,
36712 /*is_constant_init*/false, NULL_TREE,
36713 LOOKUP_ONLYCONVERTING);
36714 range_temp_decl = range_temp;
36715 range_temp = convert_from_reference (range_temp);
36716 }
36717 iter_type = cp_parser_perform_range_for_lookup (range_temp,
36718 &begin_expr, &end_expr);
36719 }
36720
36721 tree end_iter_type = iter_type;
36722 if (cxx_dialect >= cxx17)
36723 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
36724 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
36725 TREE_USED (end) = 1;
36726 DECL_ARTIFICIAL (end) = 1;
36727 pushdecl (end);
36728 cp_finish_decl (end, end_expr,
36729 /*is_constant_init*/false, NULL_TREE,
36730 LOOKUP_ONLYCONVERTING);
36731
36732 /* The new for initialization statement. */
36733 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
36734 TREE_USED (begin) = 1;
36735 DECL_ARTIFICIAL (begin) = 1;
36736 pushdecl (begin);
36737 orig_init = init;
36738 if (CLASS_TYPE_P (iter_type))
36739 init = NULL_TREE;
36740 else
36741 {
36742 init = begin_expr;
36743 begin_expr = NULL_TREE;
36744 }
36745 cp_finish_decl (begin, begin_expr,
36746 /*is_constant_init*/false, NULL_TREE,
36747 LOOKUP_ONLYCONVERTING);
36748
36749 /* The new for condition. */
36750 if (CLASS_TYPE_P (iter_type))
36751 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
36752 else
36753 cond = build_x_binary_op (input_location, NE_EXPR,
36754 begin, ERROR_MARK,
36755 end, ERROR_MARK,
36756 NULL, tf_warning_or_error);
36757
36758 /* The new increment expression. */
36759 if (CLASS_TYPE_P (iter_type))
36760 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
36761 else
36762 incr = finish_unary_op_expr (input_location,
36763 PREINCREMENT_EXPR, begin,
36764 tf_warning_or_error);
36765
36766 orig_decl = decl;
36767 decl = begin;
36768 if (for_block)
36769 {
36770 vec_safe_push (for_block, this_pre_body);
36771 this_pre_body = NULL_TREE;
36772 }
36773
36774 tree decomp_first_name = NULL_TREE;
36775 unsigned decomp_cnt = 0;
36776 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
36777 {
36778 tree v = DECL_VALUE_EXPR (orig_decl);
36779 if (TREE_CODE (v) == ARRAY_REF
36780 && VAR_P (TREE_OPERAND (v, 0))
36781 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36782 {
36783 tree d = orig_decl;
36784 orig_decl = TREE_OPERAND (v, 0);
36785 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
36786 decomp_first_name = d;
36787 }
36788 }
36789
36790 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
36791 if (auto_node)
36792 {
36793 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36794 tf_none);
36795 if (!error_operand_p (t))
36796 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
36797 t, auto_node);
36798 }
36799
36800 tree v = make_tree_vec (decomp_cnt + 3);
36801 TREE_VEC_ELT (v, 0) = range_temp_decl;
36802 TREE_VEC_ELT (v, 1) = end;
36803 TREE_VEC_ELT (v, 2) = orig_decl;
36804 for (unsigned i = 0; i < decomp_cnt; i++)
36805 {
36806 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
36807 decomp_first_name = DECL_CHAIN (decomp_first_name);
36808 }
36809 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
36810 }
36811
36812 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36813 inside of the collapsed body. */
36814
36815 void
36816 cp_finish_omp_range_for (tree orig, tree begin)
36817 {
36818 gcc_assert (TREE_CODE (orig) == TREE_LIST
36819 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
36820 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
36821 tree decomp_first_name = NULL_TREE;
36822 unsigned int decomp_cnt = 0;
36823
36824 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36825 {
36826 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
36827 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
36828 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
36829 }
36830
36831 /* The declaration is initialized with *__begin inside the loop body. */
36832 cp_finish_decl (decl,
36833 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36834 tf_warning_or_error),
36835 /*is_constant_init*/false, NULL_TREE,
36836 LOOKUP_ONLYCONVERTING);
36837 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36838 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
36839 }
36840
36841 /* Parse the restricted form of the for statement allowed by OpenMP. */
36842
36843 static tree
36844 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
36845 tree *cclauses, bool *if_p)
36846 {
36847 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
36848 tree orig_decl;
36849 tree real_decl, initv, condv, incrv, declv, orig_declv;
36850 tree this_pre_body, cl, ordered_cl = NULL_TREE;
36851 location_t loc_first;
36852 bool collapse_err = false;
36853 int i, collapse = 1, ordered = 0, count, nbraces = 0;
36854 vec<tree, va_gc> *for_block = make_tree_vector ();
36855 auto_vec<tree, 4> orig_inits;
36856 bool tiling = false;
36857
36858 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
36859 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
36860 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
36861 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
36862 {
36863 tiling = true;
36864 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
36865 }
36866 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
36867 && OMP_CLAUSE_ORDERED_EXPR (cl))
36868 {
36869 ordered_cl = cl;
36870 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
36871 }
36872
36873 if (ordered && ordered < collapse)
36874 {
36875 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36876 "%<ordered%> clause parameter is less than %<collapse%>");
36877 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
36878 = build_int_cst (NULL_TREE, collapse);
36879 ordered = collapse;
36880 }
36881 if (ordered)
36882 {
36883 for (tree *pc = &clauses; *pc; )
36884 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
36885 {
36886 error_at (OMP_CLAUSE_LOCATION (*pc),
36887 "%<linear%> clause may not be specified together "
36888 "with %<ordered%> clause with a parameter");
36889 *pc = OMP_CLAUSE_CHAIN (*pc);
36890 }
36891 else
36892 pc = &OMP_CLAUSE_CHAIN (*pc);
36893 }
36894
36895 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
36896 count = ordered ? ordered : collapse;
36897
36898 declv = make_tree_vec (count);
36899 initv = make_tree_vec (count);
36900 condv = make_tree_vec (count);
36901 incrv = make_tree_vec (count);
36902 orig_declv = NULL_TREE;
36903
36904 loc_first = cp_lexer_peek_token (parser->lexer)->location;
36905
36906 for (i = 0; i < count; i++)
36907 {
36908 int bracecount = 0;
36909 tree add_private_clause = NULL_TREE;
36910 location_t loc;
36911
36912 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36913 {
36914 if (!collapse_err)
36915 cp_parser_error (parser, "for statement expected");
36916 return NULL;
36917 }
36918 loc = cp_lexer_consume_token (parser->lexer)->location;
36919
36920 /* Don't create location wrapper nodes within an OpenMP "for"
36921 statement. */
36922 auto_suppress_location_wrappers sentinel;
36923
36924 matching_parens parens;
36925 if (!parens.require_open (parser))
36926 return NULL;
36927
36928 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
36929 this_pre_body = push_stmt_list ();
36930
36931 if (code != OACC_LOOP && cxx_dialect >= cxx11)
36932 {
36933 /* Save tokens so that we can put them back. */
36934 cp_lexer_save_tokens (parser->lexer);
36935
36936 /* Look for ':' that is not nested in () or {}. */
36937 bool is_range_for
36938 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
36939 /*recovering=*/false,
36940 CPP_COLON,
36941 /*consume_paren=*/
36942 false) == -1);
36943
36944 /* Roll back the tokens we skipped. */
36945 cp_lexer_rollback_tokens (parser->lexer);
36946
36947 if (is_range_for)
36948 {
36949 bool saved_colon_corrects_to_scope_p
36950 = parser->colon_corrects_to_scope_p;
36951
36952 /* A colon is used in range-based for. */
36953 parser->colon_corrects_to_scope_p = false;
36954
36955 /* Parse the declaration. */
36956 cp_parser_simple_declaration (parser,
36957 /*function_definition_allowed_p=*/
36958 false, &decl);
36959 parser->colon_corrects_to_scope_p
36960 = saved_colon_corrects_to_scope_p;
36961
36962 cp_parser_require (parser, CPP_COLON, RT_COLON);
36963
36964 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
36965 false, 0, true);
36966
36967 cp_convert_omp_range_for (this_pre_body, for_block, decl,
36968 orig_decl, init, orig_init,
36969 cond, incr);
36970 if (this_pre_body)
36971 {
36972 if (pre_body)
36973 {
36974 tree t = pre_body;
36975 pre_body = push_stmt_list ();
36976 add_stmt (t);
36977 add_stmt (this_pre_body);
36978 pre_body = pop_stmt_list (pre_body);
36979 }
36980 else
36981 pre_body = this_pre_body;
36982 }
36983
36984 if (ordered_cl)
36985 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36986 "%<ordered%> clause with parameter on "
36987 "range-based %<for%> loop");
36988
36989 goto parse_close_paren;
36990 }
36991 }
36992
36993 add_private_clause
36994 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
36995 init, orig_init, decl, real_decl);
36996
36997 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36998 if (this_pre_body)
36999 {
37000 this_pre_body = pop_stmt_list (this_pre_body);
37001 if (pre_body)
37002 {
37003 tree t = pre_body;
37004 pre_body = push_stmt_list ();
37005 add_stmt (t);
37006 add_stmt (this_pre_body);
37007 pre_body = pop_stmt_list (pre_body);
37008 }
37009 else
37010 pre_body = this_pre_body;
37011 }
37012
37013 if (decl)
37014 real_decl = decl;
37015 if (cclauses != NULL
37016 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
37017 && real_decl != NULL_TREE)
37018 {
37019 tree *c;
37020 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
37021 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
37022 && OMP_CLAUSE_DECL (*c) == real_decl)
37023 {
37024 error_at (loc, "iteration variable %qD"
37025 " should not be firstprivate", real_decl);
37026 *c = OMP_CLAUSE_CHAIN (*c);
37027 }
37028 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
37029 && OMP_CLAUSE_DECL (*c) == real_decl)
37030 {
37031 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
37032 tree l = *c;
37033 *c = OMP_CLAUSE_CHAIN (*c);
37034 if (code == OMP_SIMD)
37035 {
37036 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37037 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
37038 }
37039 else
37040 {
37041 OMP_CLAUSE_CHAIN (l) = clauses;
37042 clauses = l;
37043 }
37044 add_private_clause = NULL_TREE;
37045 }
37046 else
37047 {
37048 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
37049 && OMP_CLAUSE_DECL (*c) == real_decl)
37050 add_private_clause = NULL_TREE;
37051 c = &OMP_CLAUSE_CHAIN (*c);
37052 }
37053 }
37054
37055 if (add_private_clause)
37056 {
37057 tree c;
37058 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
37059 {
37060 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
37061 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
37062 && OMP_CLAUSE_DECL (c) == decl)
37063 break;
37064 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
37065 && OMP_CLAUSE_DECL (c) == decl)
37066 error_at (loc, "iteration variable %qD "
37067 "should not be firstprivate",
37068 decl);
37069 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
37070 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
37071 && OMP_CLAUSE_DECL (c) == decl)
37072 error_at (loc, "iteration variable %qD should not be reduction",
37073 decl);
37074 }
37075 if (c == NULL)
37076 {
37077 if (code != OMP_SIMD)
37078 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
37079 else if (collapse == 1)
37080 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37081 else
37082 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
37083 OMP_CLAUSE_DECL (c) = add_private_clause;
37084 c = finish_omp_clauses (c, C_ORT_OMP);
37085 if (c)
37086 {
37087 OMP_CLAUSE_CHAIN (c) = clauses;
37088 clauses = c;
37089 /* For linear, signal that we need to fill up
37090 the so far unknown linear step. */
37091 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
37092 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
37093 }
37094 }
37095 }
37096
37097 cond = NULL;
37098 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37099 cond = cp_parser_omp_for_cond (parser, decl, code);
37100 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37101
37102 incr = NULL;
37103 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
37104 {
37105 /* If decl is an iterator, preserve the operator on decl
37106 until finish_omp_for. */
37107 if (real_decl
37108 && ((processing_template_decl
37109 && (TREE_TYPE (real_decl) == NULL_TREE
37110 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
37111 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
37112 incr = cp_parser_omp_for_incr (parser, real_decl);
37113 else
37114 incr = cp_parser_expression (parser);
37115 if (!EXPR_HAS_LOCATION (incr))
37116 protected_set_expr_location (incr, input_location);
37117 }
37118
37119 parse_close_paren:
37120 if (!parens.require_close (parser))
37121 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37122 /*or_comma=*/false,
37123 /*consume_paren=*/true);
37124
37125 TREE_VEC_ELT (declv, i) = decl;
37126 TREE_VEC_ELT (initv, i) = init;
37127 TREE_VEC_ELT (condv, i) = cond;
37128 TREE_VEC_ELT (incrv, i) = incr;
37129 if (orig_init)
37130 {
37131 orig_inits.safe_grow_cleared (i + 1);
37132 orig_inits[i] = orig_init;
37133 }
37134 if (orig_decl)
37135 {
37136 if (!orig_declv)
37137 orig_declv = copy_node (declv);
37138 TREE_VEC_ELT (orig_declv, i) = orig_decl;
37139 }
37140 else if (orig_declv)
37141 TREE_VEC_ELT (orig_declv, i) = decl;
37142
37143 if (i == count - 1)
37144 break;
37145
37146 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
37147 in between the collapsed for loops to be still considered perfectly
37148 nested. Hopefully the final version clarifies this.
37149 For now handle (multiple) {'s and empty statements. */
37150 cp_parser_parse_tentatively (parser);
37151 for (;;)
37152 {
37153 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37154 break;
37155 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
37156 {
37157 cp_lexer_consume_token (parser->lexer);
37158 bracecount++;
37159 }
37160 else if (bracecount
37161 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37162 cp_lexer_consume_token (parser->lexer);
37163 else
37164 {
37165 loc = cp_lexer_peek_token (parser->lexer)->location;
37166 error_at (loc, "not enough for loops to collapse");
37167 collapse_err = true;
37168 cp_parser_abort_tentative_parse (parser);
37169 declv = NULL_TREE;
37170 break;
37171 }
37172 }
37173
37174 if (declv)
37175 {
37176 cp_parser_parse_definitely (parser);
37177 nbraces += bracecount;
37178 }
37179 }
37180
37181 if (nbraces)
37182 if_p = NULL;
37183
37184 /* Note that we saved the original contents of this flag when we entered
37185 the structured block, and so we don't need to re-save it here. */
37186 parser->in_statement = IN_OMP_FOR;
37187
37188 /* Note that the grammar doesn't call for a structured block here,
37189 though the loop as a whole is a structured block. */
37190 if (orig_declv)
37191 {
37192 body = begin_omp_structured_block ();
37193 for (i = 0; i < count; i++)
37194 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
37195 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
37196 TREE_VEC_ELT (declv, i));
37197 }
37198 else
37199 body = push_stmt_list ();
37200 cp_parser_statement (parser, NULL_TREE, false, if_p);
37201 if (orig_declv)
37202 body = finish_omp_structured_block (body);
37203 else
37204 body = pop_stmt_list (body);
37205
37206 if (declv == NULL_TREE)
37207 ret = NULL_TREE;
37208 else
37209 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
37210 incrv, body, pre_body, &orig_inits, clauses);
37211
37212 while (nbraces)
37213 {
37214 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
37215 {
37216 cp_lexer_consume_token (parser->lexer);
37217 nbraces--;
37218 }
37219 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37220 cp_lexer_consume_token (parser->lexer);
37221 else
37222 {
37223 if (!collapse_err)
37224 {
37225 error_at (cp_lexer_peek_token (parser->lexer)->location,
37226 "collapsed loops not perfectly nested");
37227 }
37228 collapse_err = true;
37229 cp_parser_statement_seq_opt (parser, NULL);
37230 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
37231 break;
37232 }
37233 }
37234
37235 while (!for_block->is_empty ())
37236 {
37237 tree t = for_block->pop ();
37238 if (TREE_CODE (t) == STATEMENT_LIST)
37239 add_stmt (pop_stmt_list (t));
37240 else
37241 add_stmt (t);
37242 }
37243 release_tree_vector (for_block);
37244
37245 return ret;
37246 }
37247
37248 /* Helper function for OpenMP parsing, split clauses and call
37249 finish_omp_clauses on each of the set of clauses afterwards. */
37250
37251 static void
37252 cp_omp_split_clauses (location_t loc, enum tree_code code,
37253 omp_clause_mask mask, tree clauses, tree *cclauses)
37254 {
37255 int i;
37256 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
37257 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
37258 if (cclauses[i])
37259 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
37260 }
37261
37262 /* OpenMP 4.0:
37263 #pragma omp simd simd-clause[optseq] new-line
37264 for-loop */
37265
37266 #define OMP_SIMD_CLAUSE_MASK \
37267 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
37268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
37277
37278 static tree
37279 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
37280 char *p_name, omp_clause_mask mask, tree *cclauses,
37281 bool *if_p)
37282 {
37283 tree clauses, sb, ret;
37284 unsigned int save;
37285 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37286
37287 strcat (p_name, " simd");
37288 mask |= OMP_SIMD_CLAUSE_MASK;
37289
37290 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37291 cclauses == NULL);
37292 if (cclauses)
37293 {
37294 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
37295 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
37296 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
37297 OMP_CLAUSE_ORDERED);
37298 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
37299 {
37300 error_at (OMP_CLAUSE_LOCATION (c),
37301 "%<ordered%> clause with parameter may not be specified "
37302 "on %qs construct", p_name);
37303 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
37304 }
37305 }
37306
37307 keep_next_level (true);
37308 sb = begin_omp_structured_block ();
37309 save = cp_parser_begin_omp_structured_block (parser);
37310
37311 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
37312
37313 cp_parser_end_omp_structured_block (parser, save);
37314 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37315
37316 return ret;
37317 }
37318
37319 /* OpenMP 2.5:
37320 #pragma omp for for-clause[optseq] new-line
37321 for-loop
37322
37323 OpenMP 4.0:
37324 #pragma omp for simd for-simd-clause[optseq] new-line
37325 for-loop */
37326
37327 #define OMP_FOR_CLAUSE_MASK \
37328 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
37334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
37335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37337
37338 static tree
37339 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
37340 char *p_name, omp_clause_mask mask, tree *cclauses,
37341 bool *if_p)
37342 {
37343 tree clauses, sb, ret;
37344 unsigned int save;
37345 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37346
37347 strcat (p_name, " for");
37348 mask |= OMP_FOR_CLAUSE_MASK;
37349 /* parallel for{, simd} disallows nowait clause, but for
37350 target {teams distribute ,}parallel for{, simd} it should be accepted. */
37351 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
37352 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37353 /* Composite distribute parallel for{, simd} disallows ordered clause. */
37354 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37355 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
37356
37357 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37358 {
37359 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37360 const char *p = IDENTIFIER_POINTER (id);
37361
37362 if (strcmp (p, "simd") == 0)
37363 {
37364 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37365 if (cclauses == NULL)
37366 cclauses = cclauses_buf;
37367
37368 cp_lexer_consume_token (parser->lexer);
37369 if (!flag_openmp) /* flag_openmp_simd */
37370 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37371 cclauses, if_p);
37372 sb = begin_omp_structured_block ();
37373 save = cp_parser_begin_omp_structured_block (parser);
37374 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37375 cclauses, if_p);
37376 cp_parser_end_omp_structured_block (parser, save);
37377 tree body = finish_omp_structured_block (sb);
37378 if (ret == NULL)
37379 return ret;
37380 ret = make_node (OMP_FOR);
37381 TREE_TYPE (ret) = void_type_node;
37382 OMP_FOR_BODY (ret) = body;
37383 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37384 SET_EXPR_LOCATION (ret, loc);
37385 add_stmt (ret);
37386 return ret;
37387 }
37388 }
37389 if (!flag_openmp) /* flag_openmp_simd */
37390 {
37391 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37392 return NULL_TREE;
37393 }
37394
37395 /* Composite distribute parallel for disallows linear clause. */
37396 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37397 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
37398
37399 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37400 cclauses == NULL);
37401 if (cclauses)
37402 {
37403 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
37404 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37405 }
37406
37407 keep_next_level (true);
37408 sb = begin_omp_structured_block ();
37409 save = cp_parser_begin_omp_structured_block (parser);
37410
37411 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
37412
37413 cp_parser_end_omp_structured_block (parser, save);
37414 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37415
37416 return ret;
37417 }
37418
37419 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
37420 omp_clause_mask, tree *, bool *);
37421
37422 /* OpenMP 2.5:
37423 # pragma omp master new-line
37424 structured-block */
37425
37426 static tree
37427 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
37428 char *p_name, omp_clause_mask mask, tree *cclauses,
37429 bool *if_p)
37430 {
37431 tree clauses, sb, ret;
37432 unsigned int save;
37433 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37434
37435 strcat (p_name, " master");
37436
37437 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37438 {
37439 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37440 const char *p = IDENTIFIER_POINTER (id);
37441
37442 if (strcmp (p, "taskloop") == 0)
37443 {
37444 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37445 if (cclauses == NULL)
37446 cclauses = cclauses_buf;
37447
37448 cp_lexer_consume_token (parser->lexer);
37449 if (!flag_openmp) /* flag_openmp_simd */
37450 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37451 cclauses, if_p);
37452 sb = begin_omp_structured_block ();
37453 save = cp_parser_begin_omp_structured_block (parser);
37454 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37455 cclauses, if_p);
37456 cp_parser_end_omp_structured_block (parser, save);
37457 tree body = finish_omp_structured_block (sb);
37458 if (ret == NULL)
37459 return ret;
37460 return c_finish_omp_master (loc, body);
37461 }
37462 }
37463 if (!flag_openmp) /* flag_openmp_simd */
37464 {
37465 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37466 return NULL_TREE;
37467 }
37468
37469 if (cclauses)
37470 {
37471 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37472 false);
37473 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
37474 }
37475 else
37476 cp_parser_require_pragma_eol (parser, pragma_tok);
37477
37478 return c_finish_omp_master (loc,
37479 cp_parser_omp_structured_block (parser, if_p));
37480 }
37481
37482 /* OpenMP 2.5:
37483 # pragma omp ordered new-line
37484 structured-block
37485
37486 OpenMP 4.5:
37487 # pragma omp ordered ordered-clauses new-line
37488 structured-block */
37489
37490 #define OMP_ORDERED_CLAUSE_MASK \
37491 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
37492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
37493
37494 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
37495 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37496
37497 static bool
37498 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
37499 enum pragma_context context, bool *if_p)
37500 {
37501 location_t loc = pragma_tok->location;
37502
37503 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37504 {
37505 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37506 const char *p = IDENTIFIER_POINTER (id);
37507
37508 if (strcmp (p, "depend") == 0)
37509 {
37510 if (!flag_openmp) /* flag_openmp_simd */
37511 {
37512 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37513 return false;
37514 }
37515 if (context == pragma_stmt)
37516 {
37517 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
37518 "%<depend%> clause may only be used in compound "
37519 "statements");
37520 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37521 return false;
37522 }
37523 tree clauses
37524 = cp_parser_omp_all_clauses (parser,
37525 OMP_ORDERED_DEPEND_CLAUSE_MASK,
37526 "#pragma omp ordered", pragma_tok);
37527 c_finish_omp_ordered (loc, clauses, NULL_TREE);
37528 return false;
37529 }
37530 }
37531
37532 tree clauses
37533 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
37534 "#pragma omp ordered", pragma_tok);
37535
37536 if (!flag_openmp /* flag_openmp_simd */
37537 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
37538 return false;
37539
37540 c_finish_omp_ordered (loc, clauses,
37541 cp_parser_omp_structured_block (parser, if_p));
37542 return true;
37543 }
37544
37545 /* OpenMP 2.5:
37546
37547 section-scope:
37548 { section-sequence }
37549
37550 section-sequence:
37551 section-directive[opt] structured-block
37552 section-sequence section-directive structured-block */
37553
37554 static tree
37555 cp_parser_omp_sections_scope (cp_parser *parser)
37556 {
37557 tree stmt, substmt;
37558 bool error_suppress = false;
37559 cp_token *tok;
37560
37561 matching_braces braces;
37562 if (!braces.require_open (parser))
37563 return NULL_TREE;
37564
37565 stmt = push_stmt_list ();
37566
37567 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
37568 != PRAGMA_OMP_SECTION)
37569 {
37570 substmt = cp_parser_omp_structured_block (parser, NULL);
37571 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37572 add_stmt (substmt);
37573 }
37574
37575 while (1)
37576 {
37577 tok = cp_lexer_peek_token (parser->lexer);
37578 if (tok->type == CPP_CLOSE_BRACE)
37579 break;
37580 if (tok->type == CPP_EOF)
37581 break;
37582
37583 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
37584 {
37585 cp_lexer_consume_token (parser->lexer);
37586 cp_parser_require_pragma_eol (parser, tok);
37587 error_suppress = false;
37588 }
37589 else if (!error_suppress)
37590 {
37591 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
37592 error_suppress = true;
37593 }
37594
37595 substmt = cp_parser_omp_structured_block (parser, NULL);
37596 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37597 add_stmt (substmt);
37598 }
37599 braces.require_close (parser);
37600
37601 substmt = pop_stmt_list (stmt);
37602
37603 stmt = make_node (OMP_SECTIONS);
37604 TREE_TYPE (stmt) = void_type_node;
37605 OMP_SECTIONS_BODY (stmt) = substmt;
37606
37607 add_stmt (stmt);
37608 return stmt;
37609 }
37610
37611 /* OpenMP 2.5:
37612 # pragma omp sections sections-clause[optseq] newline
37613 sections-scope */
37614
37615 #define OMP_SECTIONS_CLAUSE_MASK \
37616 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37621
37622 static tree
37623 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
37624 char *p_name, omp_clause_mask mask, tree *cclauses)
37625 {
37626 tree clauses, ret;
37627 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37628
37629 strcat (p_name, " sections");
37630 mask |= OMP_SECTIONS_CLAUSE_MASK;
37631 if (cclauses)
37632 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37633
37634 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37635 cclauses == NULL);
37636 if (cclauses)
37637 {
37638 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
37639 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
37640 }
37641
37642 ret = cp_parser_omp_sections_scope (parser);
37643 if (ret)
37644 OMP_SECTIONS_CLAUSES (ret) = clauses;
37645
37646 return ret;
37647 }
37648
37649 /* OpenMP 2.5:
37650 # pragma omp parallel parallel-clause[optseq] new-line
37651 structured-block
37652 # pragma omp parallel for parallel-for-clause[optseq] new-line
37653 structured-block
37654 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37655 structured-block
37656
37657 OpenMP 4.0:
37658 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37659 structured-block */
37660
37661 #define OMP_PARALLEL_CLAUSE_MASK \
37662 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37671
37672 static tree
37673 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
37674 char *p_name, omp_clause_mask mask, tree *cclauses,
37675 bool *if_p)
37676 {
37677 tree stmt, clauses, block;
37678 unsigned int save;
37679 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37680
37681 strcat (p_name, " parallel");
37682 mask |= OMP_PARALLEL_CLAUSE_MASK;
37683 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37684 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
37685 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
37686 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
37687
37688 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37689 {
37690 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37691 if (cclauses == NULL)
37692 cclauses = cclauses_buf;
37693
37694 cp_lexer_consume_token (parser->lexer);
37695 if (!flag_openmp) /* flag_openmp_simd */
37696 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37697 if_p);
37698 block = begin_omp_parallel ();
37699 save = cp_parser_begin_omp_structured_block (parser);
37700 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37701 if_p);
37702 cp_parser_end_omp_structured_block (parser, save);
37703 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37704 block);
37705 if (ret == NULL_TREE)
37706 return ret;
37707 OMP_PARALLEL_COMBINED (stmt) = 1;
37708 return stmt;
37709 }
37710 /* When combined with distribute, parallel has to be followed by for.
37711 #pragma omp target parallel is allowed though. */
37712 else if (cclauses
37713 && (mask & (OMP_CLAUSE_MASK_1
37714 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37715 {
37716 error_at (loc, "expected %<for%> after %qs", p_name);
37717 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37718 return NULL_TREE;
37719 }
37720 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37721 {
37722 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37723 const char *p = IDENTIFIER_POINTER (id);
37724 if (strcmp (p, "master") == 0)
37725 {
37726 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37727 cclauses = cclauses_buf;
37728
37729 cp_lexer_consume_token (parser->lexer);
37730 block = begin_omp_parallel ();
37731 save = cp_parser_begin_omp_structured_block (parser);
37732 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
37733 cclauses, if_p);
37734 cp_parser_end_omp_structured_block (parser, save);
37735 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37736 block);
37737 OMP_PARALLEL_COMBINED (stmt) = 1;
37738 if (ret == NULL_TREE)
37739 return ret;
37740 return stmt;
37741 }
37742 else if (!flag_openmp) /* flag_openmp_simd */
37743 {
37744 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37745 return NULL_TREE;
37746 }
37747 else if (strcmp (p, "sections") == 0)
37748 {
37749 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37750 cclauses = cclauses_buf;
37751
37752 cp_lexer_consume_token (parser->lexer);
37753 block = begin_omp_parallel ();
37754 save = cp_parser_begin_omp_structured_block (parser);
37755 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
37756 cp_parser_end_omp_structured_block (parser, save);
37757 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37758 block);
37759 OMP_PARALLEL_COMBINED (stmt) = 1;
37760 return stmt;
37761 }
37762 }
37763 else if (!flag_openmp) /* flag_openmp_simd */
37764 {
37765 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37766 return NULL_TREE;
37767 }
37768
37769 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37770 cclauses == NULL);
37771 if (cclauses)
37772 {
37773 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
37774 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
37775 }
37776
37777 block = begin_omp_parallel ();
37778 save = cp_parser_begin_omp_structured_block (parser);
37779 cp_parser_statement (parser, NULL_TREE, false, if_p);
37780 cp_parser_end_omp_structured_block (parser, save);
37781 stmt = finish_omp_parallel (clauses, block);
37782 return stmt;
37783 }
37784
37785 /* OpenMP 2.5:
37786 # pragma omp single single-clause[optseq] new-line
37787 structured-block */
37788
37789 #define OMP_SINGLE_CLAUSE_MASK \
37790 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37794
37795 static tree
37796 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37797 {
37798 tree stmt = make_node (OMP_SINGLE);
37799 TREE_TYPE (stmt) = void_type_node;
37800 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37801
37802 OMP_SINGLE_CLAUSES (stmt)
37803 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
37804 "#pragma omp single", pragma_tok);
37805 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37806
37807 return add_stmt (stmt);
37808 }
37809
37810 /* OpenMP 3.0:
37811 # pragma omp task task-clause[optseq] new-line
37812 structured-block */
37813
37814 #define OMP_TASK_CLAUSE_MASK \
37815 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37826
37827 static tree
37828 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37829 {
37830 tree clauses, block;
37831 unsigned int save;
37832
37833 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
37834 "#pragma omp task", pragma_tok);
37835 block = begin_omp_task ();
37836 save = cp_parser_begin_omp_structured_block (parser);
37837 cp_parser_statement (parser, NULL_TREE, false, if_p);
37838 cp_parser_end_omp_structured_block (parser, save);
37839 return finish_omp_task (clauses, block);
37840 }
37841
37842 /* OpenMP 3.0:
37843 # pragma omp taskwait new-line
37844
37845 OpenMP 5.0:
37846 # pragma omp taskwait taskwait-clause[opt] new-line */
37847
37848 #define OMP_TASKWAIT_CLAUSE_MASK \
37849 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37850
37851 static void
37852 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
37853 {
37854 tree clauses
37855 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
37856 "#pragma omp taskwait", pragma_tok);
37857
37858 if (clauses)
37859 {
37860 tree stmt = make_node (OMP_TASK);
37861 TREE_TYPE (stmt) = void_node;
37862 OMP_TASK_CLAUSES (stmt) = clauses;
37863 OMP_TASK_BODY (stmt) = NULL_TREE;
37864 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37865 add_stmt (stmt);
37866 }
37867 else
37868 finish_omp_taskwait ();
37869 }
37870
37871 /* OpenMP 3.1:
37872 # pragma omp taskyield new-line */
37873
37874 static void
37875 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
37876 {
37877 cp_parser_require_pragma_eol (parser, pragma_tok);
37878 finish_omp_taskyield ();
37879 }
37880
37881 /* OpenMP 4.0:
37882 # pragma omp taskgroup new-line
37883 structured-block
37884
37885 OpenMP 5.0:
37886 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37887
37888 #define OMP_TASKGROUP_CLAUSE_MASK \
37889 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37890
37891 static tree
37892 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37893 {
37894 tree clauses
37895 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
37896 "#pragma omp taskgroup", pragma_tok);
37897 return c_finish_omp_taskgroup (input_location,
37898 cp_parser_omp_structured_block (parser,
37899 if_p),
37900 clauses);
37901 }
37902
37903
37904 /* OpenMP 2.5:
37905 # pragma omp threadprivate (variable-list) */
37906
37907 static void
37908 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
37909 {
37910 tree vars;
37911
37912 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37913 cp_parser_require_pragma_eol (parser, pragma_tok);
37914
37915 finish_omp_threadprivate (vars);
37916 }
37917
37918 /* OpenMP 4.0:
37919 # pragma omp cancel cancel-clause[optseq] new-line */
37920
37921 #define OMP_CANCEL_CLAUSE_MASK \
37922 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37927
37928 static void
37929 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
37930 {
37931 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
37932 "#pragma omp cancel", pragma_tok);
37933 finish_omp_cancel (clauses);
37934 }
37935
37936 /* OpenMP 4.0:
37937 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37938
37939 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37940 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37944
37945 static void
37946 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
37947 enum pragma_context context)
37948 {
37949 tree clauses;
37950 bool point_seen = false;
37951
37952 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37953 {
37954 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37955 const char *p = IDENTIFIER_POINTER (id);
37956
37957 if (strcmp (p, "point") == 0)
37958 {
37959 cp_lexer_consume_token (parser->lexer);
37960 point_seen = true;
37961 }
37962 }
37963 if (!point_seen)
37964 {
37965 cp_parser_error (parser, "expected %<point%>");
37966 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37967 return;
37968 }
37969
37970 if (context != pragma_compound)
37971 {
37972 if (context == pragma_stmt)
37973 error_at (pragma_tok->location,
37974 "%<#pragma %s%> may only be used in compound statements",
37975 "omp cancellation point");
37976 else
37977 cp_parser_error (parser, "expected declaration specifiers");
37978 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37979 return;
37980 }
37981
37982 clauses = cp_parser_omp_all_clauses (parser,
37983 OMP_CANCELLATION_POINT_CLAUSE_MASK,
37984 "#pragma omp cancellation point",
37985 pragma_tok);
37986 finish_omp_cancellation_point (clauses);
37987 }
37988
37989 /* OpenMP 4.0:
37990 #pragma omp distribute distribute-clause[optseq] new-line
37991 for-loop */
37992
37993 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37994 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37999
38000 static tree
38001 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
38002 char *p_name, omp_clause_mask mask, tree *cclauses,
38003 bool *if_p)
38004 {
38005 tree clauses, sb, ret;
38006 unsigned int save;
38007 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38008
38009 strcat (p_name, " distribute");
38010 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
38011
38012 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38013 {
38014 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38015 const char *p = IDENTIFIER_POINTER (id);
38016 bool simd = false;
38017 bool parallel = false;
38018
38019 if (strcmp (p, "simd") == 0)
38020 simd = true;
38021 else
38022 parallel = strcmp (p, "parallel") == 0;
38023 if (parallel || simd)
38024 {
38025 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38026 if (cclauses == NULL)
38027 cclauses = cclauses_buf;
38028 cp_lexer_consume_token (parser->lexer);
38029 if (!flag_openmp) /* flag_openmp_simd */
38030 {
38031 if (simd)
38032 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38033 cclauses, if_p);
38034 else
38035 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
38036 cclauses, if_p);
38037 }
38038 sb = begin_omp_structured_block ();
38039 save = cp_parser_begin_omp_structured_block (parser);
38040 if (simd)
38041 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38042 cclauses, if_p);
38043 else
38044 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
38045 cclauses, if_p);
38046 cp_parser_end_omp_structured_block (parser, save);
38047 tree body = finish_omp_structured_block (sb);
38048 if (ret == NULL)
38049 return ret;
38050 ret = make_node (OMP_DISTRIBUTE);
38051 TREE_TYPE (ret) = void_type_node;
38052 OMP_FOR_BODY (ret) = body;
38053 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
38054 SET_EXPR_LOCATION (ret, loc);
38055 add_stmt (ret);
38056 return ret;
38057 }
38058 }
38059 if (!flag_openmp) /* flag_openmp_simd */
38060 {
38061 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38062 return NULL_TREE;
38063 }
38064
38065 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38066 cclauses == NULL);
38067 if (cclauses)
38068 {
38069 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
38070 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
38071 }
38072
38073 keep_next_level (true);
38074 sb = begin_omp_structured_block ();
38075 save = cp_parser_begin_omp_structured_block (parser);
38076
38077 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
38078
38079 cp_parser_end_omp_structured_block (parser, save);
38080 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
38081
38082 return ret;
38083 }
38084
38085 /* OpenMP 4.0:
38086 # pragma omp teams teams-clause[optseq] new-line
38087 structured-block */
38088
38089 #define OMP_TEAMS_CLAUSE_MASK \
38090 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
38093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
38095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
38096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
38097
38098 static tree
38099 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
38100 char *p_name, omp_clause_mask mask, tree *cclauses,
38101 bool *if_p)
38102 {
38103 tree clauses, sb, ret;
38104 unsigned int save;
38105 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38106
38107 strcat (p_name, " teams");
38108 mask |= OMP_TEAMS_CLAUSE_MASK;
38109
38110 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38111 {
38112 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38113 const char *p = IDENTIFIER_POINTER (id);
38114 if (strcmp (p, "distribute") == 0)
38115 {
38116 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38117 if (cclauses == NULL)
38118 cclauses = cclauses_buf;
38119
38120 cp_lexer_consume_token (parser->lexer);
38121 if (!flag_openmp) /* flag_openmp_simd */
38122 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
38123 cclauses, if_p);
38124 keep_next_level (true);
38125 sb = begin_omp_structured_block ();
38126 save = cp_parser_begin_omp_structured_block (parser);
38127 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
38128 cclauses, if_p);
38129 cp_parser_end_omp_structured_block (parser, save);
38130 tree body = finish_omp_structured_block (sb);
38131 if (ret == NULL)
38132 return ret;
38133 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38134 ret = make_node (OMP_TEAMS);
38135 TREE_TYPE (ret) = void_type_node;
38136 OMP_TEAMS_CLAUSES (ret) = clauses;
38137 OMP_TEAMS_BODY (ret) = body;
38138 OMP_TEAMS_COMBINED (ret) = 1;
38139 SET_EXPR_LOCATION (ret, loc);
38140 return add_stmt (ret);
38141 }
38142 }
38143 if (!flag_openmp) /* flag_openmp_simd */
38144 {
38145 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38146 return NULL_TREE;
38147 }
38148
38149 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38150 cclauses == NULL);
38151 if (cclauses)
38152 {
38153 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
38154 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38155 }
38156
38157 tree stmt = make_node (OMP_TEAMS);
38158 TREE_TYPE (stmt) = void_type_node;
38159 OMP_TEAMS_CLAUSES (stmt) = clauses;
38160 keep_next_level (true);
38161 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38162 SET_EXPR_LOCATION (stmt, loc);
38163
38164 return add_stmt (stmt);
38165 }
38166
38167 /* OpenMP 4.0:
38168 # pragma omp target data target-data-clause[optseq] new-line
38169 structured-block */
38170
38171 #define OMP_TARGET_DATA_CLAUSE_MASK \
38172 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
38176
38177 static tree
38178 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38179 {
38180 tree clauses
38181 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
38182 "#pragma omp target data", pragma_tok);
38183 int map_seen = 0;
38184 for (tree *pc = &clauses; *pc;)
38185 {
38186 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38187 switch (OMP_CLAUSE_MAP_KIND (*pc))
38188 {
38189 case GOMP_MAP_TO:
38190 case GOMP_MAP_ALWAYS_TO:
38191 case GOMP_MAP_FROM:
38192 case GOMP_MAP_ALWAYS_FROM:
38193 case GOMP_MAP_TOFROM:
38194 case GOMP_MAP_ALWAYS_TOFROM:
38195 case GOMP_MAP_ALLOC:
38196 map_seen = 3;
38197 break;
38198 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38199 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38200 case GOMP_MAP_ALWAYS_POINTER:
38201 break;
38202 default:
38203 map_seen |= 1;
38204 error_at (OMP_CLAUSE_LOCATION (*pc),
38205 "%<#pragma omp target data%> with map-type other "
38206 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38207 "on %<map%> clause");
38208 *pc = OMP_CLAUSE_CHAIN (*pc);
38209 continue;
38210 }
38211 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
38212 map_seen = 3;
38213 pc = &OMP_CLAUSE_CHAIN (*pc);
38214 }
38215
38216 if (map_seen != 3)
38217 {
38218 if (map_seen == 0)
38219 error_at (pragma_tok->location,
38220 "%<#pragma omp target data%> must contain at least "
38221 "one %<map%> or %<use_device_ptr%> clause");
38222 return NULL_TREE;
38223 }
38224
38225 tree stmt = make_node (OMP_TARGET_DATA);
38226 TREE_TYPE (stmt) = void_type_node;
38227 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
38228
38229 keep_next_level (true);
38230 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38231
38232 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38233 return add_stmt (stmt);
38234 }
38235
38236 /* OpenMP 4.5:
38237 # pragma omp target enter data target-enter-data-clause[optseq] new-line
38238 structured-block */
38239
38240 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
38241 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38246
38247 static tree
38248 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
38249 enum pragma_context context)
38250 {
38251 bool data_seen = false;
38252 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38253 {
38254 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38255 const char *p = IDENTIFIER_POINTER (id);
38256
38257 if (strcmp (p, "data") == 0)
38258 {
38259 cp_lexer_consume_token (parser->lexer);
38260 data_seen = true;
38261 }
38262 }
38263 if (!data_seen)
38264 {
38265 cp_parser_error (parser, "expected %<data%>");
38266 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38267 return NULL_TREE;
38268 }
38269
38270 if (context == pragma_stmt)
38271 {
38272 error_at (pragma_tok->location,
38273 "%<#pragma %s%> may only be used in compound statements",
38274 "omp target enter data");
38275 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38276 return NULL_TREE;
38277 }
38278
38279 tree clauses
38280 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
38281 "#pragma omp target enter data", pragma_tok);
38282 int map_seen = 0;
38283 for (tree *pc = &clauses; *pc;)
38284 {
38285 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38286 switch (OMP_CLAUSE_MAP_KIND (*pc))
38287 {
38288 case GOMP_MAP_TO:
38289 case GOMP_MAP_ALWAYS_TO:
38290 case GOMP_MAP_ALLOC:
38291 map_seen = 3;
38292 break;
38293 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38294 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38295 case GOMP_MAP_ALWAYS_POINTER:
38296 break;
38297 default:
38298 map_seen |= 1;
38299 error_at (OMP_CLAUSE_LOCATION (*pc),
38300 "%<#pragma omp target enter data%> with map-type other "
38301 "than %<to%> or %<alloc%> on %<map%> clause");
38302 *pc = OMP_CLAUSE_CHAIN (*pc);
38303 continue;
38304 }
38305 pc = &OMP_CLAUSE_CHAIN (*pc);
38306 }
38307
38308 if (map_seen != 3)
38309 {
38310 if (map_seen == 0)
38311 error_at (pragma_tok->location,
38312 "%<#pragma omp target enter data%> must contain at least "
38313 "one %<map%> clause");
38314 return NULL_TREE;
38315 }
38316
38317 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
38318 TREE_TYPE (stmt) = void_type_node;
38319 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
38320 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38321 return add_stmt (stmt);
38322 }
38323
38324 /* OpenMP 4.5:
38325 # pragma omp target exit data target-enter-data-clause[optseq] new-line
38326 structured-block */
38327
38328 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
38329 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38334
38335 static tree
38336 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
38337 enum pragma_context context)
38338 {
38339 bool data_seen = false;
38340 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38341 {
38342 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38343 const char *p = IDENTIFIER_POINTER (id);
38344
38345 if (strcmp (p, "data") == 0)
38346 {
38347 cp_lexer_consume_token (parser->lexer);
38348 data_seen = true;
38349 }
38350 }
38351 if (!data_seen)
38352 {
38353 cp_parser_error (parser, "expected %<data%>");
38354 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38355 return NULL_TREE;
38356 }
38357
38358 if (context == pragma_stmt)
38359 {
38360 error_at (pragma_tok->location,
38361 "%<#pragma %s%> may only be used in compound statements",
38362 "omp target exit data");
38363 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38364 return NULL_TREE;
38365 }
38366
38367 tree clauses
38368 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
38369 "#pragma omp target exit data", pragma_tok);
38370 int map_seen = 0;
38371 for (tree *pc = &clauses; *pc;)
38372 {
38373 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38374 switch (OMP_CLAUSE_MAP_KIND (*pc))
38375 {
38376 case GOMP_MAP_FROM:
38377 case GOMP_MAP_ALWAYS_FROM:
38378 case GOMP_MAP_RELEASE:
38379 case GOMP_MAP_DELETE:
38380 map_seen = 3;
38381 break;
38382 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38383 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38384 case GOMP_MAP_ALWAYS_POINTER:
38385 break;
38386 default:
38387 map_seen |= 1;
38388 error_at (OMP_CLAUSE_LOCATION (*pc),
38389 "%<#pragma omp target exit data%> with map-type other "
38390 "than %<from%>, %<release%> or %<delete%> on %<map%>"
38391 " clause");
38392 *pc = OMP_CLAUSE_CHAIN (*pc);
38393 continue;
38394 }
38395 pc = &OMP_CLAUSE_CHAIN (*pc);
38396 }
38397
38398 if (map_seen != 3)
38399 {
38400 if (map_seen == 0)
38401 error_at (pragma_tok->location,
38402 "%<#pragma omp target exit data%> must contain at least "
38403 "one %<map%> clause");
38404 return NULL_TREE;
38405 }
38406
38407 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
38408 TREE_TYPE (stmt) = void_type_node;
38409 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
38410 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38411 return add_stmt (stmt);
38412 }
38413
38414 /* OpenMP 4.0:
38415 # pragma omp target update target-update-clause[optseq] new-line */
38416
38417 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
38418 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
38419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38424
38425 static bool
38426 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
38427 enum pragma_context context)
38428 {
38429 if (context == pragma_stmt)
38430 {
38431 error_at (pragma_tok->location,
38432 "%<#pragma %s%> may only be used in compound statements",
38433 "omp target update");
38434 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38435 return false;
38436 }
38437
38438 tree clauses
38439 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
38440 "#pragma omp target update", pragma_tok);
38441 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
38442 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
38443 {
38444 error_at (pragma_tok->location,
38445 "%<#pragma omp target update%> must contain at least one "
38446 "%<from%> or %<to%> clauses");
38447 return false;
38448 }
38449
38450 tree stmt = make_node (OMP_TARGET_UPDATE);
38451 TREE_TYPE (stmt) = void_type_node;
38452 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
38453 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38454 add_stmt (stmt);
38455 return false;
38456 }
38457
38458 /* OpenMP 4.0:
38459 # pragma omp target target-clause[optseq] new-line
38460 structured-block */
38461
38462 #define OMP_TARGET_CLAUSE_MASK \
38463 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
38468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
38471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
38472
38473 static bool
38474 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
38475 enum pragma_context context, bool *if_p)
38476 {
38477 tree *pc = NULL, stmt;
38478
38479 if (flag_openmp)
38480 omp_requires_mask
38481 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
38482
38483 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38484 {
38485 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38486 const char *p = IDENTIFIER_POINTER (id);
38487 enum tree_code ccode = ERROR_MARK;
38488
38489 if (strcmp (p, "teams") == 0)
38490 ccode = OMP_TEAMS;
38491 else if (strcmp (p, "parallel") == 0)
38492 ccode = OMP_PARALLEL;
38493 else if (strcmp (p, "simd") == 0)
38494 ccode = OMP_SIMD;
38495 if (ccode != ERROR_MARK)
38496 {
38497 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
38498 char p_name[sizeof ("#pragma omp target teams distribute "
38499 "parallel for simd")];
38500
38501 cp_lexer_consume_token (parser->lexer);
38502 strcpy (p_name, "#pragma omp target");
38503 if (!flag_openmp) /* flag_openmp_simd */
38504 {
38505 tree stmt;
38506 switch (ccode)
38507 {
38508 case OMP_TEAMS:
38509 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
38510 OMP_TARGET_CLAUSE_MASK,
38511 cclauses, if_p);
38512 break;
38513 case OMP_PARALLEL:
38514 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38515 OMP_TARGET_CLAUSE_MASK,
38516 cclauses, if_p);
38517 break;
38518 case OMP_SIMD:
38519 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
38520 OMP_TARGET_CLAUSE_MASK,
38521 cclauses, if_p);
38522 break;
38523 default:
38524 gcc_unreachable ();
38525 }
38526 return stmt != NULL_TREE;
38527 }
38528 keep_next_level (true);
38529 tree sb = begin_omp_structured_block (), ret;
38530 unsigned save = cp_parser_begin_omp_structured_block (parser);
38531 switch (ccode)
38532 {
38533 case OMP_TEAMS:
38534 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
38535 OMP_TARGET_CLAUSE_MASK, cclauses,
38536 if_p);
38537 break;
38538 case OMP_PARALLEL:
38539 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38540 OMP_TARGET_CLAUSE_MASK, cclauses,
38541 if_p);
38542 break;
38543 case OMP_SIMD:
38544 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
38545 OMP_TARGET_CLAUSE_MASK, cclauses,
38546 if_p);
38547 break;
38548 default:
38549 gcc_unreachable ();
38550 }
38551 cp_parser_end_omp_structured_block (parser, save);
38552 tree body = finish_omp_structured_block (sb);
38553 if (ret == NULL_TREE)
38554 return false;
38555 if (ccode == OMP_TEAMS && !processing_template_decl)
38556 {
38557 /* For combined target teams, ensure the num_teams and
38558 thread_limit clause expressions are evaluated on the host,
38559 before entering the target construct. */
38560 tree c;
38561 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38562 c; c = OMP_CLAUSE_CHAIN (c))
38563 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
38564 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
38565 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
38566 {
38567 tree expr = OMP_CLAUSE_OPERAND (c, 0);
38568 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
38569 if (expr == error_mark_node)
38570 continue;
38571 tree tmp = TARGET_EXPR_SLOT (expr);
38572 add_stmt (expr);
38573 OMP_CLAUSE_OPERAND (c, 0) = expr;
38574 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
38575 OMP_CLAUSE_FIRSTPRIVATE);
38576 OMP_CLAUSE_DECL (tc) = tmp;
38577 OMP_CLAUSE_CHAIN (tc)
38578 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38579 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
38580 }
38581 }
38582 tree stmt = make_node (OMP_TARGET);
38583 TREE_TYPE (stmt) = void_type_node;
38584 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38585 OMP_TARGET_BODY (stmt) = body;
38586 OMP_TARGET_COMBINED (stmt) = 1;
38587 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38588 add_stmt (stmt);
38589 pc = &OMP_TARGET_CLAUSES (stmt);
38590 goto check_clauses;
38591 }
38592 else if (!flag_openmp) /* flag_openmp_simd */
38593 {
38594 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38595 return false;
38596 }
38597 else if (strcmp (p, "data") == 0)
38598 {
38599 cp_lexer_consume_token (parser->lexer);
38600 cp_parser_omp_target_data (parser, pragma_tok, if_p);
38601 return true;
38602 }
38603 else if (strcmp (p, "enter") == 0)
38604 {
38605 cp_lexer_consume_token (parser->lexer);
38606 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
38607 return false;
38608 }
38609 else if (strcmp (p, "exit") == 0)
38610 {
38611 cp_lexer_consume_token (parser->lexer);
38612 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
38613 return false;
38614 }
38615 else if (strcmp (p, "update") == 0)
38616 {
38617 cp_lexer_consume_token (parser->lexer);
38618 return cp_parser_omp_target_update (parser, pragma_tok, context);
38619 }
38620 }
38621 if (!flag_openmp) /* flag_openmp_simd */
38622 {
38623 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38624 return false;
38625 }
38626
38627 stmt = make_node (OMP_TARGET);
38628 TREE_TYPE (stmt) = void_type_node;
38629
38630 OMP_TARGET_CLAUSES (stmt)
38631 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
38632 "#pragma omp target", pragma_tok);
38633 pc = &OMP_TARGET_CLAUSES (stmt);
38634 keep_next_level (true);
38635 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38636
38637 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38638 add_stmt (stmt);
38639
38640 check_clauses:
38641 while (*pc)
38642 {
38643 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38644 switch (OMP_CLAUSE_MAP_KIND (*pc))
38645 {
38646 case GOMP_MAP_TO:
38647 case GOMP_MAP_ALWAYS_TO:
38648 case GOMP_MAP_FROM:
38649 case GOMP_MAP_ALWAYS_FROM:
38650 case GOMP_MAP_TOFROM:
38651 case GOMP_MAP_ALWAYS_TOFROM:
38652 case GOMP_MAP_ALLOC:
38653 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38654 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38655 case GOMP_MAP_ALWAYS_POINTER:
38656 break;
38657 default:
38658 error_at (OMP_CLAUSE_LOCATION (*pc),
38659 "%<#pragma omp target%> with map-type other "
38660 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38661 "on %<map%> clause");
38662 *pc = OMP_CLAUSE_CHAIN (*pc);
38663 continue;
38664 }
38665 pc = &OMP_CLAUSE_CHAIN (*pc);
38666 }
38667 return true;
38668 }
38669
38670 /* OpenACC 2.0:
38671 # pragma acc cache (variable-list) new-line
38672 */
38673
38674 static tree
38675 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
38676 {
38677 tree stmt, clauses;
38678
38679 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
38680 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38681
38682 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
38683
38684 stmt = make_node (OACC_CACHE);
38685 TREE_TYPE (stmt) = void_type_node;
38686 OACC_CACHE_CLAUSES (stmt) = clauses;
38687 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38688 add_stmt (stmt);
38689
38690 return stmt;
38691 }
38692
38693 /* OpenACC 2.0:
38694 # pragma acc data oacc-data-clause[optseq] new-line
38695 structured-block */
38696
38697 #define OACC_DATA_CLAUSE_MASK \
38698 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38705
38706 static tree
38707 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38708 {
38709 tree stmt, clauses, block;
38710 unsigned int save;
38711
38712 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
38713 "#pragma acc data", pragma_tok);
38714
38715 block = begin_omp_parallel ();
38716 save = cp_parser_begin_omp_structured_block (parser);
38717 cp_parser_statement (parser, NULL_TREE, false, if_p);
38718 cp_parser_end_omp_structured_block (parser, save);
38719 stmt = finish_oacc_data (clauses, block);
38720 return stmt;
38721 }
38722
38723 /* OpenACC 2.0:
38724 # pragma acc host_data <clauses> new-line
38725 structured-block */
38726
38727 #define OACC_HOST_DATA_CLAUSE_MASK \
38728 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38729
38730 static tree
38731 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38732 {
38733 tree stmt, clauses, block;
38734 unsigned int save;
38735
38736 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
38737 "#pragma acc host_data", pragma_tok);
38738
38739 block = begin_omp_parallel ();
38740 save = cp_parser_begin_omp_structured_block (parser);
38741 cp_parser_statement (parser, NULL_TREE, false, if_p);
38742 cp_parser_end_omp_structured_block (parser, save);
38743 stmt = finish_oacc_host_data (clauses, block);
38744 return stmt;
38745 }
38746
38747 /* OpenACC 2.0:
38748 # pragma acc declare oacc-data-clause[optseq] new-line
38749 */
38750
38751 #define OACC_DECLARE_CLAUSE_MASK \
38752 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38760
38761 static tree
38762 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
38763 {
38764 tree clauses, stmt;
38765 bool error = false;
38766
38767 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
38768 "#pragma acc declare", pragma_tok, true);
38769
38770
38771 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38772 {
38773 error_at (pragma_tok->location,
38774 "no valid clauses specified in %<#pragma acc declare%>");
38775 return NULL_TREE;
38776 }
38777
38778 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
38779 {
38780 location_t loc = OMP_CLAUSE_LOCATION (t);
38781 tree decl = OMP_CLAUSE_DECL (t);
38782 if (!DECL_P (decl))
38783 {
38784 error_at (loc, "array section in %<#pragma acc declare%>");
38785 error = true;
38786 continue;
38787 }
38788 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
38789 switch (OMP_CLAUSE_MAP_KIND (t))
38790 {
38791 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38792 case GOMP_MAP_ALLOC:
38793 case GOMP_MAP_TO:
38794 case GOMP_MAP_FORCE_DEVICEPTR:
38795 case GOMP_MAP_DEVICE_RESIDENT:
38796 break;
38797
38798 case GOMP_MAP_LINK:
38799 if (!global_bindings_p ()
38800 && (TREE_STATIC (decl)
38801 || !DECL_EXTERNAL (decl)))
38802 {
38803 error_at (loc,
38804 "%qD must be a global variable in "
38805 "%<#pragma acc declare link%>",
38806 decl);
38807 error = true;
38808 continue;
38809 }
38810 break;
38811
38812 default:
38813 if (global_bindings_p ())
38814 {
38815 error_at (loc, "invalid OpenACC clause at file scope");
38816 error = true;
38817 continue;
38818 }
38819 if (DECL_EXTERNAL (decl))
38820 {
38821 error_at (loc,
38822 "invalid use of %<extern%> variable %qD "
38823 "in %<#pragma acc declare%>", decl);
38824 error = true;
38825 continue;
38826 }
38827 else if (TREE_PUBLIC (decl))
38828 {
38829 error_at (loc,
38830 "invalid use of %<global%> variable %qD "
38831 "in %<#pragma acc declare%>", decl);
38832 error = true;
38833 continue;
38834 }
38835 break;
38836 }
38837
38838 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
38839 || lookup_attribute ("omp declare target link",
38840 DECL_ATTRIBUTES (decl)))
38841 {
38842 error_at (loc, "variable %qD used more than once with "
38843 "%<#pragma acc declare%>", decl);
38844 error = true;
38845 continue;
38846 }
38847
38848 if (!error)
38849 {
38850 tree id;
38851
38852 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
38853 id = get_identifier ("omp declare target link");
38854 else
38855 id = get_identifier ("omp declare target");
38856
38857 DECL_ATTRIBUTES (decl)
38858 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
38859 if (global_bindings_p ())
38860 {
38861 symtab_node *node = symtab_node::get (decl);
38862 if (node != NULL)
38863 {
38864 node->offloadable = 1;
38865 if (ENABLE_OFFLOADING)
38866 {
38867 g->have_offload = true;
38868 if (is_a <varpool_node *> (node))
38869 vec_safe_push (offload_vars, decl);
38870 }
38871 }
38872 }
38873 }
38874 }
38875
38876 if (error || global_bindings_p ())
38877 return NULL_TREE;
38878
38879 stmt = make_node (OACC_DECLARE);
38880 TREE_TYPE (stmt) = void_type_node;
38881 OACC_DECLARE_CLAUSES (stmt) = clauses;
38882 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38883
38884 add_stmt (stmt);
38885
38886 return NULL_TREE;
38887 }
38888
38889 /* OpenACC 2.0:
38890 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38891
38892 or
38893
38894 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38895
38896 LOC is the location of the #pragma token.
38897 */
38898
38899 #define OACC_ENTER_DATA_CLAUSE_MASK \
38900 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38905
38906 #define OACC_EXIT_DATA_CLAUSE_MASK \
38907 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38913
38914 static tree
38915 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
38916 bool enter)
38917 {
38918 location_t loc = pragma_tok->location;
38919 tree stmt, clauses;
38920 const char *p = "";
38921
38922 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38923 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38924
38925 if (strcmp (p, "data") != 0)
38926 {
38927 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
38928 enter ? "enter" : "exit");
38929 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38930 return NULL_TREE;
38931 }
38932
38933 cp_lexer_consume_token (parser->lexer);
38934
38935 if (enter)
38936 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
38937 "#pragma acc enter data", pragma_tok);
38938 else
38939 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
38940 "#pragma acc exit data", pragma_tok);
38941
38942 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38943 {
38944 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
38945 enter ? "enter" : "exit");
38946 return NULL_TREE;
38947 }
38948
38949 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
38950 TREE_TYPE (stmt) = void_type_node;
38951 OMP_STANDALONE_CLAUSES (stmt) = clauses;
38952 SET_EXPR_LOCATION (stmt, loc);
38953 add_stmt (stmt);
38954 return stmt;
38955 }
38956
38957 /* OpenACC 2.0:
38958 # pragma acc loop oacc-loop-clause[optseq] new-line
38959 structured-block */
38960
38961 #define OACC_LOOP_CLAUSE_MASK \
38962 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38972
38973 static tree
38974 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
38975 omp_clause_mask mask, tree *cclauses, bool *if_p)
38976 {
38977 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
38978
38979 strcat (p_name, " loop");
38980 mask |= OACC_LOOP_CLAUSE_MASK;
38981
38982 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
38983 cclauses == NULL);
38984 if (cclauses)
38985 {
38986 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
38987 if (*cclauses)
38988 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
38989 if (clauses)
38990 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38991 }
38992
38993 tree block = begin_omp_structured_block ();
38994 int save = cp_parser_begin_omp_structured_block (parser);
38995 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
38996 cp_parser_end_omp_structured_block (parser, save);
38997 add_stmt (finish_omp_structured_block (block));
38998
38999 return stmt;
39000 }
39001
39002 /* OpenACC 2.0:
39003 # pragma acc kernels oacc-kernels-clause[optseq] new-line
39004 structured-block
39005
39006 or
39007
39008 # pragma acc parallel oacc-parallel-clause[optseq] new-line
39009 structured-block
39010 */
39011
39012 #define OACC_KERNELS_CLAUSE_MASK \
39013 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
39019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
39022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
39023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
39024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
39025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39026
39027 #define OACC_PARALLEL_CLAUSE_MASK \
39028 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
39034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
39036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
39038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
39039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
39040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
39041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
39042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
39043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39044
39045 static tree
39046 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
39047 char *p_name, bool *if_p)
39048 {
39049 omp_clause_mask mask;
39050 enum tree_code code;
39051 switch (cp_parser_pragma_kind (pragma_tok))
39052 {
39053 case PRAGMA_OACC_KERNELS:
39054 strcat (p_name, " kernels");
39055 mask = OACC_KERNELS_CLAUSE_MASK;
39056 code = OACC_KERNELS;
39057 break;
39058 case PRAGMA_OACC_PARALLEL:
39059 strcat (p_name, " parallel");
39060 mask = OACC_PARALLEL_CLAUSE_MASK;
39061 code = OACC_PARALLEL;
39062 break;
39063 default:
39064 gcc_unreachable ();
39065 }
39066
39067 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39068 {
39069 const char *p
39070 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
39071 if (strcmp (p, "loop") == 0)
39072 {
39073 cp_lexer_consume_token (parser->lexer);
39074 tree block = begin_omp_parallel ();
39075 tree clauses;
39076 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
39077 &clauses, if_p);
39078 protected_set_expr_location (stmt, pragma_tok->location);
39079 return finish_omp_construct (code, block, clauses);
39080 }
39081 }
39082
39083 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
39084
39085 tree block = begin_omp_parallel ();
39086 unsigned int save = cp_parser_begin_omp_structured_block (parser);
39087 cp_parser_statement (parser, NULL_TREE, false, if_p);
39088 cp_parser_end_omp_structured_block (parser, save);
39089 return finish_omp_construct (code, block, clauses);
39090 }
39091
39092 /* OpenACC 2.0:
39093 # pragma acc update oacc-update-clause[optseq] new-line
39094 */
39095
39096 #define OACC_UPDATE_CLAUSE_MASK \
39097 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
39098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
39099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
39100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
39102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
39103
39104 static tree
39105 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
39106 {
39107 tree stmt, clauses;
39108
39109 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
39110 "#pragma acc update", pragma_tok);
39111
39112 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
39113 {
39114 error_at (pragma_tok->location,
39115 "%<#pragma acc update%> must contain at least one "
39116 "%<device%> or %<host%> or %<self%> clause");
39117 return NULL_TREE;
39118 }
39119
39120 stmt = make_node (OACC_UPDATE);
39121 TREE_TYPE (stmt) = void_type_node;
39122 OACC_UPDATE_CLAUSES (stmt) = clauses;
39123 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39124 add_stmt (stmt);
39125 return stmt;
39126 }
39127
39128 /* OpenACC 2.0:
39129 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
39130
39131 LOC is the location of the #pragma token.
39132 */
39133
39134 #define OACC_WAIT_CLAUSE_MASK \
39135 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
39136
39137 static tree
39138 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
39139 {
39140 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
39141 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39142
39143 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39144 list = cp_parser_oacc_wait_list (parser, loc, list);
39145
39146 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
39147 "#pragma acc wait", pragma_tok);
39148
39149 stmt = c_finish_oacc_wait (loc, list, clauses);
39150 stmt = finish_expr_stmt (stmt);
39151
39152 return stmt;
39153 }
39154
39155 /* OpenMP 4.0:
39156 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
39157
39158 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
39159 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
39160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
39162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
39163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
39164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
39165
39166 static void
39167 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
39168 enum pragma_context context)
39169 {
39170 bool first_p = parser->omp_declare_simd == NULL;
39171 cp_omp_declare_simd_data data;
39172 if (first_p)
39173 {
39174 data.error_seen = false;
39175 data.fndecl_seen = false;
39176 data.tokens = vNULL;
39177 data.clauses = NULL_TREE;
39178 /* It is safe to take the address of a local variable; it will only be
39179 used while this scope is live. */
39180 parser->omp_declare_simd = &data;
39181 }
39182
39183 /* Store away all pragma tokens. */
39184 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39185 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39186 cp_lexer_consume_token (parser->lexer);
39187 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39188 parser->omp_declare_simd->error_seen = true;
39189 cp_parser_require_pragma_eol (parser, pragma_tok);
39190 struct cp_token_cache *cp
39191 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
39192 parser->omp_declare_simd->tokens.safe_push (cp);
39193
39194 if (first_p)
39195 {
39196 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
39197 cp_parser_pragma (parser, context, NULL);
39198 switch (context)
39199 {
39200 case pragma_external:
39201 cp_parser_declaration (parser);
39202 break;
39203 case pragma_member:
39204 cp_parser_member_declaration (parser);
39205 break;
39206 case pragma_objc_icode:
39207 cp_parser_block_declaration (parser, /*statement_p=*/false);
39208 break;
39209 default:
39210 cp_parser_declaration_statement (parser);
39211 break;
39212 }
39213 if (parser->omp_declare_simd
39214 && !parser->omp_declare_simd->error_seen
39215 && !parser->omp_declare_simd->fndecl_seen)
39216 error_at (pragma_tok->location,
39217 "%<#pragma omp declare simd%> not immediately followed by "
39218 "function declaration or definition");
39219 data.tokens.release ();
39220 parser->omp_declare_simd = NULL;
39221 }
39222 }
39223
39224 /* Finalize #pragma omp declare simd clauses after direct declarator has
39225 been parsed, and put that into "omp declare simd" attribute. */
39226
39227 static tree
39228 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
39229 {
39230 struct cp_token_cache *ce;
39231 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
39232 int i;
39233
39234 if (!data->error_seen && data->fndecl_seen)
39235 {
39236 error ("%<#pragma omp declare simd%> not immediately followed by "
39237 "a single function declaration or definition");
39238 data->error_seen = true;
39239 }
39240 if (data->error_seen)
39241 return attrs;
39242
39243 FOR_EACH_VEC_ELT (data->tokens, i, ce)
39244 {
39245 tree c, cl;
39246
39247 cp_parser_push_lexer_for_tokens (parser, ce);
39248 parser->lexer->in_pragma = true;
39249 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
39250 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
39251 cp_lexer_consume_token (parser->lexer);
39252 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
39253 "#pragma omp declare simd", pragma_tok);
39254 cp_parser_pop_lexer (parser);
39255 if (cl)
39256 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
39257 c = build_tree_list (get_identifier ("omp declare simd"), cl);
39258 TREE_CHAIN (c) = attrs;
39259 if (processing_template_decl)
39260 ATTR_IS_DEPENDENT (c) = 1;
39261 attrs = c;
39262 }
39263
39264 data->fndecl_seen = true;
39265 return attrs;
39266 }
39267
39268
39269 /* OpenMP 4.0:
39270 # pragma omp declare target new-line
39271 declarations and definitions
39272 # pragma omp end declare target new-line
39273
39274 OpenMP 4.5:
39275 # pragma omp declare target ( extended-list ) new-line
39276
39277 # pragma omp declare target declare-target-clauses[seq] new-line */
39278
39279 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
39280 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
39281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
39282
39283 static void
39284 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
39285 {
39286 tree clauses = NULL_TREE;
39287 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39288 clauses
39289 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
39290 "#pragma omp declare target", pragma_tok);
39291 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39292 {
39293 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
39294 clauses);
39295 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
39296 cp_parser_require_pragma_eol (parser, pragma_tok);
39297 }
39298 else
39299 {
39300 cp_parser_require_pragma_eol (parser, pragma_tok);
39301 scope_chain->omp_declare_target_attribute++;
39302 return;
39303 }
39304 if (scope_chain->omp_declare_target_attribute)
39305 error_at (pragma_tok->location,
39306 "%<#pragma omp declare target%> with clauses in between "
39307 "%<#pragma omp declare target%> without clauses and "
39308 "%<#pragma omp end declare target%>");
39309 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
39310 {
39311 tree t = OMP_CLAUSE_DECL (c), id;
39312 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
39313 tree at2 = lookup_attribute ("omp declare target link",
39314 DECL_ATTRIBUTES (t));
39315 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
39316 {
39317 id = get_identifier ("omp declare target link");
39318 std::swap (at1, at2);
39319 }
39320 else
39321 id = get_identifier ("omp declare target");
39322 if (at2)
39323 {
39324 error_at (OMP_CLAUSE_LOCATION (c),
39325 "%qD specified both in declare target %<link%> and %<to%>"
39326 " clauses", t);
39327 continue;
39328 }
39329 if (!at1)
39330 {
39331 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
39332 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
39333 continue;
39334
39335 symtab_node *node = symtab_node::get (t);
39336 if (node != NULL)
39337 {
39338 node->offloadable = 1;
39339 if (ENABLE_OFFLOADING)
39340 {
39341 g->have_offload = true;
39342 if (is_a <varpool_node *> (node))
39343 vec_safe_push (offload_vars, t);
39344 }
39345 }
39346 }
39347 }
39348 }
39349
39350 static void
39351 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
39352 {
39353 const char *p = "";
39354 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39355 {
39356 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39357 p = IDENTIFIER_POINTER (id);
39358 }
39359 if (strcmp (p, "declare") == 0)
39360 {
39361 cp_lexer_consume_token (parser->lexer);
39362 p = "";
39363 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39364 {
39365 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39366 p = IDENTIFIER_POINTER (id);
39367 }
39368 if (strcmp (p, "target") == 0)
39369 cp_lexer_consume_token (parser->lexer);
39370 else
39371 {
39372 cp_parser_error (parser, "expected %<target%>");
39373 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39374 return;
39375 }
39376 }
39377 else
39378 {
39379 cp_parser_error (parser, "expected %<declare%>");
39380 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39381 return;
39382 }
39383 cp_parser_require_pragma_eol (parser, pragma_tok);
39384 if (!scope_chain->omp_declare_target_attribute)
39385 error_at (pragma_tok->location,
39386 "%<#pragma omp end declare target%> without corresponding "
39387 "%<#pragma omp declare target%>");
39388 else
39389 scope_chain->omp_declare_target_attribute--;
39390 }
39391
39392 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
39393 expression and optional initializer clause of
39394 #pragma omp declare reduction. We store the expression(s) as
39395 either 3, 6 or 7 special statements inside of the artificial function's
39396 body. The first two statements are DECL_EXPRs for the artificial
39397 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
39398 expression that uses those variables.
39399 If there was any INITIALIZER clause, this is followed by further statements,
39400 the fourth and fifth statements are DECL_EXPRs for the artificial
39401 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
39402 constructor variant (first token after open paren is not omp_priv),
39403 then the sixth statement is a statement with the function call expression
39404 that uses the OMP_PRIV and optionally OMP_ORIG variable.
39405 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
39406 to initialize the OMP_PRIV artificial variable and there is seventh
39407 statement, a DECL_EXPR of the OMP_PRIV statement again. */
39408
39409 static bool
39410 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
39411 {
39412 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
39413 gcc_assert (TYPE_REF_P (type));
39414 type = TREE_TYPE (type);
39415 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
39416 DECL_ARTIFICIAL (omp_out) = 1;
39417 pushdecl (omp_out);
39418 add_decl_expr (omp_out);
39419 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
39420 DECL_ARTIFICIAL (omp_in) = 1;
39421 pushdecl (omp_in);
39422 add_decl_expr (omp_in);
39423 tree combiner;
39424 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
39425
39426 keep_next_level (true);
39427 tree block = begin_omp_structured_block ();
39428 combiner = cp_parser_expression (parser);
39429 finish_expr_stmt (combiner);
39430 block = finish_omp_structured_block (block);
39431 add_stmt (block);
39432
39433 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
39434 return false;
39435
39436 const char *p = "";
39437 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39438 {
39439 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39440 p = IDENTIFIER_POINTER (id);
39441 }
39442
39443 if (strcmp (p, "initializer") == 0)
39444 {
39445 cp_lexer_consume_token (parser->lexer);
39446 matching_parens parens;
39447 if (!parens.require_open (parser))
39448 return false;
39449
39450 p = "";
39451 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39452 {
39453 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39454 p = IDENTIFIER_POINTER (id);
39455 }
39456
39457 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
39458 DECL_ARTIFICIAL (omp_priv) = 1;
39459 pushdecl (omp_priv);
39460 add_decl_expr (omp_priv);
39461 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
39462 DECL_ARTIFICIAL (omp_orig) = 1;
39463 pushdecl (omp_orig);
39464 add_decl_expr (omp_orig);
39465
39466 keep_next_level (true);
39467 block = begin_omp_structured_block ();
39468
39469 bool ctor = false;
39470 if (strcmp (p, "omp_priv") == 0)
39471 {
39472 bool is_direct_init, is_non_constant_init;
39473 ctor = true;
39474 cp_lexer_consume_token (parser->lexer);
39475 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
39476 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
39477 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39478 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
39479 == CPP_CLOSE_PAREN
39480 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
39481 == CPP_CLOSE_PAREN))
39482 {
39483 finish_omp_structured_block (block);
39484 error ("invalid initializer clause");
39485 return false;
39486 }
39487 initializer = cp_parser_initializer (parser, &is_direct_init,
39488 &is_non_constant_init);
39489 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
39490 NULL_TREE, LOOKUP_ONLYCONVERTING);
39491 }
39492 else
39493 {
39494 cp_parser_parse_tentatively (parser);
39495 /* Don't create location wrapper nodes here. */
39496 auto_suppress_location_wrappers sentinel;
39497 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
39498 /*check_dependency_p=*/true,
39499 /*template_p=*/NULL,
39500 /*declarator_p=*/false,
39501 /*optional_p=*/false);
39502 vec<tree, va_gc> *args;
39503 if (fn_name == error_mark_node
39504 || cp_parser_error_occurred (parser)
39505 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39506 || ((args = cp_parser_parenthesized_expression_list
39507 (parser, non_attr, /*cast_p=*/false,
39508 /*allow_expansion_p=*/true,
39509 /*non_constant_p=*/NULL)),
39510 cp_parser_error_occurred (parser)))
39511 {
39512 finish_omp_structured_block (block);
39513 cp_parser_abort_tentative_parse (parser);
39514 cp_parser_error (parser, "expected id-expression (arguments)");
39515 return false;
39516 }
39517 unsigned int i;
39518 tree arg;
39519 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
39520 if (arg == omp_priv
39521 || (TREE_CODE (arg) == ADDR_EXPR
39522 && TREE_OPERAND (arg, 0) == omp_priv))
39523 break;
39524 cp_parser_abort_tentative_parse (parser);
39525 if (arg == NULL_TREE)
39526 error ("one of the initializer call arguments should be %<omp_priv%>"
39527 " or %<&omp_priv%>");
39528 initializer = cp_parser_postfix_expression (parser, false, false, false,
39529 false, NULL);
39530 finish_expr_stmt (initializer);
39531 }
39532
39533 block = finish_omp_structured_block (block);
39534 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
39535 add_stmt (block);
39536
39537 if (ctor)
39538 add_decl_expr (omp_orig);
39539
39540 if (!parens.require_close (parser))
39541 return false;
39542 }
39543
39544 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
39545 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
39546 UNKNOWN_LOCATION);
39547
39548 return true;
39549 }
39550
39551 /* OpenMP 4.0
39552 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39553 initializer-clause[opt] new-line
39554
39555 initializer-clause:
39556 initializer (omp_priv initializer)
39557 initializer (function-name (argument-list)) */
39558
39559 static void
39560 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
39561 enum pragma_context)
39562 {
39563 auto_vec<tree> types;
39564 enum tree_code reduc_code = ERROR_MARK;
39565 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
39566 unsigned int i;
39567 cp_token *first_token;
39568 cp_token_cache *cp;
39569 int errs;
39570 void *p;
39571
39572 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39573 p = obstack_alloc (&declarator_obstack, 0);
39574
39575 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39576 goto fail;
39577
39578 switch (cp_lexer_peek_token (parser->lexer)->type)
39579 {
39580 case CPP_PLUS:
39581 reduc_code = PLUS_EXPR;
39582 break;
39583 case CPP_MULT:
39584 reduc_code = MULT_EXPR;
39585 break;
39586 case CPP_MINUS:
39587 reduc_code = MINUS_EXPR;
39588 break;
39589 case CPP_AND:
39590 reduc_code = BIT_AND_EXPR;
39591 break;
39592 case CPP_XOR:
39593 reduc_code = BIT_XOR_EXPR;
39594 break;
39595 case CPP_OR:
39596 reduc_code = BIT_IOR_EXPR;
39597 break;
39598 case CPP_AND_AND:
39599 reduc_code = TRUTH_ANDIF_EXPR;
39600 break;
39601 case CPP_OR_OR:
39602 reduc_code = TRUTH_ORIF_EXPR;
39603 break;
39604 case CPP_NAME:
39605 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
39606 break;
39607 default:
39608 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39609 "%<|%>, %<&&%>, %<||%> or identifier");
39610 goto fail;
39611 }
39612
39613 if (reduc_code != ERROR_MARK)
39614 cp_lexer_consume_token (parser->lexer);
39615
39616 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
39617 if (reduc_id == error_mark_node)
39618 goto fail;
39619
39620 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39621 goto fail;
39622
39623 /* Types may not be defined in declare reduction type list. */
39624 const char *saved_message;
39625 saved_message = parser->type_definition_forbidden_message;
39626 parser->type_definition_forbidden_message
39627 = G_("types may not be defined in declare reduction type list");
39628 bool saved_colon_corrects_to_scope_p;
39629 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39630 parser->colon_corrects_to_scope_p = false;
39631 bool saved_colon_doesnt_start_class_def_p;
39632 saved_colon_doesnt_start_class_def_p
39633 = parser->colon_doesnt_start_class_def_p;
39634 parser->colon_doesnt_start_class_def_p = true;
39635
39636 while (true)
39637 {
39638 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39639 type = cp_parser_type_id (parser);
39640 if (type == error_mark_node)
39641 ;
39642 else if (ARITHMETIC_TYPE_P (type)
39643 && (orig_reduc_id == NULL_TREE
39644 || (TREE_CODE (type) != COMPLEX_TYPE
39645 && (id_equal (orig_reduc_id, "min")
39646 || id_equal (orig_reduc_id, "max")))))
39647 error_at (loc, "predeclared arithmetic type %qT in "
39648 "%<#pragma omp declare reduction%>", type);
39649 else if (TREE_CODE (type) == FUNCTION_TYPE
39650 || TREE_CODE (type) == METHOD_TYPE
39651 || TREE_CODE (type) == ARRAY_TYPE)
39652 error_at (loc, "function or array type %qT in "
39653 "%<#pragma omp declare reduction%>", type);
39654 else if (TYPE_REF_P (type))
39655 error_at (loc, "reference type %qT in "
39656 "%<#pragma omp declare reduction%>", type);
39657 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
39658 error_at (loc, "const, volatile or __restrict qualified type %qT in "
39659 "%<#pragma omp declare reduction%>", type);
39660 else
39661 types.safe_push (type);
39662
39663 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39664 cp_lexer_consume_token (parser->lexer);
39665 else
39666 break;
39667 }
39668
39669 /* Restore the saved message. */
39670 parser->type_definition_forbidden_message = saved_message;
39671 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39672 parser->colon_doesnt_start_class_def_p
39673 = saved_colon_doesnt_start_class_def_p;
39674
39675 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
39676 || types.is_empty ())
39677 {
39678 fail:
39679 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39680 goto done;
39681 }
39682
39683 first_token = cp_lexer_peek_token (parser->lexer);
39684 cp = NULL;
39685 errs = errorcount;
39686 FOR_EACH_VEC_ELT (types, i, type)
39687 {
39688 tree fntype
39689 = build_function_type_list (void_type_node,
39690 cp_build_reference_type (type, false),
39691 NULL_TREE);
39692 tree this_reduc_id = reduc_id;
39693 if (!dependent_type_p (type))
39694 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
39695 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
39696 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
39697 DECL_ARTIFICIAL (fndecl) = 1;
39698 DECL_EXTERNAL (fndecl) = 1;
39699 DECL_DECLARED_INLINE_P (fndecl) = 1;
39700 DECL_IGNORED_P (fndecl) = 1;
39701 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
39702 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
39703 DECL_ATTRIBUTES (fndecl)
39704 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
39705 DECL_ATTRIBUTES (fndecl));
39706 if (processing_template_decl)
39707 fndecl = push_template_decl (fndecl);
39708 bool block_scope = false;
39709 tree block = NULL_TREE;
39710 if (current_function_decl)
39711 {
39712 block_scope = true;
39713 DECL_CONTEXT (fndecl) = global_namespace;
39714 if (!processing_template_decl)
39715 pushdecl (fndecl);
39716 }
39717 else if (current_class_type)
39718 {
39719 if (cp == NULL)
39720 {
39721 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39722 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39723 cp_lexer_consume_token (parser->lexer);
39724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39725 goto fail;
39726 cp = cp_token_cache_new (first_token,
39727 cp_lexer_peek_nth_token (parser->lexer,
39728 2));
39729 }
39730 DECL_STATIC_FUNCTION_P (fndecl) = 1;
39731 finish_member_declaration (fndecl);
39732 DECL_PENDING_INLINE_INFO (fndecl) = cp;
39733 DECL_PENDING_INLINE_P (fndecl) = 1;
39734 vec_safe_push (unparsed_funs_with_definitions, fndecl);
39735 continue;
39736 }
39737 else
39738 {
39739 DECL_CONTEXT (fndecl) = current_namespace;
39740 pushdecl (fndecl);
39741 }
39742 if (!block_scope)
39743 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
39744 else
39745 block = begin_omp_structured_block ();
39746 if (cp)
39747 {
39748 cp_parser_push_lexer_for_tokens (parser, cp);
39749 parser->lexer->in_pragma = true;
39750 }
39751 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
39752 {
39753 if (!block_scope)
39754 finish_function (/*inline_p=*/false);
39755 else
39756 DECL_CONTEXT (fndecl) = current_function_decl;
39757 if (cp)
39758 cp_parser_pop_lexer (parser);
39759 goto fail;
39760 }
39761 if (cp)
39762 cp_parser_pop_lexer (parser);
39763 if (!block_scope)
39764 finish_function (/*inline_p=*/false);
39765 else
39766 {
39767 DECL_CONTEXT (fndecl) = current_function_decl;
39768 block = finish_omp_structured_block (block);
39769 if (TREE_CODE (block) == BIND_EXPR)
39770 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
39771 else if (TREE_CODE (block) == STATEMENT_LIST)
39772 DECL_SAVED_TREE (fndecl) = block;
39773 if (processing_template_decl)
39774 add_decl_expr (fndecl);
39775 }
39776 cp_check_omp_declare_reduction (fndecl);
39777 if (cp == NULL && types.length () > 1)
39778 cp = cp_token_cache_new (first_token,
39779 cp_lexer_peek_nth_token (parser->lexer, 2));
39780 if (errs != errorcount)
39781 break;
39782 }
39783
39784 cp_parser_require_pragma_eol (parser, pragma_tok);
39785
39786 done:
39787 /* Free any declarators allocated. */
39788 obstack_free (&declarator_obstack, p);
39789 }
39790
39791 /* OpenMP 4.0
39792 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39793 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39794 initializer-clause[opt] new-line
39795 #pragma omp declare target new-line */
39796
39797 static bool
39798 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
39799 enum pragma_context context)
39800 {
39801 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39802 {
39803 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39804 const char *p = IDENTIFIER_POINTER (id);
39805
39806 if (strcmp (p, "simd") == 0)
39807 {
39808 cp_lexer_consume_token (parser->lexer);
39809 cp_parser_omp_declare_simd (parser, pragma_tok,
39810 context);
39811 return true;
39812 }
39813 cp_ensure_no_omp_declare_simd (parser);
39814 if (strcmp (p, "reduction") == 0)
39815 {
39816 cp_lexer_consume_token (parser->lexer);
39817 cp_parser_omp_declare_reduction (parser, pragma_tok,
39818 context);
39819 return false;
39820 }
39821 if (!flag_openmp) /* flag_openmp_simd */
39822 {
39823 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39824 return false;
39825 }
39826 if (strcmp (p, "target") == 0)
39827 {
39828 cp_lexer_consume_token (parser->lexer);
39829 cp_parser_omp_declare_target (parser, pragma_tok);
39830 return false;
39831 }
39832 }
39833 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
39834 "or %<target%>");
39835 cp_parser_require_pragma_eol (parser, pragma_tok);
39836 return false;
39837 }
39838
39839 /* OpenMP 5.0
39840 #pragma omp requires clauses[optseq] new-line */
39841
39842 static bool
39843 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
39844 {
39845 bool first = true;
39846 enum omp_requires new_req = (enum omp_requires) 0;
39847
39848 location_t loc = pragma_tok->location;
39849 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39850 {
39851 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39852 cp_lexer_consume_token (parser->lexer);
39853
39854 first = false;
39855
39856 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39857 {
39858 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39859 const char *p = IDENTIFIER_POINTER (id);
39860 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
39861 enum omp_requires this_req = (enum omp_requires) 0;
39862
39863 if (!strcmp (p, "unified_address"))
39864 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
39865 else if (!strcmp (p, "unified_shared_memory"))
39866 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
39867 else if (!strcmp (p, "dynamic_allocators"))
39868 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
39869 else if (!strcmp (p, "reverse_offload"))
39870 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
39871 else if (!strcmp (p, "atomic_default_mem_order"))
39872 {
39873 cp_lexer_consume_token (parser->lexer);
39874
39875 matching_parens parens;
39876 if (parens.require_open (parser))
39877 {
39878 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39879 {
39880 id = cp_lexer_peek_token (parser->lexer)->u.value;
39881 p = IDENTIFIER_POINTER (id);
39882
39883 if (!strcmp (p, "seq_cst"))
39884 this_req
39885 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
39886 else if (!strcmp (p, "relaxed"))
39887 this_req
39888 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
39889 else if (!strcmp (p, "acq_rel"))
39890 this_req
39891 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
39892 }
39893 if (this_req == 0)
39894 {
39895 error_at (cp_lexer_peek_token (parser->lexer)->location,
39896 "expected %<seq_cst%>, %<relaxed%> or "
39897 "%<acq_rel%>");
39898 if (cp_lexer_nth_token_is (parser->lexer, 2,
39899 CPP_CLOSE_PAREN))
39900 cp_lexer_consume_token (parser->lexer);
39901 }
39902 else
39903 cp_lexer_consume_token (parser->lexer);
39904
39905 if (!parens.require_close (parser))
39906 cp_parser_skip_to_closing_parenthesis (parser,
39907 /*recovering=*/true,
39908 /*or_comma=*/false,
39909 /*consume_paren=*/
39910 true);
39911
39912 if (this_req == 0)
39913 {
39914 cp_parser_require_pragma_eol (parser, pragma_tok);
39915 return false;
39916 }
39917 }
39918 p = NULL;
39919 }
39920 else
39921 {
39922 error_at (cloc, "expected %<unified_address%>, "
39923 "%<unified_shared_memory%>, "
39924 "%<dynamic_allocators%>, "
39925 "%<reverse_offload%> "
39926 "or %<atomic_default_mem_order%> clause");
39927 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39928 return false;
39929 }
39930 if (p)
39931 sorry_at (cloc, "%qs clause on %<requires%> directive not "
39932 "supported yet", p);
39933 if (p)
39934 cp_lexer_consume_token (parser->lexer);
39935 if (this_req)
39936 {
39937 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39938 {
39939 if ((this_req & new_req) != 0)
39940 error_at (cloc, "too many %qs clauses", p);
39941 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
39942 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
39943 error_at (cloc, "%qs clause used lexically after first "
39944 "target construct or offloading API", p);
39945 }
39946 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39947 {
39948 error_at (cloc, "too many %qs clauses",
39949 "atomic_default_mem_order");
39950 this_req = (enum omp_requires) 0;
39951 }
39952 else if ((omp_requires_mask
39953 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39954 {
39955 error_at (cloc, "more than one %<atomic_default_mem_order%>"
39956 " clause in a single compilation unit");
39957 this_req
39958 = (enum omp_requires)
39959 (omp_requires_mask
39960 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
39961 }
39962 else if ((omp_requires_mask
39963 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
39964 error_at (cloc, "%<atomic_default_mem_order%> clause used "
39965 "lexically after first %<atomic%> construct "
39966 "without memory order clause");
39967 new_req = (enum omp_requires) (new_req | this_req);
39968 omp_requires_mask
39969 = (enum omp_requires) (omp_requires_mask | this_req);
39970 continue;
39971 }
39972 }
39973 break;
39974 }
39975 cp_parser_require_pragma_eol (parser, pragma_tok);
39976
39977 if (new_req == 0)
39978 error_at (loc, "%<pragma omp requires%> requires at least one clause");
39979 return false;
39980 }
39981
39982
39983 /* OpenMP 4.5:
39984 #pragma omp taskloop taskloop-clause[optseq] new-line
39985 for-loop
39986
39987 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39988 for-loop */
39989
39990 #define OMP_TASKLOOP_CLAUSE_MASK \
39991 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
40000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
40002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
40003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
40004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
40005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
40007
40008 static tree
40009 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
40010 char *p_name, omp_clause_mask mask, tree *cclauses,
40011 bool *if_p)
40012 {
40013 tree clauses, sb, ret;
40014 unsigned int save;
40015 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40016
40017 strcat (p_name, " taskloop");
40018 mask |= OMP_TASKLOOP_CLAUSE_MASK;
40019 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
40020 clause. */
40021 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
40022 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
40023
40024 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40025 {
40026 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40027 const char *p = IDENTIFIER_POINTER (id);
40028
40029 if (strcmp (p, "simd") == 0)
40030 {
40031 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40032 if (cclauses == NULL)
40033 cclauses = cclauses_buf;
40034
40035 cp_lexer_consume_token (parser->lexer);
40036 if (!flag_openmp) /* flag_openmp_simd */
40037 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40038 cclauses, if_p);
40039 sb = begin_omp_structured_block ();
40040 save = cp_parser_begin_omp_structured_block (parser);
40041 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40042 cclauses, if_p);
40043 cp_parser_end_omp_structured_block (parser, save);
40044 tree body = finish_omp_structured_block (sb);
40045 if (ret == NULL)
40046 return ret;
40047 ret = make_node (OMP_TASKLOOP);
40048 TREE_TYPE (ret) = void_type_node;
40049 OMP_FOR_BODY (ret) = body;
40050 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
40051 SET_EXPR_LOCATION (ret, loc);
40052 add_stmt (ret);
40053 return ret;
40054 }
40055 }
40056 if (!flag_openmp) /* flag_openmp_simd */
40057 {
40058 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40059 return NULL_TREE;
40060 }
40061
40062 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40063 cclauses == NULL);
40064 if (cclauses)
40065 {
40066 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
40067 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
40068 }
40069
40070 keep_next_level (true);
40071 sb = begin_omp_structured_block ();
40072 save = cp_parser_begin_omp_structured_block (parser);
40073
40074 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
40075 if_p);
40076
40077 cp_parser_end_omp_structured_block (parser, save);
40078 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
40079
40080 return ret;
40081 }
40082
40083
40084 /* OpenACC 2.0:
40085 # pragma acc routine oacc-routine-clause[optseq] new-line
40086 function-definition
40087
40088 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
40089 */
40090
40091 #define OACC_ROUTINE_CLAUSE_MASK \
40092 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
40093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
40094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
40095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
40096
40097
40098 /* Parse the OpenACC routine pragma. This has an optional '( name )'
40099 component, which must resolve to a declared namespace-scope
40100 function. The clauses are either processed directly (for a named
40101 function), or defered until the immediatley following declaration
40102 is parsed. */
40103
40104 static void
40105 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
40106 enum pragma_context context)
40107 {
40108 gcc_checking_assert (context == pragma_external);
40109 /* The checking for "another pragma following this one" in the "no optional
40110 '( name )'" case makes sure that we dont re-enter. */
40111 gcc_checking_assert (parser->oacc_routine == NULL);
40112
40113 cp_oacc_routine_data data;
40114 data.error_seen = false;
40115 data.fndecl_seen = false;
40116 data.tokens = vNULL;
40117 data.clauses = NULL_TREE;
40118 data.loc = pragma_tok->location;
40119 /* It is safe to take the address of a local variable; it will only be
40120 used while this scope is live. */
40121 parser->oacc_routine = &data;
40122
40123 /* Look for optional '( name )'. */
40124 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
40125 {
40126 matching_parens parens;
40127 parens.consume_open (parser); /* '(' */
40128
40129 /* We parse the name as an id-expression. If it resolves to
40130 anything other than a non-overloaded function at namespace
40131 scope, it's an error. */
40132 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
40133 tree name = cp_parser_id_expression (parser,
40134 /*template_keyword_p=*/false,
40135 /*check_dependency_p=*/false,
40136 /*template_p=*/NULL,
40137 /*declarator_p=*/false,
40138 /*optional_p=*/false);
40139 tree decl = (identifier_p (name)
40140 ? cp_parser_lookup_name_simple (parser, name, name_loc)
40141 : name);
40142 if (name != error_mark_node && decl == error_mark_node)
40143 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
40144
40145 if (decl == error_mark_node
40146 || !parens.require_close (parser))
40147 {
40148 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40149 parser->oacc_routine = NULL;
40150 return;
40151 }
40152
40153 data.clauses
40154 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40155 "#pragma acc routine",
40156 cp_lexer_peek_token (parser->lexer));
40157
40158 if (decl && is_overloaded_fn (decl)
40159 && (TREE_CODE (decl) != FUNCTION_DECL
40160 || DECL_FUNCTION_TEMPLATE_P (decl)))
40161 {
40162 error_at (name_loc,
40163 "%<#pragma acc routine%> names a set of overloads");
40164 parser->oacc_routine = NULL;
40165 return;
40166 }
40167
40168 /* Perhaps we should use the same rule as declarations in different
40169 namespaces? */
40170 if (!DECL_NAMESPACE_SCOPE_P (decl))
40171 {
40172 error_at (name_loc,
40173 "%qD does not refer to a namespace scope function", decl);
40174 parser->oacc_routine = NULL;
40175 return;
40176 }
40177
40178 if (TREE_CODE (decl) != FUNCTION_DECL)
40179 {
40180 error_at (name_loc, "%qD does not refer to a function", decl);
40181 parser->oacc_routine = NULL;
40182 return;
40183 }
40184
40185 cp_finalize_oacc_routine (parser, decl, false);
40186 parser->oacc_routine = NULL;
40187 }
40188 else /* No optional '( name )'. */
40189 {
40190 /* Store away all pragma tokens. */
40191 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
40192 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
40193 cp_lexer_consume_token (parser->lexer);
40194 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40195 parser->oacc_routine->error_seen = true;
40196 cp_parser_require_pragma_eol (parser, pragma_tok);
40197 struct cp_token_cache *cp
40198 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
40199 parser->oacc_routine->tokens.safe_push (cp);
40200
40201 /* Emit a helpful diagnostic if there's another pragma following this
40202 one. */
40203 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
40204 {
40205 cp_ensure_no_oacc_routine (parser);
40206 data.tokens.release ();
40207 /* ..., and then just keep going. */
40208 return;
40209 }
40210
40211 /* We only have to consider the pragma_external case here. */
40212 cp_parser_declaration (parser);
40213 if (parser->oacc_routine
40214 && !parser->oacc_routine->fndecl_seen)
40215 cp_ensure_no_oacc_routine (parser);
40216 else
40217 parser->oacc_routine = NULL;
40218 data.tokens.release ();
40219 }
40220 }
40221
40222 /* Finalize #pragma acc routine clauses after direct declarator has
40223 been parsed. */
40224
40225 static tree
40226 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
40227 {
40228 struct cp_token_cache *ce;
40229 cp_oacc_routine_data *data = parser->oacc_routine;
40230
40231 if (!data->error_seen && data->fndecl_seen)
40232 {
40233 error_at (data->loc,
40234 "%<#pragma acc routine%> not immediately followed by "
40235 "a single function declaration or definition");
40236 data->error_seen = true;
40237 }
40238 if (data->error_seen)
40239 return attrs;
40240
40241 gcc_checking_assert (data->tokens.length () == 1);
40242 ce = data->tokens[0];
40243
40244 cp_parser_push_lexer_for_tokens (parser, ce);
40245 parser->lexer->in_pragma = true;
40246 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
40247
40248 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
40249 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
40250 parser->oacc_routine->clauses
40251 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40252 "#pragma acc routine", pragma_tok);
40253 cp_parser_pop_lexer (parser);
40254 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
40255 fndecl_seen. */
40256
40257 return attrs;
40258 }
40259
40260 /* Apply any saved OpenACC routine clauses to a just-parsed
40261 declaration. */
40262
40263 static void
40264 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
40265 {
40266 if (__builtin_expect (parser->oacc_routine != NULL, 0))
40267 {
40268 /* Keep going if we're in error reporting mode. */
40269 if (parser->oacc_routine->error_seen
40270 || fndecl == error_mark_node)
40271 return;
40272
40273 if (parser->oacc_routine->fndecl_seen)
40274 {
40275 error_at (parser->oacc_routine->loc,
40276 "%<#pragma acc routine%> not immediately followed by"
40277 " a single function declaration or definition");
40278 parser->oacc_routine = NULL;
40279 return;
40280 }
40281 if (TREE_CODE (fndecl) != FUNCTION_DECL)
40282 {
40283 cp_ensure_no_oacc_routine (parser);
40284 return;
40285 }
40286
40287 if (oacc_get_fn_attrib (fndecl))
40288 {
40289 error_at (parser->oacc_routine->loc,
40290 "%<#pragma acc routine%> already applied to %qD", fndecl);
40291 parser->oacc_routine = NULL;
40292 return;
40293 }
40294
40295 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
40296 {
40297 error_at (parser->oacc_routine->loc,
40298 TREE_USED (fndecl)
40299 ? G_("%<#pragma acc routine%> must be applied before use")
40300 : G_("%<#pragma acc routine%> must be applied before "
40301 "definition"));
40302 parser->oacc_routine = NULL;
40303 return;
40304 }
40305
40306 /* Process the routine's dimension clauses. */
40307 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
40308 oacc_replace_fn_attrib (fndecl, dims);
40309
40310 /* Add an "omp declare target" attribute. */
40311 DECL_ATTRIBUTES (fndecl)
40312 = tree_cons (get_identifier ("omp declare target"),
40313 NULL_TREE, DECL_ATTRIBUTES (fndecl));
40314
40315 /* Don't unset parser->oacc_routine here: we may still need it to
40316 diagnose wrong usage. But, remember that we've used this "#pragma acc
40317 routine". */
40318 parser->oacc_routine->fndecl_seen = true;
40319 }
40320 }
40321
40322 /* Main entry point to OpenMP statement pragmas. */
40323
40324 static void
40325 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40326 {
40327 tree stmt;
40328 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
40329 omp_clause_mask mask (0);
40330
40331 switch (cp_parser_pragma_kind (pragma_tok))
40332 {
40333 case PRAGMA_OACC_ATOMIC:
40334 cp_parser_omp_atomic (parser, pragma_tok);
40335 return;
40336 case PRAGMA_OACC_CACHE:
40337 stmt = cp_parser_oacc_cache (parser, pragma_tok);
40338 break;
40339 case PRAGMA_OACC_DATA:
40340 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
40341 break;
40342 case PRAGMA_OACC_ENTER_DATA:
40343 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
40344 break;
40345 case PRAGMA_OACC_EXIT_DATA:
40346 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
40347 break;
40348 case PRAGMA_OACC_HOST_DATA:
40349 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
40350 break;
40351 case PRAGMA_OACC_KERNELS:
40352 case PRAGMA_OACC_PARALLEL:
40353 strcpy (p_name, "#pragma acc");
40354 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
40355 if_p);
40356 break;
40357 case PRAGMA_OACC_LOOP:
40358 strcpy (p_name, "#pragma acc");
40359 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
40360 if_p);
40361 break;
40362 case PRAGMA_OACC_UPDATE:
40363 stmt = cp_parser_oacc_update (parser, pragma_tok);
40364 break;
40365 case PRAGMA_OACC_WAIT:
40366 stmt = cp_parser_oacc_wait (parser, pragma_tok);
40367 break;
40368 case PRAGMA_OMP_ATOMIC:
40369 cp_parser_omp_atomic (parser, pragma_tok);
40370 return;
40371 case PRAGMA_OMP_CRITICAL:
40372 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
40373 break;
40374 case PRAGMA_OMP_DISTRIBUTE:
40375 strcpy (p_name, "#pragma omp");
40376 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
40377 if_p);
40378 break;
40379 case PRAGMA_OMP_FOR:
40380 strcpy (p_name, "#pragma omp");
40381 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
40382 if_p);
40383 break;
40384 case PRAGMA_OMP_MASTER:
40385 strcpy (p_name, "#pragma omp");
40386 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
40387 if_p);
40388 break;
40389 case PRAGMA_OMP_PARALLEL:
40390 strcpy (p_name, "#pragma omp");
40391 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
40392 if_p);
40393 break;
40394 case PRAGMA_OMP_SECTIONS:
40395 strcpy (p_name, "#pragma omp");
40396 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
40397 break;
40398 case PRAGMA_OMP_SIMD:
40399 strcpy (p_name, "#pragma omp");
40400 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
40401 if_p);
40402 break;
40403 case PRAGMA_OMP_SINGLE:
40404 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
40405 break;
40406 case PRAGMA_OMP_TASK:
40407 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
40408 break;
40409 case PRAGMA_OMP_TASKGROUP:
40410 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
40411 break;
40412 case PRAGMA_OMP_TASKLOOP:
40413 strcpy (p_name, "#pragma omp");
40414 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
40415 if_p);
40416 break;
40417 case PRAGMA_OMP_TEAMS:
40418 strcpy (p_name, "#pragma omp");
40419 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
40420 if_p);
40421 break;
40422 default:
40423 gcc_unreachable ();
40424 }
40425
40426 protected_set_expr_location (stmt, pragma_tok->location);
40427 }
40428 \f
40429 /* Transactional Memory parsing routines. */
40430
40431 /* Parse a transaction attribute.
40432
40433 txn-attribute:
40434 attribute
40435 [ [ identifier ] ]
40436
40437 We use this instead of cp_parser_attributes_opt for transactions to avoid
40438 the pedwarn in C++98 mode. */
40439
40440 static tree
40441 cp_parser_txn_attribute_opt (cp_parser *parser)
40442 {
40443 cp_token *token;
40444 tree attr_name, attr = NULL;
40445
40446 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
40447 return cp_parser_attributes_opt (parser);
40448
40449 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
40450 return NULL_TREE;
40451 cp_lexer_consume_token (parser->lexer);
40452 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
40453 goto error1;
40454
40455 token = cp_lexer_peek_token (parser->lexer);
40456 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
40457 {
40458 token = cp_lexer_consume_token (parser->lexer);
40459
40460 attr_name = (token->type == CPP_KEYWORD
40461 /* For keywords, use the canonical spelling,
40462 not the parsed identifier. */
40463 ? ridpointers[(int) token->keyword]
40464 : token->u.value);
40465 attr = build_tree_list (attr_name, NULL_TREE);
40466 }
40467 else
40468 cp_parser_error (parser, "expected identifier");
40469
40470 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40471 error1:
40472 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40473 return attr;
40474 }
40475
40476 /* Parse a __transaction_atomic or __transaction_relaxed statement.
40477
40478 transaction-statement:
40479 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
40480 compound-statement
40481 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
40482 */
40483
40484 static tree
40485 cp_parser_transaction (cp_parser *parser, cp_token *token)
40486 {
40487 unsigned char old_in = parser->in_transaction;
40488 unsigned char this_in = 1, new_in;
40489 enum rid keyword = token->keyword;
40490 tree stmt, attrs, noex;
40491
40492 cp_lexer_consume_token (parser->lexer);
40493
40494 if (keyword == RID_TRANSACTION_RELAXED
40495 || keyword == RID_SYNCHRONIZED)
40496 this_in |= TM_STMT_ATTR_RELAXED;
40497 else
40498 {
40499 attrs = cp_parser_txn_attribute_opt (parser);
40500 if (attrs)
40501 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40502 }
40503
40504 /* Parse a noexcept specification. */
40505 if (keyword == RID_ATOMIC_NOEXCEPT)
40506 noex = boolean_true_node;
40507 else if (keyword == RID_ATOMIC_CANCEL)
40508 {
40509 /* cancel-and-throw is unimplemented. */
40510 sorry ("atomic_cancel");
40511 noex = NULL_TREE;
40512 }
40513 else
40514 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
40515
40516 /* Keep track if we're in the lexical scope of an outer transaction. */
40517 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
40518
40519 stmt = begin_transaction_stmt (token->location, NULL, this_in);
40520
40521 parser->in_transaction = new_in;
40522 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
40523 parser->in_transaction = old_in;
40524
40525 finish_transaction_stmt (stmt, NULL, this_in, noex);
40526
40527 return stmt;
40528 }
40529
40530 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40531
40532 transaction-expression:
40533 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40534 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40535 */
40536
40537 static tree
40538 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
40539 {
40540 unsigned char old_in = parser->in_transaction;
40541 unsigned char this_in = 1;
40542 cp_token *token;
40543 tree expr, noex;
40544 bool noex_expr;
40545 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40546
40547 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40548 || keyword == RID_TRANSACTION_RELAXED);
40549
40550 if (!flag_tm)
40551 error_at (loc,
40552 keyword == RID_TRANSACTION_RELAXED
40553 ? G_("%<__transaction_relaxed%> without transactional memory "
40554 "support enabled")
40555 : G_("%<__transaction_atomic%> without transactional memory "
40556 "support enabled"));
40557
40558 token = cp_parser_require_keyword (parser, keyword,
40559 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40560 : RT_TRANSACTION_RELAXED));
40561 gcc_assert (token != NULL);
40562
40563 if (keyword == RID_TRANSACTION_RELAXED)
40564 this_in |= TM_STMT_ATTR_RELAXED;
40565
40566 /* Set this early. This might mean that we allow transaction_cancel in
40567 an expression that we find out later actually has to be a constexpr.
40568 However, we expect that cxx_constant_value will be able to deal with
40569 this; also, if the noexcept has no constexpr, then what we parse next
40570 really is a transaction's body. */
40571 parser->in_transaction = this_in;
40572
40573 /* Parse a noexcept specification. */
40574 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
40575 true);
40576
40577 if (!noex || !noex_expr
40578 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
40579 {
40580 matching_parens parens;
40581 parens.require_open (parser);
40582
40583 expr = cp_parser_expression (parser);
40584 expr = finish_parenthesized_expr (expr);
40585
40586 parens.require_close (parser);
40587 }
40588 else
40589 {
40590 /* The only expression that is available got parsed for the noexcept
40591 already. noexcept is true then. */
40592 expr = noex;
40593 noex = boolean_true_node;
40594 }
40595
40596 expr = build_transaction_expr (token->location, expr, this_in, noex);
40597 parser->in_transaction = old_in;
40598
40599 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
40600 return error_mark_node;
40601
40602 return (flag_tm ? expr : error_mark_node);
40603 }
40604
40605 /* Parse a function-transaction-block.
40606
40607 function-transaction-block:
40608 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40609 function-body
40610 __transaction_atomic txn-attribute[opt] function-try-block
40611 __transaction_relaxed ctor-initializer[opt] function-body
40612 __transaction_relaxed function-try-block
40613 */
40614
40615 static void
40616 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
40617 {
40618 unsigned char old_in = parser->in_transaction;
40619 unsigned char new_in = 1;
40620 tree compound_stmt, stmt, attrs;
40621 cp_token *token;
40622
40623 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40624 || keyword == RID_TRANSACTION_RELAXED);
40625 token = cp_parser_require_keyword (parser, keyword,
40626 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40627 : RT_TRANSACTION_RELAXED));
40628 gcc_assert (token != NULL);
40629
40630 if (keyword == RID_TRANSACTION_RELAXED)
40631 new_in |= TM_STMT_ATTR_RELAXED;
40632 else
40633 {
40634 attrs = cp_parser_txn_attribute_opt (parser);
40635 if (attrs)
40636 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40637 }
40638
40639 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
40640
40641 parser->in_transaction = new_in;
40642
40643 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
40644 cp_parser_function_try_block (parser);
40645 else
40646 cp_parser_ctor_initializer_opt_and_function_body
40647 (parser, /*in_function_try_block=*/false);
40648
40649 parser->in_transaction = old_in;
40650
40651 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
40652 }
40653
40654 /* Parse a __transaction_cancel statement.
40655
40656 cancel-statement:
40657 __transaction_cancel txn-attribute[opt] ;
40658 __transaction_cancel txn-attribute[opt] throw-expression ;
40659
40660 ??? Cancel and throw is not yet implemented. */
40661
40662 static tree
40663 cp_parser_transaction_cancel (cp_parser *parser)
40664 {
40665 cp_token *token;
40666 bool is_outer = false;
40667 tree stmt, attrs;
40668
40669 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
40670 RT_TRANSACTION_CANCEL);
40671 gcc_assert (token != NULL);
40672
40673 attrs = cp_parser_txn_attribute_opt (parser);
40674 if (attrs)
40675 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
40676
40677 /* ??? Parse cancel-and-throw here. */
40678
40679 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40680
40681 if (!flag_tm)
40682 {
40683 error_at (token->location, "%<__transaction_cancel%> without "
40684 "transactional memory support enabled");
40685 return error_mark_node;
40686 }
40687 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
40688 {
40689 error_at (token->location, "%<__transaction_cancel%> within a "
40690 "%<__transaction_relaxed%>");
40691 return error_mark_node;
40692 }
40693 else if (is_outer)
40694 {
40695 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
40696 && !is_tm_may_cancel_outer (current_function_decl))
40697 {
40698 error_at (token->location, "outer %<__transaction_cancel%> not "
40699 "within outer %<__transaction_atomic%>");
40700 error_at (token->location,
40701 " or a %<transaction_may_cancel_outer%> function");
40702 return error_mark_node;
40703 }
40704 }
40705 else if (parser->in_transaction == 0)
40706 {
40707 error_at (token->location, "%<__transaction_cancel%> not within "
40708 "%<__transaction_atomic%>");
40709 return error_mark_node;
40710 }
40711
40712 stmt = build_tm_abort_call (token->location, is_outer);
40713 add_stmt (stmt);
40714
40715 return stmt;
40716 }
40717 \f
40718 /* The parser. */
40719
40720 static GTY (()) cp_parser *the_parser;
40721
40722 \f
40723 /* Special handling for the first token or line in the file. The first
40724 thing in the file might be #pragma GCC pch_preprocess, which loads a
40725 PCH file, which is a GC collection point. So we need to handle this
40726 first pragma without benefit of an existing lexer structure.
40727
40728 Always returns one token to the caller in *FIRST_TOKEN. This is
40729 either the true first token of the file, or the first token after
40730 the initial pragma. */
40731
40732 static void
40733 cp_parser_initial_pragma (cp_token *first_token)
40734 {
40735 tree name = NULL;
40736
40737 cp_lexer_get_preprocessor_token (NULL, first_token);
40738 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
40739 return;
40740
40741 cp_lexer_get_preprocessor_token (NULL, first_token);
40742 if (first_token->type == CPP_STRING)
40743 {
40744 name = first_token->u.value;
40745
40746 cp_lexer_get_preprocessor_token (NULL, first_token);
40747 if (first_token->type != CPP_PRAGMA_EOL)
40748 error_at (first_token->location,
40749 "junk at end of %<#pragma GCC pch_preprocess%>");
40750 }
40751 else
40752 error_at (first_token->location, "expected string literal");
40753
40754 /* Skip to the end of the pragma. */
40755 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
40756 cp_lexer_get_preprocessor_token (NULL, first_token);
40757
40758 /* Now actually load the PCH file. */
40759 if (name)
40760 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
40761
40762 /* Read one more token to return to our caller. We have to do this
40763 after reading the PCH file in, since its pointers have to be
40764 live. */
40765 cp_lexer_get_preprocessor_token (NULL, first_token);
40766 }
40767
40768 /* Parse a pragma GCC ivdep. */
40769
40770 static bool
40771 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
40772 {
40773 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40774 return true;
40775 }
40776
40777 /* Parse a pragma GCC unroll. */
40778
40779 static unsigned short
40780 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
40781 {
40782 location_t location = cp_lexer_peek_token (parser->lexer)->location;
40783 tree expr = cp_parser_constant_expression (parser);
40784 unsigned short unroll;
40785 expr = maybe_constant_value (expr);
40786 HOST_WIDE_INT lunroll = 0;
40787 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
40788 || TREE_CODE (expr) != INTEGER_CST
40789 || (lunroll = tree_to_shwi (expr)) < 0
40790 || lunroll >= USHRT_MAX)
40791 {
40792 error_at (location, "%<#pragma GCC unroll%> requires an"
40793 " assignment-expression that evaluates to a non-negative"
40794 " integral constant less than %u", USHRT_MAX);
40795 unroll = 0;
40796 }
40797 else
40798 {
40799 unroll = (unsigned short)lunroll;
40800 if (unroll == 0)
40801 unroll = 1;
40802 }
40803 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40804 return unroll;
40805 }
40806
40807 /* Normal parsing of a pragma token. Here we can (and must) use the
40808 regular lexer. */
40809
40810 static bool
40811 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
40812 {
40813 cp_token *pragma_tok;
40814 unsigned int id;
40815 tree stmt;
40816 bool ret;
40817
40818 pragma_tok = cp_lexer_consume_token (parser->lexer);
40819 gcc_assert (pragma_tok->type == CPP_PRAGMA);
40820 parser->lexer->in_pragma = true;
40821
40822 id = cp_parser_pragma_kind (pragma_tok);
40823 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
40824 cp_ensure_no_omp_declare_simd (parser);
40825 switch (id)
40826 {
40827 case PRAGMA_GCC_PCH_PREPROCESS:
40828 error_at (pragma_tok->location,
40829 "%<#pragma GCC pch_preprocess%> must be first");
40830 break;
40831
40832 case PRAGMA_OMP_BARRIER:
40833 switch (context)
40834 {
40835 case pragma_compound:
40836 cp_parser_omp_barrier (parser, pragma_tok);
40837 return false;
40838 case pragma_stmt:
40839 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40840 "used in compound statements", "omp barrier");
40841 break;
40842 default:
40843 goto bad_stmt;
40844 }
40845 break;
40846
40847 case PRAGMA_OMP_DEPOBJ:
40848 switch (context)
40849 {
40850 case pragma_compound:
40851 cp_parser_omp_depobj (parser, pragma_tok);
40852 return false;
40853 case pragma_stmt:
40854 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40855 "used in compound statements", "omp depobj");
40856 break;
40857 default:
40858 goto bad_stmt;
40859 }
40860 break;
40861
40862 case PRAGMA_OMP_FLUSH:
40863 switch (context)
40864 {
40865 case pragma_compound:
40866 cp_parser_omp_flush (parser, pragma_tok);
40867 return false;
40868 case pragma_stmt:
40869 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40870 "used in compound statements", "omp flush");
40871 break;
40872 default:
40873 goto bad_stmt;
40874 }
40875 break;
40876
40877 case PRAGMA_OMP_TASKWAIT:
40878 switch (context)
40879 {
40880 case pragma_compound:
40881 cp_parser_omp_taskwait (parser, pragma_tok);
40882 return false;
40883 case pragma_stmt:
40884 error_at (pragma_tok->location,
40885 "%<#pragma %s%> may only be used in compound statements",
40886 "omp taskwait");
40887 break;
40888 default:
40889 goto bad_stmt;
40890 }
40891 break;
40892
40893 case PRAGMA_OMP_TASKYIELD:
40894 switch (context)
40895 {
40896 case pragma_compound:
40897 cp_parser_omp_taskyield (parser, pragma_tok);
40898 return false;
40899 case pragma_stmt:
40900 error_at (pragma_tok->location,
40901 "%<#pragma %s%> may only be used in compound statements",
40902 "omp taskyield");
40903 break;
40904 default:
40905 goto bad_stmt;
40906 }
40907 break;
40908
40909 case PRAGMA_OMP_CANCEL:
40910 switch (context)
40911 {
40912 case pragma_compound:
40913 cp_parser_omp_cancel (parser, pragma_tok);
40914 return false;
40915 case pragma_stmt:
40916 error_at (pragma_tok->location,
40917 "%<#pragma %s%> may only be used in compound statements",
40918 "omp cancel");
40919 break;
40920 default:
40921 goto bad_stmt;
40922 }
40923 break;
40924
40925 case PRAGMA_OMP_CANCELLATION_POINT:
40926 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
40927 return false;
40928
40929 case PRAGMA_OMP_THREADPRIVATE:
40930 cp_parser_omp_threadprivate (parser, pragma_tok);
40931 return false;
40932
40933 case PRAGMA_OMP_DECLARE:
40934 return cp_parser_omp_declare (parser, pragma_tok, context);
40935
40936 case PRAGMA_OACC_DECLARE:
40937 cp_parser_oacc_declare (parser, pragma_tok);
40938 return false;
40939
40940 case PRAGMA_OACC_ENTER_DATA:
40941 if (context == pragma_stmt)
40942 {
40943 error_at (pragma_tok->location,
40944 "%<#pragma %s%> may only be used in compound statements",
40945 "acc enter data");
40946 break;
40947 }
40948 else if (context != pragma_compound)
40949 goto bad_stmt;
40950 cp_parser_omp_construct (parser, pragma_tok, if_p);
40951 return true;
40952
40953 case PRAGMA_OACC_EXIT_DATA:
40954 if (context == pragma_stmt)
40955 {
40956 error_at (pragma_tok->location,
40957 "%<#pragma %s%> may only be used in compound statements",
40958 "acc exit data");
40959 break;
40960 }
40961 else if (context != pragma_compound)
40962 goto bad_stmt;
40963 cp_parser_omp_construct (parser, pragma_tok, if_p);
40964 return true;
40965
40966 case PRAGMA_OACC_ROUTINE:
40967 if (context != pragma_external)
40968 {
40969 error_at (pragma_tok->location,
40970 "%<#pragma acc routine%> must be at file scope");
40971 break;
40972 }
40973 cp_parser_oacc_routine (parser, pragma_tok, context);
40974 return false;
40975
40976 case PRAGMA_OACC_UPDATE:
40977 if (context == pragma_stmt)
40978 {
40979 error_at (pragma_tok->location,
40980 "%<#pragma %s%> may only be used in compound statements",
40981 "acc update");
40982 break;
40983 }
40984 else if (context != pragma_compound)
40985 goto bad_stmt;
40986 cp_parser_omp_construct (parser, pragma_tok, if_p);
40987 return true;
40988
40989 case PRAGMA_OACC_WAIT:
40990 if (context == pragma_stmt)
40991 {
40992 error_at (pragma_tok->location,
40993 "%<#pragma %s%> may only be used in compound statements",
40994 "acc wait");
40995 break;
40996 }
40997 else if (context != pragma_compound)
40998 goto bad_stmt;
40999 cp_parser_omp_construct (parser, pragma_tok, if_p);
41000 return true;
41001
41002 case PRAGMA_OACC_ATOMIC:
41003 case PRAGMA_OACC_CACHE:
41004 case PRAGMA_OACC_DATA:
41005 case PRAGMA_OACC_HOST_DATA:
41006 case PRAGMA_OACC_KERNELS:
41007 case PRAGMA_OACC_PARALLEL:
41008 case PRAGMA_OACC_LOOP:
41009 case PRAGMA_OMP_ATOMIC:
41010 case PRAGMA_OMP_CRITICAL:
41011 case PRAGMA_OMP_DISTRIBUTE:
41012 case PRAGMA_OMP_FOR:
41013 case PRAGMA_OMP_MASTER:
41014 case PRAGMA_OMP_PARALLEL:
41015 case PRAGMA_OMP_SECTIONS:
41016 case PRAGMA_OMP_SIMD:
41017 case PRAGMA_OMP_SINGLE:
41018 case PRAGMA_OMP_TASK:
41019 case PRAGMA_OMP_TASKGROUP:
41020 case PRAGMA_OMP_TASKLOOP:
41021 case PRAGMA_OMP_TEAMS:
41022 if (context != pragma_stmt && context != pragma_compound)
41023 goto bad_stmt;
41024 stmt = push_omp_privatization_clauses (false);
41025 cp_parser_omp_construct (parser, pragma_tok, if_p);
41026 pop_omp_privatization_clauses (stmt);
41027 return true;
41028
41029 case PRAGMA_OMP_REQUIRES:
41030 return cp_parser_omp_requires (parser, pragma_tok);
41031
41032 case PRAGMA_OMP_ORDERED:
41033 if (context != pragma_stmt && context != pragma_compound)
41034 goto bad_stmt;
41035 stmt = push_omp_privatization_clauses (false);
41036 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
41037 pop_omp_privatization_clauses (stmt);
41038 return ret;
41039
41040 case PRAGMA_OMP_TARGET:
41041 if (context != pragma_stmt && context != pragma_compound)
41042 goto bad_stmt;
41043 stmt = push_omp_privatization_clauses (false);
41044 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
41045 pop_omp_privatization_clauses (stmt);
41046 return ret;
41047
41048 case PRAGMA_OMP_END_DECLARE_TARGET:
41049 cp_parser_omp_end_declare_target (parser, pragma_tok);
41050 return false;
41051
41052 case PRAGMA_OMP_SECTION:
41053 error_at (pragma_tok->location,
41054 "%<#pragma omp section%> may only be used in "
41055 "%<#pragma omp sections%> construct");
41056 break;
41057
41058 case PRAGMA_IVDEP:
41059 {
41060 if (context == pragma_external)
41061 {
41062 error_at (pragma_tok->location,
41063 "%<#pragma GCC ivdep%> must be inside a function");
41064 break;
41065 }
41066 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
41067 unsigned short unroll;
41068 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41069 if (tok->type == CPP_PRAGMA
41070 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
41071 {
41072 tok = cp_lexer_consume_token (parser->lexer);
41073 unroll = cp_parser_pragma_unroll (parser, tok);
41074 tok = cp_lexer_peek_token (the_parser->lexer);
41075 }
41076 else
41077 unroll = 0;
41078 if (tok->type != CPP_KEYWORD
41079 || (tok->keyword != RID_FOR
41080 && tok->keyword != RID_WHILE
41081 && tok->keyword != RID_DO))
41082 {
41083 cp_parser_error (parser, "for, while or do statement expected");
41084 return false;
41085 }
41086 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
41087 return true;
41088 }
41089
41090 case PRAGMA_UNROLL:
41091 {
41092 if (context == pragma_external)
41093 {
41094 error_at (pragma_tok->location,
41095 "%<#pragma GCC unroll%> must be inside a function");
41096 break;
41097 }
41098 const unsigned short unroll
41099 = cp_parser_pragma_unroll (parser, pragma_tok);
41100 bool ivdep;
41101 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41102 if (tok->type == CPP_PRAGMA
41103 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
41104 {
41105 tok = cp_lexer_consume_token (parser->lexer);
41106 ivdep = cp_parser_pragma_ivdep (parser, tok);
41107 tok = cp_lexer_peek_token (the_parser->lexer);
41108 }
41109 else
41110 ivdep = false;
41111 if (tok->type != CPP_KEYWORD
41112 || (tok->keyword != RID_FOR
41113 && tok->keyword != RID_WHILE
41114 && tok->keyword != RID_DO))
41115 {
41116 cp_parser_error (parser, "for, while or do statement expected");
41117 return false;
41118 }
41119 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
41120 return true;
41121 }
41122
41123 default:
41124 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
41125 c_invoke_pragma_handler (id);
41126 break;
41127
41128 bad_stmt:
41129 cp_parser_error (parser, "expected declaration specifiers");
41130 break;
41131 }
41132
41133 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41134 return false;
41135 }
41136
41137 /* The interface the pragma parsers have to the lexer. */
41138
41139 enum cpp_ttype
41140 pragma_lex (tree *value, location_t *loc)
41141 {
41142 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41143 enum cpp_ttype ret = tok->type;
41144
41145 *value = tok->u.value;
41146 if (loc)
41147 *loc = tok->location;
41148
41149 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
41150 ret = CPP_EOF;
41151 else if (ret == CPP_STRING)
41152 *value = cp_parser_string_literal (the_parser, false, false);
41153 else
41154 {
41155 if (ret == CPP_KEYWORD)
41156 ret = CPP_NAME;
41157 cp_lexer_consume_token (the_parser->lexer);
41158 }
41159
41160 return ret;
41161 }
41162
41163 \f
41164 /* External interface. */
41165
41166 /* Parse one entire translation unit. */
41167
41168 void
41169 c_parse_file (void)
41170 {
41171 static bool already_called = false;
41172
41173 if (already_called)
41174 fatal_error (input_location,
41175 "inter-module optimizations not implemented for C++");
41176 already_called = true;
41177
41178 the_parser = cp_parser_new ();
41179 push_deferring_access_checks (flag_access_control
41180 ? dk_no_deferred : dk_no_check);
41181 cp_parser_translation_unit (the_parser);
41182 the_parser = NULL;
41183
41184 finish_translation_unit ();
41185 }
41186
41187 /* Create an identifier for a generic parameter type (a synthesized
41188 template parameter implied by `auto' or a concept identifier). */
41189
41190 static GTY(()) int generic_parm_count;
41191 static tree
41192 make_generic_type_name ()
41193 {
41194 char buf[32];
41195 sprintf (buf, "auto:%d", ++generic_parm_count);
41196 return get_identifier (buf);
41197 }
41198
41199 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
41200 (creating a new template parameter list if necessary). Returns the newly
41201 created template type parm. */
41202
41203 static tree
41204 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
41205 {
41206 gcc_assert (current_binding_level->kind == sk_function_parms);
41207
41208 /* Before committing to modifying any scope, if we're in an
41209 implicit template scope, and we're trying to synthesize a
41210 constrained parameter, try to find a previous parameter with
41211 the same name. This is the same-type rule for abbreviated
41212 function templates.
41213
41214 NOTE: We can generate implicit parameters when tentatively
41215 parsing a nested name specifier, only to reject that parse
41216 later. However, matching the same template-id as part of a
41217 direct-declarator should generate an identical template
41218 parameter, so this rule will merge them. */
41219 if (parser->implicit_template_scope && constr)
41220 {
41221 tree t = parser->implicit_template_parms;
41222 while (t)
41223 {
41224 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
41225 {
41226 tree d = TREE_VALUE (t);
41227 if (TREE_CODE (d) == PARM_DECL)
41228 /* Return the TEMPLATE_PARM_INDEX. */
41229 d = DECL_INITIAL (d);
41230 return d;
41231 }
41232 t = TREE_CHAIN (t);
41233 }
41234 }
41235
41236 /* We are either continuing a function template that already contains implicit
41237 template parameters, creating a new fully-implicit function template, or
41238 extending an existing explicit function template with implicit template
41239 parameters. */
41240
41241 cp_binding_level *const entry_scope = current_binding_level;
41242
41243 bool become_template = false;
41244 cp_binding_level *parent_scope = 0;
41245
41246 if (parser->implicit_template_scope)
41247 {
41248 gcc_assert (parser->implicit_template_parms);
41249
41250 current_binding_level = parser->implicit_template_scope;
41251 }
41252 else
41253 {
41254 /* Roll back to the existing template parameter scope (in the case of
41255 extending an explicit function template) or introduce a new template
41256 parameter scope ahead of the function parameter scope (or class scope
41257 in the case of out-of-line member definitions). The function scope is
41258 added back after template parameter synthesis below. */
41259
41260 cp_binding_level *scope = entry_scope;
41261
41262 while (scope->kind == sk_function_parms)
41263 {
41264 parent_scope = scope;
41265 scope = scope->level_chain;
41266 }
41267 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
41268 {
41269 /* If not defining a class, then any class scope is a scope level in
41270 an out-of-line member definition. In this case simply wind back
41271 beyond the first such scope to inject the template parameter list.
41272 Otherwise wind back to the class being defined. The latter can
41273 occur in class member friend declarations such as:
41274
41275 class A {
41276 void foo (auto);
41277 };
41278 class B {
41279 friend void A::foo (auto);
41280 };
41281
41282 The template parameter list synthesized for the friend declaration
41283 must be injected in the scope of 'B'. This can also occur in
41284 erroneous cases such as:
41285
41286 struct A {
41287 struct B {
41288 void foo (auto);
41289 };
41290 void B::foo (auto) {}
41291 };
41292
41293 Here the attempted definition of 'B::foo' within 'A' is ill-formed
41294 but, nevertheless, the template parameter list synthesized for the
41295 declarator should be injected into the scope of 'A' as if the
41296 ill-formed template was specified explicitly. */
41297
41298 while (scope->kind == sk_class && !scope->defining_class_p)
41299 {
41300 parent_scope = scope;
41301 scope = scope->level_chain;
41302 }
41303 }
41304
41305 current_binding_level = scope;
41306
41307 if (scope->kind != sk_template_parms
41308 || !function_being_declared_is_template_p (parser))
41309 {
41310 /* Introduce a new template parameter list for implicit template
41311 parameters. */
41312
41313 become_template = true;
41314
41315 parser->implicit_template_scope
41316 = begin_scope (sk_template_parms, NULL);
41317
41318 ++processing_template_decl;
41319
41320 parser->fully_implicit_function_template_p = true;
41321 ++parser->num_template_parameter_lists;
41322 }
41323 else
41324 {
41325 /* Synthesize implicit template parameters at the end of the explicit
41326 template parameter list. */
41327
41328 gcc_assert (current_template_parms);
41329
41330 parser->implicit_template_scope = scope;
41331
41332 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41333 parser->implicit_template_parms
41334 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
41335 }
41336 }
41337
41338 /* Synthesize a new template parameter and track the current template
41339 parameter chain with implicit_template_parms. */
41340
41341 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
41342 tree synth_id = make_generic_type_name ();
41343 tree synth_tmpl_parm;
41344 bool non_type = false;
41345
41346 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
41347 synth_tmpl_parm
41348 = finish_template_type_parm (class_type_node, synth_id);
41349 else if (TREE_CODE (proto) == TEMPLATE_DECL)
41350 synth_tmpl_parm
41351 = finish_constrained_template_template_parm (proto, synth_id);
41352 else
41353 {
41354 synth_tmpl_parm = copy_decl (proto);
41355 DECL_NAME (synth_tmpl_parm) = synth_id;
41356 non_type = true;
41357 }
41358
41359 // Attach the constraint to the parm before processing.
41360 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
41361 TREE_TYPE (node) = constr;
41362 tree new_parm
41363 = process_template_parm (parser->implicit_template_parms,
41364 input_location,
41365 node,
41366 /*non_type=*/non_type,
41367 /*param_pack=*/false);
41368
41369 // Chain the new parameter to the list of implicit parameters.
41370 if (parser->implicit_template_parms)
41371 parser->implicit_template_parms
41372 = TREE_CHAIN (parser->implicit_template_parms);
41373 else
41374 parser->implicit_template_parms = new_parm;
41375
41376 tree new_decl = get_local_decls ();
41377 if (non_type)
41378 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
41379 new_decl = DECL_INITIAL (new_decl);
41380
41381 /* If creating a fully implicit function template, start the new implicit
41382 template parameter list with this synthesized type, otherwise grow the
41383 current template parameter list. */
41384
41385 if (become_template)
41386 {
41387 parent_scope->level_chain = current_binding_level;
41388
41389 tree new_parms = make_tree_vec (1);
41390 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
41391 current_template_parms = tree_cons (size_int (processing_template_decl),
41392 new_parms, current_template_parms);
41393 }
41394 else
41395 {
41396 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41397 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
41398 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
41399 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
41400 }
41401
41402 // If the new parameter was constrained, we need to add that to the
41403 // constraints in the template parameter list.
41404 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
41405 {
41406 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
41407 reqs = conjoin_constraints (reqs, req);
41408 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
41409 }
41410
41411 current_binding_level = entry_scope;
41412
41413 return new_decl;
41414 }
41415
41416 /* Finish the declaration of a fully implicit function template. Such a
41417 template has no explicit template parameter list so has not been through the
41418 normal template head and tail processing. synthesize_implicit_template_parm
41419 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
41420 provided if the declaration is a class member such that its template
41421 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
41422 form is returned. Otherwise NULL_TREE is returned. */
41423
41424 static tree
41425 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
41426 {
41427 gcc_assert (parser->fully_implicit_function_template_p);
41428
41429 if (member_decl_opt && member_decl_opt != error_mark_node
41430 && DECL_VIRTUAL_P (member_decl_opt))
41431 {
41432 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
41433 "implicit templates may not be %<virtual%>");
41434 DECL_VIRTUAL_P (member_decl_opt) = false;
41435 }
41436
41437 if (member_decl_opt)
41438 member_decl_opt = finish_member_template_decl (member_decl_opt);
41439 end_template_decl ();
41440
41441 parser->fully_implicit_function_template_p = false;
41442 parser->implicit_template_parms = 0;
41443 parser->implicit_template_scope = 0;
41444 --parser->num_template_parameter_lists;
41445
41446 return member_decl_opt;
41447 }
41448
41449 /* Like finish_fully_implicit_template, but to be used in error
41450 recovery, rearranging scopes so that we restore the state we had
41451 before synthesize_implicit_template_parm inserted the implement
41452 template parms scope. */
41453
41454 static void
41455 abort_fully_implicit_template (cp_parser *parser)
41456 {
41457 cp_binding_level *return_to_scope = current_binding_level;
41458
41459 if (parser->implicit_template_scope
41460 && return_to_scope != parser->implicit_template_scope)
41461 {
41462 cp_binding_level *child = return_to_scope;
41463 for (cp_binding_level *scope = child->level_chain;
41464 scope != parser->implicit_template_scope;
41465 scope = child->level_chain)
41466 child = scope;
41467 child->level_chain = parser->implicit_template_scope->level_chain;
41468 parser->implicit_template_scope->level_chain = return_to_scope;
41469 current_binding_level = parser->implicit_template_scope;
41470 }
41471 else
41472 return_to_scope = return_to_scope->level_chain;
41473
41474 finish_fully_implicit_template (parser, NULL);
41475
41476 gcc_assert (current_binding_level == return_to_scope);
41477 }
41478
41479 /* Helper function for diagnostics that have complained about things
41480 being used with 'extern "C"' linkage.
41481
41482 Attempt to issue a note showing where the 'extern "C"' linkage began. */
41483
41484 void
41485 maybe_show_extern_c_location (void)
41486 {
41487 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
41488 inform (the_parser->innermost_linkage_specification_location,
41489 "%<extern \"C\"%> linkage started here");
41490 }
41491
41492 #include "gt-cp-parser.h"