5651cfacd3c727cc68852bd122a0ac3643341e1e
[gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2021 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 uintptr_t filter = 0;
650 if (modules_p ())
651 filter = module_token_cdtor (parse_in, filter);
652
653 /* Get the remaining tokens from the preprocessor. */
654 while (tok->type != CPP_EOF)
655 {
656 if (filter)
657 /* Process the previous token. */
658 module_token_lang (tok->type, tok->keyword, tok->u.value,
659 tok->location, filter);
660 tok = vec_safe_push (lexer->buffer, cp_token ());
661 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
662 }
663
664 lexer->next_token = lexer->buffer->address ();
665 lexer->last_token = lexer->next_token
666 + lexer->buffer->length ()
667 - 1;
668
669 if (lexer->buffer->length () != 1)
670 {
671 /* Set the EOF token's location to be the just after the previous
672 token's range. That way 'at-eof' diagnostics point at something
673 meaninful. */
674 auto range = get_range_from_loc (line_table, tok[-1].location);
675 tok[0].location
676 = linemap_position_for_loc_and_offset (line_table, range.m_finish, 1);
677 }
678
679 if (filter)
680 module_token_cdtor (parse_in, filter);
681
682 /* Subsequent preprocessor diagnostics should use compiler
683 diagnostic functions to get the compiler source location. */
684 done_lexing = true;
685
686 maybe_check_all_macros (parse_in);
687
688 gcc_assert (!lexer->next_token->purged_p);
689 return lexer;
690 }
691
692 /* Create a new lexer whose token stream is primed with the tokens in
693 CACHE. When these tokens are exhausted, no new tokens will be read. */
694
695 static cp_lexer *
696 cp_lexer_new_from_tokens (cp_token_cache *cache)
697 {
698 cp_token *first = cache->first;
699 cp_token *last = cache->last;
700 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
701
702 /* We do not own the buffer. */
703 lexer->buffer = NULL;
704
705 /* Insert an EOF token. */
706 lexer->saved_type = last->type;
707 lexer->saved_keyword = last->keyword;
708 last->type = CPP_EOF;
709 last->keyword = RID_MAX;
710
711 lexer->next_token = first;
712 lexer->last_token = last;
713
714 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
715
716 /* Initially we are not debugging. */
717 lexer->debugging_p = false;
718
719 gcc_assert (!lexer->next_token->purged_p
720 && !lexer->last_token->purged_p);
721 return lexer;
722 }
723
724 /* Frees all resources associated with LEXER. */
725
726 static void
727 cp_lexer_destroy (cp_lexer *lexer)
728 {
729 if (lexer->buffer)
730 vec_free (lexer->buffer);
731 else
732 {
733 /* Restore the token we overwrite with EOF. */
734 lexer->last_token->type = lexer->saved_type;
735 lexer->last_token->keyword = lexer->saved_keyword;
736 }
737 lexer->saved_tokens.release ();
738 ggc_free (lexer);
739 }
740
741 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
742 be used. The point of this flag is to help the compiler to fold away calls
743 to cp_lexer_debugging_p within this source file at compile time, when the
744 lexer is not being debugged. */
745
746 #define LEXER_DEBUGGING_ENABLED_P false
747
748 /* Returns nonzero if debugging information should be output. */
749
750 static inline bool
751 cp_lexer_debugging_p (cp_lexer *lexer)
752 {
753 if (!LEXER_DEBUGGING_ENABLED_P)
754 return false;
755
756 return lexer->debugging_p;
757 }
758
759
760 static inline cp_token_position
761 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
762 {
763 return lexer->next_token - previous_p;
764 }
765
766 static inline cp_token *
767 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
768 {
769 return pos;
770 }
771
772 static inline void
773 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
774 {
775 lexer->next_token = cp_lexer_token_at (lexer, pos);
776 }
777
778 static inline cp_token_position
779 cp_lexer_previous_token_position (cp_lexer *lexer)
780 {
781 return cp_lexer_token_position (lexer, true);
782 }
783
784 static inline cp_token *
785 cp_lexer_previous_token (cp_lexer *lexer)
786 {
787 cp_token_position tp = cp_lexer_previous_token_position (lexer);
788
789 /* Skip past purged tokens. */
790 while (tp->purged_p)
791 {
792 gcc_assert (tp != vec_safe_address (lexer->buffer));
793 tp--;
794 }
795
796 return cp_lexer_token_at (lexer, tp);
797 }
798
799 /* Same as above, but return NULL when the lexer doesn't own the token
800 buffer or if the next_token is at the start of the token
801 vector or if all previous tokens are purged. */
802
803 static cp_token *
804 cp_lexer_safe_previous_token (cp_lexer *lexer)
805 {
806 if (lexer->buffer
807 && lexer->next_token != lexer->buffer->address ())
808 {
809 cp_token_position tp = cp_lexer_previous_token_position (lexer);
810
811 /* Skip past purged tokens. */
812 while (tp->purged_p)
813 {
814 if (tp == lexer->buffer->address ())
815 return NULL;
816 tp--;
817 }
818 return cp_lexer_token_at (lexer, tp);
819 }
820
821 return NULL;
822 }
823
824 /* Overload for make_location, taking the lexer to mean the location of the
825 previous token. */
826
827 static inline location_t
828 make_location (location_t caret, location_t start, cp_lexer *lexer)
829 {
830 cp_token *t = cp_lexer_previous_token (lexer);
831 return make_location (caret, start, t->location);
832 }
833
834 /* Overload for make_location taking tokens instead of locations. */
835
836 static inline location_t
837 make_location (cp_token *caret, cp_token *start, cp_token *end)
838 {
839 return make_location (caret->location, start->location, end->location);
840 }
841
842 /* nonzero if we are presently saving tokens. */
843
844 static inline int
845 cp_lexer_saving_tokens (const cp_lexer* lexer)
846 {
847 return lexer->saved_tokens.length () != 0;
848 }
849
850 /* Store the next token from the preprocessor in *TOKEN. Return true
851 if we reach EOF. If LEXER is NULL, assume we are handling an
852 initial #pragma pch_preprocess, and thus want the lexer to return
853 processed strings. */
854
855 static void
856 cp_lexer_get_preprocessor_token (unsigned flags, cp_token *token)
857 {
858 static int is_extern_c = 0;
859
860 /* Get a new token from the preprocessor. */
861 token->type
862 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
863 flags);
864 token->keyword = RID_MAX;
865 token->purged_p = false;
866 token->error_reported = false;
867 token->tree_check_p = false;
868 /* Usually never see a zero, but just in case ... */
869 token->main_source_p = line_table->depth <= 1;
870
871 /* On some systems, some header files are surrounded by an
872 implicit extern "C" block. Set a flag in the token if it
873 comes from such a header. */
874 is_extern_c += pending_lang_change;
875 pending_lang_change = 0;
876 token->implicit_extern_c = is_extern_c > 0;
877
878 /* Check to see if this token is a keyword. */
879 if (token->type == CPP_NAME)
880 {
881 if (IDENTIFIER_KEYWORD_P (token->u.value))
882 {
883 /* Mark this token as a keyword. */
884 token->type = CPP_KEYWORD;
885 /* Record which keyword. */
886 token->keyword = C_RID_CODE (token->u.value);
887 }
888 else
889 {
890 if (warn_cxx11_compat
891 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
892 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
893 {
894 /* Warn about the C++0x keyword (but still treat it as
895 an identifier). */
896 warning_at (token->location, OPT_Wc__11_compat,
897 "identifier %qE is a keyword in C++11",
898 token->u.value);
899
900 /* Clear out the C_RID_CODE so we don't warn about this
901 particular identifier-turned-keyword again. */
902 C_SET_RID_CODE (token->u.value, RID_MAX);
903 }
904 if (warn_cxx20_compat
905 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX20
906 && C_RID_CODE (token->u.value) <= RID_LAST_CXX20)
907 {
908 /* Warn about the C++20 keyword (but still treat it as
909 an identifier). */
910 warning_at (token->location, OPT_Wc__20_compat,
911 "identifier %qE is a keyword in C++20",
912 token->u.value);
913
914 /* Clear out the C_RID_CODE so we don't warn about this
915 particular identifier-turned-keyword again. */
916 C_SET_RID_CODE (token->u.value, RID_MAX);
917 }
918
919 token->keyword = RID_MAX;
920 }
921 }
922 else if (token->type == CPP_AT_NAME)
923 {
924 /* This only happens in Objective-C++; it must be a keyword. */
925 token->type = CPP_KEYWORD;
926 switch (C_RID_CODE (token->u.value))
927 {
928 /* Replace 'class' with '@class', 'private' with '@private',
929 etc. This prevents confusion with the C++ keyword
930 'class', and makes the tokens consistent with other
931 Objective-C 'AT' keywords. For example '@class' is
932 reported as RID_AT_CLASS which is consistent with
933 '@synchronized', which is reported as
934 RID_AT_SYNCHRONIZED.
935 */
936 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
937 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
938 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
939 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
940 case RID_THROW: token->keyword = RID_AT_THROW; break;
941 case RID_TRY: token->keyword = RID_AT_TRY; break;
942 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
943 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
944 default: token->keyword = C_RID_CODE (token->u.value);
945 }
946 }
947 }
948
949 /* Update the globals input_location and the input file stack from TOKEN. */
950 static inline void
951 cp_lexer_set_source_position_from_token (cp_token *token)
952 {
953 input_location = token->location;
954 }
955
956 /* Update the globals input_location and the input file stack from LEXER. */
957 static inline void
958 cp_lexer_set_source_position (cp_lexer *lexer)
959 {
960 cp_token *token = cp_lexer_peek_token (lexer);
961 cp_lexer_set_source_position_from_token (token);
962 }
963
964 /* Return a pointer to the next token in the token stream, but do not
965 consume it. */
966
967 static inline cp_token *
968 cp_lexer_peek_token (cp_lexer *lexer)
969 {
970 if (cp_lexer_debugging_p (lexer))
971 {
972 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
973 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
974 putc ('\n', cp_lexer_debug_stream);
975 }
976 return lexer->next_token;
977 }
978
979 /* Return true if the next token has the indicated TYPE. */
980
981 static inline bool
982 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
983 {
984 return cp_lexer_peek_token (lexer)->type == type;
985 }
986
987 /* Return true if the next token does not have the indicated TYPE. */
988
989 static inline bool
990 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
991 {
992 return !cp_lexer_next_token_is (lexer, type);
993 }
994
995 /* Return true if the next token is the indicated KEYWORD. */
996
997 static inline bool
998 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
999 {
1000 return cp_lexer_peek_token (lexer)->keyword == keyword;
1001 }
1002
1003 static inline bool
1004 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
1005 {
1006 return cp_lexer_peek_nth_token (lexer, n)->type == type;
1007 }
1008
1009 static inline bool
1010 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
1011 {
1012 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
1013 }
1014
1015 /* Return true if KEYWORD can start a decl-specifier. */
1016
1017 bool
1018 cp_keyword_starts_decl_specifier_p (enum rid keyword)
1019 {
1020 switch (keyword)
1021 {
1022 /* auto specifier: storage-class-specifier in C++,
1023 simple-type-specifier in C++0x. */
1024 case RID_AUTO:
1025 /* Storage classes. */
1026 case RID_REGISTER:
1027 case RID_STATIC:
1028 case RID_EXTERN:
1029 case RID_MUTABLE:
1030 case RID_THREAD:
1031 /* Elaborated type specifiers. */
1032 case RID_ENUM:
1033 case RID_CLASS:
1034 case RID_STRUCT:
1035 case RID_UNION:
1036 case RID_TYPENAME:
1037 /* Simple type specifiers. */
1038 case RID_CHAR:
1039 case RID_CHAR8:
1040 case RID_CHAR16:
1041 case RID_CHAR32:
1042 case RID_WCHAR:
1043 case RID_BOOL:
1044 case RID_SHORT:
1045 case RID_INT:
1046 case RID_LONG:
1047 case RID_SIGNED:
1048 case RID_UNSIGNED:
1049 case RID_FLOAT:
1050 case RID_DOUBLE:
1051 case RID_VOID:
1052 /* GNU extensions. */
1053 case RID_ATTRIBUTE:
1054 case RID_TYPEOF:
1055 /* C++11 extensions. */
1056 case RID_DECLTYPE:
1057 case RID_UNDERLYING_TYPE:
1058 case RID_CONSTEXPR:
1059 /* C++20 extensions. */
1060 case RID_CONSTINIT:
1061 case RID_CONSTEVAL:
1062 return true;
1063
1064 default:
1065 if (keyword >= RID_FIRST_INT_N
1066 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
1067 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
1068 return true;
1069 return false;
1070 }
1071 }
1072
1073 /* Return true if the next token is a keyword for a decl-specifier. */
1074
1075 static bool
1076 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1077 {
1078 cp_token *token;
1079
1080 token = cp_lexer_peek_token (lexer);
1081 return cp_keyword_starts_decl_specifier_p (token->keyword);
1082 }
1083
1084 /* Returns TRUE iff the token T begins a decltype type. */
1085
1086 static bool
1087 token_is_decltype (cp_token *t)
1088 {
1089 return (t->keyword == RID_DECLTYPE
1090 || t->type == CPP_DECLTYPE);
1091 }
1092
1093 /* Returns TRUE iff the next token begins a decltype type. */
1094
1095 static bool
1096 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1097 {
1098 cp_token *t = cp_lexer_peek_token (lexer);
1099 return token_is_decltype (t);
1100 }
1101
1102 /* Called when processing a token with tree_check_value; perform or defer the
1103 associated checks and return the value. */
1104
1105 static tree
1106 saved_checks_value (struct tree_check *check_value)
1107 {
1108 /* Perform any access checks that were deferred. */
1109 vec<deferred_access_check, va_gc> *checks;
1110 deferred_access_check *chk;
1111 checks = check_value->checks;
1112 if (checks)
1113 {
1114 int i;
1115 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1116 perform_or_defer_access_check (chk->binfo,
1117 chk->decl,
1118 chk->diag_decl, tf_warning_or_error);
1119 }
1120 /* Return the stored value. */
1121 return check_value->value;
1122 }
1123
1124 /* Return a pointer to the Nth token in the token stream. If N is 1,
1125 then this is precisely equivalent to cp_lexer_peek_token (except
1126 that it is not inline). One would like to disallow that case, but
1127 there is one case (cp_parser_nth_token_starts_template_id) where
1128 the caller passes a variable for N and it might be 1. */
1129
1130 static cp_token *
1131 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1132 {
1133 cp_token *token;
1134
1135 /* N is 1-based, not zero-based. */
1136 gcc_assert (n > 0);
1137
1138 if (cp_lexer_debugging_p (lexer))
1139 fprintf (cp_lexer_debug_stream,
1140 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1141
1142 --n;
1143 token = lexer->next_token;
1144 while (n && token->type != CPP_EOF)
1145 {
1146 ++token;
1147 if (!token->purged_p)
1148 --n;
1149 }
1150
1151 if (cp_lexer_debugging_p (lexer))
1152 {
1153 cp_lexer_print_token (cp_lexer_debug_stream, token);
1154 putc ('\n', cp_lexer_debug_stream);
1155 }
1156
1157 return token;
1158 }
1159
1160 /* Return the next token, and advance the lexer's next_token pointer
1161 to point to the next non-purged token. */
1162
1163 static cp_token *
1164 cp_lexer_consume_token (cp_lexer* lexer)
1165 {
1166 cp_token *token = lexer->next_token;
1167
1168 do
1169 {
1170 gcc_assert (token->type != CPP_EOF);
1171 lexer->next_token++;
1172 }
1173 while (lexer->next_token->purged_p);
1174
1175 cp_lexer_set_source_position_from_token (token);
1176
1177 /* Provide debugging output. */
1178 if (cp_lexer_debugging_p (lexer))
1179 {
1180 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1181 cp_lexer_print_token (cp_lexer_debug_stream, token);
1182 putc ('\n', cp_lexer_debug_stream);
1183 }
1184
1185 return token;
1186 }
1187
1188 /* Permanently remove the next token from the token stream, and
1189 advance the next_token pointer to refer to the next non-purged
1190 token. */
1191
1192 static void
1193 cp_lexer_purge_token (cp_lexer *lexer)
1194 {
1195 cp_token *tok = lexer->next_token;
1196
1197 gcc_assert (tok->type != CPP_EOF);
1198 tok->purged_p = true;
1199 tok->location = UNKNOWN_LOCATION;
1200 tok->u.value = NULL_TREE;
1201 tok->keyword = RID_MAX;
1202
1203 do
1204 tok++;
1205 while (tok->purged_p);
1206 lexer->next_token = tok;
1207 }
1208
1209 /* Permanently remove all tokens after TOK, up to, but not
1210 including, the token that will be returned next by
1211 cp_lexer_peek_token. */
1212
1213 static void
1214 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1215 {
1216 cp_token *peek = lexer->next_token;
1217
1218 gcc_assert (tok < peek);
1219
1220 for (tok++; tok != peek; tok++)
1221 {
1222 tok->purged_p = true;
1223 tok->location = UNKNOWN_LOCATION;
1224 tok->u.value = NULL_TREE;
1225 tok->keyword = RID_MAX;
1226 }
1227 }
1228
1229 /* Begin saving tokens. All tokens consumed after this point will be
1230 preserved. */
1231
1232 static void
1233 cp_lexer_save_tokens (cp_lexer* lexer)
1234 {
1235 /* Provide debugging output. */
1236 if (cp_lexer_debugging_p (lexer))
1237 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1238
1239 lexer->saved_tokens.safe_push (lexer->next_token);
1240 }
1241
1242 /* Commit to the portion of the token stream most recently saved. */
1243
1244 static void
1245 cp_lexer_commit_tokens (cp_lexer* lexer)
1246 {
1247 /* Provide debugging output. */
1248 if (cp_lexer_debugging_p (lexer))
1249 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1250
1251 lexer->saved_tokens.pop ();
1252 }
1253
1254 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1255 to the token stream. Stop saving tokens. */
1256
1257 static void
1258 cp_lexer_rollback_tokens (cp_lexer* lexer)
1259 {
1260 /* Provide debugging output. */
1261 if (cp_lexer_debugging_p (lexer))
1262 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1263
1264 lexer->next_token = lexer->saved_tokens.pop ();
1265 }
1266
1267 /* RAII wrapper around the above functions, with sanity checking. Creating
1268 a variable saves tokens, which are committed when the variable is
1269 destroyed unless they are explicitly rolled back by calling the rollback
1270 member function. */
1271
1272 struct saved_token_sentinel
1273 {
1274 cp_lexer *lexer;
1275 unsigned len;
1276 bool commit;
1277 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1278 {
1279 len = lexer->saved_tokens.length ();
1280 cp_lexer_save_tokens (lexer);
1281 }
1282 void rollback ()
1283 {
1284 cp_lexer_rollback_tokens (lexer);
1285 commit = false;
1286 }
1287 ~saved_token_sentinel()
1288 {
1289 if (commit)
1290 cp_lexer_commit_tokens (lexer);
1291 gcc_assert (lexer->saved_tokens.length () == len);
1292 }
1293 };
1294
1295 /* Print a representation of the TOKEN on the STREAM. */
1296
1297 static void
1298 cp_lexer_print_token (FILE * stream, cp_token *token)
1299 {
1300 /* We don't use cpp_type2name here because the parser defines
1301 a few tokens of its own. */
1302 static const char *const token_names[] = {
1303 /* cpplib-defined token types */
1304 #define OP(e, s) #e,
1305 #define TK(e, s) #e,
1306 TTYPE_TABLE
1307 #undef OP
1308 #undef TK
1309 /* C++ parser token types - see "Manifest constants", above. */
1310 "KEYWORD",
1311 "TEMPLATE_ID",
1312 "NESTED_NAME_SPECIFIER",
1313 };
1314
1315 /* For some tokens, print the associated data. */
1316 switch (token->type)
1317 {
1318 case CPP_KEYWORD:
1319 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1320 For example, `struct' is mapped to an INTEGER_CST. */
1321 if (!identifier_p (token->u.value))
1322 break;
1323 /* fall through */
1324 case CPP_NAME:
1325 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1326 break;
1327
1328 case CPP_STRING:
1329 case CPP_STRING16:
1330 case CPP_STRING32:
1331 case CPP_WSTRING:
1332 case CPP_UTF8STRING:
1333 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1334 break;
1335
1336 case CPP_NUMBER:
1337 print_generic_expr (stream, token->u.value);
1338 break;
1339
1340 default:
1341 /* If we have a name for the token, print it out. Otherwise, we
1342 simply give the numeric code. */
1343 if (token->type < ARRAY_SIZE(token_names))
1344 fputs (token_names[token->type], stream);
1345 else
1346 fprintf (stream, "[%d]", token->type);
1347 break;
1348 }
1349 }
1350
1351 DEBUG_FUNCTION void
1352 debug (cp_token &ref)
1353 {
1354 cp_lexer_print_token (stderr, &ref);
1355 fprintf (stderr, "\n");
1356 }
1357
1358 DEBUG_FUNCTION void
1359 debug (cp_token *ptr)
1360 {
1361 if (ptr)
1362 debug (*ptr);
1363 else
1364 fprintf (stderr, "<nil>\n");
1365 }
1366
1367
1368 /* Start emitting debugging information. */
1369
1370 static void
1371 cp_lexer_start_debugging (cp_lexer* lexer)
1372 {
1373 if (!LEXER_DEBUGGING_ENABLED_P)
1374 fatal_error (input_location,
1375 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1376
1377 lexer->debugging_p = true;
1378 cp_lexer_debug_stream = stderr;
1379 }
1380
1381 /* Stop emitting debugging information. */
1382
1383 static void
1384 cp_lexer_stop_debugging (cp_lexer* lexer)
1385 {
1386 if (!LEXER_DEBUGGING_ENABLED_P)
1387 fatal_error (input_location,
1388 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1389
1390 lexer->debugging_p = false;
1391 cp_lexer_debug_stream = NULL;
1392 }
1393
1394 /* Create a new cp_token_cache, representing a range of tokens. */
1395
1396 static cp_token_cache *
1397 cp_token_cache_new (cp_token *first, cp_token *last)
1398 {
1399 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1400 cache->first = first;
1401 cache->last = last;
1402 return cache;
1403 }
1404
1405 /* Diagnose if #pragma omp declare simd isn't followed immediately
1406 by function declaration or definition. */
1407
1408 static inline void
1409 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1410 {
1411 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1412 {
1413 error ("%<#pragma omp declare %s%> not immediately followed by "
1414 "function declaration or definition",
1415 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1416 parser->omp_declare_simd = NULL;
1417 }
1418 }
1419
1420 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1421 and put that into "omp declare simd" attribute. */
1422
1423 static inline void
1424 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1425 {
1426 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1427 {
1428 if (fndecl == error_mark_node)
1429 {
1430 parser->omp_declare_simd = NULL;
1431 return;
1432 }
1433 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1434 {
1435 cp_ensure_no_omp_declare_simd (parser);
1436 return;
1437 }
1438 }
1439 }
1440
1441 /* Diagnose if #pragma acc routine isn't followed immediately by function
1442 declaration or definition. */
1443
1444 static inline void
1445 cp_ensure_no_oacc_routine (cp_parser *parser)
1446 {
1447 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1448 {
1449 error_at (parser->oacc_routine->loc,
1450 "%<#pragma acc routine%> not immediately followed by "
1451 "function declaration or definition");
1452 parser->oacc_routine = NULL;
1453 }
1454 }
1455 \f
1456 /* Decl-specifiers. */
1457
1458 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1459
1460 static void
1461 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1462 {
1463 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1464 }
1465
1466 /* Declarators. */
1467
1468 /* Nothing other than the parser should be creating declarators;
1469 declarators are a semi-syntactic representation of C++ entities.
1470 Other parts of the front end that need to create entities (like
1471 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1472
1473 static cp_declarator *make_call_declarator
1474 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier,
1475 tree, tree, tree, tree, location_t);
1476 static cp_declarator *make_array_declarator
1477 (cp_declarator *, tree);
1478 static cp_declarator *make_pointer_declarator
1479 (cp_cv_quals, cp_declarator *, tree);
1480 static cp_declarator *make_reference_declarator
1481 (cp_cv_quals, cp_declarator *, bool, tree);
1482 static cp_declarator *make_ptrmem_declarator
1483 (cp_cv_quals, tree, cp_declarator *, tree);
1484
1485 /* An erroneous declarator. */
1486 static cp_declarator *cp_error_declarator;
1487
1488 /* The obstack on which declarators and related data structures are
1489 allocated. */
1490 static struct obstack declarator_obstack;
1491
1492 /* Alloc BYTES from the declarator memory pool. */
1493
1494 static inline void *
1495 alloc_declarator (size_t bytes)
1496 {
1497 return obstack_alloc (&declarator_obstack, bytes);
1498 }
1499
1500 /* Allocate a declarator of the indicated KIND. Clear fields that are
1501 common to all declarators. */
1502
1503 static cp_declarator *
1504 make_declarator (cp_declarator_kind kind)
1505 {
1506 cp_declarator *declarator;
1507
1508 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1509 declarator->kind = kind;
1510 declarator->parenthesized = UNKNOWN_LOCATION;
1511 declarator->attributes = NULL_TREE;
1512 declarator->std_attributes = NULL_TREE;
1513 declarator->declarator = NULL;
1514 declarator->parameter_pack_p = false;
1515 declarator->id_loc = UNKNOWN_LOCATION;
1516
1517 return declarator;
1518 }
1519
1520 /* Make a declarator for a generalized identifier. If
1521 QUALIFYING_SCOPE is non-NULL, the identifier is
1522 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1523 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1524 is, if any. */
1525
1526 static cp_declarator *
1527 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1528 special_function_kind sfk, location_t id_location)
1529 {
1530 cp_declarator *declarator;
1531
1532 /* It is valid to write:
1533
1534 class C { void f(); };
1535 typedef C D;
1536 void D::f();
1537
1538 The standard is not clear about whether `typedef const C D' is
1539 legal; as of 2002-09-15 the committee is considering that
1540 question. EDG 3.0 allows that syntax. Therefore, we do as
1541 well. */
1542 if (qualifying_scope && TYPE_P (qualifying_scope))
1543 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1544
1545 gcc_assert (identifier_p (unqualified_name)
1546 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1547 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1548
1549 declarator = make_declarator (cdk_id);
1550 declarator->u.id.qualifying_scope = qualifying_scope;
1551 declarator->u.id.unqualified_name = unqualified_name;
1552 declarator->u.id.sfk = sfk;
1553 declarator->id_loc = id_location;
1554
1555 return declarator;
1556 }
1557
1558 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1559 of modifiers such as const or volatile to apply to the pointer
1560 type, represented as identifiers. ATTRIBUTES represent the attributes that
1561 appertain to the pointer or reference. */
1562
1563 cp_declarator *
1564 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1565 tree attributes)
1566 {
1567 cp_declarator *declarator;
1568
1569 declarator = make_declarator (cdk_pointer);
1570 declarator->declarator = target;
1571 declarator->u.pointer.qualifiers = cv_qualifiers;
1572 declarator->u.pointer.class_type = NULL_TREE;
1573 if (target)
1574 {
1575 declarator->id_loc = target->id_loc;
1576 declarator->parameter_pack_p = target->parameter_pack_p;
1577 target->parameter_pack_p = false;
1578 }
1579 else
1580 declarator->parameter_pack_p = false;
1581
1582 declarator->std_attributes = attributes;
1583
1584 return declarator;
1585 }
1586
1587 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1588 represent the attributes that appertain to the pointer or
1589 reference. */
1590
1591 cp_declarator *
1592 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1593 bool rvalue_ref, tree attributes)
1594 {
1595 cp_declarator *declarator;
1596
1597 declarator = make_declarator (cdk_reference);
1598 declarator->declarator = target;
1599 declarator->u.reference.qualifiers = cv_qualifiers;
1600 declarator->u.reference.rvalue_ref = rvalue_ref;
1601 if (target)
1602 {
1603 declarator->id_loc = target->id_loc;
1604 declarator->parameter_pack_p = target->parameter_pack_p;
1605 target->parameter_pack_p = false;
1606 }
1607 else
1608 declarator->parameter_pack_p = false;
1609
1610 declarator->std_attributes = attributes;
1611
1612 return declarator;
1613 }
1614
1615 /* Like make_pointer_declarator -- but for a pointer to a non-static
1616 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1617 appertain to the pointer or reference. */
1618
1619 cp_declarator *
1620 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1621 cp_declarator *pointee,
1622 tree attributes)
1623 {
1624 cp_declarator *declarator;
1625
1626 declarator = make_declarator (cdk_ptrmem);
1627 declarator->declarator = pointee;
1628 declarator->u.pointer.qualifiers = cv_qualifiers;
1629 declarator->u.pointer.class_type = class_type;
1630
1631 if (pointee)
1632 {
1633 declarator->parameter_pack_p = pointee->parameter_pack_p;
1634 pointee->parameter_pack_p = false;
1635 }
1636 else
1637 declarator->parameter_pack_p = false;
1638
1639 declarator->std_attributes = attributes;
1640
1641 return declarator;
1642 }
1643
1644 /* Make a declarator for the function given by TARGET, with the
1645 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1646 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1647 indicates what exceptions can be thrown. */
1648
1649 cp_declarator *
1650 make_call_declarator (cp_declarator *target,
1651 tree parms,
1652 cp_cv_quals cv_qualifiers,
1653 cp_virt_specifiers virt_specifiers,
1654 cp_ref_qualifier ref_qualifier,
1655 tree tx_qualifier,
1656 tree exception_specification,
1657 tree late_return_type,
1658 tree requires_clause,
1659 location_t parens_loc)
1660 {
1661 cp_declarator *declarator;
1662
1663 declarator = make_declarator (cdk_function);
1664 declarator->declarator = target;
1665 declarator->u.function.parameters = parms;
1666 declarator->u.function.qualifiers = cv_qualifiers;
1667 declarator->u.function.virt_specifiers = virt_specifiers;
1668 declarator->u.function.ref_qualifier = ref_qualifier;
1669 declarator->u.function.tx_qualifier = tx_qualifier;
1670 declarator->u.function.exception_specification = exception_specification;
1671 declarator->u.function.late_return_type = late_return_type;
1672 declarator->u.function.requires_clause = requires_clause;
1673 declarator->u.function.parens_loc = parens_loc;
1674 if (target)
1675 {
1676 declarator->id_loc = target->id_loc;
1677 declarator->parameter_pack_p = target->parameter_pack_p;
1678 target->parameter_pack_p = false;
1679 }
1680 else
1681 declarator->parameter_pack_p = false;
1682
1683 return declarator;
1684 }
1685
1686 /* Make a declarator for an array of BOUNDS elements, each of which is
1687 defined by ELEMENT. */
1688
1689 cp_declarator *
1690 make_array_declarator (cp_declarator *element, tree bounds)
1691 {
1692 cp_declarator *declarator;
1693
1694 declarator = make_declarator (cdk_array);
1695 declarator->declarator = element;
1696 declarator->u.array.bounds = bounds;
1697 if (element)
1698 {
1699 declarator->id_loc = element->id_loc;
1700 declarator->parameter_pack_p = element->parameter_pack_p;
1701 element->parameter_pack_p = false;
1702 }
1703 else
1704 declarator->parameter_pack_p = false;
1705
1706 return declarator;
1707 }
1708
1709 /* Determine whether the declarator we've seen so far can be a
1710 parameter pack, when followed by an ellipsis. */
1711 static bool
1712 declarator_can_be_parameter_pack (cp_declarator *declarator)
1713 {
1714 if (declarator && declarator->parameter_pack_p)
1715 /* We already saw an ellipsis. */
1716 return false;
1717
1718 /* Search for a declarator name, or any other declarator that goes
1719 after the point where the ellipsis could appear in a parameter
1720 pack. If we find any of these, then this declarator cannot be
1721 made into a parameter pack. */
1722 bool found = false;
1723 while (declarator && !found)
1724 {
1725 switch ((int)declarator->kind)
1726 {
1727 case cdk_id:
1728 case cdk_array:
1729 case cdk_decomp:
1730 found = true;
1731 break;
1732
1733 case cdk_error:
1734 return true;
1735
1736 default:
1737 declarator = declarator->declarator;
1738 break;
1739 }
1740 }
1741
1742 return !found;
1743 }
1744
1745 cp_parameter_declarator *no_parameters;
1746
1747 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1748 DECLARATOR and DEFAULT_ARGUMENT. */
1749
1750 cp_parameter_declarator *
1751 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1752 cp_declarator *declarator,
1753 tree default_argument,
1754 location_t loc,
1755 bool template_parameter_pack_p = false)
1756 {
1757 cp_parameter_declarator *parameter;
1758
1759 parameter = ((cp_parameter_declarator *)
1760 alloc_declarator (sizeof (cp_parameter_declarator)));
1761 parameter->next = NULL;
1762 if (decl_specifiers)
1763 parameter->decl_specifiers = *decl_specifiers;
1764 else
1765 clear_decl_specs (&parameter->decl_specifiers);
1766 parameter->declarator = declarator;
1767 parameter->default_argument = default_argument;
1768 parameter->template_parameter_pack_p = template_parameter_pack_p;
1769 parameter->loc = loc;
1770
1771 return parameter;
1772 }
1773
1774 /* Returns true iff DECLARATOR is a declaration for a function. */
1775
1776 static bool
1777 function_declarator_p (const cp_declarator *declarator)
1778 {
1779 while (declarator)
1780 {
1781 if (declarator->kind == cdk_function
1782 && declarator->declarator->kind == cdk_id)
1783 return true;
1784 if (declarator->kind == cdk_id
1785 || declarator->kind == cdk_decomp
1786 || declarator->kind == cdk_error)
1787 return false;
1788 declarator = declarator->declarator;
1789 }
1790 return false;
1791 }
1792
1793 /* The parser. */
1794
1795 /* Overview
1796 --------
1797
1798 A cp_parser parses the token stream as specified by the C++
1799 grammar. Its job is purely parsing, not semantic analysis. For
1800 example, the parser breaks the token stream into declarators,
1801 expressions, statements, and other similar syntactic constructs.
1802 It does not check that the types of the expressions on either side
1803 of an assignment-statement are compatible, or that a function is
1804 not declared with a parameter of type `void'.
1805
1806 The parser invokes routines elsewhere in the compiler to perform
1807 semantic analysis and to build up the abstract syntax tree for the
1808 code processed.
1809
1810 The parser (and the template instantiation code, which is, in a
1811 way, a close relative of parsing) are the only parts of the
1812 compiler that should be calling push_scope and pop_scope, or
1813 related functions. The parser (and template instantiation code)
1814 keeps track of what scope is presently active; everything else
1815 should simply honor that. (The code that generates static
1816 initializers may also need to set the scope, in order to check
1817 access control correctly when emitting the initializers.)
1818
1819 Methodology
1820 -----------
1821
1822 The parser is of the standard recursive-descent variety. Upcoming
1823 tokens in the token stream are examined in order to determine which
1824 production to use when parsing a non-terminal. Some C++ constructs
1825 require arbitrary look ahead to disambiguate. For example, it is
1826 impossible, in the general case, to tell whether a statement is an
1827 expression or declaration without scanning the entire statement.
1828 Therefore, the parser is capable of "parsing tentatively." When the
1829 parser is not sure what construct comes next, it enters this mode.
1830 Then, while we attempt to parse the construct, the parser queues up
1831 error messages, rather than issuing them immediately, and saves the
1832 tokens it consumes. If the construct is parsed successfully, the
1833 parser "commits", i.e., it issues any queued error messages and
1834 the tokens that were being preserved are permanently discarded.
1835 If, however, the construct is not parsed successfully, the parser
1836 rolls back its state completely so that it can resume parsing using
1837 a different alternative.
1838
1839 Future Improvements
1840 -------------------
1841
1842 The performance of the parser could probably be improved substantially.
1843 We could often eliminate the need to parse tentatively by looking ahead
1844 a little bit. In some places, this approach might not entirely eliminate
1845 the need to parse tentatively, but it might still speed up the average
1846 case. */
1847
1848 /* Flags that are passed to some parsing functions. These values can
1849 be bitwise-ored together. */
1850
1851 enum
1852 {
1853 /* No flags. */
1854 CP_PARSER_FLAGS_NONE = 0x0,
1855 /* The construct is optional. If it is not present, then no error
1856 should be issued. */
1857 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1858 /* When parsing a type-specifier, treat user-defined type-names
1859 as non-type identifiers. */
1860 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1861 /* When parsing a type-specifier, do not try to parse a class-specifier
1862 or enum-specifier. */
1863 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1864 /* When parsing a decl-specifier-seq, only allow type-specifier or
1865 constexpr. */
1866 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1867 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
1868 for C++20 consteval. */
1869 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1870 /* When parsing a decl-specifier-seq, allow missing typename. */
1871 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
1872 /* When parsing of the noexcept-specifier should be delayed. */
1873 CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
1874 /* When parsing a consteval declarator. */
1875 CP_PARSER_FLAGS_CONSTEVAL = 0x80
1876 };
1877
1878 /* This type is used for parameters and variables which hold
1879 combinations of the above flags. */
1880 typedef int cp_parser_flags;
1881
1882 /* The different kinds of declarators we want to parse. */
1883
1884 enum cp_parser_declarator_kind
1885 {
1886 /* We want an abstract declarator. */
1887 CP_PARSER_DECLARATOR_ABSTRACT,
1888 /* We want a named declarator. */
1889 CP_PARSER_DECLARATOR_NAMED,
1890 /* We don't mind, but the name must be an unqualified-id. */
1891 CP_PARSER_DECLARATOR_EITHER
1892 };
1893
1894 /* The precedence values used to parse binary expressions. The minimum value
1895 of PREC must be 1, because zero is reserved to quickly discriminate
1896 binary operators from other tokens. */
1897
1898 enum cp_parser_prec
1899 {
1900 PREC_NOT_OPERATOR,
1901 PREC_LOGICAL_OR_EXPRESSION,
1902 PREC_LOGICAL_AND_EXPRESSION,
1903 PREC_INCLUSIVE_OR_EXPRESSION,
1904 PREC_EXCLUSIVE_OR_EXPRESSION,
1905 PREC_AND_EXPRESSION,
1906 PREC_EQUALITY_EXPRESSION,
1907 PREC_RELATIONAL_EXPRESSION,
1908 PREC_SPACESHIP_EXPRESSION,
1909 PREC_SHIFT_EXPRESSION,
1910 PREC_ADDITIVE_EXPRESSION,
1911 PREC_MULTIPLICATIVE_EXPRESSION,
1912 PREC_PM_EXPRESSION,
1913 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1914 };
1915
1916 /* A mapping from a token type to a corresponding tree node type, with a
1917 precedence value. */
1918
1919 struct cp_parser_binary_operations_map_node
1920 {
1921 /* The token type. */
1922 enum cpp_ttype token_type;
1923 /* The corresponding tree code. */
1924 enum tree_code tree_type;
1925 /* The precedence of this operator. */
1926 enum cp_parser_prec prec;
1927 };
1928
1929 struct cp_parser_expression_stack_entry
1930 {
1931 /* Left hand side of the binary operation we are currently
1932 parsing. */
1933 cp_expr lhs;
1934 /* Original tree code for left hand side, if it was a binary
1935 expression itself (used for -Wparentheses). */
1936 enum tree_code lhs_type;
1937 /* Tree code for the binary operation we are parsing. */
1938 enum tree_code tree_type;
1939 /* Precedence of the binary operation we are parsing. */
1940 enum cp_parser_prec prec;
1941 /* Location of the binary operation we are parsing. */
1942 location_t loc;
1943 };
1944
1945 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1946 entries because precedence levels on the stack are monotonically
1947 increasing. */
1948 typedef struct cp_parser_expression_stack_entry
1949 cp_parser_expression_stack[NUM_PREC_VALUES];
1950
1951 /* Prototypes. */
1952
1953 /* Constructors and destructors. */
1954
1955 static cp_parser_context *cp_parser_context_new
1956 (cp_parser_context *);
1957
1958 /* Class variables. */
1959
1960 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1961
1962 /* The operator-precedence table used by cp_parser_binary_expression.
1963 Transformed into an associative array (binops_by_token) by
1964 cp_parser_new. */
1965
1966 static const cp_parser_binary_operations_map_node binops[] = {
1967 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1968 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1969
1970 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1971 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1972 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1973
1974 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1975 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1976
1977 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1978 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1979
1980 { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
1981
1982 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1983 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1984 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1985 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1986
1987 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1988 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1989
1990 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1991
1992 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1993
1994 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1995
1996 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1997
1998 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1999 };
2000
2001 /* The same as binops, but initialized by cp_parser_new so that
2002 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
2003 for speed. */
2004 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
2005
2006 /* Constructors and destructors. */
2007
2008 /* Construct a new context. The context below this one on the stack
2009 is given by NEXT. */
2010
2011 static cp_parser_context *
2012 cp_parser_context_new (cp_parser_context* next)
2013 {
2014 cp_parser_context *context;
2015
2016 /* Allocate the storage. */
2017 if (cp_parser_context_free_list != NULL)
2018 {
2019 /* Pull the first entry from the free list. */
2020 context = cp_parser_context_free_list;
2021 cp_parser_context_free_list = context->next;
2022 memset (context, 0, sizeof (*context));
2023 }
2024 else
2025 context = ggc_cleared_alloc<cp_parser_context> ();
2026
2027 /* No errors have occurred yet in this context. */
2028 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
2029 /* If this is not the bottommost context, copy information that we
2030 need from the previous context. */
2031 if (next)
2032 {
2033 /* If, in the NEXT context, we are parsing an `x->' or `x.'
2034 expression, then we are parsing one in this context, too. */
2035 context->object_type = next->object_type;
2036 /* Thread the stack. */
2037 context->next = next;
2038 }
2039
2040 return context;
2041 }
2042
2043 /* Managing the unparsed function queues. */
2044
2045 #define unparsed_funs_with_default_args \
2046 parser->unparsed_queues->last ().funs_with_default_args
2047 #define unparsed_funs_with_definitions \
2048 parser->unparsed_queues->last ().funs_with_definitions
2049 #define unparsed_nsdmis \
2050 parser->unparsed_queues->last ().nsdmis
2051 #define unparsed_noexcepts \
2052 parser->unparsed_queues->last ().noexcepts
2053
2054 static void
2055 push_unparsed_function_queues (cp_parser *parser)
2056 {
2057 cp_unparsed_functions_entry e = { NULL, make_tree_vector (), NULL, NULL };
2058 vec_safe_push (parser->unparsed_queues, e);
2059 }
2060
2061 static void
2062 pop_unparsed_function_queues (cp_parser *parser)
2063 {
2064 release_tree_vector (unparsed_funs_with_definitions);
2065 parser->unparsed_queues->pop ();
2066 }
2067
2068 /* Prototypes. */
2069
2070 /* Constructors and destructors. */
2071
2072 static cp_parser *cp_parser_new
2073 (cp_lexer *);
2074
2075 /* Routines to parse various constructs.
2076
2077 Those that return `tree' will return the error_mark_node (rather
2078 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2079 Sometimes, they will return an ordinary node if error-recovery was
2080 attempted, even though a parse error occurred. So, to check
2081 whether or not a parse error occurred, you should always use
2082 cp_parser_error_occurred. If the construct is optional (indicated
2083 either by an `_opt' in the name of the function that does the
2084 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2085 the construct is not present. */
2086
2087 /* Lexical conventions [gram.lex] */
2088
2089 static cp_expr cp_parser_identifier
2090 (cp_parser *);
2091 static cp_expr cp_parser_string_literal
2092 (cp_parser *, bool, bool, bool);
2093 static cp_expr cp_parser_userdef_char_literal
2094 (cp_parser *);
2095 static tree cp_parser_userdef_string_literal
2096 (tree);
2097 static cp_expr cp_parser_userdef_numeric_literal
2098 (cp_parser *);
2099
2100 /* Basic concepts [gram.basic] */
2101
2102 static void cp_parser_translation_unit (cp_parser *);
2103
2104 /* Expressions [gram.expr] */
2105
2106 static cp_expr cp_parser_primary_expression
2107 (cp_parser *, bool, bool, bool, cp_id_kind *);
2108 static cp_expr cp_parser_id_expression
2109 (cp_parser *, bool, bool, bool *, bool, bool);
2110 static cp_expr cp_parser_unqualified_id
2111 (cp_parser *, bool, bool, bool, bool);
2112 static tree cp_parser_nested_name_specifier_opt
2113 (cp_parser *, bool, bool, bool, bool, bool = false);
2114 static tree cp_parser_nested_name_specifier
2115 (cp_parser *, bool, bool, bool, bool);
2116 static tree cp_parser_qualifying_entity
2117 (cp_parser *, bool, bool, bool, bool, bool);
2118 static cp_expr cp_parser_postfix_expression
2119 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2120 static tree cp_parser_postfix_open_square_expression
2121 (cp_parser *, tree, bool, bool);
2122 static tree cp_parser_postfix_dot_deref_expression
2123 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2124 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2125 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2126 bool = false);
2127 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2128 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2129 static void cp_parser_pseudo_destructor_name
2130 (cp_parser *, tree, tree *, tree *);
2131 static cp_expr cp_parser_unary_expression
2132 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2133 static enum tree_code cp_parser_unary_operator
2134 (cp_token *);
2135 static tree cp_parser_has_attribute_expression
2136 (cp_parser *);
2137 static tree cp_parser_new_expression
2138 (cp_parser *);
2139 static vec<tree, va_gc> *cp_parser_new_placement
2140 (cp_parser *);
2141 static tree cp_parser_new_type_id
2142 (cp_parser *, tree *);
2143 static cp_declarator *cp_parser_new_declarator_opt
2144 (cp_parser *);
2145 static cp_declarator *cp_parser_direct_new_declarator
2146 (cp_parser *);
2147 static vec<tree, va_gc> *cp_parser_new_initializer
2148 (cp_parser *);
2149 static tree cp_parser_delete_expression
2150 (cp_parser *);
2151 static cp_expr cp_parser_cast_expression
2152 (cp_parser *, bool, bool, bool, cp_id_kind *);
2153 static cp_expr cp_parser_binary_expression
2154 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2155 static tree cp_parser_question_colon_clause
2156 (cp_parser *, cp_expr);
2157 static cp_expr cp_parser_assignment_expression
2158 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2159 static enum tree_code cp_parser_assignment_operator_opt
2160 (cp_parser *);
2161 static cp_expr cp_parser_expression
2162 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2163 static cp_expr cp_parser_constant_expression
2164 (cp_parser *, bool = false, bool * = NULL, bool = false);
2165 static cp_expr cp_parser_builtin_offsetof
2166 (cp_parser *);
2167 static cp_expr cp_parser_lambda_expression
2168 (cp_parser *);
2169 static void cp_parser_lambda_introducer
2170 (cp_parser *, tree);
2171 static bool cp_parser_lambda_declarator_opt
2172 (cp_parser *, tree);
2173 static void cp_parser_lambda_body
2174 (cp_parser *, tree);
2175
2176 /* Statements [gram.stmt.stmt] */
2177
2178 static void cp_parser_statement
2179 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2180 static void cp_parser_label_for_labeled_statement
2181 (cp_parser *, tree);
2182 static tree cp_parser_expression_statement
2183 (cp_parser *, tree);
2184 static tree cp_parser_compound_statement
2185 (cp_parser *, tree, int, bool);
2186 static void cp_parser_statement_seq_opt
2187 (cp_parser *, tree);
2188 static tree cp_parser_selection_statement
2189 (cp_parser *, bool *, vec<tree> *);
2190 static tree cp_parser_condition
2191 (cp_parser *);
2192 static tree cp_parser_iteration_statement
2193 (cp_parser *, bool *, bool, unsigned short);
2194 static bool cp_parser_init_statement
2195 (cp_parser *, tree *decl);
2196 static tree cp_parser_for
2197 (cp_parser *, bool, unsigned short);
2198 static tree cp_parser_c_for
2199 (cp_parser *, tree, tree, bool, unsigned short);
2200 static tree cp_parser_range_for
2201 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2202 static void do_range_for_auto_deduction
2203 (tree, tree);
2204 static tree cp_parser_perform_range_for_lookup
2205 (tree, tree *, tree *);
2206 static tree cp_parser_range_for_member_function
2207 (tree, tree);
2208 static tree cp_parser_jump_statement
2209 (cp_parser *);
2210 static void cp_parser_declaration_statement
2211 (cp_parser *);
2212
2213 static tree cp_parser_implicitly_scoped_statement
2214 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2215 static void cp_parser_already_scoped_statement
2216 (cp_parser *, bool *, const token_indent_info &);
2217
2218 /* State of module-declaration parsing. */
2219 enum module_parse
2220 {
2221 MP_NOT_MODULE, /* Not a module. */
2222
2223 _MP_UNUSED,
2224
2225 MP_FIRST, /* First declaration of TU. */
2226 MP_GLOBAL, /* Global Module Fragment. */
2227
2228 MP_PURVIEW_IMPORTS, /* Imports of a module. */
2229 MP_PURVIEW, /* Purview of a named module. */
2230
2231 MP_PRIVATE_IMPORTS, /* Imports of a Private Module Fragment. */
2232 MP_PRIVATE, /* Private Module Fragment. */
2233 };
2234
2235 static module_parse cp_parser_module_declaration
2236 (cp_parser *parser, module_parse, bool exporting);
2237 static void cp_parser_import_declaration
2238 (cp_parser *parser, module_parse, bool exporting);
2239
2240 /* Declarations [gram.dcl.dcl] */
2241
2242 static void cp_parser_declaration_seq_opt
2243 (cp_parser *);
2244 static void cp_parser_declaration
2245 (cp_parser *, tree);
2246 static void cp_parser_toplevel_declaration
2247 (cp_parser *);
2248 static void cp_parser_block_declaration
2249 (cp_parser *, bool);
2250 static void cp_parser_simple_declaration
2251 (cp_parser *, bool, tree *);
2252 static void cp_parser_decl_specifier_seq
2253 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2254 static tree cp_parser_storage_class_specifier_opt
2255 (cp_parser *);
2256 static tree cp_parser_function_specifier_opt
2257 (cp_parser *, cp_decl_specifier_seq *);
2258 static tree cp_parser_type_specifier
2259 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2260 int *, bool *);
2261 static tree cp_parser_simple_type_specifier
2262 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2263 static tree cp_parser_placeholder_type_specifier
2264 (cp_parser *, location_t, tree, bool);
2265 static tree cp_parser_type_name
2266 (cp_parser *, bool);
2267 static tree cp_parser_nonclass_name
2268 (cp_parser* parser);
2269 static tree cp_parser_elaborated_type_specifier
2270 (cp_parser *, bool, bool);
2271 static tree cp_parser_enum_specifier
2272 (cp_parser *);
2273 static void cp_parser_enumerator_list
2274 (cp_parser *, tree);
2275 static void cp_parser_enumerator_definition
2276 (cp_parser *, tree);
2277 static tree cp_parser_namespace_name
2278 (cp_parser *);
2279 static void cp_parser_namespace_definition
2280 (cp_parser *);
2281 static void cp_parser_namespace_body
2282 (cp_parser *);
2283 static tree cp_parser_qualified_namespace_specifier
2284 (cp_parser *);
2285 static void cp_parser_namespace_alias_definition
2286 (cp_parser *);
2287 static bool cp_parser_using_declaration
2288 (cp_parser *, bool);
2289 static void cp_parser_using_directive
2290 (cp_parser *);
2291 static void cp_parser_using_enum
2292 (cp_parser *);
2293 static tree cp_parser_alias_declaration
2294 (cp_parser *);
2295 static void cp_parser_asm_definition
2296 (cp_parser *);
2297 static void cp_parser_linkage_specification
2298 (cp_parser *, tree);
2299 static void cp_parser_static_assert
2300 (cp_parser *, bool);
2301 static tree cp_parser_decltype
2302 (cp_parser *);
2303 static tree cp_parser_decomposition_declaration
2304 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2305
2306 /* Declarators [gram.dcl.decl] */
2307
2308 static tree cp_parser_init_declarator
2309 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2310 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2311 location_t *, tree *);
2312 static cp_declarator *cp_parser_declarator
2313 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2314 bool, bool, bool);
2315 static cp_declarator *cp_parser_direct_declarator
2316 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2317 bool);
2318 static enum tree_code cp_parser_ptr_operator
2319 (cp_parser *, tree *, cp_cv_quals *, tree *);
2320 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2321 (cp_parser *);
2322 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2323 (cp_parser *);
2324 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2325 (cp_parser *);
2326 static tree cp_parser_tx_qualifier_opt
2327 (cp_parser *);
2328 static tree cp_parser_late_return_type_opt
2329 (cp_parser *, cp_declarator *, tree &);
2330 static tree cp_parser_declarator_id
2331 (cp_parser *, bool);
2332 static tree cp_parser_type_id
2333 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2334 static tree cp_parser_template_type_arg
2335 (cp_parser *);
2336 static tree cp_parser_trailing_type_id (cp_parser *);
2337 static tree cp_parser_type_id_1
2338 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2339 static void cp_parser_type_specifier_seq
2340 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2341 static tree cp_parser_parameter_declaration_clause
2342 (cp_parser *, cp_parser_flags);
2343 static tree cp_parser_parameter_declaration_list
2344 (cp_parser *, cp_parser_flags);
2345 static cp_parameter_declarator *cp_parser_parameter_declaration
2346 (cp_parser *, cp_parser_flags, bool, bool *);
2347 static tree cp_parser_default_argument
2348 (cp_parser *, bool);
2349 static void cp_parser_function_body
2350 (cp_parser *, bool);
2351 static tree cp_parser_initializer
2352 (cp_parser *, bool *, bool *, bool = false);
2353 static cp_expr cp_parser_initializer_clause
2354 (cp_parser *, bool *);
2355 static cp_expr cp_parser_braced_list
2356 (cp_parser*, bool*);
2357 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2358 (cp_parser *, bool *, bool *);
2359
2360 static void cp_parser_ctor_initializer_opt_and_function_body
2361 (cp_parser *, bool);
2362
2363 static tree cp_parser_late_parsing_omp_declare_simd
2364 (cp_parser *, tree);
2365
2366 static tree cp_parser_late_parsing_oacc_routine
2367 (cp_parser *, tree);
2368
2369 static tree synthesize_implicit_template_parm
2370 (cp_parser *, tree);
2371 static tree finish_fully_implicit_template
2372 (cp_parser *, tree);
2373 static void abort_fully_implicit_template
2374 (cp_parser *);
2375
2376 /* Classes [gram.class] */
2377
2378 static tree cp_parser_class_name
2379 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2380 static tree cp_parser_class_specifier
2381 (cp_parser *);
2382 static tree cp_parser_class_head
2383 (cp_parser *, bool *);
2384 static enum tag_types cp_parser_class_key
2385 (cp_parser *);
2386 static void cp_parser_type_parameter_key
2387 (cp_parser* parser);
2388 static void cp_parser_member_specification_opt
2389 (cp_parser *);
2390 static void cp_parser_member_declaration
2391 (cp_parser *);
2392 static tree cp_parser_pure_specifier
2393 (cp_parser *);
2394 static tree cp_parser_constant_initializer
2395 (cp_parser *);
2396
2397 /* Derived classes [gram.class.derived] */
2398
2399 static tree cp_parser_base_clause
2400 (cp_parser *);
2401 static tree cp_parser_base_specifier
2402 (cp_parser *);
2403
2404 /* Special member functions [gram.special] */
2405
2406 static tree cp_parser_conversion_function_id
2407 (cp_parser *);
2408 static tree cp_parser_conversion_type_id
2409 (cp_parser *);
2410 static cp_declarator *cp_parser_conversion_declarator_opt
2411 (cp_parser *);
2412 static void cp_parser_ctor_initializer_opt
2413 (cp_parser *);
2414 static void cp_parser_mem_initializer_list
2415 (cp_parser *);
2416 static tree cp_parser_mem_initializer
2417 (cp_parser *);
2418 static tree cp_parser_mem_initializer_id
2419 (cp_parser *);
2420
2421 /* Overloading [gram.over] */
2422
2423 static cp_expr cp_parser_operator_function_id
2424 (cp_parser *);
2425 static cp_expr cp_parser_operator
2426 (cp_parser *, location_t);
2427
2428 /* Templates [gram.temp] */
2429
2430 static void cp_parser_template_declaration
2431 (cp_parser *, bool);
2432 static tree cp_parser_template_parameter_list
2433 (cp_parser *);
2434 static tree cp_parser_template_parameter
2435 (cp_parser *, bool *, bool *);
2436 static tree cp_parser_type_parameter
2437 (cp_parser *, bool *);
2438 static tree cp_parser_template_id
2439 (cp_parser *, bool, bool, enum tag_types, bool);
2440 static tree cp_parser_template_id_expr
2441 (cp_parser *, bool, bool, bool);
2442 static tree cp_parser_template_name
2443 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2444 static tree cp_parser_template_argument_list
2445 (cp_parser *);
2446 static tree cp_parser_template_argument
2447 (cp_parser *);
2448 static void cp_parser_explicit_instantiation
2449 (cp_parser *);
2450 static void cp_parser_explicit_specialization
2451 (cp_parser *);
2452
2453 /* Exception handling [gram.except] */
2454
2455 static tree cp_parser_try_block
2456 (cp_parser *);
2457 static void cp_parser_function_try_block
2458 (cp_parser *);
2459 static void cp_parser_handler_seq
2460 (cp_parser *);
2461 static void cp_parser_handler
2462 (cp_parser *);
2463 static tree cp_parser_exception_declaration
2464 (cp_parser *);
2465 static tree cp_parser_throw_expression
2466 (cp_parser *);
2467 static tree cp_parser_exception_specification_opt
2468 (cp_parser *, cp_parser_flags);
2469 static tree cp_parser_type_id_list
2470 (cp_parser *);
2471 static tree cp_parser_noexcept_specification_opt
2472 (cp_parser *, cp_parser_flags, bool, bool *, bool);
2473
2474 /* GNU Extensions */
2475
2476 static tree cp_parser_asm_specification_opt
2477 (cp_parser *);
2478 static tree cp_parser_asm_operand_list
2479 (cp_parser *);
2480 static tree cp_parser_asm_clobber_list
2481 (cp_parser *);
2482 static tree cp_parser_asm_label_list
2483 (cp_parser *);
2484 static bool cp_next_tokens_can_be_attribute_p
2485 (cp_parser *);
2486 static bool cp_next_tokens_can_be_gnu_attribute_p
2487 (cp_parser *);
2488 static bool cp_next_tokens_can_be_std_attribute_p
2489 (cp_parser *);
2490 static bool cp_nth_tokens_can_be_std_attribute_p
2491 (cp_parser *, size_t);
2492 static bool cp_nth_tokens_can_be_gnu_attribute_p
2493 (cp_parser *, size_t);
2494 static bool cp_nth_tokens_can_be_attribute_p
2495 (cp_parser *, size_t);
2496 static tree cp_parser_attributes_opt
2497 (cp_parser *);
2498 static tree cp_parser_gnu_attributes_opt
2499 (cp_parser *);
2500 static tree cp_parser_gnu_attribute_list
2501 (cp_parser *, bool = false);
2502 static tree cp_parser_std_attribute
2503 (cp_parser *, tree);
2504 static tree cp_parser_std_attribute_spec
2505 (cp_parser *);
2506 static tree cp_parser_std_attribute_spec_seq
2507 (cp_parser *);
2508 static size_t cp_parser_skip_attributes_opt
2509 (cp_parser *, size_t);
2510 static bool cp_parser_extension_opt
2511 (cp_parser *, int *);
2512 static void cp_parser_label_declaration
2513 (cp_parser *);
2514
2515 /* Concept Extensions */
2516
2517 static tree cp_parser_concept_definition
2518 (cp_parser *);
2519 static tree cp_parser_constraint_expression
2520 (cp_parser *);
2521 static tree cp_parser_requires_clause_opt
2522 (cp_parser *, bool);
2523 static tree cp_parser_requires_expression
2524 (cp_parser *);
2525 static tree cp_parser_requirement_parameter_list
2526 (cp_parser *);
2527 static tree cp_parser_requirement_body
2528 (cp_parser *);
2529 static tree cp_parser_requirement_seq
2530 (cp_parser *);
2531 static tree cp_parser_requirement
2532 (cp_parser *);
2533 static tree cp_parser_simple_requirement
2534 (cp_parser *);
2535 static tree cp_parser_compound_requirement
2536 (cp_parser *);
2537 static tree cp_parser_type_requirement
2538 (cp_parser *);
2539 static tree cp_parser_nested_requirement
2540 (cp_parser *);
2541
2542 /* Transactional Memory Extensions */
2543
2544 static tree cp_parser_transaction
2545 (cp_parser *, cp_token *);
2546 static tree cp_parser_transaction_expression
2547 (cp_parser *, enum rid);
2548 static void cp_parser_function_transaction
2549 (cp_parser *, enum rid);
2550 static tree cp_parser_transaction_cancel
2551 (cp_parser *);
2552
2553 /* Coroutine extensions. */
2554
2555 static tree cp_parser_yield_expression
2556 (cp_parser *);
2557
2558
2559 enum pragma_context {
2560 pragma_external,
2561 pragma_member,
2562 pragma_objc_icode,
2563 pragma_stmt,
2564 pragma_compound
2565 };
2566 static bool cp_parser_pragma
2567 (cp_parser *, enum pragma_context, bool *);
2568
2569 /* Objective-C++ Productions */
2570
2571 static tree cp_parser_objc_message_receiver
2572 (cp_parser *);
2573 static tree cp_parser_objc_message_args
2574 (cp_parser *);
2575 static tree cp_parser_objc_message_expression
2576 (cp_parser *);
2577 static cp_expr cp_parser_objc_encode_expression
2578 (cp_parser *);
2579 static tree cp_parser_objc_defs_expression
2580 (cp_parser *);
2581 static tree cp_parser_objc_protocol_expression
2582 (cp_parser *);
2583 static tree cp_parser_objc_selector_expression
2584 (cp_parser *);
2585 static cp_expr cp_parser_objc_expression
2586 (cp_parser *);
2587 static bool cp_parser_objc_selector_p
2588 (enum cpp_ttype);
2589 static tree cp_parser_objc_selector
2590 (cp_parser *);
2591 static tree cp_parser_objc_protocol_refs_opt
2592 (cp_parser *);
2593 static void cp_parser_objc_declaration
2594 (cp_parser *, tree);
2595 static tree cp_parser_objc_statement
2596 (cp_parser *);
2597 static bool cp_parser_objc_valid_prefix_attributes
2598 (cp_parser *, tree *);
2599 static void cp_parser_objc_at_property_declaration
2600 (cp_parser *) ;
2601 static void cp_parser_objc_at_synthesize_declaration
2602 (cp_parser *) ;
2603 static void cp_parser_objc_at_dynamic_declaration
2604 (cp_parser *) ;
2605 static tree cp_parser_objc_struct_declaration
2606 (cp_parser *) ;
2607
2608 /* Utility Routines */
2609
2610 static cp_expr cp_parser_lookup_name
2611 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2612 static tree cp_parser_lookup_name_simple
2613 (cp_parser *, tree, location_t);
2614 static tree cp_parser_maybe_treat_template_as_class
2615 (tree, bool);
2616 static bool cp_parser_check_declarator_template_parameters
2617 (cp_parser *, cp_declarator *, location_t);
2618 static bool cp_parser_check_template_parameters
2619 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2620 static cp_expr cp_parser_simple_cast_expression
2621 (cp_parser *);
2622 static tree cp_parser_global_scope_opt
2623 (cp_parser *, bool);
2624 static bool cp_parser_constructor_declarator_p
2625 (cp_parser *, cp_parser_flags, bool);
2626 static tree cp_parser_function_definition_from_specifiers_and_declarator
2627 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2628 static tree cp_parser_function_definition_after_declarator
2629 (cp_parser *, bool);
2630 static bool cp_parser_template_declaration_after_export
2631 (cp_parser *, bool);
2632 static void cp_parser_perform_template_parameter_access_checks
2633 (vec<deferred_access_check, va_gc> *);
2634 static tree cp_parser_single_declaration
2635 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2636 static cp_expr cp_parser_functional_cast
2637 (cp_parser *, tree);
2638 static tree cp_parser_save_member_function_body
2639 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2640 static tree cp_parser_save_nsdmi
2641 (cp_parser *);
2642 static tree cp_parser_enclosed_template_argument_list
2643 (cp_parser *);
2644 static void cp_parser_save_default_args
2645 (cp_parser *, tree);
2646 static void cp_parser_late_parsing_for_member
2647 (cp_parser *, tree);
2648 static tree cp_parser_late_parse_one_default_arg
2649 (cp_parser *, tree, tree, tree);
2650 static void cp_parser_late_parsing_nsdmi
2651 (cp_parser *, tree);
2652 static void cp_parser_late_parsing_default_args
2653 (cp_parser *, tree);
2654 static tree cp_parser_sizeof_operand
2655 (cp_parser *, enum rid);
2656 static cp_expr cp_parser_trait_expr
2657 (cp_parser *, enum rid);
2658 static bool cp_parser_declares_only_class_p
2659 (cp_parser *);
2660 static void cp_parser_set_storage_class
2661 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2662 static void cp_parser_set_decl_spec_type
2663 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2664 static void set_and_check_decl_spec_loc
2665 (cp_decl_specifier_seq *decl_specs,
2666 cp_decl_spec ds, cp_token *);
2667 static bool cp_parser_friend_p
2668 (const cp_decl_specifier_seq *);
2669 static void cp_parser_required_error
2670 (cp_parser *, required_token, bool, location_t);
2671 static cp_token *cp_parser_require
2672 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2673 static cp_token *cp_parser_require_keyword
2674 (cp_parser *, enum rid, required_token);
2675 static bool cp_parser_token_starts_function_definition_p
2676 (cp_token *);
2677 static bool cp_parser_next_token_starts_class_definition_p
2678 (cp_parser *);
2679 static bool cp_parser_next_token_ends_template_argument_p
2680 (cp_parser *);
2681 static bool cp_parser_nth_token_starts_template_argument_list_p
2682 (cp_parser *, size_t);
2683 static enum tag_types cp_parser_token_is_class_key
2684 (cp_token *);
2685 static enum tag_types cp_parser_token_is_type_parameter_key
2686 (cp_token *);
2687 static void cp_parser_maybe_warn_enum_key (cp_parser *, location_t, tree, rid);
2688 static void cp_parser_check_class_key
2689 (cp_parser *, location_t, enum tag_types, tree type, bool, bool);
2690 static void cp_parser_check_access_in_redeclaration
2691 (tree type, location_t location);
2692 static bool cp_parser_optional_template_keyword
2693 (cp_parser *);
2694 static void cp_parser_pre_parsed_nested_name_specifier
2695 (cp_parser *);
2696 static bool cp_parser_cache_group
2697 (cp_parser *, enum cpp_ttype, unsigned);
2698 static tree cp_parser_cache_defarg
2699 (cp_parser *parser, bool nsdmi);
2700 static void cp_parser_parse_tentatively
2701 (cp_parser *);
2702 static void cp_parser_commit_to_tentative_parse
2703 (cp_parser *);
2704 static void cp_parser_commit_to_topmost_tentative_parse
2705 (cp_parser *);
2706 static void cp_parser_abort_tentative_parse
2707 (cp_parser *);
2708 static bool cp_parser_parse_definitely
2709 (cp_parser *);
2710 static inline bool cp_parser_parsing_tentatively
2711 (cp_parser *);
2712 static bool cp_parser_uncommitted_to_tentative_parse_p
2713 (cp_parser *);
2714 static void cp_parser_error
2715 (cp_parser *, const char *);
2716 static void cp_parser_name_lookup_error
2717 (cp_parser *, tree, tree, name_lookup_error, location_t);
2718 static bool cp_parser_simulate_error
2719 (cp_parser *);
2720 static bool cp_parser_check_type_definition
2721 (cp_parser *);
2722 static void cp_parser_check_for_definition_in_return_type
2723 (cp_declarator *, tree, location_t type_location);
2724 static void cp_parser_check_for_invalid_template_id
2725 (cp_parser *, tree, enum tag_types, location_t location);
2726 static bool cp_parser_non_integral_constant_expression
2727 (cp_parser *, non_integral_constant);
2728 static void cp_parser_diagnose_invalid_type_name
2729 (cp_parser *, tree, location_t);
2730 static bool cp_parser_parse_and_diagnose_invalid_type_name
2731 (cp_parser *);
2732 static int cp_parser_skip_to_closing_parenthesis
2733 (cp_parser *, bool, bool, bool);
2734 static void cp_parser_skip_to_end_of_statement
2735 (cp_parser *);
2736 static void cp_parser_consume_semicolon_at_end_of_statement
2737 (cp_parser *);
2738 static void cp_parser_skip_to_end_of_block_or_statement
2739 (cp_parser *);
2740 static bool cp_parser_skip_to_closing_brace
2741 (cp_parser *);
2742 static void cp_parser_skip_to_end_of_template_parameter_list
2743 (cp_parser *);
2744 static void cp_parser_skip_to_pragma_eol
2745 (cp_parser*, cp_token *);
2746 static bool cp_parser_error_occurred
2747 (cp_parser *);
2748 static bool cp_parser_allow_gnu_extensions_p
2749 (cp_parser *);
2750 static bool cp_parser_is_pure_string_literal
2751 (cp_token *);
2752 static bool cp_parser_is_string_literal
2753 (cp_token *);
2754 static bool cp_parser_is_keyword
2755 (cp_token *, enum rid);
2756 static tree cp_parser_make_typename_type
2757 (cp_parser *, tree, location_t location);
2758 static cp_declarator * cp_parser_make_indirect_declarator
2759 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2760 static bool cp_parser_compound_literal_p
2761 (cp_parser *);
2762 static bool cp_parser_array_designator_p
2763 (cp_parser *);
2764 static bool cp_parser_init_statement_p
2765 (cp_parser *);
2766 static bool cp_parser_skip_to_closing_square_bracket
2767 (cp_parser *);
2768 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
2769
2770 // -------------------------------------------------------------------------- //
2771 // Unevaluated Operand Guard
2772 //
2773 // Implementation of an RAII helper for unevaluated operand parsing.
2774 cp_unevaluated::cp_unevaluated ()
2775 {
2776 ++cp_unevaluated_operand;
2777 ++c_inhibit_evaluation_warnings;
2778 }
2779
2780 cp_unevaluated::~cp_unevaluated ()
2781 {
2782 --c_inhibit_evaluation_warnings;
2783 --cp_unevaluated_operand;
2784 }
2785
2786 // -------------------------------------------------------------------------- //
2787 // Tentative Parsing
2788
2789 /* Returns nonzero if we are parsing tentatively. */
2790
2791 static inline bool
2792 cp_parser_parsing_tentatively (cp_parser* parser)
2793 {
2794 return parser->context->next != NULL;
2795 }
2796
2797 /* Returns nonzero if TOKEN is a string literal. */
2798
2799 static bool
2800 cp_parser_is_pure_string_literal (cp_token* token)
2801 {
2802 return (token->type == CPP_STRING ||
2803 token->type == CPP_STRING16 ||
2804 token->type == CPP_STRING32 ||
2805 token->type == CPP_WSTRING ||
2806 token->type == CPP_UTF8STRING);
2807 }
2808
2809 /* Returns nonzero if TOKEN is a string literal
2810 of a user-defined string literal. */
2811
2812 static bool
2813 cp_parser_is_string_literal (cp_token* token)
2814 {
2815 return (cp_parser_is_pure_string_literal (token) ||
2816 token->type == CPP_STRING_USERDEF ||
2817 token->type == CPP_STRING16_USERDEF ||
2818 token->type == CPP_STRING32_USERDEF ||
2819 token->type == CPP_WSTRING_USERDEF ||
2820 token->type == CPP_UTF8STRING_USERDEF);
2821 }
2822
2823 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2824
2825 static bool
2826 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2827 {
2828 return token->keyword == keyword;
2829 }
2830
2831 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2832 PRAGMA_NONE. */
2833
2834 static enum pragma_kind
2835 cp_parser_pragma_kind (cp_token *token)
2836 {
2837 if (token->type != CPP_PRAGMA)
2838 return PRAGMA_NONE;
2839 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2840 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2841 }
2842
2843 /* Helper function for cp_parser_error.
2844 Having peeked a token of kind TOK1_KIND that might signify
2845 a conflict marker, peek successor tokens to determine
2846 if we actually do have a conflict marker.
2847 Specifically, we consider a run of 7 '<', '=' or '>' characters
2848 at the start of a line as a conflict marker.
2849 These come through the lexer as three pairs and a single,
2850 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2851 If it returns true, *OUT_LOC is written to with the location/range
2852 of the marker. */
2853
2854 static bool
2855 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2856 location_t *out_loc)
2857 {
2858 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2859 if (token2->type != tok1_kind)
2860 return false;
2861 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2862 if (token3->type != tok1_kind)
2863 return false;
2864 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2865 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2866 return false;
2867
2868 /* It must be at the start of the line. */
2869 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2870 if (LOCATION_COLUMN (start_loc) != 1)
2871 return false;
2872
2873 /* We have a conflict marker. Construct a location of the form:
2874 <<<<<<<
2875 ^~~~~~~
2876 with start == caret, finishing at the end of the marker. */
2877 location_t finish_loc = get_finish (token4->location);
2878 *out_loc = make_location (start_loc, start_loc, finish_loc);
2879
2880 return true;
2881 }
2882
2883 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2884 RT_CLOSE_PAREN. */
2885
2886 static const char *
2887 get_matching_symbol (required_token token_desc)
2888 {
2889 switch (token_desc)
2890 {
2891 default:
2892 gcc_unreachable ();
2893 return "";
2894 case RT_CLOSE_BRACE:
2895 return "{";
2896 case RT_CLOSE_PAREN:
2897 return "(";
2898 }
2899 }
2900
2901 /* Attempt to convert TOKEN_DESC from a required_token to an
2902 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2903
2904 static enum cpp_ttype
2905 get_required_cpp_ttype (required_token token_desc)
2906 {
2907 switch (token_desc)
2908 {
2909 case RT_SEMICOLON:
2910 return CPP_SEMICOLON;
2911 case RT_OPEN_PAREN:
2912 return CPP_OPEN_PAREN;
2913 case RT_CLOSE_BRACE:
2914 return CPP_CLOSE_BRACE;
2915 case RT_OPEN_BRACE:
2916 return CPP_OPEN_BRACE;
2917 case RT_CLOSE_SQUARE:
2918 return CPP_CLOSE_SQUARE;
2919 case RT_OPEN_SQUARE:
2920 return CPP_OPEN_SQUARE;
2921 case RT_COMMA:
2922 return CPP_COMMA;
2923 case RT_COLON:
2924 return CPP_COLON;
2925 case RT_CLOSE_PAREN:
2926 return CPP_CLOSE_PAREN;
2927
2928 default:
2929 /* Use CPP_EOF as a "no completions possible" code. */
2930 return CPP_EOF;
2931 }
2932 }
2933
2934
2935 /* Subroutine of cp_parser_error and cp_parser_required_error.
2936
2937 Issue a diagnostic of the form
2938 FILE:LINE: MESSAGE before TOKEN
2939 where TOKEN is the next token in the input stream. MESSAGE
2940 (specified by the caller) is usually of the form "expected
2941 OTHER-TOKEN".
2942
2943 This bypasses the check for tentative passing, and potentially
2944 adds material needed by cp_parser_required_error.
2945
2946 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2947 suggesting insertion of the missing token.
2948
2949 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2950 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2951 location. */
2952
2953 static void
2954 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2955 required_token missing_token_desc,
2956 location_t matching_location)
2957 {
2958 cp_token *token = cp_lexer_peek_token (parser->lexer);
2959 /* This diagnostic makes more sense if it is tagged to the line
2960 of the token we just peeked at. */
2961 cp_lexer_set_source_position_from_token (token);
2962
2963 if (token->type == CPP_PRAGMA)
2964 {
2965 error_at (token->location,
2966 "%<#pragma%> is not allowed here");
2967 cp_parser_skip_to_pragma_eol (parser, token);
2968 return;
2969 }
2970
2971 /* If this is actually a conflict marker, report it as such. */
2972 if (token->type == CPP_LSHIFT
2973 || token->type == CPP_RSHIFT
2974 || token->type == CPP_EQ_EQ)
2975 {
2976 location_t loc;
2977 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2978 {
2979 error_at (loc, "version control conflict marker in file");
2980 expanded_location token_exploc = expand_location (token->location);
2981 /* Consume tokens until the end of the source line. */
2982 for (;;)
2983 {
2984 cp_lexer_consume_token (parser->lexer);
2985 cp_token *next = cp_lexer_peek_token (parser->lexer);
2986 if (next->type == CPP_EOF)
2987 break;
2988 if (next->location == UNKNOWN_LOCATION
2989 || loc == UNKNOWN_LOCATION)
2990 break;
2991
2992 expanded_location next_exploc = expand_location (next->location);
2993 if (next_exploc.file != token_exploc.file)
2994 break;
2995 if (next_exploc.line != token_exploc.line)
2996 break;
2997 }
2998 return;
2999 }
3000 }
3001
3002 auto_diagnostic_group d;
3003 gcc_rich_location richloc (input_location);
3004
3005 bool added_matching_location = false;
3006
3007 if (missing_token_desc != RT_NONE)
3008 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3009 {
3010 /* Potentially supply a fix-it hint, suggesting to add the
3011 missing token immediately after the *previous* token.
3012 This may move the primary location within richloc. */
3013 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
3014 location_t prev_token_loc = prev_token->location;
3015 maybe_suggest_missing_token_insertion (&richloc, ttype,
3016 prev_token_loc);
3017
3018 /* If matching_location != UNKNOWN_LOCATION, highlight it.
3019 Attempt to consolidate diagnostics by printing it as a
3020 secondary range within the main diagnostic. */
3021 if (matching_location != UNKNOWN_LOCATION)
3022 added_matching_location
3023 = richloc.add_location_if_nearby (matching_location);
3024 }
3025
3026 /* If we were parsing a string-literal and there is an unknown name
3027 token right after, then check to see if that could also have been
3028 a literal string by checking the name against a list of known
3029 standard string literal constants defined in header files. If
3030 there is one, then add that as an hint to the error message. */
3031 name_hint h;
3032 if (token->type == CPP_NAME)
3033 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3034 if (cp_parser_is_string_literal (prev_token))
3035 {
3036 tree name = token->u.value;
3037 const char *token_name = IDENTIFIER_POINTER (name);
3038 const char *header_hint
3039 = get_cp_stdlib_header_for_string_macro_name (token_name);
3040 if (header_hint != NULL)
3041 h = name_hint (NULL, new suggest_missing_header (token->location,
3042 token_name,
3043 header_hint));
3044 }
3045
3046 /* Actually emit the error. */
3047 c_parse_error (gmsgid,
3048 /* Because c_parser_error does not understand
3049 CPP_KEYWORD, keywords are treated like
3050 identifiers. */
3051 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
3052 token->u.value, token->flags, &richloc);
3053
3054 if (missing_token_desc != RT_NONE)
3055 {
3056 /* If we weren't able to consolidate matching_location, then
3057 print it as a secondary diagnostic. */
3058 if (matching_location != UNKNOWN_LOCATION
3059 && !added_matching_location)
3060 inform (matching_location, "to match this %qs",
3061 get_matching_symbol (missing_token_desc));
3062 }
3063 }
3064
3065 /* If not parsing tentatively, issue a diagnostic of the form
3066 FILE:LINE: MESSAGE before TOKEN
3067 where TOKEN is the next token in the input stream. MESSAGE
3068 (specified by the caller) is usually of the form "expected
3069 OTHER-TOKEN". */
3070
3071 static void
3072 cp_parser_error (cp_parser* parser, const char* gmsgid)
3073 {
3074 if (!cp_parser_simulate_error (parser))
3075 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
3076 }
3077
3078 /* Issue an error about name-lookup failing. NAME is the
3079 IDENTIFIER_NODE DECL is the result of
3080 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3081 the thing that we hoped to find. */
3082
3083 static void
3084 cp_parser_name_lookup_error (cp_parser* parser,
3085 tree name,
3086 tree decl,
3087 name_lookup_error desired,
3088 location_t location)
3089 {
3090 /* If name lookup completely failed, tell the user that NAME was not
3091 declared. */
3092 if (decl == error_mark_node)
3093 {
3094 if (parser->scope && parser->scope != global_namespace)
3095 error_at (location, "%<%E::%E%> has not been declared",
3096 parser->scope, name);
3097 else if (parser->scope == global_namespace)
3098 error_at (location, "%<::%E%> has not been declared", name);
3099 else if (parser->object_scope
3100 && !CLASS_TYPE_P (parser->object_scope))
3101 error_at (location, "request for member %qE in non-class type %qT",
3102 name, parser->object_scope);
3103 else if (parser->object_scope)
3104 error_at (location, "%<%T::%E%> has not been declared",
3105 parser->object_scope, name);
3106 else
3107 error_at (location, "%qE has not been declared", name);
3108 }
3109 else if (parser->scope && parser->scope != global_namespace)
3110 {
3111 switch (desired)
3112 {
3113 case NLE_TYPE:
3114 error_at (location, "%<%E::%E%> is not a type",
3115 parser->scope, name);
3116 break;
3117 case NLE_CXX98:
3118 error_at (location, "%<%E::%E%> is not a class or namespace",
3119 parser->scope, name);
3120 break;
3121 case NLE_NOT_CXX98:
3122 error_at (location,
3123 "%<%E::%E%> is not a class, namespace, or enumeration",
3124 parser->scope, name);
3125 break;
3126 default:
3127 gcc_unreachable ();
3128
3129 }
3130 }
3131 else if (parser->scope == global_namespace)
3132 {
3133 switch (desired)
3134 {
3135 case NLE_TYPE:
3136 error_at (location, "%<::%E%> is not a type", name);
3137 break;
3138 case NLE_CXX98:
3139 error_at (location, "%<::%E%> is not a class or namespace", name);
3140 break;
3141 case NLE_NOT_CXX98:
3142 error_at (location,
3143 "%<::%E%> is not a class, namespace, or enumeration",
3144 name);
3145 break;
3146 default:
3147 gcc_unreachable ();
3148 }
3149 }
3150 else
3151 {
3152 switch (desired)
3153 {
3154 case NLE_TYPE:
3155 error_at (location, "%qE is not a type", name);
3156 break;
3157 case NLE_CXX98:
3158 error_at (location, "%qE is not a class or namespace", name);
3159 break;
3160 case NLE_NOT_CXX98:
3161 error_at (location,
3162 "%qE is not a class, namespace, or enumeration", name);
3163 break;
3164 default:
3165 gcc_unreachable ();
3166 }
3167 }
3168 }
3169
3170 /* If we are parsing tentatively, remember that an error has occurred
3171 during this tentative parse. Returns true if the error was
3172 simulated; false if a message should be issued by the caller. */
3173
3174 static bool
3175 cp_parser_simulate_error (cp_parser* parser)
3176 {
3177 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3178 {
3179 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3180 return true;
3181 }
3182 return false;
3183 }
3184
3185 /* This function is called when a type is defined. If type
3186 definitions are forbidden at this point, an error message is
3187 issued. */
3188
3189 static bool
3190 cp_parser_check_type_definition (cp_parser* parser)
3191 {
3192 /* If types are forbidden here, issue a message. */
3193 if (parser->type_definition_forbidden_message)
3194 {
3195 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3196 or %qs in the message need to be interpreted. */
3197 error (parser->type_definition_forbidden_message,
3198 parser->type_definition_forbidden_message_arg);
3199 return false;
3200 }
3201 return true;
3202 }
3203
3204 /* This function is called when the DECLARATOR is processed. The TYPE
3205 was a type defined in the decl-specifiers. If it is invalid to
3206 define a type in the decl-specifiers for DECLARATOR, an error is
3207 issued. TYPE_LOCATION is the location of TYPE and is used
3208 for error reporting. */
3209
3210 static void
3211 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3212 tree type, location_t type_location)
3213 {
3214 /* [dcl.fct] forbids type definitions in return types.
3215 Unfortunately, it's not easy to know whether or not we are
3216 processing a return type until after the fact. */
3217 while (declarator
3218 && (declarator->kind == cdk_pointer
3219 || declarator->kind == cdk_reference
3220 || declarator->kind == cdk_ptrmem))
3221 declarator = declarator->declarator;
3222 if (declarator
3223 && declarator->kind == cdk_function)
3224 {
3225 error_at (type_location,
3226 "new types may not be defined in a return type");
3227 inform (type_location,
3228 "(perhaps a semicolon is missing after the definition of %qT)",
3229 type);
3230 }
3231 }
3232
3233 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3234 "<" in any valid C++ program. If the next token is indeed "<",
3235 issue a message warning the user about what appears to be an
3236 invalid attempt to form a template-id. LOCATION is the location
3237 of the type-specifier (TYPE) */
3238
3239 static void
3240 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3241 tree type,
3242 enum tag_types tag_type,
3243 location_t location)
3244 {
3245 cp_token_position start = 0;
3246
3247 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3248 {
3249 if (TREE_CODE (type) == TYPE_DECL)
3250 type = TREE_TYPE (type);
3251 if (TYPE_P (type) && !template_placeholder_p (type))
3252 error_at (location, "%qT is not a template", type);
3253 else if (identifier_p (type))
3254 {
3255 if (tag_type != none_type)
3256 error_at (location, "%qE is not a class template", type);
3257 else
3258 error_at (location, "%qE is not a template", type);
3259 }
3260 else
3261 error_at (location, "invalid template-id");
3262 /* Remember the location of the invalid "<". */
3263 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3264 start = cp_lexer_token_position (parser->lexer, true);
3265 /* Consume the "<". */
3266 cp_lexer_consume_token (parser->lexer);
3267 /* Parse the template arguments. */
3268 cp_parser_enclosed_template_argument_list (parser);
3269 /* Permanently remove the invalid template arguments so that
3270 this error message is not issued again. */
3271 if (start)
3272 cp_lexer_purge_tokens_after (parser->lexer, start);
3273 }
3274 }
3275
3276 /* If parsing an integral constant-expression, issue an error message
3277 about the fact that THING appeared and return true. Otherwise,
3278 return false. In either case, set
3279 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3280
3281 static bool
3282 cp_parser_non_integral_constant_expression (cp_parser *parser,
3283 non_integral_constant thing)
3284 {
3285 parser->non_integral_constant_expression_p = true;
3286 if (parser->integral_constant_expression_p)
3287 {
3288 if (!parser->allow_non_integral_constant_expression_p)
3289 {
3290 const char *msg = NULL;
3291 switch (thing)
3292 {
3293 case NIC_FLOAT:
3294 pedwarn (input_location, OPT_Wpedantic,
3295 "ISO C++ forbids using a floating-point literal "
3296 "in a constant-expression");
3297 return true;
3298 case NIC_CAST:
3299 error ("a cast to a type other than an integral or "
3300 "enumeration type cannot appear in a "
3301 "constant-expression");
3302 return true;
3303 case NIC_TYPEID:
3304 error ("%<typeid%> operator "
3305 "cannot appear in a constant-expression");
3306 return true;
3307 case NIC_NCC:
3308 error ("non-constant compound literals "
3309 "cannot appear in a constant-expression");
3310 return true;
3311 case NIC_FUNC_CALL:
3312 error ("a function call "
3313 "cannot appear in a constant-expression");
3314 return true;
3315 case NIC_INC:
3316 error ("an increment "
3317 "cannot appear in a constant-expression");
3318 return true;
3319 case NIC_DEC:
3320 error ("an decrement "
3321 "cannot appear in a constant-expression");
3322 return true;
3323 case NIC_ARRAY_REF:
3324 error ("an array reference "
3325 "cannot appear in a constant-expression");
3326 return true;
3327 case NIC_ADDR_LABEL:
3328 error ("the address of a label "
3329 "cannot appear in a constant-expression");
3330 return true;
3331 case NIC_OVERLOADED:
3332 error ("calls to overloaded operators "
3333 "cannot appear in a constant-expression");
3334 return true;
3335 case NIC_ASSIGNMENT:
3336 error ("an assignment cannot appear in a constant-expression");
3337 return true;
3338 case NIC_COMMA:
3339 error ("a comma operator "
3340 "cannot appear in a constant-expression");
3341 return true;
3342 case NIC_CONSTRUCTOR:
3343 error ("a call to a constructor "
3344 "cannot appear in a constant-expression");
3345 return true;
3346 case NIC_TRANSACTION:
3347 error ("a transaction expression "
3348 "cannot appear in a constant-expression");
3349 return true;
3350 case NIC_THIS:
3351 msg = "this";
3352 break;
3353 case NIC_FUNC_NAME:
3354 msg = "__FUNCTION__";
3355 break;
3356 case NIC_PRETTY_FUNC:
3357 msg = "__PRETTY_FUNCTION__";
3358 break;
3359 case NIC_C99_FUNC:
3360 msg = "__func__";
3361 break;
3362 case NIC_VA_ARG:
3363 msg = "va_arg";
3364 break;
3365 case NIC_ARROW:
3366 msg = "->";
3367 break;
3368 case NIC_POINT:
3369 msg = ".";
3370 break;
3371 case NIC_STAR:
3372 msg = "*";
3373 break;
3374 case NIC_ADDR:
3375 msg = "&";
3376 break;
3377 case NIC_PREINCREMENT:
3378 msg = "++";
3379 break;
3380 case NIC_PREDECREMENT:
3381 msg = "--";
3382 break;
3383 case NIC_NEW:
3384 msg = "new";
3385 break;
3386 case NIC_DEL:
3387 msg = "delete";
3388 break;
3389 default:
3390 gcc_unreachable ();
3391 }
3392 if (msg)
3393 error ("%qs cannot appear in a constant-expression", msg);
3394 return true;
3395 }
3396 }
3397 return false;
3398 }
3399
3400 /* Emit a diagnostic for an invalid type name. This function commits
3401 to the current active tentative parse, if any. (Otherwise, the
3402 problematic construct might be encountered again later, resulting
3403 in duplicate error messages.) LOCATION is the location of ID. */
3404
3405 static void
3406 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3407 location_t location)
3408 {
3409 tree decl, ambiguous_decls;
3410 cp_parser_commit_to_tentative_parse (parser);
3411 /* Try to lookup the identifier. */
3412 decl = cp_parser_lookup_name (parser, id, none_type,
3413 /*is_template=*/false,
3414 /*is_namespace=*/false,
3415 /*check_dependency=*/true,
3416 &ambiguous_decls, location);
3417 if (ambiguous_decls)
3418 /* If the lookup was ambiguous, an error will already have
3419 been issued. */
3420 return;
3421 /* If the lookup found a template-name, it means that the user forgot
3422 to specify an argument list. Emit a useful error message. */
3423 if (DECL_TYPE_TEMPLATE_P (decl))
3424 {
3425 auto_diagnostic_group d;
3426 error_at (location,
3427 "invalid use of template-name %qE without an argument list",
3428 decl);
3429 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3430 inform (location, "class template argument deduction is only available "
3431 "with %<-std=c++17%> or %<-std=gnu++17%>");
3432 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3433 }
3434 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3435 error_at (location, "invalid use of destructor %qD as a type", id);
3436 else if (TREE_CODE (decl) == TYPE_DECL)
3437 /* Something like 'unsigned A a;' */
3438 error_at (location, "invalid combination of multiple type-specifiers");
3439 else if (!parser->scope)
3440 {
3441 /* Issue an error message. */
3442 auto_diagnostic_group d;
3443 name_hint hint;
3444 if (TREE_CODE (id) == IDENTIFIER_NODE)
3445 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3446 if (const char *suggestion = hint.suggestion ())
3447 {
3448 gcc_rich_location richloc (location);
3449 richloc.add_fixit_replace (suggestion);
3450 error_at (&richloc,
3451 "%qE does not name a type; did you mean %qs?",
3452 id, suggestion);
3453 }
3454 else
3455 error_at (location, "%qE does not name a type", id);
3456 /* If we're in a template class, it's possible that the user was
3457 referring to a type from a base class. For example:
3458
3459 template <typename T> struct A { typedef T X; };
3460 template <typename T> struct B : public A<T> { X x; };
3461
3462 The user should have said "typename A<T>::X". */
3463 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3464 inform (location, "C++11 %<constexpr%> only available with "
3465 "%<-std=c++11%> or %<-std=gnu++11%>");
3466 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3467 inform (location, "C++11 %<noexcept%> only available with "
3468 "%<-std=c++11%> or %<-std=gnu++11%>");
3469 else if (TREE_CODE (id) == IDENTIFIER_NODE
3470 && (id_equal (id, "module") || id_equal (id, "import")))
3471 {
3472 if (!modules_p ())
3473 inform (location, "%qE only available with %<-fmodules-ts%>", id);
3474 else
3475 inform (location, "%qE was not recognized as a module control-line",
3476 id);
3477 }
3478 else if (cxx_dialect < cxx11
3479 && TREE_CODE (id) == IDENTIFIER_NODE
3480 && id_equal (id, "thread_local"))
3481 inform (location, "C++11 %<thread_local%> only available with "
3482 "%<-std=c++11%> or %<-std=gnu++11%>");
3483 else if (cxx_dialect < cxx20 && id == ridpointers[(int)RID_CONSTINIT])
3484 inform (location, "C++20 %<constinit%> only available with "
3485 "%<-std=c++20%> or %<-std=gnu++20%>");
3486 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3487 inform (location, "%<concept%> only available with %<-std=c++20%> or "
3488 "%<-fconcepts%>");
3489 else if (!flag_concepts && id == ridpointers[(int)RID_REQUIRES])
3490 inform (location, "%<requires%> only available with %<-std=c++20%> or "
3491 "%<-fconcepts%>");
3492 else if (processing_template_decl && current_class_type
3493 && TYPE_BINFO (current_class_type))
3494 {
3495 for (tree b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3496 b; b = TREE_CHAIN (b))
3497 {
3498 tree base_type = BINFO_TYPE (b);
3499 if (CLASS_TYPE_P (base_type)
3500 && dependent_type_p (base_type))
3501 {
3502 /* Go from a particular instantiation of the
3503 template (which will have an empty TYPE_FIELDs),
3504 to the main version. */
3505 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3506 for (tree field = TYPE_FIELDS (base_type);
3507 field; field = DECL_CHAIN (field))
3508 if (TREE_CODE (field) == TYPE_DECL
3509 && DECL_NAME (field) == id)
3510 {
3511 inform (location,
3512 "(perhaps %<typename %T::%E%> was intended)",
3513 BINFO_TYPE (b), id);
3514 goto found;
3515 }
3516 }
3517 }
3518 found:;
3519 }
3520 }
3521 /* Here we diagnose qualified-ids where the scope is actually correct,
3522 but the identifier does not resolve to a valid type name. */
3523 else if (parser->scope != error_mark_node)
3524 {
3525 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3526 {
3527 auto_diagnostic_group d;
3528 name_hint hint;
3529 if (decl == error_mark_node)
3530 hint = suggest_alternative_in_explicit_scope (location, id,
3531 parser->scope);
3532 const char *suggestion = hint.suggestion ();
3533 gcc_rich_location richloc (location_of (id));
3534 if (suggestion)
3535 richloc.add_fixit_replace (suggestion);
3536 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3537 {
3538 if (suggestion)
3539 error_at (&richloc,
3540 "%qE in namespace %qE does not name a template"
3541 " type; did you mean %qs?",
3542 id, parser->scope, suggestion);
3543 else
3544 error_at (&richloc,
3545 "%qE in namespace %qE does not name a template type",
3546 id, parser->scope);
3547 }
3548 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3549 {
3550 if (suggestion)
3551 error_at (&richloc,
3552 "%qE in namespace %qE does not name a template"
3553 " type; did you mean %qs?",
3554 TREE_OPERAND (id, 0), parser->scope, suggestion);
3555 else
3556 error_at (&richloc,
3557 "%qE in namespace %qE does not name a template"
3558 " type",
3559 TREE_OPERAND (id, 0), parser->scope);
3560 }
3561 else
3562 {
3563 if (suggestion)
3564 error_at (&richloc,
3565 "%qE in namespace %qE does not name a type"
3566 "; did you mean %qs?",
3567 id, parser->scope, suggestion);
3568 else
3569 error_at (&richloc,
3570 "%qE in namespace %qE does not name a type",
3571 id, parser->scope);
3572 }
3573 if (DECL_P (decl))
3574 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3575 }
3576 else if (CLASS_TYPE_P (parser->scope)
3577 && constructor_name_p (id, parser->scope))
3578 {
3579 /* A<T>::A<T>() */
3580 auto_diagnostic_group d;
3581 error_at (location, "%<%T::%E%> names the constructor, not"
3582 " the type", parser->scope, id);
3583 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3584 error_at (location, "and %qT has no template constructors",
3585 parser->scope);
3586 }
3587 else if (TYPE_P (parser->scope)
3588 && dependent_scope_p (parser->scope))
3589 {
3590 gcc_rich_location richloc (location);
3591 richloc.add_fixit_insert_before ("typename ");
3592 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3593 error_at (&richloc,
3594 "need %<typename%> before %<%T::%D::%E%> because "
3595 "%<%T::%D%> is a dependent scope",
3596 TYPE_CONTEXT (parser->scope),
3597 TYPENAME_TYPE_FULLNAME (parser->scope),
3598 id,
3599 TYPE_CONTEXT (parser->scope),
3600 TYPENAME_TYPE_FULLNAME (parser->scope));
3601 else
3602 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3603 "%qT is a dependent scope",
3604 parser->scope, id, parser->scope);
3605 }
3606 else if (TYPE_P (parser->scope))
3607 {
3608 auto_diagnostic_group d;
3609 if (!COMPLETE_TYPE_P (parser->scope))
3610 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3611 parser->scope);
3612 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3613 error_at (location_of (id),
3614 "%qE in %q#T does not name a template type",
3615 id, parser->scope);
3616 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3617 error_at (location_of (id),
3618 "%qE in %q#T does not name a template type",
3619 TREE_OPERAND (id, 0), parser->scope);
3620 else
3621 error_at (location_of (id),
3622 "%qE in %q#T does not name a type",
3623 id, parser->scope);
3624 if (DECL_P (decl))
3625 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3626 }
3627 else
3628 gcc_unreachable ();
3629 }
3630 }
3631
3632 /* Check for a common situation where a type-name should be present,
3633 but is not, and issue a sensible error message. Returns true if an
3634 invalid type-name was detected.
3635
3636 The situation handled by this function are variable declarations of the
3637 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3638 Usually, `ID' should name a type, but if we got here it means that it
3639 does not. We try to emit the best possible error message depending on
3640 how exactly the id-expression looks like. */
3641
3642 static bool
3643 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3644 {
3645 tree id;
3646 cp_token *token = cp_lexer_peek_token (parser->lexer);
3647
3648 /* Avoid duplicate error about ambiguous lookup. */
3649 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3650 {
3651 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3652 if (next->type == CPP_NAME && next->error_reported)
3653 goto out;
3654 }
3655
3656 cp_parser_parse_tentatively (parser);
3657 id = cp_parser_id_expression (parser,
3658 /*template_keyword_p=*/false,
3659 /*check_dependency_p=*/true,
3660 /*template_p=*/NULL,
3661 /*declarator_p=*/false,
3662 /*optional_p=*/false);
3663 /* If the next token is a (, this is a function with no explicit return
3664 type, i.e. constructor, destructor or conversion op. */
3665 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3666 || TREE_CODE (id) == TYPE_DECL)
3667 {
3668 cp_parser_abort_tentative_parse (parser);
3669 return false;
3670 }
3671 if (!cp_parser_parse_definitely (parser))
3672 return false;
3673
3674 /* Emit a diagnostic for the invalid type. */
3675 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3676 out:
3677 /* If we aren't in the middle of a declarator (i.e. in a
3678 parameter-declaration-clause), skip to the end of the declaration;
3679 there's no point in trying to process it. */
3680 if (!parser->in_declarator_p)
3681 cp_parser_skip_to_end_of_block_or_statement (parser);
3682 return true;
3683 }
3684
3685 /* Consume tokens up to, and including, the next non-nested closing `)'.
3686 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3687 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3688 found an unnested token of that type. */
3689
3690 static int
3691 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3692 bool recovering,
3693 cpp_ttype or_ttype,
3694 bool consume_paren)
3695 {
3696 unsigned paren_depth = 0;
3697 unsigned brace_depth = 0;
3698 unsigned square_depth = 0;
3699 unsigned condop_depth = 0;
3700
3701 if (recovering && or_ttype == CPP_EOF
3702 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3703 return 0;
3704
3705 while (true)
3706 {
3707 cp_token * token = cp_lexer_peek_token (parser->lexer);
3708
3709 /* Have we found what we're looking for before the closing paren? */
3710 if (token->type == or_ttype && or_ttype != CPP_EOF
3711 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3712 return -1;
3713
3714 switch (token->type)
3715 {
3716 case CPP_PRAGMA_EOL:
3717 if (!parser->lexer->in_pragma)
3718 break;
3719 /* FALLTHRU */
3720 case CPP_EOF:
3721 /* If we've run out of tokens, then there is no closing `)'. */
3722 return 0;
3723
3724 /* This is good for lambda expression capture-lists. */
3725 case CPP_OPEN_SQUARE:
3726 ++square_depth;
3727 break;
3728 case CPP_CLOSE_SQUARE:
3729 if (!square_depth--)
3730 return 0;
3731 break;
3732
3733 case CPP_SEMICOLON:
3734 /* This matches the processing in skip_to_end_of_statement. */
3735 if (!brace_depth)
3736 return 0;
3737 break;
3738
3739 case CPP_OPEN_BRACE:
3740 ++brace_depth;
3741 break;
3742 case CPP_CLOSE_BRACE:
3743 if (!brace_depth--)
3744 return 0;
3745 break;
3746
3747 case CPP_OPEN_PAREN:
3748 if (!brace_depth)
3749 ++paren_depth;
3750 break;
3751
3752 case CPP_CLOSE_PAREN:
3753 if (!brace_depth && !paren_depth--)
3754 {
3755 if (consume_paren)
3756 cp_lexer_consume_token (parser->lexer);
3757 return 1;
3758 }
3759 break;
3760
3761 case CPP_QUERY:
3762 if (!brace_depth && !paren_depth && !square_depth)
3763 ++condop_depth;
3764 break;
3765
3766 case CPP_COLON:
3767 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3768 condop_depth--;
3769 break;
3770
3771 case CPP_KEYWORD:
3772 if (token->keyword != RID__EXPORT
3773 && token->keyword != RID__MODULE
3774 && token->keyword != RID__IMPORT)
3775 break;
3776 /* FALLTHROUGH */
3777
3778 case CPP_PRAGMA:
3779 /* We fell into a pragma. Skip it, and continue. */
3780 cp_parser_skip_to_pragma_eol (parser, recovering ? token : nullptr);
3781 continue;
3782
3783 default:
3784 break;
3785 }
3786
3787 /* Consume the token. */
3788 cp_lexer_consume_token (parser->lexer);
3789 }
3790 }
3791
3792 /* Consume tokens up to, and including, the next non-nested closing `)'.
3793 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3794 are doing error recovery. Returns -1 if OR_COMMA is true and we
3795 found an unnested token of that type. */
3796
3797 static int
3798 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3799 bool recovering,
3800 bool or_comma,
3801 bool consume_paren)
3802 {
3803 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3804 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3805 ttype, consume_paren);
3806 }
3807
3808 /* Consume tokens until we reach the end of the current statement.
3809 Normally, that will be just before consuming a `;'. However, if a
3810 non-nested `}' comes first, then we stop before consuming that. */
3811
3812 static void
3813 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3814 {
3815 unsigned nesting_depth = 0;
3816
3817 /* Unwind generic function template scope if necessary. */
3818 if (parser->fully_implicit_function_template_p)
3819 abort_fully_implicit_template (parser);
3820
3821 while (true)
3822 {
3823 cp_token *token = cp_lexer_peek_token (parser->lexer);
3824
3825 switch (token->type)
3826 {
3827 case CPP_PRAGMA_EOL:
3828 if (!parser->lexer->in_pragma)
3829 break;
3830 /* FALLTHRU */
3831 case CPP_EOF:
3832 /* If we've run out of tokens, stop. */
3833 return;
3834
3835 case CPP_SEMICOLON:
3836 /* If the next token is a `;', we have reached the end of the
3837 statement. */
3838 if (!nesting_depth)
3839 return;
3840 break;
3841
3842 case CPP_CLOSE_BRACE:
3843 /* If this is a non-nested '}', stop before consuming it.
3844 That way, when confronted with something like:
3845
3846 { 3 + }
3847
3848 we stop before consuming the closing '}', even though we
3849 have not yet reached a `;'. */
3850 if (nesting_depth == 0)
3851 return;
3852
3853 /* If it is the closing '}' for a block that we have
3854 scanned, stop -- but only after consuming the token.
3855 That way given:
3856
3857 void f g () { ... }
3858 typedef int I;
3859
3860 we will stop after the body of the erroneously declared
3861 function, but before consuming the following `typedef'
3862 declaration. */
3863 if (--nesting_depth == 0)
3864 {
3865 cp_lexer_consume_token (parser->lexer);
3866 return;
3867 }
3868 break;
3869
3870 case CPP_OPEN_BRACE:
3871 ++nesting_depth;
3872 break;
3873
3874 case CPP_KEYWORD:
3875 if (token->keyword != RID__EXPORT
3876 && token->keyword != RID__MODULE
3877 && token->keyword != RID__IMPORT)
3878 break;
3879 /* FALLTHROUGH */
3880
3881 case CPP_PRAGMA:
3882 /* We fell into a pragma. Skip it, and continue or return. */
3883 cp_parser_skip_to_pragma_eol (parser, token);
3884 if (!nesting_depth)
3885 return;
3886 continue;
3887
3888 default:
3889 break;
3890 }
3891
3892 /* Consume the token. */
3893 cp_lexer_consume_token (parser->lexer);
3894 }
3895 }
3896
3897 /* This function is called at the end of a statement or declaration.
3898 If the next token is a semicolon, it is consumed; otherwise, error
3899 recovery is attempted. */
3900
3901 static void
3902 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3903 {
3904 /* Look for the trailing `;'. */
3905 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3906 {
3907 /* If there is additional (erroneous) input, skip to the end of
3908 the statement. */
3909 cp_parser_skip_to_end_of_statement (parser);
3910 /* If the next token is now a `;', consume it. */
3911 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3912 cp_lexer_consume_token (parser->lexer);
3913 }
3914 }
3915
3916 /* Skip tokens until we have consumed an entire block, or until we
3917 have consumed a non-nested `;'. */
3918
3919 static void
3920 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3921 {
3922 int nesting_depth = 0;
3923
3924 /* Unwind generic function template scope if necessary. */
3925 if (parser->fully_implicit_function_template_p)
3926 abort_fully_implicit_template (parser);
3927
3928 while (nesting_depth >= 0)
3929 {
3930 cp_token *token = cp_lexer_peek_token (parser->lexer);
3931
3932 switch (token->type)
3933 {
3934 case CPP_PRAGMA_EOL:
3935 if (!parser->lexer->in_pragma)
3936 break;
3937 /* FALLTHRU */
3938 case CPP_EOF:
3939 /* If we've run out of tokens, stop. */
3940 return;
3941
3942 case CPP_SEMICOLON:
3943 /* Stop if this is an unnested ';'. */
3944 if (!nesting_depth)
3945 nesting_depth = -1;
3946 break;
3947
3948 case CPP_CLOSE_BRACE:
3949 /* Stop if this is an unnested '}', or closes the outermost
3950 nesting level. */
3951 nesting_depth--;
3952 if (nesting_depth < 0)
3953 return;
3954 if (!nesting_depth)
3955 nesting_depth = -1;
3956 break;
3957
3958 case CPP_OPEN_BRACE:
3959 /* Nest. */
3960 nesting_depth++;
3961 break;
3962
3963 case CPP_KEYWORD:
3964 if (token->keyword != RID__EXPORT
3965 && token->keyword != RID__MODULE
3966 && token->keyword != RID__IMPORT)
3967 break;
3968 /* FALLTHROUGH */
3969
3970 case CPP_PRAGMA:
3971 /* Skip it, and continue or return. */
3972 cp_parser_skip_to_pragma_eol (parser, token);
3973 if (!nesting_depth)
3974 return;
3975 continue;
3976
3977 default:
3978 break;
3979 }
3980
3981 /* Consume the token. */
3982 cp_lexer_consume_token (parser->lexer);
3983 }
3984 }
3985
3986 /* Skip tokens until a non-nested closing curly brace is the next
3987 token, or there are no more tokens. Return true in the first case,
3988 false otherwise. */
3989
3990 static bool
3991 cp_parser_skip_to_closing_brace (cp_parser *parser)
3992 {
3993 unsigned nesting_depth = 0;
3994
3995 while (true)
3996 {
3997 cp_token *token = cp_lexer_peek_token (parser->lexer);
3998
3999 switch (token->type)
4000 {
4001 case CPP_PRAGMA_EOL:
4002 if (!parser->lexer->in_pragma)
4003 break;
4004 /* FALLTHRU */
4005 case CPP_EOF:
4006 /* If we've run out of tokens, stop. */
4007 return false;
4008
4009 case CPP_CLOSE_BRACE:
4010 /* If the next token is a non-nested `}', then we have reached
4011 the end of the current block. */
4012 if (nesting_depth-- == 0)
4013 return true;
4014 break;
4015
4016 case CPP_OPEN_BRACE:
4017 /* If it the next token is a `{', then we are entering a new
4018 block. Consume the entire block. */
4019 ++nesting_depth;
4020 break;
4021
4022 default:
4023 break;
4024 }
4025
4026 /* Consume the token. */
4027 cp_lexer_consume_token (parser->lexer);
4028 }
4029 }
4030
4031 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
4032 parameter is the PRAGMA token, allowing us to purge the entire pragma
4033 sequence. PRAGMA_TOK can be NULL, if we're speculatively scanning
4034 forwards (not error recovery). */
4035
4036 static void
4037 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
4038 {
4039 cp_token *token;
4040
4041 do
4042 {
4043 /* The preprocessor makes sure that a PRAGMA_EOL token appears
4044 before an EOF token, even when the EOF is on the pragma line.
4045 We should never get here without being inside a deferred
4046 pragma. */
4047 gcc_checking_assert (cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
4048 token = cp_lexer_consume_token (parser->lexer);
4049 }
4050 while (token->type != CPP_PRAGMA_EOL);
4051
4052 if (pragma_tok)
4053 {
4054 /* Ensure that the pragma is not parsed again. */
4055 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
4056 parser->lexer->in_pragma = false;
4057 }
4058 }
4059
4060 /* Require pragma end of line, resyncing with it as necessary. The
4061 arguments are as for cp_parser_skip_to_pragma_eol. */
4062
4063 static void
4064 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
4065 {
4066 parser->lexer->in_pragma = false;
4067 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
4068 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
4069 }
4070
4071 /* This is a simple wrapper around make_typename_type. When the id is
4072 an unresolved identifier node, we can provide a superior diagnostic
4073 using cp_parser_diagnose_invalid_type_name. */
4074
4075 static tree
4076 cp_parser_make_typename_type (cp_parser *parser, tree id,
4077 location_t id_location)
4078 {
4079 tree result;
4080 if (identifier_p (id))
4081 {
4082 result = make_typename_type (parser->scope, id, typename_type,
4083 /*complain=*/tf_none);
4084 if (result == error_mark_node)
4085 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
4086 return result;
4087 }
4088 return make_typename_type (parser->scope, id, typename_type, tf_error);
4089 }
4090
4091 /* This is a wrapper around the
4092 make_{pointer,ptrmem,reference}_declarator functions that decides
4093 which one to call based on the CODE and CLASS_TYPE arguments. The
4094 CODE argument should be one of the values returned by
4095 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
4096 appertain to the pointer or reference. */
4097
4098 static cp_declarator *
4099 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
4100 cp_cv_quals cv_qualifiers,
4101 cp_declarator *target,
4102 tree attributes)
4103 {
4104 if (code == ERROR_MARK || target == cp_error_declarator)
4105 return cp_error_declarator;
4106
4107 if (code == INDIRECT_REF)
4108 if (class_type == NULL_TREE)
4109 return make_pointer_declarator (cv_qualifiers, target, attributes);
4110 else
4111 return make_ptrmem_declarator (cv_qualifiers, class_type,
4112 target, attributes);
4113 else if (code == ADDR_EXPR && class_type == NULL_TREE)
4114 return make_reference_declarator (cv_qualifiers, target,
4115 false, attributes);
4116 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
4117 return make_reference_declarator (cv_qualifiers, target,
4118 true, attributes);
4119 gcc_unreachable ();
4120 }
4121
4122 /* Create a new C++ parser. */
4123
4124 static cp_parser *
4125 cp_parser_new (cp_lexer *lexer)
4126 {
4127 /* Initialize the binops_by_token so that we can get the tree
4128 directly from the token. */
4129 for (unsigned i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
4130 binops_by_token[binops[i].token_type] = binops[i];
4131
4132 cp_parser *parser = ggc_cleared_alloc<cp_parser> ();
4133 parser->lexer = lexer;
4134 parser->context = cp_parser_context_new (NULL);
4135
4136 /* For now, we always accept GNU extensions. */
4137 parser->allow_gnu_extensions_p = 1;
4138
4139 /* The `>' token is a greater-than operator, not the end of a
4140 template-id. */
4141 parser->greater_than_is_operator_p = true;
4142
4143 parser->default_arg_ok_p = true;
4144
4145 /* We are not parsing a constant-expression. */
4146 parser->integral_constant_expression_p = false;
4147 parser->allow_non_integral_constant_expression_p = false;
4148 parser->non_integral_constant_expression_p = false;
4149
4150 /* Local variable names are not forbidden. */
4151 parser->local_variables_forbidden_p = 0;
4152
4153 /* We are not processing an `extern "C"' declaration. */
4154 parser->in_unbraced_linkage_specification_p = false;
4155
4156 /* We are not processing a declarator. */
4157 parser->in_declarator_p = false;
4158
4159 /* We are not processing a template-argument-list. */
4160 parser->in_template_argument_list_p = false;
4161
4162 /* We are not in an iteration statement. */
4163 parser->in_statement = 0;
4164
4165 /* We are not in a switch statement. */
4166 parser->in_switch_statement_p = false;
4167
4168 /* We are not parsing a type-id inside an expression. */
4169 parser->in_type_id_in_expr_p = false;
4170
4171 /* String literals should be translated to the execution character set. */
4172 parser->translate_strings_p = true;
4173
4174 /* We are not parsing a function body. */
4175 parser->in_function_body = false;
4176
4177 /* We can correct until told otherwise. */
4178 parser->colon_corrects_to_scope_p = true;
4179
4180 /* The unparsed function queue is empty. */
4181 push_unparsed_function_queues (parser);
4182
4183 /* There are no classes being defined. */
4184 parser->num_classes_being_defined = 0;
4185
4186 /* No template parameters apply. */
4187 parser->num_template_parameter_lists = 0;
4188
4189 /* Special parsing data structures. */
4190 parser->omp_declare_simd = NULL;
4191 parser->oacc_routine = NULL;
4192
4193 /* Not declaring an implicit function template. */
4194 parser->auto_is_implicit_function_template_parm_p = false;
4195 parser->fully_implicit_function_template_p = false;
4196 parser->implicit_template_parms = 0;
4197 parser->implicit_template_scope = 0;
4198
4199 /* Allow constrained-type-specifiers. */
4200 parser->prevent_constrained_type_specifiers = 0;
4201
4202 /* We haven't yet seen an 'extern "C"'. */
4203 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4204
4205 return parser;
4206 }
4207
4208 /* Create a cp_lexer structure which will emit the tokens in CACHE
4209 and push it onto the parser's lexer stack. This is used for delayed
4210 parsing of in-class method bodies and default arguments, and should
4211 not be confused with tentative parsing. */
4212 static void
4213 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4214 {
4215 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4216 lexer->next = parser->lexer;
4217 parser->lexer = lexer;
4218
4219 /* Move the current source position to that of the first token in the
4220 new lexer. */
4221 cp_lexer_set_source_position_from_token (lexer->next_token);
4222 }
4223
4224 /* Pop the top lexer off the parser stack. This is never used for the
4225 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4226 static void
4227 cp_parser_pop_lexer (cp_parser *parser)
4228 {
4229 cp_lexer *lexer = parser->lexer;
4230 parser->lexer = lexer->next;
4231 cp_lexer_destroy (lexer);
4232
4233 /* Put the current source position back where it was before this
4234 lexer was pushed. */
4235 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4236 }
4237
4238 /* Lexical conventions [gram.lex] */
4239
4240 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4241 identifier. */
4242
4243 static cp_expr
4244 cp_parser_identifier (cp_parser* parser)
4245 {
4246 cp_token *token;
4247
4248 /* Look for the identifier. */
4249 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4250 /* Return the value. */
4251 if (token)
4252 return cp_expr (token->u.value, token->location);
4253 else
4254 return error_mark_node;
4255 }
4256
4257 /* Parse a sequence of adjacent string constants. Returns a
4258 TREE_STRING representing the combined, nul-terminated string
4259 constant. If TRANSLATE is true, translate the string to the
4260 execution character set. If WIDE_OK is true, a wide string is
4261 invalid here.
4262
4263 C++98 [lex.string] says that if a narrow string literal token is
4264 adjacent to a wide string literal token, the behavior is undefined.
4265 However, C99 6.4.5p4 says that this results in a wide string literal.
4266 We follow C99 here, for consistency with the C front end.
4267
4268 This code is largely lifted from lex_string() in c-lex.c.
4269
4270 FUTURE: ObjC++ will need to handle @-strings here. */
4271 static cp_expr
4272 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4273 bool lookup_udlit = true)
4274 {
4275 tree value;
4276 size_t count;
4277 struct obstack str_ob;
4278 struct obstack loc_ob;
4279 cpp_string str, istr, *strs;
4280 cp_token *tok;
4281 enum cpp_ttype type, curr_type;
4282 int have_suffix_p = 0;
4283 tree string_tree;
4284 tree suffix_id = NULL_TREE;
4285 bool curr_tok_is_userdef_p = false;
4286
4287 tok = cp_lexer_peek_token (parser->lexer);
4288 if (!cp_parser_is_string_literal (tok))
4289 {
4290 cp_parser_error (parser, "expected string-literal");
4291 return error_mark_node;
4292 }
4293
4294 location_t loc = tok->location;
4295
4296 if (cpp_userdef_string_p (tok->type))
4297 {
4298 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4299 curr_type = cpp_userdef_string_remove_type (tok->type);
4300 curr_tok_is_userdef_p = true;
4301 }
4302 else
4303 {
4304 string_tree = tok->u.value;
4305 curr_type = tok->type;
4306 }
4307 type = curr_type;
4308
4309 /* Try to avoid the overhead of creating and destroying an obstack
4310 for the common case of just one string. */
4311 if (!cp_parser_is_string_literal
4312 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4313 {
4314 cp_lexer_consume_token (parser->lexer);
4315
4316 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4317 str.len = TREE_STRING_LENGTH (string_tree);
4318 count = 1;
4319
4320 if (curr_tok_is_userdef_p)
4321 {
4322 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4323 have_suffix_p = 1;
4324 curr_type = cpp_userdef_string_remove_type (tok->type);
4325 }
4326 else
4327 curr_type = tok->type;
4328
4329 strs = &str;
4330 }
4331 else
4332 {
4333 location_t last_tok_loc = tok->location;
4334 gcc_obstack_init (&str_ob);
4335 gcc_obstack_init (&loc_ob);
4336 count = 0;
4337
4338 do
4339 {
4340 cp_lexer_consume_token (parser->lexer);
4341 count++;
4342 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4343 str.len = TREE_STRING_LENGTH (string_tree);
4344
4345 if (curr_tok_is_userdef_p)
4346 {
4347 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4348 if (have_suffix_p == 0)
4349 {
4350 suffix_id = curr_suffix_id;
4351 have_suffix_p = 1;
4352 }
4353 else if (have_suffix_p == 1
4354 && curr_suffix_id != suffix_id)
4355 {
4356 error ("inconsistent user-defined literal suffixes"
4357 " %qD and %qD in string literal",
4358 suffix_id, curr_suffix_id);
4359 have_suffix_p = -1;
4360 }
4361 curr_type = cpp_userdef_string_remove_type (tok->type);
4362 }
4363 else
4364 curr_type = tok->type;
4365
4366 if (type != curr_type)
4367 {
4368 if (type == CPP_STRING)
4369 type = curr_type;
4370 else if (curr_type != CPP_STRING)
4371 {
4372 rich_location rich_loc (line_table, tok->location);
4373 rich_loc.add_range (last_tok_loc);
4374 error_at (&rich_loc,
4375 "unsupported non-standard concatenation "
4376 "of string literals");
4377 }
4378 }
4379
4380 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4381 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4382
4383 last_tok_loc = tok->location;
4384
4385 tok = cp_lexer_peek_token (parser->lexer);
4386 if (cpp_userdef_string_p (tok->type))
4387 {
4388 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4389 curr_type = cpp_userdef_string_remove_type (tok->type);
4390 curr_tok_is_userdef_p = true;
4391 }
4392 else
4393 {
4394 string_tree = tok->u.value;
4395 curr_type = tok->type;
4396 curr_tok_is_userdef_p = false;
4397 }
4398 }
4399 while (cp_parser_is_string_literal (tok));
4400
4401 /* A string literal built by concatenation has its caret=start at
4402 the start of the initial string, and its finish at the finish of
4403 the final string literal. */
4404 loc = make_location (loc, loc, get_finish (last_tok_loc));
4405
4406 strs = (cpp_string *) obstack_finish (&str_ob);
4407 }
4408
4409 if (type != CPP_STRING && !wide_ok)
4410 {
4411 cp_parser_error (parser, "a wide string is invalid in this context");
4412 type = CPP_STRING;
4413 }
4414
4415 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4416 (parse_in, strs, count, &istr, type))
4417 {
4418 value = build_string (istr.len, (const char *)istr.text);
4419 free (CONST_CAST (unsigned char *, istr.text));
4420 if (count > 1)
4421 {
4422 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4423 gcc_assert (g_string_concat_db);
4424 g_string_concat_db->record_string_concatenation (count, locs);
4425 }
4426
4427 switch (type)
4428 {
4429 default:
4430 case CPP_STRING:
4431 TREE_TYPE (value) = char_array_type_node;
4432 break;
4433 case CPP_UTF8STRING:
4434 if (flag_char8_t)
4435 TREE_TYPE (value) = char8_array_type_node;
4436 else
4437 TREE_TYPE (value) = char_array_type_node;
4438 break;
4439 case CPP_STRING16:
4440 TREE_TYPE (value) = char16_array_type_node;
4441 break;
4442 case CPP_STRING32:
4443 TREE_TYPE (value) = char32_array_type_node;
4444 break;
4445 case CPP_WSTRING:
4446 TREE_TYPE (value) = wchar_array_type_node;
4447 break;
4448 }
4449
4450 value = fix_string_type (value);
4451
4452 if (have_suffix_p)
4453 {
4454 tree literal = build_userdef_literal (suffix_id, value,
4455 OT_NONE, NULL_TREE);
4456 if (lookup_udlit)
4457 value = cp_parser_userdef_string_literal (literal);
4458 else
4459 value = literal;
4460 }
4461 }
4462 else
4463 /* cpp_interpret_string has issued an error. */
4464 value = error_mark_node;
4465
4466 if (count > 1)
4467 {
4468 obstack_free (&str_ob, 0);
4469 obstack_free (&loc_ob, 0);
4470 }
4471
4472 return cp_expr (value, loc);
4473 }
4474
4475 /* Look up a literal operator with the name and the exact arguments. */
4476
4477 static tree
4478 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4479 {
4480 tree decl = lookup_name (name);
4481 if (!decl || !is_overloaded_fn (decl))
4482 return error_mark_node;
4483
4484 for (lkp_iterator iter (decl); iter; ++iter)
4485 {
4486 tree fn = *iter;
4487
4488 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4489 {
4490 unsigned int ix;
4491 bool found = true;
4492
4493 for (ix = 0;
4494 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4495 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4496 {
4497 tree tparm = TREE_VALUE (parmtypes);
4498 tree targ = TREE_TYPE ((*args)[ix]);
4499 bool ptr = TYPE_PTR_P (tparm);
4500 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4501 if ((ptr || arr || !same_type_p (tparm, targ))
4502 && (!ptr || !arr
4503 || !same_type_p (TREE_TYPE (tparm),
4504 TREE_TYPE (targ))))
4505 found = false;
4506 }
4507
4508 if (found
4509 && ix == vec_safe_length (args)
4510 /* May be this should be sufficient_parms_p instead,
4511 depending on how exactly should user-defined literals
4512 work in presence of default arguments on the literal
4513 operator parameters. */
4514 && parmtypes == void_list_node)
4515 return decl;
4516 }
4517 }
4518
4519 return error_mark_node;
4520 }
4521
4522 /* Parse a user-defined char constant. Returns a call to a user-defined
4523 literal operator taking the character as an argument. */
4524
4525 static cp_expr
4526 cp_parser_userdef_char_literal (cp_parser *parser)
4527 {
4528 cp_token *token = cp_lexer_consume_token (parser->lexer);
4529 tree literal = token->u.value;
4530 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4531 tree value = USERDEF_LITERAL_VALUE (literal);
4532 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4533 tree decl, result;
4534
4535 /* Build up a call to the user-defined operator */
4536 /* Lookup the name we got back from the id-expression. */
4537 releasing_vec args;
4538 vec_safe_push (args, value);
4539 decl = lookup_literal_operator (name, args);
4540 if (!decl || decl == error_mark_node)
4541 {
4542 error ("unable to find character literal operator %qD with %qT argument",
4543 name, TREE_TYPE (value));
4544 return error_mark_node;
4545 }
4546 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4547 return result;
4548 }
4549
4550 /* A subroutine of cp_parser_userdef_numeric_literal to
4551 create a char... template parameter pack from a string node. */
4552
4553 static tree
4554 make_char_string_pack (tree value)
4555 {
4556 tree charvec;
4557 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4558 const char *str = TREE_STRING_POINTER (value);
4559 int i, len = TREE_STRING_LENGTH (value) - 1;
4560 tree argvec = make_tree_vec (1);
4561
4562 /* Fill in CHARVEC with all of the parameters. */
4563 charvec = make_tree_vec (len);
4564 for (i = 0; i < len; ++i)
4565 {
4566 unsigned char s[3] = { '\'', str[i], '\'' };
4567 cpp_string in = { 3, s };
4568 cpp_string out = { 0, 0 };
4569 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4570 return NULL_TREE;
4571 gcc_assert (out.len == 2);
4572 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4573 out.text[0]);
4574 }
4575
4576 /* Build the argument packs. */
4577 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4578
4579 TREE_VEC_ELT (argvec, 0) = argpack;
4580
4581 return argvec;
4582 }
4583
4584 /* A subroutine of cp_parser_userdef_numeric_literal to
4585 create a char... template parameter pack from a string node. */
4586
4587 static tree
4588 make_string_pack (tree value)
4589 {
4590 tree charvec;
4591 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4592 const unsigned char *str
4593 = (const unsigned char *) TREE_STRING_POINTER (value);
4594 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4595 int len = TREE_STRING_LENGTH (value) / sz - 1;
4596 tree argvec = make_tree_vec (2);
4597
4598 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4599 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4600
4601 /* First template parm is character type. */
4602 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4603
4604 /* Fill in CHARVEC with all of the parameters. */
4605 charvec = make_tree_vec (len);
4606 for (int i = 0; i < len; ++i)
4607 TREE_VEC_ELT (charvec, i)
4608 = double_int_to_tree (str_char_type_node,
4609 double_int::from_buffer (str + i * sz, sz));
4610
4611 /* Build the argument packs. */
4612 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4613
4614 TREE_VEC_ELT (argvec, 1) = argpack;
4615
4616 return argvec;
4617 }
4618
4619 /* Parse a user-defined numeric constant. returns a call to a user-defined
4620 literal operator. */
4621
4622 static cp_expr
4623 cp_parser_userdef_numeric_literal (cp_parser *parser)
4624 {
4625 cp_token *token = cp_lexer_consume_token (parser->lexer);
4626 tree literal = token->u.value;
4627 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4628 tree value = USERDEF_LITERAL_VALUE (literal);
4629 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4630 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4631 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4632 tree decl, result;
4633
4634 /* Look for a literal operator taking the exact type of numeric argument
4635 as the literal value. */
4636 releasing_vec args;
4637 vec_safe_push (args, value);
4638 decl = lookup_literal_operator (name, args);
4639 if (decl && decl != error_mark_node)
4640 {
4641 result = finish_call_expr (decl, &args, false, true,
4642 tf_warning_or_error);
4643
4644 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4645 {
4646 warning_at (token->location, OPT_Woverflow,
4647 "integer literal exceeds range of %qT type",
4648 long_long_unsigned_type_node);
4649 }
4650 else
4651 {
4652 if (overflow > 0)
4653 warning_at (token->location, OPT_Woverflow,
4654 "floating literal exceeds range of %qT type",
4655 long_double_type_node);
4656 else if (overflow < 0)
4657 warning_at (token->location, OPT_Woverflow,
4658 "floating literal truncated to zero");
4659 }
4660
4661 return result;
4662 }
4663
4664 /* If the numeric argument didn't work, look for a raw literal
4665 operator taking a const char* argument consisting of the number
4666 in string format. */
4667 args->truncate (0);
4668 vec_safe_push (args, num_string);
4669 decl = lookup_literal_operator (name, args);
4670 if (decl && decl != error_mark_node)
4671 {
4672 result = finish_call_expr (decl, &args, false, true,
4673 tf_warning_or_error);
4674 return result;
4675 }
4676
4677 /* If the raw literal didn't work, look for a non-type template
4678 function with parameter pack char.... Call the function with
4679 template parameter characters representing the number. */
4680 args->truncate (0);
4681 decl = lookup_literal_operator (name, args);
4682 if (decl && decl != error_mark_node)
4683 {
4684 tree tmpl_args = make_char_string_pack (num_string);
4685 if (tmpl_args == NULL_TREE)
4686 {
4687 error ("failed to translate literal to execution character set %qT",
4688 num_string);
4689 return error_mark_node;
4690 }
4691 decl = lookup_template_function (decl, tmpl_args);
4692 result = finish_call_expr (decl, &args, false, true,
4693 tf_warning_or_error);
4694 return result;
4695 }
4696
4697 /* In C++14 the standard library defines complex number suffixes that
4698 conflict with GNU extensions. Prefer them if <complex> is #included. */
4699 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4700 bool i14 = (cxx_dialect > cxx11
4701 && (id_equal (suffix_id, "i")
4702 || id_equal (suffix_id, "if")
4703 || id_equal (suffix_id, "il")));
4704 diagnostic_t kind = DK_ERROR;
4705 int opt = 0;
4706
4707 if (i14 && ext)
4708 {
4709 tree cxlit = lookup_qualified_name (std_node, "complex_literals",
4710 LOOK_want::NORMAL, false);
4711 if (cxlit == error_mark_node)
4712 {
4713 /* No <complex>, so pedwarn and use GNU semantics. */
4714 kind = DK_PEDWARN;
4715 opt = OPT_Wpedantic;
4716 }
4717 }
4718
4719 bool complained
4720 = emit_diagnostic (kind, input_location, opt,
4721 "unable to find numeric literal operator %qD", name);
4722
4723 if (!complained)
4724 /* Don't inform either. */;
4725 else if (i14)
4726 {
4727 inform (token->location, "add %<using namespace std::complex_literals%> "
4728 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4729 "suffixes");
4730 if (ext)
4731 inform (token->location, "or use %<j%> instead of %<i%> for the "
4732 "GNU built-in suffix");
4733 }
4734 else if (!ext)
4735 inform (token->location, "use %<-fext-numeric-literals%> "
4736 "to enable more built-in suffixes");
4737
4738 if (kind == DK_ERROR)
4739 value = error_mark_node;
4740 else
4741 {
4742 /* Use the built-in semantics. */
4743 tree type;
4744 if (id_equal (suffix_id, "i"))
4745 {
4746 if (TREE_CODE (value) == INTEGER_CST)
4747 type = integer_type_node;
4748 else
4749 type = double_type_node;
4750 }
4751 else if (id_equal (suffix_id, "if"))
4752 type = float_type_node;
4753 else /* if (id_equal (suffix_id, "il")) */
4754 type = long_double_type_node;
4755
4756 value = build_complex (build_complex_type (type),
4757 fold_convert (type, integer_zero_node),
4758 fold_convert (type, value));
4759 }
4760
4761 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4762 /* Avoid repeated diagnostics. */
4763 token->u.value = value;
4764 return value;
4765 }
4766
4767 /* Parse a user-defined string constant. Returns a call to a user-defined
4768 literal operator taking a character pointer and the length of the string
4769 as arguments. */
4770
4771 static tree
4772 cp_parser_userdef_string_literal (tree literal)
4773 {
4774 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4775 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4776 tree value = USERDEF_LITERAL_VALUE (literal);
4777 int len = TREE_STRING_LENGTH (value)
4778 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4779 tree decl;
4780
4781 /* Build up a call to the user-defined operator. */
4782 /* Lookup the name we got back from the id-expression. */
4783 releasing_vec args;
4784 vec_safe_push (args, value);
4785 vec_safe_push (args, build_int_cst (size_type_node, len));
4786 decl = lookup_literal_operator (name, args);
4787
4788 if (decl && decl != error_mark_node)
4789 return finish_call_expr (decl, &args, false, true,
4790 tf_warning_or_error);
4791
4792 /* Look for a suitable template function, either (C++20) with a single
4793 parameter of class type, or (N3599) with typename parameter CharT and
4794 parameter pack CharT... */
4795 args->truncate (0);
4796 decl = lookup_literal_operator (name, args);
4797 if (decl && decl != error_mark_node)
4798 {
4799 /* Use resolve_nondeduced_context to try to choose one form of template
4800 or the other. */
4801 tree tmpl_args = make_tree_vec (1);
4802 TREE_VEC_ELT (tmpl_args, 0) = value;
4803 decl = lookup_template_function (decl, tmpl_args);
4804 tree res = resolve_nondeduced_context (decl, tf_none);
4805 if (DECL_P (res))
4806 decl = res;
4807 else
4808 {
4809 TREE_OPERAND (decl, 1) = make_string_pack (value);
4810 res = resolve_nondeduced_context (decl, tf_none);
4811 if (DECL_P (res))
4812 decl = res;
4813 }
4814 if (!DECL_P (decl) && cxx_dialect > cxx17)
4815 TREE_OPERAND (decl, 1) = tmpl_args;
4816 return finish_call_expr (decl, &args, false, true,
4817 tf_warning_or_error);
4818 }
4819
4820 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4821 name, TREE_TYPE (value), size_type_node);
4822 return error_mark_node;
4823 }
4824
4825
4826 /* Basic concepts [gram.basic] */
4827
4828 /* Parse a translation-unit.
4829
4830 translation-unit:
4831 declaration-seq [opt] */
4832
4833 static void
4834 cp_parser_translation_unit (cp_parser* parser)
4835 {
4836 gcc_checking_assert (!cp_error_declarator);
4837
4838 /* Create the declarator obstack. */
4839 gcc_obstack_init (&declarator_obstack);
4840 /* Create the error declarator. */
4841 cp_error_declarator = make_declarator (cdk_error);
4842 /* Create the empty parameter list. */
4843 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4844 UNKNOWN_LOCATION);
4845 /* Remember where the base of the declarator obstack lies. */
4846 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4847
4848 push_deferring_access_checks (flag_access_control
4849 ? dk_no_deferred : dk_no_check);
4850
4851 module_parse mp_state = MP_NOT_MODULE;
4852 if (modules_p () && !header_module_p ())
4853 mp_state = MP_FIRST;
4854
4855 bool implicit_extern_c = false;
4856
4857 /* Parse until EOF. */
4858 for (;;)
4859 {
4860 cp_token *token = cp_lexer_peek_token (parser->lexer);
4861
4862 /* If we're entering or exiting a region that's implicitly
4863 extern "C", modify the lang context appropriately. This is
4864 so horrible. Please die. */
4865 if (implicit_extern_c
4866 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4867 {
4868 implicit_extern_c = !implicit_extern_c;
4869 if (implicit_extern_c)
4870 push_lang_context (lang_name_c);
4871 else
4872 pop_lang_context ();
4873 }
4874
4875 if (token->type == CPP_EOF)
4876 break;
4877
4878 if (modules_p ())
4879 {
4880 /* Top-level module declarations are ok, and change the
4881 portion of file we're in. Top-level import declarations
4882 are significant for the import portions. */
4883
4884 cp_token *next = token;
4885 bool exporting = token->keyword == RID__EXPORT;
4886 if (exporting)
4887 {
4888 cp_lexer_consume_token (parser->lexer);
4889 next = cp_lexer_peek_token (parser->lexer);
4890 }
4891 if (next->keyword == RID__MODULE)
4892 {
4893 mp_state
4894 = cp_parser_module_declaration (parser, mp_state, exporting);
4895 continue;
4896 }
4897 else if (next->keyword == RID__IMPORT)
4898 {
4899 if (mp_state == MP_FIRST)
4900 mp_state = MP_NOT_MODULE;
4901 cp_parser_import_declaration (parser, mp_state, exporting);
4902 continue;
4903 }
4904 else
4905 gcc_checking_assert (!exporting);
4906
4907 if (mp_state == MP_GLOBAL && token->main_source_p)
4908 {
4909 static bool warned = false;
4910 if (!warned)
4911 {
4912 warned = true;
4913 error_at (token->location,
4914 "global module fragment contents must be"
4915 " from preprocessor inclusion");
4916 }
4917 }
4918 }
4919
4920 /* This relies on the ordering of module_parse values. */
4921 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
4922 /* We're no longer in the import portion of a named module. */
4923 mp_state = module_parse (mp_state + 1);
4924 else if (mp_state == MP_FIRST)
4925 mp_state = MP_NOT_MODULE;
4926
4927 if (token->type == CPP_CLOSE_BRACE)
4928 {
4929 cp_parser_error (parser, "expected declaration");
4930 cp_lexer_consume_token (parser->lexer);
4931 /* If the next token is now a `;', consume it. */
4932 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4933 cp_lexer_consume_token (parser->lexer);
4934 }
4935 else
4936 cp_parser_toplevel_declaration (parser);
4937 }
4938
4939 /* Get rid of the token array; we don't need it any more. */
4940 cp_lexer_destroy (parser->lexer);
4941 parser->lexer = NULL;
4942
4943 /* The EOF should have reset this. */
4944 gcc_checking_assert (!implicit_extern_c);
4945
4946 /* Make sure the declarator obstack was fully cleaned up. */
4947 gcc_assert (obstack_next_free (&declarator_obstack)
4948 == declarator_obstack_base);
4949 }
4950
4951 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4952 decltype context. */
4953
4954 static inline tsubst_flags_t
4955 complain_flags (bool decltype_p)
4956 {
4957 tsubst_flags_t complain = tf_warning_or_error;
4958 if (decltype_p)
4959 complain |= tf_decltype;
4960 return complain;
4961 }
4962
4963 /* We're about to parse a collection of statements. If we're currently
4964 parsing tentatively, set up a firewall so that any nested
4965 cp_parser_commit_to_tentative_parse won't affect the current context. */
4966
4967 static cp_token_position
4968 cp_parser_start_tentative_firewall (cp_parser *parser)
4969 {
4970 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4971 return 0;
4972
4973 cp_parser_parse_tentatively (parser);
4974 cp_parser_commit_to_topmost_tentative_parse (parser);
4975 return cp_lexer_token_position (parser->lexer, false);
4976 }
4977
4978 /* We've finished parsing the collection of statements. Wrap up the
4979 firewall and replace the relevant tokens with the parsed form. */
4980
4981 static void
4982 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4983 tree expr)
4984 {
4985 if (!start)
4986 return;
4987
4988 /* Finish the firewall level. */
4989 cp_parser_parse_definitely (parser);
4990 /* And remember the result of the parse for when we try again. */
4991 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4992 token->type = CPP_PREPARSED_EXPR;
4993 token->u.value = expr;
4994 token->keyword = RID_MAX;
4995 cp_lexer_purge_tokens_after (parser->lexer, start);
4996 }
4997
4998 /* Like the above functions, but let the user modify the tokens. Used by
4999 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
5000 later parses, so it makes sense to localize the effects of
5001 cp_parser_commit_to_tentative_parse. */
5002
5003 struct tentative_firewall
5004 {
5005 cp_parser *parser;
5006 bool set;
5007
5008 tentative_firewall (cp_parser *p): parser(p)
5009 {
5010 /* If we're currently parsing tentatively, start a committed level as a
5011 firewall and then an inner tentative parse. */
5012 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
5013 {
5014 cp_parser_parse_tentatively (parser);
5015 cp_parser_commit_to_topmost_tentative_parse (parser);
5016 cp_parser_parse_tentatively (parser);
5017 }
5018 }
5019
5020 ~tentative_firewall()
5021 {
5022 if (set)
5023 {
5024 /* Finish the inner tentative parse and the firewall, propagating any
5025 uncommitted error state to the outer tentative parse. */
5026 bool err = cp_parser_error_occurred (parser);
5027 cp_parser_parse_definitely (parser);
5028 cp_parser_parse_definitely (parser);
5029 if (err)
5030 cp_parser_simulate_error (parser);
5031 }
5032 }
5033 };
5034
5035 /* Some tokens naturally come in pairs e.g.'(' and ')'.
5036 This class is for tracking such a matching pair of symbols.
5037 In particular, it tracks the location of the first token,
5038 so that if the second token is missing, we can highlight the
5039 location of the first token when notifying the user about the
5040 problem. */
5041
5042 template <typename traits_t>
5043 class token_pair
5044 {
5045 public:
5046 /* token_pair's ctor. */
5047 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
5048
5049 /* If the next token is the opening symbol for this pair, consume it and
5050 return true.
5051 Otherwise, issue an error and return false.
5052 In either case, record the location of the opening token. */
5053
5054 bool require_open (cp_parser *parser)
5055 {
5056 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
5057 return cp_parser_require (parser, traits_t::open_token_type,
5058 traits_t::required_token_open);
5059 }
5060
5061 /* Consume the next token from PARSER, recording its location as
5062 that of the opening token within the pair. */
5063
5064 cp_token * consume_open (cp_parser *parser)
5065 {
5066 cp_token *tok = cp_lexer_consume_token (parser->lexer);
5067 gcc_assert (tok->type == traits_t::open_token_type);
5068 m_open_loc = tok->location;
5069 return tok;
5070 }
5071
5072 /* If the next token is the closing symbol for this pair, consume it
5073 and return it.
5074 Otherwise, issue an error, highlighting the location of the
5075 corresponding opening token, and return NULL. */
5076
5077 cp_token *require_close (cp_parser *parser) const
5078 {
5079 return cp_parser_require (parser, traits_t::close_token_type,
5080 traits_t::required_token_close,
5081 m_open_loc);
5082 }
5083
5084 location_t open_location () const { return m_open_loc; }
5085
5086 private:
5087 location_t m_open_loc;
5088 };
5089
5090 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
5091
5092 struct matching_paren_traits
5093 {
5094 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
5095 static const enum required_token required_token_open = RT_OPEN_PAREN;
5096 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
5097 static const enum required_token required_token_close = RT_CLOSE_PAREN;
5098 };
5099
5100 /* "matching_parens" is a token_pair<T> class for tracking matching
5101 pairs of parentheses. */
5102
5103 typedef token_pair<matching_paren_traits> matching_parens;
5104
5105 /* Traits for token_pair<T> for tracking matching pairs of braces. */
5106
5107 struct matching_brace_traits
5108 {
5109 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
5110 static const enum required_token required_token_open = RT_OPEN_BRACE;
5111 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
5112 static const enum required_token required_token_close = RT_CLOSE_BRACE;
5113 };
5114
5115 /* "matching_braces" is a token_pair<T> class for tracking matching
5116 pairs of braces. */
5117
5118 typedef token_pair<matching_brace_traits> matching_braces;
5119
5120
5121 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
5122 enclosing parentheses. */
5123
5124 static cp_expr
5125 cp_parser_statement_expr (cp_parser *parser)
5126 {
5127 cp_token_position start = cp_parser_start_tentative_firewall (parser);
5128
5129 /* Consume the '('. */
5130 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
5131 matching_parens parens;
5132 parens.consume_open (parser);
5133 /* Start the statement-expression. */
5134 tree expr = begin_stmt_expr ();
5135 /* Parse the compound-statement. */
5136 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
5137 /* Finish up. */
5138 expr = finish_stmt_expr (expr, false);
5139 /* Consume the ')'. */
5140 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
5141 if (!parens.require_close (parser))
5142 cp_parser_skip_to_end_of_statement (parser);
5143
5144 cp_parser_end_tentative_firewall (parser, start, expr);
5145 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
5146 return cp_expr (expr, combined_loc);
5147 }
5148
5149 /* Expressions [gram.expr] */
5150
5151 /* Parse a fold-operator.
5152
5153 fold-operator:
5154 - * / % ^ & | = < > << >>
5155 = -= *= /= %= ^= &= |= <<= >>=
5156 == != <= >= && || , .* ->*
5157
5158 This returns the tree code corresponding to the matched operator
5159 as an int. When the current token matches a compound assignment
5160 operator, the resulting tree code is the negative value of the
5161 non-assignment operator. */
5162
5163 static int
5164 cp_parser_fold_operator (cp_token *token)
5165 {
5166 switch (token->type)
5167 {
5168 case CPP_PLUS: return PLUS_EXPR;
5169 case CPP_MINUS: return MINUS_EXPR;
5170 case CPP_MULT: return MULT_EXPR;
5171 case CPP_DIV: return TRUNC_DIV_EXPR;
5172 case CPP_MOD: return TRUNC_MOD_EXPR;
5173 case CPP_XOR: return BIT_XOR_EXPR;
5174 case CPP_AND: return BIT_AND_EXPR;
5175 case CPP_OR: return BIT_IOR_EXPR;
5176 case CPP_LSHIFT: return LSHIFT_EXPR;
5177 case CPP_RSHIFT: return RSHIFT_EXPR;
5178
5179 case CPP_EQ: return -NOP_EXPR;
5180 case CPP_PLUS_EQ: return -PLUS_EXPR;
5181 case CPP_MINUS_EQ: return -MINUS_EXPR;
5182 case CPP_MULT_EQ: return -MULT_EXPR;
5183 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
5184 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
5185 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
5186 case CPP_AND_EQ: return -BIT_AND_EXPR;
5187 case CPP_OR_EQ: return -BIT_IOR_EXPR;
5188 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
5189 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
5190
5191 case CPP_EQ_EQ: return EQ_EXPR;
5192 case CPP_NOT_EQ: return NE_EXPR;
5193 case CPP_LESS: return LT_EXPR;
5194 case CPP_GREATER: return GT_EXPR;
5195 case CPP_LESS_EQ: return LE_EXPR;
5196 case CPP_GREATER_EQ: return GE_EXPR;
5197
5198 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
5199 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
5200
5201 case CPP_COMMA: return COMPOUND_EXPR;
5202
5203 case CPP_DOT_STAR: return DOTSTAR_EXPR;
5204 case CPP_DEREF_STAR: return MEMBER_REF;
5205
5206 default: return ERROR_MARK;
5207 }
5208 }
5209
5210 /* Returns true if CODE indicates a binary expression, which is not allowed in
5211 the LHS of a fold-expression. More codes will need to be added to use this
5212 function in other contexts. */
5213
5214 static bool
5215 is_binary_op (tree_code code)
5216 {
5217 switch (code)
5218 {
5219 case PLUS_EXPR:
5220 case POINTER_PLUS_EXPR:
5221 case MINUS_EXPR:
5222 case MULT_EXPR:
5223 case TRUNC_DIV_EXPR:
5224 case TRUNC_MOD_EXPR:
5225 case BIT_XOR_EXPR:
5226 case BIT_AND_EXPR:
5227 case BIT_IOR_EXPR:
5228 case LSHIFT_EXPR:
5229 case RSHIFT_EXPR:
5230
5231 case MODOP_EXPR:
5232
5233 case EQ_EXPR:
5234 case NE_EXPR:
5235 case LE_EXPR:
5236 case GE_EXPR:
5237 case LT_EXPR:
5238 case GT_EXPR:
5239
5240 case TRUTH_ANDIF_EXPR:
5241 case TRUTH_ORIF_EXPR:
5242
5243 case COMPOUND_EXPR:
5244
5245 case DOTSTAR_EXPR:
5246 case MEMBER_REF:
5247 return true;
5248
5249 default:
5250 return false;
5251 }
5252 }
5253
5254 /* If the next token is a suitable fold operator, consume it and return as
5255 the function above. */
5256
5257 static int
5258 cp_parser_fold_operator (cp_parser *parser)
5259 {
5260 cp_token* token = cp_lexer_peek_token (parser->lexer);
5261 int code = cp_parser_fold_operator (token);
5262 if (code != ERROR_MARK)
5263 cp_lexer_consume_token (parser->lexer);
5264 return code;
5265 }
5266
5267 /* Parse a fold-expression.
5268
5269 fold-expression:
5270 ( ... folding-operator cast-expression)
5271 ( cast-expression folding-operator ... )
5272 ( cast-expression folding operator ... folding-operator cast-expression)
5273
5274 Note that the '(' and ')' are matched in primary expression. */
5275
5276 static cp_expr
5277 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5278 {
5279 cp_id_kind pidk;
5280
5281 // Left fold.
5282 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5283 {
5284 if (expr1)
5285 return error_mark_node;
5286 cp_lexer_consume_token (parser->lexer);
5287 int op = cp_parser_fold_operator (parser);
5288 if (op == ERROR_MARK)
5289 {
5290 cp_parser_error (parser, "expected binary operator");
5291 return error_mark_node;
5292 }
5293
5294 tree expr = cp_parser_cast_expression (parser, false, false,
5295 false, &pidk);
5296 if (expr == error_mark_node)
5297 return error_mark_node;
5298 return finish_left_unary_fold_expr (expr, op);
5299 }
5300
5301 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5302 int op = cp_parser_fold_operator (parser);
5303 if (op == ERROR_MARK)
5304 {
5305 cp_parser_error (parser, "expected binary operator");
5306 return error_mark_node;
5307 }
5308
5309 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5310 {
5311 cp_parser_error (parser, "expected ...");
5312 return error_mark_node;
5313 }
5314 cp_lexer_consume_token (parser->lexer);
5315
5316 /* The operands of a fold-expression are cast-expressions, so binary or
5317 conditional expressions are not allowed. We check this here to avoid
5318 tentative parsing. */
5319 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5320 /* OK, the expression was parenthesized. */;
5321 else if (is_binary_op (TREE_CODE (expr1)))
5322 error_at (location_of (expr1),
5323 "binary expression in operand of fold-expression");
5324 else if (TREE_CODE (expr1) == COND_EXPR
5325 || (REFERENCE_REF_P (expr1)
5326 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5327 error_at (location_of (expr1),
5328 "conditional expression in operand of fold-expression");
5329
5330 // Right fold.
5331 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5332 return finish_right_unary_fold_expr (expr1, op);
5333
5334 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5335 {
5336 cp_parser_error (parser, "mismatched operator in fold-expression");
5337 return error_mark_node;
5338 }
5339 cp_lexer_consume_token (parser->lexer);
5340
5341 // Binary left or right fold.
5342 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5343 if (expr2 == error_mark_node)
5344 return error_mark_node;
5345 return finish_binary_fold_expr (expr1, expr2, op);
5346 }
5347
5348 /* Parse a primary-expression.
5349
5350 primary-expression:
5351 literal
5352 this
5353 ( expression )
5354 id-expression
5355 lambda-expression (C++11)
5356
5357 GNU Extensions:
5358
5359 primary-expression:
5360 ( compound-statement )
5361 __builtin_va_arg ( assignment-expression , type-id )
5362 __builtin_offsetof ( type-id , offsetof-expression )
5363
5364 C++ Extensions:
5365 __has_nothrow_assign ( type-id )
5366 __has_nothrow_constructor ( type-id )
5367 __has_nothrow_copy ( type-id )
5368 __has_trivial_assign ( type-id )
5369 __has_trivial_constructor ( type-id )
5370 __has_trivial_copy ( type-id )
5371 __has_trivial_destructor ( type-id )
5372 __has_virtual_destructor ( type-id )
5373 __is_abstract ( type-id )
5374 __is_base_of ( type-id , type-id )
5375 __is_class ( type-id )
5376 __is_empty ( type-id )
5377 __is_enum ( type-id )
5378 __is_final ( type-id )
5379 __is_literal_type ( type-id )
5380 __is_pod ( type-id )
5381 __is_polymorphic ( type-id )
5382 __is_std_layout ( type-id )
5383 __is_trivial ( type-id )
5384 __is_union ( type-id )
5385
5386 Objective-C++ Extension:
5387
5388 primary-expression:
5389 objc-expression
5390
5391 literal:
5392 __null
5393
5394 ADDRESS_P is true iff this expression was immediately preceded by
5395 "&" and therefore might denote a pointer-to-member. CAST_P is true
5396 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5397 true iff this expression is a template argument.
5398
5399 Returns a representation of the expression. Upon return, *IDK
5400 indicates what kind of id-expression (if any) was present. */
5401
5402 static cp_expr
5403 cp_parser_primary_expression (cp_parser *parser,
5404 bool address_p,
5405 bool cast_p,
5406 bool template_arg_p,
5407 bool decltype_p,
5408 cp_id_kind *idk)
5409 {
5410 cp_token *token = NULL;
5411
5412 /* Assume the primary expression is not an id-expression. */
5413 *idk = CP_ID_KIND_NONE;
5414
5415 /* Peek at the next token. */
5416 token = cp_lexer_peek_token (parser->lexer);
5417 switch ((int) token->type)
5418 {
5419 /* literal:
5420 integer-literal
5421 character-literal
5422 floating-literal
5423 string-literal
5424 boolean-literal
5425 pointer-literal
5426 user-defined-literal */
5427 case CPP_CHAR:
5428 case CPP_CHAR16:
5429 case CPP_CHAR32:
5430 case CPP_WCHAR:
5431 case CPP_UTF8CHAR:
5432 case CPP_NUMBER:
5433 case CPP_PREPARSED_EXPR:
5434 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5435 return cp_parser_userdef_numeric_literal (parser);
5436 token = cp_lexer_consume_token (parser->lexer);
5437 if (TREE_CODE (token->u.value) == FIXED_CST)
5438 {
5439 error_at (token->location,
5440 "fixed-point types not supported in C++");
5441 return error_mark_node;
5442 }
5443 /* Floating-point literals are only allowed in an integral
5444 constant expression if they are cast to an integral or
5445 enumeration type. */
5446 if (TREE_CODE (token->u.value) == REAL_CST
5447 && parser->integral_constant_expression_p
5448 && pedantic)
5449 {
5450 /* CAST_P will be set even in invalid code like "int(2.7 +
5451 ...)". Therefore, we have to check that the next token
5452 is sure to end the cast. */
5453 if (cast_p)
5454 {
5455 cp_token *next_token;
5456
5457 next_token = cp_lexer_peek_token (parser->lexer);
5458 if (/* The comma at the end of an
5459 enumerator-definition. */
5460 next_token->type != CPP_COMMA
5461 /* The curly brace at the end of an enum-specifier. */
5462 && next_token->type != CPP_CLOSE_BRACE
5463 /* The end of a statement. */
5464 && next_token->type != CPP_SEMICOLON
5465 /* The end of the cast-expression. */
5466 && next_token->type != CPP_CLOSE_PAREN
5467 /* The end of an array bound. */
5468 && next_token->type != CPP_CLOSE_SQUARE
5469 /* The closing ">" in a template-argument-list. */
5470 && (next_token->type != CPP_GREATER
5471 || parser->greater_than_is_operator_p)
5472 /* C++0x only: A ">>" treated like two ">" tokens,
5473 in a template-argument-list. */
5474 && (next_token->type != CPP_RSHIFT
5475 || (cxx_dialect == cxx98)
5476 || parser->greater_than_is_operator_p))
5477 cast_p = false;
5478 }
5479
5480 /* If we are within a cast, then the constraint that the
5481 cast is to an integral or enumeration type will be
5482 checked at that point. If we are not within a cast, then
5483 this code is invalid. */
5484 if (!cast_p)
5485 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5486 }
5487 return (cp_expr (token->u.value, token->location)
5488 .maybe_add_location_wrapper ());
5489
5490 case CPP_CHAR_USERDEF:
5491 case CPP_CHAR16_USERDEF:
5492 case CPP_CHAR32_USERDEF:
5493 case CPP_WCHAR_USERDEF:
5494 case CPP_UTF8CHAR_USERDEF:
5495 return cp_parser_userdef_char_literal (parser);
5496
5497 case CPP_STRING:
5498 case CPP_STRING16:
5499 case CPP_STRING32:
5500 case CPP_WSTRING:
5501 case CPP_UTF8STRING:
5502 case CPP_STRING_USERDEF:
5503 case CPP_STRING16_USERDEF:
5504 case CPP_STRING32_USERDEF:
5505 case CPP_WSTRING_USERDEF:
5506 case CPP_UTF8STRING_USERDEF:
5507 /* ??? Should wide strings be allowed when parser->translate_strings_p
5508 is false (i.e. in attributes)? If not, we can kill the third
5509 argument to cp_parser_string_literal. */
5510 return (cp_parser_string_literal (parser,
5511 parser->translate_strings_p,
5512 true)
5513 .maybe_add_location_wrapper ());
5514
5515 case CPP_OPEN_PAREN:
5516 /* If we see `( { ' then we are looking at the beginning of
5517 a GNU statement-expression. */
5518 if (cp_parser_allow_gnu_extensions_p (parser)
5519 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5520 {
5521 /* Statement-expressions are not allowed by the standard. */
5522 pedwarn (token->location, OPT_Wpedantic,
5523 "ISO C++ forbids braced-groups within expressions");
5524
5525 /* And they're not allowed outside of a function-body; you
5526 cannot, for example, write:
5527
5528 int i = ({ int j = 3; j + 1; });
5529
5530 at class or namespace scope. */
5531 if (!parser->in_function_body
5532 || parser->in_template_argument_list_p)
5533 {
5534 error_at (token->location,
5535 "statement-expressions are not allowed outside "
5536 "functions nor in template-argument lists");
5537 cp_parser_skip_to_end_of_block_or_statement (parser);
5538 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5539 cp_lexer_consume_token (parser->lexer);
5540 return error_mark_node;
5541 }
5542 else
5543 return cp_parser_statement_expr (parser);
5544 }
5545 /* Otherwise it's a normal parenthesized expression. */
5546 {
5547 cp_expr expr;
5548 bool saved_greater_than_is_operator_p;
5549
5550 location_t open_paren_loc = token->location;
5551
5552 /* Consume the `('. */
5553 matching_parens parens;
5554 parens.consume_open (parser);
5555 /* Within a parenthesized expression, a `>' token is always
5556 the greater-than operator. */
5557 saved_greater_than_is_operator_p
5558 = parser->greater_than_is_operator_p;
5559 parser->greater_than_is_operator_p = true;
5560
5561 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5562 /* Left fold expression. */
5563 expr = NULL_TREE;
5564 else
5565 /* Parse the parenthesized expression. */
5566 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5567
5568 token = cp_lexer_peek_token (parser->lexer);
5569 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5570 {
5571 expr = cp_parser_fold_expression (parser, expr);
5572 if (expr != error_mark_node
5573 && cxx_dialect < cxx17)
5574 pedwarn (input_location, 0, "fold-expressions only available "
5575 "with %<-std=c++17%> or %<-std=gnu++17%>");
5576 }
5577 else
5578 /* Let the front end know that this expression was
5579 enclosed in parentheses. This matters in case, for
5580 example, the expression is of the form `A::B', since
5581 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5582 not. */
5583 expr = finish_parenthesized_expr (expr);
5584
5585 /* DR 705: Wrapping an unqualified name in parentheses
5586 suppresses arg-dependent lookup. We want to pass back
5587 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5588 (c++/37862), but none of the others. */
5589 if (*idk != CP_ID_KIND_QUALIFIED)
5590 *idk = CP_ID_KIND_NONE;
5591
5592 /* The `>' token might be the end of a template-id or
5593 template-parameter-list now. */
5594 parser->greater_than_is_operator_p
5595 = saved_greater_than_is_operator_p;
5596
5597 /* Consume the `)'. */
5598 token = cp_lexer_peek_token (parser->lexer);
5599 location_t close_paren_loc = token->location;
5600 expr.set_range (open_paren_loc, close_paren_loc);
5601 if (!parens.require_close (parser)
5602 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5603 cp_parser_skip_to_end_of_statement (parser);
5604
5605 return expr;
5606 }
5607
5608 case CPP_OPEN_SQUARE:
5609 {
5610 if (c_dialect_objc ())
5611 {
5612 /* We might have an Objective-C++ message. */
5613 cp_parser_parse_tentatively (parser);
5614 tree msg = cp_parser_objc_message_expression (parser);
5615 /* If that works out, we're done ... */
5616 if (cp_parser_parse_definitely (parser))
5617 return msg;
5618 /* ... else, fall though to see if it's a lambda. */
5619 }
5620 cp_expr lam = cp_parser_lambda_expression (parser);
5621 /* Don't warn about a failed tentative parse. */
5622 if (cp_parser_error_occurred (parser))
5623 return error_mark_node;
5624 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5625 return lam;
5626 }
5627
5628 case CPP_OBJC_STRING:
5629 if (c_dialect_objc ())
5630 /* We have an Objective-C++ string literal. */
5631 return cp_parser_objc_expression (parser);
5632 cp_parser_error (parser, "expected primary-expression");
5633 return error_mark_node;
5634
5635 case CPP_KEYWORD:
5636 switch (token->keyword)
5637 {
5638 /* These two are the boolean literals. */
5639 case RID_TRUE:
5640 cp_lexer_consume_token (parser->lexer);
5641 return cp_expr (boolean_true_node, token->location);
5642 case RID_FALSE:
5643 cp_lexer_consume_token (parser->lexer);
5644 return cp_expr (boolean_false_node, token->location);
5645
5646 /* The `__null' literal. */
5647 case RID_NULL:
5648 cp_lexer_consume_token (parser->lexer);
5649 return cp_expr (null_node, token->location);
5650
5651 /* The `nullptr' literal. */
5652 case RID_NULLPTR:
5653 cp_lexer_consume_token (parser->lexer);
5654 return cp_expr (nullptr_node, token->location);
5655
5656 /* Recognize the `this' keyword. */
5657 case RID_THIS:
5658 cp_lexer_consume_token (parser->lexer);
5659 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5660 {
5661 error_at (token->location,
5662 "%<this%> may not be used in this context");
5663 return error_mark_node;
5664 }
5665 /* Pointers cannot appear in constant-expressions. */
5666 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5667 return error_mark_node;
5668 return cp_expr (finish_this_expr (), token->location);
5669
5670 /* The `operator' keyword can be the beginning of an
5671 id-expression. */
5672 case RID_OPERATOR:
5673 goto id_expression;
5674
5675 case RID_FUNCTION_NAME:
5676 case RID_PRETTY_FUNCTION_NAME:
5677 case RID_C99_FUNCTION_NAME:
5678 {
5679 non_integral_constant name;
5680
5681 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5682 __func__ are the names of variables -- but they are
5683 treated specially. Therefore, they are handled here,
5684 rather than relying on the generic id-expression logic
5685 below. Grammatically, these names are id-expressions.
5686
5687 Consume the token. */
5688 token = cp_lexer_consume_token (parser->lexer);
5689
5690 switch (token->keyword)
5691 {
5692 case RID_FUNCTION_NAME:
5693 name = NIC_FUNC_NAME;
5694 break;
5695 case RID_PRETTY_FUNCTION_NAME:
5696 name = NIC_PRETTY_FUNC;
5697 break;
5698 case RID_C99_FUNCTION_NAME:
5699 name = NIC_C99_FUNC;
5700 break;
5701 default:
5702 gcc_unreachable ();
5703 }
5704
5705 if (cp_parser_non_integral_constant_expression (parser, name))
5706 return error_mark_node;
5707
5708 /* Look up the name. */
5709 return finish_fname (token->u.value);
5710 }
5711
5712 case RID_VA_ARG:
5713 {
5714 tree expression;
5715 tree type;
5716 location_t type_location;
5717 location_t start_loc
5718 = cp_lexer_peek_token (parser->lexer)->location;
5719 /* The `__builtin_va_arg' construct is used to handle
5720 `va_arg'. Consume the `__builtin_va_arg' token. */
5721 cp_lexer_consume_token (parser->lexer);
5722 /* Look for the opening `('. */
5723 matching_parens parens;
5724 parens.require_open (parser);
5725 /* Now, parse the assignment-expression. */
5726 expression = cp_parser_assignment_expression (parser);
5727 /* Look for the `,'. */
5728 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5729 type_location = cp_lexer_peek_token (parser->lexer)->location;
5730 /* Parse the type-id. */
5731 {
5732 type_id_in_expr_sentinel s (parser);
5733 type = cp_parser_type_id (parser);
5734 }
5735 /* Look for the closing `)'. */
5736 location_t finish_loc
5737 = cp_lexer_peek_token (parser->lexer)->location;
5738 parens.require_close (parser);
5739 /* Using `va_arg' in a constant-expression is not
5740 allowed. */
5741 if (cp_parser_non_integral_constant_expression (parser,
5742 NIC_VA_ARG))
5743 return error_mark_node;
5744 /* Construct a location of the form:
5745 __builtin_va_arg (v, int)
5746 ~~~~~~~~~~~~~~~~~~~~~^~~~
5747 with the caret at the type, ranging from the start of the
5748 "__builtin_va_arg" token to the close paren. */
5749 location_t combined_loc
5750 = make_location (type_location, start_loc, finish_loc);
5751 return build_x_va_arg (combined_loc, expression, type);
5752 }
5753
5754 case RID_OFFSETOF:
5755 return cp_parser_builtin_offsetof (parser);
5756
5757 case RID_HAS_NOTHROW_ASSIGN:
5758 case RID_HAS_NOTHROW_CONSTRUCTOR:
5759 case RID_HAS_NOTHROW_COPY:
5760 case RID_HAS_TRIVIAL_ASSIGN:
5761 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5762 case RID_HAS_TRIVIAL_COPY:
5763 case RID_HAS_TRIVIAL_DESTRUCTOR:
5764 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5765 case RID_HAS_VIRTUAL_DESTRUCTOR:
5766 case RID_IS_ABSTRACT:
5767 case RID_IS_AGGREGATE:
5768 case RID_IS_BASE_OF:
5769 case RID_IS_CLASS:
5770 case RID_IS_EMPTY:
5771 case RID_IS_ENUM:
5772 case RID_IS_FINAL:
5773 case RID_IS_LITERAL_TYPE:
5774 case RID_IS_POD:
5775 case RID_IS_POLYMORPHIC:
5776 case RID_IS_SAME_AS:
5777 case RID_IS_STD_LAYOUT:
5778 case RID_IS_TRIVIAL:
5779 case RID_IS_TRIVIALLY_ASSIGNABLE:
5780 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5781 case RID_IS_TRIVIALLY_COPYABLE:
5782 case RID_IS_UNION:
5783 case RID_IS_ASSIGNABLE:
5784 case RID_IS_CONSTRUCTIBLE:
5785 case RID_IS_NOTHROW_ASSIGNABLE:
5786 case RID_IS_NOTHROW_CONSTRUCTIBLE:
5787 return cp_parser_trait_expr (parser, token->keyword);
5788
5789 // C++ concepts
5790 case RID_REQUIRES:
5791 return cp_parser_requires_expression (parser);
5792
5793 /* Objective-C++ expressions. */
5794 case RID_AT_ENCODE:
5795 case RID_AT_PROTOCOL:
5796 case RID_AT_SELECTOR:
5797 return cp_parser_objc_expression (parser);
5798
5799 case RID_TEMPLATE:
5800 if (parser->in_function_body
5801 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5802 == CPP_LESS))
5803 {
5804 error_at (token->location,
5805 "a template declaration cannot appear at block scope");
5806 cp_parser_skip_to_end_of_block_or_statement (parser);
5807 return error_mark_node;
5808 }
5809 /* FALLTHRU */
5810 default:
5811 cp_parser_error (parser, "expected primary-expression");
5812 return error_mark_node;
5813 }
5814
5815 /* An id-expression can start with either an identifier, a
5816 `::' as the beginning of a qualified-id, or the "operator"
5817 keyword. */
5818 case CPP_NAME:
5819 case CPP_SCOPE:
5820 case CPP_TEMPLATE_ID:
5821 case CPP_NESTED_NAME_SPECIFIER:
5822 {
5823 id_expression:
5824 cp_expr id_expression;
5825 cp_expr decl;
5826 const char *error_msg;
5827 bool template_p;
5828 bool done;
5829 cp_token *id_expr_token;
5830
5831 /* Parse the id-expression. */
5832 id_expression
5833 = cp_parser_id_expression (parser,
5834 /*template_keyword_p=*/false,
5835 /*check_dependency_p=*/true,
5836 &template_p,
5837 /*declarator_p=*/false,
5838 /*optional_p=*/false);
5839 if (id_expression == error_mark_node)
5840 return error_mark_node;
5841 id_expr_token = token;
5842 token = cp_lexer_peek_token (parser->lexer);
5843 done = (token->type != CPP_OPEN_SQUARE
5844 && token->type != CPP_OPEN_PAREN
5845 && token->type != CPP_DOT
5846 && token->type != CPP_DEREF
5847 && token->type != CPP_PLUS_PLUS
5848 && token->type != CPP_MINUS_MINUS);
5849 /* If we have a template-id, then no further lookup is
5850 required. If the template-id was for a template-class, we
5851 will sometimes have a TYPE_DECL at this point. */
5852 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5853 || TREE_CODE (id_expression) == TYPE_DECL)
5854 decl = id_expression;
5855 /* Look up the name. */
5856 else
5857 {
5858 tree ambiguous_decls;
5859
5860 /* If we already know that this lookup is ambiguous, then
5861 we've already issued an error message; there's no reason
5862 to check again. */
5863 if (id_expr_token->type == CPP_NAME
5864 && id_expr_token->error_reported)
5865 {
5866 cp_parser_simulate_error (parser);
5867 return error_mark_node;
5868 }
5869
5870 decl = cp_parser_lookup_name (parser, id_expression,
5871 none_type,
5872 template_p,
5873 /*is_namespace=*/false,
5874 /*check_dependency=*/true,
5875 &ambiguous_decls,
5876 id_expression.get_location ());
5877 /* If the lookup was ambiguous, an error will already have
5878 been issued. */
5879 if (ambiguous_decls)
5880 return error_mark_node;
5881
5882 /* In Objective-C++, we may have an Objective-C 2.0
5883 dot-syntax for classes here. */
5884 if (c_dialect_objc ()
5885 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5886 && TREE_CODE (decl) == TYPE_DECL
5887 && objc_is_class_name (decl))
5888 {
5889 tree component;
5890 cp_lexer_consume_token (parser->lexer);
5891 component = cp_parser_identifier (parser);
5892 if (component == error_mark_node)
5893 return error_mark_node;
5894
5895 tree result = objc_build_class_component_ref (id_expression,
5896 component);
5897 /* Build a location of the form:
5898 expr.component
5899 ~~~~~^~~~~~~~~
5900 with caret at the start of the component name (at
5901 input_location), ranging from the start of the id_expression
5902 to the end of the component name. */
5903 location_t combined_loc
5904 = make_location (input_location, id_expression.get_start (),
5905 get_finish (input_location));
5906 protected_set_expr_location (result, combined_loc);
5907 return result;
5908 }
5909
5910 /* In Objective-C++, an instance variable (ivar) may be preferred
5911 to whatever cp_parser_lookup_name() found.
5912 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5913 rest of c-family, we have to do a little extra work to preserve
5914 any location information in cp_expr "decl". Given that
5915 objc_lookup_ivar is implemented in "c-family" and "objc", we
5916 have a trip through the pure "tree" type, rather than cp_expr.
5917 Naively copying it back to "decl" would implicitly give the
5918 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5919 store an EXPR_LOCATION. Hence we only update "decl" (and
5920 hence its location_t) if we get back a different tree node. */
5921 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5922 id_expression);
5923 if (decl_tree != decl.get_value ())
5924 decl = cp_expr (decl_tree);
5925
5926 /* If name lookup gives us a SCOPE_REF, then the
5927 qualifying scope was dependent. */
5928 if (TREE_CODE (decl) == SCOPE_REF)
5929 {
5930 /* At this point, we do not know if DECL is a valid
5931 integral constant expression. We assume that it is
5932 in fact such an expression, so that code like:
5933
5934 template <int N> struct A {
5935 int a[B<N>::i];
5936 };
5937
5938 is accepted. At template-instantiation time, we
5939 will check that B<N>::i is actually a constant. */
5940 return decl;
5941 }
5942 /* Check to see if DECL is a local variable in a context
5943 where that is forbidden. */
5944 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
5945 && local_variable_p (decl))
5946 {
5947 const char *msg
5948 = (TREE_CODE (decl) == PARM_DECL
5949 ? _("parameter %qD may not appear in this context")
5950 : _("local variable %qD may not appear in this context"));
5951 error_at (id_expression.get_location (), msg,
5952 decl.get_value ());
5953 return error_mark_node;
5954 }
5955 }
5956
5957 decl = (finish_id_expression
5958 (id_expression, decl, parser->scope,
5959 idk,
5960 parser->integral_constant_expression_p,
5961 parser->allow_non_integral_constant_expression_p,
5962 &parser->non_integral_constant_expression_p,
5963 template_p, done, address_p,
5964 template_arg_p,
5965 &error_msg,
5966 id_expression.get_location ()));
5967 if (error_msg)
5968 cp_parser_error (parser, error_msg);
5969 /* Build a location for an id-expression of the form:
5970 ::ns::id
5971 ~~~~~~^~
5972 or:
5973 id
5974 ^~
5975 i.e. from the start of the first token to the end of the final
5976 token, with the caret at the start of the unqualified-id. */
5977 location_t caret_loc = get_pure_location (id_expression.get_location ());
5978 location_t start_loc = get_start (id_expr_token->location);
5979 location_t finish_loc = get_finish (id_expression.get_location ());
5980 location_t combined_loc
5981 = make_location (caret_loc, start_loc, finish_loc);
5982
5983 decl.set_location (combined_loc);
5984 return decl;
5985 }
5986
5987 /* Anything else is an error. */
5988 default:
5989 cp_parser_error (parser, "expected primary-expression");
5990 return error_mark_node;
5991 }
5992 }
5993
5994 static inline cp_expr
5995 cp_parser_primary_expression (cp_parser *parser,
5996 bool address_p,
5997 bool cast_p,
5998 bool template_arg_p,
5999 cp_id_kind *idk)
6000 {
6001 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
6002 /*decltype*/false, idk);
6003 }
6004
6005 /* Parse an id-expression.
6006
6007 id-expression:
6008 unqualified-id
6009 qualified-id
6010
6011 qualified-id:
6012 :: [opt] nested-name-specifier template [opt] unqualified-id
6013 :: identifier
6014 :: operator-function-id
6015 :: template-id
6016
6017 Return a representation of the unqualified portion of the
6018 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
6019 a `::' or nested-name-specifier.
6020
6021 Often, if the id-expression was a qualified-id, the caller will
6022 want to make a SCOPE_REF to represent the qualified-id. This
6023 function does not do this in order to avoid wastefully creating
6024 SCOPE_REFs when they are not required.
6025
6026 If TEMPLATE_KEYWORD_P is true, then we have just seen the
6027 `template' keyword.
6028
6029 If CHECK_DEPENDENCY_P is false, then names are looked up inside
6030 uninstantiated templates.
6031
6032 If *TEMPLATE_P is non-NULL, it is set to true iff the
6033 `template' keyword is used to explicitly indicate that the entity
6034 named is a template.
6035
6036 If DECLARATOR_P is true, the id-expression is appearing as part of
6037 a declarator, rather than as part of an expression. */
6038
6039 static cp_expr
6040 cp_parser_id_expression (cp_parser *parser,
6041 bool template_keyword_p,
6042 bool check_dependency_p,
6043 bool *template_p,
6044 bool declarator_p,
6045 bool optional_p)
6046 {
6047 bool global_scope_p;
6048 bool nested_name_specifier_p;
6049
6050 /* Assume the `template' keyword was not used. */
6051 if (template_p)
6052 *template_p = template_keyword_p;
6053
6054 /* Look for the optional `::' operator. */
6055 global_scope_p
6056 = (!template_keyword_p
6057 && (cp_parser_global_scope_opt (parser,
6058 /*current_scope_valid_p=*/false)
6059 != NULL_TREE));
6060
6061 /* Look for the optional nested-name-specifier. */
6062 nested_name_specifier_p
6063 = (cp_parser_nested_name_specifier_opt (parser,
6064 /*typename_keyword_p=*/false,
6065 check_dependency_p,
6066 /*type_p=*/false,
6067 declarator_p,
6068 template_keyword_p)
6069 != NULL_TREE);
6070
6071 /* If there is a nested-name-specifier, then we are looking at
6072 the first qualified-id production. */
6073 if (nested_name_specifier_p)
6074 {
6075 tree saved_scope;
6076 tree saved_object_scope;
6077 tree saved_qualifying_scope;
6078 cp_expr unqualified_id;
6079 bool is_template;
6080
6081 /* See if the next token is the `template' keyword. */
6082 if (!template_p)
6083 template_p = &is_template;
6084 *template_p = cp_parser_optional_template_keyword (parser);
6085 /* Name lookup we do during the processing of the
6086 unqualified-id might obliterate SCOPE. */
6087 saved_scope = parser->scope;
6088 saved_object_scope = parser->object_scope;
6089 saved_qualifying_scope = parser->qualifying_scope;
6090 /* Process the final unqualified-id. */
6091 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
6092 check_dependency_p,
6093 declarator_p,
6094 /*optional_p=*/false);
6095 /* Restore the SAVED_SCOPE for our caller. */
6096 parser->scope = saved_scope;
6097 parser->object_scope = saved_object_scope;
6098 parser->qualifying_scope = saved_qualifying_scope;
6099
6100 return unqualified_id;
6101 }
6102 /* Otherwise, if we are in global scope, then we are looking at one
6103 of the other qualified-id productions. */
6104 else if (global_scope_p)
6105 {
6106 cp_token *token;
6107 tree id;
6108
6109 /* Peek at the next token. */
6110 token = cp_lexer_peek_token (parser->lexer);
6111
6112 /* If it's an identifier, and the next token is not a "<", then
6113 we can avoid the template-id case. This is an optimization
6114 for this common case. */
6115 if (token->type == CPP_NAME
6116 && !cp_parser_nth_token_starts_template_argument_list_p
6117 (parser, 2))
6118 return cp_parser_identifier (parser);
6119
6120 cp_parser_parse_tentatively (parser);
6121 /* Try a template-id. */
6122 id = cp_parser_template_id_expr (parser,
6123 /*template_keyword_p=*/false,
6124 /*check_dependency_p=*/true,
6125 declarator_p);
6126 /* If that worked, we're done. */
6127 if (cp_parser_parse_definitely (parser))
6128 return id;
6129
6130 /* Peek at the next token. (Changes in the token buffer may
6131 have invalidated the pointer obtained above.) */
6132 token = cp_lexer_peek_token (parser->lexer);
6133
6134 switch (token->type)
6135 {
6136 case CPP_NAME:
6137 return cp_parser_identifier (parser);
6138
6139 case CPP_KEYWORD:
6140 if (token->keyword == RID_OPERATOR)
6141 return cp_parser_operator_function_id (parser);
6142 /* Fall through. */
6143
6144 default:
6145 cp_parser_error (parser, "expected id-expression");
6146 return error_mark_node;
6147 }
6148 }
6149 else
6150 return cp_parser_unqualified_id (parser, template_keyword_p,
6151 /*check_dependency_p=*/true,
6152 declarator_p,
6153 optional_p);
6154 }
6155
6156 /* Parse an unqualified-id.
6157
6158 unqualified-id:
6159 identifier
6160 operator-function-id
6161 conversion-function-id
6162 ~ class-name
6163 template-id
6164
6165 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6166 keyword, in a construct like `A::template ...'.
6167
6168 Returns a representation of unqualified-id. For the `identifier'
6169 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
6170 production a BIT_NOT_EXPR is returned; the operand of the
6171 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
6172 other productions, see the documentation accompanying the
6173 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
6174 names are looked up in uninstantiated templates. If DECLARATOR_P
6175 is true, the unqualified-id is appearing as part of a declarator,
6176 rather than as part of an expression. */
6177
6178 static cp_expr
6179 cp_parser_unqualified_id (cp_parser* parser,
6180 bool template_keyword_p,
6181 bool check_dependency_p,
6182 bool declarator_p,
6183 bool optional_p)
6184 {
6185 cp_token *token;
6186
6187 /* Peek at the next token. */
6188 token = cp_lexer_peek_token (parser->lexer);
6189
6190 switch ((int) token->type)
6191 {
6192 case CPP_NAME:
6193 {
6194 tree id;
6195
6196 /* We don't know yet whether or not this will be a
6197 template-id. */
6198 cp_parser_parse_tentatively (parser);
6199 /* Try a template-id. */
6200 id = cp_parser_template_id_expr (parser, template_keyword_p,
6201 check_dependency_p,
6202 declarator_p);
6203 /* If it worked, we're done. */
6204 if (cp_parser_parse_definitely (parser))
6205 return id;
6206 /* Otherwise, it's an ordinary identifier. */
6207 return cp_parser_identifier (parser);
6208 }
6209
6210 case CPP_TEMPLATE_ID:
6211 return cp_parser_template_id_expr (parser, template_keyword_p,
6212 check_dependency_p,
6213 declarator_p);
6214
6215 case CPP_COMPL:
6216 {
6217 tree type_decl;
6218 tree qualifying_scope;
6219 tree object_scope;
6220 tree scope;
6221 bool done;
6222 location_t tilde_loc = token->location;
6223
6224 /* Consume the `~' token. */
6225 cp_lexer_consume_token (parser->lexer);
6226 /* Parse the class-name. The standard, as written, seems to
6227 say that:
6228
6229 template <typename T> struct S { ~S (); };
6230 template <typename T> S<T>::~S() {}
6231
6232 is invalid, since `~' must be followed by a class-name, but
6233 `S<T>' is dependent, and so not known to be a class.
6234 That's not right; we need to look in uninstantiated
6235 templates. A further complication arises from:
6236
6237 template <typename T> void f(T t) {
6238 t.T::~T();
6239 }
6240
6241 Here, it is not possible to look up `T' in the scope of `T'
6242 itself. We must look in both the current scope, and the
6243 scope of the containing complete expression.
6244
6245 Yet another issue is:
6246
6247 struct S {
6248 int S;
6249 ~S();
6250 };
6251
6252 S::~S() {}
6253
6254 The standard does not seem to say that the `S' in `~S'
6255 should refer to the type `S' and not the data member
6256 `S::S'. */
6257
6258 /* DR 244 says that we look up the name after the "~" in the
6259 same scope as we looked up the qualifying name. That idea
6260 isn't fully worked out; it's more complicated than that. */
6261 scope = parser->scope;
6262 object_scope = parser->object_scope;
6263 qualifying_scope = parser->qualifying_scope;
6264
6265 /* Check for invalid scopes. */
6266 if (scope == error_mark_node)
6267 {
6268 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6269 cp_lexer_consume_token (parser->lexer);
6270 return error_mark_node;
6271 }
6272 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6273 {
6274 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6275 error_at (token->location,
6276 "scope %qT before %<~%> is not a class-name",
6277 scope);
6278 cp_parser_simulate_error (parser);
6279 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6280 cp_lexer_consume_token (parser->lexer);
6281 return error_mark_node;
6282 }
6283 if (template_keyword_p)
6284 {
6285 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6286 error_at (tilde_loc, "%<template%> keyword not permitted in "
6287 "destructor name");
6288 cp_parser_simulate_error (parser);
6289 return error_mark_node;
6290 }
6291
6292 gcc_assert (!scope || TYPE_P (scope));
6293
6294 token = cp_lexer_peek_token (parser->lexer);
6295
6296 /* Create a location with caret == start at the tilde,
6297 finishing at the end of the peeked token, e.g:
6298 ~token
6299 ^~~~~~. */
6300 location_t loc
6301 = make_location (tilde_loc, tilde_loc, token->location);
6302
6303 /* If the name is of the form "X::~X" it's OK even if X is a
6304 typedef. */
6305
6306 if (scope
6307 && token->type == CPP_NAME
6308 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6309 != CPP_LESS)
6310 && (token->u.value == TYPE_IDENTIFIER (scope)
6311 || (CLASS_TYPE_P (scope)
6312 && constructor_name_p (token->u.value, scope))))
6313 {
6314 cp_lexer_consume_token (parser->lexer);
6315 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6316 }
6317
6318 /* ~auto means the destructor of whatever the object is. */
6319 if (cp_parser_is_keyword (token, RID_AUTO))
6320 {
6321 if (cxx_dialect < cxx14)
6322 pedwarn (loc, 0,
6323 "%<~auto%> only available with "
6324 "%<-std=c++14%> or %<-std=gnu++14%>");
6325 cp_lexer_consume_token (parser->lexer);
6326 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6327 }
6328
6329 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6330 declarator-id of a constructor or destructor. */
6331 if (token->type == CPP_TEMPLATE_ID && cxx_dialect >= cxx20)
6332 {
6333 if (!cp_parser_simulate_error (parser))
6334 error_at (tilde_loc, "template-id not allowed for destructor");
6335 return error_mark_node;
6336 }
6337
6338 /* If there was an explicit qualification (S::~T), first look
6339 in the scope given by the qualification (i.e., S).
6340
6341 Note: in the calls to cp_parser_class_name below we pass
6342 typename_type so that lookup finds the injected-class-name
6343 rather than the constructor. */
6344 done = false;
6345 type_decl = NULL_TREE;
6346 if (scope)
6347 {
6348 cp_parser_parse_tentatively (parser);
6349 type_decl = cp_parser_class_name (parser,
6350 /*typename_keyword_p=*/false,
6351 /*template_keyword_p=*/false,
6352 typename_type,
6353 /*check_dependency=*/false,
6354 /*class_head_p=*/false,
6355 declarator_p);
6356 if (cp_parser_parse_definitely (parser))
6357 done = true;
6358 }
6359 /* In "N::S::~S", look in "N" as well. */
6360 if (!done && scope && qualifying_scope)
6361 {
6362 cp_parser_parse_tentatively (parser);
6363 parser->scope = qualifying_scope;
6364 parser->object_scope = NULL_TREE;
6365 parser->qualifying_scope = NULL_TREE;
6366 type_decl
6367 = cp_parser_class_name (parser,
6368 /*typename_keyword_p=*/false,
6369 /*template_keyword_p=*/false,
6370 typename_type,
6371 /*check_dependency=*/false,
6372 /*class_head_p=*/false,
6373 declarator_p);
6374 if (cp_parser_parse_definitely (parser))
6375 done = true;
6376 }
6377 /* In "p->S::~T", look in the scope given by "*p" as well. */
6378 else if (!done && object_scope)
6379 {
6380 cp_parser_parse_tentatively (parser);
6381 parser->scope = object_scope;
6382 parser->object_scope = NULL_TREE;
6383 parser->qualifying_scope = NULL_TREE;
6384 type_decl
6385 = cp_parser_class_name (parser,
6386 /*typename_keyword_p=*/false,
6387 /*template_keyword_p=*/false,
6388 typename_type,
6389 /*check_dependency=*/false,
6390 /*class_head_p=*/false,
6391 declarator_p);
6392 if (cp_parser_parse_definitely (parser))
6393 done = true;
6394 }
6395 /* Look in the surrounding context. */
6396 if (!done)
6397 {
6398 parser->scope = NULL_TREE;
6399 parser->object_scope = NULL_TREE;
6400 parser->qualifying_scope = NULL_TREE;
6401 if (processing_template_decl)
6402 cp_parser_parse_tentatively (parser);
6403 type_decl
6404 = cp_parser_class_name (parser,
6405 /*typename_keyword_p=*/false,
6406 /*template_keyword_p=*/false,
6407 typename_type,
6408 /*check_dependency=*/false,
6409 /*class_head_p=*/false,
6410 declarator_p);
6411 if (processing_template_decl
6412 && ! cp_parser_parse_definitely (parser))
6413 {
6414 /* We couldn't find a type with this name. If we're parsing
6415 tentatively, fail and try something else. */
6416 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6417 {
6418 cp_parser_simulate_error (parser);
6419 return error_mark_node;
6420 }
6421 /* Otherwise, accept it and check for a match at instantiation
6422 time. */
6423 type_decl = cp_parser_identifier (parser);
6424 if (type_decl != error_mark_node)
6425 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6426 return type_decl;
6427 }
6428 }
6429 /* If an error occurred, assume that the name of the
6430 destructor is the same as the name of the qualifying
6431 class. That allows us to keep parsing after running
6432 into ill-formed destructor names. */
6433 if (type_decl == error_mark_node && scope)
6434 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6435 else if (type_decl == error_mark_node)
6436 return error_mark_node;
6437
6438 /* Check that destructor name and scope match. */
6439 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6440 {
6441 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6442 error_at (loc,
6443 "declaration of %<~%T%> as member of %qT",
6444 type_decl, scope);
6445 cp_parser_simulate_error (parser);
6446 return error_mark_node;
6447 }
6448
6449 /* [class.dtor]
6450
6451 A typedef-name that names a class shall not be used as the
6452 identifier in the declarator for a destructor declaration. */
6453 if (declarator_p
6454 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6455 && !DECL_SELF_REFERENCE_P (type_decl)
6456 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6457 error_at (loc,
6458 "typedef-name %qD used as destructor declarator",
6459 type_decl);
6460
6461 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6462 }
6463
6464 case CPP_KEYWORD:
6465 if (token->keyword == RID_OPERATOR)
6466 {
6467 cp_expr id;
6468
6469 /* This could be a template-id, so we try that first. */
6470 cp_parser_parse_tentatively (parser);
6471 /* Try a template-id. */
6472 id = cp_parser_template_id_expr (parser, template_keyword_p,
6473 /*check_dependency_p=*/true,
6474 declarator_p);
6475 /* If that worked, we're done. */
6476 if (cp_parser_parse_definitely (parser))
6477 return id;
6478 /* We still don't know whether we're looking at an
6479 operator-function-id or a conversion-function-id. */
6480 cp_parser_parse_tentatively (parser);
6481 /* Try an operator-function-id. */
6482 id = cp_parser_operator_function_id (parser);
6483 /* If that didn't work, try a conversion-function-id. */
6484 if (!cp_parser_parse_definitely (parser))
6485 id = cp_parser_conversion_function_id (parser);
6486
6487 return id;
6488 }
6489 /* Fall through. */
6490
6491 default:
6492 if (optional_p)
6493 return NULL_TREE;
6494 cp_parser_error (parser, "expected unqualified-id");
6495 return error_mark_node;
6496 }
6497 }
6498
6499 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6500 be a template-id or the name shall refer to a class template or an
6501 alias template. */
6502
6503 static void
6504 check_template_keyword_in_nested_name_spec (tree name)
6505 {
6506 if (CLASS_TYPE_P (name)
6507 && ((CLASSTYPE_USE_TEMPLATE (name)
6508 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name)))
6509 || CLASSTYPE_IS_TEMPLATE (name)))
6510 return;
6511
6512 if (TREE_CODE (name) == TYPENAME_TYPE
6513 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR)
6514 return;
6515 /* Alias templates are also OK. */
6516 else if (alias_template_specialization_p (name, nt_opaque))
6517 return;
6518
6519 permerror (input_location, TYPE_P (name)
6520 ? G_("%qT is not a template")
6521 : G_("%qD is not a template"),
6522 name);
6523 }
6524
6525 /* Parse an (optional) nested-name-specifier.
6526
6527 nested-name-specifier: [C++98]
6528 class-or-namespace-name :: nested-name-specifier [opt]
6529 class-or-namespace-name :: template nested-name-specifier [opt]
6530
6531 nested-name-specifier: [C++0x]
6532 type-name ::
6533 namespace-name ::
6534 nested-name-specifier identifier ::
6535 nested-name-specifier template [opt] simple-template-id ::
6536
6537 PARSER->SCOPE should be set appropriately before this function is
6538 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6539 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6540 in name lookups.
6541
6542 Sets PARSER->SCOPE to the class (TYPE) or namespace
6543 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6544 it unchanged if there is no nested-name-specifier. Returns the new
6545 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6546
6547 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6548 part of a declaration and/or decl-specifier. */
6549
6550 static tree
6551 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6552 bool typename_keyword_p,
6553 bool check_dependency_p,
6554 bool type_p,
6555 bool is_declaration,
6556 bool template_keyword_p /* = false */)
6557 {
6558 bool success = false;
6559 cp_token_position start = 0;
6560 cp_token *token;
6561
6562 /* Remember where the nested-name-specifier starts. */
6563 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6564 && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6565 {
6566 start = cp_lexer_token_position (parser->lexer, false);
6567 push_deferring_access_checks (dk_deferred);
6568 }
6569
6570 while (true)
6571 {
6572 tree new_scope;
6573 tree old_scope;
6574 tree saved_qualifying_scope;
6575
6576 /* Spot cases that cannot be the beginning of a
6577 nested-name-specifier. */
6578 token = cp_lexer_peek_token (parser->lexer);
6579
6580 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6581 the already parsed nested-name-specifier. */
6582 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6583 {
6584 /* Grab the nested-name-specifier and continue the loop. */
6585 cp_parser_pre_parsed_nested_name_specifier (parser);
6586 /* If we originally encountered this nested-name-specifier
6587 with IS_DECLARATION set to false, we will not have
6588 resolved TYPENAME_TYPEs, so we must do so here. */
6589 if (is_declaration
6590 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6591 {
6592 new_scope = resolve_typename_type (parser->scope,
6593 /*only_current_p=*/false);
6594 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6595 parser->scope = new_scope;
6596 }
6597 success = true;
6598 continue;
6599 }
6600
6601 /* Spot cases that cannot be the beginning of a
6602 nested-name-specifier. On the second and subsequent times
6603 through the loop, we look for the `template' keyword. */
6604 if (success && token->keyword == RID_TEMPLATE)
6605 ;
6606 /* A template-id can start a nested-name-specifier. */
6607 else if (token->type == CPP_TEMPLATE_ID)
6608 ;
6609 /* DR 743: decltype can be used in a nested-name-specifier. */
6610 else if (token_is_decltype (token))
6611 ;
6612 else
6613 {
6614 /* If the next token is not an identifier, then it is
6615 definitely not a type-name or namespace-name. */
6616 if (token->type != CPP_NAME)
6617 break;
6618 /* If the following token is neither a `<' (to begin a
6619 template-id), nor a `::', then we are not looking at a
6620 nested-name-specifier. */
6621 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6622
6623 if (token->type == CPP_COLON
6624 && parser->colon_corrects_to_scope_p
6625 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6626 {
6627 gcc_rich_location richloc (token->location);
6628 richloc.add_fixit_replace ("::");
6629 error_at (&richloc,
6630 "found %<:%> in nested-name-specifier, "
6631 "expected %<::%>");
6632 token->type = CPP_SCOPE;
6633 }
6634
6635 if (token->type != CPP_SCOPE
6636 && !cp_parser_nth_token_starts_template_argument_list_p
6637 (parser, 2))
6638 break;
6639 }
6640
6641 /* The nested-name-specifier is optional, so we parse
6642 tentatively. */
6643 cp_parser_parse_tentatively (parser);
6644
6645 /* Look for the optional `template' keyword, if this isn't the
6646 first time through the loop. */
6647 if (success)
6648 {
6649 template_keyword_p = cp_parser_optional_template_keyword (parser);
6650 /* DR1710: "In a qualified-id used as the name in
6651 a typename-specifier, elaborated-type-specifier, using-declaration,
6652 or class-or-decltype, an optional keyword template appearing at
6653 the top level is ignored." */
6654 if (!template_keyword_p
6655 && typename_keyword_p
6656 && cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
6657 template_keyword_p = true;
6658 }
6659
6660 /* Save the old scope since the name lookup we are about to do
6661 might destroy it. */
6662 old_scope = parser->scope;
6663 saved_qualifying_scope = parser->qualifying_scope;
6664 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6665 look up names in "X<T>::I" in order to determine that "Y" is
6666 a template. So, if we have a typename at this point, we make
6667 an effort to look through it. */
6668 if (is_declaration
6669 && !typename_keyword_p
6670 && parser->scope
6671 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6672 parser->scope = resolve_typename_type (parser->scope,
6673 /*only_current_p=*/false);
6674 /* Parse the qualifying entity. */
6675 new_scope
6676 = cp_parser_qualifying_entity (parser,
6677 typename_keyword_p,
6678 template_keyword_p,
6679 check_dependency_p,
6680 type_p,
6681 is_declaration);
6682 /* Look for the `::' token. */
6683 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6684
6685 /* If we found what we wanted, we keep going; otherwise, we're
6686 done. */
6687 if (!cp_parser_parse_definitely (parser))
6688 {
6689 bool error_p = false;
6690
6691 /* Restore the OLD_SCOPE since it was valid before the
6692 failed attempt at finding the last
6693 class-or-namespace-name. */
6694 parser->scope = old_scope;
6695 parser->qualifying_scope = saved_qualifying_scope;
6696
6697 /* If the next token is a decltype, and the one after that is a
6698 `::', then the decltype has failed to resolve to a class or
6699 enumeration type. Give this error even when parsing
6700 tentatively since it can't possibly be valid--and we're going
6701 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6702 won't get another chance.*/
6703 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6704 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6705 == CPP_SCOPE))
6706 {
6707 token = cp_lexer_consume_token (parser->lexer);
6708 tree dtype = token->u.tree_check_value->value;
6709 if (dtype != error_mark_node)
6710 error_at (token->location, "%<decltype%> evaluates to %qT, "
6711 "which is not a class or enumeration type",
6712 dtype);
6713 parser->scope = error_mark_node;
6714 error_p = true;
6715 /* As below. */
6716 success = true;
6717 cp_lexer_consume_token (parser->lexer);
6718 }
6719
6720 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6721 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6722 {
6723 /* If we have a non-type template-id followed by ::, it can't
6724 possibly be valid. */
6725 token = cp_lexer_peek_token (parser->lexer);
6726 tree tid = token->u.tree_check_value->value;
6727 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6728 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6729 {
6730 tree tmpl = NULL_TREE;
6731 if (is_overloaded_fn (tid))
6732 {
6733 tree fns = get_fns (tid);
6734 if (OVL_SINGLE_P (fns))
6735 tmpl = OVL_FIRST (fns);
6736 if (function_concept_p (fns))
6737 error_at (token->location, "concept-id %qD "
6738 "in nested-name-specifier", tid);
6739 else
6740 error_at (token->location, "function template-id "
6741 "%qD in nested-name-specifier", tid);
6742 }
6743 else
6744 {
6745 tmpl = TREE_OPERAND (tid, 0);
6746 if (variable_concept_p (tmpl)
6747 || standard_concept_p (tmpl))
6748 error_at (token->location, "concept-id %qD "
6749 "in nested-name-specifier", tid);
6750 else
6751 {
6752 /* Variable template. */
6753 gcc_assert (variable_template_p (tmpl));
6754 error_at (token->location, "variable template-id "
6755 "%qD in nested-name-specifier", tid);
6756 }
6757 }
6758 if (tmpl)
6759 inform (DECL_SOURCE_LOCATION (tmpl),
6760 "%qD declared here", tmpl);
6761
6762 parser->scope = error_mark_node;
6763 error_p = true;
6764 /* As below. */
6765 success = true;
6766 cp_lexer_consume_token (parser->lexer);
6767 cp_lexer_consume_token (parser->lexer);
6768 }
6769 }
6770
6771 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6772 break;
6773 /* If the next token is an identifier, and the one after
6774 that is a `::', then any valid interpretation would have
6775 found a class-or-namespace-name. */
6776 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6777 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6778 == CPP_SCOPE)
6779 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6780 != CPP_COMPL))
6781 {
6782 token = cp_lexer_consume_token (parser->lexer);
6783 if (!error_p)
6784 {
6785 if (!token->error_reported)
6786 {
6787 tree decl;
6788 tree ambiguous_decls;
6789
6790 decl = cp_parser_lookup_name (parser, token->u.value,
6791 none_type,
6792 /*is_template=*/false,
6793 /*is_namespace=*/false,
6794 /*check_dependency=*/true,
6795 &ambiguous_decls,
6796 token->location);
6797 if (TREE_CODE (decl) == TEMPLATE_DECL)
6798 error_at (token->location,
6799 "%qD used without template arguments",
6800 decl);
6801 else if (ambiguous_decls)
6802 {
6803 // cp_parser_lookup_name has the same diagnostic,
6804 // thus make sure to emit it at most once.
6805 if (cp_parser_uncommitted_to_tentative_parse_p
6806 (parser))
6807 {
6808 error_at (token->location,
6809 "reference to %qD is ambiguous",
6810 token->u.value);
6811 print_candidates (ambiguous_decls);
6812 }
6813 decl = error_mark_node;
6814 }
6815 else
6816 {
6817 if (cxx_dialect != cxx98)
6818 cp_parser_name_lookup_error
6819 (parser, token->u.value, decl, NLE_NOT_CXX98,
6820 token->location);
6821 else
6822 cp_parser_name_lookup_error
6823 (parser, token->u.value, decl, NLE_CXX98,
6824 token->location);
6825 }
6826 }
6827 parser->scope = error_mark_node;
6828 error_p = true;
6829 /* Treat this as a successful nested-name-specifier
6830 due to:
6831
6832 [basic.lookup.qual]
6833
6834 If the name found is not a class-name (clause
6835 _class_) or namespace-name (_namespace.def_), the
6836 program is ill-formed. */
6837 success = true;
6838 }
6839 cp_lexer_consume_token (parser->lexer);
6840 }
6841 break;
6842 }
6843 /* We've found one valid nested-name-specifier. */
6844 success = true;
6845 /* Name lookup always gives us a DECL. */
6846 if (TREE_CODE (new_scope) == TYPE_DECL)
6847 new_scope = TREE_TYPE (new_scope);
6848 /* Uses of "template" must be followed by actual templates. */
6849 if (template_keyword_p)
6850 check_template_keyword_in_nested_name_spec (new_scope);
6851 /* If it is a class scope, try to complete it; we are about to
6852 be looking up names inside the class. */
6853 if (TYPE_P (new_scope)
6854 /* Since checking types for dependency can be expensive,
6855 avoid doing it if the type is already complete. */
6856 && !COMPLETE_TYPE_P (new_scope)
6857 /* Do not try to complete dependent types. */
6858 && !dependent_type_p (new_scope))
6859 {
6860 new_scope = complete_type (new_scope);
6861 /* If it is a typedef to current class, use the current
6862 class instead, as the typedef won't have any names inside
6863 it yet. */
6864 if (!COMPLETE_TYPE_P (new_scope)
6865 && currently_open_class (new_scope))
6866 new_scope = TYPE_MAIN_VARIANT (new_scope);
6867 }
6868 /* Make sure we look in the right scope the next time through
6869 the loop. */
6870 parser->scope = new_scope;
6871 }
6872
6873 /* If parsing tentatively, replace the sequence of tokens that makes
6874 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6875 token. That way, should we re-parse the token stream, we will
6876 not have to repeat the effort required to do the parse, nor will
6877 we issue duplicate error messages. */
6878 if (success && start)
6879 {
6880 cp_token *token;
6881
6882 token = cp_lexer_token_at (parser->lexer, start);
6883 /* Reset the contents of the START token. */
6884 token->type = CPP_NESTED_NAME_SPECIFIER;
6885 /* Retrieve any deferred checks. Do not pop this access checks yet
6886 so the memory will not be reclaimed during token replacing below. */
6887 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6888 token->tree_check_p = true;
6889 token->u.tree_check_value->value = parser->scope;
6890 token->u.tree_check_value->checks = get_deferred_access_checks ();
6891 token->u.tree_check_value->qualifying_scope =
6892 parser->qualifying_scope;
6893 token->keyword = RID_MAX;
6894
6895 /* Purge all subsequent tokens. */
6896 cp_lexer_purge_tokens_after (parser->lexer, start);
6897 }
6898
6899 if (start)
6900 pop_to_parent_deferring_access_checks ();
6901
6902 return success ? parser->scope : NULL_TREE;
6903 }
6904
6905 /* Parse a nested-name-specifier. See
6906 cp_parser_nested_name_specifier_opt for details. This function
6907 behaves identically, except that it will an issue an error if no
6908 nested-name-specifier is present. */
6909
6910 static tree
6911 cp_parser_nested_name_specifier (cp_parser *parser,
6912 bool typename_keyword_p,
6913 bool check_dependency_p,
6914 bool type_p,
6915 bool is_declaration)
6916 {
6917 tree scope;
6918
6919 /* Look for the nested-name-specifier. */
6920 scope = cp_parser_nested_name_specifier_opt (parser,
6921 typename_keyword_p,
6922 check_dependency_p,
6923 type_p,
6924 is_declaration);
6925 /* If it was not present, issue an error message. */
6926 if (!scope)
6927 {
6928 cp_parser_error (parser, "expected nested-name-specifier");
6929 parser->scope = NULL_TREE;
6930 }
6931
6932 return scope;
6933 }
6934
6935 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6936 this is either a class-name or a namespace-name (which corresponds
6937 to the class-or-namespace-name production in the grammar). For
6938 C++0x, it can also be a type-name that refers to an enumeration
6939 type or a simple-template-id.
6940
6941 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6942 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6943 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6944 TYPE_P is TRUE iff the next name should be taken as a class-name,
6945 even the same name is declared to be another entity in the same
6946 scope.
6947
6948 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6949 specified by the class-or-namespace-name. If neither is found the
6950 ERROR_MARK_NODE is returned. */
6951
6952 static tree
6953 cp_parser_qualifying_entity (cp_parser *parser,
6954 bool typename_keyword_p,
6955 bool template_keyword_p,
6956 bool check_dependency_p,
6957 bool type_p,
6958 bool is_declaration)
6959 {
6960 tree saved_scope;
6961 tree saved_qualifying_scope;
6962 tree saved_object_scope;
6963 tree scope;
6964 bool only_class_p;
6965 bool successful_parse_p;
6966
6967 /* DR 743: decltype can appear in a nested-name-specifier. */
6968 if (cp_lexer_next_token_is_decltype (parser->lexer))
6969 {
6970 scope = cp_parser_decltype (parser);
6971 if (TREE_CODE (scope) != ENUMERAL_TYPE
6972 && !MAYBE_CLASS_TYPE_P (scope))
6973 {
6974 cp_parser_simulate_error (parser);
6975 return error_mark_node;
6976 }
6977 if (TYPE_NAME (scope))
6978 scope = TYPE_NAME (scope);
6979 return scope;
6980 }
6981
6982 /* Before we try to parse the class-name, we must save away the
6983 current PARSER->SCOPE since cp_parser_class_name will destroy
6984 it. */
6985 saved_scope = parser->scope;
6986 saved_qualifying_scope = parser->qualifying_scope;
6987 saved_object_scope = parser->object_scope;
6988 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6989 there is no need to look for a namespace-name. */
6990 only_class_p = template_keyword_p
6991 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6992 if (!only_class_p)
6993 cp_parser_parse_tentatively (parser);
6994 scope = cp_parser_class_name (parser,
6995 typename_keyword_p,
6996 template_keyword_p,
6997 type_p ? class_type : none_type,
6998 check_dependency_p,
6999 /*class_head_p=*/false,
7000 is_declaration,
7001 /*enum_ok=*/cxx_dialect > cxx98);
7002 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
7003 /* If that didn't work, try for a namespace-name. */
7004 if (!only_class_p && !successful_parse_p)
7005 {
7006 /* Restore the saved scope. */
7007 parser->scope = saved_scope;
7008 parser->qualifying_scope = saved_qualifying_scope;
7009 parser->object_scope = saved_object_scope;
7010 /* If we are not looking at an identifier followed by the scope
7011 resolution operator, then this is not part of a
7012 nested-name-specifier. (Note that this function is only used
7013 to parse the components of a nested-name-specifier.) */
7014 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
7015 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
7016 return error_mark_node;
7017 scope = cp_parser_namespace_name (parser);
7018 }
7019
7020 return scope;
7021 }
7022
7023 /* Return true if we are looking at a compound-literal, false otherwise. */
7024
7025 static bool
7026 cp_parser_compound_literal_p (cp_parser *parser)
7027 {
7028 cp_lexer_save_tokens (parser->lexer);
7029
7030 /* Skip tokens until the next token is a closing parenthesis.
7031 If we find the closing `)', and the next token is a `{', then
7032 we are looking at a compound-literal. */
7033 bool compound_literal_p
7034 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7035 /*consume_paren=*/true)
7036 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7037
7038 /* Roll back the tokens we skipped. */
7039 cp_lexer_rollback_tokens (parser->lexer);
7040
7041 return compound_literal_p;
7042 }
7043
7044 /* Return true if EXPR is the integer constant zero or a complex constant
7045 of zero, without any folding, but ignoring location wrappers. */
7046
7047 bool
7048 literal_integer_zerop (const_tree expr)
7049 {
7050 return (location_wrapper_p (expr)
7051 && integer_zerop (TREE_OPERAND (expr, 0)));
7052 }
7053
7054 /* Parse a postfix-expression.
7055
7056 postfix-expression:
7057 primary-expression
7058 postfix-expression [ expression ]
7059 postfix-expression ( expression-list [opt] )
7060 simple-type-specifier ( expression-list [opt] )
7061 typename :: [opt] nested-name-specifier identifier
7062 ( expression-list [opt] )
7063 typename :: [opt] nested-name-specifier template [opt] template-id
7064 ( expression-list [opt] )
7065 postfix-expression . template [opt] id-expression
7066 postfix-expression -> template [opt] id-expression
7067 postfix-expression . pseudo-destructor-name
7068 postfix-expression -> pseudo-destructor-name
7069 postfix-expression ++
7070 postfix-expression --
7071 dynamic_cast < type-id > ( expression )
7072 static_cast < type-id > ( expression )
7073 reinterpret_cast < type-id > ( expression )
7074 const_cast < type-id > ( expression )
7075 typeid ( expression )
7076 typeid ( type-id )
7077
7078 GNU Extension:
7079
7080 postfix-expression:
7081 ( type-id ) { initializer-list , [opt] }
7082
7083 This extension is a GNU version of the C99 compound-literal
7084 construct. (The C99 grammar uses `type-name' instead of `type-id',
7085 but they are essentially the same concept.)
7086
7087 If ADDRESS_P is true, the postfix expression is the operand of the
7088 `&' operator. CAST_P is true if this expression is the target of a
7089 cast.
7090
7091 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
7092 class member access expressions [expr.ref].
7093
7094 Returns a representation of the expression. */
7095
7096 static cp_expr
7097 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
7098 bool member_access_only_p, bool decltype_p,
7099 cp_id_kind * pidk_return)
7100 {
7101 cp_token *token;
7102 location_t loc;
7103 enum rid keyword;
7104 cp_id_kind idk = CP_ID_KIND_NONE;
7105 cp_expr postfix_expression = NULL_TREE;
7106 bool is_member_access = false;
7107
7108 /* Peek at the next token. */
7109 token = cp_lexer_peek_token (parser->lexer);
7110 loc = token->location;
7111 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
7112
7113 /* Some of the productions are determined by keywords. */
7114 keyword = token->keyword;
7115 switch (keyword)
7116 {
7117 case RID_DYNCAST:
7118 case RID_STATCAST:
7119 case RID_REINTCAST:
7120 case RID_CONSTCAST:
7121 {
7122 tree type;
7123 cp_expr expression;
7124 const char *saved_message;
7125 bool saved_in_type_id_in_expr_p;
7126
7127 /* All of these can be handled in the same way from the point
7128 of view of parsing. Begin by consuming the token
7129 identifying the cast. */
7130 cp_lexer_consume_token (parser->lexer);
7131
7132 /* New types cannot be defined in the cast. */
7133 saved_message = parser->type_definition_forbidden_message;
7134 parser->type_definition_forbidden_message
7135 = G_("types may not be defined in casts");
7136
7137 /* Look for the opening `<'. */
7138 cp_parser_require (parser, CPP_LESS, RT_LESS);
7139 /* Parse the type to which we are casting. */
7140 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7141 parser->in_type_id_in_expr_p = true;
7142 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
7143 NULL);
7144 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7145 /* Look for the closing `>'. */
7146 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
7147 /* Restore the old message. */
7148 parser->type_definition_forbidden_message = saved_message;
7149
7150 bool saved_greater_than_is_operator_p
7151 = parser->greater_than_is_operator_p;
7152 parser->greater_than_is_operator_p = true;
7153
7154 /* And the expression which is being cast. */
7155 matching_parens parens;
7156 parens.require_open (parser);
7157 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
7158 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
7159 RT_CLOSE_PAREN);
7160 location_t end_loc = close_paren ?
7161 close_paren->location : UNKNOWN_LOCATION;
7162
7163 parser->greater_than_is_operator_p
7164 = saved_greater_than_is_operator_p;
7165
7166 /* Only type conversions to integral or enumeration types
7167 can be used in constant-expressions. */
7168 if (!cast_valid_in_integral_constant_expression_p (type)
7169 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
7170 {
7171 postfix_expression = error_mark_node;
7172 break;
7173 }
7174
7175 /* Construct a location e.g. :
7176 reinterpret_cast <int *> (expr)
7177 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7178 ranging from the start of the "*_cast" token to the final closing
7179 paren, with the caret at the start. */
7180 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
7181
7182 switch (keyword)
7183 {
7184 case RID_DYNCAST:
7185 postfix_expression
7186 = build_dynamic_cast (cp_cast_loc, type, expression,
7187 tf_warning_or_error);
7188 break;
7189 case RID_STATCAST:
7190 postfix_expression
7191 = build_static_cast (cp_cast_loc, type, expression,
7192 tf_warning_or_error);
7193 break;
7194 case RID_REINTCAST:
7195 postfix_expression
7196 = build_reinterpret_cast (cp_cast_loc, type, expression,
7197 tf_warning_or_error);
7198 break;
7199 case RID_CONSTCAST:
7200 postfix_expression
7201 = build_const_cast (cp_cast_loc, type, expression,
7202 tf_warning_or_error);
7203 break;
7204 default:
7205 gcc_unreachable ();
7206 }
7207 }
7208 break;
7209
7210 case RID_TYPEID:
7211 {
7212 tree type;
7213 const char *saved_message;
7214 bool saved_in_type_id_in_expr_p;
7215
7216 /* Consume the `typeid' token. */
7217 cp_lexer_consume_token (parser->lexer);
7218 /* Look for the `(' token. */
7219 matching_parens parens;
7220 parens.require_open (parser);
7221 /* Types cannot be defined in a `typeid' expression. */
7222 saved_message = parser->type_definition_forbidden_message;
7223 parser->type_definition_forbidden_message
7224 = G_("types may not be defined in a %<typeid%> expression");
7225 /* We can't be sure yet whether we're looking at a type-id or an
7226 expression. */
7227 cp_parser_parse_tentatively (parser);
7228 /* Try a type-id first. */
7229 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7230 parser->in_type_id_in_expr_p = true;
7231 type = cp_parser_type_id (parser);
7232 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7233 /* Look for the `)' token. Otherwise, we can't be sure that
7234 we're not looking at an expression: consider `typeid (int
7235 (3))', for example. */
7236 cp_token *close_paren = parens.require_close (parser);
7237 /* If all went well, simply lookup the type-id. */
7238 if (cp_parser_parse_definitely (parser))
7239 postfix_expression = get_typeid (type, tf_warning_or_error);
7240 /* Otherwise, fall back to the expression variant. */
7241 else
7242 {
7243 tree expression;
7244
7245 /* Look for an expression. */
7246 expression = cp_parser_expression (parser, & idk);
7247 /* Compute its typeid. */
7248 postfix_expression = build_typeid (expression, tf_warning_or_error);
7249 /* Look for the `)' token. */
7250 close_paren = parens.require_close (parser);
7251 }
7252 /* Restore the saved message. */
7253 parser->type_definition_forbidden_message = saved_message;
7254 /* `typeid' may not appear in an integral constant expression. */
7255 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
7256 postfix_expression = error_mark_node;
7257
7258 /* Construct a location e.g. :
7259 typeid (expr)
7260 ^~~~~~~~~~~~~
7261 ranging from the start of the "typeid" token to the final closing
7262 paren, with the caret at the start. */
7263 if (close_paren)
7264 {
7265 location_t typeid_loc
7266 = make_location (start_loc, start_loc, close_paren->location);
7267 postfix_expression.set_location (typeid_loc);
7268 postfix_expression.maybe_add_location_wrapper ();
7269 }
7270 }
7271 break;
7272
7273 case RID_TYPENAME:
7274 {
7275 tree type;
7276 /* The syntax permitted here is the same permitted for an
7277 elaborated-type-specifier. */
7278 ++parser->prevent_constrained_type_specifiers;
7279 type = cp_parser_elaborated_type_specifier (parser,
7280 /*is_friend=*/false,
7281 /*is_declaration=*/false);
7282 --parser->prevent_constrained_type_specifiers;
7283 postfix_expression = cp_parser_functional_cast (parser, type);
7284 }
7285 break;
7286
7287 case RID_ADDRESSOF:
7288 case RID_BUILTIN_SHUFFLE:
7289 case RID_BUILTIN_LAUNDER:
7290 {
7291 vec<tree, va_gc> *vec;
7292 unsigned int i;
7293 tree p;
7294
7295 cp_lexer_consume_token (parser->lexer);
7296 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7297 /*cast_p=*/false, /*allow_expansion_p=*/true,
7298 /*non_constant_p=*/NULL);
7299 if (vec == NULL)
7300 {
7301 postfix_expression = error_mark_node;
7302 break;
7303 }
7304
7305 FOR_EACH_VEC_ELT (*vec, i, p)
7306 mark_exp_read (p);
7307
7308 switch (keyword)
7309 {
7310 case RID_ADDRESSOF:
7311 if (vec->length () == 1)
7312 postfix_expression
7313 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7314 else
7315 {
7316 error_at (loc, "wrong number of arguments to "
7317 "%<__builtin_addressof%>");
7318 postfix_expression = error_mark_node;
7319 }
7320 break;
7321
7322 case RID_BUILTIN_LAUNDER:
7323 if (vec->length () == 1)
7324 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7325 tf_warning_or_error);
7326 else
7327 {
7328 error_at (loc, "wrong number of arguments to "
7329 "%<__builtin_launder%>");
7330 postfix_expression = error_mark_node;
7331 }
7332 break;
7333
7334 case RID_BUILTIN_SHUFFLE:
7335 if (vec->length () == 2)
7336 postfix_expression
7337 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7338 (*vec)[1], tf_warning_or_error);
7339 else if (vec->length () == 3)
7340 postfix_expression
7341 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7342 (*vec)[2], tf_warning_or_error);
7343 else
7344 {
7345 error_at (loc, "wrong number of arguments to "
7346 "%<__builtin_shuffle%>");
7347 postfix_expression = error_mark_node;
7348 }
7349 break;
7350
7351 default:
7352 gcc_unreachable ();
7353 }
7354 break;
7355 }
7356
7357 case RID_BUILTIN_CONVERTVECTOR:
7358 {
7359 tree expression;
7360 tree type;
7361 /* Consume the `__builtin_convertvector' token. */
7362 cp_lexer_consume_token (parser->lexer);
7363 /* Look for the opening `('. */
7364 matching_parens parens;
7365 parens.require_open (parser);
7366 /* Now, parse the assignment-expression. */
7367 expression = cp_parser_assignment_expression (parser);
7368 /* Look for the `,'. */
7369 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7370 location_t type_location
7371 = cp_lexer_peek_token (parser->lexer)->location;
7372 /* Parse the type-id. */
7373 {
7374 type_id_in_expr_sentinel s (parser);
7375 type = cp_parser_type_id (parser);
7376 }
7377 /* Look for the closing `)'. */
7378 parens.require_close (parser);
7379 return cp_build_vec_convert (expression, type_location, type,
7380 tf_warning_or_error);
7381 }
7382
7383 case RID_BUILTIN_BIT_CAST:
7384 {
7385 tree expression;
7386 tree type;
7387 /* Consume the `__builtin_bit_cast' token. */
7388 cp_lexer_consume_token (parser->lexer);
7389 /* Look for the opening `('. */
7390 matching_parens parens;
7391 parens.require_open (parser);
7392 location_t type_location
7393 = cp_lexer_peek_token (parser->lexer)->location;
7394 /* Parse the type-id. */
7395 {
7396 type_id_in_expr_sentinel s (parser);
7397 type = cp_parser_type_id (parser);
7398 }
7399 /* Look for the `,'. */
7400 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7401 /* Now, parse the assignment-expression. */
7402 expression = cp_parser_assignment_expression (parser);
7403 /* Look for the closing `)'. */
7404 parens.require_close (parser);
7405 return cp_build_bit_cast (type_location, type, expression,
7406 tf_warning_or_error);
7407 }
7408
7409 default:
7410 {
7411 tree type;
7412
7413 /* If the next thing is a simple-type-specifier, we may be
7414 looking at a functional cast. We could also be looking at
7415 an id-expression. So, we try the functional cast, and if
7416 that doesn't work we fall back to the primary-expression. */
7417 cp_parser_parse_tentatively (parser);
7418 /* Look for the simple-type-specifier. */
7419 ++parser->prevent_constrained_type_specifiers;
7420 type = cp_parser_simple_type_specifier (parser,
7421 /*decl_specs=*/NULL,
7422 CP_PARSER_FLAGS_NONE);
7423 --parser->prevent_constrained_type_specifiers;
7424 /* Parse the cast itself. */
7425 if (!cp_parser_error_occurred (parser))
7426 postfix_expression
7427 = cp_parser_functional_cast (parser, type);
7428 /* If that worked, we're done. */
7429 if (cp_parser_parse_definitely (parser))
7430 break;
7431
7432 /* If the functional-cast didn't work out, try a
7433 compound-literal. */
7434 if (cp_parser_allow_gnu_extensions_p (parser)
7435 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7436 {
7437 cp_expr initializer = NULL_TREE;
7438
7439 cp_parser_parse_tentatively (parser);
7440
7441 matching_parens parens;
7442 parens.consume_open (parser);
7443
7444 /* Avoid calling cp_parser_type_id pointlessly, see comment
7445 in cp_parser_cast_expression about c++/29234. */
7446 if (!cp_parser_compound_literal_p (parser))
7447 cp_parser_simulate_error (parser);
7448 else
7449 {
7450 /* Parse the type. */
7451 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7452 parser->in_type_id_in_expr_p = true;
7453 type = cp_parser_type_id (parser);
7454 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7455 parens.require_close (parser);
7456 }
7457
7458 /* If things aren't going well, there's no need to
7459 keep going. */
7460 if (!cp_parser_error_occurred (parser))
7461 {
7462 bool non_constant_p;
7463 /* Parse the brace-enclosed initializer list. */
7464 initializer = cp_parser_braced_list (parser,
7465 &non_constant_p);
7466 }
7467 /* If that worked, we're definitely looking at a
7468 compound-literal expression. */
7469 if (cp_parser_parse_definitely (parser))
7470 {
7471 /* Warn the user that a compound literal is not
7472 allowed in standard C++. */
7473 pedwarn (input_location, OPT_Wpedantic,
7474 "ISO C++ forbids compound-literals");
7475 /* For simplicity, we disallow compound literals in
7476 constant-expressions. We could
7477 allow compound literals of integer type, whose
7478 initializer was a constant, in constant
7479 expressions. Permitting that usage, as a further
7480 extension, would not change the meaning of any
7481 currently accepted programs. (Of course, as
7482 compound literals are not part of ISO C++, the
7483 standard has nothing to say.) */
7484 if (cp_parser_non_integral_constant_expression (parser,
7485 NIC_NCC))
7486 {
7487 postfix_expression = error_mark_node;
7488 break;
7489 }
7490 /* Form the representation of the compound-literal. */
7491 postfix_expression
7492 = finish_compound_literal (type, initializer,
7493 tf_warning_or_error, fcl_c99);
7494 postfix_expression.set_location (initializer.get_location ());
7495 break;
7496 }
7497 }
7498
7499 /* It must be a primary-expression. */
7500 postfix_expression
7501 = cp_parser_primary_expression (parser, address_p, cast_p,
7502 /*template_arg_p=*/false,
7503 decltype_p,
7504 &idk);
7505 }
7506 break;
7507 }
7508
7509 /* Note that we don't need to worry about calling build_cplus_new on a
7510 class-valued CALL_EXPR in decltype when it isn't the end of the
7511 postfix-expression; unary_complex_lvalue will take care of that for
7512 all these cases. */
7513
7514 /* Keep looping until the postfix-expression is complete. */
7515 while (true)
7516 {
7517 if (idk == CP_ID_KIND_UNQUALIFIED
7518 && identifier_p (postfix_expression)
7519 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7520 /* It is not a Koenig lookup function call. */
7521 postfix_expression
7522 = unqualified_name_lookup_error (postfix_expression);
7523
7524 /* Peek at the next token. */
7525 token = cp_lexer_peek_token (parser->lexer);
7526
7527 switch (token->type)
7528 {
7529 case CPP_OPEN_SQUARE:
7530 if (cp_next_tokens_can_be_std_attribute_p (parser))
7531 {
7532 cp_parser_error (parser,
7533 "two consecutive %<[%> shall "
7534 "only introduce an attribute");
7535 return error_mark_node;
7536 }
7537 postfix_expression
7538 = cp_parser_postfix_open_square_expression (parser,
7539 postfix_expression,
7540 false,
7541 decltype_p);
7542 postfix_expression.set_range (start_loc,
7543 postfix_expression.get_location ());
7544
7545 idk = CP_ID_KIND_NONE;
7546 is_member_access = false;
7547 break;
7548
7549 case CPP_OPEN_PAREN:
7550 /* postfix-expression ( expression-list [opt] ) */
7551 {
7552 bool koenig_p;
7553 bool is_builtin_constant_p;
7554 bool saved_integral_constant_expression_p = false;
7555 bool saved_non_integral_constant_expression_p = false;
7556 tsubst_flags_t complain = complain_flags (decltype_p);
7557 vec<tree, va_gc> *args;
7558 location_t close_paren_loc = UNKNOWN_LOCATION;
7559
7560 is_member_access = false;
7561
7562 tree stripped_expression
7563 = tree_strip_any_location_wrapper (postfix_expression);
7564 is_builtin_constant_p
7565 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7566 if (is_builtin_constant_p)
7567 {
7568 /* The whole point of __builtin_constant_p is to allow
7569 non-constant expressions to appear as arguments. */
7570 saved_integral_constant_expression_p
7571 = parser->integral_constant_expression_p;
7572 saved_non_integral_constant_expression_p
7573 = parser->non_integral_constant_expression_p;
7574 parser->integral_constant_expression_p = false;
7575 }
7576 args = (cp_parser_parenthesized_expression_list
7577 (parser, non_attr,
7578 /*cast_p=*/false, /*allow_expansion_p=*/true,
7579 /*non_constant_p=*/NULL,
7580 /*close_paren_loc=*/&close_paren_loc,
7581 /*wrap_locations_p=*/true));
7582 if (is_builtin_constant_p)
7583 {
7584 parser->integral_constant_expression_p
7585 = saved_integral_constant_expression_p;
7586 parser->non_integral_constant_expression_p
7587 = saved_non_integral_constant_expression_p;
7588 }
7589
7590 if (args == NULL)
7591 {
7592 postfix_expression = error_mark_node;
7593 break;
7594 }
7595
7596 /* Function calls are not permitted in
7597 constant-expressions. */
7598 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7599 && cp_parser_non_integral_constant_expression (parser,
7600 NIC_FUNC_CALL))
7601 {
7602 postfix_expression = error_mark_node;
7603 release_tree_vector (args);
7604 break;
7605 }
7606
7607 koenig_p = false;
7608 if (idk == CP_ID_KIND_UNQUALIFIED
7609 || idk == CP_ID_KIND_TEMPLATE_ID)
7610 {
7611 if (identifier_p (postfix_expression)
7612 /* In C++20, we may need to perform ADL for a template
7613 name. */
7614 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7615 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7616 {
7617 if (!args->is_empty ())
7618 {
7619 koenig_p = true;
7620 if (!any_type_dependent_arguments_p (args))
7621 postfix_expression
7622 = perform_koenig_lookup (postfix_expression, args,
7623 complain);
7624 }
7625 else
7626 postfix_expression
7627 = unqualified_fn_lookup_error (postfix_expression);
7628 }
7629 /* We do not perform argument-dependent lookup if
7630 normal lookup finds a non-function, in accordance
7631 with the expected resolution of DR 218. */
7632 else if (!args->is_empty ()
7633 && is_overloaded_fn (postfix_expression))
7634 {
7635 /* Do not do argument dependent lookup if regular
7636 lookup finds a member function or a block-scope
7637 function declaration. [basic.lookup.argdep]/3 */
7638 bool do_adl_p = true;
7639 tree fns = get_fns (postfix_expression);
7640 for (lkp_iterator iter (fns); iter; ++iter)
7641 {
7642 tree fn = STRIP_TEMPLATE (*iter);
7643 if ((TREE_CODE (fn) == USING_DECL
7644 && DECL_DEPENDENT_P (fn))
7645 || DECL_FUNCTION_MEMBER_P (fn)
7646 || DECL_LOCAL_DECL_P (fn))
7647 {
7648 do_adl_p = false;
7649 break;
7650 }
7651 }
7652
7653 if (do_adl_p)
7654 {
7655 koenig_p = true;
7656 if (!any_type_dependent_arguments_p (args))
7657 postfix_expression
7658 = perform_koenig_lookup (postfix_expression, args,
7659 complain);
7660 }
7661 }
7662 }
7663
7664 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7665 {
7666 tree instance = TREE_OPERAND (postfix_expression, 0);
7667 tree fn = TREE_OPERAND (postfix_expression, 1);
7668
7669 if (processing_template_decl
7670 && (type_dependent_object_expression_p (instance)
7671 || (!BASELINK_P (fn)
7672 && TREE_CODE (fn) != FIELD_DECL)
7673 || type_dependent_expression_p (fn)
7674 || any_type_dependent_arguments_p (args)))
7675 {
7676 maybe_generic_this_capture (instance, fn);
7677 postfix_expression
7678 = build_min_nt_call_vec (postfix_expression, args);
7679 }
7680 else if (BASELINK_P (fn))
7681 {
7682 postfix_expression
7683 = (build_new_method_call
7684 (instance, fn, &args, NULL_TREE,
7685 (idk == CP_ID_KIND_QUALIFIED
7686 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7687 : LOOKUP_NORMAL),
7688 /*fn_p=*/NULL,
7689 complain));
7690 }
7691 else
7692 postfix_expression
7693 = finish_call_expr (postfix_expression, &args,
7694 /*disallow_virtual=*/false,
7695 /*koenig_p=*/false,
7696 complain);
7697 }
7698 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7699 || TREE_CODE (postfix_expression) == MEMBER_REF
7700 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7701 postfix_expression = (build_offset_ref_call_from_tree
7702 (postfix_expression, &args,
7703 complain));
7704 else if (idk == CP_ID_KIND_QUALIFIED)
7705 /* A call to a static class member, or a namespace-scope
7706 function. */
7707 postfix_expression
7708 = finish_call_expr (postfix_expression, &args,
7709 /*disallow_virtual=*/true,
7710 koenig_p,
7711 complain);
7712 else
7713 /* All other function calls. */
7714 postfix_expression
7715 = finish_call_expr (postfix_expression, &args,
7716 /*disallow_virtual=*/false,
7717 koenig_p,
7718 complain);
7719
7720 if (close_paren_loc != UNKNOWN_LOCATION)
7721 {
7722 location_t combined_loc = make_location (token->location,
7723 start_loc,
7724 close_paren_loc);
7725 postfix_expression.set_location (combined_loc);
7726 }
7727
7728 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7729 idk = CP_ID_KIND_NONE;
7730
7731 release_tree_vector (args);
7732 }
7733 break;
7734
7735 case CPP_DOT:
7736 case CPP_DEREF:
7737 /* postfix-expression . template [opt] id-expression
7738 postfix-expression . pseudo-destructor-name
7739 postfix-expression -> template [opt] id-expression
7740 postfix-expression -> pseudo-destructor-name */
7741
7742 /* Consume the `.' or `->' operator. */
7743 cp_lexer_consume_token (parser->lexer);
7744
7745 postfix_expression
7746 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7747 postfix_expression,
7748 false, &idk, loc);
7749
7750 is_member_access = true;
7751 break;
7752
7753 case CPP_PLUS_PLUS:
7754 /* postfix-expression ++ */
7755 /* Consume the `++' token. */
7756 cp_lexer_consume_token (parser->lexer);
7757 /* Generate a representation for the complete expression. */
7758 postfix_expression
7759 = finish_increment_expr (postfix_expression,
7760 POSTINCREMENT_EXPR);
7761 /* Increments may not appear in constant-expressions. */
7762 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7763 postfix_expression = error_mark_node;
7764 idk = CP_ID_KIND_NONE;
7765 is_member_access = false;
7766 break;
7767
7768 case CPP_MINUS_MINUS:
7769 /* postfix-expression -- */
7770 /* Consume the `--' token. */
7771 cp_lexer_consume_token (parser->lexer);
7772 /* Generate a representation for the complete expression. */
7773 postfix_expression
7774 = finish_increment_expr (postfix_expression,
7775 POSTDECREMENT_EXPR);
7776 /* Decrements may not appear in constant-expressions. */
7777 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7778 postfix_expression = error_mark_node;
7779 idk = CP_ID_KIND_NONE;
7780 is_member_access = false;
7781 break;
7782
7783 default:
7784 if (pidk_return != NULL)
7785 * pidk_return = idk;
7786 if (member_access_only_p)
7787 return is_member_access
7788 ? postfix_expression
7789 : cp_expr (error_mark_node);
7790 else
7791 return postfix_expression;
7792 }
7793 }
7794
7795 /* We should never get here. */
7796 gcc_unreachable ();
7797 return error_mark_node;
7798 }
7799
7800 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7801 by cp_parser_builtin_offsetof. We're looking for
7802
7803 postfix-expression [ expression ]
7804 postfix-expression [ braced-init-list ] (C++11)
7805
7806 FOR_OFFSETOF is set if we're being called in that context, which
7807 changes how we deal with integer constant expressions. */
7808
7809 static tree
7810 cp_parser_postfix_open_square_expression (cp_parser *parser,
7811 tree postfix_expression,
7812 bool for_offsetof,
7813 bool decltype_p)
7814 {
7815 tree index = NULL_TREE;
7816 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7817 bool saved_greater_than_is_operator_p;
7818
7819 /* Consume the `[' token. */
7820 cp_lexer_consume_token (parser->lexer);
7821
7822 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7823 parser->greater_than_is_operator_p = true;
7824
7825 /* Parse the index expression. */
7826 /* ??? For offsetof, there is a question of what to allow here. If
7827 offsetof is not being used in an integral constant expression context,
7828 then we *could* get the right answer by computing the value at runtime.
7829 If we are in an integral constant expression context, then we might
7830 could accept any constant expression; hard to say without analysis.
7831 Rather than open the barn door too wide right away, allow only integer
7832 constant expressions here. */
7833 if (for_offsetof)
7834 index = cp_parser_constant_expression (parser);
7835 else
7836 {
7837 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7838 {
7839 bool expr_nonconst_p;
7840 cp_lexer_set_source_position (parser->lexer);
7841 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7842 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7843 }
7844 else
7845 index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
7846 /*decltype_p=*/false,
7847 /*warn_comma_p=*/warn_comma_subscript);
7848 }
7849
7850 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7851
7852 /* Look for the closing `]'. */
7853 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7854
7855 /* Build the ARRAY_REF. */
7856 postfix_expression = grok_array_decl (loc, postfix_expression,
7857 index, decltype_p);
7858
7859 /* When not doing offsetof, array references are not permitted in
7860 constant-expressions. */
7861 if (!for_offsetof
7862 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7863 postfix_expression = error_mark_node;
7864
7865 return postfix_expression;
7866 }
7867
7868 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7869 dereference of incomplete type, returns true if error_mark_node should
7870 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7871 and *DEPENDENT_P. */
7872
7873 bool
7874 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7875 bool *dependent_p)
7876 {
7877 /* In a template, be permissive by treating an object expression
7878 of incomplete type as dependent (after a pedwarn). */
7879 diagnostic_t kind = (processing_template_decl
7880 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7881
7882 switch (TREE_CODE (*postfix_expression))
7883 {
7884 case CAST_EXPR:
7885 case REINTERPRET_CAST_EXPR:
7886 case CONST_CAST_EXPR:
7887 case STATIC_CAST_EXPR:
7888 case DYNAMIC_CAST_EXPR:
7889 case IMPLICIT_CONV_EXPR:
7890 case VIEW_CONVERT_EXPR:
7891 case NON_LVALUE_EXPR:
7892 kind = DK_ERROR;
7893 break;
7894 case OVERLOAD:
7895 /* Don't emit any diagnostic for OVERLOADs. */
7896 kind = DK_IGNORED;
7897 break;
7898 default:
7899 /* Avoid clobbering e.g. DECLs. */
7900 if (!EXPR_P (*postfix_expression))
7901 kind = DK_ERROR;
7902 break;
7903 }
7904
7905 if (kind == DK_IGNORED)
7906 return false;
7907
7908 location_t exploc = location_of (*postfix_expression);
7909 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7910 if (!MAYBE_CLASS_TYPE_P (*scope))
7911 return true;
7912 if (kind == DK_ERROR)
7913 *scope = *postfix_expression = error_mark_node;
7914 else if (processing_template_decl)
7915 {
7916 *dependent_p = true;
7917 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7918 }
7919 return false;
7920 }
7921
7922 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7923 by cp_parser_builtin_offsetof. We're looking for
7924
7925 postfix-expression . template [opt] id-expression
7926 postfix-expression . pseudo-destructor-name
7927 postfix-expression -> template [opt] id-expression
7928 postfix-expression -> pseudo-destructor-name
7929
7930 FOR_OFFSETOF is set if we're being called in that context. That sorta
7931 limits what of the above we'll actually accept, but nevermind.
7932 TOKEN_TYPE is the "." or "->" token, which will already have been
7933 removed from the stream. */
7934
7935 static tree
7936 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7937 enum cpp_ttype token_type,
7938 cp_expr postfix_expression,
7939 bool for_offsetof, cp_id_kind *idk,
7940 location_t location)
7941 {
7942 tree name;
7943 bool dependent_p;
7944 bool pseudo_destructor_p;
7945 tree scope = NULL_TREE;
7946 location_t start_loc = postfix_expression.get_start ();
7947
7948 /* If this is a `->' operator, dereference the pointer. */
7949 if (token_type == CPP_DEREF)
7950 postfix_expression = build_x_arrow (location, postfix_expression,
7951 tf_warning_or_error);
7952 /* Check to see whether or not the expression is type-dependent and
7953 not the current instantiation. */
7954 dependent_p = type_dependent_object_expression_p (postfix_expression);
7955 /* The identifier following the `->' or `.' is not qualified. */
7956 parser->scope = NULL_TREE;
7957 parser->qualifying_scope = NULL_TREE;
7958 parser->object_scope = NULL_TREE;
7959 *idk = CP_ID_KIND_NONE;
7960
7961 /* Enter the scope corresponding to the type of the object
7962 given by the POSTFIX_EXPRESSION. */
7963 if (!dependent_p)
7964 {
7965 scope = TREE_TYPE (postfix_expression);
7966 /* According to the standard, no expression should ever have
7967 reference type. Unfortunately, we do not currently match
7968 the standard in this respect in that our internal representation
7969 of an expression may have reference type even when the standard
7970 says it does not. Therefore, we have to manually obtain the
7971 underlying type here. */
7972 scope = non_reference (scope);
7973 /* The type of the POSTFIX_EXPRESSION must be complete. */
7974 /* Unlike the object expression in other contexts, *this is not
7975 required to be of complete type for purposes of class member
7976 access (5.2.5) outside the member function body. */
7977 if (postfix_expression != current_class_ref
7978 && scope != error_mark_node
7979 && !currently_open_class (scope))
7980 {
7981 scope = complete_type (scope);
7982 if (!COMPLETE_TYPE_P (scope)
7983 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7984 &dependent_p))
7985 return error_mark_node;
7986 }
7987
7988 if (!dependent_p)
7989 {
7990 /* Let the name lookup machinery know that we are processing a
7991 class member access expression. */
7992 parser->context->object_type = scope;
7993 /* If something went wrong, we want to be able to discern that case,
7994 as opposed to the case where there was no SCOPE due to the type
7995 of expression being dependent. */
7996 if (!scope)
7997 scope = error_mark_node;
7998 /* If the SCOPE was erroneous, make the various semantic analysis
7999 functions exit quickly -- and without issuing additional error
8000 messages. */
8001 if (scope == error_mark_node)
8002 postfix_expression = error_mark_node;
8003 }
8004 }
8005
8006 if (dependent_p)
8007 {
8008 tree type = TREE_TYPE (postfix_expression);
8009 /* If we don't have a (type-dependent) object of class type, use
8010 typeof to figure out the type of the object. */
8011 if (type == NULL_TREE)
8012 type = finish_typeof (postfix_expression);
8013 parser->context->object_type = type;
8014 }
8015
8016 /* Assume this expression is not a pseudo-destructor access. */
8017 pseudo_destructor_p = false;
8018
8019 /* If the SCOPE is a scalar type, then, if this is a valid program,
8020 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
8021 is type dependent, it can be pseudo-destructor-name or something else.
8022 Try to parse it as pseudo-destructor-name first. */
8023 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
8024 {
8025 tree s;
8026 tree type;
8027
8028 cp_parser_parse_tentatively (parser);
8029 /* Parse the pseudo-destructor-name. */
8030 s = NULL_TREE;
8031 cp_parser_pseudo_destructor_name (parser, postfix_expression,
8032 &s, &type);
8033 if (dependent_p
8034 && (cp_parser_error_occurred (parser)
8035 || !SCALAR_TYPE_P (type)))
8036 cp_parser_abort_tentative_parse (parser);
8037 else if (cp_parser_parse_definitely (parser))
8038 {
8039 pseudo_destructor_p = true;
8040 postfix_expression
8041 = finish_pseudo_destructor_expr (postfix_expression,
8042 s, type, location);
8043 }
8044 }
8045
8046 if (!pseudo_destructor_p)
8047 {
8048 /* If the SCOPE is not a scalar type, we are looking at an
8049 ordinary class member access expression, rather than a
8050 pseudo-destructor-name. */
8051 bool template_p;
8052 cp_token *token = cp_lexer_peek_token (parser->lexer);
8053 /* Parse the id-expression. */
8054 name = (cp_parser_id_expression
8055 (parser,
8056 cp_parser_optional_template_keyword (parser),
8057 /*check_dependency_p=*/true,
8058 &template_p,
8059 /*declarator_p=*/false,
8060 /*optional_p=*/false));
8061 /* In general, build a SCOPE_REF if the member name is qualified.
8062 However, if the name was not dependent and has already been
8063 resolved; there is no need to build the SCOPE_REF. For example;
8064
8065 struct X { void f(); };
8066 template <typename T> void f(T* t) { t->X::f(); }
8067
8068 Even though "t" is dependent, "X::f" is not and has been resolved
8069 to a BASELINK; there is no need to include scope information. */
8070
8071 /* But we do need to remember that there was an explicit scope for
8072 virtual function calls. */
8073 if (parser->scope)
8074 *idk = CP_ID_KIND_QUALIFIED;
8075
8076 /* If the name is a template-id that names a type, we will get a
8077 TYPE_DECL here. That is invalid code. */
8078 if (TREE_CODE (name) == TYPE_DECL)
8079 {
8080 error_at (token->location, "invalid use of %qD", name);
8081 postfix_expression = error_mark_node;
8082 }
8083 else
8084 {
8085 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
8086 {
8087 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
8088 {
8089 error_at (token->location, "%<%D::%D%> is not a class member",
8090 parser->scope, name);
8091 postfix_expression = error_mark_node;
8092 }
8093 else
8094 name = build_qualified_name (/*type=*/NULL_TREE,
8095 parser->scope,
8096 name,
8097 template_p);
8098 parser->scope = NULL_TREE;
8099 parser->qualifying_scope = NULL_TREE;
8100 parser->object_scope = NULL_TREE;
8101 }
8102 if (parser->scope && name && BASELINK_P (name))
8103 adjust_result_of_qualified_name_lookup
8104 (name, parser->scope, scope);
8105 postfix_expression
8106 = finish_class_member_access_expr (postfix_expression, name,
8107 template_p,
8108 tf_warning_or_error);
8109 /* Build a location e.g.:
8110 ptr->access_expr
8111 ~~~^~~~~~~~~~~~~
8112 where the caret is at the deref token, ranging from
8113 the start of postfix_expression to the end of the access expr. */
8114 location_t combined_loc
8115 = make_location (input_location, start_loc, parser->lexer);
8116 protected_set_expr_location (postfix_expression, combined_loc);
8117 }
8118 }
8119
8120 /* We no longer need to look up names in the scope of the object on
8121 the left-hand side of the `.' or `->' operator. */
8122 parser->context->object_type = NULL_TREE;
8123
8124 /* Outside of offsetof, these operators may not appear in
8125 constant-expressions. */
8126 if (!for_offsetof
8127 && (cp_parser_non_integral_constant_expression
8128 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
8129 postfix_expression = error_mark_node;
8130
8131 return postfix_expression;
8132 }
8133
8134 /* Parse a parenthesized expression-list.
8135
8136 expression-list:
8137 assignment-expression
8138 expression-list, assignment-expression
8139
8140 attribute-list:
8141 expression-list
8142 identifier
8143 identifier, expression-list
8144
8145 CAST_P is true if this expression is the target of a cast.
8146
8147 ALLOW_EXPANSION_P is true if this expression allows expansion of an
8148 argument pack.
8149
8150 WRAP_LOCATIONS_P is true if expressions within this list for which
8151 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
8152 their source locations.
8153
8154 Returns a vector of trees. Each element is a representation of an
8155 assignment-expression. NULL is returned if the ( and or ) are
8156 missing. An empty, but allocated, vector is returned on no
8157 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
8158 if we are parsing an attribute list for an attribute that wants a
8159 plain identifier argument, normal_attr for an attribute that wants
8160 an expression, or non_attr if we aren't parsing an attribute list. If
8161 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
8162 not all of the expressions in the list were constant.
8163 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
8164 will be written to with the location of the closing parenthesis. If
8165 an error occurs, it may or may not be written to. */
8166
8167 static vec<tree, va_gc> *
8168 cp_parser_parenthesized_expression_list (cp_parser* parser,
8169 int is_attribute_list,
8170 bool cast_p,
8171 bool allow_expansion_p,
8172 bool *non_constant_p,
8173 location_t *close_paren_loc,
8174 bool wrap_locations_p)
8175 {
8176 vec<tree, va_gc> *expression_list;
8177 bool fold_expr_p = is_attribute_list != non_attr;
8178 tree identifier = NULL_TREE;
8179 bool saved_greater_than_is_operator_p;
8180
8181 /* Assume all the expressions will be constant. */
8182 if (non_constant_p)
8183 *non_constant_p = false;
8184
8185 matching_parens parens;
8186 if (!parens.require_open (parser))
8187 return NULL;
8188
8189 expression_list = make_tree_vector ();
8190
8191 /* Within a parenthesized expression, a `>' token is always
8192 the greater-than operator. */
8193 saved_greater_than_is_operator_p
8194 = parser->greater_than_is_operator_p;
8195 parser->greater_than_is_operator_p = true;
8196
8197 cp_expr expr (NULL_TREE);
8198
8199 /* Consume expressions until there are no more. */
8200 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8201 while (true)
8202 {
8203 /* At the beginning of attribute lists, check to see if the
8204 next token is an identifier. */
8205 if (is_attribute_list == id_attr
8206 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
8207 {
8208 cp_token *token;
8209
8210 /* Consume the identifier. */
8211 token = cp_lexer_consume_token (parser->lexer);
8212 /* Save the identifier. */
8213 identifier = token->u.value;
8214 }
8215 else
8216 {
8217 bool expr_non_constant_p;
8218
8219 /* Parse the next assignment-expression. */
8220 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8221 {
8222 /* A braced-init-list. */
8223 cp_lexer_set_source_position (parser->lexer);
8224 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8225 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8226 if (non_constant_p && expr_non_constant_p)
8227 *non_constant_p = true;
8228 }
8229 else if (non_constant_p)
8230 {
8231 expr = (cp_parser_constant_expression
8232 (parser, /*allow_non_constant_p=*/true,
8233 &expr_non_constant_p));
8234 if (expr_non_constant_p)
8235 *non_constant_p = true;
8236 }
8237 else
8238 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
8239 cast_p);
8240
8241 if (fold_expr_p)
8242 expr = instantiate_non_dependent_expr (expr);
8243
8244 /* If we have an ellipsis, then this is an expression
8245 expansion. */
8246 if (allow_expansion_p
8247 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8248 {
8249 /* Consume the `...'. */
8250 cp_lexer_consume_token (parser->lexer);
8251
8252 /* Build the argument pack. */
8253 expr = make_pack_expansion (expr);
8254 }
8255
8256 if (wrap_locations_p)
8257 expr.maybe_add_location_wrapper ();
8258
8259 /* Add it to the list. We add error_mark_node
8260 expressions to the list, so that we can still tell if
8261 the correct form for a parenthesized expression-list
8262 is found. That gives better errors. */
8263 vec_safe_push (expression_list, expr.get_value ());
8264
8265 if (expr == error_mark_node)
8266 goto skip_comma;
8267 }
8268
8269 /* After the first item, attribute lists look the same as
8270 expression lists. */
8271 is_attribute_list = non_attr;
8272
8273 get_comma:;
8274 /* If the next token isn't a `,', then we are done. */
8275 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8276 break;
8277
8278 /* Otherwise, consume the `,' and keep going. */
8279 cp_lexer_consume_token (parser->lexer);
8280 }
8281
8282 if (close_paren_loc)
8283 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
8284
8285 if (!parens.require_close (parser))
8286 {
8287 int ending;
8288
8289 skip_comma:;
8290 /* We try and resync to an unnested comma, as that will give the
8291 user better diagnostics. */
8292 ending = cp_parser_skip_to_closing_parenthesis (parser,
8293 /*recovering=*/true,
8294 /*or_comma=*/true,
8295 /*consume_paren=*/true);
8296 if (ending < 0)
8297 goto get_comma;
8298 if (!ending)
8299 {
8300 parser->greater_than_is_operator_p
8301 = saved_greater_than_is_operator_p;
8302 return NULL;
8303 }
8304 }
8305
8306 parser->greater_than_is_operator_p
8307 = saved_greater_than_is_operator_p;
8308
8309 if (identifier)
8310 vec_safe_insert (expression_list, 0, identifier);
8311
8312 return expression_list;
8313 }
8314
8315 /* Parse a pseudo-destructor-name.
8316
8317 pseudo-destructor-name:
8318 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8319 :: [opt] nested-name-specifier template template-id :: ~ type-name
8320 :: [opt] nested-name-specifier [opt] ~ type-name
8321
8322 If either of the first two productions is used, sets *SCOPE to the
8323 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8324 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8325 or ERROR_MARK_NODE if the parse fails. */
8326
8327 static void
8328 cp_parser_pseudo_destructor_name (cp_parser* parser,
8329 tree object,
8330 tree* scope,
8331 tree* type)
8332 {
8333 bool nested_name_specifier_p;
8334
8335 /* Handle ~auto. */
8336 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8337 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8338 && !type_dependent_expression_p (object))
8339 {
8340 if (cxx_dialect < cxx14)
8341 pedwarn (input_location, 0,
8342 "%<~auto%> only available with "
8343 "%<-std=c++14%> or %<-std=gnu++14%>");
8344 cp_lexer_consume_token (parser->lexer);
8345 cp_lexer_consume_token (parser->lexer);
8346 *scope = NULL_TREE;
8347 *type = TREE_TYPE (object);
8348 return;
8349 }
8350
8351 /* Assume that things will not work out. */
8352 *type = error_mark_node;
8353
8354 /* Look for the optional `::' operator. */
8355 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8356 /* Look for the optional nested-name-specifier. */
8357 nested_name_specifier_p
8358 = (cp_parser_nested_name_specifier_opt (parser,
8359 /*typename_keyword_p=*/false,
8360 /*check_dependency_p=*/true,
8361 /*type_p=*/false,
8362 /*is_declaration=*/false)
8363 != NULL_TREE);
8364 /* Now, if we saw a nested-name-specifier, we might be doing the
8365 second production. */
8366 if (nested_name_specifier_p
8367 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8368 {
8369 /* Consume the `template' keyword. */
8370 cp_lexer_consume_token (parser->lexer);
8371 /* Parse the template-id. */
8372 cp_parser_template_id (parser,
8373 /*template_keyword_p=*/true,
8374 /*check_dependency_p=*/false,
8375 class_type,
8376 /*is_declaration=*/true);
8377 /* Look for the `::' token. */
8378 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8379 }
8380 /* If the next token is not a `~', then there might be some
8381 additional qualification. */
8382 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8383 {
8384 /* At this point, we're looking for "type-name :: ~". The type-name
8385 must not be a class-name, since this is a pseudo-destructor. So,
8386 it must be either an enum-name, or a typedef-name -- both of which
8387 are just identifiers. So, we peek ahead to check that the "::"
8388 and "~" tokens are present; if they are not, then we can avoid
8389 calling type_name. */
8390 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8391 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8392 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8393 {
8394 cp_parser_error (parser, "non-scalar type");
8395 return;
8396 }
8397
8398 /* Look for the type-name. */
8399 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8400 if (*scope == error_mark_node)
8401 return;
8402
8403 /* Look for the `::' token. */
8404 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8405 }
8406 else
8407 *scope = NULL_TREE;
8408
8409 /* Look for the `~'. */
8410 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8411
8412 /* Once we see the ~, this has to be a pseudo-destructor. */
8413 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8414 cp_parser_commit_to_topmost_tentative_parse (parser);
8415
8416 /* Look for the type-name again. We are not responsible for
8417 checking that it matches the first type-name. */
8418 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8419 }
8420
8421 /* Parse a unary-expression.
8422
8423 unary-expression:
8424 postfix-expression
8425 ++ cast-expression
8426 -- cast-expression
8427 await-expression
8428 unary-operator cast-expression
8429 sizeof unary-expression
8430 sizeof ( type-id )
8431 alignof ( type-id ) [C++0x]
8432 new-expression
8433 delete-expression
8434
8435 GNU Extensions:
8436
8437 unary-expression:
8438 __extension__ cast-expression
8439 __alignof__ unary-expression
8440 __alignof__ ( type-id )
8441 alignof unary-expression [C++0x]
8442 __real__ cast-expression
8443 __imag__ cast-expression
8444 && identifier
8445 sizeof ( type-id ) { initializer-list , [opt] }
8446 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8447 __alignof__ ( type-id ) { initializer-list , [opt] }
8448
8449 ADDRESS_P is true iff the unary-expression is appearing as the
8450 operand of the `&' operator. CAST_P is true if this expression is
8451 the target of a cast.
8452
8453 Returns a representation of the expression. */
8454
8455 static cp_expr
8456 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8457 bool address_p, bool cast_p, bool decltype_p)
8458 {
8459 cp_token *token;
8460 enum tree_code unary_operator;
8461
8462 /* Peek at the next token. */
8463 token = cp_lexer_peek_token (parser->lexer);
8464 /* Some keywords give away the kind of expression. */
8465 if (token->type == CPP_KEYWORD)
8466 {
8467 enum rid keyword = token->keyword;
8468
8469 switch (keyword)
8470 {
8471 case RID_ALIGNOF:
8472 case RID_SIZEOF:
8473 {
8474 tree operand, ret;
8475 enum tree_code op;
8476 location_t start_loc = token->location;
8477
8478 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8479 bool std_alignof = id_equal (token->u.value, "alignof");
8480
8481 /* Consume the token. */
8482 cp_lexer_consume_token (parser->lexer);
8483 /* Parse the operand. */
8484 operand = cp_parser_sizeof_operand (parser, keyword);
8485
8486 /* Construct a location e.g. :
8487 alignof (expr)
8488 ^~~~~~~~~~~~~~
8489 with start == caret at the start of the "alignof"/"sizeof"
8490 token, with the endpoint at the final closing paren. */
8491 location_t compound_loc
8492 = make_location (start_loc, start_loc, parser->lexer);
8493
8494 if (TYPE_P (operand))
8495 ret = cxx_sizeof_or_alignof_type (compound_loc, operand, op,
8496 std_alignof, true);
8497 else
8498 {
8499 /* ISO C++ defines alignof only with types, not with
8500 expressions. So pedwarn if alignof is used with a non-
8501 type expression. However, __alignof__ is ok. */
8502 if (std_alignof)
8503 pedwarn (token->location, OPT_Wpedantic,
8504 "ISO C++ does not allow %<alignof%> "
8505 "with a non-type");
8506
8507 ret = cxx_sizeof_or_alignof_expr (compound_loc, operand, op,
8508 std_alignof, true);
8509 }
8510 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8511 SIZEOF_EXPR with the original operand. */
8512 if (op == SIZEOF_EXPR && ret != error_mark_node)
8513 {
8514 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8515 {
8516 if (!processing_template_decl && TYPE_P (operand))
8517 {
8518 ret = build_min (SIZEOF_EXPR, size_type_node,
8519 build1 (NOP_EXPR, operand,
8520 error_mark_node));
8521 SIZEOF_EXPR_TYPE_P (ret) = 1;
8522 }
8523 else
8524 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8525 TREE_SIDE_EFFECTS (ret) = 0;
8526 TREE_READONLY (ret) = 1;
8527 SET_EXPR_LOCATION (ret, compound_loc);
8528 }
8529 }
8530
8531 cp_expr ret_expr (ret, compound_loc);
8532 ret_expr = ret_expr.maybe_add_location_wrapper ();
8533 return ret_expr;
8534 }
8535
8536 case RID_BUILTIN_HAS_ATTRIBUTE:
8537 return cp_parser_has_attribute_expression (parser);
8538
8539 case RID_NEW:
8540 return cp_parser_new_expression (parser);
8541
8542 case RID_DELETE:
8543 return cp_parser_delete_expression (parser);
8544
8545 case RID_EXTENSION:
8546 {
8547 /* The saved value of the PEDANTIC flag. */
8548 int saved_pedantic;
8549 tree expr;
8550
8551 /* Save away the PEDANTIC flag. */
8552 cp_parser_extension_opt (parser, &saved_pedantic);
8553 /* Parse the cast-expression. */
8554 expr = cp_parser_simple_cast_expression (parser);
8555 /* Restore the PEDANTIC flag. */
8556 pedantic = saved_pedantic;
8557
8558 return expr;
8559 }
8560
8561 case RID_REALPART:
8562 case RID_IMAGPART:
8563 {
8564 tree expression;
8565
8566 /* Consume the `__real__' or `__imag__' token. */
8567 cp_lexer_consume_token (parser->lexer);
8568 /* Parse the cast-expression. */
8569 expression = cp_parser_simple_cast_expression (parser);
8570 /* Create the complete representation. */
8571 return build_x_unary_op (token->location,
8572 (keyword == RID_REALPART
8573 ? REALPART_EXPR : IMAGPART_EXPR),
8574 expression,
8575 tf_warning_or_error);
8576 }
8577 break;
8578
8579 case RID_TRANSACTION_ATOMIC:
8580 case RID_TRANSACTION_RELAXED:
8581 return cp_parser_transaction_expression (parser, keyword);
8582
8583 case RID_NOEXCEPT:
8584 {
8585 tree expr;
8586 const char *saved_message;
8587 bool saved_integral_constant_expression_p;
8588 bool saved_non_integral_constant_expression_p;
8589 bool saved_greater_than_is_operator_p;
8590
8591 location_t start_loc = token->location;
8592
8593 cp_lexer_consume_token (parser->lexer);
8594 matching_parens parens;
8595 parens.require_open (parser);
8596
8597 saved_message = parser->type_definition_forbidden_message;
8598 parser->type_definition_forbidden_message
8599 = G_("types may not be defined in %<noexcept%> expressions");
8600
8601 saved_integral_constant_expression_p
8602 = parser->integral_constant_expression_p;
8603 saved_non_integral_constant_expression_p
8604 = parser->non_integral_constant_expression_p;
8605 parser->integral_constant_expression_p = false;
8606
8607 saved_greater_than_is_operator_p
8608 = parser->greater_than_is_operator_p;
8609 parser->greater_than_is_operator_p = true;
8610
8611 ++cp_unevaluated_operand;
8612 ++c_inhibit_evaluation_warnings;
8613 ++cp_noexcept_operand;
8614 expr = cp_parser_expression (parser);
8615 --cp_noexcept_operand;
8616 --c_inhibit_evaluation_warnings;
8617 --cp_unevaluated_operand;
8618
8619 parser->greater_than_is_operator_p
8620 = saved_greater_than_is_operator_p;
8621
8622 parser->integral_constant_expression_p
8623 = saved_integral_constant_expression_p;
8624 parser->non_integral_constant_expression_p
8625 = saved_non_integral_constant_expression_p;
8626
8627 parser->type_definition_forbidden_message = saved_message;
8628
8629 parens.require_close (parser);
8630
8631 /* Construct a location of the form:
8632 noexcept (expr)
8633 ^~~~~~~~~~~~~~~
8634 with start == caret, finishing at the close-paren. */
8635 location_t noexcept_loc
8636 = make_location (start_loc, start_loc, parser->lexer);
8637
8638 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8639 noexcept_loc);
8640 }
8641
8642 case RID_CO_AWAIT:
8643 {
8644 tree expr;
8645 location_t kw_loc = token->location;
8646
8647 /* Consume the `co_await' token. */
8648 cp_lexer_consume_token (parser->lexer);
8649 /* Parse its cast-expression. */
8650 expr = cp_parser_simple_cast_expression (parser);
8651 if (expr == error_mark_node)
8652 return error_mark_node;
8653
8654 /* Handle [expr.await]. */
8655 return cp_expr (finish_co_await_expr (kw_loc, expr));
8656 }
8657
8658 default:
8659 break;
8660 }
8661 }
8662
8663 /* Look for the `:: new' and `:: delete', which also signal the
8664 beginning of a new-expression, or delete-expression,
8665 respectively. If the next token is `::', then it might be one of
8666 these. */
8667 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8668 {
8669 enum rid keyword;
8670
8671 /* See if the token after the `::' is one of the keywords in
8672 which we're interested. */
8673 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8674 /* If it's `new', we have a new-expression. */
8675 if (keyword == RID_NEW)
8676 return cp_parser_new_expression (parser);
8677 /* Similarly, for `delete'. */
8678 else if (keyword == RID_DELETE)
8679 return cp_parser_delete_expression (parser);
8680 }
8681
8682 /* Look for a unary operator. */
8683 unary_operator = cp_parser_unary_operator (token);
8684 /* The `++' and `--' operators can be handled similarly, even though
8685 they are not technically unary-operators in the grammar. */
8686 if (unary_operator == ERROR_MARK)
8687 {
8688 if (token->type == CPP_PLUS_PLUS)
8689 unary_operator = PREINCREMENT_EXPR;
8690 else if (token->type == CPP_MINUS_MINUS)
8691 unary_operator = PREDECREMENT_EXPR;
8692 /* Handle the GNU address-of-label extension. */
8693 else if (cp_parser_allow_gnu_extensions_p (parser)
8694 && token->type == CPP_AND_AND)
8695 {
8696 tree identifier;
8697 tree expression;
8698 location_t start_loc = token->location;
8699
8700 /* Consume the '&&' token. */
8701 cp_lexer_consume_token (parser->lexer);
8702 /* Look for the identifier. */
8703 identifier = cp_parser_identifier (parser);
8704 /* Construct a location of the form:
8705 &&label
8706 ^~~~~~~
8707 with caret==start at the "&&", finish at the end of the label. */
8708 location_t combined_loc
8709 = make_location (start_loc, start_loc, parser->lexer);
8710 /* Create an expression representing the address. */
8711 expression = finish_label_address_expr (identifier, combined_loc);
8712 if (cp_parser_non_integral_constant_expression (parser,
8713 NIC_ADDR_LABEL))
8714 expression = error_mark_node;
8715 return expression;
8716 }
8717 }
8718 if (unary_operator != ERROR_MARK)
8719 {
8720 cp_expr cast_expression;
8721 cp_expr expression = error_mark_node;
8722 non_integral_constant non_constant_p = NIC_NONE;
8723 location_t loc = token->location;
8724 tsubst_flags_t complain = complain_flags (decltype_p);
8725
8726 /* Consume the operator token. */
8727 token = cp_lexer_consume_token (parser->lexer);
8728 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8729
8730 /* Parse the cast-expression. */
8731 cast_expression
8732 = cp_parser_cast_expression (parser,
8733 unary_operator == ADDR_EXPR,
8734 /*cast_p=*/false,
8735 /*decltype*/false,
8736 pidk);
8737
8738 /* Make a location:
8739 OP_TOKEN CAST_EXPRESSION
8740 ^~~~~~~~~~~~~~~~~~~~~~~~~
8741 with start==caret at the operator token, and
8742 extending to the end of the cast_expression. */
8743 loc = make_location (loc, loc, cast_expression.get_finish ());
8744
8745 /* Now, build an appropriate representation. */
8746 switch (unary_operator)
8747 {
8748 case INDIRECT_REF:
8749 non_constant_p = NIC_STAR;
8750 expression = build_x_indirect_ref (loc, cast_expression,
8751 RO_UNARY_STAR,
8752 complain);
8753 /* TODO: build_x_indirect_ref does not always honor the
8754 location, so ensure it is set. */
8755 expression.set_location (loc);
8756 break;
8757
8758 case ADDR_EXPR:
8759 non_constant_p = NIC_ADDR;
8760 /* Fall through. */
8761 case BIT_NOT_EXPR:
8762 expression = build_x_unary_op (loc, unary_operator,
8763 cast_expression,
8764 complain);
8765 /* TODO: build_x_unary_op does not always honor the location,
8766 so ensure it is set. */
8767 expression.set_location (loc);
8768 break;
8769
8770 case PREINCREMENT_EXPR:
8771 case PREDECREMENT_EXPR:
8772 non_constant_p = unary_operator == PREINCREMENT_EXPR
8773 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8774 /* Fall through. */
8775 case NEGATE_EXPR:
8776 /* Immediately fold negation of a constant, unless the constant is 0
8777 (since -0 == 0) or it would overflow. */
8778 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8779 {
8780 tree stripped_expr
8781 = tree_strip_any_location_wrapper (cast_expression);
8782 if (CONSTANT_CLASS_P (stripped_expr)
8783 && !integer_zerop (stripped_expr)
8784 && !TREE_OVERFLOW (stripped_expr))
8785 {
8786 tree folded = fold_build1 (unary_operator,
8787 TREE_TYPE (stripped_expr),
8788 stripped_expr);
8789 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8790 {
8791 expression = maybe_wrap_with_location (folded, loc);
8792 break;
8793 }
8794 }
8795 }
8796 /* Fall through. */
8797 case UNARY_PLUS_EXPR:
8798 case TRUTH_NOT_EXPR:
8799 expression = finish_unary_op_expr (loc, unary_operator,
8800 cast_expression, complain);
8801 break;
8802
8803 default:
8804 gcc_unreachable ();
8805 }
8806
8807 if (non_constant_p != NIC_NONE
8808 && cp_parser_non_integral_constant_expression (parser,
8809 non_constant_p))
8810 expression = error_mark_node;
8811
8812 return expression;
8813 }
8814
8815 return cp_parser_postfix_expression (parser, address_p, cast_p,
8816 /*member_access_only_p=*/false,
8817 decltype_p,
8818 pidk);
8819 }
8820
8821 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8822 unary-operator, the corresponding tree code is returned. */
8823
8824 static enum tree_code
8825 cp_parser_unary_operator (cp_token* token)
8826 {
8827 switch (token->type)
8828 {
8829 case CPP_MULT:
8830 return INDIRECT_REF;
8831
8832 case CPP_AND:
8833 return ADDR_EXPR;
8834
8835 case CPP_PLUS:
8836 return UNARY_PLUS_EXPR;
8837
8838 case CPP_MINUS:
8839 return NEGATE_EXPR;
8840
8841 case CPP_NOT:
8842 return TRUTH_NOT_EXPR;
8843
8844 case CPP_COMPL:
8845 return BIT_NOT_EXPR;
8846
8847 default:
8848 return ERROR_MARK;
8849 }
8850 }
8851
8852 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8853 Returns a representation of the expression. */
8854
8855 static tree
8856 cp_parser_has_attribute_expression (cp_parser *parser)
8857 {
8858 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8859
8860 /* Consume the __builtin_has_attribute token. */
8861 cp_lexer_consume_token (parser->lexer);
8862
8863 matching_parens parens;
8864 if (!parens.require_open (parser))
8865 return error_mark_node;
8866
8867 /* Types cannot be defined in a `sizeof' expression. Save away the
8868 old message. */
8869 const char *saved_message = parser->type_definition_forbidden_message;
8870 const char *saved_message_arg
8871 = parser->type_definition_forbidden_message_arg;
8872 parser->type_definition_forbidden_message
8873 = G_("types may not be defined in %qs expressions");
8874 parser->type_definition_forbidden_message_arg
8875 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
8876
8877 /* The restrictions on constant-expressions do not apply inside
8878 sizeof expressions. */
8879 bool saved_integral_constant_expression_p
8880 = parser->integral_constant_expression_p;
8881 bool saved_non_integral_constant_expression_p
8882 = parser->non_integral_constant_expression_p;
8883 parser->integral_constant_expression_p = false;
8884
8885 /* Do not actually evaluate the expression. */
8886 ++cp_unevaluated_operand;
8887 ++c_inhibit_evaluation_warnings;
8888
8889 tree oper = NULL_TREE;
8890
8891 /* We can't be sure yet whether we're looking at a type-id or an
8892 expression. */
8893 cp_parser_parse_tentatively (parser);
8894
8895 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8896 parser->in_type_id_in_expr_p = true;
8897 /* Look for the type-id. */
8898 oper = cp_parser_type_id (parser);
8899 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8900
8901 cp_parser_parse_definitely (parser);
8902
8903 /* If the type-id production did not work out, then we must be
8904 looking at an expression. */
8905 if (!oper || oper == error_mark_node)
8906 oper = cp_parser_assignment_expression (parser);
8907
8908 STRIP_ANY_LOCATION_WRAPPER (oper);
8909
8910 /* Go back to evaluating expressions. */
8911 --cp_unevaluated_operand;
8912 --c_inhibit_evaluation_warnings;
8913
8914 /* And restore the old one. */
8915 parser->type_definition_forbidden_message = saved_message;
8916 parser->type_definition_forbidden_message_arg = saved_message_arg;
8917 parser->integral_constant_expression_p
8918 = saved_integral_constant_expression_p;
8919 parser->non_integral_constant_expression_p
8920 = saved_non_integral_constant_expression_p;
8921
8922 /* Consume the comma if it's there. */
8923 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
8924 {
8925 cp_parser_skip_to_closing_parenthesis (parser, false, false,
8926 /*consume_paren=*/true);
8927 return error_mark_node;
8928 }
8929
8930 /* Parse the attribute specification. */
8931 bool ret = false;
8932 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
8933 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
8934 {
8935 if (oper == error_mark_node)
8936 /* Nothing. */;
8937 else if (type_dependent_expression_p (oper))
8938 sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
8939 "not supported yet");
8940 else
8941 {
8942 /* Fold constant expressions used in attributes first. */
8943 cp_check_const_attributes (attr);
8944
8945 /* Finally, see if OPER has been declared with ATTR. */
8946 ret = has_attribute (atloc, oper, attr, default_conversion);
8947 }
8948
8949 parens.require_close (parser);
8950 }
8951 else
8952 {
8953 error_at (atloc, "expected identifier");
8954 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8955 }
8956
8957 /* Construct a location e.g. :
8958 __builtin_has_attribute (oper, attr)
8959 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8960 with start == caret at the start of the built-in token,
8961 and with the endpoint at the final closing paren. */
8962 location_t compound_loc
8963 = make_location (start_loc, start_loc, parser->lexer);
8964
8965 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
8966 ret_expr.set_location (compound_loc);
8967 ret_expr = ret_expr.maybe_add_location_wrapper ();
8968 return ret_expr;
8969 }
8970
8971 /* Parse a new-expression.
8972
8973 new-expression:
8974 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8975 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8976
8977 Returns a representation of the expression. */
8978
8979 static tree
8980 cp_parser_new_expression (cp_parser* parser)
8981 {
8982 bool global_scope_p;
8983 vec<tree, va_gc> *placement;
8984 tree type;
8985 vec<tree, va_gc> *initializer;
8986 tree nelts = NULL_TREE;
8987 tree ret;
8988
8989 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8990
8991 /* Look for the optional `::' operator. */
8992 global_scope_p
8993 = (cp_parser_global_scope_opt (parser,
8994 /*current_scope_valid_p=*/false)
8995 != NULL_TREE);
8996 /* Look for the `new' operator. */
8997 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8998 /* There's no easy way to tell a new-placement from the
8999 `( type-id )' construct. */
9000 cp_parser_parse_tentatively (parser);
9001 /* Look for a new-placement. */
9002 placement = cp_parser_new_placement (parser);
9003 /* If that didn't work out, there's no new-placement. */
9004 if (!cp_parser_parse_definitely (parser))
9005 {
9006 if (placement != NULL)
9007 release_tree_vector (placement);
9008 placement = NULL;
9009 }
9010
9011 /* If the next token is a `(', then we have a parenthesized
9012 type-id. */
9013 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9014 {
9015 cp_token *token;
9016 const char *saved_message = parser->type_definition_forbidden_message;
9017
9018 /* Consume the `('. */
9019 matching_parens parens;
9020 parens.consume_open (parser);
9021
9022 /* Parse the type-id. */
9023 parser->type_definition_forbidden_message
9024 = G_("types may not be defined in a new-expression");
9025 {
9026 type_id_in_expr_sentinel s (parser);
9027 type = cp_parser_type_id (parser);
9028 }
9029 parser->type_definition_forbidden_message = saved_message;
9030
9031 /* Look for the closing `)'. */
9032 parens.require_close (parser);
9033 token = cp_lexer_peek_token (parser->lexer);
9034 /* There should not be a direct-new-declarator in this production,
9035 but GCC used to allowed this, so we check and emit a sensible error
9036 message for this case. */
9037 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9038 {
9039 error_at (token->location,
9040 "array bound forbidden after parenthesized type-id");
9041 inform (token->location,
9042 "try removing the parentheses around the type-id");
9043 cp_parser_direct_new_declarator (parser);
9044 }
9045 }
9046 /* Otherwise, there must be a new-type-id. */
9047 else
9048 type = cp_parser_new_type_id (parser, &nelts);
9049
9050 /* If the next token is a `(' or '{', then we have a new-initializer. */
9051 cp_token *token = cp_lexer_peek_token (parser->lexer);
9052 if (token->type == CPP_OPEN_PAREN
9053 || token->type == CPP_OPEN_BRACE)
9054 initializer = cp_parser_new_initializer (parser);
9055 else
9056 initializer = NULL;
9057
9058 /* A new-expression may not appear in an integral constant
9059 expression. */
9060 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
9061 ret = error_mark_node;
9062 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
9063 of a new-type-id or type-id of a new-expression, the new-expression shall
9064 contain a new-initializer of the form ( assignment-expression )".
9065 Additionally, consistently with the spirit of DR 1467, we want to accept
9066 'new auto { 2 }' too. */
9067 else if ((ret = type_uses_auto (type))
9068 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
9069 && (vec_safe_length (initializer) != 1
9070 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
9071 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
9072 {
9073 error_at (token->location,
9074 "initialization of new-expression for type %<auto%> "
9075 "requires exactly one element");
9076 ret = error_mark_node;
9077 }
9078 else
9079 {
9080 /* Construct a location e.g.:
9081 ptr = new int[100]
9082 ^~~~~~~~~~~~
9083 with caret == start at the start of the "new" token, and the end
9084 at the end of the final token we consumed. */
9085 location_t combined_loc = make_location (start_loc, start_loc,
9086 parser->lexer);
9087 /* Create a representation of the new-expression. */
9088 ret = build_new (combined_loc, &placement, type, nelts, &initializer,
9089 global_scope_p, tf_warning_or_error);
9090 }
9091
9092 if (placement != NULL)
9093 release_tree_vector (placement);
9094 if (initializer != NULL)
9095 release_tree_vector (initializer);
9096
9097 return ret;
9098 }
9099
9100 /* Parse a new-placement.
9101
9102 new-placement:
9103 ( expression-list )
9104
9105 Returns the same representation as for an expression-list. */
9106
9107 static vec<tree, va_gc> *
9108 cp_parser_new_placement (cp_parser* parser)
9109 {
9110 vec<tree, va_gc> *expression_list;
9111
9112 /* Parse the expression-list. */
9113 expression_list = (cp_parser_parenthesized_expression_list
9114 (parser, non_attr, /*cast_p=*/false,
9115 /*allow_expansion_p=*/true,
9116 /*non_constant_p=*/NULL));
9117
9118 if (expression_list && expression_list->is_empty ())
9119 error ("expected expression-list or type-id");
9120
9121 return expression_list;
9122 }
9123
9124 /* Parse a new-type-id.
9125
9126 new-type-id:
9127 type-specifier-seq new-declarator [opt]
9128
9129 Returns the TYPE allocated. If the new-type-id indicates an array
9130 type, *NELTS is set to the number of elements in the last array
9131 bound; the TYPE will not include the last array bound. */
9132
9133 static tree
9134 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
9135 {
9136 cp_decl_specifier_seq type_specifier_seq;
9137 cp_declarator *new_declarator;
9138 cp_declarator *declarator;
9139 cp_declarator *outer_declarator;
9140 const char *saved_message;
9141
9142 /* The type-specifier sequence must not contain type definitions.
9143 (It cannot contain declarations of new types either, but if they
9144 are not definitions we will catch that because they are not
9145 complete.) */
9146 saved_message = parser->type_definition_forbidden_message;
9147 parser->type_definition_forbidden_message
9148 = G_("types may not be defined in a new-type-id");
9149 /* Parse the type-specifier-seq. */
9150 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
9151 /*is_declaration=*/false,
9152 /*is_trailing_return=*/false,
9153 &type_specifier_seq);
9154 /* Restore the old message. */
9155 parser->type_definition_forbidden_message = saved_message;
9156
9157 if (type_specifier_seq.type == error_mark_node)
9158 return error_mark_node;
9159
9160 /* Parse the new-declarator. */
9161 new_declarator = cp_parser_new_declarator_opt (parser);
9162
9163 /* Determine the number of elements in the last array dimension, if
9164 any. */
9165 *nelts = NULL_TREE;
9166 /* Skip down to the last array dimension. */
9167 declarator = new_declarator;
9168 outer_declarator = NULL;
9169 while (declarator && (declarator->kind == cdk_pointer
9170 || declarator->kind == cdk_ptrmem))
9171 {
9172 outer_declarator = declarator;
9173 declarator = declarator->declarator;
9174 }
9175 while (declarator
9176 && declarator->kind == cdk_array
9177 && declarator->declarator
9178 && declarator->declarator->kind == cdk_array)
9179 {
9180 outer_declarator = declarator;
9181 declarator = declarator->declarator;
9182 }
9183
9184 if (declarator && declarator->kind == cdk_array)
9185 {
9186 *nelts = declarator->u.array.bounds;
9187 if (*nelts == error_mark_node)
9188 *nelts = integer_one_node;
9189
9190 if (*nelts == NULL_TREE)
9191 /* Leave [] in the declarator. */;
9192 else if (outer_declarator)
9193 outer_declarator->declarator = declarator->declarator;
9194 else
9195 new_declarator = NULL;
9196 }
9197
9198 return groktypename (&type_specifier_seq, new_declarator, false);
9199 }
9200
9201 /* Parse an (optional) new-declarator.
9202
9203 new-declarator:
9204 ptr-operator new-declarator [opt]
9205 direct-new-declarator
9206
9207 Returns the declarator. */
9208
9209 static cp_declarator *
9210 cp_parser_new_declarator_opt (cp_parser* parser)
9211 {
9212 enum tree_code code;
9213 tree type, std_attributes = NULL_TREE;
9214 cp_cv_quals cv_quals;
9215
9216 /* We don't know if there's a ptr-operator next, or not. */
9217 cp_parser_parse_tentatively (parser);
9218 /* Look for a ptr-operator. */
9219 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
9220 /* If that worked, look for more new-declarators. */
9221 if (cp_parser_parse_definitely (parser))
9222 {
9223 cp_declarator *declarator;
9224
9225 /* Parse another optional declarator. */
9226 declarator = cp_parser_new_declarator_opt (parser);
9227
9228 declarator = cp_parser_make_indirect_declarator
9229 (code, type, cv_quals, declarator, std_attributes);
9230
9231 return declarator;
9232 }
9233
9234 /* If the next token is a `[', there is a direct-new-declarator. */
9235 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9236 return cp_parser_direct_new_declarator (parser);
9237
9238 return NULL;
9239 }
9240
9241 /* Parse a direct-new-declarator.
9242
9243 direct-new-declarator:
9244 [ expression ]
9245 direct-new-declarator [constant-expression]
9246
9247 */
9248
9249 static cp_declarator *
9250 cp_parser_direct_new_declarator (cp_parser* parser)
9251 {
9252 cp_declarator *declarator = NULL;
9253 bool first_p = true;
9254
9255 while (true)
9256 {
9257 tree expression;
9258 cp_token *token;
9259
9260 /* Look for the opening `['. */
9261 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9262
9263 token = cp_lexer_peek_token (parser->lexer);
9264 if (token->type == CPP_CLOSE_SQUARE && first_p)
9265 expression = NULL_TREE;
9266 else
9267 expression = cp_parser_expression (parser);
9268 /* The standard requires that the expression have integral
9269 type. DR 74 adds enumeration types. We believe that the
9270 real intent is that these expressions be handled like the
9271 expression in a `switch' condition, which also allows
9272 classes with a single conversion to integral or
9273 enumeration type. */
9274 if (expression && !processing_template_decl)
9275 {
9276 expression
9277 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
9278 expression,
9279 /*complain=*/true);
9280 if (!expression)
9281 {
9282 error_at (token->location,
9283 "expression in new-declarator must have integral "
9284 "or enumeration type");
9285 expression = error_mark_node;
9286 }
9287 }
9288
9289 /* Look for the closing `]'. */
9290 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9291
9292 /* Add this bound to the declarator. */
9293 declarator = make_array_declarator (declarator, expression);
9294
9295 /* If the next token is not a `[', then there are no more
9296 bounds. */
9297 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
9298 break;
9299 first_p = false;
9300 }
9301
9302 return declarator;
9303 }
9304
9305 /* Parse a new-initializer.
9306
9307 new-initializer:
9308 ( expression-list [opt] )
9309 braced-init-list
9310
9311 Returns a representation of the expression-list. */
9312
9313 static vec<tree, va_gc> *
9314 cp_parser_new_initializer (cp_parser* parser)
9315 {
9316 vec<tree, va_gc> *expression_list;
9317
9318 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9319 {
9320 tree t;
9321 bool expr_non_constant_p;
9322 cp_lexer_set_source_position (parser->lexer);
9323 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9324 t = cp_parser_braced_list (parser, &expr_non_constant_p);
9325 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
9326 expression_list = make_tree_vector_single (t);
9327 }
9328 else
9329 expression_list = (cp_parser_parenthesized_expression_list
9330 (parser, non_attr, /*cast_p=*/false,
9331 /*allow_expansion_p=*/true,
9332 /*non_constant_p=*/NULL));
9333
9334 return expression_list;
9335 }
9336
9337 /* Parse a delete-expression.
9338
9339 delete-expression:
9340 :: [opt] delete cast-expression
9341 :: [opt] delete [ ] cast-expression
9342
9343 Returns a representation of the expression. */
9344
9345 static tree
9346 cp_parser_delete_expression (cp_parser* parser)
9347 {
9348 bool global_scope_p;
9349 bool array_p;
9350 tree expression;
9351 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9352
9353 /* Look for the optional `::' operator. */
9354 global_scope_p
9355 = (cp_parser_global_scope_opt (parser,
9356 /*current_scope_valid_p=*/false)
9357 != NULL_TREE);
9358 /* Look for the `delete' keyword. */
9359 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9360 /* See if the array syntax is in use. */
9361 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9362 {
9363 /* Consume the `[' token. */
9364 cp_lexer_consume_token (parser->lexer);
9365 /* Look for the `]' token. */
9366 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9367 /* Remember that this is the `[]' construct. */
9368 array_p = true;
9369 }
9370 else
9371 array_p = false;
9372
9373 /* Parse the cast-expression. */
9374 expression = cp_parser_simple_cast_expression (parser);
9375
9376 /* A delete-expression may not appear in an integral constant
9377 expression. */
9378 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9379 return error_mark_node;
9380
9381 /* Construct a location e.g.:
9382 delete [ ] ptr
9383 ^~~~~~~~~~~~~~
9384 with caret == start at the start of the "delete" token, and
9385 the end at the end of the final token we consumed. */
9386 location_t combined_loc = make_location (start_loc, start_loc,
9387 parser->lexer);
9388 expression = delete_sanity (combined_loc, expression, NULL_TREE, array_p,
9389 global_scope_p, tf_warning_or_error);
9390
9391 return expression;
9392 }
9393
9394 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9395 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9396 0 otherwise. */
9397
9398 static int
9399 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9400 {
9401 cp_token *token = cp_lexer_peek_token (parser->lexer);
9402 switch (token->type)
9403 {
9404 case CPP_COMMA:
9405 case CPP_SEMICOLON:
9406 case CPP_QUERY:
9407 case CPP_COLON:
9408 case CPP_CLOSE_SQUARE:
9409 case CPP_CLOSE_PAREN:
9410 case CPP_CLOSE_BRACE:
9411 case CPP_OPEN_BRACE:
9412 case CPP_DOT:
9413 case CPP_DOT_STAR:
9414 case CPP_DEREF:
9415 case CPP_DEREF_STAR:
9416 case CPP_DIV:
9417 case CPP_MOD:
9418 case CPP_LSHIFT:
9419 case CPP_RSHIFT:
9420 case CPP_LESS:
9421 case CPP_GREATER:
9422 case CPP_LESS_EQ:
9423 case CPP_GREATER_EQ:
9424 case CPP_EQ_EQ:
9425 case CPP_NOT_EQ:
9426 case CPP_EQ:
9427 case CPP_MULT_EQ:
9428 case CPP_DIV_EQ:
9429 case CPP_MOD_EQ:
9430 case CPP_PLUS_EQ:
9431 case CPP_MINUS_EQ:
9432 case CPP_RSHIFT_EQ:
9433 case CPP_LSHIFT_EQ:
9434 case CPP_AND_EQ:
9435 case CPP_XOR_EQ:
9436 case CPP_OR_EQ:
9437 case CPP_XOR:
9438 case CPP_OR:
9439 case CPP_OR_OR:
9440 case CPP_EOF:
9441 case CPP_ELLIPSIS:
9442 return 0;
9443
9444 case CPP_OPEN_PAREN:
9445 /* In ((type ()) () the last () isn't a valid cast-expression,
9446 so the whole must be parsed as postfix-expression. */
9447 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9448 != CPP_CLOSE_PAREN;
9449
9450 case CPP_OPEN_SQUARE:
9451 /* '[' may start a primary-expression in obj-c++ and in C++11,
9452 as a lambda-expression, eg, '(void)[]{}'. */
9453 if (cxx_dialect >= cxx11)
9454 return -1;
9455 return c_dialect_objc ();
9456
9457 case CPP_PLUS_PLUS:
9458 case CPP_MINUS_MINUS:
9459 /* '++' and '--' may or may not start a cast-expression:
9460
9461 struct T { void operator++(int); };
9462 void f() { (T())++; }
9463
9464 vs
9465
9466 int a;
9467 (int)++a; */
9468 return -1;
9469
9470 default:
9471 return 1;
9472 }
9473 }
9474
9475 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9476 in the order: const_cast, static_cast, reinterpret_cast.
9477
9478 Don't suggest dynamic_cast.
9479
9480 Return the first legal cast kind found, or NULL otherwise. */
9481
9482 static const char *
9483 get_cast_suggestion (tree dst_type, tree orig_expr)
9484 {
9485 tree trial;
9486
9487 /* Reuse the parser logic by attempting to build the various kinds of
9488 cast, with "complain" disabled.
9489 Identify the first such cast that is valid. */
9490
9491 /* Don't attempt to run such logic within template processing. */
9492 if (processing_template_decl)
9493 return NULL;
9494
9495 /* First try const_cast. */
9496 trial = build_const_cast (input_location, dst_type, orig_expr, tf_none);
9497 if (trial != error_mark_node)
9498 return "const_cast";
9499
9500 /* If that fails, try static_cast. */
9501 trial = build_static_cast (input_location, dst_type, orig_expr, tf_none);
9502 if (trial != error_mark_node)
9503 return "static_cast";
9504
9505 /* Finally, try reinterpret_cast. */
9506 trial = build_reinterpret_cast (input_location, dst_type, orig_expr,
9507 tf_none);
9508 if (trial != error_mark_node)
9509 return "reinterpret_cast";
9510
9511 /* No such cast possible. */
9512 return NULL;
9513 }
9514
9515 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9516 suggesting how to convert a C-style cast of the form:
9517
9518 (DST_TYPE)ORIG_EXPR
9519
9520 to a C++-style cast.
9521
9522 The primary range of RICHLOC is asssumed to be that of the original
9523 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9524 of the parens in the C-style cast. */
9525
9526 static void
9527 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9528 location_t close_paren_loc, tree orig_expr,
9529 tree dst_type)
9530 {
9531 /* This function is non-trivial, so bail out now if the warning isn't
9532 going to be emitted. */
9533 if (!warn_old_style_cast)
9534 return;
9535
9536 /* Try to find a legal C++ cast, trying them in order:
9537 const_cast, static_cast, reinterpret_cast. */
9538 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9539 if (!cast_suggestion)
9540 return;
9541
9542 /* Replace the open paren with "CAST_SUGGESTION<". */
9543 pretty_printer pp;
9544 pp_string (&pp, cast_suggestion);
9545 pp_less (&pp);
9546 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9547
9548 /* Replace the close paren with "> (". */
9549 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9550
9551 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9552 rich_loc->add_fixit_insert_after (")");
9553 }
9554
9555
9556 /* Parse a cast-expression.
9557
9558 cast-expression:
9559 unary-expression
9560 ( type-id ) cast-expression
9561
9562 ADDRESS_P is true iff the unary-expression is appearing as the
9563 operand of the `&' operator. CAST_P is true if this expression is
9564 the target of a cast.
9565
9566 Returns a representation of the expression. */
9567
9568 static cp_expr
9569 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9570 bool decltype_p, cp_id_kind * pidk)
9571 {
9572 /* If it's a `(', then we might be looking at a cast. */
9573 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9574 {
9575 tree type = NULL_TREE;
9576 cp_expr expr (NULL_TREE);
9577 int cast_expression = 0;
9578 const char *saved_message;
9579
9580 /* There's no way to know yet whether or not this is a cast.
9581 For example, `(int (3))' is a unary-expression, while `(int)
9582 3' is a cast. So, we resort to parsing tentatively. */
9583 cp_parser_parse_tentatively (parser);
9584 /* Types may not be defined in a cast. */
9585 saved_message = parser->type_definition_forbidden_message;
9586 parser->type_definition_forbidden_message
9587 = G_("types may not be defined in casts");
9588 /* Consume the `('. */
9589 matching_parens parens;
9590 cp_token *open_paren = parens.consume_open (parser);
9591 location_t open_paren_loc = open_paren->location;
9592 location_t close_paren_loc = UNKNOWN_LOCATION;
9593
9594 /* A very tricky bit is that `(struct S) { 3 }' is a
9595 compound-literal (which we permit in C++ as an extension).
9596 But, that construct is not a cast-expression -- it is a
9597 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9598 is legal; if the compound-literal were a cast-expression,
9599 you'd need an extra set of parentheses.) But, if we parse
9600 the type-id, and it happens to be a class-specifier, then we
9601 will commit to the parse at that point, because we cannot
9602 undo the action that is done when creating a new class. So,
9603 then we cannot back up and do a postfix-expression.
9604
9605 Another tricky case is the following (c++/29234):
9606
9607 struct S { void operator () (); };
9608
9609 void foo ()
9610 {
9611 ( S()() );
9612 }
9613
9614 As a type-id we parse the parenthesized S()() as a function
9615 returning a function, groktypename complains and we cannot
9616 back up in this case either.
9617
9618 Therefore, we scan ahead to the closing `)', and check to see
9619 if the tokens after the `)' can start a cast-expression. Otherwise
9620 we are dealing with an unary-expression, a postfix-expression
9621 or something else.
9622
9623 Yet another tricky case, in C++11, is the following (c++/54891):
9624
9625 (void)[]{};
9626
9627 The issue is that usually, besides the case of lambda-expressions,
9628 the parenthesized type-id cannot be followed by '[', and, eg, we
9629 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9630 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9631 we don't commit, we try a cast-expression, then an unary-expression.
9632
9633 Save tokens so that we can put them back. */
9634 cp_lexer_save_tokens (parser->lexer);
9635
9636 /* We may be looking at a cast-expression. */
9637 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9638 /*consume_paren=*/true))
9639 cast_expression
9640 = cp_parser_tokens_start_cast_expression (parser);
9641
9642 /* Roll back the tokens we skipped. */
9643 cp_lexer_rollback_tokens (parser->lexer);
9644 /* If we aren't looking at a cast-expression, simulate an error so
9645 that the call to cp_parser_error_occurred below returns true. */
9646 if (!cast_expression)
9647 cp_parser_simulate_error (parser);
9648 else
9649 {
9650 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9651 parser->in_type_id_in_expr_p = true;
9652 /* Look for the type-id. */
9653 type = cp_parser_type_id (parser);
9654 /* Look for the closing `)'. */
9655 cp_token *close_paren = parens.require_close (parser);
9656 if (close_paren)
9657 close_paren_loc = close_paren->location;
9658 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9659 }
9660
9661 /* Restore the saved message. */
9662 parser->type_definition_forbidden_message = saved_message;
9663
9664 /* At this point this can only be either a cast or a
9665 parenthesized ctor such as `(T ())' that looks like a cast to
9666 function returning T. */
9667 if (!cp_parser_error_occurred (parser))
9668 {
9669 /* Only commit if the cast-expression doesn't start with
9670 '++', '--', or '[' in C++11. */
9671 if (cast_expression > 0)
9672 cp_parser_commit_to_topmost_tentative_parse (parser);
9673
9674 expr = cp_parser_cast_expression (parser,
9675 /*address_p=*/false,
9676 /*cast_p=*/true,
9677 /*decltype_p=*/false,
9678 pidk);
9679
9680 if (cp_parser_parse_definitely (parser))
9681 {
9682 /* Warn about old-style casts, if so requested. */
9683 if (warn_old_style_cast
9684 && !in_system_header_at (input_location)
9685 && !VOID_TYPE_P (type)
9686 && current_lang_name != lang_name_c)
9687 {
9688 gcc_rich_location rich_loc (input_location);
9689 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9690 expr, type);
9691 warning_at (&rich_loc, OPT_Wold_style_cast,
9692 "use of old-style cast to %q#T", type);
9693 }
9694
9695 /* Only type conversions to integral or enumeration types
9696 can be used in constant-expressions. */
9697 if (!cast_valid_in_integral_constant_expression_p (type)
9698 && cp_parser_non_integral_constant_expression (parser,
9699 NIC_CAST))
9700 return error_mark_node;
9701
9702 /* Perform the cast. */
9703 /* Make a location:
9704 (TYPE) EXPR
9705 ^~~~~~~~~~~
9706 with start==caret at the open paren, extending to the
9707 end of "expr". */
9708 location_t cast_loc = make_location (open_paren_loc,
9709 open_paren_loc,
9710 expr.get_finish ());
9711 expr = build_c_cast (cast_loc, type, expr);
9712 return expr;
9713 }
9714 }
9715 else
9716 cp_parser_abort_tentative_parse (parser);
9717 }
9718
9719 /* If we get here, then it's not a cast, so it must be a
9720 unary-expression. */
9721 return cp_parser_unary_expression (parser, pidk, address_p,
9722 cast_p, decltype_p);
9723 }
9724
9725 /* Parse a binary expression of the general form:
9726
9727 pm-expression:
9728 cast-expression
9729 pm-expression .* cast-expression
9730 pm-expression ->* cast-expression
9731
9732 multiplicative-expression:
9733 pm-expression
9734 multiplicative-expression * pm-expression
9735 multiplicative-expression / pm-expression
9736 multiplicative-expression % pm-expression
9737
9738 additive-expression:
9739 multiplicative-expression
9740 additive-expression + multiplicative-expression
9741 additive-expression - multiplicative-expression
9742
9743 shift-expression:
9744 additive-expression
9745 shift-expression << additive-expression
9746 shift-expression >> additive-expression
9747
9748 relational-expression:
9749 shift-expression
9750 relational-expression < shift-expression
9751 relational-expression > shift-expression
9752 relational-expression <= shift-expression
9753 relational-expression >= shift-expression
9754
9755 GNU Extension:
9756
9757 relational-expression:
9758 relational-expression <? shift-expression
9759 relational-expression >? shift-expression
9760
9761 equality-expression:
9762 relational-expression
9763 equality-expression == relational-expression
9764 equality-expression != relational-expression
9765
9766 and-expression:
9767 equality-expression
9768 and-expression & equality-expression
9769
9770 exclusive-or-expression:
9771 and-expression
9772 exclusive-or-expression ^ and-expression
9773
9774 inclusive-or-expression:
9775 exclusive-or-expression
9776 inclusive-or-expression | exclusive-or-expression
9777
9778 logical-and-expression:
9779 inclusive-or-expression
9780 logical-and-expression && inclusive-or-expression
9781
9782 logical-or-expression:
9783 logical-and-expression
9784 logical-or-expression || logical-and-expression
9785
9786 All these are implemented with a single function like:
9787
9788 binary-expression:
9789 simple-cast-expression
9790 binary-expression <token> binary-expression
9791
9792 CAST_P is true if this expression is the target of a cast.
9793
9794 The binops_by_token map is used to get the tree codes for each <token> type.
9795 binary-expressions are associated according to a precedence table. */
9796
9797 #define TOKEN_PRECEDENCE(token) \
9798 (((token->type == CPP_GREATER \
9799 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9800 && !parser->greater_than_is_operator_p) \
9801 ? PREC_NOT_OPERATOR \
9802 : binops_by_token[token->type].prec)
9803
9804 static cp_expr
9805 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9806 bool no_toplevel_fold_p,
9807 bool decltype_p,
9808 enum cp_parser_prec prec,
9809 cp_id_kind * pidk)
9810 {
9811 cp_parser_expression_stack stack;
9812 cp_parser_expression_stack_entry *sp = &stack[0];
9813 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
9814 cp_parser_expression_stack_entry current;
9815 cp_expr rhs;
9816 cp_token *token;
9817 enum tree_code rhs_type;
9818 enum cp_parser_prec new_prec, lookahead_prec;
9819 tree overload;
9820
9821 /* Parse the first expression. */
9822 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9823 ? TRUTH_NOT_EXPR : ERROR_MARK);
9824 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9825 cast_p, decltype_p, pidk);
9826 current.prec = prec;
9827
9828 if (cp_parser_error_occurred (parser))
9829 return error_mark_node;
9830
9831 for (;;)
9832 {
9833 /* Get an operator token. */
9834 token = cp_lexer_peek_token (parser->lexer);
9835
9836 if (warn_cxx11_compat
9837 && token->type == CPP_RSHIFT
9838 && !parser->greater_than_is_operator_p)
9839 {
9840 if (warning_at (token->location, OPT_Wc__11_compat,
9841 "%<>>%> operator is treated"
9842 " as two right angle brackets in C++11"))
9843 inform (token->location,
9844 "suggest parentheses around %<>>%> expression");
9845 }
9846
9847 new_prec = TOKEN_PRECEDENCE (token);
9848 if (new_prec != PREC_NOT_OPERATOR
9849 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9850 /* This is a fold-expression; handle it later. */
9851 new_prec = PREC_NOT_OPERATOR;
9852
9853 /* Popping an entry off the stack means we completed a subexpression:
9854 - either we found a token which is not an operator (`>' where it is not
9855 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9856 will happen repeatedly;
9857 - or, we found an operator which has lower priority. This is the case
9858 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9859 parsing `3 * 4'. */
9860 if (new_prec <= current.prec)
9861 {
9862 if (sp == stack)
9863 break;
9864 else
9865 goto pop;
9866 }
9867
9868 get_rhs:
9869 current.tree_type = binops_by_token[token->type].tree_type;
9870 current.loc = token->location;
9871
9872 /* We used the operator token. */
9873 cp_lexer_consume_token (parser->lexer);
9874
9875 /* For "false && x" or "true || x", x will never be executed;
9876 disable warnings while evaluating it. */
9877 if ((current.tree_type == TRUTH_ANDIF_EXPR
9878 && cp_fully_fold (current.lhs) == truthvalue_false_node)
9879 || (current.tree_type == TRUTH_ORIF_EXPR
9880 && cp_fully_fold (current.lhs) == truthvalue_true_node))
9881 {
9882 disable_warnings_sp = sp;
9883 ++c_inhibit_evaluation_warnings;
9884 }
9885
9886 /* Extract another operand. It may be the RHS of this expression
9887 or the LHS of a new, higher priority expression. */
9888 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9889 ? TRUTH_NOT_EXPR : ERROR_MARK);
9890 rhs = cp_parser_simple_cast_expression (parser);
9891
9892 /* Get another operator token. Look up its precedence to avoid
9893 building a useless (immediately popped) stack entry for common
9894 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9895 token = cp_lexer_peek_token (parser->lexer);
9896 lookahead_prec = TOKEN_PRECEDENCE (token);
9897 if (lookahead_prec != PREC_NOT_OPERATOR
9898 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9899 lookahead_prec = PREC_NOT_OPERATOR;
9900 if (lookahead_prec > new_prec)
9901 {
9902 /* ... and prepare to parse the RHS of the new, higher priority
9903 expression. Since precedence levels on the stack are
9904 monotonically increasing, we do not have to care about
9905 stack overflows. */
9906 *sp = current;
9907 ++sp;
9908 current.lhs = rhs;
9909 current.lhs_type = rhs_type;
9910 current.prec = new_prec;
9911 new_prec = lookahead_prec;
9912 goto get_rhs;
9913
9914 pop:
9915 lookahead_prec = new_prec;
9916 /* If the stack is not empty, we have parsed into LHS the right side
9917 (`4' in the example above) of an expression we had suspended.
9918 We can use the information on the stack to recover the LHS (`3')
9919 from the stack together with the tree code (`MULT_EXPR'), and
9920 the precedence of the higher level subexpression
9921 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9922 which will be used to actually build the additive expression. */
9923 rhs = current.lhs;
9924 rhs_type = current.lhs_type;
9925 --sp;
9926 current = *sp;
9927 }
9928
9929 /* Undo the disabling of warnings done above. */
9930 if (sp == disable_warnings_sp)
9931 {
9932 disable_warnings_sp = NULL;
9933 --c_inhibit_evaluation_warnings;
9934 }
9935
9936 if (warn_logical_not_paren
9937 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9938 && current.lhs_type == TRUTH_NOT_EXPR
9939 /* Avoid warning for !!x == y. */
9940 && (TREE_CODE (current.lhs) != NE_EXPR
9941 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9942 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9943 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9944 /* Avoid warning for !b == y where b is boolean. */
9945 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9946 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9947 != BOOLEAN_TYPE))))
9948 /* Avoid warning for !!b == y where b is boolean. */
9949 && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
9950 || TREE_TYPE (current.lhs) == NULL_TREE
9951 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9952 warn_logical_not_parentheses (current.loc, current.tree_type,
9953 current.lhs, maybe_constant_value (rhs));
9954
9955 overload = NULL;
9956
9957 location_t combined_loc = make_location (current.loc,
9958 current.lhs.get_start (),
9959 rhs.get_finish ());
9960
9961 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9962 ERROR_MARK for everything that is not a binary expression.
9963 This makes warn_about_parentheses miss some warnings that
9964 involve unary operators. For unary expressions we should
9965 pass the correct tree_code unless the unary expression was
9966 surrounded by parentheses.
9967 */
9968 if (no_toplevel_fold_p
9969 && lookahead_prec <= current.prec
9970 && sp == stack)
9971 {
9972 if (current.lhs == error_mark_node || rhs == error_mark_node)
9973 current.lhs = error_mark_node;
9974 else
9975 {
9976 current.lhs.maybe_add_location_wrapper ();
9977 rhs.maybe_add_location_wrapper ();
9978 current.lhs
9979 = build_min (current.tree_type,
9980 TREE_CODE_CLASS (current.tree_type)
9981 == tcc_comparison
9982 ? boolean_type_node : TREE_TYPE (current.lhs),
9983 current.lhs.get_value (), rhs.get_value ());
9984 SET_EXPR_LOCATION (current.lhs, combined_loc);
9985 }
9986 }
9987 else
9988 {
9989 op_location_t op_loc (current.loc, combined_loc);
9990 current.lhs = build_x_binary_op (op_loc, current.tree_type,
9991 current.lhs, current.lhs_type,
9992 rhs, rhs_type, &overload,
9993 complain_flags (decltype_p));
9994 /* TODO: build_x_binary_op doesn't always honor the location. */
9995 current.lhs.set_location (combined_loc);
9996 }
9997 current.lhs_type = current.tree_type;
9998
9999 /* If the binary operator required the use of an overloaded operator,
10000 then this expression cannot be an integral constant-expression.
10001 An overloaded operator can be used even if both operands are
10002 otherwise permissible in an integral constant-expression if at
10003 least one of the operands is of enumeration type. */
10004
10005 if (overload
10006 && cp_parser_non_integral_constant_expression (parser,
10007 NIC_OVERLOADED))
10008 return error_mark_node;
10009 }
10010
10011 return current.lhs;
10012 }
10013
10014 static cp_expr
10015 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10016 bool no_toplevel_fold_p,
10017 enum cp_parser_prec prec,
10018 cp_id_kind * pidk)
10019 {
10020 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
10021 /*decltype*/false, prec, pidk);
10022 }
10023
10024 /* Parse the `? expression : assignment-expression' part of a
10025 conditional-expression. The LOGICAL_OR_EXPR is the
10026 logical-or-expression that started the conditional-expression.
10027 Returns a representation of the entire conditional-expression.
10028
10029 This routine is used by cp_parser_assignment_expression.
10030
10031 ? expression : assignment-expression
10032
10033 GNU Extensions:
10034
10035 ? : assignment-expression */
10036
10037 static tree
10038 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
10039 {
10040 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
10041 cp_expr assignment_expr;
10042 struct cp_token *token;
10043 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10044
10045 /* Consume the `?' token. */
10046 cp_lexer_consume_token (parser->lexer);
10047 token = cp_lexer_peek_token (parser->lexer);
10048 if (cp_parser_allow_gnu_extensions_p (parser)
10049 && token->type == CPP_COLON)
10050 {
10051 pedwarn (token->location, OPT_Wpedantic,
10052 "ISO C++ does not allow %<?:%> with omitted middle operand");
10053 /* Implicit true clause. */
10054 expr = NULL_TREE;
10055 c_inhibit_evaluation_warnings +=
10056 folded_logical_or_expr == truthvalue_true_node;
10057 warn_for_omitted_condop (token->location, logical_or_expr);
10058 }
10059 else
10060 {
10061 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10062 parser->colon_corrects_to_scope_p = false;
10063 /* Parse the expression. */
10064 c_inhibit_evaluation_warnings +=
10065 folded_logical_or_expr == truthvalue_false_node;
10066 expr = cp_parser_expression (parser);
10067 c_inhibit_evaluation_warnings +=
10068 ((folded_logical_or_expr == truthvalue_true_node)
10069 - (folded_logical_or_expr == truthvalue_false_node));
10070 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10071 }
10072
10073 /* The next token should be a `:'. */
10074 cp_parser_require (parser, CPP_COLON, RT_COLON);
10075 /* Parse the assignment-expression. */
10076 assignment_expr = cp_parser_assignment_expression (parser);
10077 c_inhibit_evaluation_warnings -=
10078 folded_logical_or_expr == truthvalue_true_node;
10079
10080 /* Make a location:
10081 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
10082 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
10083 with the caret at the "?", ranging from the start of
10084 the logical_or_expr to the end of the assignment_expr. */
10085 loc = make_location (loc,
10086 logical_or_expr.get_start (),
10087 assignment_expr.get_finish ());
10088
10089 /* Build the conditional-expression. */
10090 return build_x_conditional_expr (loc, logical_or_expr,
10091 expr,
10092 assignment_expr,
10093 tf_warning_or_error);
10094 }
10095
10096 /* Parse an assignment-expression.
10097
10098 assignment-expression:
10099 conditional-expression
10100 logical-or-expression assignment-operator assignment_expression
10101 throw-expression
10102 yield-expression
10103
10104 CAST_P is true if this expression is the target of a cast.
10105 DECLTYPE_P is true if this expression is the operand of decltype.
10106
10107 Returns a representation for the expression. */
10108
10109 static cp_expr
10110 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
10111 bool cast_p, bool decltype_p)
10112 {
10113 cp_expr expr;
10114
10115 /* If the next token is the `throw' keyword, then we're looking at
10116 a throw-expression. */
10117 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
10118 expr = cp_parser_throw_expression (parser);
10119 /* If the next token is the `co_yield' keyword, then we're looking at
10120 a yield-expression. */
10121 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CO_YIELD))
10122 expr = cp_parser_yield_expression (parser);
10123 /* Otherwise, it must be that we are looking at a
10124 logical-or-expression. */
10125 else
10126 {
10127 /* Parse the binary expressions (logical-or-expression). */
10128 expr = cp_parser_binary_expression (parser, cast_p, false,
10129 decltype_p,
10130 PREC_NOT_OPERATOR, pidk);
10131 /* If the next token is a `?' then we're actually looking at a
10132 conditional-expression. */
10133 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10134 return cp_parser_question_colon_clause (parser, expr);
10135 else
10136 {
10137 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10138
10139 /* If it's an assignment-operator, we're using the second
10140 production. */
10141 enum tree_code assignment_operator
10142 = cp_parser_assignment_operator_opt (parser);
10143 if (assignment_operator != ERROR_MARK)
10144 {
10145 bool non_constant_p;
10146
10147 /* Parse the right-hand side of the assignment. */
10148 cp_expr rhs = cp_parser_initializer_clause (parser,
10149 &non_constant_p);
10150
10151 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
10152 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10153
10154 /* An assignment may not appear in a
10155 constant-expression. */
10156 if (cp_parser_non_integral_constant_expression (parser,
10157 NIC_ASSIGNMENT))
10158 return error_mark_node;
10159 /* Build the assignment expression. Its default
10160 location:
10161 LHS = RHS
10162 ~~~~^~~~~
10163 is the location of the '=' token as the
10164 caret, ranging from the start of the lhs to the
10165 end of the rhs. */
10166 loc = make_location (loc,
10167 expr.get_start (),
10168 rhs.get_finish ());
10169 expr = build_x_modify_expr (loc, expr,
10170 assignment_operator,
10171 rhs,
10172 complain_flags (decltype_p));
10173 /* TODO: build_x_modify_expr doesn't honor the location,
10174 so we must set it here. */
10175 expr.set_location (loc);
10176 }
10177 }
10178 }
10179
10180 return expr;
10181 }
10182
10183 /* Parse an (optional) assignment-operator.
10184
10185 assignment-operator: one of
10186 = *= /= %= += -= >>= <<= &= ^= |=
10187
10188 GNU Extension:
10189
10190 assignment-operator: one of
10191 <?= >?=
10192
10193 If the next token is an assignment operator, the corresponding tree
10194 code is returned, and the token is consumed. For example, for
10195 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
10196 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
10197 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
10198 operator, ERROR_MARK is returned. */
10199
10200 static enum tree_code
10201 cp_parser_assignment_operator_opt (cp_parser* parser)
10202 {
10203 enum tree_code op;
10204 cp_token *token;
10205
10206 /* Peek at the next token. */
10207 token = cp_lexer_peek_token (parser->lexer);
10208
10209 switch (token->type)
10210 {
10211 case CPP_EQ:
10212 op = NOP_EXPR;
10213 break;
10214
10215 case CPP_MULT_EQ:
10216 op = MULT_EXPR;
10217 break;
10218
10219 case CPP_DIV_EQ:
10220 op = TRUNC_DIV_EXPR;
10221 break;
10222
10223 case CPP_MOD_EQ:
10224 op = TRUNC_MOD_EXPR;
10225 break;
10226
10227 case CPP_PLUS_EQ:
10228 op = PLUS_EXPR;
10229 break;
10230
10231 case CPP_MINUS_EQ:
10232 op = MINUS_EXPR;
10233 break;
10234
10235 case CPP_RSHIFT_EQ:
10236 op = RSHIFT_EXPR;
10237 break;
10238
10239 case CPP_LSHIFT_EQ:
10240 op = LSHIFT_EXPR;
10241 break;
10242
10243 case CPP_AND_EQ:
10244 op = BIT_AND_EXPR;
10245 break;
10246
10247 case CPP_XOR_EQ:
10248 op = BIT_XOR_EXPR;
10249 break;
10250
10251 case CPP_OR_EQ:
10252 op = BIT_IOR_EXPR;
10253 break;
10254
10255 default:
10256 /* Nothing else is an assignment operator. */
10257 op = ERROR_MARK;
10258 }
10259
10260 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10261 if (op != ERROR_MARK
10262 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10263 op = ERROR_MARK;
10264
10265 /* If it was an assignment operator, consume it. */
10266 if (op != ERROR_MARK)
10267 cp_lexer_consume_token (parser->lexer);
10268
10269 return op;
10270 }
10271
10272 /* Parse an expression.
10273
10274 expression:
10275 assignment-expression
10276 expression , assignment-expression
10277
10278 CAST_P is true if this expression is the target of a cast.
10279 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10280 except possibly parenthesized or on the RHS of a comma (N3276).
10281 WARN_COMMA_P is true if a comma should be diagnosed.
10282
10283 Returns a representation of the expression. */
10284
10285 static cp_expr
10286 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
10287 bool cast_p, bool decltype_p, bool warn_comma_p)
10288 {
10289 cp_expr expression = NULL_TREE;
10290 location_t loc = UNKNOWN_LOCATION;
10291
10292 while (true)
10293 {
10294 cp_expr assignment_expression;
10295
10296 /* Parse the next assignment-expression. */
10297 assignment_expression
10298 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
10299
10300 /* We don't create a temporary for a call that is the immediate operand
10301 of decltype or on the RHS of a comma. But when we see a comma, we
10302 need to create a temporary for a call on the LHS. */
10303 if (decltype_p && !processing_template_decl
10304 && TREE_CODE (assignment_expression) == CALL_EXPR
10305 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
10306 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10307 assignment_expression
10308 = build_cplus_new (TREE_TYPE (assignment_expression),
10309 assignment_expression, tf_warning_or_error);
10310
10311 /* If this is the first assignment-expression, we can just
10312 save it away. */
10313 if (!expression)
10314 expression = assignment_expression;
10315 else
10316 {
10317 /* Create a location with caret at the comma, ranging
10318 from the start of the LHS to the end of the RHS. */
10319 loc = make_location (loc,
10320 expression.get_start (),
10321 assignment_expression.get_finish ());
10322 expression = build_x_compound_expr (loc, expression,
10323 assignment_expression,
10324 complain_flags (decltype_p));
10325 expression.set_location (loc);
10326 }
10327 /* If the next token is not a comma, or we're in a fold-expression, then
10328 we are done with the expression. */
10329 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
10330 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10331 break;
10332 /* Consume the `,'. */
10333 loc = cp_lexer_peek_token (parser->lexer)->location;
10334 if (warn_comma_p)
10335 {
10336 /* [depr.comma.subscript]: A comma expression appearing as
10337 the expr-or-braced-init-list of a subscripting expression
10338 is deprecated. A parenthesized comma expression is not
10339 deprecated. */
10340 warning_at (loc, OPT_Wcomma_subscript,
10341 "top-level comma expression in array subscript "
10342 "is deprecated");
10343 warn_comma_p = false;
10344 }
10345 cp_lexer_consume_token (parser->lexer);
10346 /* A comma operator cannot appear in a constant-expression. */
10347 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
10348 expression = error_mark_node;
10349 }
10350
10351 return expression;
10352 }
10353
10354 /* Parse a constant-expression.
10355
10356 constant-expression:
10357 conditional-expression
10358
10359 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10360 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10361 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10362 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
10363 only parse a conditional-expression, otherwise parse an
10364 assignment-expression. See below for rationale. */
10365
10366 static cp_expr
10367 cp_parser_constant_expression (cp_parser* parser,
10368 bool allow_non_constant_p,
10369 bool *non_constant_p,
10370 bool strict_p)
10371 {
10372 bool saved_integral_constant_expression_p;
10373 bool saved_allow_non_integral_constant_expression_p;
10374 bool saved_non_integral_constant_expression_p;
10375 cp_expr expression;
10376
10377 /* It might seem that we could simply parse the
10378 conditional-expression, and then check to see if it were
10379 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10380 one that the compiler can figure out is constant, possibly after
10381 doing some simplifications or optimizations. The standard has a
10382 precise definition of constant-expression, and we must honor
10383 that, even though it is somewhat more restrictive.
10384
10385 For example:
10386
10387 int i[(2, 3)];
10388
10389 is not a legal declaration, because `(2, 3)' is not a
10390 constant-expression. The `,' operator is forbidden in a
10391 constant-expression. However, GCC's constant-folding machinery
10392 will fold this operation to an INTEGER_CST for `3'. */
10393
10394 /* Save the old settings. */
10395 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10396 saved_allow_non_integral_constant_expression_p
10397 = parser->allow_non_integral_constant_expression_p;
10398 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10399 /* We are now parsing a constant-expression. */
10400 parser->integral_constant_expression_p = true;
10401 parser->allow_non_integral_constant_expression_p
10402 = (allow_non_constant_p || cxx_dialect >= cxx11);
10403 parser->non_integral_constant_expression_p = false;
10404 /* Although the grammar says "conditional-expression", when not STRICT_P,
10405 we parse an "assignment-expression", which also permits
10406 "throw-expression" and the use of assignment operators. In the case
10407 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10408 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10409 actually essential that we look for an assignment-expression.
10410 For example, cp_parser_initializer_clauses uses this function to
10411 determine whether a particular assignment-expression is in fact
10412 constant. */
10413 if (strict_p)
10414 {
10415 /* Parse the binary expressions (logical-or-expression). */
10416 expression = cp_parser_binary_expression (parser, false, false, false,
10417 PREC_NOT_OPERATOR, NULL);
10418 /* If the next token is a `?' then we're actually looking at
10419 a conditional-expression; otherwise we're done. */
10420 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10421 expression = cp_parser_question_colon_clause (parser, expression);
10422 }
10423 else
10424 expression = cp_parser_assignment_expression (parser);
10425 /* Restore the old settings. */
10426 parser->integral_constant_expression_p
10427 = saved_integral_constant_expression_p;
10428 parser->allow_non_integral_constant_expression_p
10429 = saved_allow_non_integral_constant_expression_p;
10430 if (cxx_dialect >= cxx11)
10431 {
10432 /* Require an rvalue constant expression here; that's what our
10433 callers expect. Reference constant expressions are handled
10434 separately in e.g. cp_parser_template_argument. */
10435 tree decay = expression;
10436 if (TREE_TYPE (expression)
10437 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10438 decay = build_address (expression);
10439 bool is_const = is_rvalue_constant_expression (decay);
10440 parser->non_integral_constant_expression_p = !is_const;
10441 if (!is_const && !allow_non_constant_p)
10442 require_rvalue_constant_expression (decay);
10443 }
10444 if (allow_non_constant_p)
10445 *non_constant_p = parser->non_integral_constant_expression_p;
10446 parser->non_integral_constant_expression_p
10447 = saved_non_integral_constant_expression_p;
10448
10449 return expression;
10450 }
10451
10452 /* Parse __builtin_offsetof.
10453
10454 offsetof-expression:
10455 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10456
10457 offsetof-member-designator:
10458 id-expression
10459 | offsetof-member-designator "." id-expression
10460 | offsetof-member-designator "[" expression "]"
10461 | offsetof-member-designator "->" id-expression */
10462
10463 static cp_expr
10464 cp_parser_builtin_offsetof (cp_parser *parser)
10465 {
10466 int save_ice_p, save_non_ice_p;
10467 tree type;
10468 cp_expr expr;
10469 cp_id_kind dummy;
10470 cp_token *token;
10471 location_t finish_loc;
10472
10473 /* We're about to accept non-integral-constant things, but will
10474 definitely yield an integral constant expression. Save and
10475 restore these values around our local parsing. */
10476 save_ice_p = parser->integral_constant_expression_p;
10477 save_non_ice_p = parser->non_integral_constant_expression_p;
10478
10479 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10480
10481 /* Consume the "__builtin_offsetof" token. */
10482 cp_lexer_consume_token (parser->lexer);
10483 /* Consume the opening `('. */
10484 matching_parens parens;
10485 parens.require_open (parser);
10486 /* Parse the type-id. */
10487 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10488 {
10489 const char *saved_message = parser->type_definition_forbidden_message;
10490 parser->type_definition_forbidden_message
10491 = G_("types may not be defined within %<__builtin_offsetof%>");
10492 type = cp_parser_type_id (parser);
10493 parser->type_definition_forbidden_message = saved_message;
10494 }
10495 /* Look for the `,'. */
10496 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10497 token = cp_lexer_peek_token (parser->lexer);
10498
10499 /* Build the (type *)null that begins the traditional offsetof macro. */
10500 tree object_ptr
10501 = build_static_cast (input_location, build_pointer_type (type),
10502 null_pointer_node, tf_warning_or_error);
10503
10504 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10505 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10506 true, &dummy, token->location);
10507 while (true)
10508 {
10509 token = cp_lexer_peek_token (parser->lexer);
10510 switch (token->type)
10511 {
10512 case CPP_OPEN_SQUARE:
10513 /* offsetof-member-designator "[" expression "]" */
10514 expr = cp_parser_postfix_open_square_expression (parser, expr,
10515 true, false);
10516 break;
10517
10518 case CPP_DEREF:
10519 /* offsetof-member-designator "->" identifier */
10520 expr = grok_array_decl (token->location, expr,
10521 integer_zero_node, false);
10522 /* FALLTHRU */
10523
10524 case CPP_DOT:
10525 /* offsetof-member-designator "." identifier */
10526 cp_lexer_consume_token (parser->lexer);
10527 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10528 expr, true, &dummy,
10529 token->location);
10530 break;
10531
10532 case CPP_CLOSE_PAREN:
10533 /* Consume the ")" token. */
10534 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10535 cp_lexer_consume_token (parser->lexer);
10536 goto success;
10537
10538 default:
10539 /* Error. We know the following require will fail, but
10540 that gives the proper error message. */
10541 parens.require_close (parser);
10542 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10543 expr = error_mark_node;
10544 goto failure;
10545 }
10546 }
10547
10548 success:
10549 /* Make a location of the form:
10550 __builtin_offsetof (struct s, f)
10551 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10552 with caret at the type-id, ranging from the start of the
10553 "_builtin_offsetof" token to the close paren. */
10554 loc = make_location (loc, start_loc, finish_loc);
10555 /* The result will be an INTEGER_CST, so we need to explicitly
10556 preserve the location. */
10557 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10558
10559 failure:
10560 parser->integral_constant_expression_p = save_ice_p;
10561 parser->non_integral_constant_expression_p = save_non_ice_p;
10562
10563 expr = expr.maybe_add_location_wrapper ();
10564 return expr;
10565 }
10566
10567 /* Parse a trait expression.
10568
10569 Returns a representation of the expression, the underlying type
10570 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10571
10572 static cp_expr
10573 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10574 {
10575 cp_trait_kind kind;
10576 tree type1, type2 = NULL_TREE;
10577 bool binary = false;
10578 bool variadic = false;
10579
10580 switch (keyword)
10581 {
10582 case RID_HAS_NOTHROW_ASSIGN:
10583 kind = CPTK_HAS_NOTHROW_ASSIGN;
10584 break;
10585 case RID_HAS_NOTHROW_CONSTRUCTOR:
10586 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10587 break;
10588 case RID_HAS_NOTHROW_COPY:
10589 kind = CPTK_HAS_NOTHROW_COPY;
10590 break;
10591 case RID_HAS_TRIVIAL_ASSIGN:
10592 kind = CPTK_HAS_TRIVIAL_ASSIGN;
10593 break;
10594 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10595 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10596 break;
10597 case RID_HAS_TRIVIAL_COPY:
10598 kind = CPTK_HAS_TRIVIAL_COPY;
10599 break;
10600 case RID_HAS_TRIVIAL_DESTRUCTOR:
10601 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10602 break;
10603 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10604 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10605 break;
10606 case RID_HAS_VIRTUAL_DESTRUCTOR:
10607 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10608 break;
10609 case RID_IS_ABSTRACT:
10610 kind = CPTK_IS_ABSTRACT;
10611 break;
10612 case RID_IS_AGGREGATE:
10613 kind = CPTK_IS_AGGREGATE;
10614 break;
10615 case RID_IS_BASE_OF:
10616 kind = CPTK_IS_BASE_OF;
10617 binary = true;
10618 break;
10619 case RID_IS_CLASS:
10620 kind = CPTK_IS_CLASS;
10621 break;
10622 case RID_IS_EMPTY:
10623 kind = CPTK_IS_EMPTY;
10624 break;
10625 case RID_IS_ENUM:
10626 kind = CPTK_IS_ENUM;
10627 break;
10628 case RID_IS_FINAL:
10629 kind = CPTK_IS_FINAL;
10630 break;
10631 case RID_IS_LITERAL_TYPE:
10632 kind = CPTK_IS_LITERAL_TYPE;
10633 break;
10634 case RID_IS_POD:
10635 kind = CPTK_IS_POD;
10636 break;
10637 case RID_IS_POLYMORPHIC:
10638 kind = CPTK_IS_POLYMORPHIC;
10639 break;
10640 case RID_IS_SAME_AS:
10641 kind = CPTK_IS_SAME_AS;
10642 binary = true;
10643 break;
10644 case RID_IS_STD_LAYOUT:
10645 kind = CPTK_IS_STD_LAYOUT;
10646 break;
10647 case RID_IS_TRIVIAL:
10648 kind = CPTK_IS_TRIVIAL;
10649 break;
10650 case RID_IS_TRIVIALLY_ASSIGNABLE:
10651 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10652 binary = true;
10653 break;
10654 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10655 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10656 variadic = true;
10657 break;
10658 case RID_IS_TRIVIALLY_COPYABLE:
10659 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10660 break;
10661 case RID_IS_UNION:
10662 kind = CPTK_IS_UNION;
10663 break;
10664 case RID_UNDERLYING_TYPE:
10665 kind = CPTK_UNDERLYING_TYPE;
10666 break;
10667 case RID_BASES:
10668 kind = CPTK_BASES;
10669 break;
10670 case RID_DIRECT_BASES:
10671 kind = CPTK_DIRECT_BASES;
10672 break;
10673 case RID_IS_ASSIGNABLE:
10674 kind = CPTK_IS_ASSIGNABLE;
10675 binary = true;
10676 break;
10677 case RID_IS_CONSTRUCTIBLE:
10678 kind = CPTK_IS_CONSTRUCTIBLE;
10679 variadic = true;
10680 break;
10681 case RID_IS_NOTHROW_ASSIGNABLE:
10682 kind = CPTK_IS_NOTHROW_ASSIGNABLE;
10683 binary = true;
10684 break;
10685 case RID_IS_NOTHROW_CONSTRUCTIBLE:
10686 kind = CPTK_IS_NOTHROW_CONSTRUCTIBLE;
10687 variadic = true;
10688 break;
10689 default:
10690 gcc_unreachable ();
10691 }
10692
10693 /* Get location of initial token. */
10694 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10695
10696 /* Consume the token. */
10697 cp_lexer_consume_token (parser->lexer);
10698
10699 matching_parens parens;
10700 parens.require_open (parser);
10701
10702 {
10703 type_id_in_expr_sentinel s (parser);
10704 type1 = cp_parser_type_id (parser);
10705 }
10706
10707 if (type1 == error_mark_node)
10708 return error_mark_node;
10709
10710 if (binary)
10711 {
10712 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10713
10714 {
10715 type_id_in_expr_sentinel s (parser);
10716 type2 = cp_parser_type_id (parser);
10717 }
10718
10719 if (type2 == error_mark_node)
10720 return error_mark_node;
10721 }
10722 else if (variadic)
10723 {
10724 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10725 {
10726 cp_lexer_consume_token (parser->lexer);
10727 tree elt = cp_parser_type_id (parser);
10728 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10729 {
10730 cp_lexer_consume_token (parser->lexer);
10731 elt = make_pack_expansion (elt);
10732 }
10733 if (elt == error_mark_node)
10734 return error_mark_node;
10735 type2 = tree_cons (NULL_TREE, elt, type2);
10736 }
10737 }
10738
10739 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10740 parens.require_close (parser);
10741
10742 /* Construct a location of the form:
10743 __is_trivially_copyable(_Tp)
10744 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10745 with start == caret, finishing at the close-paren. */
10746 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10747
10748 /* Complete the trait expression, which may mean either processing
10749 the trait expr now or saving it for template instantiation. */
10750 switch (kind)
10751 {
10752 case CPTK_UNDERLYING_TYPE:
10753 return cp_expr (finish_underlying_type (type1), trait_loc);
10754 case CPTK_BASES:
10755 return cp_expr (finish_bases (type1, false), trait_loc);
10756 case CPTK_DIRECT_BASES:
10757 return cp_expr (finish_bases (type1, true), trait_loc);
10758 default:
10759 return finish_trait_expr (trait_loc, kind, type1, type2);
10760 }
10761 }
10762
10763 /* Parse a lambda expression.
10764
10765 lambda-expression:
10766 lambda-introducer lambda-declarator [opt] compound-statement
10767 lambda-introducer < template-parameter-list > requires-clause [opt]
10768 lambda-declarator [opt] compound-statement
10769
10770 Returns a representation of the expression. */
10771
10772 static cp_expr
10773 cp_parser_lambda_expression (cp_parser* parser)
10774 {
10775 tree lambda_expr = build_lambda_expr ();
10776 tree type;
10777 bool ok = true;
10778 cp_token *token = cp_lexer_peek_token (parser->lexer);
10779 cp_token_position start = 0;
10780
10781 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10782
10783 if (cxx_dialect >= cxx20)
10784 /* C++20 allows lambdas in unevaluated context. */;
10785 else if (cp_unevaluated_operand)
10786 {
10787 if (!token->error_reported)
10788 {
10789 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10790 "lambda-expression in unevaluated context"
10791 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
10792 token->error_reported = true;
10793 }
10794 ok = false;
10795 }
10796 else if (parser->in_template_argument_list_p || processing_template_parmlist)
10797 {
10798 if (!token->error_reported)
10799 {
10800 error_at (token->location, "lambda-expression in template-argument"
10801 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
10802 token->error_reported = true;
10803 }
10804 ok = false;
10805 }
10806
10807 /* We may be in the middle of deferred access check. Disable
10808 it now. */
10809 push_deferring_access_checks (dk_no_deferred);
10810
10811 cp_parser_lambda_introducer (parser, lambda_expr);
10812 if (cp_parser_error_occurred (parser))
10813 return error_mark_node;
10814
10815 type = begin_lambda_type (lambda_expr);
10816 if (type == error_mark_node)
10817 return error_mark_node;
10818
10819 record_lambda_scope (lambda_expr);
10820
10821 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10822 determine_visibility (TYPE_NAME (type));
10823
10824 /* Now that we've started the type, add the capture fields for any
10825 explicit captures. */
10826 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10827
10828 {
10829 /* Inside the class, surrounding template-parameter-lists do not apply. */
10830 unsigned int saved_num_template_parameter_lists
10831 = parser->num_template_parameter_lists;
10832 unsigned char in_statement = parser->in_statement;
10833 bool in_switch_statement_p = parser->in_switch_statement_p;
10834 bool fully_implicit_function_template_p
10835 = parser->fully_implicit_function_template_p;
10836 tree implicit_template_parms = parser->implicit_template_parms;
10837 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10838 bool auto_is_implicit_function_template_parm_p
10839 = parser->auto_is_implicit_function_template_parm_p;
10840
10841 parser->num_template_parameter_lists = 0;
10842 parser->in_statement = 0;
10843 parser->in_switch_statement_p = false;
10844 parser->fully_implicit_function_template_p = false;
10845 parser->implicit_template_parms = 0;
10846 parser->implicit_template_scope = 0;
10847 parser->auto_is_implicit_function_template_parm_p = false;
10848
10849 /* The body of a lambda in a discarded statement is not discarded. */
10850 bool discarded = in_discarded_stmt;
10851 in_discarded_stmt = 0;
10852
10853 /* By virtue of defining a local class, a lambda expression has access to
10854 the private variables of enclosing classes. */
10855
10856 if (cp_parser_start_tentative_firewall (parser))
10857 start = token;
10858
10859 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10860
10861 if (ok && cp_parser_error_occurred (parser))
10862 ok = false;
10863
10864 if (ok)
10865 {
10866 cp_parser_lambda_body (parser, lambda_expr);
10867 }
10868 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10869 {
10870 if (cp_parser_skip_to_closing_brace (parser))
10871 cp_lexer_consume_token (parser->lexer);
10872 }
10873
10874 /* The capture list was built up in reverse order; fix that now. */
10875 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10876 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10877
10878 if (ok)
10879 maybe_add_lambda_conv_op (type);
10880
10881 finish_struct (type, /*attributes=*/NULL_TREE);
10882
10883 in_discarded_stmt = discarded;
10884
10885 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10886 parser->in_statement = in_statement;
10887 parser->in_switch_statement_p = in_switch_statement_p;
10888 parser->fully_implicit_function_template_p
10889 = fully_implicit_function_template_p;
10890 parser->implicit_template_parms = implicit_template_parms;
10891 parser->implicit_template_scope = implicit_template_scope;
10892 parser->auto_is_implicit_function_template_parm_p
10893 = auto_is_implicit_function_template_parm_p;
10894 }
10895
10896 /* This field is only used during parsing of the lambda. */
10897 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10898
10899 /* This lambda shouldn't have any proxies left at this point. */
10900 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10901 /* And now that we're done, push proxies for an enclosing lambda. */
10902 insert_pending_capture_proxies ();
10903
10904 /* Update the lambda expression to a range. */
10905 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10906 token->location,
10907 parser->lexer);
10908
10909 if (ok)
10910 lambda_expr = build_lambda_object (lambda_expr);
10911 else
10912 lambda_expr = error_mark_node;
10913
10914 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10915
10916 pop_deferring_access_checks ();
10917
10918 return lambda_expr;
10919 }
10920
10921 /* Parse the beginning of a lambda expression.
10922
10923 lambda-introducer:
10924 [ lambda-capture [opt] ]
10925
10926 LAMBDA_EXPR is the current representation of the lambda expression. */
10927
10928 static void
10929 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10930 {
10931 /* Need commas after the first capture. */
10932 bool first = true;
10933
10934 /* Eat the leading `['. */
10935 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10936
10937 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10938 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10939 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS)
10940 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
10941 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10942 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10943 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10944 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10945
10946 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10947 {
10948 cp_lexer_consume_token (parser->lexer);
10949 first = false;
10950
10951 if (!(at_function_scope_p () || parsing_nsdmi ()))
10952 error ("non-local lambda expression cannot have a capture-default");
10953 }
10954
10955 hash_set<tree, true> ids;
10956 tree first_capture_id = NULL_TREE;
10957 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10958 {
10959 cp_token* capture_token;
10960 tree capture_id;
10961 tree capture_init_expr;
10962 cp_id_kind idk = CP_ID_KIND_NONE;
10963 bool explicit_init_p = false;
10964
10965 enum capture_kind_type
10966 {
10967 BY_COPY,
10968 BY_REFERENCE
10969 };
10970 enum capture_kind_type capture_kind = BY_COPY;
10971
10972 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10973 {
10974 error ("expected end of capture-list");
10975 return;
10976 }
10977
10978 if (first)
10979 first = false;
10980 else
10981 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10982
10983 /* Possibly capture `this'. */
10984 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10985 {
10986 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10987 if (cxx_dialect < cxx20
10988 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10989 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10990 "with by-copy capture default");
10991 cp_lexer_consume_token (parser->lexer);
10992 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10993 pedwarn (input_location, 0,
10994 "already captured %qD in lambda expression",
10995 this_identifier);
10996 else
10997 add_capture (lambda_expr, /*id=*/this_identifier,
10998 /*initializer=*/finish_this_expr (),
10999 /*by_reference_p=*/true, explicit_init_p);
11000 continue;
11001 }
11002
11003 /* Possibly capture `*this'. */
11004 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
11005 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11006 {
11007 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11008 if (cxx_dialect < cxx17)
11009 pedwarn (loc, 0, "%<*this%> capture only available with "
11010 "%<-std=c++17%> or %<-std=gnu++17%>");
11011 cp_lexer_consume_token (parser->lexer);
11012 cp_lexer_consume_token (parser->lexer);
11013 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11014 pedwarn (input_location, 0,
11015 "already captured %qD in lambda expression",
11016 this_identifier);
11017 else
11018 add_capture (lambda_expr, /*id=*/this_identifier,
11019 /*initializer=*/finish_this_expr (),
11020 /*by_reference_p=*/false, explicit_init_p);
11021 continue;
11022 }
11023
11024 /* But reject `&this'. */
11025 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11026 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11027 {
11028 error_at (cp_lexer_peek_token (parser->lexer)->location,
11029 "%<this%> cannot be captured by reference");
11030 cp_lexer_consume_token (parser->lexer);
11031 cp_lexer_consume_token (parser->lexer);
11032 continue;
11033 }
11034
11035 /* Remember whether we want to capture as a reference or not. */
11036 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
11037 {
11038 capture_kind = BY_REFERENCE;
11039 cp_lexer_consume_token (parser->lexer);
11040 }
11041
11042 bool init_pack_expansion = false;
11043 location_t ellipsis_loc = UNKNOWN_LOCATION;
11044 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11045 {
11046 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
11047 if (cxx_dialect < cxx20)
11048 pedwarn (ellipsis_loc, 0, "pack init-capture only available with "
11049 "%<-std=c++20%> or %<-std=gnu++20%>");
11050 cp_lexer_consume_token (parser->lexer);
11051 init_pack_expansion = true;
11052 }
11053
11054 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
11055 if (init_pack_expansion && capture_kind != BY_REFERENCE
11056 && cp_lexer_next_token_is (parser->lexer, CPP_AND))
11057 {
11058 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
11059 0, "%<&%> should come before %<...%>");
11060 capture_kind = BY_REFERENCE;
11061 cp_lexer_consume_token (parser->lexer);
11062 }
11063
11064 /* Get the identifier. */
11065 capture_token = cp_lexer_peek_token (parser->lexer);
11066 capture_id = cp_parser_identifier (parser);
11067
11068 if (capture_id == error_mark_node)
11069 /* Would be nice to have a cp_parser_skip_to_closing_x for general
11070 delimiters, but I modified this to stop on unnested ']' as well. It
11071 was already changed to stop on unnested '}', so the
11072 "closing_parenthesis" name is no more misleading with my change. */
11073 {
11074 cp_parser_skip_to_closing_parenthesis (parser,
11075 /*recovering=*/true,
11076 /*or_comma=*/true,
11077 /*consume_paren=*/true);
11078 break;
11079 }
11080
11081 /* Find the initializer for this capture. */
11082 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11083 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11084 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11085 {
11086 bool direct, non_constant;
11087 /* An explicit initializer exists. */
11088 if (cxx_dialect < cxx14)
11089 pedwarn (input_location, 0,
11090 "lambda capture initializers "
11091 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
11092 capture_init_expr = cp_parser_initializer (parser, &direct,
11093 &non_constant, true);
11094 explicit_init_p = true;
11095 if (capture_init_expr == NULL_TREE)
11096 {
11097 error ("empty initializer for lambda init-capture");
11098 capture_init_expr = error_mark_node;
11099 }
11100 if (init_pack_expansion)
11101 capture_init_expr = make_pack_expansion (capture_init_expr);
11102 }
11103 else
11104 {
11105 const char* error_msg;
11106
11107 /* Turn the identifier into an id-expression. */
11108 capture_init_expr
11109 = cp_parser_lookup_name_simple (parser, capture_id,
11110 capture_token->location);
11111
11112 if (capture_init_expr == error_mark_node)
11113 {
11114 unqualified_name_lookup_error (capture_id);
11115 continue;
11116 }
11117 else if (!VAR_P (capture_init_expr)
11118 && TREE_CODE (capture_init_expr) != PARM_DECL)
11119 {
11120 error_at (capture_token->location,
11121 "capture of non-variable %qE",
11122 capture_init_expr);
11123 if (DECL_P (capture_init_expr))
11124 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11125 "%q#D declared here", capture_init_expr);
11126 continue;
11127 }
11128 if (VAR_P (capture_init_expr)
11129 && decl_storage_duration (capture_init_expr) != dk_auto)
11130 {
11131 if (pedwarn (capture_token->location, 0, "capture of variable "
11132 "%qD with non-automatic storage duration",
11133 capture_init_expr))
11134 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11135 "%q#D declared here", capture_init_expr);
11136 continue;
11137 }
11138
11139 capture_init_expr
11140 = finish_id_expression
11141 (capture_id,
11142 capture_init_expr,
11143 parser->scope,
11144 &idk,
11145 /*integral_constant_expression_p=*/false,
11146 /*allow_non_integral_constant_expression_p=*/false,
11147 /*non_integral_constant_expression_p=*/NULL,
11148 /*template_p=*/false,
11149 /*done=*/true,
11150 /*address_p=*/false,
11151 /*template_arg_p=*/false,
11152 &error_msg,
11153 capture_token->location);
11154
11155 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11156 {
11157 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11158 cp_lexer_consume_token (parser->lexer);
11159 capture_init_expr = make_pack_expansion (capture_init_expr);
11160 if (init_pack_expansion)
11161 {
11162 /* If what follows is an initializer, the second '...' is
11163 invalid. But for cases like [...xs...], the first one
11164 is invalid. */
11165 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11166 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11167 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11168 ellipsis_loc = loc;
11169 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
11170 continue;
11171 }
11172 }
11173 }
11174
11175 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11176 && !explicit_init_p)
11177 {
11178 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
11179 && capture_kind == BY_COPY)
11180 pedwarn (capture_token->location, 0, "explicit by-copy capture "
11181 "of %qD redundant with by-copy capture default",
11182 capture_id);
11183 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
11184 && capture_kind == BY_REFERENCE)
11185 pedwarn (capture_token->location, 0, "explicit by-reference "
11186 "capture of %qD redundant with by-reference capture "
11187 "default", capture_id);
11188 }
11189
11190 /* Check for duplicates.
11191 Optimize for the zero or one explicit captures cases and only create
11192 the hash_set after adding second capture. */
11193 bool found = false;
11194 if (!ids.is_empty ())
11195 found = ids.add (capture_id);
11196 else if (first_capture_id == NULL_TREE)
11197 first_capture_id = capture_id;
11198 else if (capture_id == first_capture_id)
11199 found = true;
11200 else
11201 {
11202 ids.add (first_capture_id);
11203 ids.add (capture_id);
11204 }
11205 if (found)
11206 pedwarn (input_location, 0,
11207 "already captured %qD in lambda expression", capture_id);
11208 else
11209 add_capture (lambda_expr, capture_id, capture_init_expr,
11210 /*by_reference_p=*/capture_kind == BY_REFERENCE,
11211 explicit_init_p);
11212
11213 /* If there is any qualification still in effect, clear it
11214 now; we will be starting fresh with the next capture. */
11215 parser->scope = NULL_TREE;
11216 parser->qualifying_scope = NULL_TREE;
11217 parser->object_scope = NULL_TREE;
11218 }
11219
11220 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11221 }
11222
11223 /* Parse the (optional) middle of a lambda expression.
11224
11225 lambda-declarator:
11226 ( parameter-declaration-clause )
11227 decl-specifier-seq [opt]
11228 noexcept-specifier [opt]
11229 attribute-specifier-seq [opt]
11230 trailing-return-type [opt]
11231 requires-clause [opt]
11232
11233 LAMBDA_EXPR is the current representation of the lambda expression. */
11234
11235 static bool
11236 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
11237 {
11238 /* 5.1.1.4 of the standard says:
11239 If a lambda-expression does not include a lambda-declarator, it is as if
11240 the lambda-declarator were ().
11241 This means an empty parameter list, no attributes, and no exception
11242 specification. */
11243 tree param_list = void_list_node;
11244 tree std_attrs = NULL_TREE;
11245 tree gnu_attrs = NULL_TREE;
11246 tree exception_spec = NULL_TREE;
11247 tree template_param_list = NULL_TREE;
11248 tree tx_qual = NULL_TREE;
11249 tree return_type = NULL_TREE;
11250 tree trailing_requires_clause = NULL_TREE;
11251 cp_decl_specifier_seq lambda_specs;
11252 clear_decl_specs (&lambda_specs);
11253 /* A lambda op() is const unless explicitly 'mutable'. */
11254 cp_cv_quals quals = TYPE_QUAL_CONST;
11255
11256 /* The template-parameter-list is optional, but must begin with
11257 an opening angle if present. */
11258 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
11259 {
11260 if (cxx_dialect < cxx14)
11261 pedwarn (parser->lexer->next_token->location, 0,
11262 "lambda templates are only available with "
11263 "%<-std=c++14%> or %<-std=gnu++14%>");
11264 else if (cxx_dialect < cxx20)
11265 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
11266 "lambda templates are only available with "
11267 "%<-std=c++20%> or %<-std=gnu++20%>");
11268
11269 cp_lexer_consume_token (parser->lexer);
11270
11271 template_param_list = cp_parser_template_parameter_list (parser);
11272 cp_parser_skip_to_end_of_template_parameter_list (parser);
11273
11274 /* We may have a constrained generic lambda; parse the requires-clause
11275 immediately after the template-parameter-list and combine with any
11276 shorthand constraints present. */
11277 tree dreqs = cp_parser_requires_clause_opt (parser, true);
11278 if (flag_concepts)
11279 {
11280 tree reqs = get_shorthand_constraints (current_template_parms);
11281 if (dreqs)
11282 reqs = combine_constraint_expressions (reqs, dreqs);
11283 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
11284 }
11285
11286 /* We just processed one more parameter list. */
11287 ++parser->num_template_parameter_lists;
11288 }
11289
11290 /* Committee discussion supports allowing attributes here. */
11291 lambda_specs.attributes = cp_parser_attributes_opt (parser);
11292
11293 /* The parameter-declaration-clause is optional (unless
11294 template-parameter-list was given), but must begin with an
11295 opening parenthesis if present. */
11296 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11297 {
11298 bool is_consteval = false;
11299 /* For C++20, before parsing the parameter list check if there is
11300 a consteval specifier in the corresponding decl-specifier-seq. */
11301 if (cxx_dialect >= cxx20)
11302 {
11303 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1);
11304 cp_lexer_nth_token_is (parser->lexer, n, CPP_KEYWORD); n++)
11305 {
11306 if (cp_lexer_peek_nth_token (parser->lexer, n)->keyword
11307 == RID_CONSTEVAL)
11308 {
11309 is_consteval = true;
11310 break;
11311 }
11312 }
11313 }
11314
11315 matching_parens parens;
11316 parens.consume_open (parser);
11317
11318 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
11319
11320 if (is_consteval)
11321 current_binding_level->immediate_fn_ctx_p = true;
11322
11323 /* Parse parameters. */
11324 param_list
11325 = cp_parser_parameter_declaration_clause
11326 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
11327
11328 /* Default arguments shall not be specified in the
11329 parameter-declaration-clause of a lambda-declarator. */
11330 if (cxx_dialect < cxx14)
11331 for (tree t = param_list; t; t = TREE_CHAIN (t))
11332 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
11333 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
11334 "default argument specified for lambda parameter");
11335
11336 parens.require_close (parser);
11337
11338 /* In the decl-specifier-seq of the lambda-declarator, each
11339 decl-specifier shall either be mutable or constexpr. */
11340 int declares_class_or_enum;
11341 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
11342 && !cp_next_tokens_can_be_gnu_attribute_p (parser))
11343 cp_parser_decl_specifier_seq (parser,
11344 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
11345 &lambda_specs, &declares_class_or_enum);
11346 if (lambda_specs.storage_class == sc_mutable)
11347 {
11348 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
11349 quals = TYPE_UNQUALIFIED;
11350 if (lambda_specs.conflicting_specifiers_p)
11351 error_at (lambda_specs.locations[ds_storage_class],
11352 "duplicate %<mutable%>");
11353 }
11354
11355 tx_qual = cp_parser_tx_qualifier_opt (parser);
11356
11357 /* Parse optional exception specification. */
11358 exception_spec
11359 = cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE);
11360
11361 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11362
11363 /* Parse optional trailing return type. */
11364 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
11365 {
11366 cp_lexer_consume_token (parser->lexer);
11367 return_type = cp_parser_trailing_type_id (parser);
11368 }
11369
11370 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11371 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
11372
11373 /* Parse optional trailing requires clause. */
11374 trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
11375
11376 /* The function parameters must be in scope all the way until after the
11377 trailing-return-type in case of decltype. */
11378 pop_bindings_and_leave_scope ();
11379 }
11380
11381 /* Create the function call operator.
11382
11383 Messing with declarators like this is no uglier than building up the
11384 FUNCTION_DECL by hand, and this is less likely to get out of sync with
11385 other code. */
11386 {
11387 cp_decl_specifier_seq return_type_specs;
11388 cp_declarator* declarator;
11389 tree fco;
11390 void *p;
11391
11392 clear_decl_specs (&return_type_specs);
11393 return_type_specs.type = make_auto ();
11394
11395 if (lambda_specs.locations[ds_constexpr])
11396 {
11397 if (cxx_dialect >= cxx17)
11398 return_type_specs.locations[ds_constexpr]
11399 = lambda_specs.locations[ds_constexpr];
11400 else
11401 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
11402 "lambda only available with %<-std=c++17%> or "
11403 "%<-std=gnu++17%>");
11404 }
11405 if (lambda_specs.locations[ds_consteval])
11406 return_type_specs.locations[ds_consteval]
11407 = lambda_specs.locations[ds_consteval];
11408
11409 p = obstack_alloc (&declarator_obstack, 0);
11410
11411 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
11412 LAMBDA_EXPR_LOCATION (lambda_expr));
11413
11414 declarator = make_call_declarator (declarator, param_list, quals,
11415 VIRT_SPEC_UNSPECIFIED,
11416 REF_QUAL_NONE,
11417 tx_qual,
11418 exception_spec,
11419 return_type,
11420 trailing_requires_clause,
11421 UNKNOWN_LOCATION);
11422 declarator->std_attributes = std_attrs;
11423
11424 fco = grokmethod (&return_type_specs,
11425 declarator,
11426 chainon (gnu_attrs, lambda_specs.attributes));
11427 if (fco != error_mark_node)
11428 {
11429 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
11430 DECL_ARTIFICIAL (fco) = 1;
11431 /* Give the object parameter a different name. */
11432 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
11433 DECL_SET_LAMBDA_FUNCTION (fco, true);
11434 }
11435 if (template_param_list)
11436 {
11437 fco = finish_member_template_decl (fco);
11438 finish_template_decl (template_param_list);
11439 --parser->num_template_parameter_lists;
11440 }
11441 else if (parser->fully_implicit_function_template_p)
11442 fco = finish_fully_implicit_template (parser, fco);
11443
11444 finish_member_declaration (fco);
11445
11446 obstack_free (&declarator_obstack, p);
11447
11448 return (fco != error_mark_node);
11449 }
11450 }
11451
11452 /* Parse the body of a lambda expression, which is simply
11453
11454 compound-statement
11455
11456 but which requires special handling.
11457 LAMBDA_EXPR is the current representation of the lambda expression. */
11458
11459 static void
11460 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
11461 {
11462 bool nested = (current_function_decl != NULL_TREE);
11463 unsigned char local_variables_forbidden_p
11464 = parser->local_variables_forbidden_p;
11465 bool in_function_body = parser->in_function_body;
11466
11467 /* The body of a lambda-expression is not a subexpression of the enclosing
11468 expression. */
11469 cp_evaluated ev;
11470
11471 if (nested)
11472 push_function_context ();
11473 else
11474 /* Still increment function_depth so that we don't GC in the
11475 middle of an expression. */
11476 ++function_depth;
11477
11478 vec<tree> omp_privatization_save;
11479 save_omp_privatization_clauses (omp_privatization_save);
11480 /* Clear this in case we're in the middle of a default argument. */
11481 parser->local_variables_forbidden_p = 0;
11482 parser->in_function_body = true;
11483
11484 {
11485 local_specialization_stack s (lss_copy);
11486 tree fco = lambda_function (lambda_expr);
11487 tree body = start_lambda_function (fco, lambda_expr);
11488
11489 /* Originally C++11 required us to peek for 'return expr'; and
11490 process it specially here to deduce the return type. N3638
11491 removed the need for that. */
11492 cp_parser_function_body (parser, false);
11493
11494 finish_lambda_function (body);
11495 }
11496
11497 restore_omp_privatization_clauses (omp_privatization_save);
11498 parser->local_variables_forbidden_p = local_variables_forbidden_p;
11499 parser->in_function_body = in_function_body;
11500 if (nested)
11501 pop_function_context();
11502 else
11503 --function_depth;
11504 }
11505
11506 /* Statements [gram.stmt.stmt] */
11507
11508 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11509
11510 static void
11511 add_debug_begin_stmt (location_t loc)
11512 {
11513 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11514 return;
11515 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11516 /* A concept is never expanded normally. */
11517 return;
11518
11519 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11520 SET_EXPR_LOCATION (stmt, loc);
11521 add_stmt (stmt);
11522 }
11523
11524 /* Parse a statement.
11525
11526 statement:
11527 labeled-statement
11528 expression-statement
11529 compound-statement
11530 selection-statement
11531 iteration-statement
11532 jump-statement
11533 declaration-statement
11534 try-block
11535
11536 C++11:
11537
11538 statement:
11539 labeled-statement
11540 attribute-specifier-seq (opt) expression-statement
11541 attribute-specifier-seq (opt) compound-statement
11542 attribute-specifier-seq (opt) selection-statement
11543 attribute-specifier-seq (opt) iteration-statement
11544 attribute-specifier-seq (opt) jump-statement
11545 declaration-statement
11546 attribute-specifier-seq (opt) try-block
11547
11548 init-statement:
11549 expression-statement
11550 simple-declaration
11551
11552 TM Extension:
11553
11554 statement:
11555 atomic-statement
11556
11557 IN_COMPOUND is true when the statement is nested inside a
11558 cp_parser_compound_statement; this matters for certain pragmas.
11559
11560 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11561 is a (possibly labeled) if statement which is not enclosed in braces
11562 and has an else clause. This is used to implement -Wparentheses.
11563
11564 CHAIN is a vector of if-else-if conditions. */
11565
11566 static void
11567 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
11568 bool in_compound, bool *if_p, vec<tree> *chain,
11569 location_t *loc_after_labels)
11570 {
11571 tree statement, std_attrs = NULL_TREE;
11572 cp_token *token;
11573 location_t statement_location, attrs_loc;
11574
11575 restart:
11576 if (if_p != NULL)
11577 *if_p = false;
11578 /* There is no statement yet. */
11579 statement = NULL_TREE;
11580
11581 saved_token_sentinel saved_tokens (parser->lexer);
11582 attrs_loc = cp_lexer_peek_token (parser->lexer)->location;
11583 if (c_dialect_objc ())
11584 /* In obj-c++, seeing '[[' might be the either the beginning of
11585 c++11 attributes, or a nested objc-message-expression. So
11586 let's parse the c++11 attributes tentatively. */
11587 cp_parser_parse_tentatively (parser);
11588 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11589 if (std_attrs)
11590 attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
11591 if (c_dialect_objc ())
11592 {
11593 if (!cp_parser_parse_definitely (parser))
11594 std_attrs = NULL_TREE;
11595 }
11596
11597 /* Peek at the next token. */
11598 token = cp_lexer_peek_token (parser->lexer);
11599 /* Remember the location of the first token in the statement. */
11600 cp_token *statement_token = token;
11601 statement_location = token->location;
11602 add_debug_begin_stmt (statement_location);
11603 /* If this is a keyword, then that will often determine what kind of
11604 statement we have. */
11605 if (token->type == CPP_KEYWORD)
11606 {
11607 enum rid keyword = token->keyword;
11608
11609 switch (keyword)
11610 {
11611 case RID_CASE:
11612 case RID_DEFAULT:
11613 /* Looks like a labeled-statement with a case label.
11614 Parse the label, and then use tail recursion to parse
11615 the statement. */
11616 cp_parser_label_for_labeled_statement (parser, std_attrs);
11617 in_compound = false;
11618 goto restart;
11619
11620 case RID_IF:
11621 case RID_SWITCH:
11622 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11623 statement = cp_parser_selection_statement (parser, if_p, chain);
11624 break;
11625
11626 case RID_WHILE:
11627 case RID_DO:
11628 case RID_FOR:
11629 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11630 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
11631 break;
11632
11633 case RID_BREAK:
11634 case RID_CONTINUE:
11635 case RID_RETURN:
11636 case RID_CO_RETURN:
11637 case RID_GOTO:
11638 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11639 statement = cp_parser_jump_statement (parser);
11640 break;
11641
11642 /* Objective-C++ exception-handling constructs. */
11643 case RID_AT_TRY:
11644 case RID_AT_CATCH:
11645 case RID_AT_FINALLY:
11646 case RID_AT_SYNCHRONIZED:
11647 case RID_AT_THROW:
11648 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11649 statement = cp_parser_objc_statement (parser);
11650 break;
11651
11652 case RID_TRY:
11653 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11654 statement = cp_parser_try_block (parser);
11655 break;
11656
11657 case RID_NAMESPACE:
11658 /* This must be a namespace alias definition. */
11659 if (std_attrs != NULL_TREE)
11660 {
11661 /* Attributes should be parsed as part of the
11662 declaration, so let's un-parse them. */
11663 saved_tokens.rollback();
11664 std_attrs = NULL_TREE;
11665 }
11666 cp_parser_declaration_statement (parser);
11667 return;
11668
11669 case RID_TRANSACTION_ATOMIC:
11670 case RID_TRANSACTION_RELAXED:
11671 case RID_SYNCHRONIZED:
11672 case RID_ATOMIC_NOEXCEPT:
11673 case RID_ATOMIC_CANCEL:
11674 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11675 statement = cp_parser_transaction (parser, token);
11676 break;
11677 case RID_TRANSACTION_CANCEL:
11678 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11679 statement = cp_parser_transaction_cancel (parser);
11680 break;
11681
11682 default:
11683 /* It might be a keyword like `int' that can start a
11684 declaration-statement. */
11685 break;
11686 }
11687 }
11688 else if (token->type == CPP_NAME)
11689 {
11690 /* If the next token is a `:', then we are looking at a
11691 labeled-statement. */
11692 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11693 if (token->type == CPP_COLON)
11694 {
11695 /* Looks like a labeled-statement with an ordinary label.
11696 Parse the label, and then use tail recursion to parse
11697 the statement. */
11698
11699 cp_parser_label_for_labeled_statement (parser, std_attrs);
11700 in_compound = false;
11701 goto restart;
11702 }
11703 }
11704 /* Anything that starts with a `{' must be a compound-statement. */
11705 else if (token->type == CPP_OPEN_BRACE)
11706 {
11707 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11708 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11709 }
11710 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11711 a statement all its own. */
11712 else if (token->type == CPP_PRAGMA)
11713 {
11714 /* Only certain OpenMP pragmas are attached to statements, and thus
11715 are considered statements themselves. All others are not. In
11716 the context of a compound, accept the pragma as a "statement" and
11717 return so that we can check for a close brace. Otherwise we
11718 require a real statement and must go back and read one. */
11719 if (in_compound)
11720 cp_parser_pragma (parser, pragma_compound, if_p);
11721 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11722 goto restart;
11723 return;
11724 }
11725 else if (token->type == CPP_EOF)
11726 {
11727 cp_parser_error (parser, "expected statement");
11728 return;
11729 }
11730
11731 /* Everything else must be a declaration-statement or an
11732 expression-statement. Try for the declaration-statement
11733 first, unless we are looking at a `;', in which case we know that
11734 we have an expression-statement. */
11735 if (!statement)
11736 {
11737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11738 {
11739 if (std_attrs != NULL_TREE)
11740 /* Attributes should be parsed as part of the declaration,
11741 so let's un-parse them. */
11742 saved_tokens.rollback();
11743
11744 cp_parser_parse_tentatively (parser);
11745 /* Try to parse the declaration-statement. */
11746 cp_parser_declaration_statement (parser);
11747 /* If that worked, we're done. */
11748 if (cp_parser_parse_definitely (parser))
11749 return;
11750 /* It didn't work, restore the post-attribute position. */
11751 if (std_attrs)
11752 cp_lexer_set_token_position (parser->lexer, statement_token);
11753 }
11754 /* All preceding labels have been parsed at this point. */
11755 if (loc_after_labels != NULL)
11756 *loc_after_labels = statement_location;
11757
11758 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11759
11760 /* Look for an expression-statement instead. */
11761 statement = cp_parser_expression_statement (parser, in_statement_expr);
11762
11763 /* Handle [[fallthrough]];. */
11764 if (attribute_fallthrough_p (std_attrs))
11765 {
11766 /* The next token after the fallthrough attribute is ';'. */
11767 if (statement == NULL_TREE)
11768 {
11769 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11770 statement = build_call_expr_internal_loc (statement_location,
11771 IFN_FALLTHROUGH,
11772 void_type_node, 0);
11773 finish_expr_stmt (statement);
11774 }
11775 else
11776 warning_at (statement_location, OPT_Wattributes,
11777 "%<fallthrough%> attribute not followed by %<;%>");
11778 std_attrs = NULL_TREE;
11779 }
11780 }
11781
11782 /* Set the line number for the statement. */
11783 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11784 SET_EXPR_LOCATION (statement, statement_location);
11785
11786 /* Allow "[[fallthrough]];", but warn otherwise. */
11787 if (std_attrs != NULL_TREE)
11788 warning_at (attrs_loc,
11789 OPT_Wattributes,
11790 "attributes at the beginning of statement are ignored");
11791 }
11792
11793 /* Append ATTR to attribute list ATTRS. */
11794
11795 static tree
11796 attr_chainon (tree attrs, tree attr)
11797 {
11798 if (attrs == error_mark_node)
11799 return error_mark_node;
11800 if (attr == error_mark_node)
11801 return error_mark_node;
11802 return chainon (attrs, attr);
11803 }
11804
11805 /* Parse the label for a labeled-statement, i.e.
11806
11807 identifier :
11808 case constant-expression :
11809 default :
11810
11811 GNU Extension:
11812 case constant-expression ... constant-expression : statement
11813
11814 When a label is parsed without errors, the label is added to the
11815 parse tree by the finish_* functions, so this function doesn't
11816 have to return the label. */
11817
11818 static void
11819 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11820 {
11821 cp_token *token;
11822 tree label = NULL_TREE;
11823 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11824
11825 /* The next token should be an identifier. */
11826 token = cp_lexer_peek_token (parser->lexer);
11827 if (token->type != CPP_NAME
11828 && token->type != CPP_KEYWORD)
11829 {
11830 cp_parser_error (parser, "expected labeled-statement");
11831 return;
11832 }
11833
11834 /* Remember whether this case or a user-defined label is allowed to fall
11835 through to. */
11836 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11837
11838 parser->colon_corrects_to_scope_p = false;
11839 switch (token->keyword)
11840 {
11841 case RID_CASE:
11842 {
11843 tree expr, expr_hi;
11844 cp_token *ellipsis;
11845
11846 /* Consume the `case' token. */
11847 cp_lexer_consume_token (parser->lexer);
11848 /* Parse the constant-expression. */
11849 expr = cp_parser_constant_expression (parser);
11850 if (check_for_bare_parameter_packs (expr))
11851 expr = error_mark_node;
11852
11853 ellipsis = cp_lexer_peek_token (parser->lexer);
11854 if (ellipsis->type == CPP_ELLIPSIS)
11855 {
11856 /* Consume the `...' token. */
11857 cp_lexer_consume_token (parser->lexer);
11858 expr_hi = cp_parser_constant_expression (parser);
11859 if (check_for_bare_parameter_packs (expr_hi))
11860 expr_hi = error_mark_node;
11861
11862 /* We don't need to emit warnings here, as the common code
11863 will do this for us. */
11864 }
11865 else
11866 expr_hi = NULL_TREE;
11867
11868 if (parser->in_switch_statement_p)
11869 {
11870 tree l = finish_case_label (token->location, expr, expr_hi);
11871 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11872 {
11873 label = CASE_LABEL (l);
11874 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11875 }
11876 }
11877 else
11878 error_at (token->location,
11879 "case label %qE not within a switch statement",
11880 expr);
11881 }
11882 break;
11883
11884 case RID_DEFAULT:
11885 /* Consume the `default' token. */
11886 cp_lexer_consume_token (parser->lexer);
11887
11888 if (parser->in_switch_statement_p)
11889 {
11890 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11891 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11892 {
11893 label = CASE_LABEL (l);
11894 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11895 }
11896 }
11897 else
11898 error_at (token->location, "case label not within a switch statement");
11899 break;
11900
11901 default:
11902 /* Anything else must be an ordinary label. */
11903 label = finish_label_stmt (cp_parser_identifier (parser));
11904 if (label && TREE_CODE (label) == LABEL_DECL)
11905 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11906 break;
11907 }
11908
11909 /* Require the `:' token. */
11910 cp_parser_require (parser, CPP_COLON, RT_COLON);
11911
11912 /* An ordinary label may optionally be followed by attributes.
11913 However, this is only permitted if the attributes are then
11914 followed by a semicolon. This is because, for backward
11915 compatibility, when parsing
11916 lab: __attribute__ ((unused)) int i;
11917 we want the attribute to attach to "i", not "lab". */
11918 if (label != NULL_TREE
11919 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11920 {
11921 tree attrs;
11922 cp_parser_parse_tentatively (parser);
11923 attrs = cp_parser_gnu_attributes_opt (parser);
11924 if (attrs == NULL_TREE
11925 /* And fallthrough always binds to the expression-statement. */
11926 || attribute_fallthrough_p (attrs)
11927 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11928 cp_parser_abort_tentative_parse (parser);
11929 else if (!cp_parser_parse_definitely (parser))
11930 ;
11931 else
11932 attributes = attr_chainon (attributes, attrs);
11933 }
11934
11935 if (attributes != NULL_TREE)
11936 cplus_decl_attributes (&label, attributes, 0);
11937
11938 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11939 }
11940
11941 /* Parse an expression-statement.
11942
11943 expression-statement:
11944 expression [opt] ;
11945
11946 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11947 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11948 indicates whether this expression-statement is part of an
11949 expression statement. */
11950
11951 static tree
11952 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11953 {
11954 tree statement = NULL_TREE;
11955 cp_token *token = cp_lexer_peek_token (parser->lexer);
11956 location_t loc = token->location;
11957
11958 /* There might be attribute fallthrough. */
11959 tree attr = cp_parser_gnu_attributes_opt (parser);
11960
11961 /* If the next token is a ';', then there is no expression
11962 statement. */
11963 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11964 {
11965 statement = cp_parser_expression (parser);
11966 if (statement == error_mark_node
11967 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11968 {
11969 cp_parser_skip_to_end_of_block_or_statement (parser);
11970 return error_mark_node;
11971 }
11972 }
11973
11974 /* Handle [[fallthrough]];. */
11975 if (attribute_fallthrough_p (attr))
11976 {
11977 /* The next token after the fallthrough attribute is ';'. */
11978 if (statement == NULL_TREE)
11979 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11980 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11981 void_type_node, 0);
11982 else
11983 warning_at (loc, OPT_Wattributes,
11984 "%<fallthrough%> attribute not followed by %<;%>");
11985 attr = NULL_TREE;
11986 }
11987
11988 /* Allow "[[fallthrough]];", but warn otherwise. */
11989 if (attr != NULL_TREE)
11990 warning_at (loc, OPT_Wattributes,
11991 "attributes at the beginning of statement are ignored");
11992
11993 /* Give a helpful message for "A<T>::type t;" and the like. */
11994 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11995 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11996 {
11997 if (TREE_CODE (statement) == SCOPE_REF)
11998 error_at (token->location, "need %<typename%> before %qE because "
11999 "%qT is a dependent scope",
12000 statement, TREE_OPERAND (statement, 0));
12001 else if (is_overloaded_fn (statement)
12002 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
12003 {
12004 /* A::A a; */
12005 tree fn = get_first_fn (statement);
12006 error_at (token->location,
12007 "%<%T::%D%> names the constructor, not the type",
12008 DECL_CONTEXT (fn), DECL_NAME (fn));
12009 }
12010 }
12011
12012 /* Consume the final `;'. */
12013 cp_parser_consume_semicolon_at_end_of_statement (parser);
12014
12015 if (in_statement_expr
12016 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12017 /* This is the final expression statement of a statement
12018 expression. */
12019 statement = finish_stmt_expr_expr (statement, in_statement_expr);
12020 else if (statement)
12021 statement = finish_expr_stmt (statement);
12022
12023 return statement;
12024 }
12025
12026 /* Parse a compound-statement.
12027
12028 compound-statement:
12029 { statement-seq [opt] }
12030
12031 GNU extension:
12032
12033 compound-statement:
12034 { label-declaration-seq [opt] statement-seq [opt] }
12035
12036 label-declaration-seq:
12037 label-declaration
12038 label-declaration-seq label-declaration
12039
12040 Returns a tree representing the statement. */
12041
12042 static tree
12043 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
12044 int bcs_flags, bool function_body)
12045 {
12046 tree compound_stmt;
12047 matching_braces braces;
12048
12049 /* Consume the `{'. */
12050 if (!braces.require_open (parser))
12051 return error_mark_node;
12052 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
12053 && !function_body && cxx_dialect < cxx14)
12054 pedwarn (input_location, OPT_Wpedantic,
12055 "compound-statement in %<constexpr%> function");
12056 /* Begin the compound-statement. */
12057 compound_stmt = begin_compound_stmt (bcs_flags);
12058 /* If the next keyword is `__label__' we have a label declaration. */
12059 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12060 cp_parser_label_declaration (parser);
12061 /* Parse an (optional) statement-seq. */
12062 cp_parser_statement_seq_opt (parser, in_statement_expr);
12063
12064 if (function_body)
12065 maybe_splice_retval_cleanup (compound_stmt);
12066
12067 /* Finish the compound-statement. */
12068 finish_compound_stmt (compound_stmt);
12069 /* Consume the `}'. */
12070 braces.require_close (parser);
12071
12072 return compound_stmt;
12073 }
12074
12075 /* Parse an (optional) statement-seq.
12076
12077 statement-seq:
12078 statement
12079 statement-seq [opt] statement */
12080
12081 static void
12082 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
12083 {
12084 /* Scan statements until there aren't any more. */
12085 while (true)
12086 {
12087 cp_token *token = cp_lexer_peek_token (parser->lexer);
12088
12089 /* If we are looking at a `}', then we have run out of
12090 statements; the same is true if we have reached the end
12091 of file, or have stumbled upon a stray '@end'. */
12092 if (token->type == CPP_CLOSE_BRACE
12093 || token->type == CPP_EOF
12094 || token->type == CPP_PRAGMA_EOL
12095 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
12096 break;
12097
12098 /* If we are in a compound statement and find 'else' then
12099 something went wrong. */
12100 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
12101 {
12102 if (parser->in_statement & IN_IF_STMT)
12103 break;
12104 else
12105 {
12106 token = cp_lexer_consume_token (parser->lexer);
12107 error_at (token->location, "%<else%> without a previous %<if%>");
12108 }
12109 }
12110
12111 /* Parse the statement. */
12112 cp_parser_statement (parser, in_statement_expr, true, NULL);
12113 }
12114 }
12115
12116 /* Return true if this is the C++20 version of range-based-for with
12117 init-statement. */
12118
12119 static bool
12120 cp_parser_range_based_for_with_init_p (cp_parser *parser)
12121 {
12122 bool r = false;
12123
12124 /* Save tokens so that we can put them back. */
12125 cp_lexer_save_tokens (parser->lexer);
12126
12127 /* There has to be an unnested ; followed by an unnested :. */
12128 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
12129 /*recovering=*/false,
12130 CPP_SEMICOLON,
12131 /*consume_paren=*/false) != -1)
12132 goto out;
12133
12134 /* We found the semicolon, eat it now. */
12135 cp_lexer_consume_token (parser->lexer);
12136
12137 /* Now look for ':' that is not nested in () or {}. */
12138 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
12139 /*recovering=*/false,
12140 CPP_COLON,
12141 /*consume_paren=*/false) == -1);
12142
12143 out:
12144 /* Roll back the tokens we skipped. */
12145 cp_lexer_rollback_tokens (parser->lexer);
12146
12147 return r;
12148 }
12149
12150 /* Return true if we're looking at (init; cond), false otherwise. */
12151
12152 static bool
12153 cp_parser_init_statement_p (cp_parser *parser)
12154 {
12155 /* Save tokens so that we can put them back. */
12156 cp_lexer_save_tokens (parser->lexer);
12157
12158 /* Look for ';' that is not nested in () or {}. */
12159 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
12160 /*recovering=*/false,
12161 CPP_SEMICOLON,
12162 /*consume_paren=*/false);
12163
12164 /* Roll back the tokens we skipped. */
12165 cp_lexer_rollback_tokens (parser->lexer);
12166
12167 return ret == -1;
12168 }
12169
12170 /* Parse a selection-statement.
12171
12172 selection-statement:
12173 if ( init-statement [opt] condition ) statement
12174 if ( init-statement [opt] condition ) statement else statement
12175 switch ( init-statement [opt] condition ) statement
12176
12177 Returns the new IF_STMT or SWITCH_STMT.
12178
12179 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12180 is a (possibly labeled) if statement which is not enclosed in
12181 braces and has an else clause. This is used to implement
12182 -Wparentheses.
12183
12184 CHAIN is a vector of if-else-if conditions. This is used to implement
12185 -Wduplicated-cond. */
12186
12187 static tree
12188 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
12189 vec<tree> *chain)
12190 {
12191 cp_token *token;
12192 enum rid keyword;
12193 token_indent_info guard_tinfo;
12194
12195 if (if_p != NULL)
12196 *if_p = false;
12197
12198 /* Peek at the next token. */
12199 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
12200 guard_tinfo = get_token_indent_info (token);
12201
12202 /* See what kind of keyword it is. */
12203 keyword = token->keyword;
12204 switch (keyword)
12205 {
12206 case RID_IF:
12207 case RID_SWITCH:
12208 {
12209 tree statement;
12210 tree condition;
12211
12212 bool cx = false;
12213 if (keyword == RID_IF
12214 && cp_lexer_next_token_is_keyword (parser->lexer,
12215 RID_CONSTEXPR))
12216 {
12217 cx = true;
12218 cp_token *tok = cp_lexer_consume_token (parser->lexer);
12219 if (cxx_dialect < cxx17)
12220 pedwarn (tok->location, 0, "%<if constexpr%> only available "
12221 "with %<-std=c++17%> or %<-std=gnu++17%>");
12222 }
12223
12224 /* Look for the `('. */
12225 matching_parens parens;
12226 if (!parens.require_open (parser))
12227 {
12228 cp_parser_skip_to_end_of_statement (parser);
12229 return error_mark_node;
12230 }
12231
12232 /* Begin the selection-statement. */
12233 if (keyword == RID_IF)
12234 {
12235 statement = begin_if_stmt ();
12236 IF_STMT_CONSTEXPR_P (statement) = cx;
12237 }
12238 else
12239 statement = begin_switch_stmt ();
12240
12241 /* Parse the optional init-statement. */
12242 if (cp_parser_init_statement_p (parser))
12243 {
12244 tree decl;
12245 if (cxx_dialect < cxx17)
12246 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12247 "init-statement in selection statements only available "
12248 "with %<-std=c++17%> or %<-std=gnu++17%>");
12249 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12250 {
12251 /* A non-empty init-statement can have arbitrary side
12252 effects. */
12253 delete chain;
12254 chain = NULL;
12255 }
12256 cp_parser_init_statement (parser, &decl);
12257 }
12258
12259 /* Parse the condition. */
12260 condition = cp_parser_condition (parser);
12261 /* Look for the `)'. */
12262 if (!parens.require_close (parser))
12263 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12264 /*consume_paren=*/true);
12265
12266 if (keyword == RID_IF)
12267 {
12268 bool nested_if;
12269 unsigned char in_statement;
12270
12271 /* Add the condition. */
12272 condition = finish_if_stmt_cond (condition, statement);
12273
12274 if (warn_duplicated_cond)
12275 warn_duplicated_cond_add_or_warn (token->location, condition,
12276 &chain);
12277
12278 /* Parse the then-clause. */
12279 in_statement = parser->in_statement;
12280 parser->in_statement |= IN_IF_STMT;
12281
12282 /* Outside a template, the non-selected branch of a constexpr
12283 if is a 'discarded statement', i.e. unevaluated. */
12284 bool was_discarded = in_discarded_stmt;
12285 bool discard_then = (cx && !processing_template_decl
12286 && integer_zerop (condition));
12287 if (discard_then)
12288 {
12289 in_discarded_stmt = true;
12290 ++c_inhibit_evaluation_warnings;
12291 }
12292
12293 cp_parser_implicitly_scoped_statement (parser, &nested_if,
12294 guard_tinfo);
12295
12296 parser->in_statement = in_statement;
12297
12298 finish_then_clause (statement);
12299
12300 if (discard_then)
12301 {
12302 THEN_CLAUSE (statement) = NULL_TREE;
12303 in_discarded_stmt = was_discarded;
12304 --c_inhibit_evaluation_warnings;
12305 }
12306
12307 /* If the next token is `else', parse the else-clause. */
12308 if (cp_lexer_next_token_is_keyword (parser->lexer,
12309 RID_ELSE))
12310 {
12311 bool discard_else = (cx && !processing_template_decl
12312 && integer_nonzerop (condition));
12313 if (discard_else)
12314 {
12315 in_discarded_stmt = true;
12316 ++c_inhibit_evaluation_warnings;
12317 }
12318
12319 guard_tinfo
12320 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12321 /* Consume the `else' keyword. */
12322 cp_lexer_consume_token (parser->lexer);
12323 if (warn_duplicated_cond)
12324 {
12325 if (cp_lexer_next_token_is_keyword (parser->lexer,
12326 RID_IF)
12327 && chain == NULL)
12328 {
12329 /* We've got "if (COND) else if (COND2)". Start
12330 the condition chain and add COND as the first
12331 element. */
12332 chain = new vec<tree> ();
12333 if (!CONSTANT_CLASS_P (condition)
12334 && !TREE_SIDE_EFFECTS (condition))
12335 {
12336 /* Wrap it in a NOP_EXPR so that we can set the
12337 location of the condition. */
12338 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
12339 condition);
12340 SET_EXPR_LOCATION (e, token->location);
12341 chain->safe_push (e);
12342 }
12343 }
12344 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
12345 RID_IF))
12346 {
12347 /* This is if-else without subsequent if. Zap the
12348 condition chain; we would have already warned at
12349 this point. */
12350 delete chain;
12351 chain = NULL;
12352 }
12353 }
12354 begin_else_clause (statement);
12355 /* Parse the else-clause. */
12356 cp_parser_implicitly_scoped_statement (parser, NULL,
12357 guard_tinfo, chain);
12358
12359 finish_else_clause (statement);
12360
12361 /* If we are currently parsing a then-clause, then
12362 IF_P will not be NULL. We set it to true to
12363 indicate that this if statement has an else clause.
12364 This may trigger the Wparentheses warning below
12365 when we get back up to the parent if statement. */
12366 if (if_p != NULL)
12367 *if_p = true;
12368
12369 if (discard_else)
12370 {
12371 ELSE_CLAUSE (statement) = NULL_TREE;
12372 in_discarded_stmt = was_discarded;
12373 --c_inhibit_evaluation_warnings;
12374 }
12375 }
12376 else
12377 {
12378 /* This if statement does not have an else clause. If
12379 NESTED_IF is true, then the then-clause has an if
12380 statement which does have an else clause. We warn
12381 about the potential ambiguity. */
12382 if (nested_if)
12383 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
12384 "suggest explicit braces to avoid ambiguous"
12385 " %<else%>");
12386 if (warn_duplicated_cond)
12387 {
12388 /* We don't need the condition chain anymore. */
12389 delete chain;
12390 chain = NULL;
12391 }
12392 }
12393
12394 /* Now we're all done with the if-statement. */
12395 finish_if_stmt (statement);
12396 }
12397 else
12398 {
12399 bool in_switch_statement_p;
12400 unsigned char in_statement;
12401
12402 /* Add the condition. */
12403 finish_switch_cond (condition, statement);
12404
12405 /* Parse the body of the switch-statement. */
12406 in_switch_statement_p = parser->in_switch_statement_p;
12407 in_statement = parser->in_statement;
12408 parser->in_switch_statement_p = true;
12409 parser->in_statement |= IN_SWITCH_STMT;
12410 cp_parser_implicitly_scoped_statement (parser, if_p,
12411 guard_tinfo);
12412 parser->in_switch_statement_p = in_switch_statement_p;
12413 parser->in_statement = in_statement;
12414
12415 /* Now we're all done with the switch-statement. */
12416 finish_switch_stmt (statement);
12417 }
12418
12419 return statement;
12420 }
12421 break;
12422
12423 default:
12424 cp_parser_error (parser, "expected selection-statement");
12425 return error_mark_node;
12426 }
12427 }
12428
12429 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
12430 If we have seen at least one decl-specifier, and the next token is not
12431 a parenthesis (after "int (" we might be looking at a functional cast)
12432 neither we are dealing with a concept-check expression then we must be
12433 looking at a declaration. */
12434
12435 static void
12436 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
12437 cp_decl_specifier_seq *decl_specs)
12438 {
12439 if (decl_specs->any_specifiers_p
12440 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12441 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12442 && !cp_parser_error_occurred (parser)
12443 && !(decl_specs->type
12444 && TREE_CODE (decl_specs->type) == TYPE_DECL
12445 && is_constrained_auto (TREE_TYPE (decl_specs->type))))
12446 cp_parser_commit_to_tentative_parse (parser);
12447 }
12448
12449 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
12450 The declarator shall not specify a function or an array. Returns
12451 TRUE if the declarator is valid, FALSE otherwise. */
12452
12453 static bool
12454 cp_parser_check_condition_declarator (cp_parser* parser,
12455 cp_declarator *declarator,
12456 location_t loc)
12457 {
12458 if (declarator == cp_error_declarator
12459 || function_declarator_p (declarator)
12460 || declarator->kind == cdk_array)
12461 {
12462 if (declarator == cp_error_declarator)
12463 /* Already complained. */;
12464 else if (declarator->kind == cdk_array)
12465 error_at (loc, "condition declares an array");
12466 else
12467 error_at (loc, "condition declares a function");
12468 if (parser->fully_implicit_function_template_p)
12469 abort_fully_implicit_template (parser);
12470 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
12471 /*or_comma=*/false,
12472 /*consume_paren=*/false);
12473 return false;
12474 }
12475 else
12476 return true;
12477 }
12478
12479 /* Parse a condition.
12480
12481 condition:
12482 expression
12483 type-specifier-seq declarator = initializer-clause
12484 type-specifier-seq declarator braced-init-list
12485
12486 GNU Extension:
12487
12488 condition:
12489 type-specifier-seq declarator asm-specification [opt]
12490 attributes [opt] = assignment-expression
12491
12492 Returns the expression that should be tested. */
12493
12494 static tree
12495 cp_parser_condition (cp_parser* parser)
12496 {
12497 cp_decl_specifier_seq type_specifiers;
12498 const char *saved_message;
12499 int declares_class_or_enum;
12500
12501 /* Try the declaration first. */
12502 cp_parser_parse_tentatively (parser);
12503 /* New types are not allowed in the type-specifier-seq for a
12504 condition. */
12505 saved_message = parser->type_definition_forbidden_message;
12506 parser->type_definition_forbidden_message
12507 = G_("types may not be defined in conditions");
12508 /* Parse the type-specifier-seq. */
12509 cp_parser_decl_specifier_seq (parser,
12510 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
12511 &type_specifiers,
12512 &declares_class_or_enum);
12513 /* Restore the saved message. */
12514 parser->type_definition_forbidden_message = saved_message;
12515
12516 /* Gather the attributes that were provided with the
12517 decl-specifiers. */
12518 tree prefix_attributes = type_specifiers.attributes;
12519
12520 cp_parser_maybe_commit_to_declaration (parser, &type_specifiers);
12521
12522 /* If all is well, we might be looking at a declaration. */
12523 if (!cp_parser_error_occurred (parser))
12524 {
12525 tree decl;
12526 tree asm_specification;
12527 tree attributes;
12528 cp_declarator *declarator;
12529 tree initializer = NULL_TREE;
12530 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12531
12532 /* Parse the declarator. */
12533 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12534 CP_PARSER_FLAGS_NONE,
12535 /*ctor_dtor_or_conv_p=*/NULL,
12536 /*parenthesized_p=*/NULL,
12537 /*member_p=*/false,
12538 /*friend_p=*/false,
12539 /*static_p=*/false);
12540 /* Parse the attributes. */
12541 attributes = cp_parser_attributes_opt (parser);
12542 /* Parse the asm-specification. */
12543 asm_specification = cp_parser_asm_specification_opt (parser);
12544 /* If the next token is not an `=' or '{', then we might still be
12545 looking at an expression. For example:
12546
12547 if (A(a).x)
12548
12549 looks like a decl-specifier-seq and a declarator -- but then
12550 there is no `=', so this is an expression. */
12551 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12552 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12553 cp_parser_simulate_error (parser);
12554
12555 /* If we did see an `=' or '{', then we are looking at a declaration
12556 for sure. */
12557 if (cp_parser_parse_definitely (parser))
12558 {
12559 tree pushed_scope;
12560 bool non_constant_p = false;
12561 int flags = LOOKUP_ONLYCONVERTING;
12562
12563 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
12564 return error_mark_node;
12565
12566 /* Create the declaration. */
12567 decl = start_decl (declarator, &type_specifiers,
12568 /*initialized_p=*/true,
12569 attributes, prefix_attributes,
12570 &pushed_scope);
12571
12572 /* Parse the initializer. */
12573 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12574 {
12575 initializer = cp_parser_braced_list (parser, &non_constant_p);
12576 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
12577 flags = 0;
12578 }
12579 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12580 {
12581 /* Consume the `='. */
12582 cp_lexer_consume_token (parser->lexer);
12583 initializer = cp_parser_initializer_clause (parser,
12584 &non_constant_p);
12585 }
12586 else
12587 {
12588 cp_parser_error (parser, "expected initializer");
12589 initializer = error_mark_node;
12590 }
12591 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
12592 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12593
12594 /* Process the initializer. */
12595 cp_finish_decl (decl,
12596 initializer, !non_constant_p,
12597 asm_specification,
12598 flags);
12599
12600 if (pushed_scope)
12601 pop_scope (pushed_scope);
12602
12603 return convert_from_reference (decl);
12604 }
12605 }
12606 /* If we didn't even get past the declarator successfully, we are
12607 definitely not looking at a declaration. */
12608 else
12609 cp_parser_abort_tentative_parse (parser);
12610
12611 /* Otherwise, we are looking at an expression. */
12612 return cp_parser_expression (parser);
12613 }
12614
12615 /* Parses a for-statement or range-for-statement until the closing ')',
12616 not included. */
12617
12618 static tree
12619 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
12620 {
12621 tree init, scope, decl;
12622 bool is_range_for;
12623
12624 /* Begin the for-statement. */
12625 scope = begin_for_scope (&init);
12626
12627 /* Parse the initialization. */
12628 is_range_for = cp_parser_init_statement (parser, &decl);
12629
12630 if (is_range_for)
12631 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
12632 false);
12633 else
12634 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
12635 }
12636
12637 static tree
12638 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
12639 unsigned short unroll)
12640 {
12641 /* Normal for loop */
12642 tree condition = NULL_TREE;
12643 tree expression = NULL_TREE;
12644 tree stmt;
12645
12646 stmt = begin_for_stmt (scope, init);
12647 /* The init-statement has already been parsed in
12648 cp_parser_init_statement, so no work is needed here. */
12649 finish_init_stmt (stmt);
12650
12651 /* If there's a condition, process it. */
12652 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12653 condition = cp_parser_condition (parser);
12654 else if (ivdep)
12655 {
12656 cp_parser_error (parser, "missing loop condition in loop with "
12657 "%<GCC ivdep%> pragma");
12658 condition = error_mark_node;
12659 }
12660 else if (unroll)
12661 {
12662 cp_parser_error (parser, "missing loop condition in loop with "
12663 "%<GCC unroll%> pragma");
12664 condition = error_mark_node;
12665 }
12666 finish_for_cond (condition, stmt, ivdep, unroll);
12667 /* Look for the `;'. */
12668 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12669
12670 /* If there's an expression, process it. */
12671 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
12672 expression = cp_parser_expression (parser);
12673 finish_for_expr (expression, stmt);
12674
12675 return stmt;
12676 }
12677
12678 /* Tries to parse a range-based for-statement:
12679
12680 range-based-for:
12681 decl-specifier-seq declarator : expression
12682
12683 The decl-specifier-seq declarator and the `:' are already parsed by
12684 cp_parser_init_statement. If processing_template_decl it returns a
12685 newly created RANGE_FOR_STMT; if not, it is converted to a
12686 regular FOR_STMT. */
12687
12688 static tree
12689 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
12690 bool ivdep, unsigned short unroll, bool is_omp)
12691 {
12692 tree stmt, range_expr;
12693 auto_vec <cxx_binding *, 16> bindings;
12694 auto_vec <tree, 16> names;
12695 tree decomp_first_name = NULL_TREE;
12696 unsigned int decomp_cnt = 0;
12697
12698 /* Get the range declaration momentarily out of the way so that
12699 the range expression doesn't clash with it. */
12700 if (range_decl != error_mark_node)
12701 {
12702 if (DECL_HAS_VALUE_EXPR_P (range_decl))
12703 {
12704 tree v = DECL_VALUE_EXPR (range_decl);
12705 /* For decomposition declaration get all of the corresponding
12706 declarations out of the way. */
12707 if (TREE_CODE (v) == ARRAY_REF
12708 && VAR_P (TREE_OPERAND (v, 0))
12709 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
12710 {
12711 tree d = range_decl;
12712 range_decl = TREE_OPERAND (v, 0);
12713 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
12714 decomp_first_name = d;
12715 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
12716 {
12717 tree name = DECL_NAME (d);
12718 names.safe_push (name);
12719 bindings.safe_push (IDENTIFIER_BINDING (name));
12720 IDENTIFIER_BINDING (name)
12721 = IDENTIFIER_BINDING (name)->previous;
12722 }
12723 }
12724 }
12725 if (names.is_empty ())
12726 {
12727 tree name = DECL_NAME (range_decl);
12728 names.safe_push (name);
12729 bindings.safe_push (IDENTIFIER_BINDING (name));
12730 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
12731 }
12732 }
12733
12734 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12735 {
12736 bool expr_non_constant_p;
12737 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12738 }
12739 else
12740 range_expr = cp_parser_expression (parser);
12741
12742 /* Put the range declaration(s) back into scope. */
12743 for (unsigned int i = 0; i < names.length (); i++)
12744 {
12745 cxx_binding *binding = bindings[i];
12746 binding->previous = IDENTIFIER_BINDING (names[i]);
12747 IDENTIFIER_BINDING (names[i]) = binding;
12748 }
12749
12750 /* finish_omp_for has its own code for the following, so just
12751 return the range_expr instead. */
12752 if (is_omp)
12753 return range_expr;
12754
12755 /* If in template, STMT is converted to a normal for-statement
12756 at instantiation. If not, it is done just ahead. */
12757 if (processing_template_decl)
12758 {
12759 if (check_for_bare_parameter_packs (range_expr))
12760 range_expr = error_mark_node;
12761 stmt = begin_range_for_stmt (scope, init);
12762 if (ivdep)
12763 RANGE_FOR_IVDEP (stmt) = 1;
12764 if (unroll)
12765 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12766 finish_range_for_decl (stmt, range_decl, range_expr);
12767 if (!type_dependent_expression_p (range_expr)
12768 /* do_auto_deduction doesn't mess with template init-lists. */
12769 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12770 do_range_for_auto_deduction (range_decl, range_expr);
12771 }
12772 else
12773 {
12774 stmt = begin_for_stmt (scope, init);
12775 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12776 decomp_first_name, decomp_cnt, ivdep,
12777 unroll);
12778 }
12779 return stmt;
12780 }
12781
12782 /* Subroutine of cp_convert_range_for: given the initializer expression,
12783 builds up the range temporary. */
12784
12785 static tree
12786 build_range_temp (tree range_expr)
12787 {
12788 tree range_type, range_temp;
12789
12790 /* Find out the type deduced by the declaration
12791 `auto &&__range = range_expr'. */
12792 range_type = cp_build_reference_type (make_auto (), true);
12793 range_type = do_auto_deduction (range_type, range_expr,
12794 type_uses_auto (range_type));
12795
12796 /* Create the __range variable. */
12797 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12798 range_type);
12799 TREE_USED (range_temp) = 1;
12800 DECL_ARTIFICIAL (range_temp) = 1;
12801
12802 return range_temp;
12803 }
12804
12805 /* Used by cp_parser_range_for in template context: we aren't going to
12806 do a full conversion yet, but we still need to resolve auto in the
12807 type of the for-range-declaration if present. This is basically
12808 a shortcut version of cp_convert_range_for. */
12809
12810 static void
12811 do_range_for_auto_deduction (tree decl, tree range_expr)
12812 {
12813 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12814 if (auto_node)
12815 {
12816 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12817 range_temp = convert_from_reference (build_range_temp (range_expr));
12818 iter_type = (cp_parser_perform_range_for_lookup
12819 (range_temp, &begin_dummy, &end_dummy));
12820 if (iter_type)
12821 {
12822 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12823 iter_type);
12824 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12825 RO_UNARY_STAR,
12826 tf_warning_or_error);
12827 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12828 iter_decl, auto_node);
12829 }
12830 }
12831 }
12832
12833 /* Warns when the loop variable should be changed to a reference type to
12834 avoid unnecessary copying. I.e., from
12835
12836 for (const auto x : range)
12837
12838 where range returns a reference, to
12839
12840 for (const auto &x : range)
12841
12842 if this version doesn't make a copy.
12843
12844 This function also warns when the loop variable is initialized with
12845 a value of a different type resulting in a copy:
12846
12847 int arr[10];
12848 for (const double &x : arr)
12849
12850 DECL is the RANGE_DECL; EXPR is the *__for_begin expression.
12851 This function is never called when processing_template_decl is on. */
12852
12853 static void
12854 warn_for_range_copy (tree decl, tree expr)
12855 {
12856 if (!warn_range_loop_construct
12857 || decl == error_mark_node)
12858 return;
12859
12860 location_t loc = DECL_SOURCE_LOCATION (decl);
12861 tree type = TREE_TYPE (decl);
12862
12863 if (from_macro_expansion_at (loc))
12864 return;
12865
12866 if (TYPE_REF_P (type))
12867 {
12868 if (glvalue_p (expr) && !ref_conv_binds_directly_p (type, expr))
12869 {
12870 auto_diagnostic_group d;
12871 if (warning_at (loc, OPT_Wrange_loop_construct,
12872 "loop variable %qD of type %qT binds to a temporary "
12873 "constructed from type %qT", decl, type,
12874 TREE_TYPE (expr)))
12875 {
12876 tree ref = cp_build_qualified_type (TREE_TYPE (expr),
12877 TYPE_QUAL_CONST);
12878 ref = cp_build_reference_type (ref, /*rval*/false);
12879 inform (loc, "use non-reference type %qT to make the copy "
12880 "explicit or %qT to prevent copying",
12881 non_reference (type), ref);
12882 }
12883 }
12884 return;
12885 }
12886 else if (!CP_TYPE_CONST_P (type))
12887 return;
12888
12889 /* Since small trivially copyable types are cheap to copy, we suppress the
12890 warning for them. 64B is a common size of a cache line. */
12891 if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
12892 || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64
12893 && trivially_copyable_p (type)))
12894 return;
12895
12896 tree rtype = cp_build_reference_type (type, /*rval*/false);
12897 /* If we could initialize the reference directly, it wouldn't involve any
12898 copies. */
12899 if (!ref_conv_binds_directly_p (rtype, expr))
12900 return;
12901
12902 auto_diagnostic_group d;
12903 if (warning_at (loc, OPT_Wrange_loop_construct,
12904 "loop variable %qD creates a copy from type %qT",
12905 decl, type))
12906 {
12907 gcc_rich_location richloc (loc);
12908 richloc.add_fixit_insert_before ("&");
12909 inform (&richloc, "use reference type to prevent copying");
12910 }
12911 }
12912
12913 /* Converts a range-based for-statement into a normal
12914 for-statement, as per the definition.
12915
12916 for (RANGE_DECL : RANGE_EXPR)
12917 BLOCK
12918
12919 should be equivalent to:
12920
12921 {
12922 auto &&__range = RANGE_EXPR;
12923 for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
12924 __begin != __end;
12925 ++__begin)
12926 {
12927 RANGE_DECL = *__begin;
12928 BLOCK
12929 }
12930 }
12931
12932 If RANGE_EXPR is an array:
12933 BEGIN_EXPR = __range
12934 END_EXPR = __range + ARRAY_SIZE(__range)
12935 Else if RANGE_EXPR has a member 'begin' or 'end':
12936 BEGIN_EXPR = __range.begin()
12937 END_EXPR = __range.end()
12938 Else:
12939 BEGIN_EXPR = begin(__range)
12940 END_EXPR = end(__range);
12941
12942 If __range has a member 'begin' but not 'end', or vice versa, we must
12943 still use the second alternative (it will surely fail, however).
12944 When calling begin()/end() in the third alternative we must use
12945 argument dependent lookup, but always considering 'std' as an associated
12946 namespace. */
12947
12948 tree
12949 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12950 tree decomp_first_name, unsigned int decomp_cnt,
12951 bool ivdep, unsigned short unroll)
12952 {
12953 tree begin, end;
12954 tree iter_type, begin_expr, end_expr;
12955 tree condition, expression;
12956
12957 range_expr = mark_lvalue_use (range_expr);
12958
12959 if (range_decl == error_mark_node || range_expr == error_mark_node)
12960 /* If an error happened previously do nothing or else a lot of
12961 unhelpful errors would be issued. */
12962 begin_expr = end_expr = iter_type = error_mark_node;
12963 else
12964 {
12965 tree range_temp;
12966
12967 if (VAR_P (range_expr)
12968 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12969 /* Can't bind a reference to an array of runtime bound. */
12970 range_temp = range_expr;
12971 else
12972 {
12973 range_temp = build_range_temp (range_expr);
12974 pushdecl (range_temp);
12975 cp_finish_decl (range_temp, range_expr,
12976 /*is_constant_init*/false, NULL_TREE,
12977 LOOKUP_ONLYCONVERTING);
12978 range_temp = convert_from_reference (range_temp);
12979 }
12980 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12981 &begin_expr, &end_expr);
12982 }
12983
12984 /* The new for initialization statement. */
12985 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12986 iter_type);
12987 TREE_USED (begin) = 1;
12988 DECL_ARTIFICIAL (begin) = 1;
12989 pushdecl (begin);
12990 cp_finish_decl (begin, begin_expr,
12991 /*is_constant_init*/false, NULL_TREE,
12992 LOOKUP_ONLYCONVERTING);
12993
12994 if (cxx_dialect >= cxx17)
12995 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12996 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12997 TREE_USED (end) = 1;
12998 DECL_ARTIFICIAL (end) = 1;
12999 pushdecl (end);
13000 cp_finish_decl (end, end_expr,
13001 /*is_constant_init*/false, NULL_TREE,
13002 LOOKUP_ONLYCONVERTING);
13003
13004 finish_init_stmt (statement);
13005
13006 /* The new for condition. */
13007 condition = build_x_binary_op (input_location, NE_EXPR,
13008 begin, ERROR_MARK,
13009 end, ERROR_MARK,
13010 NULL, tf_warning_or_error);
13011 finish_for_cond (condition, statement, ivdep, unroll);
13012
13013 /* The new increment expression. */
13014 expression = finish_unary_op_expr (input_location,
13015 PREINCREMENT_EXPR, begin,
13016 tf_warning_or_error);
13017 finish_for_expr (expression, statement);
13018
13019 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
13020 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
13021
13022 /* The declaration is initialized with *__begin inside the loop body. */
13023 tree deref_begin = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
13024 tf_warning_or_error);
13025 cp_finish_decl (range_decl, deref_begin,
13026 /*is_constant_init*/false, NULL_TREE,
13027 LOOKUP_ONLYCONVERTING);
13028 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
13029 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
13030
13031 warn_for_range_copy (range_decl, deref_begin);
13032
13033 return statement;
13034 }
13035
13036 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
13037 We need to solve both at the same time because the method used
13038 depends on the existence of members begin or end.
13039 Returns the type deduced for the iterator expression. */
13040
13041 static tree
13042 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
13043 {
13044 if (error_operand_p (range))
13045 {
13046 *begin = *end = error_mark_node;
13047 return error_mark_node;
13048 }
13049
13050 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
13051 {
13052 error ("range-based %<for%> expression of type %qT "
13053 "has incomplete type", TREE_TYPE (range));
13054 *begin = *end = error_mark_node;
13055 return error_mark_node;
13056 }
13057 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
13058 {
13059 /* If RANGE is an array, we will use pointer arithmetic. */
13060 *begin = decay_conversion (range, tf_warning_or_error);
13061 *end = build_binary_op (input_location, PLUS_EXPR,
13062 range,
13063 array_type_nelts_top (TREE_TYPE (range)),
13064 false);
13065 return TREE_TYPE (*begin);
13066 }
13067 else
13068 {
13069 /* If it is not an array, we must do a bit of magic. */
13070 tree id_begin, id_end;
13071 tree member_begin, member_end;
13072
13073 *begin = *end = error_mark_node;
13074
13075 id_begin = get_identifier ("begin");
13076 id_end = get_identifier ("end");
13077 member_begin = lookup_member (TREE_TYPE (range), id_begin,
13078 /*protect=*/2, /*want_type=*/false,
13079 tf_warning_or_error);
13080 member_end = lookup_member (TREE_TYPE (range), id_end,
13081 /*protect=*/2, /*want_type=*/false,
13082 tf_warning_or_error);
13083
13084 if (member_begin != NULL_TREE && member_end != NULL_TREE)
13085 {
13086 /* Use the member functions. */
13087 *begin = cp_parser_range_for_member_function (range, id_begin);
13088 *end = cp_parser_range_for_member_function (range, id_end);
13089 }
13090 else
13091 {
13092 /* Use global functions with ADL. */
13093 releasing_vec vec;
13094
13095 vec_safe_push (vec, range);
13096
13097 member_begin = perform_koenig_lookup (id_begin, vec,
13098 tf_warning_or_error);
13099 *begin = finish_call_expr (member_begin, &vec, false, true,
13100 tf_warning_or_error);
13101 member_end = perform_koenig_lookup (id_end, vec,
13102 tf_warning_or_error);
13103 *end = finish_call_expr (member_end, &vec, false, true,
13104 tf_warning_or_error);
13105 }
13106
13107 /* Last common checks. */
13108 if (*begin == error_mark_node || *end == error_mark_node)
13109 {
13110 /* If one of the expressions is an error do no more checks. */
13111 *begin = *end = error_mark_node;
13112 return error_mark_node;
13113 }
13114 else if (type_dependent_expression_p (*begin)
13115 || type_dependent_expression_p (*end))
13116 /* Can happen, when, eg, in a template context, Koenig lookup
13117 can't resolve begin/end (c++/58503). */
13118 return NULL_TREE;
13119 else
13120 {
13121 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
13122 /* The unqualified type of the __begin and __end temporaries should
13123 be the same, as required by the multiple auto declaration. */
13124 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
13125 {
13126 if (cxx_dialect >= cxx17
13127 && (build_x_binary_op (input_location, NE_EXPR,
13128 *begin, ERROR_MARK,
13129 *end, ERROR_MARK,
13130 NULL, tf_none)
13131 != error_mark_node))
13132 /* P0184R0 allows __begin and __end to have different types,
13133 but make sure they are comparable so we can give a better
13134 diagnostic. */;
13135 else
13136 error ("inconsistent begin/end types in range-based %<for%> "
13137 "statement: %qT and %qT",
13138 TREE_TYPE (*begin), TREE_TYPE (*end));
13139 }
13140 return iter_type;
13141 }
13142 }
13143 }
13144
13145 /* Helper function for cp_parser_perform_range_for_lookup.
13146 Builds a tree for RANGE.IDENTIFIER(). */
13147
13148 static tree
13149 cp_parser_range_for_member_function (tree range, tree identifier)
13150 {
13151 tree member, res;
13152
13153 member = finish_class_member_access_expr (range, identifier,
13154 false, tf_warning_or_error);
13155 if (member == error_mark_node)
13156 return error_mark_node;
13157
13158 releasing_vec vec;
13159 res = finish_call_expr (member, &vec,
13160 /*disallow_virtual=*/false,
13161 /*koenig_p=*/false,
13162 tf_warning_or_error);
13163 return res;
13164 }
13165
13166 /* Parse an iteration-statement.
13167
13168 iteration-statement:
13169 while ( condition ) statement
13170 do statement while ( expression ) ;
13171 for ( init-statement condition [opt] ; expression [opt] )
13172 statement
13173
13174 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
13175
13176 static tree
13177 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
13178 unsigned short unroll)
13179 {
13180 cp_token *token;
13181 enum rid keyword;
13182 tree statement;
13183 unsigned char in_statement;
13184 token_indent_info guard_tinfo;
13185
13186 /* Peek at the next token. */
13187 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
13188 if (!token)
13189 return error_mark_node;
13190
13191 guard_tinfo = get_token_indent_info (token);
13192
13193 /* Remember whether or not we are already within an iteration
13194 statement. */
13195 in_statement = parser->in_statement;
13196
13197 /* See what kind of keyword it is. */
13198 keyword = token->keyword;
13199 switch (keyword)
13200 {
13201 case RID_WHILE:
13202 {
13203 tree condition;
13204
13205 /* Begin the while-statement. */
13206 statement = begin_while_stmt ();
13207 /* Look for the `('. */
13208 matching_parens parens;
13209 parens.require_open (parser);
13210 /* Parse the condition. */
13211 condition = cp_parser_condition (parser);
13212 finish_while_stmt_cond (condition, statement, ivdep, unroll);
13213 /* Look for the `)'. */
13214 parens.require_close (parser);
13215 /* Parse the dependent statement. */
13216 parser->in_statement = IN_ITERATION_STMT;
13217 bool prev = note_iteration_stmt_body_start ();
13218 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
13219 note_iteration_stmt_body_end (prev);
13220 parser->in_statement = in_statement;
13221 /* We're done with the while-statement. */
13222 finish_while_stmt (statement);
13223 }
13224 break;
13225
13226 case RID_DO:
13227 {
13228 tree expression;
13229
13230 /* Begin the do-statement. */
13231 statement = begin_do_stmt ();
13232 /* Parse the body of the do-statement. */
13233 parser->in_statement = IN_ITERATION_STMT;
13234 bool prev = note_iteration_stmt_body_start ();
13235 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
13236 note_iteration_stmt_body_end (prev);
13237 parser->in_statement = in_statement;
13238 finish_do_body (statement);
13239 /* Look for the `while' keyword. */
13240 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
13241 /* Look for the `('. */
13242 matching_parens parens;
13243 parens.require_open (parser);
13244 /* Parse the expression. */
13245 expression = cp_parser_expression (parser);
13246 /* We're done with the do-statement. */
13247 finish_do_stmt (expression, statement, ivdep, unroll);
13248 /* Look for the `)'. */
13249 parens.require_close (parser);
13250 /* Look for the `;'. */
13251 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13252 }
13253 break;
13254
13255 case RID_FOR:
13256 {
13257 /* Look for the `('. */
13258 matching_parens parens;
13259 parens.require_open (parser);
13260
13261 statement = cp_parser_for (parser, ivdep, unroll);
13262
13263 /* Look for the `)'. */
13264 parens.require_close (parser);
13265
13266 /* Parse the body of the for-statement. */
13267 parser->in_statement = IN_ITERATION_STMT;
13268 bool prev = note_iteration_stmt_body_start ();
13269 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
13270 note_iteration_stmt_body_end (prev);
13271 parser->in_statement = in_statement;
13272
13273 /* We're done with the for-statement. */
13274 finish_for_stmt (statement);
13275 }
13276 break;
13277
13278 default:
13279 cp_parser_error (parser, "expected iteration-statement");
13280 statement = error_mark_node;
13281 break;
13282 }
13283
13284 return statement;
13285 }
13286
13287 /* Parse a init-statement or the declarator of a range-based-for.
13288 Returns true if a range-based-for declaration is seen.
13289
13290 init-statement:
13291 expression-statement
13292 simple-declaration */
13293
13294 static bool
13295 cp_parser_init_statement (cp_parser *parser, tree *decl)
13296 {
13297 /* If the next token is a `;', then we have an empty
13298 expression-statement. Grammatically, this is also a
13299 simple-declaration, but an invalid one, because it does not
13300 declare anything. Therefore, if we did not handle this case
13301 specially, we would issue an error message about an invalid
13302 declaration. */
13303 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13304 {
13305 bool is_range_for = false;
13306 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13307
13308 /* Try to parse the init-statement. */
13309 if (cp_parser_range_based_for_with_init_p (parser))
13310 {
13311 tree dummy;
13312 cp_parser_parse_tentatively (parser);
13313 /* Parse the declaration. */
13314 cp_parser_simple_declaration (parser,
13315 /*function_definition_allowed_p=*/false,
13316 &dummy);
13317 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13318 if (!cp_parser_parse_definitely (parser))
13319 /* That didn't work, try to parse it as an expression-statement. */
13320 cp_parser_expression_statement (parser, NULL_TREE);
13321
13322 if (cxx_dialect < cxx20)
13323 {
13324 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
13325 "range-based %<for%> loops with initializer only "
13326 "available with %<-std=c++20%> or %<-std=gnu++20%>");
13327 *decl = error_mark_node;
13328 }
13329 }
13330
13331 /* A colon is used in range-based for. */
13332 parser->colon_corrects_to_scope_p = false;
13333
13334 /* We're going to speculatively look for a declaration, falling back
13335 to an expression, if necessary. */
13336 cp_parser_parse_tentatively (parser);
13337 /* Parse the declaration. */
13338 cp_parser_simple_declaration (parser,
13339 /*function_definition_allowed_p=*/false,
13340 decl);
13341 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13342 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13343 {
13344 /* It is a range-for, consume the ':'. */
13345 cp_lexer_consume_token (parser->lexer);
13346 is_range_for = true;
13347 if (cxx_dialect < cxx11)
13348 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
13349 "range-based %<for%> loops only available with "
13350 "%<-std=c++11%> or %<-std=gnu++11%>");
13351 }
13352 else
13353 /* The ';' is not consumed yet because we told
13354 cp_parser_simple_declaration not to. */
13355 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13356
13357 if (cp_parser_parse_definitely (parser))
13358 return is_range_for;
13359 /* If the tentative parse failed, then we shall need to look for an
13360 expression-statement. */
13361 }
13362 /* If we are here, it is an expression-statement. */
13363 cp_parser_expression_statement (parser, NULL_TREE);
13364 return false;
13365 }
13366
13367 /* Parse a jump-statement.
13368
13369 jump-statement:
13370 break ;
13371 continue ;
13372 return expression [opt] ;
13373 return braced-init-list ;
13374 coroutine-return-statement;
13375 goto identifier ;
13376
13377 GNU extension:
13378
13379 jump-statement:
13380 goto * expression ;
13381
13382 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
13383
13384 static tree
13385 cp_parser_jump_statement (cp_parser* parser)
13386 {
13387 tree statement = error_mark_node;
13388 cp_token *token;
13389 enum rid keyword;
13390 unsigned char in_statement;
13391
13392 /* Peek at the next token. */
13393 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
13394 if (!token)
13395 return error_mark_node;
13396
13397 /* See what kind of keyword it is. */
13398 keyword = token->keyword;
13399 switch (keyword)
13400 {
13401 case RID_BREAK:
13402 in_statement = parser->in_statement & ~IN_IF_STMT;
13403 switch (in_statement)
13404 {
13405 case 0:
13406 error_at (token->location, "break statement not within loop or switch");
13407 break;
13408 default:
13409 gcc_assert ((in_statement & IN_SWITCH_STMT)
13410 || in_statement == IN_ITERATION_STMT);
13411 statement = finish_break_stmt ();
13412 if (in_statement == IN_ITERATION_STMT)
13413 break_maybe_infinite_loop ();
13414 break;
13415 case IN_OMP_BLOCK:
13416 error_at (token->location, "invalid exit from OpenMP structured block");
13417 break;
13418 case IN_OMP_FOR:
13419 error_at (token->location, "break statement used with OpenMP for loop");
13420 break;
13421 }
13422 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13423 break;
13424
13425 case RID_CONTINUE:
13426 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
13427 {
13428 case 0:
13429 error_at (token->location, "continue statement not within a loop");
13430 break;
13431 /* Fall through. */
13432 case IN_ITERATION_STMT:
13433 case IN_OMP_FOR:
13434 statement = finish_continue_stmt ();
13435 break;
13436 case IN_OMP_BLOCK:
13437 error_at (token->location, "invalid exit from OpenMP structured block");
13438 break;
13439 default:
13440 gcc_unreachable ();
13441 }
13442 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13443 break;
13444
13445 case RID_CO_RETURN:
13446 case RID_RETURN:
13447 {
13448 tree expr;
13449 bool expr_non_constant_p;
13450
13451 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13452 {
13453 cp_lexer_set_source_position (parser->lexer);
13454 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13455 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
13456 }
13457 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13458 expr = cp_parser_expression (parser);
13459 else
13460 /* If the next token is a `;', then there is no
13461 expression. */
13462 expr = NULL_TREE;
13463 /* Build the return-statement, check co-return first, since type
13464 deduction is not valid there. */
13465 if (keyword == RID_CO_RETURN)
13466 statement = finish_co_return_stmt (token->location, expr);
13467 else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
13468 /* Don't deduce from a discarded return statement. */;
13469 else
13470 statement = finish_return_stmt (expr);
13471 /* Look for the final `;'. */
13472 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13473 }
13474 break;
13475
13476 case RID_GOTO:
13477 if (parser->in_function_body
13478 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
13479 {
13480 error ("%<goto%> in %<constexpr%> function");
13481 cp_function_chain->invalid_constexpr = true;
13482 }
13483
13484 /* Create the goto-statement. */
13485 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
13486 {
13487 /* Issue a warning about this use of a GNU extension. */
13488 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
13489 /* Consume the '*' token. */
13490 cp_lexer_consume_token (parser->lexer);
13491 /* Parse the dependent expression. */
13492 finish_goto_stmt (cp_parser_expression (parser));
13493 }
13494 else
13495 finish_goto_stmt (cp_parser_identifier (parser));
13496 /* Look for the final `;'. */
13497 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13498 break;
13499
13500 default:
13501 cp_parser_error (parser, "expected jump-statement");
13502 break;
13503 }
13504
13505 return statement;
13506 }
13507
13508 /* Parse a declaration-statement.
13509
13510 declaration-statement:
13511 block-declaration */
13512
13513 static void
13514 cp_parser_declaration_statement (cp_parser* parser)
13515 {
13516 void *p;
13517
13518 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13519 p = obstack_alloc (&declarator_obstack, 0);
13520
13521 /* Parse the block-declaration. */
13522 cp_parser_block_declaration (parser, /*statement_p=*/true);
13523
13524 /* Free any declarators allocated. */
13525 obstack_free (&declarator_obstack, p);
13526 }
13527
13528 /* Some dependent statements (like `if (cond) statement'), are
13529 implicitly in their own scope. In other words, if the statement is
13530 a single statement (as opposed to a compound-statement), it is
13531 none-the-less treated as if it were enclosed in braces. Any
13532 declarations appearing in the dependent statement are out of scope
13533 after control passes that point. This function parses a statement,
13534 but ensures that is in its own scope, even if it is not a
13535 compound-statement.
13536
13537 If IF_P is not NULL, *IF_P is set to indicate whether the statement
13538 is a (possibly labeled) if statement which is not enclosed in
13539 braces and has an else clause. This is used to implement
13540 -Wparentheses.
13541
13542 CHAIN is a vector of if-else-if conditions. This is used to implement
13543 -Wduplicated-cond.
13544
13545 Returns the new statement. */
13546
13547 static tree
13548 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
13549 const token_indent_info &guard_tinfo,
13550 vec<tree> *chain)
13551 {
13552 tree statement;
13553 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
13554 location_t body_loc_after_labels = UNKNOWN_LOCATION;
13555 token_indent_info body_tinfo
13556 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13557
13558 if (if_p != NULL)
13559 *if_p = false;
13560
13561 /* Mark if () ; with a special NOP_EXPR. */
13562 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13563 {
13564 cp_lexer_consume_token (parser->lexer);
13565 statement = add_stmt (build_empty_stmt (body_loc));
13566
13567 if (guard_tinfo.keyword == RID_IF
13568 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
13569 warning_at (body_loc, OPT_Wempty_body,
13570 "suggest braces around empty body in an %<if%> statement");
13571 else if (guard_tinfo.keyword == RID_ELSE)
13572 warning_at (body_loc, OPT_Wempty_body,
13573 "suggest braces around empty body in an %<else%> statement");
13574 }
13575 /* if a compound is opened, we simply parse the statement directly. */
13576 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13577 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
13578 /* If the token is not a `{', then we must take special action. */
13579 else
13580 {
13581 /* Create a compound-statement. */
13582 statement = begin_compound_stmt (0);
13583 /* Parse the dependent-statement. */
13584 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
13585 &body_loc_after_labels);
13586 /* Finish the dummy compound-statement. */
13587 finish_compound_stmt (statement);
13588 }
13589
13590 token_indent_info next_tinfo
13591 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13592 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13593
13594 if (body_loc_after_labels != UNKNOWN_LOCATION
13595 && next_tinfo.type != CPP_SEMICOLON)
13596 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
13597 guard_tinfo.location, guard_tinfo.keyword);
13598
13599 /* Return the statement. */
13600 return statement;
13601 }
13602
13603 /* For some dependent statements (like `while (cond) statement'), we
13604 have already created a scope. Therefore, even if the dependent
13605 statement is a compound-statement, we do not want to create another
13606 scope. */
13607
13608 static void
13609 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
13610 const token_indent_info &guard_tinfo)
13611 {
13612 /* If the token is a `{', then we must take special action. */
13613 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13614 {
13615 token_indent_info body_tinfo
13616 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13617 location_t loc_after_labels = UNKNOWN_LOCATION;
13618
13619 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
13620 &loc_after_labels);
13621 token_indent_info next_tinfo
13622 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13623 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13624
13625 if (loc_after_labels != UNKNOWN_LOCATION
13626 && next_tinfo.type != CPP_SEMICOLON)
13627 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
13628 guard_tinfo.location,
13629 guard_tinfo.keyword);
13630 }
13631 else
13632 {
13633 /* Avoid calling cp_parser_compound_statement, so that we
13634 don't create a new scope. Do everything else by hand. */
13635 matching_braces braces;
13636 braces.require_open (parser);
13637 /* If the next keyword is `__label__' we have a label declaration. */
13638 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13639 cp_parser_label_declaration (parser);
13640 /* Parse an (optional) statement-seq. */
13641 cp_parser_statement_seq_opt (parser, NULL_TREE);
13642 braces.require_close (parser);
13643 }
13644 }
13645
13646 /* Modules */
13647
13648 /* Parse a module-name,
13649 identifier
13650 module-name . identifier
13651 header-name
13652
13653 Returns a pointer to module object, NULL. */
13654
13655 static module_state *
13656 cp_parser_module_name (cp_parser *parser)
13657 {
13658 cp_token *token = cp_lexer_peek_token (parser->lexer);
13659 if (token->type == CPP_HEADER_NAME)
13660 {
13661 cp_lexer_consume_token (parser->lexer);
13662
13663 return get_module (token->u.value);
13664 }
13665
13666 module_state *parent = NULL;
13667 bool partitioned = false;
13668 if (token->type == CPP_COLON && named_module_p ())
13669 {
13670 partitioned = true;
13671 cp_lexer_consume_token (parser->lexer);
13672 }
13673
13674 for (;;)
13675 {
13676 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME)
13677 {
13678 cp_parser_error (parser, "expected module-name");
13679 break;
13680 }
13681
13682 tree name = cp_lexer_consume_token (parser->lexer)->u.value;
13683 parent = get_module (name, parent, partitioned);
13684 token = cp_lexer_peek_token (parser->lexer);
13685 if (!partitioned && token->type == CPP_COLON)
13686 partitioned = true;
13687 else if (token->type != CPP_DOT)
13688 break;
13689
13690 cp_lexer_consume_token (parser->lexer);
13691 }
13692
13693 return parent;
13694 }
13695
13696 /* Named module-declaration
13697 __module ; PRAGMA_EOL
13698 __module private ; PRAGMA_EOL (unimplemented)
13699 [__export] __module module-name attr-spec-seq-opt ; PRAGMA_EOL
13700 */
13701
13702 static module_parse
13703 cp_parser_module_declaration (cp_parser *parser, module_parse mp_state,
13704 bool exporting)
13705 {
13706 /* We're a pseudo pragma. */
13707 parser->lexer->in_pragma = true;
13708 cp_token *token = cp_lexer_consume_token (parser->lexer);
13709
13710 if (mp_state == MP_FIRST && !exporting
13711 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13712 {
13713 /* Start global module fragment. */
13714 cp_lexer_consume_token (parser->lexer);
13715 module_kind |= MK_GLOBAL;
13716 mp_state = MP_GLOBAL;
13717 cp_parser_require_pragma_eol (parser, token);
13718 }
13719 else if (!exporting
13720 && cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13721 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_PRIVATE)
13722 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_SEMICOLON))
13723 {
13724 cp_lexer_consume_token (parser->lexer);
13725 cp_lexer_consume_token (parser->lexer);
13726 cp_lexer_consume_token (parser->lexer);
13727 cp_parser_require_pragma_eol (parser, token);
13728
13729 if (!(mp_state == MP_PURVIEW || mp_state == MP_PURVIEW_IMPORTS)
13730 || !module_interface_p () || module_partition_p ())
13731 error_at (token->location,
13732 "private module fragment only permitted in purview"
13733 " of module interface or partition");
13734 else
13735 {
13736 mp_state = MP_PRIVATE_IMPORTS;
13737 sorry_at (token->location, "private module fragment");
13738 }
13739 }
13740 else if (!(mp_state == MP_FIRST || mp_state == MP_GLOBAL))
13741 {
13742 /* Neither the first declaration, nor in a GMF. */
13743 error_at (token->location, "module-declaration only permitted as first"
13744 " declaration, or ending a global module fragment");
13745 skip_eol:
13746 cp_parser_skip_to_pragma_eol (parser, token);
13747 }
13748 else
13749 {
13750 module_state *mod = cp_parser_module_name (parser);
13751 tree attrs = cp_parser_attributes_opt (parser);
13752
13753 mp_state = MP_PURVIEW_IMPORTS;
13754 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
13755 goto skip_eol;
13756
13757 declare_module (mod, token->location, exporting, attrs, parse_in);
13758 cp_parser_require_pragma_eol (parser, token);
13759 }
13760
13761 return mp_state;
13762 }
13763
13764 /* Import-declaration
13765 [__export] __import module-name attr-spec-seq-opt ; PRAGMA_EOL */
13766
13767 static void
13768 cp_parser_import_declaration (cp_parser *parser, module_parse mp_state,
13769 bool exporting)
13770 {
13771 /* We're a pseudo pragma. */
13772 parser->lexer->in_pragma = true;
13773 cp_token *token = cp_lexer_consume_token (parser->lexer);
13774
13775 if (mp_state != MP_PURVIEW_IMPORTS
13776 && mp_state != MP_PRIVATE_IMPORTS
13777 && module_purview_p ()
13778 && !global_purview_p ())
13779 {
13780 error_at (token->location, "post-module-declaration"
13781 " imports must be contiguous");
13782 note_lexer:
13783 inform (token->location, "perhaps insert a line break, or other"
13784 " disambiguation, to prevent this being considered a"
13785 " module control-line");
13786 skip_eol:
13787 cp_parser_skip_to_pragma_eol (parser, token);
13788 }
13789 else if (current_scope () != global_namespace)
13790 {
13791 error_at (token->location, "import-declaration must be at global scope");
13792 goto note_lexer;
13793 }
13794 else
13795 {
13796 module_state *mod = cp_parser_module_name (parser);
13797 tree attrs = cp_parser_attributes_opt (parser);
13798
13799 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
13800 goto skip_eol;
13801 cp_parser_require_pragma_eol (parser, token);
13802
13803 if (parser->in_unbraced_linkage_specification_p)
13804 error_at (token->location, "import cannot appear directly in"
13805 " a linkage-specification");
13806
13807 /* Module-purview imports must not be from source inclusion
13808 [cpp.import]/7 */
13809 if (attrs && module_purview_p () && !global_purview_p ()
13810 && private_lookup_attribute ("__translated",
13811 strlen ("__translated"), attrs))
13812 error_at (token->location, "post-module-declaration imports"
13813 " must not be include-translated");
13814 else if ((mp_state == MP_PURVIEW_IMPORTS
13815 || mp_state == MP_PRIVATE_IMPORTS)
13816 && !token->main_source_p)
13817 error_at (token->location, "post-module-declaration imports"
13818 " must not be from header inclusion");
13819
13820 import_module (mod, token->location, exporting, attrs, parse_in);
13821 }
13822 }
13823
13824 /* export-declaration.
13825
13826 export declaration
13827 export { declaration-seq-opt } */
13828
13829 static void
13830 cp_parser_module_export (cp_parser *parser)
13831 {
13832 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT));
13833 cp_token *token = cp_lexer_consume_token (parser->lexer);
13834
13835 if (!module_interface_p ())
13836 error_at (token->location,
13837 "%qE may only occur after a module interface declaration",
13838 token->u.value);
13839
13840 bool braced = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE);
13841
13842 unsigned mk = module_kind;
13843 if (module_exporting_p ())
13844 error_at (token->location,
13845 "%qE may only occur once in an export declaration",
13846 token->u.value);
13847 module_kind |= MK_EXPORTING;
13848
13849 if (braced)
13850 {
13851 cp_ensure_no_omp_declare_simd (parser);
13852 cp_ensure_no_oacc_routine (parser);
13853
13854 cp_lexer_consume_token (parser->lexer);
13855 cp_parser_declaration_seq_opt (parser);
13856 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13857 }
13858 else
13859 {
13860 /* Explicitly check if the next tokens might be a
13861 module-directive line, so we can give a clearer error message
13862 about why the directive will be rejected. */
13863 if (cp_lexer_next_token_is_keyword (parser->lexer, RID__MODULE)
13864 || cp_lexer_next_token_is_keyword (parser->lexer, RID__IMPORT)
13865 || cp_lexer_next_token_is_keyword (parser->lexer, RID__EXPORT))
13866 error_at (token->location, "%<export%> not part of following"
13867 " module-directive");
13868 cp_parser_declaration (parser, NULL_TREE);
13869 }
13870
13871 module_kind = mk;
13872 }
13873
13874 /* Declarations [gram.dcl.dcl] */
13875
13876 /* Parse an optional declaration-sequence. TOP_LEVEL is true, if this
13877 is the top-level declaration sequence. That affects whether we
13878 deal with module-preamble.
13879
13880 declaration-seq:
13881 declaration
13882 declaration-seq declaration */
13883
13884 static void
13885 cp_parser_declaration_seq_opt (cp_parser* parser)
13886 {
13887 while (true)
13888 {
13889 cp_token *token = cp_lexer_peek_token (parser->lexer);
13890
13891 if (token->type == CPP_CLOSE_BRACE
13892 || token->type == CPP_EOF)
13893 break;
13894 else
13895 cp_parser_toplevel_declaration (parser);
13896 }
13897 }
13898
13899 /* Parse a declaration.
13900
13901 declaration:
13902 block-declaration
13903 function-definition
13904 template-declaration
13905 explicit-instantiation
13906 explicit-specialization
13907 linkage-specification
13908 namespace-definition
13909
13910 C++17:
13911 deduction-guide
13912
13913 modules:
13914 (all these are only allowed at the outermost level, check
13915 that semantically, for better diagnostics)
13916 module-declaration
13917 module-export-declaration
13918 module-import-declaration
13919 export-declaration
13920
13921 GNU extension:
13922
13923 declaration:
13924 __extension__ declaration */
13925
13926 static void
13927 cp_parser_declaration (cp_parser* parser, tree prefix_attrs)
13928 {
13929 int saved_pedantic;
13930
13931 /* Check for the `__extension__' keyword. */
13932 if (cp_parser_extension_opt (parser, &saved_pedantic))
13933 {
13934 /* Parse the qualified declaration. */
13935 cp_parser_declaration (parser, prefix_attrs);
13936 /* Restore the PEDANTIC flag. */
13937 pedantic = saved_pedantic;
13938
13939 return;
13940 }
13941
13942 /* Try to figure out what kind of declaration is present. */
13943 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
13944 cp_token *token2 = (token1->type == CPP_EOF
13945 ? token1 : cp_lexer_peek_nth_token (parser->lexer, 2));
13946
13947 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13948 void *p = obstack_alloc (&declarator_obstack, 0);
13949
13950 tree attributes = NULL_TREE;
13951
13952 /* Conditionally, allow attributes to precede a linkage specification. */
13953 if (token1->keyword == RID_ATTRIBUTE)
13954 {
13955 cp_lexer_save_tokens (parser->lexer);
13956 attributes = cp_parser_attributes_opt (parser);
13957 cp_token *t1 = cp_lexer_peek_token (parser->lexer);
13958 cp_token *t2 = (t1->type == CPP_EOF
13959 ? t1 : cp_lexer_peek_nth_token (parser->lexer, 2));
13960 if (t1->keyword == RID_EXTERN
13961 && cp_parser_is_pure_string_literal (t2))
13962 {
13963 cp_lexer_commit_tokens (parser->lexer);
13964 /* We might have already been here. */
13965 if (!c_dialect_objc ())
13966 {
13967 location_t where = get_finish (t2->location);
13968 warning_at (token1->location, OPT_Wattributes, "attributes are"
13969 " not permitted in this position");
13970 where = linemap_position_for_loc_and_offset (line_table,
13971 where, 1);
13972 inform (where, "attributes may be inserted here");
13973 attributes = NULL_TREE;
13974 }
13975 token1 = t1;
13976 token2 = t2;
13977 }
13978 else
13979 {
13980 cp_lexer_rollback_tokens (parser->lexer);
13981 attributes = NULL_TREE;
13982 }
13983 }
13984 /* If we already had some attributes, and we've added more, then prepend.
13985 Otherwise attributes just contains any that we just read. */
13986 if (prefix_attrs)
13987 {
13988 if (attributes)
13989 TREE_CHAIN (prefix_attrs) = attributes;
13990 attributes = prefix_attrs;
13991 }
13992
13993 /* If the next token is `extern' and the following token is a string
13994 literal, then we have a linkage specification. */
13995 if (token1->keyword == RID_EXTERN
13996 && cp_parser_is_pure_string_literal (token2))
13997 cp_parser_linkage_specification (parser, attributes);
13998 /* If the next token is `template', then we have either a template
13999 declaration, an explicit instantiation, or an explicit
14000 specialization. */
14001 else if (token1->keyword == RID_TEMPLATE)
14002 {
14003 /* `template <>' indicates a template specialization. */
14004 if (token2->type == CPP_LESS
14005 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14006 cp_parser_explicit_specialization (parser);
14007 /* `template <' indicates a template declaration. */
14008 else if (token2->type == CPP_LESS)
14009 cp_parser_template_declaration (parser, /*member_p=*/false);
14010 /* Anything else must be an explicit instantiation. */
14011 else
14012 cp_parser_explicit_instantiation (parser);
14013 }
14014 /* If the next token is `export', it's new-style modules or
14015 old-style template. */
14016 else if (token1->keyword == RID_EXPORT)
14017 {
14018 if (!modules_p ())
14019 cp_parser_template_declaration (parser, /*member_p=*/false);
14020 else
14021 cp_parser_module_export (parser);
14022 }
14023 else if (token1->keyword == RID__EXPORT
14024 || token1->keyword == RID__IMPORT
14025 || token1->keyword == RID__MODULE)
14026 {
14027 bool exporting = token1->keyword == RID__EXPORT;
14028 cp_token *next = exporting ? token2 : token1;
14029 if (exporting)
14030 cp_lexer_consume_token (parser->lexer);
14031 if (next->keyword == RID__MODULE)
14032 cp_parser_module_declaration (parser, MP_NOT_MODULE, exporting);
14033 else
14034 cp_parser_import_declaration (parser, MP_NOT_MODULE, exporting);
14035 }
14036 /* If the next token is `extern', 'static' or 'inline' and the one
14037 after that is `template', we have a GNU extended explicit
14038 instantiation directive. */
14039 else if (cp_parser_allow_gnu_extensions_p (parser)
14040 && token2->keyword == RID_TEMPLATE
14041 && (token1->keyword == RID_EXTERN
14042 || token1->keyword == RID_STATIC
14043 || token1->keyword == RID_INLINE))
14044 cp_parser_explicit_instantiation (parser);
14045 /* If the next token is `namespace', check for a named or unnamed
14046 namespace definition. */
14047 else if (token1->keyword == RID_NAMESPACE
14048 && (/* A named namespace definition. */
14049 (token2->type == CPP_NAME
14050 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
14051 != CPP_EQ))
14052 || (token2->type == CPP_OPEN_SQUARE
14053 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
14054 == CPP_OPEN_SQUARE)
14055 /* An unnamed namespace definition. */
14056 || token2->type == CPP_OPEN_BRACE
14057 || token2->keyword == RID_ATTRIBUTE))
14058 cp_parser_namespace_definition (parser);
14059 /* An inline (associated) namespace definition. */
14060 else if (token2->keyword == RID_NAMESPACE
14061 && token1->keyword == RID_INLINE)
14062 cp_parser_namespace_definition (parser);
14063 /* Objective-C++ declaration/definition. */
14064 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1->keyword))
14065 cp_parser_objc_declaration (parser, attributes);
14066 else if (c_dialect_objc ()
14067 && token1->keyword == RID_ATTRIBUTE
14068 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
14069 cp_parser_objc_declaration (parser, attributes);
14070 /* At this point we may have a template declared by a concept
14071 introduction. */
14072 else if (flag_concepts
14073 && cp_parser_template_declaration_after_export (parser,
14074 /*member_p=*/false))
14075 /* We did. */;
14076 else
14077 /* Try to parse a block-declaration, or a function-definition. */
14078 cp_parser_block_declaration (parser, /*statement_p=*/false);
14079
14080 /* Free any declarators allocated. */
14081 obstack_free (&declarator_obstack, p);
14082 }
14083
14084 /* Parse a namespace-scope declaration. */
14085
14086 static void
14087 cp_parser_toplevel_declaration (cp_parser* parser)
14088 {
14089 cp_token *token = cp_lexer_peek_token (parser->lexer);
14090
14091 if (token->type == CPP_PRAGMA)
14092 /* A top-level declaration can consist solely of a #pragma. A
14093 nested declaration cannot, so this is done here and not in
14094 cp_parser_declaration. (A #pragma at block scope is
14095 handled in cp_parser_statement.) */
14096 cp_parser_pragma (parser, pragma_external, NULL);
14097 else if (token->type == CPP_SEMICOLON)
14098 {
14099 cp_lexer_consume_token (parser->lexer);
14100 /* A declaration consisting of a single semicolon is invalid
14101 * before C++11. Allow it unless we're being pedantic. */
14102 if (cxx_dialect < cxx11)
14103 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
14104 }
14105 else
14106 /* Parse the declaration itself. */
14107 cp_parser_declaration (parser, NULL_TREE);
14108 }
14109
14110 /* Parse a block-declaration.
14111
14112 block-declaration:
14113 simple-declaration
14114 asm-definition
14115 namespace-alias-definition
14116 using-declaration
14117 using-directive
14118
14119 GNU Extension:
14120
14121 block-declaration:
14122 __extension__ block-declaration
14123
14124 C++0x Extension:
14125
14126 block-declaration:
14127 static_assert-declaration
14128
14129 If STATEMENT_P is TRUE, then this block-declaration is occurring as
14130 part of a declaration-statement. */
14131
14132 static void
14133 cp_parser_block_declaration (cp_parser *parser,
14134 bool statement_p)
14135 {
14136 int saved_pedantic;
14137
14138 /* Check for the `__extension__' keyword. */
14139 if (cp_parser_extension_opt (parser, &saved_pedantic))
14140 {
14141 /* Parse the qualified declaration. */
14142 cp_parser_block_declaration (parser, statement_p);
14143 /* Restore the PEDANTIC flag. */
14144 pedantic = saved_pedantic;
14145
14146 return;
14147 }
14148
14149 /* Peek at the next token to figure out which kind of declaration is
14150 present. */
14151 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
14152
14153 /* If the next keyword is `asm', we have an asm-definition. */
14154 if (token1->keyword == RID_ASM)
14155 {
14156 if (statement_p)
14157 cp_parser_commit_to_tentative_parse (parser);
14158 cp_parser_asm_definition (parser);
14159 }
14160 /* If the next keyword is `namespace', we have a
14161 namespace-alias-definition. */
14162 else if (token1->keyword == RID_NAMESPACE)
14163 cp_parser_namespace_alias_definition (parser);
14164 /* If the next keyword is `using', we have a
14165 using-declaration, a using-directive, or an alias-declaration. */
14166 else if (token1->keyword == RID_USING)
14167 {
14168 cp_token *token2;
14169
14170 if (statement_p)
14171 cp_parser_commit_to_tentative_parse (parser);
14172 /* If the token after `using' is `namespace', then we have a
14173 using-directive. */
14174 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14175 if (token2->keyword == RID_NAMESPACE)
14176 cp_parser_using_directive (parser);
14177 else if (token2->keyword == RID_ENUM)
14178 cp_parser_using_enum (parser);
14179 /* If the second token after 'using' is '=', then we have an
14180 alias-declaration. */
14181 else if (cxx_dialect >= cxx11
14182 && token2->type == CPP_NAME
14183 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
14184 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
14185 cp_parser_alias_declaration (parser);
14186 /* Otherwise, it's a using-declaration. */
14187 else
14188 cp_parser_using_declaration (parser,
14189 /*access_declaration_p=*/false);
14190 }
14191 /* If the next keyword is `__label__' we have a misplaced label
14192 declaration. */
14193 else if (token1->keyword == RID_LABEL)
14194 {
14195 cp_lexer_consume_token (parser->lexer);
14196 error_at (token1->location, "%<__label__%> not at the beginning of a block");
14197 cp_parser_skip_to_end_of_statement (parser);
14198 /* If the next token is now a `;', consume it. */
14199 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14200 cp_lexer_consume_token (parser->lexer);
14201 }
14202 /* If the next token is `static_assert' we have a static assertion. */
14203 else if (token1->keyword == RID_STATIC_ASSERT)
14204 cp_parser_static_assert (parser, /*member_p=*/false);
14205 /* Anything else must be a simple-declaration. */
14206 else
14207 cp_parser_simple_declaration (parser, !statement_p,
14208 /*maybe_range_for_decl*/NULL);
14209 }
14210
14211 /* Parse a simple-declaration.
14212
14213 simple-declaration:
14214 decl-specifier-seq [opt] init-declarator-list [opt] ;
14215 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
14216 brace-or-equal-initializer ;
14217
14218 init-declarator-list:
14219 init-declarator
14220 init-declarator-list , init-declarator
14221
14222 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
14223 function-definition as a simple-declaration.
14224
14225 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14226 parsed declaration if it is an uninitialized single declarator not followed
14227 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14228 if present, will not be consumed. */
14229
14230 static void
14231 cp_parser_simple_declaration (cp_parser* parser,
14232 bool function_definition_allowed_p,
14233 tree *maybe_range_for_decl)
14234 {
14235 cp_decl_specifier_seq decl_specifiers;
14236 int declares_class_or_enum;
14237 bool saw_declarator;
14238 location_t comma_loc = UNKNOWN_LOCATION;
14239 location_t init_loc = UNKNOWN_LOCATION;
14240
14241 if (maybe_range_for_decl)
14242 *maybe_range_for_decl = NULL_TREE;
14243
14244 /* Defer access checks until we know what is being declared; the
14245 checks for names appearing in the decl-specifier-seq should be
14246 done as if we were in the scope of the thing being declared. */
14247 push_deferring_access_checks (dk_deferred);
14248
14249 /* Parse the decl-specifier-seq. We have to keep track of whether
14250 or not the decl-specifier-seq declares a named class or
14251 enumeration type, since that is the only case in which the
14252 init-declarator-list is allowed to be empty.
14253
14254 [dcl.dcl]
14255
14256 In a simple-declaration, the optional init-declarator-list can be
14257 omitted only when declaring a class or enumeration, that is when
14258 the decl-specifier-seq contains either a class-specifier, an
14259 elaborated-type-specifier, or an enum-specifier. */
14260 cp_parser_decl_specifier_seq (parser,
14261 CP_PARSER_FLAGS_OPTIONAL,
14262 &decl_specifiers,
14263 &declares_class_or_enum);
14264 /* We no longer need to defer access checks. */
14265 stop_deferring_access_checks ();
14266
14267 /* In a block scope, a valid declaration must always have a
14268 decl-specifier-seq. By not trying to parse declarators, we can
14269 resolve the declaration/expression ambiguity more quickly. */
14270 if (!function_definition_allowed_p
14271 && !decl_specifiers.any_specifiers_p)
14272 {
14273 cp_parser_error (parser, "expected declaration");
14274 goto done;
14275 }
14276
14277 /* If the next two tokens are both identifiers, the code is
14278 erroneous. The usual cause of this situation is code like:
14279
14280 T t;
14281
14282 where "T" should name a type -- but does not. */
14283 if (!decl_specifiers.any_type_specifiers_p
14284 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14285 {
14286 /* If parsing tentatively, we should commit; we really are
14287 looking at a declaration. */
14288 cp_parser_commit_to_tentative_parse (parser);
14289 /* Give up. */
14290 goto done;
14291 }
14292
14293 cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
14294
14295 /* Look for C++17 decomposition declaration. */
14296 for (size_t n = 1; ; n++)
14297 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
14298 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
14299 continue;
14300 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
14301 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
14302 && decl_specifiers.any_specifiers_p)
14303 {
14304 tree decl
14305 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
14306 maybe_range_for_decl,
14307 &init_loc);
14308
14309 /* The next token should be either a `,' or a `;'. */
14310 cp_token *token = cp_lexer_peek_token (parser->lexer);
14311 /* If it's a `;', we are done. */
14312 if (token->type == CPP_SEMICOLON)
14313 goto finish;
14314 else if (maybe_range_for_decl)
14315 {
14316 if (*maybe_range_for_decl == NULL_TREE)
14317 *maybe_range_for_decl = error_mark_node;
14318 goto finish;
14319 }
14320 /* Anything else is an error. */
14321 else
14322 {
14323 /* If we have already issued an error message we don't need
14324 to issue another one. */
14325 if ((decl != error_mark_node
14326 && DECL_INITIAL (decl) != error_mark_node)
14327 || cp_parser_uncommitted_to_tentative_parse_p (parser))
14328 cp_parser_error (parser, "expected %<;%>");
14329 /* Skip tokens until we reach the end of the statement. */
14330 cp_parser_skip_to_end_of_statement (parser);
14331 /* If the next token is now a `;', consume it. */
14332 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14333 cp_lexer_consume_token (parser->lexer);
14334 goto done;
14335 }
14336 }
14337 else
14338 break;
14339
14340 tree last_type;
14341 bool auto_specifier_p;
14342 /* NULL_TREE if both variable and function declaration are allowed,
14343 error_mark_node if function declaration are not allowed and
14344 a FUNCTION_DECL that should be diagnosed if it is followed by
14345 variable declarations. */
14346 tree auto_function_declaration;
14347
14348 last_type = NULL_TREE;
14349 auto_specifier_p
14350 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
14351 auto_function_declaration = NULL_TREE;
14352
14353 /* Keep going until we hit the `;' at the end of the simple
14354 declaration. */
14355 saw_declarator = false;
14356 while (cp_lexer_next_token_is_not (parser->lexer,
14357 CPP_SEMICOLON))
14358 {
14359 cp_token *token;
14360 bool function_definition_p;
14361 tree decl;
14362 tree auto_result = NULL_TREE;
14363
14364 if (saw_declarator)
14365 {
14366 /* If we are processing next declarator, comma is expected */
14367 token = cp_lexer_peek_token (parser->lexer);
14368 gcc_assert (token->type == CPP_COMMA);
14369 cp_lexer_consume_token (parser->lexer);
14370 if (maybe_range_for_decl)
14371 {
14372 *maybe_range_for_decl = error_mark_node;
14373 if (comma_loc == UNKNOWN_LOCATION)
14374 comma_loc = token->location;
14375 }
14376 }
14377 else
14378 saw_declarator = true;
14379
14380 /* Parse the init-declarator. */
14381 decl = cp_parser_init_declarator (parser,
14382 CP_PARSER_FLAGS_NONE,
14383 &decl_specifiers,
14384 /*checks=*/NULL,
14385 function_definition_allowed_p,
14386 /*member_p=*/false,
14387 declares_class_or_enum,
14388 &function_definition_p,
14389 maybe_range_for_decl,
14390 &init_loc,
14391 &auto_result);
14392 /* If an error occurred while parsing tentatively, exit quickly.
14393 (That usually happens when in the body of a function; each
14394 statement is treated as a declaration-statement until proven
14395 otherwise.) */
14396 if (cp_parser_error_occurred (parser))
14397 goto done;
14398
14399 if (auto_specifier_p && cxx_dialect >= cxx14)
14400 {
14401 /* If the init-declarator-list contains more than one
14402 init-declarator, they shall all form declarations of
14403 variables. */
14404 if (auto_function_declaration == NULL_TREE)
14405 auto_function_declaration
14406 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
14407 else if (TREE_CODE (decl) == FUNCTION_DECL
14408 || auto_function_declaration != error_mark_node)
14409 {
14410 error_at (decl_specifiers.locations[ds_type_spec],
14411 "non-variable %qD in declaration with more than one "
14412 "declarator with placeholder type",
14413 TREE_CODE (decl) == FUNCTION_DECL
14414 ? decl : auto_function_declaration);
14415 auto_function_declaration = error_mark_node;
14416 }
14417 }
14418
14419 if (auto_result
14420 && (!processing_template_decl || !type_uses_auto (auto_result)))
14421 {
14422 if (last_type
14423 && last_type != error_mark_node
14424 && !same_type_p (auto_result, last_type))
14425 {
14426 /* If the list of declarators contains more than one declarator,
14427 the type of each declared variable is determined as described
14428 above. If the type deduced for the template parameter U is not
14429 the same in each deduction, the program is ill-formed. */
14430 error_at (decl_specifiers.locations[ds_type_spec],
14431 "inconsistent deduction for %qT: %qT and then %qT",
14432 decl_specifiers.type, last_type, auto_result);
14433 last_type = error_mark_node;
14434 }
14435 else
14436 last_type = auto_result;
14437 }
14438
14439 /* Handle function definitions specially. */
14440 if (function_definition_p)
14441 {
14442 /* If the next token is a `,', then we are probably
14443 processing something like:
14444
14445 void f() {}, *p;
14446
14447 which is erroneous. */
14448 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14449 {
14450 cp_token *token = cp_lexer_peek_token (parser->lexer);
14451 error_at (token->location,
14452 "mixing"
14453 " declarations and function-definitions is forbidden");
14454 }
14455 /* Otherwise, we're done with the list of declarators. */
14456 else
14457 {
14458 pop_deferring_access_checks ();
14459 return;
14460 }
14461 }
14462 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
14463 *maybe_range_for_decl = decl;
14464 /* The next token should be either a `,' or a `;'. */
14465 token = cp_lexer_peek_token (parser->lexer);
14466 /* If it's a `,', there are more declarators to come. */
14467 if (token->type == CPP_COMMA)
14468 /* will be consumed next time around */;
14469 /* If it's a `;', we are done. */
14470 else if (token->type == CPP_SEMICOLON)
14471 break;
14472 else if (maybe_range_for_decl)
14473 {
14474 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
14475 permerror (decl_specifiers.locations[ds_type_spec],
14476 "types may not be defined in a for-range-declaration");
14477 break;
14478 }
14479 /* Anything else is an error. */
14480 else
14481 {
14482 /* If we have already issued an error message we don't need
14483 to issue another one. */
14484 if ((decl != error_mark_node
14485 && DECL_INITIAL (decl) != error_mark_node)
14486 || cp_parser_uncommitted_to_tentative_parse_p (parser))
14487 cp_parser_error (parser, "expected %<,%> or %<;%>");
14488 /* Skip tokens until we reach the end of the statement. */
14489 cp_parser_skip_to_end_of_statement (parser);
14490 /* If the next token is now a `;', consume it. */
14491 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14492 cp_lexer_consume_token (parser->lexer);
14493 goto done;
14494 }
14495 /* After the first time around, a function-definition is not
14496 allowed -- even if it was OK at first. For example:
14497
14498 int i, f() {}
14499
14500 is not valid. */
14501 function_definition_allowed_p = false;
14502 }
14503
14504 /* Issue an error message if no declarators are present, and the
14505 decl-specifier-seq does not itself declare a class or
14506 enumeration: [dcl.dcl]/3. */
14507 if (!saw_declarator)
14508 {
14509 if (cp_parser_declares_only_class_p (parser))
14510 {
14511 if (!declares_class_or_enum
14512 && decl_specifiers.type
14513 && OVERLOAD_TYPE_P (decl_specifiers.type))
14514 /* Ensure an error is issued anyway when finish_decltype_type,
14515 called via cp_parser_decl_specifier_seq, returns a class or
14516 an enumeration (c++/51786). */
14517 decl_specifiers.type = NULL_TREE;
14518 shadow_tag (&decl_specifiers);
14519 }
14520 /* Perform any deferred access checks. */
14521 perform_deferred_access_checks (tf_warning_or_error);
14522 }
14523
14524 /* Consume the `;'. */
14525 finish:
14526 if (!maybe_range_for_decl)
14527 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14528 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14529 {
14530 if (init_loc != UNKNOWN_LOCATION)
14531 error_at (init_loc, "initializer in range-based %<for%> loop");
14532 if (comma_loc != UNKNOWN_LOCATION)
14533 error_at (comma_loc,
14534 "multiple declarations in range-based %<for%> loop");
14535 }
14536
14537 done:
14538 pop_deferring_access_checks ();
14539 }
14540
14541 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
14542 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
14543 initializer ; */
14544
14545 static tree
14546 cp_parser_decomposition_declaration (cp_parser *parser,
14547 cp_decl_specifier_seq *decl_specifiers,
14548 tree *maybe_range_for_decl,
14549 location_t *init_loc)
14550 {
14551 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
14552 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
14553 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
14554
14555 /* Parse the identifier-list. */
14556 auto_vec<cp_expr, 10> v;
14557 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
14558 while (true)
14559 {
14560 cp_expr e = cp_parser_identifier (parser);
14561 if (e.get_value () == error_mark_node)
14562 break;
14563 v.safe_push (e);
14564 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14565 break;
14566 cp_lexer_consume_token (parser->lexer);
14567 }
14568
14569 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
14570 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14571 {
14572 end_loc = UNKNOWN_LOCATION;
14573 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
14574 false);
14575 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
14576 cp_lexer_consume_token (parser->lexer);
14577 else
14578 {
14579 cp_parser_skip_to_end_of_statement (parser);
14580 return error_mark_node;
14581 }
14582 }
14583
14584 if (cxx_dialect < cxx17)
14585 pedwarn (loc, 0, "structured bindings only available with "
14586 "%<-std=c++17%> or %<-std=gnu++17%>");
14587
14588 tree pushed_scope;
14589 cp_declarator *declarator = make_declarator (cdk_decomp);
14590 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
14591 declarator->id_loc = loc;
14592 if (ref_qual != REF_QUAL_NONE)
14593 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
14594 ref_qual == REF_QUAL_RVALUE,
14595 NULL_TREE);
14596 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
14597 NULL_TREE, decl_specifiers->attributes,
14598 &pushed_scope);
14599 tree orig_decl = decl;
14600
14601 unsigned int i;
14602 cp_expr e;
14603 cp_decl_specifier_seq decl_specs;
14604 clear_decl_specs (&decl_specs);
14605 decl_specs.type = make_auto ();
14606 tree prev = decl;
14607 FOR_EACH_VEC_ELT (v, i, e)
14608 {
14609 if (i == 0)
14610 declarator = make_id_declarator (NULL_TREE, e.get_value (),
14611 sfk_none, e.get_location ());
14612 else
14613 {
14614 declarator->u.id.unqualified_name = e.get_value ();
14615 declarator->id_loc = e.get_location ();
14616 }
14617 tree elt_pushed_scope;
14618 tree decl2 = start_decl (declarator, &decl_specs, SD_DECOMPOSITION,
14619 NULL_TREE, NULL_TREE, &elt_pushed_scope);
14620 if (decl2 == error_mark_node)
14621 decl = error_mark_node;
14622 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
14623 {
14624 /* Ensure we've diagnosed redeclaration if we aren't creating
14625 a new VAR_DECL. */
14626 gcc_assert (errorcount);
14627 decl = error_mark_node;
14628 }
14629 else
14630 prev = decl2;
14631 if (elt_pushed_scope)
14632 pop_scope (elt_pushed_scope);
14633 }
14634
14635 if (v.is_empty ())
14636 {
14637 error_at (loc, "empty structured binding declaration");
14638 decl = error_mark_node;
14639 }
14640
14641 if (maybe_range_for_decl == NULL
14642 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14643 {
14644 bool non_constant_p = false, is_direct_init = false;
14645 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
14646 tree initializer = cp_parser_initializer (parser, &is_direct_init,
14647 &non_constant_p);
14648 if (initializer == NULL_TREE
14649 || (TREE_CODE (initializer) == TREE_LIST
14650 && TREE_CHAIN (initializer))
14651 || (is_direct_init
14652 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
14653 && CONSTRUCTOR_NELTS (initializer) != 1))
14654 {
14655 error_at (loc, "invalid initializer for structured binding "
14656 "declaration");
14657 initializer = error_mark_node;
14658 }
14659
14660 if (decl != error_mark_node)
14661 {
14662 cp_maybe_mangle_decomp (decl, prev, v.length ());
14663 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
14664 (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14665 cp_finish_decomp (decl, prev, v.length ());
14666 }
14667 }
14668 else if (decl != error_mark_node)
14669 {
14670 *maybe_range_for_decl = prev;
14671 /* Ensure DECL_VALUE_EXPR is created for all the decls but
14672 the underlying DECL. */
14673 cp_finish_decomp (decl, prev, v.length ());
14674 }
14675
14676 if (pushed_scope)
14677 pop_scope (pushed_scope);
14678
14679 if (decl == error_mark_node && DECL_P (orig_decl))
14680 {
14681 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
14682 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
14683 }
14684
14685 return decl;
14686 }
14687
14688 /* Parse a decl-specifier-seq.
14689
14690 decl-specifier-seq:
14691 decl-specifier-seq [opt] decl-specifier
14692 decl-specifier attribute-specifier-seq [opt] (C++11)
14693
14694 decl-specifier:
14695 storage-class-specifier
14696 type-specifier
14697 function-specifier
14698 friend
14699 typedef
14700
14701 GNU Extension:
14702
14703 decl-specifier:
14704 attributes
14705
14706 Concepts Extension:
14707
14708 decl-specifier:
14709 concept
14710
14711 Set *DECL_SPECS to a representation of the decl-specifier-seq.
14712
14713 The parser flags FLAGS is used to control type-specifier parsing.
14714
14715 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
14716 flags:
14717
14718 1: one of the decl-specifiers is an elaborated-type-specifier
14719 (i.e., a type declaration)
14720 2: one of the decl-specifiers is an enum-specifier or a
14721 class-specifier (i.e., a type definition)
14722
14723 */
14724
14725 static void
14726 cp_parser_decl_specifier_seq (cp_parser* parser,
14727 cp_parser_flags flags,
14728 cp_decl_specifier_seq *decl_specs,
14729 int* declares_class_or_enum)
14730 {
14731 bool constructor_possible_p = !parser->in_declarator_p;
14732 bool found_decl_spec = false;
14733 cp_token *start_token = NULL;
14734 cp_decl_spec ds;
14735
14736 /* Clear DECL_SPECS. */
14737 clear_decl_specs (decl_specs);
14738
14739 /* Assume no class or enumeration type is declared. */
14740 *declares_class_or_enum = 0;
14741
14742 /* Keep reading specifiers until there are no more to read. */
14743 while (true)
14744 {
14745 bool constructor_p;
14746 cp_token *token;
14747 ds = ds_last;
14748
14749 /* Peek at the next token. */
14750 token = cp_lexer_peek_token (parser->lexer);
14751
14752 /* Save the first token of the decl spec list for error
14753 reporting. */
14754 if (!start_token)
14755 start_token = token;
14756 /* Handle attributes. */
14757 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
14758 && cp_next_tokens_can_be_attribute_p (parser))
14759 {
14760 /* Parse the attributes. */
14761 tree attrs = cp_parser_attributes_opt (parser);
14762
14763 /* In a sequence of declaration specifiers, c++11 attributes
14764 appertain to the type that precede them. In that case
14765 [dcl.spec]/1 says:
14766
14767 The attribute-specifier-seq affects the type only for
14768 the declaration it appears in, not other declarations
14769 involving the same type.
14770
14771 But for now let's force the user to position the
14772 attribute either at the beginning of the declaration or
14773 after the declarator-id, which would clearly mean that it
14774 applies to the declarator. */
14775 if (cxx11_attribute_p (attrs))
14776 {
14777 if (!found_decl_spec)
14778 /* The c++11 attribute is at the beginning of the
14779 declaration. It appertains to the entity being
14780 declared. */;
14781 else
14782 {
14783 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
14784 {
14785 /* This is an attribute following a
14786 class-specifier. */
14787 if (decl_specs->type_definition_p)
14788 warn_misplaced_attr_for_class_type (token->location,
14789 decl_specs->type);
14790 attrs = NULL_TREE;
14791 }
14792 else
14793 {
14794 decl_specs->std_attributes
14795 = attr_chainon (decl_specs->std_attributes, attrs);
14796 if (decl_specs->locations[ds_std_attribute] == 0)
14797 decl_specs->locations[ds_std_attribute] = token->location;
14798 }
14799 continue;
14800 }
14801 }
14802
14803 decl_specs->attributes
14804 = attr_chainon (decl_specs->attributes, attrs);
14805 if (decl_specs->locations[ds_attribute] == 0)
14806 decl_specs->locations[ds_attribute] = token->location;
14807 continue;
14808 }
14809 /* Assume we will find a decl-specifier keyword. */
14810 found_decl_spec = true;
14811 /* If the next token is an appropriate keyword, we can simply
14812 add it to the list. */
14813 switch (token->keyword)
14814 {
14815 /* decl-specifier:
14816 friend
14817 constexpr
14818 constinit */
14819 case RID_FRIEND:
14820 if (!at_class_scope_p ())
14821 {
14822 gcc_rich_location richloc (token->location);
14823 richloc.add_fixit_remove ();
14824 error_at (&richloc, "%<friend%> used outside of class");
14825 cp_lexer_purge_token (parser->lexer);
14826 }
14827 else
14828 {
14829 ds = ds_friend;
14830 /* Consume the token. */
14831 cp_lexer_consume_token (parser->lexer);
14832 }
14833 break;
14834
14835 case RID_CONSTEXPR:
14836 ds = ds_constexpr;
14837 cp_lexer_consume_token (parser->lexer);
14838 break;
14839
14840 case RID_CONSTINIT:
14841 ds = ds_constinit;
14842 cp_lexer_consume_token (parser->lexer);
14843 break;
14844
14845 case RID_CONSTEVAL:
14846 ds = ds_consteval;
14847 cp_lexer_consume_token (parser->lexer);
14848 break;
14849
14850 case RID_CONCEPT:
14851 ds = ds_concept;
14852 cp_lexer_consume_token (parser->lexer);
14853
14854 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14855 break;
14856
14857 /* Warn for concept as a decl-specifier. We'll rewrite these as
14858 concept declarations later. */
14859 if (!flag_concepts_ts)
14860 {
14861 cp_token *next = cp_lexer_peek_token (parser->lexer);
14862 if (next->keyword == RID_BOOL)
14863 pedwarn (next->location, 0, "the %<bool%> keyword is not "
14864 "allowed in a C++20 concept definition");
14865 else
14866 pedwarn (token->location, 0, "C++20 concept definition syntax "
14867 "is %<concept <name> = <expr>%>");
14868 }
14869
14870 /* In C++20 a concept definition is just 'concept name = expr;'
14871 Support that syntax as a TS extension by pretending we've seen
14872 the 'bool' specifier. */
14873 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14874 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
14875 && !decl_specs->any_type_specifiers_p)
14876 {
14877 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
14878 token, /*type_definition*/false);
14879 decl_specs->any_type_specifiers_p = true;
14880 }
14881 break;
14882
14883 /* function-specifier:
14884 inline
14885 virtual
14886 explicit */
14887 case RID_INLINE:
14888 case RID_VIRTUAL:
14889 case RID_EXPLICIT:
14890 cp_parser_function_specifier_opt (parser, decl_specs);
14891 break;
14892
14893 /* decl-specifier:
14894 typedef */
14895 case RID_TYPEDEF:
14896 ds = ds_typedef;
14897 /* Consume the token. */
14898 cp_lexer_consume_token (parser->lexer);
14899
14900 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14901 break;
14902
14903 /* A constructor declarator cannot appear in a typedef. */
14904 constructor_possible_p = false;
14905 /* The "typedef" keyword can only occur in a declaration; we
14906 may as well commit at this point. */
14907 cp_parser_commit_to_tentative_parse (parser);
14908
14909 if (decl_specs->storage_class != sc_none)
14910 decl_specs->conflicting_specifiers_p = true;
14911 break;
14912
14913 /* storage-class-specifier:
14914 auto
14915 register
14916 static
14917 extern
14918 mutable
14919
14920 GNU Extension:
14921 thread */
14922 case RID_AUTO:
14923 if (cxx_dialect == cxx98)
14924 {
14925 /* Consume the token. */
14926 cp_lexer_consume_token (parser->lexer);
14927
14928 /* Complain about `auto' as a storage specifier, if
14929 we're complaining about C++0x compatibility. */
14930 gcc_rich_location richloc (token->location);
14931 richloc.add_fixit_remove ();
14932 warning_at (&richloc, OPT_Wc__11_compat,
14933 "%<auto%> changes meaning in C++11; "
14934 "please remove it");
14935
14936 /* Set the storage class anyway. */
14937 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
14938 token);
14939 }
14940 else
14941 /* C++0x auto type-specifier. */
14942 found_decl_spec = false;
14943 break;
14944
14945 case RID_REGISTER:
14946 case RID_STATIC:
14947 case RID_EXTERN:
14948 case RID_MUTABLE:
14949 /* Consume the token. */
14950 cp_lexer_consume_token (parser->lexer);
14951 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
14952 token);
14953 break;
14954 case RID_THREAD:
14955 /* Consume the token. */
14956 ds = ds_thread;
14957 cp_lexer_consume_token (parser->lexer);
14958 break;
14959
14960 default:
14961 /* We did not yet find a decl-specifier yet. */
14962 found_decl_spec = false;
14963 break;
14964 }
14965
14966 if (found_decl_spec
14967 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
14968 && token->keyword != RID_CONSTEXPR)
14969 error ("%<decl-specifier%> invalid in condition");
14970
14971 if (found_decl_spec
14972 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14973 && token->keyword != RID_MUTABLE
14974 && token->keyword != RID_CONSTEXPR
14975 && token->keyword != RID_CONSTEVAL)
14976 error_at (token->location, "%qD invalid in lambda",
14977 ridpointers[token->keyword]);
14978
14979 if (ds != ds_last)
14980 set_and_check_decl_spec_loc (decl_specs, ds, token);
14981
14982 /* Constructors are a special case. The `S' in `S()' is not a
14983 decl-specifier; it is the beginning of the declarator. */
14984 constructor_p
14985 = (!found_decl_spec
14986 && constructor_possible_p
14987 && (cp_parser_constructor_declarator_p
14988 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
14989 ds_friend))));
14990
14991 /* If we don't have a DECL_SPEC yet, then we must be looking at
14992 a type-specifier. */
14993 if (!found_decl_spec && !constructor_p)
14994 {
14995 int decl_spec_declares_class_or_enum;
14996 bool is_cv_qualifier;
14997 tree type_spec;
14998
14999 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15000 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15001
15002 type_spec
15003 = cp_parser_type_specifier (parser, flags,
15004 decl_specs,
15005 /*is_declaration=*/true,
15006 &decl_spec_declares_class_or_enum,
15007 &is_cv_qualifier);
15008 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
15009
15010 /* If this type-specifier referenced a user-defined type
15011 (a typedef, class-name, etc.), then we can't allow any
15012 more such type-specifiers henceforth.
15013
15014 [dcl.spec]
15015
15016 The longest sequence of decl-specifiers that could
15017 possibly be a type name is taken as the
15018 decl-specifier-seq of a declaration. The sequence shall
15019 be self-consistent as described below.
15020
15021 [dcl.type]
15022
15023 As a general rule, at most one type-specifier is allowed
15024 in the complete decl-specifier-seq of a declaration. The
15025 only exceptions are the following:
15026
15027 -- const or volatile can be combined with any other
15028 type-specifier.
15029
15030 -- signed or unsigned can be combined with char, long,
15031 short, or int.
15032
15033 -- ..
15034
15035 Example:
15036
15037 typedef char* Pc;
15038 void g (const int Pc);
15039
15040 Here, Pc is *not* part of the decl-specifier seq; it's
15041 the declarator. Therefore, once we see a type-specifier
15042 (other than a cv-qualifier), we forbid any additional
15043 user-defined types. We *do* still allow things like `int
15044 int' to be considered a decl-specifier-seq, and issue the
15045 error message later. */
15046 if (type_spec && !is_cv_qualifier)
15047 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15048 /* A constructor declarator cannot follow a type-specifier. */
15049 if (type_spec)
15050 {
15051 constructor_possible_p = false;
15052 found_decl_spec = true;
15053 if (!is_cv_qualifier)
15054 decl_specs->any_type_specifiers_p = true;
15055
15056 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
15057 error_at (token->location, "type-specifier invalid in lambda");
15058 }
15059 }
15060
15061 /* If we still do not have a DECL_SPEC, then there are no more
15062 decl-specifiers. */
15063 if (!found_decl_spec)
15064 break;
15065
15066 decl_specs->any_specifiers_p = true;
15067 /* After we see one decl-specifier, further decl-specifiers are
15068 always optional. */
15069 flags |= CP_PARSER_FLAGS_OPTIONAL;
15070 }
15071
15072 /* Don't allow a friend specifier with a class definition. */
15073 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
15074 && (*declares_class_or_enum & 2))
15075 error_at (decl_specs->locations[ds_friend],
15076 "class definition may not be declared a friend");
15077 }
15078
15079 /* Parse an (optional) storage-class-specifier.
15080
15081 storage-class-specifier:
15082 auto
15083 register
15084 static
15085 extern
15086 mutable
15087
15088 GNU Extension:
15089
15090 storage-class-specifier:
15091 thread
15092
15093 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
15094
15095 static tree
15096 cp_parser_storage_class_specifier_opt (cp_parser* parser)
15097 {
15098 switch (cp_lexer_peek_token (parser->lexer)->keyword)
15099 {
15100 case RID_AUTO:
15101 if (cxx_dialect != cxx98)
15102 return NULL_TREE;
15103 /* Fall through for C++98. */
15104 gcc_fallthrough ();
15105
15106 case RID_REGISTER:
15107 case RID_STATIC:
15108 case RID_EXTERN:
15109 case RID_MUTABLE:
15110 case RID_THREAD:
15111 /* Consume the token. */
15112 return cp_lexer_consume_token (parser->lexer)->u.value;
15113
15114 default:
15115 return NULL_TREE;
15116 }
15117 }
15118
15119 /* Parse an (optional) function-specifier.
15120
15121 function-specifier:
15122 inline
15123 virtual
15124 explicit
15125
15126 C++20 Extension:
15127 explicit(constant-expression)
15128
15129 Returns an IDENTIFIER_NODE corresponding to the keyword used.
15130 Updates DECL_SPECS, if it is non-NULL. */
15131
15132 static tree
15133 cp_parser_function_specifier_opt (cp_parser* parser,
15134 cp_decl_specifier_seq *decl_specs)
15135 {
15136 cp_token *token = cp_lexer_peek_token (parser->lexer);
15137 switch (token->keyword)
15138 {
15139 case RID_INLINE:
15140 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
15141 break;
15142
15143 case RID_VIRTUAL:
15144 /* 14.5.2.3 [temp.mem]
15145
15146 A member function template shall not be virtual. */
15147 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15148 && current_class_type)
15149 error_at (token->location, "templates may not be %<virtual%>");
15150 else
15151 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
15152 break;
15153
15154 case RID_EXPLICIT:
15155 {
15156 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
15157 /* If we see '(', it's C++20 explicit(bool). */
15158 tree expr;
15159 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15160 {
15161 matching_parens parens;
15162 parens.consume_open (parser);
15163
15164 /* New types are not allowed in an explicit-specifier. */
15165 const char *saved_message
15166 = parser->type_definition_forbidden_message;
15167 parser->type_definition_forbidden_message
15168 = G_("types may not be defined in explicit-specifier");
15169
15170 if (cxx_dialect < cxx20)
15171 pedwarn (token->location, 0,
15172 "%<explicit(bool)%> only available with %<-std=c++20%> "
15173 "or %<-std=gnu++20%>");
15174
15175 /* Parse the constant-expression. */
15176 expr = cp_parser_constant_expression (parser);
15177
15178 /* Restore the saved message. */
15179 parser->type_definition_forbidden_message = saved_message;
15180 parens.require_close (parser);
15181 }
15182 else
15183 /* The explicit-specifier explicit without a constant-expression is
15184 equivalent to the explicit-specifier explicit(true). */
15185 expr = boolean_true_node;
15186
15187 /* [dcl.fct.spec]
15188 "the constant-expression, if supplied, shall be a contextually
15189 converted constant expression of type bool." */
15190 expr = build_explicit_specifier (expr, tf_warning_or_error);
15191 /* We could evaluate it -- mark the decl as appropriate. */
15192 if (expr == boolean_true_node)
15193 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
15194 else if (expr == boolean_false_node)
15195 /* Don't mark the decl as explicit. */;
15196 else if (decl_specs)
15197 /* The expression was value-dependent. Remember it so that we can
15198 substitute it later. */
15199 decl_specs->explicit_specifier = expr;
15200 return id;
15201 }
15202
15203 default:
15204 return NULL_TREE;
15205 }
15206
15207 /* Consume the token. */
15208 return cp_lexer_consume_token (parser->lexer)->u.value;
15209 }
15210
15211 /* Parse a linkage-specification.
15212
15213 linkage-specification:
15214 extern string-literal { declaration-seq [opt] }
15215 extern string-literal declaration */
15216
15217 static void
15218 cp_parser_linkage_specification (cp_parser* parser, tree prefix_attr)
15219 {
15220 tree linkage;
15221
15222 /* Look for the `extern' keyword. */
15223 cp_token *extern_token
15224 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
15225
15226 /* Look for the string-literal. */
15227 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
15228 linkage = cp_parser_string_literal (parser, false, false);
15229
15230 /* Transform the literal into an identifier. If the literal is a
15231 wide-character string, or contains embedded NULs, then we can't
15232 handle it as the user wants. */
15233 if (strlen (TREE_STRING_POINTER (linkage))
15234 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
15235 {
15236 cp_parser_error (parser, "invalid linkage-specification");
15237 /* Assume C++ linkage. */
15238 linkage = lang_name_cplusplus;
15239 }
15240 else
15241 linkage = get_identifier (TREE_STRING_POINTER (linkage));
15242
15243 /* We're now using the new linkage. */
15244 push_lang_context (linkage);
15245
15246 /* Preserve the location of the innermost linkage specification,
15247 tracking the locations of nested specifications via a local. */
15248 location_t saved_location
15249 = parser->innermost_linkage_specification_location;
15250 /* Construct a location ranging from the start of the "extern" to
15251 the end of the string-literal, with the caret at the start, e.g.:
15252 extern "C" {
15253 ^~~~~~~~~~
15254 */
15255 parser->innermost_linkage_specification_location
15256 = make_location (extern_token->location,
15257 extern_token->location,
15258 get_finish (string_token->location));
15259
15260 /* If the next token is a `{', then we're using the first
15261 production. */
15262 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15263 {
15264 cp_ensure_no_omp_declare_simd (parser);
15265 cp_ensure_no_oacc_routine (parser);
15266
15267 /* Consume the `{' token. */
15268 matching_braces braces;
15269 braces.consume_open (parser);
15270 /* Parse the declarations. */
15271 cp_parser_declaration_seq_opt (parser);
15272 /* Look for the closing `}'. */
15273 braces.require_close (parser);
15274 }
15275 /* Otherwise, there's just one declaration. */
15276 else
15277 {
15278 bool saved_in_unbraced_linkage_specification_p;
15279
15280 saved_in_unbraced_linkage_specification_p
15281 = parser->in_unbraced_linkage_specification_p;
15282 parser->in_unbraced_linkage_specification_p = true;
15283 cp_parser_declaration (parser, prefix_attr);
15284 parser->in_unbraced_linkage_specification_p
15285 = saved_in_unbraced_linkage_specification_p;
15286 }
15287
15288 /* We're done with the linkage-specification. */
15289 pop_lang_context ();
15290
15291 /* Restore location of parent linkage specification, if any. */
15292 parser->innermost_linkage_specification_location = saved_location;
15293 }
15294
15295 /* Parse a static_assert-declaration.
15296
15297 static_assert-declaration:
15298 static_assert ( constant-expression , string-literal ) ;
15299 static_assert ( constant-expression ) ; (C++17)
15300
15301 If MEMBER_P, this static_assert is a class member. */
15302
15303 static void
15304 cp_parser_static_assert(cp_parser *parser, bool member_p)
15305 {
15306 cp_expr condition;
15307 location_t token_loc;
15308 tree message;
15309 bool dummy;
15310
15311 /* Peek at the `static_assert' token so we can keep track of exactly
15312 where the static assertion started. */
15313 token_loc = cp_lexer_peek_token (parser->lexer)->location;
15314
15315 /* Look for the `static_assert' keyword. */
15316 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
15317 RT_STATIC_ASSERT))
15318 return;
15319
15320 /* We know we are in a static assertion; commit to any tentative
15321 parse. */
15322 if (cp_parser_parsing_tentatively (parser))
15323 cp_parser_commit_to_tentative_parse (parser);
15324
15325 /* Parse the `(' starting the static assertion condition. */
15326 matching_parens parens;
15327 parens.require_open (parser);
15328
15329 /* Parse the constant-expression. Allow a non-constant expression
15330 here in order to give better diagnostics in finish_static_assert. */
15331 condition =
15332 cp_parser_constant_expression (parser,
15333 /*allow_non_constant_p=*/true,
15334 /*non_constant_p=*/&dummy);
15335
15336 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
15337 {
15338 if (cxx_dialect < cxx17)
15339 pedwarn (input_location, OPT_Wpedantic,
15340 "%<static_assert%> without a message "
15341 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
15342 /* Eat the ')' */
15343 cp_lexer_consume_token (parser->lexer);
15344 message = build_string (1, "");
15345 TREE_TYPE (message) = char_array_type_node;
15346 fix_string_type (message);
15347 }
15348 else
15349 {
15350 /* Parse the separating `,'. */
15351 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
15352
15353 /* Parse the string-literal message. */
15354 message = cp_parser_string_literal (parser,
15355 /*translate=*/false,
15356 /*wide_ok=*/true);
15357
15358 /* A `)' completes the static assertion. */
15359 if (!parens.require_close (parser))
15360 cp_parser_skip_to_closing_parenthesis (parser,
15361 /*recovering=*/true,
15362 /*or_comma=*/false,
15363 /*consume_paren=*/true);
15364 }
15365
15366 /* A semicolon terminates the declaration. */
15367 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15368
15369 /* Get the location for the static assertion. Use that of the
15370 condition if available, otherwise, use that of the "static_assert"
15371 token. */
15372 location_t assert_loc = condition.get_location ();
15373 if (assert_loc == UNKNOWN_LOCATION)
15374 assert_loc = token_loc;
15375
15376 /* Complete the static assertion, which may mean either processing
15377 the static assert now or saving it for template instantiation. */
15378 finish_static_assert (condition, message, assert_loc, member_p,
15379 /*show_expr_p=*/false);
15380 }
15381
15382 /* Parse the expression in decltype ( expression ). */
15383
15384 static tree
15385 cp_parser_decltype_expr (cp_parser *parser,
15386 bool &id_expression_or_member_access_p)
15387 {
15388 cp_token *id_expr_start_token;
15389 tree expr;
15390
15391 /* First, try parsing an id-expression. */
15392 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
15393 cp_parser_parse_tentatively (parser);
15394 expr = cp_parser_id_expression (parser,
15395 /*template_keyword_p=*/false,
15396 /*check_dependency_p=*/true,
15397 /*template_p=*/NULL,
15398 /*declarator_p=*/false,
15399 /*optional_p=*/false);
15400
15401 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
15402 {
15403 bool non_integral_constant_expression_p = false;
15404 tree id_expression = expr;
15405 cp_id_kind idk;
15406 const char *error_msg;
15407
15408 if (identifier_p (expr))
15409 /* Lookup the name we got back from the id-expression. */
15410 expr = cp_parser_lookup_name_simple (parser, expr,
15411 id_expr_start_token->location);
15412
15413 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
15414 /* A template without args is not a complete id-expression. */
15415 expr = error_mark_node;
15416
15417 if (expr
15418 && expr != error_mark_node
15419 && TREE_CODE (expr) != TYPE_DECL
15420 && (TREE_CODE (expr) != BIT_NOT_EXPR
15421 || !TYPE_P (TREE_OPERAND (expr, 0)))
15422 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
15423 {
15424 /* Complete lookup of the id-expression. */
15425 expr = (finish_id_expression
15426 (id_expression, expr, parser->scope, &idk,
15427 /*integral_constant_expression_p=*/false,
15428 /*allow_non_integral_constant_expression_p=*/true,
15429 &non_integral_constant_expression_p,
15430 /*template_p=*/false,
15431 /*done=*/true,
15432 /*address_p=*/false,
15433 /*template_arg_p=*/false,
15434 &error_msg,
15435 id_expr_start_token->location));
15436
15437 if (expr == error_mark_node)
15438 /* We found an id-expression, but it was something that we
15439 should not have found. This is an error, not something
15440 we can recover from, so note that we found an
15441 id-expression and we'll recover as gracefully as
15442 possible. */
15443 id_expression_or_member_access_p = true;
15444 }
15445
15446 if (expr
15447 && expr != error_mark_node
15448 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
15449 /* We have an id-expression. */
15450 id_expression_or_member_access_p = true;
15451 }
15452
15453 if (!id_expression_or_member_access_p)
15454 {
15455 /* Abort the id-expression parse. */
15456 cp_parser_abort_tentative_parse (parser);
15457
15458 /* Parsing tentatively, again. */
15459 cp_parser_parse_tentatively (parser);
15460
15461 /* Parse a class member access. */
15462 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
15463 /*cast_p=*/false, /*decltype*/true,
15464 /*member_access_only_p=*/true, NULL);
15465
15466 if (expr
15467 && expr != error_mark_node
15468 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
15469 /* We have an id-expression. */
15470 id_expression_or_member_access_p = true;
15471 }
15472
15473 if (id_expression_or_member_access_p)
15474 /* We have parsed the complete id-expression or member access. */
15475 cp_parser_parse_definitely (parser);
15476 else
15477 {
15478 /* Abort our attempt to parse an id-expression or member access
15479 expression. */
15480 cp_parser_abort_tentative_parse (parser);
15481
15482 /* Parse a full expression. */
15483 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
15484 /*decltype_p=*/true);
15485 }
15486
15487 return expr;
15488 }
15489
15490 /* Parse a `decltype' type. Returns the type.
15491
15492 decltype-specifier:
15493 decltype ( expression )
15494 C++14:
15495 decltype ( auto ) */
15496
15497 static tree
15498 cp_parser_decltype (cp_parser *parser)
15499 {
15500 bool id_expression_or_member_access_p = false;
15501 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
15502
15503 if (start_token->type == CPP_DECLTYPE)
15504 {
15505 /* Already parsed. */
15506 cp_lexer_consume_token (parser->lexer);
15507 return saved_checks_value (start_token->u.tree_check_value);
15508 }
15509
15510 /* Look for the `decltype' token. */
15511 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
15512 return error_mark_node;
15513
15514 /* Parse the opening `('. */
15515 matching_parens parens;
15516 if (!parens.require_open (parser))
15517 return error_mark_node;
15518
15519 /* Since we're going to preserve any side-effects from this parse, set up a
15520 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15521 in the expression. */
15522 tentative_firewall firewall (parser);
15523
15524 /* If in_declarator_p, a reparse as an expression might succeed (60361).
15525 Otherwise, commit now for better diagnostics. */
15526 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
15527 && !parser->in_declarator_p)
15528 cp_parser_commit_to_topmost_tentative_parse (parser);
15529
15530 push_deferring_access_checks (dk_deferred);
15531
15532 tree expr = NULL_TREE;
15533
15534 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)
15535 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
15536 {
15537 /* decltype (auto) */
15538 cp_lexer_consume_token (parser->lexer);
15539 if (cxx_dialect < cxx14)
15540 {
15541 error_at (start_token->location,
15542 "%<decltype(auto)%> type specifier only available with "
15543 "%<-std=c++14%> or %<-std=gnu++14%>");
15544 expr = error_mark_node;
15545 }
15546 }
15547 else
15548 {
15549 /* decltype (expression) */
15550
15551 /* Types cannot be defined in a `decltype' expression. Save away the
15552 old message and set the new one. */
15553 const char *saved_message = parser->type_definition_forbidden_message;
15554 parser->type_definition_forbidden_message
15555 = G_("types may not be defined in %<decltype%> expressions");
15556
15557 /* The restrictions on constant-expressions do not apply inside
15558 decltype expressions. */
15559 bool saved_integral_constant_expression_p
15560 = parser->integral_constant_expression_p;
15561 bool saved_non_integral_constant_expression_p
15562 = parser->non_integral_constant_expression_p;
15563 parser->integral_constant_expression_p = false;
15564
15565 /* Within a parenthesized expression, a `>' token is always
15566 the greater-than operator. */
15567 bool saved_greater_than_is_operator_p
15568 = parser->greater_than_is_operator_p;
15569 parser->greater_than_is_operator_p = true;
15570
15571 /* Do not actually evaluate the expression. */
15572 ++cp_unevaluated_operand;
15573
15574 /* Do not warn about problems with the expression. */
15575 ++c_inhibit_evaluation_warnings;
15576
15577 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
15578 STRIP_ANY_LOCATION_WRAPPER (expr);
15579
15580 /* Go back to evaluating expressions. */
15581 --cp_unevaluated_operand;
15582 --c_inhibit_evaluation_warnings;
15583
15584 /* The `>' token might be the end of a template-id or
15585 template-parameter-list now. */
15586 parser->greater_than_is_operator_p
15587 = saved_greater_than_is_operator_p;
15588
15589 /* Restore the old message and the integral constant expression
15590 flags. */
15591 parser->type_definition_forbidden_message = saved_message;
15592 parser->integral_constant_expression_p
15593 = saved_integral_constant_expression_p;
15594 parser->non_integral_constant_expression_p
15595 = saved_non_integral_constant_expression_p;
15596 }
15597
15598 /* Parse to the closing `)'. */
15599 if (expr == error_mark_node || !parens.require_close (parser))
15600 {
15601 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15602 /*consume_paren=*/true);
15603 expr = error_mark_node;
15604 }
15605
15606 /* If we got a parse error while tentative, bail out now. */
15607 if (cp_parser_error_occurred (parser))
15608 {
15609 pop_deferring_access_checks ();
15610 return error_mark_node;
15611 }
15612
15613 if (!expr)
15614 /* Build auto. */
15615 expr = make_decltype_auto ();
15616 else
15617 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
15618 tf_warning_or_error);
15619
15620 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
15621 it again. */
15622 start_token->type = CPP_DECLTYPE;
15623 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15624 start_token->tree_check_p = true;
15625 start_token->u.tree_check_value->value = expr;
15626 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
15627 start_token->keyword = RID_MAX;
15628
15629 location_t loc = start_token->location;
15630 loc = make_location (loc, loc, parser->lexer);
15631 start_token->location = loc;
15632
15633 cp_lexer_purge_tokens_after (parser->lexer, start_token);
15634
15635 pop_to_parent_deferring_access_checks ();
15636
15637 return expr;
15638 }
15639
15640 /* Special member functions [gram.special] */
15641
15642 /* Parse a conversion-function-id.
15643
15644 conversion-function-id:
15645 operator conversion-type-id
15646
15647 Returns an IDENTIFIER_NODE representing the operator. */
15648
15649 static tree
15650 cp_parser_conversion_function_id (cp_parser* parser)
15651 {
15652 tree type;
15653 tree saved_scope;
15654 tree saved_qualifying_scope;
15655 tree saved_object_scope;
15656 tree pushed_scope = NULL_TREE;
15657
15658 /* Look for the `operator' token. */
15659 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15660 return error_mark_node;
15661 /* When we parse the conversion-type-id, the current scope will be
15662 reset. However, we need that information in able to look up the
15663 conversion function later, so we save it here. */
15664 saved_scope = parser->scope;
15665 saved_qualifying_scope = parser->qualifying_scope;
15666 saved_object_scope = parser->object_scope;
15667 /* We must enter the scope of the class so that the names of
15668 entities declared within the class are available in the
15669 conversion-type-id. For example, consider:
15670
15671 struct S {
15672 typedef int I;
15673 operator I();
15674 };
15675
15676 S::operator I() { ... }
15677
15678 In order to see that `I' is a type-name in the definition, we
15679 must be in the scope of `S'. */
15680 if (saved_scope)
15681 pushed_scope = push_scope (saved_scope);
15682 /* Parse the conversion-type-id. */
15683 type = cp_parser_conversion_type_id (parser);
15684 /* Leave the scope of the class, if any. */
15685 if (pushed_scope)
15686 pop_scope (pushed_scope);
15687 /* Restore the saved scope. */
15688 parser->scope = saved_scope;
15689 parser->qualifying_scope = saved_qualifying_scope;
15690 parser->object_scope = saved_object_scope;
15691 /* If the TYPE is invalid, indicate failure. */
15692 if (type == error_mark_node)
15693 return error_mark_node;
15694 return make_conv_op_name (type);
15695 }
15696
15697 /* Parse a conversion-type-id:
15698
15699 conversion-type-id:
15700 type-specifier-seq conversion-declarator [opt]
15701
15702 Returns the TYPE specified. */
15703
15704 static tree
15705 cp_parser_conversion_type_id (cp_parser* parser)
15706 {
15707 tree attributes;
15708 cp_decl_specifier_seq type_specifiers;
15709 cp_declarator *declarator;
15710 tree type_specified;
15711 const char *saved_message;
15712
15713 /* Parse the attributes. */
15714 attributes = cp_parser_attributes_opt (parser);
15715
15716 saved_message = parser->type_definition_forbidden_message;
15717 parser->type_definition_forbidden_message
15718 = G_("types may not be defined in a conversion-type-id");
15719
15720 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
15721 optional in conversion-type-id. */
15722 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15723 /*is_declaration=*/false,
15724 /*is_trailing_return=*/false,
15725 &type_specifiers);
15726
15727 parser->type_definition_forbidden_message = saved_message;
15728
15729 /* If that didn't work, stop. */
15730 if (type_specifiers.type == error_mark_node)
15731 return error_mark_node;
15732 /* Parse the conversion-declarator. */
15733 declarator = cp_parser_conversion_declarator_opt (parser);
15734
15735 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
15736 /*initialized=*/0, &attributes);
15737 if (attributes)
15738 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
15739
15740 /* Don't give this error when parsing tentatively. This happens to
15741 work because we always parse this definitively once. */
15742 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
15743 && type_uses_auto (type_specified))
15744 {
15745 if (cxx_dialect < cxx14)
15746 {
15747 error ("invalid use of %<auto%> in conversion operator");
15748 return error_mark_node;
15749 }
15750 else if (template_parm_scope_p ())
15751 warning (0, "use of %<auto%> in member template "
15752 "conversion operator can never be deduced");
15753 }
15754
15755 return type_specified;
15756 }
15757
15758 /* Parse an (optional) conversion-declarator.
15759
15760 conversion-declarator:
15761 ptr-operator conversion-declarator [opt]
15762
15763 */
15764
15765 static cp_declarator *
15766 cp_parser_conversion_declarator_opt (cp_parser* parser)
15767 {
15768 enum tree_code code;
15769 tree class_type, std_attributes = NULL_TREE;
15770 cp_cv_quals cv_quals;
15771
15772 /* We don't know if there's a ptr-operator next, or not. */
15773 cp_parser_parse_tentatively (parser);
15774 /* Try the ptr-operator. */
15775 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
15776 &std_attributes);
15777 /* If it worked, look for more conversion-declarators. */
15778 if (cp_parser_parse_definitely (parser))
15779 {
15780 cp_declarator *declarator;
15781
15782 /* Parse another optional declarator. */
15783 declarator = cp_parser_conversion_declarator_opt (parser);
15784
15785 declarator = cp_parser_make_indirect_declarator
15786 (code, class_type, cv_quals, declarator, std_attributes);
15787
15788 return declarator;
15789 }
15790
15791 return NULL;
15792 }
15793
15794 /* Parse an (optional) ctor-initializer.
15795
15796 ctor-initializer:
15797 : mem-initializer-list */
15798
15799 static void
15800 cp_parser_ctor_initializer_opt (cp_parser* parser)
15801 {
15802 /* If the next token is not a `:', then there is no
15803 ctor-initializer. */
15804 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
15805 {
15806 /* Do default initialization of any bases and members. */
15807 if (DECL_CONSTRUCTOR_P (current_function_decl))
15808 finish_mem_initializers (NULL_TREE);
15809 return;
15810 }
15811
15812 /* Consume the `:' token. */
15813 cp_lexer_consume_token (parser->lexer);
15814 /* And the mem-initializer-list. */
15815 cp_parser_mem_initializer_list (parser);
15816 }
15817
15818 /* Parse a mem-initializer-list.
15819
15820 mem-initializer-list:
15821 mem-initializer ... [opt]
15822 mem-initializer ... [opt] , mem-initializer-list */
15823
15824 static void
15825 cp_parser_mem_initializer_list (cp_parser* parser)
15826 {
15827 tree mem_initializer_list = NULL_TREE;
15828 tree target_ctor = error_mark_node;
15829 cp_token *token = cp_lexer_peek_token (parser->lexer);
15830
15831 /* Let the semantic analysis code know that we are starting the
15832 mem-initializer-list. */
15833 if (!DECL_CONSTRUCTOR_P (current_function_decl))
15834 error_at (token->location,
15835 "only constructors take member initializers");
15836
15837 /* Loop through the list. */
15838 while (true)
15839 {
15840 tree mem_initializer;
15841
15842 token = cp_lexer_peek_token (parser->lexer);
15843 /* Parse the mem-initializer. */
15844 mem_initializer = cp_parser_mem_initializer (parser);
15845 /* If the next token is a `...', we're expanding member initializers. */
15846 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15847 if (ellipsis
15848 || (mem_initializer != error_mark_node
15849 && check_for_bare_parameter_packs (TREE_PURPOSE
15850 (mem_initializer))))
15851 {
15852 /* Consume the `...'. */
15853 if (ellipsis)
15854 cp_lexer_consume_token (parser->lexer);
15855
15856 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
15857 can be expanded but members cannot. */
15858 if (mem_initializer != error_mark_node
15859 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
15860 {
15861 error_at (token->location,
15862 "cannot expand initializer for member %qD",
15863 TREE_PURPOSE (mem_initializer));
15864 mem_initializer = error_mark_node;
15865 }
15866
15867 /* Construct the pack expansion type. */
15868 if (mem_initializer != error_mark_node)
15869 mem_initializer = make_pack_expansion (mem_initializer);
15870 }
15871 if (target_ctor != error_mark_node
15872 && mem_initializer != error_mark_node)
15873 {
15874 error ("mem-initializer for %qD follows constructor delegation",
15875 TREE_PURPOSE (mem_initializer));
15876 mem_initializer = error_mark_node;
15877 }
15878 /* Look for a target constructor. */
15879 if (mem_initializer != error_mark_node
15880 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
15881 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
15882 {
15883 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
15884 if (mem_initializer_list)
15885 {
15886 error ("constructor delegation follows mem-initializer for %qD",
15887 TREE_PURPOSE (mem_initializer_list));
15888 mem_initializer = error_mark_node;
15889 }
15890 target_ctor = mem_initializer;
15891 }
15892 /* Add it to the list, unless it was erroneous. */
15893 if (mem_initializer != error_mark_node)
15894 {
15895 TREE_CHAIN (mem_initializer) = mem_initializer_list;
15896 mem_initializer_list = mem_initializer;
15897 }
15898 /* If the next token is not a `,', we're done. */
15899 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15900 break;
15901 /* Consume the `,' token. */
15902 cp_lexer_consume_token (parser->lexer);
15903 }
15904
15905 /* Perform semantic analysis. */
15906 if (DECL_CONSTRUCTOR_P (current_function_decl))
15907 finish_mem_initializers (mem_initializer_list);
15908 }
15909
15910 /* Parse a mem-initializer.
15911
15912 mem-initializer:
15913 mem-initializer-id ( expression-list [opt] )
15914 mem-initializer-id braced-init-list
15915
15916 GNU extension:
15917
15918 mem-initializer:
15919 ( expression-list [opt] )
15920
15921 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
15922 class) or FIELD_DECL (for a non-static data member) to initialize;
15923 the TREE_VALUE is the expression-list. An empty initialization
15924 list is represented by void_list_node. */
15925
15926 static tree
15927 cp_parser_mem_initializer (cp_parser* parser)
15928 {
15929 tree mem_initializer_id;
15930 tree expression_list;
15931 tree member;
15932 cp_token *token = cp_lexer_peek_token (parser->lexer);
15933
15934 /* Find out what is being initialized. */
15935 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15936 {
15937 permerror (token->location,
15938 "anachronistic old-style base class initializer");
15939 mem_initializer_id = NULL_TREE;
15940 }
15941 else
15942 {
15943 mem_initializer_id = cp_parser_mem_initializer_id (parser);
15944 if (mem_initializer_id == error_mark_node)
15945 return mem_initializer_id;
15946 }
15947 member = expand_member_init (mem_initializer_id);
15948 if (member && !DECL_P (member))
15949 in_base_initializer = 1;
15950
15951 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15952 {
15953 bool expr_non_constant_p;
15954 cp_lexer_set_source_position (parser->lexer);
15955 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15956 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
15957 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
15958 expression_list = build_tree_list (NULL_TREE, expression_list);
15959 }
15960 else
15961 {
15962 vec<tree, va_gc> *vec;
15963 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15964 /*cast_p=*/false,
15965 /*allow_expansion_p=*/true,
15966 /*non_constant_p=*/NULL,
15967 /*close_paren_loc=*/NULL,
15968 /*wrap_locations_p=*/true);
15969 if (vec == NULL)
15970 return error_mark_node;
15971 expression_list = build_tree_list_vec (vec);
15972 release_tree_vector (vec);
15973 }
15974
15975 if (expression_list == error_mark_node)
15976 return error_mark_node;
15977 if (!expression_list)
15978 expression_list = void_type_node;
15979
15980 in_base_initializer = 0;
15981
15982 if (!member)
15983 return error_mark_node;
15984 tree node = build_tree_list (member, expression_list);
15985
15986 /* We can't attach the source location of this initializer directly to
15987 the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
15988 within the TREE_TYPE of the list node. */
15989 location_t loc
15990 = make_location (token->location, token->location, parser->lexer);
15991 tree dummy = build0 (EMPTY_CLASS_EXPR, NULL_TREE);
15992 SET_EXPR_LOCATION (dummy, loc);
15993 TREE_TYPE (node) = dummy;
15994
15995 return node;
15996 }
15997
15998 /* Parse a mem-initializer-id.
15999
16000 mem-initializer-id:
16001 :: [opt] nested-name-specifier [opt] class-name
16002 decltype-specifier (C++11)
16003 identifier
16004
16005 Returns a TYPE indicating the class to be initialized for the first
16006 production (and the second in C++11). Returns an IDENTIFIER_NODE
16007 indicating the data member to be initialized for the last production. */
16008
16009 static tree
16010 cp_parser_mem_initializer_id (cp_parser* parser)
16011 {
16012 bool global_scope_p;
16013 bool nested_name_specifier_p;
16014 bool template_p = false;
16015 tree id;
16016
16017 cp_token *token = cp_lexer_peek_token (parser->lexer);
16018
16019 /* `typename' is not allowed in this context ([temp.res]). */
16020 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16021 {
16022 error_at (token->location,
16023 "keyword %<typename%> not allowed in this context (a qualified "
16024 "member initializer is implicitly a type)");
16025 cp_lexer_consume_token (parser->lexer);
16026 }
16027 /* Look for the optional `::' operator. */
16028 global_scope_p
16029 = (cp_parser_global_scope_opt (parser,
16030 /*current_scope_valid_p=*/false)
16031 != NULL_TREE);
16032 /* Look for the optional nested-name-specifier. The simplest way to
16033 implement:
16034
16035 [temp.res]
16036
16037 The keyword `typename' is not permitted in a base-specifier or
16038 mem-initializer; in these contexts a qualified name that
16039 depends on a template-parameter is implicitly assumed to be a
16040 type name.
16041
16042 is to assume that we have seen the `typename' keyword at this
16043 point. */
16044 nested_name_specifier_p
16045 = (cp_parser_nested_name_specifier_opt (parser,
16046 /*typename_keyword_p=*/true,
16047 /*check_dependency_p=*/true,
16048 /*type_p=*/true,
16049 /*is_declaration=*/true)
16050 != NULL_TREE);
16051 if (nested_name_specifier_p)
16052 template_p = cp_parser_optional_template_keyword (parser);
16053 /* If there is a `::' operator or a nested-name-specifier, then we
16054 are definitely looking for a class-name. */
16055 if (global_scope_p || nested_name_specifier_p)
16056 return cp_parser_class_name (parser,
16057 /*typename_keyword_p=*/true,
16058 /*template_keyword_p=*/template_p,
16059 typename_type,
16060 /*check_dependency_p=*/true,
16061 /*class_head_p=*/false,
16062 /*is_declaration=*/true);
16063 /* Otherwise, we could also be looking for an ordinary identifier. */
16064 cp_parser_parse_tentatively (parser);
16065 if (cp_lexer_next_token_is_decltype (parser->lexer))
16066 /* Try a decltype-specifier. */
16067 id = cp_parser_decltype (parser);
16068 else
16069 /* Otherwise, try a class-name. */
16070 id = cp_parser_class_name (parser,
16071 /*typename_keyword_p=*/true,
16072 /*template_keyword_p=*/false,
16073 none_type,
16074 /*check_dependency_p=*/true,
16075 /*class_head_p=*/false,
16076 /*is_declaration=*/true);
16077 /* If we found one, we're done. */
16078 if (cp_parser_parse_definitely (parser))
16079 return id;
16080 /* Otherwise, look for an ordinary identifier. */
16081 return cp_parser_identifier (parser);
16082 }
16083
16084 /* Overloading [gram.over] */
16085
16086 /* Parse an operator-function-id.
16087
16088 operator-function-id:
16089 operator operator
16090
16091 Returns an IDENTIFIER_NODE for the operator which is a
16092 human-readable spelling of the identifier, e.g., `operator +'. */
16093
16094 static cp_expr
16095 cp_parser_operator_function_id (cp_parser* parser)
16096 {
16097 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
16098 /* Look for the `operator' keyword. */
16099 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
16100 return error_mark_node;
16101 /* And then the name of the operator itself. */
16102 return cp_parser_operator (parser, start_loc);
16103 }
16104
16105 /* Return an identifier node for a user-defined literal operator.
16106 The suffix identifier is chained to the operator name identifier. */
16107
16108 tree
16109 cp_literal_operator_id (const char* name)
16110 {
16111 tree identifier;
16112 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
16113 + strlen (name) + 10);
16114 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
16115 identifier = get_identifier (buffer);
16116 XDELETEVEC (buffer);
16117
16118 return identifier;
16119 }
16120
16121 /* Parse an operator.
16122
16123 operator:
16124 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
16125 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
16126 || ++ -- , ->* -> () []
16127
16128 GNU Extensions:
16129
16130 operator:
16131 <? >? <?= >?=
16132
16133 Returns an IDENTIFIER_NODE for the operator which is a
16134 human-readable spelling of the identifier, e.g., `operator +'. */
16135
16136 static cp_expr
16137 cp_parser_operator (cp_parser* parser, location_t start_loc)
16138 {
16139 tree id = NULL_TREE;
16140 cp_token *token;
16141 bool utf8 = false;
16142
16143 /* Peek at the next token. */
16144 token = cp_lexer_peek_token (parser->lexer);
16145
16146 location_t end_loc = token->location;
16147
16148 /* Figure out which operator we have. */
16149 enum tree_code op = ERROR_MARK;
16150 bool assop = false;
16151 bool consumed = false;
16152 switch (token->type)
16153 {
16154 case CPP_KEYWORD:
16155 {
16156 /* The keyword should be either `new', `delete' or `co_await'. */
16157 if (token->keyword == RID_NEW)
16158 op = NEW_EXPR;
16159 else if (token->keyword == RID_DELETE)
16160 op = DELETE_EXPR;
16161 else if (token->keyword == RID_CO_AWAIT)
16162 op = CO_AWAIT_EXPR;
16163 else
16164 break;
16165
16166 /* Consume the `new', `delete' or co_await token. */
16167 end_loc = cp_lexer_consume_token (parser->lexer)->location;
16168
16169 /* Peek at the next token. */
16170 token = cp_lexer_peek_token (parser->lexer);
16171 /* If it's a `[' token then this is the array variant of the
16172 operator. */
16173 if (token->type == CPP_OPEN_SQUARE
16174 && op != CO_AWAIT_EXPR)
16175 {
16176 /* Consume the `[' token. */
16177 cp_lexer_consume_token (parser->lexer);
16178 /* Look for the `]' token. */
16179 if (cp_token *close_token
16180 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16181 end_loc = close_token->location;
16182 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
16183 }
16184 consumed = true;
16185 break;
16186 }
16187
16188 case CPP_PLUS:
16189 op = PLUS_EXPR;
16190 break;
16191
16192 case CPP_MINUS:
16193 op = MINUS_EXPR;
16194 break;
16195
16196 case CPP_MULT:
16197 op = MULT_EXPR;
16198 break;
16199
16200 case CPP_DIV:
16201 op = TRUNC_DIV_EXPR;
16202 break;
16203
16204 case CPP_MOD:
16205 op = TRUNC_MOD_EXPR;
16206 break;
16207
16208 case CPP_XOR:
16209 op = BIT_XOR_EXPR;
16210 break;
16211
16212 case CPP_AND:
16213 op = BIT_AND_EXPR;
16214 break;
16215
16216 case CPP_OR:
16217 op = BIT_IOR_EXPR;
16218 break;
16219
16220 case CPP_COMPL:
16221 op = BIT_NOT_EXPR;
16222 break;
16223
16224 case CPP_NOT:
16225 op = TRUTH_NOT_EXPR;
16226 break;
16227
16228 case CPP_EQ:
16229 assop = true;
16230 op = NOP_EXPR;
16231 break;
16232
16233 case CPP_LESS:
16234 op = LT_EXPR;
16235 break;
16236
16237 case CPP_GREATER:
16238 op = GT_EXPR;
16239 break;
16240
16241 case CPP_PLUS_EQ:
16242 assop = true;
16243 op = PLUS_EXPR;
16244 break;
16245
16246 case CPP_MINUS_EQ:
16247 assop = true;
16248 op = MINUS_EXPR;
16249 break;
16250
16251 case CPP_MULT_EQ:
16252 assop = true;
16253 op = MULT_EXPR;
16254 break;
16255
16256 case CPP_DIV_EQ:
16257 assop = true;
16258 op = TRUNC_DIV_EXPR;
16259 break;
16260
16261 case CPP_MOD_EQ:
16262 assop = true;
16263 op = TRUNC_MOD_EXPR;
16264 break;
16265
16266 case CPP_XOR_EQ:
16267 assop = true;
16268 op = BIT_XOR_EXPR;
16269 break;
16270
16271 case CPP_AND_EQ:
16272 assop = true;
16273 op = BIT_AND_EXPR;
16274 break;
16275
16276 case CPP_OR_EQ:
16277 assop = true;
16278 op = BIT_IOR_EXPR;
16279 break;
16280
16281 case CPP_LSHIFT:
16282 op = LSHIFT_EXPR;
16283 break;
16284
16285 case CPP_RSHIFT:
16286 op = RSHIFT_EXPR;
16287 break;
16288
16289 case CPP_LSHIFT_EQ:
16290 assop = true;
16291 op = LSHIFT_EXPR;
16292 break;
16293
16294 case CPP_RSHIFT_EQ:
16295 assop = true;
16296 op = RSHIFT_EXPR;
16297 break;
16298
16299 case CPP_EQ_EQ:
16300 op = EQ_EXPR;
16301 break;
16302
16303 case CPP_NOT_EQ:
16304 op = NE_EXPR;
16305 break;
16306
16307 case CPP_LESS_EQ:
16308 op = LE_EXPR;
16309 break;
16310
16311 case CPP_GREATER_EQ:
16312 op = GE_EXPR;
16313 break;
16314
16315 case CPP_SPACESHIP:
16316 op = SPACESHIP_EXPR;
16317 break;
16318
16319 case CPP_AND_AND:
16320 op = TRUTH_ANDIF_EXPR;
16321 break;
16322
16323 case CPP_OR_OR:
16324 op = TRUTH_ORIF_EXPR;
16325 break;
16326
16327 case CPP_PLUS_PLUS:
16328 op = POSTINCREMENT_EXPR;
16329 break;
16330
16331 case CPP_MINUS_MINUS:
16332 op = PREDECREMENT_EXPR;
16333 break;
16334
16335 case CPP_COMMA:
16336 op = COMPOUND_EXPR;
16337 break;
16338
16339 case CPP_DEREF_STAR:
16340 op = MEMBER_REF;
16341 break;
16342
16343 case CPP_DEREF:
16344 op = COMPONENT_REF;
16345 break;
16346
16347 case CPP_QUERY:
16348 op = COND_EXPR;
16349 /* Consume the `?'. */
16350 cp_lexer_consume_token (parser->lexer);
16351 /* Look for the matching `:'. */
16352 cp_parser_require (parser, CPP_COLON, RT_COLON);
16353 consumed = true;
16354 break;
16355
16356 case CPP_OPEN_PAREN:
16357 {
16358 /* Consume the `('. */
16359 matching_parens parens;
16360 parens.consume_open (parser);
16361 /* Look for the matching `)'. */
16362 token = parens.require_close (parser);
16363 if (token)
16364 end_loc = token->location;
16365 op = CALL_EXPR;
16366 consumed = true;
16367 break;
16368 }
16369
16370 case CPP_OPEN_SQUARE:
16371 /* Consume the `['. */
16372 cp_lexer_consume_token (parser->lexer);
16373 /* Look for the matching `]'. */
16374 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
16375 if (token)
16376 end_loc = token->location;
16377 op = ARRAY_REF;
16378 consumed = true;
16379 break;
16380
16381 case CPP_UTF8STRING:
16382 case CPP_UTF8STRING_USERDEF:
16383 utf8 = true;
16384 /* FALLTHRU */
16385 case CPP_STRING:
16386 case CPP_WSTRING:
16387 case CPP_STRING16:
16388 case CPP_STRING32:
16389 case CPP_STRING_USERDEF:
16390 case CPP_WSTRING_USERDEF:
16391 case CPP_STRING16_USERDEF:
16392 case CPP_STRING32_USERDEF:
16393 {
16394 cp_expr str;
16395 tree string_tree;
16396 int sz, len;
16397
16398 if (cxx_dialect == cxx98)
16399 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
16400
16401 /* Consume the string. */
16402 str = cp_parser_string_literal (parser, /*translate=*/true,
16403 /*wide_ok=*/true, /*lookup_udlit=*/false);
16404 if (str == error_mark_node)
16405 return error_mark_node;
16406 else if (TREE_CODE (str) == USERDEF_LITERAL)
16407 {
16408 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
16409 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
16410 end_loc = str.get_location ();
16411 }
16412 else
16413 {
16414 string_tree = str;
16415 /* Look for the suffix identifier. */
16416 token = cp_lexer_peek_token (parser->lexer);
16417 if (token->type == CPP_NAME)
16418 {
16419 id = cp_parser_identifier (parser);
16420 end_loc = token->location;
16421 }
16422 else if (token->type == CPP_KEYWORD)
16423 {
16424 error ("unexpected keyword;"
16425 " remove space between quotes and suffix identifier");
16426 return error_mark_node;
16427 }
16428 else
16429 {
16430 error ("expected suffix identifier");
16431 return error_mark_node;
16432 }
16433 }
16434 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
16435 (TREE_TYPE (TREE_TYPE (string_tree))));
16436 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
16437 if (len != 0)
16438 {
16439 error ("expected empty string after %<operator%> keyword");
16440 return error_mark_node;
16441 }
16442 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
16443 != char_type_node)
16444 {
16445 error ("invalid encoding prefix in literal operator");
16446 return error_mark_node;
16447 }
16448 if (id != error_mark_node)
16449 {
16450 const char *name = IDENTIFIER_POINTER (id);
16451 id = cp_literal_operator_id (name);
16452 }
16453 /* Generate a location of the form:
16454 "" _suffix_identifier
16455 ^~~~~~~~~~~~~~~~~~~~~
16456 with caret == start at the start token, finish at the end of the
16457 suffix identifier. */
16458 location_t combined_loc
16459 = make_location (start_loc, start_loc, parser->lexer);
16460 return cp_expr (id, combined_loc);
16461 }
16462
16463 default:
16464 /* Anything else is an error. */
16465 break;
16466 }
16467
16468 /* If we have selected an identifier, we need to consume the
16469 operator token. */
16470 if (op != ERROR_MARK)
16471 {
16472 id = ovl_op_identifier (assop, op);
16473 if (!consumed)
16474 cp_lexer_consume_token (parser->lexer);
16475 }
16476 /* Otherwise, no valid operator name was present. */
16477 else
16478 {
16479 cp_parser_error (parser, "expected operator");
16480 id = error_mark_node;
16481 }
16482
16483 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
16484 return cp_expr (id, start_loc);
16485 }
16486
16487 /* Parse a template-declaration.
16488
16489 template-declaration:
16490 export [opt] template < template-parameter-list > declaration
16491
16492 If MEMBER_P is TRUE, this template-declaration occurs within a
16493 class-specifier.
16494
16495 The grammar rule given by the standard isn't correct. What
16496 is really meant is:
16497
16498 template-declaration:
16499 export [opt] template-parameter-list-seq
16500 decl-specifier-seq [opt] init-declarator [opt] ;
16501 export [opt] template-parameter-list-seq
16502 function-definition
16503
16504 template-parameter-list-seq:
16505 template-parameter-list-seq [opt]
16506 template < template-parameter-list >
16507
16508 Concept Extensions:
16509
16510 template-parameter-list-seq:
16511 template < template-parameter-list > requires-clause [opt]
16512
16513 requires-clause:
16514 requires logical-or-expression */
16515
16516 static void
16517 cp_parser_template_declaration (cp_parser* parser, bool member_p)
16518 {
16519 /* Check for `export'. */
16520 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
16521 {
16522 /* Consume the `export' token. */
16523 cp_lexer_consume_token (parser->lexer);
16524 /* Warn that this use of export is deprecated. */
16525 if (cxx_dialect < cxx11)
16526 warning (0, "keyword %<export%> not implemented, and will be ignored");
16527 else if (cxx_dialect < cxx20)
16528 warning (0, "keyword %<export%> is deprecated, and is ignored");
16529 else
16530 warning (0, "keyword %<export%> is enabled with %<-fmodules-ts%>");
16531 }
16532
16533 cp_parser_template_declaration_after_export (parser, member_p);
16534 }
16535
16536 /* Parse a template-parameter-list.
16537
16538 template-parameter-list:
16539 template-parameter
16540 template-parameter-list , template-parameter
16541
16542 Returns a TREE_LIST. Each node represents a template parameter.
16543 The nodes are connected via their TREE_CHAINs. */
16544
16545 static tree
16546 cp_parser_template_parameter_list (cp_parser* parser)
16547 {
16548 tree parameter_list = NULL_TREE;
16549
16550 /* Don't create wrapper nodes within a template-parameter-list,
16551 since we don't want to have different types based on the
16552 spelling location of constants and decls within them. */
16553 auto_suppress_location_wrappers sentinel;
16554
16555 begin_template_parm_list ();
16556
16557 /* The loop below parses the template parms. We first need to know
16558 the total number of template parms to be able to compute proper
16559 canonical types of each dependent type. So after the loop, when
16560 we know the total number of template parms,
16561 end_template_parm_list computes the proper canonical types and
16562 fixes up the dependent types accordingly. */
16563 while (true)
16564 {
16565 tree parameter;
16566 bool is_non_type;
16567 bool is_parameter_pack;
16568 location_t parm_loc;
16569
16570 /* Parse the template-parameter. */
16571 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
16572 parameter = cp_parser_template_parameter (parser,
16573 &is_non_type,
16574 &is_parameter_pack);
16575 /* Add it to the list. */
16576 if (parameter != error_mark_node)
16577 parameter_list = process_template_parm (parameter_list,
16578 parm_loc,
16579 parameter,
16580 is_non_type,
16581 is_parameter_pack);
16582 else
16583 {
16584 tree err_parm = build_tree_list (parameter, parameter);
16585 parameter_list = chainon (parameter_list, err_parm);
16586 }
16587
16588 /* If the next token is not a `,', we're done. */
16589 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16590 break;
16591 /* Otherwise, consume the `,' token. */
16592 cp_lexer_consume_token (parser->lexer);
16593 }
16594
16595 return end_template_parm_list (parameter_list);
16596 }
16597
16598 /* Parse a introduction-list.
16599
16600 introduction-list:
16601 introduced-parameter
16602 introduction-list , introduced-parameter
16603
16604 introduced-parameter:
16605 ...[opt] identifier
16606
16607 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
16608 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
16609 WILDCARD_DECL will also have DECL_NAME set and token location in
16610 DECL_SOURCE_LOCATION. */
16611
16612 static tree
16613 cp_parser_introduction_list (cp_parser *parser)
16614 {
16615 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
16616
16617 while (true)
16618 {
16619 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
16620 if (is_pack)
16621 cp_lexer_consume_token (parser->lexer);
16622
16623 tree identifier = cp_parser_identifier (parser);
16624 if (identifier == error_mark_node)
16625 break;
16626
16627 /* Build placeholder. */
16628 tree parm = build_nt (WILDCARD_DECL);
16629 DECL_SOURCE_LOCATION (parm)
16630 = cp_lexer_peek_token (parser->lexer)->location;
16631 DECL_NAME (parm) = identifier;
16632 WILDCARD_PACK_P (parm) = is_pack;
16633 vec_safe_push (introduction_vec, parm);
16634
16635 /* If the next token is not a `,', we're done. */
16636 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16637 break;
16638 /* Otherwise, consume the `,' token. */
16639 cp_lexer_consume_token (parser->lexer);
16640 }
16641
16642 /* Convert the vec into a TREE_VEC. */
16643 tree introduction_list = make_tree_vec (introduction_vec->length ());
16644 unsigned int n;
16645 tree parm;
16646 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
16647 TREE_VEC_ELT (introduction_list, n) = parm;
16648
16649 release_tree_vector (introduction_vec);
16650 return introduction_list;
16651 }
16652
16653 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
16654 is an abstract declarator. */
16655
16656 static inline cp_declarator*
16657 get_id_declarator (cp_declarator *declarator)
16658 {
16659 cp_declarator *d = declarator;
16660 while (d && d->kind != cdk_id)
16661 d = d->declarator;
16662 return d;
16663 }
16664
16665 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
16666 is an abstract declarator. */
16667
16668 static inline tree
16669 get_unqualified_id (cp_declarator *declarator)
16670 {
16671 declarator = get_id_declarator (declarator);
16672 if (declarator)
16673 return declarator->u.id.unqualified_name;
16674 else
16675 return NULL_TREE;
16676 }
16677
16678 /* Returns true if TYPE would declare a constrained constrained-parameter. */
16679
16680 static inline bool
16681 is_constrained_parameter (tree type)
16682 {
16683 return (type
16684 && TREE_CODE (type) == TYPE_DECL
16685 && CONSTRAINED_PARM_CONCEPT (type)
16686 && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
16687 }
16688
16689 /* Returns true if PARM declares a constrained-parameter. */
16690
16691 static inline bool
16692 is_constrained_parameter (cp_parameter_declarator *parm)
16693 {
16694 return is_constrained_parameter (parm->decl_specifiers.type);
16695 }
16696
16697 /* Check that the type parameter is only a declarator-id, and that its
16698 type is not cv-qualified. */
16699
16700 bool
16701 cp_parser_check_constrained_type_parm (cp_parser *parser,
16702 cp_parameter_declarator *parm)
16703 {
16704 if (!parm->declarator)
16705 return true;
16706
16707 if (parm->declarator->kind != cdk_id)
16708 {
16709 cp_parser_error (parser, "invalid constrained type parameter");
16710 return false;
16711 }
16712
16713 /* Don't allow cv-qualified type parameters. */
16714 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
16715 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
16716 {
16717 cp_parser_error (parser, "cv-qualified type parameter");
16718 return false;
16719 }
16720
16721 return true;
16722 }
16723
16724 /* Finish parsing/processing a template type parameter and checking
16725 various restrictions. */
16726
16727 static inline tree
16728 cp_parser_constrained_type_template_parm (cp_parser *parser,
16729 tree id,
16730 cp_parameter_declarator* parmdecl)
16731 {
16732 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
16733 return finish_template_type_parm (class_type_node, id);
16734 else
16735 return error_mark_node;
16736 }
16737
16738 static tree
16739 finish_constrained_template_template_parm (tree proto, tree id)
16740 {
16741 /* FIXME: This should probably be copied, and we may need to adjust
16742 the template parameter depths. */
16743 tree saved_parms = current_template_parms;
16744 begin_template_parm_list ();
16745 current_template_parms = DECL_TEMPLATE_PARMS (proto);
16746 end_template_parm_list ();
16747
16748 tree parm = finish_template_template_parm (class_type_node, id);
16749 current_template_parms = saved_parms;
16750
16751 return parm;
16752 }
16753
16754 /* Finish parsing/processing a template template parameter by borrowing
16755 the template parameter list from the prototype parameter. */
16756
16757 static tree
16758 cp_parser_constrained_template_template_parm (cp_parser *parser,
16759 tree proto,
16760 tree id,
16761 cp_parameter_declarator *parmdecl)
16762 {
16763 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
16764 return error_mark_node;
16765 return finish_constrained_template_template_parm (proto, id);
16766 }
16767
16768 /* Create a new non-type template parameter from the given PARM
16769 declarator. */
16770
16771 static tree
16772 cp_parser_constrained_non_type_template_parm (bool *is_non_type,
16773 cp_parameter_declarator *parm)
16774 {
16775 *is_non_type = true;
16776 cp_declarator *decl = parm->declarator;
16777 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
16778 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
16779 return grokdeclarator (decl, specs, TPARM, 0, NULL);
16780 }
16781
16782 /* Build a constrained template parameter based on the PARMDECL
16783 declarator. The type of PARMDECL is the constrained type, which
16784 refers to the prototype template parameter that ultimately
16785 specifies the type of the declared parameter. */
16786
16787 static tree
16788 finish_constrained_parameter (cp_parser *parser,
16789 cp_parameter_declarator *parmdecl,
16790 bool *is_non_type)
16791 {
16792 tree decl = parmdecl->decl_specifiers.type;
16793 tree id = get_unqualified_id (parmdecl->declarator);
16794 tree def = parmdecl->default_argument;
16795 tree proto = DECL_INITIAL (decl);
16796
16797 /* Build the parameter. Return an error if the declarator was invalid. */
16798 tree parm;
16799 if (TREE_CODE (proto) == TYPE_DECL)
16800 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
16801 else if (TREE_CODE (proto) == TEMPLATE_DECL)
16802 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
16803 parmdecl);
16804 else
16805 parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
16806 if (parm == error_mark_node)
16807 return error_mark_node;
16808
16809 /* Finish the parameter decl and create a node attaching the
16810 default argument and constraint. */
16811 parm = build_tree_list (def, parm);
16812 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
16813
16814 return parm;
16815 }
16816
16817 /* Returns true if the parsed type actually represents the declaration
16818 of a type template-parameter. */
16819
16820 static bool
16821 declares_constrained_type_template_parameter (tree type)
16822 {
16823 return (is_constrained_parameter (type)
16824 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
16825 }
16826
16827 /* Returns true if the parsed type actually represents the declaration of
16828 a template template-parameter. */
16829
16830 static bool
16831 declares_constrained_template_template_parameter (tree type)
16832 {
16833 return (is_constrained_parameter (type)
16834 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
16835 }
16836
16837 /* Parse a default argument for a type template-parameter.
16838 Note that diagnostics are handled in cp_parser_template_parameter. */
16839
16840 static tree
16841 cp_parser_default_type_template_argument (cp_parser *parser)
16842 {
16843 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
16844
16845 /* Consume the `=' token. */
16846 cp_lexer_consume_token (parser->lexer);
16847
16848 cp_token *token = cp_lexer_peek_token (parser->lexer);
16849
16850 /* Parse the default-argument. */
16851 push_deferring_access_checks (dk_no_deferred);
16852 tree default_argument = cp_parser_type_id (parser,
16853 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16854 NULL);
16855 pop_deferring_access_checks ();
16856
16857 if (flag_concepts && type_uses_auto (default_argument))
16858 {
16859 error_at (token->location,
16860 "invalid use of %<auto%> in default template argument");
16861 return error_mark_node;
16862 }
16863
16864 return default_argument;
16865 }
16866
16867 /* Parse a default argument for a template template-parameter. */
16868
16869 static tree
16870 cp_parser_default_template_template_argument (cp_parser *parser)
16871 {
16872 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
16873
16874 bool is_template;
16875
16876 /* Consume the `='. */
16877 cp_lexer_consume_token (parser->lexer);
16878 /* Parse the id-expression. */
16879 push_deferring_access_checks (dk_no_deferred);
16880 /* save token before parsing the id-expression, for error
16881 reporting */
16882 const cp_token* token = cp_lexer_peek_token (parser->lexer);
16883 tree default_argument
16884 = cp_parser_id_expression (parser,
16885 /*template_keyword_p=*/false,
16886 /*check_dependency_p=*/true,
16887 /*template_p=*/&is_template,
16888 /*declarator_p=*/false,
16889 /*optional_p=*/false);
16890 if (TREE_CODE (default_argument) == TYPE_DECL)
16891 /* If the id-expression was a template-id that refers to
16892 a template-class, we already have the declaration here,
16893 so no further lookup is needed. */
16894 ;
16895 else
16896 /* Look up the name. */
16897 default_argument
16898 = cp_parser_lookup_name (parser, default_argument,
16899 none_type,
16900 /*is_template=*/is_template,
16901 /*is_namespace=*/false,
16902 /*check_dependency=*/true,
16903 /*ambiguous_decls=*/NULL,
16904 token->location);
16905 /* See if the default argument is valid. */
16906 default_argument = check_template_template_default_arg (default_argument);
16907 pop_deferring_access_checks ();
16908 return default_argument;
16909 }
16910
16911 /* Parse a template-parameter.
16912
16913 template-parameter:
16914 type-parameter
16915 parameter-declaration
16916
16917 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
16918 the parameter. The TREE_PURPOSE is the default value, if any.
16919 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
16920 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
16921 set to true iff this parameter is a parameter pack. */
16922
16923 static tree
16924 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
16925 bool *is_parameter_pack)
16926 {
16927 cp_token *token;
16928 cp_parameter_declarator *parameter_declarator;
16929 tree parm;
16930
16931 /* Assume it is a type parameter or a template parameter. */
16932 *is_non_type = false;
16933 /* Assume it not a parameter pack. */
16934 *is_parameter_pack = false;
16935 /* Peek at the next token. */
16936 token = cp_lexer_peek_token (parser->lexer);
16937 /* If it is `template', we have a type-parameter. */
16938 if (token->keyword == RID_TEMPLATE)
16939 return cp_parser_type_parameter (parser, is_parameter_pack);
16940 /* If it is `class' or `typename' we do not know yet whether it is a
16941 type parameter or a non-type parameter. Consider:
16942
16943 template <typename T, typename T::X X> ...
16944
16945 or:
16946
16947 template <class C, class D*> ...
16948
16949 Here, the first parameter is a type parameter, and the second is
16950 a non-type parameter. We can tell by looking at the token after
16951 the identifier -- if it is a `,', `=', or `>' then we have a type
16952 parameter. */
16953 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
16954 {
16955 /* Peek at the token after `class' or `typename'. */
16956 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16957 /* If it's an ellipsis, we have a template type parameter
16958 pack. */
16959 if (token->type == CPP_ELLIPSIS)
16960 return cp_parser_type_parameter (parser, is_parameter_pack);
16961 /* If it's an identifier, skip it. */
16962 if (token->type == CPP_NAME)
16963 token = cp_lexer_peek_nth_token (parser->lexer, 3);
16964 /* Now, see if the token looks like the end of a template
16965 parameter. */
16966 if (token->type == CPP_COMMA
16967 || token->type == CPP_EQ
16968 || token->type == CPP_GREATER)
16969 return cp_parser_type_parameter (parser, is_parameter_pack);
16970 }
16971
16972 /* Otherwise, it is a non-type parameter or a constrained parameter.
16973
16974 [temp.param]
16975
16976 When parsing a default template-argument for a non-type
16977 template-parameter, the first non-nested `>' is taken as the end
16978 of the template parameter-list rather than a greater-than
16979 operator. */
16980 parameter_declarator
16981 = cp_parser_parameter_declaration (parser,
16982 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16983 /*template_parm_p=*/true,
16984 /*parenthesized_p=*/NULL);
16985
16986 if (!parameter_declarator)
16987 return error_mark_node;
16988
16989 /* If the parameter declaration is marked as a parameter pack, set
16990 *IS_PARAMETER_PACK to notify the caller. */
16991 if (parameter_declarator->template_parameter_pack_p)
16992 *is_parameter_pack = true;
16993
16994 if (parameter_declarator->default_argument)
16995 {
16996 /* Can happen in some cases of erroneous input (c++/34892). */
16997 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16998 /* Consume the `...' for better error recovery. */
16999 cp_lexer_consume_token (parser->lexer);
17000 }
17001
17002 /* The parameter may have been constrained type parameter. */
17003 if (is_constrained_parameter (parameter_declarator))
17004 return finish_constrained_parameter (parser,
17005 parameter_declarator,
17006 is_non_type);
17007
17008 // Now we're sure that the parameter is a non-type parameter.
17009 *is_non_type = true;
17010
17011 parm = grokdeclarator (parameter_declarator->declarator,
17012 &parameter_declarator->decl_specifiers,
17013 TPARM, /*initialized=*/0,
17014 /*attrlist=*/NULL);
17015 if (parm == error_mark_node)
17016 return error_mark_node;
17017
17018 return build_tree_list (parameter_declarator->default_argument, parm);
17019 }
17020
17021 /* Parse a type-parameter.
17022
17023 type-parameter:
17024 class identifier [opt]
17025 class identifier [opt] = type-id
17026 typename identifier [opt]
17027 typename identifier [opt] = type-id
17028 template < template-parameter-list > class identifier [opt]
17029 template < template-parameter-list > class identifier [opt]
17030 = id-expression
17031
17032 GNU Extension (variadic templates):
17033
17034 type-parameter:
17035 class ... identifier [opt]
17036 typename ... identifier [opt]
17037
17038 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
17039 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
17040 the declaration of the parameter.
17041
17042 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
17043
17044 static tree
17045 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
17046 {
17047 cp_token *token;
17048 tree parameter;
17049
17050 /* Look for a keyword to tell us what kind of parameter this is. */
17051 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
17052 if (!token)
17053 return error_mark_node;
17054
17055 switch (token->keyword)
17056 {
17057 case RID_CLASS:
17058 case RID_TYPENAME:
17059 {
17060 tree identifier;
17061 tree default_argument;
17062
17063 /* If the next token is an ellipsis, we have a template
17064 argument pack. */
17065 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17066 {
17067 /* Consume the `...' token. */
17068 cp_lexer_consume_token (parser->lexer);
17069 maybe_warn_variadic_templates ();
17070
17071 *is_parameter_pack = true;
17072 }
17073
17074 /* If the next token is an identifier, then it names the
17075 parameter. */
17076 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17077 identifier = cp_parser_identifier (parser);
17078 else
17079 identifier = NULL_TREE;
17080
17081 /* Create the parameter. */
17082 parameter = finish_template_type_parm (class_type_node, identifier);
17083
17084 /* If the next token is an `=', we have a default argument. */
17085 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17086 {
17087 default_argument
17088 = cp_parser_default_type_template_argument (parser);
17089
17090 /* Template parameter packs cannot have default
17091 arguments. */
17092 if (*is_parameter_pack)
17093 {
17094 if (identifier)
17095 error_at (token->location,
17096 "template parameter pack %qD cannot have a "
17097 "default argument", identifier);
17098 else
17099 error_at (token->location,
17100 "template parameter packs cannot have "
17101 "default arguments");
17102 default_argument = NULL_TREE;
17103 }
17104 else if (check_for_bare_parameter_packs (default_argument))
17105 default_argument = error_mark_node;
17106 }
17107 else
17108 default_argument = NULL_TREE;
17109
17110 /* Create the combined representation of the parameter and the
17111 default argument. */
17112 parameter = build_tree_list (default_argument, parameter);
17113 }
17114 break;
17115
17116 case RID_TEMPLATE:
17117 {
17118 tree identifier;
17119 tree default_argument;
17120
17121 /* Look for the `<'. */
17122 cp_parser_require (parser, CPP_LESS, RT_LESS);
17123 /* Parse the template-parameter-list. */
17124 cp_parser_template_parameter_list (parser);
17125 /* Look for the `>'. */
17126 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17127
17128 /* If template requirements are present, parse them. */
17129 if (flag_concepts)
17130 {
17131 tree reqs = get_shorthand_constraints (current_template_parms);
17132 if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
17133 reqs = combine_constraint_expressions (reqs, dreqs);
17134 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
17135 }
17136
17137 /* Look for the `class' or 'typename' keywords. */
17138 cp_parser_type_parameter_key (parser);
17139 /* If the next token is an ellipsis, we have a template
17140 argument pack. */
17141 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17142 {
17143 /* Consume the `...' token. */
17144 cp_lexer_consume_token (parser->lexer);
17145 maybe_warn_variadic_templates ();
17146
17147 *is_parameter_pack = true;
17148 }
17149 /* If the next token is an `=', then there is a
17150 default-argument. If the next token is a `>', we are at
17151 the end of the parameter-list. If the next token is a `,',
17152 then we are at the end of this parameter. */
17153 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17154 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
17155 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17156 {
17157 identifier = cp_parser_identifier (parser);
17158 /* Treat invalid names as if the parameter were nameless. */
17159 if (identifier == error_mark_node)
17160 identifier = NULL_TREE;
17161 }
17162 else
17163 identifier = NULL_TREE;
17164
17165 /* Create the template parameter. */
17166 parameter = finish_template_template_parm (class_type_node,
17167 identifier);
17168
17169 /* If the next token is an `=', then there is a
17170 default-argument. */
17171 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17172 {
17173 default_argument
17174 = cp_parser_default_template_template_argument (parser);
17175
17176 /* Template parameter packs cannot have default
17177 arguments. */
17178 if (*is_parameter_pack)
17179 {
17180 if (identifier)
17181 error_at (token->location,
17182 "template parameter pack %qD cannot "
17183 "have a default argument",
17184 identifier);
17185 else
17186 error_at (token->location, "template parameter packs cannot "
17187 "have default arguments");
17188 default_argument = NULL_TREE;
17189 }
17190 }
17191 else
17192 default_argument = NULL_TREE;
17193
17194 /* Create the combined representation of the parameter and the
17195 default argument. */
17196 parameter = build_tree_list (default_argument, parameter);
17197 }
17198 break;
17199
17200 default:
17201 gcc_unreachable ();
17202 break;
17203 }
17204
17205 return parameter;
17206 }
17207
17208 /* Parse a template-id.
17209
17210 template-id:
17211 template-name < template-argument-list [opt] >
17212
17213 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
17214 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
17215 returned. Otherwise, if the template-name names a function, or set
17216 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
17217 names a class, returns a TYPE_DECL for the specialization.
17218
17219 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
17220 uninstantiated templates. */
17221
17222 static tree
17223 cp_parser_template_id (cp_parser *parser,
17224 bool template_keyword_p,
17225 bool check_dependency_p,
17226 enum tag_types tag_type,
17227 bool is_declaration)
17228 {
17229 tree templ;
17230 tree arguments;
17231 tree template_id;
17232 cp_token_position start_of_id = 0;
17233 cp_token *next_token = NULL, *next_token_2 = NULL;
17234 bool is_identifier;
17235
17236 /* If the next token corresponds to a template-id, there is no need
17237 to reparse it. */
17238 cp_token *token = cp_lexer_peek_token (parser->lexer);
17239
17240 if (token->type == CPP_TEMPLATE_ID)
17241 {
17242 cp_lexer_consume_token (parser->lexer);
17243 return saved_checks_value (token->u.tree_check_value);
17244 }
17245
17246 /* Avoid performing name lookup if there is no possibility of
17247 finding a template-id. */
17248 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
17249 || (token->type == CPP_NAME
17250 && !cp_parser_nth_token_starts_template_argument_list_p
17251 (parser, 2)))
17252 {
17253 cp_parser_error (parser, "expected template-id");
17254 return error_mark_node;
17255 }
17256
17257 /* Remember where the template-id starts. */
17258 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
17259 start_of_id = cp_lexer_token_position (parser->lexer, false);
17260
17261 push_deferring_access_checks (dk_deferred);
17262
17263 /* Parse the template-name. */
17264 is_identifier = false;
17265 templ = cp_parser_template_name (parser, template_keyword_p,
17266 check_dependency_p,
17267 is_declaration,
17268 tag_type,
17269 &is_identifier);
17270
17271 /* Push any access checks inside the firewall we're about to create. */
17272 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
17273 pop_deferring_access_checks ();
17274 if (templ == error_mark_node || is_identifier)
17275 return templ;
17276
17277 /* Since we're going to preserve any side-effects from this parse, set up a
17278 firewall to protect our callers from cp_parser_commit_to_tentative_parse
17279 in the template arguments. */
17280 tentative_firewall firewall (parser);
17281 reopen_deferring_access_checks (checks);
17282
17283 /* If we find the sequence `[:' after a template-name, it's probably
17284 a digraph-typo for `< ::'. Substitute the tokens and check if we can
17285 parse correctly the argument list. */
17286 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
17287 == CPP_OPEN_SQUARE)
17288 && next_token->flags & DIGRAPH
17289 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
17290 == CPP_COLON)
17291 && !(next_token_2->flags & PREV_WHITE))
17292 {
17293 cp_parser_parse_tentatively (parser);
17294 /* Change `:' into `::'. */
17295 next_token_2->type = CPP_SCOPE;
17296 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
17297 CPP_LESS. */
17298 cp_lexer_consume_token (parser->lexer);
17299
17300 /* Parse the arguments. */
17301 arguments = cp_parser_enclosed_template_argument_list (parser);
17302 if (!cp_parser_parse_definitely (parser))
17303 {
17304 /* If we couldn't parse an argument list, then we revert our changes
17305 and return simply an error. Maybe this is not a template-id
17306 after all. */
17307 next_token_2->type = CPP_COLON;
17308 cp_parser_error (parser, "expected %<<%>");
17309 pop_deferring_access_checks ();
17310 return error_mark_node;
17311 }
17312 /* Otherwise, emit an error about the invalid digraph, but continue
17313 parsing because we got our argument list. */
17314 if (permerror (next_token->location,
17315 "%<<::%> cannot begin a template-argument list"))
17316 {
17317 static bool hint = false;
17318 inform (next_token->location,
17319 "%<<:%> is an alternate spelling for %<[%>."
17320 " Insert whitespace between %<<%> and %<::%>");
17321 if (!hint && !flag_permissive)
17322 {
17323 inform (next_token->location, "(if you use %<-fpermissive%> "
17324 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
17325 "accept your code)");
17326 hint = true;
17327 }
17328 }
17329 }
17330 else
17331 {
17332 /* Look for the `<' that starts the template-argument-list. */
17333 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
17334 {
17335 pop_deferring_access_checks ();
17336 return error_mark_node;
17337 }
17338 /* Parse the arguments. */
17339 arguments = cp_parser_enclosed_template_argument_list (parser);
17340
17341 if ((cxx_dialect > cxx17)
17342 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
17343 && !template_keyword_p
17344 && (cp_parser_error_occurred (parser)
17345 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
17346 {
17347 /* This didn't go well. */
17348 if (TREE_CODE (templ) == FUNCTION_DECL)
17349 {
17350 /* C++20 says that "function-name < a;" is now ill-formed. */
17351 if (cp_parser_error_occurred (parser))
17352 {
17353 error_at (token->location, "invalid template-argument-list");
17354 inform (token->location, "function name as the left hand "
17355 "operand of %<<%> is ill-formed in C++20; wrap the "
17356 "function name in %<()%>");
17357 }
17358 else
17359 /* We expect "f<targs>" to be followed by "(args)". */
17360 error_at (cp_lexer_peek_token (parser->lexer)->location,
17361 "expected %<(%> after template-argument-list");
17362 if (start_of_id)
17363 /* Purge all subsequent tokens. */
17364 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
17365 }
17366 else
17367 cp_parser_simulate_error (parser);
17368 pop_deferring_access_checks ();
17369 return error_mark_node;
17370 }
17371 }
17372
17373 /* Set the location to be of the form:
17374 template-name < template-argument-list [opt] >
17375 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17376 with caret == start at the start of the template-name,
17377 ranging until the closing '>'. */
17378 location_t combined_loc
17379 = make_location (token->location, token->location, parser->lexer);
17380
17381 /* Check for concepts autos where they don't belong. We could
17382 identify types in some cases of identifier TEMPL, looking ahead
17383 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
17384 types. We reject them in functions, but if what we have is an
17385 identifier, even with none_type we can't conclude it's NOT a
17386 type, we have to wait for template substitution. */
17387 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
17388 template_id = error_mark_node;
17389 /* Build a representation of the specialization. */
17390 else if (identifier_p (templ))
17391 template_id = build_min_nt_loc (combined_loc,
17392 TEMPLATE_ID_EXPR,
17393 templ, arguments);
17394 else if (DECL_TYPE_TEMPLATE_P (templ)
17395 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
17396 {
17397 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
17398 template (rather than some instantiation thereof) only if
17399 is not nested within some other construct. For example, in
17400 "template <typename T> void f(T) { A<T>::", A<T> is just an
17401 instantiation of A. */
17402 bool entering_scope
17403 = (template_parm_scope_p ()
17404 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
17405 template_id
17406 = finish_template_type (templ, arguments, entering_scope);
17407 }
17408 else if (concept_definition_p (templ))
17409 {
17410 /* The caller will decide whether this is a concept check or type
17411 constraint. */
17412 template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
17413 boolean_type_node, templ, arguments);
17414 }
17415 else if (variable_template_p (templ))
17416 {
17417 template_id = lookup_template_variable (templ, arguments);
17418 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
17419 SET_EXPR_LOCATION (template_id, combined_loc);
17420 }
17421 else
17422 {
17423 /* If it's not a class-template or a template-template, it should be
17424 a function-template. */
17425 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
17426
17427 template_id = lookup_template_function (templ, arguments);
17428 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
17429 SET_EXPR_LOCATION (template_id, combined_loc);
17430 }
17431
17432 /* If parsing tentatively, replace the sequence of tokens that makes
17433 up the template-id with a CPP_TEMPLATE_ID token. That way,
17434 should we re-parse the token stream, we will not have to repeat
17435 the effort required to do the parse, nor will we issue duplicate
17436 error messages about problems during instantiation of the
17437 template. */
17438 if (start_of_id
17439 /* Don't do this if we had a parse error in a declarator; re-parsing
17440 might succeed if a name changes meaning (60361). */
17441 && !(cp_parser_error_occurred (parser)
17442 && cp_parser_parsing_tentatively (parser)
17443 && parser->in_declarator_p))
17444 {
17445 /* Reset the contents of the START_OF_ID token. */
17446 token->type = CPP_TEMPLATE_ID;
17447 token->location = combined_loc;
17448
17449 /* Retrieve any deferred checks. Do not pop this access checks yet
17450 so the memory will not be reclaimed during token replacing below. */
17451 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
17452 token->tree_check_p = true;
17453 token->u.tree_check_value->value = template_id;
17454 token->u.tree_check_value->checks = get_deferred_access_checks ();
17455 token->keyword = RID_MAX;
17456
17457 /* Purge all subsequent tokens. */
17458 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
17459
17460 /* ??? Can we actually assume that, if template_id ==
17461 error_mark_node, we will have issued a diagnostic to the
17462 user, as opposed to simply marking the tentative parse as
17463 failed? */
17464 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
17465 error_at (token->location, "parse error in template argument list");
17466 }
17467
17468 pop_to_parent_deferring_access_checks ();
17469 return template_id;
17470 }
17471
17472 /* Like cp_parser_template_id, called in non-type context. */
17473
17474 static tree
17475 cp_parser_template_id_expr (cp_parser *parser,
17476 bool template_keyword_p,
17477 bool check_dependency_p,
17478 bool is_declaration)
17479 {
17480 tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
17481 none_type, is_declaration);
17482 if (TREE_CODE (x) == TEMPLATE_ID_EXPR
17483 && concept_check_p (x))
17484 /* We didn't check the arguments in cp_parser_template_id; do that now. */
17485 return build_concept_id (x);
17486 return x;
17487 }
17488
17489 /* Parse a template-name.
17490
17491 template-name:
17492 identifier
17493
17494 The standard should actually say:
17495
17496 template-name:
17497 identifier
17498 operator-function-id
17499
17500 A defect report has been filed about this issue.
17501
17502 A conversion-function-id cannot be a template name because they cannot
17503 be part of a template-id. In fact, looking at this code:
17504
17505 a.operator K<int>()
17506
17507 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
17508 It is impossible to call a templated conversion-function-id with an
17509 explicit argument list, since the only allowed template parameter is
17510 the type to which it is converting.
17511
17512 If TEMPLATE_KEYWORD_P is true, then we have just seen the
17513 `template' keyword, in a construction like:
17514
17515 T::template f<3>()
17516
17517 In that case `f' is taken to be a template-name, even though there
17518 is no way of knowing for sure.
17519
17520 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
17521 name refers to a set of overloaded functions, at least one of which
17522 is a template, or an IDENTIFIER_NODE with the name of the template,
17523 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
17524 names are looked up inside uninstantiated templates. */
17525
17526 static tree
17527 cp_parser_template_name (cp_parser* parser,
17528 bool template_keyword_p,
17529 bool check_dependency_p,
17530 bool is_declaration,
17531 enum tag_types tag_type,
17532 bool *is_identifier)
17533 {
17534 tree identifier;
17535 tree decl;
17536 cp_token *token = cp_lexer_peek_token (parser->lexer);
17537
17538 /* If the next token is `operator', then we have either an
17539 operator-function-id or a conversion-function-id. */
17540 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
17541 {
17542 /* We don't know whether we're looking at an
17543 operator-function-id or a conversion-function-id. */
17544 cp_parser_parse_tentatively (parser);
17545 /* Try an operator-function-id. */
17546 identifier = cp_parser_operator_function_id (parser);
17547 /* If that didn't work, try a conversion-function-id. */
17548 if (!cp_parser_parse_definitely (parser))
17549 {
17550 cp_parser_error (parser, "expected template-name");
17551 return error_mark_node;
17552 }
17553 }
17554 /* Look for the identifier. */
17555 else
17556 identifier = cp_parser_identifier (parser);
17557
17558 /* If we didn't find an identifier, we don't have a template-id. */
17559 if (identifier == error_mark_node)
17560 return error_mark_node;
17561
17562 /* If the name immediately followed the `template' keyword, then it
17563 is a template-name. However, if the next token is not `<', then
17564 we do not treat it as a template-name, since it is not being used
17565 as part of a template-id. This enables us to handle constructs
17566 like:
17567
17568 template <typename T> struct S { S(); };
17569 template <typename T> S<T>::S();
17570
17571 correctly. We would treat `S' as a template -- if it were `S<T>'
17572 -- but we do not if there is no `<'. */
17573
17574 if (processing_template_decl
17575 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
17576 {
17577 /* In a declaration, in a dependent context, we pretend that the
17578 "template" keyword was present in order to improve error
17579 recovery. For example, given:
17580
17581 template <typename T> void f(T::X<int>);
17582
17583 we want to treat "X<int>" as a template-id. */
17584 if (is_declaration
17585 && !template_keyword_p
17586 && parser->scope && TYPE_P (parser->scope)
17587 && check_dependency_p
17588 && dependent_scope_p (parser->scope)
17589 /* Do not do this for dtors (or ctors), since they never
17590 need the template keyword before their name. */
17591 && !constructor_name_p (identifier, parser->scope))
17592 {
17593 cp_token_position start = 0;
17594
17595 /* Explain what went wrong. */
17596 error_at (token->location, "non-template %qD used as template",
17597 identifier);
17598 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
17599 parser->scope, identifier);
17600 /* If parsing tentatively, find the location of the "<" token. */
17601 if (cp_parser_simulate_error (parser))
17602 start = cp_lexer_token_position (parser->lexer, true);
17603 /* Parse the template arguments so that we can issue error
17604 messages about them. */
17605 cp_lexer_consume_token (parser->lexer);
17606 cp_parser_enclosed_template_argument_list (parser);
17607 /* Skip tokens until we find a good place from which to
17608 continue parsing. */
17609 cp_parser_skip_to_closing_parenthesis (parser,
17610 /*recovering=*/true,
17611 /*or_comma=*/true,
17612 /*consume_paren=*/false);
17613 /* If parsing tentatively, permanently remove the
17614 template argument list. That will prevent duplicate
17615 error messages from being issued about the missing
17616 "template" keyword. */
17617 if (start)
17618 cp_lexer_purge_tokens_after (parser->lexer, start);
17619 if (is_identifier)
17620 *is_identifier = true;
17621 parser->context->object_type = NULL_TREE;
17622 return identifier;
17623 }
17624
17625 /* If the "template" keyword is present, then there is generally
17626 no point in doing name-lookup, so we just return IDENTIFIER.
17627 But, if the qualifying scope is non-dependent then we can
17628 (and must) do name-lookup normally. */
17629 if (template_keyword_p)
17630 {
17631 tree scope = (parser->scope ? parser->scope
17632 : parser->context->object_type);
17633 if (scope && TYPE_P (scope)
17634 && (!CLASS_TYPE_P (scope)
17635 || (check_dependency_p && dependent_type_p (scope))))
17636 {
17637 /* We're optimizing away the call to cp_parser_lookup_name, but
17638 we still need to do this. */
17639 parser->context->object_type = NULL_TREE;
17640 return identifier;
17641 }
17642 }
17643 }
17644
17645 /* cp_parser_lookup_name clears OBJECT_TYPE. */
17646 const bool scoped_p = ((parser->scope ? parser->scope
17647 : parser->context->object_type) != NULL_TREE);
17648
17649 /* Look up the name. */
17650 decl = cp_parser_lookup_name (parser, identifier,
17651 tag_type,
17652 /*is_template=*/true,
17653 /*is_namespace=*/false,
17654 check_dependency_p,
17655 /*ambiguous_decls=*/NULL,
17656 token->location);
17657
17658 decl = strip_using_decl (decl);
17659
17660 /* If DECL is a template, then the name was a template-name. */
17661 if (TREE_CODE (decl) == TEMPLATE_DECL)
17662 {
17663 if (TREE_DEPRECATED (decl)
17664 && deprecated_state != DEPRECATED_SUPPRESS)
17665 {
17666 tree d = DECL_TEMPLATE_RESULT (decl);
17667 tree attr;
17668 if (TREE_CODE (d) == TYPE_DECL)
17669 attr = lookup_attribute ("deprecated",
17670 TYPE_ATTRIBUTES (TREE_TYPE (d)));
17671 else
17672 attr = lookup_attribute ("deprecated",
17673 DECL_ATTRIBUTES (d));
17674 warn_deprecated_use (decl, attr);
17675 }
17676 }
17677 else
17678 {
17679 /* The standard does not explicitly indicate whether a name that
17680 names a set of overloaded declarations, some of which are
17681 templates, is a template-name. However, such a name should
17682 be a template-name; otherwise, there is no way to form a
17683 template-id for the overloaded templates. */
17684 bool found = false;
17685
17686 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
17687 !found && iter; ++iter)
17688 if (TREE_CODE (*iter) == TEMPLATE_DECL)
17689 found = true;
17690
17691 if (!found
17692 && (cxx_dialect > cxx17)
17693 && !scoped_p
17694 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
17695 && tag_type == none_type)
17696 {
17697 /* [temp.names] says "A name is also considered to refer to a template
17698 if it is an unqualified-id followed by a < and name lookup finds
17699 either one or more functions or finds nothing." */
17700
17701 /* The "more functions" case. Just use the OVERLOAD as normally.
17702 We don't use is_overloaded_fn here to avoid considering
17703 BASELINKs. */
17704 if (TREE_CODE (decl) == OVERLOAD
17705 /* Name lookup found one function. */
17706 || TREE_CODE (decl) == FUNCTION_DECL)
17707 found = true;
17708 /* Name lookup found nothing. */
17709 else if (decl == error_mark_node)
17710 return identifier;
17711 }
17712
17713 if (!found)
17714 {
17715 /* The name does not name a template. */
17716 cp_parser_error (parser, "expected template-name");
17717 return error_mark_node;
17718 }
17719 }
17720
17721 return decl;
17722 }
17723
17724 /* Parse a template-argument-list.
17725
17726 template-argument-list:
17727 template-argument ... [opt]
17728 template-argument-list , template-argument ... [opt]
17729
17730 Returns a TREE_VEC containing the arguments. */
17731
17732 static tree
17733 cp_parser_template_argument_list (cp_parser* parser)
17734 {
17735 tree fixed_args[10];
17736 unsigned n_args = 0;
17737 unsigned alloced = 10;
17738 tree *arg_ary = fixed_args;
17739 tree vec;
17740 bool saved_in_template_argument_list_p;
17741 bool saved_ice_p;
17742 bool saved_non_ice_p;
17743
17744 /* Don't create location wrapper nodes within a template-argument-list. */
17745 auto_suppress_location_wrappers sentinel;
17746
17747 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
17748 parser->in_template_argument_list_p = true;
17749 /* Even if the template-id appears in an integral
17750 constant-expression, the contents of the argument list do
17751 not. */
17752 saved_ice_p = parser->integral_constant_expression_p;
17753 parser->integral_constant_expression_p = false;
17754 saved_non_ice_p = parser->non_integral_constant_expression_p;
17755 parser->non_integral_constant_expression_p = false;
17756
17757 /* Parse the arguments. */
17758 do
17759 {
17760 tree argument;
17761
17762 if (n_args)
17763 /* Consume the comma. */
17764 cp_lexer_consume_token (parser->lexer);
17765
17766 /* Parse the template-argument. */
17767 argument = cp_parser_template_argument (parser);
17768
17769 /* If the next token is an ellipsis, we're expanding a template
17770 argument pack. */
17771 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17772 {
17773 if (argument == error_mark_node)
17774 {
17775 cp_token *token = cp_lexer_peek_token (parser->lexer);
17776 error_at (token->location,
17777 "expected parameter pack before %<...%>");
17778 }
17779 /* Consume the `...' token. */
17780 cp_lexer_consume_token (parser->lexer);
17781
17782 /* Make the argument into a TYPE_PACK_EXPANSION or
17783 EXPR_PACK_EXPANSION. */
17784 argument = make_pack_expansion (argument);
17785 }
17786
17787 if (n_args == alloced)
17788 {
17789 alloced *= 2;
17790
17791 if (arg_ary == fixed_args)
17792 {
17793 arg_ary = XNEWVEC (tree, alloced);
17794 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
17795 }
17796 else
17797 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
17798 }
17799 arg_ary[n_args++] = argument;
17800 }
17801 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17802
17803 vec = make_tree_vec (n_args);
17804
17805 while (n_args--)
17806 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
17807
17808 if (arg_ary != fixed_args)
17809 free (arg_ary);
17810 parser->non_integral_constant_expression_p = saved_non_ice_p;
17811 parser->integral_constant_expression_p = saved_ice_p;
17812 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
17813 if (CHECKING_P)
17814 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
17815 return vec;
17816 }
17817
17818 /* Parse a template-argument.
17819
17820 template-argument:
17821 assignment-expression
17822 type-id
17823 id-expression
17824
17825 The representation is that of an assignment-expression, type-id, or
17826 id-expression -- except that the qualified id-expression is
17827 evaluated, so that the value returned is either a DECL or an
17828 OVERLOAD.
17829
17830 Although the standard says "assignment-expression", it forbids
17831 throw-expressions or assignments in the template argument.
17832 Therefore, we use "conditional-expression" instead. */
17833
17834 static tree
17835 cp_parser_template_argument (cp_parser* parser)
17836 {
17837 tree argument;
17838 bool template_p;
17839 bool address_p;
17840 bool maybe_type_id = false;
17841 cp_token *token = NULL, *argument_start_token = NULL;
17842 location_t loc = 0;
17843 cp_id_kind idk;
17844
17845 /* There's really no way to know what we're looking at, so we just
17846 try each alternative in order.
17847
17848 [temp.arg]
17849
17850 In a template-argument, an ambiguity between a type-id and an
17851 expression is resolved to a type-id, regardless of the form of
17852 the corresponding template-parameter.
17853
17854 Therefore, we try a type-id first. */
17855 cp_parser_parse_tentatively (parser);
17856 argument = cp_parser_template_type_arg (parser);
17857 /* If there was no error parsing the type-id but the next token is a
17858 '>>', our behavior depends on which dialect of C++ we're
17859 parsing. In C++98, we probably found a typo for '> >'. But there
17860 are type-id which are also valid expressions. For instance:
17861
17862 struct X { int operator >> (int); };
17863 template <int V> struct Foo {};
17864 Foo<X () >> 5> r;
17865
17866 Here 'X()' is a valid type-id of a function type, but the user just
17867 wanted to write the expression "X() >> 5". Thus, we remember that we
17868 found a valid type-id, but we still try to parse the argument as an
17869 expression to see what happens.
17870
17871 In C++0x, the '>>' will be considered two separate '>'
17872 tokens. */
17873 if (!cp_parser_error_occurred (parser)
17874 && cxx_dialect == cxx98
17875 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17876 {
17877 maybe_type_id = true;
17878 cp_parser_abort_tentative_parse (parser);
17879 }
17880 else
17881 {
17882 /* If the next token isn't a `,' or a `>', then this argument wasn't
17883 really finished. This means that the argument is not a valid
17884 type-id. */
17885 if (!cp_parser_next_token_ends_template_argument_p (parser))
17886 cp_parser_error (parser, "expected template-argument");
17887 /* If that worked, we're done. */
17888 if (cp_parser_parse_definitely (parser))
17889 return argument;
17890 }
17891 /* We're still not sure what the argument will be. */
17892 cp_parser_parse_tentatively (parser);
17893 /* Try a template. */
17894 argument_start_token = cp_lexer_peek_token (parser->lexer);
17895 argument = cp_parser_id_expression (parser,
17896 /*template_keyword_p=*/false,
17897 /*check_dependency_p=*/true,
17898 &template_p,
17899 /*declarator_p=*/false,
17900 /*optional_p=*/false);
17901 /* If the next token isn't a `,' or a `>', then this argument wasn't
17902 really finished. */
17903 if (!cp_parser_next_token_ends_template_argument_p (parser))
17904 cp_parser_error (parser, "expected template-argument");
17905 if (!cp_parser_error_occurred (parser))
17906 {
17907 /* Figure out what is being referred to. If the id-expression
17908 was for a class template specialization, then we will have a
17909 TYPE_DECL at this point. There is no need to do name lookup
17910 at this point in that case. */
17911 if (TREE_CODE (argument) != TYPE_DECL)
17912 argument = cp_parser_lookup_name (parser, argument,
17913 none_type,
17914 /*is_template=*/template_p,
17915 /*is_namespace=*/false,
17916 /*check_dependency=*/true,
17917 /*ambiguous_decls=*/NULL,
17918 argument_start_token->location);
17919 if (TREE_CODE (argument) != TEMPLATE_DECL
17920 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
17921 cp_parser_error (parser, "expected template-name");
17922 }
17923 if (cp_parser_parse_definitely (parser))
17924 {
17925 if (TREE_DEPRECATED (argument))
17926 warn_deprecated_use (argument, NULL_TREE);
17927 return argument;
17928 }
17929 /* It must be a non-type argument. In C++17 any constant-expression is
17930 allowed. */
17931 if (cxx_dialect > cxx14)
17932 goto general_expr;
17933
17934 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
17935
17936 -- an integral constant-expression of integral or enumeration
17937 type; or
17938
17939 -- the name of a non-type template-parameter; or
17940
17941 -- the name of an object or function with external linkage...
17942
17943 -- the address of an object or function with external linkage...
17944
17945 -- a pointer to member... */
17946 /* Look for a non-type template parameter. */
17947 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17948 {
17949 cp_parser_parse_tentatively (parser);
17950 argument = cp_parser_primary_expression (parser,
17951 /*address_p=*/false,
17952 /*cast_p=*/false,
17953 /*template_arg_p=*/true,
17954 &idk);
17955 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
17956 || !cp_parser_next_token_ends_template_argument_p (parser))
17957 cp_parser_simulate_error (parser);
17958 if (cp_parser_parse_definitely (parser))
17959 return argument;
17960 }
17961
17962 /* If the next token is "&", the argument must be the address of an
17963 object or function with external linkage. */
17964 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
17965 if (address_p)
17966 {
17967 loc = cp_lexer_peek_token (parser->lexer)->location;
17968 cp_lexer_consume_token (parser->lexer);
17969 }
17970 /* See if we might have an id-expression. */
17971 token = cp_lexer_peek_token (parser->lexer);
17972 if (token->type == CPP_NAME
17973 || token->keyword == RID_OPERATOR
17974 || token->type == CPP_SCOPE
17975 || token->type == CPP_TEMPLATE_ID
17976 || token->type == CPP_NESTED_NAME_SPECIFIER)
17977 {
17978 cp_parser_parse_tentatively (parser);
17979 argument = cp_parser_primary_expression (parser,
17980 address_p,
17981 /*cast_p=*/false,
17982 /*template_arg_p=*/true,
17983 &idk);
17984 if (cp_parser_error_occurred (parser)
17985 || !cp_parser_next_token_ends_template_argument_p (parser))
17986 cp_parser_abort_tentative_parse (parser);
17987 else
17988 {
17989 tree probe;
17990
17991 if (INDIRECT_REF_P (argument))
17992 {
17993 /* Strip the dereference temporarily. */
17994 gcc_assert (REFERENCE_REF_P (argument));
17995 argument = TREE_OPERAND (argument, 0);
17996 }
17997
17998 /* If we're in a template, we represent a qualified-id referring
17999 to a static data member as a SCOPE_REF even if the scope isn't
18000 dependent so that we can check access control later. */
18001 probe = argument;
18002 if (TREE_CODE (probe) == SCOPE_REF)
18003 probe = TREE_OPERAND (probe, 1);
18004 if (VAR_P (probe))
18005 {
18006 /* A variable without external linkage might still be a
18007 valid constant-expression, so no error is issued here
18008 if the external-linkage check fails. */
18009 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
18010 cp_parser_simulate_error (parser);
18011 }
18012 else if (is_overloaded_fn (argument))
18013 /* All overloaded functions are allowed; if the external
18014 linkage test does not pass, an error will be issued
18015 later. */
18016 ;
18017 else if (address_p
18018 && (TREE_CODE (argument) == OFFSET_REF
18019 || TREE_CODE (argument) == SCOPE_REF))
18020 /* A pointer-to-member. */
18021 ;
18022 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
18023 ;
18024 else
18025 cp_parser_simulate_error (parser);
18026
18027 if (cp_parser_parse_definitely (parser))
18028 {
18029 if (address_p)
18030 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
18031 tf_warning_or_error);
18032 else
18033 argument = convert_from_reference (argument);
18034 return argument;
18035 }
18036 }
18037 }
18038 /* If the argument started with "&", there are no other valid
18039 alternatives at this point. */
18040 if (address_p)
18041 {
18042 cp_parser_error (parser, "invalid non-type template argument");
18043 return error_mark_node;
18044 }
18045
18046 general_expr:
18047 /* If the argument wasn't successfully parsed as a type-id followed
18048 by '>>', the argument can only be a constant expression now.
18049 Otherwise, we try parsing the constant-expression tentatively,
18050 because the argument could really be a type-id. */
18051 if (maybe_type_id)
18052 cp_parser_parse_tentatively (parser);
18053
18054 if (cxx_dialect <= cxx14)
18055 argument = cp_parser_constant_expression (parser);
18056 else
18057 {
18058 /* In C++20, we can encounter a braced-init-list. */
18059 if (cxx_dialect >= cxx20
18060 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18061 {
18062 bool expr_non_constant_p;
18063 return cp_parser_braced_list (parser, &expr_non_constant_p);
18064 }
18065
18066 /* With C++17 generalized non-type template arguments we need to handle
18067 lvalue constant expressions, too. */
18068 argument = cp_parser_assignment_expression (parser);
18069 require_potential_constant_expression (argument);
18070 }
18071
18072 if (!maybe_type_id)
18073 return argument;
18074 if (!cp_parser_next_token_ends_template_argument_p (parser))
18075 cp_parser_error (parser, "expected template-argument");
18076 if (cp_parser_parse_definitely (parser))
18077 return argument;
18078 /* We did our best to parse the argument as a non type-id, but that
18079 was the only alternative that matched (albeit with a '>' after
18080 it). We can assume it's just a typo from the user, and a
18081 diagnostic will then be issued. */
18082 return cp_parser_template_type_arg (parser);
18083 }
18084
18085 /* Parse an explicit-instantiation.
18086
18087 explicit-instantiation:
18088 template declaration
18089
18090 Although the standard says `declaration', what it really means is:
18091
18092 explicit-instantiation:
18093 template decl-specifier-seq [opt] declarator [opt] ;
18094
18095 Things like `template int S<int>::i = 5, int S<double>::j;' are not
18096 supposed to be allowed. A defect report has been filed about this
18097 issue.
18098
18099 GNU Extension:
18100
18101 explicit-instantiation:
18102 storage-class-specifier template
18103 decl-specifier-seq [opt] declarator [opt] ;
18104 function-specifier template
18105 decl-specifier-seq [opt] declarator [opt] ; */
18106
18107 static void
18108 cp_parser_explicit_instantiation (cp_parser* parser)
18109 {
18110 int declares_class_or_enum;
18111 cp_decl_specifier_seq decl_specifiers;
18112 tree extension_specifier = NULL_TREE;
18113
18114 timevar_push (TV_TEMPLATE_INST);
18115
18116 /* Look for an (optional) storage-class-specifier or
18117 function-specifier. */
18118 if (cp_parser_allow_gnu_extensions_p (parser))
18119 {
18120 extension_specifier
18121 = cp_parser_storage_class_specifier_opt (parser);
18122 if (!extension_specifier)
18123 extension_specifier
18124 = cp_parser_function_specifier_opt (parser,
18125 /*decl_specs=*/NULL);
18126 }
18127
18128 /* Look for the `template' keyword. */
18129 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
18130 /* Let the front end know that we are processing an explicit
18131 instantiation. */
18132 begin_explicit_instantiation ();
18133 /* [temp.explicit] says that we are supposed to ignore access
18134 control while processing explicit instantiation directives. */
18135 push_deferring_access_checks (dk_no_check);
18136 /* Parse a decl-specifier-seq. */
18137 cp_parser_decl_specifier_seq (parser,
18138 CP_PARSER_FLAGS_OPTIONAL,
18139 &decl_specifiers,
18140 &declares_class_or_enum);
18141 /* If there was exactly one decl-specifier, and it declared a class,
18142 and there's no declarator, then we have an explicit type
18143 instantiation. */
18144 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
18145 {
18146 tree type = check_tag_decl (&decl_specifiers,
18147 /*explicit_type_instantiation_p=*/true);
18148 /* Turn access control back on for names used during
18149 template instantiation. */
18150 pop_deferring_access_checks ();
18151 if (type)
18152 do_type_instantiation (type, extension_specifier,
18153 /*complain=*/tf_error);
18154 }
18155 else
18156 {
18157 cp_declarator *declarator;
18158 tree decl;
18159
18160 /* Parse the declarator. */
18161 declarator
18162 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18163 CP_PARSER_FLAGS_NONE,
18164 /*ctor_dtor_or_conv_p=*/NULL,
18165 /*parenthesized_p=*/NULL,
18166 /*member_p=*/false,
18167 /*friend_p=*/false,
18168 /*static_p=*/false);
18169 if (declares_class_or_enum & 2)
18170 cp_parser_check_for_definition_in_return_type (declarator,
18171 decl_specifiers.type,
18172 decl_specifiers.locations[ds_type_spec]);
18173 if (declarator != cp_error_declarator)
18174 {
18175 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
18176 permerror (decl_specifiers.locations[ds_inline],
18177 "explicit instantiation shall not use"
18178 " %<inline%> specifier");
18179 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
18180 permerror (decl_specifiers.locations[ds_constexpr],
18181 "explicit instantiation shall not use"
18182 " %<constexpr%> specifier");
18183 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
18184 permerror (decl_specifiers.locations[ds_consteval],
18185 "explicit instantiation shall not use"
18186 " %<consteval%> specifier");
18187
18188 decl = grokdeclarator (declarator, &decl_specifiers,
18189 NORMAL, 0, &decl_specifiers.attributes);
18190 /* Turn access control back on for names used during
18191 template instantiation. */
18192 pop_deferring_access_checks ();
18193 /* Do the explicit instantiation. */
18194 do_decl_instantiation (decl, extension_specifier);
18195 }
18196 else
18197 {
18198 pop_deferring_access_checks ();
18199 /* Skip the body of the explicit instantiation. */
18200 cp_parser_skip_to_end_of_statement (parser);
18201 }
18202 }
18203 /* We're done with the instantiation. */
18204 end_explicit_instantiation ();
18205
18206 cp_parser_consume_semicolon_at_end_of_statement (parser);
18207
18208 timevar_pop (TV_TEMPLATE_INST);
18209 }
18210
18211 /* Parse an explicit-specialization.
18212
18213 explicit-specialization:
18214 template < > declaration
18215
18216 Although the standard says `declaration', what it really means is:
18217
18218 explicit-specialization:
18219 template <> decl-specifier [opt] init-declarator [opt] ;
18220 template <> function-definition
18221 template <> explicit-specialization
18222 template <> template-declaration */
18223
18224 static void
18225 cp_parser_explicit_specialization (cp_parser* parser)
18226 {
18227 cp_token *token = cp_lexer_peek_token (parser->lexer);
18228
18229 /* Look for the `template' keyword. */
18230 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
18231 /* Look for the `<'. */
18232 cp_parser_require (parser, CPP_LESS, RT_LESS);
18233 /* Look for the `>'. */
18234 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
18235 /* We have processed another parameter list. */
18236 ++parser->num_template_parameter_lists;
18237
18238 /* [temp]
18239
18240 A template ... explicit specialization ... shall not have C
18241 linkage. */
18242 bool need_lang_pop = current_lang_name == lang_name_c;
18243 if (need_lang_pop)
18244 {
18245 error_at (token->location, "template specialization with C linkage");
18246 maybe_show_extern_c_location ();
18247
18248 /* Give it C++ linkage to avoid confusing other parts of the
18249 front end. */
18250 push_lang_context (lang_name_cplusplus);
18251 }
18252
18253 /* Let the front end know that we are beginning a specialization. */
18254 if (begin_specialization ())
18255 {
18256 /* If the next keyword is `template', we need to figure out
18257 whether or not we're looking a template-declaration. */
18258 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18259 {
18260 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18261 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
18262 cp_parser_template_declaration_after_export (parser,
18263 /*member_p=*/false);
18264 else
18265 cp_parser_explicit_specialization (parser);
18266 }
18267 else
18268 /* Parse the dependent declaration. */
18269 cp_parser_single_declaration (parser,
18270 /*checks=*/NULL,
18271 /*member_p=*/false,
18272 /*explicit_specialization_p=*/true,
18273 /*friend_p=*/NULL);
18274 }
18275
18276 /* We're done with the specialization. */
18277 end_specialization ();
18278
18279 /* For the erroneous case of a template with C linkage, we pushed an
18280 implicit C++ linkage scope; exit that scope now. */
18281 if (need_lang_pop)
18282 pop_lang_context ();
18283
18284 /* We're done with this parameter list. */
18285 --parser->num_template_parameter_lists;
18286 }
18287
18288 /* Parse a type-specifier.
18289
18290 type-specifier:
18291 simple-type-specifier
18292 class-specifier
18293 enum-specifier
18294 elaborated-type-specifier
18295 cv-qualifier
18296
18297 GNU Extension:
18298
18299 type-specifier:
18300 __complex__
18301
18302 Returns a representation of the type-specifier. For a
18303 class-specifier, enum-specifier, or elaborated-type-specifier, a
18304 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
18305
18306 The parser flags FLAGS is used to control type-specifier parsing.
18307
18308 If IS_DECLARATION is TRUE, then this type-specifier is appearing
18309 in a decl-specifier-seq.
18310
18311 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
18312 class-specifier, enum-specifier, or elaborated-type-specifier, then
18313 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
18314 if a type is declared; 2 if it is defined. Otherwise, it is set to
18315 zero.
18316
18317 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
18318 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
18319 is set to FALSE. */
18320
18321 static tree
18322 cp_parser_type_specifier (cp_parser* parser,
18323 cp_parser_flags flags,
18324 cp_decl_specifier_seq *decl_specs,
18325 bool is_declaration,
18326 int* declares_class_or_enum,
18327 bool* is_cv_qualifier)
18328 {
18329 tree type_spec = NULL_TREE;
18330 cp_token *token;
18331 enum rid keyword;
18332 cp_decl_spec ds = ds_last;
18333
18334 /* Assume this type-specifier does not declare a new type. */
18335 if (declares_class_or_enum)
18336 *declares_class_or_enum = 0;
18337 /* And that it does not specify a cv-qualifier. */
18338 if (is_cv_qualifier)
18339 *is_cv_qualifier = false;
18340 /* Peek at the next token. */
18341 token = cp_lexer_peek_token (parser->lexer);
18342
18343 /* If we're looking at a keyword, we can use that to guide the
18344 production we choose. */
18345 keyword = token->keyword;
18346 switch (keyword)
18347 {
18348 case RID_ENUM:
18349 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
18350 goto elaborated_type_specifier;
18351
18352 /* Look for the enum-specifier. */
18353 type_spec = cp_parser_enum_specifier (parser);
18354 /* If that worked, we're done. */
18355 if (type_spec)
18356 {
18357 if (declares_class_or_enum)
18358 *declares_class_or_enum = 2;
18359 if (decl_specs)
18360 cp_parser_set_decl_spec_type (decl_specs,
18361 type_spec,
18362 token,
18363 /*type_definition_p=*/true);
18364 return type_spec;
18365 }
18366 else
18367 goto elaborated_type_specifier;
18368
18369 /* Any of these indicate either a class-specifier, or an
18370 elaborated-type-specifier. */
18371 case RID_CLASS:
18372 case RID_STRUCT:
18373 case RID_UNION:
18374 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
18375 goto elaborated_type_specifier;
18376
18377 /* Parse tentatively so that we can back up if we don't find a
18378 class-specifier. */
18379 cp_parser_parse_tentatively (parser);
18380 /* Look for the class-specifier. */
18381 type_spec = cp_parser_class_specifier (parser);
18382 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
18383 /* If that worked, we're done. */
18384 if (cp_parser_parse_definitely (parser))
18385 {
18386 if (declares_class_or_enum)
18387 *declares_class_or_enum = 2;
18388 if (decl_specs)
18389 cp_parser_set_decl_spec_type (decl_specs,
18390 type_spec,
18391 token,
18392 /*type_definition_p=*/true);
18393 return type_spec;
18394 }
18395
18396 /* Fall through. */
18397 elaborated_type_specifier:
18398 /* We're declaring (not defining) a class or enum. */
18399 if (declares_class_or_enum)
18400 *declares_class_or_enum = 1;
18401
18402 /* Fall through. */
18403 case RID_TYPENAME:
18404 /* Look for an elaborated-type-specifier. */
18405 type_spec
18406 = (cp_parser_elaborated_type_specifier
18407 (parser,
18408 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
18409 is_declaration));
18410 if (decl_specs)
18411 cp_parser_set_decl_spec_type (decl_specs,
18412 type_spec,
18413 token,
18414 /*type_definition_p=*/false);
18415 return type_spec;
18416
18417 case RID_CONST:
18418 ds = ds_const;
18419 if (is_cv_qualifier)
18420 *is_cv_qualifier = true;
18421 break;
18422
18423 case RID_VOLATILE:
18424 ds = ds_volatile;
18425 if (is_cv_qualifier)
18426 *is_cv_qualifier = true;
18427 break;
18428
18429 case RID_RESTRICT:
18430 ds = ds_restrict;
18431 if (is_cv_qualifier)
18432 *is_cv_qualifier = true;
18433 break;
18434
18435 case RID_COMPLEX:
18436 /* The `__complex__' keyword is a GNU extension. */
18437 ds = ds_complex;
18438 break;
18439
18440 default:
18441 break;
18442 }
18443
18444 /* Handle simple keywords. */
18445 if (ds != ds_last)
18446 {
18447 if (decl_specs)
18448 {
18449 set_and_check_decl_spec_loc (decl_specs, ds, token);
18450 decl_specs->any_specifiers_p = true;
18451 }
18452 return cp_lexer_consume_token (parser->lexer)->u.value;
18453 }
18454
18455 /* If we do not already have a type-specifier, assume we are looking
18456 at a simple-type-specifier. */
18457 type_spec = cp_parser_simple_type_specifier (parser,
18458 decl_specs,
18459 flags);
18460
18461 /* If we didn't find a type-specifier, and a type-specifier was not
18462 optional in this context, issue an error message. */
18463 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
18464 {
18465 cp_parser_error (parser, "expected type specifier");
18466 return error_mark_node;
18467 }
18468
18469 return type_spec;
18470 }
18471
18472 /* Parse a simple-type-specifier.
18473
18474 simple-type-specifier:
18475 :: [opt] nested-name-specifier [opt] type-name
18476 :: [opt] nested-name-specifier template template-id
18477 char
18478 wchar_t
18479 bool
18480 short
18481 int
18482 long
18483 signed
18484 unsigned
18485 float
18486 double
18487 void
18488
18489 C++11 Extension:
18490
18491 simple-type-specifier:
18492 auto
18493 decltype ( expression )
18494 char16_t
18495 char32_t
18496 __underlying_type ( type-id )
18497
18498 C++17 extension:
18499
18500 nested-name-specifier(opt) template-name
18501
18502 GNU Extension:
18503
18504 simple-type-specifier:
18505 __int128
18506 __typeof__ unary-expression
18507 __typeof__ ( type-id )
18508 __typeof__ ( type-id ) { initializer-list , [opt] }
18509
18510 Concepts Extension:
18511
18512 simple-type-specifier:
18513 constrained-type-specifier
18514
18515 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
18516 appropriately updated. */
18517
18518 static tree
18519 cp_parser_simple_type_specifier (cp_parser* parser,
18520 cp_decl_specifier_seq *decl_specs,
18521 cp_parser_flags flags)
18522 {
18523 tree type = NULL_TREE;
18524 cp_token *token;
18525 int idx;
18526
18527 /* Peek at the next token. */
18528 token = cp_lexer_peek_token (parser->lexer);
18529
18530 /* If we're looking at a keyword, things are easy. */
18531 switch (token->keyword)
18532 {
18533 case RID_CHAR:
18534 if (decl_specs)
18535 decl_specs->explicit_char_p = true;
18536 type = char_type_node;
18537 break;
18538 case RID_CHAR8:
18539 type = char8_type_node;
18540 break;
18541 case RID_CHAR16:
18542 type = char16_type_node;
18543 break;
18544 case RID_CHAR32:
18545 type = char32_type_node;
18546 break;
18547 case RID_WCHAR:
18548 type = wchar_type_node;
18549 break;
18550 case RID_BOOL:
18551 type = boolean_type_node;
18552 break;
18553 case RID_SHORT:
18554 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
18555 type = short_integer_type_node;
18556 break;
18557 case RID_INT:
18558 if (decl_specs)
18559 decl_specs->explicit_int_p = true;
18560 type = integer_type_node;
18561 break;
18562 case RID_INT_N_0:
18563 case RID_INT_N_1:
18564 case RID_INT_N_2:
18565 case RID_INT_N_3:
18566 idx = token->keyword - RID_INT_N_0;
18567 if (! int_n_enabled_p [idx])
18568 break;
18569 if (decl_specs)
18570 {
18571 decl_specs->explicit_intN_p = true;
18572 decl_specs->int_n_idx = idx;
18573 /* Check if the alternate "__intN__" form has been used instead of
18574 "__intN". */
18575 if (strncmp (IDENTIFIER_POINTER (token->u.value)
18576 + (IDENTIFIER_LENGTH (token->u.value) - 2),
18577 "__", 2) == 0)
18578 decl_specs->int_n_alt = true;
18579 }
18580 type = int_n_trees [idx].signed_type;
18581 break;
18582 case RID_LONG:
18583 if (decl_specs)
18584 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
18585 type = long_integer_type_node;
18586 break;
18587 case RID_SIGNED:
18588 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
18589 type = integer_type_node;
18590 break;
18591 case RID_UNSIGNED:
18592 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
18593 type = unsigned_type_node;
18594 break;
18595 case RID_FLOAT:
18596 type = float_type_node;
18597 break;
18598 case RID_DOUBLE:
18599 type = double_type_node;
18600 break;
18601 case RID_VOID:
18602 type = void_type_node;
18603 break;
18604
18605 case RID_AUTO:
18606 maybe_warn_cpp0x (CPP0X_AUTO);
18607 if (parser->auto_is_implicit_function_template_parm_p)
18608 {
18609 /* The 'auto' might be the placeholder return type for a function decl
18610 with trailing return type. */
18611 bool have_trailing_return_fn_decl = false;
18612
18613 cp_parser_parse_tentatively (parser);
18614 cp_lexer_consume_token (parser->lexer);
18615 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
18616 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
18617 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18618 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
18619 {
18620 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18621 {
18622 cp_lexer_consume_token (parser->lexer);
18623 cp_parser_skip_to_closing_parenthesis (parser,
18624 /*recovering*/false,
18625 /*or_comma*/false,
18626 /*consume_paren*/true);
18627 continue;
18628 }
18629
18630 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
18631 {
18632 have_trailing_return_fn_decl = true;
18633 break;
18634 }
18635
18636 cp_lexer_consume_token (parser->lexer);
18637 }
18638 cp_parser_abort_tentative_parse (parser);
18639
18640 if (have_trailing_return_fn_decl)
18641 {
18642 type = make_auto ();
18643 break;
18644 }
18645
18646 if (cxx_dialect >= cxx14)
18647 {
18648 type = synthesize_implicit_template_parm (parser, NULL_TREE);
18649 type = TREE_TYPE (type);
18650 }
18651 else
18652 type = error_mark_node;
18653
18654 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
18655 {
18656 if (cxx_dialect < cxx14)
18657 error_at (token->location,
18658 "use of %<auto%> in lambda parameter declaration "
18659 "only available with "
18660 "%<-std=c++14%> or %<-std=gnu++14%>");
18661 }
18662 else if (cxx_dialect < cxx14)
18663 error_at (token->location,
18664 "use of %<auto%> in parameter declaration "
18665 "only available with "
18666 "%<-std=c++14%> or %<-std=gnu++14%>");
18667 else if (!flag_concepts)
18668 pedwarn (token->location, 0,
18669 "use of %<auto%> in parameter declaration "
18670 "only available with %<-fconcepts-ts%>");
18671 }
18672 else
18673 type = make_auto ();
18674 break;
18675
18676 case RID_DECLTYPE:
18677 /* Since DR 743, decltype can either be a simple-type-specifier by
18678 itself or begin a nested-name-specifier. Parsing it will replace
18679 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
18680 handling below decide what to do. */
18681 cp_parser_decltype (parser);
18682 cp_lexer_set_token_position (parser->lexer, token);
18683 break;
18684
18685 case RID_TYPEOF:
18686 /* Consume the `typeof' token. */
18687 cp_lexer_consume_token (parser->lexer);
18688 /* Parse the operand to `typeof'. */
18689 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
18690 /* If it is not already a TYPE, take its type. */
18691 if (!TYPE_P (type))
18692 type = finish_typeof (type);
18693
18694 if (decl_specs)
18695 cp_parser_set_decl_spec_type (decl_specs, type,
18696 token,
18697 /*type_definition_p=*/false);
18698
18699 return type;
18700
18701 case RID_UNDERLYING_TYPE:
18702 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
18703 if (decl_specs)
18704 cp_parser_set_decl_spec_type (decl_specs, type,
18705 token,
18706 /*type_definition_p=*/false);
18707
18708 return type;
18709
18710 case RID_BASES:
18711 case RID_DIRECT_BASES:
18712 type = cp_parser_trait_expr (parser, token->keyword);
18713 if (decl_specs)
18714 cp_parser_set_decl_spec_type (decl_specs, type,
18715 token,
18716 /*type_definition_p=*/false);
18717 return type;
18718 default:
18719 break;
18720 }
18721
18722 /* If token is an already-parsed decltype not followed by ::,
18723 it's a simple-type-specifier. */
18724 if (token->type == CPP_DECLTYPE
18725 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
18726 {
18727 type = saved_checks_value (token->u.tree_check_value);
18728 if (decl_specs)
18729 {
18730 cp_parser_set_decl_spec_type (decl_specs, type,
18731 token,
18732 /*type_definition_p=*/false);
18733 /* Remember that we are handling a decltype in order to
18734 implement the resolution of DR 1510 when the argument
18735 isn't instantiation dependent. */
18736 decl_specs->decltype_p = true;
18737 }
18738 cp_lexer_consume_token (parser->lexer);
18739 return type;
18740 }
18741
18742 /* If the type-specifier was for a built-in type, we're done. */
18743 if (type)
18744 {
18745 /* Record the type. */
18746 if (decl_specs
18747 && (token->keyword != RID_SIGNED
18748 && token->keyword != RID_UNSIGNED
18749 && token->keyword != RID_SHORT
18750 && token->keyword != RID_LONG))
18751 cp_parser_set_decl_spec_type (decl_specs,
18752 type,
18753 token,
18754 /*type_definition_p=*/false);
18755 if (decl_specs)
18756 decl_specs->any_specifiers_p = true;
18757
18758 /* Consume the token. */
18759 cp_lexer_consume_token (parser->lexer);
18760
18761 if (type == error_mark_node)
18762 return error_mark_node;
18763
18764 /* There is no valid C++ program where a non-template type is
18765 followed by a "<". That usually indicates that the user thought
18766 that the type was a template. */
18767 cp_parser_check_for_invalid_template_id (parser, type, none_type,
18768 token->location);
18769
18770 return TYPE_NAME (type);
18771 }
18772
18773 /* The type-specifier must be a user-defined type. */
18774 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
18775 {
18776 bool qualified_p;
18777 bool global_p;
18778 const bool typename_p = (cxx_dialect >= cxx20
18779 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
18780
18781 /* Don't gobble tokens or issue error messages if this is an
18782 optional type-specifier. */
18783 if (flags & CP_PARSER_FLAGS_OPTIONAL)
18784 cp_parser_parse_tentatively (parser);
18785
18786 /* Remember current tentative parsing state -- if we know we need
18787 a type, we can give better diagnostics here. */
18788 bool tent = cp_parser_parsing_tentatively (parser);
18789
18790 token = cp_lexer_peek_token (parser->lexer);
18791
18792 /* Look for the optional `::' operator. */
18793 global_p
18794 = (cp_parser_global_scope_opt (parser,
18795 /*current_scope_valid_p=*/false)
18796 != NULL_TREE);
18797 /* Look for the nested-name specifier. */
18798 qualified_p
18799 = (cp_parser_nested_name_specifier_opt (parser,
18800 /*typename_keyword_p=*/false,
18801 /*check_dependency_p=*/true,
18802 /*type_p=*/false,
18803 /*is_declaration=*/false)
18804 != NULL_TREE);
18805 /* If we have seen a nested-name-specifier, and the next token
18806 is `template', then we are using the template-id production. */
18807 if (parser->scope
18808 && cp_parser_optional_template_keyword (parser))
18809 {
18810 /* Look for the template-id. */
18811 type = cp_parser_template_id (parser,
18812 /*template_keyword_p=*/true,
18813 /*check_dependency_p=*/true,
18814 none_type,
18815 /*is_declaration=*/false);
18816 /* If the template-id did not name a type, we are out of
18817 luck. */
18818 if (TREE_CODE (type) != TYPE_DECL)
18819 {
18820 /* ...unless we pretend we have seen 'typename'. */
18821 if (typename_p)
18822 type = cp_parser_make_typename_type (parser, type,
18823 token->location);
18824 else
18825 {
18826 cp_parser_error (parser, "expected template-id for type");
18827 type = error_mark_node;
18828 }
18829 }
18830 }
18831 /* DR 1812: A < following a qualified-id in a typename-specifier
18832 could safely be assumed to begin a template argument list, so
18833 the template keyword should be optional. */
18834 else if (parser->scope
18835 && qualified_p
18836 && typename_p
18837 && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
18838 {
18839 cp_parser_parse_tentatively (parser);
18840
18841 type = cp_parser_template_id (parser,
18842 /*template_keyword_p=*/true,
18843 /*check_dependency_p=*/true,
18844 none_type,
18845 /*is_declaration=*/false);
18846 /* This is handled below, so back off. */
18847 if (type && concept_check_p (type))
18848 cp_parser_simulate_error (parser);
18849
18850 if (!cp_parser_parse_definitely (parser))
18851 type = NULL_TREE;
18852 else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
18853 type = make_typename_type (parser->scope, type, typename_type,
18854 /*complain=*/tf_error);
18855 else if (TREE_CODE (type) != TYPE_DECL)
18856 type = NULL_TREE;
18857 }
18858
18859 /* Otherwise, look for a type-name. */
18860 if (!type)
18861 {
18862 if (cxx_dialect >= cxx17)
18863 cp_parser_parse_tentatively (parser);
18864
18865 type = cp_parser_type_name (parser, (qualified_p && typename_p));
18866
18867 if (cxx_dialect >= cxx17 && !cp_parser_parse_definitely (parser))
18868 type = NULL_TREE;
18869 }
18870
18871 if (!type && flag_concepts && decl_specs)
18872 {
18873 /* Try for a type-constraint with template arguments. We check
18874 decl_specs here to avoid trying this for a functional cast. */
18875
18876 cp_parser_parse_tentatively (parser);
18877
18878 type = cp_parser_template_id (parser,
18879 /*template_keyword_p=*/false,
18880 /*check_dependency_p=*/true,
18881 none_type,
18882 /*is_declaration=*/false);
18883 if (type && concept_check_p (type))
18884 {
18885 location_t loc = EXPR_LOCATION (type);
18886 type = cp_parser_placeholder_type_specifier (parser, loc,
18887 type, tent);
18888 if (tent && type == error_mark_node)
18889 /* Perhaps it's a concept-check expression. */
18890 cp_parser_simulate_error (parser);
18891 }
18892 else
18893 cp_parser_simulate_error (parser);
18894
18895 if (!cp_parser_parse_definitely (parser))
18896 type = NULL_TREE;
18897 }
18898
18899 if (!type && cxx_dialect >= cxx17)
18900 {
18901 /* Try class template argument deduction or type-constraint without
18902 template arguments. */
18903 tree name = cp_parser_identifier (parser);
18904 if (name && TREE_CODE (name) == IDENTIFIER_NODE
18905 && parser->scope != error_mark_node)
18906 {
18907 location_t loc
18908 = cp_lexer_previous_token (parser->lexer)->location;
18909 tree tmpl = cp_parser_lookup_name (parser, name,
18910 none_type,
18911 /*is_template=*/false,
18912 /*is_namespace=*/false,
18913 /*check_dependency=*/true,
18914 /*ambiguous_decls=*/NULL,
18915 token->location);
18916 if (tmpl && tmpl != error_mark_node
18917 && ctad_template_p (tmpl))
18918 type = make_template_placeholder (tmpl);
18919 else if (flag_concepts && tmpl && concept_definition_p (tmpl))
18920 type = cp_parser_placeholder_type_specifier (parser, loc,
18921 tmpl, tent);
18922 else
18923 {
18924 type = error_mark_node;
18925 if (!cp_parser_simulate_error (parser))
18926 cp_parser_name_lookup_error (parser, name, tmpl,
18927 NLE_TYPE, token->location);
18928 }
18929 }
18930 else
18931 type = error_mark_node;
18932 }
18933
18934 /* If it didn't work out, we don't have a TYPE. */
18935 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
18936 && !cp_parser_parse_definitely (parser))
18937 type = NULL_TREE;
18938
18939 /* Keep track of all name-lookups performed in class scopes. */
18940 if (type
18941 && !global_p
18942 && !qualified_p
18943 && TREE_CODE (type) == TYPE_DECL
18944 && identifier_p (DECL_NAME (type)))
18945 maybe_note_name_used_in_class (DECL_NAME (type), type);
18946
18947 if (type && decl_specs)
18948 cp_parser_set_decl_spec_type (decl_specs, type,
18949 token,
18950 /*type_definition_p=*/false);
18951 }
18952
18953 /* If we didn't get a type-name, issue an error message. */
18954 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
18955 {
18956 cp_parser_error (parser, "expected type-name");
18957 return error_mark_node;
18958 }
18959
18960 if (type && type != error_mark_node)
18961 {
18962 /* See if TYPE is an Objective-C type, and if so, parse and
18963 accept any protocol references following it. Do this before
18964 the cp_parser_check_for_invalid_template_id() call, because
18965 Objective-C types can be followed by '<...>' which would
18966 enclose protocol names rather than template arguments, and so
18967 everything is fine. */
18968 if (c_dialect_objc () && !parser->scope
18969 && (objc_is_id (type) || objc_is_class_name (type)))
18970 {
18971 tree protos = cp_parser_objc_protocol_refs_opt (parser);
18972 tree qual_type = objc_get_protocol_qualified_type (type, protos);
18973
18974 /* Clobber the "unqualified" type previously entered into
18975 DECL_SPECS with the new, improved protocol-qualified version. */
18976 if (decl_specs)
18977 decl_specs->type = qual_type;
18978
18979 return qual_type;
18980 }
18981
18982 /* There is no valid C++ program where a non-template type is
18983 followed by a "<". That usually indicates that the user
18984 thought that the type was a template. */
18985 cp_parser_check_for_invalid_template_id (parser, type,
18986 none_type,
18987 token->location);
18988 }
18989
18990 return type;
18991 }
18992
18993 /* Parse the remainder of a placholder-type-specifier.
18994
18995 placeholder-type-specifier:
18996 type-constraint_opt auto
18997 type-constraint_opt decltype(auto)
18998
18999 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
19000 and passed as TMPL. This function converts TMPL to an actual type-constraint,
19001 parses the placeholder type, and performs some contextual syntactic analysis.
19002
19003 LOC provides the location of the template name.
19004
19005 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
19006 don't give an error if TMPL isn't a valid type-constraint, as the template-id
19007 might actually be a concept-check,
19008
19009 Note that the Concepts TS allows the auto or decltype(auto) to be
19010 omitted in a constrained-type-specifier. */
19011
19012 tree
19013 cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
19014 tree tmpl, bool tentative)
19015 {
19016 if (tmpl == error_mark_node)
19017 return error_mark_node;
19018
19019 tree orig_tmpl = tmpl;
19020
19021 /* Get the arguments as written for subsequent analysis. */
19022 tree args = NULL_TREE;
19023 if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
19024 {
19025 args = TREE_OPERAND (tmpl, 1);
19026 tmpl = TREE_OPERAND (tmpl, 0);
19027 }
19028 if (args == NULL_TREE)
19029 /* A concept-name with no arguments can't be an expression. */
19030 tentative = false;
19031
19032 tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
19033
19034 /* Get the concept and prototype parameter for the constraint. */
19035 tree_pair info = finish_type_constraints (tmpl, args, complain);
19036 tree con = info.first;
19037 tree proto = info.second;
19038 if (con == error_mark_node)
19039 return error_mark_node;
19040
19041 /* As per the standard, require auto or decltype(auto), except in some
19042 cases (template parameter lists, -fconcepts-ts enabled). */
19043 cp_token *placeholder = NULL, *close_paren = NULL;
19044 if (cxx_dialect >= cxx20)
19045 {
19046 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
19047 placeholder = cp_lexer_consume_token (parser->lexer);
19048 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
19049 {
19050 placeholder = cp_lexer_consume_token (parser->lexer);
19051 matching_parens parens;
19052 parens.require_open (parser);
19053 cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
19054 close_paren = parens.require_close (parser);
19055 }
19056 }
19057
19058 /* A type constraint constrains a contextually determined type or type
19059 parameter pack. However, the Concepts TS does allow concepts
19060 to introduce non-type and template template parameters. */
19061 if (TREE_CODE (proto) != TYPE_DECL)
19062 {
19063 if (!flag_concepts_ts
19064 || !processing_template_parmlist)
19065 {
19066 error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
19067 inform (DECL_SOURCE_LOCATION (con), "concept defined here");
19068 return error_mark_node;
19069 }
19070 }
19071
19072 /* In a template parameter list, a type-parameter can be introduced
19073 by type-constraints alone. */
19074 if (processing_template_parmlist && !placeholder)
19075 return build_constrained_parameter (con, proto, args);
19076
19077 /* Diagnose issues placeholder issues. */
19078 if (!flag_concepts_ts
19079 && !parser->in_result_type_constraint_p
19080 && !placeholder)
19081 {
19082 if (tentative)
19083 /* Perhaps it's a concept-check expression (c++/91073). */
19084 return error_mark_node;
19085
19086 tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
19087 tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
19088 error_at (input_location,
19089 "expected %<auto%> or %<decltype(auto)%> after %qE", expr);
19090 /* Fall through. This is an error of omission. */
19091 }
19092 else if (parser->in_result_type_constraint_p && placeholder)
19093 {
19094 /* A trailing return type only allows type-constraints. */
19095 error_at (input_location,
19096 "unexpected placeholder in constrained result type");
19097 }
19098
19099 /* In a parameter-declaration-clause, a placeholder-type-specifier
19100 results in an invented template parameter. */
19101 if (parser->auto_is_implicit_function_template_parm_p)
19102 {
19103 if (close_paren)
19104 {
19105 location_t loc = make_location (placeholder->location,
19106 placeholder->location,
19107 close_paren->location);
19108 error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
19109 return error_mark_node;
19110 }
19111 tree parm = build_constrained_parameter (con, proto, args);
19112 return synthesize_implicit_template_parm (parser, parm);
19113 }
19114
19115 /* Determine if the type should be deduced using template argument
19116 deduction or decltype deduction. Note that the latter is always
19117 used for type-constraints in trailing return types. */
19118 bool decltype_p = placeholder
19119 ? placeholder->keyword == RID_DECLTYPE
19120 : parser->in_result_type_constraint_p;
19121
19122 /* Otherwise, this is the type of a variable or return type. */
19123 if (decltype_p)
19124 return make_constrained_decltype_auto (con, args);
19125 else
19126 return make_constrained_auto (con, args);
19127 }
19128
19129 /* Parse a type-name.
19130
19131 type-name:
19132 class-name
19133 enum-name
19134 typedef-name
19135 simple-template-id [in c++0x]
19136
19137 enum-name:
19138 identifier
19139
19140 typedef-name:
19141 identifier
19142
19143 Concepts:
19144
19145 type-name:
19146 concept-name
19147 partial-concept-id
19148
19149 concept-name:
19150 identifier
19151
19152 Returns a TYPE_DECL for the type. */
19153
19154 static tree
19155 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
19156 {
19157 tree type_decl;
19158
19159 /* We can't know yet whether it is a class-name or not. */
19160 cp_parser_parse_tentatively (parser);
19161 /* Try a class-name. */
19162 type_decl = cp_parser_class_name (parser,
19163 typename_keyword_p,
19164 /*template_keyword_p=*/false,
19165 none_type,
19166 /*check_dependency_p=*/true,
19167 /*class_head_p=*/false,
19168 /*is_declaration=*/false);
19169 /* If it's not a class-name, keep looking. */
19170 if (!cp_parser_parse_definitely (parser))
19171 {
19172 if (cxx_dialect < cxx11)
19173 /* It must be a typedef-name or an enum-name. */
19174 return cp_parser_nonclass_name (parser);
19175
19176 cp_parser_parse_tentatively (parser);
19177 /* It is either a simple-template-id representing an
19178 instantiation of an alias template... */
19179 type_decl = cp_parser_template_id (parser,
19180 /*template_keyword_p=*/false,
19181 /*check_dependency_p=*/true,
19182 none_type,
19183 /*is_declaration=*/false);
19184 /* Note that this must be an instantiation of an alias template
19185 because [temp.names]/6 says:
19186
19187 A template-id that names an alias template specialization
19188 is a type-name.
19189
19190 Whereas [temp.names]/7 says:
19191
19192 A simple-template-id that names a class template
19193 specialization is a class-name.
19194
19195 With concepts, this could also be a partial-concept-id that
19196 declares a non-type template parameter. */
19197 if (type_decl != NULL_TREE
19198 && TREE_CODE (type_decl) == TYPE_DECL
19199 && TYPE_DECL_ALIAS_P (type_decl))
19200 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
19201 else
19202 cp_parser_simulate_error (parser);
19203
19204 if (!cp_parser_parse_definitely (parser))
19205 /* ... Or a typedef-name or an enum-name. */
19206 return cp_parser_nonclass_name (parser);
19207 }
19208
19209 return type_decl;
19210 }
19211
19212 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
19213 or a concept-name.
19214
19215 enum-name:
19216 identifier
19217
19218 typedef-name:
19219 identifier
19220
19221 concept-name:
19222 identifier
19223
19224 Returns a TYPE_DECL for the type. */
19225
19226 static tree
19227 cp_parser_nonclass_name (cp_parser* parser)
19228 {
19229 tree type_decl;
19230 tree identifier;
19231
19232 cp_token *token = cp_lexer_peek_token (parser->lexer);
19233 identifier = cp_parser_identifier (parser);
19234 if (identifier == error_mark_node)
19235 return error_mark_node;
19236
19237 /* Look up the type-name. */
19238 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
19239
19240 type_decl = strip_using_decl (type_decl);
19241
19242 if (TREE_CODE (type_decl) != TYPE_DECL
19243 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
19244 {
19245 /* See if this is an Objective-C type. */
19246 tree protos = cp_parser_objc_protocol_refs_opt (parser);
19247 tree type = objc_get_protocol_qualified_type (identifier, protos);
19248 if (type)
19249 type_decl = TYPE_NAME (type);
19250 }
19251
19252 /* Issue an error if we did not find a type-name. */
19253 if (TREE_CODE (type_decl) != TYPE_DECL
19254 /* In Objective-C, we have the complication that class names are
19255 normally type names and start declarations (eg, the
19256 "NSObject" in "NSObject *object;"), but can be used in an
19257 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
19258 is an expression. So, a classname followed by a dot is not a
19259 valid type-name. */
19260 || (objc_is_class_name (TREE_TYPE (type_decl))
19261 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
19262 {
19263 if (!cp_parser_simulate_error (parser))
19264 cp_parser_name_lookup_error (parser, identifier, type_decl,
19265 NLE_TYPE, token->location);
19266 return error_mark_node;
19267 }
19268 /* Remember that the name was used in the definition of the
19269 current class so that we can check later to see if the
19270 meaning would have been different after the class was
19271 entirely defined. */
19272 else if (type_decl != error_mark_node
19273 && !parser->scope)
19274 maybe_note_name_used_in_class (identifier, type_decl);
19275
19276 return type_decl;
19277 }
19278
19279 /* Parse an elaborated-type-specifier. Note that the grammar given
19280 here incorporates the resolution to DR68.
19281
19282 elaborated-type-specifier:
19283 class-key :: [opt] nested-name-specifier [opt] identifier
19284 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
19285 enum-key :: [opt] nested-name-specifier [opt] identifier
19286 typename :: [opt] nested-name-specifier identifier
19287 typename :: [opt] nested-name-specifier template [opt]
19288 template-id
19289
19290 GNU extension:
19291
19292 elaborated-type-specifier:
19293 class-key attributes :: [opt] nested-name-specifier [opt] identifier
19294 class-key attributes :: [opt] nested-name-specifier [opt]
19295 template [opt] template-id
19296 enum attributes :: [opt] nested-name-specifier [opt] identifier
19297
19298 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
19299 declared `friend'. If IS_DECLARATION is TRUE, then this
19300 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
19301 something is being declared.
19302
19303 Returns the TYPE specified. */
19304
19305 static tree
19306 cp_parser_elaborated_type_specifier (cp_parser* parser,
19307 bool is_friend,
19308 bool is_declaration)
19309 {
19310 enum tag_types tag_type;
19311 tree identifier;
19312 tree type = NULL_TREE;
19313 tree attributes = NULL_TREE;
19314 tree globalscope;
19315 cp_token *token = NULL;
19316
19317 /* For class and enum types the location of the class-key or enum-key. */
19318 location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
19319 /* For a scoped enum, the 'class' or 'struct' keyword id. */
19320 rid scoped_key = RID_MAX;
19321
19322 /* See if we're looking at the `enum' keyword. */
19323 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
19324 {
19325 /* Consume the `enum' token. */
19326 cp_lexer_consume_token (parser->lexer);
19327 /* Remember that it's an enumeration type. */
19328 tag_type = enum_type;
19329 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
19330 enums) is used here. */
19331 cp_token *token = cp_lexer_peek_token (parser->lexer);
19332 if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
19333 || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
19334 {
19335 location_t loc = token->location;
19336 gcc_rich_location richloc (loc);
19337 richloc.add_range (input_location);
19338 richloc.add_fixit_remove ();
19339 pedwarn (&richloc, 0, "elaborated-type-specifier for "
19340 "a scoped enum must not use the %qD keyword",
19341 token->u.value);
19342 /* Consume the `struct' or `class' and parse it anyway. */
19343 cp_lexer_consume_token (parser->lexer);
19344 /* Create a combined location for the whole scoped-enum-key. */
19345 key_loc = make_location (key_loc, key_loc, loc);
19346 }
19347 else
19348 scoped_key = RID_MAX;
19349
19350 /* Parse the attributes. */
19351 attributes = cp_parser_attributes_opt (parser);
19352 }
19353 /* Or, it might be `typename'. */
19354 else if (cp_lexer_next_token_is_keyword (parser->lexer,
19355 RID_TYPENAME))
19356 {
19357 /* Consume the `typename' token. */
19358 cp_lexer_consume_token (parser->lexer);
19359 /* Remember that it's a `typename' type. */
19360 tag_type = typename_type;
19361 }
19362 /* Otherwise it must be a class-key. */
19363 else
19364 {
19365 key_loc = cp_lexer_peek_token (parser->lexer)->location;
19366 tag_type = cp_parser_class_key (parser);
19367 if (tag_type == none_type)
19368 return error_mark_node;
19369 /* Parse the attributes. */
19370 attributes = cp_parser_attributes_opt (parser);
19371 }
19372
19373 /* Look for the `::' operator. */
19374 globalscope = cp_parser_global_scope_opt (parser,
19375 /*current_scope_valid_p=*/false);
19376 /* Look for the nested-name-specifier. */
19377 tree nested_name_specifier;
19378 if (tag_type == typename_type && !globalscope)
19379 {
19380 nested_name_specifier
19381 = cp_parser_nested_name_specifier (parser,
19382 /*typename_keyword_p=*/true,
19383 /*check_dependency_p=*/true,
19384 /*type_p=*/true,
19385 is_declaration);
19386 if (!nested_name_specifier)
19387 return error_mark_node;
19388 }
19389 else
19390 /* Even though `typename' is not present, the proposed resolution
19391 to Core Issue 180 says that in `class A<T>::B', `B' should be
19392 considered a type-name, even if `A<T>' is dependent. */
19393 nested_name_specifier
19394 = cp_parser_nested_name_specifier_opt (parser,
19395 /*typename_keyword_p=*/true,
19396 /*check_dependency_p=*/true,
19397 /*type_p=*/true,
19398 is_declaration);
19399 /* For everything but enumeration types, consider a template-id.
19400 For an enumeration type, consider only a plain identifier. */
19401 if (tag_type != enum_type)
19402 {
19403 bool template_p = false;
19404 tree decl;
19405
19406 /* Allow the `template' keyword. */
19407 template_p = cp_parser_optional_template_keyword (parser);
19408 /* If we didn't see `template', we don't know if there's a
19409 template-id or not. */
19410 if (!template_p)
19411 cp_parser_parse_tentatively (parser);
19412 /* The `template' keyword must follow a nested-name-specifier. */
19413 else if (!nested_name_specifier && !globalscope)
19414 {
19415 cp_parser_error (parser, "%<template%> must follow a nested-"
19416 "name-specifier");
19417 return error_mark_node;
19418 }
19419
19420 /* Parse the template-id. */
19421 token = cp_lexer_peek_token (parser->lexer);
19422 decl = cp_parser_template_id (parser, template_p,
19423 /*check_dependency_p=*/true,
19424 tag_type,
19425 is_declaration);
19426 /* If we didn't find a template-id, look for an ordinary
19427 identifier. */
19428 if (!template_p && !cp_parser_parse_definitely (parser))
19429 ;
19430 /* We can get here when cp_parser_template_id, called by
19431 cp_parser_class_name with tag_type == none_type, succeeds
19432 and caches a BASELINK. Then, when called again here,
19433 instead of failing and returning an error_mark_node
19434 returns it (see template/typename17.C in C++11).
19435 ??? Could we diagnose this earlier? */
19436 else if (tag_type == typename_type && BASELINK_P (decl))
19437 {
19438 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
19439 type = error_mark_node;
19440 }
19441 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
19442 in effect, then we must assume that, upon instantiation, the
19443 template will correspond to a class. */
19444 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19445 && tag_type == typename_type)
19446 type = make_typename_type (parser->scope, decl,
19447 typename_type,
19448 /*complain=*/tf_error);
19449 /* If the `typename' keyword is in effect and DECL is not a type
19450 decl, then type is non existent. */
19451 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
19452 ;
19453 else if (TREE_CODE (decl) == TYPE_DECL)
19454 {
19455 type = check_elaborated_type_specifier (tag_type, decl,
19456 /*allow_template_p=*/true);
19457
19458 /* If the next token is a semicolon, this must be a specialization,
19459 instantiation, or friend declaration. Check the scope while we
19460 still know whether or not we had a nested-name-specifier. */
19461 if (type != error_mark_node
19462 && !nested_name_specifier && !is_friend
19463 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19464 check_unqualified_spec_or_inst (type, token->location);
19465 }
19466 else if (decl == error_mark_node)
19467 type = error_mark_node;
19468 }
19469
19470 if (!type)
19471 {
19472 token = cp_lexer_peek_token (parser->lexer);
19473 identifier = cp_parser_identifier (parser);
19474
19475 if (identifier == error_mark_node)
19476 {
19477 parser->scope = NULL_TREE;
19478 return error_mark_node;
19479 }
19480
19481 /* For a `typename', we needn't call xref_tag. */
19482 if (tag_type == typename_type
19483 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
19484 return cp_parser_make_typename_type (parser, identifier,
19485 token->location);
19486
19487 /* Template parameter lists apply only if we are not within a
19488 function parameter list. */
19489 bool template_parm_lists_apply
19490 = parser->num_template_parameter_lists;
19491 if (template_parm_lists_apply)
19492 for (cp_binding_level *s = current_binding_level;
19493 s && s->kind != sk_template_parms;
19494 s = s->level_chain)
19495 if (s->kind == sk_function_parms)
19496 template_parm_lists_apply = false;
19497
19498 /* Look up a qualified name in the usual way. */
19499 if (parser->scope)
19500 {
19501 tree decl;
19502 tree ambiguous_decls;
19503
19504 decl = cp_parser_lookup_name (parser, identifier,
19505 tag_type,
19506 /*is_template=*/false,
19507 /*is_namespace=*/false,
19508 /*check_dependency=*/true,
19509 &ambiguous_decls,
19510 token->location);
19511
19512 /* If the lookup was ambiguous, an error will already have been
19513 issued. */
19514 if (ambiguous_decls)
19515 return error_mark_node;
19516
19517 /* If we are parsing friend declaration, DECL may be a
19518 TEMPLATE_DECL tree node here. However, we need to check
19519 whether this TEMPLATE_DECL results in valid code. Consider
19520 the following example:
19521
19522 namespace N {
19523 template <class T> class C {};
19524 }
19525 class X {
19526 template <class T> friend class N::C; // #1, valid code
19527 };
19528 template <class T> class Y {
19529 friend class N::C; // #2, invalid code
19530 };
19531
19532 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
19533 name lookup of `N::C'. We see that friend declaration must
19534 be template for the code to be valid. Note that
19535 processing_template_decl does not work here since it is
19536 always 1 for the above two cases. */
19537
19538 decl = (cp_parser_maybe_treat_template_as_class
19539 (decl, /*tag_name_p=*/is_friend
19540 && template_parm_lists_apply));
19541
19542 if (TREE_CODE (decl) != TYPE_DECL)
19543 {
19544 cp_parser_diagnose_invalid_type_name (parser,
19545 identifier,
19546 token->location);
19547 return error_mark_node;
19548 }
19549
19550 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
19551 {
19552 bool allow_template = (template_parm_lists_apply
19553 || DECL_SELF_REFERENCE_P (decl));
19554 type = check_elaborated_type_specifier (tag_type, decl,
19555 allow_template);
19556
19557 if (type == error_mark_node)
19558 return error_mark_node;
19559 }
19560
19561 /* Forward declarations of nested types, such as
19562
19563 class C1::C2;
19564 class C1::C2::C3;
19565
19566 are invalid unless all components preceding the final '::'
19567 are complete. If all enclosing types are complete, these
19568 declarations become merely pointless.
19569
19570 Invalid forward declarations of nested types are errors
19571 caught elsewhere in parsing. Those that are pointless arrive
19572 here. */
19573
19574 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19575 && !is_friend && is_declaration
19576 && !processing_explicit_instantiation)
19577 warning (0, "declaration %qD does not declare anything", decl);
19578
19579 type = TREE_TYPE (decl);
19580 }
19581 else
19582 {
19583 /* An elaborated-type-specifier sometimes introduces a new type and
19584 sometimes names an existing type. Normally, the rule is that it
19585 introduces a new type only if there is not an existing type of
19586 the same name already in scope. For example, given:
19587
19588 struct S {};
19589 void f() { struct S s; }
19590
19591 the `struct S' in the body of `f' is the same `struct S' as in
19592 the global scope; the existing definition is used. However, if
19593 there were no global declaration, this would introduce a new
19594 local class named `S'.
19595
19596 An exception to this rule applies to the following code:
19597
19598 namespace N { struct S; }
19599
19600 Here, the elaborated-type-specifier names a new type
19601 unconditionally; even if there is already an `S' in the
19602 containing scope this declaration names a new type.
19603 This exception only applies if the elaborated-type-specifier
19604 forms the complete declaration:
19605
19606 [class.name]
19607
19608 A declaration consisting solely of `class-key identifier ;' is
19609 either a redeclaration of the name in the current scope or a
19610 forward declaration of the identifier as a class name. It
19611 introduces the name into the current scope.
19612
19613 We are in this situation precisely when the next token is a `;'.
19614
19615 An exception to the exception is that a `friend' declaration does
19616 *not* name a new type; i.e., given:
19617
19618 struct S { friend struct T; };
19619
19620 `T' is not a new type in the scope of `S'.
19621
19622 Also, `new struct S' or `sizeof (struct S)' never results in the
19623 definition of a new type; a new type can only be declared in a
19624 declaration context. */
19625
19626 TAG_how how;
19627
19628 if (is_friend)
19629 /* Friends have special name lookup rules. */
19630 how = TAG_how::HIDDEN_FRIEND;
19631 else if (is_declaration
19632 && cp_lexer_next_token_is (parser->lexer,
19633 CPP_SEMICOLON))
19634 /* This is a `class-key identifier ;' */
19635 how = TAG_how::CURRENT_ONLY;
19636 else
19637 how = TAG_how::GLOBAL;
19638
19639 bool template_p =
19640 (template_parm_lists_apply
19641 && (cp_parser_next_token_starts_class_definition_p (parser)
19642 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
19643 /* An unqualified name was used to reference this type, so
19644 there were no qualifying templates. */
19645 if (template_parm_lists_apply
19646 && !cp_parser_check_template_parameters (parser,
19647 /*num_templates=*/0,
19648 /*template_id*/false,
19649 token->location,
19650 /*declarator=*/NULL))
19651 return error_mark_node;
19652
19653 type = xref_tag (tag_type, identifier, how, template_p);
19654 }
19655 }
19656
19657 if (type == error_mark_node)
19658 return error_mark_node;
19659
19660 /* Allow attributes on forward declarations of classes. */
19661 if (attributes)
19662 {
19663 if (TREE_CODE (type) == TYPENAME_TYPE)
19664 warning (OPT_Wattributes,
19665 "attributes ignored on uninstantiated type");
19666 else if (tag_type != enum_type
19667 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM
19668 && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
19669 && ! processing_explicit_instantiation)
19670 warning (OPT_Wattributes,
19671 "attributes ignored on template instantiation");
19672 else if (is_declaration && cp_parser_declares_only_class_p (parser))
19673 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
19674 else
19675 warning (OPT_Wattributes,
19676 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
19677 }
19678
19679 if (tag_type == enum_type)
19680 cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
19681 else
19682 {
19683 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
19684 for alias definition. */
19685 bool decl_class = (is_declaration
19686 && cp_parser_declares_only_class_p (parser));
19687 cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
19688 decl_class);
19689
19690 /* Indicate whether this class was declared as a `class' or as a
19691 `struct'. */
19692 if (CLASS_TYPE_P (type) && !currently_open_class (type))
19693 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
19694 }
19695
19696 /* A "<" cannot follow an elaborated type specifier. If that
19697 happens, the user was probably trying to form a template-id. */
19698 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
19699 token->location);
19700
19701 return type;
19702 }
19703
19704 /* Parse an enum-specifier.
19705
19706 enum-specifier:
19707 enum-head { enumerator-list [opt] }
19708 enum-head { enumerator-list , } [C++0x]
19709
19710 enum-head:
19711 enum-key identifier [opt] enum-base [opt]
19712 enum-key nested-name-specifier identifier enum-base [opt]
19713
19714 enum-key:
19715 enum
19716 enum class [C++0x]
19717 enum struct [C++0x]
19718
19719 enum-base: [C++0x]
19720 : type-specifier-seq
19721
19722 opaque-enum-specifier:
19723 enum-key identifier enum-base [opt] ;
19724
19725 GNU Extensions:
19726 enum-key attributes[opt] identifier [opt] enum-base [opt]
19727 { enumerator-list [opt] }attributes[opt]
19728 enum-key attributes[opt] identifier [opt] enum-base [opt]
19729 { enumerator-list, }attributes[opt] [C++0x]
19730
19731 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
19732 if the token stream isn't an enum-specifier after all. */
19733
19734 static tree
19735 cp_parser_enum_specifier (cp_parser* parser)
19736 {
19737 tree identifier;
19738 tree type = NULL_TREE;
19739 tree prev_scope;
19740 tree nested_name_specifier = NULL_TREE;
19741 tree attributes;
19742 bool scoped_enum_p = false;
19743 bool has_underlying_type = false;
19744 bool nested_being_defined = false;
19745 bool new_value_list = false;
19746 bool is_new_type = false;
19747 bool is_unnamed = false;
19748 tree underlying_type = NULL_TREE;
19749 cp_token *type_start_token = NULL;
19750 auto cleanup = make_temp_override (parser->colon_corrects_to_scope_p, false);
19751
19752 /* Parse tentatively so that we can back up if we don't find a
19753 enum-specifier. */
19754 cp_parser_parse_tentatively (parser);
19755
19756 /* Caller guarantees that the current token is 'enum', an identifier
19757 possibly follows, and the token after that is an opening brace.
19758 If we don't have an identifier, fabricate an anonymous name for
19759 the enumeration being defined. */
19760 cp_lexer_consume_token (parser->lexer);
19761
19762 /* Parse the "class" or "struct", which indicates a scoped
19763 enumeration type in C++0x. */
19764 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
19765 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
19766 {
19767 if (cxx_dialect < cxx11)
19768 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
19769
19770 /* Consume the `struct' or `class' token. */
19771 cp_lexer_consume_token (parser->lexer);
19772
19773 scoped_enum_p = true;
19774 }
19775
19776 attributes = cp_parser_attributes_opt (parser);
19777
19778 /* Clear the qualification. */
19779 parser->scope = NULL_TREE;
19780 parser->qualifying_scope = NULL_TREE;
19781 parser->object_scope = NULL_TREE;
19782
19783 /* Figure out in what scope the declaration is being placed. */
19784 prev_scope = current_scope ();
19785
19786 type_start_token = cp_lexer_peek_token (parser->lexer);
19787
19788 push_deferring_access_checks (dk_no_check);
19789 nested_name_specifier
19790 = cp_parser_nested_name_specifier_opt (parser,
19791 /*typename_keyword_p=*/true,
19792 /*check_dependency_p=*/false,
19793 /*type_p=*/false,
19794 /*is_declaration=*/false);
19795
19796 if (nested_name_specifier)
19797 {
19798 tree name;
19799
19800 identifier = cp_parser_identifier (parser);
19801 name = cp_parser_lookup_name (parser, identifier,
19802 enum_type,
19803 /*is_template=*/false,
19804 /*is_namespace=*/false,
19805 /*check_dependency=*/true,
19806 /*ambiguous_decls=*/NULL,
19807 input_location);
19808 if (name && name != error_mark_node)
19809 {
19810 type = TREE_TYPE (name);
19811 if (TREE_CODE (type) == TYPENAME_TYPE)
19812 {
19813 /* Are template enums allowed in ISO? */
19814 if (template_parm_scope_p ())
19815 pedwarn (type_start_token->location, OPT_Wpedantic,
19816 "%qD is an enumeration template", name);
19817 /* ignore a typename reference, for it will be solved by name
19818 in start_enum. */
19819 type = NULL_TREE;
19820 }
19821 }
19822 else if (nested_name_specifier == error_mark_node)
19823 /* We already issued an error. */;
19824 else
19825 {
19826 error_at (type_start_token->location,
19827 "%qD does not name an enumeration in %qT",
19828 identifier, nested_name_specifier);
19829 nested_name_specifier = error_mark_node;
19830 }
19831 }
19832 else
19833 {
19834 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19835 identifier = cp_parser_identifier (parser);
19836 else
19837 {
19838 identifier = make_anon_name ();
19839 is_unnamed = true;
19840 if (scoped_enum_p)
19841 error_at (type_start_token->location,
19842 "unnamed scoped enum is not allowed");
19843 }
19844 }
19845 pop_deferring_access_checks ();
19846
19847 /* Check for the `:' that denotes a specified underlying type in C++0x.
19848 Note that a ':' could also indicate a bitfield width, however. */
19849 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19850 {
19851 cp_decl_specifier_seq type_specifiers;
19852
19853 /* Consume the `:'. */
19854 cp_lexer_consume_token (parser->lexer);
19855
19856 /* Parse the type-specifier-seq. */
19857 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
19858 /*is_declaration=*/false,
19859 /*is_trailing_return=*/false,
19860 &type_specifiers);
19861
19862 /* At this point this is surely not elaborated type specifier. */
19863 if (!cp_parser_parse_definitely (parser))
19864 return NULL_TREE;
19865
19866 if (cxx_dialect < cxx11)
19867 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
19868
19869 has_underlying_type = true;
19870
19871 /* If that didn't work, stop. */
19872 if (type_specifiers.type != error_mark_node)
19873 {
19874 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
19875 /*initialized=*/0, NULL);
19876 if (underlying_type == error_mark_node
19877 || check_for_bare_parameter_packs (underlying_type))
19878 underlying_type = NULL_TREE;
19879 }
19880 }
19881
19882 /* Look for the `{' but don't consume it yet. */
19883 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19884 {
19885 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
19886 {
19887 if (has_underlying_type)
19888 cp_parser_commit_to_tentative_parse (parser);
19889 cp_parser_error (parser, "expected %<{%>");
19890 if (has_underlying_type)
19891 return error_mark_node;
19892 }
19893 /* An opaque-enum-specifier must have a ';' here. */
19894 if ((scoped_enum_p || underlying_type)
19895 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19896 {
19897 if (has_underlying_type)
19898 cp_parser_commit_to_tentative_parse (parser);
19899 cp_parser_error (parser, "expected %<;%> or %<{%>");
19900 if (has_underlying_type)
19901 return error_mark_node;
19902 }
19903 }
19904
19905 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
19906 return NULL_TREE;
19907
19908 if (nested_name_specifier)
19909 {
19910 if (CLASS_TYPE_P (nested_name_specifier))
19911 {
19912 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
19913 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
19914 push_scope (nested_name_specifier);
19915 }
19916 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
19917 push_nested_namespace (nested_name_specifier);
19918 }
19919
19920 /* Issue an error message if type-definitions are forbidden here. */
19921 if (!cp_parser_check_type_definition (parser))
19922 type = error_mark_node;
19923 else
19924 /* Create the new type. We do this before consuming the opening
19925 brace so the enum will be recorded as being on the line of its
19926 tag (or the 'enum' keyword, if there is no tag). */
19927 type = start_enum (identifier, type, underlying_type,
19928 attributes, scoped_enum_p, &is_new_type);
19929
19930 /* If the next token is not '{' it is an opaque-enum-specifier or an
19931 elaborated-type-specifier. */
19932 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19933 {
19934 timevar_push (TV_PARSE_ENUM);
19935 if (nested_name_specifier
19936 && nested_name_specifier != error_mark_node)
19937 {
19938 /* The following catches invalid code such as:
19939 enum class S<int>::E { A, B, C }; */
19940 if (!processing_specialization
19941 && CLASS_TYPE_P (nested_name_specifier)
19942 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
19943 error_at (type_start_token->location, "cannot add an enumerator "
19944 "list to a template instantiation");
19945
19946 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
19947 {
19948 error_at (type_start_token->location,
19949 "%<%T::%E%> has not been declared",
19950 TYPE_CONTEXT (nested_name_specifier),
19951 nested_name_specifier);
19952 type = error_mark_node;
19953 }
19954 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
19955 && !CLASS_TYPE_P (nested_name_specifier))
19956 {
19957 error_at (type_start_token->location, "nested name specifier "
19958 "%qT for enum declaration does not name a class "
19959 "or namespace", nested_name_specifier);
19960 type = error_mark_node;
19961 }
19962 /* If that scope does not contain the scope in which the
19963 class was originally declared, the program is invalid. */
19964 else if (prev_scope && !is_ancestor (prev_scope,
19965 nested_name_specifier))
19966 {
19967 if (at_namespace_scope_p ())
19968 error_at (type_start_token->location,
19969 "declaration of %qD in namespace %qD which does not "
19970 "enclose %qD",
19971 type, prev_scope, nested_name_specifier);
19972 else
19973 error_at (type_start_token->location,
19974 "declaration of %qD in %qD which does not "
19975 "enclose %qD",
19976 type, prev_scope, nested_name_specifier);
19977 type = error_mark_node;
19978 }
19979 /* If that scope is the scope where the declaration is being placed
19980 the program is invalid. */
19981 else if (CLASS_TYPE_P (nested_name_specifier)
19982 && CLASS_TYPE_P (prev_scope)
19983 && same_type_p (nested_name_specifier, prev_scope))
19984 {
19985 permerror (type_start_token->location,
19986 "extra qualification not allowed");
19987 nested_name_specifier = NULL_TREE;
19988 }
19989 }
19990
19991 if (scoped_enum_p)
19992 begin_scope (sk_scoped_enum, type);
19993
19994 /* Consume the opening brace. */
19995 matching_braces braces;
19996 braces.consume_open (parser);
19997
19998 if (type == error_mark_node)
19999 ; /* Nothing to add */
20000 else if (OPAQUE_ENUM_P (type)
20001 || (cxx_dialect > cxx98 && processing_specialization))
20002 {
20003 new_value_list = true;
20004 SET_OPAQUE_ENUM_P (type, false);
20005 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20006 }
20007 else
20008 {
20009 error_at (type_start_token->location,
20010 "multiple definition of %q#T", type);
20011 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
20012 "previous definition here");
20013 type = error_mark_node;
20014 }
20015
20016 if (type == error_mark_node)
20017 cp_parser_skip_to_end_of_block_or_statement (parser);
20018 /* If the next token is not '}', then there are some enumerators. */
20019 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20020 {
20021 if (is_unnamed && !scoped_enum_p)
20022 pedwarn (type_start_token->location, OPT_Wpedantic,
20023 "ISO C++ forbids empty unnamed enum");
20024 }
20025 else
20026 {
20027 /* We've seen a '{' so we know we're in an enum-specifier.
20028 Commit to any tentative parse to get syntax errors. */
20029 cp_parser_commit_to_tentative_parse (parser);
20030 cp_parser_enumerator_list (parser, type);
20031 }
20032
20033 /* Consume the final '}'. */
20034 braces.require_close (parser);
20035
20036 if (scoped_enum_p)
20037 finish_scope ();
20038 timevar_pop (TV_PARSE_ENUM);
20039 }
20040 else
20041 {
20042 /* If a ';' follows, then it is an opaque-enum-specifier
20043 and additional restrictions apply. */
20044 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20045 {
20046 if (is_unnamed)
20047 error_at (type_start_token->location,
20048 "opaque-enum-specifier without name");
20049 else if (nested_name_specifier)
20050 error_at (type_start_token->location,
20051 "opaque-enum-specifier must use a simple identifier");
20052 }
20053 }
20054
20055 /* Look for trailing attributes to apply to this enumeration, and
20056 apply them if appropriate. */
20057 if (cp_parser_allow_gnu_extensions_p (parser))
20058 {
20059 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
20060 cplus_decl_attributes (&type,
20061 trailing_attr,
20062 (int) ATTR_FLAG_TYPE_IN_PLACE);
20063 }
20064
20065 /* Finish up the enumeration. */
20066 if (type != error_mark_node)
20067 {
20068 if (new_value_list)
20069 finish_enum_value_list (type);
20070 if (is_new_type)
20071 finish_enum (type);
20072 }
20073
20074 if (nested_name_specifier)
20075 {
20076 if (CLASS_TYPE_P (nested_name_specifier))
20077 {
20078 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
20079 pop_scope (nested_name_specifier);
20080 }
20081 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
20082 pop_nested_namespace (nested_name_specifier);
20083 }
20084 return type;
20085 }
20086
20087 /* Parse an enumerator-list. The enumerators all have the indicated
20088 TYPE.
20089
20090 enumerator-list:
20091 enumerator-definition
20092 enumerator-list , enumerator-definition */
20093
20094 static void
20095 cp_parser_enumerator_list (cp_parser* parser, tree type)
20096 {
20097 while (true)
20098 {
20099 /* Parse an enumerator-definition. */
20100 cp_parser_enumerator_definition (parser, type);
20101
20102 /* If the next token is not a ',', we've reached the end of
20103 the list. */
20104 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20105 break;
20106 /* Otherwise, consume the `,' and keep going. */
20107 cp_lexer_consume_token (parser->lexer);
20108 /* If the next token is a `}', there is a trailing comma. */
20109 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20110 {
20111 if (cxx_dialect < cxx11)
20112 pedwarn (input_location, OPT_Wpedantic,
20113 "comma at end of enumerator list");
20114 break;
20115 }
20116 }
20117 }
20118
20119 /* Parse an enumerator-definition. The enumerator has the indicated
20120 TYPE.
20121
20122 enumerator-definition:
20123 enumerator
20124 enumerator = constant-expression
20125
20126 enumerator:
20127 identifier
20128
20129 GNU Extensions:
20130
20131 enumerator-definition:
20132 enumerator attributes [opt]
20133 enumerator attributes [opt] = constant-expression */
20134
20135 static void
20136 cp_parser_enumerator_definition (cp_parser* parser, tree type)
20137 {
20138 tree identifier;
20139 tree value;
20140 location_t loc;
20141
20142 /* Save the input location because we are interested in the location
20143 of the identifier and not the location of the explicit value. */
20144 loc = cp_lexer_peek_token (parser->lexer)->location;
20145
20146 /* Look for the identifier. */
20147 identifier = cp_parser_identifier (parser);
20148 if (identifier == error_mark_node)
20149 return;
20150
20151 /* Parse any specified attributes. */
20152 tree attrs = cp_parser_attributes_opt (parser);
20153
20154 /* If the next token is an '=', then there is an explicit value. */
20155 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20156 {
20157 /* Consume the `=' token. */
20158 cp_lexer_consume_token (parser->lexer);
20159 /* Parse the value. */
20160 value = cp_parser_constant_expression (parser);
20161 }
20162 else
20163 value = NULL_TREE;
20164
20165 /* If we are processing a template, make sure the initializer of the
20166 enumerator doesn't contain any bare template parameter pack. */
20167 if (check_for_bare_parameter_packs (value))
20168 value = error_mark_node;
20169
20170 /* Create the enumerator. */
20171 build_enumerator (identifier, value, type, attrs, loc);
20172 }
20173
20174 /* Parse a namespace-name.
20175
20176 namespace-name:
20177 original-namespace-name
20178 namespace-alias
20179
20180 Returns the NAMESPACE_DECL for the namespace. */
20181
20182 static tree
20183 cp_parser_namespace_name (cp_parser* parser)
20184 {
20185 tree identifier;
20186 tree namespace_decl;
20187
20188 cp_token *token = cp_lexer_peek_token (parser->lexer);
20189
20190 /* Get the name of the namespace. */
20191 identifier = cp_parser_identifier (parser);
20192 if (identifier == error_mark_node)
20193 return error_mark_node;
20194
20195 /* Look up the identifier in the currently active scope. Look only
20196 for namespaces, due to:
20197
20198 [basic.lookup.udir]
20199
20200 When looking up a namespace-name in a using-directive or alias
20201 definition, only namespace names are considered.
20202
20203 And:
20204
20205 [basic.lookup.qual]
20206
20207 During the lookup of a name preceding the :: scope resolution
20208 operator, object, function, and enumerator names are ignored.
20209
20210 (Note that cp_parser_qualifying_entity only calls this
20211 function if the token after the name is the scope resolution
20212 operator.) */
20213 namespace_decl = cp_parser_lookup_name (parser, identifier,
20214 none_type,
20215 /*is_template=*/false,
20216 /*is_namespace=*/true,
20217 /*check_dependency=*/true,
20218 /*ambiguous_decls=*/NULL,
20219 token->location);
20220 /* If it's not a namespace, issue an error. */
20221 if (namespace_decl == error_mark_node
20222 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
20223 {
20224 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20225 {
20226 auto_diagnostic_group d;
20227 name_hint hint;
20228 if (namespace_decl == error_mark_node
20229 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
20230 hint = suggest_alternative_in_explicit_scope (token->location,
20231 identifier,
20232 parser->scope);
20233 if (const char *suggestion = hint.suggestion ())
20234 {
20235 gcc_rich_location richloc (token->location);
20236 richloc.add_fixit_replace (suggestion);
20237 error_at (&richloc,
20238 "%qD is not a namespace-name; did you mean %qs?",
20239 identifier, suggestion);
20240 }
20241 else
20242 error_at (token->location, "%qD is not a namespace-name",
20243 identifier);
20244 }
20245 else
20246 cp_parser_error (parser, "expected namespace-name");
20247 namespace_decl = error_mark_node;
20248 }
20249
20250 return namespace_decl;
20251 }
20252
20253 /* Parse a namespace-definition.
20254
20255 namespace-definition:
20256 named-namespace-definition
20257 unnamed-namespace-definition
20258
20259 named-namespace-definition:
20260 original-namespace-definition
20261 extension-namespace-definition
20262
20263 original-namespace-definition:
20264 namespace identifier { namespace-body }
20265
20266 extension-namespace-definition:
20267 namespace original-namespace-name { namespace-body }
20268
20269 unnamed-namespace-definition:
20270 namespace { namespace-body } */
20271
20272 static void
20273 cp_parser_namespace_definition (cp_parser* parser)
20274 {
20275 tree identifier;
20276 int nested_definition_count = 0;
20277
20278 cp_ensure_no_omp_declare_simd (parser);
20279 cp_ensure_no_oacc_routine (parser);
20280
20281 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
20282 const bool topmost_inline_p = is_inline;
20283
20284 if (is_inline)
20285 {
20286 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
20287 cp_lexer_consume_token (parser->lexer);
20288 }
20289
20290 /* Look for the `namespace' keyword. */
20291 cp_token* token
20292 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
20293
20294 /* Parse any specified attributes before the identifier. */
20295 tree attribs = cp_parser_attributes_opt (parser);
20296
20297 for (;;)
20298 {
20299 identifier = NULL_TREE;
20300
20301 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
20302 RID_INLINE);
20303 if (nested_inline_p && nested_definition_count != 0)
20304 {
20305 if (cxx_dialect < cxx20)
20306 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
20307 OPT_Wpedantic, "nested inline namespace definitions only "
20308 "available with %<-std=c++20%> or %<-std=gnu++20%>");
20309 cp_lexer_consume_token (parser->lexer);
20310 }
20311
20312 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20313 {
20314 identifier = cp_parser_identifier (parser);
20315
20316 if (cp_next_tokens_can_be_std_attribute_p (parser))
20317 pedwarn (input_location, OPT_Wpedantic,
20318 "standard attributes on namespaces must precede "
20319 "the namespace name");
20320
20321 /* Parse any attributes specified after the identifier. */
20322 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
20323 }
20324
20325 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20326 {
20327 /* Don't forget that the innermost namespace might have been
20328 marked as inline. Use |= because we cannot overwrite
20329 IS_INLINE in case the outermost namespace is inline, but
20330 there are no nested inlines. */
20331 is_inline |= nested_inline_p;
20332 break;
20333 }
20334
20335 if (!nested_definition_count && cxx_dialect < cxx17)
20336 pedwarn (input_location, OPT_Wpedantic,
20337 "nested namespace definitions only available with "
20338 "%<-std=c++17%> or %<-std=gnu++17%>");
20339
20340 /* Nested namespace names can create new namespaces (unlike
20341 other qualified-ids). */
20342 if (int count = (identifier
20343 ? push_namespace (identifier, nested_inline_p)
20344 : 0))
20345 nested_definition_count += count;
20346 else
20347 cp_parser_error (parser, "nested namespace name required");
20348 cp_lexer_consume_token (parser->lexer);
20349 }
20350
20351 if (nested_definition_count && !identifier)
20352 cp_parser_error (parser, "namespace name required");
20353
20354 if (nested_definition_count && attribs)
20355 error_at (token->location,
20356 "a nested namespace definition cannot have attributes");
20357 if (nested_definition_count && topmost_inline_p)
20358 error_at (token->location,
20359 "a nested namespace definition cannot be inline");
20360
20361 /* Start the namespace. */
20362 nested_definition_count += push_namespace (identifier, is_inline);
20363
20364 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
20365
20366 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
20367
20368 /* Look for the `{' to validate starting the namespace. */
20369 matching_braces braces;
20370 if (braces.require_open (parser))
20371 {
20372 /* Parse the body of the namespace. */
20373 cp_parser_namespace_body (parser);
20374
20375 /* Look for the final `}'. */
20376 braces.require_close (parser);
20377 }
20378
20379 if (has_visibility)
20380 pop_visibility (1);
20381
20382 /* Pop the nested namespace definitions. */
20383 while (nested_definition_count--)
20384 pop_namespace ();
20385 }
20386
20387 /* Parse a namespace-body.
20388
20389 namespace-body:
20390 declaration-seq [opt] */
20391
20392 static void
20393 cp_parser_namespace_body (cp_parser* parser)
20394 {
20395 cp_parser_declaration_seq_opt (parser);
20396 }
20397
20398 /* Parse a namespace-alias-definition.
20399
20400 namespace-alias-definition:
20401 namespace identifier = qualified-namespace-specifier ; */
20402
20403 static void
20404 cp_parser_namespace_alias_definition (cp_parser* parser)
20405 {
20406 tree identifier;
20407 tree namespace_specifier;
20408
20409 cp_token *token = cp_lexer_peek_token (parser->lexer);
20410
20411 /* Look for the `namespace' keyword. */
20412 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
20413 /* Look for the identifier. */
20414 identifier = cp_parser_identifier (parser);
20415 if (identifier == error_mark_node)
20416 return;
20417 /* Look for the `=' token. */
20418 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
20419 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20420 {
20421 error_at (token->location, "%<namespace%> definition is not allowed here");
20422 /* Skip the definition. */
20423 cp_lexer_consume_token (parser->lexer);
20424 if (cp_parser_skip_to_closing_brace (parser))
20425 cp_lexer_consume_token (parser->lexer);
20426 return;
20427 }
20428 cp_parser_require (parser, CPP_EQ, RT_EQ);
20429 /* Look for the qualified-namespace-specifier. */
20430 namespace_specifier
20431 = cp_parser_qualified_namespace_specifier (parser);
20432 cp_warn_deprecated_use_scopes (namespace_specifier);
20433 /* Look for the `;' token. */
20434 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20435
20436 /* Register the alias in the symbol table. */
20437 do_namespace_alias (identifier, namespace_specifier);
20438 }
20439
20440 /* Parse a qualified-namespace-specifier.
20441
20442 qualified-namespace-specifier:
20443 :: [opt] nested-name-specifier [opt] namespace-name
20444
20445 Returns a NAMESPACE_DECL corresponding to the specified
20446 namespace. */
20447
20448 static tree
20449 cp_parser_qualified_namespace_specifier (cp_parser* parser)
20450 {
20451 /* Look for the optional `::'. */
20452 cp_parser_global_scope_opt (parser,
20453 /*current_scope_valid_p=*/false);
20454
20455 /* Look for the optional nested-name-specifier. */
20456 cp_parser_nested_name_specifier_opt (parser,
20457 /*typename_keyword_p=*/false,
20458 /*check_dependency_p=*/true,
20459 /*type_p=*/false,
20460 /*is_declaration=*/true);
20461
20462 return cp_parser_namespace_name (parser);
20463 }
20464
20465 /* Subroutine of cp_parser_using_declaration. */
20466
20467 static tree
20468 finish_using_decl (tree qscope, tree identifier, bool typename_p = false)
20469 {
20470 tree decl = NULL_TREE;
20471 if (at_class_scope_p ())
20472 {
20473 /* Create the USING_DECL. */
20474 decl = do_class_using_decl (qscope, identifier);
20475
20476 if (check_for_bare_parameter_packs (decl))
20477 return error_mark_node;
20478
20479 if (decl && typename_p)
20480 USING_DECL_TYPENAME_P (decl) = 1;
20481
20482 /* Add it to the list of members in this class. */
20483 finish_member_declaration (decl);
20484 }
20485 else
20486 finish_nonmember_using_decl (qscope, identifier);
20487 return decl;
20488 }
20489
20490 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
20491 access declaration.
20492
20493 using-declaration:
20494 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
20495 using :: unqualified-id ;
20496
20497 access-declaration:
20498 qualified-id ;
20499
20500 */
20501
20502 static bool
20503 cp_parser_using_declaration (cp_parser* parser,
20504 bool access_declaration_p)
20505 {
20506 cp_token *token;
20507 bool typename_p = false;
20508 bool global_scope_p;
20509 tree identifier;
20510 tree qscope;
20511 int oldcount = errorcount;
20512 cp_token *diag_token = NULL;
20513
20514 if (access_declaration_p)
20515 {
20516 diag_token = cp_lexer_peek_token (parser->lexer);
20517 cp_parser_parse_tentatively (parser);
20518 }
20519 else
20520 {
20521 /* Look for the `using' keyword. */
20522 cp_parser_require_keyword (parser, RID_USING, RT_USING);
20523
20524 again:
20525 /* Peek at the next token. */
20526 token = cp_lexer_peek_token (parser->lexer);
20527 /* See if it's `typename'. */
20528 if (token->keyword == RID_TYPENAME)
20529 {
20530 /* Remember that we've seen it. */
20531 typename_p = true;
20532 /* Consume the `typename' token. */
20533 cp_lexer_consume_token (parser->lexer);
20534 }
20535 }
20536
20537 /* Look for the optional global scope qualification. */
20538 global_scope_p
20539 = (cp_parser_global_scope_opt (parser,
20540 /*current_scope_valid_p=*/false)
20541 != NULL_TREE);
20542
20543 /* If we saw `typename', or didn't see `::', then there must be a
20544 nested-name-specifier present. */
20545 if (typename_p || !global_scope_p)
20546 {
20547 qscope = cp_parser_nested_name_specifier (parser, typename_p,
20548 /*check_dependency_p=*/true,
20549 /*type_p=*/false,
20550 /*is_declaration=*/true);
20551 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
20552 {
20553 cp_parser_skip_to_end_of_block_or_statement (parser);
20554 return false;
20555 }
20556 }
20557 /* Otherwise, we could be in either of the two productions. In that
20558 case, treat the nested-name-specifier as optional. */
20559 else
20560 qscope = cp_parser_nested_name_specifier_opt (parser,
20561 /*typename_keyword_p=*/false,
20562 /*check_dependency_p=*/true,
20563 /*type_p=*/false,
20564 /*is_declaration=*/true);
20565 if (!qscope)
20566 qscope = global_namespace;
20567
20568 cp_warn_deprecated_use_scopes (qscope);
20569
20570 if (access_declaration_p && cp_parser_error_occurred (parser))
20571 /* Something has already gone wrong; there's no need to parse
20572 further. Since an error has occurred, the return value of
20573 cp_parser_parse_definitely will be false, as required. */
20574 return cp_parser_parse_definitely (parser);
20575
20576 token = cp_lexer_peek_token (parser->lexer);
20577 /* Parse the unqualified-id. */
20578 identifier = cp_parser_unqualified_id (parser,
20579 /*template_keyword_p=*/false,
20580 /*check_dependency_p=*/true,
20581 /*declarator_p=*/true,
20582 /*optional_p=*/false);
20583
20584 if (access_declaration_p)
20585 {
20586 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20587 cp_parser_simulate_error (parser);
20588 if (!cp_parser_parse_definitely (parser))
20589 return false;
20590 }
20591 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20592 {
20593 cp_token *ell = cp_lexer_consume_token (parser->lexer);
20594 if (cxx_dialect < cxx17)
20595 pedwarn (ell->location, 0,
20596 "pack expansion in using-declaration only available "
20597 "with %<-std=c++17%> or %<-std=gnu++17%>");
20598 qscope = make_pack_expansion (qscope);
20599 }
20600
20601 /* The function we call to handle a using-declaration is different
20602 depending on what scope we are in. */
20603 if (qscope == error_mark_node || identifier == error_mark_node)
20604 ;
20605 else if (!identifier_p (identifier)
20606 && TREE_CODE (identifier) != BIT_NOT_EXPR)
20607 /* [namespace.udecl]
20608
20609 A using declaration shall not name a template-id. */
20610 error_at (token->location,
20611 "a template-id may not appear in a using-declaration");
20612 else
20613 {
20614 tree decl = finish_using_decl (qscope, identifier, typename_p);
20615
20616 if (decl == error_mark_node)
20617 {
20618 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20619 return false;
20620 }
20621 }
20622
20623 if (!access_declaration_p
20624 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20625 {
20626 cp_token *comma = cp_lexer_consume_token (parser->lexer);
20627 if (cxx_dialect < cxx17)
20628 pedwarn (comma->location, 0,
20629 "comma-separated list in using-declaration only available "
20630 "with %<-std=c++17%> or %<-std=gnu++17%>");
20631 goto again;
20632 }
20633
20634 /* Look for the final `;'. */
20635 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20636
20637 if (access_declaration_p && errorcount == oldcount)
20638 warning_at (diag_token->location, OPT_Wdeprecated,
20639 "access declarations are deprecated "
20640 "in favour of using-declarations; "
20641 "suggestion: add the %<using%> keyword");
20642
20643 return true;
20644 }
20645
20646 /* C++20 using enum declaration.
20647
20648 using-enum-declaration :
20649 using elaborated-enum-specifier ; */
20650
20651 static void
20652 cp_parser_using_enum (cp_parser *parser)
20653 {
20654 cp_parser_require_keyword (parser, RID_USING, RT_USING);
20655
20656 /* Using cp_parser_elaborated_type_specifier rejects typedef-names, which
20657 breaks one of the motivating examples in using-enum-5.C.
20658 cp_parser_simple_type_specifier seems to be closer to what we actually
20659 want, though that hasn't been properly specified yet. */
20660
20661 /* Consume 'enum'. */
20662 gcc_checking_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM));
20663 cp_lexer_consume_token (parser->lexer);
20664
20665 cp_token *start = cp_lexer_peek_token (parser->lexer);
20666
20667 tree type = (cp_parser_simple_type_specifier
20668 (parser, NULL, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
20669
20670 cp_token *end = cp_lexer_previous_token (parser->lexer);
20671
20672 if (type == error_mark_node
20673 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
20674 {
20675 cp_parser_skip_to_end_of_block_or_statement (parser);
20676 return;
20677 }
20678 if (TREE_CODE (type) == TYPE_DECL)
20679 type = TREE_TYPE (type);
20680
20681 /* The elaborated-enum-specifier shall not name a dependent type and the type
20682 shall have a reachable enum-specifier. */
20683 const char *msg = nullptr;
20684 if (cxx_dialect < cxx20)
20685 msg = _("%<using enum%> "
20686 "only available with %<-std=c++20%> or %<-std=gnu++20%>");
20687 else if (dependent_type_p (type))
20688 msg = _("%<using enum%> of dependent type %qT");
20689 else if (TREE_CODE (type) != ENUMERAL_TYPE)
20690 msg = _("%<using enum%> of non-enumeration type %q#T");
20691 else if (!COMPLETE_TYPE_P (type))
20692 msg = _("%<using enum%> of incomplete type %qT");
20693 else if (OPAQUE_ENUM_P (type))
20694 msg = _("%<using enum%> of %qT before its enum-specifier");
20695 if (msg)
20696 {
20697 location_t loc = make_location (start, start, end);
20698 auto_diagnostic_group g;
20699 error_at (loc, msg, type);
20700 loc = location_of (type);
20701 if (cxx_dialect < cxx20 || loc == input_location)
20702 ;
20703 else if (OPAQUE_ENUM_P (type))
20704 inform (loc, "opaque-enum-declaration here");
20705 else
20706 inform (loc, "declared here");
20707 }
20708
20709 /* A using-enum-declaration introduces the enumerator names of the named
20710 enumeration as if by a using-declaration for each enumerator. */
20711 if (TREE_CODE (type) == ENUMERAL_TYPE)
20712 for (tree v = TYPE_VALUES (type); v; v = TREE_CHAIN (v))
20713 finish_using_decl (type, DECL_NAME (TREE_VALUE (v)));
20714 }
20715
20716 /* Parse an alias-declaration.
20717
20718 alias-declaration:
20719 using identifier attribute-specifier-seq [opt] = type-id */
20720
20721 static tree
20722 cp_parser_alias_declaration (cp_parser* parser)
20723 {
20724 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
20725 location_t id_location, type_location;
20726 cp_declarator *declarator;
20727 cp_decl_specifier_seq decl_specs;
20728 bool member_p;
20729 const char *saved_message = NULL;
20730
20731 /* Look for the `using' keyword. */
20732 cp_token *using_token
20733 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
20734 if (using_token == NULL)
20735 return error_mark_node;
20736
20737 id_location = cp_lexer_peek_token (parser->lexer)->location;
20738 id = cp_parser_identifier (parser);
20739 if (id == error_mark_node)
20740 return error_mark_node;
20741
20742 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
20743 attributes = cp_parser_attributes_opt (parser);
20744 if (attributes == error_mark_node)
20745 return error_mark_node;
20746
20747 cp_parser_require (parser, CPP_EQ, RT_EQ);
20748
20749 if (cp_parser_error_occurred (parser))
20750 return error_mark_node;
20751
20752 cp_parser_commit_to_tentative_parse (parser);
20753
20754 /* Now we are going to parse the type-id of the declaration. */
20755
20756 /*
20757 [dcl.type]/3 says:
20758
20759 "A type-specifier-seq shall not define a class or enumeration
20760 unless it appears in the type-id of an alias-declaration (7.1.3) that
20761 is not the declaration of a template-declaration."
20762
20763 In other words, if we currently are in an alias template, the
20764 type-id should not define a type.
20765
20766 So let's set parser->type_definition_forbidden_message in that
20767 case; cp_parser_check_type_definition (called by
20768 cp_parser_class_specifier) will then emit an error if a type is
20769 defined in the type-id. */
20770 if (parser->num_template_parameter_lists)
20771 {
20772 saved_message = parser->type_definition_forbidden_message;
20773 parser->type_definition_forbidden_message =
20774 G_("types may not be defined in alias template declarations");
20775 }
20776
20777 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
20778 &type_location);
20779
20780 /* Restore the error message if need be. */
20781 if (parser->num_template_parameter_lists)
20782 parser->type_definition_forbidden_message = saved_message;
20783
20784 if (type == error_mark_node
20785 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
20786 {
20787 cp_parser_skip_to_end_of_block_or_statement (parser);
20788 return error_mark_node;
20789 }
20790
20791 /* A typedef-name can also be introduced by an alias-declaration. The
20792 identifier following the using keyword becomes a typedef-name. It has
20793 the same semantics as if it were introduced by the typedef
20794 specifier. In particular, it does not define a new type and it shall
20795 not appear in the type-id. */
20796
20797 clear_decl_specs (&decl_specs);
20798 decl_specs.type = type;
20799 if (attributes != NULL_TREE)
20800 {
20801 decl_specs.attributes = attributes;
20802 set_and_check_decl_spec_loc (&decl_specs,
20803 ds_attribute,
20804 attrs_token);
20805 }
20806 set_and_check_decl_spec_loc (&decl_specs,
20807 ds_typedef,
20808 using_token);
20809 set_and_check_decl_spec_loc (&decl_specs,
20810 ds_alias,
20811 using_token);
20812 decl_specs.locations[ds_type_spec] = type_location;
20813
20814 if (parser->num_template_parameter_lists
20815 && !cp_parser_check_template_parameters (parser,
20816 /*num_templates=*/0,
20817 /*template_id*/false,
20818 id_location,
20819 /*declarator=*/NULL))
20820 return error_mark_node;
20821
20822 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
20823
20824 member_p = at_class_scope_p ();
20825 if (member_p)
20826 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
20827 NULL_TREE, attributes);
20828 else
20829 decl = start_decl (declarator, &decl_specs, 0,
20830 attributes, NULL_TREE, &pushed_scope);
20831 if (decl == error_mark_node)
20832 return decl;
20833
20834 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
20835
20836 if (pushed_scope)
20837 pop_scope (pushed_scope);
20838
20839 /* If decl is a template, return its TEMPLATE_DECL so that it gets
20840 added into the symbol table; otherwise, return the TYPE_DECL. */
20841 if (DECL_LANG_SPECIFIC (decl)
20842 && DECL_TEMPLATE_INFO (decl)
20843 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
20844 {
20845 decl = DECL_TI_TEMPLATE (decl);
20846 if (member_p)
20847 check_member_template (decl);
20848 }
20849
20850 return decl;
20851 }
20852
20853 /* Parse a using-directive.
20854
20855 using-directive:
20856 using namespace :: [opt] nested-name-specifier [opt]
20857 namespace-name ; */
20858
20859 static void
20860 cp_parser_using_directive (cp_parser* parser)
20861 {
20862 tree namespace_decl;
20863 tree attribs;
20864
20865 /* Look for the `using' keyword. */
20866 cp_parser_require_keyword (parser, RID_USING, RT_USING);
20867 /* And the `namespace' keyword. */
20868 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
20869 /* Look for the optional `::' operator. */
20870 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
20871 /* And the optional nested-name-specifier. */
20872 cp_parser_nested_name_specifier_opt (parser,
20873 /*typename_keyword_p=*/false,
20874 /*check_dependency_p=*/true,
20875 /*type_p=*/false,
20876 /*is_declaration=*/true);
20877 /* Get the namespace being used. */
20878 namespace_decl = cp_parser_namespace_name (parser);
20879 cp_warn_deprecated_use_scopes (namespace_decl);
20880 /* And any specified attributes. */
20881 attribs = cp_parser_attributes_opt (parser);
20882
20883 /* Update the symbol table. */
20884 finish_using_directive (namespace_decl, attribs);
20885
20886 /* Look for the final `;'. */
20887 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20888 }
20889
20890 /* Parse an asm-definition.
20891
20892 asm-qualifier:
20893 volatile
20894 inline
20895 goto
20896
20897 asm-qualifier-list:
20898 asm-qualifier
20899 asm-qualifier-list asm-qualifier
20900
20901 asm-definition:
20902 asm ( string-literal ) ;
20903
20904 GNU Extension:
20905
20906 asm-definition:
20907 asm asm-qualifier-list [opt] ( string-literal ) ;
20908 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
20909 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
20910 : asm-operand-list [opt] ) ;
20911 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
20912 : asm-operand-list [opt]
20913 : asm-clobber-list [opt] ) ;
20914 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
20915 : asm-clobber-list [opt]
20916 : asm-goto-list ) ;
20917
20918 The form with asm-goto-list is valid if and only if the asm-qualifier-list
20919 contains goto, and is the only allowed form in that case. No duplicates are
20920 allowed in an asm-qualifier-list. */
20921
20922 static void
20923 cp_parser_asm_definition (cp_parser* parser)
20924 {
20925 tree string;
20926 tree outputs = NULL_TREE;
20927 tree inputs = NULL_TREE;
20928 tree clobbers = NULL_TREE;
20929 tree labels = NULL_TREE;
20930 tree asm_stmt;
20931 bool extended_p = false;
20932 bool invalid_inputs_p = false;
20933 bool invalid_outputs_p = false;
20934 required_token missing = RT_NONE;
20935 location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
20936
20937 /* Look for the `asm' keyword. */
20938 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
20939
20940 /* In C++20, unevaluated inline assembly is permitted in constexpr
20941 functions. */
20942 if (parser->in_function_body
20943 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
20944 && (cxx_dialect < cxx20))
20945 pedwarn (asm_loc, 0, "%<asm%> in %<constexpr%> function only available "
20946 "with %<-std=c++20%> or %<-std=gnu++20%>");
20947
20948 /* Handle the asm-qualifier-list. */
20949 location_t volatile_loc = UNKNOWN_LOCATION;
20950 location_t inline_loc = UNKNOWN_LOCATION;
20951 location_t goto_loc = UNKNOWN_LOCATION;
20952 location_t first_loc = UNKNOWN_LOCATION;
20953
20954 if (cp_parser_allow_gnu_extensions_p (parser))
20955 for (;;)
20956 {
20957 cp_token *token = cp_lexer_peek_token (parser->lexer);
20958 location_t loc = token->location;
20959 switch (cp_lexer_peek_token (parser->lexer)->keyword)
20960 {
20961 case RID_VOLATILE:
20962 if (volatile_loc)
20963 {
20964 error_at (loc, "duplicate %<asm%> qualifier %qT",
20965 token->u.value);
20966 inform (volatile_loc, "first seen here");
20967 }
20968 else
20969 {
20970 if (!parser->in_function_body)
20971 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
20972 "outside of function body", token->u.value);
20973 volatile_loc = loc;
20974 }
20975 cp_lexer_consume_token (parser->lexer);
20976 continue;
20977
20978 case RID_INLINE:
20979 if (inline_loc)
20980 {
20981 error_at (loc, "duplicate %<asm%> qualifier %qT",
20982 token->u.value);
20983 inform (inline_loc, "first seen here");
20984 }
20985 else
20986 inline_loc = loc;
20987 if (!first_loc)
20988 first_loc = loc;
20989 cp_lexer_consume_token (parser->lexer);
20990 continue;
20991
20992 case RID_GOTO:
20993 if (goto_loc)
20994 {
20995 error_at (loc, "duplicate %<asm%> qualifier %qT",
20996 token->u.value);
20997 inform (goto_loc, "first seen here");
20998 }
20999 else
21000 goto_loc = loc;
21001 if (!first_loc)
21002 first_loc = loc;
21003 cp_lexer_consume_token (parser->lexer);
21004 continue;
21005
21006 case RID_CONST:
21007 case RID_RESTRICT:
21008 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
21009 cp_lexer_consume_token (parser->lexer);
21010 continue;
21011
21012 default:
21013 break;
21014 }
21015 break;
21016 }
21017
21018 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
21019 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
21020 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
21021
21022 if (!parser->in_function_body && (inline_p || goto_p))
21023 {
21024 error_at (first_loc, "%<asm%> qualifier outside of function body");
21025 inline_p = goto_p = false;
21026 }
21027
21028 /* Look for the opening `('. */
21029 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21030 return;
21031 /* Look for the string. */
21032 string = cp_parser_string_literal (parser, false, false);
21033 if (string == error_mark_node)
21034 {
21035 cp_parser_skip_to_closing_parenthesis (parser, true, false,
21036 /*consume_paren=*/true);
21037 return;
21038 }
21039
21040 /* If we're allowing GNU extensions, check for the extended assembly
21041 syntax. Unfortunately, the `:' tokens need not be separated by
21042 a space in C, and so, for compatibility, we tolerate that here
21043 too. Doing that means that we have to treat the `::' operator as
21044 two `:' tokens. */
21045 if (cp_parser_allow_gnu_extensions_p (parser)
21046 && parser->in_function_body
21047 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
21048 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
21049 {
21050 bool inputs_p = false;
21051 bool clobbers_p = false;
21052 bool labels_p = false;
21053
21054 /* The extended syntax was used. */
21055 extended_p = true;
21056
21057 /* Look for outputs. */
21058 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21059 {
21060 /* Consume the `:'. */
21061 cp_lexer_consume_token (parser->lexer);
21062 /* Parse the output-operands. */
21063 if (cp_lexer_next_token_is_not (parser->lexer,
21064 CPP_COLON)
21065 && cp_lexer_next_token_is_not (parser->lexer,
21066 CPP_SCOPE)
21067 && cp_lexer_next_token_is_not (parser->lexer,
21068 CPP_CLOSE_PAREN))
21069 {
21070 outputs = cp_parser_asm_operand_list (parser);
21071 if (outputs == error_mark_node)
21072 invalid_outputs_p = true;
21073 }
21074 }
21075 /* If the next token is `::', there are no outputs, and the
21076 next token is the beginning of the inputs. */
21077 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21078 /* The inputs are coming next. */
21079 inputs_p = true;
21080
21081 /* Look for inputs. */
21082 if (inputs_p
21083 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21084 {
21085 /* Consume the `:' or `::'. */
21086 cp_lexer_consume_token (parser->lexer);
21087 /* Parse the output-operands. */
21088 if (cp_lexer_next_token_is_not (parser->lexer,
21089 CPP_COLON)
21090 && cp_lexer_next_token_is_not (parser->lexer,
21091 CPP_SCOPE)
21092 && cp_lexer_next_token_is_not (parser->lexer,
21093 CPP_CLOSE_PAREN))
21094 {
21095 inputs = cp_parser_asm_operand_list (parser);
21096 if (inputs == error_mark_node)
21097 invalid_inputs_p = true;
21098 }
21099 }
21100 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21101 /* The clobbers are coming next. */
21102 clobbers_p = true;
21103
21104 /* Look for clobbers. */
21105 if (clobbers_p
21106 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21107 {
21108 clobbers_p = true;
21109 /* Consume the `:' or `::'. */
21110 cp_lexer_consume_token (parser->lexer);
21111 /* Parse the clobbers. */
21112 if (cp_lexer_next_token_is_not (parser->lexer,
21113 CPP_COLON)
21114 && cp_lexer_next_token_is_not (parser->lexer,
21115 CPP_CLOSE_PAREN))
21116 clobbers = cp_parser_asm_clobber_list (parser);
21117 }
21118 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21119 /* The labels are coming next. */
21120 labels_p = true;
21121
21122 /* Look for labels. */
21123 if (labels_p
21124 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
21125 {
21126 labels_p = true;
21127 /* Consume the `:' or `::'. */
21128 cp_lexer_consume_token (parser->lexer);
21129 /* Parse the labels. */
21130 labels = cp_parser_asm_label_list (parser);
21131 }
21132
21133 if (goto_p && !labels_p)
21134 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
21135 }
21136 else if (goto_p)
21137 missing = RT_COLON_SCOPE;
21138
21139 /* Look for the closing `)'. */
21140 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
21141 missing ? missing : RT_CLOSE_PAREN))
21142 cp_parser_skip_to_closing_parenthesis (parser, true, false,
21143 /*consume_paren=*/true);
21144 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21145
21146 if (!invalid_inputs_p && !invalid_outputs_p)
21147 {
21148 /* Create the ASM_EXPR. */
21149 if (parser->in_function_body)
21150 {
21151 asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
21152 inputs, clobbers, labels, inline_p);
21153 /* If the extended syntax was not used, mark the ASM_EXPR. */
21154 if (!extended_p)
21155 {
21156 tree temp = asm_stmt;
21157 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
21158 temp = TREE_OPERAND (temp, 0);
21159
21160 ASM_INPUT_P (temp) = 1;
21161 }
21162 }
21163 else
21164 symtab->finalize_toplevel_asm (string);
21165 }
21166 }
21167
21168 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
21169 type that comes from the decl-specifier-seq. */
21170
21171 static tree
21172 strip_declarator_types (tree type, cp_declarator *declarator)
21173 {
21174 for (cp_declarator *d = declarator; d;)
21175 switch (d->kind)
21176 {
21177 case cdk_id:
21178 case cdk_decomp:
21179 case cdk_error:
21180 d = NULL;
21181 break;
21182
21183 default:
21184 if (TYPE_PTRMEMFUNC_P (type))
21185 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
21186 type = TREE_TYPE (type);
21187 d = d->declarator;
21188 break;
21189 }
21190
21191 return type;
21192 }
21193
21194 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
21195 a construct looks like a variable definition but is actually a function
21196 declaration. DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
21197 is the declarator for this function declaration. */
21198
21199 static void
21200 warn_about_ambiguous_parse (const cp_decl_specifier_seq *decl_specifiers,
21201 const cp_declarator *declarator)
21202 {
21203 /* Only warn if we are declaring a function at block scope. */
21204 if (!at_function_scope_p ())
21205 return;
21206
21207 /* And only if there is no storage class specified. */
21208 if (decl_specifiers->storage_class != sc_none
21209 || decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
21210 return;
21211
21212 if (declarator->kind != cdk_function
21213 || !declarator->declarator
21214 || declarator->declarator->kind != cdk_id
21215 || !identifier_p (get_unqualified_id
21216 (const_cast<cp_declarator *>(declarator))))
21217 return;
21218
21219 /* Don't warn when the whole declarator (not just the declarator-id!)
21220 was parenthesized. That is, don't warn for int(n()) but do warn
21221 for int(f)(). */
21222 if (declarator->parenthesized != UNKNOWN_LOCATION)
21223 return;
21224
21225 tree type;
21226 if (decl_specifiers->type)
21227 {
21228 type = decl_specifiers->type;
21229 if (TREE_CODE (type) == TYPE_DECL)
21230 type = TREE_TYPE (type);
21231
21232 /* If the return type is void there is no ambiguity. */
21233 if (same_type_p (type, void_type_node))
21234 return;
21235 }
21236 else if (decl_specifiers->any_type_specifiers_p)
21237 /* Code like long f(); will have null ->type. If we have any
21238 type-specifiers, pretend we've seen int. */
21239 type = integer_type_node;
21240 else
21241 return;
21242
21243 auto_diagnostic_group d;
21244 location_t loc = declarator->u.function.parens_loc;
21245 tree params = declarator->u.function.parameters;
21246 const bool has_list_ctor_p = CLASS_TYPE_P (type) && TYPE_HAS_LIST_CTOR (type);
21247
21248 /* The T t() case. */
21249 if (params == void_list_node)
21250 {
21251 if (warning_at (loc, OPT_Wvexing_parse,
21252 "empty parentheses were disambiguated as a function "
21253 "declaration"))
21254 {
21255 /* () means value-initialization (C++03 and up); {} (C++11 and up)
21256 means value-initialization or aggregate-initialization, nothing
21257 means default-initialization. We can only suggest removing the
21258 parentheses/adding {} if T has a default constructor. */
21259 if (!CLASS_TYPE_P (type) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
21260 {
21261 gcc_rich_location iloc (loc);
21262 iloc.add_fixit_remove ();
21263 inform (&iloc, "remove parentheses to default-initialize "
21264 "a variable");
21265 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
21266 {
21267 if (CP_AGGREGATE_TYPE_P (type))
21268 inform (loc, "or replace parentheses with braces to "
21269 "aggregate-initialize a variable");
21270 else
21271 inform (loc, "or replace parentheses with braces to "
21272 "value-initialize a variable");
21273 }
21274 }
21275 }
21276 return;
21277 }
21278
21279 /* If we had (...) or the parameter-list wasn't parenthesized,
21280 we're done. */
21281 if (params == NULL_TREE || !PARENTHESIZED_LIST_P (params))
21282 return;
21283
21284 /* The T t(X()) case. */
21285 if (list_length (params) == 2)
21286 {
21287 if (warning_at (loc, OPT_Wvexing_parse,
21288 "parentheses were disambiguated as a function "
21289 "declaration"))
21290 {
21291 gcc_rich_location iloc (loc);
21292 /* {}-initialization means that we can use an initializer-list
21293 constructor if no default constructor is available, so don't
21294 suggest using {} for classes that have an initializer_list
21295 constructor. */
21296 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
21297 {
21298 iloc.add_fixit_replace (get_start (loc), "{");
21299 iloc.add_fixit_replace (get_finish (loc), "}");
21300 inform (&iloc, "replace parentheses with braces to declare a "
21301 "variable");
21302 }
21303 else
21304 {
21305 iloc.add_fixit_insert_after (get_start (loc), "(");
21306 iloc.add_fixit_insert_before (get_finish (loc), ")");
21307 inform (&iloc, "add parentheses to declare a variable");
21308 }
21309 }
21310 }
21311 /* The T t(X(), X()) case. */
21312 else if (warning_at (loc, OPT_Wvexing_parse,
21313 "parentheses were disambiguated as a function "
21314 "declaration"))
21315 {
21316 gcc_rich_location iloc (loc);
21317 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
21318 {
21319 iloc.add_fixit_replace (get_start (loc), "{");
21320 iloc.add_fixit_replace (get_finish (loc), "}");
21321 inform (&iloc, "replace parentheses with braces to declare a "
21322 "variable");
21323 }
21324 }
21325 }
21326
21327 /* Declarators [gram.dcl.decl] */
21328
21329 /* Parse an init-declarator.
21330
21331 init-declarator:
21332 declarator initializer [opt]
21333
21334 GNU Extension:
21335
21336 init-declarator:
21337 declarator asm-specification [opt] attributes [opt] initializer [opt]
21338
21339 function-definition:
21340 decl-specifier-seq [opt] declarator ctor-initializer [opt]
21341 function-body
21342 decl-specifier-seq [opt] declarator function-try-block
21343
21344 GNU Extension:
21345
21346 function-definition:
21347 __extension__ function-definition
21348
21349 TM Extension:
21350
21351 function-definition:
21352 decl-specifier-seq [opt] declarator function-transaction-block
21353
21354 The parser flags FLAGS is used to control type-specifier parsing.
21355
21356 The DECL_SPECIFIERS apply to this declarator. Returns a
21357 representation of the entity declared. If MEMBER_P is TRUE, then
21358 this declarator appears in a class scope. The new DECL created by
21359 this declarator is returned.
21360
21361 The CHECKS are access checks that should be performed once we know
21362 what entity is being declared (and, therefore, what classes have
21363 befriended it).
21364
21365 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
21366 for a function-definition here as well. If the declarator is a
21367 declarator for a function-definition, *FUNCTION_DEFINITION_P will
21368 be TRUE upon return. By that point, the function-definition will
21369 have been completely parsed.
21370
21371 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
21372 is FALSE.
21373
21374 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
21375 parsed declaration if it is an uninitialized single declarator not followed
21376 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
21377 if present, will not be consumed. If returned, this declarator will be
21378 created with SD_INITIALIZED but will not call cp_finish_decl.
21379
21380 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
21381 and there is an initializer, the pointed location_t is set to the
21382 location of the '=' or `(', or '{' in C++11 token introducing the
21383 initializer. */
21384
21385 static tree
21386 cp_parser_init_declarator (cp_parser* parser,
21387 cp_parser_flags flags,
21388 cp_decl_specifier_seq *decl_specifiers,
21389 vec<deferred_access_check, va_gc> *checks,
21390 bool function_definition_allowed_p,
21391 bool member_p,
21392 int declares_class_or_enum,
21393 bool* function_definition_p,
21394 tree* maybe_range_for_decl,
21395 location_t* init_loc,
21396 tree* auto_result)
21397 {
21398 cp_token *token = NULL, *asm_spec_start_token = NULL,
21399 *attributes_start_token = NULL;
21400 cp_declarator *declarator;
21401 tree prefix_attributes;
21402 tree attributes = NULL;
21403 tree asm_specification;
21404 tree initializer;
21405 tree decl = NULL_TREE;
21406 tree scope;
21407 int is_initialized;
21408 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
21409 initialized with "= ..", CPP_OPEN_PAREN if initialized with
21410 "(...)". */
21411 enum cpp_ttype initialization_kind;
21412 bool is_direct_init = false;
21413 bool is_non_constant_init;
21414 int ctor_dtor_or_conv_p;
21415 bool friend_p = cp_parser_friend_p (decl_specifiers);
21416 tree pushed_scope = NULL_TREE;
21417 bool range_for_decl_p = false;
21418 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21419 location_t tmp_init_loc = UNKNOWN_LOCATION;
21420
21421 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
21422 flags |= CP_PARSER_FLAGS_CONSTEVAL;
21423
21424 /* Gather the attributes that were provided with the
21425 decl-specifiers. */
21426 prefix_attributes = decl_specifiers->attributes;
21427
21428 /* Assume that this is not the declarator for a function
21429 definition. */
21430 if (function_definition_p)
21431 *function_definition_p = false;
21432
21433 /* Default arguments are only permitted for function parameters. */
21434 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
21435 parser->default_arg_ok_p = false;
21436
21437 /* Defer access checks while parsing the declarator; we cannot know
21438 what names are accessible until we know what is being
21439 declared. */
21440 resume_deferring_access_checks ();
21441
21442 token = cp_lexer_peek_token (parser->lexer);
21443
21444 /* Parse the declarator. */
21445 declarator
21446 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
21447 flags, &ctor_dtor_or_conv_p,
21448 /*parenthesized_p=*/NULL,
21449 member_p, friend_p, /*static_p=*/false);
21450 /* Gather up the deferred checks. */
21451 stop_deferring_access_checks ();
21452
21453 parser->default_arg_ok_p = saved_default_arg_ok_p;
21454
21455 /* If the DECLARATOR was erroneous, there's no need to go
21456 further. */
21457 if (declarator == cp_error_declarator)
21458 return error_mark_node;
21459
21460 /* Check that the number of template-parameter-lists is OK. */
21461 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
21462 token->location))
21463 return error_mark_node;
21464
21465 if (declares_class_or_enum & 2)
21466 cp_parser_check_for_definition_in_return_type (declarator,
21467 decl_specifiers->type,
21468 decl_specifiers->locations[ds_type_spec]);
21469
21470 /* Figure out what scope the entity declared by the DECLARATOR is
21471 located in. `grokdeclarator' sometimes changes the scope, so
21472 we compute it now. */
21473 scope = get_scope_of_declarator (declarator);
21474
21475 /* Perform any lookups in the declared type which were thought to be
21476 dependent, but are not in the scope of the declarator. */
21477 decl_specifiers->type
21478 = maybe_update_decl_type (decl_specifiers->type, scope);
21479
21480 /* If we're allowing GNU extensions, look for an
21481 asm-specification. */
21482 if (cp_parser_allow_gnu_extensions_p (parser))
21483 {
21484 /* Look for an asm-specification. */
21485 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
21486 asm_specification = cp_parser_asm_specification_opt (parser);
21487 }
21488 else
21489 asm_specification = NULL_TREE;
21490
21491 /* Look for attributes. */
21492 attributes_start_token = cp_lexer_peek_token (parser->lexer);
21493 attributes = cp_parser_attributes_opt (parser);
21494
21495 /* Peek at the next token. */
21496 token = cp_lexer_peek_token (parser->lexer);
21497
21498 bool bogus_implicit_tmpl = false;
21499
21500 if (function_declarator_p (declarator))
21501 {
21502 /* Handle C++17 deduction guides. */
21503 if (!decl_specifiers->type
21504 && !decl_specifiers->any_type_specifiers_p
21505 && ctor_dtor_or_conv_p <= 0
21506 && cxx_dialect >= cxx17)
21507 {
21508 cp_declarator *id = get_id_declarator (declarator);
21509 tree name = id->u.id.unqualified_name;
21510 parser->scope = id->u.id.qualifying_scope;
21511 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
21512 if (tmpl
21513 && (DECL_CLASS_TEMPLATE_P (tmpl)
21514 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
21515 {
21516 id->u.id.unqualified_name = dguide_name (tmpl);
21517 id->u.id.sfk = sfk_deduction_guide;
21518 ctor_dtor_or_conv_p = 1;
21519 }
21520 }
21521
21522 if (!member_p && !cp_parser_error_occurred (parser))
21523 warn_about_ambiguous_parse (decl_specifiers, declarator);
21524
21525 /* Check to see if the token indicates the start of a
21526 function-definition. */
21527 if (cp_parser_token_starts_function_definition_p (token))
21528 {
21529 if (!function_definition_allowed_p)
21530 {
21531 /* If a function-definition should not appear here, issue an
21532 error message. */
21533 cp_parser_error (parser,
21534 "a function-definition is not allowed here");
21535 return error_mark_node;
21536 }
21537
21538 location_t func_brace_location
21539 = cp_lexer_peek_token (parser->lexer)->location;
21540
21541 /* Neither attributes nor an asm-specification are allowed
21542 on a function-definition. */
21543 if (asm_specification)
21544 error_at (asm_spec_start_token->location,
21545 "an %<asm%> specification is not allowed "
21546 "on a function-definition");
21547 if (attributes)
21548 error_at (attributes_start_token->location,
21549 "attributes are not allowed "
21550 "on a function-definition");
21551 /* This is a function-definition. */
21552 *function_definition_p = true;
21553
21554 /* Parse the function definition. */
21555 if (member_p)
21556 decl = cp_parser_save_member_function_body (parser,
21557 decl_specifiers,
21558 declarator,
21559 prefix_attributes);
21560 else
21561 decl =
21562 (cp_parser_function_definition_from_specifiers_and_declarator
21563 (parser, decl_specifiers, prefix_attributes, declarator));
21564
21565 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
21566 {
21567 /* This is where the prologue starts... */
21568 DECL_STRUCT_FUNCTION (decl)->function_start_locus
21569 = func_brace_location;
21570 }
21571
21572 return decl;
21573 }
21574 }
21575 else if (parser->fully_implicit_function_template_p)
21576 {
21577 /* A non-template declaration involving a function parameter list
21578 containing an implicit template parameter will be made into a
21579 template. If the resulting declaration is not going to be an
21580 actual function then finish the template scope here to prevent it.
21581 An error message will be issued once we have a decl to talk about.
21582
21583 FIXME probably we should do type deduction rather than create an
21584 implicit template, but the standard currently doesn't allow it. */
21585 bogus_implicit_tmpl = true;
21586 finish_fully_implicit_template (parser, NULL_TREE);
21587 }
21588
21589 /* [dcl.dcl]
21590
21591 Only in function declarations for constructors, destructors, type
21592 conversions, and deduction guides can the decl-specifier-seq be omitted.
21593
21594 We explicitly postpone this check past the point where we handle
21595 function-definitions because we tolerate function-definitions
21596 that are missing their return types in some modes. */
21597 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
21598 {
21599 cp_parser_error (parser,
21600 "expected constructor, destructor, or type conversion");
21601 return error_mark_node;
21602 }
21603
21604 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
21605 if (token->type == CPP_EQ
21606 || token->type == CPP_OPEN_PAREN
21607 || token->type == CPP_OPEN_BRACE)
21608 {
21609 is_initialized = SD_INITIALIZED;
21610 initialization_kind = token->type;
21611 if (maybe_range_for_decl)
21612 *maybe_range_for_decl = error_mark_node;
21613 tmp_init_loc = token->location;
21614 if (init_loc && *init_loc == UNKNOWN_LOCATION)
21615 *init_loc = tmp_init_loc;
21616
21617 if (token->type == CPP_EQ
21618 && function_declarator_p (declarator))
21619 {
21620 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
21621 if (t2->keyword == RID_DEFAULT)
21622 is_initialized = SD_DEFAULTED;
21623 else if (t2->keyword == RID_DELETE)
21624 is_initialized = SD_DELETED;
21625 }
21626 }
21627 else
21628 {
21629 /* If the init-declarator isn't initialized and isn't followed by a
21630 `,' or `;', it's not a valid init-declarator. */
21631 if (token->type != CPP_COMMA
21632 && token->type != CPP_SEMICOLON)
21633 {
21634 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
21635 range_for_decl_p = true;
21636 else
21637 {
21638 if (!maybe_range_for_decl)
21639 cp_parser_error (parser, "expected initializer");
21640 return error_mark_node;
21641 }
21642 }
21643 is_initialized = SD_UNINITIALIZED;
21644 initialization_kind = CPP_EOF;
21645 }
21646
21647 /* Because start_decl has side-effects, we should only call it if we
21648 know we're going ahead. By this point, we know that we cannot
21649 possibly be looking at any other construct. */
21650 cp_parser_commit_to_tentative_parse (parser);
21651
21652 /* Enter the newly declared entry in the symbol table. If we're
21653 processing a declaration in a class-specifier, we wait until
21654 after processing the initializer. */
21655 if (!member_p)
21656 {
21657 if (parser->in_unbraced_linkage_specification_p)
21658 decl_specifiers->storage_class = sc_extern;
21659 decl = start_decl (declarator, decl_specifiers,
21660 range_for_decl_p? SD_INITIALIZED : is_initialized,
21661 attributes, prefix_attributes, &pushed_scope);
21662 cp_finalize_omp_declare_simd (parser, decl);
21663 cp_finalize_oacc_routine (parser, decl, false);
21664 /* Adjust location of decl if declarator->id_loc is more appropriate:
21665 set, and decl wasn't merged with another decl, in which case its
21666 location would be different from input_location, and more accurate. */
21667 if (DECL_P (decl)
21668 && declarator->id_loc != UNKNOWN_LOCATION
21669 && DECL_SOURCE_LOCATION (decl) == input_location)
21670 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
21671 }
21672 else if (scope)
21673 /* Enter the SCOPE. That way unqualified names appearing in the
21674 initializer will be looked up in SCOPE. */
21675 pushed_scope = push_scope (scope);
21676
21677 /* Perform deferred access control checks, now that we know in which
21678 SCOPE the declared entity resides. */
21679 if (!member_p && decl)
21680 {
21681 tree saved_current_function_decl = NULL_TREE;
21682
21683 /* If the entity being declared is a function, pretend that we
21684 are in its scope. If it is a `friend', it may have access to
21685 things that would not otherwise be accessible. */
21686 if (TREE_CODE (decl) == FUNCTION_DECL)
21687 {
21688 saved_current_function_decl = current_function_decl;
21689 current_function_decl = decl;
21690 }
21691
21692 /* Perform access checks for template parameters. */
21693 cp_parser_perform_template_parameter_access_checks (checks);
21694
21695 /* Perform the access control checks for the declarator and the
21696 decl-specifiers. */
21697 perform_deferred_access_checks (tf_warning_or_error);
21698
21699 /* Restore the saved value. */
21700 if (TREE_CODE (decl) == FUNCTION_DECL)
21701 current_function_decl = saved_current_function_decl;
21702 }
21703
21704 /* Parse the initializer. */
21705 initializer = NULL_TREE;
21706 is_direct_init = false;
21707 is_non_constant_init = true;
21708 if (is_initialized)
21709 {
21710 if (function_declarator_p (declarator))
21711 {
21712 if (initialization_kind == CPP_EQ)
21713 initializer = cp_parser_pure_specifier (parser);
21714 else
21715 {
21716 /* If the declaration was erroneous, we don't really
21717 know what the user intended, so just silently
21718 consume the initializer. */
21719 if (decl != error_mark_node)
21720 error_at (tmp_init_loc, "initializer provided for function");
21721 cp_parser_skip_to_closing_parenthesis (parser,
21722 /*recovering=*/true,
21723 /*or_comma=*/false,
21724 /*consume_paren=*/true);
21725 }
21726 }
21727 else
21728 {
21729 /* We want to record the extra mangling scope for in-class
21730 initializers of class members and initializers of static
21731 data member templates and namespace-scope initializers.
21732 The former involves deferring parsing of the initializer
21733 until end of class as with default arguments. So right
21734 here we only handle the latter two. */
21735 bool has_lambda_scope = false;
21736
21737 if (decl != error_mark_node
21738 && !member_p
21739 && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
21740 has_lambda_scope = true;
21741
21742 if (has_lambda_scope)
21743 start_lambda_scope (decl);
21744 initializer = cp_parser_initializer (parser,
21745 &is_direct_init,
21746 &is_non_constant_init);
21747 if (has_lambda_scope)
21748 finish_lambda_scope ();
21749 if (initializer == error_mark_node)
21750 cp_parser_skip_to_end_of_statement (parser);
21751 }
21752 }
21753
21754 /* The old parser allows attributes to appear after a parenthesized
21755 initializer. Mark Mitchell proposed removing this functionality
21756 on the GCC mailing lists on 2002-08-13. This parser accepts the
21757 attributes -- but ignores them. Made a permerror in GCC 8. */
21758 if (cp_parser_allow_gnu_extensions_p (parser)
21759 && initialization_kind == CPP_OPEN_PAREN
21760 && cp_parser_attributes_opt (parser)
21761 && permerror (input_location,
21762 "attributes after parenthesized initializer ignored"))
21763 {
21764 static bool hint;
21765 if (flag_permissive && !hint)
21766 {
21767 hint = true;
21768 inform (input_location,
21769 "this flexibility is deprecated and will be removed");
21770 }
21771 }
21772
21773 /* And now complain about a non-function implicit template. */
21774 if (bogus_implicit_tmpl && decl != error_mark_node)
21775 error_at (DECL_SOURCE_LOCATION (decl),
21776 "non-function %qD declared as implicit template", decl);
21777
21778 /* For an in-class declaration, use `grokfield' to create the
21779 declaration. */
21780 if (member_p)
21781 {
21782 if (pushed_scope)
21783 {
21784 pop_scope (pushed_scope);
21785 pushed_scope = NULL_TREE;
21786 }
21787 decl = grokfield (declarator, decl_specifiers,
21788 initializer, !is_non_constant_init,
21789 /*asmspec=*/NULL_TREE,
21790 attr_chainon (attributes, prefix_attributes));
21791 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
21792 cp_parser_save_default_args (parser, decl);
21793 cp_finalize_omp_declare_simd (parser, decl);
21794 cp_finalize_oacc_routine (parser, decl, false);
21795 }
21796
21797 /* Finish processing the declaration. But, skip member
21798 declarations. */
21799 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
21800 {
21801 cp_finish_decl (decl,
21802 initializer, !is_non_constant_init,
21803 asm_specification,
21804 /* If the initializer is in parentheses, then this is
21805 a direct-initialization, which means that an
21806 `explicit' constructor is OK. Otherwise, an
21807 `explicit' constructor cannot be used. */
21808 ((is_direct_init || !is_initialized)
21809 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
21810 }
21811 else if ((cxx_dialect != cxx98) && friend_p
21812 && decl && TREE_CODE (decl) == FUNCTION_DECL)
21813 /* Core issue #226 (C++0x only): A default template-argument
21814 shall not be specified in a friend class template
21815 declaration. */
21816 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
21817 /*is_partial=*/false, /*is_friend_decl=*/1);
21818
21819 if (!friend_p && pushed_scope)
21820 pop_scope (pushed_scope);
21821
21822 if (function_declarator_p (declarator)
21823 && parser->fully_implicit_function_template_p)
21824 {
21825 if (member_p)
21826 decl = finish_fully_implicit_template (parser, decl);
21827 else
21828 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
21829 }
21830
21831 if (auto_result && is_initialized && decl_specifiers->type
21832 && type_uses_auto (decl_specifiers->type))
21833 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
21834
21835 return decl;
21836 }
21837
21838 /* Parse a declarator.
21839
21840 declarator:
21841 direct-declarator
21842 ptr-operator declarator
21843
21844 abstract-declarator:
21845 ptr-operator abstract-declarator [opt]
21846 direct-abstract-declarator
21847
21848 GNU Extensions:
21849
21850 declarator:
21851 attributes [opt] direct-declarator
21852 attributes [opt] ptr-operator declarator
21853
21854 abstract-declarator:
21855 attributes [opt] ptr-operator abstract-declarator [opt]
21856 attributes [opt] direct-abstract-declarator
21857
21858 The parser flags FLAGS is used to control type-specifier parsing.
21859
21860 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
21861 detect constructors, destructors, deduction guides, or conversion operators.
21862 It is set to -1 if the declarator is a name, and +1 if it is a
21863 function. Otherwise it is set to zero. Usually you just want to
21864 test for >0, but internally the negative value is used.
21865
21866 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
21867 a decl-specifier-seq unless it declares a constructor, destructor,
21868 or conversion. It might seem that we could check this condition in
21869 semantic analysis, rather than parsing, but that makes it difficult
21870 to handle something like `f()'. We want to notice that there are
21871 no decl-specifiers, and therefore realize that this is an
21872 expression, not a declaration.)
21873
21874 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
21875 the declarator is a direct-declarator of the form "(...)".
21876
21877 MEMBER_P is true iff this declarator is a member-declarator.
21878
21879 FRIEND_P is true iff this declarator is a friend.
21880
21881 STATIC_P is true iff the keyword static was seen. */
21882
21883 static cp_declarator *
21884 cp_parser_declarator (cp_parser* parser,
21885 cp_parser_declarator_kind dcl_kind,
21886 cp_parser_flags flags,
21887 int* ctor_dtor_or_conv_p,
21888 bool* parenthesized_p,
21889 bool member_p, bool friend_p, bool static_p)
21890 {
21891 cp_declarator *declarator;
21892 enum tree_code code;
21893 cp_cv_quals cv_quals;
21894 tree class_type;
21895 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
21896
21897 /* Assume this is not a constructor, destructor, or type-conversion
21898 operator. */
21899 if (ctor_dtor_or_conv_p)
21900 *ctor_dtor_or_conv_p = 0;
21901
21902 if (cp_parser_allow_gnu_extensions_p (parser))
21903 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
21904
21905 /* Check for the ptr-operator production. */
21906 cp_parser_parse_tentatively (parser);
21907 /* Parse the ptr-operator. */
21908 code = cp_parser_ptr_operator (parser,
21909 &class_type,
21910 &cv_quals,
21911 &std_attributes);
21912
21913 /* If that worked, then we have a ptr-operator. */
21914 if (cp_parser_parse_definitely (parser))
21915 {
21916 /* If a ptr-operator was found, then this declarator was not
21917 parenthesized. */
21918 if (parenthesized_p)
21919 *parenthesized_p = false;
21920 /* The dependent declarator is optional if we are parsing an
21921 abstract-declarator. */
21922 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
21923 cp_parser_parse_tentatively (parser);
21924
21925 /* Parse the dependent declarator. */
21926 declarator = cp_parser_declarator (parser, dcl_kind,
21927 CP_PARSER_FLAGS_NONE,
21928 /*ctor_dtor_or_conv_p=*/NULL,
21929 /*parenthesized_p=*/NULL,
21930 /*member_p=*/false,
21931 friend_p, /*static_p=*/false);
21932
21933 /* If we are parsing an abstract-declarator, we must handle the
21934 case where the dependent declarator is absent. */
21935 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
21936 && !cp_parser_parse_definitely (parser))
21937 declarator = NULL;
21938
21939 declarator = cp_parser_make_indirect_declarator
21940 (code, class_type, cv_quals, declarator, std_attributes);
21941 }
21942 /* Everything else is a direct-declarator. */
21943 else
21944 {
21945 if (parenthesized_p)
21946 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
21947 CPP_OPEN_PAREN);
21948 declarator = cp_parser_direct_declarator (parser, dcl_kind,
21949 flags, ctor_dtor_or_conv_p,
21950 member_p, friend_p, static_p);
21951 }
21952
21953 if (gnu_attributes && declarator && declarator != cp_error_declarator)
21954 declarator->attributes = gnu_attributes;
21955 return declarator;
21956 }
21957
21958 /* Parse a direct-declarator or direct-abstract-declarator.
21959
21960 direct-declarator:
21961 declarator-id
21962 direct-declarator ( parameter-declaration-clause )
21963 cv-qualifier-seq [opt]
21964 ref-qualifier [opt]
21965 exception-specification [opt]
21966 direct-declarator [ constant-expression [opt] ]
21967 ( declarator )
21968
21969 direct-abstract-declarator:
21970 direct-abstract-declarator [opt]
21971 ( parameter-declaration-clause )
21972 cv-qualifier-seq [opt]
21973 ref-qualifier [opt]
21974 exception-specification [opt]
21975 direct-abstract-declarator [opt] [ constant-expression [opt] ]
21976 ( abstract-declarator )
21977
21978 Returns a representation of the declarator. DCL_KIND is
21979 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
21980 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
21981 we are parsing a direct-declarator. It is
21982 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
21983 of ambiguity we prefer an abstract declarator, as per
21984 [dcl.ambig.res].
21985 The parser flags FLAGS is used to control type-specifier parsing.
21986 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
21987 as for cp_parser_declarator. */
21988
21989 static cp_declarator *
21990 cp_parser_direct_declarator (cp_parser* parser,
21991 cp_parser_declarator_kind dcl_kind,
21992 cp_parser_flags flags,
21993 int* ctor_dtor_or_conv_p,
21994 bool member_p, bool friend_p, bool static_p)
21995 {
21996 cp_token *token;
21997 cp_declarator *declarator = NULL;
21998 tree scope = NULL_TREE;
21999 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22000 bool saved_in_declarator_p = parser->in_declarator_p;
22001 bool first = true;
22002 tree pushed_scope = NULL_TREE;
22003 cp_token *open_paren = NULL, *close_paren = NULL;
22004
22005 while (true)
22006 {
22007 /* Peek at the next token. */
22008 token = cp_lexer_peek_token (parser->lexer);
22009 if (token->type == CPP_OPEN_PAREN)
22010 {
22011 /* This is either a parameter-declaration-clause, or a
22012 parenthesized declarator. When we know we are parsing a
22013 named declarator, it must be a parenthesized declarator
22014 if FIRST is true. For instance, `(int)' is a
22015 parameter-declaration-clause, with an omitted
22016 direct-abstract-declarator. But `((*))', is a
22017 parenthesized abstract declarator. Finally, when T is a
22018 template parameter `(T)' is a
22019 parameter-declaration-clause, and not a parenthesized
22020 named declarator.
22021
22022 We first try and parse a parameter-declaration-clause,
22023 and then try a nested declarator (if FIRST is true).
22024
22025 It is not an error for it not to be a
22026 parameter-declaration-clause, even when FIRST is
22027 false. Consider,
22028
22029 int i (int);
22030 int i (3);
22031
22032 The first is the declaration of a function while the
22033 second is the definition of a variable, including its
22034 initializer.
22035
22036 Having seen only the parenthesis, we cannot know which of
22037 these two alternatives should be selected. Even more
22038 complex are examples like:
22039
22040 int i (int (a));
22041 int i (int (3));
22042
22043 The former is a function-declaration; the latter is a
22044 variable initialization.
22045
22046 Thus again, we try a parameter-declaration-clause, and if
22047 that fails, we back out and return. */
22048
22049 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
22050 {
22051 tree params;
22052 bool is_declarator = false;
22053
22054 open_paren = NULL;
22055
22056 /* In a member-declarator, the only valid interpretation
22057 of a parenthesis is the start of a
22058 parameter-declaration-clause. (It is invalid to
22059 initialize a static data member with a parenthesized
22060 initializer; only the "=" form of initialization is
22061 permitted.) */
22062 if (!member_p)
22063 cp_parser_parse_tentatively (parser);
22064
22065 /* Consume the `('. */
22066 const location_t parens_start = token->location;
22067 matching_parens parens;
22068 parens.consume_open (parser);
22069 if (first)
22070 {
22071 /* If this is going to be an abstract declarator, we're
22072 in a declarator and we can't have default args. */
22073 parser->default_arg_ok_p = false;
22074 parser->in_declarator_p = true;
22075 }
22076
22077 begin_scope (sk_function_parms, NULL_TREE);
22078
22079 /* Signal we are in the immediate function context. */
22080 if (flags & CP_PARSER_FLAGS_CONSTEVAL)
22081 current_binding_level->immediate_fn_ctx_p = true;
22082
22083 /* Parse the parameter-declaration-clause. */
22084 params
22085 = cp_parser_parameter_declaration_clause (parser, flags);
22086 const location_t parens_end
22087 = cp_lexer_peek_token (parser->lexer)->location;
22088
22089 /* Consume the `)'. */
22090 parens.require_close (parser);
22091
22092 /* If all went well, parse the cv-qualifier-seq,
22093 ref-qualifier and the exception-specification. */
22094 if (member_p || cp_parser_parse_definitely (parser))
22095 {
22096 cp_cv_quals cv_quals;
22097 cp_virt_specifiers virt_specifiers;
22098 cp_ref_qualifier ref_qual;
22099 tree exception_specification;
22100 tree late_return;
22101 tree attrs;
22102 bool memfn = (member_p || (pushed_scope
22103 && CLASS_TYPE_P (pushed_scope)));
22104 unsigned char local_variables_forbidden_p
22105 = parser->local_variables_forbidden_p;
22106 /* 'this' is not allowed in static member functions. */
22107 if (static_p || friend_p)
22108 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
22109
22110 is_declarator = true;
22111
22112 if (ctor_dtor_or_conv_p)
22113 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
22114 first = false;
22115
22116 /* Parse the cv-qualifier-seq. */
22117 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
22118 /* Parse the ref-qualifier. */
22119 ref_qual = cp_parser_ref_qualifier_opt (parser);
22120 /* Parse the tx-qualifier. */
22121 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
22122
22123 tree save_ccp = current_class_ptr;
22124 tree save_ccr = current_class_ref;
22125 if (memfn)
22126 /* DR 1207: 'this' is in scope after the cv-quals. */
22127 inject_this_parameter (current_class_type, cv_quals);
22128
22129 /* If it turned out that this is e.g. a pointer to a
22130 function, we don't want to delay noexcept parsing. */
22131 if (declarator == NULL || declarator->kind != cdk_id)
22132 flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
22133
22134 /* Parse the exception-specification. */
22135 exception_specification
22136 = cp_parser_exception_specification_opt (parser,
22137 flags);
22138
22139 attrs = cp_parser_std_attribute_spec_seq (parser);
22140
22141 /* In here, we handle cases where attribute is used after
22142 the function declaration. For example:
22143 void func (int x) __attribute__((vector(..))); */
22144 tree gnu_attrs = NULL_TREE;
22145 tree requires_clause = NULL_TREE;
22146 late_return = (cp_parser_late_return_type_opt
22147 (parser, declarator, requires_clause));
22148
22149 /* Parse the virt-specifier-seq. */
22150 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22151
22152 location_t parens_loc = make_location (parens_start,
22153 parens_start,
22154 parens_end);
22155 /* Create the function-declarator. */
22156 declarator = make_call_declarator (declarator,
22157 params,
22158 cv_quals,
22159 virt_specifiers,
22160 ref_qual,
22161 tx_qual,
22162 exception_specification,
22163 late_return,
22164 requires_clause,
22165 parens_loc);
22166 declarator->std_attributes = attrs;
22167 declarator->attributes = gnu_attrs;
22168 /* Any subsequent parameter lists are to do with
22169 return type, so are not those of the declared
22170 function. */
22171 parser->default_arg_ok_p = false;
22172
22173 current_class_ptr = save_ccp;
22174 current_class_ref = save_ccr;
22175
22176 /* Restore the state of local_variables_forbidden_p. */
22177 parser->local_variables_forbidden_p
22178 = local_variables_forbidden_p;
22179 }
22180
22181 /* Remove the function parms from scope. */
22182 pop_bindings_and_leave_scope ();
22183
22184 if (is_declarator)
22185 /* Repeat the main loop. */
22186 continue;
22187 }
22188
22189 /* If this is the first, we can try a parenthesized
22190 declarator. */
22191 if (first)
22192 {
22193 bool saved_in_type_id_in_expr_p;
22194
22195 parser->default_arg_ok_p = saved_default_arg_ok_p;
22196 parser->in_declarator_p = saved_in_declarator_p;
22197
22198 open_paren = token;
22199 /* Consume the `('. */
22200 matching_parens parens;
22201 parens.consume_open (parser);
22202 /* Parse the nested declarator. */
22203 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
22204 parser->in_type_id_in_expr_p = true;
22205 declarator
22206 = cp_parser_declarator (parser, dcl_kind, flags,
22207 ctor_dtor_or_conv_p,
22208 /*parenthesized_p=*/NULL,
22209 member_p, friend_p,
22210 /*static_p=*/false);
22211 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
22212 first = false;
22213 /* Expect a `)'. */
22214 close_paren = cp_lexer_peek_token (parser->lexer);
22215 if (!parens.require_close (parser))
22216 declarator = cp_error_declarator;
22217 if (declarator == cp_error_declarator)
22218 break;
22219
22220 goto handle_declarator;
22221 }
22222 /* Otherwise, we must be done. */
22223 else
22224 break;
22225 }
22226 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
22227 && token->type == CPP_OPEN_SQUARE
22228 && !cp_next_tokens_can_be_attribute_p (parser))
22229 {
22230 /* Parse an array-declarator. */
22231 tree bounds, attrs;
22232
22233 if (ctor_dtor_or_conv_p)
22234 *ctor_dtor_or_conv_p = 0;
22235
22236 open_paren = NULL;
22237 first = false;
22238 parser->default_arg_ok_p = false;
22239 parser->in_declarator_p = true;
22240 /* Consume the `['. */
22241 cp_lexer_consume_token (parser->lexer);
22242 /* Peek at the next token. */
22243 token = cp_lexer_peek_token (parser->lexer);
22244 /* If the next token is `]', then there is no
22245 constant-expression. */
22246 if (token->type != CPP_CLOSE_SQUARE)
22247 {
22248 bool non_constant_p;
22249 bounds
22250 = cp_parser_constant_expression (parser,
22251 /*allow_non_constant=*/true,
22252 &non_constant_p);
22253 if (!non_constant_p)
22254 /* OK */;
22255 else if (error_operand_p (bounds))
22256 /* Already gave an error. */;
22257 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
22258 /* Let compute_array_index_type diagnose this. */;
22259 else if (!parser->in_function_body
22260 || current_binding_level->kind == sk_function_parms)
22261 {
22262 /* Normally, the array bound must be an integral constant
22263 expression. However, as an extension, we allow VLAs
22264 in function scopes as long as they aren't part of a
22265 parameter declaration. */
22266 cp_parser_error (parser,
22267 "array bound is not an integer constant");
22268 bounds = error_mark_node;
22269 }
22270 else if (processing_template_decl
22271 && !type_dependent_expression_p (bounds))
22272 {
22273 /* Remember this wasn't a constant-expression. */
22274 bounds = build_nop (TREE_TYPE (bounds), bounds);
22275 TREE_SIDE_EFFECTS (bounds) = 1;
22276 }
22277 }
22278 else
22279 bounds = NULL_TREE;
22280 /* Look for the closing `]'. */
22281 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22282 {
22283 declarator = cp_error_declarator;
22284 break;
22285 }
22286
22287 attrs = cp_parser_std_attribute_spec_seq (parser);
22288 declarator = make_array_declarator (declarator, bounds);
22289 declarator->std_attributes = attrs;
22290 }
22291 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
22292 {
22293 {
22294 tree qualifying_scope;
22295 tree unqualified_name;
22296 tree attrs;
22297 special_function_kind sfk;
22298 bool abstract_ok;
22299 bool pack_expansion_p = false;
22300 cp_token *declarator_id_start_token;
22301
22302 /* Parse a declarator-id */
22303 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
22304 if (abstract_ok)
22305 {
22306 cp_parser_parse_tentatively (parser);
22307
22308 /* If we see an ellipsis, we should be looking at a
22309 parameter pack. */
22310 if (token->type == CPP_ELLIPSIS)
22311 {
22312 /* Consume the `...' */
22313 cp_lexer_consume_token (parser->lexer);
22314
22315 pack_expansion_p = true;
22316 }
22317 }
22318
22319 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
22320 unqualified_name
22321 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
22322 qualifying_scope = parser->scope;
22323 if (abstract_ok)
22324 {
22325 bool okay = false;
22326
22327 if (!unqualified_name && pack_expansion_p)
22328 {
22329 /* Check whether an error occurred. */
22330 okay = !cp_parser_error_occurred (parser);
22331
22332 /* We already consumed the ellipsis to mark a
22333 parameter pack, but we have no way to report it,
22334 so abort the tentative parse. We will be exiting
22335 immediately anyway. */
22336 cp_parser_abort_tentative_parse (parser);
22337 }
22338 else
22339 okay = cp_parser_parse_definitely (parser);
22340
22341 if (!okay)
22342 unqualified_name = error_mark_node;
22343 else if (unqualified_name
22344 && (qualifying_scope
22345 || (!identifier_p (unqualified_name))))
22346 {
22347 cp_parser_error (parser, "expected unqualified-id");
22348 unqualified_name = error_mark_node;
22349 }
22350 }
22351
22352 if (!unqualified_name)
22353 return NULL;
22354 if (unqualified_name == error_mark_node)
22355 {
22356 declarator = cp_error_declarator;
22357 pack_expansion_p = false;
22358 declarator->parameter_pack_p = false;
22359 break;
22360 }
22361
22362 attrs = cp_parser_std_attribute_spec_seq (parser);
22363
22364 if (qualifying_scope && at_namespace_scope_p ()
22365 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
22366 {
22367 /* In the declaration of a member of a template class
22368 outside of the class itself, the SCOPE will sometimes
22369 be a TYPENAME_TYPE. For example, given:
22370
22371 template <typename T>
22372 int S<T>::R::i = 3;
22373
22374 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
22375 this context, we must resolve S<T>::R to an ordinary
22376 type, rather than a typename type.
22377
22378 The reason we normally avoid resolving TYPENAME_TYPEs
22379 is that a specialization of `S' might render
22380 `S<T>::R' not a type. However, if `S' is
22381 specialized, then this `i' will not be used, so there
22382 is no harm in resolving the types here. */
22383 tree type;
22384
22385 /* Resolve the TYPENAME_TYPE. */
22386 type = resolve_typename_type (qualifying_scope,
22387 /*only_current_p=*/false);
22388 /* If that failed, the declarator is invalid. */
22389 if (TREE_CODE (type) == TYPENAME_TYPE)
22390 {
22391 if (typedef_variant_p (type))
22392 error_at (declarator_id_start_token->location,
22393 "cannot define member of dependent typedef "
22394 "%qT", type);
22395 else
22396 error_at (declarator_id_start_token->location,
22397 "%<%T::%E%> is not a type",
22398 TYPE_CONTEXT (qualifying_scope),
22399 TYPE_IDENTIFIER (qualifying_scope));
22400 }
22401 qualifying_scope = type;
22402 }
22403
22404 sfk = sfk_none;
22405
22406 if (unqualified_name)
22407 {
22408 tree class_type;
22409
22410 if (qualifying_scope
22411 && CLASS_TYPE_P (qualifying_scope))
22412 class_type = qualifying_scope;
22413 else
22414 class_type = current_class_type;
22415
22416 if (TREE_CODE (unqualified_name) == TYPE_DECL)
22417 {
22418 tree name_type = TREE_TYPE (unqualified_name);
22419
22420 if (!class_type || !same_type_p (name_type, class_type))
22421 {
22422 /* We do not attempt to print the declarator
22423 here because we do not have enough
22424 information about its original syntactic
22425 form. */
22426 cp_parser_error (parser, "invalid declarator");
22427 declarator = cp_error_declarator;
22428 break;
22429 }
22430 else if (qualifying_scope
22431 && CLASSTYPE_USE_TEMPLATE (name_type))
22432 {
22433 error_at (declarator_id_start_token->location,
22434 "invalid use of constructor as a template");
22435 inform (declarator_id_start_token->location,
22436 "use %<%T::%D%> instead of %<%T::%D%> to "
22437 "name the constructor in a qualified name",
22438 class_type,
22439 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
22440 class_type, name_type);
22441 declarator = cp_error_declarator;
22442 break;
22443 }
22444 unqualified_name = constructor_name (class_type);
22445 }
22446
22447 if (class_type)
22448 {
22449 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
22450 sfk = sfk_destructor;
22451 else if (identifier_p (unqualified_name)
22452 && IDENTIFIER_CONV_OP_P (unqualified_name))
22453 sfk = sfk_conversion;
22454 else if (/* There's no way to declare a constructor
22455 for an unnamed type, even if the type
22456 got a name for linkage purposes. */
22457 !TYPE_WAS_UNNAMED (class_type)
22458 /* Handle correctly (c++/19200):
22459
22460 struct S {
22461 struct T{};
22462 friend void S(T);
22463 };
22464
22465 and also:
22466
22467 namespace N {
22468 void S();
22469 }
22470
22471 struct S {
22472 friend void N::S();
22473 }; */
22474 && (!friend_p || class_type == qualifying_scope)
22475 && constructor_name_p (unqualified_name,
22476 class_type))
22477 sfk = sfk_constructor;
22478 else if (is_overloaded_fn (unqualified_name)
22479 && DECL_CONSTRUCTOR_P (get_first_fn
22480 (unqualified_name)))
22481 sfk = sfk_constructor;
22482
22483 if (ctor_dtor_or_conv_p && sfk != sfk_none)
22484 *ctor_dtor_or_conv_p = -1;
22485 }
22486 }
22487 declarator = make_id_declarator (qualifying_scope,
22488 unqualified_name,
22489 sfk, token->location);
22490 declarator->std_attributes = attrs;
22491 declarator->parameter_pack_p = pack_expansion_p;
22492
22493 if (pack_expansion_p)
22494 maybe_warn_variadic_templates ();
22495
22496 /* We're looking for this case in [temp.res]:
22497 A qualified-id is assumed to name a type if [...]
22498 - it is a decl-specifier of the decl-specifier-seq of a
22499 parameter-declaration in a declarator of a function or
22500 function template declaration, ... */
22501 if (cxx_dialect >= cxx20
22502 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
22503 && declarator->kind == cdk_id
22504 && !at_class_scope_p ()
22505 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22506 {
22507 /* ...whose declarator-id is qualified. If it isn't, never
22508 assume the parameters to refer to types. */
22509 if (qualifying_scope == NULL_TREE)
22510 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
22511 else
22512 {
22513 /* Now we have something like
22514 template <typename T> int C::x(S::p);
22515 which can be a function template declaration or a
22516 variable template definition. If name lookup for
22517 the declarator-id C::x finds one or more function
22518 templates, assume S::p to name a type. Otherwise,
22519 don't. */
22520 tree decl
22521 = cp_parser_lookup_name (parser, unqualified_name,
22522 none_type,
22523 /*is_template=*/false,
22524 /*is_namespace=*/false,
22525 /*check_dependency=*/false,
22526 /*ambiguous_decls=*/NULL,
22527 token->location);
22528
22529 if (!is_overloaded_fn (decl)
22530 /* Allow
22531 template<typename T>
22532 A<T>::A(T::type) { } */
22533 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
22534 && constructor_name_p (unqualified_name,
22535 qualifying_scope)))
22536 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
22537 }
22538 }
22539 }
22540
22541 handle_declarator:;
22542 scope = get_scope_of_declarator (declarator);
22543 if (scope)
22544 {
22545 /* Any names that appear after the declarator-id for a
22546 member are looked up in the containing scope. */
22547 if (at_function_scope_p ())
22548 {
22549 /* But declarations with qualified-ids can't appear in a
22550 function. */
22551 cp_parser_error (parser, "qualified-id in declaration");
22552 declarator = cp_error_declarator;
22553 break;
22554 }
22555 pushed_scope = push_scope (scope);
22556 }
22557 parser->in_declarator_p = true;
22558 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
22559 || (declarator && declarator->kind == cdk_id))
22560 /* Default args are only allowed on function
22561 declarations. */
22562 parser->default_arg_ok_p = saved_default_arg_ok_p;
22563 else
22564 parser->default_arg_ok_p = false;
22565
22566 first = false;
22567 }
22568 /* We're done. */
22569 else
22570 break;
22571 }
22572
22573 /* For an abstract declarator, we might wind up with nothing at this
22574 point. That's an error; the declarator is not optional. */
22575 if (!declarator)
22576 cp_parser_error (parser, "expected declarator");
22577 else if (open_paren)
22578 {
22579 /* Record overly parenthesized declarator so we can give a
22580 diagnostic about confusing decl/expr disambiguation. */
22581 if (declarator->kind == cdk_array)
22582 {
22583 /* If the open and close parens are on different lines, this
22584 is probably a formatting thing, so ignore. */
22585 expanded_location open = expand_location (open_paren->location);
22586 expanded_location close = expand_location (close_paren->location);
22587 if (open.line != close.line || open.file != close.file)
22588 open_paren = NULL;
22589 }
22590 if (open_paren)
22591 declarator->parenthesized = make_location (open_paren->location,
22592 open_paren->location,
22593 close_paren->location);
22594 }
22595
22596 /* If we entered a scope, we must exit it now. */
22597 if (pushed_scope)
22598 pop_scope (pushed_scope);
22599
22600 parser->default_arg_ok_p = saved_default_arg_ok_p;
22601 parser->in_declarator_p = saved_in_declarator_p;
22602
22603 return declarator;
22604 }
22605
22606 /* Parse a ptr-operator.
22607
22608 ptr-operator:
22609 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
22610 * cv-qualifier-seq [opt]
22611 &
22612 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
22613 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
22614
22615 GNU Extension:
22616
22617 ptr-operator:
22618 & cv-qualifier-seq [opt]
22619
22620 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
22621 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
22622 an rvalue reference. In the case of a pointer-to-member, *TYPE is
22623 filled in with the TYPE containing the member. *CV_QUALS is
22624 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
22625 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
22626 Note that the tree codes returned by this function have nothing
22627 to do with the types of trees that will be eventually be created
22628 to represent the pointer or reference type being parsed. They are
22629 just constants with suggestive names. */
22630 static enum tree_code
22631 cp_parser_ptr_operator (cp_parser* parser,
22632 tree* type,
22633 cp_cv_quals *cv_quals,
22634 tree *attributes)
22635 {
22636 enum tree_code code = ERROR_MARK;
22637 cp_token *token;
22638 tree attrs = NULL_TREE;
22639
22640 /* Assume that it's not a pointer-to-member. */
22641 *type = NULL_TREE;
22642 /* And that there are no cv-qualifiers. */
22643 *cv_quals = TYPE_UNQUALIFIED;
22644
22645 /* Peek at the next token. */
22646 token = cp_lexer_peek_token (parser->lexer);
22647
22648 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
22649 if (token->type == CPP_MULT)
22650 code = INDIRECT_REF;
22651 else if (token->type == CPP_AND)
22652 code = ADDR_EXPR;
22653 else if ((cxx_dialect != cxx98) &&
22654 token->type == CPP_AND_AND) /* C++0x only */
22655 code = NON_LVALUE_EXPR;
22656
22657 if (code != ERROR_MARK)
22658 {
22659 /* Consume the `*', `&' or `&&'. */
22660 cp_lexer_consume_token (parser->lexer);
22661
22662 /* A `*' can be followed by a cv-qualifier-seq, and so can a
22663 `&', if we are allowing GNU extensions. (The only qualifier
22664 that can legally appear after `&' is `restrict', but that is
22665 enforced during semantic analysis. */
22666 if (code == INDIRECT_REF
22667 || cp_parser_allow_gnu_extensions_p (parser))
22668 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
22669
22670 attrs = cp_parser_std_attribute_spec_seq (parser);
22671 if (attributes != NULL)
22672 *attributes = attrs;
22673 }
22674 else
22675 {
22676 /* Try the pointer-to-member case. */
22677 cp_parser_parse_tentatively (parser);
22678 /* Look for the optional `::' operator. */
22679 cp_parser_global_scope_opt (parser,
22680 /*current_scope_valid_p=*/false);
22681 /* Look for the nested-name specifier. */
22682 token = cp_lexer_peek_token (parser->lexer);
22683 cp_parser_nested_name_specifier (parser,
22684 /*typename_keyword_p=*/false,
22685 /*check_dependency_p=*/true,
22686 /*type_p=*/false,
22687 /*is_declaration=*/false);
22688 /* If we found it, and the next token is a `*', then we are
22689 indeed looking at a pointer-to-member operator. */
22690 if (!cp_parser_error_occurred (parser)
22691 && cp_parser_require (parser, CPP_MULT, RT_MULT))
22692 {
22693 /* Indicate that the `*' operator was used. */
22694 code = INDIRECT_REF;
22695
22696 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
22697 error_at (token->location, "%qD is a namespace", parser->scope);
22698 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
22699 error_at (token->location, "cannot form pointer to member of "
22700 "non-class %q#T", parser->scope);
22701 else
22702 {
22703 /* The type of which the member is a member is given by the
22704 current SCOPE. */
22705 *type = parser->scope;
22706 /* The next name will not be qualified. */
22707 parser->scope = NULL_TREE;
22708 parser->qualifying_scope = NULL_TREE;
22709 parser->object_scope = NULL_TREE;
22710 /* Look for optional c++11 attributes. */
22711 attrs = cp_parser_std_attribute_spec_seq (parser);
22712 if (attributes != NULL)
22713 *attributes = attrs;
22714 /* Look for the optional cv-qualifier-seq. */
22715 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
22716 }
22717 }
22718 /* If that didn't work we don't have a ptr-operator. */
22719 if (!cp_parser_parse_definitely (parser))
22720 cp_parser_error (parser, "expected ptr-operator");
22721 }
22722
22723 return code;
22724 }
22725
22726 /* Parse an (optional) cv-qualifier-seq.
22727
22728 cv-qualifier-seq:
22729 cv-qualifier cv-qualifier-seq [opt]
22730
22731 cv-qualifier:
22732 const
22733 volatile
22734
22735 GNU Extension:
22736
22737 cv-qualifier:
22738 __restrict__
22739
22740 Returns a bitmask representing the cv-qualifiers. */
22741
22742 static cp_cv_quals
22743 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
22744 {
22745 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
22746
22747 while (true)
22748 {
22749 cp_token *token;
22750 cp_cv_quals cv_qualifier;
22751
22752 /* Peek at the next token. */
22753 token = cp_lexer_peek_token (parser->lexer);
22754 /* See if it's a cv-qualifier. */
22755 switch (token->keyword)
22756 {
22757 case RID_CONST:
22758 cv_qualifier = TYPE_QUAL_CONST;
22759 break;
22760
22761 case RID_VOLATILE:
22762 cv_qualifier = TYPE_QUAL_VOLATILE;
22763 break;
22764
22765 case RID_RESTRICT:
22766 cv_qualifier = TYPE_QUAL_RESTRICT;
22767 break;
22768
22769 default:
22770 cv_qualifier = TYPE_UNQUALIFIED;
22771 break;
22772 }
22773
22774 if (!cv_qualifier)
22775 break;
22776
22777 if (cv_quals & cv_qualifier)
22778 {
22779 gcc_rich_location richloc (token->location);
22780 richloc.add_fixit_remove ();
22781 error_at (&richloc, "duplicate cv-qualifier");
22782 cp_lexer_purge_token (parser->lexer);
22783 }
22784 else
22785 {
22786 cp_lexer_consume_token (parser->lexer);
22787 cv_quals |= cv_qualifier;
22788 }
22789 }
22790
22791 return cv_quals;
22792 }
22793
22794 /* Parse an (optional) ref-qualifier
22795
22796 ref-qualifier:
22797 &
22798 &&
22799
22800 Returns cp_ref_qualifier representing ref-qualifier. */
22801
22802 static cp_ref_qualifier
22803 cp_parser_ref_qualifier_opt (cp_parser* parser)
22804 {
22805 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
22806
22807 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
22808 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
22809 return ref_qual;
22810
22811 while (true)
22812 {
22813 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
22814 cp_token *token = cp_lexer_peek_token (parser->lexer);
22815
22816 switch (token->type)
22817 {
22818 case CPP_AND:
22819 curr_ref_qual = REF_QUAL_LVALUE;
22820 break;
22821
22822 case CPP_AND_AND:
22823 curr_ref_qual = REF_QUAL_RVALUE;
22824 break;
22825
22826 default:
22827 curr_ref_qual = REF_QUAL_NONE;
22828 break;
22829 }
22830
22831 if (!curr_ref_qual)
22832 break;
22833 else if (ref_qual)
22834 {
22835 error_at (token->location, "multiple ref-qualifiers");
22836 cp_lexer_purge_token (parser->lexer);
22837 }
22838 else
22839 {
22840 ref_qual = curr_ref_qual;
22841 cp_lexer_consume_token (parser->lexer);
22842 }
22843 }
22844
22845 return ref_qual;
22846 }
22847
22848 /* Parse an optional tx-qualifier.
22849
22850 tx-qualifier:
22851 transaction_safe
22852 transaction_safe_dynamic */
22853
22854 static tree
22855 cp_parser_tx_qualifier_opt (cp_parser *parser)
22856 {
22857 cp_token *token = cp_lexer_peek_token (parser->lexer);
22858 if (token->type == CPP_NAME)
22859 {
22860 tree name = token->u.value;
22861 const char *p = IDENTIFIER_POINTER (name);
22862 const int len = strlen ("transaction_safe");
22863 if (!strncmp (p, "transaction_safe", len))
22864 {
22865 p += len;
22866 if (*p == '\0'
22867 || !strcmp (p, "_dynamic"))
22868 {
22869 cp_lexer_consume_token (parser->lexer);
22870 if (!flag_tm)
22871 {
22872 error ("%qE requires %<-fgnu-tm%>", name);
22873 return NULL_TREE;
22874 }
22875 else
22876 return name;
22877 }
22878 }
22879 }
22880 return NULL_TREE;
22881 }
22882
22883 /* Parse an (optional) virt-specifier-seq.
22884
22885 virt-specifier-seq:
22886 virt-specifier virt-specifier-seq [opt]
22887
22888 virt-specifier:
22889 override
22890 final
22891
22892 Returns a bitmask representing the virt-specifiers. */
22893
22894 static cp_virt_specifiers
22895 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
22896 {
22897 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22898
22899 while (true)
22900 {
22901 cp_token *token;
22902 cp_virt_specifiers virt_specifier;
22903
22904 /* Peek at the next token. */
22905 token = cp_lexer_peek_token (parser->lexer);
22906 /* See if it's a virt-specifier-qualifier. */
22907 if (token->type != CPP_NAME)
22908 break;
22909 if (id_equal (token->u.value, "override"))
22910 {
22911 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
22912 virt_specifier = VIRT_SPEC_OVERRIDE;
22913 }
22914 else if (id_equal (token->u.value, "final"))
22915 {
22916 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
22917 virt_specifier = VIRT_SPEC_FINAL;
22918 }
22919 else if (id_equal (token->u.value, "__final"))
22920 {
22921 virt_specifier = VIRT_SPEC_FINAL;
22922 }
22923 else
22924 break;
22925
22926 if (virt_specifiers & virt_specifier)
22927 {
22928 gcc_rich_location richloc (token->location);
22929 richloc.add_fixit_remove ();
22930 error_at (&richloc, "duplicate virt-specifier");
22931 cp_lexer_purge_token (parser->lexer);
22932 }
22933 else
22934 {
22935 cp_lexer_consume_token (parser->lexer);
22936 virt_specifiers |= virt_specifier;
22937 }
22938 }
22939 return virt_specifiers;
22940 }
22941
22942 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
22943 is in scope even though it isn't real. */
22944
22945 void
22946 inject_this_parameter (tree ctype, cp_cv_quals quals)
22947 {
22948 tree this_parm;
22949
22950 if (current_class_ptr)
22951 {
22952 /* We don't clear this between NSDMIs. Is it already what we want? */
22953 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
22954 if (DECL_P (current_class_ptr)
22955 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
22956 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
22957 && cp_type_quals (type) == quals)
22958 return;
22959 }
22960
22961 this_parm = build_this_parm (NULL_TREE, ctype, quals);
22962 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
22963 current_class_ptr = NULL_TREE;
22964 current_class_ref
22965 = cp_build_fold_indirect_ref (this_parm);
22966 current_class_ptr = this_parm;
22967 }
22968
22969 /* Return true iff our current scope is a non-static data member
22970 initializer. */
22971
22972 bool
22973 parsing_nsdmi (void)
22974 {
22975 /* We recognize NSDMI context by the context-less 'this' pointer set up
22976 by the function above. */
22977 if (current_class_ptr
22978 && TREE_CODE (current_class_ptr) == PARM_DECL
22979 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
22980 return true;
22981 return false;
22982 }
22983
22984 /* Parse a late-specified return type, if any. This is not a separate
22985 non-terminal, but part of a function declarator, which looks like
22986
22987 -> trailing-type-specifier-seq abstract-declarator(opt)
22988
22989 Returns the type indicated by the type-id.
22990
22991 In addition to this, parse any queued up #pragma omp declare simd
22992 clauses, and #pragma acc routine clauses.
22993
22994 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
22995 function. */
22996
22997 static tree
22998 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
22999 tree& requires_clause)
23000 {
23001 cp_token *token;
23002 tree type = NULL_TREE;
23003 bool declare_simd_p = (parser->omp_declare_simd
23004 && declarator
23005 && declarator->kind == cdk_id);
23006
23007 bool oacc_routine_p = (parser->oacc_routine
23008 && declarator
23009 && declarator->kind == cdk_id);
23010
23011 /* Peek at the next token. */
23012 token = cp_lexer_peek_token (parser->lexer);
23013 /* A late-specified return type is indicated by an initial '->'. */
23014 if (token->type != CPP_DEREF
23015 && token->keyword != RID_REQUIRES
23016 && !(token->type == CPP_NAME
23017 && token->u.value == ridpointers[RID_REQUIRES])
23018 && !(declare_simd_p || oacc_routine_p))
23019 return NULL_TREE;
23020
23021 if (token->type == CPP_DEREF)
23022 {
23023 /* Consume the ->. */
23024 cp_lexer_consume_token (parser->lexer);
23025
23026 type = cp_parser_trailing_type_id (parser);
23027 }
23028
23029 /* Function declarations may be followed by a trailing
23030 requires-clause. */
23031 requires_clause = cp_parser_requires_clause_opt (parser, false);
23032
23033 if (declare_simd_p)
23034 declarator->attributes
23035 = cp_parser_late_parsing_omp_declare_simd (parser,
23036 declarator->attributes);
23037 if (oacc_routine_p)
23038 declarator->attributes
23039 = cp_parser_late_parsing_oacc_routine (parser,
23040 declarator->attributes);
23041
23042 return type;
23043 }
23044
23045 /* Parse a declarator-id.
23046
23047 declarator-id:
23048 id-expression
23049 :: [opt] nested-name-specifier [opt] type-name
23050
23051 In the `id-expression' case, the value returned is as for
23052 cp_parser_id_expression if the id-expression was an unqualified-id.
23053 If the id-expression was a qualified-id, then a SCOPE_REF is
23054 returned. The first operand is the scope (either a NAMESPACE_DECL
23055 or TREE_TYPE), but the second is still just a representation of an
23056 unqualified-id. */
23057
23058 static tree
23059 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
23060 {
23061 tree id;
23062 /* The expression must be an id-expression. Assume that qualified
23063 names are the names of types so that:
23064
23065 template <class T>
23066 int S<T>::R::i = 3;
23067
23068 will work; we must treat `S<T>::R' as the name of a type.
23069 Similarly, assume that qualified names are templates, where
23070 required, so that:
23071
23072 template <class T>
23073 int S<T>::R<T>::i = 3;
23074
23075 will work, too. */
23076 id = cp_parser_id_expression (parser,
23077 /*template_keyword_p=*/false,
23078 /*check_dependency_p=*/false,
23079 /*template_p=*/NULL,
23080 /*declarator_p=*/true,
23081 optional_p);
23082 if (id && BASELINK_P (id))
23083 id = BASELINK_FUNCTIONS (id);
23084 return id;
23085 }
23086
23087 /* Parse a type-id.
23088
23089 type-id:
23090 type-specifier-seq abstract-declarator [opt]
23091
23092 The parser flags FLAGS is used to control type-specifier parsing.
23093
23094 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
23095
23096 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
23097 i.e. we've just seen "->".
23098
23099 Returns the TYPE specified. */
23100
23101 static tree
23102 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
23103 bool is_template_arg, bool is_trailing_return,
23104 location_t *type_location)
23105 {
23106 cp_decl_specifier_seq type_specifier_seq;
23107 cp_declarator *abstract_declarator;
23108
23109 /* Parse the type-specifier-seq. */
23110 cp_parser_type_specifier_seq (parser, flags,
23111 /*is_declaration=*/false,
23112 is_trailing_return,
23113 &type_specifier_seq);
23114 if (type_location)
23115 *type_location = type_specifier_seq.locations[ds_type_spec];
23116
23117 if (is_template_arg && type_specifier_seq.type
23118 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
23119 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
23120 /* A bare template name as a template argument is a template template
23121 argument, not a placeholder, so fail parsing it as a type argument. */
23122 {
23123 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
23124 cp_parser_simulate_error (parser);
23125 return error_mark_node;
23126 }
23127 if (type_specifier_seq.type == error_mark_node)
23128 return error_mark_node;
23129
23130 /* There might or might not be an abstract declarator. */
23131 cp_parser_parse_tentatively (parser);
23132 /* Look for the declarator. */
23133 abstract_declarator
23134 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
23135 CP_PARSER_FLAGS_NONE, NULL,
23136 /*parenthesized_p=*/NULL,
23137 /*member_p=*/false,
23138 /*friend_p=*/false,
23139 /*static_p=*/false);
23140 /* Check to see if there really was a declarator. */
23141 if (!cp_parser_parse_definitely (parser))
23142 abstract_declarator = NULL;
23143
23144 bool auto_typeid_ok = false;
23145 /* The concepts TS allows 'auto' as a type-id. */
23146 if (flag_concepts_ts)
23147 auto_typeid_ok = !parser->in_type_id_in_expr_p;
23148 /* DR 625 prohibits use of auto as a template-argument. We allow 'auto'
23149 outside the template-argument-list context here only for the sake of
23150 diagnostic: grokdeclarator then can emit a better error message for
23151 e.g. using T = auto. */
23152 else if (flag_concepts)
23153 auto_typeid_ok = (!parser->in_type_id_in_expr_p
23154 && !parser->in_template_argument_list_p);
23155
23156 if (type_specifier_seq.type
23157 && !auto_typeid_ok
23158 /* None of the valid uses of 'auto' in C++14 involve the type-id
23159 nonterminal, but it is valid in a trailing-return-type. */
23160 && !(cxx_dialect >= cxx14 && is_trailing_return))
23161 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
23162 {
23163 /* A type-id with type 'auto' is only ok if the abstract declarator
23164 is a function declarator with a late-specified return type.
23165
23166 A type-id with 'auto' is also valid in a trailing-return-type
23167 in a compound-requirement. */
23168 if (abstract_declarator
23169 && abstract_declarator->kind == cdk_function
23170 && abstract_declarator->u.function.late_return_type)
23171 /* OK */;
23172 else if (parser->in_result_type_constraint_p)
23173 /* OK */;
23174 else
23175 {
23176 location_t loc = type_specifier_seq.locations[ds_type_spec];
23177 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
23178 {
23179 error_at (loc, "missing template arguments after %qT",
23180 auto_node);
23181 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
23182 tmpl);
23183 }
23184 else if (parser->in_template_argument_list_p)
23185 error_at (loc, "%qT not permitted in template argument",
23186 auto_node);
23187 else
23188 error_at (loc, "invalid use of %qT", auto_node);
23189 return error_mark_node;
23190 }
23191 }
23192
23193 return groktypename (&type_specifier_seq, abstract_declarator,
23194 is_template_arg);
23195 }
23196
23197 /* Wrapper for cp_parser_type_id_1. */
23198
23199 static tree
23200 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
23201 location_t *type_location)
23202 {
23203 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
23204 }
23205
23206 /* Wrapper for cp_parser_type_id_1. */
23207
23208 static tree
23209 cp_parser_template_type_arg (cp_parser *parser)
23210 {
23211 tree r;
23212 const char *saved_message = parser->type_definition_forbidden_message;
23213 parser->type_definition_forbidden_message
23214 = G_("types may not be defined in template arguments");
23215 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
23216 parser->type_definition_forbidden_message = saved_message;
23217 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
23218 {
23219 error ("invalid use of %<auto%> in template argument");
23220 r = error_mark_node;
23221 }
23222 return r;
23223 }
23224
23225 /* Wrapper for cp_parser_type_id_1. */
23226
23227 static tree
23228 cp_parser_trailing_type_id (cp_parser *parser)
23229 {
23230 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
23231 false, true, NULL);
23232 }
23233
23234 /* Parse a type-specifier-seq.
23235
23236 type-specifier-seq:
23237 type-specifier type-specifier-seq [opt]
23238
23239 GNU extension:
23240
23241 type-specifier-seq:
23242 attributes type-specifier-seq [opt]
23243
23244 The parser flags FLAGS is used to control type-specifier parsing.
23245
23246 If IS_DECLARATION is true, we are at the start of a "condition" or
23247 exception-declaration, so we might be followed by a declarator-id.
23248
23249 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
23250 i.e. we've just seen "->".
23251
23252 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
23253
23254 static void
23255 cp_parser_type_specifier_seq (cp_parser* parser,
23256 cp_parser_flags flags,
23257 bool is_declaration,
23258 bool is_trailing_return,
23259 cp_decl_specifier_seq *type_specifier_seq)
23260 {
23261 bool seen_type_specifier = false;
23262 cp_token *start_token = NULL;
23263
23264 /* Clear the TYPE_SPECIFIER_SEQ. */
23265 clear_decl_specs (type_specifier_seq);
23266
23267 flags |= CP_PARSER_FLAGS_OPTIONAL;
23268 /* In the context of a trailing return type, enum E { } is an
23269 elaborated-type-specifier followed by a function-body, not an
23270 enum-specifier. */
23271 if (is_trailing_return)
23272 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
23273
23274 /* Parse the type-specifiers and attributes. */
23275 while (true)
23276 {
23277 tree type_specifier;
23278 bool is_cv_qualifier;
23279
23280 /* Check for attributes first. */
23281 if (cp_next_tokens_can_be_attribute_p (parser))
23282 {
23283 /* GNU attributes at the end of a declaration apply to the
23284 declaration as a whole, not to the trailing return type. So look
23285 ahead to see if these attributes are at the end. */
23286 if (seen_type_specifier && is_trailing_return
23287 && cp_next_tokens_can_be_gnu_attribute_p (parser))
23288 {
23289 size_t n = cp_parser_skip_attributes_opt (parser, 1);
23290 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
23291 if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
23292 || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
23293 break;
23294 }
23295 type_specifier_seq->attributes
23296 = attr_chainon (type_specifier_seq->attributes,
23297 cp_parser_attributes_opt (parser));
23298 continue;
23299 }
23300
23301 /* record the token of the beginning of the type specifier seq,
23302 for error reporting purposes*/
23303 if (!start_token)
23304 start_token = cp_lexer_peek_token (parser->lexer);
23305
23306 /* Look for the type-specifier. */
23307 type_specifier = cp_parser_type_specifier (parser,
23308 flags,
23309 type_specifier_seq,
23310 /*is_declaration=*/false,
23311 NULL,
23312 &is_cv_qualifier);
23313 if (!type_specifier)
23314 {
23315 /* If the first type-specifier could not be found, this is not a
23316 type-specifier-seq at all. */
23317 if (!seen_type_specifier)
23318 {
23319 /* Set in_declarator_p to avoid skipping to the semicolon. */
23320 int in_decl = parser->in_declarator_p;
23321 parser->in_declarator_p = true;
23322
23323 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
23324 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
23325 cp_parser_error (parser, "expected type-specifier");
23326
23327 parser->in_declarator_p = in_decl;
23328
23329 type_specifier_seq->type = error_mark_node;
23330 return;
23331 }
23332 /* If subsequent type-specifiers could not be found, the
23333 type-specifier-seq is complete. */
23334 break;
23335 }
23336
23337 seen_type_specifier = true;
23338 /* The standard says that a condition can be:
23339
23340 type-specifier-seq declarator = assignment-expression
23341
23342 However, given:
23343
23344 struct S {};
23345 if (int S = ...)
23346
23347 we should treat the "S" as a declarator, not as a
23348 type-specifier. The standard doesn't say that explicitly for
23349 type-specifier-seq, but it does say that for
23350 decl-specifier-seq in an ordinary declaration. Perhaps it
23351 would be clearer just to allow a decl-specifier-seq here, and
23352 then add a semantic restriction that if any decl-specifiers
23353 that are not type-specifiers appear, the program is invalid. */
23354 if (is_declaration && !is_cv_qualifier)
23355 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
23356 }
23357 }
23358
23359 /* Return whether the function currently being declared has an associated
23360 template parameter list. */
23361
23362 static bool
23363 function_being_declared_is_template_p (cp_parser* parser)
23364 {
23365 if (!current_template_parms || processing_template_parmlist)
23366 return false;
23367
23368 if (parser->implicit_template_scope)
23369 return true;
23370
23371 if (at_class_scope_p ()
23372 && TYPE_BEING_DEFINED (current_class_type))
23373 return parser->num_template_parameter_lists != 0;
23374
23375 return ((int) parser->num_template_parameter_lists > template_class_depth
23376 (current_class_type));
23377 }
23378
23379 /* Parse a parameter-declaration-clause.
23380
23381 parameter-declaration-clause:
23382 parameter-declaration-list [opt] ... [opt]
23383 parameter-declaration-list , ...
23384
23385 The parser flags FLAGS is used to control type-specifier parsing.
23386
23387 Returns a representation for the parameter declarations. A return
23388 value of NULL indicates a parameter-declaration-clause consisting
23389 only of an ellipsis. */
23390
23391 static tree
23392 cp_parser_parameter_declaration_clause (cp_parser* parser,
23393 cp_parser_flags flags)
23394 {
23395 tree parameters;
23396 cp_token *token;
23397 bool ellipsis_p;
23398
23399 auto cleanup = make_temp_override
23400 (parser->auto_is_implicit_function_template_parm_p);
23401
23402 if (!processing_specialization
23403 && !processing_template_parmlist
23404 && !processing_explicit_instantiation
23405 /* default_arg_ok_p tracks whether this is a parameter-clause for an
23406 actual function or a random abstract declarator. */
23407 && parser->default_arg_ok_p)
23408 if (!current_function_decl
23409 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
23410 parser->auto_is_implicit_function_template_parm_p = true;
23411
23412 /* Peek at the next token. */
23413 token = cp_lexer_peek_token (parser->lexer);
23414 /* Check for trivial parameter-declaration-clauses. */
23415 if (token->type == CPP_ELLIPSIS)
23416 {
23417 /* Consume the `...' token. */
23418 cp_lexer_consume_token (parser->lexer);
23419 return NULL_TREE;
23420 }
23421 else if (token->type == CPP_CLOSE_PAREN)
23422 /* There are no parameters. */
23423 return void_list_node;
23424 /* Check for `(void)', too, which is a special case. */
23425 else if (token->keyword == RID_VOID
23426 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23427 == CPP_CLOSE_PAREN))
23428 {
23429 /* Consume the `void' token. */
23430 cp_lexer_consume_token (parser->lexer);
23431 /* There are no parameters. */
23432 return explicit_void_list_node;
23433 }
23434
23435 /* Parse the parameter-declaration-list. */
23436 parameters = cp_parser_parameter_declaration_list (parser, flags);
23437 /* If a parse error occurred while parsing the
23438 parameter-declaration-list, then the entire
23439 parameter-declaration-clause is erroneous. */
23440 if (parameters == error_mark_node)
23441 return NULL_TREE;
23442
23443 /* Peek at the next token. */
23444 token = cp_lexer_peek_token (parser->lexer);
23445 /* If it's a `,', the clause should terminate with an ellipsis. */
23446 if (token->type == CPP_COMMA)
23447 {
23448 /* Consume the `,'. */
23449 cp_lexer_consume_token (parser->lexer);
23450 /* Expect an ellipsis. */
23451 ellipsis_p
23452 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
23453 }
23454 /* It might also be `...' if the optional trailing `,' was
23455 omitted. */
23456 else if (token->type == CPP_ELLIPSIS)
23457 {
23458 /* Consume the `...' token. */
23459 cp_lexer_consume_token (parser->lexer);
23460 /* And remember that we saw it. */
23461 ellipsis_p = true;
23462 }
23463 else
23464 ellipsis_p = false;
23465
23466 /* Finish the parameter list. */
23467 if (!ellipsis_p)
23468 parameters = chainon (parameters, void_list_node);
23469
23470 return parameters;
23471 }
23472
23473 /* Parse a parameter-declaration-list.
23474
23475 parameter-declaration-list:
23476 parameter-declaration
23477 parameter-declaration-list , parameter-declaration
23478
23479 The parser flags FLAGS is used to control type-specifier parsing.
23480
23481 Returns a representation of the parameter-declaration-list, as for
23482 cp_parser_parameter_declaration_clause. However, the
23483 `void_list_node' is never appended to the list. */
23484
23485 static tree
23486 cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
23487 {
23488 tree parameters = NULL_TREE;
23489 tree *tail = &parameters;
23490 bool saved_in_unbraced_linkage_specification_p;
23491 int index = 0;
23492
23493 /* The special considerations that apply to a function within an
23494 unbraced linkage specifications do not apply to the parameters
23495 to the function. */
23496 saved_in_unbraced_linkage_specification_p
23497 = parser->in_unbraced_linkage_specification_p;
23498 parser->in_unbraced_linkage_specification_p = false;
23499
23500 /* Look for more parameters. */
23501 while (true)
23502 {
23503 cp_parameter_declarator *parameter;
23504 tree decl = error_mark_node;
23505 bool parenthesized_p = false;
23506
23507 /* Parse the parameter. */
23508 parameter
23509 = cp_parser_parameter_declaration (parser, flags,
23510 /*template_parm_p=*/false,
23511 &parenthesized_p);
23512
23513 /* We don't know yet if the enclosing context is deprecated, so wait
23514 and warn in grokparms if appropriate. */
23515 deprecated_state = DEPRECATED_SUPPRESS;
23516
23517 if (parameter)
23518 {
23519 decl = grokdeclarator (parameter->declarator,
23520 &parameter->decl_specifiers,
23521 PARM,
23522 parameter->default_argument != NULL_TREE,
23523 &parameter->decl_specifiers.attributes);
23524 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
23525 DECL_SOURCE_LOCATION (decl) = parameter->loc;
23526 }
23527
23528 deprecated_state = DEPRECATED_NORMAL;
23529
23530 /* If a parse error occurred parsing the parameter declaration,
23531 then the entire parameter-declaration-list is erroneous. */
23532 if (decl == error_mark_node)
23533 {
23534 parameters = error_mark_node;
23535 break;
23536 }
23537
23538 if (parameter->decl_specifiers.attributes)
23539 cplus_decl_attributes (&decl,
23540 parameter->decl_specifiers.attributes,
23541 0);
23542 if (DECL_NAME (decl))
23543 decl = pushdecl (decl);
23544
23545 if (decl != error_mark_node)
23546 {
23547 retrofit_lang_decl (decl);
23548 DECL_PARM_INDEX (decl) = ++index;
23549 DECL_PARM_LEVEL (decl) = function_parm_depth ();
23550 }
23551
23552 /* Add the new parameter to the list. */
23553 *tail = build_tree_list (parameter->default_argument, decl);
23554 tail = &TREE_CHAIN (*tail);
23555
23556 /* If the parameters were parenthesized, it's the case of
23557 T foo(X(x)) which looks like a variable definition but
23558 is a function declaration. */
23559 if (index == 1 || PARENTHESIZED_LIST_P (parameters))
23560 PARENTHESIZED_LIST_P (parameters) = parenthesized_p;
23561
23562 /* Peek at the next token. */
23563 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
23564 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
23565 /* These are for Objective-C++ */
23566 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23567 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23568 /* The parameter-declaration-list is complete. */
23569 break;
23570 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23571 {
23572 cp_token *token;
23573
23574 /* Peek at the next token. */
23575 token = cp_lexer_peek_nth_token (parser->lexer, 2);
23576 /* If it's an ellipsis, then the list is complete. */
23577 if (token->type == CPP_ELLIPSIS)
23578 break;
23579 /* Otherwise, there must be more parameters. Consume the
23580 `,'. */
23581 cp_lexer_consume_token (parser->lexer);
23582 /* When parsing something like:
23583
23584 int i(float f, double d)
23585
23586 we can tell after seeing the declaration for "f" that we
23587 are not looking at an initialization of a variable "i",
23588 but rather at the declaration of a function "i".
23589
23590 Due to the fact that the parsing of template arguments
23591 (as specified to a template-id) requires backtracking we
23592 cannot use this technique when inside a template argument
23593 list. */
23594 if (!parser->in_template_argument_list_p
23595 && !parser->in_type_id_in_expr_p
23596 && cp_parser_uncommitted_to_tentative_parse_p (parser)
23597 /* However, a parameter-declaration of the form
23598 "float(f)" (which is a valid declaration of a
23599 parameter "f") can also be interpreted as an
23600 expression (the conversion of "f" to "float"). */
23601 && !parenthesized_p)
23602 cp_parser_commit_to_tentative_parse (parser);
23603 }
23604 else
23605 {
23606 cp_parser_error (parser, "expected %<,%> or %<...%>");
23607 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
23608 cp_parser_skip_to_closing_parenthesis (parser,
23609 /*recovering=*/true,
23610 /*or_comma=*/false,
23611 /*consume_paren=*/false);
23612 break;
23613 }
23614 }
23615
23616 parser->in_unbraced_linkage_specification_p
23617 = saved_in_unbraced_linkage_specification_p;
23618
23619 /* Reset implicit_template_scope if we are about to leave the function
23620 parameter list that introduced it. Note that for out-of-line member
23621 definitions, there will be one or more class scopes before we get to
23622 the template parameter scope. */
23623
23624 if (cp_binding_level *its = parser->implicit_template_scope)
23625 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
23626 {
23627 while (maybe_its->kind == sk_class)
23628 maybe_its = maybe_its->level_chain;
23629 if (maybe_its == its)
23630 {
23631 parser->implicit_template_parms = 0;
23632 parser->implicit_template_scope = 0;
23633 }
23634 }
23635
23636 return parameters;
23637 }
23638
23639 /* Parse a parameter declaration.
23640
23641 parameter-declaration:
23642 decl-specifier-seq ... [opt] declarator
23643 decl-specifier-seq declarator = assignment-expression
23644 decl-specifier-seq ... [opt] abstract-declarator [opt]
23645 decl-specifier-seq abstract-declarator [opt] = assignment-expression
23646
23647 The parser flags FLAGS is used to control type-specifier parsing.
23648
23649 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
23650 declares a template parameter. (In that case, a non-nested `>'
23651 token encountered during the parsing of the assignment-expression
23652 is not interpreted as a greater-than operator.)
23653
23654 Returns a representation of the parameter, or NULL if an error
23655 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
23656 true iff the declarator is of the form "(p)". */
23657
23658 static cp_parameter_declarator *
23659 cp_parser_parameter_declaration (cp_parser *parser,
23660 cp_parser_flags flags,
23661 bool template_parm_p,
23662 bool *parenthesized_p)
23663 {
23664 int declares_class_or_enum;
23665 cp_decl_specifier_seq decl_specifiers;
23666 cp_declarator *declarator;
23667 tree default_argument;
23668 cp_token *token = NULL, *declarator_token_start = NULL;
23669 const char *saved_message;
23670 bool template_parameter_pack_p = false;
23671
23672 /* In a template parameter, `>' is not an operator.
23673
23674 [temp.param]
23675
23676 When parsing a default template-argument for a non-type
23677 template-parameter, the first non-nested `>' is taken as the end
23678 of the template parameter-list rather than a greater-than
23679 operator. */
23680
23681 /* Type definitions may not appear in parameter types. */
23682 saved_message = parser->type_definition_forbidden_message;
23683 parser->type_definition_forbidden_message
23684 = G_("types may not be defined in parameter types");
23685
23686 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
23687 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
23688 (current_template_parms)) : 0);
23689
23690 /* Parse the declaration-specifiers. */
23691 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23692 cp_parser_decl_specifier_seq (parser,
23693 flags,
23694 &decl_specifiers,
23695 &declares_class_or_enum);
23696
23697 /* Complain about missing 'typename' or other invalid type names. */
23698 if (!decl_specifiers.any_type_specifiers_p
23699 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23700 decl_specifiers.type = error_mark_node;
23701
23702 /* If an error occurred, there's no reason to attempt to parse the
23703 rest of the declaration. */
23704 if (cp_parser_error_occurred (parser))
23705 {
23706 parser->type_definition_forbidden_message = saved_message;
23707 return NULL;
23708 }
23709
23710 /* Peek at the next token. */
23711 token = cp_lexer_peek_token (parser->lexer);
23712
23713 /* If the next token is a `)', `,', `=', `>', or `...', then there
23714 is no declarator. However, when variadic templates are enabled,
23715 there may be a declarator following `...'. */
23716 if (token->type == CPP_CLOSE_PAREN
23717 || token->type == CPP_COMMA
23718 || token->type == CPP_EQ
23719 || token->type == CPP_GREATER)
23720 {
23721 declarator = NULL;
23722 if (parenthesized_p)
23723 *parenthesized_p = false;
23724 }
23725 /* Otherwise, there should be a declarator. */
23726 else
23727 {
23728 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
23729 parser->default_arg_ok_p = false;
23730
23731 /* After seeing a decl-specifier-seq, if the next token is not a
23732 "(", there is no possibility that the code is a valid
23733 expression. Therefore, if parsing tentatively, we commit at
23734 this point. */
23735 if (!parser->in_template_argument_list_p
23736 /* In an expression context, having seen:
23737
23738 (int((char ...
23739
23740 we cannot be sure whether we are looking at a
23741 function-type (taking a "char" as a parameter) or a cast
23742 of some object of type "char" to "int". */
23743 && !parser->in_type_id_in_expr_p
23744 && cp_parser_uncommitted_to_tentative_parse_p (parser)
23745 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
23746 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
23747 cp_parser_commit_to_tentative_parse (parser);
23748 /* Parse the declarator. */
23749 declarator_token_start = token;
23750 declarator = cp_parser_declarator (parser,
23751 CP_PARSER_DECLARATOR_EITHER,
23752 CP_PARSER_FLAGS_NONE,
23753 /*ctor_dtor_or_conv_p=*/NULL,
23754 parenthesized_p,
23755 /*member_p=*/false,
23756 /*friend_p=*/false,
23757 /*static_p=*/false);
23758 parser->default_arg_ok_p = saved_default_arg_ok_p;
23759 /* After the declarator, allow more attributes. */
23760 decl_specifiers.attributes
23761 = attr_chainon (decl_specifiers.attributes,
23762 cp_parser_attributes_opt (parser));
23763
23764 /* If the declarator is a template parameter pack, remember that and
23765 clear the flag in the declarator itself so we don't get errors
23766 from grokdeclarator. */
23767 if (template_parm_p && declarator && declarator->parameter_pack_p)
23768 {
23769 declarator->parameter_pack_p = false;
23770 template_parameter_pack_p = true;
23771 }
23772 }
23773
23774 /* If the next token is an ellipsis, and we have not seen a declarator
23775 name, and if either the type of the declarator contains parameter
23776 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
23777 for, eg, abbreviated integral type names), then we actually have a
23778 parameter pack expansion expression. Otherwise, leave the ellipsis
23779 for a C-style variadic function. */
23780 token = cp_lexer_peek_token (parser->lexer);
23781
23782 /* If a function parameter pack was specified and an implicit template
23783 parameter was introduced during cp_parser_parameter_declaration,
23784 change any implicit parameters introduced into packs. */
23785 if (parser->implicit_template_parms
23786 && ((token->type == CPP_ELLIPSIS
23787 && declarator_can_be_parameter_pack (declarator))
23788 || (declarator && declarator->parameter_pack_p)))
23789 {
23790 int latest_template_parm_idx = TREE_VEC_LENGTH
23791 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
23792
23793 if (latest_template_parm_idx != template_parm_idx)
23794 decl_specifiers.type = convert_generic_types_to_packs
23795 (decl_specifiers.type,
23796 template_parm_idx, latest_template_parm_idx);
23797 }
23798
23799 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23800 {
23801 tree type = decl_specifiers.type;
23802
23803 if (type && DECL_P (type))
23804 type = TREE_TYPE (type);
23805
23806 if (((type
23807 && TREE_CODE (type) != TYPE_PACK_EXPANSION
23808 && (template_parm_p || uses_parameter_packs (type)))
23809 || (!type && template_parm_p))
23810 && declarator_can_be_parameter_pack (declarator))
23811 {
23812 /* Consume the `...'. */
23813 cp_lexer_consume_token (parser->lexer);
23814 maybe_warn_variadic_templates ();
23815
23816 /* Build a pack expansion type */
23817 if (template_parm_p)
23818 template_parameter_pack_p = true;
23819 else if (declarator)
23820 declarator->parameter_pack_p = true;
23821 else
23822 decl_specifiers.type = make_pack_expansion (type);
23823 }
23824 }
23825
23826 /* The restriction on defining new types applies only to the type
23827 of the parameter, not to the default argument. */
23828 parser->type_definition_forbidden_message = saved_message;
23829
23830 /* If the next token is `=', then process a default argument. */
23831 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23832 {
23833 tree type = decl_specifiers.type;
23834 token = cp_lexer_peek_token (parser->lexer);
23835 /* If we are defining a class, then the tokens that make up the
23836 default argument must be saved and processed later. */
23837 if (!template_parm_p && at_class_scope_p ()
23838 && TYPE_BEING_DEFINED (current_class_type)
23839 && !LAMBDA_TYPE_P (current_class_type))
23840 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
23841
23842 /* A constrained-type-specifier may declare a type
23843 template-parameter. */
23844 else if (declares_constrained_type_template_parameter (type))
23845 default_argument
23846 = cp_parser_default_type_template_argument (parser);
23847
23848 /* A constrained-type-specifier may declare a
23849 template-template-parameter. */
23850 else if (declares_constrained_template_template_parameter (type))
23851 default_argument
23852 = cp_parser_default_template_template_argument (parser);
23853
23854 /* Outside of a class definition, we can just parse the
23855 assignment-expression. */
23856 else
23857 default_argument
23858 = cp_parser_default_argument (parser, template_parm_p);
23859
23860 if (!parser->default_arg_ok_p)
23861 {
23862 permerror (token->location,
23863 "default arguments are only "
23864 "permitted for function parameters");
23865 }
23866 else if ((declarator && declarator->parameter_pack_p)
23867 || template_parameter_pack_p
23868 || (decl_specifiers.type
23869 && PACK_EXPANSION_P (decl_specifiers.type)))
23870 {
23871 /* Find the name of the parameter pack. */
23872 cp_declarator *id_declarator = declarator;
23873 while (id_declarator && id_declarator->kind != cdk_id)
23874 id_declarator = id_declarator->declarator;
23875
23876 if (id_declarator && id_declarator->kind == cdk_id)
23877 error_at (declarator_token_start->location,
23878 template_parm_p
23879 ? G_("template parameter pack %qD "
23880 "cannot have a default argument")
23881 : G_("parameter pack %qD cannot have "
23882 "a default argument"),
23883 id_declarator->u.id.unqualified_name);
23884 else
23885 error_at (declarator_token_start->location,
23886 template_parm_p
23887 ? G_("template parameter pack cannot have "
23888 "a default argument")
23889 : G_("parameter pack cannot have a "
23890 "default argument"));
23891
23892 default_argument = NULL_TREE;
23893 }
23894 }
23895 else
23896 default_argument = NULL_TREE;
23897
23898 if (default_argument)
23899 STRIP_ANY_LOCATION_WRAPPER (default_argument);
23900
23901 /* Generate a location for the parameter, ranging from the start of the
23902 initial token to the end of the final token (using input_location for
23903 the latter, set up by cp_lexer_set_source_position_from_token when
23904 consuming tokens).
23905
23906 If we have a identifier, then use it for the caret location, e.g.
23907
23908 extern int callee (int one, int (*two)(int, int), float three);
23909 ~~~~~~^~~~~~~~~~~~~~
23910
23911 otherwise, reuse the start location for the caret location e.g.:
23912
23913 extern int callee (int one, int (*)(int, int), float three);
23914 ^~~~~~~~~~~~~~~~~
23915
23916 */
23917 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
23918 ? declarator->id_loc
23919 : decl_spec_token_start->location);
23920 location_t param_loc = make_location (caret_loc,
23921 decl_spec_token_start->location,
23922 input_location);
23923
23924 return make_parameter_declarator (&decl_specifiers,
23925 declarator,
23926 default_argument,
23927 param_loc,
23928 template_parameter_pack_p);
23929 }
23930
23931 /* Parse a default argument and return it.
23932
23933 TEMPLATE_PARM_P is true if this is a default argument for a
23934 non-type template parameter. */
23935 static tree
23936 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
23937 {
23938 tree default_argument = NULL_TREE;
23939 bool saved_greater_than_is_operator_p;
23940 unsigned char saved_local_variables_forbidden_p;
23941 bool non_constant_p, is_direct_init;
23942
23943 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
23944 set correctly. */
23945 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
23946 parser->greater_than_is_operator_p = !template_parm_p;
23947 /* Local variable names (and the `this' keyword) may not
23948 appear in a default argument. */
23949 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23950 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
23951 /* Parse the assignment-expression. */
23952 if (template_parm_p)
23953 push_deferring_access_checks (dk_no_deferred);
23954 tree saved_class_ptr = NULL_TREE;
23955 tree saved_class_ref = NULL_TREE;
23956 /* The "this" pointer is not valid in a default argument. */
23957 if (cfun)
23958 {
23959 saved_class_ptr = current_class_ptr;
23960 cp_function_chain->x_current_class_ptr = NULL_TREE;
23961 saved_class_ref = current_class_ref;
23962 cp_function_chain->x_current_class_ref = NULL_TREE;
23963 }
23964 default_argument
23965 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
23966 /* Restore the "this" pointer. */
23967 if (cfun)
23968 {
23969 cp_function_chain->x_current_class_ptr = saved_class_ptr;
23970 cp_function_chain->x_current_class_ref = saved_class_ref;
23971 }
23972 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
23973 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23974 if (template_parm_p)
23975 pop_deferring_access_checks ();
23976 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
23977 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
23978
23979 return default_argument;
23980 }
23981
23982 /* Parse a function-body.
23983
23984 function-body:
23985 compound_statement */
23986
23987 static void
23988 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
23989 {
23990 cp_parser_compound_statement (parser, NULL, (in_function_try_block
23991 ? BCS_TRY_BLOCK : BCS_NORMAL),
23992 true);
23993 }
23994
23995 /* Parse a ctor-initializer-opt followed by a function-body. Return
23996 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
23997 is true we are parsing a function-try-block. */
23998
23999 static void
24000 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
24001 bool in_function_try_block)
24002 {
24003 tree body, list;
24004 const bool check_body_p
24005 = (DECL_CONSTRUCTOR_P (current_function_decl)
24006 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
24007 tree last = NULL;
24008
24009 if (in_function_try_block
24010 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
24011 && cxx_dialect < cxx20)
24012 {
24013 if (DECL_CONSTRUCTOR_P (current_function_decl))
24014 pedwarn (input_location, 0,
24015 "function-try-block body of %<constexpr%> constructor only "
24016 "available with %<-std=c++20%> or %<-std=gnu++20%>");
24017 else
24018 pedwarn (input_location, 0,
24019 "function-try-block body of %<constexpr%> function only "
24020 "available with %<-std=c++20%> or %<-std=gnu++20%>");
24021 }
24022
24023 /* Begin the function body. */
24024 body = begin_function_body ();
24025 /* Parse the optional ctor-initializer. */
24026 cp_parser_ctor_initializer_opt (parser);
24027
24028 /* If we're parsing a constexpr constructor definition, we need
24029 to check that the constructor body is indeed empty. However,
24030 before we get to cp_parser_function_body lot of junk has been
24031 generated, so we can't just check that we have an empty block.
24032 Rather we take a snapshot of the outermost block, and check whether
24033 cp_parser_function_body changed its state. */
24034 if (check_body_p)
24035 {
24036 list = cur_stmt_list;
24037 if (STATEMENT_LIST_TAIL (list))
24038 last = STATEMENT_LIST_TAIL (list)->stmt;
24039 }
24040 /* Parse the function-body. */
24041 cp_parser_function_body (parser, in_function_try_block);
24042 if (check_body_p)
24043 check_constexpr_ctor_body (last, list, /*complain=*/true);
24044 /* Finish the function body. */
24045 finish_function_body (body);
24046 }
24047
24048 /* Parse an initializer.
24049
24050 initializer:
24051 = initializer-clause
24052 ( expression-list )
24053
24054 Returns an expression representing the initializer. If no
24055 initializer is present, NULL_TREE is returned.
24056
24057 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
24058 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
24059 set to TRUE if there is no initializer present. If there is an
24060 initializer, and it is not a constant-expression, *NON_CONSTANT_P
24061 is set to true; otherwise it is set to false. */
24062
24063 static tree
24064 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
24065 bool* non_constant_p, bool subexpression_p)
24066 {
24067 cp_token *token;
24068 tree init;
24069
24070 /* Peek at the next token. */
24071 token = cp_lexer_peek_token (parser->lexer);
24072
24073 /* Let our caller know whether or not this initializer was
24074 parenthesized. */
24075 *is_direct_init = (token->type != CPP_EQ);
24076 /* Assume that the initializer is constant. */
24077 *non_constant_p = false;
24078
24079 if (token->type == CPP_EQ)
24080 {
24081 /* Consume the `='. */
24082 cp_lexer_consume_token (parser->lexer);
24083 /* Parse the initializer-clause. */
24084 init = cp_parser_initializer_clause (parser, non_constant_p);
24085 }
24086 else if (token->type == CPP_OPEN_PAREN)
24087 {
24088 vec<tree, va_gc> *vec;
24089 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
24090 /*cast_p=*/false,
24091 /*allow_expansion_p=*/true,
24092 non_constant_p);
24093 if (vec == NULL)
24094 return error_mark_node;
24095 init = build_tree_list_vec (vec);
24096 release_tree_vector (vec);
24097 }
24098 else if (token->type == CPP_OPEN_BRACE)
24099 {
24100 cp_lexer_set_source_position (parser->lexer);
24101 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24102 init = cp_parser_braced_list (parser, non_constant_p);
24103 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
24104 }
24105 else
24106 {
24107 /* Anything else is an error. */
24108 cp_parser_error (parser, "expected initializer");
24109 init = error_mark_node;
24110 }
24111
24112 if (!subexpression_p && check_for_bare_parameter_packs (init))
24113 init = error_mark_node;
24114
24115 return init;
24116 }
24117
24118 /* Parse an initializer-clause.
24119
24120 initializer-clause:
24121 assignment-expression
24122 braced-init-list
24123
24124 Returns an expression representing the initializer.
24125
24126 If the `assignment-expression' production is used the value
24127 returned is simply a representation for the expression.
24128
24129 Otherwise, calls cp_parser_braced_list. */
24130
24131 static cp_expr
24132 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
24133 {
24134 cp_expr initializer;
24135
24136 /* Assume the expression is constant. */
24137 *non_constant_p = false;
24138
24139 /* If it is not a `{', then we are looking at an
24140 assignment-expression. */
24141 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
24142 {
24143 initializer
24144 = cp_parser_constant_expression (parser,
24145 /*allow_non_constant_p=*/true,
24146 non_constant_p);
24147 }
24148 else
24149 initializer = cp_parser_braced_list (parser, non_constant_p);
24150
24151 return initializer;
24152 }
24153
24154 /* Parse a brace-enclosed initializer list.
24155
24156 braced-init-list:
24157 { initializer-list , [opt] }
24158 { designated-initializer-list , [opt] }
24159 { }
24160
24161 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
24162 the elements of the initializer-list (or NULL, if the last
24163 production is used). The TREE_TYPE for the CONSTRUCTOR will be
24164 NULL_TREE. There is no way to detect whether or not the optional
24165 trailing `,' was provided. NON_CONSTANT_P is as for
24166 cp_parser_initializer. */
24167
24168 static cp_expr
24169 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
24170 {
24171 tree initializer;
24172 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
24173
24174 /* Consume the `{' token. */
24175 matching_braces braces;
24176 braces.require_open (parser);
24177 /* Create a CONSTRUCTOR to represent the braced-initializer. */
24178 initializer = make_node (CONSTRUCTOR);
24179 /* If it's not a `}', then there is a non-trivial initializer. */
24180 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
24181 {
24182 bool designated;
24183 /* Parse the initializer list. */
24184 CONSTRUCTOR_ELTS (initializer)
24185 = cp_parser_initializer_list (parser, non_constant_p, &designated);
24186 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
24187 /* A trailing `,' token is allowed. */
24188 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24189 cp_lexer_consume_token (parser->lexer);
24190 }
24191 else
24192 *non_constant_p = false;
24193 /* Now, there should be a trailing `}'. */
24194 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
24195 braces.require_close (parser);
24196 TREE_TYPE (initializer) = init_list_type_node;
24197
24198 cp_expr result (initializer);
24199 /* Build a location of the form:
24200 { ... }
24201 ^~~~~~~
24202 with caret==start at the open brace, finish at the close brace. */
24203 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
24204 result.set_location (combined_loc);
24205 return result;
24206 }
24207
24208 /* Consume tokens up to, and including, the next non-nested closing `]'.
24209 Returns true iff we found a closing `]'. */
24210
24211 static bool
24212 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
24213 {
24214 unsigned square_depth = 0;
24215
24216 while (true)
24217 {
24218 cp_token * token = cp_lexer_peek_token (parser->lexer);
24219
24220 switch (token->type)
24221 {
24222 case CPP_PRAGMA_EOL:
24223 if (!parser->lexer->in_pragma)
24224 break;
24225 /* FALLTHRU */
24226 case CPP_EOF:
24227 /* If we've run out of tokens, then there is no closing `]'. */
24228 return false;
24229
24230 case CPP_OPEN_SQUARE:
24231 ++square_depth;
24232 break;
24233
24234 case CPP_CLOSE_SQUARE:
24235 if (!square_depth--)
24236 {
24237 cp_lexer_consume_token (parser->lexer);
24238 return true;
24239 }
24240 break;
24241
24242 default:
24243 break;
24244 }
24245
24246 /* Consume the token. */
24247 cp_lexer_consume_token (parser->lexer);
24248 }
24249 }
24250
24251 /* Return true if we are looking at an array-designator, false otherwise. */
24252
24253 static bool
24254 cp_parser_array_designator_p (cp_parser *parser)
24255 {
24256 /* Consume the `['. */
24257 cp_lexer_consume_token (parser->lexer);
24258
24259 cp_lexer_save_tokens (parser->lexer);
24260
24261 /* Skip tokens until the next token is a closing square bracket.
24262 If we find the closing `]', and the next token is a `=', then
24263 we are looking at an array designator. */
24264 bool array_designator_p
24265 = (cp_parser_skip_to_closing_square_bracket (parser)
24266 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
24267
24268 /* Roll back the tokens we skipped. */
24269 cp_lexer_rollback_tokens (parser->lexer);
24270
24271 return array_designator_p;
24272 }
24273
24274 /* Parse an initializer-list.
24275
24276 initializer-list:
24277 initializer-clause ... [opt]
24278 initializer-list , initializer-clause ... [opt]
24279
24280 C++20 Extension:
24281
24282 designated-initializer-list:
24283 designated-initializer-clause
24284 designated-initializer-list , designated-initializer-clause
24285
24286 designated-initializer-clause:
24287 designator brace-or-equal-initializer
24288
24289 designator:
24290 . identifier
24291
24292 GNU Extension:
24293
24294 initializer-list:
24295 designation initializer-clause ...[opt]
24296 initializer-list , designation initializer-clause ...[opt]
24297
24298 designation:
24299 . identifier =
24300 identifier :
24301 [ constant-expression ] =
24302
24303 Returns a vec of constructor_elt. The VALUE of each elt is an expression
24304 for the initializer. If the INDEX of the elt is non-NULL, it is the
24305 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
24306 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
24307 are any designators. */
24308
24309 static vec<constructor_elt, va_gc> *
24310 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
24311 bool *designated)
24312 {
24313 vec<constructor_elt, va_gc> *v = NULL;
24314 bool first_p = true;
24315 tree first_designator = NULL_TREE;
24316
24317 /* Assume all of the expressions are constant. */
24318 *non_constant_p = false;
24319
24320 unsigned nelts = 0;
24321 int suppress = suppress_location_wrappers;
24322
24323 /* Parse the rest of the list. */
24324 while (true)
24325 {
24326 cp_token *token;
24327 tree designator;
24328 tree initializer;
24329 bool clause_non_constant_p;
24330 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24331
24332 /* Handle the C++20 syntax, '. id ='. */
24333 if ((cxx_dialect >= cxx20
24334 || cp_parser_allow_gnu_extensions_p (parser))
24335 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
24336 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
24337 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
24338 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
24339 == CPP_OPEN_BRACE)))
24340 {
24341 if (cxx_dialect < cxx20)
24342 pedwarn (loc, OPT_Wpedantic,
24343 "C++ designated initializers only available with "
24344 "%<-std=c++20%> or %<-std=gnu++20%>");
24345 /* Consume the `.'. */
24346 cp_lexer_consume_token (parser->lexer);
24347 /* Consume the identifier. */
24348 designator = cp_lexer_consume_token (parser->lexer)->u.value;
24349 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24350 /* Consume the `='. */
24351 cp_lexer_consume_token (parser->lexer);
24352 }
24353 /* Also, if the next token is an identifier and the following one is a
24354 colon, we are looking at the GNU designated-initializer
24355 syntax. */
24356 else if (cp_parser_allow_gnu_extensions_p (parser)
24357 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
24358 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24359 == CPP_COLON))
24360 {
24361 /* Warn the user that they are using an extension. */
24362 pedwarn (loc, OPT_Wpedantic,
24363 "ISO C++ does not allow GNU designated initializers");
24364 /* Consume the identifier. */
24365 designator = cp_lexer_consume_token (parser->lexer)->u.value;
24366 /* Consume the `:'. */
24367 cp_lexer_consume_token (parser->lexer);
24368 }
24369 /* Also handle C99 array designators, '[ const ] ='. */
24370 else if (cp_parser_allow_gnu_extensions_p (parser)
24371 && !c_dialect_objc ()
24372 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24373 {
24374 /* In C++11, [ could start a lambda-introducer. */
24375 bool non_const = false;
24376
24377 cp_parser_parse_tentatively (parser);
24378
24379 if (!cp_parser_array_designator_p (parser))
24380 {
24381 cp_parser_simulate_error (parser);
24382 designator = NULL_TREE;
24383 }
24384 else
24385 {
24386 designator = cp_parser_constant_expression (parser, true,
24387 &non_const);
24388 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24389 cp_parser_require (parser, CPP_EQ, RT_EQ);
24390 }
24391
24392 if (!cp_parser_parse_definitely (parser))
24393 designator = NULL_TREE;
24394 else if (non_const
24395 && (!require_potential_rvalue_constant_expression
24396 (designator)))
24397 designator = NULL_TREE;
24398 if (designator)
24399 /* Warn the user that they are using an extension. */
24400 pedwarn (loc, OPT_Wpedantic,
24401 "ISO C++ does not allow C99 designated initializers");
24402 }
24403 else
24404 designator = NULL_TREE;
24405
24406 if (first_p)
24407 {
24408 first_designator = designator;
24409 first_p = false;
24410 }
24411 else if (cxx_dialect >= cxx20
24412 && first_designator != error_mark_node
24413 && (!first_designator != !designator))
24414 {
24415 error_at (loc, "either all initializer clauses should be designated "
24416 "or none of them should be");
24417 first_designator = error_mark_node;
24418 }
24419 else if (cxx_dialect < cxx20 && !first_designator)
24420 first_designator = designator;
24421
24422 /* Parse the initializer. */
24423 initializer = cp_parser_initializer_clause (parser,
24424 &clause_non_constant_p);
24425 /* If any clause is non-constant, so is the entire initializer. */
24426 if (clause_non_constant_p)
24427 *non_constant_p = true;
24428
24429 /* If we have an ellipsis, this is an initializer pack
24430 expansion. */
24431 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24432 {
24433 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24434
24435 /* Consume the `...'. */
24436 cp_lexer_consume_token (parser->lexer);
24437
24438 if (designator && cxx_dialect >= cxx20)
24439 error_at (loc,
24440 "%<...%> not allowed in designated initializer list");
24441
24442 /* Turn the initializer into an initializer expansion. */
24443 initializer = make_pack_expansion (initializer);
24444 }
24445
24446 /* Add it to the vector. */
24447 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
24448
24449 /* If the next token is not a comma, we have reached the end of
24450 the list. */
24451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24452 break;
24453
24454 /* Peek at the next token. */
24455 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24456 /* If the next token is a `}', then we're still done. An
24457 initializer-clause can have a trailing `,' after the
24458 initializer-list and before the closing `}'. */
24459 if (token->type == CPP_CLOSE_BRACE)
24460 break;
24461
24462 /* Suppress location wrappers in a long initializer to save memory
24463 (14179). The cutoff is chosen arbitrarily. */
24464 const unsigned loc_max = 256;
24465 unsigned incr = 1;
24466 if (TREE_CODE (initializer) == CONSTRUCTOR)
24467 /* Look one level down because it's easy. Looking deeper would require
24468 passing down a nelts pointer, and I don't think multi-level massive
24469 initializers are common enough to justify this. */
24470 incr = CONSTRUCTOR_NELTS (initializer);
24471 nelts += incr;
24472 if (nelts >= loc_max && (nelts - incr) < loc_max)
24473 ++suppress_location_wrappers;
24474
24475 /* Consume the `,' token. */
24476 cp_lexer_consume_token (parser->lexer);
24477 }
24478
24479 /* The same identifier shall not appear in multiple designators
24480 of a designated-initializer-list. */
24481 if (first_designator)
24482 {
24483 unsigned int i;
24484 tree designator, val;
24485 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
24486 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
24487 {
24488 if (IDENTIFIER_MARKED (designator))
24489 {
24490 error_at (cp_expr_loc_or_input_loc (val),
24491 "%<.%s%> designator used multiple times in "
24492 "the same initializer list",
24493 IDENTIFIER_POINTER (designator));
24494 (*v)[i].index = error_mark_node;
24495 }
24496 else
24497 IDENTIFIER_MARKED (designator) = 1;
24498 }
24499 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
24500 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
24501 IDENTIFIER_MARKED (designator) = 0;
24502 }
24503
24504 suppress_location_wrappers = suppress;
24505
24506 *designated = first_designator != NULL_TREE;
24507 return v;
24508 }
24509
24510 /* Classes [gram.class] */
24511
24512 /* Parse a class-name.
24513
24514 class-name:
24515 identifier
24516 template-id
24517
24518 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
24519 to indicate that names looked up in dependent types should be
24520 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
24521 keyword has been used to indicate that the name that appears next
24522 is a template. TAG_TYPE indicates the explicit tag given before
24523 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
24524 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
24525 is the class being defined in a class-head. If ENUM_OK is TRUE,
24526 enum-names are also accepted.
24527
24528 Returns the TYPE_DECL representing the class. */
24529
24530 static tree
24531 cp_parser_class_name (cp_parser *parser,
24532 bool typename_keyword_p,
24533 bool template_keyword_p,
24534 enum tag_types tag_type,
24535 bool check_dependency_p,
24536 bool class_head_p,
24537 bool is_declaration,
24538 bool enum_ok)
24539 {
24540 tree decl;
24541 tree identifier = NULL_TREE;
24542
24543 /* All class-names start with an identifier. */
24544 cp_token *token = cp_lexer_peek_token (parser->lexer);
24545 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
24546 {
24547 cp_parser_error (parser, "expected class-name");
24548 return error_mark_node;
24549 }
24550
24551 /* PARSER->SCOPE can be cleared when parsing the template-arguments
24552 to a template-id, so we save it here. Consider object scope too,
24553 so that make_typename_type below can use it (cp_parser_template_name
24554 considers object scope also). This may happen with code like
24555
24556 p->template A<T>::a()
24557
24558 where we first want to look up A<T>::a in the class of the object
24559 expression, as per [basic.lookup.classref]. */
24560 tree scope = parser->scope ? parser->scope : parser->context->object_type;
24561 if (scope == error_mark_node)
24562 return error_mark_node;
24563
24564 /* Any name names a type if we're following the `typename' keyword
24565 in a qualified name where the enclosing scope is type-dependent. */
24566 const bool typename_p = (typename_keyword_p
24567 && parser->scope
24568 && TYPE_P (parser->scope)
24569 && dependent_type_p (parser->scope));
24570 /* Handle the common case (an identifier, but not a template-id)
24571 efficiently. */
24572 if (token->type == CPP_NAME
24573 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
24574 {
24575 cp_token *identifier_token;
24576 bool ambiguous_p;
24577
24578 /* Look for the identifier. */
24579 identifier_token = cp_lexer_peek_token (parser->lexer);
24580 ambiguous_p = identifier_token->error_reported;
24581 identifier = cp_parser_identifier (parser);
24582 /* If the next token isn't an identifier, we are certainly not
24583 looking at a class-name. */
24584 if (identifier == error_mark_node)
24585 decl = error_mark_node;
24586 /* If we know this is a type-name, there's no need to look it
24587 up. */
24588 else if (typename_p)
24589 decl = identifier;
24590 else
24591 {
24592 tree ambiguous_decls;
24593 /* If we already know that this lookup is ambiguous, then
24594 we've already issued an error message; there's no reason
24595 to check again. */
24596 if (ambiguous_p)
24597 {
24598 cp_parser_simulate_error (parser);
24599 return error_mark_node;
24600 }
24601 /* If the next token is a `::', then the name must be a type
24602 name.
24603
24604 [basic.lookup.qual]
24605
24606 During the lookup for a name preceding the :: scope
24607 resolution operator, object, function, and enumerator
24608 names are ignored. */
24609 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
24610 tag_type = scope_type;
24611 /* Look up the name. */
24612 decl = cp_parser_lookup_name (parser, identifier,
24613 tag_type,
24614 /*is_template=*/false,
24615 /*is_namespace=*/false,
24616 check_dependency_p,
24617 &ambiguous_decls,
24618 identifier_token->location);
24619 if (ambiguous_decls)
24620 {
24621 if (cp_parser_parsing_tentatively (parser))
24622 cp_parser_simulate_error (parser);
24623 return error_mark_node;
24624 }
24625 }
24626 }
24627 else
24628 {
24629 /* Try a template-id. */
24630 decl = cp_parser_template_id (parser, template_keyword_p,
24631 check_dependency_p,
24632 tag_type,
24633 is_declaration);
24634 if (decl == error_mark_node)
24635 return error_mark_node;
24636 }
24637
24638 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
24639
24640 /* If this is a typename, create a TYPENAME_TYPE. */
24641 if (typename_p
24642 && decl != error_mark_node
24643 && !is_overloaded_fn (decl))
24644 {
24645 decl = make_typename_type (scope, decl, typename_type,
24646 /*complain=*/tf_error);
24647 if (decl != error_mark_node)
24648 decl = TYPE_NAME (decl);
24649 }
24650
24651 decl = strip_using_decl (decl);
24652
24653 /* Check to see that it is really the name of a class. */
24654 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
24655 && identifier_p (TREE_OPERAND (decl, 0))
24656 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
24657 /* Situations like this:
24658
24659 template <typename T> struct A {
24660 typename T::template X<int>::I i;
24661 };
24662
24663 are problematic. Is `T::template X<int>' a class-name? The
24664 standard does not seem to be definitive, but there is no other
24665 valid interpretation of the following `::'. Therefore, those
24666 names are considered class-names. */
24667 {
24668 decl = make_typename_type (scope, decl, tag_type, tf_error);
24669 if (decl != error_mark_node)
24670 decl = TYPE_NAME (decl);
24671 }
24672 else if (TREE_CODE (decl) != TYPE_DECL
24673 || TREE_TYPE (decl) == error_mark_node
24674 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
24675 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
24676 /* In Objective-C 2.0, a classname followed by '.' starts a
24677 dot-syntax expression, and it's not a type-name. */
24678 || (c_dialect_objc ()
24679 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
24680 && objc_is_class_name (decl)))
24681 decl = error_mark_node;
24682
24683 if (decl == error_mark_node)
24684 cp_parser_error (parser, "expected class-name");
24685 else if (identifier && !parser->scope)
24686 maybe_note_name_used_in_class (identifier, decl);
24687
24688 return decl;
24689 }
24690
24691 /* Make sure that any member-function parameters are in scope.
24692 For instance, a function's noexcept-specifier can use the function's
24693 parameters:
24694
24695 struct S {
24696 void fn (int p) noexcept(noexcept(p));
24697 };
24698
24699 so we need to make sure name lookup can find them. This is used
24700 when we delay parsing of the noexcept-specifier. */
24701
24702 static void
24703 inject_parm_decls (tree decl)
24704 {
24705 begin_scope (sk_function_parms, decl);
24706 tree args = DECL_ARGUMENTS (decl);
24707
24708 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
24709
24710 if (args && is_this_parameter (args))
24711 {
24712 gcc_checking_assert (current_class_ptr == NULL_TREE);
24713 current_class_ptr = NULL_TREE;
24714 current_class_ref = cp_build_fold_indirect_ref (args);
24715 current_class_ptr = args;
24716 }
24717 }
24718
24719 /* Undo the effects of inject_parm_decls. */
24720
24721 static void
24722 pop_injected_parms (void)
24723 {
24724 pop_bindings_and_leave_scope ();
24725 current_class_ptr = current_class_ref = NULL_TREE;
24726 }
24727
24728 /* Parse a class-specifier.
24729
24730 class-specifier:
24731 class-head { member-specification [opt] }
24732
24733 Returns the TREE_TYPE representing the class. */
24734
24735 static tree
24736 cp_parser_class_specifier_1 (cp_parser* parser)
24737 {
24738 tree type;
24739 tree attributes = NULL_TREE;
24740 bool nested_name_specifier_p;
24741 unsigned saved_num_template_parameter_lists;
24742 bool saved_in_function_body;
24743 unsigned char in_statement;
24744 bool in_switch_statement_p;
24745 bool saved_in_unbraced_linkage_specification_p;
24746 tree old_scope = NULL_TREE;
24747 tree scope = NULL_TREE;
24748 cp_token *closing_brace;
24749
24750 push_deferring_access_checks (dk_no_deferred);
24751
24752 /* Parse the class-head. */
24753 type = cp_parser_class_head (parser,
24754 &nested_name_specifier_p);
24755 /* If the class-head was a semantic disaster, skip the entire body
24756 of the class. */
24757 if (!type)
24758 {
24759 cp_parser_skip_to_end_of_block_or_statement (parser);
24760 pop_deferring_access_checks ();
24761 return error_mark_node;
24762 }
24763
24764 /* Look for the `{'. */
24765 matching_braces braces;
24766 if (!braces.require_open (parser))
24767 {
24768 pop_deferring_access_checks ();
24769 return error_mark_node;
24770 }
24771
24772 cp_ensure_no_omp_declare_simd (parser);
24773 cp_ensure_no_oacc_routine (parser);
24774
24775 /* Issue an error message if type-definitions are forbidden here. */
24776 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
24777 /* Remember that we are defining one more class. */
24778 ++parser->num_classes_being_defined;
24779 /* Inside the class, surrounding template-parameter-lists do not
24780 apply. */
24781 saved_num_template_parameter_lists
24782 = parser->num_template_parameter_lists;
24783 parser->num_template_parameter_lists = 0;
24784 /* We are not in a function body. */
24785 saved_in_function_body = parser->in_function_body;
24786 parser->in_function_body = false;
24787 /* Or in a loop. */
24788 in_statement = parser->in_statement;
24789 parser->in_statement = 0;
24790 /* Or in a switch. */
24791 in_switch_statement_p = parser->in_switch_statement_p;
24792 parser->in_switch_statement_p = false;
24793 /* We are not immediately inside an extern "lang" block. */
24794 saved_in_unbraced_linkage_specification_p
24795 = parser->in_unbraced_linkage_specification_p;
24796 parser->in_unbraced_linkage_specification_p = false;
24797
24798 /* Start the class. */
24799 if (nested_name_specifier_p)
24800 {
24801 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
24802 /* SCOPE must be a scope nested inside current scope. */
24803 if (is_nested_namespace (current_namespace,
24804 decl_namespace_context (scope)))
24805 old_scope = push_inner_scope (scope);
24806 else
24807 nested_name_specifier_p = false;
24808 }
24809 type = begin_class_definition (type);
24810
24811 if (type == error_mark_node)
24812 /* If the type is erroneous, skip the entire body of the class. */
24813 cp_parser_skip_to_closing_brace (parser);
24814 else
24815 /* Parse the member-specification. */
24816 cp_parser_member_specification_opt (parser);
24817
24818 /* Look for the trailing `}'. */
24819 closing_brace = braces.require_close (parser);
24820 /* Look for trailing attributes to apply to this class. */
24821 if (cp_parser_allow_gnu_extensions_p (parser))
24822 attributes = cp_parser_gnu_attributes_opt (parser);
24823 if (type != error_mark_node)
24824 type = finish_struct (type, attributes);
24825 if (nested_name_specifier_p)
24826 pop_inner_scope (old_scope, scope);
24827
24828 /* We've finished a type definition. Check for the common syntax
24829 error of forgetting a semicolon after the definition. We need to
24830 be careful, as we can't just check for not-a-semicolon and be done
24831 with it; the user might have typed:
24832
24833 class X { } c = ...;
24834 class X { } *p = ...;
24835
24836 and so forth. Instead, enumerate all the possible tokens that
24837 might follow this production; if we don't see one of them, then
24838 complain and silently insert the semicolon. */
24839 {
24840 cp_token *token = cp_lexer_peek_token (parser->lexer);
24841 bool want_semicolon = true;
24842
24843 if (cp_next_tokens_can_be_std_attribute_p (parser))
24844 /* Don't try to parse c++11 attributes here. As per the
24845 grammar, that should be a task for
24846 cp_parser_decl_specifier_seq. */
24847 want_semicolon = false;
24848
24849 switch (token->type)
24850 {
24851 case CPP_NAME:
24852 case CPP_SEMICOLON:
24853 case CPP_MULT:
24854 case CPP_AND:
24855 case CPP_OPEN_PAREN:
24856 case CPP_CLOSE_PAREN:
24857 case CPP_COMMA:
24858 want_semicolon = false;
24859 break;
24860
24861 /* While it's legal for type qualifiers and storage class
24862 specifiers to follow type definitions in the grammar, only
24863 compiler testsuites contain code like that. Assume that if
24864 we see such code, then what we're really seeing is a case
24865 like:
24866
24867 class X { }
24868 const <type> var = ...;
24869
24870 or
24871
24872 class Y { }
24873 static <type> func (...) ...
24874
24875 i.e. the qualifier or specifier applies to the next
24876 declaration. To do so, however, we need to look ahead one
24877 more token to see if *that* token is a type specifier.
24878
24879 This code could be improved to handle:
24880
24881 class Z { }
24882 static const <type> var = ...; */
24883 case CPP_KEYWORD:
24884 if (keyword_is_decl_specifier (token->keyword))
24885 {
24886 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
24887
24888 /* Handling user-defined types here would be nice, but very
24889 tricky. */
24890 want_semicolon
24891 = (lookahead->type == CPP_KEYWORD
24892 && keyword_begins_type_specifier (lookahead->keyword));
24893 }
24894 break;
24895 default:
24896 break;
24897 }
24898
24899 /* If we don't have a type, then something is very wrong and we
24900 shouldn't try to do anything clever. Likewise for not seeing the
24901 closing brace. */
24902 if (closing_brace && TYPE_P (type) && want_semicolon)
24903 {
24904 /* Locate the closing brace. */
24905 cp_token_position prev
24906 = cp_lexer_previous_token_position (parser->lexer);
24907 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
24908 location_t loc = prev_token->location;
24909
24910 /* We want to suggest insertion of a ';' immediately *after* the
24911 closing brace, so, if we can, offset the location by 1 column. */
24912 location_t next_loc = loc;
24913 if (!linemap_location_from_macro_expansion_p (line_table, loc))
24914 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
24915
24916 rich_location richloc (line_table, next_loc);
24917
24918 /* If we successfully offset the location, suggest the fix-it. */
24919 if (next_loc != loc)
24920 richloc.add_fixit_insert_before (next_loc, ";");
24921
24922 if (CLASSTYPE_DECLARED_CLASS (type))
24923 error_at (&richloc,
24924 "expected %<;%> after class definition");
24925 else if (TREE_CODE (type) == RECORD_TYPE)
24926 error_at (&richloc,
24927 "expected %<;%> after struct definition");
24928 else if (TREE_CODE (type) == UNION_TYPE)
24929 error_at (&richloc,
24930 "expected %<;%> after union definition");
24931 else
24932 gcc_unreachable ();
24933
24934 /* Unget one token and smash it to look as though we encountered
24935 a semicolon in the input stream. */
24936 cp_lexer_set_token_position (parser->lexer, prev);
24937 token = cp_lexer_peek_token (parser->lexer);
24938 token->type = CPP_SEMICOLON;
24939 token->keyword = RID_MAX;
24940 }
24941 }
24942
24943 /* If this class is not itself within the scope of another class,
24944 then we need to parse the bodies of all of the queued function
24945 definitions. Note that the queued functions defined in a class
24946 are not always processed immediately following the
24947 class-specifier for that class. Consider:
24948
24949 struct A {
24950 struct B { void f() { sizeof (A); } };
24951 };
24952
24953 If `f' were processed before the processing of `A' were
24954 completed, there would be no way to compute the size of `A'.
24955 Note that the nesting we are interested in here is lexical --
24956 not the semantic nesting given by TYPE_CONTEXT. In particular,
24957 for:
24958
24959 struct A { struct B; };
24960 struct A::B { void f() { } };
24961
24962 there is no need to delay the parsing of `A::B::f'. */
24963 if (--parser->num_classes_being_defined == 0)
24964 {
24965 tree decl;
24966 tree class_type = NULL_TREE;
24967 tree pushed_scope = NULL_TREE;
24968 unsigned ix;
24969 cp_default_arg_entry *e;
24970 tree save_ccp, save_ccr;
24971
24972 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
24973 {
24974 /* Skip default arguments, NSDMIs, etc, in order to improve
24975 error recovery (c++/71169, c++/71832). */
24976 vec_safe_truncate (unparsed_funs_with_default_args, 0);
24977 vec_safe_truncate (unparsed_nsdmis, 0);
24978 vec_safe_truncate (unparsed_funs_with_definitions, 0);
24979 }
24980
24981 /* In a first pass, parse default arguments to the functions.
24982 Then, in a second pass, parse the bodies of the functions.
24983 This two-phased approach handles cases like:
24984
24985 struct S {
24986 void f() { g(); }
24987 void g(int i = 3);
24988 };
24989
24990 */
24991 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
24992 {
24993 decl = e->decl;
24994 /* If there are default arguments that have not yet been processed,
24995 take care of them now. */
24996 if (class_type != e->class_type)
24997 {
24998 if (pushed_scope)
24999 pop_scope (pushed_scope);
25000 class_type = e->class_type;
25001 pushed_scope = push_scope (class_type);
25002 }
25003 /* Make sure that any template parameters are in scope. */
25004 maybe_begin_member_template_processing (decl);
25005 /* Parse the default argument expressions. */
25006 cp_parser_late_parsing_default_args (parser, decl);
25007 /* Remove any template parameters from the symbol table. */
25008 maybe_end_member_template_processing ();
25009 }
25010 vec_safe_truncate (unparsed_funs_with_default_args, 0);
25011
25012 /* If there are noexcept-specifiers that have not yet been processed,
25013 take care of them now. Do this before processing NSDMIs as they
25014 may depend on noexcept-specifiers already having been processed. */
25015 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
25016 {
25017 tree ctx = DECL_CONTEXT (decl);
25018 if (class_type != ctx)
25019 {
25020 if (pushed_scope)
25021 pop_scope (pushed_scope);
25022 class_type = ctx;
25023 pushed_scope = push_scope (class_type);
25024 }
25025
25026 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
25027 spec = TREE_PURPOSE (spec);
25028
25029 /* Make sure that any template parameters are in scope. */
25030 maybe_begin_member_template_processing (decl);
25031
25032 /* Make sure that any member-function parameters are in scope. */
25033 inject_parm_decls (decl);
25034
25035 /* 'this' is not allowed in static member functions. */
25036 unsigned char local_variables_forbidden_p
25037 = parser->local_variables_forbidden_p;
25038 if (DECL_THIS_STATIC (decl))
25039 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
25040
25041 /* Now we can parse the noexcept-specifier. */
25042 spec = cp_parser_late_noexcept_specifier (parser, spec);
25043
25044 if (spec == error_mark_node)
25045 spec = NULL_TREE;
25046
25047 /* Update the fn's type directly -- it might have escaped
25048 beyond this decl :( */
25049 fixup_deferred_exception_variants (TREE_TYPE (decl), spec);
25050
25051 /* Restore the state of local_variables_forbidden_p. */
25052 parser->local_variables_forbidden_p = local_variables_forbidden_p;
25053
25054 /* The finish_struct call above performed various override checking,
25055 but it skipped unparsed noexcept-specifier operands. Now that we
25056 have resolved them, check again. */
25057 noexcept_override_late_checks (type, decl);
25058
25059 /* Remove any member-function parameters from the symbol table. */
25060 pop_injected_parms ();
25061
25062 /* Remove any template parameters from the symbol table. */
25063 maybe_end_member_template_processing ();
25064 }
25065 vec_safe_truncate (unparsed_noexcepts, 0);
25066
25067 /* Now parse any NSDMIs. */
25068 save_ccp = current_class_ptr;
25069 save_ccr = current_class_ref;
25070 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
25071 {
25072 if (class_type != DECL_CONTEXT (decl))
25073 {
25074 if (pushed_scope)
25075 pop_scope (pushed_scope);
25076 class_type = DECL_CONTEXT (decl);
25077 pushed_scope = push_scope (class_type);
25078 }
25079 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
25080 cp_parser_late_parsing_nsdmi (parser, decl);
25081 }
25082 vec_safe_truncate (unparsed_nsdmis, 0);
25083 current_class_ptr = save_ccp;
25084 current_class_ref = save_ccr;
25085 if (pushed_scope)
25086 pop_scope (pushed_scope);
25087
25088 /* Now parse the body of the functions. */
25089 if (flag_openmp)
25090 {
25091 /* OpenMP UDRs need to be parsed before all other functions. */
25092 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
25093 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
25094 cp_parser_late_parsing_for_member (parser, decl);
25095 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
25096 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
25097 cp_parser_late_parsing_for_member (parser, decl);
25098 }
25099 else
25100 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
25101 cp_parser_late_parsing_for_member (parser, decl);
25102 vec_safe_truncate (unparsed_funs_with_definitions, 0);
25103 }
25104
25105 /* Put back any saved access checks. */
25106 pop_deferring_access_checks ();
25107
25108 /* Restore saved state. */
25109 parser->in_switch_statement_p = in_switch_statement_p;
25110 parser->in_statement = in_statement;
25111 parser->in_function_body = saved_in_function_body;
25112 parser->num_template_parameter_lists
25113 = saved_num_template_parameter_lists;
25114 parser->in_unbraced_linkage_specification_p
25115 = saved_in_unbraced_linkage_specification_p;
25116
25117 return type;
25118 }
25119
25120 static tree
25121 cp_parser_class_specifier (cp_parser* parser)
25122 {
25123 tree ret;
25124 timevar_push (TV_PARSE_STRUCT);
25125 ret = cp_parser_class_specifier_1 (parser);
25126 timevar_pop (TV_PARSE_STRUCT);
25127 return ret;
25128 }
25129
25130 /* Parse a class-head.
25131
25132 class-head:
25133 class-key identifier [opt] base-clause [opt]
25134 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
25135 class-key nested-name-specifier [opt] template-id
25136 base-clause [opt]
25137
25138 class-virt-specifier:
25139 final
25140
25141 GNU Extensions:
25142 class-key attributes identifier [opt] base-clause [opt]
25143 class-key attributes nested-name-specifier identifier base-clause [opt]
25144 class-key attributes nested-name-specifier [opt] template-id
25145 base-clause [opt]
25146
25147 Upon return BASES is initialized to the list of base classes (or
25148 NULL, if there are none) in the same form returned by
25149 cp_parser_base_clause.
25150
25151 Returns the TYPE of the indicated class. Sets
25152 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
25153 involving a nested-name-specifier was used, and FALSE otherwise.
25154
25155 Returns error_mark_node if this is not a class-head.
25156
25157 Returns NULL_TREE if the class-head is syntactically valid, but
25158 semantically invalid in a way that means we should skip the entire
25159 body of the class. */
25160
25161 static tree
25162 cp_parser_class_head (cp_parser* parser,
25163 bool* nested_name_specifier_p)
25164 {
25165 tree nested_name_specifier;
25166 enum tag_types class_key;
25167 tree id = NULL_TREE;
25168 tree type = NULL_TREE;
25169 tree attributes;
25170 tree bases;
25171 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
25172 bool template_id_p = false;
25173 bool qualified_p = false;
25174 bool invalid_nested_name_p = false;
25175 bool invalid_explicit_specialization_p = false;
25176 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
25177 tree pushed_scope = NULL_TREE;
25178 unsigned num_templates;
25179 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
25180 /* Assume no nested-name-specifier will be present. */
25181 *nested_name_specifier_p = false;
25182 /* Assume no template parameter lists will be used in defining the
25183 type. */
25184 num_templates = 0;
25185 parser->colon_corrects_to_scope_p = false;
25186
25187 /* Look for the class-key. */
25188 class_key = cp_parser_class_key (parser);
25189 if (class_key == none_type)
25190 return error_mark_node;
25191
25192 location_t class_head_start_location = input_location;
25193
25194 /* Parse the attributes. */
25195 attributes = cp_parser_attributes_opt (parser);
25196
25197 /* If the next token is `::', that is invalid -- but sometimes
25198 people do try to write:
25199
25200 struct ::S {};
25201
25202 Handle this gracefully by accepting the extra qualifier, and then
25203 issuing an error about it later if this really is a
25204 class-head. If it turns out just to be an elaborated type
25205 specifier, remain silent. */
25206 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
25207 qualified_p = true;
25208
25209 push_deferring_access_checks (dk_no_check);
25210
25211 /* Determine the name of the class. Begin by looking for an
25212 optional nested-name-specifier. */
25213 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
25214 nested_name_specifier
25215 = cp_parser_nested_name_specifier_opt (parser,
25216 /*typename_keyword_p=*/false,
25217 /*check_dependency_p=*/false,
25218 /*type_p=*/true,
25219 /*is_declaration=*/false);
25220 /* If there was a nested-name-specifier, then there *must* be an
25221 identifier. */
25222
25223 cp_token *bad_template_keyword = NULL;
25224
25225 if (nested_name_specifier)
25226 {
25227 type_start_token = cp_lexer_peek_token (parser->lexer);
25228 /* Although the grammar says `identifier', it really means
25229 `class-name' or `template-name'. You are only allowed to
25230 define a class that has already been declared with this
25231 syntax.
25232
25233 The proposed resolution for Core Issue 180 says that wherever
25234 you see `class T::X' you should treat `X' as a type-name.
25235
25236 It is OK to define an inaccessible class; for example:
25237
25238 class A { class B; };
25239 class A::B {};
25240
25241 We do not know if we will see a class-name, or a
25242 template-name. We look for a class-name first, in case the
25243 class-name is a template-id; if we looked for the
25244 template-name first we would stop after the template-name. */
25245 cp_parser_parse_tentatively (parser);
25246 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25247 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
25248 type = cp_parser_class_name (parser,
25249 /*typename_keyword_p=*/false,
25250 /*template_keyword_p=*/false,
25251 class_type,
25252 /*check_dependency_p=*/false,
25253 /*class_head_p=*/true,
25254 /*is_declaration=*/false);
25255 /* If that didn't work, ignore the nested-name-specifier. */
25256 if (!cp_parser_parse_definitely (parser))
25257 {
25258 invalid_nested_name_p = true;
25259 type_start_token = cp_lexer_peek_token (parser->lexer);
25260 id = cp_parser_identifier (parser);
25261 if (id == error_mark_node)
25262 id = NULL_TREE;
25263 }
25264 /* If we could not find a corresponding TYPE, treat this
25265 declaration like an unqualified declaration. */
25266 if (type == error_mark_node)
25267 nested_name_specifier = NULL_TREE;
25268 /* Otherwise, count the number of templates used in TYPE and its
25269 containing scopes. */
25270 else
25271 num_templates = num_template_headers_for_class (TREE_TYPE (type));
25272 }
25273 /* Otherwise, the identifier is optional. */
25274 else
25275 {
25276 /* We don't know whether what comes next is a template-id,
25277 an identifier, or nothing at all. */
25278 cp_parser_parse_tentatively (parser);
25279 /* Check for a template-id. */
25280 type_start_token = cp_lexer_peek_token (parser->lexer);
25281 id = cp_parser_template_id (parser,
25282 /*template_keyword_p=*/false,
25283 /*check_dependency_p=*/true,
25284 class_key,
25285 /*is_declaration=*/true);
25286 /* If that didn't work, it could still be an identifier. */
25287 if (!cp_parser_parse_definitely (parser))
25288 {
25289 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25290 {
25291 type_start_token = cp_lexer_peek_token (parser->lexer);
25292 id = cp_parser_identifier (parser);
25293 }
25294 else
25295 id = NULL_TREE;
25296 }
25297 else
25298 {
25299 template_id_p = true;
25300 ++num_templates;
25301 }
25302 }
25303
25304 pop_deferring_access_checks ();
25305
25306 if (id)
25307 {
25308 cp_parser_check_for_invalid_template_id (parser, id,
25309 class_key,
25310 type_start_token->location);
25311 }
25312 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
25313
25314 /* If it's not a `:' or a `{' then we can't really be looking at a
25315 class-head, since a class-head only appears as part of a
25316 class-specifier. We have to detect this situation before calling
25317 xref_tag, since that has irreversible side-effects. */
25318 if (!cp_parser_next_token_starts_class_definition_p (parser))
25319 {
25320 cp_parser_error (parser, "expected %<{%> or %<:%>");
25321 type = error_mark_node;
25322 goto out;
25323 }
25324
25325 /* At this point, we're going ahead with the class-specifier, even
25326 if some other problem occurs. */
25327 cp_parser_commit_to_tentative_parse (parser);
25328 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
25329 {
25330 cp_parser_error (parser,
25331 "cannot specify %<override%> for a class");
25332 type = error_mark_node;
25333 goto out;
25334 }
25335 /* Issue the error about the overly-qualified name now. */
25336 if (qualified_p)
25337 {
25338 cp_parser_error (parser,
25339 "global qualification of class name is invalid");
25340 type = error_mark_node;
25341 goto out;
25342 }
25343 else if (invalid_nested_name_p)
25344 {
25345 cp_parser_error (parser,
25346 "qualified name does not name a class");
25347 type = error_mark_node;
25348 goto out;
25349 }
25350 else if (nested_name_specifier)
25351 {
25352 tree scope;
25353
25354 if (bad_template_keyword)
25355 /* [temp.names]: in a qualified-id formed by a class-head-name, the
25356 keyword template shall not appear at the top level. */
25357 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
25358 "keyword %<template%> not allowed in class-head-name");
25359
25360 /* Reject typedef-names in class heads. */
25361 if (!DECL_IMPLICIT_TYPEDEF_P (type))
25362 {
25363 error_at (type_start_token->location,
25364 "invalid class name in declaration of %qD",
25365 type);
25366 type = NULL_TREE;
25367 goto done;
25368 }
25369
25370 /* Figure out in what scope the declaration is being placed. */
25371 scope = current_scope ();
25372 /* If that scope does not contain the scope in which the
25373 class was originally declared, the program is invalid. */
25374 if (scope && !is_ancestor (scope, nested_name_specifier))
25375 {
25376 if (at_namespace_scope_p ())
25377 error_at (type_start_token->location,
25378 "declaration of %qD in namespace %qD which does not "
25379 "enclose %qD",
25380 type, scope, nested_name_specifier);
25381 else
25382 error_at (type_start_token->location,
25383 "declaration of %qD in %qD which does not enclose %qD",
25384 type, scope, nested_name_specifier);
25385 type = NULL_TREE;
25386 goto done;
25387 }
25388 /* [dcl.meaning]
25389
25390 A declarator-id shall not be qualified except for the
25391 definition of a ... nested class outside of its class
25392 ... [or] the definition or explicit instantiation of a
25393 class member of a namespace outside of its namespace. */
25394 if (scope == nested_name_specifier)
25395 permerror (nested_name_specifier_token_start->location,
25396 "extra qualification not allowed");
25397 }
25398 /* An explicit-specialization must be preceded by "template <>". If
25399 it is not, try to recover gracefully. */
25400 if (at_namespace_scope_p ()
25401 && parser->num_template_parameter_lists == 0
25402 && !processing_template_parmlist
25403 && template_id_p)
25404 {
25405 /* Build a location of this form:
25406 struct typename <ARGS>
25407 ^~~~~~~~~~~~~~~~~~~~~~
25408 with caret==start at the start token, and
25409 finishing at the end of the type. */
25410 location_t reported_loc
25411 = make_location (class_head_start_location,
25412 class_head_start_location,
25413 get_finish (type_start_token->location));
25414 rich_location richloc (line_table, reported_loc);
25415 richloc.add_fixit_insert_before (class_head_start_location,
25416 "template <> ");
25417 error_at (&richloc,
25418 "an explicit specialization must be preceded by"
25419 " %<template <>%>");
25420 invalid_explicit_specialization_p = true;
25421 /* Take the same action that would have been taken by
25422 cp_parser_explicit_specialization. */
25423 ++parser->num_template_parameter_lists;
25424 begin_specialization ();
25425 }
25426 /* There must be no "return" statements between this point and the
25427 end of this function; set "type "to the correct return value and
25428 use "goto done;" to return. */
25429 /* Make sure that the right number of template parameters were
25430 present. */
25431 if (!cp_parser_check_template_parameters (parser, num_templates,
25432 template_id_p,
25433 type_start_token->location,
25434 /*declarator=*/NULL))
25435 {
25436 /* If something went wrong, there is no point in even trying to
25437 process the class-definition. */
25438 type = NULL_TREE;
25439 goto done;
25440 }
25441
25442 /* Look up the type. */
25443 if (template_id_p)
25444 {
25445 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
25446 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
25447 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
25448 {
25449 error_at (type_start_token->location,
25450 "function template %qD redeclared as a class template", id);
25451 type = error_mark_node;
25452 }
25453 else
25454 {
25455 type = TREE_TYPE (id);
25456 type = maybe_process_partial_specialization (type);
25457
25458 /* Check the scope while we still know whether or not we had a
25459 nested-name-specifier. */
25460 if (type != error_mark_node)
25461 check_unqualified_spec_or_inst (type, type_start_token->location);
25462 }
25463 if (nested_name_specifier)
25464 pushed_scope = push_scope (nested_name_specifier);
25465 }
25466 else if (nested_name_specifier)
25467 {
25468 tree class_type;
25469
25470 /* Given:
25471
25472 template <typename T> struct S { struct T };
25473 template <typename T> struct S<T>::T { };
25474
25475 we will get a TYPENAME_TYPE when processing the definition of
25476 `S::T'. We need to resolve it to the actual type before we
25477 try to define it. */
25478 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
25479 {
25480 class_type = resolve_typename_type (TREE_TYPE (type),
25481 /*only_current_p=*/false);
25482 if (TREE_CODE (class_type) != TYPENAME_TYPE)
25483 type = TYPE_NAME (class_type);
25484 else
25485 {
25486 cp_parser_error (parser, "could not resolve typename type");
25487 type = error_mark_node;
25488 }
25489 }
25490
25491 if (maybe_process_partial_specialization (TREE_TYPE (type))
25492 == error_mark_node)
25493 {
25494 type = NULL_TREE;
25495 goto done;
25496 }
25497
25498 class_type = current_class_type;
25499 /* Enter the scope indicated by the nested-name-specifier. */
25500 pushed_scope = push_scope (nested_name_specifier);
25501 /* Get the canonical version of this type. */
25502 type = TYPE_MAIN_DECL (TREE_TYPE (type));
25503 /* Call push_template_decl if it seems like we should be defining a
25504 template either from the template headers or the type we're
25505 defining, so that we diagnose both extra and missing headers. */
25506 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
25507 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
25508 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
25509 {
25510 type = push_template_decl (type);
25511 if (type == error_mark_node)
25512 {
25513 type = NULL_TREE;
25514 goto done;
25515 }
25516 }
25517
25518 type = TREE_TYPE (type);
25519 *nested_name_specifier_p = true;
25520 }
25521 else /* The name is not a nested name. */
25522 {
25523 /* If the class was unnamed, create a dummy name. */
25524 if (!id)
25525 id = make_anon_name ();
25526 TAG_how how = (parser->in_type_id_in_expr_p
25527 ? TAG_how::INNERMOST_NON_CLASS
25528 : TAG_how::CURRENT_ONLY);
25529 type = xref_tag (class_key, id, how,
25530 parser->num_template_parameter_lists);
25531 }
25532
25533 /* Diagnose class/struct/union mismatches. */
25534 cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
25535 true, true);
25536
25537 /* Indicate whether this class was declared as a `class' or as a
25538 `struct'. */
25539 if (TREE_CODE (type) == RECORD_TYPE)
25540 CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
25541
25542 /* If this type was already complete, and we see another definition,
25543 that's an error. Likewise if the type is already being defined:
25544 this can happen, eg, when it's defined from within an expression
25545 (c++/84605). */
25546 if (type != error_mark_node
25547 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
25548 {
25549 error_at (type_start_token->location, "redefinition of %q#T",
25550 type);
25551 inform (location_of (type), "previous definition of %q#T",
25552 type);
25553 type = NULL_TREE;
25554 goto done;
25555 }
25556 else if (type == error_mark_node)
25557 type = NULL_TREE;
25558
25559 if (type)
25560 {
25561 /* Apply attributes now, before any use of the class as a template
25562 argument in its base list. */
25563 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
25564 fixup_attribute_variants (type);
25565 }
25566
25567 /* Associate constraints with the type. */
25568 if (flag_concepts)
25569 type = associate_classtype_constraints (type);
25570
25571 /* We will have entered the scope containing the class; the names of
25572 base classes should be looked up in that context. For example:
25573
25574 struct A { struct B {}; struct C; };
25575 struct A::C : B {};
25576
25577 is valid. */
25578
25579 /* Get the list of base-classes, if there is one. */
25580 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
25581 {
25582 /* PR59482: enter the class scope so that base-specifiers are looked
25583 up correctly. */
25584 if (type)
25585 pushclass (type);
25586 bases = cp_parser_base_clause (parser);
25587 /* PR59482: get out of the previously pushed class scope so that the
25588 subsequent pops pop the right thing. */
25589 if (type)
25590 popclass ();
25591 }
25592 else
25593 bases = NULL_TREE;
25594
25595 /* If we're really defining a class, process the base classes.
25596 If they're invalid, fail. */
25597 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25598 xref_basetypes (type, bases);
25599
25600 done:
25601 /* Leave the scope given by the nested-name-specifier. We will
25602 enter the class scope itself while processing the members. */
25603 if (pushed_scope)
25604 pop_scope (pushed_scope);
25605
25606 if (invalid_explicit_specialization_p)
25607 {
25608 end_specialization ();
25609 --parser->num_template_parameter_lists;
25610 }
25611
25612 if (type)
25613 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
25614 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
25615 CLASSTYPE_FINAL (type) = 1;
25616 out:
25617 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
25618 return type;
25619 }
25620
25621 /* Parse a class-key.
25622
25623 class-key:
25624 class
25625 struct
25626 union
25627
25628 Returns the kind of class-key specified, or none_type to indicate
25629 error. */
25630
25631 static enum tag_types
25632 cp_parser_class_key (cp_parser* parser)
25633 {
25634 cp_token *token;
25635 enum tag_types tag_type;
25636
25637 /* Look for the class-key. */
25638 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
25639 if (!token)
25640 return none_type;
25641
25642 /* Check to see if the TOKEN is a class-key. */
25643 tag_type = cp_parser_token_is_class_key (token);
25644 if (!tag_type)
25645 cp_parser_error (parser, "expected class-key");
25646 return tag_type;
25647 }
25648
25649 /* Parse a type-parameter-key.
25650
25651 type-parameter-key:
25652 class
25653 typename
25654 */
25655
25656 static void
25657 cp_parser_type_parameter_key (cp_parser* parser)
25658 {
25659 /* Look for the type-parameter-key. */
25660 enum tag_types tag_type = none_type;
25661 cp_token *token = cp_lexer_peek_token (parser->lexer);
25662 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
25663 {
25664 cp_lexer_consume_token (parser->lexer);
25665 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
25666 /* typename is not allowed in a template template parameter
25667 by the standard until C++17. */
25668 pedwarn (token->location, OPT_Wpedantic,
25669 "ISO C++ forbids typename key in template template parameter;"
25670 " use %<-std=c++17%> or %<-std=gnu++17%>");
25671 }
25672 else
25673 cp_parser_error (parser, "expected %<class%> or %<typename%>");
25674
25675 return;
25676 }
25677
25678 /* Parse an (optional) member-specification.
25679
25680 member-specification:
25681 member-declaration member-specification [opt]
25682 access-specifier : member-specification [opt] */
25683
25684 static void
25685 cp_parser_member_specification_opt (cp_parser* parser)
25686 {
25687 while (true)
25688 {
25689 cp_token *token;
25690 enum rid keyword;
25691
25692 /* Peek at the next token. */
25693 token = cp_lexer_peek_token (parser->lexer);
25694 /* If it's a `}', or EOF then we've seen all the members. */
25695 if (token->type == CPP_CLOSE_BRACE
25696 || token->type == CPP_EOF
25697 || token->type == CPP_PRAGMA_EOL)
25698 break;
25699
25700 /* See if this token is a keyword. */
25701 keyword = token->keyword;
25702 switch (keyword)
25703 {
25704 case RID_PUBLIC:
25705 case RID_PROTECTED:
25706 case RID_PRIVATE:
25707 /* Consume the access-specifier. */
25708 cp_lexer_consume_token (parser->lexer);
25709 /* Remember which access-specifier is active. */
25710 current_access_specifier = token->u.value;
25711 /* Look for the `:'. */
25712 cp_parser_require (parser, CPP_COLON, RT_COLON);
25713 break;
25714
25715 default:
25716 /* Accept #pragmas at class scope. */
25717 if (token->type == CPP_PRAGMA)
25718 {
25719 cp_parser_pragma (parser, pragma_member, NULL);
25720 break;
25721 }
25722
25723 /* Otherwise, the next construction must be a
25724 member-declaration. */
25725 cp_parser_member_declaration (parser);
25726 }
25727 }
25728 }
25729
25730 /* Parse a member-declaration.
25731
25732 member-declaration:
25733 decl-specifier-seq [opt] member-declarator-list [opt] ;
25734 function-definition ; [opt]
25735 :: [opt] nested-name-specifier template [opt] unqualified-id ;
25736 using-declaration
25737 template-declaration
25738 alias-declaration
25739
25740 member-declarator-list:
25741 member-declarator
25742 member-declarator-list , member-declarator
25743
25744 member-declarator:
25745 declarator pure-specifier [opt]
25746 declarator constant-initializer [opt]
25747 identifier [opt] : constant-expression
25748
25749 GNU Extensions:
25750
25751 member-declaration:
25752 __extension__ member-declaration
25753
25754 member-declarator:
25755 declarator attributes [opt] pure-specifier [opt]
25756 declarator attributes [opt] constant-initializer [opt]
25757 identifier [opt] attributes [opt] : constant-expression
25758
25759 C++0x Extensions:
25760
25761 member-declaration:
25762 static_assert-declaration */
25763
25764 static void
25765 cp_parser_member_declaration (cp_parser* parser)
25766 {
25767 cp_decl_specifier_seq decl_specifiers;
25768 tree prefix_attributes;
25769 tree decl;
25770 int declares_class_or_enum;
25771 bool friend_p;
25772 cp_token *token = NULL;
25773 cp_token *decl_spec_token_start = NULL;
25774 cp_token *initializer_token_start = NULL;
25775 int saved_pedantic;
25776 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
25777
25778 /* Check for the `__extension__' keyword. */
25779 if (cp_parser_extension_opt (parser, &saved_pedantic))
25780 {
25781 /* Recurse. */
25782 cp_parser_member_declaration (parser);
25783 /* Restore the old value of the PEDANTIC flag. */
25784 pedantic = saved_pedantic;
25785
25786 return;
25787 }
25788
25789 /* Check for a template-declaration. */
25790 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25791 {
25792 /* An explicit specialization here is an error condition, and we
25793 expect the specialization handler to detect and report this. */
25794 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
25795 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
25796 cp_parser_explicit_specialization (parser);
25797 else
25798 cp_parser_template_declaration (parser, /*member_p=*/true);
25799
25800 return;
25801 }
25802 /* Check for a template introduction. */
25803 else if (cp_parser_template_declaration_after_export (parser, true))
25804 return;
25805
25806 /* Check for a using-declaration. */
25807 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25808 {
25809 if (cxx_dialect < cxx11)
25810 /* Parse the using-declaration. */
25811 cp_parser_using_declaration (parser, /*access_declaration_p=*/false);
25812 else if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_ENUM))
25813 cp_parser_using_enum (parser);
25814 else
25815 {
25816 tree decl;
25817 bool alias_decl_expected;
25818 cp_parser_parse_tentatively (parser);
25819 decl = cp_parser_alias_declaration (parser);
25820 /* Note that if we actually see the '=' token after the
25821 identifier, cp_parser_alias_declaration commits the
25822 tentative parse. In that case, we really expect an
25823 alias-declaration. Otherwise, we expect a using
25824 declaration. */
25825 alias_decl_expected =
25826 !cp_parser_uncommitted_to_tentative_parse_p (parser);
25827 cp_parser_parse_definitely (parser);
25828
25829 if (alias_decl_expected)
25830 finish_member_declaration (decl);
25831 else
25832 cp_parser_using_declaration (parser,
25833 /*access_declaration_p=*/false);
25834 }
25835 return;
25836 }
25837
25838 /* Check for @defs. */
25839 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
25840 {
25841 tree ivar, member;
25842 tree ivar_chains = cp_parser_objc_defs_expression (parser);
25843 ivar = ivar_chains;
25844 while (ivar)
25845 {
25846 member = ivar;
25847 ivar = TREE_CHAIN (member);
25848 TREE_CHAIN (member) = NULL_TREE;
25849 finish_member_declaration (member);
25850 }
25851 return;
25852 }
25853
25854 /* If the next token is `static_assert' we have a static assertion. */
25855 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
25856 {
25857 cp_parser_static_assert (parser, /*member_p=*/true);
25858 return;
25859 }
25860
25861 parser->colon_corrects_to_scope_p = false;
25862
25863 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
25864 goto out;
25865
25866 /* Parse the decl-specifier-seq. */
25867 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25868 cp_parser_decl_specifier_seq (parser,
25869 (CP_PARSER_FLAGS_OPTIONAL
25870 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
25871 &decl_specifiers,
25872 &declares_class_or_enum);
25873 /* Check for an invalid type-name. */
25874 if (!decl_specifiers.any_type_specifiers_p
25875 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25876 goto out;
25877 /* If there is no declarator, then the decl-specifier-seq should
25878 specify a type. */
25879 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25880 {
25881 /* If there was no decl-specifier-seq, and the next token is a
25882 `;', then we have something like:
25883
25884 struct S { ; };
25885
25886 [class.mem]
25887
25888 Each member-declaration shall declare at least one member
25889 name of the class. */
25890 if (!decl_specifiers.any_specifiers_p)
25891 {
25892 cp_token *token = cp_lexer_peek_token (parser->lexer);
25893 if (!in_system_header_at (token->location))
25894 {
25895 gcc_rich_location richloc (token->location);
25896 richloc.add_fixit_remove ();
25897 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
25898 }
25899 }
25900 else
25901 {
25902 /* See if this declaration is a friend. */
25903 friend_p = cp_parser_friend_p (&decl_specifiers);
25904 /* If there were decl-specifiers, check to see if there was
25905 a class-declaration. */
25906 tree type = check_tag_decl (&decl_specifiers,
25907 /*explicit_type_instantiation_p=*/false);
25908 /* Nested classes have already been added to the class, but
25909 a `friend' needs to be explicitly registered. */
25910 if (friend_p)
25911 {
25912 /* If the `friend' keyword was present, the friend must
25913 be introduced with a class-key. */
25914 if (!declares_class_or_enum && cxx_dialect < cxx11)
25915 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
25916 "in C++03 a class-key must be used "
25917 "when declaring a friend");
25918 /* In this case:
25919
25920 template <typename T> struct A {
25921 friend struct A<T>::B;
25922 };
25923
25924 A<T>::B will be represented by a TYPENAME_TYPE, and
25925 therefore not recognized by check_tag_decl. */
25926 if (!type)
25927 {
25928 type = decl_specifiers.type;
25929 if (type && TREE_CODE (type) == TYPE_DECL)
25930 type = TREE_TYPE (type);
25931 }
25932 if (!type || !TYPE_P (type))
25933 error_at (decl_spec_token_start->location,
25934 "friend declaration does not name a class or "
25935 "function");
25936 else
25937 make_friend_class (current_class_type, type,
25938 /*complain=*/true);
25939 }
25940 /* If there is no TYPE, an error message will already have
25941 been issued. */
25942 else if (!type || type == error_mark_node)
25943 ;
25944 /* An anonymous aggregate has to be handled specially; such
25945 a declaration really declares a data member (with a
25946 particular type), as opposed to a nested class. */
25947 else if (ANON_AGGR_TYPE_P (type))
25948 {
25949 /* C++11 9.5/6. */
25950 if (decl_specifiers.storage_class != sc_none)
25951 error_at (decl_spec_token_start->location,
25952 "a storage class on an anonymous aggregate "
25953 "in class scope is not allowed");
25954
25955 /* Remove constructors and such from TYPE, now that we
25956 know it is an anonymous aggregate. */
25957 fixup_anonymous_aggr (type);
25958 /* And make the corresponding data member. */
25959 decl = build_decl (decl_spec_token_start->location,
25960 FIELD_DECL, NULL_TREE, type);
25961 /* Add it to the class. */
25962 finish_member_declaration (decl);
25963 }
25964 else
25965 cp_parser_check_access_in_redeclaration
25966 (TYPE_NAME (type),
25967 decl_spec_token_start->location);
25968 }
25969 }
25970 else
25971 {
25972 bool assume_semicolon = false;
25973
25974 /* Clear attributes from the decl_specifiers but keep them
25975 around as prefix attributes that apply them to the entity
25976 being declared. */
25977 prefix_attributes = decl_specifiers.attributes;
25978 decl_specifiers.attributes = NULL_TREE;
25979
25980 /* See if these declarations will be friends. */
25981 friend_p = cp_parser_friend_p (&decl_specifiers);
25982
25983 /* Keep going until we hit the `;' at the end of the
25984 declaration. */
25985 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25986 {
25987 tree attributes = NULL_TREE;
25988 tree first_attribute;
25989 tree initializer;
25990 bool named_bitfld = false;
25991
25992 /* Peek at the next token. */
25993 token = cp_lexer_peek_token (parser->lexer);
25994
25995 /* The following code wants to know early if it is a bit-field
25996 or some other declaration. Attributes can appear before
25997 the `:' token. Skip over them without consuming any tokens
25998 to peek if they are followed by `:'. */
25999 if (cp_next_tokens_can_be_attribute_p (parser)
26000 || (token->type == CPP_NAME
26001 && cp_nth_tokens_can_be_attribute_p (parser, 2)
26002 && (named_bitfld = true)))
26003 {
26004 size_t n
26005 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
26006 token = cp_lexer_peek_nth_token (parser->lexer, n);
26007 }
26008
26009 /* Check for a bitfield declaration. */
26010 if (token->type == CPP_COLON
26011 || (token->type == CPP_NAME
26012 && token == cp_lexer_peek_token (parser->lexer)
26013 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
26014 && (named_bitfld = true)))
26015 {
26016 tree identifier;
26017 tree width;
26018 tree late_attributes = NULL_TREE;
26019 location_t id_location
26020 = cp_lexer_peek_token (parser->lexer)->location;
26021
26022 if (named_bitfld)
26023 identifier = cp_parser_identifier (parser);
26024 else
26025 identifier = NULL_TREE;
26026
26027 /* Look for attributes that apply to the bitfield. */
26028 attributes = cp_parser_attributes_opt (parser);
26029
26030 /* Consume the `:' token. */
26031 cp_lexer_consume_token (parser->lexer);
26032
26033 /* Get the width of the bitfield. */
26034 width = cp_parser_constant_expression (parser, false, NULL,
26035 cxx_dialect >= cxx11);
26036
26037 /* In C++20 and as extension for C++11 and above we allow
26038 default member initializers for bit-fields. */
26039 initializer = NULL_TREE;
26040 if (cxx_dialect >= cxx11
26041 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
26042 || cp_lexer_next_token_is (parser->lexer,
26043 CPP_OPEN_BRACE)))
26044 {
26045 location_t loc
26046 = cp_lexer_peek_token (parser->lexer)->location;
26047 if (cxx_dialect < cxx20
26048 && identifier != NULL_TREE)
26049 pedwarn (loc, 0,
26050 "default member initializers for bit-fields "
26051 "only available with %<-std=c++20%> or "
26052 "%<-std=gnu++20%>");
26053
26054 initializer = cp_parser_save_nsdmi (parser);
26055 if (identifier == NULL_TREE)
26056 {
26057 error_at (loc, "default member initializer for "
26058 "unnamed bit-field");
26059 initializer = NULL_TREE;
26060 }
26061 }
26062 else
26063 {
26064 /* Look for attributes that apply to the bitfield after
26065 the `:' token and width. This is where GCC used to
26066 parse attributes in the past, pedwarn if there is
26067 a std attribute. */
26068 if (cp_next_tokens_can_be_std_attribute_p (parser))
26069 pedwarn (input_location, OPT_Wpedantic,
26070 "ISO C++ allows bit-field attributes only "
26071 "before the %<:%> token");
26072
26073 late_attributes = cp_parser_attributes_opt (parser);
26074 }
26075
26076 attributes = attr_chainon (attributes, late_attributes);
26077
26078 /* Remember which attributes are prefix attributes and
26079 which are not. */
26080 first_attribute = attributes;
26081 /* Combine the attributes. */
26082 attributes = attr_chainon (prefix_attributes, attributes);
26083
26084 /* Create the bitfield declaration. */
26085 decl = grokbitfield (identifier
26086 ? make_id_declarator (NULL_TREE,
26087 identifier,
26088 sfk_none,
26089 id_location)
26090 : NULL,
26091 &decl_specifiers,
26092 width, initializer,
26093 attributes);
26094 }
26095 else
26096 {
26097 cp_declarator *declarator;
26098 tree asm_specification;
26099 int ctor_dtor_or_conv_p;
26100 bool static_p = (decl_specifiers.storage_class == sc_static);
26101 cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
26102 /* We can't delay parsing for friends,
26103 alias-declarations, and typedefs, even though the
26104 standard seems to require it. */
26105 if (!friend_p
26106 && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26107 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
26108
26109 /* Parse the declarator. */
26110 declarator
26111 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26112 flags,
26113 &ctor_dtor_or_conv_p,
26114 /*parenthesized_p=*/NULL,
26115 /*member_p=*/true,
26116 friend_p, static_p);
26117
26118 /* If something went wrong parsing the declarator, make sure
26119 that we at least consume some tokens. */
26120 if (declarator == cp_error_declarator)
26121 {
26122 /* Skip to the end of the statement. */
26123 cp_parser_skip_to_end_of_statement (parser);
26124 /* If the next token is not a semicolon, that is
26125 probably because we just skipped over the body of
26126 a function. So, we consume a semicolon if
26127 present, but do not issue an error message if it
26128 is not present. */
26129 if (cp_lexer_next_token_is (parser->lexer,
26130 CPP_SEMICOLON))
26131 cp_lexer_consume_token (parser->lexer);
26132 goto out;
26133 }
26134
26135 if (declares_class_or_enum & 2)
26136 cp_parser_check_for_definition_in_return_type
26137 (declarator, decl_specifiers.type,
26138 decl_specifiers.locations[ds_type_spec]);
26139
26140 /* Look for an asm-specification. */
26141 asm_specification = cp_parser_asm_specification_opt (parser);
26142 /* Look for attributes that apply to the declaration. */
26143 attributes = cp_parser_attributes_opt (parser);
26144 /* Remember which attributes are prefix attributes and
26145 which are not. */
26146 first_attribute = attributes;
26147 /* Combine the attributes. */
26148 attributes = attr_chainon (prefix_attributes, attributes);
26149
26150 /* If it's an `=', then we have a constant-initializer or a
26151 pure-specifier. It is not correct to parse the
26152 initializer before registering the member declaration
26153 since the member declaration should be in scope while
26154 its initializer is processed. However, the rest of the
26155 front end does not yet provide an interface that allows
26156 us to handle this correctly. */
26157 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
26158 {
26159 /* In [class.mem]:
26160
26161 A pure-specifier shall be used only in the declaration of
26162 a virtual function.
26163
26164 A member-declarator can contain a constant-initializer
26165 only if it declares a static member of integral or
26166 enumeration type.
26167
26168 Therefore, if the DECLARATOR is for a function, we look
26169 for a pure-specifier; otherwise, we look for a
26170 constant-initializer. When we call `grokfield', it will
26171 perform more stringent semantics checks. */
26172 initializer_token_start = cp_lexer_peek_token (parser->lexer);
26173 if (function_declarator_p (declarator)
26174 || (decl_specifiers.type
26175 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
26176 && declarator->kind == cdk_id
26177 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
26178 == FUNCTION_TYPE)))
26179 initializer = cp_parser_pure_specifier (parser);
26180 else if (decl_specifiers.storage_class != sc_static)
26181 initializer = cp_parser_save_nsdmi (parser);
26182 else if (cxx_dialect >= cxx11)
26183 {
26184 bool nonconst;
26185 /* Don't require a constant rvalue in C++11, since we
26186 might want a reference constant. We'll enforce
26187 constancy later. */
26188 cp_lexer_consume_token (parser->lexer);
26189 /* Parse the initializer. */
26190 initializer = cp_parser_initializer_clause (parser,
26191 &nonconst);
26192 }
26193 else
26194 /* Parse the initializer. */
26195 initializer = cp_parser_constant_initializer (parser);
26196 }
26197 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
26198 && !function_declarator_p (declarator))
26199 {
26200 bool x;
26201 if (decl_specifiers.storage_class != sc_static)
26202 initializer = cp_parser_save_nsdmi (parser);
26203 else
26204 initializer = cp_parser_initializer (parser, &x, &x);
26205 }
26206 /* Detect invalid bit-field cases such as
26207
26208 int *p : 4;
26209 int &&r : 3;
26210
26211 and similar. */
26212 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
26213 /* If there were no type specifiers, it was a
26214 constructor. */
26215 && decl_specifiers.any_type_specifiers_p)
26216 {
26217 /* This is called for a decent diagnostic only. */
26218 tree d = grokdeclarator (declarator, &decl_specifiers,
26219 BITFIELD, /*initialized=*/false,
26220 &attributes);
26221 if (!error_operand_p (d))
26222 error_at (DECL_SOURCE_LOCATION (d),
26223 "bit-field %qD has non-integral type %qT",
26224 d, TREE_TYPE (d));
26225 cp_parser_skip_to_end_of_statement (parser);
26226 /* Avoid "extra ;" pedwarns. */
26227 if (cp_lexer_next_token_is (parser->lexer,
26228 CPP_SEMICOLON))
26229 cp_lexer_consume_token (parser->lexer);
26230 goto out;
26231 }
26232 /* Otherwise, there is no initializer. */
26233 else
26234 initializer = NULL_TREE;
26235
26236 /* See if we are probably looking at a function
26237 definition. We are certainly not looking at a
26238 member-declarator. Calling `grokfield' has
26239 side-effects, so we must not do it unless we are sure
26240 that we are looking at a member-declarator. */
26241 if (cp_parser_token_starts_function_definition_p
26242 (cp_lexer_peek_token (parser->lexer)))
26243 {
26244 /* The grammar does not allow a pure-specifier to be
26245 used when a member function is defined. (It is
26246 possible that this fact is an oversight in the
26247 standard, since a pure function may be defined
26248 outside of the class-specifier. */
26249 if (initializer && initializer_token_start)
26250 error_at (initializer_token_start->location,
26251 "pure-specifier on function-definition");
26252 decl = cp_parser_save_member_function_body (parser,
26253 &decl_specifiers,
26254 declarator,
26255 attributes);
26256 if (parser->fully_implicit_function_template_p)
26257 decl = finish_fully_implicit_template (parser, decl);
26258 /* If the member was not a friend, declare it here. */
26259 if (!friend_p)
26260 finish_member_declaration (decl);
26261 /* Peek at the next token. */
26262 token = cp_lexer_peek_token (parser->lexer);
26263 /* If the next token is a semicolon, consume it. */
26264 if (token->type == CPP_SEMICOLON)
26265 {
26266 location_t semicolon_loc
26267 = cp_lexer_consume_token (parser->lexer)->location;
26268 gcc_rich_location richloc (semicolon_loc);
26269 richloc.add_fixit_remove ();
26270 warning_at (&richloc, OPT_Wextra_semi,
26271 "extra %<;%> after in-class "
26272 "function definition");
26273 }
26274 goto out;
26275 }
26276 else
26277 if (declarator->kind == cdk_function)
26278 declarator->id_loc = token->location;
26279 /* Create the declaration. */
26280 decl = grokfield (declarator, &decl_specifiers,
26281 initializer, /*init_const_expr_p=*/true,
26282 asm_specification, attributes);
26283 if (parser->fully_implicit_function_template_p)
26284 {
26285 if (friend_p)
26286 finish_fully_implicit_template (parser, 0);
26287 else
26288 decl = finish_fully_implicit_template (parser, decl);
26289 }
26290 }
26291
26292 cp_finalize_omp_declare_simd (parser, decl);
26293 cp_finalize_oacc_routine (parser, decl, false);
26294
26295 /* Reset PREFIX_ATTRIBUTES. */
26296 if (attributes != error_mark_node)
26297 {
26298 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26299 attributes = TREE_CHAIN (attributes);
26300 if (attributes)
26301 TREE_CHAIN (attributes) = NULL_TREE;
26302 }
26303
26304 /* If there is any qualification still in effect, clear it
26305 now; we will be starting fresh with the next declarator. */
26306 parser->scope = NULL_TREE;
26307 parser->qualifying_scope = NULL_TREE;
26308 parser->object_scope = NULL_TREE;
26309 /* If it's a `,', then there are more declarators. */
26310 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26311 {
26312 cp_lexer_consume_token (parser->lexer);
26313 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26314 {
26315 cp_token *token = cp_lexer_previous_token (parser->lexer);
26316 gcc_rich_location richloc (token->location);
26317 richloc.add_fixit_remove ();
26318 error_at (&richloc, "stray %<,%> at end of "
26319 "member declaration");
26320 }
26321 }
26322 /* If the next token isn't a `;', then we have a parse error. */
26323 else if (cp_lexer_next_token_is_not (parser->lexer,
26324 CPP_SEMICOLON))
26325 {
26326 /* The next token might be a ways away from where the
26327 actual semicolon is missing. Find the previous token
26328 and use that for our error position. */
26329 cp_token *token = cp_lexer_previous_token (parser->lexer);
26330 gcc_rich_location richloc (token->location);
26331 richloc.add_fixit_insert_after (";");
26332 error_at (&richloc, "expected %<;%> at end of "
26333 "member declaration");
26334
26335 /* Assume that the user meant to provide a semicolon. If
26336 we were to cp_parser_skip_to_end_of_statement, we might
26337 skip to a semicolon inside a member function definition
26338 and issue nonsensical error messages. */
26339 assume_semicolon = true;
26340 }
26341
26342 if (decl)
26343 {
26344 /* Add DECL to the list of members. */
26345 if (!friend_p
26346 /* Explicitly include, eg, NSDMIs, for better error
26347 recovery (c++/58650). */
26348 || !DECL_DECLARES_FUNCTION_P (decl))
26349 finish_member_declaration (decl);
26350
26351 if (TREE_CODE (decl) == FUNCTION_DECL)
26352 cp_parser_save_default_args (parser, decl);
26353 else if (TREE_CODE (decl) == FIELD_DECL
26354 && DECL_INITIAL (decl))
26355 /* Add DECL to the queue of NSDMI to be parsed later. */
26356 vec_safe_push (unparsed_nsdmis, decl);
26357 }
26358
26359 if (assume_semicolon)
26360 goto out;
26361 }
26362 }
26363
26364 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26365 out:
26366 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
26367 }
26368
26369 /* Parse a pure-specifier.
26370
26371 pure-specifier:
26372 = 0
26373
26374 Returns INTEGER_ZERO_NODE if a pure specifier is found.
26375 Otherwise, ERROR_MARK_NODE is returned. */
26376
26377 static tree
26378 cp_parser_pure_specifier (cp_parser* parser)
26379 {
26380 cp_token *token;
26381
26382 /* Look for the `=' token. */
26383 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26384 return error_mark_node;
26385 /* Look for the `0' token. */
26386 token = cp_lexer_peek_token (parser->lexer);
26387
26388 if (token->type == CPP_EOF
26389 || token->type == CPP_PRAGMA_EOL)
26390 return error_mark_node;
26391
26392 cp_lexer_consume_token (parser->lexer);
26393
26394 /* Accept = default or = delete in c++0x mode. */
26395 if (token->keyword == RID_DEFAULT
26396 || token->keyword == RID_DELETE)
26397 {
26398 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
26399 return token->u.value;
26400 }
26401
26402 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
26403 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
26404 {
26405 cp_parser_error (parser,
26406 "invalid pure specifier (only %<= 0%> is allowed)");
26407 cp_parser_skip_to_end_of_statement (parser);
26408 return error_mark_node;
26409 }
26410 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
26411 {
26412 error_at (token->location, "templates may not be %<virtual%>");
26413 return error_mark_node;
26414 }
26415
26416 return integer_zero_node;
26417 }
26418
26419 /* Parse a constant-initializer.
26420
26421 constant-initializer:
26422 = constant-expression
26423
26424 Returns a representation of the constant-expression. */
26425
26426 static tree
26427 cp_parser_constant_initializer (cp_parser* parser)
26428 {
26429 /* Look for the `=' token. */
26430 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26431 return error_mark_node;
26432
26433 /* It is invalid to write:
26434
26435 struct S { static const int i = { 7 }; };
26436
26437 */
26438 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26439 {
26440 cp_parser_error (parser,
26441 "a brace-enclosed initializer is not allowed here");
26442 /* Consume the opening brace. */
26443 matching_braces braces;
26444 braces.consume_open (parser);
26445 /* Skip the initializer. */
26446 cp_parser_skip_to_closing_brace (parser);
26447 /* Look for the trailing `}'. */
26448 braces.require_close (parser);
26449
26450 return error_mark_node;
26451 }
26452
26453 return cp_parser_constant_expression (parser);
26454 }
26455
26456 /* Derived classes [gram.class.derived] */
26457
26458 /* Parse a base-clause.
26459
26460 base-clause:
26461 : base-specifier-list
26462
26463 base-specifier-list:
26464 base-specifier ... [opt]
26465 base-specifier-list , base-specifier ... [opt]
26466
26467 Returns a TREE_LIST representing the base-classes, in the order in
26468 which they were declared. The representation of each node is as
26469 described by cp_parser_base_specifier.
26470
26471 In the case that no bases are specified, this function will return
26472 NULL_TREE, not ERROR_MARK_NODE. */
26473
26474 static tree
26475 cp_parser_base_clause (cp_parser* parser)
26476 {
26477 tree bases = NULL_TREE;
26478
26479 /* Look for the `:' that begins the list. */
26480 cp_parser_require (parser, CPP_COLON, RT_COLON);
26481
26482 /* Scan the base-specifier-list. */
26483 while (true)
26484 {
26485 cp_token *token;
26486 tree base;
26487 bool pack_expansion_p = false;
26488
26489 /* Look for the base-specifier. */
26490 base = cp_parser_base_specifier (parser);
26491 /* Look for the (optional) ellipsis. */
26492 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26493 {
26494 /* Consume the `...'. */
26495 cp_lexer_consume_token (parser->lexer);
26496
26497 pack_expansion_p = true;
26498 }
26499
26500 /* Add BASE to the front of the list. */
26501 if (base && base != error_mark_node)
26502 {
26503 if (pack_expansion_p)
26504 /* Make this a pack expansion type. */
26505 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
26506
26507 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
26508 {
26509 TREE_CHAIN (base) = bases;
26510 bases = base;
26511 }
26512 }
26513 /* Peek at the next token. */
26514 token = cp_lexer_peek_token (parser->lexer);
26515 /* If it's not a comma, then the list is complete. */
26516 if (token->type != CPP_COMMA)
26517 break;
26518 /* Consume the `,'. */
26519 cp_lexer_consume_token (parser->lexer);
26520 }
26521
26522 /* PARSER->SCOPE may still be non-NULL at this point, if the last
26523 base class had a qualified name. However, the next name that
26524 appears is certainly not qualified. */
26525 parser->scope = NULL_TREE;
26526 parser->qualifying_scope = NULL_TREE;
26527 parser->object_scope = NULL_TREE;
26528
26529 return nreverse (bases);
26530 }
26531
26532 /* Parse a base-specifier.
26533
26534 base-specifier:
26535 :: [opt] nested-name-specifier [opt] class-name
26536 virtual access-specifier [opt] :: [opt] nested-name-specifier
26537 [opt] class-name
26538 access-specifier virtual [opt] :: [opt] nested-name-specifier
26539 [opt] class-name
26540
26541 Returns a TREE_LIST. The TREE_PURPOSE will be one of
26542 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
26543 indicate the specifiers provided. The TREE_VALUE will be a TYPE
26544 (or the ERROR_MARK_NODE) indicating the type that was specified. */
26545
26546 static tree
26547 cp_parser_base_specifier (cp_parser* parser)
26548 {
26549 cp_token *token;
26550 bool done = false;
26551 bool virtual_p = false;
26552 bool duplicate_virtual_error_issued_p = false;
26553 bool duplicate_access_error_issued_p = false;
26554 bool class_scope_p, template_p;
26555 tree access = access_default_node;
26556 tree type;
26557
26558 /* Process the optional `virtual' and `access-specifier'. */
26559 while (!done)
26560 {
26561 /* Peek at the next token. */
26562 token = cp_lexer_peek_token (parser->lexer);
26563 /* Process `virtual'. */
26564 switch (token->keyword)
26565 {
26566 case RID_VIRTUAL:
26567 /* If `virtual' appears more than once, issue an error. */
26568 if (virtual_p && !duplicate_virtual_error_issued_p)
26569 {
26570 cp_parser_error (parser,
26571 "%<virtual%> specified more than once in base-specifier");
26572 duplicate_virtual_error_issued_p = true;
26573 }
26574
26575 virtual_p = true;
26576
26577 /* Consume the `virtual' token. */
26578 cp_lexer_consume_token (parser->lexer);
26579
26580 break;
26581
26582 case RID_PUBLIC:
26583 case RID_PROTECTED:
26584 case RID_PRIVATE:
26585 /* If more than one access specifier appears, issue an
26586 error. */
26587 if (access != access_default_node
26588 && !duplicate_access_error_issued_p)
26589 {
26590 cp_parser_error (parser,
26591 "more than one access specifier in base-specifier");
26592 duplicate_access_error_issued_p = true;
26593 }
26594
26595 access = ridpointers[(int) token->keyword];
26596
26597 /* Consume the access-specifier. */
26598 cp_lexer_consume_token (parser->lexer);
26599
26600 break;
26601
26602 default:
26603 done = true;
26604 break;
26605 }
26606 }
26607 /* It is not uncommon to see programs mechanically, erroneously, use
26608 the 'typename' keyword to denote (dependent) qualified types
26609 as base classes. */
26610 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26611 {
26612 token = cp_lexer_peek_token (parser->lexer);
26613 if (!processing_template_decl)
26614 error_at (token->location,
26615 "keyword %<typename%> not allowed outside of templates");
26616 else
26617 error_at (token->location,
26618 "keyword %<typename%> not allowed in this context "
26619 "(the base class is implicitly a type)");
26620 cp_lexer_consume_token (parser->lexer);
26621 }
26622
26623 /* Look for the optional `::' operator. */
26624 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
26625 /* Look for the nested-name-specifier. The simplest way to
26626 implement:
26627
26628 [temp.res]
26629
26630 The keyword `typename' is not permitted in a base-specifier or
26631 mem-initializer; in these contexts a qualified name that
26632 depends on a template-parameter is implicitly assumed to be a
26633 type name.
26634
26635 is to pretend that we have seen the `typename' keyword at this
26636 point. */
26637 cp_parser_nested_name_specifier_opt (parser,
26638 /*typename_keyword_p=*/true,
26639 /*check_dependency_p=*/true,
26640 /*type_p=*/true,
26641 /*is_declaration=*/true);
26642 /* If the base class is given by a qualified name, assume that names
26643 we see are type names or templates, as appropriate. */
26644 class_scope_p = (parser->scope && TYPE_P (parser->scope));
26645 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
26646
26647 if (!parser->scope
26648 && cp_lexer_next_token_is_decltype (parser->lexer))
26649 /* DR 950 allows decltype as a base-specifier. */
26650 type = cp_parser_decltype (parser);
26651 else
26652 {
26653 /* Otherwise, look for the class-name. */
26654 type = cp_parser_class_name (parser,
26655 class_scope_p,
26656 template_p,
26657 typename_type,
26658 /*check_dependency_p=*/true,
26659 /*class_head_p=*/false,
26660 /*is_declaration=*/true);
26661 type = TREE_TYPE (type);
26662 }
26663
26664 if (type == error_mark_node)
26665 return error_mark_node;
26666
26667 return finish_base_specifier (type, access, virtual_p);
26668 }
26669
26670 /* Exception handling [gram.exception] */
26671
26672 /* Save the tokens that make up the noexcept-specifier for a member-function.
26673 Returns a DEFERRED_PARSE. */
26674
26675 static tree
26676 cp_parser_save_noexcept (cp_parser *parser)
26677 {
26678 cp_token *first = parser->lexer->next_token;
26679 /* We want everything up to, including, the final ')'. */
26680 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
26681 cp_token *last = parser->lexer->next_token;
26682
26683 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
26684 to carry the information we will need. */
26685 tree expr = make_node (DEFERRED_PARSE);
26686 /* Save away the noexcept-specifier; we will process it when the
26687 class is complete. */
26688 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
26689 expr = build_tree_list (expr, NULL_TREE);
26690 return expr;
26691 }
26692
26693 /* Used for late processing of noexcept-specifiers of member-functions.
26694 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
26695 we saved for later; parse it now. DECL is the declaration of the
26696 member function. */
26697
26698 static tree
26699 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
26700 {
26701 /* Make sure we've gotten something that hasn't been parsed yet. */
26702 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
26703
26704 push_unparsed_function_queues (parser);
26705
26706 /* Push the saved tokens for the noexcept-specifier onto the parser's
26707 lexer stack. */
26708 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
26709 cp_parser_push_lexer_for_tokens (parser, tokens);
26710
26711 /* Parse the cached noexcept-specifier. */
26712 tree parsed_arg
26713 = cp_parser_noexcept_specification_opt (parser,
26714 CP_PARSER_FLAGS_NONE,
26715 /*require_constexpr=*/true,
26716 /*consumed_expr=*/NULL,
26717 /*return_cond=*/false);
26718
26719 /* Revert to the main lexer. */
26720 cp_parser_pop_lexer (parser);
26721
26722 /* Restore the queue. */
26723 pop_unparsed_function_queues (parser);
26724
26725 /* And we're done. */
26726 return parsed_arg;
26727 }
26728
26729 /* Perform late checking of overriding function with respect to their
26730 noexcept-specifiers. TYPE is the class and FNDECL is the function
26731 that potentially overrides some virtual function with the same
26732 signature. */
26733
26734 static void
26735 noexcept_override_late_checks (tree type, tree fndecl)
26736 {
26737 tree binfo = TYPE_BINFO (type);
26738 tree base_binfo;
26739
26740 if (DECL_STATIC_FUNCTION_P (fndecl))
26741 return;
26742
26743 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
26744 {
26745 tree basetype = BINFO_TYPE (base_binfo);
26746
26747 if (!TYPE_POLYMORPHIC_P (basetype))
26748 continue;
26749
26750 tree fn = look_for_overrides_here (basetype, fndecl);
26751 if (fn)
26752 maybe_check_overriding_exception_spec (fndecl, fn);
26753 }
26754 }
26755
26756 /* Parse an (optional) noexcept-specification.
26757
26758 noexcept-specification:
26759 noexcept ( constant-expression ) [opt]
26760
26761 If no noexcept-specification is present, returns NULL_TREE.
26762 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
26763 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
26764 there are no parentheses. CONSUMED_EXPR will be set accordingly.
26765 Otherwise, returns a noexcept specification unless RETURN_COND is true,
26766 in which case a boolean condition is returned instead. The parser flags
26767 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
26768 the (member) function is `const'. */
26769
26770 static tree
26771 cp_parser_noexcept_specification_opt (cp_parser* parser,
26772 cp_parser_flags flags,
26773 bool require_constexpr,
26774 bool* consumed_expr,
26775 bool return_cond)
26776 {
26777 cp_token *token;
26778 const char *saved_message;
26779
26780 /* Peek at the next token. */
26781 token = cp_lexer_peek_token (parser->lexer);
26782
26783 /* Is it a noexcept-specification? */
26784 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
26785 {
26786 tree expr;
26787
26788 /* [class.mem]/6 says that a noexcept-specifer (within the
26789 member-specification of the class) is a complete-class context of
26790 a class. So, if the noexcept-specifier has the optional expression,
26791 just save the tokens, and reparse this after we're done with the
26792 class. */
26793
26794 if ((flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
26795 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
26796 /* No need to delay parsing for a number literal or true/false. */
26797 && !((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
26798 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26799 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
26800 && at_class_scope_p ()
26801 && TYPE_BEING_DEFINED (current_class_type)
26802 && !LAMBDA_TYPE_P (current_class_type))
26803 return cp_parser_save_noexcept (parser);
26804
26805 cp_lexer_consume_token (parser->lexer);
26806
26807 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
26808 {
26809 matching_parens parens;
26810 parens.consume_open (parser);
26811
26812 if (require_constexpr)
26813 {
26814 /* Types may not be defined in an exception-specification. */
26815 saved_message = parser->type_definition_forbidden_message;
26816 parser->type_definition_forbidden_message
26817 = G_("types may not be defined in an exception-specification");
26818
26819 bool non_constant_p;
26820 expr
26821 = cp_parser_constant_expression (parser,
26822 /*allow_non_constant=*/true,
26823 &non_constant_p);
26824 if (non_constant_p
26825 && !require_potential_rvalue_constant_expression (expr))
26826 {
26827 expr = NULL_TREE;
26828 return_cond = true;
26829 }
26830
26831 /* Restore the saved message. */
26832 parser->type_definition_forbidden_message = saved_message;
26833 }
26834 else
26835 {
26836 expr = cp_parser_expression (parser);
26837 *consumed_expr = true;
26838 }
26839
26840 parens.require_close (parser);
26841 }
26842 else
26843 {
26844 expr = boolean_true_node;
26845 if (!require_constexpr)
26846 *consumed_expr = false;
26847 }
26848
26849 /* We cannot build a noexcept-spec right away because this will check
26850 that expr is a constexpr. */
26851 if (!return_cond)
26852 return build_noexcept_spec (expr, tf_warning_or_error);
26853 else
26854 return expr;
26855 }
26856 else
26857 return NULL_TREE;
26858 }
26859
26860 /* Parse an (optional) exception-specification.
26861
26862 exception-specification:
26863 throw ( type-id-list [opt] )
26864
26865 Returns a TREE_LIST representing the exception-specification. The
26866 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
26867 control parsing. QUALS are qualifiers indicating whether the (member)
26868 function is `const'. */
26869
26870 static tree
26871 cp_parser_exception_specification_opt (cp_parser* parser,
26872 cp_parser_flags flags)
26873 {
26874 cp_token *token;
26875 tree type_id_list;
26876 const char *saved_message;
26877
26878 /* Peek at the next token. */
26879 token = cp_lexer_peek_token (parser->lexer);
26880
26881 /* Is it a noexcept-specification? */
26882 type_id_list
26883 = cp_parser_noexcept_specification_opt (parser, flags,
26884 /*require_constexpr=*/true,
26885 /*consumed_expr=*/NULL,
26886 /*return_cond=*/false);
26887 if (type_id_list != NULL_TREE)
26888 return type_id_list;
26889
26890 /* If it's not `throw', then there's no exception-specification. */
26891 if (!cp_parser_is_keyword (token, RID_THROW))
26892 return NULL_TREE;
26893
26894 location_t loc = token->location;
26895
26896 /* Consume the `throw'. */
26897 cp_lexer_consume_token (parser->lexer);
26898
26899 /* Look for the `('. */
26900 matching_parens parens;
26901 parens.require_open (parser);
26902
26903 /* Peek at the next token. */
26904 token = cp_lexer_peek_token (parser->lexer);
26905 /* If it's not a `)', then there is a type-id-list. */
26906 if (token->type != CPP_CLOSE_PAREN)
26907 {
26908 /* Types may not be defined in an exception-specification. */
26909 saved_message = parser->type_definition_forbidden_message;
26910 parser->type_definition_forbidden_message
26911 = G_("types may not be defined in an exception-specification");
26912 /* Parse the type-id-list. */
26913 type_id_list = cp_parser_type_id_list (parser);
26914 /* Restore the saved message. */
26915 parser->type_definition_forbidden_message = saved_message;
26916
26917 if (cxx_dialect >= cxx17)
26918 {
26919 error_at (loc, "ISO C++17 does not allow dynamic exception "
26920 "specifications");
26921 type_id_list = NULL_TREE;
26922 }
26923 else if (cxx_dialect >= cxx11)
26924 warning_at (loc, OPT_Wdeprecated,
26925 "dynamic exception specifications are deprecated in "
26926 "C++11");
26927 }
26928 /* In C++17, throw() is equivalent to noexcept (true). throw()
26929 is deprecated in C++11 and above as well, but is still widely used,
26930 so don't warn about it yet. */
26931 else if (cxx_dialect >= cxx17)
26932 type_id_list = noexcept_true_spec;
26933 else
26934 type_id_list = empty_except_spec;
26935
26936 /* Look for the `)'. */
26937 parens.require_close (parser);
26938
26939 return type_id_list;
26940 }
26941
26942 /* Parse an (optional) type-id-list.
26943
26944 type-id-list:
26945 type-id ... [opt]
26946 type-id-list , type-id ... [opt]
26947
26948 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
26949 in the order that the types were presented. */
26950
26951 static tree
26952 cp_parser_type_id_list (cp_parser* parser)
26953 {
26954 tree types = NULL_TREE;
26955
26956 while (true)
26957 {
26958 cp_token *token;
26959 tree type;
26960
26961 token = cp_lexer_peek_token (parser->lexer);
26962
26963 /* Get the next type-id. */
26964 type = cp_parser_type_id (parser);
26965 /* Check for invalid 'auto'. */
26966 if (flag_concepts && type_uses_auto (type))
26967 {
26968 error_at (token->location,
26969 "invalid use of %<auto%> in exception-specification");
26970 type = error_mark_node;
26971 }
26972 /* Parse the optional ellipsis. */
26973 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26974 {
26975 /* Consume the `...'. */
26976 cp_lexer_consume_token (parser->lexer);
26977
26978 /* Turn the type into a pack expansion expression. */
26979 type = make_pack_expansion (type);
26980 }
26981 /* Add it to the list. */
26982 types = add_exception_specifier (types, type, /*complain=*/1);
26983 /* Peek at the next token. */
26984 token = cp_lexer_peek_token (parser->lexer);
26985 /* If it is not a `,', we are done. */
26986 if (token->type != CPP_COMMA)
26987 break;
26988 /* Consume the `,'. */
26989 cp_lexer_consume_token (parser->lexer);
26990 }
26991
26992 return nreverse (types);
26993 }
26994
26995 /* Parse a try-block.
26996
26997 try-block:
26998 try compound-statement handler-seq */
26999
27000 static tree
27001 cp_parser_try_block (cp_parser* parser)
27002 {
27003 tree try_block;
27004
27005 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
27006 if (parser->in_function_body
27007 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
27008 && cxx_dialect < cxx20)
27009 pedwarn (input_location, 0,
27010 "%<try%> in %<constexpr%> function only "
27011 "available with %<-std=c++20%> or %<-std=gnu++20%>");
27012
27013 try_block = begin_try_block ();
27014 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
27015 finish_try_block (try_block);
27016 cp_parser_handler_seq (parser);
27017 finish_handler_sequence (try_block);
27018
27019 return try_block;
27020 }
27021
27022 /* Parse a function-try-block.
27023
27024 function-try-block:
27025 try ctor-initializer [opt] function-body handler-seq */
27026
27027 static void
27028 cp_parser_function_try_block (cp_parser* parser)
27029 {
27030 tree compound_stmt;
27031 tree try_block;
27032
27033 /* Look for the `try' keyword. */
27034 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
27035 return;
27036 /* Let the rest of the front end know where we are. */
27037 try_block = begin_function_try_block (&compound_stmt);
27038 /* Parse the function-body. */
27039 cp_parser_ctor_initializer_opt_and_function_body
27040 (parser, /*in_function_try_block=*/true);
27041 /* We're done with the `try' part. */
27042 finish_function_try_block (try_block);
27043 /* Parse the handlers. */
27044 cp_parser_handler_seq (parser);
27045 /* We're done with the handlers. */
27046 finish_function_handler_sequence (try_block, compound_stmt);
27047 }
27048
27049 /* Parse a handler-seq.
27050
27051 handler-seq:
27052 handler handler-seq [opt] */
27053
27054 static void
27055 cp_parser_handler_seq (cp_parser* parser)
27056 {
27057 while (true)
27058 {
27059 cp_token *token;
27060
27061 /* Parse the handler. */
27062 cp_parser_handler (parser);
27063 /* Peek at the next token. */
27064 token = cp_lexer_peek_token (parser->lexer);
27065 /* If it's not `catch' then there are no more handlers. */
27066 if (!cp_parser_is_keyword (token, RID_CATCH))
27067 break;
27068 }
27069 }
27070
27071 /* Parse a handler.
27072
27073 handler:
27074 catch ( exception-declaration ) compound-statement */
27075
27076 static void
27077 cp_parser_handler (cp_parser* parser)
27078 {
27079 tree handler;
27080 tree declaration;
27081
27082 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
27083 handler = begin_handler ();
27084 matching_parens parens;
27085 parens.require_open (parser);
27086 declaration = cp_parser_exception_declaration (parser);
27087 finish_handler_parms (declaration, handler);
27088 parens.require_close (parser);
27089 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
27090 finish_handler (handler);
27091 }
27092
27093 /* Parse an exception-declaration.
27094
27095 exception-declaration:
27096 type-specifier-seq declarator
27097 type-specifier-seq abstract-declarator
27098 type-specifier-seq
27099 ...
27100
27101 Returns a VAR_DECL for the declaration, or NULL_TREE if the
27102 ellipsis variant is used. */
27103
27104 static tree
27105 cp_parser_exception_declaration (cp_parser* parser)
27106 {
27107 cp_decl_specifier_seq type_specifiers;
27108 cp_declarator *declarator;
27109 const char *saved_message;
27110
27111 /* If it's an ellipsis, it's easy to handle. */
27112 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27113 {
27114 /* Consume the `...' token. */
27115 cp_lexer_consume_token (parser->lexer);
27116 return NULL_TREE;
27117 }
27118
27119 /* Types may not be defined in exception-declarations. */
27120 saved_message = parser->type_definition_forbidden_message;
27121 parser->type_definition_forbidden_message
27122 = G_("types may not be defined in exception-declarations");
27123
27124 /* Parse the type-specifier-seq. */
27125 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
27126 /*is_declaration=*/true,
27127 /*is_trailing_return=*/false,
27128 &type_specifiers);
27129 /* If it's a `)', then there is no declarator. */
27130 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27131 declarator = NULL;
27132 else
27133 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
27134 CP_PARSER_FLAGS_NONE,
27135 /*ctor_dtor_or_conv_p=*/NULL,
27136 /*parenthesized_p=*/NULL,
27137 /*member_p=*/false,
27138 /*friend_p=*/false,
27139 /*static_p=*/false);
27140
27141 /* Restore the saved message. */
27142 parser->type_definition_forbidden_message = saved_message;
27143
27144 if (!type_specifiers.any_specifiers_p)
27145 return error_mark_node;
27146
27147 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
27148 }
27149
27150 /* Parse a throw-expression.
27151
27152 throw-expression:
27153 throw assignment-expression [opt]
27154
27155 Returns a THROW_EXPR representing the throw-expression. */
27156
27157 static tree
27158 cp_parser_throw_expression (cp_parser* parser)
27159 {
27160 tree expression;
27161 cp_token* token;
27162 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27163
27164 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
27165 token = cp_lexer_peek_token (parser->lexer);
27166 /* Figure out whether or not there is an assignment-expression
27167 following the "throw" keyword. */
27168 if (token->type == CPP_COMMA
27169 || token->type == CPP_SEMICOLON
27170 || token->type == CPP_CLOSE_PAREN
27171 || token->type == CPP_CLOSE_SQUARE
27172 || token->type == CPP_CLOSE_BRACE
27173 || token->type == CPP_COLON)
27174 expression = NULL_TREE;
27175 else
27176 expression = cp_parser_assignment_expression (parser);
27177
27178 /* Construct a location e.g.:
27179 throw x
27180 ^~~~~~~
27181 with caret == start at the start of the "throw" token, and
27182 the end at the end of the final token we consumed. */
27183 location_t combined_loc = make_location (start_loc, start_loc,
27184 parser->lexer);
27185 expression = build_throw (combined_loc, expression);
27186
27187 return expression;
27188 }
27189
27190 /* Parse a yield-expression.
27191
27192 yield-expression:
27193 co_yield assignment-expression
27194 co_yield braced-init-list
27195
27196 Returns a CO_YIELD_EXPR representing the yield-expression. */
27197
27198 static tree
27199 cp_parser_yield_expression (cp_parser* parser)
27200 {
27201 tree expr;
27202
27203 cp_token *token = cp_lexer_peek_token (parser->lexer);
27204 location_t kw_loc = token->location; /* Save for later. */
27205
27206 cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
27207
27208 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27209 {
27210 bool expr_non_constant_p;
27211 cp_lexer_set_source_position (parser->lexer);
27212 /* ??? : probably a moot point? */
27213 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27214 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
27215 }
27216 else
27217 expr = cp_parser_assignment_expression (parser);
27218
27219 if (expr == error_mark_node)
27220 return expr;
27221
27222 return finish_co_yield_expr (kw_loc, expr);
27223 }
27224
27225 /* GNU Extensions */
27226
27227 /* Parse an (optional) asm-specification.
27228
27229 asm-specification:
27230 asm ( string-literal )
27231
27232 If the asm-specification is present, returns a STRING_CST
27233 corresponding to the string-literal. Otherwise, returns
27234 NULL_TREE. */
27235
27236 static tree
27237 cp_parser_asm_specification_opt (cp_parser* parser)
27238 {
27239 cp_token *token;
27240 tree asm_specification;
27241
27242 /* Peek at the next token. */
27243 token = cp_lexer_peek_token (parser->lexer);
27244 /* If the next token isn't the `asm' keyword, then there's no
27245 asm-specification. */
27246 if (!cp_parser_is_keyword (token, RID_ASM))
27247 return NULL_TREE;
27248
27249 /* Consume the `asm' token. */
27250 cp_lexer_consume_token (parser->lexer);
27251 /* Look for the `('. */
27252 matching_parens parens;
27253 parens.require_open (parser);
27254
27255 /* Look for the string-literal. */
27256 asm_specification = cp_parser_string_literal (parser, false, false);
27257
27258 /* Look for the `)'. */
27259 parens.require_close (parser);
27260
27261 return asm_specification;
27262 }
27263
27264 /* Parse an asm-operand-list.
27265
27266 asm-operand-list:
27267 asm-operand
27268 asm-operand-list , asm-operand
27269
27270 asm-operand:
27271 string-literal ( expression )
27272 [ string-literal ] string-literal ( expression )
27273
27274 Returns a TREE_LIST representing the operands. The TREE_VALUE of
27275 each node is the expression. The TREE_PURPOSE is itself a
27276 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
27277 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
27278 is a STRING_CST for the string literal before the parenthesis. Returns
27279 ERROR_MARK_NODE if any of the operands are invalid. */
27280
27281 static tree
27282 cp_parser_asm_operand_list (cp_parser* parser)
27283 {
27284 tree asm_operands = NULL_TREE;
27285 bool invalid_operands = false;
27286
27287 while (true)
27288 {
27289 tree string_literal;
27290 tree expression;
27291 tree name;
27292
27293 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27294 {
27295 /* Consume the `[' token. */
27296 cp_lexer_consume_token (parser->lexer);
27297 /* Read the operand name. */
27298 name = cp_parser_identifier (parser);
27299 if (name != error_mark_node)
27300 name = build_string (IDENTIFIER_LENGTH (name),
27301 IDENTIFIER_POINTER (name));
27302 /* Look for the closing `]'. */
27303 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27304 }
27305 else
27306 name = NULL_TREE;
27307 /* Look for the string-literal. */
27308 string_literal = cp_parser_string_literal (parser, false, false);
27309
27310 /* Look for the `('. */
27311 matching_parens parens;
27312 parens.require_open (parser);
27313 /* Parse the expression. */
27314 expression = cp_parser_expression (parser);
27315 /* Look for the `)'. */
27316 parens.require_close (parser);
27317
27318 if (name == error_mark_node
27319 || string_literal == error_mark_node
27320 || expression == error_mark_node)
27321 invalid_operands = true;
27322
27323 /* Add this operand to the list. */
27324 asm_operands = tree_cons (build_tree_list (name, string_literal),
27325 expression,
27326 asm_operands);
27327 /* If the next token is not a `,', there are no more
27328 operands. */
27329 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27330 break;
27331 /* Consume the `,'. */
27332 cp_lexer_consume_token (parser->lexer);
27333 }
27334
27335 return invalid_operands ? error_mark_node : nreverse (asm_operands);
27336 }
27337
27338 /* Parse an asm-clobber-list.
27339
27340 asm-clobber-list:
27341 string-literal
27342 asm-clobber-list , string-literal
27343
27344 Returns a TREE_LIST, indicating the clobbers in the order that they
27345 appeared. The TREE_VALUE of each node is a STRING_CST. */
27346
27347 static tree
27348 cp_parser_asm_clobber_list (cp_parser* parser)
27349 {
27350 tree clobbers = NULL_TREE;
27351
27352 while (true)
27353 {
27354 tree string_literal;
27355
27356 /* Look for the string literal. */
27357 string_literal = cp_parser_string_literal (parser, false, false);
27358 /* Add it to the list. */
27359 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
27360 /* If the next token is not a `,', then the list is
27361 complete. */
27362 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27363 break;
27364 /* Consume the `,' token. */
27365 cp_lexer_consume_token (parser->lexer);
27366 }
27367
27368 return clobbers;
27369 }
27370
27371 /* Parse an asm-label-list.
27372
27373 asm-label-list:
27374 identifier
27375 asm-label-list , identifier
27376
27377 Returns a TREE_LIST, indicating the labels in the order that they
27378 appeared. The TREE_VALUE of each node is a label. */
27379
27380 static tree
27381 cp_parser_asm_label_list (cp_parser* parser)
27382 {
27383 tree labels = NULL_TREE;
27384
27385 while (true)
27386 {
27387 tree identifier, label, name;
27388
27389 /* Look for the identifier. */
27390 identifier = cp_parser_identifier (parser);
27391 if (!error_operand_p (identifier))
27392 {
27393 label = lookup_label (identifier);
27394 if (TREE_CODE (label) == LABEL_DECL)
27395 {
27396 TREE_USED (label) = 1;
27397 check_goto (label);
27398 name = build_string (IDENTIFIER_LENGTH (identifier),
27399 IDENTIFIER_POINTER (identifier));
27400 labels = tree_cons (name, label, labels);
27401 }
27402 }
27403 /* If the next token is not a `,', then the list is
27404 complete. */
27405 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27406 break;
27407 /* Consume the `,' token. */
27408 cp_lexer_consume_token (parser->lexer);
27409 }
27410
27411 return nreverse (labels);
27412 }
27413
27414 /* Return TRUE iff the next tokens in the stream are possibly the
27415 beginning of a GNU extension attribute. */
27416
27417 static bool
27418 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
27419 {
27420 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
27421 }
27422
27423 /* Return TRUE iff the next tokens in the stream are possibly the
27424 beginning of a standard C++-11 attribute specifier. */
27425
27426 static bool
27427 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
27428 {
27429 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
27430 }
27431
27432 /* Return TRUE iff the next Nth tokens in the stream are possibly the
27433 beginning of a standard C++-11 attribute specifier. */
27434
27435 static bool
27436 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
27437 {
27438 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
27439
27440 return (cxx_dialect >= cxx11
27441 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
27442 || (token->type == CPP_OPEN_SQUARE
27443 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
27444 && token->type == CPP_OPEN_SQUARE)));
27445 }
27446
27447 /* Return TRUE iff the next Nth tokens in the stream are possibly the
27448 beginning of a GNU extension attribute. */
27449
27450 static bool
27451 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
27452 {
27453 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
27454
27455 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
27456 }
27457
27458 /* Return true iff the next tokens can be the beginning of either a
27459 GNU attribute list, or a standard C++11 attribute sequence. */
27460
27461 static bool
27462 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
27463 {
27464 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
27465 || cp_next_tokens_can_be_std_attribute_p (parser));
27466 }
27467
27468 /* Return true iff the next Nth tokens can be the beginning of either
27469 a GNU attribute list, or a standard C++11 attribute sequence. */
27470
27471 static bool
27472 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
27473 {
27474 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
27475 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
27476 }
27477
27478 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
27479 of GNU attributes, or return NULL. */
27480
27481 static tree
27482 cp_parser_attributes_opt (cp_parser *parser)
27483 {
27484 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
27485 return cp_parser_gnu_attributes_opt (parser);
27486 return cp_parser_std_attribute_spec_seq (parser);
27487 }
27488
27489 /* Parse an (optional) series of attributes.
27490
27491 attributes:
27492 attributes attribute
27493
27494 attribute:
27495 __attribute__ (( attribute-list [opt] ))
27496
27497 The return value is as for cp_parser_gnu_attribute_list. */
27498
27499 static tree
27500 cp_parser_gnu_attributes_opt (cp_parser* parser)
27501 {
27502 tree attributes = NULL_TREE;
27503
27504 auto cleanup = make_temp_override
27505 (parser->auto_is_implicit_function_template_parm_p, false);
27506
27507 while (true)
27508 {
27509 cp_token *token;
27510 tree attribute_list;
27511 bool ok = true;
27512
27513 /* Peek at the next token. */
27514 token = cp_lexer_peek_token (parser->lexer);
27515 /* If it's not `__attribute__', then we're done. */
27516 if (token->keyword != RID_ATTRIBUTE)
27517 break;
27518
27519 /* Consume the `__attribute__' keyword. */
27520 cp_lexer_consume_token (parser->lexer);
27521 /* Look for the two `(' tokens. */
27522 matching_parens outer_parens;
27523 if (!outer_parens.require_open (parser))
27524 ok = false;
27525 matching_parens inner_parens;
27526 if (!inner_parens.require_open (parser))
27527 ok = false;
27528
27529 /* Peek at the next token. */
27530 token = cp_lexer_peek_token (parser->lexer);
27531 if (token->type != CPP_CLOSE_PAREN)
27532 /* Parse the attribute-list. */
27533 attribute_list = cp_parser_gnu_attribute_list (parser);
27534 else
27535 /* If the next token is a `)', then there is no attribute
27536 list. */
27537 attribute_list = NULL;
27538
27539 /* Look for the two `)' tokens. */
27540 if (!inner_parens.require_close (parser))
27541 ok = false;
27542 if (!outer_parens.require_close (parser))
27543 ok = false;
27544 if (!ok)
27545 cp_parser_skip_to_end_of_statement (parser);
27546
27547 /* Add these new attributes to the list. */
27548 attributes = attr_chainon (attributes, attribute_list);
27549 }
27550
27551 return attributes;
27552 }
27553
27554 /* Parse a GNU attribute-list.
27555
27556 attribute-list:
27557 attribute
27558 attribute-list , attribute
27559
27560 attribute:
27561 identifier
27562 identifier ( identifier )
27563 identifier ( identifier , expression-list )
27564 identifier ( expression-list )
27565
27566 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
27567 to an attribute. The TREE_PURPOSE of each node is the identifier
27568 indicating which attribute is in use. The TREE_VALUE represents
27569 the arguments, if any. */
27570
27571 static tree
27572 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
27573 {
27574 tree attribute_list = NULL_TREE;
27575 bool save_translate_strings_p = parser->translate_strings_p;
27576
27577 /* Don't create wrapper nodes within attributes: the
27578 handlers don't know how to handle them. */
27579 auto_suppress_location_wrappers sentinel;
27580
27581 parser->translate_strings_p = false;
27582 while (true)
27583 {
27584 cp_token *token;
27585 tree identifier;
27586 tree attribute;
27587
27588 /* Look for the identifier. We also allow keywords here; for
27589 example `__attribute__ ((const))' is legal. */
27590 token = cp_lexer_peek_token (parser->lexer);
27591 if (token->type == CPP_NAME
27592 || token->type == CPP_KEYWORD)
27593 {
27594 tree arguments = NULL_TREE;
27595
27596 /* Consume the token, but save it since we need it for the
27597 SIMD enabled function parsing. */
27598 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
27599
27600 /* Save away the identifier that indicates which attribute
27601 this is. */
27602 identifier = (token->type == CPP_KEYWORD)
27603 /* For keywords, use the canonical spelling, not the
27604 parsed identifier. */
27605 ? ridpointers[(int) token->keyword]
27606 : id_token->u.value;
27607
27608 identifier = canonicalize_attr_name (identifier);
27609 attribute = build_tree_list (identifier, NULL_TREE);
27610
27611 /* Peek at the next token. */
27612 token = cp_lexer_peek_token (parser->lexer);
27613 /* If it's an `(', then parse the attribute arguments. */
27614 if (token->type == CPP_OPEN_PAREN)
27615 {
27616 vec<tree, va_gc> *vec;
27617 int attr_flag = (attribute_takes_identifier_p (identifier)
27618 ? id_attr : normal_attr);
27619 vec = cp_parser_parenthesized_expression_list
27620 (parser, attr_flag, /*cast_p=*/false,
27621 /*allow_expansion_p=*/false,
27622 /*non_constant_p=*/NULL);
27623 if (vec == NULL)
27624 arguments = error_mark_node;
27625 else
27626 {
27627 arguments = build_tree_list_vec (vec);
27628 release_tree_vector (vec);
27629 }
27630 /* Save the arguments away. */
27631 TREE_VALUE (attribute) = arguments;
27632 }
27633
27634 if (arguments != error_mark_node)
27635 {
27636 /* Add this attribute to the list. */
27637 TREE_CHAIN (attribute) = attribute_list;
27638 attribute_list = attribute;
27639 }
27640
27641 token = cp_lexer_peek_token (parser->lexer);
27642 }
27643 /* Unless EXACTLY_ONE is set look for more attributes.
27644 If the next token isn't a `,', we're done. */
27645 if (exactly_one || token->type != CPP_COMMA)
27646 break;
27647
27648 /* Consume the comma and keep going. */
27649 cp_lexer_consume_token (parser->lexer);
27650 }
27651 parser->translate_strings_p = save_translate_strings_p;
27652
27653 /* We built up the list in reverse order. */
27654 return nreverse (attribute_list);
27655 }
27656
27657 /* Parse a standard C++11 attribute.
27658
27659 The returned representation is a TREE_LIST which TREE_PURPOSE is
27660 the scoped name of the attribute, and the TREE_VALUE is its
27661 arguments list.
27662
27663 Note that the scoped name of the attribute is itself a TREE_LIST
27664 which TREE_PURPOSE is the namespace of the attribute, and
27665 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
27666 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
27667 and which TREE_PURPOSE is directly the attribute name.
27668
27669 Clients of the attribute code should use get_attribute_namespace
27670 and get_attribute_name to get the actual namespace and name of
27671 attributes, regardless of their being GNU or C++11 attributes.
27672
27673 attribute:
27674 attribute-token attribute-argument-clause [opt]
27675
27676 attribute-token:
27677 identifier
27678 attribute-scoped-token
27679
27680 attribute-scoped-token:
27681 attribute-namespace :: identifier
27682
27683 attribute-namespace:
27684 identifier
27685
27686 attribute-argument-clause:
27687 ( balanced-token-seq )
27688
27689 balanced-token-seq:
27690 balanced-token [opt]
27691 balanced-token-seq balanced-token
27692
27693 balanced-token:
27694 ( balanced-token-seq )
27695 [ balanced-token-seq ]
27696 { balanced-token-seq }. */
27697
27698 static tree
27699 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
27700 {
27701 tree attribute, attr_id = NULL_TREE, arguments;
27702 cp_token *token;
27703
27704 auto cleanup = make_temp_override
27705 (parser->auto_is_implicit_function_template_parm_p, false);
27706
27707 /* First, parse name of the attribute, a.k.a attribute-token. */
27708
27709 token = cp_lexer_peek_token (parser->lexer);
27710 if (token->type == CPP_NAME)
27711 attr_id = token->u.value;
27712 else if (token->type == CPP_KEYWORD)
27713 attr_id = ridpointers[(int) token->keyword];
27714 else if (token->flags & NAMED_OP)
27715 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
27716
27717 if (attr_id == NULL_TREE)
27718 return NULL_TREE;
27719
27720 cp_lexer_consume_token (parser->lexer);
27721
27722 token = cp_lexer_peek_token (parser->lexer);
27723 if (token->type == CPP_SCOPE)
27724 {
27725 /* We are seeing a scoped attribute token. */
27726
27727 cp_lexer_consume_token (parser->lexer);
27728 if (attr_ns)
27729 error_at (token->location, "attribute using prefix used together "
27730 "with scoped attribute token");
27731 attr_ns = attr_id;
27732
27733 token = cp_lexer_peek_token (parser->lexer);
27734 if (token->type == CPP_NAME)
27735 attr_id = token->u.value;
27736 else if (token->type == CPP_KEYWORD)
27737 attr_id = ridpointers[(int) token->keyword];
27738 else if (token->flags & NAMED_OP)
27739 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
27740 else
27741 {
27742 error_at (token->location,
27743 "expected an identifier for the attribute name");
27744 return error_mark_node;
27745 }
27746 cp_lexer_consume_token (parser->lexer);
27747
27748 attr_ns = canonicalize_attr_name (attr_ns);
27749 attr_id = canonicalize_attr_name (attr_id);
27750 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
27751 NULL_TREE);
27752 token = cp_lexer_peek_token (parser->lexer);
27753 }
27754 else if (attr_ns)
27755 {
27756 attr_ns = canonicalize_attr_name (attr_ns);
27757 attr_id = canonicalize_attr_name (attr_id);
27758 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
27759 NULL_TREE);
27760 }
27761 else
27762 {
27763 attr_id = canonicalize_attr_name (attr_id);
27764 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
27765 NULL_TREE);
27766 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
27767 but no longer: we have to be able to tell [[noreturn]] and
27768 __attribute__((noreturn)) apart. */
27769 /* C++14 deprecated attribute is equivalent to GNU's. */
27770 if (is_attribute_p ("deprecated", attr_id))
27771 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
27772 /* C++17 fallthrough attribute is equivalent to GNU's. */
27773 else if (is_attribute_p ("fallthrough", attr_id))
27774 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
27775 /* Transactional Memory TS optimize_for_synchronized attribute is
27776 equivalent to GNU transaction_callable. */
27777 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
27778 TREE_PURPOSE (attribute)
27779 = get_identifier ("transaction_callable");
27780 /* Transactional Memory attributes are GNU attributes. */
27781 else if (tm_attr_to_mask (attr_id))
27782 TREE_PURPOSE (attribute) = attr_id;
27783 }
27784
27785 /* Now parse the optional argument clause of the attribute. */
27786
27787 if (token->type != CPP_OPEN_PAREN)
27788 return attribute;
27789
27790 {
27791 vec<tree, va_gc> *vec;
27792 int attr_flag = normal_attr;
27793
27794 /* Maybe we don't expect to see any arguments for this attribute. */
27795 const attribute_spec *as
27796 = lookup_attribute_spec (TREE_PURPOSE (attribute));
27797 if (as && as->max_length == 0)
27798 {
27799 error_at (token->location, "%qE attribute does not take any arguments",
27800 attr_id);
27801 cp_parser_skip_to_closing_parenthesis (parser,
27802 /*recovering=*/true,
27803 /*or_comma=*/false,
27804 /*consume_paren=*/true);
27805 return error_mark_node;
27806 }
27807
27808 if (attr_ns == gnu_identifier
27809 && attribute_takes_identifier_p (attr_id))
27810 /* A GNU attribute that takes an identifier in parameter. */
27811 attr_flag = id_attr;
27812
27813 if (as == NULL)
27814 {
27815 /* For unknown attributes, just skip balanced tokens instead of
27816 trying to parse the arguments. */
27817 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
27818 cp_lexer_consume_token (parser->lexer);
27819 return attribute;
27820 }
27821
27822 vec = cp_parser_parenthesized_expression_list
27823 (parser, attr_flag, /*cast_p=*/false,
27824 /*allow_expansion_p=*/true,
27825 /*non_constant_p=*/NULL);
27826 if (vec == NULL)
27827 arguments = error_mark_node;
27828 else
27829 {
27830 if (vec->is_empty ())
27831 /* e.g. [[attr()]]. */
27832 error_at (token->location, "parentheses must be omitted if "
27833 "%qE attribute argument list is empty",
27834 attr_id);
27835 arguments = build_tree_list_vec (vec);
27836 release_tree_vector (vec);
27837 }
27838
27839 if (arguments == error_mark_node)
27840 attribute = error_mark_node;
27841 else
27842 TREE_VALUE (attribute) = arguments;
27843 }
27844
27845 return attribute;
27846 }
27847
27848 /* Warn if the attribute ATTRIBUTE appears more than once in the
27849 attribute-list ATTRIBUTES. This used to be enforced for certain
27850 attributes, but the restriction was removed in P2156. Note that
27851 carries_dependency ([dcl.attr.depend]) isn't implemented yet in GCC.
27852 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
27853 found in ATTRIBUTES. */
27854
27855 static bool
27856 cp_parser_check_std_attribute (location_t loc, tree attributes, tree attribute)
27857 {
27858 static auto alist = { "noreturn", "deprecated", "nodiscard", "maybe_unused",
27859 "likely", "unlikely", "fallthrough",
27860 "no_unique_address" };
27861 if (attributes)
27862 for (const auto &a : alist)
27863 if (is_attribute_p (a, get_attribute_name (attribute))
27864 && lookup_attribute (a, attributes))
27865 {
27866 if (!from_macro_expansion_at (loc))
27867 warning_at (loc, OPT_Wattributes, "attribute %qs specified "
27868 "multiple times", a);
27869 return false;
27870 }
27871 return true;
27872 }
27873
27874 /* Parse a list of standard C++-11 attributes.
27875
27876 attribute-list:
27877 attribute [opt]
27878 attribute-list , attribute[opt]
27879 attribute ...
27880 attribute-list , attribute ...
27881 */
27882
27883 static tree
27884 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
27885 {
27886 tree attributes = NULL_TREE, attribute = NULL_TREE;
27887 cp_token *token = NULL;
27888
27889 while (true)
27890 {
27891 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27892 attribute = cp_parser_std_attribute (parser, attr_ns);
27893 if (attribute == error_mark_node)
27894 break;
27895 if (attribute != NULL_TREE)
27896 {
27897 if (cp_parser_check_std_attribute (loc, attributes, attribute))
27898 {
27899 TREE_CHAIN (attribute) = attributes;
27900 attributes = attribute;
27901 }
27902 }
27903 token = cp_lexer_peek_token (parser->lexer);
27904 if (token->type == CPP_ELLIPSIS)
27905 {
27906 cp_lexer_consume_token (parser->lexer);
27907 if (attribute == NULL_TREE)
27908 error_at (token->location,
27909 "expected attribute before %<...%>");
27910 else
27911 {
27912 tree pack = make_pack_expansion (TREE_VALUE (attribute));
27913 if (pack == error_mark_node)
27914 return error_mark_node;
27915 TREE_VALUE (attribute) = pack;
27916 }
27917 token = cp_lexer_peek_token (parser->lexer);
27918 }
27919 if (token->type != CPP_COMMA)
27920 break;
27921 cp_lexer_consume_token (parser->lexer);
27922 }
27923 attributes = nreverse (attributes);
27924 return attributes;
27925 }
27926
27927 /* Parse a standard C++-11 attribute specifier.
27928
27929 attribute-specifier:
27930 [ [ attribute-using-prefix [opt] attribute-list ] ]
27931 alignment-specifier
27932
27933 attribute-using-prefix:
27934 using attribute-namespace :
27935
27936 alignment-specifier:
27937 alignas ( type-id ... [opt] )
27938 alignas ( alignment-expression ... [opt] ). */
27939
27940 static tree
27941 cp_parser_std_attribute_spec (cp_parser *parser)
27942 {
27943 tree attributes = NULL_TREE;
27944 cp_token *token = cp_lexer_peek_token (parser->lexer);
27945
27946 if (token->type == CPP_OPEN_SQUARE
27947 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
27948 {
27949 tree attr_ns = NULL_TREE;
27950
27951 cp_lexer_consume_token (parser->lexer);
27952 cp_lexer_consume_token (parser->lexer);
27953
27954 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27955 {
27956 token = cp_lexer_peek_nth_token (parser->lexer, 2);
27957 if (token->type == CPP_NAME)
27958 attr_ns = token->u.value;
27959 else if (token->type == CPP_KEYWORD)
27960 attr_ns = ridpointers[(int) token->keyword];
27961 else if (token->flags & NAMED_OP)
27962 attr_ns = get_identifier (cpp_type2name (token->type,
27963 token->flags));
27964 if (attr_ns
27965 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
27966 {
27967 if (cxx_dialect < cxx17)
27968 pedwarn (input_location, 0,
27969 "attribute using prefix only available "
27970 "with %<-std=c++17%> or %<-std=gnu++17%>");
27971
27972 cp_lexer_consume_token (parser->lexer);
27973 cp_lexer_consume_token (parser->lexer);
27974 cp_lexer_consume_token (parser->lexer);
27975 }
27976 else
27977 attr_ns = NULL_TREE;
27978 }
27979
27980 attributes = cp_parser_std_attribute_list (parser, attr_ns);
27981
27982 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
27983 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
27984 cp_parser_skip_to_end_of_statement (parser);
27985 else
27986 /* Warn about parsing c++11 attribute in non-c++11 mode, only
27987 when we are sure that we have actually parsed them. */
27988 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
27989 }
27990 else
27991 {
27992 tree alignas_expr;
27993
27994 /* Look for an alignment-specifier. */
27995
27996 token = cp_lexer_peek_token (parser->lexer);
27997
27998 if (token->type != CPP_KEYWORD
27999 || token->keyword != RID_ALIGNAS)
28000 return NULL_TREE;
28001
28002 cp_lexer_consume_token (parser->lexer);
28003 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
28004
28005 matching_parens parens;
28006 if (!parens.require_open (parser))
28007 return error_mark_node;
28008
28009 cp_parser_parse_tentatively (parser);
28010 alignas_expr = cp_parser_type_id (parser);
28011
28012 if (!cp_parser_parse_definitely (parser))
28013 {
28014 alignas_expr = cp_parser_assignment_expression (parser);
28015 if (alignas_expr == error_mark_node)
28016 cp_parser_skip_to_end_of_statement (parser);
28017 if (alignas_expr == NULL_TREE
28018 || alignas_expr == error_mark_node)
28019 return alignas_expr;
28020 }
28021
28022 alignas_expr = cxx_alignas_expr (alignas_expr);
28023 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
28024
28025 /* Handle alignas (pack...). */
28026 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28027 {
28028 cp_lexer_consume_token (parser->lexer);
28029 alignas_expr = make_pack_expansion (alignas_expr);
28030 }
28031
28032 /* Something went wrong, so don't build the attribute. */
28033 if (alignas_expr == error_mark_node)
28034 return error_mark_node;
28035
28036 /* Missing ')' means the code cannot possibly be valid; go ahead
28037 and commit to make sure we issue a hard error. */
28038 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
28039 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28040 cp_parser_commit_to_tentative_parse (parser);
28041
28042 if (!parens.require_close (parser))
28043 return error_mark_node;
28044
28045 /* Build the C++-11 representation of an 'aligned'
28046 attribute. */
28047 attributes
28048 = build_tree_list (build_tree_list (gnu_identifier,
28049 aligned_identifier), alignas_expr);
28050 }
28051
28052 return attributes;
28053 }
28054
28055 /* Parse a standard C++-11 attribute-specifier-seq.
28056
28057 attribute-specifier-seq:
28058 attribute-specifier-seq [opt] attribute-specifier
28059 */
28060
28061 static tree
28062 cp_parser_std_attribute_spec_seq (cp_parser *parser)
28063 {
28064 tree attr_specs = NULL_TREE;
28065 tree attr_last = NULL_TREE;
28066
28067 /* Don't create wrapper nodes within attributes: the
28068 handlers don't know how to handle them. */
28069 auto_suppress_location_wrappers sentinel;
28070
28071 while (true)
28072 {
28073 tree attr_spec = cp_parser_std_attribute_spec (parser);
28074 if (attr_spec == NULL_TREE)
28075 break;
28076 if (attr_spec == error_mark_node)
28077 return error_mark_node;
28078
28079 if (attr_last)
28080 TREE_CHAIN (attr_last) = attr_spec;
28081 else
28082 attr_specs = attr_last = attr_spec;
28083 attr_last = tree_last (attr_last);
28084 }
28085
28086 return attr_specs;
28087 }
28088
28089 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
28090 return index of the first token after balanced-token, or N on failure. */
28091
28092 static size_t
28093 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
28094 {
28095 size_t orig_n = n;
28096 int nparens = 0, nbraces = 0, nsquares = 0;
28097 do
28098 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
28099 {
28100 case CPP_PRAGMA_EOL:
28101 if (!parser->lexer->in_pragma)
28102 break;
28103 /* FALLTHRU */
28104 case CPP_EOF:
28105 /* Ran out of tokens. */
28106 return orig_n;
28107 case CPP_OPEN_PAREN:
28108 ++nparens;
28109 break;
28110 case CPP_OPEN_BRACE:
28111 ++nbraces;
28112 break;
28113 case CPP_OPEN_SQUARE:
28114 ++nsquares;
28115 break;
28116 case CPP_CLOSE_PAREN:
28117 --nparens;
28118 break;
28119 case CPP_CLOSE_BRACE:
28120 --nbraces;
28121 break;
28122 case CPP_CLOSE_SQUARE:
28123 --nsquares;
28124 break;
28125 default:
28126 break;
28127 }
28128 while (nparens || nbraces || nsquares);
28129 return n;
28130 }
28131
28132 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
28133 return index of the first token after the GNU attribute tokens, or N on
28134 failure. */
28135
28136 static size_t
28137 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
28138 {
28139 while (true)
28140 {
28141 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
28142 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
28143 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
28144 break;
28145
28146 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
28147 if (n2 == n + 2)
28148 break;
28149 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
28150 break;
28151 n = n2 + 1;
28152 }
28153 return n;
28154 }
28155
28156 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
28157 next token), return index of the first token after the standard C++11
28158 attribute tokens, or N on failure. */
28159
28160 static size_t
28161 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
28162 {
28163 while (true)
28164 {
28165 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
28166 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
28167 {
28168 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
28169 if (n2 == n + 1)
28170 break;
28171 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
28172 break;
28173 n = n2 + 1;
28174 }
28175 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
28176 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
28177 {
28178 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
28179 if (n2 == n + 1)
28180 break;
28181 n = n2;
28182 }
28183 else
28184 break;
28185 }
28186 return n;
28187 }
28188
28189 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
28190 as the next token), return index of the first token after the attribute
28191 tokens, or N on failure. */
28192
28193 static size_t
28194 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
28195 {
28196 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
28197 return cp_parser_skip_gnu_attributes_opt (parser, n);
28198 return cp_parser_skip_std_attribute_spec_seq (parser, n);
28199 }
28200
28201 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
28202 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
28203 current value of the PEDANTIC flag, regardless of whether or not
28204 the `__extension__' keyword is present. The caller is responsible
28205 for restoring the value of the PEDANTIC flag. */
28206
28207 static bool
28208 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
28209 {
28210 /* Save the old value of the PEDANTIC flag. */
28211 *saved_pedantic = pedantic;
28212
28213 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
28214 {
28215 /* Consume the `__extension__' token. */
28216 cp_lexer_consume_token (parser->lexer);
28217 /* We're not being pedantic while the `__extension__' keyword is
28218 in effect. */
28219 pedantic = 0;
28220
28221 return true;
28222 }
28223
28224 return false;
28225 }
28226
28227 /* Parse a label declaration.
28228
28229 label-declaration:
28230 __label__ label-declarator-seq ;
28231
28232 label-declarator-seq:
28233 identifier , label-declarator-seq
28234 identifier */
28235
28236 static void
28237 cp_parser_label_declaration (cp_parser* parser)
28238 {
28239 /* Look for the `__label__' keyword. */
28240 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
28241
28242 while (true)
28243 {
28244 tree identifier;
28245
28246 /* Look for an identifier. */
28247 identifier = cp_parser_identifier (parser);
28248 /* If we failed, stop. */
28249 if (identifier == error_mark_node)
28250 break;
28251 /* Declare it as a label. */
28252 finish_label_decl (identifier);
28253 /* If the next token is a `;', stop. */
28254 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28255 break;
28256 /* Look for the `,' separating the label declarations. */
28257 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
28258 }
28259
28260 /* Look for the final `;'. */
28261 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28262 }
28263
28264 // -------------------------------------------------------------------------- //
28265 // Concept definitions
28266
28267 static tree
28268 cp_parser_concept_definition (cp_parser *parser)
28269 {
28270 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
28271 cp_lexer_consume_token (parser->lexer);
28272
28273 cp_expr id = cp_parser_identifier (parser);
28274 if (id == error_mark_node)
28275 {
28276 cp_parser_skip_to_end_of_statement (parser);
28277 cp_parser_consume_semicolon_at_end_of_statement (parser);
28278 return NULL_TREE;
28279 }
28280
28281 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28282 {
28283 cp_parser_skip_to_end_of_statement (parser);
28284 cp_parser_consume_semicolon_at_end_of_statement (parser);
28285 return error_mark_node;
28286 }
28287
28288 processing_constraint_expression_sentinel parsing_constraint;
28289 tree init = cp_parser_constraint_expression (parser);
28290 if (init == error_mark_node)
28291 cp_parser_skip_to_end_of_statement (parser);
28292
28293 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
28294 but continue as if it were. */
28295 cp_parser_consume_semicolon_at_end_of_statement (parser);
28296
28297 return finish_concept_definition (id, init);
28298 }
28299
28300 // -------------------------------------------------------------------------- //
28301 // Requires Clause
28302
28303 /* Diagnose an expression that should appear in ()'s within a requires-clause
28304 and suggest where to place those parentheses. */
28305
28306 static void
28307 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
28308 {
28309 error_at (loc, "expression must be enclosed in parentheses");
28310 }
28311
28312 static void
28313 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
28314 {
28315 gcc_rich_location richloc (loc);
28316 richloc.add_fixit_insert_before ("(");
28317 richloc.add_fixit_insert_after (")");
28318 error_at (&richloc, "expression must be enclosed in parentheses");
28319 }
28320
28321 /* Characterizes the likely kind of expression intended by a mis-written
28322 primary constraint. */
28323 enum primary_constraint_error
28324 {
28325 pce_ok,
28326 pce_maybe_operator,
28327 pce_maybe_postfix
28328 };
28329
28330 /* Returns true if the token(s) following a primary-expression in a
28331 constraint-logical-* expression would require parentheses. */
28332
28333 static primary_constraint_error
28334 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
28335 {
28336 cp_token *token = cp_lexer_peek_token (parser->lexer);
28337 switch (token->type)
28338 {
28339 default:
28340 return pce_ok;
28341
28342 case CPP_EQ:
28343 {
28344 /* An equal sign may be part of the definition of a function,
28345 and not an assignment operator, when parsing the expression
28346 for a trailing requires-clause. For example:
28347
28348 template<typename T>
28349 struct S {
28350 S() requires C<T> = default;
28351 };
28352
28353 Don't try to reparse this a binary operator. */
28354 if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
28355 || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
28356 return pce_ok;
28357
28358 gcc_fallthrough ();
28359 }
28360
28361 /* Arithmetic operators. */
28362 case CPP_PLUS:
28363 case CPP_MINUS:
28364 case CPP_MULT:
28365 case CPP_DIV:
28366 case CPP_MOD:
28367 /* Bitwise operators. */
28368 case CPP_AND:
28369 case CPP_OR:
28370 case CPP_XOR:
28371 case CPP_RSHIFT:
28372 case CPP_LSHIFT:
28373 /* Relational operators. */
28374 case CPP_EQ_EQ:
28375 case CPP_NOT_EQ:
28376 case CPP_LESS:
28377 case CPP_GREATER:
28378 case CPP_LESS_EQ:
28379 case CPP_GREATER_EQ:
28380 case CPP_SPACESHIP:
28381 /* Pointer-to-member. */
28382 case CPP_DOT_STAR:
28383 case CPP_DEREF_STAR:
28384 /* Assignment operators. */
28385 case CPP_PLUS_EQ:
28386 case CPP_MINUS_EQ:
28387 case CPP_MULT_EQ:
28388 case CPP_DIV_EQ:
28389 case CPP_MOD_EQ:
28390 case CPP_AND_EQ:
28391 case CPP_OR_EQ:
28392 case CPP_XOR_EQ:
28393 case CPP_RSHIFT_EQ:
28394 case CPP_LSHIFT_EQ:
28395 /* Conditional operator */
28396 case CPP_QUERY:
28397 /* Unenclosed binary or conditional operator. */
28398 return pce_maybe_operator;
28399
28400 case CPP_OPEN_PAREN:
28401 {
28402 /* A primary constraint that precedes the parameter-list of a
28403 lambda expression is followed by an open paren.
28404
28405 []<typename T> requires C (T a, T b) { ... }
28406
28407 Don't try to re-parse this as a postfix expression. */
28408 if (lambda_p)
28409 return pce_ok;
28410
28411 gcc_fallthrough ();
28412 }
28413 case CPP_OPEN_SQUARE:
28414 {
28415 /* A primary-constraint-expression followed by a '[[' is not a
28416 postfix expression. */
28417 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
28418 return pce_ok;
28419
28420 gcc_fallthrough ();
28421 }
28422 case CPP_PLUS_PLUS:
28423 case CPP_MINUS_MINUS:
28424 case CPP_DOT:
28425 case CPP_DEREF:
28426 /* Unenclosed postfix operator. */
28427 return pce_maybe_postfix;
28428 }
28429 }
28430
28431 /* Returns true if the next token begins a unary expression, preceded by
28432 an operator or keyword. */
28433
28434 static bool
28435 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
28436 {
28437 cp_token *token = cp_lexer_peek_token (parser->lexer);
28438 switch (token->type)
28439 {
28440 case CPP_NOT:
28441 case CPP_PLUS:
28442 case CPP_MINUS:
28443 case CPP_MULT:
28444 case CPP_COMPL:
28445 case CPP_PLUS_PLUS:
28446 case CPP_MINUS_MINUS:
28447 return true;
28448
28449 case CPP_KEYWORD:
28450 {
28451 switch (token->keyword)
28452 {
28453 case RID_STATCAST:
28454 case RID_DYNCAST:
28455 case RID_REINTCAST:
28456 case RID_CONSTCAST:
28457 case RID_TYPEID:
28458 case RID_SIZEOF:
28459 case RID_ALIGNOF:
28460 case RID_NOEXCEPT:
28461 case RID_NEW:
28462 case RID_DELETE:
28463 case RID_THROW:
28464 return true;
28465
28466 default:
28467 break;
28468 }
28469 }
28470
28471 default:
28472 break;
28473 }
28474
28475 return false;
28476 }
28477
28478 /* Parse a primary expression within a constraint. */
28479
28480 static cp_expr
28481 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
28482 {
28483 /* If this looks like a unary expression, parse it as such, but diagnose
28484 it as ill-formed; it requires parens. */
28485 if (cp_parser_unary_constraint_requires_parens (parser))
28486 {
28487 cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
28488 cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
28489 return e;
28490 }
28491
28492 cp_lexer_save_tokens (parser->lexer);
28493 cp_id_kind idk;
28494 location_t loc = input_location;
28495 cp_expr expr = cp_parser_primary_expression (parser,
28496 /*address_p=*/false,
28497 /*cast_p=*/false,
28498 /*template_arg_p=*/false,
28499 &idk);
28500 expr.maybe_add_location_wrapper ();
28501
28502 primary_constraint_error pce = pce_ok;
28503 if (expr != error_mark_node)
28504 {
28505 /* The primary-expression could be part of an unenclosed non-logical
28506 compound expression. */
28507 pce = cp_parser_constraint_requires_parens (parser, lambda_p);
28508 }
28509 if (pce == pce_ok)
28510 {
28511 cp_lexer_commit_tokens (parser->lexer);
28512 return finish_constraint_primary_expr (expr);
28513 }
28514
28515 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
28516 error, but return the expression as if it were valid. */
28517 cp_lexer_rollback_tokens (parser->lexer);
28518 cp_parser_parse_tentatively (parser);
28519 if (pce == pce_maybe_operator)
28520 expr = cp_parser_assignment_expression (parser, NULL, false, false);
28521 else
28522 expr = cp_parser_simple_cast_expression (parser);
28523 if (cp_parser_parse_definitely (parser))
28524 {
28525 cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
28526 return expr;
28527 }
28528
28529 /* Otherwise, something has gone very wrong, and we can't generate a more
28530 meaningful diagnostic or recover. */
28531 cp_parser_diagnose_ungrouped_constraint_plain (loc);
28532 return error_mark_node;
28533 }
28534
28535 /* Parse a constraint-logical-and-expression.
28536
28537 constraint-logical-and-expression:
28538 primary-expression
28539 constraint-logical-and-expression '&&' primary-expression */
28540
28541 static cp_expr
28542 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
28543 {
28544 cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
28545 while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
28546 {
28547 cp_token *op = cp_lexer_consume_token (parser->lexer);
28548 tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
28549 lhs = finish_constraint_and_expr (op->location, lhs, rhs);
28550 }
28551 return lhs;
28552 }
28553
28554 /* Parse a constraint-logical-or-expression.
28555
28556 constraint-logical-or-expression:
28557 constraint-logical-and-expression
28558 constraint-logical-or-expression '||' constraint-logical-and-expression */
28559
28560 static cp_expr
28561 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
28562 {
28563 cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
28564 while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
28565 {
28566 cp_token *op = cp_lexer_consume_token (parser->lexer);
28567 cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
28568 lhs = finish_constraint_or_expr (op->location, lhs, rhs);
28569 }
28570 return lhs;
28571 }
28572
28573 /* Parse the expression after a requires-clause. This has a different grammar
28574 than that in the concepts TS. */
28575
28576 static tree
28577 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
28578 {
28579 processing_constraint_expression_sentinel parsing_constraint;
28580 temp_override<int> ovr (processing_template_decl);
28581 if (!processing_template_decl)
28582 /* Adjust processing_template_decl so that we always obtain template
28583 trees here. We don't do the usual ++processing_template_decl
28584 because that would skew the template parameter depth of a lambda
28585 within if we're already inside a template. */
28586 processing_template_decl = 1;
28587 cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
28588 if (check_for_bare_parameter_packs (expr))
28589 expr = error_mark_node;
28590 return expr;
28591 }
28592
28593 /* Parse a expression after a requires clause.
28594
28595 constraint-expression:
28596 logical-or-expression
28597
28598 The required logical-or-expression must be a constant expression. Note
28599 that we don't check that the expression is constepxr here. We defer until
28600 we analyze constraints and then, we only check atomic constraints. */
28601
28602 static tree
28603 cp_parser_constraint_expression (cp_parser *parser)
28604 {
28605 processing_constraint_expression_sentinel parsing_constraint;
28606 temp_override<int> ovr (processing_template_decl);
28607 if (!processing_template_decl)
28608 /* As in cp_parser_requires_clause_expression. */
28609 processing_template_decl = 1;
28610 cp_expr expr = cp_parser_binary_expression (parser, false, true,
28611 PREC_NOT_OPERATOR, NULL);
28612 if (check_for_bare_parameter_packs (expr))
28613 expr = error_mark_node;
28614 expr.maybe_add_location_wrapper ();
28615 return expr;
28616 }
28617
28618 /* Optionally parse a requires clause:
28619
28620 requires-clause:
28621 `requires` constraint-logical-or-expression.
28622 [ConceptsTS]
28623 `requires constraint-expression.
28624
28625 LAMBDA_P is true when the requires-clause is parsed before the
28626 parameter-list of a lambda-declarator. */
28627
28628 static tree
28629 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
28630 {
28631 cp_token *tok = cp_lexer_peek_token (parser->lexer);
28632 if (tok->keyword != RID_REQUIRES)
28633 {
28634 if (!flag_concepts && tok->type == CPP_NAME
28635 && tok->u.value == ridpointers[RID_REQUIRES])
28636 {
28637 error_at (cp_lexer_peek_token (parser->lexer)->location,
28638 "%<requires%> only available with "
28639 "%<-std=c++20%> or %<-fconcepts%>");
28640 /* Parse and discard the requires-clause. */
28641 cp_lexer_consume_token (parser->lexer);
28642 cp_parser_constraint_expression (parser);
28643 }
28644 return NULL_TREE;
28645 }
28646
28647 cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
28648 if (tok2->type == CPP_OPEN_BRACE)
28649 {
28650 /* An opening brace following the start of a requires-clause is
28651 ill-formed; the user likely forgot the second `requires' that
28652 would start a requires-expression. */
28653 gcc_rich_location richloc (tok2->location);
28654 richloc.add_fixit_insert_after (tok->location, " requires");
28655 error_at (&richloc, "missing additional %<requires%> to start "
28656 "a requires-expression");
28657 /* Don't consume the `requires', so that it's reused as the start of a
28658 requires-expression. */
28659 }
28660 else
28661 cp_lexer_consume_token (parser->lexer);
28662
28663 if (!flag_concepts_ts)
28664 return cp_parser_requires_clause_expression (parser, lambda_p);
28665 else
28666 return cp_parser_constraint_expression (parser);
28667 }
28668
28669 /*---------------------------------------------------------------------------
28670 Requires expressions
28671 ---------------------------------------------------------------------------*/
28672
28673 /* Parse a requires expression
28674
28675 requirement-expression:
28676 'requires' requirement-parameter-list [opt] requirement-body */
28677
28678 static tree
28679 cp_parser_requires_expression (cp_parser *parser)
28680 {
28681 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
28682 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
28683
28684 /* Avoid committing to outer tentative parse. */
28685 tentative_firewall firewall (parser);
28686
28687 /* This is definitely a requires-expression. */
28688 cp_parser_commit_to_tentative_parse (parser);
28689
28690 tree parms, reqs;
28691 {
28692 /* Local parameters are delared as variables within the scope
28693 of the expression. They are not visible past the end of
28694 the expression. Expressions within the requires-expression
28695 are unevaluated. */
28696 struct scope_sentinel
28697 {
28698 scope_sentinel ()
28699 {
28700 ++cp_unevaluated_operand;
28701 begin_scope (sk_block, NULL_TREE);
28702 }
28703
28704 ~scope_sentinel ()
28705 {
28706 pop_bindings_and_leave_scope ();
28707 --cp_unevaluated_operand;
28708 }
28709 } s;
28710
28711 /* Parse the optional parameter list. */
28712 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28713 {
28714 parms = cp_parser_requirement_parameter_list (parser);
28715 if (parms == error_mark_node)
28716 return error_mark_node;
28717 }
28718 else
28719 parms = NULL_TREE;
28720
28721 /* Parse the requirement body. */
28722 temp_override<int> ovr (processing_template_decl);
28723 if (!processing_template_decl)
28724 /* As in cp_parser_requires_clause_expression. */
28725 processing_template_decl = 1;
28726 reqs = cp_parser_requirement_body (parser);
28727 if (reqs == error_mark_node)
28728 return error_mark_node;
28729 }
28730
28731 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
28732 the parm chain. */
28733 grokparms (parms, &parms);
28734 loc = make_location (loc, loc, parser->lexer);
28735 tree expr = finish_requires_expr (loc, parms, reqs);
28736 if (!processing_template_decl)
28737 {
28738 /* Perform semantic processing now to diagnose any invalid types and
28739 expressions. */
28740 int saved_errorcount = errorcount;
28741 tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
28742 if (errorcount > saved_errorcount)
28743 return error_mark_node;
28744 }
28745 return expr;
28746 }
28747
28748 /* Parse a parameterized requirement.
28749
28750 requirement-parameter-list:
28751 '(' parameter-declaration-clause ')' */
28752
28753 static tree
28754 cp_parser_requirement_parameter_list (cp_parser *parser)
28755 {
28756 matching_parens parens;
28757 if (!parens.require_open (parser))
28758 return error_mark_node;
28759
28760 tree parms = (cp_parser_parameter_declaration_clause
28761 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
28762
28763 if (!parens.require_close (parser))
28764 return error_mark_node;
28765
28766 return parms;
28767 }
28768
28769 /* Parse the body of a requirement.
28770
28771 requirement-body:
28772 '{' requirement-list '}' */
28773 static tree
28774 cp_parser_requirement_body (cp_parser *parser)
28775 {
28776 matching_braces braces;
28777 if (!braces.require_open (parser))
28778 return error_mark_node;
28779
28780 tree reqs = cp_parser_requirement_seq (parser);
28781
28782 if (!braces.require_close (parser))
28783 return error_mark_node;
28784
28785 return reqs;
28786 }
28787
28788 /* Parse a sequence of requirements.
28789
28790 requirement-seq:
28791 requirement
28792 requirement-seq requirement */
28793
28794 static tree
28795 cp_parser_requirement_seq (cp_parser *parser)
28796 {
28797 tree result = NULL_TREE;
28798 do
28799 {
28800 tree req = cp_parser_requirement (parser);
28801 if (req != error_mark_node)
28802 result = tree_cons (NULL_TREE, req, result);
28803 } while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE));
28804
28805 /* If there are no valid requirements, this is not a valid expression. */
28806 if (!result)
28807 return error_mark_node;
28808
28809 /* Reverse the order of requirements so they are analyzed in order. */
28810 return nreverse (result);
28811 }
28812
28813 /* Parse a syntactic requirement or type requirement.
28814
28815 requirement:
28816 simple-requirement
28817 compound-requirement
28818 type-requirement
28819 nested-requirement */
28820
28821 static tree
28822 cp_parser_requirement (cp_parser *parser)
28823 {
28824 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28825 return cp_parser_compound_requirement (parser);
28826 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
28827 return cp_parser_type_requirement (parser);
28828 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
28829 return cp_parser_nested_requirement (parser);
28830 else
28831 return cp_parser_simple_requirement (parser);
28832 }
28833
28834 /* Parse a simple requirement.
28835
28836 simple-requirement:
28837 expression ';' */
28838
28839 static tree
28840 cp_parser_simple_requirement (cp_parser *parser)
28841 {
28842 location_t start = cp_lexer_peek_token (parser->lexer)->location;
28843 cp_expr expr = cp_parser_expression (parser, NULL, false, false);
28844 if (expr == error_mark_node)
28845 cp_parser_skip_to_end_of_statement (parser);
28846
28847 cp_parser_consume_semicolon_at_end_of_statement (parser);
28848
28849 if (!expr || expr == error_mark_node)
28850 return error_mark_node;
28851
28852 /* Sometimes we don't get locations, so use the cached token location
28853 as a reasonable approximation. */
28854 if (expr.get_location() == UNKNOWN_LOCATION)
28855 expr.set_location (start);
28856
28857 return finish_simple_requirement (expr.get_location (), expr);
28858 }
28859
28860 /* Parse a type requirement
28861
28862 type-requirement
28863 nested-name-specifier [opt] required-type-name ';'
28864
28865 required-type-name:
28866 type-name
28867 'template' [opt] simple-template-id */
28868
28869 static tree
28870 cp_parser_type_requirement (cp_parser *parser)
28871 {
28872 cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
28873 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28874
28875 // Save the scope before parsing name specifiers.
28876 tree saved_scope = parser->scope;
28877 tree saved_object_scope = parser->object_scope;
28878 tree saved_qualifying_scope = parser->qualifying_scope;
28879 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
28880 cp_parser_nested_name_specifier_opt (parser,
28881 /*typename_keyword_p=*/true,
28882 /*check_dependency_p=*/false,
28883 /*type_p=*/true,
28884 /*is_declaration=*/false);
28885
28886 tree type;
28887 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28888 {
28889 cp_lexer_consume_token (parser->lexer);
28890 type = cp_parser_template_id (parser,
28891 /*template_keyword_p=*/true,
28892 /*check_dependency=*/false,
28893 /*tag_type=*/none_type,
28894 /*is_declaration=*/false);
28895 type = make_typename_type (parser->scope, type, typename_type,
28896 /*complain=*/tf_error);
28897 }
28898 else
28899 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
28900
28901 if (TREE_CODE (type) == TYPE_DECL)
28902 type = TREE_TYPE (type);
28903
28904 parser->scope = saved_scope;
28905 parser->object_scope = saved_object_scope;
28906 parser->qualifying_scope = saved_qualifying_scope;
28907
28908 if (type == error_mark_node)
28909 cp_parser_skip_to_end_of_statement (parser);
28910
28911 cp_parser_consume_semicolon_at_end_of_statement (parser);
28912
28913 if (type == error_mark_node)
28914 return error_mark_node;
28915
28916 loc = make_location (loc, start_tok->location, parser->lexer);
28917 return finish_type_requirement (loc, type);
28918 }
28919
28920 /* Parse a compound requirement
28921
28922 compound-requirement:
28923 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
28924
28925 static tree
28926 cp_parser_compound_requirement (cp_parser *parser)
28927 {
28928 /* Parse an expression enclosed in '{ }'s. */
28929 matching_braces braces;
28930 if (!braces.require_open (parser))
28931 return error_mark_node;
28932
28933 cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
28934
28935 tree expr = cp_parser_expression (parser, NULL, false, false);
28936 if (expr == error_mark_node)
28937 cp_parser_skip_to_closing_brace (parser);
28938
28939 if (!braces.require_close (parser))
28940 {
28941 cp_parser_skip_to_end_of_statement (parser);
28942 cp_parser_consume_semicolon_at_end_of_statement (parser);
28943 return error_mark_node;
28944 }
28945
28946 /* If the expression was invalid, skip the remainder of the requirement. */
28947 if (!expr || expr == error_mark_node)
28948 {
28949 cp_parser_skip_to_end_of_statement (parser);
28950 cp_parser_consume_semicolon_at_end_of_statement (parser);
28951 return error_mark_node;
28952 }
28953
28954 /* Parse the optional noexcept. */
28955 bool noexcept_p = false;
28956 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
28957 {
28958 cp_lexer_consume_token (parser->lexer);
28959 noexcept_p = true;
28960 }
28961
28962 /* Parse the optional trailing return type. */
28963 tree type = NULL_TREE;
28964 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
28965 {
28966 cp_lexer_consume_token (parser->lexer);
28967 cp_token *tok = cp_lexer_peek_token (parser->lexer);
28968
28969 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
28970 parser->in_result_type_constraint_p = true;
28971 /* C++20 allows either a type-id or a type-constraint. Parsing
28972 a type-id will subsume the parsing for a type-constraint but
28973 allow for more syntactic forms (e.g., const C<T>*). */
28974 type = cp_parser_trailing_type_id (parser);
28975 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
28976 if (type == error_mark_node)
28977 return error_mark_node;
28978
28979 location_t type_loc = make_location (tok->location, tok->location,
28980 parser->lexer);
28981
28982 /* Check that we haven't written something like 'const C<T>*'. */
28983 if (type_uses_auto (type))
28984 {
28985 if (!is_auto (type))
28986 {
28987 error_at (type_loc,
28988 "result type is not a plain type-constraint");
28989 cp_parser_consume_semicolon_at_end_of_statement (parser);
28990 return error_mark_node;
28991 }
28992 }
28993 else if (!flag_concepts_ts)
28994 /* P1452R2 removed the trailing-return-type option. */
28995 error_at (type_loc,
28996 "return-type-requirement is not a type-constraint");
28997 }
28998
28999 location_t loc = make_location (expr_token->location,
29000 braces.open_location (),
29001 parser->lexer);
29002
29003 cp_parser_consume_semicolon_at_end_of_statement (parser);
29004
29005 if (expr == error_mark_node || type == error_mark_node)
29006 return error_mark_node;
29007
29008 return finish_compound_requirement (loc, expr, type, noexcept_p);
29009 }
29010
29011 /* Parse a nested requirement. This is the same as a requires clause.
29012
29013 nested-requirement:
29014 requires-clause */
29015
29016 static tree
29017 cp_parser_nested_requirement (cp_parser *parser)
29018 {
29019 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
29020 cp_token *tok = cp_lexer_consume_token (parser->lexer);
29021 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29022 tree req = cp_parser_constraint_expression (parser);
29023 if (req == error_mark_node)
29024 cp_parser_skip_to_end_of_statement (parser);
29025 loc = make_location (loc, tok->location, parser->lexer);
29026 cp_parser_consume_semicolon_at_end_of_statement (parser);
29027 if (req == error_mark_node)
29028 return error_mark_node;
29029 return finish_nested_requirement (loc, req);
29030 }
29031
29032 /* Support Functions */
29033
29034 /* Return the appropriate prefer_type argument for lookup_name based on
29035 tag_type. */
29036
29037 static inline LOOK_want
29038 prefer_type_arg (tag_types tag_type)
29039 {
29040 switch (tag_type)
29041 {
29042 case none_type: return LOOK_want::NORMAL; // No preference.
29043 case scope_type: return LOOK_want::TYPE_NAMESPACE; // Type or namespace.
29044 default: return LOOK_want::TYPE; // Type only.
29045 }
29046 }
29047
29048 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
29049 NAME should have one of the representations used for an
29050 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
29051 is returned. If PARSER->SCOPE is a dependent type, then a
29052 SCOPE_REF is returned.
29053
29054 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
29055 returned; the name was already resolved when the TEMPLATE_ID_EXPR
29056 was formed. Abstractly, such entities should not be passed to this
29057 function, because they do not need to be looked up, but it is
29058 simpler to check for this special case here, rather than at the
29059 call-sites.
29060
29061 In cases not explicitly covered above, this function returns a
29062 DECL, OVERLOAD, or baselink representing the result of the lookup.
29063 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
29064 is returned.
29065
29066 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
29067 (e.g., "struct") that was used. In that case bindings that do not
29068 refer to types are ignored.
29069
29070 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
29071 ignored.
29072
29073 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
29074 are ignored.
29075
29076 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
29077 types.
29078
29079 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
29080 TREE_LIST of candidates if name-lookup results in an ambiguity, and
29081 NULL_TREE otherwise. */
29082
29083 static cp_expr
29084 cp_parser_lookup_name (cp_parser *parser, tree name,
29085 enum tag_types tag_type,
29086 bool is_template,
29087 bool is_namespace,
29088 bool check_dependency,
29089 tree *ambiguous_decls,
29090 location_t name_location)
29091 {
29092 tree decl;
29093 tree object_type = parser->context->object_type;
29094
29095 /* Assume that the lookup will be unambiguous. */
29096 if (ambiguous_decls)
29097 *ambiguous_decls = NULL_TREE;
29098
29099 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
29100 no longer valid. Note that if we are parsing tentatively, and
29101 the parse fails, OBJECT_TYPE will be automatically restored. */
29102 parser->context->object_type = NULL_TREE;
29103
29104 if (name == error_mark_node)
29105 return error_mark_node;
29106
29107 /* A template-id has already been resolved; there is no lookup to
29108 do. */
29109 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
29110 return name;
29111 if (BASELINK_P (name))
29112 {
29113 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
29114 == TEMPLATE_ID_EXPR);
29115 return name;
29116 }
29117
29118 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
29119 it should already have been checked to make sure that the name
29120 used matches the type being destroyed. */
29121 if (TREE_CODE (name) == BIT_NOT_EXPR)
29122 {
29123 tree type;
29124
29125 /* Figure out to which type this destructor applies. */
29126 if (parser->scope)
29127 type = parser->scope;
29128 else if (object_type)
29129 type = object_type;
29130 else
29131 type = current_class_type;
29132 /* If that's not a class type, there is no destructor. */
29133 if (!type || !CLASS_TYPE_P (type))
29134 return error_mark_node;
29135
29136 /* In a non-static member function, check implicit this->. */
29137 if (current_class_ref)
29138 return lookup_destructor (current_class_ref, parser->scope, name,
29139 tf_warning_or_error);
29140
29141 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
29142 lazily_declare_fn (sfk_destructor, type);
29143
29144 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
29145 return dtor;
29146
29147 return error_mark_node;
29148 }
29149
29150 /* By this point, the NAME should be an ordinary identifier. If
29151 the id-expression was a qualified name, the qualifying scope is
29152 stored in PARSER->SCOPE at this point. */
29153 gcc_assert (identifier_p (name));
29154
29155 /* Perform the lookup. */
29156 if (parser->scope)
29157 {
29158 bool dependent_p;
29159
29160 if (parser->scope == error_mark_node)
29161 return error_mark_node;
29162
29163 /* If the SCOPE is dependent, the lookup must be deferred until
29164 the template is instantiated -- unless we are explicitly
29165 looking up names in uninstantiated templates. Even then, we
29166 cannot look up the name if the scope is not a class type; it
29167 might, for example, be a template type parameter. */
29168 dependent_p = (TYPE_P (parser->scope)
29169 && dependent_scope_p (parser->scope));
29170 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
29171 && dependent_p)
29172 /* Defer lookup. */
29173 decl = error_mark_node;
29174 else
29175 {
29176 tree pushed_scope = NULL_TREE;
29177
29178 /* If PARSER->SCOPE is a dependent type, then it must be a
29179 class type, and we must not be checking dependencies;
29180 otherwise, we would have processed this lookup above. So
29181 that PARSER->SCOPE is not considered a dependent base by
29182 lookup_member, we must enter the scope here. */
29183 if (dependent_p)
29184 pushed_scope = push_scope (parser->scope);
29185
29186 /* If the PARSER->SCOPE is a template specialization, it
29187 may be instantiated during name lookup. In that case,
29188 errors may be issued. Even if we rollback the current
29189 tentative parse, those errors are valid. */
29190 decl = lookup_qualified_name (parser->scope, name,
29191 prefer_type_arg (tag_type),
29192 /*complain=*/true);
29193
29194 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
29195 lookup result and the nested-name-specifier nominates a class C:
29196 * if the name specified after the nested-name-specifier, when
29197 looked up in C, is the injected-class-name of C (Clause 9), or
29198 * if the name specified after the nested-name-specifier is the
29199 same as the identifier or the simple-template-id's template-
29200 name in the last component of the nested-name-specifier,
29201 the name is instead considered to name the constructor of
29202 class C. [ Note: for example, the constructor is not an
29203 acceptable lookup result in an elaborated-type-specifier so
29204 the constructor would not be used in place of the
29205 injected-class-name. --end note ] Such a constructor name
29206 shall be used only in the declarator-id of a declaration that
29207 names a constructor or in a using-declaration. */
29208 if (tag_type == none_type
29209 && DECL_SELF_REFERENCE_P (decl)
29210 && same_type_p (DECL_CONTEXT (decl), parser->scope))
29211 decl = lookup_qualified_name (parser->scope, ctor_identifier,
29212 prefer_type_arg (tag_type),
29213 /*complain=*/true);
29214
29215 if (pushed_scope)
29216 pop_scope (pushed_scope);
29217 }
29218
29219 /* If the scope is a dependent type and either we deferred lookup or
29220 we did lookup but didn't find the name, rememeber the name. */
29221 if (decl == error_mark_node && TYPE_P (parser->scope)
29222 && dependent_type_p (parser->scope))
29223 {
29224 if (tag_type)
29225 {
29226 tree type;
29227
29228 /* The resolution to Core Issue 180 says that `struct
29229 A::B' should be considered a type-name, even if `A'
29230 is dependent. */
29231 type = make_typename_type (parser->scope, name, tag_type,
29232 /*complain=*/tf_error);
29233 if (type != error_mark_node)
29234 decl = TYPE_NAME (type);
29235 }
29236 else if (is_template
29237 && (cp_parser_next_token_ends_template_argument_p (parser)
29238 || cp_lexer_next_token_is (parser->lexer,
29239 CPP_CLOSE_PAREN)))
29240 decl = make_unbound_class_template (parser->scope,
29241 name, NULL_TREE,
29242 /*complain=*/tf_error);
29243 else
29244 decl = build_qualified_name (/*type=*/NULL_TREE,
29245 parser->scope, name,
29246 is_template);
29247 }
29248 parser->qualifying_scope = parser->scope;
29249 parser->object_scope = NULL_TREE;
29250 }
29251 else if (object_type)
29252 {
29253 /* Look up the name in the scope of the OBJECT_TYPE, unless the
29254 OBJECT_TYPE is not a class. */
29255 if (CLASS_TYPE_P (object_type))
29256 /* If the OBJECT_TYPE is a template specialization, it may
29257 be instantiated during name lookup. In that case, errors
29258 may be issued. Even if we rollback the current tentative
29259 parse, those errors are valid. */
29260 decl = lookup_member (object_type,
29261 name,
29262 /*protect=*/0,
29263 /*prefer_type=*/tag_type != none_type,
29264 tf_warning_or_error);
29265 else
29266 decl = NULL_TREE;
29267
29268 if (!decl)
29269 /* Look it up in the enclosing context. DR 141: When looking for a
29270 template-name after -> or ., only consider class templates. */
29271 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
29272 /* DR 141: When looking in the
29273 current enclosing context for a
29274 template-name after -> or ., only
29275 consider class templates. */
29276 : is_template ? LOOK_want::TYPE
29277 : prefer_type_arg (tag_type));
29278 parser->object_scope = object_type;
29279 parser->qualifying_scope = NULL_TREE;
29280 }
29281 else
29282 {
29283 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
29284 : prefer_type_arg (tag_type));
29285 parser->qualifying_scope = NULL_TREE;
29286 parser->object_scope = NULL_TREE;
29287 }
29288
29289 /* If the lookup failed, let our caller know. */
29290 if (!decl || decl == error_mark_node)
29291 return error_mark_node;
29292
29293 /* Pull out the template from an injected-class-name (or multiple). */
29294 if (is_template)
29295 decl = maybe_get_template_decl_from_type_decl (decl);
29296
29297 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
29298 if (TREE_CODE (decl) == TREE_LIST)
29299 {
29300 if (ambiguous_decls)
29301 *ambiguous_decls = decl;
29302 /* The error message we have to print is too complicated for
29303 cp_parser_error, so we incorporate its actions directly. */
29304 if (!cp_parser_simulate_error (parser))
29305 {
29306 error_at (name_location, "reference to %qD is ambiguous",
29307 name);
29308 print_candidates (decl);
29309 }
29310 return error_mark_node;
29311 }
29312
29313 gcc_assert (DECL_P (decl)
29314 || TREE_CODE (decl) == OVERLOAD
29315 || TREE_CODE (decl) == SCOPE_REF
29316 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
29317 || BASELINK_P (decl));
29318
29319 /* If we have resolved the name of a member declaration, check to
29320 see if the declaration is accessible. When the name resolves to
29321 set of overloaded functions, accessibility is checked when
29322 overload resolution is done.
29323
29324 During an explicit instantiation, access is not checked at all,
29325 as per [temp.explicit]. */
29326 if (DECL_P (decl))
29327 check_accessibility_of_qualified_id (decl, object_type, parser->scope,
29328 tf_warning_or_error);
29329
29330 maybe_record_typedef_use (decl);
29331
29332 return cp_expr (decl, name_location);
29333 }
29334
29335 /* Like cp_parser_lookup_name, but for use in the typical case where
29336 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
29337 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
29338
29339 static tree
29340 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
29341 {
29342 return cp_parser_lookup_name (parser, name,
29343 none_type,
29344 /*is_template=*/false,
29345 /*is_namespace=*/false,
29346 /*check_dependency=*/true,
29347 /*ambiguous_decls=*/NULL,
29348 location);
29349 }
29350
29351 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
29352 the current context, return the TYPE_DECL. If TAG_NAME_P is
29353 true, the DECL indicates the class being defined in a class-head,
29354 or declared in an elaborated-type-specifier.
29355
29356 Otherwise, return DECL. */
29357
29358 static tree
29359 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
29360 {
29361 /* If the TEMPLATE_DECL is being declared as part of a class-head,
29362 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
29363
29364 struct A {
29365 template <typename T> struct B;
29366 };
29367
29368 template <typename T> struct A::B {};
29369
29370 Similarly, in an elaborated-type-specifier:
29371
29372 namespace N { struct X{}; }
29373
29374 struct A {
29375 template <typename T> friend struct N::X;
29376 };
29377
29378 However, if the DECL refers to a class type, and we are in
29379 the scope of the class, then the name lookup automatically
29380 finds the TYPE_DECL created by build_self_reference rather
29381 than a TEMPLATE_DECL. For example, in:
29382
29383 template <class T> struct S {
29384 S s;
29385 };
29386
29387 there is no need to handle such case. */
29388
29389 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
29390 return DECL_TEMPLATE_RESULT (decl);
29391
29392 return decl;
29393 }
29394
29395 /* If too many, or too few, template-parameter lists apply to the
29396 declarator, issue an error message. Returns TRUE if all went well,
29397 and FALSE otherwise. */
29398
29399 static bool
29400 cp_parser_check_declarator_template_parameters (cp_parser* parser,
29401 cp_declarator *declarator,
29402 location_t declarator_location)
29403 {
29404 switch (declarator->kind)
29405 {
29406 case cdk_id:
29407 {
29408 unsigned num_templates = 0;
29409 tree scope = declarator->u.id.qualifying_scope;
29410 bool template_id_p = false;
29411
29412 if (scope)
29413 num_templates = num_template_headers_for_class (scope);
29414 else if (TREE_CODE (declarator->u.id.unqualified_name)
29415 == TEMPLATE_ID_EXPR)
29416 {
29417 /* If the DECLARATOR has the form `X<y>' then it uses one
29418 additional level of template parameters. */
29419 ++num_templates;
29420 template_id_p = true;
29421 }
29422
29423 return cp_parser_check_template_parameters
29424 (parser, num_templates, template_id_p, declarator_location,
29425 declarator);
29426 }
29427
29428 case cdk_function:
29429 case cdk_array:
29430 case cdk_pointer:
29431 case cdk_reference:
29432 case cdk_ptrmem:
29433 return (cp_parser_check_declarator_template_parameters
29434 (parser, declarator->declarator, declarator_location));
29435
29436 case cdk_decomp:
29437 case cdk_error:
29438 return true;
29439
29440 default:
29441 gcc_unreachable ();
29442 }
29443 return false;
29444 }
29445
29446 /* NUM_TEMPLATES were used in the current declaration. If that is
29447 invalid, return FALSE and issue an error messages. Otherwise,
29448 return TRUE. If DECLARATOR is non-NULL, then we are checking a
29449 declarator and we can print more accurate diagnostics. */
29450
29451 static bool
29452 cp_parser_check_template_parameters (cp_parser* parser,
29453 unsigned num_templates,
29454 bool template_id_p,
29455 location_t location,
29456 cp_declarator *declarator)
29457 {
29458 /* If there are the same number of template classes and parameter
29459 lists, that's OK. */
29460 if (parser->num_template_parameter_lists == num_templates)
29461 return true;
29462 /* If there are more, but only one more, and the name ends in an identifier,
29463 then we are declaring a primary template. That's OK too. */
29464 if (!template_id_p
29465 && parser->num_template_parameter_lists == num_templates + 1)
29466 return true;
29467
29468 if (cp_parser_simulate_error (parser))
29469 return false;
29470
29471 /* If there are more template classes than parameter lists, we have
29472 something like:
29473
29474 template <class T> void S<T>::R<T>::f (); */
29475 if (parser->num_template_parameter_lists < num_templates)
29476 {
29477 if (declarator && !current_function_decl)
29478 error_at (location, "specializing member %<%T::%E%> "
29479 "requires %<template<>%> syntax",
29480 declarator->u.id.qualifying_scope,
29481 declarator->u.id.unqualified_name);
29482 else if (declarator)
29483 error_at (location, "invalid declaration of %<%T::%E%>",
29484 declarator->u.id.qualifying_scope,
29485 declarator->u.id.unqualified_name);
29486 else
29487 error_at (location, "too few template-parameter-lists");
29488 return false;
29489 }
29490 /* Otherwise, there are too many template parameter lists. We have
29491 something like:
29492
29493 template <class T> template <class U> void S::f(); */
29494 error_at (location, "too many template-parameter-lists");
29495 return false;
29496 }
29497
29498 /* Parse an optional `::' token indicating that the following name is
29499 from the global namespace. If so, PARSER->SCOPE is set to the
29500 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
29501 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
29502 Returns the new value of PARSER->SCOPE, if the `::' token is
29503 present, and NULL_TREE otherwise. */
29504
29505 static tree
29506 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
29507 {
29508 cp_token *token;
29509
29510 /* Peek at the next token. */
29511 token = cp_lexer_peek_token (parser->lexer);
29512 /* If we're looking at a `::' token then we're starting from the
29513 global namespace, not our current location. */
29514 if (token->type == CPP_SCOPE)
29515 {
29516 /* Consume the `::' token. */
29517 cp_lexer_consume_token (parser->lexer);
29518 /* Set the SCOPE so that we know where to start the lookup. */
29519 parser->scope = global_namespace;
29520 parser->qualifying_scope = global_namespace;
29521 parser->object_scope = NULL_TREE;
29522
29523 return parser->scope;
29524 }
29525 else if (!current_scope_valid_p)
29526 {
29527 parser->scope = NULL_TREE;
29528 parser->qualifying_scope = NULL_TREE;
29529 parser->object_scope = NULL_TREE;
29530 }
29531
29532 return NULL_TREE;
29533 }
29534
29535 /* Returns TRUE if the upcoming token sequence is the start of a
29536 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
29537 declarator is preceded by the `friend' specifier. The parser flags FLAGS
29538 is used to control type-specifier parsing. */
29539
29540 static bool
29541 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
29542 bool friend_p)
29543 {
29544 bool constructor_p;
29545 bool outside_class_specifier_p;
29546 tree nested_name_specifier;
29547 cp_token *next_token;
29548
29549 /* The common case is that this is not a constructor declarator, so
29550 try to avoid doing lots of work if at all possible. It's not
29551 valid declare a constructor at function scope. */
29552 if (parser->in_function_body)
29553 return false;
29554 /* And only certain tokens can begin a constructor declarator. */
29555 next_token = cp_lexer_peek_token (parser->lexer);
29556 if (next_token->type != CPP_NAME
29557 && next_token->type != CPP_SCOPE
29558 && next_token->type != CPP_NESTED_NAME_SPECIFIER
29559 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
29560 declarator-id of a constructor or destructor. */
29561 && (next_token->type != CPP_TEMPLATE_ID || cxx_dialect >= cxx20))
29562 return false;
29563
29564 /* Parse tentatively; we are going to roll back all of the tokens
29565 consumed here. */
29566 cp_parser_parse_tentatively (parser);
29567 /* Assume that we are looking at a constructor declarator. */
29568 constructor_p = true;
29569
29570 /* Look for the optional `::' operator. */
29571 cp_parser_global_scope_opt (parser,
29572 /*current_scope_valid_p=*/false);
29573 /* Look for the nested-name-specifier. */
29574 nested_name_specifier
29575 = (cp_parser_nested_name_specifier_opt (parser,
29576 /*typename_keyword_p=*/false,
29577 /*check_dependency_p=*/false,
29578 /*type_p=*/false,
29579 /*is_declaration=*/false));
29580
29581 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
29582 if (nested_name_specifier
29583 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
29584 {
29585 tree s = resolve_typename_type (nested_name_specifier,
29586 /*only_current_p=*/false);
29587 if (TREE_CODE (s) != TYPENAME_TYPE)
29588 nested_name_specifier = s;
29589 }
29590
29591 outside_class_specifier_p = (!at_class_scope_p ()
29592 || !TYPE_BEING_DEFINED (current_class_type)
29593 || friend_p);
29594
29595 /* Outside of a class-specifier, there must be a
29596 nested-name-specifier. Except in C++17 mode, where we
29597 might be declaring a guiding declaration. */
29598 if (!nested_name_specifier && outside_class_specifier_p
29599 && cxx_dialect < cxx17)
29600 constructor_p = false;
29601 else if (nested_name_specifier == error_mark_node)
29602 constructor_p = false;
29603
29604 /* If we have a class scope, this is easy; DR 147 says that S::S always
29605 names the constructor, and no other qualified name could. */
29606 if (constructor_p && nested_name_specifier
29607 && CLASS_TYPE_P (nested_name_specifier))
29608 {
29609 tree id = cp_parser_unqualified_id (parser,
29610 /*template_keyword_p=*/false,
29611 /*check_dependency_p=*/false,
29612 /*declarator_p=*/true,
29613 /*optional_p=*/false);
29614 if (is_overloaded_fn (id))
29615 id = DECL_NAME (get_first_fn (id));
29616 if (!constructor_name_p (id, nested_name_specifier))
29617 constructor_p = false;
29618 }
29619 /* If we still think that this might be a constructor-declarator,
29620 look for a class-name. */
29621 else if (constructor_p)
29622 {
29623 /* If we have:
29624
29625 template <typename T> struct S {
29626 S();
29627 };
29628
29629 we must recognize that the nested `S' names a class. */
29630 if (cxx_dialect >= cxx17)
29631 cp_parser_parse_tentatively (parser);
29632
29633 tree type_decl;
29634 type_decl = cp_parser_class_name (parser,
29635 /*typename_keyword_p=*/false,
29636 /*template_keyword_p=*/false,
29637 none_type,
29638 /*check_dependency_p=*/false,
29639 /*class_head_p=*/false,
29640 /*is_declaration=*/false);
29641
29642 if (cxx_dialect >= cxx17
29643 && !cp_parser_parse_definitely (parser))
29644 {
29645 type_decl = NULL_TREE;
29646 tree tmpl = cp_parser_template_name (parser,
29647 /*template_keyword*/false,
29648 /*check_dependency_p*/false,
29649 /*is_declaration*/false,
29650 none_type,
29651 /*is_identifier*/NULL);
29652 if (DECL_CLASS_TEMPLATE_P (tmpl)
29653 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
29654 /* It's a deduction guide, return true. */;
29655 else
29656 cp_parser_simulate_error (parser);
29657 }
29658
29659 /* If there was no class-name, then this is not a constructor.
29660 Otherwise, if we are in a class-specifier and we aren't
29661 handling a friend declaration, check that its type matches
29662 current_class_type (c++/38313). Note: error_mark_node
29663 is left alone for error recovery purposes. */
29664 constructor_p = (!cp_parser_error_occurred (parser)
29665 && (outside_class_specifier_p
29666 || type_decl == NULL_TREE
29667 || type_decl == error_mark_node
29668 || same_type_p (current_class_type,
29669 TREE_TYPE (type_decl))));
29670
29671 /* If we're still considering a constructor, we have to see a `(',
29672 to begin the parameter-declaration-clause, followed by either a
29673 `)', an `...', or a decl-specifier. We need to check for a
29674 type-specifier to avoid being fooled into thinking that:
29675
29676 S (f) (int);
29677
29678 is a constructor. (It is actually a function named `f' that
29679 takes one parameter (of type `int') and returns a value of type
29680 `S'. */
29681 if (constructor_p
29682 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29683 constructor_p = false;
29684
29685 if (constructor_p
29686 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
29687 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
29688 /* A parameter declaration begins with a decl-specifier,
29689 which is either the "attribute" keyword, a storage class
29690 specifier, or (usually) a type-specifier. */
29691 && (!cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
29692 /* GNU attributes can actually appear both at the start of
29693 a parameter and parenthesized declarator.
29694 S (__attribute__((unused)) int);
29695 is a constructor, but
29696 S (__attribute__((unused)) foo) (int);
29697 is a function declaration. */
29698 || (cp_parser_allow_gnu_extensions_p (parser)
29699 && cp_next_tokens_can_be_gnu_attribute_p (parser)))
29700 /* A parameter declaration can also begin with [[attribute]]. */
29701 && !cp_next_tokens_can_be_std_attribute_p (parser))
29702 {
29703 tree type;
29704 tree pushed_scope = NULL_TREE;
29705 unsigned saved_num_template_parameter_lists;
29706
29707 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
29708 {
29709 unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
29710 while (--n)
29711 cp_lexer_consume_token (parser->lexer);
29712 }
29713
29714 /* Names appearing in the type-specifier should be looked up
29715 in the scope of the class. */
29716 if (current_class_type)
29717 type = NULL_TREE;
29718 else if (type_decl)
29719 {
29720 type = TREE_TYPE (type_decl);
29721 if (TREE_CODE (type) == TYPENAME_TYPE)
29722 {
29723 type = resolve_typename_type (type,
29724 /*only_current_p=*/false);
29725 if (TREE_CODE (type) == TYPENAME_TYPE)
29726 {
29727 cp_parser_abort_tentative_parse (parser);
29728 return false;
29729 }
29730 }
29731 pushed_scope = push_scope (type);
29732 }
29733
29734 /* Inside the constructor parameter list, surrounding
29735 template-parameter-lists do not apply. */
29736 saved_num_template_parameter_lists
29737 = parser->num_template_parameter_lists;
29738 parser->num_template_parameter_lists = 0;
29739
29740 /* Look for the type-specifier. It's not optional, but its typename
29741 might be. Unless this is a friend declaration; we don't want to
29742 treat
29743
29744 friend S (T::fn)(int);
29745
29746 as a constructor, but with P0634, we might assume a type when
29747 looking for the type-specifier. It is actually a function named
29748 `T::fn' that takes one parameter (of type `int') and returns a
29749 value of type `S'. Constructors can be friends, but they must
29750 use a qualified name.
29751
29752 Parse with an empty set of declaration specifiers since we're
29753 trying to match a decl-specifier-seq of the first parameter.
29754 This must be non-null so that cp_parser_simple_type_specifier
29755 will recognize a constrained placeholder type such as:
29756 'C<int> auto' where C is a type concept. */
29757 cp_decl_specifier_seq ctor_specs;
29758 clear_decl_specs (&ctor_specs);
29759 cp_parser_type_specifier (parser,
29760 (friend_p ? CP_PARSER_FLAGS_NONE
29761 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
29762 /*decl_specs=*/&ctor_specs,
29763 /*is_declarator=*/true,
29764 /*declares_class_or_enum=*/NULL,
29765 /*is_cv_qualifier=*/NULL);
29766
29767 parser->num_template_parameter_lists
29768 = saved_num_template_parameter_lists;
29769
29770 /* Leave the scope of the class. */
29771 if (pushed_scope)
29772 pop_scope (pushed_scope);
29773
29774 constructor_p = !cp_parser_error_occurred (parser);
29775 }
29776 }
29777
29778 /* We did not really want to consume any tokens. */
29779 cp_parser_abort_tentative_parse (parser);
29780
29781 return constructor_p;
29782 }
29783
29784 /* Parse the definition of the function given by the DECL_SPECIFIERS,
29785 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
29786 they must be performed once we are in the scope of the function.
29787
29788 Returns the function defined. */
29789
29790 static tree
29791 cp_parser_function_definition_from_specifiers_and_declarator
29792 (cp_parser* parser,
29793 cp_decl_specifier_seq *decl_specifiers,
29794 tree attributes,
29795 const cp_declarator *declarator)
29796 {
29797 tree fn;
29798 bool success_p;
29799
29800 /* Begin the function-definition. */
29801 success_p = start_function (decl_specifiers, declarator, attributes);
29802
29803 /* The things we're about to see are not directly qualified by any
29804 template headers we've seen thus far. */
29805 reset_specialization ();
29806
29807 /* If there were names looked up in the decl-specifier-seq that we
29808 did not check, check them now. We must wait until we are in the
29809 scope of the function to perform the checks, since the function
29810 might be a friend. */
29811 perform_deferred_access_checks (tf_warning_or_error);
29812
29813 if (success_p)
29814 {
29815 cp_finalize_omp_declare_simd (parser, current_function_decl);
29816 parser->omp_declare_simd = NULL;
29817 cp_finalize_oacc_routine (parser, current_function_decl, true);
29818 parser->oacc_routine = NULL;
29819 }
29820
29821 if (!success_p)
29822 {
29823 /* Skip the entire function. */
29824 cp_parser_skip_to_end_of_block_or_statement (parser);
29825 fn = error_mark_node;
29826 }
29827 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
29828 {
29829 /* Seen already, skip it. An error message has already been output. */
29830 cp_parser_skip_to_end_of_block_or_statement (parser);
29831 fn = current_function_decl;
29832 current_function_decl = NULL_TREE;
29833 /* If this is a function from a class, pop the nested class. */
29834 if (current_class_name)
29835 pop_nested_class ();
29836 }
29837 else
29838 {
29839 timevar_id_t tv;
29840 if (DECL_DECLARED_INLINE_P (current_function_decl))
29841 tv = TV_PARSE_INLINE;
29842 else
29843 tv = TV_PARSE_FUNC;
29844 timevar_push (tv);
29845 fn = cp_parser_function_definition_after_declarator (parser,
29846 /*inline_p=*/false);
29847 timevar_pop (tv);
29848 }
29849
29850 return fn;
29851 }
29852
29853 /* Parse the part of a function-definition that follows the
29854 declarator. INLINE_P is TRUE iff this function is an inline
29855 function defined within a class-specifier.
29856
29857 Returns the function defined. */
29858
29859 static tree
29860 cp_parser_function_definition_after_declarator (cp_parser* parser,
29861 bool inline_p)
29862 {
29863 tree fn;
29864 bool saved_in_unbraced_linkage_specification_p;
29865 bool saved_in_function_body;
29866 unsigned saved_num_template_parameter_lists;
29867 cp_token *token;
29868 bool fully_implicit_function_template_p
29869 = parser->fully_implicit_function_template_p;
29870 parser->fully_implicit_function_template_p = false;
29871 tree implicit_template_parms
29872 = parser->implicit_template_parms;
29873 parser->implicit_template_parms = 0;
29874 cp_binding_level* implicit_template_scope
29875 = parser->implicit_template_scope;
29876 parser->implicit_template_scope = 0;
29877
29878 saved_in_function_body = parser->in_function_body;
29879 parser->in_function_body = true;
29880 /* If the next token is `return', then the code may be trying to
29881 make use of the "named return value" extension that G++ used to
29882 support. */
29883 token = cp_lexer_peek_token (parser->lexer);
29884 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
29885 {
29886 /* Consume the `return' keyword. */
29887 cp_lexer_consume_token (parser->lexer);
29888 /* Look for the identifier that indicates what value is to be
29889 returned. */
29890 cp_parser_identifier (parser);
29891 /* Issue an error message. */
29892 error_at (token->location,
29893 "named return values are no longer supported");
29894 /* Skip tokens until we reach the start of the function body. */
29895 while (true)
29896 {
29897 cp_token *token = cp_lexer_peek_token (parser->lexer);
29898 if (token->type == CPP_OPEN_BRACE
29899 || token->type == CPP_EOF
29900 || token->type == CPP_PRAGMA_EOL)
29901 break;
29902 cp_lexer_consume_token (parser->lexer);
29903 }
29904 }
29905 /* The `extern' in `extern "C" void f () { ... }' does not apply to
29906 anything declared inside `f'. */
29907 saved_in_unbraced_linkage_specification_p
29908 = parser->in_unbraced_linkage_specification_p;
29909 parser->in_unbraced_linkage_specification_p = false;
29910 /* Inside the function, surrounding template-parameter-lists do not
29911 apply. */
29912 saved_num_template_parameter_lists
29913 = parser->num_template_parameter_lists;
29914 parser->num_template_parameter_lists = 0;
29915
29916 /* If the next token is `try', `__transaction_atomic', or
29917 `__transaction_relaxed`, then we are looking at either function-try-block
29918 or function-transaction-block. Note that all of these include the
29919 function-body. */
29920 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
29921 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
29922 else if (cp_lexer_next_token_is_keyword (parser->lexer,
29923 RID_TRANSACTION_RELAXED))
29924 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
29925 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
29926 cp_parser_function_try_block (parser);
29927 else
29928 cp_parser_ctor_initializer_opt_and_function_body
29929 (parser, /*in_function_try_block=*/false);
29930
29931 /* Finish the function. */
29932 fn = finish_function (inline_p);
29933
29934 if (modules_p ()
29935 && !inline_p
29936 && TYPE_P (DECL_CONTEXT (fn))
29937 && (DECL_DECLARED_INLINE_P (fn)
29938 || processing_template_decl))
29939 set_defining_module (fn);
29940
29941 /* Generate code for it, if necessary. */
29942 expand_or_defer_fn (fn);
29943 /* Restore the saved values. */
29944 parser->in_unbraced_linkage_specification_p
29945 = saved_in_unbraced_linkage_specification_p;
29946 parser->num_template_parameter_lists
29947 = saved_num_template_parameter_lists;
29948 parser->in_function_body = saved_in_function_body;
29949
29950 parser->fully_implicit_function_template_p
29951 = fully_implicit_function_template_p;
29952 parser->implicit_template_parms
29953 = implicit_template_parms;
29954 parser->implicit_template_scope
29955 = implicit_template_scope;
29956
29957 if (parser->fully_implicit_function_template_p)
29958 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
29959
29960 return fn;
29961 }
29962
29963 /* Parse a template-declaration body (following argument list). */
29964
29965 static void
29966 cp_parser_template_declaration_after_parameters (cp_parser* parser,
29967 tree parameter_list,
29968 bool member_p)
29969 {
29970 tree decl = NULL_TREE;
29971 bool friend_p = false;
29972
29973 /* We just processed one more parameter list. */
29974 ++parser->num_template_parameter_lists;
29975
29976 /* Get the deferred access checks from the parameter list. These
29977 will be checked once we know what is being declared, as for a
29978 member template the checks must be performed in the scope of the
29979 class containing the member. */
29980 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
29981
29982 /* Tentatively parse for a new template parameter list, which can either be
29983 the template keyword or a template introduction. */
29984 if (cp_parser_template_declaration_after_export (parser, member_p))
29985 /* OK */;
29986 else if (cxx_dialect >= cxx11
29987 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
29988 decl = cp_parser_alias_declaration (parser);
29989 else if (cxx_dialect >= cxx20 /* Implies flag_concept. */
29990 && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
29991 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_BOOL))
29992 /* Allow 'concept bool' to be handled as per the TS. */
29993 decl = cp_parser_concept_definition (parser);
29994 else
29995 {
29996 cp_token *token = cp_lexer_peek_token (parser->lexer);
29997 decl = cp_parser_single_declaration (parser,
29998 checks,
29999 member_p,
30000 /*explicit_specialization_p=*/false,
30001 &friend_p);
30002
30003 /* If this is a member template declaration, let the front
30004 end know. */
30005 if (member_p && !friend_p && decl)
30006 {
30007 if (TREE_CODE (decl) == TYPE_DECL)
30008 cp_parser_check_access_in_redeclaration (decl, token->location);
30009
30010 decl = finish_member_template_decl (decl);
30011 }
30012 else if (friend_p && decl
30013 && DECL_DECLARES_TYPE_P (decl))
30014 make_friend_class (current_class_type, TREE_TYPE (decl),
30015 /*complain=*/true);
30016 }
30017 /* We are done with the current parameter list. */
30018 --parser->num_template_parameter_lists;
30019
30020 pop_deferring_access_checks ();
30021
30022 /* Finish up. */
30023 finish_template_decl (parameter_list);
30024
30025 /* Check the template arguments for a literal operator template. */
30026 if (decl
30027 && DECL_DECLARES_FUNCTION_P (decl)
30028 && UDLIT_OPER_P (DECL_NAME (decl)))
30029 {
30030 bool ok = true;
30031 if (parameter_list == NULL_TREE)
30032 ok = false;
30033 else
30034 {
30035 int num_parms = TREE_VEC_LENGTH (parameter_list);
30036 if (num_parms == 1)
30037 {
30038 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
30039 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
30040 if (TREE_CODE (parm) != PARM_DECL)
30041 ok = false;
30042 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
30043 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
30044 /* OK, C++20 string literal operator template. We don't need
30045 to warn in lower dialects here because we will have already
30046 warned about the template parameter. */;
30047 else if (TREE_TYPE (parm) != char_type_node
30048 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
30049 ok = false;
30050 }
30051 else if (num_parms == 2 && cxx_dialect >= cxx14)
30052 {
30053 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
30054 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
30055 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
30056 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
30057 if (TREE_CODE (parm) != PARM_DECL
30058 || TREE_TYPE (parm) != TREE_TYPE (type)
30059 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
30060 ok = false;
30061 else
30062 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
30063 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
30064 "ISO C++ did not adopt string literal operator templa"
30065 "tes taking an argument pack of characters");
30066 }
30067 else
30068 ok = false;
30069 }
30070 if (!ok)
30071 {
30072 if (cxx_dialect > cxx17)
30073 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
30074 "template %qD has invalid parameter list; expected "
30075 "non-type template parameter pack %<<char...>%> or "
30076 "single non-type parameter of class type",
30077 decl);
30078 else
30079 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
30080 "template %qD has invalid parameter list; expected "
30081 "non-type template parameter pack %<<char...>%>",
30082 decl);
30083 }
30084 }
30085
30086 /* Register member declarations. */
30087 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
30088 finish_member_declaration (decl);
30089 /* If DECL is a function template, we must return to parse it later.
30090 (Even though there is no definition, there might be default
30091 arguments that need handling.) */
30092 if (member_p && decl
30093 && DECL_DECLARES_FUNCTION_P (decl))
30094 vec_safe_push (unparsed_funs_with_definitions, decl);
30095 }
30096
30097 /* Parse a template introduction header for a template-declaration. Returns
30098 false if tentative parse fails. */
30099
30100 static bool
30101 cp_parser_template_introduction (cp_parser* parser, bool member_p)
30102 {
30103 cp_parser_parse_tentatively (parser);
30104
30105 tree saved_scope = parser->scope;
30106 tree saved_object_scope = parser->object_scope;
30107 tree saved_qualifying_scope = parser->qualifying_scope;
30108
30109 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
30110
30111 /* Look for the optional `::' operator. */
30112 cp_parser_global_scope_opt (parser,
30113 /*current_scope_valid_p=*/false);
30114 /* Look for the nested-name-specifier. */
30115 cp_parser_nested_name_specifier_opt (parser,
30116 /*typename_keyword_p=*/false,
30117 /*check_dependency_p=*/true,
30118 /*type_p=*/false,
30119 /*is_declaration=*/false);
30120
30121 cp_token *token = cp_lexer_peek_token (parser->lexer);
30122 tree concept_name = cp_parser_identifier (parser);
30123
30124 /* Look up the concept for which we will be matching
30125 template parameters. */
30126 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
30127 token->location);
30128 parser->scope = saved_scope;
30129 parser->object_scope = saved_object_scope;
30130 parser->qualifying_scope = saved_qualifying_scope;
30131
30132 if (concept_name == error_mark_node
30133 || (seen_error () && !concept_definition_p (tmpl_decl)))
30134 cp_parser_simulate_error (parser);
30135
30136 /* Look for opening brace for introduction. */
30137 matching_braces braces;
30138 braces.require_open (parser);
30139 location_t open_loc = input_location;
30140
30141 if (!cp_parser_parse_definitely (parser))
30142 return false;
30143
30144 push_deferring_access_checks (dk_deferred);
30145
30146 /* Build vector of placeholder parameters and grab
30147 matching identifiers. */
30148 tree introduction_list = cp_parser_introduction_list (parser);
30149
30150 /* Look for closing brace for introduction. */
30151 if (!braces.require_close (parser))
30152 return true;
30153
30154 /* The introduction-list shall not be empty. */
30155 int nargs = TREE_VEC_LENGTH (introduction_list);
30156 if (nargs == 0)
30157 {
30158 /* In cp_parser_introduction_list we have already issued an error. */
30159 return true;
30160 }
30161
30162 if (tmpl_decl == error_mark_node)
30163 {
30164 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
30165 token->location);
30166 return true;
30167 }
30168
30169 /* Build and associate the constraint. */
30170 location_t introduction_loc = make_location (open_loc,
30171 start_token->location,
30172 parser->lexer);
30173 tree parms = finish_template_introduction (tmpl_decl,
30174 introduction_list,
30175 introduction_loc);
30176 if (parms && parms != error_mark_node)
30177 {
30178 if (!flag_concepts_ts)
30179 pedwarn (introduction_loc, 0, "template-introductions"
30180 " are not part of C++20 concepts; use %qs to enable",
30181 "-fconcepts-ts");
30182
30183 cp_parser_template_declaration_after_parameters (parser, parms,
30184 member_p);
30185 return true;
30186 }
30187
30188 if (parms == NULL_TREE)
30189 error_at (token->location, "no matching concept for template-introduction");
30190
30191 return true;
30192 }
30193
30194 /* Parse a normal template-declaration following the template keyword. */
30195
30196 static void
30197 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
30198 {
30199 tree parameter_list;
30200 bool need_lang_pop;
30201 location_t location = input_location;
30202
30203 /* Look for the `<' token. */
30204 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
30205 return;
30206 if (at_class_scope_p () && current_function_decl)
30207 {
30208 /* 14.5.2.2 [temp.mem]
30209
30210 A local class shall not have member templates. */
30211 error_at (location,
30212 "invalid declaration of member template in local class");
30213 cp_parser_skip_to_end_of_block_or_statement (parser);
30214 return;
30215 }
30216 /* [temp]
30217
30218 A template ... shall not have C linkage. */
30219 if (current_lang_name == lang_name_c)
30220 {
30221 error_at (location, "template with C linkage");
30222 maybe_show_extern_c_location ();
30223 /* Give it C++ linkage to avoid confusing other parts of the
30224 front end. */
30225 push_lang_context (lang_name_cplusplus);
30226 need_lang_pop = true;
30227 }
30228 else
30229 need_lang_pop = false;
30230
30231 /* We cannot perform access checks on the template parameter
30232 declarations until we know what is being declared, just as we
30233 cannot check the decl-specifier list. */
30234 push_deferring_access_checks (dk_deferred);
30235
30236 /* If the next token is `>', then we have an invalid
30237 specialization. Rather than complain about an invalid template
30238 parameter, issue an error message here. */
30239 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
30240 {
30241 cp_parser_error (parser, "invalid explicit specialization");
30242 begin_specialization ();
30243 parameter_list = NULL_TREE;
30244 }
30245 else
30246 {
30247 /* Parse the template parameters. */
30248 parameter_list = cp_parser_template_parameter_list (parser);
30249 }
30250
30251 /* Look for the `>'. */
30252 cp_parser_skip_to_end_of_template_parameter_list (parser);
30253
30254 /* Manage template requirements */
30255 if (flag_concepts)
30256 {
30257 tree reqs = get_shorthand_constraints (current_template_parms);
30258 if (tree treqs = cp_parser_requires_clause_opt (parser, false))
30259 reqs = combine_constraint_expressions (reqs, treqs);
30260 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
30261 }
30262
30263 cp_parser_template_declaration_after_parameters (parser, parameter_list,
30264 member_p);
30265
30266 /* For the erroneous case of a template with C linkage, we pushed an
30267 implicit C++ linkage scope; exit that scope now. */
30268 if (need_lang_pop)
30269 pop_lang_context ();
30270 }
30271
30272 /* Parse a template-declaration, assuming that the `export' (and
30273 `extern') keywords, if present, has already been scanned. MEMBER_P
30274 is as for cp_parser_template_declaration. */
30275
30276 static bool
30277 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
30278 {
30279 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
30280 {
30281 cp_lexer_consume_token (parser->lexer);
30282 cp_parser_explicit_template_declaration (parser, member_p);
30283 return true;
30284 }
30285 else if (flag_concepts)
30286 return cp_parser_template_introduction (parser, member_p);
30287
30288 return false;
30289 }
30290
30291 /* Perform the deferred access checks from a template-parameter-list.
30292 CHECKS is a TREE_LIST of access checks, as returned by
30293 get_deferred_access_checks. */
30294
30295 static void
30296 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
30297 {
30298 ++processing_template_parmlist;
30299 perform_access_checks (checks, tf_warning_or_error);
30300 --processing_template_parmlist;
30301 }
30302
30303 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
30304 `function-definition' sequence that follows a template header.
30305 If MEMBER_P is true, this declaration appears in a class scope.
30306
30307 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
30308 *FRIEND_P is set to TRUE iff the declaration is a friend. */
30309
30310 static tree
30311 cp_parser_single_declaration (cp_parser* parser,
30312 vec<deferred_access_check, va_gc> *checks,
30313 bool member_p,
30314 bool explicit_specialization_p,
30315 bool* friend_p)
30316 {
30317 int declares_class_or_enum;
30318 tree decl = NULL_TREE;
30319 cp_decl_specifier_seq decl_specifiers;
30320 bool function_definition_p = false;
30321 cp_token *decl_spec_token_start;
30322
30323 /* This function is only used when processing a template
30324 declaration. */
30325 gcc_assert (innermost_scope_kind () == sk_template_parms
30326 || innermost_scope_kind () == sk_template_spec);
30327
30328 /* Defer access checks until we know what is being declared. */
30329 push_deferring_access_checks (dk_deferred);
30330
30331 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
30332 alternative. */
30333 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
30334 cp_parser_decl_specifier_seq (parser,
30335 (CP_PARSER_FLAGS_OPTIONAL
30336 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
30337 &decl_specifiers,
30338 &declares_class_or_enum);
30339 if (friend_p)
30340 *friend_p = cp_parser_friend_p (&decl_specifiers);
30341
30342 /* There are no template typedefs. */
30343 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
30344 {
30345 error_at (decl_spec_token_start->location,
30346 "template declaration of %<typedef%>");
30347 decl = error_mark_node;
30348 }
30349
30350 /* Gather up the access checks that occurred the
30351 decl-specifier-seq. */
30352 stop_deferring_access_checks ();
30353
30354 /* Check for the declaration of a template class. */
30355 if (declares_class_or_enum)
30356 {
30357 if (cp_parser_declares_only_class_p (parser)
30358 || (declares_class_or_enum & 2))
30359 {
30360 /* If this is a declaration, but not a definition, associate
30361 any constraints with the type declaration. Constraints
30362 are associated with definitions in cp_parser_class_specifier. */
30363 if (declares_class_or_enum == 1)
30364 associate_classtype_constraints (decl_specifiers.type);
30365
30366 decl = shadow_tag (&decl_specifiers);
30367
30368 /* In this case:
30369
30370 struct C {
30371 friend template <typename T> struct A<T>::B;
30372 };
30373
30374 A<T>::B will be represented by a TYPENAME_TYPE, and
30375 therefore not recognized by shadow_tag. */
30376 if (friend_p && *friend_p
30377 && !decl
30378 && decl_specifiers.type
30379 && TYPE_P (decl_specifiers.type))
30380 decl = decl_specifiers.type;
30381
30382 if (decl && decl != error_mark_node)
30383 decl = TYPE_NAME (decl);
30384 else
30385 decl = error_mark_node;
30386
30387 /* Perform access checks for template parameters. */
30388 cp_parser_perform_template_parameter_access_checks (checks);
30389
30390 /* Give a helpful diagnostic for
30391 template <class T> struct A { } a;
30392 if we aren't already recovering from an error. */
30393 if (!cp_parser_declares_only_class_p (parser)
30394 && !seen_error ())
30395 {
30396 error_at (cp_lexer_peek_token (parser->lexer)->location,
30397 "a class template declaration must not declare "
30398 "anything else");
30399 cp_parser_skip_to_end_of_block_or_statement (parser);
30400 goto out;
30401 }
30402 }
30403 }
30404
30405 /* Complain about missing 'typename' or other invalid type names. */
30406 if (!decl_specifiers.any_type_specifiers_p
30407 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
30408 {
30409 /* cp_parser_parse_and_diagnose_invalid_type_name calls
30410 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
30411 the rest of this declaration. */
30412 decl = error_mark_node;
30413 goto out;
30414 }
30415
30416 /* If it's not a template class, try for a template function. If
30417 the next token is a `;', then this declaration does not declare
30418 anything. But, if there were errors in the decl-specifiers, then
30419 the error might well have come from an attempted class-specifier.
30420 In that case, there's no need to warn about a missing declarator. */
30421 if (!decl
30422 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
30423 || decl_specifiers.type != error_mark_node))
30424 {
30425 decl = cp_parser_init_declarator (parser,
30426 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
30427 &decl_specifiers,
30428 checks,
30429 /*function_definition_allowed_p=*/true,
30430 member_p,
30431 declares_class_or_enum,
30432 &function_definition_p,
30433 NULL, NULL, NULL);
30434
30435 /* 7.1.1-1 [dcl.stc]
30436
30437 A storage-class-specifier shall not be specified in an explicit
30438 specialization... */
30439 if (decl
30440 && explicit_specialization_p
30441 && decl_specifiers.storage_class != sc_none)
30442 {
30443 error_at (decl_spec_token_start->location,
30444 "explicit template specialization cannot have a storage class");
30445 decl = error_mark_node;
30446 }
30447
30448 if (decl && VAR_P (decl))
30449 check_template_variable (decl);
30450 }
30451
30452 /* Look for a trailing `;' after the declaration. */
30453 if (!function_definition_p
30454 && (decl == error_mark_node
30455 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
30456 cp_parser_skip_to_end_of_block_or_statement (parser);
30457
30458 out:
30459 pop_deferring_access_checks ();
30460
30461 /* Clear any current qualification; whatever comes next is the start
30462 of something new. */
30463 parser->scope = NULL_TREE;
30464 parser->qualifying_scope = NULL_TREE;
30465 parser->object_scope = NULL_TREE;
30466
30467 return decl;
30468 }
30469
30470 /* Parse a cast-expression that is not the operand of a unary "&". */
30471
30472 static cp_expr
30473 cp_parser_simple_cast_expression (cp_parser *parser)
30474 {
30475 return cp_parser_cast_expression (parser, /*address_p=*/false,
30476 /*cast_p=*/false, /*decltype*/false, NULL);
30477 }
30478
30479 /* Parse a functional cast to TYPE. Returns an expression
30480 representing the cast. */
30481
30482 static cp_expr
30483 cp_parser_functional_cast (cp_parser* parser, tree type)
30484 {
30485 vec<tree, va_gc> *vec;
30486 tree expression_list;
30487 cp_expr cast;
30488 bool nonconst_p;
30489
30490 location_t start_loc = input_location;
30491
30492 if (!type)
30493 type = error_mark_node;
30494
30495 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30496 {
30497 cp_lexer_set_source_position (parser->lexer);
30498 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
30499 expression_list = cp_parser_braced_list (parser, &nonconst_p);
30500 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
30501 if (TREE_CODE (type) == TYPE_DECL)
30502 type = TREE_TYPE (type);
30503
30504 cast = finish_compound_literal (type, expression_list,
30505 tf_warning_or_error, fcl_functional);
30506 /* Create a location of the form:
30507 type_name{i, f}
30508 ^~~~~~~~~~~~~~~
30509 with caret == start at the start of the type name,
30510 finishing at the closing brace. */
30511 location_t combined_loc = make_location (start_loc, start_loc,
30512 parser->lexer);
30513 cast.set_location (combined_loc);
30514 return cast;
30515 }
30516
30517
30518 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
30519 /*cast_p=*/true,
30520 /*allow_expansion_p=*/true,
30521 /*non_constant_p=*/NULL);
30522 if (vec == NULL)
30523 expression_list = error_mark_node;
30524 else
30525 {
30526 expression_list = build_tree_list_vec (vec);
30527 release_tree_vector (vec);
30528 }
30529
30530 /* Create a location of the form:
30531 float(i)
30532 ^~~~~~~~
30533 with caret == start at the start of the type name,
30534 finishing at the closing paren. */
30535 location_t combined_loc = make_location (start_loc, start_loc,
30536 parser->lexer);
30537 cast = build_functional_cast (combined_loc, type, expression_list,
30538 tf_warning_or_error);
30539
30540 /* [expr.const]/1: In an integral constant expression "only type
30541 conversions to integral or enumeration type can be used". */
30542 if (TREE_CODE (type) == TYPE_DECL)
30543 type = TREE_TYPE (type);
30544 if (cast != error_mark_node
30545 && !cast_valid_in_integral_constant_expression_p (type)
30546 && cp_parser_non_integral_constant_expression (parser,
30547 NIC_CONSTRUCTOR))
30548 return error_mark_node;
30549
30550 return cast;
30551 }
30552
30553 /* Save the tokens that make up the body of a member function defined
30554 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
30555 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
30556 specifiers applied to the declaration. Returns the FUNCTION_DECL
30557 for the member function. */
30558
30559 static tree
30560 cp_parser_save_member_function_body (cp_parser* parser,
30561 cp_decl_specifier_seq *decl_specifiers,
30562 cp_declarator *declarator,
30563 tree attributes)
30564 {
30565 cp_token *first;
30566 cp_token *last;
30567 tree fn;
30568 bool function_try_block = false;
30569
30570 /* Create the FUNCTION_DECL. */
30571 fn = grokmethod (decl_specifiers, declarator, attributes);
30572 cp_finalize_omp_declare_simd (parser, fn);
30573 cp_finalize_oacc_routine (parser, fn, true);
30574 /* If something went badly wrong, bail out now. */
30575 if (fn == error_mark_node)
30576 {
30577 /* If there's a function-body, skip it. */
30578 if (cp_parser_token_starts_function_definition_p
30579 (cp_lexer_peek_token (parser->lexer)))
30580 cp_parser_skip_to_end_of_block_or_statement (parser);
30581 return error_mark_node;
30582 }
30583
30584 /* Remember it, if there are default args to post process. */
30585 cp_parser_save_default_args (parser, fn);
30586
30587 /* Save away the tokens that make up the body of the
30588 function. */
30589 first = parser->lexer->next_token;
30590
30591 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
30592 cp_lexer_consume_token (parser->lexer);
30593 else if (cp_lexer_next_token_is_keyword (parser->lexer,
30594 RID_TRANSACTION_ATOMIC))
30595 {
30596 cp_lexer_consume_token (parser->lexer);
30597 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
30598 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
30599 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
30600 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
30601 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
30602 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
30603 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
30604 {
30605 cp_lexer_consume_token (parser->lexer);
30606 cp_lexer_consume_token (parser->lexer);
30607 cp_lexer_consume_token (parser->lexer);
30608 cp_lexer_consume_token (parser->lexer);
30609 cp_lexer_consume_token (parser->lexer);
30610 }
30611 else
30612 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
30613 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
30614 {
30615 cp_lexer_consume_token (parser->lexer);
30616 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
30617 break;
30618 }
30619 }
30620
30621 /* Handle function try blocks. */
30622 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
30623 {
30624 cp_lexer_consume_token (parser->lexer);
30625 function_try_block = true;
30626 }
30627 /* We can have braced-init-list mem-initializers before the fn body. */
30628 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30629 {
30630 cp_lexer_consume_token (parser->lexer);
30631 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
30632 {
30633 /* cache_group will stop after an un-nested { } pair, too. */
30634 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
30635 break;
30636
30637 /* variadic mem-inits have ... after the ')'. */
30638 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30639 cp_lexer_consume_token (parser->lexer);
30640 }
30641 }
30642 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
30643 /* Handle function try blocks. */
30644 if (function_try_block)
30645 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
30646 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
30647 last = parser->lexer->next_token;
30648
30649 /* Save away the inline definition; we will process it when the
30650 class is complete. */
30651 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
30652 DECL_PENDING_INLINE_P (fn) = 1;
30653
30654 /* We need to know that this was defined in the class, so that
30655 friend templates are handled correctly. */
30656 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
30657
30658 /* Add FN to the queue of functions to be parsed later. */
30659 vec_safe_push (unparsed_funs_with_definitions, fn);
30660
30661 return fn;
30662 }
30663
30664 /* Save the tokens that make up the in-class initializer for a non-static
30665 data member. Returns a DEFERRED_PARSE. */
30666
30667 static tree
30668 cp_parser_save_nsdmi (cp_parser* parser)
30669 {
30670 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
30671 }
30672
30673 /* Parse a template-argument-list, as well as the trailing ">" (but
30674 not the opening "<"). See cp_parser_template_argument_list for the
30675 return value. */
30676
30677 static tree
30678 cp_parser_enclosed_template_argument_list (cp_parser* parser)
30679 {
30680 tree arguments;
30681 tree saved_scope;
30682 tree saved_qualifying_scope;
30683 tree saved_object_scope;
30684 bool saved_greater_than_is_operator_p;
30685
30686 /* [temp.names]
30687
30688 When parsing a template-id, the first non-nested `>' is taken as
30689 the end of the template-argument-list rather than a greater-than
30690 operator. */
30691 saved_greater_than_is_operator_p
30692 = parser->greater_than_is_operator_p;
30693 parser->greater_than_is_operator_p = false;
30694 /* Parsing the argument list may modify SCOPE, so we save it
30695 here. */
30696 saved_scope = parser->scope;
30697 saved_qualifying_scope = parser->qualifying_scope;
30698 saved_object_scope = parser->object_scope;
30699 /* We need to evaluate the template arguments, even though this
30700 template-id may be nested within a "sizeof". */
30701 cp_evaluated ev;
30702 /* Parse the template-argument-list itself. */
30703 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
30704 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
30705 arguments = NULL_TREE;
30706 else
30707 arguments = cp_parser_template_argument_list (parser);
30708 /* Look for the `>' that ends the template-argument-list. If we find
30709 a '>>' instead, it's probably just a typo. */
30710 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
30711 {
30712 if (cxx_dialect != cxx98)
30713 {
30714 /* In C++0x, a `>>' in a template argument list or cast
30715 expression is considered to be two separate `>'
30716 tokens. So, change the current token to a `>', but don't
30717 consume it: it will be consumed later when the outer
30718 template argument list (or cast expression) is parsed.
30719 Note that this replacement of `>' for `>>' is necessary
30720 even if we are parsing tentatively: in the tentative
30721 case, after calling
30722 cp_parser_enclosed_template_argument_list we will always
30723 throw away all of the template arguments and the first
30724 closing `>', either because the template argument list
30725 was erroneous or because we are replacing those tokens
30726 with a CPP_TEMPLATE_ID token. The second `>' (which will
30727 not have been thrown away) is needed either to close an
30728 outer template argument list or to complete a new-style
30729 cast. */
30730 cp_token *token = cp_lexer_peek_token (parser->lexer);
30731 token->type = CPP_GREATER;
30732 }
30733 else if (!saved_greater_than_is_operator_p)
30734 {
30735 /* If we're in a nested template argument list, the '>>' has
30736 to be a typo for '> >'. We emit the error message, but we
30737 continue parsing and we push a '>' as next token, so that
30738 the argument list will be parsed correctly. Note that the
30739 global source location is still on the token before the
30740 '>>', so we need to say explicitly where we want it. */
30741 cp_token *token = cp_lexer_peek_token (parser->lexer);
30742 gcc_rich_location richloc (token->location);
30743 richloc.add_fixit_replace ("> >");
30744 error_at (&richloc, "%<>>%> should be %<> >%> "
30745 "within a nested template argument list");
30746
30747 token->type = CPP_GREATER;
30748 }
30749 else
30750 {
30751 /* If this is not a nested template argument list, the '>>'
30752 is a typo for '>'. Emit an error message and continue.
30753 Same deal about the token location, but here we can get it
30754 right by consuming the '>>' before issuing the diagnostic. */
30755 cp_token *token = cp_lexer_consume_token (parser->lexer);
30756 error_at (token->location,
30757 "spurious %<>>%>, use %<>%> to terminate "
30758 "a template argument list");
30759 }
30760 }
30761 else
30762 cp_parser_skip_to_end_of_template_parameter_list (parser);
30763 /* The `>' token might be a greater-than operator again now. */
30764 parser->greater_than_is_operator_p
30765 = saved_greater_than_is_operator_p;
30766 /* Restore the SAVED_SCOPE. */
30767 parser->scope = saved_scope;
30768 parser->qualifying_scope = saved_qualifying_scope;
30769 parser->object_scope = saved_object_scope;
30770
30771 return arguments;
30772 }
30773
30774 /* MEMBER_FUNCTION is a member function, or a friend. If default
30775 arguments, or the body of the function have not yet been parsed,
30776 parse them now. */
30777
30778 static void
30779 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
30780 {
30781 timevar_push (TV_PARSE_INMETH);
30782 /* If this member is a template, get the underlying
30783 FUNCTION_DECL. */
30784 if (DECL_FUNCTION_TEMPLATE_P (member_function))
30785 member_function = DECL_TEMPLATE_RESULT (member_function);
30786
30787 /* There should not be any class definitions in progress at this
30788 point; the bodies of members are only parsed outside of all class
30789 definitions. */
30790 gcc_assert (parser->num_classes_being_defined == 0);
30791 /* While we're parsing the member functions we might encounter more
30792 classes. We want to handle them right away, but we don't want
30793 them getting mixed up with functions that are currently in the
30794 queue. */
30795 push_unparsed_function_queues (parser);
30796
30797 /* Make sure that any template parameters are in scope. */
30798 maybe_begin_member_template_processing (member_function);
30799
30800 /* If the body of the function has not yet been parsed, parse it
30801 now. */
30802 if (DECL_PENDING_INLINE_P (member_function))
30803 {
30804 tree function_scope;
30805 cp_token_cache *tokens;
30806
30807 /* The function is no longer pending; we are processing it. */
30808 tokens = DECL_PENDING_INLINE_INFO (member_function);
30809 DECL_PENDING_INLINE_INFO (member_function) = NULL;
30810 DECL_PENDING_INLINE_P (member_function) = 0;
30811
30812 /* If this is a local class, enter the scope of the containing
30813 function. */
30814 function_scope = current_function_decl;
30815 if (function_scope)
30816 push_function_context ();
30817
30818 /* Push the body of the function onto the lexer stack. */
30819 cp_parser_push_lexer_for_tokens (parser, tokens);
30820
30821 /* Let the front end know that we going to be defining this
30822 function. */
30823 start_preparsed_function (member_function, NULL_TREE,
30824 SF_PRE_PARSED | SF_INCLASS_INLINE);
30825
30826 /* Don't do access checking if it is a templated function. */
30827 if (processing_template_decl)
30828 push_deferring_access_checks (dk_no_check);
30829
30830 /* #pragma omp declare reduction needs special parsing. */
30831 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
30832 {
30833 parser->lexer->in_pragma = true;
30834 cp_parser_omp_declare_reduction_exprs (member_function, parser);
30835 finish_function (/*inline_p=*/true);
30836 cp_check_omp_declare_reduction (member_function);
30837 }
30838 else
30839 /* Now, parse the body of the function. */
30840 cp_parser_function_definition_after_declarator (parser,
30841 /*inline_p=*/true);
30842
30843 if (processing_template_decl)
30844 pop_deferring_access_checks ();
30845
30846 /* Leave the scope of the containing function. */
30847 if (function_scope)
30848 pop_function_context ();
30849 cp_parser_pop_lexer (parser);
30850 }
30851
30852 /* Remove any template parameters from the symbol table. */
30853 maybe_end_member_template_processing ();
30854
30855 /* Restore the queue. */
30856 pop_unparsed_function_queues (parser);
30857 timevar_pop (TV_PARSE_INMETH);
30858 }
30859
30860 /* If DECL contains any default args, remember it on the unparsed
30861 functions queue. */
30862
30863 static void
30864 cp_parser_save_default_args (cp_parser* parser, tree decl)
30865 {
30866 tree probe;
30867
30868 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
30869 probe;
30870 probe = TREE_CHAIN (probe))
30871 if (TREE_PURPOSE (probe))
30872 {
30873 cp_default_arg_entry entry = {current_class_type, decl};
30874 vec_safe_push (unparsed_funs_with_default_args, entry);
30875 break;
30876 }
30877
30878 /* Remember if there is a noexcept-specifier to post process. */
30879 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
30880 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
30881 vec_safe_push (unparsed_noexcepts, decl);
30882 }
30883
30884 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
30885 which is either a FIELD_DECL or PARM_DECL. Parse it and return
30886 the result. For a PARM_DECL, PARMTYPE is the corresponding type
30887 from the parameter-type-list. */
30888
30889 static tree
30890 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
30891 tree default_arg, tree parmtype)
30892 {
30893 cp_token_cache *tokens;
30894 tree parsed_arg;
30895 bool dummy;
30896
30897 if (default_arg == error_mark_node)
30898 return error_mark_node;
30899
30900 /* Push the saved tokens for the default argument onto the parser's
30901 lexer stack. */
30902 tokens = DEFPARSE_TOKENS (default_arg);
30903 cp_parser_push_lexer_for_tokens (parser, tokens);
30904
30905 start_lambda_scope (decl);
30906
30907 /* Parse the default argument. */
30908 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
30909 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
30910 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
30911
30912 finish_lambda_scope ();
30913
30914 if (parsed_arg == error_mark_node)
30915 cp_parser_skip_to_end_of_statement (parser);
30916
30917 if (!processing_template_decl)
30918 {
30919 /* In a non-template class, check conversions now. In a template,
30920 we'll wait and instantiate these as needed. */
30921 if (TREE_CODE (decl) == PARM_DECL)
30922 parsed_arg = check_default_argument (parmtype, parsed_arg,
30923 tf_warning_or_error);
30924 else if (maybe_reject_flexarray_init (decl, parsed_arg))
30925 parsed_arg = error_mark_node;
30926 else
30927 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
30928 }
30929
30930 /* If the token stream has not been completely used up, then
30931 there was extra junk after the end of the default
30932 argument. */
30933 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30934 {
30935 if (TREE_CODE (decl) == PARM_DECL)
30936 cp_parser_error (parser, "expected %<,%>");
30937 else
30938 cp_parser_error (parser, "expected %<;%>");
30939 }
30940
30941 /* Revert to the main lexer. */
30942 cp_parser_pop_lexer (parser);
30943
30944 return parsed_arg;
30945 }
30946
30947 /* FIELD is a non-static data member with an initializer which we saved for
30948 later; parse it now. */
30949
30950 static void
30951 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
30952 {
30953 tree def;
30954
30955 maybe_begin_member_template_processing (field);
30956
30957 push_unparsed_function_queues (parser);
30958 def = cp_parser_late_parse_one_default_arg (parser, field,
30959 DECL_INITIAL (field),
30960 NULL_TREE);
30961 pop_unparsed_function_queues (parser);
30962
30963 maybe_end_member_template_processing ();
30964
30965 DECL_INITIAL (field) = def;
30966 }
30967
30968 /* FN is a FUNCTION_DECL which may contains a parameter with an
30969 unparsed DEFERRED_PARSE. Parse the default args now. This function
30970 assumes that the current scope is the scope in which the default
30971 argument should be processed. */
30972
30973 static void
30974 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
30975 {
30976 unsigned char saved_local_variables_forbidden_p;
30977
30978 /* While we're parsing the default args, we might (due to the
30979 statement expression extension) encounter more classes. We want
30980 to handle them right away, but we don't want them getting mixed
30981 up with default args that are currently in the queue. */
30982 push_unparsed_function_queues (parser);
30983
30984 /* Local variable names (and the `this' keyword) may not appear
30985 in a default argument. */
30986 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
30987 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
30988
30989 push_defarg_context (fn);
30990
30991 begin_scope (sk_function_parms, fn);
30992
30993 /* Gather the PARM_DECLs into a vec so we can keep track of them when
30994 pushdecl clears DECL_CHAIN. */
30995 releasing_vec parms;
30996 for (tree parmdecl = DECL_ARGUMENTS (fn); parmdecl;
30997 parmdecl = DECL_CHAIN (parmdecl))
30998 vec_safe_push (parms, parmdecl);
30999
31000 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
31001 for (int i = 0;
31002 parm && parm != void_list_node;
31003 parm = TREE_CHAIN (parm),
31004 ++i)
31005 {
31006 tree default_arg = TREE_PURPOSE (parm);
31007 tree parsed_arg;
31008
31009 tree parmdecl = parms[i];
31010 pushdecl (parmdecl);
31011
31012 if (!default_arg)
31013 continue;
31014
31015 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
31016 /* This can happen for a friend declaration for a function
31017 already declared with default arguments. */
31018 continue;
31019
31020 parsed_arg
31021 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
31022 default_arg,
31023 TREE_VALUE (parm));
31024 TREE_PURPOSE (parm) = parsed_arg;
31025
31026 /* Update any instantiations we've already created. */
31027 for (tree copy : DEFPARSE_INSTANTIATIONS (default_arg))
31028 TREE_PURPOSE (copy) = parsed_arg;
31029 }
31030
31031 pop_bindings_and_leave_scope ();
31032
31033 /* Restore DECL_CHAINs after clobbering by pushdecl. */
31034 parm = NULL_TREE;
31035 for (int i = parms->length () - 1; i >= 0; --i)
31036 {
31037 DECL_CHAIN (parms[i]) = parm;
31038 parm = parms[i];
31039 }
31040
31041 pop_defarg_context ();
31042
31043 /* Make sure no default arg is missing. */
31044 check_default_args (fn);
31045
31046 /* Restore the state of local_variables_forbidden_p. */
31047 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
31048
31049 /* Restore the queue. */
31050 pop_unparsed_function_queues (parser);
31051 }
31052
31053 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
31054
31055 sizeof ... ( identifier )
31056
31057 where the 'sizeof' token has already been consumed. */
31058
31059 static tree
31060 cp_parser_sizeof_pack (cp_parser *parser)
31061 {
31062 /* Consume the `...'. */
31063 cp_lexer_consume_token (parser->lexer);
31064 maybe_warn_variadic_templates ();
31065
31066 matching_parens parens;
31067 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
31068 if (paren)
31069 parens.consume_open (parser);
31070 else
31071 permerror (cp_lexer_peek_token (parser->lexer)->location,
31072 "%<sizeof...%> argument must be surrounded by parentheses");
31073
31074 cp_token *token = cp_lexer_peek_token (parser->lexer);
31075 tree name = cp_parser_identifier (parser);
31076 if (name == error_mark_node)
31077 return error_mark_node;
31078 /* The name is not qualified. */
31079 parser->scope = NULL_TREE;
31080 parser->qualifying_scope = NULL_TREE;
31081 parser->object_scope = NULL_TREE;
31082 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
31083 if (expr == error_mark_node)
31084 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
31085 token->location);
31086 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
31087 expr = TREE_TYPE (expr);
31088 else if (TREE_CODE (expr) == CONST_DECL)
31089 expr = DECL_INITIAL (expr);
31090 expr = make_pack_expansion (expr);
31091 PACK_EXPANSION_SIZEOF_P (expr) = true;
31092
31093 if (paren)
31094 parens.require_close (parser);
31095
31096 return expr;
31097 }
31098
31099 /* Parse the operand of `sizeof' (or a similar operator). Returns
31100 either a TYPE or an expression, depending on the form of the
31101 input. The KEYWORD indicates which kind of expression we have
31102 encountered. */
31103
31104 static tree
31105 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
31106 {
31107 tree expr = NULL_TREE;
31108 const char *saved_message;
31109 const char *saved_message_arg;
31110 bool saved_integral_constant_expression_p;
31111 bool saved_non_integral_constant_expression_p;
31112
31113 /* If it's a `...', then we are computing the length of a parameter
31114 pack. */
31115 if (keyword == RID_SIZEOF
31116 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31117 return cp_parser_sizeof_pack (parser);
31118
31119 /* Types cannot be defined in a `sizeof' expression. Save away the
31120 old message. */
31121 saved_message = parser->type_definition_forbidden_message;
31122 saved_message_arg = parser->type_definition_forbidden_message_arg;
31123 parser->type_definition_forbidden_message
31124 = G_("types may not be defined in %qs expressions");
31125 parser->type_definition_forbidden_message_arg
31126 = IDENTIFIER_POINTER (ridpointers[keyword]);
31127
31128 /* The restrictions on constant-expressions do not apply inside
31129 sizeof expressions. */
31130 saved_integral_constant_expression_p
31131 = parser->integral_constant_expression_p;
31132 saved_non_integral_constant_expression_p
31133 = parser->non_integral_constant_expression_p;
31134 parser->integral_constant_expression_p = false;
31135
31136 /* Do not actually evaluate the expression. */
31137 ++cp_unevaluated_operand;
31138 ++c_inhibit_evaluation_warnings;
31139 /* If it's a `(', then we might be looking at the type-id
31140 construction. */
31141 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31142 {
31143 tree type = NULL_TREE;
31144
31145 tentative_firewall firewall (parser);
31146
31147 /* We can't be sure yet whether we're looking at a type-id or an
31148 expression. */
31149 cp_parser_parse_tentatively (parser);
31150
31151 matching_parens parens;
31152 parens.consume_open (parser);
31153
31154 /* Note: as a GNU Extension, compound literals are considered
31155 postfix-expressions as they are in C99, so they are valid
31156 arguments to sizeof. See comment in cp_parser_cast_expression
31157 for details. */
31158 if (cp_parser_compound_literal_p (parser))
31159 cp_parser_simulate_error (parser);
31160 else
31161 {
31162 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
31163 parser->in_type_id_in_expr_p = true;
31164 /* Look for the type-id. */
31165 type = cp_parser_type_id (parser);
31166 /* Look for the closing `)'. */
31167 parens.require_close (parser);
31168 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
31169 }
31170
31171 /* If all went well, then we're done. */
31172 if (cp_parser_parse_definitely (parser))
31173 expr = type;
31174 else
31175 {
31176 /* Commit to the tentative_firewall so we get syntax errors. */
31177 cp_parser_commit_to_tentative_parse (parser);
31178
31179 expr = cp_parser_unary_expression (parser);
31180 }
31181 }
31182 else
31183 expr = cp_parser_unary_expression (parser);
31184
31185 /* Go back to evaluating expressions. */
31186 --cp_unevaluated_operand;
31187 --c_inhibit_evaluation_warnings;
31188
31189 /* And restore the old one. */
31190 parser->type_definition_forbidden_message = saved_message;
31191 parser->type_definition_forbidden_message_arg = saved_message_arg;
31192 parser->integral_constant_expression_p
31193 = saved_integral_constant_expression_p;
31194 parser->non_integral_constant_expression_p
31195 = saved_non_integral_constant_expression_p;
31196
31197 return expr;
31198 }
31199
31200 /* If the current declaration has no declarator, return true. */
31201
31202 static bool
31203 cp_parser_declares_only_class_p (cp_parser *parser)
31204 {
31205 /* If the next token is a `;' or a `,' then there is no
31206 declarator. */
31207 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
31208 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
31209 }
31210
31211 /* Update the DECL_SPECS to reflect the storage class indicated by
31212 KEYWORD. */
31213
31214 static void
31215 cp_parser_set_storage_class (cp_parser *parser,
31216 cp_decl_specifier_seq *decl_specs,
31217 enum rid keyword,
31218 cp_token *token)
31219 {
31220 cp_storage_class storage_class;
31221
31222 if (parser->in_unbraced_linkage_specification_p)
31223 {
31224 error_at (token->location, "invalid use of %qD in linkage specification",
31225 ridpointers[keyword]);
31226 return;
31227 }
31228 else if (decl_specs->storage_class != sc_none)
31229 {
31230 decl_specs->conflicting_specifiers_p = true;
31231 return;
31232 }
31233
31234 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
31235 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
31236 && decl_specs->gnu_thread_keyword_p)
31237 {
31238 pedwarn (decl_specs->locations[ds_thread], 0,
31239 "%<__thread%> before %qD", ridpointers[keyword]);
31240 }
31241
31242 switch (keyword)
31243 {
31244 case RID_AUTO:
31245 storage_class = sc_auto;
31246 break;
31247 case RID_REGISTER:
31248 storage_class = sc_register;
31249 break;
31250 case RID_STATIC:
31251 storage_class = sc_static;
31252 break;
31253 case RID_EXTERN:
31254 storage_class = sc_extern;
31255 break;
31256 case RID_MUTABLE:
31257 storage_class = sc_mutable;
31258 break;
31259 default:
31260 gcc_unreachable ();
31261 }
31262 decl_specs->storage_class = storage_class;
31263 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
31264
31265 /* A storage class specifier cannot be applied alongside a typedef
31266 specifier. If there is a typedef specifier present then set
31267 conflicting_specifiers_p which will trigger an error later
31268 on in grokdeclarator. */
31269 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
31270 decl_specs->conflicting_specifiers_p = true;
31271 }
31272
31273 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
31274 is true, the type is a class or enum definition. */
31275
31276 static void
31277 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
31278 tree type_spec,
31279 cp_token *token,
31280 bool type_definition_p)
31281 {
31282 decl_specs->any_specifiers_p = true;
31283
31284 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
31285 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
31286 this is what happened. In system headers, we ignore these
31287 declarations so that G++ can work with system headers that are not
31288 C++-safe. */
31289 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
31290 && !type_definition_p
31291 && (type_spec == boolean_type_node
31292 || type_spec == char8_type_node
31293 || type_spec == char16_type_node
31294 || type_spec == char32_type_node
31295 || type_spec == wchar_type_node)
31296 && (decl_specs->type
31297 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
31298 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
31299 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
31300 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
31301 {
31302 decl_specs->redefined_builtin_type = type_spec;
31303 set_and_check_decl_spec_loc (decl_specs,
31304 ds_redefined_builtin_type_spec,
31305 token);
31306 if (!decl_specs->type)
31307 {
31308 decl_specs->type = type_spec;
31309 decl_specs->type_definition_p = false;
31310 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
31311 }
31312 }
31313 else if (decl_specs->type)
31314 decl_specs->multiple_types_p = true;
31315 else
31316 {
31317 decl_specs->type = type_spec;
31318 decl_specs->type_definition_p = type_definition_p;
31319 decl_specs->redefined_builtin_type = NULL_TREE;
31320 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
31321 }
31322 }
31323
31324 /* True iff TOKEN is the GNU keyword __thread. */
31325
31326 static bool
31327 token_is__thread (cp_token *token)
31328 {
31329 gcc_assert (token->keyword == RID_THREAD);
31330 return id_equal (token->u.value, "__thread");
31331 }
31332
31333 /* Set the location for a declarator specifier and check if it is
31334 duplicated.
31335
31336 DECL_SPECS is the sequence of declarator specifiers onto which to
31337 set the location.
31338
31339 DS is the single declarator specifier to set which location is to
31340 be set onto the existing sequence of declarators.
31341
31342 LOCATION is the location for the declarator specifier to
31343 consider. */
31344
31345 static void
31346 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
31347 cp_decl_spec ds, cp_token *token)
31348 {
31349 gcc_assert (ds < ds_last);
31350
31351 if (decl_specs == NULL)
31352 return;
31353
31354 location_t location = token->location;
31355
31356 if (decl_specs->locations[ds] == 0)
31357 {
31358 decl_specs->locations[ds] = location;
31359 if (ds == ds_thread)
31360 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
31361 }
31362 else
31363 {
31364 if (ds == ds_long)
31365 {
31366 if (decl_specs->locations[ds_long_long] != 0)
31367 error_at (location,
31368 "%<long long long%> is too long for GCC");
31369 else
31370 {
31371 decl_specs->locations[ds_long_long] = location;
31372 pedwarn_cxx98 (location,
31373 OPT_Wlong_long,
31374 "ISO C++ 1998 does not support %<long long%>");
31375 }
31376 }
31377 else if (ds == ds_thread)
31378 {
31379 bool gnu = token_is__thread (token);
31380 gcc_rich_location richloc (location);
31381 if (gnu != decl_specs->gnu_thread_keyword_p)
31382 {
31383 richloc.add_range (decl_specs->locations[ds_thread]);
31384 error_at (&richloc,
31385 "both %<__thread%> and %<thread_local%> specified");
31386 }
31387 else
31388 {
31389 richloc.add_fixit_remove ();
31390 error_at (&richloc, "duplicate %qD", token->u.value);
31391 }
31392 }
31393 else
31394 {
31395 static const char *const decl_spec_names[] = {
31396 "signed",
31397 "unsigned",
31398 "short",
31399 "long",
31400 "const",
31401 "volatile",
31402 "restrict",
31403 "inline",
31404 "virtual",
31405 "explicit",
31406 "friend",
31407 "typedef",
31408 "using",
31409 "constexpr",
31410 "__complex",
31411 "constinit",
31412 "consteval"
31413 };
31414 gcc_rich_location richloc (location);
31415 richloc.add_fixit_remove ();
31416 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
31417 }
31418 }
31419 }
31420
31421 /* Return true iff the declarator specifier DS is present in the
31422 sequence of declarator specifiers DECL_SPECS. */
31423
31424 bool
31425 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
31426 cp_decl_spec ds)
31427 {
31428 gcc_assert (ds < ds_last);
31429
31430 if (decl_specs == NULL)
31431 return false;
31432
31433 return decl_specs->locations[ds] != 0;
31434 }
31435
31436 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
31437 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
31438
31439 static bool
31440 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
31441 {
31442 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
31443 }
31444
31445 /* Issue an error message indicating that TOKEN_DESC was expected.
31446 If KEYWORD is true, it indicated this function is called by
31447 cp_parser_require_keword and the required token can only be
31448 a indicated keyword.
31449
31450 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
31451 within any error as the location of an "opening" token matching
31452 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
31453 RT_CLOSE_PAREN). */
31454
31455 static void
31456 cp_parser_required_error (cp_parser *parser,
31457 required_token token_desc,
31458 bool keyword,
31459 location_t matching_location)
31460 {
31461 if (cp_parser_simulate_error (parser))
31462 return;
31463
31464 const char *gmsgid = NULL;
31465 switch (token_desc)
31466 {
31467 case RT_NEW:
31468 gmsgid = G_("expected %<new%>");
31469 break;
31470 case RT_DELETE:
31471 gmsgid = G_("expected %<delete%>");
31472 break;
31473 case RT_RETURN:
31474 gmsgid = G_("expected %<return%>");
31475 break;
31476 case RT_WHILE:
31477 gmsgid = G_("expected %<while%>");
31478 break;
31479 case RT_EXTERN:
31480 gmsgid = G_("expected %<extern%>");
31481 break;
31482 case RT_STATIC_ASSERT:
31483 gmsgid = G_("expected %<static_assert%>");
31484 break;
31485 case RT_DECLTYPE:
31486 gmsgid = G_("expected %<decltype%>");
31487 break;
31488 case RT_OPERATOR:
31489 gmsgid = G_("expected %<operator%>");
31490 break;
31491 case RT_CLASS:
31492 gmsgid = G_("expected %<class%>");
31493 break;
31494 case RT_TEMPLATE:
31495 gmsgid = G_("expected %<template%>");
31496 break;
31497 case RT_NAMESPACE:
31498 gmsgid = G_("expected %<namespace%>");
31499 break;
31500 case RT_USING:
31501 gmsgid = G_("expected %<using%>");
31502 break;
31503 case RT_ASM:
31504 gmsgid = G_("expected %<asm%>");
31505 break;
31506 case RT_TRY:
31507 gmsgid = G_("expected %<try%>");
31508 break;
31509 case RT_CATCH:
31510 gmsgid = G_("expected %<catch%>");
31511 break;
31512 case RT_THROW:
31513 gmsgid = G_("expected %<throw%>");
31514 break;
31515 case RT_AUTO:
31516 gmsgid = G_("expected %<auto%>");
31517 break;
31518 case RT_LABEL:
31519 gmsgid = G_("expected %<__label__%>");
31520 break;
31521 case RT_AT_TRY:
31522 gmsgid = G_("expected %<@try%>");
31523 break;
31524 case RT_AT_SYNCHRONIZED:
31525 gmsgid = G_("expected %<@synchronized%>");
31526 break;
31527 case RT_AT_THROW:
31528 gmsgid = G_("expected %<@throw%>");
31529 break;
31530 case RT_TRANSACTION_ATOMIC:
31531 gmsgid = G_("expected %<__transaction_atomic%>");
31532 break;
31533 case RT_TRANSACTION_RELAXED:
31534 gmsgid = G_("expected %<__transaction_relaxed%>");
31535 break;
31536 case RT_CO_YIELD:
31537 gmsgid = G_("expected %<co_yield%>");
31538 break;
31539 default:
31540 break;
31541 }
31542
31543 if (!gmsgid && !keyword)
31544 {
31545 switch (token_desc)
31546 {
31547 case RT_SEMICOLON:
31548 gmsgid = G_("expected %<;%>");
31549 break;
31550 case RT_OPEN_PAREN:
31551 gmsgid = G_("expected %<(%>");
31552 break;
31553 case RT_CLOSE_BRACE:
31554 gmsgid = G_("expected %<}%>");
31555 break;
31556 case RT_OPEN_BRACE:
31557 gmsgid = G_("expected %<{%>");
31558 break;
31559 case RT_CLOSE_SQUARE:
31560 gmsgid = G_("expected %<]%>");
31561 break;
31562 case RT_OPEN_SQUARE:
31563 gmsgid = G_("expected %<[%>");
31564 break;
31565 case RT_COMMA:
31566 gmsgid = G_("expected %<,%>");
31567 break;
31568 case RT_SCOPE:
31569 gmsgid = G_("expected %<::%>");
31570 break;
31571 case RT_LESS:
31572 gmsgid = G_("expected %<<%>");
31573 break;
31574 case RT_GREATER:
31575 gmsgid = G_("expected %<>%>");
31576 break;
31577 case RT_EQ:
31578 gmsgid = G_("expected %<=%>");
31579 break;
31580 case RT_ELLIPSIS:
31581 gmsgid = G_("expected %<...%>");
31582 break;
31583 case RT_MULT:
31584 gmsgid = G_("expected %<*%>");
31585 break;
31586 case RT_COMPL:
31587 gmsgid = G_("expected %<~%>");
31588 break;
31589 case RT_COLON:
31590 gmsgid = G_("expected %<:%>");
31591 break;
31592 case RT_COLON_SCOPE:
31593 gmsgid = G_("expected %<:%> or %<::%>");
31594 break;
31595 case RT_CLOSE_PAREN:
31596 gmsgid = G_("expected %<)%>");
31597 break;
31598 case RT_COMMA_CLOSE_PAREN:
31599 gmsgid = G_("expected %<,%> or %<)%>");
31600 break;
31601 case RT_PRAGMA_EOL:
31602 gmsgid = G_("expected end of line");
31603 break;
31604 case RT_NAME:
31605 gmsgid = G_("expected identifier");
31606 break;
31607 case RT_SELECT:
31608 gmsgid = G_("expected selection-statement");
31609 break;
31610 case RT_ITERATION:
31611 gmsgid = G_("expected iteration-statement");
31612 break;
31613 case RT_JUMP:
31614 gmsgid = G_("expected jump-statement");
31615 break;
31616 case RT_CLASS_KEY:
31617 gmsgid = G_("expected class-key");
31618 break;
31619 case RT_CLASS_TYPENAME_TEMPLATE:
31620 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
31621 break;
31622 default:
31623 gcc_unreachable ();
31624 }
31625 }
31626
31627 if (gmsgid)
31628 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
31629 }
31630
31631
31632 /* If the next token is of the indicated TYPE, consume it. Otherwise,
31633 issue an error message indicating that TOKEN_DESC was expected.
31634
31635 Returns the token consumed, if the token had the appropriate type.
31636 Otherwise, returns NULL.
31637
31638 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
31639 within any error as the location of an "opening" token matching
31640 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
31641 RT_CLOSE_PAREN). */
31642
31643 static cp_token *
31644 cp_parser_require (cp_parser* parser,
31645 enum cpp_ttype type,
31646 required_token token_desc,
31647 location_t matching_location)
31648 {
31649 if (cp_lexer_next_token_is (parser->lexer, type))
31650 return cp_lexer_consume_token (parser->lexer);
31651 else
31652 {
31653 /* Output the MESSAGE -- unless we're parsing tentatively. */
31654 if (!cp_parser_simulate_error (parser))
31655 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
31656 matching_location);
31657 return NULL;
31658 }
31659 }
31660
31661 /* An error message is produced if the next token is not '>'.
31662 All further tokens are skipped until the desired token is
31663 found or '{', '}', ';' or an unbalanced ')' or ']'. */
31664
31665 static void
31666 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
31667 {
31668 /* Current level of '< ... >'. */
31669 unsigned level = 0;
31670 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
31671 unsigned nesting_depth = 0;
31672
31673 /* Are we ready, yet? If not, issue error message. */
31674 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
31675 return;
31676
31677 /* Skip tokens until the desired token is found. */
31678 while (true)
31679 {
31680 /* Peek at the next token. */
31681 switch (cp_lexer_peek_token (parser->lexer)->type)
31682 {
31683 case CPP_LESS:
31684 if (!nesting_depth)
31685 ++level;
31686 break;
31687
31688 case CPP_RSHIFT:
31689 if (cxx_dialect == cxx98)
31690 /* C++0x views the `>>' operator as two `>' tokens, but
31691 C++98 does not. */
31692 break;
31693 else if (!nesting_depth && level-- == 0)
31694 {
31695 /* We've hit a `>>' where the first `>' closes the
31696 template argument list, and the second `>' is
31697 spurious. Just consume the `>>' and stop; we've
31698 already produced at least one error. */
31699 cp_lexer_consume_token (parser->lexer);
31700 return;
31701 }
31702 /* Fall through for C++0x, so we handle the second `>' in
31703 the `>>'. */
31704 gcc_fallthrough ();
31705
31706 case CPP_GREATER:
31707 if (!nesting_depth && level-- == 0)
31708 {
31709 /* We've reached the token we want, consume it and stop. */
31710 cp_lexer_consume_token (parser->lexer);
31711 return;
31712 }
31713 break;
31714
31715 case CPP_OPEN_PAREN:
31716 case CPP_OPEN_SQUARE:
31717 ++nesting_depth;
31718 break;
31719
31720 case CPP_CLOSE_PAREN:
31721 case CPP_CLOSE_SQUARE:
31722 if (nesting_depth-- == 0)
31723 return;
31724 break;
31725
31726 case CPP_EOF:
31727 case CPP_PRAGMA_EOL:
31728 case CPP_SEMICOLON:
31729 case CPP_OPEN_BRACE:
31730 case CPP_CLOSE_BRACE:
31731 /* The '>' was probably forgotten, don't look further. */
31732 return;
31733
31734 default:
31735 break;
31736 }
31737
31738 /* Consume this token. */
31739 cp_lexer_consume_token (parser->lexer);
31740 }
31741 }
31742
31743 /* If the next token is the indicated keyword, consume it. Otherwise,
31744 issue an error message indicating that TOKEN_DESC was expected.
31745
31746 Returns the token consumed, if the token had the appropriate type.
31747 Otherwise, returns NULL. */
31748
31749 static cp_token *
31750 cp_parser_require_keyword (cp_parser* parser,
31751 enum rid keyword,
31752 required_token token_desc)
31753 {
31754 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
31755
31756 if (token && token->keyword != keyword)
31757 {
31758 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
31759 UNKNOWN_LOCATION);
31760 return NULL;
31761 }
31762
31763 return token;
31764 }
31765
31766 /* Returns TRUE iff TOKEN is a token that can begin the body of a
31767 function-definition. */
31768
31769 static bool
31770 cp_parser_token_starts_function_definition_p (cp_token* token)
31771 {
31772 return (/* An ordinary function-body begins with an `{'. */
31773 token->type == CPP_OPEN_BRACE
31774 /* A ctor-initializer begins with a `:'. */
31775 || token->type == CPP_COLON
31776 /* A function-try-block begins with `try'. */
31777 || token->keyword == RID_TRY
31778 /* A function-transaction-block begins with `__transaction_atomic'
31779 or `__transaction_relaxed'. */
31780 || token->keyword == RID_TRANSACTION_ATOMIC
31781 || token->keyword == RID_TRANSACTION_RELAXED
31782 /* The named return value extension begins with `return'. */
31783 || token->keyword == RID_RETURN);
31784 }
31785
31786 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
31787 definition. */
31788
31789 static bool
31790 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
31791 {
31792 cp_token *token;
31793
31794 token = cp_lexer_peek_token (parser->lexer);
31795 return (token->type == CPP_OPEN_BRACE
31796 || (token->type == CPP_COLON
31797 && !parser->colon_doesnt_start_class_def_p));
31798 }
31799
31800 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
31801 C++0x) ending a template-argument. */
31802
31803 static bool
31804 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
31805 {
31806 cp_token *token;
31807
31808 token = cp_lexer_peek_token (parser->lexer);
31809 return (token->type == CPP_COMMA
31810 || token->type == CPP_GREATER
31811 || token->type == CPP_ELLIPSIS
31812 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
31813 }
31814
31815 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
31816 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
31817
31818 static bool
31819 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
31820 size_t n)
31821 {
31822 cp_token *token;
31823
31824 token = cp_lexer_peek_nth_token (parser->lexer, n);
31825 if (token->type == CPP_LESS)
31826 return true;
31827 /* Check for the sequence `<::' in the original code. It would be lexed as
31828 `[:', where `[' is a digraph, and there is no whitespace before
31829 `:'. */
31830 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
31831 {
31832 cp_token *token2;
31833 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
31834 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
31835 return true;
31836 }
31837 return false;
31838 }
31839
31840 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
31841 or none_type otherwise. */
31842
31843 static enum tag_types
31844 cp_parser_token_is_class_key (cp_token* token)
31845 {
31846 switch (token->keyword)
31847 {
31848 case RID_CLASS:
31849 return class_type;
31850 case RID_STRUCT:
31851 return record_type;
31852 case RID_UNION:
31853 return union_type;
31854
31855 default:
31856 return none_type;
31857 }
31858 }
31859
31860 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
31861 or none_type otherwise or if the token is null. */
31862
31863 static enum tag_types
31864 cp_parser_token_is_type_parameter_key (cp_token* token)
31865 {
31866 if (!token)
31867 return none_type;
31868
31869 switch (token->keyword)
31870 {
31871 case RID_CLASS:
31872 return class_type;
31873 case RID_TYPENAME:
31874 return typename_type;
31875
31876 default:
31877 return none_type;
31878 }
31879 }
31880
31881 /* Diagnose redundant enum-keys. */
31882
31883 static void
31884 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
31885 tree type, rid scoped_key)
31886 {
31887 if (!warn_redundant_tags)
31888 return;
31889
31890 tree type_decl = TYPE_MAIN_DECL (type);
31891 tree name = DECL_NAME (type_decl);
31892 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
31893 push_deferring_access_checks (dk_no_check);
31894 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
31895 pop_deferring_access_checks ();
31896
31897 /* The enum-key is redundant for uses of the TYPE that are not
31898 declarations and for which name lookup returns just the type
31899 itself. */
31900 if (decl != type_decl)
31901 return;
31902
31903 if (scoped_key != RID_CLASS
31904 && scoped_key != RID_STRUCT
31905 && current_lang_name != lang_name_cplusplus
31906 && current_namespace == global_namespace)
31907 {
31908 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
31909 enum tag in shared C/C++ code in files (such as headers) included
31910 in the main source file. */
31911 const line_map_ordinary *map = NULL;
31912 linemap_resolve_location (line_table, key_loc,
31913 LRK_MACRO_DEFINITION_LOCATION,
31914 &map);
31915 if (!MAIN_FILE_P (map))
31916 return;
31917 }
31918
31919 gcc_rich_location richloc (key_loc);
31920 richloc.add_fixit_remove (key_loc);
31921 warning_at (&richloc, OPT_Wredundant_tags,
31922 "redundant enum-key %<enum%s%> in reference to %q#T",
31923 (scoped_key == RID_CLASS ? " class"
31924 : scoped_key == RID_STRUCT ? " struct" : ""), type);
31925 }
31926
31927 /* Describes the set of declarations of a struct, class, or class template
31928 or its specializations. Used for -Wmismatched-tags. */
31929
31930 class class_decl_loc_t
31931 {
31932 public:
31933
31934 class_decl_loc_t ()
31935 : locvec (), idxdef (), def_class_key ()
31936 {
31937 locvec.create (4);
31938 }
31939
31940 /* Constructs an object for a single declaration of a class with
31941 CLASS_KEY at the current location in the current function (or
31942 at another scope). KEY_REDUNDANT is true if the class-key may
31943 be omitted in the current context without an ambiguity with
31944 another symbol with the same name.
31945 DEF_P is true for a class declaration that is a definition.
31946 CURLOC is the associated location. */
31947 class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
31948 location_t curloc = input_location)
31949 : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
31950 {
31951 locvec.create (4);
31952 class_key_loc_t ckl (current_function_decl, curloc, class_key,
31953 key_redundant);
31954 locvec.quick_push (ckl);
31955 }
31956
31957 /* Copy, assign, and destroy the object. Necessary because LOCVEC
31958 isn't safely copyable and assignable and doesn't release storage
31959 on its own. */
31960 class_decl_loc_t (const class_decl_loc_t &rhs)
31961 : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
31962 def_class_key (rhs.def_class_key)
31963 { }
31964
31965 class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
31966 {
31967 if (this == &rhs)
31968 return *this;
31969 locvec.release ();
31970 locvec = rhs.locvec.copy ();
31971 idxdef = rhs.idxdef;
31972 def_class_key = rhs.def_class_key;
31973 return *this;
31974 }
31975
31976 ~class_decl_loc_t ()
31977 {
31978 locvec.release ();
31979 }
31980
31981 /* Issues -Wmismatched-tags for a single class. */
31982 void diag_mismatched_tags (tree);
31983
31984 /* Issues -Wmismatched-tags for all classes. */
31985 static void diag_mismatched_tags ();
31986
31987 /* Adds TYPE_DECL to the collection of class decls and diagnoses
31988 redundant tags (if -Wredundant-tags is enabled). */
31989 static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
31990
31991 /* Either adds this decl to the collection of class decls
31992 or diagnoses it, whichever is appropriate. */
31993 void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
31994
31995 private:
31996
31997 tree function (unsigned i) const
31998 {
31999 return locvec[i].func;
32000 }
32001
32002 location_t location (unsigned i) const
32003 {
32004 return locvec[i].loc;
32005 }
32006
32007 bool key_redundant (unsigned i) const
32008 {
32009 return locvec[i].key_redundant;
32010 }
32011
32012 tag_types class_key (unsigned i) const
32013 {
32014 return locvec[i].class_key;
32015 }
32016
32017 /* True if a definition for the class has been seen. */
32018 bool def_p () const
32019 {
32020 return idxdef < locvec.length ();
32021 }
32022
32023 /* The location of a single mention of a class type with the given
32024 class-key. */
32025 struct class_key_loc_t
32026 {
32027 class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
32028 : func (func), loc (loc), class_key (key), key_redundant (redundant)
32029 { }
32030
32031 /* The function the type is mentioned in. */
32032 tree func;
32033 /* The exact location. */
32034 location_t loc;
32035 /* The class-key used in the mention of the type. */
32036 tag_types class_key;
32037 /* True when the class-key could be omitted at this location
32038 without an ambiguity with another symbol of the same name. */
32039 bool key_redundant;
32040 };
32041 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
32042 vec <class_key_loc_t> locvec;
32043 /* LOCVEC index of the definition or UINT_MAX if none exists. */
32044 unsigned idxdef;
32045 /* The class-key the class was last declared with or none_type when
32046 it has been declared with a mismatched key. */
32047 tag_types def_class_key;
32048
32049 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
32050 description above. */
32051 typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
32052 static class_to_loc_map_t class2loc;
32053 };
32054
32055 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
32056
32057 /* Issue an error message if the CLASS_KEY does not match the TYPE.
32058 DEF_P is expected to be set for a definition of class TYPE. DECL_P
32059 is set for a declaration of class TYPE and clear for a reference to
32060 it that is not a declaration of it. */
32061
32062 static void
32063 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
32064 tag_types class_key, tree type, bool def_p,
32065 bool decl_p)
32066 {
32067 if (type == error_mark_node)
32068 return;
32069
32070 bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
32071 if (seen_as_union != (class_key == union_type))
32072 {
32073 if (permerror (input_location, "%qs tag used in naming %q#T",
32074 class_key == union_type ? "union"
32075 : class_key == record_type ? "struct" : "class",
32076 type))
32077 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
32078 "%q#T was previously declared here", type);
32079 return;
32080 }
32081
32082 if (!warn_mismatched_tags && !warn_redundant_tags)
32083 return;
32084
32085 /* Only consider the true class-keys below and ignore typename_type,
32086 etc. that are not C++ class-keys. */
32087 if (class_key != class_type
32088 && class_key != record_type
32089 && class_key != union_type)
32090 return;
32091
32092 class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
32093 }
32094
32095 /* Returns the template or specialization of one to which the RECORD_TYPE
32096 TYPE corresponds. */
32097
32098 static tree
32099 specialization_of (tree type)
32100 {
32101 tree ret = type;
32102
32103 /* Determine the template or its partial specialization to which TYPE
32104 corresponds. */
32105 if (tree spec = most_specialized_partial_spec (type, tf_none))
32106 if (spec != error_mark_node)
32107 ret = TREE_TYPE (TREE_VALUE (spec));
32108
32109 if (ret == type)
32110 ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
32111
32112 return TYPE_MAIN_DECL (ret);
32113 }
32114
32115
32116 /* Adds the class TYPE to the collection of class decls and diagnoses
32117 redundant tags (if -Wredundant-tags is enabled).
32118 DEF_P is expected to be set for a definition of class TYPE. DECL_P
32119 is set for a (likely, based on syntactic context) declaration of class
32120 TYPE and clear for a reference to it that is not a declaration of it. */
32121
32122 void
32123 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
32124 tag_types class_key, tree type, bool def_p, bool decl_p)
32125 {
32126 tree type_decl = TYPE_MAIN_DECL (type);
32127 tree name = DECL_NAME (type_decl);
32128 /* Look up the NAME to see if it unambiguously refers to the TYPE
32129 and set KEY_REDUNDANT if so. */
32130 push_deferring_access_checks (dk_no_check);
32131 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
32132 pop_deferring_access_checks ();
32133
32134 /* The class-key is redundant for uses of the CLASS_TYPE that are
32135 neither definitions of it nor declarations, and for which name
32136 lookup returns just the type itself. */
32137 bool key_redundant = (!def_p && !decl_p
32138 && (decl == type_decl
32139 || TREE_CODE (decl) == TEMPLATE_DECL
32140 || TYPE_BEING_DEFINED (type)));
32141
32142 if (key_redundant
32143 && class_key != class_type
32144 && current_lang_name != lang_name_cplusplus
32145 && current_namespace == global_namespace)
32146 {
32147 /* Avoid issuing the diagnostic for apparently redundant struct
32148 and union class-keys in shared C/C++ code in files (such as
32149 headers) included in the main source file. */
32150 const line_map_ordinary *map = NULL;
32151 linemap_resolve_location (line_table, key_loc,
32152 LRK_MACRO_DEFINITION_LOCATION,
32153 &map);
32154 if (!MAIN_FILE_P (map))
32155 key_redundant = false;
32156 }
32157
32158 /* Set if a declaration of TYPE has previously been seen or if it must
32159 exist in a precompiled header. */
32160 bool exist;
32161 class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
32162 if (!exist)
32163 {
32164 tree type = TREE_TYPE (type_decl);
32165 if (def_p || !COMPLETE_TYPE_P (type))
32166 {
32167 /* TYPE_DECL is the first declaration or definition of the type
32168 (outside precompiled headers -- see below). Just create
32169 a new entry for it and return unless it's a declaration
32170 involving a template that may need to be diagnosed by
32171 -Wredundant-tags. */
32172 *rdl = class_decl_loc_t (class_key, false, def_p);
32173 if (TREE_CODE (decl) != TEMPLATE_DECL)
32174 return;
32175 }
32176 else
32177 {
32178 /* TYPE was previously defined in some unknown precompiled hdeader.
32179 Simply add a record of its definition at an unknown location and
32180 proceed below to add a reference to it at the current location.
32181 (Declarations in precompiled headers that are not definitions
32182 are ignored.) */
32183 tag_types def_key
32184 = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
32185 location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
32186 *rdl = class_decl_loc_t (def_key, false, true, def_loc);
32187 exist = true;
32188 }
32189 }
32190
32191 /* A prior declaration of TYPE_DECL has been seen. */
32192
32193 if (key_redundant)
32194 {
32195 gcc_rich_location richloc (key_loc);
32196 richloc.add_fixit_remove (key_loc);
32197 warning_at (&richloc, OPT_Wredundant_tags,
32198 "redundant class-key %qs in reference to %q#T",
32199 class_key == union_type ? "union"
32200 : class_key == record_type ? "struct" : "class",
32201 type);
32202 }
32203
32204 if (!exist)
32205 /* Do nothing if this is the first declaration of the type. */
32206 return;
32207
32208 if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
32209 /* Do nothing if the class-key in this declaration matches
32210 the definition. */
32211 return;
32212
32213 rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
32214 def_p);
32215 }
32216
32217 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
32218 of class decls or diagnoses it, whichever is appropriate. */
32219
32220 void
32221 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
32222 tag_types class_key,
32223 bool redundant,
32224 bool def_p)
32225 {
32226 /* Reset the CLASS_KEY associated with this type on mismatch.
32227 This is an optimization that lets the diagnostic code skip
32228 over classes that use the same class-key in all declarations. */
32229 if (def_class_key != class_key)
32230 def_class_key = none_type;
32231
32232 /* Set IDXDEF to the index of the vector corresponding to
32233 the definition. */
32234 if (def_p)
32235 idxdef = locvec.length ();
32236
32237 /* Append a record of this declaration to the vector. */
32238 class_key_loc_t ckl (current_function_decl, input_location, class_key,
32239 redundant);
32240 locvec.safe_push (ckl);
32241
32242 if (idxdef == UINT_MAX)
32243 return;
32244
32245 /* As a space optimization diagnose declarations of a class
32246 whose definition has been seen and purge the LOCVEC of
32247 all entries except the definition. */
32248 diag_mismatched_tags (type_decl);
32249 if (idxdef)
32250 {
32251 class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
32252 locvec.release ();
32253 locvec.reserve (2);
32254 locvec.safe_push (ent);
32255 idxdef = 0;
32256 }
32257 else
32258 /* Pop the entry pushed above for this declaration. */
32259 locvec.pop ();
32260 }
32261
32262 /* Issues -Wmismatched-tags for a single class. */
32263
32264 void
32265 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
32266 {
32267 if (!warn_mismatched_tags)
32268 return;
32269
32270 /* Number of uses of the class. */
32271 const unsigned ndecls = locvec.length ();
32272
32273 /* The class (or template) declaration guiding the decisions about
32274 the diagnostic. For ordinary classes it's the same as THIS. For
32275 uses of instantiations of templates other than their declarations
32276 it points to the record for the declaration of the corresponding
32277 primary template or partial specialization. */
32278 class_decl_loc_t *cdlguide = this;
32279
32280 tree type = TREE_TYPE (type_decl);
32281 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type))
32282 {
32283 /* For implicit instantiations of a primary template look up
32284 the primary or partial specialization and use it as
32285 the expected class-key rather than using the class-key of
32286 the first reference to the instantiation. The primary must
32287 be (and inevitably is) at index zero. */
32288 tree spec = specialization_of (type);
32289 cdlguide = class2loc.get (spec);
32290 gcc_assert (cdlguide != NULL);
32291 }
32292 else
32293 {
32294 /* Skip declarations that consistently use the same class-key. */
32295 if (def_class_key != none_type)
32296 return;
32297 }
32298
32299 /* Set if a definition for the class has been seen. */
32300 const bool def_p = cdlguide->def_p ();
32301
32302 /* The index of the declaration whose class-key this declaration
32303 is expected to match. It's either the class-key of the class
32304 definition if one exists or the first declaration otherwise. */
32305 const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
32306
32307 /* The class-key the class is expected to be declared with: it's
32308 either the key used in its definition or the first declaration
32309 if no definition has been provided.
32310 For implicit instantiations of a primary template it's
32311 the class-key used to declare the primary with. The primary
32312 must be at index zero. */
32313 const tag_types xpect_key = cdlguide->class_key (idxguide);
32314
32315 unsigned idx = 0;
32316 /* Advance IDX to the first declaration that either is not
32317 a definition or that doesn't match the first declaration
32318 if no definition is provided. */
32319 while (class_key (idx) == xpect_key)
32320 if (++idx == ndecls)
32321 return;
32322
32323 /* Save the current function before changing it below. */
32324 tree save_func = current_function_decl;
32325 /* Set the function declaration to print in diagnostic context. */
32326 current_function_decl = function (idx);
32327
32328 const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
32329 const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
32330
32331 location_t loc = location (idx);
32332 bool key_redundant_p = key_redundant (idx);
32333 auto_diagnostic_group d;
32334 /* Issue a warning for the first mismatched declaration.
32335 Avoid using "%#qT" since the class-key for the same type will
32336 be the same regardless of which one was used in the declaraion. */
32337 if (warning_at (loc, OPT_Wmismatched_tags,
32338 "%qT declared with a mismatched class-key %qs",
32339 type_decl, xmatchkstr))
32340 {
32341 /* Suggest how to avoid the warning for each instance since
32342 the guidance may be different depending on context. */
32343 inform (loc,
32344 (key_redundant_p
32345 ? G_("remove the class-key or replace it with %qs")
32346 : G_("replace the class-key with %qs")),
32347 xpectkstr);
32348
32349 /* Also point to the first declaration or definition that guided
32350 the decision to issue the warning above. */
32351 inform (cdlguide->location (idxguide),
32352 (def_p
32353 ? G_("%qT defined as %qs here")
32354 : G_("%qT first declared as %qs here")),
32355 type_decl, xpectkstr);
32356 }
32357
32358 /* Issue warnings for the remaining inconsistent declarations. */
32359 for (unsigned i = idx + 1; i != ndecls; ++i)
32360 {
32361 tag_types clskey = class_key (i);
32362 /* Skip over the declarations that match either the definition
32363 if one was provided or the first declaration. */
32364 if (clskey == xpect_key)
32365 continue;
32366
32367 loc = location (i);
32368 key_redundant_p = key_redundant (i);
32369 /* Set the function declaration to print in diagnostic context. */
32370 current_function_decl = function (i);
32371 if (warning_at (loc, OPT_Wmismatched_tags,
32372 "%qT declared with a mismatched class-key %qs",
32373 type_decl, xmatchkstr))
32374 /* Suggest how to avoid the warning for each instance since
32375 the guidance may be different depending on context. */
32376 inform (loc,
32377 (key_redundant_p
32378 ? G_("remove the class-key or replace it with %qs")
32379 : G_("replace the class-key with %qs")),
32380 xpectkstr);
32381 }
32382
32383 /* Restore the current function in case it was replaced above. */
32384 current_function_decl = save_func;
32385 }
32386
32387 /* Issues -Wmismatched-tags for all classes. Called at the end
32388 of processing a translation unit, after declarations of all class
32389 types and their uses have been recorded. */
32390
32391 void
32392 class_decl_loc_t::diag_mismatched_tags ()
32393 {
32394 /* CLASS2LOC should be empty if both -Wmismatched-tags and
32395 -Wredundant-tags are disabled. */
32396 gcc_assert (warn_mismatched_tags
32397 || warn_redundant_tags
32398 || class2loc.is_empty ());
32399
32400 /* Save the current function before changing on return. It should
32401 be null at this point. */
32402 temp_override<tree> cleanup (current_function_decl);
32403
32404 if (warn_mismatched_tags)
32405 {
32406 /* Iterate over the collected class/struct/template declarations. */
32407 typedef class_to_loc_map_t::iterator iter_t;
32408 for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
32409 {
32410 tree type_decl = (*it).first;
32411 class_decl_loc_t &recloc = (*it).second;
32412 recloc.diag_mismatched_tags (type_decl);
32413 }
32414 }
32415
32416 class2loc.empty ();
32417 }
32418
32419 /* Issue an error message if DECL is redeclared with different
32420 access than its original declaration [class.access.spec/3].
32421 This applies to nested classes, nested class templates and
32422 enumerations [class.mem/1]. */
32423
32424 static void
32425 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
32426 {
32427 if (!decl
32428 || (!CLASS_TYPE_P (TREE_TYPE (decl))
32429 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
32430 return;
32431
32432 if ((TREE_PRIVATE (decl)
32433 != (current_access_specifier == access_private_node))
32434 || (TREE_PROTECTED (decl)
32435 != (current_access_specifier == access_protected_node)))
32436 error_at (location, "%qD redeclared with different access", decl);
32437 }
32438
32439 /* Look for the `template' keyword, as a syntactic disambiguator.
32440 Return TRUE iff it is present, in which case it will be
32441 consumed. */
32442
32443 static bool
32444 cp_parser_optional_template_keyword (cp_parser *parser)
32445 {
32446 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
32447 {
32448 /* In C++98 the `template' keyword can only be used within templates;
32449 outside templates the parser can always figure out what is a
32450 template and what is not. In C++11, per the resolution of DR 468,
32451 `template' is allowed in cases where it is not strictly necessary. */
32452 if (!processing_template_decl
32453 && pedantic && cxx_dialect == cxx98)
32454 {
32455 cp_token *token = cp_lexer_peek_token (parser->lexer);
32456 pedwarn (token->location, OPT_Wpedantic,
32457 "in C++98 %<template%> (as a disambiguator) is only "
32458 "allowed within templates");
32459 /* If this part of the token stream is rescanned, the same
32460 error message would be generated. So, we purge the token
32461 from the stream. */
32462 cp_lexer_purge_token (parser->lexer);
32463 return false;
32464 }
32465 else
32466 {
32467 /* Consume the `template' keyword. */
32468 cp_lexer_consume_token (parser->lexer);
32469 return true;
32470 }
32471 }
32472 return false;
32473 }
32474
32475 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
32476 set PARSER->SCOPE, and perform other related actions. */
32477
32478 static void
32479 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
32480 {
32481 struct tree_check *check_value;
32482
32483 /* Get the stored value. */
32484 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
32485 /* Set the scope from the stored value. */
32486 parser->scope = saved_checks_value (check_value);
32487 parser->qualifying_scope = check_value->qualifying_scope;
32488 parser->object_scope = NULL_TREE;
32489 }
32490
32491 /* Consume tokens up through a non-nested END token. Returns TRUE if we
32492 encounter the end of a block before what we were looking for. */
32493
32494 static bool
32495 cp_parser_cache_group (cp_parser *parser,
32496 enum cpp_ttype end,
32497 unsigned depth)
32498 {
32499 while (true)
32500 {
32501 cp_token *token = cp_lexer_peek_token (parser->lexer);
32502
32503 /* Abort a parenthesized expression if we encounter a semicolon. */
32504 if ((end == CPP_CLOSE_PAREN || depth == 0)
32505 && token->type == CPP_SEMICOLON)
32506 return true;
32507 /* If we've reached the end of the file, stop. */
32508 if (token->type == CPP_EOF
32509 || (end != CPP_PRAGMA_EOL
32510 && token->type == CPP_PRAGMA_EOL))
32511 return true;
32512 if (token->type == CPP_CLOSE_BRACE && depth == 0)
32513 /* We've hit the end of an enclosing block, so there's been some
32514 kind of syntax error. */
32515 return true;
32516
32517 /* Consume the token. */
32518 cp_lexer_consume_token (parser->lexer);
32519 /* See if it starts a new group. */
32520 if (token->type == CPP_OPEN_BRACE)
32521 {
32522 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
32523 /* In theory this should probably check end == '}', but
32524 cp_parser_save_member_function_body needs it to exit
32525 after either '}' or ')' when called with ')'. */
32526 if (depth == 0)
32527 return false;
32528 }
32529 else if (token->type == CPP_OPEN_PAREN)
32530 {
32531 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
32532 if (depth == 0 && end == CPP_CLOSE_PAREN)
32533 return false;
32534 }
32535 else if (token->type == CPP_PRAGMA)
32536 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
32537 else if (token->type == end)
32538 return false;
32539 }
32540 }
32541
32542 /* Like above, for caching a default argument or NSDMI. Both of these are
32543 terminated by a non-nested comma, but it can be unclear whether or not a
32544 comma is nested in a template argument list unless we do more parsing.
32545 In order to handle this ambiguity, when we encounter a ',' after a '<'
32546 we try to parse what follows as a parameter-declaration-list (in the
32547 case of a default argument) or a member-declarator (in the case of an
32548 NSDMI). If that succeeds, then we stop caching. */
32549
32550 static tree
32551 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
32552 {
32553 unsigned depth = 0;
32554 int maybe_template_id = 0;
32555 cp_token *first_token;
32556 cp_token *token;
32557 tree default_argument;
32558
32559 /* Add tokens until we have processed the entire default
32560 argument. We add the range [first_token, token). */
32561 first_token = cp_lexer_peek_token (parser->lexer);
32562 if (first_token->type == CPP_OPEN_BRACE)
32563 {
32564 /* For list-initialization, this is straightforward. */
32565 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
32566 token = cp_lexer_peek_token (parser->lexer);
32567 }
32568 else while (true)
32569 {
32570 bool done = false;
32571
32572 /* Peek at the next token. */
32573 token = cp_lexer_peek_token (parser->lexer);
32574 /* What we do depends on what token we have. */
32575 switch (token->type)
32576 {
32577 /* In valid code, a default argument must be
32578 immediately followed by a `,' `)', or `...'. */
32579 case CPP_COMMA:
32580 if (depth == 0 && maybe_template_id)
32581 {
32582 /* If we've seen a '<', we might be in a
32583 template-argument-list. Until Core issue 325 is
32584 resolved, we don't know how this situation ought
32585 to be handled, so try to DTRT. We check whether
32586 what comes after the comma is a valid parameter
32587 declaration list. If it is, then the comma ends
32588 the default argument; otherwise the default
32589 argument continues. */
32590 bool error = false;
32591 cp_token *peek;
32592
32593 /* Set ITALP so cp_parser_parameter_declaration_list
32594 doesn't decide to commit to this parse. */
32595 bool saved_italp = parser->in_template_argument_list_p;
32596 parser->in_template_argument_list_p = true;
32597
32598 cp_parser_parse_tentatively (parser);
32599
32600 if (nsdmi)
32601 {
32602 /* Parse declarators until we reach a non-comma or
32603 somthing that cannot be an initializer.
32604 Just checking whether we're looking at a single
32605 declarator is insufficient. Consider:
32606 int var = tuple<T,U>::x;
32607 The template parameter 'U' looks exactly like a
32608 declarator. */
32609 do
32610 {
32611 int ctor_dtor_or_conv_p;
32612 cp_lexer_consume_token (parser->lexer);
32613 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
32614 CP_PARSER_FLAGS_NONE,
32615 &ctor_dtor_or_conv_p,
32616 /*parenthesized_p=*/NULL,
32617 /*member_p=*/true,
32618 /*friend_p=*/false,
32619 /*static_p=*/false);
32620 peek = cp_lexer_peek_token (parser->lexer);
32621 if (cp_parser_error_occurred (parser))
32622 break;
32623 }
32624 while (peek->type == CPP_COMMA);
32625 /* If we met an '=' or ';' then the original comma
32626 was the end of the NSDMI. Otherwise assume
32627 we're still in the NSDMI. */
32628 error = (peek->type != CPP_EQ
32629 && peek->type != CPP_SEMICOLON);
32630 }
32631 else
32632 {
32633 cp_lexer_consume_token (parser->lexer);
32634 begin_scope (sk_function_parms, NULL_TREE);
32635 tree t = cp_parser_parameter_declaration_list
32636 (parser, CP_PARSER_FLAGS_NONE);
32637 if (t == error_mark_node)
32638 error = true;
32639 pop_bindings_and_leave_scope ();
32640 }
32641 if (!cp_parser_error_occurred (parser) && !error)
32642 done = true;
32643 cp_parser_abort_tentative_parse (parser);
32644
32645 parser->in_template_argument_list_p = saved_italp;
32646 break;
32647 }
32648 /* FALLTHRU */
32649 case CPP_CLOSE_PAREN:
32650 case CPP_ELLIPSIS:
32651 /* If we run into a non-nested `;', `}', or `]',
32652 then the code is invalid -- but the default
32653 argument is certainly over. */
32654 case CPP_SEMICOLON:
32655 case CPP_CLOSE_BRACE:
32656 case CPP_CLOSE_SQUARE:
32657 if (depth == 0
32658 /* Handle correctly int n = sizeof ... ( p ); */
32659 && token->type != CPP_ELLIPSIS)
32660 done = true;
32661 /* Update DEPTH, if necessary. */
32662 else if (token->type == CPP_CLOSE_PAREN
32663 || token->type == CPP_CLOSE_BRACE
32664 || token->type == CPP_CLOSE_SQUARE)
32665 --depth;
32666 break;
32667
32668 case CPP_OPEN_PAREN:
32669 case CPP_OPEN_SQUARE:
32670 case CPP_OPEN_BRACE:
32671 ++depth;
32672 break;
32673
32674 case CPP_LESS:
32675 if (depth == 0)
32676 /* This might be the comparison operator, or it might
32677 start a template argument list. */
32678 ++maybe_template_id;
32679 break;
32680
32681 case CPP_RSHIFT:
32682 if (cxx_dialect == cxx98)
32683 break;
32684 /* Fall through for C++0x, which treats the `>>'
32685 operator like two `>' tokens in certain
32686 cases. */
32687 gcc_fallthrough ();
32688
32689 case CPP_GREATER:
32690 if (depth == 0)
32691 {
32692 /* This might be an operator, or it might close a
32693 template argument list. But if a previous '<'
32694 started a template argument list, this will have
32695 closed it, so we can't be in one anymore. */
32696 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
32697 if (maybe_template_id < 0)
32698 maybe_template_id = 0;
32699 }
32700 break;
32701
32702 /* If we run out of tokens, issue an error message. */
32703 case CPP_EOF:
32704 case CPP_PRAGMA_EOL:
32705 error_at (token->location, "file ends in default argument");
32706 return error_mark_node;
32707
32708 case CPP_NAME:
32709 case CPP_SCOPE:
32710 /* In these cases, we should look for template-ids.
32711 For example, if the default argument is
32712 `X<int, double>()', we need to do name lookup to
32713 figure out whether or not `X' is a template; if
32714 so, the `,' does not end the default argument.
32715
32716 That is not yet done. */
32717 break;
32718
32719 default:
32720 break;
32721 }
32722
32723 /* If we've reached the end, stop. */
32724 if (done)
32725 break;
32726
32727 /* Add the token to the token block. */
32728 token = cp_lexer_consume_token (parser->lexer);
32729 }
32730
32731 /* Create a DEFERRED_PARSE to represent the unparsed default
32732 argument. */
32733 default_argument = make_node (DEFERRED_PARSE);
32734 DEFPARSE_TOKENS (default_argument)
32735 = cp_token_cache_new (first_token, token);
32736 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
32737
32738 return default_argument;
32739 }
32740
32741 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
32742
32743 location_t
32744 defparse_location (tree default_argument)
32745 {
32746 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
32747 location_t start = tokens->first->location;
32748 location_t end = tokens->last->location;
32749 return make_location (start, start, end);
32750 }
32751
32752 /* Begin parsing tentatively. We always save tokens while parsing
32753 tentatively so that if the tentative parsing fails we can restore the
32754 tokens. */
32755
32756 static void
32757 cp_parser_parse_tentatively (cp_parser* parser)
32758 {
32759 /* Enter a new parsing context. */
32760 parser->context = cp_parser_context_new (parser->context);
32761 /* Begin saving tokens. */
32762 cp_lexer_save_tokens (parser->lexer);
32763 /* In order to avoid repetitive access control error messages,
32764 access checks are queued up until we are no longer parsing
32765 tentatively. */
32766 push_deferring_access_checks (dk_deferred);
32767 }
32768
32769 /* Commit to the currently active tentative parse. */
32770
32771 static void
32772 cp_parser_commit_to_tentative_parse (cp_parser* parser)
32773 {
32774 cp_parser_context *context;
32775 cp_lexer *lexer;
32776
32777 /* Mark all of the levels as committed. */
32778 lexer = parser->lexer;
32779 for (context = parser->context; context->next; context = context->next)
32780 {
32781 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
32782 break;
32783 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
32784 while (!cp_lexer_saving_tokens (lexer))
32785 lexer = lexer->next;
32786 cp_lexer_commit_tokens (lexer);
32787 }
32788 }
32789
32790 /* Commit to the topmost currently active tentative parse.
32791
32792 Note that this function shouldn't be called when there are
32793 irreversible side-effects while in a tentative state. For
32794 example, we shouldn't create a permanent entry in the symbol
32795 table, or issue an error message that might not apply if the
32796 tentative parse is aborted. */
32797
32798 static void
32799 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
32800 {
32801 cp_parser_context *context = parser->context;
32802 cp_lexer *lexer = parser->lexer;
32803
32804 if (context)
32805 {
32806 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
32807 return;
32808 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
32809
32810 while (!cp_lexer_saving_tokens (lexer))
32811 lexer = lexer->next;
32812 cp_lexer_commit_tokens (lexer);
32813 }
32814 }
32815
32816 /* Abort the currently active tentative parse. All consumed tokens
32817 will be rolled back, and no diagnostics will be issued. */
32818
32819 static void
32820 cp_parser_abort_tentative_parse (cp_parser* parser)
32821 {
32822 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
32823 || errorcount > 0);
32824 cp_parser_simulate_error (parser);
32825 /* Now, pretend that we want to see if the construct was
32826 successfully parsed. */
32827 cp_parser_parse_definitely (parser);
32828 }
32829
32830 /* Stop parsing tentatively. If a parse error has occurred, restore the
32831 token stream. Otherwise, commit to the tokens we have consumed.
32832 Returns true if no error occurred; false otherwise. */
32833
32834 static bool
32835 cp_parser_parse_definitely (cp_parser* parser)
32836 {
32837 bool error_occurred;
32838 cp_parser_context *context;
32839
32840 /* Remember whether or not an error occurred, since we are about to
32841 destroy that information. */
32842 error_occurred = cp_parser_error_occurred (parser);
32843 /* Remove the topmost context from the stack. */
32844 context = parser->context;
32845 parser->context = context->next;
32846 /* If no parse errors occurred, commit to the tentative parse. */
32847 if (!error_occurred)
32848 {
32849 /* Commit to the tokens read tentatively, unless that was
32850 already done. */
32851 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
32852 cp_lexer_commit_tokens (parser->lexer);
32853
32854 pop_to_parent_deferring_access_checks ();
32855 }
32856 /* Otherwise, if errors occurred, roll back our state so that things
32857 are just as they were before we began the tentative parse. */
32858 else
32859 {
32860 cp_lexer_rollback_tokens (parser->lexer);
32861 pop_deferring_access_checks ();
32862 }
32863 /* Add the context to the front of the free list. */
32864 context->next = cp_parser_context_free_list;
32865 cp_parser_context_free_list = context;
32866
32867 return !error_occurred;
32868 }
32869
32870 /* Returns true if we are parsing tentatively and are not committed to
32871 this tentative parse. */
32872
32873 static bool
32874 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
32875 {
32876 return (cp_parser_parsing_tentatively (parser)
32877 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
32878 }
32879
32880 /* Returns nonzero iff an error has occurred during the most recent
32881 tentative parse. */
32882
32883 static bool
32884 cp_parser_error_occurred (cp_parser* parser)
32885 {
32886 return (cp_parser_parsing_tentatively (parser)
32887 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
32888 }
32889
32890 /* Returns nonzero if GNU extensions are allowed. */
32891
32892 static bool
32893 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
32894 {
32895 return parser->allow_gnu_extensions_p;
32896 }
32897 \f
32898 /* Objective-C++ Productions */
32899
32900
32901 /* Parse an Objective-C expression, which feeds into a primary-expression
32902 above.
32903
32904 objc-expression:
32905 objc-message-expression
32906 objc-string-literal
32907 objc-encode-expression
32908 objc-protocol-expression
32909 objc-selector-expression
32910
32911 Returns a tree representation of the expression. */
32912
32913 static cp_expr
32914 cp_parser_objc_expression (cp_parser* parser)
32915 {
32916 /* Try to figure out what kind of declaration is present. */
32917 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
32918
32919 switch (kwd->type)
32920 {
32921 case CPP_OPEN_SQUARE:
32922 return cp_parser_objc_message_expression (parser);
32923
32924 case CPP_OBJC_STRING:
32925 kwd = cp_lexer_consume_token (parser->lexer);
32926 return objc_build_string_object (kwd->u.value);
32927
32928 case CPP_KEYWORD:
32929 switch (kwd->keyword)
32930 {
32931 case RID_AT_ENCODE:
32932 return cp_parser_objc_encode_expression (parser);
32933
32934 case RID_AT_PROTOCOL:
32935 return cp_parser_objc_protocol_expression (parser);
32936
32937 case RID_AT_SELECTOR:
32938 return cp_parser_objc_selector_expression (parser);
32939
32940 default:
32941 break;
32942 }
32943 /* FALLTHRU */
32944 default:
32945 error_at (kwd->location,
32946 "misplaced %<@%D%> Objective-C++ construct",
32947 kwd->u.value);
32948 cp_parser_skip_to_end_of_block_or_statement (parser);
32949 }
32950
32951 return error_mark_node;
32952 }
32953
32954 /* Parse an Objective-C message expression.
32955
32956 objc-message-expression:
32957 [ objc-message-receiver objc-message-args ]
32958
32959 Returns a representation of an Objective-C message. */
32960
32961 static tree
32962 cp_parser_objc_message_expression (cp_parser* parser)
32963 {
32964 tree receiver, messageargs;
32965
32966 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
32967 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
32968 receiver = cp_parser_objc_message_receiver (parser);
32969 messageargs = cp_parser_objc_message_args (parser);
32970 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
32971 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32972
32973 tree result = objc_build_message_expr (receiver, messageargs);
32974
32975 /* Construct a location e.g.
32976 [self func1:5]
32977 ^~~~~~~~~~~~~~
32978 ranging from the '[' to the ']', with the caret at the start. */
32979 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
32980 protected_set_expr_location (result, combined_loc);
32981
32982 return result;
32983 }
32984
32985 /* Parse an objc-message-receiver.
32986
32987 objc-message-receiver:
32988 expression
32989 simple-type-specifier
32990
32991 Returns a representation of the type or expression. */
32992
32993 static tree
32994 cp_parser_objc_message_receiver (cp_parser* parser)
32995 {
32996 tree rcv;
32997
32998 /* An Objective-C message receiver may be either (1) a type
32999 or (2) an expression. */
33000 cp_parser_parse_tentatively (parser);
33001 rcv = cp_parser_expression (parser);
33002
33003 /* If that worked out, fine. */
33004 if (cp_parser_parse_definitely (parser))
33005 return rcv;
33006
33007 cp_parser_parse_tentatively (parser);
33008 rcv = cp_parser_simple_type_specifier (parser,
33009 /*decl_specs=*/NULL,
33010 CP_PARSER_FLAGS_NONE);
33011
33012 if (cp_parser_parse_definitely (parser))
33013 return objc_get_class_reference (rcv);
33014
33015 cp_parser_error (parser, "objective-c++ message receiver expected");
33016 return error_mark_node;
33017 }
33018
33019 /* Parse the arguments and selectors comprising an Objective-C message.
33020
33021 objc-message-args:
33022 objc-selector
33023 objc-selector-args
33024 objc-selector-args , objc-comma-args
33025
33026 objc-selector-args:
33027 objc-selector [opt] : assignment-expression
33028 objc-selector-args objc-selector [opt] : assignment-expression
33029
33030 objc-comma-args:
33031 assignment-expression
33032 objc-comma-args , assignment-expression
33033
33034 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
33035 selector arguments and TREE_VALUE containing a list of comma
33036 arguments. */
33037
33038 static tree
33039 cp_parser_objc_message_args (cp_parser* parser)
33040 {
33041 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
33042 bool maybe_unary_selector_p = true;
33043 cp_token *token = cp_lexer_peek_token (parser->lexer);
33044
33045 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
33046 {
33047 tree selector = NULL_TREE, arg;
33048
33049 if (token->type != CPP_COLON)
33050 selector = cp_parser_objc_selector (parser);
33051
33052 /* Detect if we have a unary selector. */
33053 if (maybe_unary_selector_p
33054 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
33055 return build_tree_list (selector, NULL_TREE);
33056
33057 maybe_unary_selector_p = false;
33058 cp_parser_require (parser, CPP_COLON, RT_COLON);
33059 arg = cp_parser_assignment_expression (parser);
33060
33061 sel_args
33062 = chainon (sel_args,
33063 build_tree_list (selector, arg));
33064
33065 token = cp_lexer_peek_token (parser->lexer);
33066 }
33067
33068 /* Handle non-selector arguments, if any. */
33069 while (token->type == CPP_COMMA)
33070 {
33071 tree arg;
33072
33073 cp_lexer_consume_token (parser->lexer);
33074 arg = cp_parser_assignment_expression (parser);
33075
33076 addl_args
33077 = chainon (addl_args,
33078 build_tree_list (NULL_TREE, arg));
33079
33080 token = cp_lexer_peek_token (parser->lexer);
33081 }
33082
33083 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
33084 {
33085 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
33086 return build_tree_list (error_mark_node, error_mark_node);
33087 }
33088
33089 return build_tree_list (sel_args, addl_args);
33090 }
33091
33092 /* Parse an Objective-C encode expression.
33093
33094 objc-encode-expression:
33095 @encode objc-typename
33096
33097 Returns an encoded representation of the type argument. */
33098
33099 static cp_expr
33100 cp_parser_objc_encode_expression (cp_parser* parser)
33101 {
33102 tree type;
33103 cp_token *token;
33104 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
33105
33106 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
33107 matching_parens parens;
33108 parens.require_open (parser);
33109 token = cp_lexer_peek_token (parser->lexer);
33110 type = complete_type (cp_parser_type_id (parser));
33111 parens.require_close (parser);
33112
33113 if (!type)
33114 {
33115 error_at (token->location,
33116 "%<@encode%> must specify a type as an argument");
33117 return error_mark_node;
33118 }
33119
33120 /* This happens if we find @encode(T) (where T is a template
33121 typename or something dependent on a template typename) when
33122 parsing a template. In that case, we can't compile it
33123 immediately, but we rather create an AT_ENCODE_EXPR which will
33124 need to be instantiated when the template is used.
33125 */
33126 if (dependent_type_p (type))
33127 {
33128 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
33129 TREE_READONLY (value) = 1;
33130 return value;
33131 }
33132
33133
33134 /* Build a location of the form:
33135 @encode(int)
33136 ^~~~~~~~~~~~
33137 with caret==start at the @ token, finishing at the close paren. */
33138 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
33139
33140 return cp_expr (objc_build_encode_expr (type), combined_loc);
33141 }
33142
33143 /* Parse an Objective-C @defs expression. */
33144
33145 static tree
33146 cp_parser_objc_defs_expression (cp_parser *parser)
33147 {
33148 tree name;
33149
33150 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
33151 matching_parens parens;
33152 parens.require_open (parser);
33153 name = cp_parser_identifier (parser);
33154 parens.require_close (parser);
33155
33156 return objc_get_class_ivars (name);
33157 }
33158
33159 /* Parse an Objective-C protocol expression.
33160
33161 objc-protocol-expression:
33162 @protocol ( identifier )
33163
33164 Returns a representation of the protocol expression. */
33165
33166 static tree
33167 cp_parser_objc_protocol_expression (cp_parser* parser)
33168 {
33169 tree proto;
33170 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
33171
33172 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
33173 matching_parens parens;
33174 parens.require_open (parser);
33175 proto = cp_parser_identifier (parser);
33176 parens.require_close (parser);
33177
33178 /* Build a location of the form:
33179 @protocol(prot)
33180 ^~~~~~~~~~~~~~~
33181 with caret==start at the @ token, finishing at the close paren. */
33182 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
33183 tree result = objc_build_protocol_expr (proto);
33184 protected_set_expr_location (result, combined_loc);
33185 return result;
33186 }
33187
33188 /* Parse an Objective-C selector expression.
33189
33190 objc-selector-expression:
33191 @selector ( objc-method-signature )
33192
33193 objc-method-signature:
33194 objc-selector
33195 objc-selector-seq
33196
33197 objc-selector-seq:
33198 objc-selector :
33199 objc-selector-seq objc-selector :
33200
33201 Returns a representation of the method selector. */
33202
33203 static tree
33204 cp_parser_objc_selector_expression (cp_parser* parser)
33205 {
33206 tree sel_seq = NULL_TREE;
33207 bool maybe_unary_selector_p = true;
33208 cp_token *token;
33209 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33210
33211 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
33212 matching_parens parens;
33213 parens.require_open (parser);
33214 token = cp_lexer_peek_token (parser->lexer);
33215
33216 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
33217 || token->type == CPP_SCOPE)
33218 {
33219 tree selector = NULL_TREE;
33220
33221 if (token->type != CPP_COLON
33222 || token->type == CPP_SCOPE)
33223 selector = cp_parser_objc_selector (parser);
33224
33225 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
33226 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
33227 {
33228 /* Detect if we have a unary selector. */
33229 if (maybe_unary_selector_p)
33230 {
33231 sel_seq = selector;
33232 goto finish_selector;
33233 }
33234 else
33235 {
33236 cp_parser_error (parser, "expected %<:%>");
33237 }
33238 }
33239 maybe_unary_selector_p = false;
33240 token = cp_lexer_consume_token (parser->lexer);
33241
33242 if (token->type == CPP_SCOPE)
33243 {
33244 sel_seq
33245 = chainon (sel_seq,
33246 build_tree_list (selector, NULL_TREE));
33247 sel_seq
33248 = chainon (sel_seq,
33249 build_tree_list (NULL_TREE, NULL_TREE));
33250 }
33251 else
33252 sel_seq
33253 = chainon (sel_seq,
33254 build_tree_list (selector, NULL_TREE));
33255
33256 token = cp_lexer_peek_token (parser->lexer);
33257 }
33258
33259 finish_selector:
33260 parens.require_close (parser);
33261
33262
33263 /* Build a location of the form:
33264 @selector(func)
33265 ^~~~~~~~~~~~~~~
33266 with caret==start at the @ token, finishing at the close paren. */
33267 location_t combined_loc = make_location (loc, loc, parser->lexer);
33268 tree result = objc_build_selector_expr (combined_loc, sel_seq);
33269 /* TODO: objc_build_selector_expr doesn't always honor the location. */
33270 protected_set_expr_location (result, combined_loc);
33271 return result;
33272 }
33273
33274 /* Parse a list of identifiers.
33275
33276 objc-identifier-list:
33277 identifier
33278 objc-identifier-list , identifier
33279
33280 Returns a TREE_LIST of identifier nodes. */
33281
33282 static tree
33283 cp_parser_objc_identifier_list (cp_parser* parser)
33284 {
33285 tree identifier;
33286 tree list;
33287 cp_token *sep;
33288
33289 identifier = cp_parser_identifier (parser);
33290 if (identifier == error_mark_node)
33291 return error_mark_node;
33292
33293 list = build_tree_list (NULL_TREE, identifier);
33294 sep = cp_lexer_peek_token (parser->lexer);
33295
33296 while (sep->type == CPP_COMMA)
33297 {
33298 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
33299 identifier = cp_parser_identifier (parser);
33300 if (identifier == error_mark_node)
33301 return list;
33302
33303 list = chainon (list, build_tree_list (NULL_TREE,
33304 identifier));
33305 sep = cp_lexer_peek_token (parser->lexer);
33306 }
33307
33308 return list;
33309 }
33310
33311 /* Parse an Objective-C alias declaration.
33312
33313 objc-alias-declaration:
33314 @compatibility_alias identifier identifier ;
33315
33316 This function registers the alias mapping with the Objective-C front end.
33317 It returns nothing. */
33318
33319 static void
33320 cp_parser_objc_alias_declaration (cp_parser* parser)
33321 {
33322 tree alias, orig;
33323
33324 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
33325 alias = cp_parser_identifier (parser);
33326 orig = cp_parser_identifier (parser);
33327 objc_declare_alias (alias, orig);
33328 cp_parser_consume_semicolon_at_end_of_statement (parser);
33329 }
33330
33331 /* Parse an Objective-C class forward-declaration.
33332
33333 objc-class-declaration:
33334 @class objc-identifier-list ;
33335
33336 The function registers the forward declarations with the Objective-C
33337 front end. It returns nothing. */
33338
33339 static void
33340 cp_parser_objc_class_declaration (cp_parser* parser)
33341 {
33342 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
33343 while (true)
33344 {
33345 tree id;
33346
33347 id = cp_parser_identifier (parser);
33348 if (id == error_mark_node)
33349 break;
33350
33351 objc_declare_class (id);
33352
33353 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33354 cp_lexer_consume_token (parser->lexer);
33355 else
33356 break;
33357 }
33358 cp_parser_consume_semicolon_at_end_of_statement (parser);
33359 }
33360
33361 /* Parse a list of Objective-C protocol references.
33362
33363 objc-protocol-refs-opt:
33364 objc-protocol-refs [opt]
33365
33366 objc-protocol-refs:
33367 < objc-identifier-list >
33368
33369 Returns a TREE_LIST of identifiers, if any. */
33370
33371 static tree
33372 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
33373 {
33374 tree protorefs = NULL_TREE;
33375
33376 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
33377 {
33378 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
33379 protorefs = cp_parser_objc_identifier_list (parser);
33380 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
33381 }
33382
33383 return protorefs;
33384 }
33385
33386 /* Parse a Objective-C visibility specification. */
33387
33388 static void
33389 cp_parser_objc_visibility_spec (cp_parser* parser)
33390 {
33391 cp_token *vis = cp_lexer_peek_token (parser->lexer);
33392
33393 switch (vis->keyword)
33394 {
33395 case RID_AT_PRIVATE:
33396 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
33397 break;
33398 case RID_AT_PROTECTED:
33399 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
33400 break;
33401 case RID_AT_PUBLIC:
33402 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
33403 break;
33404 case RID_AT_PACKAGE:
33405 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
33406 break;
33407 default:
33408 return;
33409 }
33410
33411 /* Eat '@private'/'@protected'/'@public'. */
33412 cp_lexer_consume_token (parser->lexer);
33413 }
33414
33415 /* Parse an Objective-C method type. Return 'true' if it is a class
33416 (+) method, and 'false' if it is an instance (-) method. */
33417
33418 static inline bool
33419 cp_parser_objc_method_type (cp_parser* parser)
33420 {
33421 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
33422 return true;
33423 else
33424 return false;
33425 }
33426
33427 /* Parse an Objective-C protocol qualifier. */
33428
33429 static tree
33430 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
33431 {
33432 tree quals = NULL_TREE, node;
33433 cp_token *token = cp_lexer_peek_token (parser->lexer);
33434
33435 node = token->u.value;
33436
33437 while (node && identifier_p (node)
33438 && (node == ridpointers [(int) RID_IN]
33439 || node == ridpointers [(int) RID_OUT]
33440 || node == ridpointers [(int) RID_INOUT]
33441 || node == ridpointers [(int) RID_BYCOPY]
33442 || node == ridpointers [(int) RID_BYREF]
33443 || node == ridpointers [(int) RID_ONEWAY]))
33444 {
33445 quals = tree_cons (NULL_TREE, node, quals);
33446 cp_lexer_consume_token (parser->lexer);
33447 token = cp_lexer_peek_token (parser->lexer);
33448 node = token->u.value;
33449 }
33450
33451 return quals;
33452 }
33453
33454 /* Parse an Objective-C typename. */
33455
33456 static tree
33457 cp_parser_objc_typename (cp_parser* parser)
33458 {
33459 tree type_name = NULL_TREE;
33460
33461 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33462 {
33463 tree proto_quals, cp_type = NULL_TREE;
33464
33465 matching_parens parens;
33466 parens.consume_open (parser); /* Eat '('. */
33467 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
33468
33469 /* An ObjC type name may consist of just protocol qualifiers, in which
33470 case the type shall default to 'id'. */
33471 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
33472 {
33473 cp_type = cp_parser_type_id (parser);
33474
33475 /* If the type could not be parsed, an error has already
33476 been produced. For error recovery, behave as if it had
33477 not been specified, which will use the default type
33478 'id'. */
33479 if (cp_type == error_mark_node)
33480 {
33481 cp_type = NULL_TREE;
33482 /* We need to skip to the closing parenthesis as
33483 cp_parser_type_id() does not seem to do it for
33484 us. */
33485 cp_parser_skip_to_closing_parenthesis (parser,
33486 /*recovering=*/true,
33487 /*or_comma=*/false,
33488 /*consume_paren=*/false);
33489 }
33490 }
33491
33492 parens.require_close (parser);
33493 type_name = build_tree_list (proto_quals, cp_type);
33494 }
33495
33496 return type_name;
33497 }
33498
33499 /* Check to see if TYPE refers to an Objective-C selector name. */
33500
33501 static bool
33502 cp_parser_objc_selector_p (enum cpp_ttype type)
33503 {
33504 return (type == CPP_NAME || type == CPP_KEYWORD
33505 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
33506 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
33507 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
33508 || type == CPP_XOR || type == CPP_XOR_EQ);
33509 }
33510
33511 /* Parse an Objective-C selector. */
33512
33513 static tree
33514 cp_parser_objc_selector (cp_parser* parser)
33515 {
33516 cp_token *token = cp_lexer_consume_token (parser->lexer);
33517
33518 if (!cp_parser_objc_selector_p (token->type))
33519 {
33520 error_at (token->location, "invalid Objective-C++ selector name");
33521 return error_mark_node;
33522 }
33523
33524 /* C++ operator names are allowed to appear in ObjC selectors. */
33525 switch (token->type)
33526 {
33527 case CPP_AND_AND: return get_identifier ("and");
33528 case CPP_AND_EQ: return get_identifier ("and_eq");
33529 case CPP_AND: return get_identifier ("bitand");
33530 case CPP_OR: return get_identifier ("bitor");
33531 case CPP_COMPL: return get_identifier ("compl");
33532 case CPP_NOT: return get_identifier ("not");
33533 case CPP_NOT_EQ: return get_identifier ("not_eq");
33534 case CPP_OR_OR: return get_identifier ("or");
33535 case CPP_OR_EQ: return get_identifier ("or_eq");
33536 case CPP_XOR: return get_identifier ("xor");
33537 case CPP_XOR_EQ: return get_identifier ("xor_eq");
33538 default: return token->u.value;
33539 }
33540 }
33541
33542 /* Parse an Objective-C params list. */
33543
33544 static tree
33545 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
33546 {
33547 tree params = NULL_TREE;
33548 bool maybe_unary_selector_p = true;
33549 cp_token *token = cp_lexer_peek_token (parser->lexer);
33550
33551 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
33552 {
33553 tree selector = NULL_TREE, type_name, identifier;
33554 tree parm_attr = NULL_TREE;
33555
33556 if (token->keyword == RID_ATTRIBUTE)
33557 break;
33558
33559 if (token->type != CPP_COLON)
33560 selector = cp_parser_objc_selector (parser);
33561
33562 /* Detect if we have a unary selector. */
33563 if (maybe_unary_selector_p
33564 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
33565 {
33566 params = selector; /* Might be followed by attributes. */
33567 break;
33568 }
33569
33570 maybe_unary_selector_p = false;
33571 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33572 {
33573 /* Something went quite wrong. There should be a colon
33574 here, but there is not. Stop parsing parameters. */
33575 break;
33576 }
33577 type_name = cp_parser_objc_typename (parser);
33578 /* New ObjC allows attributes on parameters too. */
33579 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
33580 parm_attr = cp_parser_attributes_opt (parser);
33581 identifier = cp_parser_identifier (parser);
33582
33583 params
33584 = chainon (params,
33585 objc_build_keyword_decl (selector,
33586 type_name,
33587 identifier,
33588 parm_attr));
33589
33590 token = cp_lexer_peek_token (parser->lexer);
33591 }
33592
33593 if (params == NULL_TREE)
33594 {
33595 cp_parser_error (parser, "objective-c++ method declaration is expected");
33596 return error_mark_node;
33597 }
33598
33599 /* We allow tail attributes for the method. */
33600 if (token->keyword == RID_ATTRIBUTE)
33601 {
33602 *attributes = cp_parser_attributes_opt (parser);
33603 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
33604 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33605 return params;
33606 cp_parser_error (parser,
33607 "method attributes must be specified at the end");
33608 return error_mark_node;
33609 }
33610
33611 if (params == NULL_TREE)
33612 {
33613 cp_parser_error (parser, "objective-c++ method declaration is expected");
33614 return error_mark_node;
33615 }
33616 return params;
33617 }
33618
33619 /* Parse the non-keyword Objective-C params. */
33620
33621 static tree
33622 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
33623 tree* attributes)
33624 {
33625 tree params = make_node (TREE_LIST);
33626 cp_token *token = cp_lexer_peek_token (parser->lexer);
33627 *ellipsisp = false; /* Initially, assume no ellipsis. */
33628
33629 while (token->type == CPP_COMMA)
33630 {
33631 cp_parameter_declarator *parmdecl;
33632 tree parm;
33633
33634 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
33635 token = cp_lexer_peek_token (parser->lexer);
33636
33637 if (token->type == CPP_ELLIPSIS)
33638 {
33639 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
33640 *ellipsisp = true;
33641 token = cp_lexer_peek_token (parser->lexer);
33642 break;
33643 }
33644
33645 /* TODO: parse attributes for tail parameters. */
33646 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
33647 false, NULL);
33648 parm = grokdeclarator (parmdecl->declarator,
33649 &parmdecl->decl_specifiers,
33650 PARM, /*initialized=*/0,
33651 /*attrlist=*/NULL);
33652
33653 chainon (params, build_tree_list (NULL_TREE, parm));
33654 token = cp_lexer_peek_token (parser->lexer);
33655 }
33656
33657 /* We allow tail attributes for the method. */
33658 if (token->keyword == RID_ATTRIBUTE)
33659 {
33660 if (*attributes == NULL_TREE)
33661 {
33662 *attributes = cp_parser_attributes_opt (parser);
33663 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
33664 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33665 return params;
33666 }
33667 else
33668 /* We have an error, but parse the attributes, so that we can
33669 carry on. */
33670 *attributes = cp_parser_attributes_opt (parser);
33671
33672 cp_parser_error (parser,
33673 "method attributes must be specified at the end");
33674 return error_mark_node;
33675 }
33676
33677 return params;
33678 }
33679
33680 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
33681
33682 static void
33683 cp_parser_objc_interstitial_code (cp_parser* parser)
33684 {
33685 cp_token *token = cp_lexer_peek_token (parser->lexer);
33686
33687 /* If the next token is `extern' and the following token is a string
33688 literal, then we have a linkage specification. */
33689 if (token->keyword == RID_EXTERN
33690 && cp_parser_is_pure_string_literal
33691 (cp_lexer_peek_nth_token (parser->lexer, 2)))
33692 cp_parser_linkage_specification (parser, NULL_TREE);
33693 /* Handle #pragma, if any. */
33694 else if (token->type == CPP_PRAGMA)
33695 cp_parser_pragma (parser, pragma_objc_icode, NULL);
33696 /* Allow stray semicolons. */
33697 else if (token->type == CPP_SEMICOLON)
33698 cp_lexer_consume_token (parser->lexer);
33699 /* Mark methods as optional or required, when building protocols. */
33700 else if (token->keyword == RID_AT_OPTIONAL)
33701 {
33702 cp_lexer_consume_token (parser->lexer);
33703 objc_set_method_opt (true);
33704 }
33705 else if (token->keyword == RID_AT_REQUIRED)
33706 {
33707 cp_lexer_consume_token (parser->lexer);
33708 objc_set_method_opt (false);
33709 }
33710 else if (token->keyword == RID_NAMESPACE)
33711 cp_parser_namespace_definition (parser);
33712 /* Other stray characters must generate errors. */
33713 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
33714 {
33715 cp_lexer_consume_token (parser->lexer);
33716 error ("stray %qs between Objective-C++ methods",
33717 token->type == CPP_OPEN_BRACE ? "{" : "}");
33718 }
33719 /* Finally, try to parse a block-declaration, or a function-definition. */
33720 else
33721 cp_parser_block_declaration (parser, /*statement_p=*/false);
33722 }
33723
33724 /* Parse a method signature. */
33725
33726 static tree
33727 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
33728 {
33729 tree rettype, kwdparms, optparms;
33730 bool ellipsis = false;
33731 bool is_class_method;
33732
33733 is_class_method = cp_parser_objc_method_type (parser);
33734 rettype = cp_parser_objc_typename (parser);
33735 *attributes = NULL_TREE;
33736 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
33737 if (kwdparms == error_mark_node)
33738 return error_mark_node;
33739 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
33740 if (optparms == error_mark_node)
33741 return error_mark_node;
33742
33743 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
33744 }
33745
33746 static bool
33747 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
33748 {
33749 tree tattr;
33750 cp_lexer_save_tokens (parser->lexer);
33751 tattr = cp_parser_attributes_opt (parser);
33752 gcc_assert (tattr) ;
33753
33754 /* If the attributes are followed by a method introducer, this is not allowed.
33755 Dump the attributes and flag the situation. */
33756 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
33757 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33758 return true;
33759
33760 /* Otherwise, the attributes introduce some interstitial code, possibly so
33761 rewind to allow that check. */
33762 cp_lexer_rollback_tokens (parser->lexer);
33763 return false;
33764 }
33765
33766 /* Parse an Objective-C method prototype list. */
33767
33768 static void
33769 cp_parser_objc_method_prototype_list (cp_parser* parser)
33770 {
33771 cp_token *token = cp_lexer_peek_token (parser->lexer);
33772
33773 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
33774 {
33775 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
33776 {
33777 tree attributes, sig;
33778 bool is_class_method;
33779 if (token->type == CPP_PLUS)
33780 is_class_method = true;
33781 else
33782 is_class_method = false;
33783 sig = cp_parser_objc_method_signature (parser, &attributes);
33784 if (sig == error_mark_node)
33785 {
33786 cp_parser_skip_to_end_of_block_or_statement (parser);
33787 token = cp_lexer_peek_token (parser->lexer);
33788 continue;
33789 }
33790 objc_add_method_declaration (is_class_method, sig, attributes);
33791 cp_parser_consume_semicolon_at_end_of_statement (parser);
33792 }
33793 else if (token->keyword == RID_AT_PROPERTY)
33794 cp_parser_objc_at_property_declaration (parser);
33795 else if (token->keyword == RID_ATTRIBUTE
33796 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
33797 warning_at (cp_lexer_peek_token (parser->lexer)->location,
33798 OPT_Wattributes,
33799 "prefix attributes are ignored for methods");
33800 else
33801 /* Allow for interspersed non-ObjC++ code. */
33802 cp_parser_objc_interstitial_code (parser);
33803
33804 token = cp_lexer_peek_token (parser->lexer);
33805 }
33806
33807 if (token->type != CPP_EOF)
33808 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
33809 else
33810 cp_parser_error (parser, "expected %<@end%>");
33811
33812 objc_finish_interface ();
33813 }
33814
33815 /* Parse an Objective-C method definition list. */
33816
33817 static void
33818 cp_parser_objc_method_definition_list (cp_parser* parser)
33819 {
33820 for (;;)
33821 {
33822 cp_token *token = cp_lexer_peek_token (parser->lexer);
33823
33824 if (token->keyword == RID_AT_END)
33825 {
33826 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
33827 break;
33828 }
33829 else if (token->type == CPP_EOF)
33830 {
33831 cp_parser_error (parser, "expected %<@end%>");
33832 break;
33833 }
33834 else if (token->type == CPP_PLUS || token->type == CPP_MINUS)
33835 {
33836 bool is_class_method = token->type == CPP_PLUS;
33837
33838 push_deferring_access_checks (dk_deferred);
33839 tree attribute;
33840 tree sig = cp_parser_objc_method_signature (parser, &attribute);
33841 if (sig == error_mark_node)
33842 cp_parser_skip_to_end_of_block_or_statement (parser);
33843 else
33844 {
33845 objc_start_method_definition (is_class_method, sig,
33846 attribute, NULL_TREE);
33847
33848 /* For historical reasons, we accept an optional semicolon. */
33849 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33850 cp_lexer_consume_token (parser->lexer);
33851
33852 perform_deferred_access_checks (tf_warning_or_error);
33853 stop_deferring_access_checks ();
33854 tree meth
33855 = cp_parser_function_definition_after_declarator (parser, false);
33856 pop_deferring_access_checks ();
33857 objc_finish_method_definition (meth);
33858 }
33859 }
33860 /* The following case will be removed once @synthesize is
33861 completely implemented. */
33862 else if (token->keyword == RID_AT_PROPERTY)
33863 cp_parser_objc_at_property_declaration (parser);
33864 else if (token->keyword == RID_AT_SYNTHESIZE)
33865 cp_parser_objc_at_synthesize_declaration (parser);
33866 else if (token->keyword == RID_AT_DYNAMIC)
33867 cp_parser_objc_at_dynamic_declaration (parser);
33868 else if (token->keyword == RID_ATTRIBUTE
33869 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
33870 warning_at (token->location, OPT_Wattributes,
33871 "prefix attributes are ignored for methods");
33872 else
33873 /* Allow for interspersed non-ObjC++ code. */
33874 cp_parser_objc_interstitial_code (parser);
33875 }
33876
33877 objc_finish_implementation ();
33878 }
33879
33880 /* Parse Objective-C ivars. */
33881
33882 static void
33883 cp_parser_objc_class_ivars (cp_parser* parser)
33884 {
33885 cp_token *token = cp_lexer_peek_token (parser->lexer);
33886
33887 if (token->type != CPP_OPEN_BRACE)
33888 return; /* No ivars specified. */
33889
33890 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
33891 token = cp_lexer_peek_token (parser->lexer);
33892
33893 while (token->type != CPP_CLOSE_BRACE
33894 && token->keyword != RID_AT_END && token->type != CPP_EOF)
33895 {
33896 cp_decl_specifier_seq declspecs;
33897 int decl_class_or_enum_p;
33898 tree prefix_attributes;
33899
33900 cp_parser_objc_visibility_spec (parser);
33901
33902 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33903 break;
33904
33905 cp_parser_decl_specifier_seq (parser,
33906 CP_PARSER_FLAGS_OPTIONAL,
33907 &declspecs,
33908 &decl_class_or_enum_p);
33909
33910 /* auto, register, static, extern, mutable. */
33911 if (declspecs.storage_class != sc_none)
33912 {
33913 cp_parser_error (parser, "invalid type for instance variable");
33914 declspecs.storage_class = sc_none;
33915 }
33916
33917 /* thread_local. */
33918 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
33919 {
33920 cp_parser_error (parser, "invalid type for instance variable");
33921 declspecs.locations[ds_thread] = 0;
33922 }
33923
33924 /* typedef. */
33925 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
33926 {
33927 cp_parser_error (parser, "invalid type for instance variable");
33928 declspecs.locations[ds_typedef] = 0;
33929 }
33930
33931 prefix_attributes = declspecs.attributes;
33932 declspecs.attributes = NULL_TREE;
33933
33934 /* Keep going until we hit the `;' at the end of the
33935 declaration. */
33936 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33937 {
33938 tree width = NULL_TREE, attributes, first_attribute, decl;
33939 cp_declarator *declarator = NULL;
33940 int ctor_dtor_or_conv_p;
33941
33942 /* Check for a (possibly unnamed) bitfield declaration. */
33943 token = cp_lexer_peek_token (parser->lexer);
33944 if (token->type == CPP_COLON)
33945 goto eat_colon;
33946
33947 if (token->type == CPP_NAME
33948 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
33949 == CPP_COLON))
33950 {
33951 /* Get the name of the bitfield. */
33952 declarator = make_id_declarator (NULL_TREE,
33953 cp_parser_identifier (parser),
33954 sfk_none, token->location);
33955
33956 eat_colon:
33957 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
33958 /* Get the width of the bitfield. */
33959 width
33960 = cp_parser_constant_expression (parser);
33961 }
33962 else
33963 {
33964 /* Parse the declarator. */
33965 declarator
33966 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
33967 CP_PARSER_FLAGS_NONE,
33968 &ctor_dtor_or_conv_p,
33969 /*parenthesized_p=*/NULL,
33970 /*member_p=*/false,
33971 /*friend_p=*/false,
33972 /*static_p=*/false);
33973 }
33974
33975 /* Look for attributes that apply to the ivar. */
33976 attributes = cp_parser_attributes_opt (parser);
33977 /* Remember which attributes are prefix attributes and
33978 which are not. */
33979 first_attribute = attributes;
33980 /* Combine the attributes. */
33981 attributes = attr_chainon (prefix_attributes, attributes);
33982
33983 if (width)
33984 /* Create the bitfield declaration. */
33985 decl = grokbitfield (declarator, &declspecs,
33986 width, NULL_TREE, attributes);
33987 else
33988 decl = grokfield (declarator, &declspecs,
33989 NULL_TREE, /*init_const_expr_p=*/false,
33990 NULL_TREE, attributes);
33991
33992 /* Add the instance variable. */
33993 if (decl != error_mark_node && decl != NULL_TREE)
33994 objc_add_instance_variable (decl);
33995
33996 /* Reset PREFIX_ATTRIBUTES. */
33997 if (attributes != error_mark_node)
33998 {
33999 while (attributes && TREE_CHAIN (attributes) != first_attribute)
34000 attributes = TREE_CHAIN (attributes);
34001 if (attributes)
34002 TREE_CHAIN (attributes) = NULL_TREE;
34003 }
34004
34005 token = cp_lexer_peek_token (parser->lexer);
34006
34007 if (token->type == CPP_COMMA)
34008 {
34009 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
34010 continue;
34011 }
34012 break;
34013 }
34014
34015 cp_parser_consume_semicolon_at_end_of_statement (parser);
34016 token = cp_lexer_peek_token (parser->lexer);
34017 }
34018
34019 if (token->keyword == RID_AT_END)
34020 cp_parser_error (parser, "expected %<}%>");
34021
34022 /* Do not consume the RID_AT_END, so it will be read again as terminating
34023 the @interface of @implementation. */
34024 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
34025 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
34026
34027 /* For historical reasons, we accept an optional semicolon. */
34028 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34029 cp_lexer_consume_token (parser->lexer);
34030 }
34031
34032 /* Parse an Objective-C protocol declaration. */
34033
34034 static void
34035 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
34036 {
34037 tree proto, protorefs;
34038 cp_token *tok;
34039
34040 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
34041 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34042 {
34043 tok = cp_lexer_peek_token (parser->lexer);
34044 error_at (tok->location, "identifier expected after %<@protocol%>");
34045 cp_parser_consume_semicolon_at_end_of_statement (parser);
34046 return;
34047 }
34048
34049 /* See if we have a forward declaration or a definition. */
34050 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
34051
34052 /* Try a forward declaration first. */
34053 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
34054 {
34055 while (true)
34056 {
34057 tree id;
34058
34059 id = cp_parser_identifier (parser);
34060 if (id == error_mark_node)
34061 break;
34062
34063 objc_declare_protocol (id, attributes);
34064
34065 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34066 cp_lexer_consume_token (parser->lexer);
34067 else
34068 break;
34069 }
34070 cp_parser_consume_semicolon_at_end_of_statement (parser);
34071 }
34072
34073 /* Ok, we got a full-fledged definition (or at least should). */
34074 else
34075 {
34076 proto = cp_parser_identifier (parser);
34077 protorefs = cp_parser_objc_protocol_refs_opt (parser);
34078 objc_start_protocol (proto, protorefs, attributes);
34079 cp_parser_objc_method_prototype_list (parser);
34080 }
34081 }
34082
34083 /* Parse an Objective-C superclass or category. */
34084
34085 static void
34086 cp_parser_objc_superclass_or_category (cp_parser *parser,
34087 bool iface_p,
34088 tree *super,
34089 tree *categ, bool *is_class_extension)
34090 {
34091 cp_token *next = cp_lexer_peek_token (parser->lexer);
34092
34093 *super = *categ = NULL_TREE;
34094 *is_class_extension = false;
34095 if (next->type == CPP_COLON)
34096 {
34097 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
34098 *super = cp_parser_identifier (parser);
34099 }
34100 else if (next->type == CPP_OPEN_PAREN)
34101 {
34102 matching_parens parens;
34103 parens.consume_open (parser); /* Eat '('. */
34104
34105 /* If there is no category name, and this is an @interface, we
34106 have a class extension. */
34107 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
34108 {
34109 *categ = NULL_TREE;
34110 *is_class_extension = true;
34111 }
34112 else
34113 *categ = cp_parser_identifier (parser);
34114
34115 parens.require_close (parser);
34116 }
34117 }
34118
34119 /* Parse an Objective-C class interface. */
34120
34121 static void
34122 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
34123 {
34124 tree name, super, categ, protos;
34125 bool is_class_extension;
34126
34127 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
34128 location_t nam_loc = cp_lexer_peek_token (parser->lexer)->location;
34129 name = cp_parser_identifier (parser);
34130 if (name == error_mark_node)
34131 {
34132 /* It's hard to recover because even if valid @interface stuff
34133 is to follow, we can't compile it (or validate it) if we
34134 don't even know which class it refers to. Let's assume this
34135 was a stray '@interface' token in the stream and skip it.
34136 */
34137 return;
34138 }
34139 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
34140 &is_class_extension);
34141 protos = cp_parser_objc_protocol_refs_opt (parser);
34142
34143 /* We have either a class or a category on our hands. */
34144 if (categ || is_class_extension)
34145 objc_start_category_interface (name, categ, protos, attributes);
34146 else
34147 {
34148 objc_start_class_interface (name, nam_loc, super, protos, attributes);
34149 /* Handle instance variable declarations, if any. */
34150 cp_parser_objc_class_ivars (parser);
34151 objc_continue_interface ();
34152 }
34153
34154 cp_parser_objc_method_prototype_list (parser);
34155 }
34156
34157 /* Parse an Objective-C class implementation. */
34158
34159 static void
34160 cp_parser_objc_class_implementation (cp_parser* parser)
34161 {
34162 tree name, super, categ;
34163 bool is_class_extension;
34164
34165 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
34166 name = cp_parser_identifier (parser);
34167 if (name == error_mark_node)
34168 {
34169 /* It's hard to recover because even if valid @implementation
34170 stuff is to follow, we can't compile it (or validate it) if
34171 we don't even know which class it refers to. Let's assume
34172 this was a stray '@implementation' token in the stream and
34173 skip it.
34174 */
34175 return;
34176 }
34177 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
34178 &is_class_extension);
34179
34180 /* We have either a class or a category on our hands. */
34181 if (categ)
34182 objc_start_category_implementation (name, categ);
34183 else
34184 {
34185 objc_start_class_implementation (name, super);
34186 /* Handle instance variable declarations, if any. */
34187 cp_parser_objc_class_ivars (parser);
34188 objc_continue_implementation ();
34189 }
34190
34191 cp_parser_objc_method_definition_list (parser);
34192 }
34193
34194 /* Consume the @end token and finish off the implementation. */
34195
34196 static void
34197 cp_parser_objc_end_implementation (cp_parser* parser)
34198 {
34199 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
34200 objc_finish_implementation ();
34201 }
34202
34203 /* Parse an Objective-C declaration. */
34204
34205 static void
34206 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
34207 {
34208 /* Try to figure out what kind of declaration is present. */
34209 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
34210
34211 if (attributes)
34212 switch (kwd->keyword)
34213 {
34214 case RID_AT_ALIAS:
34215 case RID_AT_CLASS:
34216 case RID_AT_END:
34217 error_at (kwd->location, "attributes may not be specified before"
34218 " the %<@%D%> Objective-C++ keyword",
34219 kwd->u.value);
34220 attributes = NULL;
34221 break;
34222 case RID_AT_IMPLEMENTATION:
34223 warning_at (kwd->location, OPT_Wattributes,
34224 "prefix attributes are ignored before %<@%D%>",
34225 kwd->u.value);
34226 attributes = NULL;
34227 default:
34228 break;
34229 }
34230
34231 switch (kwd->keyword)
34232 {
34233 case RID_AT_ALIAS:
34234 cp_parser_objc_alias_declaration (parser);
34235 break;
34236 case RID_AT_CLASS:
34237 cp_parser_objc_class_declaration (parser);
34238 break;
34239 case RID_AT_PROTOCOL:
34240 cp_parser_objc_protocol_declaration (parser, attributes);
34241 break;
34242 case RID_AT_INTERFACE:
34243 cp_parser_objc_class_interface (parser, attributes);
34244 break;
34245 case RID_AT_IMPLEMENTATION:
34246 cp_parser_objc_class_implementation (parser);
34247 break;
34248 case RID_AT_END:
34249 cp_parser_objc_end_implementation (parser);
34250 break;
34251 default:
34252 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
34253 kwd->u.value);
34254 cp_parser_skip_to_end_of_block_or_statement (parser);
34255 }
34256 }
34257
34258 /* Parse an Objective-C try-catch-finally statement.
34259
34260 objc-try-catch-finally-stmt:
34261 @try compound-statement objc-catch-clause-seq [opt]
34262 objc-finally-clause [opt]
34263
34264 objc-catch-clause-seq:
34265 objc-catch-clause objc-catch-clause-seq [opt]
34266
34267 objc-catch-clause:
34268 @catch ( objc-exception-declaration ) compound-statement
34269
34270 objc-finally-clause:
34271 @finally compound-statement
34272
34273 objc-exception-declaration:
34274 parameter-declaration
34275 '...'
34276
34277 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
34278
34279 Returns NULL_TREE.
34280
34281 PS: This function is identical to c_parser_objc_try_catch_finally_statement
34282 for C. Keep them in sync. */
34283
34284 static tree
34285 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
34286 {
34287 location_t location;
34288 tree stmt;
34289
34290 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
34291 location = cp_lexer_peek_token (parser->lexer)->location;
34292 objc_maybe_warn_exceptions (location);
34293 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
34294 node, lest it get absorbed into the surrounding block. */
34295 stmt = push_stmt_list ();
34296 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
34297 objc_begin_try_stmt (location, pop_stmt_list (stmt));
34298
34299 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
34300 {
34301 cp_parameter_declarator *parm;
34302 tree parameter_declaration = error_mark_node;
34303 bool seen_open_paren = false;
34304 matching_parens parens;
34305
34306 cp_lexer_consume_token (parser->lexer);
34307 if (parens.require_open (parser))
34308 seen_open_paren = true;
34309 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
34310 {
34311 /* We have "@catch (...)" (where the '...' are literally
34312 what is in the code). Skip the '...'.
34313 parameter_declaration is set to NULL_TREE, and
34314 objc_being_catch_clauses() knows that that means
34315 '...'. */
34316 cp_lexer_consume_token (parser->lexer);
34317 parameter_declaration = NULL_TREE;
34318 }
34319 else
34320 {
34321 /* We have "@catch (NSException *exception)" or something
34322 like that. Parse the parameter declaration. */
34323 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
34324 false, NULL);
34325 if (parm == NULL)
34326 parameter_declaration = error_mark_node;
34327 else
34328 parameter_declaration = grokdeclarator (parm->declarator,
34329 &parm->decl_specifiers,
34330 PARM, /*initialized=*/0,
34331 /*attrlist=*/NULL);
34332 }
34333 if (seen_open_paren)
34334 parens.require_close (parser);
34335 else
34336 {
34337 /* If there was no open parenthesis, we are recovering from
34338 an error, and we are trying to figure out what mistake
34339 the user has made. */
34340
34341 /* If there is an immediate closing parenthesis, the user
34342 probably forgot the opening one (ie, they typed "@catch
34343 NSException *e)". Parse the closing parenthesis and keep
34344 going. */
34345 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
34346 cp_lexer_consume_token (parser->lexer);
34347
34348 /* If these is no immediate closing parenthesis, the user
34349 probably doesn't know that parenthesis are required at
34350 all (ie, they typed "@catch NSException *e"). So, just
34351 forget about the closing parenthesis and keep going. */
34352 }
34353 objc_begin_catch_clause (parameter_declaration);
34354 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
34355 objc_finish_catch_clause ();
34356 }
34357 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
34358 {
34359 cp_lexer_consume_token (parser->lexer);
34360 location = cp_lexer_peek_token (parser->lexer)->location;
34361 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
34362 node, lest it get absorbed into the surrounding block. */
34363 stmt = push_stmt_list ();
34364 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
34365 objc_build_finally_clause (location, pop_stmt_list (stmt));
34366 }
34367
34368 return objc_finish_try_stmt ();
34369 }
34370
34371 /* Parse an Objective-C synchronized statement.
34372
34373 objc-synchronized-stmt:
34374 @synchronized ( expression ) compound-statement
34375
34376 Returns NULL_TREE. */
34377
34378 static tree
34379 cp_parser_objc_synchronized_statement (cp_parser *parser)
34380 {
34381 location_t location;
34382 tree lock, stmt;
34383
34384 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
34385
34386 location = cp_lexer_peek_token (parser->lexer)->location;
34387 objc_maybe_warn_exceptions (location);
34388 matching_parens parens;
34389 parens.require_open (parser);
34390 lock = cp_parser_expression (parser);
34391 parens.require_close (parser);
34392
34393 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
34394 node, lest it get absorbed into the surrounding block. */
34395 stmt = push_stmt_list ();
34396 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
34397
34398 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
34399 }
34400
34401 /* Parse an Objective-C throw statement.
34402
34403 objc-throw-stmt:
34404 @throw assignment-expression [opt] ;
34405
34406 Returns a constructed '@throw' statement. */
34407
34408 static tree
34409 cp_parser_objc_throw_statement (cp_parser *parser)
34410 {
34411 tree expr = NULL_TREE;
34412 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34413
34414 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
34415
34416 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34417 expr = cp_parser_expression (parser);
34418
34419 cp_parser_consume_semicolon_at_end_of_statement (parser);
34420
34421 return objc_build_throw_stmt (loc, expr);
34422 }
34423
34424 /* Parse an Objective-C statement. */
34425
34426 static tree
34427 cp_parser_objc_statement (cp_parser * parser)
34428 {
34429 /* Try to figure out what kind of declaration is present. */
34430 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
34431
34432 switch (kwd->keyword)
34433 {
34434 case RID_AT_TRY:
34435 return cp_parser_objc_try_catch_finally_statement (parser);
34436 case RID_AT_SYNCHRONIZED:
34437 return cp_parser_objc_synchronized_statement (parser);
34438 case RID_AT_THROW:
34439 return cp_parser_objc_throw_statement (parser);
34440 default:
34441 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
34442 kwd->u.value);
34443 cp_parser_skip_to_end_of_block_or_statement (parser);
34444 }
34445
34446 return error_mark_node;
34447 }
34448
34449 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
34450 look ahead to see if an objc keyword follows the attributes. This
34451 is to detect the use of prefix attributes on ObjC @interface and
34452 @protocol. */
34453
34454 static bool
34455 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
34456 {
34457 cp_lexer_save_tokens (parser->lexer);
34458 tree addon = cp_parser_attributes_opt (parser);
34459 if (addon
34460 && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
34461 {
34462 cp_lexer_commit_tokens (parser->lexer);
34463 if (*attrib)
34464 TREE_CHAIN (*attrib) = addon;
34465 else
34466 *attrib = addon;
34467 return true;
34468 }
34469 cp_lexer_rollback_tokens (parser->lexer);
34470 return false;
34471 }
34472
34473 /* This routine is a minimal replacement for
34474 c_parser_struct_declaration () used when parsing the list of
34475 types/names or ObjC++ properties. For example, when parsing the
34476 code
34477
34478 @property (readonly) int a, b, c;
34479
34480 this function is responsible for parsing "int a, int b, int c" and
34481 returning the declarations as CHAIN of DECLs.
34482
34483 TODO: Share this code with cp_parser_objc_class_ivars. It's very
34484 similar parsing. */
34485 static tree
34486 cp_parser_objc_struct_declaration (cp_parser *parser)
34487 {
34488 tree decls = NULL_TREE;
34489 cp_decl_specifier_seq declspecs;
34490 int decl_class_or_enum_p;
34491 tree prefix_attributes;
34492
34493 cp_parser_decl_specifier_seq (parser,
34494 CP_PARSER_FLAGS_NONE,
34495 &declspecs,
34496 &decl_class_or_enum_p);
34497
34498 if (declspecs.type == error_mark_node)
34499 return error_mark_node;
34500
34501 /* auto, register, static, extern, mutable. */
34502 if (declspecs.storage_class != sc_none)
34503 {
34504 cp_parser_error (parser, "invalid type for property");
34505 declspecs.storage_class = sc_none;
34506 }
34507
34508 /* thread_local. */
34509 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
34510 {
34511 cp_parser_error (parser, "invalid type for property");
34512 declspecs.locations[ds_thread] = 0;
34513 }
34514
34515 /* typedef. */
34516 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
34517 {
34518 cp_parser_error (parser, "invalid type for property");
34519 declspecs.locations[ds_typedef] = 0;
34520 }
34521
34522 prefix_attributes = declspecs.attributes;
34523 declspecs.attributes = NULL_TREE;
34524
34525 /* Keep going until we hit the `;' at the end of the declaration. */
34526 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34527 {
34528 tree attributes, first_attribute, decl;
34529 cp_declarator *declarator;
34530 cp_token *token;
34531
34532 /* Parse the declarator. */
34533 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
34534 CP_PARSER_FLAGS_NONE,
34535 NULL, NULL, false, false, false);
34536
34537 /* Look for attributes that apply to the ivar. */
34538 attributes = cp_parser_attributes_opt (parser);
34539 /* Remember which attributes are prefix attributes and
34540 which are not. */
34541 first_attribute = attributes;
34542 /* Combine the attributes. */
34543 attributes = attr_chainon (prefix_attributes, attributes);
34544
34545 decl = grokfield (declarator, &declspecs,
34546 NULL_TREE, /*init_const_expr_p=*/false,
34547 NULL_TREE, attributes);
34548
34549 if (decl == error_mark_node || decl == NULL_TREE)
34550 return error_mark_node;
34551
34552 /* Reset PREFIX_ATTRIBUTES. */
34553 if (attributes != error_mark_node)
34554 {
34555 while (attributes && TREE_CHAIN (attributes) != first_attribute)
34556 attributes = TREE_CHAIN (attributes);
34557 if (attributes)
34558 TREE_CHAIN (attributes) = NULL_TREE;
34559 }
34560
34561 DECL_CHAIN (decl) = decls;
34562 decls = decl;
34563
34564 token = cp_lexer_peek_token (parser->lexer);
34565 if (token->type == CPP_COMMA)
34566 {
34567 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
34568 continue;
34569 }
34570 else
34571 break;
34572 }
34573 return decls;
34574 }
34575
34576 /* Parse an Objective-C @property declaration. The syntax is:
34577
34578 objc-property-declaration:
34579 '@property' objc-property-attributes[opt] struct-declaration ;
34580
34581 objc-property-attributes:
34582 '(' objc-property-attribute-list ')'
34583
34584 objc-property-attribute-list:
34585 objc-property-attribute
34586 objc-property-attribute-list, objc-property-attribute
34587
34588 objc-property-attribute
34589 'getter' = identifier
34590 'setter' = identifier
34591 'readonly'
34592 'readwrite'
34593 'assign'
34594 'retain'
34595 'copy'
34596 'nonatomic'
34597
34598 For example:
34599 @property NSString *name;
34600 @property (readonly) id object;
34601 @property (retain, nonatomic, getter=getTheName) id name;
34602 @property int a, b, c;
34603
34604 PS: This function is identical to
34605 c_parser_objc_at_property_declaration for C. Keep them in sync. */
34606 static void
34607 cp_parser_objc_at_property_declaration (cp_parser *parser)
34608 {
34609 /* Parse the optional attribute list.
34610
34611 A list of parsed, but not verified, attributes. */
34612 vec<property_attribute_info *> prop_attr_list = vNULL;
34613 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34614
34615 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
34616
34617 /* Parse the optional attribute list... */
34618 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34619 {
34620 /* Eat the '('. */
34621 matching_parens parens;
34622 location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
34623 parens.consume_open (parser);
34624 bool syntax_error = false;
34625
34626 /* Allow empty @property attribute lists, but with a warning. */
34627 location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
34628 location_t attr_comb;
34629 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
34630 {
34631 attr_comb = make_location (attr_end, attr_start, attr_end);
34632 warning_at (attr_comb, OPT_Wattributes,
34633 "empty property attribute list");
34634 }
34635 else
34636 while (true)
34637 {
34638 cp_token *token = cp_lexer_peek_token (parser->lexer);
34639 attr_start = token->location;
34640 attr_end = get_finish (token->location);
34641 attr_comb = make_location (attr_start, attr_start, attr_end);
34642
34643 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
34644 {
34645 warning_at (attr_comb, OPT_Wattributes,
34646 "missing property attribute");
34647 if (token->type == CPP_CLOSE_PAREN)
34648 break;
34649 cp_lexer_consume_token (parser->lexer);
34650 continue;
34651 }
34652
34653 tree attr_name = NULL_TREE;
34654 if (identifier_p (token->u.value))
34655 attr_name = token->u.value;
34656
34657 enum rid keyword;
34658 if (token->type == CPP_NAME)
34659 keyword = C_RID_CODE (token->u.value);
34660 else if (token->type == CPP_KEYWORD
34661 && token->keyword == RID_CLASS)
34662 /* Account for accepting the 'class' keyword in this context. */
34663 keyword = RID_CLASS;
34664 else
34665 keyword = RID_MAX; /* By definition, an unknown property. */
34666 cp_lexer_consume_token (parser->lexer);
34667
34668 enum objc_property_attribute_kind prop_kind
34669 = objc_prop_attr_kind_for_rid (keyword);
34670 property_attribute_info *prop
34671 = new property_attribute_info (attr_name, attr_comb, prop_kind);
34672 prop_attr_list.safe_push (prop);
34673
34674 tree meth_name;
34675 switch (prop->prop_kind)
34676 {
34677 default: break;
34678 case OBJC_PROPERTY_ATTR_UNKNOWN:
34679 if (attr_name)
34680 error_at (attr_start, "unknown property attribute %qE",
34681 attr_name);
34682 else
34683 error_at (attr_start, "unknown property attribute");
34684 prop->parse_error = syntax_error = true;
34685 break;
34686
34687 case OBJC_PROPERTY_ATTR_GETTER:
34688 case OBJC_PROPERTY_ATTR_SETTER:
34689 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34690 {
34691 attr_comb = make_location (attr_end, attr_start, attr_end);
34692 error_at (attr_comb, "expected %<=%> after Objective-C %qE",
34693 attr_name);
34694 prop->parse_error = syntax_error = true;
34695 break;
34696 }
34697
34698 token = cp_lexer_peek_token (parser->lexer);
34699 attr_end = token->location;
34700 cp_lexer_consume_token (parser->lexer); /* eat the = */
34701
34702 if (!cp_parser_objc_selector_p
34703 (cp_lexer_peek_token (parser->lexer)->type))
34704 {
34705 attr_comb = make_location (attr_end, attr_start, attr_end);
34706 error_at (attr_comb, "expected %qE selector name",
34707 attr_name);
34708 prop->parse_error = syntax_error = true;
34709 break;
34710 }
34711
34712 /* Get the end of the method name, and consume the name. */
34713 token = cp_lexer_peek_token (parser->lexer);
34714 attr_end = get_finish (token->location);
34715 /* Because method names may contain C++ keywords, we have a
34716 routine to fetch them (this also consumes the token). */
34717 meth_name = cp_parser_objc_selector (parser);
34718
34719 if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
34720 {
34721 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
34722 {
34723 attr_comb = make_location (attr_end, attr_start,
34724 attr_end);
34725 error_at (attr_comb, "setter method names must"
34726 " terminate with %<:%>");
34727 prop->parse_error = syntax_error = true;
34728 }
34729 else
34730 {
34731 attr_end = get_finish (cp_lexer_peek_token
34732 (parser->lexer)->location);
34733 cp_lexer_consume_token (parser->lexer);
34734 }
34735 attr_comb = make_location (attr_start, attr_start,
34736 attr_end);
34737 }
34738 else
34739 attr_comb = make_location (attr_start, attr_start,
34740 attr_end);
34741 prop->ident = meth_name;
34742 /* Updated location including all that was successfully
34743 parsed. */
34744 prop->prop_loc = attr_comb;
34745 break;
34746 }
34747
34748 /* If we see a comma here, then keep going - even if we already
34749 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
34750 this makes a more useful output and avoid spurious warnings
34751 about missing attributes that are, in fact, specified after the
34752 one with the syntax error. */
34753 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34754 cp_lexer_consume_token (parser->lexer);
34755 else
34756 break;
34757 }
34758
34759 if (syntax_error || !parens.require_close (parser))
34760 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34761 /*or_comma=*/false,
34762 /*consume_paren=*/true);
34763 }
34764
34765 /* 'properties' is the list of properties that we read. Usually a
34766 single one, but maybe more (eg, in "@property int a, b, c;" there
34767 are three).
34768 TODO: Update this parsing so that it accepts (erroneous) bitfields so
34769 that we can issue a meaningful and consistent (between C/C++) error
34770 message from objc_add_property_declaration (). */
34771 tree properties = cp_parser_objc_struct_declaration (parser);
34772
34773 if (properties == error_mark_node)
34774 cp_parser_skip_to_end_of_statement (parser);
34775 else if (properties == NULL_TREE)
34776 cp_parser_error (parser, "expected identifier");
34777 else
34778 {
34779 /* Comma-separated properties are chained together in reverse order;
34780 add them one by one. */
34781 properties = nreverse (properties);
34782 for (; properties; properties = TREE_CHAIN (properties))
34783 objc_add_property_declaration (loc, copy_node (properties),
34784 prop_attr_list);
34785 }
34786
34787 cp_parser_consume_semicolon_at_end_of_statement (parser);
34788
34789 while (!prop_attr_list.is_empty())
34790 delete prop_attr_list.pop ();
34791 prop_attr_list.release ();
34792 }
34793
34794 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
34795
34796 objc-synthesize-declaration:
34797 @synthesize objc-synthesize-identifier-list ;
34798
34799 objc-synthesize-identifier-list:
34800 objc-synthesize-identifier
34801 objc-synthesize-identifier-list, objc-synthesize-identifier
34802
34803 objc-synthesize-identifier
34804 identifier
34805 identifier = identifier
34806
34807 For example:
34808 @synthesize MyProperty;
34809 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
34810
34811 PS: This function is identical to c_parser_objc_at_synthesize_declaration
34812 for C. Keep them in sync.
34813 */
34814 static void
34815 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
34816 {
34817 tree list = NULL_TREE;
34818 location_t loc;
34819 loc = cp_lexer_peek_token (parser->lexer)->location;
34820
34821 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
34822 while (true)
34823 {
34824 tree property, ivar;
34825 property = cp_parser_identifier (parser);
34826 if (property == error_mark_node)
34827 {
34828 cp_parser_consume_semicolon_at_end_of_statement (parser);
34829 return;
34830 }
34831 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
34832 {
34833 cp_lexer_consume_token (parser->lexer);
34834 ivar = cp_parser_identifier (parser);
34835 if (ivar == error_mark_node)
34836 {
34837 cp_parser_consume_semicolon_at_end_of_statement (parser);
34838 return;
34839 }
34840 }
34841 else
34842 ivar = NULL_TREE;
34843 list = chainon (list, build_tree_list (ivar, property));
34844 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34845 cp_lexer_consume_token (parser->lexer);
34846 else
34847 break;
34848 }
34849 cp_parser_consume_semicolon_at_end_of_statement (parser);
34850 objc_add_synthesize_declaration (loc, list);
34851 }
34852
34853 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
34854
34855 objc-dynamic-declaration:
34856 @dynamic identifier-list ;
34857
34858 For example:
34859 @dynamic MyProperty;
34860 @dynamic MyProperty, AnotherProperty;
34861
34862 PS: This function is identical to c_parser_objc_at_dynamic_declaration
34863 for C. Keep them in sync.
34864 */
34865 static void
34866 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
34867 {
34868 tree list = NULL_TREE;
34869 location_t loc;
34870 loc = cp_lexer_peek_token (parser->lexer)->location;
34871
34872 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
34873 while (true)
34874 {
34875 tree property;
34876 property = cp_parser_identifier (parser);
34877 if (property == error_mark_node)
34878 {
34879 cp_parser_consume_semicolon_at_end_of_statement (parser);
34880 return;
34881 }
34882 list = chainon (list, build_tree_list (NULL, property));
34883 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34884 cp_lexer_consume_token (parser->lexer);
34885 else
34886 break;
34887 }
34888 cp_parser_consume_semicolon_at_end_of_statement (parser);
34889 objc_add_dynamic_declaration (loc, list);
34890 }
34891
34892 \f
34893 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
34894
34895 /* Returns name of the next clause.
34896 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
34897 the token is not consumed. Otherwise appropriate pragma_omp_clause is
34898 returned and the token is consumed. */
34899
34900 static pragma_omp_clause
34901 cp_parser_omp_clause_name (cp_parser *parser)
34902 {
34903 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
34904
34905 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
34906 result = PRAGMA_OACC_CLAUSE_AUTO;
34907 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
34908 result = PRAGMA_OMP_CLAUSE_IF;
34909 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
34910 result = PRAGMA_OMP_CLAUSE_DEFAULT;
34911 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
34912 result = PRAGMA_OACC_CLAUSE_DELETE;
34913 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
34914 result = PRAGMA_OMP_CLAUSE_PRIVATE;
34915 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34916 result = PRAGMA_OMP_CLAUSE_FOR;
34917 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34918 {
34919 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34920 const char *p = IDENTIFIER_POINTER (id);
34921
34922 switch (p[0])
34923 {
34924 case 'a':
34925 if (!strcmp ("aligned", p))
34926 result = PRAGMA_OMP_CLAUSE_ALIGNED;
34927 else if (!strcmp ("allocate", p))
34928 result = PRAGMA_OMP_CLAUSE_ALLOCATE;
34929 else if (!strcmp ("async", p))
34930 result = PRAGMA_OACC_CLAUSE_ASYNC;
34931 else if (!strcmp ("attach", p))
34932 result = PRAGMA_OACC_CLAUSE_ATTACH;
34933 break;
34934 case 'b':
34935 if (!strcmp ("bind", p))
34936 result = PRAGMA_OMP_CLAUSE_BIND;
34937 break;
34938 case 'c':
34939 if (!strcmp ("collapse", p))
34940 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
34941 else if (!strcmp ("copy", p))
34942 result = PRAGMA_OACC_CLAUSE_COPY;
34943 else if (!strcmp ("copyin", p))
34944 result = PRAGMA_OMP_CLAUSE_COPYIN;
34945 else if (!strcmp ("copyout", p))
34946 result = PRAGMA_OACC_CLAUSE_COPYOUT;
34947 else if (!strcmp ("copyprivate", p))
34948 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
34949 else if (!strcmp ("create", p))
34950 result = PRAGMA_OACC_CLAUSE_CREATE;
34951 break;
34952 case 'd':
34953 if (!strcmp ("defaultmap", p))
34954 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
34955 else if (!strcmp ("depend", p))
34956 result = PRAGMA_OMP_CLAUSE_DEPEND;
34957 else if (!strcmp ("detach", p))
34958 result = PRAGMA_OACC_CLAUSE_DETACH;
34959 else if (!strcmp ("device", p))
34960 result = PRAGMA_OMP_CLAUSE_DEVICE;
34961 else if (!strcmp ("deviceptr", p))
34962 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
34963 else if (!strcmp ("device_resident", p))
34964 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
34965 else if (!strcmp ("device_type", p))
34966 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
34967 else if (!strcmp ("dist_schedule", p))
34968 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
34969 break;
34970 case 'f':
34971 if (!strcmp ("final", p))
34972 result = PRAGMA_OMP_CLAUSE_FINAL;
34973 else if (!strcmp ("finalize", p))
34974 result = PRAGMA_OACC_CLAUSE_FINALIZE;
34975 else if (!strcmp ("firstprivate", p))
34976 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
34977 else if (!strcmp ("from", p))
34978 result = PRAGMA_OMP_CLAUSE_FROM;
34979 break;
34980 case 'g':
34981 if (!strcmp ("gang", p))
34982 result = PRAGMA_OACC_CLAUSE_GANG;
34983 else if (!strcmp ("grainsize", p))
34984 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
34985 break;
34986 case 'h':
34987 if (!strcmp ("hint", p))
34988 result = PRAGMA_OMP_CLAUSE_HINT;
34989 else if (!strcmp ("host", p))
34990 result = PRAGMA_OACC_CLAUSE_HOST;
34991 break;
34992 case 'i':
34993 if (!strcmp ("if_present", p))
34994 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
34995 else if (!strcmp ("in_reduction", p))
34996 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
34997 else if (!strcmp ("inbranch", p))
34998 result = PRAGMA_OMP_CLAUSE_INBRANCH;
34999 else if (!strcmp ("independent", p))
35000 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
35001 else if (!strcmp ("is_device_ptr", p))
35002 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
35003 break;
35004 case 'l':
35005 if (!strcmp ("lastprivate", p))
35006 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
35007 else if (!strcmp ("linear", p))
35008 result = PRAGMA_OMP_CLAUSE_LINEAR;
35009 else if (!strcmp ("link", p))
35010 result = PRAGMA_OMP_CLAUSE_LINK;
35011 break;
35012 case 'm':
35013 if (!strcmp ("map", p))
35014 result = PRAGMA_OMP_CLAUSE_MAP;
35015 else if (!strcmp ("mergeable", p))
35016 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
35017 break;
35018 case 'n':
35019 if (!strcmp ("no_create", p))
35020 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
35021 else if (!strcmp ("nogroup", p))
35022 result = PRAGMA_OMP_CLAUSE_NOGROUP;
35023 else if (!strcmp ("nontemporal", p))
35024 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
35025 else if (!strcmp ("notinbranch", p))
35026 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
35027 else if (!strcmp ("nowait", p))
35028 result = PRAGMA_OMP_CLAUSE_NOWAIT;
35029 else if (!strcmp ("num_gangs", p))
35030 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
35031 else if (!strcmp ("num_tasks", p))
35032 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
35033 else if (!strcmp ("num_teams", p))
35034 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
35035 else if (!strcmp ("num_threads", p))
35036 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
35037 else if (!strcmp ("num_workers", p))
35038 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
35039 break;
35040 case 'o':
35041 if (!strcmp ("ordered", p))
35042 result = PRAGMA_OMP_CLAUSE_ORDERED;
35043 else if (!strcmp ("order", p))
35044 result = PRAGMA_OMP_CLAUSE_ORDER;
35045 break;
35046 case 'p':
35047 if (!strcmp ("parallel", p))
35048 result = PRAGMA_OMP_CLAUSE_PARALLEL;
35049 else if (!strcmp ("present", p))
35050 result = PRAGMA_OACC_CLAUSE_PRESENT;
35051 else if (!strcmp ("present_or_copy", p)
35052 || !strcmp ("pcopy", p))
35053 result = PRAGMA_OACC_CLAUSE_COPY;
35054 else if (!strcmp ("present_or_copyin", p)
35055 || !strcmp ("pcopyin", p))
35056 result = PRAGMA_OACC_CLAUSE_COPYIN;
35057 else if (!strcmp ("present_or_copyout", p)
35058 || !strcmp ("pcopyout", p))
35059 result = PRAGMA_OACC_CLAUSE_COPYOUT;
35060 else if (!strcmp ("present_or_create", p)
35061 || !strcmp ("pcreate", p))
35062 result = PRAGMA_OACC_CLAUSE_CREATE;
35063 else if (!strcmp ("priority", p))
35064 result = PRAGMA_OMP_CLAUSE_PRIORITY;
35065 else if (!strcmp ("proc_bind", p))
35066 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
35067 break;
35068 case 'r':
35069 if (!strcmp ("reduction", p))
35070 result = PRAGMA_OMP_CLAUSE_REDUCTION;
35071 break;
35072 case 's':
35073 if (!strcmp ("safelen", p))
35074 result = PRAGMA_OMP_CLAUSE_SAFELEN;
35075 else if (!strcmp ("schedule", p))
35076 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
35077 else if (!strcmp ("sections", p))
35078 result = PRAGMA_OMP_CLAUSE_SECTIONS;
35079 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
35080 result = PRAGMA_OACC_CLAUSE_HOST;
35081 else if (!strcmp ("seq", p))
35082 result = PRAGMA_OACC_CLAUSE_SEQ;
35083 else if (!strcmp ("shared", p))
35084 result = PRAGMA_OMP_CLAUSE_SHARED;
35085 else if (!strcmp ("simd", p))
35086 result = PRAGMA_OMP_CLAUSE_SIMD;
35087 else if (!strcmp ("simdlen", p))
35088 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
35089 break;
35090 case 't':
35091 if (!strcmp ("task_reduction", p))
35092 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
35093 else if (!strcmp ("taskgroup", p))
35094 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
35095 else if (!strcmp ("thread_limit", p))
35096 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
35097 else if (!strcmp ("threads", p))
35098 result = PRAGMA_OMP_CLAUSE_THREADS;
35099 else if (!strcmp ("tile", p))
35100 result = PRAGMA_OACC_CLAUSE_TILE;
35101 else if (!strcmp ("to", p))
35102 result = PRAGMA_OMP_CLAUSE_TO;
35103 break;
35104 case 'u':
35105 if (!strcmp ("uniform", p))
35106 result = PRAGMA_OMP_CLAUSE_UNIFORM;
35107 else if (!strcmp ("untied", p))
35108 result = PRAGMA_OMP_CLAUSE_UNTIED;
35109 else if (!strcmp ("use_device", p))
35110 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
35111 else if (!strcmp ("use_device_addr", p))
35112 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
35113 else if (!strcmp ("use_device_ptr", p))
35114 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
35115 break;
35116 case 'v':
35117 if (!strcmp ("vector", p))
35118 result = PRAGMA_OACC_CLAUSE_VECTOR;
35119 else if (!strcmp ("vector_length", p))
35120 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
35121 break;
35122 case 'w':
35123 if (!strcmp ("wait", p))
35124 result = PRAGMA_OACC_CLAUSE_WAIT;
35125 else if (!strcmp ("worker", p))
35126 result = PRAGMA_OACC_CLAUSE_WORKER;
35127 break;
35128 }
35129 }
35130
35131 if (result != PRAGMA_OMP_CLAUSE_NONE)
35132 cp_lexer_consume_token (parser->lexer);
35133
35134 return result;
35135 }
35136
35137 /* Validate that a clause of the given type does not already exist. */
35138
35139 static void
35140 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
35141 const char *name, location_t location)
35142 {
35143 if (omp_find_clause (clauses, code))
35144 error_at (location, "too many %qs clauses", name);
35145 }
35146
35147 /* OpenMP 2.5:
35148 variable-list:
35149 identifier
35150 variable-list , identifier
35151
35152 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
35153 colon). An opening parenthesis will have been consumed by the caller.
35154
35155 If KIND is nonzero, create the appropriate node and install the decl
35156 in OMP_CLAUSE_DECL and add the node to the head of the list.
35157
35158 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
35159 return the list created.
35160
35161 COLON can be NULL if only closing parenthesis should end the list,
35162 or pointer to bool which will receive false if the list is terminated
35163 by closing parenthesis or true if the list is terminated by colon.
35164
35165 The optional ALLOW_DEREF argument is true if list items can use the deref
35166 (->) operator. */
35167
35168 static tree
35169 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
35170 tree list, bool *colon,
35171 bool allow_deref = false)
35172 {
35173 cp_token *token;
35174 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
35175 if (colon)
35176 {
35177 parser->colon_corrects_to_scope_p = false;
35178 *colon = false;
35179 }
35180 while (1)
35181 {
35182 tree name, decl;
35183
35184 if (kind == OMP_CLAUSE_DEPEND)
35185 cp_parser_parse_tentatively (parser);
35186 token = cp_lexer_peek_token (parser->lexer);
35187 if (kind != 0
35188 && current_class_ptr
35189 && cp_parser_is_keyword (token, RID_THIS))
35190 {
35191 decl = finish_this_expr ();
35192 if (TREE_CODE (decl) == NON_LVALUE_EXPR
35193 || CONVERT_EXPR_P (decl))
35194 decl = TREE_OPERAND (decl, 0);
35195 cp_lexer_consume_token (parser->lexer);
35196 }
35197 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
35198 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
35199 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
35200 {
35201 cp_id_kind idk;
35202 decl = cp_parser_primary_expression (parser, false, false, false,
35203 &idk);
35204 }
35205 else
35206 {
35207 name = cp_parser_id_expression (parser, /*template_p=*/false,
35208 /*check_dependency_p=*/true,
35209 /*template_p=*/NULL,
35210 /*declarator_p=*/false,
35211 /*optional_p=*/false);
35212 if (name == error_mark_node)
35213 {
35214 if (kind == OMP_CLAUSE_DEPEND
35215 && cp_parser_simulate_error (parser))
35216 goto depend_lvalue;
35217 goto skip_comma;
35218 }
35219
35220 if (identifier_p (name))
35221 decl = cp_parser_lookup_name_simple (parser, name, token->location);
35222 else
35223 decl = name;
35224 if (decl == error_mark_node)
35225 {
35226 if (kind == OMP_CLAUSE_DEPEND
35227 && cp_parser_simulate_error (parser))
35228 goto depend_lvalue;
35229 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
35230 token->location);
35231 }
35232 }
35233 if (outer_automatic_var_p (decl))
35234 decl = process_outer_var_ref (decl, tf_warning_or_error);
35235 if (decl == error_mark_node)
35236 ;
35237 else if (kind != 0)
35238 {
35239 switch (kind)
35240 {
35241 case OMP_CLAUSE__CACHE_:
35242 /* The OpenACC cache directive explicitly only allows "array
35243 elements or subarrays". */
35244 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
35245 {
35246 error_at (token->location, "expected %<[%>");
35247 decl = error_mark_node;
35248 break;
35249 }
35250 /* FALLTHROUGH. */
35251 case OMP_CLAUSE_MAP:
35252 case OMP_CLAUSE_FROM:
35253 case OMP_CLAUSE_TO:
35254 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
35255 || (allow_deref
35256 && cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
35257 {
35258 cpp_ttype ttype
35259 = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
35260 ? CPP_DOT : CPP_DEREF;
35261 location_t loc
35262 = cp_lexer_peek_token (parser->lexer)->location;
35263 cp_id_kind idk = CP_ID_KIND_NONE;
35264 cp_lexer_consume_token (parser->lexer);
35265 decl = convert_from_reference (decl);
35266 decl
35267 = cp_parser_postfix_dot_deref_expression (parser, ttype,
35268 decl, false,
35269 &idk, loc);
35270 }
35271 /* FALLTHROUGH. */
35272 case OMP_CLAUSE_DEPEND:
35273 case OMP_CLAUSE_REDUCTION:
35274 case OMP_CLAUSE_IN_REDUCTION:
35275 case OMP_CLAUSE_TASK_REDUCTION:
35276 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
35277 {
35278 tree low_bound = NULL_TREE, length = NULL_TREE;
35279
35280 parser->colon_corrects_to_scope_p = false;
35281 cp_lexer_consume_token (parser->lexer);
35282 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
35283 {
35284 low_bound = cp_parser_expression (parser);
35285 /* Later handling is not prepared to see through these. */
35286 gcc_checking_assert (!location_wrapper_p (low_bound));
35287 }
35288 if (!colon)
35289 parser->colon_corrects_to_scope_p
35290 = saved_colon_corrects_to_scope_p;
35291 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
35292 length = integer_one_node;
35293 else
35294 {
35295 /* Look for `:'. */
35296 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35297 {
35298 if (kind == OMP_CLAUSE_DEPEND
35299 && cp_parser_simulate_error (parser))
35300 goto depend_lvalue;
35301 goto skip_comma;
35302 }
35303 if (kind == OMP_CLAUSE_DEPEND)
35304 cp_parser_commit_to_tentative_parse (parser);
35305 if (!cp_lexer_next_token_is (parser->lexer,
35306 CPP_CLOSE_SQUARE))
35307 {
35308 length = cp_parser_expression (parser);
35309 /* Later handling is not prepared to see through these. */
35310 gcc_checking_assert (!location_wrapper_p (length));
35311 }
35312 }
35313 /* Look for the closing `]'. */
35314 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
35315 RT_CLOSE_SQUARE))
35316 {
35317 if (kind == OMP_CLAUSE_DEPEND
35318 && cp_parser_simulate_error (parser))
35319 goto depend_lvalue;
35320 goto skip_comma;
35321 }
35322
35323 decl = tree_cons (low_bound, length, decl);
35324 }
35325 break;
35326 default:
35327 break;
35328 }
35329
35330 if (kind == OMP_CLAUSE_DEPEND)
35331 {
35332 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
35333 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
35334 && cp_parser_simulate_error (parser))
35335 {
35336 depend_lvalue:
35337 cp_parser_abort_tentative_parse (parser);
35338 decl = cp_parser_assignment_expression (parser, NULL,
35339 false, false);
35340 }
35341 else
35342 cp_parser_parse_definitely (parser);
35343 }
35344
35345 tree u = build_omp_clause (token->location, kind);
35346 OMP_CLAUSE_DECL (u) = decl;
35347 OMP_CLAUSE_CHAIN (u) = list;
35348 list = u;
35349 }
35350 else
35351 list = tree_cons (decl, NULL_TREE, list);
35352
35353 get_comma:
35354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
35355 break;
35356 cp_lexer_consume_token (parser->lexer);
35357 }
35358
35359 if (colon)
35360 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
35361
35362 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
35363 {
35364 *colon = true;
35365 cp_parser_require (parser, CPP_COLON, RT_COLON);
35366 return list;
35367 }
35368
35369 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35370 {
35371 int ending;
35372
35373 /* Try to resync to an unnested comma. Copied from
35374 cp_parser_parenthesized_expression_list. */
35375 skip_comma:
35376 if (colon)
35377 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
35378 ending = cp_parser_skip_to_closing_parenthesis (parser,
35379 /*recovering=*/true,
35380 /*or_comma=*/true,
35381 /*consume_paren=*/true);
35382 if (ending < 0)
35383 goto get_comma;
35384 }
35385
35386 return list;
35387 }
35388
35389 /* Similarly, but expect leading and trailing parenthesis. This is a very
35390 common case for omp clauses. */
35391
35392 static tree
35393 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
35394 bool allow_deref = false)
35395 {
35396 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35397 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
35398 allow_deref);
35399 return list;
35400 }
35401
35402 /* OpenACC 2.0:
35403 copy ( variable-list )
35404 copyin ( variable-list )
35405 copyout ( variable-list )
35406 create ( variable-list )
35407 delete ( variable-list )
35408 present ( variable-list )
35409
35410 OpenACC 2.6:
35411 no_create ( variable-list )
35412 attach ( variable-list )
35413 detach ( variable-list ) */
35414
35415 static tree
35416 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
35417 tree list)
35418 {
35419 enum gomp_map_kind kind;
35420 switch (c_kind)
35421 {
35422 case PRAGMA_OACC_CLAUSE_ATTACH:
35423 kind = GOMP_MAP_ATTACH;
35424 break;
35425 case PRAGMA_OACC_CLAUSE_COPY:
35426 kind = GOMP_MAP_TOFROM;
35427 break;
35428 case PRAGMA_OACC_CLAUSE_COPYIN:
35429 kind = GOMP_MAP_TO;
35430 break;
35431 case PRAGMA_OACC_CLAUSE_COPYOUT:
35432 kind = GOMP_MAP_FROM;
35433 break;
35434 case PRAGMA_OACC_CLAUSE_CREATE:
35435 kind = GOMP_MAP_ALLOC;
35436 break;
35437 case PRAGMA_OACC_CLAUSE_DELETE:
35438 kind = GOMP_MAP_RELEASE;
35439 break;
35440 case PRAGMA_OACC_CLAUSE_DETACH:
35441 kind = GOMP_MAP_DETACH;
35442 break;
35443 case PRAGMA_OACC_CLAUSE_DEVICE:
35444 kind = GOMP_MAP_FORCE_TO;
35445 break;
35446 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
35447 kind = GOMP_MAP_DEVICE_RESIDENT;
35448 break;
35449 case PRAGMA_OACC_CLAUSE_HOST:
35450 kind = GOMP_MAP_FORCE_FROM;
35451 break;
35452 case PRAGMA_OACC_CLAUSE_LINK:
35453 kind = GOMP_MAP_LINK;
35454 break;
35455 case PRAGMA_OACC_CLAUSE_NO_CREATE:
35456 kind = GOMP_MAP_IF_PRESENT;
35457 break;
35458 case PRAGMA_OACC_CLAUSE_PRESENT:
35459 kind = GOMP_MAP_FORCE_PRESENT;
35460 break;
35461 default:
35462 gcc_unreachable ();
35463 }
35464 tree nl, c;
35465 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, true);
35466
35467 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
35468 OMP_CLAUSE_SET_MAP_KIND (c, kind);
35469
35470 return nl;
35471 }
35472
35473 /* OpenACC 2.0:
35474 deviceptr ( variable-list ) */
35475
35476 static tree
35477 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
35478 {
35479 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35480 tree vars, t;
35481
35482 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
35483 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
35484 variable-list must only allow for pointer variables. */
35485 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35486 for (t = vars; t; t = TREE_CHAIN (t))
35487 {
35488 tree v = TREE_PURPOSE (t);
35489 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
35490 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
35491 OMP_CLAUSE_DECL (u) = v;
35492 OMP_CLAUSE_CHAIN (u) = list;
35493 list = u;
35494 }
35495
35496 return list;
35497 }
35498
35499 /* OpenACC 2.5:
35500 auto
35501 finalize
35502 independent
35503 nohost
35504 seq */
35505
35506 static tree
35507 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
35508 tree list)
35509 {
35510 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
35511
35512 tree c = build_omp_clause (loc, code);
35513 OMP_CLAUSE_CHAIN (c) = list;
35514
35515 return c;
35516 }
35517
35518 /* OpenACC:
35519 num_gangs ( expression )
35520 num_workers ( expression )
35521 vector_length ( expression ) */
35522
35523 static tree
35524 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
35525 const char *str, tree list)
35526 {
35527 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35528
35529 matching_parens parens;
35530 if (!parens.require_open (parser))
35531 return list;
35532
35533 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
35534
35535 if (t == error_mark_node
35536 || !parens.require_close (parser))
35537 {
35538 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35539 /*or_comma=*/false,
35540 /*consume_paren=*/true);
35541 return list;
35542 }
35543
35544 check_no_duplicate_clause (list, code, str, loc);
35545
35546 tree c = build_omp_clause (loc, code);
35547 OMP_CLAUSE_OPERAND (c, 0) = t;
35548 OMP_CLAUSE_CHAIN (c) = list;
35549 return c;
35550 }
35551
35552 /* OpenACC:
35553
35554 gang [( gang-arg-list )]
35555 worker [( [num:] int-expr )]
35556 vector [( [length:] int-expr )]
35557
35558 where gang-arg is one of:
35559
35560 [num:] int-expr
35561 static: size-expr
35562
35563 and size-expr may be:
35564
35565 *
35566 int-expr
35567 */
35568
35569 static tree
35570 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
35571 omp_clause_code kind,
35572 const char *str, tree list)
35573 {
35574 const char *id = "num";
35575 cp_lexer *lexer = parser->lexer;
35576 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
35577
35578 if (kind == OMP_CLAUSE_VECTOR)
35579 id = "length";
35580
35581 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
35582 {
35583 matching_parens parens;
35584 parens.consume_open (parser);
35585
35586 do
35587 {
35588 cp_token *next = cp_lexer_peek_token (lexer);
35589 int idx = 0;
35590
35591 /* Gang static argument. */
35592 if (kind == OMP_CLAUSE_GANG
35593 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
35594 {
35595 cp_lexer_consume_token (lexer);
35596
35597 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35598 goto cleanup_error;
35599
35600 idx = 1;
35601 if (ops[idx] != NULL)
35602 {
35603 cp_parser_error (parser, "too many %<static%> arguments");
35604 goto cleanup_error;
35605 }
35606
35607 /* Check for the '*' argument. */
35608 if (cp_lexer_next_token_is (lexer, CPP_MULT)
35609 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
35610 || cp_lexer_nth_token_is (parser->lexer, 2,
35611 CPP_CLOSE_PAREN)))
35612 {
35613 cp_lexer_consume_token (lexer);
35614 ops[idx] = integer_minus_one_node;
35615
35616 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
35617 {
35618 cp_lexer_consume_token (lexer);
35619 continue;
35620 }
35621 else break;
35622 }
35623 }
35624 /* Worker num: argument and vector length: arguments. */
35625 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
35626 && id_equal (next->u.value, id)
35627 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
35628 {
35629 cp_lexer_consume_token (lexer); /* id */
35630 cp_lexer_consume_token (lexer); /* ':' */
35631 }
35632
35633 /* Now collect the actual argument. */
35634 if (ops[idx] != NULL_TREE)
35635 {
35636 cp_parser_error (parser, "unexpected argument");
35637 goto cleanup_error;
35638 }
35639
35640 tree expr = cp_parser_assignment_expression (parser, NULL, false,
35641 false);
35642 if (expr == error_mark_node)
35643 goto cleanup_error;
35644
35645 mark_exp_read (expr);
35646 ops[idx] = expr;
35647
35648 if (kind == OMP_CLAUSE_GANG
35649 && cp_lexer_next_token_is (lexer, CPP_COMMA))
35650 {
35651 cp_lexer_consume_token (lexer);
35652 continue;
35653 }
35654 break;
35655 }
35656 while (1);
35657
35658 if (!parens.require_close (parser))
35659 goto cleanup_error;
35660 }
35661
35662 check_no_duplicate_clause (list, kind, str, loc);
35663
35664 c = build_omp_clause (loc, kind);
35665
35666 if (ops[1])
35667 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
35668
35669 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
35670 OMP_CLAUSE_CHAIN (c) = list;
35671
35672 return c;
35673
35674 cleanup_error:
35675 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
35676 return list;
35677 }
35678
35679 /* OpenACC 2.0:
35680 tile ( size-expr-list ) */
35681
35682 static tree
35683 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
35684 {
35685 tree c, expr = error_mark_node;
35686 tree tile = NULL_TREE;
35687
35688 /* Collapse and tile are mutually exclusive. (The spec doesn't say
35689 so, but the spec authors never considered such a case and have
35690 differing opinions on what it might mean, including 'not
35691 allowed'.) */
35692 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
35693 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
35694 clause_loc);
35695
35696 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35697 return list;
35698
35699 do
35700 {
35701 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
35702 return list;
35703
35704 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
35705 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
35706 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
35707 {
35708 cp_lexer_consume_token (parser->lexer);
35709 expr = integer_zero_node;
35710 }
35711 else
35712 expr = cp_parser_constant_expression (parser);
35713
35714 tile = tree_cons (NULL_TREE, expr, tile);
35715 }
35716 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
35717
35718 /* Consume the trailing ')'. */
35719 cp_lexer_consume_token (parser->lexer);
35720
35721 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
35722 tile = nreverse (tile);
35723 OMP_CLAUSE_TILE_LIST (c) = tile;
35724 OMP_CLAUSE_CHAIN (c) = list;
35725 return c;
35726 }
35727
35728 /* OpenACC 2.0
35729 Parse wait clause or directive parameters. */
35730
35731 static tree
35732 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
35733 {
35734 vec<tree, va_gc> *args;
35735 tree t, args_tree;
35736
35737 args = cp_parser_parenthesized_expression_list (parser, non_attr,
35738 /*cast_p=*/false,
35739 /*allow_expansion_p=*/true,
35740 /*non_constant_p=*/NULL);
35741
35742 if (args == NULL || args->length () == 0)
35743 {
35744 if (args != NULL)
35745 {
35746 cp_parser_error (parser, "expected integer expression list");
35747 release_tree_vector (args);
35748 }
35749 return list;
35750 }
35751
35752 args_tree = build_tree_list_vec (args);
35753
35754 release_tree_vector (args);
35755
35756 for (t = args_tree; t; t = TREE_CHAIN (t))
35757 {
35758 tree targ = TREE_VALUE (t);
35759
35760 if (targ != error_mark_node)
35761 {
35762 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
35763 error ("%<wait%> expression must be integral");
35764 else
35765 {
35766 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
35767
35768 targ = mark_rvalue_use (targ);
35769 OMP_CLAUSE_DECL (c) = targ;
35770 OMP_CLAUSE_CHAIN (c) = list;
35771 list = c;
35772 }
35773 }
35774 }
35775
35776 return list;
35777 }
35778
35779 /* OpenACC:
35780 wait [( int-expr-list )] */
35781
35782 static tree
35783 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
35784 {
35785 location_t location = cp_lexer_peek_token (parser->lexer)->location;
35786
35787 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35788 list = cp_parser_oacc_wait_list (parser, location, list);
35789 else
35790 {
35791 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
35792
35793 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
35794 OMP_CLAUSE_CHAIN (c) = list;
35795 list = c;
35796 }
35797
35798 return list;
35799 }
35800
35801 /* OpenMP 3.0:
35802 collapse ( constant-expression ) */
35803
35804 static tree
35805 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
35806 {
35807 tree c, num;
35808 location_t loc;
35809 HOST_WIDE_INT n;
35810
35811 loc = cp_lexer_peek_token (parser->lexer)->location;
35812 matching_parens parens;
35813 if (!parens.require_open (parser))
35814 return list;
35815
35816 num = cp_parser_constant_expression (parser);
35817
35818 if (!parens.require_close (parser))
35819 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35820 /*or_comma=*/false,
35821 /*consume_paren=*/true);
35822
35823 if (num == error_mark_node)
35824 return list;
35825 num = fold_non_dependent_expr (num);
35826 if (!tree_fits_shwi_p (num)
35827 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
35828 || (n = tree_to_shwi (num)) <= 0
35829 || (int) n != n)
35830 {
35831 error_at (loc, "collapse argument needs positive constant integer expression");
35832 return list;
35833 }
35834
35835 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
35836 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
35837 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
35838 OMP_CLAUSE_CHAIN (c) = list;
35839 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
35840
35841 return c;
35842 }
35843
35844 /* OpenMP 2.5:
35845 default ( none | shared )
35846
35847 OpenACC:
35848 default ( none | present ) */
35849
35850 static tree
35851 cp_parser_omp_clause_default (cp_parser *parser, tree list,
35852 location_t location, bool is_oacc)
35853 {
35854 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
35855 tree c;
35856
35857 matching_parens parens;
35858 if (!parens.require_open (parser))
35859 return list;
35860 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35861 {
35862 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35863 const char *p = IDENTIFIER_POINTER (id);
35864
35865 switch (p[0])
35866 {
35867 case 'n':
35868 if (strcmp ("none", p) != 0)
35869 goto invalid_kind;
35870 kind = OMP_CLAUSE_DEFAULT_NONE;
35871 break;
35872
35873 case 'p':
35874 if (strcmp ("present", p) != 0 || !is_oacc)
35875 goto invalid_kind;
35876 kind = OMP_CLAUSE_DEFAULT_PRESENT;
35877 break;
35878
35879 case 's':
35880 if (strcmp ("shared", p) != 0 || is_oacc)
35881 goto invalid_kind;
35882 kind = OMP_CLAUSE_DEFAULT_SHARED;
35883 break;
35884
35885 default:
35886 goto invalid_kind;
35887 }
35888
35889 cp_lexer_consume_token (parser->lexer);
35890 }
35891 else
35892 {
35893 invalid_kind:
35894 if (is_oacc)
35895 cp_parser_error (parser, "expected %<none%> or %<present%>");
35896 else
35897 cp_parser_error (parser, "expected %<none%> or %<shared%>");
35898 }
35899
35900 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
35901 || !parens.require_close (parser))
35902 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35903 /*or_comma=*/false,
35904 /*consume_paren=*/true);
35905
35906 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
35907 return list;
35908
35909 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
35910 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
35911 OMP_CLAUSE_CHAIN (c) = list;
35912 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
35913
35914 return c;
35915 }
35916
35917 /* OpenMP 3.1:
35918 final ( expression ) */
35919
35920 static tree
35921 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
35922 {
35923 tree t, c;
35924
35925 matching_parens parens;
35926 if (!parens.require_open (parser))
35927 return list;
35928
35929 t = cp_parser_assignment_expression (parser);
35930
35931 if (t == error_mark_node
35932 || !parens.require_close (parser))
35933 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35934 /*or_comma=*/false,
35935 /*consume_paren=*/true);
35936
35937 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
35938
35939 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
35940 OMP_CLAUSE_FINAL_EXPR (c) = t;
35941 OMP_CLAUSE_CHAIN (c) = list;
35942
35943 return c;
35944 }
35945
35946 /* OpenMP 2.5:
35947 if ( expression )
35948
35949 OpenMP 4.5:
35950 if ( directive-name-modifier : expression )
35951
35952 directive-name-modifier:
35953 parallel | task | taskloop | target data | target | target update
35954 | target enter data | target exit data
35955
35956 OpenMP 5.0:
35957 directive-name-modifier:
35958 ... | simd | cancel */
35959
35960 static tree
35961 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
35962 bool is_omp)
35963 {
35964 tree t, c;
35965 enum tree_code if_modifier = ERROR_MARK;
35966
35967 matching_parens parens;
35968 if (!parens.require_open (parser))
35969 return list;
35970
35971 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35972 {
35973 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35974 const char *p = IDENTIFIER_POINTER (id);
35975 int n = 2;
35976
35977 if (strcmp ("cancel", p) == 0)
35978 if_modifier = VOID_CST;
35979 else if (strcmp ("parallel", p) == 0)
35980 if_modifier = OMP_PARALLEL;
35981 else if (strcmp ("simd", p) == 0)
35982 if_modifier = OMP_SIMD;
35983 else if (strcmp ("task", p) == 0)
35984 if_modifier = OMP_TASK;
35985 else if (strcmp ("taskloop", p) == 0)
35986 if_modifier = OMP_TASKLOOP;
35987 else if (strcmp ("target", p) == 0)
35988 {
35989 if_modifier = OMP_TARGET;
35990 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
35991 {
35992 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
35993 p = IDENTIFIER_POINTER (id);
35994 if (strcmp ("data", p) == 0)
35995 if_modifier = OMP_TARGET_DATA;
35996 else if (strcmp ("update", p) == 0)
35997 if_modifier = OMP_TARGET_UPDATE;
35998 else if (strcmp ("enter", p) == 0)
35999 if_modifier = OMP_TARGET_ENTER_DATA;
36000 else if (strcmp ("exit", p) == 0)
36001 if_modifier = OMP_TARGET_EXIT_DATA;
36002 if (if_modifier != OMP_TARGET)
36003 n = 3;
36004 else
36005 {
36006 location_t loc
36007 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
36008 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
36009 "or %<exit%>");
36010 if_modifier = ERROR_MARK;
36011 }
36012 if (if_modifier == OMP_TARGET_ENTER_DATA
36013 || if_modifier == OMP_TARGET_EXIT_DATA)
36014 {
36015 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
36016 {
36017 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
36018 p = IDENTIFIER_POINTER (id);
36019 if (strcmp ("data", p) == 0)
36020 n = 4;
36021 }
36022 if (n != 4)
36023 {
36024 location_t loc
36025 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
36026 error_at (loc, "expected %<data%>");
36027 if_modifier = ERROR_MARK;
36028 }
36029 }
36030 }
36031 }
36032 if (if_modifier != ERROR_MARK)
36033 {
36034 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
36035 {
36036 while (n-- > 0)
36037 cp_lexer_consume_token (parser->lexer);
36038 }
36039 else
36040 {
36041 if (n > 2)
36042 {
36043 location_t loc
36044 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
36045 error_at (loc, "expected %<:%>");
36046 }
36047 if_modifier = ERROR_MARK;
36048 }
36049 }
36050 }
36051
36052 t = cp_parser_assignment_expression (parser);
36053
36054 if (t == error_mark_node
36055 || !parens.require_close (parser))
36056 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36057 /*or_comma=*/false,
36058 /*consume_paren=*/true);
36059
36060 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
36061 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
36062 {
36063 if (if_modifier != ERROR_MARK
36064 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
36065 {
36066 const char *p = NULL;
36067 switch (if_modifier)
36068 {
36069 case VOID_CST: p = "cancel"; break;
36070 case OMP_PARALLEL: p = "parallel"; break;
36071 case OMP_SIMD: p = "simd"; break;
36072 case OMP_TASK: p = "task"; break;
36073 case OMP_TASKLOOP: p = "taskloop"; break;
36074 case OMP_TARGET_DATA: p = "target data"; break;
36075 case OMP_TARGET: p = "target"; break;
36076 case OMP_TARGET_UPDATE: p = "target update"; break;
36077 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
36078 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
36079 default: gcc_unreachable ();
36080 }
36081 error_at (location, "too many %<if%> clauses with %qs modifier",
36082 p);
36083 return list;
36084 }
36085 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
36086 {
36087 if (!is_omp)
36088 error_at (location, "too many %<if%> clauses");
36089 else
36090 error_at (location, "too many %<if%> clauses without modifier");
36091 return list;
36092 }
36093 else if (if_modifier == ERROR_MARK
36094 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
36095 {
36096 error_at (location, "if any %<if%> clause has modifier, then all "
36097 "%<if%> clauses have to use modifier");
36098 return list;
36099 }
36100 }
36101
36102 c = build_omp_clause (location, OMP_CLAUSE_IF);
36103 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
36104 OMP_CLAUSE_IF_EXPR (c) = t;
36105 OMP_CLAUSE_CHAIN (c) = list;
36106
36107 return c;
36108 }
36109
36110 /* OpenMP 3.1:
36111 mergeable */
36112
36113 static tree
36114 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
36115 tree list, location_t location)
36116 {
36117 tree c;
36118
36119 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
36120 location);
36121
36122 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
36123 OMP_CLAUSE_CHAIN (c) = list;
36124 return c;
36125 }
36126
36127 /* OpenMP 2.5:
36128 nowait */
36129
36130 static tree
36131 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
36132 tree list, location_t location)
36133 {
36134 tree c;
36135
36136 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
36137
36138 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
36139 OMP_CLAUSE_CHAIN (c) = list;
36140 return c;
36141 }
36142
36143 /* OpenMP 2.5:
36144 num_threads ( expression ) */
36145
36146 static tree
36147 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
36148 location_t location)
36149 {
36150 tree t, c;
36151
36152 matching_parens parens;
36153 if (!parens.require_open (parser))
36154 return list;
36155
36156 t = cp_parser_assignment_expression (parser);
36157
36158 if (t == error_mark_node
36159 || !parens.require_close (parser))
36160 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36161 /*or_comma=*/false,
36162 /*consume_paren=*/true);
36163
36164 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
36165 "num_threads", location);
36166
36167 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
36168 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
36169 OMP_CLAUSE_CHAIN (c) = list;
36170
36171 return c;
36172 }
36173
36174 /* OpenMP 4.5:
36175 num_tasks ( expression ) */
36176
36177 static tree
36178 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
36179 location_t location)
36180 {
36181 tree t, c;
36182
36183 matching_parens parens;
36184 if (!parens.require_open (parser))
36185 return list;
36186
36187 t = cp_parser_assignment_expression (parser);
36188
36189 if (t == error_mark_node
36190 || !parens.require_close (parser))
36191 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36192 /*or_comma=*/false,
36193 /*consume_paren=*/true);
36194
36195 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
36196 "num_tasks", location);
36197
36198 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
36199 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
36200 OMP_CLAUSE_CHAIN (c) = list;
36201
36202 return c;
36203 }
36204
36205 /* OpenMP 4.5:
36206 grainsize ( expression ) */
36207
36208 static tree
36209 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
36210 location_t location)
36211 {
36212 tree t, c;
36213
36214 matching_parens parens;
36215 if (!parens.require_open (parser))
36216 return list;
36217
36218 t = cp_parser_assignment_expression (parser);
36219
36220 if (t == error_mark_node
36221 || !parens.require_close (parser))
36222 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36223 /*or_comma=*/false,
36224 /*consume_paren=*/true);
36225
36226 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
36227 "grainsize", location);
36228
36229 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
36230 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
36231 OMP_CLAUSE_CHAIN (c) = list;
36232
36233 return c;
36234 }
36235
36236 /* OpenMP 4.5:
36237 priority ( expression ) */
36238
36239 static tree
36240 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
36241 location_t location)
36242 {
36243 tree t, c;
36244
36245 matching_parens parens;
36246 if (!parens.require_open (parser))
36247 return list;
36248
36249 t = cp_parser_assignment_expression (parser);
36250
36251 if (t == error_mark_node
36252 || !parens.require_close (parser))
36253 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36254 /*or_comma=*/false,
36255 /*consume_paren=*/true);
36256
36257 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
36258 "priority", location);
36259
36260 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
36261 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
36262 OMP_CLAUSE_CHAIN (c) = list;
36263
36264 return c;
36265 }
36266
36267 /* OpenMP 4.5:
36268 hint ( expression ) */
36269
36270 static tree
36271 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
36272 {
36273 tree t, c;
36274
36275 matching_parens parens;
36276 if (!parens.require_open (parser))
36277 return list;
36278
36279 t = cp_parser_assignment_expression (parser);
36280
36281 if (t != error_mark_node)
36282 {
36283 t = fold_non_dependent_expr (t);
36284 if (!value_dependent_expression_p (t)
36285 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
36286 || !tree_fits_shwi_p (t)
36287 || tree_int_cst_sgn (t) == -1))
36288 error_at (location, "expected constant integer expression with "
36289 "valid sync-hint value");
36290 }
36291 if (t == error_mark_node
36292 || !parens.require_close (parser))
36293 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36294 /*or_comma=*/false,
36295 /*consume_paren=*/true);
36296 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
36297
36298 c = build_omp_clause (location, OMP_CLAUSE_HINT);
36299 OMP_CLAUSE_HINT_EXPR (c) = t;
36300 OMP_CLAUSE_CHAIN (c) = list;
36301
36302 return c;
36303 }
36304
36305 /* OpenMP 4.5:
36306 defaultmap ( tofrom : scalar )
36307
36308 OpenMP 5.0:
36309 defaultmap ( implicit-behavior [ : variable-category ] ) */
36310
36311 static tree
36312 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
36313 location_t location)
36314 {
36315 tree c, id;
36316 const char *p;
36317 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
36318 enum omp_clause_defaultmap_kind category
36319 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
36320
36321 matching_parens parens;
36322 if (!parens.require_open (parser))
36323 return list;
36324
36325 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
36326 p = "default";
36327 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36328 {
36329 invalid_behavior:
36330 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
36331 "%<tofrom%>, %<firstprivate%>, %<none%> "
36332 "or %<default%>");
36333 goto out_err;
36334 }
36335 else
36336 {
36337 id = cp_lexer_peek_token (parser->lexer)->u.value;
36338 p = IDENTIFIER_POINTER (id);
36339 }
36340
36341 switch (p[0])
36342 {
36343 case 'a':
36344 if (strcmp ("alloc", p) == 0)
36345 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
36346 else
36347 goto invalid_behavior;
36348 break;
36349
36350 case 'd':
36351 if (strcmp ("default", p) == 0)
36352 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
36353 else
36354 goto invalid_behavior;
36355 break;
36356
36357 case 'f':
36358 if (strcmp ("firstprivate", p) == 0)
36359 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
36360 else if (strcmp ("from", p) == 0)
36361 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
36362 else
36363 goto invalid_behavior;
36364 break;
36365
36366 case 'n':
36367 if (strcmp ("none", p) == 0)
36368 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
36369 else
36370 goto invalid_behavior;
36371 break;
36372
36373 case 't':
36374 if (strcmp ("tofrom", p) == 0)
36375 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
36376 else if (strcmp ("to", p) == 0)
36377 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
36378 else
36379 goto invalid_behavior;
36380 break;
36381
36382 default:
36383 goto invalid_behavior;
36384 }
36385 cp_lexer_consume_token (parser->lexer);
36386
36387 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36388 {
36389 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36390 goto out_err;
36391
36392 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36393 {
36394 invalid_category:
36395 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
36396 "%<pointer%>");
36397 goto out_err;
36398 }
36399 id = cp_lexer_peek_token (parser->lexer)->u.value;
36400 p = IDENTIFIER_POINTER (id);
36401
36402 switch (p[0])
36403 {
36404 case 'a':
36405 if (strcmp ("aggregate", p) == 0)
36406 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
36407 else
36408 goto invalid_category;
36409 break;
36410
36411 case 'p':
36412 if (strcmp ("pointer", p) == 0)
36413 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
36414 else
36415 goto invalid_category;
36416 break;
36417
36418 case 's':
36419 if (strcmp ("scalar", p) == 0)
36420 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
36421 else
36422 goto invalid_category;
36423 break;
36424
36425 default:
36426 goto invalid_category;
36427 }
36428
36429 cp_lexer_consume_token (parser->lexer);
36430 }
36431 if (!parens.require_close (parser))
36432 goto out_err;
36433
36434 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
36435 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
36436 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
36437 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
36438 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
36439 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
36440 {
36441 enum omp_clause_defaultmap_kind cat = category;
36442 location_t loc = OMP_CLAUSE_LOCATION (c);
36443 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
36444 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
36445 p = NULL;
36446 switch (cat)
36447 {
36448 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
36449 p = NULL;
36450 break;
36451 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
36452 p = "aggregate";
36453 break;
36454 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
36455 p = "pointer";
36456 break;
36457 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
36458 p = "scalar";
36459 break;
36460 default:
36461 gcc_unreachable ();
36462 }
36463 if (p)
36464 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
36465 p);
36466 else
36467 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
36468 "category");
36469 break;
36470 }
36471
36472 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
36473 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
36474 OMP_CLAUSE_CHAIN (c) = list;
36475 return c;
36476
36477 out_err:
36478 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36479 /*or_comma=*/false,
36480 /*consume_paren=*/true);
36481 return list;
36482 }
36483
36484 /* OpenMP 5.0:
36485 order ( concurrent ) */
36486
36487 static tree
36488 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
36489 {
36490 tree c, id;
36491 const char *p;
36492
36493 matching_parens parens;
36494 if (!parens.require_open (parser))
36495 return list;
36496
36497 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36498 {
36499 cp_parser_error (parser, "expected %<concurrent%>");
36500 goto out_err;
36501 }
36502 else
36503 {
36504 id = cp_lexer_peek_token (parser->lexer)->u.value;
36505 p = IDENTIFIER_POINTER (id);
36506 }
36507 if (strcmp (p, "concurrent") != 0)
36508 {
36509 cp_parser_error (parser, "expected %<concurrent%>");
36510 goto out_err;
36511 }
36512 cp_lexer_consume_token (parser->lexer);
36513 if (!parens.require_close (parser))
36514 goto out_err;
36515
36516 /* check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location); */
36517 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
36518 OMP_CLAUSE_CHAIN (c) = list;
36519 return c;
36520
36521 out_err:
36522 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36523 /*or_comma=*/false,
36524 /*consume_paren=*/true);
36525 return list;
36526 }
36527
36528 /* OpenMP 5.0:
36529 bind ( teams | parallel | thread ) */
36530
36531 static tree
36532 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
36533 location_t location)
36534 {
36535 tree c;
36536 const char *p;
36537 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
36538
36539 matching_parens parens;
36540 if (!parens.require_open (parser))
36541 return list;
36542
36543 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36544 {
36545 invalid:
36546 cp_parser_error (parser,
36547 "expected %<teams%>, %<parallel%> or %<thread%>");
36548 goto out_err;
36549 }
36550 else
36551 {
36552 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36553 p = IDENTIFIER_POINTER (id);
36554 }
36555 if (strcmp (p, "teams") == 0)
36556 kind = OMP_CLAUSE_BIND_TEAMS;
36557 else if (strcmp (p, "parallel") == 0)
36558 kind = OMP_CLAUSE_BIND_PARALLEL;
36559 else if (strcmp (p, "thread") != 0)
36560 goto invalid;
36561 cp_lexer_consume_token (parser->lexer);
36562 if (!parens.require_close (parser))
36563 goto out_err;
36564
36565 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
36566 c = build_omp_clause (location, OMP_CLAUSE_BIND);
36567 OMP_CLAUSE_BIND_KIND (c) = kind;
36568 OMP_CLAUSE_CHAIN (c) = list;
36569 return c;
36570
36571 out_err:
36572 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36573 /*or_comma=*/false,
36574 /*consume_paren=*/true);
36575 return list;
36576 }
36577
36578 /* OpenMP 2.5:
36579 ordered
36580
36581 OpenMP 4.5:
36582 ordered ( constant-expression ) */
36583
36584 static tree
36585 cp_parser_omp_clause_ordered (cp_parser *parser,
36586 tree list, location_t location)
36587 {
36588 tree c, num = NULL_TREE;
36589 HOST_WIDE_INT n;
36590
36591 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
36592 "ordered", location);
36593
36594 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36595 {
36596 matching_parens parens;
36597 parens.consume_open (parser);
36598
36599 num = cp_parser_constant_expression (parser);
36600
36601 if (!parens.require_close (parser))
36602 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36603 /*or_comma=*/false,
36604 /*consume_paren=*/true);
36605
36606 if (num == error_mark_node)
36607 return list;
36608 num = fold_non_dependent_expr (num);
36609 if (!tree_fits_shwi_p (num)
36610 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
36611 || (n = tree_to_shwi (num)) <= 0
36612 || (int) n != n)
36613 {
36614 error_at (location,
36615 "ordered argument needs positive constant integer "
36616 "expression");
36617 return list;
36618 }
36619 }
36620
36621 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
36622 OMP_CLAUSE_ORDERED_EXPR (c) = num;
36623 OMP_CLAUSE_CHAIN (c) = list;
36624 return c;
36625 }
36626
36627 /* OpenMP 2.5:
36628 reduction ( reduction-operator : variable-list )
36629
36630 reduction-operator:
36631 One of: + * - & ^ | && ||
36632
36633 OpenMP 3.1:
36634
36635 reduction-operator:
36636 One of: + * - & ^ | && || min max
36637
36638 OpenMP 4.0:
36639
36640 reduction-operator:
36641 One of: + * - & ^ | && ||
36642 id-expression
36643
36644 OpenMP 5.0:
36645 reduction ( reduction-modifier, reduction-operator : variable-list )
36646 in_reduction ( reduction-operator : variable-list )
36647 task_reduction ( reduction-operator : variable-list ) */
36648
36649 static tree
36650 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
36651 bool is_omp, tree list)
36652 {
36653 enum tree_code code = ERROR_MARK;
36654 tree nlist, c, id = NULL_TREE;
36655 bool task = false;
36656 bool inscan = false;
36657
36658 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36659 return list;
36660
36661 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
36662 {
36663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
36664 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
36665 {
36666 cp_lexer_consume_token (parser->lexer);
36667 cp_lexer_consume_token (parser->lexer);
36668 }
36669 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
36670 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
36671 {
36672 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36673 const char *p = IDENTIFIER_POINTER (id);
36674 if (strcmp (p, "task") == 0)
36675 task = true;
36676 else if (strcmp (p, "inscan") == 0)
36677 inscan = true;
36678 if (task || inscan)
36679 {
36680 cp_lexer_consume_token (parser->lexer);
36681 cp_lexer_consume_token (parser->lexer);
36682 }
36683 }
36684 }
36685
36686 switch (cp_lexer_peek_token (parser->lexer)->type)
36687 {
36688 case CPP_PLUS: code = PLUS_EXPR; break;
36689 case CPP_MULT: code = MULT_EXPR; break;
36690 case CPP_MINUS: code = MINUS_EXPR; break;
36691 case CPP_AND: code = BIT_AND_EXPR; break;
36692 case CPP_XOR: code = BIT_XOR_EXPR; break;
36693 case CPP_OR: code = BIT_IOR_EXPR; break;
36694 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
36695 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
36696 default: break;
36697 }
36698
36699 if (code != ERROR_MARK)
36700 cp_lexer_consume_token (parser->lexer);
36701 else
36702 {
36703 bool saved_colon_corrects_to_scope_p;
36704 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36705 parser->colon_corrects_to_scope_p = false;
36706 id = cp_parser_id_expression (parser, /*template_p=*/false,
36707 /*check_dependency_p=*/true,
36708 /*template_p=*/NULL,
36709 /*declarator_p=*/false,
36710 /*optional_p=*/false);
36711 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36712 if (identifier_p (id))
36713 {
36714 const char *p = IDENTIFIER_POINTER (id);
36715
36716 if (strcmp (p, "min") == 0)
36717 code = MIN_EXPR;
36718 else if (strcmp (p, "max") == 0)
36719 code = MAX_EXPR;
36720 else if (id == ovl_op_identifier (false, PLUS_EXPR))
36721 code = PLUS_EXPR;
36722 else if (id == ovl_op_identifier (false, MULT_EXPR))
36723 code = MULT_EXPR;
36724 else if (id == ovl_op_identifier (false, MINUS_EXPR))
36725 code = MINUS_EXPR;
36726 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
36727 code = BIT_AND_EXPR;
36728 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
36729 code = BIT_IOR_EXPR;
36730 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
36731 code = BIT_XOR_EXPR;
36732 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
36733 code = TRUTH_ANDIF_EXPR;
36734 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
36735 code = TRUTH_ORIF_EXPR;
36736 id = omp_reduction_id (code, id, NULL_TREE);
36737 tree scope = parser->scope;
36738 if (scope)
36739 id = build_qualified_name (NULL_TREE, scope, id, false);
36740 parser->scope = NULL_TREE;
36741 parser->qualifying_scope = NULL_TREE;
36742 parser->object_scope = NULL_TREE;
36743 }
36744 else
36745 {
36746 error ("invalid reduction-identifier");
36747 resync_fail:
36748 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36749 /*or_comma=*/false,
36750 /*consume_paren=*/true);
36751 return list;
36752 }
36753 }
36754
36755 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36756 goto resync_fail;
36757
36758 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
36759 NULL);
36760 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36761 {
36762 OMP_CLAUSE_REDUCTION_CODE (c) = code;
36763 if (task)
36764 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
36765 else if (inscan)
36766 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
36767 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
36768 }
36769
36770 return nlist;
36771 }
36772
36773 /* OpenMP 2.5:
36774 schedule ( schedule-kind )
36775 schedule ( schedule-kind , expression )
36776
36777 schedule-kind:
36778 static | dynamic | guided | runtime | auto
36779
36780 OpenMP 4.5:
36781 schedule ( schedule-modifier : schedule-kind )
36782 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
36783
36784 schedule-modifier:
36785 simd
36786 monotonic
36787 nonmonotonic */
36788
36789 static tree
36790 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
36791 {
36792 tree c, t;
36793 int modifiers = 0, nmodifiers = 0;
36794
36795 matching_parens parens;
36796 if (!parens.require_open (parser))
36797 return list;
36798
36799 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
36800
36801 location_t comma = UNKNOWN_LOCATION;
36802 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36803 {
36804 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36805 const char *p = IDENTIFIER_POINTER (id);
36806 if (strcmp ("simd", p) == 0)
36807 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
36808 else if (strcmp ("monotonic", p) == 0)
36809 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
36810 else if (strcmp ("nonmonotonic", p) == 0)
36811 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
36812 else
36813 break;
36814 comma = UNKNOWN_LOCATION;
36815 cp_lexer_consume_token (parser->lexer);
36816 if (nmodifiers++ == 0
36817 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36818 {
36819 comma = cp_lexer_peek_token (parser->lexer)->location;
36820 cp_lexer_consume_token (parser->lexer);
36821 }
36822 else
36823 {
36824 cp_parser_require (parser, CPP_COLON, RT_COLON);
36825 break;
36826 }
36827 }
36828 if (comma != UNKNOWN_LOCATION)
36829 error_at (comma, "expected %<:%>");
36830
36831 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36832 {
36833 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36834 const char *p = IDENTIFIER_POINTER (id);
36835
36836 switch (p[0])
36837 {
36838 case 'd':
36839 if (strcmp ("dynamic", p) != 0)
36840 goto invalid_kind;
36841 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
36842 break;
36843
36844 case 'g':
36845 if (strcmp ("guided", p) != 0)
36846 goto invalid_kind;
36847 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
36848 break;
36849
36850 case 'r':
36851 if (strcmp ("runtime", p) != 0)
36852 goto invalid_kind;
36853 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
36854 break;
36855
36856 default:
36857 goto invalid_kind;
36858 }
36859 }
36860 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
36861 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
36862 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
36863 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
36864 else
36865 goto invalid_kind;
36866 cp_lexer_consume_token (parser->lexer);
36867
36868 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
36869 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
36870 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
36871 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
36872 {
36873 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
36874 "specified");
36875 modifiers = 0;
36876 }
36877
36878 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36879 {
36880 cp_token *token;
36881 cp_lexer_consume_token (parser->lexer);
36882
36883 token = cp_lexer_peek_token (parser->lexer);
36884 t = cp_parser_assignment_expression (parser);
36885
36886 if (t == error_mark_node)
36887 goto resync_fail;
36888 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
36889 error_at (token->location, "schedule %<runtime%> does not take "
36890 "a %<chunk_size%> parameter");
36891 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
36892 error_at (token->location, "schedule %<auto%> does not take "
36893 "a %<chunk_size%> parameter");
36894 else
36895 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
36896
36897 if (!parens.require_close (parser))
36898 goto resync_fail;
36899 }
36900 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
36901 goto resync_fail;
36902
36903 OMP_CLAUSE_SCHEDULE_KIND (c)
36904 = (enum omp_clause_schedule_kind)
36905 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
36906
36907 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
36908 OMP_CLAUSE_CHAIN (c) = list;
36909 return c;
36910
36911 invalid_kind:
36912 cp_parser_error (parser, "invalid schedule kind");
36913 resync_fail:
36914 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36915 /*or_comma=*/false,
36916 /*consume_paren=*/true);
36917 return list;
36918 }
36919
36920 /* OpenMP 3.0:
36921 untied */
36922
36923 static tree
36924 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
36925 tree list, location_t location)
36926 {
36927 tree c;
36928
36929 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
36930
36931 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
36932 OMP_CLAUSE_CHAIN (c) = list;
36933 return c;
36934 }
36935
36936 /* OpenMP 4.0:
36937 inbranch
36938 notinbranch */
36939
36940 static tree
36941 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
36942 tree list, location_t location)
36943 {
36944 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
36945 tree c = build_omp_clause (location, code);
36946 OMP_CLAUSE_CHAIN (c) = list;
36947 return c;
36948 }
36949
36950 /* OpenMP 4.0:
36951 parallel
36952 for
36953 sections
36954 taskgroup */
36955
36956 static tree
36957 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
36958 enum omp_clause_code code,
36959 tree list, location_t location)
36960 {
36961 tree c = build_omp_clause (location, code);
36962 OMP_CLAUSE_CHAIN (c) = list;
36963 return c;
36964 }
36965
36966 /* OpenMP 4.5:
36967 nogroup */
36968
36969 static tree
36970 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
36971 tree list, location_t location)
36972 {
36973 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
36974 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
36975 OMP_CLAUSE_CHAIN (c) = list;
36976 return c;
36977 }
36978
36979 /* OpenMP 4.5:
36980 simd
36981 threads */
36982
36983 static tree
36984 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
36985 enum omp_clause_code code,
36986 tree list, location_t location)
36987 {
36988 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
36989 tree c = build_omp_clause (location, code);
36990 OMP_CLAUSE_CHAIN (c) = list;
36991 return c;
36992 }
36993
36994 /* OpenMP 4.0:
36995 num_teams ( expression ) */
36996
36997 static tree
36998 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
36999 location_t location)
37000 {
37001 tree t, c;
37002
37003 matching_parens parens;
37004 if (!parens.require_open (parser))
37005 return list;
37006
37007 t = cp_parser_assignment_expression (parser);
37008
37009 if (t == error_mark_node
37010 || !parens.require_close (parser))
37011 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37012 /*or_comma=*/false,
37013 /*consume_paren=*/true);
37014
37015 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
37016 "num_teams", location);
37017
37018 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
37019 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
37020 OMP_CLAUSE_CHAIN (c) = list;
37021
37022 return c;
37023 }
37024
37025 /* OpenMP 4.0:
37026 thread_limit ( expression ) */
37027
37028 static tree
37029 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
37030 location_t location)
37031 {
37032 tree t, c;
37033
37034 matching_parens parens;
37035 if (!parens.require_open (parser))
37036 return list;
37037
37038 t = cp_parser_assignment_expression (parser);
37039
37040 if (t == error_mark_node
37041 || !parens.require_close (parser))
37042 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37043 /*or_comma=*/false,
37044 /*consume_paren=*/true);
37045
37046 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
37047 "thread_limit", location);
37048
37049 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
37050 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
37051 OMP_CLAUSE_CHAIN (c) = list;
37052
37053 return c;
37054 }
37055
37056 /* OpenMP 4.0:
37057 aligned ( variable-list )
37058 aligned ( variable-list : constant-expression ) */
37059
37060 static tree
37061 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
37062 {
37063 tree nlist, c, alignment = NULL_TREE;
37064 bool colon;
37065
37066 matching_parens parens;
37067 if (!parens.require_open (parser))
37068 return list;
37069
37070 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
37071 &colon);
37072
37073 if (colon)
37074 {
37075 alignment = cp_parser_constant_expression (parser);
37076
37077 if (!parens.require_close (parser))
37078 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37079 /*or_comma=*/false,
37080 /*consume_paren=*/true);
37081
37082 if (alignment == error_mark_node)
37083 alignment = NULL_TREE;
37084 }
37085
37086 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
37087 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
37088
37089 return nlist;
37090 }
37091
37092 /* OpenMP 5.0:
37093 allocate ( variable-list )
37094 allocate ( expression : variable-list ) */
37095
37096 static tree
37097 cp_parser_omp_clause_allocate (cp_parser *parser, tree list)
37098 {
37099 tree nlist, c, allocator = NULL_TREE;
37100 bool colon;
37101
37102 matching_parens parens;
37103 if (!parens.require_open (parser))
37104 return list;
37105
37106 cp_parser_parse_tentatively (parser);
37107 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37108 parser->colon_corrects_to_scope_p = false;
37109 allocator = cp_parser_assignment_expression (parser);
37110 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37111 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
37112 {
37113 cp_parser_parse_definitely (parser);
37114 cp_lexer_consume_token (parser->lexer);
37115 if (allocator == error_mark_node)
37116 allocator = NULL_TREE;
37117 }
37118 else
37119 {
37120 cp_parser_abort_tentative_parse (parser);
37121 allocator = NULL_TREE;
37122 }
37123
37124 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALLOCATE, list,
37125 &colon);
37126
37127 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
37128 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
37129
37130 return nlist;
37131 }
37132
37133 /* OpenMP 2.5:
37134 lastprivate ( variable-list )
37135
37136 OpenMP 5.0:
37137 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
37138
37139 static tree
37140 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
37141 {
37142 bool conditional = false;
37143
37144 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37145 return list;
37146
37147 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37148 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
37149 {
37150 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37151 const char *p = IDENTIFIER_POINTER (id);
37152
37153 if (strcmp ("conditional", p) == 0)
37154 {
37155 conditional = true;
37156 cp_lexer_consume_token (parser->lexer);
37157 cp_lexer_consume_token (parser->lexer);
37158 }
37159 }
37160
37161 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
37162 list, NULL);
37163
37164 if (conditional)
37165 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
37166 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
37167 return nlist;
37168 }
37169
37170 /* OpenMP 4.0:
37171 linear ( variable-list )
37172 linear ( variable-list : expression )
37173
37174 OpenMP 4.5:
37175 linear ( modifier ( variable-list ) )
37176 linear ( modifier ( variable-list ) : expression ) */
37177
37178 static tree
37179 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
37180 bool declare_simd)
37181 {
37182 tree nlist, c, step = integer_one_node;
37183 bool colon;
37184 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
37185
37186 matching_parens parens;
37187 if (!parens.require_open (parser))
37188 return list;
37189
37190 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37191 {
37192 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37193 const char *p = IDENTIFIER_POINTER (id);
37194
37195 if (strcmp ("ref", p) == 0)
37196 kind = OMP_CLAUSE_LINEAR_REF;
37197 else if (strcmp ("val", p) == 0)
37198 kind = OMP_CLAUSE_LINEAR_VAL;
37199 else if (strcmp ("uval", p) == 0)
37200 kind = OMP_CLAUSE_LINEAR_UVAL;
37201 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
37202 cp_lexer_consume_token (parser->lexer);
37203 else
37204 kind = OMP_CLAUSE_LINEAR_DEFAULT;
37205 }
37206
37207 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
37208 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
37209 &colon);
37210 else
37211 {
37212 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
37213 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
37214 if (colon)
37215 cp_parser_require (parser, CPP_COLON, RT_COLON);
37216 else if (!parens.require_close (parser))
37217 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37218 /*or_comma=*/false,
37219 /*consume_paren=*/true);
37220 }
37221
37222 if (colon)
37223 {
37224 step = NULL_TREE;
37225 if (declare_simd
37226 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37227 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
37228 {
37229 cp_token *token = cp_lexer_peek_token (parser->lexer);
37230 cp_parser_parse_tentatively (parser);
37231 step = cp_parser_id_expression (parser, /*template_p=*/false,
37232 /*check_dependency_p=*/true,
37233 /*template_p=*/NULL,
37234 /*declarator_p=*/false,
37235 /*optional_p=*/false);
37236 if (step != error_mark_node)
37237 step = cp_parser_lookup_name_simple (parser, step, token->location);
37238 if (step == error_mark_node)
37239 {
37240 step = NULL_TREE;
37241 cp_parser_abort_tentative_parse (parser);
37242 }
37243 else if (!cp_parser_parse_definitely (parser))
37244 step = NULL_TREE;
37245 }
37246 if (!step)
37247 step = cp_parser_assignment_expression (parser);
37248
37249 if (!parens.require_close (parser))
37250 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37251 /*or_comma=*/false,
37252 /*consume_paren=*/true);
37253
37254 if (step == error_mark_node)
37255 return list;
37256 }
37257
37258 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
37259 {
37260 OMP_CLAUSE_LINEAR_STEP (c) = step;
37261 OMP_CLAUSE_LINEAR_KIND (c) = kind;
37262 }
37263
37264 return nlist;
37265 }
37266
37267 /* OpenMP 4.0:
37268 safelen ( constant-expression ) */
37269
37270 static tree
37271 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
37272 location_t location)
37273 {
37274 tree t, c;
37275
37276 matching_parens parens;
37277 if (!parens.require_open (parser))
37278 return list;
37279
37280 t = cp_parser_constant_expression (parser);
37281
37282 if (t == error_mark_node
37283 || !parens.require_close (parser))
37284 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37285 /*or_comma=*/false,
37286 /*consume_paren=*/true);
37287
37288 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
37289
37290 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
37291 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
37292 OMP_CLAUSE_CHAIN (c) = list;
37293
37294 return c;
37295 }
37296
37297 /* OpenMP 4.0:
37298 simdlen ( constant-expression ) */
37299
37300 static tree
37301 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
37302 location_t location)
37303 {
37304 tree t, c;
37305
37306 matching_parens parens;
37307 if (!parens.require_open (parser))
37308 return list;
37309
37310 t = cp_parser_constant_expression (parser);
37311
37312 if (t == error_mark_node
37313 || !parens.require_close (parser))
37314 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37315 /*or_comma=*/false,
37316 /*consume_paren=*/true);
37317
37318 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
37319
37320 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
37321 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
37322 OMP_CLAUSE_CHAIN (c) = list;
37323
37324 return c;
37325 }
37326
37327 /* OpenMP 4.5:
37328 vec:
37329 identifier [+/- integer]
37330 vec , identifier [+/- integer]
37331 */
37332
37333 static tree
37334 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
37335 tree list)
37336 {
37337 tree vec = NULL;
37338
37339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37340 {
37341 cp_parser_error (parser, "expected identifier");
37342 return list;
37343 }
37344
37345 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37346 {
37347 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
37348 tree t, identifier = cp_parser_identifier (parser);
37349 tree addend = NULL;
37350
37351 if (identifier == error_mark_node)
37352 t = error_mark_node;
37353 else
37354 {
37355 t = cp_parser_lookup_name_simple
37356 (parser, identifier,
37357 cp_lexer_peek_token (parser->lexer)->location);
37358 if (t == error_mark_node)
37359 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
37360 id_loc);
37361 }
37362
37363 bool neg = false;
37364 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
37365 neg = true;
37366 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
37367 {
37368 addend = integer_zero_node;
37369 goto add_to_vector;
37370 }
37371 cp_lexer_consume_token (parser->lexer);
37372
37373 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
37374 {
37375 cp_parser_error (parser, "expected integer");
37376 return list;
37377 }
37378
37379 addend = cp_lexer_peek_token (parser->lexer)->u.value;
37380 if (TREE_CODE (addend) != INTEGER_CST)
37381 {
37382 cp_parser_error (parser, "expected integer");
37383 return list;
37384 }
37385 cp_lexer_consume_token (parser->lexer);
37386
37387 add_to_vector:
37388 if (t != error_mark_node)
37389 {
37390 vec = tree_cons (addend, t, vec);
37391 if (neg)
37392 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
37393 }
37394
37395 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
37396 break;
37397
37398 cp_lexer_consume_token (parser->lexer);
37399 }
37400
37401 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
37402 {
37403 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
37404 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
37405 OMP_CLAUSE_DECL (u) = nreverse (vec);
37406 OMP_CLAUSE_CHAIN (u) = list;
37407 return u;
37408 }
37409 return list;
37410 }
37411
37412 /* OpenMP 5.0:
37413 detach ( event-handle ) */
37414
37415 static tree
37416 cp_parser_omp_clause_detach (cp_parser *parser, tree list)
37417 {
37418 matching_parens parens;
37419
37420 if (!parens.require_open (parser))
37421 return list;
37422
37423 cp_token *token;
37424 tree name, decl;
37425
37426 token = cp_lexer_peek_token (parser->lexer);
37427 name = cp_parser_id_expression (parser, /*template_p=*/false,
37428 /*check_dependency_p=*/true,
37429 /*template_p=*/NULL,
37430 /*declarator_p=*/false,
37431 /*optional_p=*/false);
37432 if (name == error_mark_node)
37433 decl = error_mark_node;
37434 else
37435 {
37436 if (identifier_p (name))
37437 decl = cp_parser_lookup_name_simple (parser, name, token->location);
37438 else
37439 decl = name;
37440 if (decl == error_mark_node)
37441 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
37442 token->location);
37443 }
37444
37445 if (decl == error_mark_node
37446 || !parens.require_close (parser))
37447 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37448 /*or_comma=*/false,
37449 /*consume_paren=*/true);
37450
37451 tree u = build_omp_clause (token->location, OMP_CLAUSE_DETACH);
37452 OMP_CLAUSE_DECL (u) = decl;
37453 OMP_CLAUSE_CHAIN (u) = list;
37454
37455 return u;
37456 }
37457
37458 /* OpenMP 5.0:
37459 iterators ( iterators-definition )
37460
37461 iterators-definition:
37462 iterator-specifier
37463 iterator-specifier , iterators-definition
37464
37465 iterator-specifier:
37466 identifier = range-specification
37467 iterator-type identifier = range-specification
37468
37469 range-specification:
37470 begin : end
37471 begin : end : step */
37472
37473 static tree
37474 cp_parser_omp_iterators (cp_parser *parser)
37475 {
37476 tree ret = NULL_TREE, *last = &ret;
37477 cp_lexer_consume_token (parser->lexer);
37478
37479 matching_parens parens;
37480 if (!parens.require_open (parser))
37481 return error_mark_node;
37482
37483 bool saved_colon_corrects_to_scope_p
37484 = parser->colon_corrects_to_scope_p;
37485 bool saved_colon_doesnt_start_class_def_p
37486 = parser->colon_doesnt_start_class_def_p;
37487
37488 do
37489 {
37490 tree iter_type;
37491 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37492 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
37493 iter_type = integer_type_node;
37494 else
37495 {
37496 const char *saved_message
37497 = parser->type_definition_forbidden_message;
37498 parser->type_definition_forbidden_message
37499 = G_("types may not be defined in iterator type");
37500
37501 iter_type = cp_parser_type_id (parser);
37502
37503 parser->type_definition_forbidden_message = saved_message;
37504 }
37505
37506 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37507 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37508 {
37509 cp_parser_error (parser, "expected identifier");
37510 break;
37511 }
37512
37513 tree id = cp_parser_identifier (parser);
37514 if (id == error_mark_node)
37515 break;
37516
37517 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
37518 break;
37519
37520 parser->colon_corrects_to_scope_p = false;
37521 parser->colon_doesnt_start_class_def_p = true;
37522 tree begin = cp_parser_assignment_expression (parser);
37523
37524 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37525 break;
37526
37527 tree end = cp_parser_assignment_expression (parser);
37528
37529 tree step = integer_one_node;
37530 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
37531 {
37532 cp_lexer_consume_token (parser->lexer);
37533 step = cp_parser_assignment_expression (parser);
37534 }
37535
37536 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
37537 DECL_ARTIFICIAL (iter_var) = 1;
37538 DECL_CONTEXT (iter_var) = current_function_decl;
37539 pushdecl (iter_var);
37540
37541 *last = make_tree_vec (6);
37542 TREE_VEC_ELT (*last, 0) = iter_var;
37543 TREE_VEC_ELT (*last, 1) = begin;
37544 TREE_VEC_ELT (*last, 2) = end;
37545 TREE_VEC_ELT (*last, 3) = step;
37546 last = &TREE_CHAIN (*last);
37547
37548 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37549 {
37550 cp_lexer_consume_token (parser->lexer);
37551 continue;
37552 }
37553 break;
37554 }
37555 while (1);
37556
37557 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37558 parser->colon_doesnt_start_class_def_p
37559 = saved_colon_doesnt_start_class_def_p;
37560
37561 if (!parens.require_close (parser))
37562 cp_parser_skip_to_closing_parenthesis (parser,
37563 /*recovering=*/true,
37564 /*or_comma=*/false,
37565 /*consume_paren=*/true);
37566
37567 return ret ? ret : error_mark_node;
37568 }
37569
37570 /* OpenMP 4.0:
37571 depend ( depend-kind : variable-list )
37572
37573 depend-kind:
37574 in | out | inout
37575
37576 OpenMP 4.5:
37577 depend ( source )
37578
37579 depend ( sink : vec )
37580
37581 OpenMP 5.0:
37582 depend ( depend-modifier , depend-kind: variable-list )
37583
37584 depend-kind:
37585 in | out | inout | mutexinoutset | depobj
37586
37587 depend-modifier:
37588 iterator ( iterators-definition ) */
37589
37590 static tree
37591 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
37592 {
37593 tree nlist, c, iterators = NULL_TREE;
37594 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
37595
37596 matching_parens parens;
37597 if (!parens.require_open (parser))
37598 return list;
37599
37600 do
37601 {
37602 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37603 goto invalid_kind;
37604
37605 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37606 const char *p = IDENTIFIER_POINTER (id);
37607
37608 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
37609 {
37610 begin_scope (sk_omp, NULL);
37611 iterators = cp_parser_omp_iterators (parser);
37612 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
37613 continue;
37614 }
37615 if (strcmp ("in", p) == 0)
37616 kind = OMP_CLAUSE_DEPEND_IN;
37617 else if (strcmp ("inout", p) == 0)
37618 kind = OMP_CLAUSE_DEPEND_INOUT;
37619 else if (strcmp ("mutexinoutset", p) == 0)
37620 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
37621 else if (strcmp ("out", p) == 0)
37622 kind = OMP_CLAUSE_DEPEND_OUT;
37623 else if (strcmp ("depobj", p) == 0)
37624 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
37625 else if (strcmp ("sink", p) == 0)
37626 kind = OMP_CLAUSE_DEPEND_SINK;
37627 else if (strcmp ("source", p) == 0)
37628 kind = OMP_CLAUSE_DEPEND_SOURCE;
37629 else
37630 goto invalid_kind;
37631 break;
37632 }
37633 while (1);
37634
37635 cp_lexer_consume_token (parser->lexer);
37636
37637 if (iterators
37638 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
37639 {
37640 poplevel (0, 1, 0);
37641 error_at (loc, "%<iterator%> modifier incompatible with %qs",
37642 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
37643 iterators = NULL_TREE;
37644 }
37645
37646 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
37647 {
37648 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
37649 OMP_CLAUSE_DEPEND_KIND (c) = kind;
37650 OMP_CLAUSE_DECL (c) = NULL_TREE;
37651 OMP_CLAUSE_CHAIN (c) = list;
37652 if (!parens.require_close (parser))
37653 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37654 /*or_comma=*/false,
37655 /*consume_paren=*/true);
37656 return c;
37657 }
37658
37659 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37660 goto resync_fail;
37661
37662 if (kind == OMP_CLAUSE_DEPEND_SINK)
37663 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
37664 else
37665 {
37666 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
37667 list, NULL);
37668
37669 if (iterators)
37670 {
37671 tree block = poplevel (1, 1, 0);
37672 if (iterators == error_mark_node)
37673 iterators = NULL_TREE;
37674 else
37675 TREE_VEC_ELT (iterators, 5) = block;
37676 }
37677
37678 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
37679 {
37680 OMP_CLAUSE_DEPEND_KIND (c) = kind;
37681 if (iterators)
37682 OMP_CLAUSE_DECL (c)
37683 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
37684 }
37685 }
37686 return nlist;
37687
37688 invalid_kind:
37689 cp_parser_error (parser, "invalid depend kind");
37690 resync_fail:
37691 if (iterators)
37692 poplevel (0, 1, 0);
37693 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37694 /*or_comma=*/false,
37695 /*consume_paren=*/true);
37696 return list;
37697 }
37698
37699 /* OpenMP 4.0:
37700 map ( map-kind : variable-list )
37701 map ( variable-list )
37702
37703 map-kind:
37704 alloc | to | from | tofrom
37705
37706 OpenMP 4.5:
37707 map-kind:
37708 alloc | to | from | tofrom | release | delete
37709
37710 map ( always [,] map-kind: variable-list ) */
37711
37712 static tree
37713 cp_parser_omp_clause_map (cp_parser *parser, tree list)
37714 {
37715 tree nlist, c;
37716 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
37717 bool always = false;
37718
37719 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37720 return list;
37721
37722 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37723 {
37724 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37725 const char *p = IDENTIFIER_POINTER (id);
37726
37727 if (strcmp ("always", p) == 0)
37728 {
37729 int nth = 2;
37730 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
37731 nth++;
37732 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
37733 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
37734 == RID_DELETE))
37735 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
37736 == CPP_COLON))
37737 {
37738 always = true;
37739 cp_lexer_consume_token (parser->lexer);
37740 if (nth == 3)
37741 cp_lexer_consume_token (parser->lexer);
37742 }
37743 }
37744 }
37745
37746 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37747 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
37748 {
37749 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37750 const char *p = IDENTIFIER_POINTER (id);
37751
37752 if (strcmp ("alloc", p) == 0)
37753 kind = GOMP_MAP_ALLOC;
37754 else if (strcmp ("to", p) == 0)
37755 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
37756 else if (strcmp ("from", p) == 0)
37757 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
37758 else if (strcmp ("tofrom", p) == 0)
37759 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
37760 else if (strcmp ("release", p) == 0)
37761 kind = GOMP_MAP_RELEASE;
37762 else
37763 {
37764 cp_parser_error (parser, "invalid map kind");
37765 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37766 /*or_comma=*/false,
37767 /*consume_paren=*/true);
37768 return list;
37769 }
37770 cp_lexer_consume_token (parser->lexer);
37771 cp_lexer_consume_token (parser->lexer);
37772 }
37773 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
37774 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
37775 {
37776 kind = GOMP_MAP_DELETE;
37777 cp_lexer_consume_token (parser->lexer);
37778 cp_lexer_consume_token (parser->lexer);
37779 }
37780
37781 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
37782 NULL);
37783
37784 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
37785 OMP_CLAUSE_SET_MAP_KIND (c, kind);
37786
37787 return nlist;
37788 }
37789
37790 /* OpenMP 4.0:
37791 device ( expression ) */
37792
37793 static tree
37794 cp_parser_omp_clause_device (cp_parser *parser, tree list,
37795 location_t location)
37796 {
37797 tree t, c;
37798
37799 matching_parens parens;
37800 if (!parens.require_open (parser))
37801 return list;
37802
37803 t = cp_parser_assignment_expression (parser);
37804
37805 if (t == error_mark_node
37806 || !parens.require_close (parser))
37807 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37808 /*or_comma=*/false,
37809 /*consume_paren=*/true);
37810
37811 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
37812 "device", location);
37813
37814 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
37815 OMP_CLAUSE_DEVICE_ID (c) = t;
37816 OMP_CLAUSE_CHAIN (c) = list;
37817
37818 return c;
37819 }
37820
37821 /* OpenMP 4.0:
37822 dist_schedule ( static )
37823 dist_schedule ( static , expression ) */
37824
37825 static tree
37826 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
37827 location_t location)
37828 {
37829 tree c, t;
37830
37831 matching_parens parens;
37832 if (!parens.require_open (parser))
37833 return list;
37834
37835 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
37836
37837 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
37838 goto invalid_kind;
37839 cp_lexer_consume_token (parser->lexer);
37840
37841 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37842 {
37843 cp_lexer_consume_token (parser->lexer);
37844
37845 t = cp_parser_assignment_expression (parser);
37846
37847 if (t == error_mark_node)
37848 goto resync_fail;
37849 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
37850
37851 if (!parens.require_close (parser))
37852 goto resync_fail;
37853 }
37854 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
37855 goto resync_fail;
37856
37857 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
37858 "dist_schedule", location); */
37859 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
37860 warning_at (location, 0, "too many %qs clauses", "dist_schedule");
37861 OMP_CLAUSE_CHAIN (c) = list;
37862 return c;
37863
37864 invalid_kind:
37865 cp_parser_error (parser, "invalid dist_schedule kind");
37866 resync_fail:
37867 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37868 /*or_comma=*/false,
37869 /*consume_paren=*/true);
37870 return list;
37871 }
37872
37873 /* OpenMP 4.0:
37874 proc_bind ( proc-bind-kind )
37875
37876 proc-bind-kind:
37877 master | close | spread */
37878
37879 static tree
37880 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
37881 location_t location)
37882 {
37883 tree c;
37884 enum omp_clause_proc_bind_kind kind;
37885
37886 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37887 return list;
37888
37889 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37890 {
37891 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37892 const char *p = IDENTIFIER_POINTER (id);
37893
37894 if (strcmp ("master", p) == 0)
37895 kind = OMP_CLAUSE_PROC_BIND_MASTER;
37896 else if (strcmp ("close", p) == 0)
37897 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
37898 else if (strcmp ("spread", p) == 0)
37899 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
37900 else
37901 goto invalid_kind;
37902 }
37903 else
37904 goto invalid_kind;
37905
37906 cp_lexer_consume_token (parser->lexer);
37907 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
37908 goto resync_fail;
37909
37910 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
37911 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
37912 location);
37913 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
37914 OMP_CLAUSE_CHAIN (c) = list;
37915 return c;
37916
37917 invalid_kind:
37918 cp_parser_error (parser, "invalid depend kind");
37919 resync_fail:
37920 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37921 /*or_comma=*/false,
37922 /*consume_paren=*/true);
37923 return list;
37924 }
37925
37926 /* OpenMP 5.0:
37927 device_type ( host | nohost | any ) */
37928
37929 static tree
37930 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
37931 location_t location)
37932 {
37933 tree c;
37934 enum omp_clause_device_type_kind kind;
37935
37936 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37937 return list;
37938
37939 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37940 {
37941 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37942 const char *p = IDENTIFIER_POINTER (id);
37943
37944 if (strcmp ("host", p) == 0)
37945 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
37946 else if (strcmp ("nohost", p) == 0)
37947 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
37948 else if (strcmp ("any", p) == 0)
37949 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
37950 else
37951 goto invalid_kind;
37952 }
37953 else
37954 goto invalid_kind;
37955
37956 cp_lexer_consume_token (parser->lexer);
37957 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
37958 goto resync_fail;
37959
37960 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
37961 /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
37962 location); */
37963 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
37964 OMP_CLAUSE_CHAIN (c) = list;
37965 return c;
37966
37967 invalid_kind:
37968 cp_parser_error (parser, "invalid depend kind");
37969 resync_fail:
37970 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37971 /*or_comma=*/false,
37972 /*consume_paren=*/true);
37973 return list;
37974 }
37975
37976 /* OpenACC:
37977 async [( int-expr )] */
37978
37979 static tree
37980 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
37981 {
37982 tree c, t;
37983 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37984
37985 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
37986
37987 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37988 {
37989 matching_parens parens;
37990 parens.consume_open (parser);
37991
37992 t = cp_parser_expression (parser);
37993 if (t == error_mark_node
37994 || !parens.require_close (parser))
37995 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37996 /*or_comma=*/false,
37997 /*consume_paren=*/true);
37998 }
37999
38000 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
38001
38002 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
38003 OMP_CLAUSE_ASYNC_EXPR (c) = t;
38004 OMP_CLAUSE_CHAIN (c) = list;
38005 list = c;
38006
38007 return list;
38008 }
38009
38010 /* Parse all OpenACC clauses. The set clauses allowed by the directive
38011 is a bitmask in MASK. Return the list of clauses found. */
38012
38013 static tree
38014 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
38015 const char *where, cp_token *pragma_tok,
38016 bool finish_p = true)
38017 {
38018 tree clauses = NULL;
38019 bool first = true;
38020
38021 /* Don't create location wrapper nodes within OpenACC clauses. */
38022 auto_suppress_location_wrappers sentinel;
38023
38024 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38025 {
38026 location_t here;
38027 pragma_omp_clause c_kind;
38028 omp_clause_code code;
38029 const char *c_name;
38030 tree prev = clauses;
38031
38032 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38033 cp_lexer_consume_token (parser->lexer);
38034
38035 here = cp_lexer_peek_token (parser->lexer)->location;
38036 c_kind = cp_parser_omp_clause_name (parser);
38037
38038 switch (c_kind)
38039 {
38040 case PRAGMA_OACC_CLAUSE_ASYNC:
38041 clauses = cp_parser_oacc_clause_async (parser, clauses);
38042 c_name = "async";
38043 break;
38044 case PRAGMA_OACC_CLAUSE_AUTO:
38045 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
38046 clauses);
38047 c_name = "auto";
38048 break;
38049 case PRAGMA_OACC_CLAUSE_ATTACH:
38050 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38051 c_name = "attach";
38052 break;
38053 case PRAGMA_OACC_CLAUSE_COLLAPSE:
38054 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
38055 c_name = "collapse";
38056 break;
38057 case PRAGMA_OACC_CLAUSE_COPY:
38058 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38059 c_name = "copy";
38060 break;
38061 case PRAGMA_OACC_CLAUSE_COPYIN:
38062 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38063 c_name = "copyin";
38064 break;
38065 case PRAGMA_OACC_CLAUSE_COPYOUT:
38066 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38067 c_name = "copyout";
38068 break;
38069 case PRAGMA_OACC_CLAUSE_CREATE:
38070 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38071 c_name = "create";
38072 break;
38073 case PRAGMA_OACC_CLAUSE_DELETE:
38074 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38075 c_name = "delete";
38076 break;
38077 case PRAGMA_OMP_CLAUSE_DEFAULT:
38078 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
38079 c_name = "default";
38080 break;
38081 case PRAGMA_OACC_CLAUSE_DETACH:
38082 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38083 c_name = "detach";
38084 break;
38085 case PRAGMA_OACC_CLAUSE_DEVICE:
38086 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38087 c_name = "device";
38088 break;
38089 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
38090 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
38091 c_name = "deviceptr";
38092 break;
38093 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
38094 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38095 c_name = "device_resident";
38096 break;
38097 case PRAGMA_OACC_CLAUSE_FINALIZE:
38098 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
38099 clauses);
38100 c_name = "finalize";
38101 break;
38102 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
38103 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38104 clauses);
38105 c_name = "firstprivate";
38106 break;
38107 case PRAGMA_OACC_CLAUSE_GANG:
38108 c_name = "gang";
38109 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
38110 c_name, clauses);
38111 break;
38112 case PRAGMA_OACC_CLAUSE_HOST:
38113 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38114 c_name = "host";
38115 break;
38116 case PRAGMA_OACC_CLAUSE_IF:
38117 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
38118 c_name = "if";
38119 break;
38120 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
38121 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
38122 clauses);
38123 c_name = "if_present";
38124 break;
38125 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
38126 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
38127 clauses);
38128 c_name = "independent";
38129 break;
38130 case PRAGMA_OACC_CLAUSE_LINK:
38131 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38132 c_name = "link";
38133 break;
38134 case PRAGMA_OACC_CLAUSE_NO_CREATE:
38135 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38136 c_name = "no_create";
38137 break;
38138 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
38139 code = OMP_CLAUSE_NUM_GANGS;
38140 c_name = "num_gangs";
38141 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
38142 clauses);
38143 break;
38144 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
38145 c_name = "num_workers";
38146 code = OMP_CLAUSE_NUM_WORKERS;
38147 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
38148 clauses);
38149 break;
38150 case PRAGMA_OACC_CLAUSE_PRESENT:
38151 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38152 c_name = "present";
38153 break;
38154 case PRAGMA_OACC_CLAUSE_PRIVATE:
38155 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
38156 clauses);
38157 c_name = "private";
38158 break;
38159 case PRAGMA_OACC_CLAUSE_REDUCTION:
38160 clauses
38161 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
38162 false, clauses);
38163 c_name = "reduction";
38164 break;
38165 case PRAGMA_OACC_CLAUSE_SEQ:
38166 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
38167 clauses);
38168 c_name = "seq";
38169 break;
38170 case PRAGMA_OACC_CLAUSE_TILE:
38171 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
38172 c_name = "tile";
38173 break;
38174 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
38175 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
38176 clauses);
38177 c_name = "use_device";
38178 break;
38179 case PRAGMA_OACC_CLAUSE_VECTOR:
38180 c_name = "vector";
38181 clauses = cp_parser_oacc_shape_clause (parser, here,
38182 OMP_CLAUSE_VECTOR,
38183 c_name, clauses);
38184 break;
38185 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
38186 c_name = "vector_length";
38187 code = OMP_CLAUSE_VECTOR_LENGTH;
38188 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
38189 clauses);
38190 break;
38191 case PRAGMA_OACC_CLAUSE_WAIT:
38192 clauses = cp_parser_oacc_clause_wait (parser, clauses);
38193 c_name = "wait";
38194 break;
38195 case PRAGMA_OACC_CLAUSE_WORKER:
38196 c_name = "worker";
38197 clauses = cp_parser_oacc_shape_clause (parser, here,
38198 OMP_CLAUSE_WORKER,
38199 c_name, clauses);
38200 break;
38201 default:
38202 cp_parser_error (parser, "expected %<#pragma acc%> clause");
38203 goto saw_error;
38204 }
38205
38206 first = false;
38207
38208 if (((mask >> c_kind) & 1) == 0)
38209 {
38210 /* Remove the invalid clause(s) from the list to avoid
38211 confusing the rest of the compiler. */
38212 clauses = prev;
38213 error_at (here, "%qs is not valid for %qs", c_name, where);
38214 }
38215 }
38216
38217 saw_error:
38218 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38219
38220 if (finish_p)
38221 return finish_omp_clauses (clauses, C_ORT_ACC);
38222
38223 return clauses;
38224 }
38225
38226 /* Parse all OpenMP clauses. The set clauses allowed by the directive
38227 is a bitmask in MASK. Return the list of clauses found.
38228 FINISH_P set if finish_omp_clauses should be called.
38229 NESTED non-zero if clauses should be terminated by closing paren instead
38230 of end of pragma. If it is 2, additionally commas are required in between
38231 the clauses. */
38232
38233 static tree
38234 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
38235 const char *where, cp_token *pragma_tok,
38236 bool finish_p = true, int nested = 0)
38237 {
38238 tree clauses = NULL;
38239 bool first = true;
38240 cp_token *token = NULL;
38241
38242 /* Don't create location wrapper nodes within OpenMP clauses. */
38243 auto_suppress_location_wrappers sentinel;
38244
38245 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38246 {
38247 pragma_omp_clause c_kind;
38248 const char *c_name;
38249 tree prev = clauses;
38250
38251 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38252 break;
38253
38254 if (!first)
38255 {
38256 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38257 cp_lexer_consume_token (parser->lexer);
38258 else if (nested == 2)
38259 error_at (cp_lexer_peek_token (parser->lexer)->location,
38260 "clauses in %<simd%> trait should be separated "
38261 "by %<,%>");
38262 }
38263
38264 token = cp_lexer_peek_token (parser->lexer);
38265 c_kind = cp_parser_omp_clause_name (parser);
38266
38267 switch (c_kind)
38268 {
38269 case PRAGMA_OMP_CLAUSE_BIND:
38270 clauses = cp_parser_omp_clause_bind (parser, clauses,
38271 token->location);
38272 c_name = "bind";
38273 break;
38274 case PRAGMA_OMP_CLAUSE_COLLAPSE:
38275 clauses = cp_parser_omp_clause_collapse (parser, clauses,
38276 token->location);
38277 c_name = "collapse";
38278 break;
38279 case PRAGMA_OMP_CLAUSE_COPYIN:
38280 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
38281 c_name = "copyin";
38282 break;
38283 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
38284 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
38285 clauses);
38286 c_name = "copyprivate";
38287 break;
38288 case PRAGMA_OMP_CLAUSE_DEFAULT:
38289 clauses = cp_parser_omp_clause_default (parser, clauses,
38290 token->location, false);
38291 c_name = "default";
38292 break;
38293 case PRAGMA_OMP_CLAUSE_FINAL:
38294 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
38295 c_name = "final";
38296 break;
38297 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
38298 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38299 clauses);
38300 c_name = "firstprivate";
38301 break;
38302 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
38303 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
38304 token->location);
38305 c_name = "grainsize";
38306 break;
38307 case PRAGMA_OMP_CLAUSE_HINT:
38308 clauses = cp_parser_omp_clause_hint (parser, clauses,
38309 token->location);
38310 c_name = "hint";
38311 break;
38312 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
38313 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
38314 token->location);
38315 c_name = "defaultmap";
38316 break;
38317 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
38318 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
38319 clauses);
38320 c_name = "use_device_ptr";
38321 break;
38322 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
38323 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
38324 clauses);
38325 c_name = "use_device_addr";
38326 break;
38327 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
38328 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
38329 clauses);
38330 c_name = "is_device_ptr";
38331 break;
38332 case PRAGMA_OMP_CLAUSE_IF:
38333 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
38334 true);
38335 c_name = "if";
38336 break;
38337 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
38338 clauses
38339 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
38340 true, clauses);
38341 c_name = "in_reduction";
38342 break;
38343 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
38344 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
38345 c_name = "lastprivate";
38346 break;
38347 case PRAGMA_OMP_CLAUSE_MERGEABLE:
38348 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
38349 token->location);
38350 c_name = "mergeable";
38351 break;
38352 case PRAGMA_OMP_CLAUSE_NOWAIT:
38353 clauses = cp_parser_omp_clause_nowait (parser, clauses,
38354 token->location);
38355 c_name = "nowait";
38356 break;
38357 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
38358 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
38359 token->location);
38360 c_name = "num_tasks";
38361 break;
38362 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
38363 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
38364 token->location);
38365 c_name = "num_threads";
38366 break;
38367 case PRAGMA_OMP_CLAUSE_ORDER:
38368 clauses = cp_parser_omp_clause_order (parser, clauses,
38369 token->location);
38370 c_name = "order";
38371 break;
38372 case PRAGMA_OMP_CLAUSE_ORDERED:
38373 clauses = cp_parser_omp_clause_ordered (parser, clauses,
38374 token->location);
38375 c_name = "ordered";
38376 break;
38377 case PRAGMA_OMP_CLAUSE_PRIORITY:
38378 clauses = cp_parser_omp_clause_priority (parser, clauses,
38379 token->location);
38380 c_name = "priority";
38381 break;
38382 case PRAGMA_OMP_CLAUSE_PRIVATE:
38383 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
38384 clauses);
38385 c_name = "private";
38386 break;
38387 case PRAGMA_OMP_CLAUSE_REDUCTION:
38388 clauses
38389 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
38390 true, clauses);
38391 c_name = "reduction";
38392 break;
38393 case PRAGMA_OMP_CLAUSE_SCHEDULE:
38394 clauses = cp_parser_omp_clause_schedule (parser, clauses,
38395 token->location);
38396 c_name = "schedule";
38397 break;
38398 case PRAGMA_OMP_CLAUSE_SHARED:
38399 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
38400 clauses);
38401 c_name = "shared";
38402 break;
38403 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
38404 clauses
38405 = cp_parser_omp_clause_reduction (parser,
38406 OMP_CLAUSE_TASK_REDUCTION,
38407 true, clauses);
38408 c_name = "task_reduction";
38409 break;
38410 case PRAGMA_OMP_CLAUSE_UNTIED:
38411 clauses = cp_parser_omp_clause_untied (parser, clauses,
38412 token->location);
38413 c_name = "untied";
38414 break;
38415 case PRAGMA_OMP_CLAUSE_INBRANCH:
38416 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
38417 clauses, token->location);
38418 c_name = "inbranch";
38419 break;
38420 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
38421 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
38422 clauses);
38423 c_name = "nontemporal";
38424 break;
38425 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
38426 clauses = cp_parser_omp_clause_branch (parser,
38427 OMP_CLAUSE_NOTINBRANCH,
38428 clauses, token->location);
38429 c_name = "notinbranch";
38430 break;
38431 case PRAGMA_OMP_CLAUSE_PARALLEL:
38432 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
38433 clauses, token->location);
38434 c_name = "parallel";
38435 if (!first)
38436 {
38437 clause_not_first:
38438 error_at (token->location, "%qs must be the first clause of %qs",
38439 c_name, where);
38440 clauses = prev;
38441 }
38442 break;
38443 case PRAGMA_OMP_CLAUSE_FOR:
38444 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
38445 clauses, token->location);
38446 c_name = "for";
38447 if (!first)
38448 goto clause_not_first;
38449 break;
38450 case PRAGMA_OMP_CLAUSE_SECTIONS:
38451 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
38452 clauses, token->location);
38453 c_name = "sections";
38454 if (!first)
38455 goto clause_not_first;
38456 break;
38457 case PRAGMA_OMP_CLAUSE_TASKGROUP:
38458 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
38459 clauses, token->location);
38460 c_name = "taskgroup";
38461 if (!first)
38462 goto clause_not_first;
38463 break;
38464 case PRAGMA_OMP_CLAUSE_LINK:
38465 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
38466 c_name = "to";
38467 break;
38468 case PRAGMA_OMP_CLAUSE_TO:
38469 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
38470 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
38471 clauses);
38472 else
38473 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
38474 c_name = "to";
38475 break;
38476 case PRAGMA_OMP_CLAUSE_FROM:
38477 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
38478 c_name = "from";
38479 break;
38480 case PRAGMA_OMP_CLAUSE_UNIFORM:
38481 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
38482 clauses);
38483 c_name = "uniform";
38484 break;
38485 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
38486 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
38487 token->location);
38488 c_name = "num_teams";
38489 break;
38490 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
38491 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
38492 token->location);
38493 c_name = "thread_limit";
38494 break;
38495 case PRAGMA_OMP_CLAUSE_ALIGNED:
38496 clauses = cp_parser_omp_clause_aligned (parser, clauses);
38497 c_name = "aligned";
38498 break;
38499 case PRAGMA_OMP_CLAUSE_ALLOCATE:
38500 clauses = cp_parser_omp_clause_allocate (parser, clauses);
38501 c_name = "allocate";
38502 break;
38503 case PRAGMA_OMP_CLAUSE_LINEAR:
38504 {
38505 bool declare_simd = false;
38506 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
38507 declare_simd = true;
38508 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
38509 }
38510 c_name = "linear";
38511 break;
38512 case PRAGMA_OMP_CLAUSE_DEPEND:
38513 clauses = cp_parser_omp_clause_depend (parser, clauses,
38514 token->location);
38515 c_name = "depend";
38516 break;
38517 case PRAGMA_OMP_CLAUSE_DETACH:
38518 clauses = cp_parser_omp_clause_detach (parser, clauses);
38519 c_name = "detach";
38520 break;
38521 case PRAGMA_OMP_CLAUSE_MAP:
38522 clauses = cp_parser_omp_clause_map (parser, clauses);
38523 c_name = "map";
38524 break;
38525 case PRAGMA_OMP_CLAUSE_DEVICE:
38526 clauses = cp_parser_omp_clause_device (parser, clauses,
38527 token->location);
38528 c_name = "device";
38529 break;
38530 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
38531 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
38532 token->location);
38533 c_name = "dist_schedule";
38534 break;
38535 case PRAGMA_OMP_CLAUSE_PROC_BIND:
38536 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
38537 token->location);
38538 c_name = "proc_bind";
38539 break;
38540 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
38541 clauses = cp_parser_omp_clause_device_type (parser, clauses,
38542 token->location);
38543 c_name = "device_type";
38544 break;
38545 case PRAGMA_OMP_CLAUSE_SAFELEN:
38546 clauses = cp_parser_omp_clause_safelen (parser, clauses,
38547 token->location);
38548 c_name = "safelen";
38549 break;
38550 case PRAGMA_OMP_CLAUSE_SIMDLEN:
38551 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
38552 token->location);
38553 c_name = "simdlen";
38554 break;
38555 case PRAGMA_OMP_CLAUSE_NOGROUP:
38556 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
38557 token->location);
38558 c_name = "nogroup";
38559 break;
38560 case PRAGMA_OMP_CLAUSE_THREADS:
38561 clauses
38562 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
38563 clauses, token->location);
38564 c_name = "threads";
38565 break;
38566 case PRAGMA_OMP_CLAUSE_SIMD:
38567 clauses
38568 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
38569 clauses, token->location);
38570 c_name = "simd";
38571 break;
38572 default:
38573 cp_parser_error (parser, "expected %<#pragma omp%> clause");
38574 goto saw_error;
38575 }
38576
38577 first = false;
38578
38579 if (((mask >> c_kind) & 1) == 0)
38580 {
38581 /* Remove the invalid clause(s) from the list to avoid
38582 confusing the rest of the compiler. */
38583 clauses = prev;
38584 error_at (token->location, "%qs is not valid for %qs", c_name, where);
38585 }
38586 }
38587 saw_error:
38588 if (!nested)
38589 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38590 if (finish_p)
38591 {
38592 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
38593 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
38594 else
38595 return finish_omp_clauses (clauses, C_ORT_OMP);
38596 }
38597 return clauses;
38598 }
38599
38600 /* OpenMP 2.5:
38601 structured-block:
38602 statement
38603
38604 In practice, we're also interested in adding the statement to an
38605 outer node. So it is convenient if we work around the fact that
38606 cp_parser_statement calls add_stmt. */
38607
38608 static unsigned
38609 cp_parser_begin_omp_structured_block (cp_parser *parser)
38610 {
38611 unsigned save = parser->in_statement;
38612
38613 /* Only move the values to IN_OMP_BLOCK if they weren't false.
38614 This preserves the "not within loop or switch" style error messages
38615 for nonsense cases like
38616 void foo() {
38617 #pragma omp single
38618 break;
38619 }
38620 */
38621 if (parser->in_statement)
38622 parser->in_statement = IN_OMP_BLOCK;
38623
38624 return save;
38625 }
38626
38627 static void
38628 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
38629 {
38630 parser->in_statement = save;
38631 }
38632
38633 static tree
38634 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
38635 {
38636 tree stmt = begin_omp_structured_block ();
38637 unsigned int save = cp_parser_begin_omp_structured_block (parser);
38638
38639 cp_parser_statement (parser, NULL_TREE, false, if_p);
38640
38641 cp_parser_end_omp_structured_block (parser, save);
38642 return finish_omp_structured_block (stmt);
38643 }
38644
38645 /* OpenMP 5.0:
38646 # pragma omp allocate (list) [allocator(allocator)] */
38647
38648 static void
38649 cp_parser_omp_allocate (cp_parser *parser, cp_token *pragma_tok)
38650 {
38651 tree allocator = NULL_TREE;
38652 location_t loc = pragma_tok->location;
38653 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ALLOCATE, NULL_TREE);
38654
38655 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38656 {
38657 matching_parens parens;
38658 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38659 const char *p = IDENTIFIER_POINTER (id);
38660 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
38661 cp_lexer_consume_token (parser->lexer);
38662 if (strcmp (p, "allocator") != 0)
38663 error_at (cloc, "expected %<allocator%>");
38664 else if (parens.require_open (parser))
38665 {
38666 allocator = cp_parser_assignment_expression (parser);
38667 if (allocator == error_mark_node)
38668 allocator = NULL_TREE;
38669 parens.require_close (parser);
38670 }
38671 }
38672 cp_parser_require_pragma_eol (parser, pragma_tok);
38673
38674 if (allocator)
38675 for (tree c = nl; c != NULL_TREE; c = OMP_CLAUSE_CHAIN (c))
38676 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
38677
38678 sorry_at (loc, "%<#pragma omp allocate%> not yet supported");
38679 }
38680
38681 /* OpenMP 2.5:
38682 # pragma omp atomic new-line
38683 expression-stmt
38684
38685 expression-stmt:
38686 x binop= expr | x++ | ++x | x-- | --x
38687 binop:
38688 +, *, -, /, &, ^, |, <<, >>
38689
38690 where x is an lvalue expression with scalar type.
38691
38692 OpenMP 3.1:
38693 # pragma omp atomic new-line
38694 update-stmt
38695
38696 # pragma omp atomic read new-line
38697 read-stmt
38698
38699 # pragma omp atomic write new-line
38700 write-stmt
38701
38702 # pragma omp atomic update new-line
38703 update-stmt
38704
38705 # pragma omp atomic capture new-line
38706 capture-stmt
38707
38708 # pragma omp atomic capture new-line
38709 capture-block
38710
38711 read-stmt:
38712 v = x
38713 write-stmt:
38714 x = expr
38715 update-stmt:
38716 expression-stmt | x = x binop expr
38717 capture-stmt:
38718 v = expression-stmt
38719 capture-block:
38720 { v = x; update-stmt; } | { update-stmt; v = x; }
38721
38722 OpenMP 4.0:
38723 update-stmt:
38724 expression-stmt | x = x binop expr | x = expr binop x
38725 capture-stmt:
38726 v = update-stmt
38727 capture-block:
38728 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
38729
38730 where x and v are lvalue expressions with scalar type. */
38731
38732 static void
38733 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok, bool openacc)
38734 {
38735 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
38736 tree rhs1 = NULL_TREE, orig_lhs;
38737 location_t loc = pragma_tok->location;
38738 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
38739 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
38740 bool structured_block = false;
38741 bool first = true;
38742 tree clauses = NULL_TREE;
38743
38744 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38745 {
38746 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38747 cp_lexer_consume_token (parser->lexer);
38748
38749 first = false;
38750
38751 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38752 {
38753 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38754 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
38755 const char *p = IDENTIFIER_POINTER (id);
38756 enum tree_code new_code = ERROR_MARK;
38757 enum omp_memory_order new_memory_order
38758 = OMP_MEMORY_ORDER_UNSPECIFIED;
38759
38760 if (!strcmp (p, "read"))
38761 new_code = OMP_ATOMIC_READ;
38762 else if (!strcmp (p, "write"))
38763 new_code = NOP_EXPR;
38764 else if (!strcmp (p, "update"))
38765 new_code = OMP_ATOMIC;
38766 else if (!strcmp (p, "capture"))
38767 new_code = OMP_ATOMIC_CAPTURE_NEW;
38768 else if (openacc)
38769 {
38770 p = NULL;
38771 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
38772 "or %<capture%> clause");
38773 }
38774 else if (!strcmp (p, "seq_cst"))
38775 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
38776 else if (!strcmp (p, "acq_rel"))
38777 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
38778 else if (!strcmp (p, "release"))
38779 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
38780 else if (!strcmp (p, "acquire"))
38781 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
38782 else if (!strcmp (p, "relaxed"))
38783 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
38784 else if (!strcmp (p, "hint"))
38785 {
38786 cp_lexer_consume_token (parser->lexer);
38787 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
38788 continue;
38789 }
38790 else
38791 {
38792 p = NULL;
38793 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
38794 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
38795 "%<release%>, %<relaxed%> or %<hint%> clause");
38796 }
38797 if (p)
38798 {
38799 if (new_code != ERROR_MARK)
38800 {
38801 /* OpenACC permits 'update capture'. */
38802 if (openacc
38803 && code == OMP_ATOMIC
38804 && new_code == OMP_ATOMIC_CAPTURE_NEW)
38805 code = new_code;
38806 else if (code != ERROR_MARK)
38807 error_at (cloc, "too many atomic clauses");
38808 else
38809 code = new_code;
38810 }
38811 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
38812 {
38813 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
38814 error_at (cloc, "too many memory order clauses");
38815 else
38816 memory_order = new_memory_order;
38817 }
38818 cp_lexer_consume_token (parser->lexer);
38819 continue;
38820 }
38821 }
38822 break;
38823 }
38824 cp_parser_require_pragma_eol (parser, pragma_tok);
38825
38826 if (code == ERROR_MARK)
38827 code = OMP_ATOMIC;
38828 if (openacc)
38829 memory_order = OMP_MEMORY_ORDER_RELAXED;
38830 else if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
38831 {
38832 omp_requires_mask
38833 = (enum omp_requires) (omp_requires_mask
38834 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
38835 switch ((enum omp_memory_order)
38836 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
38837 {
38838 case OMP_MEMORY_ORDER_UNSPECIFIED:
38839 case OMP_MEMORY_ORDER_RELAXED:
38840 memory_order = OMP_MEMORY_ORDER_RELAXED;
38841 break;
38842 case OMP_MEMORY_ORDER_SEQ_CST:
38843 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
38844 break;
38845 case OMP_MEMORY_ORDER_ACQ_REL:
38846 switch (code)
38847 {
38848 case OMP_ATOMIC_READ:
38849 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
38850 break;
38851 case NOP_EXPR: /* atomic write */
38852 case OMP_ATOMIC:
38853 memory_order = OMP_MEMORY_ORDER_RELEASE;
38854 break;
38855 default:
38856 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
38857 break;
38858 }
38859 break;
38860 default:
38861 gcc_unreachable ();
38862 }
38863 }
38864 else
38865 switch (code)
38866 {
38867 case OMP_ATOMIC_READ:
38868 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
38869 || memory_order == OMP_MEMORY_ORDER_RELEASE)
38870 {
38871 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
38872 "%<acq_rel%> or %<release%> clauses");
38873 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
38874 }
38875 break;
38876 case NOP_EXPR: /* atomic write */
38877 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
38878 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
38879 {
38880 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
38881 "%<acq_rel%> or %<acquire%> clauses");
38882 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
38883 }
38884 break;
38885 case OMP_ATOMIC:
38886 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
38887 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
38888 {
38889 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
38890 "%<acq_rel%> or %<acquire%> clauses");
38891 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
38892 }
38893 break;
38894 default:
38895 break;
38896 }
38897
38898 switch (code)
38899 {
38900 case OMP_ATOMIC_READ:
38901 case NOP_EXPR: /* atomic write */
38902 v = cp_parser_unary_expression (parser);
38903 if (v == error_mark_node)
38904 goto saw_error;
38905 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
38906 goto saw_error;
38907 if (code == NOP_EXPR)
38908 lhs = cp_parser_expression (parser);
38909 else
38910 lhs = cp_parser_unary_expression (parser);
38911 if (lhs == error_mark_node)
38912 goto saw_error;
38913 if (code == NOP_EXPR)
38914 {
38915 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
38916 opcode. */
38917 code = OMP_ATOMIC;
38918 rhs = lhs;
38919 lhs = v;
38920 v = NULL_TREE;
38921 }
38922 goto done;
38923 case OMP_ATOMIC_CAPTURE_NEW:
38924 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
38925 {
38926 cp_lexer_consume_token (parser->lexer);
38927 structured_block = true;
38928 }
38929 else
38930 {
38931 v = cp_parser_unary_expression (parser);
38932 if (v == error_mark_node)
38933 goto saw_error;
38934 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
38935 goto saw_error;
38936 }
38937 default:
38938 break;
38939 }
38940
38941 restart:
38942 lhs = cp_parser_unary_expression (parser);
38943 orig_lhs = lhs;
38944 switch (TREE_CODE (lhs))
38945 {
38946 case ERROR_MARK:
38947 goto saw_error;
38948
38949 case POSTINCREMENT_EXPR:
38950 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
38951 code = OMP_ATOMIC_CAPTURE_OLD;
38952 /* FALLTHROUGH */
38953 case PREINCREMENT_EXPR:
38954 lhs = TREE_OPERAND (lhs, 0);
38955 opcode = PLUS_EXPR;
38956 rhs = integer_one_node;
38957 break;
38958
38959 case POSTDECREMENT_EXPR:
38960 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
38961 code = OMP_ATOMIC_CAPTURE_OLD;
38962 /* FALLTHROUGH */
38963 case PREDECREMENT_EXPR:
38964 lhs = TREE_OPERAND (lhs, 0);
38965 opcode = MINUS_EXPR;
38966 rhs = integer_one_node;
38967 break;
38968
38969 case COMPOUND_EXPR:
38970 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
38971 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
38972 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
38973 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
38974 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
38975 (TREE_OPERAND (lhs, 1), 0), 0)))
38976 == BOOLEAN_TYPE)
38977 /* Undo effects of boolean_increment for post {in,de}crement. */
38978 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
38979 /* FALLTHRU */
38980 case MODIFY_EXPR:
38981 if (TREE_CODE (lhs) == MODIFY_EXPR
38982 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
38983 {
38984 /* Undo effects of boolean_increment. */
38985 if (integer_onep (TREE_OPERAND (lhs, 1)))
38986 {
38987 /* This is pre or post increment. */
38988 rhs = TREE_OPERAND (lhs, 1);
38989 lhs = TREE_OPERAND (lhs, 0);
38990 opcode = NOP_EXPR;
38991 if (code == OMP_ATOMIC_CAPTURE_NEW
38992 && !structured_block
38993 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
38994 code = OMP_ATOMIC_CAPTURE_OLD;
38995 break;
38996 }
38997 }
38998 /* FALLTHRU */
38999 default:
39000 switch (cp_lexer_peek_token (parser->lexer)->type)
39001 {
39002 case CPP_MULT_EQ:
39003 opcode = MULT_EXPR;
39004 break;
39005 case CPP_DIV_EQ:
39006 opcode = TRUNC_DIV_EXPR;
39007 break;
39008 case CPP_PLUS_EQ:
39009 opcode = PLUS_EXPR;
39010 break;
39011 case CPP_MINUS_EQ:
39012 opcode = MINUS_EXPR;
39013 break;
39014 case CPP_LSHIFT_EQ:
39015 opcode = LSHIFT_EXPR;
39016 break;
39017 case CPP_RSHIFT_EQ:
39018 opcode = RSHIFT_EXPR;
39019 break;
39020 case CPP_AND_EQ:
39021 opcode = BIT_AND_EXPR;
39022 break;
39023 case CPP_OR_EQ:
39024 opcode = BIT_IOR_EXPR;
39025 break;
39026 case CPP_XOR_EQ:
39027 opcode = BIT_XOR_EXPR;
39028 break;
39029 case CPP_EQ:
39030 enum cp_parser_prec oprec;
39031 cp_token *token;
39032 cp_lexer_consume_token (parser->lexer);
39033 cp_parser_parse_tentatively (parser);
39034 rhs1 = cp_parser_simple_cast_expression (parser);
39035 if (rhs1 == error_mark_node)
39036 {
39037 cp_parser_abort_tentative_parse (parser);
39038 cp_parser_simple_cast_expression (parser);
39039 goto saw_error;
39040 }
39041 token = cp_lexer_peek_token (parser->lexer);
39042 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
39043 {
39044 cp_parser_abort_tentative_parse (parser);
39045 cp_parser_parse_tentatively (parser);
39046 rhs = cp_parser_binary_expression (parser, false, true,
39047 PREC_NOT_OPERATOR, NULL);
39048 if (rhs == error_mark_node)
39049 {
39050 cp_parser_abort_tentative_parse (parser);
39051 cp_parser_binary_expression (parser, false, true,
39052 PREC_NOT_OPERATOR, NULL);
39053 goto saw_error;
39054 }
39055 switch (TREE_CODE (rhs))
39056 {
39057 case MULT_EXPR:
39058 case TRUNC_DIV_EXPR:
39059 case RDIV_EXPR:
39060 case PLUS_EXPR:
39061 case MINUS_EXPR:
39062 case LSHIFT_EXPR:
39063 case RSHIFT_EXPR:
39064 case BIT_AND_EXPR:
39065 case BIT_IOR_EXPR:
39066 case BIT_XOR_EXPR:
39067 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
39068 {
39069 if (cp_parser_parse_definitely (parser))
39070 {
39071 opcode = TREE_CODE (rhs);
39072 rhs1 = TREE_OPERAND (rhs, 0);
39073 rhs = TREE_OPERAND (rhs, 1);
39074 goto stmt_done;
39075 }
39076 else
39077 goto saw_error;
39078 }
39079 break;
39080 default:
39081 break;
39082 }
39083 cp_parser_abort_tentative_parse (parser);
39084 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
39085 {
39086 rhs = cp_parser_expression (parser);
39087 if (rhs == error_mark_node)
39088 goto saw_error;
39089 opcode = NOP_EXPR;
39090 rhs1 = NULL_TREE;
39091 goto stmt_done;
39092 }
39093 cp_parser_error (parser,
39094 "invalid form of %<#pragma omp atomic%>");
39095 goto saw_error;
39096 }
39097 if (!cp_parser_parse_definitely (parser))
39098 goto saw_error;
39099 switch (token->type)
39100 {
39101 case CPP_SEMICOLON:
39102 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
39103 {
39104 code = OMP_ATOMIC_CAPTURE_OLD;
39105 v = lhs;
39106 lhs = NULL_TREE;
39107 lhs1 = rhs1;
39108 rhs1 = NULL_TREE;
39109 cp_lexer_consume_token (parser->lexer);
39110 goto restart;
39111 }
39112 else if (structured_block)
39113 {
39114 opcode = NOP_EXPR;
39115 rhs = rhs1;
39116 rhs1 = NULL_TREE;
39117 goto stmt_done;
39118 }
39119 cp_parser_error (parser,
39120 "invalid form of %<#pragma omp atomic%>");
39121 goto saw_error;
39122 case CPP_MULT:
39123 opcode = MULT_EXPR;
39124 break;
39125 case CPP_DIV:
39126 opcode = TRUNC_DIV_EXPR;
39127 break;
39128 case CPP_PLUS:
39129 opcode = PLUS_EXPR;
39130 break;
39131 case CPP_MINUS:
39132 opcode = MINUS_EXPR;
39133 break;
39134 case CPP_LSHIFT:
39135 opcode = LSHIFT_EXPR;
39136 break;
39137 case CPP_RSHIFT:
39138 opcode = RSHIFT_EXPR;
39139 break;
39140 case CPP_AND:
39141 opcode = BIT_AND_EXPR;
39142 break;
39143 case CPP_OR:
39144 opcode = BIT_IOR_EXPR;
39145 break;
39146 case CPP_XOR:
39147 opcode = BIT_XOR_EXPR;
39148 break;
39149 default:
39150 cp_parser_error (parser,
39151 "invalid operator for %<#pragma omp atomic%>");
39152 goto saw_error;
39153 }
39154 oprec = TOKEN_PRECEDENCE (token);
39155 gcc_assert (oprec != PREC_NOT_OPERATOR);
39156 if (commutative_tree_code (opcode))
39157 oprec = (enum cp_parser_prec) (oprec - 1);
39158 cp_lexer_consume_token (parser->lexer);
39159 rhs = cp_parser_binary_expression (parser, false, false,
39160 oprec, NULL);
39161 if (rhs == error_mark_node)
39162 goto saw_error;
39163 goto stmt_done;
39164 /* FALLTHROUGH */
39165 default:
39166 cp_parser_error (parser,
39167 "invalid operator for %<#pragma omp atomic%>");
39168 goto saw_error;
39169 }
39170 cp_lexer_consume_token (parser->lexer);
39171
39172 rhs = cp_parser_expression (parser);
39173 if (rhs == error_mark_node)
39174 goto saw_error;
39175 break;
39176 }
39177 stmt_done:
39178 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
39179 {
39180 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
39181 goto saw_error;
39182 v = cp_parser_unary_expression (parser);
39183 if (v == error_mark_node)
39184 goto saw_error;
39185 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
39186 goto saw_error;
39187 lhs1 = cp_parser_unary_expression (parser);
39188 if (lhs1 == error_mark_node)
39189 goto saw_error;
39190 }
39191 if (structured_block)
39192 {
39193 cp_parser_consume_semicolon_at_end_of_statement (parser);
39194 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
39195 }
39196 done:
39197 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
39198 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
39199 rhs1, clauses, memory_order);
39200 if (!structured_block)
39201 cp_parser_consume_semicolon_at_end_of_statement (parser);
39202 return;
39203
39204 saw_error:
39205 cp_parser_skip_to_end_of_block_or_statement (parser);
39206 if (structured_block)
39207 {
39208 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
39209 cp_lexer_consume_token (parser->lexer);
39210 else if (code == OMP_ATOMIC_CAPTURE_NEW)
39211 {
39212 cp_parser_skip_to_end_of_block_or_statement (parser);
39213 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
39214 cp_lexer_consume_token (parser->lexer);
39215 }
39216 }
39217 }
39218
39219
39220 /* OpenMP 2.5:
39221 # pragma omp barrier new-line */
39222
39223 static void
39224 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
39225 {
39226 cp_parser_require_pragma_eol (parser, pragma_tok);
39227 finish_omp_barrier ();
39228 }
39229
39230 /* OpenMP 2.5:
39231 # pragma omp critical [(name)] new-line
39232 structured-block
39233
39234 OpenMP 4.5:
39235 # pragma omp critical [(name) [hint(expression)]] new-line
39236 structured-block */
39237
39238 #define OMP_CRITICAL_CLAUSE_MASK \
39239 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
39240
39241 static tree
39242 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39243 {
39244 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
39245
39246 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39247 {
39248 matching_parens parens;
39249 parens.consume_open (parser);
39250
39251 name = cp_parser_identifier (parser);
39252
39253 if (name == error_mark_node
39254 || !parens.require_close (parser))
39255 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39256 /*or_comma=*/false,
39257 /*consume_paren=*/true);
39258 if (name == error_mark_node)
39259 name = NULL;
39260
39261 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
39262 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
39263 cp_lexer_consume_token (parser->lexer);
39264 }
39265
39266 clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
39267 "#pragma omp critical", pragma_tok);
39268
39269 stmt = cp_parser_omp_structured_block (parser, if_p);
39270 return c_finish_omp_critical (input_location, stmt, name, clauses);
39271 }
39272
39273 /* OpenMP 5.0:
39274 # pragma omp depobj ( depobj ) depobj-clause new-line
39275
39276 depobj-clause:
39277 depend (dependence-type : locator)
39278 destroy
39279 update (dependence-type)
39280
39281 dependence-type:
39282 in
39283 out
39284 inout
39285 mutexinout */
39286
39287 static void
39288 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
39289 {
39290 location_t loc = pragma_tok->location;
39291 matching_parens parens;
39292 if (!parens.require_open (parser))
39293 {
39294 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39295 return;
39296 }
39297
39298 tree depobj = cp_parser_assignment_expression (parser);
39299
39300 if (!parens.require_close (parser))
39301 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39302 /*or_comma=*/false,
39303 /*consume_paren=*/true);
39304
39305 tree clause = NULL_TREE;
39306 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
39307 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
39308 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39309 {
39310 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39311 const char *p = IDENTIFIER_POINTER (id);
39312
39313 cp_lexer_consume_token (parser->lexer);
39314 if (!strcmp ("depend", p))
39315 {
39316 /* Don't create location wrapper nodes within the depend clause. */
39317 auto_suppress_location_wrappers sentinel;
39318 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
39319 if (clause)
39320 clause = finish_omp_clauses (clause, C_ORT_OMP);
39321 if (!clause)
39322 clause = error_mark_node;
39323 }
39324 else if (!strcmp ("destroy", p))
39325 kind = OMP_CLAUSE_DEPEND_LAST;
39326 else if (!strcmp ("update", p))
39327 {
39328 matching_parens c_parens;
39329 if (c_parens.require_open (parser))
39330 {
39331 location_t c2_loc
39332 = cp_lexer_peek_token (parser->lexer)->location;
39333 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39334 {
39335 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
39336 const char *p2 = IDENTIFIER_POINTER (id2);
39337
39338 cp_lexer_consume_token (parser->lexer);
39339 if (!strcmp ("in", p2))
39340 kind = OMP_CLAUSE_DEPEND_IN;
39341 else if (!strcmp ("out", p2))
39342 kind = OMP_CLAUSE_DEPEND_OUT;
39343 else if (!strcmp ("inout", p2))
39344 kind = OMP_CLAUSE_DEPEND_INOUT;
39345 else if (!strcmp ("mutexinoutset", p2))
39346 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
39347 }
39348 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
39349 {
39350 clause = error_mark_node;
39351 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
39352 "%<mutexinoutset%>");
39353 }
39354 if (!c_parens.require_close (parser))
39355 cp_parser_skip_to_closing_parenthesis (parser,
39356 /*recovering=*/true,
39357 /*or_comma=*/false,
39358 /*consume_paren=*/true);
39359 }
39360 else
39361 clause = error_mark_node;
39362 }
39363 }
39364 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
39365 {
39366 clause = error_mark_node;
39367 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
39368 }
39369 cp_parser_require_pragma_eol (parser, pragma_tok);
39370
39371 finish_omp_depobj (loc, depobj, kind, clause);
39372 }
39373
39374
39375 /* OpenMP 2.5:
39376 # pragma omp flush flush-vars[opt] new-line
39377
39378 flush-vars:
39379 ( variable-list )
39380
39381 OpenMP 5.0:
39382 # pragma omp flush memory-order-clause new-line */
39383
39384 static void
39385 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
39386 {
39387 enum memmodel mo = MEMMODEL_LAST;
39388 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39389 {
39390 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39391 const char *p = IDENTIFIER_POINTER (id);
39392 if (!strcmp (p, "acq_rel"))
39393 mo = MEMMODEL_ACQ_REL;
39394 else if (!strcmp (p, "release"))
39395 mo = MEMMODEL_RELEASE;
39396 else if (!strcmp (p, "acquire"))
39397 mo = MEMMODEL_ACQUIRE;
39398 else
39399 error_at (cp_lexer_peek_token (parser->lexer)->location,
39400 "expected %<acq_rel%>, %<release%> or %<acquire%>");
39401 cp_lexer_consume_token (parser->lexer);
39402 }
39403 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39404 {
39405 if (mo != MEMMODEL_LAST)
39406 error_at (cp_lexer_peek_token (parser->lexer)->location,
39407 "%<flush%> list specified together with memory order "
39408 "clause");
39409 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
39410 }
39411 cp_parser_require_pragma_eol (parser, pragma_tok);
39412
39413 finish_omp_flush (mo);
39414 }
39415
39416 /* Helper function, to parse omp for increment expression. */
39417
39418 static tree
39419 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
39420 {
39421 tree cond = cp_parser_binary_expression (parser, false, true,
39422 PREC_NOT_OPERATOR, NULL);
39423 if (cond == error_mark_node
39424 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
39425 {
39426 cp_parser_skip_to_end_of_statement (parser);
39427 return error_mark_node;
39428 }
39429
39430 switch (TREE_CODE (cond))
39431 {
39432 case GT_EXPR:
39433 case GE_EXPR:
39434 case LT_EXPR:
39435 case LE_EXPR:
39436 break;
39437 case NE_EXPR:
39438 if (code != OACC_LOOP)
39439 break;
39440 gcc_fallthrough ();
39441 default:
39442 return error_mark_node;
39443 }
39444
39445 /* If decl is an iterator, preserve LHS and RHS of the relational
39446 expr until finish_omp_for. */
39447 if (decl
39448 && (type_dependent_expression_p (decl)
39449 || CLASS_TYPE_P (TREE_TYPE (decl))))
39450 return cond;
39451
39452 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
39453 TREE_CODE (cond),
39454 TREE_OPERAND (cond, 0), ERROR_MARK,
39455 TREE_OPERAND (cond, 1), ERROR_MARK,
39456 /*overload=*/NULL, tf_warning_or_error);
39457 }
39458
39459 /* Helper function, to parse omp for increment expression. */
39460
39461 static tree
39462 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
39463 {
39464 cp_token *token = cp_lexer_peek_token (parser->lexer);
39465 enum tree_code op;
39466 tree lhs, rhs;
39467 cp_id_kind idk;
39468 bool decl_first;
39469
39470 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
39471 {
39472 op = (token->type == CPP_PLUS_PLUS
39473 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
39474 cp_lexer_consume_token (parser->lexer);
39475 lhs = cp_parser_simple_cast_expression (parser);
39476 if (lhs != decl
39477 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
39478 return error_mark_node;
39479 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
39480 }
39481
39482 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
39483 if (lhs != decl
39484 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
39485 return error_mark_node;
39486
39487 token = cp_lexer_peek_token (parser->lexer);
39488 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
39489 {
39490 op = (token->type == CPP_PLUS_PLUS
39491 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
39492 cp_lexer_consume_token (parser->lexer);
39493 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
39494 }
39495
39496 op = cp_parser_assignment_operator_opt (parser);
39497 if (op == ERROR_MARK)
39498 return error_mark_node;
39499
39500 if (op != NOP_EXPR)
39501 {
39502 rhs = cp_parser_assignment_expression (parser);
39503 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
39504 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
39505 }
39506
39507 lhs = cp_parser_binary_expression (parser, false, false,
39508 PREC_ADDITIVE_EXPRESSION, NULL);
39509 token = cp_lexer_peek_token (parser->lexer);
39510 decl_first = (lhs == decl
39511 || (processing_template_decl && cp_tree_equal (lhs, decl)));
39512 if (decl_first)
39513 lhs = NULL_TREE;
39514 if (token->type != CPP_PLUS
39515 && token->type != CPP_MINUS)
39516 return error_mark_node;
39517
39518 do
39519 {
39520 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
39521 cp_lexer_consume_token (parser->lexer);
39522 rhs = cp_parser_binary_expression (parser, false, false,
39523 PREC_ADDITIVE_EXPRESSION, NULL);
39524 token = cp_lexer_peek_token (parser->lexer);
39525 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
39526 {
39527 if (lhs == NULL_TREE)
39528 {
39529 if (op == PLUS_EXPR)
39530 lhs = rhs;
39531 else
39532 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
39533 tf_warning_or_error);
39534 }
39535 else
39536 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
39537 ERROR_MARK, NULL, tf_warning_or_error);
39538 }
39539 }
39540 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
39541
39542 if (!decl_first)
39543 {
39544 if ((rhs != decl
39545 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
39546 || op == MINUS_EXPR)
39547 return error_mark_node;
39548 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
39549 }
39550 else
39551 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
39552
39553 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
39554 }
39555
39556 /* Parse the initialization statement of an OpenMP for loop.
39557
39558 Return true if the resulting construct should have an
39559 OMP_CLAUSE_PRIVATE added to it. */
39560
39561 static tree
39562 cp_parser_omp_for_loop_init (cp_parser *parser,
39563 tree &this_pre_body,
39564 releasing_vec &for_block,
39565 tree &init,
39566 tree &orig_init,
39567 tree &decl,
39568 tree &real_decl)
39569 {
39570 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
39571 return NULL_TREE;
39572
39573 tree add_private_clause = NULL_TREE;
39574
39575 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
39576
39577 init-expr:
39578 var = lb
39579 integer-type var = lb
39580 random-access-iterator-type var = lb
39581 pointer-type var = lb
39582 */
39583 cp_decl_specifier_seq type_specifiers;
39584
39585 /* First, try to parse as an initialized declaration. See
39586 cp_parser_condition, from whence the bulk of this is copied. */
39587
39588 cp_parser_parse_tentatively (parser);
39589 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
39590 /*is_declaration=*/true,
39591 /*is_trailing_return=*/false,
39592 &type_specifiers);
39593 if (cp_parser_parse_definitely (parser))
39594 {
39595 /* If parsing a type specifier seq succeeded, then this
39596 MUST be a initialized declaration. */
39597 tree asm_specification, attributes;
39598 cp_declarator *declarator;
39599
39600 declarator = cp_parser_declarator (parser,
39601 CP_PARSER_DECLARATOR_NAMED,
39602 CP_PARSER_FLAGS_NONE,
39603 /*ctor_dtor_or_conv_p=*/NULL,
39604 /*parenthesized_p=*/NULL,
39605 /*member_p=*/false,
39606 /*friend_p=*/false,
39607 /*static_p=*/false);
39608 attributes = cp_parser_attributes_opt (parser);
39609 asm_specification = cp_parser_asm_specification_opt (parser);
39610
39611 if (declarator == cp_error_declarator)
39612 cp_parser_skip_to_end_of_statement (parser);
39613
39614 else
39615 {
39616 tree pushed_scope, auto_node;
39617
39618 decl = start_decl (declarator, &type_specifiers,
39619 SD_INITIALIZED, attributes,
39620 /*prefix_attributes=*/NULL_TREE,
39621 &pushed_scope);
39622
39623 auto_node = type_uses_auto (TREE_TYPE (decl));
39624 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
39625 {
39626 if (cp_lexer_next_token_is (parser->lexer,
39627 CPP_OPEN_PAREN))
39628 error ("parenthesized initialization is not allowed in "
39629 "OpenMP %<for%> loop");
39630 else
39631 /* Trigger an error. */
39632 cp_parser_require (parser, CPP_EQ, RT_EQ);
39633
39634 init = error_mark_node;
39635 cp_parser_skip_to_end_of_statement (parser);
39636 }
39637 else if (CLASS_TYPE_P (TREE_TYPE (decl))
39638 || type_dependent_expression_p (decl)
39639 || auto_node)
39640 {
39641 bool is_direct_init, is_non_constant_init;
39642
39643 init = cp_parser_initializer (parser,
39644 &is_direct_init,
39645 &is_non_constant_init);
39646
39647 if (auto_node)
39648 {
39649 TREE_TYPE (decl)
39650 = do_auto_deduction (TREE_TYPE (decl), init,
39651 auto_node);
39652
39653 if (!CLASS_TYPE_P (TREE_TYPE (decl))
39654 && !type_dependent_expression_p (decl))
39655 goto non_class;
39656 }
39657
39658 cp_finish_decl (decl, init, !is_non_constant_init,
39659 asm_specification,
39660 LOOKUP_ONLYCONVERTING);
39661 orig_init = init;
39662 if (CLASS_TYPE_P (TREE_TYPE (decl)))
39663 {
39664 vec_safe_push (for_block, this_pre_body);
39665 init = NULL_TREE;
39666 }
39667 else
39668 {
39669 init = pop_stmt_list (this_pre_body);
39670 if (init && TREE_CODE (init) == STATEMENT_LIST)
39671 {
39672 tree_stmt_iterator i = tsi_start (init);
39673 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
39674 while (!tsi_end_p (i))
39675 {
39676 tree t = tsi_stmt (i);
39677 if (TREE_CODE (t) == DECL_EXPR
39678 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
39679 {
39680 tsi_delink (&i);
39681 vec_safe_push (for_block, t);
39682 continue;
39683 }
39684 break;
39685 }
39686 if (tsi_one_before_end_p (i))
39687 {
39688 tree t = tsi_stmt (i);
39689 tsi_delink (&i);
39690 free_stmt_list (init);
39691 init = t;
39692 }
39693 }
39694 }
39695 this_pre_body = NULL_TREE;
39696 }
39697 else
39698 {
39699 /* Consume '='. */
39700 cp_lexer_consume_token (parser->lexer);
39701 init = cp_parser_assignment_expression (parser);
39702
39703 non_class:
39704 if (TYPE_REF_P (TREE_TYPE (decl)))
39705 init = error_mark_node;
39706 else
39707 cp_finish_decl (decl, NULL_TREE,
39708 /*init_const_expr_p=*/false,
39709 asm_specification,
39710 LOOKUP_ONLYCONVERTING);
39711 }
39712
39713 if (pushed_scope)
39714 pop_scope (pushed_scope);
39715 }
39716 }
39717 else
39718 {
39719 cp_id_kind idk;
39720 /* If parsing a type specifier sequence failed, then
39721 this MUST be a simple expression. */
39722 cp_parser_parse_tentatively (parser);
39723 decl = cp_parser_primary_expression (parser, false, false,
39724 false, &idk);
39725 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
39726 if (!cp_parser_error_occurred (parser)
39727 && decl
39728 && (TREE_CODE (decl) == COMPONENT_REF
39729 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
39730 {
39731 cp_parser_abort_tentative_parse (parser);
39732 cp_parser_parse_tentatively (parser);
39733 cp_token *token = cp_lexer_peek_token (parser->lexer);
39734 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
39735 /*check_dependency_p=*/true,
39736 /*template_p=*/NULL,
39737 /*declarator_p=*/false,
39738 /*optional_p=*/false);
39739 if (name != error_mark_node
39740 && last_tok == cp_lexer_peek_token (parser->lexer))
39741 {
39742 decl = cp_parser_lookup_name_simple (parser, name,
39743 token->location);
39744 if (TREE_CODE (decl) == FIELD_DECL)
39745 add_private_clause = omp_privatize_field (decl, false);
39746 }
39747 cp_parser_abort_tentative_parse (parser);
39748 cp_parser_parse_tentatively (parser);
39749 decl = cp_parser_primary_expression (parser, false, false,
39750 false, &idk);
39751 }
39752 if (!cp_parser_error_occurred (parser)
39753 && decl
39754 && DECL_P (decl)
39755 && CLASS_TYPE_P (TREE_TYPE (decl)))
39756 {
39757 tree rhs;
39758
39759 cp_parser_parse_definitely (parser);
39760 cp_parser_require (parser, CPP_EQ, RT_EQ);
39761 rhs = cp_parser_assignment_expression (parser);
39762 orig_init = rhs;
39763 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
39764 decl, NOP_EXPR,
39765 rhs,
39766 tf_warning_or_error));
39767 if (!add_private_clause)
39768 add_private_clause = decl;
39769 }
39770 else
39771 {
39772 decl = NULL;
39773 cp_parser_abort_tentative_parse (parser);
39774 init = cp_parser_expression (parser);
39775 if (init)
39776 {
39777 if (TREE_CODE (init) == MODIFY_EXPR
39778 || TREE_CODE (init) == MODOP_EXPR)
39779 real_decl = TREE_OPERAND (init, 0);
39780 }
39781 }
39782 }
39783 return add_private_clause;
39784 }
39785
39786 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
39787
39788 void
39789 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
39790 tree &decl, tree &orig_decl, tree &init,
39791 tree &orig_init, tree &cond, tree &incr)
39792 {
39793 tree begin, end, range_temp_decl = NULL_TREE;
39794 tree iter_type, begin_expr, end_expr;
39795
39796 if (processing_template_decl)
39797 {
39798 if (check_for_bare_parameter_packs (init))
39799 init = error_mark_node;
39800 if (!type_dependent_expression_p (init)
39801 /* do_auto_deduction doesn't mess with template init-lists. */
39802 && !BRACE_ENCLOSED_INITIALIZER_P (init))
39803 {
39804 tree d = decl;
39805 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
39806 {
39807 tree v = DECL_VALUE_EXPR (decl);
39808 if (TREE_CODE (v) == ARRAY_REF
39809 && VAR_P (TREE_OPERAND (v, 0))
39810 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
39811 d = TREE_OPERAND (v, 0);
39812 }
39813 do_range_for_auto_deduction (d, init);
39814 }
39815 cond = global_namespace;
39816 incr = NULL_TREE;
39817 orig_init = init;
39818 if (this_pre_body)
39819 this_pre_body = pop_stmt_list (this_pre_body);
39820 return;
39821 }
39822
39823 init = mark_lvalue_use (init);
39824
39825 if (decl == error_mark_node || init == error_mark_node)
39826 /* If an error happened previously do nothing or else a lot of
39827 unhelpful errors would be issued. */
39828 begin_expr = end_expr = iter_type = error_mark_node;
39829 else
39830 {
39831 tree range_temp;
39832
39833 if (VAR_P (init)
39834 && array_of_runtime_bound_p (TREE_TYPE (init)))
39835 /* Can't bind a reference to an array of runtime bound. */
39836 range_temp = init;
39837 else
39838 {
39839 range_temp = build_range_temp (init);
39840 DECL_NAME (range_temp) = NULL_TREE;
39841 pushdecl (range_temp);
39842 cp_finish_decl (range_temp, init,
39843 /*is_constant_init*/false, NULL_TREE,
39844 LOOKUP_ONLYCONVERTING);
39845 range_temp_decl = range_temp;
39846 range_temp = convert_from_reference (range_temp);
39847 }
39848 iter_type = cp_parser_perform_range_for_lookup (range_temp,
39849 &begin_expr, &end_expr);
39850 }
39851
39852 tree end_iter_type = iter_type;
39853 if (cxx_dialect >= cxx17)
39854 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
39855 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
39856 TREE_USED (end) = 1;
39857 DECL_ARTIFICIAL (end) = 1;
39858 pushdecl (end);
39859 cp_finish_decl (end, end_expr,
39860 /*is_constant_init*/false, NULL_TREE,
39861 LOOKUP_ONLYCONVERTING);
39862
39863 /* The new for initialization statement. */
39864 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
39865 TREE_USED (begin) = 1;
39866 DECL_ARTIFICIAL (begin) = 1;
39867 pushdecl (begin);
39868 orig_init = init;
39869 if (CLASS_TYPE_P (iter_type))
39870 init = NULL_TREE;
39871 else
39872 {
39873 init = begin_expr;
39874 begin_expr = NULL_TREE;
39875 }
39876 cp_finish_decl (begin, begin_expr,
39877 /*is_constant_init*/false, NULL_TREE,
39878 LOOKUP_ONLYCONVERTING);
39879
39880 /* The new for condition. */
39881 if (CLASS_TYPE_P (iter_type))
39882 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
39883 else
39884 cond = build_x_binary_op (input_location, NE_EXPR,
39885 begin, ERROR_MARK,
39886 end, ERROR_MARK,
39887 NULL, tf_warning_or_error);
39888
39889 /* The new increment expression. */
39890 if (CLASS_TYPE_P (iter_type))
39891 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
39892 else
39893 incr = finish_unary_op_expr (input_location,
39894 PREINCREMENT_EXPR, begin,
39895 tf_warning_or_error);
39896
39897 orig_decl = decl;
39898 decl = begin;
39899 if (for_block)
39900 {
39901 vec_safe_push (for_block, this_pre_body);
39902 this_pre_body = NULL_TREE;
39903 }
39904
39905 tree decomp_first_name = NULL_TREE;
39906 unsigned decomp_cnt = 0;
39907 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
39908 {
39909 tree v = DECL_VALUE_EXPR (orig_decl);
39910 if (TREE_CODE (v) == ARRAY_REF
39911 && VAR_P (TREE_OPERAND (v, 0))
39912 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
39913 {
39914 tree d = orig_decl;
39915 orig_decl = TREE_OPERAND (v, 0);
39916 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
39917 decomp_first_name = d;
39918 }
39919 }
39920
39921 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
39922 if (auto_node)
39923 {
39924 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
39925 tf_none);
39926 if (!error_operand_p (t))
39927 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
39928 t, auto_node);
39929 }
39930
39931 tree v = make_tree_vec (decomp_cnt + 3);
39932 TREE_VEC_ELT (v, 0) = range_temp_decl;
39933 TREE_VEC_ELT (v, 1) = end;
39934 TREE_VEC_ELT (v, 2) = orig_decl;
39935 for (unsigned i = 0; i < decomp_cnt; i++)
39936 {
39937 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
39938 decomp_first_name = DECL_CHAIN (decomp_first_name);
39939 }
39940 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
39941 }
39942
39943 /* Helper for cp_parser_omp_for_loop, finalize part of range for
39944 inside of the collapsed body. */
39945
39946 void
39947 cp_finish_omp_range_for (tree orig, tree begin)
39948 {
39949 gcc_assert (TREE_CODE (orig) == TREE_LIST
39950 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
39951 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
39952 tree decomp_first_name = NULL_TREE;
39953 unsigned int decomp_cnt = 0;
39954
39955 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
39956 {
39957 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
39958 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
39959 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
39960 }
39961
39962 /* The declaration is initialized with *__begin inside the loop body. */
39963 cp_finish_decl (decl,
39964 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
39965 tf_warning_or_error),
39966 /*is_constant_init*/false, NULL_TREE,
39967 LOOKUP_ONLYCONVERTING);
39968 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
39969 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
39970 }
39971
39972 /* OpenMP 5.0:
39973
39974 scan-loop-body:
39975 { structured-block scan-directive structured-block } */
39976
39977 static void
39978 cp_parser_omp_scan_loop_body (cp_parser *parser)
39979 {
39980 tree substmt, clauses = NULL_TREE;
39981
39982 matching_braces braces;
39983 if (!braces.require_open (parser))
39984 return;
39985
39986 substmt = cp_parser_omp_structured_block (parser, NULL);
39987 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
39988 add_stmt (substmt);
39989
39990 cp_token *tok = cp_lexer_peek_token (parser->lexer);
39991 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
39992 {
39993 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
39994
39995 cp_lexer_consume_token (parser->lexer);
39996
39997 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39998 {
39999 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40000 const char *p = IDENTIFIER_POINTER (id);
40001 if (strcmp (p, "inclusive") == 0)
40002 clause = OMP_CLAUSE_INCLUSIVE;
40003 else if (strcmp (p, "exclusive") == 0)
40004 clause = OMP_CLAUSE_EXCLUSIVE;
40005 }
40006 if (clause != OMP_CLAUSE_ERROR)
40007 {
40008 cp_lexer_consume_token (parser->lexer);
40009 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
40010 }
40011 else
40012 cp_parser_error (parser, "expected %<inclusive%> or "
40013 "%<exclusive%> clause");
40014
40015 cp_parser_require_pragma_eol (parser, tok);
40016 }
40017 else
40018 error ("expected %<#pragma omp scan%>");
40019
40020 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
40021 substmt = cp_parser_omp_structured_block (parser, NULL);
40022 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
40023 clauses);
40024 add_stmt (substmt);
40025
40026 braces.require_close (parser);
40027 }
40028
40029 /* Parse the restricted form of the for statement allowed by OpenMP. */
40030
40031 static tree
40032 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
40033 tree *cclauses, bool *if_p)
40034 {
40035 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
40036 tree orig_decl;
40037 tree real_decl, initv, condv, incrv, declv, orig_declv;
40038 tree this_pre_body, cl, ordered_cl = NULL_TREE;
40039 location_t loc_first;
40040 bool collapse_err = false;
40041 int i, collapse = 1, ordered = 0, count, nbraces = 0;
40042 releasing_vec for_block;
40043 auto_vec<tree, 4> orig_inits;
40044 bool tiling = false;
40045 bool inscan = false;
40046
40047 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
40048 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
40049 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
40050 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
40051 {
40052 tiling = true;
40053 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
40054 }
40055 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
40056 && OMP_CLAUSE_ORDERED_EXPR (cl))
40057 {
40058 ordered_cl = cl;
40059 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
40060 }
40061 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
40062 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
40063 && (code == OMP_SIMD || code == OMP_FOR))
40064 inscan = true;
40065
40066 if (ordered && ordered < collapse)
40067 {
40068 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
40069 "%<ordered%> clause parameter is less than %<collapse%>");
40070 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
40071 = build_int_cst (NULL_TREE, collapse);
40072 ordered = collapse;
40073 }
40074 if (ordered)
40075 {
40076 for (tree *pc = &clauses; *pc; )
40077 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
40078 {
40079 error_at (OMP_CLAUSE_LOCATION (*pc),
40080 "%<linear%> clause may not be specified together "
40081 "with %<ordered%> clause with a parameter");
40082 *pc = OMP_CLAUSE_CHAIN (*pc);
40083 }
40084 else
40085 pc = &OMP_CLAUSE_CHAIN (*pc);
40086 }
40087
40088 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
40089 count = ordered ? ordered : collapse;
40090
40091 declv = make_tree_vec (count);
40092 initv = make_tree_vec (count);
40093 condv = make_tree_vec (count);
40094 incrv = make_tree_vec (count);
40095 orig_declv = NULL_TREE;
40096
40097 loc_first = cp_lexer_peek_token (parser->lexer)->location;
40098
40099 for (i = 0; i < count; i++)
40100 {
40101 int bracecount = 0;
40102 tree add_private_clause = NULL_TREE;
40103 location_t loc;
40104
40105 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
40106 {
40107 if (!collapse_err)
40108 cp_parser_error (parser, "for statement expected");
40109 return NULL;
40110 }
40111 loc = cp_lexer_consume_token (parser->lexer)->location;
40112
40113 /* Don't create location wrapper nodes within an OpenMP "for"
40114 statement. */
40115 auto_suppress_location_wrappers sentinel;
40116
40117 matching_parens parens;
40118 if (!parens.require_open (parser))
40119 return NULL;
40120
40121 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
40122 this_pre_body = push_stmt_list ();
40123
40124 if (code != OACC_LOOP && cxx_dialect >= cxx11)
40125 {
40126 /* Save tokens so that we can put them back. */
40127 cp_lexer_save_tokens (parser->lexer);
40128
40129 /* Look for ':' that is not nested in () or {}. */
40130 bool is_range_for
40131 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
40132 /*recovering=*/false,
40133 CPP_COLON,
40134 /*consume_paren=*/
40135 false) == -1);
40136
40137 /* Roll back the tokens we skipped. */
40138 cp_lexer_rollback_tokens (parser->lexer);
40139
40140 if (is_range_for)
40141 {
40142 bool saved_colon_corrects_to_scope_p
40143 = parser->colon_corrects_to_scope_p;
40144
40145 /* A colon is used in range-based for. */
40146 parser->colon_corrects_to_scope_p = false;
40147
40148 /* Parse the declaration. */
40149 cp_parser_simple_declaration (parser,
40150 /*function_definition_allowed_p=*/
40151 false, &decl);
40152 parser->colon_corrects_to_scope_p
40153 = saved_colon_corrects_to_scope_p;
40154
40155 cp_parser_require (parser, CPP_COLON, RT_COLON);
40156
40157 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
40158 false, 0, true);
40159
40160 cp_convert_omp_range_for (this_pre_body, for_block, decl,
40161 orig_decl, init, orig_init,
40162 cond, incr);
40163 if (this_pre_body)
40164 {
40165 if (pre_body)
40166 {
40167 tree t = pre_body;
40168 pre_body = push_stmt_list ();
40169 add_stmt (t);
40170 add_stmt (this_pre_body);
40171 pre_body = pop_stmt_list (pre_body);
40172 }
40173 else
40174 pre_body = this_pre_body;
40175 }
40176
40177 if (ordered_cl)
40178 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
40179 "%<ordered%> clause with parameter on "
40180 "range-based %<for%> loop");
40181
40182 goto parse_close_paren;
40183 }
40184 }
40185
40186 add_private_clause
40187 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
40188 init, orig_init, decl, real_decl);
40189
40190 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40191 if (this_pre_body)
40192 {
40193 this_pre_body = pop_stmt_list (this_pre_body);
40194 if (pre_body)
40195 {
40196 tree t = pre_body;
40197 pre_body = push_stmt_list ();
40198 add_stmt (t);
40199 add_stmt (this_pre_body);
40200 pre_body = pop_stmt_list (pre_body);
40201 }
40202 else
40203 pre_body = this_pre_body;
40204 }
40205
40206 if (decl)
40207 real_decl = decl;
40208 if (cclauses != NULL
40209 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
40210 && real_decl != NULL_TREE
40211 && code != OMP_LOOP)
40212 {
40213 tree *c;
40214 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
40215 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
40216 && OMP_CLAUSE_DECL (*c) == real_decl)
40217 {
40218 error_at (loc, "iteration variable %qD"
40219 " should not be firstprivate", real_decl);
40220 *c = OMP_CLAUSE_CHAIN (*c);
40221 }
40222 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
40223 && OMP_CLAUSE_DECL (*c) == real_decl)
40224 {
40225 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
40226 tree l = *c;
40227 *c = OMP_CLAUSE_CHAIN (*c);
40228 if (code == OMP_SIMD)
40229 {
40230 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
40231 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
40232 }
40233 else
40234 {
40235 OMP_CLAUSE_CHAIN (l) = clauses;
40236 clauses = l;
40237 }
40238 add_private_clause = NULL_TREE;
40239 }
40240 else
40241 {
40242 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
40243 && OMP_CLAUSE_DECL (*c) == real_decl)
40244 add_private_clause = NULL_TREE;
40245 c = &OMP_CLAUSE_CHAIN (*c);
40246 }
40247 }
40248
40249 if (add_private_clause)
40250 {
40251 tree c;
40252 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
40253 {
40254 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
40255 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
40256 && OMP_CLAUSE_DECL (c) == decl)
40257 break;
40258 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
40259 && OMP_CLAUSE_DECL (c) == decl)
40260 error_at (loc, "iteration variable %qD "
40261 "should not be firstprivate",
40262 decl);
40263 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
40264 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
40265 && OMP_CLAUSE_DECL (c) == decl)
40266 error_at (loc, "iteration variable %qD should not be reduction",
40267 decl);
40268 }
40269 if (c == NULL)
40270 {
40271 if ((code == OMP_SIMD && collapse != 1) || code == OMP_LOOP)
40272 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
40273 else if (code != OMP_SIMD)
40274 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
40275 else
40276 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
40277 OMP_CLAUSE_DECL (c) = add_private_clause;
40278 c = finish_omp_clauses (c, C_ORT_OMP);
40279 if (c)
40280 {
40281 OMP_CLAUSE_CHAIN (c) = clauses;
40282 clauses = c;
40283 /* For linear, signal that we need to fill up
40284 the so far unknown linear step. */
40285 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
40286 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
40287 }
40288 }
40289 }
40290
40291 cond = NULL;
40292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
40293 cond = cp_parser_omp_for_cond (parser, decl, code);
40294 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40295
40296 incr = NULL;
40297 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
40298 {
40299 /* If decl is an iterator, preserve the operator on decl
40300 until finish_omp_for. */
40301 if (real_decl
40302 && ((processing_template_decl
40303 && (TREE_TYPE (real_decl) == NULL_TREE
40304 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
40305 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
40306 incr = cp_parser_omp_for_incr (parser, real_decl);
40307 else
40308 incr = cp_parser_expression (parser);
40309 protected_set_expr_location_if_unset (incr, input_location);
40310 }
40311
40312 parse_close_paren:
40313 if (!parens.require_close (parser))
40314 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40315 /*or_comma=*/false,
40316 /*consume_paren=*/true);
40317
40318 TREE_VEC_ELT (declv, i) = decl;
40319 TREE_VEC_ELT (initv, i) = init;
40320 TREE_VEC_ELT (condv, i) = cond;
40321 TREE_VEC_ELT (incrv, i) = incr;
40322 if (orig_init)
40323 {
40324 orig_inits.safe_grow_cleared (i + 1, true);
40325 orig_inits[i] = orig_init;
40326 }
40327 if (orig_decl)
40328 {
40329 if (!orig_declv)
40330 orig_declv = copy_node (declv);
40331 TREE_VEC_ELT (orig_declv, i) = orig_decl;
40332 }
40333 else if (orig_declv)
40334 TREE_VEC_ELT (orig_declv, i) = decl;
40335
40336 if (i == count - 1)
40337 break;
40338
40339 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
40340 in between the collapsed for loops to be still considered perfectly
40341 nested. Hopefully the final version clarifies this.
40342 For now handle (multiple) {'s and empty statements. */
40343 cp_parser_parse_tentatively (parser);
40344 for (;;)
40345 {
40346 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
40347 break;
40348 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
40349 {
40350 cp_lexer_consume_token (parser->lexer);
40351 bracecount++;
40352 }
40353 else if (bracecount
40354 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
40355 cp_lexer_consume_token (parser->lexer);
40356 else
40357 {
40358 loc = cp_lexer_peek_token (parser->lexer)->location;
40359 error_at (loc, "not enough for loops to collapse");
40360 collapse_err = true;
40361 cp_parser_abort_tentative_parse (parser);
40362 declv = NULL_TREE;
40363 break;
40364 }
40365 }
40366
40367 if (declv)
40368 {
40369 cp_parser_parse_definitely (parser);
40370 nbraces += bracecount;
40371 }
40372 }
40373
40374 if (nbraces)
40375 if_p = NULL;
40376
40377 /* Note that we saved the original contents of this flag when we entered
40378 the structured block, and so we don't need to re-save it here. */
40379 parser->in_statement = IN_OMP_FOR;
40380
40381 /* Note that the grammar doesn't call for a structured block here,
40382 though the loop as a whole is a structured block. */
40383 if (orig_declv)
40384 {
40385 body = begin_omp_structured_block ();
40386 for (i = 0; i < count; i++)
40387 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
40388 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
40389 TREE_VEC_ELT (declv, i));
40390 }
40391 else
40392 body = push_stmt_list ();
40393 if (inscan)
40394 cp_parser_omp_scan_loop_body (parser);
40395 else
40396 cp_parser_statement (parser, NULL_TREE, false, if_p);
40397 if (orig_declv)
40398 body = finish_omp_structured_block (body);
40399 else
40400 body = pop_stmt_list (body);
40401
40402 if (declv == NULL_TREE)
40403 ret = NULL_TREE;
40404 else
40405 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
40406 incrv, body, pre_body, &orig_inits, clauses);
40407
40408 while (nbraces)
40409 {
40410 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
40411 {
40412 cp_lexer_consume_token (parser->lexer);
40413 nbraces--;
40414 }
40415 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
40416 cp_lexer_consume_token (parser->lexer);
40417 else
40418 {
40419 if (!collapse_err)
40420 {
40421 error_at (cp_lexer_peek_token (parser->lexer)->location,
40422 "collapsed loops not perfectly nested");
40423 }
40424 collapse_err = true;
40425 cp_parser_statement_seq_opt (parser, NULL);
40426 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
40427 break;
40428 }
40429 }
40430
40431 while (!for_block->is_empty ())
40432 {
40433 tree t = for_block->pop ();
40434 if (TREE_CODE (t) == STATEMENT_LIST)
40435 add_stmt (pop_stmt_list (t));
40436 else
40437 add_stmt (t);
40438 }
40439
40440 return ret;
40441 }
40442
40443 /* Helper function for OpenMP parsing, split clauses and call
40444 finish_omp_clauses on each of the set of clauses afterwards. */
40445
40446 static void
40447 cp_omp_split_clauses (location_t loc, enum tree_code code,
40448 omp_clause_mask mask, tree clauses, tree *cclauses)
40449 {
40450 int i;
40451 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
40452 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
40453 if (cclauses[i])
40454 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
40455 }
40456
40457 /* OpenMP 5.0:
40458 #pragma omp loop loop-clause[optseq] new-line
40459 for-loop */
40460
40461 #define OMP_LOOP_CLAUSE_MASK \
40462 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
40466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
40467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
40468
40469 static tree
40470 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
40471 char *p_name, omp_clause_mask mask, tree *cclauses,
40472 bool *if_p)
40473 {
40474 tree clauses, sb, ret;
40475 unsigned int save;
40476 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40477
40478 strcat (p_name, " loop");
40479 mask |= OMP_LOOP_CLAUSE_MASK;
40480
40481 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40482 cclauses == NULL);
40483 if (cclauses)
40484 {
40485 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
40486 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
40487 }
40488
40489 keep_next_level (true);
40490 sb = begin_omp_structured_block ();
40491 save = cp_parser_begin_omp_structured_block (parser);
40492
40493 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
40494
40495 cp_parser_end_omp_structured_block (parser, save);
40496 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
40497
40498 return ret;
40499 }
40500
40501 /* OpenMP 4.0:
40502 #pragma omp simd simd-clause[optseq] new-line
40503 for-loop */
40504
40505 #define OMP_SIMD_CLAUSE_MASK \
40506 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
40507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
40508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
40509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
40510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
40514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
40516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
40517
40518 static tree
40519 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
40520 char *p_name, omp_clause_mask mask, tree *cclauses,
40521 bool *if_p)
40522 {
40523 tree clauses, sb, ret;
40524 unsigned int save;
40525 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40526
40527 strcat (p_name, " simd");
40528 mask |= OMP_SIMD_CLAUSE_MASK;
40529
40530 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40531 cclauses == NULL);
40532 if (cclauses)
40533 {
40534 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
40535 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
40536 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
40537 OMP_CLAUSE_ORDERED);
40538 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
40539 {
40540 error_at (OMP_CLAUSE_LOCATION (c),
40541 "%<ordered%> clause with parameter may not be specified "
40542 "on %qs construct", p_name);
40543 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
40544 }
40545 }
40546
40547 keep_next_level (true);
40548 sb = begin_omp_structured_block ();
40549 save = cp_parser_begin_omp_structured_block (parser);
40550
40551 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
40552
40553 cp_parser_end_omp_structured_block (parser, save);
40554 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
40555
40556 return ret;
40557 }
40558
40559 /* OpenMP 2.5:
40560 #pragma omp for for-clause[optseq] new-line
40561 for-loop
40562
40563 OpenMP 4.0:
40564 #pragma omp for simd for-simd-clause[optseq] new-line
40565 for-loop */
40566
40567 #define OMP_FOR_CLAUSE_MASK \
40568 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
40572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
40574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
40575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
40576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
40577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
40579
40580 static tree
40581 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
40582 char *p_name, omp_clause_mask mask, tree *cclauses,
40583 bool *if_p)
40584 {
40585 tree clauses, sb, ret;
40586 unsigned int save;
40587 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40588
40589 strcat (p_name, " for");
40590 mask |= OMP_FOR_CLAUSE_MASK;
40591 /* parallel for{, simd} disallows nowait clause, but for
40592 target {teams distribute ,}parallel for{, simd} it should be accepted. */
40593 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
40594 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
40595 /* Composite distribute parallel for{, simd} disallows ordered clause. */
40596 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
40597 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
40598
40599 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40600 {
40601 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40602 const char *p = IDENTIFIER_POINTER (id);
40603
40604 if (strcmp (p, "simd") == 0)
40605 {
40606 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40607 if (cclauses == NULL)
40608 cclauses = cclauses_buf;
40609
40610 cp_lexer_consume_token (parser->lexer);
40611 if (!flag_openmp) /* flag_openmp_simd */
40612 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40613 cclauses, if_p);
40614 sb = begin_omp_structured_block ();
40615 save = cp_parser_begin_omp_structured_block (parser);
40616 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40617 cclauses, if_p);
40618 cp_parser_end_omp_structured_block (parser, save);
40619 tree body = finish_omp_structured_block (sb);
40620 if (ret == NULL)
40621 return ret;
40622 ret = make_node (OMP_FOR);
40623 TREE_TYPE (ret) = void_type_node;
40624 OMP_FOR_BODY (ret) = body;
40625 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
40626 SET_EXPR_LOCATION (ret, loc);
40627 add_stmt (ret);
40628 return ret;
40629 }
40630 }
40631 if (!flag_openmp) /* flag_openmp_simd */
40632 {
40633 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40634 return NULL_TREE;
40635 }
40636
40637 /* Composite distribute parallel for disallows linear clause. */
40638 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
40639 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
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_FOR, mask, clauses, cclauses);
40646 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
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_FOR, clauses, cclauses, 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 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
40662 omp_clause_mask, tree *, bool *);
40663
40664 /* OpenMP 2.5:
40665 # pragma omp master new-line
40666 structured-block */
40667
40668 static tree
40669 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
40670 char *p_name, omp_clause_mask mask, tree *cclauses,
40671 bool *if_p)
40672 {
40673 tree clauses, sb, ret;
40674 unsigned int save;
40675 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40676
40677 strcat (p_name, " master");
40678
40679 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40680 {
40681 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40682 const char *p = IDENTIFIER_POINTER (id);
40683
40684 if (strcmp (p, "taskloop") == 0)
40685 {
40686 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40687 if (cclauses == NULL)
40688 cclauses = cclauses_buf;
40689
40690 cp_lexer_consume_token (parser->lexer);
40691 if (!flag_openmp) /* flag_openmp_simd */
40692 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
40693 cclauses, if_p);
40694 sb = begin_omp_structured_block ();
40695 save = cp_parser_begin_omp_structured_block (parser);
40696 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
40697 cclauses, if_p);
40698 cp_parser_end_omp_structured_block (parser, save);
40699 tree body = finish_omp_structured_block (sb);
40700 if (ret == NULL)
40701 return ret;
40702 return c_finish_omp_master (loc, body);
40703 }
40704 }
40705 if (!flag_openmp) /* flag_openmp_simd */
40706 {
40707 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40708 return NULL_TREE;
40709 }
40710
40711 if (cclauses)
40712 {
40713 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40714 false);
40715 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
40716 }
40717 else
40718 cp_parser_require_pragma_eol (parser, pragma_tok);
40719
40720 return c_finish_omp_master (loc,
40721 cp_parser_omp_structured_block (parser, if_p));
40722 }
40723
40724 /* OpenMP 2.5:
40725 # pragma omp ordered new-line
40726 structured-block
40727
40728 OpenMP 4.5:
40729 # pragma omp ordered ordered-clauses new-line
40730 structured-block */
40731
40732 #define OMP_ORDERED_CLAUSE_MASK \
40733 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
40734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
40735
40736 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
40737 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
40738
40739 static bool
40740 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
40741 enum pragma_context context, bool *if_p)
40742 {
40743 location_t loc = pragma_tok->location;
40744
40745 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40746 {
40747 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40748 const char *p = IDENTIFIER_POINTER (id);
40749
40750 if (strcmp (p, "depend") == 0)
40751 {
40752 if (!flag_openmp) /* flag_openmp_simd */
40753 {
40754 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40755 return false;
40756 }
40757 if (context == pragma_stmt)
40758 {
40759 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
40760 "%<depend%> clause may only be used in compound "
40761 "statements");
40762 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40763 return false;
40764 }
40765 tree clauses
40766 = cp_parser_omp_all_clauses (parser,
40767 OMP_ORDERED_DEPEND_CLAUSE_MASK,
40768 "#pragma omp ordered", pragma_tok);
40769 c_finish_omp_ordered (loc, clauses, NULL_TREE);
40770 return false;
40771 }
40772 }
40773
40774 tree clauses
40775 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
40776 "#pragma omp ordered", pragma_tok);
40777
40778 if (!flag_openmp /* flag_openmp_simd */
40779 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
40780 return false;
40781
40782 c_finish_omp_ordered (loc, clauses,
40783 cp_parser_omp_structured_block (parser, if_p));
40784 return true;
40785 }
40786
40787 /* OpenMP 2.5:
40788
40789 section-scope:
40790 { section-sequence }
40791
40792 section-sequence:
40793 section-directive[opt] structured-block
40794 section-sequence section-directive structured-block */
40795
40796 static tree
40797 cp_parser_omp_sections_scope (cp_parser *parser)
40798 {
40799 tree stmt, substmt;
40800 bool error_suppress = false;
40801 cp_token *tok;
40802
40803 matching_braces braces;
40804 if (!braces.require_open (parser))
40805 return NULL_TREE;
40806
40807 stmt = push_stmt_list ();
40808
40809 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
40810 != PRAGMA_OMP_SECTION)
40811 {
40812 substmt = cp_parser_omp_structured_block (parser, NULL);
40813 substmt = build1 (OMP_SECTION, void_type_node, substmt);
40814 add_stmt (substmt);
40815 }
40816
40817 while (1)
40818 {
40819 tok = cp_lexer_peek_token (parser->lexer);
40820 if (tok->type == CPP_CLOSE_BRACE)
40821 break;
40822 if (tok->type == CPP_EOF)
40823 break;
40824
40825 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
40826 {
40827 cp_lexer_consume_token (parser->lexer);
40828 cp_parser_require_pragma_eol (parser, tok);
40829 error_suppress = false;
40830 }
40831 else if (!error_suppress)
40832 {
40833 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
40834 error_suppress = true;
40835 }
40836
40837 substmt = cp_parser_omp_structured_block (parser, NULL);
40838 substmt = build1 (OMP_SECTION, void_type_node, substmt);
40839 add_stmt (substmt);
40840 }
40841 braces.require_close (parser);
40842
40843 substmt = pop_stmt_list (stmt);
40844
40845 stmt = make_node (OMP_SECTIONS);
40846 TREE_TYPE (stmt) = void_type_node;
40847 OMP_SECTIONS_BODY (stmt) = substmt;
40848
40849 add_stmt (stmt);
40850 return stmt;
40851 }
40852
40853 /* OpenMP 2.5:
40854 # pragma omp sections sections-clause[optseq] newline
40855 sections-scope */
40856
40857 #define OMP_SECTIONS_CLAUSE_MASK \
40858 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40864
40865 static tree
40866 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
40867 char *p_name, omp_clause_mask mask, tree *cclauses)
40868 {
40869 tree clauses, ret;
40870 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40871
40872 strcat (p_name, " sections");
40873 mask |= OMP_SECTIONS_CLAUSE_MASK;
40874 if (cclauses)
40875 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
40876
40877 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40878 cclauses == NULL);
40879 if (cclauses)
40880 {
40881 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
40882 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
40883 }
40884
40885 ret = cp_parser_omp_sections_scope (parser);
40886 if (ret)
40887 OMP_SECTIONS_CLAUSES (ret) = clauses;
40888
40889 return ret;
40890 }
40891
40892 /* OpenMP 2.5:
40893 # pragma omp parallel parallel-clause[optseq] new-line
40894 structured-block
40895 # pragma omp parallel for parallel-for-clause[optseq] new-line
40896 structured-block
40897 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
40898 structured-block
40899
40900 OpenMP 4.0:
40901 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
40902 structured-block */
40903
40904 #define OMP_PARALLEL_CLAUSE_MASK \
40905 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
40909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
40910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
40911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
40913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
40915
40916 static tree
40917 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
40918 char *p_name, omp_clause_mask mask, tree *cclauses,
40919 bool *if_p)
40920 {
40921 tree stmt, clauses, block;
40922 unsigned int save;
40923 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40924
40925 strcat (p_name, " parallel");
40926 mask |= OMP_PARALLEL_CLAUSE_MASK;
40927 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
40928 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
40929 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
40930 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
40931
40932 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
40933 {
40934 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40935 if (cclauses == NULL)
40936 cclauses = cclauses_buf;
40937
40938 cp_lexer_consume_token (parser->lexer);
40939 if (!flag_openmp) /* flag_openmp_simd */
40940 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
40941 if_p);
40942 block = begin_omp_parallel ();
40943 save = cp_parser_begin_omp_structured_block (parser);
40944 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
40945 if_p);
40946 cp_parser_end_omp_structured_block (parser, save);
40947 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
40948 block);
40949 if (ret == NULL_TREE)
40950 return ret;
40951 OMP_PARALLEL_COMBINED (stmt) = 1;
40952 return stmt;
40953 }
40954 /* When combined with distribute, parallel has to be followed by for.
40955 #pragma omp target parallel is allowed though. */
40956 else if (cclauses
40957 && (mask & (OMP_CLAUSE_MASK_1
40958 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
40959 {
40960 error_at (loc, "expected %<for%> after %qs", p_name);
40961 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40962 return NULL_TREE;
40963 }
40964 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40965 {
40966 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40967 const char *p = IDENTIFIER_POINTER (id);
40968 if (cclauses == NULL && strcmp (p, "master") == 0)
40969 {
40970 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40971 cclauses = cclauses_buf;
40972
40973 cp_lexer_consume_token (parser->lexer);
40974 if (!flag_openmp) /* flag_openmp_simd */
40975 return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
40976 cclauses, if_p);
40977 block = begin_omp_parallel ();
40978 save = cp_parser_begin_omp_structured_block (parser);
40979 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
40980 cclauses, if_p);
40981 cp_parser_end_omp_structured_block (parser, save);
40982 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
40983 block);
40984 if (ret == NULL_TREE)
40985 return ret;
40986 OMP_PARALLEL_COMBINED (stmt) = 1;
40987 return stmt;
40988 }
40989 else if (strcmp (p, "loop") == 0)
40990 {
40991 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40992 if (cclauses == NULL)
40993 cclauses = cclauses_buf;
40994
40995 cp_lexer_consume_token (parser->lexer);
40996 if (!flag_openmp) /* flag_openmp_simd */
40997 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
40998 cclauses, if_p);
40999 block = begin_omp_parallel ();
41000 save = cp_parser_begin_omp_structured_block (parser);
41001 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
41002 cclauses, if_p);
41003 cp_parser_end_omp_structured_block (parser, save);
41004 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
41005 block);
41006 if (ret == NULL_TREE)
41007 return ret;
41008 OMP_PARALLEL_COMBINED (stmt) = 1;
41009 return stmt;
41010 }
41011 else if (!flag_openmp) /* flag_openmp_simd */
41012 {
41013 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41014 return NULL_TREE;
41015 }
41016 else if (cclauses == NULL && strcmp (p, "sections") == 0)
41017 {
41018 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
41019 cclauses = cclauses_buf;
41020
41021 cp_lexer_consume_token (parser->lexer);
41022 block = begin_omp_parallel ();
41023 save = cp_parser_begin_omp_structured_block (parser);
41024 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
41025 cp_parser_end_omp_structured_block (parser, save);
41026 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
41027 block);
41028 OMP_PARALLEL_COMBINED (stmt) = 1;
41029 return stmt;
41030 }
41031 }
41032 else if (!flag_openmp) /* flag_openmp_simd */
41033 {
41034 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41035 return NULL_TREE;
41036 }
41037
41038 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
41039 cclauses == NULL);
41040 if (cclauses)
41041 {
41042 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
41043 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
41044 }
41045
41046 block = begin_omp_parallel ();
41047 save = cp_parser_begin_omp_structured_block (parser);
41048 cp_parser_statement (parser, NULL_TREE, false, if_p);
41049 cp_parser_end_omp_structured_block (parser, save);
41050 stmt = finish_omp_parallel (clauses, block);
41051 return stmt;
41052 }
41053
41054 /* OpenMP 2.5:
41055 # pragma omp single single-clause[optseq] new-line
41056 structured-block */
41057
41058 #define OMP_SINGLE_CLAUSE_MASK \
41059 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
41062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
41064
41065 static tree
41066 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41067 {
41068 tree stmt = make_node (OMP_SINGLE);
41069 TREE_TYPE (stmt) = void_type_node;
41070 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41071
41072 OMP_SINGLE_CLAUSES (stmt)
41073 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
41074 "#pragma omp single", pragma_tok);
41075 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
41076
41077 return add_stmt (stmt);
41078 }
41079
41080 /* OpenMP 3.0:
41081 # pragma omp task task-clause[optseq] new-line
41082 structured-block */
41083
41084 #define OMP_TASK_CLAUSE_MASK \
41085 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
41087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
41088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
41091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
41092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
41093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
41095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
41097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH))
41098
41099 static tree
41100 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41101 {
41102 tree clauses, block;
41103 unsigned int save;
41104
41105 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
41106 "#pragma omp task", pragma_tok);
41107 block = begin_omp_task ();
41108 save = cp_parser_begin_omp_structured_block (parser);
41109 cp_parser_statement (parser, NULL_TREE, false, if_p);
41110 cp_parser_end_omp_structured_block (parser, save);
41111 return finish_omp_task (clauses, block);
41112 }
41113
41114 /* OpenMP 3.0:
41115 # pragma omp taskwait new-line
41116
41117 OpenMP 5.0:
41118 # pragma omp taskwait taskwait-clause[opt] new-line */
41119
41120 #define OMP_TASKWAIT_CLAUSE_MASK \
41121 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
41122
41123 static void
41124 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
41125 {
41126 tree clauses
41127 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
41128 "#pragma omp taskwait", pragma_tok);
41129
41130 if (clauses)
41131 {
41132 tree stmt = make_node (OMP_TASK);
41133 TREE_TYPE (stmt) = void_node;
41134 OMP_TASK_CLAUSES (stmt) = clauses;
41135 OMP_TASK_BODY (stmt) = NULL_TREE;
41136 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41137 add_stmt (stmt);
41138 }
41139 else
41140 finish_omp_taskwait ();
41141 }
41142
41143 /* OpenMP 3.1:
41144 # pragma omp taskyield new-line */
41145
41146 static void
41147 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
41148 {
41149 cp_parser_require_pragma_eol (parser, pragma_tok);
41150 finish_omp_taskyield ();
41151 }
41152
41153 /* OpenMP 4.0:
41154 # pragma omp taskgroup new-line
41155 structured-block
41156
41157 OpenMP 5.0:
41158 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
41159
41160 #define OMP_TASKGROUP_CLAUSE_MASK \
41161 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
41163
41164 static tree
41165 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41166 {
41167 tree clauses
41168 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
41169 "#pragma omp taskgroup", pragma_tok);
41170 return c_finish_omp_taskgroup (input_location,
41171 cp_parser_omp_structured_block (parser,
41172 if_p),
41173 clauses);
41174 }
41175
41176
41177 /* OpenMP 2.5:
41178 # pragma omp threadprivate (variable-list) */
41179
41180 static void
41181 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
41182 {
41183 tree vars;
41184
41185 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
41186 cp_parser_require_pragma_eol (parser, pragma_tok);
41187
41188 finish_omp_threadprivate (vars);
41189 }
41190
41191 /* OpenMP 4.0:
41192 # pragma omp cancel cancel-clause[optseq] new-line */
41193
41194 #define OMP_CANCEL_CLAUSE_MASK \
41195 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
41196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
41197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
41198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
41199 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
41200
41201 static void
41202 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
41203 {
41204 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
41205 "#pragma omp cancel", pragma_tok);
41206 finish_omp_cancel (clauses);
41207 }
41208
41209 /* OpenMP 4.0:
41210 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
41211
41212 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
41213 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
41214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
41215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
41216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
41217
41218 static void
41219 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
41220 enum pragma_context context)
41221 {
41222 tree clauses;
41223 bool point_seen = false;
41224
41225 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41226 {
41227 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41228 const char *p = IDENTIFIER_POINTER (id);
41229
41230 if (strcmp (p, "point") == 0)
41231 {
41232 cp_lexer_consume_token (parser->lexer);
41233 point_seen = true;
41234 }
41235 }
41236 if (!point_seen)
41237 {
41238 cp_parser_error (parser, "expected %<point%>");
41239 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41240 return;
41241 }
41242
41243 if (context != pragma_compound)
41244 {
41245 if (context == pragma_stmt)
41246 error_at (pragma_tok->location,
41247 "%<#pragma %s%> may only be used in compound statements",
41248 "omp cancellation point");
41249 else
41250 cp_parser_error (parser, "expected declaration specifiers");
41251 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41252 return;
41253 }
41254
41255 clauses = cp_parser_omp_all_clauses (parser,
41256 OMP_CANCELLATION_POINT_CLAUSE_MASK,
41257 "#pragma omp cancellation point",
41258 pragma_tok);
41259 finish_omp_cancellation_point (clauses);
41260 }
41261
41262 /* OpenMP 4.0:
41263 #pragma omp distribute distribute-clause[optseq] new-line
41264 for-loop */
41265
41266 #define OMP_DISTRIBUTE_CLAUSE_MASK \
41267 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
41270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
41271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
41273
41274 static tree
41275 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
41276 char *p_name, omp_clause_mask mask, tree *cclauses,
41277 bool *if_p)
41278 {
41279 tree clauses, sb, ret;
41280 unsigned int save;
41281 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41282
41283 strcat (p_name, " distribute");
41284 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
41285
41286 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41287 {
41288 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41289 const char *p = IDENTIFIER_POINTER (id);
41290 bool simd = false;
41291 bool parallel = false;
41292
41293 if (strcmp (p, "simd") == 0)
41294 simd = true;
41295 else
41296 parallel = strcmp (p, "parallel") == 0;
41297 if (parallel || simd)
41298 {
41299 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
41300 if (cclauses == NULL)
41301 cclauses = cclauses_buf;
41302 cp_lexer_consume_token (parser->lexer);
41303 if (!flag_openmp) /* flag_openmp_simd */
41304 {
41305 if (simd)
41306 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
41307 cclauses, if_p);
41308 else
41309 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
41310 cclauses, if_p);
41311 }
41312 sb = begin_omp_structured_block ();
41313 save = cp_parser_begin_omp_structured_block (parser);
41314 if (simd)
41315 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
41316 cclauses, if_p);
41317 else
41318 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
41319 cclauses, if_p);
41320 cp_parser_end_omp_structured_block (parser, save);
41321 tree body = finish_omp_structured_block (sb);
41322 if (ret == NULL)
41323 return ret;
41324 ret = make_node (OMP_DISTRIBUTE);
41325 TREE_TYPE (ret) = void_type_node;
41326 OMP_FOR_BODY (ret) = body;
41327 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
41328 SET_EXPR_LOCATION (ret, loc);
41329 add_stmt (ret);
41330 return ret;
41331 }
41332 }
41333 if (!flag_openmp) /* flag_openmp_simd */
41334 {
41335 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41336 return NULL_TREE;
41337 }
41338
41339 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
41340 cclauses == NULL);
41341 if (cclauses)
41342 {
41343 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
41344 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
41345 }
41346
41347 keep_next_level (true);
41348 sb = begin_omp_structured_block ();
41349 save = cp_parser_begin_omp_structured_block (parser);
41350
41351 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
41352
41353 cp_parser_end_omp_structured_block (parser, save);
41354 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
41355
41356 return ret;
41357 }
41358
41359 /* OpenMP 4.0:
41360 # pragma omp teams teams-clause[optseq] new-line
41361 structured-block */
41362
41363 #define OMP_TEAMS_CLAUSE_MASK \
41364 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
41367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
41368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
41369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
41370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
41372
41373 static tree
41374 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
41375 char *p_name, omp_clause_mask mask, tree *cclauses,
41376 bool *if_p)
41377 {
41378 tree clauses, sb, ret;
41379 unsigned int save;
41380 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41381
41382 strcat (p_name, " teams");
41383 mask |= OMP_TEAMS_CLAUSE_MASK;
41384
41385 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41386 {
41387 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41388 const char *p = IDENTIFIER_POINTER (id);
41389 if (strcmp (p, "distribute") == 0)
41390 {
41391 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
41392 if (cclauses == NULL)
41393 cclauses = cclauses_buf;
41394
41395 cp_lexer_consume_token (parser->lexer);
41396 if (!flag_openmp) /* flag_openmp_simd */
41397 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
41398 cclauses, if_p);
41399 keep_next_level (true);
41400 sb = begin_omp_structured_block ();
41401 save = cp_parser_begin_omp_structured_block (parser);
41402 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
41403 cclauses, if_p);
41404 cp_parser_end_omp_structured_block (parser, save);
41405 tree body = finish_omp_structured_block (sb);
41406 if (ret == NULL)
41407 return ret;
41408 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
41409 ret = make_node (OMP_TEAMS);
41410 TREE_TYPE (ret) = void_type_node;
41411 OMP_TEAMS_CLAUSES (ret) = clauses;
41412 OMP_TEAMS_BODY (ret) = body;
41413 OMP_TEAMS_COMBINED (ret) = 1;
41414 SET_EXPR_LOCATION (ret, loc);
41415 return add_stmt (ret);
41416 }
41417 else if (strcmp (p, "loop") == 0)
41418 {
41419 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
41420 if (cclauses == NULL)
41421 cclauses = cclauses_buf;
41422
41423 cp_lexer_consume_token (parser->lexer);
41424 if (!flag_openmp) /* flag_openmp_simd */
41425 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
41426 cclauses, if_p);
41427 keep_next_level (true);
41428 sb = begin_omp_structured_block ();
41429 save = cp_parser_begin_omp_structured_block (parser);
41430 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
41431 cclauses, if_p);
41432 cp_parser_end_omp_structured_block (parser, save);
41433 tree body = finish_omp_structured_block (sb);
41434 if (ret == NULL)
41435 return ret;
41436 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
41437 ret = make_node (OMP_TEAMS);
41438 TREE_TYPE (ret) = void_type_node;
41439 OMP_TEAMS_CLAUSES (ret) = clauses;
41440 OMP_TEAMS_BODY (ret) = body;
41441 OMP_TEAMS_COMBINED (ret) = 1;
41442 SET_EXPR_LOCATION (ret, loc);
41443 return add_stmt (ret);
41444 }
41445 }
41446 if (!flag_openmp) /* flag_openmp_simd */
41447 {
41448 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41449 return NULL_TREE;
41450 }
41451
41452 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
41453 cclauses == NULL);
41454 if (cclauses)
41455 {
41456 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
41457 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
41458 }
41459
41460 tree stmt = make_node (OMP_TEAMS);
41461 TREE_TYPE (stmt) = void_type_node;
41462 OMP_TEAMS_CLAUSES (stmt) = clauses;
41463 keep_next_level (true);
41464 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
41465 SET_EXPR_LOCATION (stmt, loc);
41466
41467 return add_stmt (stmt);
41468 }
41469
41470 /* OpenMP 4.0:
41471 # pragma omp target data target-data-clause[optseq] new-line
41472 structured-block */
41473
41474 #define OMP_TARGET_DATA_CLAUSE_MASK \
41475 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
41477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
41479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
41480
41481 static tree
41482 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41483 {
41484 tree clauses
41485 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
41486 "#pragma omp target data", pragma_tok);
41487 c_omp_adjust_map_clauses (clauses, false);
41488 int map_seen = 0;
41489 for (tree *pc = &clauses; *pc;)
41490 {
41491 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
41492 switch (OMP_CLAUSE_MAP_KIND (*pc))
41493 {
41494 case GOMP_MAP_TO:
41495 case GOMP_MAP_ALWAYS_TO:
41496 case GOMP_MAP_FROM:
41497 case GOMP_MAP_ALWAYS_FROM:
41498 case GOMP_MAP_TOFROM:
41499 case GOMP_MAP_ALWAYS_TOFROM:
41500 case GOMP_MAP_ALLOC:
41501 map_seen = 3;
41502 break;
41503 case GOMP_MAP_FIRSTPRIVATE_POINTER:
41504 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
41505 case GOMP_MAP_ALWAYS_POINTER:
41506 case GOMP_MAP_ATTACH_DETACH:
41507 break;
41508 default:
41509 map_seen |= 1;
41510 error_at (OMP_CLAUSE_LOCATION (*pc),
41511 "%<#pragma omp target data%> with map-type other "
41512 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
41513 "on %<map%> clause");
41514 *pc = OMP_CLAUSE_CHAIN (*pc);
41515 continue;
41516 }
41517 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
41518 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
41519 map_seen = 3;
41520 pc = &OMP_CLAUSE_CHAIN (*pc);
41521 }
41522
41523 if (map_seen != 3)
41524 {
41525 if (map_seen == 0)
41526 error_at (pragma_tok->location,
41527 "%<#pragma omp target data%> must contain at least "
41528 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
41529 "clause");
41530 return NULL_TREE;
41531 }
41532
41533 tree stmt = make_node (OMP_TARGET_DATA);
41534 TREE_TYPE (stmt) = void_type_node;
41535 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
41536
41537 keep_next_level (true);
41538 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
41539
41540 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41541 return add_stmt (stmt);
41542 }
41543
41544 /* OpenMP 4.5:
41545 # pragma omp target enter data target-enter-data-clause[optseq] new-line
41546 structured-block */
41547
41548 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
41549 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
41551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
41554
41555 static tree
41556 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
41557 enum pragma_context context)
41558 {
41559 bool data_seen = false;
41560 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41561 {
41562 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41563 const char *p = IDENTIFIER_POINTER (id);
41564
41565 if (strcmp (p, "data") == 0)
41566 {
41567 cp_lexer_consume_token (parser->lexer);
41568 data_seen = true;
41569 }
41570 }
41571 if (!data_seen)
41572 {
41573 cp_parser_error (parser, "expected %<data%>");
41574 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41575 return NULL_TREE;
41576 }
41577
41578 if (context == pragma_stmt)
41579 {
41580 error_at (pragma_tok->location,
41581 "%<#pragma %s%> may only be used in compound statements",
41582 "omp target enter data");
41583 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41584 return NULL_TREE;
41585 }
41586
41587 tree clauses
41588 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
41589 "#pragma omp target enter data", pragma_tok);
41590 c_omp_adjust_map_clauses (clauses, false);
41591 int map_seen = 0;
41592 for (tree *pc = &clauses; *pc;)
41593 {
41594 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
41595 switch (OMP_CLAUSE_MAP_KIND (*pc))
41596 {
41597 case GOMP_MAP_TO:
41598 case GOMP_MAP_ALWAYS_TO:
41599 case GOMP_MAP_ALLOC:
41600 map_seen = 3;
41601 break;
41602 case GOMP_MAP_FIRSTPRIVATE_POINTER:
41603 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
41604 case GOMP_MAP_ALWAYS_POINTER:
41605 case GOMP_MAP_ATTACH_DETACH:
41606 break;
41607 default:
41608 map_seen |= 1;
41609 error_at (OMP_CLAUSE_LOCATION (*pc),
41610 "%<#pragma omp target enter data%> with map-type other "
41611 "than %<to%> or %<alloc%> on %<map%> clause");
41612 *pc = OMP_CLAUSE_CHAIN (*pc);
41613 continue;
41614 }
41615 pc = &OMP_CLAUSE_CHAIN (*pc);
41616 }
41617
41618 if (map_seen != 3)
41619 {
41620 if (map_seen == 0)
41621 error_at (pragma_tok->location,
41622 "%<#pragma omp target enter data%> must contain at least "
41623 "one %<map%> clause");
41624 return NULL_TREE;
41625 }
41626
41627 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
41628 TREE_TYPE (stmt) = void_type_node;
41629 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
41630 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41631 return add_stmt (stmt);
41632 }
41633
41634 /* OpenMP 4.5:
41635 # pragma omp target exit data target-enter-data-clause[optseq] new-line
41636 structured-block */
41637
41638 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
41639 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
41641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
41644
41645 static tree
41646 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
41647 enum pragma_context context)
41648 {
41649 bool data_seen = false;
41650 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41651 {
41652 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41653 const char *p = IDENTIFIER_POINTER (id);
41654
41655 if (strcmp (p, "data") == 0)
41656 {
41657 cp_lexer_consume_token (parser->lexer);
41658 data_seen = true;
41659 }
41660 }
41661 if (!data_seen)
41662 {
41663 cp_parser_error (parser, "expected %<data%>");
41664 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41665 return NULL_TREE;
41666 }
41667
41668 if (context == pragma_stmt)
41669 {
41670 error_at (pragma_tok->location,
41671 "%<#pragma %s%> may only be used in compound statements",
41672 "omp target exit data");
41673 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41674 return NULL_TREE;
41675 }
41676
41677 tree clauses
41678 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
41679 "#pragma omp target exit data", pragma_tok);
41680 c_omp_adjust_map_clauses (clauses, false);
41681 int map_seen = 0;
41682 for (tree *pc = &clauses; *pc;)
41683 {
41684 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
41685 switch (OMP_CLAUSE_MAP_KIND (*pc))
41686 {
41687 case GOMP_MAP_FROM:
41688 case GOMP_MAP_ALWAYS_FROM:
41689 case GOMP_MAP_RELEASE:
41690 case GOMP_MAP_DELETE:
41691 map_seen = 3;
41692 break;
41693 case GOMP_MAP_FIRSTPRIVATE_POINTER:
41694 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
41695 case GOMP_MAP_ALWAYS_POINTER:
41696 case GOMP_MAP_ATTACH_DETACH:
41697 break;
41698 default:
41699 map_seen |= 1;
41700 error_at (OMP_CLAUSE_LOCATION (*pc),
41701 "%<#pragma omp target exit data%> with map-type other "
41702 "than %<from%>, %<release%> or %<delete%> on %<map%>"
41703 " clause");
41704 *pc = OMP_CLAUSE_CHAIN (*pc);
41705 continue;
41706 }
41707 pc = &OMP_CLAUSE_CHAIN (*pc);
41708 }
41709
41710 if (map_seen != 3)
41711 {
41712 if (map_seen == 0)
41713 error_at (pragma_tok->location,
41714 "%<#pragma omp target exit data%> must contain at least "
41715 "one %<map%> clause");
41716 return NULL_TREE;
41717 }
41718
41719 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
41720 TREE_TYPE (stmt) = void_type_node;
41721 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
41722 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41723 return add_stmt (stmt);
41724 }
41725
41726 /* OpenMP 4.0:
41727 # pragma omp target update target-update-clause[optseq] new-line */
41728
41729 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
41730 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
41731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
41732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
41736
41737 static bool
41738 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
41739 enum pragma_context context)
41740 {
41741 if (context == pragma_stmt)
41742 {
41743 error_at (pragma_tok->location,
41744 "%<#pragma %s%> may only be used in compound statements",
41745 "omp target update");
41746 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41747 return false;
41748 }
41749
41750 tree clauses
41751 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
41752 "#pragma omp target update", pragma_tok);
41753 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
41754 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
41755 {
41756 error_at (pragma_tok->location,
41757 "%<#pragma omp target update%> must contain at least one "
41758 "%<from%> or %<to%> clauses");
41759 return false;
41760 }
41761
41762 tree stmt = make_node (OMP_TARGET_UPDATE);
41763 TREE_TYPE (stmt) = void_type_node;
41764 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
41765 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41766 add_stmt (stmt);
41767 return false;
41768 }
41769
41770 /* OpenMP 4.0:
41771 # pragma omp target target-clause[optseq] new-line
41772 structured-block */
41773
41774 #define OMP_TARGET_CLAUSE_MASK \
41775 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
41777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
41780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
41783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
41785
41786 static bool
41787 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
41788 enum pragma_context context, bool *if_p)
41789 {
41790 tree *pc = NULL, stmt;
41791
41792 if (flag_openmp)
41793 omp_requires_mask
41794 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
41795
41796 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41797 {
41798 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41799 const char *p = IDENTIFIER_POINTER (id);
41800 enum tree_code ccode = ERROR_MARK;
41801
41802 if (strcmp (p, "teams") == 0)
41803 ccode = OMP_TEAMS;
41804 else if (strcmp (p, "parallel") == 0)
41805 ccode = OMP_PARALLEL;
41806 else if (strcmp (p, "simd") == 0)
41807 ccode = OMP_SIMD;
41808 if (ccode != ERROR_MARK)
41809 {
41810 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
41811 char p_name[sizeof ("#pragma omp target teams distribute "
41812 "parallel for simd")];
41813
41814 cp_lexer_consume_token (parser->lexer);
41815 strcpy (p_name, "#pragma omp target");
41816 if (!flag_openmp) /* flag_openmp_simd */
41817 {
41818 tree stmt;
41819 switch (ccode)
41820 {
41821 case OMP_TEAMS:
41822 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
41823 OMP_TARGET_CLAUSE_MASK,
41824 cclauses, if_p);
41825 break;
41826 case OMP_PARALLEL:
41827 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
41828 OMP_TARGET_CLAUSE_MASK,
41829 cclauses, if_p);
41830 break;
41831 case OMP_SIMD:
41832 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
41833 OMP_TARGET_CLAUSE_MASK,
41834 cclauses, if_p);
41835 break;
41836 default:
41837 gcc_unreachable ();
41838 }
41839 return stmt != NULL_TREE;
41840 }
41841 keep_next_level (true);
41842 tree sb = begin_omp_structured_block (), ret;
41843 unsigned save = cp_parser_begin_omp_structured_block (parser);
41844 switch (ccode)
41845 {
41846 case OMP_TEAMS:
41847 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
41848 OMP_TARGET_CLAUSE_MASK, cclauses,
41849 if_p);
41850 break;
41851 case OMP_PARALLEL:
41852 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
41853 OMP_TARGET_CLAUSE_MASK, cclauses,
41854 if_p);
41855 break;
41856 case OMP_SIMD:
41857 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
41858 OMP_TARGET_CLAUSE_MASK, cclauses,
41859 if_p);
41860 break;
41861 default:
41862 gcc_unreachable ();
41863 }
41864 cp_parser_end_omp_structured_block (parser, save);
41865 tree body = finish_omp_structured_block (sb);
41866 if (ret == NULL_TREE)
41867 return false;
41868 if (ccode == OMP_TEAMS && !processing_template_decl)
41869 {
41870 /* For combined target teams, ensure the num_teams and
41871 thread_limit clause expressions are evaluated on the host,
41872 before entering the target construct. */
41873 tree c;
41874 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
41875 c; c = OMP_CLAUSE_CHAIN (c))
41876 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
41877 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
41878 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
41879 {
41880 tree expr = OMP_CLAUSE_OPERAND (c, 0);
41881 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
41882 if (expr == error_mark_node)
41883 continue;
41884 tree tmp = TARGET_EXPR_SLOT (expr);
41885 add_stmt (expr);
41886 OMP_CLAUSE_OPERAND (c, 0) = expr;
41887 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
41888 OMP_CLAUSE_FIRSTPRIVATE);
41889 OMP_CLAUSE_DECL (tc) = tmp;
41890 OMP_CLAUSE_CHAIN (tc)
41891 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
41892 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
41893 }
41894 }
41895 tree stmt = make_node (OMP_TARGET);
41896 TREE_TYPE (stmt) = void_type_node;
41897 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
41898 OMP_TARGET_BODY (stmt) = body;
41899 OMP_TARGET_COMBINED (stmt) = 1;
41900 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41901 add_stmt (stmt);
41902 pc = &OMP_TARGET_CLAUSES (stmt);
41903 goto check_clauses;
41904 }
41905 else if (!flag_openmp) /* flag_openmp_simd */
41906 {
41907 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41908 return false;
41909 }
41910 else if (strcmp (p, "data") == 0)
41911 {
41912 cp_lexer_consume_token (parser->lexer);
41913 cp_parser_omp_target_data (parser, pragma_tok, if_p);
41914 return true;
41915 }
41916 else if (strcmp (p, "enter") == 0)
41917 {
41918 cp_lexer_consume_token (parser->lexer);
41919 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
41920 return false;
41921 }
41922 else if (strcmp (p, "exit") == 0)
41923 {
41924 cp_lexer_consume_token (parser->lexer);
41925 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
41926 return false;
41927 }
41928 else if (strcmp (p, "update") == 0)
41929 {
41930 cp_lexer_consume_token (parser->lexer);
41931 return cp_parser_omp_target_update (parser, pragma_tok, context);
41932 }
41933 }
41934 if (!flag_openmp) /* flag_openmp_simd */
41935 {
41936 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41937 return false;
41938 }
41939
41940 stmt = make_node (OMP_TARGET);
41941 TREE_TYPE (stmt) = void_type_node;
41942
41943 OMP_TARGET_CLAUSES (stmt)
41944 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
41945 "#pragma omp target", pragma_tok);
41946 c_omp_adjust_map_clauses (OMP_TARGET_CLAUSES (stmt), true);
41947
41948 pc = &OMP_TARGET_CLAUSES (stmt);
41949 keep_next_level (true);
41950 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
41951
41952 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41953 add_stmt (stmt);
41954
41955 check_clauses:
41956 while (*pc)
41957 {
41958 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
41959 switch (OMP_CLAUSE_MAP_KIND (*pc))
41960 {
41961 case GOMP_MAP_TO:
41962 case GOMP_MAP_ALWAYS_TO:
41963 case GOMP_MAP_FROM:
41964 case GOMP_MAP_ALWAYS_FROM:
41965 case GOMP_MAP_TOFROM:
41966 case GOMP_MAP_ALWAYS_TOFROM:
41967 case GOMP_MAP_ALLOC:
41968 case GOMP_MAP_FIRSTPRIVATE_POINTER:
41969 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
41970 case GOMP_MAP_ALWAYS_POINTER:
41971 case GOMP_MAP_ATTACH_DETACH:
41972 break;
41973 default:
41974 error_at (OMP_CLAUSE_LOCATION (*pc),
41975 "%<#pragma omp target%> with map-type other "
41976 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
41977 "on %<map%> clause");
41978 *pc = OMP_CLAUSE_CHAIN (*pc);
41979 continue;
41980 }
41981 pc = &OMP_CLAUSE_CHAIN (*pc);
41982 }
41983 return true;
41984 }
41985
41986 /* OpenACC 2.0:
41987 # pragma acc cache (variable-list) new-line
41988 */
41989
41990 static tree
41991 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
41992 {
41993 /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
41994 clauses. */
41995 auto_suppress_location_wrappers sentinel;
41996
41997 tree stmt, clauses;
41998
41999 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
42000 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
42001
42002 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
42003
42004 stmt = make_node (OACC_CACHE);
42005 TREE_TYPE (stmt) = void_type_node;
42006 OACC_CACHE_CLAUSES (stmt) = clauses;
42007 SET_EXPR_LOCATION (stmt, pragma_tok->location);
42008 add_stmt (stmt);
42009
42010 return stmt;
42011 }
42012
42013 /* OpenACC 2.0:
42014 # pragma acc data oacc-data-clause[optseq] new-line
42015 structured-block */
42016
42017 #define OACC_DATA_CLAUSE_MASK \
42018 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
42019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
42020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
42024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
42025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
42027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
42028
42029 static tree
42030 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
42031 {
42032 tree stmt, clauses, block;
42033 unsigned int save;
42034
42035 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
42036 "#pragma acc data", pragma_tok);
42037
42038 block = begin_omp_parallel ();
42039 save = cp_parser_begin_omp_structured_block (parser);
42040 cp_parser_statement (parser, NULL_TREE, false, if_p);
42041 cp_parser_end_omp_structured_block (parser, save);
42042 stmt = finish_oacc_data (clauses, block);
42043 return stmt;
42044 }
42045
42046 /* OpenACC 2.0:
42047 # pragma acc host_data <clauses> new-line
42048 structured-block */
42049
42050 #define OACC_HOST_DATA_CLAUSE_MASK \
42051 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
42052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
42054
42055 static tree
42056 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
42057 {
42058 tree stmt, clauses, block;
42059 unsigned int save;
42060
42061 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
42062 "#pragma acc host_data", pragma_tok);
42063
42064 block = begin_omp_parallel ();
42065 save = cp_parser_begin_omp_structured_block (parser);
42066 cp_parser_statement (parser, NULL_TREE, false, if_p);
42067 cp_parser_end_omp_structured_block (parser, save);
42068 stmt = finish_oacc_host_data (clauses, block);
42069 return stmt;
42070 }
42071
42072 /* OpenACC 2.0:
42073 # pragma acc declare oacc-data-clause[optseq] new-line
42074 */
42075
42076 #define OACC_DECLARE_CLAUSE_MASK \
42077 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
42078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
42082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
42083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
42084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
42085
42086 static tree
42087 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
42088 {
42089 tree clauses, stmt;
42090 bool error = false;
42091 bool found_in_scope = global_bindings_p ();
42092
42093 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
42094 "#pragma acc declare", pragma_tok, true);
42095
42096
42097 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
42098 {
42099 error_at (pragma_tok->location,
42100 "no valid clauses specified in %<#pragma acc declare%>");
42101 return NULL_TREE;
42102 }
42103
42104 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
42105 {
42106 location_t loc = OMP_CLAUSE_LOCATION (t);
42107 tree decl = OMP_CLAUSE_DECL (t);
42108 if (!DECL_P (decl))
42109 {
42110 error_at (loc, "array section in %<#pragma acc declare%>");
42111 error = true;
42112 continue;
42113 }
42114 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
42115 switch (OMP_CLAUSE_MAP_KIND (t))
42116 {
42117 case GOMP_MAP_FIRSTPRIVATE_POINTER:
42118 case GOMP_MAP_ALLOC:
42119 case GOMP_MAP_TO:
42120 case GOMP_MAP_FORCE_DEVICEPTR:
42121 case GOMP_MAP_DEVICE_RESIDENT:
42122 break;
42123
42124 case GOMP_MAP_LINK:
42125 if (!global_bindings_p ()
42126 && (TREE_STATIC (decl)
42127 || !DECL_EXTERNAL (decl)))
42128 {
42129 error_at (loc,
42130 "%qD must be a global variable in "
42131 "%<#pragma acc declare link%>",
42132 decl);
42133 error = true;
42134 continue;
42135 }
42136 break;
42137
42138 default:
42139 if (global_bindings_p ())
42140 {
42141 error_at (loc, "invalid OpenACC clause at file scope");
42142 error = true;
42143 continue;
42144 }
42145 if (DECL_EXTERNAL (decl))
42146 {
42147 error_at (loc,
42148 "invalid use of %<extern%> variable %qD "
42149 "in %<#pragma acc declare%>", decl);
42150 error = true;
42151 continue;
42152 }
42153 else if (TREE_PUBLIC (decl))
42154 {
42155 error_at (loc,
42156 "invalid use of %<global%> variable %qD "
42157 "in %<#pragma acc declare%>", decl);
42158 error = true;
42159 continue;
42160 }
42161 break;
42162 }
42163
42164 if (!found_in_scope)
42165 /* This seems to ignore the existence of cleanup scopes?
42166 What is the meaning for local extern decls? The local
42167 extern is in this scope, but it is referring to a decl that
42168 is namespace scope. */
42169 for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
42170 if (d == decl)
42171 {
42172 found_in_scope = true;
42173 break;
42174 }
42175 if (!found_in_scope)
42176 {
42177 error_at (loc,
42178 "%qD must be a variable declared in the same scope as "
42179 "%<#pragma acc declare%>", decl);
42180 error = true;
42181 continue;
42182 }
42183
42184 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
42185 || lookup_attribute ("omp declare target link",
42186 DECL_ATTRIBUTES (decl)))
42187 {
42188 error_at (loc, "variable %qD used more than once with "
42189 "%<#pragma acc declare%>", decl);
42190 error = true;
42191 continue;
42192 }
42193
42194 if (!error)
42195 {
42196 tree id;
42197
42198 if (DECL_LOCAL_DECL_P (decl))
42199 /* We need to mark the aliased decl, as that is the entity
42200 that is being referred to. This won't work for
42201 dependent variables, but it didn't work for them before
42202 DECL_LOCAL_DECL_P was a thing either. But then
42203 dependent local extern variable decls are as rare as
42204 hen's teeth. */
42205 if (auto alias = DECL_LOCAL_DECL_ALIAS (decl))
42206 decl = alias;
42207
42208 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
42209 id = get_identifier ("omp declare target link");
42210 else
42211 id = get_identifier ("omp declare target");
42212
42213 DECL_ATTRIBUTES (decl)
42214 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
42215 if (current_binding_level->kind == sk_namespace)
42216 {
42217 symtab_node *node = symtab_node::get (decl);
42218 if (node != NULL)
42219 {
42220 node->offloadable = 1;
42221 if (ENABLE_OFFLOADING)
42222 {
42223 g->have_offload = true;
42224 if (is_a <varpool_node *> (node))
42225 vec_safe_push (offload_vars, decl);
42226 }
42227 }
42228 }
42229 }
42230 }
42231
42232 if (error || current_binding_level->kind == sk_namespace)
42233 return NULL_TREE;
42234
42235 stmt = make_node (OACC_DECLARE);
42236 TREE_TYPE (stmt) = void_type_node;
42237 OACC_DECLARE_CLAUSES (stmt) = clauses;
42238 SET_EXPR_LOCATION (stmt, pragma_tok->location);
42239
42240 add_stmt (stmt);
42241
42242 return NULL_TREE;
42243 }
42244
42245 /* OpenACC 2.0:
42246 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
42247
42248 or
42249
42250 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
42251
42252 LOC is the location of the #pragma token.
42253 */
42254
42255 #define OACC_ENTER_DATA_CLAUSE_MASK \
42256 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
42258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
42262
42263 #define OACC_EXIT_DATA_CLAUSE_MASK \
42264 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
42268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
42269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
42270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
42271
42272 static tree
42273 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
42274 bool enter)
42275 {
42276 location_t loc = pragma_tok->location;
42277 tree stmt, clauses;
42278 const char *p = "";
42279
42280 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42281 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
42282
42283 if (strcmp (p, "data") != 0)
42284 {
42285 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
42286 enter ? "enter" : "exit");
42287 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42288 return NULL_TREE;
42289 }
42290
42291 cp_lexer_consume_token (parser->lexer);
42292
42293 if (enter)
42294 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
42295 "#pragma acc enter data", pragma_tok);
42296 else
42297 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
42298 "#pragma acc exit data", pragma_tok);
42299
42300 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
42301 {
42302 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
42303 enter ? "enter" : "exit");
42304 return NULL_TREE;
42305 }
42306
42307 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
42308 TREE_TYPE (stmt) = void_type_node;
42309 OMP_STANDALONE_CLAUSES (stmt) = clauses;
42310 SET_EXPR_LOCATION (stmt, loc);
42311 add_stmt (stmt);
42312 return stmt;
42313 }
42314
42315 /* OpenACC 2.0:
42316 # pragma acc loop oacc-loop-clause[optseq] new-line
42317 structured-block */
42318
42319 #define OACC_LOOP_CLAUSE_MASK \
42320 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
42321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
42322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
42323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
42324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
42325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
42326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
42327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
42328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
42329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
42330
42331 static tree
42332 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
42333 omp_clause_mask mask, tree *cclauses, bool *if_p)
42334 {
42335 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
42336
42337 strcat (p_name, " loop");
42338 mask |= OACC_LOOP_CLAUSE_MASK;
42339
42340 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
42341 cclauses == NULL);
42342 if (cclauses)
42343 {
42344 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
42345 if (*cclauses)
42346 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
42347 if (clauses)
42348 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
42349 }
42350
42351 tree block = begin_omp_structured_block ();
42352 int save = cp_parser_begin_omp_structured_block (parser);
42353 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
42354 cp_parser_end_omp_structured_block (parser, save);
42355 add_stmt (finish_omp_structured_block (block));
42356
42357 return stmt;
42358 }
42359
42360 /* OpenACC 2.0:
42361 # pragma acc kernels oacc-kernels-clause[optseq] new-line
42362 structured-block
42363
42364 or
42365
42366 # pragma acc parallel oacc-parallel-clause[optseq] new-line
42367 structured-block
42368
42369 OpenACC 2.6:
42370
42371 # pragma acc serial oacc-serial-clause[optseq] new-line
42372 */
42373
42374 #define OACC_KERNELS_CLAUSE_MASK \
42375 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
42377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
42378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
42382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
42383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
42385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
42386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
42387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
42388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
42389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
42390
42391 #define OACC_PARALLEL_CLAUSE_MASK \
42392 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
42394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
42395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
42399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
42400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
42401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
42403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
42404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
42405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
42406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
42407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
42408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
42409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
42410
42411 #define OACC_SERIAL_CLAUSE_MASK \
42412 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
42414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
42415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
42419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
42420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
42422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
42423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
42424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
42425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
42426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
42427
42428 static tree
42429 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
42430 char *p_name, bool *if_p)
42431 {
42432 omp_clause_mask mask;
42433 enum tree_code code;
42434 switch (cp_parser_pragma_kind (pragma_tok))
42435 {
42436 case PRAGMA_OACC_KERNELS:
42437 strcat (p_name, " kernels");
42438 mask = OACC_KERNELS_CLAUSE_MASK;
42439 code = OACC_KERNELS;
42440 break;
42441 case PRAGMA_OACC_PARALLEL:
42442 strcat (p_name, " parallel");
42443 mask = OACC_PARALLEL_CLAUSE_MASK;
42444 code = OACC_PARALLEL;
42445 break;
42446 case PRAGMA_OACC_SERIAL:
42447 strcat (p_name, " serial");
42448 mask = OACC_SERIAL_CLAUSE_MASK;
42449 code = OACC_SERIAL;
42450 break;
42451 default:
42452 gcc_unreachable ();
42453 }
42454
42455 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42456 {
42457 const char *p
42458 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
42459 if (strcmp (p, "loop") == 0)
42460 {
42461 cp_lexer_consume_token (parser->lexer);
42462 tree block = begin_omp_parallel ();
42463 tree clauses;
42464 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
42465 &clauses, if_p);
42466 protected_set_expr_location (stmt, pragma_tok->location);
42467 return finish_omp_construct (code, block, clauses);
42468 }
42469 }
42470
42471 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
42472
42473 tree block = begin_omp_parallel ();
42474 unsigned int save = cp_parser_begin_omp_structured_block (parser);
42475 cp_parser_statement (parser, NULL_TREE, false, if_p);
42476 cp_parser_end_omp_structured_block (parser, save);
42477 return finish_omp_construct (code, block, clauses);
42478 }
42479
42480 /* OpenACC 2.0:
42481 # pragma acc update oacc-update-clause[optseq] new-line
42482 */
42483
42484 #define OACC_UPDATE_CLAUSE_MASK \
42485 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
42487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
42488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
42490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
42491
42492 static tree
42493 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
42494 {
42495 tree stmt, clauses;
42496
42497 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
42498 "#pragma acc update", pragma_tok);
42499
42500 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
42501 {
42502 error_at (pragma_tok->location,
42503 "%<#pragma acc update%> must contain at least one "
42504 "%<device%> or %<host%> or %<self%> clause");
42505 return NULL_TREE;
42506 }
42507
42508 stmt = make_node (OACC_UPDATE);
42509 TREE_TYPE (stmt) = void_type_node;
42510 OACC_UPDATE_CLAUSES (stmt) = clauses;
42511 SET_EXPR_LOCATION (stmt, pragma_tok->location);
42512 add_stmt (stmt);
42513 return stmt;
42514 }
42515
42516 /* OpenACC 2.0:
42517 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
42518
42519 LOC is the location of the #pragma token.
42520 */
42521
42522 #define OACC_WAIT_CLAUSE_MASK \
42523 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
42524
42525 static tree
42526 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
42527 {
42528 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
42529 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42530
42531 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
42532 list = cp_parser_oacc_wait_list (parser, loc, list);
42533
42534 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
42535 "#pragma acc wait", pragma_tok);
42536
42537 stmt = c_finish_oacc_wait (loc, list, clauses);
42538 stmt = finish_expr_stmt (stmt);
42539
42540 return stmt;
42541 }
42542
42543 /* OpenMP 4.0:
42544 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
42545
42546 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
42547 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
42548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
42549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
42550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
42551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
42552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
42553
42554 static void
42555 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
42556 enum pragma_context context,
42557 bool variant_p)
42558 {
42559 bool first_p = parser->omp_declare_simd == NULL;
42560 cp_omp_declare_simd_data data;
42561 if (first_p)
42562 {
42563 data.error_seen = false;
42564 data.fndecl_seen = false;
42565 data.variant_p = variant_p;
42566 data.tokens = vNULL;
42567 data.clauses = NULL_TREE;
42568 /* It is safe to take the address of a local variable; it will only be
42569 used while this scope is live. */
42570 parser->omp_declare_simd = &data;
42571 }
42572 else if (parser->omp_declare_simd->variant_p != variant_p)
42573 {
42574 error_at (pragma_tok->location,
42575 "%<#pragma omp declare %s%> followed by "
42576 "%<#pragma omp declare %s%>",
42577 parser->omp_declare_simd->variant_p ? "variant" : "simd",
42578 parser->omp_declare_simd->variant_p ? "simd" : "variant");
42579 parser->omp_declare_simd->error_seen = true;
42580 }
42581
42582 /* Store away all pragma tokens. */
42583 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42584 cp_lexer_consume_token (parser->lexer);
42585 cp_parser_require_pragma_eol (parser, pragma_tok);
42586 struct cp_token_cache *cp
42587 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
42588 parser->omp_declare_simd->tokens.safe_push (cp);
42589
42590 if (first_p)
42591 {
42592 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
42593 cp_parser_pragma (parser, context, NULL);
42594 switch (context)
42595 {
42596 case pragma_external:
42597 cp_parser_declaration (parser, NULL_TREE);
42598 break;
42599 case pragma_member:
42600 cp_parser_member_declaration (parser);
42601 break;
42602 case pragma_objc_icode:
42603 cp_parser_block_declaration (parser, /*statement_p=*/false);
42604 break;
42605 default:
42606 cp_parser_declaration_statement (parser);
42607 break;
42608 }
42609 if (parser->omp_declare_simd
42610 && !parser->omp_declare_simd->error_seen
42611 && !parser->omp_declare_simd->fndecl_seen)
42612 error_at (pragma_tok->location,
42613 "%<#pragma omp declare %s%> not immediately followed by "
42614 "function declaration or definition",
42615 parser->omp_declare_simd->variant_p ? "variant" : "simd");
42616 data.tokens.release ();
42617 parser->omp_declare_simd = NULL;
42618 }
42619 }
42620
42621 static const char *const omp_construct_selectors[] = {
42622 "simd", "target", "teams", "parallel", "for", NULL };
42623 static const char *const omp_device_selectors[] = {
42624 "kind", "isa", "arch", NULL };
42625 static const char *const omp_implementation_selectors[] = {
42626 "vendor", "extension", "atomic_default_mem_order", "unified_address",
42627 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
42628 static const char *const omp_user_selectors[] = {
42629 "condition", NULL };
42630
42631 /* OpenMP 5.0:
42632
42633 trait-selector:
42634 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
42635
42636 trait-score:
42637 score(score-expression) */
42638
42639 static tree
42640 cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
42641 {
42642 tree ret = NULL_TREE;
42643 do
42644 {
42645 tree selector;
42646 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
42647 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42648 selector = cp_lexer_peek_token (parser->lexer)->u.value;
42649 else
42650 {
42651 cp_parser_error (parser, "expected trait selector name");
42652 return error_mark_node;
42653 }
42654
42655 tree properties = NULL_TREE;
42656 const char *const *selectors = NULL;
42657 bool allow_score = true;
42658 bool allow_user = false;
42659 int property_limit = 0;
42660 enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
42661 CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
42662 CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
42663 switch (IDENTIFIER_POINTER (set)[0])
42664 {
42665 case 'c': /* construct */
42666 selectors = omp_construct_selectors;
42667 allow_score = false;
42668 property_limit = 1;
42669 property_kind = CTX_PROPERTY_SIMD;
42670 break;
42671 case 'd': /* device */
42672 selectors = omp_device_selectors;
42673 allow_score = false;
42674 allow_user = true;
42675 property_limit = 3;
42676 property_kind = CTX_PROPERTY_NAME_LIST;
42677 break;
42678 case 'i': /* implementation */
42679 selectors = omp_implementation_selectors;
42680 allow_user = true;
42681 property_limit = 3;
42682 property_kind = CTX_PROPERTY_NAME_LIST;
42683 break;
42684 case 'u': /* user */
42685 selectors = omp_user_selectors;
42686 property_limit = 1;
42687 property_kind = CTX_PROPERTY_EXPR;
42688 break;
42689 default:
42690 gcc_unreachable ();
42691 }
42692 for (int i = 0; ; i++)
42693 {
42694 if (selectors[i] == NULL)
42695 {
42696 if (allow_user)
42697 {
42698 property_kind = CTX_PROPERTY_USER;
42699 break;
42700 }
42701 else
42702 {
42703 error ("selector %qs not allowed for context selector "
42704 "set %qs", IDENTIFIER_POINTER (selector),
42705 IDENTIFIER_POINTER (set));
42706 cp_lexer_consume_token (parser->lexer);
42707 return error_mark_node;
42708 }
42709 }
42710 if (i == property_limit)
42711 property_kind = CTX_PROPERTY_NONE;
42712 if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
42713 break;
42714 }
42715 if (property_kind == CTX_PROPERTY_NAME_LIST
42716 && IDENTIFIER_POINTER (set)[0] == 'i'
42717 && strcmp (IDENTIFIER_POINTER (selector),
42718 "atomic_default_mem_order") == 0)
42719 property_kind = CTX_PROPERTY_ID;
42720
42721 cp_lexer_consume_token (parser->lexer);
42722
42723 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
42724 {
42725 if (property_kind == CTX_PROPERTY_NONE)
42726 {
42727 error ("selector %qs does not accept any properties",
42728 IDENTIFIER_POINTER (selector));
42729 return error_mark_node;
42730 }
42731
42732 matching_parens parens;
42733 parens.consume_open (parser);
42734
42735 cp_token *token = cp_lexer_peek_token (parser->lexer);
42736 if (allow_score
42737 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
42738 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
42739 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
42740 {
42741 cp_lexer_save_tokens (parser->lexer);
42742 cp_lexer_consume_token (parser->lexer);
42743 cp_lexer_consume_token (parser->lexer);
42744 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
42745 true)
42746 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
42747 {
42748 cp_lexer_rollback_tokens (parser->lexer);
42749 cp_lexer_consume_token (parser->lexer);
42750
42751 matching_parens parens2;
42752 parens2.require_open (parser);
42753 tree score = cp_parser_constant_expression (parser);
42754 if (!parens2.require_close (parser))
42755 cp_parser_skip_to_closing_parenthesis (parser, true,
42756 false, true);
42757 cp_parser_require (parser, CPP_COLON, RT_COLON);
42758 if (score != error_mark_node)
42759 {
42760 score = fold_non_dependent_expr (score);
42761 if (value_dependent_expression_p (score))
42762 properties = tree_cons (get_identifier (" score"),
42763 score, properties);
42764 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
42765 || TREE_CODE (score) != INTEGER_CST)
42766 error_at (token->location, "score argument must be "
42767 "constant integer expression");
42768 else if (tree_int_cst_sgn (score) < 0)
42769 error_at (token->location, "score argument must be "
42770 "non-negative");
42771 else
42772 properties = tree_cons (get_identifier (" score"),
42773 score, properties);
42774 }
42775 }
42776 else
42777 cp_lexer_rollback_tokens (parser->lexer);
42778
42779 token = cp_lexer_peek_token (parser->lexer);
42780 }
42781
42782 switch (property_kind)
42783 {
42784 tree t;
42785 case CTX_PROPERTY_USER:
42786 do
42787 {
42788 t = cp_parser_constant_expression (parser);
42789 if (t != error_mark_node)
42790 {
42791 t = fold_non_dependent_expr (t);
42792 if (TREE_CODE (t) == STRING_CST)
42793 properties = tree_cons (NULL_TREE, t, properties);
42794 else if (!value_dependent_expression_p (t)
42795 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
42796 || !tree_fits_shwi_p (t)))
42797 error_at (token->location, "property must be "
42798 "constant integer expression or string "
42799 "literal");
42800 else
42801 properties = tree_cons (NULL_TREE, t, properties);
42802 }
42803 else
42804 return error_mark_node;
42805
42806 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42807 cp_lexer_consume_token (parser->lexer);
42808 else
42809 break;
42810 }
42811 while (1);
42812 break;
42813 case CTX_PROPERTY_ID:
42814 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
42815 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42816 {
42817 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
42818 cp_lexer_consume_token (parser->lexer);
42819 properties = tree_cons (prop, NULL_TREE, properties);
42820 }
42821 else
42822 {
42823 cp_parser_error (parser, "expected identifier");
42824 return error_mark_node;
42825 }
42826 break;
42827 case CTX_PROPERTY_NAME_LIST:
42828 do
42829 {
42830 tree prop = NULL_TREE, value = NULL_TREE;
42831 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
42832 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42833 {
42834 prop = cp_lexer_peek_token (parser->lexer)->u.value;
42835 cp_lexer_consume_token (parser->lexer);
42836 }
42837 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
42838 value = cp_parser_string_literal (parser, false, false);
42839 else
42840 {
42841 cp_parser_error (parser, "expected identifier or "
42842 "string literal");
42843 return error_mark_node;
42844 }
42845
42846 properties = tree_cons (prop, value, properties);
42847
42848 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42849 cp_lexer_consume_token (parser->lexer);
42850 else
42851 break;
42852 }
42853 while (1);
42854 break;
42855 case CTX_PROPERTY_EXPR:
42856 t = cp_parser_constant_expression (parser);
42857 if (t != error_mark_node)
42858 {
42859 t = fold_non_dependent_expr (t);
42860 if (!value_dependent_expression_p (t)
42861 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
42862 || !tree_fits_shwi_p (t)))
42863 error_at (token->location, "property must be "
42864 "constant integer expression");
42865 else
42866 properties = tree_cons (NULL_TREE, t, properties);
42867 }
42868 else
42869 return error_mark_node;
42870 break;
42871 case CTX_PROPERTY_SIMD:
42872 if (!has_parms_p)
42873 {
42874 error_at (token->location, "properties for %<simd%> "
42875 "selector may not be specified in "
42876 "%<metadirective%>");
42877 return error_mark_node;
42878 }
42879 properties
42880 = cp_parser_omp_all_clauses (parser,
42881 OMP_DECLARE_SIMD_CLAUSE_MASK,
42882 "simd", NULL, true, 2);
42883 break;
42884 default:
42885 gcc_unreachable ();
42886 }
42887
42888 if (!parens.require_close (parser))
42889 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
42890
42891 properties = nreverse (properties);
42892 }
42893 else if (property_kind == CTX_PROPERTY_NAME_LIST
42894 || property_kind == CTX_PROPERTY_ID
42895 || property_kind == CTX_PROPERTY_EXPR)
42896 {
42897 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
42898 return error_mark_node;
42899 }
42900
42901 ret = tree_cons (selector, properties, ret);
42902
42903 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42904 cp_lexer_consume_token (parser->lexer);
42905 else
42906 break;
42907 }
42908 while (1);
42909
42910 return nreverse (ret);
42911 }
42912
42913 /* OpenMP 5.0:
42914
42915 trait-set-selector[,trait-set-selector[,...]]
42916
42917 trait-set-selector:
42918 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
42919
42920 trait-set-selector-name:
42921 constructor
42922 device
42923 implementation
42924 user */
42925
42926 static tree
42927 cp_parser_omp_context_selector_specification (cp_parser *parser,
42928 bool has_parms_p)
42929 {
42930 tree ret = NULL_TREE;
42931 do
42932 {
42933 const char *setp = "";
42934 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42935 setp
42936 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
42937 switch (setp[0])
42938 {
42939 case 'c':
42940 if (strcmp (setp, "construct") == 0)
42941 setp = NULL;
42942 break;
42943 case 'd':
42944 if (strcmp (setp, "device") == 0)
42945 setp = NULL;
42946 break;
42947 case 'i':
42948 if (strcmp (setp, "implementation") == 0)
42949 setp = NULL;
42950 break;
42951 case 'u':
42952 if (strcmp (setp, "user") == 0)
42953 setp = NULL;
42954 break;
42955 default:
42956 break;
42957 }
42958 if (setp)
42959 {
42960 cp_parser_error (parser, "expected %<construct%>, %<device%>, "
42961 "%<implementation%> or %<user%>");
42962 return error_mark_node;
42963 }
42964
42965 tree set = cp_lexer_peek_token (parser->lexer)->u.value;
42966 cp_lexer_consume_token (parser->lexer);
42967
42968 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42969 return error_mark_node;
42970
42971 matching_braces braces;
42972 if (!braces.require_open (parser))
42973 return error_mark_node;
42974
42975 tree selectors
42976 = cp_parser_omp_context_selector (parser, set, has_parms_p);
42977 if (selectors == error_mark_node)
42978 {
42979 cp_parser_skip_to_closing_brace (parser);
42980 ret = error_mark_node;
42981 }
42982 else if (ret != error_mark_node)
42983 ret = tree_cons (set, selectors, ret);
42984
42985 braces.require_close (parser);
42986
42987 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42988 cp_lexer_consume_token (parser->lexer);
42989 else
42990 break;
42991 }
42992 while (1);
42993
42994 if (ret == error_mark_node)
42995 return ret;
42996 return nreverse (ret);
42997 }
42998
42999 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
43000 that into "omp declare variant base" attribute. */
43001
43002 static tree
43003 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
43004 tree attrs)
43005 {
43006 matching_parens parens;
43007 if (!parens.require_open (parser))
43008 {
43009 fail:
43010 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43011 return attrs;
43012 }
43013
43014 bool template_p;
43015 cp_id_kind idk = CP_ID_KIND_NONE;
43016 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
43017 cp_expr varid
43018 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
43019 /*check_dependency_p=*/true,
43020 /*template_p=*/&template_p,
43021 /*declarator_p=*/false,
43022 /*optional_p=*/false);
43023 parens.require_close (parser);
43024
43025 tree variant;
43026 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
43027 || TREE_CODE (varid) == TYPE_DECL
43028 || varid == error_mark_node)
43029 variant = varid;
43030 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
43031 variant = NULL_TREE;
43032 else
43033 {
43034 tree ambiguous_decls;
43035 variant = cp_parser_lookup_name (parser, varid, none_type,
43036 template_p, /*is_namespace=*/false,
43037 /*check_dependency=*/true,
43038 &ambiguous_decls,
43039 varid.get_location ());
43040 if (ambiguous_decls)
43041 variant = NULL_TREE;
43042 }
43043 if (variant == NULL_TREE)
43044 variant = error_mark_node;
43045 else if (TREE_CODE (variant) != SCOPE_REF)
43046 {
43047 const char *error_msg;
43048 variant
43049 = finish_id_expression (varid, variant, parser->scope,
43050 &idk, false, true,
43051 &parser->non_integral_constant_expression_p,
43052 template_p, true, false, false, &error_msg,
43053 varid.get_location ());
43054 if (error_msg)
43055 cp_parser_error (parser, error_msg);
43056 }
43057 location_t caret_loc = get_pure_location (varid.get_location ());
43058 location_t start_loc = get_start (varid_token->location);
43059 location_t finish_loc = get_finish (varid.get_location ());
43060 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
43061
43062 const char *clause = "";
43063 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
43064 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43065 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
43066 if (strcmp (clause, "match"))
43067 {
43068 cp_parser_error (parser, "expected %<match%>");
43069 goto fail;
43070 }
43071
43072 cp_lexer_consume_token (parser->lexer);
43073
43074 if (!parens.require_open (parser))
43075 goto fail;
43076
43077 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
43078 if (ctx == error_mark_node)
43079 goto fail;
43080 ctx = c_omp_check_context_selector (match_loc, ctx);
43081 if (ctx != error_mark_node && variant != error_mark_node)
43082 {
43083 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
43084 match_loc);
43085 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
43086 loc_node = tree_cons (match_loc_node,
43087 build_int_cst (integer_type_node, idk),
43088 build_tree_list (loc_node, integer_zero_node));
43089 attrs = tree_cons (get_identifier ("omp declare variant base"),
43090 tree_cons (variant, ctx, loc_node), attrs);
43091 if (processing_template_decl)
43092 ATTR_IS_DEPENDENT (attrs) = 1;
43093 }
43094
43095 parens.require_close (parser);
43096 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43097 return attrs;
43098 }
43099
43100
43101 /* Finalize #pragma omp declare simd clauses after direct declarator has
43102 been parsed, and put that into "omp declare simd" attribute. */
43103
43104 static tree
43105 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
43106 {
43107 struct cp_token_cache *ce;
43108 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
43109 int i;
43110
43111 if (!data->error_seen && data->fndecl_seen)
43112 {
43113 error ("%<#pragma omp declare %s%> not immediately followed by "
43114 "a single function declaration or definition",
43115 data->variant_p ? "variant" : "simd");
43116 data->error_seen = true;
43117 }
43118 if (data->error_seen)
43119 return attrs;
43120
43121 FOR_EACH_VEC_ELT (data->tokens, i, ce)
43122 {
43123 tree c, cl;
43124
43125 cp_parser_push_lexer_for_tokens (parser, ce);
43126 parser->lexer->in_pragma = true;
43127 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
43128 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
43129 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43130 const char *kind = IDENTIFIER_POINTER (id);
43131 cp_lexer_consume_token (parser->lexer);
43132 if (strcmp (kind, "simd") == 0)
43133 {
43134 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
43135 "#pragma omp declare simd",
43136 pragma_tok);
43137 if (cl)
43138 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
43139 c = build_tree_list (get_identifier ("omp declare simd"), cl);
43140 TREE_CHAIN (c) = attrs;
43141 if (processing_template_decl)
43142 ATTR_IS_DEPENDENT (c) = 1;
43143 attrs = c;
43144 }
43145 else
43146 {
43147 gcc_assert (strcmp (kind, "variant") == 0);
43148 attrs = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
43149 }
43150 cp_parser_pop_lexer (parser);
43151 }
43152
43153 data->fndecl_seen = true;
43154 return attrs;
43155 }
43156
43157
43158 /* OpenMP 4.0:
43159 # pragma omp declare target new-line
43160 declarations and definitions
43161 # pragma omp end declare target new-line
43162
43163 OpenMP 4.5:
43164 # pragma omp declare target ( extended-list ) new-line
43165
43166 # pragma omp declare target declare-target-clauses[seq] new-line */
43167
43168 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
43169 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
43170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
43171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
43172
43173 static void
43174 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
43175 {
43176 tree clauses = NULL_TREE;
43177 int device_type = 0;
43178 bool only_device_type = true;
43179 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43180 clauses
43181 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
43182 "#pragma omp declare target", pragma_tok);
43183 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
43184 {
43185 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
43186 clauses);
43187 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
43188 cp_parser_require_pragma_eol (parser, pragma_tok);
43189 }
43190 else
43191 {
43192 cp_parser_require_pragma_eol (parser, pragma_tok);
43193 scope_chain->omp_declare_target_attribute++;
43194 return;
43195 }
43196 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
43197 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
43198 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
43199 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
43200 {
43201 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
43202 continue;
43203 tree t = OMP_CLAUSE_DECL (c), id;
43204 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
43205 tree at2 = lookup_attribute ("omp declare target link",
43206 DECL_ATTRIBUTES (t));
43207 only_device_type = false;
43208 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
43209 {
43210 id = get_identifier ("omp declare target link");
43211 std::swap (at1, at2);
43212 }
43213 else
43214 id = get_identifier ("omp declare target");
43215 if (at2)
43216 {
43217 error_at (OMP_CLAUSE_LOCATION (c),
43218 "%qD specified both in declare target %<link%> and %<to%>"
43219 " clauses", t);
43220 continue;
43221 }
43222 if (!at1)
43223 {
43224 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
43225 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
43226 continue;
43227
43228 symtab_node *node = symtab_node::get (t);
43229 if (node != NULL)
43230 {
43231 node->offloadable = 1;
43232 if (ENABLE_OFFLOADING)
43233 {
43234 g->have_offload = true;
43235 if (is_a <varpool_node *> (node))
43236 vec_safe_push (offload_vars, t);
43237 }
43238 }
43239 }
43240 if (TREE_CODE (t) != FUNCTION_DECL)
43241 continue;
43242 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
43243 {
43244 tree at3 = lookup_attribute ("omp declare target host",
43245 DECL_ATTRIBUTES (t));
43246 if (at3 == NULL_TREE)
43247 {
43248 id = get_identifier ("omp declare target host");
43249 DECL_ATTRIBUTES (t)
43250 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
43251 }
43252 }
43253 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
43254 {
43255 tree at3 = lookup_attribute ("omp declare target nohost",
43256 DECL_ATTRIBUTES (t));
43257 if (at3 == NULL_TREE)
43258 {
43259 id = get_identifier ("omp declare target nohost");
43260 DECL_ATTRIBUTES (t)
43261 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
43262 }
43263 }
43264 }
43265 if (device_type && only_device_type)
43266 warning_at (OMP_CLAUSE_LOCATION (clauses), 0,
43267 "directive with only %<device_type%> clauses ignored");
43268 }
43269
43270 static void
43271 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
43272 {
43273 const char *p = "";
43274 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43275 {
43276 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43277 p = IDENTIFIER_POINTER (id);
43278 }
43279 if (strcmp (p, "declare") == 0)
43280 {
43281 cp_lexer_consume_token (parser->lexer);
43282 p = "";
43283 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43284 {
43285 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43286 p = IDENTIFIER_POINTER (id);
43287 }
43288 if (strcmp (p, "target") == 0)
43289 cp_lexer_consume_token (parser->lexer);
43290 else
43291 {
43292 cp_parser_error (parser, "expected %<target%>");
43293 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43294 return;
43295 }
43296 }
43297 else
43298 {
43299 cp_parser_error (parser, "expected %<declare%>");
43300 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43301 return;
43302 }
43303 cp_parser_require_pragma_eol (parser, pragma_tok);
43304 if (!scope_chain->omp_declare_target_attribute)
43305 error_at (pragma_tok->location,
43306 "%<#pragma omp end declare target%> without corresponding "
43307 "%<#pragma omp declare target%>");
43308 else
43309 scope_chain->omp_declare_target_attribute--;
43310 }
43311
43312 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
43313 expression and optional initializer clause of
43314 #pragma omp declare reduction. We store the expression(s) as
43315 either 3, 6 or 7 special statements inside of the artificial function's
43316 body. The first two statements are DECL_EXPRs for the artificial
43317 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
43318 expression that uses those variables.
43319 If there was any INITIALIZER clause, this is followed by further statements,
43320 the fourth and fifth statements are DECL_EXPRs for the artificial
43321 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
43322 constructor variant (first token after open paren is not omp_priv),
43323 then the sixth statement is a statement with the function call expression
43324 that uses the OMP_PRIV and optionally OMP_ORIG variable.
43325 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
43326 to initialize the OMP_PRIV artificial variable and there is seventh
43327 statement, a DECL_EXPR of the OMP_PRIV statement again. */
43328
43329 static bool
43330 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
43331 {
43332 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
43333 gcc_assert (TYPE_REF_P (type));
43334 type = TREE_TYPE (type);
43335 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
43336 DECL_ARTIFICIAL (omp_out) = 1;
43337 pushdecl (omp_out);
43338 add_decl_expr (omp_out);
43339 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
43340 DECL_ARTIFICIAL (omp_in) = 1;
43341 pushdecl (omp_in);
43342 add_decl_expr (omp_in);
43343 tree combiner;
43344 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
43345
43346 keep_next_level (true);
43347 tree block = begin_omp_structured_block ();
43348 combiner = cp_parser_expression (parser);
43349 finish_expr_stmt (combiner);
43350 block = finish_omp_structured_block (block);
43351 if (processing_template_decl)
43352 block = build_stmt (input_location, EXPR_STMT, block);
43353 add_stmt (block);
43354
43355 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
43356 return false;
43357
43358 const char *p = "";
43359 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43360 {
43361 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43362 p = IDENTIFIER_POINTER (id);
43363 }
43364
43365 if (strcmp (p, "initializer") == 0)
43366 {
43367 cp_lexer_consume_token (parser->lexer);
43368 matching_parens parens;
43369 if (!parens.require_open (parser))
43370 return false;
43371
43372 p = "";
43373 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43374 {
43375 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43376 p = IDENTIFIER_POINTER (id);
43377 }
43378
43379 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
43380 DECL_ARTIFICIAL (omp_priv) = 1;
43381 pushdecl (omp_priv);
43382 add_decl_expr (omp_priv);
43383 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
43384 DECL_ARTIFICIAL (omp_orig) = 1;
43385 pushdecl (omp_orig);
43386 add_decl_expr (omp_orig);
43387
43388 keep_next_level (true);
43389 block = begin_omp_structured_block ();
43390
43391 bool ctor = false;
43392 if (strcmp (p, "omp_priv") == 0)
43393 {
43394 bool is_direct_init, is_non_constant_init;
43395 ctor = true;
43396 cp_lexer_consume_token (parser->lexer);
43397 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
43398 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
43399 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
43400 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
43401 == CPP_CLOSE_PAREN
43402 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
43403 == CPP_CLOSE_PAREN))
43404 {
43405 finish_omp_structured_block (block);
43406 error ("invalid initializer clause");
43407 return false;
43408 }
43409 initializer = cp_parser_initializer (parser, &is_direct_init,
43410 &is_non_constant_init);
43411 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
43412 NULL_TREE, LOOKUP_ONLYCONVERTING);
43413 }
43414 else
43415 {
43416 cp_parser_parse_tentatively (parser);
43417 /* Don't create location wrapper nodes here. */
43418 auto_suppress_location_wrappers sentinel;
43419 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
43420 /*check_dependency_p=*/true,
43421 /*template_p=*/NULL,
43422 /*declarator_p=*/false,
43423 /*optional_p=*/false);
43424 vec<tree, va_gc> *args;
43425 if (fn_name == error_mark_node
43426 || cp_parser_error_occurred (parser)
43427 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
43428 || ((args = cp_parser_parenthesized_expression_list
43429 (parser, non_attr, /*cast_p=*/false,
43430 /*allow_expansion_p=*/true,
43431 /*non_constant_p=*/NULL)),
43432 cp_parser_error_occurred (parser)))
43433 {
43434 finish_omp_structured_block (block);
43435 cp_parser_abort_tentative_parse (parser);
43436 cp_parser_error (parser, "expected id-expression (arguments)");
43437 return false;
43438 }
43439 unsigned int i;
43440 tree arg;
43441 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
43442 if (arg == omp_priv
43443 || (TREE_CODE (arg) == ADDR_EXPR
43444 && TREE_OPERAND (arg, 0) == omp_priv))
43445 break;
43446 cp_parser_abort_tentative_parse (parser);
43447 if (arg == NULL_TREE)
43448 error ("one of the initializer call arguments should be %<omp_priv%>"
43449 " or %<&omp_priv%>");
43450 initializer = cp_parser_postfix_expression (parser, false, false, false,
43451 false, NULL);
43452 finish_expr_stmt (initializer);
43453 }
43454
43455 block = finish_omp_structured_block (block);
43456 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
43457 if (processing_template_decl)
43458 block = build_stmt (input_location, EXPR_STMT, block);
43459 add_stmt (block);
43460
43461 if (ctor)
43462 add_decl_expr (omp_orig);
43463
43464 if (!parens.require_close (parser))
43465 return false;
43466 }
43467
43468 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
43469 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
43470 UNKNOWN_LOCATION);
43471
43472 return true;
43473 }
43474
43475 /* OpenMP 4.0
43476 #pragma omp declare reduction (reduction-id : typename-list : expression) \
43477 initializer-clause[opt] new-line
43478
43479 initializer-clause:
43480 initializer (omp_priv initializer)
43481 initializer (function-name (argument-list)) */
43482
43483 static void
43484 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
43485 enum pragma_context)
43486 {
43487 auto_vec<tree> types;
43488 enum tree_code reduc_code = ERROR_MARK;
43489 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
43490 unsigned int i;
43491 cp_token *first_token;
43492 cp_token_cache *cp;
43493 int errs;
43494 void *p;
43495
43496 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
43497 p = obstack_alloc (&declarator_obstack, 0);
43498
43499 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
43500 goto fail;
43501
43502 switch (cp_lexer_peek_token (parser->lexer)->type)
43503 {
43504 case CPP_PLUS:
43505 reduc_code = PLUS_EXPR;
43506 break;
43507 case CPP_MULT:
43508 reduc_code = MULT_EXPR;
43509 break;
43510 case CPP_MINUS:
43511 reduc_code = MINUS_EXPR;
43512 break;
43513 case CPP_AND:
43514 reduc_code = BIT_AND_EXPR;
43515 break;
43516 case CPP_XOR:
43517 reduc_code = BIT_XOR_EXPR;
43518 break;
43519 case CPP_OR:
43520 reduc_code = BIT_IOR_EXPR;
43521 break;
43522 case CPP_AND_AND:
43523 reduc_code = TRUTH_ANDIF_EXPR;
43524 break;
43525 case CPP_OR_OR:
43526 reduc_code = TRUTH_ORIF_EXPR;
43527 break;
43528 case CPP_NAME:
43529 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
43530 break;
43531 default:
43532 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
43533 "%<|%>, %<&&%>, %<||%> or identifier");
43534 goto fail;
43535 }
43536
43537 if (reduc_code != ERROR_MARK)
43538 cp_lexer_consume_token (parser->lexer);
43539
43540 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
43541 if (reduc_id == error_mark_node)
43542 goto fail;
43543
43544 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
43545 goto fail;
43546
43547 /* Types may not be defined in declare reduction type list. */
43548 const char *saved_message;
43549 saved_message = parser->type_definition_forbidden_message;
43550 parser->type_definition_forbidden_message
43551 = G_("types may not be defined in declare reduction type list");
43552 bool saved_colon_corrects_to_scope_p;
43553 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
43554 parser->colon_corrects_to_scope_p = false;
43555 bool saved_colon_doesnt_start_class_def_p;
43556 saved_colon_doesnt_start_class_def_p
43557 = parser->colon_doesnt_start_class_def_p;
43558 parser->colon_doesnt_start_class_def_p = true;
43559
43560 while (true)
43561 {
43562 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43563 type = cp_parser_type_id (parser);
43564 if (type == error_mark_node)
43565 ;
43566 else if (ARITHMETIC_TYPE_P (type)
43567 && (orig_reduc_id == NULL_TREE
43568 || (TREE_CODE (type) != COMPLEX_TYPE
43569 && (id_equal (orig_reduc_id, "min")
43570 || id_equal (orig_reduc_id, "max")))))
43571 error_at (loc, "predeclared arithmetic type %qT in "
43572 "%<#pragma omp declare reduction%>", type);
43573 else if (FUNC_OR_METHOD_TYPE_P (type)
43574 || TREE_CODE (type) == ARRAY_TYPE)
43575 error_at (loc, "function or array type %qT in "
43576 "%<#pragma omp declare reduction%>", type);
43577 else if (TYPE_REF_P (type))
43578 error_at (loc, "reference type %qT in "
43579 "%<#pragma omp declare reduction%>", type);
43580 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
43581 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
43582 "type %qT in %<#pragma omp declare reduction%>", type);
43583 else
43584 types.safe_push (type);
43585
43586 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43587 cp_lexer_consume_token (parser->lexer);
43588 else
43589 break;
43590 }
43591
43592 /* Restore the saved message. */
43593 parser->type_definition_forbidden_message = saved_message;
43594 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
43595 parser->colon_doesnt_start_class_def_p
43596 = saved_colon_doesnt_start_class_def_p;
43597
43598 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
43599 || types.is_empty ())
43600 {
43601 fail:
43602 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43603 goto done;
43604 }
43605
43606 first_token = cp_lexer_peek_token (parser->lexer);
43607 cp = NULL;
43608 errs = errorcount;
43609 FOR_EACH_VEC_ELT (types, i, type)
43610 {
43611 tree fntype
43612 = build_function_type_list (void_type_node,
43613 cp_build_reference_type (type, false),
43614 NULL_TREE);
43615 tree this_reduc_id = reduc_id;
43616 if (!dependent_type_p (type))
43617 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
43618 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
43619 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
43620 DECL_ARTIFICIAL (fndecl) = 1;
43621 DECL_EXTERNAL (fndecl) = 1;
43622 DECL_DECLARED_INLINE_P (fndecl) = 1;
43623 DECL_IGNORED_P (fndecl) = 1;
43624 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
43625 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
43626 DECL_ATTRIBUTES (fndecl)
43627 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
43628 DECL_ATTRIBUTES (fndecl));
43629 bool block_scope = false;
43630 if (current_function_decl)
43631 {
43632 block_scope = true;
43633 DECL_CONTEXT (fndecl) = current_function_decl;
43634 DECL_LOCAL_DECL_P (fndecl) = true;
43635 }
43636
43637 if (processing_template_decl)
43638 fndecl = push_template_decl (fndecl);
43639
43640 if (block_scope)
43641 {
43642 if (!processing_template_decl)
43643 pushdecl (fndecl);
43644 }
43645 else if (current_class_type)
43646 {
43647 if (cp == NULL)
43648 {
43649 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
43650 cp_lexer_consume_token (parser->lexer);
43651 cp = cp_token_cache_new (first_token,
43652 cp_lexer_peek_nth_token (parser->lexer,
43653 2));
43654 }
43655 DECL_STATIC_FUNCTION_P (fndecl) = 1;
43656 finish_member_declaration (fndecl);
43657 DECL_PENDING_INLINE_INFO (fndecl) = cp;
43658 DECL_PENDING_INLINE_P (fndecl) = 1;
43659 vec_safe_push (unparsed_funs_with_definitions, fndecl);
43660 continue;
43661 }
43662 else
43663 {
43664 DECL_CONTEXT (fndecl) = current_namespace;
43665 tree d = pushdecl (fndecl);
43666 /* We should never meet a matched duplicate decl. */
43667 gcc_checking_assert (d == error_mark_node || d == fndecl);
43668 }
43669
43670 tree block = NULL_TREE;
43671 if (!block_scope)
43672 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
43673 else
43674 block = begin_omp_structured_block ();
43675 if (cp)
43676 {
43677 cp_parser_push_lexer_for_tokens (parser, cp);
43678 parser->lexer->in_pragma = true;
43679 }
43680
43681 bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
43682
43683 if (cp)
43684 cp_parser_pop_lexer (parser);
43685 if (!block_scope)
43686 finish_function (/*inline_p=*/false);
43687 else
43688 {
43689 DECL_CONTEXT (fndecl) = current_function_decl;
43690 if (DECL_TEMPLATE_INFO (fndecl))
43691 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
43692 }
43693 if (!ok)
43694 goto fail;
43695
43696 if (block_scope)
43697 {
43698 block = finish_omp_structured_block (block);
43699 if (TREE_CODE (block) == BIND_EXPR)
43700 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
43701 else if (TREE_CODE (block) == STATEMENT_LIST)
43702 DECL_SAVED_TREE (fndecl) = block;
43703 if (processing_template_decl)
43704 add_decl_expr (fndecl);
43705 }
43706
43707 cp_check_omp_declare_reduction (fndecl);
43708 if (cp == NULL && types.length () > 1)
43709 cp = cp_token_cache_new (first_token,
43710 cp_lexer_peek_nth_token (parser->lexer, 2));
43711 if (errs != errorcount)
43712 break;
43713 }
43714
43715 cp_parser_require_pragma_eol (parser, pragma_tok);
43716
43717 done:
43718 /* Free any declarators allocated. */
43719 obstack_free (&declarator_obstack, p);
43720 }
43721
43722 /* OpenMP 4.0
43723 #pragma omp declare simd declare-simd-clauses[optseq] new-line
43724 #pragma omp declare reduction (reduction-id : typename-list : expression) \
43725 initializer-clause[opt] new-line
43726 #pragma omp declare target new-line
43727
43728 OpenMP 5.0
43729 #pragma omp declare variant (identifier) match (context-selector) */
43730
43731 static bool
43732 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
43733 enum pragma_context context)
43734 {
43735 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43736 {
43737 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43738 const char *p = IDENTIFIER_POINTER (id);
43739
43740 if (strcmp (p, "simd") == 0)
43741 {
43742 cp_lexer_consume_token (parser->lexer);
43743 cp_parser_omp_declare_simd (parser, pragma_tok,
43744 context, false);
43745 return true;
43746 }
43747 if (flag_openmp && strcmp (p, "variant") == 0)
43748 {
43749 cp_lexer_consume_token (parser->lexer);
43750 cp_parser_omp_declare_simd (parser, pragma_tok,
43751 context, true);
43752 return true;
43753 }
43754 cp_ensure_no_omp_declare_simd (parser);
43755 if (strcmp (p, "reduction") == 0)
43756 {
43757 cp_lexer_consume_token (parser->lexer);
43758 cp_parser_omp_declare_reduction (parser, pragma_tok,
43759 context);
43760 return false;
43761 }
43762 if (!flag_openmp) /* flag_openmp_simd */
43763 {
43764 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43765 return false;
43766 }
43767 if (strcmp (p, "target") == 0)
43768 {
43769 cp_lexer_consume_token (parser->lexer);
43770 cp_parser_omp_declare_target (parser, pragma_tok);
43771 return false;
43772 }
43773 }
43774 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
43775 "%<target%> or %<variant%>");
43776 cp_parser_require_pragma_eol (parser, pragma_tok);
43777 return false;
43778 }
43779
43780 /* OpenMP 5.0
43781 #pragma omp requires clauses[optseq] new-line */
43782
43783 static bool
43784 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
43785 {
43786 bool first = true;
43787 enum omp_requires new_req = (enum omp_requires) 0;
43788
43789 location_t loc = pragma_tok->location;
43790 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
43791 {
43792 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43793 cp_lexer_consume_token (parser->lexer);
43794
43795 first = false;
43796
43797 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43798 {
43799 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43800 const char *p = IDENTIFIER_POINTER (id);
43801 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
43802 enum omp_requires this_req = (enum omp_requires) 0;
43803
43804 if (!strcmp (p, "unified_address"))
43805 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
43806 else if (!strcmp (p, "unified_shared_memory"))
43807 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
43808 else if (!strcmp (p, "dynamic_allocators"))
43809 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
43810 else if (!strcmp (p, "reverse_offload"))
43811 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
43812 else if (!strcmp (p, "atomic_default_mem_order"))
43813 {
43814 cp_lexer_consume_token (parser->lexer);
43815
43816 matching_parens parens;
43817 if (parens.require_open (parser))
43818 {
43819 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43820 {
43821 id = cp_lexer_peek_token (parser->lexer)->u.value;
43822 p = IDENTIFIER_POINTER (id);
43823
43824 if (!strcmp (p, "seq_cst"))
43825 this_req
43826 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
43827 else if (!strcmp (p, "relaxed"))
43828 this_req
43829 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
43830 else if (!strcmp (p, "acq_rel"))
43831 this_req
43832 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
43833 }
43834 if (this_req == 0)
43835 {
43836 error_at (cp_lexer_peek_token (parser->lexer)->location,
43837 "expected %<seq_cst%>, %<relaxed%> or "
43838 "%<acq_rel%>");
43839 if (cp_lexer_nth_token_is (parser->lexer, 2,
43840 CPP_CLOSE_PAREN))
43841 cp_lexer_consume_token (parser->lexer);
43842 }
43843 else
43844 cp_lexer_consume_token (parser->lexer);
43845
43846 if (!parens.require_close (parser))
43847 cp_parser_skip_to_closing_parenthesis (parser,
43848 /*recovering=*/true,
43849 /*or_comma=*/false,
43850 /*consume_paren=*/
43851 true);
43852
43853 if (this_req == 0)
43854 {
43855 cp_parser_require_pragma_eol (parser, pragma_tok);
43856 return false;
43857 }
43858 }
43859 p = NULL;
43860 }
43861 else
43862 {
43863 error_at (cloc, "expected %<unified_address%>, "
43864 "%<unified_shared_memory%>, "
43865 "%<dynamic_allocators%>, "
43866 "%<reverse_offload%> "
43867 "or %<atomic_default_mem_order%> clause");
43868 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43869 return false;
43870 }
43871 if (p)
43872 sorry_at (cloc, "%qs clause on %<requires%> directive not "
43873 "supported yet", p);
43874 if (p)
43875 cp_lexer_consume_token (parser->lexer);
43876 if (this_req)
43877 {
43878 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
43879 {
43880 if ((this_req & new_req) != 0)
43881 error_at (cloc, "too many %qs clauses", p);
43882 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
43883 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
43884 error_at (cloc, "%qs clause used lexically after first "
43885 "target construct or offloading API", p);
43886 }
43887 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
43888 {
43889 error_at (cloc, "too many %qs clauses",
43890 "atomic_default_mem_order");
43891 this_req = (enum omp_requires) 0;
43892 }
43893 else if ((omp_requires_mask
43894 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
43895 {
43896 error_at (cloc, "more than one %<atomic_default_mem_order%>"
43897 " clause in a single compilation unit");
43898 this_req
43899 = (enum omp_requires)
43900 (omp_requires_mask
43901 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
43902 }
43903 else if ((omp_requires_mask
43904 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
43905 error_at (cloc, "%<atomic_default_mem_order%> clause used "
43906 "lexically after first %<atomic%> construct "
43907 "without memory order clause");
43908 new_req = (enum omp_requires) (new_req | this_req);
43909 omp_requires_mask
43910 = (enum omp_requires) (omp_requires_mask | this_req);
43911 continue;
43912 }
43913 }
43914 break;
43915 }
43916 cp_parser_require_pragma_eol (parser, pragma_tok);
43917
43918 if (new_req == 0)
43919 error_at (loc, "%<pragma omp requires%> requires at least one clause");
43920 return false;
43921 }
43922
43923
43924 /* OpenMP 4.5:
43925 #pragma omp taskloop taskloop-clause[optseq] new-line
43926 for-loop
43927
43928 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
43929 for-loop */
43930
43931 #define OMP_TASKLOOP_CLAUSE_MASK \
43932 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
43933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
43937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
43938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
43939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
43941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
43943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
43944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
43945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
43946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
43949
43950 static tree
43951 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
43952 char *p_name, omp_clause_mask mask, tree *cclauses,
43953 bool *if_p)
43954 {
43955 tree clauses, sb, ret;
43956 unsigned int save;
43957 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43958
43959 strcat (p_name, " taskloop");
43960 mask |= OMP_TASKLOOP_CLAUSE_MASK;
43961 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
43962 clause. */
43963 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
43964 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
43965
43966 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43967 {
43968 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43969 const char *p = IDENTIFIER_POINTER (id);
43970
43971 if (strcmp (p, "simd") == 0)
43972 {
43973 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43974 if (cclauses == NULL)
43975 cclauses = cclauses_buf;
43976
43977 cp_lexer_consume_token (parser->lexer);
43978 if (!flag_openmp) /* flag_openmp_simd */
43979 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43980 cclauses, if_p);
43981 sb = begin_omp_structured_block ();
43982 save = cp_parser_begin_omp_structured_block (parser);
43983 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43984 cclauses, if_p);
43985 cp_parser_end_omp_structured_block (parser, save);
43986 tree body = finish_omp_structured_block (sb);
43987 if (ret == NULL)
43988 return ret;
43989 ret = make_node (OMP_TASKLOOP);
43990 TREE_TYPE (ret) = void_type_node;
43991 OMP_FOR_BODY (ret) = body;
43992 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
43993 SET_EXPR_LOCATION (ret, loc);
43994 add_stmt (ret);
43995 return ret;
43996 }
43997 }
43998 if (!flag_openmp) /* flag_openmp_simd */
43999 {
44000 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44001 return NULL_TREE;
44002 }
44003
44004 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44005 cclauses == NULL);
44006 if (cclauses)
44007 {
44008 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
44009 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
44010 }
44011
44012 keep_next_level (true);
44013 sb = begin_omp_structured_block ();
44014 save = cp_parser_begin_omp_structured_block (parser);
44015
44016 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
44017 if_p);
44018
44019 cp_parser_end_omp_structured_block (parser, save);
44020 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
44021
44022 return ret;
44023 }
44024
44025
44026 /* OpenACC 2.0:
44027 # pragma acc routine oacc-routine-clause[optseq] new-line
44028 function-definition
44029
44030 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
44031 */
44032
44033 #define OACC_ROUTINE_CLAUSE_MASK \
44034 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
44035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
44036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
44037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
44038
44039
44040 /* Parse the OpenACC routine pragma. This has an optional '( name )'
44041 component, which must resolve to a declared namespace-scope
44042 function. The clauses are either processed directly (for a named
44043 function), or defered until the immediatley following declaration
44044 is parsed. */
44045
44046 static void
44047 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
44048 enum pragma_context context)
44049 {
44050 gcc_checking_assert (context == pragma_external);
44051 /* The checking for "another pragma following this one" in the "no optional
44052 '( name )'" case makes sure that we dont re-enter. */
44053 gcc_checking_assert (parser->oacc_routine == NULL);
44054
44055 cp_oacc_routine_data data;
44056 data.error_seen = false;
44057 data.fndecl_seen = false;
44058 data.tokens = vNULL;
44059 data.clauses = NULL_TREE;
44060 data.loc = pragma_tok->location;
44061 /* It is safe to take the address of a local variable; it will only be
44062 used while this scope is live. */
44063 parser->oacc_routine = &data;
44064
44065 /* Look for optional '( name )'. */
44066 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
44067 {
44068 matching_parens parens;
44069 parens.consume_open (parser); /* '(' */
44070
44071 /* We parse the name as an id-expression. If it resolves to
44072 anything other than a non-overloaded function at namespace
44073 scope, it's an error. */
44074 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
44075 tree name = cp_parser_id_expression (parser,
44076 /*template_keyword_p=*/false,
44077 /*check_dependency_p=*/false,
44078 /*template_p=*/NULL,
44079 /*declarator_p=*/false,
44080 /*optional_p=*/false);
44081 tree decl = (identifier_p (name)
44082 ? cp_parser_lookup_name_simple (parser, name, name_loc)
44083 : name);
44084 if (name != error_mark_node && decl == error_mark_node)
44085 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
44086
44087 if (decl == error_mark_node
44088 || !parens.require_close (parser))
44089 {
44090 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44091 parser->oacc_routine = NULL;
44092 return;
44093 }
44094
44095 data.clauses
44096 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
44097 "#pragma acc routine",
44098 cp_lexer_peek_token (parser->lexer));
44099 /* The clauses are in reverse order; fix that to make later diagnostic
44100 emission easier. */
44101 data.clauses = nreverse (data.clauses);
44102
44103 if (decl && is_overloaded_fn (decl)
44104 && (TREE_CODE (decl) != FUNCTION_DECL
44105 || DECL_FUNCTION_TEMPLATE_P (decl)))
44106 {
44107 error_at (name_loc,
44108 "%<#pragma acc routine%> names a set of overloads");
44109 parser->oacc_routine = NULL;
44110 return;
44111 }
44112
44113 /* Perhaps we should use the same rule as declarations in different
44114 namespaces? */
44115 if (!DECL_NAMESPACE_SCOPE_P (decl))
44116 {
44117 error_at (name_loc,
44118 "%qD does not refer to a namespace scope function", decl);
44119 parser->oacc_routine = NULL;
44120 return;
44121 }
44122
44123 if (TREE_CODE (decl) != FUNCTION_DECL)
44124 {
44125 error_at (name_loc, "%qD does not refer to a function", decl);
44126 parser->oacc_routine = NULL;
44127 return;
44128 }
44129
44130 cp_finalize_oacc_routine (parser, decl, false);
44131 parser->oacc_routine = NULL;
44132 }
44133 else /* No optional '( name )'. */
44134 {
44135 /* Store away all pragma tokens. */
44136 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
44137 cp_lexer_consume_token (parser->lexer);
44138 cp_parser_require_pragma_eol (parser, pragma_tok);
44139 struct cp_token_cache *cp
44140 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
44141 parser->oacc_routine->tokens.safe_push (cp);
44142
44143 /* Emit a helpful diagnostic if there's another pragma following this
44144 one. */
44145 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
44146 {
44147 cp_ensure_no_oacc_routine (parser);
44148 data.tokens.release ();
44149 /* ..., and then just keep going. */
44150 return;
44151 }
44152
44153 /* We only have to consider the pragma_external case here. */
44154 cp_parser_declaration (parser, NULL_TREE);
44155 if (parser->oacc_routine
44156 && !parser->oacc_routine->fndecl_seen)
44157 cp_ensure_no_oacc_routine (parser);
44158 else
44159 parser->oacc_routine = NULL;
44160 data.tokens.release ();
44161 }
44162 }
44163
44164 /* Finalize #pragma acc routine clauses after direct declarator has
44165 been parsed. */
44166
44167 static tree
44168 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
44169 {
44170 struct cp_token_cache *ce;
44171 cp_oacc_routine_data *data = parser->oacc_routine;
44172
44173 if (!data->error_seen && data->fndecl_seen)
44174 {
44175 error_at (data->loc,
44176 "%<#pragma acc routine%> not immediately followed by "
44177 "a single function declaration or definition");
44178 data->error_seen = true;
44179 }
44180 if (data->error_seen)
44181 return attrs;
44182
44183 gcc_checking_assert (data->tokens.length () == 1);
44184 ce = data->tokens[0];
44185
44186 cp_parser_push_lexer_for_tokens (parser, ce);
44187 parser->lexer->in_pragma = true;
44188 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
44189
44190 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
44191 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
44192 parser->oacc_routine->clauses
44193 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
44194 "#pragma acc routine", pragma_tok);
44195 /* The clauses are in reverse order; fix that to make later diagnostic
44196 emission easier. */
44197 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
44198 cp_parser_pop_lexer (parser);
44199 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
44200 fndecl_seen. */
44201
44202 return attrs;
44203 }
44204
44205 /* Apply any saved OpenACC routine clauses to a just-parsed
44206 declaration. */
44207
44208 static void
44209 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
44210 {
44211 if (__builtin_expect (parser->oacc_routine != NULL, 0))
44212 {
44213 /* Keep going if we're in error reporting mode. */
44214 if (parser->oacc_routine->error_seen
44215 || fndecl == error_mark_node)
44216 return;
44217
44218 if (parser->oacc_routine->fndecl_seen)
44219 {
44220 error_at (parser->oacc_routine->loc,
44221 "%<#pragma acc routine%> not immediately followed by"
44222 " a single function declaration or definition");
44223 parser->oacc_routine = NULL;
44224 return;
44225 }
44226 if (TREE_CODE (fndecl) != FUNCTION_DECL)
44227 {
44228 cp_ensure_no_oacc_routine (parser);
44229 return;
44230 }
44231
44232 int compatible
44233 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
44234 parser->oacc_routine->loc,
44235 "#pragma acc routine");
44236 if (compatible < 0)
44237 {
44238 parser->oacc_routine = NULL;
44239 return;
44240 }
44241 if (compatible > 0)
44242 {
44243 }
44244 else
44245 {
44246 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
44247 {
44248 error_at (parser->oacc_routine->loc,
44249 TREE_USED (fndecl)
44250 ? G_("%<#pragma acc routine%> must be applied before"
44251 " use")
44252 : G_("%<#pragma acc routine%> must be applied before"
44253 " definition"));
44254 parser->oacc_routine = NULL;
44255 return;
44256 }
44257
44258 /* Set the routine's level of parallelism. */
44259 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
44260 oacc_replace_fn_attrib (fndecl, dims);
44261
44262 /* Add an "omp declare target" attribute. */
44263 DECL_ATTRIBUTES (fndecl)
44264 = tree_cons (get_identifier ("omp declare target"),
44265 parser->oacc_routine->clauses,
44266 DECL_ATTRIBUTES (fndecl));
44267 }
44268
44269 /* Don't unset parser->oacc_routine here: we may still need it to
44270 diagnose wrong usage. But, remember that we've used this "#pragma acc
44271 routine". */
44272 parser->oacc_routine->fndecl_seen = true;
44273 }
44274 }
44275
44276 /* Main entry point to OpenMP statement pragmas. */
44277
44278 static void
44279 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44280 {
44281 tree stmt;
44282 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
44283 omp_clause_mask mask (0);
44284
44285 switch (cp_parser_pragma_kind (pragma_tok))
44286 {
44287 case PRAGMA_OACC_ATOMIC:
44288 cp_parser_omp_atomic (parser, pragma_tok, true);
44289 return;
44290 case PRAGMA_OACC_CACHE:
44291 stmt = cp_parser_oacc_cache (parser, pragma_tok);
44292 break;
44293 case PRAGMA_OACC_DATA:
44294 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
44295 break;
44296 case PRAGMA_OACC_ENTER_DATA:
44297 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
44298 break;
44299 case PRAGMA_OACC_EXIT_DATA:
44300 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
44301 break;
44302 case PRAGMA_OACC_HOST_DATA:
44303 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
44304 break;
44305 case PRAGMA_OACC_KERNELS:
44306 case PRAGMA_OACC_PARALLEL:
44307 case PRAGMA_OACC_SERIAL:
44308 strcpy (p_name, "#pragma acc");
44309 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
44310 break;
44311 case PRAGMA_OACC_LOOP:
44312 strcpy (p_name, "#pragma acc");
44313 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
44314 if_p);
44315 break;
44316 case PRAGMA_OACC_UPDATE:
44317 stmt = cp_parser_oacc_update (parser, pragma_tok);
44318 break;
44319 case PRAGMA_OACC_WAIT:
44320 stmt = cp_parser_oacc_wait (parser, pragma_tok);
44321 break;
44322 case PRAGMA_OMP_ALLOCATE:
44323 cp_parser_omp_allocate (parser, pragma_tok);
44324 return;
44325 case PRAGMA_OMP_ATOMIC:
44326 cp_parser_omp_atomic (parser, pragma_tok, false);
44327 return;
44328 case PRAGMA_OMP_CRITICAL:
44329 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
44330 break;
44331 case PRAGMA_OMP_DISTRIBUTE:
44332 strcpy (p_name, "#pragma omp");
44333 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
44334 if_p);
44335 break;
44336 case PRAGMA_OMP_FOR:
44337 strcpy (p_name, "#pragma omp");
44338 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
44339 if_p);
44340 break;
44341 case PRAGMA_OMP_LOOP:
44342 strcpy (p_name, "#pragma omp");
44343 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
44344 if_p);
44345 break;
44346 case PRAGMA_OMP_MASTER:
44347 strcpy (p_name, "#pragma omp");
44348 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
44349 if_p);
44350 break;
44351 case PRAGMA_OMP_PARALLEL:
44352 strcpy (p_name, "#pragma omp");
44353 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
44354 if_p);
44355 break;
44356 case PRAGMA_OMP_SECTIONS:
44357 strcpy (p_name, "#pragma omp");
44358 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
44359 break;
44360 case PRAGMA_OMP_SIMD:
44361 strcpy (p_name, "#pragma omp");
44362 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
44363 if_p);
44364 break;
44365 case PRAGMA_OMP_SINGLE:
44366 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
44367 break;
44368 case PRAGMA_OMP_TASK:
44369 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
44370 break;
44371 case PRAGMA_OMP_TASKGROUP:
44372 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
44373 break;
44374 case PRAGMA_OMP_TASKLOOP:
44375 strcpy (p_name, "#pragma omp");
44376 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
44377 if_p);
44378 break;
44379 case PRAGMA_OMP_TEAMS:
44380 strcpy (p_name, "#pragma omp");
44381 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
44382 if_p);
44383 break;
44384 default:
44385 gcc_unreachable ();
44386 }
44387
44388 protected_set_expr_location (stmt, pragma_tok->location);
44389 }
44390 \f
44391 /* Transactional Memory parsing routines. */
44392
44393 /* Parse a transaction attribute.
44394
44395 txn-attribute:
44396 attribute
44397 [ [ identifier ] ]
44398
44399 We use this instead of cp_parser_attributes_opt for transactions to avoid
44400 the pedwarn in C++98 mode. */
44401
44402 static tree
44403 cp_parser_txn_attribute_opt (cp_parser *parser)
44404 {
44405 cp_token *token;
44406 tree attr_name, attr = NULL;
44407
44408 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
44409 return cp_parser_attributes_opt (parser);
44410
44411 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
44412 return NULL_TREE;
44413 cp_lexer_consume_token (parser->lexer);
44414 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
44415 goto error1;
44416
44417 token = cp_lexer_peek_token (parser->lexer);
44418 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
44419 {
44420 token = cp_lexer_consume_token (parser->lexer);
44421
44422 attr_name = (token->type == CPP_KEYWORD
44423 /* For keywords, use the canonical spelling,
44424 not the parsed identifier. */
44425 ? ridpointers[(int) token->keyword]
44426 : token->u.value);
44427 attr = build_tree_list (attr_name, NULL_TREE);
44428 }
44429 else
44430 cp_parser_error (parser, "expected identifier");
44431
44432 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
44433 error1:
44434 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
44435 return attr;
44436 }
44437
44438 /* Parse a __transaction_atomic or __transaction_relaxed statement.
44439
44440 transaction-statement:
44441 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
44442 compound-statement
44443 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
44444 */
44445
44446 static tree
44447 cp_parser_transaction (cp_parser *parser, cp_token *token)
44448 {
44449 unsigned char old_in = parser->in_transaction;
44450 unsigned char this_in = 1, new_in;
44451 enum rid keyword = token->keyword;
44452 tree stmt, attrs, noex;
44453
44454 cp_lexer_consume_token (parser->lexer);
44455
44456 if (keyword == RID_TRANSACTION_RELAXED
44457 || keyword == RID_SYNCHRONIZED)
44458 this_in |= TM_STMT_ATTR_RELAXED;
44459 else
44460 {
44461 attrs = cp_parser_txn_attribute_opt (parser);
44462 if (attrs)
44463 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
44464 }
44465
44466 /* Parse a noexcept specification. */
44467 if (keyword == RID_ATOMIC_NOEXCEPT)
44468 noex = boolean_true_node;
44469 else if (keyword == RID_ATOMIC_CANCEL)
44470 {
44471 /* cancel-and-throw is unimplemented. */
44472 sorry ("%<atomic_cancel%>");
44473 noex = NULL_TREE;
44474 }
44475 else
44476 noex = cp_parser_noexcept_specification_opt (parser,
44477 CP_PARSER_FLAGS_NONE,
44478 /*require_constexpr=*/true,
44479 /*consumed_expr=*/NULL,
44480 /*return_cond=*/true);
44481
44482 /* Keep track if we're in the lexical scope of an outer transaction. */
44483 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
44484
44485 stmt = begin_transaction_stmt (token->location, NULL, this_in);
44486
44487 parser->in_transaction = new_in;
44488 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
44489 parser->in_transaction = old_in;
44490
44491 finish_transaction_stmt (stmt, NULL, this_in, noex);
44492
44493 return stmt;
44494 }
44495
44496 /* Parse a __transaction_atomic or __transaction_relaxed expression.
44497
44498 transaction-expression:
44499 __transaction_atomic txn-noexcept-spec[opt] ( expression )
44500 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
44501 */
44502
44503 static tree
44504 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
44505 {
44506 unsigned char old_in = parser->in_transaction;
44507 unsigned char this_in = 1;
44508 cp_token *token;
44509 tree expr, noex;
44510 bool noex_expr;
44511 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44512
44513 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
44514 || keyword == RID_TRANSACTION_RELAXED);
44515
44516 if (!flag_tm)
44517 error_at (loc,
44518 keyword == RID_TRANSACTION_RELAXED
44519 ? G_("%<__transaction_relaxed%> without transactional memory "
44520 "support enabled")
44521 : G_("%<__transaction_atomic%> without transactional memory "
44522 "support enabled"));
44523
44524 token = cp_parser_require_keyword (parser, keyword,
44525 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
44526 : RT_TRANSACTION_RELAXED));
44527 gcc_assert (token != NULL);
44528
44529 if (keyword == RID_TRANSACTION_RELAXED)
44530 this_in |= TM_STMT_ATTR_RELAXED;
44531
44532 /* Set this early. This might mean that we allow transaction_cancel in
44533 an expression that we find out later actually has to be a constexpr.
44534 However, we expect that cxx_constant_value will be able to deal with
44535 this; also, if the noexcept has no constexpr, then what we parse next
44536 really is a transaction's body. */
44537 parser->in_transaction = this_in;
44538
44539 /* Parse a noexcept specification. */
44540 noex = cp_parser_noexcept_specification_opt (parser,
44541 CP_PARSER_FLAGS_NONE,
44542 /*require_constexpr=*/false,
44543 &noex_expr,
44544 /*return_cond=*/true);
44545
44546 if (!noex || !noex_expr
44547 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
44548 {
44549 matching_parens parens;
44550 parens.require_open (parser);
44551
44552 expr = cp_parser_expression (parser);
44553 expr = finish_parenthesized_expr (expr);
44554
44555 parens.require_close (parser);
44556 }
44557 else
44558 {
44559 /* The only expression that is available got parsed for the noexcept
44560 already. noexcept is true then. */
44561 expr = noex;
44562 noex = boolean_true_node;
44563 }
44564
44565 expr = build_transaction_expr (token->location, expr, this_in, noex);
44566 parser->in_transaction = old_in;
44567
44568 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
44569 return error_mark_node;
44570
44571 return (flag_tm ? expr : error_mark_node);
44572 }
44573
44574 /* Parse a function-transaction-block.
44575
44576 function-transaction-block:
44577 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
44578 function-body
44579 __transaction_atomic txn-attribute[opt] function-try-block
44580 __transaction_relaxed ctor-initializer[opt] function-body
44581 __transaction_relaxed function-try-block
44582 */
44583
44584 static void
44585 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
44586 {
44587 unsigned char old_in = parser->in_transaction;
44588 unsigned char new_in = 1;
44589 tree compound_stmt, stmt, attrs;
44590 cp_token *token;
44591
44592 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
44593 || keyword == RID_TRANSACTION_RELAXED);
44594 token = cp_parser_require_keyword (parser, keyword,
44595 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
44596 : RT_TRANSACTION_RELAXED));
44597 gcc_assert (token != NULL);
44598
44599 if (keyword == RID_TRANSACTION_RELAXED)
44600 new_in |= TM_STMT_ATTR_RELAXED;
44601 else
44602 {
44603 attrs = cp_parser_txn_attribute_opt (parser);
44604 if (attrs)
44605 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
44606 }
44607
44608 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
44609
44610 parser->in_transaction = new_in;
44611
44612 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
44613 cp_parser_function_try_block (parser);
44614 else
44615 cp_parser_ctor_initializer_opt_and_function_body
44616 (parser, /*in_function_try_block=*/false);
44617
44618 parser->in_transaction = old_in;
44619
44620 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
44621 }
44622
44623 /* Parse a __transaction_cancel statement.
44624
44625 cancel-statement:
44626 __transaction_cancel txn-attribute[opt] ;
44627 __transaction_cancel txn-attribute[opt] throw-expression ;
44628
44629 ??? Cancel and throw is not yet implemented. */
44630
44631 static tree
44632 cp_parser_transaction_cancel (cp_parser *parser)
44633 {
44634 cp_token *token;
44635 bool is_outer = false;
44636 tree stmt, attrs;
44637
44638 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
44639 RT_TRANSACTION_CANCEL);
44640 gcc_assert (token != NULL);
44641
44642 attrs = cp_parser_txn_attribute_opt (parser);
44643 if (attrs)
44644 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
44645
44646 /* ??? Parse cancel-and-throw here. */
44647
44648 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
44649
44650 if (!flag_tm)
44651 {
44652 error_at (token->location, "%<__transaction_cancel%> without "
44653 "transactional memory support enabled");
44654 return error_mark_node;
44655 }
44656 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
44657 {
44658 error_at (token->location, "%<__transaction_cancel%> within a "
44659 "%<__transaction_relaxed%>");
44660 return error_mark_node;
44661 }
44662 else if (is_outer)
44663 {
44664 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
44665 && !is_tm_may_cancel_outer (current_function_decl))
44666 {
44667 error_at (token->location, "outer %<__transaction_cancel%> not "
44668 "within outer %<__transaction_atomic%>");
44669 error_at (token->location,
44670 " or a %<transaction_may_cancel_outer%> function");
44671 return error_mark_node;
44672 }
44673 }
44674 else if (parser->in_transaction == 0)
44675 {
44676 error_at (token->location, "%<__transaction_cancel%> not within "
44677 "%<__transaction_atomic%>");
44678 return error_mark_node;
44679 }
44680
44681 stmt = build_tm_abort_call (token->location, is_outer);
44682 add_stmt (stmt);
44683
44684 return stmt;
44685 }
44686 \f
44687 /* The parser. */
44688
44689 static GTY (()) cp_parser *the_parser;
44690
44691 \f
44692 /* Special handling for the first token or line in the file. The first
44693 thing in the file might be #pragma GCC pch_preprocess, which loads a
44694 PCH file, which is a GC collection point. So we need to handle this
44695 first pragma without benefit of an existing lexer structure.
44696
44697 Always returns one token to the caller in *FIRST_TOKEN. This is
44698 either the true first token of the file, or the first token after
44699 the initial pragma. */
44700
44701 static void
44702 cp_parser_initial_pragma (cp_token *first_token)
44703 {
44704 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
44705 return;
44706
44707 cp_lexer_get_preprocessor_token (0, first_token);
44708
44709 tree name = NULL;
44710 if (first_token->type == CPP_STRING)
44711 {
44712 name = first_token->u.value;
44713
44714 cp_lexer_get_preprocessor_token (0, first_token);
44715 }
44716
44717 /* Skip to the end of the pragma. */
44718 if (first_token->type != CPP_PRAGMA_EOL)
44719 {
44720 error_at (first_token->location,
44721 "malformed %<#pragma GCC pch_preprocess%>");
44722 do
44723 cp_lexer_get_preprocessor_token (0, first_token);
44724 while (first_token->type != CPP_PRAGMA_EOL);
44725 }
44726
44727 /* Now actually load the PCH file. */
44728 if (name)
44729 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
44730
44731 /* Read one more token to return to our caller. We have to do this
44732 after reading the PCH file in, since its pointers have to be
44733 live. */
44734 cp_lexer_get_preprocessor_token (0, first_token);
44735 }
44736
44737 /* Parse a pragma GCC ivdep. */
44738
44739 static bool
44740 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
44741 {
44742 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44743 return true;
44744 }
44745
44746 /* Parse a pragma GCC unroll. */
44747
44748 static unsigned short
44749 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
44750 {
44751 location_t location = cp_lexer_peek_token (parser->lexer)->location;
44752 tree expr = cp_parser_constant_expression (parser);
44753 unsigned short unroll;
44754 expr = maybe_constant_value (expr);
44755 HOST_WIDE_INT lunroll = 0;
44756 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
44757 || TREE_CODE (expr) != INTEGER_CST
44758 || (lunroll = tree_to_shwi (expr)) < 0
44759 || lunroll >= USHRT_MAX)
44760 {
44761 error_at (location, "%<#pragma GCC unroll%> requires an"
44762 " assignment-expression that evaluates to a non-negative"
44763 " integral constant less than %u", USHRT_MAX);
44764 unroll = 0;
44765 }
44766 else
44767 {
44768 unroll = (unsigned short)lunroll;
44769 if (unroll == 0)
44770 unroll = 1;
44771 }
44772 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44773 return unroll;
44774 }
44775
44776 /* Normal parsing of a pragma token. Here we can (and must) use the
44777 regular lexer. */
44778
44779 static bool
44780 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
44781 {
44782 cp_token *pragma_tok;
44783 unsigned int id;
44784 tree stmt;
44785 bool ret;
44786
44787 pragma_tok = cp_lexer_consume_token (parser->lexer);
44788 gcc_assert (pragma_tok->type == CPP_PRAGMA);
44789 parser->lexer->in_pragma = true;
44790
44791 id = cp_parser_pragma_kind (pragma_tok);
44792 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
44793 cp_ensure_no_omp_declare_simd (parser);
44794 switch (id)
44795 {
44796 case PRAGMA_GCC_PCH_PREPROCESS:
44797 error_at (pragma_tok->location,
44798 "%<#pragma GCC pch_preprocess%> must be first");
44799 break;
44800
44801 case PRAGMA_OMP_BARRIER:
44802 switch (context)
44803 {
44804 case pragma_compound:
44805 cp_parser_omp_barrier (parser, pragma_tok);
44806 return false;
44807 case pragma_stmt:
44808 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
44809 "used in compound statements", "omp barrier");
44810 break;
44811 default:
44812 goto bad_stmt;
44813 }
44814 break;
44815
44816 case PRAGMA_OMP_DEPOBJ:
44817 switch (context)
44818 {
44819 case pragma_compound:
44820 cp_parser_omp_depobj (parser, pragma_tok);
44821 return false;
44822 case pragma_stmt:
44823 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
44824 "used in compound statements", "omp depobj");
44825 break;
44826 default:
44827 goto bad_stmt;
44828 }
44829 break;
44830
44831 case PRAGMA_OMP_FLUSH:
44832 switch (context)
44833 {
44834 case pragma_compound:
44835 cp_parser_omp_flush (parser, pragma_tok);
44836 return false;
44837 case pragma_stmt:
44838 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
44839 "used in compound statements", "omp flush");
44840 break;
44841 default:
44842 goto bad_stmt;
44843 }
44844 break;
44845
44846 case PRAGMA_OMP_TASKWAIT:
44847 switch (context)
44848 {
44849 case pragma_compound:
44850 cp_parser_omp_taskwait (parser, pragma_tok);
44851 return false;
44852 case pragma_stmt:
44853 error_at (pragma_tok->location,
44854 "%<#pragma %s%> may only be used in compound statements",
44855 "omp taskwait");
44856 break;
44857 default:
44858 goto bad_stmt;
44859 }
44860 break;
44861
44862 case PRAGMA_OMP_TASKYIELD:
44863 switch (context)
44864 {
44865 case pragma_compound:
44866 cp_parser_omp_taskyield (parser, pragma_tok);
44867 return false;
44868 case pragma_stmt:
44869 error_at (pragma_tok->location,
44870 "%<#pragma %s%> may only be used in compound statements",
44871 "omp taskyield");
44872 break;
44873 default:
44874 goto bad_stmt;
44875 }
44876 break;
44877
44878 case PRAGMA_OMP_CANCEL:
44879 switch (context)
44880 {
44881 case pragma_compound:
44882 cp_parser_omp_cancel (parser, pragma_tok);
44883 return false;
44884 case pragma_stmt:
44885 error_at (pragma_tok->location,
44886 "%<#pragma %s%> may only be used in compound statements",
44887 "omp cancel");
44888 break;
44889 default:
44890 goto bad_stmt;
44891 }
44892 break;
44893
44894 case PRAGMA_OMP_CANCELLATION_POINT:
44895 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
44896 return false;
44897
44898 case PRAGMA_OMP_THREADPRIVATE:
44899 cp_parser_omp_threadprivate (parser, pragma_tok);
44900 return false;
44901
44902 case PRAGMA_OMP_DECLARE:
44903 return cp_parser_omp_declare (parser, pragma_tok, context);
44904
44905 case PRAGMA_OACC_DECLARE:
44906 cp_parser_oacc_declare (parser, pragma_tok);
44907 return false;
44908
44909 case PRAGMA_OACC_ENTER_DATA:
44910 if (context == pragma_stmt)
44911 {
44912 error_at (pragma_tok->location,
44913 "%<#pragma %s%> may only be used in compound statements",
44914 "acc enter data");
44915 break;
44916 }
44917 else if (context != pragma_compound)
44918 goto bad_stmt;
44919 cp_parser_omp_construct (parser, pragma_tok, if_p);
44920 return true;
44921
44922 case PRAGMA_OACC_EXIT_DATA:
44923 if (context == pragma_stmt)
44924 {
44925 error_at (pragma_tok->location,
44926 "%<#pragma %s%> may only be used in compound statements",
44927 "acc exit data");
44928 break;
44929 }
44930 else if (context != pragma_compound)
44931 goto bad_stmt;
44932 cp_parser_omp_construct (parser, pragma_tok, if_p);
44933 return true;
44934
44935 case PRAGMA_OACC_ROUTINE:
44936 if (context != pragma_external)
44937 {
44938 error_at (pragma_tok->location,
44939 "%<#pragma acc routine%> must be at file scope");
44940 break;
44941 }
44942 cp_parser_oacc_routine (parser, pragma_tok, context);
44943 return false;
44944
44945 case PRAGMA_OACC_UPDATE:
44946 if (context == pragma_stmt)
44947 {
44948 error_at (pragma_tok->location,
44949 "%<#pragma %s%> may only be used in compound statements",
44950 "acc update");
44951 break;
44952 }
44953 else if (context != pragma_compound)
44954 goto bad_stmt;
44955 cp_parser_omp_construct (parser, pragma_tok, if_p);
44956 return true;
44957
44958 case PRAGMA_OACC_WAIT:
44959 if (context == pragma_stmt)
44960 {
44961 error_at (pragma_tok->location,
44962 "%<#pragma %s%> may only be used in compound statements",
44963 "acc wait");
44964 break;
44965 }
44966 else if (context != pragma_compound)
44967 goto bad_stmt;
44968 cp_parser_omp_construct (parser, pragma_tok, if_p);
44969 return true;
44970 case PRAGMA_OMP_ALLOCATE:
44971 cp_parser_omp_allocate (parser, pragma_tok);
44972 return false;
44973 case PRAGMA_OACC_ATOMIC:
44974 case PRAGMA_OACC_CACHE:
44975 case PRAGMA_OACC_DATA:
44976 case PRAGMA_OACC_HOST_DATA:
44977 case PRAGMA_OACC_KERNELS:
44978 case PRAGMA_OACC_LOOP:
44979 case PRAGMA_OACC_PARALLEL:
44980 case PRAGMA_OACC_SERIAL:
44981 case PRAGMA_OMP_ATOMIC:
44982 case PRAGMA_OMP_CRITICAL:
44983 case PRAGMA_OMP_DISTRIBUTE:
44984 case PRAGMA_OMP_FOR:
44985 case PRAGMA_OMP_LOOP:
44986 case PRAGMA_OMP_MASTER:
44987 case PRAGMA_OMP_PARALLEL:
44988 case PRAGMA_OMP_SECTIONS:
44989 case PRAGMA_OMP_SIMD:
44990 case PRAGMA_OMP_SINGLE:
44991 case PRAGMA_OMP_TASK:
44992 case PRAGMA_OMP_TASKGROUP:
44993 case PRAGMA_OMP_TASKLOOP:
44994 case PRAGMA_OMP_TEAMS:
44995 if (context != pragma_stmt && context != pragma_compound)
44996 goto bad_stmt;
44997 stmt = push_omp_privatization_clauses (false);
44998 cp_parser_omp_construct (parser, pragma_tok, if_p);
44999 pop_omp_privatization_clauses (stmt);
45000 return true;
45001
45002 case PRAGMA_OMP_REQUIRES:
45003 if (context != pragma_external)
45004 {
45005 error_at (pragma_tok->location,
45006 "%<#pragma omp requires%> may only be used at file or "
45007 "namespace scope");
45008 break;
45009 }
45010 return cp_parser_omp_requires (parser, pragma_tok);
45011
45012 case PRAGMA_OMP_ORDERED:
45013 if (context != pragma_stmt && context != pragma_compound)
45014 goto bad_stmt;
45015 stmt = push_omp_privatization_clauses (false);
45016 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
45017 pop_omp_privatization_clauses (stmt);
45018 return ret;
45019
45020 case PRAGMA_OMP_TARGET:
45021 if (context != pragma_stmt && context != pragma_compound)
45022 goto bad_stmt;
45023 stmt = push_omp_privatization_clauses (false);
45024 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
45025 pop_omp_privatization_clauses (stmt);
45026 return ret;
45027
45028 case PRAGMA_OMP_END_DECLARE_TARGET:
45029 cp_parser_omp_end_declare_target (parser, pragma_tok);
45030 return false;
45031
45032 case PRAGMA_OMP_SCAN:
45033 error_at (pragma_tok->location,
45034 "%<#pragma omp scan%> may only be used in "
45035 "a loop construct with %<inscan%> %<reduction%> clause");
45036 break;
45037
45038 case PRAGMA_OMP_SECTION:
45039 error_at (pragma_tok->location,
45040 "%<#pragma omp section%> may only be used in "
45041 "%<#pragma omp sections%> construct");
45042 break;
45043
45044 case PRAGMA_IVDEP:
45045 {
45046 if (context == pragma_external)
45047 {
45048 error_at (pragma_tok->location,
45049 "%<#pragma GCC ivdep%> must be inside a function");
45050 break;
45051 }
45052 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
45053 unsigned short unroll;
45054 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
45055 if (tok->type == CPP_PRAGMA
45056 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
45057 {
45058 tok = cp_lexer_consume_token (parser->lexer);
45059 unroll = cp_parser_pragma_unroll (parser, tok);
45060 tok = cp_lexer_peek_token (the_parser->lexer);
45061 }
45062 else
45063 unroll = 0;
45064 if (tok->type != CPP_KEYWORD
45065 || (tok->keyword != RID_FOR
45066 && tok->keyword != RID_WHILE
45067 && tok->keyword != RID_DO))
45068 {
45069 cp_parser_error (parser, "for, while or do statement expected");
45070 return false;
45071 }
45072 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
45073 return true;
45074 }
45075
45076 case PRAGMA_UNROLL:
45077 {
45078 if (context == pragma_external)
45079 {
45080 error_at (pragma_tok->location,
45081 "%<#pragma GCC unroll%> must be inside a function");
45082 break;
45083 }
45084 const unsigned short unroll
45085 = cp_parser_pragma_unroll (parser, pragma_tok);
45086 bool ivdep;
45087 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
45088 if (tok->type == CPP_PRAGMA
45089 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
45090 {
45091 tok = cp_lexer_consume_token (parser->lexer);
45092 ivdep = cp_parser_pragma_ivdep (parser, tok);
45093 tok = cp_lexer_peek_token (the_parser->lexer);
45094 }
45095 else
45096 ivdep = false;
45097 if (tok->type != CPP_KEYWORD
45098 || (tok->keyword != RID_FOR
45099 && tok->keyword != RID_WHILE
45100 && tok->keyword != RID_DO))
45101 {
45102 cp_parser_error (parser, "for, while or do statement expected");
45103 return false;
45104 }
45105 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
45106 return true;
45107 }
45108
45109 default:
45110 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
45111 c_invoke_pragma_handler (id);
45112 break;
45113
45114 bad_stmt:
45115 cp_parser_error (parser, "expected declaration specifiers");
45116 break;
45117 }
45118
45119 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45120 return false;
45121 }
45122
45123 /* The interface the pragma parsers have to the lexer. */
45124
45125 enum cpp_ttype
45126 pragma_lex (tree *value, location_t *loc)
45127 {
45128 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
45129 enum cpp_ttype ret = tok->type;
45130
45131 *value = tok->u.value;
45132 if (loc)
45133 *loc = tok->location;
45134
45135 if (ret == CPP_PRAGMA_EOL)
45136 ret = CPP_EOF;
45137 else if (ret == CPP_STRING)
45138 *value = cp_parser_string_literal (the_parser, false, false);
45139 else
45140 {
45141 if (ret == CPP_KEYWORD)
45142 ret = CPP_NAME;
45143 cp_lexer_consume_token (the_parser->lexer);
45144 }
45145
45146 return ret;
45147 }
45148
45149 \f
45150 /* External interface. */
45151
45152 /* Parse one entire translation unit. */
45153
45154 void
45155 c_parse_file (void)
45156 {
45157 static bool already_called = false;
45158
45159 if (already_called)
45160 fatal_error (input_location,
45161 "multi-source compilation not implemented for C++");
45162 already_called = true;
45163
45164 /* cp_lexer_new_main is called before doing any GC allocation
45165 because tokenization might load a PCH file. */
45166 cp_lexer *lexer = cp_lexer_new_main ();
45167
45168 the_parser = cp_parser_new (lexer);
45169
45170 cp_parser_translation_unit (the_parser);
45171 class_decl_loc_t::diag_mismatched_tags ();
45172
45173 the_parser = NULL;
45174
45175 finish_translation_unit ();
45176 }
45177
45178 /* Create an identifier for a generic parameter type (a synthesized
45179 template parameter implied by `auto' or a concept identifier). */
45180
45181 static GTY(()) int generic_parm_count;
45182 static tree
45183 make_generic_type_name ()
45184 {
45185 char buf[32];
45186 sprintf (buf, "auto:%d", ++generic_parm_count);
45187 return get_identifier (buf);
45188 }
45189
45190 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
45191 (creating a new template parameter list if necessary). Returns the newly
45192 created template type parm. */
45193
45194 static tree
45195 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
45196 {
45197 /* A requires-clause is not a function and cannot have placeholders. */
45198 if (current_binding_level->kind == sk_block)
45199 {
45200 error ("placeholder type not allowed in this context");
45201 return error_mark_node;
45202 }
45203
45204 gcc_assert (current_binding_level->kind == sk_function_parms);
45205
45206 /* We are either continuing a function template that already contains implicit
45207 template parameters, creating a new fully-implicit function template, or
45208 extending an existing explicit function template with implicit template
45209 parameters. */
45210
45211 cp_binding_level *const entry_scope = current_binding_level;
45212
45213 bool become_template = false;
45214 cp_binding_level *parent_scope = 0;
45215
45216 if (parser->implicit_template_scope)
45217 {
45218 gcc_assert (parser->implicit_template_parms);
45219
45220 current_binding_level = parser->implicit_template_scope;
45221 }
45222 else
45223 {
45224 /* Roll back to the existing template parameter scope (in the case of
45225 extending an explicit function template) or introduce a new template
45226 parameter scope ahead of the function parameter scope (or class scope
45227 in the case of out-of-line member definitions). The function scope is
45228 added back after template parameter synthesis below. */
45229
45230 cp_binding_level *scope = entry_scope;
45231
45232 while (scope->kind == sk_function_parms)
45233 {
45234 parent_scope = scope;
45235 scope = scope->level_chain;
45236 }
45237 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
45238 {
45239 /* If not defining a class, then any class scope is a scope level in
45240 an out-of-line member definition. In this case simply wind back
45241 beyond the first such scope to inject the template parameter list.
45242 Otherwise wind back to the class being defined. The latter can
45243 occur in class member friend declarations such as:
45244
45245 class A {
45246 void foo (auto);
45247 };
45248 class B {
45249 friend void A::foo (auto);
45250 };
45251
45252 The template parameter list synthesized for the friend declaration
45253 must be injected in the scope of 'B'. This can also occur in
45254 erroneous cases such as:
45255
45256 struct A {
45257 struct B {
45258 void foo (auto);
45259 };
45260 void B::foo (auto) {}
45261 };
45262
45263 Here the attempted definition of 'B::foo' within 'A' is ill-formed
45264 but, nevertheless, the template parameter list synthesized for the
45265 declarator should be injected into the scope of 'A' as if the
45266 ill-formed template was specified explicitly. */
45267
45268 while (scope->kind == sk_class && !scope->defining_class_p)
45269 {
45270 parent_scope = scope;
45271 scope = scope->level_chain;
45272 }
45273 }
45274
45275 current_binding_level = scope;
45276
45277 if (scope->kind != sk_template_parms
45278 || !function_being_declared_is_template_p (parser))
45279 {
45280 /* Introduce a new template parameter list for implicit template
45281 parameters. */
45282
45283 become_template = true;
45284
45285 parser->implicit_template_scope
45286 = begin_scope (sk_template_parms, NULL);
45287
45288 ++processing_template_decl;
45289
45290 parser->fully_implicit_function_template_p = true;
45291 ++parser->num_template_parameter_lists;
45292 }
45293 else
45294 {
45295 /* Synthesize implicit template parameters at the end of the explicit
45296 template parameter list. */
45297
45298 gcc_assert (current_template_parms);
45299
45300 parser->implicit_template_scope = scope;
45301
45302 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
45303 parser->implicit_template_parms
45304 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
45305 }
45306 }
45307
45308 /* Synthesize a new template parameter and track the current template
45309 parameter chain with implicit_template_parms. */
45310
45311 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
45312 tree synth_id = make_generic_type_name ();
45313 tree synth_tmpl_parm;
45314 bool non_type = false;
45315
45316 /* Synthesize the type template parameter. */
45317 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
45318 synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
45319
45320 /* Attach the constraint to the parm before processing. */
45321 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
45322 TREE_TYPE (node) = constr;
45323 tree new_parm
45324 = process_template_parm (parser->implicit_template_parms,
45325 input_location,
45326 node,
45327 /*non_type=*/non_type,
45328 /*param_pack=*/false);
45329
45330 /* Mark the synthetic declaration "virtual". This is used when
45331 comparing template-heads to determine if whether an abbreviated
45332 function template is equivalent to an explicit template.
45333
45334 Note that DECL_ARTIFICIAL is used elsewhere for template parameters. */
45335 DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
45336
45337 // Chain the new parameter to the list of implicit parameters.
45338 if (parser->implicit_template_parms)
45339 parser->implicit_template_parms
45340 = TREE_CHAIN (parser->implicit_template_parms);
45341 else
45342 parser->implicit_template_parms = new_parm;
45343
45344 tree new_decl = get_local_decls ();
45345 if (non_type)
45346 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
45347 new_decl = DECL_INITIAL (new_decl);
45348
45349 /* If creating a fully implicit function template, start the new implicit
45350 template parameter list with this synthesized type, otherwise grow the
45351 current template parameter list. */
45352
45353 if (become_template)
45354 {
45355 parent_scope->level_chain = current_binding_level;
45356
45357 tree new_parms = make_tree_vec (1);
45358 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
45359 current_template_parms = tree_cons (size_int (processing_template_decl),
45360 new_parms, current_template_parms);
45361 }
45362 else
45363 {
45364 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
45365 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
45366 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
45367 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
45368 }
45369
45370 /* If the new parameter was constrained, we need to add that to the
45371 constraints in the template parameter list. */
45372 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
45373 {
45374 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
45375 reqs = combine_constraint_expressions (reqs, req);
45376 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
45377 }
45378
45379 current_binding_level = entry_scope;
45380
45381 return new_decl;
45382 }
45383
45384 /* Finish the declaration of a fully implicit function template. Such a
45385 template has no explicit template parameter list so has not been through the
45386 normal template head and tail processing. synthesize_implicit_template_parm
45387 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
45388 provided if the declaration is a class member such that its template
45389 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
45390 form is returned. Otherwise NULL_TREE is returned. */
45391
45392 static tree
45393 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
45394 {
45395 gcc_assert (parser->fully_implicit_function_template_p);
45396
45397 if (member_decl_opt && member_decl_opt != error_mark_node
45398 && DECL_VIRTUAL_P (member_decl_opt))
45399 {
45400 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
45401 "implicit templates may not be %<virtual%>");
45402 DECL_VIRTUAL_P (member_decl_opt) = false;
45403 }
45404
45405 if (member_decl_opt)
45406 member_decl_opt = finish_member_template_decl (member_decl_opt);
45407 end_template_decl ();
45408
45409 parser->fully_implicit_function_template_p = false;
45410 parser->implicit_template_parms = 0;
45411 parser->implicit_template_scope = 0;
45412 --parser->num_template_parameter_lists;
45413
45414 return member_decl_opt;
45415 }
45416
45417 /* Like finish_fully_implicit_template, but to be used in error
45418 recovery, rearranging scopes so that we restore the state we had
45419 before synthesize_implicit_template_parm inserted the implement
45420 template parms scope. */
45421
45422 static void
45423 abort_fully_implicit_template (cp_parser *parser)
45424 {
45425 cp_binding_level *return_to_scope = current_binding_level;
45426
45427 if (parser->implicit_template_scope
45428 && return_to_scope != parser->implicit_template_scope)
45429 {
45430 cp_binding_level *child = return_to_scope;
45431 for (cp_binding_level *scope = child->level_chain;
45432 scope != parser->implicit_template_scope;
45433 scope = child->level_chain)
45434 child = scope;
45435 child->level_chain = parser->implicit_template_scope->level_chain;
45436 parser->implicit_template_scope->level_chain = return_to_scope;
45437 current_binding_level = parser->implicit_template_scope;
45438 }
45439 else
45440 return_to_scope = return_to_scope->level_chain;
45441
45442 finish_fully_implicit_template (parser, NULL);
45443
45444 gcc_assert (current_binding_level == return_to_scope);
45445 }
45446
45447 /* Helper function for diagnostics that have complained about things
45448 being used with 'extern "C"' linkage.
45449
45450 Attempt to issue a note showing where the 'extern "C"' linkage began. */
45451
45452 void
45453 maybe_show_extern_c_location (void)
45454 {
45455 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
45456 inform (the_parser->innermost_linkage_specification_location,
45457 "%<extern \"C\"%> linkage started here");
45458 }
45459
45460 #include "gt-cp-parser.h"