]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/parser.c
c++: Correct the handling of alignof(expr) [PR88115]
[gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2020 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 #include "c-family/known-headers.h"
49
50 \f
51 /* The lexer. */
52
53 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
54 and c-lex.c) and the C++ parser. */
55
56 /* The various kinds of non integral constant we encounter. */
57 enum non_integral_constant {
58 NIC_NONE,
59 /* floating-point literal */
60 NIC_FLOAT,
61 /* %<this%> */
62 NIC_THIS,
63 /* %<__FUNCTION__%> */
64 NIC_FUNC_NAME,
65 /* %<__PRETTY_FUNCTION__%> */
66 NIC_PRETTY_FUNC,
67 /* %<__func__%> */
68 NIC_C99_FUNC,
69 /* "%<va_arg%> */
70 NIC_VA_ARG,
71 /* a cast */
72 NIC_CAST,
73 /* %<typeid%> operator */
74 NIC_TYPEID,
75 /* non-constant compound literals */
76 NIC_NCC,
77 /* a function call */
78 NIC_FUNC_CALL,
79 /* an increment */
80 NIC_INC,
81 /* an decrement */
82 NIC_DEC,
83 /* an array reference */
84 NIC_ARRAY_REF,
85 /* %<->%> */
86 NIC_ARROW,
87 /* %<.%> */
88 NIC_POINT,
89 /* the address of a label */
90 NIC_ADDR_LABEL,
91 /* %<*%> */
92 NIC_STAR,
93 /* %<&%> */
94 NIC_ADDR,
95 /* %<++%> */
96 NIC_PREINCREMENT,
97 /* %<--%> */
98 NIC_PREDECREMENT,
99 /* %<new%> */
100 NIC_NEW,
101 /* %<delete%> */
102 NIC_DEL,
103 /* calls to overloaded operators */
104 NIC_OVERLOADED,
105 /* an assignment */
106 NIC_ASSIGNMENT,
107 /* a comma operator */
108 NIC_COMMA,
109 /* a call to a constructor */
110 NIC_CONSTRUCTOR,
111 /* a transaction expression */
112 NIC_TRANSACTION
113 };
114
115 /* The various kinds of errors about name-lookup failing. */
116 enum name_lookup_error {
117 /* NULL */
118 NLE_NULL,
119 /* is not a type */
120 NLE_TYPE,
121 /* is not a class or namespace */
122 NLE_CXX98,
123 /* is not a class, namespace, or enumeration */
124 NLE_NOT_CXX98
125 };
126
127 /* The various kinds of required token */
128 enum required_token {
129 RT_NONE,
130 RT_SEMICOLON, /* ';' */
131 RT_OPEN_PAREN, /* '(' */
132 RT_CLOSE_BRACE, /* '}' */
133 RT_OPEN_BRACE, /* '{' */
134 RT_CLOSE_SQUARE, /* ']' */
135 RT_OPEN_SQUARE, /* '[' */
136 RT_COMMA, /* ',' */
137 RT_SCOPE, /* '::' */
138 RT_LESS, /* '<' */
139 RT_GREATER, /* '>' */
140 RT_EQ, /* '=' */
141 RT_ELLIPSIS, /* '...' */
142 RT_MULT, /* '*' */
143 RT_COMPL, /* '~' */
144 RT_COLON, /* ':' */
145 RT_COLON_SCOPE, /* ':' or '::' */
146 RT_CLOSE_PAREN, /* ')' */
147 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
148 RT_PRAGMA_EOL, /* end of line */
149 RT_NAME, /* identifier */
150
151 /* The type is CPP_KEYWORD */
152 RT_NEW, /* new */
153 RT_DELETE, /* delete */
154 RT_RETURN, /* return */
155 RT_WHILE, /* while */
156 RT_EXTERN, /* extern */
157 RT_STATIC_ASSERT, /* static_assert */
158 RT_DECLTYPE, /* decltype */
159 RT_OPERATOR, /* operator */
160 RT_CLASS, /* class */
161 RT_TEMPLATE, /* template */
162 RT_NAMESPACE, /* namespace */
163 RT_USING, /* using */
164 RT_ASM, /* asm */
165 RT_TRY, /* try */
166 RT_CATCH, /* catch */
167 RT_THROW, /* throw */
168 RT_AUTO, /* auto */
169 RT_LABEL, /* __label__ */
170 RT_AT_TRY, /* @try */
171 RT_AT_SYNCHRONIZED, /* @synchronized */
172 RT_AT_THROW, /* @throw */
173
174 RT_SELECT, /* selection-statement */
175 RT_ITERATION, /* iteration-statement */
176 RT_JUMP, /* jump-statement */
177 RT_CLASS_KEY, /* class-key */
178 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
179 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
180 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
181 RT_TRANSACTION_CANCEL, /* __transaction_cancel */
182
183 RT_CO_YIELD /* co_yield */
184 };
185
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
188
189 class type_id_in_expr_sentinel
190 {
191 cp_parser *parser;
192 bool saved;
193 public:
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195 : parser (parser),
196 saved (parser->in_type_id_in_expr_p)
197 { parser->in_type_id_in_expr_p = set; }
198 ~type_id_in_expr_sentinel ()
199 { parser->in_type_id_in_expr_p = saved; }
200 };
201
202 /* Prototypes. */
203
204 static cp_lexer *cp_lexer_new_main
205 (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209 (cp_lexer *);
210 static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215 (unsigned, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217 (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219 (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227 (cp_lexer *);
228 static void cp_lexer_purge_token
229 (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233 (cp_lexer *);
234 static void cp_lexer_commit_tokens
235 (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237 (cp_lexer *);
238 static void cp_lexer_print_token
239 (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241 (cp_lexer *);
242 static void cp_lexer_start_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
246
247 static cp_token_cache *cp_token_cache_new
248 (cp_token *, cp_token *);
249 static tree cp_parser_late_noexcept_specifier
250 (cp_parser *, tree);
251 static void noexcept_override_late_checks
252 (tree, tree);
253
254 static void cp_parser_initial_pragma
255 (cp_token *);
256
257 static bool cp_parser_omp_declare_reduction_exprs
258 (tree, cp_parser *);
259 static void cp_finalize_oacc_routine
260 (cp_parser *, tree, bool);
261
262 /* Manifest constants. */
263 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
264 #define CP_SAVED_TOKEN_STACK 5
265
266 /* Variables. */
267
268 /* The stream to which debugging output should be written. */
269 static FILE *cp_lexer_debug_stream;
270
271 /* Nonzero if we are parsing an unevaluated operand: an operand to
272 sizeof, typeof, or alignof. */
273 int cp_unevaluated_operand;
274
275 /* Dump up to NUM tokens in BUFFER to FILE starting with token
276 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
277 first token in BUFFER. If NUM is 0, dump all the tokens. If
278 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
279 highlighted by surrounding it in [[ ]]. */
280
281 static void
282 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
283 cp_token *start_token, unsigned num,
284 cp_token *curr_token)
285 {
286 unsigned i, nprinted;
287 cp_token *token;
288 bool do_print;
289
290 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
291
292 if (buffer == NULL)
293 return;
294
295 if (num == 0)
296 num = buffer->length ();
297
298 if (start_token == NULL)
299 start_token = buffer->address ();
300
301 if (start_token > buffer->address ())
302 {
303 cp_lexer_print_token (file, &(*buffer)[0]);
304 fprintf (file, " ... ");
305 }
306
307 do_print = false;
308 nprinted = 0;
309 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
310 {
311 if (token == start_token)
312 do_print = true;
313
314 if (!do_print)
315 continue;
316
317 nprinted++;
318 if (token == curr_token)
319 fprintf (file, "[[");
320
321 cp_lexer_print_token (file, token);
322
323 if (token == curr_token)
324 fprintf (file, "]]");
325
326 switch (token->type)
327 {
328 case CPP_SEMICOLON:
329 case CPP_OPEN_BRACE:
330 case CPP_CLOSE_BRACE:
331 case CPP_EOF:
332 fputc ('\n', file);
333 break;
334
335 default:
336 fputc (' ', file);
337 }
338 }
339
340 if (i == num && i < buffer->length ())
341 {
342 fprintf (file, " ... ");
343 cp_lexer_print_token (file, &buffer->last ());
344 }
345
346 fprintf (file, "\n");
347 }
348
349
350 /* Dump all tokens in BUFFER to stderr. */
351
352 void
353 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
354 {
355 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
356 }
357
358 DEBUG_FUNCTION void
359 debug (vec<cp_token, va_gc> &ref)
360 {
361 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
362 }
363
364 DEBUG_FUNCTION void
365 debug (vec<cp_token, va_gc> *ptr)
366 {
367 if (ptr)
368 debug (*ptr);
369 else
370 fprintf (stderr, "<nil>\n");
371 }
372
373
374 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
375 description for T. */
376
377 static void
378 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
379 {
380 if (t)
381 {
382 fprintf (file, "%s: ", desc);
383 print_node_brief (file, "", t, 0);
384 }
385 }
386
387
388 /* Dump parser context C to FILE. */
389
390 static void
391 cp_debug_print_context (FILE *file, cp_parser_context *c)
392 {
393 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
394 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
395 print_node_brief (file, "", c->object_type, 0);
396 fprintf (file, "}\n");
397 }
398
399
400 /* Print the stack of parsing contexts to FILE starting with FIRST. */
401
402 static void
403 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
404 {
405 unsigned i;
406 cp_parser_context *c;
407
408 fprintf (file, "Parsing context stack:\n");
409 for (i = 0, c = first; c; c = c->next, i++)
410 {
411 fprintf (file, "\t#%u: ", i);
412 cp_debug_print_context (file, c);
413 }
414 }
415
416
417 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
418
419 static void
420 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
421 {
422 if (flag)
423 fprintf (file, "%s: true\n", desc);
424 }
425
426
427 /* Print an unparsed function entry UF to FILE. */
428
429 static void
430 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
431 {
432 unsigned i;
433 cp_default_arg_entry *default_arg_fn;
434 tree fn;
435
436 fprintf (file, "\tFunctions with default args:\n");
437 for (i = 0;
438 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
439 i++)
440 {
441 fprintf (file, "\t\tClass type: ");
442 print_node_brief (file, "", default_arg_fn->class_type, 0);
443 fprintf (file, "\t\tDeclaration: ");
444 print_node_brief (file, "", default_arg_fn->decl, 0);
445 fprintf (file, "\n");
446 }
447
448 fprintf (file, "\n\tFunctions with definitions that require "
449 "post-processing\n\t\t");
450 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
451 {
452 print_node_brief (file, "", fn, 0);
453 fprintf (file, " ");
454 }
455 fprintf (file, "\n");
456
457 fprintf (file, "\n\tNon-static data members with initializers that require "
458 "post-processing\n\t\t");
459 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
460 {
461 print_node_brief (file, "", fn, 0);
462 fprintf (file, " ");
463 }
464 fprintf (file, "\n");
465 }
466
467
468 /* Print the stack of unparsed member functions S to FILE. */
469
470 static void
471 cp_debug_print_unparsed_queues (FILE *file,
472 vec<cp_unparsed_functions_entry, va_gc> *s)
473 {
474 unsigned i;
475 cp_unparsed_functions_entry *uf;
476
477 fprintf (file, "Unparsed functions\n");
478 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
479 {
480 fprintf (file, "#%u:\n", i);
481 cp_debug_print_unparsed_function (file, uf);
482 }
483 }
484
485
486 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
487 the given PARSER. If FILE is NULL, the output is printed on stderr. */
488
489 static void
490 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
491 {
492 cp_token *next_token, *first_token, *start_token;
493
494 if (file == NULL)
495 file = stderr;
496
497 next_token = parser->lexer->next_token;
498 first_token = parser->lexer->buffer->address ();
499 start_token = (next_token > first_token + window_size / 2)
500 ? next_token - window_size / 2
501 : first_token;
502 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
503 next_token);
504 }
505
506
507 /* Dump debugging information for the given PARSER. If FILE is NULL,
508 the output is printed on stderr. */
509
510 void
511 cp_debug_parser (FILE *file, cp_parser *parser)
512 {
513 const size_t window_size = 20;
514 cp_token *token;
515 expanded_location eloc;
516
517 if (file == NULL)
518 file = stderr;
519
520 fprintf (file, "Parser state\n\n");
521 fprintf (file, "Number of tokens: %u\n",
522 vec_safe_length (parser->lexer->buffer));
523 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
524 cp_debug_print_tree_if_set (file, "Object scope",
525 parser->object_scope);
526 cp_debug_print_tree_if_set (file, "Qualifying scope",
527 parser->qualifying_scope);
528 cp_debug_print_context_stack (file, parser->context);
529 cp_debug_print_flag (file, "Allow GNU extensions",
530 parser->allow_gnu_extensions_p);
531 cp_debug_print_flag (file, "'>' token is greater-than",
532 parser->greater_than_is_operator_p);
533 cp_debug_print_flag (file, "Default args allowed in current "
534 "parameter list", parser->default_arg_ok_p);
535 cp_debug_print_flag (file, "Parsing integral constant-expression",
536 parser->integral_constant_expression_p);
537 cp_debug_print_flag (file, "Allow non-constant expression in current "
538 "constant-expression",
539 parser->allow_non_integral_constant_expression_p);
540 cp_debug_print_flag (file, "Seen non-constant expression",
541 parser->non_integral_constant_expression_p);
542 cp_debug_print_flag (file, "Local names forbidden in current context",
543 (parser->local_variables_forbidden_p
544 & LOCAL_VARS_FORBIDDEN));
545 cp_debug_print_flag (file, "'this' forbidden in current context",
546 (parser->local_variables_forbidden_p
547 & THIS_FORBIDDEN));
548 cp_debug_print_flag (file, "In unbraced linkage specification",
549 parser->in_unbraced_linkage_specification_p);
550 cp_debug_print_flag (file, "Parsing a declarator",
551 parser->in_declarator_p);
552 cp_debug_print_flag (file, "In template argument list",
553 parser->in_template_argument_list_p);
554 cp_debug_print_flag (file, "Parsing an iteration statement",
555 parser->in_statement & IN_ITERATION_STMT);
556 cp_debug_print_flag (file, "Parsing a switch statement",
557 parser->in_statement & IN_SWITCH_STMT);
558 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
559 parser->in_statement & IN_OMP_BLOCK);
560 cp_debug_print_flag (file, "Parsing an OpenMP loop",
561 parser->in_statement & IN_OMP_FOR);
562 cp_debug_print_flag (file, "Parsing an if statement",
563 parser->in_statement & IN_IF_STMT);
564 cp_debug_print_flag (file, "Parsing a type-id in an expression "
565 "context", parser->in_type_id_in_expr_p);
566 cp_debug_print_flag (file, "String expressions should be translated "
567 "to execution character set",
568 parser->translate_strings_p);
569 cp_debug_print_flag (file, "Parsing function body outside of a "
570 "local class", parser->in_function_body);
571 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
572 parser->colon_corrects_to_scope_p);
573 cp_debug_print_flag (file, "Colon doesn't start a class definition",
574 parser->colon_doesnt_start_class_def_p);
575 if (parser->type_definition_forbidden_message)
576 fprintf (file, "Error message for forbidden type definitions: %s %s\n",
577 parser->type_definition_forbidden_message,
578 parser->type_definition_forbidden_message_arg
579 ? parser->type_definition_forbidden_message_arg : "<none>");
580 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
581 fprintf (file, "Number of class definitions in progress: %u\n",
582 parser->num_classes_being_defined);
583 fprintf (file, "Number of template parameter lists for the current "
584 "declaration: %u\n", parser->num_template_parameter_lists);
585 cp_debug_parser_tokens (file, parser, window_size);
586 token = parser->lexer->next_token;
587 fprintf (file, "Next token to parse:\n");
588 fprintf (file, "\tToken: ");
589 cp_lexer_print_token (file, token);
590 eloc = expand_location (token->location);
591 fprintf (file, "\n\tFile: %s\n", eloc.file);
592 fprintf (file, "\tLine: %d\n", eloc.line);
593 fprintf (file, "\tColumn: %d\n", eloc.column);
594 }
595
596 DEBUG_FUNCTION void
597 debug (cp_parser &ref)
598 {
599 cp_debug_parser (stderr, &ref);
600 }
601
602 DEBUG_FUNCTION void
603 debug (cp_parser *ptr)
604 {
605 if (ptr)
606 debug (*ptr);
607 else
608 fprintf (stderr, "<nil>\n");
609 }
610
611 /* Allocate memory for a new lexer object and return it. */
612
613 static cp_lexer *
614 cp_lexer_alloc (void)
615 {
616 /* Allocate the memory. */
617 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
618
619 /* Initially we are not debugging. */
620 lexer->debugging_p = false;
621
622 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
623
624 /* Create the buffer. */
625 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
626
627 return lexer;
628 }
629
630 /* Create a new main C++ lexer, the lexer that gets tokens from the
631 preprocessor. */
632
633 static cp_lexer *
634 cp_lexer_new_main (void)
635 {
636 cp_token token;
637
638 /* It's possible that parsing the first pragma will load a PCH file,
639 which is a GC collection point. So we have to do that before
640 allocating any memory. */
641 cp_lexer_get_preprocessor_token (0, &token);
642 cp_parser_initial_pragma (&token);
643 c_common_no_more_pch ();
644
645 cp_lexer *lexer = cp_lexer_alloc ();
646 /* Put the first token in the buffer. */
647 cp_token *tok = lexer->buffer->quick_push (token);
648
649 /* Get the remaining tokens from the preprocessor. */
650 while (tok->type != CPP_EOF)
651 {
652 tok = vec_safe_push (lexer->buffer, cp_token ());
653 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
654 }
655
656 lexer->next_token = lexer->buffer->address ();
657 lexer->last_token = lexer->next_token
658 + lexer->buffer->length ()
659 - 1;
660
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
663 done_lexing = true;
664
665 gcc_assert (!lexer->next_token->purged_p);
666 return lexer;
667 }
668
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
671
672 static cp_lexer *
673 cp_lexer_new_from_tokens (cp_token_cache *cache)
674 {
675 cp_token *first = cache->first;
676 cp_token *last = cache->last;
677 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
678
679 /* We do not own the buffer. */
680 lexer->buffer = NULL;
681
682 /* Insert an EOF token. */
683 lexer->saved_type = last->type;
684 lexer->saved_keyword = last->keyword;
685 last->type = CPP_EOF;
686 last->keyword = RID_MAX;
687
688 lexer->next_token = first;
689 lexer->last_token = last;
690
691 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
692
693 /* Initially we are not debugging. */
694 lexer->debugging_p = false;
695
696 gcc_assert (!lexer->next_token->purged_p
697 && !lexer->last_token->purged_p);
698 return lexer;
699 }
700
701 /* Frees all resources associated with LEXER. */
702
703 static void
704 cp_lexer_destroy (cp_lexer *lexer)
705 {
706 if (lexer->buffer)
707 vec_free (lexer->buffer);
708 else
709 {
710 /* Restore the token we overwrite with EOF. */
711 lexer->last_token->type = lexer->saved_type;
712 lexer->last_token->keyword = lexer->saved_keyword;
713 }
714 lexer->saved_tokens.release ();
715 ggc_free (lexer);
716 }
717
718 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
719 be used. The point of this flag is to help the compiler to fold away calls
720 to cp_lexer_debugging_p within this source file at compile time, when the
721 lexer is not being debugged. */
722
723 #define LEXER_DEBUGGING_ENABLED_P false
724
725 /* Returns nonzero if debugging information should be output. */
726
727 static inline bool
728 cp_lexer_debugging_p (cp_lexer *lexer)
729 {
730 if (!LEXER_DEBUGGING_ENABLED_P)
731 return false;
732
733 return lexer->debugging_p;
734 }
735
736
737 static inline cp_token_position
738 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
739 {
740 return lexer->next_token - previous_p;
741 }
742
743 static inline cp_token *
744 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
745 {
746 return pos;
747 }
748
749 static inline void
750 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
751 {
752 lexer->next_token = cp_lexer_token_at (lexer, pos);
753 }
754
755 static inline cp_token_position
756 cp_lexer_previous_token_position (cp_lexer *lexer)
757 {
758 return cp_lexer_token_position (lexer, true);
759 }
760
761 static inline cp_token *
762 cp_lexer_previous_token (cp_lexer *lexer)
763 {
764 cp_token_position tp = cp_lexer_previous_token_position (lexer);
765
766 /* Skip past purged tokens. */
767 while (tp->purged_p)
768 {
769 gcc_assert (tp != vec_safe_address (lexer->buffer));
770 tp--;
771 }
772
773 return cp_lexer_token_at (lexer, tp);
774 }
775
776 /* Same as above, but return NULL when the lexer doesn't own the token
777 buffer or if the next_token is at the start of the token
778 vector or if all previous tokens are purged. */
779
780 static cp_token *
781 cp_lexer_safe_previous_token (cp_lexer *lexer)
782 {
783 if (lexer->buffer
784 && lexer->next_token != lexer->buffer->address ())
785 {
786 cp_token_position tp = cp_lexer_previous_token_position (lexer);
787
788 /* Skip past purged tokens. */
789 while (tp->purged_p)
790 {
791 if (tp == lexer->buffer->address ())
792 return NULL;
793 tp--;
794 }
795 return cp_lexer_token_at (lexer, tp);
796 }
797
798 return NULL;
799 }
800
801 /* Overload for make_location, taking the lexer to mean the location of the
802 previous token. */
803
804 static inline location_t
805 make_location (location_t caret, location_t start, cp_lexer *lexer)
806 {
807 cp_token *t = cp_lexer_previous_token (lexer);
808 return make_location (caret, start, t->location);
809 }
810
811 /* nonzero if we are presently saving tokens. */
812
813 static inline int
814 cp_lexer_saving_tokens (const cp_lexer* lexer)
815 {
816 return lexer->saved_tokens.length () != 0;
817 }
818
819 /* Store the next token from the preprocessor in *TOKEN. Return true
820 if we reach EOF. If LEXER is NULL, assume we are handling an
821 initial #pragma pch_preprocess, and thus want the lexer to return
822 processed strings. */
823
824 static void
825 cp_lexer_get_preprocessor_token (unsigned flags, cp_token *token)
826 {
827 static int is_extern_c = 0;
828
829 /* Get a new token from the preprocessor. */
830 token->type
831 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
832 flags);
833 token->keyword = RID_MAX;
834 token->purged_p = false;
835 token->error_reported = false;
836 token->tree_check_p = false;
837
838 /* On some systems, some header files are surrounded by an
839 implicit extern "C" block. Set a flag in the token if it
840 comes from such a header. */
841 is_extern_c += pending_lang_change;
842 pending_lang_change = 0;
843 token->implicit_extern_c = is_extern_c > 0;
844
845 /* Check to see if this token is a keyword. */
846 if (token->type == CPP_NAME)
847 {
848 if (IDENTIFIER_KEYWORD_P (token->u.value))
849 {
850 /* Mark this token as a keyword. */
851 token->type = CPP_KEYWORD;
852 /* Record which keyword. */
853 token->keyword = C_RID_CODE (token->u.value);
854 }
855 else
856 {
857 if (warn_cxx11_compat
858 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
859 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
860 {
861 /* Warn about the C++0x keyword (but still treat it as
862 an identifier). */
863 warning_at (token->location, OPT_Wc__11_compat,
864 "identifier %qE is a keyword in C++11",
865 token->u.value);
866
867 /* Clear out the C_RID_CODE so we don't warn about this
868 particular identifier-turned-keyword again. */
869 C_SET_RID_CODE (token->u.value, RID_MAX);
870 }
871 if (warn_cxx20_compat
872 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX20
873 && C_RID_CODE (token->u.value) <= RID_LAST_CXX20)
874 {
875 /* Warn about the C++20 keyword (but still treat it as
876 an identifier). */
877 warning_at (token->location, OPT_Wc__20_compat,
878 "identifier %qE is a keyword in C++20",
879 token->u.value);
880
881 /* Clear out the C_RID_CODE so we don't warn about this
882 particular identifier-turned-keyword again. */
883 C_SET_RID_CODE (token->u.value, RID_MAX);
884 }
885
886 token->keyword = RID_MAX;
887 }
888 }
889 else if (token->type == CPP_AT_NAME)
890 {
891 /* This only happens in Objective-C++; it must be a keyword. */
892 token->type = CPP_KEYWORD;
893 switch (C_RID_CODE (token->u.value))
894 {
895 /* Replace 'class' with '@class', 'private' with '@private',
896 etc. This prevents confusion with the C++ keyword
897 'class', and makes the tokens consistent with other
898 Objective-C 'AT' keywords. For example '@class' is
899 reported as RID_AT_CLASS which is consistent with
900 '@synchronized', which is reported as
901 RID_AT_SYNCHRONIZED.
902 */
903 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
904 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
905 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
906 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
907 case RID_THROW: token->keyword = RID_AT_THROW; break;
908 case RID_TRY: token->keyword = RID_AT_TRY; break;
909 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
910 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
911 default: token->keyword = C_RID_CODE (token->u.value);
912 }
913 }
914 }
915
916 /* Update the globals input_location and the input file stack from TOKEN. */
917 static inline void
918 cp_lexer_set_source_position_from_token (cp_token *token)
919 {
920 input_location = token->location;
921 }
922
923 /* Update the globals input_location and the input file stack from LEXER. */
924 static inline void
925 cp_lexer_set_source_position (cp_lexer *lexer)
926 {
927 cp_token *token = cp_lexer_peek_token (lexer);
928 cp_lexer_set_source_position_from_token (token);
929 }
930
931 /* Return a pointer to the next token in the token stream, but do not
932 consume it. */
933
934 static inline cp_token *
935 cp_lexer_peek_token (cp_lexer *lexer)
936 {
937 if (cp_lexer_debugging_p (lexer))
938 {
939 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
940 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
941 putc ('\n', cp_lexer_debug_stream);
942 }
943 return lexer->next_token;
944 }
945
946 /* Return true if the next token has the indicated TYPE. */
947
948 static inline bool
949 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
950 {
951 return cp_lexer_peek_token (lexer)->type == type;
952 }
953
954 /* Return true if the next token does not have the indicated TYPE. */
955
956 static inline bool
957 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
958 {
959 return !cp_lexer_next_token_is (lexer, type);
960 }
961
962 /* Return true if the next token is the indicated KEYWORD. */
963
964 static inline bool
965 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
966 {
967 return cp_lexer_peek_token (lexer)->keyword == keyword;
968 }
969
970 static inline bool
971 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
972 {
973 return cp_lexer_peek_nth_token (lexer, n)->type == type;
974 }
975
976 static inline bool
977 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
978 {
979 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
980 }
981
982 /* Return true if KEYWORD can start a decl-specifier. */
983
984 bool
985 cp_keyword_starts_decl_specifier_p (enum rid keyword)
986 {
987 switch (keyword)
988 {
989 /* auto specifier: storage-class-specifier in C++,
990 simple-type-specifier in C++0x. */
991 case RID_AUTO:
992 /* Storage classes. */
993 case RID_REGISTER:
994 case RID_STATIC:
995 case RID_EXTERN:
996 case RID_MUTABLE:
997 case RID_THREAD:
998 /* Elaborated type specifiers. */
999 case RID_ENUM:
1000 case RID_CLASS:
1001 case RID_STRUCT:
1002 case RID_UNION:
1003 case RID_TYPENAME:
1004 /* Simple type specifiers. */
1005 case RID_CHAR:
1006 case RID_CHAR8:
1007 case RID_CHAR16:
1008 case RID_CHAR32:
1009 case RID_WCHAR:
1010 case RID_BOOL:
1011 case RID_SHORT:
1012 case RID_INT:
1013 case RID_LONG:
1014 case RID_SIGNED:
1015 case RID_UNSIGNED:
1016 case RID_FLOAT:
1017 case RID_DOUBLE:
1018 case RID_VOID:
1019 /* GNU extensions. */
1020 case RID_ATTRIBUTE:
1021 case RID_TYPEOF:
1022 /* C++11 extensions. */
1023 case RID_DECLTYPE:
1024 case RID_UNDERLYING_TYPE:
1025 case RID_CONSTEXPR:
1026 /* C++20 extensions. */
1027 case RID_CONSTINIT:
1028 case RID_CONSTEVAL:
1029 return true;
1030
1031 default:
1032 if (keyword >= RID_FIRST_INT_N
1033 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
1034 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
1035 return true;
1036 return false;
1037 }
1038 }
1039
1040 /* Return true if the next token is a keyword for a decl-specifier. */
1041
1042 static bool
1043 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1044 {
1045 cp_token *token;
1046
1047 token = cp_lexer_peek_token (lexer);
1048 return cp_keyword_starts_decl_specifier_p (token->keyword);
1049 }
1050
1051 /* Returns TRUE iff the token T begins a decltype type. */
1052
1053 static bool
1054 token_is_decltype (cp_token *t)
1055 {
1056 return (t->keyword == RID_DECLTYPE
1057 || t->type == CPP_DECLTYPE);
1058 }
1059
1060 /* Returns TRUE iff the next token begins a decltype type. */
1061
1062 static bool
1063 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1064 {
1065 cp_token *t = cp_lexer_peek_token (lexer);
1066 return token_is_decltype (t);
1067 }
1068
1069 /* Called when processing a token with tree_check_value; perform or defer the
1070 associated checks and return the value. */
1071
1072 static tree
1073 saved_checks_value (struct tree_check *check_value)
1074 {
1075 /* Perform any access checks that were deferred. */
1076 vec<deferred_access_check, va_gc> *checks;
1077 deferred_access_check *chk;
1078 checks = check_value->checks;
1079 if (checks)
1080 {
1081 int i;
1082 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1083 perform_or_defer_access_check (chk->binfo,
1084 chk->decl,
1085 chk->diag_decl, tf_warning_or_error);
1086 }
1087 /* Return the stored value. */
1088 return check_value->value;
1089 }
1090
1091 /* Return a pointer to the Nth token in the token stream. If N is 1,
1092 then this is precisely equivalent to cp_lexer_peek_token (except
1093 that it is not inline). One would like to disallow that case, but
1094 there is one case (cp_parser_nth_token_starts_template_id) where
1095 the caller passes a variable for N and it might be 1. */
1096
1097 static cp_token *
1098 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1099 {
1100 cp_token *token;
1101
1102 /* N is 1-based, not zero-based. */
1103 gcc_assert (n > 0);
1104
1105 if (cp_lexer_debugging_p (lexer))
1106 fprintf (cp_lexer_debug_stream,
1107 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1108
1109 --n;
1110 token = lexer->next_token;
1111 while (n && token->type != CPP_EOF)
1112 {
1113 ++token;
1114 if (!token->purged_p)
1115 --n;
1116 }
1117
1118 if (cp_lexer_debugging_p (lexer))
1119 {
1120 cp_lexer_print_token (cp_lexer_debug_stream, token);
1121 putc ('\n', cp_lexer_debug_stream);
1122 }
1123
1124 return token;
1125 }
1126
1127 /* Return the next token, and advance the lexer's next_token pointer
1128 to point to the next non-purged token. */
1129
1130 static cp_token *
1131 cp_lexer_consume_token (cp_lexer* lexer)
1132 {
1133 cp_token *token = lexer->next_token;
1134
1135 do
1136 {
1137 gcc_assert (token->type != CPP_EOF);
1138 lexer->next_token++;
1139 }
1140 while (lexer->next_token->purged_p);
1141
1142 cp_lexer_set_source_position_from_token (token);
1143
1144 /* Provide debugging output. */
1145 if (cp_lexer_debugging_p (lexer))
1146 {
1147 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1148 cp_lexer_print_token (cp_lexer_debug_stream, token);
1149 putc ('\n', cp_lexer_debug_stream);
1150 }
1151
1152 return token;
1153 }
1154
1155 /* Permanently remove the next token from the token stream, and
1156 advance the next_token pointer to refer to the next non-purged
1157 token. */
1158
1159 static void
1160 cp_lexer_purge_token (cp_lexer *lexer)
1161 {
1162 cp_token *tok = lexer->next_token;
1163
1164 gcc_assert (tok->type != CPP_EOF);
1165 tok->purged_p = true;
1166 tok->location = UNKNOWN_LOCATION;
1167 tok->u.value = NULL_TREE;
1168 tok->keyword = RID_MAX;
1169
1170 do
1171 tok++;
1172 while (tok->purged_p);
1173 lexer->next_token = tok;
1174 }
1175
1176 /* Permanently remove all tokens after TOK, up to, but not
1177 including, the token that will be returned next by
1178 cp_lexer_peek_token. */
1179
1180 static void
1181 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1182 {
1183 cp_token *peek = lexer->next_token;
1184
1185 gcc_assert (tok < peek);
1186
1187 for (tok++; tok != peek; tok++)
1188 {
1189 tok->purged_p = true;
1190 tok->location = UNKNOWN_LOCATION;
1191 tok->u.value = NULL_TREE;
1192 tok->keyword = RID_MAX;
1193 }
1194 }
1195
1196 /* Begin saving tokens. All tokens consumed after this point will be
1197 preserved. */
1198
1199 static void
1200 cp_lexer_save_tokens (cp_lexer* lexer)
1201 {
1202 /* Provide debugging output. */
1203 if (cp_lexer_debugging_p (lexer))
1204 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1205
1206 lexer->saved_tokens.safe_push (lexer->next_token);
1207 }
1208
1209 /* Commit to the portion of the token stream most recently saved. */
1210
1211 static void
1212 cp_lexer_commit_tokens (cp_lexer* lexer)
1213 {
1214 /* Provide debugging output. */
1215 if (cp_lexer_debugging_p (lexer))
1216 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1217
1218 lexer->saved_tokens.pop ();
1219 }
1220
1221 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1222 to the token stream. Stop saving tokens. */
1223
1224 static void
1225 cp_lexer_rollback_tokens (cp_lexer* lexer)
1226 {
1227 /* Provide debugging output. */
1228 if (cp_lexer_debugging_p (lexer))
1229 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1230
1231 lexer->next_token = lexer->saved_tokens.pop ();
1232 }
1233
1234 /* RAII wrapper around the above functions, with sanity checking. Creating
1235 a variable saves tokens, which are committed when the variable is
1236 destroyed unless they are explicitly rolled back by calling the rollback
1237 member function. */
1238
1239 struct saved_token_sentinel
1240 {
1241 cp_lexer *lexer;
1242 unsigned len;
1243 bool commit;
1244 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1245 {
1246 len = lexer->saved_tokens.length ();
1247 cp_lexer_save_tokens (lexer);
1248 }
1249 void rollback ()
1250 {
1251 cp_lexer_rollback_tokens (lexer);
1252 commit = false;
1253 }
1254 ~saved_token_sentinel()
1255 {
1256 if (commit)
1257 cp_lexer_commit_tokens (lexer);
1258 gcc_assert (lexer->saved_tokens.length () == len);
1259 }
1260 };
1261
1262 /* Print a representation of the TOKEN on the STREAM. */
1263
1264 static void
1265 cp_lexer_print_token (FILE * stream, cp_token *token)
1266 {
1267 /* We don't use cpp_type2name here because the parser defines
1268 a few tokens of its own. */
1269 static const char *const token_names[] = {
1270 /* cpplib-defined token types */
1271 #define OP(e, s) #e,
1272 #define TK(e, s) #e,
1273 TTYPE_TABLE
1274 #undef OP
1275 #undef TK
1276 /* C++ parser token types - see "Manifest constants", above. */
1277 "KEYWORD",
1278 "TEMPLATE_ID",
1279 "NESTED_NAME_SPECIFIER",
1280 };
1281
1282 /* For some tokens, print the associated data. */
1283 switch (token->type)
1284 {
1285 case CPP_KEYWORD:
1286 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1287 For example, `struct' is mapped to an INTEGER_CST. */
1288 if (!identifier_p (token->u.value))
1289 break;
1290 /* fall through */
1291 case CPP_NAME:
1292 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1293 break;
1294
1295 case CPP_STRING:
1296 case CPP_STRING16:
1297 case CPP_STRING32:
1298 case CPP_WSTRING:
1299 case CPP_UTF8STRING:
1300 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1301 break;
1302
1303 case CPP_NUMBER:
1304 print_generic_expr (stream, token->u.value);
1305 break;
1306
1307 default:
1308 /* If we have a name for the token, print it out. Otherwise, we
1309 simply give the numeric code. */
1310 if (token->type < ARRAY_SIZE(token_names))
1311 fputs (token_names[token->type], stream);
1312 else
1313 fprintf (stream, "[%d]", token->type);
1314 break;
1315 }
1316 }
1317
1318 DEBUG_FUNCTION void
1319 debug (cp_token &ref)
1320 {
1321 cp_lexer_print_token (stderr, &ref);
1322 fprintf (stderr, "\n");
1323 }
1324
1325 DEBUG_FUNCTION void
1326 debug (cp_token *ptr)
1327 {
1328 if (ptr)
1329 debug (*ptr);
1330 else
1331 fprintf (stderr, "<nil>\n");
1332 }
1333
1334
1335 /* Start emitting debugging information. */
1336
1337 static void
1338 cp_lexer_start_debugging (cp_lexer* lexer)
1339 {
1340 if (!LEXER_DEBUGGING_ENABLED_P)
1341 fatal_error (input_location,
1342 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1343
1344 lexer->debugging_p = true;
1345 cp_lexer_debug_stream = stderr;
1346 }
1347
1348 /* Stop emitting debugging information. */
1349
1350 static void
1351 cp_lexer_stop_debugging (cp_lexer* lexer)
1352 {
1353 if (!LEXER_DEBUGGING_ENABLED_P)
1354 fatal_error (input_location,
1355 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1356
1357 lexer->debugging_p = false;
1358 cp_lexer_debug_stream = NULL;
1359 }
1360
1361 /* Create a new cp_token_cache, representing a range of tokens. */
1362
1363 static cp_token_cache *
1364 cp_token_cache_new (cp_token *first, cp_token *last)
1365 {
1366 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1367 cache->first = first;
1368 cache->last = last;
1369 return cache;
1370 }
1371
1372 /* Diagnose if #pragma omp declare simd isn't followed immediately
1373 by function declaration or definition. */
1374
1375 static inline void
1376 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1377 {
1378 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1379 {
1380 error ("%<#pragma omp declare %s%> not immediately followed by "
1381 "function declaration or definition",
1382 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1383 parser->omp_declare_simd = NULL;
1384 }
1385 }
1386
1387 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1388 and put that into "omp declare simd" attribute. */
1389
1390 static inline void
1391 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1392 {
1393 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1394 {
1395 if (fndecl == error_mark_node)
1396 {
1397 parser->omp_declare_simd = NULL;
1398 return;
1399 }
1400 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1401 {
1402 cp_ensure_no_omp_declare_simd (parser);
1403 return;
1404 }
1405 }
1406 }
1407
1408 /* Diagnose if #pragma acc routine isn't followed immediately by function
1409 declaration or definition. */
1410
1411 static inline void
1412 cp_ensure_no_oacc_routine (cp_parser *parser)
1413 {
1414 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1415 {
1416 error_at (parser->oacc_routine->loc,
1417 "%<#pragma acc routine%> not immediately followed by "
1418 "function declaration or definition");
1419 parser->oacc_routine = NULL;
1420 }
1421 }
1422 \f
1423 /* Decl-specifiers. */
1424
1425 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1426
1427 static void
1428 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1429 {
1430 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1431 }
1432
1433 /* Declarators. */
1434
1435 /* Nothing other than the parser should be creating declarators;
1436 declarators are a semi-syntactic representation of C++ entities.
1437 Other parts of the front end that need to create entities (like
1438 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1439
1440 static cp_declarator *make_call_declarator
1441 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier,
1442 tree, tree, tree, tree, location_t);
1443 static cp_declarator *make_array_declarator
1444 (cp_declarator *, tree);
1445 static cp_declarator *make_pointer_declarator
1446 (cp_cv_quals, cp_declarator *, tree);
1447 static cp_declarator *make_reference_declarator
1448 (cp_cv_quals, cp_declarator *, bool, tree);
1449 static cp_declarator *make_ptrmem_declarator
1450 (cp_cv_quals, tree, cp_declarator *, tree);
1451
1452 /* An erroneous declarator. */
1453 static cp_declarator *cp_error_declarator;
1454
1455 /* The obstack on which declarators and related data structures are
1456 allocated. */
1457 static struct obstack declarator_obstack;
1458
1459 /* Alloc BYTES from the declarator memory pool. */
1460
1461 static inline void *
1462 alloc_declarator (size_t bytes)
1463 {
1464 return obstack_alloc (&declarator_obstack, bytes);
1465 }
1466
1467 /* Allocate a declarator of the indicated KIND. Clear fields that are
1468 common to all declarators. */
1469
1470 static cp_declarator *
1471 make_declarator (cp_declarator_kind kind)
1472 {
1473 cp_declarator *declarator;
1474
1475 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1476 declarator->kind = kind;
1477 declarator->parenthesized = UNKNOWN_LOCATION;
1478 declarator->attributes = NULL_TREE;
1479 declarator->std_attributes = NULL_TREE;
1480 declarator->declarator = NULL;
1481 declarator->parameter_pack_p = false;
1482 declarator->id_loc = UNKNOWN_LOCATION;
1483
1484 return declarator;
1485 }
1486
1487 /* Make a declarator for a generalized identifier. If
1488 QUALIFYING_SCOPE is non-NULL, the identifier is
1489 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1490 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1491 is, if any. */
1492
1493 static cp_declarator *
1494 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1495 special_function_kind sfk, location_t id_location)
1496 {
1497 cp_declarator *declarator;
1498
1499 /* It is valid to write:
1500
1501 class C { void f(); };
1502 typedef C D;
1503 void D::f();
1504
1505 The standard is not clear about whether `typedef const C D' is
1506 legal; as of 2002-09-15 the committee is considering that
1507 question. EDG 3.0 allows that syntax. Therefore, we do as
1508 well. */
1509 if (qualifying_scope && TYPE_P (qualifying_scope))
1510 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1511
1512 gcc_assert (identifier_p (unqualified_name)
1513 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1514 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1515
1516 declarator = make_declarator (cdk_id);
1517 declarator->u.id.qualifying_scope = qualifying_scope;
1518 declarator->u.id.unqualified_name = unqualified_name;
1519 declarator->u.id.sfk = sfk;
1520 declarator->id_loc = id_location;
1521
1522 return declarator;
1523 }
1524
1525 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1526 of modifiers such as const or volatile to apply to the pointer
1527 type, represented as identifiers. ATTRIBUTES represent the attributes that
1528 appertain to the pointer or reference. */
1529
1530 cp_declarator *
1531 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1532 tree attributes)
1533 {
1534 cp_declarator *declarator;
1535
1536 declarator = make_declarator (cdk_pointer);
1537 declarator->declarator = target;
1538 declarator->u.pointer.qualifiers = cv_qualifiers;
1539 declarator->u.pointer.class_type = NULL_TREE;
1540 if (target)
1541 {
1542 declarator->id_loc = target->id_loc;
1543 declarator->parameter_pack_p = target->parameter_pack_p;
1544 target->parameter_pack_p = false;
1545 }
1546 else
1547 declarator->parameter_pack_p = false;
1548
1549 declarator->std_attributes = attributes;
1550
1551 return declarator;
1552 }
1553
1554 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1555 represent the attributes that appertain to the pointer or
1556 reference. */
1557
1558 cp_declarator *
1559 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1560 bool rvalue_ref, tree attributes)
1561 {
1562 cp_declarator *declarator;
1563
1564 declarator = make_declarator (cdk_reference);
1565 declarator->declarator = target;
1566 declarator->u.reference.qualifiers = cv_qualifiers;
1567 declarator->u.reference.rvalue_ref = rvalue_ref;
1568 if (target)
1569 {
1570 declarator->id_loc = target->id_loc;
1571 declarator->parameter_pack_p = target->parameter_pack_p;
1572 target->parameter_pack_p = false;
1573 }
1574 else
1575 declarator->parameter_pack_p = false;
1576
1577 declarator->std_attributes = attributes;
1578
1579 return declarator;
1580 }
1581
1582 /* Like make_pointer_declarator -- but for a pointer to a non-static
1583 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1584 appertain to the pointer or reference. */
1585
1586 cp_declarator *
1587 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1588 cp_declarator *pointee,
1589 tree attributes)
1590 {
1591 cp_declarator *declarator;
1592
1593 declarator = make_declarator (cdk_ptrmem);
1594 declarator->declarator = pointee;
1595 declarator->u.pointer.qualifiers = cv_qualifiers;
1596 declarator->u.pointer.class_type = class_type;
1597
1598 if (pointee)
1599 {
1600 declarator->parameter_pack_p = pointee->parameter_pack_p;
1601 pointee->parameter_pack_p = false;
1602 }
1603 else
1604 declarator->parameter_pack_p = false;
1605
1606 declarator->std_attributes = attributes;
1607
1608 return declarator;
1609 }
1610
1611 /* Make a declarator for the function given by TARGET, with the
1612 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1613 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1614 indicates what exceptions can be thrown. */
1615
1616 cp_declarator *
1617 make_call_declarator (cp_declarator *target,
1618 tree parms,
1619 cp_cv_quals cv_qualifiers,
1620 cp_virt_specifiers virt_specifiers,
1621 cp_ref_qualifier ref_qualifier,
1622 tree tx_qualifier,
1623 tree exception_specification,
1624 tree late_return_type,
1625 tree requires_clause,
1626 location_t parens_loc)
1627 {
1628 cp_declarator *declarator;
1629
1630 declarator = make_declarator (cdk_function);
1631 declarator->declarator = target;
1632 declarator->u.function.parameters = parms;
1633 declarator->u.function.qualifiers = cv_qualifiers;
1634 declarator->u.function.virt_specifiers = virt_specifiers;
1635 declarator->u.function.ref_qualifier = ref_qualifier;
1636 declarator->u.function.tx_qualifier = tx_qualifier;
1637 declarator->u.function.exception_specification = exception_specification;
1638 declarator->u.function.late_return_type = late_return_type;
1639 declarator->u.function.requires_clause = requires_clause;
1640 declarator->u.function.parens_loc = parens_loc;
1641 if (target)
1642 {
1643 declarator->id_loc = target->id_loc;
1644 declarator->parameter_pack_p = target->parameter_pack_p;
1645 target->parameter_pack_p = false;
1646 }
1647 else
1648 declarator->parameter_pack_p = false;
1649
1650 return declarator;
1651 }
1652
1653 /* Make a declarator for an array of BOUNDS elements, each of which is
1654 defined by ELEMENT. */
1655
1656 cp_declarator *
1657 make_array_declarator (cp_declarator *element, tree bounds)
1658 {
1659 cp_declarator *declarator;
1660
1661 declarator = make_declarator (cdk_array);
1662 declarator->declarator = element;
1663 declarator->u.array.bounds = bounds;
1664 if (element)
1665 {
1666 declarator->id_loc = element->id_loc;
1667 declarator->parameter_pack_p = element->parameter_pack_p;
1668 element->parameter_pack_p = false;
1669 }
1670 else
1671 declarator->parameter_pack_p = false;
1672
1673 return declarator;
1674 }
1675
1676 /* Determine whether the declarator we've seen so far can be a
1677 parameter pack, when followed by an ellipsis. */
1678 static bool
1679 declarator_can_be_parameter_pack (cp_declarator *declarator)
1680 {
1681 if (declarator && declarator->parameter_pack_p)
1682 /* We already saw an ellipsis. */
1683 return false;
1684
1685 /* Search for a declarator name, or any other declarator that goes
1686 after the point where the ellipsis could appear in a parameter
1687 pack. If we find any of these, then this declarator cannot be
1688 made into a parameter pack. */
1689 bool found = false;
1690 while (declarator && !found)
1691 {
1692 switch ((int)declarator->kind)
1693 {
1694 case cdk_id:
1695 case cdk_array:
1696 case cdk_decomp:
1697 found = true;
1698 break;
1699
1700 case cdk_error:
1701 return true;
1702
1703 default:
1704 declarator = declarator->declarator;
1705 break;
1706 }
1707 }
1708
1709 return !found;
1710 }
1711
1712 cp_parameter_declarator *no_parameters;
1713
1714 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1715 DECLARATOR and DEFAULT_ARGUMENT. */
1716
1717 cp_parameter_declarator *
1718 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1719 cp_declarator *declarator,
1720 tree default_argument,
1721 location_t loc,
1722 bool template_parameter_pack_p = false)
1723 {
1724 cp_parameter_declarator *parameter;
1725
1726 parameter = ((cp_parameter_declarator *)
1727 alloc_declarator (sizeof (cp_parameter_declarator)));
1728 parameter->next = NULL;
1729 if (decl_specifiers)
1730 parameter->decl_specifiers = *decl_specifiers;
1731 else
1732 clear_decl_specs (&parameter->decl_specifiers);
1733 parameter->declarator = declarator;
1734 parameter->default_argument = default_argument;
1735 parameter->template_parameter_pack_p = template_parameter_pack_p;
1736 parameter->loc = loc;
1737
1738 return parameter;
1739 }
1740
1741 /* Returns true iff DECLARATOR is a declaration for a function. */
1742
1743 static bool
1744 function_declarator_p (const cp_declarator *declarator)
1745 {
1746 while (declarator)
1747 {
1748 if (declarator->kind == cdk_function
1749 && declarator->declarator->kind == cdk_id)
1750 return true;
1751 if (declarator->kind == cdk_id
1752 || declarator->kind == cdk_decomp
1753 || declarator->kind == cdk_error)
1754 return false;
1755 declarator = declarator->declarator;
1756 }
1757 return false;
1758 }
1759
1760 /* The parser. */
1761
1762 /* Overview
1763 --------
1764
1765 A cp_parser parses the token stream as specified by the C++
1766 grammar. Its job is purely parsing, not semantic analysis. For
1767 example, the parser breaks the token stream into declarators,
1768 expressions, statements, and other similar syntactic constructs.
1769 It does not check that the types of the expressions on either side
1770 of an assignment-statement are compatible, or that a function is
1771 not declared with a parameter of type `void'.
1772
1773 The parser invokes routines elsewhere in the compiler to perform
1774 semantic analysis and to build up the abstract syntax tree for the
1775 code processed.
1776
1777 The parser (and the template instantiation code, which is, in a
1778 way, a close relative of parsing) are the only parts of the
1779 compiler that should be calling push_scope and pop_scope, or
1780 related functions. The parser (and template instantiation code)
1781 keeps track of what scope is presently active; everything else
1782 should simply honor that. (The code that generates static
1783 initializers may also need to set the scope, in order to check
1784 access control correctly when emitting the initializers.)
1785
1786 Methodology
1787 -----------
1788
1789 The parser is of the standard recursive-descent variety. Upcoming
1790 tokens in the token stream are examined in order to determine which
1791 production to use when parsing a non-terminal. Some C++ constructs
1792 require arbitrary look ahead to disambiguate. For example, it is
1793 impossible, in the general case, to tell whether a statement is an
1794 expression or declaration without scanning the entire statement.
1795 Therefore, the parser is capable of "parsing tentatively." When the
1796 parser is not sure what construct comes next, it enters this mode.
1797 Then, while we attempt to parse the construct, the parser queues up
1798 error messages, rather than issuing them immediately, and saves the
1799 tokens it consumes. If the construct is parsed successfully, the
1800 parser "commits", i.e., it issues any queued error messages and
1801 the tokens that were being preserved are permanently discarded.
1802 If, however, the construct is not parsed successfully, the parser
1803 rolls back its state completely so that it can resume parsing using
1804 a different alternative.
1805
1806 Future Improvements
1807 -------------------
1808
1809 The performance of the parser could probably be improved substantially.
1810 We could often eliminate the need to parse tentatively by looking ahead
1811 a little bit. In some places, this approach might not entirely eliminate
1812 the need to parse tentatively, but it might still speed up the average
1813 case. */
1814
1815 /* Flags that are passed to some parsing functions. These values can
1816 be bitwise-ored together. */
1817
1818 enum
1819 {
1820 /* No flags. */
1821 CP_PARSER_FLAGS_NONE = 0x0,
1822 /* The construct is optional. If it is not present, then no error
1823 should be issued. */
1824 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1825 /* When parsing a type-specifier, treat user-defined type-names
1826 as non-type identifiers. */
1827 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1828 /* When parsing a type-specifier, do not try to parse a class-specifier
1829 or enum-specifier. */
1830 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1831 /* When parsing a decl-specifier-seq, only allow type-specifier or
1832 constexpr. */
1833 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1834 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
1835 for C++20 consteval. */
1836 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1837 /* When parsing a decl-specifier-seq, allow missing typename. */
1838 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
1839 /* When parsing of the noexcept-specifier should be delayed. */
1840 CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
1841 /* When parsing a consteval declarator. */
1842 CP_PARSER_FLAGS_CONSTEVAL = 0x80
1843 };
1844
1845 /* This type is used for parameters and variables which hold
1846 combinations of the above flags. */
1847 typedef int cp_parser_flags;
1848
1849 /* The different kinds of declarators we want to parse. */
1850
1851 enum cp_parser_declarator_kind
1852 {
1853 /* We want an abstract declarator. */
1854 CP_PARSER_DECLARATOR_ABSTRACT,
1855 /* We want a named declarator. */
1856 CP_PARSER_DECLARATOR_NAMED,
1857 /* We don't mind, but the name must be an unqualified-id. */
1858 CP_PARSER_DECLARATOR_EITHER
1859 };
1860
1861 /* The precedence values used to parse binary expressions. The minimum value
1862 of PREC must be 1, because zero is reserved to quickly discriminate
1863 binary operators from other tokens. */
1864
1865 enum cp_parser_prec
1866 {
1867 PREC_NOT_OPERATOR,
1868 PREC_LOGICAL_OR_EXPRESSION,
1869 PREC_LOGICAL_AND_EXPRESSION,
1870 PREC_INCLUSIVE_OR_EXPRESSION,
1871 PREC_EXCLUSIVE_OR_EXPRESSION,
1872 PREC_AND_EXPRESSION,
1873 PREC_EQUALITY_EXPRESSION,
1874 PREC_RELATIONAL_EXPRESSION,
1875 PREC_SPACESHIP_EXPRESSION,
1876 PREC_SHIFT_EXPRESSION,
1877 PREC_ADDITIVE_EXPRESSION,
1878 PREC_MULTIPLICATIVE_EXPRESSION,
1879 PREC_PM_EXPRESSION,
1880 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1881 };
1882
1883 /* A mapping from a token type to a corresponding tree node type, with a
1884 precedence value. */
1885
1886 struct cp_parser_binary_operations_map_node
1887 {
1888 /* The token type. */
1889 enum cpp_ttype token_type;
1890 /* The corresponding tree code. */
1891 enum tree_code tree_type;
1892 /* The precedence of this operator. */
1893 enum cp_parser_prec prec;
1894 };
1895
1896 struct cp_parser_expression_stack_entry
1897 {
1898 /* Left hand side of the binary operation we are currently
1899 parsing. */
1900 cp_expr lhs;
1901 /* Original tree code for left hand side, if it was a binary
1902 expression itself (used for -Wparentheses). */
1903 enum tree_code lhs_type;
1904 /* Tree code for the binary operation we are parsing. */
1905 enum tree_code tree_type;
1906 /* Precedence of the binary operation we are parsing. */
1907 enum cp_parser_prec prec;
1908 /* Location of the binary operation we are parsing. */
1909 location_t loc;
1910 };
1911
1912 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1913 entries because precedence levels on the stack are monotonically
1914 increasing. */
1915 typedef struct cp_parser_expression_stack_entry
1916 cp_parser_expression_stack[NUM_PREC_VALUES];
1917
1918 /* Prototypes. */
1919
1920 /* Constructors and destructors. */
1921
1922 static cp_parser_context *cp_parser_context_new
1923 (cp_parser_context *);
1924
1925 /* Class variables. */
1926
1927 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1928
1929 /* The operator-precedence table used by cp_parser_binary_expression.
1930 Transformed into an associative array (binops_by_token) by
1931 cp_parser_new. */
1932
1933 static const cp_parser_binary_operations_map_node binops[] = {
1934 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1935 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1936
1937 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1938 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1939 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1940
1941 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1942 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1943
1944 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1945 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1946
1947 { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
1948
1949 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1950 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1951 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1952 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1953
1954 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1955 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1956
1957 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1958
1959 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1960
1961 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1962
1963 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1964
1965 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1966 };
1967
1968 /* The same as binops, but initialized by cp_parser_new so that
1969 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1970 for speed. */
1971 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1972
1973 /* Constructors and destructors. */
1974
1975 /* Construct a new context. The context below this one on the stack
1976 is given by NEXT. */
1977
1978 static cp_parser_context *
1979 cp_parser_context_new (cp_parser_context* next)
1980 {
1981 cp_parser_context *context;
1982
1983 /* Allocate the storage. */
1984 if (cp_parser_context_free_list != NULL)
1985 {
1986 /* Pull the first entry from the free list. */
1987 context = cp_parser_context_free_list;
1988 cp_parser_context_free_list = context->next;
1989 memset (context, 0, sizeof (*context));
1990 }
1991 else
1992 context = ggc_cleared_alloc<cp_parser_context> ();
1993
1994 /* No errors have occurred yet in this context. */
1995 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1996 /* If this is not the bottommost context, copy information that we
1997 need from the previous context. */
1998 if (next)
1999 {
2000 /* If, in the NEXT context, we are parsing an `x->' or `x.'
2001 expression, then we are parsing one in this context, too. */
2002 context->object_type = next->object_type;
2003 /* Thread the stack. */
2004 context->next = next;
2005 }
2006
2007 return context;
2008 }
2009
2010 /* Managing the unparsed function queues. */
2011
2012 #define unparsed_funs_with_default_args \
2013 parser->unparsed_queues->last ().funs_with_default_args
2014 #define unparsed_funs_with_definitions \
2015 parser->unparsed_queues->last ().funs_with_definitions
2016 #define unparsed_nsdmis \
2017 parser->unparsed_queues->last ().nsdmis
2018 #define unparsed_noexcepts \
2019 parser->unparsed_queues->last ().noexcepts
2020
2021 static void
2022 push_unparsed_function_queues (cp_parser *parser)
2023 {
2024 cp_unparsed_functions_entry e = { NULL, make_tree_vector (), NULL, NULL };
2025 vec_safe_push (parser->unparsed_queues, e);
2026 }
2027
2028 static void
2029 pop_unparsed_function_queues (cp_parser *parser)
2030 {
2031 release_tree_vector (unparsed_funs_with_definitions);
2032 parser->unparsed_queues->pop ();
2033 }
2034
2035 /* Prototypes. */
2036
2037 /* Constructors and destructors. */
2038
2039 static cp_parser *cp_parser_new
2040 (cp_lexer *);
2041
2042 /* Routines to parse various constructs.
2043
2044 Those that return `tree' will return the error_mark_node (rather
2045 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2046 Sometimes, they will return an ordinary node if error-recovery was
2047 attempted, even though a parse error occurred. So, to check
2048 whether or not a parse error occurred, you should always use
2049 cp_parser_error_occurred. If the construct is optional (indicated
2050 either by an `_opt' in the name of the function that does the
2051 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2052 the construct is not present. */
2053
2054 /* Lexical conventions [gram.lex] */
2055
2056 static cp_expr cp_parser_identifier
2057 (cp_parser *);
2058 static cp_expr cp_parser_string_literal
2059 (cp_parser *, bool, bool, bool);
2060 static cp_expr cp_parser_userdef_char_literal
2061 (cp_parser *);
2062 static tree cp_parser_userdef_string_literal
2063 (tree);
2064 static cp_expr cp_parser_userdef_numeric_literal
2065 (cp_parser *);
2066
2067 /* Basic concepts [gram.basic] */
2068
2069 static void cp_parser_translation_unit (cp_parser *);
2070
2071 /* Expressions [gram.expr] */
2072
2073 static cp_expr cp_parser_primary_expression
2074 (cp_parser *, bool, bool, bool, cp_id_kind *);
2075 static cp_expr cp_parser_id_expression
2076 (cp_parser *, bool, bool, bool *, bool, bool);
2077 static cp_expr cp_parser_unqualified_id
2078 (cp_parser *, bool, bool, bool, bool);
2079 static tree cp_parser_nested_name_specifier_opt
2080 (cp_parser *, bool, bool, bool, bool, bool = false);
2081 static tree cp_parser_nested_name_specifier
2082 (cp_parser *, bool, bool, bool, bool);
2083 static tree cp_parser_qualifying_entity
2084 (cp_parser *, bool, bool, bool, bool, bool);
2085 static cp_expr cp_parser_postfix_expression
2086 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2087 static tree cp_parser_postfix_open_square_expression
2088 (cp_parser *, tree, bool, bool);
2089 static tree cp_parser_postfix_dot_deref_expression
2090 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2091 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2092 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2093 bool = false);
2094 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2095 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2096 static void cp_parser_pseudo_destructor_name
2097 (cp_parser *, tree, tree *, tree *);
2098 static cp_expr cp_parser_unary_expression
2099 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2100 static enum tree_code cp_parser_unary_operator
2101 (cp_token *);
2102 static tree cp_parser_has_attribute_expression
2103 (cp_parser *);
2104 static tree cp_parser_new_expression
2105 (cp_parser *);
2106 static vec<tree, va_gc> *cp_parser_new_placement
2107 (cp_parser *);
2108 static tree cp_parser_new_type_id
2109 (cp_parser *, tree *);
2110 static cp_declarator *cp_parser_new_declarator_opt
2111 (cp_parser *);
2112 static cp_declarator *cp_parser_direct_new_declarator
2113 (cp_parser *);
2114 static vec<tree, va_gc> *cp_parser_new_initializer
2115 (cp_parser *);
2116 static tree cp_parser_delete_expression
2117 (cp_parser *);
2118 static cp_expr cp_parser_cast_expression
2119 (cp_parser *, bool, bool, bool, cp_id_kind *);
2120 static cp_expr cp_parser_binary_expression
2121 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2122 static tree cp_parser_question_colon_clause
2123 (cp_parser *, cp_expr);
2124 static cp_expr cp_parser_assignment_expression
2125 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2126 static enum tree_code cp_parser_assignment_operator_opt
2127 (cp_parser *);
2128 static cp_expr cp_parser_expression
2129 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2130 static cp_expr cp_parser_constant_expression
2131 (cp_parser *, bool = false, bool * = NULL, bool = false);
2132 static cp_expr cp_parser_builtin_offsetof
2133 (cp_parser *);
2134 static cp_expr cp_parser_lambda_expression
2135 (cp_parser *);
2136 static void cp_parser_lambda_introducer
2137 (cp_parser *, tree);
2138 static bool cp_parser_lambda_declarator_opt
2139 (cp_parser *, tree);
2140 static void cp_parser_lambda_body
2141 (cp_parser *, tree);
2142
2143 /* Statements [gram.stmt.stmt] */
2144
2145 static void cp_parser_statement
2146 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2147 static void cp_parser_label_for_labeled_statement
2148 (cp_parser *, tree);
2149 static tree cp_parser_expression_statement
2150 (cp_parser *, tree);
2151 static tree cp_parser_compound_statement
2152 (cp_parser *, tree, int, bool);
2153 static void cp_parser_statement_seq_opt
2154 (cp_parser *, tree);
2155 static tree cp_parser_selection_statement
2156 (cp_parser *, bool *, vec<tree> *);
2157 static tree cp_parser_condition
2158 (cp_parser *);
2159 static tree cp_parser_iteration_statement
2160 (cp_parser *, bool *, bool, unsigned short);
2161 static bool cp_parser_init_statement
2162 (cp_parser *, tree *decl);
2163 static tree cp_parser_for
2164 (cp_parser *, bool, unsigned short);
2165 static tree cp_parser_c_for
2166 (cp_parser *, tree, tree, bool, unsigned short);
2167 static tree cp_parser_range_for
2168 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2169 static void do_range_for_auto_deduction
2170 (tree, tree);
2171 static tree cp_parser_perform_range_for_lookup
2172 (tree, tree *, tree *);
2173 static tree cp_parser_range_for_member_function
2174 (tree, tree);
2175 static tree cp_parser_jump_statement
2176 (cp_parser *);
2177 static void cp_parser_declaration_statement
2178 (cp_parser *);
2179
2180 static tree cp_parser_implicitly_scoped_statement
2181 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2182 static void cp_parser_already_scoped_statement
2183 (cp_parser *, bool *, const token_indent_info &);
2184
2185 /* Declarations [gram.dcl.dcl] */
2186
2187 static void cp_parser_declaration_seq_opt
2188 (cp_parser *);
2189 static void cp_parser_declaration
2190 (cp_parser *);
2191 static void cp_parser_toplevel_declaration
2192 (cp_parser *);
2193 static void cp_parser_block_declaration
2194 (cp_parser *, bool);
2195 static void cp_parser_simple_declaration
2196 (cp_parser *, bool, tree *);
2197 static void cp_parser_decl_specifier_seq
2198 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2199 static tree cp_parser_storage_class_specifier_opt
2200 (cp_parser *);
2201 static tree cp_parser_function_specifier_opt
2202 (cp_parser *, cp_decl_specifier_seq *);
2203 static tree cp_parser_type_specifier
2204 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2205 int *, bool *);
2206 static tree cp_parser_simple_type_specifier
2207 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2208 static tree cp_parser_placeholder_type_specifier
2209 (cp_parser *, location_t, tree, bool);
2210 static tree cp_parser_type_name
2211 (cp_parser *, bool);
2212 static tree cp_parser_nonclass_name
2213 (cp_parser* parser);
2214 static tree cp_parser_elaborated_type_specifier
2215 (cp_parser *, bool, bool);
2216 static tree cp_parser_enum_specifier
2217 (cp_parser *);
2218 static void cp_parser_enumerator_list
2219 (cp_parser *, tree);
2220 static void cp_parser_enumerator_definition
2221 (cp_parser *, tree);
2222 static tree cp_parser_namespace_name
2223 (cp_parser *);
2224 static void cp_parser_namespace_definition
2225 (cp_parser *);
2226 static void cp_parser_namespace_body
2227 (cp_parser *);
2228 static tree cp_parser_qualified_namespace_specifier
2229 (cp_parser *);
2230 static void cp_parser_namespace_alias_definition
2231 (cp_parser *);
2232 static bool cp_parser_using_declaration
2233 (cp_parser *, bool);
2234 static void cp_parser_using_directive
2235 (cp_parser *);
2236 static tree cp_parser_alias_declaration
2237 (cp_parser *);
2238 static void cp_parser_asm_definition
2239 (cp_parser *);
2240 static void cp_parser_linkage_specification
2241 (cp_parser *);
2242 static void cp_parser_static_assert
2243 (cp_parser *, bool);
2244 static tree cp_parser_decltype
2245 (cp_parser *);
2246 static tree cp_parser_decomposition_declaration
2247 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2248
2249 /* Declarators [gram.dcl.decl] */
2250
2251 static tree cp_parser_init_declarator
2252 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2253 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2254 location_t *, tree *);
2255 static cp_declarator *cp_parser_declarator
2256 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2257 bool, bool, bool);
2258 static cp_declarator *cp_parser_direct_declarator
2259 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2260 bool);
2261 static enum tree_code cp_parser_ptr_operator
2262 (cp_parser *, tree *, cp_cv_quals *, tree *);
2263 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2264 (cp_parser *);
2265 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2266 (cp_parser *);
2267 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2268 (cp_parser *);
2269 static tree cp_parser_tx_qualifier_opt
2270 (cp_parser *);
2271 static tree cp_parser_late_return_type_opt
2272 (cp_parser *, cp_declarator *, tree &);
2273 static tree cp_parser_declarator_id
2274 (cp_parser *, bool);
2275 static tree cp_parser_type_id
2276 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2277 static tree cp_parser_template_type_arg
2278 (cp_parser *);
2279 static tree cp_parser_trailing_type_id (cp_parser *);
2280 static tree cp_parser_type_id_1
2281 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2282 static void cp_parser_type_specifier_seq
2283 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2284 static tree cp_parser_parameter_declaration_clause
2285 (cp_parser *, cp_parser_flags);
2286 static tree cp_parser_parameter_declaration_list
2287 (cp_parser *, cp_parser_flags);
2288 static cp_parameter_declarator *cp_parser_parameter_declaration
2289 (cp_parser *, cp_parser_flags, bool, bool *);
2290 static tree cp_parser_default_argument
2291 (cp_parser *, bool);
2292 static void cp_parser_function_body
2293 (cp_parser *, bool);
2294 static tree cp_parser_initializer
2295 (cp_parser *, bool *, bool *, bool = false);
2296 static cp_expr cp_parser_initializer_clause
2297 (cp_parser *, bool *);
2298 static cp_expr cp_parser_braced_list
2299 (cp_parser*, bool*);
2300 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2301 (cp_parser *, bool *, bool *);
2302
2303 static void cp_parser_ctor_initializer_opt_and_function_body
2304 (cp_parser *, bool);
2305
2306 static tree cp_parser_late_parsing_omp_declare_simd
2307 (cp_parser *, tree);
2308
2309 static tree cp_parser_late_parsing_oacc_routine
2310 (cp_parser *, tree);
2311
2312 static tree synthesize_implicit_template_parm
2313 (cp_parser *, tree);
2314 static tree finish_fully_implicit_template
2315 (cp_parser *, tree);
2316 static void abort_fully_implicit_template
2317 (cp_parser *);
2318
2319 /* Classes [gram.class] */
2320
2321 static tree cp_parser_class_name
2322 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2323 static tree cp_parser_class_specifier
2324 (cp_parser *);
2325 static tree cp_parser_class_head
2326 (cp_parser *, bool *);
2327 static enum tag_types cp_parser_class_key
2328 (cp_parser *);
2329 static void cp_parser_type_parameter_key
2330 (cp_parser* parser);
2331 static void cp_parser_member_specification_opt
2332 (cp_parser *);
2333 static void cp_parser_member_declaration
2334 (cp_parser *);
2335 static tree cp_parser_pure_specifier
2336 (cp_parser *);
2337 static tree cp_parser_constant_initializer
2338 (cp_parser *);
2339
2340 /* Derived classes [gram.class.derived] */
2341
2342 static tree cp_parser_base_clause
2343 (cp_parser *);
2344 static tree cp_parser_base_specifier
2345 (cp_parser *);
2346
2347 /* Special member functions [gram.special] */
2348
2349 static tree cp_parser_conversion_function_id
2350 (cp_parser *);
2351 static tree cp_parser_conversion_type_id
2352 (cp_parser *);
2353 static cp_declarator *cp_parser_conversion_declarator_opt
2354 (cp_parser *);
2355 static void cp_parser_ctor_initializer_opt
2356 (cp_parser *);
2357 static void cp_parser_mem_initializer_list
2358 (cp_parser *);
2359 static tree cp_parser_mem_initializer
2360 (cp_parser *);
2361 static tree cp_parser_mem_initializer_id
2362 (cp_parser *);
2363
2364 /* Overloading [gram.over] */
2365
2366 static cp_expr cp_parser_operator_function_id
2367 (cp_parser *);
2368 static cp_expr cp_parser_operator
2369 (cp_parser *, location_t);
2370
2371 /* Templates [gram.temp] */
2372
2373 static void cp_parser_template_declaration
2374 (cp_parser *, bool);
2375 static tree cp_parser_template_parameter_list
2376 (cp_parser *);
2377 static tree cp_parser_template_parameter
2378 (cp_parser *, bool *, bool *);
2379 static tree cp_parser_type_parameter
2380 (cp_parser *, bool *);
2381 static tree cp_parser_template_id
2382 (cp_parser *, bool, bool, enum tag_types, bool);
2383 static tree cp_parser_template_id_expr
2384 (cp_parser *, bool, bool, bool);
2385 static tree cp_parser_template_name
2386 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2387 static tree cp_parser_template_argument_list
2388 (cp_parser *);
2389 static tree cp_parser_template_argument
2390 (cp_parser *);
2391 static void cp_parser_explicit_instantiation
2392 (cp_parser *);
2393 static void cp_parser_explicit_specialization
2394 (cp_parser *);
2395
2396 /* Exception handling [gram.except] */
2397
2398 static tree cp_parser_try_block
2399 (cp_parser *);
2400 static void cp_parser_function_try_block
2401 (cp_parser *);
2402 static void cp_parser_handler_seq
2403 (cp_parser *);
2404 static void cp_parser_handler
2405 (cp_parser *);
2406 static tree cp_parser_exception_declaration
2407 (cp_parser *);
2408 static tree cp_parser_throw_expression
2409 (cp_parser *);
2410 static tree cp_parser_exception_specification_opt
2411 (cp_parser *, cp_parser_flags);
2412 static tree cp_parser_type_id_list
2413 (cp_parser *);
2414 static tree cp_parser_noexcept_specification_opt
2415 (cp_parser *, cp_parser_flags, bool, bool *, bool);
2416
2417 /* GNU Extensions */
2418
2419 static tree cp_parser_asm_specification_opt
2420 (cp_parser *);
2421 static tree cp_parser_asm_operand_list
2422 (cp_parser *);
2423 static tree cp_parser_asm_clobber_list
2424 (cp_parser *);
2425 static tree cp_parser_asm_label_list
2426 (cp_parser *);
2427 static bool cp_next_tokens_can_be_attribute_p
2428 (cp_parser *);
2429 static bool cp_next_tokens_can_be_gnu_attribute_p
2430 (cp_parser *);
2431 static bool cp_next_tokens_can_be_std_attribute_p
2432 (cp_parser *);
2433 static bool cp_nth_tokens_can_be_std_attribute_p
2434 (cp_parser *, size_t);
2435 static bool cp_nth_tokens_can_be_gnu_attribute_p
2436 (cp_parser *, size_t);
2437 static bool cp_nth_tokens_can_be_attribute_p
2438 (cp_parser *, size_t);
2439 static tree cp_parser_attributes_opt
2440 (cp_parser *);
2441 static tree cp_parser_gnu_attributes_opt
2442 (cp_parser *);
2443 static tree cp_parser_gnu_attribute_list
2444 (cp_parser *, bool = false);
2445 static tree cp_parser_std_attribute
2446 (cp_parser *, tree);
2447 static tree cp_parser_std_attribute_spec
2448 (cp_parser *);
2449 static tree cp_parser_std_attribute_spec_seq
2450 (cp_parser *);
2451 static size_t cp_parser_skip_attributes_opt
2452 (cp_parser *, size_t);
2453 static bool cp_parser_extension_opt
2454 (cp_parser *, int *);
2455 static void cp_parser_label_declaration
2456 (cp_parser *);
2457
2458 /* Concept Extensions */
2459
2460 static tree cp_parser_concept_definition
2461 (cp_parser *);
2462 static tree cp_parser_constraint_expression
2463 (cp_parser *);
2464 static tree cp_parser_requires_clause_opt
2465 (cp_parser *, bool);
2466 static tree cp_parser_requires_expression
2467 (cp_parser *);
2468 static tree cp_parser_requirement_parameter_list
2469 (cp_parser *);
2470 static tree cp_parser_requirement_body
2471 (cp_parser *);
2472 static tree cp_parser_requirement_seq
2473 (cp_parser *);
2474 static tree cp_parser_requirement
2475 (cp_parser *);
2476 static tree cp_parser_simple_requirement
2477 (cp_parser *);
2478 static tree cp_parser_compound_requirement
2479 (cp_parser *);
2480 static tree cp_parser_type_requirement
2481 (cp_parser *);
2482 static tree cp_parser_nested_requirement
2483 (cp_parser *);
2484
2485 /* Transactional Memory Extensions */
2486
2487 static tree cp_parser_transaction
2488 (cp_parser *, cp_token *);
2489 static tree cp_parser_transaction_expression
2490 (cp_parser *, enum rid);
2491 static void cp_parser_function_transaction
2492 (cp_parser *, enum rid);
2493 static tree cp_parser_transaction_cancel
2494 (cp_parser *);
2495
2496 /* Coroutine extensions. */
2497
2498 static tree cp_parser_yield_expression
2499 (cp_parser *);
2500
2501
2502 enum pragma_context {
2503 pragma_external,
2504 pragma_member,
2505 pragma_objc_icode,
2506 pragma_stmt,
2507 pragma_compound
2508 };
2509 static bool cp_parser_pragma
2510 (cp_parser *, enum pragma_context, bool *);
2511
2512 /* Objective-C++ Productions */
2513
2514 static tree cp_parser_objc_message_receiver
2515 (cp_parser *);
2516 static tree cp_parser_objc_message_args
2517 (cp_parser *);
2518 static tree cp_parser_objc_message_expression
2519 (cp_parser *);
2520 static cp_expr cp_parser_objc_encode_expression
2521 (cp_parser *);
2522 static tree cp_parser_objc_defs_expression
2523 (cp_parser *);
2524 static tree cp_parser_objc_protocol_expression
2525 (cp_parser *);
2526 static tree cp_parser_objc_selector_expression
2527 (cp_parser *);
2528 static cp_expr cp_parser_objc_expression
2529 (cp_parser *);
2530 static bool cp_parser_objc_selector_p
2531 (enum cpp_ttype);
2532 static tree cp_parser_objc_selector
2533 (cp_parser *);
2534 static tree cp_parser_objc_protocol_refs_opt
2535 (cp_parser *);
2536 static void cp_parser_objc_declaration
2537 (cp_parser *, tree);
2538 static tree cp_parser_objc_statement
2539 (cp_parser *);
2540 static bool cp_parser_objc_valid_prefix_attributes
2541 (cp_parser *, tree *);
2542 static void cp_parser_objc_at_property_declaration
2543 (cp_parser *) ;
2544 static void cp_parser_objc_at_synthesize_declaration
2545 (cp_parser *) ;
2546 static void cp_parser_objc_at_dynamic_declaration
2547 (cp_parser *) ;
2548 static tree cp_parser_objc_struct_declaration
2549 (cp_parser *) ;
2550
2551 /* Utility Routines */
2552
2553 static cp_expr cp_parser_lookup_name
2554 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2555 static tree cp_parser_lookup_name_simple
2556 (cp_parser *, tree, location_t);
2557 static tree cp_parser_maybe_treat_template_as_class
2558 (tree, bool);
2559 static bool cp_parser_check_declarator_template_parameters
2560 (cp_parser *, cp_declarator *, location_t);
2561 static bool cp_parser_check_template_parameters
2562 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2563 static cp_expr cp_parser_simple_cast_expression
2564 (cp_parser *);
2565 static tree cp_parser_global_scope_opt
2566 (cp_parser *, bool);
2567 static bool cp_parser_constructor_declarator_p
2568 (cp_parser *, cp_parser_flags, bool);
2569 static tree cp_parser_function_definition_from_specifiers_and_declarator
2570 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2571 static tree cp_parser_function_definition_after_declarator
2572 (cp_parser *, bool);
2573 static bool cp_parser_template_declaration_after_export
2574 (cp_parser *, bool);
2575 static void cp_parser_perform_template_parameter_access_checks
2576 (vec<deferred_access_check, va_gc> *);
2577 static tree cp_parser_single_declaration
2578 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2579 static cp_expr cp_parser_functional_cast
2580 (cp_parser *, tree);
2581 static tree cp_parser_save_member_function_body
2582 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2583 static tree cp_parser_save_nsdmi
2584 (cp_parser *);
2585 static tree cp_parser_enclosed_template_argument_list
2586 (cp_parser *);
2587 static void cp_parser_save_default_args
2588 (cp_parser *, tree);
2589 static void cp_parser_late_parsing_for_member
2590 (cp_parser *, tree);
2591 static tree cp_parser_late_parse_one_default_arg
2592 (cp_parser *, tree, tree, tree);
2593 static void cp_parser_late_parsing_nsdmi
2594 (cp_parser *, tree);
2595 static void cp_parser_late_parsing_default_args
2596 (cp_parser *, tree);
2597 static tree cp_parser_sizeof_operand
2598 (cp_parser *, enum rid);
2599 static cp_expr cp_parser_trait_expr
2600 (cp_parser *, enum rid);
2601 static bool cp_parser_declares_only_class_p
2602 (cp_parser *);
2603 static void cp_parser_set_storage_class
2604 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2605 static void cp_parser_set_decl_spec_type
2606 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2607 static void set_and_check_decl_spec_loc
2608 (cp_decl_specifier_seq *decl_specs,
2609 cp_decl_spec ds, cp_token *);
2610 static bool cp_parser_friend_p
2611 (const cp_decl_specifier_seq *);
2612 static void cp_parser_required_error
2613 (cp_parser *, required_token, bool, location_t);
2614 static cp_token *cp_parser_require
2615 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2616 static cp_token *cp_parser_require_keyword
2617 (cp_parser *, enum rid, required_token);
2618 static bool cp_parser_token_starts_function_definition_p
2619 (cp_token *);
2620 static bool cp_parser_next_token_starts_class_definition_p
2621 (cp_parser *);
2622 static bool cp_parser_next_token_ends_template_argument_p
2623 (cp_parser *);
2624 static bool cp_parser_nth_token_starts_template_argument_list_p
2625 (cp_parser *, size_t);
2626 static enum tag_types cp_parser_token_is_class_key
2627 (cp_token *);
2628 static enum tag_types cp_parser_token_is_type_parameter_key
2629 (cp_token *);
2630 static void cp_parser_maybe_warn_enum_key (cp_parser *, location_t, tree, rid);
2631 static void cp_parser_check_class_key
2632 (cp_parser *, location_t, enum tag_types, tree type, bool, bool);
2633 static void cp_parser_check_access_in_redeclaration
2634 (tree type, location_t location);
2635 static bool cp_parser_optional_template_keyword
2636 (cp_parser *);
2637 static void cp_parser_pre_parsed_nested_name_specifier
2638 (cp_parser *);
2639 static bool cp_parser_cache_group
2640 (cp_parser *, enum cpp_ttype, unsigned);
2641 static tree cp_parser_cache_defarg
2642 (cp_parser *parser, bool nsdmi);
2643 static void cp_parser_parse_tentatively
2644 (cp_parser *);
2645 static void cp_parser_commit_to_tentative_parse
2646 (cp_parser *);
2647 static void cp_parser_commit_to_topmost_tentative_parse
2648 (cp_parser *);
2649 static void cp_parser_abort_tentative_parse
2650 (cp_parser *);
2651 static bool cp_parser_parse_definitely
2652 (cp_parser *);
2653 static inline bool cp_parser_parsing_tentatively
2654 (cp_parser *);
2655 static bool cp_parser_uncommitted_to_tentative_parse_p
2656 (cp_parser *);
2657 static void cp_parser_error
2658 (cp_parser *, const char *);
2659 static void cp_parser_name_lookup_error
2660 (cp_parser *, tree, tree, name_lookup_error, location_t);
2661 static bool cp_parser_simulate_error
2662 (cp_parser *);
2663 static bool cp_parser_check_type_definition
2664 (cp_parser *);
2665 static void cp_parser_check_for_definition_in_return_type
2666 (cp_declarator *, tree, location_t type_location);
2667 static void cp_parser_check_for_invalid_template_id
2668 (cp_parser *, tree, enum tag_types, location_t location);
2669 static bool cp_parser_non_integral_constant_expression
2670 (cp_parser *, non_integral_constant);
2671 static void cp_parser_diagnose_invalid_type_name
2672 (cp_parser *, tree, location_t);
2673 static bool cp_parser_parse_and_diagnose_invalid_type_name
2674 (cp_parser *);
2675 static int cp_parser_skip_to_closing_parenthesis
2676 (cp_parser *, bool, bool, bool);
2677 static void cp_parser_skip_to_end_of_statement
2678 (cp_parser *);
2679 static void cp_parser_consume_semicolon_at_end_of_statement
2680 (cp_parser *);
2681 static void cp_parser_skip_to_end_of_block_or_statement
2682 (cp_parser *);
2683 static bool cp_parser_skip_to_closing_brace
2684 (cp_parser *);
2685 static void cp_parser_skip_to_end_of_template_parameter_list
2686 (cp_parser *);
2687 static void cp_parser_skip_to_pragma_eol
2688 (cp_parser*, cp_token *);
2689 static bool cp_parser_error_occurred
2690 (cp_parser *);
2691 static bool cp_parser_allow_gnu_extensions_p
2692 (cp_parser *);
2693 static bool cp_parser_is_pure_string_literal
2694 (cp_token *);
2695 static bool cp_parser_is_string_literal
2696 (cp_token *);
2697 static bool cp_parser_is_keyword
2698 (cp_token *, enum rid);
2699 static tree cp_parser_make_typename_type
2700 (cp_parser *, tree, location_t location);
2701 static cp_declarator * cp_parser_make_indirect_declarator
2702 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2703 static bool cp_parser_compound_literal_p
2704 (cp_parser *);
2705 static bool cp_parser_array_designator_p
2706 (cp_parser *);
2707 static bool cp_parser_init_statement_p
2708 (cp_parser *);
2709 static bool cp_parser_skip_to_closing_square_bracket
2710 (cp_parser *);
2711 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
2712
2713 // -------------------------------------------------------------------------- //
2714 // Unevaluated Operand Guard
2715 //
2716 // Implementation of an RAII helper for unevaluated operand parsing.
2717 cp_unevaluated::cp_unevaluated ()
2718 {
2719 ++cp_unevaluated_operand;
2720 ++c_inhibit_evaluation_warnings;
2721 }
2722
2723 cp_unevaluated::~cp_unevaluated ()
2724 {
2725 --c_inhibit_evaluation_warnings;
2726 --cp_unevaluated_operand;
2727 }
2728
2729 // -------------------------------------------------------------------------- //
2730 // Tentative Parsing
2731
2732 /* Returns nonzero if we are parsing tentatively. */
2733
2734 static inline bool
2735 cp_parser_parsing_tentatively (cp_parser* parser)
2736 {
2737 return parser->context->next != NULL;
2738 }
2739
2740 /* Returns nonzero if TOKEN is a string literal. */
2741
2742 static bool
2743 cp_parser_is_pure_string_literal (cp_token* token)
2744 {
2745 return (token->type == CPP_STRING ||
2746 token->type == CPP_STRING16 ||
2747 token->type == CPP_STRING32 ||
2748 token->type == CPP_WSTRING ||
2749 token->type == CPP_UTF8STRING);
2750 }
2751
2752 /* Returns nonzero if TOKEN is a string literal
2753 of a user-defined string literal. */
2754
2755 static bool
2756 cp_parser_is_string_literal (cp_token* token)
2757 {
2758 return (cp_parser_is_pure_string_literal (token) ||
2759 token->type == CPP_STRING_USERDEF ||
2760 token->type == CPP_STRING16_USERDEF ||
2761 token->type == CPP_STRING32_USERDEF ||
2762 token->type == CPP_WSTRING_USERDEF ||
2763 token->type == CPP_UTF8STRING_USERDEF);
2764 }
2765
2766 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2767
2768 static bool
2769 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2770 {
2771 return token->keyword == keyword;
2772 }
2773
2774 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2775 PRAGMA_NONE. */
2776
2777 static enum pragma_kind
2778 cp_parser_pragma_kind (cp_token *token)
2779 {
2780 if (token->type != CPP_PRAGMA)
2781 return PRAGMA_NONE;
2782 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2783 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2784 }
2785
2786 /* Helper function for cp_parser_error.
2787 Having peeked a token of kind TOK1_KIND that might signify
2788 a conflict marker, peek successor tokens to determine
2789 if we actually do have a conflict marker.
2790 Specifically, we consider a run of 7 '<', '=' or '>' characters
2791 at the start of a line as a conflict marker.
2792 These come through the lexer as three pairs and a single,
2793 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2794 If it returns true, *OUT_LOC is written to with the location/range
2795 of the marker. */
2796
2797 static bool
2798 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2799 location_t *out_loc)
2800 {
2801 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2802 if (token2->type != tok1_kind)
2803 return false;
2804 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2805 if (token3->type != tok1_kind)
2806 return false;
2807 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2808 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2809 return false;
2810
2811 /* It must be at the start of the line. */
2812 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2813 if (LOCATION_COLUMN (start_loc) != 1)
2814 return false;
2815
2816 /* We have a conflict marker. Construct a location of the form:
2817 <<<<<<<
2818 ^~~~~~~
2819 with start == caret, finishing at the end of the marker. */
2820 location_t finish_loc = get_finish (token4->location);
2821 *out_loc = make_location (start_loc, start_loc, finish_loc);
2822
2823 return true;
2824 }
2825
2826 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2827 RT_CLOSE_PAREN. */
2828
2829 static const char *
2830 get_matching_symbol (required_token token_desc)
2831 {
2832 switch (token_desc)
2833 {
2834 default:
2835 gcc_unreachable ();
2836 return "";
2837 case RT_CLOSE_BRACE:
2838 return "{";
2839 case RT_CLOSE_PAREN:
2840 return "(";
2841 }
2842 }
2843
2844 /* Attempt to convert TOKEN_DESC from a required_token to an
2845 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2846
2847 static enum cpp_ttype
2848 get_required_cpp_ttype (required_token token_desc)
2849 {
2850 switch (token_desc)
2851 {
2852 case RT_SEMICOLON:
2853 return CPP_SEMICOLON;
2854 case RT_OPEN_PAREN:
2855 return CPP_OPEN_PAREN;
2856 case RT_CLOSE_BRACE:
2857 return CPP_CLOSE_BRACE;
2858 case RT_OPEN_BRACE:
2859 return CPP_OPEN_BRACE;
2860 case RT_CLOSE_SQUARE:
2861 return CPP_CLOSE_SQUARE;
2862 case RT_OPEN_SQUARE:
2863 return CPP_OPEN_SQUARE;
2864 case RT_COMMA:
2865 return CPP_COMMA;
2866 case RT_COLON:
2867 return CPP_COLON;
2868 case RT_CLOSE_PAREN:
2869 return CPP_CLOSE_PAREN;
2870
2871 default:
2872 /* Use CPP_EOF as a "no completions possible" code. */
2873 return CPP_EOF;
2874 }
2875 }
2876
2877
2878 /* Subroutine of cp_parser_error and cp_parser_required_error.
2879
2880 Issue a diagnostic of the form
2881 FILE:LINE: MESSAGE before TOKEN
2882 where TOKEN is the next token in the input stream. MESSAGE
2883 (specified by the caller) is usually of the form "expected
2884 OTHER-TOKEN".
2885
2886 This bypasses the check for tentative passing, and potentially
2887 adds material needed by cp_parser_required_error.
2888
2889 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2890 suggesting insertion of the missing token.
2891
2892 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2893 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2894 location. */
2895
2896 static void
2897 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2898 required_token missing_token_desc,
2899 location_t matching_location)
2900 {
2901 cp_token *token = cp_lexer_peek_token (parser->lexer);
2902 /* This diagnostic makes more sense if it is tagged to the line
2903 of the token we just peeked at. */
2904 cp_lexer_set_source_position_from_token (token);
2905
2906 if (token->type == CPP_PRAGMA)
2907 {
2908 error_at (token->location,
2909 "%<#pragma%> is not allowed here");
2910 cp_parser_skip_to_pragma_eol (parser, token);
2911 return;
2912 }
2913
2914 /* If this is actually a conflict marker, report it as such. */
2915 if (token->type == CPP_LSHIFT
2916 || token->type == CPP_RSHIFT
2917 || token->type == CPP_EQ_EQ)
2918 {
2919 location_t loc;
2920 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2921 {
2922 error_at (loc, "version control conflict marker in file");
2923 expanded_location token_exploc = expand_location (token->location);
2924 /* Consume tokens until the end of the source line. */
2925 for (;;)
2926 {
2927 cp_lexer_consume_token (parser->lexer);
2928 cp_token *next = cp_lexer_peek_token (parser->lexer);
2929 if (next->type == CPP_EOF)
2930 break;
2931 if (next->location == UNKNOWN_LOCATION
2932 || loc == UNKNOWN_LOCATION)
2933 break;
2934
2935 expanded_location next_exploc = expand_location (next->location);
2936 if (next_exploc.file != token_exploc.file)
2937 break;
2938 if (next_exploc.line != token_exploc.line)
2939 break;
2940 }
2941 return;
2942 }
2943 }
2944
2945 auto_diagnostic_group d;
2946 gcc_rich_location richloc (input_location);
2947
2948 bool added_matching_location = false;
2949
2950 if (missing_token_desc != RT_NONE)
2951 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
2952 {
2953 /* Potentially supply a fix-it hint, suggesting to add the
2954 missing token immediately after the *previous* token.
2955 This may move the primary location within richloc. */
2956 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2957 location_t prev_token_loc = prev_token->location;
2958 maybe_suggest_missing_token_insertion (&richloc, ttype,
2959 prev_token_loc);
2960
2961 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2962 Attempt to consolidate diagnostics by printing it as a
2963 secondary range within the main diagnostic. */
2964 if (matching_location != UNKNOWN_LOCATION)
2965 added_matching_location
2966 = richloc.add_location_if_nearby (matching_location);
2967 }
2968
2969 /* If we were parsing a string-literal and there is an unknown name
2970 token right after, then check to see if that could also have been
2971 a literal string by checking the name against a list of known
2972 standard string literal constants defined in header files. If
2973 there is one, then add that as an hint to the error message. */
2974 name_hint h;
2975 if (token->type == CPP_NAME)
2976 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
2977 if (cp_parser_is_string_literal (prev_token))
2978 {
2979 tree name = token->u.value;
2980 const char *token_name = IDENTIFIER_POINTER (name);
2981 const char *header_hint
2982 = get_cp_stdlib_header_for_string_macro_name (token_name);
2983 if (header_hint != NULL)
2984 h = name_hint (NULL, new suggest_missing_header (token->location,
2985 token_name,
2986 header_hint));
2987 }
2988
2989 /* Actually emit the error. */
2990 c_parse_error (gmsgid,
2991 /* Because c_parser_error does not understand
2992 CPP_KEYWORD, keywords are treated like
2993 identifiers. */
2994 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2995 token->u.value, token->flags, &richloc);
2996
2997 if (missing_token_desc != RT_NONE)
2998 {
2999 /* If we weren't able to consolidate matching_location, then
3000 print it as a secondary diagnostic. */
3001 if (matching_location != UNKNOWN_LOCATION
3002 && !added_matching_location)
3003 inform (matching_location, "to match this %qs",
3004 get_matching_symbol (missing_token_desc));
3005 }
3006 }
3007
3008 /* If not parsing tentatively, issue a diagnostic of the form
3009 FILE:LINE: MESSAGE before TOKEN
3010 where TOKEN is the next token in the input stream. MESSAGE
3011 (specified by the caller) is usually of the form "expected
3012 OTHER-TOKEN". */
3013
3014 static void
3015 cp_parser_error (cp_parser* parser, const char* gmsgid)
3016 {
3017 if (!cp_parser_simulate_error (parser))
3018 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
3019 }
3020
3021 /* Issue an error about name-lookup failing. NAME is the
3022 IDENTIFIER_NODE DECL is the result of
3023 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3024 the thing that we hoped to find. */
3025
3026 static void
3027 cp_parser_name_lookup_error (cp_parser* parser,
3028 tree name,
3029 tree decl,
3030 name_lookup_error desired,
3031 location_t location)
3032 {
3033 /* If name lookup completely failed, tell the user that NAME was not
3034 declared. */
3035 if (decl == error_mark_node)
3036 {
3037 if (parser->scope && parser->scope != global_namespace)
3038 error_at (location, "%<%E::%E%> has not been declared",
3039 parser->scope, name);
3040 else if (parser->scope == global_namespace)
3041 error_at (location, "%<::%E%> has not been declared", name);
3042 else if (parser->object_scope
3043 && !CLASS_TYPE_P (parser->object_scope))
3044 error_at (location, "request for member %qE in non-class type %qT",
3045 name, parser->object_scope);
3046 else if (parser->object_scope)
3047 error_at (location, "%<%T::%E%> has not been declared",
3048 parser->object_scope, name);
3049 else
3050 error_at (location, "%qE has not been declared", name);
3051 }
3052 else if (parser->scope && parser->scope != global_namespace)
3053 {
3054 switch (desired)
3055 {
3056 case NLE_TYPE:
3057 error_at (location, "%<%E::%E%> is not a type",
3058 parser->scope, name);
3059 break;
3060 case NLE_CXX98:
3061 error_at (location, "%<%E::%E%> is not a class or namespace",
3062 parser->scope, name);
3063 break;
3064 case NLE_NOT_CXX98:
3065 error_at (location,
3066 "%<%E::%E%> is not a class, namespace, or enumeration",
3067 parser->scope, name);
3068 break;
3069 default:
3070 gcc_unreachable ();
3071
3072 }
3073 }
3074 else if (parser->scope == global_namespace)
3075 {
3076 switch (desired)
3077 {
3078 case NLE_TYPE:
3079 error_at (location, "%<::%E%> is not a type", name);
3080 break;
3081 case NLE_CXX98:
3082 error_at (location, "%<::%E%> is not a class or namespace", name);
3083 break;
3084 case NLE_NOT_CXX98:
3085 error_at (location,
3086 "%<::%E%> is not a class, namespace, or enumeration",
3087 name);
3088 break;
3089 default:
3090 gcc_unreachable ();
3091 }
3092 }
3093 else
3094 {
3095 switch (desired)
3096 {
3097 case NLE_TYPE:
3098 error_at (location, "%qE is not a type", name);
3099 break;
3100 case NLE_CXX98:
3101 error_at (location, "%qE is not a class or namespace", name);
3102 break;
3103 case NLE_NOT_CXX98:
3104 error_at (location,
3105 "%qE is not a class, namespace, or enumeration", name);
3106 break;
3107 default:
3108 gcc_unreachable ();
3109 }
3110 }
3111 }
3112
3113 /* If we are parsing tentatively, remember that an error has occurred
3114 during this tentative parse. Returns true if the error was
3115 simulated; false if a message should be issued by the caller. */
3116
3117 static bool
3118 cp_parser_simulate_error (cp_parser* parser)
3119 {
3120 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3121 {
3122 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3123 return true;
3124 }
3125 return false;
3126 }
3127
3128 /* This function is called when a type is defined. If type
3129 definitions are forbidden at this point, an error message is
3130 issued. */
3131
3132 static bool
3133 cp_parser_check_type_definition (cp_parser* parser)
3134 {
3135 /* If types are forbidden here, issue a message. */
3136 if (parser->type_definition_forbidden_message)
3137 {
3138 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3139 or %qs in the message need to be interpreted. */
3140 error (parser->type_definition_forbidden_message,
3141 parser->type_definition_forbidden_message_arg);
3142 return false;
3143 }
3144 return true;
3145 }
3146
3147 /* This function is called when the DECLARATOR is processed. The TYPE
3148 was a type defined in the decl-specifiers. If it is invalid to
3149 define a type in the decl-specifiers for DECLARATOR, an error is
3150 issued. TYPE_LOCATION is the location of TYPE and is used
3151 for error reporting. */
3152
3153 static void
3154 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3155 tree type, location_t type_location)
3156 {
3157 /* [dcl.fct] forbids type definitions in return types.
3158 Unfortunately, it's not easy to know whether or not we are
3159 processing a return type until after the fact. */
3160 while (declarator
3161 && (declarator->kind == cdk_pointer
3162 || declarator->kind == cdk_reference
3163 || declarator->kind == cdk_ptrmem))
3164 declarator = declarator->declarator;
3165 if (declarator
3166 && declarator->kind == cdk_function)
3167 {
3168 error_at (type_location,
3169 "new types may not be defined in a return type");
3170 inform (type_location,
3171 "(perhaps a semicolon is missing after the definition of %qT)",
3172 type);
3173 }
3174 }
3175
3176 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3177 "<" in any valid C++ program. If the next token is indeed "<",
3178 issue a message warning the user about what appears to be an
3179 invalid attempt to form a template-id. LOCATION is the location
3180 of the type-specifier (TYPE) */
3181
3182 static void
3183 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3184 tree type,
3185 enum tag_types tag_type,
3186 location_t location)
3187 {
3188 cp_token_position start = 0;
3189
3190 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3191 {
3192 if (TREE_CODE (type) == TYPE_DECL)
3193 type = TREE_TYPE (type);
3194 if (TYPE_P (type) && !template_placeholder_p (type))
3195 error_at (location, "%qT is not a template", type);
3196 else if (identifier_p (type))
3197 {
3198 if (tag_type != none_type)
3199 error_at (location, "%qE is not a class template", type);
3200 else
3201 error_at (location, "%qE is not a template", type);
3202 }
3203 else
3204 error_at (location, "invalid template-id");
3205 /* Remember the location of the invalid "<". */
3206 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3207 start = cp_lexer_token_position (parser->lexer, true);
3208 /* Consume the "<". */
3209 cp_lexer_consume_token (parser->lexer);
3210 /* Parse the template arguments. */
3211 cp_parser_enclosed_template_argument_list (parser);
3212 /* Permanently remove the invalid template arguments so that
3213 this error message is not issued again. */
3214 if (start)
3215 cp_lexer_purge_tokens_after (parser->lexer, start);
3216 }
3217 }
3218
3219 /* If parsing an integral constant-expression, issue an error message
3220 about the fact that THING appeared and return true. Otherwise,
3221 return false. In either case, set
3222 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3223
3224 static bool
3225 cp_parser_non_integral_constant_expression (cp_parser *parser,
3226 non_integral_constant thing)
3227 {
3228 parser->non_integral_constant_expression_p = true;
3229 if (parser->integral_constant_expression_p)
3230 {
3231 if (!parser->allow_non_integral_constant_expression_p)
3232 {
3233 const char *msg = NULL;
3234 switch (thing)
3235 {
3236 case NIC_FLOAT:
3237 pedwarn (input_location, OPT_Wpedantic,
3238 "ISO C++ forbids using a floating-point literal "
3239 "in a constant-expression");
3240 return true;
3241 case NIC_CAST:
3242 error ("a cast to a type other than an integral or "
3243 "enumeration type cannot appear in a "
3244 "constant-expression");
3245 return true;
3246 case NIC_TYPEID:
3247 error ("%<typeid%> operator "
3248 "cannot appear in a constant-expression");
3249 return true;
3250 case NIC_NCC:
3251 error ("non-constant compound literals "
3252 "cannot appear in a constant-expression");
3253 return true;
3254 case NIC_FUNC_CALL:
3255 error ("a function call "
3256 "cannot appear in a constant-expression");
3257 return true;
3258 case NIC_INC:
3259 error ("an increment "
3260 "cannot appear in a constant-expression");
3261 return true;
3262 case NIC_DEC:
3263 error ("an decrement "
3264 "cannot appear in a constant-expression");
3265 return true;
3266 case NIC_ARRAY_REF:
3267 error ("an array reference "
3268 "cannot appear in a constant-expression");
3269 return true;
3270 case NIC_ADDR_LABEL:
3271 error ("the address of a label "
3272 "cannot appear in a constant-expression");
3273 return true;
3274 case NIC_OVERLOADED:
3275 error ("calls to overloaded operators "
3276 "cannot appear in a constant-expression");
3277 return true;
3278 case NIC_ASSIGNMENT:
3279 error ("an assignment cannot appear in a constant-expression");
3280 return true;
3281 case NIC_COMMA:
3282 error ("a comma operator "
3283 "cannot appear in a constant-expression");
3284 return true;
3285 case NIC_CONSTRUCTOR:
3286 error ("a call to a constructor "
3287 "cannot appear in a constant-expression");
3288 return true;
3289 case NIC_TRANSACTION:
3290 error ("a transaction expression "
3291 "cannot appear in a constant-expression");
3292 return true;
3293 case NIC_THIS:
3294 msg = "this";
3295 break;
3296 case NIC_FUNC_NAME:
3297 msg = "__FUNCTION__";
3298 break;
3299 case NIC_PRETTY_FUNC:
3300 msg = "__PRETTY_FUNCTION__";
3301 break;
3302 case NIC_C99_FUNC:
3303 msg = "__func__";
3304 break;
3305 case NIC_VA_ARG:
3306 msg = "va_arg";
3307 break;
3308 case NIC_ARROW:
3309 msg = "->";
3310 break;
3311 case NIC_POINT:
3312 msg = ".";
3313 break;
3314 case NIC_STAR:
3315 msg = "*";
3316 break;
3317 case NIC_ADDR:
3318 msg = "&";
3319 break;
3320 case NIC_PREINCREMENT:
3321 msg = "++";
3322 break;
3323 case NIC_PREDECREMENT:
3324 msg = "--";
3325 break;
3326 case NIC_NEW:
3327 msg = "new";
3328 break;
3329 case NIC_DEL:
3330 msg = "delete";
3331 break;
3332 default:
3333 gcc_unreachable ();
3334 }
3335 if (msg)
3336 error ("%qs cannot appear in a constant-expression", msg);
3337 return true;
3338 }
3339 }
3340 return false;
3341 }
3342
3343 /* Emit a diagnostic for an invalid type name. This function commits
3344 to the current active tentative parse, if any. (Otherwise, the
3345 problematic construct might be encountered again later, resulting
3346 in duplicate error messages.) LOCATION is the location of ID. */
3347
3348 static void
3349 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3350 location_t location)
3351 {
3352 tree decl, ambiguous_decls;
3353 cp_parser_commit_to_tentative_parse (parser);
3354 /* Try to lookup the identifier. */
3355 decl = cp_parser_lookup_name (parser, id, none_type,
3356 /*is_template=*/false,
3357 /*is_namespace=*/false,
3358 /*check_dependency=*/true,
3359 &ambiguous_decls, location);
3360 if (ambiguous_decls)
3361 /* If the lookup was ambiguous, an error will already have
3362 been issued. */
3363 return;
3364 /* If the lookup found a template-name, it means that the user forgot
3365 to specify an argument list. Emit a useful error message. */
3366 if (DECL_TYPE_TEMPLATE_P (decl))
3367 {
3368 auto_diagnostic_group d;
3369 error_at (location,
3370 "invalid use of template-name %qE without an argument list",
3371 decl);
3372 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3373 inform (location, "class template argument deduction is only available "
3374 "with %<-std=c++17%> or %<-std=gnu++17%>");
3375 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3376 }
3377 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3378 error_at (location, "invalid use of destructor %qD as a type", id);
3379 else if (TREE_CODE (decl) == TYPE_DECL)
3380 /* Something like 'unsigned A a;' */
3381 error_at (location, "invalid combination of multiple type-specifiers");
3382 else if (!parser->scope)
3383 {
3384 /* Issue an error message. */
3385 auto_diagnostic_group d;
3386 name_hint hint;
3387 if (TREE_CODE (id) == IDENTIFIER_NODE)
3388 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3389 if (const char *suggestion = hint.suggestion ())
3390 {
3391 gcc_rich_location richloc (location);
3392 richloc.add_fixit_replace (suggestion);
3393 error_at (&richloc,
3394 "%qE does not name a type; did you mean %qs?",
3395 id, suggestion);
3396 }
3397 else
3398 error_at (location, "%qE does not name a type", id);
3399 /* If we're in a template class, it's possible that the user was
3400 referring to a type from a base class. For example:
3401
3402 template <typename T> struct A { typedef T X; };
3403 template <typename T> struct B : public A<T> { X x; };
3404
3405 The user should have said "typename A<T>::X". */
3406 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3407 inform (location, "C++11 %<constexpr%> only available with "
3408 "%<-std=c++11%> or %<-std=gnu++11%>");
3409 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3410 inform (location, "C++11 %<noexcept%> only available with "
3411 "%<-std=c++11%> or %<-std=gnu++11%>");
3412 else if (cxx_dialect < cxx11
3413 && TREE_CODE (id) == IDENTIFIER_NODE
3414 && id_equal (id, "thread_local"))
3415 inform (location, "C++11 %<thread_local%> only available with "
3416 "%<-std=c++11%> or %<-std=gnu++11%>");
3417 else if (cxx_dialect < cxx20 && id == ridpointers[(int)RID_CONSTINIT])
3418 inform (location, "C++20 %<constinit%> only available with "
3419 "%<-std=c++20%> or %<-std=gnu++20%>");
3420 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3421 inform (location, "%<concept%> only available with %<-std=c++20%> or "
3422 "%<-fconcepts%>");
3423 else if (!flag_concepts && id == ridpointers[(int)RID_REQUIRES])
3424 inform (location, "%<requires%> only available with %<-std=c++20%> or "
3425 "%<-fconcepts%>");
3426 else if (processing_template_decl && current_class_type
3427 && TYPE_BINFO (current_class_type))
3428 {
3429 for (tree b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3430 b; b = TREE_CHAIN (b))
3431 {
3432 tree base_type = BINFO_TYPE (b);
3433 if (CLASS_TYPE_P (base_type)
3434 && dependent_type_p (base_type))
3435 {
3436 /* Go from a particular instantiation of the
3437 template (which will have an empty TYPE_FIELDs),
3438 to the main version. */
3439 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3440 for (tree field = TYPE_FIELDS (base_type);
3441 field; field = DECL_CHAIN (field))
3442 if (TREE_CODE (field) == TYPE_DECL
3443 && DECL_NAME (field) == id)
3444 {
3445 inform (location,
3446 "(perhaps %<typename %T::%E%> was intended)",
3447 BINFO_TYPE (b), id);
3448 goto found;
3449 }
3450 }
3451 }
3452 found:;
3453 }
3454 }
3455 /* Here we diagnose qualified-ids where the scope is actually correct,
3456 but the identifier does not resolve to a valid type name. */
3457 else if (parser->scope != error_mark_node)
3458 {
3459 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3460 {
3461 auto_diagnostic_group d;
3462 name_hint hint;
3463 if (decl == error_mark_node)
3464 hint = suggest_alternative_in_explicit_scope (location, id,
3465 parser->scope);
3466 const char *suggestion = hint.suggestion ();
3467 gcc_rich_location richloc (location_of (id));
3468 if (suggestion)
3469 richloc.add_fixit_replace (suggestion);
3470 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3471 {
3472 if (suggestion)
3473 error_at (&richloc,
3474 "%qE in namespace %qE does not name a template"
3475 " type; did you mean %qs?",
3476 id, parser->scope, suggestion);
3477 else
3478 error_at (&richloc,
3479 "%qE in namespace %qE does not name a template type",
3480 id, parser->scope);
3481 }
3482 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3483 {
3484 if (suggestion)
3485 error_at (&richloc,
3486 "%qE in namespace %qE does not name a template"
3487 " type; did you mean %qs?",
3488 TREE_OPERAND (id, 0), parser->scope, suggestion);
3489 else
3490 error_at (&richloc,
3491 "%qE in namespace %qE does not name a template"
3492 " type",
3493 TREE_OPERAND (id, 0), parser->scope);
3494 }
3495 else
3496 {
3497 if (suggestion)
3498 error_at (&richloc,
3499 "%qE in namespace %qE does not name a type"
3500 "; did you mean %qs?",
3501 id, parser->scope, suggestion);
3502 else
3503 error_at (&richloc,
3504 "%qE in namespace %qE does not name a type",
3505 id, parser->scope);
3506 }
3507 if (DECL_P (decl))
3508 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3509 }
3510 else if (CLASS_TYPE_P (parser->scope)
3511 && constructor_name_p (id, parser->scope))
3512 {
3513 /* A<T>::A<T>() */
3514 auto_diagnostic_group d;
3515 error_at (location, "%<%T::%E%> names the constructor, not"
3516 " the type", parser->scope, id);
3517 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3518 error_at (location, "and %qT has no template constructors",
3519 parser->scope);
3520 }
3521 else if (TYPE_P (parser->scope)
3522 && dependent_scope_p (parser->scope))
3523 {
3524 gcc_rich_location richloc (location);
3525 richloc.add_fixit_insert_before ("typename ");
3526 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3527 error_at (&richloc,
3528 "need %<typename%> before %<%T::%D::%E%> because "
3529 "%<%T::%D%> is a dependent scope",
3530 TYPE_CONTEXT (parser->scope),
3531 TYPENAME_TYPE_FULLNAME (parser->scope),
3532 id,
3533 TYPE_CONTEXT (parser->scope),
3534 TYPENAME_TYPE_FULLNAME (parser->scope));
3535 else
3536 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3537 "%qT is a dependent scope",
3538 parser->scope, id, parser->scope);
3539 }
3540 else if (TYPE_P (parser->scope))
3541 {
3542 auto_diagnostic_group d;
3543 if (!COMPLETE_TYPE_P (parser->scope))
3544 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3545 parser->scope);
3546 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3547 error_at (location_of (id),
3548 "%qE in %q#T does not name a template type",
3549 id, parser->scope);
3550 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3551 error_at (location_of (id),
3552 "%qE in %q#T does not name a template type",
3553 TREE_OPERAND (id, 0), parser->scope);
3554 else
3555 error_at (location_of (id),
3556 "%qE in %q#T does not name a type",
3557 id, parser->scope);
3558 if (DECL_P (decl))
3559 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3560 }
3561 else
3562 gcc_unreachable ();
3563 }
3564 }
3565
3566 /* Check for a common situation where a type-name should be present,
3567 but is not, and issue a sensible error message. Returns true if an
3568 invalid type-name was detected.
3569
3570 The situation handled by this function are variable declarations of the
3571 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3572 Usually, `ID' should name a type, but if we got here it means that it
3573 does not. We try to emit the best possible error message depending on
3574 how exactly the id-expression looks like. */
3575
3576 static bool
3577 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3578 {
3579 tree id;
3580 cp_token *token = cp_lexer_peek_token (parser->lexer);
3581
3582 /* Avoid duplicate error about ambiguous lookup. */
3583 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3584 {
3585 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3586 if (next->type == CPP_NAME && next->error_reported)
3587 goto out;
3588 }
3589
3590 cp_parser_parse_tentatively (parser);
3591 id = cp_parser_id_expression (parser,
3592 /*template_keyword_p=*/false,
3593 /*check_dependency_p=*/true,
3594 /*template_p=*/NULL,
3595 /*declarator_p=*/false,
3596 /*optional_p=*/false);
3597 /* If the next token is a (, this is a function with no explicit return
3598 type, i.e. constructor, destructor or conversion op. */
3599 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3600 || TREE_CODE (id) == TYPE_DECL)
3601 {
3602 cp_parser_abort_tentative_parse (parser);
3603 return false;
3604 }
3605 if (!cp_parser_parse_definitely (parser))
3606 return false;
3607
3608 /* Emit a diagnostic for the invalid type. */
3609 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3610 out:
3611 /* If we aren't in the middle of a declarator (i.e. in a
3612 parameter-declaration-clause), skip to the end of the declaration;
3613 there's no point in trying to process it. */
3614 if (!parser->in_declarator_p)
3615 cp_parser_skip_to_end_of_block_or_statement (parser);
3616 return true;
3617 }
3618
3619 /* Consume tokens up to, and including, the next non-nested closing `)'.
3620 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3621 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3622 found an unnested token of that type. */
3623
3624 static int
3625 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3626 bool recovering,
3627 cpp_ttype or_ttype,
3628 bool consume_paren)
3629 {
3630 unsigned paren_depth = 0;
3631 unsigned brace_depth = 0;
3632 unsigned square_depth = 0;
3633 unsigned condop_depth = 0;
3634
3635 if (recovering && or_ttype == CPP_EOF
3636 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3637 return 0;
3638
3639 while (true)
3640 {
3641 cp_token * token = cp_lexer_peek_token (parser->lexer);
3642
3643 /* Have we found what we're looking for before the closing paren? */
3644 if (token->type == or_ttype && or_ttype != CPP_EOF
3645 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3646 return -1;
3647
3648 switch (token->type)
3649 {
3650 case CPP_PRAGMA_EOL:
3651 if (!parser->lexer->in_pragma)
3652 break;
3653 /* FALLTHRU */
3654 case CPP_EOF:
3655 /* If we've run out of tokens, then there is no closing `)'. */
3656 return 0;
3657
3658 /* This is good for lambda expression capture-lists. */
3659 case CPP_OPEN_SQUARE:
3660 ++square_depth;
3661 break;
3662 case CPP_CLOSE_SQUARE:
3663 if (!square_depth--)
3664 return 0;
3665 break;
3666
3667 case CPP_SEMICOLON:
3668 /* This matches the processing in skip_to_end_of_statement. */
3669 if (!brace_depth)
3670 return 0;
3671 break;
3672
3673 case CPP_OPEN_BRACE:
3674 ++brace_depth;
3675 break;
3676 case CPP_CLOSE_BRACE:
3677 if (!brace_depth--)
3678 return 0;
3679 break;
3680
3681 case CPP_OPEN_PAREN:
3682 if (!brace_depth)
3683 ++paren_depth;
3684 break;
3685
3686 case CPP_CLOSE_PAREN:
3687 if (!brace_depth && !paren_depth--)
3688 {
3689 if (consume_paren)
3690 cp_lexer_consume_token (parser->lexer);
3691 return 1;
3692 }
3693 break;
3694
3695 case CPP_QUERY:
3696 if (!brace_depth && !paren_depth && !square_depth)
3697 ++condop_depth;
3698 break;
3699
3700 case CPP_COLON:
3701 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3702 condop_depth--;
3703 break;
3704
3705 case CPP_PRAGMA:
3706 /* We fell into a pragma. Skip it, and continue. */
3707 cp_parser_skip_to_pragma_eol (parser, recovering ? token : nullptr);
3708 continue;
3709
3710 default:
3711 break;
3712 }
3713
3714 /* Consume the token. */
3715 cp_lexer_consume_token (parser->lexer);
3716 }
3717 }
3718
3719 /* Consume tokens up to, and including, the next non-nested closing `)'.
3720 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3721 are doing error recovery. Returns -1 if OR_COMMA is true and we
3722 found an unnested token of that type. */
3723
3724 static int
3725 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3726 bool recovering,
3727 bool or_comma,
3728 bool consume_paren)
3729 {
3730 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3731 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3732 ttype, consume_paren);
3733 }
3734
3735 /* Consume tokens until we reach the end of the current statement.
3736 Normally, that will be just before consuming a `;'. However, if a
3737 non-nested `}' comes first, then we stop before consuming that. */
3738
3739 static void
3740 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3741 {
3742 unsigned nesting_depth = 0;
3743
3744 /* Unwind generic function template scope if necessary. */
3745 if (parser->fully_implicit_function_template_p)
3746 abort_fully_implicit_template (parser);
3747
3748 while (true)
3749 {
3750 cp_token *token = cp_lexer_peek_token (parser->lexer);
3751
3752 switch (token->type)
3753 {
3754 case CPP_PRAGMA_EOL:
3755 if (!parser->lexer->in_pragma)
3756 break;
3757 /* FALLTHRU */
3758 case CPP_EOF:
3759 /* If we've run out of tokens, stop. */
3760 return;
3761
3762 case CPP_SEMICOLON:
3763 /* If the next token is a `;', we have reached the end of the
3764 statement. */
3765 if (!nesting_depth)
3766 return;
3767 break;
3768
3769 case CPP_CLOSE_BRACE:
3770 /* If this is a non-nested '}', stop before consuming it.
3771 That way, when confronted with something like:
3772
3773 { 3 + }
3774
3775 we stop before consuming the closing '}', even though we
3776 have not yet reached a `;'. */
3777 if (nesting_depth == 0)
3778 return;
3779
3780 /* If it is the closing '}' for a block that we have
3781 scanned, stop -- but only after consuming the token.
3782 That way given:
3783
3784 void f g () { ... }
3785 typedef int I;
3786
3787 we will stop after the body of the erroneously declared
3788 function, but before consuming the following `typedef'
3789 declaration. */
3790 if (--nesting_depth == 0)
3791 {
3792 cp_lexer_consume_token (parser->lexer);
3793 return;
3794 }
3795 break;
3796
3797 case CPP_OPEN_BRACE:
3798 ++nesting_depth;
3799 break;
3800
3801 case CPP_PRAGMA:
3802 /* We fell into a pragma. Skip it, and continue or return. */
3803 cp_parser_skip_to_pragma_eol (parser, token);
3804 if (!nesting_depth)
3805 return;
3806 continue;
3807
3808 default:
3809 break;
3810 }
3811
3812 /* Consume the token. */
3813 cp_lexer_consume_token (parser->lexer);
3814 }
3815 }
3816
3817 /* This function is called at the end of a statement or declaration.
3818 If the next token is a semicolon, it is consumed; otherwise, error
3819 recovery is attempted. */
3820
3821 static void
3822 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3823 {
3824 /* Look for the trailing `;'. */
3825 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3826 {
3827 /* If there is additional (erroneous) input, skip to the end of
3828 the statement. */
3829 cp_parser_skip_to_end_of_statement (parser);
3830 /* If the next token is now a `;', consume it. */
3831 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3832 cp_lexer_consume_token (parser->lexer);
3833 }
3834 }
3835
3836 /* Skip tokens until we have consumed an entire block, or until we
3837 have consumed a non-nested `;'. */
3838
3839 static void
3840 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3841 {
3842 int nesting_depth = 0;
3843
3844 /* Unwind generic function template scope if necessary. */
3845 if (parser->fully_implicit_function_template_p)
3846 abort_fully_implicit_template (parser);
3847
3848 while (nesting_depth >= 0)
3849 {
3850 cp_token *token = cp_lexer_peek_token (parser->lexer);
3851
3852 switch (token->type)
3853 {
3854 case CPP_PRAGMA_EOL:
3855 if (!parser->lexer->in_pragma)
3856 break;
3857 /* FALLTHRU */
3858 case CPP_EOF:
3859 /* If we've run out of tokens, stop. */
3860 return;
3861
3862 case CPP_SEMICOLON:
3863 /* Stop if this is an unnested ';'. */
3864 if (!nesting_depth)
3865 nesting_depth = -1;
3866 break;
3867
3868 case CPP_CLOSE_BRACE:
3869 /* Stop if this is an unnested '}', or closes the outermost
3870 nesting level. */
3871 nesting_depth--;
3872 if (nesting_depth < 0)
3873 return;
3874 if (!nesting_depth)
3875 nesting_depth = -1;
3876 break;
3877
3878 case CPP_OPEN_BRACE:
3879 /* Nest. */
3880 nesting_depth++;
3881 break;
3882
3883 case CPP_PRAGMA:
3884 /* Skip it, and continue or return. */
3885 cp_parser_skip_to_pragma_eol (parser, token);
3886 if (!nesting_depth)
3887 return;
3888 continue;
3889
3890 default:
3891 break;
3892 }
3893
3894 /* Consume the token. */
3895 cp_lexer_consume_token (parser->lexer);
3896 }
3897 }
3898
3899 /* Skip tokens until a non-nested closing curly brace is the next
3900 token, or there are no more tokens. Return true in the first case,
3901 false otherwise. */
3902
3903 static bool
3904 cp_parser_skip_to_closing_brace (cp_parser *parser)
3905 {
3906 unsigned nesting_depth = 0;
3907
3908 while (true)
3909 {
3910 cp_token *token = cp_lexer_peek_token (parser->lexer);
3911
3912 switch (token->type)
3913 {
3914 case CPP_PRAGMA_EOL:
3915 if (!parser->lexer->in_pragma)
3916 break;
3917 /* FALLTHRU */
3918 case CPP_EOF:
3919 /* If we've run out of tokens, stop. */
3920 return false;
3921
3922 case CPP_CLOSE_BRACE:
3923 /* If the next token is a non-nested `}', then we have reached
3924 the end of the current block. */
3925 if (nesting_depth-- == 0)
3926 return true;
3927 break;
3928
3929 case CPP_OPEN_BRACE:
3930 /* If it the next token is a `{', then we are entering a new
3931 block. Consume the entire block. */
3932 ++nesting_depth;
3933 break;
3934
3935 default:
3936 break;
3937 }
3938
3939 /* Consume the token. */
3940 cp_lexer_consume_token (parser->lexer);
3941 }
3942 }
3943
3944 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3945 parameter is the PRAGMA token, allowing us to purge the entire pragma
3946 sequence. PRAGMA_TOK can be NULL, if we're speculatively scanning
3947 forwards (not error recovery). */
3948
3949 static void
3950 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3951 {
3952 cp_token *token;
3953
3954 do
3955 {
3956 /* The preprocessor makes sure that a PRAGMA_EOL token appears
3957 before an EOF token, even when the EOF is on the pragma line.
3958 We should never get here without being inside a deferred
3959 pragma. */
3960 gcc_checking_assert (cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
3961 token = cp_lexer_consume_token (parser->lexer);
3962 }
3963 while (token->type != CPP_PRAGMA_EOL);
3964
3965 if (pragma_tok)
3966 {
3967 /* Ensure that the pragma is not parsed again. */
3968 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3969 parser->lexer->in_pragma = false;
3970 }
3971 }
3972
3973 /* Require pragma end of line, resyncing with it as necessary. The
3974 arguments are as for cp_parser_skip_to_pragma_eol. */
3975
3976 static void
3977 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3978 {
3979 parser->lexer->in_pragma = false;
3980 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3981 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3982 }
3983
3984 /* This is a simple wrapper around make_typename_type. When the id is
3985 an unresolved identifier node, we can provide a superior diagnostic
3986 using cp_parser_diagnose_invalid_type_name. */
3987
3988 static tree
3989 cp_parser_make_typename_type (cp_parser *parser, tree id,
3990 location_t id_location)
3991 {
3992 tree result;
3993 if (identifier_p (id))
3994 {
3995 result = make_typename_type (parser->scope, id, typename_type,
3996 /*complain=*/tf_none);
3997 if (result == error_mark_node)
3998 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3999 return result;
4000 }
4001 return make_typename_type (parser->scope, id, typename_type, tf_error);
4002 }
4003
4004 /* This is a wrapper around the
4005 make_{pointer,ptrmem,reference}_declarator functions that decides
4006 which one to call based on the CODE and CLASS_TYPE arguments. The
4007 CODE argument should be one of the values returned by
4008 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
4009 appertain to the pointer or reference. */
4010
4011 static cp_declarator *
4012 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
4013 cp_cv_quals cv_qualifiers,
4014 cp_declarator *target,
4015 tree attributes)
4016 {
4017 if (code == ERROR_MARK || target == cp_error_declarator)
4018 return cp_error_declarator;
4019
4020 if (code == INDIRECT_REF)
4021 if (class_type == NULL_TREE)
4022 return make_pointer_declarator (cv_qualifiers, target, attributes);
4023 else
4024 return make_ptrmem_declarator (cv_qualifiers, class_type,
4025 target, attributes);
4026 else if (code == ADDR_EXPR && class_type == NULL_TREE)
4027 return make_reference_declarator (cv_qualifiers, target,
4028 false, attributes);
4029 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
4030 return make_reference_declarator (cv_qualifiers, target,
4031 true, attributes);
4032 gcc_unreachable ();
4033 }
4034
4035 /* Create a new C++ parser. */
4036
4037 static cp_parser *
4038 cp_parser_new (cp_lexer *lexer)
4039 {
4040 /* Initialize the binops_by_token so that we can get the tree
4041 directly from the token. */
4042 for (unsigned i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
4043 binops_by_token[binops[i].token_type] = binops[i];
4044
4045 cp_parser *parser = ggc_cleared_alloc<cp_parser> ();
4046 parser->lexer = lexer;
4047 parser->context = cp_parser_context_new (NULL);
4048
4049 /* For now, we always accept GNU extensions. */
4050 parser->allow_gnu_extensions_p = 1;
4051
4052 /* The `>' token is a greater-than operator, not the end of a
4053 template-id. */
4054 parser->greater_than_is_operator_p = true;
4055
4056 parser->default_arg_ok_p = true;
4057
4058 /* We are not parsing a constant-expression. */
4059 parser->integral_constant_expression_p = false;
4060 parser->allow_non_integral_constant_expression_p = false;
4061 parser->non_integral_constant_expression_p = false;
4062
4063 /* Local variable names are not forbidden. */
4064 parser->local_variables_forbidden_p = 0;
4065
4066 /* We are not processing an `extern "C"' declaration. */
4067 parser->in_unbraced_linkage_specification_p = false;
4068
4069 /* We are not processing a declarator. */
4070 parser->in_declarator_p = false;
4071
4072 /* We are not processing a template-argument-list. */
4073 parser->in_template_argument_list_p = false;
4074
4075 /* We are not in an iteration statement. */
4076 parser->in_statement = 0;
4077
4078 /* We are not in a switch statement. */
4079 parser->in_switch_statement_p = false;
4080
4081 /* We are not parsing a type-id inside an expression. */
4082 parser->in_type_id_in_expr_p = false;
4083
4084 /* String literals should be translated to the execution character set. */
4085 parser->translate_strings_p = true;
4086
4087 /* We are not parsing a function body. */
4088 parser->in_function_body = false;
4089
4090 /* We can correct until told otherwise. */
4091 parser->colon_corrects_to_scope_p = true;
4092
4093 /* The unparsed function queue is empty. */
4094 push_unparsed_function_queues (parser);
4095
4096 /* There are no classes being defined. */
4097 parser->num_classes_being_defined = 0;
4098
4099 /* No template parameters apply. */
4100 parser->num_template_parameter_lists = 0;
4101
4102 /* Special parsing data structures. */
4103 parser->omp_declare_simd = NULL;
4104 parser->oacc_routine = NULL;
4105
4106 /* Not declaring an implicit function template. */
4107 parser->auto_is_implicit_function_template_parm_p = false;
4108 parser->fully_implicit_function_template_p = false;
4109 parser->implicit_template_parms = 0;
4110 parser->implicit_template_scope = 0;
4111
4112 /* Allow constrained-type-specifiers. */
4113 parser->prevent_constrained_type_specifiers = 0;
4114
4115 /* We haven't yet seen an 'extern "C"'. */
4116 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4117
4118 return parser;
4119 }
4120
4121 /* Create a cp_lexer structure which will emit the tokens in CACHE
4122 and push it onto the parser's lexer stack. This is used for delayed
4123 parsing of in-class method bodies and default arguments, and should
4124 not be confused with tentative parsing. */
4125 static void
4126 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4127 {
4128 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4129 lexer->next = parser->lexer;
4130 parser->lexer = lexer;
4131
4132 /* Move the current source position to that of the first token in the
4133 new lexer. */
4134 cp_lexer_set_source_position_from_token (lexer->next_token);
4135 }
4136
4137 /* Pop the top lexer off the parser stack. This is never used for the
4138 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4139 static void
4140 cp_parser_pop_lexer (cp_parser *parser)
4141 {
4142 cp_lexer *lexer = parser->lexer;
4143 parser->lexer = lexer->next;
4144 cp_lexer_destroy (lexer);
4145
4146 /* Put the current source position back where it was before this
4147 lexer was pushed. */
4148 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4149 }
4150
4151 /* Lexical conventions [gram.lex] */
4152
4153 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4154 identifier. */
4155
4156 static cp_expr
4157 cp_parser_identifier (cp_parser* parser)
4158 {
4159 cp_token *token;
4160
4161 /* Look for the identifier. */
4162 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4163 /* Return the value. */
4164 if (token)
4165 return cp_expr (token->u.value, token->location);
4166 else
4167 return error_mark_node;
4168 }
4169
4170 /* Parse a sequence of adjacent string constants. Returns a
4171 TREE_STRING representing the combined, nul-terminated string
4172 constant. If TRANSLATE is true, translate the string to the
4173 execution character set. If WIDE_OK is true, a wide string is
4174 invalid here.
4175
4176 C++98 [lex.string] says that if a narrow string literal token is
4177 adjacent to a wide string literal token, the behavior is undefined.
4178 However, C99 6.4.5p4 says that this results in a wide string literal.
4179 We follow C99 here, for consistency with the C front end.
4180
4181 This code is largely lifted from lex_string() in c-lex.c.
4182
4183 FUTURE: ObjC++ will need to handle @-strings here. */
4184 static cp_expr
4185 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4186 bool lookup_udlit = true)
4187 {
4188 tree value;
4189 size_t count;
4190 struct obstack str_ob;
4191 struct obstack loc_ob;
4192 cpp_string str, istr, *strs;
4193 cp_token *tok;
4194 enum cpp_ttype type, curr_type;
4195 int have_suffix_p = 0;
4196 tree string_tree;
4197 tree suffix_id = NULL_TREE;
4198 bool curr_tok_is_userdef_p = false;
4199
4200 tok = cp_lexer_peek_token (parser->lexer);
4201 if (!cp_parser_is_string_literal (tok))
4202 {
4203 cp_parser_error (parser, "expected string-literal");
4204 return error_mark_node;
4205 }
4206
4207 location_t loc = tok->location;
4208
4209 if (cpp_userdef_string_p (tok->type))
4210 {
4211 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4212 curr_type = cpp_userdef_string_remove_type (tok->type);
4213 curr_tok_is_userdef_p = true;
4214 }
4215 else
4216 {
4217 string_tree = tok->u.value;
4218 curr_type = tok->type;
4219 }
4220 type = curr_type;
4221
4222 /* Try to avoid the overhead of creating and destroying an obstack
4223 for the common case of just one string. */
4224 if (!cp_parser_is_string_literal
4225 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4226 {
4227 cp_lexer_consume_token (parser->lexer);
4228
4229 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4230 str.len = TREE_STRING_LENGTH (string_tree);
4231 count = 1;
4232
4233 if (curr_tok_is_userdef_p)
4234 {
4235 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4236 have_suffix_p = 1;
4237 curr_type = cpp_userdef_string_remove_type (tok->type);
4238 }
4239 else
4240 curr_type = tok->type;
4241
4242 strs = &str;
4243 }
4244 else
4245 {
4246 location_t last_tok_loc = tok->location;
4247 gcc_obstack_init (&str_ob);
4248 gcc_obstack_init (&loc_ob);
4249 count = 0;
4250
4251 do
4252 {
4253 cp_lexer_consume_token (parser->lexer);
4254 count++;
4255 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4256 str.len = TREE_STRING_LENGTH (string_tree);
4257
4258 if (curr_tok_is_userdef_p)
4259 {
4260 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4261 if (have_suffix_p == 0)
4262 {
4263 suffix_id = curr_suffix_id;
4264 have_suffix_p = 1;
4265 }
4266 else if (have_suffix_p == 1
4267 && curr_suffix_id != suffix_id)
4268 {
4269 error ("inconsistent user-defined literal suffixes"
4270 " %qD and %qD in string literal",
4271 suffix_id, curr_suffix_id);
4272 have_suffix_p = -1;
4273 }
4274 curr_type = cpp_userdef_string_remove_type (tok->type);
4275 }
4276 else
4277 curr_type = tok->type;
4278
4279 if (type != curr_type)
4280 {
4281 if (type == CPP_STRING)
4282 type = curr_type;
4283 else if (curr_type != CPP_STRING)
4284 {
4285 rich_location rich_loc (line_table, tok->location);
4286 rich_loc.add_range (last_tok_loc);
4287 error_at (&rich_loc,
4288 "unsupported non-standard concatenation "
4289 "of string literals");
4290 }
4291 }
4292
4293 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4294 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4295
4296 last_tok_loc = tok->location;
4297
4298 tok = cp_lexer_peek_token (parser->lexer);
4299 if (cpp_userdef_string_p (tok->type))
4300 {
4301 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4302 curr_type = cpp_userdef_string_remove_type (tok->type);
4303 curr_tok_is_userdef_p = true;
4304 }
4305 else
4306 {
4307 string_tree = tok->u.value;
4308 curr_type = tok->type;
4309 curr_tok_is_userdef_p = false;
4310 }
4311 }
4312 while (cp_parser_is_string_literal (tok));
4313
4314 /* A string literal built by concatenation has its caret=start at
4315 the start of the initial string, and its finish at the finish of
4316 the final string literal. */
4317 loc = make_location (loc, loc, get_finish (last_tok_loc));
4318
4319 strs = (cpp_string *) obstack_finish (&str_ob);
4320 }
4321
4322 if (type != CPP_STRING && !wide_ok)
4323 {
4324 cp_parser_error (parser, "a wide string is invalid in this context");
4325 type = CPP_STRING;
4326 }
4327
4328 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4329 (parse_in, strs, count, &istr, type))
4330 {
4331 value = build_string (istr.len, (const char *)istr.text);
4332 free (CONST_CAST (unsigned char *, istr.text));
4333 if (count > 1)
4334 {
4335 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4336 gcc_assert (g_string_concat_db);
4337 g_string_concat_db->record_string_concatenation (count, locs);
4338 }
4339
4340 switch (type)
4341 {
4342 default:
4343 case CPP_STRING:
4344 TREE_TYPE (value) = char_array_type_node;
4345 break;
4346 case CPP_UTF8STRING:
4347 if (flag_char8_t)
4348 TREE_TYPE (value) = char8_array_type_node;
4349 else
4350 TREE_TYPE (value) = char_array_type_node;
4351 break;
4352 case CPP_STRING16:
4353 TREE_TYPE (value) = char16_array_type_node;
4354 break;
4355 case CPP_STRING32:
4356 TREE_TYPE (value) = char32_array_type_node;
4357 break;
4358 case CPP_WSTRING:
4359 TREE_TYPE (value) = wchar_array_type_node;
4360 break;
4361 }
4362
4363 value = fix_string_type (value);
4364
4365 if (have_suffix_p)
4366 {
4367 tree literal = build_userdef_literal (suffix_id, value,
4368 OT_NONE, NULL_TREE);
4369 if (lookup_udlit)
4370 value = cp_parser_userdef_string_literal (literal);
4371 else
4372 value = literal;
4373 }
4374 }
4375 else
4376 /* cpp_interpret_string has issued an error. */
4377 value = error_mark_node;
4378
4379 if (count > 1)
4380 {
4381 obstack_free (&str_ob, 0);
4382 obstack_free (&loc_ob, 0);
4383 }
4384
4385 return cp_expr (value, loc);
4386 }
4387
4388 /* Look up a literal operator with the name and the exact arguments. */
4389
4390 static tree
4391 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4392 {
4393 tree decl = lookup_name (name);
4394 if (!decl || !is_overloaded_fn (decl))
4395 return error_mark_node;
4396
4397 for (lkp_iterator iter (decl); iter; ++iter)
4398 {
4399 tree fn = *iter;
4400
4401 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4402 {
4403 unsigned int ix;
4404 bool found = true;
4405
4406 for (ix = 0;
4407 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4408 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4409 {
4410 tree tparm = TREE_VALUE (parmtypes);
4411 tree targ = TREE_TYPE ((*args)[ix]);
4412 bool ptr = TYPE_PTR_P (tparm);
4413 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4414 if ((ptr || arr || !same_type_p (tparm, targ))
4415 && (!ptr || !arr
4416 || !same_type_p (TREE_TYPE (tparm),
4417 TREE_TYPE (targ))))
4418 found = false;
4419 }
4420
4421 if (found
4422 && ix == vec_safe_length (args)
4423 /* May be this should be sufficient_parms_p instead,
4424 depending on how exactly should user-defined literals
4425 work in presence of default arguments on the literal
4426 operator parameters. */
4427 && parmtypes == void_list_node)
4428 return decl;
4429 }
4430 }
4431
4432 return error_mark_node;
4433 }
4434
4435 /* Parse a user-defined char constant. Returns a call to a user-defined
4436 literal operator taking the character as an argument. */
4437
4438 static cp_expr
4439 cp_parser_userdef_char_literal (cp_parser *parser)
4440 {
4441 cp_token *token = cp_lexer_consume_token (parser->lexer);
4442 tree literal = token->u.value;
4443 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4444 tree value = USERDEF_LITERAL_VALUE (literal);
4445 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4446 tree decl, result;
4447
4448 /* Build up a call to the user-defined operator */
4449 /* Lookup the name we got back from the id-expression. */
4450 releasing_vec args;
4451 vec_safe_push (args, value);
4452 decl = lookup_literal_operator (name, args);
4453 if (!decl || decl == error_mark_node)
4454 {
4455 error ("unable to find character literal operator %qD with %qT argument",
4456 name, TREE_TYPE (value));
4457 return error_mark_node;
4458 }
4459 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4460 return result;
4461 }
4462
4463 /* A subroutine of cp_parser_userdef_numeric_literal to
4464 create a char... template parameter pack from a string node. */
4465
4466 static tree
4467 make_char_string_pack (tree value)
4468 {
4469 tree charvec;
4470 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4471 const char *str = TREE_STRING_POINTER (value);
4472 int i, len = TREE_STRING_LENGTH (value) - 1;
4473 tree argvec = make_tree_vec (1);
4474
4475 /* Fill in CHARVEC with all of the parameters. */
4476 charvec = make_tree_vec (len);
4477 for (i = 0; i < len; ++i)
4478 {
4479 unsigned char s[3] = { '\'', str[i], '\'' };
4480 cpp_string in = { 3, s };
4481 cpp_string out = { 0, 0 };
4482 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4483 return NULL_TREE;
4484 gcc_assert (out.len == 2);
4485 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4486 out.text[0]);
4487 }
4488
4489 /* Build the argument packs. */
4490 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4491
4492 TREE_VEC_ELT (argvec, 0) = argpack;
4493
4494 return argvec;
4495 }
4496
4497 /* A subroutine of cp_parser_userdef_numeric_literal to
4498 create a char... template parameter pack from a string node. */
4499
4500 static tree
4501 make_string_pack (tree value)
4502 {
4503 tree charvec;
4504 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4505 const unsigned char *str
4506 = (const unsigned char *) TREE_STRING_POINTER (value);
4507 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4508 int len = TREE_STRING_LENGTH (value) / sz - 1;
4509 tree argvec = make_tree_vec (2);
4510
4511 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4512 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4513
4514 /* First template parm is character type. */
4515 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4516
4517 /* Fill in CHARVEC with all of the parameters. */
4518 charvec = make_tree_vec (len);
4519 for (int i = 0; i < len; ++i)
4520 TREE_VEC_ELT (charvec, i)
4521 = double_int_to_tree (str_char_type_node,
4522 double_int::from_buffer (str + i * sz, sz));
4523
4524 /* Build the argument packs. */
4525 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4526
4527 TREE_VEC_ELT (argvec, 1) = argpack;
4528
4529 return argvec;
4530 }
4531
4532 /* Parse a user-defined numeric constant. returns a call to a user-defined
4533 literal operator. */
4534
4535 static cp_expr
4536 cp_parser_userdef_numeric_literal (cp_parser *parser)
4537 {
4538 cp_token *token = cp_lexer_consume_token (parser->lexer);
4539 tree literal = token->u.value;
4540 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4541 tree value = USERDEF_LITERAL_VALUE (literal);
4542 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4543 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4544 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4545 tree decl, result;
4546
4547 /* Look for a literal operator taking the exact type of numeric argument
4548 as the literal value. */
4549 releasing_vec args;
4550 vec_safe_push (args, value);
4551 decl = lookup_literal_operator (name, args);
4552 if (decl && decl != error_mark_node)
4553 {
4554 result = finish_call_expr (decl, &args, false, true,
4555 tf_warning_or_error);
4556
4557 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4558 {
4559 warning_at (token->location, OPT_Woverflow,
4560 "integer literal exceeds range of %qT type",
4561 long_long_unsigned_type_node);
4562 }
4563 else
4564 {
4565 if (overflow > 0)
4566 warning_at (token->location, OPT_Woverflow,
4567 "floating literal exceeds range of %qT type",
4568 long_double_type_node);
4569 else if (overflow < 0)
4570 warning_at (token->location, OPT_Woverflow,
4571 "floating literal truncated to zero");
4572 }
4573
4574 return result;
4575 }
4576
4577 /* If the numeric argument didn't work, look for a raw literal
4578 operator taking a const char* argument consisting of the number
4579 in string format. */
4580 args->truncate (0);
4581 vec_safe_push (args, num_string);
4582 decl = lookup_literal_operator (name, args);
4583 if (decl && decl != error_mark_node)
4584 {
4585 result = finish_call_expr (decl, &args, false, true,
4586 tf_warning_or_error);
4587 return result;
4588 }
4589
4590 /* If the raw literal didn't work, look for a non-type template
4591 function with parameter pack char.... Call the function with
4592 template parameter characters representing the number. */
4593 args->truncate (0);
4594 decl = lookup_literal_operator (name, args);
4595 if (decl && decl != error_mark_node)
4596 {
4597 tree tmpl_args = make_char_string_pack (num_string);
4598 if (tmpl_args == NULL_TREE)
4599 {
4600 error ("failed to translate literal to execution character set %qT",
4601 num_string);
4602 return error_mark_node;
4603 }
4604 decl = lookup_template_function (decl, tmpl_args);
4605 result = finish_call_expr (decl, &args, false, true,
4606 tf_warning_or_error);
4607 return result;
4608 }
4609
4610 /* In C++14 the standard library defines complex number suffixes that
4611 conflict with GNU extensions. Prefer them if <complex> is #included. */
4612 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4613 bool i14 = (cxx_dialect > cxx11
4614 && (id_equal (suffix_id, "i")
4615 || id_equal (suffix_id, "if")
4616 || id_equal (suffix_id, "il")));
4617 diagnostic_t kind = DK_ERROR;
4618 int opt = 0;
4619
4620 if (i14 && ext)
4621 {
4622 tree cxlit = lookup_qualified_name (std_node, "complex_literals",
4623 LOOK_want::NORMAL, false);
4624 if (cxlit == error_mark_node)
4625 {
4626 /* No <complex>, so pedwarn and use GNU semantics. */
4627 kind = DK_PEDWARN;
4628 opt = OPT_Wpedantic;
4629 }
4630 }
4631
4632 bool complained
4633 = emit_diagnostic (kind, input_location, opt,
4634 "unable to find numeric literal operator %qD", name);
4635
4636 if (!complained)
4637 /* Don't inform either. */;
4638 else if (i14)
4639 {
4640 inform (token->location, "add %<using namespace std::complex_literals%> "
4641 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4642 "suffixes");
4643 if (ext)
4644 inform (token->location, "or use %<j%> instead of %<i%> for the "
4645 "GNU built-in suffix");
4646 }
4647 else if (!ext)
4648 inform (token->location, "use %<-fext-numeric-literals%> "
4649 "to enable more built-in suffixes");
4650
4651 if (kind == DK_ERROR)
4652 value = error_mark_node;
4653 else
4654 {
4655 /* Use the built-in semantics. */
4656 tree type;
4657 if (id_equal (suffix_id, "i"))
4658 {
4659 if (TREE_CODE (value) == INTEGER_CST)
4660 type = integer_type_node;
4661 else
4662 type = double_type_node;
4663 }
4664 else if (id_equal (suffix_id, "if"))
4665 type = float_type_node;
4666 else /* if (id_equal (suffix_id, "il")) */
4667 type = long_double_type_node;
4668
4669 value = build_complex (build_complex_type (type),
4670 fold_convert (type, integer_zero_node),
4671 fold_convert (type, value));
4672 }
4673
4674 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4675 /* Avoid repeated diagnostics. */
4676 token->u.value = value;
4677 return value;
4678 }
4679
4680 /* Parse a user-defined string constant. Returns a call to a user-defined
4681 literal operator taking a character pointer and the length of the string
4682 as arguments. */
4683
4684 static tree
4685 cp_parser_userdef_string_literal (tree literal)
4686 {
4687 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4688 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4689 tree value = USERDEF_LITERAL_VALUE (literal);
4690 int len = TREE_STRING_LENGTH (value)
4691 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4692 tree decl;
4693
4694 /* Build up a call to the user-defined operator. */
4695 /* Lookup the name we got back from the id-expression. */
4696 releasing_vec args;
4697 vec_safe_push (args, value);
4698 vec_safe_push (args, build_int_cst (size_type_node, len));
4699 decl = lookup_literal_operator (name, args);
4700
4701 if (decl && decl != error_mark_node)
4702 return finish_call_expr (decl, &args, false, true,
4703 tf_warning_or_error);
4704
4705 /* Look for a suitable template function, either (C++20) with a single
4706 parameter of class type, or (N3599) with typename parameter CharT and
4707 parameter pack CharT... */
4708 args->truncate (0);
4709 decl = lookup_literal_operator (name, args);
4710 if (decl && decl != error_mark_node)
4711 {
4712 /* Use resolve_nondeduced_context to try to choose one form of template
4713 or the other. */
4714 tree tmpl_args = make_tree_vec (1);
4715 TREE_VEC_ELT (tmpl_args, 0) = value;
4716 decl = lookup_template_function (decl, tmpl_args);
4717 tree res = resolve_nondeduced_context (decl, tf_none);
4718 if (DECL_P (res))
4719 decl = res;
4720 else
4721 {
4722 TREE_OPERAND (decl, 1) = make_string_pack (value);
4723 res = resolve_nondeduced_context (decl, tf_none);
4724 if (DECL_P (res))
4725 decl = res;
4726 }
4727 if (!DECL_P (decl) && cxx_dialect > cxx17)
4728 TREE_OPERAND (decl, 1) = tmpl_args;
4729 return finish_call_expr (decl, &args, false, true,
4730 tf_warning_or_error);
4731 }
4732
4733 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4734 name, TREE_TYPE (value), size_type_node);
4735 return error_mark_node;
4736 }
4737
4738
4739 /* Basic concepts [gram.basic] */
4740
4741 /* Parse a translation-unit.
4742
4743 translation-unit:
4744 declaration-seq [opt] */
4745
4746 static void
4747 cp_parser_translation_unit (cp_parser* parser)
4748 {
4749 gcc_checking_assert (!cp_error_declarator);
4750
4751 /* Create the declarator obstack. */
4752 gcc_obstack_init (&declarator_obstack);
4753 /* Create the error declarator. */
4754 cp_error_declarator = make_declarator (cdk_error);
4755 /* Create the empty parameter list. */
4756 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4757 UNKNOWN_LOCATION);
4758 /* Remember where the base of the declarator obstack lies. */
4759 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4760
4761 push_deferring_access_checks (flag_access_control
4762 ? dk_no_deferred : dk_no_check);
4763
4764 bool implicit_extern_c = false;
4765
4766 /* Parse until EOF. */
4767 for (;;)
4768 {
4769 cp_token *token = cp_lexer_peek_token (parser->lexer);
4770
4771 /* If we're entering or exiting a region that's implicitly
4772 extern "C", modify the lang context appropriately. This is
4773 so horrible. Please die. */
4774 if (implicit_extern_c
4775 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4776 {
4777 implicit_extern_c = !implicit_extern_c;
4778 if (implicit_extern_c)
4779 push_lang_context (lang_name_c);
4780 else
4781 pop_lang_context ();
4782 }
4783
4784 if (token->type == CPP_EOF)
4785 break;
4786
4787 if (token->type == CPP_CLOSE_BRACE)
4788 {
4789 cp_parser_error (parser, "expected declaration");
4790 cp_lexer_consume_token (parser->lexer);
4791 /* If the next token is now a `;', consume it. */
4792 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4793 cp_lexer_consume_token (parser->lexer);
4794 }
4795 else
4796 cp_parser_toplevel_declaration (parser);
4797 }
4798
4799 /* Get rid of the token array; we don't need it any more. */
4800 cp_lexer_destroy (parser->lexer);
4801 parser->lexer = NULL;
4802
4803 /* The EOF should have reset this. */
4804 gcc_checking_assert (!implicit_extern_c);
4805
4806 /* Make sure the declarator obstack was fully cleaned up. */
4807 gcc_assert (obstack_next_free (&declarator_obstack)
4808 == declarator_obstack_base);
4809 }
4810
4811 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4812 decltype context. */
4813
4814 static inline tsubst_flags_t
4815 complain_flags (bool decltype_p)
4816 {
4817 tsubst_flags_t complain = tf_warning_or_error;
4818 if (decltype_p)
4819 complain |= tf_decltype;
4820 return complain;
4821 }
4822
4823 /* We're about to parse a collection of statements. If we're currently
4824 parsing tentatively, set up a firewall so that any nested
4825 cp_parser_commit_to_tentative_parse won't affect the current context. */
4826
4827 static cp_token_position
4828 cp_parser_start_tentative_firewall (cp_parser *parser)
4829 {
4830 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4831 return 0;
4832
4833 cp_parser_parse_tentatively (parser);
4834 cp_parser_commit_to_topmost_tentative_parse (parser);
4835 return cp_lexer_token_position (parser->lexer, false);
4836 }
4837
4838 /* We've finished parsing the collection of statements. Wrap up the
4839 firewall and replace the relevant tokens with the parsed form. */
4840
4841 static void
4842 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4843 tree expr)
4844 {
4845 if (!start)
4846 return;
4847
4848 /* Finish the firewall level. */
4849 cp_parser_parse_definitely (parser);
4850 /* And remember the result of the parse for when we try again. */
4851 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4852 token->type = CPP_PREPARSED_EXPR;
4853 token->u.value = expr;
4854 token->keyword = RID_MAX;
4855 cp_lexer_purge_tokens_after (parser->lexer, start);
4856 }
4857
4858 /* Like the above functions, but let the user modify the tokens. Used by
4859 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4860 later parses, so it makes sense to localize the effects of
4861 cp_parser_commit_to_tentative_parse. */
4862
4863 struct tentative_firewall
4864 {
4865 cp_parser *parser;
4866 bool set;
4867
4868 tentative_firewall (cp_parser *p): parser(p)
4869 {
4870 /* If we're currently parsing tentatively, start a committed level as a
4871 firewall and then an inner tentative parse. */
4872 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4873 {
4874 cp_parser_parse_tentatively (parser);
4875 cp_parser_commit_to_topmost_tentative_parse (parser);
4876 cp_parser_parse_tentatively (parser);
4877 }
4878 }
4879
4880 ~tentative_firewall()
4881 {
4882 if (set)
4883 {
4884 /* Finish the inner tentative parse and the firewall, propagating any
4885 uncommitted error state to the outer tentative parse. */
4886 bool err = cp_parser_error_occurred (parser);
4887 cp_parser_parse_definitely (parser);
4888 cp_parser_parse_definitely (parser);
4889 if (err)
4890 cp_parser_simulate_error (parser);
4891 }
4892 }
4893 };
4894
4895 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4896 This class is for tracking such a matching pair of symbols.
4897 In particular, it tracks the location of the first token,
4898 so that if the second token is missing, we can highlight the
4899 location of the first token when notifying the user about the
4900 problem. */
4901
4902 template <typename traits_t>
4903 class token_pair
4904 {
4905 public:
4906 /* token_pair's ctor. */
4907 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4908
4909 /* If the next token is the opening symbol for this pair, consume it and
4910 return true.
4911 Otherwise, issue an error and return false.
4912 In either case, record the location of the opening token. */
4913
4914 bool require_open (cp_parser *parser)
4915 {
4916 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4917 return cp_parser_require (parser, traits_t::open_token_type,
4918 traits_t::required_token_open);
4919 }
4920
4921 /* Consume the next token from PARSER, recording its location as
4922 that of the opening token within the pair. */
4923
4924 cp_token * consume_open (cp_parser *parser)
4925 {
4926 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4927 gcc_assert (tok->type == traits_t::open_token_type);
4928 m_open_loc = tok->location;
4929 return tok;
4930 }
4931
4932 /* If the next token is the closing symbol for this pair, consume it
4933 and return it.
4934 Otherwise, issue an error, highlighting the location of the
4935 corresponding opening token, and return NULL. */
4936
4937 cp_token *require_close (cp_parser *parser) const
4938 {
4939 return cp_parser_require (parser, traits_t::close_token_type,
4940 traits_t::required_token_close,
4941 m_open_loc);
4942 }
4943
4944 location_t open_location () const { return m_open_loc; }
4945
4946 private:
4947 location_t m_open_loc;
4948 };
4949
4950 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4951
4952 struct matching_paren_traits
4953 {
4954 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4955 static const enum required_token required_token_open = RT_OPEN_PAREN;
4956 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4957 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4958 };
4959
4960 /* "matching_parens" is a token_pair<T> class for tracking matching
4961 pairs of parentheses. */
4962
4963 typedef token_pair<matching_paren_traits> matching_parens;
4964
4965 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4966
4967 struct matching_brace_traits
4968 {
4969 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4970 static const enum required_token required_token_open = RT_OPEN_BRACE;
4971 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4972 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4973 };
4974
4975 /* "matching_braces" is a token_pair<T> class for tracking matching
4976 pairs of braces. */
4977
4978 typedef token_pair<matching_brace_traits> matching_braces;
4979
4980
4981 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4982 enclosing parentheses. */
4983
4984 static cp_expr
4985 cp_parser_statement_expr (cp_parser *parser)
4986 {
4987 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4988
4989 /* Consume the '('. */
4990 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4991 matching_parens parens;
4992 parens.consume_open (parser);
4993 /* Start the statement-expression. */
4994 tree expr = begin_stmt_expr ();
4995 /* Parse the compound-statement. */
4996 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4997 /* Finish up. */
4998 expr = finish_stmt_expr (expr, false);
4999 /* Consume the ')'. */
5000 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
5001 if (!parens.require_close (parser))
5002 cp_parser_skip_to_end_of_statement (parser);
5003
5004 cp_parser_end_tentative_firewall (parser, start, expr);
5005 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
5006 return cp_expr (expr, combined_loc);
5007 }
5008
5009 /* Expressions [gram.expr] */
5010
5011 /* Parse a fold-operator.
5012
5013 fold-operator:
5014 - * / % ^ & | = < > << >>
5015 = -= *= /= %= ^= &= |= <<= >>=
5016 == != <= >= && || , .* ->*
5017
5018 This returns the tree code corresponding to the matched operator
5019 as an int. When the current token matches a compound assignment
5020 operator, the resulting tree code is the negative value of the
5021 non-assignment operator. */
5022
5023 static int
5024 cp_parser_fold_operator (cp_token *token)
5025 {
5026 switch (token->type)
5027 {
5028 case CPP_PLUS: return PLUS_EXPR;
5029 case CPP_MINUS: return MINUS_EXPR;
5030 case CPP_MULT: return MULT_EXPR;
5031 case CPP_DIV: return TRUNC_DIV_EXPR;
5032 case CPP_MOD: return TRUNC_MOD_EXPR;
5033 case CPP_XOR: return BIT_XOR_EXPR;
5034 case CPP_AND: return BIT_AND_EXPR;
5035 case CPP_OR: return BIT_IOR_EXPR;
5036 case CPP_LSHIFT: return LSHIFT_EXPR;
5037 case CPP_RSHIFT: return RSHIFT_EXPR;
5038
5039 case CPP_EQ: return -NOP_EXPR;
5040 case CPP_PLUS_EQ: return -PLUS_EXPR;
5041 case CPP_MINUS_EQ: return -MINUS_EXPR;
5042 case CPP_MULT_EQ: return -MULT_EXPR;
5043 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
5044 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
5045 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
5046 case CPP_AND_EQ: return -BIT_AND_EXPR;
5047 case CPP_OR_EQ: return -BIT_IOR_EXPR;
5048 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
5049 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
5050
5051 case CPP_EQ_EQ: return EQ_EXPR;
5052 case CPP_NOT_EQ: return NE_EXPR;
5053 case CPP_LESS: return LT_EXPR;
5054 case CPP_GREATER: return GT_EXPR;
5055 case CPP_LESS_EQ: return LE_EXPR;
5056 case CPP_GREATER_EQ: return GE_EXPR;
5057
5058 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
5059 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
5060
5061 case CPP_COMMA: return COMPOUND_EXPR;
5062
5063 case CPP_DOT_STAR: return DOTSTAR_EXPR;
5064 case CPP_DEREF_STAR: return MEMBER_REF;
5065
5066 default: return ERROR_MARK;
5067 }
5068 }
5069
5070 /* Returns true if CODE indicates a binary expression, which is not allowed in
5071 the LHS of a fold-expression. More codes will need to be added to use this
5072 function in other contexts. */
5073
5074 static bool
5075 is_binary_op (tree_code code)
5076 {
5077 switch (code)
5078 {
5079 case PLUS_EXPR:
5080 case POINTER_PLUS_EXPR:
5081 case MINUS_EXPR:
5082 case MULT_EXPR:
5083 case TRUNC_DIV_EXPR:
5084 case TRUNC_MOD_EXPR:
5085 case BIT_XOR_EXPR:
5086 case BIT_AND_EXPR:
5087 case BIT_IOR_EXPR:
5088 case LSHIFT_EXPR:
5089 case RSHIFT_EXPR:
5090
5091 case MODOP_EXPR:
5092
5093 case EQ_EXPR:
5094 case NE_EXPR:
5095 case LE_EXPR:
5096 case GE_EXPR:
5097 case LT_EXPR:
5098 case GT_EXPR:
5099
5100 case TRUTH_ANDIF_EXPR:
5101 case TRUTH_ORIF_EXPR:
5102
5103 case COMPOUND_EXPR:
5104
5105 case DOTSTAR_EXPR:
5106 case MEMBER_REF:
5107 return true;
5108
5109 default:
5110 return false;
5111 }
5112 }
5113
5114 /* If the next token is a suitable fold operator, consume it and return as
5115 the function above. */
5116
5117 static int
5118 cp_parser_fold_operator (cp_parser *parser)
5119 {
5120 cp_token* token = cp_lexer_peek_token (parser->lexer);
5121 int code = cp_parser_fold_operator (token);
5122 if (code != ERROR_MARK)
5123 cp_lexer_consume_token (parser->lexer);
5124 return code;
5125 }
5126
5127 /* Parse a fold-expression.
5128
5129 fold-expression:
5130 ( ... folding-operator cast-expression)
5131 ( cast-expression folding-operator ... )
5132 ( cast-expression folding operator ... folding-operator cast-expression)
5133
5134 Note that the '(' and ')' are matched in primary expression. */
5135
5136 static cp_expr
5137 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5138 {
5139 cp_id_kind pidk;
5140
5141 // Left fold.
5142 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5143 {
5144 if (expr1)
5145 return error_mark_node;
5146 cp_lexer_consume_token (parser->lexer);
5147 int op = cp_parser_fold_operator (parser);
5148 if (op == ERROR_MARK)
5149 {
5150 cp_parser_error (parser, "expected binary operator");
5151 return error_mark_node;
5152 }
5153
5154 tree expr = cp_parser_cast_expression (parser, false, false,
5155 false, &pidk);
5156 if (expr == error_mark_node)
5157 return error_mark_node;
5158 return finish_left_unary_fold_expr (expr, op);
5159 }
5160
5161 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5162 int op = cp_parser_fold_operator (parser);
5163 if (op == ERROR_MARK)
5164 {
5165 cp_parser_error (parser, "expected binary operator");
5166 return error_mark_node;
5167 }
5168
5169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5170 {
5171 cp_parser_error (parser, "expected ...");
5172 return error_mark_node;
5173 }
5174 cp_lexer_consume_token (parser->lexer);
5175
5176 /* The operands of a fold-expression are cast-expressions, so binary or
5177 conditional expressions are not allowed. We check this here to avoid
5178 tentative parsing. */
5179 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5180 /* OK, the expression was parenthesized. */;
5181 else if (is_binary_op (TREE_CODE (expr1)))
5182 error_at (location_of (expr1),
5183 "binary expression in operand of fold-expression");
5184 else if (TREE_CODE (expr1) == COND_EXPR
5185 || (REFERENCE_REF_P (expr1)
5186 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5187 error_at (location_of (expr1),
5188 "conditional expression in operand of fold-expression");
5189
5190 // Right fold.
5191 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5192 return finish_right_unary_fold_expr (expr1, op);
5193
5194 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5195 {
5196 cp_parser_error (parser, "mismatched operator in fold-expression");
5197 return error_mark_node;
5198 }
5199 cp_lexer_consume_token (parser->lexer);
5200
5201 // Binary left or right fold.
5202 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5203 if (expr2 == error_mark_node)
5204 return error_mark_node;
5205 return finish_binary_fold_expr (expr1, expr2, op);
5206 }
5207
5208 /* Parse a primary-expression.
5209
5210 primary-expression:
5211 literal
5212 this
5213 ( expression )
5214 id-expression
5215 lambda-expression (C++11)
5216
5217 GNU Extensions:
5218
5219 primary-expression:
5220 ( compound-statement )
5221 __builtin_va_arg ( assignment-expression , type-id )
5222 __builtin_offsetof ( type-id , offsetof-expression )
5223
5224 C++ Extensions:
5225 __has_nothrow_assign ( type-id )
5226 __has_nothrow_constructor ( type-id )
5227 __has_nothrow_copy ( type-id )
5228 __has_trivial_assign ( type-id )
5229 __has_trivial_constructor ( type-id )
5230 __has_trivial_copy ( type-id )
5231 __has_trivial_destructor ( type-id )
5232 __has_virtual_destructor ( type-id )
5233 __is_abstract ( type-id )
5234 __is_base_of ( type-id , type-id )
5235 __is_class ( type-id )
5236 __is_empty ( type-id )
5237 __is_enum ( type-id )
5238 __is_final ( type-id )
5239 __is_literal_type ( type-id )
5240 __is_pod ( type-id )
5241 __is_polymorphic ( type-id )
5242 __is_std_layout ( type-id )
5243 __is_trivial ( type-id )
5244 __is_union ( type-id )
5245
5246 Objective-C++ Extension:
5247
5248 primary-expression:
5249 objc-expression
5250
5251 literal:
5252 __null
5253
5254 ADDRESS_P is true iff this expression was immediately preceded by
5255 "&" and therefore might denote a pointer-to-member. CAST_P is true
5256 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5257 true iff this expression is a template argument.
5258
5259 Returns a representation of the expression. Upon return, *IDK
5260 indicates what kind of id-expression (if any) was present. */
5261
5262 static cp_expr
5263 cp_parser_primary_expression (cp_parser *parser,
5264 bool address_p,
5265 bool cast_p,
5266 bool template_arg_p,
5267 bool decltype_p,
5268 cp_id_kind *idk)
5269 {
5270 cp_token *token = NULL;
5271
5272 /* Assume the primary expression is not an id-expression. */
5273 *idk = CP_ID_KIND_NONE;
5274
5275 /* Peek at the next token. */
5276 token = cp_lexer_peek_token (parser->lexer);
5277 switch ((int) token->type)
5278 {
5279 /* literal:
5280 integer-literal
5281 character-literal
5282 floating-literal
5283 string-literal
5284 boolean-literal
5285 pointer-literal
5286 user-defined-literal */
5287 case CPP_CHAR:
5288 case CPP_CHAR16:
5289 case CPP_CHAR32:
5290 case CPP_WCHAR:
5291 case CPP_UTF8CHAR:
5292 case CPP_NUMBER:
5293 case CPP_PREPARSED_EXPR:
5294 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5295 return cp_parser_userdef_numeric_literal (parser);
5296 token = cp_lexer_consume_token (parser->lexer);
5297 if (TREE_CODE (token->u.value) == FIXED_CST)
5298 {
5299 error_at (token->location,
5300 "fixed-point types not supported in C++");
5301 return error_mark_node;
5302 }
5303 /* Floating-point literals are only allowed in an integral
5304 constant expression if they are cast to an integral or
5305 enumeration type. */
5306 if (TREE_CODE (token->u.value) == REAL_CST
5307 && parser->integral_constant_expression_p
5308 && pedantic)
5309 {
5310 /* CAST_P will be set even in invalid code like "int(2.7 +
5311 ...)". Therefore, we have to check that the next token
5312 is sure to end the cast. */
5313 if (cast_p)
5314 {
5315 cp_token *next_token;
5316
5317 next_token = cp_lexer_peek_token (parser->lexer);
5318 if (/* The comma at the end of an
5319 enumerator-definition. */
5320 next_token->type != CPP_COMMA
5321 /* The curly brace at the end of an enum-specifier. */
5322 && next_token->type != CPP_CLOSE_BRACE
5323 /* The end of a statement. */
5324 && next_token->type != CPP_SEMICOLON
5325 /* The end of the cast-expression. */
5326 && next_token->type != CPP_CLOSE_PAREN
5327 /* The end of an array bound. */
5328 && next_token->type != CPP_CLOSE_SQUARE
5329 /* The closing ">" in a template-argument-list. */
5330 && (next_token->type != CPP_GREATER
5331 || parser->greater_than_is_operator_p)
5332 /* C++0x only: A ">>" treated like two ">" tokens,
5333 in a template-argument-list. */
5334 && (next_token->type != CPP_RSHIFT
5335 || (cxx_dialect == cxx98)
5336 || parser->greater_than_is_operator_p))
5337 cast_p = false;
5338 }
5339
5340 /* If we are within a cast, then the constraint that the
5341 cast is to an integral or enumeration type will be
5342 checked at that point. If we are not within a cast, then
5343 this code is invalid. */
5344 if (!cast_p)
5345 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5346 }
5347 return (cp_expr (token->u.value, token->location)
5348 .maybe_add_location_wrapper ());
5349
5350 case CPP_CHAR_USERDEF:
5351 case CPP_CHAR16_USERDEF:
5352 case CPP_CHAR32_USERDEF:
5353 case CPP_WCHAR_USERDEF:
5354 case CPP_UTF8CHAR_USERDEF:
5355 return cp_parser_userdef_char_literal (parser);
5356
5357 case CPP_STRING:
5358 case CPP_STRING16:
5359 case CPP_STRING32:
5360 case CPP_WSTRING:
5361 case CPP_UTF8STRING:
5362 case CPP_STRING_USERDEF:
5363 case CPP_STRING16_USERDEF:
5364 case CPP_STRING32_USERDEF:
5365 case CPP_WSTRING_USERDEF:
5366 case CPP_UTF8STRING_USERDEF:
5367 /* ??? Should wide strings be allowed when parser->translate_strings_p
5368 is false (i.e. in attributes)? If not, we can kill the third
5369 argument to cp_parser_string_literal. */
5370 return (cp_parser_string_literal (parser,
5371 parser->translate_strings_p,
5372 true)
5373 .maybe_add_location_wrapper ());
5374
5375 case CPP_OPEN_PAREN:
5376 /* If we see `( { ' then we are looking at the beginning of
5377 a GNU statement-expression. */
5378 if (cp_parser_allow_gnu_extensions_p (parser)
5379 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5380 {
5381 /* Statement-expressions are not allowed by the standard. */
5382 pedwarn (token->location, OPT_Wpedantic,
5383 "ISO C++ forbids braced-groups within expressions");
5384
5385 /* And they're not allowed outside of a function-body; you
5386 cannot, for example, write:
5387
5388 int i = ({ int j = 3; j + 1; });
5389
5390 at class or namespace scope. */
5391 if (!parser->in_function_body
5392 || parser->in_template_argument_list_p)
5393 {
5394 error_at (token->location,
5395 "statement-expressions are not allowed outside "
5396 "functions nor in template-argument lists");
5397 cp_parser_skip_to_end_of_block_or_statement (parser);
5398 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5399 cp_lexer_consume_token (parser->lexer);
5400 return error_mark_node;
5401 }
5402 else
5403 return cp_parser_statement_expr (parser);
5404 }
5405 /* Otherwise it's a normal parenthesized expression. */
5406 {
5407 cp_expr expr;
5408 bool saved_greater_than_is_operator_p;
5409
5410 location_t open_paren_loc = token->location;
5411
5412 /* Consume the `('. */
5413 matching_parens parens;
5414 parens.consume_open (parser);
5415 /* Within a parenthesized expression, a `>' token is always
5416 the greater-than operator. */
5417 saved_greater_than_is_operator_p
5418 = parser->greater_than_is_operator_p;
5419 parser->greater_than_is_operator_p = true;
5420
5421 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5422 /* Left fold expression. */
5423 expr = NULL_TREE;
5424 else
5425 /* Parse the parenthesized expression. */
5426 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5427
5428 token = cp_lexer_peek_token (parser->lexer);
5429 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5430 {
5431 expr = cp_parser_fold_expression (parser, expr);
5432 if (expr != error_mark_node
5433 && cxx_dialect < cxx17)
5434 pedwarn (input_location, 0, "fold-expressions only available "
5435 "with %<-std=c++17%> or %<-std=gnu++17%>");
5436 }
5437 else
5438 /* Let the front end know that this expression was
5439 enclosed in parentheses. This matters in case, for
5440 example, the expression is of the form `A::B', since
5441 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5442 not. */
5443 expr = finish_parenthesized_expr (expr);
5444
5445 /* DR 705: Wrapping an unqualified name in parentheses
5446 suppresses arg-dependent lookup. We want to pass back
5447 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5448 (c++/37862), but none of the others. */
5449 if (*idk != CP_ID_KIND_QUALIFIED)
5450 *idk = CP_ID_KIND_NONE;
5451
5452 /* The `>' token might be the end of a template-id or
5453 template-parameter-list now. */
5454 parser->greater_than_is_operator_p
5455 = saved_greater_than_is_operator_p;
5456
5457 /* Consume the `)'. */
5458 token = cp_lexer_peek_token (parser->lexer);
5459 location_t close_paren_loc = token->location;
5460 expr.set_range (open_paren_loc, close_paren_loc);
5461 if (!parens.require_close (parser)
5462 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5463 cp_parser_skip_to_end_of_statement (parser);
5464
5465 return expr;
5466 }
5467
5468 case CPP_OPEN_SQUARE:
5469 {
5470 if (c_dialect_objc ())
5471 {
5472 /* We might have an Objective-C++ message. */
5473 cp_parser_parse_tentatively (parser);
5474 tree msg = cp_parser_objc_message_expression (parser);
5475 /* If that works out, we're done ... */
5476 if (cp_parser_parse_definitely (parser))
5477 return msg;
5478 /* ... else, fall though to see if it's a lambda. */
5479 }
5480 cp_expr lam = cp_parser_lambda_expression (parser);
5481 /* Don't warn about a failed tentative parse. */
5482 if (cp_parser_error_occurred (parser))
5483 return error_mark_node;
5484 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5485 return lam;
5486 }
5487
5488 case CPP_OBJC_STRING:
5489 if (c_dialect_objc ())
5490 /* We have an Objective-C++ string literal. */
5491 return cp_parser_objc_expression (parser);
5492 cp_parser_error (parser, "expected primary-expression");
5493 return error_mark_node;
5494
5495 case CPP_KEYWORD:
5496 switch (token->keyword)
5497 {
5498 /* These two are the boolean literals. */
5499 case RID_TRUE:
5500 cp_lexer_consume_token (parser->lexer);
5501 return cp_expr (boolean_true_node, token->location);
5502 case RID_FALSE:
5503 cp_lexer_consume_token (parser->lexer);
5504 return cp_expr (boolean_false_node, token->location);
5505
5506 /* The `__null' literal. */
5507 case RID_NULL:
5508 cp_lexer_consume_token (parser->lexer);
5509 return cp_expr (null_node, token->location);
5510
5511 /* The `nullptr' literal. */
5512 case RID_NULLPTR:
5513 cp_lexer_consume_token (parser->lexer);
5514 return cp_expr (nullptr_node, token->location);
5515
5516 /* Recognize the `this' keyword. */
5517 case RID_THIS:
5518 cp_lexer_consume_token (parser->lexer);
5519 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5520 {
5521 error_at (token->location,
5522 "%<this%> may not be used in this context");
5523 return error_mark_node;
5524 }
5525 /* Pointers cannot appear in constant-expressions. */
5526 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5527 return error_mark_node;
5528 return cp_expr (finish_this_expr (), token->location);
5529
5530 /* The `operator' keyword can be the beginning of an
5531 id-expression. */
5532 case RID_OPERATOR:
5533 goto id_expression;
5534
5535 case RID_FUNCTION_NAME:
5536 case RID_PRETTY_FUNCTION_NAME:
5537 case RID_C99_FUNCTION_NAME:
5538 {
5539 non_integral_constant name;
5540
5541 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5542 __func__ are the names of variables -- but they are
5543 treated specially. Therefore, they are handled here,
5544 rather than relying on the generic id-expression logic
5545 below. Grammatically, these names are id-expressions.
5546
5547 Consume the token. */
5548 token = cp_lexer_consume_token (parser->lexer);
5549
5550 switch (token->keyword)
5551 {
5552 case RID_FUNCTION_NAME:
5553 name = NIC_FUNC_NAME;
5554 break;
5555 case RID_PRETTY_FUNCTION_NAME:
5556 name = NIC_PRETTY_FUNC;
5557 break;
5558 case RID_C99_FUNCTION_NAME:
5559 name = NIC_C99_FUNC;
5560 break;
5561 default:
5562 gcc_unreachable ();
5563 }
5564
5565 if (cp_parser_non_integral_constant_expression (parser, name))
5566 return error_mark_node;
5567
5568 /* Look up the name. */
5569 return finish_fname (token->u.value);
5570 }
5571
5572 case RID_VA_ARG:
5573 {
5574 tree expression;
5575 tree type;
5576 location_t type_location;
5577 location_t start_loc
5578 = cp_lexer_peek_token (parser->lexer)->location;
5579 /* The `__builtin_va_arg' construct is used to handle
5580 `va_arg'. Consume the `__builtin_va_arg' token. */
5581 cp_lexer_consume_token (parser->lexer);
5582 /* Look for the opening `('. */
5583 matching_parens parens;
5584 parens.require_open (parser);
5585 /* Now, parse the assignment-expression. */
5586 expression = cp_parser_assignment_expression (parser);
5587 /* Look for the `,'. */
5588 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5589 type_location = cp_lexer_peek_token (parser->lexer)->location;
5590 /* Parse the type-id. */
5591 {
5592 type_id_in_expr_sentinel s (parser);
5593 type = cp_parser_type_id (parser);
5594 }
5595 /* Look for the closing `)'. */
5596 location_t finish_loc
5597 = cp_lexer_peek_token (parser->lexer)->location;
5598 parens.require_close (parser);
5599 /* Using `va_arg' in a constant-expression is not
5600 allowed. */
5601 if (cp_parser_non_integral_constant_expression (parser,
5602 NIC_VA_ARG))
5603 return error_mark_node;
5604 /* Construct a location of the form:
5605 __builtin_va_arg (v, int)
5606 ~~~~~~~~~~~~~~~~~~~~~^~~~
5607 with the caret at the type, ranging from the start of the
5608 "__builtin_va_arg" token to the close paren. */
5609 location_t combined_loc
5610 = make_location (type_location, start_loc, finish_loc);
5611 return build_x_va_arg (combined_loc, expression, type);
5612 }
5613
5614 case RID_OFFSETOF:
5615 return cp_parser_builtin_offsetof (parser);
5616
5617 case RID_HAS_NOTHROW_ASSIGN:
5618 case RID_HAS_NOTHROW_CONSTRUCTOR:
5619 case RID_HAS_NOTHROW_COPY:
5620 case RID_HAS_TRIVIAL_ASSIGN:
5621 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5622 case RID_HAS_TRIVIAL_COPY:
5623 case RID_HAS_TRIVIAL_DESTRUCTOR:
5624 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5625 case RID_HAS_VIRTUAL_DESTRUCTOR:
5626 case RID_IS_ABSTRACT:
5627 case RID_IS_AGGREGATE:
5628 case RID_IS_BASE_OF:
5629 case RID_IS_CLASS:
5630 case RID_IS_EMPTY:
5631 case RID_IS_ENUM:
5632 case RID_IS_FINAL:
5633 case RID_IS_LITERAL_TYPE:
5634 case RID_IS_POD:
5635 case RID_IS_POLYMORPHIC:
5636 case RID_IS_SAME_AS:
5637 case RID_IS_STD_LAYOUT:
5638 case RID_IS_TRIVIAL:
5639 case RID_IS_TRIVIALLY_ASSIGNABLE:
5640 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5641 case RID_IS_TRIVIALLY_COPYABLE:
5642 case RID_IS_UNION:
5643 case RID_IS_ASSIGNABLE:
5644 case RID_IS_CONSTRUCTIBLE:
5645 case RID_IS_NOTHROW_ASSIGNABLE:
5646 case RID_IS_NOTHROW_CONSTRUCTIBLE:
5647 return cp_parser_trait_expr (parser, token->keyword);
5648
5649 // C++ concepts
5650 case RID_REQUIRES:
5651 return cp_parser_requires_expression (parser);
5652
5653 /* Objective-C++ expressions. */
5654 case RID_AT_ENCODE:
5655 case RID_AT_PROTOCOL:
5656 case RID_AT_SELECTOR:
5657 return cp_parser_objc_expression (parser);
5658
5659 case RID_TEMPLATE:
5660 if (parser->in_function_body
5661 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5662 == CPP_LESS))
5663 {
5664 error_at (token->location,
5665 "a template declaration cannot appear at block scope");
5666 cp_parser_skip_to_end_of_block_or_statement (parser);
5667 return error_mark_node;
5668 }
5669 /* FALLTHRU */
5670 default:
5671 cp_parser_error (parser, "expected primary-expression");
5672 return error_mark_node;
5673 }
5674
5675 /* An id-expression can start with either an identifier, a
5676 `::' as the beginning of a qualified-id, or the "operator"
5677 keyword. */
5678 case CPP_NAME:
5679 case CPP_SCOPE:
5680 case CPP_TEMPLATE_ID:
5681 case CPP_NESTED_NAME_SPECIFIER:
5682 {
5683 id_expression:
5684 cp_expr id_expression;
5685 cp_expr decl;
5686 const char *error_msg;
5687 bool template_p;
5688 bool done;
5689 cp_token *id_expr_token;
5690
5691 /* Parse the id-expression. */
5692 id_expression
5693 = cp_parser_id_expression (parser,
5694 /*template_keyword_p=*/false,
5695 /*check_dependency_p=*/true,
5696 &template_p,
5697 /*declarator_p=*/false,
5698 /*optional_p=*/false);
5699 if (id_expression == error_mark_node)
5700 return error_mark_node;
5701 id_expr_token = token;
5702 token = cp_lexer_peek_token (parser->lexer);
5703 done = (token->type != CPP_OPEN_SQUARE
5704 && token->type != CPP_OPEN_PAREN
5705 && token->type != CPP_DOT
5706 && token->type != CPP_DEREF
5707 && token->type != CPP_PLUS_PLUS
5708 && token->type != CPP_MINUS_MINUS);
5709 /* If we have a template-id, then no further lookup is
5710 required. If the template-id was for a template-class, we
5711 will sometimes have a TYPE_DECL at this point. */
5712 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5713 || TREE_CODE (id_expression) == TYPE_DECL)
5714 decl = id_expression;
5715 /* Look up the name. */
5716 else
5717 {
5718 tree ambiguous_decls;
5719
5720 /* If we already know that this lookup is ambiguous, then
5721 we've already issued an error message; there's no reason
5722 to check again. */
5723 if (id_expr_token->type == CPP_NAME
5724 && id_expr_token->error_reported)
5725 {
5726 cp_parser_simulate_error (parser);
5727 return error_mark_node;
5728 }
5729
5730 decl = cp_parser_lookup_name (parser, id_expression,
5731 none_type,
5732 template_p,
5733 /*is_namespace=*/false,
5734 /*check_dependency=*/true,
5735 &ambiguous_decls,
5736 id_expression.get_location ());
5737 /* If the lookup was ambiguous, an error will already have
5738 been issued. */
5739 if (ambiguous_decls)
5740 return error_mark_node;
5741
5742 /* In Objective-C++, we may have an Objective-C 2.0
5743 dot-syntax for classes here. */
5744 if (c_dialect_objc ()
5745 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5746 && TREE_CODE (decl) == TYPE_DECL
5747 && objc_is_class_name (decl))
5748 {
5749 tree component;
5750 cp_lexer_consume_token (parser->lexer);
5751 component = cp_parser_identifier (parser);
5752 if (component == error_mark_node)
5753 return error_mark_node;
5754
5755 tree result = objc_build_class_component_ref (id_expression,
5756 component);
5757 /* Build a location of the form:
5758 expr.component
5759 ~~~~~^~~~~~~~~
5760 with caret at the start of the component name (at
5761 input_location), ranging from the start of the id_expression
5762 to the end of the component name. */
5763 location_t combined_loc
5764 = make_location (input_location, id_expression.get_start (),
5765 get_finish (input_location));
5766 protected_set_expr_location (result, combined_loc);
5767 return result;
5768 }
5769
5770 /* In Objective-C++, an instance variable (ivar) may be preferred
5771 to whatever cp_parser_lookup_name() found.
5772 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5773 rest of c-family, we have to do a little extra work to preserve
5774 any location information in cp_expr "decl". Given that
5775 objc_lookup_ivar is implemented in "c-family" and "objc", we
5776 have a trip through the pure "tree" type, rather than cp_expr.
5777 Naively copying it back to "decl" would implicitly give the
5778 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5779 store an EXPR_LOCATION. Hence we only update "decl" (and
5780 hence its location_t) if we get back a different tree node. */
5781 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5782 id_expression);
5783 if (decl_tree != decl.get_value ())
5784 decl = cp_expr (decl_tree);
5785
5786 /* If name lookup gives us a SCOPE_REF, then the
5787 qualifying scope was dependent. */
5788 if (TREE_CODE (decl) == SCOPE_REF)
5789 {
5790 /* At this point, we do not know if DECL is a valid
5791 integral constant expression. We assume that it is
5792 in fact such an expression, so that code like:
5793
5794 template <int N> struct A {
5795 int a[B<N>::i];
5796 };
5797
5798 is accepted. At template-instantiation time, we
5799 will check that B<N>::i is actually a constant. */
5800 return decl;
5801 }
5802 /* Check to see if DECL is a local variable in a context
5803 where that is forbidden. */
5804 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
5805 && local_variable_p (decl))
5806 {
5807 error_at (id_expression.get_location (),
5808 "local variable %qD may not appear in this context",
5809 decl.get_value ());
5810 return error_mark_node;
5811 }
5812 }
5813
5814 decl = (finish_id_expression
5815 (id_expression, decl, parser->scope,
5816 idk,
5817 parser->integral_constant_expression_p,
5818 parser->allow_non_integral_constant_expression_p,
5819 &parser->non_integral_constant_expression_p,
5820 template_p, done, address_p,
5821 template_arg_p,
5822 &error_msg,
5823 id_expression.get_location ()));
5824 if (error_msg)
5825 cp_parser_error (parser, error_msg);
5826 /* Build a location for an id-expression of the form:
5827 ::ns::id
5828 ~~~~~~^~
5829 or:
5830 id
5831 ^~
5832 i.e. from the start of the first token to the end of the final
5833 token, with the caret at the start of the unqualified-id. */
5834 location_t caret_loc = get_pure_location (id_expression.get_location ());
5835 location_t start_loc = get_start (id_expr_token->location);
5836 location_t finish_loc = get_finish (id_expression.get_location ());
5837 location_t combined_loc
5838 = make_location (caret_loc, start_loc, finish_loc);
5839
5840 decl.set_location (combined_loc);
5841 return decl;
5842 }
5843
5844 /* Anything else is an error. */
5845 default:
5846 cp_parser_error (parser, "expected primary-expression");
5847 return error_mark_node;
5848 }
5849 }
5850
5851 static inline cp_expr
5852 cp_parser_primary_expression (cp_parser *parser,
5853 bool address_p,
5854 bool cast_p,
5855 bool template_arg_p,
5856 cp_id_kind *idk)
5857 {
5858 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5859 /*decltype*/false, idk);
5860 }
5861
5862 /* Parse an id-expression.
5863
5864 id-expression:
5865 unqualified-id
5866 qualified-id
5867
5868 qualified-id:
5869 :: [opt] nested-name-specifier template [opt] unqualified-id
5870 :: identifier
5871 :: operator-function-id
5872 :: template-id
5873
5874 Return a representation of the unqualified portion of the
5875 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5876 a `::' or nested-name-specifier.
5877
5878 Often, if the id-expression was a qualified-id, the caller will
5879 want to make a SCOPE_REF to represent the qualified-id. This
5880 function does not do this in order to avoid wastefully creating
5881 SCOPE_REFs when they are not required.
5882
5883 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5884 `template' keyword.
5885
5886 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5887 uninstantiated templates.
5888
5889 If *TEMPLATE_P is non-NULL, it is set to true iff the
5890 `template' keyword is used to explicitly indicate that the entity
5891 named is a template.
5892
5893 If DECLARATOR_P is true, the id-expression is appearing as part of
5894 a declarator, rather than as part of an expression. */
5895
5896 static cp_expr
5897 cp_parser_id_expression (cp_parser *parser,
5898 bool template_keyword_p,
5899 bool check_dependency_p,
5900 bool *template_p,
5901 bool declarator_p,
5902 bool optional_p)
5903 {
5904 bool global_scope_p;
5905 bool nested_name_specifier_p;
5906
5907 /* Assume the `template' keyword was not used. */
5908 if (template_p)
5909 *template_p = template_keyword_p;
5910
5911 /* Look for the optional `::' operator. */
5912 global_scope_p
5913 = (!template_keyword_p
5914 && (cp_parser_global_scope_opt (parser,
5915 /*current_scope_valid_p=*/false)
5916 != NULL_TREE));
5917
5918 /* Look for the optional nested-name-specifier. */
5919 nested_name_specifier_p
5920 = (cp_parser_nested_name_specifier_opt (parser,
5921 /*typename_keyword_p=*/false,
5922 check_dependency_p,
5923 /*type_p=*/false,
5924 declarator_p,
5925 template_keyword_p)
5926 != NULL_TREE);
5927
5928 /* If there is a nested-name-specifier, then we are looking at
5929 the first qualified-id production. */
5930 if (nested_name_specifier_p)
5931 {
5932 tree saved_scope;
5933 tree saved_object_scope;
5934 tree saved_qualifying_scope;
5935 cp_expr unqualified_id;
5936 bool is_template;
5937
5938 /* See if the next token is the `template' keyword. */
5939 if (!template_p)
5940 template_p = &is_template;
5941 *template_p = cp_parser_optional_template_keyword (parser);
5942 /* Name lookup we do during the processing of the
5943 unqualified-id might obliterate SCOPE. */
5944 saved_scope = parser->scope;
5945 saved_object_scope = parser->object_scope;
5946 saved_qualifying_scope = parser->qualifying_scope;
5947 /* Process the final unqualified-id. */
5948 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5949 check_dependency_p,
5950 declarator_p,
5951 /*optional_p=*/false);
5952 /* Restore the SAVED_SCOPE for our caller. */
5953 parser->scope = saved_scope;
5954 parser->object_scope = saved_object_scope;
5955 parser->qualifying_scope = saved_qualifying_scope;
5956
5957 return unqualified_id;
5958 }
5959 /* Otherwise, if we are in global scope, then we are looking at one
5960 of the other qualified-id productions. */
5961 else if (global_scope_p)
5962 {
5963 cp_token *token;
5964 tree id;
5965
5966 /* Peek at the next token. */
5967 token = cp_lexer_peek_token (parser->lexer);
5968
5969 /* If it's an identifier, and the next token is not a "<", then
5970 we can avoid the template-id case. This is an optimization
5971 for this common case. */
5972 if (token->type == CPP_NAME
5973 && !cp_parser_nth_token_starts_template_argument_list_p
5974 (parser, 2))
5975 return cp_parser_identifier (parser);
5976
5977 cp_parser_parse_tentatively (parser);
5978 /* Try a template-id. */
5979 id = cp_parser_template_id_expr (parser,
5980 /*template_keyword_p=*/false,
5981 /*check_dependency_p=*/true,
5982 declarator_p);
5983 /* If that worked, we're done. */
5984 if (cp_parser_parse_definitely (parser))
5985 return id;
5986
5987 /* Peek at the next token. (Changes in the token buffer may
5988 have invalidated the pointer obtained above.) */
5989 token = cp_lexer_peek_token (parser->lexer);
5990
5991 switch (token->type)
5992 {
5993 case CPP_NAME:
5994 return cp_parser_identifier (parser);
5995
5996 case CPP_KEYWORD:
5997 if (token->keyword == RID_OPERATOR)
5998 return cp_parser_operator_function_id (parser);
5999 /* Fall through. */
6000
6001 default:
6002 cp_parser_error (parser, "expected id-expression");
6003 return error_mark_node;
6004 }
6005 }
6006 else
6007 return cp_parser_unqualified_id (parser, template_keyword_p,
6008 /*check_dependency_p=*/true,
6009 declarator_p,
6010 optional_p);
6011 }
6012
6013 /* Parse an unqualified-id.
6014
6015 unqualified-id:
6016 identifier
6017 operator-function-id
6018 conversion-function-id
6019 ~ class-name
6020 template-id
6021
6022 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6023 keyword, in a construct like `A::template ...'.
6024
6025 Returns a representation of unqualified-id. For the `identifier'
6026 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
6027 production a BIT_NOT_EXPR is returned; the operand of the
6028 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
6029 other productions, see the documentation accompanying the
6030 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
6031 names are looked up in uninstantiated templates. If DECLARATOR_P
6032 is true, the unqualified-id is appearing as part of a declarator,
6033 rather than as part of an expression. */
6034
6035 static cp_expr
6036 cp_parser_unqualified_id (cp_parser* parser,
6037 bool template_keyword_p,
6038 bool check_dependency_p,
6039 bool declarator_p,
6040 bool optional_p)
6041 {
6042 cp_token *token;
6043
6044 /* Peek at the next token. */
6045 token = cp_lexer_peek_token (parser->lexer);
6046
6047 switch ((int) token->type)
6048 {
6049 case CPP_NAME:
6050 {
6051 tree id;
6052
6053 /* We don't know yet whether or not this will be a
6054 template-id. */
6055 cp_parser_parse_tentatively (parser);
6056 /* Try a template-id. */
6057 id = cp_parser_template_id_expr (parser, template_keyword_p,
6058 check_dependency_p,
6059 declarator_p);
6060 /* If it worked, we're done. */
6061 if (cp_parser_parse_definitely (parser))
6062 return id;
6063 /* Otherwise, it's an ordinary identifier. */
6064 return cp_parser_identifier (parser);
6065 }
6066
6067 case CPP_TEMPLATE_ID:
6068 return cp_parser_template_id_expr (parser, template_keyword_p,
6069 check_dependency_p,
6070 declarator_p);
6071
6072 case CPP_COMPL:
6073 {
6074 tree type_decl;
6075 tree qualifying_scope;
6076 tree object_scope;
6077 tree scope;
6078 bool done;
6079 location_t tilde_loc = token->location;
6080
6081 /* Consume the `~' token. */
6082 cp_lexer_consume_token (parser->lexer);
6083 /* Parse the class-name. The standard, as written, seems to
6084 say that:
6085
6086 template <typename T> struct S { ~S (); };
6087 template <typename T> S<T>::~S() {}
6088
6089 is invalid, since `~' must be followed by a class-name, but
6090 `S<T>' is dependent, and so not known to be a class.
6091 That's not right; we need to look in uninstantiated
6092 templates. A further complication arises from:
6093
6094 template <typename T> void f(T t) {
6095 t.T::~T();
6096 }
6097
6098 Here, it is not possible to look up `T' in the scope of `T'
6099 itself. We must look in both the current scope, and the
6100 scope of the containing complete expression.
6101
6102 Yet another issue is:
6103
6104 struct S {
6105 int S;
6106 ~S();
6107 };
6108
6109 S::~S() {}
6110
6111 The standard does not seem to say that the `S' in `~S'
6112 should refer to the type `S' and not the data member
6113 `S::S'. */
6114
6115 /* DR 244 says that we look up the name after the "~" in the
6116 same scope as we looked up the qualifying name. That idea
6117 isn't fully worked out; it's more complicated than that. */
6118 scope = parser->scope;
6119 object_scope = parser->object_scope;
6120 qualifying_scope = parser->qualifying_scope;
6121
6122 /* Check for invalid scopes. */
6123 if (scope == error_mark_node)
6124 {
6125 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6126 cp_lexer_consume_token (parser->lexer);
6127 return error_mark_node;
6128 }
6129 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6130 {
6131 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6132 error_at (token->location,
6133 "scope %qT before %<~%> is not a class-name",
6134 scope);
6135 cp_parser_simulate_error (parser);
6136 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6137 cp_lexer_consume_token (parser->lexer);
6138 return error_mark_node;
6139 }
6140 if (template_keyword_p)
6141 {
6142 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6143 error_at (tilde_loc, "%<template%> keyword not permitted in "
6144 "destructor name");
6145 cp_parser_simulate_error (parser);
6146 return error_mark_node;
6147 }
6148
6149 gcc_assert (!scope || TYPE_P (scope));
6150
6151 token = cp_lexer_peek_token (parser->lexer);
6152
6153 /* Create a location with caret == start at the tilde,
6154 finishing at the end of the peeked token, e.g:
6155 ~token
6156 ^~~~~~. */
6157 location_t loc
6158 = make_location (tilde_loc, tilde_loc, token->location);
6159
6160 /* If the name is of the form "X::~X" it's OK even if X is a
6161 typedef. */
6162
6163 if (scope
6164 && token->type == CPP_NAME
6165 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6166 != CPP_LESS)
6167 && (token->u.value == TYPE_IDENTIFIER (scope)
6168 || (CLASS_TYPE_P (scope)
6169 && constructor_name_p (token->u.value, scope))))
6170 {
6171 cp_lexer_consume_token (parser->lexer);
6172 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6173 }
6174
6175 /* ~auto means the destructor of whatever the object is. */
6176 if (cp_parser_is_keyword (token, RID_AUTO))
6177 {
6178 if (cxx_dialect < cxx14)
6179 pedwarn (loc, 0,
6180 "%<~auto%> only available with "
6181 "%<-std=c++14%> or %<-std=gnu++14%>");
6182 cp_lexer_consume_token (parser->lexer);
6183 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6184 }
6185
6186 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6187 declarator-id of a constructor or destructor. */
6188 if (token->type == CPP_TEMPLATE_ID && cxx_dialect >= cxx20)
6189 {
6190 if (!cp_parser_simulate_error (parser))
6191 error_at (tilde_loc, "template-id not allowed for destructor");
6192 return error_mark_node;
6193 }
6194
6195 /* If there was an explicit qualification (S::~T), first look
6196 in the scope given by the qualification (i.e., S).
6197
6198 Note: in the calls to cp_parser_class_name below we pass
6199 typename_type so that lookup finds the injected-class-name
6200 rather than the constructor. */
6201 done = false;
6202 type_decl = NULL_TREE;
6203 if (scope)
6204 {
6205 cp_parser_parse_tentatively (parser);
6206 type_decl = cp_parser_class_name (parser,
6207 /*typename_keyword_p=*/false,
6208 /*template_keyword_p=*/false,
6209 typename_type,
6210 /*check_dependency=*/false,
6211 /*class_head_p=*/false,
6212 declarator_p);
6213 if (cp_parser_parse_definitely (parser))
6214 done = true;
6215 }
6216 /* In "N::S::~S", look in "N" as well. */
6217 if (!done && scope && qualifying_scope)
6218 {
6219 cp_parser_parse_tentatively (parser);
6220 parser->scope = qualifying_scope;
6221 parser->object_scope = NULL_TREE;
6222 parser->qualifying_scope = NULL_TREE;
6223 type_decl
6224 = cp_parser_class_name (parser,
6225 /*typename_keyword_p=*/false,
6226 /*template_keyword_p=*/false,
6227 typename_type,
6228 /*check_dependency=*/false,
6229 /*class_head_p=*/false,
6230 declarator_p);
6231 if (cp_parser_parse_definitely (parser))
6232 done = true;
6233 }
6234 /* In "p->S::~T", look in the scope given by "*p" as well. */
6235 else if (!done && object_scope)
6236 {
6237 cp_parser_parse_tentatively (parser);
6238 parser->scope = object_scope;
6239 parser->object_scope = NULL_TREE;
6240 parser->qualifying_scope = NULL_TREE;
6241 type_decl
6242 = cp_parser_class_name (parser,
6243 /*typename_keyword_p=*/false,
6244 /*template_keyword_p=*/false,
6245 typename_type,
6246 /*check_dependency=*/false,
6247 /*class_head_p=*/false,
6248 declarator_p);
6249 if (cp_parser_parse_definitely (parser))
6250 done = true;
6251 }
6252 /* Look in the surrounding context. */
6253 if (!done)
6254 {
6255 parser->scope = NULL_TREE;
6256 parser->object_scope = NULL_TREE;
6257 parser->qualifying_scope = NULL_TREE;
6258 if (processing_template_decl)
6259 cp_parser_parse_tentatively (parser);
6260 type_decl
6261 = cp_parser_class_name (parser,
6262 /*typename_keyword_p=*/false,
6263 /*template_keyword_p=*/false,
6264 typename_type,
6265 /*check_dependency=*/false,
6266 /*class_head_p=*/false,
6267 declarator_p);
6268 if (processing_template_decl
6269 && ! cp_parser_parse_definitely (parser))
6270 {
6271 /* We couldn't find a type with this name. If we're parsing
6272 tentatively, fail and try something else. */
6273 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6274 {
6275 cp_parser_simulate_error (parser);
6276 return error_mark_node;
6277 }
6278 /* Otherwise, accept it and check for a match at instantiation
6279 time. */
6280 type_decl = cp_parser_identifier (parser);
6281 if (type_decl != error_mark_node)
6282 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6283 return type_decl;
6284 }
6285 }
6286 /* If an error occurred, assume that the name of the
6287 destructor is the same as the name of the qualifying
6288 class. That allows us to keep parsing after running
6289 into ill-formed destructor names. */
6290 if (type_decl == error_mark_node && scope)
6291 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6292 else if (type_decl == error_mark_node)
6293 return error_mark_node;
6294
6295 /* Check that destructor name and scope match. */
6296 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6297 {
6298 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6299 error_at (loc,
6300 "declaration of %<~%T%> as member of %qT",
6301 type_decl, scope);
6302 cp_parser_simulate_error (parser);
6303 return error_mark_node;
6304 }
6305
6306 /* [class.dtor]
6307
6308 A typedef-name that names a class shall not be used as the
6309 identifier in the declarator for a destructor declaration. */
6310 if (declarator_p
6311 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6312 && !DECL_SELF_REFERENCE_P (type_decl)
6313 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6314 error_at (loc,
6315 "typedef-name %qD used as destructor declarator",
6316 type_decl);
6317
6318 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6319 }
6320
6321 case CPP_KEYWORD:
6322 if (token->keyword == RID_OPERATOR)
6323 {
6324 cp_expr id;
6325
6326 /* This could be a template-id, so we try that first. */
6327 cp_parser_parse_tentatively (parser);
6328 /* Try a template-id. */
6329 id = cp_parser_template_id_expr (parser, template_keyword_p,
6330 /*check_dependency_p=*/true,
6331 declarator_p);
6332 /* If that worked, we're done. */
6333 if (cp_parser_parse_definitely (parser))
6334 return id;
6335 /* We still don't know whether we're looking at an
6336 operator-function-id or a conversion-function-id. */
6337 cp_parser_parse_tentatively (parser);
6338 /* Try an operator-function-id. */
6339 id = cp_parser_operator_function_id (parser);
6340 /* If that didn't work, try a conversion-function-id. */
6341 if (!cp_parser_parse_definitely (parser))
6342 id = cp_parser_conversion_function_id (parser);
6343
6344 return id;
6345 }
6346 /* Fall through. */
6347
6348 default:
6349 if (optional_p)
6350 return NULL_TREE;
6351 cp_parser_error (parser, "expected unqualified-id");
6352 return error_mark_node;
6353 }
6354 }
6355
6356 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6357 be a template-id or the name shall refer to a class template or an
6358 alias template. */
6359
6360 static void
6361 check_template_keyword_in_nested_name_spec (tree name)
6362 {
6363 if (CLASS_TYPE_P (name)
6364 && ((CLASSTYPE_USE_TEMPLATE (name)
6365 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name)))
6366 || CLASSTYPE_IS_TEMPLATE (name)))
6367 return;
6368
6369 if (TREE_CODE (name) == TYPENAME_TYPE
6370 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR)
6371 return;
6372 /* Alias templates are also OK. */
6373 else if (alias_template_specialization_p (name, nt_opaque))
6374 return;
6375
6376 permerror (input_location, TYPE_P (name)
6377 ? G_("%qT is not a template")
6378 : G_("%qD is not a template"),
6379 name);
6380 }
6381
6382 /* Parse an (optional) nested-name-specifier.
6383
6384 nested-name-specifier: [C++98]
6385 class-or-namespace-name :: nested-name-specifier [opt]
6386 class-or-namespace-name :: template nested-name-specifier [opt]
6387
6388 nested-name-specifier: [C++0x]
6389 type-name ::
6390 namespace-name ::
6391 nested-name-specifier identifier ::
6392 nested-name-specifier template [opt] simple-template-id ::
6393
6394 PARSER->SCOPE should be set appropriately before this function is
6395 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6396 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6397 in name lookups.
6398
6399 Sets PARSER->SCOPE to the class (TYPE) or namespace
6400 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6401 it unchanged if there is no nested-name-specifier. Returns the new
6402 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6403
6404 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6405 part of a declaration and/or decl-specifier. */
6406
6407 static tree
6408 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6409 bool typename_keyword_p,
6410 bool check_dependency_p,
6411 bool type_p,
6412 bool is_declaration,
6413 bool template_keyword_p /* = false */)
6414 {
6415 bool success = false;
6416 cp_token_position start = 0;
6417 cp_token *token;
6418
6419 /* Remember where the nested-name-specifier starts. */
6420 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6421 && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6422 {
6423 start = cp_lexer_token_position (parser->lexer, false);
6424 push_deferring_access_checks (dk_deferred);
6425 }
6426
6427 while (true)
6428 {
6429 tree new_scope;
6430 tree old_scope;
6431 tree saved_qualifying_scope;
6432
6433 /* Spot cases that cannot be the beginning of a
6434 nested-name-specifier. */
6435 token = cp_lexer_peek_token (parser->lexer);
6436
6437 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6438 the already parsed nested-name-specifier. */
6439 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6440 {
6441 /* Grab the nested-name-specifier and continue the loop. */
6442 cp_parser_pre_parsed_nested_name_specifier (parser);
6443 /* If we originally encountered this nested-name-specifier
6444 with IS_DECLARATION set to false, we will not have
6445 resolved TYPENAME_TYPEs, so we must do so here. */
6446 if (is_declaration
6447 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6448 {
6449 new_scope = resolve_typename_type (parser->scope,
6450 /*only_current_p=*/false);
6451 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6452 parser->scope = new_scope;
6453 }
6454 success = true;
6455 continue;
6456 }
6457
6458 /* Spot cases that cannot be the beginning of a
6459 nested-name-specifier. On the second and subsequent times
6460 through the loop, we look for the `template' keyword. */
6461 if (success && token->keyword == RID_TEMPLATE)
6462 ;
6463 /* A template-id can start a nested-name-specifier. */
6464 else if (token->type == CPP_TEMPLATE_ID)
6465 ;
6466 /* DR 743: decltype can be used in a nested-name-specifier. */
6467 else if (token_is_decltype (token))
6468 ;
6469 else
6470 {
6471 /* If the next token is not an identifier, then it is
6472 definitely not a type-name or namespace-name. */
6473 if (token->type != CPP_NAME)
6474 break;
6475 /* If the following token is neither a `<' (to begin a
6476 template-id), nor a `::', then we are not looking at a
6477 nested-name-specifier. */
6478 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6479
6480 if (token->type == CPP_COLON
6481 && parser->colon_corrects_to_scope_p
6482 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6483 {
6484 gcc_rich_location richloc (token->location);
6485 richloc.add_fixit_replace ("::");
6486 error_at (&richloc,
6487 "found %<:%> in nested-name-specifier, "
6488 "expected %<::%>");
6489 token->type = CPP_SCOPE;
6490 }
6491
6492 if (token->type != CPP_SCOPE
6493 && !cp_parser_nth_token_starts_template_argument_list_p
6494 (parser, 2))
6495 break;
6496 }
6497
6498 /* The nested-name-specifier is optional, so we parse
6499 tentatively. */
6500 cp_parser_parse_tentatively (parser);
6501
6502 /* Look for the optional `template' keyword, if this isn't the
6503 first time through the loop. */
6504 if (success)
6505 {
6506 template_keyword_p = cp_parser_optional_template_keyword (parser);
6507 /* DR1710: "In a qualified-id used as the name in
6508 a typename-specifier, elaborated-type-specifier, using-declaration,
6509 or class-or-decltype, an optional keyword template appearing at
6510 the top level is ignored." */
6511 if (!template_keyword_p
6512 && typename_keyword_p
6513 && cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
6514 template_keyword_p = true;
6515 }
6516
6517 /* Save the old scope since the name lookup we are about to do
6518 might destroy it. */
6519 old_scope = parser->scope;
6520 saved_qualifying_scope = parser->qualifying_scope;
6521 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6522 look up names in "X<T>::I" in order to determine that "Y" is
6523 a template. So, if we have a typename at this point, we make
6524 an effort to look through it. */
6525 if (is_declaration
6526 && !typename_keyword_p
6527 && parser->scope
6528 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6529 parser->scope = resolve_typename_type (parser->scope,
6530 /*only_current_p=*/false);
6531 /* Parse the qualifying entity. */
6532 new_scope
6533 = cp_parser_qualifying_entity (parser,
6534 typename_keyword_p,
6535 template_keyword_p,
6536 check_dependency_p,
6537 type_p,
6538 is_declaration);
6539 /* Look for the `::' token. */
6540 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6541
6542 /* If we found what we wanted, we keep going; otherwise, we're
6543 done. */
6544 if (!cp_parser_parse_definitely (parser))
6545 {
6546 bool error_p = false;
6547
6548 /* Restore the OLD_SCOPE since it was valid before the
6549 failed attempt at finding the last
6550 class-or-namespace-name. */
6551 parser->scope = old_scope;
6552 parser->qualifying_scope = saved_qualifying_scope;
6553
6554 /* If the next token is a decltype, and the one after that is a
6555 `::', then the decltype has failed to resolve to a class or
6556 enumeration type. Give this error even when parsing
6557 tentatively since it can't possibly be valid--and we're going
6558 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6559 won't get another chance.*/
6560 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6561 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6562 == CPP_SCOPE))
6563 {
6564 token = cp_lexer_consume_token (parser->lexer);
6565 tree dtype = token->u.tree_check_value->value;
6566 if (dtype != error_mark_node)
6567 error_at (token->location, "%<decltype%> evaluates to %qT, "
6568 "which is not a class or enumeration type",
6569 dtype);
6570 parser->scope = error_mark_node;
6571 error_p = true;
6572 /* As below. */
6573 success = true;
6574 cp_lexer_consume_token (parser->lexer);
6575 }
6576
6577 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6578 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6579 {
6580 /* If we have a non-type template-id followed by ::, it can't
6581 possibly be valid. */
6582 token = cp_lexer_peek_token (parser->lexer);
6583 tree tid = token->u.tree_check_value->value;
6584 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6585 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6586 {
6587 tree tmpl = NULL_TREE;
6588 if (is_overloaded_fn (tid))
6589 {
6590 tree fns = get_fns (tid);
6591 if (OVL_SINGLE_P (fns))
6592 tmpl = OVL_FIRST (fns);
6593 if (function_concept_p (fns))
6594 error_at (token->location, "concept-id %qD "
6595 "in nested-name-specifier", tid);
6596 else
6597 error_at (token->location, "function template-id "
6598 "%qD in nested-name-specifier", tid);
6599 }
6600 else
6601 {
6602 tmpl = TREE_OPERAND (tid, 0);
6603 if (variable_concept_p (tmpl)
6604 || standard_concept_p (tmpl))
6605 error_at (token->location, "concept-id %qD "
6606 "in nested-name-specifier", tid);
6607 else
6608 {
6609 /* Variable template. */
6610 gcc_assert (variable_template_p (tmpl));
6611 error_at (token->location, "variable template-id "
6612 "%qD in nested-name-specifier", tid);
6613 }
6614 }
6615 if (tmpl)
6616 inform (DECL_SOURCE_LOCATION (tmpl),
6617 "%qD declared here", tmpl);
6618
6619 parser->scope = error_mark_node;
6620 error_p = true;
6621 /* As below. */
6622 success = true;
6623 cp_lexer_consume_token (parser->lexer);
6624 cp_lexer_consume_token (parser->lexer);
6625 }
6626 }
6627
6628 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6629 break;
6630 /* If the next token is an identifier, and the one after
6631 that is a `::', then any valid interpretation would have
6632 found a class-or-namespace-name. */
6633 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6634 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6635 == CPP_SCOPE)
6636 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6637 != CPP_COMPL))
6638 {
6639 token = cp_lexer_consume_token (parser->lexer);
6640 if (!error_p)
6641 {
6642 if (!token->error_reported)
6643 {
6644 tree decl;
6645 tree ambiguous_decls;
6646
6647 decl = cp_parser_lookup_name (parser, token->u.value,
6648 none_type,
6649 /*is_template=*/false,
6650 /*is_namespace=*/false,
6651 /*check_dependency=*/true,
6652 &ambiguous_decls,
6653 token->location);
6654 if (TREE_CODE (decl) == TEMPLATE_DECL)
6655 error_at (token->location,
6656 "%qD used without template arguments",
6657 decl);
6658 else if (ambiguous_decls)
6659 {
6660 // cp_parser_lookup_name has the same diagnostic,
6661 // thus make sure to emit it at most once.
6662 if (cp_parser_uncommitted_to_tentative_parse_p
6663 (parser))
6664 {
6665 error_at (token->location,
6666 "reference to %qD is ambiguous",
6667 token->u.value);
6668 print_candidates (ambiguous_decls);
6669 }
6670 decl = error_mark_node;
6671 }
6672 else
6673 {
6674 if (cxx_dialect != cxx98)
6675 cp_parser_name_lookup_error
6676 (parser, token->u.value, decl, NLE_NOT_CXX98,
6677 token->location);
6678 else
6679 cp_parser_name_lookup_error
6680 (parser, token->u.value, decl, NLE_CXX98,
6681 token->location);
6682 }
6683 }
6684 parser->scope = error_mark_node;
6685 error_p = true;
6686 /* Treat this as a successful nested-name-specifier
6687 due to:
6688
6689 [basic.lookup.qual]
6690
6691 If the name found is not a class-name (clause
6692 _class_) or namespace-name (_namespace.def_), the
6693 program is ill-formed. */
6694 success = true;
6695 }
6696 cp_lexer_consume_token (parser->lexer);
6697 }
6698 break;
6699 }
6700 /* We've found one valid nested-name-specifier. */
6701 success = true;
6702 /* Name lookup always gives us a DECL. */
6703 if (TREE_CODE (new_scope) == TYPE_DECL)
6704 new_scope = TREE_TYPE (new_scope);
6705 /* Uses of "template" must be followed by actual templates. */
6706 if (template_keyword_p)
6707 check_template_keyword_in_nested_name_spec (new_scope);
6708 /* If it is a class scope, try to complete it; we are about to
6709 be looking up names inside the class. */
6710 if (TYPE_P (new_scope)
6711 /* Since checking types for dependency can be expensive,
6712 avoid doing it if the type is already complete. */
6713 && !COMPLETE_TYPE_P (new_scope)
6714 /* Do not try to complete dependent types. */
6715 && !dependent_type_p (new_scope))
6716 {
6717 new_scope = complete_type (new_scope);
6718 /* If it is a typedef to current class, use the current
6719 class instead, as the typedef won't have any names inside
6720 it yet. */
6721 if (!COMPLETE_TYPE_P (new_scope)
6722 && currently_open_class (new_scope))
6723 new_scope = TYPE_MAIN_VARIANT (new_scope);
6724 }
6725 /* Make sure we look in the right scope the next time through
6726 the loop. */
6727 parser->scope = new_scope;
6728 }
6729
6730 /* If parsing tentatively, replace the sequence of tokens that makes
6731 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6732 token. That way, should we re-parse the token stream, we will
6733 not have to repeat the effort required to do the parse, nor will
6734 we issue duplicate error messages. */
6735 if (success && start)
6736 {
6737 cp_token *token;
6738
6739 token = cp_lexer_token_at (parser->lexer, start);
6740 /* Reset the contents of the START token. */
6741 token->type = CPP_NESTED_NAME_SPECIFIER;
6742 /* Retrieve any deferred checks. Do not pop this access checks yet
6743 so the memory will not be reclaimed during token replacing below. */
6744 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6745 token->tree_check_p = true;
6746 token->u.tree_check_value->value = parser->scope;
6747 token->u.tree_check_value->checks = get_deferred_access_checks ();
6748 token->u.tree_check_value->qualifying_scope =
6749 parser->qualifying_scope;
6750 token->keyword = RID_MAX;
6751
6752 /* Purge all subsequent tokens. */
6753 cp_lexer_purge_tokens_after (parser->lexer, start);
6754 }
6755
6756 if (start)
6757 pop_to_parent_deferring_access_checks ();
6758
6759 return success ? parser->scope : NULL_TREE;
6760 }
6761
6762 /* Parse a nested-name-specifier. See
6763 cp_parser_nested_name_specifier_opt for details. This function
6764 behaves identically, except that it will an issue an error if no
6765 nested-name-specifier is present. */
6766
6767 static tree
6768 cp_parser_nested_name_specifier (cp_parser *parser,
6769 bool typename_keyword_p,
6770 bool check_dependency_p,
6771 bool type_p,
6772 bool is_declaration)
6773 {
6774 tree scope;
6775
6776 /* Look for the nested-name-specifier. */
6777 scope = cp_parser_nested_name_specifier_opt (parser,
6778 typename_keyword_p,
6779 check_dependency_p,
6780 type_p,
6781 is_declaration);
6782 /* If it was not present, issue an error message. */
6783 if (!scope)
6784 {
6785 cp_parser_error (parser, "expected nested-name-specifier");
6786 parser->scope = NULL_TREE;
6787 }
6788
6789 return scope;
6790 }
6791
6792 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6793 this is either a class-name or a namespace-name (which corresponds
6794 to the class-or-namespace-name production in the grammar). For
6795 C++0x, it can also be a type-name that refers to an enumeration
6796 type or a simple-template-id.
6797
6798 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6799 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6800 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6801 TYPE_P is TRUE iff the next name should be taken as a class-name,
6802 even the same name is declared to be another entity in the same
6803 scope.
6804
6805 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6806 specified by the class-or-namespace-name. If neither is found the
6807 ERROR_MARK_NODE is returned. */
6808
6809 static tree
6810 cp_parser_qualifying_entity (cp_parser *parser,
6811 bool typename_keyword_p,
6812 bool template_keyword_p,
6813 bool check_dependency_p,
6814 bool type_p,
6815 bool is_declaration)
6816 {
6817 tree saved_scope;
6818 tree saved_qualifying_scope;
6819 tree saved_object_scope;
6820 tree scope;
6821 bool only_class_p;
6822 bool successful_parse_p;
6823
6824 /* DR 743: decltype can appear in a nested-name-specifier. */
6825 if (cp_lexer_next_token_is_decltype (parser->lexer))
6826 {
6827 scope = cp_parser_decltype (parser);
6828 if (TREE_CODE (scope) != ENUMERAL_TYPE
6829 && !MAYBE_CLASS_TYPE_P (scope))
6830 {
6831 cp_parser_simulate_error (parser);
6832 return error_mark_node;
6833 }
6834 if (TYPE_NAME (scope))
6835 scope = TYPE_NAME (scope);
6836 return scope;
6837 }
6838
6839 /* Before we try to parse the class-name, we must save away the
6840 current PARSER->SCOPE since cp_parser_class_name will destroy
6841 it. */
6842 saved_scope = parser->scope;
6843 saved_qualifying_scope = parser->qualifying_scope;
6844 saved_object_scope = parser->object_scope;
6845 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6846 there is no need to look for a namespace-name. */
6847 only_class_p = template_keyword_p
6848 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6849 if (!only_class_p)
6850 cp_parser_parse_tentatively (parser);
6851 scope = cp_parser_class_name (parser,
6852 typename_keyword_p,
6853 template_keyword_p,
6854 type_p ? class_type : none_type,
6855 check_dependency_p,
6856 /*class_head_p=*/false,
6857 is_declaration,
6858 /*enum_ok=*/cxx_dialect > cxx98);
6859 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6860 /* If that didn't work, try for a namespace-name. */
6861 if (!only_class_p && !successful_parse_p)
6862 {
6863 /* Restore the saved scope. */
6864 parser->scope = saved_scope;
6865 parser->qualifying_scope = saved_qualifying_scope;
6866 parser->object_scope = saved_object_scope;
6867 /* If we are not looking at an identifier followed by the scope
6868 resolution operator, then this is not part of a
6869 nested-name-specifier. (Note that this function is only used
6870 to parse the components of a nested-name-specifier.) */
6871 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6872 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6873 return error_mark_node;
6874 scope = cp_parser_namespace_name (parser);
6875 }
6876
6877 return scope;
6878 }
6879
6880 /* Return true if we are looking at a compound-literal, false otherwise. */
6881
6882 static bool
6883 cp_parser_compound_literal_p (cp_parser *parser)
6884 {
6885 cp_lexer_save_tokens (parser->lexer);
6886
6887 /* Skip tokens until the next token is a closing parenthesis.
6888 If we find the closing `)', and the next token is a `{', then
6889 we are looking at a compound-literal. */
6890 bool compound_literal_p
6891 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6892 /*consume_paren=*/true)
6893 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6894
6895 /* Roll back the tokens we skipped. */
6896 cp_lexer_rollback_tokens (parser->lexer);
6897
6898 return compound_literal_p;
6899 }
6900
6901 /* Return true if EXPR is the integer constant zero or a complex constant
6902 of zero, without any folding, but ignoring location wrappers. */
6903
6904 bool
6905 literal_integer_zerop (const_tree expr)
6906 {
6907 return (location_wrapper_p (expr)
6908 && integer_zerop (TREE_OPERAND (expr, 0)));
6909 }
6910
6911 /* Parse a postfix-expression.
6912
6913 postfix-expression:
6914 primary-expression
6915 postfix-expression [ expression ]
6916 postfix-expression ( expression-list [opt] )
6917 simple-type-specifier ( expression-list [opt] )
6918 typename :: [opt] nested-name-specifier identifier
6919 ( expression-list [opt] )
6920 typename :: [opt] nested-name-specifier template [opt] template-id
6921 ( expression-list [opt] )
6922 postfix-expression . template [opt] id-expression
6923 postfix-expression -> template [opt] id-expression
6924 postfix-expression . pseudo-destructor-name
6925 postfix-expression -> pseudo-destructor-name
6926 postfix-expression ++
6927 postfix-expression --
6928 dynamic_cast < type-id > ( expression )
6929 static_cast < type-id > ( expression )
6930 reinterpret_cast < type-id > ( expression )
6931 const_cast < type-id > ( expression )
6932 typeid ( expression )
6933 typeid ( type-id )
6934
6935 GNU Extension:
6936
6937 postfix-expression:
6938 ( type-id ) { initializer-list , [opt] }
6939
6940 This extension is a GNU version of the C99 compound-literal
6941 construct. (The C99 grammar uses `type-name' instead of `type-id',
6942 but they are essentially the same concept.)
6943
6944 If ADDRESS_P is true, the postfix expression is the operand of the
6945 `&' operator. CAST_P is true if this expression is the target of a
6946 cast.
6947
6948 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6949 class member access expressions [expr.ref].
6950
6951 Returns a representation of the expression. */
6952
6953 static cp_expr
6954 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6955 bool member_access_only_p, bool decltype_p,
6956 cp_id_kind * pidk_return)
6957 {
6958 cp_token *token;
6959 location_t loc;
6960 enum rid keyword;
6961 cp_id_kind idk = CP_ID_KIND_NONE;
6962 cp_expr postfix_expression = NULL_TREE;
6963 bool is_member_access = false;
6964
6965 /* Peek at the next token. */
6966 token = cp_lexer_peek_token (parser->lexer);
6967 loc = token->location;
6968 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6969
6970 /* Some of the productions are determined by keywords. */
6971 keyword = token->keyword;
6972 switch (keyword)
6973 {
6974 case RID_DYNCAST:
6975 case RID_STATCAST:
6976 case RID_REINTCAST:
6977 case RID_CONSTCAST:
6978 {
6979 tree type;
6980 cp_expr expression;
6981 const char *saved_message;
6982 bool saved_in_type_id_in_expr_p;
6983
6984 /* All of these can be handled in the same way from the point
6985 of view of parsing. Begin by consuming the token
6986 identifying the cast. */
6987 cp_lexer_consume_token (parser->lexer);
6988
6989 /* New types cannot be defined in the cast. */
6990 saved_message = parser->type_definition_forbidden_message;
6991 parser->type_definition_forbidden_message
6992 = G_("types may not be defined in casts");
6993
6994 /* Look for the opening `<'. */
6995 cp_parser_require (parser, CPP_LESS, RT_LESS);
6996 /* Parse the type to which we are casting. */
6997 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6998 parser->in_type_id_in_expr_p = true;
6999 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
7000 NULL);
7001 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7002 /* Look for the closing `>'. */
7003 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
7004 /* Restore the old message. */
7005 parser->type_definition_forbidden_message = saved_message;
7006
7007 bool saved_greater_than_is_operator_p
7008 = parser->greater_than_is_operator_p;
7009 parser->greater_than_is_operator_p = true;
7010
7011 /* And the expression which is being cast. */
7012 matching_parens parens;
7013 parens.require_open (parser);
7014 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
7015 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
7016 RT_CLOSE_PAREN);
7017 location_t end_loc = close_paren ?
7018 close_paren->location : UNKNOWN_LOCATION;
7019
7020 parser->greater_than_is_operator_p
7021 = saved_greater_than_is_operator_p;
7022
7023 /* Only type conversions to integral or enumeration types
7024 can be used in constant-expressions. */
7025 if (!cast_valid_in_integral_constant_expression_p (type)
7026 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
7027 {
7028 postfix_expression = error_mark_node;
7029 break;
7030 }
7031
7032 /* Construct a location e.g. :
7033 reinterpret_cast <int *> (expr)
7034 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7035 ranging from the start of the "*_cast" token to the final closing
7036 paren, with the caret at the start. */
7037 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
7038
7039 switch (keyword)
7040 {
7041 case RID_DYNCAST:
7042 postfix_expression
7043 = build_dynamic_cast (cp_cast_loc, type, expression,
7044 tf_warning_or_error);
7045 break;
7046 case RID_STATCAST:
7047 postfix_expression
7048 = build_static_cast (cp_cast_loc, type, expression,
7049 tf_warning_or_error);
7050 break;
7051 case RID_REINTCAST:
7052 postfix_expression
7053 = build_reinterpret_cast (cp_cast_loc, type, expression,
7054 tf_warning_or_error);
7055 break;
7056 case RID_CONSTCAST:
7057 postfix_expression
7058 = build_const_cast (cp_cast_loc, type, expression,
7059 tf_warning_or_error);
7060 break;
7061 default:
7062 gcc_unreachable ();
7063 }
7064 }
7065 break;
7066
7067 case RID_TYPEID:
7068 {
7069 tree type;
7070 const char *saved_message;
7071 bool saved_in_type_id_in_expr_p;
7072
7073 /* Consume the `typeid' token. */
7074 cp_lexer_consume_token (parser->lexer);
7075 /* Look for the `(' token. */
7076 matching_parens parens;
7077 parens.require_open (parser);
7078 /* Types cannot be defined in a `typeid' expression. */
7079 saved_message = parser->type_definition_forbidden_message;
7080 parser->type_definition_forbidden_message
7081 = G_("types may not be defined in a %<typeid%> expression");
7082 /* We can't be sure yet whether we're looking at a type-id or an
7083 expression. */
7084 cp_parser_parse_tentatively (parser);
7085 /* Try a type-id first. */
7086 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7087 parser->in_type_id_in_expr_p = true;
7088 type = cp_parser_type_id (parser);
7089 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7090 /* Look for the `)' token. Otherwise, we can't be sure that
7091 we're not looking at an expression: consider `typeid (int
7092 (3))', for example. */
7093 cp_token *close_paren = parens.require_close (parser);
7094 /* If all went well, simply lookup the type-id. */
7095 if (cp_parser_parse_definitely (parser))
7096 postfix_expression = get_typeid (type, tf_warning_or_error);
7097 /* Otherwise, fall back to the expression variant. */
7098 else
7099 {
7100 tree expression;
7101
7102 /* Look for an expression. */
7103 expression = cp_parser_expression (parser, & idk);
7104 /* Compute its typeid. */
7105 postfix_expression = build_typeid (expression, tf_warning_or_error);
7106 /* Look for the `)' token. */
7107 close_paren = parens.require_close (parser);
7108 }
7109 /* Restore the saved message. */
7110 parser->type_definition_forbidden_message = saved_message;
7111 /* `typeid' may not appear in an integral constant expression. */
7112 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
7113 postfix_expression = error_mark_node;
7114
7115 /* Construct a location e.g. :
7116 typeid (expr)
7117 ^~~~~~~~~~~~~
7118 ranging from the start of the "typeid" token to the final closing
7119 paren, with the caret at the start. */
7120 if (close_paren)
7121 {
7122 location_t typeid_loc
7123 = make_location (start_loc, start_loc, close_paren->location);
7124 postfix_expression.set_location (typeid_loc);
7125 postfix_expression.maybe_add_location_wrapper ();
7126 }
7127 }
7128 break;
7129
7130 case RID_TYPENAME:
7131 {
7132 tree type;
7133 /* The syntax permitted here is the same permitted for an
7134 elaborated-type-specifier. */
7135 ++parser->prevent_constrained_type_specifiers;
7136 type = cp_parser_elaborated_type_specifier (parser,
7137 /*is_friend=*/false,
7138 /*is_declaration=*/false);
7139 --parser->prevent_constrained_type_specifiers;
7140 postfix_expression = cp_parser_functional_cast (parser, type);
7141 }
7142 break;
7143
7144 case RID_ADDRESSOF:
7145 case RID_BUILTIN_SHUFFLE:
7146 case RID_BUILTIN_LAUNDER:
7147 {
7148 vec<tree, va_gc> *vec;
7149 unsigned int i;
7150 tree p;
7151
7152 cp_lexer_consume_token (parser->lexer);
7153 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7154 /*cast_p=*/false, /*allow_expansion_p=*/true,
7155 /*non_constant_p=*/NULL);
7156 if (vec == NULL)
7157 {
7158 postfix_expression = error_mark_node;
7159 break;
7160 }
7161
7162 FOR_EACH_VEC_ELT (*vec, i, p)
7163 mark_exp_read (p);
7164
7165 switch (keyword)
7166 {
7167 case RID_ADDRESSOF:
7168 if (vec->length () == 1)
7169 postfix_expression
7170 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7171 else
7172 {
7173 error_at (loc, "wrong number of arguments to "
7174 "%<__builtin_addressof%>");
7175 postfix_expression = error_mark_node;
7176 }
7177 break;
7178
7179 case RID_BUILTIN_LAUNDER:
7180 if (vec->length () == 1)
7181 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7182 tf_warning_or_error);
7183 else
7184 {
7185 error_at (loc, "wrong number of arguments to "
7186 "%<__builtin_launder%>");
7187 postfix_expression = error_mark_node;
7188 }
7189 break;
7190
7191 case RID_BUILTIN_SHUFFLE:
7192 if (vec->length () == 2)
7193 postfix_expression
7194 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7195 (*vec)[1], tf_warning_or_error);
7196 else if (vec->length () == 3)
7197 postfix_expression
7198 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7199 (*vec)[2], tf_warning_or_error);
7200 else
7201 {
7202 error_at (loc, "wrong number of arguments to "
7203 "%<__builtin_shuffle%>");
7204 postfix_expression = error_mark_node;
7205 }
7206 break;
7207
7208 default:
7209 gcc_unreachable ();
7210 }
7211 break;
7212 }
7213
7214 case RID_BUILTIN_CONVERTVECTOR:
7215 {
7216 tree expression;
7217 tree type;
7218 /* Consume the `__builtin_convertvector' token. */
7219 cp_lexer_consume_token (parser->lexer);
7220 /* Look for the opening `('. */
7221 matching_parens parens;
7222 parens.require_open (parser);
7223 /* Now, parse the assignment-expression. */
7224 expression = cp_parser_assignment_expression (parser);
7225 /* Look for the `,'. */
7226 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7227 location_t type_location
7228 = cp_lexer_peek_token (parser->lexer)->location;
7229 /* Parse the type-id. */
7230 {
7231 type_id_in_expr_sentinel s (parser);
7232 type = cp_parser_type_id (parser);
7233 }
7234 /* Look for the closing `)'. */
7235 parens.require_close (parser);
7236 return cp_build_vec_convert (expression, type_location, type,
7237 tf_warning_or_error);
7238 }
7239
7240 default:
7241 {
7242 tree type;
7243
7244 /* If the next thing is a simple-type-specifier, we may be
7245 looking at a functional cast. We could also be looking at
7246 an id-expression. So, we try the functional cast, and if
7247 that doesn't work we fall back to the primary-expression. */
7248 cp_parser_parse_tentatively (parser);
7249 /* Look for the simple-type-specifier. */
7250 ++parser->prevent_constrained_type_specifiers;
7251 type = cp_parser_simple_type_specifier (parser,
7252 /*decl_specs=*/NULL,
7253 CP_PARSER_FLAGS_NONE);
7254 --parser->prevent_constrained_type_specifiers;
7255 /* Parse the cast itself. */
7256 if (!cp_parser_error_occurred (parser))
7257 postfix_expression
7258 = cp_parser_functional_cast (parser, type);
7259 /* If that worked, we're done. */
7260 if (cp_parser_parse_definitely (parser))
7261 break;
7262
7263 /* If the functional-cast didn't work out, try a
7264 compound-literal. */
7265 if (cp_parser_allow_gnu_extensions_p (parser)
7266 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7267 {
7268 cp_expr initializer = NULL_TREE;
7269
7270 cp_parser_parse_tentatively (parser);
7271
7272 matching_parens parens;
7273 parens.consume_open (parser);
7274
7275 /* Avoid calling cp_parser_type_id pointlessly, see comment
7276 in cp_parser_cast_expression about c++/29234. */
7277 if (!cp_parser_compound_literal_p (parser))
7278 cp_parser_simulate_error (parser);
7279 else
7280 {
7281 /* Parse the type. */
7282 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7283 parser->in_type_id_in_expr_p = true;
7284 type = cp_parser_type_id (parser);
7285 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7286 parens.require_close (parser);
7287 }
7288
7289 /* If things aren't going well, there's no need to
7290 keep going. */
7291 if (!cp_parser_error_occurred (parser))
7292 {
7293 bool non_constant_p;
7294 /* Parse the brace-enclosed initializer list. */
7295 initializer = cp_parser_braced_list (parser,
7296 &non_constant_p);
7297 }
7298 /* If that worked, we're definitely looking at a
7299 compound-literal expression. */
7300 if (cp_parser_parse_definitely (parser))
7301 {
7302 /* Warn the user that a compound literal is not
7303 allowed in standard C++. */
7304 pedwarn (input_location, OPT_Wpedantic,
7305 "ISO C++ forbids compound-literals");
7306 /* For simplicity, we disallow compound literals in
7307 constant-expressions. We could
7308 allow compound literals of integer type, whose
7309 initializer was a constant, in constant
7310 expressions. Permitting that usage, as a further
7311 extension, would not change the meaning of any
7312 currently accepted programs. (Of course, as
7313 compound literals are not part of ISO C++, the
7314 standard has nothing to say.) */
7315 if (cp_parser_non_integral_constant_expression (parser,
7316 NIC_NCC))
7317 {
7318 postfix_expression = error_mark_node;
7319 break;
7320 }
7321 /* Form the representation of the compound-literal. */
7322 postfix_expression
7323 = finish_compound_literal (type, initializer,
7324 tf_warning_or_error, fcl_c99);
7325 postfix_expression.set_location (initializer.get_location ());
7326 break;
7327 }
7328 }
7329
7330 /* It must be a primary-expression. */
7331 postfix_expression
7332 = cp_parser_primary_expression (parser, address_p, cast_p,
7333 /*template_arg_p=*/false,
7334 decltype_p,
7335 &idk);
7336 }
7337 break;
7338 }
7339
7340 /* Note that we don't need to worry about calling build_cplus_new on a
7341 class-valued CALL_EXPR in decltype when it isn't the end of the
7342 postfix-expression; unary_complex_lvalue will take care of that for
7343 all these cases. */
7344
7345 /* Keep looping until the postfix-expression is complete. */
7346 while (true)
7347 {
7348 if (idk == CP_ID_KIND_UNQUALIFIED
7349 && identifier_p (postfix_expression)
7350 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7351 /* It is not a Koenig lookup function call. */
7352 postfix_expression
7353 = unqualified_name_lookup_error (postfix_expression);
7354
7355 /* Peek at the next token. */
7356 token = cp_lexer_peek_token (parser->lexer);
7357
7358 switch (token->type)
7359 {
7360 case CPP_OPEN_SQUARE:
7361 if (cp_next_tokens_can_be_std_attribute_p (parser))
7362 {
7363 cp_parser_error (parser,
7364 "two consecutive %<[%> shall "
7365 "only introduce an attribute");
7366 return error_mark_node;
7367 }
7368 postfix_expression
7369 = cp_parser_postfix_open_square_expression (parser,
7370 postfix_expression,
7371 false,
7372 decltype_p);
7373 postfix_expression.set_range (start_loc,
7374 postfix_expression.get_location ());
7375
7376 idk = CP_ID_KIND_NONE;
7377 is_member_access = false;
7378 break;
7379
7380 case CPP_OPEN_PAREN:
7381 /* postfix-expression ( expression-list [opt] ) */
7382 {
7383 bool koenig_p;
7384 bool is_builtin_constant_p;
7385 bool saved_integral_constant_expression_p = false;
7386 bool saved_non_integral_constant_expression_p = false;
7387 tsubst_flags_t complain = complain_flags (decltype_p);
7388 vec<tree, va_gc> *args;
7389 location_t close_paren_loc = UNKNOWN_LOCATION;
7390
7391 is_member_access = false;
7392
7393 tree stripped_expression
7394 = tree_strip_any_location_wrapper (postfix_expression);
7395 is_builtin_constant_p
7396 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7397 if (is_builtin_constant_p)
7398 {
7399 /* The whole point of __builtin_constant_p is to allow
7400 non-constant expressions to appear as arguments. */
7401 saved_integral_constant_expression_p
7402 = parser->integral_constant_expression_p;
7403 saved_non_integral_constant_expression_p
7404 = parser->non_integral_constant_expression_p;
7405 parser->integral_constant_expression_p = false;
7406 }
7407 args = (cp_parser_parenthesized_expression_list
7408 (parser, non_attr,
7409 /*cast_p=*/false, /*allow_expansion_p=*/true,
7410 /*non_constant_p=*/NULL,
7411 /*close_paren_loc=*/&close_paren_loc,
7412 /*wrap_locations_p=*/true));
7413 if (is_builtin_constant_p)
7414 {
7415 parser->integral_constant_expression_p
7416 = saved_integral_constant_expression_p;
7417 parser->non_integral_constant_expression_p
7418 = saved_non_integral_constant_expression_p;
7419 }
7420
7421 if (args == NULL)
7422 {
7423 postfix_expression = error_mark_node;
7424 break;
7425 }
7426
7427 /* Function calls are not permitted in
7428 constant-expressions. */
7429 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7430 && cp_parser_non_integral_constant_expression (parser,
7431 NIC_FUNC_CALL))
7432 {
7433 postfix_expression = error_mark_node;
7434 release_tree_vector (args);
7435 break;
7436 }
7437
7438 koenig_p = false;
7439 if (idk == CP_ID_KIND_UNQUALIFIED
7440 || idk == CP_ID_KIND_TEMPLATE_ID)
7441 {
7442 if (identifier_p (postfix_expression)
7443 /* In C++20, we may need to perform ADL for a template
7444 name. */
7445 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7446 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7447 {
7448 if (!args->is_empty ())
7449 {
7450 koenig_p = true;
7451 if (!any_type_dependent_arguments_p (args))
7452 postfix_expression
7453 = perform_koenig_lookup (postfix_expression, args,
7454 complain);
7455 }
7456 else
7457 postfix_expression
7458 = unqualified_fn_lookup_error (postfix_expression);
7459 }
7460 /* We do not perform argument-dependent lookup if
7461 normal lookup finds a non-function, in accordance
7462 with the expected resolution of DR 218. */
7463 else if (!args->is_empty ()
7464 && is_overloaded_fn (postfix_expression))
7465 {
7466 /* Do not do argument dependent lookup if regular
7467 lookup finds a member function or a block-scope
7468 function declaration. [basic.lookup.argdep]/3 */
7469 bool do_adl_p = true;
7470 tree fns = get_fns (postfix_expression);
7471 for (lkp_iterator iter (fns); iter; ++iter)
7472 {
7473 tree fn = STRIP_TEMPLATE (*iter);
7474 if ((TREE_CODE (fn) == USING_DECL
7475 && DECL_DEPENDENT_P (fn))
7476 || DECL_FUNCTION_MEMBER_P (fn)
7477 || DECL_LOCAL_DECL_P (fn))
7478 {
7479 do_adl_p = false;
7480 break;
7481 }
7482 }
7483
7484 if (do_adl_p)
7485 {
7486 koenig_p = true;
7487 if (!any_type_dependent_arguments_p (args))
7488 postfix_expression
7489 = perform_koenig_lookup (postfix_expression, args,
7490 complain);
7491 }
7492 }
7493 }
7494
7495 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7496 {
7497 tree instance = TREE_OPERAND (postfix_expression, 0);
7498 tree fn = TREE_OPERAND (postfix_expression, 1);
7499
7500 if (processing_template_decl
7501 && (type_dependent_object_expression_p (instance)
7502 || (!BASELINK_P (fn)
7503 && TREE_CODE (fn) != FIELD_DECL)
7504 || type_dependent_expression_p (fn)
7505 || any_type_dependent_arguments_p (args)))
7506 {
7507 maybe_generic_this_capture (instance, fn);
7508 postfix_expression
7509 = build_min_nt_call_vec (postfix_expression, args);
7510 }
7511 else if (BASELINK_P (fn))
7512 {
7513 postfix_expression
7514 = (build_new_method_call
7515 (instance, fn, &args, NULL_TREE,
7516 (idk == CP_ID_KIND_QUALIFIED
7517 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7518 : LOOKUP_NORMAL),
7519 /*fn_p=*/NULL,
7520 complain));
7521 }
7522 else
7523 postfix_expression
7524 = finish_call_expr (postfix_expression, &args,
7525 /*disallow_virtual=*/false,
7526 /*koenig_p=*/false,
7527 complain);
7528 }
7529 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7530 || TREE_CODE (postfix_expression) == MEMBER_REF
7531 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7532 postfix_expression = (build_offset_ref_call_from_tree
7533 (postfix_expression, &args,
7534 complain));
7535 else if (idk == CP_ID_KIND_QUALIFIED)
7536 /* A call to a static class member, or a namespace-scope
7537 function. */
7538 postfix_expression
7539 = finish_call_expr (postfix_expression, &args,
7540 /*disallow_virtual=*/true,
7541 koenig_p,
7542 complain);
7543 else
7544 /* All other function calls. */
7545 postfix_expression
7546 = finish_call_expr (postfix_expression, &args,
7547 /*disallow_virtual=*/false,
7548 koenig_p,
7549 complain);
7550
7551 if (close_paren_loc != UNKNOWN_LOCATION)
7552 {
7553 location_t combined_loc = make_location (token->location,
7554 start_loc,
7555 close_paren_loc);
7556 postfix_expression.set_location (combined_loc);
7557 }
7558
7559 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7560 idk = CP_ID_KIND_NONE;
7561
7562 release_tree_vector (args);
7563 }
7564 break;
7565
7566 case CPP_DOT:
7567 case CPP_DEREF:
7568 /* postfix-expression . template [opt] id-expression
7569 postfix-expression . pseudo-destructor-name
7570 postfix-expression -> template [opt] id-expression
7571 postfix-expression -> pseudo-destructor-name */
7572
7573 /* Consume the `.' or `->' operator. */
7574 cp_lexer_consume_token (parser->lexer);
7575
7576 postfix_expression
7577 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7578 postfix_expression,
7579 false, &idk, loc);
7580
7581 is_member_access = true;
7582 break;
7583
7584 case CPP_PLUS_PLUS:
7585 /* postfix-expression ++ */
7586 /* Consume the `++' token. */
7587 cp_lexer_consume_token (parser->lexer);
7588 /* Generate a representation for the complete expression. */
7589 postfix_expression
7590 = finish_increment_expr (postfix_expression,
7591 POSTINCREMENT_EXPR);
7592 /* Increments may not appear in constant-expressions. */
7593 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7594 postfix_expression = error_mark_node;
7595 idk = CP_ID_KIND_NONE;
7596 is_member_access = false;
7597 break;
7598
7599 case CPP_MINUS_MINUS:
7600 /* postfix-expression -- */
7601 /* Consume the `--' token. */
7602 cp_lexer_consume_token (parser->lexer);
7603 /* Generate a representation for the complete expression. */
7604 postfix_expression
7605 = finish_increment_expr (postfix_expression,
7606 POSTDECREMENT_EXPR);
7607 /* Decrements may not appear in constant-expressions. */
7608 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7609 postfix_expression = error_mark_node;
7610 idk = CP_ID_KIND_NONE;
7611 is_member_access = false;
7612 break;
7613
7614 default:
7615 if (pidk_return != NULL)
7616 * pidk_return = idk;
7617 if (member_access_only_p)
7618 return is_member_access
7619 ? postfix_expression
7620 : cp_expr (error_mark_node);
7621 else
7622 return postfix_expression;
7623 }
7624 }
7625
7626 /* We should never get here. */
7627 gcc_unreachable ();
7628 return error_mark_node;
7629 }
7630
7631 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7632 by cp_parser_builtin_offsetof. We're looking for
7633
7634 postfix-expression [ expression ]
7635 postfix-expression [ braced-init-list ] (C++11)
7636
7637 FOR_OFFSETOF is set if we're being called in that context, which
7638 changes how we deal with integer constant expressions. */
7639
7640 static tree
7641 cp_parser_postfix_open_square_expression (cp_parser *parser,
7642 tree postfix_expression,
7643 bool for_offsetof,
7644 bool decltype_p)
7645 {
7646 tree index = NULL_TREE;
7647 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7648 bool saved_greater_than_is_operator_p;
7649
7650 /* Consume the `[' token. */
7651 cp_lexer_consume_token (parser->lexer);
7652
7653 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7654 parser->greater_than_is_operator_p = true;
7655
7656 /* Parse the index expression. */
7657 /* ??? For offsetof, there is a question of what to allow here. If
7658 offsetof is not being used in an integral constant expression context,
7659 then we *could* get the right answer by computing the value at runtime.
7660 If we are in an integral constant expression context, then we might
7661 could accept any constant expression; hard to say without analysis.
7662 Rather than open the barn door too wide right away, allow only integer
7663 constant expressions here. */
7664 if (for_offsetof)
7665 index = cp_parser_constant_expression (parser);
7666 else
7667 {
7668 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7669 {
7670 bool expr_nonconst_p;
7671 cp_lexer_set_source_position (parser->lexer);
7672 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7673 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7674 }
7675 else
7676 index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
7677 /*decltype_p=*/false,
7678 /*warn_comma_p=*/warn_comma_subscript);
7679 }
7680
7681 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7682
7683 /* Look for the closing `]'. */
7684 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7685
7686 /* Build the ARRAY_REF. */
7687 postfix_expression = grok_array_decl (loc, postfix_expression,
7688 index, decltype_p);
7689
7690 /* When not doing offsetof, array references are not permitted in
7691 constant-expressions. */
7692 if (!for_offsetof
7693 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7694 postfix_expression = error_mark_node;
7695
7696 return postfix_expression;
7697 }
7698
7699 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7700 dereference of incomplete type, returns true if error_mark_node should
7701 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7702 and *DEPENDENT_P. */
7703
7704 bool
7705 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7706 bool *dependent_p)
7707 {
7708 /* In a template, be permissive by treating an object expression
7709 of incomplete type as dependent (after a pedwarn). */
7710 diagnostic_t kind = (processing_template_decl
7711 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7712
7713 switch (TREE_CODE (*postfix_expression))
7714 {
7715 case CAST_EXPR:
7716 case REINTERPRET_CAST_EXPR:
7717 case CONST_CAST_EXPR:
7718 case STATIC_CAST_EXPR:
7719 case DYNAMIC_CAST_EXPR:
7720 case IMPLICIT_CONV_EXPR:
7721 case VIEW_CONVERT_EXPR:
7722 case NON_LVALUE_EXPR:
7723 kind = DK_ERROR;
7724 break;
7725 case OVERLOAD:
7726 /* Don't emit any diagnostic for OVERLOADs. */
7727 kind = DK_IGNORED;
7728 break;
7729 default:
7730 /* Avoid clobbering e.g. DECLs. */
7731 if (!EXPR_P (*postfix_expression))
7732 kind = DK_ERROR;
7733 break;
7734 }
7735
7736 if (kind == DK_IGNORED)
7737 return false;
7738
7739 location_t exploc = location_of (*postfix_expression);
7740 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7741 if (!MAYBE_CLASS_TYPE_P (*scope))
7742 return true;
7743 if (kind == DK_ERROR)
7744 *scope = *postfix_expression = error_mark_node;
7745 else if (processing_template_decl)
7746 {
7747 *dependent_p = true;
7748 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7749 }
7750 return false;
7751 }
7752
7753 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7754 by cp_parser_builtin_offsetof. We're looking for
7755
7756 postfix-expression . template [opt] id-expression
7757 postfix-expression . pseudo-destructor-name
7758 postfix-expression -> template [opt] id-expression
7759 postfix-expression -> pseudo-destructor-name
7760
7761 FOR_OFFSETOF is set if we're being called in that context. That sorta
7762 limits what of the above we'll actually accept, but nevermind.
7763 TOKEN_TYPE is the "." or "->" token, which will already have been
7764 removed from the stream. */
7765
7766 static tree
7767 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7768 enum cpp_ttype token_type,
7769 cp_expr postfix_expression,
7770 bool for_offsetof, cp_id_kind *idk,
7771 location_t location)
7772 {
7773 tree name;
7774 bool dependent_p;
7775 bool pseudo_destructor_p;
7776 tree scope = NULL_TREE;
7777 location_t start_loc = postfix_expression.get_start ();
7778
7779 /* If this is a `->' operator, dereference the pointer. */
7780 if (token_type == CPP_DEREF)
7781 postfix_expression = build_x_arrow (location, postfix_expression,
7782 tf_warning_or_error);
7783 /* Check to see whether or not the expression is type-dependent and
7784 not the current instantiation. */
7785 dependent_p = type_dependent_object_expression_p (postfix_expression);
7786 /* The identifier following the `->' or `.' is not qualified. */
7787 parser->scope = NULL_TREE;
7788 parser->qualifying_scope = NULL_TREE;
7789 parser->object_scope = NULL_TREE;
7790 *idk = CP_ID_KIND_NONE;
7791
7792 /* Enter the scope corresponding to the type of the object
7793 given by the POSTFIX_EXPRESSION. */
7794 if (!dependent_p)
7795 {
7796 scope = TREE_TYPE (postfix_expression);
7797 /* According to the standard, no expression should ever have
7798 reference type. Unfortunately, we do not currently match
7799 the standard in this respect in that our internal representation
7800 of an expression may have reference type even when the standard
7801 says it does not. Therefore, we have to manually obtain the
7802 underlying type here. */
7803 scope = non_reference (scope);
7804 /* The type of the POSTFIX_EXPRESSION must be complete. */
7805 /* Unlike the object expression in other contexts, *this is not
7806 required to be of complete type for purposes of class member
7807 access (5.2.5) outside the member function body. */
7808 if (postfix_expression != current_class_ref
7809 && scope != error_mark_node
7810 && !currently_open_class (scope))
7811 {
7812 scope = complete_type (scope);
7813 if (!COMPLETE_TYPE_P (scope)
7814 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7815 &dependent_p))
7816 return error_mark_node;
7817 }
7818
7819 if (!dependent_p)
7820 {
7821 /* Let the name lookup machinery know that we are processing a
7822 class member access expression. */
7823 parser->context->object_type = scope;
7824 /* If something went wrong, we want to be able to discern that case,
7825 as opposed to the case where there was no SCOPE due to the type
7826 of expression being dependent. */
7827 if (!scope)
7828 scope = error_mark_node;
7829 /* If the SCOPE was erroneous, make the various semantic analysis
7830 functions exit quickly -- and without issuing additional error
7831 messages. */
7832 if (scope == error_mark_node)
7833 postfix_expression = error_mark_node;
7834 }
7835 }
7836
7837 if (dependent_p)
7838 {
7839 tree type = TREE_TYPE (postfix_expression);
7840 /* If we don't have a (type-dependent) object of class type, use
7841 typeof to figure out the type of the object. */
7842 if (type == NULL_TREE)
7843 type = finish_typeof (postfix_expression);
7844 parser->context->object_type = type;
7845 }
7846
7847 /* Assume this expression is not a pseudo-destructor access. */
7848 pseudo_destructor_p = false;
7849
7850 /* If the SCOPE is a scalar type, then, if this is a valid program,
7851 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7852 is type dependent, it can be pseudo-destructor-name or something else.
7853 Try to parse it as pseudo-destructor-name first. */
7854 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7855 {
7856 tree s;
7857 tree type;
7858
7859 cp_parser_parse_tentatively (parser);
7860 /* Parse the pseudo-destructor-name. */
7861 s = NULL_TREE;
7862 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7863 &s, &type);
7864 if (dependent_p
7865 && (cp_parser_error_occurred (parser)
7866 || !SCALAR_TYPE_P (type)))
7867 cp_parser_abort_tentative_parse (parser);
7868 else if (cp_parser_parse_definitely (parser))
7869 {
7870 pseudo_destructor_p = true;
7871 postfix_expression
7872 = finish_pseudo_destructor_expr (postfix_expression,
7873 s, type, location);
7874 }
7875 }
7876
7877 if (!pseudo_destructor_p)
7878 {
7879 /* If the SCOPE is not a scalar type, we are looking at an
7880 ordinary class member access expression, rather than a
7881 pseudo-destructor-name. */
7882 bool template_p;
7883 cp_token *token = cp_lexer_peek_token (parser->lexer);
7884 /* Parse the id-expression. */
7885 name = (cp_parser_id_expression
7886 (parser,
7887 cp_parser_optional_template_keyword (parser),
7888 /*check_dependency_p=*/true,
7889 &template_p,
7890 /*declarator_p=*/false,
7891 /*optional_p=*/false));
7892 /* In general, build a SCOPE_REF if the member name is qualified.
7893 However, if the name was not dependent and has already been
7894 resolved; there is no need to build the SCOPE_REF. For example;
7895
7896 struct X { void f(); };
7897 template <typename T> void f(T* t) { t->X::f(); }
7898
7899 Even though "t" is dependent, "X::f" is not and has been resolved
7900 to a BASELINK; there is no need to include scope information. */
7901
7902 /* But we do need to remember that there was an explicit scope for
7903 virtual function calls. */
7904 if (parser->scope)
7905 *idk = CP_ID_KIND_QUALIFIED;
7906
7907 /* If the name is a template-id that names a type, we will get a
7908 TYPE_DECL here. That is invalid code. */
7909 if (TREE_CODE (name) == TYPE_DECL)
7910 {
7911 error_at (token->location, "invalid use of %qD", name);
7912 postfix_expression = error_mark_node;
7913 }
7914 else
7915 {
7916 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7917 {
7918 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7919 {
7920 error_at (token->location, "%<%D::%D%> is not a class member",
7921 parser->scope, name);
7922 postfix_expression = error_mark_node;
7923 }
7924 else
7925 name = build_qualified_name (/*type=*/NULL_TREE,
7926 parser->scope,
7927 name,
7928 template_p);
7929 parser->scope = NULL_TREE;
7930 parser->qualifying_scope = NULL_TREE;
7931 parser->object_scope = NULL_TREE;
7932 }
7933 if (parser->scope && name && BASELINK_P (name))
7934 adjust_result_of_qualified_name_lookup
7935 (name, parser->scope, scope);
7936 postfix_expression
7937 = finish_class_member_access_expr (postfix_expression, name,
7938 template_p,
7939 tf_warning_or_error);
7940 /* Build a location e.g.:
7941 ptr->access_expr
7942 ~~~^~~~~~~~~~~~~
7943 where the caret is at the deref token, ranging from
7944 the start of postfix_expression to the end of the access expr. */
7945 location_t combined_loc
7946 = make_location (input_location, start_loc, parser->lexer);
7947 protected_set_expr_location (postfix_expression, combined_loc);
7948 }
7949 }
7950
7951 /* We no longer need to look up names in the scope of the object on
7952 the left-hand side of the `.' or `->' operator. */
7953 parser->context->object_type = NULL_TREE;
7954
7955 /* Outside of offsetof, these operators may not appear in
7956 constant-expressions. */
7957 if (!for_offsetof
7958 && (cp_parser_non_integral_constant_expression
7959 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7960 postfix_expression = error_mark_node;
7961
7962 return postfix_expression;
7963 }
7964
7965 /* Parse a parenthesized expression-list.
7966
7967 expression-list:
7968 assignment-expression
7969 expression-list, assignment-expression
7970
7971 attribute-list:
7972 expression-list
7973 identifier
7974 identifier, expression-list
7975
7976 CAST_P is true if this expression is the target of a cast.
7977
7978 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7979 argument pack.
7980
7981 WRAP_LOCATIONS_P is true if expressions within this list for which
7982 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7983 their source locations.
7984
7985 Returns a vector of trees. Each element is a representation of an
7986 assignment-expression. NULL is returned if the ( and or ) are
7987 missing. An empty, but allocated, vector is returned on no
7988 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7989 if we are parsing an attribute list for an attribute that wants a
7990 plain identifier argument, normal_attr for an attribute that wants
7991 an expression, or non_attr if we aren't parsing an attribute list. If
7992 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7993 not all of the expressions in the list were constant.
7994 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7995 will be written to with the location of the closing parenthesis. If
7996 an error occurs, it may or may not be written to. */
7997
7998 static vec<tree, va_gc> *
7999 cp_parser_parenthesized_expression_list (cp_parser* parser,
8000 int is_attribute_list,
8001 bool cast_p,
8002 bool allow_expansion_p,
8003 bool *non_constant_p,
8004 location_t *close_paren_loc,
8005 bool wrap_locations_p)
8006 {
8007 vec<tree, va_gc> *expression_list;
8008 bool fold_expr_p = is_attribute_list != non_attr;
8009 tree identifier = NULL_TREE;
8010 bool saved_greater_than_is_operator_p;
8011
8012 /* Assume all the expressions will be constant. */
8013 if (non_constant_p)
8014 *non_constant_p = false;
8015
8016 matching_parens parens;
8017 if (!parens.require_open (parser))
8018 return NULL;
8019
8020 expression_list = make_tree_vector ();
8021
8022 /* Within a parenthesized expression, a `>' token is always
8023 the greater-than operator. */
8024 saved_greater_than_is_operator_p
8025 = parser->greater_than_is_operator_p;
8026 parser->greater_than_is_operator_p = true;
8027
8028 cp_expr expr (NULL_TREE);
8029
8030 /* Consume expressions until there are no more. */
8031 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8032 while (true)
8033 {
8034 /* At the beginning of attribute lists, check to see if the
8035 next token is an identifier. */
8036 if (is_attribute_list == id_attr
8037 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
8038 {
8039 cp_token *token;
8040
8041 /* Consume the identifier. */
8042 token = cp_lexer_consume_token (parser->lexer);
8043 /* Save the identifier. */
8044 identifier = token->u.value;
8045 }
8046 else
8047 {
8048 bool expr_non_constant_p;
8049
8050 /* Parse the next assignment-expression. */
8051 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8052 {
8053 /* A braced-init-list. */
8054 cp_lexer_set_source_position (parser->lexer);
8055 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8056 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8057 if (non_constant_p && expr_non_constant_p)
8058 *non_constant_p = true;
8059 }
8060 else if (non_constant_p)
8061 {
8062 expr = (cp_parser_constant_expression
8063 (parser, /*allow_non_constant_p=*/true,
8064 &expr_non_constant_p));
8065 if (expr_non_constant_p)
8066 *non_constant_p = true;
8067 }
8068 else
8069 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
8070 cast_p);
8071
8072 if (fold_expr_p)
8073 expr = instantiate_non_dependent_expr (expr);
8074
8075 /* If we have an ellipsis, then this is an expression
8076 expansion. */
8077 if (allow_expansion_p
8078 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8079 {
8080 /* Consume the `...'. */
8081 cp_lexer_consume_token (parser->lexer);
8082
8083 /* Build the argument pack. */
8084 expr = make_pack_expansion (expr);
8085 }
8086
8087 if (wrap_locations_p)
8088 expr.maybe_add_location_wrapper ();
8089
8090 /* Add it to the list. We add error_mark_node
8091 expressions to the list, so that we can still tell if
8092 the correct form for a parenthesized expression-list
8093 is found. That gives better errors. */
8094 vec_safe_push (expression_list, expr.get_value ());
8095
8096 if (expr == error_mark_node)
8097 goto skip_comma;
8098 }
8099
8100 /* After the first item, attribute lists look the same as
8101 expression lists. */
8102 is_attribute_list = non_attr;
8103
8104 get_comma:;
8105 /* If the next token isn't a `,', then we are done. */
8106 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8107 break;
8108
8109 /* Otherwise, consume the `,' and keep going. */
8110 cp_lexer_consume_token (parser->lexer);
8111 }
8112
8113 if (close_paren_loc)
8114 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
8115
8116 if (!parens.require_close (parser))
8117 {
8118 int ending;
8119
8120 skip_comma:;
8121 /* We try and resync to an unnested comma, as that will give the
8122 user better diagnostics. */
8123 ending = cp_parser_skip_to_closing_parenthesis (parser,
8124 /*recovering=*/true,
8125 /*or_comma=*/true,
8126 /*consume_paren=*/true);
8127 if (ending < 0)
8128 goto get_comma;
8129 if (!ending)
8130 {
8131 parser->greater_than_is_operator_p
8132 = saved_greater_than_is_operator_p;
8133 return NULL;
8134 }
8135 }
8136
8137 parser->greater_than_is_operator_p
8138 = saved_greater_than_is_operator_p;
8139
8140 if (identifier)
8141 vec_safe_insert (expression_list, 0, identifier);
8142
8143 return expression_list;
8144 }
8145
8146 /* Parse a pseudo-destructor-name.
8147
8148 pseudo-destructor-name:
8149 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8150 :: [opt] nested-name-specifier template template-id :: ~ type-name
8151 :: [opt] nested-name-specifier [opt] ~ type-name
8152
8153 If either of the first two productions is used, sets *SCOPE to the
8154 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8155 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8156 or ERROR_MARK_NODE if the parse fails. */
8157
8158 static void
8159 cp_parser_pseudo_destructor_name (cp_parser* parser,
8160 tree object,
8161 tree* scope,
8162 tree* type)
8163 {
8164 bool nested_name_specifier_p;
8165
8166 /* Handle ~auto. */
8167 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8168 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8169 && !type_dependent_expression_p (object))
8170 {
8171 if (cxx_dialect < cxx14)
8172 pedwarn (input_location, 0,
8173 "%<~auto%> only available with "
8174 "%<-std=c++14%> or %<-std=gnu++14%>");
8175 cp_lexer_consume_token (parser->lexer);
8176 cp_lexer_consume_token (parser->lexer);
8177 *scope = NULL_TREE;
8178 *type = TREE_TYPE (object);
8179 return;
8180 }
8181
8182 /* Assume that things will not work out. */
8183 *type = error_mark_node;
8184
8185 /* Look for the optional `::' operator. */
8186 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8187 /* Look for the optional nested-name-specifier. */
8188 nested_name_specifier_p
8189 = (cp_parser_nested_name_specifier_opt (parser,
8190 /*typename_keyword_p=*/false,
8191 /*check_dependency_p=*/true,
8192 /*type_p=*/false,
8193 /*is_declaration=*/false)
8194 != NULL_TREE);
8195 /* Now, if we saw a nested-name-specifier, we might be doing the
8196 second production. */
8197 if (nested_name_specifier_p
8198 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8199 {
8200 /* Consume the `template' keyword. */
8201 cp_lexer_consume_token (parser->lexer);
8202 /* Parse the template-id. */
8203 cp_parser_template_id (parser,
8204 /*template_keyword_p=*/true,
8205 /*check_dependency_p=*/false,
8206 class_type,
8207 /*is_declaration=*/true);
8208 /* Look for the `::' token. */
8209 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8210 }
8211 /* If the next token is not a `~', then there might be some
8212 additional qualification. */
8213 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8214 {
8215 /* At this point, we're looking for "type-name :: ~". The type-name
8216 must not be a class-name, since this is a pseudo-destructor. So,
8217 it must be either an enum-name, or a typedef-name -- both of which
8218 are just identifiers. So, we peek ahead to check that the "::"
8219 and "~" tokens are present; if they are not, then we can avoid
8220 calling type_name. */
8221 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8222 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8223 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8224 {
8225 cp_parser_error (parser, "non-scalar type");
8226 return;
8227 }
8228
8229 /* Look for the type-name. */
8230 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8231 if (*scope == error_mark_node)
8232 return;
8233
8234 /* Look for the `::' token. */
8235 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8236 }
8237 else
8238 *scope = NULL_TREE;
8239
8240 /* Look for the `~'. */
8241 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8242
8243 /* Once we see the ~, this has to be a pseudo-destructor. */
8244 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8245 cp_parser_commit_to_topmost_tentative_parse (parser);
8246
8247 /* Look for the type-name again. We are not responsible for
8248 checking that it matches the first type-name. */
8249 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8250 }
8251
8252 /* Parse a unary-expression.
8253
8254 unary-expression:
8255 postfix-expression
8256 ++ cast-expression
8257 -- cast-expression
8258 await-expression
8259 unary-operator cast-expression
8260 sizeof unary-expression
8261 sizeof ( type-id )
8262 alignof ( type-id ) [C++0x]
8263 new-expression
8264 delete-expression
8265
8266 GNU Extensions:
8267
8268 unary-expression:
8269 __extension__ cast-expression
8270 __alignof__ unary-expression
8271 __alignof__ ( type-id )
8272 alignof unary-expression [C++0x]
8273 __real__ cast-expression
8274 __imag__ cast-expression
8275 && identifier
8276 sizeof ( type-id ) { initializer-list , [opt] }
8277 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8278 __alignof__ ( type-id ) { initializer-list , [opt] }
8279
8280 ADDRESS_P is true iff the unary-expression is appearing as the
8281 operand of the `&' operator. CAST_P is true if this expression is
8282 the target of a cast.
8283
8284 Returns a representation of the expression. */
8285
8286 static cp_expr
8287 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8288 bool address_p, bool cast_p, bool decltype_p)
8289 {
8290 cp_token *token;
8291 enum tree_code unary_operator;
8292
8293 /* Peek at the next token. */
8294 token = cp_lexer_peek_token (parser->lexer);
8295 /* Some keywords give away the kind of expression. */
8296 if (token->type == CPP_KEYWORD)
8297 {
8298 enum rid keyword = token->keyword;
8299
8300 switch (keyword)
8301 {
8302 case RID_ALIGNOF:
8303 case RID_SIZEOF:
8304 {
8305 tree operand, ret;
8306 enum tree_code op;
8307 location_t start_loc = token->location;
8308
8309 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8310 bool std_alignof = id_equal (token->u.value, "alignof");
8311
8312 /* Consume the token. */
8313 cp_lexer_consume_token (parser->lexer);
8314 /* Parse the operand. */
8315 operand = cp_parser_sizeof_operand (parser, keyword);
8316
8317 /* Construct a location e.g. :
8318 alignof (expr)
8319 ^~~~~~~~~~~~~~
8320 with start == caret at the start of the "alignof"/"sizeof"
8321 token, with the endpoint at the final closing paren. */
8322 location_t compound_loc
8323 = make_location (start_loc, start_loc, parser->lexer);
8324
8325 if (TYPE_P (operand))
8326 ret = cxx_sizeof_or_alignof_type (compound_loc, operand, op,
8327 std_alignof, true);
8328 else
8329 {
8330 /* ISO C++ defines alignof only with types, not with
8331 expressions. So pedwarn if alignof is used with a non-
8332 type expression. However, __alignof__ is ok. */
8333 if (std_alignof)
8334 pedwarn (token->location, OPT_Wpedantic,
8335 "ISO C++ does not allow %<alignof%> "
8336 "with a non-type");
8337
8338 ret = cxx_sizeof_or_alignof_expr (compound_loc, operand, op,
8339 std_alignof, true);
8340 }
8341 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8342 SIZEOF_EXPR with the original operand. */
8343 if (op == SIZEOF_EXPR && ret != error_mark_node)
8344 {
8345 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8346 {
8347 if (!processing_template_decl && TYPE_P (operand))
8348 {
8349 ret = build_min (SIZEOF_EXPR, size_type_node,
8350 build1 (NOP_EXPR, operand,
8351 error_mark_node));
8352 SIZEOF_EXPR_TYPE_P (ret) = 1;
8353 }
8354 else
8355 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8356 TREE_SIDE_EFFECTS (ret) = 0;
8357 TREE_READONLY (ret) = 1;
8358 SET_EXPR_LOCATION (ret, compound_loc);
8359 }
8360 }
8361
8362 cp_expr ret_expr (ret, compound_loc);
8363 ret_expr = ret_expr.maybe_add_location_wrapper ();
8364 return ret_expr;
8365 }
8366
8367 case RID_BUILTIN_HAS_ATTRIBUTE:
8368 return cp_parser_has_attribute_expression (parser);
8369
8370 case RID_NEW:
8371 return cp_parser_new_expression (parser);
8372
8373 case RID_DELETE:
8374 return cp_parser_delete_expression (parser);
8375
8376 case RID_EXTENSION:
8377 {
8378 /* The saved value of the PEDANTIC flag. */
8379 int saved_pedantic;
8380 tree expr;
8381
8382 /* Save away the PEDANTIC flag. */
8383 cp_parser_extension_opt (parser, &saved_pedantic);
8384 /* Parse the cast-expression. */
8385 expr = cp_parser_simple_cast_expression (parser);
8386 /* Restore the PEDANTIC flag. */
8387 pedantic = saved_pedantic;
8388
8389 return expr;
8390 }
8391
8392 case RID_REALPART:
8393 case RID_IMAGPART:
8394 {
8395 tree expression;
8396
8397 /* Consume the `__real__' or `__imag__' token. */
8398 cp_lexer_consume_token (parser->lexer);
8399 /* Parse the cast-expression. */
8400 expression = cp_parser_simple_cast_expression (parser);
8401 /* Create the complete representation. */
8402 return build_x_unary_op (token->location,
8403 (keyword == RID_REALPART
8404 ? REALPART_EXPR : IMAGPART_EXPR),
8405 expression,
8406 tf_warning_or_error);
8407 }
8408 break;
8409
8410 case RID_TRANSACTION_ATOMIC:
8411 case RID_TRANSACTION_RELAXED:
8412 return cp_parser_transaction_expression (parser, keyword);
8413
8414 case RID_NOEXCEPT:
8415 {
8416 tree expr;
8417 const char *saved_message;
8418 bool saved_integral_constant_expression_p;
8419 bool saved_non_integral_constant_expression_p;
8420 bool saved_greater_than_is_operator_p;
8421
8422 location_t start_loc = token->location;
8423
8424 cp_lexer_consume_token (parser->lexer);
8425 matching_parens parens;
8426 parens.require_open (parser);
8427
8428 saved_message = parser->type_definition_forbidden_message;
8429 parser->type_definition_forbidden_message
8430 = G_("types may not be defined in %<noexcept%> expressions");
8431
8432 saved_integral_constant_expression_p
8433 = parser->integral_constant_expression_p;
8434 saved_non_integral_constant_expression_p
8435 = parser->non_integral_constant_expression_p;
8436 parser->integral_constant_expression_p = false;
8437
8438 saved_greater_than_is_operator_p
8439 = parser->greater_than_is_operator_p;
8440 parser->greater_than_is_operator_p = true;
8441
8442 ++cp_unevaluated_operand;
8443 ++c_inhibit_evaluation_warnings;
8444 ++cp_noexcept_operand;
8445 expr = cp_parser_expression (parser);
8446 --cp_noexcept_operand;
8447 --c_inhibit_evaluation_warnings;
8448 --cp_unevaluated_operand;
8449
8450 parser->greater_than_is_operator_p
8451 = saved_greater_than_is_operator_p;
8452
8453 parser->integral_constant_expression_p
8454 = saved_integral_constant_expression_p;
8455 parser->non_integral_constant_expression_p
8456 = saved_non_integral_constant_expression_p;
8457
8458 parser->type_definition_forbidden_message = saved_message;
8459
8460 parens.require_close (parser);
8461
8462 /* Construct a location of the form:
8463 noexcept (expr)
8464 ^~~~~~~~~~~~~~~
8465 with start == caret, finishing at the close-paren. */
8466 location_t noexcept_loc
8467 = make_location (start_loc, start_loc, parser->lexer);
8468
8469 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8470 noexcept_loc);
8471 }
8472
8473 case RID_CO_AWAIT:
8474 {
8475 tree expr;
8476 location_t kw_loc = token->location;
8477
8478 /* Consume the `co_await' token. */
8479 cp_lexer_consume_token (parser->lexer);
8480 /* Parse its cast-expression. */
8481 expr = cp_parser_simple_cast_expression (parser);
8482 if (expr == error_mark_node)
8483 return error_mark_node;
8484
8485 /* Handle [expr.await]. */
8486 return cp_expr (finish_co_await_expr (kw_loc, expr));
8487 }
8488
8489 default:
8490 break;
8491 }
8492 }
8493
8494 /* Look for the `:: new' and `:: delete', which also signal the
8495 beginning of a new-expression, or delete-expression,
8496 respectively. If the next token is `::', then it might be one of
8497 these. */
8498 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8499 {
8500 enum rid keyword;
8501
8502 /* See if the token after the `::' is one of the keywords in
8503 which we're interested. */
8504 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8505 /* If it's `new', we have a new-expression. */
8506 if (keyword == RID_NEW)
8507 return cp_parser_new_expression (parser);
8508 /* Similarly, for `delete'. */
8509 else if (keyword == RID_DELETE)
8510 return cp_parser_delete_expression (parser);
8511 }
8512
8513 /* Look for a unary operator. */
8514 unary_operator = cp_parser_unary_operator (token);
8515 /* The `++' and `--' operators can be handled similarly, even though
8516 they are not technically unary-operators in the grammar. */
8517 if (unary_operator == ERROR_MARK)
8518 {
8519 if (token->type == CPP_PLUS_PLUS)
8520 unary_operator = PREINCREMENT_EXPR;
8521 else if (token->type == CPP_MINUS_MINUS)
8522 unary_operator = PREDECREMENT_EXPR;
8523 /* Handle the GNU address-of-label extension. */
8524 else if (cp_parser_allow_gnu_extensions_p (parser)
8525 && token->type == CPP_AND_AND)
8526 {
8527 tree identifier;
8528 tree expression;
8529 location_t start_loc = token->location;
8530
8531 /* Consume the '&&' token. */
8532 cp_lexer_consume_token (parser->lexer);
8533 /* Look for the identifier. */
8534 identifier = cp_parser_identifier (parser);
8535 /* Construct a location of the form:
8536 &&label
8537 ^~~~~~~
8538 with caret==start at the "&&", finish at the end of the label. */
8539 location_t combined_loc
8540 = make_location (start_loc, start_loc, parser->lexer);
8541 /* Create an expression representing the address. */
8542 expression = finish_label_address_expr (identifier, combined_loc);
8543 if (cp_parser_non_integral_constant_expression (parser,
8544 NIC_ADDR_LABEL))
8545 expression = error_mark_node;
8546 return expression;
8547 }
8548 }
8549 if (unary_operator != ERROR_MARK)
8550 {
8551 cp_expr cast_expression;
8552 cp_expr expression = error_mark_node;
8553 non_integral_constant non_constant_p = NIC_NONE;
8554 location_t loc = token->location;
8555 tsubst_flags_t complain = complain_flags (decltype_p);
8556
8557 /* Consume the operator token. */
8558 token = cp_lexer_consume_token (parser->lexer);
8559 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8560
8561 /* Parse the cast-expression. */
8562 cast_expression
8563 = cp_parser_cast_expression (parser,
8564 unary_operator == ADDR_EXPR,
8565 /*cast_p=*/false,
8566 /*decltype*/false,
8567 pidk);
8568
8569 /* Make a location:
8570 OP_TOKEN CAST_EXPRESSION
8571 ^~~~~~~~~~~~~~~~~~~~~~~~~
8572 with start==caret at the operator token, and
8573 extending to the end of the cast_expression. */
8574 loc = make_location (loc, loc, cast_expression.get_finish ());
8575
8576 /* Now, build an appropriate representation. */
8577 switch (unary_operator)
8578 {
8579 case INDIRECT_REF:
8580 non_constant_p = NIC_STAR;
8581 expression = build_x_indirect_ref (loc, cast_expression,
8582 RO_UNARY_STAR,
8583 complain);
8584 /* TODO: build_x_indirect_ref does not always honor the
8585 location, so ensure it is set. */
8586 expression.set_location (loc);
8587 break;
8588
8589 case ADDR_EXPR:
8590 non_constant_p = NIC_ADDR;
8591 /* Fall through. */
8592 case BIT_NOT_EXPR:
8593 expression = build_x_unary_op (loc, unary_operator,
8594 cast_expression,
8595 complain);
8596 /* TODO: build_x_unary_op does not always honor the location,
8597 so ensure it is set. */
8598 expression.set_location (loc);
8599 break;
8600
8601 case PREINCREMENT_EXPR:
8602 case PREDECREMENT_EXPR:
8603 non_constant_p = unary_operator == PREINCREMENT_EXPR
8604 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8605 /* Fall through. */
8606 case NEGATE_EXPR:
8607 /* Immediately fold negation of a constant, unless the constant is 0
8608 (since -0 == 0) or it would overflow. */
8609 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8610 {
8611 tree stripped_expr
8612 = tree_strip_any_location_wrapper (cast_expression);
8613 if (CONSTANT_CLASS_P (stripped_expr)
8614 && !integer_zerop (stripped_expr)
8615 && !TREE_OVERFLOW (stripped_expr))
8616 {
8617 tree folded = fold_build1 (unary_operator,
8618 TREE_TYPE (stripped_expr),
8619 stripped_expr);
8620 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8621 {
8622 expression = maybe_wrap_with_location (folded, loc);
8623 break;
8624 }
8625 }
8626 }
8627 /* Fall through. */
8628 case UNARY_PLUS_EXPR:
8629 case TRUTH_NOT_EXPR:
8630 expression = finish_unary_op_expr (loc, unary_operator,
8631 cast_expression, complain);
8632 break;
8633
8634 default:
8635 gcc_unreachable ();
8636 }
8637
8638 if (non_constant_p != NIC_NONE
8639 && cp_parser_non_integral_constant_expression (parser,
8640 non_constant_p))
8641 expression = error_mark_node;
8642
8643 return expression;
8644 }
8645
8646 return cp_parser_postfix_expression (parser, address_p, cast_p,
8647 /*member_access_only_p=*/false,
8648 decltype_p,
8649 pidk);
8650 }
8651
8652 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8653 unary-operator, the corresponding tree code is returned. */
8654
8655 static enum tree_code
8656 cp_parser_unary_operator (cp_token* token)
8657 {
8658 switch (token->type)
8659 {
8660 case CPP_MULT:
8661 return INDIRECT_REF;
8662
8663 case CPP_AND:
8664 return ADDR_EXPR;
8665
8666 case CPP_PLUS:
8667 return UNARY_PLUS_EXPR;
8668
8669 case CPP_MINUS:
8670 return NEGATE_EXPR;
8671
8672 case CPP_NOT:
8673 return TRUTH_NOT_EXPR;
8674
8675 case CPP_COMPL:
8676 return BIT_NOT_EXPR;
8677
8678 default:
8679 return ERROR_MARK;
8680 }
8681 }
8682
8683 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8684 Returns a representation of the expression. */
8685
8686 static tree
8687 cp_parser_has_attribute_expression (cp_parser *parser)
8688 {
8689 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8690
8691 /* Consume the __builtin_has_attribute token. */
8692 cp_lexer_consume_token (parser->lexer);
8693
8694 matching_parens parens;
8695 if (!parens.require_open (parser))
8696 return error_mark_node;
8697
8698 /* Types cannot be defined in a `sizeof' expression. Save away the
8699 old message. */
8700 const char *saved_message = parser->type_definition_forbidden_message;
8701 const char *saved_message_arg
8702 = parser->type_definition_forbidden_message_arg;
8703 parser->type_definition_forbidden_message
8704 = G_("types may not be defined in %qs expressions");
8705 parser->type_definition_forbidden_message_arg
8706 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
8707
8708 /* The restrictions on constant-expressions do not apply inside
8709 sizeof expressions. */
8710 bool saved_integral_constant_expression_p
8711 = parser->integral_constant_expression_p;
8712 bool saved_non_integral_constant_expression_p
8713 = parser->non_integral_constant_expression_p;
8714 parser->integral_constant_expression_p = false;
8715
8716 /* Do not actually evaluate the expression. */
8717 ++cp_unevaluated_operand;
8718 ++c_inhibit_evaluation_warnings;
8719
8720 tree oper = NULL_TREE;
8721
8722 /* We can't be sure yet whether we're looking at a type-id or an
8723 expression. */
8724 cp_parser_parse_tentatively (parser);
8725
8726 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8727 parser->in_type_id_in_expr_p = true;
8728 /* Look for the type-id. */
8729 oper = cp_parser_type_id (parser);
8730 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8731
8732 cp_parser_parse_definitely (parser);
8733
8734 /* If the type-id production did not work out, then we must be
8735 looking at an expression. */
8736 if (!oper || oper == error_mark_node)
8737 oper = cp_parser_assignment_expression (parser);
8738
8739 STRIP_ANY_LOCATION_WRAPPER (oper);
8740
8741 /* Go back to evaluating expressions. */
8742 --cp_unevaluated_operand;
8743 --c_inhibit_evaluation_warnings;
8744
8745 /* And restore the old one. */
8746 parser->type_definition_forbidden_message = saved_message;
8747 parser->type_definition_forbidden_message_arg = saved_message_arg;
8748 parser->integral_constant_expression_p
8749 = saved_integral_constant_expression_p;
8750 parser->non_integral_constant_expression_p
8751 = saved_non_integral_constant_expression_p;
8752
8753 /* Consume the comma if it's there. */
8754 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
8755 {
8756 cp_parser_skip_to_closing_parenthesis (parser, false, false,
8757 /*consume_paren=*/true);
8758 return error_mark_node;
8759 }
8760
8761 /* Parse the attribute specification. */
8762 bool ret = false;
8763 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
8764 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
8765 {
8766 if (oper == error_mark_node)
8767 /* Nothing. */;
8768 else if (type_dependent_expression_p (oper))
8769 sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
8770 "not supported yet");
8771 else
8772 {
8773 /* Fold constant expressions used in attributes first. */
8774 cp_check_const_attributes (attr);
8775
8776 /* Finally, see if OPER has been declared with ATTR. */
8777 ret = has_attribute (atloc, oper, attr, default_conversion);
8778 }
8779
8780 parens.require_close (parser);
8781 }
8782 else
8783 {
8784 error_at (atloc, "expected identifier");
8785 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8786 }
8787
8788 /* Construct a location e.g. :
8789 __builtin_has_attribute (oper, attr)
8790 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8791 with start == caret at the start of the built-in token,
8792 and with the endpoint at the final closing paren. */
8793 location_t compound_loc
8794 = make_location (start_loc, start_loc, parser->lexer);
8795
8796 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
8797 ret_expr.set_location (compound_loc);
8798 ret_expr = ret_expr.maybe_add_location_wrapper ();
8799 return ret_expr;
8800 }
8801
8802 /* Parse a new-expression.
8803
8804 new-expression:
8805 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8806 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8807
8808 Returns a representation of the expression. */
8809
8810 static tree
8811 cp_parser_new_expression (cp_parser* parser)
8812 {
8813 bool global_scope_p;
8814 vec<tree, va_gc> *placement;
8815 tree type;
8816 vec<tree, va_gc> *initializer;
8817 tree nelts = NULL_TREE;
8818 tree ret;
8819
8820 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8821
8822 /* Look for the optional `::' operator. */
8823 global_scope_p
8824 = (cp_parser_global_scope_opt (parser,
8825 /*current_scope_valid_p=*/false)
8826 != NULL_TREE);
8827 /* Look for the `new' operator. */
8828 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8829 /* There's no easy way to tell a new-placement from the
8830 `( type-id )' construct. */
8831 cp_parser_parse_tentatively (parser);
8832 /* Look for a new-placement. */
8833 placement = cp_parser_new_placement (parser);
8834 /* If that didn't work out, there's no new-placement. */
8835 if (!cp_parser_parse_definitely (parser))
8836 {
8837 if (placement != NULL)
8838 release_tree_vector (placement);
8839 placement = NULL;
8840 }
8841
8842 /* If the next token is a `(', then we have a parenthesized
8843 type-id. */
8844 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8845 {
8846 cp_token *token;
8847 const char *saved_message = parser->type_definition_forbidden_message;
8848
8849 /* Consume the `('. */
8850 matching_parens parens;
8851 parens.consume_open (parser);
8852
8853 /* Parse the type-id. */
8854 parser->type_definition_forbidden_message
8855 = G_("types may not be defined in a new-expression");
8856 {
8857 type_id_in_expr_sentinel s (parser);
8858 type = cp_parser_type_id (parser);
8859 }
8860 parser->type_definition_forbidden_message = saved_message;
8861
8862 /* Look for the closing `)'. */
8863 parens.require_close (parser);
8864 token = cp_lexer_peek_token (parser->lexer);
8865 /* There should not be a direct-new-declarator in this production,
8866 but GCC used to allowed this, so we check and emit a sensible error
8867 message for this case. */
8868 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8869 {
8870 error_at (token->location,
8871 "array bound forbidden after parenthesized type-id");
8872 inform (token->location,
8873 "try removing the parentheses around the type-id");
8874 cp_parser_direct_new_declarator (parser);
8875 }
8876 }
8877 /* Otherwise, there must be a new-type-id. */
8878 else
8879 type = cp_parser_new_type_id (parser, &nelts);
8880
8881 /* If the next token is a `(' or '{', then we have a new-initializer. */
8882 cp_token *token = cp_lexer_peek_token (parser->lexer);
8883 if (token->type == CPP_OPEN_PAREN
8884 || token->type == CPP_OPEN_BRACE)
8885 initializer = cp_parser_new_initializer (parser);
8886 else
8887 initializer = NULL;
8888
8889 /* A new-expression may not appear in an integral constant
8890 expression. */
8891 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8892 ret = error_mark_node;
8893 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8894 of a new-type-id or type-id of a new-expression, the new-expression shall
8895 contain a new-initializer of the form ( assignment-expression )".
8896 Additionally, consistently with the spirit of DR 1467, we want to accept
8897 'new auto { 2 }' too. */
8898 else if ((ret = type_uses_auto (type))
8899 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8900 && (vec_safe_length (initializer) != 1
8901 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8902 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8903 {
8904 error_at (token->location,
8905 "initialization of new-expression for type %<auto%> "
8906 "requires exactly one element");
8907 ret = error_mark_node;
8908 }
8909 else
8910 {
8911 /* Construct a location e.g.:
8912 ptr = new int[100]
8913 ^~~~~~~~~~~~
8914 with caret == start at the start of the "new" token, and the end
8915 at the end of the final token we consumed. */
8916 location_t combined_loc = make_location (start_loc, start_loc,
8917 parser->lexer);
8918 /* Create a representation of the new-expression. */
8919 ret = build_new (combined_loc, &placement, type, nelts, &initializer,
8920 global_scope_p, tf_warning_or_error);
8921 }
8922
8923 if (placement != NULL)
8924 release_tree_vector (placement);
8925 if (initializer != NULL)
8926 release_tree_vector (initializer);
8927
8928 return ret;
8929 }
8930
8931 /* Parse a new-placement.
8932
8933 new-placement:
8934 ( expression-list )
8935
8936 Returns the same representation as for an expression-list. */
8937
8938 static vec<tree, va_gc> *
8939 cp_parser_new_placement (cp_parser* parser)
8940 {
8941 vec<tree, va_gc> *expression_list;
8942
8943 /* Parse the expression-list. */
8944 expression_list = (cp_parser_parenthesized_expression_list
8945 (parser, non_attr, /*cast_p=*/false,
8946 /*allow_expansion_p=*/true,
8947 /*non_constant_p=*/NULL));
8948
8949 if (expression_list && expression_list->is_empty ())
8950 error ("expected expression-list or type-id");
8951
8952 return expression_list;
8953 }
8954
8955 /* Parse a new-type-id.
8956
8957 new-type-id:
8958 type-specifier-seq new-declarator [opt]
8959
8960 Returns the TYPE allocated. If the new-type-id indicates an array
8961 type, *NELTS is set to the number of elements in the last array
8962 bound; the TYPE will not include the last array bound. */
8963
8964 static tree
8965 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8966 {
8967 cp_decl_specifier_seq type_specifier_seq;
8968 cp_declarator *new_declarator;
8969 cp_declarator *declarator;
8970 cp_declarator *outer_declarator;
8971 const char *saved_message;
8972
8973 /* The type-specifier sequence must not contain type definitions.
8974 (It cannot contain declarations of new types either, but if they
8975 are not definitions we will catch that because they are not
8976 complete.) */
8977 saved_message = parser->type_definition_forbidden_message;
8978 parser->type_definition_forbidden_message
8979 = G_("types may not be defined in a new-type-id");
8980 /* Parse the type-specifier-seq. */
8981 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
8982 /*is_declaration=*/false,
8983 /*is_trailing_return=*/false,
8984 &type_specifier_seq);
8985 /* Restore the old message. */
8986 parser->type_definition_forbidden_message = saved_message;
8987
8988 if (type_specifier_seq.type == error_mark_node)
8989 return error_mark_node;
8990
8991 /* Parse the new-declarator. */
8992 new_declarator = cp_parser_new_declarator_opt (parser);
8993
8994 /* Determine the number of elements in the last array dimension, if
8995 any. */
8996 *nelts = NULL_TREE;
8997 /* Skip down to the last array dimension. */
8998 declarator = new_declarator;
8999 outer_declarator = NULL;
9000 while (declarator && (declarator->kind == cdk_pointer
9001 || declarator->kind == cdk_ptrmem))
9002 {
9003 outer_declarator = declarator;
9004 declarator = declarator->declarator;
9005 }
9006 while (declarator
9007 && declarator->kind == cdk_array
9008 && declarator->declarator
9009 && declarator->declarator->kind == cdk_array)
9010 {
9011 outer_declarator = declarator;
9012 declarator = declarator->declarator;
9013 }
9014
9015 if (declarator && declarator->kind == cdk_array)
9016 {
9017 *nelts = declarator->u.array.bounds;
9018 if (*nelts == error_mark_node)
9019 *nelts = integer_one_node;
9020
9021 if (*nelts == NULL_TREE)
9022 /* Leave [] in the declarator. */;
9023 else if (outer_declarator)
9024 outer_declarator->declarator = declarator->declarator;
9025 else
9026 new_declarator = NULL;
9027 }
9028
9029 return groktypename (&type_specifier_seq, new_declarator, false);
9030 }
9031
9032 /* Parse an (optional) new-declarator.
9033
9034 new-declarator:
9035 ptr-operator new-declarator [opt]
9036 direct-new-declarator
9037
9038 Returns the declarator. */
9039
9040 static cp_declarator *
9041 cp_parser_new_declarator_opt (cp_parser* parser)
9042 {
9043 enum tree_code code;
9044 tree type, std_attributes = NULL_TREE;
9045 cp_cv_quals cv_quals;
9046
9047 /* We don't know if there's a ptr-operator next, or not. */
9048 cp_parser_parse_tentatively (parser);
9049 /* Look for a ptr-operator. */
9050 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
9051 /* If that worked, look for more new-declarators. */
9052 if (cp_parser_parse_definitely (parser))
9053 {
9054 cp_declarator *declarator;
9055
9056 /* Parse another optional declarator. */
9057 declarator = cp_parser_new_declarator_opt (parser);
9058
9059 declarator = cp_parser_make_indirect_declarator
9060 (code, type, cv_quals, declarator, std_attributes);
9061
9062 return declarator;
9063 }
9064
9065 /* If the next token is a `[', there is a direct-new-declarator. */
9066 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9067 return cp_parser_direct_new_declarator (parser);
9068
9069 return NULL;
9070 }
9071
9072 /* Parse a direct-new-declarator.
9073
9074 direct-new-declarator:
9075 [ expression ]
9076 direct-new-declarator [constant-expression]
9077
9078 */
9079
9080 static cp_declarator *
9081 cp_parser_direct_new_declarator (cp_parser* parser)
9082 {
9083 cp_declarator *declarator = NULL;
9084 bool first_p = true;
9085
9086 while (true)
9087 {
9088 tree expression;
9089 cp_token *token;
9090
9091 /* Look for the opening `['. */
9092 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9093
9094 token = cp_lexer_peek_token (parser->lexer);
9095 if (token->type == CPP_CLOSE_SQUARE && first_p)
9096 expression = NULL_TREE;
9097 else
9098 expression = cp_parser_expression (parser);
9099 /* The standard requires that the expression have integral
9100 type. DR 74 adds enumeration types. We believe that the
9101 real intent is that these expressions be handled like the
9102 expression in a `switch' condition, which also allows
9103 classes with a single conversion to integral or
9104 enumeration type. */
9105 if (expression && !processing_template_decl)
9106 {
9107 expression
9108 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
9109 expression,
9110 /*complain=*/true);
9111 if (!expression)
9112 {
9113 error_at (token->location,
9114 "expression in new-declarator must have integral "
9115 "or enumeration type");
9116 expression = error_mark_node;
9117 }
9118 }
9119
9120 /* Look for the closing `]'. */
9121 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9122
9123 /* Add this bound to the declarator. */
9124 declarator = make_array_declarator (declarator, expression);
9125
9126 /* If the next token is not a `[', then there are no more
9127 bounds. */
9128 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
9129 break;
9130 first_p = false;
9131 }
9132
9133 return declarator;
9134 }
9135
9136 /* Parse a new-initializer.
9137
9138 new-initializer:
9139 ( expression-list [opt] )
9140 braced-init-list
9141
9142 Returns a representation of the expression-list. */
9143
9144 static vec<tree, va_gc> *
9145 cp_parser_new_initializer (cp_parser* parser)
9146 {
9147 vec<tree, va_gc> *expression_list;
9148
9149 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9150 {
9151 tree t;
9152 bool expr_non_constant_p;
9153 cp_lexer_set_source_position (parser->lexer);
9154 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9155 t = cp_parser_braced_list (parser, &expr_non_constant_p);
9156 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
9157 expression_list = make_tree_vector_single (t);
9158 }
9159 else
9160 expression_list = (cp_parser_parenthesized_expression_list
9161 (parser, non_attr, /*cast_p=*/false,
9162 /*allow_expansion_p=*/true,
9163 /*non_constant_p=*/NULL));
9164
9165 return expression_list;
9166 }
9167
9168 /* Parse a delete-expression.
9169
9170 delete-expression:
9171 :: [opt] delete cast-expression
9172 :: [opt] delete [ ] cast-expression
9173
9174 Returns a representation of the expression. */
9175
9176 static tree
9177 cp_parser_delete_expression (cp_parser* parser)
9178 {
9179 bool global_scope_p;
9180 bool array_p;
9181 tree expression;
9182 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9183
9184 /* Look for the optional `::' operator. */
9185 global_scope_p
9186 = (cp_parser_global_scope_opt (parser,
9187 /*current_scope_valid_p=*/false)
9188 != NULL_TREE);
9189 /* Look for the `delete' keyword. */
9190 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9191 /* See if the array syntax is in use. */
9192 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9193 {
9194 /* Consume the `[' token. */
9195 cp_lexer_consume_token (parser->lexer);
9196 /* Look for the `]' token. */
9197 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9198 /* Remember that this is the `[]' construct. */
9199 array_p = true;
9200 }
9201 else
9202 array_p = false;
9203
9204 /* Parse the cast-expression. */
9205 expression = cp_parser_simple_cast_expression (parser);
9206
9207 /* A delete-expression may not appear in an integral constant
9208 expression. */
9209 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9210 return error_mark_node;
9211
9212 /* Construct a location e.g.:
9213 delete [ ] ptr
9214 ^~~~~~~~~~~~~~
9215 with caret == start at the start of the "delete" token, and
9216 the end at the end of the final token we consumed. */
9217 location_t combined_loc = make_location (start_loc, start_loc,
9218 parser->lexer);
9219 expression = delete_sanity (combined_loc, expression, NULL_TREE, array_p,
9220 global_scope_p, tf_warning_or_error);
9221
9222 return expression;
9223 }
9224
9225 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9226 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9227 0 otherwise. */
9228
9229 static int
9230 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9231 {
9232 cp_token *token = cp_lexer_peek_token (parser->lexer);
9233 switch (token->type)
9234 {
9235 case CPP_COMMA:
9236 case CPP_SEMICOLON:
9237 case CPP_QUERY:
9238 case CPP_COLON:
9239 case CPP_CLOSE_SQUARE:
9240 case CPP_CLOSE_PAREN:
9241 case CPP_CLOSE_BRACE:
9242 case CPP_OPEN_BRACE:
9243 case CPP_DOT:
9244 case CPP_DOT_STAR:
9245 case CPP_DEREF:
9246 case CPP_DEREF_STAR:
9247 case CPP_DIV:
9248 case CPP_MOD:
9249 case CPP_LSHIFT:
9250 case CPP_RSHIFT:
9251 case CPP_LESS:
9252 case CPP_GREATER:
9253 case CPP_LESS_EQ:
9254 case CPP_GREATER_EQ:
9255 case CPP_EQ_EQ:
9256 case CPP_NOT_EQ:
9257 case CPP_EQ:
9258 case CPP_MULT_EQ:
9259 case CPP_DIV_EQ:
9260 case CPP_MOD_EQ:
9261 case CPP_PLUS_EQ:
9262 case CPP_MINUS_EQ:
9263 case CPP_RSHIFT_EQ:
9264 case CPP_LSHIFT_EQ:
9265 case CPP_AND_EQ:
9266 case CPP_XOR_EQ:
9267 case CPP_OR_EQ:
9268 case CPP_XOR:
9269 case CPP_OR:
9270 case CPP_OR_OR:
9271 case CPP_EOF:
9272 case CPP_ELLIPSIS:
9273 return 0;
9274
9275 case CPP_OPEN_PAREN:
9276 /* In ((type ()) () the last () isn't a valid cast-expression,
9277 so the whole must be parsed as postfix-expression. */
9278 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9279 != CPP_CLOSE_PAREN;
9280
9281 case CPP_OPEN_SQUARE:
9282 /* '[' may start a primary-expression in obj-c++ and in C++11,
9283 as a lambda-expression, eg, '(void)[]{}'. */
9284 if (cxx_dialect >= cxx11)
9285 return -1;
9286 return c_dialect_objc ();
9287
9288 case CPP_PLUS_PLUS:
9289 case CPP_MINUS_MINUS:
9290 /* '++' and '--' may or may not start a cast-expression:
9291
9292 struct T { void operator++(int); };
9293 void f() { (T())++; }
9294
9295 vs
9296
9297 int a;
9298 (int)++a; */
9299 return -1;
9300
9301 default:
9302 return 1;
9303 }
9304 }
9305
9306 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9307 in the order: const_cast, static_cast, reinterpret_cast.
9308
9309 Don't suggest dynamic_cast.
9310
9311 Return the first legal cast kind found, or NULL otherwise. */
9312
9313 static const char *
9314 get_cast_suggestion (tree dst_type, tree orig_expr)
9315 {
9316 tree trial;
9317
9318 /* Reuse the parser logic by attempting to build the various kinds of
9319 cast, with "complain" disabled.
9320 Identify the first such cast that is valid. */
9321
9322 /* Don't attempt to run such logic within template processing. */
9323 if (processing_template_decl)
9324 return NULL;
9325
9326 /* First try const_cast. */
9327 trial = build_const_cast (input_location, dst_type, orig_expr, tf_none);
9328 if (trial != error_mark_node)
9329 return "const_cast";
9330
9331 /* If that fails, try static_cast. */
9332 trial = build_static_cast (input_location, dst_type, orig_expr, tf_none);
9333 if (trial != error_mark_node)
9334 return "static_cast";
9335
9336 /* Finally, try reinterpret_cast. */
9337 trial = build_reinterpret_cast (input_location, dst_type, orig_expr,
9338 tf_none);
9339 if (trial != error_mark_node)
9340 return "reinterpret_cast";
9341
9342 /* No such cast possible. */
9343 return NULL;
9344 }
9345
9346 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9347 suggesting how to convert a C-style cast of the form:
9348
9349 (DST_TYPE)ORIG_EXPR
9350
9351 to a C++-style cast.
9352
9353 The primary range of RICHLOC is asssumed to be that of the original
9354 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9355 of the parens in the C-style cast. */
9356
9357 static void
9358 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9359 location_t close_paren_loc, tree orig_expr,
9360 tree dst_type)
9361 {
9362 /* This function is non-trivial, so bail out now if the warning isn't
9363 going to be emitted. */
9364 if (!warn_old_style_cast)
9365 return;
9366
9367 /* Try to find a legal C++ cast, trying them in order:
9368 const_cast, static_cast, reinterpret_cast. */
9369 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9370 if (!cast_suggestion)
9371 return;
9372
9373 /* Replace the open paren with "CAST_SUGGESTION<". */
9374 pretty_printer pp;
9375 pp_printf (&pp, "%s<", cast_suggestion);
9376 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9377
9378 /* Replace the close paren with "> (". */
9379 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9380
9381 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9382 rich_loc->add_fixit_insert_after (")");
9383 }
9384
9385
9386 /* Parse a cast-expression.
9387
9388 cast-expression:
9389 unary-expression
9390 ( type-id ) cast-expression
9391
9392 ADDRESS_P is true iff the unary-expression is appearing as the
9393 operand of the `&' operator. CAST_P is true if this expression is
9394 the target of a cast.
9395
9396 Returns a representation of the expression. */
9397
9398 static cp_expr
9399 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9400 bool decltype_p, cp_id_kind * pidk)
9401 {
9402 /* If it's a `(', then we might be looking at a cast. */
9403 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9404 {
9405 tree type = NULL_TREE;
9406 cp_expr expr (NULL_TREE);
9407 int cast_expression = 0;
9408 const char *saved_message;
9409
9410 /* There's no way to know yet whether or not this is a cast.
9411 For example, `(int (3))' is a unary-expression, while `(int)
9412 3' is a cast. So, we resort to parsing tentatively. */
9413 cp_parser_parse_tentatively (parser);
9414 /* Types may not be defined in a cast. */
9415 saved_message = parser->type_definition_forbidden_message;
9416 parser->type_definition_forbidden_message
9417 = G_("types may not be defined in casts");
9418 /* Consume the `('. */
9419 matching_parens parens;
9420 cp_token *open_paren = parens.consume_open (parser);
9421 location_t open_paren_loc = open_paren->location;
9422 location_t close_paren_loc = UNKNOWN_LOCATION;
9423
9424 /* A very tricky bit is that `(struct S) { 3 }' is a
9425 compound-literal (which we permit in C++ as an extension).
9426 But, that construct is not a cast-expression -- it is a
9427 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9428 is legal; if the compound-literal were a cast-expression,
9429 you'd need an extra set of parentheses.) But, if we parse
9430 the type-id, and it happens to be a class-specifier, then we
9431 will commit to the parse at that point, because we cannot
9432 undo the action that is done when creating a new class. So,
9433 then we cannot back up and do a postfix-expression.
9434
9435 Another tricky case is the following (c++/29234):
9436
9437 struct S { void operator () (); };
9438
9439 void foo ()
9440 {
9441 ( S()() );
9442 }
9443
9444 As a type-id we parse the parenthesized S()() as a function
9445 returning a function, groktypename complains and we cannot
9446 back up in this case either.
9447
9448 Therefore, we scan ahead to the closing `)', and check to see
9449 if the tokens after the `)' can start a cast-expression. Otherwise
9450 we are dealing with an unary-expression, a postfix-expression
9451 or something else.
9452
9453 Yet another tricky case, in C++11, is the following (c++/54891):
9454
9455 (void)[]{};
9456
9457 The issue is that usually, besides the case of lambda-expressions,
9458 the parenthesized type-id cannot be followed by '[', and, eg, we
9459 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9460 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9461 we don't commit, we try a cast-expression, then an unary-expression.
9462
9463 Save tokens so that we can put them back. */
9464 cp_lexer_save_tokens (parser->lexer);
9465
9466 /* We may be looking at a cast-expression. */
9467 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9468 /*consume_paren=*/true))
9469 cast_expression
9470 = cp_parser_tokens_start_cast_expression (parser);
9471
9472 /* Roll back the tokens we skipped. */
9473 cp_lexer_rollback_tokens (parser->lexer);
9474 /* If we aren't looking at a cast-expression, simulate an error so
9475 that the call to cp_parser_error_occurred below returns true. */
9476 if (!cast_expression)
9477 cp_parser_simulate_error (parser);
9478 else
9479 {
9480 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9481 parser->in_type_id_in_expr_p = true;
9482 /* Look for the type-id. */
9483 type = cp_parser_type_id (parser);
9484 /* Look for the closing `)'. */
9485 cp_token *close_paren = parens.require_close (parser);
9486 if (close_paren)
9487 close_paren_loc = close_paren->location;
9488 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9489 }
9490
9491 /* Restore the saved message. */
9492 parser->type_definition_forbidden_message = saved_message;
9493
9494 /* At this point this can only be either a cast or a
9495 parenthesized ctor such as `(T ())' that looks like a cast to
9496 function returning T. */
9497 if (!cp_parser_error_occurred (parser))
9498 {
9499 /* Only commit if the cast-expression doesn't start with
9500 '++', '--', or '[' in C++11. */
9501 if (cast_expression > 0)
9502 cp_parser_commit_to_topmost_tentative_parse (parser);
9503
9504 expr = cp_parser_cast_expression (parser,
9505 /*address_p=*/false,
9506 /*cast_p=*/true,
9507 /*decltype_p=*/false,
9508 pidk);
9509
9510 if (cp_parser_parse_definitely (parser))
9511 {
9512 /* Warn about old-style casts, if so requested. */
9513 if (warn_old_style_cast
9514 && !in_system_header_at (input_location)
9515 && !VOID_TYPE_P (type)
9516 && current_lang_name != lang_name_c)
9517 {
9518 gcc_rich_location rich_loc (input_location);
9519 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9520 expr, type);
9521 warning_at (&rich_loc, OPT_Wold_style_cast,
9522 "use of old-style cast to %q#T", type);
9523 }
9524
9525 /* Only type conversions to integral or enumeration types
9526 can be used in constant-expressions. */
9527 if (!cast_valid_in_integral_constant_expression_p (type)
9528 && cp_parser_non_integral_constant_expression (parser,
9529 NIC_CAST))
9530 return error_mark_node;
9531
9532 /* Perform the cast. */
9533 /* Make a location:
9534 (TYPE) EXPR
9535 ^~~~~~~~~~~
9536 with start==caret at the open paren, extending to the
9537 end of "expr". */
9538 location_t cast_loc = make_location (open_paren_loc,
9539 open_paren_loc,
9540 expr.get_finish ());
9541 expr = build_c_cast (cast_loc, type, expr);
9542 return expr;
9543 }
9544 }
9545 else
9546 cp_parser_abort_tentative_parse (parser);
9547 }
9548
9549 /* If we get here, then it's not a cast, so it must be a
9550 unary-expression. */
9551 return cp_parser_unary_expression (parser, pidk, address_p,
9552 cast_p, decltype_p);
9553 }
9554
9555 /* Parse a binary expression of the general form:
9556
9557 pm-expression:
9558 cast-expression
9559 pm-expression .* cast-expression
9560 pm-expression ->* cast-expression
9561
9562 multiplicative-expression:
9563 pm-expression
9564 multiplicative-expression * pm-expression
9565 multiplicative-expression / pm-expression
9566 multiplicative-expression % pm-expression
9567
9568 additive-expression:
9569 multiplicative-expression
9570 additive-expression + multiplicative-expression
9571 additive-expression - multiplicative-expression
9572
9573 shift-expression:
9574 additive-expression
9575 shift-expression << additive-expression
9576 shift-expression >> additive-expression
9577
9578 relational-expression:
9579 shift-expression
9580 relational-expression < shift-expression
9581 relational-expression > shift-expression
9582 relational-expression <= shift-expression
9583 relational-expression >= shift-expression
9584
9585 GNU Extension:
9586
9587 relational-expression:
9588 relational-expression <? shift-expression
9589 relational-expression >? shift-expression
9590
9591 equality-expression:
9592 relational-expression
9593 equality-expression == relational-expression
9594 equality-expression != relational-expression
9595
9596 and-expression:
9597 equality-expression
9598 and-expression & equality-expression
9599
9600 exclusive-or-expression:
9601 and-expression
9602 exclusive-or-expression ^ and-expression
9603
9604 inclusive-or-expression:
9605 exclusive-or-expression
9606 inclusive-or-expression | exclusive-or-expression
9607
9608 logical-and-expression:
9609 inclusive-or-expression
9610 logical-and-expression && inclusive-or-expression
9611
9612 logical-or-expression:
9613 logical-and-expression
9614 logical-or-expression || logical-and-expression
9615
9616 All these are implemented with a single function like:
9617
9618 binary-expression:
9619 simple-cast-expression
9620 binary-expression <token> binary-expression
9621
9622 CAST_P is true if this expression is the target of a cast.
9623
9624 The binops_by_token map is used to get the tree codes for each <token> type.
9625 binary-expressions are associated according to a precedence table. */
9626
9627 #define TOKEN_PRECEDENCE(token) \
9628 (((token->type == CPP_GREATER \
9629 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9630 && !parser->greater_than_is_operator_p) \
9631 ? PREC_NOT_OPERATOR \
9632 : binops_by_token[token->type].prec)
9633
9634 static cp_expr
9635 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9636 bool no_toplevel_fold_p,
9637 bool decltype_p,
9638 enum cp_parser_prec prec,
9639 cp_id_kind * pidk)
9640 {
9641 cp_parser_expression_stack stack;
9642 cp_parser_expression_stack_entry *sp = &stack[0];
9643 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
9644 cp_parser_expression_stack_entry current;
9645 cp_expr rhs;
9646 cp_token *token;
9647 enum tree_code rhs_type;
9648 enum cp_parser_prec new_prec, lookahead_prec;
9649 tree overload;
9650
9651 /* Parse the first expression. */
9652 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9653 ? TRUTH_NOT_EXPR : ERROR_MARK);
9654 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9655 cast_p, decltype_p, pidk);
9656 current.prec = prec;
9657
9658 if (cp_parser_error_occurred (parser))
9659 return error_mark_node;
9660
9661 for (;;)
9662 {
9663 /* Get an operator token. */
9664 token = cp_lexer_peek_token (parser->lexer);
9665
9666 if (warn_cxx11_compat
9667 && token->type == CPP_RSHIFT
9668 && !parser->greater_than_is_operator_p)
9669 {
9670 if (warning_at (token->location, OPT_Wc__11_compat,
9671 "%<>>%> operator is treated"
9672 " as two right angle brackets in C++11"))
9673 inform (token->location,
9674 "suggest parentheses around %<>>%> expression");
9675 }
9676
9677 new_prec = TOKEN_PRECEDENCE (token);
9678 if (new_prec != PREC_NOT_OPERATOR
9679 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9680 /* This is a fold-expression; handle it later. */
9681 new_prec = PREC_NOT_OPERATOR;
9682
9683 /* Popping an entry off the stack means we completed a subexpression:
9684 - either we found a token which is not an operator (`>' where it is not
9685 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9686 will happen repeatedly;
9687 - or, we found an operator which has lower priority. This is the case
9688 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9689 parsing `3 * 4'. */
9690 if (new_prec <= current.prec)
9691 {
9692 if (sp == stack)
9693 break;
9694 else
9695 goto pop;
9696 }
9697
9698 get_rhs:
9699 current.tree_type = binops_by_token[token->type].tree_type;
9700 current.loc = token->location;
9701
9702 /* We used the operator token. */
9703 cp_lexer_consume_token (parser->lexer);
9704
9705 /* For "false && x" or "true || x", x will never be executed;
9706 disable warnings while evaluating it. */
9707 if ((current.tree_type == TRUTH_ANDIF_EXPR
9708 && cp_fully_fold (current.lhs) == truthvalue_false_node)
9709 || (current.tree_type == TRUTH_ORIF_EXPR
9710 && cp_fully_fold (current.lhs) == truthvalue_true_node))
9711 {
9712 disable_warnings_sp = sp;
9713 ++c_inhibit_evaluation_warnings;
9714 }
9715
9716 /* Extract another operand. It may be the RHS of this expression
9717 or the LHS of a new, higher priority expression. */
9718 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9719 ? TRUTH_NOT_EXPR : ERROR_MARK);
9720 rhs = cp_parser_simple_cast_expression (parser);
9721
9722 /* Get another operator token. Look up its precedence to avoid
9723 building a useless (immediately popped) stack entry for common
9724 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9725 token = cp_lexer_peek_token (parser->lexer);
9726 lookahead_prec = TOKEN_PRECEDENCE (token);
9727 if (lookahead_prec != PREC_NOT_OPERATOR
9728 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9729 lookahead_prec = PREC_NOT_OPERATOR;
9730 if (lookahead_prec > new_prec)
9731 {
9732 /* ... and prepare to parse the RHS of the new, higher priority
9733 expression. Since precedence levels on the stack are
9734 monotonically increasing, we do not have to care about
9735 stack overflows. */
9736 *sp = current;
9737 ++sp;
9738 current.lhs = rhs;
9739 current.lhs_type = rhs_type;
9740 current.prec = new_prec;
9741 new_prec = lookahead_prec;
9742 goto get_rhs;
9743
9744 pop:
9745 lookahead_prec = new_prec;
9746 /* If the stack is not empty, we have parsed into LHS the right side
9747 (`4' in the example above) of an expression we had suspended.
9748 We can use the information on the stack to recover the LHS (`3')
9749 from the stack together with the tree code (`MULT_EXPR'), and
9750 the precedence of the higher level subexpression
9751 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9752 which will be used to actually build the additive expression. */
9753 rhs = current.lhs;
9754 rhs_type = current.lhs_type;
9755 --sp;
9756 current = *sp;
9757 }
9758
9759 /* Undo the disabling of warnings done above. */
9760 if (sp == disable_warnings_sp)
9761 {
9762 disable_warnings_sp = NULL;
9763 --c_inhibit_evaluation_warnings;
9764 }
9765
9766 if (warn_logical_not_paren
9767 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9768 && current.lhs_type == TRUTH_NOT_EXPR
9769 /* Avoid warning for !!x == y. */
9770 && (TREE_CODE (current.lhs) != NE_EXPR
9771 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9772 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9773 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9774 /* Avoid warning for !b == y where b is boolean. */
9775 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9776 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9777 != BOOLEAN_TYPE))))
9778 /* Avoid warning for !!b == y where b is boolean. */
9779 && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
9780 || TREE_TYPE (current.lhs) == NULL_TREE
9781 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9782 warn_logical_not_parentheses (current.loc, current.tree_type,
9783 current.lhs, maybe_constant_value (rhs));
9784
9785 overload = NULL;
9786
9787 location_t combined_loc = make_location (current.loc,
9788 current.lhs.get_start (),
9789 rhs.get_finish ());
9790
9791 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9792 ERROR_MARK for everything that is not a binary expression.
9793 This makes warn_about_parentheses miss some warnings that
9794 involve unary operators. For unary expressions we should
9795 pass the correct tree_code unless the unary expression was
9796 surrounded by parentheses.
9797 */
9798 if (no_toplevel_fold_p
9799 && lookahead_prec <= current.prec
9800 && sp == stack)
9801 {
9802 if (current.lhs == error_mark_node || rhs == error_mark_node)
9803 current.lhs = error_mark_node;
9804 else
9805 {
9806 current.lhs.maybe_add_location_wrapper ();
9807 rhs.maybe_add_location_wrapper ();
9808 current.lhs
9809 = build_min (current.tree_type,
9810 TREE_CODE_CLASS (current.tree_type)
9811 == tcc_comparison
9812 ? boolean_type_node : TREE_TYPE (current.lhs),
9813 current.lhs.get_value (), rhs.get_value ());
9814 SET_EXPR_LOCATION (current.lhs, combined_loc);
9815 }
9816 }
9817 else
9818 {
9819 op_location_t op_loc (current.loc, combined_loc);
9820 current.lhs = build_x_binary_op (op_loc, current.tree_type,
9821 current.lhs, current.lhs_type,
9822 rhs, rhs_type, &overload,
9823 complain_flags (decltype_p));
9824 /* TODO: build_x_binary_op doesn't always honor the location. */
9825 current.lhs.set_location (combined_loc);
9826 }
9827 current.lhs_type = current.tree_type;
9828
9829 /* If the binary operator required the use of an overloaded operator,
9830 then this expression cannot be an integral constant-expression.
9831 An overloaded operator can be used even if both operands are
9832 otherwise permissible in an integral constant-expression if at
9833 least one of the operands is of enumeration type. */
9834
9835 if (overload
9836 && cp_parser_non_integral_constant_expression (parser,
9837 NIC_OVERLOADED))
9838 return error_mark_node;
9839 }
9840
9841 return current.lhs;
9842 }
9843
9844 static cp_expr
9845 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9846 bool no_toplevel_fold_p,
9847 enum cp_parser_prec prec,
9848 cp_id_kind * pidk)
9849 {
9850 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9851 /*decltype*/false, prec, pidk);
9852 }
9853
9854 /* Parse the `? expression : assignment-expression' part of a
9855 conditional-expression. The LOGICAL_OR_EXPR is the
9856 logical-or-expression that started the conditional-expression.
9857 Returns a representation of the entire conditional-expression.
9858
9859 This routine is used by cp_parser_assignment_expression.
9860
9861 ? expression : assignment-expression
9862
9863 GNU Extensions:
9864
9865 ? : assignment-expression */
9866
9867 static tree
9868 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9869 {
9870 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9871 cp_expr assignment_expr;
9872 struct cp_token *token;
9873 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9874
9875 /* Consume the `?' token. */
9876 cp_lexer_consume_token (parser->lexer);
9877 token = cp_lexer_peek_token (parser->lexer);
9878 if (cp_parser_allow_gnu_extensions_p (parser)
9879 && token->type == CPP_COLON)
9880 {
9881 pedwarn (token->location, OPT_Wpedantic,
9882 "ISO C++ does not allow %<?:%> with omitted middle operand");
9883 /* Implicit true clause. */
9884 expr = NULL_TREE;
9885 c_inhibit_evaluation_warnings +=
9886 folded_logical_or_expr == truthvalue_true_node;
9887 warn_for_omitted_condop (token->location, logical_or_expr);
9888 }
9889 else
9890 {
9891 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9892 parser->colon_corrects_to_scope_p = false;
9893 /* Parse the expression. */
9894 c_inhibit_evaluation_warnings +=
9895 folded_logical_or_expr == truthvalue_false_node;
9896 expr = cp_parser_expression (parser);
9897 c_inhibit_evaluation_warnings +=
9898 ((folded_logical_or_expr == truthvalue_true_node)
9899 - (folded_logical_or_expr == truthvalue_false_node));
9900 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9901 }
9902
9903 /* The next token should be a `:'. */
9904 cp_parser_require (parser, CPP_COLON, RT_COLON);
9905 /* Parse the assignment-expression. */
9906 assignment_expr = cp_parser_assignment_expression (parser);
9907 c_inhibit_evaluation_warnings -=
9908 folded_logical_or_expr == truthvalue_true_node;
9909
9910 /* Make a location:
9911 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9912 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9913 with the caret at the "?", ranging from the start of
9914 the logical_or_expr to the end of the assignment_expr. */
9915 loc = make_location (loc,
9916 logical_or_expr.get_start (),
9917 assignment_expr.get_finish ());
9918
9919 /* Build the conditional-expression. */
9920 return build_x_conditional_expr (loc, logical_or_expr,
9921 expr,
9922 assignment_expr,
9923 tf_warning_or_error);
9924 }
9925
9926 /* Parse an assignment-expression.
9927
9928 assignment-expression:
9929 conditional-expression
9930 logical-or-expression assignment-operator assignment_expression
9931 throw-expression
9932 yield-expression
9933
9934 CAST_P is true if this expression is the target of a cast.
9935 DECLTYPE_P is true if this expression is the operand of decltype.
9936
9937 Returns a representation for the expression. */
9938
9939 static cp_expr
9940 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9941 bool cast_p, bool decltype_p)
9942 {
9943 cp_expr expr;
9944
9945 /* If the next token is the `throw' keyword, then we're looking at
9946 a throw-expression. */
9947 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9948 expr = cp_parser_throw_expression (parser);
9949 /* If the next token is the `co_yield' keyword, then we're looking at
9950 a yield-expression. */
9951 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CO_YIELD))
9952 expr = cp_parser_yield_expression (parser);
9953 /* Otherwise, it must be that we are looking at a
9954 logical-or-expression. */
9955 else
9956 {
9957 /* Parse the binary expressions (logical-or-expression). */
9958 expr = cp_parser_binary_expression (parser, cast_p, false,
9959 decltype_p,
9960 PREC_NOT_OPERATOR, pidk);
9961 /* If the next token is a `?' then we're actually looking at a
9962 conditional-expression. */
9963 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9964 return cp_parser_question_colon_clause (parser, expr);
9965 else
9966 {
9967 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9968
9969 /* If it's an assignment-operator, we're using the second
9970 production. */
9971 enum tree_code assignment_operator
9972 = cp_parser_assignment_operator_opt (parser);
9973 if (assignment_operator != ERROR_MARK)
9974 {
9975 bool non_constant_p;
9976
9977 /* Parse the right-hand side of the assignment. */
9978 cp_expr rhs = cp_parser_initializer_clause (parser,
9979 &non_constant_p);
9980
9981 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9982 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9983
9984 /* An assignment may not appear in a
9985 constant-expression. */
9986 if (cp_parser_non_integral_constant_expression (parser,
9987 NIC_ASSIGNMENT))
9988 return error_mark_node;
9989 /* Build the assignment expression. Its default
9990 location:
9991 LHS = RHS
9992 ~~~~^~~~~
9993 is the location of the '=' token as the
9994 caret, ranging from the start of the lhs to the
9995 end of the rhs. */
9996 loc = make_location (loc,
9997 expr.get_start (),
9998 rhs.get_finish ());
9999 expr = build_x_modify_expr (loc, expr,
10000 assignment_operator,
10001 rhs,
10002 complain_flags (decltype_p));
10003 /* TODO: build_x_modify_expr doesn't honor the location,
10004 so we must set it here. */
10005 expr.set_location (loc);
10006 }
10007 }
10008 }
10009
10010 return expr;
10011 }
10012
10013 /* Parse an (optional) assignment-operator.
10014
10015 assignment-operator: one of
10016 = *= /= %= += -= >>= <<= &= ^= |=
10017
10018 GNU Extension:
10019
10020 assignment-operator: one of
10021 <?= >?=
10022
10023 If the next token is an assignment operator, the corresponding tree
10024 code is returned, and the token is consumed. For example, for
10025 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
10026 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
10027 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
10028 operator, ERROR_MARK is returned. */
10029
10030 static enum tree_code
10031 cp_parser_assignment_operator_opt (cp_parser* parser)
10032 {
10033 enum tree_code op;
10034 cp_token *token;
10035
10036 /* Peek at the next token. */
10037 token = cp_lexer_peek_token (parser->lexer);
10038
10039 switch (token->type)
10040 {
10041 case CPP_EQ:
10042 op = NOP_EXPR;
10043 break;
10044
10045 case CPP_MULT_EQ:
10046 op = MULT_EXPR;
10047 break;
10048
10049 case CPP_DIV_EQ:
10050 op = TRUNC_DIV_EXPR;
10051 break;
10052
10053 case CPP_MOD_EQ:
10054 op = TRUNC_MOD_EXPR;
10055 break;
10056
10057 case CPP_PLUS_EQ:
10058 op = PLUS_EXPR;
10059 break;
10060
10061 case CPP_MINUS_EQ:
10062 op = MINUS_EXPR;
10063 break;
10064
10065 case CPP_RSHIFT_EQ:
10066 op = RSHIFT_EXPR;
10067 break;
10068
10069 case CPP_LSHIFT_EQ:
10070 op = LSHIFT_EXPR;
10071 break;
10072
10073 case CPP_AND_EQ:
10074 op = BIT_AND_EXPR;
10075 break;
10076
10077 case CPP_XOR_EQ:
10078 op = BIT_XOR_EXPR;
10079 break;
10080
10081 case CPP_OR_EQ:
10082 op = BIT_IOR_EXPR;
10083 break;
10084
10085 default:
10086 /* Nothing else is an assignment operator. */
10087 op = ERROR_MARK;
10088 }
10089
10090 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10091 if (op != ERROR_MARK
10092 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10093 op = ERROR_MARK;
10094
10095 /* If it was an assignment operator, consume it. */
10096 if (op != ERROR_MARK)
10097 cp_lexer_consume_token (parser->lexer);
10098
10099 return op;
10100 }
10101
10102 /* Parse an expression.
10103
10104 expression:
10105 assignment-expression
10106 expression , assignment-expression
10107
10108 CAST_P is true if this expression is the target of a cast.
10109 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10110 except possibly parenthesized or on the RHS of a comma (N3276).
10111 WARN_COMMA_P is true if a comma should be diagnosed.
10112
10113 Returns a representation of the expression. */
10114
10115 static cp_expr
10116 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
10117 bool cast_p, bool decltype_p, bool warn_comma_p)
10118 {
10119 cp_expr expression = NULL_TREE;
10120 location_t loc = UNKNOWN_LOCATION;
10121
10122 while (true)
10123 {
10124 cp_expr assignment_expression;
10125
10126 /* Parse the next assignment-expression. */
10127 assignment_expression
10128 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
10129
10130 /* We don't create a temporary for a call that is the immediate operand
10131 of decltype or on the RHS of a comma. But when we see a comma, we
10132 need to create a temporary for a call on the LHS. */
10133 if (decltype_p && !processing_template_decl
10134 && TREE_CODE (assignment_expression) == CALL_EXPR
10135 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
10136 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10137 assignment_expression
10138 = build_cplus_new (TREE_TYPE (assignment_expression),
10139 assignment_expression, tf_warning_or_error);
10140
10141 /* If this is the first assignment-expression, we can just
10142 save it away. */
10143 if (!expression)
10144 expression = assignment_expression;
10145 else
10146 {
10147 /* Create a location with caret at the comma, ranging
10148 from the start of the LHS to the end of the RHS. */
10149 loc = make_location (loc,
10150 expression.get_start (),
10151 assignment_expression.get_finish ());
10152 expression = build_x_compound_expr (loc, expression,
10153 assignment_expression,
10154 complain_flags (decltype_p));
10155 expression.set_location (loc);
10156 }
10157 /* If the next token is not a comma, or we're in a fold-expression, then
10158 we are done with the expression. */
10159 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
10160 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10161 break;
10162 /* Consume the `,'. */
10163 loc = cp_lexer_peek_token (parser->lexer)->location;
10164 if (warn_comma_p)
10165 {
10166 /* [depr.comma.subscript]: A comma expression appearing as
10167 the expr-or-braced-init-list of a subscripting expression
10168 is deprecated. A parenthesized comma expression is not
10169 deprecated. */
10170 warning_at (loc, OPT_Wcomma_subscript,
10171 "top-level comma expression in array subscript "
10172 "is deprecated");
10173 warn_comma_p = false;
10174 }
10175 cp_lexer_consume_token (parser->lexer);
10176 /* A comma operator cannot appear in a constant-expression. */
10177 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
10178 expression = error_mark_node;
10179 }
10180
10181 return expression;
10182 }
10183
10184 /* Parse a constant-expression.
10185
10186 constant-expression:
10187 conditional-expression
10188
10189 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10190 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10191 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10192 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
10193 only parse a conditional-expression, otherwise parse an
10194 assignment-expression. See below for rationale. */
10195
10196 static cp_expr
10197 cp_parser_constant_expression (cp_parser* parser,
10198 bool allow_non_constant_p,
10199 bool *non_constant_p,
10200 bool strict_p)
10201 {
10202 bool saved_integral_constant_expression_p;
10203 bool saved_allow_non_integral_constant_expression_p;
10204 bool saved_non_integral_constant_expression_p;
10205 cp_expr expression;
10206
10207 /* It might seem that we could simply parse the
10208 conditional-expression, and then check to see if it were
10209 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10210 one that the compiler can figure out is constant, possibly after
10211 doing some simplifications or optimizations. The standard has a
10212 precise definition of constant-expression, and we must honor
10213 that, even though it is somewhat more restrictive.
10214
10215 For example:
10216
10217 int i[(2, 3)];
10218
10219 is not a legal declaration, because `(2, 3)' is not a
10220 constant-expression. The `,' operator is forbidden in a
10221 constant-expression. However, GCC's constant-folding machinery
10222 will fold this operation to an INTEGER_CST for `3'. */
10223
10224 /* Save the old settings. */
10225 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10226 saved_allow_non_integral_constant_expression_p
10227 = parser->allow_non_integral_constant_expression_p;
10228 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10229 /* We are now parsing a constant-expression. */
10230 parser->integral_constant_expression_p = true;
10231 parser->allow_non_integral_constant_expression_p
10232 = (allow_non_constant_p || cxx_dialect >= cxx11);
10233 parser->non_integral_constant_expression_p = false;
10234 /* Although the grammar says "conditional-expression", when not STRICT_P,
10235 we parse an "assignment-expression", which also permits
10236 "throw-expression" and the use of assignment operators. In the case
10237 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10238 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10239 actually essential that we look for an assignment-expression.
10240 For example, cp_parser_initializer_clauses uses this function to
10241 determine whether a particular assignment-expression is in fact
10242 constant. */
10243 if (strict_p)
10244 {
10245 /* Parse the binary expressions (logical-or-expression). */
10246 expression = cp_parser_binary_expression (parser, false, false, false,
10247 PREC_NOT_OPERATOR, NULL);
10248 /* If the next token is a `?' then we're actually looking at
10249 a conditional-expression; otherwise we're done. */
10250 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10251 expression = cp_parser_question_colon_clause (parser, expression);
10252 }
10253 else
10254 expression = cp_parser_assignment_expression (parser);
10255 /* Restore the old settings. */
10256 parser->integral_constant_expression_p
10257 = saved_integral_constant_expression_p;
10258 parser->allow_non_integral_constant_expression_p
10259 = saved_allow_non_integral_constant_expression_p;
10260 if (cxx_dialect >= cxx11)
10261 {
10262 /* Require an rvalue constant expression here; that's what our
10263 callers expect. Reference constant expressions are handled
10264 separately in e.g. cp_parser_template_argument. */
10265 tree decay = expression;
10266 if (TREE_TYPE (expression)
10267 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10268 decay = build_address (expression);
10269 bool is_const = is_rvalue_constant_expression (decay);
10270 parser->non_integral_constant_expression_p = !is_const;
10271 if (!is_const && !allow_non_constant_p)
10272 require_rvalue_constant_expression (decay);
10273 }
10274 if (allow_non_constant_p)
10275 *non_constant_p = parser->non_integral_constant_expression_p;
10276 parser->non_integral_constant_expression_p
10277 = saved_non_integral_constant_expression_p;
10278
10279 return expression;
10280 }
10281
10282 /* Parse __builtin_offsetof.
10283
10284 offsetof-expression:
10285 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10286
10287 offsetof-member-designator:
10288 id-expression
10289 | offsetof-member-designator "." id-expression
10290 | offsetof-member-designator "[" expression "]"
10291 | offsetof-member-designator "->" id-expression */
10292
10293 static cp_expr
10294 cp_parser_builtin_offsetof (cp_parser *parser)
10295 {
10296 int save_ice_p, save_non_ice_p;
10297 tree type;
10298 cp_expr expr;
10299 cp_id_kind dummy;
10300 cp_token *token;
10301 location_t finish_loc;
10302
10303 /* We're about to accept non-integral-constant things, but will
10304 definitely yield an integral constant expression. Save and
10305 restore these values around our local parsing. */
10306 save_ice_p = parser->integral_constant_expression_p;
10307 save_non_ice_p = parser->non_integral_constant_expression_p;
10308
10309 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10310
10311 /* Consume the "__builtin_offsetof" token. */
10312 cp_lexer_consume_token (parser->lexer);
10313 /* Consume the opening `('. */
10314 matching_parens parens;
10315 parens.require_open (parser);
10316 /* Parse the type-id. */
10317 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10318 {
10319 const char *saved_message = parser->type_definition_forbidden_message;
10320 parser->type_definition_forbidden_message
10321 = G_("types may not be defined within %<__builtin_offsetof%>");
10322 type = cp_parser_type_id (parser);
10323 parser->type_definition_forbidden_message = saved_message;
10324 }
10325 /* Look for the `,'. */
10326 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10327 token = cp_lexer_peek_token (parser->lexer);
10328
10329 /* Build the (type *)null that begins the traditional offsetof macro. */
10330 tree object_ptr
10331 = build_static_cast (input_location, build_pointer_type (type),
10332 null_pointer_node, tf_warning_or_error);
10333
10334 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10335 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10336 true, &dummy, token->location);
10337 while (true)
10338 {
10339 token = cp_lexer_peek_token (parser->lexer);
10340 switch (token->type)
10341 {
10342 case CPP_OPEN_SQUARE:
10343 /* offsetof-member-designator "[" expression "]" */
10344 expr = cp_parser_postfix_open_square_expression (parser, expr,
10345 true, false);
10346 break;
10347
10348 case CPP_DEREF:
10349 /* offsetof-member-designator "->" identifier */
10350 expr = grok_array_decl (token->location, expr,
10351 integer_zero_node, false);
10352 /* FALLTHRU */
10353
10354 case CPP_DOT:
10355 /* offsetof-member-designator "." identifier */
10356 cp_lexer_consume_token (parser->lexer);
10357 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10358 expr, true, &dummy,
10359 token->location);
10360 break;
10361
10362 case CPP_CLOSE_PAREN:
10363 /* Consume the ")" token. */
10364 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10365 cp_lexer_consume_token (parser->lexer);
10366 goto success;
10367
10368 default:
10369 /* Error. We know the following require will fail, but
10370 that gives the proper error message. */
10371 parens.require_close (parser);
10372 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10373 expr = error_mark_node;
10374 goto failure;
10375 }
10376 }
10377
10378 success:
10379 /* Make a location of the form:
10380 __builtin_offsetof (struct s, f)
10381 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10382 with caret at the type-id, ranging from the start of the
10383 "_builtin_offsetof" token to the close paren. */
10384 loc = make_location (loc, start_loc, finish_loc);
10385 /* The result will be an INTEGER_CST, so we need to explicitly
10386 preserve the location. */
10387 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10388
10389 failure:
10390 parser->integral_constant_expression_p = save_ice_p;
10391 parser->non_integral_constant_expression_p = save_non_ice_p;
10392
10393 expr = expr.maybe_add_location_wrapper ();
10394 return expr;
10395 }
10396
10397 /* Parse a trait expression.
10398
10399 Returns a representation of the expression, the underlying type
10400 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10401
10402 static cp_expr
10403 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10404 {
10405 cp_trait_kind kind;
10406 tree type1, type2 = NULL_TREE;
10407 bool binary = false;
10408 bool variadic = false;
10409
10410 switch (keyword)
10411 {
10412 case RID_HAS_NOTHROW_ASSIGN:
10413 kind = CPTK_HAS_NOTHROW_ASSIGN;
10414 break;
10415 case RID_HAS_NOTHROW_CONSTRUCTOR:
10416 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10417 break;
10418 case RID_HAS_NOTHROW_COPY:
10419 kind = CPTK_HAS_NOTHROW_COPY;
10420 break;
10421 case RID_HAS_TRIVIAL_ASSIGN:
10422 kind = CPTK_HAS_TRIVIAL_ASSIGN;
10423 break;
10424 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10425 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10426 break;
10427 case RID_HAS_TRIVIAL_COPY:
10428 kind = CPTK_HAS_TRIVIAL_COPY;
10429 break;
10430 case RID_HAS_TRIVIAL_DESTRUCTOR:
10431 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10432 break;
10433 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10434 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10435 break;
10436 case RID_HAS_VIRTUAL_DESTRUCTOR:
10437 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10438 break;
10439 case RID_IS_ABSTRACT:
10440 kind = CPTK_IS_ABSTRACT;
10441 break;
10442 case RID_IS_AGGREGATE:
10443 kind = CPTK_IS_AGGREGATE;
10444 break;
10445 case RID_IS_BASE_OF:
10446 kind = CPTK_IS_BASE_OF;
10447 binary = true;
10448 break;
10449 case RID_IS_CLASS:
10450 kind = CPTK_IS_CLASS;
10451 break;
10452 case RID_IS_EMPTY:
10453 kind = CPTK_IS_EMPTY;
10454 break;
10455 case RID_IS_ENUM:
10456 kind = CPTK_IS_ENUM;
10457 break;
10458 case RID_IS_FINAL:
10459 kind = CPTK_IS_FINAL;
10460 break;
10461 case RID_IS_LITERAL_TYPE:
10462 kind = CPTK_IS_LITERAL_TYPE;
10463 break;
10464 case RID_IS_POD:
10465 kind = CPTK_IS_POD;
10466 break;
10467 case RID_IS_POLYMORPHIC:
10468 kind = CPTK_IS_POLYMORPHIC;
10469 break;
10470 case RID_IS_SAME_AS:
10471 kind = CPTK_IS_SAME_AS;
10472 binary = true;
10473 break;
10474 case RID_IS_STD_LAYOUT:
10475 kind = CPTK_IS_STD_LAYOUT;
10476 break;
10477 case RID_IS_TRIVIAL:
10478 kind = CPTK_IS_TRIVIAL;
10479 break;
10480 case RID_IS_TRIVIALLY_ASSIGNABLE:
10481 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10482 binary = true;
10483 break;
10484 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10485 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10486 variadic = true;
10487 break;
10488 case RID_IS_TRIVIALLY_COPYABLE:
10489 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10490 break;
10491 case RID_IS_UNION:
10492 kind = CPTK_IS_UNION;
10493 break;
10494 case RID_UNDERLYING_TYPE:
10495 kind = CPTK_UNDERLYING_TYPE;
10496 break;
10497 case RID_BASES:
10498 kind = CPTK_BASES;
10499 break;
10500 case RID_DIRECT_BASES:
10501 kind = CPTK_DIRECT_BASES;
10502 break;
10503 case RID_IS_ASSIGNABLE:
10504 kind = CPTK_IS_ASSIGNABLE;
10505 binary = true;
10506 break;
10507 case RID_IS_CONSTRUCTIBLE:
10508 kind = CPTK_IS_CONSTRUCTIBLE;
10509 variadic = true;
10510 break;
10511 case RID_IS_NOTHROW_ASSIGNABLE:
10512 kind = CPTK_IS_NOTHROW_ASSIGNABLE;
10513 binary = true;
10514 break;
10515 case RID_IS_NOTHROW_CONSTRUCTIBLE:
10516 kind = CPTK_IS_NOTHROW_CONSTRUCTIBLE;
10517 variadic = true;
10518 break;
10519 default:
10520 gcc_unreachable ();
10521 }
10522
10523 /* Get location of initial token. */
10524 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10525
10526 /* Consume the token. */
10527 cp_lexer_consume_token (parser->lexer);
10528
10529 matching_parens parens;
10530 parens.require_open (parser);
10531
10532 {
10533 type_id_in_expr_sentinel s (parser);
10534 type1 = cp_parser_type_id (parser);
10535 }
10536
10537 if (type1 == error_mark_node)
10538 return error_mark_node;
10539
10540 if (binary)
10541 {
10542 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10543
10544 {
10545 type_id_in_expr_sentinel s (parser);
10546 type2 = cp_parser_type_id (parser);
10547 }
10548
10549 if (type2 == error_mark_node)
10550 return error_mark_node;
10551 }
10552 else if (variadic)
10553 {
10554 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10555 {
10556 cp_lexer_consume_token (parser->lexer);
10557 tree elt = cp_parser_type_id (parser);
10558 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10559 {
10560 cp_lexer_consume_token (parser->lexer);
10561 elt = make_pack_expansion (elt);
10562 }
10563 if (elt == error_mark_node)
10564 return error_mark_node;
10565 type2 = tree_cons (NULL_TREE, elt, type2);
10566 }
10567 }
10568
10569 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10570 parens.require_close (parser);
10571
10572 /* Construct a location of the form:
10573 __is_trivially_copyable(_Tp)
10574 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10575 with start == caret, finishing at the close-paren. */
10576 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10577
10578 /* Complete the trait expression, which may mean either processing
10579 the trait expr now or saving it for template instantiation. */
10580 switch (kind)
10581 {
10582 case CPTK_UNDERLYING_TYPE:
10583 return cp_expr (finish_underlying_type (type1), trait_loc);
10584 case CPTK_BASES:
10585 return cp_expr (finish_bases (type1, false), trait_loc);
10586 case CPTK_DIRECT_BASES:
10587 return cp_expr (finish_bases (type1, true), trait_loc);
10588 default:
10589 return finish_trait_expr (trait_loc, kind, type1, type2);
10590 }
10591 }
10592
10593 /* Parse a lambda expression.
10594
10595 lambda-expression:
10596 lambda-introducer lambda-declarator [opt] compound-statement
10597
10598 Returns a representation of the expression. */
10599
10600 static cp_expr
10601 cp_parser_lambda_expression (cp_parser* parser)
10602 {
10603 tree lambda_expr = build_lambda_expr ();
10604 tree type;
10605 bool ok = true;
10606 cp_token *token = cp_lexer_peek_token (parser->lexer);
10607 cp_token_position start = 0;
10608
10609 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10610
10611 if (cxx_dialect >= cxx20)
10612 /* C++20 allows lambdas in unevaluated context. */;
10613 else if (cp_unevaluated_operand)
10614 {
10615 if (!token->error_reported)
10616 {
10617 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10618 "lambda-expression in unevaluated context"
10619 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
10620 token->error_reported = true;
10621 }
10622 ok = false;
10623 }
10624 else if (parser->in_template_argument_list_p || processing_template_parmlist)
10625 {
10626 if (!token->error_reported)
10627 {
10628 error_at (token->location, "lambda-expression in template-argument"
10629 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
10630 token->error_reported = true;
10631 }
10632 ok = false;
10633 }
10634
10635 /* We may be in the middle of deferred access check. Disable
10636 it now. */
10637 push_deferring_access_checks (dk_no_deferred);
10638
10639 cp_parser_lambda_introducer (parser, lambda_expr);
10640 if (cp_parser_error_occurred (parser))
10641 return error_mark_node;
10642
10643 type = begin_lambda_type (lambda_expr);
10644 if (type == error_mark_node)
10645 return error_mark_node;
10646
10647 record_lambda_scope (lambda_expr);
10648
10649 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10650 determine_visibility (TYPE_NAME (type));
10651
10652 /* Now that we've started the type, add the capture fields for any
10653 explicit captures. */
10654 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10655
10656 {
10657 /* Inside the class, surrounding template-parameter-lists do not apply. */
10658 unsigned int saved_num_template_parameter_lists
10659 = parser->num_template_parameter_lists;
10660 unsigned char in_statement = parser->in_statement;
10661 bool in_switch_statement_p = parser->in_switch_statement_p;
10662 bool fully_implicit_function_template_p
10663 = parser->fully_implicit_function_template_p;
10664 tree implicit_template_parms = parser->implicit_template_parms;
10665 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10666 bool auto_is_implicit_function_template_parm_p
10667 = parser->auto_is_implicit_function_template_parm_p;
10668
10669 parser->num_template_parameter_lists = 0;
10670 parser->in_statement = 0;
10671 parser->in_switch_statement_p = false;
10672 parser->fully_implicit_function_template_p = false;
10673 parser->implicit_template_parms = 0;
10674 parser->implicit_template_scope = 0;
10675 parser->auto_is_implicit_function_template_parm_p = false;
10676
10677 /* The body of a lambda in a discarded statement is not discarded. */
10678 bool discarded = in_discarded_stmt;
10679 in_discarded_stmt = 0;
10680
10681 /* By virtue of defining a local class, a lambda expression has access to
10682 the private variables of enclosing classes. */
10683
10684 if (cp_parser_start_tentative_firewall (parser))
10685 start = token;
10686
10687 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10688
10689 if (ok && cp_parser_error_occurred (parser))
10690 ok = false;
10691
10692 if (ok)
10693 {
10694 cp_parser_lambda_body (parser, lambda_expr);
10695 }
10696 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10697 {
10698 if (cp_parser_skip_to_closing_brace (parser))
10699 cp_lexer_consume_token (parser->lexer);
10700 }
10701
10702 /* The capture list was built up in reverse order; fix that now. */
10703 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10704 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10705
10706 if (ok)
10707 maybe_add_lambda_conv_op (type);
10708
10709 finish_struct (type, /*attributes=*/NULL_TREE);
10710
10711 in_discarded_stmt = discarded;
10712
10713 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10714 parser->in_statement = in_statement;
10715 parser->in_switch_statement_p = in_switch_statement_p;
10716 parser->fully_implicit_function_template_p
10717 = fully_implicit_function_template_p;
10718 parser->implicit_template_parms = implicit_template_parms;
10719 parser->implicit_template_scope = implicit_template_scope;
10720 parser->auto_is_implicit_function_template_parm_p
10721 = auto_is_implicit_function_template_parm_p;
10722 }
10723
10724 /* This field is only used during parsing of the lambda. */
10725 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10726
10727 /* This lambda shouldn't have any proxies left at this point. */
10728 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10729 /* And now that we're done, push proxies for an enclosing lambda. */
10730 insert_pending_capture_proxies ();
10731
10732 /* Update the lambda expression to a range. */
10733 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10734 token->location,
10735 parser->lexer);
10736
10737 if (ok)
10738 lambda_expr = build_lambda_object (lambda_expr);
10739 else
10740 lambda_expr = error_mark_node;
10741
10742 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10743
10744 pop_deferring_access_checks ();
10745
10746 return lambda_expr;
10747 }
10748
10749 /* Parse the beginning of a lambda expression.
10750
10751 lambda-introducer:
10752 [ lambda-capture [opt] ]
10753
10754 LAMBDA_EXPR is the current representation of the lambda expression. */
10755
10756 static void
10757 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10758 {
10759 /* Need commas after the first capture. */
10760 bool first = true;
10761
10762 /* Eat the leading `['. */
10763 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10764
10765 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10766 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10767 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS)
10768 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
10769 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10770 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10771 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10772 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10773
10774 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10775 {
10776 cp_lexer_consume_token (parser->lexer);
10777 first = false;
10778
10779 if (!(at_function_scope_p () || parsing_nsdmi ()))
10780 error ("non-local lambda expression cannot have a capture-default");
10781 }
10782
10783 hash_set<tree, true> ids;
10784 tree first_capture_id = NULL_TREE;
10785 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10786 {
10787 cp_token* capture_token;
10788 tree capture_id;
10789 tree capture_init_expr;
10790 cp_id_kind idk = CP_ID_KIND_NONE;
10791 bool explicit_init_p = false;
10792
10793 enum capture_kind_type
10794 {
10795 BY_COPY,
10796 BY_REFERENCE
10797 };
10798 enum capture_kind_type capture_kind = BY_COPY;
10799
10800 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10801 {
10802 error ("expected end of capture-list");
10803 return;
10804 }
10805
10806 if (first)
10807 first = false;
10808 else
10809 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10810
10811 /* Possibly capture `this'. */
10812 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10813 {
10814 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10815 if (cxx_dialect < cxx20
10816 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10817 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10818 "with by-copy capture default");
10819 cp_lexer_consume_token (parser->lexer);
10820 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10821 pedwarn (input_location, 0,
10822 "already captured %qD in lambda expression",
10823 this_identifier);
10824 else
10825 add_capture (lambda_expr, /*id=*/this_identifier,
10826 /*initializer=*/finish_this_expr (),
10827 /*by_reference_p=*/true, explicit_init_p);
10828 continue;
10829 }
10830
10831 /* Possibly capture `*this'. */
10832 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10833 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10834 {
10835 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10836 if (cxx_dialect < cxx17)
10837 pedwarn (loc, 0, "%<*this%> capture only available with "
10838 "%<-std=c++17%> or %<-std=gnu++17%>");
10839 cp_lexer_consume_token (parser->lexer);
10840 cp_lexer_consume_token (parser->lexer);
10841 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10842 pedwarn (input_location, 0,
10843 "already captured %qD in lambda expression",
10844 this_identifier);
10845 else
10846 add_capture (lambda_expr, /*id=*/this_identifier,
10847 /*initializer=*/finish_this_expr (),
10848 /*by_reference_p=*/false, explicit_init_p);
10849 continue;
10850 }
10851
10852 /* But reject `&this'. */
10853 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10854 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10855 {
10856 error_at (cp_lexer_peek_token (parser->lexer)->location,
10857 "%<this%> cannot be captured by reference");
10858 cp_lexer_consume_token (parser->lexer);
10859 cp_lexer_consume_token (parser->lexer);
10860 continue;
10861 }
10862
10863 /* Remember whether we want to capture as a reference or not. */
10864 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10865 {
10866 capture_kind = BY_REFERENCE;
10867 cp_lexer_consume_token (parser->lexer);
10868 }
10869
10870 bool init_pack_expansion = false;
10871 location_t ellipsis_loc = UNKNOWN_LOCATION;
10872 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10873 {
10874 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
10875 if (cxx_dialect < cxx20)
10876 pedwarn (ellipsis_loc, 0, "pack init-capture only available with "
10877 "%<-std=c++20%> or %<-std=gnu++20%>");
10878 cp_lexer_consume_token (parser->lexer);
10879 init_pack_expansion = true;
10880 }
10881
10882 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
10883 if (init_pack_expansion && capture_kind != BY_REFERENCE
10884 && cp_lexer_next_token_is (parser->lexer, CPP_AND))
10885 {
10886 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
10887 0, "%<&%> should come before %<...%>");
10888 capture_kind = BY_REFERENCE;
10889 cp_lexer_consume_token (parser->lexer);
10890 }
10891
10892 /* Get the identifier. */
10893 capture_token = cp_lexer_peek_token (parser->lexer);
10894 capture_id = cp_parser_identifier (parser);
10895
10896 if (capture_id == error_mark_node)
10897 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10898 delimiters, but I modified this to stop on unnested ']' as well. It
10899 was already changed to stop on unnested '}', so the
10900 "closing_parenthesis" name is no more misleading with my change. */
10901 {
10902 cp_parser_skip_to_closing_parenthesis (parser,
10903 /*recovering=*/true,
10904 /*or_comma=*/true,
10905 /*consume_paren=*/true);
10906 break;
10907 }
10908
10909 /* Find the initializer for this capture. */
10910 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10911 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10912 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10913 {
10914 bool direct, non_constant;
10915 /* An explicit initializer exists. */
10916 if (cxx_dialect < cxx14)
10917 pedwarn (input_location, 0,
10918 "lambda capture initializers "
10919 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
10920 capture_init_expr = cp_parser_initializer (parser, &direct,
10921 &non_constant, true);
10922 explicit_init_p = true;
10923 if (capture_init_expr == NULL_TREE)
10924 {
10925 error ("empty initializer for lambda init-capture");
10926 capture_init_expr = error_mark_node;
10927 }
10928 if (init_pack_expansion)
10929 capture_init_expr = make_pack_expansion (capture_init_expr);
10930 }
10931 else
10932 {
10933 const char* error_msg;
10934
10935 /* Turn the identifier into an id-expression. */
10936 capture_init_expr
10937 = cp_parser_lookup_name_simple (parser, capture_id,
10938 capture_token->location);
10939
10940 if (capture_init_expr == error_mark_node)
10941 {
10942 unqualified_name_lookup_error (capture_id);
10943 continue;
10944 }
10945 else if (!VAR_P (capture_init_expr)
10946 && TREE_CODE (capture_init_expr) != PARM_DECL)
10947 {
10948 error_at (capture_token->location,
10949 "capture of non-variable %qE",
10950 capture_init_expr);
10951 if (DECL_P (capture_init_expr))
10952 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10953 "%q#D declared here", capture_init_expr);
10954 continue;
10955 }
10956 if (VAR_P (capture_init_expr)
10957 && decl_storage_duration (capture_init_expr) != dk_auto)
10958 {
10959 if (pedwarn (capture_token->location, 0, "capture of variable "
10960 "%qD with non-automatic storage duration",
10961 capture_init_expr))
10962 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10963 "%q#D declared here", capture_init_expr);
10964 continue;
10965 }
10966
10967 capture_init_expr
10968 = finish_id_expression
10969 (capture_id,
10970 capture_init_expr,
10971 parser->scope,
10972 &idk,
10973 /*integral_constant_expression_p=*/false,
10974 /*allow_non_integral_constant_expression_p=*/false,
10975 /*non_integral_constant_expression_p=*/NULL,
10976 /*template_p=*/false,
10977 /*done=*/true,
10978 /*address_p=*/false,
10979 /*template_arg_p=*/false,
10980 &error_msg,
10981 capture_token->location);
10982
10983 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10984 {
10985 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10986 cp_lexer_consume_token (parser->lexer);
10987 capture_init_expr = make_pack_expansion (capture_init_expr);
10988 if (init_pack_expansion)
10989 {
10990 /* If what follows is an initializer, the second '...' is
10991 invalid. But for cases like [...xs...], the first one
10992 is invalid. */
10993 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10994 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10995 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10996 ellipsis_loc = loc;
10997 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
10998 continue;
10999 }
11000 }
11001 }
11002
11003 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11004 && !explicit_init_p)
11005 {
11006 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
11007 && capture_kind == BY_COPY)
11008 pedwarn (capture_token->location, 0, "explicit by-copy capture "
11009 "of %qD redundant with by-copy capture default",
11010 capture_id);
11011 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
11012 && capture_kind == BY_REFERENCE)
11013 pedwarn (capture_token->location, 0, "explicit by-reference "
11014 "capture of %qD redundant with by-reference capture "
11015 "default", capture_id);
11016 }
11017
11018 /* Check for duplicates.
11019 Optimize for the zero or one explicit captures cases and only create
11020 the hash_set after adding second capture. */
11021 bool found = false;
11022 if (!ids.is_empty ())
11023 found = ids.add (capture_id);
11024 else if (first_capture_id == NULL_TREE)
11025 first_capture_id = capture_id;
11026 else if (capture_id == first_capture_id)
11027 found = true;
11028 else
11029 {
11030 ids.add (first_capture_id);
11031 ids.add (capture_id);
11032 }
11033 if (found)
11034 pedwarn (input_location, 0,
11035 "already captured %qD in lambda expression", capture_id);
11036 else
11037 add_capture (lambda_expr, capture_id, capture_init_expr,
11038 /*by_reference_p=*/capture_kind == BY_REFERENCE,
11039 explicit_init_p);
11040
11041 /* If there is any qualification still in effect, clear it
11042 now; we will be starting fresh with the next capture. */
11043 parser->scope = NULL_TREE;
11044 parser->qualifying_scope = NULL_TREE;
11045 parser->object_scope = NULL_TREE;
11046 }
11047
11048 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11049 }
11050
11051 /* Parse the (optional) middle of a lambda expression.
11052
11053 lambda-declarator:
11054 < template-parameter-list [opt] >
11055 requires-clause [opt]
11056 ( parameter-declaration-clause [opt] )
11057 attribute-specifier [opt]
11058 decl-specifier-seq [opt]
11059 exception-specification [opt]
11060 lambda-return-type-clause [opt]
11061 requires-clause [opt]
11062
11063 LAMBDA_EXPR is the current representation of the lambda expression. */
11064
11065 static bool
11066 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
11067 {
11068 /* 5.1.1.4 of the standard says:
11069 If a lambda-expression does not include a lambda-declarator, it is as if
11070 the lambda-declarator were ().
11071 This means an empty parameter list, no attributes, and no exception
11072 specification. */
11073 tree param_list = void_list_node;
11074 tree std_attrs = NULL_TREE;
11075 tree gnu_attrs = NULL_TREE;
11076 tree exception_spec = NULL_TREE;
11077 tree template_param_list = NULL_TREE;
11078 tree tx_qual = NULL_TREE;
11079 tree return_type = NULL_TREE;
11080 tree trailing_requires_clause = NULL_TREE;
11081 cp_decl_specifier_seq lambda_specs;
11082 clear_decl_specs (&lambda_specs);
11083 /* A lambda op() is const unless explicitly 'mutable'. */
11084 cp_cv_quals quals = TYPE_QUAL_CONST;
11085
11086 /* The template-parameter-list is optional, but must begin with
11087 an opening angle if present. */
11088 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
11089 {
11090 if (cxx_dialect < cxx14)
11091 pedwarn (parser->lexer->next_token->location, 0,
11092 "lambda templates are only available with "
11093 "%<-std=c++14%> or %<-std=gnu++14%>");
11094 else if (cxx_dialect < cxx20)
11095 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
11096 "lambda templates are only available with "
11097 "%<-std=c++20%> or %<-std=gnu++20%>");
11098
11099 cp_lexer_consume_token (parser->lexer);
11100
11101 template_param_list = cp_parser_template_parameter_list (parser);
11102 cp_parser_skip_to_end_of_template_parameter_list (parser);
11103
11104 /* We may have a constrained generic lambda; parse the requires-clause
11105 immediately after the template-parameter-list and combine with any
11106 shorthand constraints present. */
11107 tree dreqs = cp_parser_requires_clause_opt (parser, true);
11108 if (flag_concepts)
11109 {
11110 tree reqs = get_shorthand_constraints (current_template_parms);
11111 if (dreqs)
11112 reqs = combine_constraint_expressions (reqs, dreqs);
11113 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
11114 }
11115
11116 /* We just processed one more parameter list. */
11117 ++parser->num_template_parameter_lists;
11118 }
11119
11120 /* Committee discussion supports allowing attributes here. */
11121 lambda_specs.attributes = cp_parser_attributes_opt (parser);
11122
11123 /* The parameter-declaration-clause is optional (unless
11124 template-parameter-list was given), but must begin with an
11125 opening parenthesis if present. */
11126 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11127 {
11128 bool is_consteval = false;
11129 /* For C++20, before parsing the parameter list check if there is
11130 a consteval specifier in the corresponding decl-specifier-seq. */
11131 if (cxx_dialect >= cxx20)
11132 {
11133 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1);
11134 cp_lexer_nth_token_is (parser->lexer, n, CPP_KEYWORD); n++)
11135 {
11136 if (cp_lexer_peek_nth_token (parser->lexer, n)->keyword
11137 == RID_CONSTEVAL)
11138 {
11139 is_consteval = true;
11140 break;
11141 }
11142 }
11143 }
11144
11145 matching_parens parens;
11146 parens.consume_open (parser);
11147
11148 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
11149
11150 if (is_consteval)
11151 current_binding_level->immediate_fn_ctx_p = true;
11152
11153 /* Parse parameters. */
11154 param_list
11155 = cp_parser_parameter_declaration_clause
11156 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
11157
11158 /* Default arguments shall not be specified in the
11159 parameter-declaration-clause of a lambda-declarator. */
11160 if (cxx_dialect < cxx14)
11161 for (tree t = param_list; t; t = TREE_CHAIN (t))
11162 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
11163 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
11164 "default argument specified for lambda parameter");
11165
11166 parens.require_close (parser);
11167
11168 /* In the decl-specifier-seq of the lambda-declarator, each
11169 decl-specifier shall either be mutable or constexpr. */
11170 int declares_class_or_enum;
11171 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
11172 && !cp_next_tokens_can_be_gnu_attribute_p (parser))
11173 cp_parser_decl_specifier_seq (parser,
11174 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
11175 &lambda_specs, &declares_class_or_enum);
11176 if (lambda_specs.storage_class == sc_mutable)
11177 {
11178 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
11179 quals = TYPE_UNQUALIFIED;
11180 if (lambda_specs.conflicting_specifiers_p)
11181 error_at (lambda_specs.locations[ds_storage_class],
11182 "duplicate %<mutable%>");
11183 }
11184
11185 tx_qual = cp_parser_tx_qualifier_opt (parser);
11186
11187 /* Parse optional exception specification. */
11188 exception_spec
11189 = cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE);
11190
11191 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11192
11193 /* Parse optional trailing return type. */
11194 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
11195 {
11196 cp_lexer_consume_token (parser->lexer);
11197 return_type = cp_parser_trailing_type_id (parser);
11198 }
11199
11200 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11201 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
11202
11203 /* Parse optional trailing requires clause. */
11204 trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
11205
11206 /* The function parameters must be in scope all the way until after the
11207 trailing-return-type in case of decltype. */
11208 pop_bindings_and_leave_scope ();
11209 }
11210 else if (template_param_list != NULL_TREE) // generate diagnostic
11211 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11212
11213 /* Create the function call operator.
11214
11215 Messing with declarators like this is no uglier than building up the
11216 FUNCTION_DECL by hand, and this is less likely to get out of sync with
11217 other code. */
11218 {
11219 cp_decl_specifier_seq return_type_specs;
11220 cp_declarator* declarator;
11221 tree fco;
11222 void *p;
11223
11224 clear_decl_specs (&return_type_specs);
11225 return_type_specs.type = make_auto ();
11226
11227 if (lambda_specs.locations[ds_constexpr])
11228 {
11229 if (cxx_dialect >= cxx17)
11230 return_type_specs.locations[ds_constexpr]
11231 = lambda_specs.locations[ds_constexpr];
11232 else
11233 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
11234 "lambda only available with %<-std=c++17%> or "
11235 "%<-std=gnu++17%>");
11236 }
11237 if (lambda_specs.locations[ds_consteval])
11238 return_type_specs.locations[ds_consteval]
11239 = lambda_specs.locations[ds_consteval];
11240
11241 p = obstack_alloc (&declarator_obstack, 0);
11242
11243 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
11244 LAMBDA_EXPR_LOCATION (lambda_expr));
11245
11246 declarator = make_call_declarator (declarator, param_list, quals,
11247 VIRT_SPEC_UNSPECIFIED,
11248 REF_QUAL_NONE,
11249 tx_qual,
11250 exception_spec,
11251 return_type,
11252 trailing_requires_clause,
11253 UNKNOWN_LOCATION);
11254 declarator->std_attributes = std_attrs;
11255
11256 fco = grokmethod (&return_type_specs,
11257 declarator,
11258 chainon (gnu_attrs, lambda_specs.attributes));
11259 if (fco != error_mark_node)
11260 {
11261 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
11262 DECL_ARTIFICIAL (fco) = 1;
11263 /* Give the object parameter a different name. */
11264 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
11265 DECL_SET_LAMBDA_FUNCTION (fco, true);
11266 }
11267 if (template_param_list)
11268 {
11269 fco = finish_member_template_decl (fco);
11270 finish_template_decl (template_param_list);
11271 --parser->num_template_parameter_lists;
11272 }
11273 else if (parser->fully_implicit_function_template_p)
11274 fco = finish_fully_implicit_template (parser, fco);
11275
11276 finish_member_declaration (fco);
11277
11278 obstack_free (&declarator_obstack, p);
11279
11280 return (fco != error_mark_node);
11281 }
11282 }
11283
11284 /* Parse the body of a lambda expression, which is simply
11285
11286 compound-statement
11287
11288 but which requires special handling.
11289 LAMBDA_EXPR is the current representation of the lambda expression. */
11290
11291 static void
11292 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
11293 {
11294 bool nested = (current_function_decl != NULL_TREE);
11295 unsigned char local_variables_forbidden_p
11296 = parser->local_variables_forbidden_p;
11297 bool in_function_body = parser->in_function_body;
11298
11299 /* The body of a lambda-expression is not a subexpression of the enclosing
11300 expression. */
11301 cp_evaluated ev;
11302
11303 if (nested)
11304 push_function_context ();
11305 else
11306 /* Still increment function_depth so that we don't GC in the
11307 middle of an expression. */
11308 ++function_depth;
11309
11310 vec<tree> omp_privatization_save;
11311 save_omp_privatization_clauses (omp_privatization_save);
11312 /* Clear this in case we're in the middle of a default argument. */
11313 parser->local_variables_forbidden_p = 0;
11314 parser->in_function_body = true;
11315
11316 {
11317 local_specialization_stack s (lss_copy);
11318 tree fco = lambda_function (lambda_expr);
11319 tree body = start_lambda_function (fco, lambda_expr);
11320
11321 /* Originally C++11 required us to peek for 'return expr'; and
11322 process it specially here to deduce the return type. N3638
11323 removed the need for that. */
11324 cp_parser_function_body (parser, false);
11325
11326 finish_lambda_function (body);
11327 }
11328
11329 restore_omp_privatization_clauses (omp_privatization_save);
11330 parser->local_variables_forbidden_p = local_variables_forbidden_p;
11331 parser->in_function_body = in_function_body;
11332 if (nested)
11333 pop_function_context();
11334 else
11335 --function_depth;
11336 }
11337
11338 /* Statements [gram.stmt.stmt] */
11339
11340 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11341
11342 static void
11343 add_debug_begin_stmt (location_t loc)
11344 {
11345 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11346 return;
11347 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11348 /* A concept is never expanded normally. */
11349 return;
11350
11351 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11352 SET_EXPR_LOCATION (stmt, loc);
11353 add_stmt (stmt);
11354 }
11355
11356 /* Parse a statement.
11357
11358 statement:
11359 labeled-statement
11360 expression-statement
11361 compound-statement
11362 selection-statement
11363 iteration-statement
11364 jump-statement
11365 declaration-statement
11366 try-block
11367
11368 C++11:
11369
11370 statement:
11371 labeled-statement
11372 attribute-specifier-seq (opt) expression-statement
11373 attribute-specifier-seq (opt) compound-statement
11374 attribute-specifier-seq (opt) selection-statement
11375 attribute-specifier-seq (opt) iteration-statement
11376 attribute-specifier-seq (opt) jump-statement
11377 declaration-statement
11378 attribute-specifier-seq (opt) try-block
11379
11380 init-statement:
11381 expression-statement
11382 simple-declaration
11383
11384 TM Extension:
11385
11386 statement:
11387 atomic-statement
11388
11389 IN_COMPOUND is true when the statement is nested inside a
11390 cp_parser_compound_statement; this matters for certain pragmas.
11391
11392 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11393 is a (possibly labeled) if statement which is not enclosed in braces
11394 and has an else clause. This is used to implement -Wparentheses.
11395
11396 CHAIN is a vector of if-else-if conditions. */
11397
11398 static void
11399 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
11400 bool in_compound, bool *if_p, vec<tree> *chain,
11401 location_t *loc_after_labels)
11402 {
11403 tree statement, std_attrs = NULL_TREE;
11404 cp_token *token;
11405 location_t statement_location, attrs_loc;
11406
11407 restart:
11408 if (if_p != NULL)
11409 *if_p = false;
11410 /* There is no statement yet. */
11411 statement = NULL_TREE;
11412
11413 saved_token_sentinel saved_tokens (parser->lexer);
11414 attrs_loc = cp_lexer_peek_token (parser->lexer)->location;
11415 if (c_dialect_objc ())
11416 /* In obj-c++, seeing '[[' might be the either the beginning of
11417 c++11 attributes, or a nested objc-message-expression. So
11418 let's parse the c++11 attributes tentatively. */
11419 cp_parser_parse_tentatively (parser);
11420 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11421 if (std_attrs)
11422 attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
11423 if (c_dialect_objc ())
11424 {
11425 if (!cp_parser_parse_definitely (parser))
11426 std_attrs = NULL_TREE;
11427 }
11428
11429 /* Peek at the next token. */
11430 token = cp_lexer_peek_token (parser->lexer);
11431 /* Remember the location of the first token in the statement. */
11432 cp_token *statement_token = token;
11433 statement_location = token->location;
11434 add_debug_begin_stmt (statement_location);
11435 /* If this is a keyword, then that will often determine what kind of
11436 statement we have. */
11437 if (token->type == CPP_KEYWORD)
11438 {
11439 enum rid keyword = token->keyword;
11440
11441 switch (keyword)
11442 {
11443 case RID_CASE:
11444 case RID_DEFAULT:
11445 /* Looks like a labeled-statement with a case label.
11446 Parse the label, and then use tail recursion to parse
11447 the statement. */
11448 cp_parser_label_for_labeled_statement (parser, std_attrs);
11449 in_compound = false;
11450 goto restart;
11451
11452 case RID_IF:
11453 case RID_SWITCH:
11454 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11455 statement = cp_parser_selection_statement (parser, if_p, chain);
11456 break;
11457
11458 case RID_WHILE:
11459 case RID_DO:
11460 case RID_FOR:
11461 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11462 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
11463 break;
11464
11465 case RID_BREAK:
11466 case RID_CONTINUE:
11467 case RID_RETURN:
11468 case RID_CO_RETURN:
11469 case RID_GOTO:
11470 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11471 statement = cp_parser_jump_statement (parser);
11472 break;
11473
11474 /* Objective-C++ exception-handling constructs. */
11475 case RID_AT_TRY:
11476 case RID_AT_CATCH:
11477 case RID_AT_FINALLY:
11478 case RID_AT_SYNCHRONIZED:
11479 case RID_AT_THROW:
11480 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11481 statement = cp_parser_objc_statement (parser);
11482 break;
11483
11484 case RID_TRY:
11485 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11486 statement = cp_parser_try_block (parser);
11487 break;
11488
11489 case RID_NAMESPACE:
11490 /* This must be a namespace alias definition. */
11491 if (std_attrs != NULL_TREE)
11492 {
11493 /* Attributes should be parsed as part of the
11494 declaration, so let's un-parse them. */
11495 saved_tokens.rollback();
11496 std_attrs = NULL_TREE;
11497 }
11498 cp_parser_declaration_statement (parser);
11499 return;
11500
11501 case RID_TRANSACTION_ATOMIC:
11502 case RID_TRANSACTION_RELAXED:
11503 case RID_SYNCHRONIZED:
11504 case RID_ATOMIC_NOEXCEPT:
11505 case RID_ATOMIC_CANCEL:
11506 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11507 statement = cp_parser_transaction (parser, token);
11508 break;
11509 case RID_TRANSACTION_CANCEL:
11510 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11511 statement = cp_parser_transaction_cancel (parser);
11512 break;
11513
11514 default:
11515 /* It might be a keyword like `int' that can start a
11516 declaration-statement. */
11517 break;
11518 }
11519 }
11520 else if (token->type == CPP_NAME)
11521 {
11522 /* If the next token is a `:', then we are looking at a
11523 labeled-statement. */
11524 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11525 if (token->type == CPP_COLON)
11526 {
11527 /* Looks like a labeled-statement with an ordinary label.
11528 Parse the label, and then use tail recursion to parse
11529 the statement. */
11530
11531 cp_parser_label_for_labeled_statement (parser, std_attrs);
11532 in_compound = false;
11533 goto restart;
11534 }
11535 }
11536 /* Anything that starts with a `{' must be a compound-statement. */
11537 else if (token->type == CPP_OPEN_BRACE)
11538 {
11539 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11540 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11541 }
11542 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11543 a statement all its own. */
11544 else if (token->type == CPP_PRAGMA)
11545 {
11546 /* Only certain OpenMP pragmas are attached to statements, and thus
11547 are considered statements themselves. All others are not. In
11548 the context of a compound, accept the pragma as a "statement" and
11549 return so that we can check for a close brace. Otherwise we
11550 require a real statement and must go back and read one. */
11551 if (in_compound)
11552 cp_parser_pragma (parser, pragma_compound, if_p);
11553 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11554 goto restart;
11555 return;
11556 }
11557 else if (token->type == CPP_EOF)
11558 {
11559 cp_parser_error (parser, "expected statement");
11560 return;
11561 }
11562
11563 /* Everything else must be a declaration-statement or an
11564 expression-statement. Try for the declaration-statement
11565 first, unless we are looking at a `;', in which case we know that
11566 we have an expression-statement. */
11567 if (!statement)
11568 {
11569 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11570 {
11571 if (std_attrs != NULL_TREE)
11572 /* Attributes should be parsed as part of the declaration,
11573 so let's un-parse them. */
11574 saved_tokens.rollback();
11575
11576 cp_parser_parse_tentatively (parser);
11577 /* Try to parse the declaration-statement. */
11578 cp_parser_declaration_statement (parser);
11579 /* If that worked, we're done. */
11580 if (cp_parser_parse_definitely (parser))
11581 return;
11582 /* It didn't work, restore the post-attribute position. */
11583 if (std_attrs)
11584 cp_lexer_set_token_position (parser->lexer, statement_token);
11585 }
11586 /* All preceding labels have been parsed at this point. */
11587 if (loc_after_labels != NULL)
11588 *loc_after_labels = statement_location;
11589
11590 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11591
11592 /* Look for an expression-statement instead. */
11593 statement = cp_parser_expression_statement (parser, in_statement_expr);
11594
11595 /* Handle [[fallthrough]];. */
11596 if (attribute_fallthrough_p (std_attrs))
11597 {
11598 /* The next token after the fallthrough attribute is ';'. */
11599 if (statement == NULL_TREE)
11600 {
11601 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11602 statement = build_call_expr_internal_loc (statement_location,
11603 IFN_FALLTHROUGH,
11604 void_type_node, 0);
11605 finish_expr_stmt (statement);
11606 }
11607 else
11608 warning_at (statement_location, OPT_Wattributes,
11609 "%<fallthrough%> attribute not followed by %<;%>");
11610 std_attrs = NULL_TREE;
11611 }
11612 }
11613
11614 /* Set the line number for the statement. */
11615 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11616 SET_EXPR_LOCATION (statement, statement_location);
11617
11618 /* Allow "[[fallthrough]];", but warn otherwise. */
11619 if (std_attrs != NULL_TREE)
11620 warning_at (attrs_loc,
11621 OPT_Wattributes,
11622 "attributes at the beginning of statement are ignored");
11623 }
11624
11625 /* Append ATTR to attribute list ATTRS. */
11626
11627 static tree
11628 attr_chainon (tree attrs, tree attr)
11629 {
11630 if (attrs == error_mark_node)
11631 return error_mark_node;
11632 if (attr == error_mark_node)
11633 return error_mark_node;
11634 return chainon (attrs, attr);
11635 }
11636
11637 /* Parse the label for a labeled-statement, i.e.
11638
11639 identifier :
11640 case constant-expression :
11641 default :
11642
11643 GNU Extension:
11644 case constant-expression ... constant-expression : statement
11645
11646 When a label is parsed without errors, the label is added to the
11647 parse tree by the finish_* functions, so this function doesn't
11648 have to return the label. */
11649
11650 static void
11651 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11652 {
11653 cp_token *token;
11654 tree label = NULL_TREE;
11655 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11656
11657 /* The next token should be an identifier. */
11658 token = cp_lexer_peek_token (parser->lexer);
11659 if (token->type != CPP_NAME
11660 && token->type != CPP_KEYWORD)
11661 {
11662 cp_parser_error (parser, "expected labeled-statement");
11663 return;
11664 }
11665
11666 /* Remember whether this case or a user-defined label is allowed to fall
11667 through to. */
11668 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11669
11670 parser->colon_corrects_to_scope_p = false;
11671 switch (token->keyword)
11672 {
11673 case RID_CASE:
11674 {
11675 tree expr, expr_hi;
11676 cp_token *ellipsis;
11677
11678 /* Consume the `case' token. */
11679 cp_lexer_consume_token (parser->lexer);
11680 /* Parse the constant-expression. */
11681 expr = cp_parser_constant_expression (parser);
11682 if (check_for_bare_parameter_packs (expr))
11683 expr = error_mark_node;
11684
11685 ellipsis = cp_lexer_peek_token (parser->lexer);
11686 if (ellipsis->type == CPP_ELLIPSIS)
11687 {
11688 /* Consume the `...' token. */
11689 cp_lexer_consume_token (parser->lexer);
11690 expr_hi = cp_parser_constant_expression (parser);
11691 if (check_for_bare_parameter_packs (expr_hi))
11692 expr_hi = error_mark_node;
11693
11694 /* We don't need to emit warnings here, as the common code
11695 will do this for us. */
11696 }
11697 else
11698 expr_hi = NULL_TREE;
11699
11700 if (parser->in_switch_statement_p)
11701 {
11702 tree l = finish_case_label (token->location, expr, expr_hi);
11703 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11704 {
11705 label = CASE_LABEL (l);
11706 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11707 }
11708 }
11709 else
11710 error_at (token->location,
11711 "case label %qE not within a switch statement",
11712 expr);
11713 }
11714 break;
11715
11716 case RID_DEFAULT:
11717 /* Consume the `default' token. */
11718 cp_lexer_consume_token (parser->lexer);
11719
11720 if (parser->in_switch_statement_p)
11721 {
11722 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11723 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11724 {
11725 label = CASE_LABEL (l);
11726 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11727 }
11728 }
11729 else
11730 error_at (token->location, "case label not within a switch statement");
11731 break;
11732
11733 default:
11734 /* Anything else must be an ordinary label. */
11735 label = finish_label_stmt (cp_parser_identifier (parser));
11736 if (label && TREE_CODE (label) == LABEL_DECL)
11737 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11738 break;
11739 }
11740
11741 /* Require the `:' token. */
11742 cp_parser_require (parser, CPP_COLON, RT_COLON);
11743
11744 /* An ordinary label may optionally be followed by attributes.
11745 However, this is only permitted if the attributes are then
11746 followed by a semicolon. This is because, for backward
11747 compatibility, when parsing
11748 lab: __attribute__ ((unused)) int i;
11749 we want the attribute to attach to "i", not "lab". */
11750 if (label != NULL_TREE
11751 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11752 {
11753 tree attrs;
11754 cp_parser_parse_tentatively (parser);
11755 attrs = cp_parser_gnu_attributes_opt (parser);
11756 if (attrs == NULL_TREE
11757 /* And fallthrough always binds to the expression-statement. */
11758 || attribute_fallthrough_p (attrs)
11759 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11760 cp_parser_abort_tentative_parse (parser);
11761 else if (!cp_parser_parse_definitely (parser))
11762 ;
11763 else
11764 attributes = attr_chainon (attributes, attrs);
11765 }
11766
11767 if (attributes != NULL_TREE)
11768 cplus_decl_attributes (&label, attributes, 0);
11769
11770 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11771 }
11772
11773 /* Parse an expression-statement.
11774
11775 expression-statement:
11776 expression [opt] ;
11777
11778 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11779 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11780 indicates whether this expression-statement is part of an
11781 expression statement. */
11782
11783 static tree
11784 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11785 {
11786 tree statement = NULL_TREE;
11787 cp_token *token = cp_lexer_peek_token (parser->lexer);
11788 location_t loc = token->location;
11789
11790 /* There might be attribute fallthrough. */
11791 tree attr = cp_parser_gnu_attributes_opt (parser);
11792
11793 /* If the next token is a ';', then there is no expression
11794 statement. */
11795 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11796 {
11797 statement = cp_parser_expression (parser);
11798 if (statement == error_mark_node
11799 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11800 {
11801 cp_parser_skip_to_end_of_block_or_statement (parser);
11802 return error_mark_node;
11803 }
11804 }
11805
11806 /* Handle [[fallthrough]];. */
11807 if (attribute_fallthrough_p (attr))
11808 {
11809 /* The next token after the fallthrough attribute is ';'. */
11810 if (statement == NULL_TREE)
11811 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11812 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11813 void_type_node, 0);
11814 else
11815 warning_at (loc, OPT_Wattributes,
11816 "%<fallthrough%> attribute not followed by %<;%>");
11817 attr = NULL_TREE;
11818 }
11819
11820 /* Allow "[[fallthrough]];", but warn otherwise. */
11821 if (attr != NULL_TREE)
11822 warning_at (loc, OPT_Wattributes,
11823 "attributes at the beginning of statement are ignored");
11824
11825 /* Give a helpful message for "A<T>::type t;" and the like. */
11826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11827 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11828 {
11829 if (TREE_CODE (statement) == SCOPE_REF)
11830 error_at (token->location, "need %<typename%> before %qE because "
11831 "%qT is a dependent scope",
11832 statement, TREE_OPERAND (statement, 0));
11833 else if (is_overloaded_fn (statement)
11834 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11835 {
11836 /* A::A a; */
11837 tree fn = get_first_fn (statement);
11838 error_at (token->location,
11839 "%<%T::%D%> names the constructor, not the type",
11840 DECL_CONTEXT (fn), DECL_NAME (fn));
11841 }
11842 }
11843
11844 /* Consume the final `;'. */
11845 cp_parser_consume_semicolon_at_end_of_statement (parser);
11846
11847 if (in_statement_expr
11848 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11849 /* This is the final expression statement of a statement
11850 expression. */
11851 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11852 else if (statement)
11853 statement = finish_expr_stmt (statement);
11854
11855 return statement;
11856 }
11857
11858 /* Parse a compound-statement.
11859
11860 compound-statement:
11861 { statement-seq [opt] }
11862
11863 GNU extension:
11864
11865 compound-statement:
11866 { label-declaration-seq [opt] statement-seq [opt] }
11867
11868 label-declaration-seq:
11869 label-declaration
11870 label-declaration-seq label-declaration
11871
11872 Returns a tree representing the statement. */
11873
11874 static tree
11875 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11876 int bcs_flags, bool function_body)
11877 {
11878 tree compound_stmt;
11879 matching_braces braces;
11880
11881 /* Consume the `{'. */
11882 if (!braces.require_open (parser))
11883 return error_mark_node;
11884 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11885 && !function_body && cxx_dialect < cxx14)
11886 pedwarn (input_location, OPT_Wpedantic,
11887 "compound-statement in %<constexpr%> function");
11888 /* Begin the compound-statement. */
11889 compound_stmt = begin_compound_stmt (bcs_flags);
11890 /* If the next keyword is `__label__' we have a label declaration. */
11891 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11892 cp_parser_label_declaration (parser);
11893 /* Parse an (optional) statement-seq. */
11894 cp_parser_statement_seq_opt (parser, in_statement_expr);
11895
11896 if (function_body)
11897 maybe_splice_retval_cleanup (compound_stmt);
11898
11899 /* Finish the compound-statement. */
11900 finish_compound_stmt (compound_stmt);
11901 /* Consume the `}'. */
11902 braces.require_close (parser);
11903
11904 return compound_stmt;
11905 }
11906
11907 /* Parse an (optional) statement-seq.
11908
11909 statement-seq:
11910 statement
11911 statement-seq [opt] statement */
11912
11913 static void
11914 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11915 {
11916 /* Scan statements until there aren't any more. */
11917 while (true)
11918 {
11919 cp_token *token = cp_lexer_peek_token (parser->lexer);
11920
11921 /* If we are looking at a `}', then we have run out of
11922 statements; the same is true if we have reached the end
11923 of file, or have stumbled upon a stray '@end'. */
11924 if (token->type == CPP_CLOSE_BRACE
11925 || token->type == CPP_EOF
11926 || token->type == CPP_PRAGMA_EOL
11927 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11928 break;
11929
11930 /* If we are in a compound statement and find 'else' then
11931 something went wrong. */
11932 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11933 {
11934 if (parser->in_statement & IN_IF_STMT)
11935 break;
11936 else
11937 {
11938 token = cp_lexer_consume_token (parser->lexer);
11939 error_at (token->location, "%<else%> without a previous %<if%>");
11940 }
11941 }
11942
11943 /* Parse the statement. */
11944 cp_parser_statement (parser, in_statement_expr, true, NULL);
11945 }
11946 }
11947
11948 /* Return true if this is the C++20 version of range-based-for with
11949 init-statement. */
11950
11951 static bool
11952 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11953 {
11954 bool r = false;
11955
11956 /* Save tokens so that we can put them back. */
11957 cp_lexer_save_tokens (parser->lexer);
11958
11959 /* There has to be an unnested ; followed by an unnested :. */
11960 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11961 /*recovering=*/false,
11962 CPP_SEMICOLON,
11963 /*consume_paren=*/false) != -1)
11964 goto out;
11965
11966 /* We found the semicolon, eat it now. */
11967 cp_lexer_consume_token (parser->lexer);
11968
11969 /* Now look for ':' that is not nested in () or {}. */
11970 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11971 /*recovering=*/false,
11972 CPP_COLON,
11973 /*consume_paren=*/false) == -1);
11974
11975 out:
11976 /* Roll back the tokens we skipped. */
11977 cp_lexer_rollback_tokens (parser->lexer);
11978
11979 return r;
11980 }
11981
11982 /* Return true if we're looking at (init; cond), false otherwise. */
11983
11984 static bool
11985 cp_parser_init_statement_p (cp_parser *parser)
11986 {
11987 /* Save tokens so that we can put them back. */
11988 cp_lexer_save_tokens (parser->lexer);
11989
11990 /* Look for ';' that is not nested in () or {}. */
11991 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11992 /*recovering=*/false,
11993 CPP_SEMICOLON,
11994 /*consume_paren=*/false);
11995
11996 /* Roll back the tokens we skipped. */
11997 cp_lexer_rollback_tokens (parser->lexer);
11998
11999 return ret == -1;
12000 }
12001
12002 /* Parse a selection-statement.
12003
12004 selection-statement:
12005 if ( init-statement [opt] condition ) statement
12006 if ( init-statement [opt] condition ) statement else statement
12007 switch ( init-statement [opt] condition ) statement
12008
12009 Returns the new IF_STMT or SWITCH_STMT.
12010
12011 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12012 is a (possibly labeled) if statement which is not enclosed in
12013 braces and has an else clause. This is used to implement
12014 -Wparentheses.
12015
12016 CHAIN is a vector of if-else-if conditions. This is used to implement
12017 -Wduplicated-cond. */
12018
12019 static tree
12020 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
12021 vec<tree> *chain)
12022 {
12023 cp_token *token;
12024 enum rid keyword;
12025 token_indent_info guard_tinfo;
12026
12027 if (if_p != NULL)
12028 *if_p = false;
12029
12030 /* Peek at the next token. */
12031 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
12032 guard_tinfo = get_token_indent_info (token);
12033
12034 /* See what kind of keyword it is. */
12035 keyword = token->keyword;
12036 switch (keyword)
12037 {
12038 case RID_IF:
12039 case RID_SWITCH:
12040 {
12041 tree statement;
12042 tree condition;
12043
12044 bool cx = false;
12045 if (keyword == RID_IF
12046 && cp_lexer_next_token_is_keyword (parser->lexer,
12047 RID_CONSTEXPR))
12048 {
12049 cx = true;
12050 cp_token *tok = cp_lexer_consume_token (parser->lexer);
12051 if (cxx_dialect < cxx17)
12052 pedwarn (tok->location, 0, "%<if constexpr%> only available "
12053 "with %<-std=c++17%> or %<-std=gnu++17%>");
12054 }
12055
12056 /* Look for the `('. */
12057 matching_parens parens;
12058 if (!parens.require_open (parser))
12059 {
12060 cp_parser_skip_to_end_of_statement (parser);
12061 return error_mark_node;
12062 }
12063
12064 /* Begin the selection-statement. */
12065 if (keyword == RID_IF)
12066 {
12067 statement = begin_if_stmt ();
12068 IF_STMT_CONSTEXPR_P (statement) = cx;
12069 }
12070 else
12071 statement = begin_switch_stmt ();
12072
12073 /* Parse the optional init-statement. */
12074 if (cp_parser_init_statement_p (parser))
12075 {
12076 tree decl;
12077 if (cxx_dialect < cxx17)
12078 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12079 "init-statement in selection statements only available "
12080 "with %<-std=c++17%> or %<-std=gnu++17%>");
12081 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12082 {
12083 /* A non-empty init-statement can have arbitrary side
12084 effects. */
12085 delete chain;
12086 chain = NULL;
12087 }
12088 cp_parser_init_statement (parser, &decl);
12089 }
12090
12091 /* Parse the condition. */
12092 condition = cp_parser_condition (parser);
12093 /* Look for the `)'. */
12094 if (!parens.require_close (parser))
12095 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12096 /*consume_paren=*/true);
12097
12098 if (keyword == RID_IF)
12099 {
12100 bool nested_if;
12101 unsigned char in_statement;
12102
12103 /* Add the condition. */
12104 condition = finish_if_stmt_cond (condition, statement);
12105
12106 if (warn_duplicated_cond)
12107 warn_duplicated_cond_add_or_warn (token->location, condition,
12108 &chain);
12109
12110 /* Parse the then-clause. */
12111 in_statement = parser->in_statement;
12112 parser->in_statement |= IN_IF_STMT;
12113
12114 /* Outside a template, the non-selected branch of a constexpr
12115 if is a 'discarded statement', i.e. unevaluated. */
12116 bool was_discarded = in_discarded_stmt;
12117 bool discard_then = (cx && !processing_template_decl
12118 && integer_zerop (condition));
12119 if (discard_then)
12120 {
12121 in_discarded_stmt = true;
12122 ++c_inhibit_evaluation_warnings;
12123 }
12124
12125 cp_parser_implicitly_scoped_statement (parser, &nested_if,
12126 guard_tinfo);
12127
12128 parser->in_statement = in_statement;
12129
12130 finish_then_clause (statement);
12131
12132 if (discard_then)
12133 {
12134 THEN_CLAUSE (statement) = NULL_TREE;
12135 in_discarded_stmt = was_discarded;
12136 --c_inhibit_evaluation_warnings;
12137 }
12138
12139 /* If the next token is `else', parse the else-clause. */
12140 if (cp_lexer_next_token_is_keyword (parser->lexer,
12141 RID_ELSE))
12142 {
12143 bool discard_else = (cx && !processing_template_decl
12144 && integer_nonzerop (condition));
12145 if (discard_else)
12146 {
12147 in_discarded_stmt = true;
12148 ++c_inhibit_evaluation_warnings;
12149 }
12150
12151 guard_tinfo
12152 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12153 /* Consume the `else' keyword. */
12154 cp_lexer_consume_token (parser->lexer);
12155 if (warn_duplicated_cond)
12156 {
12157 if (cp_lexer_next_token_is_keyword (parser->lexer,
12158 RID_IF)
12159 && chain == NULL)
12160 {
12161 /* We've got "if (COND) else if (COND2)". Start
12162 the condition chain and add COND as the first
12163 element. */
12164 chain = new vec<tree> ();
12165 if (!CONSTANT_CLASS_P (condition)
12166 && !TREE_SIDE_EFFECTS (condition))
12167 {
12168 /* Wrap it in a NOP_EXPR so that we can set the
12169 location of the condition. */
12170 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
12171 condition);
12172 SET_EXPR_LOCATION (e, token->location);
12173 chain->safe_push (e);
12174 }
12175 }
12176 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
12177 RID_IF))
12178 {
12179 /* This is if-else without subsequent if. Zap the
12180 condition chain; we would have already warned at
12181 this point. */
12182 delete chain;
12183 chain = NULL;
12184 }
12185 }
12186 begin_else_clause (statement);
12187 /* Parse the else-clause. */
12188 cp_parser_implicitly_scoped_statement (parser, NULL,
12189 guard_tinfo, chain);
12190
12191 finish_else_clause (statement);
12192
12193 /* If we are currently parsing a then-clause, then
12194 IF_P will not be NULL. We set it to true to
12195 indicate that this if statement has an else clause.
12196 This may trigger the Wparentheses warning below
12197 when we get back up to the parent if statement. */
12198 if (if_p != NULL)
12199 *if_p = true;
12200
12201 if (discard_else)
12202 {
12203 ELSE_CLAUSE (statement) = NULL_TREE;
12204 in_discarded_stmt = was_discarded;
12205 --c_inhibit_evaluation_warnings;
12206 }
12207 }
12208 else
12209 {
12210 /* This if statement does not have an else clause. If
12211 NESTED_IF is true, then the then-clause has an if
12212 statement which does have an else clause. We warn
12213 about the potential ambiguity. */
12214 if (nested_if)
12215 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
12216 "suggest explicit braces to avoid ambiguous"
12217 " %<else%>");
12218 if (warn_duplicated_cond)
12219 {
12220 /* We don't need the condition chain anymore. */
12221 delete chain;
12222 chain = NULL;
12223 }
12224 }
12225
12226 /* Now we're all done with the if-statement. */
12227 finish_if_stmt (statement);
12228 }
12229 else
12230 {
12231 bool in_switch_statement_p;
12232 unsigned char in_statement;
12233
12234 /* Add the condition. */
12235 finish_switch_cond (condition, statement);
12236
12237 /* Parse the body of the switch-statement. */
12238 in_switch_statement_p = parser->in_switch_statement_p;
12239 in_statement = parser->in_statement;
12240 parser->in_switch_statement_p = true;
12241 parser->in_statement |= IN_SWITCH_STMT;
12242 cp_parser_implicitly_scoped_statement (parser, if_p,
12243 guard_tinfo);
12244 parser->in_switch_statement_p = in_switch_statement_p;
12245 parser->in_statement = in_statement;
12246
12247 /* Now we're all done with the switch-statement. */
12248 finish_switch_stmt (statement);
12249 }
12250
12251 return statement;
12252 }
12253 break;
12254
12255 default:
12256 cp_parser_error (parser, "expected selection-statement");
12257 return error_mark_node;
12258 }
12259 }
12260
12261 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
12262 If we have seen at least one decl-specifier, and the next token is not
12263 a parenthesis (after "int (" we might be looking at a functional cast)
12264 neither we are dealing with a concept-check expression then we must be
12265 looking at a declaration. */
12266
12267 static void
12268 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
12269 cp_decl_specifier_seq *decl_specs)
12270 {
12271 if (decl_specs->any_specifiers_p
12272 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12273 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12274 && !cp_parser_error_occurred (parser)
12275 && !(decl_specs->type
12276 && TREE_CODE (decl_specs->type) == TYPE_DECL
12277 && is_constrained_auto (TREE_TYPE (decl_specs->type))))
12278 cp_parser_commit_to_tentative_parse (parser);
12279 }
12280
12281 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
12282 The declarator shall not specify a function or an array. Returns
12283 TRUE if the declarator is valid, FALSE otherwise. */
12284
12285 static bool
12286 cp_parser_check_condition_declarator (cp_parser* parser,
12287 cp_declarator *declarator,
12288 location_t loc)
12289 {
12290 if (declarator == cp_error_declarator
12291 || function_declarator_p (declarator)
12292 || declarator->kind == cdk_array)
12293 {
12294 if (declarator == cp_error_declarator)
12295 /* Already complained. */;
12296 else if (declarator->kind == cdk_array)
12297 error_at (loc, "condition declares an array");
12298 else
12299 error_at (loc, "condition declares a function");
12300 if (parser->fully_implicit_function_template_p)
12301 abort_fully_implicit_template (parser);
12302 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
12303 /*or_comma=*/false,
12304 /*consume_paren=*/false);
12305 return false;
12306 }
12307 else
12308 return true;
12309 }
12310
12311 /* Parse a condition.
12312
12313 condition:
12314 expression
12315 type-specifier-seq declarator = initializer-clause
12316 type-specifier-seq declarator braced-init-list
12317
12318 GNU Extension:
12319
12320 condition:
12321 type-specifier-seq declarator asm-specification [opt]
12322 attributes [opt] = assignment-expression
12323
12324 Returns the expression that should be tested. */
12325
12326 static tree
12327 cp_parser_condition (cp_parser* parser)
12328 {
12329 cp_decl_specifier_seq type_specifiers;
12330 const char *saved_message;
12331 int declares_class_or_enum;
12332
12333 /* Try the declaration first. */
12334 cp_parser_parse_tentatively (parser);
12335 /* New types are not allowed in the type-specifier-seq for a
12336 condition. */
12337 saved_message = parser->type_definition_forbidden_message;
12338 parser->type_definition_forbidden_message
12339 = G_("types may not be defined in conditions");
12340 /* Parse the type-specifier-seq. */
12341 cp_parser_decl_specifier_seq (parser,
12342 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
12343 &type_specifiers,
12344 &declares_class_or_enum);
12345 /* Restore the saved message. */
12346 parser->type_definition_forbidden_message = saved_message;
12347
12348 /* Gather the attributes that were provided with the
12349 decl-specifiers. */
12350 tree prefix_attributes = type_specifiers.attributes;
12351
12352 cp_parser_maybe_commit_to_declaration (parser, &type_specifiers);
12353
12354 /* If all is well, we might be looking at a declaration. */
12355 if (!cp_parser_error_occurred (parser))
12356 {
12357 tree decl;
12358 tree asm_specification;
12359 tree attributes;
12360 cp_declarator *declarator;
12361 tree initializer = NULL_TREE;
12362 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12363
12364 /* Parse the declarator. */
12365 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12366 CP_PARSER_FLAGS_NONE,
12367 /*ctor_dtor_or_conv_p=*/NULL,
12368 /*parenthesized_p=*/NULL,
12369 /*member_p=*/false,
12370 /*friend_p=*/false,
12371 /*static_p=*/false);
12372 /* Parse the attributes. */
12373 attributes = cp_parser_attributes_opt (parser);
12374 /* Parse the asm-specification. */
12375 asm_specification = cp_parser_asm_specification_opt (parser);
12376 /* If the next token is not an `=' or '{', then we might still be
12377 looking at an expression. For example:
12378
12379 if (A(a).x)
12380
12381 looks like a decl-specifier-seq and a declarator -- but then
12382 there is no `=', so this is an expression. */
12383 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12384 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12385 cp_parser_simulate_error (parser);
12386
12387 /* If we did see an `=' or '{', then we are looking at a declaration
12388 for sure. */
12389 if (cp_parser_parse_definitely (parser))
12390 {
12391 tree pushed_scope;
12392 bool non_constant_p = false;
12393 int flags = LOOKUP_ONLYCONVERTING;
12394
12395 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
12396 return error_mark_node;
12397
12398 /* Create the declaration. */
12399 decl = start_decl (declarator, &type_specifiers,
12400 /*initialized_p=*/true,
12401 attributes, prefix_attributes,
12402 &pushed_scope);
12403
12404 /* Parse the initializer. */
12405 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12406 {
12407 initializer = cp_parser_braced_list (parser, &non_constant_p);
12408 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
12409 flags = 0;
12410 }
12411 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12412 {
12413 /* Consume the `='. */
12414 cp_lexer_consume_token (parser->lexer);
12415 initializer = cp_parser_initializer_clause (parser,
12416 &non_constant_p);
12417 }
12418 else
12419 {
12420 cp_parser_error (parser, "expected initializer");
12421 initializer = error_mark_node;
12422 }
12423 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
12424 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12425
12426 /* Process the initializer. */
12427 cp_finish_decl (decl,
12428 initializer, !non_constant_p,
12429 asm_specification,
12430 flags);
12431
12432 if (pushed_scope)
12433 pop_scope (pushed_scope);
12434
12435 return convert_from_reference (decl);
12436 }
12437 }
12438 /* If we didn't even get past the declarator successfully, we are
12439 definitely not looking at a declaration. */
12440 else
12441 cp_parser_abort_tentative_parse (parser);
12442
12443 /* Otherwise, we are looking at an expression. */
12444 return cp_parser_expression (parser);
12445 }
12446
12447 /* Parses a for-statement or range-for-statement until the closing ')',
12448 not included. */
12449
12450 static tree
12451 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
12452 {
12453 tree init, scope, decl;
12454 bool is_range_for;
12455
12456 /* Begin the for-statement. */
12457 scope = begin_for_scope (&init);
12458
12459 /* Parse the initialization. */
12460 is_range_for = cp_parser_init_statement (parser, &decl);
12461
12462 if (is_range_for)
12463 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
12464 false);
12465 else
12466 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
12467 }
12468
12469 static tree
12470 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
12471 unsigned short unroll)
12472 {
12473 /* Normal for loop */
12474 tree condition = NULL_TREE;
12475 tree expression = NULL_TREE;
12476 tree stmt;
12477
12478 stmt = begin_for_stmt (scope, init);
12479 /* The init-statement has already been parsed in
12480 cp_parser_init_statement, so no work is needed here. */
12481 finish_init_stmt (stmt);
12482
12483 /* If there's a condition, process it. */
12484 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12485 condition = cp_parser_condition (parser);
12486 else if (ivdep)
12487 {
12488 cp_parser_error (parser, "missing loop condition in loop with "
12489 "%<GCC ivdep%> pragma");
12490 condition = error_mark_node;
12491 }
12492 else if (unroll)
12493 {
12494 cp_parser_error (parser, "missing loop condition in loop with "
12495 "%<GCC unroll%> pragma");
12496 condition = error_mark_node;
12497 }
12498 finish_for_cond (condition, stmt, ivdep, unroll);
12499 /* Look for the `;'. */
12500 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12501
12502 /* If there's an expression, process it. */
12503 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
12504 expression = cp_parser_expression (parser);
12505 finish_for_expr (expression, stmt);
12506
12507 return stmt;
12508 }
12509
12510 /* Tries to parse a range-based for-statement:
12511
12512 range-based-for:
12513 decl-specifier-seq declarator : expression
12514
12515 The decl-specifier-seq declarator and the `:' are already parsed by
12516 cp_parser_init_statement. If processing_template_decl it returns a
12517 newly created RANGE_FOR_STMT; if not, it is converted to a
12518 regular FOR_STMT. */
12519
12520 static tree
12521 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
12522 bool ivdep, unsigned short unroll, bool is_omp)
12523 {
12524 tree stmt, range_expr;
12525 auto_vec <cxx_binding *, 16> bindings;
12526 auto_vec <tree, 16> names;
12527 tree decomp_first_name = NULL_TREE;
12528 unsigned int decomp_cnt = 0;
12529
12530 /* Get the range declaration momentarily out of the way so that
12531 the range expression doesn't clash with it. */
12532 if (range_decl != error_mark_node)
12533 {
12534 if (DECL_HAS_VALUE_EXPR_P (range_decl))
12535 {
12536 tree v = DECL_VALUE_EXPR (range_decl);
12537 /* For decomposition declaration get all of the corresponding
12538 declarations out of the way. */
12539 if (TREE_CODE (v) == ARRAY_REF
12540 && VAR_P (TREE_OPERAND (v, 0))
12541 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
12542 {
12543 tree d = range_decl;
12544 range_decl = TREE_OPERAND (v, 0);
12545 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
12546 decomp_first_name = d;
12547 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
12548 {
12549 tree name = DECL_NAME (d);
12550 names.safe_push (name);
12551 bindings.safe_push (IDENTIFIER_BINDING (name));
12552 IDENTIFIER_BINDING (name)
12553 = IDENTIFIER_BINDING (name)->previous;
12554 }
12555 }
12556 }
12557 if (names.is_empty ())
12558 {
12559 tree name = DECL_NAME (range_decl);
12560 names.safe_push (name);
12561 bindings.safe_push (IDENTIFIER_BINDING (name));
12562 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
12563 }
12564 }
12565
12566 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12567 {
12568 bool expr_non_constant_p;
12569 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12570 }
12571 else
12572 range_expr = cp_parser_expression (parser);
12573
12574 /* Put the range declaration(s) back into scope. */
12575 for (unsigned int i = 0; i < names.length (); i++)
12576 {
12577 cxx_binding *binding = bindings[i];
12578 binding->previous = IDENTIFIER_BINDING (names[i]);
12579 IDENTIFIER_BINDING (names[i]) = binding;
12580 }
12581
12582 /* finish_omp_for has its own code for the following, so just
12583 return the range_expr instead. */
12584 if (is_omp)
12585 return range_expr;
12586
12587 /* If in template, STMT is converted to a normal for-statement
12588 at instantiation. If not, it is done just ahead. */
12589 if (processing_template_decl)
12590 {
12591 if (check_for_bare_parameter_packs (range_expr))
12592 range_expr = error_mark_node;
12593 stmt = begin_range_for_stmt (scope, init);
12594 if (ivdep)
12595 RANGE_FOR_IVDEP (stmt) = 1;
12596 if (unroll)
12597 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12598 finish_range_for_decl (stmt, range_decl, range_expr);
12599 if (!type_dependent_expression_p (range_expr)
12600 /* do_auto_deduction doesn't mess with template init-lists. */
12601 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12602 do_range_for_auto_deduction (range_decl, range_expr);
12603 }
12604 else
12605 {
12606 stmt = begin_for_stmt (scope, init);
12607 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12608 decomp_first_name, decomp_cnt, ivdep,
12609 unroll);
12610 }
12611 return stmt;
12612 }
12613
12614 /* Subroutine of cp_convert_range_for: given the initializer expression,
12615 builds up the range temporary. */
12616
12617 static tree
12618 build_range_temp (tree range_expr)
12619 {
12620 tree range_type, range_temp;
12621
12622 /* Find out the type deduced by the declaration
12623 `auto &&__range = range_expr'. */
12624 range_type = cp_build_reference_type (make_auto (), true);
12625 range_type = do_auto_deduction (range_type, range_expr,
12626 type_uses_auto (range_type));
12627
12628 /* Create the __range variable. */
12629 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12630 range_type);
12631 TREE_USED (range_temp) = 1;
12632 DECL_ARTIFICIAL (range_temp) = 1;
12633
12634 return range_temp;
12635 }
12636
12637 /* Used by cp_parser_range_for in template context: we aren't going to
12638 do a full conversion yet, but we still need to resolve auto in the
12639 type of the for-range-declaration if present. This is basically
12640 a shortcut version of cp_convert_range_for. */
12641
12642 static void
12643 do_range_for_auto_deduction (tree decl, tree range_expr)
12644 {
12645 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12646 if (auto_node)
12647 {
12648 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12649 range_temp = convert_from_reference (build_range_temp (range_expr));
12650 iter_type = (cp_parser_perform_range_for_lookup
12651 (range_temp, &begin_dummy, &end_dummy));
12652 if (iter_type)
12653 {
12654 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12655 iter_type);
12656 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12657 RO_UNARY_STAR,
12658 tf_warning_or_error);
12659 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12660 iter_decl, auto_node);
12661 }
12662 }
12663 }
12664
12665 /* Warns when the loop variable should be changed to a reference type to
12666 avoid unnecessary copying. I.e., from
12667
12668 for (const auto x : range)
12669
12670 where range returns a reference, to
12671
12672 for (const auto &x : range)
12673
12674 if this version doesn't make a copy. DECL is the RANGE_DECL; EXPR is the
12675 *__for_begin expression.
12676 This function is never called when processing_template_decl is on. */
12677
12678 static void
12679 warn_for_range_copy (tree decl, tree expr)
12680 {
12681 if (!warn_range_loop_construct
12682 || decl == error_mark_node)
12683 return;
12684
12685 location_t loc = DECL_SOURCE_LOCATION (decl);
12686 tree type = TREE_TYPE (decl);
12687
12688 if (from_macro_expansion_at (loc))
12689 return;
12690
12691 if (TYPE_REF_P (type))
12692 {
12693 /* TODO: Implement reference warnings. */
12694 return;
12695 }
12696 else if (!CP_TYPE_CONST_P (type))
12697 return;
12698
12699 /* Since small trivially copyable types are cheap to copy, we suppress the
12700 warning for them. 64B is a common size of a cache line. */
12701 if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
12702 || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64
12703 && trivially_copyable_p (type)))
12704 return;
12705
12706 tree rtype = cp_build_reference_type (type, /*rval*/false);
12707 /* If we could initialize the reference directly, it wouldn't involve any
12708 copies. */
12709 if (!ref_conv_binds_directly_p (rtype, expr))
12710 return;
12711
12712 auto_diagnostic_group d;
12713 if (warning_at (loc, OPT_Wrange_loop_construct,
12714 "loop variable %qD creates a copy from type %qT",
12715 decl, type))
12716 {
12717 gcc_rich_location richloc (loc);
12718 richloc.add_fixit_insert_before ("&");
12719 inform (&richloc, "use reference type to prevent copying");
12720 }
12721 }
12722
12723 /* Converts a range-based for-statement into a normal
12724 for-statement, as per the definition.
12725
12726 for (RANGE_DECL : RANGE_EXPR)
12727 BLOCK
12728
12729 should be equivalent to:
12730
12731 {
12732 auto &&__range = RANGE_EXPR;
12733 for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
12734 __begin != __end;
12735 ++__begin)
12736 {
12737 RANGE_DECL = *__begin;
12738 BLOCK
12739 }
12740 }
12741
12742 If RANGE_EXPR is an array:
12743 BEGIN_EXPR = __range
12744 END_EXPR = __range + ARRAY_SIZE(__range)
12745 Else if RANGE_EXPR has a member 'begin' or 'end':
12746 BEGIN_EXPR = __range.begin()
12747 END_EXPR = __range.end()
12748 Else:
12749 BEGIN_EXPR = begin(__range)
12750 END_EXPR = end(__range);
12751
12752 If __range has a member 'begin' but not 'end', or vice versa, we must
12753 still use the second alternative (it will surely fail, however).
12754 When calling begin()/end() in the third alternative we must use
12755 argument dependent lookup, but always considering 'std' as an associated
12756 namespace. */
12757
12758 tree
12759 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12760 tree decomp_first_name, unsigned int decomp_cnt,
12761 bool ivdep, unsigned short unroll)
12762 {
12763 tree begin, end;
12764 tree iter_type, begin_expr, end_expr;
12765 tree condition, expression;
12766
12767 range_expr = mark_lvalue_use (range_expr);
12768
12769 if (range_decl == error_mark_node || range_expr == error_mark_node)
12770 /* If an error happened previously do nothing or else a lot of
12771 unhelpful errors would be issued. */
12772 begin_expr = end_expr = iter_type = error_mark_node;
12773 else
12774 {
12775 tree range_temp;
12776
12777 if (VAR_P (range_expr)
12778 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12779 /* Can't bind a reference to an array of runtime bound. */
12780 range_temp = range_expr;
12781 else
12782 {
12783 range_temp = build_range_temp (range_expr);
12784 pushdecl (range_temp);
12785 cp_finish_decl (range_temp, range_expr,
12786 /*is_constant_init*/false, NULL_TREE,
12787 LOOKUP_ONLYCONVERTING);
12788 range_temp = convert_from_reference (range_temp);
12789 }
12790 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12791 &begin_expr, &end_expr);
12792 }
12793
12794 /* The new for initialization statement. */
12795 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12796 iter_type);
12797 TREE_USED (begin) = 1;
12798 DECL_ARTIFICIAL (begin) = 1;
12799 pushdecl (begin);
12800 cp_finish_decl (begin, begin_expr,
12801 /*is_constant_init*/false, NULL_TREE,
12802 LOOKUP_ONLYCONVERTING);
12803
12804 if (cxx_dialect >= cxx17)
12805 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12806 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12807 TREE_USED (end) = 1;
12808 DECL_ARTIFICIAL (end) = 1;
12809 pushdecl (end);
12810 cp_finish_decl (end, end_expr,
12811 /*is_constant_init*/false, NULL_TREE,
12812 LOOKUP_ONLYCONVERTING);
12813
12814 finish_init_stmt (statement);
12815
12816 /* The new for condition. */
12817 condition = build_x_binary_op (input_location, NE_EXPR,
12818 begin, ERROR_MARK,
12819 end, ERROR_MARK,
12820 NULL, tf_warning_or_error);
12821 finish_for_cond (condition, statement, ivdep, unroll);
12822
12823 /* The new increment expression. */
12824 expression = finish_unary_op_expr (input_location,
12825 PREINCREMENT_EXPR, begin,
12826 tf_warning_or_error);
12827 finish_for_expr (expression, statement);
12828
12829 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12830 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12831
12832 /* The declaration is initialized with *__begin inside the loop body. */
12833 tree deref_begin = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12834 tf_warning_or_error);
12835 cp_finish_decl (range_decl, deref_begin,
12836 /*is_constant_init*/false, NULL_TREE,
12837 LOOKUP_ONLYCONVERTING);
12838 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12839 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12840
12841 warn_for_range_copy (range_decl, deref_begin);
12842
12843 return statement;
12844 }
12845
12846 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12847 We need to solve both at the same time because the method used
12848 depends on the existence of members begin or end.
12849 Returns the type deduced for the iterator expression. */
12850
12851 static tree
12852 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12853 {
12854 if (error_operand_p (range))
12855 {
12856 *begin = *end = error_mark_node;
12857 return error_mark_node;
12858 }
12859
12860 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12861 {
12862 error ("range-based %<for%> expression of type %qT "
12863 "has incomplete type", TREE_TYPE (range));
12864 *begin = *end = error_mark_node;
12865 return error_mark_node;
12866 }
12867 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12868 {
12869 /* If RANGE is an array, we will use pointer arithmetic. */
12870 *begin = decay_conversion (range, tf_warning_or_error);
12871 *end = build_binary_op (input_location, PLUS_EXPR,
12872 range,
12873 array_type_nelts_top (TREE_TYPE (range)),
12874 false);
12875 return TREE_TYPE (*begin);
12876 }
12877 else
12878 {
12879 /* If it is not an array, we must do a bit of magic. */
12880 tree id_begin, id_end;
12881 tree member_begin, member_end;
12882
12883 *begin = *end = error_mark_node;
12884
12885 id_begin = get_identifier ("begin");
12886 id_end = get_identifier ("end");
12887 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12888 /*protect=*/2, /*want_type=*/false,
12889 tf_warning_or_error);
12890 member_end = lookup_member (TREE_TYPE (range), id_end,
12891 /*protect=*/2, /*want_type=*/false,
12892 tf_warning_or_error);
12893
12894 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12895 {
12896 /* Use the member functions. */
12897 *begin = cp_parser_range_for_member_function (range, id_begin);
12898 *end = cp_parser_range_for_member_function (range, id_end);
12899 }
12900 else
12901 {
12902 /* Use global functions with ADL. */
12903 releasing_vec vec;
12904
12905 vec_safe_push (vec, range);
12906
12907 member_begin = perform_koenig_lookup (id_begin, vec,
12908 tf_warning_or_error);
12909 *begin = finish_call_expr (member_begin, &vec, false, true,
12910 tf_warning_or_error);
12911 member_end = perform_koenig_lookup (id_end, vec,
12912 tf_warning_or_error);
12913 *end = finish_call_expr (member_end, &vec, false, true,
12914 tf_warning_or_error);
12915 }
12916
12917 /* Last common checks. */
12918 if (*begin == error_mark_node || *end == error_mark_node)
12919 {
12920 /* If one of the expressions is an error do no more checks. */
12921 *begin = *end = error_mark_node;
12922 return error_mark_node;
12923 }
12924 else if (type_dependent_expression_p (*begin)
12925 || type_dependent_expression_p (*end))
12926 /* Can happen, when, eg, in a template context, Koenig lookup
12927 can't resolve begin/end (c++/58503). */
12928 return NULL_TREE;
12929 else
12930 {
12931 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12932 /* The unqualified type of the __begin and __end temporaries should
12933 be the same, as required by the multiple auto declaration. */
12934 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12935 {
12936 if (cxx_dialect >= cxx17
12937 && (build_x_binary_op (input_location, NE_EXPR,
12938 *begin, ERROR_MARK,
12939 *end, ERROR_MARK,
12940 NULL, tf_none)
12941 != error_mark_node))
12942 /* P0184R0 allows __begin and __end to have different types,
12943 but make sure they are comparable so we can give a better
12944 diagnostic. */;
12945 else
12946 error ("inconsistent begin/end types in range-based %<for%> "
12947 "statement: %qT and %qT",
12948 TREE_TYPE (*begin), TREE_TYPE (*end));
12949 }
12950 return iter_type;
12951 }
12952 }
12953 }
12954
12955 /* Helper function for cp_parser_perform_range_for_lookup.
12956 Builds a tree for RANGE.IDENTIFIER(). */
12957
12958 static tree
12959 cp_parser_range_for_member_function (tree range, tree identifier)
12960 {
12961 tree member, res;
12962
12963 member = finish_class_member_access_expr (range, identifier,
12964 false, tf_warning_or_error);
12965 if (member == error_mark_node)
12966 return error_mark_node;
12967
12968 releasing_vec vec;
12969 res = finish_call_expr (member, &vec,
12970 /*disallow_virtual=*/false,
12971 /*koenig_p=*/false,
12972 tf_warning_or_error);
12973 return res;
12974 }
12975
12976 /* Parse an iteration-statement.
12977
12978 iteration-statement:
12979 while ( condition ) statement
12980 do statement while ( expression ) ;
12981 for ( init-statement condition [opt] ; expression [opt] )
12982 statement
12983
12984 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12985
12986 static tree
12987 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12988 unsigned short unroll)
12989 {
12990 cp_token *token;
12991 enum rid keyword;
12992 tree statement;
12993 unsigned char in_statement;
12994 token_indent_info guard_tinfo;
12995
12996 /* Peek at the next token. */
12997 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12998 if (!token)
12999 return error_mark_node;
13000
13001 guard_tinfo = get_token_indent_info (token);
13002
13003 /* Remember whether or not we are already within an iteration
13004 statement. */
13005 in_statement = parser->in_statement;
13006
13007 /* See what kind of keyword it is. */
13008 keyword = token->keyword;
13009 switch (keyword)
13010 {
13011 case RID_WHILE:
13012 {
13013 tree condition;
13014
13015 /* Begin the while-statement. */
13016 statement = begin_while_stmt ();
13017 /* Look for the `('. */
13018 matching_parens parens;
13019 parens.require_open (parser);
13020 /* Parse the condition. */
13021 condition = cp_parser_condition (parser);
13022 finish_while_stmt_cond (condition, statement, ivdep, unroll);
13023 /* Look for the `)'. */
13024 parens.require_close (parser);
13025 /* Parse the dependent statement. */
13026 parser->in_statement = IN_ITERATION_STMT;
13027 bool prev = note_iteration_stmt_body_start ();
13028 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
13029 note_iteration_stmt_body_end (prev);
13030 parser->in_statement = in_statement;
13031 /* We're done with the while-statement. */
13032 finish_while_stmt (statement);
13033 }
13034 break;
13035
13036 case RID_DO:
13037 {
13038 tree expression;
13039
13040 /* Begin the do-statement. */
13041 statement = begin_do_stmt ();
13042 /* Parse the body of the do-statement. */
13043 parser->in_statement = IN_ITERATION_STMT;
13044 bool prev = note_iteration_stmt_body_start ();
13045 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
13046 note_iteration_stmt_body_end (prev);
13047 parser->in_statement = in_statement;
13048 finish_do_body (statement);
13049 /* Look for the `while' keyword. */
13050 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
13051 /* Look for the `('. */
13052 matching_parens parens;
13053 parens.require_open (parser);
13054 /* Parse the expression. */
13055 expression = cp_parser_expression (parser);
13056 /* We're done with the do-statement. */
13057 finish_do_stmt (expression, statement, ivdep, unroll);
13058 /* Look for the `)'. */
13059 parens.require_close (parser);
13060 /* Look for the `;'. */
13061 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13062 }
13063 break;
13064
13065 case RID_FOR:
13066 {
13067 /* Look for the `('. */
13068 matching_parens parens;
13069 parens.require_open (parser);
13070
13071 statement = cp_parser_for (parser, ivdep, unroll);
13072
13073 /* Look for the `)'. */
13074 parens.require_close (parser);
13075
13076 /* Parse the body of the for-statement. */
13077 parser->in_statement = IN_ITERATION_STMT;
13078 bool prev = note_iteration_stmt_body_start ();
13079 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
13080 note_iteration_stmt_body_end (prev);
13081 parser->in_statement = in_statement;
13082
13083 /* We're done with the for-statement. */
13084 finish_for_stmt (statement);
13085 }
13086 break;
13087
13088 default:
13089 cp_parser_error (parser, "expected iteration-statement");
13090 statement = error_mark_node;
13091 break;
13092 }
13093
13094 return statement;
13095 }
13096
13097 /* Parse a init-statement or the declarator of a range-based-for.
13098 Returns true if a range-based-for declaration is seen.
13099
13100 init-statement:
13101 expression-statement
13102 simple-declaration */
13103
13104 static bool
13105 cp_parser_init_statement (cp_parser *parser, tree *decl)
13106 {
13107 /* If the next token is a `;', then we have an empty
13108 expression-statement. Grammatically, this is also a
13109 simple-declaration, but an invalid one, because it does not
13110 declare anything. Therefore, if we did not handle this case
13111 specially, we would issue an error message about an invalid
13112 declaration. */
13113 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13114 {
13115 bool is_range_for = false;
13116 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13117
13118 /* Try to parse the init-statement. */
13119 if (cp_parser_range_based_for_with_init_p (parser))
13120 {
13121 tree dummy;
13122 cp_parser_parse_tentatively (parser);
13123 /* Parse the declaration. */
13124 cp_parser_simple_declaration (parser,
13125 /*function_definition_allowed_p=*/false,
13126 &dummy);
13127 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13128 if (!cp_parser_parse_definitely (parser))
13129 /* That didn't work, try to parse it as an expression-statement. */
13130 cp_parser_expression_statement (parser, NULL_TREE);
13131
13132 if (cxx_dialect < cxx20)
13133 {
13134 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
13135 "range-based %<for%> loops with initializer only "
13136 "available with %<-std=c++20%> or %<-std=gnu++20%>");
13137 *decl = error_mark_node;
13138 }
13139 }
13140
13141 /* A colon is used in range-based for. */
13142 parser->colon_corrects_to_scope_p = false;
13143
13144 /* We're going to speculatively look for a declaration, falling back
13145 to an expression, if necessary. */
13146 cp_parser_parse_tentatively (parser);
13147 /* Parse the declaration. */
13148 cp_parser_simple_declaration (parser,
13149 /*function_definition_allowed_p=*/false,
13150 decl);
13151 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13152 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13153 {
13154 /* It is a range-for, consume the ':'. */
13155 cp_lexer_consume_token (parser->lexer);
13156 is_range_for = true;
13157 if (cxx_dialect < cxx11)
13158 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
13159 "range-based %<for%> loops only available with "
13160 "%<-std=c++11%> or %<-std=gnu++11%>");
13161 }
13162 else
13163 /* The ';' is not consumed yet because we told
13164 cp_parser_simple_declaration not to. */
13165 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13166
13167 if (cp_parser_parse_definitely (parser))
13168 return is_range_for;
13169 /* If the tentative parse failed, then we shall need to look for an
13170 expression-statement. */
13171 }
13172 /* If we are here, it is an expression-statement. */
13173 cp_parser_expression_statement (parser, NULL_TREE);
13174 return false;
13175 }
13176
13177 /* Parse a jump-statement.
13178
13179 jump-statement:
13180 break ;
13181 continue ;
13182 return expression [opt] ;
13183 return braced-init-list ;
13184 coroutine-return-statement;
13185 goto identifier ;
13186
13187 GNU extension:
13188
13189 jump-statement:
13190 goto * expression ;
13191
13192 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
13193
13194 static tree
13195 cp_parser_jump_statement (cp_parser* parser)
13196 {
13197 tree statement = error_mark_node;
13198 cp_token *token;
13199 enum rid keyword;
13200 unsigned char in_statement;
13201
13202 /* Peek at the next token. */
13203 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
13204 if (!token)
13205 return error_mark_node;
13206
13207 /* See what kind of keyword it is. */
13208 keyword = token->keyword;
13209 switch (keyword)
13210 {
13211 case RID_BREAK:
13212 in_statement = parser->in_statement & ~IN_IF_STMT;
13213 switch (in_statement)
13214 {
13215 case 0:
13216 error_at (token->location, "break statement not within loop or switch");
13217 break;
13218 default:
13219 gcc_assert ((in_statement & IN_SWITCH_STMT)
13220 || in_statement == IN_ITERATION_STMT);
13221 statement = finish_break_stmt ();
13222 if (in_statement == IN_ITERATION_STMT)
13223 break_maybe_infinite_loop ();
13224 break;
13225 case IN_OMP_BLOCK:
13226 error_at (token->location, "invalid exit from OpenMP structured block");
13227 break;
13228 case IN_OMP_FOR:
13229 error_at (token->location, "break statement used with OpenMP for loop");
13230 break;
13231 }
13232 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13233 break;
13234
13235 case RID_CONTINUE:
13236 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
13237 {
13238 case 0:
13239 error_at (token->location, "continue statement not within a loop");
13240 break;
13241 /* Fall through. */
13242 case IN_ITERATION_STMT:
13243 case IN_OMP_FOR:
13244 statement = finish_continue_stmt ();
13245 break;
13246 case IN_OMP_BLOCK:
13247 error_at (token->location, "invalid exit from OpenMP structured block");
13248 break;
13249 default:
13250 gcc_unreachable ();
13251 }
13252 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13253 break;
13254
13255 case RID_CO_RETURN:
13256 case RID_RETURN:
13257 {
13258 tree expr;
13259 bool expr_non_constant_p;
13260
13261 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13262 {
13263 cp_lexer_set_source_position (parser->lexer);
13264 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13265 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
13266 }
13267 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13268 expr = cp_parser_expression (parser);
13269 else
13270 /* If the next token is a `;', then there is no
13271 expression. */
13272 expr = NULL_TREE;
13273 /* Build the return-statement, check co-return first, since type
13274 deduction is not valid there. */
13275 if (keyword == RID_CO_RETURN)
13276 statement = finish_co_return_stmt (token->location, expr);
13277 else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
13278 /* Don't deduce from a discarded return statement. */;
13279 else
13280 statement = finish_return_stmt (expr);
13281 /* Look for the final `;'. */
13282 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13283 }
13284 break;
13285
13286 case RID_GOTO:
13287 if (parser->in_function_body
13288 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
13289 {
13290 error ("%<goto%> in %<constexpr%> function");
13291 cp_function_chain->invalid_constexpr = true;
13292 }
13293
13294 /* Create the goto-statement. */
13295 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
13296 {
13297 /* Issue a warning about this use of a GNU extension. */
13298 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
13299 /* Consume the '*' token. */
13300 cp_lexer_consume_token (parser->lexer);
13301 /* Parse the dependent expression. */
13302 finish_goto_stmt (cp_parser_expression (parser));
13303 }
13304 else
13305 finish_goto_stmt (cp_parser_identifier (parser));
13306 /* Look for the final `;'. */
13307 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13308 break;
13309
13310 default:
13311 cp_parser_error (parser, "expected jump-statement");
13312 break;
13313 }
13314
13315 return statement;
13316 }
13317
13318 /* Parse a declaration-statement.
13319
13320 declaration-statement:
13321 block-declaration */
13322
13323 static void
13324 cp_parser_declaration_statement (cp_parser* parser)
13325 {
13326 void *p;
13327
13328 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13329 p = obstack_alloc (&declarator_obstack, 0);
13330
13331 /* Parse the block-declaration. */
13332 cp_parser_block_declaration (parser, /*statement_p=*/true);
13333
13334 /* Free any declarators allocated. */
13335 obstack_free (&declarator_obstack, p);
13336 }
13337
13338 /* Some dependent statements (like `if (cond) statement'), are
13339 implicitly in their own scope. In other words, if the statement is
13340 a single statement (as opposed to a compound-statement), it is
13341 none-the-less treated as if it were enclosed in braces. Any
13342 declarations appearing in the dependent statement are out of scope
13343 after control passes that point. This function parses a statement,
13344 but ensures that is in its own scope, even if it is not a
13345 compound-statement.
13346
13347 If IF_P is not NULL, *IF_P is set to indicate whether the statement
13348 is a (possibly labeled) if statement which is not enclosed in
13349 braces and has an else clause. This is used to implement
13350 -Wparentheses.
13351
13352 CHAIN is a vector of if-else-if conditions. This is used to implement
13353 -Wduplicated-cond.
13354
13355 Returns the new statement. */
13356
13357 static tree
13358 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
13359 const token_indent_info &guard_tinfo,
13360 vec<tree> *chain)
13361 {
13362 tree statement;
13363 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
13364 location_t body_loc_after_labels = UNKNOWN_LOCATION;
13365 token_indent_info body_tinfo
13366 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13367
13368 if (if_p != NULL)
13369 *if_p = false;
13370
13371 /* Mark if () ; with a special NOP_EXPR. */
13372 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13373 {
13374 cp_lexer_consume_token (parser->lexer);
13375 statement = add_stmt (build_empty_stmt (body_loc));
13376
13377 if (guard_tinfo.keyword == RID_IF
13378 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
13379 warning_at (body_loc, OPT_Wempty_body,
13380 "suggest braces around empty body in an %<if%> statement");
13381 else if (guard_tinfo.keyword == RID_ELSE)
13382 warning_at (body_loc, OPT_Wempty_body,
13383 "suggest braces around empty body in an %<else%> statement");
13384 }
13385 /* if a compound is opened, we simply parse the statement directly. */
13386 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13387 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
13388 /* If the token is not a `{', then we must take special action. */
13389 else
13390 {
13391 /* Create a compound-statement. */
13392 statement = begin_compound_stmt (0);
13393 /* Parse the dependent-statement. */
13394 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
13395 &body_loc_after_labels);
13396 /* Finish the dummy compound-statement. */
13397 finish_compound_stmt (statement);
13398 }
13399
13400 token_indent_info next_tinfo
13401 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13402 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13403
13404 if (body_loc_after_labels != UNKNOWN_LOCATION
13405 && next_tinfo.type != CPP_SEMICOLON)
13406 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
13407 guard_tinfo.location, guard_tinfo.keyword);
13408
13409 /* Return the statement. */
13410 return statement;
13411 }
13412
13413 /* For some dependent statements (like `while (cond) statement'), we
13414 have already created a scope. Therefore, even if the dependent
13415 statement is a compound-statement, we do not want to create another
13416 scope. */
13417
13418 static void
13419 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
13420 const token_indent_info &guard_tinfo)
13421 {
13422 /* If the token is a `{', then we must take special action. */
13423 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13424 {
13425 token_indent_info body_tinfo
13426 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13427 location_t loc_after_labels = UNKNOWN_LOCATION;
13428
13429 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
13430 &loc_after_labels);
13431 token_indent_info next_tinfo
13432 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13433 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13434
13435 if (loc_after_labels != UNKNOWN_LOCATION
13436 && next_tinfo.type != CPP_SEMICOLON)
13437 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
13438 guard_tinfo.location,
13439 guard_tinfo.keyword);
13440 }
13441 else
13442 {
13443 /* Avoid calling cp_parser_compound_statement, so that we
13444 don't create a new scope. Do everything else by hand. */
13445 matching_braces braces;
13446 braces.require_open (parser);
13447 /* If the next keyword is `__label__' we have a label declaration. */
13448 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13449 cp_parser_label_declaration (parser);
13450 /* Parse an (optional) statement-seq. */
13451 cp_parser_statement_seq_opt (parser, NULL_TREE);
13452 braces.require_close (parser);
13453 }
13454 }
13455
13456 /* Declarations [gram.dcl.dcl] */
13457
13458 /* Parse an optional declaration-sequence.
13459
13460 declaration-seq:
13461 declaration
13462 declaration-seq declaration */
13463
13464 static void
13465 cp_parser_declaration_seq_opt (cp_parser* parser)
13466 {
13467 while (true)
13468 {
13469 cp_token *token = cp_lexer_peek_token (parser->lexer);
13470
13471 if (token->type == CPP_CLOSE_BRACE
13472 || token->type == CPP_EOF)
13473 break;
13474 else
13475 cp_parser_toplevel_declaration (parser);
13476 }
13477 }
13478
13479 /* Parse a declaration.
13480
13481 declaration:
13482 block-declaration
13483 function-definition
13484 template-declaration
13485 explicit-instantiation
13486 explicit-specialization
13487 linkage-specification
13488 namespace-definition
13489
13490 C++17:
13491 deduction-guide
13492
13493 GNU extension:
13494
13495 declaration:
13496 __extension__ declaration */
13497
13498 static void
13499 cp_parser_declaration (cp_parser* parser)
13500 {
13501 int saved_pedantic;
13502
13503 /* Check for the `__extension__' keyword. */
13504 if (cp_parser_extension_opt (parser, &saved_pedantic))
13505 {
13506 /* Parse the qualified declaration. */
13507 cp_parser_declaration (parser);
13508 /* Restore the PEDANTIC flag. */
13509 pedantic = saved_pedantic;
13510
13511 return;
13512 }
13513
13514 /* Try to figure out what kind of declaration is present. */
13515 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
13516 cp_token *token2 = (token1->type == CPP_EOF
13517 ? token1 : cp_lexer_peek_nth_token (parser->lexer, 2));
13518
13519 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13520 void *p = obstack_alloc (&declarator_obstack, 0);
13521
13522 tree attributes = NULL_TREE;
13523
13524 /* If the next token is `extern' and the following token is a string
13525 literal, then we have a linkage specification. */
13526 if (token1->keyword == RID_EXTERN
13527 && cp_parser_is_pure_string_literal (token2))
13528 cp_parser_linkage_specification (parser);
13529 /* If the next token is `template', then we have either a template
13530 declaration, an explicit instantiation, or an explicit
13531 specialization. */
13532 else if (token1->keyword == RID_TEMPLATE)
13533 {
13534 /* `template <>' indicates a template specialization. */
13535 if (token2->type == CPP_LESS
13536 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13537 cp_parser_explicit_specialization (parser);
13538 /* `template <' indicates a template declaration. */
13539 else if (token2->type == CPP_LESS)
13540 cp_parser_template_declaration (parser, /*member_p=*/false);
13541 /* Anything else must be an explicit instantiation. */
13542 else
13543 cp_parser_explicit_instantiation (parser);
13544 }
13545 /* If the next token is `export', then we have a template
13546 declaration. */
13547 else if (token1->keyword == RID_EXPORT)
13548 cp_parser_template_declaration (parser, /*member_p=*/false);
13549 /* If the next token is `extern', 'static' or 'inline' and the one
13550 after that is `template', we have a GNU extended explicit
13551 instantiation directive. */
13552 else if (cp_parser_allow_gnu_extensions_p (parser)
13553 && token2->keyword == RID_TEMPLATE
13554 && (token1->keyword == RID_EXTERN
13555 || token1->keyword == RID_STATIC
13556 || token1->keyword == RID_INLINE))
13557 cp_parser_explicit_instantiation (parser);
13558 /* If the next token is `namespace', check for a named or unnamed
13559 namespace definition. */
13560 else if (token1->keyword == RID_NAMESPACE
13561 && (/* A named namespace definition. */
13562 (token2->type == CPP_NAME
13563 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
13564 != CPP_EQ))
13565 || (token2->type == CPP_OPEN_SQUARE
13566 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
13567 == CPP_OPEN_SQUARE)
13568 /* An unnamed namespace definition. */
13569 || token2->type == CPP_OPEN_BRACE
13570 || token2->keyword == RID_ATTRIBUTE))
13571 cp_parser_namespace_definition (parser);
13572 /* An inline (associated) namespace definition. */
13573 else if (token2->keyword == RID_NAMESPACE
13574 && token1->keyword == RID_INLINE)
13575 cp_parser_namespace_definition (parser);
13576 /* Objective-C++ declaration/definition. */
13577 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1->keyword))
13578 cp_parser_objc_declaration (parser, NULL_TREE);
13579 else if (c_dialect_objc ()
13580 && token1->keyword == RID_ATTRIBUTE
13581 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
13582 cp_parser_objc_declaration (parser, attributes);
13583 /* At this point we may have a template declared by a concept
13584 introduction. */
13585 else if (flag_concepts
13586 && cp_parser_template_declaration_after_export (parser,
13587 /*member_p=*/false))
13588 /* We did. */;
13589 else
13590 /* Try to parse a block-declaration, or a function-definition. */
13591 cp_parser_block_declaration (parser, /*statement_p=*/false);
13592
13593 /* Free any declarators allocated. */
13594 obstack_free (&declarator_obstack, p);
13595 }
13596
13597 /* Parse a namespace-scope declaration. */
13598
13599 static void
13600 cp_parser_toplevel_declaration (cp_parser* parser)
13601 {
13602 cp_token *token = cp_lexer_peek_token (parser->lexer);
13603
13604 if (token->type == CPP_PRAGMA)
13605 /* A top-level declaration can consist solely of a #pragma. A
13606 nested declaration cannot, so this is done here and not in
13607 cp_parser_declaration. (A #pragma at block scope is
13608 handled in cp_parser_statement.) */
13609 cp_parser_pragma (parser, pragma_external, NULL);
13610 else if (token->type == CPP_SEMICOLON)
13611 {
13612 cp_lexer_consume_token (parser->lexer);
13613 /* A declaration consisting of a single semicolon is invalid
13614 * before C++11. Allow it unless we're being pedantic. */
13615 if (cxx_dialect < cxx11)
13616 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
13617 }
13618 else
13619 /* Parse the declaration itself. */
13620 cp_parser_declaration (parser);
13621 }
13622
13623 /* Parse a block-declaration.
13624
13625 block-declaration:
13626 simple-declaration
13627 asm-definition
13628 namespace-alias-definition
13629 using-declaration
13630 using-directive
13631
13632 GNU Extension:
13633
13634 block-declaration:
13635 __extension__ block-declaration
13636
13637 C++0x Extension:
13638
13639 block-declaration:
13640 static_assert-declaration
13641
13642 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13643 part of a declaration-statement. */
13644
13645 static void
13646 cp_parser_block_declaration (cp_parser *parser,
13647 bool statement_p)
13648 {
13649 int saved_pedantic;
13650
13651 /* Check for the `__extension__' keyword. */
13652 if (cp_parser_extension_opt (parser, &saved_pedantic))
13653 {
13654 /* Parse the qualified declaration. */
13655 cp_parser_block_declaration (parser, statement_p);
13656 /* Restore the PEDANTIC flag. */
13657 pedantic = saved_pedantic;
13658
13659 return;
13660 }
13661
13662 /* Peek at the next token to figure out which kind of declaration is
13663 present. */
13664 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
13665
13666 /* If the next keyword is `asm', we have an asm-definition. */
13667 if (token1->keyword == RID_ASM)
13668 {
13669 if (statement_p)
13670 cp_parser_commit_to_tentative_parse (parser);
13671 cp_parser_asm_definition (parser);
13672 }
13673 /* If the next keyword is `namespace', we have a
13674 namespace-alias-definition. */
13675 else if (token1->keyword == RID_NAMESPACE)
13676 cp_parser_namespace_alias_definition (parser);
13677 /* If the next keyword is `using', we have a
13678 using-declaration, a using-directive, or an alias-declaration. */
13679 else if (token1->keyword == RID_USING)
13680 {
13681 cp_token *token2;
13682
13683 if (statement_p)
13684 cp_parser_commit_to_tentative_parse (parser);
13685 /* If the token after `using' is `namespace', then we have a
13686 using-directive. */
13687 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13688 if (token2->keyword == RID_NAMESPACE)
13689 cp_parser_using_directive (parser);
13690 /* If the second token after 'using' is '=', then we have an
13691 alias-declaration. */
13692 else if (cxx_dialect >= cxx11
13693 && token2->type == CPP_NAME
13694 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13695 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13696 cp_parser_alias_declaration (parser);
13697 /* Otherwise, it's a using-declaration. */
13698 else
13699 cp_parser_using_declaration (parser,
13700 /*access_declaration_p=*/false);
13701 }
13702 /* If the next keyword is `__label__' we have a misplaced label
13703 declaration. */
13704 else if (token1->keyword == RID_LABEL)
13705 {
13706 cp_lexer_consume_token (parser->lexer);
13707 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13708 cp_parser_skip_to_end_of_statement (parser);
13709 /* If the next token is now a `;', consume it. */
13710 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13711 cp_lexer_consume_token (parser->lexer);
13712 }
13713 /* If the next token is `static_assert' we have a static assertion. */
13714 else if (token1->keyword == RID_STATIC_ASSERT)
13715 cp_parser_static_assert (parser, /*member_p=*/false);
13716 /* Anything else must be a simple-declaration. */
13717 else
13718 cp_parser_simple_declaration (parser, !statement_p,
13719 /*maybe_range_for_decl*/NULL);
13720 }
13721
13722 /* Parse a simple-declaration.
13723
13724 simple-declaration:
13725 decl-specifier-seq [opt] init-declarator-list [opt] ;
13726 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13727 brace-or-equal-initializer ;
13728
13729 init-declarator-list:
13730 init-declarator
13731 init-declarator-list , init-declarator
13732
13733 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13734 function-definition as a simple-declaration.
13735
13736 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13737 parsed declaration if it is an uninitialized single declarator not followed
13738 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13739 if present, will not be consumed. */
13740
13741 static void
13742 cp_parser_simple_declaration (cp_parser* parser,
13743 bool function_definition_allowed_p,
13744 tree *maybe_range_for_decl)
13745 {
13746 cp_decl_specifier_seq decl_specifiers;
13747 int declares_class_or_enum;
13748 bool saw_declarator;
13749 location_t comma_loc = UNKNOWN_LOCATION;
13750 location_t init_loc = UNKNOWN_LOCATION;
13751
13752 if (maybe_range_for_decl)
13753 *maybe_range_for_decl = NULL_TREE;
13754
13755 /* Defer access checks until we know what is being declared; the
13756 checks for names appearing in the decl-specifier-seq should be
13757 done as if we were in the scope of the thing being declared. */
13758 push_deferring_access_checks (dk_deferred);
13759
13760 /* Parse the decl-specifier-seq. We have to keep track of whether
13761 or not the decl-specifier-seq declares a named class or
13762 enumeration type, since that is the only case in which the
13763 init-declarator-list is allowed to be empty.
13764
13765 [dcl.dcl]
13766
13767 In a simple-declaration, the optional init-declarator-list can be
13768 omitted only when declaring a class or enumeration, that is when
13769 the decl-specifier-seq contains either a class-specifier, an
13770 elaborated-type-specifier, or an enum-specifier. */
13771 cp_parser_decl_specifier_seq (parser,
13772 CP_PARSER_FLAGS_OPTIONAL,
13773 &decl_specifiers,
13774 &declares_class_or_enum);
13775 /* We no longer need to defer access checks. */
13776 stop_deferring_access_checks ();
13777
13778 /* In a block scope, a valid declaration must always have a
13779 decl-specifier-seq. By not trying to parse declarators, we can
13780 resolve the declaration/expression ambiguity more quickly. */
13781 if (!function_definition_allowed_p
13782 && !decl_specifiers.any_specifiers_p)
13783 {
13784 cp_parser_error (parser, "expected declaration");
13785 goto done;
13786 }
13787
13788 /* If the next two tokens are both identifiers, the code is
13789 erroneous. The usual cause of this situation is code like:
13790
13791 T t;
13792
13793 where "T" should name a type -- but does not. */
13794 if (!decl_specifiers.any_type_specifiers_p
13795 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13796 {
13797 /* If parsing tentatively, we should commit; we really are
13798 looking at a declaration. */
13799 cp_parser_commit_to_tentative_parse (parser);
13800 /* Give up. */
13801 goto done;
13802 }
13803
13804 cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
13805
13806 /* Look for C++17 decomposition declaration. */
13807 for (size_t n = 1; ; n++)
13808 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13809 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13810 continue;
13811 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13812 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13813 && decl_specifiers.any_specifiers_p)
13814 {
13815 tree decl
13816 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13817 maybe_range_for_decl,
13818 &init_loc);
13819
13820 /* The next token should be either a `,' or a `;'. */
13821 cp_token *token = cp_lexer_peek_token (parser->lexer);
13822 /* If it's a `;', we are done. */
13823 if (token->type == CPP_SEMICOLON)
13824 goto finish;
13825 else if (maybe_range_for_decl)
13826 {
13827 if (*maybe_range_for_decl == NULL_TREE)
13828 *maybe_range_for_decl = error_mark_node;
13829 goto finish;
13830 }
13831 /* Anything else is an error. */
13832 else
13833 {
13834 /* If we have already issued an error message we don't need
13835 to issue another one. */
13836 if ((decl != error_mark_node
13837 && DECL_INITIAL (decl) != error_mark_node)
13838 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13839 cp_parser_error (parser, "expected %<;%>");
13840 /* Skip tokens until we reach the end of the statement. */
13841 cp_parser_skip_to_end_of_statement (parser);
13842 /* If the next token is now a `;', consume it. */
13843 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13844 cp_lexer_consume_token (parser->lexer);
13845 goto done;
13846 }
13847 }
13848 else
13849 break;
13850
13851 tree last_type;
13852 bool auto_specifier_p;
13853 /* NULL_TREE if both variable and function declaration are allowed,
13854 error_mark_node if function declaration are not allowed and
13855 a FUNCTION_DECL that should be diagnosed if it is followed by
13856 variable declarations. */
13857 tree auto_function_declaration;
13858
13859 last_type = NULL_TREE;
13860 auto_specifier_p
13861 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13862 auto_function_declaration = NULL_TREE;
13863
13864 /* Keep going until we hit the `;' at the end of the simple
13865 declaration. */
13866 saw_declarator = false;
13867 while (cp_lexer_next_token_is_not (parser->lexer,
13868 CPP_SEMICOLON))
13869 {
13870 cp_token *token;
13871 bool function_definition_p;
13872 tree decl;
13873 tree auto_result = NULL_TREE;
13874
13875 if (saw_declarator)
13876 {
13877 /* If we are processing next declarator, comma is expected */
13878 token = cp_lexer_peek_token (parser->lexer);
13879 gcc_assert (token->type == CPP_COMMA);
13880 cp_lexer_consume_token (parser->lexer);
13881 if (maybe_range_for_decl)
13882 {
13883 *maybe_range_for_decl = error_mark_node;
13884 if (comma_loc == UNKNOWN_LOCATION)
13885 comma_loc = token->location;
13886 }
13887 }
13888 else
13889 saw_declarator = true;
13890
13891 /* Parse the init-declarator. */
13892 decl = cp_parser_init_declarator (parser,
13893 CP_PARSER_FLAGS_NONE,
13894 &decl_specifiers,
13895 /*checks=*/NULL,
13896 function_definition_allowed_p,
13897 /*member_p=*/false,
13898 declares_class_or_enum,
13899 &function_definition_p,
13900 maybe_range_for_decl,
13901 &init_loc,
13902 &auto_result);
13903 /* If an error occurred while parsing tentatively, exit quickly.
13904 (That usually happens when in the body of a function; each
13905 statement is treated as a declaration-statement until proven
13906 otherwise.) */
13907 if (cp_parser_error_occurred (parser))
13908 goto done;
13909
13910 if (auto_specifier_p && cxx_dialect >= cxx14)
13911 {
13912 /* If the init-declarator-list contains more than one
13913 init-declarator, they shall all form declarations of
13914 variables. */
13915 if (auto_function_declaration == NULL_TREE)
13916 auto_function_declaration
13917 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13918 else if (TREE_CODE (decl) == FUNCTION_DECL
13919 || auto_function_declaration != error_mark_node)
13920 {
13921 error_at (decl_specifiers.locations[ds_type_spec],
13922 "non-variable %qD in declaration with more than one "
13923 "declarator with placeholder type",
13924 TREE_CODE (decl) == FUNCTION_DECL
13925 ? decl : auto_function_declaration);
13926 auto_function_declaration = error_mark_node;
13927 }
13928 }
13929
13930 if (auto_result
13931 && (!processing_template_decl || !type_uses_auto (auto_result)))
13932 {
13933 if (last_type
13934 && last_type != error_mark_node
13935 && !same_type_p (auto_result, last_type))
13936 {
13937 /* If the list of declarators contains more than one declarator,
13938 the type of each declared variable is determined as described
13939 above. If the type deduced for the template parameter U is not
13940 the same in each deduction, the program is ill-formed. */
13941 error_at (decl_specifiers.locations[ds_type_spec],
13942 "inconsistent deduction for %qT: %qT and then %qT",
13943 decl_specifiers.type, last_type, auto_result);
13944 last_type = error_mark_node;
13945 }
13946 else
13947 last_type = auto_result;
13948 }
13949
13950 /* Handle function definitions specially. */
13951 if (function_definition_p)
13952 {
13953 /* If the next token is a `,', then we are probably
13954 processing something like:
13955
13956 void f() {}, *p;
13957
13958 which is erroneous. */
13959 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13960 {
13961 cp_token *token = cp_lexer_peek_token (parser->lexer);
13962 error_at (token->location,
13963 "mixing"
13964 " declarations and function-definitions is forbidden");
13965 }
13966 /* Otherwise, we're done with the list of declarators. */
13967 else
13968 {
13969 pop_deferring_access_checks ();
13970 return;
13971 }
13972 }
13973 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13974 *maybe_range_for_decl = decl;
13975 /* The next token should be either a `,' or a `;'. */
13976 token = cp_lexer_peek_token (parser->lexer);
13977 /* If it's a `,', there are more declarators to come. */
13978 if (token->type == CPP_COMMA)
13979 /* will be consumed next time around */;
13980 /* If it's a `;', we are done. */
13981 else if (token->type == CPP_SEMICOLON)
13982 break;
13983 else if (maybe_range_for_decl)
13984 {
13985 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13986 permerror (decl_specifiers.locations[ds_type_spec],
13987 "types may not be defined in a for-range-declaration");
13988 break;
13989 }
13990 /* Anything else is an error. */
13991 else
13992 {
13993 /* If we have already issued an error message we don't need
13994 to issue another one. */
13995 if ((decl != error_mark_node
13996 && DECL_INITIAL (decl) != error_mark_node)
13997 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13998 cp_parser_error (parser, "expected %<,%> or %<;%>");
13999 /* Skip tokens until we reach the end of the statement. */
14000 cp_parser_skip_to_end_of_statement (parser);
14001 /* If the next token is now a `;', consume it. */
14002 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14003 cp_lexer_consume_token (parser->lexer);
14004 goto done;
14005 }
14006 /* After the first time around, a function-definition is not
14007 allowed -- even if it was OK at first. For example:
14008
14009 int i, f() {}
14010
14011 is not valid. */
14012 function_definition_allowed_p = false;
14013 }
14014
14015 /* Issue an error message if no declarators are present, and the
14016 decl-specifier-seq does not itself declare a class or
14017 enumeration: [dcl.dcl]/3. */
14018 if (!saw_declarator)
14019 {
14020 if (cp_parser_declares_only_class_p (parser))
14021 {
14022 if (!declares_class_or_enum
14023 && decl_specifiers.type
14024 && OVERLOAD_TYPE_P (decl_specifiers.type))
14025 /* Ensure an error is issued anyway when finish_decltype_type,
14026 called via cp_parser_decl_specifier_seq, returns a class or
14027 an enumeration (c++/51786). */
14028 decl_specifiers.type = NULL_TREE;
14029 shadow_tag (&decl_specifiers);
14030 }
14031 /* Perform any deferred access checks. */
14032 perform_deferred_access_checks (tf_warning_or_error);
14033 }
14034
14035 /* Consume the `;'. */
14036 finish:
14037 if (!maybe_range_for_decl)
14038 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14039 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14040 {
14041 if (init_loc != UNKNOWN_LOCATION)
14042 error_at (init_loc, "initializer in range-based %<for%> loop");
14043 if (comma_loc != UNKNOWN_LOCATION)
14044 error_at (comma_loc,
14045 "multiple declarations in range-based %<for%> loop");
14046 }
14047
14048 done:
14049 pop_deferring_access_checks ();
14050 }
14051
14052 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
14053 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
14054 initializer ; */
14055
14056 static tree
14057 cp_parser_decomposition_declaration (cp_parser *parser,
14058 cp_decl_specifier_seq *decl_specifiers,
14059 tree *maybe_range_for_decl,
14060 location_t *init_loc)
14061 {
14062 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
14063 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
14064 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
14065
14066 /* Parse the identifier-list. */
14067 auto_vec<cp_expr, 10> v;
14068 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
14069 while (true)
14070 {
14071 cp_expr e = cp_parser_identifier (parser);
14072 if (e.get_value () == error_mark_node)
14073 break;
14074 v.safe_push (e);
14075 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14076 break;
14077 cp_lexer_consume_token (parser->lexer);
14078 }
14079
14080 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
14081 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14082 {
14083 end_loc = UNKNOWN_LOCATION;
14084 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
14085 false);
14086 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
14087 cp_lexer_consume_token (parser->lexer);
14088 else
14089 {
14090 cp_parser_skip_to_end_of_statement (parser);
14091 return error_mark_node;
14092 }
14093 }
14094
14095 if (cxx_dialect < cxx17)
14096 pedwarn (loc, 0, "structured bindings only available with "
14097 "%<-std=c++17%> or %<-std=gnu++17%>");
14098
14099 tree pushed_scope;
14100 cp_declarator *declarator = make_declarator (cdk_decomp);
14101 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
14102 declarator->id_loc = loc;
14103 if (ref_qual != REF_QUAL_NONE)
14104 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
14105 ref_qual == REF_QUAL_RVALUE,
14106 NULL_TREE);
14107 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
14108 NULL_TREE, decl_specifiers->attributes,
14109 &pushed_scope);
14110 tree orig_decl = decl;
14111
14112 unsigned int i;
14113 cp_expr e;
14114 cp_decl_specifier_seq decl_specs;
14115 clear_decl_specs (&decl_specs);
14116 decl_specs.type = make_auto ();
14117 tree prev = decl;
14118 FOR_EACH_VEC_ELT (v, i, e)
14119 {
14120 if (i == 0)
14121 declarator = make_id_declarator (NULL_TREE, e.get_value (),
14122 sfk_none, e.get_location ());
14123 else
14124 {
14125 declarator->u.id.unqualified_name = e.get_value ();
14126 declarator->id_loc = e.get_location ();
14127 }
14128 tree elt_pushed_scope;
14129 tree decl2 = start_decl (declarator, &decl_specs, SD_DECOMPOSITION,
14130 NULL_TREE, NULL_TREE, &elt_pushed_scope);
14131 if (decl2 == error_mark_node)
14132 decl = error_mark_node;
14133 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
14134 {
14135 /* Ensure we've diagnosed redeclaration if we aren't creating
14136 a new VAR_DECL. */
14137 gcc_assert (errorcount);
14138 decl = error_mark_node;
14139 }
14140 else
14141 prev = decl2;
14142 if (elt_pushed_scope)
14143 pop_scope (elt_pushed_scope);
14144 }
14145
14146 if (v.is_empty ())
14147 {
14148 error_at (loc, "empty structured binding declaration");
14149 decl = error_mark_node;
14150 }
14151
14152 if (maybe_range_for_decl == NULL
14153 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14154 {
14155 bool non_constant_p = false, is_direct_init = false;
14156 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
14157 tree initializer = cp_parser_initializer (parser, &is_direct_init,
14158 &non_constant_p);
14159 if (initializer == NULL_TREE
14160 || (TREE_CODE (initializer) == TREE_LIST
14161 && TREE_CHAIN (initializer))
14162 || (is_direct_init
14163 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
14164 && CONSTRUCTOR_NELTS (initializer) != 1))
14165 {
14166 error_at (loc, "invalid initializer for structured binding "
14167 "declaration");
14168 initializer = error_mark_node;
14169 }
14170
14171 if (decl != error_mark_node)
14172 {
14173 cp_maybe_mangle_decomp (decl, prev, v.length ());
14174 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
14175 (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14176 cp_finish_decomp (decl, prev, v.length ());
14177 }
14178 }
14179 else if (decl != error_mark_node)
14180 {
14181 *maybe_range_for_decl = prev;
14182 /* Ensure DECL_VALUE_EXPR is created for all the decls but
14183 the underlying DECL. */
14184 cp_finish_decomp (decl, prev, v.length ());
14185 }
14186
14187 if (pushed_scope)
14188 pop_scope (pushed_scope);
14189
14190 if (decl == error_mark_node && DECL_P (orig_decl))
14191 {
14192 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
14193 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
14194 }
14195
14196 return decl;
14197 }
14198
14199 /* Parse a decl-specifier-seq.
14200
14201 decl-specifier-seq:
14202 decl-specifier-seq [opt] decl-specifier
14203 decl-specifier attribute-specifier-seq [opt] (C++11)
14204
14205 decl-specifier:
14206 storage-class-specifier
14207 type-specifier
14208 function-specifier
14209 friend
14210 typedef
14211
14212 GNU Extension:
14213
14214 decl-specifier:
14215 attributes
14216
14217 Concepts Extension:
14218
14219 decl-specifier:
14220 concept
14221
14222 Set *DECL_SPECS to a representation of the decl-specifier-seq.
14223
14224 The parser flags FLAGS is used to control type-specifier parsing.
14225
14226 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
14227 flags:
14228
14229 1: one of the decl-specifiers is an elaborated-type-specifier
14230 (i.e., a type declaration)
14231 2: one of the decl-specifiers is an enum-specifier or a
14232 class-specifier (i.e., a type definition)
14233
14234 */
14235
14236 static void
14237 cp_parser_decl_specifier_seq (cp_parser* parser,
14238 cp_parser_flags flags,
14239 cp_decl_specifier_seq *decl_specs,
14240 int* declares_class_or_enum)
14241 {
14242 bool constructor_possible_p = !parser->in_declarator_p;
14243 bool found_decl_spec = false;
14244 cp_token *start_token = NULL;
14245 cp_decl_spec ds;
14246
14247 /* Clear DECL_SPECS. */
14248 clear_decl_specs (decl_specs);
14249
14250 /* Assume no class or enumeration type is declared. */
14251 *declares_class_or_enum = 0;
14252
14253 /* Keep reading specifiers until there are no more to read. */
14254 while (true)
14255 {
14256 bool constructor_p;
14257 cp_token *token;
14258 ds = ds_last;
14259
14260 /* Peek at the next token. */
14261 token = cp_lexer_peek_token (parser->lexer);
14262
14263 /* Save the first token of the decl spec list for error
14264 reporting. */
14265 if (!start_token)
14266 start_token = token;
14267 /* Handle attributes. */
14268 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
14269 && cp_next_tokens_can_be_attribute_p (parser))
14270 {
14271 /* Parse the attributes. */
14272 tree attrs = cp_parser_attributes_opt (parser);
14273
14274 /* In a sequence of declaration specifiers, c++11 attributes
14275 appertain to the type that precede them. In that case
14276 [dcl.spec]/1 says:
14277
14278 The attribute-specifier-seq affects the type only for
14279 the declaration it appears in, not other declarations
14280 involving the same type.
14281
14282 But for now let's force the user to position the
14283 attribute either at the beginning of the declaration or
14284 after the declarator-id, which would clearly mean that it
14285 applies to the declarator. */
14286 if (cxx11_attribute_p (attrs))
14287 {
14288 if (!found_decl_spec)
14289 /* The c++11 attribute is at the beginning of the
14290 declaration. It appertains to the entity being
14291 declared. */;
14292 else
14293 {
14294 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
14295 {
14296 /* This is an attribute following a
14297 class-specifier. */
14298 if (decl_specs->type_definition_p)
14299 warn_misplaced_attr_for_class_type (token->location,
14300 decl_specs->type);
14301 attrs = NULL_TREE;
14302 }
14303 else
14304 {
14305 decl_specs->std_attributes
14306 = attr_chainon (decl_specs->std_attributes, attrs);
14307 if (decl_specs->locations[ds_std_attribute] == 0)
14308 decl_specs->locations[ds_std_attribute] = token->location;
14309 }
14310 continue;
14311 }
14312 }
14313
14314 decl_specs->attributes
14315 = attr_chainon (decl_specs->attributes, attrs);
14316 if (decl_specs->locations[ds_attribute] == 0)
14317 decl_specs->locations[ds_attribute] = token->location;
14318 continue;
14319 }
14320 /* Assume we will find a decl-specifier keyword. */
14321 found_decl_spec = true;
14322 /* If the next token is an appropriate keyword, we can simply
14323 add it to the list. */
14324 switch (token->keyword)
14325 {
14326 /* decl-specifier:
14327 friend
14328 constexpr
14329 constinit */
14330 case RID_FRIEND:
14331 if (!at_class_scope_p ())
14332 {
14333 gcc_rich_location richloc (token->location);
14334 richloc.add_fixit_remove ();
14335 error_at (&richloc, "%<friend%> used outside of class");
14336 cp_lexer_purge_token (parser->lexer);
14337 }
14338 else
14339 {
14340 ds = ds_friend;
14341 /* Consume the token. */
14342 cp_lexer_consume_token (parser->lexer);
14343 }
14344 break;
14345
14346 case RID_CONSTEXPR:
14347 ds = ds_constexpr;
14348 cp_lexer_consume_token (parser->lexer);
14349 break;
14350
14351 case RID_CONSTINIT:
14352 ds = ds_constinit;
14353 cp_lexer_consume_token (parser->lexer);
14354 break;
14355
14356 case RID_CONSTEVAL:
14357 ds = ds_consteval;
14358 cp_lexer_consume_token (parser->lexer);
14359 break;
14360
14361 case RID_CONCEPT:
14362 ds = ds_concept;
14363 cp_lexer_consume_token (parser->lexer);
14364
14365 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14366 break;
14367
14368 /* Warn for concept as a decl-specifier. We'll rewrite these as
14369 concept declarations later. */
14370 if (!flag_concepts_ts)
14371 {
14372 cp_token *next = cp_lexer_peek_token (parser->lexer);
14373 if (next->keyword == RID_BOOL)
14374 pedwarn (next->location, 0, "the %<bool%> keyword is not "
14375 "allowed in a C++20 concept definition");
14376 else
14377 pedwarn (token->location, 0, "C++20 concept definition syntax "
14378 "is %<concept <name> = <expr>%>");
14379 }
14380
14381 /* In C++20 a concept definition is just 'concept name = expr;'
14382 Support that syntax as a TS extension by pretending we've seen
14383 the 'bool' specifier. */
14384 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14385 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
14386 && !decl_specs->any_type_specifiers_p)
14387 {
14388 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
14389 token, /*type_definition*/false);
14390 decl_specs->any_type_specifiers_p = true;
14391 }
14392 break;
14393
14394 /* function-specifier:
14395 inline
14396 virtual
14397 explicit */
14398 case RID_INLINE:
14399 case RID_VIRTUAL:
14400 case RID_EXPLICIT:
14401 cp_parser_function_specifier_opt (parser, decl_specs);
14402 break;
14403
14404 /* decl-specifier:
14405 typedef */
14406 case RID_TYPEDEF:
14407 ds = ds_typedef;
14408 /* Consume the token. */
14409 cp_lexer_consume_token (parser->lexer);
14410
14411 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14412 break;
14413
14414 /* A constructor declarator cannot appear in a typedef. */
14415 constructor_possible_p = false;
14416 /* The "typedef" keyword can only occur in a declaration; we
14417 may as well commit at this point. */
14418 cp_parser_commit_to_tentative_parse (parser);
14419
14420 if (decl_specs->storage_class != sc_none)
14421 decl_specs->conflicting_specifiers_p = true;
14422 break;
14423
14424 /* storage-class-specifier:
14425 auto
14426 register
14427 static
14428 extern
14429 mutable
14430
14431 GNU Extension:
14432 thread */
14433 case RID_AUTO:
14434 if (cxx_dialect == cxx98)
14435 {
14436 /* Consume the token. */
14437 cp_lexer_consume_token (parser->lexer);
14438
14439 /* Complain about `auto' as a storage specifier, if
14440 we're complaining about C++0x compatibility. */
14441 gcc_rich_location richloc (token->location);
14442 richloc.add_fixit_remove ();
14443 warning_at (&richloc, OPT_Wc__11_compat,
14444 "%<auto%> changes meaning in C++11; "
14445 "please remove it");
14446
14447 /* Set the storage class anyway. */
14448 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
14449 token);
14450 }
14451 else
14452 /* C++0x auto type-specifier. */
14453 found_decl_spec = false;
14454 break;
14455
14456 case RID_REGISTER:
14457 case RID_STATIC:
14458 case RID_EXTERN:
14459 case RID_MUTABLE:
14460 /* Consume the token. */
14461 cp_lexer_consume_token (parser->lexer);
14462 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
14463 token);
14464 break;
14465 case RID_THREAD:
14466 /* Consume the token. */
14467 ds = ds_thread;
14468 cp_lexer_consume_token (parser->lexer);
14469 break;
14470
14471 default:
14472 /* We did not yet find a decl-specifier yet. */
14473 found_decl_spec = false;
14474 break;
14475 }
14476
14477 if (found_decl_spec
14478 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
14479 && token->keyword != RID_CONSTEXPR)
14480 error ("%<decl-specifier%> invalid in condition");
14481
14482 if (found_decl_spec
14483 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14484 && token->keyword != RID_MUTABLE
14485 && token->keyword != RID_CONSTEXPR
14486 && token->keyword != RID_CONSTEVAL)
14487 error_at (token->location, "%qD invalid in lambda",
14488 ridpointers[token->keyword]);
14489
14490 if (ds != ds_last)
14491 set_and_check_decl_spec_loc (decl_specs, ds, token);
14492
14493 /* Constructors are a special case. The `S' in `S()' is not a
14494 decl-specifier; it is the beginning of the declarator. */
14495 constructor_p
14496 = (!found_decl_spec
14497 && constructor_possible_p
14498 && (cp_parser_constructor_declarator_p
14499 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
14500 ds_friend))));
14501
14502 /* If we don't have a DECL_SPEC yet, then we must be looking at
14503 a type-specifier. */
14504 if (!found_decl_spec && !constructor_p)
14505 {
14506 int decl_spec_declares_class_or_enum;
14507 bool is_cv_qualifier;
14508 tree type_spec;
14509
14510 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14511 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14512
14513 type_spec
14514 = cp_parser_type_specifier (parser, flags,
14515 decl_specs,
14516 /*is_declaration=*/true,
14517 &decl_spec_declares_class_or_enum,
14518 &is_cv_qualifier);
14519 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
14520
14521 /* If this type-specifier referenced a user-defined type
14522 (a typedef, class-name, etc.), then we can't allow any
14523 more such type-specifiers henceforth.
14524
14525 [dcl.spec]
14526
14527 The longest sequence of decl-specifiers that could
14528 possibly be a type name is taken as the
14529 decl-specifier-seq of a declaration. The sequence shall
14530 be self-consistent as described below.
14531
14532 [dcl.type]
14533
14534 As a general rule, at most one type-specifier is allowed
14535 in the complete decl-specifier-seq of a declaration. The
14536 only exceptions are the following:
14537
14538 -- const or volatile can be combined with any other
14539 type-specifier.
14540
14541 -- signed or unsigned can be combined with char, long,
14542 short, or int.
14543
14544 -- ..
14545
14546 Example:
14547
14548 typedef char* Pc;
14549 void g (const int Pc);
14550
14551 Here, Pc is *not* part of the decl-specifier seq; it's
14552 the declarator. Therefore, once we see a type-specifier
14553 (other than a cv-qualifier), we forbid any additional
14554 user-defined types. We *do* still allow things like `int
14555 int' to be considered a decl-specifier-seq, and issue the
14556 error message later. */
14557 if (type_spec && !is_cv_qualifier)
14558 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14559 /* A constructor declarator cannot follow a type-specifier. */
14560 if (type_spec)
14561 {
14562 constructor_possible_p = false;
14563 found_decl_spec = true;
14564 if (!is_cv_qualifier)
14565 decl_specs->any_type_specifiers_p = true;
14566
14567 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
14568 error_at (token->location, "type-specifier invalid in lambda");
14569 }
14570 }
14571
14572 /* If we still do not have a DECL_SPEC, then there are no more
14573 decl-specifiers. */
14574 if (!found_decl_spec)
14575 break;
14576
14577 decl_specs->any_specifiers_p = true;
14578 /* After we see one decl-specifier, further decl-specifiers are
14579 always optional. */
14580 flags |= CP_PARSER_FLAGS_OPTIONAL;
14581 }
14582
14583 /* Don't allow a friend specifier with a class definition. */
14584 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
14585 && (*declares_class_or_enum & 2))
14586 error_at (decl_specs->locations[ds_friend],
14587 "class definition may not be declared a friend");
14588 }
14589
14590 /* Parse an (optional) storage-class-specifier.
14591
14592 storage-class-specifier:
14593 auto
14594 register
14595 static
14596 extern
14597 mutable
14598
14599 GNU Extension:
14600
14601 storage-class-specifier:
14602 thread
14603
14604 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14605
14606 static tree
14607 cp_parser_storage_class_specifier_opt (cp_parser* parser)
14608 {
14609 switch (cp_lexer_peek_token (parser->lexer)->keyword)
14610 {
14611 case RID_AUTO:
14612 if (cxx_dialect != cxx98)
14613 return NULL_TREE;
14614 /* Fall through for C++98. */
14615 gcc_fallthrough ();
14616
14617 case RID_REGISTER:
14618 case RID_STATIC:
14619 case RID_EXTERN:
14620 case RID_MUTABLE:
14621 case RID_THREAD:
14622 /* Consume the token. */
14623 return cp_lexer_consume_token (parser->lexer)->u.value;
14624
14625 default:
14626 return NULL_TREE;
14627 }
14628 }
14629
14630 /* Parse an (optional) function-specifier.
14631
14632 function-specifier:
14633 inline
14634 virtual
14635 explicit
14636
14637 C++20 Extension:
14638 explicit(constant-expression)
14639
14640 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14641 Updates DECL_SPECS, if it is non-NULL. */
14642
14643 static tree
14644 cp_parser_function_specifier_opt (cp_parser* parser,
14645 cp_decl_specifier_seq *decl_specs)
14646 {
14647 cp_token *token = cp_lexer_peek_token (parser->lexer);
14648 switch (token->keyword)
14649 {
14650 case RID_INLINE:
14651 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
14652 break;
14653
14654 case RID_VIRTUAL:
14655 /* 14.5.2.3 [temp.mem]
14656
14657 A member function template shall not be virtual. */
14658 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14659 && current_class_type)
14660 error_at (token->location, "templates may not be %<virtual%>");
14661 else
14662 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
14663 break;
14664
14665 case RID_EXPLICIT:
14666 {
14667 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
14668 /* If we see '(', it's C++20 explicit(bool). */
14669 tree expr;
14670 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14671 {
14672 matching_parens parens;
14673 parens.consume_open (parser);
14674
14675 /* New types are not allowed in an explicit-specifier. */
14676 const char *saved_message
14677 = parser->type_definition_forbidden_message;
14678 parser->type_definition_forbidden_message
14679 = G_("types may not be defined in explicit-specifier");
14680
14681 if (cxx_dialect < cxx20)
14682 pedwarn (token->location, 0,
14683 "%<explicit(bool)%> only available with %<-std=c++20%> "
14684 "or %<-std=gnu++20%>");
14685
14686 /* Parse the constant-expression. */
14687 expr = cp_parser_constant_expression (parser);
14688
14689 /* Restore the saved message. */
14690 parser->type_definition_forbidden_message = saved_message;
14691 parens.require_close (parser);
14692 }
14693 else
14694 /* The explicit-specifier explicit without a constant-expression is
14695 equivalent to the explicit-specifier explicit(true). */
14696 expr = boolean_true_node;
14697
14698 /* [dcl.fct.spec]
14699 "the constant-expression, if supplied, shall be a contextually
14700 converted constant expression of type bool." */
14701 expr = build_explicit_specifier (expr, tf_warning_or_error);
14702 /* We could evaluate it -- mark the decl as appropriate. */
14703 if (expr == boolean_true_node)
14704 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14705 else if (expr == boolean_false_node)
14706 /* Don't mark the decl as explicit. */;
14707 else if (decl_specs)
14708 /* The expression was value-dependent. Remember it so that we can
14709 substitute it later. */
14710 decl_specs->explicit_specifier = expr;
14711 return id;
14712 }
14713
14714 default:
14715 return NULL_TREE;
14716 }
14717
14718 /* Consume the token. */
14719 return cp_lexer_consume_token (parser->lexer)->u.value;
14720 }
14721
14722 /* Parse a linkage-specification.
14723
14724 linkage-specification:
14725 extern string-literal { declaration-seq [opt] }
14726 extern string-literal declaration */
14727
14728 static void
14729 cp_parser_linkage_specification (cp_parser* parser)
14730 {
14731 tree linkage;
14732
14733 /* Look for the `extern' keyword. */
14734 cp_token *extern_token
14735 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14736
14737 /* Look for the string-literal. */
14738 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14739 linkage = cp_parser_string_literal (parser, false, false);
14740
14741 /* Transform the literal into an identifier. If the literal is a
14742 wide-character string, or contains embedded NULs, then we can't
14743 handle it as the user wants. */
14744 if (strlen (TREE_STRING_POINTER (linkage))
14745 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14746 {
14747 cp_parser_error (parser, "invalid linkage-specification");
14748 /* Assume C++ linkage. */
14749 linkage = lang_name_cplusplus;
14750 }
14751 else
14752 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14753
14754 /* We're now using the new linkage. */
14755 push_lang_context (linkage);
14756
14757 /* Preserve the location of the innermost linkage specification,
14758 tracking the locations of nested specifications via a local. */
14759 location_t saved_location
14760 = parser->innermost_linkage_specification_location;
14761 /* Construct a location ranging from the start of the "extern" to
14762 the end of the string-literal, with the caret at the start, e.g.:
14763 extern "C" {
14764 ^~~~~~~~~~
14765 */
14766 parser->innermost_linkage_specification_location
14767 = make_location (extern_token->location,
14768 extern_token->location,
14769 get_finish (string_token->location));
14770
14771 /* If the next token is a `{', then we're using the first
14772 production. */
14773 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14774 {
14775 cp_ensure_no_omp_declare_simd (parser);
14776 cp_ensure_no_oacc_routine (parser);
14777
14778 /* Consume the `{' token. */
14779 matching_braces braces;
14780 braces.consume_open (parser);
14781 /* Parse the declarations. */
14782 cp_parser_declaration_seq_opt (parser);
14783 /* Look for the closing `}'. */
14784 braces.require_close (parser);
14785 }
14786 /* Otherwise, there's just one declaration. */
14787 else
14788 {
14789 bool saved_in_unbraced_linkage_specification_p;
14790
14791 saved_in_unbraced_linkage_specification_p
14792 = parser->in_unbraced_linkage_specification_p;
14793 parser->in_unbraced_linkage_specification_p = true;
14794 cp_parser_declaration (parser);
14795 parser->in_unbraced_linkage_specification_p
14796 = saved_in_unbraced_linkage_specification_p;
14797 }
14798
14799 /* We're done with the linkage-specification. */
14800 pop_lang_context ();
14801
14802 /* Restore location of parent linkage specification, if any. */
14803 parser->innermost_linkage_specification_location = saved_location;
14804 }
14805
14806 /* Parse a static_assert-declaration.
14807
14808 static_assert-declaration:
14809 static_assert ( constant-expression , string-literal ) ;
14810 static_assert ( constant-expression ) ; (C++17)
14811
14812 If MEMBER_P, this static_assert is a class member. */
14813
14814 static void
14815 cp_parser_static_assert(cp_parser *parser, bool member_p)
14816 {
14817 cp_expr condition;
14818 location_t token_loc;
14819 tree message;
14820 bool dummy;
14821
14822 /* Peek at the `static_assert' token so we can keep track of exactly
14823 where the static assertion started. */
14824 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14825
14826 /* Look for the `static_assert' keyword. */
14827 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14828 RT_STATIC_ASSERT))
14829 return;
14830
14831 /* We know we are in a static assertion; commit to any tentative
14832 parse. */
14833 if (cp_parser_parsing_tentatively (parser))
14834 cp_parser_commit_to_tentative_parse (parser);
14835
14836 /* Parse the `(' starting the static assertion condition. */
14837 matching_parens parens;
14838 parens.require_open (parser);
14839
14840 /* Parse the constant-expression. Allow a non-constant expression
14841 here in order to give better diagnostics in finish_static_assert. */
14842 condition =
14843 cp_parser_constant_expression (parser,
14844 /*allow_non_constant_p=*/true,
14845 /*non_constant_p=*/&dummy);
14846
14847 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14848 {
14849 if (cxx_dialect < cxx17)
14850 pedwarn (input_location, OPT_Wpedantic,
14851 "%<static_assert%> without a message "
14852 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
14853 /* Eat the ')' */
14854 cp_lexer_consume_token (parser->lexer);
14855 message = build_string (1, "");
14856 TREE_TYPE (message) = char_array_type_node;
14857 fix_string_type (message);
14858 }
14859 else
14860 {
14861 /* Parse the separating `,'. */
14862 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14863
14864 /* Parse the string-literal message. */
14865 message = cp_parser_string_literal (parser,
14866 /*translate=*/false,
14867 /*wide_ok=*/true);
14868
14869 /* A `)' completes the static assertion. */
14870 if (!parens.require_close (parser))
14871 cp_parser_skip_to_closing_parenthesis (parser,
14872 /*recovering=*/true,
14873 /*or_comma=*/false,
14874 /*consume_paren=*/true);
14875 }
14876
14877 /* A semicolon terminates the declaration. */
14878 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14879
14880 /* Get the location for the static assertion. Use that of the
14881 condition if available, otherwise, use that of the "static_assert"
14882 token. */
14883 location_t assert_loc = condition.get_location ();
14884 if (assert_loc == UNKNOWN_LOCATION)
14885 assert_loc = token_loc;
14886
14887 /* Complete the static assertion, which may mean either processing
14888 the static assert now or saving it for template instantiation. */
14889 finish_static_assert (condition, message, assert_loc, member_p,
14890 /*show_expr_p=*/false);
14891 }
14892
14893 /* Parse the expression in decltype ( expression ). */
14894
14895 static tree
14896 cp_parser_decltype_expr (cp_parser *parser,
14897 bool &id_expression_or_member_access_p)
14898 {
14899 cp_token *id_expr_start_token;
14900 tree expr;
14901
14902 /* First, try parsing an id-expression. */
14903 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14904 cp_parser_parse_tentatively (parser);
14905 expr = cp_parser_id_expression (parser,
14906 /*template_keyword_p=*/false,
14907 /*check_dependency_p=*/true,
14908 /*template_p=*/NULL,
14909 /*declarator_p=*/false,
14910 /*optional_p=*/false);
14911
14912 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14913 {
14914 bool non_integral_constant_expression_p = false;
14915 tree id_expression = expr;
14916 cp_id_kind idk;
14917 const char *error_msg;
14918
14919 if (identifier_p (expr))
14920 /* Lookup the name we got back from the id-expression. */
14921 expr = cp_parser_lookup_name_simple (parser, expr,
14922 id_expr_start_token->location);
14923
14924 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14925 /* A template without args is not a complete id-expression. */
14926 expr = error_mark_node;
14927
14928 if (expr
14929 && expr != error_mark_node
14930 && TREE_CODE (expr) != TYPE_DECL
14931 && (TREE_CODE (expr) != BIT_NOT_EXPR
14932 || !TYPE_P (TREE_OPERAND (expr, 0)))
14933 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14934 {
14935 /* Complete lookup of the id-expression. */
14936 expr = (finish_id_expression
14937 (id_expression, expr, parser->scope, &idk,
14938 /*integral_constant_expression_p=*/false,
14939 /*allow_non_integral_constant_expression_p=*/true,
14940 &non_integral_constant_expression_p,
14941 /*template_p=*/false,
14942 /*done=*/true,
14943 /*address_p=*/false,
14944 /*template_arg_p=*/false,
14945 &error_msg,
14946 id_expr_start_token->location));
14947
14948 if (expr == error_mark_node)
14949 /* We found an id-expression, but it was something that we
14950 should not have found. This is an error, not something
14951 we can recover from, so note that we found an
14952 id-expression and we'll recover as gracefully as
14953 possible. */
14954 id_expression_or_member_access_p = true;
14955 }
14956
14957 if (expr
14958 && expr != error_mark_node
14959 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14960 /* We have an id-expression. */
14961 id_expression_or_member_access_p = true;
14962 }
14963
14964 if (!id_expression_or_member_access_p)
14965 {
14966 /* Abort the id-expression parse. */
14967 cp_parser_abort_tentative_parse (parser);
14968
14969 /* Parsing tentatively, again. */
14970 cp_parser_parse_tentatively (parser);
14971
14972 /* Parse a class member access. */
14973 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14974 /*cast_p=*/false, /*decltype*/true,
14975 /*member_access_only_p=*/true, NULL);
14976
14977 if (expr
14978 && expr != error_mark_node
14979 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14980 /* We have an id-expression. */
14981 id_expression_or_member_access_p = true;
14982 }
14983
14984 if (id_expression_or_member_access_p)
14985 /* We have parsed the complete id-expression or member access. */
14986 cp_parser_parse_definitely (parser);
14987 else
14988 {
14989 /* Abort our attempt to parse an id-expression or member access
14990 expression. */
14991 cp_parser_abort_tentative_parse (parser);
14992
14993 /* Parse a full expression. */
14994 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14995 /*decltype_p=*/true);
14996 }
14997
14998 return expr;
14999 }
15000
15001 /* Parse a `decltype' type. Returns the type.
15002
15003 decltype-specifier:
15004 decltype ( expression )
15005 C++14:
15006 decltype ( auto ) */
15007
15008 static tree
15009 cp_parser_decltype (cp_parser *parser)
15010 {
15011 bool id_expression_or_member_access_p = false;
15012 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
15013
15014 if (start_token->type == CPP_DECLTYPE)
15015 {
15016 /* Already parsed. */
15017 cp_lexer_consume_token (parser->lexer);
15018 return saved_checks_value (start_token->u.tree_check_value);
15019 }
15020
15021 /* Look for the `decltype' token. */
15022 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
15023 return error_mark_node;
15024
15025 /* Parse the opening `('. */
15026 matching_parens parens;
15027 if (!parens.require_open (parser))
15028 return error_mark_node;
15029
15030 /* Since we're going to preserve any side-effects from this parse, set up a
15031 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15032 in the expression. */
15033 tentative_firewall firewall (parser);
15034
15035 /* If in_declarator_p, a reparse as an expression might succeed (60361).
15036 Otherwise, commit now for better diagnostics. */
15037 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
15038 && !parser->in_declarator_p)
15039 cp_parser_commit_to_topmost_tentative_parse (parser);
15040
15041 push_deferring_access_checks (dk_deferred);
15042
15043 tree expr = NULL_TREE;
15044
15045 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)
15046 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
15047 {
15048 /* decltype (auto) */
15049 cp_lexer_consume_token (parser->lexer);
15050 if (cxx_dialect < cxx14)
15051 {
15052 error_at (start_token->location,
15053 "%<decltype(auto)%> type specifier only available with "
15054 "%<-std=c++14%> or %<-std=gnu++14%>");
15055 expr = error_mark_node;
15056 }
15057 }
15058 else
15059 {
15060 /* decltype (expression) */
15061
15062 /* Types cannot be defined in a `decltype' expression. Save away the
15063 old message and set the new one. */
15064 const char *saved_message = parser->type_definition_forbidden_message;
15065 parser->type_definition_forbidden_message
15066 = G_("types may not be defined in %<decltype%> expressions");
15067
15068 /* The restrictions on constant-expressions do not apply inside
15069 decltype expressions. */
15070 bool saved_integral_constant_expression_p
15071 = parser->integral_constant_expression_p;
15072 bool saved_non_integral_constant_expression_p
15073 = parser->non_integral_constant_expression_p;
15074 parser->integral_constant_expression_p = false;
15075
15076 /* Within a parenthesized expression, a `>' token is always
15077 the greater-than operator. */
15078 bool saved_greater_than_is_operator_p
15079 = parser->greater_than_is_operator_p;
15080 parser->greater_than_is_operator_p = true;
15081
15082 /* Do not actually evaluate the expression. */
15083 ++cp_unevaluated_operand;
15084
15085 /* Do not warn about problems with the expression. */
15086 ++c_inhibit_evaluation_warnings;
15087
15088 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
15089 STRIP_ANY_LOCATION_WRAPPER (expr);
15090
15091 /* Go back to evaluating expressions. */
15092 --cp_unevaluated_operand;
15093 --c_inhibit_evaluation_warnings;
15094
15095 /* The `>' token might be the end of a template-id or
15096 template-parameter-list now. */
15097 parser->greater_than_is_operator_p
15098 = saved_greater_than_is_operator_p;
15099
15100 /* Restore the old message and the integral constant expression
15101 flags. */
15102 parser->type_definition_forbidden_message = saved_message;
15103 parser->integral_constant_expression_p
15104 = saved_integral_constant_expression_p;
15105 parser->non_integral_constant_expression_p
15106 = saved_non_integral_constant_expression_p;
15107 }
15108
15109 /* Parse to the closing `)'. */
15110 if (expr == error_mark_node || !parens.require_close (parser))
15111 {
15112 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15113 /*consume_paren=*/true);
15114 expr = error_mark_node;
15115 }
15116
15117 /* If we got a parse error while tentative, bail out now. */
15118 if (cp_parser_error_occurred (parser))
15119 {
15120 pop_deferring_access_checks ();
15121 return error_mark_node;
15122 }
15123
15124 if (!expr)
15125 /* Build auto. */
15126 expr = make_decltype_auto ();
15127 else
15128 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
15129 tf_warning_or_error);
15130
15131 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
15132 it again. */
15133 start_token->type = CPP_DECLTYPE;
15134 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15135 start_token->tree_check_p = true;
15136 start_token->u.tree_check_value->value = expr;
15137 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
15138 start_token->keyword = RID_MAX;
15139
15140 location_t loc = start_token->location;
15141 loc = make_location (loc, loc, parser->lexer);
15142 start_token->location = loc;
15143
15144 cp_lexer_purge_tokens_after (parser->lexer, start_token);
15145
15146 pop_to_parent_deferring_access_checks ();
15147
15148 return expr;
15149 }
15150
15151 /* Special member functions [gram.special] */
15152
15153 /* Parse a conversion-function-id.
15154
15155 conversion-function-id:
15156 operator conversion-type-id
15157
15158 Returns an IDENTIFIER_NODE representing the operator. */
15159
15160 static tree
15161 cp_parser_conversion_function_id (cp_parser* parser)
15162 {
15163 tree type;
15164 tree saved_scope;
15165 tree saved_qualifying_scope;
15166 tree saved_object_scope;
15167 tree pushed_scope = NULL_TREE;
15168
15169 /* Look for the `operator' token. */
15170 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15171 return error_mark_node;
15172 /* When we parse the conversion-type-id, the current scope will be
15173 reset. However, we need that information in able to look up the
15174 conversion function later, so we save it here. */
15175 saved_scope = parser->scope;
15176 saved_qualifying_scope = parser->qualifying_scope;
15177 saved_object_scope = parser->object_scope;
15178 /* We must enter the scope of the class so that the names of
15179 entities declared within the class are available in the
15180 conversion-type-id. For example, consider:
15181
15182 struct S {
15183 typedef int I;
15184 operator I();
15185 };
15186
15187 S::operator I() { ... }
15188
15189 In order to see that `I' is a type-name in the definition, we
15190 must be in the scope of `S'. */
15191 if (saved_scope)
15192 pushed_scope = push_scope (saved_scope);
15193 /* Parse the conversion-type-id. */
15194 type = cp_parser_conversion_type_id (parser);
15195 /* Leave the scope of the class, if any. */
15196 if (pushed_scope)
15197 pop_scope (pushed_scope);
15198 /* Restore the saved scope. */
15199 parser->scope = saved_scope;
15200 parser->qualifying_scope = saved_qualifying_scope;
15201 parser->object_scope = saved_object_scope;
15202 /* If the TYPE is invalid, indicate failure. */
15203 if (type == error_mark_node)
15204 return error_mark_node;
15205 return make_conv_op_name (type);
15206 }
15207
15208 /* Parse a conversion-type-id:
15209
15210 conversion-type-id:
15211 type-specifier-seq conversion-declarator [opt]
15212
15213 Returns the TYPE specified. */
15214
15215 static tree
15216 cp_parser_conversion_type_id (cp_parser* parser)
15217 {
15218 tree attributes;
15219 cp_decl_specifier_seq type_specifiers;
15220 cp_declarator *declarator;
15221 tree type_specified;
15222 const char *saved_message;
15223
15224 /* Parse the attributes. */
15225 attributes = cp_parser_attributes_opt (parser);
15226
15227 saved_message = parser->type_definition_forbidden_message;
15228 parser->type_definition_forbidden_message
15229 = G_("types may not be defined in a conversion-type-id");
15230
15231 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
15232 optional in conversion-type-id. */
15233 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15234 /*is_declaration=*/false,
15235 /*is_trailing_return=*/false,
15236 &type_specifiers);
15237
15238 parser->type_definition_forbidden_message = saved_message;
15239
15240 /* If that didn't work, stop. */
15241 if (type_specifiers.type == error_mark_node)
15242 return error_mark_node;
15243 /* Parse the conversion-declarator. */
15244 declarator = cp_parser_conversion_declarator_opt (parser);
15245
15246 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
15247 /*initialized=*/0, &attributes);
15248 if (attributes)
15249 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
15250
15251 /* Don't give this error when parsing tentatively. This happens to
15252 work because we always parse this definitively once. */
15253 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
15254 && type_uses_auto (type_specified))
15255 {
15256 if (cxx_dialect < cxx14)
15257 {
15258 error ("invalid use of %<auto%> in conversion operator");
15259 return error_mark_node;
15260 }
15261 else if (template_parm_scope_p ())
15262 warning (0, "use of %<auto%> in member template "
15263 "conversion operator can never be deduced");
15264 }
15265
15266 return type_specified;
15267 }
15268
15269 /* Parse an (optional) conversion-declarator.
15270
15271 conversion-declarator:
15272 ptr-operator conversion-declarator [opt]
15273
15274 */
15275
15276 static cp_declarator *
15277 cp_parser_conversion_declarator_opt (cp_parser* parser)
15278 {
15279 enum tree_code code;
15280 tree class_type, std_attributes = NULL_TREE;
15281 cp_cv_quals cv_quals;
15282
15283 /* We don't know if there's a ptr-operator next, or not. */
15284 cp_parser_parse_tentatively (parser);
15285 /* Try the ptr-operator. */
15286 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
15287 &std_attributes);
15288 /* If it worked, look for more conversion-declarators. */
15289 if (cp_parser_parse_definitely (parser))
15290 {
15291 cp_declarator *declarator;
15292
15293 /* Parse another optional declarator. */
15294 declarator = cp_parser_conversion_declarator_opt (parser);
15295
15296 declarator = cp_parser_make_indirect_declarator
15297 (code, class_type, cv_quals, declarator, std_attributes);
15298
15299 return declarator;
15300 }
15301
15302 return NULL;
15303 }
15304
15305 /* Parse an (optional) ctor-initializer.
15306
15307 ctor-initializer:
15308 : mem-initializer-list */
15309
15310 static void
15311 cp_parser_ctor_initializer_opt (cp_parser* parser)
15312 {
15313 /* If the next token is not a `:', then there is no
15314 ctor-initializer. */
15315 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
15316 {
15317 /* Do default initialization of any bases and members. */
15318 if (DECL_CONSTRUCTOR_P (current_function_decl))
15319 finish_mem_initializers (NULL_TREE);
15320 return;
15321 }
15322
15323 /* Consume the `:' token. */
15324 cp_lexer_consume_token (parser->lexer);
15325 /* And the mem-initializer-list. */
15326 cp_parser_mem_initializer_list (parser);
15327 }
15328
15329 /* Parse a mem-initializer-list.
15330
15331 mem-initializer-list:
15332 mem-initializer ... [opt]
15333 mem-initializer ... [opt] , mem-initializer-list */
15334
15335 static void
15336 cp_parser_mem_initializer_list (cp_parser* parser)
15337 {
15338 tree mem_initializer_list = NULL_TREE;
15339 tree target_ctor = error_mark_node;
15340 cp_token *token = cp_lexer_peek_token (parser->lexer);
15341
15342 /* Let the semantic analysis code know that we are starting the
15343 mem-initializer-list. */
15344 if (!DECL_CONSTRUCTOR_P (current_function_decl))
15345 error_at (token->location,
15346 "only constructors take member initializers");
15347
15348 /* Loop through the list. */
15349 while (true)
15350 {
15351 tree mem_initializer;
15352
15353 token = cp_lexer_peek_token (parser->lexer);
15354 /* Parse the mem-initializer. */
15355 mem_initializer = cp_parser_mem_initializer (parser);
15356 /* If the next token is a `...', we're expanding member initializers. */
15357 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15358 if (ellipsis
15359 || (mem_initializer != error_mark_node
15360 && check_for_bare_parameter_packs (TREE_PURPOSE
15361 (mem_initializer))))
15362 {
15363 /* Consume the `...'. */
15364 if (ellipsis)
15365 cp_lexer_consume_token (parser->lexer);
15366
15367 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
15368 can be expanded but members cannot. */
15369 if (mem_initializer != error_mark_node
15370 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
15371 {
15372 error_at (token->location,
15373 "cannot expand initializer for member %qD",
15374 TREE_PURPOSE (mem_initializer));
15375 mem_initializer = error_mark_node;
15376 }
15377
15378 /* Construct the pack expansion type. */
15379 if (mem_initializer != error_mark_node)
15380 mem_initializer = make_pack_expansion (mem_initializer);
15381 }
15382 if (target_ctor != error_mark_node
15383 && mem_initializer != error_mark_node)
15384 {
15385 error ("mem-initializer for %qD follows constructor delegation",
15386 TREE_PURPOSE (mem_initializer));
15387 mem_initializer = error_mark_node;
15388 }
15389 /* Look for a target constructor. */
15390 if (mem_initializer != error_mark_node
15391 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
15392 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
15393 {
15394 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
15395 if (mem_initializer_list)
15396 {
15397 error ("constructor delegation follows mem-initializer for %qD",
15398 TREE_PURPOSE (mem_initializer_list));
15399 mem_initializer = error_mark_node;
15400 }
15401 target_ctor = mem_initializer;
15402 }
15403 /* Add it to the list, unless it was erroneous. */
15404 if (mem_initializer != error_mark_node)
15405 {
15406 TREE_CHAIN (mem_initializer) = mem_initializer_list;
15407 mem_initializer_list = mem_initializer;
15408 }
15409 /* If the next token is not a `,', we're done. */
15410 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15411 break;
15412 /* Consume the `,' token. */
15413 cp_lexer_consume_token (parser->lexer);
15414 }
15415
15416 /* Perform semantic analysis. */
15417 if (DECL_CONSTRUCTOR_P (current_function_decl))
15418 finish_mem_initializers (mem_initializer_list);
15419 }
15420
15421 /* Parse a mem-initializer.
15422
15423 mem-initializer:
15424 mem-initializer-id ( expression-list [opt] )
15425 mem-initializer-id braced-init-list
15426
15427 GNU extension:
15428
15429 mem-initializer:
15430 ( expression-list [opt] )
15431
15432 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
15433 class) or FIELD_DECL (for a non-static data member) to initialize;
15434 the TREE_VALUE is the expression-list. An empty initialization
15435 list is represented by void_list_node. */
15436
15437 static tree
15438 cp_parser_mem_initializer (cp_parser* parser)
15439 {
15440 tree mem_initializer_id;
15441 tree expression_list;
15442 tree member;
15443 cp_token *token = cp_lexer_peek_token (parser->lexer);
15444
15445 /* Find out what is being initialized. */
15446 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15447 {
15448 permerror (token->location,
15449 "anachronistic old-style base class initializer");
15450 mem_initializer_id = NULL_TREE;
15451 }
15452 else
15453 {
15454 mem_initializer_id = cp_parser_mem_initializer_id (parser);
15455 if (mem_initializer_id == error_mark_node)
15456 return mem_initializer_id;
15457 }
15458 member = expand_member_init (mem_initializer_id);
15459 if (member && !DECL_P (member))
15460 in_base_initializer = 1;
15461
15462 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15463 {
15464 bool expr_non_constant_p;
15465 cp_lexer_set_source_position (parser->lexer);
15466 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15467 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
15468 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
15469 expression_list = build_tree_list (NULL_TREE, expression_list);
15470 }
15471 else
15472 {
15473 vec<tree, va_gc> *vec;
15474 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15475 /*cast_p=*/false,
15476 /*allow_expansion_p=*/true,
15477 /*non_constant_p=*/NULL,
15478 /*close_paren_loc=*/NULL,
15479 /*wrap_locations_p=*/true);
15480 if (vec == NULL)
15481 return error_mark_node;
15482 expression_list = build_tree_list_vec (vec);
15483 release_tree_vector (vec);
15484 }
15485
15486 if (expression_list == error_mark_node)
15487 return error_mark_node;
15488 if (!expression_list)
15489 expression_list = void_type_node;
15490
15491 in_base_initializer = 0;
15492
15493 if (!member)
15494 return error_mark_node;
15495 tree node = build_tree_list (member, expression_list);
15496
15497 /* We can't attach the source location of this initializer directly to
15498 the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
15499 within the TREE_TYPE of the list node. */
15500 location_t loc
15501 = make_location (token->location, token->location, parser->lexer);
15502 tree dummy = build0 (EMPTY_CLASS_EXPR, NULL_TREE);
15503 SET_EXPR_LOCATION (dummy, loc);
15504 TREE_TYPE (node) = dummy;
15505
15506 return node;
15507 }
15508
15509 /* Parse a mem-initializer-id.
15510
15511 mem-initializer-id:
15512 :: [opt] nested-name-specifier [opt] class-name
15513 decltype-specifier (C++11)
15514 identifier
15515
15516 Returns a TYPE indicating the class to be initialized for the first
15517 production (and the second in C++11). Returns an IDENTIFIER_NODE
15518 indicating the data member to be initialized for the last production. */
15519
15520 static tree
15521 cp_parser_mem_initializer_id (cp_parser* parser)
15522 {
15523 bool global_scope_p;
15524 bool nested_name_specifier_p;
15525 bool template_p = false;
15526 tree id;
15527
15528 cp_token *token = cp_lexer_peek_token (parser->lexer);
15529
15530 /* `typename' is not allowed in this context ([temp.res]). */
15531 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15532 {
15533 error_at (token->location,
15534 "keyword %<typename%> not allowed in this context (a qualified "
15535 "member initializer is implicitly a type)");
15536 cp_lexer_consume_token (parser->lexer);
15537 }
15538 /* Look for the optional `::' operator. */
15539 global_scope_p
15540 = (cp_parser_global_scope_opt (parser,
15541 /*current_scope_valid_p=*/false)
15542 != NULL_TREE);
15543 /* Look for the optional nested-name-specifier. The simplest way to
15544 implement:
15545
15546 [temp.res]
15547
15548 The keyword `typename' is not permitted in a base-specifier or
15549 mem-initializer; in these contexts a qualified name that
15550 depends on a template-parameter is implicitly assumed to be a
15551 type name.
15552
15553 is to assume that we have seen the `typename' keyword at this
15554 point. */
15555 nested_name_specifier_p
15556 = (cp_parser_nested_name_specifier_opt (parser,
15557 /*typename_keyword_p=*/true,
15558 /*check_dependency_p=*/true,
15559 /*type_p=*/true,
15560 /*is_declaration=*/true)
15561 != NULL_TREE);
15562 if (nested_name_specifier_p)
15563 template_p = cp_parser_optional_template_keyword (parser);
15564 /* If there is a `::' operator or a nested-name-specifier, then we
15565 are definitely looking for a class-name. */
15566 if (global_scope_p || nested_name_specifier_p)
15567 return cp_parser_class_name (parser,
15568 /*typename_keyword_p=*/true,
15569 /*template_keyword_p=*/template_p,
15570 typename_type,
15571 /*check_dependency_p=*/true,
15572 /*class_head_p=*/false,
15573 /*is_declaration=*/true);
15574 /* Otherwise, we could also be looking for an ordinary identifier. */
15575 cp_parser_parse_tentatively (parser);
15576 if (cp_lexer_next_token_is_decltype (parser->lexer))
15577 /* Try a decltype-specifier. */
15578 id = cp_parser_decltype (parser);
15579 else
15580 /* Otherwise, try a class-name. */
15581 id = cp_parser_class_name (parser,
15582 /*typename_keyword_p=*/true,
15583 /*template_keyword_p=*/false,
15584 none_type,
15585 /*check_dependency_p=*/true,
15586 /*class_head_p=*/false,
15587 /*is_declaration=*/true);
15588 /* If we found one, we're done. */
15589 if (cp_parser_parse_definitely (parser))
15590 return id;
15591 /* Otherwise, look for an ordinary identifier. */
15592 return cp_parser_identifier (parser);
15593 }
15594
15595 /* Overloading [gram.over] */
15596
15597 /* Parse an operator-function-id.
15598
15599 operator-function-id:
15600 operator operator
15601
15602 Returns an IDENTIFIER_NODE for the operator which is a
15603 human-readable spelling of the identifier, e.g., `operator +'. */
15604
15605 static cp_expr
15606 cp_parser_operator_function_id (cp_parser* parser)
15607 {
15608 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
15609 /* Look for the `operator' keyword. */
15610 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15611 return error_mark_node;
15612 /* And then the name of the operator itself. */
15613 return cp_parser_operator (parser, start_loc);
15614 }
15615
15616 /* Return an identifier node for a user-defined literal operator.
15617 The suffix identifier is chained to the operator name identifier. */
15618
15619 tree
15620 cp_literal_operator_id (const char* name)
15621 {
15622 tree identifier;
15623 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
15624 + strlen (name) + 10);
15625 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
15626 identifier = get_identifier (buffer);
15627 XDELETEVEC (buffer);
15628
15629 return identifier;
15630 }
15631
15632 /* Parse an operator.
15633
15634 operator:
15635 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15636 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15637 || ++ -- , ->* -> () []
15638
15639 GNU Extensions:
15640
15641 operator:
15642 <? >? <?= >?=
15643
15644 Returns an IDENTIFIER_NODE for the operator which is a
15645 human-readable spelling of the identifier, e.g., `operator +'. */
15646
15647 static cp_expr
15648 cp_parser_operator (cp_parser* parser, location_t start_loc)
15649 {
15650 tree id = NULL_TREE;
15651 cp_token *token;
15652 bool utf8 = false;
15653
15654 /* Peek at the next token. */
15655 token = cp_lexer_peek_token (parser->lexer);
15656
15657 location_t end_loc = token->location;
15658
15659 /* Figure out which operator we have. */
15660 enum tree_code op = ERROR_MARK;
15661 bool assop = false;
15662 bool consumed = false;
15663 switch (token->type)
15664 {
15665 case CPP_KEYWORD:
15666 {
15667 /* The keyword should be either `new', `delete' or `co_await'. */
15668 if (token->keyword == RID_NEW)
15669 op = NEW_EXPR;
15670 else if (token->keyword == RID_DELETE)
15671 op = DELETE_EXPR;
15672 else if (token->keyword == RID_CO_AWAIT)
15673 op = CO_AWAIT_EXPR;
15674 else
15675 break;
15676
15677 /* Consume the `new', `delete' or co_await token. */
15678 end_loc = cp_lexer_consume_token (parser->lexer)->location;
15679
15680 /* Peek at the next token. */
15681 token = cp_lexer_peek_token (parser->lexer);
15682 /* If it's a `[' token then this is the array variant of the
15683 operator. */
15684 if (token->type == CPP_OPEN_SQUARE
15685 && op != CO_AWAIT_EXPR)
15686 {
15687 /* Consume the `[' token. */
15688 cp_lexer_consume_token (parser->lexer);
15689 /* Look for the `]' token. */
15690 if (cp_token *close_token
15691 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15692 end_loc = close_token->location;
15693 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
15694 }
15695 consumed = true;
15696 break;
15697 }
15698
15699 case CPP_PLUS:
15700 op = PLUS_EXPR;
15701 break;
15702
15703 case CPP_MINUS:
15704 op = MINUS_EXPR;
15705 break;
15706
15707 case CPP_MULT:
15708 op = MULT_EXPR;
15709 break;
15710
15711 case CPP_DIV:
15712 op = TRUNC_DIV_EXPR;
15713 break;
15714
15715 case CPP_MOD:
15716 op = TRUNC_MOD_EXPR;
15717 break;
15718
15719 case CPP_XOR:
15720 op = BIT_XOR_EXPR;
15721 break;
15722
15723 case CPP_AND:
15724 op = BIT_AND_EXPR;
15725 break;
15726
15727 case CPP_OR:
15728 op = BIT_IOR_EXPR;
15729 break;
15730
15731 case CPP_COMPL:
15732 op = BIT_NOT_EXPR;
15733 break;
15734
15735 case CPP_NOT:
15736 op = TRUTH_NOT_EXPR;
15737 break;
15738
15739 case CPP_EQ:
15740 assop = true;
15741 op = NOP_EXPR;
15742 break;
15743
15744 case CPP_LESS:
15745 op = LT_EXPR;
15746 break;
15747
15748 case CPP_GREATER:
15749 op = GT_EXPR;
15750 break;
15751
15752 case CPP_PLUS_EQ:
15753 assop = true;
15754 op = PLUS_EXPR;
15755 break;
15756
15757 case CPP_MINUS_EQ:
15758 assop = true;
15759 op = MINUS_EXPR;
15760 break;
15761
15762 case CPP_MULT_EQ:
15763 assop = true;
15764 op = MULT_EXPR;
15765 break;
15766
15767 case CPP_DIV_EQ:
15768 assop = true;
15769 op = TRUNC_DIV_EXPR;
15770 break;
15771
15772 case CPP_MOD_EQ:
15773 assop = true;
15774 op = TRUNC_MOD_EXPR;
15775 break;
15776
15777 case CPP_XOR_EQ:
15778 assop = true;
15779 op = BIT_XOR_EXPR;
15780 break;
15781
15782 case CPP_AND_EQ:
15783 assop = true;
15784 op = BIT_AND_EXPR;
15785 break;
15786
15787 case CPP_OR_EQ:
15788 assop = true;
15789 op = BIT_IOR_EXPR;
15790 break;
15791
15792 case CPP_LSHIFT:
15793 op = LSHIFT_EXPR;
15794 break;
15795
15796 case CPP_RSHIFT:
15797 op = RSHIFT_EXPR;
15798 break;
15799
15800 case CPP_LSHIFT_EQ:
15801 assop = true;
15802 op = LSHIFT_EXPR;
15803 break;
15804
15805 case CPP_RSHIFT_EQ:
15806 assop = true;
15807 op = RSHIFT_EXPR;
15808 break;
15809
15810 case CPP_EQ_EQ:
15811 op = EQ_EXPR;
15812 break;
15813
15814 case CPP_NOT_EQ:
15815 op = NE_EXPR;
15816 break;
15817
15818 case CPP_LESS_EQ:
15819 op = LE_EXPR;
15820 break;
15821
15822 case CPP_GREATER_EQ:
15823 op = GE_EXPR;
15824 break;
15825
15826 case CPP_SPACESHIP:
15827 op = SPACESHIP_EXPR;
15828 break;
15829
15830 case CPP_AND_AND:
15831 op = TRUTH_ANDIF_EXPR;
15832 break;
15833
15834 case CPP_OR_OR:
15835 op = TRUTH_ORIF_EXPR;
15836 break;
15837
15838 case CPP_PLUS_PLUS:
15839 op = POSTINCREMENT_EXPR;
15840 break;
15841
15842 case CPP_MINUS_MINUS:
15843 op = PREDECREMENT_EXPR;
15844 break;
15845
15846 case CPP_COMMA:
15847 op = COMPOUND_EXPR;
15848 break;
15849
15850 case CPP_DEREF_STAR:
15851 op = MEMBER_REF;
15852 break;
15853
15854 case CPP_DEREF:
15855 op = COMPONENT_REF;
15856 break;
15857
15858 case CPP_QUERY:
15859 op = COND_EXPR;
15860 /* Consume the `?'. */
15861 cp_lexer_consume_token (parser->lexer);
15862 /* Look for the matching `:'. */
15863 cp_parser_require (parser, CPP_COLON, RT_COLON);
15864 consumed = true;
15865 break;
15866
15867 case CPP_OPEN_PAREN:
15868 {
15869 /* Consume the `('. */
15870 matching_parens parens;
15871 parens.consume_open (parser);
15872 /* Look for the matching `)'. */
15873 token = parens.require_close (parser);
15874 if (token)
15875 end_loc = token->location;
15876 op = CALL_EXPR;
15877 consumed = true;
15878 break;
15879 }
15880
15881 case CPP_OPEN_SQUARE:
15882 /* Consume the `['. */
15883 cp_lexer_consume_token (parser->lexer);
15884 /* Look for the matching `]'. */
15885 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15886 if (token)
15887 end_loc = token->location;
15888 op = ARRAY_REF;
15889 consumed = true;
15890 break;
15891
15892 case CPP_UTF8STRING:
15893 case CPP_UTF8STRING_USERDEF:
15894 utf8 = true;
15895 /* FALLTHRU */
15896 case CPP_STRING:
15897 case CPP_WSTRING:
15898 case CPP_STRING16:
15899 case CPP_STRING32:
15900 case CPP_STRING_USERDEF:
15901 case CPP_WSTRING_USERDEF:
15902 case CPP_STRING16_USERDEF:
15903 case CPP_STRING32_USERDEF:
15904 {
15905 cp_expr str;
15906 tree string_tree;
15907 int sz, len;
15908
15909 if (cxx_dialect == cxx98)
15910 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15911
15912 /* Consume the string. */
15913 str = cp_parser_string_literal (parser, /*translate=*/true,
15914 /*wide_ok=*/true, /*lookup_udlit=*/false);
15915 if (str == error_mark_node)
15916 return error_mark_node;
15917 else if (TREE_CODE (str) == USERDEF_LITERAL)
15918 {
15919 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
15920 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
15921 end_loc = str.get_location ();
15922 }
15923 else
15924 {
15925 string_tree = str;
15926 /* Look for the suffix identifier. */
15927 token = cp_lexer_peek_token (parser->lexer);
15928 if (token->type == CPP_NAME)
15929 {
15930 id = cp_parser_identifier (parser);
15931 end_loc = token->location;
15932 }
15933 else if (token->type == CPP_KEYWORD)
15934 {
15935 error ("unexpected keyword;"
15936 " remove space between quotes and suffix identifier");
15937 return error_mark_node;
15938 }
15939 else
15940 {
15941 error ("expected suffix identifier");
15942 return error_mark_node;
15943 }
15944 }
15945 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15946 (TREE_TYPE (TREE_TYPE (string_tree))));
15947 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15948 if (len != 0)
15949 {
15950 error ("expected empty string after %<operator%> keyword");
15951 return error_mark_node;
15952 }
15953 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15954 != char_type_node)
15955 {
15956 error ("invalid encoding prefix in literal operator");
15957 return error_mark_node;
15958 }
15959 if (id != error_mark_node)
15960 {
15961 const char *name = IDENTIFIER_POINTER (id);
15962 id = cp_literal_operator_id (name);
15963 }
15964 /* Generate a location of the form:
15965 "" _suffix_identifier
15966 ^~~~~~~~~~~~~~~~~~~~~
15967 with caret == start at the start token, finish at the end of the
15968 suffix identifier. */
15969 location_t combined_loc
15970 = make_location (start_loc, start_loc, parser->lexer);
15971 return cp_expr (id, combined_loc);
15972 }
15973
15974 default:
15975 /* Anything else is an error. */
15976 break;
15977 }
15978
15979 /* If we have selected an identifier, we need to consume the
15980 operator token. */
15981 if (op != ERROR_MARK)
15982 {
15983 id = ovl_op_identifier (assop, op);
15984 if (!consumed)
15985 cp_lexer_consume_token (parser->lexer);
15986 }
15987 /* Otherwise, no valid operator name was present. */
15988 else
15989 {
15990 cp_parser_error (parser, "expected operator");
15991 id = error_mark_node;
15992 }
15993
15994 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15995 return cp_expr (id, start_loc);
15996 }
15997
15998 /* Parse a template-declaration.
15999
16000 template-declaration:
16001 export [opt] template < template-parameter-list > declaration
16002
16003 If MEMBER_P is TRUE, this template-declaration occurs within a
16004 class-specifier.
16005
16006 The grammar rule given by the standard isn't correct. What
16007 is really meant is:
16008
16009 template-declaration:
16010 export [opt] template-parameter-list-seq
16011 decl-specifier-seq [opt] init-declarator [opt] ;
16012 export [opt] template-parameter-list-seq
16013 function-definition
16014
16015 template-parameter-list-seq:
16016 template-parameter-list-seq [opt]
16017 template < template-parameter-list >
16018
16019 Concept Extensions:
16020
16021 template-parameter-list-seq:
16022 template < template-parameter-list > requires-clause [opt]
16023
16024 requires-clause:
16025 requires logical-or-expression */
16026
16027 static void
16028 cp_parser_template_declaration (cp_parser* parser, bool member_p)
16029 {
16030 /* Check for `export'. */
16031 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
16032 {
16033 /* Consume the `export' token. */
16034 cp_lexer_consume_token (parser->lexer);
16035 /* Warn that this use of export is deprecated. */
16036 if (cxx_dialect < cxx11)
16037 warning (0, "keyword %<export%> not implemented, and will be ignored");
16038 else if (cxx_dialect < cxx20)
16039 warning (0, "keyword %<export%> is deprecated, and is ignored");
16040 else
16041 warning (0, "keyword %<export%> not implemented, and will be ignored");
16042 }
16043
16044 cp_parser_template_declaration_after_export (parser, member_p);
16045 }
16046
16047 /* Parse a template-parameter-list.
16048
16049 template-parameter-list:
16050 template-parameter
16051 template-parameter-list , template-parameter
16052
16053 Returns a TREE_LIST. Each node represents a template parameter.
16054 The nodes are connected via their TREE_CHAINs. */
16055
16056 static tree
16057 cp_parser_template_parameter_list (cp_parser* parser)
16058 {
16059 tree parameter_list = NULL_TREE;
16060
16061 /* Don't create wrapper nodes within a template-parameter-list,
16062 since we don't want to have different types based on the
16063 spelling location of constants and decls within them. */
16064 auto_suppress_location_wrappers sentinel;
16065
16066 begin_template_parm_list ();
16067
16068 /* The loop below parses the template parms. We first need to know
16069 the total number of template parms to be able to compute proper
16070 canonical types of each dependent type. So after the loop, when
16071 we know the total number of template parms,
16072 end_template_parm_list computes the proper canonical types and
16073 fixes up the dependent types accordingly. */
16074 while (true)
16075 {
16076 tree parameter;
16077 bool is_non_type;
16078 bool is_parameter_pack;
16079 location_t parm_loc;
16080
16081 /* Parse the template-parameter. */
16082 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
16083 parameter = cp_parser_template_parameter (parser,
16084 &is_non_type,
16085 &is_parameter_pack);
16086 /* Add it to the list. */
16087 if (parameter != error_mark_node)
16088 parameter_list = process_template_parm (parameter_list,
16089 parm_loc,
16090 parameter,
16091 is_non_type,
16092 is_parameter_pack);
16093 else
16094 {
16095 tree err_parm = build_tree_list (parameter, parameter);
16096 parameter_list = chainon (parameter_list, err_parm);
16097 }
16098
16099 /* If the next token is not a `,', we're done. */
16100 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16101 break;
16102 /* Otherwise, consume the `,' token. */
16103 cp_lexer_consume_token (parser->lexer);
16104 }
16105
16106 return end_template_parm_list (parameter_list);
16107 }
16108
16109 /* Parse a introduction-list.
16110
16111 introduction-list:
16112 introduced-parameter
16113 introduction-list , introduced-parameter
16114
16115 introduced-parameter:
16116 ...[opt] identifier
16117
16118 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
16119 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
16120 WILDCARD_DECL will also have DECL_NAME set and token location in
16121 DECL_SOURCE_LOCATION. */
16122
16123 static tree
16124 cp_parser_introduction_list (cp_parser *parser)
16125 {
16126 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
16127
16128 while (true)
16129 {
16130 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
16131 if (is_pack)
16132 cp_lexer_consume_token (parser->lexer);
16133
16134 tree identifier = cp_parser_identifier (parser);
16135 if (identifier == error_mark_node)
16136 break;
16137
16138 /* Build placeholder. */
16139 tree parm = build_nt (WILDCARD_DECL);
16140 DECL_SOURCE_LOCATION (parm)
16141 = cp_lexer_peek_token (parser->lexer)->location;
16142 DECL_NAME (parm) = identifier;
16143 WILDCARD_PACK_P (parm) = is_pack;
16144 vec_safe_push (introduction_vec, parm);
16145
16146 /* If the next token is not a `,', we're done. */
16147 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16148 break;
16149 /* Otherwise, consume the `,' token. */
16150 cp_lexer_consume_token (parser->lexer);
16151 }
16152
16153 /* Convert the vec into a TREE_VEC. */
16154 tree introduction_list = make_tree_vec (introduction_vec->length ());
16155 unsigned int n;
16156 tree parm;
16157 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
16158 TREE_VEC_ELT (introduction_list, n) = parm;
16159
16160 release_tree_vector (introduction_vec);
16161 return introduction_list;
16162 }
16163
16164 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
16165 is an abstract declarator. */
16166
16167 static inline cp_declarator*
16168 get_id_declarator (cp_declarator *declarator)
16169 {
16170 cp_declarator *d = declarator;
16171 while (d && d->kind != cdk_id)
16172 d = d->declarator;
16173 return d;
16174 }
16175
16176 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
16177 is an abstract declarator. */
16178
16179 static inline tree
16180 get_unqualified_id (cp_declarator *declarator)
16181 {
16182 declarator = get_id_declarator (declarator);
16183 if (declarator)
16184 return declarator->u.id.unqualified_name;
16185 else
16186 return NULL_TREE;
16187 }
16188
16189 /* Returns true if TYPE would declare a constrained constrained-parameter. */
16190
16191 static inline bool
16192 is_constrained_parameter (tree type)
16193 {
16194 return (type
16195 && TREE_CODE (type) == TYPE_DECL
16196 && CONSTRAINED_PARM_CONCEPT (type)
16197 && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
16198 }
16199
16200 /* Returns true if PARM declares a constrained-parameter. */
16201
16202 static inline bool
16203 is_constrained_parameter (cp_parameter_declarator *parm)
16204 {
16205 return is_constrained_parameter (parm->decl_specifiers.type);
16206 }
16207
16208 /* Check that the type parameter is only a declarator-id, and that its
16209 type is not cv-qualified. */
16210
16211 bool
16212 cp_parser_check_constrained_type_parm (cp_parser *parser,
16213 cp_parameter_declarator *parm)
16214 {
16215 if (!parm->declarator)
16216 return true;
16217
16218 if (parm->declarator->kind != cdk_id)
16219 {
16220 cp_parser_error (parser, "invalid constrained type parameter");
16221 return false;
16222 }
16223
16224 /* Don't allow cv-qualified type parameters. */
16225 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
16226 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
16227 {
16228 cp_parser_error (parser, "cv-qualified type parameter");
16229 return false;
16230 }
16231
16232 return true;
16233 }
16234
16235 /* Finish parsing/processing a template type parameter and checking
16236 various restrictions. */
16237
16238 static inline tree
16239 cp_parser_constrained_type_template_parm (cp_parser *parser,
16240 tree id,
16241 cp_parameter_declarator* parmdecl)
16242 {
16243 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
16244 return finish_template_type_parm (class_type_node, id);
16245 else
16246 return error_mark_node;
16247 }
16248
16249 static tree
16250 finish_constrained_template_template_parm (tree proto, tree id)
16251 {
16252 /* FIXME: This should probably be copied, and we may need to adjust
16253 the template parameter depths. */
16254 tree saved_parms = current_template_parms;
16255 begin_template_parm_list ();
16256 current_template_parms = DECL_TEMPLATE_PARMS (proto);
16257 end_template_parm_list ();
16258
16259 tree parm = finish_template_template_parm (class_type_node, id);
16260 current_template_parms = saved_parms;
16261
16262 return parm;
16263 }
16264
16265 /* Finish parsing/processing a template template parameter by borrowing
16266 the template parameter list from the prototype parameter. */
16267
16268 static tree
16269 cp_parser_constrained_template_template_parm (cp_parser *parser,
16270 tree proto,
16271 tree id,
16272 cp_parameter_declarator *parmdecl)
16273 {
16274 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
16275 return error_mark_node;
16276 return finish_constrained_template_template_parm (proto, id);
16277 }
16278
16279 /* Create a new non-type template parameter from the given PARM
16280 declarator. */
16281
16282 static tree
16283 cp_parser_constrained_non_type_template_parm (bool *is_non_type,
16284 cp_parameter_declarator *parm)
16285 {
16286 *is_non_type = true;
16287 cp_declarator *decl = parm->declarator;
16288 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
16289 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
16290 return grokdeclarator (decl, specs, TPARM, 0, NULL);
16291 }
16292
16293 /* Build a constrained template parameter based on the PARMDECL
16294 declarator. The type of PARMDECL is the constrained type, which
16295 refers to the prototype template parameter that ultimately
16296 specifies the type of the declared parameter. */
16297
16298 static tree
16299 finish_constrained_parameter (cp_parser *parser,
16300 cp_parameter_declarator *parmdecl,
16301 bool *is_non_type)
16302 {
16303 tree decl = parmdecl->decl_specifiers.type;
16304 tree id = get_unqualified_id (parmdecl->declarator);
16305 tree def = parmdecl->default_argument;
16306 tree proto = DECL_INITIAL (decl);
16307
16308 /* Build the parameter. Return an error if the declarator was invalid. */
16309 tree parm;
16310 if (TREE_CODE (proto) == TYPE_DECL)
16311 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
16312 else if (TREE_CODE (proto) == TEMPLATE_DECL)
16313 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
16314 parmdecl);
16315 else
16316 parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
16317 if (parm == error_mark_node)
16318 return error_mark_node;
16319
16320 /* Finish the parameter decl and create a node attaching the
16321 default argument and constraint. */
16322 parm = build_tree_list (def, parm);
16323 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
16324
16325 return parm;
16326 }
16327
16328 /* Returns true if the parsed type actually represents the declaration
16329 of a type template-parameter. */
16330
16331 static bool
16332 declares_constrained_type_template_parameter (tree type)
16333 {
16334 return (is_constrained_parameter (type)
16335 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
16336 }
16337
16338 /* Returns true if the parsed type actually represents the declaration of
16339 a template template-parameter. */
16340
16341 static bool
16342 declares_constrained_template_template_parameter (tree type)
16343 {
16344 return (is_constrained_parameter (type)
16345 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
16346 }
16347
16348 /* Parse a default argument for a type template-parameter.
16349 Note that diagnostics are handled in cp_parser_template_parameter. */
16350
16351 static tree
16352 cp_parser_default_type_template_argument (cp_parser *parser)
16353 {
16354 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
16355
16356 /* Consume the `=' token. */
16357 cp_lexer_consume_token (parser->lexer);
16358
16359 cp_token *token = cp_lexer_peek_token (parser->lexer);
16360
16361 /* Parse the default-argument. */
16362 push_deferring_access_checks (dk_no_deferred);
16363 tree default_argument = cp_parser_type_id (parser,
16364 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16365 NULL);
16366 pop_deferring_access_checks ();
16367
16368 if (flag_concepts && type_uses_auto (default_argument))
16369 {
16370 error_at (token->location,
16371 "invalid use of %<auto%> in default template argument");
16372 return error_mark_node;
16373 }
16374
16375 return default_argument;
16376 }
16377
16378 /* Parse a default argument for a template template-parameter. */
16379
16380 static tree
16381 cp_parser_default_template_template_argument (cp_parser *parser)
16382 {
16383 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
16384
16385 bool is_template;
16386
16387 /* Consume the `='. */
16388 cp_lexer_consume_token (parser->lexer);
16389 /* Parse the id-expression. */
16390 push_deferring_access_checks (dk_no_deferred);
16391 /* save token before parsing the id-expression, for error
16392 reporting */
16393 const cp_token* token = cp_lexer_peek_token (parser->lexer);
16394 tree default_argument
16395 = cp_parser_id_expression (parser,
16396 /*template_keyword_p=*/false,
16397 /*check_dependency_p=*/true,
16398 /*template_p=*/&is_template,
16399 /*declarator_p=*/false,
16400 /*optional_p=*/false);
16401 if (TREE_CODE (default_argument) == TYPE_DECL)
16402 /* If the id-expression was a template-id that refers to
16403 a template-class, we already have the declaration here,
16404 so no further lookup is needed. */
16405 ;
16406 else
16407 /* Look up the name. */
16408 default_argument
16409 = cp_parser_lookup_name (parser, default_argument,
16410 none_type,
16411 /*is_template=*/is_template,
16412 /*is_namespace=*/false,
16413 /*check_dependency=*/true,
16414 /*ambiguous_decls=*/NULL,
16415 token->location);
16416 /* See if the default argument is valid. */
16417 default_argument = check_template_template_default_arg (default_argument);
16418 pop_deferring_access_checks ();
16419 return default_argument;
16420 }
16421
16422 /* Parse a template-parameter.
16423
16424 template-parameter:
16425 type-parameter
16426 parameter-declaration
16427
16428 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
16429 the parameter. The TREE_PURPOSE is the default value, if any.
16430 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
16431 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
16432 set to true iff this parameter is a parameter pack. */
16433
16434 static tree
16435 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
16436 bool *is_parameter_pack)
16437 {
16438 cp_token *token;
16439 cp_parameter_declarator *parameter_declarator;
16440 tree parm;
16441
16442 /* Assume it is a type parameter or a template parameter. */
16443 *is_non_type = false;
16444 /* Assume it not a parameter pack. */
16445 *is_parameter_pack = false;
16446 /* Peek at the next token. */
16447 token = cp_lexer_peek_token (parser->lexer);
16448 /* If it is `template', we have a type-parameter. */
16449 if (token->keyword == RID_TEMPLATE)
16450 return cp_parser_type_parameter (parser, is_parameter_pack);
16451 /* If it is `class' or `typename' we do not know yet whether it is a
16452 type parameter or a non-type parameter. Consider:
16453
16454 template <typename T, typename T::X X> ...
16455
16456 or:
16457
16458 template <class C, class D*> ...
16459
16460 Here, the first parameter is a type parameter, and the second is
16461 a non-type parameter. We can tell by looking at the token after
16462 the identifier -- if it is a `,', `=', or `>' then we have a type
16463 parameter. */
16464 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
16465 {
16466 /* Peek at the token after `class' or `typename'. */
16467 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16468 /* If it's an ellipsis, we have a template type parameter
16469 pack. */
16470 if (token->type == CPP_ELLIPSIS)
16471 return cp_parser_type_parameter (parser, is_parameter_pack);
16472 /* If it's an identifier, skip it. */
16473 if (token->type == CPP_NAME)
16474 token = cp_lexer_peek_nth_token (parser->lexer, 3);
16475 /* Now, see if the token looks like the end of a template
16476 parameter. */
16477 if (token->type == CPP_COMMA
16478 || token->type == CPP_EQ
16479 || token->type == CPP_GREATER)
16480 return cp_parser_type_parameter (parser, is_parameter_pack);
16481 }
16482
16483 /* Otherwise, it is a non-type parameter or a constrained parameter.
16484
16485 [temp.param]
16486
16487 When parsing a default template-argument for a non-type
16488 template-parameter, the first non-nested `>' is taken as the end
16489 of the template parameter-list rather than a greater-than
16490 operator. */
16491 parameter_declarator
16492 = cp_parser_parameter_declaration (parser,
16493 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16494 /*template_parm_p=*/true,
16495 /*parenthesized_p=*/NULL);
16496
16497 if (!parameter_declarator)
16498 return error_mark_node;
16499
16500 /* If the parameter declaration is marked as a parameter pack, set
16501 *IS_PARAMETER_PACK to notify the caller. */
16502 if (parameter_declarator->template_parameter_pack_p)
16503 *is_parameter_pack = true;
16504
16505 if (parameter_declarator->default_argument)
16506 {
16507 /* Can happen in some cases of erroneous input (c++/34892). */
16508 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16509 /* Consume the `...' for better error recovery. */
16510 cp_lexer_consume_token (parser->lexer);
16511 }
16512
16513 /* The parameter may have been constrained type parameter. */
16514 if (is_constrained_parameter (parameter_declarator))
16515 return finish_constrained_parameter (parser,
16516 parameter_declarator,
16517 is_non_type);
16518
16519 // Now we're sure that the parameter is a non-type parameter.
16520 *is_non_type = true;
16521
16522 parm = grokdeclarator (parameter_declarator->declarator,
16523 &parameter_declarator->decl_specifiers,
16524 TPARM, /*initialized=*/0,
16525 /*attrlist=*/NULL);
16526 if (parm == error_mark_node)
16527 return error_mark_node;
16528
16529 return build_tree_list (parameter_declarator->default_argument, parm);
16530 }
16531
16532 /* Parse a type-parameter.
16533
16534 type-parameter:
16535 class identifier [opt]
16536 class identifier [opt] = type-id
16537 typename identifier [opt]
16538 typename identifier [opt] = type-id
16539 template < template-parameter-list > class identifier [opt]
16540 template < template-parameter-list > class identifier [opt]
16541 = id-expression
16542
16543 GNU Extension (variadic templates):
16544
16545 type-parameter:
16546 class ... identifier [opt]
16547 typename ... identifier [opt]
16548
16549 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16550 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16551 the declaration of the parameter.
16552
16553 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16554
16555 static tree
16556 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
16557 {
16558 cp_token *token;
16559 tree parameter;
16560
16561 /* Look for a keyword to tell us what kind of parameter this is. */
16562 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
16563 if (!token)
16564 return error_mark_node;
16565
16566 switch (token->keyword)
16567 {
16568 case RID_CLASS:
16569 case RID_TYPENAME:
16570 {
16571 tree identifier;
16572 tree default_argument;
16573
16574 /* If the next token is an ellipsis, we have a template
16575 argument pack. */
16576 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16577 {
16578 /* Consume the `...' token. */
16579 cp_lexer_consume_token (parser->lexer);
16580 maybe_warn_variadic_templates ();
16581
16582 *is_parameter_pack = true;
16583 }
16584
16585 /* If the next token is an identifier, then it names the
16586 parameter. */
16587 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16588 identifier = cp_parser_identifier (parser);
16589 else
16590 identifier = NULL_TREE;
16591
16592 /* Create the parameter. */
16593 parameter = finish_template_type_parm (class_type_node, identifier);
16594
16595 /* If the next token is an `=', we have a default argument. */
16596 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16597 {
16598 default_argument
16599 = cp_parser_default_type_template_argument (parser);
16600
16601 /* Template parameter packs cannot have default
16602 arguments. */
16603 if (*is_parameter_pack)
16604 {
16605 if (identifier)
16606 error_at (token->location,
16607 "template parameter pack %qD cannot have a "
16608 "default argument", identifier);
16609 else
16610 error_at (token->location,
16611 "template parameter packs cannot have "
16612 "default arguments");
16613 default_argument = NULL_TREE;
16614 }
16615 else if (check_for_bare_parameter_packs (default_argument))
16616 default_argument = error_mark_node;
16617 }
16618 else
16619 default_argument = NULL_TREE;
16620
16621 /* Create the combined representation of the parameter and the
16622 default argument. */
16623 parameter = build_tree_list (default_argument, parameter);
16624 }
16625 break;
16626
16627 case RID_TEMPLATE:
16628 {
16629 tree identifier;
16630 tree default_argument;
16631
16632 /* Look for the `<'. */
16633 cp_parser_require (parser, CPP_LESS, RT_LESS);
16634 /* Parse the template-parameter-list. */
16635 cp_parser_template_parameter_list (parser);
16636 /* Look for the `>'. */
16637 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16638
16639 /* If template requirements are present, parse them. */
16640 if (flag_concepts)
16641 {
16642 tree reqs = get_shorthand_constraints (current_template_parms);
16643 if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
16644 reqs = combine_constraint_expressions (reqs, dreqs);
16645 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
16646 }
16647
16648 /* Look for the `class' or 'typename' keywords. */
16649 cp_parser_type_parameter_key (parser);
16650 /* If the next token is an ellipsis, we have a template
16651 argument pack. */
16652 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16653 {
16654 /* Consume the `...' token. */
16655 cp_lexer_consume_token (parser->lexer);
16656 maybe_warn_variadic_templates ();
16657
16658 *is_parameter_pack = true;
16659 }
16660 /* If the next token is an `=', then there is a
16661 default-argument. If the next token is a `>', we are at
16662 the end of the parameter-list. If the next token is a `,',
16663 then we are at the end of this parameter. */
16664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16665 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
16666 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16667 {
16668 identifier = cp_parser_identifier (parser);
16669 /* Treat invalid names as if the parameter were nameless. */
16670 if (identifier == error_mark_node)
16671 identifier = NULL_TREE;
16672 }
16673 else
16674 identifier = NULL_TREE;
16675
16676 /* Create the template parameter. */
16677 parameter = finish_template_template_parm (class_type_node,
16678 identifier);
16679
16680 /* If the next token is an `=', then there is a
16681 default-argument. */
16682 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16683 {
16684 default_argument
16685 = cp_parser_default_template_template_argument (parser);
16686
16687 /* Template parameter packs cannot have default
16688 arguments. */
16689 if (*is_parameter_pack)
16690 {
16691 if (identifier)
16692 error_at (token->location,
16693 "template parameter pack %qD cannot "
16694 "have a default argument",
16695 identifier);
16696 else
16697 error_at (token->location, "template parameter packs cannot "
16698 "have default arguments");
16699 default_argument = NULL_TREE;
16700 }
16701 }
16702 else
16703 default_argument = NULL_TREE;
16704
16705 /* Create the combined representation of the parameter and the
16706 default argument. */
16707 parameter = build_tree_list (default_argument, parameter);
16708 }
16709 break;
16710
16711 default:
16712 gcc_unreachable ();
16713 break;
16714 }
16715
16716 return parameter;
16717 }
16718
16719 /* Parse a template-id.
16720
16721 template-id:
16722 template-name < template-argument-list [opt] >
16723
16724 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16725 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16726 returned. Otherwise, if the template-name names a function, or set
16727 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16728 names a class, returns a TYPE_DECL for the specialization.
16729
16730 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16731 uninstantiated templates. */
16732
16733 static tree
16734 cp_parser_template_id (cp_parser *parser,
16735 bool template_keyword_p,
16736 bool check_dependency_p,
16737 enum tag_types tag_type,
16738 bool is_declaration)
16739 {
16740 tree templ;
16741 tree arguments;
16742 tree template_id;
16743 cp_token_position start_of_id = 0;
16744 cp_token *next_token = NULL, *next_token_2 = NULL;
16745 bool is_identifier;
16746
16747 /* If the next token corresponds to a template-id, there is no need
16748 to reparse it. */
16749 cp_token *token = cp_lexer_peek_token (parser->lexer);
16750
16751 if (token->type == CPP_TEMPLATE_ID)
16752 {
16753 cp_lexer_consume_token (parser->lexer);
16754 return saved_checks_value (token->u.tree_check_value);
16755 }
16756
16757 /* Avoid performing name lookup if there is no possibility of
16758 finding a template-id. */
16759 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16760 || (token->type == CPP_NAME
16761 && !cp_parser_nth_token_starts_template_argument_list_p
16762 (parser, 2)))
16763 {
16764 cp_parser_error (parser, "expected template-id");
16765 return error_mark_node;
16766 }
16767
16768 /* Remember where the template-id starts. */
16769 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16770 start_of_id = cp_lexer_token_position (parser->lexer, false);
16771
16772 push_deferring_access_checks (dk_deferred);
16773
16774 /* Parse the template-name. */
16775 is_identifier = false;
16776 templ = cp_parser_template_name (parser, template_keyword_p,
16777 check_dependency_p,
16778 is_declaration,
16779 tag_type,
16780 &is_identifier);
16781
16782 /* Push any access checks inside the firewall we're about to create. */
16783 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
16784 pop_deferring_access_checks ();
16785 if (templ == error_mark_node || is_identifier)
16786 return templ;
16787
16788 /* Since we're going to preserve any side-effects from this parse, set up a
16789 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16790 in the template arguments. */
16791 tentative_firewall firewall (parser);
16792 reopen_deferring_access_checks (checks);
16793
16794 /* If we find the sequence `[:' after a template-name, it's probably
16795 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16796 parse correctly the argument list. */
16797 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16798 == CPP_OPEN_SQUARE)
16799 && next_token->flags & DIGRAPH
16800 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16801 == CPP_COLON)
16802 && !(next_token_2->flags & PREV_WHITE))
16803 {
16804 cp_parser_parse_tentatively (parser);
16805 /* Change `:' into `::'. */
16806 next_token_2->type = CPP_SCOPE;
16807 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16808 CPP_LESS. */
16809 cp_lexer_consume_token (parser->lexer);
16810
16811 /* Parse the arguments. */
16812 arguments = cp_parser_enclosed_template_argument_list (parser);
16813 if (!cp_parser_parse_definitely (parser))
16814 {
16815 /* If we couldn't parse an argument list, then we revert our changes
16816 and return simply an error. Maybe this is not a template-id
16817 after all. */
16818 next_token_2->type = CPP_COLON;
16819 cp_parser_error (parser, "expected %<<%>");
16820 pop_deferring_access_checks ();
16821 return error_mark_node;
16822 }
16823 /* Otherwise, emit an error about the invalid digraph, but continue
16824 parsing because we got our argument list. */
16825 if (permerror (next_token->location,
16826 "%<<::%> cannot begin a template-argument list"))
16827 {
16828 static bool hint = false;
16829 inform (next_token->location,
16830 "%<<:%> is an alternate spelling for %<[%>."
16831 " Insert whitespace between %<<%> and %<::%>");
16832 if (!hint && !flag_permissive)
16833 {
16834 inform (next_token->location, "(if you use %<-fpermissive%> "
16835 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16836 "accept your code)");
16837 hint = true;
16838 }
16839 }
16840 }
16841 else
16842 {
16843 /* Look for the `<' that starts the template-argument-list. */
16844 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16845 {
16846 pop_deferring_access_checks ();
16847 return error_mark_node;
16848 }
16849 /* Parse the arguments. */
16850 arguments = cp_parser_enclosed_template_argument_list (parser);
16851
16852 if ((cxx_dialect > cxx17)
16853 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16854 && !template_keyword_p
16855 && (cp_parser_error_occurred (parser)
16856 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16857 {
16858 /* This didn't go well. */
16859 if (TREE_CODE (templ) == FUNCTION_DECL)
16860 {
16861 /* C++20 says that "function-name < a;" is now ill-formed. */
16862 if (cp_parser_error_occurred (parser))
16863 {
16864 error_at (token->location, "invalid template-argument-list");
16865 inform (token->location, "function name as the left hand "
16866 "operand of %<<%> is ill-formed in C++20; wrap the "
16867 "function name in %<()%>");
16868 }
16869 else
16870 /* We expect "f<targs>" to be followed by "(args)". */
16871 error_at (cp_lexer_peek_token (parser->lexer)->location,
16872 "expected %<(%> after template-argument-list");
16873 if (start_of_id)
16874 /* Purge all subsequent tokens. */
16875 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16876 }
16877 else
16878 cp_parser_simulate_error (parser);
16879 pop_deferring_access_checks ();
16880 return error_mark_node;
16881 }
16882 }
16883
16884 /* Set the location to be of the form:
16885 template-name < template-argument-list [opt] >
16886 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16887 with caret == start at the start of the template-name,
16888 ranging until the closing '>'. */
16889 location_t combined_loc
16890 = make_location (token->location, token->location, parser->lexer);
16891
16892 /* Check for concepts autos where they don't belong. We could
16893 identify types in some cases of identifier TEMPL, looking ahead
16894 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16895 types. We reject them in functions, but if what we have is an
16896 identifier, even with none_type we can't conclude it's NOT a
16897 type, we have to wait for template substitution. */
16898 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16899 template_id = error_mark_node;
16900 /* Build a representation of the specialization. */
16901 else if (identifier_p (templ))
16902 template_id = build_min_nt_loc (combined_loc,
16903 TEMPLATE_ID_EXPR,
16904 templ, arguments);
16905 else if (DECL_TYPE_TEMPLATE_P (templ)
16906 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16907 {
16908 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16909 template (rather than some instantiation thereof) only if
16910 is not nested within some other construct. For example, in
16911 "template <typename T> void f(T) { A<T>::", A<T> is just an
16912 instantiation of A. */
16913 bool entering_scope
16914 = (template_parm_scope_p ()
16915 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16916 template_id
16917 = finish_template_type (templ, arguments, entering_scope);
16918 }
16919 else if (concept_definition_p (templ))
16920 {
16921 /* The caller will decide whether this is a concept check or type
16922 constraint. */
16923 template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
16924 boolean_type_node, templ, arguments);
16925 }
16926 else if (variable_template_p (templ))
16927 {
16928 template_id = lookup_template_variable (templ, arguments);
16929 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16930 SET_EXPR_LOCATION (template_id, combined_loc);
16931 }
16932 else
16933 {
16934 /* If it's not a class-template or a template-template, it should be
16935 a function-template. */
16936 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
16937
16938 template_id = lookup_template_function (templ, arguments);
16939 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16940 SET_EXPR_LOCATION (template_id, combined_loc);
16941 }
16942
16943 /* If parsing tentatively, replace the sequence of tokens that makes
16944 up the template-id with a CPP_TEMPLATE_ID token. That way,
16945 should we re-parse the token stream, we will not have to repeat
16946 the effort required to do the parse, nor will we issue duplicate
16947 error messages about problems during instantiation of the
16948 template. */
16949 if (start_of_id
16950 /* Don't do this if we had a parse error in a declarator; re-parsing
16951 might succeed if a name changes meaning (60361). */
16952 && !(cp_parser_error_occurred (parser)
16953 && cp_parser_parsing_tentatively (parser)
16954 && parser->in_declarator_p))
16955 {
16956 /* Reset the contents of the START_OF_ID token. */
16957 token->type = CPP_TEMPLATE_ID;
16958 token->location = combined_loc;
16959
16960 /* Retrieve any deferred checks. Do not pop this access checks yet
16961 so the memory will not be reclaimed during token replacing below. */
16962 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16963 token->tree_check_p = true;
16964 token->u.tree_check_value->value = template_id;
16965 token->u.tree_check_value->checks = get_deferred_access_checks ();
16966 token->keyword = RID_MAX;
16967
16968 /* Purge all subsequent tokens. */
16969 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16970
16971 /* ??? Can we actually assume that, if template_id ==
16972 error_mark_node, we will have issued a diagnostic to the
16973 user, as opposed to simply marking the tentative parse as
16974 failed? */
16975 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16976 error_at (token->location, "parse error in template argument list");
16977 }
16978
16979 pop_to_parent_deferring_access_checks ();
16980 return template_id;
16981 }
16982
16983 /* Like cp_parser_template_id, called in non-type context. */
16984
16985 static tree
16986 cp_parser_template_id_expr (cp_parser *parser,
16987 bool template_keyword_p,
16988 bool check_dependency_p,
16989 bool is_declaration)
16990 {
16991 tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
16992 none_type, is_declaration);
16993 if (TREE_CODE (x) == TEMPLATE_ID_EXPR
16994 && concept_check_p (x))
16995 /* We didn't check the arguments in cp_parser_template_id; do that now. */
16996 return build_concept_id (x);
16997 return x;
16998 }
16999
17000 /* Parse a template-name.
17001
17002 template-name:
17003 identifier
17004
17005 The standard should actually say:
17006
17007 template-name:
17008 identifier
17009 operator-function-id
17010
17011 A defect report has been filed about this issue.
17012
17013 A conversion-function-id cannot be a template name because they cannot
17014 be part of a template-id. In fact, looking at this code:
17015
17016 a.operator K<int>()
17017
17018 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
17019 It is impossible to call a templated conversion-function-id with an
17020 explicit argument list, since the only allowed template parameter is
17021 the type to which it is converting.
17022
17023 If TEMPLATE_KEYWORD_P is true, then we have just seen the
17024 `template' keyword, in a construction like:
17025
17026 T::template f<3>()
17027
17028 In that case `f' is taken to be a template-name, even though there
17029 is no way of knowing for sure.
17030
17031 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
17032 name refers to a set of overloaded functions, at least one of which
17033 is a template, or an IDENTIFIER_NODE with the name of the template,
17034 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
17035 names are looked up inside uninstantiated templates. */
17036
17037 static tree
17038 cp_parser_template_name (cp_parser* parser,
17039 bool template_keyword_p,
17040 bool check_dependency_p,
17041 bool is_declaration,
17042 enum tag_types tag_type,
17043 bool *is_identifier)
17044 {
17045 tree identifier;
17046 tree decl;
17047 cp_token *token = cp_lexer_peek_token (parser->lexer);
17048
17049 /* If the next token is `operator', then we have either an
17050 operator-function-id or a conversion-function-id. */
17051 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
17052 {
17053 /* We don't know whether we're looking at an
17054 operator-function-id or a conversion-function-id. */
17055 cp_parser_parse_tentatively (parser);
17056 /* Try an operator-function-id. */
17057 identifier = cp_parser_operator_function_id (parser);
17058 /* If that didn't work, try a conversion-function-id. */
17059 if (!cp_parser_parse_definitely (parser))
17060 {
17061 cp_parser_error (parser, "expected template-name");
17062 return error_mark_node;
17063 }
17064 }
17065 /* Look for the identifier. */
17066 else
17067 identifier = cp_parser_identifier (parser);
17068
17069 /* If we didn't find an identifier, we don't have a template-id. */
17070 if (identifier == error_mark_node)
17071 return error_mark_node;
17072
17073 /* If the name immediately followed the `template' keyword, then it
17074 is a template-name. However, if the next token is not `<', then
17075 we do not treat it as a template-name, since it is not being used
17076 as part of a template-id. This enables us to handle constructs
17077 like:
17078
17079 template <typename T> struct S { S(); };
17080 template <typename T> S<T>::S();
17081
17082 correctly. We would treat `S' as a template -- if it were `S<T>'
17083 -- but we do not if there is no `<'. */
17084
17085 if (processing_template_decl
17086 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
17087 {
17088 /* In a declaration, in a dependent context, we pretend that the
17089 "template" keyword was present in order to improve error
17090 recovery. For example, given:
17091
17092 template <typename T> void f(T::X<int>);
17093
17094 we want to treat "X<int>" as a template-id. */
17095 if (is_declaration
17096 && !template_keyword_p
17097 && parser->scope && TYPE_P (parser->scope)
17098 && check_dependency_p
17099 && dependent_scope_p (parser->scope)
17100 /* Do not do this for dtors (or ctors), since they never
17101 need the template keyword before their name. */
17102 && !constructor_name_p (identifier, parser->scope))
17103 {
17104 cp_token_position start = 0;
17105
17106 /* Explain what went wrong. */
17107 error_at (token->location, "non-template %qD used as template",
17108 identifier);
17109 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
17110 parser->scope, identifier);
17111 /* If parsing tentatively, find the location of the "<" token. */
17112 if (cp_parser_simulate_error (parser))
17113 start = cp_lexer_token_position (parser->lexer, true);
17114 /* Parse the template arguments so that we can issue error
17115 messages about them. */
17116 cp_lexer_consume_token (parser->lexer);
17117 cp_parser_enclosed_template_argument_list (parser);
17118 /* Skip tokens until we find a good place from which to
17119 continue parsing. */
17120 cp_parser_skip_to_closing_parenthesis (parser,
17121 /*recovering=*/true,
17122 /*or_comma=*/true,
17123 /*consume_paren=*/false);
17124 /* If parsing tentatively, permanently remove the
17125 template argument list. That will prevent duplicate
17126 error messages from being issued about the missing
17127 "template" keyword. */
17128 if (start)
17129 cp_lexer_purge_tokens_after (parser->lexer, start);
17130 if (is_identifier)
17131 *is_identifier = true;
17132 parser->context->object_type = NULL_TREE;
17133 return identifier;
17134 }
17135
17136 /* If the "template" keyword is present, then there is generally
17137 no point in doing name-lookup, so we just return IDENTIFIER.
17138 But, if the qualifying scope is non-dependent then we can
17139 (and must) do name-lookup normally. */
17140 if (template_keyword_p)
17141 {
17142 tree scope = (parser->scope ? parser->scope
17143 : parser->context->object_type);
17144 if (scope && TYPE_P (scope)
17145 && (!CLASS_TYPE_P (scope)
17146 || (check_dependency_p && dependent_type_p (scope))))
17147 {
17148 /* We're optimizing away the call to cp_parser_lookup_name, but
17149 we still need to do this. */
17150 parser->context->object_type = NULL_TREE;
17151 return identifier;
17152 }
17153 }
17154 }
17155
17156 /* cp_parser_lookup_name clears OBJECT_TYPE. */
17157 const bool scoped_p = ((parser->scope ? parser->scope
17158 : parser->context->object_type) != NULL_TREE);
17159
17160 /* Look up the name. */
17161 decl = cp_parser_lookup_name (parser, identifier,
17162 tag_type,
17163 /*is_template=*/true,
17164 /*is_namespace=*/false,
17165 check_dependency_p,
17166 /*ambiguous_decls=*/NULL,
17167 token->location);
17168
17169 decl = strip_using_decl (decl);
17170
17171 /* If DECL is a template, then the name was a template-name. */
17172 if (TREE_CODE (decl) == TEMPLATE_DECL)
17173 {
17174 if (TREE_DEPRECATED (decl)
17175 && deprecated_state != DEPRECATED_SUPPRESS)
17176 {
17177 tree d = DECL_TEMPLATE_RESULT (decl);
17178 tree attr;
17179 if (TREE_CODE (d) == TYPE_DECL)
17180 attr = lookup_attribute ("deprecated",
17181 TYPE_ATTRIBUTES (TREE_TYPE (d)));
17182 else
17183 attr = lookup_attribute ("deprecated",
17184 DECL_ATTRIBUTES (d));
17185 warn_deprecated_use (decl, attr);
17186 }
17187 }
17188 else
17189 {
17190 /* The standard does not explicitly indicate whether a name that
17191 names a set of overloaded declarations, some of which are
17192 templates, is a template-name. However, such a name should
17193 be a template-name; otherwise, there is no way to form a
17194 template-id for the overloaded templates. */
17195 bool found = false;
17196
17197 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
17198 !found && iter; ++iter)
17199 if (TREE_CODE (*iter) == TEMPLATE_DECL)
17200 found = true;
17201
17202 if (!found
17203 && (cxx_dialect > cxx17)
17204 && !scoped_p
17205 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
17206 && tag_type == none_type)
17207 {
17208 /* [temp.names] says "A name is also considered to refer to a template
17209 if it is an unqualified-id followed by a < and name lookup finds
17210 either one or more functions or finds nothing." */
17211
17212 /* The "more functions" case. Just use the OVERLOAD as normally.
17213 We don't use is_overloaded_fn here to avoid considering
17214 BASELINKs. */
17215 if (TREE_CODE (decl) == OVERLOAD
17216 /* Name lookup found one function. */
17217 || TREE_CODE (decl) == FUNCTION_DECL)
17218 found = true;
17219 /* Name lookup found nothing. */
17220 else if (decl == error_mark_node)
17221 return identifier;
17222 }
17223
17224 if (!found)
17225 {
17226 /* The name does not name a template. */
17227 cp_parser_error (parser, "expected template-name");
17228 return error_mark_node;
17229 }
17230 }
17231
17232 return decl;
17233 }
17234
17235 /* Parse a template-argument-list.
17236
17237 template-argument-list:
17238 template-argument ... [opt]
17239 template-argument-list , template-argument ... [opt]
17240
17241 Returns a TREE_VEC containing the arguments. */
17242
17243 static tree
17244 cp_parser_template_argument_list (cp_parser* parser)
17245 {
17246 tree fixed_args[10];
17247 unsigned n_args = 0;
17248 unsigned alloced = 10;
17249 tree *arg_ary = fixed_args;
17250 tree vec;
17251 bool saved_in_template_argument_list_p;
17252 bool saved_ice_p;
17253 bool saved_non_ice_p;
17254
17255 /* Don't create location wrapper nodes within a template-argument-list. */
17256 auto_suppress_location_wrappers sentinel;
17257
17258 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
17259 parser->in_template_argument_list_p = true;
17260 /* Even if the template-id appears in an integral
17261 constant-expression, the contents of the argument list do
17262 not. */
17263 saved_ice_p = parser->integral_constant_expression_p;
17264 parser->integral_constant_expression_p = false;
17265 saved_non_ice_p = parser->non_integral_constant_expression_p;
17266 parser->non_integral_constant_expression_p = false;
17267
17268 /* Parse the arguments. */
17269 do
17270 {
17271 tree argument;
17272
17273 if (n_args)
17274 /* Consume the comma. */
17275 cp_lexer_consume_token (parser->lexer);
17276
17277 /* Parse the template-argument. */
17278 argument = cp_parser_template_argument (parser);
17279
17280 /* If the next token is an ellipsis, we're expanding a template
17281 argument pack. */
17282 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17283 {
17284 if (argument == error_mark_node)
17285 {
17286 cp_token *token = cp_lexer_peek_token (parser->lexer);
17287 error_at (token->location,
17288 "expected parameter pack before %<...%>");
17289 }
17290 /* Consume the `...' token. */
17291 cp_lexer_consume_token (parser->lexer);
17292
17293 /* Make the argument into a TYPE_PACK_EXPANSION or
17294 EXPR_PACK_EXPANSION. */
17295 argument = make_pack_expansion (argument);
17296 }
17297
17298 if (n_args == alloced)
17299 {
17300 alloced *= 2;
17301
17302 if (arg_ary == fixed_args)
17303 {
17304 arg_ary = XNEWVEC (tree, alloced);
17305 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
17306 }
17307 else
17308 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
17309 }
17310 arg_ary[n_args++] = argument;
17311 }
17312 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17313
17314 vec = make_tree_vec (n_args);
17315
17316 while (n_args--)
17317 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
17318
17319 if (arg_ary != fixed_args)
17320 free (arg_ary);
17321 parser->non_integral_constant_expression_p = saved_non_ice_p;
17322 parser->integral_constant_expression_p = saved_ice_p;
17323 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
17324 if (CHECKING_P)
17325 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
17326 return vec;
17327 }
17328
17329 /* Parse a template-argument.
17330
17331 template-argument:
17332 assignment-expression
17333 type-id
17334 id-expression
17335
17336 The representation is that of an assignment-expression, type-id, or
17337 id-expression -- except that the qualified id-expression is
17338 evaluated, so that the value returned is either a DECL or an
17339 OVERLOAD.
17340
17341 Although the standard says "assignment-expression", it forbids
17342 throw-expressions or assignments in the template argument.
17343 Therefore, we use "conditional-expression" instead. */
17344
17345 static tree
17346 cp_parser_template_argument (cp_parser* parser)
17347 {
17348 tree argument;
17349 bool template_p;
17350 bool address_p;
17351 bool maybe_type_id = false;
17352 cp_token *token = NULL, *argument_start_token = NULL;
17353 location_t loc = 0;
17354 cp_id_kind idk;
17355
17356 /* There's really no way to know what we're looking at, so we just
17357 try each alternative in order.
17358
17359 [temp.arg]
17360
17361 In a template-argument, an ambiguity between a type-id and an
17362 expression is resolved to a type-id, regardless of the form of
17363 the corresponding template-parameter.
17364
17365 Therefore, we try a type-id first. */
17366 cp_parser_parse_tentatively (parser);
17367 argument = cp_parser_template_type_arg (parser);
17368 /* If there was no error parsing the type-id but the next token is a
17369 '>>', our behavior depends on which dialect of C++ we're
17370 parsing. In C++98, we probably found a typo for '> >'. But there
17371 are type-id which are also valid expressions. For instance:
17372
17373 struct X { int operator >> (int); };
17374 template <int V> struct Foo {};
17375 Foo<X () >> 5> r;
17376
17377 Here 'X()' is a valid type-id of a function type, but the user just
17378 wanted to write the expression "X() >> 5". Thus, we remember that we
17379 found a valid type-id, but we still try to parse the argument as an
17380 expression to see what happens.
17381
17382 In C++0x, the '>>' will be considered two separate '>'
17383 tokens. */
17384 if (!cp_parser_error_occurred (parser)
17385 && cxx_dialect == cxx98
17386 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17387 {
17388 maybe_type_id = true;
17389 cp_parser_abort_tentative_parse (parser);
17390 }
17391 else
17392 {
17393 /* If the next token isn't a `,' or a `>', then this argument wasn't
17394 really finished. This means that the argument is not a valid
17395 type-id. */
17396 if (!cp_parser_next_token_ends_template_argument_p (parser))
17397 cp_parser_error (parser, "expected template-argument");
17398 /* If that worked, we're done. */
17399 if (cp_parser_parse_definitely (parser))
17400 return argument;
17401 }
17402 /* We're still not sure what the argument will be. */
17403 cp_parser_parse_tentatively (parser);
17404 /* Try a template. */
17405 argument_start_token = cp_lexer_peek_token (parser->lexer);
17406 argument = cp_parser_id_expression (parser,
17407 /*template_keyword_p=*/false,
17408 /*check_dependency_p=*/true,
17409 &template_p,
17410 /*declarator_p=*/false,
17411 /*optional_p=*/false);
17412 /* If the next token isn't a `,' or a `>', then this argument wasn't
17413 really finished. */
17414 if (!cp_parser_next_token_ends_template_argument_p (parser))
17415 cp_parser_error (parser, "expected template-argument");
17416 if (!cp_parser_error_occurred (parser))
17417 {
17418 /* Figure out what is being referred to. If the id-expression
17419 was for a class template specialization, then we will have a
17420 TYPE_DECL at this point. There is no need to do name lookup
17421 at this point in that case. */
17422 if (TREE_CODE (argument) != TYPE_DECL)
17423 argument = cp_parser_lookup_name (parser, argument,
17424 none_type,
17425 /*is_template=*/template_p,
17426 /*is_namespace=*/false,
17427 /*check_dependency=*/true,
17428 /*ambiguous_decls=*/NULL,
17429 argument_start_token->location);
17430 if (TREE_CODE (argument) != TEMPLATE_DECL
17431 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
17432 cp_parser_error (parser, "expected template-name");
17433 }
17434 if (cp_parser_parse_definitely (parser))
17435 {
17436 if (TREE_DEPRECATED (argument))
17437 warn_deprecated_use (argument, NULL_TREE);
17438 return argument;
17439 }
17440 /* It must be a non-type argument. In C++17 any constant-expression is
17441 allowed. */
17442 if (cxx_dialect > cxx14)
17443 goto general_expr;
17444
17445 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
17446
17447 -- an integral constant-expression of integral or enumeration
17448 type; or
17449
17450 -- the name of a non-type template-parameter; or
17451
17452 -- the name of an object or function with external linkage...
17453
17454 -- the address of an object or function with external linkage...
17455
17456 -- a pointer to member... */
17457 /* Look for a non-type template parameter. */
17458 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17459 {
17460 cp_parser_parse_tentatively (parser);
17461 argument = cp_parser_primary_expression (parser,
17462 /*address_p=*/false,
17463 /*cast_p=*/false,
17464 /*template_arg_p=*/true,
17465 &idk);
17466 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
17467 || !cp_parser_next_token_ends_template_argument_p (parser))
17468 cp_parser_simulate_error (parser);
17469 if (cp_parser_parse_definitely (parser))
17470 return argument;
17471 }
17472
17473 /* If the next token is "&", the argument must be the address of an
17474 object or function with external linkage. */
17475 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
17476 if (address_p)
17477 {
17478 loc = cp_lexer_peek_token (parser->lexer)->location;
17479 cp_lexer_consume_token (parser->lexer);
17480 }
17481 /* See if we might have an id-expression. */
17482 token = cp_lexer_peek_token (parser->lexer);
17483 if (token->type == CPP_NAME
17484 || token->keyword == RID_OPERATOR
17485 || token->type == CPP_SCOPE
17486 || token->type == CPP_TEMPLATE_ID
17487 || token->type == CPP_NESTED_NAME_SPECIFIER)
17488 {
17489 cp_parser_parse_tentatively (parser);
17490 argument = cp_parser_primary_expression (parser,
17491 address_p,
17492 /*cast_p=*/false,
17493 /*template_arg_p=*/true,
17494 &idk);
17495 if (cp_parser_error_occurred (parser)
17496 || !cp_parser_next_token_ends_template_argument_p (parser))
17497 cp_parser_abort_tentative_parse (parser);
17498 else
17499 {
17500 tree probe;
17501
17502 if (INDIRECT_REF_P (argument))
17503 {
17504 /* Strip the dereference temporarily. */
17505 gcc_assert (REFERENCE_REF_P (argument));
17506 argument = TREE_OPERAND (argument, 0);
17507 }
17508
17509 /* If we're in a template, we represent a qualified-id referring
17510 to a static data member as a SCOPE_REF even if the scope isn't
17511 dependent so that we can check access control later. */
17512 probe = argument;
17513 if (TREE_CODE (probe) == SCOPE_REF)
17514 probe = TREE_OPERAND (probe, 1);
17515 if (VAR_P (probe))
17516 {
17517 /* A variable without external linkage might still be a
17518 valid constant-expression, so no error is issued here
17519 if the external-linkage check fails. */
17520 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
17521 cp_parser_simulate_error (parser);
17522 }
17523 else if (is_overloaded_fn (argument))
17524 /* All overloaded functions are allowed; if the external
17525 linkage test does not pass, an error will be issued
17526 later. */
17527 ;
17528 else if (address_p
17529 && (TREE_CODE (argument) == OFFSET_REF
17530 || TREE_CODE (argument) == SCOPE_REF))
17531 /* A pointer-to-member. */
17532 ;
17533 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
17534 ;
17535 else
17536 cp_parser_simulate_error (parser);
17537
17538 if (cp_parser_parse_definitely (parser))
17539 {
17540 if (address_p)
17541 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
17542 tf_warning_or_error);
17543 else
17544 argument = convert_from_reference (argument);
17545 return argument;
17546 }
17547 }
17548 }
17549 /* If the argument started with "&", there are no other valid
17550 alternatives at this point. */
17551 if (address_p)
17552 {
17553 cp_parser_error (parser, "invalid non-type template argument");
17554 return error_mark_node;
17555 }
17556
17557 general_expr:
17558 /* If the argument wasn't successfully parsed as a type-id followed
17559 by '>>', the argument can only be a constant expression now.
17560 Otherwise, we try parsing the constant-expression tentatively,
17561 because the argument could really be a type-id. */
17562 if (maybe_type_id)
17563 cp_parser_parse_tentatively (parser);
17564
17565 if (cxx_dialect <= cxx14)
17566 argument = cp_parser_constant_expression (parser);
17567 else
17568 {
17569 /* In C++20, we can encounter a braced-init-list. */
17570 if (cxx_dialect >= cxx20
17571 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17572 {
17573 bool expr_non_constant_p;
17574 return cp_parser_braced_list (parser, &expr_non_constant_p);
17575 }
17576
17577 /* With C++17 generalized non-type template arguments we need to handle
17578 lvalue constant expressions, too. */
17579 argument = cp_parser_assignment_expression (parser);
17580 require_potential_constant_expression (argument);
17581 }
17582
17583 if (!maybe_type_id)
17584 return argument;
17585 if (!cp_parser_next_token_ends_template_argument_p (parser))
17586 cp_parser_error (parser, "expected template-argument");
17587 if (cp_parser_parse_definitely (parser))
17588 return argument;
17589 /* We did our best to parse the argument as a non type-id, but that
17590 was the only alternative that matched (albeit with a '>' after
17591 it). We can assume it's just a typo from the user, and a
17592 diagnostic will then be issued. */
17593 return cp_parser_template_type_arg (parser);
17594 }
17595
17596 /* Parse an explicit-instantiation.
17597
17598 explicit-instantiation:
17599 template declaration
17600
17601 Although the standard says `declaration', what it really means is:
17602
17603 explicit-instantiation:
17604 template decl-specifier-seq [opt] declarator [opt] ;
17605
17606 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17607 supposed to be allowed. A defect report has been filed about this
17608 issue.
17609
17610 GNU Extension:
17611
17612 explicit-instantiation:
17613 storage-class-specifier template
17614 decl-specifier-seq [opt] declarator [opt] ;
17615 function-specifier template
17616 decl-specifier-seq [opt] declarator [opt] ; */
17617
17618 static void
17619 cp_parser_explicit_instantiation (cp_parser* parser)
17620 {
17621 int declares_class_or_enum;
17622 cp_decl_specifier_seq decl_specifiers;
17623 tree extension_specifier = NULL_TREE;
17624
17625 timevar_push (TV_TEMPLATE_INST);
17626
17627 /* Look for an (optional) storage-class-specifier or
17628 function-specifier. */
17629 if (cp_parser_allow_gnu_extensions_p (parser))
17630 {
17631 extension_specifier
17632 = cp_parser_storage_class_specifier_opt (parser);
17633 if (!extension_specifier)
17634 extension_specifier
17635 = cp_parser_function_specifier_opt (parser,
17636 /*decl_specs=*/NULL);
17637 }
17638
17639 /* Look for the `template' keyword. */
17640 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17641 /* Let the front end know that we are processing an explicit
17642 instantiation. */
17643 begin_explicit_instantiation ();
17644 /* [temp.explicit] says that we are supposed to ignore access
17645 control while processing explicit instantiation directives. */
17646 push_deferring_access_checks (dk_no_check);
17647 /* Parse a decl-specifier-seq. */
17648 cp_parser_decl_specifier_seq (parser,
17649 CP_PARSER_FLAGS_OPTIONAL,
17650 &decl_specifiers,
17651 &declares_class_or_enum);
17652 /* If there was exactly one decl-specifier, and it declared a class,
17653 and there's no declarator, then we have an explicit type
17654 instantiation. */
17655 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
17656 {
17657 tree type = check_tag_decl (&decl_specifiers,
17658 /*explicit_type_instantiation_p=*/true);
17659 /* Turn access control back on for names used during
17660 template instantiation. */
17661 pop_deferring_access_checks ();
17662 if (type)
17663 do_type_instantiation (type, extension_specifier,
17664 /*complain=*/tf_error);
17665 }
17666 else
17667 {
17668 cp_declarator *declarator;
17669 tree decl;
17670
17671 /* Parse the declarator. */
17672 declarator
17673 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17674 CP_PARSER_FLAGS_NONE,
17675 /*ctor_dtor_or_conv_p=*/NULL,
17676 /*parenthesized_p=*/NULL,
17677 /*member_p=*/false,
17678 /*friend_p=*/false,
17679 /*static_p=*/false);
17680 if (declares_class_or_enum & 2)
17681 cp_parser_check_for_definition_in_return_type (declarator,
17682 decl_specifiers.type,
17683 decl_specifiers.locations[ds_type_spec]);
17684 if (declarator != cp_error_declarator)
17685 {
17686 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
17687 permerror (decl_specifiers.locations[ds_inline],
17688 "explicit instantiation shall not use"
17689 " %<inline%> specifier");
17690 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
17691 permerror (decl_specifiers.locations[ds_constexpr],
17692 "explicit instantiation shall not use"
17693 " %<constexpr%> specifier");
17694 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
17695 permerror (decl_specifiers.locations[ds_consteval],
17696 "explicit instantiation shall not use"
17697 " %<consteval%> specifier");
17698
17699 decl = grokdeclarator (declarator, &decl_specifiers,
17700 NORMAL, 0, &decl_specifiers.attributes);
17701 /* Turn access control back on for names used during
17702 template instantiation. */
17703 pop_deferring_access_checks ();
17704 /* Do the explicit instantiation. */
17705 do_decl_instantiation (decl, extension_specifier);
17706 }
17707 else
17708 {
17709 pop_deferring_access_checks ();
17710 /* Skip the body of the explicit instantiation. */
17711 cp_parser_skip_to_end_of_statement (parser);
17712 }
17713 }
17714 /* We're done with the instantiation. */
17715 end_explicit_instantiation ();
17716
17717 cp_parser_consume_semicolon_at_end_of_statement (parser);
17718
17719 timevar_pop (TV_TEMPLATE_INST);
17720 }
17721
17722 /* Parse an explicit-specialization.
17723
17724 explicit-specialization:
17725 template < > declaration
17726
17727 Although the standard says `declaration', what it really means is:
17728
17729 explicit-specialization:
17730 template <> decl-specifier [opt] init-declarator [opt] ;
17731 template <> function-definition
17732 template <> explicit-specialization
17733 template <> template-declaration */
17734
17735 static void
17736 cp_parser_explicit_specialization (cp_parser* parser)
17737 {
17738 cp_token *token = cp_lexer_peek_token (parser->lexer);
17739
17740 /* Look for the `template' keyword. */
17741 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17742 /* Look for the `<'. */
17743 cp_parser_require (parser, CPP_LESS, RT_LESS);
17744 /* Look for the `>'. */
17745 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17746 /* We have processed another parameter list. */
17747 ++parser->num_template_parameter_lists;
17748
17749 /* [temp]
17750
17751 A template ... explicit specialization ... shall not have C
17752 linkage. */
17753 bool need_lang_pop = current_lang_name == lang_name_c;
17754 if (need_lang_pop)
17755 {
17756 error_at (token->location, "template specialization with C linkage");
17757 maybe_show_extern_c_location ();
17758
17759 /* Give it C++ linkage to avoid confusing other parts of the
17760 front end. */
17761 push_lang_context (lang_name_cplusplus);
17762 }
17763
17764 /* Let the front end know that we are beginning a specialization. */
17765 if (begin_specialization ())
17766 {
17767 /* If the next keyword is `template', we need to figure out
17768 whether or not we're looking a template-declaration. */
17769 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17770 {
17771 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17772 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
17773 cp_parser_template_declaration_after_export (parser,
17774 /*member_p=*/false);
17775 else
17776 cp_parser_explicit_specialization (parser);
17777 }
17778 else
17779 /* Parse the dependent declaration. */
17780 cp_parser_single_declaration (parser,
17781 /*checks=*/NULL,
17782 /*member_p=*/false,
17783 /*explicit_specialization_p=*/true,
17784 /*friend_p=*/NULL);
17785 }
17786
17787 /* We're done with the specialization. */
17788 end_specialization ();
17789
17790 /* For the erroneous case of a template with C linkage, we pushed an
17791 implicit C++ linkage scope; exit that scope now. */
17792 if (need_lang_pop)
17793 pop_lang_context ();
17794
17795 /* We're done with this parameter list. */
17796 --parser->num_template_parameter_lists;
17797 }
17798
17799 /* Parse a type-specifier.
17800
17801 type-specifier:
17802 simple-type-specifier
17803 class-specifier
17804 enum-specifier
17805 elaborated-type-specifier
17806 cv-qualifier
17807
17808 GNU Extension:
17809
17810 type-specifier:
17811 __complex__
17812
17813 Returns a representation of the type-specifier. For a
17814 class-specifier, enum-specifier, or elaborated-type-specifier, a
17815 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17816
17817 The parser flags FLAGS is used to control type-specifier parsing.
17818
17819 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17820 in a decl-specifier-seq.
17821
17822 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17823 class-specifier, enum-specifier, or elaborated-type-specifier, then
17824 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17825 if a type is declared; 2 if it is defined. Otherwise, it is set to
17826 zero.
17827
17828 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17829 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17830 is set to FALSE. */
17831
17832 static tree
17833 cp_parser_type_specifier (cp_parser* parser,
17834 cp_parser_flags flags,
17835 cp_decl_specifier_seq *decl_specs,
17836 bool is_declaration,
17837 int* declares_class_or_enum,
17838 bool* is_cv_qualifier)
17839 {
17840 tree type_spec = NULL_TREE;
17841 cp_token *token;
17842 enum rid keyword;
17843 cp_decl_spec ds = ds_last;
17844
17845 /* Assume this type-specifier does not declare a new type. */
17846 if (declares_class_or_enum)
17847 *declares_class_or_enum = 0;
17848 /* And that it does not specify a cv-qualifier. */
17849 if (is_cv_qualifier)
17850 *is_cv_qualifier = false;
17851 /* Peek at the next token. */
17852 token = cp_lexer_peek_token (parser->lexer);
17853
17854 /* If we're looking at a keyword, we can use that to guide the
17855 production we choose. */
17856 keyword = token->keyword;
17857 switch (keyword)
17858 {
17859 case RID_ENUM:
17860 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17861 goto elaborated_type_specifier;
17862
17863 /* Look for the enum-specifier. */
17864 type_spec = cp_parser_enum_specifier (parser);
17865 /* If that worked, we're done. */
17866 if (type_spec)
17867 {
17868 if (declares_class_or_enum)
17869 *declares_class_or_enum = 2;
17870 if (decl_specs)
17871 cp_parser_set_decl_spec_type (decl_specs,
17872 type_spec,
17873 token,
17874 /*type_definition_p=*/true);
17875 return type_spec;
17876 }
17877 else
17878 goto elaborated_type_specifier;
17879
17880 /* Any of these indicate either a class-specifier, or an
17881 elaborated-type-specifier. */
17882 case RID_CLASS:
17883 case RID_STRUCT:
17884 case RID_UNION:
17885 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17886 goto elaborated_type_specifier;
17887
17888 /* Parse tentatively so that we can back up if we don't find a
17889 class-specifier. */
17890 cp_parser_parse_tentatively (parser);
17891 /* Look for the class-specifier. */
17892 type_spec = cp_parser_class_specifier (parser);
17893 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17894 /* If that worked, we're done. */
17895 if (cp_parser_parse_definitely (parser))
17896 {
17897 if (declares_class_or_enum)
17898 *declares_class_or_enum = 2;
17899 if (decl_specs)
17900 cp_parser_set_decl_spec_type (decl_specs,
17901 type_spec,
17902 token,
17903 /*type_definition_p=*/true);
17904 return type_spec;
17905 }
17906
17907 /* Fall through. */
17908 elaborated_type_specifier:
17909 /* We're declaring (not defining) a class or enum. */
17910 if (declares_class_or_enum)
17911 *declares_class_or_enum = 1;
17912
17913 /* Fall through. */
17914 case RID_TYPENAME:
17915 /* Look for an elaborated-type-specifier. */
17916 type_spec
17917 = (cp_parser_elaborated_type_specifier
17918 (parser,
17919 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17920 is_declaration));
17921 if (decl_specs)
17922 cp_parser_set_decl_spec_type (decl_specs,
17923 type_spec,
17924 token,
17925 /*type_definition_p=*/false);
17926 return type_spec;
17927
17928 case RID_CONST:
17929 ds = ds_const;
17930 if (is_cv_qualifier)
17931 *is_cv_qualifier = true;
17932 break;
17933
17934 case RID_VOLATILE:
17935 ds = ds_volatile;
17936 if (is_cv_qualifier)
17937 *is_cv_qualifier = true;
17938 break;
17939
17940 case RID_RESTRICT:
17941 ds = ds_restrict;
17942 if (is_cv_qualifier)
17943 *is_cv_qualifier = true;
17944 break;
17945
17946 case RID_COMPLEX:
17947 /* The `__complex__' keyword is a GNU extension. */
17948 ds = ds_complex;
17949 break;
17950
17951 default:
17952 break;
17953 }
17954
17955 /* Handle simple keywords. */
17956 if (ds != ds_last)
17957 {
17958 if (decl_specs)
17959 {
17960 set_and_check_decl_spec_loc (decl_specs, ds, token);
17961 decl_specs->any_specifiers_p = true;
17962 }
17963 return cp_lexer_consume_token (parser->lexer)->u.value;
17964 }
17965
17966 /* If we do not already have a type-specifier, assume we are looking
17967 at a simple-type-specifier. */
17968 type_spec = cp_parser_simple_type_specifier (parser,
17969 decl_specs,
17970 flags);
17971
17972 /* If we didn't find a type-specifier, and a type-specifier was not
17973 optional in this context, issue an error message. */
17974 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17975 {
17976 cp_parser_error (parser, "expected type specifier");
17977 return error_mark_node;
17978 }
17979
17980 return type_spec;
17981 }
17982
17983 /* Parse a simple-type-specifier.
17984
17985 simple-type-specifier:
17986 :: [opt] nested-name-specifier [opt] type-name
17987 :: [opt] nested-name-specifier template template-id
17988 char
17989 wchar_t
17990 bool
17991 short
17992 int
17993 long
17994 signed
17995 unsigned
17996 float
17997 double
17998 void
17999
18000 C++11 Extension:
18001
18002 simple-type-specifier:
18003 auto
18004 decltype ( expression )
18005 char16_t
18006 char32_t
18007 __underlying_type ( type-id )
18008
18009 C++17 extension:
18010
18011 nested-name-specifier(opt) template-name
18012
18013 GNU Extension:
18014
18015 simple-type-specifier:
18016 __int128
18017 __typeof__ unary-expression
18018 __typeof__ ( type-id )
18019 __typeof__ ( type-id ) { initializer-list , [opt] }
18020
18021 Concepts Extension:
18022
18023 simple-type-specifier:
18024 constrained-type-specifier
18025
18026 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
18027 appropriately updated. */
18028
18029 static tree
18030 cp_parser_simple_type_specifier (cp_parser* parser,
18031 cp_decl_specifier_seq *decl_specs,
18032 cp_parser_flags flags)
18033 {
18034 tree type = NULL_TREE;
18035 cp_token *token;
18036 int idx;
18037
18038 /* Peek at the next token. */
18039 token = cp_lexer_peek_token (parser->lexer);
18040
18041 /* If we're looking at a keyword, things are easy. */
18042 switch (token->keyword)
18043 {
18044 case RID_CHAR:
18045 if (decl_specs)
18046 decl_specs->explicit_char_p = true;
18047 type = char_type_node;
18048 break;
18049 case RID_CHAR8:
18050 type = char8_type_node;
18051 break;
18052 case RID_CHAR16:
18053 type = char16_type_node;
18054 break;
18055 case RID_CHAR32:
18056 type = char32_type_node;
18057 break;
18058 case RID_WCHAR:
18059 type = wchar_type_node;
18060 break;
18061 case RID_BOOL:
18062 type = boolean_type_node;
18063 break;
18064 case RID_SHORT:
18065 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
18066 type = short_integer_type_node;
18067 break;
18068 case RID_INT:
18069 if (decl_specs)
18070 decl_specs->explicit_int_p = true;
18071 type = integer_type_node;
18072 break;
18073 case RID_INT_N_0:
18074 case RID_INT_N_1:
18075 case RID_INT_N_2:
18076 case RID_INT_N_3:
18077 idx = token->keyword - RID_INT_N_0;
18078 if (! int_n_enabled_p [idx])
18079 break;
18080 if (decl_specs)
18081 {
18082 decl_specs->explicit_intN_p = true;
18083 decl_specs->int_n_idx = idx;
18084 /* Check if the alternate "__intN__" form has been used instead of
18085 "__intN". */
18086 if (strncmp (IDENTIFIER_POINTER (token->u.value)
18087 + (IDENTIFIER_LENGTH (token->u.value) - 2),
18088 "__", 2) == 0)
18089 decl_specs->int_n_alt = true;
18090 }
18091 type = int_n_trees [idx].signed_type;
18092 break;
18093 case RID_LONG:
18094 if (decl_specs)
18095 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
18096 type = long_integer_type_node;
18097 break;
18098 case RID_SIGNED:
18099 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
18100 type = integer_type_node;
18101 break;
18102 case RID_UNSIGNED:
18103 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
18104 type = unsigned_type_node;
18105 break;
18106 case RID_FLOAT:
18107 type = float_type_node;
18108 break;
18109 case RID_DOUBLE:
18110 type = double_type_node;
18111 break;
18112 case RID_VOID:
18113 type = void_type_node;
18114 break;
18115
18116 case RID_AUTO:
18117 maybe_warn_cpp0x (CPP0X_AUTO);
18118 if (parser->auto_is_implicit_function_template_parm_p)
18119 {
18120 /* The 'auto' might be the placeholder return type for a function decl
18121 with trailing return type. */
18122 bool have_trailing_return_fn_decl = false;
18123
18124 cp_parser_parse_tentatively (parser);
18125 cp_lexer_consume_token (parser->lexer);
18126 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
18127 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
18128 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18129 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
18130 {
18131 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18132 {
18133 cp_lexer_consume_token (parser->lexer);
18134 cp_parser_skip_to_closing_parenthesis (parser,
18135 /*recovering*/false,
18136 /*or_comma*/false,
18137 /*consume_paren*/true);
18138 continue;
18139 }
18140
18141 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
18142 {
18143 have_trailing_return_fn_decl = true;
18144 break;
18145 }
18146
18147 cp_lexer_consume_token (parser->lexer);
18148 }
18149 cp_parser_abort_tentative_parse (parser);
18150
18151 if (have_trailing_return_fn_decl)
18152 {
18153 type = make_auto ();
18154 break;
18155 }
18156
18157 if (cxx_dialect >= cxx14)
18158 {
18159 type = synthesize_implicit_template_parm (parser, NULL_TREE);
18160 type = TREE_TYPE (type);
18161 }
18162 else
18163 type = error_mark_node;
18164
18165 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
18166 {
18167 if (cxx_dialect < cxx14)
18168 error_at (token->location,
18169 "use of %<auto%> in lambda parameter declaration "
18170 "only available with "
18171 "%<-std=c++14%> or %<-std=gnu++14%>");
18172 }
18173 else if (cxx_dialect < cxx14)
18174 error_at (token->location,
18175 "use of %<auto%> in parameter declaration "
18176 "only available with "
18177 "%<-std=c++14%> or %<-std=gnu++14%>");
18178 else if (!flag_concepts)
18179 pedwarn (token->location, 0,
18180 "use of %<auto%> in parameter declaration "
18181 "only available with %<-fconcepts-ts%>");
18182 }
18183 else
18184 type = make_auto ();
18185 break;
18186
18187 case RID_DECLTYPE:
18188 /* Since DR 743, decltype can either be a simple-type-specifier by
18189 itself or begin a nested-name-specifier. Parsing it will replace
18190 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
18191 handling below decide what to do. */
18192 cp_parser_decltype (parser);
18193 cp_lexer_set_token_position (parser->lexer, token);
18194 break;
18195
18196 case RID_TYPEOF:
18197 /* Consume the `typeof' token. */
18198 cp_lexer_consume_token (parser->lexer);
18199 /* Parse the operand to `typeof'. */
18200 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
18201 /* If it is not already a TYPE, take its type. */
18202 if (!TYPE_P (type))
18203 type = finish_typeof (type);
18204
18205 if (decl_specs)
18206 cp_parser_set_decl_spec_type (decl_specs, type,
18207 token,
18208 /*type_definition_p=*/false);
18209
18210 return type;
18211
18212 case RID_UNDERLYING_TYPE:
18213 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
18214 if (decl_specs)
18215 cp_parser_set_decl_spec_type (decl_specs, type,
18216 token,
18217 /*type_definition_p=*/false);
18218
18219 return type;
18220
18221 case RID_BASES:
18222 case RID_DIRECT_BASES:
18223 type = cp_parser_trait_expr (parser, token->keyword);
18224 if (decl_specs)
18225 cp_parser_set_decl_spec_type (decl_specs, type,
18226 token,
18227 /*type_definition_p=*/false);
18228 return type;
18229 default:
18230 break;
18231 }
18232
18233 /* If token is an already-parsed decltype not followed by ::,
18234 it's a simple-type-specifier. */
18235 if (token->type == CPP_DECLTYPE
18236 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
18237 {
18238 type = saved_checks_value (token->u.tree_check_value);
18239 if (decl_specs)
18240 {
18241 cp_parser_set_decl_spec_type (decl_specs, type,
18242 token,
18243 /*type_definition_p=*/false);
18244 /* Remember that we are handling a decltype in order to
18245 implement the resolution of DR 1510 when the argument
18246 isn't instantiation dependent. */
18247 decl_specs->decltype_p = true;
18248 }
18249 cp_lexer_consume_token (parser->lexer);
18250 return type;
18251 }
18252
18253 /* If the type-specifier was for a built-in type, we're done. */
18254 if (type)
18255 {
18256 /* Record the type. */
18257 if (decl_specs
18258 && (token->keyword != RID_SIGNED
18259 && token->keyword != RID_UNSIGNED
18260 && token->keyword != RID_SHORT
18261 && token->keyword != RID_LONG))
18262 cp_parser_set_decl_spec_type (decl_specs,
18263 type,
18264 token,
18265 /*type_definition_p=*/false);
18266 if (decl_specs)
18267 decl_specs->any_specifiers_p = true;
18268
18269 /* Consume the token. */
18270 cp_lexer_consume_token (parser->lexer);
18271
18272 if (type == error_mark_node)
18273 return error_mark_node;
18274
18275 /* There is no valid C++ program where a non-template type is
18276 followed by a "<". That usually indicates that the user thought
18277 that the type was a template. */
18278 cp_parser_check_for_invalid_template_id (parser, type, none_type,
18279 token->location);
18280
18281 return TYPE_NAME (type);
18282 }
18283
18284 /* The type-specifier must be a user-defined type. */
18285 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
18286 {
18287 bool qualified_p;
18288 bool global_p;
18289 const bool typename_p = (cxx_dialect >= cxx20
18290 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
18291
18292 /* Don't gobble tokens or issue error messages if this is an
18293 optional type-specifier. */
18294 if (flags & CP_PARSER_FLAGS_OPTIONAL)
18295 cp_parser_parse_tentatively (parser);
18296
18297 /* Remember current tentative parsing state -- if we know we need
18298 a type, we can give better diagnostics here. */
18299 bool tent = cp_parser_parsing_tentatively (parser);
18300
18301 token = cp_lexer_peek_token (parser->lexer);
18302
18303 /* Look for the optional `::' operator. */
18304 global_p
18305 = (cp_parser_global_scope_opt (parser,
18306 /*current_scope_valid_p=*/false)
18307 != NULL_TREE);
18308 /* Look for the nested-name specifier. */
18309 qualified_p
18310 = (cp_parser_nested_name_specifier_opt (parser,
18311 /*typename_keyword_p=*/false,
18312 /*check_dependency_p=*/true,
18313 /*type_p=*/false,
18314 /*is_declaration=*/false)
18315 != NULL_TREE);
18316 /* If we have seen a nested-name-specifier, and the next token
18317 is `template', then we are using the template-id production. */
18318 if (parser->scope
18319 && cp_parser_optional_template_keyword (parser))
18320 {
18321 /* Look for the template-id. */
18322 type = cp_parser_template_id (parser,
18323 /*template_keyword_p=*/true,
18324 /*check_dependency_p=*/true,
18325 none_type,
18326 /*is_declaration=*/false);
18327 /* If the template-id did not name a type, we are out of
18328 luck. */
18329 if (TREE_CODE (type) != TYPE_DECL)
18330 {
18331 /* ...unless we pretend we have seen 'typename'. */
18332 if (typename_p)
18333 type = cp_parser_make_typename_type (parser, type,
18334 token->location);
18335 else
18336 {
18337 cp_parser_error (parser, "expected template-id for type");
18338 type = error_mark_node;
18339 }
18340 }
18341 }
18342 /* DR 1812: A < following a qualified-id in a typename-specifier
18343 could safely be assumed to begin a template argument list, so
18344 the template keyword should be optional. */
18345 else if (parser->scope
18346 && qualified_p
18347 && typename_p
18348 && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
18349 {
18350 cp_parser_parse_tentatively (parser);
18351
18352 type = cp_parser_template_id (parser,
18353 /*template_keyword_p=*/true,
18354 /*check_dependency_p=*/true,
18355 none_type,
18356 /*is_declaration=*/false);
18357 /* This is handled below, so back off. */
18358 if (type && concept_check_p (type))
18359 cp_parser_simulate_error (parser);
18360
18361 if (!cp_parser_parse_definitely (parser))
18362 type = NULL_TREE;
18363 else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
18364 type = make_typename_type (parser->scope, type, typename_type,
18365 /*complain=*/tf_error);
18366 else if (TREE_CODE (type) != TYPE_DECL)
18367 type = NULL_TREE;
18368 }
18369
18370 /* Otherwise, look for a type-name. */
18371 if (!type)
18372 {
18373 if (cxx_dialect >= cxx17)
18374 cp_parser_parse_tentatively (parser);
18375
18376 type = cp_parser_type_name (parser, (qualified_p && typename_p));
18377
18378 if (cxx_dialect >= cxx17 && !cp_parser_parse_definitely (parser))
18379 type = NULL_TREE;
18380 }
18381
18382 if (!type && flag_concepts && decl_specs)
18383 {
18384 /* Try for a type-constraint with template arguments. We check
18385 decl_specs here to avoid trying this for a functional cast. */
18386
18387 cp_parser_parse_tentatively (parser);
18388
18389 type = cp_parser_template_id (parser,
18390 /*template_keyword_p=*/false,
18391 /*check_dependency_p=*/true,
18392 none_type,
18393 /*is_declaration=*/false);
18394 if (type && concept_check_p (type))
18395 {
18396 location_t loc = EXPR_LOCATION (type);
18397 type = cp_parser_placeholder_type_specifier (parser, loc,
18398 type, tent);
18399 if (tent && type == error_mark_node)
18400 /* Perhaps it's a concept-check expression. */
18401 cp_parser_simulate_error (parser);
18402 }
18403 else
18404 cp_parser_simulate_error (parser);
18405
18406 if (!cp_parser_parse_definitely (parser))
18407 type = NULL_TREE;
18408 }
18409
18410 if (!type && cxx_dialect >= cxx17)
18411 {
18412 /* Try class template argument deduction or type-constraint without
18413 template arguments. */
18414 tree name = cp_parser_identifier (parser);
18415 if (name && TREE_CODE (name) == IDENTIFIER_NODE
18416 && parser->scope != error_mark_node)
18417 {
18418 location_t loc
18419 = cp_lexer_previous_token (parser->lexer)->location;
18420 tree tmpl = cp_parser_lookup_name (parser, name,
18421 none_type,
18422 /*is_template=*/false,
18423 /*is_namespace=*/false,
18424 /*check_dependency=*/true,
18425 /*ambiguous_decls=*/NULL,
18426 token->location);
18427 if (tmpl && tmpl != error_mark_node
18428 && ctad_template_p (tmpl))
18429 type = make_template_placeholder (tmpl);
18430 else if (flag_concepts && tmpl && concept_definition_p (tmpl))
18431 type = cp_parser_placeholder_type_specifier (parser, loc,
18432 tmpl, tent);
18433 else
18434 {
18435 type = error_mark_node;
18436 if (!cp_parser_simulate_error (parser))
18437 cp_parser_name_lookup_error (parser, name, tmpl,
18438 NLE_TYPE, token->location);
18439 }
18440 }
18441 else
18442 type = error_mark_node;
18443 }
18444
18445 /* If it didn't work out, we don't have a TYPE. */
18446 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
18447 && !cp_parser_parse_definitely (parser))
18448 type = NULL_TREE;
18449
18450 /* Keep track of all name-lookups performed in class scopes. */
18451 if (type
18452 && !global_p
18453 && !qualified_p
18454 && TREE_CODE (type) == TYPE_DECL
18455 && identifier_p (DECL_NAME (type)))
18456 maybe_note_name_used_in_class (DECL_NAME (type), type);
18457
18458 if (type && decl_specs)
18459 cp_parser_set_decl_spec_type (decl_specs, type,
18460 token,
18461 /*type_definition_p=*/false);
18462 }
18463
18464 /* If we didn't get a type-name, issue an error message. */
18465 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
18466 {
18467 cp_parser_error (parser, "expected type-name");
18468 return error_mark_node;
18469 }
18470
18471 if (type && type != error_mark_node)
18472 {
18473 /* See if TYPE is an Objective-C type, and if so, parse and
18474 accept any protocol references following it. Do this before
18475 the cp_parser_check_for_invalid_template_id() call, because
18476 Objective-C types can be followed by '<...>' which would
18477 enclose protocol names rather than template arguments, and so
18478 everything is fine. */
18479 if (c_dialect_objc () && !parser->scope
18480 && (objc_is_id (type) || objc_is_class_name (type)))
18481 {
18482 tree protos = cp_parser_objc_protocol_refs_opt (parser);
18483 tree qual_type = objc_get_protocol_qualified_type (type, protos);
18484
18485 /* Clobber the "unqualified" type previously entered into
18486 DECL_SPECS with the new, improved protocol-qualified version. */
18487 if (decl_specs)
18488 decl_specs->type = qual_type;
18489
18490 return qual_type;
18491 }
18492
18493 /* There is no valid C++ program where a non-template type is
18494 followed by a "<". That usually indicates that the user
18495 thought that the type was a template. */
18496 cp_parser_check_for_invalid_template_id (parser, type,
18497 none_type,
18498 token->location);
18499 }
18500
18501 return type;
18502 }
18503
18504 /* Parse the remainder of a placholder-type-specifier.
18505
18506 placeholder-type-specifier:
18507 type-constraint_opt auto
18508 type-constraint_opt decltype(auto)
18509
18510 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
18511 and passed as TMPL. This function converts TMPL to an actual type-constraint,
18512 parses the placeholder type, and performs some contextual syntactic analysis.
18513
18514 LOC provides the location of the template name.
18515
18516 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
18517 don't give an error if TMPL isn't a valid type-constraint, as the template-id
18518 might actually be a concept-check,
18519
18520 Note that the Concepts TS allows the auto or decltype(auto) to be
18521 omitted in a constrained-type-specifier. */
18522
18523 tree
18524 cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
18525 tree tmpl, bool tentative)
18526 {
18527 if (tmpl == error_mark_node)
18528 return error_mark_node;
18529
18530 tree orig_tmpl = tmpl;
18531
18532 /* Get the arguments as written for subsequent analysis. */
18533 tree args = NULL_TREE;
18534 if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
18535 {
18536 args = TREE_OPERAND (tmpl, 1);
18537 tmpl = TREE_OPERAND (tmpl, 0);
18538 }
18539 if (args == NULL_TREE)
18540 /* A concept-name with no arguments can't be an expression. */
18541 tentative = false;
18542
18543 tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
18544
18545 /* Get the concept and prototype parameter for the constraint. */
18546 tree_pair info = finish_type_constraints (tmpl, args, complain);
18547 tree con = info.first;
18548 tree proto = info.second;
18549 if (con == error_mark_node)
18550 return error_mark_node;
18551
18552 /* As per the standard, require auto or decltype(auto), except in some
18553 cases (template parameter lists, -fconcepts-ts enabled). */
18554 cp_token *placeholder = NULL, *close_paren = NULL;
18555 if (cxx_dialect >= cxx20)
18556 {
18557 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
18558 placeholder = cp_lexer_consume_token (parser->lexer);
18559 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
18560 {
18561 placeholder = cp_lexer_consume_token (parser->lexer);
18562 matching_parens parens;
18563 parens.require_open (parser);
18564 cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
18565 close_paren = parens.require_close (parser);
18566 }
18567 }
18568
18569 /* A type constraint constrains a contextually determined type or type
18570 parameter pack. However, the Concepts TS does allow concepts
18571 to introduce non-type and template template parameters. */
18572 if (TREE_CODE (proto) != TYPE_DECL)
18573 {
18574 if (!flag_concepts_ts
18575 || !processing_template_parmlist)
18576 {
18577 error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
18578 inform (DECL_SOURCE_LOCATION (con), "concept defined here");
18579 return error_mark_node;
18580 }
18581 }
18582
18583 /* In a template parameter list, a type-parameter can be introduced
18584 by type-constraints alone. */
18585 if (processing_template_parmlist && !placeholder)
18586 return build_constrained_parameter (con, proto, args);
18587
18588 /* Diagnose issues placeholder issues. */
18589 if (!flag_concepts_ts
18590 && !parser->in_result_type_constraint_p
18591 && !placeholder)
18592 {
18593 if (tentative)
18594 /* Perhaps it's a concept-check expression (c++/91073). */
18595 return error_mark_node;
18596
18597 tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
18598 tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
18599 error_at (input_location,
18600 "expected %<auto%> or %<decltype(auto)%> after %qE", expr);
18601 /* Fall through. This is an error of omission. */
18602 }
18603 else if (parser->in_result_type_constraint_p && placeholder)
18604 {
18605 /* A trailing return type only allows type-constraints. */
18606 error_at (input_location,
18607 "unexpected placeholder in constrained result type");
18608 }
18609
18610 /* In a parameter-declaration-clause, a placeholder-type-specifier
18611 results in an invented template parameter. */
18612 if (parser->auto_is_implicit_function_template_parm_p)
18613 {
18614 if (close_paren)
18615 {
18616 location_t loc = make_location (placeholder->location,
18617 placeholder->location,
18618 close_paren->location);
18619 error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
18620 return error_mark_node;
18621 }
18622 tree parm = build_constrained_parameter (con, proto, args);
18623 return synthesize_implicit_template_parm (parser, parm);
18624 }
18625
18626 /* Determine if the type should be deduced using template argument
18627 deduction or decltype deduction. Note that the latter is always
18628 used for type-constraints in trailing return types. */
18629 bool decltype_p = placeholder
18630 ? placeholder->keyword == RID_DECLTYPE
18631 : parser->in_result_type_constraint_p;
18632
18633 /* Otherwise, this is the type of a variable or return type. */
18634 if (decltype_p)
18635 return make_constrained_decltype_auto (con, args);
18636 else
18637 return make_constrained_auto (con, args);
18638 }
18639
18640 /* Parse a type-name.
18641
18642 type-name:
18643 class-name
18644 enum-name
18645 typedef-name
18646 simple-template-id [in c++0x]
18647
18648 enum-name:
18649 identifier
18650
18651 typedef-name:
18652 identifier
18653
18654 Concepts:
18655
18656 type-name:
18657 concept-name
18658 partial-concept-id
18659
18660 concept-name:
18661 identifier
18662
18663 Returns a TYPE_DECL for the type. */
18664
18665 static tree
18666 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
18667 {
18668 tree type_decl;
18669
18670 /* We can't know yet whether it is a class-name or not. */
18671 cp_parser_parse_tentatively (parser);
18672 /* Try a class-name. */
18673 type_decl = cp_parser_class_name (parser,
18674 typename_keyword_p,
18675 /*template_keyword_p=*/false,
18676 none_type,
18677 /*check_dependency_p=*/true,
18678 /*class_head_p=*/false,
18679 /*is_declaration=*/false);
18680 /* If it's not a class-name, keep looking. */
18681 if (!cp_parser_parse_definitely (parser))
18682 {
18683 if (cxx_dialect < cxx11)
18684 /* It must be a typedef-name or an enum-name. */
18685 return cp_parser_nonclass_name (parser);
18686
18687 cp_parser_parse_tentatively (parser);
18688 /* It is either a simple-template-id representing an
18689 instantiation of an alias template... */
18690 type_decl = cp_parser_template_id (parser,
18691 /*template_keyword_p=*/false,
18692 /*check_dependency_p=*/true,
18693 none_type,
18694 /*is_declaration=*/false);
18695 /* Note that this must be an instantiation of an alias template
18696 because [temp.names]/6 says:
18697
18698 A template-id that names an alias template specialization
18699 is a type-name.
18700
18701 Whereas [temp.names]/7 says:
18702
18703 A simple-template-id that names a class template
18704 specialization is a class-name.
18705
18706 With concepts, this could also be a partial-concept-id that
18707 declares a non-type template parameter. */
18708 if (type_decl != NULL_TREE
18709 && TREE_CODE (type_decl) == TYPE_DECL
18710 && TYPE_DECL_ALIAS_P (type_decl))
18711 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
18712 else
18713 cp_parser_simulate_error (parser);
18714
18715 if (!cp_parser_parse_definitely (parser))
18716 /* ... Or a typedef-name or an enum-name. */
18717 return cp_parser_nonclass_name (parser);
18718 }
18719
18720 return type_decl;
18721 }
18722
18723 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18724 or a concept-name.
18725
18726 enum-name:
18727 identifier
18728
18729 typedef-name:
18730 identifier
18731
18732 concept-name:
18733 identifier
18734
18735 Returns a TYPE_DECL for the type. */
18736
18737 static tree
18738 cp_parser_nonclass_name (cp_parser* parser)
18739 {
18740 tree type_decl;
18741 tree identifier;
18742
18743 cp_token *token = cp_lexer_peek_token (parser->lexer);
18744 identifier = cp_parser_identifier (parser);
18745 if (identifier == error_mark_node)
18746 return error_mark_node;
18747
18748 /* Look up the type-name. */
18749 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
18750
18751 type_decl = strip_using_decl (type_decl);
18752
18753 if (TREE_CODE (type_decl) != TYPE_DECL
18754 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
18755 {
18756 /* See if this is an Objective-C type. */
18757 tree protos = cp_parser_objc_protocol_refs_opt (parser);
18758 tree type = objc_get_protocol_qualified_type (identifier, protos);
18759 if (type)
18760 type_decl = TYPE_NAME (type);
18761 }
18762
18763 /* Issue an error if we did not find a type-name. */
18764 if (TREE_CODE (type_decl) != TYPE_DECL
18765 /* In Objective-C, we have the complication that class names are
18766 normally type names and start declarations (eg, the
18767 "NSObject" in "NSObject *object;"), but can be used in an
18768 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18769 is an expression. So, a classname followed by a dot is not a
18770 valid type-name. */
18771 || (objc_is_class_name (TREE_TYPE (type_decl))
18772 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
18773 {
18774 if (!cp_parser_simulate_error (parser))
18775 cp_parser_name_lookup_error (parser, identifier, type_decl,
18776 NLE_TYPE, token->location);
18777 return error_mark_node;
18778 }
18779 /* Remember that the name was used in the definition of the
18780 current class so that we can check later to see if the
18781 meaning would have been different after the class was
18782 entirely defined. */
18783 else if (type_decl != error_mark_node
18784 && !parser->scope)
18785 maybe_note_name_used_in_class (identifier, type_decl);
18786
18787 return type_decl;
18788 }
18789
18790 /* Parse an elaborated-type-specifier. Note that the grammar given
18791 here incorporates the resolution to DR68.
18792
18793 elaborated-type-specifier:
18794 class-key :: [opt] nested-name-specifier [opt] identifier
18795 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18796 enum-key :: [opt] nested-name-specifier [opt] identifier
18797 typename :: [opt] nested-name-specifier identifier
18798 typename :: [opt] nested-name-specifier template [opt]
18799 template-id
18800
18801 GNU extension:
18802
18803 elaborated-type-specifier:
18804 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18805 class-key attributes :: [opt] nested-name-specifier [opt]
18806 template [opt] template-id
18807 enum attributes :: [opt] nested-name-specifier [opt] identifier
18808
18809 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18810 declared `friend'. If IS_DECLARATION is TRUE, then this
18811 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18812 something is being declared.
18813
18814 Returns the TYPE specified. */
18815
18816 static tree
18817 cp_parser_elaborated_type_specifier (cp_parser* parser,
18818 bool is_friend,
18819 bool is_declaration)
18820 {
18821 enum tag_types tag_type;
18822 tree identifier;
18823 tree type = NULL_TREE;
18824 tree attributes = NULL_TREE;
18825 tree globalscope;
18826 cp_token *token = NULL;
18827
18828 /* For class and enum types the location of the class-key or enum-key. */
18829 location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
18830 /* For a scoped enum, the 'class' or 'struct' keyword id. */
18831 rid scoped_key = RID_MAX;
18832
18833 /* See if we're looking at the `enum' keyword. */
18834 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
18835 {
18836 /* Consume the `enum' token. */
18837 cp_lexer_consume_token (parser->lexer);
18838 /* Remember that it's an enumeration type. */
18839 tag_type = enum_type;
18840 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18841 enums) is used here. */
18842 cp_token *token = cp_lexer_peek_token (parser->lexer);
18843 if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
18844 || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
18845 {
18846 location_t loc = token->location;
18847 gcc_rich_location richloc (loc);
18848 richloc.add_range (input_location);
18849 richloc.add_fixit_remove ();
18850 pedwarn (&richloc, 0, "elaborated-type-specifier for "
18851 "a scoped enum must not use the %qD keyword",
18852 token->u.value);
18853 /* Consume the `struct' or `class' and parse it anyway. */
18854 cp_lexer_consume_token (parser->lexer);
18855 /* Create a combined location for the whole scoped-enum-key. */
18856 key_loc = make_location (key_loc, key_loc, loc);
18857 }
18858 else
18859 scoped_key = RID_MAX;
18860
18861 /* Parse the attributes. */
18862 attributes = cp_parser_attributes_opt (parser);
18863 }
18864 /* Or, it might be `typename'. */
18865 else if (cp_lexer_next_token_is_keyword (parser->lexer,
18866 RID_TYPENAME))
18867 {
18868 /* Consume the `typename' token. */
18869 cp_lexer_consume_token (parser->lexer);
18870 /* Remember that it's a `typename' type. */
18871 tag_type = typename_type;
18872 }
18873 /* Otherwise it must be a class-key. */
18874 else
18875 {
18876 key_loc = cp_lexer_peek_token (parser->lexer)->location;
18877 tag_type = cp_parser_class_key (parser);
18878 if (tag_type == none_type)
18879 return error_mark_node;
18880 /* Parse the attributes. */
18881 attributes = cp_parser_attributes_opt (parser);
18882 }
18883
18884 /* Look for the `::' operator. */
18885 globalscope = cp_parser_global_scope_opt (parser,
18886 /*current_scope_valid_p=*/false);
18887 /* Look for the nested-name-specifier. */
18888 tree nested_name_specifier;
18889 if (tag_type == typename_type && !globalscope)
18890 {
18891 nested_name_specifier
18892 = cp_parser_nested_name_specifier (parser,
18893 /*typename_keyword_p=*/true,
18894 /*check_dependency_p=*/true,
18895 /*type_p=*/true,
18896 is_declaration);
18897 if (!nested_name_specifier)
18898 return error_mark_node;
18899 }
18900 else
18901 /* Even though `typename' is not present, the proposed resolution
18902 to Core Issue 180 says that in `class A<T>::B', `B' should be
18903 considered a type-name, even if `A<T>' is dependent. */
18904 nested_name_specifier
18905 = cp_parser_nested_name_specifier_opt (parser,
18906 /*typename_keyword_p=*/true,
18907 /*check_dependency_p=*/true,
18908 /*type_p=*/true,
18909 is_declaration);
18910 /* For everything but enumeration types, consider a template-id.
18911 For an enumeration type, consider only a plain identifier. */
18912 if (tag_type != enum_type)
18913 {
18914 bool template_p = false;
18915 tree decl;
18916
18917 /* Allow the `template' keyword. */
18918 template_p = cp_parser_optional_template_keyword (parser);
18919 /* If we didn't see `template', we don't know if there's a
18920 template-id or not. */
18921 if (!template_p)
18922 cp_parser_parse_tentatively (parser);
18923 /* The `template' keyword must follow a nested-name-specifier. */
18924 else if (!nested_name_specifier && !globalscope)
18925 {
18926 cp_parser_error (parser, "%<template%> must follow a nested-"
18927 "name-specifier");
18928 return error_mark_node;
18929 }
18930
18931 /* Parse the template-id. */
18932 token = cp_lexer_peek_token (parser->lexer);
18933 decl = cp_parser_template_id (parser, template_p,
18934 /*check_dependency_p=*/true,
18935 tag_type,
18936 is_declaration);
18937 /* If we didn't find a template-id, look for an ordinary
18938 identifier. */
18939 if (!template_p && !cp_parser_parse_definitely (parser))
18940 ;
18941 /* We can get here when cp_parser_template_id, called by
18942 cp_parser_class_name with tag_type == none_type, succeeds
18943 and caches a BASELINK. Then, when called again here,
18944 instead of failing and returning an error_mark_node
18945 returns it (see template/typename17.C in C++11).
18946 ??? Could we diagnose this earlier? */
18947 else if (tag_type == typename_type && BASELINK_P (decl))
18948 {
18949 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18950 type = error_mark_node;
18951 }
18952 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18953 in effect, then we must assume that, upon instantiation, the
18954 template will correspond to a class. */
18955 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18956 && tag_type == typename_type)
18957 type = make_typename_type (parser->scope, decl,
18958 typename_type,
18959 /*complain=*/tf_error);
18960 /* If the `typename' keyword is in effect and DECL is not a type
18961 decl, then type is non existent. */
18962 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18963 ;
18964 else if (TREE_CODE (decl) == TYPE_DECL)
18965 {
18966 type = check_elaborated_type_specifier (tag_type, decl,
18967 /*allow_template_p=*/true);
18968
18969 /* If the next token is a semicolon, this must be a specialization,
18970 instantiation, or friend declaration. Check the scope while we
18971 still know whether or not we had a nested-name-specifier. */
18972 if (type != error_mark_node
18973 && !nested_name_specifier && !is_friend
18974 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18975 check_unqualified_spec_or_inst (type, token->location);
18976 }
18977 else if (decl == error_mark_node)
18978 type = error_mark_node;
18979 }
18980
18981 if (!type)
18982 {
18983 token = cp_lexer_peek_token (parser->lexer);
18984 identifier = cp_parser_identifier (parser);
18985
18986 if (identifier == error_mark_node)
18987 {
18988 parser->scope = NULL_TREE;
18989 return error_mark_node;
18990 }
18991
18992 /* For a `typename', we needn't call xref_tag. */
18993 if (tag_type == typename_type
18994 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18995 return cp_parser_make_typename_type (parser, identifier,
18996 token->location);
18997
18998 /* Template parameter lists apply only if we are not within a
18999 function parameter list. */
19000 bool template_parm_lists_apply
19001 = parser->num_template_parameter_lists;
19002 if (template_parm_lists_apply)
19003 for (cp_binding_level *s = current_binding_level;
19004 s && s->kind != sk_template_parms;
19005 s = s->level_chain)
19006 if (s->kind == sk_function_parms)
19007 template_parm_lists_apply = false;
19008
19009 /* Look up a qualified name in the usual way. */
19010 if (parser->scope)
19011 {
19012 tree decl;
19013 tree ambiguous_decls;
19014
19015 decl = cp_parser_lookup_name (parser, identifier,
19016 tag_type,
19017 /*is_template=*/false,
19018 /*is_namespace=*/false,
19019 /*check_dependency=*/true,
19020 &ambiguous_decls,
19021 token->location);
19022
19023 /* If the lookup was ambiguous, an error will already have been
19024 issued. */
19025 if (ambiguous_decls)
19026 return error_mark_node;
19027
19028 /* If we are parsing friend declaration, DECL may be a
19029 TEMPLATE_DECL tree node here. However, we need to check
19030 whether this TEMPLATE_DECL results in valid code. Consider
19031 the following example:
19032
19033 namespace N {
19034 template <class T> class C {};
19035 }
19036 class X {
19037 template <class T> friend class N::C; // #1, valid code
19038 };
19039 template <class T> class Y {
19040 friend class N::C; // #2, invalid code
19041 };
19042
19043 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
19044 name lookup of `N::C'. We see that friend declaration must
19045 be template for the code to be valid. Note that
19046 processing_template_decl does not work here since it is
19047 always 1 for the above two cases. */
19048
19049 decl = (cp_parser_maybe_treat_template_as_class
19050 (decl, /*tag_name_p=*/is_friend
19051 && template_parm_lists_apply));
19052
19053 if (TREE_CODE (decl) != TYPE_DECL)
19054 {
19055 cp_parser_diagnose_invalid_type_name (parser,
19056 identifier,
19057 token->location);
19058 return error_mark_node;
19059 }
19060
19061 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
19062 {
19063 bool allow_template = (template_parm_lists_apply
19064 || DECL_SELF_REFERENCE_P (decl));
19065 type = check_elaborated_type_specifier (tag_type, decl,
19066 allow_template);
19067
19068 if (type == error_mark_node)
19069 return error_mark_node;
19070 }
19071
19072 /* Forward declarations of nested types, such as
19073
19074 class C1::C2;
19075 class C1::C2::C3;
19076
19077 are invalid unless all components preceding the final '::'
19078 are complete. If all enclosing types are complete, these
19079 declarations become merely pointless.
19080
19081 Invalid forward declarations of nested types are errors
19082 caught elsewhere in parsing. Those that are pointless arrive
19083 here. */
19084
19085 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19086 && !is_friend && is_declaration
19087 && !processing_explicit_instantiation)
19088 warning (0, "declaration %qD does not declare anything", decl);
19089
19090 type = TREE_TYPE (decl);
19091 }
19092 else
19093 {
19094 /* An elaborated-type-specifier sometimes introduces a new type and
19095 sometimes names an existing type. Normally, the rule is that it
19096 introduces a new type only if there is not an existing type of
19097 the same name already in scope. For example, given:
19098
19099 struct S {};
19100 void f() { struct S s; }
19101
19102 the `struct S' in the body of `f' is the same `struct S' as in
19103 the global scope; the existing definition is used. However, if
19104 there were no global declaration, this would introduce a new
19105 local class named `S'.
19106
19107 An exception to this rule applies to the following code:
19108
19109 namespace N { struct S; }
19110
19111 Here, the elaborated-type-specifier names a new type
19112 unconditionally; even if there is already an `S' in the
19113 containing scope this declaration names a new type.
19114 This exception only applies if the elaborated-type-specifier
19115 forms the complete declaration:
19116
19117 [class.name]
19118
19119 A declaration consisting solely of `class-key identifier ;' is
19120 either a redeclaration of the name in the current scope or a
19121 forward declaration of the identifier as a class name. It
19122 introduces the name into the current scope.
19123
19124 We are in this situation precisely when the next token is a `;'.
19125
19126 An exception to the exception is that a `friend' declaration does
19127 *not* name a new type; i.e., given:
19128
19129 struct S { friend struct T; };
19130
19131 `T' is not a new type in the scope of `S'.
19132
19133 Also, `new struct S' or `sizeof (struct S)' never results in the
19134 definition of a new type; a new type can only be declared in a
19135 declaration context. */
19136
19137 TAG_how how;
19138
19139 if (is_friend)
19140 /* Friends have special name lookup rules. */
19141 how = TAG_how::HIDDEN_FRIEND;
19142 else if (is_declaration
19143 && cp_lexer_next_token_is (parser->lexer,
19144 CPP_SEMICOLON))
19145 /* This is a `class-key identifier ;' */
19146 how = TAG_how::CURRENT_ONLY;
19147 else
19148 how = TAG_how::GLOBAL;
19149
19150 bool template_p =
19151 (template_parm_lists_apply
19152 && (cp_parser_next_token_starts_class_definition_p (parser)
19153 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
19154 /* An unqualified name was used to reference this type, so
19155 there were no qualifying templates. */
19156 if (template_parm_lists_apply
19157 && !cp_parser_check_template_parameters (parser,
19158 /*num_templates=*/0,
19159 /*template_id*/false,
19160 token->location,
19161 /*declarator=*/NULL))
19162 return error_mark_node;
19163
19164 type = xref_tag (tag_type, identifier, how, template_p);
19165 }
19166 }
19167
19168 if (type == error_mark_node)
19169 return error_mark_node;
19170
19171 /* Allow attributes on forward declarations of classes. */
19172 if (attributes)
19173 {
19174 if (TREE_CODE (type) == TYPENAME_TYPE)
19175 warning (OPT_Wattributes,
19176 "attributes ignored on uninstantiated type");
19177 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
19178 && ! processing_explicit_instantiation)
19179 warning (OPT_Wattributes,
19180 "attributes ignored on template instantiation");
19181 else if (is_declaration && cp_parser_declares_only_class_p (parser))
19182 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
19183 else
19184 warning (OPT_Wattributes,
19185 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
19186 }
19187
19188 if (tag_type == enum_type)
19189 cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
19190 else
19191 {
19192 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
19193 for alias definition. */
19194 bool decl_class = (is_declaration
19195 && cp_parser_declares_only_class_p (parser));
19196 cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
19197 decl_class);
19198
19199 /* Indicate whether this class was declared as a `class' or as a
19200 `struct'. */
19201 if (CLASS_TYPE_P (type) && !currently_open_class (type))
19202 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
19203 }
19204
19205 /* A "<" cannot follow an elaborated type specifier. If that
19206 happens, the user was probably trying to form a template-id. */
19207 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
19208 token->location);
19209
19210 return type;
19211 }
19212
19213 /* Parse an enum-specifier.
19214
19215 enum-specifier:
19216 enum-head { enumerator-list [opt] }
19217 enum-head { enumerator-list , } [C++0x]
19218
19219 enum-head:
19220 enum-key identifier [opt] enum-base [opt]
19221 enum-key nested-name-specifier identifier enum-base [opt]
19222
19223 enum-key:
19224 enum
19225 enum class [C++0x]
19226 enum struct [C++0x]
19227
19228 enum-base: [C++0x]
19229 : type-specifier-seq
19230
19231 opaque-enum-specifier:
19232 enum-key identifier enum-base [opt] ;
19233
19234 GNU Extensions:
19235 enum-key attributes[opt] identifier [opt] enum-base [opt]
19236 { enumerator-list [opt] }attributes[opt]
19237 enum-key attributes[opt] identifier [opt] enum-base [opt]
19238 { enumerator-list, }attributes[opt] [C++0x]
19239
19240 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
19241 if the token stream isn't an enum-specifier after all. */
19242
19243 static tree
19244 cp_parser_enum_specifier (cp_parser* parser)
19245 {
19246 tree identifier;
19247 tree type = NULL_TREE;
19248 tree prev_scope;
19249 tree nested_name_specifier = NULL_TREE;
19250 tree attributes;
19251 bool scoped_enum_p = false;
19252 bool has_underlying_type = false;
19253 bool nested_being_defined = false;
19254 bool new_value_list = false;
19255 bool is_new_type = false;
19256 bool is_unnamed = false;
19257 tree underlying_type = NULL_TREE;
19258 cp_token *type_start_token = NULL;
19259 temp_override<bool> cleanup (parser->colon_corrects_to_scope_p, false);
19260
19261 /* Parse tentatively so that we can back up if we don't find a
19262 enum-specifier. */
19263 cp_parser_parse_tentatively (parser);
19264
19265 /* Caller guarantees that the current token is 'enum', an identifier
19266 possibly follows, and the token after that is an opening brace.
19267 If we don't have an identifier, fabricate an anonymous name for
19268 the enumeration being defined. */
19269 cp_lexer_consume_token (parser->lexer);
19270
19271 /* Parse the "class" or "struct", which indicates a scoped
19272 enumeration type in C++0x. */
19273 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
19274 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
19275 {
19276 if (cxx_dialect < cxx11)
19277 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
19278
19279 /* Consume the `struct' or `class' token. */
19280 cp_lexer_consume_token (parser->lexer);
19281
19282 scoped_enum_p = true;
19283 }
19284
19285 attributes = cp_parser_attributes_opt (parser);
19286
19287 /* Clear the qualification. */
19288 parser->scope = NULL_TREE;
19289 parser->qualifying_scope = NULL_TREE;
19290 parser->object_scope = NULL_TREE;
19291
19292 /* Figure out in what scope the declaration is being placed. */
19293 prev_scope = current_scope ();
19294
19295 type_start_token = cp_lexer_peek_token (parser->lexer);
19296
19297 push_deferring_access_checks (dk_no_check);
19298 nested_name_specifier
19299 = cp_parser_nested_name_specifier_opt (parser,
19300 /*typename_keyword_p=*/true,
19301 /*check_dependency_p=*/false,
19302 /*type_p=*/false,
19303 /*is_declaration=*/false);
19304
19305 if (nested_name_specifier)
19306 {
19307 tree name;
19308
19309 identifier = cp_parser_identifier (parser);
19310 name = cp_parser_lookup_name (parser, identifier,
19311 enum_type,
19312 /*is_template=*/false,
19313 /*is_namespace=*/false,
19314 /*check_dependency=*/true,
19315 /*ambiguous_decls=*/NULL,
19316 input_location);
19317 if (name && name != error_mark_node)
19318 {
19319 type = TREE_TYPE (name);
19320 if (TREE_CODE (type) == TYPENAME_TYPE)
19321 {
19322 /* Are template enums allowed in ISO? */
19323 if (template_parm_scope_p ())
19324 pedwarn (type_start_token->location, OPT_Wpedantic,
19325 "%qD is an enumeration template", name);
19326 /* ignore a typename reference, for it will be solved by name
19327 in start_enum. */
19328 type = NULL_TREE;
19329 }
19330 }
19331 else if (nested_name_specifier == error_mark_node)
19332 /* We already issued an error. */;
19333 else
19334 {
19335 error_at (type_start_token->location,
19336 "%qD does not name an enumeration in %qT",
19337 identifier, nested_name_specifier);
19338 nested_name_specifier = error_mark_node;
19339 }
19340 }
19341 else
19342 {
19343 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19344 identifier = cp_parser_identifier (parser);
19345 else
19346 {
19347 identifier = make_anon_name ();
19348 is_unnamed = true;
19349 if (scoped_enum_p)
19350 error_at (type_start_token->location,
19351 "unnamed scoped enum is not allowed");
19352 }
19353 }
19354 pop_deferring_access_checks ();
19355
19356 /* Check for the `:' that denotes a specified underlying type in C++0x.
19357 Note that a ':' could also indicate a bitfield width, however. */
19358 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19359 {
19360 cp_decl_specifier_seq type_specifiers;
19361
19362 /* Consume the `:'. */
19363 cp_lexer_consume_token (parser->lexer);
19364
19365 /* Parse the type-specifier-seq. */
19366 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
19367 /*is_declaration=*/false,
19368 /*is_trailing_return=*/false,
19369 &type_specifiers);
19370
19371 /* At this point this is surely not elaborated type specifier. */
19372 if (!cp_parser_parse_definitely (parser))
19373 return NULL_TREE;
19374
19375 if (cxx_dialect < cxx11)
19376 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
19377
19378 has_underlying_type = true;
19379
19380 /* If that didn't work, stop. */
19381 if (type_specifiers.type != error_mark_node)
19382 {
19383 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
19384 /*initialized=*/0, NULL);
19385 if (underlying_type == error_mark_node
19386 || check_for_bare_parameter_packs (underlying_type))
19387 underlying_type = NULL_TREE;
19388 }
19389 }
19390
19391 /* Look for the `{' but don't consume it yet. */
19392 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19393 {
19394 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
19395 {
19396 if (has_underlying_type)
19397 cp_parser_commit_to_tentative_parse (parser);
19398 cp_parser_error (parser, "expected %<{%>");
19399 if (has_underlying_type)
19400 return error_mark_node;
19401 }
19402 /* An opaque-enum-specifier must have a ';' here. */
19403 if ((scoped_enum_p || underlying_type)
19404 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19405 {
19406 if (has_underlying_type)
19407 cp_parser_commit_to_tentative_parse (parser);
19408 cp_parser_error (parser, "expected %<;%> or %<{%>");
19409 if (has_underlying_type)
19410 return error_mark_node;
19411 }
19412 }
19413
19414 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
19415 return NULL_TREE;
19416
19417 if (nested_name_specifier)
19418 {
19419 if (CLASS_TYPE_P (nested_name_specifier))
19420 {
19421 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
19422 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
19423 push_scope (nested_name_specifier);
19424 }
19425 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
19426 push_nested_namespace (nested_name_specifier);
19427 }
19428
19429 /* Issue an error message if type-definitions are forbidden here. */
19430 if (!cp_parser_check_type_definition (parser))
19431 type = error_mark_node;
19432 else
19433 /* Create the new type. We do this before consuming the opening
19434 brace so the enum will be recorded as being on the line of its
19435 tag (or the 'enum' keyword, if there is no tag). */
19436 type = start_enum (identifier, type, underlying_type,
19437 attributes, scoped_enum_p, &is_new_type);
19438
19439 /* If the next token is not '{' it is an opaque-enum-specifier or an
19440 elaborated-type-specifier. */
19441 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19442 {
19443 timevar_push (TV_PARSE_ENUM);
19444 if (nested_name_specifier
19445 && nested_name_specifier != error_mark_node)
19446 {
19447 /* The following catches invalid code such as:
19448 enum class S<int>::E { A, B, C }; */
19449 if (!processing_specialization
19450 && CLASS_TYPE_P (nested_name_specifier)
19451 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
19452 error_at (type_start_token->location, "cannot add an enumerator "
19453 "list to a template instantiation");
19454
19455 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
19456 {
19457 error_at (type_start_token->location,
19458 "%<%T::%E%> has not been declared",
19459 TYPE_CONTEXT (nested_name_specifier),
19460 nested_name_specifier);
19461 type = error_mark_node;
19462 }
19463 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
19464 && !CLASS_TYPE_P (nested_name_specifier))
19465 {
19466 error_at (type_start_token->location, "nested name specifier "
19467 "%qT for enum declaration does not name a class "
19468 "or namespace", nested_name_specifier);
19469 type = error_mark_node;
19470 }
19471 /* If that scope does not contain the scope in which the
19472 class was originally declared, the program is invalid. */
19473 else if (prev_scope && !is_ancestor (prev_scope,
19474 nested_name_specifier))
19475 {
19476 if (at_namespace_scope_p ())
19477 error_at (type_start_token->location,
19478 "declaration of %qD in namespace %qD which does not "
19479 "enclose %qD",
19480 type, prev_scope, nested_name_specifier);
19481 else
19482 error_at (type_start_token->location,
19483 "declaration of %qD in %qD which does not "
19484 "enclose %qD",
19485 type, prev_scope, nested_name_specifier);
19486 type = error_mark_node;
19487 }
19488 /* If that scope is the scope where the declaration is being placed
19489 the program is invalid. */
19490 else if (CLASS_TYPE_P (nested_name_specifier)
19491 && CLASS_TYPE_P (prev_scope)
19492 && same_type_p (nested_name_specifier, prev_scope))
19493 {
19494 permerror (type_start_token->location,
19495 "extra qualification not allowed");
19496 nested_name_specifier = NULL_TREE;
19497 }
19498 }
19499
19500 if (scoped_enum_p)
19501 begin_scope (sk_scoped_enum, type);
19502
19503 /* Consume the opening brace. */
19504 matching_braces braces;
19505 braces.consume_open (parser);
19506
19507 if (type == error_mark_node)
19508 ; /* Nothing to add */
19509 else if (OPAQUE_ENUM_P (type)
19510 || (cxx_dialect > cxx98 && processing_specialization))
19511 {
19512 new_value_list = true;
19513 SET_OPAQUE_ENUM_P (type, false);
19514 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
19515 }
19516 else
19517 {
19518 error_at (type_start_token->location,
19519 "multiple definition of %q#T", type);
19520 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
19521 "previous definition here");
19522 type = error_mark_node;
19523 }
19524
19525 if (type == error_mark_node)
19526 cp_parser_skip_to_end_of_block_or_statement (parser);
19527 /* If the next token is not '}', then there are some enumerators. */
19528 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19529 {
19530 if (is_unnamed && !scoped_enum_p)
19531 pedwarn (type_start_token->location, OPT_Wpedantic,
19532 "ISO C++ forbids empty unnamed enum");
19533 }
19534 else
19535 {
19536 /* We've seen a '{' so we know we're in an enum-specifier.
19537 Commit to any tentative parse to get syntax errors. */
19538 cp_parser_commit_to_tentative_parse (parser);
19539 cp_parser_enumerator_list (parser, type);
19540 }
19541
19542 /* Consume the final '}'. */
19543 braces.require_close (parser);
19544
19545 if (scoped_enum_p)
19546 finish_scope ();
19547 timevar_pop (TV_PARSE_ENUM);
19548 }
19549 else
19550 {
19551 /* If a ';' follows, then it is an opaque-enum-specifier
19552 and additional restrictions apply. */
19553 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19554 {
19555 if (is_unnamed)
19556 error_at (type_start_token->location,
19557 "opaque-enum-specifier without name");
19558 else if (nested_name_specifier)
19559 error_at (type_start_token->location,
19560 "opaque-enum-specifier must use a simple identifier");
19561 }
19562 }
19563
19564 /* Look for trailing attributes to apply to this enumeration, and
19565 apply them if appropriate. */
19566 if (cp_parser_allow_gnu_extensions_p (parser))
19567 {
19568 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
19569 cplus_decl_attributes (&type,
19570 trailing_attr,
19571 (int) ATTR_FLAG_TYPE_IN_PLACE);
19572 }
19573
19574 /* Finish up the enumeration. */
19575 if (type != error_mark_node)
19576 {
19577 if (new_value_list)
19578 finish_enum_value_list (type);
19579 if (is_new_type)
19580 finish_enum (type);
19581 }
19582
19583 if (nested_name_specifier)
19584 {
19585 if (CLASS_TYPE_P (nested_name_specifier))
19586 {
19587 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
19588 pop_scope (nested_name_specifier);
19589 }
19590 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
19591 pop_nested_namespace (nested_name_specifier);
19592 }
19593 return type;
19594 }
19595
19596 /* Parse an enumerator-list. The enumerators all have the indicated
19597 TYPE.
19598
19599 enumerator-list:
19600 enumerator-definition
19601 enumerator-list , enumerator-definition */
19602
19603 static void
19604 cp_parser_enumerator_list (cp_parser* parser, tree type)
19605 {
19606 while (true)
19607 {
19608 /* Parse an enumerator-definition. */
19609 cp_parser_enumerator_definition (parser, type);
19610
19611 /* If the next token is not a ',', we've reached the end of
19612 the list. */
19613 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19614 break;
19615 /* Otherwise, consume the `,' and keep going. */
19616 cp_lexer_consume_token (parser->lexer);
19617 /* If the next token is a `}', there is a trailing comma. */
19618 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19619 {
19620 if (cxx_dialect < cxx11)
19621 pedwarn (input_location, OPT_Wpedantic,
19622 "comma at end of enumerator list");
19623 break;
19624 }
19625 }
19626 }
19627
19628 /* Parse an enumerator-definition. The enumerator has the indicated
19629 TYPE.
19630
19631 enumerator-definition:
19632 enumerator
19633 enumerator = constant-expression
19634
19635 enumerator:
19636 identifier
19637
19638 GNU Extensions:
19639
19640 enumerator-definition:
19641 enumerator attributes [opt]
19642 enumerator attributes [opt] = constant-expression */
19643
19644 static void
19645 cp_parser_enumerator_definition (cp_parser* parser, tree type)
19646 {
19647 tree identifier;
19648 tree value;
19649 location_t loc;
19650
19651 /* Save the input location because we are interested in the location
19652 of the identifier and not the location of the explicit value. */
19653 loc = cp_lexer_peek_token (parser->lexer)->location;
19654
19655 /* Look for the identifier. */
19656 identifier = cp_parser_identifier (parser);
19657 if (identifier == error_mark_node)
19658 return;
19659
19660 /* Parse any specified attributes. */
19661 tree attrs = cp_parser_attributes_opt (parser);
19662
19663 /* If the next token is an '=', then there is an explicit value. */
19664 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19665 {
19666 /* Consume the `=' token. */
19667 cp_lexer_consume_token (parser->lexer);
19668 /* Parse the value. */
19669 value = cp_parser_constant_expression (parser);
19670 }
19671 else
19672 value = NULL_TREE;
19673
19674 /* If we are processing a template, make sure the initializer of the
19675 enumerator doesn't contain any bare template parameter pack. */
19676 if (check_for_bare_parameter_packs (value))
19677 value = error_mark_node;
19678
19679 /* Create the enumerator. */
19680 build_enumerator (identifier, value, type, attrs, loc);
19681 }
19682
19683 /* Parse a namespace-name.
19684
19685 namespace-name:
19686 original-namespace-name
19687 namespace-alias
19688
19689 Returns the NAMESPACE_DECL for the namespace. */
19690
19691 static tree
19692 cp_parser_namespace_name (cp_parser* parser)
19693 {
19694 tree identifier;
19695 tree namespace_decl;
19696
19697 cp_token *token = cp_lexer_peek_token (parser->lexer);
19698
19699 /* Get the name of the namespace. */
19700 identifier = cp_parser_identifier (parser);
19701 if (identifier == error_mark_node)
19702 return error_mark_node;
19703
19704 /* Look up the identifier in the currently active scope. Look only
19705 for namespaces, due to:
19706
19707 [basic.lookup.udir]
19708
19709 When looking up a namespace-name in a using-directive or alias
19710 definition, only namespace names are considered.
19711
19712 And:
19713
19714 [basic.lookup.qual]
19715
19716 During the lookup of a name preceding the :: scope resolution
19717 operator, object, function, and enumerator names are ignored.
19718
19719 (Note that cp_parser_qualifying_entity only calls this
19720 function if the token after the name is the scope resolution
19721 operator.) */
19722 namespace_decl = cp_parser_lookup_name (parser, identifier,
19723 none_type,
19724 /*is_template=*/false,
19725 /*is_namespace=*/true,
19726 /*check_dependency=*/true,
19727 /*ambiguous_decls=*/NULL,
19728 token->location);
19729 /* If it's not a namespace, issue an error. */
19730 if (namespace_decl == error_mark_node
19731 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
19732 {
19733 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19734 {
19735 auto_diagnostic_group d;
19736 name_hint hint;
19737 if (namespace_decl == error_mark_node
19738 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
19739 hint = suggest_alternative_in_explicit_scope (token->location,
19740 identifier,
19741 parser->scope);
19742 if (const char *suggestion = hint.suggestion ())
19743 {
19744 gcc_rich_location richloc (token->location);
19745 richloc.add_fixit_replace (suggestion);
19746 error_at (&richloc,
19747 "%qD is not a namespace-name; did you mean %qs?",
19748 identifier, suggestion);
19749 }
19750 else
19751 error_at (token->location, "%qD is not a namespace-name",
19752 identifier);
19753 }
19754 else
19755 cp_parser_error (parser, "expected namespace-name");
19756 namespace_decl = error_mark_node;
19757 }
19758
19759 return namespace_decl;
19760 }
19761
19762 /* Parse a namespace-definition.
19763
19764 namespace-definition:
19765 named-namespace-definition
19766 unnamed-namespace-definition
19767
19768 named-namespace-definition:
19769 original-namespace-definition
19770 extension-namespace-definition
19771
19772 original-namespace-definition:
19773 namespace identifier { namespace-body }
19774
19775 extension-namespace-definition:
19776 namespace original-namespace-name { namespace-body }
19777
19778 unnamed-namespace-definition:
19779 namespace { namespace-body } */
19780
19781 static void
19782 cp_parser_namespace_definition (cp_parser* parser)
19783 {
19784 tree identifier;
19785 int nested_definition_count = 0;
19786
19787 cp_ensure_no_omp_declare_simd (parser);
19788 cp_ensure_no_oacc_routine (parser);
19789
19790 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
19791 const bool topmost_inline_p = is_inline;
19792
19793 if (is_inline)
19794 {
19795 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
19796 cp_lexer_consume_token (parser->lexer);
19797 }
19798
19799 /* Look for the `namespace' keyword. */
19800 cp_token* token
19801 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19802
19803 /* Parse any specified attributes before the identifier. */
19804 tree attribs = cp_parser_attributes_opt (parser);
19805
19806 for (;;)
19807 {
19808 identifier = NULL_TREE;
19809
19810 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
19811 RID_INLINE);
19812 if (nested_inline_p && nested_definition_count != 0)
19813 {
19814 if (cxx_dialect < cxx20)
19815 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
19816 OPT_Wpedantic, "nested inline namespace definitions only "
19817 "available with %<-std=c++20%> or %<-std=gnu++20%>");
19818 cp_lexer_consume_token (parser->lexer);
19819 }
19820
19821 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19822 {
19823 identifier = cp_parser_identifier (parser);
19824
19825 if (cp_next_tokens_can_be_std_attribute_p (parser))
19826 pedwarn (input_location, OPT_Wpedantic,
19827 "standard attributes on namespaces must precede "
19828 "the namespace name");
19829
19830 /* Parse any attributes specified after the identifier. */
19831 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
19832 }
19833
19834 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19835 {
19836 /* Don't forget that the innermost namespace might have been
19837 marked as inline. Use |= because we cannot overwrite
19838 IS_INLINE in case the outermost namespace is inline, but
19839 there are no nested inlines. */
19840 is_inline |= nested_inline_p;
19841 break;
19842 }
19843
19844 if (!nested_definition_count && cxx_dialect < cxx17)
19845 pedwarn (input_location, OPT_Wpedantic,
19846 "nested namespace definitions only available with "
19847 "%<-std=c++17%> or %<-std=gnu++17%>");
19848
19849 /* Nested namespace names can create new namespaces (unlike
19850 other qualified-ids). */
19851 if (int count = (identifier
19852 ? push_namespace (identifier, nested_inline_p)
19853 : 0))
19854 nested_definition_count += count;
19855 else
19856 cp_parser_error (parser, "nested namespace name required");
19857 cp_lexer_consume_token (parser->lexer);
19858 }
19859
19860 if (nested_definition_count && !identifier)
19861 cp_parser_error (parser, "namespace name required");
19862
19863 if (nested_definition_count && attribs)
19864 error_at (token->location,
19865 "a nested namespace definition cannot have attributes");
19866 if (nested_definition_count && topmost_inline_p)
19867 error_at (token->location,
19868 "a nested namespace definition cannot be inline");
19869
19870 /* Start the namespace. */
19871 nested_definition_count += push_namespace (identifier, is_inline);
19872
19873 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
19874
19875 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
19876
19877 /* Look for the `{' to validate starting the namespace. */
19878 matching_braces braces;
19879 if (braces.require_open (parser))
19880 {
19881 /* Parse the body of the namespace. */
19882 cp_parser_namespace_body (parser);
19883
19884 /* Look for the final `}'. */
19885 braces.require_close (parser);
19886 }
19887
19888 if (has_visibility)
19889 pop_visibility (1);
19890
19891 /* Pop the nested namespace definitions. */
19892 while (nested_definition_count--)
19893 pop_namespace ();
19894 }
19895
19896 /* Parse a namespace-body.
19897
19898 namespace-body:
19899 declaration-seq [opt] */
19900
19901 static void
19902 cp_parser_namespace_body (cp_parser* parser)
19903 {
19904 cp_parser_declaration_seq_opt (parser);
19905 }
19906
19907 /* Parse a namespace-alias-definition.
19908
19909 namespace-alias-definition:
19910 namespace identifier = qualified-namespace-specifier ; */
19911
19912 static void
19913 cp_parser_namespace_alias_definition (cp_parser* parser)
19914 {
19915 tree identifier;
19916 tree namespace_specifier;
19917
19918 cp_token *token = cp_lexer_peek_token (parser->lexer);
19919
19920 /* Look for the `namespace' keyword. */
19921 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19922 /* Look for the identifier. */
19923 identifier = cp_parser_identifier (parser);
19924 if (identifier == error_mark_node)
19925 return;
19926 /* Look for the `=' token. */
19927 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
19928 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19929 {
19930 error_at (token->location, "%<namespace%> definition is not allowed here");
19931 /* Skip the definition. */
19932 cp_lexer_consume_token (parser->lexer);
19933 if (cp_parser_skip_to_closing_brace (parser))
19934 cp_lexer_consume_token (parser->lexer);
19935 return;
19936 }
19937 cp_parser_require (parser, CPP_EQ, RT_EQ);
19938 /* Look for the qualified-namespace-specifier. */
19939 namespace_specifier
19940 = cp_parser_qualified_namespace_specifier (parser);
19941 cp_warn_deprecated_use_scopes (namespace_specifier);
19942 /* Look for the `;' token. */
19943 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19944
19945 /* Register the alias in the symbol table. */
19946 do_namespace_alias (identifier, namespace_specifier);
19947 }
19948
19949 /* Parse a qualified-namespace-specifier.
19950
19951 qualified-namespace-specifier:
19952 :: [opt] nested-name-specifier [opt] namespace-name
19953
19954 Returns a NAMESPACE_DECL corresponding to the specified
19955 namespace. */
19956
19957 static tree
19958 cp_parser_qualified_namespace_specifier (cp_parser* parser)
19959 {
19960 /* Look for the optional `::'. */
19961 cp_parser_global_scope_opt (parser,
19962 /*current_scope_valid_p=*/false);
19963
19964 /* Look for the optional nested-name-specifier. */
19965 cp_parser_nested_name_specifier_opt (parser,
19966 /*typename_keyword_p=*/false,
19967 /*check_dependency_p=*/true,
19968 /*type_p=*/false,
19969 /*is_declaration=*/true);
19970
19971 return cp_parser_namespace_name (parser);
19972 }
19973
19974 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19975 access declaration.
19976
19977 using-declaration:
19978 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19979 using :: unqualified-id ;
19980
19981 access-declaration:
19982 qualified-id ;
19983
19984 */
19985
19986 static bool
19987 cp_parser_using_declaration (cp_parser* parser,
19988 bool access_declaration_p)
19989 {
19990 cp_token *token;
19991 bool typename_p = false;
19992 bool global_scope_p;
19993 tree decl;
19994 tree identifier;
19995 tree qscope;
19996 int oldcount = errorcount;
19997 cp_token *diag_token = NULL;
19998
19999 if (access_declaration_p)
20000 {
20001 diag_token = cp_lexer_peek_token (parser->lexer);
20002 cp_parser_parse_tentatively (parser);
20003 }
20004 else
20005 {
20006 /* Look for the `using' keyword. */
20007 cp_parser_require_keyword (parser, RID_USING, RT_USING);
20008
20009 again:
20010 /* Peek at the next token. */
20011 token = cp_lexer_peek_token (parser->lexer);
20012 /* See if it's `typename'. */
20013 if (token->keyword == RID_TYPENAME)
20014 {
20015 /* Remember that we've seen it. */
20016 typename_p = true;
20017 /* Consume the `typename' token. */
20018 cp_lexer_consume_token (parser->lexer);
20019 }
20020 }
20021
20022 /* Look for the optional global scope qualification. */
20023 global_scope_p
20024 = (cp_parser_global_scope_opt (parser,
20025 /*current_scope_valid_p=*/false)
20026 != NULL_TREE);
20027
20028 /* If we saw `typename', or didn't see `::', then there must be a
20029 nested-name-specifier present. */
20030 if (typename_p || !global_scope_p)
20031 {
20032 qscope = cp_parser_nested_name_specifier (parser, typename_p,
20033 /*check_dependency_p=*/true,
20034 /*type_p=*/false,
20035 /*is_declaration=*/true);
20036 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
20037 {
20038 cp_parser_skip_to_end_of_block_or_statement (parser);
20039 return false;
20040 }
20041 }
20042 /* Otherwise, we could be in either of the two productions. In that
20043 case, treat the nested-name-specifier as optional. */
20044 else
20045 qscope = cp_parser_nested_name_specifier_opt (parser,
20046 /*typename_keyword_p=*/false,
20047 /*check_dependency_p=*/true,
20048 /*type_p=*/false,
20049 /*is_declaration=*/true);
20050 if (!qscope)
20051 qscope = global_namespace;
20052 else if (UNSCOPED_ENUM_P (qscope)
20053 && !TYPE_FUNCTION_SCOPE_P (qscope))
20054 qscope = CP_TYPE_CONTEXT (qscope);
20055
20056 cp_warn_deprecated_use_scopes (qscope);
20057
20058 if (access_declaration_p && cp_parser_error_occurred (parser))
20059 /* Something has already gone wrong; there's no need to parse
20060 further. Since an error has occurred, the return value of
20061 cp_parser_parse_definitely will be false, as required. */
20062 return cp_parser_parse_definitely (parser);
20063
20064 token = cp_lexer_peek_token (parser->lexer);
20065 /* Parse the unqualified-id. */
20066 identifier = cp_parser_unqualified_id (parser,
20067 /*template_keyword_p=*/false,
20068 /*check_dependency_p=*/true,
20069 /*declarator_p=*/true,
20070 /*optional_p=*/false);
20071
20072 if (access_declaration_p)
20073 {
20074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20075 cp_parser_simulate_error (parser);
20076 if (!cp_parser_parse_definitely (parser))
20077 return false;
20078 }
20079 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20080 {
20081 cp_token *ell = cp_lexer_consume_token (parser->lexer);
20082 if (cxx_dialect < cxx17)
20083 pedwarn (ell->location, 0,
20084 "pack expansion in using-declaration only available "
20085 "with %<-std=c++17%> or %<-std=gnu++17%>");
20086 qscope = make_pack_expansion (qscope);
20087 }
20088
20089 /* The function we call to handle a using-declaration is different
20090 depending on what scope we are in. */
20091 if (qscope == error_mark_node || identifier == error_mark_node)
20092 ;
20093 else if (!identifier_p (identifier)
20094 && TREE_CODE (identifier) != BIT_NOT_EXPR)
20095 /* [namespace.udecl]
20096
20097 A using declaration shall not name a template-id. */
20098 error_at (token->location,
20099 "a template-id may not appear in a using-declaration");
20100 else
20101 {
20102 if (at_class_scope_p ())
20103 {
20104 /* Create the USING_DECL. */
20105 decl = do_class_using_decl (qscope, identifier);
20106
20107 if (decl && typename_p)
20108 USING_DECL_TYPENAME_P (decl) = 1;
20109
20110 if (check_for_bare_parameter_packs (decl))
20111 {
20112 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20113 return false;
20114 }
20115 else
20116 /* Add it to the list of members in this class. */
20117 finish_member_declaration (decl);
20118 }
20119 else
20120 finish_nonmember_using_decl (qscope, identifier);
20121 }
20122
20123 if (!access_declaration_p
20124 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20125 {
20126 cp_token *comma = cp_lexer_consume_token (parser->lexer);
20127 if (cxx_dialect < cxx17)
20128 pedwarn (comma->location, 0,
20129 "comma-separated list in using-declaration only available "
20130 "with %<-std=c++17%> or %<-std=gnu++17%>");
20131 goto again;
20132 }
20133
20134 /* Look for the final `;'. */
20135 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20136
20137 if (access_declaration_p && errorcount == oldcount)
20138 warning_at (diag_token->location, OPT_Wdeprecated,
20139 "access declarations are deprecated "
20140 "in favour of using-declarations; "
20141 "suggestion: add the %<using%> keyword");
20142
20143 return true;
20144 }
20145
20146 /* Parse an alias-declaration.
20147
20148 alias-declaration:
20149 using identifier attribute-specifier-seq [opt] = type-id */
20150
20151 static tree
20152 cp_parser_alias_declaration (cp_parser* parser)
20153 {
20154 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
20155 location_t id_location, type_location;
20156 cp_declarator *declarator;
20157 cp_decl_specifier_seq decl_specs;
20158 bool member_p;
20159 const char *saved_message = NULL;
20160
20161 /* Look for the `using' keyword. */
20162 cp_token *using_token
20163 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
20164 if (using_token == NULL)
20165 return error_mark_node;
20166
20167 id_location = cp_lexer_peek_token (parser->lexer)->location;
20168 id = cp_parser_identifier (parser);
20169 if (id == error_mark_node)
20170 return error_mark_node;
20171
20172 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
20173 attributes = cp_parser_attributes_opt (parser);
20174 if (attributes == error_mark_node)
20175 return error_mark_node;
20176
20177 cp_parser_require (parser, CPP_EQ, RT_EQ);
20178
20179 if (cp_parser_error_occurred (parser))
20180 return error_mark_node;
20181
20182 cp_parser_commit_to_tentative_parse (parser);
20183
20184 /* Now we are going to parse the type-id of the declaration. */
20185
20186 /*
20187 [dcl.type]/3 says:
20188
20189 "A type-specifier-seq shall not define a class or enumeration
20190 unless it appears in the type-id of an alias-declaration (7.1.3) that
20191 is not the declaration of a template-declaration."
20192
20193 In other words, if we currently are in an alias template, the
20194 type-id should not define a type.
20195
20196 So let's set parser->type_definition_forbidden_message in that
20197 case; cp_parser_check_type_definition (called by
20198 cp_parser_class_specifier) will then emit an error if a type is
20199 defined in the type-id. */
20200 if (parser->num_template_parameter_lists)
20201 {
20202 saved_message = parser->type_definition_forbidden_message;
20203 parser->type_definition_forbidden_message =
20204 G_("types may not be defined in alias template declarations");
20205 }
20206
20207 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
20208 &type_location);
20209
20210 /* Restore the error message if need be. */
20211 if (parser->num_template_parameter_lists)
20212 parser->type_definition_forbidden_message = saved_message;
20213
20214 if (type == error_mark_node
20215 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
20216 {
20217 cp_parser_skip_to_end_of_block_or_statement (parser);
20218 return error_mark_node;
20219 }
20220
20221 /* A typedef-name can also be introduced by an alias-declaration. The
20222 identifier following the using keyword becomes a typedef-name. It has
20223 the same semantics as if it were introduced by the typedef
20224 specifier. In particular, it does not define a new type and it shall
20225 not appear in the type-id. */
20226
20227 clear_decl_specs (&decl_specs);
20228 decl_specs.type = type;
20229 if (attributes != NULL_TREE)
20230 {
20231 decl_specs.attributes = attributes;
20232 set_and_check_decl_spec_loc (&decl_specs,
20233 ds_attribute,
20234 attrs_token);
20235 }
20236 set_and_check_decl_spec_loc (&decl_specs,
20237 ds_typedef,
20238 using_token);
20239 set_and_check_decl_spec_loc (&decl_specs,
20240 ds_alias,
20241 using_token);
20242 decl_specs.locations[ds_type_spec] = type_location;
20243
20244 if (parser->num_template_parameter_lists
20245 && !cp_parser_check_template_parameters (parser,
20246 /*num_templates=*/0,
20247 /*template_id*/false,
20248 id_location,
20249 /*declarator=*/NULL))
20250 return error_mark_node;
20251
20252 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
20253
20254 member_p = at_class_scope_p ();
20255 if (member_p)
20256 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
20257 NULL_TREE, attributes);
20258 else
20259 decl = start_decl (declarator, &decl_specs, 0,
20260 attributes, NULL_TREE, &pushed_scope);
20261 if (decl == error_mark_node)
20262 return decl;
20263
20264 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
20265
20266 if (pushed_scope)
20267 pop_scope (pushed_scope);
20268
20269 /* If decl is a template, return its TEMPLATE_DECL so that it gets
20270 added into the symbol table; otherwise, return the TYPE_DECL. */
20271 if (DECL_LANG_SPECIFIC (decl)
20272 && DECL_TEMPLATE_INFO (decl)
20273 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
20274 {
20275 decl = DECL_TI_TEMPLATE (decl);
20276 if (member_p)
20277 check_member_template (decl);
20278 }
20279
20280 return decl;
20281 }
20282
20283 /* Parse a using-directive.
20284
20285 using-directive:
20286 using namespace :: [opt] nested-name-specifier [opt]
20287 namespace-name ; */
20288
20289 static void
20290 cp_parser_using_directive (cp_parser* parser)
20291 {
20292 tree namespace_decl;
20293 tree attribs;
20294
20295 /* Look for the `using' keyword. */
20296 cp_parser_require_keyword (parser, RID_USING, RT_USING);
20297 /* And the `namespace' keyword. */
20298 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
20299 /* Look for the optional `::' operator. */
20300 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
20301 /* And the optional nested-name-specifier. */
20302 cp_parser_nested_name_specifier_opt (parser,
20303 /*typename_keyword_p=*/false,
20304 /*check_dependency_p=*/true,
20305 /*type_p=*/false,
20306 /*is_declaration=*/true);
20307 /* Get the namespace being used. */
20308 namespace_decl = cp_parser_namespace_name (parser);
20309 cp_warn_deprecated_use_scopes (namespace_decl);
20310 /* And any specified attributes. */
20311 attribs = cp_parser_attributes_opt (parser);
20312
20313 /* Update the symbol table. */
20314 finish_using_directive (namespace_decl, attribs);
20315
20316 /* Look for the final `;'. */
20317 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20318 }
20319
20320 /* Parse an asm-definition.
20321
20322 asm-qualifier:
20323 volatile
20324 inline
20325 goto
20326
20327 asm-qualifier-list:
20328 asm-qualifier
20329 asm-qualifier-list asm-qualifier
20330
20331 asm-definition:
20332 asm ( string-literal ) ;
20333
20334 GNU Extension:
20335
20336 asm-definition:
20337 asm asm-qualifier-list [opt] ( string-literal ) ;
20338 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
20339 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
20340 : asm-operand-list [opt] ) ;
20341 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
20342 : asm-operand-list [opt]
20343 : asm-clobber-list [opt] ) ;
20344 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
20345 : asm-clobber-list [opt]
20346 : asm-goto-list ) ;
20347
20348 The form with asm-goto-list is valid if and only if the asm-qualifier-list
20349 contains goto, and is the only allowed form in that case. No duplicates are
20350 allowed in an asm-qualifier-list. */
20351
20352 static void
20353 cp_parser_asm_definition (cp_parser* parser)
20354 {
20355 tree string;
20356 tree outputs = NULL_TREE;
20357 tree inputs = NULL_TREE;
20358 tree clobbers = NULL_TREE;
20359 tree labels = NULL_TREE;
20360 tree asm_stmt;
20361 bool extended_p = false;
20362 bool invalid_inputs_p = false;
20363 bool invalid_outputs_p = false;
20364 required_token missing = RT_NONE;
20365 location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
20366
20367 /* Look for the `asm' keyword. */
20368 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
20369
20370 /* In C++20, unevaluated inline assembly is permitted in constexpr
20371 functions. */
20372 if (parser->in_function_body
20373 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
20374 && (cxx_dialect < cxx20))
20375 pedwarn (asm_loc, 0, "%<asm%> in %<constexpr%> function only available "
20376 "with %<-std=c++20%> or %<-std=gnu++20%>");
20377
20378 /* Handle the asm-qualifier-list. */
20379 location_t volatile_loc = UNKNOWN_LOCATION;
20380 location_t inline_loc = UNKNOWN_LOCATION;
20381 location_t goto_loc = UNKNOWN_LOCATION;
20382 location_t first_loc = UNKNOWN_LOCATION;
20383
20384 if (cp_parser_allow_gnu_extensions_p (parser))
20385 for (;;)
20386 {
20387 cp_token *token = cp_lexer_peek_token (parser->lexer);
20388 location_t loc = token->location;
20389 switch (cp_lexer_peek_token (parser->lexer)->keyword)
20390 {
20391 case RID_VOLATILE:
20392 if (volatile_loc)
20393 {
20394 error_at (loc, "duplicate %<asm%> qualifier %qT",
20395 token->u.value);
20396 inform (volatile_loc, "first seen here");
20397 }
20398 else
20399 {
20400 if (!parser->in_function_body)
20401 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
20402 "outside of function body", token->u.value);
20403 volatile_loc = loc;
20404 }
20405 cp_lexer_consume_token (parser->lexer);
20406 continue;
20407
20408 case RID_INLINE:
20409 if (inline_loc)
20410 {
20411 error_at (loc, "duplicate %<asm%> qualifier %qT",
20412 token->u.value);
20413 inform (inline_loc, "first seen here");
20414 }
20415 else
20416 inline_loc = loc;
20417 if (!first_loc)
20418 first_loc = loc;
20419 cp_lexer_consume_token (parser->lexer);
20420 continue;
20421
20422 case RID_GOTO:
20423 if (goto_loc)
20424 {
20425 error_at (loc, "duplicate %<asm%> qualifier %qT",
20426 token->u.value);
20427 inform (goto_loc, "first seen here");
20428 }
20429 else
20430 goto_loc = loc;
20431 if (!first_loc)
20432 first_loc = loc;
20433 cp_lexer_consume_token (parser->lexer);
20434 continue;
20435
20436 case RID_CONST:
20437 case RID_RESTRICT:
20438 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
20439 cp_lexer_consume_token (parser->lexer);
20440 continue;
20441
20442 default:
20443 break;
20444 }
20445 break;
20446 }
20447
20448 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
20449 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
20450 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
20451
20452 if (!parser->in_function_body && (inline_p || goto_p))
20453 {
20454 error_at (first_loc, "%<asm%> qualifier outside of function body");
20455 inline_p = goto_p = false;
20456 }
20457
20458 /* Look for the opening `('. */
20459 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20460 return;
20461 /* Look for the string. */
20462 string = cp_parser_string_literal (parser, false, false);
20463 if (string == error_mark_node)
20464 {
20465 cp_parser_skip_to_closing_parenthesis (parser, true, false,
20466 /*consume_paren=*/true);
20467 return;
20468 }
20469
20470 /* If we're allowing GNU extensions, check for the extended assembly
20471 syntax. Unfortunately, the `:' tokens need not be separated by
20472 a space in C, and so, for compatibility, we tolerate that here
20473 too. Doing that means that we have to treat the `::' operator as
20474 two `:' tokens. */
20475 if (cp_parser_allow_gnu_extensions_p (parser)
20476 && parser->in_function_body
20477 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
20478 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
20479 {
20480 bool inputs_p = false;
20481 bool clobbers_p = false;
20482 bool labels_p = false;
20483
20484 /* The extended syntax was used. */
20485 extended_p = true;
20486
20487 /* Look for outputs. */
20488 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20489 {
20490 /* Consume the `:'. */
20491 cp_lexer_consume_token (parser->lexer);
20492 /* Parse the output-operands. */
20493 if (cp_lexer_next_token_is_not (parser->lexer,
20494 CPP_COLON)
20495 && cp_lexer_next_token_is_not (parser->lexer,
20496 CPP_SCOPE)
20497 && cp_lexer_next_token_is_not (parser->lexer,
20498 CPP_CLOSE_PAREN)
20499 && !goto_p)
20500 {
20501 outputs = cp_parser_asm_operand_list (parser);
20502 if (outputs == error_mark_node)
20503 invalid_outputs_p = true;
20504 }
20505 }
20506 /* If the next token is `::', there are no outputs, and the
20507 next token is the beginning of the inputs. */
20508 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20509 /* The inputs are coming next. */
20510 inputs_p = true;
20511
20512 /* Look for inputs. */
20513 if (inputs_p
20514 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20515 {
20516 /* Consume the `:' or `::'. */
20517 cp_lexer_consume_token (parser->lexer);
20518 /* Parse the output-operands. */
20519 if (cp_lexer_next_token_is_not (parser->lexer,
20520 CPP_COLON)
20521 && cp_lexer_next_token_is_not (parser->lexer,
20522 CPP_SCOPE)
20523 && cp_lexer_next_token_is_not (parser->lexer,
20524 CPP_CLOSE_PAREN))
20525 {
20526 inputs = cp_parser_asm_operand_list (parser);
20527 if (inputs == error_mark_node)
20528 invalid_inputs_p = true;
20529 }
20530 }
20531 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20532 /* The clobbers are coming next. */
20533 clobbers_p = true;
20534
20535 /* Look for clobbers. */
20536 if (clobbers_p
20537 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20538 {
20539 clobbers_p = true;
20540 /* Consume the `:' or `::'. */
20541 cp_lexer_consume_token (parser->lexer);
20542 /* Parse the clobbers. */
20543 if (cp_lexer_next_token_is_not (parser->lexer,
20544 CPP_COLON)
20545 && cp_lexer_next_token_is_not (parser->lexer,
20546 CPP_CLOSE_PAREN))
20547 clobbers = cp_parser_asm_clobber_list (parser);
20548 }
20549 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20550 /* The labels are coming next. */
20551 labels_p = true;
20552
20553 /* Look for labels. */
20554 if (labels_p
20555 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
20556 {
20557 labels_p = true;
20558 /* Consume the `:' or `::'. */
20559 cp_lexer_consume_token (parser->lexer);
20560 /* Parse the labels. */
20561 labels = cp_parser_asm_label_list (parser);
20562 }
20563
20564 if (goto_p && !labels_p)
20565 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
20566 }
20567 else if (goto_p)
20568 missing = RT_COLON_SCOPE;
20569
20570 /* Look for the closing `)'. */
20571 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
20572 missing ? missing : RT_CLOSE_PAREN))
20573 cp_parser_skip_to_closing_parenthesis (parser, true, false,
20574 /*consume_paren=*/true);
20575 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20576
20577 if (!invalid_inputs_p && !invalid_outputs_p)
20578 {
20579 /* Create the ASM_EXPR. */
20580 if (parser->in_function_body)
20581 {
20582 asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
20583 inputs, clobbers, labels, inline_p);
20584 /* If the extended syntax was not used, mark the ASM_EXPR. */
20585 if (!extended_p)
20586 {
20587 tree temp = asm_stmt;
20588 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
20589 temp = TREE_OPERAND (temp, 0);
20590
20591 ASM_INPUT_P (temp) = 1;
20592 }
20593 }
20594 else
20595 symtab->finalize_toplevel_asm (string);
20596 }
20597 }
20598
20599 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
20600 type that comes from the decl-specifier-seq. */
20601
20602 static tree
20603 strip_declarator_types (tree type, cp_declarator *declarator)
20604 {
20605 for (cp_declarator *d = declarator; d;)
20606 switch (d->kind)
20607 {
20608 case cdk_id:
20609 case cdk_decomp:
20610 case cdk_error:
20611 d = NULL;
20612 break;
20613
20614 default:
20615 if (TYPE_PTRMEMFUNC_P (type))
20616 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
20617 type = TREE_TYPE (type);
20618 d = d->declarator;
20619 break;
20620 }
20621
20622 return type;
20623 }
20624
20625 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
20626 a construct looks like a variable definition but is actually a function
20627 declaration. DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
20628 is the declarator for this function declaration. */
20629
20630 static void
20631 warn_about_ambiguous_parse (const cp_decl_specifier_seq *decl_specifiers,
20632 const cp_declarator *declarator)
20633 {
20634 /* Only warn if we are declaring a function at block scope. */
20635 if (!at_function_scope_p ())
20636 return;
20637
20638 /* And only if there is no storage class specified. */
20639 if (decl_specifiers->storage_class != sc_none
20640 || decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
20641 return;
20642
20643 if (declarator->kind != cdk_function
20644 || !declarator->declarator
20645 || declarator->declarator->kind != cdk_id
20646 || !identifier_p (get_unqualified_id
20647 (const_cast<cp_declarator *>(declarator))))
20648 return;
20649
20650 /* Don't warn when the whole declarator (not just the declarator-id!)
20651 was parenthesized. That is, don't warn for int(n()) but do warn
20652 for int(f)(). */
20653 if (declarator->parenthesized != UNKNOWN_LOCATION)
20654 return;
20655
20656 tree type;
20657 if (decl_specifiers->type)
20658 {
20659 type = decl_specifiers->type;
20660 if (TREE_CODE (type) == TYPE_DECL)
20661 type = TREE_TYPE (type);
20662
20663 /* If the return type is void there is no ambiguity. */
20664 if (same_type_p (type, void_type_node))
20665 return;
20666 }
20667 else
20668 {
20669 /* Code like long f(); will have null ->type. If we have any
20670 type-specifiers, pretend we've seen int. */
20671 gcc_checking_assert (decl_specifiers->any_type_specifiers_p);
20672 type = integer_type_node;
20673 }
20674
20675 auto_diagnostic_group d;
20676 location_t loc = declarator->u.function.parens_loc;
20677 tree params = declarator->u.function.parameters;
20678 const bool has_list_ctor_p = CLASS_TYPE_P (type) && TYPE_HAS_LIST_CTOR (type);
20679
20680 /* The T t() case. */
20681 if (params == void_list_node)
20682 {
20683 if (warning_at (loc, OPT_Wvexing_parse,
20684 "empty parentheses were disambiguated as a function "
20685 "declaration"))
20686 {
20687 /* () means value-initialization (C++03 and up); {} (C++11 and up)
20688 means value-initialization or aggregate-initialization, nothing
20689 means default-initialization. We can only suggest removing the
20690 parentheses/adding {} if T has a default constructor. */
20691 if (!CLASS_TYPE_P (type) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
20692 {
20693 gcc_rich_location iloc (loc);
20694 iloc.add_fixit_remove ();
20695 inform (&iloc, "remove parentheses to default-initialize "
20696 "a variable");
20697 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
20698 {
20699 if (CP_AGGREGATE_TYPE_P (type))
20700 inform (loc, "or replace parentheses with braces to "
20701 "aggregate-initialize a variable");
20702 else
20703 inform (loc, "or replace parentheses with braces to "
20704 "value-initialize a variable");
20705 }
20706 }
20707 }
20708 return;
20709 }
20710
20711 /* If we had (...) or the parameter-list wasn't parenthesized,
20712 we're done. */
20713 if (params == NULL_TREE || !PARENTHESIZED_LIST_P (params))
20714 return;
20715
20716 /* The T t(X()) case. */
20717 if (list_length (params) == 2)
20718 {
20719 if (warning_at (loc, OPT_Wvexing_parse,
20720 "parentheses were disambiguated as a function "
20721 "declaration"))
20722 {
20723 gcc_rich_location iloc (loc);
20724 /* {}-initialization means that we can use an initializer-list
20725 constructor if no default constructor is available, so don't
20726 suggest using {} for classes that have an initializer_list
20727 constructor. */
20728 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
20729 {
20730 iloc.add_fixit_replace (get_start (loc), "{");
20731 iloc.add_fixit_replace (get_finish (loc), "}");
20732 inform (&iloc, "replace parentheses with braces to declare a "
20733 "variable");
20734 }
20735 else
20736 {
20737 iloc.add_fixit_insert_after (get_start (loc), "(");
20738 iloc.add_fixit_insert_before (get_finish (loc), ")");
20739 inform (&iloc, "add parentheses to declare a variable");
20740 }
20741 }
20742 }
20743 /* The T t(X(), X()) case. */
20744 else if (warning_at (loc, OPT_Wvexing_parse,
20745 "parentheses were disambiguated as a function "
20746 "declaration"))
20747 {
20748 gcc_rich_location iloc (loc);
20749 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
20750 {
20751 iloc.add_fixit_replace (get_start (loc), "{");
20752 iloc.add_fixit_replace (get_finish (loc), "}");
20753 inform (&iloc, "replace parentheses with braces to declare a "
20754 "variable");
20755 }
20756 }
20757 }
20758
20759 /* Declarators [gram.dcl.decl] */
20760
20761 /* Parse an init-declarator.
20762
20763 init-declarator:
20764 declarator initializer [opt]
20765
20766 GNU Extension:
20767
20768 init-declarator:
20769 declarator asm-specification [opt] attributes [opt] initializer [opt]
20770
20771 function-definition:
20772 decl-specifier-seq [opt] declarator ctor-initializer [opt]
20773 function-body
20774 decl-specifier-seq [opt] declarator function-try-block
20775
20776 GNU Extension:
20777
20778 function-definition:
20779 __extension__ function-definition
20780
20781 TM Extension:
20782
20783 function-definition:
20784 decl-specifier-seq [opt] declarator function-transaction-block
20785
20786 The parser flags FLAGS is used to control type-specifier parsing.
20787
20788 The DECL_SPECIFIERS apply to this declarator. Returns a
20789 representation of the entity declared. If MEMBER_P is TRUE, then
20790 this declarator appears in a class scope. The new DECL created by
20791 this declarator is returned.
20792
20793 The CHECKS are access checks that should be performed once we know
20794 what entity is being declared (and, therefore, what classes have
20795 befriended it).
20796
20797 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20798 for a function-definition here as well. If the declarator is a
20799 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20800 be TRUE upon return. By that point, the function-definition will
20801 have been completely parsed.
20802
20803 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20804 is FALSE.
20805
20806 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20807 parsed declaration if it is an uninitialized single declarator not followed
20808 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20809 if present, will not be consumed. If returned, this declarator will be
20810 created with SD_INITIALIZED but will not call cp_finish_decl.
20811
20812 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20813 and there is an initializer, the pointed location_t is set to the
20814 location of the '=' or `(', or '{' in C++11 token introducing the
20815 initializer. */
20816
20817 static tree
20818 cp_parser_init_declarator (cp_parser* parser,
20819 cp_parser_flags flags,
20820 cp_decl_specifier_seq *decl_specifiers,
20821 vec<deferred_access_check, va_gc> *checks,
20822 bool function_definition_allowed_p,
20823 bool member_p,
20824 int declares_class_or_enum,
20825 bool* function_definition_p,
20826 tree* maybe_range_for_decl,
20827 location_t* init_loc,
20828 tree* auto_result)
20829 {
20830 cp_token *token = NULL, *asm_spec_start_token = NULL,
20831 *attributes_start_token = NULL;
20832 cp_declarator *declarator;
20833 tree prefix_attributes;
20834 tree attributes = NULL;
20835 tree asm_specification;
20836 tree initializer;
20837 tree decl = NULL_TREE;
20838 tree scope;
20839 int is_initialized;
20840 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20841 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20842 "(...)". */
20843 enum cpp_ttype initialization_kind;
20844 bool is_direct_init = false;
20845 bool is_non_constant_init;
20846 int ctor_dtor_or_conv_p;
20847 bool friend_p = cp_parser_friend_p (decl_specifiers);
20848 tree pushed_scope = NULL_TREE;
20849 bool range_for_decl_p = false;
20850 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20851 location_t tmp_init_loc = UNKNOWN_LOCATION;
20852
20853 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
20854 flags |= CP_PARSER_FLAGS_CONSTEVAL;
20855
20856 /* Gather the attributes that were provided with the
20857 decl-specifiers. */
20858 prefix_attributes = decl_specifiers->attributes;
20859
20860 /* Assume that this is not the declarator for a function
20861 definition. */
20862 if (function_definition_p)
20863 *function_definition_p = false;
20864
20865 /* Default arguments are only permitted for function parameters. */
20866 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
20867 parser->default_arg_ok_p = false;
20868
20869 /* Defer access checks while parsing the declarator; we cannot know
20870 what names are accessible until we know what is being
20871 declared. */
20872 resume_deferring_access_checks ();
20873
20874 token = cp_lexer_peek_token (parser->lexer);
20875
20876 /* Parse the declarator. */
20877 declarator
20878 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20879 flags, &ctor_dtor_or_conv_p,
20880 /*parenthesized_p=*/NULL,
20881 member_p, friend_p, /*static_p=*/false);
20882 /* Gather up the deferred checks. */
20883 stop_deferring_access_checks ();
20884
20885 parser->default_arg_ok_p = saved_default_arg_ok_p;
20886
20887 /* If the DECLARATOR was erroneous, there's no need to go
20888 further. */
20889 if (declarator == cp_error_declarator)
20890 return error_mark_node;
20891
20892 /* Check that the number of template-parameter-lists is OK. */
20893 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
20894 token->location))
20895 return error_mark_node;
20896
20897 if (declares_class_or_enum & 2)
20898 cp_parser_check_for_definition_in_return_type (declarator,
20899 decl_specifiers->type,
20900 decl_specifiers->locations[ds_type_spec]);
20901
20902 /* Figure out what scope the entity declared by the DECLARATOR is
20903 located in. `grokdeclarator' sometimes changes the scope, so
20904 we compute it now. */
20905 scope = get_scope_of_declarator (declarator);
20906
20907 /* Perform any lookups in the declared type which were thought to be
20908 dependent, but are not in the scope of the declarator. */
20909 decl_specifiers->type
20910 = maybe_update_decl_type (decl_specifiers->type, scope);
20911
20912 /* If we're allowing GNU extensions, look for an
20913 asm-specification. */
20914 if (cp_parser_allow_gnu_extensions_p (parser))
20915 {
20916 /* Look for an asm-specification. */
20917 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
20918 asm_specification = cp_parser_asm_specification_opt (parser);
20919 }
20920 else
20921 asm_specification = NULL_TREE;
20922
20923 /* Look for attributes. */
20924 attributes_start_token = cp_lexer_peek_token (parser->lexer);
20925 attributes = cp_parser_attributes_opt (parser);
20926
20927 /* Peek at the next token. */
20928 token = cp_lexer_peek_token (parser->lexer);
20929
20930 bool bogus_implicit_tmpl = false;
20931
20932 if (function_declarator_p (declarator))
20933 {
20934 /* Handle C++17 deduction guides. */
20935 if (!decl_specifiers->type
20936 && !decl_specifiers->any_type_specifiers_p
20937 && ctor_dtor_or_conv_p <= 0
20938 && cxx_dialect >= cxx17)
20939 {
20940 cp_declarator *id = get_id_declarator (declarator);
20941 tree name = id->u.id.unqualified_name;
20942 parser->scope = id->u.id.qualifying_scope;
20943 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
20944 if (tmpl
20945 && (DECL_CLASS_TEMPLATE_P (tmpl)
20946 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
20947 {
20948 id->u.id.unqualified_name = dguide_name (tmpl);
20949 id->u.id.sfk = sfk_deduction_guide;
20950 ctor_dtor_or_conv_p = 1;
20951 }
20952 }
20953
20954 if (!member_p && !cp_parser_error_occurred (parser))
20955 warn_about_ambiguous_parse (decl_specifiers, declarator);
20956
20957 /* Check to see if the token indicates the start of a
20958 function-definition. */
20959 if (cp_parser_token_starts_function_definition_p (token))
20960 {
20961 if (!function_definition_allowed_p)
20962 {
20963 /* If a function-definition should not appear here, issue an
20964 error message. */
20965 cp_parser_error (parser,
20966 "a function-definition is not allowed here");
20967 return error_mark_node;
20968 }
20969
20970 location_t func_brace_location
20971 = cp_lexer_peek_token (parser->lexer)->location;
20972
20973 /* Neither attributes nor an asm-specification are allowed
20974 on a function-definition. */
20975 if (asm_specification)
20976 error_at (asm_spec_start_token->location,
20977 "an %<asm%> specification is not allowed "
20978 "on a function-definition");
20979 if (attributes)
20980 error_at (attributes_start_token->location,
20981 "attributes are not allowed "
20982 "on a function-definition");
20983 /* This is a function-definition. */
20984 *function_definition_p = true;
20985
20986 /* Parse the function definition. */
20987 if (member_p)
20988 decl = cp_parser_save_member_function_body (parser,
20989 decl_specifiers,
20990 declarator,
20991 prefix_attributes);
20992 else
20993 decl =
20994 (cp_parser_function_definition_from_specifiers_and_declarator
20995 (parser, decl_specifiers, prefix_attributes, declarator));
20996
20997 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
20998 {
20999 /* This is where the prologue starts... */
21000 DECL_STRUCT_FUNCTION (decl)->function_start_locus
21001 = func_brace_location;
21002 }
21003
21004 return decl;
21005 }
21006 }
21007 else if (parser->fully_implicit_function_template_p)
21008 {
21009 /* A non-template declaration involving a function parameter list
21010 containing an implicit template parameter will be made into a
21011 template. If the resulting declaration is not going to be an
21012 actual function then finish the template scope here to prevent it.
21013 An error message will be issued once we have a decl to talk about.
21014
21015 FIXME probably we should do type deduction rather than create an
21016 implicit template, but the standard currently doesn't allow it. */
21017 bogus_implicit_tmpl = true;
21018 finish_fully_implicit_template (parser, NULL_TREE);
21019 }
21020
21021 /* [dcl.dcl]
21022
21023 Only in function declarations for constructors, destructors, type
21024 conversions, and deduction guides can the decl-specifier-seq be omitted.
21025
21026 We explicitly postpone this check past the point where we handle
21027 function-definitions because we tolerate function-definitions
21028 that are missing their return types in some modes. */
21029 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
21030 {
21031 cp_parser_error (parser,
21032 "expected constructor, destructor, or type conversion");
21033 return error_mark_node;
21034 }
21035
21036 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
21037 if (token->type == CPP_EQ
21038 || token->type == CPP_OPEN_PAREN
21039 || token->type == CPP_OPEN_BRACE)
21040 {
21041 is_initialized = SD_INITIALIZED;
21042 initialization_kind = token->type;
21043 if (maybe_range_for_decl)
21044 *maybe_range_for_decl = error_mark_node;
21045 tmp_init_loc = token->location;
21046 if (init_loc && *init_loc == UNKNOWN_LOCATION)
21047 *init_loc = tmp_init_loc;
21048
21049 if (token->type == CPP_EQ
21050 && function_declarator_p (declarator))
21051 {
21052 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
21053 if (t2->keyword == RID_DEFAULT)
21054 is_initialized = SD_DEFAULTED;
21055 else if (t2->keyword == RID_DELETE)
21056 is_initialized = SD_DELETED;
21057 }
21058 }
21059 else
21060 {
21061 /* If the init-declarator isn't initialized and isn't followed by a
21062 `,' or `;', it's not a valid init-declarator. */
21063 if (token->type != CPP_COMMA
21064 && token->type != CPP_SEMICOLON)
21065 {
21066 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
21067 range_for_decl_p = true;
21068 else
21069 {
21070 if (!maybe_range_for_decl)
21071 cp_parser_error (parser, "expected initializer");
21072 return error_mark_node;
21073 }
21074 }
21075 is_initialized = SD_UNINITIALIZED;
21076 initialization_kind = CPP_EOF;
21077 }
21078
21079 /* Because start_decl has side-effects, we should only call it if we
21080 know we're going ahead. By this point, we know that we cannot
21081 possibly be looking at any other construct. */
21082 cp_parser_commit_to_tentative_parse (parser);
21083
21084 /* Enter the newly declared entry in the symbol table. If we're
21085 processing a declaration in a class-specifier, we wait until
21086 after processing the initializer. */
21087 if (!member_p)
21088 {
21089 if (parser->in_unbraced_linkage_specification_p)
21090 decl_specifiers->storage_class = sc_extern;
21091 decl = start_decl (declarator, decl_specifiers,
21092 range_for_decl_p? SD_INITIALIZED : is_initialized,
21093 attributes, prefix_attributes, &pushed_scope);
21094 cp_finalize_omp_declare_simd (parser, decl);
21095 cp_finalize_oacc_routine (parser, decl, false);
21096 /* Adjust location of decl if declarator->id_loc is more appropriate:
21097 set, and decl wasn't merged with another decl, in which case its
21098 location would be different from input_location, and more accurate. */
21099 if (DECL_P (decl)
21100 && declarator->id_loc != UNKNOWN_LOCATION
21101 && DECL_SOURCE_LOCATION (decl) == input_location)
21102 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
21103 }
21104 else if (scope)
21105 /* Enter the SCOPE. That way unqualified names appearing in the
21106 initializer will be looked up in SCOPE. */
21107 pushed_scope = push_scope (scope);
21108
21109 /* Perform deferred access control checks, now that we know in which
21110 SCOPE the declared entity resides. */
21111 if (!member_p && decl)
21112 {
21113 tree saved_current_function_decl = NULL_TREE;
21114
21115 /* If the entity being declared is a function, pretend that we
21116 are in its scope. If it is a `friend', it may have access to
21117 things that would not otherwise be accessible. */
21118 if (TREE_CODE (decl) == FUNCTION_DECL)
21119 {
21120 saved_current_function_decl = current_function_decl;
21121 current_function_decl = decl;
21122 }
21123
21124 /* Perform access checks for template parameters. */
21125 cp_parser_perform_template_parameter_access_checks (checks);
21126
21127 /* Perform the access control checks for the declarator and the
21128 decl-specifiers. */
21129 perform_deferred_access_checks (tf_warning_or_error);
21130
21131 /* Restore the saved value. */
21132 if (TREE_CODE (decl) == FUNCTION_DECL)
21133 current_function_decl = saved_current_function_decl;
21134 }
21135
21136 /* Parse the initializer. */
21137 initializer = NULL_TREE;
21138 is_direct_init = false;
21139 is_non_constant_init = true;
21140 if (is_initialized)
21141 {
21142 if (function_declarator_p (declarator))
21143 {
21144 if (initialization_kind == CPP_EQ)
21145 initializer = cp_parser_pure_specifier (parser);
21146 else
21147 {
21148 /* If the declaration was erroneous, we don't really
21149 know what the user intended, so just silently
21150 consume the initializer. */
21151 if (decl != error_mark_node)
21152 error_at (tmp_init_loc, "initializer provided for function");
21153 cp_parser_skip_to_closing_parenthesis (parser,
21154 /*recovering=*/true,
21155 /*or_comma=*/false,
21156 /*consume_paren=*/true);
21157 }
21158 }
21159 else
21160 {
21161 /* We want to record the extra mangling scope for in-class
21162 initializers of class members and initializers of static
21163 data member templates and namespace-scope initializers.
21164 The former involves deferring parsing of the initializer
21165 until end of class as with default arguments. So right
21166 here we only handle the latter two. */
21167 bool has_lambda_scope = false;
21168
21169 if (decl != error_mark_node
21170 && !member_p
21171 && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
21172 has_lambda_scope = true;
21173
21174 if (has_lambda_scope)
21175 start_lambda_scope (decl);
21176 initializer = cp_parser_initializer (parser,
21177 &is_direct_init,
21178 &is_non_constant_init);
21179 if (has_lambda_scope)
21180 finish_lambda_scope ();
21181 if (initializer == error_mark_node)
21182 cp_parser_skip_to_end_of_statement (parser);
21183 }
21184 }
21185
21186 /* The old parser allows attributes to appear after a parenthesized
21187 initializer. Mark Mitchell proposed removing this functionality
21188 on the GCC mailing lists on 2002-08-13. This parser accepts the
21189 attributes -- but ignores them. Made a permerror in GCC 8. */
21190 if (cp_parser_allow_gnu_extensions_p (parser)
21191 && initialization_kind == CPP_OPEN_PAREN
21192 && cp_parser_attributes_opt (parser)
21193 && permerror (input_location,
21194 "attributes after parenthesized initializer ignored"))
21195 {
21196 static bool hint;
21197 if (flag_permissive && !hint)
21198 {
21199 hint = true;
21200 inform (input_location,
21201 "this flexibility is deprecated and will be removed");
21202 }
21203 }
21204
21205 /* And now complain about a non-function implicit template. */
21206 if (bogus_implicit_tmpl && decl != error_mark_node)
21207 error_at (DECL_SOURCE_LOCATION (decl),
21208 "non-function %qD declared as implicit template", decl);
21209
21210 /* For an in-class declaration, use `grokfield' to create the
21211 declaration. */
21212 if (member_p)
21213 {
21214 if (pushed_scope)
21215 {
21216 pop_scope (pushed_scope);
21217 pushed_scope = NULL_TREE;
21218 }
21219 decl = grokfield (declarator, decl_specifiers,
21220 initializer, !is_non_constant_init,
21221 /*asmspec=*/NULL_TREE,
21222 attr_chainon (attributes, prefix_attributes));
21223 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
21224 cp_parser_save_default_args (parser, decl);
21225 cp_finalize_omp_declare_simd (parser, decl);
21226 cp_finalize_oacc_routine (parser, decl, false);
21227 }
21228
21229 /* Finish processing the declaration. But, skip member
21230 declarations. */
21231 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
21232 {
21233 cp_finish_decl (decl,
21234 initializer, !is_non_constant_init,
21235 asm_specification,
21236 /* If the initializer is in parentheses, then this is
21237 a direct-initialization, which means that an
21238 `explicit' constructor is OK. Otherwise, an
21239 `explicit' constructor cannot be used. */
21240 ((is_direct_init || !is_initialized)
21241 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
21242 }
21243 else if ((cxx_dialect != cxx98) && friend_p
21244 && decl && TREE_CODE (decl) == FUNCTION_DECL)
21245 /* Core issue #226 (C++0x only): A default template-argument
21246 shall not be specified in a friend class template
21247 declaration. */
21248 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
21249 /*is_partial=*/false, /*is_friend_decl=*/1);
21250
21251 if (!friend_p && pushed_scope)
21252 pop_scope (pushed_scope);
21253
21254 if (function_declarator_p (declarator)
21255 && parser->fully_implicit_function_template_p)
21256 {
21257 if (member_p)
21258 decl = finish_fully_implicit_template (parser, decl);
21259 else
21260 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
21261 }
21262
21263 if (auto_result && is_initialized && decl_specifiers->type
21264 && type_uses_auto (decl_specifiers->type))
21265 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
21266
21267 return decl;
21268 }
21269
21270 /* Parse a declarator.
21271
21272 declarator:
21273 direct-declarator
21274 ptr-operator declarator
21275
21276 abstract-declarator:
21277 ptr-operator abstract-declarator [opt]
21278 direct-abstract-declarator
21279
21280 GNU Extensions:
21281
21282 declarator:
21283 attributes [opt] direct-declarator
21284 attributes [opt] ptr-operator declarator
21285
21286 abstract-declarator:
21287 attributes [opt] ptr-operator abstract-declarator [opt]
21288 attributes [opt] direct-abstract-declarator
21289
21290 The parser flags FLAGS is used to control type-specifier parsing.
21291
21292 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
21293 detect constructors, destructors, deduction guides, or conversion operators.
21294 It is set to -1 if the declarator is a name, and +1 if it is a
21295 function. Otherwise it is set to zero. Usually you just want to
21296 test for >0, but internally the negative value is used.
21297
21298 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
21299 a decl-specifier-seq unless it declares a constructor, destructor,
21300 or conversion. It might seem that we could check this condition in
21301 semantic analysis, rather than parsing, but that makes it difficult
21302 to handle something like `f()'. We want to notice that there are
21303 no decl-specifiers, and therefore realize that this is an
21304 expression, not a declaration.)
21305
21306 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
21307 the declarator is a direct-declarator of the form "(...)".
21308
21309 MEMBER_P is true iff this declarator is a member-declarator.
21310
21311 FRIEND_P is true iff this declarator is a friend.
21312
21313 STATIC_P is true iff the keyword static was seen. */
21314
21315 static cp_declarator *
21316 cp_parser_declarator (cp_parser* parser,
21317 cp_parser_declarator_kind dcl_kind,
21318 cp_parser_flags flags,
21319 int* ctor_dtor_or_conv_p,
21320 bool* parenthesized_p,
21321 bool member_p, bool friend_p, bool static_p)
21322 {
21323 cp_declarator *declarator;
21324 enum tree_code code;
21325 cp_cv_quals cv_quals;
21326 tree class_type;
21327 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
21328
21329 /* Assume this is not a constructor, destructor, or type-conversion
21330 operator. */
21331 if (ctor_dtor_or_conv_p)
21332 *ctor_dtor_or_conv_p = 0;
21333
21334 if (cp_parser_allow_gnu_extensions_p (parser))
21335 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
21336
21337 /* Check for the ptr-operator production. */
21338 cp_parser_parse_tentatively (parser);
21339 /* Parse the ptr-operator. */
21340 code = cp_parser_ptr_operator (parser,
21341 &class_type,
21342 &cv_quals,
21343 &std_attributes);
21344
21345 /* If that worked, then we have a ptr-operator. */
21346 if (cp_parser_parse_definitely (parser))
21347 {
21348 /* If a ptr-operator was found, then this declarator was not
21349 parenthesized. */
21350 if (parenthesized_p)
21351 *parenthesized_p = false;
21352 /* The dependent declarator is optional if we are parsing an
21353 abstract-declarator. */
21354 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
21355 cp_parser_parse_tentatively (parser);
21356
21357 /* Parse the dependent declarator. */
21358 declarator = cp_parser_declarator (parser, dcl_kind,
21359 CP_PARSER_FLAGS_NONE,
21360 /*ctor_dtor_or_conv_p=*/NULL,
21361 /*parenthesized_p=*/NULL,
21362 /*member_p=*/false,
21363 friend_p, /*static_p=*/false);
21364
21365 /* If we are parsing an abstract-declarator, we must handle the
21366 case where the dependent declarator is absent. */
21367 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
21368 && !cp_parser_parse_definitely (parser))
21369 declarator = NULL;
21370
21371 declarator = cp_parser_make_indirect_declarator
21372 (code, class_type, cv_quals, declarator, std_attributes);
21373 }
21374 /* Everything else is a direct-declarator. */
21375 else
21376 {
21377 if (parenthesized_p)
21378 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
21379 CPP_OPEN_PAREN);
21380 declarator = cp_parser_direct_declarator (parser, dcl_kind,
21381 flags, ctor_dtor_or_conv_p,
21382 member_p, friend_p, static_p);
21383 }
21384
21385 if (gnu_attributes && declarator && declarator != cp_error_declarator)
21386 declarator->attributes = gnu_attributes;
21387 return declarator;
21388 }
21389
21390 /* Parse a direct-declarator or direct-abstract-declarator.
21391
21392 direct-declarator:
21393 declarator-id
21394 direct-declarator ( parameter-declaration-clause )
21395 cv-qualifier-seq [opt]
21396 ref-qualifier [opt]
21397 exception-specification [opt]
21398 direct-declarator [ constant-expression [opt] ]
21399 ( declarator )
21400
21401 direct-abstract-declarator:
21402 direct-abstract-declarator [opt]
21403 ( parameter-declaration-clause )
21404 cv-qualifier-seq [opt]
21405 ref-qualifier [opt]
21406 exception-specification [opt]
21407 direct-abstract-declarator [opt] [ constant-expression [opt] ]
21408 ( abstract-declarator )
21409
21410 Returns a representation of the declarator. DCL_KIND is
21411 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
21412 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
21413 we are parsing a direct-declarator. It is
21414 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
21415 of ambiguity we prefer an abstract declarator, as per
21416 [dcl.ambig.res].
21417 The parser flags FLAGS is used to control type-specifier parsing.
21418 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
21419 as for cp_parser_declarator. */
21420
21421 static cp_declarator *
21422 cp_parser_direct_declarator (cp_parser* parser,
21423 cp_parser_declarator_kind dcl_kind,
21424 cp_parser_flags flags,
21425 int* ctor_dtor_or_conv_p,
21426 bool member_p, bool friend_p, bool static_p)
21427 {
21428 cp_token *token;
21429 cp_declarator *declarator = NULL;
21430 tree scope = NULL_TREE;
21431 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21432 bool saved_in_declarator_p = parser->in_declarator_p;
21433 bool first = true;
21434 tree pushed_scope = NULL_TREE;
21435 cp_token *open_paren = NULL, *close_paren = NULL;
21436
21437 while (true)
21438 {
21439 /* Peek at the next token. */
21440 token = cp_lexer_peek_token (parser->lexer);
21441 if (token->type == CPP_OPEN_PAREN)
21442 {
21443 /* This is either a parameter-declaration-clause, or a
21444 parenthesized declarator. When we know we are parsing a
21445 named declarator, it must be a parenthesized declarator
21446 if FIRST is true. For instance, `(int)' is a
21447 parameter-declaration-clause, with an omitted
21448 direct-abstract-declarator. But `((*))', is a
21449 parenthesized abstract declarator. Finally, when T is a
21450 template parameter `(T)' is a
21451 parameter-declaration-clause, and not a parenthesized
21452 named declarator.
21453
21454 We first try and parse a parameter-declaration-clause,
21455 and then try a nested declarator (if FIRST is true).
21456
21457 It is not an error for it not to be a
21458 parameter-declaration-clause, even when FIRST is
21459 false. Consider,
21460
21461 int i (int);
21462 int i (3);
21463
21464 The first is the declaration of a function while the
21465 second is the definition of a variable, including its
21466 initializer.
21467
21468 Having seen only the parenthesis, we cannot know which of
21469 these two alternatives should be selected. Even more
21470 complex are examples like:
21471
21472 int i (int (a));
21473 int i (int (3));
21474
21475 The former is a function-declaration; the latter is a
21476 variable initialization.
21477
21478 Thus again, we try a parameter-declaration-clause, and if
21479 that fails, we back out and return. */
21480
21481 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
21482 {
21483 tree params;
21484 bool is_declarator = false;
21485
21486 open_paren = NULL;
21487
21488 /* In a member-declarator, the only valid interpretation
21489 of a parenthesis is the start of a
21490 parameter-declaration-clause. (It is invalid to
21491 initialize a static data member with a parenthesized
21492 initializer; only the "=" form of initialization is
21493 permitted.) */
21494 if (!member_p)
21495 cp_parser_parse_tentatively (parser);
21496
21497 /* Consume the `('. */
21498 const location_t parens_start = token->location;
21499 matching_parens parens;
21500 parens.consume_open (parser);
21501 if (first)
21502 {
21503 /* If this is going to be an abstract declarator, we're
21504 in a declarator and we can't have default args. */
21505 parser->default_arg_ok_p = false;
21506 parser->in_declarator_p = true;
21507 }
21508
21509 begin_scope (sk_function_parms, NULL_TREE);
21510
21511 /* Signal we are in the immediate function context. */
21512 if (flags & CP_PARSER_FLAGS_CONSTEVAL)
21513 current_binding_level->immediate_fn_ctx_p = true;
21514
21515 /* Parse the parameter-declaration-clause. */
21516 params
21517 = cp_parser_parameter_declaration_clause (parser, flags);
21518 const location_t parens_end
21519 = cp_lexer_peek_token (parser->lexer)->location;
21520
21521 /* Consume the `)'. */
21522 parens.require_close (parser);
21523
21524 /* If all went well, parse the cv-qualifier-seq,
21525 ref-qualifier and the exception-specification. */
21526 if (member_p || cp_parser_parse_definitely (parser))
21527 {
21528 cp_cv_quals cv_quals;
21529 cp_virt_specifiers virt_specifiers;
21530 cp_ref_qualifier ref_qual;
21531 tree exception_specification;
21532 tree late_return;
21533 tree attrs;
21534 bool memfn = (member_p || (pushed_scope
21535 && CLASS_TYPE_P (pushed_scope)));
21536 unsigned char local_variables_forbidden_p
21537 = parser->local_variables_forbidden_p;
21538 /* 'this' is not allowed in static member functions. */
21539 if (static_p || friend_p)
21540 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
21541
21542 is_declarator = true;
21543
21544 if (ctor_dtor_or_conv_p)
21545 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
21546 first = false;
21547
21548 /* Parse the cv-qualifier-seq. */
21549 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21550 /* Parse the ref-qualifier. */
21551 ref_qual = cp_parser_ref_qualifier_opt (parser);
21552 /* Parse the tx-qualifier. */
21553 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
21554
21555 tree save_ccp = current_class_ptr;
21556 tree save_ccr = current_class_ref;
21557 if (memfn)
21558 /* DR 1207: 'this' is in scope after the cv-quals. */
21559 inject_this_parameter (current_class_type, cv_quals);
21560
21561 /* If it turned out that this is e.g. a pointer to a
21562 function, we don't want to delay noexcept parsing. */
21563 if (declarator == NULL || declarator->kind != cdk_id)
21564 flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
21565
21566 /* Parse the exception-specification. */
21567 exception_specification
21568 = cp_parser_exception_specification_opt (parser,
21569 flags);
21570
21571 attrs = cp_parser_std_attribute_spec_seq (parser);
21572
21573 /* In here, we handle cases where attribute is used after
21574 the function declaration. For example:
21575 void func (int x) __attribute__((vector(..))); */
21576 tree gnu_attrs = NULL_TREE;
21577 tree requires_clause = NULL_TREE;
21578 late_return = (cp_parser_late_return_type_opt
21579 (parser, declarator, requires_clause));
21580
21581 /* Parse the virt-specifier-seq. */
21582 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21583
21584 location_t parens_loc = make_location (parens_start,
21585 parens_start,
21586 parens_end);
21587 /* Create the function-declarator. */
21588 declarator = make_call_declarator (declarator,
21589 params,
21590 cv_quals,
21591 virt_specifiers,
21592 ref_qual,
21593 tx_qual,
21594 exception_specification,
21595 late_return,
21596 requires_clause,
21597 parens_loc);
21598 declarator->std_attributes = attrs;
21599 declarator->attributes = gnu_attrs;
21600 /* Any subsequent parameter lists are to do with
21601 return type, so are not those of the declared
21602 function. */
21603 parser->default_arg_ok_p = false;
21604
21605 current_class_ptr = save_ccp;
21606 current_class_ref = save_ccr;
21607
21608 /* Restore the state of local_variables_forbidden_p. */
21609 parser->local_variables_forbidden_p
21610 = local_variables_forbidden_p;
21611 }
21612
21613 /* Remove the function parms from scope. */
21614 pop_bindings_and_leave_scope ();
21615
21616 if (is_declarator)
21617 /* Repeat the main loop. */
21618 continue;
21619 }
21620
21621 /* If this is the first, we can try a parenthesized
21622 declarator. */
21623 if (first)
21624 {
21625 bool saved_in_type_id_in_expr_p;
21626
21627 parser->default_arg_ok_p = saved_default_arg_ok_p;
21628 parser->in_declarator_p = saved_in_declarator_p;
21629
21630 open_paren = token;
21631 /* Consume the `('. */
21632 matching_parens parens;
21633 parens.consume_open (parser);
21634 /* Parse the nested declarator. */
21635 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21636 parser->in_type_id_in_expr_p = true;
21637 declarator
21638 = cp_parser_declarator (parser, dcl_kind, flags,
21639 ctor_dtor_or_conv_p,
21640 /*parenthesized_p=*/NULL,
21641 member_p, friend_p,
21642 /*static_p=*/false);
21643 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21644 first = false;
21645 /* Expect a `)'. */
21646 close_paren = cp_lexer_peek_token (parser->lexer);
21647 if (!parens.require_close (parser))
21648 declarator = cp_error_declarator;
21649 if (declarator == cp_error_declarator)
21650 break;
21651
21652 goto handle_declarator;
21653 }
21654 /* Otherwise, we must be done. */
21655 else
21656 break;
21657 }
21658 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
21659 && token->type == CPP_OPEN_SQUARE
21660 && !cp_next_tokens_can_be_attribute_p (parser))
21661 {
21662 /* Parse an array-declarator. */
21663 tree bounds, attrs;
21664
21665 if (ctor_dtor_or_conv_p)
21666 *ctor_dtor_or_conv_p = 0;
21667
21668 open_paren = NULL;
21669 first = false;
21670 parser->default_arg_ok_p = false;
21671 parser->in_declarator_p = true;
21672 /* Consume the `['. */
21673 cp_lexer_consume_token (parser->lexer);
21674 /* Peek at the next token. */
21675 token = cp_lexer_peek_token (parser->lexer);
21676 /* If the next token is `]', then there is no
21677 constant-expression. */
21678 if (token->type != CPP_CLOSE_SQUARE)
21679 {
21680 bool non_constant_p;
21681 bounds
21682 = cp_parser_constant_expression (parser,
21683 /*allow_non_constant=*/true,
21684 &non_constant_p);
21685 if (!non_constant_p)
21686 /* OK */;
21687 else if (error_operand_p (bounds))
21688 /* Already gave an error. */;
21689 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21690 /* Let compute_array_index_type diagnose this. */;
21691 else if (!parser->in_function_body
21692 || current_binding_level->kind == sk_function_parms)
21693 {
21694 /* Normally, the array bound must be an integral constant
21695 expression. However, as an extension, we allow VLAs
21696 in function scopes as long as they aren't part of a
21697 parameter declaration. */
21698 cp_parser_error (parser,
21699 "array bound is not an integer constant");
21700 bounds = error_mark_node;
21701 }
21702 else if (processing_template_decl
21703 && !type_dependent_expression_p (bounds))
21704 {
21705 /* Remember this wasn't a constant-expression. */
21706 bounds = build_nop (TREE_TYPE (bounds), bounds);
21707 TREE_SIDE_EFFECTS (bounds) = 1;
21708 }
21709 }
21710 else
21711 bounds = NULL_TREE;
21712 /* Look for the closing `]'. */
21713 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21714 {
21715 declarator = cp_error_declarator;
21716 break;
21717 }
21718
21719 attrs = cp_parser_std_attribute_spec_seq (parser);
21720 declarator = make_array_declarator (declarator, bounds);
21721 declarator->std_attributes = attrs;
21722 }
21723 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
21724 {
21725 {
21726 tree qualifying_scope;
21727 tree unqualified_name;
21728 tree attrs;
21729 special_function_kind sfk;
21730 bool abstract_ok;
21731 bool pack_expansion_p = false;
21732 cp_token *declarator_id_start_token;
21733
21734 /* Parse a declarator-id */
21735 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
21736 if (abstract_ok)
21737 {
21738 cp_parser_parse_tentatively (parser);
21739
21740 /* If we see an ellipsis, we should be looking at a
21741 parameter pack. */
21742 if (token->type == CPP_ELLIPSIS)
21743 {
21744 /* Consume the `...' */
21745 cp_lexer_consume_token (parser->lexer);
21746
21747 pack_expansion_p = true;
21748 }
21749 }
21750
21751 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
21752 unqualified_name
21753 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
21754 qualifying_scope = parser->scope;
21755 if (abstract_ok)
21756 {
21757 bool okay = false;
21758
21759 if (!unqualified_name && pack_expansion_p)
21760 {
21761 /* Check whether an error occurred. */
21762 okay = !cp_parser_error_occurred (parser);
21763
21764 /* We already consumed the ellipsis to mark a
21765 parameter pack, but we have no way to report it,
21766 so abort the tentative parse. We will be exiting
21767 immediately anyway. */
21768 cp_parser_abort_tentative_parse (parser);
21769 }
21770 else
21771 okay = cp_parser_parse_definitely (parser);
21772
21773 if (!okay)
21774 unqualified_name = error_mark_node;
21775 else if (unqualified_name
21776 && (qualifying_scope
21777 || (!identifier_p (unqualified_name))))
21778 {
21779 cp_parser_error (parser, "expected unqualified-id");
21780 unqualified_name = error_mark_node;
21781 }
21782 }
21783
21784 if (!unqualified_name)
21785 return NULL;
21786 if (unqualified_name == error_mark_node)
21787 {
21788 declarator = cp_error_declarator;
21789 pack_expansion_p = false;
21790 declarator->parameter_pack_p = false;
21791 break;
21792 }
21793
21794 attrs = cp_parser_std_attribute_spec_seq (parser);
21795
21796 if (qualifying_scope && at_namespace_scope_p ()
21797 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
21798 {
21799 /* In the declaration of a member of a template class
21800 outside of the class itself, the SCOPE will sometimes
21801 be a TYPENAME_TYPE. For example, given:
21802
21803 template <typename T>
21804 int S<T>::R::i = 3;
21805
21806 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
21807 this context, we must resolve S<T>::R to an ordinary
21808 type, rather than a typename type.
21809
21810 The reason we normally avoid resolving TYPENAME_TYPEs
21811 is that a specialization of `S' might render
21812 `S<T>::R' not a type. However, if `S' is
21813 specialized, then this `i' will not be used, so there
21814 is no harm in resolving the types here. */
21815 tree type;
21816
21817 /* Resolve the TYPENAME_TYPE. */
21818 type = resolve_typename_type (qualifying_scope,
21819 /*only_current_p=*/false);
21820 /* If that failed, the declarator is invalid. */
21821 if (TREE_CODE (type) == TYPENAME_TYPE)
21822 {
21823 if (typedef_variant_p (type))
21824 error_at (declarator_id_start_token->location,
21825 "cannot define member of dependent typedef "
21826 "%qT", type);
21827 else
21828 error_at (declarator_id_start_token->location,
21829 "%<%T::%E%> is not a type",
21830 TYPE_CONTEXT (qualifying_scope),
21831 TYPE_IDENTIFIER (qualifying_scope));
21832 }
21833 qualifying_scope = type;
21834 }
21835
21836 sfk = sfk_none;
21837
21838 if (unqualified_name)
21839 {
21840 tree class_type;
21841
21842 if (qualifying_scope
21843 && CLASS_TYPE_P (qualifying_scope))
21844 class_type = qualifying_scope;
21845 else
21846 class_type = current_class_type;
21847
21848 if (TREE_CODE (unqualified_name) == TYPE_DECL)
21849 {
21850 tree name_type = TREE_TYPE (unqualified_name);
21851
21852 if (!class_type || !same_type_p (name_type, class_type))
21853 {
21854 /* We do not attempt to print the declarator
21855 here because we do not have enough
21856 information about its original syntactic
21857 form. */
21858 cp_parser_error (parser, "invalid declarator");
21859 declarator = cp_error_declarator;
21860 break;
21861 }
21862 else if (qualifying_scope
21863 && CLASSTYPE_USE_TEMPLATE (name_type))
21864 {
21865 error_at (declarator_id_start_token->location,
21866 "invalid use of constructor as a template");
21867 inform (declarator_id_start_token->location,
21868 "use %<%T::%D%> instead of %<%T::%D%> to "
21869 "name the constructor in a qualified name",
21870 class_type,
21871 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
21872 class_type, name_type);
21873 declarator = cp_error_declarator;
21874 break;
21875 }
21876 unqualified_name = constructor_name (class_type);
21877 }
21878
21879 if (class_type)
21880 {
21881 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
21882 sfk = sfk_destructor;
21883 else if (identifier_p (unqualified_name)
21884 && IDENTIFIER_CONV_OP_P (unqualified_name))
21885 sfk = sfk_conversion;
21886 else if (/* There's no way to declare a constructor
21887 for an unnamed type, even if the type
21888 got a name for linkage purposes. */
21889 !TYPE_WAS_UNNAMED (class_type)
21890 /* Handle correctly (c++/19200):
21891
21892 struct S {
21893 struct T{};
21894 friend void S(T);
21895 };
21896
21897 and also:
21898
21899 namespace N {
21900 void S();
21901 }
21902
21903 struct S {
21904 friend void N::S();
21905 }; */
21906 && (!friend_p || class_type == qualifying_scope)
21907 && constructor_name_p (unqualified_name,
21908 class_type))
21909 sfk = sfk_constructor;
21910 else if (is_overloaded_fn (unqualified_name)
21911 && DECL_CONSTRUCTOR_P (get_first_fn
21912 (unqualified_name)))
21913 sfk = sfk_constructor;
21914
21915 if (ctor_dtor_or_conv_p && sfk != sfk_none)
21916 *ctor_dtor_or_conv_p = -1;
21917 }
21918 }
21919 declarator = make_id_declarator (qualifying_scope,
21920 unqualified_name,
21921 sfk, token->location);
21922 declarator->std_attributes = attrs;
21923 declarator->parameter_pack_p = pack_expansion_p;
21924
21925 if (pack_expansion_p)
21926 maybe_warn_variadic_templates ();
21927
21928 /* We're looking for this case in [temp.res]:
21929 A qualified-id is assumed to name a type if [...]
21930 - it is a decl-specifier of the decl-specifier-seq of a
21931 parameter-declaration in a declarator of a function or
21932 function template declaration, ... */
21933 if (cxx_dialect >= cxx20
21934 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
21935 && declarator->kind == cdk_id
21936 && !at_class_scope_p ()
21937 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21938 {
21939 /* ...whose declarator-id is qualified. If it isn't, never
21940 assume the parameters to refer to types. */
21941 if (qualifying_scope == NULL_TREE)
21942 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21943 else
21944 {
21945 /* Now we have something like
21946 template <typename T> int C::x(S::p);
21947 which can be a function template declaration or a
21948 variable template definition. If name lookup for
21949 the declarator-id C::x finds one or more function
21950 templates, assume S::p to name a type. Otherwise,
21951 don't. */
21952 tree decl
21953 = cp_parser_lookup_name (parser, unqualified_name,
21954 none_type,
21955 /*is_template=*/false,
21956 /*is_namespace=*/false,
21957 /*check_dependency=*/false,
21958 /*ambiguous_decls=*/NULL,
21959 token->location);
21960
21961 if (!is_overloaded_fn (decl)
21962 /* Allow
21963 template<typename T>
21964 A<T>::A(T::type) { } */
21965 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
21966 && constructor_name_p (unqualified_name,
21967 qualifying_scope)))
21968 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21969 }
21970 }
21971 }
21972
21973 handle_declarator:;
21974 scope = get_scope_of_declarator (declarator);
21975 if (scope)
21976 {
21977 /* Any names that appear after the declarator-id for a
21978 member are looked up in the containing scope. */
21979 if (at_function_scope_p ())
21980 {
21981 /* But declarations with qualified-ids can't appear in a
21982 function. */
21983 cp_parser_error (parser, "qualified-id in declaration");
21984 declarator = cp_error_declarator;
21985 break;
21986 }
21987 pushed_scope = push_scope (scope);
21988 }
21989 parser->in_declarator_p = true;
21990 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
21991 || (declarator && declarator->kind == cdk_id))
21992 /* Default args are only allowed on function
21993 declarations. */
21994 parser->default_arg_ok_p = saved_default_arg_ok_p;
21995 else
21996 parser->default_arg_ok_p = false;
21997
21998 first = false;
21999 }
22000 /* We're done. */
22001 else
22002 break;
22003 }
22004
22005 /* For an abstract declarator, we might wind up with nothing at this
22006 point. That's an error; the declarator is not optional. */
22007 if (!declarator)
22008 cp_parser_error (parser, "expected declarator");
22009 else if (open_paren)
22010 {
22011 /* Record overly parenthesized declarator so we can give a
22012 diagnostic about confusing decl/expr disambiguation. */
22013 if (declarator->kind == cdk_array)
22014 {
22015 /* If the open and close parens are on different lines, this
22016 is probably a formatting thing, so ignore. */
22017 expanded_location open = expand_location (open_paren->location);
22018 expanded_location close = expand_location (close_paren->location);
22019 if (open.line != close.line || open.file != close.file)
22020 open_paren = NULL;
22021 }
22022 if (open_paren)
22023 declarator->parenthesized = make_location (open_paren->location,
22024 open_paren->location,
22025 close_paren->location);
22026 }
22027
22028 /* If we entered a scope, we must exit it now. */
22029 if (pushed_scope)
22030 pop_scope (pushed_scope);
22031
22032 parser->default_arg_ok_p = saved_default_arg_ok_p;
22033 parser->in_declarator_p = saved_in_declarator_p;
22034
22035 return declarator;
22036 }
22037
22038 /* Parse a ptr-operator.
22039
22040 ptr-operator:
22041 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
22042 * cv-qualifier-seq [opt]
22043 &
22044 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
22045 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
22046
22047 GNU Extension:
22048
22049 ptr-operator:
22050 & cv-qualifier-seq [opt]
22051
22052 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
22053 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
22054 an rvalue reference. In the case of a pointer-to-member, *TYPE is
22055 filled in with the TYPE containing the member. *CV_QUALS is
22056 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
22057 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
22058 Note that the tree codes returned by this function have nothing
22059 to do with the types of trees that will be eventually be created
22060 to represent the pointer or reference type being parsed. They are
22061 just constants with suggestive names. */
22062 static enum tree_code
22063 cp_parser_ptr_operator (cp_parser* parser,
22064 tree* type,
22065 cp_cv_quals *cv_quals,
22066 tree *attributes)
22067 {
22068 enum tree_code code = ERROR_MARK;
22069 cp_token *token;
22070 tree attrs = NULL_TREE;
22071
22072 /* Assume that it's not a pointer-to-member. */
22073 *type = NULL_TREE;
22074 /* And that there are no cv-qualifiers. */
22075 *cv_quals = TYPE_UNQUALIFIED;
22076
22077 /* Peek at the next token. */
22078 token = cp_lexer_peek_token (parser->lexer);
22079
22080 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
22081 if (token->type == CPP_MULT)
22082 code = INDIRECT_REF;
22083 else if (token->type == CPP_AND)
22084 code = ADDR_EXPR;
22085 else if ((cxx_dialect != cxx98) &&
22086 token->type == CPP_AND_AND) /* C++0x only */
22087 code = NON_LVALUE_EXPR;
22088
22089 if (code != ERROR_MARK)
22090 {
22091 /* Consume the `*', `&' or `&&'. */
22092 cp_lexer_consume_token (parser->lexer);
22093
22094 /* A `*' can be followed by a cv-qualifier-seq, and so can a
22095 `&', if we are allowing GNU extensions. (The only qualifier
22096 that can legally appear after `&' is `restrict', but that is
22097 enforced during semantic analysis. */
22098 if (code == INDIRECT_REF
22099 || cp_parser_allow_gnu_extensions_p (parser))
22100 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
22101
22102 attrs = cp_parser_std_attribute_spec_seq (parser);
22103 if (attributes != NULL)
22104 *attributes = attrs;
22105 }
22106 else
22107 {
22108 /* Try the pointer-to-member case. */
22109 cp_parser_parse_tentatively (parser);
22110 /* Look for the optional `::' operator. */
22111 cp_parser_global_scope_opt (parser,
22112 /*current_scope_valid_p=*/false);
22113 /* Look for the nested-name specifier. */
22114 token = cp_lexer_peek_token (parser->lexer);
22115 cp_parser_nested_name_specifier (parser,
22116 /*typename_keyword_p=*/false,
22117 /*check_dependency_p=*/true,
22118 /*type_p=*/false,
22119 /*is_declaration=*/false);
22120 /* If we found it, and the next token is a `*', then we are
22121 indeed looking at a pointer-to-member operator. */
22122 if (!cp_parser_error_occurred (parser)
22123 && cp_parser_require (parser, CPP_MULT, RT_MULT))
22124 {
22125 /* Indicate that the `*' operator was used. */
22126 code = INDIRECT_REF;
22127
22128 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
22129 error_at (token->location, "%qD is a namespace", parser->scope);
22130 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
22131 error_at (token->location, "cannot form pointer to member of "
22132 "non-class %q#T", parser->scope);
22133 else
22134 {
22135 /* The type of which the member is a member is given by the
22136 current SCOPE. */
22137 *type = parser->scope;
22138 /* The next name will not be qualified. */
22139 parser->scope = NULL_TREE;
22140 parser->qualifying_scope = NULL_TREE;
22141 parser->object_scope = NULL_TREE;
22142 /* Look for optional c++11 attributes. */
22143 attrs = cp_parser_std_attribute_spec_seq (parser);
22144 if (attributes != NULL)
22145 *attributes = attrs;
22146 /* Look for the optional cv-qualifier-seq. */
22147 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
22148 }
22149 }
22150 /* If that didn't work we don't have a ptr-operator. */
22151 if (!cp_parser_parse_definitely (parser))
22152 cp_parser_error (parser, "expected ptr-operator");
22153 }
22154
22155 return code;
22156 }
22157
22158 /* Parse an (optional) cv-qualifier-seq.
22159
22160 cv-qualifier-seq:
22161 cv-qualifier cv-qualifier-seq [opt]
22162
22163 cv-qualifier:
22164 const
22165 volatile
22166
22167 GNU Extension:
22168
22169 cv-qualifier:
22170 __restrict__
22171
22172 Returns a bitmask representing the cv-qualifiers. */
22173
22174 static cp_cv_quals
22175 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
22176 {
22177 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
22178
22179 while (true)
22180 {
22181 cp_token *token;
22182 cp_cv_quals cv_qualifier;
22183
22184 /* Peek at the next token. */
22185 token = cp_lexer_peek_token (parser->lexer);
22186 /* See if it's a cv-qualifier. */
22187 switch (token->keyword)
22188 {
22189 case RID_CONST:
22190 cv_qualifier = TYPE_QUAL_CONST;
22191 break;
22192
22193 case RID_VOLATILE:
22194 cv_qualifier = TYPE_QUAL_VOLATILE;
22195 break;
22196
22197 case RID_RESTRICT:
22198 cv_qualifier = TYPE_QUAL_RESTRICT;
22199 break;
22200
22201 default:
22202 cv_qualifier = TYPE_UNQUALIFIED;
22203 break;
22204 }
22205
22206 if (!cv_qualifier)
22207 break;
22208
22209 if (cv_quals & cv_qualifier)
22210 {
22211 gcc_rich_location richloc (token->location);
22212 richloc.add_fixit_remove ();
22213 error_at (&richloc, "duplicate cv-qualifier");
22214 cp_lexer_purge_token (parser->lexer);
22215 }
22216 else
22217 {
22218 cp_lexer_consume_token (parser->lexer);
22219 cv_quals |= cv_qualifier;
22220 }
22221 }
22222
22223 return cv_quals;
22224 }
22225
22226 /* Parse an (optional) ref-qualifier
22227
22228 ref-qualifier:
22229 &
22230 &&
22231
22232 Returns cp_ref_qualifier representing ref-qualifier. */
22233
22234 static cp_ref_qualifier
22235 cp_parser_ref_qualifier_opt (cp_parser* parser)
22236 {
22237 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
22238
22239 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
22240 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
22241 return ref_qual;
22242
22243 while (true)
22244 {
22245 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
22246 cp_token *token = cp_lexer_peek_token (parser->lexer);
22247
22248 switch (token->type)
22249 {
22250 case CPP_AND:
22251 curr_ref_qual = REF_QUAL_LVALUE;
22252 break;
22253
22254 case CPP_AND_AND:
22255 curr_ref_qual = REF_QUAL_RVALUE;
22256 break;
22257
22258 default:
22259 curr_ref_qual = REF_QUAL_NONE;
22260 break;
22261 }
22262
22263 if (!curr_ref_qual)
22264 break;
22265 else if (ref_qual)
22266 {
22267 error_at (token->location, "multiple ref-qualifiers");
22268 cp_lexer_purge_token (parser->lexer);
22269 }
22270 else
22271 {
22272 ref_qual = curr_ref_qual;
22273 cp_lexer_consume_token (parser->lexer);
22274 }
22275 }
22276
22277 return ref_qual;
22278 }
22279
22280 /* Parse an optional tx-qualifier.
22281
22282 tx-qualifier:
22283 transaction_safe
22284 transaction_safe_dynamic */
22285
22286 static tree
22287 cp_parser_tx_qualifier_opt (cp_parser *parser)
22288 {
22289 cp_token *token = cp_lexer_peek_token (parser->lexer);
22290 if (token->type == CPP_NAME)
22291 {
22292 tree name = token->u.value;
22293 const char *p = IDENTIFIER_POINTER (name);
22294 const int len = strlen ("transaction_safe");
22295 if (!strncmp (p, "transaction_safe", len))
22296 {
22297 p += len;
22298 if (*p == '\0'
22299 || !strcmp (p, "_dynamic"))
22300 {
22301 cp_lexer_consume_token (parser->lexer);
22302 if (!flag_tm)
22303 {
22304 error ("%qE requires %<-fgnu-tm%>", name);
22305 return NULL_TREE;
22306 }
22307 else
22308 return name;
22309 }
22310 }
22311 }
22312 return NULL_TREE;
22313 }
22314
22315 /* Parse an (optional) virt-specifier-seq.
22316
22317 virt-specifier-seq:
22318 virt-specifier virt-specifier-seq [opt]
22319
22320 virt-specifier:
22321 override
22322 final
22323
22324 Returns a bitmask representing the virt-specifiers. */
22325
22326 static cp_virt_specifiers
22327 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
22328 {
22329 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22330
22331 while (true)
22332 {
22333 cp_token *token;
22334 cp_virt_specifiers virt_specifier;
22335
22336 /* Peek at the next token. */
22337 token = cp_lexer_peek_token (parser->lexer);
22338 /* See if it's a virt-specifier-qualifier. */
22339 if (token->type != CPP_NAME)
22340 break;
22341 if (id_equal (token->u.value, "override"))
22342 {
22343 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
22344 virt_specifier = VIRT_SPEC_OVERRIDE;
22345 }
22346 else if (id_equal (token->u.value, "final"))
22347 {
22348 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
22349 virt_specifier = VIRT_SPEC_FINAL;
22350 }
22351 else if (id_equal (token->u.value, "__final"))
22352 {
22353 virt_specifier = VIRT_SPEC_FINAL;
22354 }
22355 else
22356 break;
22357
22358 if (virt_specifiers & virt_specifier)
22359 {
22360 gcc_rich_location richloc (token->location);
22361 richloc.add_fixit_remove ();
22362 error_at (&richloc, "duplicate virt-specifier");
22363 cp_lexer_purge_token (parser->lexer);
22364 }
22365 else
22366 {
22367 cp_lexer_consume_token (parser->lexer);
22368 virt_specifiers |= virt_specifier;
22369 }
22370 }
22371 return virt_specifiers;
22372 }
22373
22374 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
22375 is in scope even though it isn't real. */
22376
22377 void
22378 inject_this_parameter (tree ctype, cp_cv_quals quals)
22379 {
22380 tree this_parm;
22381
22382 if (current_class_ptr)
22383 {
22384 /* We don't clear this between NSDMIs. Is it already what we want? */
22385 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
22386 if (DECL_P (current_class_ptr)
22387 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
22388 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
22389 && cp_type_quals (type) == quals)
22390 return;
22391 }
22392
22393 this_parm = build_this_parm (NULL_TREE, ctype, quals);
22394 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
22395 current_class_ptr = NULL_TREE;
22396 current_class_ref
22397 = cp_build_fold_indirect_ref (this_parm);
22398 current_class_ptr = this_parm;
22399 }
22400
22401 /* Return true iff our current scope is a non-static data member
22402 initializer. */
22403
22404 bool
22405 parsing_nsdmi (void)
22406 {
22407 /* We recognize NSDMI context by the context-less 'this' pointer set up
22408 by the function above. */
22409 if (current_class_ptr
22410 && TREE_CODE (current_class_ptr) == PARM_DECL
22411 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
22412 return true;
22413 return false;
22414 }
22415
22416 /* Parse a late-specified return type, if any. This is not a separate
22417 non-terminal, but part of a function declarator, which looks like
22418
22419 -> trailing-type-specifier-seq abstract-declarator(opt)
22420
22421 Returns the type indicated by the type-id.
22422
22423 In addition to this, parse any queued up #pragma omp declare simd
22424 clauses, and #pragma acc routine clauses.
22425
22426 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
22427 function. */
22428
22429 static tree
22430 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
22431 tree& requires_clause)
22432 {
22433 cp_token *token;
22434 tree type = NULL_TREE;
22435 bool declare_simd_p = (parser->omp_declare_simd
22436 && declarator
22437 && declarator->kind == cdk_id);
22438
22439 bool oacc_routine_p = (parser->oacc_routine
22440 && declarator
22441 && declarator->kind == cdk_id);
22442
22443 /* Peek at the next token. */
22444 token = cp_lexer_peek_token (parser->lexer);
22445 /* A late-specified return type is indicated by an initial '->'. */
22446 if (token->type != CPP_DEREF
22447 && token->keyword != RID_REQUIRES
22448 && !(token->type == CPP_NAME
22449 && token->u.value == ridpointers[RID_REQUIRES])
22450 && !(declare_simd_p || oacc_routine_p))
22451 return NULL_TREE;
22452
22453 if (token->type == CPP_DEREF)
22454 {
22455 /* Consume the ->. */
22456 cp_lexer_consume_token (parser->lexer);
22457
22458 type = cp_parser_trailing_type_id (parser);
22459 }
22460
22461 /* Function declarations may be followed by a trailing
22462 requires-clause. */
22463 requires_clause = cp_parser_requires_clause_opt (parser, false);
22464
22465 if (declare_simd_p)
22466 declarator->attributes
22467 = cp_parser_late_parsing_omp_declare_simd (parser,
22468 declarator->attributes);
22469 if (oacc_routine_p)
22470 declarator->attributes
22471 = cp_parser_late_parsing_oacc_routine (parser,
22472 declarator->attributes);
22473
22474 return type;
22475 }
22476
22477 /* Parse a declarator-id.
22478
22479 declarator-id:
22480 id-expression
22481 :: [opt] nested-name-specifier [opt] type-name
22482
22483 In the `id-expression' case, the value returned is as for
22484 cp_parser_id_expression if the id-expression was an unqualified-id.
22485 If the id-expression was a qualified-id, then a SCOPE_REF is
22486 returned. The first operand is the scope (either a NAMESPACE_DECL
22487 or TREE_TYPE), but the second is still just a representation of an
22488 unqualified-id. */
22489
22490 static tree
22491 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
22492 {
22493 tree id;
22494 /* The expression must be an id-expression. Assume that qualified
22495 names are the names of types so that:
22496
22497 template <class T>
22498 int S<T>::R::i = 3;
22499
22500 will work; we must treat `S<T>::R' as the name of a type.
22501 Similarly, assume that qualified names are templates, where
22502 required, so that:
22503
22504 template <class T>
22505 int S<T>::R<T>::i = 3;
22506
22507 will work, too. */
22508 id = cp_parser_id_expression (parser,
22509 /*template_keyword_p=*/false,
22510 /*check_dependency_p=*/false,
22511 /*template_p=*/NULL,
22512 /*declarator_p=*/true,
22513 optional_p);
22514 if (id && BASELINK_P (id))
22515 id = BASELINK_FUNCTIONS (id);
22516 return id;
22517 }
22518
22519 /* Parse a type-id.
22520
22521 type-id:
22522 type-specifier-seq abstract-declarator [opt]
22523
22524 The parser flags FLAGS is used to control type-specifier parsing.
22525
22526 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
22527
22528 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
22529 i.e. we've just seen "->".
22530
22531 Returns the TYPE specified. */
22532
22533 static tree
22534 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
22535 bool is_template_arg, bool is_trailing_return,
22536 location_t *type_location)
22537 {
22538 cp_decl_specifier_seq type_specifier_seq;
22539 cp_declarator *abstract_declarator;
22540
22541 /* Parse the type-specifier-seq. */
22542 cp_parser_type_specifier_seq (parser, flags,
22543 /*is_declaration=*/false,
22544 is_trailing_return,
22545 &type_specifier_seq);
22546 if (type_location)
22547 *type_location = type_specifier_seq.locations[ds_type_spec];
22548
22549 if (is_template_arg && type_specifier_seq.type
22550 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
22551 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
22552 /* A bare template name as a template argument is a template template
22553 argument, not a placeholder, so fail parsing it as a type argument. */
22554 {
22555 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
22556 cp_parser_simulate_error (parser);
22557 return error_mark_node;
22558 }
22559 if (type_specifier_seq.type == error_mark_node)
22560 return error_mark_node;
22561
22562 /* There might or might not be an abstract declarator. */
22563 cp_parser_parse_tentatively (parser);
22564 /* Look for the declarator. */
22565 abstract_declarator
22566 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
22567 CP_PARSER_FLAGS_NONE, NULL,
22568 /*parenthesized_p=*/NULL,
22569 /*member_p=*/false,
22570 /*friend_p=*/false,
22571 /*static_p=*/false);
22572 /* Check to see if there really was a declarator. */
22573 if (!cp_parser_parse_definitely (parser))
22574 abstract_declarator = NULL;
22575
22576 bool auto_typeid_ok = false;
22577 /* The concepts TS allows 'auto' as a type-id. */
22578 if (flag_concepts_ts)
22579 auto_typeid_ok = !parser->in_type_id_in_expr_p;
22580 /* DR 625 prohibits use of auto as a template-argument. We allow 'auto'
22581 outside the template-argument-list context here only for the sake of
22582 diagnostic: grokdeclarator then can emit a better error message for
22583 e.g. using T = auto. */
22584 else if (flag_concepts)
22585 auto_typeid_ok = (!parser->in_type_id_in_expr_p
22586 && !parser->in_template_argument_list_p);
22587
22588 if (type_specifier_seq.type
22589 && !auto_typeid_ok
22590 /* None of the valid uses of 'auto' in C++14 involve the type-id
22591 nonterminal, but it is valid in a trailing-return-type. */
22592 && !(cxx_dialect >= cxx14 && is_trailing_return))
22593 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
22594 {
22595 /* A type-id with type 'auto' is only ok if the abstract declarator
22596 is a function declarator with a late-specified return type.
22597
22598 A type-id with 'auto' is also valid in a trailing-return-type
22599 in a compound-requirement. */
22600 if (abstract_declarator
22601 && abstract_declarator->kind == cdk_function
22602 && abstract_declarator->u.function.late_return_type)
22603 /* OK */;
22604 else if (parser->in_result_type_constraint_p)
22605 /* OK */;
22606 else
22607 {
22608 location_t loc = type_specifier_seq.locations[ds_type_spec];
22609 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
22610 {
22611 error_at (loc, "missing template arguments after %qT",
22612 auto_node);
22613 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
22614 tmpl);
22615 }
22616 else if (parser->in_template_argument_list_p)
22617 error_at (loc, "%qT not permitted in template argument",
22618 auto_node);
22619 else
22620 error_at (loc, "invalid use of %qT", auto_node);
22621 return error_mark_node;
22622 }
22623 }
22624
22625 return groktypename (&type_specifier_seq, abstract_declarator,
22626 is_template_arg);
22627 }
22628
22629 /* Wrapper for cp_parser_type_id_1. */
22630
22631 static tree
22632 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
22633 location_t *type_location)
22634 {
22635 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
22636 }
22637
22638 /* Wrapper for cp_parser_type_id_1. */
22639
22640 static tree
22641 cp_parser_template_type_arg (cp_parser *parser)
22642 {
22643 tree r;
22644 const char *saved_message = parser->type_definition_forbidden_message;
22645 parser->type_definition_forbidden_message
22646 = G_("types may not be defined in template arguments");
22647 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
22648 parser->type_definition_forbidden_message = saved_message;
22649 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
22650 {
22651 error ("invalid use of %<auto%> in template argument");
22652 r = error_mark_node;
22653 }
22654 return r;
22655 }
22656
22657 /* Wrapper for cp_parser_type_id_1. */
22658
22659 static tree
22660 cp_parser_trailing_type_id (cp_parser *parser)
22661 {
22662 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
22663 false, true, NULL);
22664 }
22665
22666 /* Parse a type-specifier-seq.
22667
22668 type-specifier-seq:
22669 type-specifier type-specifier-seq [opt]
22670
22671 GNU extension:
22672
22673 type-specifier-seq:
22674 attributes type-specifier-seq [opt]
22675
22676 The parser flags FLAGS is used to control type-specifier parsing.
22677
22678 If IS_DECLARATION is true, we are at the start of a "condition" or
22679 exception-declaration, so we might be followed by a declarator-id.
22680
22681 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
22682 i.e. we've just seen "->".
22683
22684 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
22685
22686 static void
22687 cp_parser_type_specifier_seq (cp_parser* parser,
22688 cp_parser_flags flags,
22689 bool is_declaration,
22690 bool is_trailing_return,
22691 cp_decl_specifier_seq *type_specifier_seq)
22692 {
22693 bool seen_type_specifier = false;
22694 cp_token *start_token = NULL;
22695
22696 /* Clear the TYPE_SPECIFIER_SEQ. */
22697 clear_decl_specs (type_specifier_seq);
22698
22699 flags |= CP_PARSER_FLAGS_OPTIONAL;
22700 /* In the context of a trailing return type, enum E { } is an
22701 elaborated-type-specifier followed by a function-body, not an
22702 enum-specifier. */
22703 if (is_trailing_return)
22704 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
22705
22706 /* Parse the type-specifiers and attributes. */
22707 while (true)
22708 {
22709 tree type_specifier;
22710 bool is_cv_qualifier;
22711
22712 /* Check for attributes first. */
22713 if (cp_next_tokens_can_be_attribute_p (parser))
22714 {
22715 /* GNU attributes at the end of a declaration apply to the
22716 declaration as a whole, not to the trailing return type. So look
22717 ahead to see if these attributes are at the end. */
22718 if (seen_type_specifier && is_trailing_return
22719 && cp_next_tokens_can_be_gnu_attribute_p (parser))
22720 {
22721 size_t n = cp_parser_skip_attributes_opt (parser, 1);
22722 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
22723 if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
22724 || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
22725 break;
22726 }
22727 type_specifier_seq->attributes
22728 = attr_chainon (type_specifier_seq->attributes,
22729 cp_parser_attributes_opt (parser));
22730 continue;
22731 }
22732
22733 /* record the token of the beginning of the type specifier seq,
22734 for error reporting purposes*/
22735 if (!start_token)
22736 start_token = cp_lexer_peek_token (parser->lexer);
22737
22738 /* Look for the type-specifier. */
22739 type_specifier = cp_parser_type_specifier (parser,
22740 flags,
22741 type_specifier_seq,
22742 /*is_declaration=*/false,
22743 NULL,
22744 &is_cv_qualifier);
22745 if (!type_specifier)
22746 {
22747 /* If the first type-specifier could not be found, this is not a
22748 type-specifier-seq at all. */
22749 if (!seen_type_specifier)
22750 {
22751 /* Set in_declarator_p to avoid skipping to the semicolon. */
22752 int in_decl = parser->in_declarator_p;
22753 parser->in_declarator_p = true;
22754
22755 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
22756 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
22757 cp_parser_error (parser, "expected type-specifier");
22758
22759 parser->in_declarator_p = in_decl;
22760
22761 type_specifier_seq->type = error_mark_node;
22762 return;
22763 }
22764 /* If subsequent type-specifiers could not be found, the
22765 type-specifier-seq is complete. */
22766 break;
22767 }
22768
22769 seen_type_specifier = true;
22770 /* The standard says that a condition can be:
22771
22772 type-specifier-seq declarator = assignment-expression
22773
22774 However, given:
22775
22776 struct S {};
22777 if (int S = ...)
22778
22779 we should treat the "S" as a declarator, not as a
22780 type-specifier. The standard doesn't say that explicitly for
22781 type-specifier-seq, but it does say that for
22782 decl-specifier-seq in an ordinary declaration. Perhaps it
22783 would be clearer just to allow a decl-specifier-seq here, and
22784 then add a semantic restriction that if any decl-specifiers
22785 that are not type-specifiers appear, the program is invalid. */
22786 if (is_declaration && !is_cv_qualifier)
22787 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
22788 }
22789 }
22790
22791 /* Return whether the function currently being declared has an associated
22792 template parameter list. */
22793
22794 static bool
22795 function_being_declared_is_template_p (cp_parser* parser)
22796 {
22797 if (!current_template_parms || processing_template_parmlist)
22798 return false;
22799
22800 if (parser->implicit_template_scope)
22801 return true;
22802
22803 if (at_class_scope_p ()
22804 && TYPE_BEING_DEFINED (current_class_type))
22805 return parser->num_template_parameter_lists != 0;
22806
22807 return ((int) parser->num_template_parameter_lists > template_class_depth
22808 (current_class_type));
22809 }
22810
22811 /* Parse a parameter-declaration-clause.
22812
22813 parameter-declaration-clause:
22814 parameter-declaration-list [opt] ... [opt]
22815 parameter-declaration-list , ...
22816
22817 The parser flags FLAGS is used to control type-specifier parsing.
22818
22819 Returns a representation for the parameter declarations. A return
22820 value of NULL indicates a parameter-declaration-clause consisting
22821 only of an ellipsis. */
22822
22823 static tree
22824 cp_parser_parameter_declaration_clause (cp_parser* parser,
22825 cp_parser_flags flags)
22826 {
22827 tree parameters;
22828 cp_token *token;
22829 bool ellipsis_p;
22830
22831 temp_override<bool> cleanup
22832 (parser->auto_is_implicit_function_template_parm_p);
22833
22834 if (!processing_specialization
22835 && !processing_template_parmlist
22836 && !processing_explicit_instantiation
22837 /* default_arg_ok_p tracks whether this is a parameter-clause for an
22838 actual function or a random abstract declarator. */
22839 && parser->default_arg_ok_p)
22840 if (!current_function_decl
22841 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
22842 parser->auto_is_implicit_function_template_parm_p = true;
22843
22844 /* Peek at the next token. */
22845 token = cp_lexer_peek_token (parser->lexer);
22846 /* Check for trivial parameter-declaration-clauses. */
22847 if (token->type == CPP_ELLIPSIS)
22848 {
22849 /* Consume the `...' token. */
22850 cp_lexer_consume_token (parser->lexer);
22851 return NULL_TREE;
22852 }
22853 else if (token->type == CPP_CLOSE_PAREN)
22854 /* There are no parameters. */
22855 return void_list_node;
22856 /* Check for `(void)', too, which is a special case. */
22857 else if (token->keyword == RID_VOID
22858 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22859 == CPP_CLOSE_PAREN))
22860 {
22861 /* Consume the `void' token. */
22862 cp_lexer_consume_token (parser->lexer);
22863 /* There are no parameters. */
22864 return explicit_void_list_node;
22865 }
22866
22867 /* Parse the parameter-declaration-list. */
22868 parameters = cp_parser_parameter_declaration_list (parser, flags);
22869 /* If a parse error occurred while parsing the
22870 parameter-declaration-list, then the entire
22871 parameter-declaration-clause is erroneous. */
22872 if (parameters == error_mark_node)
22873 return NULL_TREE;
22874
22875 /* Peek at the next token. */
22876 token = cp_lexer_peek_token (parser->lexer);
22877 /* If it's a `,', the clause should terminate with an ellipsis. */
22878 if (token->type == CPP_COMMA)
22879 {
22880 /* Consume the `,'. */
22881 cp_lexer_consume_token (parser->lexer);
22882 /* Expect an ellipsis. */
22883 ellipsis_p
22884 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
22885 }
22886 /* It might also be `...' if the optional trailing `,' was
22887 omitted. */
22888 else if (token->type == CPP_ELLIPSIS)
22889 {
22890 /* Consume the `...' token. */
22891 cp_lexer_consume_token (parser->lexer);
22892 /* And remember that we saw it. */
22893 ellipsis_p = true;
22894 }
22895 else
22896 ellipsis_p = false;
22897
22898 /* Finish the parameter list. */
22899 if (!ellipsis_p)
22900 parameters = chainon (parameters, void_list_node);
22901
22902 return parameters;
22903 }
22904
22905 /* Parse a parameter-declaration-list.
22906
22907 parameter-declaration-list:
22908 parameter-declaration
22909 parameter-declaration-list , parameter-declaration
22910
22911 The parser flags FLAGS is used to control type-specifier parsing.
22912
22913 Returns a representation of the parameter-declaration-list, as for
22914 cp_parser_parameter_declaration_clause. However, the
22915 `void_list_node' is never appended to the list. */
22916
22917 static tree
22918 cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
22919 {
22920 tree parameters = NULL_TREE;
22921 tree *tail = &parameters;
22922 bool saved_in_unbraced_linkage_specification_p;
22923 int index = 0;
22924
22925 /* The special considerations that apply to a function within an
22926 unbraced linkage specifications do not apply to the parameters
22927 to the function. */
22928 saved_in_unbraced_linkage_specification_p
22929 = parser->in_unbraced_linkage_specification_p;
22930 parser->in_unbraced_linkage_specification_p = false;
22931
22932 /* Look for more parameters. */
22933 while (true)
22934 {
22935 cp_parameter_declarator *parameter;
22936 tree decl = error_mark_node;
22937 bool parenthesized_p = false;
22938
22939 /* Parse the parameter. */
22940 parameter
22941 = cp_parser_parameter_declaration (parser, flags,
22942 /*template_parm_p=*/false,
22943 &parenthesized_p);
22944
22945 /* We don't know yet if the enclosing context is deprecated, so wait
22946 and warn in grokparms if appropriate. */
22947 deprecated_state = DEPRECATED_SUPPRESS;
22948
22949 if (parameter)
22950 {
22951 decl = grokdeclarator (parameter->declarator,
22952 &parameter->decl_specifiers,
22953 PARM,
22954 parameter->default_argument != NULL_TREE,
22955 &parameter->decl_specifiers.attributes);
22956 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
22957 DECL_SOURCE_LOCATION (decl) = parameter->loc;
22958 }
22959
22960 deprecated_state = DEPRECATED_NORMAL;
22961
22962 /* If a parse error occurred parsing the parameter declaration,
22963 then the entire parameter-declaration-list is erroneous. */
22964 if (decl == error_mark_node)
22965 {
22966 parameters = error_mark_node;
22967 break;
22968 }
22969
22970 if (parameter->decl_specifiers.attributes)
22971 cplus_decl_attributes (&decl,
22972 parameter->decl_specifiers.attributes,
22973 0);
22974 if (DECL_NAME (decl))
22975 decl = pushdecl (decl);
22976
22977 if (decl != error_mark_node)
22978 {
22979 retrofit_lang_decl (decl);
22980 DECL_PARM_INDEX (decl) = ++index;
22981 DECL_PARM_LEVEL (decl) = function_parm_depth ();
22982 }
22983
22984 /* Add the new parameter to the list. */
22985 *tail = build_tree_list (parameter->default_argument, decl);
22986 tail = &TREE_CHAIN (*tail);
22987
22988 /* If the parameters were parenthesized, it's the case of
22989 T foo(X(x)) which looks like a variable definition but
22990 is a function declaration. */
22991 if (index == 1 || PARENTHESIZED_LIST_P (parameters))
22992 PARENTHESIZED_LIST_P (parameters) = parenthesized_p;
22993
22994 /* Peek at the next token. */
22995 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
22996 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
22997 /* These are for Objective-C++ */
22998 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22999 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23000 /* The parameter-declaration-list is complete. */
23001 break;
23002 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23003 {
23004 cp_token *token;
23005
23006 /* Peek at the next token. */
23007 token = cp_lexer_peek_nth_token (parser->lexer, 2);
23008 /* If it's an ellipsis, then the list is complete. */
23009 if (token->type == CPP_ELLIPSIS)
23010 break;
23011 /* Otherwise, there must be more parameters. Consume the
23012 `,'. */
23013 cp_lexer_consume_token (parser->lexer);
23014 /* When parsing something like:
23015
23016 int i(float f, double d)
23017
23018 we can tell after seeing the declaration for "f" that we
23019 are not looking at an initialization of a variable "i",
23020 but rather at the declaration of a function "i".
23021
23022 Due to the fact that the parsing of template arguments
23023 (as specified to a template-id) requires backtracking we
23024 cannot use this technique when inside a template argument
23025 list. */
23026 if (!parser->in_template_argument_list_p
23027 && !parser->in_type_id_in_expr_p
23028 && cp_parser_uncommitted_to_tentative_parse_p (parser)
23029 /* However, a parameter-declaration of the form
23030 "float(f)" (which is a valid declaration of a
23031 parameter "f") can also be interpreted as an
23032 expression (the conversion of "f" to "float"). */
23033 && !parenthesized_p)
23034 cp_parser_commit_to_tentative_parse (parser);
23035 }
23036 else
23037 {
23038 cp_parser_error (parser, "expected %<,%> or %<...%>");
23039 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
23040 cp_parser_skip_to_closing_parenthesis (parser,
23041 /*recovering=*/true,
23042 /*or_comma=*/false,
23043 /*consume_paren=*/false);
23044 break;
23045 }
23046 }
23047
23048 parser->in_unbraced_linkage_specification_p
23049 = saved_in_unbraced_linkage_specification_p;
23050
23051 /* Reset implicit_template_scope if we are about to leave the function
23052 parameter list that introduced it. Note that for out-of-line member
23053 definitions, there will be one or more class scopes before we get to
23054 the template parameter scope. */
23055
23056 if (cp_binding_level *its = parser->implicit_template_scope)
23057 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
23058 {
23059 while (maybe_its->kind == sk_class)
23060 maybe_its = maybe_its->level_chain;
23061 if (maybe_its == its)
23062 {
23063 parser->implicit_template_parms = 0;
23064 parser->implicit_template_scope = 0;
23065 }
23066 }
23067
23068 return parameters;
23069 }
23070
23071 /* Parse a parameter declaration.
23072
23073 parameter-declaration:
23074 decl-specifier-seq ... [opt] declarator
23075 decl-specifier-seq declarator = assignment-expression
23076 decl-specifier-seq ... [opt] abstract-declarator [opt]
23077 decl-specifier-seq abstract-declarator [opt] = assignment-expression
23078
23079 The parser flags FLAGS is used to control type-specifier parsing.
23080
23081 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
23082 declares a template parameter. (In that case, a non-nested `>'
23083 token encountered during the parsing of the assignment-expression
23084 is not interpreted as a greater-than operator.)
23085
23086 Returns a representation of the parameter, or NULL if an error
23087 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
23088 true iff the declarator is of the form "(p)". */
23089
23090 static cp_parameter_declarator *
23091 cp_parser_parameter_declaration (cp_parser *parser,
23092 cp_parser_flags flags,
23093 bool template_parm_p,
23094 bool *parenthesized_p)
23095 {
23096 int declares_class_or_enum;
23097 cp_decl_specifier_seq decl_specifiers;
23098 cp_declarator *declarator;
23099 tree default_argument;
23100 cp_token *token = NULL, *declarator_token_start = NULL;
23101 const char *saved_message;
23102 bool template_parameter_pack_p = false;
23103
23104 /* In a template parameter, `>' is not an operator.
23105
23106 [temp.param]
23107
23108 When parsing a default template-argument for a non-type
23109 template-parameter, the first non-nested `>' is taken as the end
23110 of the template parameter-list rather than a greater-than
23111 operator. */
23112
23113 /* Type definitions may not appear in parameter types. */
23114 saved_message = parser->type_definition_forbidden_message;
23115 parser->type_definition_forbidden_message
23116 = G_("types may not be defined in parameter types");
23117
23118 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
23119 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
23120 (current_template_parms)) : 0);
23121
23122 /* Parse the declaration-specifiers. */
23123 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23124 cp_parser_decl_specifier_seq (parser,
23125 flags,
23126 &decl_specifiers,
23127 &declares_class_or_enum);
23128
23129 /* Complain about missing 'typename' or other invalid type names. */
23130 if (!decl_specifiers.any_type_specifiers_p
23131 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23132 decl_specifiers.type = error_mark_node;
23133
23134 /* If an error occurred, there's no reason to attempt to parse the
23135 rest of the declaration. */
23136 if (cp_parser_error_occurred (parser))
23137 {
23138 parser->type_definition_forbidden_message = saved_message;
23139 return NULL;
23140 }
23141
23142 /* Peek at the next token. */
23143 token = cp_lexer_peek_token (parser->lexer);
23144
23145 /* If the next token is a `)', `,', `=', `>', or `...', then there
23146 is no declarator. However, when variadic templates are enabled,
23147 there may be a declarator following `...'. */
23148 if (token->type == CPP_CLOSE_PAREN
23149 || token->type == CPP_COMMA
23150 || token->type == CPP_EQ
23151 || token->type == CPP_GREATER)
23152 {
23153 declarator = NULL;
23154 if (parenthesized_p)
23155 *parenthesized_p = false;
23156 }
23157 /* Otherwise, there should be a declarator. */
23158 else
23159 {
23160 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
23161 parser->default_arg_ok_p = false;
23162
23163 /* After seeing a decl-specifier-seq, if the next token is not a
23164 "(", there is no possibility that the code is a valid
23165 expression. Therefore, if parsing tentatively, we commit at
23166 this point. */
23167 if (!parser->in_template_argument_list_p
23168 /* In an expression context, having seen:
23169
23170 (int((char ...
23171
23172 we cannot be sure whether we are looking at a
23173 function-type (taking a "char" as a parameter) or a cast
23174 of some object of type "char" to "int". */
23175 && !parser->in_type_id_in_expr_p
23176 && cp_parser_uncommitted_to_tentative_parse_p (parser)
23177 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
23178 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
23179 cp_parser_commit_to_tentative_parse (parser);
23180 /* Parse the declarator. */
23181 declarator_token_start = token;
23182 declarator = cp_parser_declarator (parser,
23183 CP_PARSER_DECLARATOR_EITHER,
23184 CP_PARSER_FLAGS_NONE,
23185 /*ctor_dtor_or_conv_p=*/NULL,
23186 parenthesized_p,
23187 /*member_p=*/false,
23188 /*friend_p=*/false,
23189 /*static_p=*/false);
23190 parser->default_arg_ok_p = saved_default_arg_ok_p;
23191 /* After the declarator, allow more attributes. */
23192 decl_specifiers.attributes
23193 = attr_chainon (decl_specifiers.attributes,
23194 cp_parser_attributes_opt (parser));
23195
23196 /* If the declarator is a template parameter pack, remember that and
23197 clear the flag in the declarator itself so we don't get errors
23198 from grokdeclarator. */
23199 if (template_parm_p && declarator && declarator->parameter_pack_p)
23200 {
23201 declarator->parameter_pack_p = false;
23202 template_parameter_pack_p = true;
23203 }
23204 }
23205
23206 /* If the next token is an ellipsis, and we have not seen a declarator
23207 name, and if either the type of the declarator contains parameter
23208 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
23209 for, eg, abbreviated integral type names), then we actually have a
23210 parameter pack expansion expression. Otherwise, leave the ellipsis
23211 for a C-style variadic function. */
23212 token = cp_lexer_peek_token (parser->lexer);
23213
23214 /* If a function parameter pack was specified and an implicit template
23215 parameter was introduced during cp_parser_parameter_declaration,
23216 change any implicit parameters introduced into packs. */
23217 if (parser->implicit_template_parms
23218 && ((token->type == CPP_ELLIPSIS
23219 && declarator_can_be_parameter_pack (declarator))
23220 || (declarator && declarator->parameter_pack_p)))
23221 {
23222 int latest_template_parm_idx = TREE_VEC_LENGTH
23223 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
23224
23225 if (latest_template_parm_idx != template_parm_idx)
23226 decl_specifiers.type = convert_generic_types_to_packs
23227 (decl_specifiers.type,
23228 template_parm_idx, latest_template_parm_idx);
23229 }
23230
23231 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23232 {
23233 tree type = decl_specifiers.type;
23234
23235 if (type && DECL_P (type))
23236 type = TREE_TYPE (type);
23237
23238 if (((type
23239 && TREE_CODE (type) != TYPE_PACK_EXPANSION
23240 && (template_parm_p || uses_parameter_packs (type)))
23241 || (!type && template_parm_p))
23242 && declarator_can_be_parameter_pack (declarator))
23243 {
23244 /* Consume the `...'. */
23245 cp_lexer_consume_token (parser->lexer);
23246 maybe_warn_variadic_templates ();
23247
23248 /* Build a pack expansion type */
23249 if (template_parm_p)
23250 template_parameter_pack_p = true;
23251 else if (declarator)
23252 declarator->parameter_pack_p = true;
23253 else
23254 decl_specifiers.type = make_pack_expansion (type);
23255 }
23256 }
23257
23258 /* The restriction on defining new types applies only to the type
23259 of the parameter, not to the default argument. */
23260 parser->type_definition_forbidden_message = saved_message;
23261
23262 /* If the next token is `=', then process a default argument. */
23263 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23264 {
23265 tree type = decl_specifiers.type;
23266 token = cp_lexer_peek_token (parser->lexer);
23267 /* If we are defining a class, then the tokens that make up the
23268 default argument must be saved and processed later. */
23269 if (!template_parm_p && at_class_scope_p ()
23270 && TYPE_BEING_DEFINED (current_class_type)
23271 && !LAMBDA_TYPE_P (current_class_type))
23272 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
23273
23274 /* A constrained-type-specifier may declare a type
23275 template-parameter. */
23276 else if (declares_constrained_type_template_parameter (type))
23277 default_argument
23278 = cp_parser_default_type_template_argument (parser);
23279
23280 /* A constrained-type-specifier may declare a
23281 template-template-parameter. */
23282 else if (declares_constrained_template_template_parameter (type))
23283 default_argument
23284 = cp_parser_default_template_template_argument (parser);
23285
23286 /* Outside of a class definition, we can just parse the
23287 assignment-expression. */
23288 else
23289 default_argument
23290 = cp_parser_default_argument (parser, template_parm_p);
23291
23292 if (!parser->default_arg_ok_p)
23293 {
23294 permerror (token->location,
23295 "default arguments are only "
23296 "permitted for function parameters");
23297 }
23298 else if ((declarator && declarator->parameter_pack_p)
23299 || template_parameter_pack_p
23300 || (decl_specifiers.type
23301 && PACK_EXPANSION_P (decl_specifiers.type)))
23302 {
23303 /* Find the name of the parameter pack. */
23304 cp_declarator *id_declarator = declarator;
23305 while (id_declarator && id_declarator->kind != cdk_id)
23306 id_declarator = id_declarator->declarator;
23307
23308 if (id_declarator && id_declarator->kind == cdk_id)
23309 error_at (declarator_token_start->location,
23310 template_parm_p
23311 ? G_("template parameter pack %qD "
23312 "cannot have a default argument")
23313 : G_("parameter pack %qD cannot have "
23314 "a default argument"),
23315 id_declarator->u.id.unqualified_name);
23316 else
23317 error_at (declarator_token_start->location,
23318 template_parm_p
23319 ? G_("template parameter pack cannot have "
23320 "a default argument")
23321 : G_("parameter pack cannot have a "
23322 "default argument"));
23323
23324 default_argument = NULL_TREE;
23325 }
23326 }
23327 else
23328 default_argument = NULL_TREE;
23329
23330 if (default_argument)
23331 STRIP_ANY_LOCATION_WRAPPER (default_argument);
23332
23333 /* Generate a location for the parameter, ranging from the start of the
23334 initial token to the end of the final token (using input_location for
23335 the latter, set up by cp_lexer_set_source_position_from_token when
23336 consuming tokens).
23337
23338 If we have a identifier, then use it for the caret location, e.g.
23339
23340 extern int callee (int one, int (*two)(int, int), float three);
23341 ~~~~~~^~~~~~~~~~~~~~
23342
23343 otherwise, reuse the start location for the caret location e.g.:
23344
23345 extern int callee (int one, int (*)(int, int), float three);
23346 ^~~~~~~~~~~~~~~~~
23347
23348 */
23349 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
23350 ? declarator->id_loc
23351 : decl_spec_token_start->location);
23352 location_t param_loc = make_location (caret_loc,
23353 decl_spec_token_start->location,
23354 input_location);
23355
23356 return make_parameter_declarator (&decl_specifiers,
23357 declarator,
23358 default_argument,
23359 param_loc,
23360 template_parameter_pack_p);
23361 }
23362
23363 /* Parse a default argument and return it.
23364
23365 TEMPLATE_PARM_P is true if this is a default argument for a
23366 non-type template parameter. */
23367 static tree
23368 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
23369 {
23370 tree default_argument = NULL_TREE;
23371 bool saved_greater_than_is_operator_p;
23372 unsigned char saved_local_variables_forbidden_p;
23373 bool non_constant_p, is_direct_init;
23374
23375 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
23376 set correctly. */
23377 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
23378 parser->greater_than_is_operator_p = !template_parm_p;
23379 /* Local variable names (and the `this' keyword) may not
23380 appear in a default argument. */
23381 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23382 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
23383 /* Parse the assignment-expression. */
23384 if (template_parm_p)
23385 push_deferring_access_checks (dk_no_deferred);
23386 tree saved_class_ptr = NULL_TREE;
23387 tree saved_class_ref = NULL_TREE;
23388 /* The "this" pointer is not valid in a default argument. */
23389 if (cfun)
23390 {
23391 saved_class_ptr = current_class_ptr;
23392 cp_function_chain->x_current_class_ptr = NULL_TREE;
23393 saved_class_ref = current_class_ref;
23394 cp_function_chain->x_current_class_ref = NULL_TREE;
23395 }
23396 default_argument
23397 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
23398 /* Restore the "this" pointer. */
23399 if (cfun)
23400 {
23401 cp_function_chain->x_current_class_ptr = saved_class_ptr;
23402 cp_function_chain->x_current_class_ref = saved_class_ref;
23403 }
23404 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
23405 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23406 if (template_parm_p)
23407 pop_deferring_access_checks ();
23408 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
23409 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
23410
23411 return default_argument;
23412 }
23413
23414 /* Parse a function-body.
23415
23416 function-body:
23417 compound_statement */
23418
23419 static void
23420 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
23421 {
23422 cp_parser_compound_statement (parser, NULL, (in_function_try_block
23423 ? BCS_TRY_BLOCK : BCS_NORMAL),
23424 true);
23425 }
23426
23427 /* Parse a ctor-initializer-opt followed by a function-body. Return
23428 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
23429 is true we are parsing a function-try-block. */
23430
23431 static void
23432 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
23433 bool in_function_try_block)
23434 {
23435 tree body, list;
23436 const bool check_body_p
23437 = (DECL_CONSTRUCTOR_P (current_function_decl)
23438 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
23439 tree last = NULL;
23440
23441 if (in_function_try_block
23442 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
23443 && cxx_dialect < cxx20)
23444 {
23445 if (DECL_CONSTRUCTOR_P (current_function_decl))
23446 pedwarn (input_location, 0,
23447 "function-try-block body of %<constexpr%> constructor only "
23448 "available with %<-std=c++20%> or %<-std=gnu++20%>");
23449 else
23450 pedwarn (input_location, 0,
23451 "function-try-block body of %<constexpr%> function only "
23452 "available with %<-std=c++20%> or %<-std=gnu++20%>");
23453 }
23454
23455 /* Begin the function body. */
23456 body = begin_function_body ();
23457 /* Parse the optional ctor-initializer. */
23458 cp_parser_ctor_initializer_opt (parser);
23459
23460 /* If we're parsing a constexpr constructor definition, we need
23461 to check that the constructor body is indeed empty. However,
23462 before we get to cp_parser_function_body lot of junk has been
23463 generated, so we can't just check that we have an empty block.
23464 Rather we take a snapshot of the outermost block, and check whether
23465 cp_parser_function_body changed its state. */
23466 if (check_body_p)
23467 {
23468 list = cur_stmt_list;
23469 if (STATEMENT_LIST_TAIL (list))
23470 last = STATEMENT_LIST_TAIL (list)->stmt;
23471 }
23472 /* Parse the function-body. */
23473 cp_parser_function_body (parser, in_function_try_block);
23474 if (check_body_p)
23475 check_constexpr_ctor_body (last, list, /*complain=*/true);
23476 /* Finish the function body. */
23477 finish_function_body (body);
23478 }
23479
23480 /* Parse an initializer.
23481
23482 initializer:
23483 = initializer-clause
23484 ( expression-list )
23485
23486 Returns an expression representing the initializer. If no
23487 initializer is present, NULL_TREE is returned.
23488
23489 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
23490 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
23491 set to TRUE if there is no initializer present. If there is an
23492 initializer, and it is not a constant-expression, *NON_CONSTANT_P
23493 is set to true; otherwise it is set to false. */
23494
23495 static tree
23496 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
23497 bool* non_constant_p, bool subexpression_p)
23498 {
23499 cp_token *token;
23500 tree init;
23501
23502 /* Peek at the next token. */
23503 token = cp_lexer_peek_token (parser->lexer);
23504
23505 /* Let our caller know whether or not this initializer was
23506 parenthesized. */
23507 *is_direct_init = (token->type != CPP_EQ);
23508 /* Assume that the initializer is constant. */
23509 *non_constant_p = false;
23510
23511 if (token->type == CPP_EQ)
23512 {
23513 /* Consume the `='. */
23514 cp_lexer_consume_token (parser->lexer);
23515 /* Parse the initializer-clause. */
23516 init = cp_parser_initializer_clause (parser, non_constant_p);
23517 }
23518 else if (token->type == CPP_OPEN_PAREN)
23519 {
23520 vec<tree, va_gc> *vec;
23521 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23522 /*cast_p=*/false,
23523 /*allow_expansion_p=*/true,
23524 non_constant_p);
23525 if (vec == NULL)
23526 return error_mark_node;
23527 init = build_tree_list_vec (vec);
23528 release_tree_vector (vec);
23529 }
23530 else if (token->type == CPP_OPEN_BRACE)
23531 {
23532 cp_lexer_set_source_position (parser->lexer);
23533 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23534 init = cp_parser_braced_list (parser, non_constant_p);
23535 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
23536 }
23537 else
23538 {
23539 /* Anything else is an error. */
23540 cp_parser_error (parser, "expected initializer");
23541 init = error_mark_node;
23542 }
23543
23544 if (!subexpression_p && check_for_bare_parameter_packs (init))
23545 init = error_mark_node;
23546
23547 return init;
23548 }
23549
23550 /* Parse an initializer-clause.
23551
23552 initializer-clause:
23553 assignment-expression
23554 braced-init-list
23555
23556 Returns an expression representing the initializer.
23557
23558 If the `assignment-expression' production is used the value
23559 returned is simply a representation for the expression.
23560
23561 Otherwise, calls cp_parser_braced_list. */
23562
23563 static cp_expr
23564 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
23565 {
23566 cp_expr initializer;
23567
23568 /* Assume the expression is constant. */
23569 *non_constant_p = false;
23570
23571 /* If it is not a `{', then we are looking at an
23572 assignment-expression. */
23573 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23574 {
23575 initializer
23576 = cp_parser_constant_expression (parser,
23577 /*allow_non_constant_p=*/true,
23578 non_constant_p);
23579 }
23580 else
23581 initializer = cp_parser_braced_list (parser, non_constant_p);
23582
23583 return initializer;
23584 }
23585
23586 /* Parse a brace-enclosed initializer list.
23587
23588 braced-init-list:
23589 { initializer-list , [opt] }
23590 { designated-initializer-list , [opt] }
23591 { }
23592
23593 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
23594 the elements of the initializer-list (or NULL, if the last
23595 production is used). The TREE_TYPE for the CONSTRUCTOR will be
23596 NULL_TREE. There is no way to detect whether or not the optional
23597 trailing `,' was provided. NON_CONSTANT_P is as for
23598 cp_parser_initializer. */
23599
23600 static cp_expr
23601 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
23602 {
23603 tree initializer;
23604 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
23605
23606 /* Consume the `{' token. */
23607 matching_braces braces;
23608 braces.require_open (parser);
23609 /* Create a CONSTRUCTOR to represent the braced-initializer. */
23610 initializer = make_node (CONSTRUCTOR);
23611 /* If it's not a `}', then there is a non-trivial initializer. */
23612 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
23613 {
23614 bool designated;
23615 /* Parse the initializer list. */
23616 CONSTRUCTOR_ELTS (initializer)
23617 = cp_parser_initializer_list (parser, non_constant_p, &designated);
23618 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
23619 /* A trailing `,' token is allowed. */
23620 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23621 cp_lexer_consume_token (parser->lexer);
23622 }
23623 else
23624 *non_constant_p = false;
23625 /* Now, there should be a trailing `}'. */
23626 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
23627 braces.require_close (parser);
23628 TREE_TYPE (initializer) = init_list_type_node;
23629
23630 cp_expr result (initializer);
23631 /* Build a location of the form:
23632 { ... }
23633 ^~~~~~~
23634 with caret==start at the open brace, finish at the close brace. */
23635 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
23636 result.set_location (combined_loc);
23637 return result;
23638 }
23639
23640 /* Consume tokens up to, and including, the next non-nested closing `]'.
23641 Returns true iff we found a closing `]'. */
23642
23643 static bool
23644 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
23645 {
23646 unsigned square_depth = 0;
23647
23648 while (true)
23649 {
23650 cp_token * token = cp_lexer_peek_token (parser->lexer);
23651
23652 switch (token->type)
23653 {
23654 case CPP_PRAGMA_EOL:
23655 if (!parser->lexer->in_pragma)
23656 break;
23657 /* FALLTHRU */
23658 case CPP_EOF:
23659 /* If we've run out of tokens, then there is no closing `]'. */
23660 return false;
23661
23662 case CPP_OPEN_SQUARE:
23663 ++square_depth;
23664 break;
23665
23666 case CPP_CLOSE_SQUARE:
23667 if (!square_depth--)
23668 {
23669 cp_lexer_consume_token (parser->lexer);
23670 return true;
23671 }
23672 break;
23673
23674 default:
23675 break;
23676 }
23677
23678 /* Consume the token. */
23679 cp_lexer_consume_token (parser->lexer);
23680 }
23681 }
23682
23683 /* Return true if we are looking at an array-designator, false otherwise. */
23684
23685 static bool
23686 cp_parser_array_designator_p (cp_parser *parser)
23687 {
23688 /* Consume the `['. */
23689 cp_lexer_consume_token (parser->lexer);
23690
23691 cp_lexer_save_tokens (parser->lexer);
23692
23693 /* Skip tokens until the next token is a closing square bracket.
23694 If we find the closing `]', and the next token is a `=', then
23695 we are looking at an array designator. */
23696 bool array_designator_p
23697 = (cp_parser_skip_to_closing_square_bracket (parser)
23698 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
23699
23700 /* Roll back the tokens we skipped. */
23701 cp_lexer_rollback_tokens (parser->lexer);
23702
23703 return array_designator_p;
23704 }
23705
23706 /* Parse an initializer-list.
23707
23708 initializer-list:
23709 initializer-clause ... [opt]
23710 initializer-list , initializer-clause ... [opt]
23711
23712 C++20 Extension:
23713
23714 designated-initializer-list:
23715 designated-initializer-clause
23716 designated-initializer-list , designated-initializer-clause
23717
23718 designated-initializer-clause:
23719 designator brace-or-equal-initializer
23720
23721 designator:
23722 . identifier
23723
23724 GNU Extension:
23725
23726 initializer-list:
23727 designation initializer-clause ...[opt]
23728 initializer-list , designation initializer-clause ...[opt]
23729
23730 designation:
23731 . identifier =
23732 identifier :
23733 [ constant-expression ] =
23734
23735 Returns a vec of constructor_elt. The VALUE of each elt is an expression
23736 for the initializer. If the INDEX of the elt is non-NULL, it is the
23737 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
23738 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
23739 are any designators. */
23740
23741 static vec<constructor_elt, va_gc> *
23742 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
23743 bool *designated)
23744 {
23745 vec<constructor_elt, va_gc> *v = NULL;
23746 bool first_p = true;
23747 tree first_designator = NULL_TREE;
23748
23749 /* Assume all of the expressions are constant. */
23750 *non_constant_p = false;
23751
23752 unsigned nelts = 0;
23753 int suppress = suppress_location_wrappers;
23754
23755 /* Parse the rest of the list. */
23756 while (true)
23757 {
23758 cp_token *token;
23759 tree designator;
23760 tree initializer;
23761 bool clause_non_constant_p;
23762 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23763
23764 /* Handle the C++20 syntax, '. id ='. */
23765 if ((cxx_dialect >= cxx20
23766 || cp_parser_allow_gnu_extensions_p (parser))
23767 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
23768 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
23769 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
23770 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
23771 == CPP_OPEN_BRACE)))
23772 {
23773 if (cxx_dialect < cxx20)
23774 pedwarn (loc, OPT_Wpedantic,
23775 "C++ designated initializers only available with "
23776 "%<-std=c++20%> or %<-std=gnu++20%>");
23777 /* Consume the `.'. */
23778 cp_lexer_consume_token (parser->lexer);
23779 /* Consume the identifier. */
23780 designator = cp_lexer_consume_token (parser->lexer)->u.value;
23781 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23782 /* Consume the `='. */
23783 cp_lexer_consume_token (parser->lexer);
23784 }
23785 /* Also, if the next token is an identifier and the following one is a
23786 colon, we are looking at the GNU designated-initializer
23787 syntax. */
23788 else if (cp_parser_allow_gnu_extensions_p (parser)
23789 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
23790 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23791 == CPP_COLON))
23792 {
23793 /* Warn the user that they are using an extension. */
23794 pedwarn (loc, OPT_Wpedantic,
23795 "ISO C++ does not allow GNU designated initializers");
23796 /* Consume the identifier. */
23797 designator = cp_lexer_consume_token (parser->lexer)->u.value;
23798 /* Consume the `:'. */
23799 cp_lexer_consume_token (parser->lexer);
23800 }
23801 /* Also handle C99 array designators, '[ const ] ='. */
23802 else if (cp_parser_allow_gnu_extensions_p (parser)
23803 && !c_dialect_objc ()
23804 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23805 {
23806 /* In C++11, [ could start a lambda-introducer. */
23807 bool non_const = false;
23808
23809 cp_parser_parse_tentatively (parser);
23810
23811 if (!cp_parser_array_designator_p (parser))
23812 {
23813 cp_parser_simulate_error (parser);
23814 designator = NULL_TREE;
23815 }
23816 else
23817 {
23818 designator = cp_parser_constant_expression (parser, true,
23819 &non_const);
23820 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23821 cp_parser_require (parser, CPP_EQ, RT_EQ);
23822 }
23823
23824 if (!cp_parser_parse_definitely (parser))
23825 designator = NULL_TREE;
23826 else if (non_const
23827 && (!require_potential_rvalue_constant_expression
23828 (designator)))
23829 designator = NULL_TREE;
23830 if (designator)
23831 /* Warn the user that they are using an extension. */
23832 pedwarn (loc, OPT_Wpedantic,
23833 "ISO C++ does not allow C99 designated initializers");
23834 }
23835 else
23836 designator = NULL_TREE;
23837
23838 if (first_p)
23839 {
23840 first_designator = designator;
23841 first_p = false;
23842 }
23843 else if (cxx_dialect >= cxx20
23844 && first_designator != error_mark_node
23845 && (!first_designator != !designator))
23846 {
23847 error_at (loc, "either all initializer clauses should be designated "
23848 "or none of them should be");
23849 first_designator = error_mark_node;
23850 }
23851 else if (cxx_dialect < cxx20 && !first_designator)
23852 first_designator = designator;
23853
23854 /* Parse the initializer. */
23855 initializer = cp_parser_initializer_clause (parser,
23856 &clause_non_constant_p);
23857 /* If any clause is non-constant, so is the entire initializer. */
23858 if (clause_non_constant_p)
23859 *non_constant_p = true;
23860
23861 /* If we have an ellipsis, this is an initializer pack
23862 expansion. */
23863 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23864 {
23865 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23866
23867 /* Consume the `...'. */
23868 cp_lexer_consume_token (parser->lexer);
23869
23870 if (designator && cxx_dialect >= cxx20)
23871 error_at (loc,
23872 "%<...%> not allowed in designated initializer list");
23873
23874 /* Turn the initializer into an initializer expansion. */
23875 initializer = make_pack_expansion (initializer);
23876 }
23877
23878 /* Add it to the vector. */
23879 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
23880
23881 /* If the next token is not a comma, we have reached the end of
23882 the list. */
23883 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23884 break;
23885
23886 /* Peek at the next token. */
23887 token = cp_lexer_peek_nth_token (parser->lexer, 2);
23888 /* If the next token is a `}', then we're still done. An
23889 initializer-clause can have a trailing `,' after the
23890 initializer-list and before the closing `}'. */
23891 if (token->type == CPP_CLOSE_BRACE)
23892 break;
23893
23894 /* Suppress location wrappers in a long initializer to save memory
23895 (14179). The cutoff is chosen arbitrarily. */
23896 const unsigned loc_max = 256;
23897 unsigned incr = 1;
23898 if (TREE_CODE (initializer) == CONSTRUCTOR)
23899 /* Look one level down because it's easy. Looking deeper would require
23900 passing down a nelts pointer, and I don't think multi-level massive
23901 initializers are common enough to justify this. */
23902 incr = CONSTRUCTOR_NELTS (initializer);
23903 nelts += incr;
23904 if (nelts >= loc_max && (nelts - incr) < loc_max)
23905 ++suppress_location_wrappers;
23906
23907 /* Consume the `,' token. */
23908 cp_lexer_consume_token (parser->lexer);
23909 }
23910
23911 /* The same identifier shall not appear in multiple designators
23912 of a designated-initializer-list. */
23913 if (first_designator)
23914 {
23915 unsigned int i;
23916 tree designator, val;
23917 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23918 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23919 {
23920 if (IDENTIFIER_MARKED (designator))
23921 {
23922 error_at (cp_expr_loc_or_input_loc (val),
23923 "%<.%s%> designator used multiple times in "
23924 "the same initializer list",
23925 IDENTIFIER_POINTER (designator));
23926 (*v)[i].index = error_mark_node;
23927 }
23928 else
23929 IDENTIFIER_MARKED (designator) = 1;
23930 }
23931 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23932 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23933 IDENTIFIER_MARKED (designator) = 0;
23934 }
23935
23936 suppress_location_wrappers = suppress;
23937
23938 *designated = first_designator != NULL_TREE;
23939 return v;
23940 }
23941
23942 /* Classes [gram.class] */
23943
23944 /* Parse a class-name.
23945
23946 class-name:
23947 identifier
23948 template-id
23949
23950 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23951 to indicate that names looked up in dependent types should be
23952 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23953 keyword has been used to indicate that the name that appears next
23954 is a template. TAG_TYPE indicates the explicit tag given before
23955 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23956 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23957 is the class being defined in a class-head. If ENUM_OK is TRUE,
23958 enum-names are also accepted.
23959
23960 Returns the TYPE_DECL representing the class. */
23961
23962 static tree
23963 cp_parser_class_name (cp_parser *parser,
23964 bool typename_keyword_p,
23965 bool template_keyword_p,
23966 enum tag_types tag_type,
23967 bool check_dependency_p,
23968 bool class_head_p,
23969 bool is_declaration,
23970 bool enum_ok)
23971 {
23972 tree decl;
23973 tree identifier = NULL_TREE;
23974
23975 /* All class-names start with an identifier. */
23976 cp_token *token = cp_lexer_peek_token (parser->lexer);
23977 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
23978 {
23979 cp_parser_error (parser, "expected class-name");
23980 return error_mark_node;
23981 }
23982
23983 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23984 to a template-id, so we save it here. Consider object scope too,
23985 so that make_typename_type below can use it (cp_parser_template_name
23986 considers object scope also). This may happen with code like
23987
23988 p->template A<T>::a()
23989
23990 where we first want to look up A<T>::a in the class of the object
23991 expression, as per [basic.lookup.classref]. */
23992 tree scope = parser->scope ? parser->scope : parser->context->object_type;
23993 if (scope == error_mark_node)
23994 return error_mark_node;
23995
23996 /* Any name names a type if we're following the `typename' keyword
23997 in a qualified name where the enclosing scope is type-dependent. */
23998 const bool typename_p = (typename_keyword_p
23999 && parser->scope
24000 && TYPE_P (parser->scope)
24001 && dependent_type_p (parser->scope));
24002 /* Handle the common case (an identifier, but not a template-id)
24003 efficiently. */
24004 if (token->type == CPP_NAME
24005 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
24006 {
24007 cp_token *identifier_token;
24008 bool ambiguous_p;
24009
24010 /* Look for the identifier. */
24011 identifier_token = cp_lexer_peek_token (parser->lexer);
24012 ambiguous_p = identifier_token->error_reported;
24013 identifier = cp_parser_identifier (parser);
24014 /* If the next token isn't an identifier, we are certainly not
24015 looking at a class-name. */
24016 if (identifier == error_mark_node)
24017 decl = error_mark_node;
24018 /* If we know this is a type-name, there's no need to look it
24019 up. */
24020 else if (typename_p)
24021 decl = identifier;
24022 else
24023 {
24024 tree ambiguous_decls;
24025 /* If we already know that this lookup is ambiguous, then
24026 we've already issued an error message; there's no reason
24027 to check again. */
24028 if (ambiguous_p)
24029 {
24030 cp_parser_simulate_error (parser);
24031 return error_mark_node;
24032 }
24033 /* If the next token is a `::', then the name must be a type
24034 name.
24035
24036 [basic.lookup.qual]
24037
24038 During the lookup for a name preceding the :: scope
24039 resolution operator, object, function, and enumerator
24040 names are ignored. */
24041 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
24042 tag_type = scope_type;
24043 /* Look up the name. */
24044 decl = cp_parser_lookup_name (parser, identifier,
24045 tag_type,
24046 /*is_template=*/false,
24047 /*is_namespace=*/false,
24048 check_dependency_p,
24049 &ambiguous_decls,
24050 identifier_token->location);
24051 if (ambiguous_decls)
24052 {
24053 if (cp_parser_parsing_tentatively (parser))
24054 cp_parser_simulate_error (parser);
24055 return error_mark_node;
24056 }
24057 }
24058 }
24059 else
24060 {
24061 /* Try a template-id. */
24062 decl = cp_parser_template_id (parser, template_keyword_p,
24063 check_dependency_p,
24064 tag_type,
24065 is_declaration);
24066 if (decl == error_mark_node)
24067 return error_mark_node;
24068 }
24069
24070 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
24071
24072 /* If this is a typename, create a TYPENAME_TYPE. */
24073 if (typename_p
24074 && decl != error_mark_node
24075 && !is_overloaded_fn (decl))
24076 {
24077 decl = make_typename_type (scope, decl, typename_type,
24078 /*complain=*/tf_error);
24079 if (decl != error_mark_node)
24080 decl = TYPE_NAME (decl);
24081 }
24082
24083 decl = strip_using_decl (decl);
24084
24085 /* Check to see that it is really the name of a class. */
24086 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
24087 && identifier_p (TREE_OPERAND (decl, 0))
24088 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
24089 /* Situations like this:
24090
24091 template <typename T> struct A {
24092 typename T::template X<int>::I i;
24093 };
24094
24095 are problematic. Is `T::template X<int>' a class-name? The
24096 standard does not seem to be definitive, but there is no other
24097 valid interpretation of the following `::'. Therefore, those
24098 names are considered class-names. */
24099 {
24100 decl = make_typename_type (scope, decl, tag_type, tf_error);
24101 if (decl != error_mark_node)
24102 decl = TYPE_NAME (decl);
24103 }
24104 else if (TREE_CODE (decl) != TYPE_DECL
24105 || TREE_TYPE (decl) == error_mark_node
24106 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
24107 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
24108 /* In Objective-C 2.0, a classname followed by '.' starts a
24109 dot-syntax expression, and it's not a type-name. */
24110 || (c_dialect_objc ()
24111 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
24112 && objc_is_class_name (decl)))
24113 decl = error_mark_node;
24114
24115 if (decl == error_mark_node)
24116 cp_parser_error (parser, "expected class-name");
24117 else if (identifier && !parser->scope)
24118 maybe_note_name_used_in_class (identifier, decl);
24119
24120 return decl;
24121 }
24122
24123 /* Make sure that any member-function parameters are in scope.
24124 For instance, a function's noexcept-specifier can use the function's
24125 parameters:
24126
24127 struct S {
24128 void fn (int p) noexcept(noexcept(p));
24129 };
24130
24131 so we need to make sure name lookup can find them. This is used
24132 when we delay parsing of the noexcept-specifier. */
24133
24134 static void
24135 inject_parm_decls (tree decl)
24136 {
24137 begin_scope (sk_function_parms, decl);
24138 tree args = DECL_ARGUMENTS (decl);
24139
24140 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
24141
24142 if (args && is_this_parameter (args))
24143 {
24144 gcc_checking_assert (current_class_ptr == NULL_TREE);
24145 current_class_ptr = NULL_TREE;
24146 current_class_ref = cp_build_fold_indirect_ref (args);
24147 current_class_ptr = args;
24148 }
24149 }
24150
24151 /* Undo the effects of inject_parm_decls. */
24152
24153 static void
24154 pop_injected_parms (void)
24155 {
24156 pop_bindings_and_leave_scope ();
24157 current_class_ptr = current_class_ref = NULL_TREE;
24158 }
24159
24160 /* Parse a class-specifier.
24161
24162 class-specifier:
24163 class-head { member-specification [opt] }
24164
24165 Returns the TREE_TYPE representing the class. */
24166
24167 static tree
24168 cp_parser_class_specifier_1 (cp_parser* parser)
24169 {
24170 tree type;
24171 tree attributes = NULL_TREE;
24172 bool nested_name_specifier_p;
24173 unsigned saved_num_template_parameter_lists;
24174 bool saved_in_function_body;
24175 unsigned char in_statement;
24176 bool in_switch_statement_p;
24177 bool saved_in_unbraced_linkage_specification_p;
24178 tree old_scope = NULL_TREE;
24179 tree scope = NULL_TREE;
24180 cp_token *closing_brace;
24181
24182 push_deferring_access_checks (dk_no_deferred);
24183
24184 /* Parse the class-head. */
24185 type = cp_parser_class_head (parser,
24186 &nested_name_specifier_p);
24187 /* If the class-head was a semantic disaster, skip the entire body
24188 of the class. */
24189 if (!type)
24190 {
24191 cp_parser_skip_to_end_of_block_or_statement (parser);
24192 pop_deferring_access_checks ();
24193 return error_mark_node;
24194 }
24195
24196 /* Look for the `{'. */
24197 matching_braces braces;
24198 if (!braces.require_open (parser))
24199 {
24200 pop_deferring_access_checks ();
24201 return error_mark_node;
24202 }
24203
24204 cp_ensure_no_omp_declare_simd (parser);
24205 cp_ensure_no_oacc_routine (parser);
24206
24207 /* Issue an error message if type-definitions are forbidden here. */
24208 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
24209 /* Remember that we are defining one more class. */
24210 ++parser->num_classes_being_defined;
24211 /* Inside the class, surrounding template-parameter-lists do not
24212 apply. */
24213 saved_num_template_parameter_lists
24214 = parser->num_template_parameter_lists;
24215 parser->num_template_parameter_lists = 0;
24216 /* We are not in a function body. */
24217 saved_in_function_body = parser->in_function_body;
24218 parser->in_function_body = false;
24219 /* Or in a loop. */
24220 in_statement = parser->in_statement;
24221 parser->in_statement = 0;
24222 /* Or in a switch. */
24223 in_switch_statement_p = parser->in_switch_statement_p;
24224 parser->in_switch_statement_p = false;
24225 /* We are not immediately inside an extern "lang" block. */
24226 saved_in_unbraced_linkage_specification_p
24227 = parser->in_unbraced_linkage_specification_p;
24228 parser->in_unbraced_linkage_specification_p = false;
24229
24230 /* Start the class. */
24231 if (nested_name_specifier_p)
24232 {
24233 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
24234 /* SCOPE must be a scope nested inside current scope. */
24235 if (is_nested_namespace (current_namespace,
24236 decl_namespace_context (scope)))
24237 old_scope = push_inner_scope (scope);
24238 else
24239 nested_name_specifier_p = false;
24240 }
24241 type = begin_class_definition (type);
24242
24243 if (type == error_mark_node)
24244 /* If the type is erroneous, skip the entire body of the class. */
24245 cp_parser_skip_to_closing_brace (parser);
24246 else
24247 /* Parse the member-specification. */
24248 cp_parser_member_specification_opt (parser);
24249
24250 /* Look for the trailing `}'. */
24251 closing_brace = braces.require_close (parser);
24252 /* Look for trailing attributes to apply to this class. */
24253 if (cp_parser_allow_gnu_extensions_p (parser))
24254 attributes = cp_parser_gnu_attributes_opt (parser);
24255 if (type != error_mark_node)
24256 type = finish_struct (type, attributes);
24257 if (nested_name_specifier_p)
24258 pop_inner_scope (old_scope, scope);
24259
24260 /* We've finished a type definition. Check for the common syntax
24261 error of forgetting a semicolon after the definition. We need to
24262 be careful, as we can't just check for not-a-semicolon and be done
24263 with it; the user might have typed:
24264
24265 class X { } c = ...;
24266 class X { } *p = ...;
24267
24268 and so forth. Instead, enumerate all the possible tokens that
24269 might follow this production; if we don't see one of them, then
24270 complain and silently insert the semicolon. */
24271 {
24272 cp_token *token = cp_lexer_peek_token (parser->lexer);
24273 bool want_semicolon = true;
24274
24275 if (cp_next_tokens_can_be_std_attribute_p (parser))
24276 /* Don't try to parse c++11 attributes here. As per the
24277 grammar, that should be a task for
24278 cp_parser_decl_specifier_seq. */
24279 want_semicolon = false;
24280
24281 switch (token->type)
24282 {
24283 case CPP_NAME:
24284 case CPP_SEMICOLON:
24285 case CPP_MULT:
24286 case CPP_AND:
24287 case CPP_OPEN_PAREN:
24288 case CPP_CLOSE_PAREN:
24289 case CPP_COMMA:
24290 want_semicolon = false;
24291 break;
24292
24293 /* While it's legal for type qualifiers and storage class
24294 specifiers to follow type definitions in the grammar, only
24295 compiler testsuites contain code like that. Assume that if
24296 we see such code, then what we're really seeing is a case
24297 like:
24298
24299 class X { }
24300 const <type> var = ...;
24301
24302 or
24303
24304 class Y { }
24305 static <type> func (...) ...
24306
24307 i.e. the qualifier or specifier applies to the next
24308 declaration. To do so, however, we need to look ahead one
24309 more token to see if *that* token is a type specifier.
24310
24311 This code could be improved to handle:
24312
24313 class Z { }
24314 static const <type> var = ...; */
24315 case CPP_KEYWORD:
24316 if (keyword_is_decl_specifier (token->keyword))
24317 {
24318 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
24319
24320 /* Handling user-defined types here would be nice, but very
24321 tricky. */
24322 want_semicolon
24323 = (lookahead->type == CPP_KEYWORD
24324 && keyword_begins_type_specifier (lookahead->keyword));
24325 }
24326 break;
24327 default:
24328 break;
24329 }
24330
24331 /* If we don't have a type, then something is very wrong and we
24332 shouldn't try to do anything clever. Likewise for not seeing the
24333 closing brace. */
24334 if (closing_brace && TYPE_P (type) && want_semicolon)
24335 {
24336 /* Locate the closing brace. */
24337 cp_token_position prev
24338 = cp_lexer_previous_token_position (parser->lexer);
24339 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
24340 location_t loc = prev_token->location;
24341
24342 /* We want to suggest insertion of a ';' immediately *after* the
24343 closing brace, so, if we can, offset the location by 1 column. */
24344 location_t next_loc = loc;
24345 if (!linemap_location_from_macro_expansion_p (line_table, loc))
24346 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
24347
24348 rich_location richloc (line_table, next_loc);
24349
24350 /* If we successfully offset the location, suggest the fix-it. */
24351 if (next_loc != loc)
24352 richloc.add_fixit_insert_before (next_loc, ";");
24353
24354 if (CLASSTYPE_DECLARED_CLASS (type))
24355 error_at (&richloc,
24356 "expected %<;%> after class definition");
24357 else if (TREE_CODE (type) == RECORD_TYPE)
24358 error_at (&richloc,
24359 "expected %<;%> after struct definition");
24360 else if (TREE_CODE (type) == UNION_TYPE)
24361 error_at (&richloc,
24362 "expected %<;%> after union definition");
24363 else
24364 gcc_unreachable ();
24365
24366 /* Unget one token and smash it to look as though we encountered
24367 a semicolon in the input stream. */
24368 cp_lexer_set_token_position (parser->lexer, prev);
24369 token = cp_lexer_peek_token (parser->lexer);
24370 token->type = CPP_SEMICOLON;
24371 token->keyword = RID_MAX;
24372 }
24373 }
24374
24375 /* If this class is not itself within the scope of another class,
24376 then we need to parse the bodies of all of the queued function
24377 definitions. Note that the queued functions defined in a class
24378 are not always processed immediately following the
24379 class-specifier for that class. Consider:
24380
24381 struct A {
24382 struct B { void f() { sizeof (A); } };
24383 };
24384
24385 If `f' were processed before the processing of `A' were
24386 completed, there would be no way to compute the size of `A'.
24387 Note that the nesting we are interested in here is lexical --
24388 not the semantic nesting given by TYPE_CONTEXT. In particular,
24389 for:
24390
24391 struct A { struct B; };
24392 struct A::B { void f() { } };
24393
24394 there is no need to delay the parsing of `A::B::f'. */
24395 if (--parser->num_classes_being_defined == 0)
24396 {
24397 tree decl;
24398 tree class_type = NULL_TREE;
24399 tree pushed_scope = NULL_TREE;
24400 unsigned ix;
24401 cp_default_arg_entry *e;
24402 tree save_ccp, save_ccr;
24403
24404 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
24405 {
24406 /* Skip default arguments, NSDMIs, etc, in order to improve
24407 error recovery (c++/71169, c++/71832). */
24408 vec_safe_truncate (unparsed_funs_with_default_args, 0);
24409 vec_safe_truncate (unparsed_nsdmis, 0);
24410 vec_safe_truncate (unparsed_funs_with_definitions, 0);
24411 }
24412
24413 /* In a first pass, parse default arguments to the functions.
24414 Then, in a second pass, parse the bodies of the functions.
24415 This two-phased approach handles cases like:
24416
24417 struct S {
24418 void f() { g(); }
24419 void g(int i = 3);
24420 };
24421
24422 */
24423 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
24424 {
24425 decl = e->decl;
24426 /* If there are default arguments that have not yet been processed,
24427 take care of them now. */
24428 if (class_type != e->class_type)
24429 {
24430 if (pushed_scope)
24431 pop_scope (pushed_scope);
24432 class_type = e->class_type;
24433 pushed_scope = push_scope (class_type);
24434 }
24435 /* Make sure that any template parameters are in scope. */
24436 maybe_begin_member_template_processing (decl);
24437 /* Parse the default argument expressions. */
24438 cp_parser_late_parsing_default_args (parser, decl);
24439 /* Remove any template parameters from the symbol table. */
24440 maybe_end_member_template_processing ();
24441 }
24442 vec_safe_truncate (unparsed_funs_with_default_args, 0);
24443 /* Now parse any NSDMIs. */
24444 save_ccp = current_class_ptr;
24445 save_ccr = current_class_ref;
24446 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
24447 {
24448 if (class_type != DECL_CONTEXT (decl))
24449 {
24450 if (pushed_scope)
24451 pop_scope (pushed_scope);
24452 class_type = DECL_CONTEXT (decl);
24453 pushed_scope = push_scope (class_type);
24454 }
24455 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
24456 cp_parser_late_parsing_nsdmi (parser, decl);
24457 }
24458 vec_safe_truncate (unparsed_nsdmis, 0);
24459 current_class_ptr = save_ccp;
24460 current_class_ref = save_ccr;
24461 if (pushed_scope)
24462 pop_scope (pushed_scope);
24463
24464 /* If there are noexcept-specifiers that have not yet been processed,
24465 take care of them now. */
24466 class_type = NULL_TREE;
24467 pushed_scope = NULL_TREE;
24468 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
24469 {
24470 tree ctx = DECL_CONTEXT (decl);
24471 if (class_type != ctx)
24472 {
24473 if (pushed_scope)
24474 pop_scope (pushed_scope);
24475 class_type = ctx;
24476 pushed_scope = push_scope (class_type);
24477 }
24478
24479 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
24480 spec = TREE_PURPOSE (spec);
24481
24482 /* Make sure that any template parameters are in scope. */
24483 maybe_begin_member_template_processing (decl);
24484
24485 /* Make sure that any member-function parameters are in scope. */
24486 inject_parm_decls (decl);
24487
24488 /* 'this' is not allowed in static member functions. */
24489 unsigned char local_variables_forbidden_p
24490 = parser->local_variables_forbidden_p;
24491 if (DECL_THIS_STATIC (decl))
24492 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
24493
24494 /* Now we can parse the noexcept-specifier. */
24495 spec = cp_parser_late_noexcept_specifier (parser, spec);
24496
24497 if (spec == error_mark_node)
24498 spec = NULL_TREE;
24499
24500 /* Update the fn's type directly -- it might have escaped
24501 beyond this decl :( */
24502 fixup_deferred_exception_variants (TREE_TYPE (decl), spec);
24503
24504 /* Restore the state of local_variables_forbidden_p. */
24505 parser->local_variables_forbidden_p = local_variables_forbidden_p;
24506
24507 /* The finish_struct call above performed various override checking,
24508 but it skipped unparsed noexcept-specifier operands. Now that we
24509 have resolved them, check again. */
24510 noexcept_override_late_checks (type, decl);
24511
24512 /* Remove any member-function parameters from the symbol table. */
24513 pop_injected_parms ();
24514
24515 /* Remove any template parameters from the symbol table. */
24516 maybe_end_member_template_processing ();
24517 }
24518 vec_safe_truncate (unparsed_noexcepts, 0);
24519 if (pushed_scope)
24520 pop_scope (pushed_scope);
24521
24522 /* Now parse the body of the functions. */
24523 if (flag_openmp)
24524 {
24525 /* OpenMP UDRs need to be parsed before all other functions. */
24526 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
24527 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
24528 cp_parser_late_parsing_for_member (parser, decl);
24529 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
24530 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
24531 cp_parser_late_parsing_for_member (parser, decl);
24532 }
24533 else
24534 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
24535 cp_parser_late_parsing_for_member (parser, decl);
24536 vec_safe_truncate (unparsed_funs_with_definitions, 0);
24537 }
24538
24539 /* Put back any saved access checks. */
24540 pop_deferring_access_checks ();
24541
24542 /* Restore saved state. */
24543 parser->in_switch_statement_p = in_switch_statement_p;
24544 parser->in_statement = in_statement;
24545 parser->in_function_body = saved_in_function_body;
24546 parser->num_template_parameter_lists
24547 = saved_num_template_parameter_lists;
24548 parser->in_unbraced_linkage_specification_p
24549 = saved_in_unbraced_linkage_specification_p;
24550
24551 return type;
24552 }
24553
24554 static tree
24555 cp_parser_class_specifier (cp_parser* parser)
24556 {
24557 tree ret;
24558 timevar_push (TV_PARSE_STRUCT);
24559 ret = cp_parser_class_specifier_1 (parser);
24560 timevar_pop (TV_PARSE_STRUCT);
24561 return ret;
24562 }
24563
24564 /* Parse a class-head.
24565
24566 class-head:
24567 class-key identifier [opt] base-clause [opt]
24568 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
24569 class-key nested-name-specifier [opt] template-id
24570 base-clause [opt]
24571
24572 class-virt-specifier:
24573 final
24574
24575 GNU Extensions:
24576 class-key attributes identifier [opt] base-clause [opt]
24577 class-key attributes nested-name-specifier identifier base-clause [opt]
24578 class-key attributes nested-name-specifier [opt] template-id
24579 base-clause [opt]
24580
24581 Upon return BASES is initialized to the list of base classes (or
24582 NULL, if there are none) in the same form returned by
24583 cp_parser_base_clause.
24584
24585 Returns the TYPE of the indicated class. Sets
24586 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
24587 involving a nested-name-specifier was used, and FALSE otherwise.
24588
24589 Returns error_mark_node if this is not a class-head.
24590
24591 Returns NULL_TREE if the class-head is syntactically valid, but
24592 semantically invalid in a way that means we should skip the entire
24593 body of the class. */
24594
24595 static tree
24596 cp_parser_class_head (cp_parser* parser,
24597 bool* nested_name_specifier_p)
24598 {
24599 tree nested_name_specifier;
24600 enum tag_types class_key;
24601 tree id = NULL_TREE;
24602 tree type = NULL_TREE;
24603 tree attributes;
24604 tree bases;
24605 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
24606 bool template_id_p = false;
24607 bool qualified_p = false;
24608 bool invalid_nested_name_p = false;
24609 bool invalid_explicit_specialization_p = false;
24610 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24611 tree pushed_scope = NULL_TREE;
24612 unsigned num_templates;
24613 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
24614 /* Assume no nested-name-specifier will be present. */
24615 *nested_name_specifier_p = false;
24616 /* Assume no template parameter lists will be used in defining the
24617 type. */
24618 num_templates = 0;
24619 parser->colon_corrects_to_scope_p = false;
24620
24621 /* Look for the class-key. */
24622 class_key = cp_parser_class_key (parser);
24623 if (class_key == none_type)
24624 return error_mark_node;
24625
24626 location_t class_head_start_location = input_location;
24627
24628 /* Parse the attributes. */
24629 attributes = cp_parser_attributes_opt (parser);
24630
24631 /* If the next token is `::', that is invalid -- but sometimes
24632 people do try to write:
24633
24634 struct ::S {};
24635
24636 Handle this gracefully by accepting the extra qualifier, and then
24637 issuing an error about it later if this really is a
24638 class-head. If it turns out just to be an elaborated type
24639 specifier, remain silent. */
24640 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
24641 qualified_p = true;
24642
24643 push_deferring_access_checks (dk_no_check);
24644
24645 /* Determine the name of the class. Begin by looking for an
24646 optional nested-name-specifier. */
24647 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
24648 nested_name_specifier
24649 = cp_parser_nested_name_specifier_opt (parser,
24650 /*typename_keyword_p=*/false,
24651 /*check_dependency_p=*/false,
24652 /*type_p=*/true,
24653 /*is_declaration=*/false);
24654 /* If there was a nested-name-specifier, then there *must* be an
24655 identifier. */
24656
24657 cp_token *bad_template_keyword = NULL;
24658
24659 if (nested_name_specifier)
24660 {
24661 type_start_token = cp_lexer_peek_token (parser->lexer);
24662 /* Although the grammar says `identifier', it really means
24663 `class-name' or `template-name'. You are only allowed to
24664 define a class that has already been declared with this
24665 syntax.
24666
24667 The proposed resolution for Core Issue 180 says that wherever
24668 you see `class T::X' you should treat `X' as a type-name.
24669
24670 It is OK to define an inaccessible class; for example:
24671
24672 class A { class B; };
24673 class A::B {};
24674
24675 We do not know if we will see a class-name, or a
24676 template-name. We look for a class-name first, in case the
24677 class-name is a template-id; if we looked for the
24678 template-name first we would stop after the template-name. */
24679 cp_parser_parse_tentatively (parser);
24680 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24681 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
24682 type = cp_parser_class_name (parser,
24683 /*typename_keyword_p=*/false,
24684 /*template_keyword_p=*/false,
24685 class_type,
24686 /*check_dependency_p=*/false,
24687 /*class_head_p=*/true,
24688 /*is_declaration=*/false);
24689 /* If that didn't work, ignore the nested-name-specifier. */
24690 if (!cp_parser_parse_definitely (parser))
24691 {
24692 invalid_nested_name_p = true;
24693 type_start_token = cp_lexer_peek_token (parser->lexer);
24694 id = cp_parser_identifier (parser);
24695 if (id == error_mark_node)
24696 id = NULL_TREE;
24697 }
24698 /* If we could not find a corresponding TYPE, treat this
24699 declaration like an unqualified declaration. */
24700 if (type == error_mark_node)
24701 nested_name_specifier = NULL_TREE;
24702 /* Otherwise, count the number of templates used in TYPE and its
24703 containing scopes. */
24704 else
24705 num_templates = num_template_headers_for_class (TREE_TYPE (type));
24706 }
24707 /* Otherwise, the identifier is optional. */
24708 else
24709 {
24710 /* We don't know whether what comes next is a template-id,
24711 an identifier, or nothing at all. */
24712 cp_parser_parse_tentatively (parser);
24713 /* Check for a template-id. */
24714 type_start_token = cp_lexer_peek_token (parser->lexer);
24715 id = cp_parser_template_id (parser,
24716 /*template_keyword_p=*/false,
24717 /*check_dependency_p=*/true,
24718 class_key,
24719 /*is_declaration=*/true);
24720 /* If that didn't work, it could still be an identifier. */
24721 if (!cp_parser_parse_definitely (parser))
24722 {
24723 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24724 {
24725 type_start_token = cp_lexer_peek_token (parser->lexer);
24726 id = cp_parser_identifier (parser);
24727 }
24728 else
24729 id = NULL_TREE;
24730 }
24731 else
24732 {
24733 template_id_p = true;
24734 ++num_templates;
24735 }
24736 }
24737
24738 pop_deferring_access_checks ();
24739
24740 if (id)
24741 {
24742 cp_parser_check_for_invalid_template_id (parser, id,
24743 class_key,
24744 type_start_token->location);
24745 }
24746 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
24747
24748 /* If it's not a `:' or a `{' then we can't really be looking at a
24749 class-head, since a class-head only appears as part of a
24750 class-specifier. We have to detect this situation before calling
24751 xref_tag, since that has irreversible side-effects. */
24752 if (!cp_parser_next_token_starts_class_definition_p (parser))
24753 {
24754 cp_parser_error (parser, "expected %<{%> or %<:%>");
24755 type = error_mark_node;
24756 goto out;
24757 }
24758
24759 /* At this point, we're going ahead with the class-specifier, even
24760 if some other problem occurs. */
24761 cp_parser_commit_to_tentative_parse (parser);
24762 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
24763 {
24764 cp_parser_error (parser,
24765 "cannot specify %<override%> for a class");
24766 type = error_mark_node;
24767 goto out;
24768 }
24769 /* Issue the error about the overly-qualified name now. */
24770 if (qualified_p)
24771 {
24772 cp_parser_error (parser,
24773 "global qualification of class name is invalid");
24774 type = error_mark_node;
24775 goto out;
24776 }
24777 else if (invalid_nested_name_p)
24778 {
24779 cp_parser_error (parser,
24780 "qualified name does not name a class");
24781 type = error_mark_node;
24782 goto out;
24783 }
24784 else if (nested_name_specifier)
24785 {
24786 tree scope;
24787
24788 if (bad_template_keyword)
24789 /* [temp.names]: in a qualified-id formed by a class-head-name, the
24790 keyword template shall not appear at the top level. */
24791 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
24792 "keyword %<template%> not allowed in class-head-name");
24793
24794 /* Reject typedef-names in class heads. */
24795 if (!DECL_IMPLICIT_TYPEDEF_P (type))
24796 {
24797 error_at (type_start_token->location,
24798 "invalid class name in declaration of %qD",
24799 type);
24800 type = NULL_TREE;
24801 goto done;
24802 }
24803
24804 /* Figure out in what scope the declaration is being placed. */
24805 scope = current_scope ();
24806 /* If that scope does not contain the scope in which the
24807 class was originally declared, the program is invalid. */
24808 if (scope && !is_ancestor (scope, nested_name_specifier))
24809 {
24810 if (at_namespace_scope_p ())
24811 error_at (type_start_token->location,
24812 "declaration of %qD in namespace %qD which does not "
24813 "enclose %qD",
24814 type, scope, nested_name_specifier);
24815 else
24816 error_at (type_start_token->location,
24817 "declaration of %qD in %qD which does not enclose %qD",
24818 type, scope, nested_name_specifier);
24819 type = NULL_TREE;
24820 goto done;
24821 }
24822 /* [dcl.meaning]
24823
24824 A declarator-id shall not be qualified except for the
24825 definition of a ... nested class outside of its class
24826 ... [or] the definition or explicit instantiation of a
24827 class member of a namespace outside of its namespace. */
24828 if (scope == nested_name_specifier)
24829 permerror (nested_name_specifier_token_start->location,
24830 "extra qualification not allowed");
24831 }
24832 /* An explicit-specialization must be preceded by "template <>". If
24833 it is not, try to recover gracefully. */
24834 if (at_namespace_scope_p ()
24835 && parser->num_template_parameter_lists == 0
24836 && !processing_template_parmlist
24837 && template_id_p)
24838 {
24839 /* Build a location of this form:
24840 struct typename <ARGS>
24841 ^~~~~~~~~~~~~~~~~~~~~~
24842 with caret==start at the start token, and
24843 finishing at the end of the type. */
24844 location_t reported_loc
24845 = make_location (class_head_start_location,
24846 class_head_start_location,
24847 get_finish (type_start_token->location));
24848 rich_location richloc (line_table, reported_loc);
24849 richloc.add_fixit_insert_before (class_head_start_location,
24850 "template <> ");
24851 error_at (&richloc,
24852 "an explicit specialization must be preceded by"
24853 " %<template <>%>");
24854 invalid_explicit_specialization_p = true;
24855 /* Take the same action that would have been taken by
24856 cp_parser_explicit_specialization. */
24857 ++parser->num_template_parameter_lists;
24858 begin_specialization ();
24859 }
24860 /* There must be no "return" statements between this point and the
24861 end of this function; set "type "to the correct return value and
24862 use "goto done;" to return. */
24863 /* Make sure that the right number of template parameters were
24864 present. */
24865 if (!cp_parser_check_template_parameters (parser, num_templates,
24866 template_id_p,
24867 type_start_token->location,
24868 /*declarator=*/NULL))
24869 {
24870 /* If something went wrong, there is no point in even trying to
24871 process the class-definition. */
24872 type = NULL_TREE;
24873 goto done;
24874 }
24875
24876 /* Look up the type. */
24877 if (template_id_p)
24878 {
24879 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
24880 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
24881 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
24882 {
24883 error_at (type_start_token->location,
24884 "function template %qD redeclared as a class template", id);
24885 type = error_mark_node;
24886 }
24887 else
24888 {
24889 type = TREE_TYPE (id);
24890 type = maybe_process_partial_specialization (type);
24891
24892 /* Check the scope while we still know whether or not we had a
24893 nested-name-specifier. */
24894 if (type != error_mark_node)
24895 check_unqualified_spec_or_inst (type, type_start_token->location);
24896 }
24897 if (nested_name_specifier)
24898 pushed_scope = push_scope (nested_name_specifier);
24899 }
24900 else if (nested_name_specifier)
24901 {
24902 tree class_type;
24903
24904 /* Given:
24905
24906 template <typename T> struct S { struct T };
24907 template <typename T> struct S<T>::T { };
24908
24909 we will get a TYPENAME_TYPE when processing the definition of
24910 `S::T'. We need to resolve it to the actual type before we
24911 try to define it. */
24912 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
24913 {
24914 class_type = resolve_typename_type (TREE_TYPE (type),
24915 /*only_current_p=*/false);
24916 if (TREE_CODE (class_type) != TYPENAME_TYPE)
24917 type = TYPE_NAME (class_type);
24918 else
24919 {
24920 cp_parser_error (parser, "could not resolve typename type");
24921 type = error_mark_node;
24922 }
24923 }
24924
24925 if (maybe_process_partial_specialization (TREE_TYPE (type))
24926 == error_mark_node)
24927 {
24928 type = NULL_TREE;
24929 goto done;
24930 }
24931
24932 class_type = current_class_type;
24933 /* Enter the scope indicated by the nested-name-specifier. */
24934 pushed_scope = push_scope (nested_name_specifier);
24935 /* Get the canonical version of this type. */
24936 type = TYPE_MAIN_DECL (TREE_TYPE (type));
24937 /* Call push_template_decl if it seems like we should be defining a
24938 template either from the template headers or the type we're
24939 defining, so that we diagnose both extra and missing headers. */
24940 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
24941 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
24942 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
24943 {
24944 type = push_template_decl (type);
24945 if (type == error_mark_node)
24946 {
24947 type = NULL_TREE;
24948 goto done;
24949 }
24950 }
24951
24952 type = TREE_TYPE (type);
24953 *nested_name_specifier_p = true;
24954 }
24955 else /* The name is not a nested name. */
24956 {
24957 /* If the class was unnamed, create a dummy name. */
24958 if (!id)
24959 id = make_anon_name ();
24960 TAG_how how = (parser->in_type_id_in_expr_p
24961 ? TAG_how::INNERMOST_NON_CLASS
24962 : TAG_how::CURRENT_ONLY);
24963 type = xref_tag (class_key, id, how,
24964 parser->num_template_parameter_lists);
24965 }
24966
24967 /* Diagnose class/struct/union mismatches. */
24968 cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
24969 true, true);
24970
24971 /* Indicate whether this class was declared as a `class' or as a
24972 `struct'. */
24973 if (TREE_CODE (type) == RECORD_TYPE)
24974 CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
24975
24976 /* If this type was already complete, and we see another definition,
24977 that's an error. Likewise if the type is already being defined:
24978 this can happen, eg, when it's defined from within an expression
24979 (c++/84605). */
24980 if (type != error_mark_node
24981 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
24982 {
24983 error_at (type_start_token->location, "redefinition of %q#T",
24984 type);
24985 inform (location_of (type), "previous definition of %q#T",
24986 type);
24987 type = NULL_TREE;
24988 goto done;
24989 }
24990 else if (type == error_mark_node)
24991 type = NULL_TREE;
24992
24993 if (type)
24994 {
24995 /* Apply attributes now, before any use of the class as a template
24996 argument in its base list. */
24997 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
24998 fixup_attribute_variants (type);
24999 }
25000
25001 /* Associate constraints with the type. */
25002 if (flag_concepts)
25003 type = associate_classtype_constraints (type);
25004
25005 /* We will have entered the scope containing the class; the names of
25006 base classes should be looked up in that context. For example:
25007
25008 struct A { struct B {}; struct C; };
25009 struct A::C : B {};
25010
25011 is valid. */
25012
25013 /* Get the list of base-classes, if there is one. */
25014 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
25015 {
25016 /* PR59482: enter the class scope so that base-specifiers are looked
25017 up correctly. */
25018 if (type)
25019 pushclass (type);
25020 bases = cp_parser_base_clause (parser);
25021 /* PR59482: get out of the previously pushed class scope so that the
25022 subsequent pops pop the right thing. */
25023 if (type)
25024 popclass ();
25025 }
25026 else
25027 bases = NULL_TREE;
25028
25029 /* If we're really defining a class, process the base classes.
25030 If they're invalid, fail. */
25031 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25032 xref_basetypes (type, bases);
25033
25034 done:
25035 /* Leave the scope given by the nested-name-specifier. We will
25036 enter the class scope itself while processing the members. */
25037 if (pushed_scope)
25038 pop_scope (pushed_scope);
25039
25040 if (invalid_explicit_specialization_p)
25041 {
25042 end_specialization ();
25043 --parser->num_template_parameter_lists;
25044 }
25045
25046 if (type)
25047 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
25048 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
25049 CLASSTYPE_FINAL (type) = 1;
25050 out:
25051 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
25052 return type;
25053 }
25054
25055 /* Parse a class-key.
25056
25057 class-key:
25058 class
25059 struct
25060 union
25061
25062 Returns the kind of class-key specified, or none_type to indicate
25063 error. */
25064
25065 static enum tag_types
25066 cp_parser_class_key (cp_parser* parser)
25067 {
25068 cp_token *token;
25069 enum tag_types tag_type;
25070
25071 /* Look for the class-key. */
25072 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
25073 if (!token)
25074 return none_type;
25075
25076 /* Check to see if the TOKEN is a class-key. */
25077 tag_type = cp_parser_token_is_class_key (token);
25078 if (!tag_type)
25079 cp_parser_error (parser, "expected class-key");
25080 return tag_type;
25081 }
25082
25083 /* Parse a type-parameter-key.
25084
25085 type-parameter-key:
25086 class
25087 typename
25088 */
25089
25090 static void
25091 cp_parser_type_parameter_key (cp_parser* parser)
25092 {
25093 /* Look for the type-parameter-key. */
25094 enum tag_types tag_type = none_type;
25095 cp_token *token = cp_lexer_peek_token (parser->lexer);
25096 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
25097 {
25098 cp_lexer_consume_token (parser->lexer);
25099 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
25100 /* typename is not allowed in a template template parameter
25101 by the standard until C++17. */
25102 pedwarn (token->location, OPT_Wpedantic,
25103 "ISO C++ forbids typename key in template template parameter;"
25104 " use %<-std=c++17%> or %<-std=gnu++17%>");
25105 }
25106 else
25107 cp_parser_error (parser, "expected %<class%> or %<typename%>");
25108
25109 return;
25110 }
25111
25112 /* Parse an (optional) member-specification.
25113
25114 member-specification:
25115 member-declaration member-specification [opt]
25116 access-specifier : member-specification [opt] */
25117
25118 static void
25119 cp_parser_member_specification_opt (cp_parser* parser)
25120 {
25121 while (true)
25122 {
25123 cp_token *token;
25124 enum rid keyword;
25125
25126 /* Peek at the next token. */
25127 token = cp_lexer_peek_token (parser->lexer);
25128 /* If it's a `}', or EOF then we've seen all the members. */
25129 if (token->type == CPP_CLOSE_BRACE
25130 || token->type == CPP_EOF
25131 || token->type == CPP_PRAGMA_EOL)
25132 break;
25133
25134 /* See if this token is a keyword. */
25135 keyword = token->keyword;
25136 switch (keyword)
25137 {
25138 case RID_PUBLIC:
25139 case RID_PROTECTED:
25140 case RID_PRIVATE:
25141 /* Consume the access-specifier. */
25142 cp_lexer_consume_token (parser->lexer);
25143 /* Remember which access-specifier is active. */
25144 current_access_specifier = token->u.value;
25145 /* Look for the `:'. */
25146 cp_parser_require (parser, CPP_COLON, RT_COLON);
25147 break;
25148
25149 default:
25150 /* Accept #pragmas at class scope. */
25151 if (token->type == CPP_PRAGMA)
25152 {
25153 cp_parser_pragma (parser, pragma_member, NULL);
25154 break;
25155 }
25156
25157 /* Otherwise, the next construction must be a
25158 member-declaration. */
25159 cp_parser_member_declaration (parser);
25160 }
25161 }
25162 }
25163
25164 /* Parse a member-declaration.
25165
25166 member-declaration:
25167 decl-specifier-seq [opt] member-declarator-list [opt] ;
25168 function-definition ; [opt]
25169 :: [opt] nested-name-specifier template [opt] unqualified-id ;
25170 using-declaration
25171 template-declaration
25172 alias-declaration
25173
25174 member-declarator-list:
25175 member-declarator
25176 member-declarator-list , member-declarator
25177
25178 member-declarator:
25179 declarator pure-specifier [opt]
25180 declarator constant-initializer [opt]
25181 identifier [opt] : constant-expression
25182
25183 GNU Extensions:
25184
25185 member-declaration:
25186 __extension__ member-declaration
25187
25188 member-declarator:
25189 declarator attributes [opt] pure-specifier [opt]
25190 declarator attributes [opt] constant-initializer [opt]
25191 identifier [opt] attributes [opt] : constant-expression
25192
25193 C++0x Extensions:
25194
25195 member-declaration:
25196 static_assert-declaration */
25197
25198 static void
25199 cp_parser_member_declaration (cp_parser* parser)
25200 {
25201 cp_decl_specifier_seq decl_specifiers;
25202 tree prefix_attributes;
25203 tree decl;
25204 int declares_class_or_enum;
25205 bool friend_p;
25206 cp_token *token = NULL;
25207 cp_token *decl_spec_token_start = NULL;
25208 cp_token *initializer_token_start = NULL;
25209 int saved_pedantic;
25210 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
25211
25212 /* Check for the `__extension__' keyword. */
25213 if (cp_parser_extension_opt (parser, &saved_pedantic))
25214 {
25215 /* Recurse. */
25216 cp_parser_member_declaration (parser);
25217 /* Restore the old value of the PEDANTIC flag. */
25218 pedantic = saved_pedantic;
25219
25220 return;
25221 }
25222
25223 /* Check for a template-declaration. */
25224 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25225 {
25226 /* An explicit specialization here is an error condition, and we
25227 expect the specialization handler to detect and report this. */
25228 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
25229 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
25230 cp_parser_explicit_specialization (parser);
25231 else
25232 cp_parser_template_declaration (parser, /*member_p=*/true);
25233
25234 return;
25235 }
25236 /* Check for a template introduction. */
25237 else if (cp_parser_template_declaration_after_export (parser, true))
25238 return;
25239
25240 /* Check for a using-declaration. */
25241 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25242 {
25243 if (cxx_dialect < cxx11)
25244 {
25245 /* Parse the using-declaration. */
25246 cp_parser_using_declaration (parser,
25247 /*access_declaration_p=*/false);
25248 return;
25249 }
25250 else
25251 {
25252 tree decl;
25253 bool alias_decl_expected;
25254 cp_parser_parse_tentatively (parser);
25255 decl = cp_parser_alias_declaration (parser);
25256 /* Note that if we actually see the '=' token after the
25257 identifier, cp_parser_alias_declaration commits the
25258 tentative parse. In that case, we really expect an
25259 alias-declaration. Otherwise, we expect a using
25260 declaration. */
25261 alias_decl_expected =
25262 !cp_parser_uncommitted_to_tentative_parse_p (parser);
25263 cp_parser_parse_definitely (parser);
25264
25265 if (alias_decl_expected)
25266 finish_member_declaration (decl);
25267 else
25268 cp_parser_using_declaration (parser,
25269 /*access_declaration_p=*/false);
25270 return;
25271 }
25272 }
25273
25274 /* Check for @defs. */
25275 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
25276 {
25277 tree ivar, member;
25278 tree ivar_chains = cp_parser_objc_defs_expression (parser);
25279 ivar = ivar_chains;
25280 while (ivar)
25281 {
25282 member = ivar;
25283 ivar = TREE_CHAIN (member);
25284 TREE_CHAIN (member) = NULL_TREE;
25285 finish_member_declaration (member);
25286 }
25287 return;
25288 }
25289
25290 /* If the next token is `static_assert' we have a static assertion. */
25291 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
25292 {
25293 cp_parser_static_assert (parser, /*member_p=*/true);
25294 return;
25295 }
25296
25297 parser->colon_corrects_to_scope_p = false;
25298
25299 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
25300 goto out;
25301
25302 /* Parse the decl-specifier-seq. */
25303 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25304 cp_parser_decl_specifier_seq (parser,
25305 (CP_PARSER_FLAGS_OPTIONAL
25306 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
25307 &decl_specifiers,
25308 &declares_class_or_enum);
25309 /* Check for an invalid type-name. */
25310 if (!decl_specifiers.any_type_specifiers_p
25311 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25312 goto out;
25313 /* If there is no declarator, then the decl-specifier-seq should
25314 specify a type. */
25315 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25316 {
25317 /* If there was no decl-specifier-seq, and the next token is a
25318 `;', then we have something like:
25319
25320 struct S { ; };
25321
25322 [class.mem]
25323
25324 Each member-declaration shall declare at least one member
25325 name of the class. */
25326 if (!decl_specifiers.any_specifiers_p)
25327 {
25328 cp_token *token = cp_lexer_peek_token (parser->lexer);
25329 if (!in_system_header_at (token->location))
25330 {
25331 gcc_rich_location richloc (token->location);
25332 richloc.add_fixit_remove ();
25333 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
25334 }
25335 }
25336 else
25337 {
25338 /* See if this declaration is a friend. */
25339 friend_p = cp_parser_friend_p (&decl_specifiers);
25340 /* If there were decl-specifiers, check to see if there was
25341 a class-declaration. */
25342 tree type = check_tag_decl (&decl_specifiers,
25343 /*explicit_type_instantiation_p=*/false);
25344 /* Nested classes have already been added to the class, but
25345 a `friend' needs to be explicitly registered. */
25346 if (friend_p)
25347 {
25348 /* If the `friend' keyword was present, the friend must
25349 be introduced with a class-key. */
25350 if (!declares_class_or_enum && cxx_dialect < cxx11)
25351 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
25352 "in C++03 a class-key must be used "
25353 "when declaring a friend");
25354 /* In this case:
25355
25356 template <typename T> struct A {
25357 friend struct A<T>::B;
25358 };
25359
25360 A<T>::B will be represented by a TYPENAME_TYPE, and
25361 therefore not recognized by check_tag_decl. */
25362 if (!type)
25363 {
25364 type = decl_specifiers.type;
25365 if (type && TREE_CODE (type) == TYPE_DECL)
25366 type = TREE_TYPE (type);
25367 }
25368 if (!type || !TYPE_P (type))
25369 error_at (decl_spec_token_start->location,
25370 "friend declaration does not name a class or "
25371 "function");
25372 else
25373 make_friend_class (current_class_type, type,
25374 /*complain=*/true);
25375 }
25376 /* If there is no TYPE, an error message will already have
25377 been issued. */
25378 else if (!type || type == error_mark_node)
25379 ;
25380 /* An anonymous aggregate has to be handled specially; such
25381 a declaration really declares a data member (with a
25382 particular type), as opposed to a nested class. */
25383 else if (ANON_AGGR_TYPE_P (type))
25384 {
25385 /* C++11 9.5/6. */
25386 if (decl_specifiers.storage_class != sc_none)
25387 error_at (decl_spec_token_start->location,
25388 "a storage class on an anonymous aggregate "
25389 "in class scope is not allowed");
25390
25391 /* Remove constructors and such from TYPE, now that we
25392 know it is an anonymous aggregate. */
25393 fixup_anonymous_aggr (type);
25394 /* And make the corresponding data member. */
25395 decl = build_decl (decl_spec_token_start->location,
25396 FIELD_DECL, NULL_TREE, type);
25397 /* Add it to the class. */
25398 finish_member_declaration (decl);
25399 }
25400 else
25401 cp_parser_check_access_in_redeclaration
25402 (TYPE_NAME (type),
25403 decl_spec_token_start->location);
25404 }
25405 }
25406 else
25407 {
25408 bool assume_semicolon = false;
25409
25410 /* Clear attributes from the decl_specifiers but keep them
25411 around as prefix attributes that apply them to the entity
25412 being declared. */
25413 prefix_attributes = decl_specifiers.attributes;
25414 decl_specifiers.attributes = NULL_TREE;
25415
25416 /* See if these declarations will be friends. */
25417 friend_p = cp_parser_friend_p (&decl_specifiers);
25418
25419 /* Keep going until we hit the `;' at the end of the
25420 declaration. */
25421 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25422 {
25423 tree attributes = NULL_TREE;
25424 tree first_attribute;
25425 tree initializer;
25426 bool named_bitfld = false;
25427
25428 /* Peek at the next token. */
25429 token = cp_lexer_peek_token (parser->lexer);
25430
25431 /* The following code wants to know early if it is a bit-field
25432 or some other declaration. Attributes can appear before
25433 the `:' token. Skip over them without consuming any tokens
25434 to peek if they are followed by `:'. */
25435 if (cp_next_tokens_can_be_attribute_p (parser)
25436 || (token->type == CPP_NAME
25437 && cp_nth_tokens_can_be_attribute_p (parser, 2)
25438 && (named_bitfld = true)))
25439 {
25440 size_t n
25441 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
25442 token = cp_lexer_peek_nth_token (parser->lexer, n);
25443 }
25444
25445 /* Check for a bitfield declaration. */
25446 if (token->type == CPP_COLON
25447 || (token->type == CPP_NAME
25448 && token == cp_lexer_peek_token (parser->lexer)
25449 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
25450 && (named_bitfld = true)))
25451 {
25452 tree identifier;
25453 tree width;
25454 tree late_attributes = NULL_TREE;
25455 location_t id_location
25456 = cp_lexer_peek_token (parser->lexer)->location;
25457
25458 if (named_bitfld)
25459 identifier = cp_parser_identifier (parser);
25460 else
25461 identifier = NULL_TREE;
25462
25463 /* Look for attributes that apply to the bitfield. */
25464 attributes = cp_parser_attributes_opt (parser);
25465
25466 /* Consume the `:' token. */
25467 cp_lexer_consume_token (parser->lexer);
25468
25469 /* Get the width of the bitfield. */
25470 width = cp_parser_constant_expression (parser, false, NULL,
25471 cxx_dialect >= cxx11);
25472
25473 /* In C++20 and as extension for C++11 and above we allow
25474 default member initializers for bit-fields. */
25475 initializer = NULL_TREE;
25476 if (cxx_dialect >= cxx11
25477 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
25478 || cp_lexer_next_token_is (parser->lexer,
25479 CPP_OPEN_BRACE)))
25480 {
25481 location_t loc
25482 = cp_lexer_peek_token (parser->lexer)->location;
25483 if (cxx_dialect < cxx20
25484 && identifier != NULL_TREE)
25485 pedwarn (loc, 0,
25486 "default member initializers for bit-fields "
25487 "only available with %<-std=c++20%> or "
25488 "%<-std=gnu++20%>");
25489
25490 initializer = cp_parser_save_nsdmi (parser);
25491 if (identifier == NULL_TREE)
25492 {
25493 error_at (loc, "default member initializer for "
25494 "unnamed bit-field");
25495 initializer = NULL_TREE;
25496 }
25497 }
25498 else
25499 {
25500 /* Look for attributes that apply to the bitfield after
25501 the `:' token and width. This is where GCC used to
25502 parse attributes in the past, pedwarn if there is
25503 a std attribute. */
25504 if (cp_next_tokens_can_be_std_attribute_p (parser))
25505 pedwarn (input_location, OPT_Wpedantic,
25506 "ISO C++ allows bit-field attributes only "
25507 "before the %<:%> token");
25508
25509 late_attributes = cp_parser_attributes_opt (parser);
25510 }
25511
25512 attributes = attr_chainon (attributes, late_attributes);
25513
25514 /* Remember which attributes are prefix attributes and
25515 which are not. */
25516 first_attribute = attributes;
25517 /* Combine the attributes. */
25518 attributes = attr_chainon (prefix_attributes, attributes);
25519
25520 /* Create the bitfield declaration. */
25521 decl = grokbitfield (identifier
25522 ? make_id_declarator (NULL_TREE,
25523 identifier,
25524 sfk_none,
25525 id_location)
25526 : NULL,
25527 &decl_specifiers,
25528 width, initializer,
25529 attributes);
25530 }
25531 else
25532 {
25533 cp_declarator *declarator;
25534 tree asm_specification;
25535 int ctor_dtor_or_conv_p;
25536 bool static_p = (decl_specifiers.storage_class == sc_static);
25537 cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
25538 /* We can't delay parsing for friends,
25539 alias-declarations, and typedefs, even though the
25540 standard seems to require it. */
25541 if (!friend_p
25542 && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25543 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
25544
25545 /* Parse the declarator. */
25546 declarator
25547 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25548 flags,
25549 &ctor_dtor_or_conv_p,
25550 /*parenthesized_p=*/NULL,
25551 /*member_p=*/true,
25552 friend_p, static_p);
25553
25554 /* If something went wrong parsing the declarator, make sure
25555 that we at least consume some tokens. */
25556 if (declarator == cp_error_declarator)
25557 {
25558 /* Skip to the end of the statement. */
25559 cp_parser_skip_to_end_of_statement (parser);
25560 /* If the next token is not a semicolon, that is
25561 probably because we just skipped over the body of
25562 a function. So, we consume a semicolon if
25563 present, but do not issue an error message if it
25564 is not present. */
25565 if (cp_lexer_next_token_is (parser->lexer,
25566 CPP_SEMICOLON))
25567 cp_lexer_consume_token (parser->lexer);
25568 goto out;
25569 }
25570
25571 if (declares_class_or_enum & 2)
25572 cp_parser_check_for_definition_in_return_type
25573 (declarator, decl_specifiers.type,
25574 decl_specifiers.locations[ds_type_spec]);
25575
25576 /* Look for an asm-specification. */
25577 asm_specification = cp_parser_asm_specification_opt (parser);
25578 /* Look for attributes that apply to the declaration. */
25579 attributes = cp_parser_attributes_opt (parser);
25580 /* Remember which attributes are prefix attributes and
25581 which are not. */
25582 first_attribute = attributes;
25583 /* Combine the attributes. */
25584 attributes = attr_chainon (prefix_attributes, attributes);
25585
25586 /* If it's an `=', then we have a constant-initializer or a
25587 pure-specifier. It is not correct to parse the
25588 initializer before registering the member declaration
25589 since the member declaration should be in scope while
25590 its initializer is processed. However, the rest of the
25591 front end does not yet provide an interface that allows
25592 us to handle this correctly. */
25593 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25594 {
25595 /* In [class.mem]:
25596
25597 A pure-specifier shall be used only in the declaration of
25598 a virtual function.
25599
25600 A member-declarator can contain a constant-initializer
25601 only if it declares a static member of integral or
25602 enumeration type.
25603
25604 Therefore, if the DECLARATOR is for a function, we look
25605 for a pure-specifier; otherwise, we look for a
25606 constant-initializer. When we call `grokfield', it will
25607 perform more stringent semantics checks. */
25608 initializer_token_start = cp_lexer_peek_token (parser->lexer);
25609 if (function_declarator_p (declarator)
25610 || (decl_specifiers.type
25611 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
25612 && declarator->kind == cdk_id
25613 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
25614 == FUNCTION_TYPE)))
25615 initializer = cp_parser_pure_specifier (parser);
25616 else if (decl_specifiers.storage_class != sc_static)
25617 initializer = cp_parser_save_nsdmi (parser);
25618 else if (cxx_dialect >= cxx11)
25619 {
25620 bool nonconst;
25621 /* Don't require a constant rvalue in C++11, since we
25622 might want a reference constant. We'll enforce
25623 constancy later. */
25624 cp_lexer_consume_token (parser->lexer);
25625 /* Parse the initializer. */
25626 initializer = cp_parser_initializer_clause (parser,
25627 &nonconst);
25628 }
25629 else
25630 /* Parse the initializer. */
25631 initializer = cp_parser_constant_initializer (parser);
25632 }
25633 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
25634 && !function_declarator_p (declarator))
25635 {
25636 bool x;
25637 if (decl_specifiers.storage_class != sc_static)
25638 initializer = cp_parser_save_nsdmi (parser);
25639 else
25640 initializer = cp_parser_initializer (parser, &x, &x);
25641 }
25642 /* Detect invalid bit-field cases such as
25643
25644 int *p : 4;
25645 int &&r : 3;
25646
25647 and similar. */
25648 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
25649 /* If there were no type specifiers, it was a
25650 constructor. */
25651 && decl_specifiers.any_type_specifiers_p)
25652 {
25653 /* This is called for a decent diagnostic only. */
25654 tree d = grokdeclarator (declarator, &decl_specifiers,
25655 BITFIELD, /*initialized=*/false,
25656 &attributes);
25657 if (!error_operand_p (d))
25658 error_at (DECL_SOURCE_LOCATION (d),
25659 "bit-field %qD has non-integral type %qT",
25660 d, TREE_TYPE (d));
25661 cp_parser_skip_to_end_of_statement (parser);
25662 /* Avoid "extra ;" pedwarns. */
25663 if (cp_lexer_next_token_is (parser->lexer,
25664 CPP_SEMICOLON))
25665 cp_lexer_consume_token (parser->lexer);
25666 goto out;
25667 }
25668 /* Otherwise, there is no initializer. */
25669 else
25670 initializer = NULL_TREE;
25671
25672 /* See if we are probably looking at a function
25673 definition. We are certainly not looking at a
25674 member-declarator. Calling `grokfield' has
25675 side-effects, so we must not do it unless we are sure
25676 that we are looking at a member-declarator. */
25677 if (cp_parser_token_starts_function_definition_p
25678 (cp_lexer_peek_token (parser->lexer)))
25679 {
25680 /* The grammar does not allow a pure-specifier to be
25681 used when a member function is defined. (It is
25682 possible that this fact is an oversight in the
25683 standard, since a pure function may be defined
25684 outside of the class-specifier. */
25685 if (initializer && initializer_token_start)
25686 error_at (initializer_token_start->location,
25687 "pure-specifier on function-definition");
25688 decl = cp_parser_save_member_function_body (parser,
25689 &decl_specifiers,
25690 declarator,
25691 attributes);
25692 if (parser->fully_implicit_function_template_p)
25693 decl = finish_fully_implicit_template (parser, decl);
25694 /* If the member was not a friend, declare it here. */
25695 if (!friend_p)
25696 finish_member_declaration (decl);
25697 /* Peek at the next token. */
25698 token = cp_lexer_peek_token (parser->lexer);
25699 /* If the next token is a semicolon, consume it. */
25700 if (token->type == CPP_SEMICOLON)
25701 {
25702 location_t semicolon_loc
25703 = cp_lexer_consume_token (parser->lexer)->location;
25704 gcc_rich_location richloc (semicolon_loc);
25705 richloc.add_fixit_remove ();
25706 warning_at (&richloc, OPT_Wextra_semi,
25707 "extra %<;%> after in-class "
25708 "function definition");
25709 }
25710 goto out;
25711 }
25712 else
25713 if (declarator->kind == cdk_function)
25714 declarator->id_loc = token->location;
25715 /* Create the declaration. */
25716 decl = grokfield (declarator, &decl_specifiers,
25717 initializer, /*init_const_expr_p=*/true,
25718 asm_specification, attributes);
25719 if (parser->fully_implicit_function_template_p)
25720 {
25721 if (friend_p)
25722 finish_fully_implicit_template (parser, 0);
25723 else
25724 decl = finish_fully_implicit_template (parser, decl);
25725 }
25726 }
25727
25728 cp_finalize_omp_declare_simd (parser, decl);
25729 cp_finalize_oacc_routine (parser, decl, false);
25730
25731 /* Reset PREFIX_ATTRIBUTES. */
25732 if (attributes != error_mark_node)
25733 {
25734 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25735 attributes = TREE_CHAIN (attributes);
25736 if (attributes)
25737 TREE_CHAIN (attributes) = NULL_TREE;
25738 }
25739
25740 /* If there is any qualification still in effect, clear it
25741 now; we will be starting fresh with the next declarator. */
25742 parser->scope = NULL_TREE;
25743 parser->qualifying_scope = NULL_TREE;
25744 parser->object_scope = NULL_TREE;
25745 /* If it's a `,', then there are more declarators. */
25746 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25747 {
25748 cp_lexer_consume_token (parser->lexer);
25749 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25750 {
25751 cp_token *token = cp_lexer_previous_token (parser->lexer);
25752 gcc_rich_location richloc (token->location);
25753 richloc.add_fixit_remove ();
25754 error_at (&richloc, "stray %<,%> at end of "
25755 "member declaration");
25756 }
25757 }
25758 /* If the next token isn't a `;', then we have a parse error. */
25759 else if (cp_lexer_next_token_is_not (parser->lexer,
25760 CPP_SEMICOLON))
25761 {
25762 /* The next token might be a ways away from where the
25763 actual semicolon is missing. Find the previous token
25764 and use that for our error position. */
25765 cp_token *token = cp_lexer_previous_token (parser->lexer);
25766 gcc_rich_location richloc (token->location);
25767 richloc.add_fixit_insert_after (";");
25768 error_at (&richloc, "expected %<;%> at end of "
25769 "member declaration");
25770
25771 /* Assume that the user meant to provide a semicolon. If
25772 we were to cp_parser_skip_to_end_of_statement, we might
25773 skip to a semicolon inside a member function definition
25774 and issue nonsensical error messages. */
25775 assume_semicolon = true;
25776 }
25777
25778 if (decl)
25779 {
25780 /* Add DECL to the list of members. */
25781 if (!friend_p
25782 /* Explicitly include, eg, NSDMIs, for better error
25783 recovery (c++/58650). */
25784 || !DECL_DECLARES_FUNCTION_P (decl))
25785 finish_member_declaration (decl);
25786
25787 if (TREE_CODE (decl) == FUNCTION_DECL)
25788 cp_parser_save_default_args (parser, decl);
25789 else if (TREE_CODE (decl) == FIELD_DECL
25790 && DECL_INITIAL (decl))
25791 /* Add DECL to the queue of NSDMI to be parsed later. */
25792 vec_safe_push (unparsed_nsdmis, decl);
25793 }
25794
25795 if (assume_semicolon)
25796 goto out;
25797 }
25798 }
25799
25800 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25801 out:
25802 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
25803 }
25804
25805 /* Parse a pure-specifier.
25806
25807 pure-specifier:
25808 = 0
25809
25810 Returns INTEGER_ZERO_NODE if a pure specifier is found.
25811 Otherwise, ERROR_MARK_NODE is returned. */
25812
25813 static tree
25814 cp_parser_pure_specifier (cp_parser* parser)
25815 {
25816 cp_token *token;
25817
25818 /* Look for the `=' token. */
25819 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25820 return error_mark_node;
25821 /* Look for the `0' token. */
25822 token = cp_lexer_peek_token (parser->lexer);
25823
25824 if (token->type == CPP_EOF
25825 || token->type == CPP_PRAGMA_EOL)
25826 return error_mark_node;
25827
25828 cp_lexer_consume_token (parser->lexer);
25829
25830 /* Accept = default or = delete in c++0x mode. */
25831 if (token->keyword == RID_DEFAULT
25832 || token->keyword == RID_DELETE)
25833 {
25834 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
25835 return token->u.value;
25836 }
25837
25838 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
25839 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
25840 {
25841 cp_parser_error (parser,
25842 "invalid pure specifier (only %<= 0%> is allowed)");
25843 cp_parser_skip_to_end_of_statement (parser);
25844 return error_mark_node;
25845 }
25846 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
25847 {
25848 error_at (token->location, "templates may not be %<virtual%>");
25849 return error_mark_node;
25850 }
25851
25852 return integer_zero_node;
25853 }
25854
25855 /* Parse a constant-initializer.
25856
25857 constant-initializer:
25858 = constant-expression
25859
25860 Returns a representation of the constant-expression. */
25861
25862 static tree
25863 cp_parser_constant_initializer (cp_parser* parser)
25864 {
25865 /* Look for the `=' token. */
25866 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25867 return error_mark_node;
25868
25869 /* It is invalid to write:
25870
25871 struct S { static const int i = { 7 }; };
25872
25873 */
25874 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25875 {
25876 cp_parser_error (parser,
25877 "a brace-enclosed initializer is not allowed here");
25878 /* Consume the opening brace. */
25879 matching_braces braces;
25880 braces.consume_open (parser);
25881 /* Skip the initializer. */
25882 cp_parser_skip_to_closing_brace (parser);
25883 /* Look for the trailing `}'. */
25884 braces.require_close (parser);
25885
25886 return error_mark_node;
25887 }
25888
25889 return cp_parser_constant_expression (parser);
25890 }
25891
25892 /* Derived classes [gram.class.derived] */
25893
25894 /* Parse a base-clause.
25895
25896 base-clause:
25897 : base-specifier-list
25898
25899 base-specifier-list:
25900 base-specifier ... [opt]
25901 base-specifier-list , base-specifier ... [opt]
25902
25903 Returns a TREE_LIST representing the base-classes, in the order in
25904 which they were declared. The representation of each node is as
25905 described by cp_parser_base_specifier.
25906
25907 In the case that no bases are specified, this function will return
25908 NULL_TREE, not ERROR_MARK_NODE. */
25909
25910 static tree
25911 cp_parser_base_clause (cp_parser* parser)
25912 {
25913 tree bases = NULL_TREE;
25914
25915 /* Look for the `:' that begins the list. */
25916 cp_parser_require (parser, CPP_COLON, RT_COLON);
25917
25918 /* Scan the base-specifier-list. */
25919 while (true)
25920 {
25921 cp_token *token;
25922 tree base;
25923 bool pack_expansion_p = false;
25924
25925 /* Look for the base-specifier. */
25926 base = cp_parser_base_specifier (parser);
25927 /* Look for the (optional) ellipsis. */
25928 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25929 {
25930 /* Consume the `...'. */
25931 cp_lexer_consume_token (parser->lexer);
25932
25933 pack_expansion_p = true;
25934 }
25935
25936 /* Add BASE to the front of the list. */
25937 if (base && base != error_mark_node)
25938 {
25939 if (pack_expansion_p)
25940 /* Make this a pack expansion type. */
25941 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
25942
25943 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
25944 {
25945 TREE_CHAIN (base) = bases;
25946 bases = base;
25947 }
25948 }
25949 /* Peek at the next token. */
25950 token = cp_lexer_peek_token (parser->lexer);
25951 /* If it's not a comma, then the list is complete. */
25952 if (token->type != CPP_COMMA)
25953 break;
25954 /* Consume the `,'. */
25955 cp_lexer_consume_token (parser->lexer);
25956 }
25957
25958 /* PARSER->SCOPE may still be non-NULL at this point, if the last
25959 base class had a qualified name. However, the next name that
25960 appears is certainly not qualified. */
25961 parser->scope = NULL_TREE;
25962 parser->qualifying_scope = NULL_TREE;
25963 parser->object_scope = NULL_TREE;
25964
25965 return nreverse (bases);
25966 }
25967
25968 /* Parse a base-specifier.
25969
25970 base-specifier:
25971 :: [opt] nested-name-specifier [opt] class-name
25972 virtual access-specifier [opt] :: [opt] nested-name-specifier
25973 [opt] class-name
25974 access-specifier virtual [opt] :: [opt] nested-name-specifier
25975 [opt] class-name
25976
25977 Returns a TREE_LIST. The TREE_PURPOSE will be one of
25978 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
25979 indicate the specifiers provided. The TREE_VALUE will be a TYPE
25980 (or the ERROR_MARK_NODE) indicating the type that was specified. */
25981
25982 static tree
25983 cp_parser_base_specifier (cp_parser* parser)
25984 {
25985 cp_token *token;
25986 bool done = false;
25987 bool virtual_p = false;
25988 bool duplicate_virtual_error_issued_p = false;
25989 bool duplicate_access_error_issued_p = false;
25990 bool class_scope_p, template_p;
25991 tree access = access_default_node;
25992 tree type;
25993
25994 /* Process the optional `virtual' and `access-specifier'. */
25995 while (!done)
25996 {
25997 /* Peek at the next token. */
25998 token = cp_lexer_peek_token (parser->lexer);
25999 /* Process `virtual'. */
26000 switch (token->keyword)
26001 {
26002 case RID_VIRTUAL:
26003 /* If `virtual' appears more than once, issue an error. */
26004 if (virtual_p && !duplicate_virtual_error_issued_p)
26005 {
26006 cp_parser_error (parser,
26007 "%<virtual%> specified more than once in base-specifier");
26008 duplicate_virtual_error_issued_p = true;
26009 }
26010
26011 virtual_p = true;
26012
26013 /* Consume the `virtual' token. */
26014 cp_lexer_consume_token (parser->lexer);
26015
26016 break;
26017
26018 case RID_PUBLIC:
26019 case RID_PROTECTED:
26020 case RID_PRIVATE:
26021 /* If more than one access specifier appears, issue an
26022 error. */
26023 if (access != access_default_node
26024 && !duplicate_access_error_issued_p)
26025 {
26026 cp_parser_error (parser,
26027 "more than one access specifier in base-specifier");
26028 duplicate_access_error_issued_p = true;
26029 }
26030
26031 access = ridpointers[(int) token->keyword];
26032
26033 /* Consume the access-specifier. */
26034 cp_lexer_consume_token (parser->lexer);
26035
26036 break;
26037
26038 default:
26039 done = true;
26040 break;
26041 }
26042 }
26043 /* It is not uncommon to see programs mechanically, erroneously, use
26044 the 'typename' keyword to denote (dependent) qualified types
26045 as base classes. */
26046 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26047 {
26048 token = cp_lexer_peek_token (parser->lexer);
26049 if (!processing_template_decl)
26050 error_at (token->location,
26051 "keyword %<typename%> not allowed outside of templates");
26052 else
26053 error_at (token->location,
26054 "keyword %<typename%> not allowed in this context "
26055 "(the base class is implicitly a type)");
26056 cp_lexer_consume_token (parser->lexer);
26057 }
26058
26059 /* Look for the optional `::' operator. */
26060 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
26061 /* Look for the nested-name-specifier. The simplest way to
26062 implement:
26063
26064 [temp.res]
26065
26066 The keyword `typename' is not permitted in a base-specifier or
26067 mem-initializer; in these contexts a qualified name that
26068 depends on a template-parameter is implicitly assumed to be a
26069 type name.
26070
26071 is to pretend that we have seen the `typename' keyword at this
26072 point. */
26073 cp_parser_nested_name_specifier_opt (parser,
26074 /*typename_keyword_p=*/true,
26075 /*check_dependency_p=*/true,
26076 /*type_p=*/true,
26077 /*is_declaration=*/true);
26078 /* If the base class is given by a qualified name, assume that names
26079 we see are type names or templates, as appropriate. */
26080 class_scope_p = (parser->scope && TYPE_P (parser->scope));
26081 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
26082
26083 if (!parser->scope
26084 && cp_lexer_next_token_is_decltype (parser->lexer))
26085 /* DR 950 allows decltype as a base-specifier. */
26086 type = cp_parser_decltype (parser);
26087 else
26088 {
26089 /* Otherwise, look for the class-name. */
26090 type = cp_parser_class_name (parser,
26091 class_scope_p,
26092 template_p,
26093 typename_type,
26094 /*check_dependency_p=*/true,
26095 /*class_head_p=*/false,
26096 /*is_declaration=*/true);
26097 type = TREE_TYPE (type);
26098 }
26099
26100 if (type == error_mark_node)
26101 return error_mark_node;
26102
26103 return finish_base_specifier (type, access, virtual_p);
26104 }
26105
26106 /* Exception handling [gram.exception] */
26107
26108 /* Save the tokens that make up the noexcept-specifier for a member-function.
26109 Returns a DEFERRED_PARSE. */
26110
26111 static tree
26112 cp_parser_save_noexcept (cp_parser *parser)
26113 {
26114 cp_token *first = parser->lexer->next_token;
26115 /* We want everything up to, including, the final ')'. */
26116 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
26117 cp_token *last = parser->lexer->next_token;
26118
26119 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
26120 to carry the information we will need. */
26121 tree expr = make_node (DEFERRED_PARSE);
26122 /* Save away the noexcept-specifier; we will process it when the
26123 class is complete. */
26124 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
26125 expr = build_tree_list (expr, NULL_TREE);
26126 return expr;
26127 }
26128
26129 /* Used for late processing of noexcept-specifiers of member-functions.
26130 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
26131 we saved for later; parse it now. DECL is the declaration of the
26132 member function. */
26133
26134 static tree
26135 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
26136 {
26137 /* Make sure we've gotten something that hasn't been parsed yet. */
26138 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
26139
26140 push_unparsed_function_queues (parser);
26141
26142 /* Push the saved tokens for the noexcept-specifier onto the parser's
26143 lexer stack. */
26144 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
26145 cp_parser_push_lexer_for_tokens (parser, tokens);
26146
26147 /* Parse the cached noexcept-specifier. */
26148 tree parsed_arg
26149 = cp_parser_noexcept_specification_opt (parser,
26150 CP_PARSER_FLAGS_NONE,
26151 /*require_constexpr=*/true,
26152 /*consumed_expr=*/NULL,
26153 /*return_cond=*/false);
26154
26155 /* Revert to the main lexer. */
26156 cp_parser_pop_lexer (parser);
26157
26158 /* Restore the queue. */
26159 pop_unparsed_function_queues (parser);
26160
26161 /* And we're done. */
26162 return parsed_arg;
26163 }
26164
26165 /* Perform late checking of overriding function with respect to their
26166 noexcept-specifiers. TYPE is the class and FNDECL is the function
26167 that potentially overrides some virtual function with the same
26168 signature. */
26169
26170 static void
26171 noexcept_override_late_checks (tree type, tree fndecl)
26172 {
26173 tree binfo = TYPE_BINFO (type);
26174 tree base_binfo;
26175
26176 if (DECL_STATIC_FUNCTION_P (fndecl))
26177 return;
26178
26179 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
26180 {
26181 tree basetype = BINFO_TYPE (base_binfo);
26182
26183 if (!TYPE_POLYMORPHIC_P (basetype))
26184 continue;
26185
26186 tree fn = look_for_overrides_here (basetype, fndecl);
26187 if (fn)
26188 maybe_check_overriding_exception_spec (fndecl, fn);
26189 }
26190 }
26191
26192 /* Parse an (optional) noexcept-specification.
26193
26194 noexcept-specification:
26195 noexcept ( constant-expression ) [opt]
26196
26197 If no noexcept-specification is present, returns NULL_TREE.
26198 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
26199 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
26200 there are no parentheses. CONSUMED_EXPR will be set accordingly.
26201 Otherwise, returns a noexcept specification unless RETURN_COND is true,
26202 in which case a boolean condition is returned instead. The parser flags
26203 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
26204 the (member) function is `const'. */
26205
26206 static tree
26207 cp_parser_noexcept_specification_opt (cp_parser* parser,
26208 cp_parser_flags flags,
26209 bool require_constexpr,
26210 bool* consumed_expr,
26211 bool return_cond)
26212 {
26213 cp_token *token;
26214 const char *saved_message;
26215
26216 /* Peek at the next token. */
26217 token = cp_lexer_peek_token (parser->lexer);
26218
26219 /* Is it a noexcept-specification? */
26220 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
26221 {
26222 tree expr;
26223
26224 /* [class.mem]/6 says that a noexcept-specifer (within the
26225 member-specification of the class) is a complete-class context of
26226 a class. So, if the noexcept-specifier has the optional expression,
26227 just save the tokens, and reparse this after we're done with the
26228 class. */
26229
26230 if ((flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
26231 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
26232 /* No need to delay parsing for a number literal or true/false. */
26233 && !((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
26234 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26235 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
26236 && at_class_scope_p ()
26237 && TYPE_BEING_DEFINED (current_class_type)
26238 && !LAMBDA_TYPE_P (current_class_type))
26239 return cp_parser_save_noexcept (parser);
26240
26241 cp_lexer_consume_token (parser->lexer);
26242
26243 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
26244 {
26245 matching_parens parens;
26246 parens.consume_open (parser);
26247
26248 if (require_constexpr)
26249 {
26250 /* Types may not be defined in an exception-specification. */
26251 saved_message = parser->type_definition_forbidden_message;
26252 parser->type_definition_forbidden_message
26253 = G_("types may not be defined in an exception-specification");
26254
26255 bool non_constant_p;
26256 expr
26257 = cp_parser_constant_expression (parser,
26258 /*allow_non_constant=*/true,
26259 &non_constant_p);
26260 if (non_constant_p
26261 && !require_potential_rvalue_constant_expression (expr))
26262 {
26263 expr = NULL_TREE;
26264 return_cond = true;
26265 }
26266
26267 /* Restore the saved message. */
26268 parser->type_definition_forbidden_message = saved_message;
26269 }
26270 else
26271 {
26272 expr = cp_parser_expression (parser);
26273 *consumed_expr = true;
26274 }
26275
26276 parens.require_close (parser);
26277 }
26278 else
26279 {
26280 expr = boolean_true_node;
26281 if (!require_constexpr)
26282 *consumed_expr = false;
26283 }
26284
26285 /* We cannot build a noexcept-spec right away because this will check
26286 that expr is a constexpr. */
26287 if (!return_cond)
26288 return build_noexcept_spec (expr, tf_warning_or_error);
26289 else
26290 return expr;
26291 }
26292 else
26293 return NULL_TREE;
26294 }
26295
26296 /* Parse an (optional) exception-specification.
26297
26298 exception-specification:
26299 throw ( type-id-list [opt] )
26300
26301 Returns a TREE_LIST representing the exception-specification. The
26302 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
26303 control parsing. QUALS are qualifiers indicating whether the (member)
26304 function is `const'. */
26305
26306 static tree
26307 cp_parser_exception_specification_opt (cp_parser* parser,
26308 cp_parser_flags flags)
26309 {
26310 cp_token *token;
26311 tree type_id_list;
26312 const char *saved_message;
26313
26314 /* Peek at the next token. */
26315 token = cp_lexer_peek_token (parser->lexer);
26316
26317 /* Is it a noexcept-specification? */
26318 type_id_list
26319 = cp_parser_noexcept_specification_opt (parser, flags,
26320 /*require_constexpr=*/true,
26321 /*consumed_expr=*/NULL,
26322 /*return_cond=*/false);
26323 if (type_id_list != NULL_TREE)
26324 return type_id_list;
26325
26326 /* If it's not `throw', then there's no exception-specification. */
26327 if (!cp_parser_is_keyword (token, RID_THROW))
26328 return NULL_TREE;
26329
26330 location_t loc = token->location;
26331
26332 /* Consume the `throw'. */
26333 cp_lexer_consume_token (parser->lexer);
26334
26335 /* Look for the `('. */
26336 matching_parens parens;
26337 parens.require_open (parser);
26338
26339 /* Peek at the next token. */
26340 token = cp_lexer_peek_token (parser->lexer);
26341 /* If it's not a `)', then there is a type-id-list. */
26342 if (token->type != CPP_CLOSE_PAREN)
26343 {
26344 /* Types may not be defined in an exception-specification. */
26345 saved_message = parser->type_definition_forbidden_message;
26346 parser->type_definition_forbidden_message
26347 = G_("types may not be defined in an exception-specification");
26348 /* Parse the type-id-list. */
26349 type_id_list = cp_parser_type_id_list (parser);
26350 /* Restore the saved message. */
26351 parser->type_definition_forbidden_message = saved_message;
26352
26353 if (cxx_dialect >= cxx17)
26354 {
26355 error_at (loc, "ISO C++17 does not allow dynamic exception "
26356 "specifications");
26357 type_id_list = NULL_TREE;
26358 }
26359 else if (cxx_dialect >= cxx11)
26360 warning_at (loc, OPT_Wdeprecated,
26361 "dynamic exception specifications are deprecated in "
26362 "C++11");
26363 }
26364 /* In C++17, throw() is equivalent to noexcept (true). throw()
26365 is deprecated in C++11 and above as well, but is still widely used,
26366 so don't warn about it yet. */
26367 else if (cxx_dialect >= cxx17)
26368 type_id_list = noexcept_true_spec;
26369 else
26370 type_id_list = empty_except_spec;
26371
26372 /* Look for the `)'. */
26373 parens.require_close (parser);
26374
26375 return type_id_list;
26376 }
26377
26378 /* Parse an (optional) type-id-list.
26379
26380 type-id-list:
26381 type-id ... [opt]
26382 type-id-list , type-id ... [opt]
26383
26384 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
26385 in the order that the types were presented. */
26386
26387 static tree
26388 cp_parser_type_id_list (cp_parser* parser)
26389 {
26390 tree types = NULL_TREE;
26391
26392 while (true)
26393 {
26394 cp_token *token;
26395 tree type;
26396
26397 token = cp_lexer_peek_token (parser->lexer);
26398
26399 /* Get the next type-id. */
26400 type = cp_parser_type_id (parser);
26401 /* Check for invalid 'auto'. */
26402 if (flag_concepts && type_uses_auto (type))
26403 {
26404 error_at (token->location,
26405 "invalid use of %<auto%> in exception-specification");
26406 type = error_mark_node;
26407 }
26408 /* Parse the optional ellipsis. */
26409 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26410 {
26411 /* Consume the `...'. */
26412 cp_lexer_consume_token (parser->lexer);
26413
26414 /* Turn the type into a pack expansion expression. */
26415 type = make_pack_expansion (type);
26416 }
26417 /* Add it to the list. */
26418 types = add_exception_specifier (types, type, /*complain=*/1);
26419 /* Peek at the next token. */
26420 token = cp_lexer_peek_token (parser->lexer);
26421 /* If it is not a `,', we are done. */
26422 if (token->type != CPP_COMMA)
26423 break;
26424 /* Consume the `,'. */
26425 cp_lexer_consume_token (parser->lexer);
26426 }
26427
26428 return nreverse (types);
26429 }
26430
26431 /* Parse a try-block.
26432
26433 try-block:
26434 try compound-statement handler-seq */
26435
26436 static tree
26437 cp_parser_try_block (cp_parser* parser)
26438 {
26439 tree try_block;
26440
26441 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
26442 if (parser->in_function_body
26443 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
26444 && cxx_dialect < cxx20)
26445 pedwarn (input_location, 0,
26446 "%<try%> in %<constexpr%> function only "
26447 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26448
26449 try_block = begin_try_block ();
26450 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
26451 finish_try_block (try_block);
26452 cp_parser_handler_seq (parser);
26453 finish_handler_sequence (try_block);
26454
26455 return try_block;
26456 }
26457
26458 /* Parse a function-try-block.
26459
26460 function-try-block:
26461 try ctor-initializer [opt] function-body handler-seq */
26462
26463 static void
26464 cp_parser_function_try_block (cp_parser* parser)
26465 {
26466 tree compound_stmt;
26467 tree try_block;
26468
26469 /* Look for the `try' keyword. */
26470 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
26471 return;
26472 /* Let the rest of the front end know where we are. */
26473 try_block = begin_function_try_block (&compound_stmt);
26474 /* Parse the function-body. */
26475 cp_parser_ctor_initializer_opt_and_function_body
26476 (parser, /*in_function_try_block=*/true);
26477 /* We're done with the `try' part. */
26478 finish_function_try_block (try_block);
26479 /* Parse the handlers. */
26480 cp_parser_handler_seq (parser);
26481 /* We're done with the handlers. */
26482 finish_function_handler_sequence (try_block, compound_stmt);
26483 }
26484
26485 /* Parse a handler-seq.
26486
26487 handler-seq:
26488 handler handler-seq [opt] */
26489
26490 static void
26491 cp_parser_handler_seq (cp_parser* parser)
26492 {
26493 while (true)
26494 {
26495 cp_token *token;
26496
26497 /* Parse the handler. */
26498 cp_parser_handler (parser);
26499 /* Peek at the next token. */
26500 token = cp_lexer_peek_token (parser->lexer);
26501 /* If it's not `catch' then there are no more handlers. */
26502 if (!cp_parser_is_keyword (token, RID_CATCH))
26503 break;
26504 }
26505 }
26506
26507 /* Parse a handler.
26508
26509 handler:
26510 catch ( exception-declaration ) compound-statement */
26511
26512 static void
26513 cp_parser_handler (cp_parser* parser)
26514 {
26515 tree handler;
26516 tree declaration;
26517
26518 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
26519 handler = begin_handler ();
26520 matching_parens parens;
26521 parens.require_open (parser);
26522 declaration = cp_parser_exception_declaration (parser);
26523 finish_handler_parms (declaration, handler);
26524 parens.require_close (parser);
26525 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
26526 finish_handler (handler);
26527 }
26528
26529 /* Parse an exception-declaration.
26530
26531 exception-declaration:
26532 type-specifier-seq declarator
26533 type-specifier-seq abstract-declarator
26534 type-specifier-seq
26535 ...
26536
26537 Returns a VAR_DECL for the declaration, or NULL_TREE if the
26538 ellipsis variant is used. */
26539
26540 static tree
26541 cp_parser_exception_declaration (cp_parser* parser)
26542 {
26543 cp_decl_specifier_seq type_specifiers;
26544 cp_declarator *declarator;
26545 const char *saved_message;
26546
26547 /* If it's an ellipsis, it's easy to handle. */
26548 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26549 {
26550 /* Consume the `...' token. */
26551 cp_lexer_consume_token (parser->lexer);
26552 return NULL_TREE;
26553 }
26554
26555 /* Types may not be defined in exception-declarations. */
26556 saved_message = parser->type_definition_forbidden_message;
26557 parser->type_definition_forbidden_message
26558 = G_("types may not be defined in exception-declarations");
26559
26560 /* Parse the type-specifier-seq. */
26561 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
26562 /*is_declaration=*/true,
26563 /*is_trailing_return=*/false,
26564 &type_specifiers);
26565 /* If it's a `)', then there is no declarator. */
26566 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26567 declarator = NULL;
26568 else
26569 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
26570 CP_PARSER_FLAGS_NONE,
26571 /*ctor_dtor_or_conv_p=*/NULL,
26572 /*parenthesized_p=*/NULL,
26573 /*member_p=*/false,
26574 /*friend_p=*/false,
26575 /*static_p=*/false);
26576
26577 /* Restore the saved message. */
26578 parser->type_definition_forbidden_message = saved_message;
26579
26580 if (!type_specifiers.any_specifiers_p)
26581 return error_mark_node;
26582
26583 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
26584 }
26585
26586 /* Parse a throw-expression.
26587
26588 throw-expression:
26589 throw assignment-expression [opt]
26590
26591 Returns a THROW_EXPR representing the throw-expression. */
26592
26593 static tree
26594 cp_parser_throw_expression (cp_parser* parser)
26595 {
26596 tree expression;
26597 cp_token* token;
26598 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
26599
26600 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
26601 token = cp_lexer_peek_token (parser->lexer);
26602 /* Figure out whether or not there is an assignment-expression
26603 following the "throw" keyword. */
26604 if (token->type == CPP_COMMA
26605 || token->type == CPP_SEMICOLON
26606 || token->type == CPP_CLOSE_PAREN
26607 || token->type == CPP_CLOSE_SQUARE
26608 || token->type == CPP_CLOSE_BRACE
26609 || token->type == CPP_COLON)
26610 expression = NULL_TREE;
26611 else
26612 expression = cp_parser_assignment_expression (parser);
26613
26614 /* Construct a location e.g.:
26615 throw x
26616 ^~~~~~~
26617 with caret == start at the start of the "throw" token, and
26618 the end at the end of the final token we consumed. */
26619 location_t combined_loc = make_location (start_loc, start_loc,
26620 parser->lexer);
26621 expression = build_throw (combined_loc, expression);
26622
26623 return expression;
26624 }
26625
26626 /* Parse a yield-expression.
26627
26628 yield-expression:
26629 co_yield assignment-expression
26630 co_yield braced-init-list
26631
26632 Returns a CO_YIELD_EXPR representing the yield-expression. */
26633
26634 static tree
26635 cp_parser_yield_expression (cp_parser* parser)
26636 {
26637 tree expr;
26638
26639 cp_token *token = cp_lexer_peek_token (parser->lexer);
26640 location_t kw_loc = token->location; /* Save for later. */
26641
26642 cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
26643
26644 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26645 {
26646 bool expr_non_constant_p;
26647 cp_lexer_set_source_position (parser->lexer);
26648 /* ??? : probably a moot point? */
26649 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26650 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
26651 }
26652 else
26653 expr = cp_parser_assignment_expression (parser);
26654
26655 if (expr == error_mark_node)
26656 return expr;
26657
26658 return finish_co_yield_expr (kw_loc, expr);
26659 }
26660
26661 /* GNU Extensions */
26662
26663 /* Parse an (optional) asm-specification.
26664
26665 asm-specification:
26666 asm ( string-literal )
26667
26668 If the asm-specification is present, returns a STRING_CST
26669 corresponding to the string-literal. Otherwise, returns
26670 NULL_TREE. */
26671
26672 static tree
26673 cp_parser_asm_specification_opt (cp_parser* parser)
26674 {
26675 cp_token *token;
26676 tree asm_specification;
26677
26678 /* Peek at the next token. */
26679 token = cp_lexer_peek_token (parser->lexer);
26680 /* If the next token isn't the `asm' keyword, then there's no
26681 asm-specification. */
26682 if (!cp_parser_is_keyword (token, RID_ASM))
26683 return NULL_TREE;
26684
26685 /* Consume the `asm' token. */
26686 cp_lexer_consume_token (parser->lexer);
26687 /* Look for the `('. */
26688 matching_parens parens;
26689 parens.require_open (parser);
26690
26691 /* Look for the string-literal. */
26692 asm_specification = cp_parser_string_literal (parser, false, false);
26693
26694 /* Look for the `)'. */
26695 parens.require_close (parser);
26696
26697 return asm_specification;
26698 }
26699
26700 /* Parse an asm-operand-list.
26701
26702 asm-operand-list:
26703 asm-operand
26704 asm-operand-list , asm-operand
26705
26706 asm-operand:
26707 string-literal ( expression )
26708 [ string-literal ] string-literal ( expression )
26709
26710 Returns a TREE_LIST representing the operands. The TREE_VALUE of
26711 each node is the expression. The TREE_PURPOSE is itself a
26712 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
26713 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
26714 is a STRING_CST for the string literal before the parenthesis. Returns
26715 ERROR_MARK_NODE if any of the operands are invalid. */
26716
26717 static tree
26718 cp_parser_asm_operand_list (cp_parser* parser)
26719 {
26720 tree asm_operands = NULL_TREE;
26721 bool invalid_operands = false;
26722
26723 while (true)
26724 {
26725 tree string_literal;
26726 tree expression;
26727 tree name;
26728
26729 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
26730 {
26731 /* Consume the `[' token. */
26732 cp_lexer_consume_token (parser->lexer);
26733 /* Read the operand name. */
26734 name = cp_parser_identifier (parser);
26735 if (name != error_mark_node)
26736 name = build_string (IDENTIFIER_LENGTH (name),
26737 IDENTIFIER_POINTER (name));
26738 /* Look for the closing `]'. */
26739 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26740 }
26741 else
26742 name = NULL_TREE;
26743 /* Look for the string-literal. */
26744 string_literal = cp_parser_string_literal (parser, false, false);
26745
26746 /* Look for the `('. */
26747 matching_parens parens;
26748 parens.require_open (parser);
26749 /* Parse the expression. */
26750 expression = cp_parser_expression (parser);
26751 /* Look for the `)'. */
26752 parens.require_close (parser);
26753
26754 if (name == error_mark_node
26755 || string_literal == error_mark_node
26756 || expression == error_mark_node)
26757 invalid_operands = true;
26758
26759 /* Add this operand to the list. */
26760 asm_operands = tree_cons (build_tree_list (name, string_literal),
26761 expression,
26762 asm_operands);
26763 /* If the next token is not a `,', there are no more
26764 operands. */
26765 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26766 break;
26767 /* Consume the `,'. */
26768 cp_lexer_consume_token (parser->lexer);
26769 }
26770
26771 return invalid_operands ? error_mark_node : nreverse (asm_operands);
26772 }
26773
26774 /* Parse an asm-clobber-list.
26775
26776 asm-clobber-list:
26777 string-literal
26778 asm-clobber-list , string-literal
26779
26780 Returns a TREE_LIST, indicating the clobbers in the order that they
26781 appeared. The TREE_VALUE of each node is a STRING_CST. */
26782
26783 static tree
26784 cp_parser_asm_clobber_list (cp_parser* parser)
26785 {
26786 tree clobbers = NULL_TREE;
26787
26788 while (true)
26789 {
26790 tree string_literal;
26791
26792 /* Look for the string literal. */
26793 string_literal = cp_parser_string_literal (parser, false, false);
26794 /* Add it to the list. */
26795 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
26796 /* If the next token is not a `,', then the list is
26797 complete. */
26798 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26799 break;
26800 /* Consume the `,' token. */
26801 cp_lexer_consume_token (parser->lexer);
26802 }
26803
26804 return clobbers;
26805 }
26806
26807 /* Parse an asm-label-list.
26808
26809 asm-label-list:
26810 identifier
26811 asm-label-list , identifier
26812
26813 Returns a TREE_LIST, indicating the labels in the order that they
26814 appeared. The TREE_VALUE of each node is a label. */
26815
26816 static tree
26817 cp_parser_asm_label_list (cp_parser* parser)
26818 {
26819 tree labels = NULL_TREE;
26820
26821 while (true)
26822 {
26823 tree identifier, label, name;
26824
26825 /* Look for the identifier. */
26826 identifier = cp_parser_identifier (parser);
26827 if (!error_operand_p (identifier))
26828 {
26829 label = lookup_label (identifier);
26830 if (TREE_CODE (label) == LABEL_DECL)
26831 {
26832 TREE_USED (label) = 1;
26833 check_goto (label);
26834 name = build_string (IDENTIFIER_LENGTH (identifier),
26835 IDENTIFIER_POINTER (identifier));
26836 labels = tree_cons (name, label, labels);
26837 }
26838 }
26839 /* If the next token is not a `,', then the list is
26840 complete. */
26841 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26842 break;
26843 /* Consume the `,' token. */
26844 cp_lexer_consume_token (parser->lexer);
26845 }
26846
26847 return nreverse (labels);
26848 }
26849
26850 /* Return TRUE iff the next tokens in the stream are possibly the
26851 beginning of a GNU extension attribute. */
26852
26853 static bool
26854 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
26855 {
26856 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
26857 }
26858
26859 /* Return TRUE iff the next tokens in the stream are possibly the
26860 beginning of a standard C++-11 attribute specifier. */
26861
26862 static bool
26863 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
26864 {
26865 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
26866 }
26867
26868 /* Return TRUE iff the next Nth tokens in the stream are possibly the
26869 beginning of a standard C++-11 attribute specifier. */
26870
26871 static bool
26872 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
26873 {
26874 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
26875
26876 return (cxx_dialect >= cxx11
26877 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
26878 || (token->type == CPP_OPEN_SQUARE
26879 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
26880 && token->type == CPP_OPEN_SQUARE)));
26881 }
26882
26883 /* Return TRUE iff the next Nth tokens in the stream are possibly the
26884 beginning of a GNU extension attribute. */
26885
26886 static bool
26887 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
26888 {
26889 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
26890
26891 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
26892 }
26893
26894 /* Return true iff the next tokens can be the beginning of either a
26895 GNU attribute list, or a standard C++11 attribute sequence. */
26896
26897 static bool
26898 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
26899 {
26900 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
26901 || cp_next_tokens_can_be_std_attribute_p (parser));
26902 }
26903
26904 /* Return true iff the next Nth tokens can be the beginning of either
26905 a GNU attribute list, or a standard C++11 attribute sequence. */
26906
26907 static bool
26908 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
26909 {
26910 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
26911 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
26912 }
26913
26914 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
26915 of GNU attributes, or return NULL. */
26916
26917 static tree
26918 cp_parser_attributes_opt (cp_parser *parser)
26919 {
26920 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
26921 return cp_parser_gnu_attributes_opt (parser);
26922 return cp_parser_std_attribute_spec_seq (parser);
26923 }
26924
26925 /* Parse an (optional) series of attributes.
26926
26927 attributes:
26928 attributes attribute
26929
26930 attribute:
26931 __attribute__ (( attribute-list [opt] ))
26932
26933 The return value is as for cp_parser_gnu_attribute_list. */
26934
26935 static tree
26936 cp_parser_gnu_attributes_opt (cp_parser* parser)
26937 {
26938 tree attributes = NULL_TREE;
26939
26940 temp_override<bool> cleanup
26941 (parser->auto_is_implicit_function_template_parm_p, false);
26942
26943 while (true)
26944 {
26945 cp_token *token;
26946 tree attribute_list;
26947 bool ok = true;
26948
26949 /* Peek at the next token. */
26950 token = cp_lexer_peek_token (parser->lexer);
26951 /* If it's not `__attribute__', then we're done. */
26952 if (token->keyword != RID_ATTRIBUTE)
26953 break;
26954
26955 /* Consume the `__attribute__' keyword. */
26956 cp_lexer_consume_token (parser->lexer);
26957 /* Look for the two `(' tokens. */
26958 matching_parens outer_parens;
26959 if (!outer_parens.require_open (parser))
26960 ok = false;
26961 matching_parens inner_parens;
26962 if (!inner_parens.require_open (parser))
26963 ok = false;
26964
26965 /* Peek at the next token. */
26966 token = cp_lexer_peek_token (parser->lexer);
26967 if (token->type != CPP_CLOSE_PAREN)
26968 /* Parse the attribute-list. */
26969 attribute_list = cp_parser_gnu_attribute_list (parser);
26970 else
26971 /* If the next token is a `)', then there is no attribute
26972 list. */
26973 attribute_list = NULL;
26974
26975 /* Look for the two `)' tokens. */
26976 if (!inner_parens.require_close (parser))
26977 ok = false;
26978 if (!outer_parens.require_close (parser))
26979 ok = false;
26980 if (!ok)
26981 cp_parser_skip_to_end_of_statement (parser);
26982
26983 /* Add these new attributes to the list. */
26984 attributes = attr_chainon (attributes, attribute_list);
26985 }
26986
26987 return attributes;
26988 }
26989
26990 /* Parse a GNU attribute-list.
26991
26992 attribute-list:
26993 attribute
26994 attribute-list , attribute
26995
26996 attribute:
26997 identifier
26998 identifier ( identifier )
26999 identifier ( identifier , expression-list )
27000 identifier ( expression-list )
27001
27002 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
27003 to an attribute. The TREE_PURPOSE of each node is the identifier
27004 indicating which attribute is in use. The TREE_VALUE represents
27005 the arguments, if any. */
27006
27007 static tree
27008 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
27009 {
27010 tree attribute_list = NULL_TREE;
27011 bool save_translate_strings_p = parser->translate_strings_p;
27012
27013 /* Don't create wrapper nodes within attributes: the
27014 handlers don't know how to handle them. */
27015 auto_suppress_location_wrappers sentinel;
27016
27017 parser->translate_strings_p = false;
27018 while (true)
27019 {
27020 cp_token *token;
27021 tree identifier;
27022 tree attribute;
27023
27024 /* Look for the identifier. We also allow keywords here; for
27025 example `__attribute__ ((const))' is legal. */
27026 token = cp_lexer_peek_token (parser->lexer);
27027 if (token->type == CPP_NAME
27028 || token->type == CPP_KEYWORD)
27029 {
27030 tree arguments = NULL_TREE;
27031
27032 /* Consume the token, but save it since we need it for the
27033 SIMD enabled function parsing. */
27034 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
27035
27036 /* Save away the identifier that indicates which attribute
27037 this is. */
27038 identifier = (token->type == CPP_KEYWORD)
27039 /* For keywords, use the canonical spelling, not the
27040 parsed identifier. */
27041 ? ridpointers[(int) token->keyword]
27042 : id_token->u.value;
27043
27044 identifier = canonicalize_attr_name (identifier);
27045 attribute = build_tree_list (identifier, NULL_TREE);
27046
27047 /* Peek at the next token. */
27048 token = cp_lexer_peek_token (parser->lexer);
27049 /* If it's an `(', then parse the attribute arguments. */
27050 if (token->type == CPP_OPEN_PAREN)
27051 {
27052 vec<tree, va_gc> *vec;
27053 int attr_flag = (attribute_takes_identifier_p (identifier)
27054 ? id_attr : normal_attr);
27055 vec = cp_parser_parenthesized_expression_list
27056 (parser, attr_flag, /*cast_p=*/false,
27057 /*allow_expansion_p=*/false,
27058 /*non_constant_p=*/NULL);
27059 if (vec == NULL)
27060 arguments = error_mark_node;
27061 else
27062 {
27063 arguments = build_tree_list_vec (vec);
27064 release_tree_vector (vec);
27065 }
27066 /* Save the arguments away. */
27067 TREE_VALUE (attribute) = arguments;
27068 }
27069
27070 if (arguments != error_mark_node)
27071 {
27072 /* Add this attribute to the list. */
27073 TREE_CHAIN (attribute) = attribute_list;
27074 attribute_list = attribute;
27075 }
27076
27077 token = cp_lexer_peek_token (parser->lexer);
27078 }
27079 /* Unless EXACTLY_ONE is set look for more attributes.
27080 If the next token isn't a `,', we're done. */
27081 if (exactly_one || token->type != CPP_COMMA)
27082 break;
27083
27084 /* Consume the comma and keep going. */
27085 cp_lexer_consume_token (parser->lexer);
27086 }
27087 parser->translate_strings_p = save_translate_strings_p;
27088
27089 /* We built up the list in reverse order. */
27090 return nreverse (attribute_list);
27091 }
27092
27093 /* Parse a standard C++11 attribute.
27094
27095 The returned representation is a TREE_LIST which TREE_PURPOSE is
27096 the scoped name of the attribute, and the TREE_VALUE is its
27097 arguments list.
27098
27099 Note that the scoped name of the attribute is itself a TREE_LIST
27100 which TREE_PURPOSE is the namespace of the attribute, and
27101 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
27102 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
27103 and which TREE_PURPOSE is directly the attribute name.
27104
27105 Clients of the attribute code should use get_attribute_namespace
27106 and get_attribute_name to get the actual namespace and name of
27107 attributes, regardless of their being GNU or C++11 attributes.
27108
27109 attribute:
27110 attribute-token attribute-argument-clause [opt]
27111
27112 attribute-token:
27113 identifier
27114 attribute-scoped-token
27115
27116 attribute-scoped-token:
27117 attribute-namespace :: identifier
27118
27119 attribute-namespace:
27120 identifier
27121
27122 attribute-argument-clause:
27123 ( balanced-token-seq )
27124
27125 balanced-token-seq:
27126 balanced-token [opt]
27127 balanced-token-seq balanced-token
27128
27129 balanced-token:
27130 ( balanced-token-seq )
27131 [ balanced-token-seq ]
27132 { balanced-token-seq }. */
27133
27134 static tree
27135 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
27136 {
27137 tree attribute, attr_id = NULL_TREE, arguments;
27138 cp_token *token;
27139
27140 temp_override<bool> cleanup
27141 (parser->auto_is_implicit_function_template_parm_p, false);
27142
27143 /* First, parse name of the attribute, a.k.a attribute-token. */
27144
27145 token = cp_lexer_peek_token (parser->lexer);
27146 if (token->type == CPP_NAME)
27147 attr_id = token->u.value;
27148 else if (token->type == CPP_KEYWORD)
27149 attr_id = ridpointers[(int) token->keyword];
27150 else if (token->flags & NAMED_OP)
27151 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
27152
27153 if (attr_id == NULL_TREE)
27154 return NULL_TREE;
27155
27156 cp_lexer_consume_token (parser->lexer);
27157
27158 token = cp_lexer_peek_token (parser->lexer);
27159 if (token->type == CPP_SCOPE)
27160 {
27161 /* We are seeing a scoped attribute token. */
27162
27163 cp_lexer_consume_token (parser->lexer);
27164 if (attr_ns)
27165 error_at (token->location, "attribute using prefix used together "
27166 "with scoped attribute token");
27167 attr_ns = attr_id;
27168
27169 token = cp_lexer_peek_token (parser->lexer);
27170 if (token->type == CPP_NAME)
27171 attr_id = token->u.value;
27172 else if (token->type == CPP_KEYWORD)
27173 attr_id = ridpointers[(int) token->keyword];
27174 else if (token->flags & NAMED_OP)
27175 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
27176 else
27177 {
27178 error_at (token->location,
27179 "expected an identifier for the attribute name");
27180 return error_mark_node;
27181 }
27182 cp_lexer_consume_token (parser->lexer);
27183
27184 attr_ns = canonicalize_attr_name (attr_ns);
27185 attr_id = canonicalize_attr_name (attr_id);
27186 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
27187 NULL_TREE);
27188 token = cp_lexer_peek_token (parser->lexer);
27189 }
27190 else if (attr_ns)
27191 {
27192 attr_ns = canonicalize_attr_name (attr_ns);
27193 attr_id = canonicalize_attr_name (attr_id);
27194 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
27195 NULL_TREE);
27196 }
27197 else
27198 {
27199 attr_id = canonicalize_attr_name (attr_id);
27200 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
27201 NULL_TREE);
27202 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
27203 but no longer: we have to be able to tell [[noreturn]] and
27204 __attribute__((noreturn)) apart. */
27205 /* C++14 deprecated attribute is equivalent to GNU's. */
27206 if (is_attribute_p ("deprecated", attr_id))
27207 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
27208 /* C++17 fallthrough attribute is equivalent to GNU's. */
27209 else if (is_attribute_p ("fallthrough", attr_id))
27210 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
27211 /* Transactional Memory TS optimize_for_synchronized attribute is
27212 equivalent to GNU transaction_callable. */
27213 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
27214 TREE_PURPOSE (attribute)
27215 = get_identifier ("transaction_callable");
27216 /* Transactional Memory attributes are GNU attributes. */
27217 else if (tm_attr_to_mask (attr_id))
27218 TREE_PURPOSE (attribute) = attr_id;
27219 }
27220
27221 /* Now parse the optional argument clause of the attribute. */
27222
27223 if (token->type != CPP_OPEN_PAREN)
27224 return attribute;
27225
27226 {
27227 vec<tree, va_gc> *vec;
27228 int attr_flag = normal_attr;
27229
27230 /* Maybe we don't expect to see any arguments for this attribute. */
27231 const attribute_spec *as
27232 = lookup_attribute_spec (TREE_PURPOSE (attribute));
27233 if (as && as->max_length == 0)
27234 {
27235 error_at (token->location, "%qE attribute does not take any arguments",
27236 attr_id);
27237 cp_parser_skip_to_closing_parenthesis (parser,
27238 /*recovering=*/true,
27239 /*or_comma=*/false,
27240 /*consume_paren=*/true);
27241 return error_mark_node;
27242 }
27243
27244 if (attr_ns == gnu_identifier
27245 && attribute_takes_identifier_p (attr_id))
27246 /* A GNU attribute that takes an identifier in parameter. */
27247 attr_flag = id_attr;
27248
27249 if (as == NULL)
27250 {
27251 /* For unknown attributes, just skip balanced tokens instead of
27252 trying to parse the arguments. */
27253 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
27254 cp_lexer_consume_token (parser->lexer);
27255 return attribute;
27256 }
27257
27258 vec = cp_parser_parenthesized_expression_list
27259 (parser, attr_flag, /*cast_p=*/false,
27260 /*allow_expansion_p=*/true,
27261 /*non_constant_p=*/NULL);
27262 if (vec == NULL)
27263 arguments = error_mark_node;
27264 else
27265 {
27266 if (vec->is_empty ())
27267 /* e.g. [[attr()]]. */
27268 error_at (token->location, "parentheses must be omitted if "
27269 "%qE attribute argument list is empty",
27270 attr_id);
27271 arguments = build_tree_list_vec (vec);
27272 release_tree_vector (vec);
27273 }
27274
27275 if (arguments == error_mark_node)
27276 attribute = error_mark_node;
27277 else
27278 TREE_VALUE (attribute) = arguments;
27279 }
27280
27281 return attribute;
27282 }
27283
27284 /* Warn if the attribute ATTRIBUTE appears more than once in the
27285 attribute-list ATTRIBUTES. This used to be enforced for certain
27286 attributes, but the restriction was removed in P2156. Note that
27287 carries_dependency ([dcl.attr.depend]) isn't implemented yet in GCC.
27288 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
27289 found in ATTRIBUTES. */
27290
27291 static bool
27292 cp_parser_check_std_attribute (location_t loc, tree attributes, tree attribute)
27293 {
27294 static auto alist = { "noreturn", "deprecated", "nodiscard", "maybe_unused",
27295 "likely", "unlikely", "fallthrough",
27296 "no_unique_address" };
27297 if (attributes)
27298 for (const auto &a : alist)
27299 if (is_attribute_p (a, get_attribute_name (attribute))
27300 && lookup_attribute (a, attributes))
27301 {
27302 if (!from_macro_expansion_at (loc))
27303 warning_at (loc, OPT_Wattributes, "attribute %qs specified "
27304 "multiple times", a);
27305 return false;
27306 }
27307 return true;
27308 }
27309
27310 /* Parse a list of standard C++-11 attributes.
27311
27312 attribute-list:
27313 attribute [opt]
27314 attribute-list , attribute[opt]
27315 attribute ...
27316 attribute-list , attribute ...
27317 */
27318
27319 static tree
27320 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
27321 {
27322 tree attributes = NULL_TREE, attribute = NULL_TREE;
27323 cp_token *token = NULL;
27324
27325 while (true)
27326 {
27327 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27328 attribute = cp_parser_std_attribute (parser, attr_ns);
27329 if (attribute == error_mark_node)
27330 break;
27331 if (attribute != NULL_TREE)
27332 {
27333 if (cp_parser_check_std_attribute (loc, attributes, attribute))
27334 {
27335 TREE_CHAIN (attribute) = attributes;
27336 attributes = attribute;
27337 }
27338 }
27339 token = cp_lexer_peek_token (parser->lexer);
27340 if (token->type == CPP_ELLIPSIS)
27341 {
27342 cp_lexer_consume_token (parser->lexer);
27343 if (attribute == NULL_TREE)
27344 error_at (token->location,
27345 "expected attribute before %<...%>");
27346 else
27347 {
27348 tree pack = make_pack_expansion (TREE_VALUE (attribute));
27349 if (pack == error_mark_node)
27350 return error_mark_node;
27351 TREE_VALUE (attribute) = pack;
27352 }
27353 token = cp_lexer_peek_token (parser->lexer);
27354 }
27355 if (token->type != CPP_COMMA)
27356 break;
27357 cp_lexer_consume_token (parser->lexer);
27358 }
27359 attributes = nreverse (attributes);
27360 return attributes;
27361 }
27362
27363 /* Parse a standard C++-11 attribute specifier.
27364
27365 attribute-specifier:
27366 [ [ attribute-using-prefix [opt] attribute-list ] ]
27367 alignment-specifier
27368
27369 attribute-using-prefix:
27370 using attribute-namespace :
27371
27372 alignment-specifier:
27373 alignas ( type-id ... [opt] )
27374 alignas ( alignment-expression ... [opt] ). */
27375
27376 static tree
27377 cp_parser_std_attribute_spec (cp_parser *parser)
27378 {
27379 tree attributes = NULL_TREE;
27380 cp_token *token = cp_lexer_peek_token (parser->lexer);
27381
27382 if (token->type == CPP_OPEN_SQUARE
27383 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
27384 {
27385 tree attr_ns = NULL_TREE;
27386
27387 cp_lexer_consume_token (parser->lexer);
27388 cp_lexer_consume_token (parser->lexer);
27389
27390 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27391 {
27392 token = cp_lexer_peek_nth_token (parser->lexer, 2);
27393 if (token->type == CPP_NAME)
27394 attr_ns = token->u.value;
27395 else if (token->type == CPP_KEYWORD)
27396 attr_ns = ridpointers[(int) token->keyword];
27397 else if (token->flags & NAMED_OP)
27398 attr_ns = get_identifier (cpp_type2name (token->type,
27399 token->flags));
27400 if (attr_ns
27401 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
27402 {
27403 if (cxx_dialect < cxx17)
27404 pedwarn (input_location, 0,
27405 "attribute using prefix only available "
27406 "with %<-std=c++17%> or %<-std=gnu++17%>");
27407
27408 cp_lexer_consume_token (parser->lexer);
27409 cp_lexer_consume_token (parser->lexer);
27410 cp_lexer_consume_token (parser->lexer);
27411 }
27412 else
27413 attr_ns = NULL_TREE;
27414 }
27415
27416 attributes = cp_parser_std_attribute_list (parser, attr_ns);
27417
27418 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
27419 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
27420 cp_parser_skip_to_end_of_statement (parser);
27421 else
27422 /* Warn about parsing c++11 attribute in non-c++11 mode, only
27423 when we are sure that we have actually parsed them. */
27424 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
27425 }
27426 else
27427 {
27428 tree alignas_expr;
27429
27430 /* Look for an alignment-specifier. */
27431
27432 token = cp_lexer_peek_token (parser->lexer);
27433
27434 if (token->type != CPP_KEYWORD
27435 || token->keyword != RID_ALIGNAS)
27436 return NULL_TREE;
27437
27438 cp_lexer_consume_token (parser->lexer);
27439 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
27440
27441 matching_parens parens;
27442 if (!parens.require_open (parser))
27443 return error_mark_node;
27444
27445 cp_parser_parse_tentatively (parser);
27446 alignas_expr = cp_parser_type_id (parser);
27447
27448 if (!cp_parser_parse_definitely (parser))
27449 {
27450 alignas_expr = cp_parser_assignment_expression (parser);
27451 if (alignas_expr == error_mark_node)
27452 cp_parser_skip_to_end_of_statement (parser);
27453 if (alignas_expr == NULL_TREE
27454 || alignas_expr == error_mark_node)
27455 return alignas_expr;
27456 }
27457
27458 alignas_expr = cxx_alignas_expr (alignas_expr);
27459 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
27460
27461 /* Handle alignas (pack...). */
27462 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27463 {
27464 cp_lexer_consume_token (parser->lexer);
27465 alignas_expr = make_pack_expansion (alignas_expr);
27466 }
27467
27468 /* Something went wrong, so don't build the attribute. */
27469 if (alignas_expr == error_mark_node)
27470 return error_mark_node;
27471
27472 /* Missing ')' means the code cannot possibly be valid; go ahead
27473 and commit to make sure we issue a hard error. */
27474 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
27475 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27476 cp_parser_commit_to_tentative_parse (parser);
27477
27478 if (!parens.require_close (parser))
27479 return error_mark_node;
27480
27481 /* Build the C++-11 representation of an 'aligned'
27482 attribute. */
27483 attributes
27484 = build_tree_list (build_tree_list (gnu_identifier,
27485 aligned_identifier), alignas_expr);
27486 }
27487
27488 return attributes;
27489 }
27490
27491 /* Parse a standard C++-11 attribute-specifier-seq.
27492
27493 attribute-specifier-seq:
27494 attribute-specifier-seq [opt] attribute-specifier
27495 */
27496
27497 static tree
27498 cp_parser_std_attribute_spec_seq (cp_parser *parser)
27499 {
27500 tree attr_specs = NULL_TREE;
27501 tree attr_last = NULL_TREE;
27502
27503 /* Don't create wrapper nodes within attributes: the
27504 handlers don't know how to handle them. */
27505 auto_suppress_location_wrappers sentinel;
27506
27507 while (true)
27508 {
27509 tree attr_spec = cp_parser_std_attribute_spec (parser);
27510 if (attr_spec == NULL_TREE)
27511 break;
27512 if (attr_spec == error_mark_node)
27513 return error_mark_node;
27514
27515 if (attr_last)
27516 TREE_CHAIN (attr_last) = attr_spec;
27517 else
27518 attr_specs = attr_last = attr_spec;
27519 attr_last = tree_last (attr_last);
27520 }
27521
27522 return attr_specs;
27523 }
27524
27525 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
27526 return index of the first token after balanced-token, or N on failure. */
27527
27528 static size_t
27529 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
27530 {
27531 size_t orig_n = n;
27532 int nparens = 0, nbraces = 0, nsquares = 0;
27533 do
27534 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
27535 {
27536 case CPP_PRAGMA_EOL:
27537 if (!parser->lexer->in_pragma)
27538 break;
27539 /* FALLTHRU */
27540 case CPP_EOF:
27541 /* Ran out of tokens. */
27542 return orig_n;
27543 case CPP_OPEN_PAREN:
27544 ++nparens;
27545 break;
27546 case CPP_OPEN_BRACE:
27547 ++nbraces;
27548 break;
27549 case CPP_OPEN_SQUARE:
27550 ++nsquares;
27551 break;
27552 case CPP_CLOSE_PAREN:
27553 --nparens;
27554 break;
27555 case CPP_CLOSE_BRACE:
27556 --nbraces;
27557 break;
27558 case CPP_CLOSE_SQUARE:
27559 --nsquares;
27560 break;
27561 default:
27562 break;
27563 }
27564 while (nparens || nbraces || nsquares);
27565 return n;
27566 }
27567
27568 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
27569 return index of the first token after the GNU attribute tokens, or N on
27570 failure. */
27571
27572 static size_t
27573 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
27574 {
27575 while (true)
27576 {
27577 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
27578 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
27579 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
27580 break;
27581
27582 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
27583 if (n2 == n + 2)
27584 break;
27585 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
27586 break;
27587 n = n2 + 1;
27588 }
27589 return n;
27590 }
27591
27592 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
27593 next token), return index of the first token after the standard C++11
27594 attribute tokens, or N on failure. */
27595
27596 static size_t
27597 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
27598 {
27599 while (true)
27600 {
27601 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
27602 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
27603 {
27604 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
27605 if (n2 == n + 1)
27606 break;
27607 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
27608 break;
27609 n = n2 + 1;
27610 }
27611 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
27612 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
27613 {
27614 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
27615 if (n2 == n + 1)
27616 break;
27617 n = n2;
27618 }
27619 else
27620 break;
27621 }
27622 return n;
27623 }
27624
27625 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
27626 as the next token), return index of the first token after the attribute
27627 tokens, or N on failure. */
27628
27629 static size_t
27630 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
27631 {
27632 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
27633 return cp_parser_skip_gnu_attributes_opt (parser, n);
27634 return cp_parser_skip_std_attribute_spec_seq (parser, n);
27635 }
27636
27637 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
27638 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
27639 current value of the PEDANTIC flag, regardless of whether or not
27640 the `__extension__' keyword is present. The caller is responsible
27641 for restoring the value of the PEDANTIC flag. */
27642
27643 static bool
27644 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
27645 {
27646 /* Save the old value of the PEDANTIC flag. */
27647 *saved_pedantic = pedantic;
27648
27649 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
27650 {
27651 /* Consume the `__extension__' token. */
27652 cp_lexer_consume_token (parser->lexer);
27653 /* We're not being pedantic while the `__extension__' keyword is
27654 in effect. */
27655 pedantic = 0;
27656
27657 return true;
27658 }
27659
27660 return false;
27661 }
27662
27663 /* Parse a label declaration.
27664
27665 label-declaration:
27666 __label__ label-declarator-seq ;
27667
27668 label-declarator-seq:
27669 identifier , label-declarator-seq
27670 identifier */
27671
27672 static void
27673 cp_parser_label_declaration (cp_parser* parser)
27674 {
27675 /* Look for the `__label__' keyword. */
27676 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
27677
27678 while (true)
27679 {
27680 tree identifier;
27681
27682 /* Look for an identifier. */
27683 identifier = cp_parser_identifier (parser);
27684 /* If we failed, stop. */
27685 if (identifier == error_mark_node)
27686 break;
27687 /* Declare it as a label. */
27688 finish_label_decl (identifier);
27689 /* If the next token is a `;', stop. */
27690 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27691 break;
27692 /* Look for the `,' separating the label declarations. */
27693 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
27694 }
27695
27696 /* Look for the final `;'. */
27697 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27698 }
27699
27700 // -------------------------------------------------------------------------- //
27701 // Concept definitions
27702
27703 static tree
27704 cp_parser_concept_definition (cp_parser *parser)
27705 {
27706 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
27707 cp_lexer_consume_token (parser->lexer);
27708
27709 cp_expr id = cp_parser_identifier (parser);
27710 if (id == error_mark_node)
27711 {
27712 cp_parser_skip_to_end_of_statement (parser);
27713 cp_parser_consume_semicolon_at_end_of_statement (parser);
27714 return NULL_TREE;
27715 }
27716
27717 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27718 {
27719 cp_parser_skip_to_end_of_statement (parser);
27720 cp_parser_consume_semicolon_at_end_of_statement (parser);
27721 return error_mark_node;
27722 }
27723
27724 processing_constraint_expression_sentinel parsing_constraint;
27725 tree init = cp_parser_constraint_expression (parser);
27726 if (init == error_mark_node)
27727 cp_parser_skip_to_end_of_statement (parser);
27728
27729 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
27730 but continue as if it were. */
27731 cp_parser_consume_semicolon_at_end_of_statement (parser);
27732
27733 return finish_concept_definition (id, init);
27734 }
27735
27736 // -------------------------------------------------------------------------- //
27737 // Requires Clause
27738
27739 /* Diagnose an expression that should appear in ()'s within a requires-clause
27740 and suggest where to place those parentheses. */
27741
27742 static void
27743 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
27744 {
27745 error_at (loc, "expression must be enclosed in parentheses");
27746 }
27747
27748 static void
27749 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
27750 {
27751 gcc_rich_location richloc (loc);
27752 richloc.add_fixit_insert_before ("(");
27753 richloc.add_fixit_insert_after (")");
27754 error_at (&richloc, "expression must be enclosed in parentheses");
27755 }
27756
27757 /* Characterizes the likely kind of expression intended by a mis-written
27758 primary constraint. */
27759 enum primary_constraint_error
27760 {
27761 pce_ok,
27762 pce_maybe_operator,
27763 pce_maybe_postfix
27764 };
27765
27766 /* Returns true if the token(s) following a primary-expression in a
27767 constraint-logical-* expression would require parentheses. */
27768
27769 static primary_constraint_error
27770 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
27771 {
27772 cp_token *token = cp_lexer_peek_token (parser->lexer);
27773 switch (token->type)
27774 {
27775 default:
27776 return pce_ok;
27777
27778 case CPP_EQ:
27779 {
27780 /* An equal sign may be part of the definition of a function,
27781 and not an assignment operator, when parsing the expression
27782 for a trailing requires-clause. For example:
27783
27784 template<typename T>
27785 struct S {
27786 S() requires C<T> = default;
27787 };
27788
27789 Don't try to reparse this a binary operator. */
27790 if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
27791 || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
27792 return pce_ok;
27793
27794 gcc_fallthrough ();
27795 }
27796
27797 /* Arithmetic operators. */
27798 case CPP_PLUS:
27799 case CPP_MINUS:
27800 case CPP_MULT:
27801 case CPP_DIV:
27802 case CPP_MOD:
27803 /* Bitwise operators. */
27804 case CPP_AND:
27805 case CPP_OR:
27806 case CPP_XOR:
27807 case CPP_RSHIFT:
27808 case CPP_LSHIFT:
27809 /* Relational operators. */
27810 case CPP_EQ_EQ:
27811 case CPP_NOT_EQ:
27812 case CPP_LESS:
27813 case CPP_GREATER:
27814 case CPP_LESS_EQ:
27815 case CPP_GREATER_EQ:
27816 case CPP_SPACESHIP:
27817 /* Pointer-to-member. */
27818 case CPP_DOT_STAR:
27819 case CPP_DEREF_STAR:
27820 /* Assignment operators. */
27821 case CPP_PLUS_EQ:
27822 case CPP_MINUS_EQ:
27823 case CPP_MULT_EQ:
27824 case CPP_DIV_EQ:
27825 case CPP_MOD_EQ:
27826 case CPP_AND_EQ:
27827 case CPP_OR_EQ:
27828 case CPP_XOR_EQ:
27829 case CPP_RSHIFT_EQ:
27830 case CPP_LSHIFT_EQ:
27831 /* Conditional operator */
27832 case CPP_QUERY:
27833 /* Unenclosed binary or conditional operator. */
27834 return pce_maybe_operator;
27835
27836 case CPP_OPEN_PAREN:
27837 {
27838 /* A primary constraint that precedes the parameter-list of a
27839 lambda expression is followed by an open paren.
27840
27841 []<typename T> requires C (T a, T b) { ... }
27842
27843 Don't try to re-parse this as a postfix expression. */
27844 if (lambda_p)
27845 return pce_ok;
27846
27847 gcc_fallthrough ();
27848 }
27849 case CPP_OPEN_SQUARE:
27850 {
27851 /* A primary-constraint-expression followed by a '[[' is not a
27852 postfix expression. */
27853 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
27854 return pce_ok;
27855
27856 gcc_fallthrough ();
27857 }
27858 case CPP_PLUS_PLUS:
27859 case CPP_MINUS_MINUS:
27860 case CPP_DOT:
27861 case CPP_DEREF:
27862 /* Unenclosed postfix operator. */
27863 return pce_maybe_postfix;
27864 }
27865 }
27866
27867 /* Returns true if the next token begins a unary expression, preceded by
27868 an operator or keyword. */
27869
27870 static bool
27871 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
27872 {
27873 cp_token *token = cp_lexer_peek_token (parser->lexer);
27874 switch (token->type)
27875 {
27876 case CPP_NOT:
27877 case CPP_PLUS:
27878 case CPP_MINUS:
27879 case CPP_MULT:
27880 case CPP_COMPL:
27881 case CPP_PLUS_PLUS:
27882 case CPP_MINUS_MINUS:
27883 return true;
27884
27885 case CPP_KEYWORD:
27886 {
27887 switch (token->keyword)
27888 {
27889 case RID_STATCAST:
27890 case RID_DYNCAST:
27891 case RID_REINTCAST:
27892 case RID_CONSTCAST:
27893 case RID_TYPEID:
27894 case RID_SIZEOF:
27895 case RID_ALIGNOF:
27896 case RID_NOEXCEPT:
27897 case RID_NEW:
27898 case RID_DELETE:
27899 case RID_THROW:
27900 return true;
27901
27902 default:
27903 break;
27904 }
27905 }
27906
27907 default:
27908 break;
27909 }
27910
27911 return false;
27912 }
27913
27914 /* Parse a primary expression within a constraint. */
27915
27916 static cp_expr
27917 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
27918 {
27919 /* If this looks like a unary expression, parse it as such, but diagnose
27920 it as ill-formed; it requires parens. */
27921 if (cp_parser_unary_constraint_requires_parens (parser))
27922 {
27923 cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
27924 cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
27925 return e;
27926 }
27927
27928 cp_lexer_save_tokens (parser->lexer);
27929 cp_id_kind idk;
27930 location_t loc = input_location;
27931 cp_expr expr = cp_parser_primary_expression (parser,
27932 /*address_p=*/false,
27933 /*cast_p=*/false,
27934 /*template_arg_p=*/false,
27935 &idk);
27936 expr.maybe_add_location_wrapper ();
27937
27938 primary_constraint_error pce = pce_ok;
27939 if (expr != error_mark_node)
27940 {
27941 /* The primary-expression could be part of an unenclosed non-logical
27942 compound expression. */
27943 pce = cp_parser_constraint_requires_parens (parser, lambda_p);
27944 }
27945 if (pce == pce_ok)
27946 {
27947 cp_lexer_commit_tokens (parser->lexer);
27948 return finish_constraint_primary_expr (expr);
27949 }
27950
27951 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
27952 error, but return the expression as if it were valid. */
27953 cp_lexer_rollback_tokens (parser->lexer);
27954 cp_parser_parse_tentatively (parser);
27955 if (pce == pce_maybe_operator)
27956 expr = cp_parser_assignment_expression (parser, NULL, false, false);
27957 else
27958 expr = cp_parser_simple_cast_expression (parser);
27959 if (cp_parser_parse_definitely (parser))
27960 {
27961 cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
27962 return expr;
27963 }
27964
27965 /* Otherwise, something has gone very wrong, and we can't generate a more
27966 meaningful diagnostic or recover. */
27967 cp_parser_diagnose_ungrouped_constraint_plain (loc);
27968 return error_mark_node;
27969 }
27970
27971 /* Parse a constraint-logical-and-expression.
27972
27973 constraint-logical-and-expression:
27974 primary-expression
27975 constraint-logical-and-expression '&&' primary-expression */
27976
27977 static cp_expr
27978 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
27979 {
27980 cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
27981 while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
27982 {
27983 cp_token *op = cp_lexer_consume_token (parser->lexer);
27984 tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
27985 lhs = finish_constraint_and_expr (op->location, lhs, rhs);
27986 }
27987 return lhs;
27988 }
27989
27990 /* Parse a constraint-logical-or-expression.
27991
27992 constraint-logical-or-expression:
27993 constraint-logical-and-expression
27994 constraint-logical-or-expression '||' constraint-logical-and-expression */
27995
27996 static cp_expr
27997 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
27998 {
27999 cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
28000 while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
28001 {
28002 cp_token *op = cp_lexer_consume_token (parser->lexer);
28003 cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
28004 lhs = finish_constraint_or_expr (op->location, lhs, rhs);
28005 }
28006 return lhs;
28007 }
28008
28009 /* Parse the expression after a requires-clause. This has a different grammar
28010 than that in the concepts TS. */
28011
28012 static tree
28013 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
28014 {
28015 processing_constraint_expression_sentinel parsing_constraint;
28016 temp_override<int> ovr (processing_template_decl);
28017 if (!processing_template_decl)
28018 /* Adjust processing_template_decl so that we always obtain template
28019 trees here. We don't do the usual ++processing_template_decl
28020 because that would skew the template parameter depth of a lambda
28021 within if we're already inside a template. */
28022 processing_template_decl = 1;
28023 cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
28024 if (check_for_bare_parameter_packs (expr))
28025 expr = error_mark_node;
28026 return expr;
28027 }
28028
28029 /* Parse a expression after a requires clause.
28030
28031 constraint-expression:
28032 logical-or-expression
28033
28034 The required logical-or-expression must be a constant expression. Note
28035 that we don't check that the expression is constepxr here. We defer until
28036 we analyze constraints and then, we only check atomic constraints. */
28037
28038 static tree
28039 cp_parser_constraint_expression (cp_parser *parser)
28040 {
28041 processing_constraint_expression_sentinel parsing_constraint;
28042 temp_override<int> ovr (processing_template_decl);
28043 if (!processing_template_decl)
28044 /* As in cp_parser_requires_clause_expression. */
28045 processing_template_decl = 1;
28046 cp_expr expr = cp_parser_binary_expression (parser, false, true,
28047 PREC_NOT_OPERATOR, NULL);
28048 if (check_for_bare_parameter_packs (expr))
28049 expr = error_mark_node;
28050 expr.maybe_add_location_wrapper ();
28051 return expr;
28052 }
28053
28054 /* Optionally parse a requires clause:
28055
28056 requires-clause:
28057 `requires` constraint-logical-or-expression.
28058 [ConceptsTS]
28059 `requires constraint-expression.
28060
28061 LAMBDA_P is true when the requires-clause is parsed before the
28062 parameter-list of a lambda-declarator. */
28063
28064 static tree
28065 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
28066 {
28067 cp_token *tok = cp_lexer_peek_token (parser->lexer);
28068 if (tok->keyword != RID_REQUIRES)
28069 {
28070 if (!flag_concepts && tok->type == CPP_NAME
28071 && tok->u.value == ridpointers[RID_REQUIRES])
28072 {
28073 error_at (cp_lexer_peek_token (parser->lexer)->location,
28074 "%<requires%> only available with "
28075 "%<-std=c++20%> or %<-fconcepts%>");
28076 /* Parse and discard the requires-clause. */
28077 cp_lexer_consume_token (parser->lexer);
28078 cp_parser_constraint_expression (parser);
28079 }
28080 return NULL_TREE;
28081 }
28082
28083 cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
28084 if (tok2->type == CPP_OPEN_BRACE)
28085 {
28086 /* An opening brace following the start of a requires-clause is
28087 ill-formed; the user likely forgot the second `requires' that
28088 would start a requires-expression. */
28089 gcc_rich_location richloc (tok2->location);
28090 richloc.add_fixit_insert_after (tok->location, " requires");
28091 error_at (&richloc, "missing additional %<requires%> to start "
28092 "a requires-expression");
28093 /* Don't consume the `requires', so that it's reused as the start of a
28094 requires-expression. */
28095 }
28096 else
28097 cp_lexer_consume_token (parser->lexer);
28098
28099 if (!flag_concepts_ts)
28100 return cp_parser_requires_clause_expression (parser, lambda_p);
28101 else
28102 return cp_parser_constraint_expression (parser);
28103 }
28104
28105 /*---------------------------------------------------------------------------
28106 Requires expressions
28107 ---------------------------------------------------------------------------*/
28108
28109 /* Parse a requires expression
28110
28111 requirement-expression:
28112 'requires' requirement-parameter-list [opt] requirement-body */
28113
28114 static tree
28115 cp_parser_requires_expression (cp_parser *parser)
28116 {
28117 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
28118 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
28119
28120 /* Avoid committing to outer tentative parse. */
28121 tentative_firewall firewall (parser);
28122
28123 /* This is definitely a requires-expression. */
28124 cp_parser_commit_to_tentative_parse (parser);
28125
28126 tree parms, reqs;
28127 {
28128 /* Local parameters are delared as variables within the scope
28129 of the expression. They are not visible past the end of
28130 the expression. Expressions within the requires-expression
28131 are unevaluated. */
28132 struct scope_sentinel
28133 {
28134 scope_sentinel ()
28135 {
28136 ++cp_unevaluated_operand;
28137 begin_scope (sk_block, NULL_TREE);
28138 }
28139
28140 ~scope_sentinel ()
28141 {
28142 pop_bindings_and_leave_scope ();
28143 --cp_unevaluated_operand;
28144 }
28145 } s;
28146
28147 /* Parse the optional parameter list. */
28148 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28149 {
28150 parms = cp_parser_requirement_parameter_list (parser);
28151 if (parms == error_mark_node)
28152 return error_mark_node;
28153 }
28154 else
28155 parms = NULL_TREE;
28156
28157 /* Parse the requirement body. */
28158 temp_override<int> ovr (processing_template_decl);
28159 if (!processing_template_decl)
28160 /* As in cp_parser_requires_clause_expression. */
28161 processing_template_decl = 1;
28162 reqs = cp_parser_requirement_body (parser);
28163 if (reqs == error_mark_node)
28164 return error_mark_node;
28165 }
28166
28167 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
28168 the parm chain. */
28169 grokparms (parms, &parms);
28170 loc = make_location (loc, loc, parser->lexer);
28171 tree expr = finish_requires_expr (loc, parms, reqs);
28172 if (!processing_template_decl)
28173 {
28174 /* Perform semantic processing now to diagnose any invalid types and
28175 expressions. */
28176 int saved_errorcount = errorcount;
28177 tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
28178 if (errorcount > saved_errorcount)
28179 return error_mark_node;
28180 }
28181 return expr;
28182 }
28183
28184 /* Parse a parameterized requirement.
28185
28186 requirement-parameter-list:
28187 '(' parameter-declaration-clause ')' */
28188
28189 static tree
28190 cp_parser_requirement_parameter_list (cp_parser *parser)
28191 {
28192 matching_parens parens;
28193 if (!parens.require_open (parser))
28194 return error_mark_node;
28195
28196 tree parms = (cp_parser_parameter_declaration_clause
28197 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
28198
28199 if (!parens.require_close (parser))
28200 return error_mark_node;
28201
28202 return parms;
28203 }
28204
28205 /* Parse the body of a requirement.
28206
28207 requirement-body:
28208 '{' requirement-list '}' */
28209 static tree
28210 cp_parser_requirement_body (cp_parser *parser)
28211 {
28212 matching_braces braces;
28213 if (!braces.require_open (parser))
28214 return error_mark_node;
28215
28216 tree reqs = cp_parser_requirement_seq (parser);
28217
28218 if (!braces.require_close (parser))
28219 return error_mark_node;
28220
28221 return reqs;
28222 }
28223
28224 /* Parse a sequence of requirements.
28225
28226 requirement-seq:
28227 requirement
28228 requirement-seq requirement */
28229
28230 static tree
28231 cp_parser_requirement_seq (cp_parser *parser)
28232 {
28233 tree result = NULL_TREE;
28234 do
28235 {
28236 tree req = cp_parser_requirement (parser);
28237 if (req != error_mark_node)
28238 result = tree_cons (NULL_TREE, req, result);
28239 } while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE));
28240
28241 /* If there are no valid requirements, this is not a valid expression. */
28242 if (!result)
28243 return error_mark_node;
28244
28245 /* Reverse the order of requirements so they are analyzed in order. */
28246 return nreverse (result);
28247 }
28248
28249 /* Parse a syntactic requirement or type requirement.
28250
28251 requirement:
28252 simple-requirement
28253 compound-requirement
28254 type-requirement
28255 nested-requirement */
28256
28257 static tree
28258 cp_parser_requirement (cp_parser *parser)
28259 {
28260 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28261 return cp_parser_compound_requirement (parser);
28262 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
28263 return cp_parser_type_requirement (parser);
28264 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
28265 return cp_parser_nested_requirement (parser);
28266 else
28267 return cp_parser_simple_requirement (parser);
28268 }
28269
28270 /* Parse a simple requirement.
28271
28272 simple-requirement:
28273 expression ';' */
28274
28275 static tree
28276 cp_parser_simple_requirement (cp_parser *parser)
28277 {
28278 location_t start = cp_lexer_peek_token (parser->lexer)->location;
28279 cp_expr expr = cp_parser_expression (parser, NULL, false, false);
28280 if (expr == error_mark_node)
28281 cp_parser_skip_to_end_of_statement (parser);
28282
28283 cp_parser_consume_semicolon_at_end_of_statement (parser);
28284
28285 if (!expr || expr == error_mark_node)
28286 return error_mark_node;
28287
28288 /* Sometimes we don't get locations, so use the cached token location
28289 as a reasonable approximation. */
28290 if (expr.get_location() == UNKNOWN_LOCATION)
28291 expr.set_location (start);
28292
28293 return finish_simple_requirement (expr.get_location (), expr);
28294 }
28295
28296 /* Parse a type requirement
28297
28298 type-requirement
28299 nested-name-specifier [opt] required-type-name ';'
28300
28301 required-type-name:
28302 type-name
28303 'template' [opt] simple-template-id */
28304
28305 static tree
28306 cp_parser_type_requirement (cp_parser *parser)
28307 {
28308 cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
28309 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28310
28311 // Save the scope before parsing name specifiers.
28312 tree saved_scope = parser->scope;
28313 tree saved_object_scope = parser->object_scope;
28314 tree saved_qualifying_scope = parser->qualifying_scope;
28315 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
28316 cp_parser_nested_name_specifier_opt (parser,
28317 /*typename_keyword_p=*/true,
28318 /*check_dependency_p=*/false,
28319 /*type_p=*/true,
28320 /*is_declaration=*/false);
28321
28322 tree type;
28323 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28324 {
28325 cp_lexer_consume_token (parser->lexer);
28326 type = cp_parser_template_id (parser,
28327 /*template_keyword_p=*/true,
28328 /*check_dependency=*/false,
28329 /*tag_type=*/none_type,
28330 /*is_declaration=*/false);
28331 type = make_typename_type (parser->scope, type, typename_type,
28332 /*complain=*/tf_error);
28333 }
28334 else
28335 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
28336
28337 if (TREE_CODE (type) == TYPE_DECL)
28338 type = TREE_TYPE (type);
28339
28340 parser->scope = saved_scope;
28341 parser->object_scope = saved_object_scope;
28342 parser->qualifying_scope = saved_qualifying_scope;
28343
28344 if (type == error_mark_node)
28345 cp_parser_skip_to_end_of_statement (parser);
28346
28347 cp_parser_consume_semicolon_at_end_of_statement (parser);
28348
28349 if (type == error_mark_node)
28350 return error_mark_node;
28351
28352 loc = make_location (loc, start_tok->location, parser->lexer);
28353 return finish_type_requirement (loc, type);
28354 }
28355
28356 /* Parse a compound requirement
28357
28358 compound-requirement:
28359 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
28360
28361 static tree
28362 cp_parser_compound_requirement (cp_parser *parser)
28363 {
28364 /* Parse an expression enclosed in '{ }'s. */
28365 matching_braces braces;
28366 if (!braces.require_open (parser))
28367 return error_mark_node;
28368
28369 cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
28370
28371 tree expr = cp_parser_expression (parser, NULL, false, false);
28372 if (expr == error_mark_node)
28373 cp_parser_skip_to_closing_brace (parser);
28374
28375 if (!braces.require_close (parser))
28376 {
28377 cp_parser_skip_to_end_of_statement (parser);
28378 cp_parser_consume_semicolon_at_end_of_statement (parser);
28379 return error_mark_node;
28380 }
28381
28382 /* If the expression was invalid, skip the remainder of the requirement. */
28383 if (!expr || expr == error_mark_node)
28384 {
28385 cp_parser_skip_to_end_of_statement (parser);
28386 cp_parser_consume_semicolon_at_end_of_statement (parser);
28387 return error_mark_node;
28388 }
28389
28390 /* Parse the optional noexcept. */
28391 bool noexcept_p = false;
28392 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
28393 {
28394 cp_lexer_consume_token (parser->lexer);
28395 noexcept_p = true;
28396 }
28397
28398 /* Parse the optional trailing return type. */
28399 tree type = NULL_TREE;
28400 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
28401 {
28402 cp_lexer_consume_token (parser->lexer);
28403 cp_token *tok = cp_lexer_peek_token (parser->lexer);
28404
28405 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
28406 parser->in_result_type_constraint_p = true;
28407 /* C++20 allows either a type-id or a type-constraint. Parsing
28408 a type-id will subsume the parsing for a type-constraint but
28409 allow for more syntactic forms (e.g., const C<T>*). */
28410 type = cp_parser_trailing_type_id (parser);
28411 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
28412 if (type == error_mark_node)
28413 return error_mark_node;
28414
28415 location_t type_loc = make_location (tok->location, tok->location,
28416 parser->lexer);
28417
28418 /* Check that we haven't written something like 'const C<T>*'. */
28419 if (type_uses_auto (type))
28420 {
28421 if (!is_auto (type))
28422 {
28423 error_at (type_loc,
28424 "result type is not a plain type-constraint");
28425 cp_parser_consume_semicolon_at_end_of_statement (parser);
28426 return error_mark_node;
28427 }
28428 }
28429 else if (!flag_concepts_ts)
28430 /* P1452R2 removed the trailing-return-type option. */
28431 error_at (type_loc,
28432 "return-type-requirement is not a type-constraint");
28433 }
28434
28435 location_t loc = make_location (expr_token->location,
28436 braces.open_location (),
28437 parser->lexer);
28438
28439 cp_parser_consume_semicolon_at_end_of_statement (parser);
28440
28441 if (expr == error_mark_node || type == error_mark_node)
28442 return error_mark_node;
28443
28444 return finish_compound_requirement (loc, expr, type, noexcept_p);
28445 }
28446
28447 /* Parse a nested requirement. This is the same as a requires clause.
28448
28449 nested-requirement:
28450 requires-clause */
28451
28452 static tree
28453 cp_parser_nested_requirement (cp_parser *parser)
28454 {
28455 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
28456 cp_token *tok = cp_lexer_consume_token (parser->lexer);
28457 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28458 tree req = cp_parser_constraint_expression (parser);
28459 if (req == error_mark_node)
28460 cp_parser_skip_to_end_of_statement (parser);
28461 loc = make_location (loc, tok->location, parser->lexer);
28462 cp_parser_consume_semicolon_at_end_of_statement (parser);
28463 if (req == error_mark_node)
28464 return error_mark_node;
28465 return finish_nested_requirement (loc, req);
28466 }
28467
28468 /* Support Functions */
28469
28470 /* Return the appropriate prefer_type argument for lookup_name based on
28471 tag_type. */
28472
28473 static inline LOOK_want
28474 prefer_type_arg (tag_types tag_type)
28475 {
28476 switch (tag_type)
28477 {
28478 case none_type: return LOOK_want::NORMAL; // No preference.
28479 case scope_type: return LOOK_want::TYPE_NAMESPACE; // Type or namespace.
28480 default: return LOOK_want::TYPE; // Type only.
28481 }
28482 }
28483
28484 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
28485 NAME should have one of the representations used for an
28486 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
28487 is returned. If PARSER->SCOPE is a dependent type, then a
28488 SCOPE_REF is returned.
28489
28490 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
28491 returned; the name was already resolved when the TEMPLATE_ID_EXPR
28492 was formed. Abstractly, such entities should not be passed to this
28493 function, because they do not need to be looked up, but it is
28494 simpler to check for this special case here, rather than at the
28495 call-sites.
28496
28497 In cases not explicitly covered above, this function returns a
28498 DECL, OVERLOAD, or baselink representing the result of the lookup.
28499 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
28500 is returned.
28501
28502 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
28503 (e.g., "struct") that was used. In that case bindings that do not
28504 refer to types are ignored.
28505
28506 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
28507 ignored.
28508
28509 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
28510 are ignored.
28511
28512 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
28513 types.
28514
28515 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
28516 TREE_LIST of candidates if name-lookup results in an ambiguity, and
28517 NULL_TREE otherwise. */
28518
28519 static cp_expr
28520 cp_parser_lookup_name (cp_parser *parser, tree name,
28521 enum tag_types tag_type,
28522 bool is_template,
28523 bool is_namespace,
28524 bool check_dependency,
28525 tree *ambiguous_decls,
28526 location_t name_location)
28527 {
28528 tree decl;
28529 tree object_type = parser->context->object_type;
28530
28531 /* Assume that the lookup will be unambiguous. */
28532 if (ambiguous_decls)
28533 *ambiguous_decls = NULL_TREE;
28534
28535 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
28536 no longer valid. Note that if we are parsing tentatively, and
28537 the parse fails, OBJECT_TYPE will be automatically restored. */
28538 parser->context->object_type = NULL_TREE;
28539
28540 if (name == error_mark_node)
28541 return error_mark_node;
28542
28543 /* A template-id has already been resolved; there is no lookup to
28544 do. */
28545 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
28546 return name;
28547 if (BASELINK_P (name))
28548 {
28549 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
28550 == TEMPLATE_ID_EXPR);
28551 return name;
28552 }
28553
28554 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
28555 it should already have been checked to make sure that the name
28556 used matches the type being destroyed. */
28557 if (TREE_CODE (name) == BIT_NOT_EXPR)
28558 {
28559 tree type;
28560
28561 /* Figure out to which type this destructor applies. */
28562 if (parser->scope)
28563 type = parser->scope;
28564 else if (object_type)
28565 type = object_type;
28566 else
28567 type = current_class_type;
28568 /* If that's not a class type, there is no destructor. */
28569 if (!type || !CLASS_TYPE_P (type))
28570 return error_mark_node;
28571
28572 /* In a non-static member function, check implicit this->. */
28573 if (current_class_ref)
28574 return lookup_destructor (current_class_ref, parser->scope, name,
28575 tf_warning_or_error);
28576
28577 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
28578 lazily_declare_fn (sfk_destructor, type);
28579
28580 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
28581 return dtor;
28582
28583 return error_mark_node;
28584 }
28585
28586 /* By this point, the NAME should be an ordinary identifier. If
28587 the id-expression was a qualified name, the qualifying scope is
28588 stored in PARSER->SCOPE at this point. */
28589 gcc_assert (identifier_p (name));
28590
28591 /* Perform the lookup. */
28592 if (parser->scope)
28593 {
28594 bool dependent_p;
28595
28596 if (parser->scope == error_mark_node)
28597 return error_mark_node;
28598
28599 /* If the SCOPE is dependent, the lookup must be deferred until
28600 the template is instantiated -- unless we are explicitly
28601 looking up names in uninstantiated templates. Even then, we
28602 cannot look up the name if the scope is not a class type; it
28603 might, for example, be a template type parameter. */
28604 dependent_p = (TYPE_P (parser->scope)
28605 && dependent_scope_p (parser->scope));
28606 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
28607 && dependent_p)
28608 /* Defer lookup. */
28609 decl = error_mark_node;
28610 else
28611 {
28612 tree pushed_scope = NULL_TREE;
28613
28614 /* If PARSER->SCOPE is a dependent type, then it must be a
28615 class type, and we must not be checking dependencies;
28616 otherwise, we would have processed this lookup above. So
28617 that PARSER->SCOPE is not considered a dependent base by
28618 lookup_member, we must enter the scope here. */
28619 if (dependent_p)
28620 pushed_scope = push_scope (parser->scope);
28621
28622 /* If the PARSER->SCOPE is a template specialization, it
28623 may be instantiated during name lookup. In that case,
28624 errors may be issued. Even if we rollback the current
28625 tentative parse, those errors are valid. */
28626 decl = lookup_qualified_name (parser->scope, name,
28627 prefer_type_arg (tag_type),
28628 /*complain=*/true);
28629
28630 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
28631 lookup result and the nested-name-specifier nominates a class C:
28632 * if the name specified after the nested-name-specifier, when
28633 looked up in C, is the injected-class-name of C (Clause 9), or
28634 * if the name specified after the nested-name-specifier is the
28635 same as the identifier or the simple-template-id's template-
28636 name in the last component of the nested-name-specifier,
28637 the name is instead considered to name the constructor of
28638 class C. [ Note: for example, the constructor is not an
28639 acceptable lookup result in an elaborated-type-specifier so
28640 the constructor would not be used in place of the
28641 injected-class-name. --end note ] Such a constructor name
28642 shall be used only in the declarator-id of a declaration that
28643 names a constructor or in a using-declaration. */
28644 if (tag_type == none_type
28645 && DECL_SELF_REFERENCE_P (decl)
28646 && same_type_p (DECL_CONTEXT (decl), parser->scope))
28647 decl = lookup_qualified_name (parser->scope, ctor_identifier,
28648 prefer_type_arg (tag_type),
28649 /*complain=*/true);
28650
28651 if (pushed_scope)
28652 pop_scope (pushed_scope);
28653 }
28654
28655 /* If the scope is a dependent type and either we deferred lookup or
28656 we did lookup but didn't find the name, rememeber the name. */
28657 if (decl == error_mark_node && TYPE_P (parser->scope)
28658 && dependent_type_p (parser->scope))
28659 {
28660 if (tag_type)
28661 {
28662 tree type;
28663
28664 /* The resolution to Core Issue 180 says that `struct
28665 A::B' should be considered a type-name, even if `A'
28666 is dependent. */
28667 type = make_typename_type (parser->scope, name, tag_type,
28668 /*complain=*/tf_error);
28669 if (type != error_mark_node)
28670 decl = TYPE_NAME (type);
28671 }
28672 else if (is_template
28673 && (cp_parser_next_token_ends_template_argument_p (parser)
28674 || cp_lexer_next_token_is (parser->lexer,
28675 CPP_CLOSE_PAREN)))
28676 decl = make_unbound_class_template (parser->scope,
28677 name, NULL_TREE,
28678 /*complain=*/tf_error);
28679 else
28680 decl = build_qualified_name (/*type=*/NULL_TREE,
28681 parser->scope, name,
28682 is_template);
28683 }
28684 parser->qualifying_scope = parser->scope;
28685 parser->object_scope = NULL_TREE;
28686 }
28687 else if (object_type)
28688 {
28689 /* Look up the name in the scope of the OBJECT_TYPE, unless the
28690 OBJECT_TYPE is not a class. */
28691 if (CLASS_TYPE_P (object_type))
28692 /* If the OBJECT_TYPE is a template specialization, it may
28693 be instantiated during name lookup. In that case, errors
28694 may be issued. Even if we rollback the current tentative
28695 parse, those errors are valid. */
28696 decl = lookup_member (object_type,
28697 name,
28698 /*protect=*/0,
28699 /*prefer_type=*/tag_type != none_type,
28700 tf_warning_or_error);
28701 else
28702 decl = NULL_TREE;
28703
28704 if (!decl)
28705 /* Look it up in the enclosing context. DR 141: When looking for a
28706 template-name after -> or ., only consider class templates. */
28707 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
28708 /* DR 141: When looking in the
28709 current enclosing context for a
28710 template-name after -> or ., only
28711 consider class templates. */
28712 : is_template ? LOOK_want::TYPE
28713 : prefer_type_arg (tag_type));
28714 parser->object_scope = object_type;
28715 parser->qualifying_scope = NULL_TREE;
28716 }
28717 else
28718 {
28719 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
28720 : prefer_type_arg (tag_type));
28721 parser->qualifying_scope = NULL_TREE;
28722 parser->object_scope = NULL_TREE;
28723 }
28724
28725 /* If the lookup failed, let our caller know. */
28726 if (!decl || decl == error_mark_node)
28727 return error_mark_node;
28728
28729 /* Pull out the template from an injected-class-name (or multiple). */
28730 if (is_template)
28731 decl = maybe_get_template_decl_from_type_decl (decl);
28732
28733 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
28734 if (TREE_CODE (decl) == TREE_LIST)
28735 {
28736 if (ambiguous_decls)
28737 *ambiguous_decls = decl;
28738 /* The error message we have to print is too complicated for
28739 cp_parser_error, so we incorporate its actions directly. */
28740 if (!cp_parser_simulate_error (parser))
28741 {
28742 error_at (name_location, "reference to %qD is ambiguous",
28743 name);
28744 print_candidates (decl);
28745 }
28746 return error_mark_node;
28747 }
28748
28749 gcc_assert (DECL_P (decl)
28750 || TREE_CODE (decl) == OVERLOAD
28751 || TREE_CODE (decl) == SCOPE_REF
28752 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
28753 || BASELINK_P (decl));
28754
28755 /* If we have resolved the name of a member declaration, check to
28756 see if the declaration is accessible. When the name resolves to
28757 set of overloaded functions, accessibility is checked when
28758 overload resolution is done.
28759
28760 During an explicit instantiation, access is not checked at all,
28761 as per [temp.explicit]. */
28762 if (DECL_P (decl))
28763 check_accessibility_of_qualified_id (decl, object_type, parser->scope,
28764 tf_warning_or_error);
28765
28766 maybe_record_typedef_use (decl);
28767
28768 return cp_expr (decl, name_location);
28769 }
28770
28771 /* Like cp_parser_lookup_name, but for use in the typical case where
28772 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
28773 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
28774
28775 static tree
28776 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
28777 {
28778 return cp_parser_lookup_name (parser, name,
28779 none_type,
28780 /*is_template=*/false,
28781 /*is_namespace=*/false,
28782 /*check_dependency=*/true,
28783 /*ambiguous_decls=*/NULL,
28784 location);
28785 }
28786
28787 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
28788 the current context, return the TYPE_DECL. If TAG_NAME_P is
28789 true, the DECL indicates the class being defined in a class-head,
28790 or declared in an elaborated-type-specifier.
28791
28792 Otherwise, return DECL. */
28793
28794 static tree
28795 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
28796 {
28797 /* If the TEMPLATE_DECL is being declared as part of a class-head,
28798 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
28799
28800 struct A {
28801 template <typename T> struct B;
28802 };
28803
28804 template <typename T> struct A::B {};
28805
28806 Similarly, in an elaborated-type-specifier:
28807
28808 namespace N { struct X{}; }
28809
28810 struct A {
28811 template <typename T> friend struct N::X;
28812 };
28813
28814 However, if the DECL refers to a class type, and we are in
28815 the scope of the class, then the name lookup automatically
28816 finds the TYPE_DECL created by build_self_reference rather
28817 than a TEMPLATE_DECL. For example, in:
28818
28819 template <class T> struct S {
28820 S s;
28821 };
28822
28823 there is no need to handle such case. */
28824
28825 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
28826 return DECL_TEMPLATE_RESULT (decl);
28827
28828 return decl;
28829 }
28830
28831 /* If too many, or too few, template-parameter lists apply to the
28832 declarator, issue an error message. Returns TRUE if all went well,
28833 and FALSE otherwise. */
28834
28835 static bool
28836 cp_parser_check_declarator_template_parameters (cp_parser* parser,
28837 cp_declarator *declarator,
28838 location_t declarator_location)
28839 {
28840 switch (declarator->kind)
28841 {
28842 case cdk_id:
28843 {
28844 unsigned num_templates = 0;
28845 tree scope = declarator->u.id.qualifying_scope;
28846 bool template_id_p = false;
28847
28848 if (scope)
28849 num_templates = num_template_headers_for_class (scope);
28850 else if (TREE_CODE (declarator->u.id.unqualified_name)
28851 == TEMPLATE_ID_EXPR)
28852 {
28853 /* If the DECLARATOR has the form `X<y>' then it uses one
28854 additional level of template parameters. */
28855 ++num_templates;
28856 template_id_p = true;
28857 }
28858
28859 return cp_parser_check_template_parameters
28860 (parser, num_templates, template_id_p, declarator_location,
28861 declarator);
28862 }
28863
28864 case cdk_function:
28865 case cdk_array:
28866 case cdk_pointer:
28867 case cdk_reference:
28868 case cdk_ptrmem:
28869 return (cp_parser_check_declarator_template_parameters
28870 (parser, declarator->declarator, declarator_location));
28871
28872 case cdk_decomp:
28873 case cdk_error:
28874 return true;
28875
28876 default:
28877 gcc_unreachable ();
28878 }
28879 return false;
28880 }
28881
28882 /* NUM_TEMPLATES were used in the current declaration. If that is
28883 invalid, return FALSE and issue an error messages. Otherwise,
28884 return TRUE. If DECLARATOR is non-NULL, then we are checking a
28885 declarator and we can print more accurate diagnostics. */
28886
28887 static bool
28888 cp_parser_check_template_parameters (cp_parser* parser,
28889 unsigned num_templates,
28890 bool template_id_p,
28891 location_t location,
28892 cp_declarator *declarator)
28893 {
28894 /* If there are the same number of template classes and parameter
28895 lists, that's OK. */
28896 if (parser->num_template_parameter_lists == num_templates)
28897 return true;
28898 /* If there are more, but only one more, and the name ends in an identifier,
28899 then we are declaring a primary template. That's OK too. */
28900 if (!template_id_p
28901 && parser->num_template_parameter_lists == num_templates + 1)
28902 return true;
28903
28904 if (cp_parser_simulate_error (parser))
28905 return false;
28906
28907 /* If there are more template classes than parameter lists, we have
28908 something like:
28909
28910 template <class T> void S<T>::R<T>::f (); */
28911 if (parser->num_template_parameter_lists < num_templates)
28912 {
28913 if (declarator && !current_function_decl)
28914 error_at (location, "specializing member %<%T::%E%> "
28915 "requires %<template<>%> syntax",
28916 declarator->u.id.qualifying_scope,
28917 declarator->u.id.unqualified_name);
28918 else if (declarator)
28919 error_at (location, "invalid declaration of %<%T::%E%>",
28920 declarator->u.id.qualifying_scope,
28921 declarator->u.id.unqualified_name);
28922 else
28923 error_at (location, "too few template-parameter-lists");
28924 return false;
28925 }
28926 /* Otherwise, there are too many template parameter lists. We have
28927 something like:
28928
28929 template <class T> template <class U> void S::f(); */
28930 error_at (location, "too many template-parameter-lists");
28931 return false;
28932 }
28933
28934 /* Parse an optional `::' token indicating that the following name is
28935 from the global namespace. If so, PARSER->SCOPE is set to the
28936 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
28937 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
28938 Returns the new value of PARSER->SCOPE, if the `::' token is
28939 present, and NULL_TREE otherwise. */
28940
28941 static tree
28942 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
28943 {
28944 cp_token *token;
28945
28946 /* Peek at the next token. */
28947 token = cp_lexer_peek_token (parser->lexer);
28948 /* If we're looking at a `::' token then we're starting from the
28949 global namespace, not our current location. */
28950 if (token->type == CPP_SCOPE)
28951 {
28952 /* Consume the `::' token. */
28953 cp_lexer_consume_token (parser->lexer);
28954 /* Set the SCOPE so that we know where to start the lookup. */
28955 parser->scope = global_namespace;
28956 parser->qualifying_scope = global_namespace;
28957 parser->object_scope = NULL_TREE;
28958
28959 return parser->scope;
28960 }
28961 else if (!current_scope_valid_p)
28962 {
28963 parser->scope = NULL_TREE;
28964 parser->qualifying_scope = NULL_TREE;
28965 parser->object_scope = NULL_TREE;
28966 }
28967
28968 return NULL_TREE;
28969 }
28970
28971 /* Returns TRUE if the upcoming token sequence is the start of a
28972 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
28973 declarator is preceded by the `friend' specifier. The parser flags FLAGS
28974 is used to control type-specifier parsing. */
28975
28976 static bool
28977 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
28978 bool friend_p)
28979 {
28980 bool constructor_p;
28981 bool outside_class_specifier_p;
28982 tree nested_name_specifier;
28983 cp_token *next_token;
28984
28985 /* The common case is that this is not a constructor declarator, so
28986 try to avoid doing lots of work if at all possible. It's not
28987 valid declare a constructor at function scope. */
28988 if (parser->in_function_body)
28989 return false;
28990 /* And only certain tokens can begin a constructor declarator. */
28991 next_token = cp_lexer_peek_token (parser->lexer);
28992 if (next_token->type != CPP_NAME
28993 && next_token->type != CPP_SCOPE
28994 && next_token->type != CPP_NESTED_NAME_SPECIFIER
28995 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
28996 declarator-id of a constructor or destructor. */
28997 && (next_token->type != CPP_TEMPLATE_ID || cxx_dialect >= cxx20))
28998 return false;
28999
29000 /* Parse tentatively; we are going to roll back all of the tokens
29001 consumed here. */
29002 cp_parser_parse_tentatively (parser);
29003 /* Assume that we are looking at a constructor declarator. */
29004 constructor_p = true;
29005
29006 /* Look for the optional `::' operator. */
29007 cp_parser_global_scope_opt (parser,
29008 /*current_scope_valid_p=*/false);
29009 /* Look for the nested-name-specifier. */
29010 nested_name_specifier
29011 = (cp_parser_nested_name_specifier_opt (parser,
29012 /*typename_keyword_p=*/false,
29013 /*check_dependency_p=*/false,
29014 /*type_p=*/false,
29015 /*is_declaration=*/false));
29016
29017 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
29018 if (nested_name_specifier
29019 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
29020 {
29021 tree s = resolve_typename_type (nested_name_specifier,
29022 /*only_current_p=*/false);
29023 if (TREE_CODE (s) != TYPENAME_TYPE)
29024 nested_name_specifier = s;
29025 }
29026
29027 outside_class_specifier_p = (!at_class_scope_p ()
29028 || !TYPE_BEING_DEFINED (current_class_type)
29029 || friend_p);
29030
29031 /* Outside of a class-specifier, there must be a
29032 nested-name-specifier. Except in C++17 mode, where we
29033 might be declaring a guiding declaration. */
29034 if (!nested_name_specifier && outside_class_specifier_p
29035 && cxx_dialect < cxx17)
29036 constructor_p = false;
29037 else if (nested_name_specifier == error_mark_node)
29038 constructor_p = false;
29039
29040 /* If we have a class scope, this is easy; DR 147 says that S::S always
29041 names the constructor, and no other qualified name could. */
29042 if (constructor_p && nested_name_specifier
29043 && CLASS_TYPE_P (nested_name_specifier))
29044 {
29045 tree id = cp_parser_unqualified_id (parser,
29046 /*template_keyword_p=*/false,
29047 /*check_dependency_p=*/false,
29048 /*declarator_p=*/true,
29049 /*optional_p=*/false);
29050 if (is_overloaded_fn (id))
29051 id = DECL_NAME (get_first_fn (id));
29052 if (!constructor_name_p (id, nested_name_specifier))
29053 constructor_p = false;
29054 }
29055 /* If we still think that this might be a constructor-declarator,
29056 look for a class-name. */
29057 else if (constructor_p)
29058 {
29059 /* If we have:
29060
29061 template <typename T> struct S {
29062 S();
29063 };
29064
29065 we must recognize that the nested `S' names a class. */
29066 if (cxx_dialect >= cxx17)
29067 cp_parser_parse_tentatively (parser);
29068
29069 tree type_decl;
29070 type_decl = cp_parser_class_name (parser,
29071 /*typename_keyword_p=*/false,
29072 /*template_keyword_p=*/false,
29073 none_type,
29074 /*check_dependency_p=*/false,
29075 /*class_head_p=*/false,
29076 /*is_declaration=*/false);
29077
29078 if (cxx_dialect >= cxx17
29079 && !cp_parser_parse_definitely (parser))
29080 {
29081 type_decl = NULL_TREE;
29082 tree tmpl = cp_parser_template_name (parser,
29083 /*template_keyword*/false,
29084 /*check_dependency_p*/false,
29085 /*is_declaration*/false,
29086 none_type,
29087 /*is_identifier*/NULL);
29088 if (DECL_CLASS_TEMPLATE_P (tmpl)
29089 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
29090 /* It's a deduction guide, return true. */;
29091 else
29092 cp_parser_simulate_error (parser);
29093 }
29094
29095 /* If there was no class-name, then this is not a constructor.
29096 Otherwise, if we are in a class-specifier and we aren't
29097 handling a friend declaration, check that its type matches
29098 current_class_type (c++/38313). Note: error_mark_node
29099 is left alone for error recovery purposes. */
29100 constructor_p = (!cp_parser_error_occurred (parser)
29101 && (outside_class_specifier_p
29102 || type_decl == NULL_TREE
29103 || type_decl == error_mark_node
29104 || same_type_p (current_class_type,
29105 TREE_TYPE (type_decl))));
29106
29107 /* If we're still considering a constructor, we have to see a `(',
29108 to begin the parameter-declaration-clause, followed by either a
29109 `)', an `...', or a decl-specifier. We need to check for a
29110 type-specifier to avoid being fooled into thinking that:
29111
29112 S (f) (int);
29113
29114 is a constructor. (It is actually a function named `f' that
29115 takes one parameter (of type `int') and returns a value of type
29116 `S'. */
29117 if (constructor_p
29118 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29119 constructor_p = false;
29120
29121 if (constructor_p
29122 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
29123 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
29124 /* A parameter declaration begins with a decl-specifier,
29125 which is either the "attribute" keyword, a storage class
29126 specifier, or (usually) a type-specifier. */
29127 && (!cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
29128 /* GNU attributes can actually appear both at the start of
29129 a parameter and parenthesized declarator.
29130 S (__attribute__((unused)) int);
29131 is a constructor, but
29132 S (__attribute__((unused)) foo) (int);
29133 is a function declaration. */
29134 || (cp_parser_allow_gnu_extensions_p (parser)
29135 && cp_next_tokens_can_be_gnu_attribute_p (parser)))
29136 /* A parameter declaration can also begin with [[attribute]]. */
29137 && !cp_next_tokens_can_be_std_attribute_p (parser))
29138 {
29139 tree type;
29140 tree pushed_scope = NULL_TREE;
29141 unsigned saved_num_template_parameter_lists;
29142
29143 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
29144 {
29145 unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
29146 while (--n)
29147 cp_lexer_consume_token (parser->lexer);
29148 }
29149
29150 /* Names appearing in the type-specifier should be looked up
29151 in the scope of the class. */
29152 if (current_class_type)
29153 type = NULL_TREE;
29154 else if (type_decl)
29155 {
29156 type = TREE_TYPE (type_decl);
29157 if (TREE_CODE (type) == TYPENAME_TYPE)
29158 {
29159 type = resolve_typename_type (type,
29160 /*only_current_p=*/false);
29161 if (TREE_CODE (type) == TYPENAME_TYPE)
29162 {
29163 cp_parser_abort_tentative_parse (parser);
29164 return false;
29165 }
29166 }
29167 pushed_scope = push_scope (type);
29168 }
29169
29170 /* Inside the constructor parameter list, surrounding
29171 template-parameter-lists do not apply. */
29172 saved_num_template_parameter_lists
29173 = parser->num_template_parameter_lists;
29174 parser->num_template_parameter_lists = 0;
29175
29176 /* Look for the type-specifier. It's not optional, but its typename
29177 might be. Unless this is a friend declaration; we don't want to
29178 treat
29179
29180 friend S (T::fn)(int);
29181
29182 as a constructor, but with P0634, we might assume a type when
29183 looking for the type-specifier. It is actually a function named
29184 `T::fn' that takes one parameter (of type `int') and returns a
29185 value of type `S'. Constructors can be friends, but they must
29186 use a qualified name.
29187
29188 Parse with an empty set of declaration specifiers since we're
29189 trying to match a decl-specifier-seq of the first parameter.
29190 This must be non-null so that cp_parser_simple_type_specifier
29191 will recognize a constrained placeholder type such as:
29192 'C<int> auto' where C is a type concept. */
29193 cp_decl_specifier_seq ctor_specs;
29194 clear_decl_specs (&ctor_specs);
29195 cp_parser_type_specifier (parser,
29196 (friend_p ? CP_PARSER_FLAGS_NONE
29197 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
29198 /*decl_specs=*/&ctor_specs,
29199 /*is_declarator=*/true,
29200 /*declares_class_or_enum=*/NULL,
29201 /*is_cv_qualifier=*/NULL);
29202
29203 parser->num_template_parameter_lists
29204 = saved_num_template_parameter_lists;
29205
29206 /* Leave the scope of the class. */
29207 if (pushed_scope)
29208 pop_scope (pushed_scope);
29209
29210 constructor_p = !cp_parser_error_occurred (parser);
29211 }
29212 }
29213
29214 /* We did not really want to consume any tokens. */
29215 cp_parser_abort_tentative_parse (parser);
29216
29217 return constructor_p;
29218 }
29219
29220 /* Parse the definition of the function given by the DECL_SPECIFIERS,
29221 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
29222 they must be performed once we are in the scope of the function.
29223
29224 Returns the function defined. */
29225
29226 static tree
29227 cp_parser_function_definition_from_specifiers_and_declarator
29228 (cp_parser* parser,
29229 cp_decl_specifier_seq *decl_specifiers,
29230 tree attributes,
29231 const cp_declarator *declarator)
29232 {
29233 tree fn;
29234 bool success_p;
29235
29236 /* Begin the function-definition. */
29237 success_p = start_function (decl_specifiers, declarator, attributes);
29238
29239 /* The things we're about to see are not directly qualified by any
29240 template headers we've seen thus far. */
29241 reset_specialization ();
29242
29243 /* If there were names looked up in the decl-specifier-seq that we
29244 did not check, check them now. We must wait until we are in the
29245 scope of the function to perform the checks, since the function
29246 might be a friend. */
29247 perform_deferred_access_checks (tf_warning_or_error);
29248
29249 if (success_p)
29250 {
29251 cp_finalize_omp_declare_simd (parser, current_function_decl);
29252 parser->omp_declare_simd = NULL;
29253 cp_finalize_oacc_routine (parser, current_function_decl, true);
29254 parser->oacc_routine = NULL;
29255 }
29256
29257 if (!success_p)
29258 {
29259 /* Skip the entire function. */
29260 cp_parser_skip_to_end_of_block_or_statement (parser);
29261 fn = error_mark_node;
29262 }
29263 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
29264 {
29265 /* Seen already, skip it. An error message has already been output. */
29266 cp_parser_skip_to_end_of_block_or_statement (parser);
29267 fn = current_function_decl;
29268 current_function_decl = NULL_TREE;
29269 /* If this is a function from a class, pop the nested class. */
29270 if (current_class_name)
29271 pop_nested_class ();
29272 }
29273 else
29274 {
29275 timevar_id_t tv;
29276 if (DECL_DECLARED_INLINE_P (current_function_decl))
29277 tv = TV_PARSE_INLINE;
29278 else
29279 tv = TV_PARSE_FUNC;
29280 timevar_push (tv);
29281 fn = cp_parser_function_definition_after_declarator (parser,
29282 /*inline_p=*/false);
29283 timevar_pop (tv);
29284 }
29285
29286 return fn;
29287 }
29288
29289 /* Parse the part of a function-definition that follows the
29290 declarator. INLINE_P is TRUE iff this function is an inline
29291 function defined within a class-specifier.
29292
29293 Returns the function defined. */
29294
29295 static tree
29296 cp_parser_function_definition_after_declarator (cp_parser* parser,
29297 bool inline_p)
29298 {
29299 tree fn;
29300 bool saved_in_unbraced_linkage_specification_p;
29301 bool saved_in_function_body;
29302 unsigned saved_num_template_parameter_lists;
29303 cp_token *token;
29304 bool fully_implicit_function_template_p
29305 = parser->fully_implicit_function_template_p;
29306 parser->fully_implicit_function_template_p = false;
29307 tree implicit_template_parms
29308 = parser->implicit_template_parms;
29309 parser->implicit_template_parms = 0;
29310 cp_binding_level* implicit_template_scope
29311 = parser->implicit_template_scope;
29312 parser->implicit_template_scope = 0;
29313
29314 saved_in_function_body = parser->in_function_body;
29315 parser->in_function_body = true;
29316 /* If the next token is `return', then the code may be trying to
29317 make use of the "named return value" extension that G++ used to
29318 support. */
29319 token = cp_lexer_peek_token (parser->lexer);
29320 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
29321 {
29322 /* Consume the `return' keyword. */
29323 cp_lexer_consume_token (parser->lexer);
29324 /* Look for the identifier that indicates what value is to be
29325 returned. */
29326 cp_parser_identifier (parser);
29327 /* Issue an error message. */
29328 error_at (token->location,
29329 "named return values are no longer supported");
29330 /* Skip tokens until we reach the start of the function body. */
29331 while (true)
29332 {
29333 cp_token *token = cp_lexer_peek_token (parser->lexer);
29334 if (token->type == CPP_OPEN_BRACE
29335 || token->type == CPP_EOF
29336 || token->type == CPP_PRAGMA_EOL)
29337 break;
29338 cp_lexer_consume_token (parser->lexer);
29339 }
29340 }
29341 /* The `extern' in `extern "C" void f () { ... }' does not apply to
29342 anything declared inside `f'. */
29343 saved_in_unbraced_linkage_specification_p
29344 = parser->in_unbraced_linkage_specification_p;
29345 parser->in_unbraced_linkage_specification_p = false;
29346 /* Inside the function, surrounding template-parameter-lists do not
29347 apply. */
29348 saved_num_template_parameter_lists
29349 = parser->num_template_parameter_lists;
29350 parser->num_template_parameter_lists = 0;
29351
29352 /* If the next token is `try', `__transaction_atomic', or
29353 `__transaction_relaxed`, then we are looking at either function-try-block
29354 or function-transaction-block. Note that all of these include the
29355 function-body. */
29356 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
29357 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
29358 else if (cp_lexer_next_token_is_keyword (parser->lexer,
29359 RID_TRANSACTION_RELAXED))
29360 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
29361 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
29362 cp_parser_function_try_block (parser);
29363 else
29364 cp_parser_ctor_initializer_opt_and_function_body
29365 (parser, /*in_function_try_block=*/false);
29366
29367 /* Finish the function. */
29368 fn = finish_function (inline_p);
29369 /* Generate code for it, if necessary. */
29370 expand_or_defer_fn (fn);
29371 /* Restore the saved values. */
29372 parser->in_unbraced_linkage_specification_p
29373 = saved_in_unbraced_linkage_specification_p;
29374 parser->num_template_parameter_lists
29375 = saved_num_template_parameter_lists;
29376 parser->in_function_body = saved_in_function_body;
29377
29378 parser->fully_implicit_function_template_p
29379 = fully_implicit_function_template_p;
29380 parser->implicit_template_parms
29381 = implicit_template_parms;
29382 parser->implicit_template_scope
29383 = implicit_template_scope;
29384
29385 if (parser->fully_implicit_function_template_p)
29386 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
29387
29388 return fn;
29389 }
29390
29391 /* Parse a template-declaration body (following argument list). */
29392
29393 static void
29394 cp_parser_template_declaration_after_parameters (cp_parser* parser,
29395 tree parameter_list,
29396 bool member_p)
29397 {
29398 tree decl = NULL_TREE;
29399 bool friend_p = false;
29400
29401 /* We just processed one more parameter list. */
29402 ++parser->num_template_parameter_lists;
29403
29404 /* Get the deferred access checks from the parameter list. These
29405 will be checked once we know what is being declared, as for a
29406 member template the checks must be performed in the scope of the
29407 class containing the member. */
29408 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
29409
29410 /* Tentatively parse for a new template parameter list, which can either be
29411 the template keyword or a template introduction. */
29412 if (cp_parser_template_declaration_after_export (parser, member_p))
29413 /* OK */;
29414 else if (cxx_dialect >= cxx11
29415 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
29416 decl = cp_parser_alias_declaration (parser);
29417 else if (cxx_dialect >= cxx20 /* Implies flag_concept. */
29418 && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
29419 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_BOOL))
29420 /* Allow 'concept bool' to be handled as per the TS. */
29421 decl = cp_parser_concept_definition (parser);
29422 else
29423 {
29424 cp_token *token = cp_lexer_peek_token (parser->lexer);
29425 decl = cp_parser_single_declaration (parser,
29426 checks,
29427 member_p,
29428 /*explicit_specialization_p=*/false,
29429 &friend_p);
29430
29431 /* If this is a member template declaration, let the front
29432 end know. */
29433 if (member_p && !friend_p && decl)
29434 {
29435 if (TREE_CODE (decl) == TYPE_DECL)
29436 cp_parser_check_access_in_redeclaration (decl, token->location);
29437
29438 decl = finish_member_template_decl (decl);
29439 }
29440 else if (friend_p && decl
29441 && DECL_DECLARES_TYPE_P (decl))
29442 make_friend_class (current_class_type, TREE_TYPE (decl),
29443 /*complain=*/true);
29444 }
29445 /* We are done with the current parameter list. */
29446 --parser->num_template_parameter_lists;
29447
29448 pop_deferring_access_checks ();
29449
29450 /* Finish up. */
29451 finish_template_decl (parameter_list);
29452
29453 /* Check the template arguments for a literal operator template. */
29454 if (decl
29455 && DECL_DECLARES_FUNCTION_P (decl)
29456 && UDLIT_OPER_P (DECL_NAME (decl)))
29457 {
29458 bool ok = true;
29459 if (parameter_list == NULL_TREE)
29460 ok = false;
29461 else
29462 {
29463 int num_parms = TREE_VEC_LENGTH (parameter_list);
29464 if (num_parms == 1)
29465 {
29466 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
29467 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
29468 if (TREE_CODE (parm) != PARM_DECL)
29469 ok = false;
29470 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
29471 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
29472 /* OK, C++20 string literal operator template. We don't need
29473 to warn in lower dialects here because we will have already
29474 warned about the template parameter. */;
29475 else if (TREE_TYPE (parm) != char_type_node
29476 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
29477 ok = false;
29478 }
29479 else if (num_parms == 2 && cxx_dialect >= cxx14)
29480 {
29481 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
29482 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
29483 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
29484 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
29485 if (TREE_CODE (parm) != PARM_DECL
29486 || TREE_TYPE (parm) != TREE_TYPE (type)
29487 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
29488 ok = false;
29489 else
29490 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
29491 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
29492 "ISO C++ did not adopt string literal operator templa"
29493 "tes taking an argument pack of characters");
29494 }
29495 else
29496 ok = false;
29497 }
29498 if (!ok)
29499 {
29500 if (cxx_dialect > cxx17)
29501 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
29502 "template %qD has invalid parameter list; expected "
29503 "non-type template parameter pack %<<char...>%> or "
29504 "single non-type parameter of class type",
29505 decl);
29506 else
29507 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
29508 "template %qD has invalid parameter list; expected "
29509 "non-type template parameter pack %<<char...>%>",
29510 decl);
29511 }
29512 }
29513
29514 /* Register member declarations. */
29515 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
29516 finish_member_declaration (decl);
29517 /* If DECL is a function template, we must return to parse it later.
29518 (Even though there is no definition, there might be default
29519 arguments that need handling.) */
29520 if (member_p && decl
29521 && DECL_DECLARES_FUNCTION_P (decl))
29522 vec_safe_push (unparsed_funs_with_definitions, decl);
29523 }
29524
29525 /* Parse a template introduction header for a template-declaration. Returns
29526 false if tentative parse fails. */
29527
29528 static bool
29529 cp_parser_template_introduction (cp_parser* parser, bool member_p)
29530 {
29531 cp_parser_parse_tentatively (parser);
29532
29533 tree saved_scope = parser->scope;
29534 tree saved_object_scope = parser->object_scope;
29535 tree saved_qualifying_scope = parser->qualifying_scope;
29536
29537 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
29538
29539 /* Look for the optional `::' operator. */
29540 cp_parser_global_scope_opt (parser,
29541 /*current_scope_valid_p=*/false);
29542 /* Look for the nested-name-specifier. */
29543 cp_parser_nested_name_specifier_opt (parser,
29544 /*typename_keyword_p=*/false,
29545 /*check_dependency_p=*/true,
29546 /*type_p=*/false,
29547 /*is_declaration=*/false);
29548
29549 cp_token *token = cp_lexer_peek_token (parser->lexer);
29550 tree concept_name = cp_parser_identifier (parser);
29551
29552 /* Look up the concept for which we will be matching
29553 template parameters. */
29554 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
29555 token->location);
29556 parser->scope = saved_scope;
29557 parser->object_scope = saved_object_scope;
29558 parser->qualifying_scope = saved_qualifying_scope;
29559
29560 if (concept_name == error_mark_node
29561 || (seen_error () && !concept_definition_p (tmpl_decl)))
29562 cp_parser_simulate_error (parser);
29563
29564 /* Look for opening brace for introduction. */
29565 matching_braces braces;
29566 braces.require_open (parser);
29567 location_t open_loc = input_location;
29568
29569 if (!cp_parser_parse_definitely (parser))
29570 return false;
29571
29572 push_deferring_access_checks (dk_deferred);
29573
29574 /* Build vector of placeholder parameters and grab
29575 matching identifiers. */
29576 tree introduction_list = cp_parser_introduction_list (parser);
29577
29578 /* Look for closing brace for introduction. */
29579 if (!braces.require_close (parser))
29580 return true;
29581
29582 /* The introduction-list shall not be empty. */
29583 int nargs = TREE_VEC_LENGTH (introduction_list);
29584 if (nargs == 0)
29585 {
29586 /* In cp_parser_introduction_list we have already issued an error. */
29587 return true;
29588 }
29589
29590 if (tmpl_decl == error_mark_node)
29591 {
29592 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
29593 token->location);
29594 return true;
29595 }
29596
29597 /* Build and associate the constraint. */
29598 location_t introduction_loc = make_location (open_loc,
29599 start_token->location,
29600 parser->lexer);
29601 tree parms = finish_template_introduction (tmpl_decl,
29602 introduction_list,
29603 introduction_loc);
29604 if (parms && parms != error_mark_node)
29605 {
29606 if (!flag_concepts_ts)
29607 pedwarn (introduction_loc, 0, "template-introductions"
29608 " are not part of C++20 concepts [-fconcepts-ts]");
29609
29610 cp_parser_template_declaration_after_parameters (parser, parms,
29611 member_p);
29612 return true;
29613 }
29614
29615 if (parms == NULL_TREE)
29616 error_at (token->location, "no matching concept for template-introduction");
29617
29618 return true;
29619 }
29620
29621 /* Parse a normal template-declaration following the template keyword. */
29622
29623 static void
29624 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
29625 {
29626 tree parameter_list;
29627 bool need_lang_pop;
29628 location_t location = input_location;
29629
29630 /* Look for the `<' token. */
29631 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
29632 return;
29633 if (at_class_scope_p () && current_function_decl)
29634 {
29635 /* 14.5.2.2 [temp.mem]
29636
29637 A local class shall not have member templates. */
29638 error_at (location,
29639 "invalid declaration of member template in local class");
29640 cp_parser_skip_to_end_of_block_or_statement (parser);
29641 return;
29642 }
29643 /* [temp]
29644
29645 A template ... shall not have C linkage. */
29646 if (current_lang_name == lang_name_c)
29647 {
29648 error_at (location, "template with C linkage");
29649 maybe_show_extern_c_location ();
29650 /* Give it C++ linkage to avoid confusing other parts of the
29651 front end. */
29652 push_lang_context (lang_name_cplusplus);
29653 need_lang_pop = true;
29654 }
29655 else
29656 need_lang_pop = false;
29657
29658 /* We cannot perform access checks on the template parameter
29659 declarations until we know what is being declared, just as we
29660 cannot check the decl-specifier list. */
29661 push_deferring_access_checks (dk_deferred);
29662
29663 /* If the next token is `>', then we have an invalid
29664 specialization. Rather than complain about an invalid template
29665 parameter, issue an error message here. */
29666 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
29667 {
29668 cp_parser_error (parser, "invalid explicit specialization");
29669 begin_specialization ();
29670 parameter_list = NULL_TREE;
29671 }
29672 else
29673 {
29674 /* Parse the template parameters. */
29675 parameter_list = cp_parser_template_parameter_list (parser);
29676 }
29677
29678 /* Look for the `>'. */
29679 cp_parser_skip_to_end_of_template_parameter_list (parser);
29680
29681 /* Manage template requirements */
29682 if (flag_concepts)
29683 {
29684 tree reqs = get_shorthand_constraints (current_template_parms);
29685 if (tree treqs = cp_parser_requires_clause_opt (parser, false))
29686 reqs = combine_constraint_expressions (reqs, treqs);
29687 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
29688 }
29689
29690 cp_parser_template_declaration_after_parameters (parser, parameter_list,
29691 member_p);
29692
29693 /* For the erroneous case of a template with C linkage, we pushed an
29694 implicit C++ linkage scope; exit that scope now. */
29695 if (need_lang_pop)
29696 pop_lang_context ();
29697 }
29698
29699 /* Parse a template-declaration, assuming that the `export' (and
29700 `extern') keywords, if present, has already been scanned. MEMBER_P
29701 is as for cp_parser_template_declaration. */
29702
29703 static bool
29704 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
29705 {
29706 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29707 {
29708 cp_lexer_consume_token (parser->lexer);
29709 cp_parser_explicit_template_declaration (parser, member_p);
29710 return true;
29711 }
29712 else if (flag_concepts)
29713 return cp_parser_template_introduction (parser, member_p);
29714
29715 return false;
29716 }
29717
29718 /* Perform the deferred access checks from a template-parameter-list.
29719 CHECKS is a TREE_LIST of access checks, as returned by
29720 get_deferred_access_checks. */
29721
29722 static void
29723 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
29724 {
29725 ++processing_template_parmlist;
29726 perform_access_checks (checks, tf_warning_or_error);
29727 --processing_template_parmlist;
29728 }
29729
29730 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
29731 `function-definition' sequence that follows a template header.
29732 If MEMBER_P is true, this declaration appears in a class scope.
29733
29734 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
29735 *FRIEND_P is set to TRUE iff the declaration is a friend. */
29736
29737 static tree
29738 cp_parser_single_declaration (cp_parser* parser,
29739 vec<deferred_access_check, va_gc> *checks,
29740 bool member_p,
29741 bool explicit_specialization_p,
29742 bool* friend_p)
29743 {
29744 int declares_class_or_enum;
29745 tree decl = NULL_TREE;
29746 cp_decl_specifier_seq decl_specifiers;
29747 bool function_definition_p = false;
29748 cp_token *decl_spec_token_start;
29749
29750 /* This function is only used when processing a template
29751 declaration. */
29752 gcc_assert (innermost_scope_kind () == sk_template_parms
29753 || innermost_scope_kind () == sk_template_spec);
29754
29755 /* Defer access checks until we know what is being declared. */
29756 push_deferring_access_checks (dk_deferred);
29757
29758 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
29759 alternative. */
29760 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
29761 cp_parser_decl_specifier_seq (parser,
29762 (CP_PARSER_FLAGS_OPTIONAL
29763 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
29764 &decl_specifiers,
29765 &declares_class_or_enum);
29766 if (friend_p)
29767 *friend_p = cp_parser_friend_p (&decl_specifiers);
29768
29769 /* There are no template typedefs. */
29770 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
29771 {
29772 error_at (decl_spec_token_start->location,
29773 "template declaration of %<typedef%>");
29774 decl = error_mark_node;
29775 }
29776
29777 /* Gather up the access checks that occurred the
29778 decl-specifier-seq. */
29779 stop_deferring_access_checks ();
29780
29781 /* Check for the declaration of a template class. */
29782 if (declares_class_or_enum)
29783 {
29784 if (cp_parser_declares_only_class_p (parser)
29785 || (declares_class_or_enum & 2))
29786 {
29787 /* If this is a declaration, but not a definition, associate
29788 any constraints with the type declaration. Constraints
29789 are associated with definitions in cp_parser_class_specifier. */
29790 if (declares_class_or_enum == 1)
29791 associate_classtype_constraints (decl_specifiers.type);
29792
29793 decl = shadow_tag (&decl_specifiers);
29794
29795 /* In this case:
29796
29797 struct C {
29798 friend template <typename T> struct A<T>::B;
29799 };
29800
29801 A<T>::B will be represented by a TYPENAME_TYPE, and
29802 therefore not recognized by shadow_tag. */
29803 if (friend_p && *friend_p
29804 && !decl
29805 && decl_specifiers.type
29806 && TYPE_P (decl_specifiers.type))
29807 decl = decl_specifiers.type;
29808
29809 if (decl && decl != error_mark_node)
29810 decl = TYPE_NAME (decl);
29811 else
29812 decl = error_mark_node;
29813
29814 /* Perform access checks for template parameters. */
29815 cp_parser_perform_template_parameter_access_checks (checks);
29816
29817 /* Give a helpful diagnostic for
29818 template <class T> struct A { } a;
29819 if we aren't already recovering from an error. */
29820 if (!cp_parser_declares_only_class_p (parser)
29821 && !seen_error ())
29822 {
29823 error_at (cp_lexer_peek_token (parser->lexer)->location,
29824 "a class template declaration must not declare "
29825 "anything else");
29826 cp_parser_skip_to_end_of_block_or_statement (parser);
29827 goto out;
29828 }
29829 }
29830 }
29831
29832 /* Complain about missing 'typename' or other invalid type names. */
29833 if (!decl_specifiers.any_type_specifiers_p
29834 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
29835 {
29836 /* cp_parser_parse_and_diagnose_invalid_type_name calls
29837 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
29838 the rest of this declaration. */
29839 decl = error_mark_node;
29840 goto out;
29841 }
29842
29843 /* If it's not a template class, try for a template function. If
29844 the next token is a `;', then this declaration does not declare
29845 anything. But, if there were errors in the decl-specifiers, then
29846 the error might well have come from an attempted class-specifier.
29847 In that case, there's no need to warn about a missing declarator. */
29848 if (!decl
29849 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
29850 || decl_specifiers.type != error_mark_node))
29851 {
29852 decl = cp_parser_init_declarator (parser,
29853 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
29854 &decl_specifiers,
29855 checks,
29856 /*function_definition_allowed_p=*/true,
29857 member_p,
29858 declares_class_or_enum,
29859 &function_definition_p,
29860 NULL, NULL, NULL);
29861
29862 /* 7.1.1-1 [dcl.stc]
29863
29864 A storage-class-specifier shall not be specified in an explicit
29865 specialization... */
29866 if (decl
29867 && explicit_specialization_p
29868 && decl_specifiers.storage_class != sc_none)
29869 {
29870 error_at (decl_spec_token_start->location,
29871 "explicit template specialization cannot have a storage class");
29872 decl = error_mark_node;
29873 }
29874
29875 if (decl && VAR_P (decl))
29876 check_template_variable (decl);
29877 }
29878
29879 /* Look for a trailing `;' after the declaration. */
29880 if (!function_definition_p
29881 && (decl == error_mark_node
29882 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
29883 cp_parser_skip_to_end_of_block_or_statement (parser);
29884
29885 out:
29886 pop_deferring_access_checks ();
29887
29888 /* Clear any current qualification; whatever comes next is the start
29889 of something new. */
29890 parser->scope = NULL_TREE;
29891 parser->qualifying_scope = NULL_TREE;
29892 parser->object_scope = NULL_TREE;
29893
29894 return decl;
29895 }
29896
29897 /* Parse a cast-expression that is not the operand of a unary "&". */
29898
29899 static cp_expr
29900 cp_parser_simple_cast_expression (cp_parser *parser)
29901 {
29902 return cp_parser_cast_expression (parser, /*address_p=*/false,
29903 /*cast_p=*/false, /*decltype*/false, NULL);
29904 }
29905
29906 /* Parse a functional cast to TYPE. Returns an expression
29907 representing the cast. */
29908
29909 static cp_expr
29910 cp_parser_functional_cast (cp_parser* parser, tree type)
29911 {
29912 vec<tree, va_gc> *vec;
29913 tree expression_list;
29914 cp_expr cast;
29915 bool nonconst_p;
29916
29917 location_t start_loc = input_location;
29918
29919 if (!type)
29920 type = error_mark_node;
29921
29922 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29923 {
29924 cp_lexer_set_source_position (parser->lexer);
29925 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
29926 expression_list = cp_parser_braced_list (parser, &nonconst_p);
29927 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
29928 if (TREE_CODE (type) == TYPE_DECL)
29929 type = TREE_TYPE (type);
29930
29931 cast = finish_compound_literal (type, expression_list,
29932 tf_warning_or_error, fcl_functional);
29933 /* Create a location of the form:
29934 type_name{i, f}
29935 ^~~~~~~~~~~~~~~
29936 with caret == start at the start of the type name,
29937 finishing at the closing brace. */
29938 location_t combined_loc = make_location (start_loc, start_loc,
29939 parser->lexer);
29940 cast.set_location (combined_loc);
29941 return cast;
29942 }
29943
29944
29945 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
29946 /*cast_p=*/true,
29947 /*allow_expansion_p=*/true,
29948 /*non_constant_p=*/NULL);
29949 if (vec == NULL)
29950 expression_list = error_mark_node;
29951 else
29952 {
29953 expression_list = build_tree_list_vec (vec);
29954 release_tree_vector (vec);
29955 }
29956
29957 /* Create a location of the form:
29958 float(i)
29959 ^~~~~~~~
29960 with caret == start at the start of the type name,
29961 finishing at the closing paren. */
29962 location_t combined_loc = make_location (start_loc, start_loc,
29963 parser->lexer);
29964 cast = build_functional_cast (combined_loc, type, expression_list,
29965 tf_warning_or_error);
29966
29967 /* [expr.const]/1: In an integral constant expression "only type
29968 conversions to integral or enumeration type can be used". */
29969 if (TREE_CODE (type) == TYPE_DECL)
29970 type = TREE_TYPE (type);
29971 if (cast != error_mark_node
29972 && !cast_valid_in_integral_constant_expression_p (type)
29973 && cp_parser_non_integral_constant_expression (parser,
29974 NIC_CONSTRUCTOR))
29975 return error_mark_node;
29976
29977 return cast;
29978 }
29979
29980 /* Save the tokens that make up the body of a member function defined
29981 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
29982 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
29983 specifiers applied to the declaration. Returns the FUNCTION_DECL
29984 for the member function. */
29985
29986 static tree
29987 cp_parser_save_member_function_body (cp_parser* parser,
29988 cp_decl_specifier_seq *decl_specifiers,
29989 cp_declarator *declarator,
29990 tree attributes)
29991 {
29992 cp_token *first;
29993 cp_token *last;
29994 tree fn;
29995 bool function_try_block = false;
29996
29997 /* Create the FUNCTION_DECL. */
29998 fn = grokmethod (decl_specifiers, declarator, attributes);
29999 cp_finalize_omp_declare_simd (parser, fn);
30000 cp_finalize_oacc_routine (parser, fn, true);
30001 /* If something went badly wrong, bail out now. */
30002 if (fn == error_mark_node)
30003 {
30004 /* If there's a function-body, skip it. */
30005 if (cp_parser_token_starts_function_definition_p
30006 (cp_lexer_peek_token (parser->lexer)))
30007 cp_parser_skip_to_end_of_block_or_statement (parser);
30008 return error_mark_node;
30009 }
30010
30011 /* Remember it, if there are default args to post process. */
30012 cp_parser_save_default_args (parser, fn);
30013
30014 /* Save away the tokens that make up the body of the
30015 function. */
30016 first = parser->lexer->next_token;
30017
30018 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
30019 cp_lexer_consume_token (parser->lexer);
30020 else if (cp_lexer_next_token_is_keyword (parser->lexer,
30021 RID_TRANSACTION_ATOMIC))
30022 {
30023 cp_lexer_consume_token (parser->lexer);
30024 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
30025 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
30026 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
30027 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
30028 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
30029 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
30030 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
30031 {
30032 cp_lexer_consume_token (parser->lexer);
30033 cp_lexer_consume_token (parser->lexer);
30034 cp_lexer_consume_token (parser->lexer);
30035 cp_lexer_consume_token (parser->lexer);
30036 cp_lexer_consume_token (parser->lexer);
30037 }
30038 else
30039 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
30040 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
30041 {
30042 cp_lexer_consume_token (parser->lexer);
30043 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
30044 break;
30045 }
30046 }
30047
30048 /* Handle function try blocks. */
30049 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
30050 {
30051 cp_lexer_consume_token (parser->lexer);
30052 function_try_block = true;
30053 }
30054 /* We can have braced-init-list mem-initializers before the fn body. */
30055 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30056 {
30057 cp_lexer_consume_token (parser->lexer);
30058 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
30059 {
30060 /* cache_group will stop after an un-nested { } pair, too. */
30061 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
30062 break;
30063
30064 /* variadic mem-inits have ... after the ')'. */
30065 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30066 cp_lexer_consume_token (parser->lexer);
30067 }
30068 }
30069 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
30070 /* Handle function try blocks. */
30071 if (function_try_block)
30072 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
30073 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
30074 last = parser->lexer->next_token;
30075
30076 /* Save away the inline definition; we will process it when the
30077 class is complete. */
30078 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
30079 DECL_PENDING_INLINE_P (fn) = 1;
30080
30081 /* We need to know that this was defined in the class, so that
30082 friend templates are handled correctly. */
30083 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
30084
30085 /* Add FN to the queue of functions to be parsed later. */
30086 vec_safe_push (unparsed_funs_with_definitions, fn);
30087
30088 return fn;
30089 }
30090
30091 /* Save the tokens that make up the in-class initializer for a non-static
30092 data member. Returns a DEFERRED_PARSE. */
30093
30094 static tree
30095 cp_parser_save_nsdmi (cp_parser* parser)
30096 {
30097 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
30098 }
30099
30100 /* Parse a template-argument-list, as well as the trailing ">" (but
30101 not the opening "<"). See cp_parser_template_argument_list for the
30102 return value. */
30103
30104 static tree
30105 cp_parser_enclosed_template_argument_list (cp_parser* parser)
30106 {
30107 tree arguments;
30108 tree saved_scope;
30109 tree saved_qualifying_scope;
30110 tree saved_object_scope;
30111 bool saved_greater_than_is_operator_p;
30112
30113 /* [temp.names]
30114
30115 When parsing a template-id, the first non-nested `>' is taken as
30116 the end of the template-argument-list rather than a greater-than
30117 operator. */
30118 saved_greater_than_is_operator_p
30119 = parser->greater_than_is_operator_p;
30120 parser->greater_than_is_operator_p = false;
30121 /* Parsing the argument list may modify SCOPE, so we save it
30122 here. */
30123 saved_scope = parser->scope;
30124 saved_qualifying_scope = parser->qualifying_scope;
30125 saved_object_scope = parser->object_scope;
30126 /* We need to evaluate the template arguments, even though this
30127 template-id may be nested within a "sizeof". */
30128 cp_evaluated ev;
30129 /* Parse the template-argument-list itself. */
30130 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
30131 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
30132 arguments = NULL_TREE;
30133 else
30134 arguments = cp_parser_template_argument_list (parser);
30135 /* Look for the `>' that ends the template-argument-list. If we find
30136 a '>>' instead, it's probably just a typo. */
30137 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
30138 {
30139 if (cxx_dialect != cxx98)
30140 {
30141 /* In C++0x, a `>>' in a template argument list or cast
30142 expression is considered to be two separate `>'
30143 tokens. So, change the current token to a `>', but don't
30144 consume it: it will be consumed later when the outer
30145 template argument list (or cast expression) is parsed.
30146 Note that this replacement of `>' for `>>' is necessary
30147 even if we are parsing tentatively: in the tentative
30148 case, after calling
30149 cp_parser_enclosed_template_argument_list we will always
30150 throw away all of the template arguments and the first
30151 closing `>', either because the template argument list
30152 was erroneous or because we are replacing those tokens
30153 with a CPP_TEMPLATE_ID token. The second `>' (which will
30154 not have been thrown away) is needed either to close an
30155 outer template argument list or to complete a new-style
30156 cast. */
30157 cp_token *token = cp_lexer_peek_token (parser->lexer);
30158 token->type = CPP_GREATER;
30159 }
30160 else if (!saved_greater_than_is_operator_p)
30161 {
30162 /* If we're in a nested template argument list, the '>>' has
30163 to be a typo for '> >'. We emit the error message, but we
30164 continue parsing and we push a '>' as next token, so that
30165 the argument list will be parsed correctly. Note that the
30166 global source location is still on the token before the
30167 '>>', so we need to say explicitly where we want it. */
30168 cp_token *token = cp_lexer_peek_token (parser->lexer);
30169 gcc_rich_location richloc (token->location);
30170 richloc.add_fixit_replace ("> >");
30171 error_at (&richloc, "%<>>%> should be %<> >%> "
30172 "within a nested template argument list");
30173
30174 token->type = CPP_GREATER;
30175 }
30176 else
30177 {
30178 /* If this is not a nested template argument list, the '>>'
30179 is a typo for '>'. Emit an error message and continue.
30180 Same deal about the token location, but here we can get it
30181 right by consuming the '>>' before issuing the diagnostic. */
30182 cp_token *token = cp_lexer_consume_token (parser->lexer);
30183 error_at (token->location,
30184 "spurious %<>>%>, use %<>%> to terminate "
30185 "a template argument list");
30186 }
30187 }
30188 else
30189 cp_parser_skip_to_end_of_template_parameter_list (parser);
30190 /* The `>' token might be a greater-than operator again now. */
30191 parser->greater_than_is_operator_p
30192 = saved_greater_than_is_operator_p;
30193 /* Restore the SAVED_SCOPE. */
30194 parser->scope = saved_scope;
30195 parser->qualifying_scope = saved_qualifying_scope;
30196 parser->object_scope = saved_object_scope;
30197
30198 return arguments;
30199 }
30200
30201 /* MEMBER_FUNCTION is a member function, or a friend. If default
30202 arguments, or the body of the function have not yet been parsed,
30203 parse them now. */
30204
30205 static void
30206 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
30207 {
30208 timevar_push (TV_PARSE_INMETH);
30209 /* If this member is a template, get the underlying
30210 FUNCTION_DECL. */
30211 if (DECL_FUNCTION_TEMPLATE_P (member_function))
30212 member_function = DECL_TEMPLATE_RESULT (member_function);
30213
30214 /* There should not be any class definitions in progress at this
30215 point; the bodies of members are only parsed outside of all class
30216 definitions. */
30217 gcc_assert (parser->num_classes_being_defined == 0);
30218 /* While we're parsing the member functions we might encounter more
30219 classes. We want to handle them right away, but we don't want
30220 them getting mixed up with functions that are currently in the
30221 queue. */
30222 push_unparsed_function_queues (parser);
30223
30224 /* Make sure that any template parameters are in scope. */
30225 maybe_begin_member_template_processing (member_function);
30226
30227 /* If the body of the function has not yet been parsed, parse it
30228 now. */
30229 if (DECL_PENDING_INLINE_P (member_function))
30230 {
30231 tree function_scope;
30232 cp_token_cache *tokens;
30233
30234 /* The function is no longer pending; we are processing it. */
30235 tokens = DECL_PENDING_INLINE_INFO (member_function);
30236 DECL_PENDING_INLINE_INFO (member_function) = NULL;
30237 DECL_PENDING_INLINE_P (member_function) = 0;
30238
30239 /* If this is a local class, enter the scope of the containing
30240 function. */
30241 function_scope = current_function_decl;
30242 if (function_scope)
30243 push_function_context ();
30244
30245 /* Push the body of the function onto the lexer stack. */
30246 cp_parser_push_lexer_for_tokens (parser, tokens);
30247
30248 /* Let the front end know that we going to be defining this
30249 function. */
30250 start_preparsed_function (member_function, NULL_TREE,
30251 SF_PRE_PARSED | SF_INCLASS_INLINE);
30252
30253 /* Don't do access checking if it is a templated function. */
30254 if (processing_template_decl)
30255 push_deferring_access_checks (dk_no_check);
30256
30257 /* #pragma omp declare reduction needs special parsing. */
30258 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
30259 {
30260 parser->lexer->in_pragma = true;
30261 cp_parser_omp_declare_reduction_exprs (member_function, parser);
30262 finish_function (/*inline_p=*/true);
30263 cp_check_omp_declare_reduction (member_function);
30264 }
30265 else
30266 /* Now, parse the body of the function. */
30267 cp_parser_function_definition_after_declarator (parser,
30268 /*inline_p=*/true);
30269
30270 if (processing_template_decl)
30271 pop_deferring_access_checks ();
30272
30273 /* Leave the scope of the containing function. */
30274 if (function_scope)
30275 pop_function_context ();
30276 cp_parser_pop_lexer (parser);
30277 }
30278
30279 /* Remove any template parameters from the symbol table. */
30280 maybe_end_member_template_processing ();
30281
30282 /* Restore the queue. */
30283 pop_unparsed_function_queues (parser);
30284 timevar_pop (TV_PARSE_INMETH);
30285 }
30286
30287 /* If DECL contains any default args, remember it on the unparsed
30288 functions queue. */
30289
30290 static void
30291 cp_parser_save_default_args (cp_parser* parser, tree decl)
30292 {
30293 tree probe;
30294
30295 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
30296 probe;
30297 probe = TREE_CHAIN (probe))
30298 if (TREE_PURPOSE (probe))
30299 {
30300 cp_default_arg_entry entry = {current_class_type, decl};
30301 vec_safe_push (unparsed_funs_with_default_args, entry);
30302 break;
30303 }
30304
30305 /* Remember if there is a noexcept-specifier to post process. */
30306 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
30307 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
30308 vec_safe_push (unparsed_noexcepts, decl);
30309 }
30310
30311 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
30312 which is either a FIELD_DECL or PARM_DECL. Parse it and return
30313 the result. For a PARM_DECL, PARMTYPE is the corresponding type
30314 from the parameter-type-list. */
30315
30316 static tree
30317 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
30318 tree default_arg, tree parmtype)
30319 {
30320 cp_token_cache *tokens;
30321 tree parsed_arg;
30322 bool dummy;
30323
30324 if (default_arg == error_mark_node)
30325 return error_mark_node;
30326
30327 /* Push the saved tokens for the default argument onto the parser's
30328 lexer stack. */
30329 tokens = DEFPARSE_TOKENS (default_arg);
30330 cp_parser_push_lexer_for_tokens (parser, tokens);
30331
30332 start_lambda_scope (decl);
30333
30334 /* Parse the default argument. */
30335 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
30336 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
30337 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
30338
30339 finish_lambda_scope ();
30340
30341 if (parsed_arg == error_mark_node)
30342 cp_parser_skip_to_end_of_statement (parser);
30343
30344 if (!processing_template_decl)
30345 {
30346 /* In a non-template class, check conversions now. In a template,
30347 we'll wait and instantiate these as needed. */
30348 if (TREE_CODE (decl) == PARM_DECL)
30349 parsed_arg = check_default_argument (parmtype, parsed_arg,
30350 tf_warning_or_error);
30351 else if (maybe_reject_flexarray_init (decl, parsed_arg))
30352 parsed_arg = error_mark_node;
30353 else
30354 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
30355 }
30356
30357 /* If the token stream has not been completely used up, then
30358 there was extra junk after the end of the default
30359 argument. */
30360 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30361 {
30362 if (TREE_CODE (decl) == PARM_DECL)
30363 cp_parser_error (parser, "expected %<,%>");
30364 else
30365 cp_parser_error (parser, "expected %<;%>");
30366 }
30367
30368 /* Revert to the main lexer. */
30369 cp_parser_pop_lexer (parser);
30370
30371 return parsed_arg;
30372 }
30373
30374 /* FIELD is a non-static data member with an initializer which we saved for
30375 later; parse it now. */
30376
30377 static void
30378 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
30379 {
30380 tree def;
30381
30382 maybe_begin_member_template_processing (field);
30383
30384 push_unparsed_function_queues (parser);
30385 def = cp_parser_late_parse_one_default_arg (parser, field,
30386 DECL_INITIAL (field),
30387 NULL_TREE);
30388 pop_unparsed_function_queues (parser);
30389
30390 maybe_end_member_template_processing ();
30391
30392 DECL_INITIAL (field) = def;
30393 }
30394
30395 /* FN is a FUNCTION_DECL which may contains a parameter with an
30396 unparsed DEFERRED_PARSE. Parse the default args now. This function
30397 assumes that the current scope is the scope in which the default
30398 argument should be processed. */
30399
30400 static void
30401 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
30402 {
30403 unsigned char saved_local_variables_forbidden_p;
30404 tree parm, parmdecl;
30405
30406 /* While we're parsing the default args, we might (due to the
30407 statement expression extension) encounter more classes. We want
30408 to handle them right away, but we don't want them getting mixed
30409 up with default args that are currently in the queue. */
30410 push_unparsed_function_queues (parser);
30411
30412 /* Local variable names (and the `this' keyword) may not appear
30413 in a default argument. */
30414 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
30415 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
30416
30417 push_defarg_context (fn);
30418
30419 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
30420 parmdecl = DECL_ARGUMENTS (fn);
30421 parm && parm != void_list_node;
30422 parm = TREE_CHAIN (parm),
30423 parmdecl = DECL_CHAIN (parmdecl))
30424 {
30425 tree default_arg = TREE_PURPOSE (parm);
30426 tree parsed_arg;
30427 vec<tree, va_gc> *insts;
30428 tree copy;
30429 unsigned ix;
30430
30431 if (!default_arg)
30432 continue;
30433
30434 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
30435 /* This can happen for a friend declaration for a function
30436 already declared with default arguments. */
30437 continue;
30438
30439 parsed_arg
30440 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
30441 default_arg,
30442 TREE_VALUE (parm));
30443 TREE_PURPOSE (parm) = parsed_arg;
30444
30445 /* Update any instantiations we've already created. */
30446 for (insts = DEFPARSE_INSTANTIATIONS (default_arg), ix = 0;
30447 vec_safe_iterate (insts, ix, &copy); ix++)
30448 TREE_PURPOSE (copy) = parsed_arg;
30449 }
30450
30451 pop_defarg_context ();
30452
30453 /* Make sure no default arg is missing. */
30454 check_default_args (fn);
30455
30456 /* Restore the state of local_variables_forbidden_p. */
30457 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
30458
30459 /* Restore the queue. */
30460 pop_unparsed_function_queues (parser);
30461 }
30462
30463 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
30464
30465 sizeof ... ( identifier )
30466
30467 where the 'sizeof' token has already been consumed. */
30468
30469 static tree
30470 cp_parser_sizeof_pack (cp_parser *parser)
30471 {
30472 /* Consume the `...'. */
30473 cp_lexer_consume_token (parser->lexer);
30474 maybe_warn_variadic_templates ();
30475
30476 matching_parens parens;
30477 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
30478 if (paren)
30479 parens.consume_open (parser);
30480 else
30481 permerror (cp_lexer_peek_token (parser->lexer)->location,
30482 "%<sizeof...%> argument must be surrounded by parentheses");
30483
30484 cp_token *token = cp_lexer_peek_token (parser->lexer);
30485 tree name = cp_parser_identifier (parser);
30486 if (name == error_mark_node)
30487 return error_mark_node;
30488 /* The name is not qualified. */
30489 parser->scope = NULL_TREE;
30490 parser->qualifying_scope = NULL_TREE;
30491 parser->object_scope = NULL_TREE;
30492 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
30493 if (expr == error_mark_node)
30494 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
30495 token->location);
30496 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
30497 expr = TREE_TYPE (expr);
30498 else if (TREE_CODE (expr) == CONST_DECL)
30499 expr = DECL_INITIAL (expr);
30500 expr = make_pack_expansion (expr);
30501 PACK_EXPANSION_SIZEOF_P (expr) = true;
30502
30503 if (paren)
30504 parens.require_close (parser);
30505
30506 return expr;
30507 }
30508
30509 /* Parse the operand of `sizeof' (or a similar operator). Returns
30510 either a TYPE or an expression, depending on the form of the
30511 input. The KEYWORD indicates which kind of expression we have
30512 encountered. */
30513
30514 static tree
30515 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
30516 {
30517 tree expr = NULL_TREE;
30518 const char *saved_message;
30519 const char *saved_message_arg;
30520 bool saved_integral_constant_expression_p;
30521 bool saved_non_integral_constant_expression_p;
30522
30523 /* If it's a `...', then we are computing the length of a parameter
30524 pack. */
30525 if (keyword == RID_SIZEOF
30526 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30527 return cp_parser_sizeof_pack (parser);
30528
30529 /* Types cannot be defined in a `sizeof' expression. Save away the
30530 old message. */
30531 saved_message = parser->type_definition_forbidden_message;
30532 saved_message_arg = parser->type_definition_forbidden_message_arg;
30533 parser->type_definition_forbidden_message
30534 = G_("types may not be defined in %qs expressions");
30535 parser->type_definition_forbidden_message_arg
30536 = IDENTIFIER_POINTER (ridpointers[keyword]);
30537
30538 /* The restrictions on constant-expressions do not apply inside
30539 sizeof expressions. */
30540 saved_integral_constant_expression_p
30541 = parser->integral_constant_expression_p;
30542 saved_non_integral_constant_expression_p
30543 = parser->non_integral_constant_expression_p;
30544 parser->integral_constant_expression_p = false;
30545
30546 /* Do not actually evaluate the expression. */
30547 ++cp_unevaluated_operand;
30548 ++c_inhibit_evaluation_warnings;
30549 /* If it's a `(', then we might be looking at the type-id
30550 construction. */
30551 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30552 {
30553 tree type = NULL_TREE;
30554
30555 tentative_firewall firewall (parser);
30556
30557 /* We can't be sure yet whether we're looking at a type-id or an
30558 expression. */
30559 cp_parser_parse_tentatively (parser);
30560
30561 matching_parens parens;
30562 parens.consume_open (parser);
30563
30564 /* Note: as a GNU Extension, compound literals are considered
30565 postfix-expressions as they are in C99, so they are valid
30566 arguments to sizeof. See comment in cp_parser_cast_expression
30567 for details. */
30568 if (cp_parser_compound_literal_p (parser))
30569 cp_parser_simulate_error (parser);
30570 else
30571 {
30572 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
30573 parser->in_type_id_in_expr_p = true;
30574 /* Look for the type-id. */
30575 type = cp_parser_type_id (parser);
30576 /* Look for the closing `)'. */
30577 parens.require_close (parser);
30578 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
30579 }
30580
30581 /* If all went well, then we're done. */
30582 if (cp_parser_parse_definitely (parser))
30583 expr = type;
30584 else
30585 {
30586 /* Commit to the tentative_firewall so we get syntax errors. */
30587 cp_parser_commit_to_tentative_parse (parser);
30588
30589 expr = cp_parser_unary_expression (parser);
30590 }
30591 }
30592 else
30593 expr = cp_parser_unary_expression (parser);
30594
30595 /* Go back to evaluating expressions. */
30596 --cp_unevaluated_operand;
30597 --c_inhibit_evaluation_warnings;
30598
30599 /* And restore the old one. */
30600 parser->type_definition_forbidden_message = saved_message;
30601 parser->type_definition_forbidden_message_arg = saved_message_arg;
30602 parser->integral_constant_expression_p
30603 = saved_integral_constant_expression_p;
30604 parser->non_integral_constant_expression_p
30605 = saved_non_integral_constant_expression_p;
30606
30607 return expr;
30608 }
30609
30610 /* If the current declaration has no declarator, return true. */
30611
30612 static bool
30613 cp_parser_declares_only_class_p (cp_parser *parser)
30614 {
30615 /* If the next token is a `;' or a `,' then there is no
30616 declarator. */
30617 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30618 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
30619 }
30620
30621 /* Update the DECL_SPECS to reflect the storage class indicated by
30622 KEYWORD. */
30623
30624 static void
30625 cp_parser_set_storage_class (cp_parser *parser,
30626 cp_decl_specifier_seq *decl_specs,
30627 enum rid keyword,
30628 cp_token *token)
30629 {
30630 cp_storage_class storage_class;
30631
30632 if (parser->in_unbraced_linkage_specification_p)
30633 {
30634 error_at (token->location, "invalid use of %qD in linkage specification",
30635 ridpointers[keyword]);
30636 return;
30637 }
30638 else if (decl_specs->storage_class != sc_none)
30639 {
30640 decl_specs->conflicting_specifiers_p = true;
30641 return;
30642 }
30643
30644 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
30645 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
30646 && decl_specs->gnu_thread_keyword_p)
30647 {
30648 pedwarn (decl_specs->locations[ds_thread], 0,
30649 "%<__thread%> before %qD", ridpointers[keyword]);
30650 }
30651
30652 switch (keyword)
30653 {
30654 case RID_AUTO:
30655 storage_class = sc_auto;
30656 break;
30657 case RID_REGISTER:
30658 storage_class = sc_register;
30659 break;
30660 case RID_STATIC:
30661 storage_class = sc_static;
30662 break;
30663 case RID_EXTERN:
30664 storage_class = sc_extern;
30665 break;
30666 case RID_MUTABLE:
30667 storage_class = sc_mutable;
30668 break;
30669 default:
30670 gcc_unreachable ();
30671 }
30672 decl_specs->storage_class = storage_class;
30673 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
30674
30675 /* A storage class specifier cannot be applied alongside a typedef
30676 specifier. If there is a typedef specifier present then set
30677 conflicting_specifiers_p which will trigger an error later
30678 on in grokdeclarator. */
30679 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
30680 decl_specs->conflicting_specifiers_p = true;
30681 }
30682
30683 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
30684 is true, the type is a class or enum definition. */
30685
30686 static void
30687 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
30688 tree type_spec,
30689 cp_token *token,
30690 bool type_definition_p)
30691 {
30692 decl_specs->any_specifiers_p = true;
30693
30694 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
30695 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
30696 this is what happened. In system headers, we ignore these
30697 declarations so that G++ can work with system headers that are not
30698 C++-safe. */
30699 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
30700 && !type_definition_p
30701 && (type_spec == boolean_type_node
30702 || type_spec == char8_type_node
30703 || type_spec == char16_type_node
30704 || type_spec == char32_type_node
30705 || type_spec == wchar_type_node)
30706 && (decl_specs->type
30707 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
30708 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
30709 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
30710 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
30711 {
30712 decl_specs->redefined_builtin_type = type_spec;
30713 set_and_check_decl_spec_loc (decl_specs,
30714 ds_redefined_builtin_type_spec,
30715 token);
30716 if (!decl_specs->type)
30717 {
30718 decl_specs->type = type_spec;
30719 decl_specs->type_definition_p = false;
30720 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
30721 }
30722 }
30723 else if (decl_specs->type)
30724 decl_specs->multiple_types_p = true;
30725 else
30726 {
30727 decl_specs->type = type_spec;
30728 decl_specs->type_definition_p = type_definition_p;
30729 decl_specs->redefined_builtin_type = NULL_TREE;
30730 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
30731 }
30732 }
30733
30734 /* True iff TOKEN is the GNU keyword __thread. */
30735
30736 static bool
30737 token_is__thread (cp_token *token)
30738 {
30739 gcc_assert (token->keyword == RID_THREAD);
30740 return id_equal (token->u.value, "__thread");
30741 }
30742
30743 /* Set the location for a declarator specifier and check if it is
30744 duplicated.
30745
30746 DECL_SPECS is the sequence of declarator specifiers onto which to
30747 set the location.
30748
30749 DS is the single declarator specifier to set which location is to
30750 be set onto the existing sequence of declarators.
30751
30752 LOCATION is the location for the declarator specifier to
30753 consider. */
30754
30755 static void
30756 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
30757 cp_decl_spec ds, cp_token *token)
30758 {
30759 gcc_assert (ds < ds_last);
30760
30761 if (decl_specs == NULL)
30762 return;
30763
30764 location_t location = token->location;
30765
30766 if (decl_specs->locations[ds] == 0)
30767 {
30768 decl_specs->locations[ds] = location;
30769 if (ds == ds_thread)
30770 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
30771 }
30772 else
30773 {
30774 if (ds == ds_long)
30775 {
30776 if (decl_specs->locations[ds_long_long] != 0)
30777 error_at (location,
30778 "%<long long long%> is too long for GCC");
30779 else
30780 {
30781 decl_specs->locations[ds_long_long] = location;
30782 pedwarn_cxx98 (location,
30783 OPT_Wlong_long,
30784 "ISO C++ 1998 does not support %<long long%>");
30785 }
30786 }
30787 else if (ds == ds_thread)
30788 {
30789 bool gnu = token_is__thread (token);
30790 gcc_rich_location richloc (location);
30791 if (gnu != decl_specs->gnu_thread_keyword_p)
30792 {
30793 richloc.add_range (decl_specs->locations[ds_thread]);
30794 error_at (&richloc,
30795 "both %<__thread%> and %<thread_local%> specified");
30796 }
30797 else
30798 {
30799 richloc.add_fixit_remove ();
30800 error_at (&richloc, "duplicate %qD", token->u.value);
30801 }
30802 }
30803 else
30804 {
30805 static const char *const decl_spec_names[] = {
30806 "signed",
30807 "unsigned",
30808 "short",
30809 "long",
30810 "const",
30811 "volatile",
30812 "restrict",
30813 "inline",
30814 "virtual",
30815 "explicit",
30816 "friend",
30817 "typedef",
30818 "using",
30819 "constexpr",
30820 "__complex",
30821 "constinit",
30822 "consteval"
30823 };
30824 gcc_rich_location richloc (location);
30825 richloc.add_fixit_remove ();
30826 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
30827 }
30828 }
30829 }
30830
30831 /* Return true iff the declarator specifier DS is present in the
30832 sequence of declarator specifiers DECL_SPECS. */
30833
30834 bool
30835 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
30836 cp_decl_spec ds)
30837 {
30838 gcc_assert (ds < ds_last);
30839
30840 if (decl_specs == NULL)
30841 return false;
30842
30843 return decl_specs->locations[ds] != 0;
30844 }
30845
30846 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
30847 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
30848
30849 static bool
30850 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
30851 {
30852 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
30853 }
30854
30855 /* Issue an error message indicating that TOKEN_DESC was expected.
30856 If KEYWORD is true, it indicated this function is called by
30857 cp_parser_require_keword and the required token can only be
30858 a indicated keyword.
30859
30860 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
30861 within any error as the location of an "opening" token matching
30862 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
30863 RT_CLOSE_PAREN). */
30864
30865 static void
30866 cp_parser_required_error (cp_parser *parser,
30867 required_token token_desc,
30868 bool keyword,
30869 location_t matching_location)
30870 {
30871 if (cp_parser_simulate_error (parser))
30872 return;
30873
30874 const char *gmsgid = NULL;
30875 switch (token_desc)
30876 {
30877 case RT_NEW:
30878 gmsgid = G_("expected %<new%>");
30879 break;
30880 case RT_DELETE:
30881 gmsgid = G_("expected %<delete%>");
30882 break;
30883 case RT_RETURN:
30884 gmsgid = G_("expected %<return%>");
30885 break;
30886 case RT_WHILE:
30887 gmsgid = G_("expected %<while%>");
30888 break;
30889 case RT_EXTERN:
30890 gmsgid = G_("expected %<extern%>");
30891 break;
30892 case RT_STATIC_ASSERT:
30893 gmsgid = G_("expected %<static_assert%>");
30894 break;
30895 case RT_DECLTYPE:
30896 gmsgid = G_("expected %<decltype%>");
30897 break;
30898 case RT_OPERATOR:
30899 gmsgid = G_("expected %<operator%>");
30900 break;
30901 case RT_CLASS:
30902 gmsgid = G_("expected %<class%>");
30903 break;
30904 case RT_TEMPLATE:
30905 gmsgid = G_("expected %<template%>");
30906 break;
30907 case RT_NAMESPACE:
30908 gmsgid = G_("expected %<namespace%>");
30909 break;
30910 case RT_USING:
30911 gmsgid = G_("expected %<using%>");
30912 break;
30913 case RT_ASM:
30914 gmsgid = G_("expected %<asm%>");
30915 break;
30916 case RT_TRY:
30917 gmsgid = G_("expected %<try%>");
30918 break;
30919 case RT_CATCH:
30920 gmsgid = G_("expected %<catch%>");
30921 break;
30922 case RT_THROW:
30923 gmsgid = G_("expected %<throw%>");
30924 break;
30925 case RT_AUTO:
30926 gmsgid = G_("expected %<auto%>");
30927 break;
30928 case RT_LABEL:
30929 gmsgid = G_("expected %<__label__%>");
30930 break;
30931 case RT_AT_TRY:
30932 gmsgid = G_("expected %<@try%>");
30933 break;
30934 case RT_AT_SYNCHRONIZED:
30935 gmsgid = G_("expected %<@synchronized%>");
30936 break;
30937 case RT_AT_THROW:
30938 gmsgid = G_("expected %<@throw%>");
30939 break;
30940 case RT_TRANSACTION_ATOMIC:
30941 gmsgid = G_("expected %<__transaction_atomic%>");
30942 break;
30943 case RT_TRANSACTION_RELAXED:
30944 gmsgid = G_("expected %<__transaction_relaxed%>");
30945 break;
30946 case RT_CO_YIELD:
30947 gmsgid = G_("expected %<co_yield%>");
30948 break;
30949 default:
30950 break;
30951 }
30952
30953 if (!gmsgid && !keyword)
30954 {
30955 switch (token_desc)
30956 {
30957 case RT_SEMICOLON:
30958 gmsgid = G_("expected %<;%>");
30959 break;
30960 case RT_OPEN_PAREN:
30961 gmsgid = G_("expected %<(%>");
30962 break;
30963 case RT_CLOSE_BRACE:
30964 gmsgid = G_("expected %<}%>");
30965 break;
30966 case RT_OPEN_BRACE:
30967 gmsgid = G_("expected %<{%>");
30968 break;
30969 case RT_CLOSE_SQUARE:
30970 gmsgid = G_("expected %<]%>");
30971 break;
30972 case RT_OPEN_SQUARE:
30973 gmsgid = G_("expected %<[%>");
30974 break;
30975 case RT_COMMA:
30976 gmsgid = G_("expected %<,%>");
30977 break;
30978 case RT_SCOPE:
30979 gmsgid = G_("expected %<::%>");
30980 break;
30981 case RT_LESS:
30982 gmsgid = G_("expected %<<%>");
30983 break;
30984 case RT_GREATER:
30985 gmsgid = G_("expected %<>%>");
30986 break;
30987 case RT_EQ:
30988 gmsgid = G_("expected %<=%>");
30989 break;
30990 case RT_ELLIPSIS:
30991 gmsgid = G_("expected %<...%>");
30992 break;
30993 case RT_MULT:
30994 gmsgid = G_("expected %<*%>");
30995 break;
30996 case RT_COMPL:
30997 gmsgid = G_("expected %<~%>");
30998 break;
30999 case RT_COLON:
31000 gmsgid = G_("expected %<:%>");
31001 break;
31002 case RT_COLON_SCOPE:
31003 gmsgid = G_("expected %<:%> or %<::%>");
31004 break;
31005 case RT_CLOSE_PAREN:
31006 gmsgid = G_("expected %<)%>");
31007 break;
31008 case RT_COMMA_CLOSE_PAREN:
31009 gmsgid = G_("expected %<,%> or %<)%>");
31010 break;
31011 case RT_PRAGMA_EOL:
31012 gmsgid = G_("expected end of line");
31013 break;
31014 case RT_NAME:
31015 gmsgid = G_("expected identifier");
31016 break;
31017 case RT_SELECT:
31018 gmsgid = G_("expected selection-statement");
31019 break;
31020 case RT_ITERATION:
31021 gmsgid = G_("expected iteration-statement");
31022 break;
31023 case RT_JUMP:
31024 gmsgid = G_("expected jump-statement");
31025 break;
31026 case RT_CLASS_KEY:
31027 gmsgid = G_("expected class-key");
31028 break;
31029 case RT_CLASS_TYPENAME_TEMPLATE:
31030 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
31031 break;
31032 default:
31033 gcc_unreachable ();
31034 }
31035 }
31036
31037 if (gmsgid)
31038 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
31039 }
31040
31041
31042 /* If the next token is of the indicated TYPE, consume it. Otherwise,
31043 issue an error message indicating that TOKEN_DESC was expected.
31044
31045 Returns the token consumed, if the token had the appropriate type.
31046 Otherwise, returns NULL.
31047
31048 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
31049 within any error as the location of an "opening" token matching
31050 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
31051 RT_CLOSE_PAREN). */
31052
31053 static cp_token *
31054 cp_parser_require (cp_parser* parser,
31055 enum cpp_ttype type,
31056 required_token token_desc,
31057 location_t matching_location)
31058 {
31059 if (cp_lexer_next_token_is (parser->lexer, type))
31060 return cp_lexer_consume_token (parser->lexer);
31061 else
31062 {
31063 /* Output the MESSAGE -- unless we're parsing tentatively. */
31064 if (!cp_parser_simulate_error (parser))
31065 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
31066 matching_location);
31067 return NULL;
31068 }
31069 }
31070
31071 /* An error message is produced if the next token is not '>'.
31072 All further tokens are skipped until the desired token is
31073 found or '{', '}', ';' or an unbalanced ')' or ']'. */
31074
31075 static void
31076 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
31077 {
31078 /* Current level of '< ... >'. */
31079 unsigned level = 0;
31080 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
31081 unsigned nesting_depth = 0;
31082
31083 /* Are we ready, yet? If not, issue error message. */
31084 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
31085 return;
31086
31087 /* Skip tokens until the desired token is found. */
31088 while (true)
31089 {
31090 /* Peek at the next token. */
31091 switch (cp_lexer_peek_token (parser->lexer)->type)
31092 {
31093 case CPP_LESS:
31094 if (!nesting_depth)
31095 ++level;
31096 break;
31097
31098 case CPP_RSHIFT:
31099 if (cxx_dialect == cxx98)
31100 /* C++0x views the `>>' operator as two `>' tokens, but
31101 C++98 does not. */
31102 break;
31103 else if (!nesting_depth && level-- == 0)
31104 {
31105 /* We've hit a `>>' where the first `>' closes the
31106 template argument list, and the second `>' is
31107 spurious. Just consume the `>>' and stop; we've
31108 already produced at least one error. */
31109 cp_lexer_consume_token (parser->lexer);
31110 return;
31111 }
31112 /* Fall through for C++0x, so we handle the second `>' in
31113 the `>>'. */
31114 gcc_fallthrough ();
31115
31116 case CPP_GREATER:
31117 if (!nesting_depth && level-- == 0)
31118 {
31119 /* We've reached the token we want, consume it and stop. */
31120 cp_lexer_consume_token (parser->lexer);
31121 return;
31122 }
31123 break;
31124
31125 case CPP_OPEN_PAREN:
31126 case CPP_OPEN_SQUARE:
31127 ++nesting_depth;
31128 break;
31129
31130 case CPP_CLOSE_PAREN:
31131 case CPP_CLOSE_SQUARE:
31132 if (nesting_depth-- == 0)
31133 return;
31134 break;
31135
31136 case CPP_EOF:
31137 case CPP_PRAGMA_EOL:
31138 case CPP_SEMICOLON:
31139 case CPP_OPEN_BRACE:
31140 case CPP_CLOSE_BRACE:
31141 /* The '>' was probably forgotten, don't look further. */
31142 return;
31143
31144 default:
31145 break;
31146 }
31147
31148 /* Consume this token. */
31149 cp_lexer_consume_token (parser->lexer);
31150 }
31151 }
31152
31153 /* If the next token is the indicated keyword, consume it. Otherwise,
31154 issue an error message indicating that TOKEN_DESC was expected.
31155
31156 Returns the token consumed, if the token had the appropriate type.
31157 Otherwise, returns NULL. */
31158
31159 static cp_token *
31160 cp_parser_require_keyword (cp_parser* parser,
31161 enum rid keyword,
31162 required_token token_desc)
31163 {
31164 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
31165
31166 if (token && token->keyword != keyword)
31167 {
31168 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
31169 UNKNOWN_LOCATION);
31170 return NULL;
31171 }
31172
31173 return token;
31174 }
31175
31176 /* Returns TRUE iff TOKEN is a token that can begin the body of a
31177 function-definition. */
31178
31179 static bool
31180 cp_parser_token_starts_function_definition_p (cp_token* token)
31181 {
31182 return (/* An ordinary function-body begins with an `{'. */
31183 token->type == CPP_OPEN_BRACE
31184 /* A ctor-initializer begins with a `:'. */
31185 || token->type == CPP_COLON
31186 /* A function-try-block begins with `try'. */
31187 || token->keyword == RID_TRY
31188 /* A function-transaction-block begins with `__transaction_atomic'
31189 or `__transaction_relaxed'. */
31190 || token->keyword == RID_TRANSACTION_ATOMIC
31191 || token->keyword == RID_TRANSACTION_RELAXED
31192 /* The named return value extension begins with `return'. */
31193 || token->keyword == RID_RETURN);
31194 }
31195
31196 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
31197 definition. */
31198
31199 static bool
31200 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
31201 {
31202 cp_token *token;
31203
31204 token = cp_lexer_peek_token (parser->lexer);
31205 return (token->type == CPP_OPEN_BRACE
31206 || (token->type == CPP_COLON
31207 && !parser->colon_doesnt_start_class_def_p));
31208 }
31209
31210 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
31211 C++0x) ending a template-argument. */
31212
31213 static bool
31214 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
31215 {
31216 cp_token *token;
31217
31218 token = cp_lexer_peek_token (parser->lexer);
31219 return (token->type == CPP_COMMA
31220 || token->type == CPP_GREATER
31221 || token->type == CPP_ELLIPSIS
31222 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
31223 }
31224
31225 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
31226 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
31227
31228 static bool
31229 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
31230 size_t n)
31231 {
31232 cp_token *token;
31233
31234 token = cp_lexer_peek_nth_token (parser->lexer, n);
31235 if (token->type == CPP_LESS)
31236 return true;
31237 /* Check for the sequence `<::' in the original code. It would be lexed as
31238 `[:', where `[' is a digraph, and there is no whitespace before
31239 `:'. */
31240 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
31241 {
31242 cp_token *token2;
31243 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
31244 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
31245 return true;
31246 }
31247 return false;
31248 }
31249
31250 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
31251 or none_type otherwise. */
31252
31253 static enum tag_types
31254 cp_parser_token_is_class_key (cp_token* token)
31255 {
31256 switch (token->keyword)
31257 {
31258 case RID_CLASS:
31259 return class_type;
31260 case RID_STRUCT:
31261 return record_type;
31262 case RID_UNION:
31263 return union_type;
31264
31265 default:
31266 return none_type;
31267 }
31268 }
31269
31270 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
31271 or none_type otherwise or if the token is null. */
31272
31273 static enum tag_types
31274 cp_parser_token_is_type_parameter_key (cp_token* token)
31275 {
31276 if (!token)
31277 return none_type;
31278
31279 switch (token->keyword)
31280 {
31281 case RID_CLASS:
31282 return class_type;
31283 case RID_TYPENAME:
31284 return typename_type;
31285
31286 default:
31287 return none_type;
31288 }
31289 }
31290
31291 /* Diagnose redundant enum-keys. */
31292
31293 static void
31294 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
31295 tree type, rid scoped_key)
31296 {
31297 if (!warn_redundant_tags)
31298 return;
31299
31300 tree type_decl = TYPE_MAIN_DECL (type);
31301 tree name = DECL_NAME (type_decl);
31302 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
31303 push_deferring_access_checks (dk_no_check);
31304 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
31305 pop_deferring_access_checks ();
31306
31307 /* The enum-key is redundant for uses of the TYPE that are not
31308 declarations and for which name lookup returns just the type
31309 itself. */
31310 if (decl != type_decl)
31311 return;
31312
31313 if (scoped_key != RID_CLASS
31314 && scoped_key != RID_STRUCT
31315 && current_lang_name != lang_name_cplusplus
31316 && current_namespace == global_namespace)
31317 {
31318 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
31319 enum tag in shared C/C++ code in files (such as headers) included
31320 in the main source file. */
31321 const line_map_ordinary *map = NULL;
31322 linemap_resolve_location (line_table, key_loc,
31323 LRK_MACRO_DEFINITION_LOCATION,
31324 &map);
31325 if (!MAIN_FILE_P (map))
31326 return;
31327 }
31328
31329 gcc_rich_location richloc (key_loc);
31330 richloc.add_fixit_remove (key_loc);
31331 warning_at (&richloc, OPT_Wredundant_tags,
31332 "redundant enum-key %<enum%s%> in reference to %q#T",
31333 (scoped_key == RID_CLASS ? " class"
31334 : scoped_key == RID_STRUCT ? " struct" : ""), type);
31335 }
31336
31337 /* Describes the set of declarations of a struct, class, or class template
31338 or its specializations. Used for -Wmismatched-tags. */
31339
31340 class class_decl_loc_t
31341 {
31342 public:
31343
31344 class_decl_loc_t ()
31345 : locvec (), idxdef (), def_class_key ()
31346 {
31347 locvec.create (4);
31348 }
31349
31350 /* Constructs an object for a single declaration of a class with
31351 CLASS_KEY at the current location in the current function (or
31352 at another scope). KEY_REDUNDANT is true if the class-key may
31353 be omitted in the current context without an ambiguity with
31354 another symbol with the same name.
31355 DEF_P is true for a class declaration that is a definition.
31356 CURLOC is the associated location. */
31357 class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
31358 location_t curloc = input_location)
31359 : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
31360 {
31361 locvec.create (4);
31362 class_key_loc_t ckl (current_function_decl, curloc, class_key,
31363 key_redundant);
31364 locvec.quick_push (ckl);
31365 }
31366
31367 /* Copy, assign, and destroy the object. Necessary because LOCVEC
31368 isn't safely copyable and assignable and doesn't release storage
31369 on its own. */
31370 class_decl_loc_t (const class_decl_loc_t &rhs)
31371 : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
31372 def_class_key (rhs.def_class_key)
31373 { }
31374
31375 class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
31376 {
31377 if (this == &rhs)
31378 return *this;
31379 locvec.release ();
31380 locvec = rhs.locvec.copy ();
31381 idxdef = rhs.idxdef;
31382 def_class_key = rhs.def_class_key;
31383 return *this;
31384 }
31385
31386 ~class_decl_loc_t ()
31387 {
31388 locvec.release ();
31389 }
31390
31391 /* Issues -Wmismatched-tags for a single class. */
31392 void diag_mismatched_tags (tree);
31393
31394 /* Issues -Wmismatched-tags for all classes. */
31395 static void diag_mismatched_tags ();
31396
31397 /* Adds TYPE_DECL to the collection of class decls and diagnoses
31398 redundant tags (if -Wredundant-tags is enabled). */
31399 static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
31400
31401 /* Either adds this decl to the collection of class decls
31402 or diagnoses it, whichever is appropriate. */
31403 void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
31404
31405 private:
31406
31407 tree function (unsigned i) const
31408 {
31409 return locvec[i].func;
31410 }
31411
31412 location_t location (unsigned i) const
31413 {
31414 return locvec[i].loc;
31415 }
31416
31417 bool key_redundant (unsigned i) const
31418 {
31419 return locvec[i].key_redundant;
31420 }
31421
31422 tag_types class_key (unsigned i) const
31423 {
31424 return locvec[i].class_key;
31425 }
31426
31427 /* True if a definition for the class has been seen. */
31428 bool def_p () const
31429 {
31430 return idxdef < locvec.length ();
31431 }
31432
31433 /* The location of a single mention of a class type with the given
31434 class-key. */
31435 struct class_key_loc_t
31436 {
31437 class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
31438 : func (func), loc (loc), class_key (key), key_redundant (redundant)
31439 { }
31440
31441 /* The function the type is mentioned in. */
31442 tree func;
31443 /* The exact location. */
31444 location_t loc;
31445 /* The class-key used in the mention of the type. */
31446 tag_types class_key;
31447 /* True when the class-key could be omitted at this location
31448 without an ambiguity with another symbol of the same name. */
31449 bool key_redundant;
31450 };
31451 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
31452 vec <class_key_loc_t> locvec;
31453 /* LOCVEC index of the definition or UINT_MAX if none exists. */
31454 unsigned idxdef;
31455 /* The class-key the class was last declared with or none_type when
31456 it has been declared with a mismatched key. */
31457 tag_types def_class_key;
31458
31459 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
31460 description above. */
31461 typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
31462 static class_to_loc_map_t class2loc;
31463 };
31464
31465 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
31466
31467 /* Issue an error message if the CLASS_KEY does not match the TYPE.
31468 DEF_P is expected to be set for a definition of class TYPE. DECL_P
31469 is set for a declaration of class TYPE and clear for a reference to
31470 it that is not a declaration of it. */
31471
31472 static void
31473 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
31474 tag_types class_key, tree type, bool def_p,
31475 bool decl_p)
31476 {
31477 if (type == error_mark_node)
31478 return;
31479
31480 bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
31481 if (seen_as_union != (class_key == union_type))
31482 {
31483 if (permerror (input_location, "%qs tag used in naming %q#T",
31484 class_key == union_type ? "union"
31485 : class_key == record_type ? "struct" : "class",
31486 type))
31487 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
31488 "%q#T was previously declared here", type);
31489 return;
31490 }
31491
31492 if (!warn_mismatched_tags && !warn_redundant_tags)
31493 return;
31494
31495 /* Only consider the true class-keys below and ignore typename_type,
31496 etc. that are not C++ class-keys. */
31497 if (class_key != class_type
31498 && class_key != record_type
31499 && class_key != union_type)
31500 return;
31501
31502 class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
31503 }
31504
31505 /* Returns the template or specialization of one to which the RECORD_TYPE
31506 TYPE corresponds. */
31507
31508 static tree
31509 specialization_of (tree type)
31510 {
31511 tree ret = type;
31512
31513 /* Determine the template or its partial specialization to which TYPE
31514 corresponds. */
31515 if (tree spec = most_specialized_partial_spec (type, tf_none))
31516 if (spec != error_mark_node)
31517 ret = TREE_TYPE (TREE_VALUE (spec));
31518
31519 if (ret == type)
31520 ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
31521
31522 return TYPE_MAIN_DECL (ret);
31523 }
31524
31525
31526 /* Adds the class TYPE to the collection of class decls and diagnoses
31527 redundant tags (if -Wredundant-tags is enabled).
31528 DEF_P is expected to be set for a definition of class TYPE. DECL_P
31529 is set for a (likely, based on syntactic context) declaration of class
31530 TYPE and clear for a reference to it that is not a declaration of it. */
31531
31532 void
31533 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
31534 tag_types class_key, tree type, bool def_p, bool decl_p)
31535 {
31536 tree type_decl = TYPE_MAIN_DECL (type);
31537 tree name = DECL_NAME (type_decl);
31538 /* Look up the NAME to see if it unambiguously refers to the TYPE
31539 and set KEY_REDUNDANT if so. */
31540 push_deferring_access_checks (dk_no_check);
31541 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
31542 pop_deferring_access_checks ();
31543
31544 /* The class-key is redundant for uses of the CLASS_TYPE that are
31545 neither definitions of it nor declarations, and for which name
31546 lookup returns just the type itself. */
31547 bool key_redundant = (!def_p && !decl_p
31548 && (decl == type_decl
31549 || TREE_CODE (decl) == TEMPLATE_DECL
31550 || TYPE_BEING_DEFINED (type)));
31551
31552 if (key_redundant
31553 && class_key != class_type
31554 && current_lang_name != lang_name_cplusplus
31555 && current_namespace == global_namespace)
31556 {
31557 /* Avoid issuing the diagnostic for apparently redundant struct
31558 and union class-keys in shared C/C++ code in files (such as
31559 headers) included in the main source file. */
31560 const line_map_ordinary *map = NULL;
31561 linemap_resolve_location (line_table, key_loc,
31562 LRK_MACRO_DEFINITION_LOCATION,
31563 &map);
31564 if (!MAIN_FILE_P (map))
31565 key_redundant = false;
31566 }
31567
31568 /* Set if a declaration of TYPE has previously been seen or if it must
31569 exist in a precompiled header. */
31570 bool exist;
31571 class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
31572 if (!exist)
31573 {
31574 tree type = TREE_TYPE (type_decl);
31575 if (def_p || !COMPLETE_TYPE_P (type))
31576 {
31577 /* TYPE_DECL is the first declaration or definition of the type
31578 (outside precompiled headers -- see below). Just create
31579 a new entry for it and return unless it's a declaration
31580 involving a template that may need to be diagnosed by
31581 -Wredundant-tags. */
31582 *rdl = class_decl_loc_t (class_key, false, def_p);
31583 if (TREE_CODE (decl) != TEMPLATE_DECL)
31584 return;
31585 }
31586 else
31587 {
31588 /* TYPE was previously defined in some unknown precompiled hdeader.
31589 Simply add a record of its definition at an unknown location and
31590 proceed below to add a reference to it at the current location.
31591 (Declarations in precompiled headers that are not definitions
31592 are ignored.) */
31593 tag_types def_key
31594 = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
31595 location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
31596 *rdl = class_decl_loc_t (def_key, false, true, def_loc);
31597 exist = true;
31598 }
31599 }
31600
31601 /* A prior declaration of TYPE_DECL has been seen. */
31602
31603 if (key_redundant)
31604 {
31605 gcc_rich_location richloc (key_loc);
31606 richloc.add_fixit_remove (key_loc);
31607 warning_at (&richloc, OPT_Wredundant_tags,
31608 "redundant class-key %qs in reference to %q#T",
31609 class_key == union_type ? "union"
31610 : class_key == record_type ? "struct" : "class",
31611 type);
31612 }
31613
31614 if (!exist)
31615 /* Do nothing if this is the first declaration of the type. */
31616 return;
31617
31618 if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
31619 /* Do nothing if the class-key in this declaration matches
31620 the definition. */
31621 return;
31622
31623 rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
31624 def_p);
31625 }
31626
31627 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
31628 of class decls or diagnoses it, whichever is appropriate. */
31629
31630 void
31631 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
31632 tag_types class_key,
31633 bool redundant,
31634 bool def_p)
31635 {
31636 /* Reset the CLASS_KEY associated with this type on mismatch.
31637 This is an optimization that lets the diagnostic code skip
31638 over classes that use the same class-key in all declarations. */
31639 if (def_class_key != class_key)
31640 def_class_key = none_type;
31641
31642 /* Set IDXDEF to the index of the vector corresponding to
31643 the definition. */
31644 if (def_p)
31645 idxdef = locvec.length ();
31646
31647 /* Append a record of this declaration to the vector. */
31648 class_key_loc_t ckl (current_function_decl, input_location, class_key,
31649 redundant);
31650 locvec.safe_push (ckl);
31651
31652 if (idxdef == UINT_MAX)
31653 return;
31654
31655 /* As a space optimization diagnose declarations of a class
31656 whose definition has been seen and purge the LOCVEC of
31657 all entries except the definition. */
31658 diag_mismatched_tags (type_decl);
31659 if (idxdef)
31660 {
31661 class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
31662 locvec.release ();
31663 locvec.reserve (2);
31664 locvec.safe_push (ent);
31665 idxdef = 0;
31666 }
31667 else
31668 /* Pop the entry pushed above for this declaration. */
31669 locvec.pop ();
31670 }
31671
31672 /* Issues -Wmismatched-tags for a single class. */
31673
31674 void
31675 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
31676 {
31677 if (!warn_mismatched_tags)
31678 return;
31679
31680 /* Number of uses of the class. */
31681 const unsigned ndecls = locvec.length ();
31682
31683 /* The class (or template) declaration guiding the decisions about
31684 the diagnostic. For ordinary classes it's the same as THIS. For
31685 uses of instantiations of templates other than their declarations
31686 it points to the record for the declaration of the corresponding
31687 primary template or partial specialization. */
31688 class_decl_loc_t *cdlguide = this;
31689
31690 tree type = TREE_TYPE (type_decl);
31691 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type))
31692 {
31693 /* For implicit instantiations of a primary template look up
31694 the primary or partial specialization and use it as
31695 the expected class-key rather than using the class-key of
31696 the first reference to the instantiation. The primary must
31697 be (and inevitably is) at index zero. */
31698 tree spec = specialization_of (type);
31699 cdlguide = class2loc.get (spec);
31700 gcc_assert (cdlguide != NULL);
31701 }
31702 else
31703 {
31704 /* Skip declarations that consistently use the same class-key. */
31705 if (def_class_key != none_type)
31706 return;
31707 }
31708
31709 /* Set if a definition for the class has been seen. */
31710 const bool def_p = cdlguide->def_p ();
31711
31712 /* The index of the declaration whose class-key this declaration
31713 is expected to match. It's either the class-key of the class
31714 definition if one exists or the first declaration otherwise. */
31715 const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
31716
31717 /* The class-key the class is expected to be declared with: it's
31718 either the key used in its definition or the first declaration
31719 if no definition has been provided.
31720 For implicit instantiations of a primary template it's
31721 the class-key used to declare the primary with. The primary
31722 must be at index zero. */
31723 const tag_types xpect_key = cdlguide->class_key (idxguide);
31724
31725 unsigned idx = 0;
31726 /* Advance IDX to the first declaration that either is not
31727 a definition or that doesn't match the first declaration
31728 if no definition is provided. */
31729 while (class_key (idx) == xpect_key)
31730 if (++idx == ndecls)
31731 return;
31732
31733 /* Save the current function before changing it below. */
31734 tree save_func = current_function_decl;
31735 /* Set the function declaration to print in diagnostic context. */
31736 current_function_decl = function (idx);
31737
31738 const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
31739 const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
31740
31741 location_t loc = location (idx);
31742 bool key_redundant_p = key_redundant (idx);
31743 auto_diagnostic_group d;
31744 /* Issue a warning for the first mismatched declaration.
31745 Avoid using "%#qT" since the class-key for the same type will
31746 be the same regardless of which one was used in the declaraion. */
31747 if (warning_at (loc, OPT_Wmismatched_tags,
31748 "%qT declared with a mismatched class-key %qs",
31749 type_decl, xmatchkstr))
31750 {
31751 /* Suggest how to avoid the warning for each instance since
31752 the guidance may be different depending on context. */
31753 inform (loc,
31754 (key_redundant_p
31755 ? G_("remove the class-key or replace it with %qs")
31756 : G_("replace the class-key with %qs")),
31757 xpectkstr);
31758
31759 /* Also point to the first declaration or definition that guided
31760 the decision to issue the warning above. */
31761 inform (cdlguide->location (idxguide),
31762 (def_p
31763 ? G_("%qT defined as %qs here")
31764 : G_("%qT first declared as %qs here")),
31765 type_decl, xpectkstr);
31766 }
31767
31768 /* Issue warnings for the remaining inconsistent declarations. */
31769 for (unsigned i = idx + 1; i != ndecls; ++i)
31770 {
31771 tag_types clskey = class_key (i);
31772 /* Skip over the declarations that match either the definition
31773 if one was provided or the first declaration. */
31774 if (clskey == xpect_key)
31775 continue;
31776
31777 loc = location (i);
31778 key_redundant_p = key_redundant (i);
31779 /* Set the function declaration to print in diagnostic context. */
31780 current_function_decl = function (i);
31781 if (warning_at (loc, OPT_Wmismatched_tags,
31782 "%qT declared with a mismatched class-key %qs",
31783 type_decl, xmatchkstr))
31784 /* Suggest how to avoid the warning for each instance since
31785 the guidance may be different depending on context. */
31786 inform (loc,
31787 (key_redundant_p
31788 ? G_("remove the class-key or replace it with %qs")
31789 : G_("replace the class-key with %qs")),
31790 xpectkstr);
31791 }
31792
31793 /* Restore the current function in case it was replaced above. */
31794 current_function_decl = save_func;
31795 }
31796
31797 /* Issues -Wmismatched-tags for all classes. Called at the end
31798 of processing a translation unit, after declarations of all class
31799 types and their uses have been recorded. */
31800
31801 void
31802 class_decl_loc_t::diag_mismatched_tags ()
31803 {
31804 /* CLASS2LOC should be empty if both -Wmismatched-tags and
31805 -Wredundant-tags are disabled. */
31806 gcc_assert (warn_mismatched_tags
31807 || warn_redundant_tags
31808 || class2loc.is_empty ());
31809
31810 /* Save the current function before changing on return. It should
31811 be null at this point. */
31812 temp_override<tree> cleanup (current_function_decl);
31813
31814 if (warn_mismatched_tags)
31815 {
31816 /* Iterate over the collected class/struct/template declarations. */
31817 typedef class_to_loc_map_t::iterator iter_t;
31818 for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
31819 {
31820 tree type_decl = (*it).first;
31821 class_decl_loc_t &recloc = (*it).second;
31822 recloc.diag_mismatched_tags (type_decl);
31823 }
31824 }
31825
31826 class2loc.empty ();
31827 }
31828
31829 /* Issue an error message if DECL is redeclared with different
31830 access than its original declaration [class.access.spec/3].
31831 This applies to nested classes, nested class templates and
31832 enumerations [class.mem/1]. */
31833
31834 static void
31835 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
31836 {
31837 if (!decl
31838 || (!CLASS_TYPE_P (TREE_TYPE (decl))
31839 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
31840 return;
31841
31842 if ((TREE_PRIVATE (decl)
31843 != (current_access_specifier == access_private_node))
31844 || (TREE_PROTECTED (decl)
31845 != (current_access_specifier == access_protected_node)))
31846 error_at (location, "%qD redeclared with different access", decl);
31847 }
31848
31849 /* Look for the `template' keyword, as a syntactic disambiguator.
31850 Return TRUE iff it is present, in which case it will be
31851 consumed. */
31852
31853 static bool
31854 cp_parser_optional_template_keyword (cp_parser *parser)
31855 {
31856 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
31857 {
31858 /* In C++98 the `template' keyword can only be used within templates;
31859 outside templates the parser can always figure out what is a
31860 template and what is not. In C++11, per the resolution of DR 468,
31861 `template' is allowed in cases where it is not strictly necessary. */
31862 if (!processing_template_decl
31863 && pedantic && cxx_dialect == cxx98)
31864 {
31865 cp_token *token = cp_lexer_peek_token (parser->lexer);
31866 pedwarn (token->location, OPT_Wpedantic,
31867 "in C++98 %<template%> (as a disambiguator) is only "
31868 "allowed within templates");
31869 /* If this part of the token stream is rescanned, the same
31870 error message would be generated. So, we purge the token
31871 from the stream. */
31872 cp_lexer_purge_token (parser->lexer);
31873 return false;
31874 }
31875 else
31876 {
31877 /* Consume the `template' keyword. */
31878 cp_lexer_consume_token (parser->lexer);
31879 return true;
31880 }
31881 }
31882 return false;
31883 }
31884
31885 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
31886 set PARSER->SCOPE, and perform other related actions. */
31887
31888 static void
31889 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
31890 {
31891 struct tree_check *check_value;
31892
31893 /* Get the stored value. */
31894 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
31895 /* Set the scope from the stored value. */
31896 parser->scope = saved_checks_value (check_value);
31897 parser->qualifying_scope = check_value->qualifying_scope;
31898 parser->object_scope = NULL_TREE;
31899 }
31900
31901 /* Consume tokens up through a non-nested END token. Returns TRUE if we
31902 encounter the end of a block before what we were looking for. */
31903
31904 static bool
31905 cp_parser_cache_group (cp_parser *parser,
31906 enum cpp_ttype end,
31907 unsigned depth)
31908 {
31909 while (true)
31910 {
31911 cp_token *token = cp_lexer_peek_token (parser->lexer);
31912
31913 /* Abort a parenthesized expression if we encounter a semicolon. */
31914 if ((end == CPP_CLOSE_PAREN || depth == 0)
31915 && token->type == CPP_SEMICOLON)
31916 return true;
31917 /* If we've reached the end of the file, stop. */
31918 if (token->type == CPP_EOF
31919 || (end != CPP_PRAGMA_EOL
31920 && token->type == CPP_PRAGMA_EOL))
31921 return true;
31922 if (token->type == CPP_CLOSE_BRACE && depth == 0)
31923 /* We've hit the end of an enclosing block, so there's been some
31924 kind of syntax error. */
31925 return true;
31926
31927 /* Consume the token. */
31928 cp_lexer_consume_token (parser->lexer);
31929 /* See if it starts a new group. */
31930 if (token->type == CPP_OPEN_BRACE)
31931 {
31932 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
31933 /* In theory this should probably check end == '}', but
31934 cp_parser_save_member_function_body needs it to exit
31935 after either '}' or ')' when called with ')'. */
31936 if (depth == 0)
31937 return false;
31938 }
31939 else if (token->type == CPP_OPEN_PAREN)
31940 {
31941 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
31942 if (depth == 0 && end == CPP_CLOSE_PAREN)
31943 return false;
31944 }
31945 else if (token->type == CPP_PRAGMA)
31946 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
31947 else if (token->type == end)
31948 return false;
31949 }
31950 }
31951
31952 /* Like above, for caching a default argument or NSDMI. Both of these are
31953 terminated by a non-nested comma, but it can be unclear whether or not a
31954 comma is nested in a template argument list unless we do more parsing.
31955 In order to handle this ambiguity, when we encounter a ',' after a '<'
31956 we try to parse what follows as a parameter-declaration-list (in the
31957 case of a default argument) or a member-declarator (in the case of an
31958 NSDMI). If that succeeds, then we stop caching. */
31959
31960 static tree
31961 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
31962 {
31963 unsigned depth = 0;
31964 int maybe_template_id = 0;
31965 cp_token *first_token;
31966 cp_token *token;
31967 tree default_argument;
31968
31969 /* Add tokens until we have processed the entire default
31970 argument. We add the range [first_token, token). */
31971 first_token = cp_lexer_peek_token (parser->lexer);
31972 if (first_token->type == CPP_OPEN_BRACE)
31973 {
31974 /* For list-initialization, this is straightforward. */
31975 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
31976 token = cp_lexer_peek_token (parser->lexer);
31977 }
31978 else while (true)
31979 {
31980 bool done = false;
31981
31982 /* Peek at the next token. */
31983 token = cp_lexer_peek_token (parser->lexer);
31984 /* What we do depends on what token we have. */
31985 switch (token->type)
31986 {
31987 /* In valid code, a default argument must be
31988 immediately followed by a `,' `)', or `...'. */
31989 case CPP_COMMA:
31990 if (depth == 0 && maybe_template_id)
31991 {
31992 /* If we've seen a '<', we might be in a
31993 template-argument-list. Until Core issue 325 is
31994 resolved, we don't know how this situation ought
31995 to be handled, so try to DTRT. We check whether
31996 what comes after the comma is a valid parameter
31997 declaration list. If it is, then the comma ends
31998 the default argument; otherwise the default
31999 argument continues. */
32000 bool error = false;
32001 cp_token *peek;
32002
32003 /* Set ITALP so cp_parser_parameter_declaration_list
32004 doesn't decide to commit to this parse. */
32005 bool saved_italp = parser->in_template_argument_list_p;
32006 parser->in_template_argument_list_p = true;
32007
32008 cp_parser_parse_tentatively (parser);
32009
32010 if (nsdmi)
32011 {
32012 /* Parse declarators until we reach a non-comma or
32013 somthing that cannot be an initializer.
32014 Just checking whether we're looking at a single
32015 declarator is insufficient. Consider:
32016 int var = tuple<T,U>::x;
32017 The template parameter 'U' looks exactly like a
32018 declarator. */
32019 do
32020 {
32021 int ctor_dtor_or_conv_p;
32022 cp_lexer_consume_token (parser->lexer);
32023 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
32024 CP_PARSER_FLAGS_NONE,
32025 &ctor_dtor_or_conv_p,
32026 /*parenthesized_p=*/NULL,
32027 /*member_p=*/true,
32028 /*friend_p=*/false,
32029 /*static_p=*/false);
32030 peek = cp_lexer_peek_token (parser->lexer);
32031 if (cp_parser_error_occurred (parser))
32032 break;
32033 }
32034 while (peek->type == CPP_COMMA);
32035 /* If we met an '=' or ';' then the original comma
32036 was the end of the NSDMI. Otherwise assume
32037 we're still in the NSDMI. */
32038 error = (peek->type != CPP_EQ
32039 && peek->type != CPP_SEMICOLON);
32040 }
32041 else
32042 {
32043 cp_lexer_consume_token (parser->lexer);
32044 begin_scope (sk_function_parms, NULL_TREE);
32045 tree t = cp_parser_parameter_declaration_list
32046 (parser, CP_PARSER_FLAGS_NONE);
32047 if (t == error_mark_node)
32048 error = true;
32049 pop_bindings_and_leave_scope ();
32050 }
32051 if (!cp_parser_error_occurred (parser) && !error)
32052 done = true;
32053 cp_parser_abort_tentative_parse (parser);
32054
32055 parser->in_template_argument_list_p = saved_italp;
32056 break;
32057 }
32058 /* FALLTHRU */
32059 case CPP_CLOSE_PAREN:
32060 case CPP_ELLIPSIS:
32061 /* If we run into a non-nested `;', `}', or `]',
32062 then the code is invalid -- but the default
32063 argument is certainly over. */
32064 case CPP_SEMICOLON:
32065 case CPP_CLOSE_BRACE:
32066 case CPP_CLOSE_SQUARE:
32067 if (depth == 0
32068 /* Handle correctly int n = sizeof ... ( p ); */
32069 && token->type != CPP_ELLIPSIS)
32070 done = true;
32071 /* Update DEPTH, if necessary. */
32072 else if (token->type == CPP_CLOSE_PAREN
32073 || token->type == CPP_CLOSE_BRACE
32074 || token->type == CPP_CLOSE_SQUARE)
32075 --depth;
32076 break;
32077
32078 case CPP_OPEN_PAREN:
32079 case CPP_OPEN_SQUARE:
32080 case CPP_OPEN_BRACE:
32081 ++depth;
32082 break;
32083
32084 case CPP_LESS:
32085 if (depth == 0)
32086 /* This might be the comparison operator, or it might
32087 start a template argument list. */
32088 ++maybe_template_id;
32089 break;
32090
32091 case CPP_RSHIFT:
32092 if (cxx_dialect == cxx98)
32093 break;
32094 /* Fall through for C++0x, which treats the `>>'
32095 operator like two `>' tokens in certain
32096 cases. */
32097 gcc_fallthrough ();
32098
32099 case CPP_GREATER:
32100 if (depth == 0)
32101 {
32102 /* This might be an operator, or it might close a
32103 template argument list. But if a previous '<'
32104 started a template argument list, this will have
32105 closed it, so we can't be in one anymore. */
32106 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
32107 if (maybe_template_id < 0)
32108 maybe_template_id = 0;
32109 }
32110 break;
32111
32112 /* If we run out of tokens, issue an error message. */
32113 case CPP_EOF:
32114 case CPP_PRAGMA_EOL:
32115 error_at (token->location, "file ends in default argument");
32116 return error_mark_node;
32117
32118 case CPP_NAME:
32119 case CPP_SCOPE:
32120 /* In these cases, we should look for template-ids.
32121 For example, if the default argument is
32122 `X<int, double>()', we need to do name lookup to
32123 figure out whether or not `X' is a template; if
32124 so, the `,' does not end the default argument.
32125
32126 That is not yet done. */
32127 break;
32128
32129 default:
32130 break;
32131 }
32132
32133 /* If we've reached the end, stop. */
32134 if (done)
32135 break;
32136
32137 /* Add the token to the token block. */
32138 token = cp_lexer_consume_token (parser->lexer);
32139 }
32140
32141 /* Create a DEFERRED_PARSE to represent the unparsed default
32142 argument. */
32143 default_argument = make_node (DEFERRED_PARSE);
32144 DEFPARSE_TOKENS (default_argument)
32145 = cp_token_cache_new (first_token, token);
32146 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
32147
32148 return default_argument;
32149 }
32150
32151 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
32152
32153 location_t
32154 defparse_location (tree default_argument)
32155 {
32156 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
32157 location_t start = tokens->first->location;
32158 location_t end = tokens->last->location;
32159 return make_location (start, start, end);
32160 }
32161
32162 /* Begin parsing tentatively. We always save tokens while parsing
32163 tentatively so that if the tentative parsing fails we can restore the
32164 tokens. */
32165
32166 static void
32167 cp_parser_parse_tentatively (cp_parser* parser)
32168 {
32169 /* Enter a new parsing context. */
32170 parser->context = cp_parser_context_new (parser->context);
32171 /* Begin saving tokens. */
32172 cp_lexer_save_tokens (parser->lexer);
32173 /* In order to avoid repetitive access control error messages,
32174 access checks are queued up until we are no longer parsing
32175 tentatively. */
32176 push_deferring_access_checks (dk_deferred);
32177 }
32178
32179 /* Commit to the currently active tentative parse. */
32180
32181 static void
32182 cp_parser_commit_to_tentative_parse (cp_parser* parser)
32183 {
32184 cp_parser_context *context;
32185 cp_lexer *lexer;
32186
32187 /* Mark all of the levels as committed. */
32188 lexer = parser->lexer;
32189 for (context = parser->context; context->next; context = context->next)
32190 {
32191 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
32192 break;
32193 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
32194 while (!cp_lexer_saving_tokens (lexer))
32195 lexer = lexer->next;
32196 cp_lexer_commit_tokens (lexer);
32197 }
32198 }
32199
32200 /* Commit to the topmost currently active tentative parse.
32201
32202 Note that this function shouldn't be called when there are
32203 irreversible side-effects while in a tentative state. For
32204 example, we shouldn't create a permanent entry in the symbol
32205 table, or issue an error message that might not apply if the
32206 tentative parse is aborted. */
32207
32208 static void
32209 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
32210 {
32211 cp_parser_context *context = parser->context;
32212 cp_lexer *lexer = parser->lexer;
32213
32214 if (context)
32215 {
32216 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
32217 return;
32218 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
32219
32220 while (!cp_lexer_saving_tokens (lexer))
32221 lexer = lexer->next;
32222 cp_lexer_commit_tokens (lexer);
32223 }
32224 }
32225
32226 /* Abort the currently active tentative parse. All consumed tokens
32227 will be rolled back, and no diagnostics will be issued. */
32228
32229 static void
32230 cp_parser_abort_tentative_parse (cp_parser* parser)
32231 {
32232 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
32233 || errorcount > 0);
32234 cp_parser_simulate_error (parser);
32235 /* Now, pretend that we want to see if the construct was
32236 successfully parsed. */
32237 cp_parser_parse_definitely (parser);
32238 }
32239
32240 /* Stop parsing tentatively. If a parse error has occurred, restore the
32241 token stream. Otherwise, commit to the tokens we have consumed.
32242 Returns true if no error occurred; false otherwise. */
32243
32244 static bool
32245 cp_parser_parse_definitely (cp_parser* parser)
32246 {
32247 bool error_occurred;
32248 cp_parser_context *context;
32249
32250 /* Remember whether or not an error occurred, since we are about to
32251 destroy that information. */
32252 error_occurred = cp_parser_error_occurred (parser);
32253 /* Remove the topmost context from the stack. */
32254 context = parser->context;
32255 parser->context = context->next;
32256 /* If no parse errors occurred, commit to the tentative parse. */
32257 if (!error_occurred)
32258 {
32259 /* Commit to the tokens read tentatively, unless that was
32260 already done. */
32261 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
32262 cp_lexer_commit_tokens (parser->lexer);
32263
32264 pop_to_parent_deferring_access_checks ();
32265 }
32266 /* Otherwise, if errors occurred, roll back our state so that things
32267 are just as they were before we began the tentative parse. */
32268 else
32269 {
32270 cp_lexer_rollback_tokens (parser->lexer);
32271 pop_deferring_access_checks ();
32272 }
32273 /* Add the context to the front of the free list. */
32274 context->next = cp_parser_context_free_list;
32275 cp_parser_context_free_list = context;
32276
32277 return !error_occurred;
32278 }
32279
32280 /* Returns true if we are parsing tentatively and are not committed to
32281 this tentative parse. */
32282
32283 static bool
32284 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
32285 {
32286 return (cp_parser_parsing_tentatively (parser)
32287 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
32288 }
32289
32290 /* Returns nonzero iff an error has occurred during the most recent
32291 tentative parse. */
32292
32293 static bool
32294 cp_parser_error_occurred (cp_parser* parser)
32295 {
32296 return (cp_parser_parsing_tentatively (parser)
32297 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
32298 }
32299
32300 /* Returns nonzero if GNU extensions are allowed. */
32301
32302 static bool
32303 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
32304 {
32305 return parser->allow_gnu_extensions_p;
32306 }
32307 \f
32308 /* Objective-C++ Productions */
32309
32310
32311 /* Parse an Objective-C expression, which feeds into a primary-expression
32312 above.
32313
32314 objc-expression:
32315 objc-message-expression
32316 objc-string-literal
32317 objc-encode-expression
32318 objc-protocol-expression
32319 objc-selector-expression
32320
32321 Returns a tree representation of the expression. */
32322
32323 static cp_expr
32324 cp_parser_objc_expression (cp_parser* parser)
32325 {
32326 /* Try to figure out what kind of declaration is present. */
32327 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
32328
32329 switch (kwd->type)
32330 {
32331 case CPP_OPEN_SQUARE:
32332 return cp_parser_objc_message_expression (parser);
32333
32334 case CPP_OBJC_STRING:
32335 kwd = cp_lexer_consume_token (parser->lexer);
32336 return objc_build_string_object (kwd->u.value);
32337
32338 case CPP_KEYWORD:
32339 switch (kwd->keyword)
32340 {
32341 case RID_AT_ENCODE:
32342 return cp_parser_objc_encode_expression (parser);
32343
32344 case RID_AT_PROTOCOL:
32345 return cp_parser_objc_protocol_expression (parser);
32346
32347 case RID_AT_SELECTOR:
32348 return cp_parser_objc_selector_expression (parser);
32349
32350 default:
32351 break;
32352 }
32353 /* FALLTHRU */
32354 default:
32355 error_at (kwd->location,
32356 "misplaced %<@%D%> Objective-C++ construct",
32357 kwd->u.value);
32358 cp_parser_skip_to_end_of_block_or_statement (parser);
32359 }
32360
32361 return error_mark_node;
32362 }
32363
32364 /* Parse an Objective-C message expression.
32365
32366 objc-message-expression:
32367 [ objc-message-receiver objc-message-args ]
32368
32369 Returns a representation of an Objective-C message. */
32370
32371 static tree
32372 cp_parser_objc_message_expression (cp_parser* parser)
32373 {
32374 tree receiver, messageargs;
32375
32376 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
32377 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
32378 receiver = cp_parser_objc_message_receiver (parser);
32379 messageargs = cp_parser_objc_message_args (parser);
32380 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
32381 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32382
32383 tree result = objc_build_message_expr (receiver, messageargs);
32384
32385 /* Construct a location e.g.
32386 [self func1:5]
32387 ^~~~~~~~~~~~~~
32388 ranging from the '[' to the ']', with the caret at the start. */
32389 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
32390 protected_set_expr_location (result, combined_loc);
32391
32392 return result;
32393 }
32394
32395 /* Parse an objc-message-receiver.
32396
32397 objc-message-receiver:
32398 expression
32399 simple-type-specifier
32400
32401 Returns a representation of the type or expression. */
32402
32403 static tree
32404 cp_parser_objc_message_receiver (cp_parser* parser)
32405 {
32406 tree rcv;
32407
32408 /* An Objective-C message receiver may be either (1) a type
32409 or (2) an expression. */
32410 cp_parser_parse_tentatively (parser);
32411 rcv = cp_parser_expression (parser);
32412
32413 /* If that worked out, fine. */
32414 if (cp_parser_parse_definitely (parser))
32415 return rcv;
32416
32417 cp_parser_parse_tentatively (parser);
32418 rcv = cp_parser_simple_type_specifier (parser,
32419 /*decl_specs=*/NULL,
32420 CP_PARSER_FLAGS_NONE);
32421
32422 if (cp_parser_parse_definitely (parser))
32423 return objc_get_class_reference (rcv);
32424
32425 cp_parser_error (parser, "objective-c++ message receiver expected");
32426 return error_mark_node;
32427 }
32428
32429 /* Parse the arguments and selectors comprising an Objective-C message.
32430
32431 objc-message-args:
32432 objc-selector
32433 objc-selector-args
32434 objc-selector-args , objc-comma-args
32435
32436 objc-selector-args:
32437 objc-selector [opt] : assignment-expression
32438 objc-selector-args objc-selector [opt] : assignment-expression
32439
32440 objc-comma-args:
32441 assignment-expression
32442 objc-comma-args , assignment-expression
32443
32444 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
32445 selector arguments and TREE_VALUE containing a list of comma
32446 arguments. */
32447
32448 static tree
32449 cp_parser_objc_message_args (cp_parser* parser)
32450 {
32451 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
32452 bool maybe_unary_selector_p = true;
32453 cp_token *token = cp_lexer_peek_token (parser->lexer);
32454
32455 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
32456 {
32457 tree selector = NULL_TREE, arg;
32458
32459 if (token->type != CPP_COLON)
32460 selector = cp_parser_objc_selector (parser);
32461
32462 /* Detect if we have a unary selector. */
32463 if (maybe_unary_selector_p
32464 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
32465 return build_tree_list (selector, NULL_TREE);
32466
32467 maybe_unary_selector_p = false;
32468 cp_parser_require (parser, CPP_COLON, RT_COLON);
32469 arg = cp_parser_assignment_expression (parser);
32470
32471 sel_args
32472 = chainon (sel_args,
32473 build_tree_list (selector, arg));
32474
32475 token = cp_lexer_peek_token (parser->lexer);
32476 }
32477
32478 /* Handle non-selector arguments, if any. */
32479 while (token->type == CPP_COMMA)
32480 {
32481 tree arg;
32482
32483 cp_lexer_consume_token (parser->lexer);
32484 arg = cp_parser_assignment_expression (parser);
32485
32486 addl_args
32487 = chainon (addl_args,
32488 build_tree_list (NULL_TREE, arg));
32489
32490 token = cp_lexer_peek_token (parser->lexer);
32491 }
32492
32493 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
32494 {
32495 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
32496 return build_tree_list (error_mark_node, error_mark_node);
32497 }
32498
32499 return build_tree_list (sel_args, addl_args);
32500 }
32501
32502 /* Parse an Objective-C encode expression.
32503
32504 objc-encode-expression:
32505 @encode objc-typename
32506
32507 Returns an encoded representation of the type argument. */
32508
32509 static cp_expr
32510 cp_parser_objc_encode_expression (cp_parser* parser)
32511 {
32512 tree type;
32513 cp_token *token;
32514 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
32515
32516 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
32517 matching_parens parens;
32518 parens.require_open (parser);
32519 token = cp_lexer_peek_token (parser->lexer);
32520 type = complete_type (cp_parser_type_id (parser));
32521 parens.require_close (parser);
32522
32523 if (!type)
32524 {
32525 error_at (token->location,
32526 "%<@encode%> must specify a type as an argument");
32527 return error_mark_node;
32528 }
32529
32530 /* This happens if we find @encode(T) (where T is a template
32531 typename or something dependent on a template typename) when
32532 parsing a template. In that case, we can't compile it
32533 immediately, but we rather create an AT_ENCODE_EXPR which will
32534 need to be instantiated when the template is used.
32535 */
32536 if (dependent_type_p (type))
32537 {
32538 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
32539 TREE_READONLY (value) = 1;
32540 return value;
32541 }
32542
32543
32544 /* Build a location of the form:
32545 @encode(int)
32546 ^~~~~~~~~~~~
32547 with caret==start at the @ token, finishing at the close paren. */
32548 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
32549
32550 return cp_expr (objc_build_encode_expr (type), combined_loc);
32551 }
32552
32553 /* Parse an Objective-C @defs expression. */
32554
32555 static tree
32556 cp_parser_objc_defs_expression (cp_parser *parser)
32557 {
32558 tree name;
32559
32560 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
32561 matching_parens parens;
32562 parens.require_open (parser);
32563 name = cp_parser_identifier (parser);
32564 parens.require_close (parser);
32565
32566 return objc_get_class_ivars (name);
32567 }
32568
32569 /* Parse an Objective-C protocol expression.
32570
32571 objc-protocol-expression:
32572 @protocol ( identifier )
32573
32574 Returns a representation of the protocol expression. */
32575
32576 static tree
32577 cp_parser_objc_protocol_expression (cp_parser* parser)
32578 {
32579 tree proto;
32580 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
32581
32582 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
32583 matching_parens parens;
32584 parens.require_open (parser);
32585 proto = cp_parser_identifier (parser);
32586 parens.require_close (parser);
32587
32588 /* Build a location of the form:
32589 @protocol(prot)
32590 ^~~~~~~~~~~~~~~
32591 with caret==start at the @ token, finishing at the close paren. */
32592 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
32593 tree result = objc_build_protocol_expr (proto);
32594 protected_set_expr_location (result, combined_loc);
32595 return result;
32596 }
32597
32598 /* Parse an Objective-C selector expression.
32599
32600 objc-selector-expression:
32601 @selector ( objc-method-signature )
32602
32603 objc-method-signature:
32604 objc-selector
32605 objc-selector-seq
32606
32607 objc-selector-seq:
32608 objc-selector :
32609 objc-selector-seq objc-selector :
32610
32611 Returns a representation of the method selector. */
32612
32613 static tree
32614 cp_parser_objc_selector_expression (cp_parser* parser)
32615 {
32616 tree sel_seq = NULL_TREE;
32617 bool maybe_unary_selector_p = true;
32618 cp_token *token;
32619 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32620
32621 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
32622 matching_parens parens;
32623 parens.require_open (parser);
32624 token = cp_lexer_peek_token (parser->lexer);
32625
32626 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
32627 || token->type == CPP_SCOPE)
32628 {
32629 tree selector = NULL_TREE;
32630
32631 if (token->type != CPP_COLON
32632 || token->type == CPP_SCOPE)
32633 selector = cp_parser_objc_selector (parser);
32634
32635 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
32636 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
32637 {
32638 /* Detect if we have a unary selector. */
32639 if (maybe_unary_selector_p)
32640 {
32641 sel_seq = selector;
32642 goto finish_selector;
32643 }
32644 else
32645 {
32646 cp_parser_error (parser, "expected %<:%>");
32647 }
32648 }
32649 maybe_unary_selector_p = false;
32650 token = cp_lexer_consume_token (parser->lexer);
32651
32652 if (token->type == CPP_SCOPE)
32653 {
32654 sel_seq
32655 = chainon (sel_seq,
32656 build_tree_list (selector, NULL_TREE));
32657 sel_seq
32658 = chainon (sel_seq,
32659 build_tree_list (NULL_TREE, NULL_TREE));
32660 }
32661 else
32662 sel_seq
32663 = chainon (sel_seq,
32664 build_tree_list (selector, NULL_TREE));
32665
32666 token = cp_lexer_peek_token (parser->lexer);
32667 }
32668
32669 finish_selector:
32670 parens.require_close (parser);
32671
32672
32673 /* Build a location of the form:
32674 @selector(func)
32675 ^~~~~~~~~~~~~~~
32676 with caret==start at the @ token, finishing at the close paren. */
32677 location_t combined_loc = make_location (loc, loc, parser->lexer);
32678 tree result = objc_build_selector_expr (combined_loc, sel_seq);
32679 /* TODO: objc_build_selector_expr doesn't always honor the location. */
32680 protected_set_expr_location (result, combined_loc);
32681 return result;
32682 }
32683
32684 /* Parse a list of identifiers.
32685
32686 objc-identifier-list:
32687 identifier
32688 objc-identifier-list , identifier
32689
32690 Returns a TREE_LIST of identifier nodes. */
32691
32692 static tree
32693 cp_parser_objc_identifier_list (cp_parser* parser)
32694 {
32695 tree identifier;
32696 tree list;
32697 cp_token *sep;
32698
32699 identifier = cp_parser_identifier (parser);
32700 if (identifier == error_mark_node)
32701 return error_mark_node;
32702
32703 list = build_tree_list (NULL_TREE, identifier);
32704 sep = cp_lexer_peek_token (parser->lexer);
32705
32706 while (sep->type == CPP_COMMA)
32707 {
32708 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
32709 identifier = cp_parser_identifier (parser);
32710 if (identifier == error_mark_node)
32711 return list;
32712
32713 list = chainon (list, build_tree_list (NULL_TREE,
32714 identifier));
32715 sep = cp_lexer_peek_token (parser->lexer);
32716 }
32717
32718 return list;
32719 }
32720
32721 /* Parse an Objective-C alias declaration.
32722
32723 objc-alias-declaration:
32724 @compatibility_alias identifier identifier ;
32725
32726 This function registers the alias mapping with the Objective-C front end.
32727 It returns nothing. */
32728
32729 static void
32730 cp_parser_objc_alias_declaration (cp_parser* parser)
32731 {
32732 tree alias, orig;
32733
32734 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
32735 alias = cp_parser_identifier (parser);
32736 orig = cp_parser_identifier (parser);
32737 objc_declare_alias (alias, orig);
32738 cp_parser_consume_semicolon_at_end_of_statement (parser);
32739 }
32740
32741 /* Parse an Objective-C class forward-declaration.
32742
32743 objc-class-declaration:
32744 @class objc-identifier-list ;
32745
32746 The function registers the forward declarations with the Objective-C
32747 front end. It returns nothing. */
32748
32749 static void
32750 cp_parser_objc_class_declaration (cp_parser* parser)
32751 {
32752 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
32753 while (true)
32754 {
32755 tree id;
32756
32757 id = cp_parser_identifier (parser);
32758 if (id == error_mark_node)
32759 break;
32760
32761 objc_declare_class (id);
32762
32763 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32764 cp_lexer_consume_token (parser->lexer);
32765 else
32766 break;
32767 }
32768 cp_parser_consume_semicolon_at_end_of_statement (parser);
32769 }
32770
32771 /* Parse a list of Objective-C protocol references.
32772
32773 objc-protocol-refs-opt:
32774 objc-protocol-refs [opt]
32775
32776 objc-protocol-refs:
32777 < objc-identifier-list >
32778
32779 Returns a TREE_LIST of identifiers, if any. */
32780
32781 static tree
32782 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
32783 {
32784 tree protorefs = NULL_TREE;
32785
32786 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
32787 {
32788 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
32789 protorefs = cp_parser_objc_identifier_list (parser);
32790 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
32791 }
32792
32793 return protorefs;
32794 }
32795
32796 /* Parse a Objective-C visibility specification. */
32797
32798 static void
32799 cp_parser_objc_visibility_spec (cp_parser* parser)
32800 {
32801 cp_token *vis = cp_lexer_peek_token (parser->lexer);
32802
32803 switch (vis->keyword)
32804 {
32805 case RID_AT_PRIVATE:
32806 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
32807 break;
32808 case RID_AT_PROTECTED:
32809 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
32810 break;
32811 case RID_AT_PUBLIC:
32812 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
32813 break;
32814 case RID_AT_PACKAGE:
32815 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
32816 break;
32817 default:
32818 return;
32819 }
32820
32821 /* Eat '@private'/'@protected'/'@public'. */
32822 cp_lexer_consume_token (parser->lexer);
32823 }
32824
32825 /* Parse an Objective-C method type. Return 'true' if it is a class
32826 (+) method, and 'false' if it is an instance (-) method. */
32827
32828 static inline bool
32829 cp_parser_objc_method_type (cp_parser* parser)
32830 {
32831 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
32832 return true;
32833 else
32834 return false;
32835 }
32836
32837 /* Parse an Objective-C protocol qualifier. */
32838
32839 static tree
32840 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
32841 {
32842 tree quals = NULL_TREE, node;
32843 cp_token *token = cp_lexer_peek_token (parser->lexer);
32844
32845 node = token->u.value;
32846
32847 while (node && identifier_p (node)
32848 && (node == ridpointers [(int) RID_IN]
32849 || node == ridpointers [(int) RID_OUT]
32850 || node == ridpointers [(int) RID_INOUT]
32851 || node == ridpointers [(int) RID_BYCOPY]
32852 || node == ridpointers [(int) RID_BYREF]
32853 || node == ridpointers [(int) RID_ONEWAY]))
32854 {
32855 quals = tree_cons (NULL_TREE, node, quals);
32856 cp_lexer_consume_token (parser->lexer);
32857 token = cp_lexer_peek_token (parser->lexer);
32858 node = token->u.value;
32859 }
32860
32861 return quals;
32862 }
32863
32864 /* Parse an Objective-C typename. */
32865
32866 static tree
32867 cp_parser_objc_typename (cp_parser* parser)
32868 {
32869 tree type_name = NULL_TREE;
32870
32871 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32872 {
32873 tree proto_quals, cp_type = NULL_TREE;
32874
32875 matching_parens parens;
32876 parens.consume_open (parser); /* Eat '('. */
32877 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
32878
32879 /* An ObjC type name may consist of just protocol qualifiers, in which
32880 case the type shall default to 'id'. */
32881 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
32882 {
32883 cp_type = cp_parser_type_id (parser);
32884
32885 /* If the type could not be parsed, an error has already
32886 been produced. For error recovery, behave as if it had
32887 not been specified, which will use the default type
32888 'id'. */
32889 if (cp_type == error_mark_node)
32890 {
32891 cp_type = NULL_TREE;
32892 /* We need to skip to the closing parenthesis as
32893 cp_parser_type_id() does not seem to do it for
32894 us. */
32895 cp_parser_skip_to_closing_parenthesis (parser,
32896 /*recovering=*/true,
32897 /*or_comma=*/false,
32898 /*consume_paren=*/false);
32899 }
32900 }
32901
32902 parens.require_close (parser);
32903 type_name = build_tree_list (proto_quals, cp_type);
32904 }
32905
32906 return type_name;
32907 }
32908
32909 /* Check to see if TYPE refers to an Objective-C selector name. */
32910
32911 static bool
32912 cp_parser_objc_selector_p (enum cpp_ttype type)
32913 {
32914 return (type == CPP_NAME || type == CPP_KEYWORD
32915 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
32916 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
32917 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
32918 || type == CPP_XOR || type == CPP_XOR_EQ);
32919 }
32920
32921 /* Parse an Objective-C selector. */
32922
32923 static tree
32924 cp_parser_objc_selector (cp_parser* parser)
32925 {
32926 cp_token *token = cp_lexer_consume_token (parser->lexer);
32927
32928 if (!cp_parser_objc_selector_p (token->type))
32929 {
32930 error_at (token->location, "invalid Objective-C++ selector name");
32931 return error_mark_node;
32932 }
32933
32934 /* C++ operator names are allowed to appear in ObjC selectors. */
32935 switch (token->type)
32936 {
32937 case CPP_AND_AND: return get_identifier ("and");
32938 case CPP_AND_EQ: return get_identifier ("and_eq");
32939 case CPP_AND: return get_identifier ("bitand");
32940 case CPP_OR: return get_identifier ("bitor");
32941 case CPP_COMPL: return get_identifier ("compl");
32942 case CPP_NOT: return get_identifier ("not");
32943 case CPP_NOT_EQ: return get_identifier ("not_eq");
32944 case CPP_OR_OR: return get_identifier ("or");
32945 case CPP_OR_EQ: return get_identifier ("or_eq");
32946 case CPP_XOR: return get_identifier ("xor");
32947 case CPP_XOR_EQ: return get_identifier ("xor_eq");
32948 default: return token->u.value;
32949 }
32950 }
32951
32952 /* Parse an Objective-C params list. */
32953
32954 static tree
32955 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
32956 {
32957 tree params = NULL_TREE;
32958 bool maybe_unary_selector_p = true;
32959 cp_token *token = cp_lexer_peek_token (parser->lexer);
32960
32961 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
32962 {
32963 tree selector = NULL_TREE, type_name, identifier;
32964 tree parm_attr = NULL_TREE;
32965
32966 if (token->keyword == RID_ATTRIBUTE)
32967 break;
32968
32969 if (token->type != CPP_COLON)
32970 selector = cp_parser_objc_selector (parser);
32971
32972 /* Detect if we have a unary selector. */
32973 if (maybe_unary_selector_p
32974 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
32975 {
32976 params = selector; /* Might be followed by attributes. */
32977 break;
32978 }
32979
32980 maybe_unary_selector_p = false;
32981 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32982 {
32983 /* Something went quite wrong. There should be a colon
32984 here, but there is not. Stop parsing parameters. */
32985 break;
32986 }
32987 type_name = cp_parser_objc_typename (parser);
32988 /* New ObjC allows attributes on parameters too. */
32989 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
32990 parm_attr = cp_parser_attributes_opt (parser);
32991 identifier = cp_parser_identifier (parser);
32992
32993 params
32994 = chainon (params,
32995 objc_build_keyword_decl (selector,
32996 type_name,
32997 identifier,
32998 parm_attr));
32999
33000 token = cp_lexer_peek_token (parser->lexer);
33001 }
33002
33003 if (params == NULL_TREE)
33004 {
33005 cp_parser_error (parser, "objective-c++ method declaration is expected");
33006 return error_mark_node;
33007 }
33008
33009 /* We allow tail attributes for the method. */
33010 if (token->keyword == RID_ATTRIBUTE)
33011 {
33012 *attributes = cp_parser_attributes_opt (parser);
33013 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
33014 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33015 return params;
33016 cp_parser_error (parser,
33017 "method attributes must be specified at the end");
33018 return error_mark_node;
33019 }
33020
33021 if (params == NULL_TREE)
33022 {
33023 cp_parser_error (parser, "objective-c++ method declaration is expected");
33024 return error_mark_node;
33025 }
33026 return params;
33027 }
33028
33029 /* Parse the non-keyword Objective-C params. */
33030
33031 static tree
33032 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
33033 tree* attributes)
33034 {
33035 tree params = make_node (TREE_LIST);
33036 cp_token *token = cp_lexer_peek_token (parser->lexer);
33037 *ellipsisp = false; /* Initially, assume no ellipsis. */
33038
33039 while (token->type == CPP_COMMA)
33040 {
33041 cp_parameter_declarator *parmdecl;
33042 tree parm;
33043
33044 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
33045 token = cp_lexer_peek_token (parser->lexer);
33046
33047 if (token->type == CPP_ELLIPSIS)
33048 {
33049 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
33050 *ellipsisp = true;
33051 token = cp_lexer_peek_token (parser->lexer);
33052 break;
33053 }
33054
33055 /* TODO: parse attributes for tail parameters. */
33056 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
33057 false, NULL);
33058 parm = grokdeclarator (parmdecl->declarator,
33059 &parmdecl->decl_specifiers,
33060 PARM, /*initialized=*/0,
33061 /*attrlist=*/NULL);
33062
33063 chainon (params, build_tree_list (NULL_TREE, parm));
33064 token = cp_lexer_peek_token (parser->lexer);
33065 }
33066
33067 /* We allow tail attributes for the method. */
33068 if (token->keyword == RID_ATTRIBUTE)
33069 {
33070 if (*attributes == NULL_TREE)
33071 {
33072 *attributes = cp_parser_attributes_opt (parser);
33073 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
33074 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33075 return params;
33076 }
33077 else
33078 /* We have an error, but parse the attributes, so that we can
33079 carry on. */
33080 *attributes = cp_parser_attributes_opt (parser);
33081
33082 cp_parser_error (parser,
33083 "method attributes must be specified at the end");
33084 return error_mark_node;
33085 }
33086
33087 return params;
33088 }
33089
33090 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
33091
33092 static void
33093 cp_parser_objc_interstitial_code (cp_parser* parser)
33094 {
33095 cp_token *token = cp_lexer_peek_token (parser->lexer);
33096
33097 /* If the next token is `extern' and the following token is a string
33098 literal, then we have a linkage specification. */
33099 if (token->keyword == RID_EXTERN
33100 && cp_parser_is_pure_string_literal
33101 (cp_lexer_peek_nth_token (parser->lexer, 2)))
33102 cp_parser_linkage_specification (parser);
33103 /* Handle #pragma, if any. */
33104 else if (token->type == CPP_PRAGMA)
33105 cp_parser_pragma (parser, pragma_objc_icode, NULL);
33106 /* Allow stray semicolons. */
33107 else if (token->type == CPP_SEMICOLON)
33108 cp_lexer_consume_token (parser->lexer);
33109 /* Mark methods as optional or required, when building protocols. */
33110 else if (token->keyword == RID_AT_OPTIONAL)
33111 {
33112 cp_lexer_consume_token (parser->lexer);
33113 objc_set_method_opt (true);
33114 }
33115 else if (token->keyword == RID_AT_REQUIRED)
33116 {
33117 cp_lexer_consume_token (parser->lexer);
33118 objc_set_method_opt (false);
33119 }
33120 else if (token->keyword == RID_NAMESPACE)
33121 cp_parser_namespace_definition (parser);
33122 /* Other stray characters must generate errors. */
33123 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
33124 {
33125 cp_lexer_consume_token (parser->lexer);
33126 error ("stray %qs between Objective-C++ methods",
33127 token->type == CPP_OPEN_BRACE ? "{" : "}");
33128 }
33129 /* Finally, try to parse a block-declaration, or a function-definition. */
33130 else
33131 cp_parser_block_declaration (parser, /*statement_p=*/false);
33132 }
33133
33134 /* Parse a method signature. */
33135
33136 static tree
33137 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
33138 {
33139 tree rettype, kwdparms, optparms;
33140 bool ellipsis = false;
33141 bool is_class_method;
33142
33143 is_class_method = cp_parser_objc_method_type (parser);
33144 rettype = cp_parser_objc_typename (parser);
33145 *attributes = NULL_TREE;
33146 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
33147 if (kwdparms == error_mark_node)
33148 return error_mark_node;
33149 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
33150 if (optparms == error_mark_node)
33151 return error_mark_node;
33152
33153 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
33154 }
33155
33156 static bool
33157 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
33158 {
33159 tree tattr;
33160 cp_lexer_save_tokens (parser->lexer);
33161 tattr = cp_parser_attributes_opt (parser);
33162 gcc_assert (tattr) ;
33163
33164 /* If the attributes are followed by a method introducer, this is not allowed.
33165 Dump the attributes and flag the situation. */
33166 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
33167 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33168 return true;
33169
33170 /* Otherwise, the attributes introduce some interstitial code, possibly so
33171 rewind to allow that check. */
33172 cp_lexer_rollback_tokens (parser->lexer);
33173 return false;
33174 }
33175
33176 /* Parse an Objective-C method prototype list. */
33177
33178 static void
33179 cp_parser_objc_method_prototype_list (cp_parser* parser)
33180 {
33181 cp_token *token = cp_lexer_peek_token (parser->lexer);
33182
33183 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
33184 {
33185 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
33186 {
33187 tree attributes, sig;
33188 bool is_class_method;
33189 if (token->type == CPP_PLUS)
33190 is_class_method = true;
33191 else
33192 is_class_method = false;
33193 sig = cp_parser_objc_method_signature (parser, &attributes);
33194 if (sig == error_mark_node)
33195 {
33196 cp_parser_skip_to_end_of_block_or_statement (parser);
33197 token = cp_lexer_peek_token (parser->lexer);
33198 continue;
33199 }
33200 objc_add_method_declaration (is_class_method, sig, attributes);
33201 cp_parser_consume_semicolon_at_end_of_statement (parser);
33202 }
33203 else if (token->keyword == RID_AT_PROPERTY)
33204 cp_parser_objc_at_property_declaration (parser);
33205 else if (token->keyword == RID_ATTRIBUTE
33206 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
33207 warning_at (cp_lexer_peek_token (parser->lexer)->location,
33208 OPT_Wattributes,
33209 "prefix attributes are ignored for methods");
33210 else
33211 /* Allow for interspersed non-ObjC++ code. */
33212 cp_parser_objc_interstitial_code (parser);
33213
33214 token = cp_lexer_peek_token (parser->lexer);
33215 }
33216
33217 if (token->type != CPP_EOF)
33218 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
33219 else
33220 cp_parser_error (parser, "expected %<@end%>");
33221
33222 objc_finish_interface ();
33223 }
33224
33225 /* Parse an Objective-C method definition list. */
33226
33227 static void
33228 cp_parser_objc_method_definition_list (cp_parser* parser)
33229 {
33230 for (;;)
33231 {
33232 cp_token *token = cp_lexer_peek_token (parser->lexer);
33233
33234 if (token->keyword == RID_AT_END)
33235 {
33236 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
33237 break;
33238 }
33239 else if (token->type == CPP_EOF)
33240 {
33241 cp_parser_error (parser, "expected %<@end%>");
33242 break;
33243 }
33244 else if (token->type == CPP_PLUS || token->type == CPP_MINUS)
33245 {
33246 bool is_class_method = token->type == CPP_PLUS;
33247
33248 push_deferring_access_checks (dk_deferred);
33249 tree attribute;
33250 tree sig = cp_parser_objc_method_signature (parser, &attribute);
33251 if (sig == error_mark_node)
33252 cp_parser_skip_to_end_of_block_or_statement (parser);
33253 else
33254 {
33255 objc_start_method_definition (is_class_method, sig,
33256 attribute, NULL_TREE);
33257
33258 /* For historical reasons, we accept an optional semicolon. */
33259 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33260 cp_lexer_consume_token (parser->lexer);
33261
33262 perform_deferred_access_checks (tf_warning_or_error);
33263 stop_deferring_access_checks ();
33264 tree meth
33265 = cp_parser_function_definition_after_declarator (parser, false);
33266 pop_deferring_access_checks ();
33267 objc_finish_method_definition (meth);
33268 }
33269 }
33270 /* The following case will be removed once @synthesize is
33271 completely implemented. */
33272 else if (token->keyword == RID_AT_PROPERTY)
33273 cp_parser_objc_at_property_declaration (parser);
33274 else if (token->keyword == RID_AT_SYNTHESIZE)
33275 cp_parser_objc_at_synthesize_declaration (parser);
33276 else if (token->keyword == RID_AT_DYNAMIC)
33277 cp_parser_objc_at_dynamic_declaration (parser);
33278 else if (token->keyword == RID_ATTRIBUTE
33279 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
33280 warning_at (token->location, OPT_Wattributes,
33281 "prefix attributes are ignored for methods");
33282 else
33283 /* Allow for interspersed non-ObjC++ code. */
33284 cp_parser_objc_interstitial_code (parser);
33285 }
33286
33287 objc_finish_implementation ();
33288 }
33289
33290 /* Parse Objective-C ivars. */
33291
33292 static void
33293 cp_parser_objc_class_ivars (cp_parser* parser)
33294 {
33295 cp_token *token = cp_lexer_peek_token (parser->lexer);
33296
33297 if (token->type != CPP_OPEN_BRACE)
33298 return; /* No ivars specified. */
33299
33300 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
33301 token = cp_lexer_peek_token (parser->lexer);
33302
33303 while (token->type != CPP_CLOSE_BRACE
33304 && token->keyword != RID_AT_END && token->type != CPP_EOF)
33305 {
33306 cp_decl_specifier_seq declspecs;
33307 int decl_class_or_enum_p;
33308 tree prefix_attributes;
33309
33310 cp_parser_objc_visibility_spec (parser);
33311
33312 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33313 break;
33314
33315 cp_parser_decl_specifier_seq (parser,
33316 CP_PARSER_FLAGS_OPTIONAL,
33317 &declspecs,
33318 &decl_class_or_enum_p);
33319
33320 /* auto, register, static, extern, mutable. */
33321 if (declspecs.storage_class != sc_none)
33322 {
33323 cp_parser_error (parser, "invalid type for instance variable");
33324 declspecs.storage_class = sc_none;
33325 }
33326
33327 /* thread_local. */
33328 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
33329 {
33330 cp_parser_error (parser, "invalid type for instance variable");
33331 declspecs.locations[ds_thread] = 0;
33332 }
33333
33334 /* typedef. */
33335 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
33336 {
33337 cp_parser_error (parser, "invalid type for instance variable");
33338 declspecs.locations[ds_typedef] = 0;
33339 }
33340
33341 prefix_attributes = declspecs.attributes;
33342 declspecs.attributes = NULL_TREE;
33343
33344 /* Keep going until we hit the `;' at the end of the
33345 declaration. */
33346 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33347 {
33348 tree width = NULL_TREE, attributes, first_attribute, decl;
33349 cp_declarator *declarator = NULL;
33350 int ctor_dtor_or_conv_p;
33351
33352 /* Check for a (possibly unnamed) bitfield declaration. */
33353 token = cp_lexer_peek_token (parser->lexer);
33354 if (token->type == CPP_COLON)
33355 goto eat_colon;
33356
33357 if (token->type == CPP_NAME
33358 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
33359 == CPP_COLON))
33360 {
33361 /* Get the name of the bitfield. */
33362 declarator = make_id_declarator (NULL_TREE,
33363 cp_parser_identifier (parser),
33364 sfk_none, token->location);
33365
33366 eat_colon:
33367 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
33368 /* Get the width of the bitfield. */
33369 width
33370 = cp_parser_constant_expression (parser);
33371 }
33372 else
33373 {
33374 /* Parse the declarator. */
33375 declarator
33376 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
33377 CP_PARSER_FLAGS_NONE,
33378 &ctor_dtor_or_conv_p,
33379 /*parenthesized_p=*/NULL,
33380 /*member_p=*/false,
33381 /*friend_p=*/false,
33382 /*static_p=*/false);
33383 }
33384
33385 /* Look for attributes that apply to the ivar. */
33386 attributes = cp_parser_attributes_opt (parser);
33387 /* Remember which attributes are prefix attributes and
33388 which are not. */
33389 first_attribute = attributes;
33390 /* Combine the attributes. */
33391 attributes = attr_chainon (prefix_attributes, attributes);
33392
33393 if (width)
33394 /* Create the bitfield declaration. */
33395 decl = grokbitfield (declarator, &declspecs,
33396 width, NULL_TREE, attributes);
33397 else
33398 decl = grokfield (declarator, &declspecs,
33399 NULL_TREE, /*init_const_expr_p=*/false,
33400 NULL_TREE, attributes);
33401
33402 /* Add the instance variable. */
33403 if (decl != error_mark_node && decl != NULL_TREE)
33404 objc_add_instance_variable (decl);
33405
33406 /* Reset PREFIX_ATTRIBUTES. */
33407 if (attributes != error_mark_node)
33408 {
33409 while (attributes && TREE_CHAIN (attributes) != first_attribute)
33410 attributes = TREE_CHAIN (attributes);
33411 if (attributes)
33412 TREE_CHAIN (attributes) = NULL_TREE;
33413 }
33414
33415 token = cp_lexer_peek_token (parser->lexer);
33416
33417 if (token->type == CPP_COMMA)
33418 {
33419 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
33420 continue;
33421 }
33422 break;
33423 }
33424
33425 cp_parser_consume_semicolon_at_end_of_statement (parser);
33426 token = cp_lexer_peek_token (parser->lexer);
33427 }
33428
33429 if (token->keyword == RID_AT_END)
33430 cp_parser_error (parser, "expected %<}%>");
33431
33432 /* Do not consume the RID_AT_END, so it will be read again as terminating
33433 the @interface of @implementation. */
33434 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
33435 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
33436
33437 /* For historical reasons, we accept an optional semicolon. */
33438 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33439 cp_lexer_consume_token (parser->lexer);
33440 }
33441
33442 /* Parse an Objective-C protocol declaration. */
33443
33444 static void
33445 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
33446 {
33447 tree proto, protorefs;
33448 cp_token *tok;
33449
33450 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
33451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33452 {
33453 tok = cp_lexer_peek_token (parser->lexer);
33454 error_at (tok->location, "identifier expected after %<@protocol%>");
33455 cp_parser_consume_semicolon_at_end_of_statement (parser);
33456 return;
33457 }
33458
33459 /* See if we have a forward declaration or a definition. */
33460 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
33461
33462 /* Try a forward declaration first. */
33463 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
33464 {
33465 while (true)
33466 {
33467 tree id;
33468
33469 id = cp_parser_identifier (parser);
33470 if (id == error_mark_node)
33471 break;
33472
33473 objc_declare_protocol (id, attributes);
33474
33475 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33476 cp_lexer_consume_token (parser->lexer);
33477 else
33478 break;
33479 }
33480 cp_parser_consume_semicolon_at_end_of_statement (parser);
33481 }
33482
33483 /* Ok, we got a full-fledged definition (or at least should). */
33484 else
33485 {
33486 proto = cp_parser_identifier (parser);
33487 protorefs = cp_parser_objc_protocol_refs_opt (parser);
33488 objc_start_protocol (proto, protorefs, attributes);
33489 cp_parser_objc_method_prototype_list (parser);
33490 }
33491 }
33492
33493 /* Parse an Objective-C superclass or category. */
33494
33495 static void
33496 cp_parser_objc_superclass_or_category (cp_parser *parser,
33497 bool iface_p,
33498 tree *super,
33499 tree *categ, bool *is_class_extension)
33500 {
33501 cp_token *next = cp_lexer_peek_token (parser->lexer);
33502
33503 *super = *categ = NULL_TREE;
33504 *is_class_extension = false;
33505 if (next->type == CPP_COLON)
33506 {
33507 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
33508 *super = cp_parser_identifier (parser);
33509 }
33510 else if (next->type == CPP_OPEN_PAREN)
33511 {
33512 matching_parens parens;
33513 parens.consume_open (parser); /* Eat '('. */
33514
33515 /* If there is no category name, and this is an @interface, we
33516 have a class extension. */
33517 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33518 {
33519 *categ = NULL_TREE;
33520 *is_class_extension = true;
33521 }
33522 else
33523 *categ = cp_parser_identifier (parser);
33524
33525 parens.require_close (parser);
33526 }
33527 }
33528
33529 /* Parse an Objective-C class interface. */
33530
33531 static void
33532 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
33533 {
33534 tree name, super, categ, protos;
33535 bool is_class_extension;
33536
33537 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
33538 name = cp_parser_identifier (parser);
33539 if (name == error_mark_node)
33540 {
33541 /* It's hard to recover because even if valid @interface stuff
33542 is to follow, we can't compile it (or validate it) if we
33543 don't even know which class it refers to. Let's assume this
33544 was a stray '@interface' token in the stream and skip it.
33545 */
33546 return;
33547 }
33548 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
33549 &is_class_extension);
33550 protos = cp_parser_objc_protocol_refs_opt (parser);
33551
33552 /* We have either a class or a category on our hands. */
33553 if (categ || is_class_extension)
33554 objc_start_category_interface (name, categ, protos, attributes);
33555 else
33556 {
33557 objc_start_class_interface (name, super, protos, attributes);
33558 /* Handle instance variable declarations, if any. */
33559 cp_parser_objc_class_ivars (parser);
33560 objc_continue_interface ();
33561 }
33562
33563 cp_parser_objc_method_prototype_list (parser);
33564 }
33565
33566 /* Parse an Objective-C class implementation. */
33567
33568 static void
33569 cp_parser_objc_class_implementation (cp_parser* parser)
33570 {
33571 tree name, super, categ;
33572 bool is_class_extension;
33573
33574 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
33575 name = cp_parser_identifier (parser);
33576 if (name == error_mark_node)
33577 {
33578 /* It's hard to recover because even if valid @implementation
33579 stuff is to follow, we can't compile it (or validate it) if
33580 we don't even know which class it refers to. Let's assume
33581 this was a stray '@implementation' token in the stream and
33582 skip it.
33583 */
33584 return;
33585 }
33586 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
33587 &is_class_extension);
33588
33589 /* We have either a class or a category on our hands. */
33590 if (categ)
33591 objc_start_category_implementation (name, categ);
33592 else
33593 {
33594 objc_start_class_implementation (name, super);
33595 /* Handle instance variable declarations, if any. */
33596 cp_parser_objc_class_ivars (parser);
33597 objc_continue_implementation ();
33598 }
33599
33600 cp_parser_objc_method_definition_list (parser);
33601 }
33602
33603 /* Consume the @end token and finish off the implementation. */
33604
33605 static void
33606 cp_parser_objc_end_implementation (cp_parser* parser)
33607 {
33608 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
33609 objc_finish_implementation ();
33610 }
33611
33612 /* Parse an Objective-C declaration. */
33613
33614 static void
33615 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
33616 {
33617 /* Try to figure out what kind of declaration is present. */
33618 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
33619
33620 if (attributes)
33621 switch (kwd->keyword)
33622 {
33623 case RID_AT_ALIAS:
33624 case RID_AT_CLASS:
33625 case RID_AT_END:
33626 error_at (kwd->location, "attributes may not be specified before"
33627 " the %<@%D%> Objective-C++ keyword",
33628 kwd->u.value);
33629 attributes = NULL;
33630 break;
33631 case RID_AT_IMPLEMENTATION:
33632 warning_at (kwd->location, OPT_Wattributes,
33633 "prefix attributes are ignored before %<@%D%>",
33634 kwd->u.value);
33635 attributes = NULL;
33636 default:
33637 break;
33638 }
33639
33640 switch (kwd->keyword)
33641 {
33642 case RID_AT_ALIAS:
33643 cp_parser_objc_alias_declaration (parser);
33644 break;
33645 case RID_AT_CLASS:
33646 cp_parser_objc_class_declaration (parser);
33647 break;
33648 case RID_AT_PROTOCOL:
33649 cp_parser_objc_protocol_declaration (parser, attributes);
33650 break;
33651 case RID_AT_INTERFACE:
33652 cp_parser_objc_class_interface (parser, attributes);
33653 break;
33654 case RID_AT_IMPLEMENTATION:
33655 cp_parser_objc_class_implementation (parser);
33656 break;
33657 case RID_AT_END:
33658 cp_parser_objc_end_implementation (parser);
33659 break;
33660 default:
33661 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
33662 kwd->u.value);
33663 cp_parser_skip_to_end_of_block_or_statement (parser);
33664 }
33665 }
33666
33667 /* Parse an Objective-C try-catch-finally statement.
33668
33669 objc-try-catch-finally-stmt:
33670 @try compound-statement objc-catch-clause-seq [opt]
33671 objc-finally-clause [opt]
33672
33673 objc-catch-clause-seq:
33674 objc-catch-clause objc-catch-clause-seq [opt]
33675
33676 objc-catch-clause:
33677 @catch ( objc-exception-declaration ) compound-statement
33678
33679 objc-finally-clause:
33680 @finally compound-statement
33681
33682 objc-exception-declaration:
33683 parameter-declaration
33684 '...'
33685
33686 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
33687
33688 Returns NULL_TREE.
33689
33690 PS: This function is identical to c_parser_objc_try_catch_finally_statement
33691 for C. Keep them in sync. */
33692
33693 static tree
33694 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
33695 {
33696 location_t location;
33697 tree stmt;
33698
33699 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
33700 location = cp_lexer_peek_token (parser->lexer)->location;
33701 objc_maybe_warn_exceptions (location);
33702 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
33703 node, lest it get absorbed into the surrounding block. */
33704 stmt = push_stmt_list ();
33705 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
33706 objc_begin_try_stmt (location, pop_stmt_list (stmt));
33707
33708 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
33709 {
33710 cp_parameter_declarator *parm;
33711 tree parameter_declaration = error_mark_node;
33712 bool seen_open_paren = false;
33713 matching_parens parens;
33714
33715 cp_lexer_consume_token (parser->lexer);
33716 if (parens.require_open (parser))
33717 seen_open_paren = true;
33718 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
33719 {
33720 /* We have "@catch (...)" (where the '...' are literally
33721 what is in the code). Skip the '...'.
33722 parameter_declaration is set to NULL_TREE, and
33723 objc_being_catch_clauses() knows that that means
33724 '...'. */
33725 cp_lexer_consume_token (parser->lexer);
33726 parameter_declaration = NULL_TREE;
33727 }
33728 else
33729 {
33730 /* We have "@catch (NSException *exception)" or something
33731 like that. Parse the parameter declaration. */
33732 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
33733 false, NULL);
33734 if (parm == NULL)
33735 parameter_declaration = error_mark_node;
33736 else
33737 parameter_declaration = grokdeclarator (parm->declarator,
33738 &parm->decl_specifiers,
33739 PARM, /*initialized=*/0,
33740 /*attrlist=*/NULL);
33741 }
33742 if (seen_open_paren)
33743 parens.require_close (parser);
33744 else
33745 {
33746 /* If there was no open parenthesis, we are recovering from
33747 an error, and we are trying to figure out what mistake
33748 the user has made. */
33749
33750 /* If there is an immediate closing parenthesis, the user
33751 probably forgot the opening one (ie, they typed "@catch
33752 NSException *e)". Parse the closing parenthesis and keep
33753 going. */
33754 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33755 cp_lexer_consume_token (parser->lexer);
33756
33757 /* If these is no immediate closing parenthesis, the user
33758 probably doesn't know that parenthesis are required at
33759 all (ie, they typed "@catch NSException *e"). So, just
33760 forget about the closing parenthesis and keep going. */
33761 }
33762 objc_begin_catch_clause (parameter_declaration);
33763 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
33764 objc_finish_catch_clause ();
33765 }
33766 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
33767 {
33768 cp_lexer_consume_token (parser->lexer);
33769 location = cp_lexer_peek_token (parser->lexer)->location;
33770 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
33771 node, lest it get absorbed into the surrounding block. */
33772 stmt = push_stmt_list ();
33773 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
33774 objc_build_finally_clause (location, pop_stmt_list (stmt));
33775 }
33776
33777 return objc_finish_try_stmt ();
33778 }
33779
33780 /* Parse an Objective-C synchronized statement.
33781
33782 objc-synchronized-stmt:
33783 @synchronized ( expression ) compound-statement
33784
33785 Returns NULL_TREE. */
33786
33787 static tree
33788 cp_parser_objc_synchronized_statement (cp_parser *parser)
33789 {
33790 location_t location;
33791 tree lock, stmt;
33792
33793 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
33794
33795 location = cp_lexer_peek_token (parser->lexer)->location;
33796 objc_maybe_warn_exceptions (location);
33797 matching_parens parens;
33798 parens.require_open (parser);
33799 lock = cp_parser_expression (parser);
33800 parens.require_close (parser);
33801
33802 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
33803 node, lest it get absorbed into the surrounding block. */
33804 stmt = push_stmt_list ();
33805 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
33806
33807 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
33808 }
33809
33810 /* Parse an Objective-C throw statement.
33811
33812 objc-throw-stmt:
33813 @throw assignment-expression [opt] ;
33814
33815 Returns a constructed '@throw' statement. */
33816
33817 static tree
33818 cp_parser_objc_throw_statement (cp_parser *parser)
33819 {
33820 tree expr = NULL_TREE;
33821 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33822
33823 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
33824
33825 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33826 expr = cp_parser_expression (parser);
33827
33828 cp_parser_consume_semicolon_at_end_of_statement (parser);
33829
33830 return objc_build_throw_stmt (loc, expr);
33831 }
33832
33833 /* Parse an Objective-C statement. */
33834
33835 static tree
33836 cp_parser_objc_statement (cp_parser * parser)
33837 {
33838 /* Try to figure out what kind of declaration is present. */
33839 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
33840
33841 switch (kwd->keyword)
33842 {
33843 case RID_AT_TRY:
33844 return cp_parser_objc_try_catch_finally_statement (parser);
33845 case RID_AT_SYNCHRONIZED:
33846 return cp_parser_objc_synchronized_statement (parser);
33847 case RID_AT_THROW:
33848 return cp_parser_objc_throw_statement (parser);
33849 default:
33850 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
33851 kwd->u.value);
33852 cp_parser_skip_to_end_of_block_or_statement (parser);
33853 }
33854
33855 return error_mark_node;
33856 }
33857
33858 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
33859 look ahead to see if an objc keyword follows the attributes. This
33860 is to detect the use of prefix attributes on ObjC @interface and
33861 @protocol. */
33862
33863 static bool
33864 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
33865 {
33866 cp_lexer_save_tokens (parser->lexer);
33867 *attrib = cp_parser_attributes_opt (parser);
33868 gcc_assert (*attrib);
33869 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
33870 {
33871 cp_lexer_commit_tokens (parser->lexer);
33872 return true;
33873 }
33874 cp_lexer_rollback_tokens (parser->lexer);
33875 return false;
33876 }
33877
33878 /* This routine is a minimal replacement for
33879 c_parser_struct_declaration () used when parsing the list of
33880 types/names or ObjC++ properties. For example, when parsing the
33881 code
33882
33883 @property (readonly) int a, b, c;
33884
33885 this function is responsible for parsing "int a, int b, int c" and
33886 returning the declarations as CHAIN of DECLs.
33887
33888 TODO: Share this code with cp_parser_objc_class_ivars. It's very
33889 similar parsing. */
33890 static tree
33891 cp_parser_objc_struct_declaration (cp_parser *parser)
33892 {
33893 tree decls = NULL_TREE;
33894 cp_decl_specifier_seq declspecs;
33895 int decl_class_or_enum_p;
33896 tree prefix_attributes;
33897
33898 cp_parser_decl_specifier_seq (parser,
33899 CP_PARSER_FLAGS_NONE,
33900 &declspecs,
33901 &decl_class_or_enum_p);
33902
33903 if (declspecs.type == error_mark_node)
33904 return error_mark_node;
33905
33906 /* auto, register, static, extern, mutable. */
33907 if (declspecs.storage_class != sc_none)
33908 {
33909 cp_parser_error (parser, "invalid type for property");
33910 declspecs.storage_class = sc_none;
33911 }
33912
33913 /* thread_local. */
33914 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
33915 {
33916 cp_parser_error (parser, "invalid type for property");
33917 declspecs.locations[ds_thread] = 0;
33918 }
33919
33920 /* typedef. */
33921 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
33922 {
33923 cp_parser_error (parser, "invalid type for property");
33924 declspecs.locations[ds_typedef] = 0;
33925 }
33926
33927 prefix_attributes = declspecs.attributes;
33928 declspecs.attributes = NULL_TREE;
33929
33930 /* Keep going until we hit the `;' at the end of the declaration. */
33931 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33932 {
33933 tree attributes, first_attribute, decl;
33934 cp_declarator *declarator;
33935 cp_token *token;
33936
33937 /* Parse the declarator. */
33938 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
33939 CP_PARSER_FLAGS_NONE,
33940 NULL, NULL, false, false, false);
33941
33942 /* Look for attributes that apply to the ivar. */
33943 attributes = cp_parser_attributes_opt (parser);
33944 /* Remember which attributes are prefix attributes and
33945 which are not. */
33946 first_attribute = attributes;
33947 /* Combine the attributes. */
33948 attributes = attr_chainon (prefix_attributes, attributes);
33949
33950 decl = grokfield (declarator, &declspecs,
33951 NULL_TREE, /*init_const_expr_p=*/false,
33952 NULL_TREE, attributes);
33953
33954 if (decl == error_mark_node || decl == NULL_TREE)
33955 return error_mark_node;
33956
33957 /* Reset PREFIX_ATTRIBUTES. */
33958 if (attributes != error_mark_node)
33959 {
33960 while (attributes && TREE_CHAIN (attributes) != first_attribute)
33961 attributes = TREE_CHAIN (attributes);
33962 if (attributes)
33963 TREE_CHAIN (attributes) = NULL_TREE;
33964 }
33965
33966 DECL_CHAIN (decl) = decls;
33967 decls = decl;
33968
33969 token = cp_lexer_peek_token (parser->lexer);
33970 if (token->type == CPP_COMMA)
33971 {
33972 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
33973 continue;
33974 }
33975 else
33976 break;
33977 }
33978 return decls;
33979 }
33980
33981 /* Parse an Objective-C @property declaration. The syntax is:
33982
33983 objc-property-declaration:
33984 '@property' objc-property-attributes[opt] struct-declaration ;
33985
33986 objc-property-attributes:
33987 '(' objc-property-attribute-list ')'
33988
33989 objc-property-attribute-list:
33990 objc-property-attribute
33991 objc-property-attribute-list, objc-property-attribute
33992
33993 objc-property-attribute
33994 'getter' = identifier
33995 'setter' = identifier
33996 'readonly'
33997 'readwrite'
33998 'assign'
33999 'retain'
34000 'copy'
34001 'nonatomic'
34002
34003 For example:
34004 @property NSString *name;
34005 @property (readonly) id object;
34006 @property (retain, nonatomic, getter=getTheName) id name;
34007 @property int a, b, c;
34008
34009 PS: This function is identical to
34010 c_parser_objc_at_property_declaration for C. Keep them in sync. */
34011 static void
34012 cp_parser_objc_at_property_declaration (cp_parser *parser)
34013 {
34014 /* Parse the optional attribute list.
34015
34016 A list of parsed, but not verified, attributes. */
34017 vec<property_attribute_info *> prop_attr_list = vNULL;
34018 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34019
34020 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
34021
34022 /* Parse the optional attribute list... */
34023 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34024 {
34025 /* Eat the '('. */
34026 matching_parens parens;
34027 location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
34028 parens.consume_open (parser);
34029 bool syntax_error = false;
34030
34031 /* Allow empty @property attribute lists, but with a warning. */
34032 location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
34033 location_t attr_comb;
34034 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
34035 {
34036 attr_comb = make_location (attr_end, attr_start, attr_end);
34037 warning_at (attr_comb, OPT_Wattributes,
34038 "empty property attribute list");
34039 }
34040 else
34041 while (true)
34042 {
34043 cp_token *token = cp_lexer_peek_token (parser->lexer);
34044 attr_start = token->location;
34045 attr_end = get_finish (token->location);
34046 attr_comb = make_location (attr_start, attr_start, attr_end);
34047
34048 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
34049 {
34050 warning_at (attr_comb, OPT_Wattributes,
34051 "missing property attribute");
34052 if (token->type == CPP_CLOSE_PAREN)
34053 break;
34054 cp_lexer_consume_token (parser->lexer);
34055 continue;
34056 }
34057
34058 tree attr_name = NULL_TREE;
34059 if (identifier_p (token->u.value))
34060 attr_name = token->u.value;
34061
34062 enum rid keyword;
34063 if (token->type == CPP_NAME)
34064 keyword = C_RID_CODE (token->u.value);
34065 else if (token->type == CPP_KEYWORD
34066 && token->keyword == RID_CLASS)
34067 /* Account for accepting the 'class' keyword in this context. */
34068 keyword = RID_CLASS;
34069 else
34070 keyword = RID_MAX; /* By definition, an unknown property. */
34071 cp_lexer_consume_token (parser->lexer);
34072
34073 enum objc_property_attribute_kind prop_kind
34074 = objc_prop_attr_kind_for_rid (keyword);
34075 property_attribute_info *prop
34076 = new property_attribute_info (attr_name, attr_comb, prop_kind);
34077 prop_attr_list.safe_push (prop);
34078
34079 tree meth_name;
34080 switch (prop->prop_kind)
34081 {
34082 default: break;
34083 case OBJC_PROPERTY_ATTR_UNKNOWN:
34084 if (attr_name)
34085 error_at (attr_start, "unknown property attribute %qE",
34086 attr_name);
34087 else
34088 error_at (attr_start, "unknown property attribute");
34089 prop->parse_error = syntax_error = true;
34090 break;
34091
34092 case OBJC_PROPERTY_ATTR_GETTER:
34093 case OBJC_PROPERTY_ATTR_SETTER:
34094 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34095 {
34096 attr_comb = make_location (attr_end, attr_start, attr_end);
34097 error_at (attr_comb, "expected %<=%> after Objective-C %qE",
34098 attr_name);
34099 prop->parse_error = syntax_error = true;
34100 break;
34101 }
34102
34103 token = cp_lexer_peek_token (parser->lexer);
34104 attr_end = token->location;
34105 cp_lexer_consume_token (parser->lexer); /* eat the = */
34106
34107 if (!cp_parser_objc_selector_p
34108 (cp_lexer_peek_token (parser->lexer)->type))
34109 {
34110 attr_comb = make_location (attr_end, attr_start, attr_end);
34111 error_at (attr_comb, "expected %qE selector name",
34112 attr_name);
34113 prop->parse_error = syntax_error = true;
34114 break;
34115 }
34116
34117 /* Get the end of the method name, and consume the name. */
34118 token = cp_lexer_peek_token (parser->lexer);
34119 attr_end = get_finish (token->location);
34120 /* Because method names may contain C++ keywords, we have a
34121 routine to fetch them (this also consumes the token). */
34122 meth_name = cp_parser_objc_selector (parser);
34123
34124 if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
34125 {
34126 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
34127 {
34128 attr_comb = make_location (attr_end, attr_start,
34129 attr_end);
34130 error_at (attr_comb, "setter method names must"
34131 " terminate with %<:%>");
34132 prop->parse_error = syntax_error = true;
34133 }
34134 else
34135 {
34136 attr_end = get_finish (cp_lexer_peek_token
34137 (parser->lexer)->location);
34138 cp_lexer_consume_token (parser->lexer);
34139 }
34140 attr_comb = make_location (attr_start, attr_start,
34141 attr_end);
34142 }
34143 else
34144 attr_comb = make_location (attr_start, attr_start,
34145 attr_end);
34146 prop->ident = meth_name;
34147 /* Updated location including all that was successfully
34148 parsed. */
34149 prop->prop_loc = attr_comb;
34150 break;
34151 }
34152
34153 /* If we see a comma here, then keep going - even if we already
34154 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
34155 this makes a more useful output and avoid spurious warnings
34156 about missing attributes that are, in fact, specified after the
34157 one with the syntax error. */
34158 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34159 cp_lexer_consume_token (parser->lexer);
34160 else
34161 break;
34162 }
34163
34164 if (syntax_error || !parens.require_close (parser))
34165 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34166 /*or_comma=*/false,
34167 /*consume_paren=*/true);
34168 }
34169
34170 /* 'properties' is the list of properties that we read. Usually a
34171 single one, but maybe more (eg, in "@property int a, b, c;" there
34172 are three).
34173 TODO: Update this parsing so that it accepts (erroneous) bitfields so
34174 that we can issue a meaningful and consistent (between C/C++) error
34175 message from objc_add_property_declaration (). */
34176 tree properties = cp_parser_objc_struct_declaration (parser);
34177
34178 if (properties == error_mark_node)
34179 cp_parser_skip_to_end_of_statement (parser);
34180 else if (properties == NULL_TREE)
34181 cp_parser_error (parser, "expected identifier");
34182 else
34183 {
34184 /* Comma-separated properties are chained together in reverse order;
34185 add them one by one. */
34186 properties = nreverse (properties);
34187 for (; properties; properties = TREE_CHAIN (properties))
34188 objc_add_property_declaration (loc, copy_node (properties),
34189 prop_attr_list);
34190 }
34191
34192 cp_parser_consume_semicolon_at_end_of_statement (parser);
34193
34194 while (!prop_attr_list.is_empty())
34195 delete prop_attr_list.pop ();
34196 prop_attr_list.release ();
34197 }
34198
34199 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
34200
34201 objc-synthesize-declaration:
34202 @synthesize objc-synthesize-identifier-list ;
34203
34204 objc-synthesize-identifier-list:
34205 objc-synthesize-identifier
34206 objc-synthesize-identifier-list, objc-synthesize-identifier
34207
34208 objc-synthesize-identifier
34209 identifier
34210 identifier = identifier
34211
34212 For example:
34213 @synthesize MyProperty;
34214 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
34215
34216 PS: This function is identical to c_parser_objc_at_synthesize_declaration
34217 for C. Keep them in sync.
34218 */
34219 static void
34220 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
34221 {
34222 tree list = NULL_TREE;
34223 location_t loc;
34224 loc = cp_lexer_peek_token (parser->lexer)->location;
34225
34226 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
34227 while (true)
34228 {
34229 tree property, ivar;
34230 property = cp_parser_identifier (parser);
34231 if (property == error_mark_node)
34232 {
34233 cp_parser_consume_semicolon_at_end_of_statement (parser);
34234 return;
34235 }
34236 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
34237 {
34238 cp_lexer_consume_token (parser->lexer);
34239 ivar = cp_parser_identifier (parser);
34240 if (ivar == error_mark_node)
34241 {
34242 cp_parser_consume_semicolon_at_end_of_statement (parser);
34243 return;
34244 }
34245 }
34246 else
34247 ivar = NULL_TREE;
34248 list = chainon (list, build_tree_list (ivar, property));
34249 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34250 cp_lexer_consume_token (parser->lexer);
34251 else
34252 break;
34253 }
34254 cp_parser_consume_semicolon_at_end_of_statement (parser);
34255 objc_add_synthesize_declaration (loc, list);
34256 }
34257
34258 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
34259
34260 objc-dynamic-declaration:
34261 @dynamic identifier-list ;
34262
34263 For example:
34264 @dynamic MyProperty;
34265 @dynamic MyProperty, AnotherProperty;
34266
34267 PS: This function is identical to c_parser_objc_at_dynamic_declaration
34268 for C. Keep them in sync.
34269 */
34270 static void
34271 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
34272 {
34273 tree list = NULL_TREE;
34274 location_t loc;
34275 loc = cp_lexer_peek_token (parser->lexer)->location;
34276
34277 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
34278 while (true)
34279 {
34280 tree property;
34281 property = cp_parser_identifier (parser);
34282 if (property == error_mark_node)
34283 {
34284 cp_parser_consume_semicolon_at_end_of_statement (parser);
34285 return;
34286 }
34287 list = chainon (list, build_tree_list (NULL, property));
34288 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34289 cp_lexer_consume_token (parser->lexer);
34290 else
34291 break;
34292 }
34293 cp_parser_consume_semicolon_at_end_of_statement (parser);
34294 objc_add_dynamic_declaration (loc, list);
34295 }
34296
34297 \f
34298 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
34299
34300 /* Returns name of the next clause.
34301 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
34302 the token is not consumed. Otherwise appropriate pragma_omp_clause is
34303 returned and the token is consumed. */
34304
34305 static pragma_omp_clause
34306 cp_parser_omp_clause_name (cp_parser *parser)
34307 {
34308 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
34309
34310 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
34311 result = PRAGMA_OACC_CLAUSE_AUTO;
34312 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
34313 result = PRAGMA_OMP_CLAUSE_IF;
34314 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
34315 result = PRAGMA_OMP_CLAUSE_DEFAULT;
34316 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
34317 result = PRAGMA_OACC_CLAUSE_DELETE;
34318 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
34319 result = PRAGMA_OMP_CLAUSE_PRIVATE;
34320 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34321 result = PRAGMA_OMP_CLAUSE_FOR;
34322 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34323 {
34324 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34325 const char *p = IDENTIFIER_POINTER (id);
34326
34327 switch (p[0])
34328 {
34329 case 'a':
34330 if (!strcmp ("aligned", p))
34331 result = PRAGMA_OMP_CLAUSE_ALIGNED;
34332 else if (!strcmp ("allocate", p))
34333 result = PRAGMA_OMP_CLAUSE_ALLOCATE;
34334 else if (!strcmp ("async", p))
34335 result = PRAGMA_OACC_CLAUSE_ASYNC;
34336 else if (!strcmp ("attach", p))
34337 result = PRAGMA_OACC_CLAUSE_ATTACH;
34338 break;
34339 case 'b':
34340 if (!strcmp ("bind", p))
34341 result = PRAGMA_OMP_CLAUSE_BIND;
34342 break;
34343 case 'c':
34344 if (!strcmp ("collapse", p))
34345 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
34346 else if (!strcmp ("copy", p))
34347 result = PRAGMA_OACC_CLAUSE_COPY;
34348 else if (!strcmp ("copyin", p))
34349 result = PRAGMA_OMP_CLAUSE_COPYIN;
34350 else if (!strcmp ("copyout", p))
34351 result = PRAGMA_OACC_CLAUSE_COPYOUT;
34352 else if (!strcmp ("copyprivate", p))
34353 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
34354 else if (!strcmp ("create", p))
34355 result = PRAGMA_OACC_CLAUSE_CREATE;
34356 break;
34357 case 'd':
34358 if (!strcmp ("defaultmap", p))
34359 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
34360 else if (!strcmp ("depend", p))
34361 result = PRAGMA_OMP_CLAUSE_DEPEND;
34362 else if (!strcmp ("detach", p))
34363 result = PRAGMA_OACC_CLAUSE_DETACH;
34364 else if (!strcmp ("device", p))
34365 result = PRAGMA_OMP_CLAUSE_DEVICE;
34366 else if (!strcmp ("deviceptr", p))
34367 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
34368 else if (!strcmp ("device_resident", p))
34369 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
34370 else if (!strcmp ("device_type", p))
34371 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
34372 else if (!strcmp ("dist_schedule", p))
34373 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
34374 break;
34375 case 'f':
34376 if (!strcmp ("final", p))
34377 result = PRAGMA_OMP_CLAUSE_FINAL;
34378 else if (!strcmp ("finalize", p))
34379 result = PRAGMA_OACC_CLAUSE_FINALIZE;
34380 else if (!strcmp ("firstprivate", p))
34381 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
34382 else if (!strcmp ("from", p))
34383 result = PRAGMA_OMP_CLAUSE_FROM;
34384 break;
34385 case 'g':
34386 if (!strcmp ("gang", p))
34387 result = PRAGMA_OACC_CLAUSE_GANG;
34388 else if (!strcmp ("grainsize", p))
34389 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
34390 break;
34391 case 'h':
34392 if (!strcmp ("hint", p))
34393 result = PRAGMA_OMP_CLAUSE_HINT;
34394 else if (!strcmp ("host", p))
34395 result = PRAGMA_OACC_CLAUSE_HOST;
34396 break;
34397 case 'i':
34398 if (!strcmp ("if_present", p))
34399 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
34400 else if (!strcmp ("in_reduction", p))
34401 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
34402 else if (!strcmp ("inbranch", p))
34403 result = PRAGMA_OMP_CLAUSE_INBRANCH;
34404 else if (!strcmp ("independent", p))
34405 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
34406 else if (!strcmp ("is_device_ptr", p))
34407 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
34408 break;
34409 case 'l':
34410 if (!strcmp ("lastprivate", p))
34411 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
34412 else if (!strcmp ("linear", p))
34413 result = PRAGMA_OMP_CLAUSE_LINEAR;
34414 else if (!strcmp ("link", p))
34415 result = PRAGMA_OMP_CLAUSE_LINK;
34416 break;
34417 case 'm':
34418 if (!strcmp ("map", p))
34419 result = PRAGMA_OMP_CLAUSE_MAP;
34420 else if (!strcmp ("mergeable", p))
34421 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
34422 break;
34423 case 'n':
34424 if (!strcmp ("no_create", p))
34425 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
34426 else if (!strcmp ("nogroup", p))
34427 result = PRAGMA_OMP_CLAUSE_NOGROUP;
34428 else if (!strcmp ("nontemporal", p))
34429 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
34430 else if (!strcmp ("notinbranch", p))
34431 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
34432 else if (!strcmp ("nowait", p))
34433 result = PRAGMA_OMP_CLAUSE_NOWAIT;
34434 else if (!strcmp ("num_gangs", p))
34435 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
34436 else if (!strcmp ("num_tasks", p))
34437 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
34438 else if (!strcmp ("num_teams", p))
34439 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
34440 else if (!strcmp ("num_threads", p))
34441 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
34442 else if (!strcmp ("num_workers", p))
34443 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
34444 break;
34445 case 'o':
34446 if (!strcmp ("ordered", p))
34447 result = PRAGMA_OMP_CLAUSE_ORDERED;
34448 else if (!strcmp ("order", p))
34449 result = PRAGMA_OMP_CLAUSE_ORDER;
34450 break;
34451 case 'p':
34452 if (!strcmp ("parallel", p))
34453 result = PRAGMA_OMP_CLAUSE_PARALLEL;
34454 else if (!strcmp ("present", p))
34455 result = PRAGMA_OACC_CLAUSE_PRESENT;
34456 else if (!strcmp ("present_or_copy", p)
34457 || !strcmp ("pcopy", p))
34458 result = PRAGMA_OACC_CLAUSE_COPY;
34459 else if (!strcmp ("present_or_copyin", p)
34460 || !strcmp ("pcopyin", p))
34461 result = PRAGMA_OACC_CLAUSE_COPYIN;
34462 else if (!strcmp ("present_or_copyout", p)
34463 || !strcmp ("pcopyout", p))
34464 result = PRAGMA_OACC_CLAUSE_COPYOUT;
34465 else if (!strcmp ("present_or_create", p)
34466 || !strcmp ("pcreate", p))
34467 result = PRAGMA_OACC_CLAUSE_CREATE;
34468 else if (!strcmp ("priority", p))
34469 result = PRAGMA_OMP_CLAUSE_PRIORITY;
34470 else if (!strcmp ("proc_bind", p))
34471 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
34472 break;
34473 case 'r':
34474 if (!strcmp ("reduction", p))
34475 result = PRAGMA_OMP_CLAUSE_REDUCTION;
34476 break;
34477 case 's':
34478 if (!strcmp ("safelen", p))
34479 result = PRAGMA_OMP_CLAUSE_SAFELEN;
34480 else if (!strcmp ("schedule", p))
34481 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
34482 else if (!strcmp ("sections", p))
34483 result = PRAGMA_OMP_CLAUSE_SECTIONS;
34484 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
34485 result = PRAGMA_OACC_CLAUSE_HOST;
34486 else if (!strcmp ("seq", p))
34487 result = PRAGMA_OACC_CLAUSE_SEQ;
34488 else if (!strcmp ("shared", p))
34489 result = PRAGMA_OMP_CLAUSE_SHARED;
34490 else if (!strcmp ("simd", p))
34491 result = PRAGMA_OMP_CLAUSE_SIMD;
34492 else if (!strcmp ("simdlen", p))
34493 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
34494 break;
34495 case 't':
34496 if (!strcmp ("task_reduction", p))
34497 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
34498 else if (!strcmp ("taskgroup", p))
34499 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
34500 else if (!strcmp ("thread_limit", p))
34501 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
34502 else if (!strcmp ("threads", p))
34503 result = PRAGMA_OMP_CLAUSE_THREADS;
34504 else if (!strcmp ("tile", p))
34505 result = PRAGMA_OACC_CLAUSE_TILE;
34506 else if (!strcmp ("to", p))
34507 result = PRAGMA_OMP_CLAUSE_TO;
34508 break;
34509 case 'u':
34510 if (!strcmp ("uniform", p))
34511 result = PRAGMA_OMP_CLAUSE_UNIFORM;
34512 else if (!strcmp ("untied", p))
34513 result = PRAGMA_OMP_CLAUSE_UNTIED;
34514 else if (!strcmp ("use_device", p))
34515 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
34516 else if (!strcmp ("use_device_addr", p))
34517 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
34518 else if (!strcmp ("use_device_ptr", p))
34519 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
34520 break;
34521 case 'v':
34522 if (!strcmp ("vector", p))
34523 result = PRAGMA_OACC_CLAUSE_VECTOR;
34524 else if (!strcmp ("vector_length", p))
34525 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
34526 break;
34527 case 'w':
34528 if (!strcmp ("wait", p))
34529 result = PRAGMA_OACC_CLAUSE_WAIT;
34530 else if (!strcmp ("worker", p))
34531 result = PRAGMA_OACC_CLAUSE_WORKER;
34532 break;
34533 }
34534 }
34535
34536 if (result != PRAGMA_OMP_CLAUSE_NONE)
34537 cp_lexer_consume_token (parser->lexer);
34538
34539 return result;
34540 }
34541
34542 /* Validate that a clause of the given type does not already exist. */
34543
34544 static void
34545 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
34546 const char *name, location_t location)
34547 {
34548 if (omp_find_clause (clauses, code))
34549 error_at (location, "too many %qs clauses", name);
34550 }
34551
34552 /* OpenMP 2.5:
34553 variable-list:
34554 identifier
34555 variable-list , identifier
34556
34557 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
34558 colon). An opening parenthesis will have been consumed by the caller.
34559
34560 If KIND is nonzero, create the appropriate node and install the decl
34561 in OMP_CLAUSE_DECL and add the node to the head of the list.
34562
34563 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
34564 return the list created.
34565
34566 COLON can be NULL if only closing parenthesis should end the list,
34567 or pointer to bool which will receive false if the list is terminated
34568 by closing parenthesis or true if the list is terminated by colon.
34569
34570 The optional ALLOW_DEREF argument is true if list items can use the deref
34571 (->) operator. */
34572
34573 static tree
34574 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
34575 tree list, bool *colon,
34576 bool allow_deref = false)
34577 {
34578 cp_token *token;
34579 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
34580 if (colon)
34581 {
34582 parser->colon_corrects_to_scope_p = false;
34583 *colon = false;
34584 }
34585 while (1)
34586 {
34587 tree name, decl;
34588
34589 if (kind == OMP_CLAUSE_DEPEND)
34590 cp_parser_parse_tentatively (parser);
34591 token = cp_lexer_peek_token (parser->lexer);
34592 if (kind != 0
34593 && current_class_ptr
34594 && cp_parser_is_keyword (token, RID_THIS))
34595 {
34596 decl = finish_this_expr ();
34597 if (TREE_CODE (decl) == NON_LVALUE_EXPR
34598 || CONVERT_EXPR_P (decl))
34599 decl = TREE_OPERAND (decl, 0);
34600 cp_lexer_consume_token (parser->lexer);
34601 }
34602 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
34603 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
34604 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
34605 {
34606 cp_id_kind idk;
34607 decl = cp_parser_primary_expression (parser, false, false, false,
34608 &idk);
34609 }
34610 else
34611 {
34612 name = cp_parser_id_expression (parser, /*template_p=*/false,
34613 /*check_dependency_p=*/true,
34614 /*template_p=*/NULL,
34615 /*declarator_p=*/false,
34616 /*optional_p=*/false);
34617 if (name == error_mark_node)
34618 {
34619 if (kind == OMP_CLAUSE_DEPEND
34620 && cp_parser_simulate_error (parser))
34621 goto depend_lvalue;
34622 goto skip_comma;
34623 }
34624
34625 if (identifier_p (name))
34626 decl = cp_parser_lookup_name_simple (parser, name, token->location);
34627 else
34628 decl = name;
34629 if (decl == error_mark_node)
34630 {
34631 if (kind == OMP_CLAUSE_DEPEND
34632 && cp_parser_simulate_error (parser))
34633 goto depend_lvalue;
34634 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
34635 token->location);
34636 }
34637 }
34638 if (outer_automatic_var_p (decl))
34639 decl = process_outer_var_ref (decl, tf_warning_or_error);
34640 if (decl == error_mark_node)
34641 ;
34642 else if (kind != 0)
34643 {
34644 switch (kind)
34645 {
34646 case OMP_CLAUSE__CACHE_:
34647 /* The OpenACC cache directive explicitly only allows "array
34648 elements or subarrays". */
34649 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
34650 {
34651 error_at (token->location, "expected %<[%>");
34652 decl = error_mark_node;
34653 break;
34654 }
34655 /* FALLTHROUGH. */
34656 case OMP_CLAUSE_MAP:
34657 case OMP_CLAUSE_FROM:
34658 case OMP_CLAUSE_TO:
34659 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
34660 || (allow_deref
34661 && cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
34662 {
34663 cpp_ttype ttype
34664 = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
34665 ? CPP_DOT : CPP_DEREF;
34666 location_t loc
34667 = cp_lexer_peek_token (parser->lexer)->location;
34668 cp_id_kind idk = CP_ID_KIND_NONE;
34669 cp_lexer_consume_token (parser->lexer);
34670 decl = convert_from_reference (decl);
34671 decl
34672 = cp_parser_postfix_dot_deref_expression (parser, ttype,
34673 decl, false,
34674 &idk, loc);
34675 }
34676 /* FALLTHROUGH. */
34677 case OMP_CLAUSE_DEPEND:
34678 case OMP_CLAUSE_REDUCTION:
34679 case OMP_CLAUSE_IN_REDUCTION:
34680 case OMP_CLAUSE_TASK_REDUCTION:
34681 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
34682 {
34683 tree low_bound = NULL_TREE, length = NULL_TREE;
34684
34685 parser->colon_corrects_to_scope_p = false;
34686 cp_lexer_consume_token (parser->lexer);
34687 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34688 low_bound = cp_parser_expression (parser);
34689 if (!colon)
34690 parser->colon_corrects_to_scope_p
34691 = saved_colon_corrects_to_scope_p;
34692 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
34693 length = integer_one_node;
34694 else
34695 {
34696 /* Look for `:'. */
34697 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34698 {
34699 if (kind == OMP_CLAUSE_DEPEND
34700 && cp_parser_simulate_error (parser))
34701 goto depend_lvalue;
34702 goto skip_comma;
34703 }
34704 if (kind == OMP_CLAUSE_DEPEND)
34705 cp_parser_commit_to_tentative_parse (parser);
34706 if (!cp_lexer_next_token_is (parser->lexer,
34707 CPP_CLOSE_SQUARE))
34708 length = cp_parser_expression (parser);
34709 }
34710 /* Look for the closing `]'. */
34711 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
34712 RT_CLOSE_SQUARE))
34713 {
34714 if (kind == OMP_CLAUSE_DEPEND
34715 && cp_parser_simulate_error (parser))
34716 goto depend_lvalue;
34717 goto skip_comma;
34718 }
34719
34720 decl = tree_cons (low_bound, length, decl);
34721 }
34722 break;
34723 default:
34724 break;
34725 }
34726
34727 if (kind == OMP_CLAUSE_DEPEND)
34728 {
34729 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
34730 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
34731 && cp_parser_simulate_error (parser))
34732 {
34733 depend_lvalue:
34734 cp_parser_abort_tentative_parse (parser);
34735 decl = cp_parser_assignment_expression (parser, NULL,
34736 false, false);
34737 }
34738 else
34739 cp_parser_parse_definitely (parser);
34740 }
34741
34742 tree u = build_omp_clause (token->location, kind);
34743 OMP_CLAUSE_DECL (u) = decl;
34744 OMP_CLAUSE_CHAIN (u) = list;
34745 list = u;
34746 }
34747 else
34748 list = tree_cons (decl, NULL_TREE, list);
34749
34750 get_comma:
34751 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
34752 break;
34753 cp_lexer_consume_token (parser->lexer);
34754 }
34755
34756 if (colon)
34757 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34758
34759 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34760 {
34761 *colon = true;
34762 cp_parser_require (parser, CPP_COLON, RT_COLON);
34763 return list;
34764 }
34765
34766 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34767 {
34768 int ending;
34769
34770 /* Try to resync to an unnested comma. Copied from
34771 cp_parser_parenthesized_expression_list. */
34772 skip_comma:
34773 if (colon)
34774 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34775 ending = cp_parser_skip_to_closing_parenthesis (parser,
34776 /*recovering=*/true,
34777 /*or_comma=*/true,
34778 /*consume_paren=*/true);
34779 if (ending < 0)
34780 goto get_comma;
34781 }
34782
34783 return list;
34784 }
34785
34786 /* Similarly, but expect leading and trailing parenthesis. This is a very
34787 common case for omp clauses. */
34788
34789 static tree
34790 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
34791 bool allow_deref = false)
34792 {
34793 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34794 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
34795 allow_deref);
34796 return list;
34797 }
34798
34799 /* OpenACC 2.0:
34800 copy ( variable-list )
34801 copyin ( variable-list )
34802 copyout ( variable-list )
34803 create ( variable-list )
34804 delete ( variable-list )
34805 present ( variable-list )
34806
34807 OpenACC 2.6:
34808 no_create ( variable-list )
34809 attach ( variable-list )
34810 detach ( variable-list ) */
34811
34812 static tree
34813 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
34814 tree list)
34815 {
34816 enum gomp_map_kind kind;
34817 switch (c_kind)
34818 {
34819 case PRAGMA_OACC_CLAUSE_ATTACH:
34820 kind = GOMP_MAP_ATTACH;
34821 break;
34822 case PRAGMA_OACC_CLAUSE_COPY:
34823 kind = GOMP_MAP_TOFROM;
34824 break;
34825 case PRAGMA_OACC_CLAUSE_COPYIN:
34826 kind = GOMP_MAP_TO;
34827 break;
34828 case PRAGMA_OACC_CLAUSE_COPYOUT:
34829 kind = GOMP_MAP_FROM;
34830 break;
34831 case PRAGMA_OACC_CLAUSE_CREATE:
34832 kind = GOMP_MAP_ALLOC;
34833 break;
34834 case PRAGMA_OACC_CLAUSE_DELETE:
34835 kind = GOMP_MAP_RELEASE;
34836 break;
34837 case PRAGMA_OACC_CLAUSE_DETACH:
34838 kind = GOMP_MAP_DETACH;
34839 break;
34840 case PRAGMA_OACC_CLAUSE_DEVICE:
34841 kind = GOMP_MAP_FORCE_TO;
34842 break;
34843 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
34844 kind = GOMP_MAP_DEVICE_RESIDENT;
34845 break;
34846 case PRAGMA_OACC_CLAUSE_HOST:
34847 kind = GOMP_MAP_FORCE_FROM;
34848 break;
34849 case PRAGMA_OACC_CLAUSE_LINK:
34850 kind = GOMP_MAP_LINK;
34851 break;
34852 case PRAGMA_OACC_CLAUSE_NO_CREATE:
34853 kind = GOMP_MAP_IF_PRESENT;
34854 break;
34855 case PRAGMA_OACC_CLAUSE_PRESENT:
34856 kind = GOMP_MAP_FORCE_PRESENT;
34857 break;
34858 default:
34859 gcc_unreachable ();
34860 }
34861 tree nl, c;
34862 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, true);
34863
34864 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
34865 OMP_CLAUSE_SET_MAP_KIND (c, kind);
34866
34867 return nl;
34868 }
34869
34870 /* OpenACC 2.0:
34871 deviceptr ( variable-list ) */
34872
34873 static tree
34874 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
34875 {
34876 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34877 tree vars, t;
34878
34879 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
34880 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
34881 variable-list must only allow for pointer variables. */
34882 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34883 for (t = vars; t; t = TREE_CHAIN (t))
34884 {
34885 tree v = TREE_PURPOSE (t);
34886 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
34887 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
34888 OMP_CLAUSE_DECL (u) = v;
34889 OMP_CLAUSE_CHAIN (u) = list;
34890 list = u;
34891 }
34892
34893 return list;
34894 }
34895
34896 /* OpenACC 2.5:
34897 auto
34898 finalize
34899 independent
34900 nohost
34901 seq */
34902
34903 static tree
34904 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
34905 tree list)
34906 {
34907 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
34908
34909 tree c = build_omp_clause (loc, code);
34910 OMP_CLAUSE_CHAIN (c) = list;
34911
34912 return c;
34913 }
34914
34915 /* OpenACC:
34916 num_gangs ( expression )
34917 num_workers ( expression )
34918 vector_length ( expression ) */
34919
34920 static tree
34921 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
34922 const char *str, tree list)
34923 {
34924 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34925
34926 matching_parens parens;
34927 if (!parens.require_open (parser))
34928 return list;
34929
34930 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
34931
34932 if (t == error_mark_node
34933 || !parens.require_close (parser))
34934 {
34935 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34936 /*or_comma=*/false,
34937 /*consume_paren=*/true);
34938 return list;
34939 }
34940
34941 check_no_duplicate_clause (list, code, str, loc);
34942
34943 tree c = build_omp_clause (loc, code);
34944 OMP_CLAUSE_OPERAND (c, 0) = t;
34945 OMP_CLAUSE_CHAIN (c) = list;
34946 return c;
34947 }
34948
34949 /* OpenACC:
34950
34951 gang [( gang-arg-list )]
34952 worker [( [num:] int-expr )]
34953 vector [( [length:] int-expr )]
34954
34955 where gang-arg is one of:
34956
34957 [num:] int-expr
34958 static: size-expr
34959
34960 and size-expr may be:
34961
34962 *
34963 int-expr
34964 */
34965
34966 static tree
34967 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
34968 omp_clause_code kind,
34969 const char *str, tree list)
34970 {
34971 const char *id = "num";
34972 cp_lexer *lexer = parser->lexer;
34973 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
34974
34975 if (kind == OMP_CLAUSE_VECTOR)
34976 id = "length";
34977
34978 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
34979 {
34980 matching_parens parens;
34981 parens.consume_open (parser);
34982
34983 do
34984 {
34985 cp_token *next = cp_lexer_peek_token (lexer);
34986 int idx = 0;
34987
34988 /* Gang static argument. */
34989 if (kind == OMP_CLAUSE_GANG
34990 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
34991 {
34992 cp_lexer_consume_token (lexer);
34993
34994 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34995 goto cleanup_error;
34996
34997 idx = 1;
34998 if (ops[idx] != NULL)
34999 {
35000 cp_parser_error (parser, "too many %<static%> arguments");
35001 goto cleanup_error;
35002 }
35003
35004 /* Check for the '*' argument. */
35005 if (cp_lexer_next_token_is (lexer, CPP_MULT)
35006 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
35007 || cp_lexer_nth_token_is (parser->lexer, 2,
35008 CPP_CLOSE_PAREN)))
35009 {
35010 cp_lexer_consume_token (lexer);
35011 ops[idx] = integer_minus_one_node;
35012
35013 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
35014 {
35015 cp_lexer_consume_token (lexer);
35016 continue;
35017 }
35018 else break;
35019 }
35020 }
35021 /* Worker num: argument and vector length: arguments. */
35022 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
35023 && id_equal (next->u.value, id)
35024 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
35025 {
35026 cp_lexer_consume_token (lexer); /* id */
35027 cp_lexer_consume_token (lexer); /* ':' */
35028 }
35029
35030 /* Now collect the actual argument. */
35031 if (ops[idx] != NULL_TREE)
35032 {
35033 cp_parser_error (parser, "unexpected argument");
35034 goto cleanup_error;
35035 }
35036
35037 tree expr = cp_parser_assignment_expression (parser, NULL, false,
35038 false);
35039 if (expr == error_mark_node)
35040 goto cleanup_error;
35041
35042 mark_exp_read (expr);
35043 ops[idx] = expr;
35044
35045 if (kind == OMP_CLAUSE_GANG
35046 && cp_lexer_next_token_is (lexer, CPP_COMMA))
35047 {
35048 cp_lexer_consume_token (lexer);
35049 continue;
35050 }
35051 break;
35052 }
35053 while (1);
35054
35055 if (!parens.require_close (parser))
35056 goto cleanup_error;
35057 }
35058
35059 check_no_duplicate_clause (list, kind, str, loc);
35060
35061 c = build_omp_clause (loc, kind);
35062
35063 if (ops[1])
35064 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
35065
35066 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
35067 OMP_CLAUSE_CHAIN (c) = list;
35068
35069 return c;
35070
35071 cleanup_error:
35072 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
35073 return list;
35074 }
35075
35076 /* OpenACC 2.0:
35077 tile ( size-expr-list ) */
35078
35079 static tree
35080 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
35081 {
35082 tree c, expr = error_mark_node;
35083 tree tile = NULL_TREE;
35084
35085 /* Collapse and tile are mutually exclusive. (The spec doesn't say
35086 so, but the spec authors never considered such a case and have
35087 differing opinions on what it might mean, including 'not
35088 allowed'.) */
35089 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
35090 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
35091 clause_loc);
35092
35093 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35094 return list;
35095
35096 do
35097 {
35098 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
35099 return list;
35100
35101 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
35102 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
35103 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
35104 {
35105 cp_lexer_consume_token (parser->lexer);
35106 expr = integer_zero_node;
35107 }
35108 else
35109 expr = cp_parser_constant_expression (parser);
35110
35111 tile = tree_cons (NULL_TREE, expr, tile);
35112 }
35113 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
35114
35115 /* Consume the trailing ')'. */
35116 cp_lexer_consume_token (parser->lexer);
35117
35118 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
35119 tile = nreverse (tile);
35120 OMP_CLAUSE_TILE_LIST (c) = tile;
35121 OMP_CLAUSE_CHAIN (c) = list;
35122 return c;
35123 }
35124
35125 /* OpenACC 2.0
35126 Parse wait clause or directive parameters. */
35127
35128 static tree
35129 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
35130 {
35131 vec<tree, va_gc> *args;
35132 tree t, args_tree;
35133
35134 args = cp_parser_parenthesized_expression_list (parser, non_attr,
35135 /*cast_p=*/false,
35136 /*allow_expansion_p=*/true,
35137 /*non_constant_p=*/NULL);
35138
35139 if (args == NULL || args->length () == 0)
35140 {
35141 if (args != NULL)
35142 {
35143 cp_parser_error (parser, "expected integer expression list");
35144 release_tree_vector (args);
35145 }
35146 return list;
35147 }
35148
35149 args_tree = build_tree_list_vec (args);
35150
35151 release_tree_vector (args);
35152
35153 for (t = args_tree; t; t = TREE_CHAIN (t))
35154 {
35155 tree targ = TREE_VALUE (t);
35156
35157 if (targ != error_mark_node)
35158 {
35159 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
35160 error ("%<wait%> expression must be integral");
35161 else
35162 {
35163 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
35164
35165 targ = mark_rvalue_use (targ);
35166 OMP_CLAUSE_DECL (c) = targ;
35167 OMP_CLAUSE_CHAIN (c) = list;
35168 list = c;
35169 }
35170 }
35171 }
35172
35173 return list;
35174 }
35175
35176 /* OpenACC:
35177 wait [( int-expr-list )] */
35178
35179 static tree
35180 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
35181 {
35182 location_t location = cp_lexer_peek_token (parser->lexer)->location;
35183
35184 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35185 list = cp_parser_oacc_wait_list (parser, location, list);
35186 else
35187 {
35188 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
35189
35190 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
35191 OMP_CLAUSE_CHAIN (c) = list;
35192 list = c;
35193 }
35194
35195 return list;
35196 }
35197
35198 /* OpenMP 3.0:
35199 collapse ( constant-expression ) */
35200
35201 static tree
35202 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
35203 {
35204 tree c, num;
35205 location_t loc;
35206 HOST_WIDE_INT n;
35207
35208 loc = cp_lexer_peek_token (parser->lexer)->location;
35209 matching_parens parens;
35210 if (!parens.require_open (parser))
35211 return list;
35212
35213 num = cp_parser_constant_expression (parser);
35214
35215 if (!parens.require_close (parser))
35216 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35217 /*or_comma=*/false,
35218 /*consume_paren=*/true);
35219
35220 if (num == error_mark_node)
35221 return list;
35222 num = fold_non_dependent_expr (num);
35223 if (!tree_fits_shwi_p (num)
35224 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
35225 || (n = tree_to_shwi (num)) <= 0
35226 || (int) n != n)
35227 {
35228 error_at (loc, "collapse argument needs positive constant integer expression");
35229 return list;
35230 }
35231
35232 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
35233 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
35234 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
35235 OMP_CLAUSE_CHAIN (c) = list;
35236 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
35237
35238 return c;
35239 }
35240
35241 /* OpenMP 2.5:
35242 default ( none | shared )
35243
35244 OpenACC:
35245 default ( none | present ) */
35246
35247 static tree
35248 cp_parser_omp_clause_default (cp_parser *parser, tree list,
35249 location_t location, bool is_oacc)
35250 {
35251 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
35252 tree c;
35253
35254 matching_parens parens;
35255 if (!parens.require_open (parser))
35256 return list;
35257 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35258 {
35259 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35260 const char *p = IDENTIFIER_POINTER (id);
35261
35262 switch (p[0])
35263 {
35264 case 'n':
35265 if (strcmp ("none", p) != 0)
35266 goto invalid_kind;
35267 kind = OMP_CLAUSE_DEFAULT_NONE;
35268 break;
35269
35270 case 'p':
35271 if (strcmp ("present", p) != 0 || !is_oacc)
35272 goto invalid_kind;
35273 kind = OMP_CLAUSE_DEFAULT_PRESENT;
35274 break;
35275
35276 case 's':
35277 if (strcmp ("shared", p) != 0 || is_oacc)
35278 goto invalid_kind;
35279 kind = OMP_CLAUSE_DEFAULT_SHARED;
35280 break;
35281
35282 default:
35283 goto invalid_kind;
35284 }
35285
35286 cp_lexer_consume_token (parser->lexer);
35287 }
35288 else
35289 {
35290 invalid_kind:
35291 if (is_oacc)
35292 cp_parser_error (parser, "expected %<none%> or %<present%>");
35293 else
35294 cp_parser_error (parser, "expected %<none%> or %<shared%>");
35295 }
35296
35297 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
35298 || !parens.require_close (parser))
35299 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35300 /*or_comma=*/false,
35301 /*consume_paren=*/true);
35302
35303 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
35304 return list;
35305
35306 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
35307 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
35308 OMP_CLAUSE_CHAIN (c) = list;
35309 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
35310
35311 return c;
35312 }
35313
35314 /* OpenMP 3.1:
35315 final ( expression ) */
35316
35317 static tree
35318 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
35319 {
35320 tree t, c;
35321
35322 matching_parens parens;
35323 if (!parens.require_open (parser))
35324 return list;
35325
35326 t = cp_parser_assignment_expression (parser);
35327
35328 if (t == error_mark_node
35329 || !parens.require_close (parser))
35330 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35331 /*or_comma=*/false,
35332 /*consume_paren=*/true);
35333
35334 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
35335
35336 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
35337 OMP_CLAUSE_FINAL_EXPR (c) = t;
35338 OMP_CLAUSE_CHAIN (c) = list;
35339
35340 return c;
35341 }
35342
35343 /* OpenMP 2.5:
35344 if ( expression )
35345
35346 OpenMP 4.5:
35347 if ( directive-name-modifier : expression )
35348
35349 directive-name-modifier:
35350 parallel | task | taskloop | target data | target | target update
35351 | target enter data | target exit data
35352
35353 OpenMP 5.0:
35354 directive-name-modifier:
35355 ... | simd | cancel */
35356
35357 static tree
35358 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
35359 bool is_omp)
35360 {
35361 tree t, c;
35362 enum tree_code if_modifier = ERROR_MARK;
35363
35364 matching_parens parens;
35365 if (!parens.require_open (parser))
35366 return list;
35367
35368 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35369 {
35370 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35371 const char *p = IDENTIFIER_POINTER (id);
35372 int n = 2;
35373
35374 if (strcmp ("cancel", p) == 0)
35375 if_modifier = VOID_CST;
35376 else if (strcmp ("parallel", p) == 0)
35377 if_modifier = OMP_PARALLEL;
35378 else if (strcmp ("simd", p) == 0)
35379 if_modifier = OMP_SIMD;
35380 else if (strcmp ("task", p) == 0)
35381 if_modifier = OMP_TASK;
35382 else if (strcmp ("taskloop", p) == 0)
35383 if_modifier = OMP_TASKLOOP;
35384 else if (strcmp ("target", p) == 0)
35385 {
35386 if_modifier = OMP_TARGET;
35387 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
35388 {
35389 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
35390 p = IDENTIFIER_POINTER (id);
35391 if (strcmp ("data", p) == 0)
35392 if_modifier = OMP_TARGET_DATA;
35393 else if (strcmp ("update", p) == 0)
35394 if_modifier = OMP_TARGET_UPDATE;
35395 else if (strcmp ("enter", p) == 0)
35396 if_modifier = OMP_TARGET_ENTER_DATA;
35397 else if (strcmp ("exit", p) == 0)
35398 if_modifier = OMP_TARGET_EXIT_DATA;
35399 if (if_modifier != OMP_TARGET)
35400 n = 3;
35401 else
35402 {
35403 location_t loc
35404 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
35405 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
35406 "or %<exit%>");
35407 if_modifier = ERROR_MARK;
35408 }
35409 if (if_modifier == OMP_TARGET_ENTER_DATA
35410 || if_modifier == OMP_TARGET_EXIT_DATA)
35411 {
35412 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
35413 {
35414 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
35415 p = IDENTIFIER_POINTER (id);
35416 if (strcmp ("data", p) == 0)
35417 n = 4;
35418 }
35419 if (n != 4)
35420 {
35421 location_t loc
35422 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
35423 error_at (loc, "expected %<data%>");
35424 if_modifier = ERROR_MARK;
35425 }
35426 }
35427 }
35428 }
35429 if (if_modifier != ERROR_MARK)
35430 {
35431 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
35432 {
35433 while (n-- > 0)
35434 cp_lexer_consume_token (parser->lexer);
35435 }
35436 else
35437 {
35438 if (n > 2)
35439 {
35440 location_t loc
35441 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
35442 error_at (loc, "expected %<:%>");
35443 }
35444 if_modifier = ERROR_MARK;
35445 }
35446 }
35447 }
35448
35449 t = cp_parser_assignment_expression (parser);
35450
35451 if (t == error_mark_node
35452 || !parens.require_close (parser))
35453 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35454 /*or_comma=*/false,
35455 /*consume_paren=*/true);
35456
35457 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
35458 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
35459 {
35460 if (if_modifier != ERROR_MARK
35461 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
35462 {
35463 const char *p = NULL;
35464 switch (if_modifier)
35465 {
35466 case VOID_CST: p = "cancel"; break;
35467 case OMP_PARALLEL: p = "parallel"; break;
35468 case OMP_SIMD: p = "simd"; break;
35469 case OMP_TASK: p = "task"; break;
35470 case OMP_TASKLOOP: p = "taskloop"; break;
35471 case OMP_TARGET_DATA: p = "target data"; break;
35472 case OMP_TARGET: p = "target"; break;
35473 case OMP_TARGET_UPDATE: p = "target update"; break;
35474 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
35475 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
35476 default: gcc_unreachable ();
35477 }
35478 error_at (location, "too many %<if%> clauses with %qs modifier",
35479 p);
35480 return list;
35481 }
35482 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
35483 {
35484 if (!is_omp)
35485 error_at (location, "too many %<if%> clauses");
35486 else
35487 error_at (location, "too many %<if%> clauses without modifier");
35488 return list;
35489 }
35490 else if (if_modifier == ERROR_MARK
35491 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
35492 {
35493 error_at (location, "if any %<if%> clause has modifier, then all "
35494 "%<if%> clauses have to use modifier");
35495 return list;
35496 }
35497 }
35498
35499 c = build_omp_clause (location, OMP_CLAUSE_IF);
35500 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
35501 OMP_CLAUSE_IF_EXPR (c) = t;
35502 OMP_CLAUSE_CHAIN (c) = list;
35503
35504 return c;
35505 }
35506
35507 /* OpenMP 3.1:
35508 mergeable */
35509
35510 static tree
35511 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
35512 tree list, location_t location)
35513 {
35514 tree c;
35515
35516 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
35517 location);
35518
35519 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
35520 OMP_CLAUSE_CHAIN (c) = list;
35521 return c;
35522 }
35523
35524 /* OpenMP 2.5:
35525 nowait */
35526
35527 static tree
35528 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
35529 tree list, location_t location)
35530 {
35531 tree c;
35532
35533 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
35534
35535 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
35536 OMP_CLAUSE_CHAIN (c) = list;
35537 return c;
35538 }
35539
35540 /* OpenMP 2.5:
35541 num_threads ( expression ) */
35542
35543 static tree
35544 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
35545 location_t location)
35546 {
35547 tree t, c;
35548
35549 matching_parens parens;
35550 if (!parens.require_open (parser))
35551 return list;
35552
35553 t = cp_parser_assignment_expression (parser);
35554
35555 if (t == error_mark_node
35556 || !parens.require_close (parser))
35557 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35558 /*or_comma=*/false,
35559 /*consume_paren=*/true);
35560
35561 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
35562 "num_threads", location);
35563
35564 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
35565 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
35566 OMP_CLAUSE_CHAIN (c) = list;
35567
35568 return c;
35569 }
35570
35571 /* OpenMP 4.5:
35572 num_tasks ( expression ) */
35573
35574 static tree
35575 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
35576 location_t location)
35577 {
35578 tree t, c;
35579
35580 matching_parens parens;
35581 if (!parens.require_open (parser))
35582 return list;
35583
35584 t = cp_parser_assignment_expression (parser);
35585
35586 if (t == error_mark_node
35587 || !parens.require_close (parser))
35588 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35589 /*or_comma=*/false,
35590 /*consume_paren=*/true);
35591
35592 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
35593 "num_tasks", location);
35594
35595 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
35596 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
35597 OMP_CLAUSE_CHAIN (c) = list;
35598
35599 return c;
35600 }
35601
35602 /* OpenMP 4.5:
35603 grainsize ( expression ) */
35604
35605 static tree
35606 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
35607 location_t location)
35608 {
35609 tree t, c;
35610
35611 matching_parens parens;
35612 if (!parens.require_open (parser))
35613 return list;
35614
35615 t = cp_parser_assignment_expression (parser);
35616
35617 if (t == error_mark_node
35618 || !parens.require_close (parser))
35619 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35620 /*or_comma=*/false,
35621 /*consume_paren=*/true);
35622
35623 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
35624 "grainsize", location);
35625
35626 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
35627 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
35628 OMP_CLAUSE_CHAIN (c) = list;
35629
35630 return c;
35631 }
35632
35633 /* OpenMP 4.5:
35634 priority ( expression ) */
35635
35636 static tree
35637 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
35638 location_t location)
35639 {
35640 tree t, c;
35641
35642 matching_parens parens;
35643 if (!parens.require_open (parser))
35644 return list;
35645
35646 t = cp_parser_assignment_expression (parser);
35647
35648 if (t == error_mark_node
35649 || !parens.require_close (parser))
35650 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35651 /*or_comma=*/false,
35652 /*consume_paren=*/true);
35653
35654 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
35655 "priority", location);
35656
35657 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
35658 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
35659 OMP_CLAUSE_CHAIN (c) = list;
35660
35661 return c;
35662 }
35663
35664 /* OpenMP 4.5:
35665 hint ( expression ) */
35666
35667 static tree
35668 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
35669 {
35670 tree t, c;
35671
35672 matching_parens parens;
35673 if (!parens.require_open (parser))
35674 return list;
35675
35676 t = cp_parser_assignment_expression (parser);
35677
35678 if (t != error_mark_node)
35679 {
35680 t = fold_non_dependent_expr (t);
35681 if (!value_dependent_expression_p (t)
35682 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
35683 || !tree_fits_shwi_p (t)
35684 || tree_int_cst_sgn (t) == -1))
35685 error_at (location, "expected constant integer expression with "
35686 "valid sync-hint value");
35687 }
35688 if (t == error_mark_node
35689 || !parens.require_close (parser))
35690 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35691 /*or_comma=*/false,
35692 /*consume_paren=*/true);
35693 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
35694
35695 c = build_omp_clause (location, OMP_CLAUSE_HINT);
35696 OMP_CLAUSE_HINT_EXPR (c) = t;
35697 OMP_CLAUSE_CHAIN (c) = list;
35698
35699 return c;
35700 }
35701
35702 /* OpenMP 4.5:
35703 defaultmap ( tofrom : scalar )
35704
35705 OpenMP 5.0:
35706 defaultmap ( implicit-behavior [ : variable-category ] ) */
35707
35708 static tree
35709 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
35710 location_t location)
35711 {
35712 tree c, id;
35713 const char *p;
35714 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
35715 enum omp_clause_defaultmap_kind category
35716 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
35717
35718 matching_parens parens;
35719 if (!parens.require_open (parser))
35720 return list;
35721
35722 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
35723 p = "default";
35724 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35725 {
35726 invalid_behavior:
35727 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
35728 "%<tofrom%>, %<firstprivate%>, %<none%> "
35729 "or %<default%>");
35730 goto out_err;
35731 }
35732 else
35733 {
35734 id = cp_lexer_peek_token (parser->lexer)->u.value;
35735 p = IDENTIFIER_POINTER (id);
35736 }
35737
35738 switch (p[0])
35739 {
35740 case 'a':
35741 if (strcmp ("alloc", p) == 0)
35742 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
35743 else
35744 goto invalid_behavior;
35745 break;
35746
35747 case 'd':
35748 if (strcmp ("default", p) == 0)
35749 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
35750 else
35751 goto invalid_behavior;
35752 break;
35753
35754 case 'f':
35755 if (strcmp ("firstprivate", p) == 0)
35756 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
35757 else if (strcmp ("from", p) == 0)
35758 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
35759 else
35760 goto invalid_behavior;
35761 break;
35762
35763 case 'n':
35764 if (strcmp ("none", p) == 0)
35765 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
35766 else
35767 goto invalid_behavior;
35768 break;
35769
35770 case 't':
35771 if (strcmp ("tofrom", p) == 0)
35772 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
35773 else if (strcmp ("to", p) == 0)
35774 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
35775 else
35776 goto invalid_behavior;
35777 break;
35778
35779 default:
35780 goto invalid_behavior;
35781 }
35782 cp_lexer_consume_token (parser->lexer);
35783
35784 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
35785 {
35786 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35787 goto out_err;
35788
35789 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35790 {
35791 invalid_category:
35792 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
35793 "%<pointer%>");
35794 goto out_err;
35795 }
35796 id = cp_lexer_peek_token (parser->lexer)->u.value;
35797 p = IDENTIFIER_POINTER (id);
35798
35799 switch (p[0])
35800 {
35801 case 'a':
35802 if (strcmp ("aggregate", p) == 0)
35803 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
35804 else
35805 goto invalid_category;
35806 break;
35807
35808 case 'p':
35809 if (strcmp ("pointer", p) == 0)
35810 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
35811 else
35812 goto invalid_category;
35813 break;
35814
35815 case 's':
35816 if (strcmp ("scalar", p) == 0)
35817 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
35818 else
35819 goto invalid_category;
35820 break;
35821
35822 default:
35823 goto invalid_category;
35824 }
35825
35826 cp_lexer_consume_token (parser->lexer);
35827 }
35828 if (!parens.require_close (parser))
35829 goto out_err;
35830
35831 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
35832 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
35833 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
35834 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
35835 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
35836 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
35837 {
35838 enum omp_clause_defaultmap_kind cat = category;
35839 location_t loc = OMP_CLAUSE_LOCATION (c);
35840 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
35841 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
35842 p = NULL;
35843 switch (cat)
35844 {
35845 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
35846 p = NULL;
35847 break;
35848 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
35849 p = "aggregate";
35850 break;
35851 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
35852 p = "pointer";
35853 break;
35854 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
35855 p = "scalar";
35856 break;
35857 default:
35858 gcc_unreachable ();
35859 }
35860 if (p)
35861 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
35862 p);
35863 else
35864 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
35865 "category");
35866 break;
35867 }
35868
35869 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
35870 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
35871 OMP_CLAUSE_CHAIN (c) = list;
35872 return c;
35873
35874 out_err:
35875 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35876 /*or_comma=*/false,
35877 /*consume_paren=*/true);
35878 return list;
35879 }
35880
35881 /* OpenMP 5.0:
35882 order ( concurrent ) */
35883
35884 static tree
35885 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
35886 {
35887 tree c, id;
35888 const char *p;
35889
35890 matching_parens parens;
35891 if (!parens.require_open (parser))
35892 return list;
35893
35894 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35895 {
35896 cp_parser_error (parser, "expected %<concurrent%>");
35897 goto out_err;
35898 }
35899 else
35900 {
35901 id = cp_lexer_peek_token (parser->lexer)->u.value;
35902 p = IDENTIFIER_POINTER (id);
35903 }
35904 if (strcmp (p, "concurrent") != 0)
35905 {
35906 cp_parser_error (parser, "expected %<concurrent%>");
35907 goto out_err;
35908 }
35909 cp_lexer_consume_token (parser->lexer);
35910 if (!parens.require_close (parser))
35911 goto out_err;
35912
35913 /* check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location); */
35914 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
35915 OMP_CLAUSE_CHAIN (c) = list;
35916 return c;
35917
35918 out_err:
35919 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35920 /*or_comma=*/false,
35921 /*consume_paren=*/true);
35922 return list;
35923 }
35924
35925 /* OpenMP 5.0:
35926 bind ( teams | parallel | thread ) */
35927
35928 static tree
35929 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
35930 location_t location)
35931 {
35932 tree c;
35933 const char *p;
35934 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
35935
35936 matching_parens parens;
35937 if (!parens.require_open (parser))
35938 return list;
35939
35940 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35941 {
35942 invalid:
35943 cp_parser_error (parser,
35944 "expected %<teams%>, %<parallel%> or %<thread%>");
35945 goto out_err;
35946 }
35947 else
35948 {
35949 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35950 p = IDENTIFIER_POINTER (id);
35951 }
35952 if (strcmp (p, "teams") == 0)
35953 kind = OMP_CLAUSE_BIND_TEAMS;
35954 else if (strcmp (p, "parallel") == 0)
35955 kind = OMP_CLAUSE_BIND_PARALLEL;
35956 else if (strcmp (p, "thread") != 0)
35957 goto invalid;
35958 cp_lexer_consume_token (parser->lexer);
35959 if (!parens.require_close (parser))
35960 goto out_err;
35961
35962 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
35963 c = build_omp_clause (location, OMP_CLAUSE_BIND);
35964 OMP_CLAUSE_BIND_KIND (c) = kind;
35965 OMP_CLAUSE_CHAIN (c) = list;
35966 return c;
35967
35968 out_err:
35969 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35970 /*or_comma=*/false,
35971 /*consume_paren=*/true);
35972 return list;
35973 }
35974
35975 /* OpenMP 2.5:
35976 ordered
35977
35978 OpenMP 4.5:
35979 ordered ( constant-expression ) */
35980
35981 static tree
35982 cp_parser_omp_clause_ordered (cp_parser *parser,
35983 tree list, location_t location)
35984 {
35985 tree c, num = NULL_TREE;
35986 HOST_WIDE_INT n;
35987
35988 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
35989 "ordered", location);
35990
35991 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35992 {
35993 matching_parens parens;
35994 parens.consume_open (parser);
35995
35996 num = cp_parser_constant_expression (parser);
35997
35998 if (!parens.require_close (parser))
35999 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36000 /*or_comma=*/false,
36001 /*consume_paren=*/true);
36002
36003 if (num == error_mark_node)
36004 return list;
36005 num = fold_non_dependent_expr (num);
36006 if (!tree_fits_shwi_p (num)
36007 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
36008 || (n = tree_to_shwi (num)) <= 0
36009 || (int) n != n)
36010 {
36011 error_at (location,
36012 "ordered argument needs positive constant integer "
36013 "expression");
36014 return list;
36015 }
36016 }
36017
36018 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
36019 OMP_CLAUSE_ORDERED_EXPR (c) = num;
36020 OMP_CLAUSE_CHAIN (c) = list;
36021 return c;
36022 }
36023
36024 /* OpenMP 2.5:
36025 reduction ( reduction-operator : variable-list )
36026
36027 reduction-operator:
36028 One of: + * - & ^ | && ||
36029
36030 OpenMP 3.1:
36031
36032 reduction-operator:
36033 One of: + * - & ^ | && || min max
36034
36035 OpenMP 4.0:
36036
36037 reduction-operator:
36038 One of: + * - & ^ | && ||
36039 id-expression
36040
36041 OpenMP 5.0:
36042 reduction ( reduction-modifier, reduction-operator : variable-list )
36043 in_reduction ( reduction-operator : variable-list )
36044 task_reduction ( reduction-operator : variable-list ) */
36045
36046 static tree
36047 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
36048 bool is_omp, tree list)
36049 {
36050 enum tree_code code = ERROR_MARK;
36051 tree nlist, c, id = NULL_TREE;
36052 bool task = false;
36053 bool inscan = false;
36054
36055 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36056 return list;
36057
36058 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
36059 {
36060 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
36061 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
36062 {
36063 cp_lexer_consume_token (parser->lexer);
36064 cp_lexer_consume_token (parser->lexer);
36065 }
36066 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
36067 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
36068 {
36069 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36070 const char *p = IDENTIFIER_POINTER (id);
36071 if (strcmp (p, "task") == 0)
36072 task = true;
36073 else if (strcmp (p, "inscan") == 0)
36074 inscan = true;
36075 if (task || inscan)
36076 {
36077 cp_lexer_consume_token (parser->lexer);
36078 cp_lexer_consume_token (parser->lexer);
36079 }
36080 }
36081 }
36082
36083 switch (cp_lexer_peek_token (parser->lexer)->type)
36084 {
36085 case CPP_PLUS: code = PLUS_EXPR; break;
36086 case CPP_MULT: code = MULT_EXPR; break;
36087 case CPP_MINUS: code = MINUS_EXPR; break;
36088 case CPP_AND: code = BIT_AND_EXPR; break;
36089 case CPP_XOR: code = BIT_XOR_EXPR; break;
36090 case CPP_OR: code = BIT_IOR_EXPR; break;
36091 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
36092 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
36093 default: break;
36094 }
36095
36096 if (code != ERROR_MARK)
36097 cp_lexer_consume_token (parser->lexer);
36098 else
36099 {
36100 bool saved_colon_corrects_to_scope_p;
36101 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36102 parser->colon_corrects_to_scope_p = false;
36103 id = cp_parser_id_expression (parser, /*template_p=*/false,
36104 /*check_dependency_p=*/true,
36105 /*template_p=*/NULL,
36106 /*declarator_p=*/false,
36107 /*optional_p=*/false);
36108 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36109 if (identifier_p (id))
36110 {
36111 const char *p = IDENTIFIER_POINTER (id);
36112
36113 if (strcmp (p, "min") == 0)
36114 code = MIN_EXPR;
36115 else if (strcmp (p, "max") == 0)
36116 code = MAX_EXPR;
36117 else if (id == ovl_op_identifier (false, PLUS_EXPR))
36118 code = PLUS_EXPR;
36119 else if (id == ovl_op_identifier (false, MULT_EXPR))
36120 code = MULT_EXPR;
36121 else if (id == ovl_op_identifier (false, MINUS_EXPR))
36122 code = MINUS_EXPR;
36123 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
36124 code = BIT_AND_EXPR;
36125 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
36126 code = BIT_IOR_EXPR;
36127 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
36128 code = BIT_XOR_EXPR;
36129 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
36130 code = TRUTH_ANDIF_EXPR;
36131 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
36132 code = TRUTH_ORIF_EXPR;
36133 id = omp_reduction_id (code, id, NULL_TREE);
36134 tree scope = parser->scope;
36135 if (scope)
36136 id = build_qualified_name (NULL_TREE, scope, id, false);
36137 parser->scope = NULL_TREE;
36138 parser->qualifying_scope = NULL_TREE;
36139 parser->object_scope = NULL_TREE;
36140 }
36141 else
36142 {
36143 error ("invalid reduction-identifier");
36144 resync_fail:
36145 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36146 /*or_comma=*/false,
36147 /*consume_paren=*/true);
36148 return list;
36149 }
36150 }
36151
36152 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36153 goto resync_fail;
36154
36155 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
36156 NULL);
36157 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36158 {
36159 OMP_CLAUSE_REDUCTION_CODE (c) = code;
36160 if (task)
36161 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
36162 else if (inscan)
36163 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
36164 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
36165 }
36166
36167 return nlist;
36168 }
36169
36170 /* OpenMP 2.5:
36171 schedule ( schedule-kind )
36172 schedule ( schedule-kind , expression )
36173
36174 schedule-kind:
36175 static | dynamic | guided | runtime | auto
36176
36177 OpenMP 4.5:
36178 schedule ( schedule-modifier : schedule-kind )
36179 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
36180
36181 schedule-modifier:
36182 simd
36183 monotonic
36184 nonmonotonic */
36185
36186 static tree
36187 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
36188 {
36189 tree c, t;
36190 int modifiers = 0, nmodifiers = 0;
36191
36192 matching_parens parens;
36193 if (!parens.require_open (parser))
36194 return list;
36195
36196 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
36197
36198 location_t comma = UNKNOWN_LOCATION;
36199 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36200 {
36201 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36202 const char *p = IDENTIFIER_POINTER (id);
36203 if (strcmp ("simd", p) == 0)
36204 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
36205 else if (strcmp ("monotonic", p) == 0)
36206 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
36207 else if (strcmp ("nonmonotonic", p) == 0)
36208 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
36209 else
36210 break;
36211 comma = UNKNOWN_LOCATION;
36212 cp_lexer_consume_token (parser->lexer);
36213 if (nmodifiers++ == 0
36214 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36215 {
36216 comma = cp_lexer_peek_token (parser->lexer)->location;
36217 cp_lexer_consume_token (parser->lexer);
36218 }
36219 else
36220 {
36221 cp_parser_require (parser, CPP_COLON, RT_COLON);
36222 break;
36223 }
36224 }
36225 if (comma != UNKNOWN_LOCATION)
36226 error_at (comma, "expected %<:%>");
36227
36228 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36229 {
36230 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36231 const char *p = IDENTIFIER_POINTER (id);
36232
36233 switch (p[0])
36234 {
36235 case 'd':
36236 if (strcmp ("dynamic", p) != 0)
36237 goto invalid_kind;
36238 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
36239 break;
36240
36241 case 'g':
36242 if (strcmp ("guided", p) != 0)
36243 goto invalid_kind;
36244 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
36245 break;
36246
36247 case 'r':
36248 if (strcmp ("runtime", p) != 0)
36249 goto invalid_kind;
36250 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
36251 break;
36252
36253 default:
36254 goto invalid_kind;
36255 }
36256 }
36257 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
36258 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
36259 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
36260 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
36261 else
36262 goto invalid_kind;
36263 cp_lexer_consume_token (parser->lexer);
36264
36265 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
36266 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
36267 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
36268 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
36269 {
36270 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
36271 "specified");
36272 modifiers = 0;
36273 }
36274
36275 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36276 {
36277 cp_token *token;
36278 cp_lexer_consume_token (parser->lexer);
36279
36280 token = cp_lexer_peek_token (parser->lexer);
36281 t = cp_parser_assignment_expression (parser);
36282
36283 if (t == error_mark_node)
36284 goto resync_fail;
36285 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
36286 error_at (token->location, "schedule %<runtime%> does not take "
36287 "a %<chunk_size%> parameter");
36288 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
36289 error_at (token->location, "schedule %<auto%> does not take "
36290 "a %<chunk_size%> parameter");
36291 else
36292 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
36293
36294 if (!parens.require_close (parser))
36295 goto resync_fail;
36296 }
36297 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
36298 goto resync_fail;
36299
36300 OMP_CLAUSE_SCHEDULE_KIND (c)
36301 = (enum omp_clause_schedule_kind)
36302 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
36303
36304 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
36305 OMP_CLAUSE_CHAIN (c) = list;
36306 return c;
36307
36308 invalid_kind:
36309 cp_parser_error (parser, "invalid schedule kind");
36310 resync_fail:
36311 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36312 /*or_comma=*/false,
36313 /*consume_paren=*/true);
36314 return list;
36315 }
36316
36317 /* OpenMP 3.0:
36318 untied */
36319
36320 static tree
36321 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
36322 tree list, location_t location)
36323 {
36324 tree c;
36325
36326 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
36327
36328 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
36329 OMP_CLAUSE_CHAIN (c) = list;
36330 return c;
36331 }
36332
36333 /* OpenMP 4.0:
36334 inbranch
36335 notinbranch */
36336
36337 static tree
36338 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
36339 tree list, location_t location)
36340 {
36341 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
36342 tree c = build_omp_clause (location, code);
36343 OMP_CLAUSE_CHAIN (c) = list;
36344 return c;
36345 }
36346
36347 /* OpenMP 4.0:
36348 parallel
36349 for
36350 sections
36351 taskgroup */
36352
36353 static tree
36354 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
36355 enum omp_clause_code code,
36356 tree list, location_t location)
36357 {
36358 tree c = build_omp_clause (location, code);
36359 OMP_CLAUSE_CHAIN (c) = list;
36360 return c;
36361 }
36362
36363 /* OpenMP 4.5:
36364 nogroup */
36365
36366 static tree
36367 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
36368 tree list, location_t location)
36369 {
36370 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
36371 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
36372 OMP_CLAUSE_CHAIN (c) = list;
36373 return c;
36374 }
36375
36376 /* OpenMP 4.5:
36377 simd
36378 threads */
36379
36380 static tree
36381 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
36382 enum omp_clause_code code,
36383 tree list, location_t location)
36384 {
36385 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
36386 tree c = build_omp_clause (location, code);
36387 OMP_CLAUSE_CHAIN (c) = list;
36388 return c;
36389 }
36390
36391 /* OpenMP 4.0:
36392 num_teams ( expression ) */
36393
36394 static tree
36395 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
36396 location_t location)
36397 {
36398 tree t, c;
36399
36400 matching_parens parens;
36401 if (!parens.require_open (parser))
36402 return list;
36403
36404 t = cp_parser_assignment_expression (parser);
36405
36406 if (t == error_mark_node
36407 || !parens.require_close (parser))
36408 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36409 /*or_comma=*/false,
36410 /*consume_paren=*/true);
36411
36412 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
36413 "num_teams", location);
36414
36415 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
36416 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
36417 OMP_CLAUSE_CHAIN (c) = list;
36418
36419 return c;
36420 }
36421
36422 /* OpenMP 4.0:
36423 thread_limit ( expression ) */
36424
36425 static tree
36426 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
36427 location_t location)
36428 {
36429 tree t, c;
36430
36431 matching_parens parens;
36432 if (!parens.require_open (parser))
36433 return list;
36434
36435 t = cp_parser_assignment_expression (parser);
36436
36437 if (t == error_mark_node
36438 || !parens.require_close (parser))
36439 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36440 /*or_comma=*/false,
36441 /*consume_paren=*/true);
36442
36443 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
36444 "thread_limit", location);
36445
36446 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
36447 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
36448 OMP_CLAUSE_CHAIN (c) = list;
36449
36450 return c;
36451 }
36452
36453 /* OpenMP 4.0:
36454 aligned ( variable-list )
36455 aligned ( variable-list : constant-expression ) */
36456
36457 static tree
36458 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
36459 {
36460 tree nlist, c, alignment = NULL_TREE;
36461 bool colon;
36462
36463 matching_parens parens;
36464 if (!parens.require_open (parser))
36465 return list;
36466
36467 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
36468 &colon);
36469
36470 if (colon)
36471 {
36472 alignment = cp_parser_constant_expression (parser);
36473
36474 if (!parens.require_close (parser))
36475 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36476 /*or_comma=*/false,
36477 /*consume_paren=*/true);
36478
36479 if (alignment == error_mark_node)
36480 alignment = NULL_TREE;
36481 }
36482
36483 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36484 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
36485
36486 return nlist;
36487 }
36488
36489 /* OpenMP 5.0:
36490 allocate ( variable-list )
36491 allocate ( expression : variable-list ) */
36492
36493 static tree
36494 cp_parser_omp_clause_allocate (cp_parser *parser, tree list)
36495 {
36496 tree nlist, c, allocator = NULL_TREE;
36497 bool colon;
36498
36499 matching_parens parens;
36500 if (!parens.require_open (parser))
36501 return list;
36502
36503 cp_parser_parse_tentatively (parser);
36504 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36505 parser->colon_corrects_to_scope_p = false;
36506 allocator = cp_parser_assignment_expression (parser);
36507 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36508 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
36509 {
36510 cp_parser_parse_definitely (parser);
36511 cp_lexer_consume_token (parser->lexer);
36512 if (allocator == error_mark_node)
36513 allocator = NULL_TREE;
36514 }
36515 else
36516 {
36517 cp_parser_abort_tentative_parse (parser);
36518 allocator = NULL_TREE;
36519 }
36520
36521 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALLOCATE, list,
36522 &colon);
36523
36524 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36525 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
36526
36527 return nlist;
36528 }
36529
36530 /* OpenMP 2.5:
36531 lastprivate ( variable-list )
36532
36533 OpenMP 5.0:
36534 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
36535
36536 static tree
36537 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
36538 {
36539 bool conditional = false;
36540
36541 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36542 return list;
36543
36544 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
36545 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
36546 {
36547 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36548 const char *p = IDENTIFIER_POINTER (id);
36549
36550 if (strcmp ("conditional", p) == 0)
36551 {
36552 conditional = true;
36553 cp_lexer_consume_token (parser->lexer);
36554 cp_lexer_consume_token (parser->lexer);
36555 }
36556 }
36557
36558 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
36559 list, NULL);
36560
36561 if (conditional)
36562 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36563 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
36564 return nlist;
36565 }
36566
36567 /* OpenMP 4.0:
36568 linear ( variable-list )
36569 linear ( variable-list : expression )
36570
36571 OpenMP 4.5:
36572 linear ( modifier ( variable-list ) )
36573 linear ( modifier ( variable-list ) : expression ) */
36574
36575 static tree
36576 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
36577 bool declare_simd)
36578 {
36579 tree nlist, c, step = integer_one_node;
36580 bool colon;
36581 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
36582
36583 matching_parens parens;
36584 if (!parens.require_open (parser))
36585 return list;
36586
36587 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36588 {
36589 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36590 const char *p = IDENTIFIER_POINTER (id);
36591
36592 if (strcmp ("ref", p) == 0)
36593 kind = OMP_CLAUSE_LINEAR_REF;
36594 else if (strcmp ("val", p) == 0)
36595 kind = OMP_CLAUSE_LINEAR_VAL;
36596 else if (strcmp ("uval", p) == 0)
36597 kind = OMP_CLAUSE_LINEAR_UVAL;
36598 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
36599 cp_lexer_consume_token (parser->lexer);
36600 else
36601 kind = OMP_CLAUSE_LINEAR_DEFAULT;
36602 }
36603
36604 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
36605 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
36606 &colon);
36607 else
36608 {
36609 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
36610 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
36611 if (colon)
36612 cp_parser_require (parser, CPP_COLON, RT_COLON);
36613 else if (!parens.require_close (parser))
36614 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36615 /*or_comma=*/false,
36616 /*consume_paren=*/true);
36617 }
36618
36619 if (colon)
36620 {
36621 step = NULL_TREE;
36622 if (declare_simd
36623 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
36624 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
36625 {
36626 cp_token *token = cp_lexer_peek_token (parser->lexer);
36627 cp_parser_parse_tentatively (parser);
36628 step = cp_parser_id_expression (parser, /*template_p=*/false,
36629 /*check_dependency_p=*/true,
36630 /*template_p=*/NULL,
36631 /*declarator_p=*/false,
36632 /*optional_p=*/false);
36633 if (step != error_mark_node)
36634 step = cp_parser_lookup_name_simple (parser, step, token->location);
36635 if (step == error_mark_node)
36636 {
36637 step = NULL_TREE;
36638 cp_parser_abort_tentative_parse (parser);
36639 }
36640 else if (!cp_parser_parse_definitely (parser))
36641 step = NULL_TREE;
36642 }
36643 if (!step)
36644 step = cp_parser_assignment_expression (parser);
36645
36646 if (!parens.require_close (parser))
36647 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36648 /*or_comma=*/false,
36649 /*consume_paren=*/true);
36650
36651 if (step == error_mark_node)
36652 return list;
36653 }
36654
36655 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36656 {
36657 OMP_CLAUSE_LINEAR_STEP (c) = step;
36658 OMP_CLAUSE_LINEAR_KIND (c) = kind;
36659 }
36660
36661 return nlist;
36662 }
36663
36664 /* OpenMP 4.0:
36665 safelen ( constant-expression ) */
36666
36667 static tree
36668 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
36669 location_t location)
36670 {
36671 tree t, c;
36672
36673 matching_parens parens;
36674 if (!parens.require_open (parser))
36675 return list;
36676
36677 t = cp_parser_constant_expression (parser);
36678
36679 if (t == error_mark_node
36680 || !parens.require_close (parser))
36681 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36682 /*or_comma=*/false,
36683 /*consume_paren=*/true);
36684
36685 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
36686
36687 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
36688 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
36689 OMP_CLAUSE_CHAIN (c) = list;
36690
36691 return c;
36692 }
36693
36694 /* OpenMP 4.0:
36695 simdlen ( constant-expression ) */
36696
36697 static tree
36698 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
36699 location_t location)
36700 {
36701 tree t, c;
36702
36703 matching_parens parens;
36704 if (!parens.require_open (parser))
36705 return list;
36706
36707 t = cp_parser_constant_expression (parser);
36708
36709 if (t == error_mark_node
36710 || !parens.require_close (parser))
36711 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36712 /*or_comma=*/false,
36713 /*consume_paren=*/true);
36714
36715 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
36716
36717 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
36718 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
36719 OMP_CLAUSE_CHAIN (c) = list;
36720
36721 return c;
36722 }
36723
36724 /* OpenMP 4.5:
36725 vec:
36726 identifier [+/- integer]
36727 vec , identifier [+/- integer]
36728 */
36729
36730 static tree
36731 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
36732 tree list)
36733 {
36734 tree vec = NULL;
36735
36736 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36737 {
36738 cp_parser_error (parser, "expected identifier");
36739 return list;
36740 }
36741
36742 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36743 {
36744 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
36745 tree t, identifier = cp_parser_identifier (parser);
36746 tree addend = NULL;
36747
36748 if (identifier == error_mark_node)
36749 t = error_mark_node;
36750 else
36751 {
36752 t = cp_parser_lookup_name_simple
36753 (parser, identifier,
36754 cp_lexer_peek_token (parser->lexer)->location);
36755 if (t == error_mark_node)
36756 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
36757 id_loc);
36758 }
36759
36760 bool neg = false;
36761 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
36762 neg = true;
36763 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
36764 {
36765 addend = integer_zero_node;
36766 goto add_to_vector;
36767 }
36768 cp_lexer_consume_token (parser->lexer);
36769
36770 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
36771 {
36772 cp_parser_error (parser, "expected integer");
36773 return list;
36774 }
36775
36776 addend = cp_lexer_peek_token (parser->lexer)->u.value;
36777 if (TREE_CODE (addend) != INTEGER_CST)
36778 {
36779 cp_parser_error (parser, "expected integer");
36780 return list;
36781 }
36782 cp_lexer_consume_token (parser->lexer);
36783
36784 add_to_vector:
36785 if (t != error_mark_node)
36786 {
36787 vec = tree_cons (addend, t, vec);
36788 if (neg)
36789 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
36790 }
36791
36792 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
36793 break;
36794
36795 cp_lexer_consume_token (parser->lexer);
36796 }
36797
36798 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
36799 {
36800 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
36801 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
36802 OMP_CLAUSE_DECL (u) = nreverse (vec);
36803 OMP_CLAUSE_CHAIN (u) = list;
36804 return u;
36805 }
36806 return list;
36807 }
36808
36809 /* OpenMP 5.0:
36810 iterators ( iterators-definition )
36811
36812 iterators-definition:
36813 iterator-specifier
36814 iterator-specifier , iterators-definition
36815
36816 iterator-specifier:
36817 identifier = range-specification
36818 iterator-type identifier = range-specification
36819
36820 range-specification:
36821 begin : end
36822 begin : end : step */
36823
36824 static tree
36825 cp_parser_omp_iterators (cp_parser *parser)
36826 {
36827 tree ret = NULL_TREE, *last = &ret;
36828 cp_lexer_consume_token (parser->lexer);
36829
36830 matching_parens parens;
36831 if (!parens.require_open (parser))
36832 return error_mark_node;
36833
36834 bool saved_colon_corrects_to_scope_p
36835 = parser->colon_corrects_to_scope_p;
36836 bool saved_colon_doesnt_start_class_def_p
36837 = parser->colon_doesnt_start_class_def_p;
36838
36839 do
36840 {
36841 tree iter_type;
36842 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
36843 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
36844 iter_type = integer_type_node;
36845 else
36846 {
36847 const char *saved_message
36848 = parser->type_definition_forbidden_message;
36849 parser->type_definition_forbidden_message
36850 = G_("types may not be defined in iterator type");
36851
36852 iter_type = cp_parser_type_id (parser);
36853
36854 parser->type_definition_forbidden_message = saved_message;
36855 }
36856
36857 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36858 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36859 {
36860 cp_parser_error (parser, "expected identifier");
36861 break;
36862 }
36863
36864 tree id = cp_parser_identifier (parser);
36865 if (id == error_mark_node)
36866 break;
36867
36868 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
36869 break;
36870
36871 parser->colon_corrects_to_scope_p = false;
36872 parser->colon_doesnt_start_class_def_p = true;
36873 tree begin = cp_parser_assignment_expression (parser);
36874
36875 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36876 break;
36877
36878 tree end = cp_parser_assignment_expression (parser);
36879
36880 tree step = integer_one_node;
36881 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
36882 {
36883 cp_lexer_consume_token (parser->lexer);
36884 step = cp_parser_assignment_expression (parser);
36885 }
36886
36887 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
36888 DECL_ARTIFICIAL (iter_var) = 1;
36889 DECL_CONTEXT (iter_var) = current_function_decl;
36890 pushdecl (iter_var);
36891
36892 *last = make_tree_vec (6);
36893 TREE_VEC_ELT (*last, 0) = iter_var;
36894 TREE_VEC_ELT (*last, 1) = begin;
36895 TREE_VEC_ELT (*last, 2) = end;
36896 TREE_VEC_ELT (*last, 3) = step;
36897 last = &TREE_CHAIN (*last);
36898
36899 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36900 {
36901 cp_lexer_consume_token (parser->lexer);
36902 continue;
36903 }
36904 break;
36905 }
36906 while (1);
36907
36908 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36909 parser->colon_doesnt_start_class_def_p
36910 = saved_colon_doesnt_start_class_def_p;
36911
36912 if (!parens.require_close (parser))
36913 cp_parser_skip_to_closing_parenthesis (parser,
36914 /*recovering=*/true,
36915 /*or_comma=*/false,
36916 /*consume_paren=*/true);
36917
36918 return ret ? ret : error_mark_node;
36919 }
36920
36921 /* OpenMP 4.0:
36922 depend ( depend-kind : variable-list )
36923
36924 depend-kind:
36925 in | out | inout
36926
36927 OpenMP 4.5:
36928 depend ( source )
36929
36930 depend ( sink : vec )
36931
36932 OpenMP 5.0:
36933 depend ( depend-modifier , depend-kind: variable-list )
36934
36935 depend-kind:
36936 in | out | inout | mutexinoutset | depobj
36937
36938 depend-modifier:
36939 iterator ( iterators-definition ) */
36940
36941 static tree
36942 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
36943 {
36944 tree nlist, c, iterators = NULL_TREE;
36945 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
36946
36947 matching_parens parens;
36948 if (!parens.require_open (parser))
36949 return list;
36950
36951 do
36952 {
36953 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36954 goto invalid_kind;
36955
36956 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36957 const char *p = IDENTIFIER_POINTER (id);
36958
36959 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
36960 {
36961 begin_scope (sk_omp, NULL);
36962 iterators = cp_parser_omp_iterators (parser);
36963 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
36964 continue;
36965 }
36966 if (strcmp ("in", p) == 0)
36967 kind = OMP_CLAUSE_DEPEND_IN;
36968 else if (strcmp ("inout", p) == 0)
36969 kind = OMP_CLAUSE_DEPEND_INOUT;
36970 else if (strcmp ("mutexinoutset", p) == 0)
36971 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
36972 else if (strcmp ("out", p) == 0)
36973 kind = OMP_CLAUSE_DEPEND_OUT;
36974 else if (strcmp ("depobj", p) == 0)
36975 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
36976 else if (strcmp ("sink", p) == 0)
36977 kind = OMP_CLAUSE_DEPEND_SINK;
36978 else if (strcmp ("source", p) == 0)
36979 kind = OMP_CLAUSE_DEPEND_SOURCE;
36980 else
36981 goto invalid_kind;
36982 break;
36983 }
36984 while (1);
36985
36986 cp_lexer_consume_token (parser->lexer);
36987
36988 if (iterators
36989 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
36990 {
36991 poplevel (0, 1, 0);
36992 error_at (loc, "%<iterator%> modifier incompatible with %qs",
36993 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
36994 iterators = NULL_TREE;
36995 }
36996
36997 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
36998 {
36999 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
37000 OMP_CLAUSE_DEPEND_KIND (c) = kind;
37001 OMP_CLAUSE_DECL (c) = NULL_TREE;
37002 OMP_CLAUSE_CHAIN (c) = list;
37003 if (!parens.require_close (parser))
37004 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37005 /*or_comma=*/false,
37006 /*consume_paren=*/true);
37007 return c;
37008 }
37009
37010 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37011 goto resync_fail;
37012
37013 if (kind == OMP_CLAUSE_DEPEND_SINK)
37014 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
37015 else
37016 {
37017 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
37018 list, NULL);
37019
37020 if (iterators)
37021 {
37022 tree block = poplevel (1, 1, 0);
37023 if (iterators == error_mark_node)
37024 iterators = NULL_TREE;
37025 else
37026 TREE_VEC_ELT (iterators, 5) = block;
37027 }
37028
37029 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
37030 {
37031 OMP_CLAUSE_DEPEND_KIND (c) = kind;
37032 if (iterators)
37033 OMP_CLAUSE_DECL (c)
37034 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
37035 }
37036 }
37037 return nlist;
37038
37039 invalid_kind:
37040 cp_parser_error (parser, "invalid depend kind");
37041 resync_fail:
37042 if (iterators)
37043 poplevel (0, 1, 0);
37044 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37045 /*or_comma=*/false,
37046 /*consume_paren=*/true);
37047 return list;
37048 }
37049
37050 /* OpenMP 4.0:
37051 map ( map-kind : variable-list )
37052 map ( variable-list )
37053
37054 map-kind:
37055 alloc | to | from | tofrom
37056
37057 OpenMP 4.5:
37058 map-kind:
37059 alloc | to | from | tofrom | release | delete
37060
37061 map ( always [,] map-kind: variable-list ) */
37062
37063 static tree
37064 cp_parser_omp_clause_map (cp_parser *parser, tree list)
37065 {
37066 tree nlist, c;
37067 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
37068 bool always = false;
37069
37070 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37071 return list;
37072
37073 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37074 {
37075 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37076 const char *p = IDENTIFIER_POINTER (id);
37077
37078 if (strcmp ("always", p) == 0)
37079 {
37080 int nth = 2;
37081 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
37082 nth++;
37083 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
37084 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
37085 == RID_DELETE))
37086 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
37087 == CPP_COLON))
37088 {
37089 always = true;
37090 cp_lexer_consume_token (parser->lexer);
37091 if (nth == 3)
37092 cp_lexer_consume_token (parser->lexer);
37093 }
37094 }
37095 }
37096
37097 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37098 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
37099 {
37100 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37101 const char *p = IDENTIFIER_POINTER (id);
37102
37103 if (strcmp ("alloc", p) == 0)
37104 kind = GOMP_MAP_ALLOC;
37105 else if (strcmp ("to", p) == 0)
37106 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
37107 else if (strcmp ("from", p) == 0)
37108 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
37109 else if (strcmp ("tofrom", p) == 0)
37110 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
37111 else if (strcmp ("release", p) == 0)
37112 kind = GOMP_MAP_RELEASE;
37113 else
37114 {
37115 cp_parser_error (parser, "invalid map kind");
37116 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37117 /*or_comma=*/false,
37118 /*consume_paren=*/true);
37119 return list;
37120 }
37121 cp_lexer_consume_token (parser->lexer);
37122 cp_lexer_consume_token (parser->lexer);
37123 }
37124 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
37125 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
37126 {
37127 kind = GOMP_MAP_DELETE;
37128 cp_lexer_consume_token (parser->lexer);
37129 cp_lexer_consume_token (parser->lexer);
37130 }
37131
37132 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
37133 NULL);
37134
37135 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
37136 OMP_CLAUSE_SET_MAP_KIND (c, kind);
37137
37138 return nlist;
37139 }
37140
37141 /* OpenMP 4.0:
37142 device ( expression ) */
37143
37144 static tree
37145 cp_parser_omp_clause_device (cp_parser *parser, tree list,
37146 location_t location)
37147 {
37148 tree t, c;
37149
37150 matching_parens parens;
37151 if (!parens.require_open (parser))
37152 return list;
37153
37154 t = cp_parser_assignment_expression (parser);
37155
37156 if (t == error_mark_node
37157 || !parens.require_close (parser))
37158 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37159 /*or_comma=*/false,
37160 /*consume_paren=*/true);
37161
37162 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
37163 "device", location);
37164
37165 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
37166 OMP_CLAUSE_DEVICE_ID (c) = t;
37167 OMP_CLAUSE_CHAIN (c) = list;
37168
37169 return c;
37170 }
37171
37172 /* OpenMP 4.0:
37173 dist_schedule ( static )
37174 dist_schedule ( static , expression ) */
37175
37176 static tree
37177 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
37178 location_t location)
37179 {
37180 tree c, t;
37181
37182 matching_parens parens;
37183 if (!parens.require_open (parser))
37184 return list;
37185
37186 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
37187
37188 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
37189 goto invalid_kind;
37190 cp_lexer_consume_token (parser->lexer);
37191
37192 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37193 {
37194 cp_lexer_consume_token (parser->lexer);
37195
37196 t = cp_parser_assignment_expression (parser);
37197
37198 if (t == error_mark_node)
37199 goto resync_fail;
37200 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
37201
37202 if (!parens.require_close (parser))
37203 goto resync_fail;
37204 }
37205 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
37206 goto resync_fail;
37207
37208 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
37209 "dist_schedule", location); */
37210 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
37211 warning_at (location, 0, "too many %qs clauses", "dist_schedule");
37212 OMP_CLAUSE_CHAIN (c) = list;
37213 return c;
37214
37215 invalid_kind:
37216 cp_parser_error (parser, "invalid dist_schedule kind");
37217 resync_fail:
37218 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37219 /*or_comma=*/false,
37220 /*consume_paren=*/true);
37221 return list;
37222 }
37223
37224 /* OpenMP 4.0:
37225 proc_bind ( proc-bind-kind )
37226
37227 proc-bind-kind:
37228 master | close | spread */
37229
37230 static tree
37231 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
37232 location_t location)
37233 {
37234 tree c;
37235 enum omp_clause_proc_bind_kind kind;
37236
37237 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37238 return list;
37239
37240 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37241 {
37242 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37243 const char *p = IDENTIFIER_POINTER (id);
37244
37245 if (strcmp ("master", p) == 0)
37246 kind = OMP_CLAUSE_PROC_BIND_MASTER;
37247 else if (strcmp ("close", p) == 0)
37248 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
37249 else if (strcmp ("spread", p) == 0)
37250 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
37251 else
37252 goto invalid_kind;
37253 }
37254 else
37255 goto invalid_kind;
37256
37257 cp_lexer_consume_token (parser->lexer);
37258 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
37259 goto resync_fail;
37260
37261 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
37262 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
37263 location);
37264 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
37265 OMP_CLAUSE_CHAIN (c) = list;
37266 return c;
37267
37268 invalid_kind:
37269 cp_parser_error (parser, "invalid depend kind");
37270 resync_fail:
37271 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37272 /*or_comma=*/false,
37273 /*consume_paren=*/true);
37274 return list;
37275 }
37276
37277 /* OpenMP 5.0:
37278 device_type ( host | nohost | any ) */
37279
37280 static tree
37281 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
37282 location_t location)
37283 {
37284 tree c;
37285 enum omp_clause_device_type_kind kind;
37286
37287 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37288 return list;
37289
37290 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37291 {
37292 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37293 const char *p = IDENTIFIER_POINTER (id);
37294
37295 if (strcmp ("host", p) == 0)
37296 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
37297 else if (strcmp ("nohost", p) == 0)
37298 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
37299 else if (strcmp ("any", p) == 0)
37300 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
37301 else
37302 goto invalid_kind;
37303 }
37304 else
37305 goto invalid_kind;
37306
37307 cp_lexer_consume_token (parser->lexer);
37308 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
37309 goto resync_fail;
37310
37311 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
37312 /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
37313 location); */
37314 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
37315 OMP_CLAUSE_CHAIN (c) = list;
37316 return c;
37317
37318 invalid_kind:
37319 cp_parser_error (parser, "invalid depend kind");
37320 resync_fail:
37321 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37322 /*or_comma=*/false,
37323 /*consume_paren=*/true);
37324 return list;
37325 }
37326
37327 /* OpenACC:
37328 async [( int-expr )] */
37329
37330 static tree
37331 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
37332 {
37333 tree c, t;
37334 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37335
37336 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
37337
37338 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37339 {
37340 matching_parens parens;
37341 parens.consume_open (parser);
37342
37343 t = cp_parser_expression (parser);
37344 if (t == error_mark_node
37345 || !parens.require_close (parser))
37346 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37347 /*or_comma=*/false,
37348 /*consume_paren=*/true);
37349 }
37350
37351 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
37352
37353 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
37354 OMP_CLAUSE_ASYNC_EXPR (c) = t;
37355 OMP_CLAUSE_CHAIN (c) = list;
37356 list = c;
37357
37358 return list;
37359 }
37360
37361 /* Parse all OpenACC clauses. The set clauses allowed by the directive
37362 is a bitmask in MASK. Return the list of clauses found. */
37363
37364 static tree
37365 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
37366 const char *where, cp_token *pragma_tok,
37367 bool finish_p = true)
37368 {
37369 tree clauses = NULL;
37370 bool first = true;
37371
37372 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37373 {
37374 location_t here;
37375 pragma_omp_clause c_kind;
37376 omp_clause_code code;
37377 const char *c_name;
37378 tree prev = clauses;
37379
37380 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37381 cp_lexer_consume_token (parser->lexer);
37382
37383 here = cp_lexer_peek_token (parser->lexer)->location;
37384 c_kind = cp_parser_omp_clause_name (parser);
37385
37386 switch (c_kind)
37387 {
37388 case PRAGMA_OACC_CLAUSE_ASYNC:
37389 clauses = cp_parser_oacc_clause_async (parser, clauses);
37390 c_name = "async";
37391 break;
37392 case PRAGMA_OACC_CLAUSE_AUTO:
37393 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
37394 clauses);
37395 c_name = "auto";
37396 break;
37397 case PRAGMA_OACC_CLAUSE_ATTACH:
37398 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37399 c_name = "attach";
37400 break;
37401 case PRAGMA_OACC_CLAUSE_COLLAPSE:
37402 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
37403 c_name = "collapse";
37404 break;
37405 case PRAGMA_OACC_CLAUSE_COPY:
37406 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37407 c_name = "copy";
37408 break;
37409 case PRAGMA_OACC_CLAUSE_COPYIN:
37410 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37411 c_name = "copyin";
37412 break;
37413 case PRAGMA_OACC_CLAUSE_COPYOUT:
37414 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37415 c_name = "copyout";
37416 break;
37417 case PRAGMA_OACC_CLAUSE_CREATE:
37418 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37419 c_name = "create";
37420 break;
37421 case PRAGMA_OACC_CLAUSE_DELETE:
37422 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37423 c_name = "delete";
37424 break;
37425 case PRAGMA_OMP_CLAUSE_DEFAULT:
37426 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
37427 c_name = "default";
37428 break;
37429 case PRAGMA_OACC_CLAUSE_DETACH:
37430 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37431 c_name = "detach";
37432 break;
37433 case PRAGMA_OACC_CLAUSE_DEVICE:
37434 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37435 c_name = "device";
37436 break;
37437 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
37438 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
37439 c_name = "deviceptr";
37440 break;
37441 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
37442 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37443 c_name = "device_resident";
37444 break;
37445 case PRAGMA_OACC_CLAUSE_FINALIZE:
37446 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
37447 clauses);
37448 c_name = "finalize";
37449 break;
37450 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
37451 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37452 clauses);
37453 c_name = "firstprivate";
37454 break;
37455 case PRAGMA_OACC_CLAUSE_GANG:
37456 c_name = "gang";
37457 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
37458 c_name, clauses);
37459 break;
37460 case PRAGMA_OACC_CLAUSE_HOST:
37461 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37462 c_name = "host";
37463 break;
37464 case PRAGMA_OACC_CLAUSE_IF:
37465 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
37466 c_name = "if";
37467 break;
37468 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
37469 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
37470 clauses);
37471 c_name = "if_present";
37472 break;
37473 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
37474 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
37475 clauses);
37476 c_name = "independent";
37477 break;
37478 case PRAGMA_OACC_CLAUSE_LINK:
37479 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37480 c_name = "link";
37481 break;
37482 case PRAGMA_OACC_CLAUSE_NO_CREATE:
37483 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37484 c_name = "no_create";
37485 break;
37486 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
37487 code = OMP_CLAUSE_NUM_GANGS;
37488 c_name = "num_gangs";
37489 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
37490 clauses);
37491 break;
37492 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
37493 c_name = "num_workers";
37494 code = OMP_CLAUSE_NUM_WORKERS;
37495 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
37496 clauses);
37497 break;
37498 case PRAGMA_OACC_CLAUSE_PRESENT:
37499 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37500 c_name = "present";
37501 break;
37502 case PRAGMA_OACC_CLAUSE_PRIVATE:
37503 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
37504 clauses);
37505 c_name = "private";
37506 break;
37507 case PRAGMA_OACC_CLAUSE_REDUCTION:
37508 clauses
37509 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
37510 false, clauses);
37511 c_name = "reduction";
37512 break;
37513 case PRAGMA_OACC_CLAUSE_SEQ:
37514 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
37515 clauses);
37516 c_name = "seq";
37517 break;
37518 case PRAGMA_OACC_CLAUSE_TILE:
37519 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
37520 c_name = "tile";
37521 break;
37522 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
37523 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
37524 clauses);
37525 c_name = "use_device";
37526 break;
37527 case PRAGMA_OACC_CLAUSE_VECTOR:
37528 c_name = "vector";
37529 clauses = cp_parser_oacc_shape_clause (parser, here,
37530 OMP_CLAUSE_VECTOR,
37531 c_name, clauses);
37532 break;
37533 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
37534 c_name = "vector_length";
37535 code = OMP_CLAUSE_VECTOR_LENGTH;
37536 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
37537 clauses);
37538 break;
37539 case PRAGMA_OACC_CLAUSE_WAIT:
37540 clauses = cp_parser_oacc_clause_wait (parser, clauses);
37541 c_name = "wait";
37542 break;
37543 case PRAGMA_OACC_CLAUSE_WORKER:
37544 c_name = "worker";
37545 clauses = cp_parser_oacc_shape_clause (parser, here,
37546 OMP_CLAUSE_WORKER,
37547 c_name, clauses);
37548 break;
37549 default:
37550 cp_parser_error (parser, "expected %<#pragma acc%> clause");
37551 goto saw_error;
37552 }
37553
37554 first = false;
37555
37556 if (((mask >> c_kind) & 1) == 0)
37557 {
37558 /* Remove the invalid clause(s) from the list to avoid
37559 confusing the rest of the compiler. */
37560 clauses = prev;
37561 error_at (here, "%qs is not valid for %qs", c_name, where);
37562 }
37563 }
37564
37565 saw_error:
37566 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37567
37568 if (finish_p)
37569 return finish_omp_clauses (clauses, C_ORT_ACC);
37570
37571 return clauses;
37572 }
37573
37574 /* Parse all OpenMP clauses. The set clauses allowed by the directive
37575 is a bitmask in MASK. Return the list of clauses found.
37576 FINISH_P set if finish_omp_clauses should be called.
37577 NESTED non-zero if clauses should be terminated by closing paren instead
37578 of end of pragma. If it is 2, additionally commas are required in between
37579 the clauses. */
37580
37581 static tree
37582 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
37583 const char *where, cp_token *pragma_tok,
37584 bool finish_p = true, int nested = 0)
37585 {
37586 tree clauses = NULL;
37587 bool first = true;
37588 cp_token *token = NULL;
37589
37590 /* Don't create location wrapper nodes within OpenMP clauses. */
37591 auto_suppress_location_wrappers sentinel;
37592
37593 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37594 {
37595 pragma_omp_clause c_kind;
37596 const char *c_name;
37597 tree prev = clauses;
37598
37599 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37600 break;
37601
37602 if (!first)
37603 {
37604 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37605 cp_lexer_consume_token (parser->lexer);
37606 else if (nested == 2)
37607 error_at (cp_lexer_peek_token (parser->lexer)->location,
37608 "clauses in %<simd%> trait should be separated "
37609 "by %<,%>");
37610 }
37611
37612 token = cp_lexer_peek_token (parser->lexer);
37613 c_kind = cp_parser_omp_clause_name (parser);
37614
37615 switch (c_kind)
37616 {
37617 case PRAGMA_OMP_CLAUSE_BIND:
37618 clauses = cp_parser_omp_clause_bind (parser, clauses,
37619 token->location);
37620 c_name = "bind";
37621 break;
37622 case PRAGMA_OMP_CLAUSE_COLLAPSE:
37623 clauses = cp_parser_omp_clause_collapse (parser, clauses,
37624 token->location);
37625 c_name = "collapse";
37626 break;
37627 case PRAGMA_OMP_CLAUSE_COPYIN:
37628 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
37629 c_name = "copyin";
37630 break;
37631 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
37632 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
37633 clauses);
37634 c_name = "copyprivate";
37635 break;
37636 case PRAGMA_OMP_CLAUSE_DEFAULT:
37637 clauses = cp_parser_omp_clause_default (parser, clauses,
37638 token->location, false);
37639 c_name = "default";
37640 break;
37641 case PRAGMA_OMP_CLAUSE_FINAL:
37642 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
37643 c_name = "final";
37644 break;
37645 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
37646 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37647 clauses);
37648 c_name = "firstprivate";
37649 break;
37650 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
37651 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
37652 token->location);
37653 c_name = "grainsize";
37654 break;
37655 case PRAGMA_OMP_CLAUSE_HINT:
37656 clauses = cp_parser_omp_clause_hint (parser, clauses,
37657 token->location);
37658 c_name = "hint";
37659 break;
37660 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
37661 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
37662 token->location);
37663 c_name = "defaultmap";
37664 break;
37665 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
37666 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
37667 clauses);
37668 c_name = "use_device_ptr";
37669 break;
37670 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
37671 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
37672 clauses);
37673 c_name = "use_device_addr";
37674 break;
37675 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
37676 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
37677 clauses);
37678 c_name = "is_device_ptr";
37679 break;
37680 case PRAGMA_OMP_CLAUSE_IF:
37681 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
37682 true);
37683 c_name = "if";
37684 break;
37685 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
37686 clauses
37687 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
37688 true, clauses);
37689 c_name = "in_reduction";
37690 break;
37691 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
37692 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
37693 c_name = "lastprivate";
37694 break;
37695 case PRAGMA_OMP_CLAUSE_MERGEABLE:
37696 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
37697 token->location);
37698 c_name = "mergeable";
37699 break;
37700 case PRAGMA_OMP_CLAUSE_NOWAIT:
37701 clauses = cp_parser_omp_clause_nowait (parser, clauses,
37702 token->location);
37703 c_name = "nowait";
37704 break;
37705 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
37706 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
37707 token->location);
37708 c_name = "num_tasks";
37709 break;
37710 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
37711 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
37712 token->location);
37713 c_name = "num_threads";
37714 break;
37715 case PRAGMA_OMP_CLAUSE_ORDER:
37716 clauses = cp_parser_omp_clause_order (parser, clauses,
37717 token->location);
37718 c_name = "order";
37719 break;
37720 case PRAGMA_OMP_CLAUSE_ORDERED:
37721 clauses = cp_parser_omp_clause_ordered (parser, clauses,
37722 token->location);
37723 c_name = "ordered";
37724 break;
37725 case PRAGMA_OMP_CLAUSE_PRIORITY:
37726 clauses = cp_parser_omp_clause_priority (parser, clauses,
37727 token->location);
37728 c_name = "priority";
37729 break;
37730 case PRAGMA_OMP_CLAUSE_PRIVATE:
37731 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
37732 clauses);
37733 c_name = "private";
37734 break;
37735 case PRAGMA_OMP_CLAUSE_REDUCTION:
37736 clauses
37737 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
37738 true, clauses);
37739 c_name = "reduction";
37740 break;
37741 case PRAGMA_OMP_CLAUSE_SCHEDULE:
37742 clauses = cp_parser_omp_clause_schedule (parser, clauses,
37743 token->location);
37744 c_name = "schedule";
37745 break;
37746 case PRAGMA_OMP_CLAUSE_SHARED:
37747 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
37748 clauses);
37749 c_name = "shared";
37750 break;
37751 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
37752 clauses
37753 = cp_parser_omp_clause_reduction (parser,
37754 OMP_CLAUSE_TASK_REDUCTION,
37755 true, clauses);
37756 c_name = "task_reduction";
37757 break;
37758 case PRAGMA_OMP_CLAUSE_UNTIED:
37759 clauses = cp_parser_omp_clause_untied (parser, clauses,
37760 token->location);
37761 c_name = "untied";
37762 break;
37763 case PRAGMA_OMP_CLAUSE_INBRANCH:
37764 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
37765 clauses, token->location);
37766 c_name = "inbranch";
37767 break;
37768 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
37769 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
37770 clauses);
37771 c_name = "nontemporal";
37772 break;
37773 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
37774 clauses = cp_parser_omp_clause_branch (parser,
37775 OMP_CLAUSE_NOTINBRANCH,
37776 clauses, token->location);
37777 c_name = "notinbranch";
37778 break;
37779 case PRAGMA_OMP_CLAUSE_PARALLEL:
37780 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
37781 clauses, token->location);
37782 c_name = "parallel";
37783 if (!first)
37784 {
37785 clause_not_first:
37786 error_at (token->location, "%qs must be the first clause of %qs",
37787 c_name, where);
37788 clauses = prev;
37789 }
37790 break;
37791 case PRAGMA_OMP_CLAUSE_FOR:
37792 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
37793 clauses, token->location);
37794 c_name = "for";
37795 if (!first)
37796 goto clause_not_first;
37797 break;
37798 case PRAGMA_OMP_CLAUSE_SECTIONS:
37799 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
37800 clauses, token->location);
37801 c_name = "sections";
37802 if (!first)
37803 goto clause_not_first;
37804 break;
37805 case PRAGMA_OMP_CLAUSE_TASKGROUP:
37806 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
37807 clauses, token->location);
37808 c_name = "taskgroup";
37809 if (!first)
37810 goto clause_not_first;
37811 break;
37812 case PRAGMA_OMP_CLAUSE_LINK:
37813 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
37814 c_name = "to";
37815 break;
37816 case PRAGMA_OMP_CLAUSE_TO:
37817 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
37818 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37819 clauses);
37820 else
37821 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
37822 c_name = "to";
37823 break;
37824 case PRAGMA_OMP_CLAUSE_FROM:
37825 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
37826 c_name = "from";
37827 break;
37828 case PRAGMA_OMP_CLAUSE_UNIFORM:
37829 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
37830 clauses);
37831 c_name = "uniform";
37832 break;
37833 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
37834 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
37835 token->location);
37836 c_name = "num_teams";
37837 break;
37838 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
37839 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
37840 token->location);
37841 c_name = "thread_limit";
37842 break;
37843 case PRAGMA_OMP_CLAUSE_ALIGNED:
37844 clauses = cp_parser_omp_clause_aligned (parser, clauses);
37845 c_name = "aligned";
37846 break;
37847 case PRAGMA_OMP_CLAUSE_ALLOCATE:
37848 clauses = cp_parser_omp_clause_allocate (parser, clauses);
37849 c_name = "allocate";
37850 break;
37851 case PRAGMA_OMP_CLAUSE_LINEAR:
37852 {
37853 bool declare_simd = false;
37854 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
37855 declare_simd = true;
37856 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
37857 }
37858 c_name = "linear";
37859 break;
37860 case PRAGMA_OMP_CLAUSE_DEPEND:
37861 clauses = cp_parser_omp_clause_depend (parser, clauses,
37862 token->location);
37863 c_name = "depend";
37864 break;
37865 case PRAGMA_OMP_CLAUSE_MAP:
37866 clauses = cp_parser_omp_clause_map (parser, clauses);
37867 c_name = "map";
37868 break;
37869 case PRAGMA_OMP_CLAUSE_DEVICE:
37870 clauses = cp_parser_omp_clause_device (parser, clauses,
37871 token->location);
37872 c_name = "device";
37873 break;
37874 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
37875 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
37876 token->location);
37877 c_name = "dist_schedule";
37878 break;
37879 case PRAGMA_OMP_CLAUSE_PROC_BIND:
37880 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
37881 token->location);
37882 c_name = "proc_bind";
37883 break;
37884 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
37885 clauses = cp_parser_omp_clause_device_type (parser, clauses,
37886 token->location);
37887 c_name = "device_type";
37888 break;
37889 case PRAGMA_OMP_CLAUSE_SAFELEN:
37890 clauses = cp_parser_omp_clause_safelen (parser, clauses,
37891 token->location);
37892 c_name = "safelen";
37893 break;
37894 case PRAGMA_OMP_CLAUSE_SIMDLEN:
37895 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
37896 token->location);
37897 c_name = "simdlen";
37898 break;
37899 case PRAGMA_OMP_CLAUSE_NOGROUP:
37900 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
37901 token->location);
37902 c_name = "nogroup";
37903 break;
37904 case PRAGMA_OMP_CLAUSE_THREADS:
37905 clauses
37906 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
37907 clauses, token->location);
37908 c_name = "threads";
37909 break;
37910 case PRAGMA_OMP_CLAUSE_SIMD:
37911 clauses
37912 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
37913 clauses, token->location);
37914 c_name = "simd";
37915 break;
37916 default:
37917 cp_parser_error (parser, "expected %<#pragma omp%> clause");
37918 goto saw_error;
37919 }
37920
37921 first = false;
37922
37923 if (((mask >> c_kind) & 1) == 0)
37924 {
37925 /* Remove the invalid clause(s) from the list to avoid
37926 confusing the rest of the compiler. */
37927 clauses = prev;
37928 error_at (token->location, "%qs is not valid for %qs", c_name, where);
37929 }
37930 }
37931 saw_error:
37932 if (!nested)
37933 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37934 if (finish_p)
37935 {
37936 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
37937 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
37938 else
37939 return finish_omp_clauses (clauses, C_ORT_OMP);
37940 }
37941 return clauses;
37942 }
37943
37944 /* OpenMP 2.5:
37945 structured-block:
37946 statement
37947
37948 In practice, we're also interested in adding the statement to an
37949 outer node. So it is convenient if we work around the fact that
37950 cp_parser_statement calls add_stmt. */
37951
37952 static unsigned
37953 cp_parser_begin_omp_structured_block (cp_parser *parser)
37954 {
37955 unsigned save = parser->in_statement;
37956
37957 /* Only move the values to IN_OMP_BLOCK if they weren't false.
37958 This preserves the "not within loop or switch" style error messages
37959 for nonsense cases like
37960 void foo() {
37961 #pragma omp single
37962 break;
37963 }
37964 */
37965 if (parser->in_statement)
37966 parser->in_statement = IN_OMP_BLOCK;
37967
37968 return save;
37969 }
37970
37971 static void
37972 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
37973 {
37974 parser->in_statement = save;
37975 }
37976
37977 static tree
37978 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
37979 {
37980 tree stmt = begin_omp_structured_block ();
37981 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37982
37983 cp_parser_statement (parser, NULL_TREE, false, if_p);
37984
37985 cp_parser_end_omp_structured_block (parser, save);
37986 return finish_omp_structured_block (stmt);
37987 }
37988
37989 /* OpenMP 2.5:
37990 # pragma omp atomic new-line
37991 expression-stmt
37992
37993 expression-stmt:
37994 x binop= expr | x++ | ++x | x-- | --x
37995 binop:
37996 +, *, -, /, &, ^, |, <<, >>
37997
37998 where x is an lvalue expression with scalar type.
37999
38000 OpenMP 3.1:
38001 # pragma omp atomic new-line
38002 update-stmt
38003
38004 # pragma omp atomic read new-line
38005 read-stmt
38006
38007 # pragma omp atomic write new-line
38008 write-stmt
38009
38010 # pragma omp atomic update new-line
38011 update-stmt
38012
38013 # pragma omp atomic capture new-line
38014 capture-stmt
38015
38016 # pragma omp atomic capture new-line
38017 capture-block
38018
38019 read-stmt:
38020 v = x
38021 write-stmt:
38022 x = expr
38023 update-stmt:
38024 expression-stmt | x = x binop expr
38025 capture-stmt:
38026 v = expression-stmt
38027 capture-block:
38028 { v = x; update-stmt; } | { update-stmt; v = x; }
38029
38030 OpenMP 4.0:
38031 update-stmt:
38032 expression-stmt | x = x binop expr | x = expr binop x
38033 capture-stmt:
38034 v = update-stmt
38035 capture-block:
38036 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
38037
38038 where x and v are lvalue expressions with scalar type. */
38039
38040 static void
38041 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok, bool openacc)
38042 {
38043 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
38044 tree rhs1 = NULL_TREE, orig_lhs;
38045 location_t loc = pragma_tok->location;
38046 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
38047 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
38048 bool structured_block = false;
38049 bool first = true;
38050 tree clauses = NULL_TREE;
38051
38052 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38053 {
38054 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38055 cp_lexer_consume_token (parser->lexer);
38056
38057 first = false;
38058
38059 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38060 {
38061 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38062 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
38063 const char *p = IDENTIFIER_POINTER (id);
38064 enum tree_code new_code = ERROR_MARK;
38065 enum omp_memory_order new_memory_order
38066 = OMP_MEMORY_ORDER_UNSPECIFIED;
38067
38068 if (!strcmp (p, "read"))
38069 new_code = OMP_ATOMIC_READ;
38070 else if (!strcmp (p, "write"))
38071 new_code = NOP_EXPR;
38072 else if (!strcmp (p, "update"))
38073 new_code = OMP_ATOMIC;
38074 else if (!strcmp (p, "capture"))
38075 new_code = OMP_ATOMIC_CAPTURE_NEW;
38076 else if (openacc)
38077 {
38078 p = NULL;
38079 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
38080 "or %<capture%> clause");
38081 }
38082 else if (!strcmp (p, "seq_cst"))
38083 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
38084 else if (!strcmp (p, "acq_rel"))
38085 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
38086 else if (!strcmp (p, "release"))
38087 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
38088 else if (!strcmp (p, "acquire"))
38089 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
38090 else if (!strcmp (p, "relaxed"))
38091 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
38092 else if (!strcmp (p, "hint"))
38093 {
38094 cp_lexer_consume_token (parser->lexer);
38095 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
38096 continue;
38097 }
38098 else
38099 {
38100 p = NULL;
38101 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
38102 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
38103 "%<release%>, %<relaxed%> or %<hint%> clause");
38104 }
38105 if (p)
38106 {
38107 if (new_code != ERROR_MARK)
38108 {
38109 /* OpenACC permits 'update capture'. */
38110 if (openacc
38111 && code == OMP_ATOMIC
38112 && new_code == OMP_ATOMIC_CAPTURE_NEW)
38113 code = new_code;
38114 else if (code != ERROR_MARK)
38115 error_at (cloc, "too many atomic clauses");
38116 else
38117 code = new_code;
38118 }
38119 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
38120 {
38121 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
38122 error_at (cloc, "too many memory order clauses");
38123 else
38124 memory_order = new_memory_order;
38125 }
38126 cp_lexer_consume_token (parser->lexer);
38127 continue;
38128 }
38129 }
38130 break;
38131 }
38132 cp_parser_require_pragma_eol (parser, pragma_tok);
38133
38134 if (code == ERROR_MARK)
38135 code = OMP_ATOMIC;
38136 if (openacc)
38137 memory_order = OMP_MEMORY_ORDER_RELAXED;
38138 else if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
38139 {
38140 omp_requires_mask
38141 = (enum omp_requires) (omp_requires_mask
38142 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
38143 switch ((enum omp_memory_order)
38144 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
38145 {
38146 case OMP_MEMORY_ORDER_UNSPECIFIED:
38147 case OMP_MEMORY_ORDER_RELAXED:
38148 memory_order = OMP_MEMORY_ORDER_RELAXED;
38149 break;
38150 case OMP_MEMORY_ORDER_SEQ_CST:
38151 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
38152 break;
38153 case OMP_MEMORY_ORDER_ACQ_REL:
38154 switch (code)
38155 {
38156 case OMP_ATOMIC_READ:
38157 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
38158 break;
38159 case NOP_EXPR: /* atomic write */
38160 case OMP_ATOMIC:
38161 memory_order = OMP_MEMORY_ORDER_RELEASE;
38162 break;
38163 default:
38164 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
38165 break;
38166 }
38167 break;
38168 default:
38169 gcc_unreachable ();
38170 }
38171 }
38172 else
38173 switch (code)
38174 {
38175 case OMP_ATOMIC_READ:
38176 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
38177 || memory_order == OMP_MEMORY_ORDER_RELEASE)
38178 {
38179 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
38180 "%<acq_rel%> or %<release%> clauses");
38181 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
38182 }
38183 break;
38184 case NOP_EXPR: /* atomic write */
38185 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
38186 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
38187 {
38188 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
38189 "%<acq_rel%> or %<acquire%> clauses");
38190 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
38191 }
38192 break;
38193 case OMP_ATOMIC:
38194 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
38195 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
38196 {
38197 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
38198 "%<acq_rel%> or %<acquire%> clauses");
38199 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
38200 }
38201 break;
38202 default:
38203 break;
38204 }
38205
38206 switch (code)
38207 {
38208 case OMP_ATOMIC_READ:
38209 case NOP_EXPR: /* atomic write */
38210 v = cp_parser_unary_expression (parser);
38211 if (v == error_mark_node)
38212 goto saw_error;
38213 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
38214 goto saw_error;
38215 if (code == NOP_EXPR)
38216 lhs = cp_parser_expression (parser);
38217 else
38218 lhs = cp_parser_unary_expression (parser);
38219 if (lhs == error_mark_node)
38220 goto saw_error;
38221 if (code == NOP_EXPR)
38222 {
38223 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
38224 opcode. */
38225 code = OMP_ATOMIC;
38226 rhs = lhs;
38227 lhs = v;
38228 v = NULL_TREE;
38229 }
38230 goto done;
38231 case OMP_ATOMIC_CAPTURE_NEW:
38232 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
38233 {
38234 cp_lexer_consume_token (parser->lexer);
38235 structured_block = true;
38236 }
38237 else
38238 {
38239 v = cp_parser_unary_expression (parser);
38240 if (v == error_mark_node)
38241 goto saw_error;
38242 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
38243 goto saw_error;
38244 }
38245 default:
38246 break;
38247 }
38248
38249 restart:
38250 lhs = cp_parser_unary_expression (parser);
38251 orig_lhs = lhs;
38252 switch (TREE_CODE (lhs))
38253 {
38254 case ERROR_MARK:
38255 goto saw_error;
38256
38257 case POSTINCREMENT_EXPR:
38258 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
38259 code = OMP_ATOMIC_CAPTURE_OLD;
38260 /* FALLTHROUGH */
38261 case PREINCREMENT_EXPR:
38262 lhs = TREE_OPERAND (lhs, 0);
38263 opcode = PLUS_EXPR;
38264 rhs = integer_one_node;
38265 break;
38266
38267 case POSTDECREMENT_EXPR:
38268 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
38269 code = OMP_ATOMIC_CAPTURE_OLD;
38270 /* FALLTHROUGH */
38271 case PREDECREMENT_EXPR:
38272 lhs = TREE_OPERAND (lhs, 0);
38273 opcode = MINUS_EXPR;
38274 rhs = integer_one_node;
38275 break;
38276
38277 case COMPOUND_EXPR:
38278 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
38279 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
38280 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
38281 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
38282 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
38283 (TREE_OPERAND (lhs, 1), 0), 0)))
38284 == BOOLEAN_TYPE)
38285 /* Undo effects of boolean_increment for post {in,de}crement. */
38286 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
38287 /* FALLTHRU */
38288 case MODIFY_EXPR:
38289 if (TREE_CODE (lhs) == MODIFY_EXPR
38290 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
38291 {
38292 /* Undo effects of boolean_increment. */
38293 if (integer_onep (TREE_OPERAND (lhs, 1)))
38294 {
38295 /* This is pre or post increment. */
38296 rhs = TREE_OPERAND (lhs, 1);
38297 lhs = TREE_OPERAND (lhs, 0);
38298 opcode = NOP_EXPR;
38299 if (code == OMP_ATOMIC_CAPTURE_NEW
38300 && !structured_block
38301 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
38302 code = OMP_ATOMIC_CAPTURE_OLD;
38303 break;
38304 }
38305 }
38306 /* FALLTHRU */
38307 default:
38308 switch (cp_lexer_peek_token (parser->lexer)->type)
38309 {
38310 case CPP_MULT_EQ:
38311 opcode = MULT_EXPR;
38312 break;
38313 case CPP_DIV_EQ:
38314 opcode = TRUNC_DIV_EXPR;
38315 break;
38316 case CPP_PLUS_EQ:
38317 opcode = PLUS_EXPR;
38318 break;
38319 case CPP_MINUS_EQ:
38320 opcode = MINUS_EXPR;
38321 break;
38322 case CPP_LSHIFT_EQ:
38323 opcode = LSHIFT_EXPR;
38324 break;
38325 case CPP_RSHIFT_EQ:
38326 opcode = RSHIFT_EXPR;
38327 break;
38328 case CPP_AND_EQ:
38329 opcode = BIT_AND_EXPR;
38330 break;
38331 case CPP_OR_EQ:
38332 opcode = BIT_IOR_EXPR;
38333 break;
38334 case CPP_XOR_EQ:
38335 opcode = BIT_XOR_EXPR;
38336 break;
38337 case CPP_EQ:
38338 enum cp_parser_prec oprec;
38339 cp_token *token;
38340 cp_lexer_consume_token (parser->lexer);
38341 cp_parser_parse_tentatively (parser);
38342 rhs1 = cp_parser_simple_cast_expression (parser);
38343 if (rhs1 == error_mark_node)
38344 {
38345 cp_parser_abort_tentative_parse (parser);
38346 cp_parser_simple_cast_expression (parser);
38347 goto saw_error;
38348 }
38349 token = cp_lexer_peek_token (parser->lexer);
38350 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
38351 {
38352 cp_parser_abort_tentative_parse (parser);
38353 cp_parser_parse_tentatively (parser);
38354 rhs = cp_parser_binary_expression (parser, false, true,
38355 PREC_NOT_OPERATOR, NULL);
38356 if (rhs == error_mark_node)
38357 {
38358 cp_parser_abort_tentative_parse (parser);
38359 cp_parser_binary_expression (parser, false, true,
38360 PREC_NOT_OPERATOR, NULL);
38361 goto saw_error;
38362 }
38363 switch (TREE_CODE (rhs))
38364 {
38365 case MULT_EXPR:
38366 case TRUNC_DIV_EXPR:
38367 case RDIV_EXPR:
38368 case PLUS_EXPR:
38369 case MINUS_EXPR:
38370 case LSHIFT_EXPR:
38371 case RSHIFT_EXPR:
38372 case BIT_AND_EXPR:
38373 case BIT_IOR_EXPR:
38374 case BIT_XOR_EXPR:
38375 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
38376 {
38377 if (cp_parser_parse_definitely (parser))
38378 {
38379 opcode = TREE_CODE (rhs);
38380 rhs1 = TREE_OPERAND (rhs, 0);
38381 rhs = TREE_OPERAND (rhs, 1);
38382 goto stmt_done;
38383 }
38384 else
38385 goto saw_error;
38386 }
38387 break;
38388 default:
38389 break;
38390 }
38391 cp_parser_abort_tentative_parse (parser);
38392 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
38393 {
38394 rhs = cp_parser_expression (parser);
38395 if (rhs == error_mark_node)
38396 goto saw_error;
38397 opcode = NOP_EXPR;
38398 rhs1 = NULL_TREE;
38399 goto stmt_done;
38400 }
38401 cp_parser_error (parser,
38402 "invalid form of %<#pragma omp atomic%>");
38403 goto saw_error;
38404 }
38405 if (!cp_parser_parse_definitely (parser))
38406 goto saw_error;
38407 switch (token->type)
38408 {
38409 case CPP_SEMICOLON:
38410 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
38411 {
38412 code = OMP_ATOMIC_CAPTURE_OLD;
38413 v = lhs;
38414 lhs = NULL_TREE;
38415 lhs1 = rhs1;
38416 rhs1 = NULL_TREE;
38417 cp_lexer_consume_token (parser->lexer);
38418 goto restart;
38419 }
38420 else if (structured_block)
38421 {
38422 opcode = NOP_EXPR;
38423 rhs = rhs1;
38424 rhs1 = NULL_TREE;
38425 goto stmt_done;
38426 }
38427 cp_parser_error (parser,
38428 "invalid form of %<#pragma omp atomic%>");
38429 goto saw_error;
38430 case CPP_MULT:
38431 opcode = MULT_EXPR;
38432 break;
38433 case CPP_DIV:
38434 opcode = TRUNC_DIV_EXPR;
38435 break;
38436 case CPP_PLUS:
38437 opcode = PLUS_EXPR;
38438 break;
38439 case CPP_MINUS:
38440 opcode = MINUS_EXPR;
38441 break;
38442 case CPP_LSHIFT:
38443 opcode = LSHIFT_EXPR;
38444 break;
38445 case CPP_RSHIFT:
38446 opcode = RSHIFT_EXPR;
38447 break;
38448 case CPP_AND:
38449 opcode = BIT_AND_EXPR;
38450 break;
38451 case CPP_OR:
38452 opcode = BIT_IOR_EXPR;
38453 break;
38454 case CPP_XOR:
38455 opcode = BIT_XOR_EXPR;
38456 break;
38457 default:
38458 cp_parser_error (parser,
38459 "invalid operator for %<#pragma omp atomic%>");
38460 goto saw_error;
38461 }
38462 oprec = TOKEN_PRECEDENCE (token);
38463 gcc_assert (oprec != PREC_NOT_OPERATOR);
38464 if (commutative_tree_code (opcode))
38465 oprec = (enum cp_parser_prec) (oprec - 1);
38466 cp_lexer_consume_token (parser->lexer);
38467 rhs = cp_parser_binary_expression (parser, false, false,
38468 oprec, NULL);
38469 if (rhs == error_mark_node)
38470 goto saw_error;
38471 goto stmt_done;
38472 /* FALLTHROUGH */
38473 default:
38474 cp_parser_error (parser,
38475 "invalid operator for %<#pragma omp atomic%>");
38476 goto saw_error;
38477 }
38478 cp_lexer_consume_token (parser->lexer);
38479
38480 rhs = cp_parser_expression (parser);
38481 if (rhs == error_mark_node)
38482 goto saw_error;
38483 break;
38484 }
38485 stmt_done:
38486 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
38487 {
38488 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
38489 goto saw_error;
38490 v = cp_parser_unary_expression (parser);
38491 if (v == error_mark_node)
38492 goto saw_error;
38493 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
38494 goto saw_error;
38495 lhs1 = cp_parser_unary_expression (parser);
38496 if (lhs1 == error_mark_node)
38497 goto saw_error;
38498 }
38499 if (structured_block)
38500 {
38501 cp_parser_consume_semicolon_at_end_of_statement (parser);
38502 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
38503 }
38504 done:
38505 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
38506 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
38507 rhs1, clauses, memory_order);
38508 if (!structured_block)
38509 cp_parser_consume_semicolon_at_end_of_statement (parser);
38510 return;
38511
38512 saw_error:
38513 cp_parser_skip_to_end_of_block_or_statement (parser);
38514 if (structured_block)
38515 {
38516 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
38517 cp_lexer_consume_token (parser->lexer);
38518 else if (code == OMP_ATOMIC_CAPTURE_NEW)
38519 {
38520 cp_parser_skip_to_end_of_block_or_statement (parser);
38521 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
38522 cp_lexer_consume_token (parser->lexer);
38523 }
38524 }
38525 }
38526
38527
38528 /* OpenMP 2.5:
38529 # pragma omp barrier new-line */
38530
38531 static void
38532 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
38533 {
38534 cp_parser_require_pragma_eol (parser, pragma_tok);
38535 finish_omp_barrier ();
38536 }
38537
38538 /* OpenMP 2.5:
38539 # pragma omp critical [(name)] new-line
38540 structured-block
38541
38542 OpenMP 4.5:
38543 # pragma omp critical [(name) [hint(expression)]] new-line
38544 structured-block */
38545
38546 #define OMP_CRITICAL_CLAUSE_MASK \
38547 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
38548
38549 static tree
38550 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38551 {
38552 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
38553
38554 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38555 {
38556 matching_parens parens;
38557 parens.consume_open (parser);
38558
38559 name = cp_parser_identifier (parser);
38560
38561 if (name == error_mark_node
38562 || !parens.require_close (parser))
38563 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38564 /*or_comma=*/false,
38565 /*consume_paren=*/true);
38566 if (name == error_mark_node)
38567 name = NULL;
38568
38569 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
38570 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
38571 cp_lexer_consume_token (parser->lexer);
38572 }
38573
38574 clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
38575 "#pragma omp critical", pragma_tok);
38576
38577 stmt = cp_parser_omp_structured_block (parser, if_p);
38578 return c_finish_omp_critical (input_location, stmt, name, clauses);
38579 }
38580
38581 /* OpenMP 5.0:
38582 # pragma omp depobj ( depobj ) depobj-clause new-line
38583
38584 depobj-clause:
38585 depend (dependence-type : locator)
38586 destroy
38587 update (dependence-type)
38588
38589 dependence-type:
38590 in
38591 out
38592 inout
38593 mutexinout */
38594
38595 static void
38596 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
38597 {
38598 location_t loc = pragma_tok->location;
38599 matching_parens parens;
38600 if (!parens.require_open (parser))
38601 {
38602 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38603 return;
38604 }
38605
38606 tree depobj = cp_parser_assignment_expression (parser);
38607
38608 if (!parens.require_close (parser))
38609 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38610 /*or_comma=*/false,
38611 /*consume_paren=*/true);
38612
38613 tree clause = NULL_TREE;
38614 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
38615 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
38616 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38617 {
38618 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38619 const char *p = IDENTIFIER_POINTER (id);
38620
38621 cp_lexer_consume_token (parser->lexer);
38622 if (!strcmp ("depend", p))
38623 {
38624 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
38625 if (clause)
38626 clause = finish_omp_clauses (clause, C_ORT_OMP);
38627 if (!clause)
38628 clause = error_mark_node;
38629 }
38630 else if (!strcmp ("destroy", p))
38631 kind = OMP_CLAUSE_DEPEND_LAST;
38632 else if (!strcmp ("update", p))
38633 {
38634 matching_parens c_parens;
38635 if (c_parens.require_open (parser))
38636 {
38637 location_t c2_loc
38638 = cp_lexer_peek_token (parser->lexer)->location;
38639 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38640 {
38641 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
38642 const char *p2 = IDENTIFIER_POINTER (id2);
38643
38644 cp_lexer_consume_token (parser->lexer);
38645 if (!strcmp ("in", p2))
38646 kind = OMP_CLAUSE_DEPEND_IN;
38647 else if (!strcmp ("out", p2))
38648 kind = OMP_CLAUSE_DEPEND_OUT;
38649 else if (!strcmp ("inout", p2))
38650 kind = OMP_CLAUSE_DEPEND_INOUT;
38651 else if (!strcmp ("mutexinoutset", p2))
38652 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
38653 }
38654 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
38655 {
38656 clause = error_mark_node;
38657 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
38658 "%<mutexinoutset%>");
38659 }
38660 if (!c_parens.require_close (parser))
38661 cp_parser_skip_to_closing_parenthesis (parser,
38662 /*recovering=*/true,
38663 /*or_comma=*/false,
38664 /*consume_paren=*/true);
38665 }
38666 else
38667 clause = error_mark_node;
38668 }
38669 }
38670 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
38671 {
38672 clause = error_mark_node;
38673 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
38674 }
38675 cp_parser_require_pragma_eol (parser, pragma_tok);
38676
38677 finish_omp_depobj (loc, depobj, kind, clause);
38678 }
38679
38680
38681 /* OpenMP 2.5:
38682 # pragma omp flush flush-vars[opt] new-line
38683
38684 flush-vars:
38685 ( variable-list )
38686
38687 OpenMP 5.0:
38688 # pragma omp flush memory-order-clause new-line */
38689
38690 static void
38691 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
38692 {
38693 enum memmodel mo = MEMMODEL_LAST;
38694 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38695 {
38696 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38697 const char *p = IDENTIFIER_POINTER (id);
38698 if (!strcmp (p, "acq_rel"))
38699 mo = MEMMODEL_ACQ_REL;
38700 else if (!strcmp (p, "release"))
38701 mo = MEMMODEL_RELEASE;
38702 else if (!strcmp (p, "acquire"))
38703 mo = MEMMODEL_ACQUIRE;
38704 else
38705 error_at (cp_lexer_peek_token (parser->lexer)->location,
38706 "expected %<acq_rel%>, %<release%> or %<acquire%>");
38707 cp_lexer_consume_token (parser->lexer);
38708 }
38709 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38710 {
38711 if (mo != MEMMODEL_LAST)
38712 error_at (cp_lexer_peek_token (parser->lexer)->location,
38713 "%<flush%> list specified together with memory order "
38714 "clause");
38715 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
38716 }
38717 cp_parser_require_pragma_eol (parser, pragma_tok);
38718
38719 finish_omp_flush (mo);
38720 }
38721
38722 /* Helper function, to parse omp for increment expression. */
38723
38724 static tree
38725 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
38726 {
38727 tree cond = cp_parser_binary_expression (parser, false, true,
38728 PREC_NOT_OPERATOR, NULL);
38729 if (cond == error_mark_node
38730 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
38731 {
38732 cp_parser_skip_to_end_of_statement (parser);
38733 return error_mark_node;
38734 }
38735
38736 switch (TREE_CODE (cond))
38737 {
38738 case GT_EXPR:
38739 case GE_EXPR:
38740 case LT_EXPR:
38741 case LE_EXPR:
38742 break;
38743 case NE_EXPR:
38744 if (code != OACC_LOOP)
38745 break;
38746 gcc_fallthrough ();
38747 default:
38748 return error_mark_node;
38749 }
38750
38751 /* If decl is an iterator, preserve LHS and RHS of the relational
38752 expr until finish_omp_for. */
38753 if (decl
38754 && (type_dependent_expression_p (decl)
38755 || CLASS_TYPE_P (TREE_TYPE (decl))))
38756 return cond;
38757
38758 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
38759 TREE_CODE (cond),
38760 TREE_OPERAND (cond, 0), ERROR_MARK,
38761 TREE_OPERAND (cond, 1), ERROR_MARK,
38762 /*overload=*/NULL, tf_warning_or_error);
38763 }
38764
38765 /* Helper function, to parse omp for increment expression. */
38766
38767 static tree
38768 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
38769 {
38770 cp_token *token = cp_lexer_peek_token (parser->lexer);
38771 enum tree_code op;
38772 tree lhs, rhs;
38773 cp_id_kind idk;
38774 bool decl_first;
38775
38776 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
38777 {
38778 op = (token->type == CPP_PLUS_PLUS
38779 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
38780 cp_lexer_consume_token (parser->lexer);
38781 lhs = cp_parser_simple_cast_expression (parser);
38782 if (lhs != decl
38783 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
38784 return error_mark_node;
38785 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
38786 }
38787
38788 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
38789 if (lhs != decl
38790 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
38791 return error_mark_node;
38792
38793 token = cp_lexer_peek_token (parser->lexer);
38794 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
38795 {
38796 op = (token->type == CPP_PLUS_PLUS
38797 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
38798 cp_lexer_consume_token (parser->lexer);
38799 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
38800 }
38801
38802 op = cp_parser_assignment_operator_opt (parser);
38803 if (op == ERROR_MARK)
38804 return error_mark_node;
38805
38806 if (op != NOP_EXPR)
38807 {
38808 rhs = cp_parser_assignment_expression (parser);
38809 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
38810 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
38811 }
38812
38813 lhs = cp_parser_binary_expression (parser, false, false,
38814 PREC_ADDITIVE_EXPRESSION, NULL);
38815 token = cp_lexer_peek_token (parser->lexer);
38816 decl_first = (lhs == decl
38817 || (processing_template_decl && cp_tree_equal (lhs, decl)));
38818 if (decl_first)
38819 lhs = NULL_TREE;
38820 if (token->type != CPP_PLUS
38821 && token->type != CPP_MINUS)
38822 return error_mark_node;
38823
38824 do
38825 {
38826 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
38827 cp_lexer_consume_token (parser->lexer);
38828 rhs = cp_parser_binary_expression (parser, false, false,
38829 PREC_ADDITIVE_EXPRESSION, NULL);
38830 token = cp_lexer_peek_token (parser->lexer);
38831 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
38832 {
38833 if (lhs == NULL_TREE)
38834 {
38835 if (op == PLUS_EXPR)
38836 lhs = rhs;
38837 else
38838 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
38839 tf_warning_or_error);
38840 }
38841 else
38842 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
38843 ERROR_MARK, NULL, tf_warning_or_error);
38844 }
38845 }
38846 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
38847
38848 if (!decl_first)
38849 {
38850 if ((rhs != decl
38851 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
38852 || op == MINUS_EXPR)
38853 return error_mark_node;
38854 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
38855 }
38856 else
38857 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
38858
38859 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
38860 }
38861
38862 /* Parse the initialization statement of an OpenMP for loop.
38863
38864 Return true if the resulting construct should have an
38865 OMP_CLAUSE_PRIVATE added to it. */
38866
38867 static tree
38868 cp_parser_omp_for_loop_init (cp_parser *parser,
38869 tree &this_pre_body,
38870 releasing_vec &for_block,
38871 tree &init,
38872 tree &orig_init,
38873 tree &decl,
38874 tree &real_decl)
38875 {
38876 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
38877 return NULL_TREE;
38878
38879 tree add_private_clause = NULL_TREE;
38880
38881 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
38882
38883 init-expr:
38884 var = lb
38885 integer-type var = lb
38886 random-access-iterator-type var = lb
38887 pointer-type var = lb
38888 */
38889 cp_decl_specifier_seq type_specifiers;
38890
38891 /* First, try to parse as an initialized declaration. See
38892 cp_parser_condition, from whence the bulk of this is copied. */
38893
38894 cp_parser_parse_tentatively (parser);
38895 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
38896 /*is_declaration=*/true,
38897 /*is_trailing_return=*/false,
38898 &type_specifiers);
38899 if (cp_parser_parse_definitely (parser))
38900 {
38901 /* If parsing a type specifier seq succeeded, then this
38902 MUST be a initialized declaration. */
38903 tree asm_specification, attributes;
38904 cp_declarator *declarator;
38905
38906 declarator = cp_parser_declarator (parser,
38907 CP_PARSER_DECLARATOR_NAMED,
38908 CP_PARSER_FLAGS_NONE,
38909 /*ctor_dtor_or_conv_p=*/NULL,
38910 /*parenthesized_p=*/NULL,
38911 /*member_p=*/false,
38912 /*friend_p=*/false,
38913 /*static_p=*/false);
38914 attributes = cp_parser_attributes_opt (parser);
38915 asm_specification = cp_parser_asm_specification_opt (parser);
38916
38917 if (declarator == cp_error_declarator)
38918 cp_parser_skip_to_end_of_statement (parser);
38919
38920 else
38921 {
38922 tree pushed_scope, auto_node;
38923
38924 decl = start_decl (declarator, &type_specifiers,
38925 SD_INITIALIZED, attributes,
38926 /*prefix_attributes=*/NULL_TREE,
38927 &pushed_scope);
38928
38929 auto_node = type_uses_auto (TREE_TYPE (decl));
38930 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
38931 {
38932 if (cp_lexer_next_token_is (parser->lexer,
38933 CPP_OPEN_PAREN))
38934 error ("parenthesized initialization is not allowed in "
38935 "OpenMP %<for%> loop");
38936 else
38937 /* Trigger an error. */
38938 cp_parser_require (parser, CPP_EQ, RT_EQ);
38939
38940 init = error_mark_node;
38941 cp_parser_skip_to_end_of_statement (parser);
38942 }
38943 else if (CLASS_TYPE_P (TREE_TYPE (decl))
38944 || type_dependent_expression_p (decl)
38945 || auto_node)
38946 {
38947 bool is_direct_init, is_non_constant_init;
38948
38949 init = cp_parser_initializer (parser,
38950 &is_direct_init,
38951 &is_non_constant_init);
38952
38953 if (auto_node)
38954 {
38955 TREE_TYPE (decl)
38956 = do_auto_deduction (TREE_TYPE (decl), init,
38957 auto_node);
38958
38959 if (!CLASS_TYPE_P (TREE_TYPE (decl))
38960 && !type_dependent_expression_p (decl))
38961 goto non_class;
38962 }
38963
38964 cp_finish_decl (decl, init, !is_non_constant_init,
38965 asm_specification,
38966 LOOKUP_ONLYCONVERTING);
38967 orig_init = init;
38968 if (CLASS_TYPE_P (TREE_TYPE (decl)))
38969 {
38970 vec_safe_push (for_block, this_pre_body);
38971 init = NULL_TREE;
38972 }
38973 else
38974 {
38975 init = pop_stmt_list (this_pre_body);
38976 if (init && TREE_CODE (init) == STATEMENT_LIST)
38977 {
38978 tree_stmt_iterator i = tsi_start (init);
38979 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
38980 while (!tsi_end_p (i))
38981 {
38982 tree t = tsi_stmt (i);
38983 if (TREE_CODE (t) == DECL_EXPR
38984 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
38985 {
38986 tsi_delink (&i);
38987 vec_safe_push (for_block, t);
38988 continue;
38989 }
38990 break;
38991 }
38992 if (tsi_one_before_end_p (i))
38993 {
38994 tree t = tsi_stmt (i);
38995 tsi_delink (&i);
38996 free_stmt_list (init);
38997 init = t;
38998 }
38999 }
39000 }
39001 this_pre_body = NULL_TREE;
39002 }
39003 else
39004 {
39005 /* Consume '='. */
39006 cp_lexer_consume_token (parser->lexer);
39007 init = cp_parser_assignment_expression (parser);
39008
39009 non_class:
39010 if (TYPE_REF_P (TREE_TYPE (decl)))
39011 init = error_mark_node;
39012 else
39013 cp_finish_decl (decl, NULL_TREE,
39014 /*init_const_expr_p=*/false,
39015 asm_specification,
39016 LOOKUP_ONLYCONVERTING);
39017 }
39018
39019 if (pushed_scope)
39020 pop_scope (pushed_scope);
39021 }
39022 }
39023 else
39024 {
39025 cp_id_kind idk;
39026 /* If parsing a type specifier sequence failed, then
39027 this MUST be a simple expression. */
39028 cp_parser_parse_tentatively (parser);
39029 decl = cp_parser_primary_expression (parser, false, false,
39030 false, &idk);
39031 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
39032 if (!cp_parser_error_occurred (parser)
39033 && decl
39034 && (TREE_CODE (decl) == COMPONENT_REF
39035 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
39036 {
39037 cp_parser_abort_tentative_parse (parser);
39038 cp_parser_parse_tentatively (parser);
39039 cp_token *token = cp_lexer_peek_token (parser->lexer);
39040 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
39041 /*check_dependency_p=*/true,
39042 /*template_p=*/NULL,
39043 /*declarator_p=*/false,
39044 /*optional_p=*/false);
39045 if (name != error_mark_node
39046 && last_tok == cp_lexer_peek_token (parser->lexer))
39047 {
39048 decl = cp_parser_lookup_name_simple (parser, name,
39049 token->location);
39050 if (TREE_CODE (decl) == FIELD_DECL)
39051 add_private_clause = omp_privatize_field (decl, false);
39052 }
39053 cp_parser_abort_tentative_parse (parser);
39054 cp_parser_parse_tentatively (parser);
39055 decl = cp_parser_primary_expression (parser, false, false,
39056 false, &idk);
39057 }
39058 if (!cp_parser_error_occurred (parser)
39059 && decl
39060 && DECL_P (decl)
39061 && CLASS_TYPE_P (TREE_TYPE (decl)))
39062 {
39063 tree rhs;
39064
39065 cp_parser_parse_definitely (parser);
39066 cp_parser_require (parser, CPP_EQ, RT_EQ);
39067 rhs = cp_parser_assignment_expression (parser);
39068 orig_init = rhs;
39069 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
39070 decl, NOP_EXPR,
39071 rhs,
39072 tf_warning_or_error));
39073 if (!add_private_clause)
39074 add_private_clause = decl;
39075 }
39076 else
39077 {
39078 decl = NULL;
39079 cp_parser_abort_tentative_parse (parser);
39080 init = cp_parser_expression (parser);
39081 if (init)
39082 {
39083 if (TREE_CODE (init) == MODIFY_EXPR
39084 || TREE_CODE (init) == MODOP_EXPR)
39085 real_decl = TREE_OPERAND (init, 0);
39086 }
39087 }
39088 }
39089 return add_private_clause;
39090 }
39091
39092 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
39093
39094 void
39095 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
39096 tree &decl, tree &orig_decl, tree &init,
39097 tree &orig_init, tree &cond, tree &incr)
39098 {
39099 tree begin, end, range_temp_decl = NULL_TREE;
39100 tree iter_type, begin_expr, end_expr;
39101
39102 if (processing_template_decl)
39103 {
39104 if (check_for_bare_parameter_packs (init))
39105 init = error_mark_node;
39106 if (!type_dependent_expression_p (init)
39107 /* do_auto_deduction doesn't mess with template init-lists. */
39108 && !BRACE_ENCLOSED_INITIALIZER_P (init))
39109 {
39110 tree d = decl;
39111 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
39112 {
39113 tree v = DECL_VALUE_EXPR (decl);
39114 if (TREE_CODE (v) == ARRAY_REF
39115 && VAR_P (TREE_OPERAND (v, 0))
39116 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
39117 d = TREE_OPERAND (v, 0);
39118 }
39119 do_range_for_auto_deduction (d, init);
39120 }
39121 cond = global_namespace;
39122 incr = NULL_TREE;
39123 orig_init = init;
39124 if (this_pre_body)
39125 this_pre_body = pop_stmt_list (this_pre_body);
39126 return;
39127 }
39128
39129 init = mark_lvalue_use (init);
39130
39131 if (decl == error_mark_node || init == error_mark_node)
39132 /* If an error happened previously do nothing or else a lot of
39133 unhelpful errors would be issued. */
39134 begin_expr = end_expr = iter_type = error_mark_node;
39135 else
39136 {
39137 tree range_temp;
39138
39139 if (VAR_P (init)
39140 && array_of_runtime_bound_p (TREE_TYPE (init)))
39141 /* Can't bind a reference to an array of runtime bound. */
39142 range_temp = init;
39143 else
39144 {
39145 range_temp = build_range_temp (init);
39146 DECL_NAME (range_temp) = NULL_TREE;
39147 pushdecl (range_temp);
39148 cp_finish_decl (range_temp, init,
39149 /*is_constant_init*/false, NULL_TREE,
39150 LOOKUP_ONLYCONVERTING);
39151 range_temp_decl = range_temp;
39152 range_temp = convert_from_reference (range_temp);
39153 }
39154 iter_type = cp_parser_perform_range_for_lookup (range_temp,
39155 &begin_expr, &end_expr);
39156 }
39157
39158 tree end_iter_type = iter_type;
39159 if (cxx_dialect >= cxx17)
39160 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
39161 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
39162 TREE_USED (end) = 1;
39163 DECL_ARTIFICIAL (end) = 1;
39164 pushdecl (end);
39165 cp_finish_decl (end, end_expr,
39166 /*is_constant_init*/false, NULL_TREE,
39167 LOOKUP_ONLYCONVERTING);
39168
39169 /* The new for initialization statement. */
39170 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
39171 TREE_USED (begin) = 1;
39172 DECL_ARTIFICIAL (begin) = 1;
39173 pushdecl (begin);
39174 orig_init = init;
39175 if (CLASS_TYPE_P (iter_type))
39176 init = NULL_TREE;
39177 else
39178 {
39179 init = begin_expr;
39180 begin_expr = NULL_TREE;
39181 }
39182 cp_finish_decl (begin, begin_expr,
39183 /*is_constant_init*/false, NULL_TREE,
39184 LOOKUP_ONLYCONVERTING);
39185
39186 /* The new for condition. */
39187 if (CLASS_TYPE_P (iter_type))
39188 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
39189 else
39190 cond = build_x_binary_op (input_location, NE_EXPR,
39191 begin, ERROR_MARK,
39192 end, ERROR_MARK,
39193 NULL, tf_warning_or_error);
39194
39195 /* The new increment expression. */
39196 if (CLASS_TYPE_P (iter_type))
39197 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
39198 else
39199 incr = finish_unary_op_expr (input_location,
39200 PREINCREMENT_EXPR, begin,
39201 tf_warning_or_error);
39202
39203 orig_decl = decl;
39204 decl = begin;
39205 if (for_block)
39206 {
39207 vec_safe_push (for_block, this_pre_body);
39208 this_pre_body = NULL_TREE;
39209 }
39210
39211 tree decomp_first_name = NULL_TREE;
39212 unsigned decomp_cnt = 0;
39213 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
39214 {
39215 tree v = DECL_VALUE_EXPR (orig_decl);
39216 if (TREE_CODE (v) == ARRAY_REF
39217 && VAR_P (TREE_OPERAND (v, 0))
39218 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
39219 {
39220 tree d = orig_decl;
39221 orig_decl = TREE_OPERAND (v, 0);
39222 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
39223 decomp_first_name = d;
39224 }
39225 }
39226
39227 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
39228 if (auto_node)
39229 {
39230 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
39231 tf_none);
39232 if (!error_operand_p (t))
39233 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
39234 t, auto_node);
39235 }
39236
39237 tree v = make_tree_vec (decomp_cnt + 3);
39238 TREE_VEC_ELT (v, 0) = range_temp_decl;
39239 TREE_VEC_ELT (v, 1) = end;
39240 TREE_VEC_ELT (v, 2) = orig_decl;
39241 for (unsigned i = 0; i < decomp_cnt; i++)
39242 {
39243 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
39244 decomp_first_name = DECL_CHAIN (decomp_first_name);
39245 }
39246 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
39247 }
39248
39249 /* Helper for cp_parser_omp_for_loop, finalize part of range for
39250 inside of the collapsed body. */
39251
39252 void
39253 cp_finish_omp_range_for (tree orig, tree begin)
39254 {
39255 gcc_assert (TREE_CODE (orig) == TREE_LIST
39256 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
39257 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
39258 tree decomp_first_name = NULL_TREE;
39259 unsigned int decomp_cnt = 0;
39260
39261 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
39262 {
39263 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
39264 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
39265 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
39266 }
39267
39268 /* The declaration is initialized with *__begin inside the loop body. */
39269 cp_finish_decl (decl,
39270 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
39271 tf_warning_or_error),
39272 /*is_constant_init*/false, NULL_TREE,
39273 LOOKUP_ONLYCONVERTING);
39274 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
39275 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
39276 }
39277
39278 /* OpenMP 5.0:
39279
39280 scan-loop-body:
39281 { structured-block scan-directive structured-block } */
39282
39283 static void
39284 cp_parser_omp_scan_loop_body (cp_parser *parser)
39285 {
39286 tree substmt, clauses = NULL_TREE;
39287
39288 matching_braces braces;
39289 if (!braces.require_open (parser))
39290 return;
39291
39292 substmt = cp_parser_omp_structured_block (parser, NULL);
39293 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
39294 add_stmt (substmt);
39295
39296 cp_token *tok = cp_lexer_peek_token (parser->lexer);
39297 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
39298 {
39299 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
39300
39301 cp_lexer_consume_token (parser->lexer);
39302
39303 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39304 {
39305 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39306 const char *p = IDENTIFIER_POINTER (id);
39307 if (strcmp (p, "inclusive") == 0)
39308 clause = OMP_CLAUSE_INCLUSIVE;
39309 else if (strcmp (p, "exclusive") == 0)
39310 clause = OMP_CLAUSE_EXCLUSIVE;
39311 }
39312 if (clause != OMP_CLAUSE_ERROR)
39313 {
39314 cp_lexer_consume_token (parser->lexer);
39315 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
39316 }
39317 else
39318 cp_parser_error (parser, "expected %<inclusive%> or "
39319 "%<exclusive%> clause");
39320
39321 cp_parser_require_pragma_eol (parser, tok);
39322 }
39323 else
39324 error ("expected %<#pragma omp scan%>");
39325
39326 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
39327 substmt = cp_parser_omp_structured_block (parser, NULL);
39328 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
39329 clauses);
39330 add_stmt (substmt);
39331
39332 braces.require_close (parser);
39333 }
39334
39335 /* Parse the restricted form of the for statement allowed by OpenMP. */
39336
39337 static tree
39338 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
39339 tree *cclauses, bool *if_p)
39340 {
39341 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
39342 tree orig_decl;
39343 tree real_decl, initv, condv, incrv, declv, orig_declv;
39344 tree this_pre_body, cl, ordered_cl = NULL_TREE;
39345 location_t loc_first;
39346 bool collapse_err = false;
39347 int i, collapse = 1, ordered = 0, count, nbraces = 0;
39348 releasing_vec for_block;
39349 auto_vec<tree, 4> orig_inits;
39350 bool tiling = false;
39351 bool inscan = false;
39352
39353 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
39354 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
39355 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
39356 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
39357 {
39358 tiling = true;
39359 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
39360 }
39361 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
39362 && OMP_CLAUSE_ORDERED_EXPR (cl))
39363 {
39364 ordered_cl = cl;
39365 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
39366 }
39367 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
39368 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
39369 && (code == OMP_SIMD || code == OMP_FOR))
39370 inscan = true;
39371
39372 if (ordered && ordered < collapse)
39373 {
39374 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
39375 "%<ordered%> clause parameter is less than %<collapse%>");
39376 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
39377 = build_int_cst (NULL_TREE, collapse);
39378 ordered = collapse;
39379 }
39380 if (ordered)
39381 {
39382 for (tree *pc = &clauses; *pc; )
39383 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
39384 {
39385 error_at (OMP_CLAUSE_LOCATION (*pc),
39386 "%<linear%> clause may not be specified together "
39387 "with %<ordered%> clause with a parameter");
39388 *pc = OMP_CLAUSE_CHAIN (*pc);
39389 }
39390 else
39391 pc = &OMP_CLAUSE_CHAIN (*pc);
39392 }
39393
39394 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
39395 count = ordered ? ordered : collapse;
39396
39397 declv = make_tree_vec (count);
39398 initv = make_tree_vec (count);
39399 condv = make_tree_vec (count);
39400 incrv = make_tree_vec (count);
39401 orig_declv = NULL_TREE;
39402
39403 loc_first = cp_lexer_peek_token (parser->lexer)->location;
39404
39405 for (i = 0; i < count; i++)
39406 {
39407 int bracecount = 0;
39408 tree add_private_clause = NULL_TREE;
39409 location_t loc;
39410
39411 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
39412 {
39413 if (!collapse_err)
39414 cp_parser_error (parser, "for statement expected");
39415 return NULL;
39416 }
39417 loc = cp_lexer_consume_token (parser->lexer)->location;
39418
39419 /* Don't create location wrapper nodes within an OpenMP "for"
39420 statement. */
39421 auto_suppress_location_wrappers sentinel;
39422
39423 matching_parens parens;
39424 if (!parens.require_open (parser))
39425 return NULL;
39426
39427 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
39428 this_pre_body = push_stmt_list ();
39429
39430 if (code != OACC_LOOP && cxx_dialect >= cxx11)
39431 {
39432 /* Save tokens so that we can put them back. */
39433 cp_lexer_save_tokens (parser->lexer);
39434
39435 /* Look for ':' that is not nested in () or {}. */
39436 bool is_range_for
39437 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
39438 /*recovering=*/false,
39439 CPP_COLON,
39440 /*consume_paren=*/
39441 false) == -1);
39442
39443 /* Roll back the tokens we skipped. */
39444 cp_lexer_rollback_tokens (parser->lexer);
39445
39446 if (is_range_for)
39447 {
39448 bool saved_colon_corrects_to_scope_p
39449 = parser->colon_corrects_to_scope_p;
39450
39451 /* A colon is used in range-based for. */
39452 parser->colon_corrects_to_scope_p = false;
39453
39454 /* Parse the declaration. */
39455 cp_parser_simple_declaration (parser,
39456 /*function_definition_allowed_p=*/
39457 false, &decl);
39458 parser->colon_corrects_to_scope_p
39459 = saved_colon_corrects_to_scope_p;
39460
39461 cp_parser_require (parser, CPP_COLON, RT_COLON);
39462
39463 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
39464 false, 0, true);
39465
39466 cp_convert_omp_range_for (this_pre_body, for_block, decl,
39467 orig_decl, init, orig_init,
39468 cond, incr);
39469 if (this_pre_body)
39470 {
39471 if (pre_body)
39472 {
39473 tree t = pre_body;
39474 pre_body = push_stmt_list ();
39475 add_stmt (t);
39476 add_stmt (this_pre_body);
39477 pre_body = pop_stmt_list (pre_body);
39478 }
39479 else
39480 pre_body = this_pre_body;
39481 }
39482
39483 if (ordered_cl)
39484 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
39485 "%<ordered%> clause with parameter on "
39486 "range-based %<for%> loop");
39487
39488 goto parse_close_paren;
39489 }
39490 }
39491
39492 add_private_clause
39493 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
39494 init, orig_init, decl, real_decl);
39495
39496 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
39497 if (this_pre_body)
39498 {
39499 this_pre_body = pop_stmt_list (this_pre_body);
39500 if (pre_body)
39501 {
39502 tree t = pre_body;
39503 pre_body = push_stmt_list ();
39504 add_stmt (t);
39505 add_stmt (this_pre_body);
39506 pre_body = pop_stmt_list (pre_body);
39507 }
39508 else
39509 pre_body = this_pre_body;
39510 }
39511
39512 if (decl)
39513 real_decl = decl;
39514 if (cclauses != NULL
39515 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
39516 && real_decl != NULL_TREE
39517 && code != OMP_LOOP)
39518 {
39519 tree *c;
39520 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
39521 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
39522 && OMP_CLAUSE_DECL (*c) == real_decl)
39523 {
39524 error_at (loc, "iteration variable %qD"
39525 " should not be firstprivate", real_decl);
39526 *c = OMP_CLAUSE_CHAIN (*c);
39527 }
39528 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
39529 && OMP_CLAUSE_DECL (*c) == real_decl)
39530 {
39531 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
39532 tree l = *c;
39533 *c = OMP_CLAUSE_CHAIN (*c);
39534 if (code == OMP_SIMD)
39535 {
39536 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
39537 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
39538 }
39539 else
39540 {
39541 OMP_CLAUSE_CHAIN (l) = clauses;
39542 clauses = l;
39543 }
39544 add_private_clause = NULL_TREE;
39545 }
39546 else
39547 {
39548 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
39549 && OMP_CLAUSE_DECL (*c) == real_decl)
39550 add_private_clause = NULL_TREE;
39551 c = &OMP_CLAUSE_CHAIN (*c);
39552 }
39553 }
39554
39555 if (add_private_clause)
39556 {
39557 tree c;
39558 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
39559 {
39560 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
39561 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
39562 && OMP_CLAUSE_DECL (c) == decl)
39563 break;
39564 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
39565 && OMP_CLAUSE_DECL (c) == decl)
39566 error_at (loc, "iteration variable %qD "
39567 "should not be firstprivate",
39568 decl);
39569 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
39570 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
39571 && OMP_CLAUSE_DECL (c) == decl)
39572 error_at (loc, "iteration variable %qD should not be reduction",
39573 decl);
39574 }
39575 if (c == NULL)
39576 {
39577 if ((code == OMP_SIMD && collapse != 1) || code == OMP_LOOP)
39578 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
39579 else if (code != OMP_SIMD)
39580 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
39581 else
39582 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
39583 OMP_CLAUSE_DECL (c) = add_private_clause;
39584 c = finish_omp_clauses (c, C_ORT_OMP);
39585 if (c)
39586 {
39587 OMP_CLAUSE_CHAIN (c) = clauses;
39588 clauses = c;
39589 /* For linear, signal that we need to fill up
39590 the so far unknown linear step. */
39591 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
39592 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
39593 }
39594 }
39595 }
39596
39597 cond = NULL;
39598 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
39599 cond = cp_parser_omp_for_cond (parser, decl, code);
39600 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
39601
39602 incr = NULL;
39603 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
39604 {
39605 /* If decl is an iterator, preserve the operator on decl
39606 until finish_omp_for. */
39607 if (real_decl
39608 && ((processing_template_decl
39609 && (TREE_TYPE (real_decl) == NULL_TREE
39610 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
39611 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
39612 incr = cp_parser_omp_for_incr (parser, real_decl);
39613 else
39614 incr = cp_parser_expression (parser);
39615 protected_set_expr_location_if_unset (incr, input_location);
39616 }
39617
39618 parse_close_paren:
39619 if (!parens.require_close (parser))
39620 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39621 /*or_comma=*/false,
39622 /*consume_paren=*/true);
39623
39624 TREE_VEC_ELT (declv, i) = decl;
39625 TREE_VEC_ELT (initv, i) = init;
39626 TREE_VEC_ELT (condv, i) = cond;
39627 TREE_VEC_ELT (incrv, i) = incr;
39628 if (orig_init)
39629 {
39630 orig_inits.safe_grow_cleared (i + 1, true);
39631 orig_inits[i] = orig_init;
39632 }
39633 if (orig_decl)
39634 {
39635 if (!orig_declv)
39636 orig_declv = copy_node (declv);
39637 TREE_VEC_ELT (orig_declv, i) = orig_decl;
39638 }
39639 else if (orig_declv)
39640 TREE_VEC_ELT (orig_declv, i) = decl;
39641
39642 if (i == count - 1)
39643 break;
39644
39645 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
39646 in between the collapsed for loops to be still considered perfectly
39647 nested. Hopefully the final version clarifies this.
39648 For now handle (multiple) {'s and empty statements. */
39649 cp_parser_parse_tentatively (parser);
39650 for (;;)
39651 {
39652 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
39653 break;
39654 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
39655 {
39656 cp_lexer_consume_token (parser->lexer);
39657 bracecount++;
39658 }
39659 else if (bracecount
39660 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
39661 cp_lexer_consume_token (parser->lexer);
39662 else
39663 {
39664 loc = cp_lexer_peek_token (parser->lexer)->location;
39665 error_at (loc, "not enough for loops to collapse");
39666 collapse_err = true;
39667 cp_parser_abort_tentative_parse (parser);
39668 declv = NULL_TREE;
39669 break;
39670 }
39671 }
39672
39673 if (declv)
39674 {
39675 cp_parser_parse_definitely (parser);
39676 nbraces += bracecount;
39677 }
39678 }
39679
39680 if (nbraces)
39681 if_p = NULL;
39682
39683 /* Note that we saved the original contents of this flag when we entered
39684 the structured block, and so we don't need to re-save it here. */
39685 parser->in_statement = IN_OMP_FOR;
39686
39687 /* Note that the grammar doesn't call for a structured block here,
39688 though the loop as a whole is a structured block. */
39689 if (orig_declv)
39690 {
39691 body = begin_omp_structured_block ();
39692 for (i = 0; i < count; i++)
39693 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
39694 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
39695 TREE_VEC_ELT (declv, i));
39696 }
39697 else
39698 body = push_stmt_list ();
39699 if (inscan)
39700 cp_parser_omp_scan_loop_body (parser);
39701 else
39702 cp_parser_statement (parser, NULL_TREE, false, if_p);
39703 if (orig_declv)
39704 body = finish_omp_structured_block (body);
39705 else
39706 body = pop_stmt_list (body);
39707
39708 if (declv == NULL_TREE)
39709 ret = NULL_TREE;
39710 else
39711 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
39712 incrv, body, pre_body, &orig_inits, clauses);
39713
39714 while (nbraces)
39715 {
39716 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
39717 {
39718 cp_lexer_consume_token (parser->lexer);
39719 nbraces--;
39720 }
39721 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
39722 cp_lexer_consume_token (parser->lexer);
39723 else
39724 {
39725 if (!collapse_err)
39726 {
39727 error_at (cp_lexer_peek_token (parser->lexer)->location,
39728 "collapsed loops not perfectly nested");
39729 }
39730 collapse_err = true;
39731 cp_parser_statement_seq_opt (parser, NULL);
39732 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
39733 break;
39734 }
39735 }
39736
39737 while (!for_block->is_empty ())
39738 {
39739 tree t = for_block->pop ();
39740 if (TREE_CODE (t) == STATEMENT_LIST)
39741 add_stmt (pop_stmt_list (t));
39742 else
39743 add_stmt (t);
39744 }
39745
39746 return ret;
39747 }
39748
39749 /* Helper function for OpenMP parsing, split clauses and call
39750 finish_omp_clauses on each of the set of clauses afterwards. */
39751
39752 static void
39753 cp_omp_split_clauses (location_t loc, enum tree_code code,
39754 omp_clause_mask mask, tree clauses, tree *cclauses)
39755 {
39756 int i;
39757 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
39758 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
39759 if (cclauses[i])
39760 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
39761 }
39762
39763 /* OpenMP 5.0:
39764 #pragma omp loop loop-clause[optseq] new-line
39765 for-loop */
39766
39767 #define OMP_LOOP_CLAUSE_MASK \
39768 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
39773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
39774
39775 static tree
39776 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
39777 char *p_name, omp_clause_mask mask, tree *cclauses,
39778 bool *if_p)
39779 {
39780 tree clauses, sb, ret;
39781 unsigned int save;
39782 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39783
39784 strcat (p_name, " loop");
39785 mask |= OMP_LOOP_CLAUSE_MASK;
39786
39787 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39788 cclauses == NULL);
39789 if (cclauses)
39790 {
39791 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
39792 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
39793 }
39794
39795 keep_next_level (true);
39796 sb = begin_omp_structured_block ();
39797 save = cp_parser_begin_omp_structured_block (parser);
39798
39799 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
39800
39801 cp_parser_end_omp_structured_block (parser, save);
39802 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39803
39804 return ret;
39805 }
39806
39807 /* OpenMP 4.0:
39808 #pragma omp simd simd-clause[optseq] new-line
39809 for-loop */
39810
39811 #define OMP_SIMD_CLAUSE_MASK \
39812 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
39813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
39814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
39816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
39822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
39823
39824 static tree
39825 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
39826 char *p_name, omp_clause_mask mask, tree *cclauses,
39827 bool *if_p)
39828 {
39829 tree clauses, sb, ret;
39830 unsigned int save;
39831 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39832
39833 strcat (p_name, " simd");
39834 mask |= OMP_SIMD_CLAUSE_MASK;
39835
39836 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39837 cclauses == NULL);
39838 if (cclauses)
39839 {
39840 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
39841 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
39842 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
39843 OMP_CLAUSE_ORDERED);
39844 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
39845 {
39846 error_at (OMP_CLAUSE_LOCATION (c),
39847 "%<ordered%> clause with parameter may not be specified "
39848 "on %qs construct", p_name);
39849 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
39850 }
39851 }
39852
39853 keep_next_level (true);
39854 sb = begin_omp_structured_block ();
39855 save = cp_parser_begin_omp_structured_block (parser);
39856
39857 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
39858
39859 cp_parser_end_omp_structured_block (parser, save);
39860 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39861
39862 return ret;
39863 }
39864
39865 /* OpenMP 2.5:
39866 #pragma omp for for-clause[optseq] new-line
39867 for-loop
39868
39869 OpenMP 4.0:
39870 #pragma omp for simd for-simd-clause[optseq] new-line
39871 for-loop */
39872
39873 #define OMP_FOR_CLAUSE_MASK \
39874 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
39880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
39881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
39882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
39884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
39885
39886 static tree
39887 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
39888 char *p_name, omp_clause_mask mask, tree *cclauses,
39889 bool *if_p)
39890 {
39891 tree clauses, sb, ret;
39892 unsigned int save;
39893 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39894
39895 strcat (p_name, " for");
39896 mask |= OMP_FOR_CLAUSE_MASK;
39897 /* parallel for{, simd} disallows nowait clause, but for
39898 target {teams distribute ,}parallel for{, simd} it should be accepted. */
39899 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
39900 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
39901 /* Composite distribute parallel for{, simd} disallows ordered clause. */
39902 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
39903 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
39904
39905 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39906 {
39907 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39908 const char *p = IDENTIFIER_POINTER (id);
39909
39910 if (strcmp (p, "simd") == 0)
39911 {
39912 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39913 if (cclauses == NULL)
39914 cclauses = cclauses_buf;
39915
39916 cp_lexer_consume_token (parser->lexer);
39917 if (!flag_openmp) /* flag_openmp_simd */
39918 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39919 cclauses, if_p);
39920 sb = begin_omp_structured_block ();
39921 save = cp_parser_begin_omp_structured_block (parser);
39922 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39923 cclauses, if_p);
39924 cp_parser_end_omp_structured_block (parser, save);
39925 tree body = finish_omp_structured_block (sb);
39926 if (ret == NULL)
39927 return ret;
39928 ret = make_node (OMP_FOR);
39929 TREE_TYPE (ret) = void_type_node;
39930 OMP_FOR_BODY (ret) = body;
39931 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
39932 SET_EXPR_LOCATION (ret, loc);
39933 add_stmt (ret);
39934 return ret;
39935 }
39936 }
39937 if (!flag_openmp) /* flag_openmp_simd */
39938 {
39939 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39940 return NULL_TREE;
39941 }
39942
39943 /* Composite distribute parallel for disallows linear clause. */
39944 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
39945 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
39946
39947 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39948 cclauses == NULL);
39949 if (cclauses)
39950 {
39951 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
39952 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
39953 }
39954
39955 keep_next_level (true);
39956 sb = begin_omp_structured_block ();
39957 save = cp_parser_begin_omp_structured_block (parser);
39958
39959 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
39960
39961 cp_parser_end_omp_structured_block (parser, save);
39962 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39963
39964 return ret;
39965 }
39966
39967 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
39968 omp_clause_mask, tree *, bool *);
39969
39970 /* OpenMP 2.5:
39971 # pragma omp master new-line
39972 structured-block */
39973
39974 static tree
39975 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
39976 char *p_name, omp_clause_mask mask, tree *cclauses,
39977 bool *if_p)
39978 {
39979 tree clauses, sb, ret;
39980 unsigned int save;
39981 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39982
39983 strcat (p_name, " master");
39984
39985 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39986 {
39987 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39988 const char *p = IDENTIFIER_POINTER (id);
39989
39990 if (strcmp (p, "taskloop") == 0)
39991 {
39992 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39993 if (cclauses == NULL)
39994 cclauses = cclauses_buf;
39995
39996 cp_lexer_consume_token (parser->lexer);
39997 if (!flag_openmp) /* flag_openmp_simd */
39998 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
39999 cclauses, if_p);
40000 sb = begin_omp_structured_block ();
40001 save = cp_parser_begin_omp_structured_block (parser);
40002 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
40003 cclauses, if_p);
40004 cp_parser_end_omp_structured_block (parser, save);
40005 tree body = finish_omp_structured_block (sb);
40006 if (ret == NULL)
40007 return ret;
40008 return c_finish_omp_master (loc, body);
40009 }
40010 }
40011 if (!flag_openmp) /* flag_openmp_simd */
40012 {
40013 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40014 return NULL_TREE;
40015 }
40016
40017 if (cclauses)
40018 {
40019 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40020 false);
40021 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
40022 }
40023 else
40024 cp_parser_require_pragma_eol (parser, pragma_tok);
40025
40026 return c_finish_omp_master (loc,
40027 cp_parser_omp_structured_block (parser, if_p));
40028 }
40029
40030 /* OpenMP 2.5:
40031 # pragma omp ordered new-line
40032 structured-block
40033
40034 OpenMP 4.5:
40035 # pragma omp ordered ordered-clauses new-line
40036 structured-block */
40037
40038 #define OMP_ORDERED_CLAUSE_MASK \
40039 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
40040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
40041
40042 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
40043 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
40044
40045 static bool
40046 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
40047 enum pragma_context context, bool *if_p)
40048 {
40049 location_t loc = pragma_tok->location;
40050
40051 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40052 {
40053 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40054 const char *p = IDENTIFIER_POINTER (id);
40055
40056 if (strcmp (p, "depend") == 0)
40057 {
40058 if (!flag_openmp) /* flag_openmp_simd */
40059 {
40060 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40061 return false;
40062 }
40063 if (context == pragma_stmt)
40064 {
40065 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
40066 "%<depend%> clause may only be used in compound "
40067 "statements");
40068 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40069 return false;
40070 }
40071 tree clauses
40072 = cp_parser_omp_all_clauses (parser,
40073 OMP_ORDERED_DEPEND_CLAUSE_MASK,
40074 "#pragma omp ordered", pragma_tok);
40075 c_finish_omp_ordered (loc, clauses, NULL_TREE);
40076 return false;
40077 }
40078 }
40079
40080 tree clauses
40081 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
40082 "#pragma omp ordered", pragma_tok);
40083
40084 if (!flag_openmp /* flag_openmp_simd */
40085 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
40086 return false;
40087
40088 c_finish_omp_ordered (loc, clauses,
40089 cp_parser_omp_structured_block (parser, if_p));
40090 return true;
40091 }
40092
40093 /* OpenMP 2.5:
40094
40095 section-scope:
40096 { section-sequence }
40097
40098 section-sequence:
40099 section-directive[opt] structured-block
40100 section-sequence section-directive structured-block */
40101
40102 static tree
40103 cp_parser_omp_sections_scope (cp_parser *parser)
40104 {
40105 tree stmt, substmt;
40106 bool error_suppress = false;
40107 cp_token *tok;
40108
40109 matching_braces braces;
40110 if (!braces.require_open (parser))
40111 return NULL_TREE;
40112
40113 stmt = push_stmt_list ();
40114
40115 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
40116 != PRAGMA_OMP_SECTION)
40117 {
40118 substmt = cp_parser_omp_structured_block (parser, NULL);
40119 substmt = build1 (OMP_SECTION, void_type_node, substmt);
40120 add_stmt (substmt);
40121 }
40122
40123 while (1)
40124 {
40125 tok = cp_lexer_peek_token (parser->lexer);
40126 if (tok->type == CPP_CLOSE_BRACE)
40127 break;
40128 if (tok->type == CPP_EOF)
40129 break;
40130
40131 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
40132 {
40133 cp_lexer_consume_token (parser->lexer);
40134 cp_parser_require_pragma_eol (parser, tok);
40135 error_suppress = false;
40136 }
40137 else if (!error_suppress)
40138 {
40139 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
40140 error_suppress = true;
40141 }
40142
40143 substmt = cp_parser_omp_structured_block (parser, NULL);
40144 substmt = build1 (OMP_SECTION, void_type_node, substmt);
40145 add_stmt (substmt);
40146 }
40147 braces.require_close (parser);
40148
40149 substmt = pop_stmt_list (stmt);
40150
40151 stmt = make_node (OMP_SECTIONS);
40152 TREE_TYPE (stmt) = void_type_node;
40153 OMP_SECTIONS_BODY (stmt) = substmt;
40154
40155 add_stmt (stmt);
40156 return stmt;
40157 }
40158
40159 /* OpenMP 2.5:
40160 # pragma omp sections sections-clause[optseq] newline
40161 sections-scope */
40162
40163 #define OMP_SECTIONS_CLAUSE_MASK \
40164 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40170
40171 static tree
40172 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
40173 char *p_name, omp_clause_mask mask, tree *cclauses)
40174 {
40175 tree clauses, ret;
40176 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40177
40178 strcat (p_name, " sections");
40179 mask |= OMP_SECTIONS_CLAUSE_MASK;
40180 if (cclauses)
40181 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
40182
40183 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40184 cclauses == NULL);
40185 if (cclauses)
40186 {
40187 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
40188 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
40189 }
40190
40191 ret = cp_parser_omp_sections_scope (parser);
40192 if (ret)
40193 OMP_SECTIONS_CLAUSES (ret) = clauses;
40194
40195 return ret;
40196 }
40197
40198 /* OpenMP 2.5:
40199 # pragma omp parallel parallel-clause[optseq] new-line
40200 structured-block
40201 # pragma omp parallel for parallel-for-clause[optseq] new-line
40202 structured-block
40203 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
40204 structured-block
40205
40206 OpenMP 4.0:
40207 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
40208 structured-block */
40209
40210 #define OMP_PARALLEL_CLAUSE_MASK \
40211 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
40215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
40216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
40217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
40219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
40221
40222 static tree
40223 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
40224 char *p_name, omp_clause_mask mask, tree *cclauses,
40225 bool *if_p)
40226 {
40227 tree stmt, clauses, block;
40228 unsigned int save;
40229 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40230
40231 strcat (p_name, " parallel");
40232 mask |= OMP_PARALLEL_CLAUSE_MASK;
40233 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
40234 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
40235 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
40236 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
40237
40238 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
40239 {
40240 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40241 if (cclauses == NULL)
40242 cclauses = cclauses_buf;
40243
40244 cp_lexer_consume_token (parser->lexer);
40245 if (!flag_openmp) /* flag_openmp_simd */
40246 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
40247 if_p);
40248 block = begin_omp_parallel ();
40249 save = cp_parser_begin_omp_structured_block (parser);
40250 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
40251 if_p);
40252 cp_parser_end_omp_structured_block (parser, save);
40253 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
40254 block);
40255 if (ret == NULL_TREE)
40256 return ret;
40257 OMP_PARALLEL_COMBINED (stmt) = 1;
40258 return stmt;
40259 }
40260 /* When combined with distribute, parallel has to be followed by for.
40261 #pragma omp target parallel is allowed though. */
40262 else if (cclauses
40263 && (mask & (OMP_CLAUSE_MASK_1
40264 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
40265 {
40266 error_at (loc, "expected %<for%> after %qs", p_name);
40267 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40268 return NULL_TREE;
40269 }
40270 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40271 {
40272 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40273 const char *p = IDENTIFIER_POINTER (id);
40274 if (cclauses == NULL && strcmp (p, "master") == 0)
40275 {
40276 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40277 cclauses = cclauses_buf;
40278
40279 cp_lexer_consume_token (parser->lexer);
40280 block = begin_omp_parallel ();
40281 save = cp_parser_begin_omp_structured_block (parser);
40282 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
40283 cclauses, if_p);
40284 cp_parser_end_omp_structured_block (parser, save);
40285 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
40286 block);
40287 if (ret == NULL_TREE)
40288 return ret;
40289 OMP_PARALLEL_COMBINED (stmt) = 1;
40290 return stmt;
40291 }
40292 else if (strcmp (p, "loop") == 0)
40293 {
40294 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40295 if (cclauses == NULL)
40296 cclauses = cclauses_buf;
40297
40298 cp_lexer_consume_token (parser->lexer);
40299 if (!flag_openmp) /* flag_openmp_simd */
40300 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
40301 cclauses, if_p);
40302 block = begin_omp_parallel ();
40303 save = cp_parser_begin_omp_structured_block (parser);
40304 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
40305 cclauses, if_p);
40306 cp_parser_end_omp_structured_block (parser, save);
40307 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
40308 block);
40309 if (ret == NULL_TREE)
40310 return ret;
40311 OMP_PARALLEL_COMBINED (stmt) = 1;
40312 return stmt;
40313 }
40314 else if (!flag_openmp) /* flag_openmp_simd */
40315 {
40316 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40317 return NULL_TREE;
40318 }
40319 else if (cclauses == NULL && strcmp (p, "sections") == 0)
40320 {
40321 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40322 cclauses = cclauses_buf;
40323
40324 cp_lexer_consume_token (parser->lexer);
40325 block = begin_omp_parallel ();
40326 save = cp_parser_begin_omp_structured_block (parser);
40327 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
40328 cp_parser_end_omp_structured_block (parser, save);
40329 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
40330 block);
40331 OMP_PARALLEL_COMBINED (stmt) = 1;
40332 return stmt;
40333 }
40334 }
40335 else if (!flag_openmp) /* flag_openmp_simd */
40336 {
40337 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40338 return NULL_TREE;
40339 }
40340
40341 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40342 cclauses == NULL);
40343 if (cclauses)
40344 {
40345 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
40346 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
40347 }
40348
40349 block = begin_omp_parallel ();
40350 save = cp_parser_begin_omp_structured_block (parser);
40351 cp_parser_statement (parser, NULL_TREE, false, if_p);
40352 cp_parser_end_omp_structured_block (parser, save);
40353 stmt = finish_omp_parallel (clauses, block);
40354 return stmt;
40355 }
40356
40357 /* OpenMP 2.5:
40358 # pragma omp single single-clause[optseq] new-line
40359 structured-block */
40360
40361 #define OMP_SINGLE_CLAUSE_MASK \
40362 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
40365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40367
40368 static tree
40369 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40370 {
40371 tree stmt = make_node (OMP_SINGLE);
40372 TREE_TYPE (stmt) = void_type_node;
40373 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40374
40375 OMP_SINGLE_CLAUSES (stmt)
40376 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
40377 "#pragma omp single", pragma_tok);
40378 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
40379
40380 return add_stmt (stmt);
40381 }
40382
40383 /* OpenMP 3.0:
40384 # pragma omp task task-clause[optseq] new-line
40385 structured-block */
40386
40387 #define OMP_TASK_CLAUSE_MASK \
40388 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
40390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
40391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
40394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
40395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
40396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
40398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
40400
40401 static tree
40402 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40403 {
40404 tree clauses, block;
40405 unsigned int save;
40406
40407 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
40408 "#pragma omp task", pragma_tok);
40409 block = begin_omp_task ();
40410 save = cp_parser_begin_omp_structured_block (parser);
40411 cp_parser_statement (parser, NULL_TREE, false, if_p);
40412 cp_parser_end_omp_structured_block (parser, save);
40413 return finish_omp_task (clauses, block);
40414 }
40415
40416 /* OpenMP 3.0:
40417 # pragma omp taskwait new-line
40418
40419 OpenMP 5.0:
40420 # pragma omp taskwait taskwait-clause[opt] new-line */
40421
40422 #define OMP_TASKWAIT_CLAUSE_MASK \
40423 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
40424
40425 static void
40426 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
40427 {
40428 tree clauses
40429 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
40430 "#pragma omp taskwait", pragma_tok);
40431
40432 if (clauses)
40433 {
40434 tree stmt = make_node (OMP_TASK);
40435 TREE_TYPE (stmt) = void_node;
40436 OMP_TASK_CLAUSES (stmt) = clauses;
40437 OMP_TASK_BODY (stmt) = NULL_TREE;
40438 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40439 add_stmt (stmt);
40440 }
40441 else
40442 finish_omp_taskwait ();
40443 }
40444
40445 /* OpenMP 3.1:
40446 # pragma omp taskyield new-line */
40447
40448 static void
40449 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
40450 {
40451 cp_parser_require_pragma_eol (parser, pragma_tok);
40452 finish_omp_taskyield ();
40453 }
40454
40455 /* OpenMP 4.0:
40456 # pragma omp taskgroup new-line
40457 structured-block
40458
40459 OpenMP 5.0:
40460 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
40461
40462 #define OMP_TASKGROUP_CLAUSE_MASK \
40463 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
40465
40466 static tree
40467 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40468 {
40469 tree clauses
40470 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
40471 "#pragma omp taskgroup", pragma_tok);
40472 return c_finish_omp_taskgroup (input_location,
40473 cp_parser_omp_structured_block (parser,
40474 if_p),
40475 clauses);
40476 }
40477
40478
40479 /* OpenMP 2.5:
40480 # pragma omp threadprivate (variable-list) */
40481
40482 static void
40483 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
40484 {
40485 tree vars;
40486
40487 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
40488 cp_parser_require_pragma_eol (parser, pragma_tok);
40489
40490 finish_omp_threadprivate (vars);
40491 }
40492
40493 /* OpenMP 4.0:
40494 # pragma omp cancel cancel-clause[optseq] new-line */
40495
40496 #define OMP_CANCEL_CLAUSE_MASK \
40497 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
40498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
40499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
40500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
40501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
40502
40503 static void
40504 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
40505 {
40506 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
40507 "#pragma omp cancel", pragma_tok);
40508 finish_omp_cancel (clauses);
40509 }
40510
40511 /* OpenMP 4.0:
40512 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
40513
40514 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
40515 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
40516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
40517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
40518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
40519
40520 static void
40521 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
40522 enum pragma_context context)
40523 {
40524 tree clauses;
40525 bool point_seen = false;
40526
40527 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40528 {
40529 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40530 const char *p = IDENTIFIER_POINTER (id);
40531
40532 if (strcmp (p, "point") == 0)
40533 {
40534 cp_lexer_consume_token (parser->lexer);
40535 point_seen = true;
40536 }
40537 }
40538 if (!point_seen)
40539 {
40540 cp_parser_error (parser, "expected %<point%>");
40541 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40542 return;
40543 }
40544
40545 if (context != pragma_compound)
40546 {
40547 if (context == pragma_stmt)
40548 error_at (pragma_tok->location,
40549 "%<#pragma %s%> may only be used in compound statements",
40550 "omp cancellation point");
40551 else
40552 cp_parser_error (parser, "expected declaration specifiers");
40553 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40554 return;
40555 }
40556
40557 clauses = cp_parser_omp_all_clauses (parser,
40558 OMP_CANCELLATION_POINT_CLAUSE_MASK,
40559 "#pragma omp cancellation point",
40560 pragma_tok);
40561 finish_omp_cancellation_point (clauses);
40562 }
40563
40564 /* OpenMP 4.0:
40565 #pragma omp distribute distribute-clause[optseq] new-line
40566 for-loop */
40567
40568 #define OMP_DISTRIBUTE_CLAUSE_MASK \
40569 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
40573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
40575
40576 static tree
40577 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
40578 char *p_name, omp_clause_mask mask, tree *cclauses,
40579 bool *if_p)
40580 {
40581 tree clauses, sb, ret;
40582 unsigned int save;
40583 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40584
40585 strcat (p_name, " distribute");
40586 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
40587
40588 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40589 {
40590 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40591 const char *p = IDENTIFIER_POINTER (id);
40592 bool simd = false;
40593 bool parallel = false;
40594
40595 if (strcmp (p, "simd") == 0)
40596 simd = true;
40597 else
40598 parallel = strcmp (p, "parallel") == 0;
40599 if (parallel || simd)
40600 {
40601 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40602 if (cclauses == NULL)
40603 cclauses = cclauses_buf;
40604 cp_lexer_consume_token (parser->lexer);
40605 if (!flag_openmp) /* flag_openmp_simd */
40606 {
40607 if (simd)
40608 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40609 cclauses, if_p);
40610 else
40611 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
40612 cclauses, if_p);
40613 }
40614 sb = begin_omp_structured_block ();
40615 save = cp_parser_begin_omp_structured_block (parser);
40616 if (simd)
40617 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40618 cclauses, if_p);
40619 else
40620 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
40621 cclauses, if_p);
40622 cp_parser_end_omp_structured_block (parser, save);
40623 tree body = finish_omp_structured_block (sb);
40624 if (ret == NULL)
40625 return ret;
40626 ret = make_node (OMP_DISTRIBUTE);
40627 TREE_TYPE (ret) = void_type_node;
40628 OMP_FOR_BODY (ret) = body;
40629 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
40630 SET_EXPR_LOCATION (ret, loc);
40631 add_stmt (ret);
40632 return ret;
40633 }
40634 }
40635 if (!flag_openmp) /* flag_openmp_simd */
40636 {
40637 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40638 return NULL_TREE;
40639 }
40640
40641 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40642 cclauses == NULL);
40643 if (cclauses)
40644 {
40645 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
40646 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
40647 }
40648
40649 keep_next_level (true);
40650 sb = begin_omp_structured_block ();
40651 save = cp_parser_begin_omp_structured_block (parser);
40652
40653 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
40654
40655 cp_parser_end_omp_structured_block (parser, save);
40656 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
40657
40658 return ret;
40659 }
40660
40661 /* OpenMP 4.0:
40662 # pragma omp teams teams-clause[optseq] new-line
40663 structured-block */
40664
40665 #define OMP_TEAMS_CLAUSE_MASK \
40666 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
40669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
40671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
40672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
40674
40675 static tree
40676 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
40677 char *p_name, omp_clause_mask mask, tree *cclauses,
40678 bool *if_p)
40679 {
40680 tree clauses, sb, ret;
40681 unsigned int save;
40682 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40683
40684 strcat (p_name, " teams");
40685 mask |= OMP_TEAMS_CLAUSE_MASK;
40686
40687 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40688 {
40689 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40690 const char *p = IDENTIFIER_POINTER (id);
40691 if (strcmp (p, "distribute") == 0)
40692 {
40693 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40694 if (cclauses == NULL)
40695 cclauses = cclauses_buf;
40696
40697 cp_lexer_consume_token (parser->lexer);
40698 if (!flag_openmp) /* flag_openmp_simd */
40699 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
40700 cclauses, if_p);
40701 keep_next_level (true);
40702 sb = begin_omp_structured_block ();
40703 save = cp_parser_begin_omp_structured_block (parser);
40704 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
40705 cclauses, if_p);
40706 cp_parser_end_omp_structured_block (parser, save);
40707 tree body = finish_omp_structured_block (sb);
40708 if (ret == NULL)
40709 return ret;
40710 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
40711 ret = make_node (OMP_TEAMS);
40712 TREE_TYPE (ret) = void_type_node;
40713 OMP_TEAMS_CLAUSES (ret) = clauses;
40714 OMP_TEAMS_BODY (ret) = body;
40715 OMP_TEAMS_COMBINED (ret) = 1;
40716 SET_EXPR_LOCATION (ret, loc);
40717 return add_stmt (ret);
40718 }
40719 else if (strcmp (p, "loop") == 0)
40720 {
40721 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40722 if (cclauses == NULL)
40723 cclauses = cclauses_buf;
40724
40725 cp_lexer_consume_token (parser->lexer);
40726 if (!flag_openmp) /* flag_openmp_simd */
40727 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
40728 cclauses, if_p);
40729 keep_next_level (true);
40730 sb = begin_omp_structured_block ();
40731 save = cp_parser_begin_omp_structured_block (parser);
40732 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
40733 cclauses, if_p);
40734 cp_parser_end_omp_structured_block (parser, save);
40735 tree body = finish_omp_structured_block (sb);
40736 if (ret == NULL)
40737 return ret;
40738 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
40739 ret = make_node (OMP_TEAMS);
40740 TREE_TYPE (ret) = void_type_node;
40741 OMP_TEAMS_CLAUSES (ret) = clauses;
40742 OMP_TEAMS_BODY (ret) = body;
40743 OMP_TEAMS_COMBINED (ret) = 1;
40744 SET_EXPR_LOCATION (ret, loc);
40745 return add_stmt (ret);
40746 }
40747 }
40748 if (!flag_openmp) /* flag_openmp_simd */
40749 {
40750 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40751 return NULL_TREE;
40752 }
40753
40754 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40755 cclauses == NULL);
40756 if (cclauses)
40757 {
40758 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
40759 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
40760 }
40761
40762 tree stmt = make_node (OMP_TEAMS);
40763 TREE_TYPE (stmt) = void_type_node;
40764 OMP_TEAMS_CLAUSES (stmt) = clauses;
40765 keep_next_level (true);
40766 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
40767 SET_EXPR_LOCATION (stmt, loc);
40768
40769 return add_stmt (stmt);
40770 }
40771
40772 /* OpenMP 4.0:
40773 # pragma omp target data target-data-clause[optseq] new-line
40774 structured-block */
40775
40776 #define OMP_TARGET_DATA_CLAUSE_MASK \
40777 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
40779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
40781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
40782
40783 static tree
40784 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40785 {
40786 tree clauses
40787 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
40788 "#pragma omp target data", pragma_tok);
40789 c_omp_adjust_map_clauses (clauses, false);
40790 int map_seen = 0;
40791 for (tree *pc = &clauses; *pc;)
40792 {
40793 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
40794 switch (OMP_CLAUSE_MAP_KIND (*pc))
40795 {
40796 case GOMP_MAP_TO:
40797 case GOMP_MAP_ALWAYS_TO:
40798 case GOMP_MAP_FROM:
40799 case GOMP_MAP_ALWAYS_FROM:
40800 case GOMP_MAP_TOFROM:
40801 case GOMP_MAP_ALWAYS_TOFROM:
40802 case GOMP_MAP_ALLOC:
40803 map_seen = 3;
40804 break;
40805 case GOMP_MAP_FIRSTPRIVATE_POINTER:
40806 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
40807 case GOMP_MAP_ALWAYS_POINTER:
40808 case GOMP_MAP_ATTACH_DETACH:
40809 break;
40810 default:
40811 map_seen |= 1;
40812 error_at (OMP_CLAUSE_LOCATION (*pc),
40813 "%<#pragma omp target data%> with map-type other "
40814 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
40815 "on %<map%> clause");
40816 *pc = OMP_CLAUSE_CHAIN (*pc);
40817 continue;
40818 }
40819 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
40820 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
40821 map_seen = 3;
40822 pc = &OMP_CLAUSE_CHAIN (*pc);
40823 }
40824
40825 if (map_seen != 3)
40826 {
40827 if (map_seen == 0)
40828 error_at (pragma_tok->location,
40829 "%<#pragma omp target data%> must contain at least "
40830 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
40831 "clause");
40832 return NULL_TREE;
40833 }
40834
40835 tree stmt = make_node (OMP_TARGET_DATA);
40836 TREE_TYPE (stmt) = void_type_node;
40837 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
40838
40839 keep_next_level (true);
40840 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
40841
40842 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40843 return add_stmt (stmt);
40844 }
40845
40846 /* OpenMP 4.5:
40847 # pragma omp target enter data target-enter-data-clause[optseq] new-line
40848 structured-block */
40849
40850 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
40851 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
40853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40856
40857 static tree
40858 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
40859 enum pragma_context context)
40860 {
40861 bool data_seen = false;
40862 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40863 {
40864 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40865 const char *p = IDENTIFIER_POINTER (id);
40866
40867 if (strcmp (p, "data") == 0)
40868 {
40869 cp_lexer_consume_token (parser->lexer);
40870 data_seen = true;
40871 }
40872 }
40873 if (!data_seen)
40874 {
40875 cp_parser_error (parser, "expected %<data%>");
40876 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40877 return NULL_TREE;
40878 }
40879
40880 if (context == pragma_stmt)
40881 {
40882 error_at (pragma_tok->location,
40883 "%<#pragma %s%> may only be used in compound statements",
40884 "omp target enter data");
40885 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40886 return NULL_TREE;
40887 }
40888
40889 tree clauses
40890 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
40891 "#pragma omp target enter data", pragma_tok);
40892 c_omp_adjust_map_clauses (clauses, false);
40893 int map_seen = 0;
40894 for (tree *pc = &clauses; *pc;)
40895 {
40896 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
40897 switch (OMP_CLAUSE_MAP_KIND (*pc))
40898 {
40899 case GOMP_MAP_TO:
40900 case GOMP_MAP_ALWAYS_TO:
40901 case GOMP_MAP_ALLOC:
40902 map_seen = 3;
40903 break;
40904 case GOMP_MAP_FIRSTPRIVATE_POINTER:
40905 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
40906 case GOMP_MAP_ALWAYS_POINTER:
40907 case GOMP_MAP_ATTACH_DETACH:
40908 break;
40909 default:
40910 map_seen |= 1;
40911 error_at (OMP_CLAUSE_LOCATION (*pc),
40912 "%<#pragma omp target enter data%> with map-type other "
40913 "than %<to%> or %<alloc%> on %<map%> clause");
40914 *pc = OMP_CLAUSE_CHAIN (*pc);
40915 continue;
40916 }
40917 pc = &OMP_CLAUSE_CHAIN (*pc);
40918 }
40919
40920 if (map_seen != 3)
40921 {
40922 if (map_seen == 0)
40923 error_at (pragma_tok->location,
40924 "%<#pragma omp target enter data%> must contain at least "
40925 "one %<map%> clause");
40926 return NULL_TREE;
40927 }
40928
40929 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
40930 TREE_TYPE (stmt) = void_type_node;
40931 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
40932 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40933 return add_stmt (stmt);
40934 }
40935
40936 /* OpenMP 4.5:
40937 # pragma omp target exit data target-enter-data-clause[optseq] new-line
40938 structured-block */
40939
40940 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
40941 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
40943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40946
40947 static tree
40948 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
40949 enum pragma_context context)
40950 {
40951 bool data_seen = false;
40952 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40953 {
40954 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40955 const char *p = IDENTIFIER_POINTER (id);
40956
40957 if (strcmp (p, "data") == 0)
40958 {
40959 cp_lexer_consume_token (parser->lexer);
40960 data_seen = true;
40961 }
40962 }
40963 if (!data_seen)
40964 {
40965 cp_parser_error (parser, "expected %<data%>");
40966 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40967 return NULL_TREE;
40968 }
40969
40970 if (context == pragma_stmt)
40971 {
40972 error_at (pragma_tok->location,
40973 "%<#pragma %s%> may only be used in compound statements",
40974 "omp target exit data");
40975 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40976 return NULL_TREE;
40977 }
40978
40979 tree clauses
40980 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
40981 "#pragma omp target exit data", pragma_tok);
40982 c_omp_adjust_map_clauses (clauses, false);
40983 int map_seen = 0;
40984 for (tree *pc = &clauses; *pc;)
40985 {
40986 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
40987 switch (OMP_CLAUSE_MAP_KIND (*pc))
40988 {
40989 case GOMP_MAP_FROM:
40990 case GOMP_MAP_ALWAYS_FROM:
40991 case GOMP_MAP_RELEASE:
40992 case GOMP_MAP_DELETE:
40993 map_seen = 3;
40994 break;
40995 case GOMP_MAP_FIRSTPRIVATE_POINTER:
40996 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
40997 case GOMP_MAP_ALWAYS_POINTER:
40998 case GOMP_MAP_ATTACH_DETACH:
40999 break;
41000 default:
41001 map_seen |= 1;
41002 error_at (OMP_CLAUSE_LOCATION (*pc),
41003 "%<#pragma omp target exit data%> with map-type other "
41004 "than %<from%>, %<release%> or %<delete%> on %<map%>"
41005 " clause");
41006 *pc = OMP_CLAUSE_CHAIN (*pc);
41007 continue;
41008 }
41009 pc = &OMP_CLAUSE_CHAIN (*pc);
41010 }
41011
41012 if (map_seen != 3)
41013 {
41014 if (map_seen == 0)
41015 error_at (pragma_tok->location,
41016 "%<#pragma omp target exit data%> must contain at least "
41017 "one %<map%> clause");
41018 return NULL_TREE;
41019 }
41020
41021 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
41022 TREE_TYPE (stmt) = void_type_node;
41023 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
41024 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41025 return add_stmt (stmt);
41026 }
41027
41028 /* OpenMP 4.0:
41029 # pragma omp target update target-update-clause[optseq] new-line */
41030
41031 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
41032 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
41033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
41034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
41038
41039 static bool
41040 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
41041 enum pragma_context context)
41042 {
41043 if (context == pragma_stmt)
41044 {
41045 error_at (pragma_tok->location,
41046 "%<#pragma %s%> may only be used in compound statements",
41047 "omp target update");
41048 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41049 return false;
41050 }
41051
41052 tree clauses
41053 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
41054 "#pragma omp target update", pragma_tok);
41055 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
41056 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
41057 {
41058 error_at (pragma_tok->location,
41059 "%<#pragma omp target update%> must contain at least one "
41060 "%<from%> or %<to%> clauses");
41061 return false;
41062 }
41063
41064 tree stmt = make_node (OMP_TARGET_UPDATE);
41065 TREE_TYPE (stmt) = void_type_node;
41066 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
41067 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41068 add_stmt (stmt);
41069 return false;
41070 }
41071
41072 /* OpenMP 4.0:
41073 # pragma omp target target-clause[optseq] new-line
41074 structured-block */
41075
41076 #define OMP_TARGET_CLAUSE_MASK \
41077 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
41079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
41082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
41085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
41087
41088 static bool
41089 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
41090 enum pragma_context context, bool *if_p)
41091 {
41092 tree *pc = NULL, stmt;
41093
41094 if (flag_openmp)
41095 omp_requires_mask
41096 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
41097
41098 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41099 {
41100 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41101 const char *p = IDENTIFIER_POINTER (id);
41102 enum tree_code ccode = ERROR_MARK;
41103
41104 if (strcmp (p, "teams") == 0)
41105 ccode = OMP_TEAMS;
41106 else if (strcmp (p, "parallel") == 0)
41107 ccode = OMP_PARALLEL;
41108 else if (strcmp (p, "simd") == 0)
41109 ccode = OMP_SIMD;
41110 if (ccode != ERROR_MARK)
41111 {
41112 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
41113 char p_name[sizeof ("#pragma omp target teams distribute "
41114 "parallel for simd")];
41115
41116 cp_lexer_consume_token (parser->lexer);
41117 strcpy (p_name, "#pragma omp target");
41118 if (!flag_openmp) /* flag_openmp_simd */
41119 {
41120 tree stmt;
41121 switch (ccode)
41122 {
41123 case OMP_TEAMS:
41124 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
41125 OMP_TARGET_CLAUSE_MASK,
41126 cclauses, if_p);
41127 break;
41128 case OMP_PARALLEL:
41129 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
41130 OMP_TARGET_CLAUSE_MASK,
41131 cclauses, if_p);
41132 break;
41133 case OMP_SIMD:
41134 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
41135 OMP_TARGET_CLAUSE_MASK,
41136 cclauses, if_p);
41137 break;
41138 default:
41139 gcc_unreachable ();
41140 }
41141 return stmt != NULL_TREE;
41142 }
41143 keep_next_level (true);
41144 tree sb = begin_omp_structured_block (), ret;
41145 unsigned save = cp_parser_begin_omp_structured_block (parser);
41146 switch (ccode)
41147 {
41148 case OMP_TEAMS:
41149 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
41150 OMP_TARGET_CLAUSE_MASK, cclauses,
41151 if_p);
41152 break;
41153 case OMP_PARALLEL:
41154 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
41155 OMP_TARGET_CLAUSE_MASK, cclauses,
41156 if_p);
41157 break;
41158 case OMP_SIMD:
41159 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
41160 OMP_TARGET_CLAUSE_MASK, cclauses,
41161 if_p);
41162 break;
41163 default:
41164 gcc_unreachable ();
41165 }
41166 cp_parser_end_omp_structured_block (parser, save);
41167 tree body = finish_omp_structured_block (sb);
41168 if (ret == NULL_TREE)
41169 return false;
41170 if (ccode == OMP_TEAMS && !processing_template_decl)
41171 {
41172 /* For combined target teams, ensure the num_teams and
41173 thread_limit clause expressions are evaluated on the host,
41174 before entering the target construct. */
41175 tree c;
41176 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
41177 c; c = OMP_CLAUSE_CHAIN (c))
41178 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
41179 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
41180 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
41181 {
41182 tree expr = OMP_CLAUSE_OPERAND (c, 0);
41183 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
41184 if (expr == error_mark_node)
41185 continue;
41186 tree tmp = TARGET_EXPR_SLOT (expr);
41187 add_stmt (expr);
41188 OMP_CLAUSE_OPERAND (c, 0) = expr;
41189 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
41190 OMP_CLAUSE_FIRSTPRIVATE);
41191 OMP_CLAUSE_DECL (tc) = tmp;
41192 OMP_CLAUSE_CHAIN (tc)
41193 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
41194 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
41195 }
41196 }
41197 tree stmt = make_node (OMP_TARGET);
41198 TREE_TYPE (stmt) = void_type_node;
41199 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
41200 OMP_TARGET_BODY (stmt) = body;
41201 OMP_TARGET_COMBINED (stmt) = 1;
41202 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41203 add_stmt (stmt);
41204 pc = &OMP_TARGET_CLAUSES (stmt);
41205 goto check_clauses;
41206 }
41207 else if (!flag_openmp) /* flag_openmp_simd */
41208 {
41209 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41210 return false;
41211 }
41212 else if (strcmp (p, "data") == 0)
41213 {
41214 cp_lexer_consume_token (parser->lexer);
41215 cp_parser_omp_target_data (parser, pragma_tok, if_p);
41216 return true;
41217 }
41218 else if (strcmp (p, "enter") == 0)
41219 {
41220 cp_lexer_consume_token (parser->lexer);
41221 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
41222 return false;
41223 }
41224 else if (strcmp (p, "exit") == 0)
41225 {
41226 cp_lexer_consume_token (parser->lexer);
41227 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
41228 return false;
41229 }
41230 else if (strcmp (p, "update") == 0)
41231 {
41232 cp_lexer_consume_token (parser->lexer);
41233 return cp_parser_omp_target_update (parser, pragma_tok, context);
41234 }
41235 }
41236 if (!flag_openmp) /* flag_openmp_simd */
41237 {
41238 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41239 return false;
41240 }
41241
41242 stmt = make_node (OMP_TARGET);
41243 TREE_TYPE (stmt) = void_type_node;
41244
41245 OMP_TARGET_CLAUSES (stmt)
41246 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
41247 "#pragma omp target", pragma_tok);
41248 c_omp_adjust_map_clauses (OMP_TARGET_CLAUSES (stmt), true);
41249
41250 pc = &OMP_TARGET_CLAUSES (stmt);
41251 keep_next_level (true);
41252 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
41253
41254 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41255 add_stmt (stmt);
41256
41257 check_clauses:
41258 while (*pc)
41259 {
41260 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
41261 switch (OMP_CLAUSE_MAP_KIND (*pc))
41262 {
41263 case GOMP_MAP_TO:
41264 case GOMP_MAP_ALWAYS_TO:
41265 case GOMP_MAP_FROM:
41266 case GOMP_MAP_ALWAYS_FROM:
41267 case GOMP_MAP_TOFROM:
41268 case GOMP_MAP_ALWAYS_TOFROM:
41269 case GOMP_MAP_ALLOC:
41270 case GOMP_MAP_FIRSTPRIVATE_POINTER:
41271 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
41272 case GOMP_MAP_ALWAYS_POINTER:
41273 case GOMP_MAP_ATTACH_DETACH:
41274 break;
41275 default:
41276 error_at (OMP_CLAUSE_LOCATION (*pc),
41277 "%<#pragma omp target%> with map-type other "
41278 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
41279 "on %<map%> clause");
41280 *pc = OMP_CLAUSE_CHAIN (*pc);
41281 continue;
41282 }
41283 pc = &OMP_CLAUSE_CHAIN (*pc);
41284 }
41285 return true;
41286 }
41287
41288 /* OpenACC 2.0:
41289 # pragma acc cache (variable-list) new-line
41290 */
41291
41292 static tree
41293 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
41294 {
41295 tree stmt, clauses;
41296
41297 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
41298 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
41299
41300 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
41301
41302 stmt = make_node (OACC_CACHE);
41303 TREE_TYPE (stmt) = void_type_node;
41304 OACC_CACHE_CLAUSES (stmt) = clauses;
41305 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41306 add_stmt (stmt);
41307
41308 return stmt;
41309 }
41310
41311 /* OpenACC 2.0:
41312 # pragma acc data oacc-data-clause[optseq] new-line
41313 structured-block */
41314
41315 #define OACC_DATA_CLAUSE_MASK \
41316 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
41318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
41322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
41323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
41325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
41326
41327 static tree
41328 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41329 {
41330 tree stmt, clauses, block;
41331 unsigned int save;
41332
41333 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
41334 "#pragma acc data", pragma_tok);
41335
41336 block = begin_omp_parallel ();
41337 save = cp_parser_begin_omp_structured_block (parser);
41338 cp_parser_statement (parser, NULL_TREE, false, if_p);
41339 cp_parser_end_omp_structured_block (parser, save);
41340 stmt = finish_oacc_data (clauses, block);
41341 return stmt;
41342 }
41343
41344 /* OpenACC 2.0:
41345 # pragma acc host_data <clauses> new-line
41346 structured-block */
41347
41348 #define OACC_HOST_DATA_CLAUSE_MASK \
41349 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
41350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
41352
41353 static tree
41354 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41355 {
41356 tree stmt, clauses, block;
41357 unsigned int save;
41358
41359 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
41360 "#pragma acc host_data", pragma_tok);
41361
41362 block = begin_omp_parallel ();
41363 save = cp_parser_begin_omp_structured_block (parser);
41364 cp_parser_statement (parser, NULL_TREE, false, if_p);
41365 cp_parser_end_omp_structured_block (parser, save);
41366 stmt = finish_oacc_host_data (clauses, block);
41367 return stmt;
41368 }
41369
41370 /* OpenACC 2.0:
41371 # pragma acc declare oacc-data-clause[optseq] new-line
41372 */
41373
41374 #define OACC_DECLARE_CLAUSE_MASK \
41375 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
41376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
41380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
41381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
41382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
41383
41384 static tree
41385 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
41386 {
41387 tree clauses, stmt;
41388 bool error = false;
41389 bool found_in_scope = global_bindings_p ();
41390
41391 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
41392 "#pragma acc declare", pragma_tok, true);
41393
41394
41395 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
41396 {
41397 error_at (pragma_tok->location,
41398 "no valid clauses specified in %<#pragma acc declare%>");
41399 return NULL_TREE;
41400 }
41401
41402 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
41403 {
41404 location_t loc = OMP_CLAUSE_LOCATION (t);
41405 tree decl = OMP_CLAUSE_DECL (t);
41406 if (!DECL_P (decl))
41407 {
41408 error_at (loc, "array section in %<#pragma acc declare%>");
41409 error = true;
41410 continue;
41411 }
41412 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
41413 switch (OMP_CLAUSE_MAP_KIND (t))
41414 {
41415 case GOMP_MAP_FIRSTPRIVATE_POINTER:
41416 case GOMP_MAP_ALLOC:
41417 case GOMP_MAP_TO:
41418 case GOMP_MAP_FORCE_DEVICEPTR:
41419 case GOMP_MAP_DEVICE_RESIDENT:
41420 break;
41421
41422 case GOMP_MAP_LINK:
41423 if (!global_bindings_p ()
41424 && (TREE_STATIC (decl)
41425 || !DECL_EXTERNAL (decl)))
41426 {
41427 error_at (loc,
41428 "%qD must be a global variable in "
41429 "%<#pragma acc declare link%>",
41430 decl);
41431 error = true;
41432 continue;
41433 }
41434 break;
41435
41436 default:
41437 if (global_bindings_p ())
41438 {
41439 error_at (loc, "invalid OpenACC clause at file scope");
41440 error = true;
41441 continue;
41442 }
41443 if (DECL_EXTERNAL (decl))
41444 {
41445 error_at (loc,
41446 "invalid use of %<extern%> variable %qD "
41447 "in %<#pragma acc declare%>", decl);
41448 error = true;
41449 continue;
41450 }
41451 else if (TREE_PUBLIC (decl))
41452 {
41453 error_at (loc,
41454 "invalid use of %<global%> variable %qD "
41455 "in %<#pragma acc declare%>", decl);
41456 error = true;
41457 continue;
41458 }
41459 break;
41460 }
41461
41462 if (!found_in_scope)
41463 /* This seems to ignore the existence of cleanup scopes?
41464 What is the meaning for local extern decls? The local
41465 extern is in this scope, but it is referring to a decl that
41466 is namespace scope. */
41467 for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
41468 if (d == decl)
41469 {
41470 found_in_scope = true;
41471 break;
41472 }
41473 if (!found_in_scope)
41474 {
41475 error_at (loc,
41476 "%qD must be a variable declared in the same scope as "
41477 "%<#pragma acc declare%>", decl);
41478 error = true;
41479 continue;
41480 }
41481
41482 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
41483 || lookup_attribute ("omp declare target link",
41484 DECL_ATTRIBUTES (decl)))
41485 {
41486 error_at (loc, "variable %qD used more than once with "
41487 "%<#pragma acc declare%>", decl);
41488 error = true;
41489 continue;
41490 }
41491
41492 if (!error)
41493 {
41494 tree id;
41495
41496 if (DECL_LOCAL_DECL_P (decl))
41497 /* We need to mark the aliased decl, as that is the entity
41498 that is being referred to. This won't work for
41499 dependent variables, but it didn't work for them before
41500 DECL_LOCAL_DECL_P was a thing either. But then
41501 dependent local extern variable decls are as rare as
41502 hen's teeth. */
41503 if (auto alias = DECL_LOCAL_DECL_ALIAS (decl))
41504 decl = alias;
41505
41506 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
41507 id = get_identifier ("omp declare target link");
41508 else
41509 id = get_identifier ("omp declare target");
41510
41511 DECL_ATTRIBUTES (decl)
41512 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
41513 if (current_binding_level->kind == sk_namespace)
41514 {
41515 symtab_node *node = symtab_node::get (decl);
41516 if (node != NULL)
41517 {
41518 node->offloadable = 1;
41519 if (ENABLE_OFFLOADING)
41520 {
41521 g->have_offload = true;
41522 if (is_a <varpool_node *> (node))
41523 vec_safe_push (offload_vars, decl);
41524 }
41525 }
41526 }
41527 }
41528 }
41529
41530 if (error || current_binding_level->kind == sk_namespace)
41531 return NULL_TREE;
41532
41533 stmt = make_node (OACC_DECLARE);
41534 TREE_TYPE (stmt) = void_type_node;
41535 OACC_DECLARE_CLAUSES (stmt) = clauses;
41536 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41537
41538 add_stmt (stmt);
41539
41540 return NULL_TREE;
41541 }
41542
41543 /* OpenACC 2.0:
41544 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
41545
41546 or
41547
41548 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
41549
41550 LOC is the location of the #pragma token.
41551 */
41552
41553 #define OACC_ENTER_DATA_CLAUSE_MASK \
41554 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41560
41561 #define OACC_EXIT_DATA_CLAUSE_MASK \
41562 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
41566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
41567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
41568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41569
41570 static tree
41571 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
41572 bool enter)
41573 {
41574 location_t loc = pragma_tok->location;
41575 tree stmt, clauses;
41576 const char *p = "";
41577
41578 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41579 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
41580
41581 if (strcmp (p, "data") != 0)
41582 {
41583 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
41584 enter ? "enter" : "exit");
41585 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41586 return NULL_TREE;
41587 }
41588
41589 cp_lexer_consume_token (parser->lexer);
41590
41591 if (enter)
41592 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
41593 "#pragma acc enter data", pragma_tok);
41594 else
41595 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
41596 "#pragma acc exit data", pragma_tok);
41597
41598 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
41599 {
41600 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
41601 enter ? "enter" : "exit");
41602 return NULL_TREE;
41603 }
41604
41605 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
41606 TREE_TYPE (stmt) = void_type_node;
41607 OMP_STANDALONE_CLAUSES (stmt) = clauses;
41608 SET_EXPR_LOCATION (stmt, loc);
41609 add_stmt (stmt);
41610 return stmt;
41611 }
41612
41613 /* OpenACC 2.0:
41614 # pragma acc loop oacc-loop-clause[optseq] new-line
41615 structured-block */
41616
41617 #define OACC_LOOP_CLAUSE_MASK \
41618 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
41619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
41620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
41621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
41622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
41623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
41624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
41625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
41626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
41627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
41628
41629 static tree
41630 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
41631 omp_clause_mask mask, tree *cclauses, bool *if_p)
41632 {
41633 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
41634
41635 strcat (p_name, " loop");
41636 mask |= OACC_LOOP_CLAUSE_MASK;
41637
41638 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
41639 cclauses == NULL);
41640 if (cclauses)
41641 {
41642 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
41643 if (*cclauses)
41644 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
41645 if (clauses)
41646 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
41647 }
41648
41649 tree block = begin_omp_structured_block ();
41650 int save = cp_parser_begin_omp_structured_block (parser);
41651 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
41652 cp_parser_end_omp_structured_block (parser, save);
41653 add_stmt (finish_omp_structured_block (block));
41654
41655 return stmt;
41656 }
41657
41658 /* OpenACC 2.0:
41659 # pragma acc kernels oacc-kernels-clause[optseq] new-line
41660 structured-block
41661
41662 or
41663
41664 # pragma acc parallel oacc-parallel-clause[optseq] new-line
41665 structured-block
41666
41667 OpenACC 2.6:
41668
41669 # pragma acc serial oacc-serial-clause[optseq] new-line
41670 */
41671
41672 #define OACC_KERNELS_CLAUSE_MASK \
41673 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
41676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
41680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
41681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
41683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
41684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
41685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
41686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
41687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41688
41689 #define OACC_PARALLEL_CLAUSE_MASK \
41690 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
41693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
41697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
41698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
41699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
41701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
41702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
41703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
41704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
41705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
41706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
41707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41708
41709 #define OACC_SERIAL_CLAUSE_MASK \
41710 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
41713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
41717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
41718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
41720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
41721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
41722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
41723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
41724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41725
41726 static tree
41727 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
41728 char *p_name, bool *if_p)
41729 {
41730 omp_clause_mask mask;
41731 enum tree_code code;
41732 switch (cp_parser_pragma_kind (pragma_tok))
41733 {
41734 case PRAGMA_OACC_KERNELS:
41735 strcat (p_name, " kernels");
41736 mask = OACC_KERNELS_CLAUSE_MASK;
41737 code = OACC_KERNELS;
41738 break;
41739 case PRAGMA_OACC_PARALLEL:
41740 strcat (p_name, " parallel");
41741 mask = OACC_PARALLEL_CLAUSE_MASK;
41742 code = OACC_PARALLEL;
41743 break;
41744 case PRAGMA_OACC_SERIAL:
41745 strcat (p_name, " serial");
41746 mask = OACC_SERIAL_CLAUSE_MASK;
41747 code = OACC_SERIAL;
41748 break;
41749 default:
41750 gcc_unreachable ();
41751 }
41752
41753 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41754 {
41755 const char *p
41756 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
41757 if (strcmp (p, "loop") == 0)
41758 {
41759 cp_lexer_consume_token (parser->lexer);
41760 tree block = begin_omp_parallel ();
41761 tree clauses;
41762 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
41763 &clauses, if_p);
41764 protected_set_expr_location (stmt, pragma_tok->location);
41765 return finish_omp_construct (code, block, clauses);
41766 }
41767 }
41768
41769 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
41770
41771 tree block = begin_omp_parallel ();
41772 unsigned int save = cp_parser_begin_omp_structured_block (parser);
41773 cp_parser_statement (parser, NULL_TREE, false, if_p);
41774 cp_parser_end_omp_structured_block (parser, save);
41775 return finish_omp_construct (code, block, clauses);
41776 }
41777
41778 /* OpenACC 2.0:
41779 # pragma acc update oacc-update-clause[optseq] new-line
41780 */
41781
41782 #define OACC_UPDATE_CLAUSE_MASK \
41783 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
41785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
41786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
41788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
41789
41790 static tree
41791 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
41792 {
41793 tree stmt, clauses;
41794
41795 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
41796 "#pragma acc update", pragma_tok);
41797
41798 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
41799 {
41800 error_at (pragma_tok->location,
41801 "%<#pragma acc update%> must contain at least one "
41802 "%<device%> or %<host%> or %<self%> clause");
41803 return NULL_TREE;
41804 }
41805
41806 stmt = make_node (OACC_UPDATE);
41807 TREE_TYPE (stmt) = void_type_node;
41808 OACC_UPDATE_CLAUSES (stmt) = clauses;
41809 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41810 add_stmt (stmt);
41811 return stmt;
41812 }
41813
41814 /* OpenACC 2.0:
41815 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
41816
41817 LOC is the location of the #pragma token.
41818 */
41819
41820 #define OACC_WAIT_CLAUSE_MASK \
41821 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
41822
41823 static tree
41824 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
41825 {
41826 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
41827 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41828
41829 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
41830 list = cp_parser_oacc_wait_list (parser, loc, list);
41831
41832 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
41833 "#pragma acc wait", pragma_tok);
41834
41835 stmt = c_finish_oacc_wait (loc, list, clauses);
41836 stmt = finish_expr_stmt (stmt);
41837
41838 return stmt;
41839 }
41840
41841 /* OpenMP 4.0:
41842 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
41843
41844 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
41845 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
41846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
41847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
41848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
41849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
41850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
41851
41852 static void
41853 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
41854 enum pragma_context context,
41855 bool variant_p)
41856 {
41857 bool first_p = parser->omp_declare_simd == NULL;
41858 cp_omp_declare_simd_data data;
41859 if (first_p)
41860 {
41861 data.error_seen = false;
41862 data.fndecl_seen = false;
41863 data.variant_p = variant_p;
41864 data.tokens = vNULL;
41865 data.clauses = NULL_TREE;
41866 /* It is safe to take the address of a local variable; it will only be
41867 used while this scope is live. */
41868 parser->omp_declare_simd = &data;
41869 }
41870 else if (parser->omp_declare_simd->variant_p != variant_p)
41871 {
41872 error_at (pragma_tok->location,
41873 "%<#pragma omp declare %s%> followed by "
41874 "%<#pragma omp declare %s%>",
41875 parser->omp_declare_simd->variant_p ? "variant" : "simd",
41876 parser->omp_declare_simd->variant_p ? "simd" : "variant");
41877 parser->omp_declare_simd->error_seen = true;
41878 }
41879
41880 /* Store away all pragma tokens. */
41881 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41882 cp_lexer_consume_token (parser->lexer);
41883 cp_parser_require_pragma_eol (parser, pragma_tok);
41884 struct cp_token_cache *cp
41885 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
41886 parser->omp_declare_simd->tokens.safe_push (cp);
41887
41888 if (first_p)
41889 {
41890 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
41891 cp_parser_pragma (parser, context, NULL);
41892 switch (context)
41893 {
41894 case pragma_external:
41895 cp_parser_declaration (parser);
41896 break;
41897 case pragma_member:
41898 cp_parser_member_declaration (parser);
41899 break;
41900 case pragma_objc_icode:
41901 cp_parser_block_declaration (parser, /*statement_p=*/false);
41902 break;
41903 default:
41904 cp_parser_declaration_statement (parser);
41905 break;
41906 }
41907 if (parser->omp_declare_simd
41908 && !parser->omp_declare_simd->error_seen
41909 && !parser->omp_declare_simd->fndecl_seen)
41910 error_at (pragma_tok->location,
41911 "%<#pragma omp declare %s%> not immediately followed by "
41912 "function declaration or definition",
41913 parser->omp_declare_simd->variant_p ? "variant" : "simd");
41914 data.tokens.release ();
41915 parser->omp_declare_simd = NULL;
41916 }
41917 }
41918
41919 static const char *const omp_construct_selectors[] = {
41920 "simd", "target", "teams", "parallel", "for", NULL };
41921 static const char *const omp_device_selectors[] = {
41922 "kind", "isa", "arch", NULL };
41923 static const char *const omp_implementation_selectors[] = {
41924 "vendor", "extension", "atomic_default_mem_order", "unified_address",
41925 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
41926 static const char *const omp_user_selectors[] = {
41927 "condition", NULL };
41928
41929 /* OpenMP 5.0:
41930
41931 trait-selector:
41932 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
41933
41934 trait-score:
41935 score(score-expression) */
41936
41937 static tree
41938 cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
41939 {
41940 tree ret = NULL_TREE;
41941 do
41942 {
41943 tree selector;
41944 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
41945 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41946 selector = cp_lexer_peek_token (parser->lexer)->u.value;
41947 else
41948 {
41949 cp_parser_error (parser, "expected trait selector name");
41950 return error_mark_node;
41951 }
41952
41953 tree properties = NULL_TREE;
41954 const char *const *selectors = NULL;
41955 bool allow_score = true;
41956 bool allow_user = false;
41957 int property_limit = 0;
41958 enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
41959 CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
41960 CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
41961 switch (IDENTIFIER_POINTER (set)[0])
41962 {
41963 case 'c': /* construct */
41964 selectors = omp_construct_selectors;
41965 allow_score = false;
41966 property_limit = 1;
41967 property_kind = CTX_PROPERTY_SIMD;
41968 break;
41969 case 'd': /* device */
41970 selectors = omp_device_selectors;
41971 allow_score = false;
41972 allow_user = true;
41973 property_limit = 3;
41974 property_kind = CTX_PROPERTY_NAME_LIST;
41975 break;
41976 case 'i': /* implementation */
41977 selectors = omp_implementation_selectors;
41978 allow_user = true;
41979 property_limit = 3;
41980 property_kind = CTX_PROPERTY_NAME_LIST;
41981 break;
41982 case 'u': /* user */
41983 selectors = omp_user_selectors;
41984 property_limit = 1;
41985 property_kind = CTX_PROPERTY_EXPR;
41986 break;
41987 default:
41988 gcc_unreachable ();
41989 }
41990 for (int i = 0; ; i++)
41991 {
41992 if (selectors[i] == NULL)
41993 {
41994 if (allow_user)
41995 {
41996 property_kind = CTX_PROPERTY_USER;
41997 break;
41998 }
41999 else
42000 {
42001 error ("selector %qs not allowed for context selector "
42002 "set %qs", IDENTIFIER_POINTER (selector),
42003 IDENTIFIER_POINTER (set));
42004 cp_lexer_consume_token (parser->lexer);
42005 return error_mark_node;
42006 }
42007 }
42008 if (i == property_limit)
42009 property_kind = CTX_PROPERTY_NONE;
42010 if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
42011 break;
42012 }
42013 if (property_kind == CTX_PROPERTY_NAME_LIST
42014 && IDENTIFIER_POINTER (set)[0] == 'i'
42015 && strcmp (IDENTIFIER_POINTER (selector),
42016 "atomic_default_mem_order") == 0)
42017 property_kind = CTX_PROPERTY_ID;
42018
42019 cp_lexer_consume_token (parser->lexer);
42020
42021 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
42022 {
42023 if (property_kind == CTX_PROPERTY_NONE)
42024 {
42025 error ("selector %qs does not accept any properties",
42026 IDENTIFIER_POINTER (selector));
42027 return error_mark_node;
42028 }
42029
42030 matching_parens parens;
42031 parens.consume_open (parser);
42032
42033 cp_token *token = cp_lexer_peek_token (parser->lexer);
42034 if (allow_score
42035 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
42036 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
42037 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
42038 {
42039 cp_lexer_save_tokens (parser->lexer);
42040 cp_lexer_consume_token (parser->lexer);
42041 cp_lexer_consume_token (parser->lexer);
42042 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
42043 true)
42044 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
42045 {
42046 cp_lexer_rollback_tokens (parser->lexer);
42047 cp_lexer_consume_token (parser->lexer);
42048
42049 matching_parens parens2;
42050 parens2.require_open (parser);
42051 tree score = cp_parser_constant_expression (parser);
42052 if (!parens2.require_close (parser))
42053 cp_parser_skip_to_closing_parenthesis (parser, true,
42054 false, true);
42055 cp_parser_require (parser, CPP_COLON, RT_COLON);
42056 if (score != error_mark_node)
42057 {
42058 score = fold_non_dependent_expr (score);
42059 if (value_dependent_expression_p (score))
42060 properties = tree_cons (get_identifier (" score"),
42061 score, properties);
42062 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
42063 || TREE_CODE (score) != INTEGER_CST)
42064 error_at (token->location, "score argument must be "
42065 "constant integer expression");
42066 else if (tree_int_cst_sgn (score) < 0)
42067 error_at (token->location, "score argument must be "
42068 "non-negative");
42069 else
42070 properties = tree_cons (get_identifier (" score"),
42071 score, properties);
42072 }
42073 }
42074 else
42075 cp_lexer_rollback_tokens (parser->lexer);
42076
42077 token = cp_lexer_peek_token (parser->lexer);
42078 }
42079
42080 switch (property_kind)
42081 {
42082 tree t;
42083 case CTX_PROPERTY_USER:
42084 do
42085 {
42086 t = cp_parser_constant_expression (parser);
42087 if (t != error_mark_node)
42088 {
42089 t = fold_non_dependent_expr (t);
42090 if (TREE_CODE (t) == STRING_CST)
42091 properties = tree_cons (NULL_TREE, t, properties);
42092 else if (!value_dependent_expression_p (t)
42093 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
42094 || !tree_fits_shwi_p (t)))
42095 error_at (token->location, "property must be "
42096 "constant integer expression or string "
42097 "literal");
42098 else
42099 properties = tree_cons (NULL_TREE, t, properties);
42100 }
42101 else
42102 return error_mark_node;
42103
42104 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42105 cp_lexer_consume_token (parser->lexer);
42106 else
42107 break;
42108 }
42109 while (1);
42110 break;
42111 case CTX_PROPERTY_ID:
42112 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
42113 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42114 {
42115 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
42116 cp_lexer_consume_token (parser->lexer);
42117 properties = tree_cons (prop, NULL_TREE, properties);
42118 }
42119 else
42120 {
42121 cp_parser_error (parser, "expected identifier");
42122 return error_mark_node;
42123 }
42124 break;
42125 case CTX_PROPERTY_NAME_LIST:
42126 do
42127 {
42128 tree prop = NULL_TREE, value = NULL_TREE;
42129 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
42130 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42131 {
42132 prop = cp_lexer_peek_token (parser->lexer)->u.value;
42133 cp_lexer_consume_token (parser->lexer);
42134 }
42135 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
42136 value = cp_parser_string_literal (parser, false, false);
42137 else
42138 {
42139 cp_parser_error (parser, "expected identifier or "
42140 "string literal");
42141 return error_mark_node;
42142 }
42143
42144 properties = tree_cons (prop, value, properties);
42145
42146 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42147 cp_lexer_consume_token (parser->lexer);
42148 else
42149 break;
42150 }
42151 while (1);
42152 break;
42153 case CTX_PROPERTY_EXPR:
42154 t = cp_parser_constant_expression (parser);
42155 if (t != error_mark_node)
42156 {
42157 t = fold_non_dependent_expr (t);
42158 if (!value_dependent_expression_p (t)
42159 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
42160 || !tree_fits_shwi_p (t)))
42161 error_at (token->location, "property must be "
42162 "constant integer expression");
42163 else
42164 properties = tree_cons (NULL_TREE, t, properties);
42165 }
42166 else
42167 return error_mark_node;
42168 break;
42169 case CTX_PROPERTY_SIMD:
42170 if (!has_parms_p)
42171 {
42172 error_at (token->location, "properties for %<simd%> "
42173 "selector may not be specified in "
42174 "%<metadirective%>");
42175 return error_mark_node;
42176 }
42177 properties
42178 = cp_parser_omp_all_clauses (parser,
42179 OMP_DECLARE_SIMD_CLAUSE_MASK,
42180 "simd", NULL, true, 2);
42181 break;
42182 default:
42183 gcc_unreachable ();
42184 }
42185
42186 if (!parens.require_close (parser))
42187 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
42188
42189 properties = nreverse (properties);
42190 }
42191 else if (property_kind == CTX_PROPERTY_NAME_LIST
42192 || property_kind == CTX_PROPERTY_ID
42193 || property_kind == CTX_PROPERTY_EXPR)
42194 {
42195 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
42196 return error_mark_node;
42197 }
42198
42199 ret = tree_cons (selector, properties, ret);
42200
42201 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42202 cp_lexer_consume_token (parser->lexer);
42203 else
42204 break;
42205 }
42206 while (1);
42207
42208 return nreverse (ret);
42209 }
42210
42211 /* OpenMP 5.0:
42212
42213 trait-set-selector[,trait-set-selector[,...]]
42214
42215 trait-set-selector:
42216 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
42217
42218 trait-set-selector-name:
42219 constructor
42220 device
42221 implementation
42222 user */
42223
42224 static tree
42225 cp_parser_omp_context_selector_specification (cp_parser *parser,
42226 bool has_parms_p)
42227 {
42228 tree ret = NULL_TREE;
42229 do
42230 {
42231 const char *setp = "";
42232 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42233 setp
42234 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
42235 switch (setp[0])
42236 {
42237 case 'c':
42238 if (strcmp (setp, "construct") == 0)
42239 setp = NULL;
42240 break;
42241 case 'd':
42242 if (strcmp (setp, "device") == 0)
42243 setp = NULL;
42244 break;
42245 case 'i':
42246 if (strcmp (setp, "implementation") == 0)
42247 setp = NULL;
42248 break;
42249 case 'u':
42250 if (strcmp (setp, "user") == 0)
42251 setp = NULL;
42252 break;
42253 default:
42254 break;
42255 }
42256 if (setp)
42257 {
42258 cp_parser_error (parser, "expected %<construct%>, %<device%>, "
42259 "%<implementation%> or %<user%>");
42260 return error_mark_node;
42261 }
42262
42263 tree set = cp_lexer_peek_token (parser->lexer)->u.value;
42264 cp_lexer_consume_token (parser->lexer);
42265
42266 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42267 return error_mark_node;
42268
42269 matching_braces braces;
42270 if (!braces.require_open (parser))
42271 return error_mark_node;
42272
42273 tree selectors
42274 = cp_parser_omp_context_selector (parser, set, has_parms_p);
42275 if (selectors == error_mark_node)
42276 {
42277 cp_parser_skip_to_closing_brace (parser);
42278 ret = error_mark_node;
42279 }
42280 else if (ret != error_mark_node)
42281 ret = tree_cons (set, selectors, ret);
42282
42283 braces.require_close (parser);
42284
42285 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42286 cp_lexer_consume_token (parser->lexer);
42287 else
42288 break;
42289 }
42290 while (1);
42291
42292 if (ret == error_mark_node)
42293 return ret;
42294 return nreverse (ret);
42295 }
42296
42297 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
42298 that into "omp declare variant base" attribute. */
42299
42300 static tree
42301 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
42302 tree attrs)
42303 {
42304 matching_parens parens;
42305 if (!parens.require_open (parser))
42306 {
42307 fail:
42308 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42309 return attrs;
42310 }
42311
42312 bool template_p;
42313 cp_id_kind idk = CP_ID_KIND_NONE;
42314 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
42315 cp_expr varid
42316 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
42317 /*check_dependency_p=*/true,
42318 /*template_p=*/&template_p,
42319 /*declarator_p=*/false,
42320 /*optional_p=*/false);
42321 parens.require_close (parser);
42322
42323 tree variant;
42324 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
42325 || TREE_CODE (varid) == TYPE_DECL
42326 || varid == error_mark_node)
42327 variant = varid;
42328 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
42329 variant = NULL_TREE;
42330 else
42331 {
42332 tree ambiguous_decls;
42333 variant = cp_parser_lookup_name (parser, varid, none_type,
42334 template_p, /*is_namespace=*/false,
42335 /*check_dependency=*/true,
42336 &ambiguous_decls,
42337 varid.get_location ());
42338 if (ambiguous_decls)
42339 variant = NULL_TREE;
42340 }
42341 if (variant == NULL_TREE)
42342 variant = error_mark_node;
42343 else if (TREE_CODE (variant) != SCOPE_REF)
42344 {
42345 const char *error_msg;
42346 variant
42347 = finish_id_expression (varid, variant, parser->scope,
42348 &idk, false, true,
42349 &parser->non_integral_constant_expression_p,
42350 template_p, true, false, false, &error_msg,
42351 varid.get_location ());
42352 if (error_msg)
42353 cp_parser_error (parser, error_msg);
42354 }
42355 location_t caret_loc = get_pure_location (varid.get_location ());
42356 location_t start_loc = get_start (varid_token->location);
42357 location_t finish_loc = get_finish (varid.get_location ());
42358 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
42359
42360 const char *clause = "";
42361 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
42362 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42363 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
42364 if (strcmp (clause, "match"))
42365 {
42366 cp_parser_error (parser, "expected %<match%>");
42367 goto fail;
42368 }
42369
42370 cp_lexer_consume_token (parser->lexer);
42371
42372 if (!parens.require_open (parser))
42373 goto fail;
42374
42375 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
42376 if (ctx == error_mark_node)
42377 goto fail;
42378 ctx = c_omp_check_context_selector (match_loc, ctx);
42379 if (ctx != error_mark_node && variant != error_mark_node)
42380 {
42381 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
42382 match_loc);
42383 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
42384 loc_node = tree_cons (match_loc_node,
42385 build_int_cst (integer_type_node, idk),
42386 build_tree_list (loc_node, integer_zero_node));
42387 attrs = tree_cons (get_identifier ("omp declare variant base"),
42388 tree_cons (variant, ctx, loc_node), attrs);
42389 if (processing_template_decl)
42390 ATTR_IS_DEPENDENT (attrs) = 1;
42391 }
42392
42393 parens.require_close (parser);
42394 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42395 return attrs;
42396 }
42397
42398
42399 /* Finalize #pragma omp declare simd clauses after direct declarator has
42400 been parsed, and put that into "omp declare simd" attribute. */
42401
42402 static tree
42403 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
42404 {
42405 struct cp_token_cache *ce;
42406 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
42407 int i;
42408
42409 if (!data->error_seen && data->fndecl_seen)
42410 {
42411 error ("%<#pragma omp declare %s%> not immediately followed by "
42412 "a single function declaration or definition",
42413 data->variant_p ? "variant" : "simd");
42414 data->error_seen = true;
42415 }
42416 if (data->error_seen)
42417 return attrs;
42418
42419 FOR_EACH_VEC_ELT (data->tokens, i, ce)
42420 {
42421 tree c, cl;
42422
42423 cp_parser_push_lexer_for_tokens (parser, ce);
42424 parser->lexer->in_pragma = true;
42425 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
42426 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
42427 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42428 const char *kind = IDENTIFIER_POINTER (id);
42429 cp_lexer_consume_token (parser->lexer);
42430 if (strcmp (kind, "simd") == 0)
42431 {
42432 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
42433 "#pragma omp declare simd",
42434 pragma_tok);
42435 if (cl)
42436 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
42437 c = build_tree_list (get_identifier ("omp declare simd"), cl);
42438 TREE_CHAIN (c) = attrs;
42439 if (processing_template_decl)
42440 ATTR_IS_DEPENDENT (c) = 1;
42441 attrs = c;
42442 }
42443 else
42444 {
42445 gcc_assert (strcmp (kind, "variant") == 0);
42446 attrs = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
42447 }
42448 cp_parser_pop_lexer (parser);
42449 }
42450
42451 data->fndecl_seen = true;
42452 return attrs;
42453 }
42454
42455
42456 /* OpenMP 4.0:
42457 # pragma omp declare target new-line
42458 declarations and definitions
42459 # pragma omp end declare target new-line
42460
42461 OpenMP 4.5:
42462 # pragma omp declare target ( extended-list ) new-line
42463
42464 # pragma omp declare target declare-target-clauses[seq] new-line */
42465
42466 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
42467 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
42468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
42469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
42470
42471 static void
42472 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
42473 {
42474 tree clauses = NULL_TREE;
42475 int device_type = 0;
42476 bool only_device_type = true;
42477 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42478 clauses
42479 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
42480 "#pragma omp declare target", pragma_tok);
42481 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
42482 {
42483 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
42484 clauses);
42485 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
42486 cp_parser_require_pragma_eol (parser, pragma_tok);
42487 }
42488 else
42489 {
42490 cp_parser_require_pragma_eol (parser, pragma_tok);
42491 scope_chain->omp_declare_target_attribute++;
42492 return;
42493 }
42494 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
42495 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
42496 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
42497 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
42498 {
42499 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
42500 continue;
42501 tree t = OMP_CLAUSE_DECL (c), id;
42502 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
42503 tree at2 = lookup_attribute ("omp declare target link",
42504 DECL_ATTRIBUTES (t));
42505 only_device_type = false;
42506 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
42507 {
42508 id = get_identifier ("omp declare target link");
42509 std::swap (at1, at2);
42510 }
42511 else
42512 id = get_identifier ("omp declare target");
42513 if (at2)
42514 {
42515 error_at (OMP_CLAUSE_LOCATION (c),
42516 "%qD specified both in declare target %<link%> and %<to%>"
42517 " clauses", t);
42518 continue;
42519 }
42520 if (!at1)
42521 {
42522 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
42523 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
42524 continue;
42525
42526 symtab_node *node = symtab_node::get (t);
42527 if (node != NULL)
42528 {
42529 node->offloadable = 1;
42530 if (ENABLE_OFFLOADING)
42531 {
42532 g->have_offload = true;
42533 if (is_a <varpool_node *> (node))
42534 vec_safe_push (offload_vars, t);
42535 }
42536 }
42537 }
42538 if (TREE_CODE (t) != FUNCTION_DECL)
42539 continue;
42540 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
42541 {
42542 tree at3 = lookup_attribute ("omp declare target host",
42543 DECL_ATTRIBUTES (t));
42544 if (at3 == NULL_TREE)
42545 {
42546 id = get_identifier ("omp declare target host");
42547 DECL_ATTRIBUTES (t)
42548 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
42549 }
42550 }
42551 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
42552 {
42553 tree at3 = lookup_attribute ("omp declare target nohost",
42554 DECL_ATTRIBUTES (t));
42555 if (at3 == NULL_TREE)
42556 {
42557 id = get_identifier ("omp declare target nohost");
42558 DECL_ATTRIBUTES (t)
42559 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
42560 }
42561 }
42562 }
42563 if (device_type && only_device_type)
42564 warning_at (OMP_CLAUSE_LOCATION (clauses), 0,
42565 "directive with only %<device_type%> clauses ignored");
42566 }
42567
42568 static void
42569 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
42570 {
42571 const char *p = "";
42572 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42573 {
42574 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42575 p = IDENTIFIER_POINTER (id);
42576 }
42577 if (strcmp (p, "declare") == 0)
42578 {
42579 cp_lexer_consume_token (parser->lexer);
42580 p = "";
42581 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42582 {
42583 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42584 p = IDENTIFIER_POINTER (id);
42585 }
42586 if (strcmp (p, "target") == 0)
42587 cp_lexer_consume_token (parser->lexer);
42588 else
42589 {
42590 cp_parser_error (parser, "expected %<target%>");
42591 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42592 return;
42593 }
42594 }
42595 else
42596 {
42597 cp_parser_error (parser, "expected %<declare%>");
42598 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42599 return;
42600 }
42601 cp_parser_require_pragma_eol (parser, pragma_tok);
42602 if (!scope_chain->omp_declare_target_attribute)
42603 error_at (pragma_tok->location,
42604 "%<#pragma omp end declare target%> without corresponding "
42605 "%<#pragma omp declare target%>");
42606 else
42607 scope_chain->omp_declare_target_attribute--;
42608 }
42609
42610 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
42611 expression and optional initializer clause of
42612 #pragma omp declare reduction. We store the expression(s) as
42613 either 3, 6 or 7 special statements inside of the artificial function's
42614 body. The first two statements are DECL_EXPRs for the artificial
42615 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
42616 expression that uses those variables.
42617 If there was any INITIALIZER clause, this is followed by further statements,
42618 the fourth and fifth statements are DECL_EXPRs for the artificial
42619 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
42620 constructor variant (first token after open paren is not omp_priv),
42621 then the sixth statement is a statement with the function call expression
42622 that uses the OMP_PRIV and optionally OMP_ORIG variable.
42623 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
42624 to initialize the OMP_PRIV artificial variable and there is seventh
42625 statement, a DECL_EXPR of the OMP_PRIV statement again. */
42626
42627 static bool
42628 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
42629 {
42630 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
42631 gcc_assert (TYPE_REF_P (type));
42632 type = TREE_TYPE (type);
42633 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
42634 DECL_ARTIFICIAL (omp_out) = 1;
42635 pushdecl (omp_out);
42636 add_decl_expr (omp_out);
42637 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
42638 DECL_ARTIFICIAL (omp_in) = 1;
42639 pushdecl (omp_in);
42640 add_decl_expr (omp_in);
42641 tree combiner;
42642 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
42643
42644 keep_next_level (true);
42645 tree block = begin_omp_structured_block ();
42646 combiner = cp_parser_expression (parser);
42647 finish_expr_stmt (combiner);
42648 block = finish_omp_structured_block (block);
42649 if (processing_template_decl)
42650 block = build_stmt (input_location, EXPR_STMT, block);
42651 add_stmt (block);
42652
42653 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
42654 return false;
42655
42656 const char *p = "";
42657 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42658 {
42659 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42660 p = IDENTIFIER_POINTER (id);
42661 }
42662
42663 if (strcmp (p, "initializer") == 0)
42664 {
42665 cp_lexer_consume_token (parser->lexer);
42666 matching_parens parens;
42667 if (!parens.require_open (parser))
42668 return false;
42669
42670 p = "";
42671 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42672 {
42673 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42674 p = IDENTIFIER_POINTER (id);
42675 }
42676
42677 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
42678 DECL_ARTIFICIAL (omp_priv) = 1;
42679 pushdecl (omp_priv);
42680 add_decl_expr (omp_priv);
42681 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
42682 DECL_ARTIFICIAL (omp_orig) = 1;
42683 pushdecl (omp_orig);
42684 add_decl_expr (omp_orig);
42685
42686 keep_next_level (true);
42687 block = begin_omp_structured_block ();
42688
42689 bool ctor = false;
42690 if (strcmp (p, "omp_priv") == 0)
42691 {
42692 bool is_direct_init, is_non_constant_init;
42693 ctor = true;
42694 cp_lexer_consume_token (parser->lexer);
42695 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
42696 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
42697 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
42698 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
42699 == CPP_CLOSE_PAREN
42700 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
42701 == CPP_CLOSE_PAREN))
42702 {
42703 finish_omp_structured_block (block);
42704 error ("invalid initializer clause");
42705 return false;
42706 }
42707 initializer = cp_parser_initializer (parser, &is_direct_init,
42708 &is_non_constant_init);
42709 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
42710 NULL_TREE, LOOKUP_ONLYCONVERTING);
42711 }
42712 else
42713 {
42714 cp_parser_parse_tentatively (parser);
42715 /* Don't create location wrapper nodes here. */
42716 auto_suppress_location_wrappers sentinel;
42717 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
42718 /*check_dependency_p=*/true,
42719 /*template_p=*/NULL,
42720 /*declarator_p=*/false,
42721 /*optional_p=*/false);
42722 vec<tree, va_gc> *args;
42723 if (fn_name == error_mark_node
42724 || cp_parser_error_occurred (parser)
42725 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
42726 || ((args = cp_parser_parenthesized_expression_list
42727 (parser, non_attr, /*cast_p=*/false,
42728 /*allow_expansion_p=*/true,
42729 /*non_constant_p=*/NULL)),
42730 cp_parser_error_occurred (parser)))
42731 {
42732 finish_omp_structured_block (block);
42733 cp_parser_abort_tentative_parse (parser);
42734 cp_parser_error (parser, "expected id-expression (arguments)");
42735 return false;
42736 }
42737 unsigned int i;
42738 tree arg;
42739 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
42740 if (arg == omp_priv
42741 || (TREE_CODE (arg) == ADDR_EXPR
42742 && TREE_OPERAND (arg, 0) == omp_priv))
42743 break;
42744 cp_parser_abort_tentative_parse (parser);
42745 if (arg == NULL_TREE)
42746 error ("one of the initializer call arguments should be %<omp_priv%>"
42747 " or %<&omp_priv%>");
42748 initializer = cp_parser_postfix_expression (parser, false, false, false,
42749 false, NULL);
42750 finish_expr_stmt (initializer);
42751 }
42752
42753 block = finish_omp_structured_block (block);
42754 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
42755 if (processing_template_decl)
42756 block = build_stmt (input_location, EXPR_STMT, block);
42757 add_stmt (block);
42758
42759 if (ctor)
42760 add_decl_expr (omp_orig);
42761
42762 if (!parens.require_close (parser))
42763 return false;
42764 }
42765
42766 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
42767 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
42768 UNKNOWN_LOCATION);
42769
42770 return true;
42771 }
42772
42773 /* OpenMP 4.0
42774 #pragma omp declare reduction (reduction-id : typename-list : expression) \
42775 initializer-clause[opt] new-line
42776
42777 initializer-clause:
42778 initializer (omp_priv initializer)
42779 initializer (function-name (argument-list)) */
42780
42781 static void
42782 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
42783 enum pragma_context)
42784 {
42785 auto_vec<tree> types;
42786 enum tree_code reduc_code = ERROR_MARK;
42787 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
42788 unsigned int i;
42789 cp_token *first_token;
42790 cp_token_cache *cp;
42791 int errs;
42792 void *p;
42793
42794 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
42795 p = obstack_alloc (&declarator_obstack, 0);
42796
42797 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
42798 goto fail;
42799
42800 switch (cp_lexer_peek_token (parser->lexer)->type)
42801 {
42802 case CPP_PLUS:
42803 reduc_code = PLUS_EXPR;
42804 break;
42805 case CPP_MULT:
42806 reduc_code = MULT_EXPR;
42807 break;
42808 case CPP_MINUS:
42809 reduc_code = MINUS_EXPR;
42810 break;
42811 case CPP_AND:
42812 reduc_code = BIT_AND_EXPR;
42813 break;
42814 case CPP_XOR:
42815 reduc_code = BIT_XOR_EXPR;
42816 break;
42817 case CPP_OR:
42818 reduc_code = BIT_IOR_EXPR;
42819 break;
42820 case CPP_AND_AND:
42821 reduc_code = TRUTH_ANDIF_EXPR;
42822 break;
42823 case CPP_OR_OR:
42824 reduc_code = TRUTH_ORIF_EXPR;
42825 break;
42826 case CPP_NAME:
42827 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
42828 break;
42829 default:
42830 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
42831 "%<|%>, %<&&%>, %<||%> or identifier");
42832 goto fail;
42833 }
42834
42835 if (reduc_code != ERROR_MARK)
42836 cp_lexer_consume_token (parser->lexer);
42837
42838 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
42839 if (reduc_id == error_mark_node)
42840 goto fail;
42841
42842 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
42843 goto fail;
42844
42845 /* Types may not be defined in declare reduction type list. */
42846 const char *saved_message;
42847 saved_message = parser->type_definition_forbidden_message;
42848 parser->type_definition_forbidden_message
42849 = G_("types may not be defined in declare reduction type list");
42850 bool saved_colon_corrects_to_scope_p;
42851 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
42852 parser->colon_corrects_to_scope_p = false;
42853 bool saved_colon_doesnt_start_class_def_p;
42854 saved_colon_doesnt_start_class_def_p
42855 = parser->colon_doesnt_start_class_def_p;
42856 parser->colon_doesnt_start_class_def_p = true;
42857
42858 while (true)
42859 {
42860 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42861 type = cp_parser_type_id (parser);
42862 if (type == error_mark_node)
42863 ;
42864 else if (ARITHMETIC_TYPE_P (type)
42865 && (orig_reduc_id == NULL_TREE
42866 || (TREE_CODE (type) != COMPLEX_TYPE
42867 && (id_equal (orig_reduc_id, "min")
42868 || id_equal (orig_reduc_id, "max")))))
42869 error_at (loc, "predeclared arithmetic type %qT in "
42870 "%<#pragma omp declare reduction%>", type);
42871 else if (FUNC_OR_METHOD_TYPE_P (type)
42872 || TREE_CODE (type) == ARRAY_TYPE)
42873 error_at (loc, "function or array type %qT in "
42874 "%<#pragma omp declare reduction%>", type);
42875 else if (TYPE_REF_P (type))
42876 error_at (loc, "reference type %qT in "
42877 "%<#pragma omp declare reduction%>", type);
42878 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
42879 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
42880 "type %qT in %<#pragma omp declare reduction%>", type);
42881 else
42882 types.safe_push (type);
42883
42884 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42885 cp_lexer_consume_token (parser->lexer);
42886 else
42887 break;
42888 }
42889
42890 /* Restore the saved message. */
42891 parser->type_definition_forbidden_message = saved_message;
42892 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
42893 parser->colon_doesnt_start_class_def_p
42894 = saved_colon_doesnt_start_class_def_p;
42895
42896 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
42897 || types.is_empty ())
42898 {
42899 fail:
42900 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42901 goto done;
42902 }
42903
42904 first_token = cp_lexer_peek_token (parser->lexer);
42905 cp = NULL;
42906 errs = errorcount;
42907 FOR_EACH_VEC_ELT (types, i, type)
42908 {
42909 tree fntype
42910 = build_function_type_list (void_type_node,
42911 cp_build_reference_type (type, false),
42912 NULL_TREE);
42913 tree this_reduc_id = reduc_id;
42914 if (!dependent_type_p (type))
42915 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
42916 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
42917 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
42918 DECL_ARTIFICIAL (fndecl) = 1;
42919 DECL_EXTERNAL (fndecl) = 1;
42920 DECL_DECLARED_INLINE_P (fndecl) = 1;
42921 DECL_IGNORED_P (fndecl) = 1;
42922 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
42923 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
42924 DECL_ATTRIBUTES (fndecl)
42925 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
42926 DECL_ATTRIBUTES (fndecl));
42927 bool block_scope = false;
42928 if (current_function_decl)
42929 {
42930 block_scope = true;
42931 DECL_CONTEXT (fndecl) = current_function_decl;
42932 DECL_LOCAL_DECL_P (fndecl) = true;
42933 }
42934
42935 if (processing_template_decl)
42936 fndecl = push_template_decl (fndecl);
42937
42938 if (block_scope)
42939 {
42940 if (!processing_template_decl)
42941 pushdecl (fndecl);
42942 }
42943 else if (current_class_type)
42944 {
42945 if (cp == NULL)
42946 {
42947 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42948 cp_lexer_consume_token (parser->lexer);
42949 cp = cp_token_cache_new (first_token,
42950 cp_lexer_peek_nth_token (parser->lexer,
42951 2));
42952 }
42953 DECL_STATIC_FUNCTION_P (fndecl) = 1;
42954 finish_member_declaration (fndecl);
42955 DECL_PENDING_INLINE_INFO (fndecl) = cp;
42956 DECL_PENDING_INLINE_P (fndecl) = 1;
42957 vec_safe_push (unparsed_funs_with_definitions, fndecl);
42958 continue;
42959 }
42960 else
42961 {
42962 DECL_CONTEXT (fndecl) = current_namespace;
42963 tree d = pushdecl (fndecl);
42964 /* We should never meet a matched duplicate decl. */
42965 gcc_checking_assert (d == error_mark_node || d == fndecl);
42966 }
42967
42968 tree block = NULL_TREE;
42969 if (!block_scope)
42970 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
42971 else
42972 block = begin_omp_structured_block ();
42973 if (cp)
42974 {
42975 cp_parser_push_lexer_for_tokens (parser, cp);
42976 parser->lexer->in_pragma = true;
42977 }
42978
42979 bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
42980
42981 if (cp)
42982 cp_parser_pop_lexer (parser);
42983 if (!block_scope)
42984 finish_function (/*inline_p=*/false);
42985 else
42986 {
42987 DECL_CONTEXT (fndecl) = current_function_decl;
42988 if (DECL_TEMPLATE_INFO (fndecl))
42989 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
42990 }
42991 if (!ok)
42992 goto fail;
42993
42994 if (block_scope)
42995 {
42996 block = finish_omp_structured_block (block);
42997 if (TREE_CODE (block) == BIND_EXPR)
42998 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
42999 else if (TREE_CODE (block) == STATEMENT_LIST)
43000 DECL_SAVED_TREE (fndecl) = block;
43001 if (processing_template_decl)
43002 add_decl_expr (fndecl);
43003 }
43004
43005 cp_check_omp_declare_reduction (fndecl);
43006 if (cp == NULL && types.length () > 1)
43007 cp = cp_token_cache_new (first_token,
43008 cp_lexer_peek_nth_token (parser->lexer, 2));
43009 if (errs != errorcount)
43010 break;
43011 }
43012
43013 cp_parser_require_pragma_eol (parser, pragma_tok);
43014
43015 done:
43016 /* Free any declarators allocated. */
43017 obstack_free (&declarator_obstack, p);
43018 }
43019
43020 /* OpenMP 4.0
43021 #pragma omp declare simd declare-simd-clauses[optseq] new-line
43022 #pragma omp declare reduction (reduction-id : typename-list : expression) \
43023 initializer-clause[opt] new-line
43024 #pragma omp declare target new-line
43025
43026 OpenMP 5.0
43027 #pragma omp declare variant (identifier) match (context-selector) */
43028
43029 static bool
43030 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
43031 enum pragma_context context)
43032 {
43033 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43034 {
43035 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43036 const char *p = IDENTIFIER_POINTER (id);
43037
43038 if (strcmp (p, "simd") == 0)
43039 {
43040 cp_lexer_consume_token (parser->lexer);
43041 cp_parser_omp_declare_simd (parser, pragma_tok,
43042 context, false);
43043 return true;
43044 }
43045 if (flag_openmp && strcmp (p, "variant") == 0)
43046 {
43047 cp_lexer_consume_token (parser->lexer);
43048 cp_parser_omp_declare_simd (parser, pragma_tok,
43049 context, true);
43050 return true;
43051 }
43052 cp_ensure_no_omp_declare_simd (parser);
43053 if (strcmp (p, "reduction") == 0)
43054 {
43055 cp_lexer_consume_token (parser->lexer);
43056 cp_parser_omp_declare_reduction (parser, pragma_tok,
43057 context);
43058 return false;
43059 }
43060 if (!flag_openmp) /* flag_openmp_simd */
43061 {
43062 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43063 return false;
43064 }
43065 if (strcmp (p, "target") == 0)
43066 {
43067 cp_lexer_consume_token (parser->lexer);
43068 cp_parser_omp_declare_target (parser, pragma_tok);
43069 return false;
43070 }
43071 }
43072 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
43073 "%<target%> or %<variant%>");
43074 cp_parser_require_pragma_eol (parser, pragma_tok);
43075 return false;
43076 }
43077
43078 /* OpenMP 5.0
43079 #pragma omp requires clauses[optseq] new-line */
43080
43081 static bool
43082 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
43083 {
43084 bool first = true;
43085 enum omp_requires new_req = (enum omp_requires) 0;
43086
43087 location_t loc = pragma_tok->location;
43088 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
43089 {
43090 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43091 cp_lexer_consume_token (parser->lexer);
43092
43093 first = false;
43094
43095 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43096 {
43097 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43098 const char *p = IDENTIFIER_POINTER (id);
43099 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
43100 enum omp_requires this_req = (enum omp_requires) 0;
43101
43102 if (!strcmp (p, "unified_address"))
43103 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
43104 else if (!strcmp (p, "unified_shared_memory"))
43105 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
43106 else if (!strcmp (p, "dynamic_allocators"))
43107 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
43108 else if (!strcmp (p, "reverse_offload"))
43109 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
43110 else if (!strcmp (p, "atomic_default_mem_order"))
43111 {
43112 cp_lexer_consume_token (parser->lexer);
43113
43114 matching_parens parens;
43115 if (parens.require_open (parser))
43116 {
43117 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43118 {
43119 id = cp_lexer_peek_token (parser->lexer)->u.value;
43120 p = IDENTIFIER_POINTER (id);
43121
43122 if (!strcmp (p, "seq_cst"))
43123 this_req
43124 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
43125 else if (!strcmp (p, "relaxed"))
43126 this_req
43127 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
43128 else if (!strcmp (p, "acq_rel"))
43129 this_req
43130 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
43131 }
43132 if (this_req == 0)
43133 {
43134 error_at (cp_lexer_peek_token (parser->lexer)->location,
43135 "expected %<seq_cst%>, %<relaxed%> or "
43136 "%<acq_rel%>");
43137 if (cp_lexer_nth_token_is (parser->lexer, 2,
43138 CPP_CLOSE_PAREN))
43139 cp_lexer_consume_token (parser->lexer);
43140 }
43141 else
43142 cp_lexer_consume_token (parser->lexer);
43143
43144 if (!parens.require_close (parser))
43145 cp_parser_skip_to_closing_parenthesis (parser,
43146 /*recovering=*/true,
43147 /*or_comma=*/false,
43148 /*consume_paren=*/
43149 true);
43150
43151 if (this_req == 0)
43152 {
43153 cp_parser_require_pragma_eol (parser, pragma_tok);
43154 return false;
43155 }
43156 }
43157 p = NULL;
43158 }
43159 else
43160 {
43161 error_at (cloc, "expected %<unified_address%>, "
43162 "%<unified_shared_memory%>, "
43163 "%<dynamic_allocators%>, "
43164 "%<reverse_offload%> "
43165 "or %<atomic_default_mem_order%> clause");
43166 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43167 return false;
43168 }
43169 if (p)
43170 sorry_at (cloc, "%qs clause on %<requires%> directive not "
43171 "supported yet", p);
43172 if (p)
43173 cp_lexer_consume_token (parser->lexer);
43174 if (this_req)
43175 {
43176 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
43177 {
43178 if ((this_req & new_req) != 0)
43179 error_at (cloc, "too many %qs clauses", p);
43180 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
43181 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
43182 error_at (cloc, "%qs clause used lexically after first "
43183 "target construct or offloading API", p);
43184 }
43185 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
43186 {
43187 error_at (cloc, "too many %qs clauses",
43188 "atomic_default_mem_order");
43189 this_req = (enum omp_requires) 0;
43190 }
43191 else if ((omp_requires_mask
43192 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
43193 {
43194 error_at (cloc, "more than one %<atomic_default_mem_order%>"
43195 " clause in a single compilation unit");
43196 this_req
43197 = (enum omp_requires)
43198 (omp_requires_mask
43199 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
43200 }
43201 else if ((omp_requires_mask
43202 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
43203 error_at (cloc, "%<atomic_default_mem_order%> clause used "
43204 "lexically after first %<atomic%> construct "
43205 "without memory order clause");
43206 new_req = (enum omp_requires) (new_req | this_req);
43207 omp_requires_mask
43208 = (enum omp_requires) (omp_requires_mask | this_req);
43209 continue;
43210 }
43211 }
43212 break;
43213 }
43214 cp_parser_require_pragma_eol (parser, pragma_tok);
43215
43216 if (new_req == 0)
43217 error_at (loc, "%<pragma omp requires%> requires at least one clause");
43218 return false;
43219 }
43220
43221
43222 /* OpenMP 4.5:
43223 #pragma omp taskloop taskloop-clause[optseq] new-line
43224 for-loop
43225
43226 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
43227 for-loop */
43228
43229 #define OMP_TASKLOOP_CLAUSE_MASK \
43230 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
43231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
43235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
43236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
43237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
43239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
43241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
43242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
43243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
43244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
43247
43248 static tree
43249 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
43250 char *p_name, omp_clause_mask mask, tree *cclauses,
43251 bool *if_p)
43252 {
43253 tree clauses, sb, ret;
43254 unsigned int save;
43255 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43256
43257 strcat (p_name, " taskloop");
43258 mask |= OMP_TASKLOOP_CLAUSE_MASK;
43259 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
43260 clause. */
43261 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
43262 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
43263
43264 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43265 {
43266 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43267 const char *p = IDENTIFIER_POINTER (id);
43268
43269 if (strcmp (p, "simd") == 0)
43270 {
43271 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43272 if (cclauses == NULL)
43273 cclauses = cclauses_buf;
43274
43275 cp_lexer_consume_token (parser->lexer);
43276 if (!flag_openmp) /* flag_openmp_simd */
43277 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43278 cclauses, if_p);
43279 sb = begin_omp_structured_block ();
43280 save = cp_parser_begin_omp_structured_block (parser);
43281 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43282 cclauses, if_p);
43283 cp_parser_end_omp_structured_block (parser, save);
43284 tree body = finish_omp_structured_block (sb);
43285 if (ret == NULL)
43286 return ret;
43287 ret = make_node (OMP_TASKLOOP);
43288 TREE_TYPE (ret) = void_type_node;
43289 OMP_FOR_BODY (ret) = body;
43290 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
43291 SET_EXPR_LOCATION (ret, loc);
43292 add_stmt (ret);
43293 return ret;
43294 }
43295 }
43296 if (!flag_openmp) /* flag_openmp_simd */
43297 {
43298 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43299 return NULL_TREE;
43300 }
43301
43302 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43303 cclauses == NULL);
43304 if (cclauses)
43305 {
43306 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
43307 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
43308 }
43309
43310 keep_next_level (true);
43311 sb = begin_omp_structured_block ();
43312 save = cp_parser_begin_omp_structured_block (parser);
43313
43314 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
43315 if_p);
43316
43317 cp_parser_end_omp_structured_block (parser, save);
43318 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
43319
43320 return ret;
43321 }
43322
43323
43324 /* OpenACC 2.0:
43325 # pragma acc routine oacc-routine-clause[optseq] new-line
43326 function-definition
43327
43328 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
43329 */
43330
43331 #define OACC_ROUTINE_CLAUSE_MASK \
43332 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
43333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
43334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
43335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
43336
43337
43338 /* Parse the OpenACC routine pragma. This has an optional '( name )'
43339 component, which must resolve to a declared namespace-scope
43340 function. The clauses are either processed directly (for a named
43341 function), or defered until the immediatley following declaration
43342 is parsed. */
43343
43344 static void
43345 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
43346 enum pragma_context context)
43347 {
43348 gcc_checking_assert (context == pragma_external);
43349 /* The checking for "another pragma following this one" in the "no optional
43350 '( name )'" case makes sure that we dont re-enter. */
43351 gcc_checking_assert (parser->oacc_routine == NULL);
43352
43353 cp_oacc_routine_data data;
43354 data.error_seen = false;
43355 data.fndecl_seen = false;
43356 data.tokens = vNULL;
43357 data.clauses = NULL_TREE;
43358 data.loc = pragma_tok->location;
43359 /* It is safe to take the address of a local variable; it will only be
43360 used while this scope is live. */
43361 parser->oacc_routine = &data;
43362
43363 /* Look for optional '( name )'. */
43364 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
43365 {
43366 matching_parens parens;
43367 parens.consume_open (parser); /* '(' */
43368
43369 /* We parse the name as an id-expression. If it resolves to
43370 anything other than a non-overloaded function at namespace
43371 scope, it's an error. */
43372 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
43373 tree name = cp_parser_id_expression (parser,
43374 /*template_keyword_p=*/false,
43375 /*check_dependency_p=*/false,
43376 /*template_p=*/NULL,
43377 /*declarator_p=*/false,
43378 /*optional_p=*/false);
43379 tree decl = (identifier_p (name)
43380 ? cp_parser_lookup_name_simple (parser, name, name_loc)
43381 : name);
43382 if (name != error_mark_node && decl == error_mark_node)
43383 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
43384
43385 if (decl == error_mark_node
43386 || !parens.require_close (parser))
43387 {
43388 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43389 parser->oacc_routine = NULL;
43390 return;
43391 }
43392
43393 data.clauses
43394 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
43395 "#pragma acc routine",
43396 cp_lexer_peek_token (parser->lexer));
43397 /* The clauses are in reverse order; fix that to make later diagnostic
43398 emission easier. */
43399 data.clauses = nreverse (data.clauses);
43400
43401 if (decl && is_overloaded_fn (decl)
43402 && (TREE_CODE (decl) != FUNCTION_DECL
43403 || DECL_FUNCTION_TEMPLATE_P (decl)))
43404 {
43405 error_at (name_loc,
43406 "%<#pragma acc routine%> names a set of overloads");
43407 parser->oacc_routine = NULL;
43408 return;
43409 }
43410
43411 /* Perhaps we should use the same rule as declarations in different
43412 namespaces? */
43413 if (!DECL_NAMESPACE_SCOPE_P (decl))
43414 {
43415 error_at (name_loc,
43416 "%qD does not refer to a namespace scope function", decl);
43417 parser->oacc_routine = NULL;
43418 return;
43419 }
43420
43421 if (TREE_CODE (decl) != FUNCTION_DECL)
43422 {
43423 error_at (name_loc, "%qD does not refer to a function", decl);
43424 parser->oacc_routine = NULL;
43425 return;
43426 }
43427
43428 cp_finalize_oacc_routine (parser, decl, false);
43429 parser->oacc_routine = NULL;
43430 }
43431 else /* No optional '( name )'. */
43432 {
43433 /* Store away all pragma tokens. */
43434 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
43435 cp_lexer_consume_token (parser->lexer);
43436 cp_parser_require_pragma_eol (parser, pragma_tok);
43437 struct cp_token_cache *cp
43438 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
43439 parser->oacc_routine->tokens.safe_push (cp);
43440
43441 /* Emit a helpful diagnostic if there's another pragma following this
43442 one. */
43443 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
43444 {
43445 cp_ensure_no_oacc_routine (parser);
43446 data.tokens.release ();
43447 /* ..., and then just keep going. */
43448 return;
43449 }
43450
43451 /* We only have to consider the pragma_external case here. */
43452 cp_parser_declaration (parser);
43453 if (parser->oacc_routine
43454 && !parser->oacc_routine->fndecl_seen)
43455 cp_ensure_no_oacc_routine (parser);
43456 else
43457 parser->oacc_routine = NULL;
43458 data.tokens.release ();
43459 }
43460 }
43461
43462 /* Finalize #pragma acc routine clauses after direct declarator has
43463 been parsed. */
43464
43465 static tree
43466 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
43467 {
43468 struct cp_token_cache *ce;
43469 cp_oacc_routine_data *data = parser->oacc_routine;
43470
43471 if (!data->error_seen && data->fndecl_seen)
43472 {
43473 error_at (data->loc,
43474 "%<#pragma acc routine%> not immediately followed by "
43475 "a single function declaration or definition");
43476 data->error_seen = true;
43477 }
43478 if (data->error_seen)
43479 return attrs;
43480
43481 gcc_checking_assert (data->tokens.length () == 1);
43482 ce = data->tokens[0];
43483
43484 cp_parser_push_lexer_for_tokens (parser, ce);
43485 parser->lexer->in_pragma = true;
43486 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
43487
43488 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
43489 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
43490 parser->oacc_routine->clauses
43491 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
43492 "#pragma acc routine", pragma_tok);
43493 /* The clauses are in reverse order; fix that to make later diagnostic
43494 emission easier. */
43495 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
43496 cp_parser_pop_lexer (parser);
43497 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
43498 fndecl_seen. */
43499
43500 return attrs;
43501 }
43502
43503 /* Apply any saved OpenACC routine clauses to a just-parsed
43504 declaration. */
43505
43506 static void
43507 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
43508 {
43509 if (__builtin_expect (parser->oacc_routine != NULL, 0))
43510 {
43511 /* Keep going if we're in error reporting mode. */
43512 if (parser->oacc_routine->error_seen
43513 || fndecl == error_mark_node)
43514 return;
43515
43516 if (parser->oacc_routine->fndecl_seen)
43517 {
43518 error_at (parser->oacc_routine->loc,
43519 "%<#pragma acc routine%> not immediately followed by"
43520 " a single function declaration or definition");
43521 parser->oacc_routine = NULL;
43522 return;
43523 }
43524 if (TREE_CODE (fndecl) != FUNCTION_DECL)
43525 {
43526 cp_ensure_no_oacc_routine (parser);
43527 return;
43528 }
43529
43530 int compatible
43531 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
43532 parser->oacc_routine->loc,
43533 "#pragma acc routine");
43534 if (compatible < 0)
43535 {
43536 parser->oacc_routine = NULL;
43537 return;
43538 }
43539 if (compatible > 0)
43540 {
43541 }
43542 else
43543 {
43544 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
43545 {
43546 error_at (parser->oacc_routine->loc,
43547 TREE_USED (fndecl)
43548 ? G_("%<#pragma acc routine%> must be applied before"
43549 " use")
43550 : G_("%<#pragma acc routine%> must be applied before"
43551 " definition"));
43552 parser->oacc_routine = NULL;
43553 return;
43554 }
43555
43556 /* Set the routine's level of parallelism. */
43557 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
43558 oacc_replace_fn_attrib (fndecl, dims);
43559
43560 /* Add an "omp declare target" attribute. */
43561 DECL_ATTRIBUTES (fndecl)
43562 = tree_cons (get_identifier ("omp declare target"),
43563 parser->oacc_routine->clauses,
43564 DECL_ATTRIBUTES (fndecl));
43565 }
43566
43567 /* Don't unset parser->oacc_routine here: we may still need it to
43568 diagnose wrong usage. But, remember that we've used this "#pragma acc
43569 routine". */
43570 parser->oacc_routine->fndecl_seen = true;
43571 }
43572 }
43573
43574 /* Main entry point to OpenMP statement pragmas. */
43575
43576 static void
43577 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43578 {
43579 tree stmt;
43580 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
43581 omp_clause_mask mask (0);
43582
43583 switch (cp_parser_pragma_kind (pragma_tok))
43584 {
43585 case PRAGMA_OACC_ATOMIC:
43586 cp_parser_omp_atomic (parser, pragma_tok, true);
43587 return;
43588 case PRAGMA_OACC_CACHE:
43589 stmt = cp_parser_oacc_cache (parser, pragma_tok);
43590 break;
43591 case PRAGMA_OACC_DATA:
43592 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
43593 break;
43594 case PRAGMA_OACC_ENTER_DATA:
43595 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
43596 break;
43597 case PRAGMA_OACC_EXIT_DATA:
43598 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
43599 break;
43600 case PRAGMA_OACC_HOST_DATA:
43601 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
43602 break;
43603 case PRAGMA_OACC_KERNELS:
43604 case PRAGMA_OACC_PARALLEL:
43605 case PRAGMA_OACC_SERIAL:
43606 strcpy (p_name, "#pragma acc");
43607 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
43608 break;
43609 case PRAGMA_OACC_LOOP:
43610 strcpy (p_name, "#pragma acc");
43611 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
43612 if_p);
43613 break;
43614 case PRAGMA_OACC_UPDATE:
43615 stmt = cp_parser_oacc_update (parser, pragma_tok);
43616 break;
43617 case PRAGMA_OACC_WAIT:
43618 stmt = cp_parser_oacc_wait (parser, pragma_tok);
43619 break;
43620 case PRAGMA_OMP_ATOMIC:
43621 cp_parser_omp_atomic (parser, pragma_tok, false);
43622 return;
43623 case PRAGMA_OMP_CRITICAL:
43624 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
43625 break;
43626 case PRAGMA_OMP_DISTRIBUTE:
43627 strcpy (p_name, "#pragma omp");
43628 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
43629 if_p);
43630 break;
43631 case PRAGMA_OMP_FOR:
43632 strcpy (p_name, "#pragma omp");
43633 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
43634 if_p);
43635 break;
43636 case PRAGMA_OMP_LOOP:
43637 strcpy (p_name, "#pragma omp");
43638 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
43639 if_p);
43640 break;
43641 case PRAGMA_OMP_MASTER:
43642 strcpy (p_name, "#pragma omp");
43643 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
43644 if_p);
43645 break;
43646 case PRAGMA_OMP_PARALLEL:
43647 strcpy (p_name, "#pragma omp");
43648 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
43649 if_p);
43650 break;
43651 case PRAGMA_OMP_SECTIONS:
43652 strcpy (p_name, "#pragma omp");
43653 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
43654 break;
43655 case PRAGMA_OMP_SIMD:
43656 strcpy (p_name, "#pragma omp");
43657 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
43658 if_p);
43659 break;
43660 case PRAGMA_OMP_SINGLE:
43661 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
43662 break;
43663 case PRAGMA_OMP_TASK:
43664 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
43665 break;
43666 case PRAGMA_OMP_TASKGROUP:
43667 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
43668 break;
43669 case PRAGMA_OMP_TASKLOOP:
43670 strcpy (p_name, "#pragma omp");
43671 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
43672 if_p);
43673 break;
43674 case PRAGMA_OMP_TEAMS:
43675 strcpy (p_name, "#pragma omp");
43676 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
43677 if_p);
43678 break;
43679 default:
43680 gcc_unreachable ();
43681 }
43682
43683 protected_set_expr_location (stmt, pragma_tok->location);
43684 }
43685 \f
43686 /* Transactional Memory parsing routines. */
43687
43688 /* Parse a transaction attribute.
43689
43690 txn-attribute:
43691 attribute
43692 [ [ identifier ] ]
43693
43694 We use this instead of cp_parser_attributes_opt for transactions to avoid
43695 the pedwarn in C++98 mode. */
43696
43697 static tree
43698 cp_parser_txn_attribute_opt (cp_parser *parser)
43699 {
43700 cp_token *token;
43701 tree attr_name, attr = NULL;
43702
43703 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
43704 return cp_parser_attributes_opt (parser);
43705
43706 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
43707 return NULL_TREE;
43708 cp_lexer_consume_token (parser->lexer);
43709 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
43710 goto error1;
43711
43712 token = cp_lexer_peek_token (parser->lexer);
43713 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
43714 {
43715 token = cp_lexer_consume_token (parser->lexer);
43716
43717 attr_name = (token->type == CPP_KEYWORD
43718 /* For keywords, use the canonical spelling,
43719 not the parsed identifier. */
43720 ? ridpointers[(int) token->keyword]
43721 : token->u.value);
43722 attr = build_tree_list (attr_name, NULL_TREE);
43723 }
43724 else
43725 cp_parser_error (parser, "expected identifier");
43726
43727 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
43728 error1:
43729 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
43730 return attr;
43731 }
43732
43733 /* Parse a __transaction_atomic or __transaction_relaxed statement.
43734
43735 transaction-statement:
43736 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
43737 compound-statement
43738 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
43739 */
43740
43741 static tree
43742 cp_parser_transaction (cp_parser *parser, cp_token *token)
43743 {
43744 unsigned char old_in = parser->in_transaction;
43745 unsigned char this_in = 1, new_in;
43746 enum rid keyword = token->keyword;
43747 tree stmt, attrs, noex;
43748
43749 cp_lexer_consume_token (parser->lexer);
43750
43751 if (keyword == RID_TRANSACTION_RELAXED
43752 || keyword == RID_SYNCHRONIZED)
43753 this_in |= TM_STMT_ATTR_RELAXED;
43754 else
43755 {
43756 attrs = cp_parser_txn_attribute_opt (parser);
43757 if (attrs)
43758 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
43759 }
43760
43761 /* Parse a noexcept specification. */
43762 if (keyword == RID_ATOMIC_NOEXCEPT)
43763 noex = boolean_true_node;
43764 else if (keyword == RID_ATOMIC_CANCEL)
43765 {
43766 /* cancel-and-throw is unimplemented. */
43767 sorry ("%<atomic_cancel%>");
43768 noex = NULL_TREE;
43769 }
43770 else
43771 noex = cp_parser_noexcept_specification_opt (parser,
43772 CP_PARSER_FLAGS_NONE,
43773 /*require_constexpr=*/true,
43774 /*consumed_expr=*/NULL,
43775 /*return_cond=*/true);
43776
43777 /* Keep track if we're in the lexical scope of an outer transaction. */
43778 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
43779
43780 stmt = begin_transaction_stmt (token->location, NULL, this_in);
43781
43782 parser->in_transaction = new_in;
43783 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
43784 parser->in_transaction = old_in;
43785
43786 finish_transaction_stmt (stmt, NULL, this_in, noex);
43787
43788 return stmt;
43789 }
43790
43791 /* Parse a __transaction_atomic or __transaction_relaxed expression.
43792
43793 transaction-expression:
43794 __transaction_atomic txn-noexcept-spec[opt] ( expression )
43795 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
43796 */
43797
43798 static tree
43799 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
43800 {
43801 unsigned char old_in = parser->in_transaction;
43802 unsigned char this_in = 1;
43803 cp_token *token;
43804 tree expr, noex;
43805 bool noex_expr;
43806 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43807
43808 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
43809 || keyword == RID_TRANSACTION_RELAXED);
43810
43811 if (!flag_tm)
43812 error_at (loc,
43813 keyword == RID_TRANSACTION_RELAXED
43814 ? G_("%<__transaction_relaxed%> without transactional memory "
43815 "support enabled")
43816 : G_("%<__transaction_atomic%> without transactional memory "
43817 "support enabled"));
43818
43819 token = cp_parser_require_keyword (parser, keyword,
43820 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
43821 : RT_TRANSACTION_RELAXED));
43822 gcc_assert (token != NULL);
43823
43824 if (keyword == RID_TRANSACTION_RELAXED)
43825 this_in |= TM_STMT_ATTR_RELAXED;
43826
43827 /* Set this early. This might mean that we allow transaction_cancel in
43828 an expression that we find out later actually has to be a constexpr.
43829 However, we expect that cxx_constant_value will be able to deal with
43830 this; also, if the noexcept has no constexpr, then what we parse next
43831 really is a transaction's body. */
43832 parser->in_transaction = this_in;
43833
43834 /* Parse a noexcept specification. */
43835 noex = cp_parser_noexcept_specification_opt (parser,
43836 CP_PARSER_FLAGS_NONE,
43837 /*require_constexpr=*/false,
43838 &noex_expr,
43839 /*return_cond=*/true);
43840
43841 if (!noex || !noex_expr
43842 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
43843 {
43844 matching_parens parens;
43845 parens.require_open (parser);
43846
43847 expr = cp_parser_expression (parser);
43848 expr = finish_parenthesized_expr (expr);
43849
43850 parens.require_close (parser);
43851 }
43852 else
43853 {
43854 /* The only expression that is available got parsed for the noexcept
43855 already. noexcept is true then. */
43856 expr = noex;
43857 noex = boolean_true_node;
43858 }
43859
43860 expr = build_transaction_expr (token->location, expr, this_in, noex);
43861 parser->in_transaction = old_in;
43862
43863 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
43864 return error_mark_node;
43865
43866 return (flag_tm ? expr : error_mark_node);
43867 }
43868
43869 /* Parse a function-transaction-block.
43870
43871 function-transaction-block:
43872 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
43873 function-body
43874 __transaction_atomic txn-attribute[opt] function-try-block
43875 __transaction_relaxed ctor-initializer[opt] function-body
43876 __transaction_relaxed function-try-block
43877 */
43878
43879 static void
43880 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
43881 {
43882 unsigned char old_in = parser->in_transaction;
43883 unsigned char new_in = 1;
43884 tree compound_stmt, stmt, attrs;
43885 cp_token *token;
43886
43887 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
43888 || keyword == RID_TRANSACTION_RELAXED);
43889 token = cp_parser_require_keyword (parser, keyword,
43890 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
43891 : RT_TRANSACTION_RELAXED));
43892 gcc_assert (token != NULL);
43893
43894 if (keyword == RID_TRANSACTION_RELAXED)
43895 new_in |= TM_STMT_ATTR_RELAXED;
43896 else
43897 {
43898 attrs = cp_parser_txn_attribute_opt (parser);
43899 if (attrs)
43900 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
43901 }
43902
43903 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
43904
43905 parser->in_transaction = new_in;
43906
43907 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
43908 cp_parser_function_try_block (parser);
43909 else
43910 cp_parser_ctor_initializer_opt_and_function_body
43911 (parser, /*in_function_try_block=*/false);
43912
43913 parser->in_transaction = old_in;
43914
43915 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
43916 }
43917
43918 /* Parse a __transaction_cancel statement.
43919
43920 cancel-statement:
43921 __transaction_cancel txn-attribute[opt] ;
43922 __transaction_cancel txn-attribute[opt] throw-expression ;
43923
43924 ??? Cancel and throw is not yet implemented. */
43925
43926 static tree
43927 cp_parser_transaction_cancel (cp_parser *parser)
43928 {
43929 cp_token *token;
43930 bool is_outer = false;
43931 tree stmt, attrs;
43932
43933 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
43934 RT_TRANSACTION_CANCEL);
43935 gcc_assert (token != NULL);
43936
43937 attrs = cp_parser_txn_attribute_opt (parser);
43938 if (attrs)
43939 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
43940
43941 /* ??? Parse cancel-and-throw here. */
43942
43943 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
43944
43945 if (!flag_tm)
43946 {
43947 error_at (token->location, "%<__transaction_cancel%> without "
43948 "transactional memory support enabled");
43949 return error_mark_node;
43950 }
43951 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
43952 {
43953 error_at (token->location, "%<__transaction_cancel%> within a "
43954 "%<__transaction_relaxed%>");
43955 return error_mark_node;
43956 }
43957 else if (is_outer)
43958 {
43959 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
43960 && !is_tm_may_cancel_outer (current_function_decl))
43961 {
43962 error_at (token->location, "outer %<__transaction_cancel%> not "
43963 "within outer %<__transaction_atomic%>");
43964 error_at (token->location,
43965 " or a %<transaction_may_cancel_outer%> function");
43966 return error_mark_node;
43967 }
43968 }
43969 else if (parser->in_transaction == 0)
43970 {
43971 error_at (token->location, "%<__transaction_cancel%> not within "
43972 "%<__transaction_atomic%>");
43973 return error_mark_node;
43974 }
43975
43976 stmt = build_tm_abort_call (token->location, is_outer);
43977 add_stmt (stmt);
43978
43979 return stmt;
43980 }
43981 \f
43982 /* The parser. */
43983
43984 static GTY (()) cp_parser *the_parser;
43985
43986 \f
43987 /* Special handling for the first token or line in the file. The first
43988 thing in the file might be #pragma GCC pch_preprocess, which loads a
43989 PCH file, which is a GC collection point. So we need to handle this
43990 first pragma without benefit of an existing lexer structure.
43991
43992 Always returns one token to the caller in *FIRST_TOKEN. This is
43993 either the true first token of the file, or the first token after
43994 the initial pragma. */
43995
43996 static void
43997 cp_parser_initial_pragma (cp_token *first_token)
43998 {
43999 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
44000 return;
44001
44002 cp_lexer_get_preprocessor_token (0, first_token);
44003
44004 tree name = NULL;
44005 if (first_token->type == CPP_STRING)
44006 {
44007 name = first_token->u.value;
44008
44009 cp_lexer_get_preprocessor_token (0, first_token);
44010 }
44011
44012 /* Skip to the end of the pragma. */
44013 if (first_token->type != CPP_PRAGMA_EOL)
44014 {
44015 error_at (first_token->location,
44016 "malformed %<#pragma GCC pch_preprocess%>");
44017 do
44018 cp_lexer_get_preprocessor_token (0, first_token);
44019 while (first_token->type != CPP_PRAGMA_EOL);
44020 }
44021
44022 /* Now actually load the PCH file. */
44023 if (name)
44024 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
44025
44026 /* Read one more token to return to our caller. We have to do this
44027 after reading the PCH file in, since its pointers have to be
44028 live. */
44029 cp_lexer_get_preprocessor_token (0, first_token);
44030 }
44031
44032 /* Parse a pragma GCC ivdep. */
44033
44034 static bool
44035 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
44036 {
44037 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44038 return true;
44039 }
44040
44041 /* Parse a pragma GCC unroll. */
44042
44043 static unsigned short
44044 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
44045 {
44046 location_t location = cp_lexer_peek_token (parser->lexer)->location;
44047 tree expr = cp_parser_constant_expression (parser);
44048 unsigned short unroll;
44049 expr = maybe_constant_value (expr);
44050 HOST_WIDE_INT lunroll = 0;
44051 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
44052 || TREE_CODE (expr) != INTEGER_CST
44053 || (lunroll = tree_to_shwi (expr)) < 0
44054 || lunroll >= USHRT_MAX)
44055 {
44056 error_at (location, "%<#pragma GCC unroll%> requires an"
44057 " assignment-expression that evaluates to a non-negative"
44058 " integral constant less than %u", USHRT_MAX);
44059 unroll = 0;
44060 }
44061 else
44062 {
44063 unroll = (unsigned short)lunroll;
44064 if (unroll == 0)
44065 unroll = 1;
44066 }
44067 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44068 return unroll;
44069 }
44070
44071 /* Normal parsing of a pragma token. Here we can (and must) use the
44072 regular lexer. */
44073
44074 static bool
44075 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
44076 {
44077 cp_token *pragma_tok;
44078 unsigned int id;
44079 tree stmt;
44080 bool ret;
44081
44082 pragma_tok = cp_lexer_consume_token (parser->lexer);
44083 gcc_assert (pragma_tok->type == CPP_PRAGMA);
44084 parser->lexer->in_pragma = true;
44085
44086 id = cp_parser_pragma_kind (pragma_tok);
44087 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
44088 cp_ensure_no_omp_declare_simd (parser);
44089 switch (id)
44090 {
44091 case PRAGMA_GCC_PCH_PREPROCESS:
44092 error_at (pragma_tok->location,
44093 "%<#pragma GCC pch_preprocess%> must be first");
44094 break;
44095
44096 case PRAGMA_OMP_BARRIER:
44097 switch (context)
44098 {
44099 case pragma_compound:
44100 cp_parser_omp_barrier (parser, pragma_tok);
44101 return false;
44102 case pragma_stmt:
44103 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
44104 "used in compound statements", "omp barrier");
44105 break;
44106 default:
44107 goto bad_stmt;
44108 }
44109 break;
44110
44111 case PRAGMA_OMP_DEPOBJ:
44112 switch (context)
44113 {
44114 case pragma_compound:
44115 cp_parser_omp_depobj (parser, pragma_tok);
44116 return false;
44117 case pragma_stmt:
44118 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
44119 "used in compound statements", "omp depobj");
44120 break;
44121 default:
44122 goto bad_stmt;
44123 }
44124 break;
44125
44126 case PRAGMA_OMP_FLUSH:
44127 switch (context)
44128 {
44129 case pragma_compound:
44130 cp_parser_omp_flush (parser, pragma_tok);
44131 return false;
44132 case pragma_stmt:
44133 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
44134 "used in compound statements", "omp flush");
44135 break;
44136 default:
44137 goto bad_stmt;
44138 }
44139 break;
44140
44141 case PRAGMA_OMP_TASKWAIT:
44142 switch (context)
44143 {
44144 case pragma_compound:
44145 cp_parser_omp_taskwait (parser, pragma_tok);
44146 return false;
44147 case pragma_stmt:
44148 error_at (pragma_tok->location,
44149 "%<#pragma %s%> may only be used in compound statements",
44150 "omp taskwait");
44151 break;
44152 default:
44153 goto bad_stmt;
44154 }
44155 break;
44156
44157 case PRAGMA_OMP_TASKYIELD:
44158 switch (context)
44159 {
44160 case pragma_compound:
44161 cp_parser_omp_taskyield (parser, pragma_tok);
44162 return false;
44163 case pragma_stmt:
44164 error_at (pragma_tok->location,
44165 "%<#pragma %s%> may only be used in compound statements",
44166 "omp taskyield");
44167 break;
44168 default:
44169 goto bad_stmt;
44170 }
44171 break;
44172
44173 case PRAGMA_OMP_CANCEL:
44174 switch (context)
44175 {
44176 case pragma_compound:
44177 cp_parser_omp_cancel (parser, pragma_tok);
44178 return false;
44179 case pragma_stmt:
44180 error_at (pragma_tok->location,
44181 "%<#pragma %s%> may only be used in compound statements",
44182 "omp cancel");
44183 break;
44184 default:
44185 goto bad_stmt;
44186 }
44187 break;
44188
44189 case PRAGMA_OMP_CANCELLATION_POINT:
44190 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
44191 return false;
44192
44193 case PRAGMA_OMP_THREADPRIVATE:
44194 cp_parser_omp_threadprivate (parser, pragma_tok);
44195 return false;
44196
44197 case PRAGMA_OMP_DECLARE:
44198 return cp_parser_omp_declare (parser, pragma_tok, context);
44199
44200 case PRAGMA_OACC_DECLARE:
44201 cp_parser_oacc_declare (parser, pragma_tok);
44202 return false;
44203
44204 case PRAGMA_OACC_ENTER_DATA:
44205 if (context == pragma_stmt)
44206 {
44207 error_at (pragma_tok->location,
44208 "%<#pragma %s%> may only be used in compound statements",
44209 "acc enter data");
44210 break;
44211 }
44212 else if (context != pragma_compound)
44213 goto bad_stmt;
44214 cp_parser_omp_construct (parser, pragma_tok, if_p);
44215 return true;
44216
44217 case PRAGMA_OACC_EXIT_DATA:
44218 if (context == pragma_stmt)
44219 {
44220 error_at (pragma_tok->location,
44221 "%<#pragma %s%> may only be used in compound statements",
44222 "acc exit data");
44223 break;
44224 }
44225 else if (context != pragma_compound)
44226 goto bad_stmt;
44227 cp_parser_omp_construct (parser, pragma_tok, if_p);
44228 return true;
44229
44230 case PRAGMA_OACC_ROUTINE:
44231 if (context != pragma_external)
44232 {
44233 error_at (pragma_tok->location,
44234 "%<#pragma acc routine%> must be at file scope");
44235 break;
44236 }
44237 cp_parser_oacc_routine (parser, pragma_tok, context);
44238 return false;
44239
44240 case PRAGMA_OACC_UPDATE:
44241 if (context == pragma_stmt)
44242 {
44243 error_at (pragma_tok->location,
44244 "%<#pragma %s%> may only be used in compound statements",
44245 "acc update");
44246 break;
44247 }
44248 else if (context != pragma_compound)
44249 goto bad_stmt;
44250 cp_parser_omp_construct (parser, pragma_tok, if_p);
44251 return true;
44252
44253 case PRAGMA_OACC_WAIT:
44254 if (context == pragma_stmt)
44255 {
44256 error_at (pragma_tok->location,
44257 "%<#pragma %s%> may only be used in compound statements",
44258 "acc wait");
44259 break;
44260 }
44261 else if (context != pragma_compound)
44262 goto bad_stmt;
44263 cp_parser_omp_construct (parser, pragma_tok, if_p);
44264 return true;
44265
44266 case PRAGMA_OACC_ATOMIC:
44267 case PRAGMA_OACC_CACHE:
44268 case PRAGMA_OACC_DATA:
44269 case PRAGMA_OACC_HOST_DATA:
44270 case PRAGMA_OACC_KERNELS:
44271 case PRAGMA_OACC_LOOP:
44272 case PRAGMA_OACC_PARALLEL:
44273 case PRAGMA_OACC_SERIAL:
44274 case PRAGMA_OMP_ATOMIC:
44275 case PRAGMA_OMP_CRITICAL:
44276 case PRAGMA_OMP_DISTRIBUTE:
44277 case PRAGMA_OMP_FOR:
44278 case PRAGMA_OMP_LOOP:
44279 case PRAGMA_OMP_MASTER:
44280 case PRAGMA_OMP_PARALLEL:
44281 case PRAGMA_OMP_SECTIONS:
44282 case PRAGMA_OMP_SIMD:
44283 case PRAGMA_OMP_SINGLE:
44284 case PRAGMA_OMP_TASK:
44285 case PRAGMA_OMP_TASKGROUP:
44286 case PRAGMA_OMP_TASKLOOP:
44287 case PRAGMA_OMP_TEAMS:
44288 if (context != pragma_stmt && context != pragma_compound)
44289 goto bad_stmt;
44290 stmt = push_omp_privatization_clauses (false);
44291 cp_parser_omp_construct (parser, pragma_tok, if_p);
44292 pop_omp_privatization_clauses (stmt);
44293 return true;
44294
44295 case PRAGMA_OMP_REQUIRES:
44296 if (context != pragma_external)
44297 {
44298 error_at (pragma_tok->location,
44299 "%<#pragma omp requires%> may only be used at file or "
44300 "namespace scope");
44301 break;
44302 }
44303 return cp_parser_omp_requires (parser, pragma_tok);
44304
44305 case PRAGMA_OMP_ORDERED:
44306 if (context != pragma_stmt && context != pragma_compound)
44307 goto bad_stmt;
44308 stmt = push_omp_privatization_clauses (false);
44309 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
44310 pop_omp_privatization_clauses (stmt);
44311 return ret;
44312
44313 case PRAGMA_OMP_TARGET:
44314 if (context != pragma_stmt && context != pragma_compound)
44315 goto bad_stmt;
44316 stmt = push_omp_privatization_clauses (false);
44317 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
44318 pop_omp_privatization_clauses (stmt);
44319 return ret;
44320
44321 case PRAGMA_OMP_END_DECLARE_TARGET:
44322 cp_parser_omp_end_declare_target (parser, pragma_tok);
44323 return false;
44324
44325 case PRAGMA_OMP_SCAN:
44326 error_at (pragma_tok->location,
44327 "%<#pragma omp scan%> may only be used in "
44328 "a loop construct with %<inscan%> %<reduction%> clause");
44329 break;
44330
44331 case PRAGMA_OMP_SECTION:
44332 error_at (pragma_tok->location,
44333 "%<#pragma omp section%> may only be used in "
44334 "%<#pragma omp sections%> construct");
44335 break;
44336
44337 case PRAGMA_IVDEP:
44338 {
44339 if (context == pragma_external)
44340 {
44341 error_at (pragma_tok->location,
44342 "%<#pragma GCC ivdep%> must be inside a function");
44343 break;
44344 }
44345 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
44346 unsigned short unroll;
44347 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
44348 if (tok->type == CPP_PRAGMA
44349 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
44350 {
44351 tok = cp_lexer_consume_token (parser->lexer);
44352 unroll = cp_parser_pragma_unroll (parser, tok);
44353 tok = cp_lexer_peek_token (the_parser->lexer);
44354 }
44355 else
44356 unroll = 0;
44357 if (tok->type != CPP_KEYWORD
44358 || (tok->keyword != RID_FOR
44359 && tok->keyword != RID_WHILE
44360 && tok->keyword != RID_DO))
44361 {
44362 cp_parser_error (parser, "for, while or do statement expected");
44363 return false;
44364 }
44365 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
44366 return true;
44367 }
44368
44369 case PRAGMA_UNROLL:
44370 {
44371 if (context == pragma_external)
44372 {
44373 error_at (pragma_tok->location,
44374 "%<#pragma GCC unroll%> must be inside a function");
44375 break;
44376 }
44377 const unsigned short unroll
44378 = cp_parser_pragma_unroll (parser, pragma_tok);
44379 bool ivdep;
44380 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
44381 if (tok->type == CPP_PRAGMA
44382 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
44383 {
44384 tok = cp_lexer_consume_token (parser->lexer);
44385 ivdep = cp_parser_pragma_ivdep (parser, tok);
44386 tok = cp_lexer_peek_token (the_parser->lexer);
44387 }
44388 else
44389 ivdep = false;
44390 if (tok->type != CPP_KEYWORD
44391 || (tok->keyword != RID_FOR
44392 && tok->keyword != RID_WHILE
44393 && tok->keyword != RID_DO))
44394 {
44395 cp_parser_error (parser, "for, while or do statement expected");
44396 return false;
44397 }
44398 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
44399 return true;
44400 }
44401
44402 default:
44403 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
44404 c_invoke_pragma_handler (id);
44405 break;
44406
44407 bad_stmt:
44408 cp_parser_error (parser, "expected declaration specifiers");
44409 break;
44410 }
44411
44412 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44413 return false;
44414 }
44415
44416 /* The interface the pragma parsers have to the lexer. */
44417
44418 enum cpp_ttype
44419 pragma_lex (tree *value, location_t *loc)
44420 {
44421 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
44422 enum cpp_ttype ret = tok->type;
44423
44424 *value = tok->u.value;
44425 if (loc)
44426 *loc = tok->location;
44427
44428 if (ret == CPP_PRAGMA_EOL)
44429 ret = CPP_EOF;
44430 else if (ret == CPP_STRING)
44431 *value = cp_parser_string_literal (the_parser, false, false);
44432 else
44433 {
44434 if (ret == CPP_KEYWORD)
44435 ret = CPP_NAME;
44436 cp_lexer_consume_token (the_parser->lexer);
44437 }
44438
44439 return ret;
44440 }
44441
44442 \f
44443 /* External interface. */
44444
44445 /* Parse one entire translation unit. */
44446
44447 void
44448 c_parse_file (void)
44449 {
44450 static bool already_called = false;
44451
44452 if (already_called)
44453 fatal_error (input_location,
44454 "multi-source compilation not implemented for C++");
44455 already_called = true;
44456
44457 /* cp_lexer_new_main is called before doing any GC allocation
44458 because tokenization might load a PCH file. */
44459 cp_lexer *lexer = cp_lexer_new_main ();
44460
44461 the_parser = cp_parser_new (lexer);
44462
44463 cp_parser_translation_unit (the_parser);
44464 class_decl_loc_t::diag_mismatched_tags ();
44465
44466 the_parser = NULL;
44467
44468 finish_translation_unit ();
44469 }
44470
44471 /* Create an identifier for a generic parameter type (a synthesized
44472 template parameter implied by `auto' or a concept identifier). */
44473
44474 static GTY(()) int generic_parm_count;
44475 static tree
44476 make_generic_type_name ()
44477 {
44478 char buf[32];
44479 sprintf (buf, "auto:%d", ++generic_parm_count);
44480 return get_identifier (buf);
44481 }
44482
44483 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
44484 (creating a new template parameter list if necessary). Returns the newly
44485 created template type parm. */
44486
44487 static tree
44488 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
44489 {
44490 /* A requires-clause is not a function and cannot have placeholders. */
44491 if (current_binding_level->kind == sk_block)
44492 {
44493 error ("placeholder type not allowed in this context");
44494 return error_mark_node;
44495 }
44496
44497 gcc_assert (current_binding_level->kind == sk_function_parms);
44498
44499 /* We are either continuing a function template that already contains implicit
44500 template parameters, creating a new fully-implicit function template, or
44501 extending an existing explicit function template with implicit template
44502 parameters. */
44503
44504 cp_binding_level *const entry_scope = current_binding_level;
44505
44506 bool become_template = false;
44507 cp_binding_level *parent_scope = 0;
44508
44509 if (parser->implicit_template_scope)
44510 {
44511 gcc_assert (parser->implicit_template_parms);
44512
44513 current_binding_level = parser->implicit_template_scope;
44514 }
44515 else
44516 {
44517 /* Roll back to the existing template parameter scope (in the case of
44518 extending an explicit function template) or introduce a new template
44519 parameter scope ahead of the function parameter scope (or class scope
44520 in the case of out-of-line member definitions). The function scope is
44521 added back after template parameter synthesis below. */
44522
44523 cp_binding_level *scope = entry_scope;
44524
44525 while (scope->kind == sk_function_parms)
44526 {
44527 parent_scope = scope;
44528 scope = scope->level_chain;
44529 }
44530 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
44531 {
44532 /* If not defining a class, then any class scope is a scope level in
44533 an out-of-line member definition. In this case simply wind back
44534 beyond the first such scope to inject the template parameter list.
44535 Otherwise wind back to the class being defined. The latter can
44536 occur in class member friend declarations such as:
44537
44538 class A {
44539 void foo (auto);
44540 };
44541 class B {
44542 friend void A::foo (auto);
44543 };
44544
44545 The template parameter list synthesized for the friend declaration
44546 must be injected in the scope of 'B'. This can also occur in
44547 erroneous cases such as:
44548
44549 struct A {
44550 struct B {
44551 void foo (auto);
44552 };
44553 void B::foo (auto) {}
44554 };
44555
44556 Here the attempted definition of 'B::foo' within 'A' is ill-formed
44557 but, nevertheless, the template parameter list synthesized for the
44558 declarator should be injected into the scope of 'A' as if the
44559 ill-formed template was specified explicitly. */
44560
44561 while (scope->kind == sk_class && !scope->defining_class_p)
44562 {
44563 parent_scope = scope;
44564 scope = scope->level_chain;
44565 }
44566 }
44567
44568 current_binding_level = scope;
44569
44570 if (scope->kind != sk_template_parms
44571 || !function_being_declared_is_template_p (parser))
44572 {
44573 /* Introduce a new template parameter list for implicit template
44574 parameters. */
44575
44576 become_template = true;
44577
44578 parser->implicit_template_scope
44579 = begin_scope (sk_template_parms, NULL);
44580
44581 ++processing_template_decl;
44582
44583 parser->fully_implicit_function_template_p = true;
44584 ++parser->num_template_parameter_lists;
44585 }
44586 else
44587 {
44588 /* Synthesize implicit template parameters at the end of the explicit
44589 template parameter list. */
44590
44591 gcc_assert (current_template_parms);
44592
44593 parser->implicit_template_scope = scope;
44594
44595 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
44596 parser->implicit_template_parms
44597 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
44598 }
44599 }
44600
44601 /* Synthesize a new template parameter and track the current template
44602 parameter chain with implicit_template_parms. */
44603
44604 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
44605 tree synth_id = make_generic_type_name ();
44606 tree synth_tmpl_parm;
44607 bool non_type = false;
44608
44609 /* Synthesize the type template parameter. */
44610 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
44611 synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
44612
44613 /* Attach the constraint to the parm before processing. */
44614 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
44615 TREE_TYPE (node) = constr;
44616 tree new_parm
44617 = process_template_parm (parser->implicit_template_parms,
44618 input_location,
44619 node,
44620 /*non_type=*/non_type,
44621 /*param_pack=*/false);
44622
44623 /* Mark the synthetic declaration "virtual". This is used when
44624 comparing template-heads to determine if whether an abbreviated
44625 function template is equivalent to an explicit template.
44626
44627 Note that DECL_ARTIFICIAL is used elsewhere for template parameters. */
44628 DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
44629
44630 // Chain the new parameter to the list of implicit parameters.
44631 if (parser->implicit_template_parms)
44632 parser->implicit_template_parms
44633 = TREE_CHAIN (parser->implicit_template_parms);
44634 else
44635 parser->implicit_template_parms = new_parm;
44636
44637 tree new_decl = get_local_decls ();
44638 if (non_type)
44639 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
44640 new_decl = DECL_INITIAL (new_decl);
44641
44642 /* If creating a fully implicit function template, start the new implicit
44643 template parameter list with this synthesized type, otherwise grow the
44644 current template parameter list. */
44645
44646 if (become_template)
44647 {
44648 parent_scope->level_chain = current_binding_level;
44649
44650 tree new_parms = make_tree_vec (1);
44651 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
44652 current_template_parms = tree_cons (size_int (processing_template_decl),
44653 new_parms, current_template_parms);
44654 }
44655 else
44656 {
44657 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
44658 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
44659 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
44660 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
44661 }
44662
44663 /* If the new parameter was constrained, we need to add that to the
44664 constraints in the template parameter list. */
44665 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
44666 {
44667 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
44668 reqs = combine_constraint_expressions (reqs, req);
44669 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
44670 }
44671
44672 current_binding_level = entry_scope;
44673
44674 return new_decl;
44675 }
44676
44677 /* Finish the declaration of a fully implicit function template. Such a
44678 template has no explicit template parameter list so has not been through the
44679 normal template head and tail processing. synthesize_implicit_template_parm
44680 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
44681 provided if the declaration is a class member such that its template
44682 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
44683 form is returned. Otherwise NULL_TREE is returned. */
44684
44685 static tree
44686 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
44687 {
44688 gcc_assert (parser->fully_implicit_function_template_p);
44689
44690 if (member_decl_opt && member_decl_opt != error_mark_node
44691 && DECL_VIRTUAL_P (member_decl_opt))
44692 {
44693 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
44694 "implicit templates may not be %<virtual%>");
44695 DECL_VIRTUAL_P (member_decl_opt) = false;
44696 }
44697
44698 if (member_decl_opt)
44699 member_decl_opt = finish_member_template_decl (member_decl_opt);
44700 end_template_decl ();
44701
44702 parser->fully_implicit_function_template_p = false;
44703 parser->implicit_template_parms = 0;
44704 parser->implicit_template_scope = 0;
44705 --parser->num_template_parameter_lists;
44706
44707 return member_decl_opt;
44708 }
44709
44710 /* Like finish_fully_implicit_template, but to be used in error
44711 recovery, rearranging scopes so that we restore the state we had
44712 before synthesize_implicit_template_parm inserted the implement
44713 template parms scope. */
44714
44715 static void
44716 abort_fully_implicit_template (cp_parser *parser)
44717 {
44718 cp_binding_level *return_to_scope = current_binding_level;
44719
44720 if (parser->implicit_template_scope
44721 && return_to_scope != parser->implicit_template_scope)
44722 {
44723 cp_binding_level *child = return_to_scope;
44724 for (cp_binding_level *scope = child->level_chain;
44725 scope != parser->implicit_template_scope;
44726 scope = child->level_chain)
44727 child = scope;
44728 child->level_chain = parser->implicit_template_scope->level_chain;
44729 parser->implicit_template_scope->level_chain = return_to_scope;
44730 current_binding_level = parser->implicit_template_scope;
44731 }
44732 else
44733 return_to_scope = return_to_scope->level_chain;
44734
44735 finish_fully_implicit_template (parser, NULL);
44736
44737 gcc_assert (current_binding_level == return_to_scope);
44738 }
44739
44740 /* Helper function for diagnostics that have complained about things
44741 being used with 'extern "C"' linkage.
44742
44743 Attempt to issue a note showing where the 'extern "C"' linkage began. */
44744
44745 void
44746 maybe_show_extern_c_location (void)
44747 {
44748 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
44749 inform (the_parser->innermost_linkage_specification_location,
44750 "%<extern \"C\"%> linkage started here");
44751 }
44752
44753 #include "gt-cp-parser.h"
This page took 2.111504 seconds and 5 git commands to generate.