eeffc2e7ae3aa528fd3ef016dbc441a60de552b0
[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 /* #pragma omp declare reduction needs special parsing. */
30827 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
30828 {
30829 parser->lexer->in_pragma = true;
30830 cp_parser_omp_declare_reduction_exprs (member_function, parser);
30831 finish_function (/*inline_p=*/true);
30832 cp_check_omp_declare_reduction (member_function);
30833 }
30834 else
30835 /* Now, parse the body of the function. */
30836 cp_parser_function_definition_after_declarator (parser,
30837 /*inline_p=*/true);
30838
30839 /* Leave the scope of the containing function. */
30840 if (function_scope)
30841 pop_function_context ();
30842 cp_parser_pop_lexer (parser);
30843 }
30844
30845 /* Remove any template parameters from the symbol table. */
30846 maybe_end_member_template_processing ();
30847
30848 /* Restore the queue. */
30849 pop_unparsed_function_queues (parser);
30850 timevar_pop (TV_PARSE_INMETH);
30851 }
30852
30853 /* If DECL contains any default args, remember it on the unparsed
30854 functions queue. */
30855
30856 static void
30857 cp_parser_save_default_args (cp_parser* parser, tree decl)
30858 {
30859 tree probe;
30860
30861 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
30862 probe;
30863 probe = TREE_CHAIN (probe))
30864 if (TREE_PURPOSE (probe))
30865 {
30866 cp_default_arg_entry entry = {current_class_type, decl};
30867 vec_safe_push (unparsed_funs_with_default_args, entry);
30868 break;
30869 }
30870
30871 /* Remember if there is a noexcept-specifier to post process. */
30872 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
30873 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
30874 vec_safe_push (unparsed_noexcepts, decl);
30875 }
30876
30877 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
30878 which is either a FIELD_DECL or PARM_DECL. Parse it and return
30879 the result. For a PARM_DECL, PARMTYPE is the corresponding type
30880 from the parameter-type-list. */
30881
30882 static tree
30883 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
30884 tree default_arg, tree parmtype)
30885 {
30886 cp_token_cache *tokens;
30887 tree parsed_arg;
30888 bool dummy;
30889
30890 if (default_arg == error_mark_node)
30891 return error_mark_node;
30892
30893 /* Push the saved tokens for the default argument onto the parser's
30894 lexer stack. */
30895 tokens = DEFPARSE_TOKENS (default_arg);
30896 cp_parser_push_lexer_for_tokens (parser, tokens);
30897
30898 start_lambda_scope (decl);
30899
30900 /* Parse the default argument. */
30901 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
30902 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
30903 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
30904
30905 finish_lambda_scope ();
30906
30907 if (parsed_arg == error_mark_node)
30908 cp_parser_skip_to_end_of_statement (parser);
30909
30910 if (!processing_template_decl)
30911 {
30912 /* In a non-template class, check conversions now. In a template,
30913 we'll wait and instantiate these as needed. */
30914 if (TREE_CODE (decl) == PARM_DECL)
30915 parsed_arg = check_default_argument (parmtype, parsed_arg,
30916 tf_warning_or_error);
30917 else if (maybe_reject_flexarray_init (decl, parsed_arg))
30918 parsed_arg = error_mark_node;
30919 else
30920 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
30921 }
30922
30923 /* If the token stream has not been completely used up, then
30924 there was extra junk after the end of the default
30925 argument. */
30926 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30927 {
30928 if (TREE_CODE (decl) == PARM_DECL)
30929 cp_parser_error (parser, "expected %<,%>");
30930 else
30931 cp_parser_error (parser, "expected %<;%>");
30932 }
30933
30934 /* Revert to the main lexer. */
30935 cp_parser_pop_lexer (parser);
30936
30937 return parsed_arg;
30938 }
30939
30940 /* FIELD is a non-static data member with an initializer which we saved for
30941 later; parse it now. */
30942
30943 static void
30944 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
30945 {
30946 tree def;
30947
30948 maybe_begin_member_template_processing (field);
30949
30950 push_unparsed_function_queues (parser);
30951 def = cp_parser_late_parse_one_default_arg (parser, field,
30952 DECL_INITIAL (field),
30953 NULL_TREE);
30954 pop_unparsed_function_queues (parser);
30955
30956 maybe_end_member_template_processing ();
30957
30958 DECL_INITIAL (field) = def;
30959 }
30960
30961 /* FN is a FUNCTION_DECL which may contains a parameter with an
30962 unparsed DEFERRED_PARSE. Parse the default args now. This function
30963 assumes that the current scope is the scope in which the default
30964 argument should be processed. */
30965
30966 static void
30967 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
30968 {
30969 unsigned char saved_local_variables_forbidden_p;
30970
30971 /* While we're parsing the default args, we might (due to the
30972 statement expression extension) encounter more classes. We want
30973 to handle them right away, but we don't want them getting mixed
30974 up with default args that are currently in the queue. */
30975 push_unparsed_function_queues (parser);
30976
30977 /* Local variable names (and the `this' keyword) may not appear
30978 in a default argument. */
30979 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
30980 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
30981
30982 push_defarg_context (fn);
30983
30984 begin_scope (sk_function_parms, fn);
30985
30986 /* Gather the PARM_DECLs into a vec so we can keep track of them when
30987 pushdecl clears DECL_CHAIN. */
30988 releasing_vec parms;
30989 for (tree parmdecl = DECL_ARGUMENTS (fn); parmdecl;
30990 parmdecl = DECL_CHAIN (parmdecl))
30991 vec_safe_push (parms, parmdecl);
30992
30993 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
30994 for (int i = 0;
30995 parm && parm != void_list_node;
30996 parm = TREE_CHAIN (parm),
30997 ++i)
30998 {
30999 tree default_arg = TREE_PURPOSE (parm);
31000 tree parsed_arg;
31001
31002 tree parmdecl = parms[i];
31003 pushdecl (parmdecl);
31004
31005 if (!default_arg)
31006 continue;
31007
31008 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
31009 /* This can happen for a friend declaration for a function
31010 already declared with default arguments. */
31011 continue;
31012
31013 parsed_arg
31014 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
31015 default_arg,
31016 TREE_VALUE (parm));
31017 TREE_PURPOSE (parm) = parsed_arg;
31018
31019 /* Update any instantiations we've already created. */
31020 for (tree copy : DEFPARSE_INSTANTIATIONS (default_arg))
31021 TREE_PURPOSE (copy) = parsed_arg;
31022 }
31023
31024 pop_bindings_and_leave_scope ();
31025
31026 /* Restore DECL_CHAINs after clobbering by pushdecl. */
31027 parm = NULL_TREE;
31028 for (int i = parms->length () - 1; i >= 0; --i)
31029 {
31030 DECL_CHAIN (parms[i]) = parm;
31031 parm = parms[i];
31032 }
31033
31034 pop_defarg_context ();
31035
31036 /* Make sure no default arg is missing. */
31037 check_default_args (fn);
31038
31039 /* Restore the state of local_variables_forbidden_p. */
31040 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
31041
31042 /* Restore the queue. */
31043 pop_unparsed_function_queues (parser);
31044 }
31045
31046 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
31047
31048 sizeof ... ( identifier )
31049
31050 where the 'sizeof' token has already been consumed. */
31051
31052 static tree
31053 cp_parser_sizeof_pack (cp_parser *parser)
31054 {
31055 /* Consume the `...'. */
31056 cp_lexer_consume_token (parser->lexer);
31057 maybe_warn_variadic_templates ();
31058
31059 matching_parens parens;
31060 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
31061 if (paren)
31062 parens.consume_open (parser);
31063 else
31064 permerror (cp_lexer_peek_token (parser->lexer)->location,
31065 "%<sizeof...%> argument must be surrounded by parentheses");
31066
31067 cp_token *token = cp_lexer_peek_token (parser->lexer);
31068 tree name = cp_parser_identifier (parser);
31069 if (name == error_mark_node)
31070 return error_mark_node;
31071 /* The name is not qualified. */
31072 parser->scope = NULL_TREE;
31073 parser->qualifying_scope = NULL_TREE;
31074 parser->object_scope = NULL_TREE;
31075 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
31076 if (expr == error_mark_node)
31077 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
31078 token->location);
31079 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
31080 expr = TREE_TYPE (expr);
31081 else if (TREE_CODE (expr) == CONST_DECL)
31082 expr = DECL_INITIAL (expr);
31083 expr = make_pack_expansion (expr);
31084 PACK_EXPANSION_SIZEOF_P (expr) = true;
31085
31086 if (paren)
31087 parens.require_close (parser);
31088
31089 return expr;
31090 }
31091
31092 /* Parse the operand of `sizeof' (or a similar operator). Returns
31093 either a TYPE or an expression, depending on the form of the
31094 input. The KEYWORD indicates which kind of expression we have
31095 encountered. */
31096
31097 static tree
31098 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
31099 {
31100 tree expr = NULL_TREE;
31101 const char *saved_message;
31102 const char *saved_message_arg;
31103 bool saved_integral_constant_expression_p;
31104 bool saved_non_integral_constant_expression_p;
31105
31106 /* If it's a `...', then we are computing the length of a parameter
31107 pack. */
31108 if (keyword == RID_SIZEOF
31109 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31110 return cp_parser_sizeof_pack (parser);
31111
31112 /* Types cannot be defined in a `sizeof' expression. Save away the
31113 old message. */
31114 saved_message = parser->type_definition_forbidden_message;
31115 saved_message_arg = parser->type_definition_forbidden_message_arg;
31116 parser->type_definition_forbidden_message
31117 = G_("types may not be defined in %qs expressions");
31118 parser->type_definition_forbidden_message_arg
31119 = IDENTIFIER_POINTER (ridpointers[keyword]);
31120
31121 /* The restrictions on constant-expressions do not apply inside
31122 sizeof expressions. */
31123 saved_integral_constant_expression_p
31124 = parser->integral_constant_expression_p;
31125 saved_non_integral_constant_expression_p
31126 = parser->non_integral_constant_expression_p;
31127 parser->integral_constant_expression_p = false;
31128
31129 /* Do not actually evaluate the expression. */
31130 ++cp_unevaluated_operand;
31131 ++c_inhibit_evaluation_warnings;
31132 /* If it's a `(', then we might be looking at the type-id
31133 construction. */
31134 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31135 {
31136 tree type = NULL_TREE;
31137
31138 tentative_firewall firewall (parser);
31139
31140 /* We can't be sure yet whether we're looking at a type-id or an
31141 expression. */
31142 cp_parser_parse_tentatively (parser);
31143
31144 matching_parens parens;
31145 parens.consume_open (parser);
31146
31147 /* Note: as a GNU Extension, compound literals are considered
31148 postfix-expressions as they are in C99, so they are valid
31149 arguments to sizeof. See comment in cp_parser_cast_expression
31150 for details. */
31151 if (cp_parser_compound_literal_p (parser))
31152 cp_parser_simulate_error (parser);
31153 else
31154 {
31155 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
31156 parser->in_type_id_in_expr_p = true;
31157 /* Look for the type-id. */
31158 type = cp_parser_type_id (parser);
31159 /* Look for the closing `)'. */
31160 parens.require_close (parser);
31161 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
31162 }
31163
31164 /* If all went well, then we're done. */
31165 if (cp_parser_parse_definitely (parser))
31166 expr = type;
31167 else
31168 {
31169 /* Commit to the tentative_firewall so we get syntax errors. */
31170 cp_parser_commit_to_tentative_parse (parser);
31171
31172 expr = cp_parser_unary_expression (parser);
31173 }
31174 }
31175 else
31176 expr = cp_parser_unary_expression (parser);
31177
31178 /* Go back to evaluating expressions. */
31179 --cp_unevaluated_operand;
31180 --c_inhibit_evaluation_warnings;
31181
31182 /* And restore the old one. */
31183 parser->type_definition_forbidden_message = saved_message;
31184 parser->type_definition_forbidden_message_arg = saved_message_arg;
31185 parser->integral_constant_expression_p
31186 = saved_integral_constant_expression_p;
31187 parser->non_integral_constant_expression_p
31188 = saved_non_integral_constant_expression_p;
31189
31190 return expr;
31191 }
31192
31193 /* If the current declaration has no declarator, return true. */
31194
31195 static bool
31196 cp_parser_declares_only_class_p (cp_parser *parser)
31197 {
31198 /* If the next token is a `;' or a `,' then there is no
31199 declarator. */
31200 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
31201 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
31202 }
31203
31204 /* Update the DECL_SPECS to reflect the storage class indicated by
31205 KEYWORD. */
31206
31207 static void
31208 cp_parser_set_storage_class (cp_parser *parser,
31209 cp_decl_specifier_seq *decl_specs,
31210 enum rid keyword,
31211 cp_token *token)
31212 {
31213 cp_storage_class storage_class;
31214
31215 if (parser->in_unbraced_linkage_specification_p)
31216 {
31217 error_at (token->location, "invalid use of %qD in linkage specification",
31218 ridpointers[keyword]);
31219 return;
31220 }
31221 else if (decl_specs->storage_class != sc_none)
31222 {
31223 decl_specs->conflicting_specifiers_p = true;
31224 return;
31225 }
31226
31227 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
31228 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
31229 && decl_specs->gnu_thread_keyword_p)
31230 {
31231 pedwarn (decl_specs->locations[ds_thread], 0,
31232 "%<__thread%> before %qD", ridpointers[keyword]);
31233 }
31234
31235 switch (keyword)
31236 {
31237 case RID_AUTO:
31238 storage_class = sc_auto;
31239 break;
31240 case RID_REGISTER:
31241 storage_class = sc_register;
31242 break;
31243 case RID_STATIC:
31244 storage_class = sc_static;
31245 break;
31246 case RID_EXTERN:
31247 storage_class = sc_extern;
31248 break;
31249 case RID_MUTABLE:
31250 storage_class = sc_mutable;
31251 break;
31252 default:
31253 gcc_unreachable ();
31254 }
31255 decl_specs->storage_class = storage_class;
31256 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
31257
31258 /* A storage class specifier cannot be applied alongside a typedef
31259 specifier. If there is a typedef specifier present then set
31260 conflicting_specifiers_p which will trigger an error later
31261 on in grokdeclarator. */
31262 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
31263 decl_specs->conflicting_specifiers_p = true;
31264 }
31265
31266 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
31267 is true, the type is a class or enum definition. */
31268
31269 static void
31270 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
31271 tree type_spec,
31272 cp_token *token,
31273 bool type_definition_p)
31274 {
31275 decl_specs->any_specifiers_p = true;
31276
31277 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
31278 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
31279 this is what happened. In system headers, we ignore these
31280 declarations so that G++ can work with system headers that are not
31281 C++-safe. */
31282 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
31283 && !type_definition_p
31284 && (type_spec == boolean_type_node
31285 || type_spec == char8_type_node
31286 || type_spec == char16_type_node
31287 || type_spec == char32_type_node
31288 || type_spec == wchar_type_node)
31289 && (decl_specs->type
31290 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
31291 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
31292 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
31293 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
31294 {
31295 decl_specs->redefined_builtin_type = type_spec;
31296 set_and_check_decl_spec_loc (decl_specs,
31297 ds_redefined_builtin_type_spec,
31298 token);
31299 if (!decl_specs->type)
31300 {
31301 decl_specs->type = type_spec;
31302 decl_specs->type_definition_p = false;
31303 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
31304 }
31305 }
31306 else if (decl_specs->type)
31307 decl_specs->multiple_types_p = true;
31308 else
31309 {
31310 decl_specs->type = type_spec;
31311 decl_specs->type_definition_p = type_definition_p;
31312 decl_specs->redefined_builtin_type = NULL_TREE;
31313 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
31314 }
31315 }
31316
31317 /* True iff TOKEN is the GNU keyword __thread. */
31318
31319 static bool
31320 token_is__thread (cp_token *token)
31321 {
31322 gcc_assert (token->keyword == RID_THREAD);
31323 return id_equal (token->u.value, "__thread");
31324 }
31325
31326 /* Set the location for a declarator specifier and check if it is
31327 duplicated.
31328
31329 DECL_SPECS is the sequence of declarator specifiers onto which to
31330 set the location.
31331
31332 DS is the single declarator specifier to set which location is to
31333 be set onto the existing sequence of declarators.
31334
31335 LOCATION is the location for the declarator specifier to
31336 consider. */
31337
31338 static void
31339 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
31340 cp_decl_spec ds, cp_token *token)
31341 {
31342 gcc_assert (ds < ds_last);
31343
31344 if (decl_specs == NULL)
31345 return;
31346
31347 location_t location = token->location;
31348
31349 if (decl_specs->locations[ds] == 0)
31350 {
31351 decl_specs->locations[ds] = location;
31352 if (ds == ds_thread)
31353 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
31354 }
31355 else
31356 {
31357 if (ds == ds_long)
31358 {
31359 if (decl_specs->locations[ds_long_long] != 0)
31360 error_at (location,
31361 "%<long long long%> is too long for GCC");
31362 else
31363 {
31364 decl_specs->locations[ds_long_long] = location;
31365 pedwarn_cxx98 (location,
31366 OPT_Wlong_long,
31367 "ISO C++ 1998 does not support %<long long%>");
31368 }
31369 }
31370 else if (ds == ds_thread)
31371 {
31372 bool gnu = token_is__thread (token);
31373 gcc_rich_location richloc (location);
31374 if (gnu != decl_specs->gnu_thread_keyword_p)
31375 {
31376 richloc.add_range (decl_specs->locations[ds_thread]);
31377 error_at (&richloc,
31378 "both %<__thread%> and %<thread_local%> specified");
31379 }
31380 else
31381 {
31382 richloc.add_fixit_remove ();
31383 error_at (&richloc, "duplicate %qD", token->u.value);
31384 }
31385 }
31386 else
31387 {
31388 static const char *const decl_spec_names[] = {
31389 "signed",
31390 "unsigned",
31391 "short",
31392 "long",
31393 "const",
31394 "volatile",
31395 "restrict",
31396 "inline",
31397 "virtual",
31398 "explicit",
31399 "friend",
31400 "typedef",
31401 "using",
31402 "constexpr",
31403 "__complex",
31404 "constinit",
31405 "consteval"
31406 };
31407 gcc_rich_location richloc (location);
31408 richloc.add_fixit_remove ();
31409 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
31410 }
31411 }
31412 }
31413
31414 /* Return true iff the declarator specifier DS is present in the
31415 sequence of declarator specifiers DECL_SPECS. */
31416
31417 bool
31418 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
31419 cp_decl_spec ds)
31420 {
31421 gcc_assert (ds < ds_last);
31422
31423 if (decl_specs == NULL)
31424 return false;
31425
31426 return decl_specs->locations[ds] != 0;
31427 }
31428
31429 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
31430 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
31431
31432 static bool
31433 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
31434 {
31435 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
31436 }
31437
31438 /* Issue an error message indicating that TOKEN_DESC was expected.
31439 If KEYWORD is true, it indicated this function is called by
31440 cp_parser_require_keword and the required token can only be
31441 a indicated keyword.
31442
31443 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
31444 within any error as the location of an "opening" token matching
31445 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
31446 RT_CLOSE_PAREN). */
31447
31448 static void
31449 cp_parser_required_error (cp_parser *parser,
31450 required_token token_desc,
31451 bool keyword,
31452 location_t matching_location)
31453 {
31454 if (cp_parser_simulate_error (parser))
31455 return;
31456
31457 const char *gmsgid = NULL;
31458 switch (token_desc)
31459 {
31460 case RT_NEW:
31461 gmsgid = G_("expected %<new%>");
31462 break;
31463 case RT_DELETE:
31464 gmsgid = G_("expected %<delete%>");
31465 break;
31466 case RT_RETURN:
31467 gmsgid = G_("expected %<return%>");
31468 break;
31469 case RT_WHILE:
31470 gmsgid = G_("expected %<while%>");
31471 break;
31472 case RT_EXTERN:
31473 gmsgid = G_("expected %<extern%>");
31474 break;
31475 case RT_STATIC_ASSERT:
31476 gmsgid = G_("expected %<static_assert%>");
31477 break;
31478 case RT_DECLTYPE:
31479 gmsgid = G_("expected %<decltype%>");
31480 break;
31481 case RT_OPERATOR:
31482 gmsgid = G_("expected %<operator%>");
31483 break;
31484 case RT_CLASS:
31485 gmsgid = G_("expected %<class%>");
31486 break;
31487 case RT_TEMPLATE:
31488 gmsgid = G_("expected %<template%>");
31489 break;
31490 case RT_NAMESPACE:
31491 gmsgid = G_("expected %<namespace%>");
31492 break;
31493 case RT_USING:
31494 gmsgid = G_("expected %<using%>");
31495 break;
31496 case RT_ASM:
31497 gmsgid = G_("expected %<asm%>");
31498 break;
31499 case RT_TRY:
31500 gmsgid = G_("expected %<try%>");
31501 break;
31502 case RT_CATCH:
31503 gmsgid = G_("expected %<catch%>");
31504 break;
31505 case RT_THROW:
31506 gmsgid = G_("expected %<throw%>");
31507 break;
31508 case RT_AUTO:
31509 gmsgid = G_("expected %<auto%>");
31510 break;
31511 case RT_LABEL:
31512 gmsgid = G_("expected %<__label__%>");
31513 break;
31514 case RT_AT_TRY:
31515 gmsgid = G_("expected %<@try%>");
31516 break;
31517 case RT_AT_SYNCHRONIZED:
31518 gmsgid = G_("expected %<@synchronized%>");
31519 break;
31520 case RT_AT_THROW:
31521 gmsgid = G_("expected %<@throw%>");
31522 break;
31523 case RT_TRANSACTION_ATOMIC:
31524 gmsgid = G_("expected %<__transaction_atomic%>");
31525 break;
31526 case RT_TRANSACTION_RELAXED:
31527 gmsgid = G_("expected %<__transaction_relaxed%>");
31528 break;
31529 case RT_CO_YIELD:
31530 gmsgid = G_("expected %<co_yield%>");
31531 break;
31532 default:
31533 break;
31534 }
31535
31536 if (!gmsgid && !keyword)
31537 {
31538 switch (token_desc)
31539 {
31540 case RT_SEMICOLON:
31541 gmsgid = G_("expected %<;%>");
31542 break;
31543 case RT_OPEN_PAREN:
31544 gmsgid = G_("expected %<(%>");
31545 break;
31546 case RT_CLOSE_BRACE:
31547 gmsgid = G_("expected %<}%>");
31548 break;
31549 case RT_OPEN_BRACE:
31550 gmsgid = G_("expected %<{%>");
31551 break;
31552 case RT_CLOSE_SQUARE:
31553 gmsgid = G_("expected %<]%>");
31554 break;
31555 case RT_OPEN_SQUARE:
31556 gmsgid = G_("expected %<[%>");
31557 break;
31558 case RT_COMMA:
31559 gmsgid = G_("expected %<,%>");
31560 break;
31561 case RT_SCOPE:
31562 gmsgid = G_("expected %<::%>");
31563 break;
31564 case RT_LESS:
31565 gmsgid = G_("expected %<<%>");
31566 break;
31567 case RT_GREATER:
31568 gmsgid = G_("expected %<>%>");
31569 break;
31570 case RT_EQ:
31571 gmsgid = G_("expected %<=%>");
31572 break;
31573 case RT_ELLIPSIS:
31574 gmsgid = G_("expected %<...%>");
31575 break;
31576 case RT_MULT:
31577 gmsgid = G_("expected %<*%>");
31578 break;
31579 case RT_COMPL:
31580 gmsgid = G_("expected %<~%>");
31581 break;
31582 case RT_COLON:
31583 gmsgid = G_("expected %<:%>");
31584 break;
31585 case RT_COLON_SCOPE:
31586 gmsgid = G_("expected %<:%> or %<::%>");
31587 break;
31588 case RT_CLOSE_PAREN:
31589 gmsgid = G_("expected %<)%>");
31590 break;
31591 case RT_COMMA_CLOSE_PAREN:
31592 gmsgid = G_("expected %<,%> or %<)%>");
31593 break;
31594 case RT_PRAGMA_EOL:
31595 gmsgid = G_("expected end of line");
31596 break;
31597 case RT_NAME:
31598 gmsgid = G_("expected identifier");
31599 break;
31600 case RT_SELECT:
31601 gmsgid = G_("expected selection-statement");
31602 break;
31603 case RT_ITERATION:
31604 gmsgid = G_("expected iteration-statement");
31605 break;
31606 case RT_JUMP:
31607 gmsgid = G_("expected jump-statement");
31608 break;
31609 case RT_CLASS_KEY:
31610 gmsgid = G_("expected class-key");
31611 break;
31612 case RT_CLASS_TYPENAME_TEMPLATE:
31613 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
31614 break;
31615 default:
31616 gcc_unreachable ();
31617 }
31618 }
31619
31620 if (gmsgid)
31621 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
31622 }
31623
31624
31625 /* If the next token is of the indicated TYPE, consume it. Otherwise,
31626 issue an error message indicating that TOKEN_DESC was expected.
31627
31628 Returns the token consumed, if the token had the appropriate type.
31629 Otherwise, returns NULL.
31630
31631 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
31632 within any error as the location of an "opening" token matching
31633 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
31634 RT_CLOSE_PAREN). */
31635
31636 static cp_token *
31637 cp_parser_require (cp_parser* parser,
31638 enum cpp_ttype type,
31639 required_token token_desc,
31640 location_t matching_location)
31641 {
31642 if (cp_lexer_next_token_is (parser->lexer, type))
31643 return cp_lexer_consume_token (parser->lexer);
31644 else
31645 {
31646 /* Output the MESSAGE -- unless we're parsing tentatively. */
31647 if (!cp_parser_simulate_error (parser))
31648 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
31649 matching_location);
31650 return NULL;
31651 }
31652 }
31653
31654 /* An error message is produced if the next token is not '>'.
31655 All further tokens are skipped until the desired token is
31656 found or '{', '}', ';' or an unbalanced ')' or ']'. */
31657
31658 static void
31659 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
31660 {
31661 /* Current level of '< ... >'. */
31662 unsigned level = 0;
31663 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
31664 unsigned nesting_depth = 0;
31665
31666 /* Are we ready, yet? If not, issue error message. */
31667 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
31668 return;
31669
31670 /* Skip tokens until the desired token is found. */
31671 while (true)
31672 {
31673 /* Peek at the next token. */
31674 switch (cp_lexer_peek_token (parser->lexer)->type)
31675 {
31676 case CPP_LESS:
31677 if (!nesting_depth)
31678 ++level;
31679 break;
31680
31681 case CPP_RSHIFT:
31682 if (cxx_dialect == cxx98)
31683 /* C++0x views the `>>' operator as two `>' tokens, but
31684 C++98 does not. */
31685 break;
31686 else if (!nesting_depth && level-- == 0)
31687 {
31688 /* We've hit a `>>' where the first `>' closes the
31689 template argument list, and the second `>' is
31690 spurious. Just consume the `>>' and stop; we've
31691 already produced at least one error. */
31692 cp_lexer_consume_token (parser->lexer);
31693 return;
31694 }
31695 /* Fall through for C++0x, so we handle the second `>' in
31696 the `>>'. */
31697 gcc_fallthrough ();
31698
31699 case CPP_GREATER:
31700 if (!nesting_depth && level-- == 0)
31701 {
31702 /* We've reached the token we want, consume it and stop. */
31703 cp_lexer_consume_token (parser->lexer);
31704 return;
31705 }
31706 break;
31707
31708 case CPP_OPEN_PAREN:
31709 case CPP_OPEN_SQUARE:
31710 ++nesting_depth;
31711 break;
31712
31713 case CPP_CLOSE_PAREN:
31714 case CPP_CLOSE_SQUARE:
31715 if (nesting_depth-- == 0)
31716 return;
31717 break;
31718
31719 case CPP_EOF:
31720 case CPP_PRAGMA_EOL:
31721 case CPP_SEMICOLON:
31722 case CPP_OPEN_BRACE:
31723 case CPP_CLOSE_BRACE:
31724 /* The '>' was probably forgotten, don't look further. */
31725 return;
31726
31727 default:
31728 break;
31729 }
31730
31731 /* Consume this token. */
31732 cp_lexer_consume_token (parser->lexer);
31733 }
31734 }
31735
31736 /* If the next token is the indicated keyword, consume it. Otherwise,
31737 issue an error message indicating that TOKEN_DESC was expected.
31738
31739 Returns the token consumed, if the token had the appropriate type.
31740 Otherwise, returns NULL. */
31741
31742 static cp_token *
31743 cp_parser_require_keyword (cp_parser* parser,
31744 enum rid keyword,
31745 required_token token_desc)
31746 {
31747 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
31748
31749 if (token && token->keyword != keyword)
31750 {
31751 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
31752 UNKNOWN_LOCATION);
31753 return NULL;
31754 }
31755
31756 return token;
31757 }
31758
31759 /* Returns TRUE iff TOKEN is a token that can begin the body of a
31760 function-definition. */
31761
31762 static bool
31763 cp_parser_token_starts_function_definition_p (cp_token* token)
31764 {
31765 return (/* An ordinary function-body begins with an `{'. */
31766 token->type == CPP_OPEN_BRACE
31767 /* A ctor-initializer begins with a `:'. */
31768 || token->type == CPP_COLON
31769 /* A function-try-block begins with `try'. */
31770 || token->keyword == RID_TRY
31771 /* A function-transaction-block begins with `__transaction_atomic'
31772 or `__transaction_relaxed'. */
31773 || token->keyword == RID_TRANSACTION_ATOMIC
31774 || token->keyword == RID_TRANSACTION_RELAXED
31775 /* The named return value extension begins with `return'. */
31776 || token->keyword == RID_RETURN);
31777 }
31778
31779 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
31780 definition. */
31781
31782 static bool
31783 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
31784 {
31785 cp_token *token;
31786
31787 token = cp_lexer_peek_token (parser->lexer);
31788 return (token->type == CPP_OPEN_BRACE
31789 || (token->type == CPP_COLON
31790 && !parser->colon_doesnt_start_class_def_p));
31791 }
31792
31793 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
31794 C++0x) ending a template-argument. */
31795
31796 static bool
31797 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
31798 {
31799 cp_token *token;
31800
31801 token = cp_lexer_peek_token (parser->lexer);
31802 return (token->type == CPP_COMMA
31803 || token->type == CPP_GREATER
31804 || token->type == CPP_ELLIPSIS
31805 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
31806 }
31807
31808 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
31809 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
31810
31811 static bool
31812 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
31813 size_t n)
31814 {
31815 cp_token *token;
31816
31817 token = cp_lexer_peek_nth_token (parser->lexer, n);
31818 if (token->type == CPP_LESS)
31819 return true;
31820 /* Check for the sequence `<::' in the original code. It would be lexed as
31821 `[:', where `[' is a digraph, and there is no whitespace before
31822 `:'. */
31823 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
31824 {
31825 cp_token *token2;
31826 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
31827 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
31828 return true;
31829 }
31830 return false;
31831 }
31832
31833 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
31834 or none_type otherwise. */
31835
31836 static enum tag_types
31837 cp_parser_token_is_class_key (cp_token* token)
31838 {
31839 switch (token->keyword)
31840 {
31841 case RID_CLASS:
31842 return class_type;
31843 case RID_STRUCT:
31844 return record_type;
31845 case RID_UNION:
31846 return union_type;
31847
31848 default:
31849 return none_type;
31850 }
31851 }
31852
31853 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
31854 or none_type otherwise or if the token is null. */
31855
31856 static enum tag_types
31857 cp_parser_token_is_type_parameter_key (cp_token* token)
31858 {
31859 if (!token)
31860 return none_type;
31861
31862 switch (token->keyword)
31863 {
31864 case RID_CLASS:
31865 return class_type;
31866 case RID_TYPENAME:
31867 return typename_type;
31868
31869 default:
31870 return none_type;
31871 }
31872 }
31873
31874 /* Diagnose redundant enum-keys. */
31875
31876 static void
31877 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
31878 tree type, rid scoped_key)
31879 {
31880 if (!warn_redundant_tags)
31881 return;
31882
31883 tree type_decl = TYPE_MAIN_DECL (type);
31884 tree name = DECL_NAME (type_decl);
31885 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
31886 push_deferring_access_checks (dk_no_check);
31887 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
31888 pop_deferring_access_checks ();
31889
31890 /* The enum-key is redundant for uses of the TYPE that are not
31891 declarations and for which name lookup returns just the type
31892 itself. */
31893 if (decl != type_decl)
31894 return;
31895
31896 if (scoped_key != RID_CLASS
31897 && scoped_key != RID_STRUCT
31898 && current_lang_name != lang_name_cplusplus
31899 && current_namespace == global_namespace)
31900 {
31901 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
31902 enum tag in shared C/C++ code in files (such as headers) included
31903 in the main source file. */
31904 const line_map_ordinary *map = NULL;
31905 linemap_resolve_location (line_table, key_loc,
31906 LRK_MACRO_DEFINITION_LOCATION,
31907 &map);
31908 if (!MAIN_FILE_P (map))
31909 return;
31910 }
31911
31912 gcc_rich_location richloc (key_loc);
31913 richloc.add_fixit_remove (key_loc);
31914 warning_at (&richloc, OPT_Wredundant_tags,
31915 "redundant enum-key %<enum%s%> in reference to %q#T",
31916 (scoped_key == RID_CLASS ? " class"
31917 : scoped_key == RID_STRUCT ? " struct" : ""), type);
31918 }
31919
31920 /* Describes the set of declarations of a struct, class, or class template
31921 or its specializations. Used for -Wmismatched-tags. */
31922
31923 class class_decl_loc_t
31924 {
31925 public:
31926
31927 class_decl_loc_t ()
31928 : locvec (), idxdef (), def_class_key ()
31929 {
31930 locvec.create (4);
31931 }
31932
31933 /* Constructs an object for a single declaration of a class with
31934 CLASS_KEY at the current location in the current function (or
31935 at another scope). KEY_REDUNDANT is true if the class-key may
31936 be omitted in the current context without an ambiguity with
31937 another symbol with the same name.
31938 DEF_P is true for a class declaration that is a definition.
31939 CURLOC is the associated location. */
31940 class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
31941 location_t curloc = input_location)
31942 : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
31943 {
31944 locvec.create (4);
31945 class_key_loc_t ckl (current_function_decl, curloc, class_key,
31946 key_redundant);
31947 locvec.quick_push (ckl);
31948 }
31949
31950 /* Copy, assign, and destroy the object. Necessary because LOCVEC
31951 isn't safely copyable and assignable and doesn't release storage
31952 on its own. */
31953 class_decl_loc_t (const class_decl_loc_t &rhs)
31954 : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
31955 def_class_key (rhs.def_class_key)
31956 { }
31957
31958 class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
31959 {
31960 if (this == &rhs)
31961 return *this;
31962 locvec.release ();
31963 locvec = rhs.locvec.copy ();
31964 idxdef = rhs.idxdef;
31965 def_class_key = rhs.def_class_key;
31966 return *this;
31967 }
31968
31969 ~class_decl_loc_t ()
31970 {
31971 locvec.release ();
31972 }
31973
31974 /* Issues -Wmismatched-tags for a single class. */
31975 void diag_mismatched_tags (tree);
31976
31977 /* Issues -Wmismatched-tags for all classes. */
31978 static void diag_mismatched_tags ();
31979
31980 /* Adds TYPE_DECL to the collection of class decls and diagnoses
31981 redundant tags (if -Wredundant-tags is enabled). */
31982 static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
31983
31984 /* Either adds this decl to the collection of class decls
31985 or diagnoses it, whichever is appropriate. */
31986 void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
31987
31988 private:
31989
31990 tree function (unsigned i) const
31991 {
31992 return locvec[i].func;
31993 }
31994
31995 location_t location (unsigned i) const
31996 {
31997 return locvec[i].loc;
31998 }
31999
32000 bool key_redundant (unsigned i) const
32001 {
32002 return locvec[i].key_redundant;
32003 }
32004
32005 tag_types class_key (unsigned i) const
32006 {
32007 return locvec[i].class_key;
32008 }
32009
32010 /* True if a definition for the class has been seen. */
32011 bool def_p () const
32012 {
32013 return idxdef < locvec.length ();
32014 }
32015
32016 /* The location of a single mention of a class type with the given
32017 class-key. */
32018 struct class_key_loc_t
32019 {
32020 class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
32021 : func (func), loc (loc), class_key (key), key_redundant (redundant)
32022 { }
32023
32024 /* The function the type is mentioned in. */
32025 tree func;
32026 /* The exact location. */
32027 location_t loc;
32028 /* The class-key used in the mention of the type. */
32029 tag_types class_key;
32030 /* True when the class-key could be omitted at this location
32031 without an ambiguity with another symbol of the same name. */
32032 bool key_redundant;
32033 };
32034 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
32035 vec <class_key_loc_t> locvec;
32036 /* LOCVEC index of the definition or UINT_MAX if none exists. */
32037 unsigned idxdef;
32038 /* The class-key the class was last declared with or none_type when
32039 it has been declared with a mismatched key. */
32040 tag_types def_class_key;
32041
32042 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
32043 description above. */
32044 typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
32045 static class_to_loc_map_t class2loc;
32046 };
32047
32048 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
32049
32050 /* Issue an error message if the CLASS_KEY does not match the TYPE.
32051 DEF_P is expected to be set for a definition of class TYPE. DECL_P
32052 is set for a declaration of class TYPE and clear for a reference to
32053 it that is not a declaration of it. */
32054
32055 static void
32056 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
32057 tag_types class_key, tree type, bool def_p,
32058 bool decl_p)
32059 {
32060 if (type == error_mark_node)
32061 return;
32062
32063 bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
32064 if (seen_as_union != (class_key == union_type))
32065 {
32066 if (permerror (input_location, "%qs tag used in naming %q#T",
32067 class_key == union_type ? "union"
32068 : class_key == record_type ? "struct" : "class",
32069 type))
32070 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
32071 "%q#T was previously declared here", type);
32072 return;
32073 }
32074
32075 if (!warn_mismatched_tags && !warn_redundant_tags)
32076 return;
32077
32078 /* Only consider the true class-keys below and ignore typename_type,
32079 etc. that are not C++ class-keys. */
32080 if (class_key != class_type
32081 && class_key != record_type
32082 && class_key != union_type)
32083 return;
32084
32085 class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
32086 }
32087
32088 /* Returns the template or specialization of one to which the RECORD_TYPE
32089 TYPE corresponds. */
32090
32091 static tree
32092 specialization_of (tree type)
32093 {
32094 tree ret = type;
32095
32096 /* Determine the template or its partial specialization to which TYPE
32097 corresponds. */
32098 if (tree spec = most_specialized_partial_spec (type, tf_none))
32099 if (spec != error_mark_node)
32100 ret = TREE_TYPE (TREE_VALUE (spec));
32101
32102 if (ret == type)
32103 ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
32104
32105 return TYPE_MAIN_DECL (ret);
32106 }
32107
32108
32109 /* Adds the class TYPE to the collection of class decls and diagnoses
32110 redundant tags (if -Wredundant-tags is enabled).
32111 DEF_P is expected to be set for a definition of class TYPE. DECL_P
32112 is set for a (likely, based on syntactic context) declaration of class
32113 TYPE and clear for a reference to it that is not a declaration of it. */
32114
32115 void
32116 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
32117 tag_types class_key, tree type, bool def_p, bool decl_p)
32118 {
32119 tree type_decl = TYPE_MAIN_DECL (type);
32120 tree name = DECL_NAME (type_decl);
32121 /* Look up the NAME to see if it unambiguously refers to the TYPE
32122 and set KEY_REDUNDANT if so. */
32123 push_deferring_access_checks (dk_no_check);
32124 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
32125 pop_deferring_access_checks ();
32126
32127 /* The class-key is redundant for uses of the CLASS_TYPE that are
32128 neither definitions of it nor declarations, and for which name
32129 lookup returns just the type itself. */
32130 bool key_redundant = (!def_p && !decl_p
32131 && (decl == type_decl
32132 || TREE_CODE (decl) == TEMPLATE_DECL
32133 || TYPE_BEING_DEFINED (type)));
32134
32135 if (key_redundant
32136 && class_key != class_type
32137 && current_lang_name != lang_name_cplusplus
32138 && current_namespace == global_namespace)
32139 {
32140 /* Avoid issuing the diagnostic for apparently redundant struct
32141 and union class-keys in shared C/C++ code in files (such as
32142 headers) included in the main source file. */
32143 const line_map_ordinary *map = NULL;
32144 linemap_resolve_location (line_table, key_loc,
32145 LRK_MACRO_DEFINITION_LOCATION,
32146 &map);
32147 if (!MAIN_FILE_P (map))
32148 key_redundant = false;
32149 }
32150
32151 /* Set if a declaration of TYPE has previously been seen or if it must
32152 exist in a precompiled header. */
32153 bool exist;
32154 class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
32155 if (!exist)
32156 {
32157 tree type = TREE_TYPE (type_decl);
32158 if (def_p || !COMPLETE_TYPE_P (type))
32159 {
32160 /* TYPE_DECL is the first declaration or definition of the type
32161 (outside precompiled headers -- see below). Just create
32162 a new entry for it and return unless it's a declaration
32163 involving a template that may need to be diagnosed by
32164 -Wredundant-tags. */
32165 *rdl = class_decl_loc_t (class_key, false, def_p);
32166 if (TREE_CODE (decl) != TEMPLATE_DECL)
32167 return;
32168 }
32169 else
32170 {
32171 /* TYPE was previously defined in some unknown precompiled hdeader.
32172 Simply add a record of its definition at an unknown location and
32173 proceed below to add a reference to it at the current location.
32174 (Declarations in precompiled headers that are not definitions
32175 are ignored.) */
32176 tag_types def_key
32177 = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
32178 location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
32179 *rdl = class_decl_loc_t (def_key, false, true, def_loc);
32180 exist = true;
32181 }
32182 }
32183
32184 /* A prior declaration of TYPE_DECL has been seen. */
32185
32186 if (key_redundant)
32187 {
32188 gcc_rich_location richloc (key_loc);
32189 richloc.add_fixit_remove (key_loc);
32190 warning_at (&richloc, OPT_Wredundant_tags,
32191 "redundant class-key %qs in reference to %q#T",
32192 class_key == union_type ? "union"
32193 : class_key == record_type ? "struct" : "class",
32194 type);
32195 }
32196
32197 if (!exist)
32198 /* Do nothing if this is the first declaration of the type. */
32199 return;
32200
32201 if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
32202 /* Do nothing if the class-key in this declaration matches
32203 the definition. */
32204 return;
32205
32206 rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
32207 def_p);
32208 }
32209
32210 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
32211 of class decls or diagnoses it, whichever is appropriate. */
32212
32213 void
32214 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
32215 tag_types class_key,
32216 bool redundant,
32217 bool def_p)
32218 {
32219 /* Reset the CLASS_KEY associated with this type on mismatch.
32220 This is an optimization that lets the diagnostic code skip
32221 over classes that use the same class-key in all declarations. */
32222 if (def_class_key != class_key)
32223 def_class_key = none_type;
32224
32225 /* Set IDXDEF to the index of the vector corresponding to
32226 the definition. */
32227 if (def_p)
32228 idxdef = locvec.length ();
32229
32230 /* Append a record of this declaration to the vector. */
32231 class_key_loc_t ckl (current_function_decl, input_location, class_key,
32232 redundant);
32233 locvec.safe_push (ckl);
32234
32235 if (idxdef == UINT_MAX)
32236 return;
32237
32238 /* As a space optimization diagnose declarations of a class
32239 whose definition has been seen and purge the LOCVEC of
32240 all entries except the definition. */
32241 diag_mismatched_tags (type_decl);
32242 if (idxdef)
32243 {
32244 class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
32245 locvec.release ();
32246 locvec.reserve (2);
32247 locvec.safe_push (ent);
32248 idxdef = 0;
32249 }
32250 else
32251 /* Pop the entry pushed above for this declaration. */
32252 locvec.pop ();
32253 }
32254
32255 /* Issues -Wmismatched-tags for a single class. */
32256
32257 void
32258 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
32259 {
32260 if (!warn_mismatched_tags)
32261 return;
32262
32263 /* Number of uses of the class. */
32264 const unsigned ndecls = locvec.length ();
32265
32266 /* The class (or template) declaration guiding the decisions about
32267 the diagnostic. For ordinary classes it's the same as THIS. For
32268 uses of instantiations of templates other than their declarations
32269 it points to the record for the declaration of the corresponding
32270 primary template or partial specialization. */
32271 class_decl_loc_t *cdlguide = this;
32272
32273 tree type = TREE_TYPE (type_decl);
32274 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type))
32275 {
32276 /* For implicit instantiations of a primary template look up
32277 the primary or partial specialization and use it as
32278 the expected class-key rather than using the class-key of
32279 the first reference to the instantiation. The primary must
32280 be (and inevitably is) at index zero. */
32281 tree spec = specialization_of (type);
32282 cdlguide = class2loc.get (spec);
32283 gcc_assert (cdlguide != NULL);
32284 }
32285 else
32286 {
32287 /* Skip declarations that consistently use the same class-key. */
32288 if (def_class_key != none_type)
32289 return;
32290 }
32291
32292 /* Set if a definition for the class has been seen. */
32293 const bool def_p = cdlguide->def_p ();
32294
32295 /* The index of the declaration whose class-key this declaration
32296 is expected to match. It's either the class-key of the class
32297 definition if one exists or the first declaration otherwise. */
32298 const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
32299
32300 /* The class-key the class is expected to be declared with: it's
32301 either the key used in its definition or the first declaration
32302 if no definition has been provided.
32303 For implicit instantiations of a primary template it's
32304 the class-key used to declare the primary with. The primary
32305 must be at index zero. */
32306 const tag_types xpect_key = cdlguide->class_key (idxguide);
32307
32308 unsigned idx = 0;
32309 /* Advance IDX to the first declaration that either is not
32310 a definition or that doesn't match the first declaration
32311 if no definition is provided. */
32312 while (class_key (idx) == xpect_key)
32313 if (++idx == ndecls)
32314 return;
32315
32316 /* Save the current function before changing it below. */
32317 tree save_func = current_function_decl;
32318 /* Set the function declaration to print in diagnostic context. */
32319 current_function_decl = function (idx);
32320
32321 const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
32322 const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
32323
32324 location_t loc = location (idx);
32325 bool key_redundant_p = key_redundant (idx);
32326 auto_diagnostic_group d;
32327 /* Issue a warning for the first mismatched declaration.
32328 Avoid using "%#qT" since the class-key for the same type will
32329 be the same regardless of which one was used in the declaraion. */
32330 if (warning_at (loc, OPT_Wmismatched_tags,
32331 "%qT declared with a mismatched class-key %qs",
32332 type_decl, xmatchkstr))
32333 {
32334 /* Suggest how to avoid the warning for each instance since
32335 the guidance may be different depending on context. */
32336 inform (loc,
32337 (key_redundant_p
32338 ? G_("remove the class-key or replace it with %qs")
32339 : G_("replace the class-key with %qs")),
32340 xpectkstr);
32341
32342 /* Also point to the first declaration or definition that guided
32343 the decision to issue the warning above. */
32344 inform (cdlguide->location (idxguide),
32345 (def_p
32346 ? G_("%qT defined as %qs here")
32347 : G_("%qT first declared as %qs here")),
32348 type_decl, xpectkstr);
32349 }
32350
32351 /* Issue warnings for the remaining inconsistent declarations. */
32352 for (unsigned i = idx + 1; i != ndecls; ++i)
32353 {
32354 tag_types clskey = class_key (i);
32355 /* Skip over the declarations that match either the definition
32356 if one was provided or the first declaration. */
32357 if (clskey == xpect_key)
32358 continue;
32359
32360 loc = location (i);
32361 key_redundant_p = key_redundant (i);
32362 /* Set the function declaration to print in diagnostic context. */
32363 current_function_decl = function (i);
32364 if (warning_at (loc, OPT_Wmismatched_tags,
32365 "%qT declared with a mismatched class-key %qs",
32366 type_decl, xmatchkstr))
32367 /* Suggest how to avoid the warning for each instance since
32368 the guidance may be different depending on context. */
32369 inform (loc,
32370 (key_redundant_p
32371 ? G_("remove the class-key or replace it with %qs")
32372 : G_("replace the class-key with %qs")),
32373 xpectkstr);
32374 }
32375
32376 /* Restore the current function in case it was replaced above. */
32377 current_function_decl = save_func;
32378 }
32379
32380 /* Issues -Wmismatched-tags for all classes. Called at the end
32381 of processing a translation unit, after declarations of all class
32382 types and their uses have been recorded. */
32383
32384 void
32385 class_decl_loc_t::diag_mismatched_tags ()
32386 {
32387 /* CLASS2LOC should be empty if both -Wmismatched-tags and
32388 -Wredundant-tags are disabled. */
32389 gcc_assert (warn_mismatched_tags
32390 || warn_redundant_tags
32391 || class2loc.is_empty ());
32392
32393 /* Save the current function before changing on return. It should
32394 be null at this point. */
32395 temp_override<tree> cleanup (current_function_decl);
32396
32397 if (warn_mismatched_tags)
32398 {
32399 /* Iterate over the collected class/struct/template declarations. */
32400 typedef class_to_loc_map_t::iterator iter_t;
32401 for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
32402 {
32403 tree type_decl = (*it).first;
32404 class_decl_loc_t &recloc = (*it).second;
32405 recloc.diag_mismatched_tags (type_decl);
32406 }
32407 }
32408
32409 class2loc.empty ();
32410 }
32411
32412 /* Issue an error message if DECL is redeclared with different
32413 access than its original declaration [class.access.spec/3].
32414 This applies to nested classes, nested class templates and
32415 enumerations [class.mem/1]. */
32416
32417 static void
32418 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
32419 {
32420 if (!decl
32421 || (!CLASS_TYPE_P (TREE_TYPE (decl))
32422 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
32423 return;
32424
32425 if ((TREE_PRIVATE (decl)
32426 != (current_access_specifier == access_private_node))
32427 || (TREE_PROTECTED (decl)
32428 != (current_access_specifier == access_protected_node)))
32429 error_at (location, "%qD redeclared with different access", decl);
32430 }
32431
32432 /* Look for the `template' keyword, as a syntactic disambiguator.
32433 Return TRUE iff it is present, in which case it will be
32434 consumed. */
32435
32436 static bool
32437 cp_parser_optional_template_keyword (cp_parser *parser)
32438 {
32439 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
32440 {
32441 /* In C++98 the `template' keyword can only be used within templates;
32442 outside templates the parser can always figure out what is a
32443 template and what is not. In C++11, per the resolution of DR 468,
32444 `template' is allowed in cases where it is not strictly necessary. */
32445 if (!processing_template_decl
32446 && pedantic && cxx_dialect == cxx98)
32447 {
32448 cp_token *token = cp_lexer_peek_token (parser->lexer);
32449 pedwarn (token->location, OPT_Wpedantic,
32450 "in C++98 %<template%> (as a disambiguator) is only "
32451 "allowed within templates");
32452 /* If this part of the token stream is rescanned, the same
32453 error message would be generated. So, we purge the token
32454 from the stream. */
32455 cp_lexer_purge_token (parser->lexer);
32456 return false;
32457 }
32458 else
32459 {
32460 /* Consume the `template' keyword. */
32461 cp_lexer_consume_token (parser->lexer);
32462 return true;
32463 }
32464 }
32465 return false;
32466 }
32467
32468 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
32469 set PARSER->SCOPE, and perform other related actions. */
32470
32471 static void
32472 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
32473 {
32474 struct tree_check *check_value;
32475
32476 /* Get the stored value. */
32477 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
32478 /* Set the scope from the stored value. */
32479 parser->scope = saved_checks_value (check_value);
32480 parser->qualifying_scope = check_value->qualifying_scope;
32481 parser->object_scope = NULL_TREE;
32482 }
32483
32484 /* Consume tokens up through a non-nested END token. Returns TRUE if we
32485 encounter the end of a block before what we were looking for. */
32486
32487 static bool
32488 cp_parser_cache_group (cp_parser *parser,
32489 enum cpp_ttype end,
32490 unsigned depth)
32491 {
32492 while (true)
32493 {
32494 cp_token *token = cp_lexer_peek_token (parser->lexer);
32495
32496 /* Abort a parenthesized expression if we encounter a semicolon. */
32497 if ((end == CPP_CLOSE_PAREN || depth == 0)
32498 && token->type == CPP_SEMICOLON)
32499 return true;
32500 /* If we've reached the end of the file, stop. */
32501 if (token->type == CPP_EOF
32502 || (end != CPP_PRAGMA_EOL
32503 && token->type == CPP_PRAGMA_EOL))
32504 return true;
32505 if (token->type == CPP_CLOSE_BRACE && depth == 0)
32506 /* We've hit the end of an enclosing block, so there's been some
32507 kind of syntax error. */
32508 return true;
32509
32510 /* Consume the token. */
32511 cp_lexer_consume_token (parser->lexer);
32512 /* See if it starts a new group. */
32513 if (token->type == CPP_OPEN_BRACE)
32514 {
32515 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
32516 /* In theory this should probably check end == '}', but
32517 cp_parser_save_member_function_body needs it to exit
32518 after either '}' or ')' when called with ')'. */
32519 if (depth == 0)
32520 return false;
32521 }
32522 else if (token->type == CPP_OPEN_PAREN)
32523 {
32524 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
32525 if (depth == 0 && end == CPP_CLOSE_PAREN)
32526 return false;
32527 }
32528 else if (token->type == CPP_PRAGMA)
32529 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
32530 else if (token->type == end)
32531 return false;
32532 }
32533 }
32534
32535 /* Like above, for caching a default argument or NSDMI. Both of these are
32536 terminated by a non-nested comma, but it can be unclear whether or not a
32537 comma is nested in a template argument list unless we do more parsing.
32538 In order to handle this ambiguity, when we encounter a ',' after a '<'
32539 we try to parse what follows as a parameter-declaration-list (in the
32540 case of a default argument) or a member-declarator (in the case of an
32541 NSDMI). If that succeeds, then we stop caching. */
32542
32543 static tree
32544 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
32545 {
32546 unsigned depth = 0;
32547 int maybe_template_id = 0;
32548 cp_token *first_token;
32549 cp_token *token;
32550 tree default_argument;
32551
32552 /* Add tokens until we have processed the entire default
32553 argument. We add the range [first_token, token). */
32554 first_token = cp_lexer_peek_token (parser->lexer);
32555 if (first_token->type == CPP_OPEN_BRACE)
32556 {
32557 /* For list-initialization, this is straightforward. */
32558 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
32559 token = cp_lexer_peek_token (parser->lexer);
32560 }
32561 else while (true)
32562 {
32563 bool done = false;
32564
32565 /* Peek at the next token. */
32566 token = cp_lexer_peek_token (parser->lexer);
32567 /* What we do depends on what token we have. */
32568 switch (token->type)
32569 {
32570 /* In valid code, a default argument must be
32571 immediately followed by a `,' `)', or `...'. */
32572 case CPP_COMMA:
32573 if (depth == 0 && maybe_template_id)
32574 {
32575 /* If we've seen a '<', we might be in a
32576 template-argument-list. Until Core issue 325 is
32577 resolved, we don't know how this situation ought
32578 to be handled, so try to DTRT. We check whether
32579 what comes after the comma is a valid parameter
32580 declaration list. If it is, then the comma ends
32581 the default argument; otherwise the default
32582 argument continues. */
32583 bool error = false;
32584 cp_token *peek;
32585
32586 /* Set ITALP so cp_parser_parameter_declaration_list
32587 doesn't decide to commit to this parse. */
32588 bool saved_italp = parser->in_template_argument_list_p;
32589 parser->in_template_argument_list_p = true;
32590
32591 cp_parser_parse_tentatively (parser);
32592
32593 if (nsdmi)
32594 {
32595 /* Parse declarators until we reach a non-comma or
32596 somthing that cannot be an initializer.
32597 Just checking whether we're looking at a single
32598 declarator is insufficient. Consider:
32599 int var = tuple<T,U>::x;
32600 The template parameter 'U' looks exactly like a
32601 declarator. */
32602 do
32603 {
32604 int ctor_dtor_or_conv_p;
32605 cp_lexer_consume_token (parser->lexer);
32606 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
32607 CP_PARSER_FLAGS_NONE,
32608 &ctor_dtor_or_conv_p,
32609 /*parenthesized_p=*/NULL,
32610 /*member_p=*/true,
32611 /*friend_p=*/false,
32612 /*static_p=*/false);
32613 peek = cp_lexer_peek_token (parser->lexer);
32614 if (cp_parser_error_occurred (parser))
32615 break;
32616 }
32617 while (peek->type == CPP_COMMA);
32618 /* If we met an '=' or ';' then the original comma
32619 was the end of the NSDMI. Otherwise assume
32620 we're still in the NSDMI. */
32621 error = (peek->type != CPP_EQ
32622 && peek->type != CPP_SEMICOLON);
32623 }
32624 else
32625 {
32626 cp_lexer_consume_token (parser->lexer);
32627 begin_scope (sk_function_parms, NULL_TREE);
32628 tree t = cp_parser_parameter_declaration_list
32629 (parser, CP_PARSER_FLAGS_NONE);
32630 if (t == error_mark_node)
32631 error = true;
32632 pop_bindings_and_leave_scope ();
32633 }
32634 if (!cp_parser_error_occurred (parser) && !error)
32635 done = true;
32636 cp_parser_abort_tentative_parse (parser);
32637
32638 parser->in_template_argument_list_p = saved_italp;
32639 break;
32640 }
32641 /* FALLTHRU */
32642 case CPP_CLOSE_PAREN:
32643 case CPP_ELLIPSIS:
32644 /* If we run into a non-nested `;', `}', or `]',
32645 then the code is invalid -- but the default
32646 argument is certainly over. */
32647 case CPP_SEMICOLON:
32648 case CPP_CLOSE_BRACE:
32649 case CPP_CLOSE_SQUARE:
32650 if (depth == 0
32651 /* Handle correctly int n = sizeof ... ( p ); */
32652 && token->type != CPP_ELLIPSIS)
32653 done = true;
32654 /* Update DEPTH, if necessary. */
32655 else if (token->type == CPP_CLOSE_PAREN
32656 || token->type == CPP_CLOSE_BRACE
32657 || token->type == CPP_CLOSE_SQUARE)
32658 --depth;
32659 break;
32660
32661 case CPP_OPEN_PAREN:
32662 case CPP_OPEN_SQUARE:
32663 case CPP_OPEN_BRACE:
32664 ++depth;
32665 break;
32666
32667 case CPP_LESS:
32668 if (depth == 0)
32669 /* This might be the comparison operator, or it might
32670 start a template argument list. */
32671 ++maybe_template_id;
32672 break;
32673
32674 case CPP_RSHIFT:
32675 if (cxx_dialect == cxx98)
32676 break;
32677 /* Fall through for C++0x, which treats the `>>'
32678 operator like two `>' tokens in certain
32679 cases. */
32680 gcc_fallthrough ();
32681
32682 case CPP_GREATER:
32683 if (depth == 0)
32684 {
32685 /* This might be an operator, or it might close a
32686 template argument list. But if a previous '<'
32687 started a template argument list, this will have
32688 closed it, so we can't be in one anymore. */
32689 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
32690 if (maybe_template_id < 0)
32691 maybe_template_id = 0;
32692 }
32693 break;
32694
32695 /* If we run out of tokens, issue an error message. */
32696 case CPP_EOF:
32697 case CPP_PRAGMA_EOL:
32698 error_at (token->location, "file ends in default argument");
32699 return error_mark_node;
32700
32701 case CPP_NAME:
32702 case CPP_SCOPE:
32703 /* In these cases, we should look for template-ids.
32704 For example, if the default argument is
32705 `X<int, double>()', we need to do name lookup to
32706 figure out whether or not `X' is a template; if
32707 so, the `,' does not end the default argument.
32708
32709 That is not yet done. */
32710 break;
32711
32712 default:
32713 break;
32714 }
32715
32716 /* If we've reached the end, stop. */
32717 if (done)
32718 break;
32719
32720 /* Add the token to the token block. */
32721 token = cp_lexer_consume_token (parser->lexer);
32722 }
32723
32724 /* Create a DEFERRED_PARSE to represent the unparsed default
32725 argument. */
32726 default_argument = make_node (DEFERRED_PARSE);
32727 DEFPARSE_TOKENS (default_argument)
32728 = cp_token_cache_new (first_token, token);
32729 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
32730
32731 return default_argument;
32732 }
32733
32734 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
32735
32736 location_t
32737 defparse_location (tree default_argument)
32738 {
32739 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
32740 location_t start = tokens->first->location;
32741 location_t end = tokens->last->location;
32742 return make_location (start, start, end);
32743 }
32744
32745 /* Begin parsing tentatively. We always save tokens while parsing
32746 tentatively so that if the tentative parsing fails we can restore the
32747 tokens. */
32748
32749 static void
32750 cp_parser_parse_tentatively (cp_parser* parser)
32751 {
32752 /* Enter a new parsing context. */
32753 parser->context = cp_parser_context_new (parser->context);
32754 /* Begin saving tokens. */
32755 cp_lexer_save_tokens (parser->lexer);
32756 /* In order to avoid repetitive access control error messages,
32757 access checks are queued up until we are no longer parsing
32758 tentatively. */
32759 push_deferring_access_checks (dk_deferred);
32760 }
32761
32762 /* Commit to the currently active tentative parse. */
32763
32764 static void
32765 cp_parser_commit_to_tentative_parse (cp_parser* parser)
32766 {
32767 cp_parser_context *context;
32768 cp_lexer *lexer;
32769
32770 /* Mark all of the levels as committed. */
32771 lexer = parser->lexer;
32772 for (context = parser->context; context->next; context = context->next)
32773 {
32774 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
32775 break;
32776 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
32777 while (!cp_lexer_saving_tokens (lexer))
32778 lexer = lexer->next;
32779 cp_lexer_commit_tokens (lexer);
32780 }
32781 }
32782
32783 /* Commit to the topmost currently active tentative parse.
32784
32785 Note that this function shouldn't be called when there are
32786 irreversible side-effects while in a tentative state. For
32787 example, we shouldn't create a permanent entry in the symbol
32788 table, or issue an error message that might not apply if the
32789 tentative parse is aborted. */
32790
32791 static void
32792 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
32793 {
32794 cp_parser_context *context = parser->context;
32795 cp_lexer *lexer = parser->lexer;
32796
32797 if (context)
32798 {
32799 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
32800 return;
32801 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
32802
32803 while (!cp_lexer_saving_tokens (lexer))
32804 lexer = lexer->next;
32805 cp_lexer_commit_tokens (lexer);
32806 }
32807 }
32808
32809 /* Abort the currently active tentative parse. All consumed tokens
32810 will be rolled back, and no diagnostics will be issued. */
32811
32812 static void
32813 cp_parser_abort_tentative_parse (cp_parser* parser)
32814 {
32815 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
32816 || errorcount > 0);
32817 cp_parser_simulate_error (parser);
32818 /* Now, pretend that we want to see if the construct was
32819 successfully parsed. */
32820 cp_parser_parse_definitely (parser);
32821 }
32822
32823 /* Stop parsing tentatively. If a parse error has occurred, restore the
32824 token stream. Otherwise, commit to the tokens we have consumed.
32825 Returns true if no error occurred; false otherwise. */
32826
32827 static bool
32828 cp_parser_parse_definitely (cp_parser* parser)
32829 {
32830 bool error_occurred;
32831 cp_parser_context *context;
32832
32833 /* Remember whether or not an error occurred, since we are about to
32834 destroy that information. */
32835 error_occurred = cp_parser_error_occurred (parser);
32836 /* Remove the topmost context from the stack. */
32837 context = parser->context;
32838 parser->context = context->next;
32839 /* If no parse errors occurred, commit to the tentative parse. */
32840 if (!error_occurred)
32841 {
32842 /* Commit to the tokens read tentatively, unless that was
32843 already done. */
32844 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
32845 cp_lexer_commit_tokens (parser->lexer);
32846
32847 pop_to_parent_deferring_access_checks ();
32848 }
32849 /* Otherwise, if errors occurred, roll back our state so that things
32850 are just as they were before we began the tentative parse. */
32851 else
32852 {
32853 cp_lexer_rollback_tokens (parser->lexer);
32854 pop_deferring_access_checks ();
32855 }
32856 /* Add the context to the front of the free list. */
32857 context->next = cp_parser_context_free_list;
32858 cp_parser_context_free_list = context;
32859
32860 return !error_occurred;
32861 }
32862
32863 /* Returns true if we are parsing tentatively and are not committed to
32864 this tentative parse. */
32865
32866 static bool
32867 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
32868 {
32869 return (cp_parser_parsing_tentatively (parser)
32870 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
32871 }
32872
32873 /* Returns nonzero iff an error has occurred during the most recent
32874 tentative parse. */
32875
32876 static bool
32877 cp_parser_error_occurred (cp_parser* parser)
32878 {
32879 return (cp_parser_parsing_tentatively (parser)
32880 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
32881 }
32882
32883 /* Returns nonzero if GNU extensions are allowed. */
32884
32885 static bool
32886 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
32887 {
32888 return parser->allow_gnu_extensions_p;
32889 }
32890 \f
32891 /* Objective-C++ Productions */
32892
32893
32894 /* Parse an Objective-C expression, which feeds into a primary-expression
32895 above.
32896
32897 objc-expression:
32898 objc-message-expression
32899 objc-string-literal
32900 objc-encode-expression
32901 objc-protocol-expression
32902 objc-selector-expression
32903
32904 Returns a tree representation of the expression. */
32905
32906 static cp_expr
32907 cp_parser_objc_expression (cp_parser* parser)
32908 {
32909 /* Try to figure out what kind of declaration is present. */
32910 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
32911
32912 switch (kwd->type)
32913 {
32914 case CPP_OPEN_SQUARE:
32915 return cp_parser_objc_message_expression (parser);
32916
32917 case CPP_OBJC_STRING:
32918 kwd = cp_lexer_consume_token (parser->lexer);
32919 return objc_build_string_object (kwd->u.value);
32920
32921 case CPP_KEYWORD:
32922 switch (kwd->keyword)
32923 {
32924 case RID_AT_ENCODE:
32925 return cp_parser_objc_encode_expression (parser);
32926
32927 case RID_AT_PROTOCOL:
32928 return cp_parser_objc_protocol_expression (parser);
32929
32930 case RID_AT_SELECTOR:
32931 return cp_parser_objc_selector_expression (parser);
32932
32933 default:
32934 break;
32935 }
32936 /* FALLTHRU */
32937 default:
32938 error_at (kwd->location,
32939 "misplaced %<@%D%> Objective-C++ construct",
32940 kwd->u.value);
32941 cp_parser_skip_to_end_of_block_or_statement (parser);
32942 }
32943
32944 return error_mark_node;
32945 }
32946
32947 /* Parse an Objective-C message expression.
32948
32949 objc-message-expression:
32950 [ objc-message-receiver objc-message-args ]
32951
32952 Returns a representation of an Objective-C message. */
32953
32954 static tree
32955 cp_parser_objc_message_expression (cp_parser* parser)
32956 {
32957 tree receiver, messageargs;
32958
32959 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
32960 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
32961 receiver = cp_parser_objc_message_receiver (parser);
32962 messageargs = cp_parser_objc_message_args (parser);
32963 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
32964 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32965
32966 tree result = objc_build_message_expr (receiver, messageargs);
32967
32968 /* Construct a location e.g.
32969 [self func1:5]
32970 ^~~~~~~~~~~~~~
32971 ranging from the '[' to the ']', with the caret at the start. */
32972 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
32973 protected_set_expr_location (result, combined_loc);
32974
32975 return result;
32976 }
32977
32978 /* Parse an objc-message-receiver.
32979
32980 objc-message-receiver:
32981 expression
32982 simple-type-specifier
32983
32984 Returns a representation of the type or expression. */
32985
32986 static tree
32987 cp_parser_objc_message_receiver (cp_parser* parser)
32988 {
32989 tree rcv;
32990
32991 /* An Objective-C message receiver may be either (1) a type
32992 or (2) an expression. */
32993 cp_parser_parse_tentatively (parser);
32994 rcv = cp_parser_expression (parser);
32995
32996 /* If that worked out, fine. */
32997 if (cp_parser_parse_definitely (parser))
32998 return rcv;
32999
33000 cp_parser_parse_tentatively (parser);
33001 rcv = cp_parser_simple_type_specifier (parser,
33002 /*decl_specs=*/NULL,
33003 CP_PARSER_FLAGS_NONE);
33004
33005 if (cp_parser_parse_definitely (parser))
33006 return objc_get_class_reference (rcv);
33007
33008 cp_parser_error (parser, "objective-c++ message receiver expected");
33009 return error_mark_node;
33010 }
33011
33012 /* Parse the arguments and selectors comprising an Objective-C message.
33013
33014 objc-message-args:
33015 objc-selector
33016 objc-selector-args
33017 objc-selector-args , objc-comma-args
33018
33019 objc-selector-args:
33020 objc-selector [opt] : assignment-expression
33021 objc-selector-args objc-selector [opt] : assignment-expression
33022
33023 objc-comma-args:
33024 assignment-expression
33025 objc-comma-args , assignment-expression
33026
33027 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
33028 selector arguments and TREE_VALUE containing a list of comma
33029 arguments. */
33030
33031 static tree
33032 cp_parser_objc_message_args (cp_parser* parser)
33033 {
33034 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
33035 bool maybe_unary_selector_p = true;
33036 cp_token *token = cp_lexer_peek_token (parser->lexer);
33037
33038 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
33039 {
33040 tree selector = NULL_TREE, arg;
33041
33042 if (token->type != CPP_COLON)
33043 selector = cp_parser_objc_selector (parser);
33044
33045 /* Detect if we have a unary selector. */
33046 if (maybe_unary_selector_p
33047 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
33048 return build_tree_list (selector, NULL_TREE);
33049
33050 maybe_unary_selector_p = false;
33051 cp_parser_require (parser, CPP_COLON, RT_COLON);
33052 arg = cp_parser_assignment_expression (parser);
33053
33054 sel_args
33055 = chainon (sel_args,
33056 build_tree_list (selector, arg));
33057
33058 token = cp_lexer_peek_token (parser->lexer);
33059 }
33060
33061 /* Handle non-selector arguments, if any. */
33062 while (token->type == CPP_COMMA)
33063 {
33064 tree arg;
33065
33066 cp_lexer_consume_token (parser->lexer);
33067 arg = cp_parser_assignment_expression (parser);
33068
33069 addl_args
33070 = chainon (addl_args,
33071 build_tree_list (NULL_TREE, arg));
33072
33073 token = cp_lexer_peek_token (parser->lexer);
33074 }
33075
33076 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
33077 {
33078 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
33079 return build_tree_list (error_mark_node, error_mark_node);
33080 }
33081
33082 return build_tree_list (sel_args, addl_args);
33083 }
33084
33085 /* Parse an Objective-C encode expression.
33086
33087 objc-encode-expression:
33088 @encode objc-typename
33089
33090 Returns an encoded representation of the type argument. */
33091
33092 static cp_expr
33093 cp_parser_objc_encode_expression (cp_parser* parser)
33094 {
33095 tree type;
33096 cp_token *token;
33097 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
33098
33099 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
33100 matching_parens parens;
33101 parens.require_open (parser);
33102 token = cp_lexer_peek_token (parser->lexer);
33103 type = complete_type (cp_parser_type_id (parser));
33104 parens.require_close (parser);
33105
33106 if (!type)
33107 {
33108 error_at (token->location,
33109 "%<@encode%> must specify a type as an argument");
33110 return error_mark_node;
33111 }
33112
33113 /* This happens if we find @encode(T) (where T is a template
33114 typename or something dependent on a template typename) when
33115 parsing a template. In that case, we can't compile it
33116 immediately, but we rather create an AT_ENCODE_EXPR which will
33117 need to be instantiated when the template is used.
33118 */
33119 if (dependent_type_p (type))
33120 {
33121 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
33122 TREE_READONLY (value) = 1;
33123 return value;
33124 }
33125
33126
33127 /* Build a location of the form:
33128 @encode(int)
33129 ^~~~~~~~~~~~
33130 with caret==start at the @ token, finishing at the close paren. */
33131 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
33132
33133 return cp_expr (objc_build_encode_expr (type), combined_loc);
33134 }
33135
33136 /* Parse an Objective-C @defs expression. */
33137
33138 static tree
33139 cp_parser_objc_defs_expression (cp_parser *parser)
33140 {
33141 tree name;
33142
33143 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
33144 matching_parens parens;
33145 parens.require_open (parser);
33146 name = cp_parser_identifier (parser);
33147 parens.require_close (parser);
33148
33149 return objc_get_class_ivars (name);
33150 }
33151
33152 /* Parse an Objective-C protocol expression.
33153
33154 objc-protocol-expression:
33155 @protocol ( identifier )
33156
33157 Returns a representation of the protocol expression. */
33158
33159 static tree
33160 cp_parser_objc_protocol_expression (cp_parser* parser)
33161 {
33162 tree proto;
33163 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
33164
33165 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
33166 matching_parens parens;
33167 parens.require_open (parser);
33168 proto = cp_parser_identifier (parser);
33169 parens.require_close (parser);
33170
33171 /* Build a location of the form:
33172 @protocol(prot)
33173 ^~~~~~~~~~~~~~~
33174 with caret==start at the @ token, finishing at the close paren. */
33175 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
33176 tree result = objc_build_protocol_expr (proto);
33177 protected_set_expr_location (result, combined_loc);
33178 return result;
33179 }
33180
33181 /* Parse an Objective-C selector expression.
33182
33183 objc-selector-expression:
33184 @selector ( objc-method-signature )
33185
33186 objc-method-signature:
33187 objc-selector
33188 objc-selector-seq
33189
33190 objc-selector-seq:
33191 objc-selector :
33192 objc-selector-seq objc-selector :
33193
33194 Returns a representation of the method selector. */
33195
33196 static tree
33197 cp_parser_objc_selector_expression (cp_parser* parser)
33198 {
33199 tree sel_seq = NULL_TREE;
33200 bool maybe_unary_selector_p = true;
33201 cp_token *token;
33202 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33203
33204 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
33205 matching_parens parens;
33206 parens.require_open (parser);
33207 token = cp_lexer_peek_token (parser->lexer);
33208
33209 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
33210 || token->type == CPP_SCOPE)
33211 {
33212 tree selector = NULL_TREE;
33213
33214 if (token->type != CPP_COLON
33215 || token->type == CPP_SCOPE)
33216 selector = cp_parser_objc_selector (parser);
33217
33218 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
33219 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
33220 {
33221 /* Detect if we have a unary selector. */
33222 if (maybe_unary_selector_p)
33223 {
33224 sel_seq = selector;
33225 goto finish_selector;
33226 }
33227 else
33228 {
33229 cp_parser_error (parser, "expected %<:%>");
33230 }
33231 }
33232 maybe_unary_selector_p = false;
33233 token = cp_lexer_consume_token (parser->lexer);
33234
33235 if (token->type == CPP_SCOPE)
33236 {
33237 sel_seq
33238 = chainon (sel_seq,
33239 build_tree_list (selector, NULL_TREE));
33240 sel_seq
33241 = chainon (sel_seq,
33242 build_tree_list (NULL_TREE, NULL_TREE));
33243 }
33244 else
33245 sel_seq
33246 = chainon (sel_seq,
33247 build_tree_list (selector, NULL_TREE));
33248
33249 token = cp_lexer_peek_token (parser->lexer);
33250 }
33251
33252 finish_selector:
33253 parens.require_close (parser);
33254
33255
33256 /* Build a location of the form:
33257 @selector(func)
33258 ^~~~~~~~~~~~~~~
33259 with caret==start at the @ token, finishing at the close paren. */
33260 location_t combined_loc = make_location (loc, loc, parser->lexer);
33261 tree result = objc_build_selector_expr (combined_loc, sel_seq);
33262 /* TODO: objc_build_selector_expr doesn't always honor the location. */
33263 protected_set_expr_location (result, combined_loc);
33264 return result;
33265 }
33266
33267 /* Parse a list of identifiers.
33268
33269 objc-identifier-list:
33270 identifier
33271 objc-identifier-list , identifier
33272
33273 Returns a TREE_LIST of identifier nodes. */
33274
33275 static tree
33276 cp_parser_objc_identifier_list (cp_parser* parser)
33277 {
33278 tree identifier;
33279 tree list;
33280 cp_token *sep;
33281
33282 identifier = cp_parser_identifier (parser);
33283 if (identifier == error_mark_node)
33284 return error_mark_node;
33285
33286 list = build_tree_list (NULL_TREE, identifier);
33287 sep = cp_lexer_peek_token (parser->lexer);
33288
33289 while (sep->type == CPP_COMMA)
33290 {
33291 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
33292 identifier = cp_parser_identifier (parser);
33293 if (identifier == error_mark_node)
33294 return list;
33295
33296 list = chainon (list, build_tree_list (NULL_TREE,
33297 identifier));
33298 sep = cp_lexer_peek_token (parser->lexer);
33299 }
33300
33301 return list;
33302 }
33303
33304 /* Parse an Objective-C alias declaration.
33305
33306 objc-alias-declaration:
33307 @compatibility_alias identifier identifier ;
33308
33309 This function registers the alias mapping with the Objective-C front end.
33310 It returns nothing. */
33311
33312 static void
33313 cp_parser_objc_alias_declaration (cp_parser* parser)
33314 {
33315 tree alias, orig;
33316
33317 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
33318 alias = cp_parser_identifier (parser);
33319 orig = cp_parser_identifier (parser);
33320 objc_declare_alias (alias, orig);
33321 cp_parser_consume_semicolon_at_end_of_statement (parser);
33322 }
33323
33324 /* Parse an Objective-C class forward-declaration.
33325
33326 objc-class-declaration:
33327 @class objc-identifier-list ;
33328
33329 The function registers the forward declarations with the Objective-C
33330 front end. It returns nothing. */
33331
33332 static void
33333 cp_parser_objc_class_declaration (cp_parser* parser)
33334 {
33335 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
33336 while (true)
33337 {
33338 tree id;
33339
33340 id = cp_parser_identifier (parser);
33341 if (id == error_mark_node)
33342 break;
33343
33344 objc_declare_class (id);
33345
33346 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33347 cp_lexer_consume_token (parser->lexer);
33348 else
33349 break;
33350 }
33351 cp_parser_consume_semicolon_at_end_of_statement (parser);
33352 }
33353
33354 /* Parse a list of Objective-C protocol references.
33355
33356 objc-protocol-refs-opt:
33357 objc-protocol-refs [opt]
33358
33359 objc-protocol-refs:
33360 < objc-identifier-list >
33361
33362 Returns a TREE_LIST of identifiers, if any. */
33363
33364 static tree
33365 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
33366 {
33367 tree protorefs = NULL_TREE;
33368
33369 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
33370 {
33371 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
33372 protorefs = cp_parser_objc_identifier_list (parser);
33373 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
33374 }
33375
33376 return protorefs;
33377 }
33378
33379 /* Parse a Objective-C visibility specification. */
33380
33381 static void
33382 cp_parser_objc_visibility_spec (cp_parser* parser)
33383 {
33384 cp_token *vis = cp_lexer_peek_token (parser->lexer);
33385
33386 switch (vis->keyword)
33387 {
33388 case RID_AT_PRIVATE:
33389 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
33390 break;
33391 case RID_AT_PROTECTED:
33392 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
33393 break;
33394 case RID_AT_PUBLIC:
33395 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
33396 break;
33397 case RID_AT_PACKAGE:
33398 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
33399 break;
33400 default:
33401 return;
33402 }
33403
33404 /* Eat '@private'/'@protected'/'@public'. */
33405 cp_lexer_consume_token (parser->lexer);
33406 }
33407
33408 /* Parse an Objective-C method type. Return 'true' if it is a class
33409 (+) method, and 'false' if it is an instance (-) method. */
33410
33411 static inline bool
33412 cp_parser_objc_method_type (cp_parser* parser)
33413 {
33414 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
33415 return true;
33416 else
33417 return false;
33418 }
33419
33420 /* Parse an Objective-C protocol qualifier. */
33421
33422 static tree
33423 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
33424 {
33425 tree quals = NULL_TREE, node;
33426 cp_token *token = cp_lexer_peek_token (parser->lexer);
33427
33428 node = token->u.value;
33429
33430 while (node && identifier_p (node)
33431 && (node == ridpointers [(int) RID_IN]
33432 || node == ridpointers [(int) RID_OUT]
33433 || node == ridpointers [(int) RID_INOUT]
33434 || node == ridpointers [(int) RID_BYCOPY]
33435 || node == ridpointers [(int) RID_BYREF]
33436 || node == ridpointers [(int) RID_ONEWAY]))
33437 {
33438 quals = tree_cons (NULL_TREE, node, quals);
33439 cp_lexer_consume_token (parser->lexer);
33440 token = cp_lexer_peek_token (parser->lexer);
33441 node = token->u.value;
33442 }
33443
33444 return quals;
33445 }
33446
33447 /* Parse an Objective-C typename. */
33448
33449 static tree
33450 cp_parser_objc_typename (cp_parser* parser)
33451 {
33452 tree type_name = NULL_TREE;
33453
33454 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33455 {
33456 tree proto_quals, cp_type = NULL_TREE;
33457
33458 matching_parens parens;
33459 parens.consume_open (parser); /* Eat '('. */
33460 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
33461
33462 /* An ObjC type name may consist of just protocol qualifiers, in which
33463 case the type shall default to 'id'. */
33464 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
33465 {
33466 cp_type = cp_parser_type_id (parser);
33467
33468 /* If the type could not be parsed, an error has already
33469 been produced. For error recovery, behave as if it had
33470 not been specified, which will use the default type
33471 'id'. */
33472 if (cp_type == error_mark_node)
33473 {
33474 cp_type = NULL_TREE;
33475 /* We need to skip to the closing parenthesis as
33476 cp_parser_type_id() does not seem to do it for
33477 us. */
33478 cp_parser_skip_to_closing_parenthesis (parser,
33479 /*recovering=*/true,
33480 /*or_comma=*/false,
33481 /*consume_paren=*/false);
33482 }
33483 }
33484
33485 parens.require_close (parser);
33486 type_name = build_tree_list (proto_quals, cp_type);
33487 }
33488
33489 return type_name;
33490 }
33491
33492 /* Check to see if TYPE refers to an Objective-C selector name. */
33493
33494 static bool
33495 cp_parser_objc_selector_p (enum cpp_ttype type)
33496 {
33497 return (type == CPP_NAME || type == CPP_KEYWORD
33498 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
33499 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
33500 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
33501 || type == CPP_XOR || type == CPP_XOR_EQ);
33502 }
33503
33504 /* Parse an Objective-C selector. */
33505
33506 static tree
33507 cp_parser_objc_selector (cp_parser* parser)
33508 {
33509 cp_token *token = cp_lexer_consume_token (parser->lexer);
33510
33511 if (!cp_parser_objc_selector_p (token->type))
33512 {
33513 error_at (token->location, "invalid Objective-C++ selector name");
33514 return error_mark_node;
33515 }
33516
33517 /* C++ operator names are allowed to appear in ObjC selectors. */
33518 switch (token->type)
33519 {
33520 case CPP_AND_AND: return get_identifier ("and");
33521 case CPP_AND_EQ: return get_identifier ("and_eq");
33522 case CPP_AND: return get_identifier ("bitand");
33523 case CPP_OR: return get_identifier ("bitor");
33524 case CPP_COMPL: return get_identifier ("compl");
33525 case CPP_NOT: return get_identifier ("not");
33526 case CPP_NOT_EQ: return get_identifier ("not_eq");
33527 case CPP_OR_OR: return get_identifier ("or");
33528 case CPP_OR_EQ: return get_identifier ("or_eq");
33529 case CPP_XOR: return get_identifier ("xor");
33530 case CPP_XOR_EQ: return get_identifier ("xor_eq");
33531 default: return token->u.value;
33532 }
33533 }
33534
33535 /* Parse an Objective-C params list. */
33536
33537 static tree
33538 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
33539 {
33540 tree params = NULL_TREE;
33541 bool maybe_unary_selector_p = true;
33542 cp_token *token = cp_lexer_peek_token (parser->lexer);
33543
33544 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
33545 {
33546 tree selector = NULL_TREE, type_name, identifier;
33547 tree parm_attr = NULL_TREE;
33548
33549 if (token->keyword == RID_ATTRIBUTE)
33550 break;
33551
33552 if (token->type != CPP_COLON)
33553 selector = cp_parser_objc_selector (parser);
33554
33555 /* Detect if we have a unary selector. */
33556 if (maybe_unary_selector_p
33557 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
33558 {
33559 params = selector; /* Might be followed by attributes. */
33560 break;
33561 }
33562
33563 maybe_unary_selector_p = false;
33564 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33565 {
33566 /* Something went quite wrong. There should be a colon
33567 here, but there is not. Stop parsing parameters. */
33568 break;
33569 }
33570 type_name = cp_parser_objc_typename (parser);
33571 /* New ObjC allows attributes on parameters too. */
33572 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
33573 parm_attr = cp_parser_attributes_opt (parser);
33574 identifier = cp_parser_identifier (parser);
33575
33576 params
33577 = chainon (params,
33578 objc_build_keyword_decl (selector,
33579 type_name,
33580 identifier,
33581 parm_attr));
33582
33583 token = cp_lexer_peek_token (parser->lexer);
33584 }
33585
33586 if (params == NULL_TREE)
33587 {
33588 cp_parser_error (parser, "objective-c++ method declaration is expected");
33589 return error_mark_node;
33590 }
33591
33592 /* We allow tail attributes for the method. */
33593 if (token->keyword == RID_ATTRIBUTE)
33594 {
33595 *attributes = cp_parser_attributes_opt (parser);
33596 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
33597 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33598 return params;
33599 cp_parser_error (parser,
33600 "method attributes must be specified at the end");
33601 return error_mark_node;
33602 }
33603
33604 if (params == NULL_TREE)
33605 {
33606 cp_parser_error (parser, "objective-c++ method declaration is expected");
33607 return error_mark_node;
33608 }
33609 return params;
33610 }
33611
33612 /* Parse the non-keyword Objective-C params. */
33613
33614 static tree
33615 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
33616 tree* attributes)
33617 {
33618 tree params = make_node (TREE_LIST);
33619 cp_token *token = cp_lexer_peek_token (parser->lexer);
33620 *ellipsisp = false; /* Initially, assume no ellipsis. */
33621
33622 while (token->type == CPP_COMMA)
33623 {
33624 cp_parameter_declarator *parmdecl;
33625 tree parm;
33626
33627 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
33628 token = cp_lexer_peek_token (parser->lexer);
33629
33630 if (token->type == CPP_ELLIPSIS)
33631 {
33632 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
33633 *ellipsisp = true;
33634 token = cp_lexer_peek_token (parser->lexer);
33635 break;
33636 }
33637
33638 /* TODO: parse attributes for tail parameters. */
33639 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
33640 false, NULL);
33641 parm = grokdeclarator (parmdecl->declarator,
33642 &parmdecl->decl_specifiers,
33643 PARM, /*initialized=*/0,
33644 /*attrlist=*/NULL);
33645
33646 chainon (params, build_tree_list (NULL_TREE, parm));
33647 token = cp_lexer_peek_token (parser->lexer);
33648 }
33649
33650 /* We allow tail attributes for the method. */
33651 if (token->keyword == RID_ATTRIBUTE)
33652 {
33653 if (*attributes == NULL_TREE)
33654 {
33655 *attributes = cp_parser_attributes_opt (parser);
33656 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
33657 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33658 return params;
33659 }
33660 else
33661 /* We have an error, but parse the attributes, so that we can
33662 carry on. */
33663 *attributes = cp_parser_attributes_opt (parser);
33664
33665 cp_parser_error (parser,
33666 "method attributes must be specified at the end");
33667 return error_mark_node;
33668 }
33669
33670 return params;
33671 }
33672
33673 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
33674
33675 static void
33676 cp_parser_objc_interstitial_code (cp_parser* parser)
33677 {
33678 cp_token *token = cp_lexer_peek_token (parser->lexer);
33679
33680 /* If the next token is `extern' and the following token is a string
33681 literal, then we have a linkage specification. */
33682 if (token->keyword == RID_EXTERN
33683 && cp_parser_is_pure_string_literal
33684 (cp_lexer_peek_nth_token (parser->lexer, 2)))
33685 cp_parser_linkage_specification (parser, NULL_TREE);
33686 /* Handle #pragma, if any. */
33687 else if (token->type == CPP_PRAGMA)
33688 cp_parser_pragma (parser, pragma_objc_icode, NULL);
33689 /* Allow stray semicolons. */
33690 else if (token->type == CPP_SEMICOLON)
33691 cp_lexer_consume_token (parser->lexer);
33692 /* Mark methods as optional or required, when building protocols. */
33693 else if (token->keyword == RID_AT_OPTIONAL)
33694 {
33695 cp_lexer_consume_token (parser->lexer);
33696 objc_set_method_opt (true);
33697 }
33698 else if (token->keyword == RID_AT_REQUIRED)
33699 {
33700 cp_lexer_consume_token (parser->lexer);
33701 objc_set_method_opt (false);
33702 }
33703 else if (token->keyword == RID_NAMESPACE)
33704 cp_parser_namespace_definition (parser);
33705 /* Other stray characters must generate errors. */
33706 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
33707 {
33708 cp_lexer_consume_token (parser->lexer);
33709 error ("stray %qs between Objective-C++ methods",
33710 token->type == CPP_OPEN_BRACE ? "{" : "}");
33711 }
33712 /* Finally, try to parse a block-declaration, or a function-definition. */
33713 else
33714 cp_parser_block_declaration (parser, /*statement_p=*/false);
33715 }
33716
33717 /* Parse a method signature. */
33718
33719 static tree
33720 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
33721 {
33722 tree rettype, kwdparms, optparms;
33723 bool ellipsis = false;
33724 bool is_class_method;
33725
33726 is_class_method = cp_parser_objc_method_type (parser);
33727 rettype = cp_parser_objc_typename (parser);
33728 *attributes = NULL_TREE;
33729 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
33730 if (kwdparms == error_mark_node)
33731 return error_mark_node;
33732 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
33733 if (optparms == error_mark_node)
33734 return error_mark_node;
33735
33736 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
33737 }
33738
33739 static bool
33740 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
33741 {
33742 tree tattr;
33743 cp_lexer_save_tokens (parser->lexer);
33744 tattr = cp_parser_attributes_opt (parser);
33745 gcc_assert (tattr) ;
33746
33747 /* If the attributes are followed by a method introducer, this is not allowed.
33748 Dump the attributes and flag the situation. */
33749 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
33750 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33751 return true;
33752
33753 /* Otherwise, the attributes introduce some interstitial code, possibly so
33754 rewind to allow that check. */
33755 cp_lexer_rollback_tokens (parser->lexer);
33756 return false;
33757 }
33758
33759 /* Parse an Objective-C method prototype list. */
33760
33761 static void
33762 cp_parser_objc_method_prototype_list (cp_parser* parser)
33763 {
33764 cp_token *token = cp_lexer_peek_token (parser->lexer);
33765
33766 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
33767 {
33768 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
33769 {
33770 tree attributes, sig;
33771 bool is_class_method;
33772 if (token->type == CPP_PLUS)
33773 is_class_method = true;
33774 else
33775 is_class_method = false;
33776 sig = cp_parser_objc_method_signature (parser, &attributes);
33777 if (sig == error_mark_node)
33778 {
33779 cp_parser_skip_to_end_of_block_or_statement (parser);
33780 token = cp_lexer_peek_token (parser->lexer);
33781 continue;
33782 }
33783 objc_add_method_declaration (is_class_method, sig, attributes);
33784 cp_parser_consume_semicolon_at_end_of_statement (parser);
33785 }
33786 else if (token->keyword == RID_AT_PROPERTY)
33787 cp_parser_objc_at_property_declaration (parser);
33788 else if (token->keyword == RID_ATTRIBUTE
33789 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
33790 warning_at (cp_lexer_peek_token (parser->lexer)->location,
33791 OPT_Wattributes,
33792 "prefix attributes are ignored for methods");
33793 else
33794 /* Allow for interspersed non-ObjC++ code. */
33795 cp_parser_objc_interstitial_code (parser);
33796
33797 token = cp_lexer_peek_token (parser->lexer);
33798 }
33799
33800 if (token->type != CPP_EOF)
33801 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
33802 else
33803 cp_parser_error (parser, "expected %<@end%>");
33804
33805 objc_finish_interface ();
33806 }
33807
33808 /* Parse an Objective-C method definition list. */
33809
33810 static void
33811 cp_parser_objc_method_definition_list (cp_parser* parser)
33812 {
33813 for (;;)
33814 {
33815 cp_token *token = cp_lexer_peek_token (parser->lexer);
33816
33817 if (token->keyword == RID_AT_END)
33818 {
33819 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
33820 break;
33821 }
33822 else if (token->type == CPP_EOF)
33823 {
33824 cp_parser_error (parser, "expected %<@end%>");
33825 break;
33826 }
33827 else if (token->type == CPP_PLUS || token->type == CPP_MINUS)
33828 {
33829 bool is_class_method = token->type == CPP_PLUS;
33830
33831 push_deferring_access_checks (dk_deferred);
33832 tree attribute;
33833 tree sig = cp_parser_objc_method_signature (parser, &attribute);
33834 if (sig == error_mark_node)
33835 cp_parser_skip_to_end_of_block_or_statement (parser);
33836 else
33837 {
33838 objc_start_method_definition (is_class_method, sig,
33839 attribute, NULL_TREE);
33840
33841 /* For historical reasons, we accept an optional semicolon. */
33842 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33843 cp_lexer_consume_token (parser->lexer);
33844
33845 perform_deferred_access_checks (tf_warning_or_error);
33846 stop_deferring_access_checks ();
33847 tree meth
33848 = cp_parser_function_definition_after_declarator (parser, false);
33849 pop_deferring_access_checks ();
33850 objc_finish_method_definition (meth);
33851 }
33852 }
33853 /* The following case will be removed once @synthesize is
33854 completely implemented. */
33855 else if (token->keyword == RID_AT_PROPERTY)
33856 cp_parser_objc_at_property_declaration (parser);
33857 else if (token->keyword == RID_AT_SYNTHESIZE)
33858 cp_parser_objc_at_synthesize_declaration (parser);
33859 else if (token->keyword == RID_AT_DYNAMIC)
33860 cp_parser_objc_at_dynamic_declaration (parser);
33861 else if (token->keyword == RID_ATTRIBUTE
33862 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
33863 warning_at (token->location, OPT_Wattributes,
33864 "prefix attributes are ignored for methods");
33865 else
33866 /* Allow for interspersed non-ObjC++ code. */
33867 cp_parser_objc_interstitial_code (parser);
33868 }
33869
33870 objc_finish_implementation ();
33871 }
33872
33873 /* Parse Objective-C ivars. */
33874
33875 static void
33876 cp_parser_objc_class_ivars (cp_parser* parser)
33877 {
33878 cp_token *token = cp_lexer_peek_token (parser->lexer);
33879
33880 if (token->type != CPP_OPEN_BRACE)
33881 return; /* No ivars specified. */
33882
33883 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
33884 token = cp_lexer_peek_token (parser->lexer);
33885
33886 while (token->type != CPP_CLOSE_BRACE
33887 && token->keyword != RID_AT_END && token->type != CPP_EOF)
33888 {
33889 cp_decl_specifier_seq declspecs;
33890 int decl_class_or_enum_p;
33891 tree prefix_attributes;
33892
33893 cp_parser_objc_visibility_spec (parser);
33894
33895 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33896 break;
33897
33898 cp_parser_decl_specifier_seq (parser,
33899 CP_PARSER_FLAGS_OPTIONAL,
33900 &declspecs,
33901 &decl_class_or_enum_p);
33902
33903 /* auto, register, static, extern, mutable. */
33904 if (declspecs.storage_class != sc_none)
33905 {
33906 cp_parser_error (parser, "invalid type for instance variable");
33907 declspecs.storage_class = sc_none;
33908 }
33909
33910 /* thread_local. */
33911 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
33912 {
33913 cp_parser_error (parser, "invalid type for instance variable");
33914 declspecs.locations[ds_thread] = 0;
33915 }
33916
33917 /* typedef. */
33918 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
33919 {
33920 cp_parser_error (parser, "invalid type for instance variable");
33921 declspecs.locations[ds_typedef] = 0;
33922 }
33923
33924 prefix_attributes = declspecs.attributes;
33925 declspecs.attributes = NULL_TREE;
33926
33927 /* Keep going until we hit the `;' at the end of the
33928 declaration. */
33929 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33930 {
33931 tree width = NULL_TREE, attributes, first_attribute, decl;
33932 cp_declarator *declarator = NULL;
33933 int ctor_dtor_or_conv_p;
33934
33935 /* Check for a (possibly unnamed) bitfield declaration. */
33936 token = cp_lexer_peek_token (parser->lexer);
33937 if (token->type == CPP_COLON)
33938 goto eat_colon;
33939
33940 if (token->type == CPP_NAME
33941 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
33942 == CPP_COLON))
33943 {
33944 /* Get the name of the bitfield. */
33945 declarator = make_id_declarator (NULL_TREE,
33946 cp_parser_identifier (parser),
33947 sfk_none, token->location);
33948
33949 eat_colon:
33950 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
33951 /* Get the width of the bitfield. */
33952 width
33953 = cp_parser_constant_expression (parser);
33954 }
33955 else
33956 {
33957 /* Parse the declarator. */
33958 declarator
33959 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
33960 CP_PARSER_FLAGS_NONE,
33961 &ctor_dtor_or_conv_p,
33962 /*parenthesized_p=*/NULL,
33963 /*member_p=*/false,
33964 /*friend_p=*/false,
33965 /*static_p=*/false);
33966 }
33967
33968 /* Look for attributes that apply to the ivar. */
33969 attributes = cp_parser_attributes_opt (parser);
33970 /* Remember which attributes are prefix attributes and
33971 which are not. */
33972 first_attribute = attributes;
33973 /* Combine the attributes. */
33974 attributes = attr_chainon (prefix_attributes, attributes);
33975
33976 if (width)
33977 /* Create the bitfield declaration. */
33978 decl = grokbitfield (declarator, &declspecs,
33979 width, NULL_TREE, attributes);
33980 else
33981 decl = grokfield (declarator, &declspecs,
33982 NULL_TREE, /*init_const_expr_p=*/false,
33983 NULL_TREE, attributes);
33984
33985 /* Add the instance variable. */
33986 if (decl != error_mark_node && decl != NULL_TREE)
33987 objc_add_instance_variable (decl);
33988
33989 /* Reset PREFIX_ATTRIBUTES. */
33990 if (attributes != error_mark_node)
33991 {
33992 while (attributes && TREE_CHAIN (attributes) != first_attribute)
33993 attributes = TREE_CHAIN (attributes);
33994 if (attributes)
33995 TREE_CHAIN (attributes) = NULL_TREE;
33996 }
33997
33998 token = cp_lexer_peek_token (parser->lexer);
33999
34000 if (token->type == CPP_COMMA)
34001 {
34002 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
34003 continue;
34004 }
34005 break;
34006 }
34007
34008 cp_parser_consume_semicolon_at_end_of_statement (parser);
34009 token = cp_lexer_peek_token (parser->lexer);
34010 }
34011
34012 if (token->keyword == RID_AT_END)
34013 cp_parser_error (parser, "expected %<}%>");
34014
34015 /* Do not consume the RID_AT_END, so it will be read again as terminating
34016 the @interface of @implementation. */
34017 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
34018 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
34019
34020 /* For historical reasons, we accept an optional semicolon. */
34021 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34022 cp_lexer_consume_token (parser->lexer);
34023 }
34024
34025 /* Parse an Objective-C protocol declaration. */
34026
34027 static void
34028 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
34029 {
34030 tree proto, protorefs;
34031 cp_token *tok;
34032
34033 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
34034 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34035 {
34036 tok = cp_lexer_peek_token (parser->lexer);
34037 error_at (tok->location, "identifier expected after %<@protocol%>");
34038 cp_parser_consume_semicolon_at_end_of_statement (parser);
34039 return;
34040 }
34041
34042 /* See if we have a forward declaration or a definition. */
34043 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
34044
34045 /* Try a forward declaration first. */
34046 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
34047 {
34048 while (true)
34049 {
34050 tree id;
34051
34052 id = cp_parser_identifier (parser);
34053 if (id == error_mark_node)
34054 break;
34055
34056 objc_declare_protocol (id, attributes);
34057
34058 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34059 cp_lexer_consume_token (parser->lexer);
34060 else
34061 break;
34062 }
34063 cp_parser_consume_semicolon_at_end_of_statement (parser);
34064 }
34065
34066 /* Ok, we got a full-fledged definition (or at least should). */
34067 else
34068 {
34069 proto = cp_parser_identifier (parser);
34070 protorefs = cp_parser_objc_protocol_refs_opt (parser);
34071 objc_start_protocol (proto, protorefs, attributes);
34072 cp_parser_objc_method_prototype_list (parser);
34073 }
34074 }
34075
34076 /* Parse an Objective-C superclass or category. */
34077
34078 static void
34079 cp_parser_objc_superclass_or_category (cp_parser *parser,
34080 bool iface_p,
34081 tree *super,
34082 tree *categ, bool *is_class_extension)
34083 {
34084 cp_token *next = cp_lexer_peek_token (parser->lexer);
34085
34086 *super = *categ = NULL_TREE;
34087 *is_class_extension = false;
34088 if (next->type == CPP_COLON)
34089 {
34090 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
34091 *super = cp_parser_identifier (parser);
34092 }
34093 else if (next->type == CPP_OPEN_PAREN)
34094 {
34095 matching_parens parens;
34096 parens.consume_open (parser); /* Eat '('. */
34097
34098 /* If there is no category name, and this is an @interface, we
34099 have a class extension. */
34100 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
34101 {
34102 *categ = NULL_TREE;
34103 *is_class_extension = true;
34104 }
34105 else
34106 *categ = cp_parser_identifier (parser);
34107
34108 parens.require_close (parser);
34109 }
34110 }
34111
34112 /* Parse an Objective-C class interface. */
34113
34114 static void
34115 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
34116 {
34117 tree name, super, categ, protos;
34118 bool is_class_extension;
34119
34120 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
34121 location_t nam_loc = cp_lexer_peek_token (parser->lexer)->location;
34122 name = cp_parser_identifier (parser);
34123 if (name == error_mark_node)
34124 {
34125 /* It's hard to recover because even if valid @interface stuff
34126 is to follow, we can't compile it (or validate it) if we
34127 don't even know which class it refers to. Let's assume this
34128 was a stray '@interface' token in the stream and skip it.
34129 */
34130 return;
34131 }
34132 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
34133 &is_class_extension);
34134 protos = cp_parser_objc_protocol_refs_opt (parser);
34135
34136 /* We have either a class or a category on our hands. */
34137 if (categ || is_class_extension)
34138 objc_start_category_interface (name, categ, protos, attributes);
34139 else
34140 {
34141 objc_start_class_interface (name, nam_loc, super, protos, attributes);
34142 /* Handle instance variable declarations, if any. */
34143 cp_parser_objc_class_ivars (parser);
34144 objc_continue_interface ();
34145 }
34146
34147 cp_parser_objc_method_prototype_list (parser);
34148 }
34149
34150 /* Parse an Objective-C class implementation. */
34151
34152 static void
34153 cp_parser_objc_class_implementation (cp_parser* parser)
34154 {
34155 tree name, super, categ;
34156 bool is_class_extension;
34157
34158 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
34159 name = cp_parser_identifier (parser);
34160 if (name == error_mark_node)
34161 {
34162 /* It's hard to recover because even if valid @implementation
34163 stuff is to follow, we can't compile it (or validate it) if
34164 we don't even know which class it refers to. Let's assume
34165 this was a stray '@implementation' token in the stream and
34166 skip it.
34167 */
34168 return;
34169 }
34170 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
34171 &is_class_extension);
34172
34173 /* We have either a class or a category on our hands. */
34174 if (categ)
34175 objc_start_category_implementation (name, categ);
34176 else
34177 {
34178 objc_start_class_implementation (name, super);
34179 /* Handle instance variable declarations, if any. */
34180 cp_parser_objc_class_ivars (parser);
34181 objc_continue_implementation ();
34182 }
34183
34184 cp_parser_objc_method_definition_list (parser);
34185 }
34186
34187 /* Consume the @end token and finish off the implementation. */
34188
34189 static void
34190 cp_parser_objc_end_implementation (cp_parser* parser)
34191 {
34192 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
34193 objc_finish_implementation ();
34194 }
34195
34196 /* Parse an Objective-C declaration. */
34197
34198 static void
34199 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
34200 {
34201 /* Try to figure out what kind of declaration is present. */
34202 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
34203
34204 if (attributes)
34205 switch (kwd->keyword)
34206 {
34207 case RID_AT_ALIAS:
34208 case RID_AT_CLASS:
34209 case RID_AT_END:
34210 error_at (kwd->location, "attributes may not be specified before"
34211 " the %<@%D%> Objective-C++ keyword",
34212 kwd->u.value);
34213 attributes = NULL;
34214 break;
34215 case RID_AT_IMPLEMENTATION:
34216 warning_at (kwd->location, OPT_Wattributes,
34217 "prefix attributes are ignored before %<@%D%>",
34218 kwd->u.value);
34219 attributes = NULL;
34220 default:
34221 break;
34222 }
34223
34224 switch (kwd->keyword)
34225 {
34226 case RID_AT_ALIAS:
34227 cp_parser_objc_alias_declaration (parser);
34228 break;
34229 case RID_AT_CLASS:
34230 cp_parser_objc_class_declaration (parser);
34231 break;
34232 case RID_AT_PROTOCOL:
34233 cp_parser_objc_protocol_declaration (parser, attributes);
34234 break;
34235 case RID_AT_INTERFACE:
34236 cp_parser_objc_class_interface (parser, attributes);
34237 break;
34238 case RID_AT_IMPLEMENTATION:
34239 cp_parser_objc_class_implementation (parser);
34240 break;
34241 case RID_AT_END:
34242 cp_parser_objc_end_implementation (parser);
34243 break;
34244 default:
34245 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
34246 kwd->u.value);
34247 cp_parser_skip_to_end_of_block_or_statement (parser);
34248 }
34249 }
34250
34251 /* Parse an Objective-C try-catch-finally statement.
34252
34253 objc-try-catch-finally-stmt:
34254 @try compound-statement objc-catch-clause-seq [opt]
34255 objc-finally-clause [opt]
34256
34257 objc-catch-clause-seq:
34258 objc-catch-clause objc-catch-clause-seq [opt]
34259
34260 objc-catch-clause:
34261 @catch ( objc-exception-declaration ) compound-statement
34262
34263 objc-finally-clause:
34264 @finally compound-statement
34265
34266 objc-exception-declaration:
34267 parameter-declaration
34268 '...'
34269
34270 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
34271
34272 Returns NULL_TREE.
34273
34274 PS: This function is identical to c_parser_objc_try_catch_finally_statement
34275 for C. Keep them in sync. */
34276
34277 static tree
34278 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
34279 {
34280 location_t location;
34281 tree stmt;
34282
34283 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
34284 location = cp_lexer_peek_token (parser->lexer)->location;
34285 objc_maybe_warn_exceptions (location);
34286 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
34287 node, lest it get absorbed into the surrounding block. */
34288 stmt = push_stmt_list ();
34289 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
34290 objc_begin_try_stmt (location, pop_stmt_list (stmt));
34291
34292 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
34293 {
34294 cp_parameter_declarator *parm;
34295 tree parameter_declaration = error_mark_node;
34296 bool seen_open_paren = false;
34297 matching_parens parens;
34298
34299 cp_lexer_consume_token (parser->lexer);
34300 if (parens.require_open (parser))
34301 seen_open_paren = true;
34302 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
34303 {
34304 /* We have "@catch (...)" (where the '...' are literally
34305 what is in the code). Skip the '...'.
34306 parameter_declaration is set to NULL_TREE, and
34307 objc_being_catch_clauses() knows that that means
34308 '...'. */
34309 cp_lexer_consume_token (parser->lexer);
34310 parameter_declaration = NULL_TREE;
34311 }
34312 else
34313 {
34314 /* We have "@catch (NSException *exception)" or something
34315 like that. Parse the parameter declaration. */
34316 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
34317 false, NULL);
34318 if (parm == NULL)
34319 parameter_declaration = error_mark_node;
34320 else
34321 parameter_declaration = grokdeclarator (parm->declarator,
34322 &parm->decl_specifiers,
34323 PARM, /*initialized=*/0,
34324 /*attrlist=*/NULL);
34325 }
34326 if (seen_open_paren)
34327 parens.require_close (parser);
34328 else
34329 {
34330 /* If there was no open parenthesis, we are recovering from
34331 an error, and we are trying to figure out what mistake
34332 the user has made. */
34333
34334 /* If there is an immediate closing parenthesis, the user
34335 probably forgot the opening one (ie, they typed "@catch
34336 NSException *e)". Parse the closing parenthesis and keep
34337 going. */
34338 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
34339 cp_lexer_consume_token (parser->lexer);
34340
34341 /* If these is no immediate closing parenthesis, the user
34342 probably doesn't know that parenthesis are required at
34343 all (ie, they typed "@catch NSException *e"). So, just
34344 forget about the closing parenthesis and keep going. */
34345 }
34346 objc_begin_catch_clause (parameter_declaration);
34347 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
34348 objc_finish_catch_clause ();
34349 }
34350 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
34351 {
34352 cp_lexer_consume_token (parser->lexer);
34353 location = cp_lexer_peek_token (parser->lexer)->location;
34354 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
34355 node, lest it get absorbed into the surrounding block. */
34356 stmt = push_stmt_list ();
34357 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
34358 objc_build_finally_clause (location, pop_stmt_list (stmt));
34359 }
34360
34361 return objc_finish_try_stmt ();
34362 }
34363
34364 /* Parse an Objective-C synchronized statement.
34365
34366 objc-synchronized-stmt:
34367 @synchronized ( expression ) compound-statement
34368
34369 Returns NULL_TREE. */
34370
34371 static tree
34372 cp_parser_objc_synchronized_statement (cp_parser *parser)
34373 {
34374 location_t location;
34375 tree lock, stmt;
34376
34377 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
34378
34379 location = cp_lexer_peek_token (parser->lexer)->location;
34380 objc_maybe_warn_exceptions (location);
34381 matching_parens parens;
34382 parens.require_open (parser);
34383 lock = cp_parser_expression (parser);
34384 parens.require_close (parser);
34385
34386 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
34387 node, lest it get absorbed into the surrounding block. */
34388 stmt = push_stmt_list ();
34389 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
34390
34391 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
34392 }
34393
34394 /* Parse an Objective-C throw statement.
34395
34396 objc-throw-stmt:
34397 @throw assignment-expression [opt] ;
34398
34399 Returns a constructed '@throw' statement. */
34400
34401 static tree
34402 cp_parser_objc_throw_statement (cp_parser *parser)
34403 {
34404 tree expr = NULL_TREE;
34405 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34406
34407 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
34408
34409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34410 expr = cp_parser_expression (parser);
34411
34412 cp_parser_consume_semicolon_at_end_of_statement (parser);
34413
34414 return objc_build_throw_stmt (loc, expr);
34415 }
34416
34417 /* Parse an Objective-C statement. */
34418
34419 static tree
34420 cp_parser_objc_statement (cp_parser * parser)
34421 {
34422 /* Try to figure out what kind of declaration is present. */
34423 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
34424
34425 switch (kwd->keyword)
34426 {
34427 case RID_AT_TRY:
34428 return cp_parser_objc_try_catch_finally_statement (parser);
34429 case RID_AT_SYNCHRONIZED:
34430 return cp_parser_objc_synchronized_statement (parser);
34431 case RID_AT_THROW:
34432 return cp_parser_objc_throw_statement (parser);
34433 default:
34434 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
34435 kwd->u.value);
34436 cp_parser_skip_to_end_of_block_or_statement (parser);
34437 }
34438
34439 return error_mark_node;
34440 }
34441
34442 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
34443 look ahead to see if an objc keyword follows the attributes. This
34444 is to detect the use of prefix attributes on ObjC @interface and
34445 @protocol. */
34446
34447 static bool
34448 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
34449 {
34450 cp_lexer_save_tokens (parser->lexer);
34451 tree addon = cp_parser_attributes_opt (parser);
34452 if (addon
34453 && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
34454 {
34455 cp_lexer_commit_tokens (parser->lexer);
34456 if (*attrib)
34457 TREE_CHAIN (*attrib) = addon;
34458 else
34459 *attrib = addon;
34460 return true;
34461 }
34462 cp_lexer_rollback_tokens (parser->lexer);
34463 return false;
34464 }
34465
34466 /* This routine is a minimal replacement for
34467 c_parser_struct_declaration () used when parsing the list of
34468 types/names or ObjC++ properties. For example, when parsing the
34469 code
34470
34471 @property (readonly) int a, b, c;
34472
34473 this function is responsible for parsing "int a, int b, int c" and
34474 returning the declarations as CHAIN of DECLs.
34475
34476 TODO: Share this code with cp_parser_objc_class_ivars. It's very
34477 similar parsing. */
34478 static tree
34479 cp_parser_objc_struct_declaration (cp_parser *parser)
34480 {
34481 tree decls = NULL_TREE;
34482 cp_decl_specifier_seq declspecs;
34483 int decl_class_or_enum_p;
34484 tree prefix_attributes;
34485
34486 cp_parser_decl_specifier_seq (parser,
34487 CP_PARSER_FLAGS_NONE,
34488 &declspecs,
34489 &decl_class_or_enum_p);
34490
34491 if (declspecs.type == error_mark_node)
34492 return error_mark_node;
34493
34494 /* auto, register, static, extern, mutable. */
34495 if (declspecs.storage_class != sc_none)
34496 {
34497 cp_parser_error (parser, "invalid type for property");
34498 declspecs.storage_class = sc_none;
34499 }
34500
34501 /* thread_local. */
34502 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
34503 {
34504 cp_parser_error (parser, "invalid type for property");
34505 declspecs.locations[ds_thread] = 0;
34506 }
34507
34508 /* typedef. */
34509 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
34510 {
34511 cp_parser_error (parser, "invalid type for property");
34512 declspecs.locations[ds_typedef] = 0;
34513 }
34514
34515 prefix_attributes = declspecs.attributes;
34516 declspecs.attributes = NULL_TREE;
34517
34518 /* Keep going until we hit the `;' at the end of the declaration. */
34519 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34520 {
34521 tree attributes, first_attribute, decl;
34522 cp_declarator *declarator;
34523 cp_token *token;
34524
34525 /* Parse the declarator. */
34526 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
34527 CP_PARSER_FLAGS_NONE,
34528 NULL, NULL, false, false, false);
34529
34530 /* Look for attributes that apply to the ivar. */
34531 attributes = cp_parser_attributes_opt (parser);
34532 /* Remember which attributes are prefix attributes and
34533 which are not. */
34534 first_attribute = attributes;
34535 /* Combine the attributes. */
34536 attributes = attr_chainon (prefix_attributes, attributes);
34537
34538 decl = grokfield (declarator, &declspecs,
34539 NULL_TREE, /*init_const_expr_p=*/false,
34540 NULL_TREE, attributes);
34541
34542 if (decl == error_mark_node || decl == NULL_TREE)
34543 return error_mark_node;
34544
34545 /* Reset PREFIX_ATTRIBUTES. */
34546 if (attributes != error_mark_node)
34547 {
34548 while (attributes && TREE_CHAIN (attributes) != first_attribute)
34549 attributes = TREE_CHAIN (attributes);
34550 if (attributes)
34551 TREE_CHAIN (attributes) = NULL_TREE;
34552 }
34553
34554 DECL_CHAIN (decl) = decls;
34555 decls = decl;
34556
34557 token = cp_lexer_peek_token (parser->lexer);
34558 if (token->type == CPP_COMMA)
34559 {
34560 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
34561 continue;
34562 }
34563 else
34564 break;
34565 }
34566 return decls;
34567 }
34568
34569 /* Parse an Objective-C @property declaration. The syntax is:
34570
34571 objc-property-declaration:
34572 '@property' objc-property-attributes[opt] struct-declaration ;
34573
34574 objc-property-attributes:
34575 '(' objc-property-attribute-list ')'
34576
34577 objc-property-attribute-list:
34578 objc-property-attribute
34579 objc-property-attribute-list, objc-property-attribute
34580
34581 objc-property-attribute
34582 'getter' = identifier
34583 'setter' = identifier
34584 'readonly'
34585 'readwrite'
34586 'assign'
34587 'retain'
34588 'copy'
34589 'nonatomic'
34590
34591 For example:
34592 @property NSString *name;
34593 @property (readonly) id object;
34594 @property (retain, nonatomic, getter=getTheName) id name;
34595 @property int a, b, c;
34596
34597 PS: This function is identical to
34598 c_parser_objc_at_property_declaration for C. Keep them in sync. */
34599 static void
34600 cp_parser_objc_at_property_declaration (cp_parser *parser)
34601 {
34602 /* Parse the optional attribute list.
34603
34604 A list of parsed, but not verified, attributes. */
34605 vec<property_attribute_info *> prop_attr_list = vNULL;
34606 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34607
34608 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
34609
34610 /* Parse the optional attribute list... */
34611 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34612 {
34613 /* Eat the '('. */
34614 matching_parens parens;
34615 location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
34616 parens.consume_open (parser);
34617 bool syntax_error = false;
34618
34619 /* Allow empty @property attribute lists, but with a warning. */
34620 location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
34621 location_t attr_comb;
34622 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
34623 {
34624 attr_comb = make_location (attr_end, attr_start, attr_end);
34625 warning_at (attr_comb, OPT_Wattributes,
34626 "empty property attribute list");
34627 }
34628 else
34629 while (true)
34630 {
34631 cp_token *token = cp_lexer_peek_token (parser->lexer);
34632 attr_start = token->location;
34633 attr_end = get_finish (token->location);
34634 attr_comb = make_location (attr_start, attr_start, attr_end);
34635
34636 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
34637 {
34638 warning_at (attr_comb, OPT_Wattributes,
34639 "missing property attribute");
34640 if (token->type == CPP_CLOSE_PAREN)
34641 break;
34642 cp_lexer_consume_token (parser->lexer);
34643 continue;
34644 }
34645
34646 tree attr_name = NULL_TREE;
34647 if (identifier_p (token->u.value))
34648 attr_name = token->u.value;
34649
34650 enum rid keyword;
34651 if (token->type == CPP_NAME)
34652 keyword = C_RID_CODE (token->u.value);
34653 else if (token->type == CPP_KEYWORD
34654 && token->keyword == RID_CLASS)
34655 /* Account for accepting the 'class' keyword in this context. */
34656 keyword = RID_CLASS;
34657 else
34658 keyword = RID_MAX; /* By definition, an unknown property. */
34659 cp_lexer_consume_token (parser->lexer);
34660
34661 enum objc_property_attribute_kind prop_kind
34662 = objc_prop_attr_kind_for_rid (keyword);
34663 property_attribute_info *prop
34664 = new property_attribute_info (attr_name, attr_comb, prop_kind);
34665 prop_attr_list.safe_push (prop);
34666
34667 tree meth_name;
34668 switch (prop->prop_kind)
34669 {
34670 default: break;
34671 case OBJC_PROPERTY_ATTR_UNKNOWN:
34672 if (attr_name)
34673 error_at (attr_start, "unknown property attribute %qE",
34674 attr_name);
34675 else
34676 error_at (attr_start, "unknown property attribute");
34677 prop->parse_error = syntax_error = true;
34678 break;
34679
34680 case OBJC_PROPERTY_ATTR_GETTER:
34681 case OBJC_PROPERTY_ATTR_SETTER:
34682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34683 {
34684 attr_comb = make_location (attr_end, attr_start, attr_end);
34685 error_at (attr_comb, "expected %<=%> after Objective-C %qE",
34686 attr_name);
34687 prop->parse_error = syntax_error = true;
34688 break;
34689 }
34690
34691 token = cp_lexer_peek_token (parser->lexer);
34692 attr_end = token->location;
34693 cp_lexer_consume_token (parser->lexer); /* eat the = */
34694
34695 if (!cp_parser_objc_selector_p
34696 (cp_lexer_peek_token (parser->lexer)->type))
34697 {
34698 attr_comb = make_location (attr_end, attr_start, attr_end);
34699 error_at (attr_comb, "expected %qE selector name",
34700 attr_name);
34701 prop->parse_error = syntax_error = true;
34702 break;
34703 }
34704
34705 /* Get the end of the method name, and consume the name. */
34706 token = cp_lexer_peek_token (parser->lexer);
34707 attr_end = get_finish (token->location);
34708 /* Because method names may contain C++ keywords, we have a
34709 routine to fetch them (this also consumes the token). */
34710 meth_name = cp_parser_objc_selector (parser);
34711
34712 if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
34713 {
34714 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
34715 {
34716 attr_comb = make_location (attr_end, attr_start,
34717 attr_end);
34718 error_at (attr_comb, "setter method names must"
34719 " terminate with %<:%>");
34720 prop->parse_error = syntax_error = true;
34721 }
34722 else
34723 {
34724 attr_end = get_finish (cp_lexer_peek_token
34725 (parser->lexer)->location);
34726 cp_lexer_consume_token (parser->lexer);
34727 }
34728 attr_comb = make_location (attr_start, attr_start,
34729 attr_end);
34730 }
34731 else
34732 attr_comb = make_location (attr_start, attr_start,
34733 attr_end);
34734 prop->ident = meth_name;
34735 /* Updated location including all that was successfully
34736 parsed. */
34737 prop->prop_loc = attr_comb;
34738 break;
34739 }
34740
34741 /* If we see a comma here, then keep going - even if we already
34742 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
34743 this makes a more useful output and avoid spurious warnings
34744 about missing attributes that are, in fact, specified after the
34745 one with the syntax error. */
34746 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34747 cp_lexer_consume_token (parser->lexer);
34748 else
34749 break;
34750 }
34751
34752 if (syntax_error || !parens.require_close (parser))
34753 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34754 /*or_comma=*/false,
34755 /*consume_paren=*/true);
34756 }
34757
34758 /* 'properties' is the list of properties that we read. Usually a
34759 single one, but maybe more (eg, in "@property int a, b, c;" there
34760 are three).
34761 TODO: Update this parsing so that it accepts (erroneous) bitfields so
34762 that we can issue a meaningful and consistent (between C/C++) error
34763 message from objc_add_property_declaration (). */
34764 tree properties = cp_parser_objc_struct_declaration (parser);
34765
34766 if (properties == error_mark_node)
34767 cp_parser_skip_to_end_of_statement (parser);
34768 else if (properties == NULL_TREE)
34769 cp_parser_error (parser, "expected identifier");
34770 else
34771 {
34772 /* Comma-separated properties are chained together in reverse order;
34773 add them one by one. */
34774 properties = nreverse (properties);
34775 for (; properties; properties = TREE_CHAIN (properties))
34776 objc_add_property_declaration (loc, copy_node (properties),
34777 prop_attr_list);
34778 }
34779
34780 cp_parser_consume_semicolon_at_end_of_statement (parser);
34781
34782 while (!prop_attr_list.is_empty())
34783 delete prop_attr_list.pop ();
34784 prop_attr_list.release ();
34785 }
34786
34787 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
34788
34789 objc-synthesize-declaration:
34790 @synthesize objc-synthesize-identifier-list ;
34791
34792 objc-synthesize-identifier-list:
34793 objc-synthesize-identifier
34794 objc-synthesize-identifier-list, objc-synthesize-identifier
34795
34796 objc-synthesize-identifier
34797 identifier
34798 identifier = identifier
34799
34800 For example:
34801 @synthesize MyProperty;
34802 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
34803
34804 PS: This function is identical to c_parser_objc_at_synthesize_declaration
34805 for C. Keep them in sync.
34806 */
34807 static void
34808 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
34809 {
34810 tree list = NULL_TREE;
34811 location_t loc;
34812 loc = cp_lexer_peek_token (parser->lexer)->location;
34813
34814 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
34815 while (true)
34816 {
34817 tree property, ivar;
34818 property = cp_parser_identifier (parser);
34819 if (property == error_mark_node)
34820 {
34821 cp_parser_consume_semicolon_at_end_of_statement (parser);
34822 return;
34823 }
34824 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
34825 {
34826 cp_lexer_consume_token (parser->lexer);
34827 ivar = cp_parser_identifier (parser);
34828 if (ivar == error_mark_node)
34829 {
34830 cp_parser_consume_semicolon_at_end_of_statement (parser);
34831 return;
34832 }
34833 }
34834 else
34835 ivar = NULL_TREE;
34836 list = chainon (list, build_tree_list (ivar, property));
34837 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34838 cp_lexer_consume_token (parser->lexer);
34839 else
34840 break;
34841 }
34842 cp_parser_consume_semicolon_at_end_of_statement (parser);
34843 objc_add_synthesize_declaration (loc, list);
34844 }
34845
34846 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
34847
34848 objc-dynamic-declaration:
34849 @dynamic identifier-list ;
34850
34851 For example:
34852 @dynamic MyProperty;
34853 @dynamic MyProperty, AnotherProperty;
34854
34855 PS: This function is identical to c_parser_objc_at_dynamic_declaration
34856 for C. Keep them in sync.
34857 */
34858 static void
34859 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
34860 {
34861 tree list = NULL_TREE;
34862 location_t loc;
34863 loc = cp_lexer_peek_token (parser->lexer)->location;
34864
34865 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
34866 while (true)
34867 {
34868 tree property;
34869 property = cp_parser_identifier (parser);
34870 if (property == error_mark_node)
34871 {
34872 cp_parser_consume_semicolon_at_end_of_statement (parser);
34873 return;
34874 }
34875 list = chainon (list, build_tree_list (NULL, property));
34876 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34877 cp_lexer_consume_token (parser->lexer);
34878 else
34879 break;
34880 }
34881 cp_parser_consume_semicolon_at_end_of_statement (parser);
34882 objc_add_dynamic_declaration (loc, list);
34883 }
34884
34885 \f
34886 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
34887
34888 /* Returns name of the next clause.
34889 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
34890 the token is not consumed. Otherwise appropriate pragma_omp_clause is
34891 returned and the token is consumed. */
34892
34893 static pragma_omp_clause
34894 cp_parser_omp_clause_name (cp_parser *parser)
34895 {
34896 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
34897
34898 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
34899 result = PRAGMA_OACC_CLAUSE_AUTO;
34900 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
34901 result = PRAGMA_OMP_CLAUSE_IF;
34902 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
34903 result = PRAGMA_OMP_CLAUSE_DEFAULT;
34904 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
34905 result = PRAGMA_OACC_CLAUSE_DELETE;
34906 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
34907 result = PRAGMA_OMP_CLAUSE_PRIVATE;
34908 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34909 result = PRAGMA_OMP_CLAUSE_FOR;
34910 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34911 {
34912 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34913 const char *p = IDENTIFIER_POINTER (id);
34914
34915 switch (p[0])
34916 {
34917 case 'a':
34918 if (!strcmp ("aligned", p))
34919 result = PRAGMA_OMP_CLAUSE_ALIGNED;
34920 else if (!strcmp ("allocate", p))
34921 result = PRAGMA_OMP_CLAUSE_ALLOCATE;
34922 else if (!strcmp ("async", p))
34923 result = PRAGMA_OACC_CLAUSE_ASYNC;
34924 else if (!strcmp ("attach", p))
34925 result = PRAGMA_OACC_CLAUSE_ATTACH;
34926 break;
34927 case 'b':
34928 if (!strcmp ("bind", p))
34929 result = PRAGMA_OMP_CLAUSE_BIND;
34930 break;
34931 case 'c':
34932 if (!strcmp ("collapse", p))
34933 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
34934 else if (!strcmp ("copy", p))
34935 result = PRAGMA_OACC_CLAUSE_COPY;
34936 else if (!strcmp ("copyin", p))
34937 result = PRAGMA_OMP_CLAUSE_COPYIN;
34938 else if (!strcmp ("copyout", p))
34939 result = PRAGMA_OACC_CLAUSE_COPYOUT;
34940 else if (!strcmp ("copyprivate", p))
34941 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
34942 else if (!strcmp ("create", p))
34943 result = PRAGMA_OACC_CLAUSE_CREATE;
34944 break;
34945 case 'd':
34946 if (!strcmp ("defaultmap", p))
34947 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
34948 else if (!strcmp ("depend", p))
34949 result = PRAGMA_OMP_CLAUSE_DEPEND;
34950 else if (!strcmp ("detach", p))
34951 result = PRAGMA_OACC_CLAUSE_DETACH;
34952 else if (!strcmp ("device", p))
34953 result = PRAGMA_OMP_CLAUSE_DEVICE;
34954 else if (!strcmp ("deviceptr", p))
34955 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
34956 else if (!strcmp ("device_resident", p))
34957 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
34958 else if (!strcmp ("device_type", p))
34959 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
34960 else if (!strcmp ("dist_schedule", p))
34961 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
34962 break;
34963 case 'f':
34964 if (!strcmp ("final", p))
34965 result = PRAGMA_OMP_CLAUSE_FINAL;
34966 else if (!strcmp ("finalize", p))
34967 result = PRAGMA_OACC_CLAUSE_FINALIZE;
34968 else if (!strcmp ("firstprivate", p))
34969 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
34970 else if (!strcmp ("from", p))
34971 result = PRAGMA_OMP_CLAUSE_FROM;
34972 break;
34973 case 'g':
34974 if (!strcmp ("gang", p))
34975 result = PRAGMA_OACC_CLAUSE_GANG;
34976 else if (!strcmp ("grainsize", p))
34977 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
34978 break;
34979 case 'h':
34980 if (!strcmp ("hint", p))
34981 result = PRAGMA_OMP_CLAUSE_HINT;
34982 else if (!strcmp ("host", p))
34983 result = PRAGMA_OACC_CLAUSE_HOST;
34984 break;
34985 case 'i':
34986 if (!strcmp ("if_present", p))
34987 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
34988 else if (!strcmp ("in_reduction", p))
34989 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
34990 else if (!strcmp ("inbranch", p))
34991 result = PRAGMA_OMP_CLAUSE_INBRANCH;
34992 else if (!strcmp ("independent", p))
34993 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
34994 else if (!strcmp ("is_device_ptr", p))
34995 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
34996 break;
34997 case 'l':
34998 if (!strcmp ("lastprivate", p))
34999 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
35000 else if (!strcmp ("linear", p))
35001 result = PRAGMA_OMP_CLAUSE_LINEAR;
35002 else if (!strcmp ("link", p))
35003 result = PRAGMA_OMP_CLAUSE_LINK;
35004 break;
35005 case 'm':
35006 if (!strcmp ("map", p))
35007 result = PRAGMA_OMP_CLAUSE_MAP;
35008 else if (!strcmp ("mergeable", p))
35009 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
35010 break;
35011 case 'n':
35012 if (!strcmp ("no_create", p))
35013 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
35014 else if (!strcmp ("nogroup", p))
35015 result = PRAGMA_OMP_CLAUSE_NOGROUP;
35016 else if (!strcmp ("nontemporal", p))
35017 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
35018 else if (!strcmp ("notinbranch", p))
35019 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
35020 else if (!strcmp ("nowait", p))
35021 result = PRAGMA_OMP_CLAUSE_NOWAIT;
35022 else if (!strcmp ("num_gangs", p))
35023 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
35024 else if (!strcmp ("num_tasks", p))
35025 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
35026 else if (!strcmp ("num_teams", p))
35027 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
35028 else if (!strcmp ("num_threads", p))
35029 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
35030 else if (!strcmp ("num_workers", p))
35031 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
35032 break;
35033 case 'o':
35034 if (!strcmp ("ordered", p))
35035 result = PRAGMA_OMP_CLAUSE_ORDERED;
35036 else if (!strcmp ("order", p))
35037 result = PRAGMA_OMP_CLAUSE_ORDER;
35038 break;
35039 case 'p':
35040 if (!strcmp ("parallel", p))
35041 result = PRAGMA_OMP_CLAUSE_PARALLEL;
35042 else if (!strcmp ("present", p))
35043 result = PRAGMA_OACC_CLAUSE_PRESENT;
35044 else if (!strcmp ("present_or_copy", p)
35045 || !strcmp ("pcopy", p))
35046 result = PRAGMA_OACC_CLAUSE_COPY;
35047 else if (!strcmp ("present_or_copyin", p)
35048 || !strcmp ("pcopyin", p))
35049 result = PRAGMA_OACC_CLAUSE_COPYIN;
35050 else if (!strcmp ("present_or_copyout", p)
35051 || !strcmp ("pcopyout", p))
35052 result = PRAGMA_OACC_CLAUSE_COPYOUT;
35053 else if (!strcmp ("present_or_create", p)
35054 || !strcmp ("pcreate", p))
35055 result = PRAGMA_OACC_CLAUSE_CREATE;
35056 else if (!strcmp ("priority", p))
35057 result = PRAGMA_OMP_CLAUSE_PRIORITY;
35058 else if (!strcmp ("proc_bind", p))
35059 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
35060 break;
35061 case 'r':
35062 if (!strcmp ("reduction", p))
35063 result = PRAGMA_OMP_CLAUSE_REDUCTION;
35064 break;
35065 case 's':
35066 if (!strcmp ("safelen", p))
35067 result = PRAGMA_OMP_CLAUSE_SAFELEN;
35068 else if (!strcmp ("schedule", p))
35069 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
35070 else if (!strcmp ("sections", p))
35071 result = PRAGMA_OMP_CLAUSE_SECTIONS;
35072 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
35073 result = PRAGMA_OACC_CLAUSE_HOST;
35074 else if (!strcmp ("seq", p))
35075 result = PRAGMA_OACC_CLAUSE_SEQ;
35076 else if (!strcmp ("shared", p))
35077 result = PRAGMA_OMP_CLAUSE_SHARED;
35078 else if (!strcmp ("simd", p))
35079 result = PRAGMA_OMP_CLAUSE_SIMD;
35080 else if (!strcmp ("simdlen", p))
35081 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
35082 break;
35083 case 't':
35084 if (!strcmp ("task_reduction", p))
35085 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
35086 else if (!strcmp ("taskgroup", p))
35087 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
35088 else if (!strcmp ("thread_limit", p))
35089 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
35090 else if (!strcmp ("threads", p))
35091 result = PRAGMA_OMP_CLAUSE_THREADS;
35092 else if (!strcmp ("tile", p))
35093 result = PRAGMA_OACC_CLAUSE_TILE;
35094 else if (!strcmp ("to", p))
35095 result = PRAGMA_OMP_CLAUSE_TO;
35096 break;
35097 case 'u':
35098 if (!strcmp ("uniform", p))
35099 result = PRAGMA_OMP_CLAUSE_UNIFORM;
35100 else if (!strcmp ("untied", p))
35101 result = PRAGMA_OMP_CLAUSE_UNTIED;
35102 else if (!strcmp ("use_device", p))
35103 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
35104 else if (!strcmp ("use_device_addr", p))
35105 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
35106 else if (!strcmp ("use_device_ptr", p))
35107 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
35108 break;
35109 case 'v':
35110 if (!strcmp ("vector", p))
35111 result = PRAGMA_OACC_CLAUSE_VECTOR;
35112 else if (!strcmp ("vector_length", p))
35113 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
35114 break;
35115 case 'w':
35116 if (!strcmp ("wait", p))
35117 result = PRAGMA_OACC_CLAUSE_WAIT;
35118 else if (!strcmp ("worker", p))
35119 result = PRAGMA_OACC_CLAUSE_WORKER;
35120 break;
35121 }
35122 }
35123
35124 if (result != PRAGMA_OMP_CLAUSE_NONE)
35125 cp_lexer_consume_token (parser->lexer);
35126
35127 return result;
35128 }
35129
35130 /* Validate that a clause of the given type does not already exist. */
35131
35132 static void
35133 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
35134 const char *name, location_t location)
35135 {
35136 if (omp_find_clause (clauses, code))
35137 error_at (location, "too many %qs clauses", name);
35138 }
35139
35140 /* OpenMP 2.5:
35141 variable-list:
35142 identifier
35143 variable-list , identifier
35144
35145 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
35146 colon). An opening parenthesis will have been consumed by the caller.
35147
35148 If KIND is nonzero, create the appropriate node and install the decl
35149 in OMP_CLAUSE_DECL and add the node to the head of the list.
35150
35151 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
35152 return the list created.
35153
35154 COLON can be NULL if only closing parenthesis should end the list,
35155 or pointer to bool which will receive false if the list is terminated
35156 by closing parenthesis or true if the list is terminated by colon.
35157
35158 The optional ALLOW_DEREF argument is true if list items can use the deref
35159 (->) operator. */
35160
35161 static tree
35162 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
35163 tree list, bool *colon,
35164 bool allow_deref = false)
35165 {
35166 cp_token *token;
35167 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
35168 if (colon)
35169 {
35170 parser->colon_corrects_to_scope_p = false;
35171 *colon = false;
35172 }
35173 while (1)
35174 {
35175 tree name, decl;
35176
35177 if (kind == OMP_CLAUSE_DEPEND)
35178 cp_parser_parse_tentatively (parser);
35179 token = cp_lexer_peek_token (parser->lexer);
35180 if (kind != 0
35181 && current_class_ptr
35182 && cp_parser_is_keyword (token, RID_THIS))
35183 {
35184 decl = finish_this_expr ();
35185 if (TREE_CODE (decl) == NON_LVALUE_EXPR
35186 || CONVERT_EXPR_P (decl))
35187 decl = TREE_OPERAND (decl, 0);
35188 cp_lexer_consume_token (parser->lexer);
35189 }
35190 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
35191 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
35192 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
35193 {
35194 cp_id_kind idk;
35195 decl = cp_parser_primary_expression (parser, false, false, false,
35196 &idk);
35197 }
35198 else
35199 {
35200 name = cp_parser_id_expression (parser, /*template_p=*/false,
35201 /*check_dependency_p=*/true,
35202 /*template_p=*/NULL,
35203 /*declarator_p=*/false,
35204 /*optional_p=*/false);
35205 if (name == error_mark_node)
35206 {
35207 if (kind == OMP_CLAUSE_DEPEND
35208 && cp_parser_simulate_error (parser))
35209 goto depend_lvalue;
35210 goto skip_comma;
35211 }
35212
35213 if (identifier_p (name))
35214 decl = cp_parser_lookup_name_simple (parser, name, token->location);
35215 else
35216 decl = name;
35217 if (decl == error_mark_node)
35218 {
35219 if (kind == OMP_CLAUSE_DEPEND
35220 && cp_parser_simulate_error (parser))
35221 goto depend_lvalue;
35222 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
35223 token->location);
35224 }
35225 }
35226 if (outer_automatic_var_p (decl))
35227 decl = process_outer_var_ref (decl, tf_warning_or_error);
35228 if (decl == error_mark_node)
35229 ;
35230 else if (kind != 0)
35231 {
35232 switch (kind)
35233 {
35234 case OMP_CLAUSE__CACHE_:
35235 /* The OpenACC cache directive explicitly only allows "array
35236 elements or subarrays". */
35237 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
35238 {
35239 error_at (token->location, "expected %<[%>");
35240 decl = error_mark_node;
35241 break;
35242 }
35243 /* FALLTHROUGH. */
35244 case OMP_CLAUSE_MAP:
35245 case OMP_CLAUSE_FROM:
35246 case OMP_CLAUSE_TO:
35247 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
35248 || (allow_deref
35249 && cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
35250 {
35251 cpp_ttype ttype
35252 = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
35253 ? CPP_DOT : CPP_DEREF;
35254 location_t loc
35255 = cp_lexer_peek_token (parser->lexer)->location;
35256 cp_id_kind idk = CP_ID_KIND_NONE;
35257 cp_lexer_consume_token (parser->lexer);
35258 decl = convert_from_reference (decl);
35259 decl
35260 = cp_parser_postfix_dot_deref_expression (parser, ttype,
35261 decl, false,
35262 &idk, loc);
35263 }
35264 /* FALLTHROUGH. */
35265 case OMP_CLAUSE_DEPEND:
35266 case OMP_CLAUSE_REDUCTION:
35267 case OMP_CLAUSE_IN_REDUCTION:
35268 case OMP_CLAUSE_TASK_REDUCTION:
35269 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
35270 {
35271 tree low_bound = NULL_TREE, length = NULL_TREE;
35272
35273 parser->colon_corrects_to_scope_p = false;
35274 cp_lexer_consume_token (parser->lexer);
35275 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
35276 {
35277 low_bound = cp_parser_expression (parser);
35278 /* Later handling is not prepared to see through these. */
35279 gcc_checking_assert (!location_wrapper_p (low_bound));
35280 }
35281 if (!colon)
35282 parser->colon_corrects_to_scope_p
35283 = saved_colon_corrects_to_scope_p;
35284 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
35285 length = integer_one_node;
35286 else
35287 {
35288 /* Look for `:'. */
35289 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35290 {
35291 if (kind == OMP_CLAUSE_DEPEND
35292 && cp_parser_simulate_error (parser))
35293 goto depend_lvalue;
35294 goto skip_comma;
35295 }
35296 if (kind == OMP_CLAUSE_DEPEND)
35297 cp_parser_commit_to_tentative_parse (parser);
35298 if (!cp_lexer_next_token_is (parser->lexer,
35299 CPP_CLOSE_SQUARE))
35300 {
35301 length = cp_parser_expression (parser);
35302 /* Later handling is not prepared to see through these. */
35303 gcc_checking_assert (!location_wrapper_p (length));
35304 }
35305 }
35306 /* Look for the closing `]'. */
35307 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
35308 RT_CLOSE_SQUARE))
35309 {
35310 if (kind == OMP_CLAUSE_DEPEND
35311 && cp_parser_simulate_error (parser))
35312 goto depend_lvalue;
35313 goto skip_comma;
35314 }
35315
35316 decl = tree_cons (low_bound, length, decl);
35317 }
35318 break;
35319 default:
35320 break;
35321 }
35322
35323 if (kind == OMP_CLAUSE_DEPEND)
35324 {
35325 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
35326 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
35327 && cp_parser_simulate_error (parser))
35328 {
35329 depend_lvalue:
35330 cp_parser_abort_tentative_parse (parser);
35331 decl = cp_parser_assignment_expression (parser, NULL,
35332 false, false);
35333 }
35334 else
35335 cp_parser_parse_definitely (parser);
35336 }
35337
35338 tree u = build_omp_clause (token->location, kind);
35339 OMP_CLAUSE_DECL (u) = decl;
35340 OMP_CLAUSE_CHAIN (u) = list;
35341 list = u;
35342 }
35343 else
35344 list = tree_cons (decl, NULL_TREE, list);
35345
35346 get_comma:
35347 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
35348 break;
35349 cp_lexer_consume_token (parser->lexer);
35350 }
35351
35352 if (colon)
35353 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
35354
35355 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
35356 {
35357 *colon = true;
35358 cp_parser_require (parser, CPP_COLON, RT_COLON);
35359 return list;
35360 }
35361
35362 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35363 {
35364 int ending;
35365
35366 /* Try to resync to an unnested comma. Copied from
35367 cp_parser_parenthesized_expression_list. */
35368 skip_comma:
35369 if (colon)
35370 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
35371 ending = cp_parser_skip_to_closing_parenthesis (parser,
35372 /*recovering=*/true,
35373 /*or_comma=*/true,
35374 /*consume_paren=*/true);
35375 if (ending < 0)
35376 goto get_comma;
35377 }
35378
35379 return list;
35380 }
35381
35382 /* Similarly, but expect leading and trailing parenthesis. This is a very
35383 common case for omp clauses. */
35384
35385 static tree
35386 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
35387 bool allow_deref = false)
35388 {
35389 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35390 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
35391 allow_deref);
35392 return list;
35393 }
35394
35395 /* OpenACC 2.0:
35396 copy ( variable-list )
35397 copyin ( variable-list )
35398 copyout ( variable-list )
35399 create ( variable-list )
35400 delete ( variable-list )
35401 present ( variable-list )
35402
35403 OpenACC 2.6:
35404 no_create ( variable-list )
35405 attach ( variable-list )
35406 detach ( variable-list ) */
35407
35408 static tree
35409 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
35410 tree list)
35411 {
35412 enum gomp_map_kind kind;
35413 switch (c_kind)
35414 {
35415 case PRAGMA_OACC_CLAUSE_ATTACH:
35416 kind = GOMP_MAP_ATTACH;
35417 break;
35418 case PRAGMA_OACC_CLAUSE_COPY:
35419 kind = GOMP_MAP_TOFROM;
35420 break;
35421 case PRAGMA_OACC_CLAUSE_COPYIN:
35422 kind = GOMP_MAP_TO;
35423 break;
35424 case PRAGMA_OACC_CLAUSE_COPYOUT:
35425 kind = GOMP_MAP_FROM;
35426 break;
35427 case PRAGMA_OACC_CLAUSE_CREATE:
35428 kind = GOMP_MAP_ALLOC;
35429 break;
35430 case PRAGMA_OACC_CLAUSE_DELETE:
35431 kind = GOMP_MAP_RELEASE;
35432 break;
35433 case PRAGMA_OACC_CLAUSE_DETACH:
35434 kind = GOMP_MAP_DETACH;
35435 break;
35436 case PRAGMA_OACC_CLAUSE_DEVICE:
35437 kind = GOMP_MAP_FORCE_TO;
35438 break;
35439 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
35440 kind = GOMP_MAP_DEVICE_RESIDENT;
35441 break;
35442 case PRAGMA_OACC_CLAUSE_HOST:
35443 kind = GOMP_MAP_FORCE_FROM;
35444 break;
35445 case PRAGMA_OACC_CLAUSE_LINK:
35446 kind = GOMP_MAP_LINK;
35447 break;
35448 case PRAGMA_OACC_CLAUSE_NO_CREATE:
35449 kind = GOMP_MAP_IF_PRESENT;
35450 break;
35451 case PRAGMA_OACC_CLAUSE_PRESENT:
35452 kind = GOMP_MAP_FORCE_PRESENT;
35453 break;
35454 default:
35455 gcc_unreachable ();
35456 }
35457 tree nl, c;
35458 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, true);
35459
35460 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
35461 OMP_CLAUSE_SET_MAP_KIND (c, kind);
35462
35463 return nl;
35464 }
35465
35466 /* OpenACC 2.0:
35467 deviceptr ( variable-list ) */
35468
35469 static tree
35470 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
35471 {
35472 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35473 tree vars, t;
35474
35475 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
35476 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
35477 variable-list must only allow for pointer variables. */
35478 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35479 for (t = vars; t; t = TREE_CHAIN (t))
35480 {
35481 tree v = TREE_PURPOSE (t);
35482 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
35483 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
35484 OMP_CLAUSE_DECL (u) = v;
35485 OMP_CLAUSE_CHAIN (u) = list;
35486 list = u;
35487 }
35488
35489 return list;
35490 }
35491
35492 /* OpenACC 2.5:
35493 auto
35494 finalize
35495 independent
35496 nohost
35497 seq */
35498
35499 static tree
35500 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
35501 tree list)
35502 {
35503 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
35504
35505 tree c = build_omp_clause (loc, code);
35506 OMP_CLAUSE_CHAIN (c) = list;
35507
35508 return c;
35509 }
35510
35511 /* OpenACC:
35512 num_gangs ( expression )
35513 num_workers ( expression )
35514 vector_length ( expression ) */
35515
35516 static tree
35517 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
35518 const char *str, tree list)
35519 {
35520 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35521
35522 matching_parens parens;
35523 if (!parens.require_open (parser))
35524 return list;
35525
35526 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
35527
35528 if (t == error_mark_node
35529 || !parens.require_close (parser))
35530 {
35531 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35532 /*or_comma=*/false,
35533 /*consume_paren=*/true);
35534 return list;
35535 }
35536
35537 check_no_duplicate_clause (list, code, str, loc);
35538
35539 tree c = build_omp_clause (loc, code);
35540 OMP_CLAUSE_OPERAND (c, 0) = t;
35541 OMP_CLAUSE_CHAIN (c) = list;
35542 return c;
35543 }
35544
35545 /* OpenACC:
35546
35547 gang [( gang-arg-list )]
35548 worker [( [num:] int-expr )]
35549 vector [( [length:] int-expr )]
35550
35551 where gang-arg is one of:
35552
35553 [num:] int-expr
35554 static: size-expr
35555
35556 and size-expr may be:
35557
35558 *
35559 int-expr
35560 */
35561
35562 static tree
35563 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
35564 omp_clause_code kind,
35565 const char *str, tree list)
35566 {
35567 const char *id = "num";
35568 cp_lexer *lexer = parser->lexer;
35569 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
35570
35571 if (kind == OMP_CLAUSE_VECTOR)
35572 id = "length";
35573
35574 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
35575 {
35576 matching_parens parens;
35577 parens.consume_open (parser);
35578
35579 do
35580 {
35581 cp_token *next = cp_lexer_peek_token (lexer);
35582 int idx = 0;
35583
35584 /* Gang static argument. */
35585 if (kind == OMP_CLAUSE_GANG
35586 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
35587 {
35588 cp_lexer_consume_token (lexer);
35589
35590 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35591 goto cleanup_error;
35592
35593 idx = 1;
35594 if (ops[idx] != NULL)
35595 {
35596 cp_parser_error (parser, "too many %<static%> arguments");
35597 goto cleanup_error;
35598 }
35599
35600 /* Check for the '*' argument. */
35601 if (cp_lexer_next_token_is (lexer, CPP_MULT)
35602 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
35603 || cp_lexer_nth_token_is (parser->lexer, 2,
35604 CPP_CLOSE_PAREN)))
35605 {
35606 cp_lexer_consume_token (lexer);
35607 ops[idx] = integer_minus_one_node;
35608
35609 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
35610 {
35611 cp_lexer_consume_token (lexer);
35612 continue;
35613 }
35614 else break;
35615 }
35616 }
35617 /* Worker num: argument and vector length: arguments. */
35618 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
35619 && id_equal (next->u.value, id)
35620 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
35621 {
35622 cp_lexer_consume_token (lexer); /* id */
35623 cp_lexer_consume_token (lexer); /* ':' */
35624 }
35625
35626 /* Now collect the actual argument. */
35627 if (ops[idx] != NULL_TREE)
35628 {
35629 cp_parser_error (parser, "unexpected argument");
35630 goto cleanup_error;
35631 }
35632
35633 tree expr = cp_parser_assignment_expression (parser, NULL, false,
35634 false);
35635 if (expr == error_mark_node)
35636 goto cleanup_error;
35637
35638 mark_exp_read (expr);
35639 ops[idx] = expr;
35640
35641 if (kind == OMP_CLAUSE_GANG
35642 && cp_lexer_next_token_is (lexer, CPP_COMMA))
35643 {
35644 cp_lexer_consume_token (lexer);
35645 continue;
35646 }
35647 break;
35648 }
35649 while (1);
35650
35651 if (!parens.require_close (parser))
35652 goto cleanup_error;
35653 }
35654
35655 check_no_duplicate_clause (list, kind, str, loc);
35656
35657 c = build_omp_clause (loc, kind);
35658
35659 if (ops[1])
35660 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
35661
35662 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
35663 OMP_CLAUSE_CHAIN (c) = list;
35664
35665 return c;
35666
35667 cleanup_error:
35668 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
35669 return list;
35670 }
35671
35672 /* OpenACC 2.0:
35673 tile ( size-expr-list ) */
35674
35675 static tree
35676 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
35677 {
35678 tree c, expr = error_mark_node;
35679 tree tile = NULL_TREE;
35680
35681 /* Collapse and tile are mutually exclusive. (The spec doesn't say
35682 so, but the spec authors never considered such a case and have
35683 differing opinions on what it might mean, including 'not
35684 allowed'.) */
35685 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
35686 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
35687 clause_loc);
35688
35689 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35690 return list;
35691
35692 do
35693 {
35694 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
35695 return list;
35696
35697 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
35698 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
35699 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
35700 {
35701 cp_lexer_consume_token (parser->lexer);
35702 expr = integer_zero_node;
35703 }
35704 else
35705 expr = cp_parser_constant_expression (parser);
35706
35707 tile = tree_cons (NULL_TREE, expr, tile);
35708 }
35709 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
35710
35711 /* Consume the trailing ')'. */
35712 cp_lexer_consume_token (parser->lexer);
35713
35714 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
35715 tile = nreverse (tile);
35716 OMP_CLAUSE_TILE_LIST (c) = tile;
35717 OMP_CLAUSE_CHAIN (c) = list;
35718 return c;
35719 }
35720
35721 /* OpenACC 2.0
35722 Parse wait clause or directive parameters. */
35723
35724 static tree
35725 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
35726 {
35727 vec<tree, va_gc> *args;
35728 tree t, args_tree;
35729
35730 args = cp_parser_parenthesized_expression_list (parser, non_attr,
35731 /*cast_p=*/false,
35732 /*allow_expansion_p=*/true,
35733 /*non_constant_p=*/NULL);
35734
35735 if (args == NULL || args->length () == 0)
35736 {
35737 if (args != NULL)
35738 {
35739 cp_parser_error (parser, "expected integer expression list");
35740 release_tree_vector (args);
35741 }
35742 return list;
35743 }
35744
35745 args_tree = build_tree_list_vec (args);
35746
35747 release_tree_vector (args);
35748
35749 for (t = args_tree; t; t = TREE_CHAIN (t))
35750 {
35751 tree targ = TREE_VALUE (t);
35752
35753 if (targ != error_mark_node)
35754 {
35755 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
35756 error ("%<wait%> expression must be integral");
35757 else
35758 {
35759 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
35760
35761 targ = mark_rvalue_use (targ);
35762 OMP_CLAUSE_DECL (c) = targ;
35763 OMP_CLAUSE_CHAIN (c) = list;
35764 list = c;
35765 }
35766 }
35767 }
35768
35769 return list;
35770 }
35771
35772 /* OpenACC:
35773 wait [( int-expr-list )] */
35774
35775 static tree
35776 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
35777 {
35778 location_t location = cp_lexer_peek_token (parser->lexer)->location;
35779
35780 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35781 list = cp_parser_oacc_wait_list (parser, location, list);
35782 else
35783 {
35784 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
35785
35786 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
35787 OMP_CLAUSE_CHAIN (c) = list;
35788 list = c;
35789 }
35790
35791 return list;
35792 }
35793
35794 /* OpenMP 3.0:
35795 collapse ( constant-expression ) */
35796
35797 static tree
35798 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
35799 {
35800 tree c, num;
35801 location_t loc;
35802 HOST_WIDE_INT n;
35803
35804 loc = cp_lexer_peek_token (parser->lexer)->location;
35805 matching_parens parens;
35806 if (!parens.require_open (parser))
35807 return list;
35808
35809 num = cp_parser_constant_expression (parser);
35810
35811 if (!parens.require_close (parser))
35812 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35813 /*or_comma=*/false,
35814 /*consume_paren=*/true);
35815
35816 if (num == error_mark_node)
35817 return list;
35818 num = fold_non_dependent_expr (num);
35819 if (!tree_fits_shwi_p (num)
35820 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
35821 || (n = tree_to_shwi (num)) <= 0
35822 || (int) n != n)
35823 {
35824 error_at (loc, "collapse argument needs positive constant integer expression");
35825 return list;
35826 }
35827
35828 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
35829 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
35830 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
35831 OMP_CLAUSE_CHAIN (c) = list;
35832 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
35833
35834 return c;
35835 }
35836
35837 /* OpenMP 2.5:
35838 default ( none | shared )
35839
35840 OpenACC:
35841 default ( none | present ) */
35842
35843 static tree
35844 cp_parser_omp_clause_default (cp_parser *parser, tree list,
35845 location_t location, bool is_oacc)
35846 {
35847 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
35848 tree c;
35849
35850 matching_parens parens;
35851 if (!parens.require_open (parser))
35852 return list;
35853 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35854 {
35855 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35856 const char *p = IDENTIFIER_POINTER (id);
35857
35858 switch (p[0])
35859 {
35860 case 'n':
35861 if (strcmp ("none", p) != 0)
35862 goto invalid_kind;
35863 kind = OMP_CLAUSE_DEFAULT_NONE;
35864 break;
35865
35866 case 'p':
35867 if (strcmp ("present", p) != 0 || !is_oacc)
35868 goto invalid_kind;
35869 kind = OMP_CLAUSE_DEFAULT_PRESENT;
35870 break;
35871
35872 case 's':
35873 if (strcmp ("shared", p) != 0 || is_oacc)
35874 goto invalid_kind;
35875 kind = OMP_CLAUSE_DEFAULT_SHARED;
35876 break;
35877
35878 default:
35879 goto invalid_kind;
35880 }
35881
35882 cp_lexer_consume_token (parser->lexer);
35883 }
35884 else
35885 {
35886 invalid_kind:
35887 if (is_oacc)
35888 cp_parser_error (parser, "expected %<none%> or %<present%>");
35889 else
35890 cp_parser_error (parser, "expected %<none%> or %<shared%>");
35891 }
35892
35893 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
35894 || !parens.require_close (parser))
35895 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35896 /*or_comma=*/false,
35897 /*consume_paren=*/true);
35898
35899 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
35900 return list;
35901
35902 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
35903 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
35904 OMP_CLAUSE_CHAIN (c) = list;
35905 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
35906
35907 return c;
35908 }
35909
35910 /* OpenMP 3.1:
35911 final ( expression ) */
35912
35913 static tree
35914 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
35915 {
35916 tree t, c;
35917
35918 matching_parens parens;
35919 if (!parens.require_open (parser))
35920 return list;
35921
35922 t = cp_parser_assignment_expression (parser);
35923
35924 if (t == error_mark_node
35925 || !parens.require_close (parser))
35926 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35927 /*or_comma=*/false,
35928 /*consume_paren=*/true);
35929
35930 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
35931
35932 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
35933 OMP_CLAUSE_FINAL_EXPR (c) = t;
35934 OMP_CLAUSE_CHAIN (c) = list;
35935
35936 return c;
35937 }
35938
35939 /* OpenMP 2.5:
35940 if ( expression )
35941
35942 OpenMP 4.5:
35943 if ( directive-name-modifier : expression )
35944
35945 directive-name-modifier:
35946 parallel | task | taskloop | target data | target | target update
35947 | target enter data | target exit data
35948
35949 OpenMP 5.0:
35950 directive-name-modifier:
35951 ... | simd | cancel */
35952
35953 static tree
35954 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
35955 bool is_omp)
35956 {
35957 tree t, c;
35958 enum tree_code if_modifier = ERROR_MARK;
35959
35960 matching_parens parens;
35961 if (!parens.require_open (parser))
35962 return list;
35963
35964 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35965 {
35966 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35967 const char *p = IDENTIFIER_POINTER (id);
35968 int n = 2;
35969
35970 if (strcmp ("cancel", p) == 0)
35971 if_modifier = VOID_CST;
35972 else if (strcmp ("parallel", p) == 0)
35973 if_modifier = OMP_PARALLEL;
35974 else if (strcmp ("simd", p) == 0)
35975 if_modifier = OMP_SIMD;
35976 else if (strcmp ("task", p) == 0)
35977 if_modifier = OMP_TASK;
35978 else if (strcmp ("taskloop", p) == 0)
35979 if_modifier = OMP_TASKLOOP;
35980 else if (strcmp ("target", p) == 0)
35981 {
35982 if_modifier = OMP_TARGET;
35983 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
35984 {
35985 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
35986 p = IDENTIFIER_POINTER (id);
35987 if (strcmp ("data", p) == 0)
35988 if_modifier = OMP_TARGET_DATA;
35989 else if (strcmp ("update", p) == 0)
35990 if_modifier = OMP_TARGET_UPDATE;
35991 else if (strcmp ("enter", p) == 0)
35992 if_modifier = OMP_TARGET_ENTER_DATA;
35993 else if (strcmp ("exit", p) == 0)
35994 if_modifier = OMP_TARGET_EXIT_DATA;
35995 if (if_modifier != OMP_TARGET)
35996 n = 3;
35997 else
35998 {
35999 location_t loc
36000 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
36001 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
36002 "or %<exit%>");
36003 if_modifier = ERROR_MARK;
36004 }
36005 if (if_modifier == OMP_TARGET_ENTER_DATA
36006 || if_modifier == OMP_TARGET_EXIT_DATA)
36007 {
36008 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
36009 {
36010 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
36011 p = IDENTIFIER_POINTER (id);
36012 if (strcmp ("data", p) == 0)
36013 n = 4;
36014 }
36015 if (n != 4)
36016 {
36017 location_t loc
36018 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
36019 error_at (loc, "expected %<data%>");
36020 if_modifier = ERROR_MARK;
36021 }
36022 }
36023 }
36024 }
36025 if (if_modifier != ERROR_MARK)
36026 {
36027 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
36028 {
36029 while (n-- > 0)
36030 cp_lexer_consume_token (parser->lexer);
36031 }
36032 else
36033 {
36034 if (n > 2)
36035 {
36036 location_t loc
36037 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
36038 error_at (loc, "expected %<:%>");
36039 }
36040 if_modifier = ERROR_MARK;
36041 }
36042 }
36043 }
36044
36045 t = cp_parser_assignment_expression (parser);
36046
36047 if (t == error_mark_node
36048 || !parens.require_close (parser))
36049 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36050 /*or_comma=*/false,
36051 /*consume_paren=*/true);
36052
36053 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
36054 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
36055 {
36056 if (if_modifier != ERROR_MARK
36057 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
36058 {
36059 const char *p = NULL;
36060 switch (if_modifier)
36061 {
36062 case VOID_CST: p = "cancel"; break;
36063 case OMP_PARALLEL: p = "parallel"; break;
36064 case OMP_SIMD: p = "simd"; break;
36065 case OMP_TASK: p = "task"; break;
36066 case OMP_TASKLOOP: p = "taskloop"; break;
36067 case OMP_TARGET_DATA: p = "target data"; break;
36068 case OMP_TARGET: p = "target"; break;
36069 case OMP_TARGET_UPDATE: p = "target update"; break;
36070 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
36071 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
36072 default: gcc_unreachable ();
36073 }
36074 error_at (location, "too many %<if%> clauses with %qs modifier",
36075 p);
36076 return list;
36077 }
36078 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
36079 {
36080 if (!is_omp)
36081 error_at (location, "too many %<if%> clauses");
36082 else
36083 error_at (location, "too many %<if%> clauses without modifier");
36084 return list;
36085 }
36086 else if (if_modifier == ERROR_MARK
36087 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
36088 {
36089 error_at (location, "if any %<if%> clause has modifier, then all "
36090 "%<if%> clauses have to use modifier");
36091 return list;
36092 }
36093 }
36094
36095 c = build_omp_clause (location, OMP_CLAUSE_IF);
36096 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
36097 OMP_CLAUSE_IF_EXPR (c) = t;
36098 OMP_CLAUSE_CHAIN (c) = list;
36099
36100 return c;
36101 }
36102
36103 /* OpenMP 3.1:
36104 mergeable */
36105
36106 static tree
36107 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
36108 tree list, location_t location)
36109 {
36110 tree c;
36111
36112 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
36113 location);
36114
36115 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
36116 OMP_CLAUSE_CHAIN (c) = list;
36117 return c;
36118 }
36119
36120 /* OpenMP 2.5:
36121 nowait */
36122
36123 static tree
36124 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
36125 tree list, location_t location)
36126 {
36127 tree c;
36128
36129 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
36130
36131 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
36132 OMP_CLAUSE_CHAIN (c) = list;
36133 return c;
36134 }
36135
36136 /* OpenMP 2.5:
36137 num_threads ( expression ) */
36138
36139 static tree
36140 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
36141 location_t location)
36142 {
36143 tree t, c;
36144
36145 matching_parens parens;
36146 if (!parens.require_open (parser))
36147 return list;
36148
36149 t = cp_parser_assignment_expression (parser);
36150
36151 if (t == error_mark_node
36152 || !parens.require_close (parser))
36153 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36154 /*or_comma=*/false,
36155 /*consume_paren=*/true);
36156
36157 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
36158 "num_threads", location);
36159
36160 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
36161 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
36162 OMP_CLAUSE_CHAIN (c) = list;
36163
36164 return c;
36165 }
36166
36167 /* OpenMP 4.5:
36168 num_tasks ( expression ) */
36169
36170 static tree
36171 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
36172 location_t location)
36173 {
36174 tree t, c;
36175
36176 matching_parens parens;
36177 if (!parens.require_open (parser))
36178 return list;
36179
36180 t = cp_parser_assignment_expression (parser);
36181
36182 if (t == error_mark_node
36183 || !parens.require_close (parser))
36184 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36185 /*or_comma=*/false,
36186 /*consume_paren=*/true);
36187
36188 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
36189 "num_tasks", location);
36190
36191 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
36192 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
36193 OMP_CLAUSE_CHAIN (c) = list;
36194
36195 return c;
36196 }
36197
36198 /* OpenMP 4.5:
36199 grainsize ( expression ) */
36200
36201 static tree
36202 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
36203 location_t location)
36204 {
36205 tree t, c;
36206
36207 matching_parens parens;
36208 if (!parens.require_open (parser))
36209 return list;
36210
36211 t = cp_parser_assignment_expression (parser);
36212
36213 if (t == error_mark_node
36214 || !parens.require_close (parser))
36215 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36216 /*or_comma=*/false,
36217 /*consume_paren=*/true);
36218
36219 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
36220 "grainsize", location);
36221
36222 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
36223 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
36224 OMP_CLAUSE_CHAIN (c) = list;
36225
36226 return c;
36227 }
36228
36229 /* OpenMP 4.5:
36230 priority ( expression ) */
36231
36232 static tree
36233 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
36234 location_t location)
36235 {
36236 tree t, c;
36237
36238 matching_parens parens;
36239 if (!parens.require_open (parser))
36240 return list;
36241
36242 t = cp_parser_assignment_expression (parser);
36243
36244 if (t == error_mark_node
36245 || !parens.require_close (parser))
36246 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36247 /*or_comma=*/false,
36248 /*consume_paren=*/true);
36249
36250 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
36251 "priority", location);
36252
36253 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
36254 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
36255 OMP_CLAUSE_CHAIN (c) = list;
36256
36257 return c;
36258 }
36259
36260 /* OpenMP 4.5:
36261 hint ( expression ) */
36262
36263 static tree
36264 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
36265 {
36266 tree t, c;
36267
36268 matching_parens parens;
36269 if (!parens.require_open (parser))
36270 return list;
36271
36272 t = cp_parser_assignment_expression (parser);
36273
36274 if (t != error_mark_node)
36275 {
36276 t = fold_non_dependent_expr (t);
36277 if (!value_dependent_expression_p (t)
36278 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
36279 || !tree_fits_shwi_p (t)
36280 || tree_int_cst_sgn (t) == -1))
36281 error_at (location, "expected constant integer expression with "
36282 "valid sync-hint value");
36283 }
36284 if (t == error_mark_node
36285 || !parens.require_close (parser))
36286 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36287 /*or_comma=*/false,
36288 /*consume_paren=*/true);
36289 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
36290
36291 c = build_omp_clause (location, OMP_CLAUSE_HINT);
36292 OMP_CLAUSE_HINT_EXPR (c) = t;
36293 OMP_CLAUSE_CHAIN (c) = list;
36294
36295 return c;
36296 }
36297
36298 /* OpenMP 4.5:
36299 defaultmap ( tofrom : scalar )
36300
36301 OpenMP 5.0:
36302 defaultmap ( implicit-behavior [ : variable-category ] ) */
36303
36304 static tree
36305 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
36306 location_t location)
36307 {
36308 tree c, id;
36309 const char *p;
36310 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
36311 enum omp_clause_defaultmap_kind category
36312 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
36313
36314 matching_parens parens;
36315 if (!parens.require_open (parser))
36316 return list;
36317
36318 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
36319 p = "default";
36320 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36321 {
36322 invalid_behavior:
36323 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
36324 "%<tofrom%>, %<firstprivate%>, %<none%> "
36325 "or %<default%>");
36326 goto out_err;
36327 }
36328 else
36329 {
36330 id = cp_lexer_peek_token (parser->lexer)->u.value;
36331 p = IDENTIFIER_POINTER (id);
36332 }
36333
36334 switch (p[0])
36335 {
36336 case 'a':
36337 if (strcmp ("alloc", p) == 0)
36338 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
36339 else
36340 goto invalid_behavior;
36341 break;
36342
36343 case 'd':
36344 if (strcmp ("default", p) == 0)
36345 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
36346 else
36347 goto invalid_behavior;
36348 break;
36349
36350 case 'f':
36351 if (strcmp ("firstprivate", p) == 0)
36352 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
36353 else if (strcmp ("from", p) == 0)
36354 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
36355 else
36356 goto invalid_behavior;
36357 break;
36358
36359 case 'n':
36360 if (strcmp ("none", p) == 0)
36361 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
36362 else
36363 goto invalid_behavior;
36364 break;
36365
36366 case 't':
36367 if (strcmp ("tofrom", p) == 0)
36368 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
36369 else if (strcmp ("to", p) == 0)
36370 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
36371 else
36372 goto invalid_behavior;
36373 break;
36374
36375 default:
36376 goto invalid_behavior;
36377 }
36378 cp_lexer_consume_token (parser->lexer);
36379
36380 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36381 {
36382 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36383 goto out_err;
36384
36385 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36386 {
36387 invalid_category:
36388 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
36389 "%<pointer%>");
36390 goto out_err;
36391 }
36392 id = cp_lexer_peek_token (parser->lexer)->u.value;
36393 p = IDENTIFIER_POINTER (id);
36394
36395 switch (p[0])
36396 {
36397 case 'a':
36398 if (strcmp ("aggregate", p) == 0)
36399 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
36400 else
36401 goto invalid_category;
36402 break;
36403
36404 case 'p':
36405 if (strcmp ("pointer", p) == 0)
36406 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
36407 else
36408 goto invalid_category;
36409 break;
36410
36411 case 's':
36412 if (strcmp ("scalar", p) == 0)
36413 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
36414 else
36415 goto invalid_category;
36416 break;
36417
36418 default:
36419 goto invalid_category;
36420 }
36421
36422 cp_lexer_consume_token (parser->lexer);
36423 }
36424 if (!parens.require_close (parser))
36425 goto out_err;
36426
36427 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
36428 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
36429 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
36430 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
36431 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
36432 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
36433 {
36434 enum omp_clause_defaultmap_kind cat = category;
36435 location_t loc = OMP_CLAUSE_LOCATION (c);
36436 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
36437 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
36438 p = NULL;
36439 switch (cat)
36440 {
36441 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
36442 p = NULL;
36443 break;
36444 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
36445 p = "aggregate";
36446 break;
36447 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
36448 p = "pointer";
36449 break;
36450 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
36451 p = "scalar";
36452 break;
36453 default:
36454 gcc_unreachable ();
36455 }
36456 if (p)
36457 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
36458 p);
36459 else
36460 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
36461 "category");
36462 break;
36463 }
36464
36465 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
36466 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
36467 OMP_CLAUSE_CHAIN (c) = list;
36468 return c;
36469
36470 out_err:
36471 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36472 /*or_comma=*/false,
36473 /*consume_paren=*/true);
36474 return list;
36475 }
36476
36477 /* OpenMP 5.0:
36478 order ( concurrent ) */
36479
36480 static tree
36481 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
36482 {
36483 tree c, id;
36484 const char *p;
36485
36486 matching_parens parens;
36487 if (!parens.require_open (parser))
36488 return list;
36489
36490 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36491 {
36492 cp_parser_error (parser, "expected %<concurrent%>");
36493 goto out_err;
36494 }
36495 else
36496 {
36497 id = cp_lexer_peek_token (parser->lexer)->u.value;
36498 p = IDENTIFIER_POINTER (id);
36499 }
36500 if (strcmp (p, "concurrent") != 0)
36501 {
36502 cp_parser_error (parser, "expected %<concurrent%>");
36503 goto out_err;
36504 }
36505 cp_lexer_consume_token (parser->lexer);
36506 if (!parens.require_close (parser))
36507 goto out_err;
36508
36509 /* check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location); */
36510 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
36511 OMP_CLAUSE_CHAIN (c) = list;
36512 return c;
36513
36514 out_err:
36515 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36516 /*or_comma=*/false,
36517 /*consume_paren=*/true);
36518 return list;
36519 }
36520
36521 /* OpenMP 5.0:
36522 bind ( teams | parallel | thread ) */
36523
36524 static tree
36525 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
36526 location_t location)
36527 {
36528 tree c;
36529 const char *p;
36530 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
36531
36532 matching_parens parens;
36533 if (!parens.require_open (parser))
36534 return list;
36535
36536 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36537 {
36538 invalid:
36539 cp_parser_error (parser,
36540 "expected %<teams%>, %<parallel%> or %<thread%>");
36541 goto out_err;
36542 }
36543 else
36544 {
36545 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36546 p = IDENTIFIER_POINTER (id);
36547 }
36548 if (strcmp (p, "teams") == 0)
36549 kind = OMP_CLAUSE_BIND_TEAMS;
36550 else if (strcmp (p, "parallel") == 0)
36551 kind = OMP_CLAUSE_BIND_PARALLEL;
36552 else if (strcmp (p, "thread") != 0)
36553 goto invalid;
36554 cp_lexer_consume_token (parser->lexer);
36555 if (!parens.require_close (parser))
36556 goto out_err;
36557
36558 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
36559 c = build_omp_clause (location, OMP_CLAUSE_BIND);
36560 OMP_CLAUSE_BIND_KIND (c) = kind;
36561 OMP_CLAUSE_CHAIN (c) = list;
36562 return c;
36563
36564 out_err:
36565 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36566 /*or_comma=*/false,
36567 /*consume_paren=*/true);
36568 return list;
36569 }
36570
36571 /* OpenMP 2.5:
36572 ordered
36573
36574 OpenMP 4.5:
36575 ordered ( constant-expression ) */
36576
36577 static tree
36578 cp_parser_omp_clause_ordered (cp_parser *parser,
36579 tree list, location_t location)
36580 {
36581 tree c, num = NULL_TREE;
36582 HOST_WIDE_INT n;
36583
36584 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
36585 "ordered", location);
36586
36587 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36588 {
36589 matching_parens parens;
36590 parens.consume_open (parser);
36591
36592 num = cp_parser_constant_expression (parser);
36593
36594 if (!parens.require_close (parser))
36595 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36596 /*or_comma=*/false,
36597 /*consume_paren=*/true);
36598
36599 if (num == error_mark_node)
36600 return list;
36601 num = fold_non_dependent_expr (num);
36602 if (!tree_fits_shwi_p (num)
36603 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
36604 || (n = tree_to_shwi (num)) <= 0
36605 || (int) n != n)
36606 {
36607 error_at (location,
36608 "ordered argument needs positive constant integer "
36609 "expression");
36610 return list;
36611 }
36612 }
36613
36614 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
36615 OMP_CLAUSE_ORDERED_EXPR (c) = num;
36616 OMP_CLAUSE_CHAIN (c) = list;
36617 return c;
36618 }
36619
36620 /* OpenMP 2.5:
36621 reduction ( reduction-operator : variable-list )
36622
36623 reduction-operator:
36624 One of: + * - & ^ | && ||
36625
36626 OpenMP 3.1:
36627
36628 reduction-operator:
36629 One of: + * - & ^ | && || min max
36630
36631 OpenMP 4.0:
36632
36633 reduction-operator:
36634 One of: + * - & ^ | && ||
36635 id-expression
36636
36637 OpenMP 5.0:
36638 reduction ( reduction-modifier, reduction-operator : variable-list )
36639 in_reduction ( reduction-operator : variable-list )
36640 task_reduction ( reduction-operator : variable-list ) */
36641
36642 static tree
36643 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
36644 bool is_omp, tree list)
36645 {
36646 enum tree_code code = ERROR_MARK;
36647 tree nlist, c, id = NULL_TREE;
36648 bool task = false;
36649 bool inscan = false;
36650
36651 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36652 return list;
36653
36654 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
36655 {
36656 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
36657 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
36658 {
36659 cp_lexer_consume_token (parser->lexer);
36660 cp_lexer_consume_token (parser->lexer);
36661 }
36662 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
36663 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
36664 {
36665 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36666 const char *p = IDENTIFIER_POINTER (id);
36667 if (strcmp (p, "task") == 0)
36668 task = true;
36669 else if (strcmp (p, "inscan") == 0)
36670 inscan = true;
36671 if (task || inscan)
36672 {
36673 cp_lexer_consume_token (parser->lexer);
36674 cp_lexer_consume_token (parser->lexer);
36675 }
36676 }
36677 }
36678
36679 switch (cp_lexer_peek_token (parser->lexer)->type)
36680 {
36681 case CPP_PLUS: code = PLUS_EXPR; break;
36682 case CPP_MULT: code = MULT_EXPR; break;
36683 case CPP_MINUS: code = MINUS_EXPR; break;
36684 case CPP_AND: code = BIT_AND_EXPR; break;
36685 case CPP_XOR: code = BIT_XOR_EXPR; break;
36686 case CPP_OR: code = BIT_IOR_EXPR; break;
36687 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
36688 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
36689 default: break;
36690 }
36691
36692 if (code != ERROR_MARK)
36693 cp_lexer_consume_token (parser->lexer);
36694 else
36695 {
36696 bool saved_colon_corrects_to_scope_p;
36697 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36698 parser->colon_corrects_to_scope_p = false;
36699 id = cp_parser_id_expression (parser, /*template_p=*/false,
36700 /*check_dependency_p=*/true,
36701 /*template_p=*/NULL,
36702 /*declarator_p=*/false,
36703 /*optional_p=*/false);
36704 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36705 if (identifier_p (id))
36706 {
36707 const char *p = IDENTIFIER_POINTER (id);
36708
36709 if (strcmp (p, "min") == 0)
36710 code = MIN_EXPR;
36711 else if (strcmp (p, "max") == 0)
36712 code = MAX_EXPR;
36713 else if (id == ovl_op_identifier (false, PLUS_EXPR))
36714 code = PLUS_EXPR;
36715 else if (id == ovl_op_identifier (false, MULT_EXPR))
36716 code = MULT_EXPR;
36717 else if (id == ovl_op_identifier (false, MINUS_EXPR))
36718 code = MINUS_EXPR;
36719 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
36720 code = BIT_AND_EXPR;
36721 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
36722 code = BIT_IOR_EXPR;
36723 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
36724 code = BIT_XOR_EXPR;
36725 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
36726 code = TRUTH_ANDIF_EXPR;
36727 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
36728 code = TRUTH_ORIF_EXPR;
36729 id = omp_reduction_id (code, id, NULL_TREE);
36730 tree scope = parser->scope;
36731 if (scope)
36732 id = build_qualified_name (NULL_TREE, scope, id, false);
36733 parser->scope = NULL_TREE;
36734 parser->qualifying_scope = NULL_TREE;
36735 parser->object_scope = NULL_TREE;
36736 }
36737 else
36738 {
36739 error ("invalid reduction-identifier");
36740 resync_fail:
36741 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36742 /*or_comma=*/false,
36743 /*consume_paren=*/true);
36744 return list;
36745 }
36746 }
36747
36748 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36749 goto resync_fail;
36750
36751 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
36752 NULL);
36753 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36754 {
36755 OMP_CLAUSE_REDUCTION_CODE (c) = code;
36756 if (task)
36757 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
36758 else if (inscan)
36759 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
36760 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
36761 }
36762
36763 return nlist;
36764 }
36765
36766 /* OpenMP 2.5:
36767 schedule ( schedule-kind )
36768 schedule ( schedule-kind , expression )
36769
36770 schedule-kind:
36771 static | dynamic | guided | runtime | auto
36772
36773 OpenMP 4.5:
36774 schedule ( schedule-modifier : schedule-kind )
36775 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
36776
36777 schedule-modifier:
36778 simd
36779 monotonic
36780 nonmonotonic */
36781
36782 static tree
36783 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
36784 {
36785 tree c, t;
36786 int modifiers = 0, nmodifiers = 0;
36787
36788 matching_parens parens;
36789 if (!parens.require_open (parser))
36790 return list;
36791
36792 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
36793
36794 location_t comma = UNKNOWN_LOCATION;
36795 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36796 {
36797 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36798 const char *p = IDENTIFIER_POINTER (id);
36799 if (strcmp ("simd", p) == 0)
36800 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
36801 else if (strcmp ("monotonic", p) == 0)
36802 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
36803 else if (strcmp ("nonmonotonic", p) == 0)
36804 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
36805 else
36806 break;
36807 comma = UNKNOWN_LOCATION;
36808 cp_lexer_consume_token (parser->lexer);
36809 if (nmodifiers++ == 0
36810 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36811 {
36812 comma = cp_lexer_peek_token (parser->lexer)->location;
36813 cp_lexer_consume_token (parser->lexer);
36814 }
36815 else
36816 {
36817 cp_parser_require (parser, CPP_COLON, RT_COLON);
36818 break;
36819 }
36820 }
36821 if (comma != UNKNOWN_LOCATION)
36822 error_at (comma, "expected %<:%>");
36823
36824 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36825 {
36826 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36827 const char *p = IDENTIFIER_POINTER (id);
36828
36829 switch (p[0])
36830 {
36831 case 'd':
36832 if (strcmp ("dynamic", p) != 0)
36833 goto invalid_kind;
36834 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
36835 break;
36836
36837 case 'g':
36838 if (strcmp ("guided", p) != 0)
36839 goto invalid_kind;
36840 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
36841 break;
36842
36843 case 'r':
36844 if (strcmp ("runtime", p) != 0)
36845 goto invalid_kind;
36846 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
36847 break;
36848
36849 default:
36850 goto invalid_kind;
36851 }
36852 }
36853 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
36854 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
36855 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
36856 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
36857 else
36858 goto invalid_kind;
36859 cp_lexer_consume_token (parser->lexer);
36860
36861 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
36862 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
36863 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
36864 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
36865 {
36866 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
36867 "specified");
36868 modifiers = 0;
36869 }
36870
36871 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36872 {
36873 cp_token *token;
36874 cp_lexer_consume_token (parser->lexer);
36875
36876 token = cp_lexer_peek_token (parser->lexer);
36877 t = cp_parser_assignment_expression (parser);
36878
36879 if (t == error_mark_node)
36880 goto resync_fail;
36881 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
36882 error_at (token->location, "schedule %<runtime%> does not take "
36883 "a %<chunk_size%> parameter");
36884 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
36885 error_at (token->location, "schedule %<auto%> does not take "
36886 "a %<chunk_size%> parameter");
36887 else
36888 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
36889
36890 if (!parens.require_close (parser))
36891 goto resync_fail;
36892 }
36893 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
36894 goto resync_fail;
36895
36896 OMP_CLAUSE_SCHEDULE_KIND (c)
36897 = (enum omp_clause_schedule_kind)
36898 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
36899
36900 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
36901 OMP_CLAUSE_CHAIN (c) = list;
36902 return c;
36903
36904 invalid_kind:
36905 cp_parser_error (parser, "invalid schedule kind");
36906 resync_fail:
36907 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36908 /*or_comma=*/false,
36909 /*consume_paren=*/true);
36910 return list;
36911 }
36912
36913 /* OpenMP 3.0:
36914 untied */
36915
36916 static tree
36917 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
36918 tree list, location_t location)
36919 {
36920 tree c;
36921
36922 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
36923
36924 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
36925 OMP_CLAUSE_CHAIN (c) = list;
36926 return c;
36927 }
36928
36929 /* OpenMP 4.0:
36930 inbranch
36931 notinbranch */
36932
36933 static tree
36934 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
36935 tree list, location_t location)
36936 {
36937 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
36938 tree c = build_omp_clause (location, code);
36939 OMP_CLAUSE_CHAIN (c) = list;
36940 return c;
36941 }
36942
36943 /* OpenMP 4.0:
36944 parallel
36945 for
36946 sections
36947 taskgroup */
36948
36949 static tree
36950 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
36951 enum omp_clause_code code,
36952 tree list, location_t location)
36953 {
36954 tree c = build_omp_clause (location, code);
36955 OMP_CLAUSE_CHAIN (c) = list;
36956 return c;
36957 }
36958
36959 /* OpenMP 4.5:
36960 nogroup */
36961
36962 static tree
36963 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
36964 tree list, location_t location)
36965 {
36966 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
36967 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
36968 OMP_CLAUSE_CHAIN (c) = list;
36969 return c;
36970 }
36971
36972 /* OpenMP 4.5:
36973 simd
36974 threads */
36975
36976 static tree
36977 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
36978 enum omp_clause_code code,
36979 tree list, location_t location)
36980 {
36981 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
36982 tree c = build_omp_clause (location, code);
36983 OMP_CLAUSE_CHAIN (c) = list;
36984 return c;
36985 }
36986
36987 /* OpenMP 4.0:
36988 num_teams ( expression ) */
36989
36990 static tree
36991 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
36992 location_t location)
36993 {
36994 tree t, c;
36995
36996 matching_parens parens;
36997 if (!parens.require_open (parser))
36998 return list;
36999
37000 t = cp_parser_assignment_expression (parser);
37001
37002 if (t == error_mark_node
37003 || !parens.require_close (parser))
37004 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37005 /*or_comma=*/false,
37006 /*consume_paren=*/true);
37007
37008 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
37009 "num_teams", location);
37010
37011 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
37012 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
37013 OMP_CLAUSE_CHAIN (c) = list;
37014
37015 return c;
37016 }
37017
37018 /* OpenMP 4.0:
37019 thread_limit ( expression ) */
37020
37021 static tree
37022 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
37023 location_t location)
37024 {
37025 tree t, c;
37026
37027 matching_parens parens;
37028 if (!parens.require_open (parser))
37029 return list;
37030
37031 t = cp_parser_assignment_expression (parser);
37032
37033 if (t == error_mark_node
37034 || !parens.require_close (parser))
37035 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37036 /*or_comma=*/false,
37037 /*consume_paren=*/true);
37038
37039 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
37040 "thread_limit", location);
37041
37042 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
37043 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
37044 OMP_CLAUSE_CHAIN (c) = list;
37045
37046 return c;
37047 }
37048
37049 /* OpenMP 4.0:
37050 aligned ( variable-list )
37051 aligned ( variable-list : constant-expression ) */
37052
37053 static tree
37054 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
37055 {
37056 tree nlist, c, alignment = NULL_TREE;
37057 bool colon;
37058
37059 matching_parens parens;
37060 if (!parens.require_open (parser))
37061 return list;
37062
37063 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
37064 &colon);
37065
37066 if (colon)
37067 {
37068 alignment = cp_parser_constant_expression (parser);
37069
37070 if (!parens.require_close (parser))
37071 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37072 /*or_comma=*/false,
37073 /*consume_paren=*/true);
37074
37075 if (alignment == error_mark_node)
37076 alignment = NULL_TREE;
37077 }
37078
37079 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
37080 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
37081
37082 return nlist;
37083 }
37084
37085 /* OpenMP 5.0:
37086 allocate ( variable-list )
37087 allocate ( expression : variable-list ) */
37088
37089 static tree
37090 cp_parser_omp_clause_allocate (cp_parser *parser, tree list)
37091 {
37092 tree nlist, c, allocator = NULL_TREE;
37093 bool colon;
37094
37095 matching_parens parens;
37096 if (!parens.require_open (parser))
37097 return list;
37098
37099 cp_parser_parse_tentatively (parser);
37100 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37101 parser->colon_corrects_to_scope_p = false;
37102 allocator = cp_parser_assignment_expression (parser);
37103 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37104 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
37105 {
37106 cp_parser_parse_definitely (parser);
37107 cp_lexer_consume_token (parser->lexer);
37108 if (allocator == error_mark_node)
37109 allocator = NULL_TREE;
37110 }
37111 else
37112 {
37113 cp_parser_abort_tentative_parse (parser);
37114 allocator = NULL_TREE;
37115 }
37116
37117 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALLOCATE, list,
37118 &colon);
37119
37120 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
37121 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
37122
37123 return nlist;
37124 }
37125
37126 /* OpenMP 2.5:
37127 lastprivate ( variable-list )
37128
37129 OpenMP 5.0:
37130 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
37131
37132 static tree
37133 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
37134 {
37135 bool conditional = false;
37136
37137 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37138 return list;
37139
37140 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37141 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
37142 {
37143 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37144 const char *p = IDENTIFIER_POINTER (id);
37145
37146 if (strcmp ("conditional", p) == 0)
37147 {
37148 conditional = true;
37149 cp_lexer_consume_token (parser->lexer);
37150 cp_lexer_consume_token (parser->lexer);
37151 }
37152 }
37153
37154 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
37155 list, NULL);
37156
37157 if (conditional)
37158 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
37159 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
37160 return nlist;
37161 }
37162
37163 /* OpenMP 4.0:
37164 linear ( variable-list )
37165 linear ( variable-list : expression )
37166
37167 OpenMP 4.5:
37168 linear ( modifier ( variable-list ) )
37169 linear ( modifier ( variable-list ) : expression ) */
37170
37171 static tree
37172 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
37173 bool declare_simd)
37174 {
37175 tree nlist, c, step = integer_one_node;
37176 bool colon;
37177 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
37178
37179 matching_parens parens;
37180 if (!parens.require_open (parser))
37181 return list;
37182
37183 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37184 {
37185 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37186 const char *p = IDENTIFIER_POINTER (id);
37187
37188 if (strcmp ("ref", p) == 0)
37189 kind = OMP_CLAUSE_LINEAR_REF;
37190 else if (strcmp ("val", p) == 0)
37191 kind = OMP_CLAUSE_LINEAR_VAL;
37192 else if (strcmp ("uval", p) == 0)
37193 kind = OMP_CLAUSE_LINEAR_UVAL;
37194 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
37195 cp_lexer_consume_token (parser->lexer);
37196 else
37197 kind = OMP_CLAUSE_LINEAR_DEFAULT;
37198 }
37199
37200 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
37201 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
37202 &colon);
37203 else
37204 {
37205 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
37206 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
37207 if (colon)
37208 cp_parser_require (parser, CPP_COLON, RT_COLON);
37209 else if (!parens.require_close (parser))
37210 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37211 /*or_comma=*/false,
37212 /*consume_paren=*/true);
37213 }
37214
37215 if (colon)
37216 {
37217 step = NULL_TREE;
37218 if (declare_simd
37219 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37220 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
37221 {
37222 cp_token *token = cp_lexer_peek_token (parser->lexer);
37223 cp_parser_parse_tentatively (parser);
37224 step = cp_parser_id_expression (parser, /*template_p=*/false,
37225 /*check_dependency_p=*/true,
37226 /*template_p=*/NULL,
37227 /*declarator_p=*/false,
37228 /*optional_p=*/false);
37229 if (step != error_mark_node)
37230 step = cp_parser_lookup_name_simple (parser, step, token->location);
37231 if (step == error_mark_node)
37232 {
37233 step = NULL_TREE;
37234 cp_parser_abort_tentative_parse (parser);
37235 }
37236 else if (!cp_parser_parse_definitely (parser))
37237 step = NULL_TREE;
37238 }
37239 if (!step)
37240 step = cp_parser_assignment_expression (parser);
37241
37242 if (!parens.require_close (parser))
37243 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37244 /*or_comma=*/false,
37245 /*consume_paren=*/true);
37246
37247 if (step == error_mark_node)
37248 return list;
37249 }
37250
37251 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
37252 {
37253 OMP_CLAUSE_LINEAR_STEP (c) = step;
37254 OMP_CLAUSE_LINEAR_KIND (c) = kind;
37255 }
37256
37257 return nlist;
37258 }
37259
37260 /* OpenMP 4.0:
37261 safelen ( constant-expression ) */
37262
37263 static tree
37264 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
37265 location_t location)
37266 {
37267 tree t, c;
37268
37269 matching_parens parens;
37270 if (!parens.require_open (parser))
37271 return list;
37272
37273 t = cp_parser_constant_expression (parser);
37274
37275 if (t == error_mark_node
37276 || !parens.require_close (parser))
37277 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37278 /*or_comma=*/false,
37279 /*consume_paren=*/true);
37280
37281 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
37282
37283 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
37284 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
37285 OMP_CLAUSE_CHAIN (c) = list;
37286
37287 return c;
37288 }
37289
37290 /* OpenMP 4.0:
37291 simdlen ( constant-expression ) */
37292
37293 static tree
37294 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
37295 location_t location)
37296 {
37297 tree t, c;
37298
37299 matching_parens parens;
37300 if (!parens.require_open (parser))
37301 return list;
37302
37303 t = cp_parser_constant_expression (parser);
37304
37305 if (t == error_mark_node
37306 || !parens.require_close (parser))
37307 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37308 /*or_comma=*/false,
37309 /*consume_paren=*/true);
37310
37311 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
37312
37313 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
37314 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
37315 OMP_CLAUSE_CHAIN (c) = list;
37316
37317 return c;
37318 }
37319
37320 /* OpenMP 4.5:
37321 vec:
37322 identifier [+/- integer]
37323 vec , identifier [+/- integer]
37324 */
37325
37326 static tree
37327 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
37328 tree list)
37329 {
37330 tree vec = NULL;
37331
37332 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37333 {
37334 cp_parser_error (parser, "expected identifier");
37335 return list;
37336 }
37337
37338 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37339 {
37340 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
37341 tree t, identifier = cp_parser_identifier (parser);
37342 tree addend = NULL;
37343
37344 if (identifier == error_mark_node)
37345 t = error_mark_node;
37346 else
37347 {
37348 t = cp_parser_lookup_name_simple
37349 (parser, identifier,
37350 cp_lexer_peek_token (parser->lexer)->location);
37351 if (t == error_mark_node)
37352 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
37353 id_loc);
37354 }
37355
37356 bool neg = false;
37357 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
37358 neg = true;
37359 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
37360 {
37361 addend = integer_zero_node;
37362 goto add_to_vector;
37363 }
37364 cp_lexer_consume_token (parser->lexer);
37365
37366 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
37367 {
37368 cp_parser_error (parser, "expected integer");
37369 return list;
37370 }
37371
37372 addend = cp_lexer_peek_token (parser->lexer)->u.value;
37373 if (TREE_CODE (addend) != INTEGER_CST)
37374 {
37375 cp_parser_error (parser, "expected integer");
37376 return list;
37377 }
37378 cp_lexer_consume_token (parser->lexer);
37379
37380 add_to_vector:
37381 if (t != error_mark_node)
37382 {
37383 vec = tree_cons (addend, t, vec);
37384 if (neg)
37385 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
37386 }
37387
37388 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
37389 break;
37390
37391 cp_lexer_consume_token (parser->lexer);
37392 }
37393
37394 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
37395 {
37396 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
37397 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
37398 OMP_CLAUSE_DECL (u) = nreverse (vec);
37399 OMP_CLAUSE_CHAIN (u) = list;
37400 return u;
37401 }
37402 return list;
37403 }
37404
37405 /* OpenMP 5.0:
37406 detach ( event-handle ) */
37407
37408 static tree
37409 cp_parser_omp_clause_detach (cp_parser *parser, tree list)
37410 {
37411 matching_parens parens;
37412
37413 if (!parens.require_open (parser))
37414 return list;
37415
37416 cp_token *token;
37417 tree name, decl;
37418
37419 token = cp_lexer_peek_token (parser->lexer);
37420 name = cp_parser_id_expression (parser, /*template_p=*/false,
37421 /*check_dependency_p=*/true,
37422 /*template_p=*/NULL,
37423 /*declarator_p=*/false,
37424 /*optional_p=*/false);
37425 if (name == error_mark_node)
37426 decl = error_mark_node;
37427 else
37428 {
37429 if (identifier_p (name))
37430 decl = cp_parser_lookup_name_simple (parser, name, token->location);
37431 else
37432 decl = name;
37433 if (decl == error_mark_node)
37434 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
37435 token->location);
37436 }
37437
37438 if (decl == error_mark_node
37439 || !parens.require_close (parser))
37440 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37441 /*or_comma=*/false,
37442 /*consume_paren=*/true);
37443
37444 tree u = build_omp_clause (token->location, OMP_CLAUSE_DETACH);
37445 OMP_CLAUSE_DECL (u) = decl;
37446 OMP_CLAUSE_CHAIN (u) = list;
37447
37448 return u;
37449 }
37450
37451 /* OpenMP 5.0:
37452 iterators ( iterators-definition )
37453
37454 iterators-definition:
37455 iterator-specifier
37456 iterator-specifier , iterators-definition
37457
37458 iterator-specifier:
37459 identifier = range-specification
37460 iterator-type identifier = range-specification
37461
37462 range-specification:
37463 begin : end
37464 begin : end : step */
37465
37466 static tree
37467 cp_parser_omp_iterators (cp_parser *parser)
37468 {
37469 tree ret = NULL_TREE, *last = &ret;
37470 cp_lexer_consume_token (parser->lexer);
37471
37472 matching_parens parens;
37473 if (!parens.require_open (parser))
37474 return error_mark_node;
37475
37476 bool saved_colon_corrects_to_scope_p
37477 = parser->colon_corrects_to_scope_p;
37478 bool saved_colon_doesnt_start_class_def_p
37479 = parser->colon_doesnt_start_class_def_p;
37480
37481 do
37482 {
37483 tree iter_type;
37484 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37485 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
37486 iter_type = integer_type_node;
37487 else
37488 {
37489 const char *saved_message
37490 = parser->type_definition_forbidden_message;
37491 parser->type_definition_forbidden_message
37492 = G_("types may not be defined in iterator type");
37493
37494 iter_type = cp_parser_type_id (parser);
37495
37496 parser->type_definition_forbidden_message = saved_message;
37497 }
37498
37499 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37500 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37501 {
37502 cp_parser_error (parser, "expected identifier");
37503 break;
37504 }
37505
37506 tree id = cp_parser_identifier (parser);
37507 if (id == error_mark_node)
37508 break;
37509
37510 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
37511 break;
37512
37513 parser->colon_corrects_to_scope_p = false;
37514 parser->colon_doesnt_start_class_def_p = true;
37515 tree begin = cp_parser_assignment_expression (parser);
37516
37517 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37518 break;
37519
37520 tree end = cp_parser_assignment_expression (parser);
37521
37522 tree step = integer_one_node;
37523 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
37524 {
37525 cp_lexer_consume_token (parser->lexer);
37526 step = cp_parser_assignment_expression (parser);
37527 }
37528
37529 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
37530 DECL_ARTIFICIAL (iter_var) = 1;
37531 DECL_CONTEXT (iter_var) = current_function_decl;
37532 pushdecl (iter_var);
37533
37534 *last = make_tree_vec (6);
37535 TREE_VEC_ELT (*last, 0) = iter_var;
37536 TREE_VEC_ELT (*last, 1) = begin;
37537 TREE_VEC_ELT (*last, 2) = end;
37538 TREE_VEC_ELT (*last, 3) = step;
37539 last = &TREE_CHAIN (*last);
37540
37541 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37542 {
37543 cp_lexer_consume_token (parser->lexer);
37544 continue;
37545 }
37546 break;
37547 }
37548 while (1);
37549
37550 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37551 parser->colon_doesnt_start_class_def_p
37552 = saved_colon_doesnt_start_class_def_p;
37553
37554 if (!parens.require_close (parser))
37555 cp_parser_skip_to_closing_parenthesis (parser,
37556 /*recovering=*/true,
37557 /*or_comma=*/false,
37558 /*consume_paren=*/true);
37559
37560 return ret ? ret : error_mark_node;
37561 }
37562
37563 /* OpenMP 4.0:
37564 depend ( depend-kind : variable-list )
37565
37566 depend-kind:
37567 in | out | inout
37568
37569 OpenMP 4.5:
37570 depend ( source )
37571
37572 depend ( sink : vec )
37573
37574 OpenMP 5.0:
37575 depend ( depend-modifier , depend-kind: variable-list )
37576
37577 depend-kind:
37578 in | out | inout | mutexinoutset | depobj
37579
37580 depend-modifier:
37581 iterator ( iterators-definition ) */
37582
37583 static tree
37584 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
37585 {
37586 tree nlist, c, iterators = NULL_TREE;
37587 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
37588
37589 matching_parens parens;
37590 if (!parens.require_open (parser))
37591 return list;
37592
37593 do
37594 {
37595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37596 goto invalid_kind;
37597
37598 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37599 const char *p = IDENTIFIER_POINTER (id);
37600
37601 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
37602 {
37603 begin_scope (sk_omp, NULL);
37604 iterators = cp_parser_omp_iterators (parser);
37605 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
37606 continue;
37607 }
37608 if (strcmp ("in", p) == 0)
37609 kind = OMP_CLAUSE_DEPEND_IN;
37610 else if (strcmp ("inout", p) == 0)
37611 kind = OMP_CLAUSE_DEPEND_INOUT;
37612 else if (strcmp ("mutexinoutset", p) == 0)
37613 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
37614 else if (strcmp ("out", p) == 0)
37615 kind = OMP_CLAUSE_DEPEND_OUT;
37616 else if (strcmp ("depobj", p) == 0)
37617 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
37618 else if (strcmp ("sink", p) == 0)
37619 kind = OMP_CLAUSE_DEPEND_SINK;
37620 else if (strcmp ("source", p) == 0)
37621 kind = OMP_CLAUSE_DEPEND_SOURCE;
37622 else
37623 goto invalid_kind;
37624 break;
37625 }
37626 while (1);
37627
37628 cp_lexer_consume_token (parser->lexer);
37629
37630 if (iterators
37631 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
37632 {
37633 poplevel (0, 1, 0);
37634 error_at (loc, "%<iterator%> modifier incompatible with %qs",
37635 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
37636 iterators = NULL_TREE;
37637 }
37638
37639 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
37640 {
37641 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
37642 OMP_CLAUSE_DEPEND_KIND (c) = kind;
37643 OMP_CLAUSE_DECL (c) = NULL_TREE;
37644 OMP_CLAUSE_CHAIN (c) = list;
37645 if (!parens.require_close (parser))
37646 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37647 /*or_comma=*/false,
37648 /*consume_paren=*/true);
37649 return c;
37650 }
37651
37652 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37653 goto resync_fail;
37654
37655 if (kind == OMP_CLAUSE_DEPEND_SINK)
37656 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
37657 else
37658 {
37659 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
37660 list, NULL);
37661
37662 if (iterators)
37663 {
37664 tree block = poplevel (1, 1, 0);
37665 if (iterators == error_mark_node)
37666 iterators = NULL_TREE;
37667 else
37668 TREE_VEC_ELT (iterators, 5) = block;
37669 }
37670
37671 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
37672 {
37673 OMP_CLAUSE_DEPEND_KIND (c) = kind;
37674 if (iterators)
37675 OMP_CLAUSE_DECL (c)
37676 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
37677 }
37678 }
37679 return nlist;
37680
37681 invalid_kind:
37682 cp_parser_error (parser, "invalid depend kind");
37683 resync_fail:
37684 if (iterators)
37685 poplevel (0, 1, 0);
37686 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37687 /*or_comma=*/false,
37688 /*consume_paren=*/true);
37689 return list;
37690 }
37691
37692 /* OpenMP 4.0:
37693 map ( map-kind : variable-list )
37694 map ( variable-list )
37695
37696 map-kind:
37697 alloc | to | from | tofrom
37698
37699 OpenMP 4.5:
37700 map-kind:
37701 alloc | to | from | tofrom | release | delete
37702
37703 map ( always [,] map-kind: variable-list ) */
37704
37705 static tree
37706 cp_parser_omp_clause_map (cp_parser *parser, tree list)
37707 {
37708 tree nlist, c;
37709 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
37710 bool always = false;
37711
37712 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37713 return list;
37714
37715 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37716 {
37717 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37718 const char *p = IDENTIFIER_POINTER (id);
37719
37720 if (strcmp ("always", p) == 0)
37721 {
37722 int nth = 2;
37723 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
37724 nth++;
37725 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
37726 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
37727 == RID_DELETE))
37728 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
37729 == CPP_COLON))
37730 {
37731 always = true;
37732 cp_lexer_consume_token (parser->lexer);
37733 if (nth == 3)
37734 cp_lexer_consume_token (parser->lexer);
37735 }
37736 }
37737 }
37738
37739 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37740 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
37741 {
37742 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37743 const char *p = IDENTIFIER_POINTER (id);
37744
37745 if (strcmp ("alloc", p) == 0)
37746 kind = GOMP_MAP_ALLOC;
37747 else if (strcmp ("to", p) == 0)
37748 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
37749 else if (strcmp ("from", p) == 0)
37750 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
37751 else if (strcmp ("tofrom", p) == 0)
37752 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
37753 else if (strcmp ("release", p) == 0)
37754 kind = GOMP_MAP_RELEASE;
37755 else
37756 {
37757 cp_parser_error (parser, "invalid map kind");
37758 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37759 /*or_comma=*/false,
37760 /*consume_paren=*/true);
37761 return list;
37762 }
37763 cp_lexer_consume_token (parser->lexer);
37764 cp_lexer_consume_token (parser->lexer);
37765 }
37766 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
37767 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
37768 {
37769 kind = GOMP_MAP_DELETE;
37770 cp_lexer_consume_token (parser->lexer);
37771 cp_lexer_consume_token (parser->lexer);
37772 }
37773
37774 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
37775 NULL);
37776
37777 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
37778 OMP_CLAUSE_SET_MAP_KIND (c, kind);
37779
37780 return nlist;
37781 }
37782
37783 /* OpenMP 4.0:
37784 device ( expression ) */
37785
37786 static tree
37787 cp_parser_omp_clause_device (cp_parser *parser, tree list,
37788 location_t location)
37789 {
37790 tree t, c;
37791
37792 matching_parens parens;
37793 if (!parens.require_open (parser))
37794 return list;
37795
37796 t = cp_parser_assignment_expression (parser);
37797
37798 if (t == error_mark_node
37799 || !parens.require_close (parser))
37800 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37801 /*or_comma=*/false,
37802 /*consume_paren=*/true);
37803
37804 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
37805 "device", location);
37806
37807 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
37808 OMP_CLAUSE_DEVICE_ID (c) = t;
37809 OMP_CLAUSE_CHAIN (c) = list;
37810
37811 return c;
37812 }
37813
37814 /* OpenMP 4.0:
37815 dist_schedule ( static )
37816 dist_schedule ( static , expression ) */
37817
37818 static tree
37819 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
37820 location_t location)
37821 {
37822 tree c, t;
37823
37824 matching_parens parens;
37825 if (!parens.require_open (parser))
37826 return list;
37827
37828 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
37829
37830 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
37831 goto invalid_kind;
37832 cp_lexer_consume_token (parser->lexer);
37833
37834 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37835 {
37836 cp_lexer_consume_token (parser->lexer);
37837
37838 t = cp_parser_assignment_expression (parser);
37839
37840 if (t == error_mark_node)
37841 goto resync_fail;
37842 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
37843
37844 if (!parens.require_close (parser))
37845 goto resync_fail;
37846 }
37847 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
37848 goto resync_fail;
37849
37850 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
37851 "dist_schedule", location); */
37852 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
37853 warning_at (location, 0, "too many %qs clauses", "dist_schedule");
37854 OMP_CLAUSE_CHAIN (c) = list;
37855 return c;
37856
37857 invalid_kind:
37858 cp_parser_error (parser, "invalid dist_schedule kind");
37859 resync_fail:
37860 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37861 /*or_comma=*/false,
37862 /*consume_paren=*/true);
37863 return list;
37864 }
37865
37866 /* OpenMP 4.0:
37867 proc_bind ( proc-bind-kind )
37868
37869 proc-bind-kind:
37870 master | close | spread */
37871
37872 static tree
37873 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
37874 location_t location)
37875 {
37876 tree c;
37877 enum omp_clause_proc_bind_kind kind;
37878
37879 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37880 return list;
37881
37882 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37883 {
37884 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37885 const char *p = IDENTIFIER_POINTER (id);
37886
37887 if (strcmp ("master", p) == 0)
37888 kind = OMP_CLAUSE_PROC_BIND_MASTER;
37889 else if (strcmp ("close", p) == 0)
37890 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
37891 else if (strcmp ("spread", p) == 0)
37892 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
37893 else
37894 goto invalid_kind;
37895 }
37896 else
37897 goto invalid_kind;
37898
37899 cp_lexer_consume_token (parser->lexer);
37900 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
37901 goto resync_fail;
37902
37903 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
37904 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
37905 location);
37906 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
37907 OMP_CLAUSE_CHAIN (c) = list;
37908 return c;
37909
37910 invalid_kind:
37911 cp_parser_error (parser, "invalid depend kind");
37912 resync_fail:
37913 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37914 /*or_comma=*/false,
37915 /*consume_paren=*/true);
37916 return list;
37917 }
37918
37919 /* OpenMP 5.0:
37920 device_type ( host | nohost | any ) */
37921
37922 static tree
37923 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
37924 location_t location)
37925 {
37926 tree c;
37927 enum omp_clause_device_type_kind kind;
37928
37929 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37930 return list;
37931
37932 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37933 {
37934 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37935 const char *p = IDENTIFIER_POINTER (id);
37936
37937 if (strcmp ("host", p) == 0)
37938 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
37939 else if (strcmp ("nohost", p) == 0)
37940 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
37941 else if (strcmp ("any", p) == 0)
37942 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
37943 else
37944 goto invalid_kind;
37945 }
37946 else
37947 goto invalid_kind;
37948
37949 cp_lexer_consume_token (parser->lexer);
37950 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
37951 goto resync_fail;
37952
37953 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
37954 /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
37955 location); */
37956 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
37957 OMP_CLAUSE_CHAIN (c) = list;
37958 return c;
37959
37960 invalid_kind:
37961 cp_parser_error (parser, "invalid depend kind");
37962 resync_fail:
37963 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37964 /*or_comma=*/false,
37965 /*consume_paren=*/true);
37966 return list;
37967 }
37968
37969 /* OpenACC:
37970 async [( int-expr )] */
37971
37972 static tree
37973 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
37974 {
37975 tree c, t;
37976 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37977
37978 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
37979
37980 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37981 {
37982 matching_parens parens;
37983 parens.consume_open (parser);
37984
37985 t = cp_parser_expression (parser);
37986 if (t == error_mark_node
37987 || !parens.require_close (parser))
37988 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37989 /*or_comma=*/false,
37990 /*consume_paren=*/true);
37991 }
37992
37993 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
37994
37995 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
37996 OMP_CLAUSE_ASYNC_EXPR (c) = t;
37997 OMP_CLAUSE_CHAIN (c) = list;
37998 list = c;
37999
38000 return list;
38001 }
38002
38003 /* Parse all OpenACC clauses. The set clauses allowed by the directive
38004 is a bitmask in MASK. Return the list of clauses found. */
38005
38006 static tree
38007 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
38008 const char *where, cp_token *pragma_tok,
38009 bool finish_p = true)
38010 {
38011 tree clauses = NULL;
38012 bool first = true;
38013
38014 /* Don't create location wrapper nodes within OpenACC clauses. */
38015 auto_suppress_location_wrappers sentinel;
38016
38017 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38018 {
38019 location_t here;
38020 pragma_omp_clause c_kind;
38021 omp_clause_code code;
38022 const char *c_name;
38023 tree prev = clauses;
38024
38025 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38026 cp_lexer_consume_token (parser->lexer);
38027
38028 here = cp_lexer_peek_token (parser->lexer)->location;
38029 c_kind = cp_parser_omp_clause_name (parser);
38030
38031 switch (c_kind)
38032 {
38033 case PRAGMA_OACC_CLAUSE_ASYNC:
38034 clauses = cp_parser_oacc_clause_async (parser, clauses);
38035 c_name = "async";
38036 break;
38037 case PRAGMA_OACC_CLAUSE_AUTO:
38038 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
38039 clauses);
38040 c_name = "auto";
38041 break;
38042 case PRAGMA_OACC_CLAUSE_ATTACH:
38043 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38044 c_name = "attach";
38045 break;
38046 case PRAGMA_OACC_CLAUSE_COLLAPSE:
38047 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
38048 c_name = "collapse";
38049 break;
38050 case PRAGMA_OACC_CLAUSE_COPY:
38051 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38052 c_name = "copy";
38053 break;
38054 case PRAGMA_OACC_CLAUSE_COPYIN:
38055 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38056 c_name = "copyin";
38057 break;
38058 case PRAGMA_OACC_CLAUSE_COPYOUT:
38059 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38060 c_name = "copyout";
38061 break;
38062 case PRAGMA_OACC_CLAUSE_CREATE:
38063 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38064 c_name = "create";
38065 break;
38066 case PRAGMA_OACC_CLAUSE_DELETE:
38067 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38068 c_name = "delete";
38069 break;
38070 case PRAGMA_OMP_CLAUSE_DEFAULT:
38071 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
38072 c_name = "default";
38073 break;
38074 case PRAGMA_OACC_CLAUSE_DETACH:
38075 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38076 c_name = "detach";
38077 break;
38078 case PRAGMA_OACC_CLAUSE_DEVICE:
38079 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38080 c_name = "device";
38081 break;
38082 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
38083 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
38084 c_name = "deviceptr";
38085 break;
38086 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
38087 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38088 c_name = "device_resident";
38089 break;
38090 case PRAGMA_OACC_CLAUSE_FINALIZE:
38091 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
38092 clauses);
38093 c_name = "finalize";
38094 break;
38095 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
38096 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38097 clauses);
38098 c_name = "firstprivate";
38099 break;
38100 case PRAGMA_OACC_CLAUSE_GANG:
38101 c_name = "gang";
38102 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
38103 c_name, clauses);
38104 break;
38105 case PRAGMA_OACC_CLAUSE_HOST:
38106 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38107 c_name = "host";
38108 break;
38109 case PRAGMA_OACC_CLAUSE_IF:
38110 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
38111 c_name = "if";
38112 break;
38113 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
38114 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
38115 clauses);
38116 c_name = "if_present";
38117 break;
38118 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
38119 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
38120 clauses);
38121 c_name = "independent";
38122 break;
38123 case PRAGMA_OACC_CLAUSE_LINK:
38124 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38125 c_name = "link";
38126 break;
38127 case PRAGMA_OACC_CLAUSE_NO_CREATE:
38128 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38129 c_name = "no_create";
38130 break;
38131 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
38132 code = OMP_CLAUSE_NUM_GANGS;
38133 c_name = "num_gangs";
38134 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
38135 clauses);
38136 break;
38137 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
38138 c_name = "num_workers";
38139 code = OMP_CLAUSE_NUM_WORKERS;
38140 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
38141 clauses);
38142 break;
38143 case PRAGMA_OACC_CLAUSE_PRESENT:
38144 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38145 c_name = "present";
38146 break;
38147 case PRAGMA_OACC_CLAUSE_PRIVATE:
38148 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
38149 clauses);
38150 c_name = "private";
38151 break;
38152 case PRAGMA_OACC_CLAUSE_REDUCTION:
38153 clauses
38154 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
38155 false, clauses);
38156 c_name = "reduction";
38157 break;
38158 case PRAGMA_OACC_CLAUSE_SEQ:
38159 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
38160 clauses);
38161 c_name = "seq";
38162 break;
38163 case PRAGMA_OACC_CLAUSE_TILE:
38164 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
38165 c_name = "tile";
38166 break;
38167 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
38168 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
38169 clauses);
38170 c_name = "use_device";
38171 break;
38172 case PRAGMA_OACC_CLAUSE_VECTOR:
38173 c_name = "vector";
38174 clauses = cp_parser_oacc_shape_clause (parser, here,
38175 OMP_CLAUSE_VECTOR,
38176 c_name, clauses);
38177 break;
38178 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
38179 c_name = "vector_length";
38180 code = OMP_CLAUSE_VECTOR_LENGTH;
38181 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
38182 clauses);
38183 break;
38184 case PRAGMA_OACC_CLAUSE_WAIT:
38185 clauses = cp_parser_oacc_clause_wait (parser, clauses);
38186 c_name = "wait";
38187 break;
38188 case PRAGMA_OACC_CLAUSE_WORKER:
38189 c_name = "worker";
38190 clauses = cp_parser_oacc_shape_clause (parser, here,
38191 OMP_CLAUSE_WORKER,
38192 c_name, clauses);
38193 break;
38194 default:
38195 cp_parser_error (parser, "expected %<#pragma acc%> clause");
38196 goto saw_error;
38197 }
38198
38199 first = false;
38200
38201 if (((mask >> c_kind) & 1) == 0)
38202 {
38203 /* Remove the invalid clause(s) from the list to avoid
38204 confusing the rest of the compiler. */
38205 clauses = prev;
38206 error_at (here, "%qs is not valid for %qs", c_name, where);
38207 }
38208 }
38209
38210 saw_error:
38211 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38212
38213 if (finish_p)
38214 return finish_omp_clauses (clauses, C_ORT_ACC);
38215
38216 return clauses;
38217 }
38218
38219 /* Parse all OpenMP clauses. The set clauses allowed by the directive
38220 is a bitmask in MASK. Return the list of clauses found.
38221 FINISH_P set if finish_omp_clauses should be called.
38222 NESTED non-zero if clauses should be terminated by closing paren instead
38223 of end of pragma. If it is 2, additionally commas are required in between
38224 the clauses. */
38225
38226 static tree
38227 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
38228 const char *where, cp_token *pragma_tok,
38229 bool finish_p = true, int nested = 0)
38230 {
38231 tree clauses = NULL;
38232 bool first = true;
38233 cp_token *token = NULL;
38234
38235 /* Don't create location wrapper nodes within OpenMP clauses. */
38236 auto_suppress_location_wrappers sentinel;
38237
38238 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38239 {
38240 pragma_omp_clause c_kind;
38241 const char *c_name;
38242 tree prev = clauses;
38243
38244 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38245 break;
38246
38247 if (!first)
38248 {
38249 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38250 cp_lexer_consume_token (parser->lexer);
38251 else if (nested == 2)
38252 error_at (cp_lexer_peek_token (parser->lexer)->location,
38253 "clauses in %<simd%> trait should be separated "
38254 "by %<,%>");
38255 }
38256
38257 token = cp_lexer_peek_token (parser->lexer);
38258 c_kind = cp_parser_omp_clause_name (parser);
38259
38260 switch (c_kind)
38261 {
38262 case PRAGMA_OMP_CLAUSE_BIND:
38263 clauses = cp_parser_omp_clause_bind (parser, clauses,
38264 token->location);
38265 c_name = "bind";
38266 break;
38267 case PRAGMA_OMP_CLAUSE_COLLAPSE:
38268 clauses = cp_parser_omp_clause_collapse (parser, clauses,
38269 token->location);
38270 c_name = "collapse";
38271 break;
38272 case PRAGMA_OMP_CLAUSE_COPYIN:
38273 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
38274 c_name = "copyin";
38275 break;
38276 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
38277 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
38278 clauses);
38279 c_name = "copyprivate";
38280 break;
38281 case PRAGMA_OMP_CLAUSE_DEFAULT:
38282 clauses = cp_parser_omp_clause_default (parser, clauses,
38283 token->location, false);
38284 c_name = "default";
38285 break;
38286 case PRAGMA_OMP_CLAUSE_FINAL:
38287 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
38288 c_name = "final";
38289 break;
38290 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
38291 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38292 clauses);
38293 c_name = "firstprivate";
38294 break;
38295 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
38296 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
38297 token->location);
38298 c_name = "grainsize";
38299 break;
38300 case PRAGMA_OMP_CLAUSE_HINT:
38301 clauses = cp_parser_omp_clause_hint (parser, clauses,
38302 token->location);
38303 c_name = "hint";
38304 break;
38305 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
38306 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
38307 token->location);
38308 c_name = "defaultmap";
38309 break;
38310 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
38311 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
38312 clauses);
38313 c_name = "use_device_ptr";
38314 break;
38315 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
38316 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
38317 clauses);
38318 c_name = "use_device_addr";
38319 break;
38320 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
38321 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
38322 clauses);
38323 c_name = "is_device_ptr";
38324 break;
38325 case PRAGMA_OMP_CLAUSE_IF:
38326 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
38327 true);
38328 c_name = "if";
38329 break;
38330 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
38331 clauses
38332 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
38333 true, clauses);
38334 c_name = "in_reduction";
38335 break;
38336 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
38337 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
38338 c_name = "lastprivate";
38339 break;
38340 case PRAGMA_OMP_CLAUSE_MERGEABLE:
38341 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
38342 token->location);
38343 c_name = "mergeable";
38344 break;
38345 case PRAGMA_OMP_CLAUSE_NOWAIT:
38346 clauses = cp_parser_omp_clause_nowait (parser, clauses,
38347 token->location);
38348 c_name = "nowait";
38349 break;
38350 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
38351 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
38352 token->location);
38353 c_name = "num_tasks";
38354 break;
38355 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
38356 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
38357 token->location);
38358 c_name = "num_threads";
38359 break;
38360 case PRAGMA_OMP_CLAUSE_ORDER:
38361 clauses = cp_parser_omp_clause_order (parser, clauses,
38362 token->location);
38363 c_name = "order";
38364 break;
38365 case PRAGMA_OMP_CLAUSE_ORDERED:
38366 clauses = cp_parser_omp_clause_ordered (parser, clauses,
38367 token->location);
38368 c_name = "ordered";
38369 break;
38370 case PRAGMA_OMP_CLAUSE_PRIORITY:
38371 clauses = cp_parser_omp_clause_priority (parser, clauses,
38372 token->location);
38373 c_name = "priority";
38374 break;
38375 case PRAGMA_OMP_CLAUSE_PRIVATE:
38376 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
38377 clauses);
38378 c_name = "private";
38379 break;
38380 case PRAGMA_OMP_CLAUSE_REDUCTION:
38381 clauses
38382 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
38383 true, clauses);
38384 c_name = "reduction";
38385 break;
38386 case PRAGMA_OMP_CLAUSE_SCHEDULE:
38387 clauses = cp_parser_omp_clause_schedule (parser, clauses,
38388 token->location);
38389 c_name = "schedule";
38390 break;
38391 case PRAGMA_OMP_CLAUSE_SHARED:
38392 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
38393 clauses);
38394 c_name = "shared";
38395 break;
38396 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
38397 clauses
38398 = cp_parser_omp_clause_reduction (parser,
38399 OMP_CLAUSE_TASK_REDUCTION,
38400 true, clauses);
38401 c_name = "task_reduction";
38402 break;
38403 case PRAGMA_OMP_CLAUSE_UNTIED:
38404 clauses = cp_parser_omp_clause_untied (parser, clauses,
38405 token->location);
38406 c_name = "untied";
38407 break;
38408 case PRAGMA_OMP_CLAUSE_INBRANCH:
38409 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
38410 clauses, token->location);
38411 c_name = "inbranch";
38412 break;
38413 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
38414 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
38415 clauses);
38416 c_name = "nontemporal";
38417 break;
38418 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
38419 clauses = cp_parser_omp_clause_branch (parser,
38420 OMP_CLAUSE_NOTINBRANCH,
38421 clauses, token->location);
38422 c_name = "notinbranch";
38423 break;
38424 case PRAGMA_OMP_CLAUSE_PARALLEL:
38425 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
38426 clauses, token->location);
38427 c_name = "parallel";
38428 if (!first)
38429 {
38430 clause_not_first:
38431 error_at (token->location, "%qs must be the first clause of %qs",
38432 c_name, where);
38433 clauses = prev;
38434 }
38435 break;
38436 case PRAGMA_OMP_CLAUSE_FOR:
38437 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
38438 clauses, token->location);
38439 c_name = "for";
38440 if (!first)
38441 goto clause_not_first;
38442 break;
38443 case PRAGMA_OMP_CLAUSE_SECTIONS:
38444 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
38445 clauses, token->location);
38446 c_name = "sections";
38447 if (!first)
38448 goto clause_not_first;
38449 break;
38450 case PRAGMA_OMP_CLAUSE_TASKGROUP:
38451 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
38452 clauses, token->location);
38453 c_name = "taskgroup";
38454 if (!first)
38455 goto clause_not_first;
38456 break;
38457 case PRAGMA_OMP_CLAUSE_LINK:
38458 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
38459 c_name = "to";
38460 break;
38461 case PRAGMA_OMP_CLAUSE_TO:
38462 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
38463 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
38464 clauses);
38465 else
38466 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
38467 c_name = "to";
38468 break;
38469 case PRAGMA_OMP_CLAUSE_FROM:
38470 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
38471 c_name = "from";
38472 break;
38473 case PRAGMA_OMP_CLAUSE_UNIFORM:
38474 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
38475 clauses);
38476 c_name = "uniform";
38477 break;
38478 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
38479 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
38480 token->location);
38481 c_name = "num_teams";
38482 break;
38483 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
38484 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
38485 token->location);
38486 c_name = "thread_limit";
38487 break;
38488 case PRAGMA_OMP_CLAUSE_ALIGNED:
38489 clauses = cp_parser_omp_clause_aligned (parser, clauses);
38490 c_name = "aligned";
38491 break;
38492 case PRAGMA_OMP_CLAUSE_ALLOCATE:
38493 clauses = cp_parser_omp_clause_allocate (parser, clauses);
38494 c_name = "allocate";
38495 break;
38496 case PRAGMA_OMP_CLAUSE_LINEAR:
38497 {
38498 bool declare_simd = false;
38499 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
38500 declare_simd = true;
38501 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
38502 }
38503 c_name = "linear";
38504 break;
38505 case PRAGMA_OMP_CLAUSE_DEPEND:
38506 clauses = cp_parser_omp_clause_depend (parser, clauses,
38507 token->location);
38508 c_name = "depend";
38509 break;
38510 case PRAGMA_OMP_CLAUSE_DETACH:
38511 clauses = cp_parser_omp_clause_detach (parser, clauses);
38512 c_name = "detach";
38513 break;
38514 case PRAGMA_OMP_CLAUSE_MAP:
38515 clauses = cp_parser_omp_clause_map (parser, clauses);
38516 c_name = "map";
38517 break;
38518 case PRAGMA_OMP_CLAUSE_DEVICE:
38519 clauses = cp_parser_omp_clause_device (parser, clauses,
38520 token->location);
38521 c_name = "device";
38522 break;
38523 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
38524 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
38525 token->location);
38526 c_name = "dist_schedule";
38527 break;
38528 case PRAGMA_OMP_CLAUSE_PROC_BIND:
38529 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
38530 token->location);
38531 c_name = "proc_bind";
38532 break;
38533 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
38534 clauses = cp_parser_omp_clause_device_type (parser, clauses,
38535 token->location);
38536 c_name = "device_type";
38537 break;
38538 case PRAGMA_OMP_CLAUSE_SAFELEN:
38539 clauses = cp_parser_omp_clause_safelen (parser, clauses,
38540 token->location);
38541 c_name = "safelen";
38542 break;
38543 case PRAGMA_OMP_CLAUSE_SIMDLEN:
38544 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
38545 token->location);
38546 c_name = "simdlen";
38547 break;
38548 case PRAGMA_OMP_CLAUSE_NOGROUP:
38549 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
38550 token->location);
38551 c_name = "nogroup";
38552 break;
38553 case PRAGMA_OMP_CLAUSE_THREADS:
38554 clauses
38555 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
38556 clauses, token->location);
38557 c_name = "threads";
38558 break;
38559 case PRAGMA_OMP_CLAUSE_SIMD:
38560 clauses
38561 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
38562 clauses, token->location);
38563 c_name = "simd";
38564 break;
38565 default:
38566 cp_parser_error (parser, "expected %<#pragma omp%> clause");
38567 goto saw_error;
38568 }
38569
38570 first = false;
38571
38572 if (((mask >> c_kind) & 1) == 0)
38573 {
38574 /* Remove the invalid clause(s) from the list to avoid
38575 confusing the rest of the compiler. */
38576 clauses = prev;
38577 error_at (token->location, "%qs is not valid for %qs", c_name, where);
38578 }
38579 }
38580 saw_error:
38581 if (!nested)
38582 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38583 if (finish_p)
38584 {
38585 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
38586 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
38587 else
38588 return finish_omp_clauses (clauses, C_ORT_OMP);
38589 }
38590 return clauses;
38591 }
38592
38593 /* OpenMP 2.5:
38594 structured-block:
38595 statement
38596
38597 In practice, we're also interested in adding the statement to an
38598 outer node. So it is convenient if we work around the fact that
38599 cp_parser_statement calls add_stmt. */
38600
38601 static unsigned
38602 cp_parser_begin_omp_structured_block (cp_parser *parser)
38603 {
38604 unsigned save = parser->in_statement;
38605
38606 /* Only move the values to IN_OMP_BLOCK if they weren't false.
38607 This preserves the "not within loop or switch" style error messages
38608 for nonsense cases like
38609 void foo() {
38610 #pragma omp single
38611 break;
38612 }
38613 */
38614 if (parser->in_statement)
38615 parser->in_statement = IN_OMP_BLOCK;
38616
38617 return save;
38618 }
38619
38620 static void
38621 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
38622 {
38623 parser->in_statement = save;
38624 }
38625
38626 static tree
38627 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
38628 {
38629 tree stmt = begin_omp_structured_block ();
38630 unsigned int save = cp_parser_begin_omp_structured_block (parser);
38631
38632 cp_parser_statement (parser, NULL_TREE, false, if_p);
38633
38634 cp_parser_end_omp_structured_block (parser, save);
38635 return finish_omp_structured_block (stmt);
38636 }
38637
38638 /* OpenMP 5.0:
38639 # pragma omp allocate (list) [allocator(allocator)] */
38640
38641 static void
38642 cp_parser_omp_allocate (cp_parser *parser, cp_token *pragma_tok)
38643 {
38644 tree allocator = NULL_TREE;
38645 location_t loc = pragma_tok->location;
38646 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ALLOCATE, NULL_TREE);
38647
38648 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38649 {
38650 matching_parens parens;
38651 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38652 const char *p = IDENTIFIER_POINTER (id);
38653 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
38654 cp_lexer_consume_token (parser->lexer);
38655 if (strcmp (p, "allocator") != 0)
38656 error_at (cloc, "expected %<allocator%>");
38657 else if (parens.require_open (parser))
38658 {
38659 allocator = cp_parser_assignment_expression (parser);
38660 if (allocator == error_mark_node)
38661 allocator = NULL_TREE;
38662 parens.require_close (parser);
38663 }
38664 }
38665 cp_parser_require_pragma_eol (parser, pragma_tok);
38666
38667 if (allocator)
38668 for (tree c = nl; c != NULL_TREE; c = OMP_CLAUSE_CHAIN (c))
38669 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
38670
38671 sorry_at (loc, "%<#pragma omp allocate%> not yet supported");
38672 }
38673
38674 /* OpenMP 2.5:
38675 # pragma omp atomic new-line
38676 expression-stmt
38677
38678 expression-stmt:
38679 x binop= expr | x++ | ++x | x-- | --x
38680 binop:
38681 +, *, -, /, &, ^, |, <<, >>
38682
38683 where x is an lvalue expression with scalar type.
38684
38685 OpenMP 3.1:
38686 # pragma omp atomic new-line
38687 update-stmt
38688
38689 # pragma omp atomic read new-line
38690 read-stmt
38691
38692 # pragma omp atomic write new-line
38693 write-stmt
38694
38695 # pragma omp atomic update new-line
38696 update-stmt
38697
38698 # pragma omp atomic capture new-line
38699 capture-stmt
38700
38701 # pragma omp atomic capture new-line
38702 capture-block
38703
38704 read-stmt:
38705 v = x
38706 write-stmt:
38707 x = expr
38708 update-stmt:
38709 expression-stmt | x = x binop expr
38710 capture-stmt:
38711 v = expression-stmt
38712 capture-block:
38713 { v = x; update-stmt; } | { update-stmt; v = x; }
38714
38715 OpenMP 4.0:
38716 update-stmt:
38717 expression-stmt | x = x binop expr | x = expr binop x
38718 capture-stmt:
38719 v = update-stmt
38720 capture-block:
38721 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
38722
38723 where x and v are lvalue expressions with scalar type. */
38724
38725 static void
38726 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok, bool openacc)
38727 {
38728 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
38729 tree rhs1 = NULL_TREE, orig_lhs;
38730 location_t loc = pragma_tok->location;
38731 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
38732 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
38733 bool structured_block = false;
38734 bool first = true;
38735 tree clauses = NULL_TREE;
38736
38737 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38738 {
38739 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38740 cp_lexer_consume_token (parser->lexer);
38741
38742 first = false;
38743
38744 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38745 {
38746 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38747 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
38748 const char *p = IDENTIFIER_POINTER (id);
38749 enum tree_code new_code = ERROR_MARK;
38750 enum omp_memory_order new_memory_order
38751 = OMP_MEMORY_ORDER_UNSPECIFIED;
38752
38753 if (!strcmp (p, "read"))
38754 new_code = OMP_ATOMIC_READ;
38755 else if (!strcmp (p, "write"))
38756 new_code = NOP_EXPR;
38757 else if (!strcmp (p, "update"))
38758 new_code = OMP_ATOMIC;
38759 else if (!strcmp (p, "capture"))
38760 new_code = OMP_ATOMIC_CAPTURE_NEW;
38761 else if (openacc)
38762 {
38763 p = NULL;
38764 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
38765 "or %<capture%> clause");
38766 }
38767 else if (!strcmp (p, "seq_cst"))
38768 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
38769 else if (!strcmp (p, "acq_rel"))
38770 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
38771 else if (!strcmp (p, "release"))
38772 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
38773 else if (!strcmp (p, "acquire"))
38774 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
38775 else if (!strcmp (p, "relaxed"))
38776 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
38777 else if (!strcmp (p, "hint"))
38778 {
38779 cp_lexer_consume_token (parser->lexer);
38780 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
38781 continue;
38782 }
38783 else
38784 {
38785 p = NULL;
38786 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
38787 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
38788 "%<release%>, %<relaxed%> or %<hint%> clause");
38789 }
38790 if (p)
38791 {
38792 if (new_code != ERROR_MARK)
38793 {
38794 /* OpenACC permits 'update capture'. */
38795 if (openacc
38796 && code == OMP_ATOMIC
38797 && new_code == OMP_ATOMIC_CAPTURE_NEW)
38798 code = new_code;
38799 else if (code != ERROR_MARK)
38800 error_at (cloc, "too many atomic clauses");
38801 else
38802 code = new_code;
38803 }
38804 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
38805 {
38806 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
38807 error_at (cloc, "too many memory order clauses");
38808 else
38809 memory_order = new_memory_order;
38810 }
38811 cp_lexer_consume_token (parser->lexer);
38812 continue;
38813 }
38814 }
38815 break;
38816 }
38817 cp_parser_require_pragma_eol (parser, pragma_tok);
38818
38819 if (code == ERROR_MARK)
38820 code = OMP_ATOMIC;
38821 if (openacc)
38822 memory_order = OMP_MEMORY_ORDER_RELAXED;
38823 else if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
38824 {
38825 omp_requires_mask
38826 = (enum omp_requires) (omp_requires_mask
38827 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
38828 switch ((enum omp_memory_order)
38829 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
38830 {
38831 case OMP_MEMORY_ORDER_UNSPECIFIED:
38832 case OMP_MEMORY_ORDER_RELAXED:
38833 memory_order = OMP_MEMORY_ORDER_RELAXED;
38834 break;
38835 case OMP_MEMORY_ORDER_SEQ_CST:
38836 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
38837 break;
38838 case OMP_MEMORY_ORDER_ACQ_REL:
38839 switch (code)
38840 {
38841 case OMP_ATOMIC_READ:
38842 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
38843 break;
38844 case NOP_EXPR: /* atomic write */
38845 case OMP_ATOMIC:
38846 memory_order = OMP_MEMORY_ORDER_RELEASE;
38847 break;
38848 default:
38849 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
38850 break;
38851 }
38852 break;
38853 default:
38854 gcc_unreachable ();
38855 }
38856 }
38857 else
38858 switch (code)
38859 {
38860 case OMP_ATOMIC_READ:
38861 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
38862 || memory_order == OMP_MEMORY_ORDER_RELEASE)
38863 {
38864 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
38865 "%<acq_rel%> or %<release%> clauses");
38866 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
38867 }
38868 break;
38869 case NOP_EXPR: /* atomic write */
38870 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
38871 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
38872 {
38873 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
38874 "%<acq_rel%> or %<acquire%> clauses");
38875 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
38876 }
38877 break;
38878 case OMP_ATOMIC:
38879 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
38880 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
38881 {
38882 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
38883 "%<acq_rel%> or %<acquire%> clauses");
38884 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
38885 }
38886 break;
38887 default:
38888 break;
38889 }
38890
38891 switch (code)
38892 {
38893 case OMP_ATOMIC_READ:
38894 case NOP_EXPR: /* atomic write */
38895 v = cp_parser_unary_expression (parser);
38896 if (v == error_mark_node)
38897 goto saw_error;
38898 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
38899 goto saw_error;
38900 if (code == NOP_EXPR)
38901 lhs = cp_parser_expression (parser);
38902 else
38903 lhs = cp_parser_unary_expression (parser);
38904 if (lhs == error_mark_node)
38905 goto saw_error;
38906 if (code == NOP_EXPR)
38907 {
38908 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
38909 opcode. */
38910 code = OMP_ATOMIC;
38911 rhs = lhs;
38912 lhs = v;
38913 v = NULL_TREE;
38914 }
38915 goto done;
38916 case OMP_ATOMIC_CAPTURE_NEW:
38917 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
38918 {
38919 cp_lexer_consume_token (parser->lexer);
38920 structured_block = true;
38921 }
38922 else
38923 {
38924 v = cp_parser_unary_expression (parser);
38925 if (v == error_mark_node)
38926 goto saw_error;
38927 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
38928 goto saw_error;
38929 }
38930 default:
38931 break;
38932 }
38933
38934 restart:
38935 lhs = cp_parser_unary_expression (parser);
38936 orig_lhs = lhs;
38937 switch (TREE_CODE (lhs))
38938 {
38939 case ERROR_MARK:
38940 goto saw_error;
38941
38942 case POSTINCREMENT_EXPR:
38943 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
38944 code = OMP_ATOMIC_CAPTURE_OLD;
38945 /* FALLTHROUGH */
38946 case PREINCREMENT_EXPR:
38947 lhs = TREE_OPERAND (lhs, 0);
38948 opcode = PLUS_EXPR;
38949 rhs = integer_one_node;
38950 break;
38951
38952 case POSTDECREMENT_EXPR:
38953 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
38954 code = OMP_ATOMIC_CAPTURE_OLD;
38955 /* FALLTHROUGH */
38956 case PREDECREMENT_EXPR:
38957 lhs = TREE_OPERAND (lhs, 0);
38958 opcode = MINUS_EXPR;
38959 rhs = integer_one_node;
38960 break;
38961
38962 case COMPOUND_EXPR:
38963 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
38964 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
38965 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
38966 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
38967 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
38968 (TREE_OPERAND (lhs, 1), 0), 0)))
38969 == BOOLEAN_TYPE)
38970 /* Undo effects of boolean_increment for post {in,de}crement. */
38971 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
38972 /* FALLTHRU */
38973 case MODIFY_EXPR:
38974 if (TREE_CODE (lhs) == MODIFY_EXPR
38975 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
38976 {
38977 /* Undo effects of boolean_increment. */
38978 if (integer_onep (TREE_OPERAND (lhs, 1)))
38979 {
38980 /* This is pre or post increment. */
38981 rhs = TREE_OPERAND (lhs, 1);
38982 lhs = TREE_OPERAND (lhs, 0);
38983 opcode = NOP_EXPR;
38984 if (code == OMP_ATOMIC_CAPTURE_NEW
38985 && !structured_block
38986 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
38987 code = OMP_ATOMIC_CAPTURE_OLD;
38988 break;
38989 }
38990 }
38991 /* FALLTHRU */
38992 default:
38993 switch (cp_lexer_peek_token (parser->lexer)->type)
38994 {
38995 case CPP_MULT_EQ:
38996 opcode = MULT_EXPR;
38997 break;
38998 case CPP_DIV_EQ:
38999 opcode = TRUNC_DIV_EXPR;
39000 break;
39001 case CPP_PLUS_EQ:
39002 opcode = PLUS_EXPR;
39003 break;
39004 case CPP_MINUS_EQ:
39005 opcode = MINUS_EXPR;
39006 break;
39007 case CPP_LSHIFT_EQ:
39008 opcode = LSHIFT_EXPR;
39009 break;
39010 case CPP_RSHIFT_EQ:
39011 opcode = RSHIFT_EXPR;
39012 break;
39013 case CPP_AND_EQ:
39014 opcode = BIT_AND_EXPR;
39015 break;
39016 case CPP_OR_EQ:
39017 opcode = BIT_IOR_EXPR;
39018 break;
39019 case CPP_XOR_EQ:
39020 opcode = BIT_XOR_EXPR;
39021 break;
39022 case CPP_EQ:
39023 enum cp_parser_prec oprec;
39024 cp_token *token;
39025 cp_lexer_consume_token (parser->lexer);
39026 cp_parser_parse_tentatively (parser);
39027 rhs1 = cp_parser_simple_cast_expression (parser);
39028 if (rhs1 == error_mark_node)
39029 {
39030 cp_parser_abort_tentative_parse (parser);
39031 cp_parser_simple_cast_expression (parser);
39032 goto saw_error;
39033 }
39034 token = cp_lexer_peek_token (parser->lexer);
39035 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
39036 {
39037 cp_parser_abort_tentative_parse (parser);
39038 cp_parser_parse_tentatively (parser);
39039 rhs = cp_parser_binary_expression (parser, false, true,
39040 PREC_NOT_OPERATOR, NULL);
39041 if (rhs == error_mark_node)
39042 {
39043 cp_parser_abort_tentative_parse (parser);
39044 cp_parser_binary_expression (parser, false, true,
39045 PREC_NOT_OPERATOR, NULL);
39046 goto saw_error;
39047 }
39048 switch (TREE_CODE (rhs))
39049 {
39050 case MULT_EXPR:
39051 case TRUNC_DIV_EXPR:
39052 case RDIV_EXPR:
39053 case PLUS_EXPR:
39054 case MINUS_EXPR:
39055 case LSHIFT_EXPR:
39056 case RSHIFT_EXPR:
39057 case BIT_AND_EXPR:
39058 case BIT_IOR_EXPR:
39059 case BIT_XOR_EXPR:
39060 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
39061 {
39062 if (cp_parser_parse_definitely (parser))
39063 {
39064 opcode = TREE_CODE (rhs);
39065 rhs1 = TREE_OPERAND (rhs, 0);
39066 rhs = TREE_OPERAND (rhs, 1);
39067 goto stmt_done;
39068 }
39069 else
39070 goto saw_error;
39071 }
39072 break;
39073 default:
39074 break;
39075 }
39076 cp_parser_abort_tentative_parse (parser);
39077 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
39078 {
39079 rhs = cp_parser_expression (parser);
39080 if (rhs == error_mark_node)
39081 goto saw_error;
39082 opcode = NOP_EXPR;
39083 rhs1 = NULL_TREE;
39084 goto stmt_done;
39085 }
39086 cp_parser_error (parser,
39087 "invalid form of %<#pragma omp atomic%>");
39088 goto saw_error;
39089 }
39090 if (!cp_parser_parse_definitely (parser))
39091 goto saw_error;
39092 switch (token->type)
39093 {
39094 case CPP_SEMICOLON:
39095 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
39096 {
39097 code = OMP_ATOMIC_CAPTURE_OLD;
39098 v = lhs;
39099 lhs = NULL_TREE;
39100 lhs1 = rhs1;
39101 rhs1 = NULL_TREE;
39102 cp_lexer_consume_token (parser->lexer);
39103 goto restart;
39104 }
39105 else if (structured_block)
39106 {
39107 opcode = NOP_EXPR;
39108 rhs = rhs1;
39109 rhs1 = NULL_TREE;
39110 goto stmt_done;
39111 }
39112 cp_parser_error (parser,
39113 "invalid form of %<#pragma omp atomic%>");
39114 goto saw_error;
39115 case CPP_MULT:
39116 opcode = MULT_EXPR;
39117 break;
39118 case CPP_DIV:
39119 opcode = TRUNC_DIV_EXPR;
39120 break;
39121 case CPP_PLUS:
39122 opcode = PLUS_EXPR;
39123 break;
39124 case CPP_MINUS:
39125 opcode = MINUS_EXPR;
39126 break;
39127 case CPP_LSHIFT:
39128 opcode = LSHIFT_EXPR;
39129 break;
39130 case CPP_RSHIFT:
39131 opcode = RSHIFT_EXPR;
39132 break;
39133 case CPP_AND:
39134 opcode = BIT_AND_EXPR;
39135 break;
39136 case CPP_OR:
39137 opcode = BIT_IOR_EXPR;
39138 break;
39139 case CPP_XOR:
39140 opcode = BIT_XOR_EXPR;
39141 break;
39142 default:
39143 cp_parser_error (parser,
39144 "invalid operator for %<#pragma omp atomic%>");
39145 goto saw_error;
39146 }
39147 oprec = TOKEN_PRECEDENCE (token);
39148 gcc_assert (oprec != PREC_NOT_OPERATOR);
39149 if (commutative_tree_code (opcode))
39150 oprec = (enum cp_parser_prec) (oprec - 1);
39151 cp_lexer_consume_token (parser->lexer);
39152 rhs = cp_parser_binary_expression (parser, false, false,
39153 oprec, NULL);
39154 if (rhs == error_mark_node)
39155 goto saw_error;
39156 goto stmt_done;
39157 /* FALLTHROUGH */
39158 default:
39159 cp_parser_error (parser,
39160 "invalid operator for %<#pragma omp atomic%>");
39161 goto saw_error;
39162 }
39163 cp_lexer_consume_token (parser->lexer);
39164
39165 rhs = cp_parser_expression (parser);
39166 if (rhs == error_mark_node)
39167 goto saw_error;
39168 break;
39169 }
39170 stmt_done:
39171 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
39172 {
39173 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
39174 goto saw_error;
39175 v = cp_parser_unary_expression (parser);
39176 if (v == error_mark_node)
39177 goto saw_error;
39178 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
39179 goto saw_error;
39180 lhs1 = cp_parser_unary_expression (parser);
39181 if (lhs1 == error_mark_node)
39182 goto saw_error;
39183 }
39184 if (structured_block)
39185 {
39186 cp_parser_consume_semicolon_at_end_of_statement (parser);
39187 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
39188 }
39189 done:
39190 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
39191 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
39192 rhs1, clauses, memory_order);
39193 if (!structured_block)
39194 cp_parser_consume_semicolon_at_end_of_statement (parser);
39195 return;
39196
39197 saw_error:
39198 cp_parser_skip_to_end_of_block_or_statement (parser);
39199 if (structured_block)
39200 {
39201 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
39202 cp_lexer_consume_token (parser->lexer);
39203 else if (code == OMP_ATOMIC_CAPTURE_NEW)
39204 {
39205 cp_parser_skip_to_end_of_block_or_statement (parser);
39206 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
39207 cp_lexer_consume_token (parser->lexer);
39208 }
39209 }
39210 }
39211
39212
39213 /* OpenMP 2.5:
39214 # pragma omp barrier new-line */
39215
39216 static void
39217 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
39218 {
39219 cp_parser_require_pragma_eol (parser, pragma_tok);
39220 finish_omp_barrier ();
39221 }
39222
39223 /* OpenMP 2.5:
39224 # pragma omp critical [(name)] new-line
39225 structured-block
39226
39227 OpenMP 4.5:
39228 # pragma omp critical [(name) [hint(expression)]] new-line
39229 structured-block */
39230
39231 #define OMP_CRITICAL_CLAUSE_MASK \
39232 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
39233
39234 static tree
39235 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39236 {
39237 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
39238
39239 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39240 {
39241 matching_parens parens;
39242 parens.consume_open (parser);
39243
39244 name = cp_parser_identifier (parser);
39245
39246 if (name == error_mark_node
39247 || !parens.require_close (parser))
39248 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39249 /*or_comma=*/false,
39250 /*consume_paren=*/true);
39251 if (name == error_mark_node)
39252 name = NULL;
39253
39254 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
39255 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
39256 cp_lexer_consume_token (parser->lexer);
39257 }
39258
39259 clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
39260 "#pragma omp critical", pragma_tok);
39261
39262 stmt = cp_parser_omp_structured_block (parser, if_p);
39263 return c_finish_omp_critical (input_location, stmt, name, clauses);
39264 }
39265
39266 /* OpenMP 5.0:
39267 # pragma omp depobj ( depobj ) depobj-clause new-line
39268
39269 depobj-clause:
39270 depend (dependence-type : locator)
39271 destroy
39272 update (dependence-type)
39273
39274 dependence-type:
39275 in
39276 out
39277 inout
39278 mutexinout */
39279
39280 static void
39281 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
39282 {
39283 location_t loc = pragma_tok->location;
39284 matching_parens parens;
39285 if (!parens.require_open (parser))
39286 {
39287 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39288 return;
39289 }
39290
39291 tree depobj = cp_parser_assignment_expression (parser);
39292
39293 if (!parens.require_close (parser))
39294 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39295 /*or_comma=*/false,
39296 /*consume_paren=*/true);
39297
39298 tree clause = NULL_TREE;
39299 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
39300 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
39301 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39302 {
39303 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39304 const char *p = IDENTIFIER_POINTER (id);
39305
39306 cp_lexer_consume_token (parser->lexer);
39307 if (!strcmp ("depend", p))
39308 {
39309 /* Don't create location wrapper nodes within the depend clause. */
39310 auto_suppress_location_wrappers sentinel;
39311 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
39312 if (clause)
39313 clause = finish_omp_clauses (clause, C_ORT_OMP);
39314 if (!clause)
39315 clause = error_mark_node;
39316 }
39317 else if (!strcmp ("destroy", p))
39318 kind = OMP_CLAUSE_DEPEND_LAST;
39319 else if (!strcmp ("update", p))
39320 {
39321 matching_parens c_parens;
39322 if (c_parens.require_open (parser))
39323 {
39324 location_t c2_loc
39325 = cp_lexer_peek_token (parser->lexer)->location;
39326 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39327 {
39328 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
39329 const char *p2 = IDENTIFIER_POINTER (id2);
39330
39331 cp_lexer_consume_token (parser->lexer);
39332 if (!strcmp ("in", p2))
39333 kind = OMP_CLAUSE_DEPEND_IN;
39334 else if (!strcmp ("out", p2))
39335 kind = OMP_CLAUSE_DEPEND_OUT;
39336 else if (!strcmp ("inout", p2))
39337 kind = OMP_CLAUSE_DEPEND_INOUT;
39338 else if (!strcmp ("mutexinoutset", p2))
39339 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
39340 }
39341 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
39342 {
39343 clause = error_mark_node;
39344 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
39345 "%<mutexinoutset%>");
39346 }
39347 if (!c_parens.require_close (parser))
39348 cp_parser_skip_to_closing_parenthesis (parser,
39349 /*recovering=*/true,
39350 /*or_comma=*/false,
39351 /*consume_paren=*/true);
39352 }
39353 else
39354 clause = error_mark_node;
39355 }
39356 }
39357 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
39358 {
39359 clause = error_mark_node;
39360 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
39361 }
39362 cp_parser_require_pragma_eol (parser, pragma_tok);
39363
39364 finish_omp_depobj (loc, depobj, kind, clause);
39365 }
39366
39367
39368 /* OpenMP 2.5:
39369 # pragma omp flush flush-vars[opt] new-line
39370
39371 flush-vars:
39372 ( variable-list )
39373
39374 OpenMP 5.0:
39375 # pragma omp flush memory-order-clause new-line */
39376
39377 static void
39378 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
39379 {
39380 enum memmodel mo = MEMMODEL_LAST;
39381 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39382 {
39383 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39384 const char *p = IDENTIFIER_POINTER (id);
39385 if (!strcmp (p, "acq_rel"))
39386 mo = MEMMODEL_ACQ_REL;
39387 else if (!strcmp (p, "release"))
39388 mo = MEMMODEL_RELEASE;
39389 else if (!strcmp (p, "acquire"))
39390 mo = MEMMODEL_ACQUIRE;
39391 else
39392 error_at (cp_lexer_peek_token (parser->lexer)->location,
39393 "expected %<acq_rel%>, %<release%> or %<acquire%>");
39394 cp_lexer_consume_token (parser->lexer);
39395 }
39396 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39397 {
39398 if (mo != MEMMODEL_LAST)
39399 error_at (cp_lexer_peek_token (parser->lexer)->location,
39400 "%<flush%> list specified together with memory order "
39401 "clause");
39402 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
39403 }
39404 cp_parser_require_pragma_eol (parser, pragma_tok);
39405
39406 finish_omp_flush (mo);
39407 }
39408
39409 /* Helper function, to parse omp for increment expression. */
39410
39411 static tree
39412 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
39413 {
39414 tree cond = cp_parser_binary_expression (parser, false, true,
39415 PREC_NOT_OPERATOR, NULL);
39416 if (cond == error_mark_node
39417 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
39418 {
39419 cp_parser_skip_to_end_of_statement (parser);
39420 return error_mark_node;
39421 }
39422
39423 switch (TREE_CODE (cond))
39424 {
39425 case GT_EXPR:
39426 case GE_EXPR:
39427 case LT_EXPR:
39428 case LE_EXPR:
39429 break;
39430 case NE_EXPR:
39431 if (code != OACC_LOOP)
39432 break;
39433 gcc_fallthrough ();
39434 default:
39435 return error_mark_node;
39436 }
39437
39438 /* If decl is an iterator, preserve LHS and RHS of the relational
39439 expr until finish_omp_for. */
39440 if (decl
39441 && (type_dependent_expression_p (decl)
39442 || CLASS_TYPE_P (TREE_TYPE (decl))))
39443 return cond;
39444
39445 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
39446 TREE_CODE (cond),
39447 TREE_OPERAND (cond, 0), ERROR_MARK,
39448 TREE_OPERAND (cond, 1), ERROR_MARK,
39449 /*overload=*/NULL, tf_warning_or_error);
39450 }
39451
39452 /* Helper function, to parse omp for increment expression. */
39453
39454 static tree
39455 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
39456 {
39457 cp_token *token = cp_lexer_peek_token (parser->lexer);
39458 enum tree_code op;
39459 tree lhs, rhs;
39460 cp_id_kind idk;
39461 bool decl_first;
39462
39463 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
39464 {
39465 op = (token->type == CPP_PLUS_PLUS
39466 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
39467 cp_lexer_consume_token (parser->lexer);
39468 lhs = cp_parser_simple_cast_expression (parser);
39469 if (lhs != decl
39470 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
39471 return error_mark_node;
39472 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
39473 }
39474
39475 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
39476 if (lhs != decl
39477 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
39478 return error_mark_node;
39479
39480 token = cp_lexer_peek_token (parser->lexer);
39481 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
39482 {
39483 op = (token->type == CPP_PLUS_PLUS
39484 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
39485 cp_lexer_consume_token (parser->lexer);
39486 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
39487 }
39488
39489 op = cp_parser_assignment_operator_opt (parser);
39490 if (op == ERROR_MARK)
39491 return error_mark_node;
39492
39493 if (op != NOP_EXPR)
39494 {
39495 rhs = cp_parser_assignment_expression (parser);
39496 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
39497 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
39498 }
39499
39500 lhs = cp_parser_binary_expression (parser, false, false,
39501 PREC_ADDITIVE_EXPRESSION, NULL);
39502 token = cp_lexer_peek_token (parser->lexer);
39503 decl_first = (lhs == decl
39504 || (processing_template_decl && cp_tree_equal (lhs, decl)));
39505 if (decl_first)
39506 lhs = NULL_TREE;
39507 if (token->type != CPP_PLUS
39508 && token->type != CPP_MINUS)
39509 return error_mark_node;
39510
39511 do
39512 {
39513 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
39514 cp_lexer_consume_token (parser->lexer);
39515 rhs = cp_parser_binary_expression (parser, false, false,
39516 PREC_ADDITIVE_EXPRESSION, NULL);
39517 token = cp_lexer_peek_token (parser->lexer);
39518 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
39519 {
39520 if (lhs == NULL_TREE)
39521 {
39522 if (op == PLUS_EXPR)
39523 lhs = rhs;
39524 else
39525 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
39526 tf_warning_or_error);
39527 }
39528 else
39529 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
39530 ERROR_MARK, NULL, tf_warning_or_error);
39531 }
39532 }
39533 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
39534
39535 if (!decl_first)
39536 {
39537 if ((rhs != decl
39538 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
39539 || op == MINUS_EXPR)
39540 return error_mark_node;
39541 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
39542 }
39543 else
39544 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
39545
39546 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
39547 }
39548
39549 /* Parse the initialization statement of an OpenMP for loop.
39550
39551 Return true if the resulting construct should have an
39552 OMP_CLAUSE_PRIVATE added to it. */
39553
39554 static tree
39555 cp_parser_omp_for_loop_init (cp_parser *parser,
39556 tree &this_pre_body,
39557 releasing_vec &for_block,
39558 tree &init,
39559 tree &orig_init,
39560 tree &decl,
39561 tree &real_decl)
39562 {
39563 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
39564 return NULL_TREE;
39565
39566 tree add_private_clause = NULL_TREE;
39567
39568 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
39569
39570 init-expr:
39571 var = lb
39572 integer-type var = lb
39573 random-access-iterator-type var = lb
39574 pointer-type var = lb
39575 */
39576 cp_decl_specifier_seq type_specifiers;
39577
39578 /* First, try to parse as an initialized declaration. See
39579 cp_parser_condition, from whence the bulk of this is copied. */
39580
39581 cp_parser_parse_tentatively (parser);
39582 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
39583 /*is_declaration=*/true,
39584 /*is_trailing_return=*/false,
39585 &type_specifiers);
39586 if (cp_parser_parse_definitely (parser))
39587 {
39588 /* If parsing a type specifier seq succeeded, then this
39589 MUST be a initialized declaration. */
39590 tree asm_specification, attributes;
39591 cp_declarator *declarator;
39592
39593 declarator = cp_parser_declarator (parser,
39594 CP_PARSER_DECLARATOR_NAMED,
39595 CP_PARSER_FLAGS_NONE,
39596 /*ctor_dtor_or_conv_p=*/NULL,
39597 /*parenthesized_p=*/NULL,
39598 /*member_p=*/false,
39599 /*friend_p=*/false,
39600 /*static_p=*/false);
39601 attributes = cp_parser_attributes_opt (parser);
39602 asm_specification = cp_parser_asm_specification_opt (parser);
39603
39604 if (declarator == cp_error_declarator)
39605 cp_parser_skip_to_end_of_statement (parser);
39606
39607 else
39608 {
39609 tree pushed_scope, auto_node;
39610
39611 decl = start_decl (declarator, &type_specifiers,
39612 SD_INITIALIZED, attributes,
39613 /*prefix_attributes=*/NULL_TREE,
39614 &pushed_scope);
39615
39616 auto_node = type_uses_auto (TREE_TYPE (decl));
39617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
39618 {
39619 if (cp_lexer_next_token_is (parser->lexer,
39620 CPP_OPEN_PAREN))
39621 error ("parenthesized initialization is not allowed in "
39622 "OpenMP %<for%> loop");
39623 else
39624 /* Trigger an error. */
39625 cp_parser_require (parser, CPP_EQ, RT_EQ);
39626
39627 init = error_mark_node;
39628 cp_parser_skip_to_end_of_statement (parser);
39629 }
39630 else if (CLASS_TYPE_P (TREE_TYPE (decl))
39631 || type_dependent_expression_p (decl)
39632 || auto_node)
39633 {
39634 bool is_direct_init, is_non_constant_init;
39635
39636 init = cp_parser_initializer (parser,
39637 &is_direct_init,
39638 &is_non_constant_init);
39639
39640 if (auto_node)
39641 {
39642 TREE_TYPE (decl)
39643 = do_auto_deduction (TREE_TYPE (decl), init,
39644 auto_node);
39645
39646 if (!CLASS_TYPE_P (TREE_TYPE (decl))
39647 && !type_dependent_expression_p (decl))
39648 goto non_class;
39649 }
39650
39651 cp_finish_decl (decl, init, !is_non_constant_init,
39652 asm_specification,
39653 LOOKUP_ONLYCONVERTING);
39654 orig_init = init;
39655 if (CLASS_TYPE_P (TREE_TYPE (decl)))
39656 {
39657 vec_safe_push (for_block, this_pre_body);
39658 init = NULL_TREE;
39659 }
39660 else
39661 {
39662 init = pop_stmt_list (this_pre_body);
39663 if (init && TREE_CODE (init) == STATEMENT_LIST)
39664 {
39665 tree_stmt_iterator i = tsi_start (init);
39666 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
39667 while (!tsi_end_p (i))
39668 {
39669 tree t = tsi_stmt (i);
39670 if (TREE_CODE (t) == DECL_EXPR
39671 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
39672 {
39673 tsi_delink (&i);
39674 vec_safe_push (for_block, t);
39675 continue;
39676 }
39677 break;
39678 }
39679 if (tsi_one_before_end_p (i))
39680 {
39681 tree t = tsi_stmt (i);
39682 tsi_delink (&i);
39683 free_stmt_list (init);
39684 init = t;
39685 }
39686 }
39687 }
39688 this_pre_body = NULL_TREE;
39689 }
39690 else
39691 {
39692 /* Consume '='. */
39693 cp_lexer_consume_token (parser->lexer);
39694 init = cp_parser_assignment_expression (parser);
39695
39696 non_class:
39697 if (TYPE_REF_P (TREE_TYPE (decl)))
39698 init = error_mark_node;
39699 else
39700 cp_finish_decl (decl, NULL_TREE,
39701 /*init_const_expr_p=*/false,
39702 asm_specification,
39703 LOOKUP_ONLYCONVERTING);
39704 }
39705
39706 if (pushed_scope)
39707 pop_scope (pushed_scope);
39708 }
39709 }
39710 else
39711 {
39712 cp_id_kind idk;
39713 /* If parsing a type specifier sequence failed, then
39714 this MUST be a simple expression. */
39715 cp_parser_parse_tentatively (parser);
39716 decl = cp_parser_primary_expression (parser, false, false,
39717 false, &idk);
39718 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
39719 if (!cp_parser_error_occurred (parser)
39720 && decl
39721 && (TREE_CODE (decl) == COMPONENT_REF
39722 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
39723 {
39724 cp_parser_abort_tentative_parse (parser);
39725 cp_parser_parse_tentatively (parser);
39726 cp_token *token = cp_lexer_peek_token (parser->lexer);
39727 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
39728 /*check_dependency_p=*/true,
39729 /*template_p=*/NULL,
39730 /*declarator_p=*/false,
39731 /*optional_p=*/false);
39732 if (name != error_mark_node
39733 && last_tok == cp_lexer_peek_token (parser->lexer))
39734 {
39735 decl = cp_parser_lookup_name_simple (parser, name,
39736 token->location);
39737 if (TREE_CODE (decl) == FIELD_DECL)
39738 add_private_clause = omp_privatize_field (decl, false);
39739 }
39740 cp_parser_abort_tentative_parse (parser);
39741 cp_parser_parse_tentatively (parser);
39742 decl = cp_parser_primary_expression (parser, false, false,
39743 false, &idk);
39744 }
39745 if (!cp_parser_error_occurred (parser)
39746 && decl
39747 && DECL_P (decl)
39748 && CLASS_TYPE_P (TREE_TYPE (decl)))
39749 {
39750 tree rhs;
39751
39752 cp_parser_parse_definitely (parser);
39753 cp_parser_require (parser, CPP_EQ, RT_EQ);
39754 rhs = cp_parser_assignment_expression (parser);
39755 orig_init = rhs;
39756 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
39757 decl, NOP_EXPR,
39758 rhs,
39759 tf_warning_or_error));
39760 if (!add_private_clause)
39761 add_private_clause = decl;
39762 }
39763 else
39764 {
39765 decl = NULL;
39766 cp_parser_abort_tentative_parse (parser);
39767 init = cp_parser_expression (parser);
39768 if (init)
39769 {
39770 if (TREE_CODE (init) == MODIFY_EXPR
39771 || TREE_CODE (init) == MODOP_EXPR)
39772 real_decl = TREE_OPERAND (init, 0);
39773 }
39774 }
39775 }
39776 return add_private_clause;
39777 }
39778
39779 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
39780
39781 void
39782 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
39783 tree &decl, tree &orig_decl, tree &init,
39784 tree &orig_init, tree &cond, tree &incr)
39785 {
39786 tree begin, end, range_temp_decl = NULL_TREE;
39787 tree iter_type, begin_expr, end_expr;
39788
39789 if (processing_template_decl)
39790 {
39791 if (check_for_bare_parameter_packs (init))
39792 init = error_mark_node;
39793 if (!type_dependent_expression_p (init)
39794 /* do_auto_deduction doesn't mess with template init-lists. */
39795 && !BRACE_ENCLOSED_INITIALIZER_P (init))
39796 {
39797 tree d = decl;
39798 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
39799 {
39800 tree v = DECL_VALUE_EXPR (decl);
39801 if (TREE_CODE (v) == ARRAY_REF
39802 && VAR_P (TREE_OPERAND (v, 0))
39803 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
39804 d = TREE_OPERAND (v, 0);
39805 }
39806 do_range_for_auto_deduction (d, init);
39807 }
39808 cond = global_namespace;
39809 incr = NULL_TREE;
39810 orig_init = init;
39811 if (this_pre_body)
39812 this_pre_body = pop_stmt_list (this_pre_body);
39813 return;
39814 }
39815
39816 init = mark_lvalue_use (init);
39817
39818 if (decl == error_mark_node || init == error_mark_node)
39819 /* If an error happened previously do nothing or else a lot of
39820 unhelpful errors would be issued. */
39821 begin_expr = end_expr = iter_type = error_mark_node;
39822 else
39823 {
39824 tree range_temp;
39825
39826 if (VAR_P (init)
39827 && array_of_runtime_bound_p (TREE_TYPE (init)))
39828 /* Can't bind a reference to an array of runtime bound. */
39829 range_temp = init;
39830 else
39831 {
39832 range_temp = build_range_temp (init);
39833 DECL_NAME (range_temp) = NULL_TREE;
39834 pushdecl (range_temp);
39835 cp_finish_decl (range_temp, init,
39836 /*is_constant_init*/false, NULL_TREE,
39837 LOOKUP_ONLYCONVERTING);
39838 range_temp_decl = range_temp;
39839 range_temp = convert_from_reference (range_temp);
39840 }
39841 iter_type = cp_parser_perform_range_for_lookup (range_temp,
39842 &begin_expr, &end_expr);
39843 }
39844
39845 tree end_iter_type = iter_type;
39846 if (cxx_dialect >= cxx17)
39847 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
39848 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
39849 TREE_USED (end) = 1;
39850 DECL_ARTIFICIAL (end) = 1;
39851 pushdecl (end);
39852 cp_finish_decl (end, end_expr,
39853 /*is_constant_init*/false, NULL_TREE,
39854 LOOKUP_ONLYCONVERTING);
39855
39856 /* The new for initialization statement. */
39857 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
39858 TREE_USED (begin) = 1;
39859 DECL_ARTIFICIAL (begin) = 1;
39860 pushdecl (begin);
39861 orig_init = init;
39862 if (CLASS_TYPE_P (iter_type))
39863 init = NULL_TREE;
39864 else
39865 {
39866 init = begin_expr;
39867 begin_expr = NULL_TREE;
39868 }
39869 cp_finish_decl (begin, begin_expr,
39870 /*is_constant_init*/false, NULL_TREE,
39871 LOOKUP_ONLYCONVERTING);
39872
39873 /* The new for condition. */
39874 if (CLASS_TYPE_P (iter_type))
39875 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
39876 else
39877 cond = build_x_binary_op (input_location, NE_EXPR,
39878 begin, ERROR_MARK,
39879 end, ERROR_MARK,
39880 NULL, tf_warning_or_error);
39881
39882 /* The new increment expression. */
39883 if (CLASS_TYPE_P (iter_type))
39884 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
39885 else
39886 incr = finish_unary_op_expr (input_location,
39887 PREINCREMENT_EXPR, begin,
39888 tf_warning_or_error);
39889
39890 orig_decl = decl;
39891 decl = begin;
39892 if (for_block)
39893 {
39894 vec_safe_push (for_block, this_pre_body);
39895 this_pre_body = NULL_TREE;
39896 }
39897
39898 tree decomp_first_name = NULL_TREE;
39899 unsigned decomp_cnt = 0;
39900 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
39901 {
39902 tree v = DECL_VALUE_EXPR (orig_decl);
39903 if (TREE_CODE (v) == ARRAY_REF
39904 && VAR_P (TREE_OPERAND (v, 0))
39905 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
39906 {
39907 tree d = orig_decl;
39908 orig_decl = TREE_OPERAND (v, 0);
39909 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
39910 decomp_first_name = d;
39911 }
39912 }
39913
39914 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
39915 if (auto_node)
39916 {
39917 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
39918 tf_none);
39919 if (!error_operand_p (t))
39920 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
39921 t, auto_node);
39922 }
39923
39924 tree v = make_tree_vec (decomp_cnt + 3);
39925 TREE_VEC_ELT (v, 0) = range_temp_decl;
39926 TREE_VEC_ELT (v, 1) = end;
39927 TREE_VEC_ELT (v, 2) = orig_decl;
39928 for (unsigned i = 0; i < decomp_cnt; i++)
39929 {
39930 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
39931 decomp_first_name = DECL_CHAIN (decomp_first_name);
39932 }
39933 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
39934 }
39935
39936 /* Helper for cp_parser_omp_for_loop, finalize part of range for
39937 inside of the collapsed body. */
39938
39939 void
39940 cp_finish_omp_range_for (tree orig, tree begin)
39941 {
39942 gcc_assert (TREE_CODE (orig) == TREE_LIST
39943 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
39944 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
39945 tree decomp_first_name = NULL_TREE;
39946 unsigned int decomp_cnt = 0;
39947
39948 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
39949 {
39950 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
39951 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
39952 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
39953 }
39954
39955 /* The declaration is initialized with *__begin inside the loop body. */
39956 cp_finish_decl (decl,
39957 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
39958 tf_warning_or_error),
39959 /*is_constant_init*/false, NULL_TREE,
39960 LOOKUP_ONLYCONVERTING);
39961 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
39962 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
39963 }
39964
39965 /* OpenMP 5.0:
39966
39967 scan-loop-body:
39968 { structured-block scan-directive structured-block } */
39969
39970 static void
39971 cp_parser_omp_scan_loop_body (cp_parser *parser)
39972 {
39973 tree substmt, clauses = NULL_TREE;
39974
39975 matching_braces braces;
39976 if (!braces.require_open (parser))
39977 return;
39978
39979 substmt = cp_parser_omp_structured_block (parser, NULL);
39980 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
39981 add_stmt (substmt);
39982
39983 cp_token *tok = cp_lexer_peek_token (parser->lexer);
39984 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
39985 {
39986 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
39987
39988 cp_lexer_consume_token (parser->lexer);
39989
39990 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39991 {
39992 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39993 const char *p = IDENTIFIER_POINTER (id);
39994 if (strcmp (p, "inclusive") == 0)
39995 clause = OMP_CLAUSE_INCLUSIVE;
39996 else if (strcmp (p, "exclusive") == 0)
39997 clause = OMP_CLAUSE_EXCLUSIVE;
39998 }
39999 if (clause != OMP_CLAUSE_ERROR)
40000 {
40001 cp_lexer_consume_token (parser->lexer);
40002 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
40003 }
40004 else
40005 cp_parser_error (parser, "expected %<inclusive%> or "
40006 "%<exclusive%> clause");
40007
40008 cp_parser_require_pragma_eol (parser, tok);
40009 }
40010 else
40011 error ("expected %<#pragma omp scan%>");
40012
40013 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
40014 substmt = cp_parser_omp_structured_block (parser, NULL);
40015 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
40016 clauses);
40017 add_stmt (substmt);
40018
40019 braces.require_close (parser);
40020 }
40021
40022 /* Parse the restricted form of the for statement allowed by OpenMP. */
40023
40024 static tree
40025 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
40026 tree *cclauses, bool *if_p)
40027 {
40028 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
40029 tree orig_decl;
40030 tree real_decl, initv, condv, incrv, declv, orig_declv;
40031 tree this_pre_body, cl, ordered_cl = NULL_TREE;
40032 location_t loc_first;
40033 bool collapse_err = false;
40034 int i, collapse = 1, ordered = 0, count, nbraces = 0;
40035 releasing_vec for_block;
40036 auto_vec<tree, 4> orig_inits;
40037 bool tiling = false;
40038 bool inscan = false;
40039
40040 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
40041 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
40042 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
40043 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
40044 {
40045 tiling = true;
40046 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
40047 }
40048 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
40049 && OMP_CLAUSE_ORDERED_EXPR (cl))
40050 {
40051 ordered_cl = cl;
40052 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
40053 }
40054 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
40055 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
40056 && (code == OMP_SIMD || code == OMP_FOR))
40057 inscan = true;
40058
40059 if (ordered && ordered < collapse)
40060 {
40061 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
40062 "%<ordered%> clause parameter is less than %<collapse%>");
40063 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
40064 = build_int_cst (NULL_TREE, collapse);
40065 ordered = collapse;
40066 }
40067 if (ordered)
40068 {
40069 for (tree *pc = &clauses; *pc; )
40070 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
40071 {
40072 error_at (OMP_CLAUSE_LOCATION (*pc),
40073 "%<linear%> clause may not be specified together "
40074 "with %<ordered%> clause with a parameter");
40075 *pc = OMP_CLAUSE_CHAIN (*pc);
40076 }
40077 else
40078 pc = &OMP_CLAUSE_CHAIN (*pc);
40079 }
40080
40081 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
40082 count = ordered ? ordered : collapse;
40083
40084 declv = make_tree_vec (count);
40085 initv = make_tree_vec (count);
40086 condv = make_tree_vec (count);
40087 incrv = make_tree_vec (count);
40088 orig_declv = NULL_TREE;
40089
40090 loc_first = cp_lexer_peek_token (parser->lexer)->location;
40091
40092 for (i = 0; i < count; i++)
40093 {
40094 int bracecount = 0;
40095 tree add_private_clause = NULL_TREE;
40096 location_t loc;
40097
40098 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
40099 {
40100 if (!collapse_err)
40101 cp_parser_error (parser, "for statement expected");
40102 return NULL;
40103 }
40104 loc = cp_lexer_consume_token (parser->lexer)->location;
40105
40106 /* Don't create location wrapper nodes within an OpenMP "for"
40107 statement. */
40108 auto_suppress_location_wrappers sentinel;
40109
40110 matching_parens parens;
40111 if (!parens.require_open (parser))
40112 return NULL;
40113
40114 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
40115 this_pre_body = push_stmt_list ();
40116
40117 if (code != OACC_LOOP && cxx_dialect >= cxx11)
40118 {
40119 /* Save tokens so that we can put them back. */
40120 cp_lexer_save_tokens (parser->lexer);
40121
40122 /* Look for ':' that is not nested in () or {}. */
40123 bool is_range_for
40124 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
40125 /*recovering=*/false,
40126 CPP_COLON,
40127 /*consume_paren=*/
40128 false) == -1);
40129
40130 /* Roll back the tokens we skipped. */
40131 cp_lexer_rollback_tokens (parser->lexer);
40132
40133 if (is_range_for)
40134 {
40135 bool saved_colon_corrects_to_scope_p
40136 = parser->colon_corrects_to_scope_p;
40137
40138 /* A colon is used in range-based for. */
40139 parser->colon_corrects_to_scope_p = false;
40140
40141 /* Parse the declaration. */
40142 cp_parser_simple_declaration (parser,
40143 /*function_definition_allowed_p=*/
40144 false, &decl);
40145 parser->colon_corrects_to_scope_p
40146 = saved_colon_corrects_to_scope_p;
40147
40148 cp_parser_require (parser, CPP_COLON, RT_COLON);
40149
40150 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
40151 false, 0, true);
40152
40153 cp_convert_omp_range_for (this_pre_body, for_block, decl,
40154 orig_decl, init, orig_init,
40155 cond, incr);
40156 if (this_pre_body)
40157 {
40158 if (pre_body)
40159 {
40160 tree t = pre_body;
40161 pre_body = push_stmt_list ();
40162 add_stmt (t);
40163 add_stmt (this_pre_body);
40164 pre_body = pop_stmt_list (pre_body);
40165 }
40166 else
40167 pre_body = this_pre_body;
40168 }
40169
40170 if (ordered_cl)
40171 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
40172 "%<ordered%> clause with parameter on "
40173 "range-based %<for%> loop");
40174
40175 goto parse_close_paren;
40176 }
40177 }
40178
40179 add_private_clause
40180 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
40181 init, orig_init, decl, real_decl);
40182
40183 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40184 if (this_pre_body)
40185 {
40186 this_pre_body = pop_stmt_list (this_pre_body);
40187 if (pre_body)
40188 {
40189 tree t = pre_body;
40190 pre_body = push_stmt_list ();
40191 add_stmt (t);
40192 add_stmt (this_pre_body);
40193 pre_body = pop_stmt_list (pre_body);
40194 }
40195 else
40196 pre_body = this_pre_body;
40197 }
40198
40199 if (decl)
40200 real_decl = decl;
40201 if (cclauses != NULL
40202 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
40203 && real_decl != NULL_TREE
40204 && code != OMP_LOOP)
40205 {
40206 tree *c;
40207 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
40208 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
40209 && OMP_CLAUSE_DECL (*c) == real_decl)
40210 {
40211 error_at (loc, "iteration variable %qD"
40212 " should not be firstprivate", real_decl);
40213 *c = OMP_CLAUSE_CHAIN (*c);
40214 }
40215 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
40216 && OMP_CLAUSE_DECL (*c) == real_decl)
40217 {
40218 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
40219 tree l = *c;
40220 *c = OMP_CLAUSE_CHAIN (*c);
40221 if (code == OMP_SIMD)
40222 {
40223 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
40224 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
40225 }
40226 else
40227 {
40228 OMP_CLAUSE_CHAIN (l) = clauses;
40229 clauses = l;
40230 }
40231 add_private_clause = NULL_TREE;
40232 }
40233 else
40234 {
40235 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
40236 && OMP_CLAUSE_DECL (*c) == real_decl)
40237 add_private_clause = NULL_TREE;
40238 c = &OMP_CLAUSE_CHAIN (*c);
40239 }
40240 }
40241
40242 if (add_private_clause)
40243 {
40244 tree c;
40245 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
40246 {
40247 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
40248 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
40249 && OMP_CLAUSE_DECL (c) == decl)
40250 break;
40251 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
40252 && OMP_CLAUSE_DECL (c) == decl)
40253 error_at (loc, "iteration variable %qD "
40254 "should not be firstprivate",
40255 decl);
40256 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
40257 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
40258 && OMP_CLAUSE_DECL (c) == decl)
40259 error_at (loc, "iteration variable %qD should not be reduction",
40260 decl);
40261 }
40262 if (c == NULL)
40263 {
40264 if ((code == OMP_SIMD && collapse != 1) || code == OMP_LOOP)
40265 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
40266 else if (code != OMP_SIMD)
40267 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
40268 else
40269 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
40270 OMP_CLAUSE_DECL (c) = add_private_clause;
40271 c = finish_omp_clauses (c, C_ORT_OMP);
40272 if (c)
40273 {
40274 OMP_CLAUSE_CHAIN (c) = clauses;
40275 clauses = c;
40276 /* For linear, signal that we need to fill up
40277 the so far unknown linear step. */
40278 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
40279 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
40280 }
40281 }
40282 }
40283
40284 cond = NULL;
40285 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
40286 cond = cp_parser_omp_for_cond (parser, decl, code);
40287 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40288
40289 incr = NULL;
40290 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
40291 {
40292 /* If decl is an iterator, preserve the operator on decl
40293 until finish_omp_for. */
40294 if (real_decl
40295 && ((processing_template_decl
40296 && (TREE_TYPE (real_decl) == NULL_TREE
40297 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
40298 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
40299 incr = cp_parser_omp_for_incr (parser, real_decl);
40300 else
40301 incr = cp_parser_expression (parser);
40302 protected_set_expr_location_if_unset (incr, input_location);
40303 }
40304
40305 parse_close_paren:
40306 if (!parens.require_close (parser))
40307 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40308 /*or_comma=*/false,
40309 /*consume_paren=*/true);
40310
40311 TREE_VEC_ELT (declv, i) = decl;
40312 TREE_VEC_ELT (initv, i) = init;
40313 TREE_VEC_ELT (condv, i) = cond;
40314 TREE_VEC_ELT (incrv, i) = incr;
40315 if (orig_init)
40316 {
40317 orig_inits.safe_grow_cleared (i + 1, true);
40318 orig_inits[i] = orig_init;
40319 }
40320 if (orig_decl)
40321 {
40322 if (!orig_declv)
40323 orig_declv = copy_node (declv);
40324 TREE_VEC_ELT (orig_declv, i) = orig_decl;
40325 }
40326 else if (orig_declv)
40327 TREE_VEC_ELT (orig_declv, i) = decl;
40328
40329 if (i == count - 1)
40330 break;
40331
40332 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
40333 in between the collapsed for loops to be still considered perfectly
40334 nested. Hopefully the final version clarifies this.
40335 For now handle (multiple) {'s and empty statements. */
40336 cp_parser_parse_tentatively (parser);
40337 for (;;)
40338 {
40339 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
40340 break;
40341 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
40342 {
40343 cp_lexer_consume_token (parser->lexer);
40344 bracecount++;
40345 }
40346 else if (bracecount
40347 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
40348 cp_lexer_consume_token (parser->lexer);
40349 else
40350 {
40351 loc = cp_lexer_peek_token (parser->lexer)->location;
40352 error_at (loc, "not enough for loops to collapse");
40353 collapse_err = true;
40354 cp_parser_abort_tentative_parse (parser);
40355 declv = NULL_TREE;
40356 break;
40357 }
40358 }
40359
40360 if (declv)
40361 {
40362 cp_parser_parse_definitely (parser);
40363 nbraces += bracecount;
40364 }
40365 }
40366
40367 if (nbraces)
40368 if_p = NULL;
40369
40370 /* Note that we saved the original contents of this flag when we entered
40371 the structured block, and so we don't need to re-save it here. */
40372 parser->in_statement = IN_OMP_FOR;
40373
40374 /* Note that the grammar doesn't call for a structured block here,
40375 though the loop as a whole is a structured block. */
40376 if (orig_declv)
40377 {
40378 body = begin_omp_structured_block ();
40379 for (i = 0; i < count; i++)
40380 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
40381 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
40382 TREE_VEC_ELT (declv, i));
40383 }
40384 else
40385 body = push_stmt_list ();
40386 if (inscan)
40387 cp_parser_omp_scan_loop_body (parser);
40388 else
40389 cp_parser_statement (parser, NULL_TREE, false, if_p);
40390 if (orig_declv)
40391 body = finish_omp_structured_block (body);
40392 else
40393 body = pop_stmt_list (body);
40394
40395 if (declv == NULL_TREE)
40396 ret = NULL_TREE;
40397 else
40398 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
40399 incrv, body, pre_body, &orig_inits, clauses);
40400
40401 while (nbraces)
40402 {
40403 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
40404 {
40405 cp_lexer_consume_token (parser->lexer);
40406 nbraces--;
40407 }
40408 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
40409 cp_lexer_consume_token (parser->lexer);
40410 else
40411 {
40412 if (!collapse_err)
40413 {
40414 error_at (cp_lexer_peek_token (parser->lexer)->location,
40415 "collapsed loops not perfectly nested");
40416 }
40417 collapse_err = true;
40418 cp_parser_statement_seq_opt (parser, NULL);
40419 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
40420 break;
40421 }
40422 }
40423
40424 while (!for_block->is_empty ())
40425 {
40426 tree t = for_block->pop ();
40427 if (TREE_CODE (t) == STATEMENT_LIST)
40428 add_stmt (pop_stmt_list (t));
40429 else
40430 add_stmt (t);
40431 }
40432
40433 return ret;
40434 }
40435
40436 /* Helper function for OpenMP parsing, split clauses and call
40437 finish_omp_clauses on each of the set of clauses afterwards. */
40438
40439 static void
40440 cp_omp_split_clauses (location_t loc, enum tree_code code,
40441 omp_clause_mask mask, tree clauses, tree *cclauses)
40442 {
40443 int i;
40444 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
40445 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
40446 if (cclauses[i])
40447 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
40448 }
40449
40450 /* OpenMP 5.0:
40451 #pragma omp loop loop-clause[optseq] new-line
40452 for-loop */
40453
40454 #define OMP_LOOP_CLAUSE_MASK \
40455 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
40459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
40460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
40461
40462 static tree
40463 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
40464 char *p_name, omp_clause_mask mask, tree *cclauses,
40465 bool *if_p)
40466 {
40467 tree clauses, sb, ret;
40468 unsigned int save;
40469 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40470
40471 strcat (p_name, " loop");
40472 mask |= OMP_LOOP_CLAUSE_MASK;
40473
40474 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40475 cclauses == NULL);
40476 if (cclauses)
40477 {
40478 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
40479 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
40480 }
40481
40482 keep_next_level (true);
40483 sb = begin_omp_structured_block ();
40484 save = cp_parser_begin_omp_structured_block (parser);
40485
40486 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
40487
40488 cp_parser_end_omp_structured_block (parser, save);
40489 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
40490
40491 return ret;
40492 }
40493
40494 /* OpenMP 4.0:
40495 #pragma omp simd simd-clause[optseq] new-line
40496 for-loop */
40497
40498 #define OMP_SIMD_CLAUSE_MASK \
40499 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
40500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
40501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
40502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
40503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
40507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
40509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
40510
40511 static tree
40512 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
40513 char *p_name, omp_clause_mask mask, tree *cclauses,
40514 bool *if_p)
40515 {
40516 tree clauses, sb, ret;
40517 unsigned int save;
40518 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40519
40520 strcat (p_name, " simd");
40521 mask |= OMP_SIMD_CLAUSE_MASK;
40522
40523 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40524 cclauses == NULL);
40525 if (cclauses)
40526 {
40527 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
40528 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
40529 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
40530 OMP_CLAUSE_ORDERED);
40531 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
40532 {
40533 error_at (OMP_CLAUSE_LOCATION (c),
40534 "%<ordered%> clause with parameter may not be specified "
40535 "on %qs construct", p_name);
40536 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
40537 }
40538 }
40539
40540 keep_next_level (true);
40541 sb = begin_omp_structured_block ();
40542 save = cp_parser_begin_omp_structured_block (parser);
40543
40544 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
40545
40546 cp_parser_end_omp_structured_block (parser, save);
40547 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
40548
40549 return ret;
40550 }
40551
40552 /* OpenMP 2.5:
40553 #pragma omp for for-clause[optseq] new-line
40554 for-loop
40555
40556 OpenMP 4.0:
40557 #pragma omp for simd for-simd-clause[optseq] new-line
40558 for-loop */
40559
40560 #define OMP_FOR_CLAUSE_MASK \
40561 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
40565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
40567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
40568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
40569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
40570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
40572
40573 static tree
40574 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
40575 char *p_name, omp_clause_mask mask, tree *cclauses,
40576 bool *if_p)
40577 {
40578 tree clauses, sb, ret;
40579 unsigned int save;
40580 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40581
40582 strcat (p_name, " for");
40583 mask |= OMP_FOR_CLAUSE_MASK;
40584 /* parallel for{, simd} disallows nowait clause, but for
40585 target {teams distribute ,}parallel for{, simd} it should be accepted. */
40586 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
40587 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
40588 /* Composite distribute parallel for{, simd} disallows ordered clause. */
40589 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
40590 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
40591
40592 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40593 {
40594 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40595 const char *p = IDENTIFIER_POINTER (id);
40596
40597 if (strcmp (p, "simd") == 0)
40598 {
40599 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40600 if (cclauses == NULL)
40601 cclauses = cclauses_buf;
40602
40603 cp_lexer_consume_token (parser->lexer);
40604 if (!flag_openmp) /* flag_openmp_simd */
40605 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40606 cclauses, if_p);
40607 sb = begin_omp_structured_block ();
40608 save = cp_parser_begin_omp_structured_block (parser);
40609 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40610 cclauses, if_p);
40611 cp_parser_end_omp_structured_block (parser, save);
40612 tree body = finish_omp_structured_block (sb);
40613 if (ret == NULL)
40614 return ret;
40615 ret = make_node (OMP_FOR);
40616 TREE_TYPE (ret) = void_type_node;
40617 OMP_FOR_BODY (ret) = body;
40618 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
40619 SET_EXPR_LOCATION (ret, loc);
40620 add_stmt (ret);
40621 return ret;
40622 }
40623 }
40624 if (!flag_openmp) /* flag_openmp_simd */
40625 {
40626 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40627 return NULL_TREE;
40628 }
40629
40630 /* Composite distribute parallel for disallows linear clause. */
40631 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
40632 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
40633
40634 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40635 cclauses == NULL);
40636 if (cclauses)
40637 {
40638 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
40639 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
40640 }
40641
40642 keep_next_level (true);
40643 sb = begin_omp_structured_block ();
40644 save = cp_parser_begin_omp_structured_block (parser);
40645
40646 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
40647
40648 cp_parser_end_omp_structured_block (parser, save);
40649 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
40650
40651 return ret;
40652 }
40653
40654 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
40655 omp_clause_mask, tree *, bool *);
40656
40657 /* OpenMP 2.5:
40658 # pragma omp master new-line
40659 structured-block */
40660
40661 static tree
40662 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
40663 char *p_name, omp_clause_mask mask, tree *cclauses,
40664 bool *if_p)
40665 {
40666 tree clauses, sb, ret;
40667 unsigned int save;
40668 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40669
40670 strcat (p_name, " master");
40671
40672 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40673 {
40674 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40675 const char *p = IDENTIFIER_POINTER (id);
40676
40677 if (strcmp (p, "taskloop") == 0)
40678 {
40679 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40680 if (cclauses == NULL)
40681 cclauses = cclauses_buf;
40682
40683 cp_lexer_consume_token (parser->lexer);
40684 if (!flag_openmp) /* flag_openmp_simd */
40685 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
40686 cclauses, if_p);
40687 sb = begin_omp_structured_block ();
40688 save = cp_parser_begin_omp_structured_block (parser);
40689 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
40690 cclauses, if_p);
40691 cp_parser_end_omp_structured_block (parser, save);
40692 tree body = finish_omp_structured_block (sb);
40693 if (ret == NULL)
40694 return ret;
40695 return c_finish_omp_master (loc, body);
40696 }
40697 }
40698 if (!flag_openmp) /* flag_openmp_simd */
40699 {
40700 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40701 return NULL_TREE;
40702 }
40703
40704 if (cclauses)
40705 {
40706 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40707 false);
40708 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
40709 }
40710 else
40711 cp_parser_require_pragma_eol (parser, pragma_tok);
40712
40713 return c_finish_omp_master (loc,
40714 cp_parser_omp_structured_block (parser, if_p));
40715 }
40716
40717 /* OpenMP 2.5:
40718 # pragma omp ordered new-line
40719 structured-block
40720
40721 OpenMP 4.5:
40722 # pragma omp ordered ordered-clauses new-line
40723 structured-block */
40724
40725 #define OMP_ORDERED_CLAUSE_MASK \
40726 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
40727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
40728
40729 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
40730 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
40731
40732 static bool
40733 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
40734 enum pragma_context context, bool *if_p)
40735 {
40736 location_t loc = pragma_tok->location;
40737
40738 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40739 {
40740 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40741 const char *p = IDENTIFIER_POINTER (id);
40742
40743 if (strcmp (p, "depend") == 0)
40744 {
40745 if (!flag_openmp) /* flag_openmp_simd */
40746 {
40747 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40748 return false;
40749 }
40750 if (context == pragma_stmt)
40751 {
40752 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
40753 "%<depend%> clause may only be used in compound "
40754 "statements");
40755 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40756 return false;
40757 }
40758 tree clauses
40759 = cp_parser_omp_all_clauses (parser,
40760 OMP_ORDERED_DEPEND_CLAUSE_MASK,
40761 "#pragma omp ordered", pragma_tok);
40762 c_finish_omp_ordered (loc, clauses, NULL_TREE);
40763 return false;
40764 }
40765 }
40766
40767 tree clauses
40768 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
40769 "#pragma omp ordered", pragma_tok);
40770
40771 if (!flag_openmp /* flag_openmp_simd */
40772 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
40773 return false;
40774
40775 c_finish_omp_ordered (loc, clauses,
40776 cp_parser_omp_structured_block (parser, if_p));
40777 return true;
40778 }
40779
40780 /* OpenMP 2.5:
40781
40782 section-scope:
40783 { section-sequence }
40784
40785 section-sequence:
40786 section-directive[opt] structured-block
40787 section-sequence section-directive structured-block */
40788
40789 static tree
40790 cp_parser_omp_sections_scope (cp_parser *parser)
40791 {
40792 tree stmt, substmt;
40793 bool error_suppress = false;
40794 cp_token *tok;
40795
40796 matching_braces braces;
40797 if (!braces.require_open (parser))
40798 return NULL_TREE;
40799
40800 stmt = push_stmt_list ();
40801
40802 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
40803 != PRAGMA_OMP_SECTION)
40804 {
40805 substmt = cp_parser_omp_structured_block (parser, NULL);
40806 substmt = build1 (OMP_SECTION, void_type_node, substmt);
40807 add_stmt (substmt);
40808 }
40809
40810 while (1)
40811 {
40812 tok = cp_lexer_peek_token (parser->lexer);
40813 if (tok->type == CPP_CLOSE_BRACE)
40814 break;
40815 if (tok->type == CPP_EOF)
40816 break;
40817
40818 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
40819 {
40820 cp_lexer_consume_token (parser->lexer);
40821 cp_parser_require_pragma_eol (parser, tok);
40822 error_suppress = false;
40823 }
40824 else if (!error_suppress)
40825 {
40826 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
40827 error_suppress = true;
40828 }
40829
40830 substmt = cp_parser_omp_structured_block (parser, NULL);
40831 substmt = build1 (OMP_SECTION, void_type_node, substmt);
40832 add_stmt (substmt);
40833 }
40834 braces.require_close (parser);
40835
40836 substmt = pop_stmt_list (stmt);
40837
40838 stmt = make_node (OMP_SECTIONS);
40839 TREE_TYPE (stmt) = void_type_node;
40840 OMP_SECTIONS_BODY (stmt) = substmt;
40841
40842 add_stmt (stmt);
40843 return stmt;
40844 }
40845
40846 /* OpenMP 2.5:
40847 # pragma omp sections sections-clause[optseq] newline
40848 sections-scope */
40849
40850 #define OMP_SECTIONS_CLAUSE_MASK \
40851 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40857
40858 static tree
40859 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
40860 char *p_name, omp_clause_mask mask, tree *cclauses)
40861 {
40862 tree clauses, ret;
40863 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40864
40865 strcat (p_name, " sections");
40866 mask |= OMP_SECTIONS_CLAUSE_MASK;
40867 if (cclauses)
40868 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
40869
40870 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40871 cclauses == NULL);
40872 if (cclauses)
40873 {
40874 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
40875 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
40876 }
40877
40878 ret = cp_parser_omp_sections_scope (parser);
40879 if (ret)
40880 OMP_SECTIONS_CLAUSES (ret) = clauses;
40881
40882 return ret;
40883 }
40884
40885 /* OpenMP 2.5:
40886 # pragma omp parallel parallel-clause[optseq] new-line
40887 structured-block
40888 # pragma omp parallel for parallel-for-clause[optseq] new-line
40889 structured-block
40890 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
40891 structured-block
40892
40893 OpenMP 4.0:
40894 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
40895 structured-block */
40896
40897 #define OMP_PARALLEL_CLAUSE_MASK \
40898 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
40902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
40903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
40904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
40906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
40908
40909 static tree
40910 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
40911 char *p_name, omp_clause_mask mask, tree *cclauses,
40912 bool *if_p)
40913 {
40914 tree stmt, clauses, block;
40915 unsigned int save;
40916 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40917
40918 strcat (p_name, " parallel");
40919 mask |= OMP_PARALLEL_CLAUSE_MASK;
40920 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
40921 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
40922 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
40923 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
40924
40925 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
40926 {
40927 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40928 if (cclauses == NULL)
40929 cclauses = cclauses_buf;
40930
40931 cp_lexer_consume_token (parser->lexer);
40932 if (!flag_openmp) /* flag_openmp_simd */
40933 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
40934 if_p);
40935 block = begin_omp_parallel ();
40936 save = cp_parser_begin_omp_structured_block (parser);
40937 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
40938 if_p);
40939 cp_parser_end_omp_structured_block (parser, save);
40940 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
40941 block);
40942 if (ret == NULL_TREE)
40943 return ret;
40944 OMP_PARALLEL_COMBINED (stmt) = 1;
40945 return stmt;
40946 }
40947 /* When combined with distribute, parallel has to be followed by for.
40948 #pragma omp target parallel is allowed though. */
40949 else if (cclauses
40950 && (mask & (OMP_CLAUSE_MASK_1
40951 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
40952 {
40953 error_at (loc, "expected %<for%> after %qs", p_name);
40954 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40955 return NULL_TREE;
40956 }
40957 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40958 {
40959 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40960 const char *p = IDENTIFIER_POINTER (id);
40961 if (cclauses == NULL && strcmp (p, "master") == 0)
40962 {
40963 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40964 cclauses = cclauses_buf;
40965
40966 cp_lexer_consume_token (parser->lexer);
40967 if (!flag_openmp) /* flag_openmp_simd */
40968 return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
40969 cclauses, if_p);
40970 block = begin_omp_parallel ();
40971 save = cp_parser_begin_omp_structured_block (parser);
40972 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
40973 cclauses, if_p);
40974 cp_parser_end_omp_structured_block (parser, save);
40975 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
40976 block);
40977 if (ret == NULL_TREE)
40978 return ret;
40979 OMP_PARALLEL_COMBINED (stmt) = 1;
40980 return stmt;
40981 }
40982 else if (strcmp (p, "loop") == 0)
40983 {
40984 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40985 if (cclauses == NULL)
40986 cclauses = cclauses_buf;
40987
40988 cp_lexer_consume_token (parser->lexer);
40989 if (!flag_openmp) /* flag_openmp_simd */
40990 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
40991 cclauses, if_p);
40992 block = begin_omp_parallel ();
40993 save = cp_parser_begin_omp_structured_block (parser);
40994 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
40995 cclauses, if_p);
40996 cp_parser_end_omp_structured_block (parser, save);
40997 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
40998 block);
40999 if (ret == NULL_TREE)
41000 return ret;
41001 OMP_PARALLEL_COMBINED (stmt) = 1;
41002 return stmt;
41003 }
41004 else if (!flag_openmp) /* flag_openmp_simd */
41005 {
41006 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41007 return NULL_TREE;
41008 }
41009 else if (cclauses == NULL && strcmp (p, "sections") == 0)
41010 {
41011 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
41012 cclauses = cclauses_buf;
41013
41014 cp_lexer_consume_token (parser->lexer);
41015 block = begin_omp_parallel ();
41016 save = cp_parser_begin_omp_structured_block (parser);
41017 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
41018 cp_parser_end_omp_structured_block (parser, save);
41019 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
41020 block);
41021 OMP_PARALLEL_COMBINED (stmt) = 1;
41022 return stmt;
41023 }
41024 }
41025 else if (!flag_openmp) /* flag_openmp_simd */
41026 {
41027 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41028 return NULL_TREE;
41029 }
41030
41031 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
41032 cclauses == NULL);
41033 if (cclauses)
41034 {
41035 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
41036 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
41037 }
41038
41039 block = begin_omp_parallel ();
41040 save = cp_parser_begin_omp_structured_block (parser);
41041 cp_parser_statement (parser, NULL_TREE, false, if_p);
41042 cp_parser_end_omp_structured_block (parser, save);
41043 stmt = finish_omp_parallel (clauses, block);
41044 return stmt;
41045 }
41046
41047 /* OpenMP 2.5:
41048 # pragma omp single single-clause[optseq] new-line
41049 structured-block */
41050
41051 #define OMP_SINGLE_CLAUSE_MASK \
41052 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
41055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
41057
41058 static tree
41059 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41060 {
41061 tree stmt = make_node (OMP_SINGLE);
41062 TREE_TYPE (stmt) = void_type_node;
41063 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41064
41065 OMP_SINGLE_CLAUSES (stmt)
41066 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
41067 "#pragma omp single", pragma_tok);
41068 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
41069
41070 return add_stmt (stmt);
41071 }
41072
41073 /* OpenMP 3.0:
41074 # pragma omp task task-clause[optseq] new-line
41075 structured-block */
41076
41077 #define OMP_TASK_CLAUSE_MASK \
41078 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
41080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
41081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
41084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
41085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
41086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
41088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
41090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH))
41091
41092 static tree
41093 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41094 {
41095 tree clauses, block;
41096 unsigned int save;
41097
41098 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
41099 "#pragma omp task", pragma_tok);
41100 block = begin_omp_task ();
41101 save = cp_parser_begin_omp_structured_block (parser);
41102 cp_parser_statement (parser, NULL_TREE, false, if_p);
41103 cp_parser_end_omp_structured_block (parser, save);
41104 return finish_omp_task (clauses, block);
41105 }
41106
41107 /* OpenMP 3.0:
41108 # pragma omp taskwait new-line
41109
41110 OpenMP 5.0:
41111 # pragma omp taskwait taskwait-clause[opt] new-line */
41112
41113 #define OMP_TASKWAIT_CLAUSE_MASK \
41114 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
41115
41116 static void
41117 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
41118 {
41119 tree clauses
41120 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
41121 "#pragma omp taskwait", pragma_tok);
41122
41123 if (clauses)
41124 {
41125 tree stmt = make_node (OMP_TASK);
41126 TREE_TYPE (stmt) = void_node;
41127 OMP_TASK_CLAUSES (stmt) = clauses;
41128 OMP_TASK_BODY (stmt) = NULL_TREE;
41129 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41130 add_stmt (stmt);
41131 }
41132 else
41133 finish_omp_taskwait ();
41134 }
41135
41136 /* OpenMP 3.1:
41137 # pragma omp taskyield new-line */
41138
41139 static void
41140 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
41141 {
41142 cp_parser_require_pragma_eol (parser, pragma_tok);
41143 finish_omp_taskyield ();
41144 }
41145
41146 /* OpenMP 4.0:
41147 # pragma omp taskgroup new-line
41148 structured-block
41149
41150 OpenMP 5.0:
41151 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
41152
41153 #define OMP_TASKGROUP_CLAUSE_MASK \
41154 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
41156
41157 static tree
41158 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41159 {
41160 tree clauses
41161 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
41162 "#pragma omp taskgroup", pragma_tok);
41163 return c_finish_omp_taskgroup (input_location,
41164 cp_parser_omp_structured_block (parser,
41165 if_p),
41166 clauses);
41167 }
41168
41169
41170 /* OpenMP 2.5:
41171 # pragma omp threadprivate (variable-list) */
41172
41173 static void
41174 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
41175 {
41176 tree vars;
41177
41178 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
41179 cp_parser_require_pragma_eol (parser, pragma_tok);
41180
41181 finish_omp_threadprivate (vars);
41182 }
41183
41184 /* OpenMP 4.0:
41185 # pragma omp cancel cancel-clause[optseq] new-line */
41186
41187 #define OMP_CANCEL_CLAUSE_MASK \
41188 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
41189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
41190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
41191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
41192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
41193
41194 static void
41195 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
41196 {
41197 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
41198 "#pragma omp cancel", pragma_tok);
41199 finish_omp_cancel (clauses);
41200 }
41201
41202 /* OpenMP 4.0:
41203 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
41204
41205 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
41206 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
41207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
41208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
41209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
41210
41211 static void
41212 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
41213 enum pragma_context context)
41214 {
41215 tree clauses;
41216 bool point_seen = false;
41217
41218 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41219 {
41220 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41221 const char *p = IDENTIFIER_POINTER (id);
41222
41223 if (strcmp (p, "point") == 0)
41224 {
41225 cp_lexer_consume_token (parser->lexer);
41226 point_seen = true;
41227 }
41228 }
41229 if (!point_seen)
41230 {
41231 cp_parser_error (parser, "expected %<point%>");
41232 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41233 return;
41234 }
41235
41236 if (context != pragma_compound)
41237 {
41238 if (context == pragma_stmt)
41239 error_at (pragma_tok->location,
41240 "%<#pragma %s%> may only be used in compound statements",
41241 "omp cancellation point");
41242 else
41243 cp_parser_error (parser, "expected declaration specifiers");
41244 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41245 return;
41246 }
41247
41248 clauses = cp_parser_omp_all_clauses (parser,
41249 OMP_CANCELLATION_POINT_CLAUSE_MASK,
41250 "#pragma omp cancellation point",
41251 pragma_tok);
41252 finish_omp_cancellation_point (clauses);
41253 }
41254
41255 /* OpenMP 4.0:
41256 #pragma omp distribute distribute-clause[optseq] new-line
41257 for-loop */
41258
41259 #define OMP_DISTRIBUTE_CLAUSE_MASK \
41260 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
41263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
41264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
41266
41267 static tree
41268 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
41269 char *p_name, omp_clause_mask mask, tree *cclauses,
41270 bool *if_p)
41271 {
41272 tree clauses, sb, ret;
41273 unsigned int save;
41274 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41275
41276 strcat (p_name, " distribute");
41277 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
41278
41279 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41280 {
41281 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41282 const char *p = IDENTIFIER_POINTER (id);
41283 bool simd = false;
41284 bool parallel = false;
41285
41286 if (strcmp (p, "simd") == 0)
41287 simd = true;
41288 else
41289 parallel = strcmp (p, "parallel") == 0;
41290 if (parallel || simd)
41291 {
41292 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
41293 if (cclauses == NULL)
41294 cclauses = cclauses_buf;
41295 cp_lexer_consume_token (parser->lexer);
41296 if (!flag_openmp) /* flag_openmp_simd */
41297 {
41298 if (simd)
41299 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
41300 cclauses, if_p);
41301 else
41302 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
41303 cclauses, if_p);
41304 }
41305 sb = begin_omp_structured_block ();
41306 save = cp_parser_begin_omp_structured_block (parser);
41307 if (simd)
41308 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
41309 cclauses, if_p);
41310 else
41311 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
41312 cclauses, if_p);
41313 cp_parser_end_omp_structured_block (parser, save);
41314 tree body = finish_omp_structured_block (sb);
41315 if (ret == NULL)
41316 return ret;
41317 ret = make_node (OMP_DISTRIBUTE);
41318 TREE_TYPE (ret) = void_type_node;
41319 OMP_FOR_BODY (ret) = body;
41320 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
41321 SET_EXPR_LOCATION (ret, loc);
41322 add_stmt (ret);
41323 return ret;
41324 }
41325 }
41326 if (!flag_openmp) /* flag_openmp_simd */
41327 {
41328 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41329 return NULL_TREE;
41330 }
41331
41332 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
41333 cclauses == NULL);
41334 if (cclauses)
41335 {
41336 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
41337 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
41338 }
41339
41340 keep_next_level (true);
41341 sb = begin_omp_structured_block ();
41342 save = cp_parser_begin_omp_structured_block (parser);
41343
41344 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
41345
41346 cp_parser_end_omp_structured_block (parser, save);
41347 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
41348
41349 return ret;
41350 }
41351
41352 /* OpenMP 4.0:
41353 # pragma omp teams teams-clause[optseq] new-line
41354 structured-block */
41355
41356 #define OMP_TEAMS_CLAUSE_MASK \
41357 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
41360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
41361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
41362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
41363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
41365
41366 static tree
41367 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
41368 char *p_name, omp_clause_mask mask, tree *cclauses,
41369 bool *if_p)
41370 {
41371 tree clauses, sb, ret;
41372 unsigned int save;
41373 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41374
41375 strcat (p_name, " teams");
41376 mask |= OMP_TEAMS_CLAUSE_MASK;
41377
41378 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41379 {
41380 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41381 const char *p = IDENTIFIER_POINTER (id);
41382 if (strcmp (p, "distribute") == 0)
41383 {
41384 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
41385 if (cclauses == NULL)
41386 cclauses = cclauses_buf;
41387
41388 cp_lexer_consume_token (parser->lexer);
41389 if (!flag_openmp) /* flag_openmp_simd */
41390 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
41391 cclauses, if_p);
41392 keep_next_level (true);
41393 sb = begin_omp_structured_block ();
41394 save = cp_parser_begin_omp_structured_block (parser);
41395 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
41396 cclauses, if_p);
41397 cp_parser_end_omp_structured_block (parser, save);
41398 tree body = finish_omp_structured_block (sb);
41399 if (ret == NULL)
41400 return ret;
41401 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
41402 ret = make_node (OMP_TEAMS);
41403 TREE_TYPE (ret) = void_type_node;
41404 OMP_TEAMS_CLAUSES (ret) = clauses;
41405 OMP_TEAMS_BODY (ret) = body;
41406 OMP_TEAMS_COMBINED (ret) = 1;
41407 SET_EXPR_LOCATION (ret, loc);
41408 return add_stmt (ret);
41409 }
41410 else if (strcmp (p, "loop") == 0)
41411 {
41412 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
41413 if (cclauses == NULL)
41414 cclauses = cclauses_buf;
41415
41416 cp_lexer_consume_token (parser->lexer);
41417 if (!flag_openmp) /* flag_openmp_simd */
41418 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
41419 cclauses, if_p);
41420 keep_next_level (true);
41421 sb = begin_omp_structured_block ();
41422 save = cp_parser_begin_omp_structured_block (parser);
41423 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
41424 cclauses, if_p);
41425 cp_parser_end_omp_structured_block (parser, save);
41426 tree body = finish_omp_structured_block (sb);
41427 if (ret == NULL)
41428 return ret;
41429 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
41430 ret = make_node (OMP_TEAMS);
41431 TREE_TYPE (ret) = void_type_node;
41432 OMP_TEAMS_CLAUSES (ret) = clauses;
41433 OMP_TEAMS_BODY (ret) = body;
41434 OMP_TEAMS_COMBINED (ret) = 1;
41435 SET_EXPR_LOCATION (ret, loc);
41436 return add_stmt (ret);
41437 }
41438 }
41439 if (!flag_openmp) /* flag_openmp_simd */
41440 {
41441 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41442 return NULL_TREE;
41443 }
41444
41445 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
41446 cclauses == NULL);
41447 if (cclauses)
41448 {
41449 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
41450 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
41451 }
41452
41453 tree stmt = make_node (OMP_TEAMS);
41454 TREE_TYPE (stmt) = void_type_node;
41455 OMP_TEAMS_CLAUSES (stmt) = clauses;
41456 keep_next_level (true);
41457 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
41458 SET_EXPR_LOCATION (stmt, loc);
41459
41460 return add_stmt (stmt);
41461 }
41462
41463 /* OpenMP 4.0:
41464 # pragma omp target data target-data-clause[optseq] new-line
41465 structured-block */
41466
41467 #define OMP_TARGET_DATA_CLAUSE_MASK \
41468 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
41470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
41472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
41473
41474 static tree
41475 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41476 {
41477 tree clauses
41478 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
41479 "#pragma omp target data", pragma_tok);
41480 c_omp_adjust_map_clauses (clauses, false);
41481 int map_seen = 0;
41482 for (tree *pc = &clauses; *pc;)
41483 {
41484 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
41485 switch (OMP_CLAUSE_MAP_KIND (*pc))
41486 {
41487 case GOMP_MAP_TO:
41488 case GOMP_MAP_ALWAYS_TO:
41489 case GOMP_MAP_FROM:
41490 case GOMP_MAP_ALWAYS_FROM:
41491 case GOMP_MAP_TOFROM:
41492 case GOMP_MAP_ALWAYS_TOFROM:
41493 case GOMP_MAP_ALLOC:
41494 map_seen = 3;
41495 break;
41496 case GOMP_MAP_FIRSTPRIVATE_POINTER:
41497 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
41498 case GOMP_MAP_ALWAYS_POINTER:
41499 case GOMP_MAP_ATTACH_DETACH:
41500 break;
41501 default:
41502 map_seen |= 1;
41503 error_at (OMP_CLAUSE_LOCATION (*pc),
41504 "%<#pragma omp target data%> with map-type other "
41505 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
41506 "on %<map%> clause");
41507 *pc = OMP_CLAUSE_CHAIN (*pc);
41508 continue;
41509 }
41510 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
41511 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
41512 map_seen = 3;
41513 pc = &OMP_CLAUSE_CHAIN (*pc);
41514 }
41515
41516 if (map_seen != 3)
41517 {
41518 if (map_seen == 0)
41519 error_at (pragma_tok->location,
41520 "%<#pragma omp target data%> must contain at least "
41521 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
41522 "clause");
41523 return NULL_TREE;
41524 }
41525
41526 tree stmt = make_node (OMP_TARGET_DATA);
41527 TREE_TYPE (stmt) = void_type_node;
41528 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
41529
41530 keep_next_level (true);
41531 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
41532
41533 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41534 return add_stmt (stmt);
41535 }
41536
41537 /* OpenMP 4.5:
41538 # pragma omp target enter data target-enter-data-clause[optseq] new-line
41539 structured-block */
41540
41541 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
41542 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
41544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
41547
41548 static tree
41549 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
41550 enum pragma_context context)
41551 {
41552 bool data_seen = false;
41553 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41554 {
41555 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41556 const char *p = IDENTIFIER_POINTER (id);
41557
41558 if (strcmp (p, "data") == 0)
41559 {
41560 cp_lexer_consume_token (parser->lexer);
41561 data_seen = true;
41562 }
41563 }
41564 if (!data_seen)
41565 {
41566 cp_parser_error (parser, "expected %<data%>");
41567 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41568 return NULL_TREE;
41569 }
41570
41571 if (context == pragma_stmt)
41572 {
41573 error_at (pragma_tok->location,
41574 "%<#pragma %s%> may only be used in compound statements",
41575 "omp target enter data");
41576 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41577 return NULL_TREE;
41578 }
41579
41580 tree clauses
41581 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
41582 "#pragma omp target enter data", pragma_tok);
41583 c_omp_adjust_map_clauses (clauses, false);
41584 int map_seen = 0;
41585 for (tree *pc = &clauses; *pc;)
41586 {
41587 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
41588 switch (OMP_CLAUSE_MAP_KIND (*pc))
41589 {
41590 case GOMP_MAP_TO:
41591 case GOMP_MAP_ALWAYS_TO:
41592 case GOMP_MAP_ALLOC:
41593 map_seen = 3;
41594 break;
41595 case GOMP_MAP_FIRSTPRIVATE_POINTER:
41596 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
41597 case GOMP_MAP_ALWAYS_POINTER:
41598 case GOMP_MAP_ATTACH_DETACH:
41599 break;
41600 default:
41601 map_seen |= 1;
41602 error_at (OMP_CLAUSE_LOCATION (*pc),
41603 "%<#pragma omp target enter data%> with map-type other "
41604 "than %<to%> or %<alloc%> on %<map%> clause");
41605 *pc = OMP_CLAUSE_CHAIN (*pc);
41606 continue;
41607 }
41608 pc = &OMP_CLAUSE_CHAIN (*pc);
41609 }
41610
41611 if (map_seen != 3)
41612 {
41613 if (map_seen == 0)
41614 error_at (pragma_tok->location,
41615 "%<#pragma omp target enter data%> must contain at least "
41616 "one %<map%> clause");
41617 return NULL_TREE;
41618 }
41619
41620 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
41621 TREE_TYPE (stmt) = void_type_node;
41622 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
41623 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41624 return add_stmt (stmt);
41625 }
41626
41627 /* OpenMP 4.5:
41628 # pragma omp target exit data target-enter-data-clause[optseq] new-line
41629 structured-block */
41630
41631 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
41632 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
41634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
41637
41638 static tree
41639 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
41640 enum pragma_context context)
41641 {
41642 bool data_seen = false;
41643 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41644 {
41645 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41646 const char *p = IDENTIFIER_POINTER (id);
41647
41648 if (strcmp (p, "data") == 0)
41649 {
41650 cp_lexer_consume_token (parser->lexer);
41651 data_seen = true;
41652 }
41653 }
41654 if (!data_seen)
41655 {
41656 cp_parser_error (parser, "expected %<data%>");
41657 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41658 return NULL_TREE;
41659 }
41660
41661 if (context == pragma_stmt)
41662 {
41663 error_at (pragma_tok->location,
41664 "%<#pragma %s%> may only be used in compound statements",
41665 "omp target exit data");
41666 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41667 return NULL_TREE;
41668 }
41669
41670 tree clauses
41671 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
41672 "#pragma omp target exit data", pragma_tok);
41673 c_omp_adjust_map_clauses (clauses, false);
41674 int map_seen = 0;
41675 for (tree *pc = &clauses; *pc;)
41676 {
41677 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
41678 switch (OMP_CLAUSE_MAP_KIND (*pc))
41679 {
41680 case GOMP_MAP_FROM:
41681 case GOMP_MAP_ALWAYS_FROM:
41682 case GOMP_MAP_RELEASE:
41683 case GOMP_MAP_DELETE:
41684 map_seen = 3;
41685 break;
41686 case GOMP_MAP_FIRSTPRIVATE_POINTER:
41687 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
41688 case GOMP_MAP_ALWAYS_POINTER:
41689 case GOMP_MAP_ATTACH_DETACH:
41690 break;
41691 default:
41692 map_seen |= 1;
41693 error_at (OMP_CLAUSE_LOCATION (*pc),
41694 "%<#pragma omp target exit data%> with map-type other "
41695 "than %<from%>, %<release%> or %<delete%> on %<map%>"
41696 " clause");
41697 *pc = OMP_CLAUSE_CHAIN (*pc);
41698 continue;
41699 }
41700 pc = &OMP_CLAUSE_CHAIN (*pc);
41701 }
41702
41703 if (map_seen != 3)
41704 {
41705 if (map_seen == 0)
41706 error_at (pragma_tok->location,
41707 "%<#pragma omp target exit data%> must contain at least "
41708 "one %<map%> clause");
41709 return NULL_TREE;
41710 }
41711
41712 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
41713 TREE_TYPE (stmt) = void_type_node;
41714 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
41715 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41716 return add_stmt (stmt);
41717 }
41718
41719 /* OpenMP 4.0:
41720 # pragma omp target update target-update-clause[optseq] new-line */
41721
41722 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
41723 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
41724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
41725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
41729
41730 static bool
41731 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
41732 enum pragma_context context)
41733 {
41734 if (context == pragma_stmt)
41735 {
41736 error_at (pragma_tok->location,
41737 "%<#pragma %s%> may only be used in compound statements",
41738 "omp target update");
41739 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41740 return false;
41741 }
41742
41743 tree clauses
41744 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
41745 "#pragma omp target update", pragma_tok);
41746 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
41747 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
41748 {
41749 error_at (pragma_tok->location,
41750 "%<#pragma omp target update%> must contain at least one "
41751 "%<from%> or %<to%> clauses");
41752 return false;
41753 }
41754
41755 tree stmt = make_node (OMP_TARGET_UPDATE);
41756 TREE_TYPE (stmt) = void_type_node;
41757 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
41758 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41759 add_stmt (stmt);
41760 return false;
41761 }
41762
41763 /* OpenMP 4.0:
41764 # pragma omp target target-clause[optseq] new-line
41765 structured-block */
41766
41767 #define OMP_TARGET_CLAUSE_MASK \
41768 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
41770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
41773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
41776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
41778
41779 static bool
41780 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
41781 enum pragma_context context, bool *if_p)
41782 {
41783 tree *pc = NULL, stmt;
41784
41785 if (flag_openmp)
41786 omp_requires_mask
41787 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
41788
41789 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41790 {
41791 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41792 const char *p = IDENTIFIER_POINTER (id);
41793 enum tree_code ccode = ERROR_MARK;
41794
41795 if (strcmp (p, "teams") == 0)
41796 ccode = OMP_TEAMS;
41797 else if (strcmp (p, "parallel") == 0)
41798 ccode = OMP_PARALLEL;
41799 else if (strcmp (p, "simd") == 0)
41800 ccode = OMP_SIMD;
41801 if (ccode != ERROR_MARK)
41802 {
41803 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
41804 char p_name[sizeof ("#pragma omp target teams distribute "
41805 "parallel for simd")];
41806
41807 cp_lexer_consume_token (parser->lexer);
41808 strcpy (p_name, "#pragma omp target");
41809 if (!flag_openmp) /* flag_openmp_simd */
41810 {
41811 tree stmt;
41812 switch (ccode)
41813 {
41814 case OMP_TEAMS:
41815 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
41816 OMP_TARGET_CLAUSE_MASK,
41817 cclauses, if_p);
41818 break;
41819 case OMP_PARALLEL:
41820 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
41821 OMP_TARGET_CLAUSE_MASK,
41822 cclauses, if_p);
41823 break;
41824 case OMP_SIMD:
41825 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
41826 OMP_TARGET_CLAUSE_MASK,
41827 cclauses, if_p);
41828 break;
41829 default:
41830 gcc_unreachable ();
41831 }
41832 return stmt != NULL_TREE;
41833 }
41834 keep_next_level (true);
41835 tree sb = begin_omp_structured_block (), ret;
41836 unsigned save = cp_parser_begin_omp_structured_block (parser);
41837 switch (ccode)
41838 {
41839 case OMP_TEAMS:
41840 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
41841 OMP_TARGET_CLAUSE_MASK, cclauses,
41842 if_p);
41843 break;
41844 case OMP_PARALLEL:
41845 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
41846 OMP_TARGET_CLAUSE_MASK, cclauses,
41847 if_p);
41848 break;
41849 case OMP_SIMD:
41850 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
41851 OMP_TARGET_CLAUSE_MASK, cclauses,
41852 if_p);
41853 break;
41854 default:
41855 gcc_unreachable ();
41856 }
41857 cp_parser_end_omp_structured_block (parser, save);
41858 tree body = finish_omp_structured_block (sb);
41859 if (ret == NULL_TREE)
41860 return false;
41861 if (ccode == OMP_TEAMS && !processing_template_decl)
41862 {
41863 /* For combined target teams, ensure the num_teams and
41864 thread_limit clause expressions are evaluated on the host,
41865 before entering the target construct. */
41866 tree c;
41867 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
41868 c; c = OMP_CLAUSE_CHAIN (c))
41869 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
41870 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
41871 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
41872 {
41873 tree expr = OMP_CLAUSE_OPERAND (c, 0);
41874 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
41875 if (expr == error_mark_node)
41876 continue;
41877 tree tmp = TARGET_EXPR_SLOT (expr);
41878 add_stmt (expr);
41879 OMP_CLAUSE_OPERAND (c, 0) = expr;
41880 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
41881 OMP_CLAUSE_FIRSTPRIVATE);
41882 OMP_CLAUSE_DECL (tc) = tmp;
41883 OMP_CLAUSE_CHAIN (tc)
41884 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
41885 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
41886 }
41887 }
41888 tree stmt = make_node (OMP_TARGET);
41889 TREE_TYPE (stmt) = void_type_node;
41890 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
41891 OMP_TARGET_BODY (stmt) = body;
41892 OMP_TARGET_COMBINED (stmt) = 1;
41893 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41894 add_stmt (stmt);
41895 pc = &OMP_TARGET_CLAUSES (stmt);
41896 goto check_clauses;
41897 }
41898 else if (!flag_openmp) /* flag_openmp_simd */
41899 {
41900 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41901 return false;
41902 }
41903 else if (strcmp (p, "data") == 0)
41904 {
41905 cp_lexer_consume_token (parser->lexer);
41906 cp_parser_omp_target_data (parser, pragma_tok, if_p);
41907 return true;
41908 }
41909 else if (strcmp (p, "enter") == 0)
41910 {
41911 cp_lexer_consume_token (parser->lexer);
41912 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
41913 return false;
41914 }
41915 else if (strcmp (p, "exit") == 0)
41916 {
41917 cp_lexer_consume_token (parser->lexer);
41918 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
41919 return false;
41920 }
41921 else if (strcmp (p, "update") == 0)
41922 {
41923 cp_lexer_consume_token (parser->lexer);
41924 return cp_parser_omp_target_update (parser, pragma_tok, context);
41925 }
41926 }
41927 if (!flag_openmp) /* flag_openmp_simd */
41928 {
41929 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41930 return false;
41931 }
41932
41933 stmt = make_node (OMP_TARGET);
41934 TREE_TYPE (stmt) = void_type_node;
41935
41936 OMP_TARGET_CLAUSES (stmt)
41937 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
41938 "#pragma omp target", pragma_tok);
41939 c_omp_adjust_map_clauses (OMP_TARGET_CLAUSES (stmt), true);
41940
41941 pc = &OMP_TARGET_CLAUSES (stmt);
41942 keep_next_level (true);
41943 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
41944
41945 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41946 add_stmt (stmt);
41947
41948 check_clauses:
41949 while (*pc)
41950 {
41951 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
41952 switch (OMP_CLAUSE_MAP_KIND (*pc))
41953 {
41954 case GOMP_MAP_TO:
41955 case GOMP_MAP_ALWAYS_TO:
41956 case GOMP_MAP_FROM:
41957 case GOMP_MAP_ALWAYS_FROM:
41958 case GOMP_MAP_TOFROM:
41959 case GOMP_MAP_ALWAYS_TOFROM:
41960 case GOMP_MAP_ALLOC:
41961 case GOMP_MAP_FIRSTPRIVATE_POINTER:
41962 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
41963 case GOMP_MAP_ALWAYS_POINTER:
41964 case GOMP_MAP_ATTACH_DETACH:
41965 break;
41966 default:
41967 error_at (OMP_CLAUSE_LOCATION (*pc),
41968 "%<#pragma omp target%> with map-type other "
41969 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
41970 "on %<map%> clause");
41971 *pc = OMP_CLAUSE_CHAIN (*pc);
41972 continue;
41973 }
41974 pc = &OMP_CLAUSE_CHAIN (*pc);
41975 }
41976 return true;
41977 }
41978
41979 /* OpenACC 2.0:
41980 # pragma acc cache (variable-list) new-line
41981 */
41982
41983 static tree
41984 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
41985 {
41986 /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
41987 clauses. */
41988 auto_suppress_location_wrappers sentinel;
41989
41990 tree stmt, clauses;
41991
41992 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
41993 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
41994
41995 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
41996
41997 stmt = make_node (OACC_CACHE);
41998 TREE_TYPE (stmt) = void_type_node;
41999 OACC_CACHE_CLAUSES (stmt) = clauses;
42000 SET_EXPR_LOCATION (stmt, pragma_tok->location);
42001 add_stmt (stmt);
42002
42003 return stmt;
42004 }
42005
42006 /* OpenACC 2.0:
42007 # pragma acc data oacc-data-clause[optseq] new-line
42008 structured-block */
42009
42010 #define OACC_DATA_CLAUSE_MASK \
42011 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
42012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
42013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
42017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
42018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
42020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
42021
42022 static tree
42023 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
42024 {
42025 tree stmt, clauses, block;
42026 unsigned int save;
42027
42028 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
42029 "#pragma acc data", pragma_tok);
42030
42031 block = begin_omp_parallel ();
42032 save = cp_parser_begin_omp_structured_block (parser);
42033 cp_parser_statement (parser, NULL_TREE, false, if_p);
42034 cp_parser_end_omp_structured_block (parser, save);
42035 stmt = finish_oacc_data (clauses, block);
42036 return stmt;
42037 }
42038
42039 /* OpenACC 2.0:
42040 # pragma acc host_data <clauses> new-line
42041 structured-block */
42042
42043 #define OACC_HOST_DATA_CLAUSE_MASK \
42044 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
42045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
42047
42048 static tree
42049 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
42050 {
42051 tree stmt, clauses, block;
42052 unsigned int save;
42053
42054 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
42055 "#pragma acc host_data", pragma_tok);
42056
42057 block = begin_omp_parallel ();
42058 save = cp_parser_begin_omp_structured_block (parser);
42059 cp_parser_statement (parser, NULL_TREE, false, if_p);
42060 cp_parser_end_omp_structured_block (parser, save);
42061 stmt = finish_oacc_host_data (clauses, block);
42062 return stmt;
42063 }
42064
42065 /* OpenACC 2.0:
42066 # pragma acc declare oacc-data-clause[optseq] new-line
42067 */
42068
42069 #define OACC_DECLARE_CLAUSE_MASK \
42070 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
42071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
42075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
42076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
42077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
42078
42079 static tree
42080 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
42081 {
42082 tree clauses, stmt;
42083 bool error = false;
42084 bool found_in_scope = global_bindings_p ();
42085
42086 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
42087 "#pragma acc declare", pragma_tok, true);
42088
42089
42090 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
42091 {
42092 error_at (pragma_tok->location,
42093 "no valid clauses specified in %<#pragma acc declare%>");
42094 return NULL_TREE;
42095 }
42096
42097 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
42098 {
42099 location_t loc = OMP_CLAUSE_LOCATION (t);
42100 tree decl = OMP_CLAUSE_DECL (t);
42101 if (!DECL_P (decl))
42102 {
42103 error_at (loc, "array section in %<#pragma acc declare%>");
42104 error = true;
42105 continue;
42106 }
42107 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
42108 switch (OMP_CLAUSE_MAP_KIND (t))
42109 {
42110 case GOMP_MAP_FIRSTPRIVATE_POINTER:
42111 case GOMP_MAP_ALLOC:
42112 case GOMP_MAP_TO:
42113 case GOMP_MAP_FORCE_DEVICEPTR:
42114 case GOMP_MAP_DEVICE_RESIDENT:
42115 break;
42116
42117 case GOMP_MAP_LINK:
42118 if (!global_bindings_p ()
42119 && (TREE_STATIC (decl)
42120 || !DECL_EXTERNAL (decl)))
42121 {
42122 error_at (loc,
42123 "%qD must be a global variable in "
42124 "%<#pragma acc declare link%>",
42125 decl);
42126 error = true;
42127 continue;
42128 }
42129 break;
42130
42131 default:
42132 if (global_bindings_p ())
42133 {
42134 error_at (loc, "invalid OpenACC clause at file scope");
42135 error = true;
42136 continue;
42137 }
42138 if (DECL_EXTERNAL (decl))
42139 {
42140 error_at (loc,
42141 "invalid use of %<extern%> variable %qD "
42142 "in %<#pragma acc declare%>", decl);
42143 error = true;
42144 continue;
42145 }
42146 else if (TREE_PUBLIC (decl))
42147 {
42148 error_at (loc,
42149 "invalid use of %<global%> variable %qD "
42150 "in %<#pragma acc declare%>", decl);
42151 error = true;
42152 continue;
42153 }
42154 break;
42155 }
42156
42157 if (!found_in_scope)
42158 /* This seems to ignore the existence of cleanup scopes?
42159 What is the meaning for local extern decls? The local
42160 extern is in this scope, but it is referring to a decl that
42161 is namespace scope. */
42162 for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
42163 if (d == decl)
42164 {
42165 found_in_scope = true;
42166 break;
42167 }
42168 if (!found_in_scope)
42169 {
42170 error_at (loc,
42171 "%qD must be a variable declared in the same scope as "
42172 "%<#pragma acc declare%>", decl);
42173 error = true;
42174 continue;
42175 }
42176
42177 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
42178 || lookup_attribute ("omp declare target link",
42179 DECL_ATTRIBUTES (decl)))
42180 {
42181 error_at (loc, "variable %qD used more than once with "
42182 "%<#pragma acc declare%>", decl);
42183 error = true;
42184 continue;
42185 }
42186
42187 if (!error)
42188 {
42189 tree id;
42190
42191 if (DECL_LOCAL_DECL_P (decl))
42192 /* We need to mark the aliased decl, as that is the entity
42193 that is being referred to. This won't work for
42194 dependent variables, but it didn't work for them before
42195 DECL_LOCAL_DECL_P was a thing either. But then
42196 dependent local extern variable decls are as rare as
42197 hen's teeth. */
42198 if (auto alias = DECL_LOCAL_DECL_ALIAS (decl))
42199 decl = alias;
42200
42201 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
42202 id = get_identifier ("omp declare target link");
42203 else
42204 id = get_identifier ("omp declare target");
42205
42206 DECL_ATTRIBUTES (decl)
42207 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
42208 if (current_binding_level->kind == sk_namespace)
42209 {
42210 symtab_node *node = symtab_node::get (decl);
42211 if (node != NULL)
42212 {
42213 node->offloadable = 1;
42214 if (ENABLE_OFFLOADING)
42215 {
42216 g->have_offload = true;
42217 if (is_a <varpool_node *> (node))
42218 vec_safe_push (offload_vars, decl);
42219 }
42220 }
42221 }
42222 }
42223 }
42224
42225 if (error || current_binding_level->kind == sk_namespace)
42226 return NULL_TREE;
42227
42228 stmt = make_node (OACC_DECLARE);
42229 TREE_TYPE (stmt) = void_type_node;
42230 OACC_DECLARE_CLAUSES (stmt) = clauses;
42231 SET_EXPR_LOCATION (stmt, pragma_tok->location);
42232
42233 add_stmt (stmt);
42234
42235 return NULL_TREE;
42236 }
42237
42238 /* OpenACC 2.0:
42239 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
42240
42241 or
42242
42243 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
42244
42245 LOC is the location of the #pragma token.
42246 */
42247
42248 #define OACC_ENTER_DATA_CLAUSE_MASK \
42249 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
42251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
42255
42256 #define OACC_EXIT_DATA_CLAUSE_MASK \
42257 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
42261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
42262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
42263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
42264
42265 static tree
42266 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
42267 bool enter)
42268 {
42269 location_t loc = pragma_tok->location;
42270 tree stmt, clauses;
42271 const char *p = "";
42272
42273 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42274 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
42275
42276 if (strcmp (p, "data") != 0)
42277 {
42278 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
42279 enter ? "enter" : "exit");
42280 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42281 return NULL_TREE;
42282 }
42283
42284 cp_lexer_consume_token (parser->lexer);
42285
42286 if (enter)
42287 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
42288 "#pragma acc enter data", pragma_tok);
42289 else
42290 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
42291 "#pragma acc exit data", pragma_tok);
42292
42293 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
42294 {
42295 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
42296 enter ? "enter" : "exit");
42297 return NULL_TREE;
42298 }
42299
42300 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
42301 TREE_TYPE (stmt) = void_type_node;
42302 OMP_STANDALONE_CLAUSES (stmt) = clauses;
42303 SET_EXPR_LOCATION (stmt, loc);
42304 add_stmt (stmt);
42305 return stmt;
42306 }
42307
42308 /* OpenACC 2.0:
42309 # pragma acc loop oacc-loop-clause[optseq] new-line
42310 structured-block */
42311
42312 #define OACC_LOOP_CLAUSE_MASK \
42313 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
42314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
42315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
42316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
42317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
42318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
42319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
42320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
42321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
42322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
42323
42324 static tree
42325 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
42326 omp_clause_mask mask, tree *cclauses, bool *if_p)
42327 {
42328 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
42329
42330 strcat (p_name, " loop");
42331 mask |= OACC_LOOP_CLAUSE_MASK;
42332
42333 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
42334 cclauses == NULL);
42335 if (cclauses)
42336 {
42337 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
42338 if (*cclauses)
42339 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
42340 if (clauses)
42341 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
42342 }
42343
42344 tree block = begin_omp_structured_block ();
42345 int save = cp_parser_begin_omp_structured_block (parser);
42346 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
42347 cp_parser_end_omp_structured_block (parser, save);
42348 add_stmt (finish_omp_structured_block (block));
42349
42350 return stmt;
42351 }
42352
42353 /* OpenACC 2.0:
42354 # pragma acc kernels oacc-kernels-clause[optseq] new-line
42355 structured-block
42356
42357 or
42358
42359 # pragma acc parallel oacc-parallel-clause[optseq] new-line
42360 structured-block
42361
42362 OpenACC 2.6:
42363
42364 # pragma acc serial oacc-serial-clause[optseq] new-line
42365 */
42366
42367 #define OACC_KERNELS_CLAUSE_MASK \
42368 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
42370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
42371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
42375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
42376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
42378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
42379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
42380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
42381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
42382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
42383
42384 #define OACC_PARALLEL_CLAUSE_MASK \
42385 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
42387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
42388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
42392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
42393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
42394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
42396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
42397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
42398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
42399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
42400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
42401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
42402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
42403
42404 #define OACC_SERIAL_CLAUSE_MASK \
42405 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
42407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
42408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
42412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
42413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
42415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
42416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
42417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
42418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
42419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
42420
42421 static tree
42422 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
42423 char *p_name, bool *if_p)
42424 {
42425 omp_clause_mask mask;
42426 enum tree_code code;
42427 switch (cp_parser_pragma_kind (pragma_tok))
42428 {
42429 case PRAGMA_OACC_KERNELS:
42430 strcat (p_name, " kernels");
42431 mask = OACC_KERNELS_CLAUSE_MASK;
42432 code = OACC_KERNELS;
42433 break;
42434 case PRAGMA_OACC_PARALLEL:
42435 strcat (p_name, " parallel");
42436 mask = OACC_PARALLEL_CLAUSE_MASK;
42437 code = OACC_PARALLEL;
42438 break;
42439 case PRAGMA_OACC_SERIAL:
42440 strcat (p_name, " serial");
42441 mask = OACC_SERIAL_CLAUSE_MASK;
42442 code = OACC_SERIAL;
42443 break;
42444 default:
42445 gcc_unreachable ();
42446 }
42447
42448 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42449 {
42450 const char *p
42451 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
42452 if (strcmp (p, "loop") == 0)
42453 {
42454 cp_lexer_consume_token (parser->lexer);
42455 tree block = begin_omp_parallel ();
42456 tree clauses;
42457 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
42458 &clauses, if_p);
42459 protected_set_expr_location (stmt, pragma_tok->location);
42460 return finish_omp_construct (code, block, clauses);
42461 }
42462 }
42463
42464 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
42465
42466 tree block = begin_omp_parallel ();
42467 unsigned int save = cp_parser_begin_omp_structured_block (parser);
42468 cp_parser_statement (parser, NULL_TREE, false, if_p);
42469 cp_parser_end_omp_structured_block (parser, save);
42470 return finish_omp_construct (code, block, clauses);
42471 }
42472
42473 /* OpenACC 2.0:
42474 # pragma acc update oacc-update-clause[optseq] new-line
42475 */
42476
42477 #define OACC_UPDATE_CLAUSE_MASK \
42478 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
42480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
42481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
42483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
42484
42485 static tree
42486 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
42487 {
42488 tree stmt, clauses;
42489
42490 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
42491 "#pragma acc update", pragma_tok);
42492
42493 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
42494 {
42495 error_at (pragma_tok->location,
42496 "%<#pragma acc update%> must contain at least one "
42497 "%<device%> or %<host%> or %<self%> clause");
42498 return NULL_TREE;
42499 }
42500
42501 stmt = make_node (OACC_UPDATE);
42502 TREE_TYPE (stmt) = void_type_node;
42503 OACC_UPDATE_CLAUSES (stmt) = clauses;
42504 SET_EXPR_LOCATION (stmt, pragma_tok->location);
42505 add_stmt (stmt);
42506 return stmt;
42507 }
42508
42509 /* OpenACC 2.0:
42510 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
42511
42512 LOC is the location of the #pragma token.
42513 */
42514
42515 #define OACC_WAIT_CLAUSE_MASK \
42516 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
42517
42518 static tree
42519 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
42520 {
42521 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
42522 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42523
42524 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
42525 list = cp_parser_oacc_wait_list (parser, loc, list);
42526
42527 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
42528 "#pragma acc wait", pragma_tok);
42529
42530 stmt = c_finish_oacc_wait (loc, list, clauses);
42531 stmt = finish_expr_stmt (stmt);
42532
42533 return stmt;
42534 }
42535
42536 /* OpenMP 4.0:
42537 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
42538
42539 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
42540 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
42541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
42542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
42543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
42544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
42545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
42546
42547 static void
42548 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
42549 enum pragma_context context,
42550 bool variant_p)
42551 {
42552 bool first_p = parser->omp_declare_simd == NULL;
42553 cp_omp_declare_simd_data data;
42554 if (first_p)
42555 {
42556 data.error_seen = false;
42557 data.fndecl_seen = false;
42558 data.variant_p = variant_p;
42559 data.tokens = vNULL;
42560 data.clauses = NULL_TREE;
42561 /* It is safe to take the address of a local variable; it will only be
42562 used while this scope is live. */
42563 parser->omp_declare_simd = &data;
42564 }
42565 else if (parser->omp_declare_simd->variant_p != variant_p)
42566 {
42567 error_at (pragma_tok->location,
42568 "%<#pragma omp declare %s%> followed by "
42569 "%<#pragma omp declare %s%>",
42570 parser->omp_declare_simd->variant_p ? "variant" : "simd",
42571 parser->omp_declare_simd->variant_p ? "simd" : "variant");
42572 parser->omp_declare_simd->error_seen = true;
42573 }
42574
42575 /* Store away all pragma tokens. */
42576 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42577 cp_lexer_consume_token (parser->lexer);
42578 cp_parser_require_pragma_eol (parser, pragma_tok);
42579 struct cp_token_cache *cp
42580 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
42581 parser->omp_declare_simd->tokens.safe_push (cp);
42582
42583 if (first_p)
42584 {
42585 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
42586 cp_parser_pragma (parser, context, NULL);
42587 switch (context)
42588 {
42589 case pragma_external:
42590 cp_parser_declaration (parser, NULL_TREE);
42591 break;
42592 case pragma_member:
42593 cp_parser_member_declaration (parser);
42594 break;
42595 case pragma_objc_icode:
42596 cp_parser_block_declaration (parser, /*statement_p=*/false);
42597 break;
42598 default:
42599 cp_parser_declaration_statement (parser);
42600 break;
42601 }
42602 if (parser->omp_declare_simd
42603 && !parser->omp_declare_simd->error_seen
42604 && !parser->omp_declare_simd->fndecl_seen)
42605 error_at (pragma_tok->location,
42606 "%<#pragma omp declare %s%> not immediately followed by "
42607 "function declaration or definition",
42608 parser->omp_declare_simd->variant_p ? "variant" : "simd");
42609 data.tokens.release ();
42610 parser->omp_declare_simd = NULL;
42611 }
42612 }
42613
42614 static const char *const omp_construct_selectors[] = {
42615 "simd", "target", "teams", "parallel", "for", NULL };
42616 static const char *const omp_device_selectors[] = {
42617 "kind", "isa", "arch", NULL };
42618 static const char *const omp_implementation_selectors[] = {
42619 "vendor", "extension", "atomic_default_mem_order", "unified_address",
42620 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
42621 static const char *const omp_user_selectors[] = {
42622 "condition", NULL };
42623
42624 /* OpenMP 5.0:
42625
42626 trait-selector:
42627 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
42628
42629 trait-score:
42630 score(score-expression) */
42631
42632 static tree
42633 cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
42634 {
42635 tree ret = NULL_TREE;
42636 do
42637 {
42638 tree selector;
42639 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
42640 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42641 selector = cp_lexer_peek_token (parser->lexer)->u.value;
42642 else
42643 {
42644 cp_parser_error (parser, "expected trait selector name");
42645 return error_mark_node;
42646 }
42647
42648 tree properties = NULL_TREE;
42649 const char *const *selectors = NULL;
42650 bool allow_score = true;
42651 bool allow_user = false;
42652 int property_limit = 0;
42653 enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
42654 CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
42655 CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
42656 switch (IDENTIFIER_POINTER (set)[0])
42657 {
42658 case 'c': /* construct */
42659 selectors = omp_construct_selectors;
42660 allow_score = false;
42661 property_limit = 1;
42662 property_kind = CTX_PROPERTY_SIMD;
42663 break;
42664 case 'd': /* device */
42665 selectors = omp_device_selectors;
42666 allow_score = false;
42667 allow_user = true;
42668 property_limit = 3;
42669 property_kind = CTX_PROPERTY_NAME_LIST;
42670 break;
42671 case 'i': /* implementation */
42672 selectors = omp_implementation_selectors;
42673 allow_user = true;
42674 property_limit = 3;
42675 property_kind = CTX_PROPERTY_NAME_LIST;
42676 break;
42677 case 'u': /* user */
42678 selectors = omp_user_selectors;
42679 property_limit = 1;
42680 property_kind = CTX_PROPERTY_EXPR;
42681 break;
42682 default:
42683 gcc_unreachable ();
42684 }
42685 for (int i = 0; ; i++)
42686 {
42687 if (selectors[i] == NULL)
42688 {
42689 if (allow_user)
42690 {
42691 property_kind = CTX_PROPERTY_USER;
42692 break;
42693 }
42694 else
42695 {
42696 error ("selector %qs not allowed for context selector "
42697 "set %qs", IDENTIFIER_POINTER (selector),
42698 IDENTIFIER_POINTER (set));
42699 cp_lexer_consume_token (parser->lexer);
42700 return error_mark_node;
42701 }
42702 }
42703 if (i == property_limit)
42704 property_kind = CTX_PROPERTY_NONE;
42705 if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
42706 break;
42707 }
42708 if (property_kind == CTX_PROPERTY_NAME_LIST
42709 && IDENTIFIER_POINTER (set)[0] == 'i'
42710 && strcmp (IDENTIFIER_POINTER (selector),
42711 "atomic_default_mem_order") == 0)
42712 property_kind = CTX_PROPERTY_ID;
42713
42714 cp_lexer_consume_token (parser->lexer);
42715
42716 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
42717 {
42718 if (property_kind == CTX_PROPERTY_NONE)
42719 {
42720 error ("selector %qs does not accept any properties",
42721 IDENTIFIER_POINTER (selector));
42722 return error_mark_node;
42723 }
42724
42725 matching_parens parens;
42726 parens.consume_open (parser);
42727
42728 cp_token *token = cp_lexer_peek_token (parser->lexer);
42729 if (allow_score
42730 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
42731 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
42732 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
42733 {
42734 cp_lexer_save_tokens (parser->lexer);
42735 cp_lexer_consume_token (parser->lexer);
42736 cp_lexer_consume_token (parser->lexer);
42737 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
42738 true)
42739 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
42740 {
42741 cp_lexer_rollback_tokens (parser->lexer);
42742 cp_lexer_consume_token (parser->lexer);
42743
42744 matching_parens parens2;
42745 parens2.require_open (parser);
42746 tree score = cp_parser_constant_expression (parser);
42747 if (!parens2.require_close (parser))
42748 cp_parser_skip_to_closing_parenthesis (parser, true,
42749 false, true);
42750 cp_parser_require (parser, CPP_COLON, RT_COLON);
42751 if (score != error_mark_node)
42752 {
42753 score = fold_non_dependent_expr (score);
42754 if (value_dependent_expression_p (score))
42755 properties = tree_cons (get_identifier (" score"),
42756 score, properties);
42757 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
42758 || TREE_CODE (score) != INTEGER_CST)
42759 error_at (token->location, "score argument must be "
42760 "constant integer expression");
42761 else if (tree_int_cst_sgn (score) < 0)
42762 error_at (token->location, "score argument must be "
42763 "non-negative");
42764 else
42765 properties = tree_cons (get_identifier (" score"),
42766 score, properties);
42767 }
42768 }
42769 else
42770 cp_lexer_rollback_tokens (parser->lexer);
42771
42772 token = cp_lexer_peek_token (parser->lexer);
42773 }
42774
42775 switch (property_kind)
42776 {
42777 tree t;
42778 case CTX_PROPERTY_USER:
42779 do
42780 {
42781 t = cp_parser_constant_expression (parser);
42782 if (t != error_mark_node)
42783 {
42784 t = fold_non_dependent_expr (t);
42785 if (TREE_CODE (t) == STRING_CST)
42786 properties = tree_cons (NULL_TREE, t, properties);
42787 else if (!value_dependent_expression_p (t)
42788 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
42789 || !tree_fits_shwi_p (t)))
42790 error_at (token->location, "property must be "
42791 "constant integer expression or string "
42792 "literal");
42793 else
42794 properties = tree_cons (NULL_TREE, t, properties);
42795 }
42796 else
42797 return error_mark_node;
42798
42799 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42800 cp_lexer_consume_token (parser->lexer);
42801 else
42802 break;
42803 }
42804 while (1);
42805 break;
42806 case CTX_PROPERTY_ID:
42807 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
42808 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42809 {
42810 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
42811 cp_lexer_consume_token (parser->lexer);
42812 properties = tree_cons (prop, NULL_TREE, properties);
42813 }
42814 else
42815 {
42816 cp_parser_error (parser, "expected identifier");
42817 return error_mark_node;
42818 }
42819 break;
42820 case CTX_PROPERTY_NAME_LIST:
42821 do
42822 {
42823 tree prop = NULL_TREE, value = NULL_TREE;
42824 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
42825 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42826 {
42827 prop = cp_lexer_peek_token (parser->lexer)->u.value;
42828 cp_lexer_consume_token (parser->lexer);
42829 }
42830 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
42831 value = cp_parser_string_literal (parser, false, false);
42832 else
42833 {
42834 cp_parser_error (parser, "expected identifier or "
42835 "string literal");
42836 return error_mark_node;
42837 }
42838
42839 properties = tree_cons (prop, value, properties);
42840
42841 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42842 cp_lexer_consume_token (parser->lexer);
42843 else
42844 break;
42845 }
42846 while (1);
42847 break;
42848 case CTX_PROPERTY_EXPR:
42849 t = cp_parser_constant_expression (parser);
42850 if (t != error_mark_node)
42851 {
42852 t = fold_non_dependent_expr (t);
42853 if (!value_dependent_expression_p (t)
42854 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
42855 || !tree_fits_shwi_p (t)))
42856 error_at (token->location, "property must be "
42857 "constant integer expression");
42858 else
42859 properties = tree_cons (NULL_TREE, t, properties);
42860 }
42861 else
42862 return error_mark_node;
42863 break;
42864 case CTX_PROPERTY_SIMD:
42865 if (!has_parms_p)
42866 {
42867 error_at (token->location, "properties for %<simd%> "
42868 "selector may not be specified in "
42869 "%<metadirective%>");
42870 return error_mark_node;
42871 }
42872 properties
42873 = cp_parser_omp_all_clauses (parser,
42874 OMP_DECLARE_SIMD_CLAUSE_MASK,
42875 "simd", NULL, true, 2);
42876 break;
42877 default:
42878 gcc_unreachable ();
42879 }
42880
42881 if (!parens.require_close (parser))
42882 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
42883
42884 properties = nreverse (properties);
42885 }
42886 else if (property_kind == CTX_PROPERTY_NAME_LIST
42887 || property_kind == CTX_PROPERTY_ID
42888 || property_kind == CTX_PROPERTY_EXPR)
42889 {
42890 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
42891 return error_mark_node;
42892 }
42893
42894 ret = tree_cons (selector, properties, ret);
42895
42896 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42897 cp_lexer_consume_token (parser->lexer);
42898 else
42899 break;
42900 }
42901 while (1);
42902
42903 return nreverse (ret);
42904 }
42905
42906 /* OpenMP 5.0:
42907
42908 trait-set-selector[,trait-set-selector[,...]]
42909
42910 trait-set-selector:
42911 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
42912
42913 trait-set-selector-name:
42914 constructor
42915 device
42916 implementation
42917 user */
42918
42919 static tree
42920 cp_parser_omp_context_selector_specification (cp_parser *parser,
42921 bool has_parms_p)
42922 {
42923 tree ret = NULL_TREE;
42924 do
42925 {
42926 const char *setp = "";
42927 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42928 setp
42929 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
42930 switch (setp[0])
42931 {
42932 case 'c':
42933 if (strcmp (setp, "construct") == 0)
42934 setp = NULL;
42935 break;
42936 case 'd':
42937 if (strcmp (setp, "device") == 0)
42938 setp = NULL;
42939 break;
42940 case 'i':
42941 if (strcmp (setp, "implementation") == 0)
42942 setp = NULL;
42943 break;
42944 case 'u':
42945 if (strcmp (setp, "user") == 0)
42946 setp = NULL;
42947 break;
42948 default:
42949 break;
42950 }
42951 if (setp)
42952 {
42953 cp_parser_error (parser, "expected %<construct%>, %<device%>, "
42954 "%<implementation%> or %<user%>");
42955 return error_mark_node;
42956 }
42957
42958 tree set = cp_lexer_peek_token (parser->lexer)->u.value;
42959 cp_lexer_consume_token (parser->lexer);
42960
42961 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42962 return error_mark_node;
42963
42964 matching_braces braces;
42965 if (!braces.require_open (parser))
42966 return error_mark_node;
42967
42968 tree selectors
42969 = cp_parser_omp_context_selector (parser, set, has_parms_p);
42970 if (selectors == error_mark_node)
42971 {
42972 cp_parser_skip_to_closing_brace (parser);
42973 ret = error_mark_node;
42974 }
42975 else if (ret != error_mark_node)
42976 ret = tree_cons (set, selectors, ret);
42977
42978 braces.require_close (parser);
42979
42980 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42981 cp_lexer_consume_token (parser->lexer);
42982 else
42983 break;
42984 }
42985 while (1);
42986
42987 if (ret == error_mark_node)
42988 return ret;
42989 return nreverse (ret);
42990 }
42991
42992 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
42993 that into "omp declare variant base" attribute. */
42994
42995 static tree
42996 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
42997 tree attrs)
42998 {
42999 matching_parens parens;
43000 if (!parens.require_open (parser))
43001 {
43002 fail:
43003 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43004 return attrs;
43005 }
43006
43007 bool template_p;
43008 cp_id_kind idk = CP_ID_KIND_NONE;
43009 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
43010 cp_expr varid
43011 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
43012 /*check_dependency_p=*/true,
43013 /*template_p=*/&template_p,
43014 /*declarator_p=*/false,
43015 /*optional_p=*/false);
43016 parens.require_close (parser);
43017
43018 tree variant;
43019 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
43020 || TREE_CODE (varid) == TYPE_DECL
43021 || varid == error_mark_node)
43022 variant = varid;
43023 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
43024 variant = NULL_TREE;
43025 else
43026 {
43027 tree ambiguous_decls;
43028 variant = cp_parser_lookup_name (parser, varid, none_type,
43029 template_p, /*is_namespace=*/false,
43030 /*check_dependency=*/true,
43031 &ambiguous_decls,
43032 varid.get_location ());
43033 if (ambiguous_decls)
43034 variant = NULL_TREE;
43035 }
43036 if (variant == NULL_TREE)
43037 variant = error_mark_node;
43038 else if (TREE_CODE (variant) != SCOPE_REF)
43039 {
43040 const char *error_msg;
43041 variant
43042 = finish_id_expression (varid, variant, parser->scope,
43043 &idk, false, true,
43044 &parser->non_integral_constant_expression_p,
43045 template_p, true, false, false, &error_msg,
43046 varid.get_location ());
43047 if (error_msg)
43048 cp_parser_error (parser, error_msg);
43049 }
43050 location_t caret_loc = get_pure_location (varid.get_location ());
43051 location_t start_loc = get_start (varid_token->location);
43052 location_t finish_loc = get_finish (varid.get_location ());
43053 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
43054
43055 const char *clause = "";
43056 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
43057 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43058 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
43059 if (strcmp (clause, "match"))
43060 {
43061 cp_parser_error (parser, "expected %<match%>");
43062 goto fail;
43063 }
43064
43065 cp_lexer_consume_token (parser->lexer);
43066
43067 if (!parens.require_open (parser))
43068 goto fail;
43069
43070 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
43071 if (ctx == error_mark_node)
43072 goto fail;
43073 ctx = c_omp_check_context_selector (match_loc, ctx);
43074 if (ctx != error_mark_node && variant != error_mark_node)
43075 {
43076 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
43077 match_loc);
43078 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
43079 loc_node = tree_cons (match_loc_node,
43080 build_int_cst (integer_type_node, idk),
43081 build_tree_list (loc_node, integer_zero_node));
43082 attrs = tree_cons (get_identifier ("omp declare variant base"),
43083 tree_cons (variant, ctx, loc_node), attrs);
43084 if (processing_template_decl)
43085 ATTR_IS_DEPENDENT (attrs) = 1;
43086 }
43087
43088 parens.require_close (parser);
43089 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43090 return attrs;
43091 }
43092
43093
43094 /* Finalize #pragma omp declare simd clauses after direct declarator has
43095 been parsed, and put that into "omp declare simd" attribute. */
43096
43097 static tree
43098 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
43099 {
43100 struct cp_token_cache *ce;
43101 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
43102 int i;
43103
43104 if (!data->error_seen && data->fndecl_seen)
43105 {
43106 error ("%<#pragma omp declare %s%> not immediately followed by "
43107 "a single function declaration or definition",
43108 data->variant_p ? "variant" : "simd");
43109 data->error_seen = true;
43110 }
43111 if (data->error_seen)
43112 return attrs;
43113
43114 FOR_EACH_VEC_ELT (data->tokens, i, ce)
43115 {
43116 tree c, cl;
43117
43118 cp_parser_push_lexer_for_tokens (parser, ce);
43119 parser->lexer->in_pragma = true;
43120 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
43121 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
43122 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43123 const char *kind = IDENTIFIER_POINTER (id);
43124 cp_lexer_consume_token (parser->lexer);
43125 if (strcmp (kind, "simd") == 0)
43126 {
43127 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
43128 "#pragma omp declare simd",
43129 pragma_tok);
43130 if (cl)
43131 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
43132 c = build_tree_list (get_identifier ("omp declare simd"), cl);
43133 TREE_CHAIN (c) = attrs;
43134 if (processing_template_decl)
43135 ATTR_IS_DEPENDENT (c) = 1;
43136 attrs = c;
43137 }
43138 else
43139 {
43140 gcc_assert (strcmp (kind, "variant") == 0);
43141 attrs = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
43142 }
43143 cp_parser_pop_lexer (parser);
43144 }
43145
43146 data->fndecl_seen = true;
43147 return attrs;
43148 }
43149
43150
43151 /* OpenMP 4.0:
43152 # pragma omp declare target new-line
43153 declarations and definitions
43154 # pragma omp end declare target new-line
43155
43156 OpenMP 4.5:
43157 # pragma omp declare target ( extended-list ) new-line
43158
43159 # pragma omp declare target declare-target-clauses[seq] new-line */
43160
43161 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
43162 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
43163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
43164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
43165
43166 static void
43167 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
43168 {
43169 tree clauses = NULL_TREE;
43170 int device_type = 0;
43171 bool only_device_type = true;
43172 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43173 clauses
43174 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
43175 "#pragma omp declare target", pragma_tok);
43176 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
43177 {
43178 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
43179 clauses);
43180 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
43181 cp_parser_require_pragma_eol (parser, pragma_tok);
43182 }
43183 else
43184 {
43185 cp_parser_require_pragma_eol (parser, pragma_tok);
43186 scope_chain->omp_declare_target_attribute++;
43187 return;
43188 }
43189 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
43190 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
43191 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
43192 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
43193 {
43194 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
43195 continue;
43196 tree t = OMP_CLAUSE_DECL (c), id;
43197 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
43198 tree at2 = lookup_attribute ("omp declare target link",
43199 DECL_ATTRIBUTES (t));
43200 only_device_type = false;
43201 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
43202 {
43203 id = get_identifier ("omp declare target link");
43204 std::swap (at1, at2);
43205 }
43206 else
43207 id = get_identifier ("omp declare target");
43208 if (at2)
43209 {
43210 error_at (OMP_CLAUSE_LOCATION (c),
43211 "%qD specified both in declare target %<link%> and %<to%>"
43212 " clauses", t);
43213 continue;
43214 }
43215 if (!at1)
43216 {
43217 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
43218 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
43219 continue;
43220
43221 symtab_node *node = symtab_node::get (t);
43222 if (node != NULL)
43223 {
43224 node->offloadable = 1;
43225 if (ENABLE_OFFLOADING)
43226 {
43227 g->have_offload = true;
43228 if (is_a <varpool_node *> (node))
43229 vec_safe_push (offload_vars, t);
43230 }
43231 }
43232 }
43233 if (TREE_CODE (t) != FUNCTION_DECL)
43234 continue;
43235 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
43236 {
43237 tree at3 = lookup_attribute ("omp declare target host",
43238 DECL_ATTRIBUTES (t));
43239 if (at3 == NULL_TREE)
43240 {
43241 id = get_identifier ("omp declare target host");
43242 DECL_ATTRIBUTES (t)
43243 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
43244 }
43245 }
43246 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
43247 {
43248 tree at3 = lookup_attribute ("omp declare target nohost",
43249 DECL_ATTRIBUTES (t));
43250 if (at3 == NULL_TREE)
43251 {
43252 id = get_identifier ("omp declare target nohost");
43253 DECL_ATTRIBUTES (t)
43254 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
43255 }
43256 }
43257 }
43258 if (device_type && only_device_type)
43259 warning_at (OMP_CLAUSE_LOCATION (clauses), 0,
43260 "directive with only %<device_type%> clauses ignored");
43261 }
43262
43263 static void
43264 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
43265 {
43266 const char *p = "";
43267 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43268 {
43269 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43270 p = IDENTIFIER_POINTER (id);
43271 }
43272 if (strcmp (p, "declare") == 0)
43273 {
43274 cp_lexer_consume_token (parser->lexer);
43275 p = "";
43276 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43277 {
43278 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43279 p = IDENTIFIER_POINTER (id);
43280 }
43281 if (strcmp (p, "target") == 0)
43282 cp_lexer_consume_token (parser->lexer);
43283 else
43284 {
43285 cp_parser_error (parser, "expected %<target%>");
43286 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43287 return;
43288 }
43289 }
43290 else
43291 {
43292 cp_parser_error (parser, "expected %<declare%>");
43293 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43294 return;
43295 }
43296 cp_parser_require_pragma_eol (parser, pragma_tok);
43297 if (!scope_chain->omp_declare_target_attribute)
43298 error_at (pragma_tok->location,
43299 "%<#pragma omp end declare target%> without corresponding "
43300 "%<#pragma omp declare target%>");
43301 else
43302 scope_chain->omp_declare_target_attribute--;
43303 }
43304
43305 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
43306 expression and optional initializer clause of
43307 #pragma omp declare reduction. We store the expression(s) as
43308 either 3, 6 or 7 special statements inside of the artificial function's
43309 body. The first two statements are DECL_EXPRs for the artificial
43310 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
43311 expression that uses those variables.
43312 If there was any INITIALIZER clause, this is followed by further statements,
43313 the fourth and fifth statements are DECL_EXPRs for the artificial
43314 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
43315 constructor variant (first token after open paren is not omp_priv),
43316 then the sixth statement is a statement with the function call expression
43317 that uses the OMP_PRIV and optionally OMP_ORIG variable.
43318 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
43319 to initialize the OMP_PRIV artificial variable and there is seventh
43320 statement, a DECL_EXPR of the OMP_PRIV statement again. */
43321
43322 static bool
43323 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
43324 {
43325 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
43326 gcc_assert (TYPE_REF_P (type));
43327 type = TREE_TYPE (type);
43328 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
43329 DECL_ARTIFICIAL (omp_out) = 1;
43330 pushdecl (omp_out);
43331 add_decl_expr (omp_out);
43332 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
43333 DECL_ARTIFICIAL (omp_in) = 1;
43334 pushdecl (omp_in);
43335 add_decl_expr (omp_in);
43336 tree combiner;
43337 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
43338
43339 keep_next_level (true);
43340 tree block = begin_omp_structured_block ();
43341 combiner = cp_parser_expression (parser);
43342 finish_expr_stmt (combiner);
43343 block = finish_omp_structured_block (block);
43344 if (processing_template_decl)
43345 block = build_stmt (input_location, EXPR_STMT, block);
43346 add_stmt (block);
43347
43348 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
43349 return false;
43350
43351 const char *p = "";
43352 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43353 {
43354 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43355 p = IDENTIFIER_POINTER (id);
43356 }
43357
43358 if (strcmp (p, "initializer") == 0)
43359 {
43360 cp_lexer_consume_token (parser->lexer);
43361 matching_parens parens;
43362 if (!parens.require_open (parser))
43363 return false;
43364
43365 p = "";
43366 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43367 {
43368 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43369 p = IDENTIFIER_POINTER (id);
43370 }
43371
43372 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
43373 DECL_ARTIFICIAL (omp_priv) = 1;
43374 pushdecl (omp_priv);
43375 add_decl_expr (omp_priv);
43376 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
43377 DECL_ARTIFICIAL (omp_orig) = 1;
43378 pushdecl (omp_orig);
43379 add_decl_expr (omp_orig);
43380
43381 keep_next_level (true);
43382 block = begin_omp_structured_block ();
43383
43384 bool ctor = false;
43385 if (strcmp (p, "omp_priv") == 0)
43386 {
43387 bool is_direct_init, is_non_constant_init;
43388 ctor = true;
43389 cp_lexer_consume_token (parser->lexer);
43390 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
43391 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
43392 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
43393 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
43394 == CPP_CLOSE_PAREN
43395 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
43396 == CPP_CLOSE_PAREN))
43397 {
43398 finish_omp_structured_block (block);
43399 error ("invalid initializer clause");
43400 return false;
43401 }
43402 initializer = cp_parser_initializer (parser, &is_direct_init,
43403 &is_non_constant_init);
43404 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
43405 NULL_TREE, LOOKUP_ONLYCONVERTING);
43406 }
43407 else
43408 {
43409 cp_parser_parse_tentatively (parser);
43410 /* Don't create location wrapper nodes here. */
43411 auto_suppress_location_wrappers sentinel;
43412 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
43413 /*check_dependency_p=*/true,
43414 /*template_p=*/NULL,
43415 /*declarator_p=*/false,
43416 /*optional_p=*/false);
43417 vec<tree, va_gc> *args;
43418 if (fn_name == error_mark_node
43419 || cp_parser_error_occurred (parser)
43420 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
43421 || ((args = cp_parser_parenthesized_expression_list
43422 (parser, non_attr, /*cast_p=*/false,
43423 /*allow_expansion_p=*/true,
43424 /*non_constant_p=*/NULL)),
43425 cp_parser_error_occurred (parser)))
43426 {
43427 finish_omp_structured_block (block);
43428 cp_parser_abort_tentative_parse (parser);
43429 cp_parser_error (parser, "expected id-expression (arguments)");
43430 return false;
43431 }
43432 unsigned int i;
43433 tree arg;
43434 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
43435 if (arg == omp_priv
43436 || (TREE_CODE (arg) == ADDR_EXPR
43437 && TREE_OPERAND (arg, 0) == omp_priv))
43438 break;
43439 cp_parser_abort_tentative_parse (parser);
43440 if (arg == NULL_TREE)
43441 error ("one of the initializer call arguments should be %<omp_priv%>"
43442 " or %<&omp_priv%>");
43443 initializer = cp_parser_postfix_expression (parser, false, false, false,
43444 false, NULL);
43445 finish_expr_stmt (initializer);
43446 }
43447
43448 block = finish_omp_structured_block (block);
43449 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
43450 if (processing_template_decl)
43451 block = build_stmt (input_location, EXPR_STMT, block);
43452 add_stmt (block);
43453
43454 if (ctor)
43455 add_decl_expr (omp_orig);
43456
43457 if (!parens.require_close (parser))
43458 return false;
43459 }
43460
43461 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
43462 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
43463 UNKNOWN_LOCATION);
43464
43465 return true;
43466 }
43467
43468 /* OpenMP 4.0
43469 #pragma omp declare reduction (reduction-id : typename-list : expression) \
43470 initializer-clause[opt] new-line
43471
43472 initializer-clause:
43473 initializer (omp_priv initializer)
43474 initializer (function-name (argument-list)) */
43475
43476 static void
43477 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
43478 enum pragma_context)
43479 {
43480 auto_vec<tree> types;
43481 enum tree_code reduc_code = ERROR_MARK;
43482 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
43483 unsigned int i;
43484 cp_token *first_token;
43485 cp_token_cache *cp;
43486 int errs;
43487 void *p;
43488
43489 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
43490 p = obstack_alloc (&declarator_obstack, 0);
43491
43492 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
43493 goto fail;
43494
43495 switch (cp_lexer_peek_token (parser->lexer)->type)
43496 {
43497 case CPP_PLUS:
43498 reduc_code = PLUS_EXPR;
43499 break;
43500 case CPP_MULT:
43501 reduc_code = MULT_EXPR;
43502 break;
43503 case CPP_MINUS:
43504 reduc_code = MINUS_EXPR;
43505 break;
43506 case CPP_AND:
43507 reduc_code = BIT_AND_EXPR;
43508 break;
43509 case CPP_XOR:
43510 reduc_code = BIT_XOR_EXPR;
43511 break;
43512 case CPP_OR:
43513 reduc_code = BIT_IOR_EXPR;
43514 break;
43515 case CPP_AND_AND:
43516 reduc_code = TRUTH_ANDIF_EXPR;
43517 break;
43518 case CPP_OR_OR:
43519 reduc_code = TRUTH_ORIF_EXPR;
43520 break;
43521 case CPP_NAME:
43522 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
43523 break;
43524 default:
43525 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
43526 "%<|%>, %<&&%>, %<||%> or identifier");
43527 goto fail;
43528 }
43529
43530 if (reduc_code != ERROR_MARK)
43531 cp_lexer_consume_token (parser->lexer);
43532
43533 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
43534 if (reduc_id == error_mark_node)
43535 goto fail;
43536
43537 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
43538 goto fail;
43539
43540 /* Types may not be defined in declare reduction type list. */
43541 const char *saved_message;
43542 saved_message = parser->type_definition_forbidden_message;
43543 parser->type_definition_forbidden_message
43544 = G_("types may not be defined in declare reduction type list");
43545 bool saved_colon_corrects_to_scope_p;
43546 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
43547 parser->colon_corrects_to_scope_p = false;
43548 bool saved_colon_doesnt_start_class_def_p;
43549 saved_colon_doesnt_start_class_def_p
43550 = parser->colon_doesnt_start_class_def_p;
43551 parser->colon_doesnt_start_class_def_p = true;
43552
43553 while (true)
43554 {
43555 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43556 type = cp_parser_type_id (parser);
43557 if (type == error_mark_node)
43558 ;
43559 else if (ARITHMETIC_TYPE_P (type)
43560 && (orig_reduc_id == NULL_TREE
43561 || (TREE_CODE (type) != COMPLEX_TYPE
43562 && (id_equal (orig_reduc_id, "min")
43563 || id_equal (orig_reduc_id, "max")))))
43564 error_at (loc, "predeclared arithmetic type %qT in "
43565 "%<#pragma omp declare reduction%>", type);
43566 else if (FUNC_OR_METHOD_TYPE_P (type)
43567 || TREE_CODE (type) == ARRAY_TYPE)
43568 error_at (loc, "function or array type %qT in "
43569 "%<#pragma omp declare reduction%>", type);
43570 else if (TYPE_REF_P (type))
43571 error_at (loc, "reference type %qT in "
43572 "%<#pragma omp declare reduction%>", type);
43573 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
43574 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
43575 "type %qT in %<#pragma omp declare reduction%>", type);
43576 else
43577 types.safe_push (type);
43578
43579 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43580 cp_lexer_consume_token (parser->lexer);
43581 else
43582 break;
43583 }
43584
43585 /* Restore the saved message. */
43586 parser->type_definition_forbidden_message = saved_message;
43587 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
43588 parser->colon_doesnt_start_class_def_p
43589 = saved_colon_doesnt_start_class_def_p;
43590
43591 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
43592 || types.is_empty ())
43593 {
43594 fail:
43595 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43596 goto done;
43597 }
43598
43599 first_token = cp_lexer_peek_token (parser->lexer);
43600 cp = NULL;
43601 errs = errorcount;
43602 FOR_EACH_VEC_ELT (types, i, type)
43603 {
43604 tree fntype
43605 = build_function_type_list (void_type_node,
43606 cp_build_reference_type (type, false),
43607 NULL_TREE);
43608 tree this_reduc_id = reduc_id;
43609 if (!dependent_type_p (type))
43610 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
43611 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
43612 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
43613 DECL_ARTIFICIAL (fndecl) = 1;
43614 DECL_EXTERNAL (fndecl) = 1;
43615 DECL_DECLARED_INLINE_P (fndecl) = 1;
43616 DECL_IGNORED_P (fndecl) = 1;
43617 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
43618 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
43619 DECL_ATTRIBUTES (fndecl)
43620 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
43621 DECL_ATTRIBUTES (fndecl));
43622 bool block_scope = false;
43623 if (current_function_decl)
43624 {
43625 block_scope = true;
43626 DECL_CONTEXT (fndecl) = current_function_decl;
43627 DECL_LOCAL_DECL_P (fndecl) = true;
43628 }
43629
43630 if (processing_template_decl)
43631 fndecl = push_template_decl (fndecl);
43632
43633 if (block_scope)
43634 {
43635 if (!processing_template_decl)
43636 pushdecl (fndecl);
43637 }
43638 else if (current_class_type)
43639 {
43640 if (cp == NULL)
43641 {
43642 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
43643 cp_lexer_consume_token (parser->lexer);
43644 cp = cp_token_cache_new (first_token,
43645 cp_lexer_peek_nth_token (parser->lexer,
43646 2));
43647 }
43648 DECL_STATIC_FUNCTION_P (fndecl) = 1;
43649 finish_member_declaration (fndecl);
43650 DECL_PENDING_INLINE_INFO (fndecl) = cp;
43651 DECL_PENDING_INLINE_P (fndecl) = 1;
43652 vec_safe_push (unparsed_funs_with_definitions, fndecl);
43653 continue;
43654 }
43655 else
43656 {
43657 DECL_CONTEXT (fndecl) = current_namespace;
43658 tree d = pushdecl (fndecl);
43659 /* We should never meet a matched duplicate decl. */
43660 gcc_checking_assert (d == error_mark_node || d == fndecl);
43661 }
43662
43663 tree block = NULL_TREE;
43664 if (!block_scope)
43665 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
43666 else
43667 block = begin_omp_structured_block ();
43668 if (cp)
43669 {
43670 cp_parser_push_lexer_for_tokens (parser, cp);
43671 parser->lexer->in_pragma = true;
43672 }
43673
43674 bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
43675
43676 if (cp)
43677 cp_parser_pop_lexer (parser);
43678 if (!block_scope)
43679 finish_function (/*inline_p=*/false);
43680 else
43681 {
43682 DECL_CONTEXT (fndecl) = current_function_decl;
43683 if (DECL_TEMPLATE_INFO (fndecl))
43684 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
43685 }
43686 if (!ok)
43687 goto fail;
43688
43689 if (block_scope)
43690 {
43691 block = finish_omp_structured_block (block);
43692 if (TREE_CODE (block) == BIND_EXPR)
43693 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
43694 else if (TREE_CODE (block) == STATEMENT_LIST)
43695 DECL_SAVED_TREE (fndecl) = block;
43696 if (processing_template_decl)
43697 add_decl_expr (fndecl);
43698 }
43699
43700 cp_check_omp_declare_reduction (fndecl);
43701 if (cp == NULL && types.length () > 1)
43702 cp = cp_token_cache_new (first_token,
43703 cp_lexer_peek_nth_token (parser->lexer, 2));
43704 if (errs != errorcount)
43705 break;
43706 }
43707
43708 cp_parser_require_pragma_eol (parser, pragma_tok);
43709
43710 done:
43711 /* Free any declarators allocated. */
43712 obstack_free (&declarator_obstack, p);
43713 }
43714
43715 /* OpenMP 4.0
43716 #pragma omp declare simd declare-simd-clauses[optseq] new-line
43717 #pragma omp declare reduction (reduction-id : typename-list : expression) \
43718 initializer-clause[opt] new-line
43719 #pragma omp declare target new-line
43720
43721 OpenMP 5.0
43722 #pragma omp declare variant (identifier) match (context-selector) */
43723
43724 static bool
43725 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
43726 enum pragma_context context)
43727 {
43728 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43729 {
43730 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43731 const char *p = IDENTIFIER_POINTER (id);
43732
43733 if (strcmp (p, "simd") == 0)
43734 {
43735 cp_lexer_consume_token (parser->lexer);
43736 cp_parser_omp_declare_simd (parser, pragma_tok,
43737 context, false);
43738 return true;
43739 }
43740 if (flag_openmp && strcmp (p, "variant") == 0)
43741 {
43742 cp_lexer_consume_token (parser->lexer);
43743 cp_parser_omp_declare_simd (parser, pragma_tok,
43744 context, true);
43745 return true;
43746 }
43747 cp_ensure_no_omp_declare_simd (parser);
43748 if (strcmp (p, "reduction") == 0)
43749 {
43750 cp_lexer_consume_token (parser->lexer);
43751 cp_parser_omp_declare_reduction (parser, pragma_tok,
43752 context);
43753 return false;
43754 }
43755 if (!flag_openmp) /* flag_openmp_simd */
43756 {
43757 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43758 return false;
43759 }
43760 if (strcmp (p, "target") == 0)
43761 {
43762 cp_lexer_consume_token (parser->lexer);
43763 cp_parser_omp_declare_target (parser, pragma_tok);
43764 return false;
43765 }
43766 }
43767 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
43768 "%<target%> or %<variant%>");
43769 cp_parser_require_pragma_eol (parser, pragma_tok);
43770 return false;
43771 }
43772
43773 /* OpenMP 5.0
43774 #pragma omp requires clauses[optseq] new-line */
43775
43776 static bool
43777 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
43778 {
43779 bool first = true;
43780 enum omp_requires new_req = (enum omp_requires) 0;
43781
43782 location_t loc = pragma_tok->location;
43783 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
43784 {
43785 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43786 cp_lexer_consume_token (parser->lexer);
43787
43788 first = false;
43789
43790 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43791 {
43792 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43793 const char *p = IDENTIFIER_POINTER (id);
43794 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
43795 enum omp_requires this_req = (enum omp_requires) 0;
43796
43797 if (!strcmp (p, "unified_address"))
43798 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
43799 else if (!strcmp (p, "unified_shared_memory"))
43800 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
43801 else if (!strcmp (p, "dynamic_allocators"))
43802 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
43803 else if (!strcmp (p, "reverse_offload"))
43804 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
43805 else if (!strcmp (p, "atomic_default_mem_order"))
43806 {
43807 cp_lexer_consume_token (parser->lexer);
43808
43809 matching_parens parens;
43810 if (parens.require_open (parser))
43811 {
43812 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43813 {
43814 id = cp_lexer_peek_token (parser->lexer)->u.value;
43815 p = IDENTIFIER_POINTER (id);
43816
43817 if (!strcmp (p, "seq_cst"))
43818 this_req
43819 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
43820 else if (!strcmp (p, "relaxed"))
43821 this_req
43822 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
43823 else if (!strcmp (p, "acq_rel"))
43824 this_req
43825 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
43826 }
43827 if (this_req == 0)
43828 {
43829 error_at (cp_lexer_peek_token (parser->lexer)->location,
43830 "expected %<seq_cst%>, %<relaxed%> or "
43831 "%<acq_rel%>");
43832 if (cp_lexer_nth_token_is (parser->lexer, 2,
43833 CPP_CLOSE_PAREN))
43834 cp_lexer_consume_token (parser->lexer);
43835 }
43836 else
43837 cp_lexer_consume_token (parser->lexer);
43838
43839 if (!parens.require_close (parser))
43840 cp_parser_skip_to_closing_parenthesis (parser,
43841 /*recovering=*/true,
43842 /*or_comma=*/false,
43843 /*consume_paren=*/
43844 true);
43845
43846 if (this_req == 0)
43847 {
43848 cp_parser_require_pragma_eol (parser, pragma_tok);
43849 return false;
43850 }
43851 }
43852 p = NULL;
43853 }
43854 else
43855 {
43856 error_at (cloc, "expected %<unified_address%>, "
43857 "%<unified_shared_memory%>, "
43858 "%<dynamic_allocators%>, "
43859 "%<reverse_offload%> "
43860 "or %<atomic_default_mem_order%> clause");
43861 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43862 return false;
43863 }
43864 if (p)
43865 sorry_at (cloc, "%qs clause on %<requires%> directive not "
43866 "supported yet", p);
43867 if (p)
43868 cp_lexer_consume_token (parser->lexer);
43869 if (this_req)
43870 {
43871 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
43872 {
43873 if ((this_req & new_req) != 0)
43874 error_at (cloc, "too many %qs clauses", p);
43875 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
43876 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
43877 error_at (cloc, "%qs clause used lexically after first "
43878 "target construct or offloading API", p);
43879 }
43880 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
43881 {
43882 error_at (cloc, "too many %qs clauses",
43883 "atomic_default_mem_order");
43884 this_req = (enum omp_requires) 0;
43885 }
43886 else if ((omp_requires_mask
43887 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
43888 {
43889 error_at (cloc, "more than one %<atomic_default_mem_order%>"
43890 " clause in a single compilation unit");
43891 this_req
43892 = (enum omp_requires)
43893 (omp_requires_mask
43894 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
43895 }
43896 else if ((omp_requires_mask
43897 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
43898 error_at (cloc, "%<atomic_default_mem_order%> clause used "
43899 "lexically after first %<atomic%> construct "
43900 "without memory order clause");
43901 new_req = (enum omp_requires) (new_req | this_req);
43902 omp_requires_mask
43903 = (enum omp_requires) (omp_requires_mask | this_req);
43904 continue;
43905 }
43906 }
43907 break;
43908 }
43909 cp_parser_require_pragma_eol (parser, pragma_tok);
43910
43911 if (new_req == 0)
43912 error_at (loc, "%<pragma omp requires%> requires at least one clause");
43913 return false;
43914 }
43915
43916
43917 /* OpenMP 4.5:
43918 #pragma omp taskloop taskloop-clause[optseq] new-line
43919 for-loop
43920
43921 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
43922 for-loop */
43923
43924 #define OMP_TASKLOOP_CLAUSE_MASK \
43925 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
43926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
43930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
43931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
43932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
43934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
43936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
43937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
43938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
43939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
43942
43943 static tree
43944 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
43945 char *p_name, omp_clause_mask mask, tree *cclauses,
43946 bool *if_p)
43947 {
43948 tree clauses, sb, ret;
43949 unsigned int save;
43950 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43951
43952 strcat (p_name, " taskloop");
43953 mask |= OMP_TASKLOOP_CLAUSE_MASK;
43954 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
43955 clause. */
43956 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
43957 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
43958
43959 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43960 {
43961 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43962 const char *p = IDENTIFIER_POINTER (id);
43963
43964 if (strcmp (p, "simd") == 0)
43965 {
43966 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43967 if (cclauses == NULL)
43968 cclauses = cclauses_buf;
43969
43970 cp_lexer_consume_token (parser->lexer);
43971 if (!flag_openmp) /* flag_openmp_simd */
43972 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43973 cclauses, if_p);
43974 sb = begin_omp_structured_block ();
43975 save = cp_parser_begin_omp_structured_block (parser);
43976 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43977 cclauses, if_p);
43978 cp_parser_end_omp_structured_block (parser, save);
43979 tree body = finish_omp_structured_block (sb);
43980 if (ret == NULL)
43981 return ret;
43982 ret = make_node (OMP_TASKLOOP);
43983 TREE_TYPE (ret) = void_type_node;
43984 OMP_FOR_BODY (ret) = body;
43985 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
43986 SET_EXPR_LOCATION (ret, loc);
43987 add_stmt (ret);
43988 return ret;
43989 }
43990 }
43991 if (!flag_openmp) /* flag_openmp_simd */
43992 {
43993 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43994 return NULL_TREE;
43995 }
43996
43997 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43998 cclauses == NULL);
43999 if (cclauses)
44000 {
44001 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
44002 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
44003 }
44004
44005 keep_next_level (true);
44006 sb = begin_omp_structured_block ();
44007 save = cp_parser_begin_omp_structured_block (parser);
44008
44009 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
44010 if_p);
44011
44012 cp_parser_end_omp_structured_block (parser, save);
44013 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
44014
44015 return ret;
44016 }
44017
44018
44019 /* OpenACC 2.0:
44020 # pragma acc routine oacc-routine-clause[optseq] new-line
44021 function-definition
44022
44023 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
44024 */
44025
44026 #define OACC_ROUTINE_CLAUSE_MASK \
44027 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
44028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
44029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
44030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
44031
44032
44033 /* Parse the OpenACC routine pragma. This has an optional '( name )'
44034 component, which must resolve to a declared namespace-scope
44035 function. The clauses are either processed directly (for a named
44036 function), or defered until the immediatley following declaration
44037 is parsed. */
44038
44039 static void
44040 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
44041 enum pragma_context context)
44042 {
44043 gcc_checking_assert (context == pragma_external);
44044 /* The checking for "another pragma following this one" in the "no optional
44045 '( name )'" case makes sure that we dont re-enter. */
44046 gcc_checking_assert (parser->oacc_routine == NULL);
44047
44048 cp_oacc_routine_data data;
44049 data.error_seen = false;
44050 data.fndecl_seen = false;
44051 data.tokens = vNULL;
44052 data.clauses = NULL_TREE;
44053 data.loc = pragma_tok->location;
44054 /* It is safe to take the address of a local variable; it will only be
44055 used while this scope is live. */
44056 parser->oacc_routine = &data;
44057
44058 /* Look for optional '( name )'. */
44059 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
44060 {
44061 matching_parens parens;
44062 parens.consume_open (parser); /* '(' */
44063
44064 /* We parse the name as an id-expression. If it resolves to
44065 anything other than a non-overloaded function at namespace
44066 scope, it's an error. */
44067 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
44068 tree name = cp_parser_id_expression (parser,
44069 /*template_keyword_p=*/false,
44070 /*check_dependency_p=*/false,
44071 /*template_p=*/NULL,
44072 /*declarator_p=*/false,
44073 /*optional_p=*/false);
44074 tree decl = (identifier_p (name)
44075 ? cp_parser_lookup_name_simple (parser, name, name_loc)
44076 : name);
44077 if (name != error_mark_node && decl == error_mark_node)
44078 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
44079
44080 if (decl == error_mark_node
44081 || !parens.require_close (parser))
44082 {
44083 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44084 parser->oacc_routine = NULL;
44085 return;
44086 }
44087
44088 data.clauses
44089 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
44090 "#pragma acc routine",
44091 cp_lexer_peek_token (parser->lexer));
44092 /* The clauses are in reverse order; fix that to make later diagnostic
44093 emission easier. */
44094 data.clauses = nreverse (data.clauses);
44095
44096 if (decl && is_overloaded_fn (decl)
44097 && (TREE_CODE (decl) != FUNCTION_DECL
44098 || DECL_FUNCTION_TEMPLATE_P (decl)))
44099 {
44100 error_at (name_loc,
44101 "%<#pragma acc routine%> names a set of overloads");
44102 parser->oacc_routine = NULL;
44103 return;
44104 }
44105
44106 /* Perhaps we should use the same rule as declarations in different
44107 namespaces? */
44108 if (!DECL_NAMESPACE_SCOPE_P (decl))
44109 {
44110 error_at (name_loc,
44111 "%qD does not refer to a namespace scope function", decl);
44112 parser->oacc_routine = NULL;
44113 return;
44114 }
44115
44116 if (TREE_CODE (decl) != FUNCTION_DECL)
44117 {
44118 error_at (name_loc, "%qD does not refer to a function", decl);
44119 parser->oacc_routine = NULL;
44120 return;
44121 }
44122
44123 cp_finalize_oacc_routine (parser, decl, false);
44124 parser->oacc_routine = NULL;
44125 }
44126 else /* No optional '( name )'. */
44127 {
44128 /* Store away all pragma tokens. */
44129 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
44130 cp_lexer_consume_token (parser->lexer);
44131 cp_parser_require_pragma_eol (parser, pragma_tok);
44132 struct cp_token_cache *cp
44133 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
44134 parser->oacc_routine->tokens.safe_push (cp);
44135
44136 /* Emit a helpful diagnostic if there's another pragma following this
44137 one. */
44138 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
44139 {
44140 cp_ensure_no_oacc_routine (parser);
44141 data.tokens.release ();
44142 /* ..., and then just keep going. */
44143 return;
44144 }
44145
44146 /* We only have to consider the pragma_external case here. */
44147 cp_parser_declaration (parser, NULL_TREE);
44148 if (parser->oacc_routine
44149 && !parser->oacc_routine->fndecl_seen)
44150 cp_ensure_no_oacc_routine (parser);
44151 else
44152 parser->oacc_routine = NULL;
44153 data.tokens.release ();
44154 }
44155 }
44156
44157 /* Finalize #pragma acc routine clauses after direct declarator has
44158 been parsed. */
44159
44160 static tree
44161 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
44162 {
44163 struct cp_token_cache *ce;
44164 cp_oacc_routine_data *data = parser->oacc_routine;
44165
44166 if (!data->error_seen && data->fndecl_seen)
44167 {
44168 error_at (data->loc,
44169 "%<#pragma acc routine%> not immediately followed by "
44170 "a single function declaration or definition");
44171 data->error_seen = true;
44172 }
44173 if (data->error_seen)
44174 return attrs;
44175
44176 gcc_checking_assert (data->tokens.length () == 1);
44177 ce = data->tokens[0];
44178
44179 cp_parser_push_lexer_for_tokens (parser, ce);
44180 parser->lexer->in_pragma = true;
44181 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
44182
44183 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
44184 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
44185 parser->oacc_routine->clauses
44186 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
44187 "#pragma acc routine", pragma_tok);
44188 /* The clauses are in reverse order; fix that to make later diagnostic
44189 emission easier. */
44190 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
44191 cp_parser_pop_lexer (parser);
44192 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
44193 fndecl_seen. */
44194
44195 return attrs;
44196 }
44197
44198 /* Apply any saved OpenACC routine clauses to a just-parsed
44199 declaration. */
44200
44201 static void
44202 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
44203 {
44204 if (__builtin_expect (parser->oacc_routine != NULL, 0))
44205 {
44206 /* Keep going if we're in error reporting mode. */
44207 if (parser->oacc_routine->error_seen
44208 || fndecl == error_mark_node)
44209 return;
44210
44211 if (parser->oacc_routine->fndecl_seen)
44212 {
44213 error_at (parser->oacc_routine->loc,
44214 "%<#pragma acc routine%> not immediately followed by"
44215 " a single function declaration or definition");
44216 parser->oacc_routine = NULL;
44217 return;
44218 }
44219 if (TREE_CODE (fndecl) != FUNCTION_DECL)
44220 {
44221 cp_ensure_no_oacc_routine (parser);
44222 return;
44223 }
44224
44225 int compatible
44226 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
44227 parser->oacc_routine->loc,
44228 "#pragma acc routine");
44229 if (compatible < 0)
44230 {
44231 parser->oacc_routine = NULL;
44232 return;
44233 }
44234 if (compatible > 0)
44235 {
44236 }
44237 else
44238 {
44239 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
44240 {
44241 error_at (parser->oacc_routine->loc,
44242 TREE_USED (fndecl)
44243 ? G_("%<#pragma acc routine%> must be applied before"
44244 " use")
44245 : G_("%<#pragma acc routine%> must be applied before"
44246 " definition"));
44247 parser->oacc_routine = NULL;
44248 return;
44249 }
44250
44251 /* Set the routine's level of parallelism. */
44252 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
44253 oacc_replace_fn_attrib (fndecl, dims);
44254
44255 /* Add an "omp declare target" attribute. */
44256 DECL_ATTRIBUTES (fndecl)
44257 = tree_cons (get_identifier ("omp declare target"),
44258 parser->oacc_routine->clauses,
44259 DECL_ATTRIBUTES (fndecl));
44260 }
44261
44262 /* Don't unset parser->oacc_routine here: we may still need it to
44263 diagnose wrong usage. But, remember that we've used this "#pragma acc
44264 routine". */
44265 parser->oacc_routine->fndecl_seen = true;
44266 }
44267 }
44268
44269 /* Main entry point to OpenMP statement pragmas. */
44270
44271 static void
44272 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44273 {
44274 tree stmt;
44275 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
44276 omp_clause_mask mask (0);
44277
44278 switch (cp_parser_pragma_kind (pragma_tok))
44279 {
44280 case PRAGMA_OACC_ATOMIC:
44281 cp_parser_omp_atomic (parser, pragma_tok, true);
44282 return;
44283 case PRAGMA_OACC_CACHE:
44284 stmt = cp_parser_oacc_cache (parser, pragma_tok);
44285 break;
44286 case PRAGMA_OACC_DATA:
44287 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
44288 break;
44289 case PRAGMA_OACC_ENTER_DATA:
44290 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
44291 break;
44292 case PRAGMA_OACC_EXIT_DATA:
44293 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
44294 break;
44295 case PRAGMA_OACC_HOST_DATA:
44296 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
44297 break;
44298 case PRAGMA_OACC_KERNELS:
44299 case PRAGMA_OACC_PARALLEL:
44300 case PRAGMA_OACC_SERIAL:
44301 strcpy (p_name, "#pragma acc");
44302 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
44303 break;
44304 case PRAGMA_OACC_LOOP:
44305 strcpy (p_name, "#pragma acc");
44306 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
44307 if_p);
44308 break;
44309 case PRAGMA_OACC_UPDATE:
44310 stmt = cp_parser_oacc_update (parser, pragma_tok);
44311 break;
44312 case PRAGMA_OACC_WAIT:
44313 stmt = cp_parser_oacc_wait (parser, pragma_tok);
44314 break;
44315 case PRAGMA_OMP_ALLOCATE:
44316 cp_parser_omp_allocate (parser, pragma_tok);
44317 return;
44318 case PRAGMA_OMP_ATOMIC:
44319 cp_parser_omp_atomic (parser, pragma_tok, false);
44320 return;
44321 case PRAGMA_OMP_CRITICAL:
44322 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
44323 break;
44324 case PRAGMA_OMP_DISTRIBUTE:
44325 strcpy (p_name, "#pragma omp");
44326 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
44327 if_p);
44328 break;
44329 case PRAGMA_OMP_FOR:
44330 strcpy (p_name, "#pragma omp");
44331 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
44332 if_p);
44333 break;
44334 case PRAGMA_OMP_LOOP:
44335 strcpy (p_name, "#pragma omp");
44336 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
44337 if_p);
44338 break;
44339 case PRAGMA_OMP_MASTER:
44340 strcpy (p_name, "#pragma omp");
44341 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
44342 if_p);
44343 break;
44344 case PRAGMA_OMP_PARALLEL:
44345 strcpy (p_name, "#pragma omp");
44346 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
44347 if_p);
44348 break;
44349 case PRAGMA_OMP_SECTIONS:
44350 strcpy (p_name, "#pragma omp");
44351 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
44352 break;
44353 case PRAGMA_OMP_SIMD:
44354 strcpy (p_name, "#pragma omp");
44355 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
44356 if_p);
44357 break;
44358 case PRAGMA_OMP_SINGLE:
44359 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
44360 break;
44361 case PRAGMA_OMP_TASK:
44362 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
44363 break;
44364 case PRAGMA_OMP_TASKGROUP:
44365 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
44366 break;
44367 case PRAGMA_OMP_TASKLOOP:
44368 strcpy (p_name, "#pragma omp");
44369 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
44370 if_p);
44371 break;
44372 case PRAGMA_OMP_TEAMS:
44373 strcpy (p_name, "#pragma omp");
44374 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
44375 if_p);
44376 break;
44377 default:
44378 gcc_unreachable ();
44379 }
44380
44381 protected_set_expr_location (stmt, pragma_tok->location);
44382 }
44383 \f
44384 /* Transactional Memory parsing routines. */
44385
44386 /* Parse a transaction attribute.
44387
44388 txn-attribute:
44389 attribute
44390 [ [ identifier ] ]
44391
44392 We use this instead of cp_parser_attributes_opt for transactions to avoid
44393 the pedwarn in C++98 mode. */
44394
44395 static tree
44396 cp_parser_txn_attribute_opt (cp_parser *parser)
44397 {
44398 cp_token *token;
44399 tree attr_name, attr = NULL;
44400
44401 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
44402 return cp_parser_attributes_opt (parser);
44403
44404 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
44405 return NULL_TREE;
44406 cp_lexer_consume_token (parser->lexer);
44407 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
44408 goto error1;
44409
44410 token = cp_lexer_peek_token (parser->lexer);
44411 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
44412 {
44413 token = cp_lexer_consume_token (parser->lexer);
44414
44415 attr_name = (token->type == CPP_KEYWORD
44416 /* For keywords, use the canonical spelling,
44417 not the parsed identifier. */
44418 ? ridpointers[(int) token->keyword]
44419 : token->u.value);
44420 attr = build_tree_list (attr_name, NULL_TREE);
44421 }
44422 else
44423 cp_parser_error (parser, "expected identifier");
44424
44425 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
44426 error1:
44427 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
44428 return attr;
44429 }
44430
44431 /* Parse a __transaction_atomic or __transaction_relaxed statement.
44432
44433 transaction-statement:
44434 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
44435 compound-statement
44436 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
44437 */
44438
44439 static tree
44440 cp_parser_transaction (cp_parser *parser, cp_token *token)
44441 {
44442 unsigned char old_in = parser->in_transaction;
44443 unsigned char this_in = 1, new_in;
44444 enum rid keyword = token->keyword;
44445 tree stmt, attrs, noex;
44446
44447 cp_lexer_consume_token (parser->lexer);
44448
44449 if (keyword == RID_TRANSACTION_RELAXED
44450 || keyword == RID_SYNCHRONIZED)
44451 this_in |= TM_STMT_ATTR_RELAXED;
44452 else
44453 {
44454 attrs = cp_parser_txn_attribute_opt (parser);
44455 if (attrs)
44456 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
44457 }
44458
44459 /* Parse a noexcept specification. */
44460 if (keyword == RID_ATOMIC_NOEXCEPT)
44461 noex = boolean_true_node;
44462 else if (keyword == RID_ATOMIC_CANCEL)
44463 {
44464 /* cancel-and-throw is unimplemented. */
44465 sorry ("%<atomic_cancel%>");
44466 noex = NULL_TREE;
44467 }
44468 else
44469 noex = cp_parser_noexcept_specification_opt (parser,
44470 CP_PARSER_FLAGS_NONE,
44471 /*require_constexpr=*/true,
44472 /*consumed_expr=*/NULL,
44473 /*return_cond=*/true);
44474
44475 /* Keep track if we're in the lexical scope of an outer transaction. */
44476 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
44477
44478 stmt = begin_transaction_stmt (token->location, NULL, this_in);
44479
44480 parser->in_transaction = new_in;
44481 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
44482 parser->in_transaction = old_in;
44483
44484 finish_transaction_stmt (stmt, NULL, this_in, noex);
44485
44486 return stmt;
44487 }
44488
44489 /* Parse a __transaction_atomic or __transaction_relaxed expression.
44490
44491 transaction-expression:
44492 __transaction_atomic txn-noexcept-spec[opt] ( expression )
44493 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
44494 */
44495
44496 static tree
44497 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
44498 {
44499 unsigned char old_in = parser->in_transaction;
44500 unsigned char this_in = 1;
44501 cp_token *token;
44502 tree expr, noex;
44503 bool noex_expr;
44504 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44505
44506 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
44507 || keyword == RID_TRANSACTION_RELAXED);
44508
44509 if (!flag_tm)
44510 error_at (loc,
44511 keyword == RID_TRANSACTION_RELAXED
44512 ? G_("%<__transaction_relaxed%> without transactional memory "
44513 "support enabled")
44514 : G_("%<__transaction_atomic%> without transactional memory "
44515 "support enabled"));
44516
44517 token = cp_parser_require_keyword (parser, keyword,
44518 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
44519 : RT_TRANSACTION_RELAXED));
44520 gcc_assert (token != NULL);
44521
44522 if (keyword == RID_TRANSACTION_RELAXED)
44523 this_in |= TM_STMT_ATTR_RELAXED;
44524
44525 /* Set this early. This might mean that we allow transaction_cancel in
44526 an expression that we find out later actually has to be a constexpr.
44527 However, we expect that cxx_constant_value will be able to deal with
44528 this; also, if the noexcept has no constexpr, then what we parse next
44529 really is a transaction's body. */
44530 parser->in_transaction = this_in;
44531
44532 /* Parse a noexcept specification. */
44533 noex = cp_parser_noexcept_specification_opt (parser,
44534 CP_PARSER_FLAGS_NONE,
44535 /*require_constexpr=*/false,
44536 &noex_expr,
44537 /*return_cond=*/true);
44538
44539 if (!noex || !noex_expr
44540 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
44541 {
44542 matching_parens parens;
44543 parens.require_open (parser);
44544
44545 expr = cp_parser_expression (parser);
44546 expr = finish_parenthesized_expr (expr);
44547
44548 parens.require_close (parser);
44549 }
44550 else
44551 {
44552 /* The only expression that is available got parsed for the noexcept
44553 already. noexcept is true then. */
44554 expr = noex;
44555 noex = boolean_true_node;
44556 }
44557
44558 expr = build_transaction_expr (token->location, expr, this_in, noex);
44559 parser->in_transaction = old_in;
44560
44561 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
44562 return error_mark_node;
44563
44564 return (flag_tm ? expr : error_mark_node);
44565 }
44566
44567 /* Parse a function-transaction-block.
44568
44569 function-transaction-block:
44570 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
44571 function-body
44572 __transaction_atomic txn-attribute[opt] function-try-block
44573 __transaction_relaxed ctor-initializer[opt] function-body
44574 __transaction_relaxed function-try-block
44575 */
44576
44577 static void
44578 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
44579 {
44580 unsigned char old_in = parser->in_transaction;
44581 unsigned char new_in = 1;
44582 tree compound_stmt, stmt, attrs;
44583 cp_token *token;
44584
44585 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
44586 || keyword == RID_TRANSACTION_RELAXED);
44587 token = cp_parser_require_keyword (parser, keyword,
44588 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
44589 : RT_TRANSACTION_RELAXED));
44590 gcc_assert (token != NULL);
44591
44592 if (keyword == RID_TRANSACTION_RELAXED)
44593 new_in |= TM_STMT_ATTR_RELAXED;
44594 else
44595 {
44596 attrs = cp_parser_txn_attribute_opt (parser);
44597 if (attrs)
44598 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
44599 }
44600
44601 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
44602
44603 parser->in_transaction = new_in;
44604
44605 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
44606 cp_parser_function_try_block (parser);
44607 else
44608 cp_parser_ctor_initializer_opt_and_function_body
44609 (parser, /*in_function_try_block=*/false);
44610
44611 parser->in_transaction = old_in;
44612
44613 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
44614 }
44615
44616 /* Parse a __transaction_cancel statement.
44617
44618 cancel-statement:
44619 __transaction_cancel txn-attribute[opt] ;
44620 __transaction_cancel txn-attribute[opt] throw-expression ;
44621
44622 ??? Cancel and throw is not yet implemented. */
44623
44624 static tree
44625 cp_parser_transaction_cancel (cp_parser *parser)
44626 {
44627 cp_token *token;
44628 bool is_outer = false;
44629 tree stmt, attrs;
44630
44631 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
44632 RT_TRANSACTION_CANCEL);
44633 gcc_assert (token != NULL);
44634
44635 attrs = cp_parser_txn_attribute_opt (parser);
44636 if (attrs)
44637 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
44638
44639 /* ??? Parse cancel-and-throw here. */
44640
44641 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
44642
44643 if (!flag_tm)
44644 {
44645 error_at (token->location, "%<__transaction_cancel%> without "
44646 "transactional memory support enabled");
44647 return error_mark_node;
44648 }
44649 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
44650 {
44651 error_at (token->location, "%<__transaction_cancel%> within a "
44652 "%<__transaction_relaxed%>");
44653 return error_mark_node;
44654 }
44655 else if (is_outer)
44656 {
44657 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
44658 && !is_tm_may_cancel_outer (current_function_decl))
44659 {
44660 error_at (token->location, "outer %<__transaction_cancel%> not "
44661 "within outer %<__transaction_atomic%>");
44662 error_at (token->location,
44663 " or a %<transaction_may_cancel_outer%> function");
44664 return error_mark_node;
44665 }
44666 }
44667 else if (parser->in_transaction == 0)
44668 {
44669 error_at (token->location, "%<__transaction_cancel%> not within "
44670 "%<__transaction_atomic%>");
44671 return error_mark_node;
44672 }
44673
44674 stmt = build_tm_abort_call (token->location, is_outer);
44675 add_stmt (stmt);
44676
44677 return stmt;
44678 }
44679 \f
44680 /* The parser. */
44681
44682 static GTY (()) cp_parser *the_parser;
44683
44684 \f
44685 /* Special handling for the first token or line in the file. The first
44686 thing in the file might be #pragma GCC pch_preprocess, which loads a
44687 PCH file, which is a GC collection point. So we need to handle this
44688 first pragma without benefit of an existing lexer structure.
44689
44690 Always returns one token to the caller in *FIRST_TOKEN. This is
44691 either the true first token of the file, or the first token after
44692 the initial pragma. */
44693
44694 static void
44695 cp_parser_initial_pragma (cp_token *first_token)
44696 {
44697 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
44698 return;
44699
44700 cp_lexer_get_preprocessor_token (0, first_token);
44701
44702 tree name = NULL;
44703 if (first_token->type == CPP_STRING)
44704 {
44705 name = first_token->u.value;
44706
44707 cp_lexer_get_preprocessor_token (0, first_token);
44708 }
44709
44710 /* Skip to the end of the pragma. */
44711 if (first_token->type != CPP_PRAGMA_EOL)
44712 {
44713 error_at (first_token->location,
44714 "malformed %<#pragma GCC pch_preprocess%>");
44715 do
44716 cp_lexer_get_preprocessor_token (0, first_token);
44717 while (first_token->type != CPP_PRAGMA_EOL);
44718 }
44719
44720 /* Now actually load the PCH file. */
44721 if (name)
44722 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
44723
44724 /* Read one more token to return to our caller. We have to do this
44725 after reading the PCH file in, since its pointers have to be
44726 live. */
44727 cp_lexer_get_preprocessor_token (0, first_token);
44728 }
44729
44730 /* Parse a pragma GCC ivdep. */
44731
44732 static bool
44733 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
44734 {
44735 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44736 return true;
44737 }
44738
44739 /* Parse a pragma GCC unroll. */
44740
44741 static unsigned short
44742 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
44743 {
44744 location_t location = cp_lexer_peek_token (parser->lexer)->location;
44745 tree expr = cp_parser_constant_expression (parser);
44746 unsigned short unroll;
44747 expr = maybe_constant_value (expr);
44748 HOST_WIDE_INT lunroll = 0;
44749 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
44750 || TREE_CODE (expr) != INTEGER_CST
44751 || (lunroll = tree_to_shwi (expr)) < 0
44752 || lunroll >= USHRT_MAX)
44753 {
44754 error_at (location, "%<#pragma GCC unroll%> requires an"
44755 " assignment-expression that evaluates to a non-negative"
44756 " integral constant less than %u", USHRT_MAX);
44757 unroll = 0;
44758 }
44759 else
44760 {
44761 unroll = (unsigned short)lunroll;
44762 if (unroll == 0)
44763 unroll = 1;
44764 }
44765 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44766 return unroll;
44767 }
44768
44769 /* Normal parsing of a pragma token. Here we can (and must) use the
44770 regular lexer. */
44771
44772 static bool
44773 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
44774 {
44775 cp_token *pragma_tok;
44776 unsigned int id;
44777 tree stmt;
44778 bool ret;
44779
44780 pragma_tok = cp_lexer_consume_token (parser->lexer);
44781 gcc_assert (pragma_tok->type == CPP_PRAGMA);
44782 parser->lexer->in_pragma = true;
44783
44784 id = cp_parser_pragma_kind (pragma_tok);
44785 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
44786 cp_ensure_no_omp_declare_simd (parser);
44787 switch (id)
44788 {
44789 case PRAGMA_GCC_PCH_PREPROCESS:
44790 error_at (pragma_tok->location,
44791 "%<#pragma GCC pch_preprocess%> must be first");
44792 break;
44793
44794 case PRAGMA_OMP_BARRIER:
44795 switch (context)
44796 {
44797 case pragma_compound:
44798 cp_parser_omp_barrier (parser, pragma_tok);
44799 return false;
44800 case pragma_stmt:
44801 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
44802 "used in compound statements", "omp barrier");
44803 break;
44804 default:
44805 goto bad_stmt;
44806 }
44807 break;
44808
44809 case PRAGMA_OMP_DEPOBJ:
44810 switch (context)
44811 {
44812 case pragma_compound:
44813 cp_parser_omp_depobj (parser, pragma_tok);
44814 return false;
44815 case pragma_stmt:
44816 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
44817 "used in compound statements", "omp depobj");
44818 break;
44819 default:
44820 goto bad_stmt;
44821 }
44822 break;
44823
44824 case PRAGMA_OMP_FLUSH:
44825 switch (context)
44826 {
44827 case pragma_compound:
44828 cp_parser_omp_flush (parser, pragma_tok);
44829 return false;
44830 case pragma_stmt:
44831 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
44832 "used in compound statements", "omp flush");
44833 break;
44834 default:
44835 goto bad_stmt;
44836 }
44837 break;
44838
44839 case PRAGMA_OMP_TASKWAIT:
44840 switch (context)
44841 {
44842 case pragma_compound:
44843 cp_parser_omp_taskwait (parser, pragma_tok);
44844 return false;
44845 case pragma_stmt:
44846 error_at (pragma_tok->location,
44847 "%<#pragma %s%> may only be used in compound statements",
44848 "omp taskwait");
44849 break;
44850 default:
44851 goto bad_stmt;
44852 }
44853 break;
44854
44855 case PRAGMA_OMP_TASKYIELD:
44856 switch (context)
44857 {
44858 case pragma_compound:
44859 cp_parser_omp_taskyield (parser, pragma_tok);
44860 return false;
44861 case pragma_stmt:
44862 error_at (pragma_tok->location,
44863 "%<#pragma %s%> may only be used in compound statements",
44864 "omp taskyield");
44865 break;
44866 default:
44867 goto bad_stmt;
44868 }
44869 break;
44870
44871 case PRAGMA_OMP_CANCEL:
44872 switch (context)
44873 {
44874 case pragma_compound:
44875 cp_parser_omp_cancel (parser, pragma_tok);
44876 return false;
44877 case pragma_stmt:
44878 error_at (pragma_tok->location,
44879 "%<#pragma %s%> may only be used in compound statements",
44880 "omp cancel");
44881 break;
44882 default:
44883 goto bad_stmt;
44884 }
44885 break;
44886
44887 case PRAGMA_OMP_CANCELLATION_POINT:
44888 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
44889 return false;
44890
44891 case PRAGMA_OMP_THREADPRIVATE:
44892 cp_parser_omp_threadprivate (parser, pragma_tok);
44893 return false;
44894
44895 case PRAGMA_OMP_DECLARE:
44896 return cp_parser_omp_declare (parser, pragma_tok, context);
44897
44898 case PRAGMA_OACC_DECLARE:
44899 cp_parser_oacc_declare (parser, pragma_tok);
44900 return false;
44901
44902 case PRAGMA_OACC_ENTER_DATA:
44903 if (context == pragma_stmt)
44904 {
44905 error_at (pragma_tok->location,
44906 "%<#pragma %s%> may only be used in compound statements",
44907 "acc enter data");
44908 break;
44909 }
44910 else if (context != pragma_compound)
44911 goto bad_stmt;
44912 cp_parser_omp_construct (parser, pragma_tok, if_p);
44913 return true;
44914
44915 case PRAGMA_OACC_EXIT_DATA:
44916 if (context == pragma_stmt)
44917 {
44918 error_at (pragma_tok->location,
44919 "%<#pragma %s%> may only be used in compound statements",
44920 "acc exit data");
44921 break;
44922 }
44923 else if (context != pragma_compound)
44924 goto bad_stmt;
44925 cp_parser_omp_construct (parser, pragma_tok, if_p);
44926 return true;
44927
44928 case PRAGMA_OACC_ROUTINE:
44929 if (context != pragma_external)
44930 {
44931 error_at (pragma_tok->location,
44932 "%<#pragma acc routine%> must be at file scope");
44933 break;
44934 }
44935 cp_parser_oacc_routine (parser, pragma_tok, context);
44936 return false;
44937
44938 case PRAGMA_OACC_UPDATE:
44939 if (context == pragma_stmt)
44940 {
44941 error_at (pragma_tok->location,
44942 "%<#pragma %s%> may only be used in compound statements",
44943 "acc update");
44944 break;
44945 }
44946 else if (context != pragma_compound)
44947 goto bad_stmt;
44948 cp_parser_omp_construct (parser, pragma_tok, if_p);
44949 return true;
44950
44951 case PRAGMA_OACC_WAIT:
44952 if (context == pragma_stmt)
44953 {
44954 error_at (pragma_tok->location,
44955 "%<#pragma %s%> may only be used in compound statements",
44956 "acc wait");
44957 break;
44958 }
44959 else if (context != pragma_compound)
44960 goto bad_stmt;
44961 cp_parser_omp_construct (parser, pragma_tok, if_p);
44962 return true;
44963 case PRAGMA_OMP_ALLOCATE:
44964 cp_parser_omp_allocate (parser, pragma_tok);
44965 return false;
44966 case PRAGMA_OACC_ATOMIC:
44967 case PRAGMA_OACC_CACHE:
44968 case PRAGMA_OACC_DATA:
44969 case PRAGMA_OACC_HOST_DATA:
44970 case PRAGMA_OACC_KERNELS:
44971 case PRAGMA_OACC_LOOP:
44972 case PRAGMA_OACC_PARALLEL:
44973 case PRAGMA_OACC_SERIAL:
44974 case PRAGMA_OMP_ATOMIC:
44975 case PRAGMA_OMP_CRITICAL:
44976 case PRAGMA_OMP_DISTRIBUTE:
44977 case PRAGMA_OMP_FOR:
44978 case PRAGMA_OMP_LOOP:
44979 case PRAGMA_OMP_MASTER:
44980 case PRAGMA_OMP_PARALLEL:
44981 case PRAGMA_OMP_SECTIONS:
44982 case PRAGMA_OMP_SIMD:
44983 case PRAGMA_OMP_SINGLE:
44984 case PRAGMA_OMP_TASK:
44985 case PRAGMA_OMP_TASKGROUP:
44986 case PRAGMA_OMP_TASKLOOP:
44987 case PRAGMA_OMP_TEAMS:
44988 if (context != pragma_stmt && context != pragma_compound)
44989 goto bad_stmt;
44990 stmt = push_omp_privatization_clauses (false);
44991 cp_parser_omp_construct (parser, pragma_tok, if_p);
44992 pop_omp_privatization_clauses (stmt);
44993 return true;
44994
44995 case PRAGMA_OMP_REQUIRES:
44996 if (context != pragma_external)
44997 {
44998 error_at (pragma_tok->location,
44999 "%<#pragma omp requires%> may only be used at file or "
45000 "namespace scope");
45001 break;
45002 }
45003 return cp_parser_omp_requires (parser, pragma_tok);
45004
45005 case PRAGMA_OMP_ORDERED:
45006 if (context != pragma_stmt && context != pragma_compound)
45007 goto bad_stmt;
45008 stmt = push_omp_privatization_clauses (false);
45009 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
45010 pop_omp_privatization_clauses (stmt);
45011 return ret;
45012
45013 case PRAGMA_OMP_TARGET:
45014 if (context != pragma_stmt && context != pragma_compound)
45015 goto bad_stmt;
45016 stmt = push_omp_privatization_clauses (false);
45017 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
45018 pop_omp_privatization_clauses (stmt);
45019 return ret;
45020
45021 case PRAGMA_OMP_END_DECLARE_TARGET:
45022 cp_parser_omp_end_declare_target (parser, pragma_tok);
45023 return false;
45024
45025 case PRAGMA_OMP_SCAN:
45026 error_at (pragma_tok->location,
45027 "%<#pragma omp scan%> may only be used in "
45028 "a loop construct with %<inscan%> %<reduction%> clause");
45029 break;
45030
45031 case PRAGMA_OMP_SECTION:
45032 error_at (pragma_tok->location,
45033 "%<#pragma omp section%> may only be used in "
45034 "%<#pragma omp sections%> construct");
45035 break;
45036
45037 case PRAGMA_IVDEP:
45038 {
45039 if (context == pragma_external)
45040 {
45041 error_at (pragma_tok->location,
45042 "%<#pragma GCC ivdep%> must be inside a function");
45043 break;
45044 }
45045 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
45046 unsigned short unroll;
45047 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
45048 if (tok->type == CPP_PRAGMA
45049 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
45050 {
45051 tok = cp_lexer_consume_token (parser->lexer);
45052 unroll = cp_parser_pragma_unroll (parser, tok);
45053 tok = cp_lexer_peek_token (the_parser->lexer);
45054 }
45055 else
45056 unroll = 0;
45057 if (tok->type != CPP_KEYWORD
45058 || (tok->keyword != RID_FOR
45059 && tok->keyword != RID_WHILE
45060 && tok->keyword != RID_DO))
45061 {
45062 cp_parser_error (parser, "for, while or do statement expected");
45063 return false;
45064 }
45065 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
45066 return true;
45067 }
45068
45069 case PRAGMA_UNROLL:
45070 {
45071 if (context == pragma_external)
45072 {
45073 error_at (pragma_tok->location,
45074 "%<#pragma GCC unroll%> must be inside a function");
45075 break;
45076 }
45077 const unsigned short unroll
45078 = cp_parser_pragma_unroll (parser, pragma_tok);
45079 bool ivdep;
45080 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
45081 if (tok->type == CPP_PRAGMA
45082 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
45083 {
45084 tok = cp_lexer_consume_token (parser->lexer);
45085 ivdep = cp_parser_pragma_ivdep (parser, tok);
45086 tok = cp_lexer_peek_token (the_parser->lexer);
45087 }
45088 else
45089 ivdep = false;
45090 if (tok->type != CPP_KEYWORD
45091 || (tok->keyword != RID_FOR
45092 && tok->keyword != RID_WHILE
45093 && tok->keyword != RID_DO))
45094 {
45095 cp_parser_error (parser, "for, while or do statement expected");
45096 return false;
45097 }
45098 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
45099 return true;
45100 }
45101
45102 default:
45103 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
45104 c_invoke_pragma_handler (id);
45105 break;
45106
45107 bad_stmt:
45108 cp_parser_error (parser, "expected declaration specifiers");
45109 break;
45110 }
45111
45112 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45113 return false;
45114 }
45115
45116 /* The interface the pragma parsers have to the lexer. */
45117
45118 enum cpp_ttype
45119 pragma_lex (tree *value, location_t *loc)
45120 {
45121 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
45122 enum cpp_ttype ret = tok->type;
45123
45124 *value = tok->u.value;
45125 if (loc)
45126 *loc = tok->location;
45127
45128 if (ret == CPP_PRAGMA_EOL)
45129 ret = CPP_EOF;
45130 else if (ret == CPP_STRING)
45131 *value = cp_parser_string_literal (the_parser, false, false);
45132 else
45133 {
45134 if (ret == CPP_KEYWORD)
45135 ret = CPP_NAME;
45136 cp_lexer_consume_token (the_parser->lexer);
45137 }
45138
45139 return ret;
45140 }
45141
45142 \f
45143 /* External interface. */
45144
45145 /* Parse one entire translation unit. */
45146
45147 void
45148 c_parse_file (void)
45149 {
45150 static bool already_called = false;
45151
45152 if (already_called)
45153 fatal_error (input_location,
45154 "multi-source compilation not implemented for C++");
45155 already_called = true;
45156
45157 /* cp_lexer_new_main is called before doing any GC allocation
45158 because tokenization might load a PCH file. */
45159 cp_lexer *lexer = cp_lexer_new_main ();
45160
45161 the_parser = cp_parser_new (lexer);
45162
45163 cp_parser_translation_unit (the_parser);
45164 class_decl_loc_t::diag_mismatched_tags ();
45165
45166 the_parser = NULL;
45167
45168 finish_translation_unit ();
45169 }
45170
45171 /* Create an identifier for a generic parameter type (a synthesized
45172 template parameter implied by `auto' or a concept identifier). */
45173
45174 static GTY(()) int generic_parm_count;
45175 static tree
45176 make_generic_type_name ()
45177 {
45178 char buf[32];
45179 sprintf (buf, "auto:%d", ++generic_parm_count);
45180 return get_identifier (buf);
45181 }
45182
45183 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
45184 (creating a new template parameter list if necessary). Returns the newly
45185 created template type parm. */
45186
45187 static tree
45188 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
45189 {
45190 /* A requires-clause is not a function and cannot have placeholders. */
45191 if (current_binding_level->kind == sk_block)
45192 {
45193 error ("placeholder type not allowed in this context");
45194 return error_mark_node;
45195 }
45196
45197 gcc_assert (current_binding_level->kind == sk_function_parms);
45198
45199 /* We are either continuing a function template that already contains implicit
45200 template parameters, creating a new fully-implicit function template, or
45201 extending an existing explicit function template with implicit template
45202 parameters. */
45203
45204 cp_binding_level *const entry_scope = current_binding_level;
45205
45206 bool become_template = false;
45207 cp_binding_level *parent_scope = 0;
45208
45209 if (parser->implicit_template_scope)
45210 {
45211 gcc_assert (parser->implicit_template_parms);
45212
45213 current_binding_level = parser->implicit_template_scope;
45214 }
45215 else
45216 {
45217 /* Roll back to the existing template parameter scope (in the case of
45218 extending an explicit function template) or introduce a new template
45219 parameter scope ahead of the function parameter scope (or class scope
45220 in the case of out-of-line member definitions). The function scope is
45221 added back after template parameter synthesis below. */
45222
45223 cp_binding_level *scope = entry_scope;
45224
45225 while (scope->kind == sk_function_parms)
45226 {
45227 parent_scope = scope;
45228 scope = scope->level_chain;
45229 }
45230 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
45231 {
45232 /* If not defining a class, then any class scope is a scope level in
45233 an out-of-line member definition. In this case simply wind back
45234 beyond the first such scope to inject the template parameter list.
45235 Otherwise wind back to the class being defined. The latter can
45236 occur in class member friend declarations such as:
45237
45238 class A {
45239 void foo (auto);
45240 };
45241 class B {
45242 friend void A::foo (auto);
45243 };
45244
45245 The template parameter list synthesized for the friend declaration
45246 must be injected in the scope of 'B'. This can also occur in
45247 erroneous cases such as:
45248
45249 struct A {
45250 struct B {
45251 void foo (auto);
45252 };
45253 void B::foo (auto) {}
45254 };
45255
45256 Here the attempted definition of 'B::foo' within 'A' is ill-formed
45257 but, nevertheless, the template parameter list synthesized for the
45258 declarator should be injected into the scope of 'A' as if the
45259 ill-formed template was specified explicitly. */
45260
45261 while (scope->kind == sk_class && !scope->defining_class_p)
45262 {
45263 parent_scope = scope;
45264 scope = scope->level_chain;
45265 }
45266 }
45267
45268 current_binding_level = scope;
45269
45270 if (scope->kind != sk_template_parms
45271 || !function_being_declared_is_template_p (parser))
45272 {
45273 /* Introduce a new template parameter list for implicit template
45274 parameters. */
45275
45276 become_template = true;
45277
45278 parser->implicit_template_scope
45279 = begin_scope (sk_template_parms, NULL);
45280
45281 ++processing_template_decl;
45282
45283 parser->fully_implicit_function_template_p = true;
45284 ++parser->num_template_parameter_lists;
45285 }
45286 else
45287 {
45288 /* Synthesize implicit template parameters at the end of the explicit
45289 template parameter list. */
45290
45291 gcc_assert (current_template_parms);
45292
45293 parser->implicit_template_scope = scope;
45294
45295 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
45296 parser->implicit_template_parms
45297 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
45298 }
45299 }
45300
45301 /* Synthesize a new template parameter and track the current template
45302 parameter chain with implicit_template_parms. */
45303
45304 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
45305 tree synth_id = make_generic_type_name ();
45306 tree synth_tmpl_parm;
45307 bool non_type = false;
45308
45309 /* Synthesize the type template parameter. */
45310 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
45311 synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
45312
45313 /* Attach the constraint to the parm before processing. */
45314 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
45315 TREE_TYPE (node) = constr;
45316 tree new_parm
45317 = process_template_parm (parser->implicit_template_parms,
45318 input_location,
45319 node,
45320 /*non_type=*/non_type,
45321 /*param_pack=*/false);
45322
45323 /* Mark the synthetic declaration "virtual". This is used when
45324 comparing template-heads to determine if whether an abbreviated
45325 function template is equivalent to an explicit template.
45326
45327 Note that DECL_ARTIFICIAL is used elsewhere for template parameters. */
45328 DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
45329
45330 // Chain the new parameter to the list of implicit parameters.
45331 if (parser->implicit_template_parms)
45332 parser->implicit_template_parms
45333 = TREE_CHAIN (parser->implicit_template_parms);
45334 else
45335 parser->implicit_template_parms = new_parm;
45336
45337 tree new_decl = get_local_decls ();
45338 if (non_type)
45339 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
45340 new_decl = DECL_INITIAL (new_decl);
45341
45342 /* If creating a fully implicit function template, start the new implicit
45343 template parameter list with this synthesized type, otherwise grow the
45344 current template parameter list. */
45345
45346 if (become_template)
45347 {
45348 parent_scope->level_chain = current_binding_level;
45349
45350 tree new_parms = make_tree_vec (1);
45351 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
45352 current_template_parms = tree_cons (size_int (processing_template_decl),
45353 new_parms, current_template_parms);
45354 }
45355 else
45356 {
45357 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
45358 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
45359 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
45360 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
45361 }
45362
45363 /* If the new parameter was constrained, we need to add that to the
45364 constraints in the template parameter list. */
45365 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
45366 {
45367 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
45368 reqs = combine_constraint_expressions (reqs, req);
45369 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
45370 }
45371
45372 current_binding_level = entry_scope;
45373
45374 return new_decl;
45375 }
45376
45377 /* Finish the declaration of a fully implicit function template. Such a
45378 template has no explicit template parameter list so has not been through the
45379 normal template head and tail processing. synthesize_implicit_template_parm
45380 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
45381 provided if the declaration is a class member such that its template
45382 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
45383 form is returned. Otherwise NULL_TREE is returned. */
45384
45385 static tree
45386 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
45387 {
45388 gcc_assert (parser->fully_implicit_function_template_p);
45389
45390 if (member_decl_opt && member_decl_opt != error_mark_node
45391 && DECL_VIRTUAL_P (member_decl_opt))
45392 {
45393 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
45394 "implicit templates may not be %<virtual%>");
45395 DECL_VIRTUAL_P (member_decl_opt) = false;
45396 }
45397
45398 if (member_decl_opt)
45399 member_decl_opt = finish_member_template_decl (member_decl_opt);
45400 end_template_decl ();
45401
45402 parser->fully_implicit_function_template_p = false;
45403 parser->implicit_template_parms = 0;
45404 parser->implicit_template_scope = 0;
45405 --parser->num_template_parameter_lists;
45406
45407 return member_decl_opt;
45408 }
45409
45410 /* Like finish_fully_implicit_template, but to be used in error
45411 recovery, rearranging scopes so that we restore the state we had
45412 before synthesize_implicit_template_parm inserted the implement
45413 template parms scope. */
45414
45415 static void
45416 abort_fully_implicit_template (cp_parser *parser)
45417 {
45418 cp_binding_level *return_to_scope = current_binding_level;
45419
45420 if (parser->implicit_template_scope
45421 && return_to_scope != parser->implicit_template_scope)
45422 {
45423 cp_binding_level *child = return_to_scope;
45424 for (cp_binding_level *scope = child->level_chain;
45425 scope != parser->implicit_template_scope;
45426 scope = child->level_chain)
45427 child = scope;
45428 child->level_chain = parser->implicit_template_scope->level_chain;
45429 parser->implicit_template_scope->level_chain = return_to_scope;
45430 current_binding_level = parser->implicit_template_scope;
45431 }
45432 else
45433 return_to_scope = return_to_scope->level_chain;
45434
45435 finish_fully_implicit_template (parser, NULL);
45436
45437 gcc_assert (current_binding_level == return_to_scope);
45438 }
45439
45440 /* Helper function for diagnostics that have complained about things
45441 being used with 'extern "C"' linkage.
45442
45443 Attempt to issue a note showing where the 'extern "C"' linkage began. */
45444
45445 void
45446 maybe_show_extern_c_location (void)
45447 {
45448 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
45449 inform (the_parser->innermost_linkage_specification_location,
45450 "%<extern \"C\"%> linkage started here");
45451 }
45452
45453 #include "gt-cp-parser.h"