4b2bca3fd1178bc92b256ff23683903a01af6a6e
[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. Defer access checking
25580 until the entire list has been seen, as per [class.access.general]. */
25581 push_deferring_access_checks (dk_deferred);
25582 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
25583 bases = cp_parser_base_clause (parser);
25584 else
25585 bases = NULL_TREE;
25586
25587 /* If we're really defining a class, process the base classes.
25588 If they're invalid, fail. */
25589 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25590 xref_basetypes (type, bases);
25591
25592 /* Now that all bases have been seen and attached to the class, check
25593 accessibility of the types named in the base-clause. This must be
25594 done relative to the class scope, so that we accept e.g.
25595
25596 struct A { protected: struct B {}; };
25597 struct C : A::B, A {}; // OK: A::B is accessible via base A
25598
25599 as per [class.access.general]. */
25600 if (type)
25601 pushclass (type);
25602 pop_to_parent_deferring_access_checks ();
25603 if (type)
25604 popclass ();
25605
25606 done:
25607 /* Leave the scope given by the nested-name-specifier. We will
25608 enter the class scope itself while processing the members. */
25609 if (pushed_scope)
25610 pop_scope (pushed_scope);
25611
25612 if (invalid_explicit_specialization_p)
25613 {
25614 end_specialization ();
25615 --parser->num_template_parameter_lists;
25616 }
25617
25618 if (type)
25619 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
25620 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
25621 CLASSTYPE_FINAL (type) = 1;
25622 out:
25623 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
25624 return type;
25625 }
25626
25627 /* Parse a class-key.
25628
25629 class-key:
25630 class
25631 struct
25632 union
25633
25634 Returns the kind of class-key specified, or none_type to indicate
25635 error. */
25636
25637 static enum tag_types
25638 cp_parser_class_key (cp_parser* parser)
25639 {
25640 cp_token *token;
25641 enum tag_types tag_type;
25642
25643 /* Look for the class-key. */
25644 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
25645 if (!token)
25646 return none_type;
25647
25648 /* Check to see if the TOKEN is a class-key. */
25649 tag_type = cp_parser_token_is_class_key (token);
25650 if (!tag_type)
25651 cp_parser_error (parser, "expected class-key");
25652 return tag_type;
25653 }
25654
25655 /* Parse a type-parameter-key.
25656
25657 type-parameter-key:
25658 class
25659 typename
25660 */
25661
25662 static void
25663 cp_parser_type_parameter_key (cp_parser* parser)
25664 {
25665 /* Look for the type-parameter-key. */
25666 enum tag_types tag_type = none_type;
25667 cp_token *token = cp_lexer_peek_token (parser->lexer);
25668 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
25669 {
25670 cp_lexer_consume_token (parser->lexer);
25671 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
25672 /* typename is not allowed in a template template parameter
25673 by the standard until C++17. */
25674 pedwarn (token->location, OPT_Wpedantic,
25675 "ISO C++ forbids typename key in template template parameter;"
25676 " use %<-std=c++17%> or %<-std=gnu++17%>");
25677 }
25678 else
25679 cp_parser_error (parser, "expected %<class%> or %<typename%>");
25680
25681 return;
25682 }
25683
25684 /* Parse an (optional) member-specification.
25685
25686 member-specification:
25687 member-declaration member-specification [opt]
25688 access-specifier : member-specification [opt] */
25689
25690 static void
25691 cp_parser_member_specification_opt (cp_parser* parser)
25692 {
25693 while (true)
25694 {
25695 cp_token *token;
25696 enum rid keyword;
25697
25698 /* Peek at the next token. */
25699 token = cp_lexer_peek_token (parser->lexer);
25700 /* If it's a `}', or EOF then we've seen all the members. */
25701 if (token->type == CPP_CLOSE_BRACE
25702 || token->type == CPP_EOF
25703 || token->type == CPP_PRAGMA_EOL)
25704 break;
25705
25706 /* See if this token is a keyword. */
25707 keyword = token->keyword;
25708 switch (keyword)
25709 {
25710 case RID_PUBLIC:
25711 case RID_PROTECTED:
25712 case RID_PRIVATE:
25713 /* Consume the access-specifier. */
25714 cp_lexer_consume_token (parser->lexer);
25715 /* Remember which access-specifier is active. */
25716 current_access_specifier = token->u.value;
25717 /* Look for the `:'. */
25718 cp_parser_require (parser, CPP_COLON, RT_COLON);
25719 break;
25720
25721 default:
25722 /* Accept #pragmas at class scope. */
25723 if (token->type == CPP_PRAGMA)
25724 {
25725 cp_parser_pragma (parser, pragma_member, NULL);
25726 break;
25727 }
25728
25729 /* Otherwise, the next construction must be a
25730 member-declaration. */
25731 cp_parser_member_declaration (parser);
25732 }
25733 }
25734 }
25735
25736 /* Parse a member-declaration.
25737
25738 member-declaration:
25739 decl-specifier-seq [opt] member-declarator-list [opt] ;
25740 function-definition ; [opt]
25741 :: [opt] nested-name-specifier template [opt] unqualified-id ;
25742 using-declaration
25743 template-declaration
25744 alias-declaration
25745
25746 member-declarator-list:
25747 member-declarator
25748 member-declarator-list , member-declarator
25749
25750 member-declarator:
25751 declarator pure-specifier [opt]
25752 declarator constant-initializer [opt]
25753 identifier [opt] : constant-expression
25754
25755 GNU Extensions:
25756
25757 member-declaration:
25758 __extension__ member-declaration
25759
25760 member-declarator:
25761 declarator attributes [opt] pure-specifier [opt]
25762 declarator attributes [opt] constant-initializer [opt]
25763 identifier [opt] attributes [opt] : constant-expression
25764
25765 C++0x Extensions:
25766
25767 member-declaration:
25768 static_assert-declaration */
25769
25770 static void
25771 cp_parser_member_declaration (cp_parser* parser)
25772 {
25773 cp_decl_specifier_seq decl_specifiers;
25774 tree prefix_attributes;
25775 tree decl;
25776 int declares_class_or_enum;
25777 bool friend_p;
25778 cp_token *token = NULL;
25779 cp_token *decl_spec_token_start = NULL;
25780 cp_token *initializer_token_start = NULL;
25781 int saved_pedantic;
25782 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
25783
25784 /* Check for the `__extension__' keyword. */
25785 if (cp_parser_extension_opt (parser, &saved_pedantic))
25786 {
25787 /* Recurse. */
25788 cp_parser_member_declaration (parser);
25789 /* Restore the old value of the PEDANTIC flag. */
25790 pedantic = saved_pedantic;
25791
25792 return;
25793 }
25794
25795 /* Check for a template-declaration. */
25796 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25797 {
25798 /* An explicit specialization here is an error condition, and we
25799 expect the specialization handler to detect and report this. */
25800 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
25801 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
25802 cp_parser_explicit_specialization (parser);
25803 else
25804 cp_parser_template_declaration (parser, /*member_p=*/true);
25805
25806 return;
25807 }
25808 /* Check for a template introduction. */
25809 else if (cp_parser_template_declaration_after_export (parser, true))
25810 return;
25811
25812 /* Check for a using-declaration. */
25813 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25814 {
25815 if (cxx_dialect < cxx11)
25816 /* Parse the using-declaration. */
25817 cp_parser_using_declaration (parser, /*access_declaration_p=*/false);
25818 else if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_ENUM))
25819 cp_parser_using_enum (parser);
25820 else
25821 {
25822 tree decl;
25823 bool alias_decl_expected;
25824 cp_parser_parse_tentatively (parser);
25825 decl = cp_parser_alias_declaration (parser);
25826 /* Note that if we actually see the '=' token after the
25827 identifier, cp_parser_alias_declaration commits the
25828 tentative parse. In that case, we really expect an
25829 alias-declaration. Otherwise, we expect a using
25830 declaration. */
25831 alias_decl_expected =
25832 !cp_parser_uncommitted_to_tentative_parse_p (parser);
25833 cp_parser_parse_definitely (parser);
25834
25835 if (alias_decl_expected)
25836 finish_member_declaration (decl);
25837 else
25838 cp_parser_using_declaration (parser,
25839 /*access_declaration_p=*/false);
25840 }
25841 return;
25842 }
25843
25844 /* Check for @defs. */
25845 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
25846 {
25847 tree ivar, member;
25848 tree ivar_chains = cp_parser_objc_defs_expression (parser);
25849 ivar = ivar_chains;
25850 while (ivar)
25851 {
25852 member = ivar;
25853 ivar = TREE_CHAIN (member);
25854 TREE_CHAIN (member) = NULL_TREE;
25855 finish_member_declaration (member);
25856 }
25857 return;
25858 }
25859
25860 /* If the next token is `static_assert' we have a static assertion. */
25861 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
25862 {
25863 cp_parser_static_assert (parser, /*member_p=*/true);
25864 return;
25865 }
25866
25867 parser->colon_corrects_to_scope_p = false;
25868
25869 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
25870 goto out;
25871
25872 /* Parse the decl-specifier-seq. */
25873 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25874 cp_parser_decl_specifier_seq (parser,
25875 (CP_PARSER_FLAGS_OPTIONAL
25876 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
25877 &decl_specifiers,
25878 &declares_class_or_enum);
25879 /* Check for an invalid type-name. */
25880 if (!decl_specifiers.any_type_specifiers_p
25881 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25882 goto out;
25883 /* If there is no declarator, then the decl-specifier-seq should
25884 specify a type. */
25885 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25886 {
25887 /* If there was no decl-specifier-seq, and the next token is a
25888 `;', then we have something like:
25889
25890 struct S { ; };
25891
25892 [class.mem]
25893
25894 Each member-declaration shall declare at least one member
25895 name of the class. */
25896 if (!decl_specifiers.any_specifiers_p)
25897 {
25898 cp_token *token = cp_lexer_peek_token (parser->lexer);
25899 if (!in_system_header_at (token->location))
25900 {
25901 gcc_rich_location richloc (token->location);
25902 richloc.add_fixit_remove ();
25903 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
25904 }
25905 }
25906 else
25907 {
25908 /* See if this declaration is a friend. */
25909 friend_p = cp_parser_friend_p (&decl_specifiers);
25910 /* If there were decl-specifiers, check to see if there was
25911 a class-declaration. */
25912 tree type = check_tag_decl (&decl_specifiers,
25913 /*explicit_type_instantiation_p=*/false);
25914 /* Nested classes have already been added to the class, but
25915 a `friend' needs to be explicitly registered. */
25916 if (friend_p)
25917 {
25918 /* If the `friend' keyword was present, the friend must
25919 be introduced with a class-key. */
25920 if (!declares_class_or_enum && cxx_dialect < cxx11)
25921 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
25922 "in C++03 a class-key must be used "
25923 "when declaring a friend");
25924 /* In this case:
25925
25926 template <typename T> struct A {
25927 friend struct A<T>::B;
25928 };
25929
25930 A<T>::B will be represented by a TYPENAME_TYPE, and
25931 therefore not recognized by check_tag_decl. */
25932 if (!type)
25933 {
25934 type = decl_specifiers.type;
25935 if (type && TREE_CODE (type) == TYPE_DECL)
25936 type = TREE_TYPE (type);
25937 }
25938 if (!type || !TYPE_P (type))
25939 error_at (decl_spec_token_start->location,
25940 "friend declaration does not name a class or "
25941 "function");
25942 else
25943 make_friend_class (current_class_type, type,
25944 /*complain=*/true);
25945 }
25946 /* If there is no TYPE, an error message will already have
25947 been issued. */
25948 else if (!type || type == error_mark_node)
25949 ;
25950 /* An anonymous aggregate has to be handled specially; such
25951 a declaration really declares a data member (with a
25952 particular type), as opposed to a nested class. */
25953 else if (ANON_AGGR_TYPE_P (type))
25954 {
25955 /* C++11 9.5/6. */
25956 if (decl_specifiers.storage_class != sc_none)
25957 error_at (decl_spec_token_start->location,
25958 "a storage class on an anonymous aggregate "
25959 "in class scope is not allowed");
25960
25961 /* Remove constructors and such from TYPE, now that we
25962 know it is an anonymous aggregate. */
25963 fixup_anonymous_aggr (type);
25964 /* And make the corresponding data member. */
25965 decl = build_decl (decl_spec_token_start->location,
25966 FIELD_DECL, NULL_TREE, type);
25967 /* Add it to the class. */
25968 finish_member_declaration (decl);
25969 }
25970 else
25971 cp_parser_check_access_in_redeclaration
25972 (TYPE_NAME (type),
25973 decl_spec_token_start->location);
25974 }
25975 }
25976 else
25977 {
25978 bool assume_semicolon = false;
25979
25980 /* Clear attributes from the decl_specifiers but keep them
25981 around as prefix attributes that apply them to the entity
25982 being declared. */
25983 prefix_attributes = decl_specifiers.attributes;
25984 decl_specifiers.attributes = NULL_TREE;
25985
25986 /* See if these declarations will be friends. */
25987 friend_p = cp_parser_friend_p (&decl_specifiers);
25988
25989 /* Keep going until we hit the `;' at the end of the
25990 declaration. */
25991 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25992 {
25993 tree attributes = NULL_TREE;
25994 tree first_attribute;
25995 tree initializer;
25996 bool named_bitfld = false;
25997
25998 /* Peek at the next token. */
25999 token = cp_lexer_peek_token (parser->lexer);
26000
26001 /* The following code wants to know early if it is a bit-field
26002 or some other declaration. Attributes can appear before
26003 the `:' token. Skip over them without consuming any tokens
26004 to peek if they are followed by `:'. */
26005 if (cp_next_tokens_can_be_attribute_p (parser)
26006 || (token->type == CPP_NAME
26007 && cp_nth_tokens_can_be_attribute_p (parser, 2)
26008 && (named_bitfld = true)))
26009 {
26010 size_t n
26011 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
26012 token = cp_lexer_peek_nth_token (parser->lexer, n);
26013 }
26014
26015 /* Check for a bitfield declaration. */
26016 if (token->type == CPP_COLON
26017 || (token->type == CPP_NAME
26018 && token == cp_lexer_peek_token (parser->lexer)
26019 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
26020 && (named_bitfld = true)))
26021 {
26022 tree identifier;
26023 tree width;
26024 tree late_attributes = NULL_TREE;
26025 location_t id_location
26026 = cp_lexer_peek_token (parser->lexer)->location;
26027
26028 if (named_bitfld)
26029 identifier = cp_parser_identifier (parser);
26030 else
26031 identifier = NULL_TREE;
26032
26033 /* Look for attributes that apply to the bitfield. */
26034 attributes = cp_parser_attributes_opt (parser);
26035
26036 /* Consume the `:' token. */
26037 cp_lexer_consume_token (parser->lexer);
26038
26039 /* Get the width of the bitfield. */
26040 width = cp_parser_constant_expression (parser, false, NULL,
26041 cxx_dialect >= cxx11);
26042
26043 /* In C++20 and as extension for C++11 and above we allow
26044 default member initializers for bit-fields. */
26045 initializer = NULL_TREE;
26046 if (cxx_dialect >= cxx11
26047 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
26048 || cp_lexer_next_token_is (parser->lexer,
26049 CPP_OPEN_BRACE)))
26050 {
26051 location_t loc
26052 = cp_lexer_peek_token (parser->lexer)->location;
26053 if (cxx_dialect < cxx20
26054 && identifier != NULL_TREE)
26055 pedwarn (loc, 0,
26056 "default member initializers for bit-fields "
26057 "only available with %<-std=c++20%> or "
26058 "%<-std=gnu++20%>");
26059
26060 initializer = cp_parser_save_nsdmi (parser);
26061 if (identifier == NULL_TREE)
26062 {
26063 error_at (loc, "default member initializer for "
26064 "unnamed bit-field");
26065 initializer = NULL_TREE;
26066 }
26067 }
26068 else
26069 {
26070 /* Look for attributes that apply to the bitfield after
26071 the `:' token and width. This is where GCC used to
26072 parse attributes in the past, pedwarn if there is
26073 a std attribute. */
26074 if (cp_next_tokens_can_be_std_attribute_p (parser))
26075 pedwarn (input_location, OPT_Wpedantic,
26076 "ISO C++ allows bit-field attributes only "
26077 "before the %<:%> token");
26078
26079 late_attributes = cp_parser_attributes_opt (parser);
26080 }
26081
26082 attributes = attr_chainon (attributes, late_attributes);
26083
26084 /* Remember which attributes are prefix attributes and
26085 which are not. */
26086 first_attribute = attributes;
26087 /* Combine the attributes. */
26088 attributes = attr_chainon (prefix_attributes, attributes);
26089
26090 /* Create the bitfield declaration. */
26091 decl = grokbitfield (identifier
26092 ? make_id_declarator (NULL_TREE,
26093 identifier,
26094 sfk_none,
26095 id_location)
26096 : NULL,
26097 &decl_specifiers,
26098 width, initializer,
26099 attributes);
26100 }
26101 else
26102 {
26103 cp_declarator *declarator;
26104 tree asm_specification;
26105 int ctor_dtor_or_conv_p;
26106 bool static_p = (decl_specifiers.storage_class == sc_static);
26107 cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
26108 /* We can't delay parsing for friends,
26109 alias-declarations, and typedefs, even though the
26110 standard seems to require it. */
26111 if (!friend_p
26112 && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26113 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
26114
26115 /* Parse the declarator. */
26116 declarator
26117 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26118 flags,
26119 &ctor_dtor_or_conv_p,
26120 /*parenthesized_p=*/NULL,
26121 /*member_p=*/true,
26122 friend_p, static_p);
26123
26124 /* If something went wrong parsing the declarator, make sure
26125 that we at least consume some tokens. */
26126 if (declarator == cp_error_declarator)
26127 {
26128 /* Skip to the end of the statement. */
26129 cp_parser_skip_to_end_of_statement (parser);
26130 /* If the next token is not a semicolon, that is
26131 probably because we just skipped over the body of
26132 a function. So, we consume a semicolon if
26133 present, but do not issue an error message if it
26134 is not present. */
26135 if (cp_lexer_next_token_is (parser->lexer,
26136 CPP_SEMICOLON))
26137 cp_lexer_consume_token (parser->lexer);
26138 goto out;
26139 }
26140
26141 if (declares_class_or_enum & 2)
26142 cp_parser_check_for_definition_in_return_type
26143 (declarator, decl_specifiers.type,
26144 decl_specifiers.locations[ds_type_spec]);
26145
26146 /* Look for an asm-specification. */
26147 asm_specification = cp_parser_asm_specification_opt (parser);
26148 /* Look for attributes that apply to the declaration. */
26149 attributes = cp_parser_attributes_opt (parser);
26150 /* Remember which attributes are prefix attributes and
26151 which are not. */
26152 first_attribute = attributes;
26153 /* Combine the attributes. */
26154 attributes = attr_chainon (prefix_attributes, attributes);
26155
26156 /* If it's an `=', then we have a constant-initializer or a
26157 pure-specifier. It is not correct to parse the
26158 initializer before registering the member declaration
26159 since the member declaration should be in scope while
26160 its initializer is processed. However, the rest of the
26161 front end does not yet provide an interface that allows
26162 us to handle this correctly. */
26163 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
26164 {
26165 /* In [class.mem]:
26166
26167 A pure-specifier shall be used only in the declaration of
26168 a virtual function.
26169
26170 A member-declarator can contain a constant-initializer
26171 only if it declares a static member of integral or
26172 enumeration type.
26173
26174 Therefore, if the DECLARATOR is for a function, we look
26175 for a pure-specifier; otherwise, we look for a
26176 constant-initializer. When we call `grokfield', it will
26177 perform more stringent semantics checks. */
26178 initializer_token_start = cp_lexer_peek_token (parser->lexer);
26179 if (function_declarator_p (declarator)
26180 || (decl_specifiers.type
26181 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
26182 && declarator->kind == cdk_id
26183 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
26184 == FUNCTION_TYPE)))
26185 initializer = cp_parser_pure_specifier (parser);
26186 else if (decl_specifiers.storage_class != sc_static)
26187 initializer = cp_parser_save_nsdmi (parser);
26188 else if (cxx_dialect >= cxx11)
26189 {
26190 bool nonconst;
26191 /* Don't require a constant rvalue in C++11, since we
26192 might want a reference constant. We'll enforce
26193 constancy later. */
26194 cp_lexer_consume_token (parser->lexer);
26195 /* Parse the initializer. */
26196 initializer = cp_parser_initializer_clause (parser,
26197 &nonconst);
26198 }
26199 else
26200 /* Parse the initializer. */
26201 initializer = cp_parser_constant_initializer (parser);
26202 }
26203 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
26204 && !function_declarator_p (declarator))
26205 {
26206 bool x;
26207 if (decl_specifiers.storage_class != sc_static)
26208 initializer = cp_parser_save_nsdmi (parser);
26209 else
26210 initializer = cp_parser_initializer (parser, &x, &x);
26211 }
26212 /* Detect invalid bit-field cases such as
26213
26214 int *p : 4;
26215 int &&r : 3;
26216
26217 and similar. */
26218 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
26219 /* If there were no type specifiers, it was a
26220 constructor. */
26221 && decl_specifiers.any_type_specifiers_p)
26222 {
26223 /* This is called for a decent diagnostic only. */
26224 tree d = grokdeclarator (declarator, &decl_specifiers,
26225 BITFIELD, /*initialized=*/false,
26226 &attributes);
26227 if (!error_operand_p (d))
26228 error_at (DECL_SOURCE_LOCATION (d),
26229 "bit-field %qD has non-integral type %qT",
26230 d, TREE_TYPE (d));
26231 cp_parser_skip_to_end_of_statement (parser);
26232 /* Avoid "extra ;" pedwarns. */
26233 if (cp_lexer_next_token_is (parser->lexer,
26234 CPP_SEMICOLON))
26235 cp_lexer_consume_token (parser->lexer);
26236 goto out;
26237 }
26238 /* Otherwise, there is no initializer. */
26239 else
26240 initializer = NULL_TREE;
26241
26242 /* See if we are probably looking at a function
26243 definition. We are certainly not looking at a
26244 member-declarator. Calling `grokfield' has
26245 side-effects, so we must not do it unless we are sure
26246 that we are looking at a member-declarator. */
26247 if (cp_parser_token_starts_function_definition_p
26248 (cp_lexer_peek_token (parser->lexer)))
26249 {
26250 /* The grammar does not allow a pure-specifier to be
26251 used when a member function is defined. (It is
26252 possible that this fact is an oversight in the
26253 standard, since a pure function may be defined
26254 outside of the class-specifier. */
26255 if (initializer && initializer_token_start)
26256 error_at (initializer_token_start->location,
26257 "pure-specifier on function-definition");
26258 decl = cp_parser_save_member_function_body (parser,
26259 &decl_specifiers,
26260 declarator,
26261 attributes);
26262 if (parser->fully_implicit_function_template_p)
26263 decl = finish_fully_implicit_template (parser, decl);
26264 /* If the member was not a friend, declare it here. */
26265 if (!friend_p)
26266 finish_member_declaration (decl);
26267 /* Peek at the next token. */
26268 token = cp_lexer_peek_token (parser->lexer);
26269 /* If the next token is a semicolon, consume it. */
26270 if (token->type == CPP_SEMICOLON)
26271 {
26272 location_t semicolon_loc
26273 = cp_lexer_consume_token (parser->lexer)->location;
26274 gcc_rich_location richloc (semicolon_loc);
26275 richloc.add_fixit_remove ();
26276 warning_at (&richloc, OPT_Wextra_semi,
26277 "extra %<;%> after in-class "
26278 "function definition");
26279 }
26280 goto out;
26281 }
26282 else
26283 if (declarator->kind == cdk_function)
26284 declarator->id_loc = token->location;
26285 /* Create the declaration. */
26286 decl = grokfield (declarator, &decl_specifiers,
26287 initializer, /*init_const_expr_p=*/true,
26288 asm_specification, attributes);
26289 if (parser->fully_implicit_function_template_p)
26290 {
26291 if (friend_p)
26292 finish_fully_implicit_template (parser, 0);
26293 else
26294 decl = finish_fully_implicit_template (parser, decl);
26295 }
26296 }
26297
26298 cp_finalize_omp_declare_simd (parser, decl);
26299 cp_finalize_oacc_routine (parser, decl, false);
26300
26301 /* Reset PREFIX_ATTRIBUTES. */
26302 if (attributes != error_mark_node)
26303 {
26304 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26305 attributes = TREE_CHAIN (attributes);
26306 if (attributes)
26307 TREE_CHAIN (attributes) = NULL_TREE;
26308 }
26309
26310 /* If there is any qualification still in effect, clear it
26311 now; we will be starting fresh with the next declarator. */
26312 parser->scope = NULL_TREE;
26313 parser->qualifying_scope = NULL_TREE;
26314 parser->object_scope = NULL_TREE;
26315 /* If it's a `,', then there are more declarators. */
26316 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26317 {
26318 cp_lexer_consume_token (parser->lexer);
26319 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26320 {
26321 cp_token *token = cp_lexer_previous_token (parser->lexer);
26322 gcc_rich_location richloc (token->location);
26323 richloc.add_fixit_remove ();
26324 error_at (&richloc, "stray %<,%> at end of "
26325 "member declaration");
26326 }
26327 }
26328 /* If the next token isn't a `;', then we have a parse error. */
26329 else if (cp_lexer_next_token_is_not (parser->lexer,
26330 CPP_SEMICOLON))
26331 {
26332 /* The next token might be a ways away from where the
26333 actual semicolon is missing. Find the previous token
26334 and use that for our error position. */
26335 cp_token *token = cp_lexer_previous_token (parser->lexer);
26336 gcc_rich_location richloc (token->location);
26337 richloc.add_fixit_insert_after (";");
26338 error_at (&richloc, "expected %<;%> at end of "
26339 "member declaration");
26340
26341 /* Assume that the user meant to provide a semicolon. If
26342 we were to cp_parser_skip_to_end_of_statement, we might
26343 skip to a semicolon inside a member function definition
26344 and issue nonsensical error messages. */
26345 assume_semicolon = true;
26346 }
26347
26348 if (decl)
26349 {
26350 /* Add DECL to the list of members. */
26351 if (!friend_p
26352 /* Explicitly include, eg, NSDMIs, for better error
26353 recovery (c++/58650). */
26354 || !DECL_DECLARES_FUNCTION_P (decl))
26355 finish_member_declaration (decl);
26356
26357 if (TREE_CODE (decl) == FUNCTION_DECL)
26358 cp_parser_save_default_args (parser, decl);
26359 else if (TREE_CODE (decl) == FIELD_DECL
26360 && DECL_INITIAL (decl))
26361 /* Add DECL to the queue of NSDMI to be parsed later. */
26362 vec_safe_push (unparsed_nsdmis, decl);
26363 }
26364
26365 if (assume_semicolon)
26366 goto out;
26367 }
26368 }
26369
26370 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26371 out:
26372 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
26373 }
26374
26375 /* Parse a pure-specifier.
26376
26377 pure-specifier:
26378 = 0
26379
26380 Returns INTEGER_ZERO_NODE if a pure specifier is found.
26381 Otherwise, ERROR_MARK_NODE is returned. */
26382
26383 static tree
26384 cp_parser_pure_specifier (cp_parser* parser)
26385 {
26386 cp_token *token;
26387
26388 /* Look for the `=' token. */
26389 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26390 return error_mark_node;
26391 /* Look for the `0' token. */
26392 token = cp_lexer_peek_token (parser->lexer);
26393
26394 if (token->type == CPP_EOF
26395 || token->type == CPP_PRAGMA_EOL)
26396 return error_mark_node;
26397
26398 cp_lexer_consume_token (parser->lexer);
26399
26400 /* Accept = default or = delete in c++0x mode. */
26401 if (token->keyword == RID_DEFAULT
26402 || token->keyword == RID_DELETE)
26403 {
26404 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
26405 return token->u.value;
26406 }
26407
26408 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
26409 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
26410 {
26411 cp_parser_error (parser,
26412 "invalid pure specifier (only %<= 0%> is allowed)");
26413 cp_parser_skip_to_end_of_statement (parser);
26414 return error_mark_node;
26415 }
26416 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
26417 {
26418 error_at (token->location, "templates may not be %<virtual%>");
26419 return error_mark_node;
26420 }
26421
26422 return integer_zero_node;
26423 }
26424
26425 /* Parse a constant-initializer.
26426
26427 constant-initializer:
26428 = constant-expression
26429
26430 Returns a representation of the constant-expression. */
26431
26432 static tree
26433 cp_parser_constant_initializer (cp_parser* parser)
26434 {
26435 /* Look for the `=' token. */
26436 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26437 return error_mark_node;
26438
26439 /* It is invalid to write:
26440
26441 struct S { static const int i = { 7 }; };
26442
26443 */
26444 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26445 {
26446 cp_parser_error (parser,
26447 "a brace-enclosed initializer is not allowed here");
26448 /* Consume the opening brace. */
26449 matching_braces braces;
26450 braces.consume_open (parser);
26451 /* Skip the initializer. */
26452 cp_parser_skip_to_closing_brace (parser);
26453 /* Look for the trailing `}'. */
26454 braces.require_close (parser);
26455
26456 return error_mark_node;
26457 }
26458
26459 return cp_parser_constant_expression (parser);
26460 }
26461
26462 /* Derived classes [gram.class.derived] */
26463
26464 /* Parse a base-clause.
26465
26466 base-clause:
26467 : base-specifier-list
26468
26469 base-specifier-list:
26470 base-specifier ... [opt]
26471 base-specifier-list , base-specifier ... [opt]
26472
26473 Returns a TREE_LIST representing the base-classes, in the order in
26474 which they were declared. The representation of each node is as
26475 described by cp_parser_base_specifier.
26476
26477 In the case that no bases are specified, this function will return
26478 NULL_TREE, not ERROR_MARK_NODE. */
26479
26480 static tree
26481 cp_parser_base_clause (cp_parser* parser)
26482 {
26483 tree bases = NULL_TREE;
26484
26485 /* Look for the `:' that begins the list. */
26486 cp_parser_require (parser, CPP_COLON, RT_COLON);
26487
26488 /* Scan the base-specifier-list. */
26489 while (true)
26490 {
26491 cp_token *token;
26492 tree base;
26493 bool pack_expansion_p = false;
26494
26495 /* Look for the base-specifier. */
26496 base = cp_parser_base_specifier (parser);
26497 /* Look for the (optional) ellipsis. */
26498 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26499 {
26500 /* Consume the `...'. */
26501 cp_lexer_consume_token (parser->lexer);
26502
26503 pack_expansion_p = true;
26504 }
26505
26506 /* Add BASE to the front of the list. */
26507 if (base && base != error_mark_node)
26508 {
26509 if (pack_expansion_p)
26510 /* Make this a pack expansion type. */
26511 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
26512
26513 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
26514 {
26515 TREE_CHAIN (base) = bases;
26516 bases = base;
26517 }
26518 }
26519 /* Peek at the next token. */
26520 token = cp_lexer_peek_token (parser->lexer);
26521 /* If it's not a comma, then the list is complete. */
26522 if (token->type != CPP_COMMA)
26523 break;
26524 /* Consume the `,'. */
26525 cp_lexer_consume_token (parser->lexer);
26526 }
26527
26528 /* PARSER->SCOPE may still be non-NULL at this point, if the last
26529 base class had a qualified name. However, the next name that
26530 appears is certainly not qualified. */
26531 parser->scope = NULL_TREE;
26532 parser->qualifying_scope = NULL_TREE;
26533 parser->object_scope = NULL_TREE;
26534
26535 return nreverse (bases);
26536 }
26537
26538 /* Parse a base-specifier.
26539
26540 base-specifier:
26541 :: [opt] nested-name-specifier [opt] class-name
26542 virtual access-specifier [opt] :: [opt] nested-name-specifier
26543 [opt] class-name
26544 access-specifier virtual [opt] :: [opt] nested-name-specifier
26545 [opt] class-name
26546
26547 Returns a TREE_LIST. The TREE_PURPOSE will be one of
26548 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
26549 indicate the specifiers provided. The TREE_VALUE will be a TYPE
26550 (or the ERROR_MARK_NODE) indicating the type that was specified. */
26551
26552 static tree
26553 cp_parser_base_specifier (cp_parser* parser)
26554 {
26555 cp_token *token;
26556 bool done = false;
26557 bool virtual_p = false;
26558 bool duplicate_virtual_error_issued_p = false;
26559 bool duplicate_access_error_issued_p = false;
26560 bool class_scope_p, template_p;
26561 tree access = access_default_node;
26562 tree type;
26563
26564 /* Process the optional `virtual' and `access-specifier'. */
26565 while (!done)
26566 {
26567 /* Peek at the next token. */
26568 token = cp_lexer_peek_token (parser->lexer);
26569 /* Process `virtual'. */
26570 switch (token->keyword)
26571 {
26572 case RID_VIRTUAL:
26573 /* If `virtual' appears more than once, issue an error. */
26574 if (virtual_p && !duplicate_virtual_error_issued_p)
26575 {
26576 cp_parser_error (parser,
26577 "%<virtual%> specified more than once in base-specifier");
26578 duplicate_virtual_error_issued_p = true;
26579 }
26580
26581 virtual_p = true;
26582
26583 /* Consume the `virtual' token. */
26584 cp_lexer_consume_token (parser->lexer);
26585
26586 break;
26587
26588 case RID_PUBLIC:
26589 case RID_PROTECTED:
26590 case RID_PRIVATE:
26591 /* If more than one access specifier appears, issue an
26592 error. */
26593 if (access != access_default_node
26594 && !duplicate_access_error_issued_p)
26595 {
26596 cp_parser_error (parser,
26597 "more than one access specifier in base-specifier");
26598 duplicate_access_error_issued_p = true;
26599 }
26600
26601 access = ridpointers[(int) token->keyword];
26602
26603 /* Consume the access-specifier. */
26604 cp_lexer_consume_token (parser->lexer);
26605
26606 break;
26607
26608 default:
26609 done = true;
26610 break;
26611 }
26612 }
26613 /* It is not uncommon to see programs mechanically, erroneously, use
26614 the 'typename' keyword to denote (dependent) qualified types
26615 as base classes. */
26616 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26617 {
26618 token = cp_lexer_peek_token (parser->lexer);
26619 if (!processing_template_decl)
26620 error_at (token->location,
26621 "keyword %<typename%> not allowed outside of templates");
26622 else
26623 error_at (token->location,
26624 "keyword %<typename%> not allowed in this context "
26625 "(the base class is implicitly a type)");
26626 cp_lexer_consume_token (parser->lexer);
26627 }
26628
26629 /* Look for the optional `::' operator. */
26630 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
26631 /* Look for the nested-name-specifier. The simplest way to
26632 implement:
26633
26634 [temp.res]
26635
26636 The keyword `typename' is not permitted in a base-specifier or
26637 mem-initializer; in these contexts a qualified name that
26638 depends on a template-parameter is implicitly assumed to be a
26639 type name.
26640
26641 is to pretend that we have seen the `typename' keyword at this
26642 point. */
26643 cp_parser_nested_name_specifier_opt (parser,
26644 /*typename_keyword_p=*/true,
26645 /*check_dependency_p=*/true,
26646 /*type_p=*/true,
26647 /*is_declaration=*/true);
26648 /* If the base class is given by a qualified name, assume that names
26649 we see are type names or templates, as appropriate. */
26650 class_scope_p = (parser->scope && TYPE_P (parser->scope));
26651 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
26652
26653 if (!parser->scope
26654 && cp_lexer_next_token_is_decltype (parser->lexer))
26655 /* DR 950 allows decltype as a base-specifier. */
26656 type = cp_parser_decltype (parser);
26657 else
26658 {
26659 /* Otherwise, look for the class-name. */
26660 type = cp_parser_class_name (parser,
26661 class_scope_p,
26662 template_p,
26663 typename_type,
26664 /*check_dependency_p=*/true,
26665 /*class_head_p=*/false,
26666 /*is_declaration=*/true);
26667 type = TREE_TYPE (type);
26668 }
26669
26670 if (type == error_mark_node)
26671 return error_mark_node;
26672
26673 return finish_base_specifier (type, access, virtual_p);
26674 }
26675
26676 /* Exception handling [gram.exception] */
26677
26678 /* Save the tokens that make up the noexcept-specifier for a member-function.
26679 Returns a DEFERRED_PARSE. */
26680
26681 static tree
26682 cp_parser_save_noexcept (cp_parser *parser)
26683 {
26684 cp_token *first = parser->lexer->next_token;
26685 /* We want everything up to, including, the final ')'. */
26686 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
26687 cp_token *last = parser->lexer->next_token;
26688
26689 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
26690 to carry the information we will need. */
26691 tree expr = make_node (DEFERRED_PARSE);
26692 /* Save away the noexcept-specifier; we will process it when the
26693 class is complete. */
26694 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
26695 expr = build_tree_list (expr, NULL_TREE);
26696 return expr;
26697 }
26698
26699 /* Used for late processing of noexcept-specifiers of member-functions.
26700 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
26701 we saved for later; parse it now. DECL is the declaration of the
26702 member function. */
26703
26704 static tree
26705 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
26706 {
26707 /* Make sure we've gotten something that hasn't been parsed yet. */
26708 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
26709
26710 push_unparsed_function_queues (parser);
26711
26712 /* Push the saved tokens for the noexcept-specifier onto the parser's
26713 lexer stack. */
26714 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
26715 cp_parser_push_lexer_for_tokens (parser, tokens);
26716
26717 /* Parse the cached noexcept-specifier. */
26718 tree parsed_arg
26719 = cp_parser_noexcept_specification_opt (parser,
26720 CP_PARSER_FLAGS_NONE,
26721 /*require_constexpr=*/true,
26722 /*consumed_expr=*/NULL,
26723 /*return_cond=*/false);
26724
26725 /* Revert to the main lexer. */
26726 cp_parser_pop_lexer (parser);
26727
26728 /* Restore the queue. */
26729 pop_unparsed_function_queues (parser);
26730
26731 /* And we're done. */
26732 return parsed_arg;
26733 }
26734
26735 /* Perform late checking of overriding function with respect to their
26736 noexcept-specifiers. TYPE is the class and FNDECL is the function
26737 that potentially overrides some virtual function with the same
26738 signature. */
26739
26740 static void
26741 noexcept_override_late_checks (tree type, tree fndecl)
26742 {
26743 tree binfo = TYPE_BINFO (type);
26744 tree base_binfo;
26745
26746 if (DECL_STATIC_FUNCTION_P (fndecl))
26747 return;
26748
26749 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
26750 {
26751 tree basetype = BINFO_TYPE (base_binfo);
26752
26753 if (!TYPE_POLYMORPHIC_P (basetype))
26754 continue;
26755
26756 tree fn = look_for_overrides_here (basetype, fndecl);
26757 if (fn)
26758 maybe_check_overriding_exception_spec (fndecl, fn);
26759 }
26760 }
26761
26762 /* Parse an (optional) noexcept-specification.
26763
26764 noexcept-specification:
26765 noexcept ( constant-expression ) [opt]
26766
26767 If no noexcept-specification is present, returns NULL_TREE.
26768 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
26769 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
26770 there are no parentheses. CONSUMED_EXPR will be set accordingly.
26771 Otherwise, returns a noexcept specification unless RETURN_COND is true,
26772 in which case a boolean condition is returned instead. The parser flags
26773 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
26774 the (member) function is `const'. */
26775
26776 static tree
26777 cp_parser_noexcept_specification_opt (cp_parser* parser,
26778 cp_parser_flags flags,
26779 bool require_constexpr,
26780 bool* consumed_expr,
26781 bool return_cond)
26782 {
26783 cp_token *token;
26784 const char *saved_message;
26785
26786 /* Peek at the next token. */
26787 token = cp_lexer_peek_token (parser->lexer);
26788
26789 /* Is it a noexcept-specification? */
26790 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
26791 {
26792 tree expr;
26793
26794 /* [class.mem]/6 says that a noexcept-specifer (within the
26795 member-specification of the class) is a complete-class context of
26796 a class. So, if the noexcept-specifier has the optional expression,
26797 just save the tokens, and reparse this after we're done with the
26798 class. */
26799
26800 if ((flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
26801 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
26802 /* No need to delay parsing for a number literal or true/false. */
26803 && !((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
26804 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26805 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
26806 && at_class_scope_p ()
26807 && TYPE_BEING_DEFINED (current_class_type)
26808 && !LAMBDA_TYPE_P (current_class_type))
26809 return cp_parser_save_noexcept (parser);
26810
26811 cp_lexer_consume_token (parser->lexer);
26812
26813 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
26814 {
26815 matching_parens parens;
26816 parens.consume_open (parser);
26817
26818 if (require_constexpr)
26819 {
26820 /* Types may not be defined in an exception-specification. */
26821 saved_message = parser->type_definition_forbidden_message;
26822 parser->type_definition_forbidden_message
26823 = G_("types may not be defined in an exception-specification");
26824
26825 bool non_constant_p;
26826 expr
26827 = cp_parser_constant_expression (parser,
26828 /*allow_non_constant=*/true,
26829 &non_constant_p);
26830 if (non_constant_p
26831 && !require_potential_rvalue_constant_expression (expr))
26832 {
26833 expr = NULL_TREE;
26834 return_cond = true;
26835 }
26836
26837 /* Restore the saved message. */
26838 parser->type_definition_forbidden_message = saved_message;
26839 }
26840 else
26841 {
26842 expr = cp_parser_expression (parser);
26843 *consumed_expr = true;
26844 }
26845
26846 parens.require_close (parser);
26847 }
26848 else
26849 {
26850 expr = boolean_true_node;
26851 if (!require_constexpr)
26852 *consumed_expr = false;
26853 }
26854
26855 /* We cannot build a noexcept-spec right away because this will check
26856 that expr is a constexpr. */
26857 if (!return_cond)
26858 return build_noexcept_spec (expr, tf_warning_or_error);
26859 else
26860 return expr;
26861 }
26862 else
26863 return NULL_TREE;
26864 }
26865
26866 /* Parse an (optional) exception-specification.
26867
26868 exception-specification:
26869 throw ( type-id-list [opt] )
26870
26871 Returns a TREE_LIST representing the exception-specification. The
26872 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
26873 control parsing. QUALS are qualifiers indicating whether the (member)
26874 function is `const'. */
26875
26876 static tree
26877 cp_parser_exception_specification_opt (cp_parser* parser,
26878 cp_parser_flags flags)
26879 {
26880 cp_token *token;
26881 tree type_id_list;
26882 const char *saved_message;
26883
26884 /* Peek at the next token. */
26885 token = cp_lexer_peek_token (parser->lexer);
26886
26887 /* Is it a noexcept-specification? */
26888 type_id_list
26889 = cp_parser_noexcept_specification_opt (parser, flags,
26890 /*require_constexpr=*/true,
26891 /*consumed_expr=*/NULL,
26892 /*return_cond=*/false);
26893 if (type_id_list != NULL_TREE)
26894 return type_id_list;
26895
26896 /* If it's not `throw', then there's no exception-specification. */
26897 if (!cp_parser_is_keyword (token, RID_THROW))
26898 return NULL_TREE;
26899
26900 location_t loc = token->location;
26901
26902 /* Consume the `throw'. */
26903 cp_lexer_consume_token (parser->lexer);
26904
26905 /* Look for the `('. */
26906 matching_parens parens;
26907 parens.require_open (parser);
26908
26909 /* Peek at the next token. */
26910 token = cp_lexer_peek_token (parser->lexer);
26911 /* If it's not a `)', then there is a type-id-list. */
26912 if (token->type != CPP_CLOSE_PAREN)
26913 {
26914 /* Types may not be defined in an exception-specification. */
26915 saved_message = parser->type_definition_forbidden_message;
26916 parser->type_definition_forbidden_message
26917 = G_("types may not be defined in an exception-specification");
26918 /* Parse the type-id-list. */
26919 type_id_list = cp_parser_type_id_list (parser);
26920 /* Restore the saved message. */
26921 parser->type_definition_forbidden_message = saved_message;
26922
26923 if (cxx_dialect >= cxx17)
26924 {
26925 error_at (loc, "ISO C++17 does not allow dynamic exception "
26926 "specifications");
26927 type_id_list = NULL_TREE;
26928 }
26929 else if (cxx_dialect >= cxx11)
26930 warning_at (loc, OPT_Wdeprecated,
26931 "dynamic exception specifications are deprecated in "
26932 "C++11");
26933 }
26934 /* In C++17, throw() is equivalent to noexcept (true). throw()
26935 is deprecated in C++11 and above as well, but is still widely used,
26936 so don't warn about it yet. */
26937 else if (cxx_dialect >= cxx17)
26938 type_id_list = noexcept_true_spec;
26939 else
26940 type_id_list = empty_except_spec;
26941
26942 /* Look for the `)'. */
26943 parens.require_close (parser);
26944
26945 return type_id_list;
26946 }
26947
26948 /* Parse an (optional) type-id-list.
26949
26950 type-id-list:
26951 type-id ... [opt]
26952 type-id-list , type-id ... [opt]
26953
26954 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
26955 in the order that the types were presented. */
26956
26957 static tree
26958 cp_parser_type_id_list (cp_parser* parser)
26959 {
26960 tree types = NULL_TREE;
26961
26962 while (true)
26963 {
26964 cp_token *token;
26965 tree type;
26966
26967 token = cp_lexer_peek_token (parser->lexer);
26968
26969 /* Get the next type-id. */
26970 type = cp_parser_type_id (parser);
26971 /* Check for invalid 'auto'. */
26972 if (flag_concepts && type_uses_auto (type))
26973 {
26974 error_at (token->location,
26975 "invalid use of %<auto%> in exception-specification");
26976 type = error_mark_node;
26977 }
26978 /* Parse the optional ellipsis. */
26979 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26980 {
26981 /* Consume the `...'. */
26982 cp_lexer_consume_token (parser->lexer);
26983
26984 /* Turn the type into a pack expansion expression. */
26985 type = make_pack_expansion (type);
26986 }
26987 /* Add it to the list. */
26988 types = add_exception_specifier (types, type, /*complain=*/1);
26989 /* Peek at the next token. */
26990 token = cp_lexer_peek_token (parser->lexer);
26991 /* If it is not a `,', we are done. */
26992 if (token->type != CPP_COMMA)
26993 break;
26994 /* Consume the `,'. */
26995 cp_lexer_consume_token (parser->lexer);
26996 }
26997
26998 return nreverse (types);
26999 }
27000
27001 /* Parse a try-block.
27002
27003 try-block:
27004 try compound-statement handler-seq */
27005
27006 static tree
27007 cp_parser_try_block (cp_parser* parser)
27008 {
27009 tree try_block;
27010
27011 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
27012 if (parser->in_function_body
27013 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
27014 && cxx_dialect < cxx20)
27015 pedwarn (input_location, 0,
27016 "%<try%> in %<constexpr%> function only "
27017 "available with %<-std=c++20%> or %<-std=gnu++20%>");
27018
27019 try_block = begin_try_block ();
27020 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
27021 finish_try_block (try_block);
27022 cp_parser_handler_seq (parser);
27023 finish_handler_sequence (try_block);
27024
27025 return try_block;
27026 }
27027
27028 /* Parse a function-try-block.
27029
27030 function-try-block:
27031 try ctor-initializer [opt] function-body handler-seq */
27032
27033 static void
27034 cp_parser_function_try_block (cp_parser* parser)
27035 {
27036 tree compound_stmt;
27037 tree try_block;
27038
27039 /* Look for the `try' keyword. */
27040 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
27041 return;
27042 /* Let the rest of the front end know where we are. */
27043 try_block = begin_function_try_block (&compound_stmt);
27044 /* Parse the function-body. */
27045 cp_parser_ctor_initializer_opt_and_function_body
27046 (parser, /*in_function_try_block=*/true);
27047 /* We're done with the `try' part. */
27048 finish_function_try_block (try_block);
27049 /* Parse the handlers. */
27050 cp_parser_handler_seq (parser);
27051 /* We're done with the handlers. */
27052 finish_function_handler_sequence (try_block, compound_stmt);
27053 }
27054
27055 /* Parse a handler-seq.
27056
27057 handler-seq:
27058 handler handler-seq [opt] */
27059
27060 static void
27061 cp_parser_handler_seq (cp_parser* parser)
27062 {
27063 while (true)
27064 {
27065 cp_token *token;
27066
27067 /* Parse the handler. */
27068 cp_parser_handler (parser);
27069 /* Peek at the next token. */
27070 token = cp_lexer_peek_token (parser->lexer);
27071 /* If it's not `catch' then there are no more handlers. */
27072 if (!cp_parser_is_keyword (token, RID_CATCH))
27073 break;
27074 }
27075 }
27076
27077 /* Parse a handler.
27078
27079 handler:
27080 catch ( exception-declaration ) compound-statement */
27081
27082 static void
27083 cp_parser_handler (cp_parser* parser)
27084 {
27085 tree handler;
27086 tree declaration;
27087
27088 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
27089 handler = begin_handler ();
27090 matching_parens parens;
27091 parens.require_open (parser);
27092 declaration = cp_parser_exception_declaration (parser);
27093 finish_handler_parms (declaration, handler);
27094 parens.require_close (parser);
27095 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
27096 finish_handler (handler);
27097 }
27098
27099 /* Parse an exception-declaration.
27100
27101 exception-declaration:
27102 type-specifier-seq declarator
27103 type-specifier-seq abstract-declarator
27104 type-specifier-seq
27105 ...
27106
27107 Returns a VAR_DECL for the declaration, or NULL_TREE if the
27108 ellipsis variant is used. */
27109
27110 static tree
27111 cp_parser_exception_declaration (cp_parser* parser)
27112 {
27113 cp_decl_specifier_seq type_specifiers;
27114 cp_declarator *declarator;
27115 const char *saved_message;
27116
27117 /* If it's an ellipsis, it's easy to handle. */
27118 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27119 {
27120 /* Consume the `...' token. */
27121 cp_lexer_consume_token (parser->lexer);
27122 return NULL_TREE;
27123 }
27124
27125 /* Types may not be defined in exception-declarations. */
27126 saved_message = parser->type_definition_forbidden_message;
27127 parser->type_definition_forbidden_message
27128 = G_("types may not be defined in exception-declarations");
27129
27130 /* Parse the type-specifier-seq. */
27131 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
27132 /*is_declaration=*/true,
27133 /*is_trailing_return=*/false,
27134 &type_specifiers);
27135 /* If it's a `)', then there is no declarator. */
27136 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27137 declarator = NULL;
27138 else
27139 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
27140 CP_PARSER_FLAGS_NONE,
27141 /*ctor_dtor_or_conv_p=*/NULL,
27142 /*parenthesized_p=*/NULL,
27143 /*member_p=*/false,
27144 /*friend_p=*/false,
27145 /*static_p=*/false);
27146
27147 /* Restore the saved message. */
27148 parser->type_definition_forbidden_message = saved_message;
27149
27150 if (!type_specifiers.any_specifiers_p)
27151 return error_mark_node;
27152
27153 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
27154 }
27155
27156 /* Parse a throw-expression.
27157
27158 throw-expression:
27159 throw assignment-expression [opt]
27160
27161 Returns a THROW_EXPR representing the throw-expression. */
27162
27163 static tree
27164 cp_parser_throw_expression (cp_parser* parser)
27165 {
27166 tree expression;
27167 cp_token* token;
27168 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27169
27170 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
27171 token = cp_lexer_peek_token (parser->lexer);
27172 /* Figure out whether or not there is an assignment-expression
27173 following the "throw" keyword. */
27174 if (token->type == CPP_COMMA
27175 || token->type == CPP_SEMICOLON
27176 || token->type == CPP_CLOSE_PAREN
27177 || token->type == CPP_CLOSE_SQUARE
27178 || token->type == CPP_CLOSE_BRACE
27179 || token->type == CPP_COLON)
27180 expression = NULL_TREE;
27181 else
27182 expression = cp_parser_assignment_expression (parser);
27183
27184 /* Construct a location e.g.:
27185 throw x
27186 ^~~~~~~
27187 with caret == start at the start of the "throw" token, and
27188 the end at the end of the final token we consumed. */
27189 location_t combined_loc = make_location (start_loc, start_loc,
27190 parser->lexer);
27191 expression = build_throw (combined_loc, expression);
27192
27193 return expression;
27194 }
27195
27196 /* Parse a yield-expression.
27197
27198 yield-expression:
27199 co_yield assignment-expression
27200 co_yield braced-init-list
27201
27202 Returns a CO_YIELD_EXPR representing the yield-expression. */
27203
27204 static tree
27205 cp_parser_yield_expression (cp_parser* parser)
27206 {
27207 tree expr;
27208
27209 cp_token *token = cp_lexer_peek_token (parser->lexer);
27210 location_t kw_loc = token->location; /* Save for later. */
27211
27212 cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
27213
27214 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27215 {
27216 bool expr_non_constant_p;
27217 cp_lexer_set_source_position (parser->lexer);
27218 /* ??? : probably a moot point? */
27219 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27220 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
27221 }
27222 else
27223 expr = cp_parser_assignment_expression (parser);
27224
27225 if (expr == error_mark_node)
27226 return expr;
27227
27228 return finish_co_yield_expr (kw_loc, expr);
27229 }
27230
27231 /* GNU Extensions */
27232
27233 /* Parse an (optional) asm-specification.
27234
27235 asm-specification:
27236 asm ( string-literal )
27237
27238 If the asm-specification is present, returns a STRING_CST
27239 corresponding to the string-literal. Otherwise, returns
27240 NULL_TREE. */
27241
27242 static tree
27243 cp_parser_asm_specification_opt (cp_parser* parser)
27244 {
27245 cp_token *token;
27246 tree asm_specification;
27247
27248 /* Peek at the next token. */
27249 token = cp_lexer_peek_token (parser->lexer);
27250 /* If the next token isn't the `asm' keyword, then there's no
27251 asm-specification. */
27252 if (!cp_parser_is_keyword (token, RID_ASM))
27253 return NULL_TREE;
27254
27255 /* Consume the `asm' token. */
27256 cp_lexer_consume_token (parser->lexer);
27257 /* Look for the `('. */
27258 matching_parens parens;
27259 parens.require_open (parser);
27260
27261 /* Look for the string-literal. */
27262 asm_specification = cp_parser_string_literal (parser, false, false);
27263
27264 /* Look for the `)'. */
27265 parens.require_close (parser);
27266
27267 return asm_specification;
27268 }
27269
27270 /* Parse an asm-operand-list.
27271
27272 asm-operand-list:
27273 asm-operand
27274 asm-operand-list , asm-operand
27275
27276 asm-operand:
27277 string-literal ( expression )
27278 [ string-literal ] string-literal ( expression )
27279
27280 Returns a TREE_LIST representing the operands. The TREE_VALUE of
27281 each node is the expression. The TREE_PURPOSE is itself a
27282 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
27283 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
27284 is a STRING_CST for the string literal before the parenthesis. Returns
27285 ERROR_MARK_NODE if any of the operands are invalid. */
27286
27287 static tree
27288 cp_parser_asm_operand_list (cp_parser* parser)
27289 {
27290 tree asm_operands = NULL_TREE;
27291 bool invalid_operands = false;
27292
27293 while (true)
27294 {
27295 tree string_literal;
27296 tree expression;
27297 tree name;
27298
27299 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27300 {
27301 /* Consume the `[' token. */
27302 cp_lexer_consume_token (parser->lexer);
27303 /* Read the operand name. */
27304 name = cp_parser_identifier (parser);
27305 if (name != error_mark_node)
27306 name = build_string (IDENTIFIER_LENGTH (name),
27307 IDENTIFIER_POINTER (name));
27308 /* Look for the closing `]'. */
27309 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27310 }
27311 else
27312 name = NULL_TREE;
27313 /* Look for the string-literal. */
27314 string_literal = cp_parser_string_literal (parser, false, false);
27315
27316 /* Look for the `('. */
27317 matching_parens parens;
27318 parens.require_open (parser);
27319 /* Parse the expression. */
27320 expression = cp_parser_expression (parser);
27321 /* Look for the `)'. */
27322 parens.require_close (parser);
27323
27324 if (name == error_mark_node
27325 || string_literal == error_mark_node
27326 || expression == error_mark_node)
27327 invalid_operands = true;
27328
27329 /* Add this operand to the list. */
27330 asm_operands = tree_cons (build_tree_list (name, string_literal),
27331 expression,
27332 asm_operands);
27333 /* If the next token is not a `,', there are no more
27334 operands. */
27335 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27336 break;
27337 /* Consume the `,'. */
27338 cp_lexer_consume_token (parser->lexer);
27339 }
27340
27341 return invalid_operands ? error_mark_node : nreverse (asm_operands);
27342 }
27343
27344 /* Parse an asm-clobber-list.
27345
27346 asm-clobber-list:
27347 string-literal
27348 asm-clobber-list , string-literal
27349
27350 Returns a TREE_LIST, indicating the clobbers in the order that they
27351 appeared. The TREE_VALUE of each node is a STRING_CST. */
27352
27353 static tree
27354 cp_parser_asm_clobber_list (cp_parser* parser)
27355 {
27356 tree clobbers = NULL_TREE;
27357
27358 while (true)
27359 {
27360 tree string_literal;
27361
27362 /* Look for the string literal. */
27363 string_literal = cp_parser_string_literal (parser, false, false);
27364 /* Add it to the list. */
27365 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
27366 /* If the next token is not a `,', then the list is
27367 complete. */
27368 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27369 break;
27370 /* Consume the `,' token. */
27371 cp_lexer_consume_token (parser->lexer);
27372 }
27373
27374 return clobbers;
27375 }
27376
27377 /* Parse an asm-label-list.
27378
27379 asm-label-list:
27380 identifier
27381 asm-label-list , identifier
27382
27383 Returns a TREE_LIST, indicating the labels in the order that they
27384 appeared. The TREE_VALUE of each node is a label. */
27385
27386 static tree
27387 cp_parser_asm_label_list (cp_parser* parser)
27388 {
27389 tree labels = NULL_TREE;
27390
27391 while (true)
27392 {
27393 tree identifier, label, name;
27394
27395 /* Look for the identifier. */
27396 identifier = cp_parser_identifier (parser);
27397 if (!error_operand_p (identifier))
27398 {
27399 label = lookup_label (identifier);
27400 if (TREE_CODE (label) == LABEL_DECL)
27401 {
27402 TREE_USED (label) = 1;
27403 check_goto (label);
27404 name = build_string (IDENTIFIER_LENGTH (identifier),
27405 IDENTIFIER_POINTER (identifier));
27406 labels = tree_cons (name, label, labels);
27407 }
27408 }
27409 /* If the next token is not a `,', then the list is
27410 complete. */
27411 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27412 break;
27413 /* Consume the `,' token. */
27414 cp_lexer_consume_token (parser->lexer);
27415 }
27416
27417 return nreverse (labels);
27418 }
27419
27420 /* Return TRUE iff the next tokens in the stream are possibly the
27421 beginning of a GNU extension attribute. */
27422
27423 static bool
27424 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
27425 {
27426 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
27427 }
27428
27429 /* Return TRUE iff the next tokens in the stream are possibly the
27430 beginning of a standard C++-11 attribute specifier. */
27431
27432 static bool
27433 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
27434 {
27435 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
27436 }
27437
27438 /* Return TRUE iff the next Nth tokens in the stream are possibly the
27439 beginning of a standard C++-11 attribute specifier. */
27440
27441 static bool
27442 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
27443 {
27444 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
27445
27446 return (cxx_dialect >= cxx11
27447 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
27448 || (token->type == CPP_OPEN_SQUARE
27449 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
27450 && token->type == CPP_OPEN_SQUARE)));
27451 }
27452
27453 /* Return TRUE iff the next Nth tokens in the stream are possibly the
27454 beginning of a GNU extension attribute. */
27455
27456 static bool
27457 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
27458 {
27459 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
27460
27461 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
27462 }
27463
27464 /* Return true iff the next tokens can be the beginning of either a
27465 GNU attribute list, or a standard C++11 attribute sequence. */
27466
27467 static bool
27468 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
27469 {
27470 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
27471 || cp_next_tokens_can_be_std_attribute_p (parser));
27472 }
27473
27474 /* Return true iff the next Nth tokens can be the beginning of either
27475 a GNU attribute list, or a standard C++11 attribute sequence. */
27476
27477 static bool
27478 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
27479 {
27480 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
27481 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
27482 }
27483
27484 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
27485 of GNU attributes, or return NULL. */
27486
27487 static tree
27488 cp_parser_attributes_opt (cp_parser *parser)
27489 {
27490 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
27491 return cp_parser_gnu_attributes_opt (parser);
27492 return cp_parser_std_attribute_spec_seq (parser);
27493 }
27494
27495 /* Parse an (optional) series of attributes.
27496
27497 attributes:
27498 attributes attribute
27499
27500 attribute:
27501 __attribute__ (( attribute-list [opt] ))
27502
27503 The return value is as for cp_parser_gnu_attribute_list. */
27504
27505 static tree
27506 cp_parser_gnu_attributes_opt (cp_parser* parser)
27507 {
27508 tree attributes = NULL_TREE;
27509
27510 auto cleanup = make_temp_override
27511 (parser->auto_is_implicit_function_template_parm_p, false);
27512
27513 while (true)
27514 {
27515 cp_token *token;
27516 tree attribute_list;
27517 bool ok = true;
27518
27519 /* Peek at the next token. */
27520 token = cp_lexer_peek_token (parser->lexer);
27521 /* If it's not `__attribute__', then we're done. */
27522 if (token->keyword != RID_ATTRIBUTE)
27523 break;
27524
27525 /* Consume the `__attribute__' keyword. */
27526 cp_lexer_consume_token (parser->lexer);
27527 /* Look for the two `(' tokens. */
27528 matching_parens outer_parens;
27529 if (!outer_parens.require_open (parser))
27530 ok = false;
27531 matching_parens inner_parens;
27532 if (!inner_parens.require_open (parser))
27533 ok = false;
27534
27535 /* Peek at the next token. */
27536 token = cp_lexer_peek_token (parser->lexer);
27537 if (token->type != CPP_CLOSE_PAREN)
27538 /* Parse the attribute-list. */
27539 attribute_list = cp_parser_gnu_attribute_list (parser);
27540 else
27541 /* If the next token is a `)', then there is no attribute
27542 list. */
27543 attribute_list = NULL;
27544
27545 /* Look for the two `)' tokens. */
27546 if (!inner_parens.require_close (parser))
27547 ok = false;
27548 if (!outer_parens.require_close (parser))
27549 ok = false;
27550 if (!ok)
27551 cp_parser_skip_to_end_of_statement (parser);
27552
27553 /* Add these new attributes to the list. */
27554 attributes = attr_chainon (attributes, attribute_list);
27555 }
27556
27557 return attributes;
27558 }
27559
27560 /* Parse a GNU attribute-list.
27561
27562 attribute-list:
27563 attribute
27564 attribute-list , attribute
27565
27566 attribute:
27567 identifier
27568 identifier ( identifier )
27569 identifier ( identifier , expression-list )
27570 identifier ( expression-list )
27571
27572 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
27573 to an attribute. The TREE_PURPOSE of each node is the identifier
27574 indicating which attribute is in use. The TREE_VALUE represents
27575 the arguments, if any. */
27576
27577 static tree
27578 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
27579 {
27580 tree attribute_list = NULL_TREE;
27581 bool save_translate_strings_p = parser->translate_strings_p;
27582
27583 /* Don't create wrapper nodes within attributes: the
27584 handlers don't know how to handle them. */
27585 auto_suppress_location_wrappers sentinel;
27586
27587 parser->translate_strings_p = false;
27588 while (true)
27589 {
27590 cp_token *token;
27591 tree identifier;
27592 tree attribute;
27593
27594 /* Look for the identifier. We also allow keywords here; for
27595 example `__attribute__ ((const))' is legal. */
27596 token = cp_lexer_peek_token (parser->lexer);
27597 if (token->type == CPP_NAME
27598 || token->type == CPP_KEYWORD)
27599 {
27600 tree arguments = NULL_TREE;
27601
27602 /* Consume the token, but save it since we need it for the
27603 SIMD enabled function parsing. */
27604 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
27605
27606 /* Save away the identifier that indicates which attribute
27607 this is. */
27608 identifier = (token->type == CPP_KEYWORD)
27609 /* For keywords, use the canonical spelling, not the
27610 parsed identifier. */
27611 ? ridpointers[(int) token->keyword]
27612 : id_token->u.value;
27613
27614 identifier = canonicalize_attr_name (identifier);
27615 attribute = build_tree_list (identifier, NULL_TREE);
27616
27617 /* Peek at the next token. */
27618 token = cp_lexer_peek_token (parser->lexer);
27619 /* If it's an `(', then parse the attribute arguments. */
27620 if (token->type == CPP_OPEN_PAREN)
27621 {
27622 vec<tree, va_gc> *vec;
27623 int attr_flag = (attribute_takes_identifier_p (identifier)
27624 ? id_attr : normal_attr);
27625 vec = cp_parser_parenthesized_expression_list
27626 (parser, attr_flag, /*cast_p=*/false,
27627 /*allow_expansion_p=*/false,
27628 /*non_constant_p=*/NULL);
27629 if (vec == NULL)
27630 arguments = error_mark_node;
27631 else
27632 {
27633 arguments = build_tree_list_vec (vec);
27634 release_tree_vector (vec);
27635 }
27636 /* Save the arguments away. */
27637 TREE_VALUE (attribute) = arguments;
27638 }
27639
27640 if (arguments != error_mark_node)
27641 {
27642 /* Add this attribute to the list. */
27643 TREE_CHAIN (attribute) = attribute_list;
27644 attribute_list = attribute;
27645 }
27646
27647 token = cp_lexer_peek_token (parser->lexer);
27648 }
27649 /* Unless EXACTLY_ONE is set look for more attributes.
27650 If the next token isn't a `,', we're done. */
27651 if (exactly_one || token->type != CPP_COMMA)
27652 break;
27653
27654 /* Consume the comma and keep going. */
27655 cp_lexer_consume_token (parser->lexer);
27656 }
27657 parser->translate_strings_p = save_translate_strings_p;
27658
27659 /* We built up the list in reverse order. */
27660 return nreverse (attribute_list);
27661 }
27662
27663 /* Parse a standard C++11 attribute.
27664
27665 The returned representation is a TREE_LIST which TREE_PURPOSE is
27666 the scoped name of the attribute, and the TREE_VALUE is its
27667 arguments list.
27668
27669 Note that the scoped name of the attribute is itself a TREE_LIST
27670 which TREE_PURPOSE is the namespace of the attribute, and
27671 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
27672 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
27673 and which TREE_PURPOSE is directly the attribute name.
27674
27675 Clients of the attribute code should use get_attribute_namespace
27676 and get_attribute_name to get the actual namespace and name of
27677 attributes, regardless of their being GNU or C++11 attributes.
27678
27679 attribute:
27680 attribute-token attribute-argument-clause [opt]
27681
27682 attribute-token:
27683 identifier
27684 attribute-scoped-token
27685
27686 attribute-scoped-token:
27687 attribute-namespace :: identifier
27688
27689 attribute-namespace:
27690 identifier
27691
27692 attribute-argument-clause:
27693 ( balanced-token-seq )
27694
27695 balanced-token-seq:
27696 balanced-token [opt]
27697 balanced-token-seq balanced-token
27698
27699 balanced-token:
27700 ( balanced-token-seq )
27701 [ balanced-token-seq ]
27702 { balanced-token-seq }. */
27703
27704 static tree
27705 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
27706 {
27707 tree attribute, attr_id = NULL_TREE, arguments;
27708 cp_token *token;
27709
27710 auto cleanup = make_temp_override
27711 (parser->auto_is_implicit_function_template_parm_p, false);
27712
27713 /* First, parse name of the attribute, a.k.a attribute-token. */
27714
27715 token = cp_lexer_peek_token (parser->lexer);
27716 if (token->type == CPP_NAME)
27717 attr_id = token->u.value;
27718 else if (token->type == CPP_KEYWORD)
27719 attr_id = ridpointers[(int) token->keyword];
27720 else if (token->flags & NAMED_OP)
27721 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
27722
27723 if (attr_id == NULL_TREE)
27724 return NULL_TREE;
27725
27726 cp_lexer_consume_token (parser->lexer);
27727
27728 token = cp_lexer_peek_token (parser->lexer);
27729 if (token->type == CPP_SCOPE)
27730 {
27731 /* We are seeing a scoped attribute token. */
27732
27733 cp_lexer_consume_token (parser->lexer);
27734 if (attr_ns)
27735 error_at (token->location, "attribute using prefix used together "
27736 "with scoped attribute token");
27737 attr_ns = attr_id;
27738
27739 token = cp_lexer_peek_token (parser->lexer);
27740 if (token->type == CPP_NAME)
27741 attr_id = token->u.value;
27742 else if (token->type == CPP_KEYWORD)
27743 attr_id = ridpointers[(int) token->keyword];
27744 else if (token->flags & NAMED_OP)
27745 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
27746 else
27747 {
27748 error_at (token->location,
27749 "expected an identifier for the attribute name");
27750 return error_mark_node;
27751 }
27752 cp_lexer_consume_token (parser->lexer);
27753
27754 attr_ns = canonicalize_attr_name (attr_ns);
27755 attr_id = canonicalize_attr_name (attr_id);
27756 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
27757 NULL_TREE);
27758 token = cp_lexer_peek_token (parser->lexer);
27759 }
27760 else if (attr_ns)
27761 {
27762 attr_ns = canonicalize_attr_name (attr_ns);
27763 attr_id = canonicalize_attr_name (attr_id);
27764 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
27765 NULL_TREE);
27766 }
27767 else
27768 {
27769 attr_id = canonicalize_attr_name (attr_id);
27770 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
27771 NULL_TREE);
27772 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
27773 but no longer: we have to be able to tell [[noreturn]] and
27774 __attribute__((noreturn)) apart. */
27775 /* C++14 deprecated attribute is equivalent to GNU's. */
27776 if (is_attribute_p ("deprecated", attr_id))
27777 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
27778 /* C++17 fallthrough attribute is equivalent to GNU's. */
27779 else if (is_attribute_p ("fallthrough", attr_id))
27780 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
27781 /* Transactional Memory TS optimize_for_synchronized attribute is
27782 equivalent to GNU transaction_callable. */
27783 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
27784 TREE_PURPOSE (attribute)
27785 = get_identifier ("transaction_callable");
27786 /* Transactional Memory attributes are GNU attributes. */
27787 else if (tm_attr_to_mask (attr_id))
27788 TREE_PURPOSE (attribute) = attr_id;
27789 }
27790
27791 /* Now parse the optional argument clause of the attribute. */
27792
27793 if (token->type != CPP_OPEN_PAREN)
27794 return attribute;
27795
27796 {
27797 vec<tree, va_gc> *vec;
27798 int attr_flag = normal_attr;
27799
27800 /* Maybe we don't expect to see any arguments for this attribute. */
27801 const attribute_spec *as
27802 = lookup_attribute_spec (TREE_PURPOSE (attribute));
27803 if (as && as->max_length == 0)
27804 {
27805 error_at (token->location, "%qE attribute does not take any arguments",
27806 attr_id);
27807 cp_parser_skip_to_closing_parenthesis (parser,
27808 /*recovering=*/true,
27809 /*or_comma=*/false,
27810 /*consume_paren=*/true);
27811 return error_mark_node;
27812 }
27813
27814 if (attr_ns == gnu_identifier
27815 && attribute_takes_identifier_p (attr_id))
27816 /* A GNU attribute that takes an identifier in parameter. */
27817 attr_flag = id_attr;
27818
27819 if (as == NULL)
27820 {
27821 /* For unknown attributes, just skip balanced tokens instead of
27822 trying to parse the arguments. */
27823 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
27824 cp_lexer_consume_token (parser->lexer);
27825 return attribute;
27826 }
27827
27828 vec = cp_parser_parenthesized_expression_list
27829 (parser, attr_flag, /*cast_p=*/false,
27830 /*allow_expansion_p=*/true,
27831 /*non_constant_p=*/NULL);
27832 if (vec == NULL)
27833 arguments = error_mark_node;
27834 else
27835 {
27836 if (vec->is_empty ())
27837 /* e.g. [[attr()]]. */
27838 error_at (token->location, "parentheses must be omitted if "
27839 "%qE attribute argument list is empty",
27840 attr_id);
27841 arguments = build_tree_list_vec (vec);
27842 release_tree_vector (vec);
27843 }
27844
27845 if (arguments == error_mark_node)
27846 attribute = error_mark_node;
27847 else
27848 TREE_VALUE (attribute) = arguments;
27849 }
27850
27851 return attribute;
27852 }
27853
27854 /* Warn if the attribute ATTRIBUTE appears more than once in the
27855 attribute-list ATTRIBUTES. This used to be enforced for certain
27856 attributes, but the restriction was removed in P2156. Note that
27857 carries_dependency ([dcl.attr.depend]) isn't implemented yet in GCC.
27858 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
27859 found in ATTRIBUTES. */
27860
27861 static bool
27862 cp_parser_check_std_attribute (location_t loc, tree attributes, tree attribute)
27863 {
27864 static auto alist = { "noreturn", "deprecated", "nodiscard", "maybe_unused",
27865 "likely", "unlikely", "fallthrough",
27866 "no_unique_address" };
27867 if (attributes)
27868 for (const auto &a : alist)
27869 if (is_attribute_p (a, get_attribute_name (attribute))
27870 && lookup_attribute (a, attributes))
27871 {
27872 if (!from_macro_expansion_at (loc))
27873 warning_at (loc, OPT_Wattributes, "attribute %qs specified "
27874 "multiple times", a);
27875 return false;
27876 }
27877 return true;
27878 }
27879
27880 /* Parse a list of standard C++-11 attributes.
27881
27882 attribute-list:
27883 attribute [opt]
27884 attribute-list , attribute[opt]
27885 attribute ...
27886 attribute-list , attribute ...
27887 */
27888
27889 static tree
27890 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
27891 {
27892 tree attributes = NULL_TREE, attribute = NULL_TREE;
27893 cp_token *token = NULL;
27894
27895 while (true)
27896 {
27897 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27898 attribute = cp_parser_std_attribute (parser, attr_ns);
27899 if (attribute == error_mark_node)
27900 break;
27901 if (attribute != NULL_TREE)
27902 {
27903 if (cp_parser_check_std_attribute (loc, attributes, attribute))
27904 {
27905 TREE_CHAIN (attribute) = attributes;
27906 attributes = attribute;
27907 }
27908 }
27909 token = cp_lexer_peek_token (parser->lexer);
27910 if (token->type == CPP_ELLIPSIS)
27911 {
27912 cp_lexer_consume_token (parser->lexer);
27913 if (attribute == NULL_TREE)
27914 error_at (token->location,
27915 "expected attribute before %<...%>");
27916 else
27917 {
27918 tree pack = make_pack_expansion (TREE_VALUE (attribute));
27919 if (pack == error_mark_node)
27920 return error_mark_node;
27921 TREE_VALUE (attribute) = pack;
27922 }
27923 token = cp_lexer_peek_token (parser->lexer);
27924 }
27925 if (token->type != CPP_COMMA)
27926 break;
27927 cp_lexer_consume_token (parser->lexer);
27928 }
27929 attributes = nreverse (attributes);
27930 return attributes;
27931 }
27932
27933 /* Parse a standard C++-11 attribute specifier.
27934
27935 attribute-specifier:
27936 [ [ attribute-using-prefix [opt] attribute-list ] ]
27937 alignment-specifier
27938
27939 attribute-using-prefix:
27940 using attribute-namespace :
27941
27942 alignment-specifier:
27943 alignas ( type-id ... [opt] )
27944 alignas ( alignment-expression ... [opt] ). */
27945
27946 static tree
27947 cp_parser_std_attribute_spec (cp_parser *parser)
27948 {
27949 tree attributes = NULL_TREE;
27950 cp_token *token = cp_lexer_peek_token (parser->lexer);
27951
27952 if (token->type == CPP_OPEN_SQUARE
27953 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
27954 {
27955 tree attr_ns = NULL_TREE;
27956
27957 cp_lexer_consume_token (parser->lexer);
27958 cp_lexer_consume_token (parser->lexer);
27959
27960 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27961 {
27962 token = cp_lexer_peek_nth_token (parser->lexer, 2);
27963 if (token->type == CPP_NAME)
27964 attr_ns = token->u.value;
27965 else if (token->type == CPP_KEYWORD)
27966 attr_ns = ridpointers[(int) token->keyword];
27967 else if (token->flags & NAMED_OP)
27968 attr_ns = get_identifier (cpp_type2name (token->type,
27969 token->flags));
27970 if (attr_ns
27971 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
27972 {
27973 if (cxx_dialect < cxx17)
27974 pedwarn (input_location, 0,
27975 "attribute using prefix only available "
27976 "with %<-std=c++17%> or %<-std=gnu++17%>");
27977
27978 cp_lexer_consume_token (parser->lexer);
27979 cp_lexer_consume_token (parser->lexer);
27980 cp_lexer_consume_token (parser->lexer);
27981 }
27982 else
27983 attr_ns = NULL_TREE;
27984 }
27985
27986 attributes = cp_parser_std_attribute_list (parser, attr_ns);
27987
27988 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
27989 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
27990 cp_parser_skip_to_end_of_statement (parser);
27991 else
27992 /* Warn about parsing c++11 attribute in non-c++11 mode, only
27993 when we are sure that we have actually parsed them. */
27994 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
27995 }
27996 else
27997 {
27998 tree alignas_expr;
27999
28000 /* Look for an alignment-specifier. */
28001
28002 token = cp_lexer_peek_token (parser->lexer);
28003
28004 if (token->type != CPP_KEYWORD
28005 || token->keyword != RID_ALIGNAS)
28006 return NULL_TREE;
28007
28008 cp_lexer_consume_token (parser->lexer);
28009 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
28010
28011 matching_parens parens;
28012 if (!parens.require_open (parser))
28013 return error_mark_node;
28014
28015 cp_parser_parse_tentatively (parser);
28016 alignas_expr = cp_parser_type_id (parser);
28017
28018 if (!cp_parser_parse_definitely (parser))
28019 {
28020 alignas_expr = cp_parser_assignment_expression (parser);
28021 if (alignas_expr == error_mark_node)
28022 cp_parser_skip_to_end_of_statement (parser);
28023 if (alignas_expr == NULL_TREE
28024 || alignas_expr == error_mark_node)
28025 return alignas_expr;
28026 }
28027
28028 alignas_expr = cxx_alignas_expr (alignas_expr);
28029 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
28030
28031 /* Handle alignas (pack...). */
28032 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28033 {
28034 cp_lexer_consume_token (parser->lexer);
28035 alignas_expr = make_pack_expansion (alignas_expr);
28036 }
28037
28038 /* Something went wrong, so don't build the attribute. */
28039 if (alignas_expr == error_mark_node)
28040 return error_mark_node;
28041
28042 /* Missing ')' means the code cannot possibly be valid; go ahead
28043 and commit to make sure we issue a hard error. */
28044 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
28045 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28046 cp_parser_commit_to_tentative_parse (parser);
28047
28048 if (!parens.require_close (parser))
28049 return error_mark_node;
28050
28051 /* Build the C++-11 representation of an 'aligned'
28052 attribute. */
28053 attributes
28054 = build_tree_list (build_tree_list (gnu_identifier,
28055 aligned_identifier), alignas_expr);
28056 }
28057
28058 return attributes;
28059 }
28060
28061 /* Parse a standard C++-11 attribute-specifier-seq.
28062
28063 attribute-specifier-seq:
28064 attribute-specifier-seq [opt] attribute-specifier
28065 */
28066
28067 static tree
28068 cp_parser_std_attribute_spec_seq (cp_parser *parser)
28069 {
28070 tree attr_specs = NULL_TREE;
28071 tree attr_last = NULL_TREE;
28072
28073 /* Don't create wrapper nodes within attributes: the
28074 handlers don't know how to handle them. */
28075 auto_suppress_location_wrappers sentinel;
28076
28077 while (true)
28078 {
28079 tree attr_spec = cp_parser_std_attribute_spec (parser);
28080 if (attr_spec == NULL_TREE)
28081 break;
28082 if (attr_spec == error_mark_node)
28083 return error_mark_node;
28084
28085 if (attr_last)
28086 TREE_CHAIN (attr_last) = attr_spec;
28087 else
28088 attr_specs = attr_last = attr_spec;
28089 attr_last = tree_last (attr_last);
28090 }
28091
28092 return attr_specs;
28093 }
28094
28095 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
28096 return index of the first token after balanced-token, or N on failure. */
28097
28098 static size_t
28099 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
28100 {
28101 size_t orig_n = n;
28102 int nparens = 0, nbraces = 0, nsquares = 0;
28103 do
28104 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
28105 {
28106 case CPP_PRAGMA_EOL:
28107 if (!parser->lexer->in_pragma)
28108 break;
28109 /* FALLTHRU */
28110 case CPP_EOF:
28111 /* Ran out of tokens. */
28112 return orig_n;
28113 case CPP_OPEN_PAREN:
28114 ++nparens;
28115 break;
28116 case CPP_OPEN_BRACE:
28117 ++nbraces;
28118 break;
28119 case CPP_OPEN_SQUARE:
28120 ++nsquares;
28121 break;
28122 case CPP_CLOSE_PAREN:
28123 --nparens;
28124 break;
28125 case CPP_CLOSE_BRACE:
28126 --nbraces;
28127 break;
28128 case CPP_CLOSE_SQUARE:
28129 --nsquares;
28130 break;
28131 default:
28132 break;
28133 }
28134 while (nparens || nbraces || nsquares);
28135 return n;
28136 }
28137
28138 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
28139 return index of the first token after the GNU attribute tokens, or N on
28140 failure. */
28141
28142 static size_t
28143 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
28144 {
28145 while (true)
28146 {
28147 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
28148 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
28149 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
28150 break;
28151
28152 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
28153 if (n2 == n + 2)
28154 break;
28155 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
28156 break;
28157 n = n2 + 1;
28158 }
28159 return n;
28160 }
28161
28162 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
28163 next token), return index of the first token after the standard C++11
28164 attribute tokens, or N on failure. */
28165
28166 static size_t
28167 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
28168 {
28169 while (true)
28170 {
28171 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
28172 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
28173 {
28174 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
28175 if (n2 == n + 1)
28176 break;
28177 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
28178 break;
28179 n = n2 + 1;
28180 }
28181 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
28182 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
28183 {
28184 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
28185 if (n2 == n + 1)
28186 break;
28187 n = n2;
28188 }
28189 else
28190 break;
28191 }
28192 return n;
28193 }
28194
28195 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
28196 as the next token), return index of the first token after the attribute
28197 tokens, or N on failure. */
28198
28199 static size_t
28200 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
28201 {
28202 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
28203 return cp_parser_skip_gnu_attributes_opt (parser, n);
28204 return cp_parser_skip_std_attribute_spec_seq (parser, n);
28205 }
28206
28207 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
28208 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
28209 current value of the PEDANTIC flag, regardless of whether or not
28210 the `__extension__' keyword is present. The caller is responsible
28211 for restoring the value of the PEDANTIC flag. */
28212
28213 static bool
28214 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
28215 {
28216 /* Save the old value of the PEDANTIC flag. */
28217 *saved_pedantic = pedantic;
28218
28219 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
28220 {
28221 /* Consume the `__extension__' token. */
28222 cp_lexer_consume_token (parser->lexer);
28223 /* We're not being pedantic while the `__extension__' keyword is
28224 in effect. */
28225 pedantic = 0;
28226
28227 return true;
28228 }
28229
28230 return false;
28231 }
28232
28233 /* Parse a label declaration.
28234
28235 label-declaration:
28236 __label__ label-declarator-seq ;
28237
28238 label-declarator-seq:
28239 identifier , label-declarator-seq
28240 identifier */
28241
28242 static void
28243 cp_parser_label_declaration (cp_parser* parser)
28244 {
28245 /* Look for the `__label__' keyword. */
28246 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
28247
28248 while (true)
28249 {
28250 tree identifier;
28251
28252 /* Look for an identifier. */
28253 identifier = cp_parser_identifier (parser);
28254 /* If we failed, stop. */
28255 if (identifier == error_mark_node)
28256 break;
28257 /* Declare it as a label. */
28258 finish_label_decl (identifier);
28259 /* If the next token is a `;', stop. */
28260 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28261 break;
28262 /* Look for the `,' separating the label declarations. */
28263 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
28264 }
28265
28266 /* Look for the final `;'. */
28267 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28268 }
28269
28270 // -------------------------------------------------------------------------- //
28271 // Concept definitions
28272
28273 static tree
28274 cp_parser_concept_definition (cp_parser *parser)
28275 {
28276 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
28277 cp_lexer_consume_token (parser->lexer);
28278
28279 cp_expr id = cp_parser_identifier (parser);
28280 if (id == error_mark_node)
28281 {
28282 cp_parser_skip_to_end_of_statement (parser);
28283 cp_parser_consume_semicolon_at_end_of_statement (parser);
28284 return NULL_TREE;
28285 }
28286
28287 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28288 {
28289 cp_parser_skip_to_end_of_statement (parser);
28290 cp_parser_consume_semicolon_at_end_of_statement (parser);
28291 return error_mark_node;
28292 }
28293
28294 processing_constraint_expression_sentinel parsing_constraint;
28295 tree init = cp_parser_constraint_expression (parser);
28296 if (init == error_mark_node)
28297 cp_parser_skip_to_end_of_statement (parser);
28298
28299 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
28300 but continue as if it were. */
28301 cp_parser_consume_semicolon_at_end_of_statement (parser);
28302
28303 return finish_concept_definition (id, init);
28304 }
28305
28306 // -------------------------------------------------------------------------- //
28307 // Requires Clause
28308
28309 /* Diagnose an expression that should appear in ()'s within a requires-clause
28310 and suggest where to place those parentheses. */
28311
28312 static void
28313 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
28314 {
28315 error_at (loc, "expression must be enclosed in parentheses");
28316 }
28317
28318 static void
28319 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
28320 {
28321 gcc_rich_location richloc (loc);
28322 richloc.add_fixit_insert_before ("(");
28323 richloc.add_fixit_insert_after (")");
28324 error_at (&richloc, "expression must be enclosed in parentheses");
28325 }
28326
28327 /* Characterizes the likely kind of expression intended by a mis-written
28328 primary constraint. */
28329 enum primary_constraint_error
28330 {
28331 pce_ok,
28332 pce_maybe_operator,
28333 pce_maybe_postfix
28334 };
28335
28336 /* Returns true if the token(s) following a primary-expression in a
28337 constraint-logical-* expression would require parentheses. */
28338
28339 static primary_constraint_error
28340 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
28341 {
28342 cp_token *token = cp_lexer_peek_token (parser->lexer);
28343 switch (token->type)
28344 {
28345 default:
28346 return pce_ok;
28347
28348 case CPP_EQ:
28349 {
28350 /* An equal sign may be part of the definition of a function,
28351 and not an assignment operator, when parsing the expression
28352 for a trailing requires-clause. For example:
28353
28354 template<typename T>
28355 struct S {
28356 S() requires C<T> = default;
28357 };
28358
28359 Don't try to reparse this a binary operator. */
28360 if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
28361 || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
28362 return pce_ok;
28363
28364 gcc_fallthrough ();
28365 }
28366
28367 /* Arithmetic operators. */
28368 case CPP_PLUS:
28369 case CPP_MINUS:
28370 case CPP_MULT:
28371 case CPP_DIV:
28372 case CPP_MOD:
28373 /* Bitwise operators. */
28374 case CPP_AND:
28375 case CPP_OR:
28376 case CPP_XOR:
28377 case CPP_RSHIFT:
28378 case CPP_LSHIFT:
28379 /* Relational operators. */
28380 case CPP_EQ_EQ:
28381 case CPP_NOT_EQ:
28382 case CPP_LESS:
28383 case CPP_GREATER:
28384 case CPP_LESS_EQ:
28385 case CPP_GREATER_EQ:
28386 case CPP_SPACESHIP:
28387 /* Pointer-to-member. */
28388 case CPP_DOT_STAR:
28389 case CPP_DEREF_STAR:
28390 /* Assignment operators. */
28391 case CPP_PLUS_EQ:
28392 case CPP_MINUS_EQ:
28393 case CPP_MULT_EQ:
28394 case CPP_DIV_EQ:
28395 case CPP_MOD_EQ:
28396 case CPP_AND_EQ:
28397 case CPP_OR_EQ:
28398 case CPP_XOR_EQ:
28399 case CPP_RSHIFT_EQ:
28400 case CPP_LSHIFT_EQ:
28401 /* Conditional operator */
28402 case CPP_QUERY:
28403 /* Unenclosed binary or conditional operator. */
28404 return pce_maybe_operator;
28405
28406 case CPP_OPEN_PAREN:
28407 {
28408 /* A primary constraint that precedes the parameter-list of a
28409 lambda expression is followed by an open paren.
28410
28411 []<typename T> requires C (T a, T b) { ... }
28412
28413 Don't try to re-parse this as a postfix expression. */
28414 if (lambda_p)
28415 return pce_ok;
28416
28417 gcc_fallthrough ();
28418 }
28419 case CPP_OPEN_SQUARE:
28420 {
28421 /* A primary-constraint-expression followed by a '[[' is not a
28422 postfix expression. */
28423 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
28424 return pce_ok;
28425
28426 gcc_fallthrough ();
28427 }
28428 case CPP_PLUS_PLUS:
28429 case CPP_MINUS_MINUS:
28430 case CPP_DOT:
28431 case CPP_DEREF:
28432 /* Unenclosed postfix operator. */
28433 return pce_maybe_postfix;
28434 }
28435 }
28436
28437 /* Returns true if the next token begins a unary expression, preceded by
28438 an operator or keyword. */
28439
28440 static bool
28441 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
28442 {
28443 cp_token *token = cp_lexer_peek_token (parser->lexer);
28444 switch (token->type)
28445 {
28446 case CPP_NOT:
28447 case CPP_PLUS:
28448 case CPP_MINUS:
28449 case CPP_MULT:
28450 case CPP_COMPL:
28451 case CPP_PLUS_PLUS:
28452 case CPP_MINUS_MINUS:
28453 return true;
28454
28455 case CPP_KEYWORD:
28456 {
28457 switch (token->keyword)
28458 {
28459 case RID_STATCAST:
28460 case RID_DYNCAST:
28461 case RID_REINTCAST:
28462 case RID_CONSTCAST:
28463 case RID_TYPEID:
28464 case RID_SIZEOF:
28465 case RID_ALIGNOF:
28466 case RID_NOEXCEPT:
28467 case RID_NEW:
28468 case RID_DELETE:
28469 case RID_THROW:
28470 return true;
28471
28472 default:
28473 break;
28474 }
28475 }
28476
28477 default:
28478 break;
28479 }
28480
28481 return false;
28482 }
28483
28484 /* Parse a primary expression within a constraint. */
28485
28486 static cp_expr
28487 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
28488 {
28489 /* If this looks like a unary expression, parse it as such, but diagnose
28490 it as ill-formed; it requires parens. */
28491 if (cp_parser_unary_constraint_requires_parens (parser))
28492 {
28493 cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
28494 cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
28495 return e;
28496 }
28497
28498 cp_lexer_save_tokens (parser->lexer);
28499 cp_id_kind idk;
28500 location_t loc = input_location;
28501 cp_expr expr = cp_parser_primary_expression (parser,
28502 /*address_p=*/false,
28503 /*cast_p=*/false,
28504 /*template_arg_p=*/false,
28505 &idk);
28506 expr.maybe_add_location_wrapper ();
28507
28508 primary_constraint_error pce = pce_ok;
28509 if (expr != error_mark_node)
28510 {
28511 /* The primary-expression could be part of an unenclosed non-logical
28512 compound expression. */
28513 pce = cp_parser_constraint_requires_parens (parser, lambda_p);
28514 }
28515 if (pce == pce_ok)
28516 {
28517 cp_lexer_commit_tokens (parser->lexer);
28518 return finish_constraint_primary_expr (expr);
28519 }
28520
28521 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
28522 error, but return the expression as if it were valid. */
28523 cp_lexer_rollback_tokens (parser->lexer);
28524 cp_parser_parse_tentatively (parser);
28525 if (pce == pce_maybe_operator)
28526 expr = cp_parser_assignment_expression (parser, NULL, false, false);
28527 else
28528 expr = cp_parser_simple_cast_expression (parser);
28529 if (cp_parser_parse_definitely (parser))
28530 {
28531 cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
28532 return expr;
28533 }
28534
28535 /* Otherwise, something has gone very wrong, and we can't generate a more
28536 meaningful diagnostic or recover. */
28537 cp_parser_diagnose_ungrouped_constraint_plain (loc);
28538 return error_mark_node;
28539 }
28540
28541 /* Parse a constraint-logical-and-expression.
28542
28543 constraint-logical-and-expression:
28544 primary-expression
28545 constraint-logical-and-expression '&&' primary-expression */
28546
28547 static cp_expr
28548 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
28549 {
28550 cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
28551 while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
28552 {
28553 cp_token *op = cp_lexer_consume_token (parser->lexer);
28554 tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
28555 lhs = finish_constraint_and_expr (op->location, lhs, rhs);
28556 }
28557 return lhs;
28558 }
28559
28560 /* Parse a constraint-logical-or-expression.
28561
28562 constraint-logical-or-expression:
28563 constraint-logical-and-expression
28564 constraint-logical-or-expression '||' constraint-logical-and-expression */
28565
28566 static cp_expr
28567 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
28568 {
28569 cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
28570 while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
28571 {
28572 cp_token *op = cp_lexer_consume_token (parser->lexer);
28573 cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
28574 lhs = finish_constraint_or_expr (op->location, lhs, rhs);
28575 }
28576 return lhs;
28577 }
28578
28579 /* Parse the expression after a requires-clause. This has a different grammar
28580 than that in the concepts TS. */
28581
28582 static tree
28583 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
28584 {
28585 processing_constraint_expression_sentinel parsing_constraint;
28586 temp_override<int> ovr (processing_template_decl);
28587 if (!processing_template_decl)
28588 /* Adjust processing_template_decl so that we always obtain template
28589 trees here. We don't do the usual ++processing_template_decl
28590 because that would skew the template parameter depth of a lambda
28591 within if we're already inside a template. */
28592 processing_template_decl = 1;
28593 cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
28594 if (check_for_bare_parameter_packs (expr))
28595 expr = error_mark_node;
28596 return expr;
28597 }
28598
28599 /* Parse a expression after a requires clause.
28600
28601 constraint-expression:
28602 logical-or-expression
28603
28604 The required logical-or-expression must be a constant expression. Note
28605 that we don't check that the expression is constepxr here. We defer until
28606 we analyze constraints and then, we only check atomic constraints. */
28607
28608 static tree
28609 cp_parser_constraint_expression (cp_parser *parser)
28610 {
28611 processing_constraint_expression_sentinel parsing_constraint;
28612 temp_override<int> ovr (processing_template_decl);
28613 if (!processing_template_decl)
28614 /* As in cp_parser_requires_clause_expression. */
28615 processing_template_decl = 1;
28616 cp_expr expr = cp_parser_binary_expression (parser, false, true,
28617 PREC_NOT_OPERATOR, NULL);
28618 if (check_for_bare_parameter_packs (expr))
28619 expr = error_mark_node;
28620 expr.maybe_add_location_wrapper ();
28621 return expr;
28622 }
28623
28624 /* Optionally parse a requires clause:
28625
28626 requires-clause:
28627 `requires` constraint-logical-or-expression.
28628 [ConceptsTS]
28629 `requires constraint-expression.
28630
28631 LAMBDA_P is true when the requires-clause is parsed before the
28632 parameter-list of a lambda-declarator. */
28633
28634 static tree
28635 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
28636 {
28637 cp_token *tok = cp_lexer_peek_token (parser->lexer);
28638 if (tok->keyword != RID_REQUIRES)
28639 {
28640 if (!flag_concepts && tok->type == CPP_NAME
28641 && tok->u.value == ridpointers[RID_REQUIRES])
28642 {
28643 error_at (cp_lexer_peek_token (parser->lexer)->location,
28644 "%<requires%> only available with "
28645 "%<-std=c++20%> or %<-fconcepts%>");
28646 /* Parse and discard the requires-clause. */
28647 cp_lexer_consume_token (parser->lexer);
28648 cp_parser_constraint_expression (parser);
28649 }
28650 return NULL_TREE;
28651 }
28652
28653 cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
28654 if (tok2->type == CPP_OPEN_BRACE)
28655 {
28656 /* An opening brace following the start of a requires-clause is
28657 ill-formed; the user likely forgot the second `requires' that
28658 would start a requires-expression. */
28659 gcc_rich_location richloc (tok2->location);
28660 richloc.add_fixit_insert_after (tok->location, " requires");
28661 error_at (&richloc, "missing additional %<requires%> to start "
28662 "a requires-expression");
28663 /* Don't consume the `requires', so that it's reused as the start of a
28664 requires-expression. */
28665 }
28666 else
28667 cp_lexer_consume_token (parser->lexer);
28668
28669 if (!flag_concepts_ts)
28670 return cp_parser_requires_clause_expression (parser, lambda_p);
28671 else
28672 return cp_parser_constraint_expression (parser);
28673 }
28674
28675 /*---------------------------------------------------------------------------
28676 Requires expressions
28677 ---------------------------------------------------------------------------*/
28678
28679 /* Parse a requires expression
28680
28681 requirement-expression:
28682 'requires' requirement-parameter-list [opt] requirement-body */
28683
28684 static tree
28685 cp_parser_requires_expression (cp_parser *parser)
28686 {
28687 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
28688 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
28689
28690 /* Avoid committing to outer tentative parse. */
28691 tentative_firewall firewall (parser);
28692
28693 /* This is definitely a requires-expression. */
28694 cp_parser_commit_to_tentative_parse (parser);
28695
28696 tree parms, reqs;
28697 {
28698 /* Local parameters are delared as variables within the scope
28699 of the expression. They are not visible past the end of
28700 the expression. Expressions within the requires-expression
28701 are unevaluated. */
28702 struct scope_sentinel
28703 {
28704 scope_sentinel ()
28705 {
28706 ++cp_unevaluated_operand;
28707 begin_scope (sk_block, NULL_TREE);
28708 }
28709
28710 ~scope_sentinel ()
28711 {
28712 pop_bindings_and_leave_scope ();
28713 --cp_unevaluated_operand;
28714 }
28715 } s;
28716
28717 /* Parse the optional parameter list. */
28718 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28719 {
28720 parms = cp_parser_requirement_parameter_list (parser);
28721 if (parms == error_mark_node)
28722 return error_mark_node;
28723 }
28724 else
28725 parms = NULL_TREE;
28726
28727 /* Parse the requirement body. */
28728 temp_override<int> ovr (processing_template_decl);
28729 if (!processing_template_decl)
28730 /* As in cp_parser_requires_clause_expression. */
28731 processing_template_decl = 1;
28732 reqs = cp_parser_requirement_body (parser);
28733 if (reqs == error_mark_node)
28734 return error_mark_node;
28735 }
28736
28737 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
28738 the parm chain. */
28739 grokparms (parms, &parms);
28740 loc = make_location (loc, loc, parser->lexer);
28741 tree expr = finish_requires_expr (loc, parms, reqs);
28742 if (!processing_template_decl)
28743 {
28744 /* Perform semantic processing now to diagnose any invalid types and
28745 expressions. */
28746 int saved_errorcount = errorcount;
28747 tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
28748 if (errorcount > saved_errorcount)
28749 return error_mark_node;
28750 }
28751 return expr;
28752 }
28753
28754 /* Parse a parameterized requirement.
28755
28756 requirement-parameter-list:
28757 '(' parameter-declaration-clause ')' */
28758
28759 static tree
28760 cp_parser_requirement_parameter_list (cp_parser *parser)
28761 {
28762 matching_parens parens;
28763 if (!parens.require_open (parser))
28764 return error_mark_node;
28765
28766 tree parms = (cp_parser_parameter_declaration_clause
28767 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
28768
28769 if (!parens.require_close (parser))
28770 return error_mark_node;
28771
28772 return parms;
28773 }
28774
28775 /* Parse the body of a requirement.
28776
28777 requirement-body:
28778 '{' requirement-list '}' */
28779 static tree
28780 cp_parser_requirement_body (cp_parser *parser)
28781 {
28782 matching_braces braces;
28783 if (!braces.require_open (parser))
28784 return error_mark_node;
28785
28786 tree reqs = cp_parser_requirement_seq (parser);
28787
28788 if (!braces.require_close (parser))
28789 return error_mark_node;
28790
28791 return reqs;
28792 }
28793
28794 /* Parse a sequence of requirements.
28795
28796 requirement-seq:
28797 requirement
28798 requirement-seq requirement */
28799
28800 static tree
28801 cp_parser_requirement_seq (cp_parser *parser)
28802 {
28803 tree result = NULL_TREE;
28804 do
28805 {
28806 tree req = cp_parser_requirement (parser);
28807 if (req != error_mark_node)
28808 result = tree_cons (NULL_TREE, req, result);
28809 } while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE));
28810
28811 /* If there are no valid requirements, this is not a valid expression. */
28812 if (!result)
28813 return error_mark_node;
28814
28815 /* Reverse the order of requirements so they are analyzed in order. */
28816 return nreverse (result);
28817 }
28818
28819 /* Parse a syntactic requirement or type requirement.
28820
28821 requirement:
28822 simple-requirement
28823 compound-requirement
28824 type-requirement
28825 nested-requirement */
28826
28827 static tree
28828 cp_parser_requirement (cp_parser *parser)
28829 {
28830 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28831 return cp_parser_compound_requirement (parser);
28832 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
28833 return cp_parser_type_requirement (parser);
28834 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
28835 return cp_parser_nested_requirement (parser);
28836 else
28837 return cp_parser_simple_requirement (parser);
28838 }
28839
28840 /* Parse a simple requirement.
28841
28842 simple-requirement:
28843 expression ';' */
28844
28845 static tree
28846 cp_parser_simple_requirement (cp_parser *parser)
28847 {
28848 location_t start = cp_lexer_peek_token (parser->lexer)->location;
28849 cp_expr expr = cp_parser_expression (parser, NULL, false, false);
28850 if (expr == error_mark_node)
28851 cp_parser_skip_to_end_of_statement (parser);
28852
28853 cp_parser_consume_semicolon_at_end_of_statement (parser);
28854
28855 if (!expr || expr == error_mark_node)
28856 return error_mark_node;
28857
28858 /* Sometimes we don't get locations, so use the cached token location
28859 as a reasonable approximation. */
28860 if (expr.get_location() == UNKNOWN_LOCATION)
28861 expr.set_location (start);
28862
28863 return finish_simple_requirement (expr.get_location (), expr);
28864 }
28865
28866 /* Parse a type requirement
28867
28868 type-requirement
28869 nested-name-specifier [opt] required-type-name ';'
28870
28871 required-type-name:
28872 type-name
28873 'template' [opt] simple-template-id */
28874
28875 static tree
28876 cp_parser_type_requirement (cp_parser *parser)
28877 {
28878 cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
28879 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28880
28881 // Save the scope before parsing name specifiers.
28882 tree saved_scope = parser->scope;
28883 tree saved_object_scope = parser->object_scope;
28884 tree saved_qualifying_scope = parser->qualifying_scope;
28885 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
28886 cp_parser_nested_name_specifier_opt (parser,
28887 /*typename_keyword_p=*/true,
28888 /*check_dependency_p=*/false,
28889 /*type_p=*/true,
28890 /*is_declaration=*/false);
28891
28892 tree type;
28893 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28894 {
28895 cp_lexer_consume_token (parser->lexer);
28896 type = cp_parser_template_id (parser,
28897 /*template_keyword_p=*/true,
28898 /*check_dependency=*/false,
28899 /*tag_type=*/none_type,
28900 /*is_declaration=*/false);
28901 type = make_typename_type (parser->scope, type, typename_type,
28902 /*complain=*/tf_error);
28903 }
28904 else
28905 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
28906
28907 if (TREE_CODE (type) == TYPE_DECL)
28908 type = TREE_TYPE (type);
28909
28910 parser->scope = saved_scope;
28911 parser->object_scope = saved_object_scope;
28912 parser->qualifying_scope = saved_qualifying_scope;
28913
28914 if (type == error_mark_node)
28915 cp_parser_skip_to_end_of_statement (parser);
28916
28917 cp_parser_consume_semicolon_at_end_of_statement (parser);
28918
28919 if (type == error_mark_node)
28920 return error_mark_node;
28921
28922 loc = make_location (loc, start_tok->location, parser->lexer);
28923 return finish_type_requirement (loc, type);
28924 }
28925
28926 /* Parse a compound requirement
28927
28928 compound-requirement:
28929 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
28930
28931 static tree
28932 cp_parser_compound_requirement (cp_parser *parser)
28933 {
28934 /* Parse an expression enclosed in '{ }'s. */
28935 matching_braces braces;
28936 if (!braces.require_open (parser))
28937 return error_mark_node;
28938
28939 cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
28940
28941 tree expr = cp_parser_expression (parser, NULL, false, false);
28942 if (expr == error_mark_node)
28943 cp_parser_skip_to_closing_brace (parser);
28944
28945 if (!braces.require_close (parser))
28946 {
28947 cp_parser_skip_to_end_of_statement (parser);
28948 cp_parser_consume_semicolon_at_end_of_statement (parser);
28949 return error_mark_node;
28950 }
28951
28952 /* If the expression was invalid, skip the remainder of the requirement. */
28953 if (!expr || expr == error_mark_node)
28954 {
28955 cp_parser_skip_to_end_of_statement (parser);
28956 cp_parser_consume_semicolon_at_end_of_statement (parser);
28957 return error_mark_node;
28958 }
28959
28960 /* Parse the optional noexcept. */
28961 bool noexcept_p = false;
28962 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
28963 {
28964 cp_lexer_consume_token (parser->lexer);
28965 noexcept_p = true;
28966 }
28967
28968 /* Parse the optional trailing return type. */
28969 tree type = NULL_TREE;
28970 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
28971 {
28972 cp_lexer_consume_token (parser->lexer);
28973 cp_token *tok = cp_lexer_peek_token (parser->lexer);
28974
28975 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
28976 parser->in_result_type_constraint_p = true;
28977 /* C++20 allows either a type-id or a type-constraint. Parsing
28978 a type-id will subsume the parsing for a type-constraint but
28979 allow for more syntactic forms (e.g., const C<T>*). */
28980 type = cp_parser_trailing_type_id (parser);
28981 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
28982 if (type == error_mark_node)
28983 return error_mark_node;
28984
28985 location_t type_loc = make_location (tok->location, tok->location,
28986 parser->lexer);
28987
28988 /* Check that we haven't written something like 'const C<T>*'. */
28989 if (type_uses_auto (type))
28990 {
28991 if (!is_auto (type))
28992 {
28993 error_at (type_loc,
28994 "result type is not a plain type-constraint");
28995 cp_parser_consume_semicolon_at_end_of_statement (parser);
28996 return error_mark_node;
28997 }
28998 }
28999 else if (!flag_concepts_ts)
29000 /* P1452R2 removed the trailing-return-type option. */
29001 error_at (type_loc,
29002 "return-type-requirement is not a type-constraint");
29003 }
29004
29005 location_t loc = make_location (expr_token->location,
29006 braces.open_location (),
29007 parser->lexer);
29008
29009 cp_parser_consume_semicolon_at_end_of_statement (parser);
29010
29011 if (expr == error_mark_node || type == error_mark_node)
29012 return error_mark_node;
29013
29014 return finish_compound_requirement (loc, expr, type, noexcept_p);
29015 }
29016
29017 /* Parse a nested requirement. This is the same as a requires clause.
29018
29019 nested-requirement:
29020 requires-clause */
29021
29022 static tree
29023 cp_parser_nested_requirement (cp_parser *parser)
29024 {
29025 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
29026 cp_token *tok = cp_lexer_consume_token (parser->lexer);
29027 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29028 tree req = cp_parser_constraint_expression (parser);
29029 if (req == error_mark_node)
29030 cp_parser_skip_to_end_of_statement (parser);
29031 loc = make_location (loc, tok->location, parser->lexer);
29032 cp_parser_consume_semicolon_at_end_of_statement (parser);
29033 if (req == error_mark_node)
29034 return error_mark_node;
29035 return finish_nested_requirement (loc, req);
29036 }
29037
29038 /* Support Functions */
29039
29040 /* Return the appropriate prefer_type argument for lookup_name based on
29041 tag_type. */
29042
29043 static inline LOOK_want
29044 prefer_type_arg (tag_types tag_type)
29045 {
29046 switch (tag_type)
29047 {
29048 case none_type: return LOOK_want::NORMAL; // No preference.
29049 case scope_type: return LOOK_want::TYPE_NAMESPACE; // Type or namespace.
29050 default: return LOOK_want::TYPE; // Type only.
29051 }
29052 }
29053
29054 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
29055 NAME should have one of the representations used for an
29056 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
29057 is returned. If PARSER->SCOPE is a dependent type, then a
29058 SCOPE_REF is returned.
29059
29060 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
29061 returned; the name was already resolved when the TEMPLATE_ID_EXPR
29062 was formed. Abstractly, such entities should not be passed to this
29063 function, because they do not need to be looked up, but it is
29064 simpler to check for this special case here, rather than at the
29065 call-sites.
29066
29067 In cases not explicitly covered above, this function returns a
29068 DECL, OVERLOAD, or baselink representing the result of the lookup.
29069 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
29070 is returned.
29071
29072 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
29073 (e.g., "struct") that was used. In that case bindings that do not
29074 refer to types are ignored.
29075
29076 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
29077 ignored.
29078
29079 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
29080 are ignored.
29081
29082 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
29083 types.
29084
29085 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
29086 TREE_LIST of candidates if name-lookup results in an ambiguity, and
29087 NULL_TREE otherwise. */
29088
29089 static cp_expr
29090 cp_parser_lookup_name (cp_parser *parser, tree name,
29091 enum tag_types tag_type,
29092 bool is_template,
29093 bool is_namespace,
29094 bool check_dependency,
29095 tree *ambiguous_decls,
29096 location_t name_location)
29097 {
29098 tree decl;
29099 tree object_type = parser->context->object_type;
29100
29101 /* Assume that the lookup will be unambiguous. */
29102 if (ambiguous_decls)
29103 *ambiguous_decls = NULL_TREE;
29104
29105 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
29106 no longer valid. Note that if we are parsing tentatively, and
29107 the parse fails, OBJECT_TYPE will be automatically restored. */
29108 parser->context->object_type = NULL_TREE;
29109
29110 if (name == error_mark_node)
29111 return error_mark_node;
29112
29113 /* A template-id has already been resolved; there is no lookup to
29114 do. */
29115 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
29116 return name;
29117 if (BASELINK_P (name))
29118 {
29119 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
29120 == TEMPLATE_ID_EXPR);
29121 return name;
29122 }
29123
29124 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
29125 it should already have been checked to make sure that the name
29126 used matches the type being destroyed. */
29127 if (TREE_CODE (name) == BIT_NOT_EXPR)
29128 {
29129 tree type;
29130
29131 /* Figure out to which type this destructor applies. */
29132 if (parser->scope)
29133 type = parser->scope;
29134 else if (object_type)
29135 type = object_type;
29136 else
29137 type = current_class_type;
29138 /* If that's not a class type, there is no destructor. */
29139 if (!type || !CLASS_TYPE_P (type))
29140 return error_mark_node;
29141
29142 /* In a non-static member function, check implicit this->. */
29143 if (current_class_ref)
29144 return lookup_destructor (current_class_ref, parser->scope, name,
29145 tf_warning_or_error);
29146
29147 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
29148 lazily_declare_fn (sfk_destructor, type);
29149
29150 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
29151 return dtor;
29152
29153 return error_mark_node;
29154 }
29155
29156 /* By this point, the NAME should be an ordinary identifier. If
29157 the id-expression was a qualified name, the qualifying scope is
29158 stored in PARSER->SCOPE at this point. */
29159 gcc_assert (identifier_p (name));
29160
29161 /* Perform the lookup. */
29162 if (parser->scope)
29163 {
29164 bool dependent_p;
29165
29166 if (parser->scope == error_mark_node)
29167 return error_mark_node;
29168
29169 /* If the SCOPE is dependent, the lookup must be deferred until
29170 the template is instantiated -- unless we are explicitly
29171 looking up names in uninstantiated templates. Even then, we
29172 cannot look up the name if the scope is not a class type; it
29173 might, for example, be a template type parameter. */
29174 dependent_p = (TYPE_P (parser->scope)
29175 && dependent_scope_p (parser->scope));
29176 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
29177 && dependent_p)
29178 /* Defer lookup. */
29179 decl = error_mark_node;
29180 else
29181 {
29182 tree pushed_scope = NULL_TREE;
29183
29184 /* If PARSER->SCOPE is a dependent type, then it must be a
29185 class type, and we must not be checking dependencies;
29186 otherwise, we would have processed this lookup above. So
29187 that PARSER->SCOPE is not considered a dependent base by
29188 lookup_member, we must enter the scope here. */
29189 if (dependent_p)
29190 pushed_scope = push_scope (parser->scope);
29191
29192 /* If the PARSER->SCOPE is a template specialization, it
29193 may be instantiated during name lookup. In that case,
29194 errors may be issued. Even if we rollback the current
29195 tentative parse, those errors are valid. */
29196 decl = lookup_qualified_name (parser->scope, name,
29197 prefer_type_arg (tag_type),
29198 /*complain=*/true);
29199
29200 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
29201 lookup result and the nested-name-specifier nominates a class C:
29202 * if the name specified after the nested-name-specifier, when
29203 looked up in C, is the injected-class-name of C (Clause 9), or
29204 * if the name specified after the nested-name-specifier is the
29205 same as the identifier or the simple-template-id's template-
29206 name in the last component of the nested-name-specifier,
29207 the name is instead considered to name the constructor of
29208 class C. [ Note: for example, the constructor is not an
29209 acceptable lookup result in an elaborated-type-specifier so
29210 the constructor would not be used in place of the
29211 injected-class-name. --end note ] Such a constructor name
29212 shall be used only in the declarator-id of a declaration that
29213 names a constructor or in a using-declaration. */
29214 if (tag_type == none_type
29215 && DECL_SELF_REFERENCE_P (decl)
29216 && same_type_p (DECL_CONTEXT (decl), parser->scope))
29217 decl = lookup_qualified_name (parser->scope, ctor_identifier,
29218 prefer_type_arg (tag_type),
29219 /*complain=*/true);
29220
29221 if (pushed_scope)
29222 pop_scope (pushed_scope);
29223 }
29224
29225 /* If the scope is a dependent type and either we deferred lookup or
29226 we did lookup but didn't find the name, rememeber the name. */
29227 if (decl == error_mark_node && TYPE_P (parser->scope)
29228 && dependent_type_p (parser->scope))
29229 {
29230 if (tag_type)
29231 {
29232 tree type;
29233
29234 /* The resolution to Core Issue 180 says that `struct
29235 A::B' should be considered a type-name, even if `A'
29236 is dependent. */
29237 type = make_typename_type (parser->scope, name, tag_type,
29238 /*complain=*/tf_error);
29239 if (type != error_mark_node)
29240 decl = TYPE_NAME (type);
29241 }
29242 else if (is_template
29243 && (cp_parser_next_token_ends_template_argument_p (parser)
29244 || cp_lexer_next_token_is (parser->lexer,
29245 CPP_CLOSE_PAREN)))
29246 decl = make_unbound_class_template (parser->scope,
29247 name, NULL_TREE,
29248 /*complain=*/tf_error);
29249 else
29250 decl = build_qualified_name (/*type=*/NULL_TREE,
29251 parser->scope, name,
29252 is_template);
29253 }
29254 parser->qualifying_scope = parser->scope;
29255 parser->object_scope = NULL_TREE;
29256 }
29257 else if (object_type)
29258 {
29259 /* Look up the name in the scope of the OBJECT_TYPE, unless the
29260 OBJECT_TYPE is not a class. */
29261 if (CLASS_TYPE_P (object_type))
29262 /* If the OBJECT_TYPE is a template specialization, it may
29263 be instantiated during name lookup. In that case, errors
29264 may be issued. Even if we rollback the current tentative
29265 parse, those errors are valid. */
29266 decl = lookup_member (object_type,
29267 name,
29268 /*protect=*/0,
29269 /*prefer_type=*/tag_type != none_type,
29270 tf_warning_or_error);
29271 else
29272 decl = NULL_TREE;
29273
29274 if (!decl)
29275 /* Look it up in the enclosing context. DR 141: When looking for a
29276 template-name after -> or ., only consider class templates. */
29277 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
29278 /* DR 141: When looking in the
29279 current enclosing context for a
29280 template-name after -> or ., only
29281 consider class templates. */
29282 : is_template ? LOOK_want::TYPE
29283 : prefer_type_arg (tag_type));
29284 parser->object_scope = object_type;
29285 parser->qualifying_scope = NULL_TREE;
29286 }
29287 else
29288 {
29289 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
29290 : prefer_type_arg (tag_type));
29291 parser->qualifying_scope = NULL_TREE;
29292 parser->object_scope = NULL_TREE;
29293 }
29294
29295 /* If the lookup failed, let our caller know. */
29296 if (!decl || decl == error_mark_node)
29297 return error_mark_node;
29298
29299 /* Pull out the template from an injected-class-name (or multiple). */
29300 if (is_template)
29301 decl = maybe_get_template_decl_from_type_decl (decl);
29302
29303 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
29304 if (TREE_CODE (decl) == TREE_LIST)
29305 {
29306 if (ambiguous_decls)
29307 *ambiguous_decls = decl;
29308 /* The error message we have to print is too complicated for
29309 cp_parser_error, so we incorporate its actions directly. */
29310 if (!cp_parser_simulate_error (parser))
29311 {
29312 error_at (name_location, "reference to %qD is ambiguous",
29313 name);
29314 print_candidates (decl);
29315 }
29316 return error_mark_node;
29317 }
29318
29319 gcc_assert (DECL_P (decl)
29320 || TREE_CODE (decl) == OVERLOAD
29321 || TREE_CODE (decl) == SCOPE_REF
29322 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
29323 || BASELINK_P (decl));
29324
29325 /* If we have resolved the name of a member declaration, check to
29326 see if the declaration is accessible. When the name resolves to
29327 set of overloaded functions, accessibility is checked when
29328 overload resolution is done.
29329
29330 During an explicit instantiation, access is not checked at all,
29331 as per [temp.explicit]. */
29332 if (DECL_P (decl))
29333 check_accessibility_of_qualified_id (decl, object_type, parser->scope,
29334 tf_warning_or_error);
29335
29336 maybe_record_typedef_use (decl);
29337
29338 return cp_expr (decl, name_location);
29339 }
29340
29341 /* Like cp_parser_lookup_name, but for use in the typical case where
29342 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
29343 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
29344
29345 static tree
29346 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
29347 {
29348 return cp_parser_lookup_name (parser, name,
29349 none_type,
29350 /*is_template=*/false,
29351 /*is_namespace=*/false,
29352 /*check_dependency=*/true,
29353 /*ambiguous_decls=*/NULL,
29354 location);
29355 }
29356
29357 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
29358 the current context, return the TYPE_DECL. If TAG_NAME_P is
29359 true, the DECL indicates the class being defined in a class-head,
29360 or declared in an elaborated-type-specifier.
29361
29362 Otherwise, return DECL. */
29363
29364 static tree
29365 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
29366 {
29367 /* If the TEMPLATE_DECL is being declared as part of a class-head,
29368 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
29369
29370 struct A {
29371 template <typename T> struct B;
29372 };
29373
29374 template <typename T> struct A::B {};
29375
29376 Similarly, in an elaborated-type-specifier:
29377
29378 namespace N { struct X{}; }
29379
29380 struct A {
29381 template <typename T> friend struct N::X;
29382 };
29383
29384 However, if the DECL refers to a class type, and we are in
29385 the scope of the class, then the name lookup automatically
29386 finds the TYPE_DECL created by build_self_reference rather
29387 than a TEMPLATE_DECL. For example, in:
29388
29389 template <class T> struct S {
29390 S s;
29391 };
29392
29393 there is no need to handle such case. */
29394
29395 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
29396 return DECL_TEMPLATE_RESULT (decl);
29397
29398 return decl;
29399 }
29400
29401 /* If too many, or too few, template-parameter lists apply to the
29402 declarator, issue an error message. Returns TRUE if all went well,
29403 and FALSE otherwise. */
29404
29405 static bool
29406 cp_parser_check_declarator_template_parameters (cp_parser* parser,
29407 cp_declarator *declarator,
29408 location_t declarator_location)
29409 {
29410 switch (declarator->kind)
29411 {
29412 case cdk_id:
29413 {
29414 unsigned num_templates = 0;
29415 tree scope = declarator->u.id.qualifying_scope;
29416 bool template_id_p = false;
29417
29418 if (scope)
29419 num_templates = num_template_headers_for_class (scope);
29420 else if (TREE_CODE (declarator->u.id.unqualified_name)
29421 == TEMPLATE_ID_EXPR)
29422 {
29423 /* If the DECLARATOR has the form `X<y>' then it uses one
29424 additional level of template parameters. */
29425 ++num_templates;
29426 template_id_p = true;
29427 }
29428
29429 return cp_parser_check_template_parameters
29430 (parser, num_templates, template_id_p, declarator_location,
29431 declarator);
29432 }
29433
29434 case cdk_function:
29435 case cdk_array:
29436 case cdk_pointer:
29437 case cdk_reference:
29438 case cdk_ptrmem:
29439 return (cp_parser_check_declarator_template_parameters
29440 (parser, declarator->declarator, declarator_location));
29441
29442 case cdk_decomp:
29443 case cdk_error:
29444 return true;
29445
29446 default:
29447 gcc_unreachable ();
29448 }
29449 return false;
29450 }
29451
29452 /* NUM_TEMPLATES were used in the current declaration. If that is
29453 invalid, return FALSE and issue an error messages. Otherwise,
29454 return TRUE. If DECLARATOR is non-NULL, then we are checking a
29455 declarator and we can print more accurate diagnostics. */
29456
29457 static bool
29458 cp_parser_check_template_parameters (cp_parser* parser,
29459 unsigned num_templates,
29460 bool template_id_p,
29461 location_t location,
29462 cp_declarator *declarator)
29463 {
29464 /* If there are the same number of template classes and parameter
29465 lists, that's OK. */
29466 if (parser->num_template_parameter_lists == num_templates)
29467 return true;
29468 /* If there are more, but only one more, and the name ends in an identifier,
29469 then we are declaring a primary template. That's OK too. */
29470 if (!template_id_p
29471 && parser->num_template_parameter_lists == num_templates + 1)
29472 return true;
29473
29474 if (cp_parser_simulate_error (parser))
29475 return false;
29476
29477 /* If there are more template classes than parameter lists, we have
29478 something like:
29479
29480 template <class T> void S<T>::R<T>::f (); */
29481 if (parser->num_template_parameter_lists < num_templates)
29482 {
29483 if (declarator && !current_function_decl)
29484 error_at (location, "specializing member %<%T::%E%> "
29485 "requires %<template<>%> syntax",
29486 declarator->u.id.qualifying_scope,
29487 declarator->u.id.unqualified_name);
29488 else if (declarator)
29489 error_at (location, "invalid declaration of %<%T::%E%>",
29490 declarator->u.id.qualifying_scope,
29491 declarator->u.id.unqualified_name);
29492 else
29493 error_at (location, "too few template-parameter-lists");
29494 return false;
29495 }
29496 /* Otherwise, there are too many template parameter lists. We have
29497 something like:
29498
29499 template <class T> template <class U> void S::f(); */
29500 error_at (location, "too many template-parameter-lists");
29501 return false;
29502 }
29503
29504 /* Parse an optional `::' token indicating that the following name is
29505 from the global namespace. If so, PARSER->SCOPE is set to the
29506 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
29507 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
29508 Returns the new value of PARSER->SCOPE, if the `::' token is
29509 present, and NULL_TREE otherwise. */
29510
29511 static tree
29512 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
29513 {
29514 cp_token *token;
29515
29516 /* Peek at the next token. */
29517 token = cp_lexer_peek_token (parser->lexer);
29518 /* If we're looking at a `::' token then we're starting from the
29519 global namespace, not our current location. */
29520 if (token->type == CPP_SCOPE)
29521 {
29522 /* Consume the `::' token. */
29523 cp_lexer_consume_token (parser->lexer);
29524 /* Set the SCOPE so that we know where to start the lookup. */
29525 parser->scope = global_namespace;
29526 parser->qualifying_scope = global_namespace;
29527 parser->object_scope = NULL_TREE;
29528
29529 return parser->scope;
29530 }
29531 else if (!current_scope_valid_p)
29532 {
29533 parser->scope = NULL_TREE;
29534 parser->qualifying_scope = NULL_TREE;
29535 parser->object_scope = NULL_TREE;
29536 }
29537
29538 return NULL_TREE;
29539 }
29540
29541 /* Returns TRUE if the upcoming token sequence is the start of a
29542 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
29543 declarator is preceded by the `friend' specifier. The parser flags FLAGS
29544 is used to control type-specifier parsing. */
29545
29546 static bool
29547 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
29548 bool friend_p)
29549 {
29550 bool constructor_p;
29551 bool outside_class_specifier_p;
29552 tree nested_name_specifier;
29553 cp_token *next_token;
29554
29555 /* The common case is that this is not a constructor declarator, so
29556 try to avoid doing lots of work if at all possible. It's not
29557 valid declare a constructor at function scope. */
29558 if (parser->in_function_body)
29559 return false;
29560 /* And only certain tokens can begin a constructor declarator. */
29561 next_token = cp_lexer_peek_token (parser->lexer);
29562 if (next_token->type != CPP_NAME
29563 && next_token->type != CPP_SCOPE
29564 && next_token->type != CPP_NESTED_NAME_SPECIFIER
29565 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
29566 declarator-id of a constructor or destructor. */
29567 && (next_token->type != CPP_TEMPLATE_ID || cxx_dialect >= cxx20))
29568 return false;
29569
29570 /* Parse tentatively; we are going to roll back all of the tokens
29571 consumed here. */
29572 cp_parser_parse_tentatively (parser);
29573 /* Assume that we are looking at a constructor declarator. */
29574 constructor_p = true;
29575
29576 /* Look for the optional `::' operator. */
29577 cp_parser_global_scope_opt (parser,
29578 /*current_scope_valid_p=*/false);
29579 /* Look for the nested-name-specifier. */
29580 nested_name_specifier
29581 = (cp_parser_nested_name_specifier_opt (parser,
29582 /*typename_keyword_p=*/false,
29583 /*check_dependency_p=*/false,
29584 /*type_p=*/false,
29585 /*is_declaration=*/false));
29586
29587 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
29588 if (nested_name_specifier
29589 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
29590 {
29591 tree s = resolve_typename_type (nested_name_specifier,
29592 /*only_current_p=*/false);
29593 if (TREE_CODE (s) != TYPENAME_TYPE)
29594 nested_name_specifier = s;
29595 }
29596
29597 outside_class_specifier_p = (!at_class_scope_p ()
29598 || !TYPE_BEING_DEFINED (current_class_type)
29599 || friend_p);
29600
29601 /* Outside of a class-specifier, there must be a
29602 nested-name-specifier. Except in C++17 mode, where we
29603 might be declaring a guiding declaration. */
29604 if (!nested_name_specifier && outside_class_specifier_p
29605 && cxx_dialect < cxx17)
29606 constructor_p = false;
29607 else if (nested_name_specifier == error_mark_node)
29608 constructor_p = false;
29609
29610 /* If we have a class scope, this is easy; DR 147 says that S::S always
29611 names the constructor, and no other qualified name could. */
29612 if (constructor_p && nested_name_specifier
29613 && CLASS_TYPE_P (nested_name_specifier))
29614 {
29615 tree id = cp_parser_unqualified_id (parser,
29616 /*template_keyword_p=*/false,
29617 /*check_dependency_p=*/false,
29618 /*declarator_p=*/true,
29619 /*optional_p=*/false);
29620 if (is_overloaded_fn (id))
29621 id = DECL_NAME (get_first_fn (id));
29622 if (!constructor_name_p (id, nested_name_specifier))
29623 constructor_p = false;
29624 }
29625 /* If we still think that this might be a constructor-declarator,
29626 look for a class-name. */
29627 else if (constructor_p)
29628 {
29629 /* If we have:
29630
29631 template <typename T> struct S {
29632 S();
29633 };
29634
29635 we must recognize that the nested `S' names a class. */
29636 if (cxx_dialect >= cxx17)
29637 cp_parser_parse_tentatively (parser);
29638
29639 tree type_decl;
29640 type_decl = cp_parser_class_name (parser,
29641 /*typename_keyword_p=*/false,
29642 /*template_keyword_p=*/false,
29643 none_type,
29644 /*check_dependency_p=*/false,
29645 /*class_head_p=*/false,
29646 /*is_declaration=*/false);
29647
29648 if (cxx_dialect >= cxx17
29649 && !cp_parser_parse_definitely (parser))
29650 {
29651 type_decl = NULL_TREE;
29652 tree tmpl = cp_parser_template_name (parser,
29653 /*template_keyword*/false,
29654 /*check_dependency_p*/false,
29655 /*is_declaration*/false,
29656 none_type,
29657 /*is_identifier*/NULL);
29658 if (DECL_CLASS_TEMPLATE_P (tmpl)
29659 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
29660 /* It's a deduction guide, return true. */;
29661 else
29662 cp_parser_simulate_error (parser);
29663 }
29664
29665 /* If there was no class-name, then this is not a constructor.
29666 Otherwise, if we are in a class-specifier and we aren't
29667 handling a friend declaration, check that its type matches
29668 current_class_type (c++/38313). Note: error_mark_node
29669 is left alone for error recovery purposes. */
29670 constructor_p = (!cp_parser_error_occurred (parser)
29671 && (outside_class_specifier_p
29672 || type_decl == NULL_TREE
29673 || type_decl == error_mark_node
29674 || same_type_p (current_class_type,
29675 TREE_TYPE (type_decl))));
29676
29677 /* If we're still considering a constructor, we have to see a `(',
29678 to begin the parameter-declaration-clause, followed by either a
29679 `)', an `...', or a decl-specifier. We need to check for a
29680 type-specifier to avoid being fooled into thinking that:
29681
29682 S (f) (int);
29683
29684 is a constructor. (It is actually a function named `f' that
29685 takes one parameter (of type `int') and returns a value of type
29686 `S'. */
29687 if (constructor_p
29688 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29689 constructor_p = false;
29690
29691 if (constructor_p
29692 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
29693 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
29694 /* A parameter declaration begins with a decl-specifier,
29695 which is either the "attribute" keyword, a storage class
29696 specifier, or (usually) a type-specifier. */
29697 && (!cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
29698 /* GNU attributes can actually appear both at the start of
29699 a parameter and parenthesized declarator.
29700 S (__attribute__((unused)) int);
29701 is a constructor, but
29702 S (__attribute__((unused)) foo) (int);
29703 is a function declaration. */
29704 || (cp_parser_allow_gnu_extensions_p (parser)
29705 && cp_next_tokens_can_be_gnu_attribute_p (parser)))
29706 /* A parameter declaration can also begin with [[attribute]]. */
29707 && !cp_next_tokens_can_be_std_attribute_p (parser))
29708 {
29709 tree type;
29710 tree pushed_scope = NULL_TREE;
29711 unsigned saved_num_template_parameter_lists;
29712
29713 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
29714 {
29715 unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
29716 while (--n)
29717 cp_lexer_consume_token (parser->lexer);
29718 }
29719
29720 /* Names appearing in the type-specifier should be looked up
29721 in the scope of the class. */
29722 if (current_class_type)
29723 type = NULL_TREE;
29724 else if (type_decl)
29725 {
29726 type = TREE_TYPE (type_decl);
29727 if (TREE_CODE (type) == TYPENAME_TYPE)
29728 {
29729 type = resolve_typename_type (type,
29730 /*only_current_p=*/false);
29731 if (TREE_CODE (type) == TYPENAME_TYPE)
29732 {
29733 cp_parser_abort_tentative_parse (parser);
29734 return false;
29735 }
29736 }
29737 pushed_scope = push_scope (type);
29738 }
29739
29740 /* Inside the constructor parameter list, surrounding
29741 template-parameter-lists do not apply. */
29742 saved_num_template_parameter_lists
29743 = parser->num_template_parameter_lists;
29744 parser->num_template_parameter_lists = 0;
29745
29746 /* Look for the type-specifier. It's not optional, but its typename
29747 might be. Unless this is a friend declaration; we don't want to
29748 treat
29749
29750 friend S (T::fn)(int);
29751
29752 as a constructor, but with P0634, we might assume a type when
29753 looking for the type-specifier. It is actually a function named
29754 `T::fn' that takes one parameter (of type `int') and returns a
29755 value of type `S'. Constructors can be friends, but they must
29756 use a qualified name.
29757
29758 Parse with an empty set of declaration specifiers since we're
29759 trying to match a decl-specifier-seq of the first parameter.
29760 This must be non-null so that cp_parser_simple_type_specifier
29761 will recognize a constrained placeholder type such as:
29762 'C<int> auto' where C is a type concept. */
29763 cp_decl_specifier_seq ctor_specs;
29764 clear_decl_specs (&ctor_specs);
29765 cp_parser_type_specifier (parser,
29766 (friend_p ? CP_PARSER_FLAGS_NONE
29767 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
29768 /*decl_specs=*/&ctor_specs,
29769 /*is_declarator=*/true,
29770 /*declares_class_or_enum=*/NULL,
29771 /*is_cv_qualifier=*/NULL);
29772
29773 parser->num_template_parameter_lists
29774 = saved_num_template_parameter_lists;
29775
29776 /* Leave the scope of the class. */
29777 if (pushed_scope)
29778 pop_scope (pushed_scope);
29779
29780 constructor_p = !cp_parser_error_occurred (parser);
29781 }
29782 }
29783
29784 /* We did not really want to consume any tokens. */
29785 cp_parser_abort_tentative_parse (parser);
29786
29787 return constructor_p;
29788 }
29789
29790 /* Parse the definition of the function given by the DECL_SPECIFIERS,
29791 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
29792 they must be performed once we are in the scope of the function.
29793
29794 Returns the function defined. */
29795
29796 static tree
29797 cp_parser_function_definition_from_specifiers_and_declarator
29798 (cp_parser* parser,
29799 cp_decl_specifier_seq *decl_specifiers,
29800 tree attributes,
29801 const cp_declarator *declarator)
29802 {
29803 tree fn;
29804 bool success_p;
29805
29806 /* Begin the function-definition. */
29807 success_p = start_function (decl_specifiers, declarator, attributes);
29808
29809 /* The things we're about to see are not directly qualified by any
29810 template headers we've seen thus far. */
29811 reset_specialization ();
29812
29813 /* If there were names looked up in the decl-specifier-seq that we
29814 did not check, check them now. We must wait until we are in the
29815 scope of the function to perform the checks, since the function
29816 might be a friend. */
29817 perform_deferred_access_checks (tf_warning_or_error);
29818
29819 if (success_p)
29820 {
29821 cp_finalize_omp_declare_simd (parser, current_function_decl);
29822 parser->omp_declare_simd = NULL;
29823 cp_finalize_oacc_routine (parser, current_function_decl, true);
29824 parser->oacc_routine = NULL;
29825 }
29826
29827 if (!success_p)
29828 {
29829 /* Skip the entire function. */
29830 cp_parser_skip_to_end_of_block_or_statement (parser);
29831 fn = error_mark_node;
29832 }
29833 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
29834 {
29835 /* Seen already, skip it. An error message has already been output. */
29836 cp_parser_skip_to_end_of_block_or_statement (parser);
29837 fn = current_function_decl;
29838 current_function_decl = NULL_TREE;
29839 /* If this is a function from a class, pop the nested class. */
29840 if (current_class_name)
29841 pop_nested_class ();
29842 }
29843 else
29844 {
29845 timevar_id_t tv;
29846 if (DECL_DECLARED_INLINE_P (current_function_decl))
29847 tv = TV_PARSE_INLINE;
29848 else
29849 tv = TV_PARSE_FUNC;
29850 timevar_push (tv);
29851 fn = cp_parser_function_definition_after_declarator (parser,
29852 /*inline_p=*/false);
29853 timevar_pop (tv);
29854 }
29855
29856 return fn;
29857 }
29858
29859 /* Parse the part of a function-definition that follows the
29860 declarator. INLINE_P is TRUE iff this function is an inline
29861 function defined within a class-specifier.
29862
29863 Returns the function defined. */
29864
29865 static tree
29866 cp_parser_function_definition_after_declarator (cp_parser* parser,
29867 bool inline_p)
29868 {
29869 tree fn;
29870 bool saved_in_unbraced_linkage_specification_p;
29871 bool saved_in_function_body;
29872 unsigned saved_num_template_parameter_lists;
29873 cp_token *token;
29874 bool fully_implicit_function_template_p
29875 = parser->fully_implicit_function_template_p;
29876 parser->fully_implicit_function_template_p = false;
29877 tree implicit_template_parms
29878 = parser->implicit_template_parms;
29879 parser->implicit_template_parms = 0;
29880 cp_binding_level* implicit_template_scope
29881 = parser->implicit_template_scope;
29882 parser->implicit_template_scope = 0;
29883
29884 saved_in_function_body = parser->in_function_body;
29885 parser->in_function_body = true;
29886 /* If the next token is `return', then the code may be trying to
29887 make use of the "named return value" extension that G++ used to
29888 support. */
29889 token = cp_lexer_peek_token (parser->lexer);
29890 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
29891 {
29892 /* Consume the `return' keyword. */
29893 cp_lexer_consume_token (parser->lexer);
29894 /* Look for the identifier that indicates what value is to be
29895 returned. */
29896 cp_parser_identifier (parser);
29897 /* Issue an error message. */
29898 error_at (token->location,
29899 "named return values are no longer supported");
29900 /* Skip tokens until we reach the start of the function body. */
29901 while (true)
29902 {
29903 cp_token *token = cp_lexer_peek_token (parser->lexer);
29904 if (token->type == CPP_OPEN_BRACE
29905 || token->type == CPP_EOF
29906 || token->type == CPP_PRAGMA_EOL)
29907 break;
29908 cp_lexer_consume_token (parser->lexer);
29909 }
29910 }
29911 /* The `extern' in `extern "C" void f () { ... }' does not apply to
29912 anything declared inside `f'. */
29913 saved_in_unbraced_linkage_specification_p
29914 = parser->in_unbraced_linkage_specification_p;
29915 parser->in_unbraced_linkage_specification_p = false;
29916 /* Inside the function, surrounding template-parameter-lists do not
29917 apply. */
29918 saved_num_template_parameter_lists
29919 = parser->num_template_parameter_lists;
29920 parser->num_template_parameter_lists = 0;
29921
29922 /* If the next token is `try', `__transaction_atomic', or
29923 `__transaction_relaxed`, then we are looking at either function-try-block
29924 or function-transaction-block. Note that all of these include the
29925 function-body. */
29926 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
29927 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
29928 else if (cp_lexer_next_token_is_keyword (parser->lexer,
29929 RID_TRANSACTION_RELAXED))
29930 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
29931 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
29932 cp_parser_function_try_block (parser);
29933 else
29934 cp_parser_ctor_initializer_opt_and_function_body
29935 (parser, /*in_function_try_block=*/false);
29936
29937 /* Finish the function. */
29938 fn = finish_function (inline_p);
29939
29940 if (modules_p ()
29941 && !inline_p
29942 && TYPE_P (DECL_CONTEXT (fn))
29943 && (DECL_DECLARED_INLINE_P (fn)
29944 || processing_template_decl))
29945 set_defining_module (fn);
29946
29947 /* Generate code for it, if necessary. */
29948 expand_or_defer_fn (fn);
29949 /* Restore the saved values. */
29950 parser->in_unbraced_linkage_specification_p
29951 = saved_in_unbraced_linkage_specification_p;
29952 parser->num_template_parameter_lists
29953 = saved_num_template_parameter_lists;
29954 parser->in_function_body = saved_in_function_body;
29955
29956 parser->fully_implicit_function_template_p
29957 = fully_implicit_function_template_p;
29958 parser->implicit_template_parms
29959 = implicit_template_parms;
29960 parser->implicit_template_scope
29961 = implicit_template_scope;
29962
29963 if (parser->fully_implicit_function_template_p)
29964 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
29965
29966 return fn;
29967 }
29968
29969 /* Parse a template-declaration body (following argument list). */
29970
29971 static void
29972 cp_parser_template_declaration_after_parameters (cp_parser* parser,
29973 tree parameter_list,
29974 bool member_p)
29975 {
29976 tree decl = NULL_TREE;
29977 bool friend_p = false;
29978
29979 /* We just processed one more parameter list. */
29980 ++parser->num_template_parameter_lists;
29981
29982 /* Get the deferred access checks from the parameter list. These
29983 will be checked once we know what is being declared, as for a
29984 member template the checks must be performed in the scope of the
29985 class containing the member. */
29986 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
29987
29988 /* Tentatively parse for a new template parameter list, which can either be
29989 the template keyword or a template introduction. */
29990 if (cp_parser_template_declaration_after_export (parser, member_p))
29991 /* OK */;
29992 else if (cxx_dialect >= cxx11
29993 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
29994 decl = cp_parser_alias_declaration (parser);
29995 else if (cxx_dialect >= cxx20 /* Implies flag_concept. */
29996 && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
29997 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_BOOL))
29998 /* Allow 'concept bool' to be handled as per the TS. */
29999 decl = cp_parser_concept_definition (parser);
30000 else
30001 {
30002 cp_token *token = cp_lexer_peek_token (parser->lexer);
30003 decl = cp_parser_single_declaration (parser,
30004 checks,
30005 member_p,
30006 /*explicit_specialization_p=*/false,
30007 &friend_p);
30008
30009 /* If this is a member template declaration, let the front
30010 end know. */
30011 if (member_p && !friend_p && decl)
30012 {
30013 if (TREE_CODE (decl) == TYPE_DECL)
30014 cp_parser_check_access_in_redeclaration (decl, token->location);
30015
30016 decl = finish_member_template_decl (decl);
30017 }
30018 else if (friend_p && decl
30019 && DECL_DECLARES_TYPE_P (decl))
30020 make_friend_class (current_class_type, TREE_TYPE (decl),
30021 /*complain=*/true);
30022 }
30023 /* We are done with the current parameter list. */
30024 --parser->num_template_parameter_lists;
30025
30026 pop_deferring_access_checks ();
30027
30028 /* Finish up. */
30029 finish_template_decl (parameter_list);
30030
30031 /* Check the template arguments for a literal operator template. */
30032 if (decl
30033 && DECL_DECLARES_FUNCTION_P (decl)
30034 && UDLIT_OPER_P (DECL_NAME (decl)))
30035 {
30036 bool ok = true;
30037 if (parameter_list == NULL_TREE)
30038 ok = false;
30039 else
30040 {
30041 int num_parms = TREE_VEC_LENGTH (parameter_list);
30042 if (num_parms == 1)
30043 {
30044 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
30045 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
30046 if (TREE_CODE (parm) != PARM_DECL)
30047 ok = false;
30048 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
30049 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
30050 /* OK, C++20 string literal operator template. We don't need
30051 to warn in lower dialects here because we will have already
30052 warned about the template parameter. */;
30053 else if (TREE_TYPE (parm) != char_type_node
30054 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
30055 ok = false;
30056 }
30057 else if (num_parms == 2 && cxx_dialect >= cxx14)
30058 {
30059 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
30060 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
30061 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
30062 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
30063 if (TREE_CODE (parm) != PARM_DECL
30064 || TREE_TYPE (parm) != TREE_TYPE (type)
30065 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
30066 ok = false;
30067 else
30068 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
30069 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
30070 "ISO C++ did not adopt string literal operator templa"
30071 "tes taking an argument pack of characters");
30072 }
30073 else
30074 ok = false;
30075 }
30076 if (!ok)
30077 {
30078 if (cxx_dialect > cxx17)
30079 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
30080 "template %qD has invalid parameter list; expected "
30081 "non-type template parameter pack %<<char...>%> or "
30082 "single non-type parameter of class type",
30083 decl);
30084 else
30085 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
30086 "template %qD has invalid parameter list; expected "
30087 "non-type template parameter pack %<<char...>%>",
30088 decl);
30089 }
30090 }
30091
30092 /* Register member declarations. */
30093 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
30094 finish_member_declaration (decl);
30095 /* If DECL is a function template, we must return to parse it later.
30096 (Even though there is no definition, there might be default
30097 arguments that need handling.) */
30098 if (member_p && decl
30099 && DECL_DECLARES_FUNCTION_P (decl))
30100 vec_safe_push (unparsed_funs_with_definitions, decl);
30101 }
30102
30103 /* Parse a template introduction header for a template-declaration. Returns
30104 false if tentative parse fails. */
30105
30106 static bool
30107 cp_parser_template_introduction (cp_parser* parser, bool member_p)
30108 {
30109 cp_parser_parse_tentatively (parser);
30110
30111 tree saved_scope = parser->scope;
30112 tree saved_object_scope = parser->object_scope;
30113 tree saved_qualifying_scope = parser->qualifying_scope;
30114
30115 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
30116
30117 /* Look for the optional `::' operator. */
30118 cp_parser_global_scope_opt (parser,
30119 /*current_scope_valid_p=*/false);
30120 /* Look for the nested-name-specifier. */
30121 cp_parser_nested_name_specifier_opt (parser,
30122 /*typename_keyword_p=*/false,
30123 /*check_dependency_p=*/true,
30124 /*type_p=*/false,
30125 /*is_declaration=*/false);
30126
30127 cp_token *token = cp_lexer_peek_token (parser->lexer);
30128 tree concept_name = cp_parser_identifier (parser);
30129
30130 /* Look up the concept for which we will be matching
30131 template parameters. */
30132 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
30133 token->location);
30134 parser->scope = saved_scope;
30135 parser->object_scope = saved_object_scope;
30136 parser->qualifying_scope = saved_qualifying_scope;
30137
30138 if (concept_name == error_mark_node
30139 || (seen_error () && !concept_definition_p (tmpl_decl)))
30140 cp_parser_simulate_error (parser);
30141
30142 /* Look for opening brace for introduction. */
30143 matching_braces braces;
30144 braces.require_open (parser);
30145 location_t open_loc = input_location;
30146
30147 if (!cp_parser_parse_definitely (parser))
30148 return false;
30149
30150 push_deferring_access_checks (dk_deferred);
30151
30152 /* Build vector of placeholder parameters and grab
30153 matching identifiers. */
30154 tree introduction_list = cp_parser_introduction_list (parser);
30155
30156 /* Look for closing brace for introduction. */
30157 if (!braces.require_close (parser))
30158 return true;
30159
30160 /* The introduction-list shall not be empty. */
30161 int nargs = TREE_VEC_LENGTH (introduction_list);
30162 if (nargs == 0)
30163 {
30164 /* In cp_parser_introduction_list we have already issued an error. */
30165 return true;
30166 }
30167
30168 if (tmpl_decl == error_mark_node)
30169 {
30170 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
30171 token->location);
30172 return true;
30173 }
30174
30175 /* Build and associate the constraint. */
30176 location_t introduction_loc = make_location (open_loc,
30177 start_token->location,
30178 parser->lexer);
30179 tree parms = finish_template_introduction (tmpl_decl,
30180 introduction_list,
30181 introduction_loc);
30182 if (parms && parms != error_mark_node)
30183 {
30184 if (!flag_concepts_ts)
30185 pedwarn (introduction_loc, 0, "template-introductions"
30186 " are not part of C++20 concepts; use %qs to enable",
30187 "-fconcepts-ts");
30188
30189 cp_parser_template_declaration_after_parameters (parser, parms,
30190 member_p);
30191 return true;
30192 }
30193
30194 if (parms == NULL_TREE)
30195 error_at (token->location, "no matching concept for template-introduction");
30196
30197 return true;
30198 }
30199
30200 /* Parse a normal template-declaration following the template keyword. */
30201
30202 static void
30203 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
30204 {
30205 tree parameter_list;
30206 bool need_lang_pop;
30207 location_t location = input_location;
30208
30209 /* Look for the `<' token. */
30210 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
30211 return;
30212 if (at_class_scope_p () && current_function_decl)
30213 {
30214 /* 14.5.2.2 [temp.mem]
30215
30216 A local class shall not have member templates. */
30217 error_at (location,
30218 "invalid declaration of member template in local class");
30219 cp_parser_skip_to_end_of_block_or_statement (parser);
30220 return;
30221 }
30222 /* [temp]
30223
30224 A template ... shall not have C linkage. */
30225 if (current_lang_name == lang_name_c)
30226 {
30227 error_at (location, "template with C linkage");
30228 maybe_show_extern_c_location ();
30229 /* Give it C++ linkage to avoid confusing other parts of the
30230 front end. */
30231 push_lang_context (lang_name_cplusplus);
30232 need_lang_pop = true;
30233 }
30234 else
30235 need_lang_pop = false;
30236
30237 /* We cannot perform access checks on the template parameter
30238 declarations until we know what is being declared, just as we
30239 cannot check the decl-specifier list. */
30240 push_deferring_access_checks (dk_deferred);
30241
30242 /* If the next token is `>', then we have an invalid
30243 specialization. Rather than complain about an invalid template
30244 parameter, issue an error message here. */
30245 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
30246 {
30247 cp_parser_error (parser, "invalid explicit specialization");
30248 begin_specialization ();
30249 parameter_list = NULL_TREE;
30250 }
30251 else
30252 {
30253 /* Parse the template parameters. */
30254 parameter_list = cp_parser_template_parameter_list (parser);
30255 }
30256
30257 /* Look for the `>'. */
30258 cp_parser_skip_to_end_of_template_parameter_list (parser);
30259
30260 /* Manage template requirements */
30261 if (flag_concepts)
30262 {
30263 tree reqs = get_shorthand_constraints (current_template_parms);
30264 if (tree treqs = cp_parser_requires_clause_opt (parser, false))
30265 reqs = combine_constraint_expressions (reqs, treqs);
30266 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
30267 }
30268
30269 cp_parser_template_declaration_after_parameters (parser, parameter_list,
30270 member_p);
30271
30272 /* For the erroneous case of a template with C linkage, we pushed an
30273 implicit C++ linkage scope; exit that scope now. */
30274 if (need_lang_pop)
30275 pop_lang_context ();
30276 }
30277
30278 /* Parse a template-declaration, assuming that the `export' (and
30279 `extern') keywords, if present, has already been scanned. MEMBER_P
30280 is as for cp_parser_template_declaration. */
30281
30282 static bool
30283 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
30284 {
30285 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
30286 {
30287 cp_lexer_consume_token (parser->lexer);
30288 cp_parser_explicit_template_declaration (parser, member_p);
30289 return true;
30290 }
30291 else if (flag_concepts)
30292 return cp_parser_template_introduction (parser, member_p);
30293
30294 return false;
30295 }
30296
30297 /* Perform the deferred access checks from a template-parameter-list.
30298 CHECKS is a TREE_LIST of access checks, as returned by
30299 get_deferred_access_checks. */
30300
30301 static void
30302 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
30303 {
30304 ++processing_template_parmlist;
30305 perform_access_checks (checks, tf_warning_or_error);
30306 --processing_template_parmlist;
30307 }
30308
30309 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
30310 `function-definition' sequence that follows a template header.
30311 If MEMBER_P is true, this declaration appears in a class scope.
30312
30313 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
30314 *FRIEND_P is set to TRUE iff the declaration is a friend. */
30315
30316 static tree
30317 cp_parser_single_declaration (cp_parser* parser,
30318 vec<deferred_access_check, va_gc> *checks,
30319 bool member_p,
30320 bool explicit_specialization_p,
30321 bool* friend_p)
30322 {
30323 int declares_class_or_enum;
30324 tree decl = NULL_TREE;
30325 cp_decl_specifier_seq decl_specifiers;
30326 bool function_definition_p = false;
30327 cp_token *decl_spec_token_start;
30328
30329 /* This function is only used when processing a template
30330 declaration. */
30331 gcc_assert (innermost_scope_kind () == sk_template_parms
30332 || innermost_scope_kind () == sk_template_spec);
30333
30334 /* Defer access checks until we know what is being declared. */
30335 push_deferring_access_checks (dk_deferred);
30336
30337 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
30338 alternative. */
30339 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
30340 cp_parser_decl_specifier_seq (parser,
30341 (CP_PARSER_FLAGS_OPTIONAL
30342 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
30343 &decl_specifiers,
30344 &declares_class_or_enum);
30345 if (friend_p)
30346 *friend_p = cp_parser_friend_p (&decl_specifiers);
30347
30348 /* There are no template typedefs. */
30349 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
30350 {
30351 error_at (decl_spec_token_start->location,
30352 "template declaration of %<typedef%>");
30353 decl = error_mark_node;
30354 }
30355
30356 /* Gather up the access checks that occurred the
30357 decl-specifier-seq. */
30358 stop_deferring_access_checks ();
30359
30360 /* Check for the declaration of a template class. */
30361 if (declares_class_or_enum)
30362 {
30363 if (cp_parser_declares_only_class_p (parser)
30364 || (declares_class_or_enum & 2))
30365 {
30366 /* If this is a declaration, but not a definition, associate
30367 any constraints with the type declaration. Constraints
30368 are associated with definitions in cp_parser_class_specifier. */
30369 if (declares_class_or_enum == 1)
30370 associate_classtype_constraints (decl_specifiers.type);
30371
30372 decl = shadow_tag (&decl_specifiers);
30373
30374 /* In this case:
30375
30376 struct C {
30377 friend template <typename T> struct A<T>::B;
30378 };
30379
30380 A<T>::B will be represented by a TYPENAME_TYPE, and
30381 therefore not recognized by shadow_tag. */
30382 if (friend_p && *friend_p
30383 && !decl
30384 && decl_specifiers.type
30385 && TYPE_P (decl_specifiers.type))
30386 decl = decl_specifiers.type;
30387
30388 if (decl && decl != error_mark_node)
30389 decl = TYPE_NAME (decl);
30390 else
30391 decl = error_mark_node;
30392
30393 /* Perform access checks for template parameters. */
30394 cp_parser_perform_template_parameter_access_checks (checks);
30395
30396 /* Give a helpful diagnostic for
30397 template <class T> struct A { } a;
30398 if we aren't already recovering from an error. */
30399 if (!cp_parser_declares_only_class_p (parser)
30400 && !seen_error ())
30401 {
30402 error_at (cp_lexer_peek_token (parser->lexer)->location,
30403 "a class template declaration must not declare "
30404 "anything else");
30405 cp_parser_skip_to_end_of_block_or_statement (parser);
30406 goto out;
30407 }
30408 }
30409 }
30410
30411 /* Complain about missing 'typename' or other invalid type names. */
30412 if (!decl_specifiers.any_type_specifiers_p
30413 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
30414 {
30415 /* cp_parser_parse_and_diagnose_invalid_type_name calls
30416 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
30417 the rest of this declaration. */
30418 decl = error_mark_node;
30419 goto out;
30420 }
30421
30422 /* If it's not a template class, try for a template function. If
30423 the next token is a `;', then this declaration does not declare
30424 anything. But, if there were errors in the decl-specifiers, then
30425 the error might well have come from an attempted class-specifier.
30426 In that case, there's no need to warn about a missing declarator. */
30427 if (!decl
30428 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
30429 || decl_specifiers.type != error_mark_node))
30430 {
30431 decl = cp_parser_init_declarator (parser,
30432 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
30433 &decl_specifiers,
30434 checks,
30435 /*function_definition_allowed_p=*/true,
30436 member_p,
30437 declares_class_or_enum,
30438 &function_definition_p,
30439 NULL, NULL, NULL);
30440
30441 /* 7.1.1-1 [dcl.stc]
30442
30443 A storage-class-specifier shall not be specified in an explicit
30444 specialization... */
30445 if (decl
30446 && explicit_specialization_p
30447 && decl_specifiers.storage_class != sc_none)
30448 {
30449 error_at (decl_spec_token_start->location,
30450 "explicit template specialization cannot have a storage class");
30451 decl = error_mark_node;
30452 }
30453
30454 if (decl && VAR_P (decl))
30455 check_template_variable (decl);
30456 }
30457
30458 /* Look for a trailing `;' after the declaration. */
30459 if (!function_definition_p
30460 && (decl == error_mark_node
30461 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
30462 cp_parser_skip_to_end_of_block_or_statement (parser);
30463
30464 out:
30465 pop_deferring_access_checks ();
30466
30467 /* Clear any current qualification; whatever comes next is the start
30468 of something new. */
30469 parser->scope = NULL_TREE;
30470 parser->qualifying_scope = NULL_TREE;
30471 parser->object_scope = NULL_TREE;
30472
30473 return decl;
30474 }
30475
30476 /* Parse a cast-expression that is not the operand of a unary "&". */
30477
30478 static cp_expr
30479 cp_parser_simple_cast_expression (cp_parser *parser)
30480 {
30481 return cp_parser_cast_expression (parser, /*address_p=*/false,
30482 /*cast_p=*/false, /*decltype*/false, NULL);
30483 }
30484
30485 /* Parse a functional cast to TYPE. Returns an expression
30486 representing the cast. */
30487
30488 static cp_expr
30489 cp_parser_functional_cast (cp_parser* parser, tree type)
30490 {
30491 vec<tree, va_gc> *vec;
30492 tree expression_list;
30493 cp_expr cast;
30494 bool nonconst_p;
30495
30496 location_t start_loc = input_location;
30497
30498 if (!type)
30499 type = error_mark_node;
30500
30501 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30502 {
30503 cp_lexer_set_source_position (parser->lexer);
30504 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
30505 expression_list = cp_parser_braced_list (parser, &nonconst_p);
30506 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
30507 if (TREE_CODE (type) == TYPE_DECL)
30508 type = TREE_TYPE (type);
30509
30510 cast = finish_compound_literal (type, expression_list,
30511 tf_warning_or_error, fcl_functional);
30512 /* Create a location of the form:
30513 type_name{i, f}
30514 ^~~~~~~~~~~~~~~
30515 with caret == start at the start of the type name,
30516 finishing at the closing brace. */
30517 location_t combined_loc = make_location (start_loc, start_loc,
30518 parser->lexer);
30519 cast.set_location (combined_loc);
30520 return cast;
30521 }
30522
30523
30524 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
30525 /*cast_p=*/true,
30526 /*allow_expansion_p=*/true,
30527 /*non_constant_p=*/NULL);
30528 if (vec == NULL)
30529 expression_list = error_mark_node;
30530 else
30531 {
30532 expression_list = build_tree_list_vec (vec);
30533 release_tree_vector (vec);
30534 }
30535
30536 /* Create a location of the form:
30537 float(i)
30538 ^~~~~~~~
30539 with caret == start at the start of the type name,
30540 finishing at the closing paren. */
30541 location_t combined_loc = make_location (start_loc, start_loc,
30542 parser->lexer);
30543 cast = build_functional_cast (combined_loc, type, expression_list,
30544 tf_warning_or_error);
30545
30546 /* [expr.const]/1: In an integral constant expression "only type
30547 conversions to integral or enumeration type can be used". */
30548 if (TREE_CODE (type) == TYPE_DECL)
30549 type = TREE_TYPE (type);
30550 if (cast != error_mark_node
30551 && !cast_valid_in_integral_constant_expression_p (type)
30552 && cp_parser_non_integral_constant_expression (parser,
30553 NIC_CONSTRUCTOR))
30554 return error_mark_node;
30555
30556 return cast;
30557 }
30558
30559 /* Save the tokens that make up the body of a member function defined
30560 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
30561 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
30562 specifiers applied to the declaration. Returns the FUNCTION_DECL
30563 for the member function. */
30564
30565 static tree
30566 cp_parser_save_member_function_body (cp_parser* parser,
30567 cp_decl_specifier_seq *decl_specifiers,
30568 cp_declarator *declarator,
30569 tree attributes)
30570 {
30571 cp_token *first;
30572 cp_token *last;
30573 tree fn;
30574 bool function_try_block = false;
30575
30576 /* Create the FUNCTION_DECL. */
30577 fn = grokmethod (decl_specifiers, declarator, attributes);
30578 cp_finalize_omp_declare_simd (parser, fn);
30579 cp_finalize_oacc_routine (parser, fn, true);
30580 /* If something went badly wrong, bail out now. */
30581 if (fn == error_mark_node)
30582 {
30583 /* If there's a function-body, skip it. */
30584 if (cp_parser_token_starts_function_definition_p
30585 (cp_lexer_peek_token (parser->lexer)))
30586 cp_parser_skip_to_end_of_block_or_statement (parser);
30587 return error_mark_node;
30588 }
30589
30590 /* Remember it, if there are default args to post process. */
30591 cp_parser_save_default_args (parser, fn);
30592
30593 /* Save away the tokens that make up the body of the
30594 function. */
30595 first = parser->lexer->next_token;
30596
30597 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
30598 cp_lexer_consume_token (parser->lexer);
30599 else if (cp_lexer_next_token_is_keyword (parser->lexer,
30600 RID_TRANSACTION_ATOMIC))
30601 {
30602 cp_lexer_consume_token (parser->lexer);
30603 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
30604 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
30605 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
30606 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
30607 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
30608 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
30609 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
30610 {
30611 cp_lexer_consume_token (parser->lexer);
30612 cp_lexer_consume_token (parser->lexer);
30613 cp_lexer_consume_token (parser->lexer);
30614 cp_lexer_consume_token (parser->lexer);
30615 cp_lexer_consume_token (parser->lexer);
30616 }
30617 else
30618 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
30619 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
30620 {
30621 cp_lexer_consume_token (parser->lexer);
30622 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
30623 break;
30624 }
30625 }
30626
30627 /* Handle function try blocks. */
30628 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
30629 {
30630 cp_lexer_consume_token (parser->lexer);
30631 function_try_block = true;
30632 }
30633 /* We can have braced-init-list mem-initializers before the fn body. */
30634 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30635 {
30636 cp_lexer_consume_token (parser->lexer);
30637 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
30638 {
30639 /* cache_group will stop after an un-nested { } pair, too. */
30640 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
30641 break;
30642
30643 /* variadic mem-inits have ... after the ')'. */
30644 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30645 cp_lexer_consume_token (parser->lexer);
30646 }
30647 }
30648 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
30649 /* Handle function try blocks. */
30650 if (function_try_block)
30651 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
30652 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
30653 last = parser->lexer->next_token;
30654
30655 /* Save away the inline definition; we will process it when the
30656 class is complete. */
30657 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
30658 DECL_PENDING_INLINE_P (fn) = 1;
30659
30660 /* We need to know that this was defined in the class, so that
30661 friend templates are handled correctly. */
30662 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
30663
30664 /* Add FN to the queue of functions to be parsed later. */
30665 vec_safe_push (unparsed_funs_with_definitions, fn);
30666
30667 return fn;
30668 }
30669
30670 /* Save the tokens that make up the in-class initializer for a non-static
30671 data member. Returns a DEFERRED_PARSE. */
30672
30673 static tree
30674 cp_parser_save_nsdmi (cp_parser* parser)
30675 {
30676 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
30677 }
30678
30679 /* Parse a template-argument-list, as well as the trailing ">" (but
30680 not the opening "<"). See cp_parser_template_argument_list for the
30681 return value. */
30682
30683 static tree
30684 cp_parser_enclosed_template_argument_list (cp_parser* parser)
30685 {
30686 tree arguments;
30687 tree saved_scope;
30688 tree saved_qualifying_scope;
30689 tree saved_object_scope;
30690 bool saved_greater_than_is_operator_p;
30691
30692 /* [temp.names]
30693
30694 When parsing a template-id, the first non-nested `>' is taken as
30695 the end of the template-argument-list rather than a greater-than
30696 operator. */
30697 saved_greater_than_is_operator_p
30698 = parser->greater_than_is_operator_p;
30699 parser->greater_than_is_operator_p = false;
30700 /* Parsing the argument list may modify SCOPE, so we save it
30701 here. */
30702 saved_scope = parser->scope;
30703 saved_qualifying_scope = parser->qualifying_scope;
30704 saved_object_scope = parser->object_scope;
30705 /* We need to evaluate the template arguments, even though this
30706 template-id may be nested within a "sizeof". */
30707 cp_evaluated ev;
30708 /* Parse the template-argument-list itself. */
30709 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
30710 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
30711 arguments = NULL_TREE;
30712 else
30713 arguments = cp_parser_template_argument_list (parser);
30714 /* Look for the `>' that ends the template-argument-list. If we find
30715 a '>>' instead, it's probably just a typo. */
30716 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
30717 {
30718 if (cxx_dialect != cxx98)
30719 {
30720 /* In C++0x, a `>>' in a template argument list or cast
30721 expression is considered to be two separate `>'
30722 tokens. So, change the current token to a `>', but don't
30723 consume it: it will be consumed later when the outer
30724 template argument list (or cast expression) is parsed.
30725 Note that this replacement of `>' for `>>' is necessary
30726 even if we are parsing tentatively: in the tentative
30727 case, after calling
30728 cp_parser_enclosed_template_argument_list we will always
30729 throw away all of the template arguments and the first
30730 closing `>', either because the template argument list
30731 was erroneous or because we are replacing those tokens
30732 with a CPP_TEMPLATE_ID token. The second `>' (which will
30733 not have been thrown away) is needed either to close an
30734 outer template argument list or to complete a new-style
30735 cast. */
30736 cp_token *token = cp_lexer_peek_token (parser->lexer);
30737 token->type = CPP_GREATER;
30738 }
30739 else if (!saved_greater_than_is_operator_p)
30740 {
30741 /* If we're in a nested template argument list, the '>>' has
30742 to be a typo for '> >'. We emit the error message, but we
30743 continue parsing and we push a '>' as next token, so that
30744 the argument list will be parsed correctly. Note that the
30745 global source location is still on the token before the
30746 '>>', so we need to say explicitly where we want it. */
30747 cp_token *token = cp_lexer_peek_token (parser->lexer);
30748 gcc_rich_location richloc (token->location);
30749 richloc.add_fixit_replace ("> >");
30750 error_at (&richloc, "%<>>%> should be %<> >%> "
30751 "within a nested template argument list");
30752
30753 token->type = CPP_GREATER;
30754 }
30755 else
30756 {
30757 /* If this is not a nested template argument list, the '>>'
30758 is a typo for '>'. Emit an error message and continue.
30759 Same deal about the token location, but here we can get it
30760 right by consuming the '>>' before issuing the diagnostic. */
30761 cp_token *token = cp_lexer_consume_token (parser->lexer);
30762 error_at (token->location,
30763 "spurious %<>>%>, use %<>%> to terminate "
30764 "a template argument list");
30765 }
30766 }
30767 else
30768 cp_parser_skip_to_end_of_template_parameter_list (parser);
30769 /* The `>' token might be a greater-than operator again now. */
30770 parser->greater_than_is_operator_p
30771 = saved_greater_than_is_operator_p;
30772 /* Restore the SAVED_SCOPE. */
30773 parser->scope = saved_scope;
30774 parser->qualifying_scope = saved_qualifying_scope;
30775 parser->object_scope = saved_object_scope;
30776
30777 return arguments;
30778 }
30779
30780 /* MEMBER_FUNCTION is a member function, or a friend. If default
30781 arguments, or the body of the function have not yet been parsed,
30782 parse them now. */
30783
30784 static void
30785 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
30786 {
30787 timevar_push (TV_PARSE_INMETH);
30788 /* If this member is a template, get the underlying
30789 FUNCTION_DECL. */
30790 if (DECL_FUNCTION_TEMPLATE_P (member_function))
30791 member_function = DECL_TEMPLATE_RESULT (member_function);
30792
30793 /* There should not be any class definitions in progress at this
30794 point; the bodies of members are only parsed outside of all class
30795 definitions. */
30796 gcc_assert (parser->num_classes_being_defined == 0);
30797 /* While we're parsing the member functions we might encounter more
30798 classes. We want to handle them right away, but we don't want
30799 them getting mixed up with functions that are currently in the
30800 queue. */
30801 push_unparsed_function_queues (parser);
30802
30803 /* Make sure that any template parameters are in scope. */
30804 maybe_begin_member_template_processing (member_function);
30805
30806 /* If the body of the function has not yet been parsed, parse it
30807 now. */
30808 if (DECL_PENDING_INLINE_P (member_function))
30809 {
30810 tree function_scope;
30811 cp_token_cache *tokens;
30812
30813 /* The function is no longer pending; we are processing it. */
30814 tokens = DECL_PENDING_INLINE_INFO (member_function);
30815 DECL_PENDING_INLINE_INFO (member_function) = NULL;
30816 DECL_PENDING_INLINE_P (member_function) = 0;
30817
30818 /* If this is a local class, enter the scope of the containing
30819 function. */
30820 function_scope = current_function_decl;
30821 if (function_scope)
30822 push_function_context ();
30823
30824 /* Push the body of the function onto the lexer stack. */
30825 cp_parser_push_lexer_for_tokens (parser, tokens);
30826
30827 /* Let the front end know that we going to be defining this
30828 function. */
30829 start_preparsed_function (member_function, NULL_TREE,
30830 SF_PRE_PARSED | SF_INCLASS_INLINE);
30831
30832 /* #pragma omp declare reduction needs special parsing. */
30833 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
30834 {
30835 parser->lexer->in_pragma = true;
30836 cp_parser_omp_declare_reduction_exprs (member_function, parser);
30837 finish_function (/*inline_p=*/true);
30838 cp_check_omp_declare_reduction (member_function);
30839 }
30840 else
30841 /* Now, parse the body of the function. */
30842 cp_parser_function_definition_after_declarator (parser,
30843 /*inline_p=*/true);
30844
30845 /* Leave the scope of the containing function. */
30846 if (function_scope)
30847 pop_function_context ();
30848 cp_parser_pop_lexer (parser);
30849 }
30850
30851 /* Remove any template parameters from the symbol table. */
30852 maybe_end_member_template_processing ();
30853
30854 /* Restore the queue. */
30855 pop_unparsed_function_queues (parser);
30856 timevar_pop (TV_PARSE_INMETH);
30857 }
30858
30859 /* If DECL contains any default args, remember it on the unparsed
30860 functions queue. */
30861
30862 static void
30863 cp_parser_save_default_args (cp_parser* parser, tree decl)
30864 {
30865 tree probe;
30866
30867 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
30868 probe;
30869 probe = TREE_CHAIN (probe))
30870 if (TREE_PURPOSE (probe))
30871 {
30872 cp_default_arg_entry entry = {current_class_type, decl};
30873 vec_safe_push (unparsed_funs_with_default_args, entry);
30874 break;
30875 }
30876
30877 /* Remember if there is a noexcept-specifier to post process. */
30878 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
30879 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
30880 vec_safe_push (unparsed_noexcepts, decl);
30881 }
30882
30883 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
30884 which is either a FIELD_DECL or PARM_DECL. Parse it and return
30885 the result. For a PARM_DECL, PARMTYPE is the corresponding type
30886 from the parameter-type-list. */
30887
30888 static tree
30889 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
30890 tree default_arg, tree parmtype)
30891 {
30892 cp_token_cache *tokens;
30893 tree parsed_arg;
30894 bool dummy;
30895
30896 if (default_arg == error_mark_node)
30897 return error_mark_node;
30898
30899 /* Push the saved tokens for the default argument onto the parser's
30900 lexer stack. */
30901 tokens = DEFPARSE_TOKENS (default_arg);
30902 cp_parser_push_lexer_for_tokens (parser, tokens);
30903
30904 start_lambda_scope (decl);
30905
30906 /* Parse the default argument. */
30907 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
30908 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
30909 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
30910
30911 finish_lambda_scope ();
30912
30913 if (parsed_arg == error_mark_node)
30914 cp_parser_skip_to_end_of_statement (parser);
30915
30916 if (!processing_template_decl)
30917 {
30918 /* In a non-template class, check conversions now. In a template,
30919 we'll wait and instantiate these as needed. */
30920 if (TREE_CODE (decl) == PARM_DECL)
30921 parsed_arg = check_default_argument (parmtype, parsed_arg,
30922 tf_warning_or_error);
30923 else if (maybe_reject_flexarray_init (decl, parsed_arg))
30924 parsed_arg = error_mark_node;
30925 else
30926 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
30927 }
30928
30929 /* If the token stream has not been completely used up, then
30930 there was extra junk after the end of the default
30931 argument. */
30932 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30933 {
30934 if (TREE_CODE (decl) == PARM_DECL)
30935 cp_parser_error (parser, "expected %<,%>");
30936 else
30937 cp_parser_error (parser, "expected %<;%>");
30938 }
30939
30940 /* Revert to the main lexer. */
30941 cp_parser_pop_lexer (parser);
30942
30943 return parsed_arg;
30944 }
30945
30946 /* FIELD is a non-static data member with an initializer which we saved for
30947 later; parse it now. */
30948
30949 static void
30950 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
30951 {
30952 tree def;
30953
30954 maybe_begin_member_template_processing (field);
30955
30956 push_unparsed_function_queues (parser);
30957 def = cp_parser_late_parse_one_default_arg (parser, field,
30958 DECL_INITIAL (field),
30959 NULL_TREE);
30960 pop_unparsed_function_queues (parser);
30961
30962 maybe_end_member_template_processing ();
30963
30964 DECL_INITIAL (field) = def;
30965 }
30966
30967 /* FN is a FUNCTION_DECL which may contains a parameter with an
30968 unparsed DEFERRED_PARSE. Parse the default args now. This function
30969 assumes that the current scope is the scope in which the default
30970 argument should be processed. */
30971
30972 static void
30973 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
30974 {
30975 unsigned char saved_local_variables_forbidden_p;
30976
30977 /* While we're parsing the default args, we might (due to the
30978 statement expression extension) encounter more classes. We want
30979 to handle them right away, but we don't want them getting mixed
30980 up with default args that are currently in the queue. */
30981 push_unparsed_function_queues (parser);
30982
30983 /* Local variable names (and the `this' keyword) may not appear
30984 in a default argument. */
30985 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
30986 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
30987
30988 push_defarg_context (fn);
30989
30990 begin_scope (sk_function_parms, fn);
30991
30992 /* Gather the PARM_DECLs into a vec so we can keep track of them when
30993 pushdecl clears DECL_CHAIN. */
30994 releasing_vec parms;
30995 for (tree parmdecl = DECL_ARGUMENTS (fn); parmdecl;
30996 parmdecl = DECL_CHAIN (parmdecl))
30997 vec_safe_push (parms, parmdecl);
30998
30999 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
31000 for (int i = 0;
31001 parm && parm != void_list_node;
31002 parm = TREE_CHAIN (parm),
31003 ++i)
31004 {
31005 tree default_arg = TREE_PURPOSE (parm);
31006 tree parsed_arg;
31007
31008 tree parmdecl = parms[i];
31009 pushdecl (parmdecl);
31010
31011 if (!default_arg)
31012 continue;
31013
31014 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
31015 /* This can happen for a friend declaration for a function
31016 already declared with default arguments. */
31017 continue;
31018
31019 parsed_arg
31020 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
31021 default_arg,
31022 TREE_VALUE (parm));
31023 TREE_PURPOSE (parm) = parsed_arg;
31024
31025 /* Update any instantiations we've already created. */
31026 for (tree copy : DEFPARSE_INSTANTIATIONS (default_arg))
31027 TREE_PURPOSE (copy) = parsed_arg;
31028 }
31029
31030 pop_bindings_and_leave_scope ();
31031
31032 /* Restore DECL_CHAINs after clobbering by pushdecl. */
31033 parm = NULL_TREE;
31034 for (int i = parms->length () - 1; i >= 0; --i)
31035 {
31036 DECL_CHAIN (parms[i]) = parm;
31037 parm = parms[i];
31038 }
31039
31040 pop_defarg_context ();
31041
31042 /* Make sure no default arg is missing. */
31043 check_default_args (fn);
31044
31045 /* Restore the state of local_variables_forbidden_p. */
31046 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
31047
31048 /* Restore the queue. */
31049 pop_unparsed_function_queues (parser);
31050 }
31051
31052 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
31053
31054 sizeof ... ( identifier )
31055
31056 where the 'sizeof' token has already been consumed. */
31057
31058 static tree
31059 cp_parser_sizeof_pack (cp_parser *parser)
31060 {
31061 /* Consume the `...'. */
31062 cp_lexer_consume_token (parser->lexer);
31063 maybe_warn_variadic_templates ();
31064
31065 matching_parens parens;
31066 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
31067 if (paren)
31068 parens.consume_open (parser);
31069 else
31070 permerror (cp_lexer_peek_token (parser->lexer)->location,
31071 "%<sizeof...%> argument must be surrounded by parentheses");
31072
31073 cp_token *token = cp_lexer_peek_token (parser->lexer);
31074 tree name = cp_parser_identifier (parser);
31075 if (name == error_mark_node)
31076 return error_mark_node;
31077 /* The name is not qualified. */
31078 parser->scope = NULL_TREE;
31079 parser->qualifying_scope = NULL_TREE;
31080 parser->object_scope = NULL_TREE;
31081 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
31082 if (expr == error_mark_node)
31083 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
31084 token->location);
31085 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
31086 expr = TREE_TYPE (expr);
31087 else if (TREE_CODE (expr) == CONST_DECL)
31088 expr = DECL_INITIAL (expr);
31089 expr = make_pack_expansion (expr);
31090 PACK_EXPANSION_SIZEOF_P (expr) = true;
31091
31092 if (paren)
31093 parens.require_close (parser);
31094
31095 return expr;
31096 }
31097
31098 /* Parse the operand of `sizeof' (or a similar operator). Returns
31099 either a TYPE or an expression, depending on the form of the
31100 input. The KEYWORD indicates which kind of expression we have
31101 encountered. */
31102
31103 static tree
31104 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
31105 {
31106 tree expr = NULL_TREE;
31107 const char *saved_message;
31108 const char *saved_message_arg;
31109 bool saved_integral_constant_expression_p;
31110 bool saved_non_integral_constant_expression_p;
31111
31112 /* If it's a `...', then we are computing the length of a parameter
31113 pack. */
31114 if (keyword == RID_SIZEOF
31115 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31116 return cp_parser_sizeof_pack (parser);
31117
31118 /* Types cannot be defined in a `sizeof' expression. Save away the
31119 old message. */
31120 saved_message = parser->type_definition_forbidden_message;
31121 saved_message_arg = parser->type_definition_forbidden_message_arg;
31122 parser->type_definition_forbidden_message
31123 = G_("types may not be defined in %qs expressions");
31124 parser->type_definition_forbidden_message_arg
31125 = IDENTIFIER_POINTER (ridpointers[keyword]);
31126
31127 /* The restrictions on constant-expressions do not apply inside
31128 sizeof expressions. */
31129 saved_integral_constant_expression_p
31130 = parser->integral_constant_expression_p;
31131 saved_non_integral_constant_expression_p
31132 = parser->non_integral_constant_expression_p;
31133 parser->integral_constant_expression_p = false;
31134
31135 /* Do not actually evaluate the expression. */
31136 ++cp_unevaluated_operand;
31137 ++c_inhibit_evaluation_warnings;
31138 /* If it's a `(', then we might be looking at the type-id
31139 construction. */
31140 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31141 {
31142 tree type = NULL_TREE;
31143
31144 tentative_firewall firewall (parser);
31145
31146 /* We can't be sure yet whether we're looking at a type-id or an
31147 expression. */
31148 cp_parser_parse_tentatively (parser);
31149
31150 matching_parens parens;
31151 parens.consume_open (parser);
31152
31153 /* Note: as a GNU Extension, compound literals are considered
31154 postfix-expressions as they are in C99, so they are valid
31155 arguments to sizeof. See comment in cp_parser_cast_expression
31156 for details. */
31157 if (cp_parser_compound_literal_p (parser))
31158 cp_parser_simulate_error (parser);
31159 else
31160 {
31161 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
31162 parser->in_type_id_in_expr_p = true;
31163 /* Look for the type-id. */
31164 type = cp_parser_type_id (parser);
31165 /* Look for the closing `)'. */
31166 parens.require_close (parser);
31167 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
31168 }
31169
31170 /* If all went well, then we're done. */
31171 if (cp_parser_parse_definitely (parser))
31172 expr = type;
31173 else
31174 {
31175 /* Commit to the tentative_firewall so we get syntax errors. */
31176 cp_parser_commit_to_tentative_parse (parser);
31177
31178 expr = cp_parser_unary_expression (parser);
31179 }
31180 }
31181 else
31182 expr = cp_parser_unary_expression (parser);
31183
31184 /* Go back to evaluating expressions. */
31185 --cp_unevaluated_operand;
31186 --c_inhibit_evaluation_warnings;
31187
31188 /* And restore the old one. */
31189 parser->type_definition_forbidden_message = saved_message;
31190 parser->type_definition_forbidden_message_arg = saved_message_arg;
31191 parser->integral_constant_expression_p
31192 = saved_integral_constant_expression_p;
31193 parser->non_integral_constant_expression_p
31194 = saved_non_integral_constant_expression_p;
31195
31196 return expr;
31197 }
31198
31199 /* If the current declaration has no declarator, return true. */
31200
31201 static bool
31202 cp_parser_declares_only_class_p (cp_parser *parser)
31203 {
31204 /* If the next token is a `;' or a `,' then there is no
31205 declarator. */
31206 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
31207 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
31208 }
31209
31210 /* Update the DECL_SPECS to reflect the storage class indicated by
31211 KEYWORD. */
31212
31213 static void
31214 cp_parser_set_storage_class (cp_parser *parser,
31215 cp_decl_specifier_seq *decl_specs,
31216 enum rid keyword,
31217 cp_token *token)
31218 {
31219 cp_storage_class storage_class;
31220
31221 if (parser->in_unbraced_linkage_specification_p)
31222 {
31223 error_at (token->location, "invalid use of %qD in linkage specification",
31224 ridpointers[keyword]);
31225 return;
31226 }
31227 else if (decl_specs->storage_class != sc_none)
31228 {
31229 decl_specs->conflicting_specifiers_p = true;
31230 return;
31231 }
31232
31233 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
31234 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
31235 && decl_specs->gnu_thread_keyword_p)
31236 {
31237 pedwarn (decl_specs->locations[ds_thread], 0,
31238 "%<__thread%> before %qD", ridpointers[keyword]);
31239 }
31240
31241 switch (keyword)
31242 {
31243 case RID_AUTO:
31244 storage_class = sc_auto;
31245 break;
31246 case RID_REGISTER:
31247 storage_class = sc_register;
31248 break;
31249 case RID_STATIC:
31250 storage_class = sc_static;
31251 break;
31252 case RID_EXTERN:
31253 storage_class = sc_extern;
31254 break;
31255 case RID_MUTABLE:
31256 storage_class = sc_mutable;
31257 break;
31258 default:
31259 gcc_unreachable ();
31260 }
31261 decl_specs->storage_class = storage_class;
31262 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
31263
31264 /* A storage class specifier cannot be applied alongside a typedef
31265 specifier. If there is a typedef specifier present then set
31266 conflicting_specifiers_p which will trigger an error later
31267 on in grokdeclarator. */
31268 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
31269 decl_specs->conflicting_specifiers_p = true;
31270 }
31271
31272 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
31273 is true, the type is a class or enum definition. */
31274
31275 static void
31276 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
31277 tree type_spec,
31278 cp_token *token,
31279 bool type_definition_p)
31280 {
31281 decl_specs->any_specifiers_p = true;
31282
31283 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
31284 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
31285 this is what happened. In system headers, we ignore these
31286 declarations so that G++ can work with system headers that are not
31287 C++-safe. */
31288 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
31289 && !type_definition_p
31290 && (type_spec == boolean_type_node
31291 || type_spec == char8_type_node
31292 || type_spec == char16_type_node
31293 || type_spec == char32_type_node
31294 || type_spec == wchar_type_node)
31295 && (decl_specs->type
31296 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
31297 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
31298 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
31299 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
31300 {
31301 decl_specs->redefined_builtin_type = type_spec;
31302 set_and_check_decl_spec_loc (decl_specs,
31303 ds_redefined_builtin_type_spec,
31304 token);
31305 if (!decl_specs->type)
31306 {
31307 decl_specs->type = type_spec;
31308 decl_specs->type_definition_p = false;
31309 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
31310 }
31311 }
31312 else if (decl_specs->type)
31313 decl_specs->multiple_types_p = true;
31314 else
31315 {
31316 decl_specs->type = type_spec;
31317 decl_specs->type_definition_p = type_definition_p;
31318 decl_specs->redefined_builtin_type = NULL_TREE;
31319 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
31320 }
31321 }
31322
31323 /* True iff TOKEN is the GNU keyword __thread. */
31324
31325 static bool
31326 token_is__thread (cp_token *token)
31327 {
31328 gcc_assert (token->keyword == RID_THREAD);
31329 return id_equal (token->u.value, "__thread");
31330 }
31331
31332 /* Set the location for a declarator specifier and check if it is
31333 duplicated.
31334
31335 DECL_SPECS is the sequence of declarator specifiers onto which to
31336 set the location.
31337
31338 DS is the single declarator specifier to set which location is to
31339 be set onto the existing sequence of declarators.
31340
31341 LOCATION is the location for the declarator specifier to
31342 consider. */
31343
31344 static void
31345 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
31346 cp_decl_spec ds, cp_token *token)
31347 {
31348 gcc_assert (ds < ds_last);
31349
31350 if (decl_specs == NULL)
31351 return;
31352
31353 location_t location = token->location;
31354
31355 if (decl_specs->locations[ds] == 0)
31356 {
31357 decl_specs->locations[ds] = location;
31358 if (ds == ds_thread)
31359 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
31360 }
31361 else
31362 {
31363 if (ds == ds_long)
31364 {
31365 if (decl_specs->locations[ds_long_long] != 0)
31366 error_at (location,
31367 "%<long long long%> is too long for GCC");
31368 else
31369 {
31370 decl_specs->locations[ds_long_long] = location;
31371 pedwarn_cxx98 (location,
31372 OPT_Wlong_long,
31373 "ISO C++ 1998 does not support %<long long%>");
31374 }
31375 }
31376 else if (ds == ds_thread)
31377 {
31378 bool gnu = token_is__thread (token);
31379 gcc_rich_location richloc (location);
31380 if (gnu != decl_specs->gnu_thread_keyword_p)
31381 {
31382 richloc.add_range (decl_specs->locations[ds_thread]);
31383 error_at (&richloc,
31384 "both %<__thread%> and %<thread_local%> specified");
31385 }
31386 else
31387 {
31388 richloc.add_fixit_remove ();
31389 error_at (&richloc, "duplicate %qD", token->u.value);
31390 }
31391 }
31392 else
31393 {
31394 static const char *const decl_spec_names[] = {
31395 "signed",
31396 "unsigned",
31397 "short",
31398 "long",
31399 "const",
31400 "volatile",
31401 "restrict",
31402 "inline",
31403 "virtual",
31404 "explicit",
31405 "friend",
31406 "typedef",
31407 "using",
31408 "constexpr",
31409 "__complex",
31410 "constinit",
31411 "consteval"
31412 };
31413 gcc_rich_location richloc (location);
31414 richloc.add_fixit_remove ();
31415 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
31416 }
31417 }
31418 }
31419
31420 /* Return true iff the declarator specifier DS is present in the
31421 sequence of declarator specifiers DECL_SPECS. */
31422
31423 bool
31424 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
31425 cp_decl_spec ds)
31426 {
31427 gcc_assert (ds < ds_last);
31428
31429 if (decl_specs == NULL)
31430 return false;
31431
31432 return decl_specs->locations[ds] != 0;
31433 }
31434
31435 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
31436 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
31437
31438 static bool
31439 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
31440 {
31441 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
31442 }
31443
31444 /* Issue an error message indicating that TOKEN_DESC was expected.
31445 If KEYWORD is true, it indicated this function is called by
31446 cp_parser_require_keword and the required token can only be
31447 a indicated keyword.
31448
31449 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
31450 within any error as the location of an "opening" token matching
31451 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
31452 RT_CLOSE_PAREN). */
31453
31454 static void
31455 cp_parser_required_error (cp_parser *parser,
31456 required_token token_desc,
31457 bool keyword,
31458 location_t matching_location)
31459 {
31460 if (cp_parser_simulate_error (parser))
31461 return;
31462
31463 const char *gmsgid = NULL;
31464 switch (token_desc)
31465 {
31466 case RT_NEW:
31467 gmsgid = G_("expected %<new%>");
31468 break;
31469 case RT_DELETE:
31470 gmsgid = G_("expected %<delete%>");
31471 break;
31472 case RT_RETURN:
31473 gmsgid = G_("expected %<return%>");
31474 break;
31475 case RT_WHILE:
31476 gmsgid = G_("expected %<while%>");
31477 break;
31478 case RT_EXTERN:
31479 gmsgid = G_("expected %<extern%>");
31480 break;
31481 case RT_STATIC_ASSERT:
31482 gmsgid = G_("expected %<static_assert%>");
31483 break;
31484 case RT_DECLTYPE:
31485 gmsgid = G_("expected %<decltype%>");
31486 break;
31487 case RT_OPERATOR:
31488 gmsgid = G_("expected %<operator%>");
31489 break;
31490 case RT_CLASS:
31491 gmsgid = G_("expected %<class%>");
31492 break;
31493 case RT_TEMPLATE:
31494 gmsgid = G_("expected %<template%>");
31495 break;
31496 case RT_NAMESPACE:
31497 gmsgid = G_("expected %<namespace%>");
31498 break;
31499 case RT_USING:
31500 gmsgid = G_("expected %<using%>");
31501 break;
31502 case RT_ASM:
31503 gmsgid = G_("expected %<asm%>");
31504 break;
31505 case RT_TRY:
31506 gmsgid = G_("expected %<try%>");
31507 break;
31508 case RT_CATCH:
31509 gmsgid = G_("expected %<catch%>");
31510 break;
31511 case RT_THROW:
31512 gmsgid = G_("expected %<throw%>");
31513 break;
31514 case RT_AUTO:
31515 gmsgid = G_("expected %<auto%>");
31516 break;
31517 case RT_LABEL:
31518 gmsgid = G_("expected %<__label__%>");
31519 break;
31520 case RT_AT_TRY:
31521 gmsgid = G_("expected %<@try%>");
31522 break;
31523 case RT_AT_SYNCHRONIZED:
31524 gmsgid = G_("expected %<@synchronized%>");
31525 break;
31526 case RT_AT_THROW:
31527 gmsgid = G_("expected %<@throw%>");
31528 break;
31529 case RT_TRANSACTION_ATOMIC:
31530 gmsgid = G_("expected %<__transaction_atomic%>");
31531 break;
31532 case RT_TRANSACTION_RELAXED:
31533 gmsgid = G_("expected %<__transaction_relaxed%>");
31534 break;
31535 case RT_CO_YIELD:
31536 gmsgid = G_("expected %<co_yield%>");
31537 break;
31538 default:
31539 break;
31540 }
31541
31542 if (!gmsgid && !keyword)
31543 {
31544 switch (token_desc)
31545 {
31546 case RT_SEMICOLON:
31547 gmsgid = G_("expected %<;%>");
31548 break;
31549 case RT_OPEN_PAREN:
31550 gmsgid = G_("expected %<(%>");
31551 break;
31552 case RT_CLOSE_BRACE:
31553 gmsgid = G_("expected %<}%>");
31554 break;
31555 case RT_OPEN_BRACE:
31556 gmsgid = G_("expected %<{%>");
31557 break;
31558 case RT_CLOSE_SQUARE:
31559 gmsgid = G_("expected %<]%>");
31560 break;
31561 case RT_OPEN_SQUARE:
31562 gmsgid = G_("expected %<[%>");
31563 break;
31564 case RT_COMMA:
31565 gmsgid = G_("expected %<,%>");
31566 break;
31567 case RT_SCOPE:
31568 gmsgid = G_("expected %<::%>");
31569 break;
31570 case RT_LESS:
31571 gmsgid = G_("expected %<<%>");
31572 break;
31573 case RT_GREATER:
31574 gmsgid = G_("expected %<>%>");
31575 break;
31576 case RT_EQ:
31577 gmsgid = G_("expected %<=%>");
31578 break;
31579 case RT_ELLIPSIS:
31580 gmsgid = G_("expected %<...%>");
31581 break;
31582 case RT_MULT:
31583 gmsgid = G_("expected %<*%>");
31584 break;
31585 case RT_COMPL:
31586 gmsgid = G_("expected %<~%>");
31587 break;
31588 case RT_COLON:
31589 gmsgid = G_("expected %<:%>");
31590 break;
31591 case RT_COLON_SCOPE:
31592 gmsgid = G_("expected %<:%> or %<::%>");
31593 break;
31594 case RT_CLOSE_PAREN:
31595 gmsgid = G_("expected %<)%>");
31596 break;
31597 case RT_COMMA_CLOSE_PAREN:
31598 gmsgid = G_("expected %<,%> or %<)%>");
31599 break;
31600 case RT_PRAGMA_EOL:
31601 gmsgid = G_("expected end of line");
31602 break;
31603 case RT_NAME:
31604 gmsgid = G_("expected identifier");
31605 break;
31606 case RT_SELECT:
31607 gmsgid = G_("expected selection-statement");
31608 break;
31609 case RT_ITERATION:
31610 gmsgid = G_("expected iteration-statement");
31611 break;
31612 case RT_JUMP:
31613 gmsgid = G_("expected jump-statement");
31614 break;
31615 case RT_CLASS_KEY:
31616 gmsgid = G_("expected class-key");
31617 break;
31618 case RT_CLASS_TYPENAME_TEMPLATE:
31619 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
31620 break;
31621 default:
31622 gcc_unreachable ();
31623 }
31624 }
31625
31626 if (gmsgid)
31627 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
31628 }
31629
31630
31631 /* If the next token is of the indicated TYPE, consume it. Otherwise,
31632 issue an error message indicating that TOKEN_DESC was expected.
31633
31634 Returns the token consumed, if the token had the appropriate type.
31635 Otherwise, returns NULL.
31636
31637 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
31638 within any error as the location of an "opening" token matching
31639 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
31640 RT_CLOSE_PAREN). */
31641
31642 static cp_token *
31643 cp_parser_require (cp_parser* parser,
31644 enum cpp_ttype type,
31645 required_token token_desc,
31646 location_t matching_location)
31647 {
31648 if (cp_lexer_next_token_is (parser->lexer, type))
31649 return cp_lexer_consume_token (parser->lexer);
31650 else
31651 {
31652 /* Output the MESSAGE -- unless we're parsing tentatively. */
31653 if (!cp_parser_simulate_error (parser))
31654 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
31655 matching_location);
31656 return NULL;
31657 }
31658 }
31659
31660 /* An error message is produced if the next token is not '>'.
31661 All further tokens are skipped until the desired token is
31662 found or '{', '}', ';' or an unbalanced ')' or ']'. */
31663
31664 static void
31665 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
31666 {
31667 /* Current level of '< ... >'. */
31668 unsigned level = 0;
31669 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
31670 unsigned nesting_depth = 0;
31671
31672 /* Are we ready, yet? If not, issue error message. */
31673 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
31674 return;
31675
31676 /* Skip tokens until the desired token is found. */
31677 while (true)
31678 {
31679 /* Peek at the next token. */
31680 switch (cp_lexer_peek_token (parser->lexer)->type)
31681 {
31682 case CPP_LESS:
31683 if (!nesting_depth)
31684 ++level;
31685 break;
31686
31687 case CPP_RSHIFT:
31688 if (cxx_dialect == cxx98)
31689 /* C++0x views the `>>' operator as two `>' tokens, but
31690 C++98 does not. */
31691 break;
31692 else if (!nesting_depth && level-- == 0)
31693 {
31694 /* We've hit a `>>' where the first `>' closes the
31695 template argument list, and the second `>' is
31696 spurious. Just consume the `>>' and stop; we've
31697 already produced at least one error. */
31698 cp_lexer_consume_token (parser->lexer);
31699 return;
31700 }
31701 /* Fall through for C++0x, so we handle the second `>' in
31702 the `>>'. */
31703 gcc_fallthrough ();
31704
31705 case CPP_GREATER:
31706 if (!nesting_depth && level-- == 0)
31707 {
31708 /* We've reached the token we want, consume it and stop. */
31709 cp_lexer_consume_token (parser->lexer);
31710 return;
31711 }
31712 break;
31713
31714 case CPP_OPEN_PAREN:
31715 case CPP_OPEN_SQUARE:
31716 ++nesting_depth;
31717 break;
31718
31719 case CPP_CLOSE_PAREN:
31720 case CPP_CLOSE_SQUARE:
31721 if (nesting_depth-- == 0)
31722 return;
31723 break;
31724
31725 case CPP_EOF:
31726 case CPP_PRAGMA_EOL:
31727 case CPP_SEMICOLON:
31728 case CPP_OPEN_BRACE:
31729 case CPP_CLOSE_BRACE:
31730 /* The '>' was probably forgotten, don't look further. */
31731 return;
31732
31733 default:
31734 break;
31735 }
31736
31737 /* Consume this token. */
31738 cp_lexer_consume_token (parser->lexer);
31739 }
31740 }
31741
31742 /* If the next token is the indicated keyword, consume it. Otherwise,
31743 issue an error message indicating that TOKEN_DESC was expected.
31744
31745 Returns the token consumed, if the token had the appropriate type.
31746 Otherwise, returns NULL. */
31747
31748 static cp_token *
31749 cp_parser_require_keyword (cp_parser* parser,
31750 enum rid keyword,
31751 required_token token_desc)
31752 {
31753 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
31754
31755 if (token && token->keyword != keyword)
31756 {
31757 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
31758 UNKNOWN_LOCATION);
31759 return NULL;
31760 }
31761
31762 return token;
31763 }
31764
31765 /* Returns TRUE iff TOKEN is a token that can begin the body of a
31766 function-definition. */
31767
31768 static bool
31769 cp_parser_token_starts_function_definition_p (cp_token* token)
31770 {
31771 return (/* An ordinary function-body begins with an `{'. */
31772 token->type == CPP_OPEN_BRACE
31773 /* A ctor-initializer begins with a `:'. */
31774 || token->type == CPP_COLON
31775 /* A function-try-block begins with `try'. */
31776 || token->keyword == RID_TRY
31777 /* A function-transaction-block begins with `__transaction_atomic'
31778 or `__transaction_relaxed'. */
31779 || token->keyword == RID_TRANSACTION_ATOMIC
31780 || token->keyword == RID_TRANSACTION_RELAXED
31781 /* The named return value extension begins with `return'. */
31782 || token->keyword == RID_RETURN);
31783 }
31784
31785 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
31786 definition. */
31787
31788 static bool
31789 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
31790 {
31791 cp_token *token;
31792
31793 token = cp_lexer_peek_token (parser->lexer);
31794 return (token->type == CPP_OPEN_BRACE
31795 || (token->type == CPP_COLON
31796 && !parser->colon_doesnt_start_class_def_p));
31797 }
31798
31799 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
31800 C++0x) ending a template-argument. */
31801
31802 static bool
31803 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
31804 {
31805 cp_token *token;
31806
31807 token = cp_lexer_peek_token (parser->lexer);
31808 return (token->type == CPP_COMMA
31809 || token->type == CPP_GREATER
31810 || token->type == CPP_ELLIPSIS
31811 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
31812 }
31813
31814 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
31815 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
31816
31817 static bool
31818 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
31819 size_t n)
31820 {
31821 cp_token *token;
31822
31823 token = cp_lexer_peek_nth_token (parser->lexer, n);
31824 if (token->type == CPP_LESS)
31825 return true;
31826 /* Check for the sequence `<::' in the original code. It would be lexed as
31827 `[:', where `[' is a digraph, and there is no whitespace before
31828 `:'. */
31829 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
31830 {
31831 cp_token *token2;
31832 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
31833 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
31834 return true;
31835 }
31836 return false;
31837 }
31838
31839 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
31840 or none_type otherwise. */
31841
31842 static enum tag_types
31843 cp_parser_token_is_class_key (cp_token* token)
31844 {
31845 switch (token->keyword)
31846 {
31847 case RID_CLASS:
31848 return class_type;
31849 case RID_STRUCT:
31850 return record_type;
31851 case RID_UNION:
31852 return union_type;
31853
31854 default:
31855 return none_type;
31856 }
31857 }
31858
31859 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
31860 or none_type otherwise or if the token is null. */
31861
31862 static enum tag_types
31863 cp_parser_token_is_type_parameter_key (cp_token* token)
31864 {
31865 if (!token)
31866 return none_type;
31867
31868 switch (token->keyword)
31869 {
31870 case RID_CLASS:
31871 return class_type;
31872 case RID_TYPENAME:
31873 return typename_type;
31874
31875 default:
31876 return none_type;
31877 }
31878 }
31879
31880 /* Diagnose redundant enum-keys. */
31881
31882 static void
31883 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
31884 tree type, rid scoped_key)
31885 {
31886 if (!warn_redundant_tags)
31887 return;
31888
31889 tree type_decl = TYPE_MAIN_DECL (type);
31890 tree name = DECL_NAME (type_decl);
31891 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
31892 push_deferring_access_checks (dk_no_check);
31893 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
31894 pop_deferring_access_checks ();
31895
31896 /* The enum-key is redundant for uses of the TYPE that are not
31897 declarations and for which name lookup returns just the type
31898 itself. */
31899 if (decl != type_decl)
31900 return;
31901
31902 if (scoped_key != RID_CLASS
31903 && scoped_key != RID_STRUCT
31904 && current_lang_name != lang_name_cplusplus
31905 && current_namespace == global_namespace)
31906 {
31907 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
31908 enum tag in shared C/C++ code in files (such as headers) included
31909 in the main source file. */
31910 const line_map_ordinary *map = NULL;
31911 linemap_resolve_location (line_table, key_loc,
31912 LRK_MACRO_DEFINITION_LOCATION,
31913 &map);
31914 if (!MAIN_FILE_P (map))
31915 return;
31916 }
31917
31918 gcc_rich_location richloc (key_loc);
31919 richloc.add_fixit_remove (key_loc);
31920 warning_at (&richloc, OPT_Wredundant_tags,
31921 "redundant enum-key %<enum%s%> in reference to %q#T",
31922 (scoped_key == RID_CLASS ? " class"
31923 : scoped_key == RID_STRUCT ? " struct" : ""), type);
31924 }
31925
31926 /* Describes the set of declarations of a struct, class, or class template
31927 or its specializations. Used for -Wmismatched-tags. */
31928
31929 class class_decl_loc_t
31930 {
31931 public:
31932
31933 class_decl_loc_t ()
31934 : locvec (), idxdef (), def_class_key ()
31935 {
31936 locvec.create (4);
31937 }
31938
31939 /* Constructs an object for a single declaration of a class with
31940 CLASS_KEY at the current location in the current function (or
31941 at another scope). KEY_REDUNDANT is true if the class-key may
31942 be omitted in the current context without an ambiguity with
31943 another symbol with the same name.
31944 DEF_P is true for a class declaration that is a definition.
31945 CURLOC is the associated location. */
31946 class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
31947 location_t curloc = input_location)
31948 : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
31949 {
31950 locvec.create (4);
31951 class_key_loc_t ckl (current_function_decl, curloc, class_key,
31952 key_redundant);
31953 locvec.quick_push (ckl);
31954 }
31955
31956 /* Copy, assign, and destroy the object. Necessary because LOCVEC
31957 isn't safely copyable and assignable and doesn't release storage
31958 on its own. */
31959 class_decl_loc_t (const class_decl_loc_t &rhs)
31960 : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
31961 def_class_key (rhs.def_class_key)
31962 { }
31963
31964 class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
31965 {
31966 if (this == &rhs)
31967 return *this;
31968 locvec.release ();
31969 locvec = rhs.locvec.copy ();
31970 idxdef = rhs.idxdef;
31971 def_class_key = rhs.def_class_key;
31972 return *this;
31973 }
31974
31975 ~class_decl_loc_t ()
31976 {
31977 locvec.release ();
31978 }
31979
31980 /* Issues -Wmismatched-tags for a single class. */
31981 void diag_mismatched_tags (tree);
31982
31983 /* Issues -Wmismatched-tags for all classes. */
31984 static void diag_mismatched_tags ();
31985
31986 /* Adds TYPE_DECL to the collection of class decls and diagnoses
31987 redundant tags (if -Wredundant-tags is enabled). */
31988 static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
31989
31990 /* Either adds this decl to the collection of class decls
31991 or diagnoses it, whichever is appropriate. */
31992 void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
31993
31994 private:
31995
31996 tree function (unsigned i) const
31997 {
31998 return locvec[i].func;
31999 }
32000
32001 location_t location (unsigned i) const
32002 {
32003 return locvec[i].loc;
32004 }
32005
32006 bool key_redundant (unsigned i) const
32007 {
32008 return locvec[i].key_redundant;
32009 }
32010
32011 tag_types class_key (unsigned i) const
32012 {
32013 return locvec[i].class_key;
32014 }
32015
32016 /* True if a definition for the class has been seen. */
32017 bool def_p () const
32018 {
32019 return idxdef < locvec.length ();
32020 }
32021
32022 /* The location of a single mention of a class type with the given
32023 class-key. */
32024 struct class_key_loc_t
32025 {
32026 class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
32027 : func (func), loc (loc), class_key (key), key_redundant (redundant)
32028 { }
32029
32030 /* The function the type is mentioned in. */
32031 tree func;
32032 /* The exact location. */
32033 location_t loc;
32034 /* The class-key used in the mention of the type. */
32035 tag_types class_key;
32036 /* True when the class-key could be omitted at this location
32037 without an ambiguity with another symbol of the same name. */
32038 bool key_redundant;
32039 };
32040 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
32041 vec <class_key_loc_t> locvec;
32042 /* LOCVEC index of the definition or UINT_MAX if none exists. */
32043 unsigned idxdef;
32044 /* The class-key the class was last declared with or none_type when
32045 it has been declared with a mismatched key. */
32046 tag_types def_class_key;
32047
32048 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
32049 description above. */
32050 typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
32051 static class_to_loc_map_t class2loc;
32052 };
32053
32054 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
32055
32056 /* Issue an error message if the CLASS_KEY does not match the TYPE.
32057 DEF_P is expected to be set for a definition of class TYPE. DECL_P
32058 is set for a declaration of class TYPE and clear for a reference to
32059 it that is not a declaration of it. */
32060
32061 static void
32062 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
32063 tag_types class_key, tree type, bool def_p,
32064 bool decl_p)
32065 {
32066 if (type == error_mark_node)
32067 return;
32068
32069 bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
32070 if (seen_as_union != (class_key == union_type))
32071 {
32072 if (permerror (input_location, "%qs tag used in naming %q#T",
32073 class_key == union_type ? "union"
32074 : class_key == record_type ? "struct" : "class",
32075 type))
32076 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
32077 "%q#T was previously declared here", type);
32078 return;
32079 }
32080
32081 if (!warn_mismatched_tags && !warn_redundant_tags)
32082 return;
32083
32084 /* Only consider the true class-keys below and ignore typename_type,
32085 etc. that are not C++ class-keys. */
32086 if (class_key != class_type
32087 && class_key != record_type
32088 && class_key != union_type)
32089 return;
32090
32091 class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
32092 }
32093
32094 /* Returns the template or specialization of one to which the RECORD_TYPE
32095 TYPE corresponds. */
32096
32097 static tree
32098 specialization_of (tree type)
32099 {
32100 tree ret = type;
32101
32102 /* Determine the template or its partial specialization to which TYPE
32103 corresponds. */
32104 if (tree spec = most_specialized_partial_spec (type, tf_none))
32105 if (spec != error_mark_node)
32106 ret = TREE_TYPE (TREE_VALUE (spec));
32107
32108 if (ret == type)
32109 ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
32110
32111 return TYPE_MAIN_DECL (ret);
32112 }
32113
32114
32115 /* Adds the class TYPE to the collection of class decls and diagnoses
32116 redundant tags (if -Wredundant-tags is enabled).
32117 DEF_P is expected to be set for a definition of class TYPE. DECL_P
32118 is set for a (likely, based on syntactic context) declaration of class
32119 TYPE and clear for a reference to it that is not a declaration of it. */
32120
32121 void
32122 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
32123 tag_types class_key, tree type, bool def_p, bool decl_p)
32124 {
32125 tree type_decl = TYPE_MAIN_DECL (type);
32126 tree name = DECL_NAME (type_decl);
32127 /* Look up the NAME to see if it unambiguously refers to the TYPE
32128 and set KEY_REDUNDANT if so. */
32129 push_deferring_access_checks (dk_no_check);
32130 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
32131 pop_deferring_access_checks ();
32132
32133 /* The class-key is redundant for uses of the CLASS_TYPE that are
32134 neither definitions of it nor declarations, and for which name
32135 lookup returns just the type itself. */
32136 bool key_redundant = (!def_p && !decl_p
32137 && (decl == type_decl
32138 || TREE_CODE (decl) == TEMPLATE_DECL
32139 || TYPE_BEING_DEFINED (type)));
32140
32141 if (key_redundant
32142 && class_key != class_type
32143 && current_lang_name != lang_name_cplusplus
32144 && current_namespace == global_namespace)
32145 {
32146 /* Avoid issuing the diagnostic for apparently redundant struct
32147 and union class-keys in shared C/C++ code in files (such as
32148 headers) included in the main source file. */
32149 const line_map_ordinary *map = NULL;
32150 linemap_resolve_location (line_table, key_loc,
32151 LRK_MACRO_DEFINITION_LOCATION,
32152 &map);
32153 if (!MAIN_FILE_P (map))
32154 key_redundant = false;
32155 }
32156
32157 /* Set if a declaration of TYPE has previously been seen or if it must
32158 exist in a precompiled header. */
32159 bool exist;
32160 class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
32161 if (!exist)
32162 {
32163 tree type = TREE_TYPE (type_decl);
32164 if (def_p || !COMPLETE_TYPE_P (type))
32165 {
32166 /* TYPE_DECL is the first declaration or definition of the type
32167 (outside precompiled headers -- see below). Just create
32168 a new entry for it and return unless it's a declaration
32169 involving a template that may need to be diagnosed by
32170 -Wredundant-tags. */
32171 *rdl = class_decl_loc_t (class_key, false, def_p);
32172 if (TREE_CODE (decl) != TEMPLATE_DECL)
32173 return;
32174 }
32175 else
32176 {
32177 /* TYPE was previously defined in some unknown precompiled hdeader.
32178 Simply add a record of its definition at an unknown location and
32179 proceed below to add a reference to it at the current location.
32180 (Declarations in precompiled headers that are not definitions
32181 are ignored.) */
32182 tag_types def_key
32183 = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
32184 location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
32185 *rdl = class_decl_loc_t (def_key, false, true, def_loc);
32186 exist = true;
32187 }
32188 }
32189
32190 /* A prior declaration of TYPE_DECL has been seen. */
32191
32192 if (key_redundant)
32193 {
32194 gcc_rich_location richloc (key_loc);
32195 richloc.add_fixit_remove (key_loc);
32196 warning_at (&richloc, OPT_Wredundant_tags,
32197 "redundant class-key %qs in reference to %q#T",
32198 class_key == union_type ? "union"
32199 : class_key == record_type ? "struct" : "class",
32200 type);
32201 }
32202
32203 if (!exist)
32204 /* Do nothing if this is the first declaration of the type. */
32205 return;
32206
32207 if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
32208 /* Do nothing if the class-key in this declaration matches
32209 the definition. */
32210 return;
32211
32212 rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
32213 def_p);
32214 }
32215
32216 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
32217 of class decls or diagnoses it, whichever is appropriate. */
32218
32219 void
32220 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
32221 tag_types class_key,
32222 bool redundant,
32223 bool def_p)
32224 {
32225 /* Reset the CLASS_KEY associated with this type on mismatch.
32226 This is an optimization that lets the diagnostic code skip
32227 over classes that use the same class-key in all declarations. */
32228 if (def_class_key != class_key)
32229 def_class_key = none_type;
32230
32231 /* Set IDXDEF to the index of the vector corresponding to
32232 the definition. */
32233 if (def_p)
32234 idxdef = locvec.length ();
32235
32236 /* Append a record of this declaration to the vector. */
32237 class_key_loc_t ckl (current_function_decl, input_location, class_key,
32238 redundant);
32239 locvec.safe_push (ckl);
32240
32241 if (idxdef == UINT_MAX)
32242 return;
32243
32244 /* As a space optimization diagnose declarations of a class
32245 whose definition has been seen and purge the LOCVEC of
32246 all entries except the definition. */
32247 diag_mismatched_tags (type_decl);
32248 if (idxdef)
32249 {
32250 class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
32251 locvec.release ();
32252 locvec.reserve (2);
32253 locvec.safe_push (ent);
32254 idxdef = 0;
32255 }
32256 else
32257 /* Pop the entry pushed above for this declaration. */
32258 locvec.pop ();
32259 }
32260
32261 /* Issues -Wmismatched-tags for a single class. */
32262
32263 void
32264 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
32265 {
32266 if (!warn_mismatched_tags)
32267 return;
32268
32269 /* Number of uses of the class. */
32270 const unsigned ndecls = locvec.length ();
32271
32272 /* The class (or template) declaration guiding the decisions about
32273 the diagnostic. For ordinary classes it's the same as THIS. For
32274 uses of instantiations of templates other than their declarations
32275 it points to the record for the declaration of the corresponding
32276 primary template or partial specialization. */
32277 class_decl_loc_t *cdlguide = this;
32278
32279 tree type = TREE_TYPE (type_decl);
32280 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type))
32281 {
32282 /* For implicit instantiations of a primary template look up
32283 the primary or partial specialization and use it as
32284 the expected class-key rather than using the class-key of
32285 the first reference to the instantiation. The primary must
32286 be (and inevitably is) at index zero. */
32287 tree spec = specialization_of (type);
32288 cdlguide = class2loc.get (spec);
32289 gcc_assert (cdlguide != NULL);
32290 }
32291 else
32292 {
32293 /* Skip declarations that consistently use the same class-key. */
32294 if (def_class_key != none_type)
32295 return;
32296 }
32297
32298 /* Set if a definition for the class has been seen. */
32299 const bool def_p = cdlguide->def_p ();
32300
32301 /* The index of the declaration whose class-key this declaration
32302 is expected to match. It's either the class-key of the class
32303 definition if one exists or the first declaration otherwise. */
32304 const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
32305
32306 /* The class-key the class is expected to be declared with: it's
32307 either the key used in its definition or the first declaration
32308 if no definition has been provided.
32309 For implicit instantiations of a primary template it's
32310 the class-key used to declare the primary with. The primary
32311 must be at index zero. */
32312 const tag_types xpect_key = cdlguide->class_key (idxguide);
32313
32314 unsigned idx = 0;
32315 /* Advance IDX to the first declaration that either is not
32316 a definition or that doesn't match the first declaration
32317 if no definition is provided. */
32318 while (class_key (idx) == xpect_key)
32319 if (++idx == ndecls)
32320 return;
32321
32322 /* Save the current function before changing it below. */
32323 tree save_func = current_function_decl;
32324 /* Set the function declaration to print in diagnostic context. */
32325 current_function_decl = function (idx);
32326
32327 const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
32328 const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
32329
32330 location_t loc = location (idx);
32331 bool key_redundant_p = key_redundant (idx);
32332 auto_diagnostic_group d;
32333 /* Issue a warning for the first mismatched declaration.
32334 Avoid using "%#qT" since the class-key for the same type will
32335 be the same regardless of which one was used in the declaraion. */
32336 if (warning_at (loc, OPT_Wmismatched_tags,
32337 "%qT declared with a mismatched class-key %qs",
32338 type_decl, xmatchkstr))
32339 {
32340 /* Suggest how to avoid the warning for each instance since
32341 the guidance may be different depending on context. */
32342 inform (loc,
32343 (key_redundant_p
32344 ? G_("remove the class-key or replace it with %qs")
32345 : G_("replace the class-key with %qs")),
32346 xpectkstr);
32347
32348 /* Also point to the first declaration or definition that guided
32349 the decision to issue the warning above. */
32350 inform (cdlguide->location (idxguide),
32351 (def_p
32352 ? G_("%qT defined as %qs here")
32353 : G_("%qT first declared as %qs here")),
32354 type_decl, xpectkstr);
32355 }
32356
32357 /* Issue warnings for the remaining inconsistent declarations. */
32358 for (unsigned i = idx + 1; i != ndecls; ++i)
32359 {
32360 tag_types clskey = class_key (i);
32361 /* Skip over the declarations that match either the definition
32362 if one was provided or the first declaration. */
32363 if (clskey == xpect_key)
32364 continue;
32365
32366 loc = location (i);
32367 key_redundant_p = key_redundant (i);
32368 /* Set the function declaration to print in diagnostic context. */
32369 current_function_decl = function (i);
32370 if (warning_at (loc, OPT_Wmismatched_tags,
32371 "%qT declared with a mismatched class-key %qs",
32372 type_decl, xmatchkstr))
32373 /* Suggest how to avoid the warning for each instance since
32374 the guidance may be different depending on context. */
32375 inform (loc,
32376 (key_redundant_p
32377 ? G_("remove the class-key or replace it with %qs")
32378 : G_("replace the class-key with %qs")),
32379 xpectkstr);
32380 }
32381
32382 /* Restore the current function in case it was replaced above. */
32383 current_function_decl = save_func;
32384 }
32385
32386 /* Issues -Wmismatched-tags for all classes. Called at the end
32387 of processing a translation unit, after declarations of all class
32388 types and their uses have been recorded. */
32389
32390 void
32391 class_decl_loc_t::diag_mismatched_tags ()
32392 {
32393 /* CLASS2LOC should be empty if both -Wmismatched-tags and
32394 -Wredundant-tags are disabled. */
32395 gcc_assert (warn_mismatched_tags
32396 || warn_redundant_tags
32397 || class2loc.is_empty ());
32398
32399 /* Save the current function before changing on return. It should
32400 be null at this point. */
32401 temp_override<tree> cleanup (current_function_decl);
32402
32403 if (warn_mismatched_tags)
32404 {
32405 /* Iterate over the collected class/struct/template declarations. */
32406 typedef class_to_loc_map_t::iterator iter_t;
32407 for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
32408 {
32409 tree type_decl = (*it).first;
32410 class_decl_loc_t &recloc = (*it).second;
32411 recloc.diag_mismatched_tags (type_decl);
32412 }
32413 }
32414
32415 class2loc.empty ();
32416 }
32417
32418 /* Issue an error message if DECL is redeclared with different
32419 access than its original declaration [class.access.spec/3].
32420 This applies to nested classes, nested class templates and
32421 enumerations [class.mem/1]. */
32422
32423 static void
32424 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
32425 {
32426 if (!decl
32427 || (!CLASS_TYPE_P (TREE_TYPE (decl))
32428 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
32429 return;
32430
32431 if ((TREE_PRIVATE (decl)
32432 != (current_access_specifier == access_private_node))
32433 || (TREE_PROTECTED (decl)
32434 != (current_access_specifier == access_protected_node)))
32435 error_at (location, "%qD redeclared with different access", decl);
32436 }
32437
32438 /* Look for the `template' keyword, as a syntactic disambiguator.
32439 Return TRUE iff it is present, in which case it will be
32440 consumed. */
32441
32442 static bool
32443 cp_parser_optional_template_keyword (cp_parser *parser)
32444 {
32445 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
32446 {
32447 /* In C++98 the `template' keyword can only be used within templates;
32448 outside templates the parser can always figure out what is a
32449 template and what is not. In C++11, per the resolution of DR 468,
32450 `template' is allowed in cases where it is not strictly necessary. */
32451 if (!processing_template_decl
32452 && pedantic && cxx_dialect == cxx98)
32453 {
32454 cp_token *token = cp_lexer_peek_token (parser->lexer);
32455 pedwarn (token->location, OPT_Wpedantic,
32456 "in C++98 %<template%> (as a disambiguator) is only "
32457 "allowed within templates");
32458 /* If this part of the token stream is rescanned, the same
32459 error message would be generated. So, we purge the token
32460 from the stream. */
32461 cp_lexer_purge_token (parser->lexer);
32462 return false;
32463 }
32464 else
32465 {
32466 /* Consume the `template' keyword. */
32467 cp_lexer_consume_token (parser->lexer);
32468 return true;
32469 }
32470 }
32471 return false;
32472 }
32473
32474 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
32475 set PARSER->SCOPE, and perform other related actions. */
32476
32477 static void
32478 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
32479 {
32480 struct tree_check *check_value;
32481
32482 /* Get the stored value. */
32483 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
32484 /* Set the scope from the stored value. */
32485 parser->scope = saved_checks_value (check_value);
32486 parser->qualifying_scope = check_value->qualifying_scope;
32487 parser->object_scope = NULL_TREE;
32488 }
32489
32490 /* Consume tokens up through a non-nested END token. Returns TRUE if we
32491 encounter the end of a block before what we were looking for. */
32492
32493 static bool
32494 cp_parser_cache_group (cp_parser *parser,
32495 enum cpp_ttype end,
32496 unsigned depth)
32497 {
32498 while (true)
32499 {
32500 cp_token *token = cp_lexer_peek_token (parser->lexer);
32501
32502 /* Abort a parenthesized expression if we encounter a semicolon. */
32503 if ((end == CPP_CLOSE_PAREN || depth == 0)
32504 && token->type == CPP_SEMICOLON)
32505 return true;
32506 /* If we've reached the end of the file, stop. */
32507 if (token->type == CPP_EOF
32508 || (end != CPP_PRAGMA_EOL
32509 && token->type == CPP_PRAGMA_EOL))
32510 return true;
32511 if (token->type == CPP_CLOSE_BRACE && depth == 0)
32512 /* We've hit the end of an enclosing block, so there's been some
32513 kind of syntax error. */
32514 return true;
32515
32516 /* Consume the token. */
32517 cp_lexer_consume_token (parser->lexer);
32518 /* See if it starts a new group. */
32519 if (token->type == CPP_OPEN_BRACE)
32520 {
32521 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
32522 /* In theory this should probably check end == '}', but
32523 cp_parser_save_member_function_body needs it to exit
32524 after either '}' or ')' when called with ')'. */
32525 if (depth == 0)
32526 return false;
32527 }
32528 else if (token->type == CPP_OPEN_PAREN)
32529 {
32530 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
32531 if (depth == 0 && end == CPP_CLOSE_PAREN)
32532 return false;
32533 }
32534 else if (token->type == CPP_PRAGMA)
32535 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
32536 else if (token->type == end)
32537 return false;
32538 }
32539 }
32540
32541 /* Like above, for caching a default argument or NSDMI. Both of these are
32542 terminated by a non-nested comma, but it can be unclear whether or not a
32543 comma is nested in a template argument list unless we do more parsing.
32544 In order to handle this ambiguity, when we encounter a ',' after a '<'
32545 we try to parse what follows as a parameter-declaration-list (in the
32546 case of a default argument) or a member-declarator (in the case of an
32547 NSDMI). If that succeeds, then we stop caching. */
32548
32549 static tree
32550 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
32551 {
32552 unsigned depth = 0;
32553 int maybe_template_id = 0;
32554 cp_token *first_token;
32555 cp_token *token;
32556 tree default_argument;
32557
32558 /* Add tokens until we have processed the entire default
32559 argument. We add the range [first_token, token). */
32560 first_token = cp_lexer_peek_token (parser->lexer);
32561 if (first_token->type == CPP_OPEN_BRACE)
32562 {
32563 /* For list-initialization, this is straightforward. */
32564 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
32565 token = cp_lexer_peek_token (parser->lexer);
32566 }
32567 else while (true)
32568 {
32569 bool done = false;
32570
32571 /* Peek at the next token. */
32572 token = cp_lexer_peek_token (parser->lexer);
32573 /* What we do depends on what token we have. */
32574 switch (token->type)
32575 {
32576 /* In valid code, a default argument must be
32577 immediately followed by a `,' `)', or `...'. */
32578 case CPP_COMMA:
32579 if (depth == 0 && maybe_template_id)
32580 {
32581 /* If we've seen a '<', we might be in a
32582 template-argument-list. Until Core issue 325 is
32583 resolved, we don't know how this situation ought
32584 to be handled, so try to DTRT. We check whether
32585 what comes after the comma is a valid parameter
32586 declaration list. If it is, then the comma ends
32587 the default argument; otherwise the default
32588 argument continues. */
32589 bool error = false;
32590 cp_token *peek;
32591
32592 /* Set ITALP so cp_parser_parameter_declaration_list
32593 doesn't decide to commit to this parse. */
32594 bool saved_italp = parser->in_template_argument_list_p;
32595 parser->in_template_argument_list_p = true;
32596
32597 cp_parser_parse_tentatively (parser);
32598
32599 if (nsdmi)
32600 {
32601 /* Parse declarators until we reach a non-comma or
32602 somthing that cannot be an initializer.
32603 Just checking whether we're looking at a single
32604 declarator is insufficient. Consider:
32605 int var = tuple<T,U>::x;
32606 The template parameter 'U' looks exactly like a
32607 declarator. */
32608 do
32609 {
32610 int ctor_dtor_or_conv_p;
32611 cp_lexer_consume_token (parser->lexer);
32612 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
32613 CP_PARSER_FLAGS_NONE,
32614 &ctor_dtor_or_conv_p,
32615 /*parenthesized_p=*/NULL,
32616 /*member_p=*/true,
32617 /*friend_p=*/false,
32618 /*static_p=*/false);
32619 peek = cp_lexer_peek_token (parser->lexer);
32620 if (cp_parser_error_occurred (parser))
32621 break;
32622 }
32623 while (peek->type == CPP_COMMA);
32624 /* If we met an '=' or ';' then the original comma
32625 was the end of the NSDMI. Otherwise assume
32626 we're still in the NSDMI. */
32627 error = (peek->type != CPP_EQ
32628 && peek->type != CPP_SEMICOLON);
32629 }
32630 else
32631 {
32632 cp_lexer_consume_token (parser->lexer);
32633 begin_scope (sk_function_parms, NULL_TREE);
32634 tree t = cp_parser_parameter_declaration_list
32635 (parser, CP_PARSER_FLAGS_NONE);
32636 if (t == error_mark_node)
32637 error = true;
32638 pop_bindings_and_leave_scope ();
32639 }
32640 if (!cp_parser_error_occurred (parser) && !error)
32641 done = true;
32642 cp_parser_abort_tentative_parse (parser);
32643
32644 parser->in_template_argument_list_p = saved_italp;
32645 break;
32646 }
32647 /* FALLTHRU */
32648 case CPP_CLOSE_PAREN:
32649 case CPP_ELLIPSIS:
32650 /* If we run into a non-nested `;', `}', or `]',
32651 then the code is invalid -- but the default
32652 argument is certainly over. */
32653 case CPP_SEMICOLON:
32654 case CPP_CLOSE_BRACE:
32655 case CPP_CLOSE_SQUARE:
32656 if (depth == 0
32657 /* Handle correctly int n = sizeof ... ( p ); */
32658 && token->type != CPP_ELLIPSIS)
32659 done = true;
32660 /* Update DEPTH, if necessary. */
32661 else if (token->type == CPP_CLOSE_PAREN
32662 || token->type == CPP_CLOSE_BRACE
32663 || token->type == CPP_CLOSE_SQUARE)
32664 --depth;
32665 break;
32666
32667 case CPP_OPEN_PAREN:
32668 case CPP_OPEN_SQUARE:
32669 case CPP_OPEN_BRACE:
32670 ++depth;
32671 break;
32672
32673 case CPP_LESS:
32674 if (depth == 0)
32675 /* This might be the comparison operator, or it might
32676 start a template argument list. */
32677 ++maybe_template_id;
32678 break;
32679
32680 case CPP_RSHIFT:
32681 if (cxx_dialect == cxx98)
32682 break;
32683 /* Fall through for C++0x, which treats the `>>'
32684 operator like two `>' tokens in certain
32685 cases. */
32686 gcc_fallthrough ();
32687
32688 case CPP_GREATER:
32689 if (depth == 0)
32690 {
32691 /* This might be an operator, or it might close a
32692 template argument list. But if a previous '<'
32693 started a template argument list, this will have
32694 closed it, so we can't be in one anymore. */
32695 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
32696 if (maybe_template_id < 0)
32697 maybe_template_id = 0;
32698 }
32699 break;
32700
32701 /* If we run out of tokens, issue an error message. */
32702 case CPP_EOF:
32703 case CPP_PRAGMA_EOL:
32704 error_at (token->location, "file ends in default argument");
32705 return error_mark_node;
32706
32707 case CPP_NAME:
32708 case CPP_SCOPE:
32709 /* In these cases, we should look for template-ids.
32710 For example, if the default argument is
32711 `X<int, double>()', we need to do name lookup to
32712 figure out whether or not `X' is a template; if
32713 so, the `,' does not end the default argument.
32714
32715 That is not yet done. */
32716 break;
32717
32718 default:
32719 break;
32720 }
32721
32722 /* If we've reached the end, stop. */
32723 if (done)
32724 break;
32725
32726 /* Add the token to the token block. */
32727 token = cp_lexer_consume_token (parser->lexer);
32728 }
32729
32730 /* Create a DEFERRED_PARSE to represent the unparsed default
32731 argument. */
32732 default_argument = make_node (DEFERRED_PARSE);
32733 DEFPARSE_TOKENS (default_argument)
32734 = cp_token_cache_new (first_token, token);
32735 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
32736
32737 return default_argument;
32738 }
32739
32740 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
32741
32742 location_t
32743 defparse_location (tree default_argument)
32744 {
32745 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
32746 location_t start = tokens->first->location;
32747 location_t end = tokens->last->location;
32748 return make_location (start, start, end);
32749 }
32750
32751 /* Begin parsing tentatively. We always save tokens while parsing
32752 tentatively so that if the tentative parsing fails we can restore the
32753 tokens. */
32754
32755 static void
32756 cp_parser_parse_tentatively (cp_parser* parser)
32757 {
32758 /* Enter a new parsing context. */
32759 parser->context = cp_parser_context_new (parser->context);
32760 /* Begin saving tokens. */
32761 cp_lexer_save_tokens (parser->lexer);
32762 /* In order to avoid repetitive access control error messages,
32763 access checks are queued up until we are no longer parsing
32764 tentatively. */
32765 push_deferring_access_checks (dk_deferred);
32766 }
32767
32768 /* Commit to the currently active tentative parse. */
32769
32770 static void
32771 cp_parser_commit_to_tentative_parse (cp_parser* parser)
32772 {
32773 cp_parser_context *context;
32774 cp_lexer *lexer;
32775
32776 /* Mark all of the levels as committed. */
32777 lexer = parser->lexer;
32778 for (context = parser->context; context->next; context = context->next)
32779 {
32780 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
32781 break;
32782 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
32783 while (!cp_lexer_saving_tokens (lexer))
32784 lexer = lexer->next;
32785 cp_lexer_commit_tokens (lexer);
32786 }
32787 }
32788
32789 /* Commit to the topmost currently active tentative parse.
32790
32791 Note that this function shouldn't be called when there are
32792 irreversible side-effects while in a tentative state. For
32793 example, we shouldn't create a permanent entry in the symbol
32794 table, or issue an error message that might not apply if the
32795 tentative parse is aborted. */
32796
32797 static void
32798 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
32799 {
32800 cp_parser_context *context = parser->context;
32801 cp_lexer *lexer = parser->lexer;
32802
32803 if (context)
32804 {
32805 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
32806 return;
32807 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
32808
32809 while (!cp_lexer_saving_tokens (lexer))
32810 lexer = lexer->next;
32811 cp_lexer_commit_tokens (lexer);
32812 }
32813 }
32814
32815 /* Abort the currently active tentative parse. All consumed tokens
32816 will be rolled back, and no diagnostics will be issued. */
32817
32818 static void
32819 cp_parser_abort_tentative_parse (cp_parser* parser)
32820 {
32821 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
32822 || errorcount > 0);
32823 cp_parser_simulate_error (parser);
32824 /* Now, pretend that we want to see if the construct was
32825 successfully parsed. */
32826 cp_parser_parse_definitely (parser);
32827 }
32828
32829 /* Stop parsing tentatively. If a parse error has occurred, restore the
32830 token stream. Otherwise, commit to the tokens we have consumed.
32831 Returns true if no error occurred; false otherwise. */
32832
32833 static bool
32834 cp_parser_parse_definitely (cp_parser* parser)
32835 {
32836 bool error_occurred;
32837 cp_parser_context *context;
32838
32839 /* Remember whether or not an error occurred, since we are about to
32840 destroy that information. */
32841 error_occurred = cp_parser_error_occurred (parser);
32842 /* Remove the topmost context from the stack. */
32843 context = parser->context;
32844 parser->context = context->next;
32845 /* If no parse errors occurred, commit to the tentative parse. */
32846 if (!error_occurred)
32847 {
32848 /* Commit to the tokens read tentatively, unless that was
32849 already done. */
32850 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
32851 cp_lexer_commit_tokens (parser->lexer);
32852
32853 pop_to_parent_deferring_access_checks ();
32854 }
32855 /* Otherwise, if errors occurred, roll back our state so that things
32856 are just as they were before we began the tentative parse. */
32857 else
32858 {
32859 cp_lexer_rollback_tokens (parser->lexer);
32860 pop_deferring_access_checks ();
32861 }
32862 /* Add the context to the front of the free list. */
32863 context->next = cp_parser_context_free_list;
32864 cp_parser_context_free_list = context;
32865
32866 return !error_occurred;
32867 }
32868
32869 /* Returns true if we are parsing tentatively and are not committed to
32870 this tentative parse. */
32871
32872 static bool
32873 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
32874 {
32875 return (cp_parser_parsing_tentatively (parser)
32876 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
32877 }
32878
32879 /* Returns nonzero iff an error has occurred during the most recent
32880 tentative parse. */
32881
32882 static bool
32883 cp_parser_error_occurred (cp_parser* parser)
32884 {
32885 return (cp_parser_parsing_tentatively (parser)
32886 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
32887 }
32888
32889 /* Returns nonzero if GNU extensions are allowed. */
32890
32891 static bool
32892 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
32893 {
32894 return parser->allow_gnu_extensions_p;
32895 }
32896 \f
32897 /* Objective-C++ Productions */
32898
32899
32900 /* Parse an Objective-C expression, which feeds into a primary-expression
32901 above.
32902
32903 objc-expression:
32904 objc-message-expression
32905 objc-string-literal
32906 objc-encode-expression
32907 objc-protocol-expression
32908 objc-selector-expression
32909
32910 Returns a tree representation of the expression. */
32911
32912 static cp_expr
32913 cp_parser_objc_expression (cp_parser* parser)
32914 {
32915 /* Try to figure out what kind of declaration is present. */
32916 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
32917
32918 switch (kwd->type)
32919 {
32920 case CPP_OPEN_SQUARE:
32921 return cp_parser_objc_message_expression (parser);
32922
32923 case CPP_OBJC_STRING:
32924 kwd = cp_lexer_consume_token (parser->lexer);
32925 return objc_build_string_object (kwd->u.value);
32926
32927 case CPP_KEYWORD:
32928 switch (kwd->keyword)
32929 {
32930 case RID_AT_ENCODE:
32931 return cp_parser_objc_encode_expression (parser);
32932
32933 case RID_AT_PROTOCOL:
32934 return cp_parser_objc_protocol_expression (parser);
32935
32936 case RID_AT_SELECTOR:
32937 return cp_parser_objc_selector_expression (parser);
32938
32939 default:
32940 break;
32941 }
32942 /* FALLTHRU */
32943 default:
32944 error_at (kwd->location,
32945 "misplaced %<@%D%> Objective-C++ construct",
32946 kwd->u.value);
32947 cp_parser_skip_to_end_of_block_or_statement (parser);
32948 }
32949
32950 return error_mark_node;
32951 }
32952
32953 /* Parse an Objective-C message expression.
32954
32955 objc-message-expression:
32956 [ objc-message-receiver objc-message-args ]
32957
32958 Returns a representation of an Objective-C message. */
32959
32960 static tree
32961 cp_parser_objc_message_expression (cp_parser* parser)
32962 {
32963 tree receiver, messageargs;
32964
32965 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
32966 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
32967 receiver = cp_parser_objc_message_receiver (parser);
32968 messageargs = cp_parser_objc_message_args (parser);
32969 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
32970 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32971
32972 tree result = objc_build_message_expr (receiver, messageargs);
32973
32974 /* Construct a location e.g.
32975 [self func1:5]
32976 ^~~~~~~~~~~~~~
32977 ranging from the '[' to the ']', with the caret at the start. */
32978 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
32979 protected_set_expr_location (result, combined_loc);
32980
32981 return result;
32982 }
32983
32984 /* Parse an objc-message-receiver.
32985
32986 objc-message-receiver:
32987 expression
32988 simple-type-specifier
32989
32990 Returns a representation of the type or expression. */
32991
32992 static tree
32993 cp_parser_objc_message_receiver (cp_parser* parser)
32994 {
32995 tree rcv;
32996
32997 /* An Objective-C message receiver may be either (1) a type
32998 or (2) an expression. */
32999 cp_parser_parse_tentatively (parser);
33000 rcv = cp_parser_expression (parser);
33001
33002 /* If that worked out, fine. */
33003 if (cp_parser_parse_definitely (parser))
33004 return rcv;
33005
33006 cp_parser_parse_tentatively (parser);
33007 rcv = cp_parser_simple_type_specifier (parser,
33008 /*decl_specs=*/NULL,
33009 CP_PARSER_FLAGS_NONE);
33010
33011 if (cp_parser_parse_definitely (parser))
33012 return objc_get_class_reference (rcv);
33013
33014 cp_parser_error (parser, "objective-c++ message receiver expected");
33015 return error_mark_node;
33016 }
33017
33018 /* Parse the arguments and selectors comprising an Objective-C message.
33019
33020 objc-message-args:
33021 objc-selector
33022 objc-selector-args
33023 objc-selector-args , objc-comma-args
33024
33025 objc-selector-args:
33026 objc-selector [opt] : assignment-expression
33027 objc-selector-args objc-selector [opt] : assignment-expression
33028
33029 objc-comma-args:
33030 assignment-expression
33031 objc-comma-args , assignment-expression
33032
33033 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
33034 selector arguments and TREE_VALUE containing a list of comma
33035 arguments. */
33036
33037 static tree
33038 cp_parser_objc_message_args (cp_parser* parser)
33039 {
33040 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
33041 bool maybe_unary_selector_p = true;
33042 cp_token *token = cp_lexer_peek_token (parser->lexer);
33043
33044 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
33045 {
33046 tree selector = NULL_TREE, arg;
33047
33048 if (token->type != CPP_COLON)
33049 selector = cp_parser_objc_selector (parser);
33050
33051 /* Detect if we have a unary selector. */
33052 if (maybe_unary_selector_p
33053 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
33054 return build_tree_list (selector, NULL_TREE);
33055
33056 maybe_unary_selector_p = false;
33057 cp_parser_require (parser, CPP_COLON, RT_COLON);
33058 arg = cp_parser_assignment_expression (parser);
33059
33060 sel_args
33061 = chainon (sel_args,
33062 build_tree_list (selector, arg));
33063
33064 token = cp_lexer_peek_token (parser->lexer);
33065 }
33066
33067 /* Handle non-selector arguments, if any. */
33068 while (token->type == CPP_COMMA)
33069 {
33070 tree arg;
33071
33072 cp_lexer_consume_token (parser->lexer);
33073 arg = cp_parser_assignment_expression (parser);
33074
33075 addl_args
33076 = chainon (addl_args,
33077 build_tree_list (NULL_TREE, arg));
33078
33079 token = cp_lexer_peek_token (parser->lexer);
33080 }
33081
33082 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
33083 {
33084 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
33085 return build_tree_list (error_mark_node, error_mark_node);
33086 }
33087
33088 return build_tree_list (sel_args, addl_args);
33089 }
33090
33091 /* Parse an Objective-C encode expression.
33092
33093 objc-encode-expression:
33094 @encode objc-typename
33095
33096 Returns an encoded representation of the type argument. */
33097
33098 static cp_expr
33099 cp_parser_objc_encode_expression (cp_parser* parser)
33100 {
33101 tree type;
33102 cp_token *token;
33103 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
33104
33105 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
33106 matching_parens parens;
33107 parens.require_open (parser);
33108 token = cp_lexer_peek_token (parser->lexer);
33109 type = complete_type (cp_parser_type_id (parser));
33110 parens.require_close (parser);
33111
33112 if (!type)
33113 {
33114 error_at (token->location,
33115 "%<@encode%> must specify a type as an argument");
33116 return error_mark_node;
33117 }
33118
33119 /* This happens if we find @encode(T) (where T is a template
33120 typename or something dependent on a template typename) when
33121 parsing a template. In that case, we can't compile it
33122 immediately, but we rather create an AT_ENCODE_EXPR which will
33123 need to be instantiated when the template is used.
33124 */
33125 if (dependent_type_p (type))
33126 {
33127 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
33128 TREE_READONLY (value) = 1;
33129 return value;
33130 }
33131
33132
33133 /* Build a location of the form:
33134 @encode(int)
33135 ^~~~~~~~~~~~
33136 with caret==start at the @ token, finishing at the close paren. */
33137 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
33138
33139 return cp_expr (objc_build_encode_expr (type), combined_loc);
33140 }
33141
33142 /* Parse an Objective-C @defs expression. */
33143
33144 static tree
33145 cp_parser_objc_defs_expression (cp_parser *parser)
33146 {
33147 tree name;
33148
33149 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
33150 matching_parens parens;
33151 parens.require_open (parser);
33152 name = cp_parser_identifier (parser);
33153 parens.require_close (parser);
33154
33155 return objc_get_class_ivars (name);
33156 }
33157
33158 /* Parse an Objective-C protocol expression.
33159
33160 objc-protocol-expression:
33161 @protocol ( identifier )
33162
33163 Returns a representation of the protocol expression. */
33164
33165 static tree
33166 cp_parser_objc_protocol_expression (cp_parser* parser)
33167 {
33168 tree proto;
33169 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
33170
33171 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
33172 matching_parens parens;
33173 parens.require_open (parser);
33174 proto = cp_parser_identifier (parser);
33175 parens.require_close (parser);
33176
33177 /* Build a location of the form:
33178 @protocol(prot)
33179 ^~~~~~~~~~~~~~~
33180 with caret==start at the @ token, finishing at the close paren. */
33181 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
33182 tree result = objc_build_protocol_expr (proto);
33183 protected_set_expr_location (result, combined_loc);
33184 return result;
33185 }
33186
33187 /* Parse an Objective-C selector expression.
33188
33189 objc-selector-expression:
33190 @selector ( objc-method-signature )
33191
33192 objc-method-signature:
33193 objc-selector
33194 objc-selector-seq
33195
33196 objc-selector-seq:
33197 objc-selector :
33198 objc-selector-seq objc-selector :
33199
33200 Returns a representation of the method selector. */
33201
33202 static tree
33203 cp_parser_objc_selector_expression (cp_parser* parser)
33204 {
33205 tree sel_seq = NULL_TREE;
33206 bool maybe_unary_selector_p = true;
33207 cp_token *token;
33208 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33209
33210 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
33211 matching_parens parens;
33212 parens.require_open (parser);
33213 token = cp_lexer_peek_token (parser->lexer);
33214
33215 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
33216 || token->type == CPP_SCOPE)
33217 {
33218 tree selector = NULL_TREE;
33219
33220 if (token->type != CPP_COLON
33221 || token->type == CPP_SCOPE)
33222 selector = cp_parser_objc_selector (parser);
33223
33224 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
33225 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
33226 {
33227 /* Detect if we have a unary selector. */
33228 if (maybe_unary_selector_p)
33229 {
33230 sel_seq = selector;
33231 goto finish_selector;
33232 }
33233 else
33234 {
33235 cp_parser_error (parser, "expected %<:%>");
33236 }
33237 }
33238 maybe_unary_selector_p = false;
33239 token = cp_lexer_consume_token (parser->lexer);
33240
33241 if (token->type == CPP_SCOPE)
33242 {
33243 sel_seq
33244 = chainon (sel_seq,
33245 build_tree_list (selector, NULL_TREE));
33246 sel_seq
33247 = chainon (sel_seq,
33248 build_tree_list (NULL_TREE, NULL_TREE));
33249 }
33250 else
33251 sel_seq
33252 = chainon (sel_seq,
33253 build_tree_list (selector, NULL_TREE));
33254
33255 token = cp_lexer_peek_token (parser->lexer);
33256 }
33257
33258 finish_selector:
33259 parens.require_close (parser);
33260
33261
33262 /* Build a location of the form:
33263 @selector(func)
33264 ^~~~~~~~~~~~~~~
33265 with caret==start at the @ token, finishing at the close paren. */
33266 location_t combined_loc = make_location (loc, loc, parser->lexer);
33267 tree result = objc_build_selector_expr (combined_loc, sel_seq);
33268 /* TODO: objc_build_selector_expr doesn't always honor the location. */
33269 protected_set_expr_location (result, combined_loc);
33270 return result;
33271 }
33272
33273 /* Parse a list of identifiers.
33274
33275 objc-identifier-list:
33276 identifier
33277 objc-identifier-list , identifier
33278
33279 Returns a TREE_LIST of identifier nodes. */
33280
33281 static tree
33282 cp_parser_objc_identifier_list (cp_parser* parser)
33283 {
33284 tree identifier;
33285 tree list;
33286 cp_token *sep;
33287
33288 identifier = cp_parser_identifier (parser);
33289 if (identifier == error_mark_node)
33290 return error_mark_node;
33291
33292 list = build_tree_list (NULL_TREE, identifier);
33293 sep = cp_lexer_peek_token (parser->lexer);
33294
33295 while (sep->type == CPP_COMMA)
33296 {
33297 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
33298 identifier = cp_parser_identifier (parser);
33299 if (identifier == error_mark_node)
33300 return list;
33301
33302 list = chainon (list, build_tree_list (NULL_TREE,
33303 identifier));
33304 sep = cp_lexer_peek_token (parser->lexer);
33305 }
33306
33307 return list;
33308 }
33309
33310 /* Parse an Objective-C alias declaration.
33311
33312 objc-alias-declaration:
33313 @compatibility_alias identifier identifier ;
33314
33315 This function registers the alias mapping with the Objective-C front end.
33316 It returns nothing. */
33317
33318 static void
33319 cp_parser_objc_alias_declaration (cp_parser* parser)
33320 {
33321 tree alias, orig;
33322
33323 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
33324 alias = cp_parser_identifier (parser);
33325 orig = cp_parser_identifier (parser);
33326 objc_declare_alias (alias, orig);
33327 cp_parser_consume_semicolon_at_end_of_statement (parser);
33328 }
33329
33330 /* Parse an Objective-C class forward-declaration.
33331
33332 objc-class-declaration:
33333 @class objc-identifier-list ;
33334
33335 The function registers the forward declarations with the Objective-C
33336 front end. It returns nothing. */
33337
33338 static void
33339 cp_parser_objc_class_declaration (cp_parser* parser)
33340 {
33341 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
33342 while (true)
33343 {
33344 tree id;
33345
33346 id = cp_parser_identifier (parser);
33347 if (id == error_mark_node)
33348 break;
33349
33350 objc_declare_class (id);
33351
33352 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33353 cp_lexer_consume_token (parser->lexer);
33354 else
33355 break;
33356 }
33357 cp_parser_consume_semicolon_at_end_of_statement (parser);
33358 }
33359
33360 /* Parse a list of Objective-C protocol references.
33361
33362 objc-protocol-refs-opt:
33363 objc-protocol-refs [opt]
33364
33365 objc-protocol-refs:
33366 < objc-identifier-list >
33367
33368 Returns a TREE_LIST of identifiers, if any. */
33369
33370 static tree
33371 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
33372 {
33373 tree protorefs = NULL_TREE;
33374
33375 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
33376 {
33377 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
33378 protorefs = cp_parser_objc_identifier_list (parser);
33379 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
33380 }
33381
33382 return protorefs;
33383 }
33384
33385 /* Parse a Objective-C visibility specification. */
33386
33387 static void
33388 cp_parser_objc_visibility_spec (cp_parser* parser)
33389 {
33390 cp_token *vis = cp_lexer_peek_token (parser->lexer);
33391
33392 switch (vis->keyword)
33393 {
33394 case RID_AT_PRIVATE:
33395 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
33396 break;
33397 case RID_AT_PROTECTED:
33398 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
33399 break;
33400 case RID_AT_PUBLIC:
33401 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
33402 break;
33403 case RID_AT_PACKAGE:
33404 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
33405 break;
33406 default:
33407 return;
33408 }
33409
33410 /* Eat '@private'/'@protected'/'@public'. */
33411 cp_lexer_consume_token (parser->lexer);
33412 }
33413
33414 /* Parse an Objective-C method type. Return 'true' if it is a class
33415 (+) method, and 'false' if it is an instance (-) method. */
33416
33417 static inline bool
33418 cp_parser_objc_method_type (cp_parser* parser)
33419 {
33420 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
33421 return true;
33422 else
33423 return false;
33424 }
33425
33426 /* Parse an Objective-C protocol qualifier. */
33427
33428 static tree
33429 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
33430 {
33431 tree quals = NULL_TREE, node;
33432 cp_token *token = cp_lexer_peek_token (parser->lexer);
33433
33434 node = token->u.value;
33435
33436 while (node && identifier_p (node)
33437 && (node == ridpointers [(int) RID_IN]
33438 || node == ridpointers [(int) RID_OUT]
33439 || node == ridpointers [(int) RID_INOUT]
33440 || node == ridpointers [(int) RID_BYCOPY]
33441 || node == ridpointers [(int) RID_BYREF]
33442 || node == ridpointers [(int) RID_ONEWAY]))
33443 {
33444 quals = tree_cons (NULL_TREE, node, quals);
33445 cp_lexer_consume_token (parser->lexer);
33446 token = cp_lexer_peek_token (parser->lexer);
33447 node = token->u.value;
33448 }
33449
33450 return quals;
33451 }
33452
33453 /* Parse an Objective-C typename. */
33454
33455 static tree
33456 cp_parser_objc_typename (cp_parser* parser)
33457 {
33458 tree type_name = NULL_TREE;
33459
33460 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33461 {
33462 tree proto_quals, cp_type = NULL_TREE;
33463
33464 matching_parens parens;
33465 parens.consume_open (parser); /* Eat '('. */
33466 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
33467
33468 /* An ObjC type name may consist of just protocol qualifiers, in which
33469 case the type shall default to 'id'. */
33470 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
33471 {
33472 cp_type = cp_parser_type_id (parser);
33473
33474 /* If the type could not be parsed, an error has already
33475 been produced. For error recovery, behave as if it had
33476 not been specified, which will use the default type
33477 'id'. */
33478 if (cp_type == error_mark_node)
33479 {
33480 cp_type = NULL_TREE;
33481 /* We need to skip to the closing parenthesis as
33482 cp_parser_type_id() does not seem to do it for
33483 us. */
33484 cp_parser_skip_to_closing_parenthesis (parser,
33485 /*recovering=*/true,
33486 /*or_comma=*/false,
33487 /*consume_paren=*/false);
33488 }
33489 }
33490
33491 parens.require_close (parser);
33492 type_name = build_tree_list (proto_quals, cp_type);
33493 }
33494
33495 return type_name;
33496 }
33497
33498 /* Check to see if TYPE refers to an Objective-C selector name. */
33499
33500 static bool
33501 cp_parser_objc_selector_p (enum cpp_ttype type)
33502 {
33503 return (type == CPP_NAME || type == CPP_KEYWORD
33504 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
33505 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
33506 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
33507 || type == CPP_XOR || type == CPP_XOR_EQ);
33508 }
33509
33510 /* Parse an Objective-C selector. */
33511
33512 static tree
33513 cp_parser_objc_selector (cp_parser* parser)
33514 {
33515 cp_token *token = cp_lexer_consume_token (parser->lexer);
33516
33517 if (!cp_parser_objc_selector_p (token->type))
33518 {
33519 error_at (token->location, "invalid Objective-C++ selector name");
33520 return error_mark_node;
33521 }
33522
33523 /* C++ operator names are allowed to appear in ObjC selectors. */
33524 switch (token->type)
33525 {
33526 case CPP_AND_AND: return get_identifier ("and");
33527 case CPP_AND_EQ: return get_identifier ("and_eq");
33528 case CPP_AND: return get_identifier ("bitand");
33529 case CPP_OR: return get_identifier ("bitor");
33530 case CPP_COMPL: return get_identifier ("compl");
33531 case CPP_NOT: return get_identifier ("not");
33532 case CPP_NOT_EQ: return get_identifier ("not_eq");
33533 case CPP_OR_OR: return get_identifier ("or");
33534 case CPP_OR_EQ: return get_identifier ("or_eq");
33535 case CPP_XOR: return get_identifier ("xor");
33536 case CPP_XOR_EQ: return get_identifier ("xor_eq");
33537 default: return token->u.value;
33538 }
33539 }
33540
33541 /* Parse an Objective-C params list. */
33542
33543 static tree
33544 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
33545 {
33546 tree params = NULL_TREE;
33547 bool maybe_unary_selector_p = true;
33548 cp_token *token = cp_lexer_peek_token (parser->lexer);
33549
33550 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
33551 {
33552 tree selector = NULL_TREE, type_name, identifier;
33553 tree parm_attr = NULL_TREE;
33554
33555 if (token->keyword == RID_ATTRIBUTE)
33556 break;
33557
33558 if (token->type != CPP_COLON)
33559 selector = cp_parser_objc_selector (parser);
33560
33561 /* Detect if we have a unary selector. */
33562 if (maybe_unary_selector_p
33563 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
33564 {
33565 params = selector; /* Might be followed by attributes. */
33566 break;
33567 }
33568
33569 maybe_unary_selector_p = false;
33570 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33571 {
33572 /* Something went quite wrong. There should be a colon
33573 here, but there is not. Stop parsing parameters. */
33574 break;
33575 }
33576 type_name = cp_parser_objc_typename (parser);
33577 /* New ObjC allows attributes on parameters too. */
33578 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
33579 parm_attr = cp_parser_attributes_opt (parser);
33580 identifier = cp_parser_identifier (parser);
33581
33582 params
33583 = chainon (params,
33584 objc_build_keyword_decl (selector,
33585 type_name,
33586 identifier,
33587 parm_attr));
33588
33589 token = cp_lexer_peek_token (parser->lexer);
33590 }
33591
33592 if (params == NULL_TREE)
33593 {
33594 cp_parser_error (parser, "objective-c++ method declaration is expected");
33595 return error_mark_node;
33596 }
33597
33598 /* We allow tail attributes for the method. */
33599 if (token->keyword == RID_ATTRIBUTE)
33600 {
33601 *attributes = cp_parser_attributes_opt (parser);
33602 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
33603 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33604 return params;
33605 cp_parser_error (parser,
33606 "method attributes must be specified at the end");
33607 return error_mark_node;
33608 }
33609
33610 if (params == NULL_TREE)
33611 {
33612 cp_parser_error (parser, "objective-c++ method declaration is expected");
33613 return error_mark_node;
33614 }
33615 return params;
33616 }
33617
33618 /* Parse the non-keyword Objective-C params. */
33619
33620 static tree
33621 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
33622 tree* attributes)
33623 {
33624 tree params = make_node (TREE_LIST);
33625 cp_token *token = cp_lexer_peek_token (parser->lexer);
33626 *ellipsisp = false; /* Initially, assume no ellipsis. */
33627
33628 while (token->type == CPP_COMMA)
33629 {
33630 cp_parameter_declarator *parmdecl;
33631 tree parm;
33632
33633 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
33634 token = cp_lexer_peek_token (parser->lexer);
33635
33636 if (token->type == CPP_ELLIPSIS)
33637 {
33638 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
33639 *ellipsisp = true;
33640 token = cp_lexer_peek_token (parser->lexer);
33641 break;
33642 }
33643
33644 /* TODO: parse attributes for tail parameters. */
33645 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
33646 false, NULL);
33647 parm = grokdeclarator (parmdecl->declarator,
33648 &parmdecl->decl_specifiers,
33649 PARM, /*initialized=*/0,
33650 /*attrlist=*/NULL);
33651
33652 chainon (params, build_tree_list (NULL_TREE, parm));
33653 token = cp_lexer_peek_token (parser->lexer);
33654 }
33655
33656 /* We allow tail attributes for the method. */
33657 if (token->keyword == RID_ATTRIBUTE)
33658 {
33659 if (*attributes == NULL_TREE)
33660 {
33661 *attributes = cp_parser_attributes_opt (parser);
33662 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
33663 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33664 return params;
33665 }
33666 else
33667 /* We have an error, but parse the attributes, so that we can
33668 carry on. */
33669 *attributes = cp_parser_attributes_opt (parser);
33670
33671 cp_parser_error (parser,
33672 "method attributes must be specified at the end");
33673 return error_mark_node;
33674 }
33675
33676 return params;
33677 }
33678
33679 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
33680
33681 static void
33682 cp_parser_objc_interstitial_code (cp_parser* parser)
33683 {
33684 cp_token *token = cp_lexer_peek_token (parser->lexer);
33685
33686 /* If the next token is `extern' and the following token is a string
33687 literal, then we have a linkage specification. */
33688 if (token->keyword == RID_EXTERN
33689 && cp_parser_is_pure_string_literal
33690 (cp_lexer_peek_nth_token (parser->lexer, 2)))
33691 cp_parser_linkage_specification (parser, NULL_TREE);
33692 /* Handle #pragma, if any. */
33693 else if (token->type == CPP_PRAGMA)
33694 cp_parser_pragma (parser, pragma_objc_icode, NULL);
33695 /* Allow stray semicolons. */
33696 else if (token->type == CPP_SEMICOLON)
33697 cp_lexer_consume_token (parser->lexer);
33698 /* Mark methods as optional or required, when building protocols. */
33699 else if (token->keyword == RID_AT_OPTIONAL)
33700 {
33701 cp_lexer_consume_token (parser->lexer);
33702 objc_set_method_opt (true);
33703 }
33704 else if (token->keyword == RID_AT_REQUIRED)
33705 {
33706 cp_lexer_consume_token (parser->lexer);
33707 objc_set_method_opt (false);
33708 }
33709 else if (token->keyword == RID_NAMESPACE)
33710 cp_parser_namespace_definition (parser);
33711 /* Other stray characters must generate errors. */
33712 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
33713 {
33714 cp_lexer_consume_token (parser->lexer);
33715 error ("stray %qs between Objective-C++ methods",
33716 token->type == CPP_OPEN_BRACE ? "{" : "}");
33717 }
33718 /* Finally, try to parse a block-declaration, or a function-definition. */
33719 else
33720 cp_parser_block_declaration (parser, /*statement_p=*/false);
33721 }
33722
33723 /* Parse a method signature. */
33724
33725 static tree
33726 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
33727 {
33728 tree rettype, kwdparms, optparms;
33729 bool ellipsis = false;
33730 bool is_class_method;
33731
33732 is_class_method = cp_parser_objc_method_type (parser);
33733 rettype = cp_parser_objc_typename (parser);
33734 *attributes = NULL_TREE;
33735 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
33736 if (kwdparms == error_mark_node)
33737 return error_mark_node;
33738 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
33739 if (optparms == error_mark_node)
33740 return error_mark_node;
33741
33742 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
33743 }
33744
33745 static bool
33746 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
33747 {
33748 tree tattr;
33749 cp_lexer_save_tokens (parser->lexer);
33750 tattr = cp_parser_attributes_opt (parser);
33751 gcc_assert (tattr) ;
33752
33753 /* If the attributes are followed by a method introducer, this is not allowed.
33754 Dump the attributes and flag the situation. */
33755 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
33756 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33757 return true;
33758
33759 /* Otherwise, the attributes introduce some interstitial code, possibly so
33760 rewind to allow that check. */
33761 cp_lexer_rollback_tokens (parser->lexer);
33762 return false;
33763 }
33764
33765 /* Parse an Objective-C method prototype list. */
33766
33767 static void
33768 cp_parser_objc_method_prototype_list (cp_parser* parser)
33769 {
33770 cp_token *token = cp_lexer_peek_token (parser->lexer);
33771
33772 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
33773 {
33774 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
33775 {
33776 tree attributes, sig;
33777 bool is_class_method;
33778 if (token->type == CPP_PLUS)
33779 is_class_method = true;
33780 else
33781 is_class_method = false;
33782 sig = cp_parser_objc_method_signature (parser, &attributes);
33783 if (sig == error_mark_node)
33784 {
33785 cp_parser_skip_to_end_of_block_or_statement (parser);
33786 token = cp_lexer_peek_token (parser->lexer);
33787 continue;
33788 }
33789 objc_add_method_declaration (is_class_method, sig, attributes);
33790 cp_parser_consume_semicolon_at_end_of_statement (parser);
33791 }
33792 else if (token->keyword == RID_AT_PROPERTY)
33793 cp_parser_objc_at_property_declaration (parser);
33794 else if (token->keyword == RID_ATTRIBUTE
33795 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
33796 warning_at (cp_lexer_peek_token (parser->lexer)->location,
33797 OPT_Wattributes,
33798 "prefix attributes are ignored for methods");
33799 else
33800 /* Allow for interspersed non-ObjC++ code. */
33801 cp_parser_objc_interstitial_code (parser);
33802
33803 token = cp_lexer_peek_token (parser->lexer);
33804 }
33805
33806 if (token->type != CPP_EOF)
33807 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
33808 else
33809 cp_parser_error (parser, "expected %<@end%>");
33810
33811 objc_finish_interface ();
33812 }
33813
33814 /* Parse an Objective-C method definition list. */
33815
33816 static void
33817 cp_parser_objc_method_definition_list (cp_parser* parser)
33818 {
33819 for (;;)
33820 {
33821 cp_token *token = cp_lexer_peek_token (parser->lexer);
33822
33823 if (token->keyword == RID_AT_END)
33824 {
33825 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
33826 break;
33827 }
33828 else if (token->type == CPP_EOF)
33829 {
33830 cp_parser_error (parser, "expected %<@end%>");
33831 break;
33832 }
33833 else if (token->type == CPP_PLUS || token->type == CPP_MINUS)
33834 {
33835 bool is_class_method = token->type == CPP_PLUS;
33836
33837 push_deferring_access_checks (dk_deferred);
33838 tree attribute;
33839 tree sig = cp_parser_objc_method_signature (parser, &attribute);
33840 if (sig == error_mark_node)
33841 cp_parser_skip_to_end_of_block_or_statement (parser);
33842 else
33843 {
33844 objc_start_method_definition (is_class_method, sig,
33845 attribute, NULL_TREE);
33846
33847 /* For historical reasons, we accept an optional semicolon. */
33848 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33849 cp_lexer_consume_token (parser->lexer);
33850
33851 perform_deferred_access_checks (tf_warning_or_error);
33852 stop_deferring_access_checks ();
33853 tree meth
33854 = cp_parser_function_definition_after_declarator (parser, false);
33855 pop_deferring_access_checks ();
33856 objc_finish_method_definition (meth);
33857 }
33858 }
33859 /* The following case will be removed once @synthesize is
33860 completely implemented. */
33861 else if (token->keyword == RID_AT_PROPERTY)
33862 cp_parser_objc_at_property_declaration (parser);
33863 else if (token->keyword == RID_AT_SYNTHESIZE)
33864 cp_parser_objc_at_synthesize_declaration (parser);
33865 else if (token->keyword == RID_AT_DYNAMIC)
33866 cp_parser_objc_at_dynamic_declaration (parser);
33867 else if (token->keyword == RID_ATTRIBUTE
33868 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
33869 warning_at (token->location, OPT_Wattributes,
33870 "prefix attributes are ignored for methods");
33871 else
33872 /* Allow for interspersed non-ObjC++ code. */
33873 cp_parser_objc_interstitial_code (parser);
33874 }
33875
33876 objc_finish_implementation ();
33877 }
33878
33879 /* Parse Objective-C ivars. */
33880
33881 static void
33882 cp_parser_objc_class_ivars (cp_parser* parser)
33883 {
33884 cp_token *token = cp_lexer_peek_token (parser->lexer);
33885
33886 if (token->type != CPP_OPEN_BRACE)
33887 return; /* No ivars specified. */
33888
33889 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
33890 token = cp_lexer_peek_token (parser->lexer);
33891
33892 while (token->type != CPP_CLOSE_BRACE
33893 && token->keyword != RID_AT_END && token->type != CPP_EOF)
33894 {
33895 cp_decl_specifier_seq declspecs;
33896 int decl_class_or_enum_p;
33897 tree prefix_attributes;
33898
33899 cp_parser_objc_visibility_spec (parser);
33900
33901 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33902 break;
33903
33904 cp_parser_decl_specifier_seq (parser,
33905 CP_PARSER_FLAGS_OPTIONAL,
33906 &declspecs,
33907 &decl_class_or_enum_p);
33908
33909 /* auto, register, static, extern, mutable. */
33910 if (declspecs.storage_class != sc_none)
33911 {
33912 cp_parser_error (parser, "invalid type for instance variable");
33913 declspecs.storage_class = sc_none;
33914 }
33915
33916 /* thread_local. */
33917 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
33918 {
33919 cp_parser_error (parser, "invalid type for instance variable");
33920 declspecs.locations[ds_thread] = 0;
33921 }
33922
33923 /* typedef. */
33924 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
33925 {
33926 cp_parser_error (parser, "invalid type for instance variable");
33927 declspecs.locations[ds_typedef] = 0;
33928 }
33929
33930 prefix_attributes = declspecs.attributes;
33931 declspecs.attributes = NULL_TREE;
33932
33933 /* Keep going until we hit the `;' at the end of the
33934 declaration. */
33935 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33936 {
33937 tree width = NULL_TREE, attributes, first_attribute, decl;
33938 cp_declarator *declarator = NULL;
33939 int ctor_dtor_or_conv_p;
33940
33941 /* Check for a (possibly unnamed) bitfield declaration. */
33942 token = cp_lexer_peek_token (parser->lexer);
33943 if (token->type == CPP_COLON)
33944 goto eat_colon;
33945
33946 if (token->type == CPP_NAME
33947 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
33948 == CPP_COLON))
33949 {
33950 /* Get the name of the bitfield. */
33951 declarator = make_id_declarator (NULL_TREE,
33952 cp_parser_identifier (parser),
33953 sfk_none, token->location);
33954
33955 eat_colon:
33956 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
33957 /* Get the width of the bitfield. */
33958 width
33959 = cp_parser_constant_expression (parser);
33960 }
33961 else
33962 {
33963 /* Parse the declarator. */
33964 declarator
33965 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
33966 CP_PARSER_FLAGS_NONE,
33967 &ctor_dtor_or_conv_p,
33968 /*parenthesized_p=*/NULL,
33969 /*member_p=*/false,
33970 /*friend_p=*/false,
33971 /*static_p=*/false);
33972 }
33973
33974 /* Look for attributes that apply to the ivar. */
33975 attributes = cp_parser_attributes_opt (parser);
33976 /* Remember which attributes are prefix attributes and
33977 which are not. */
33978 first_attribute = attributes;
33979 /* Combine the attributes. */
33980 attributes = attr_chainon (prefix_attributes, attributes);
33981
33982 if (width)
33983 /* Create the bitfield declaration. */
33984 decl = grokbitfield (declarator, &declspecs,
33985 width, NULL_TREE, attributes);
33986 else
33987 decl = grokfield (declarator, &declspecs,
33988 NULL_TREE, /*init_const_expr_p=*/false,
33989 NULL_TREE, attributes);
33990
33991 /* Add the instance variable. */
33992 if (decl != error_mark_node && decl != NULL_TREE)
33993 objc_add_instance_variable (decl);
33994
33995 /* Reset PREFIX_ATTRIBUTES. */
33996 if (attributes != error_mark_node)
33997 {
33998 while (attributes && TREE_CHAIN (attributes) != first_attribute)
33999 attributes = TREE_CHAIN (attributes);
34000 if (attributes)
34001 TREE_CHAIN (attributes) = NULL_TREE;
34002 }
34003
34004 token = cp_lexer_peek_token (parser->lexer);
34005
34006 if (token->type == CPP_COMMA)
34007 {
34008 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
34009 continue;
34010 }
34011 break;
34012 }
34013
34014 cp_parser_consume_semicolon_at_end_of_statement (parser);
34015 token = cp_lexer_peek_token (parser->lexer);
34016 }
34017
34018 if (token->keyword == RID_AT_END)
34019 cp_parser_error (parser, "expected %<}%>");
34020
34021 /* Do not consume the RID_AT_END, so it will be read again as terminating
34022 the @interface of @implementation. */
34023 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
34024 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
34025
34026 /* For historical reasons, we accept an optional semicolon. */
34027 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34028 cp_lexer_consume_token (parser->lexer);
34029 }
34030
34031 /* Parse an Objective-C protocol declaration. */
34032
34033 static void
34034 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
34035 {
34036 tree proto, protorefs;
34037 cp_token *tok;
34038
34039 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
34040 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34041 {
34042 tok = cp_lexer_peek_token (parser->lexer);
34043 error_at (tok->location, "identifier expected after %<@protocol%>");
34044 cp_parser_consume_semicolon_at_end_of_statement (parser);
34045 return;
34046 }
34047
34048 /* See if we have a forward declaration or a definition. */
34049 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
34050
34051 /* Try a forward declaration first. */
34052 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
34053 {
34054 while (true)
34055 {
34056 tree id;
34057
34058 id = cp_parser_identifier (parser);
34059 if (id == error_mark_node)
34060 break;
34061
34062 objc_declare_protocol (id, attributes);
34063
34064 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34065 cp_lexer_consume_token (parser->lexer);
34066 else
34067 break;
34068 }
34069 cp_parser_consume_semicolon_at_end_of_statement (parser);
34070 }
34071
34072 /* Ok, we got a full-fledged definition (or at least should). */
34073 else
34074 {
34075 proto = cp_parser_identifier (parser);
34076 protorefs = cp_parser_objc_protocol_refs_opt (parser);
34077 objc_start_protocol (proto, protorefs, attributes);
34078 cp_parser_objc_method_prototype_list (parser);
34079 }
34080 }
34081
34082 /* Parse an Objective-C superclass or category. */
34083
34084 static void
34085 cp_parser_objc_superclass_or_category (cp_parser *parser,
34086 bool iface_p,
34087 tree *super,
34088 tree *categ, bool *is_class_extension)
34089 {
34090 cp_token *next = cp_lexer_peek_token (parser->lexer);
34091
34092 *super = *categ = NULL_TREE;
34093 *is_class_extension = false;
34094 if (next->type == CPP_COLON)
34095 {
34096 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
34097 *super = cp_parser_identifier (parser);
34098 }
34099 else if (next->type == CPP_OPEN_PAREN)
34100 {
34101 matching_parens parens;
34102 parens.consume_open (parser); /* Eat '('. */
34103
34104 /* If there is no category name, and this is an @interface, we
34105 have a class extension. */
34106 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
34107 {
34108 *categ = NULL_TREE;
34109 *is_class_extension = true;
34110 }
34111 else
34112 *categ = cp_parser_identifier (parser);
34113
34114 parens.require_close (parser);
34115 }
34116 }
34117
34118 /* Parse an Objective-C class interface. */
34119
34120 static void
34121 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
34122 {
34123 tree name, super, categ, protos;
34124 bool is_class_extension;
34125
34126 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
34127 location_t nam_loc = cp_lexer_peek_token (parser->lexer)->location;
34128 name = cp_parser_identifier (parser);
34129 if (name == error_mark_node)
34130 {
34131 /* It's hard to recover because even if valid @interface stuff
34132 is to follow, we can't compile it (or validate it) if we
34133 don't even know which class it refers to. Let's assume this
34134 was a stray '@interface' token in the stream and skip it.
34135 */
34136 return;
34137 }
34138 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
34139 &is_class_extension);
34140 protos = cp_parser_objc_protocol_refs_opt (parser);
34141
34142 /* We have either a class or a category on our hands. */
34143 if (categ || is_class_extension)
34144 objc_start_category_interface (name, categ, protos, attributes);
34145 else
34146 {
34147 objc_start_class_interface (name, nam_loc, super, protos, attributes);
34148 /* Handle instance variable declarations, if any. */
34149 cp_parser_objc_class_ivars (parser);
34150 objc_continue_interface ();
34151 }
34152
34153 cp_parser_objc_method_prototype_list (parser);
34154 }
34155
34156 /* Parse an Objective-C class implementation. */
34157
34158 static void
34159 cp_parser_objc_class_implementation (cp_parser* parser)
34160 {
34161 tree name, super, categ;
34162 bool is_class_extension;
34163
34164 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
34165 name = cp_parser_identifier (parser);
34166 if (name == error_mark_node)
34167 {
34168 /* It's hard to recover because even if valid @implementation
34169 stuff is to follow, we can't compile it (or validate it) if
34170 we don't even know which class it refers to. Let's assume
34171 this was a stray '@implementation' token in the stream and
34172 skip it.
34173 */
34174 return;
34175 }
34176 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
34177 &is_class_extension);
34178
34179 /* We have either a class or a category on our hands. */
34180 if (categ)
34181 objc_start_category_implementation (name, categ);
34182 else
34183 {
34184 objc_start_class_implementation (name, super);
34185 /* Handle instance variable declarations, if any. */
34186 cp_parser_objc_class_ivars (parser);
34187 objc_continue_implementation ();
34188 }
34189
34190 cp_parser_objc_method_definition_list (parser);
34191 }
34192
34193 /* Consume the @end token and finish off the implementation. */
34194
34195 static void
34196 cp_parser_objc_end_implementation (cp_parser* parser)
34197 {
34198 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
34199 objc_finish_implementation ();
34200 }
34201
34202 /* Parse an Objective-C declaration. */
34203
34204 static void
34205 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
34206 {
34207 /* Try to figure out what kind of declaration is present. */
34208 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
34209
34210 if (attributes)
34211 switch (kwd->keyword)
34212 {
34213 case RID_AT_ALIAS:
34214 case RID_AT_CLASS:
34215 case RID_AT_END:
34216 error_at (kwd->location, "attributes may not be specified before"
34217 " the %<@%D%> Objective-C++ keyword",
34218 kwd->u.value);
34219 attributes = NULL;
34220 break;
34221 case RID_AT_IMPLEMENTATION:
34222 warning_at (kwd->location, OPT_Wattributes,
34223 "prefix attributes are ignored before %<@%D%>",
34224 kwd->u.value);
34225 attributes = NULL;
34226 default:
34227 break;
34228 }
34229
34230 switch (kwd->keyword)
34231 {
34232 case RID_AT_ALIAS:
34233 cp_parser_objc_alias_declaration (parser);
34234 break;
34235 case RID_AT_CLASS:
34236 cp_parser_objc_class_declaration (parser);
34237 break;
34238 case RID_AT_PROTOCOL:
34239 cp_parser_objc_protocol_declaration (parser, attributes);
34240 break;
34241 case RID_AT_INTERFACE:
34242 cp_parser_objc_class_interface (parser, attributes);
34243 break;
34244 case RID_AT_IMPLEMENTATION:
34245 cp_parser_objc_class_implementation (parser);
34246 break;
34247 case RID_AT_END:
34248 cp_parser_objc_end_implementation (parser);
34249 break;
34250 default:
34251 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
34252 kwd->u.value);
34253 cp_parser_skip_to_end_of_block_or_statement (parser);
34254 }
34255 }
34256
34257 /* Parse an Objective-C try-catch-finally statement.
34258
34259 objc-try-catch-finally-stmt:
34260 @try compound-statement objc-catch-clause-seq [opt]
34261 objc-finally-clause [opt]
34262
34263 objc-catch-clause-seq:
34264 objc-catch-clause objc-catch-clause-seq [opt]
34265
34266 objc-catch-clause:
34267 @catch ( objc-exception-declaration ) compound-statement
34268
34269 objc-finally-clause:
34270 @finally compound-statement
34271
34272 objc-exception-declaration:
34273 parameter-declaration
34274 '...'
34275
34276 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
34277
34278 Returns NULL_TREE.
34279
34280 PS: This function is identical to c_parser_objc_try_catch_finally_statement
34281 for C. Keep them in sync. */
34282
34283 static tree
34284 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
34285 {
34286 location_t location;
34287 tree stmt;
34288
34289 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
34290 location = cp_lexer_peek_token (parser->lexer)->location;
34291 objc_maybe_warn_exceptions (location);
34292 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
34293 node, lest it get absorbed into the surrounding block. */
34294 stmt = push_stmt_list ();
34295 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
34296 objc_begin_try_stmt (location, pop_stmt_list (stmt));
34297
34298 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
34299 {
34300 cp_parameter_declarator *parm;
34301 tree parameter_declaration = error_mark_node;
34302 bool seen_open_paren = false;
34303 matching_parens parens;
34304
34305 cp_lexer_consume_token (parser->lexer);
34306 if (parens.require_open (parser))
34307 seen_open_paren = true;
34308 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
34309 {
34310 /* We have "@catch (...)" (where the '...' are literally
34311 what is in the code). Skip the '...'.
34312 parameter_declaration is set to NULL_TREE, and
34313 objc_being_catch_clauses() knows that that means
34314 '...'. */
34315 cp_lexer_consume_token (parser->lexer);
34316 parameter_declaration = NULL_TREE;
34317 }
34318 else
34319 {
34320 /* We have "@catch (NSException *exception)" or something
34321 like that. Parse the parameter declaration. */
34322 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
34323 false, NULL);
34324 if (parm == NULL)
34325 parameter_declaration = error_mark_node;
34326 else
34327 parameter_declaration = grokdeclarator (parm->declarator,
34328 &parm->decl_specifiers,
34329 PARM, /*initialized=*/0,
34330 /*attrlist=*/NULL);
34331 }
34332 if (seen_open_paren)
34333 parens.require_close (parser);
34334 else
34335 {
34336 /* If there was no open parenthesis, we are recovering from
34337 an error, and we are trying to figure out what mistake
34338 the user has made. */
34339
34340 /* If there is an immediate closing parenthesis, the user
34341 probably forgot the opening one (ie, they typed "@catch
34342 NSException *e)". Parse the closing parenthesis and keep
34343 going. */
34344 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
34345 cp_lexer_consume_token (parser->lexer);
34346
34347 /* If these is no immediate closing parenthesis, the user
34348 probably doesn't know that parenthesis are required at
34349 all (ie, they typed "@catch NSException *e"). So, just
34350 forget about the closing parenthesis and keep going. */
34351 }
34352 objc_begin_catch_clause (parameter_declaration);
34353 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
34354 objc_finish_catch_clause ();
34355 }
34356 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
34357 {
34358 cp_lexer_consume_token (parser->lexer);
34359 location = cp_lexer_peek_token (parser->lexer)->location;
34360 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
34361 node, lest it get absorbed into the surrounding block. */
34362 stmt = push_stmt_list ();
34363 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
34364 objc_build_finally_clause (location, pop_stmt_list (stmt));
34365 }
34366
34367 return objc_finish_try_stmt ();
34368 }
34369
34370 /* Parse an Objective-C synchronized statement.
34371
34372 objc-synchronized-stmt:
34373 @synchronized ( expression ) compound-statement
34374
34375 Returns NULL_TREE. */
34376
34377 static tree
34378 cp_parser_objc_synchronized_statement (cp_parser *parser)
34379 {
34380 location_t location;
34381 tree lock, stmt;
34382
34383 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
34384
34385 location = cp_lexer_peek_token (parser->lexer)->location;
34386 objc_maybe_warn_exceptions (location);
34387 matching_parens parens;
34388 parens.require_open (parser);
34389 lock = cp_parser_expression (parser);
34390 parens.require_close (parser);
34391
34392 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
34393 node, lest it get absorbed into the surrounding block. */
34394 stmt = push_stmt_list ();
34395 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
34396
34397 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
34398 }
34399
34400 /* Parse an Objective-C throw statement.
34401
34402 objc-throw-stmt:
34403 @throw assignment-expression [opt] ;
34404
34405 Returns a constructed '@throw' statement. */
34406
34407 static tree
34408 cp_parser_objc_throw_statement (cp_parser *parser)
34409 {
34410 tree expr = NULL_TREE;
34411 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34412
34413 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
34414
34415 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34416 expr = cp_parser_expression (parser);
34417
34418 cp_parser_consume_semicolon_at_end_of_statement (parser);
34419
34420 return objc_build_throw_stmt (loc, expr);
34421 }
34422
34423 /* Parse an Objective-C statement. */
34424
34425 static tree
34426 cp_parser_objc_statement (cp_parser * parser)
34427 {
34428 /* Try to figure out what kind of declaration is present. */
34429 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
34430
34431 switch (kwd->keyword)
34432 {
34433 case RID_AT_TRY:
34434 return cp_parser_objc_try_catch_finally_statement (parser);
34435 case RID_AT_SYNCHRONIZED:
34436 return cp_parser_objc_synchronized_statement (parser);
34437 case RID_AT_THROW:
34438 return cp_parser_objc_throw_statement (parser);
34439 default:
34440 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
34441 kwd->u.value);
34442 cp_parser_skip_to_end_of_block_or_statement (parser);
34443 }
34444
34445 return error_mark_node;
34446 }
34447
34448 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
34449 look ahead to see if an objc keyword follows the attributes. This
34450 is to detect the use of prefix attributes on ObjC @interface and
34451 @protocol. */
34452
34453 static bool
34454 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
34455 {
34456 cp_lexer_save_tokens (parser->lexer);
34457 tree addon = cp_parser_attributes_opt (parser);
34458 if (addon
34459 && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
34460 {
34461 cp_lexer_commit_tokens (parser->lexer);
34462 if (*attrib)
34463 TREE_CHAIN (*attrib) = addon;
34464 else
34465 *attrib = addon;
34466 return true;
34467 }
34468 cp_lexer_rollback_tokens (parser->lexer);
34469 return false;
34470 }
34471
34472 /* This routine is a minimal replacement for
34473 c_parser_struct_declaration () used when parsing the list of
34474 types/names or ObjC++ properties. For example, when parsing the
34475 code
34476
34477 @property (readonly) int a, b, c;
34478
34479 this function is responsible for parsing "int a, int b, int c" and
34480 returning the declarations as CHAIN of DECLs.
34481
34482 TODO: Share this code with cp_parser_objc_class_ivars. It's very
34483 similar parsing. */
34484 static tree
34485 cp_parser_objc_struct_declaration (cp_parser *parser)
34486 {
34487 tree decls = NULL_TREE;
34488 cp_decl_specifier_seq declspecs;
34489 int decl_class_or_enum_p;
34490 tree prefix_attributes;
34491
34492 cp_parser_decl_specifier_seq (parser,
34493 CP_PARSER_FLAGS_NONE,
34494 &declspecs,
34495 &decl_class_or_enum_p);
34496
34497 if (declspecs.type == error_mark_node)
34498 return error_mark_node;
34499
34500 /* auto, register, static, extern, mutable. */
34501 if (declspecs.storage_class != sc_none)
34502 {
34503 cp_parser_error (parser, "invalid type for property");
34504 declspecs.storage_class = sc_none;
34505 }
34506
34507 /* thread_local. */
34508 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
34509 {
34510 cp_parser_error (parser, "invalid type for property");
34511 declspecs.locations[ds_thread] = 0;
34512 }
34513
34514 /* typedef. */
34515 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
34516 {
34517 cp_parser_error (parser, "invalid type for property");
34518 declspecs.locations[ds_typedef] = 0;
34519 }
34520
34521 prefix_attributes = declspecs.attributes;
34522 declspecs.attributes = NULL_TREE;
34523
34524 /* Keep going until we hit the `;' at the end of the declaration. */
34525 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34526 {
34527 tree attributes, first_attribute, decl;
34528 cp_declarator *declarator;
34529 cp_token *token;
34530
34531 /* Parse the declarator. */
34532 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
34533 CP_PARSER_FLAGS_NONE,
34534 NULL, NULL, false, false, false);
34535
34536 /* Look for attributes that apply to the ivar. */
34537 attributes = cp_parser_attributes_opt (parser);
34538 /* Remember which attributes are prefix attributes and
34539 which are not. */
34540 first_attribute = attributes;
34541 /* Combine the attributes. */
34542 attributes = attr_chainon (prefix_attributes, attributes);
34543
34544 decl = grokfield (declarator, &declspecs,
34545 NULL_TREE, /*init_const_expr_p=*/false,
34546 NULL_TREE, attributes);
34547
34548 if (decl == error_mark_node || decl == NULL_TREE)
34549 return error_mark_node;
34550
34551 /* Reset PREFIX_ATTRIBUTES. */
34552 if (attributes != error_mark_node)
34553 {
34554 while (attributes && TREE_CHAIN (attributes) != first_attribute)
34555 attributes = TREE_CHAIN (attributes);
34556 if (attributes)
34557 TREE_CHAIN (attributes) = NULL_TREE;
34558 }
34559
34560 DECL_CHAIN (decl) = decls;
34561 decls = decl;
34562
34563 token = cp_lexer_peek_token (parser->lexer);
34564 if (token->type == CPP_COMMA)
34565 {
34566 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
34567 continue;
34568 }
34569 else
34570 break;
34571 }
34572 return decls;
34573 }
34574
34575 /* Parse an Objective-C @property declaration. The syntax is:
34576
34577 objc-property-declaration:
34578 '@property' objc-property-attributes[opt] struct-declaration ;
34579
34580 objc-property-attributes:
34581 '(' objc-property-attribute-list ')'
34582
34583 objc-property-attribute-list:
34584 objc-property-attribute
34585 objc-property-attribute-list, objc-property-attribute
34586
34587 objc-property-attribute
34588 'getter' = identifier
34589 'setter' = identifier
34590 'readonly'
34591 'readwrite'
34592 'assign'
34593 'retain'
34594 'copy'
34595 'nonatomic'
34596
34597 For example:
34598 @property NSString *name;
34599 @property (readonly) id object;
34600 @property (retain, nonatomic, getter=getTheName) id name;
34601 @property int a, b, c;
34602
34603 PS: This function is identical to
34604 c_parser_objc_at_property_declaration for C. Keep them in sync. */
34605 static void
34606 cp_parser_objc_at_property_declaration (cp_parser *parser)
34607 {
34608 /* Parse the optional attribute list.
34609
34610 A list of parsed, but not verified, attributes. */
34611 vec<property_attribute_info *> prop_attr_list = vNULL;
34612 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34613
34614 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
34615
34616 /* Parse the optional attribute list... */
34617 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34618 {
34619 /* Eat the '('. */
34620 matching_parens parens;
34621 location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
34622 parens.consume_open (parser);
34623 bool syntax_error = false;
34624
34625 /* Allow empty @property attribute lists, but with a warning. */
34626 location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
34627 location_t attr_comb;
34628 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
34629 {
34630 attr_comb = make_location (attr_end, attr_start, attr_end);
34631 warning_at (attr_comb, OPT_Wattributes,
34632 "empty property attribute list");
34633 }
34634 else
34635 while (true)
34636 {
34637 cp_token *token = cp_lexer_peek_token (parser->lexer);
34638 attr_start = token->location;
34639 attr_end = get_finish (token->location);
34640 attr_comb = make_location (attr_start, attr_start, attr_end);
34641
34642 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
34643 {
34644 warning_at (attr_comb, OPT_Wattributes,
34645 "missing property attribute");
34646 if (token->type == CPP_CLOSE_PAREN)
34647 break;
34648 cp_lexer_consume_token (parser->lexer);
34649 continue;
34650 }
34651
34652 tree attr_name = NULL_TREE;
34653 if (identifier_p (token->u.value))
34654 attr_name = token->u.value;
34655
34656 enum rid keyword;
34657 if (token->type == CPP_NAME)
34658 keyword = C_RID_CODE (token->u.value);
34659 else if (token->type == CPP_KEYWORD
34660 && token->keyword == RID_CLASS)
34661 /* Account for accepting the 'class' keyword in this context. */
34662 keyword = RID_CLASS;
34663 else
34664 keyword = RID_MAX; /* By definition, an unknown property. */
34665 cp_lexer_consume_token (parser->lexer);
34666
34667 enum objc_property_attribute_kind prop_kind
34668 = objc_prop_attr_kind_for_rid (keyword);
34669 property_attribute_info *prop
34670 = new property_attribute_info (attr_name, attr_comb, prop_kind);
34671 prop_attr_list.safe_push (prop);
34672
34673 tree meth_name;
34674 switch (prop->prop_kind)
34675 {
34676 default: break;
34677 case OBJC_PROPERTY_ATTR_UNKNOWN:
34678 if (attr_name)
34679 error_at (attr_start, "unknown property attribute %qE",
34680 attr_name);
34681 else
34682 error_at (attr_start, "unknown property attribute");
34683 prop->parse_error = syntax_error = true;
34684 break;
34685
34686 case OBJC_PROPERTY_ATTR_GETTER:
34687 case OBJC_PROPERTY_ATTR_SETTER:
34688 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34689 {
34690 attr_comb = make_location (attr_end, attr_start, attr_end);
34691 error_at (attr_comb, "expected %<=%> after Objective-C %qE",
34692 attr_name);
34693 prop->parse_error = syntax_error = true;
34694 break;
34695 }
34696
34697 token = cp_lexer_peek_token (parser->lexer);
34698 attr_end = token->location;
34699 cp_lexer_consume_token (parser->lexer); /* eat the = */
34700
34701 if (!cp_parser_objc_selector_p
34702 (cp_lexer_peek_token (parser->lexer)->type))
34703 {
34704 attr_comb = make_location (attr_end, attr_start, attr_end);
34705 error_at (attr_comb, "expected %qE selector name",
34706 attr_name);
34707 prop->parse_error = syntax_error = true;
34708 break;
34709 }
34710
34711 /* Get the end of the method name, and consume the name. */
34712 token = cp_lexer_peek_token (parser->lexer);
34713 attr_end = get_finish (token->location);
34714 /* Because method names may contain C++ keywords, we have a
34715 routine to fetch them (this also consumes the token). */
34716 meth_name = cp_parser_objc_selector (parser);
34717
34718 if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
34719 {
34720 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
34721 {
34722 attr_comb = make_location (attr_end, attr_start,
34723 attr_end);
34724 error_at (attr_comb, "setter method names must"
34725 " terminate with %<:%>");
34726 prop->parse_error = syntax_error = true;
34727 }
34728 else
34729 {
34730 attr_end = get_finish (cp_lexer_peek_token
34731 (parser->lexer)->location);
34732 cp_lexer_consume_token (parser->lexer);
34733 }
34734 attr_comb = make_location (attr_start, attr_start,
34735 attr_end);
34736 }
34737 else
34738 attr_comb = make_location (attr_start, attr_start,
34739 attr_end);
34740 prop->ident = meth_name;
34741 /* Updated location including all that was successfully
34742 parsed. */
34743 prop->prop_loc = attr_comb;
34744 break;
34745 }
34746
34747 /* If we see a comma here, then keep going - even if we already
34748 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
34749 this makes a more useful output and avoid spurious warnings
34750 about missing attributes that are, in fact, specified after the
34751 one with the syntax error. */
34752 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34753 cp_lexer_consume_token (parser->lexer);
34754 else
34755 break;
34756 }
34757
34758 if (syntax_error || !parens.require_close (parser))
34759 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34760 /*or_comma=*/false,
34761 /*consume_paren=*/true);
34762 }
34763
34764 /* 'properties' is the list of properties that we read. Usually a
34765 single one, but maybe more (eg, in "@property int a, b, c;" there
34766 are three).
34767 TODO: Update this parsing so that it accepts (erroneous) bitfields so
34768 that we can issue a meaningful and consistent (between C/C++) error
34769 message from objc_add_property_declaration (). */
34770 tree properties = cp_parser_objc_struct_declaration (parser);
34771
34772 if (properties == error_mark_node)
34773 cp_parser_skip_to_end_of_statement (parser);
34774 else if (properties == NULL_TREE)
34775 cp_parser_error (parser, "expected identifier");
34776 else
34777 {
34778 /* Comma-separated properties are chained together in reverse order;
34779 add them one by one. */
34780 properties = nreverse (properties);
34781 for (; properties; properties = TREE_CHAIN (properties))
34782 objc_add_property_declaration (loc, copy_node (properties),
34783 prop_attr_list);
34784 }
34785
34786 cp_parser_consume_semicolon_at_end_of_statement (parser);
34787
34788 while (!prop_attr_list.is_empty())
34789 delete prop_attr_list.pop ();
34790 prop_attr_list.release ();
34791 }
34792
34793 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
34794
34795 objc-synthesize-declaration:
34796 @synthesize objc-synthesize-identifier-list ;
34797
34798 objc-synthesize-identifier-list:
34799 objc-synthesize-identifier
34800 objc-synthesize-identifier-list, objc-synthesize-identifier
34801
34802 objc-synthesize-identifier
34803 identifier
34804 identifier = identifier
34805
34806 For example:
34807 @synthesize MyProperty;
34808 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
34809
34810 PS: This function is identical to c_parser_objc_at_synthesize_declaration
34811 for C. Keep them in sync.
34812 */
34813 static void
34814 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
34815 {
34816 tree list = NULL_TREE;
34817 location_t loc;
34818 loc = cp_lexer_peek_token (parser->lexer)->location;
34819
34820 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
34821 while (true)
34822 {
34823 tree property, ivar;
34824 property = cp_parser_identifier (parser);
34825 if (property == error_mark_node)
34826 {
34827 cp_parser_consume_semicolon_at_end_of_statement (parser);
34828 return;
34829 }
34830 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
34831 {
34832 cp_lexer_consume_token (parser->lexer);
34833 ivar = cp_parser_identifier (parser);
34834 if (ivar == error_mark_node)
34835 {
34836 cp_parser_consume_semicolon_at_end_of_statement (parser);
34837 return;
34838 }
34839 }
34840 else
34841 ivar = NULL_TREE;
34842 list = chainon (list, build_tree_list (ivar, property));
34843 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34844 cp_lexer_consume_token (parser->lexer);
34845 else
34846 break;
34847 }
34848 cp_parser_consume_semicolon_at_end_of_statement (parser);
34849 objc_add_synthesize_declaration (loc, list);
34850 }
34851
34852 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
34853
34854 objc-dynamic-declaration:
34855 @dynamic identifier-list ;
34856
34857 For example:
34858 @dynamic MyProperty;
34859 @dynamic MyProperty, AnotherProperty;
34860
34861 PS: This function is identical to c_parser_objc_at_dynamic_declaration
34862 for C. Keep them in sync.
34863 */
34864 static void
34865 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
34866 {
34867 tree list = NULL_TREE;
34868 location_t loc;
34869 loc = cp_lexer_peek_token (parser->lexer)->location;
34870
34871 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
34872 while (true)
34873 {
34874 tree property;
34875 property = cp_parser_identifier (parser);
34876 if (property == error_mark_node)
34877 {
34878 cp_parser_consume_semicolon_at_end_of_statement (parser);
34879 return;
34880 }
34881 list = chainon (list, build_tree_list (NULL, property));
34882 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34883 cp_lexer_consume_token (parser->lexer);
34884 else
34885 break;
34886 }
34887 cp_parser_consume_semicolon_at_end_of_statement (parser);
34888 objc_add_dynamic_declaration (loc, list);
34889 }
34890
34891 \f
34892 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
34893
34894 /* Returns name of the next clause.
34895 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
34896 the token is not consumed. Otherwise appropriate pragma_omp_clause is
34897 returned and the token is consumed. */
34898
34899 static pragma_omp_clause
34900 cp_parser_omp_clause_name (cp_parser *parser)
34901 {
34902 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
34903
34904 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
34905 result = PRAGMA_OACC_CLAUSE_AUTO;
34906 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
34907 result = PRAGMA_OMP_CLAUSE_IF;
34908 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
34909 result = PRAGMA_OMP_CLAUSE_DEFAULT;
34910 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
34911 result = PRAGMA_OACC_CLAUSE_DELETE;
34912 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
34913 result = PRAGMA_OMP_CLAUSE_PRIVATE;
34914 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34915 result = PRAGMA_OMP_CLAUSE_FOR;
34916 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34917 {
34918 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34919 const char *p = IDENTIFIER_POINTER (id);
34920
34921 switch (p[0])
34922 {
34923 case 'a':
34924 if (!strcmp ("aligned", p))
34925 result = PRAGMA_OMP_CLAUSE_ALIGNED;
34926 else if (!strcmp ("allocate", p))
34927 result = PRAGMA_OMP_CLAUSE_ALLOCATE;
34928 else if (!strcmp ("async", p))
34929 result = PRAGMA_OACC_CLAUSE_ASYNC;
34930 else if (!strcmp ("attach", p))
34931 result = PRAGMA_OACC_CLAUSE_ATTACH;
34932 break;
34933 case 'b':
34934 if (!strcmp ("bind", p))
34935 result = PRAGMA_OMP_CLAUSE_BIND;
34936 break;
34937 case 'c':
34938 if (!strcmp ("collapse", p))
34939 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
34940 else if (!strcmp ("copy", p))
34941 result = PRAGMA_OACC_CLAUSE_COPY;
34942 else if (!strcmp ("copyin", p))
34943 result = PRAGMA_OMP_CLAUSE_COPYIN;
34944 else if (!strcmp ("copyout", p))
34945 result = PRAGMA_OACC_CLAUSE_COPYOUT;
34946 else if (!strcmp ("copyprivate", p))
34947 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
34948 else if (!strcmp ("create", p))
34949 result = PRAGMA_OACC_CLAUSE_CREATE;
34950 break;
34951 case 'd':
34952 if (!strcmp ("defaultmap", p))
34953 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
34954 else if (!strcmp ("depend", p))
34955 result = PRAGMA_OMP_CLAUSE_DEPEND;
34956 else if (!strcmp ("detach", p))
34957 result = PRAGMA_OACC_CLAUSE_DETACH;
34958 else if (!strcmp ("device", p))
34959 result = PRAGMA_OMP_CLAUSE_DEVICE;
34960 else if (!strcmp ("deviceptr", p))
34961 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
34962 else if (!strcmp ("device_resident", p))
34963 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
34964 else if (!strcmp ("device_type", p))
34965 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
34966 else if (!strcmp ("dist_schedule", p))
34967 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
34968 break;
34969 case 'f':
34970 if (!strcmp ("final", p))
34971 result = PRAGMA_OMP_CLAUSE_FINAL;
34972 else if (!strcmp ("finalize", p))
34973 result = PRAGMA_OACC_CLAUSE_FINALIZE;
34974 else if (!strcmp ("firstprivate", p))
34975 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
34976 else if (!strcmp ("from", p))
34977 result = PRAGMA_OMP_CLAUSE_FROM;
34978 break;
34979 case 'g':
34980 if (!strcmp ("gang", p))
34981 result = PRAGMA_OACC_CLAUSE_GANG;
34982 else if (!strcmp ("grainsize", p))
34983 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
34984 break;
34985 case 'h':
34986 if (!strcmp ("hint", p))
34987 result = PRAGMA_OMP_CLAUSE_HINT;
34988 else if (!strcmp ("host", p))
34989 result = PRAGMA_OACC_CLAUSE_HOST;
34990 break;
34991 case 'i':
34992 if (!strcmp ("if_present", p))
34993 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
34994 else if (!strcmp ("in_reduction", p))
34995 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
34996 else if (!strcmp ("inbranch", p))
34997 result = PRAGMA_OMP_CLAUSE_INBRANCH;
34998 else if (!strcmp ("independent", p))
34999 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
35000 else if (!strcmp ("is_device_ptr", p))
35001 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
35002 break;
35003 case 'l':
35004 if (!strcmp ("lastprivate", p))
35005 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
35006 else if (!strcmp ("linear", p))
35007 result = PRAGMA_OMP_CLAUSE_LINEAR;
35008 else if (!strcmp ("link", p))
35009 result = PRAGMA_OMP_CLAUSE_LINK;
35010 break;
35011 case 'm':
35012 if (!strcmp ("map", p))
35013 result = PRAGMA_OMP_CLAUSE_MAP;
35014 else if (!strcmp ("mergeable", p))
35015 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
35016 break;
35017 case 'n':
35018 if (!strcmp ("no_create", p))
35019 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
35020 else if (!strcmp ("nogroup", p))
35021 result = PRAGMA_OMP_CLAUSE_NOGROUP;
35022 else if (!strcmp ("nontemporal", p))
35023 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
35024 else if (!strcmp ("notinbranch", p))
35025 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
35026 else if (!strcmp ("nowait", p))
35027 result = PRAGMA_OMP_CLAUSE_NOWAIT;
35028 else if (!strcmp ("num_gangs", p))
35029 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
35030 else if (!strcmp ("num_tasks", p))
35031 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
35032 else if (!strcmp ("num_teams", p))
35033 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
35034 else if (!strcmp ("num_threads", p))
35035 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
35036 else if (!strcmp ("num_workers", p))
35037 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
35038 break;
35039 case 'o':
35040 if (!strcmp ("ordered", p))
35041 result = PRAGMA_OMP_CLAUSE_ORDERED;
35042 else if (!strcmp ("order", p))
35043 result = PRAGMA_OMP_CLAUSE_ORDER;
35044 break;
35045 case 'p':
35046 if (!strcmp ("parallel", p))
35047 result = PRAGMA_OMP_CLAUSE_PARALLEL;
35048 else if (!strcmp ("present", p))
35049 result = PRAGMA_OACC_CLAUSE_PRESENT;
35050 else if (!strcmp ("present_or_copy", p)
35051 || !strcmp ("pcopy", p))
35052 result = PRAGMA_OACC_CLAUSE_COPY;
35053 else if (!strcmp ("present_or_copyin", p)
35054 || !strcmp ("pcopyin", p))
35055 result = PRAGMA_OACC_CLAUSE_COPYIN;
35056 else if (!strcmp ("present_or_copyout", p)
35057 || !strcmp ("pcopyout", p))
35058 result = PRAGMA_OACC_CLAUSE_COPYOUT;
35059 else if (!strcmp ("present_or_create", p)
35060 || !strcmp ("pcreate", p))
35061 result = PRAGMA_OACC_CLAUSE_CREATE;
35062 else if (!strcmp ("priority", p))
35063 result = PRAGMA_OMP_CLAUSE_PRIORITY;
35064 else if (!strcmp ("proc_bind", p))
35065 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
35066 break;
35067 case 'r':
35068 if (!strcmp ("reduction", p))
35069 result = PRAGMA_OMP_CLAUSE_REDUCTION;
35070 break;
35071 case 's':
35072 if (!strcmp ("safelen", p))
35073 result = PRAGMA_OMP_CLAUSE_SAFELEN;
35074 else if (!strcmp ("schedule", p))
35075 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
35076 else if (!strcmp ("sections", p))
35077 result = PRAGMA_OMP_CLAUSE_SECTIONS;
35078 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
35079 result = PRAGMA_OACC_CLAUSE_HOST;
35080 else if (!strcmp ("seq", p))
35081 result = PRAGMA_OACC_CLAUSE_SEQ;
35082 else if (!strcmp ("shared", p))
35083 result = PRAGMA_OMP_CLAUSE_SHARED;
35084 else if (!strcmp ("simd", p))
35085 result = PRAGMA_OMP_CLAUSE_SIMD;
35086 else if (!strcmp ("simdlen", p))
35087 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
35088 break;
35089 case 't':
35090 if (!strcmp ("task_reduction", p))
35091 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
35092 else if (!strcmp ("taskgroup", p))
35093 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
35094 else if (!strcmp ("thread_limit", p))
35095 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
35096 else if (!strcmp ("threads", p))
35097 result = PRAGMA_OMP_CLAUSE_THREADS;
35098 else if (!strcmp ("tile", p))
35099 result = PRAGMA_OACC_CLAUSE_TILE;
35100 else if (!strcmp ("to", p))
35101 result = PRAGMA_OMP_CLAUSE_TO;
35102 break;
35103 case 'u':
35104 if (!strcmp ("uniform", p))
35105 result = PRAGMA_OMP_CLAUSE_UNIFORM;
35106 else if (!strcmp ("untied", p))
35107 result = PRAGMA_OMP_CLAUSE_UNTIED;
35108 else if (!strcmp ("use_device", p))
35109 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
35110 else if (!strcmp ("use_device_addr", p))
35111 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
35112 else if (!strcmp ("use_device_ptr", p))
35113 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
35114 break;
35115 case 'v':
35116 if (!strcmp ("vector", p))
35117 result = PRAGMA_OACC_CLAUSE_VECTOR;
35118 else if (!strcmp ("vector_length", p))
35119 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
35120 break;
35121 case 'w':
35122 if (!strcmp ("wait", p))
35123 result = PRAGMA_OACC_CLAUSE_WAIT;
35124 else if (!strcmp ("worker", p))
35125 result = PRAGMA_OACC_CLAUSE_WORKER;
35126 break;
35127 }
35128 }
35129
35130 if (result != PRAGMA_OMP_CLAUSE_NONE)
35131 cp_lexer_consume_token (parser->lexer);
35132
35133 return result;
35134 }
35135
35136 /* Validate that a clause of the given type does not already exist. */
35137
35138 static void
35139 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
35140 const char *name, location_t location)
35141 {
35142 if (omp_find_clause (clauses, code))
35143 error_at (location, "too many %qs clauses", name);
35144 }
35145
35146 /* OpenMP 2.5:
35147 variable-list:
35148 identifier
35149 variable-list , identifier
35150
35151 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
35152 colon). An opening parenthesis will have been consumed by the caller.
35153
35154 If KIND is nonzero, create the appropriate node and install the decl
35155 in OMP_CLAUSE_DECL and add the node to the head of the list.
35156
35157 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
35158 return the list created.
35159
35160 COLON can be NULL if only closing parenthesis should end the list,
35161 or pointer to bool which will receive false if the list is terminated
35162 by closing parenthesis or true if the list is terminated by colon.
35163
35164 The optional ALLOW_DEREF argument is true if list items can use the deref
35165 (->) operator. */
35166
35167 static tree
35168 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
35169 tree list, bool *colon,
35170 bool allow_deref = false)
35171 {
35172 cp_token *token;
35173 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
35174 if (colon)
35175 {
35176 parser->colon_corrects_to_scope_p = false;
35177 *colon = false;
35178 }
35179 while (1)
35180 {
35181 tree name, decl;
35182
35183 if (kind == OMP_CLAUSE_DEPEND)
35184 cp_parser_parse_tentatively (parser);
35185 token = cp_lexer_peek_token (parser->lexer);
35186 if (kind != 0
35187 && current_class_ptr
35188 && cp_parser_is_keyword (token, RID_THIS))
35189 {
35190 decl = finish_this_expr ();
35191 if (TREE_CODE (decl) == NON_LVALUE_EXPR
35192 || CONVERT_EXPR_P (decl))
35193 decl = TREE_OPERAND (decl, 0);
35194 cp_lexer_consume_token (parser->lexer);
35195 }
35196 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
35197 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
35198 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
35199 {
35200 cp_id_kind idk;
35201 decl = cp_parser_primary_expression (parser, false, false, false,
35202 &idk);
35203 }
35204 else
35205 {
35206 name = cp_parser_id_expression (parser, /*template_p=*/false,
35207 /*check_dependency_p=*/true,
35208 /*template_p=*/NULL,
35209 /*declarator_p=*/false,
35210 /*optional_p=*/false);
35211 if (name == error_mark_node)
35212 {
35213 if (kind == OMP_CLAUSE_DEPEND
35214 && cp_parser_simulate_error (parser))
35215 goto depend_lvalue;
35216 goto skip_comma;
35217 }
35218
35219 if (identifier_p (name))
35220 decl = cp_parser_lookup_name_simple (parser, name, token->location);
35221 else
35222 decl = name;
35223 if (decl == error_mark_node)
35224 {
35225 if (kind == OMP_CLAUSE_DEPEND
35226 && cp_parser_simulate_error (parser))
35227 goto depend_lvalue;
35228 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
35229 token->location);
35230 }
35231 }
35232 if (outer_automatic_var_p (decl))
35233 decl = process_outer_var_ref (decl, tf_warning_or_error);
35234 if (decl == error_mark_node)
35235 ;
35236 else if (kind != 0)
35237 {
35238 switch (kind)
35239 {
35240 case OMP_CLAUSE__CACHE_:
35241 /* The OpenACC cache directive explicitly only allows "array
35242 elements or subarrays". */
35243 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
35244 {
35245 error_at (token->location, "expected %<[%>");
35246 decl = error_mark_node;
35247 break;
35248 }
35249 /* FALLTHROUGH. */
35250 case OMP_CLAUSE_MAP:
35251 case OMP_CLAUSE_FROM:
35252 case OMP_CLAUSE_TO:
35253 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
35254 || (allow_deref
35255 && cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
35256 {
35257 cpp_ttype ttype
35258 = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
35259 ? CPP_DOT : CPP_DEREF;
35260 location_t loc
35261 = cp_lexer_peek_token (parser->lexer)->location;
35262 cp_id_kind idk = CP_ID_KIND_NONE;
35263 cp_lexer_consume_token (parser->lexer);
35264 decl = convert_from_reference (decl);
35265 decl
35266 = cp_parser_postfix_dot_deref_expression (parser, ttype,
35267 decl, false,
35268 &idk, loc);
35269 }
35270 /* FALLTHROUGH. */
35271 case OMP_CLAUSE_DEPEND:
35272 case OMP_CLAUSE_REDUCTION:
35273 case OMP_CLAUSE_IN_REDUCTION:
35274 case OMP_CLAUSE_TASK_REDUCTION:
35275 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
35276 {
35277 tree low_bound = NULL_TREE, length = NULL_TREE;
35278
35279 parser->colon_corrects_to_scope_p = false;
35280 cp_lexer_consume_token (parser->lexer);
35281 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
35282 {
35283 low_bound = cp_parser_expression (parser);
35284 /* Later handling is not prepared to see through these. */
35285 gcc_checking_assert (!location_wrapper_p (low_bound));
35286 }
35287 if (!colon)
35288 parser->colon_corrects_to_scope_p
35289 = saved_colon_corrects_to_scope_p;
35290 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
35291 length = integer_one_node;
35292 else
35293 {
35294 /* Look for `:'. */
35295 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35296 {
35297 if (kind == OMP_CLAUSE_DEPEND
35298 && cp_parser_simulate_error (parser))
35299 goto depend_lvalue;
35300 goto skip_comma;
35301 }
35302 if (kind == OMP_CLAUSE_DEPEND)
35303 cp_parser_commit_to_tentative_parse (parser);
35304 if (!cp_lexer_next_token_is (parser->lexer,
35305 CPP_CLOSE_SQUARE))
35306 {
35307 length = cp_parser_expression (parser);
35308 /* Later handling is not prepared to see through these. */
35309 gcc_checking_assert (!location_wrapper_p (length));
35310 }
35311 }
35312 /* Look for the closing `]'. */
35313 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
35314 RT_CLOSE_SQUARE))
35315 {
35316 if (kind == OMP_CLAUSE_DEPEND
35317 && cp_parser_simulate_error (parser))
35318 goto depend_lvalue;
35319 goto skip_comma;
35320 }
35321
35322 decl = tree_cons (low_bound, length, decl);
35323 }
35324 break;
35325 default:
35326 break;
35327 }
35328
35329 if (kind == OMP_CLAUSE_DEPEND)
35330 {
35331 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
35332 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
35333 && cp_parser_simulate_error (parser))
35334 {
35335 depend_lvalue:
35336 cp_parser_abort_tentative_parse (parser);
35337 decl = cp_parser_assignment_expression (parser, NULL,
35338 false, false);
35339 }
35340 else
35341 cp_parser_parse_definitely (parser);
35342 }
35343
35344 tree u = build_omp_clause (token->location, kind);
35345 OMP_CLAUSE_DECL (u) = decl;
35346 OMP_CLAUSE_CHAIN (u) = list;
35347 list = u;
35348 }
35349 else
35350 list = tree_cons (decl, NULL_TREE, list);
35351
35352 get_comma:
35353 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
35354 break;
35355 cp_lexer_consume_token (parser->lexer);
35356 }
35357
35358 if (colon)
35359 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
35360
35361 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
35362 {
35363 *colon = true;
35364 cp_parser_require (parser, CPP_COLON, RT_COLON);
35365 return list;
35366 }
35367
35368 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35369 {
35370 int ending;
35371
35372 /* Try to resync to an unnested comma. Copied from
35373 cp_parser_parenthesized_expression_list. */
35374 skip_comma:
35375 if (colon)
35376 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
35377 ending = cp_parser_skip_to_closing_parenthesis (parser,
35378 /*recovering=*/true,
35379 /*or_comma=*/true,
35380 /*consume_paren=*/true);
35381 if (ending < 0)
35382 goto get_comma;
35383 }
35384
35385 return list;
35386 }
35387
35388 /* Similarly, but expect leading and trailing parenthesis. This is a very
35389 common case for omp clauses. */
35390
35391 static tree
35392 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
35393 bool allow_deref = false)
35394 {
35395 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35396 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
35397 allow_deref);
35398 return list;
35399 }
35400
35401 /* OpenACC 2.0:
35402 copy ( variable-list )
35403 copyin ( variable-list )
35404 copyout ( variable-list )
35405 create ( variable-list )
35406 delete ( variable-list )
35407 present ( variable-list )
35408
35409 OpenACC 2.6:
35410 no_create ( variable-list )
35411 attach ( variable-list )
35412 detach ( variable-list ) */
35413
35414 static tree
35415 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
35416 tree list)
35417 {
35418 enum gomp_map_kind kind;
35419 switch (c_kind)
35420 {
35421 case PRAGMA_OACC_CLAUSE_ATTACH:
35422 kind = GOMP_MAP_ATTACH;
35423 break;
35424 case PRAGMA_OACC_CLAUSE_COPY:
35425 kind = GOMP_MAP_TOFROM;
35426 break;
35427 case PRAGMA_OACC_CLAUSE_COPYIN:
35428 kind = GOMP_MAP_TO;
35429 break;
35430 case PRAGMA_OACC_CLAUSE_COPYOUT:
35431 kind = GOMP_MAP_FROM;
35432 break;
35433 case PRAGMA_OACC_CLAUSE_CREATE:
35434 kind = GOMP_MAP_ALLOC;
35435 break;
35436 case PRAGMA_OACC_CLAUSE_DELETE:
35437 kind = GOMP_MAP_RELEASE;
35438 break;
35439 case PRAGMA_OACC_CLAUSE_DETACH:
35440 kind = GOMP_MAP_DETACH;
35441 break;
35442 case PRAGMA_OACC_CLAUSE_DEVICE:
35443 kind = GOMP_MAP_FORCE_TO;
35444 break;
35445 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
35446 kind = GOMP_MAP_DEVICE_RESIDENT;
35447 break;
35448 case PRAGMA_OACC_CLAUSE_HOST:
35449 kind = GOMP_MAP_FORCE_FROM;
35450 break;
35451 case PRAGMA_OACC_CLAUSE_LINK:
35452 kind = GOMP_MAP_LINK;
35453 break;
35454 case PRAGMA_OACC_CLAUSE_NO_CREATE:
35455 kind = GOMP_MAP_IF_PRESENT;
35456 break;
35457 case PRAGMA_OACC_CLAUSE_PRESENT:
35458 kind = GOMP_MAP_FORCE_PRESENT;
35459 break;
35460 default:
35461 gcc_unreachable ();
35462 }
35463 tree nl, c;
35464 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, true);
35465
35466 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
35467 OMP_CLAUSE_SET_MAP_KIND (c, kind);
35468
35469 return nl;
35470 }
35471
35472 /* OpenACC 2.0:
35473 deviceptr ( variable-list ) */
35474
35475 static tree
35476 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
35477 {
35478 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35479 tree vars, t;
35480
35481 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
35482 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
35483 variable-list must only allow for pointer variables. */
35484 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35485 for (t = vars; t; t = TREE_CHAIN (t))
35486 {
35487 tree v = TREE_PURPOSE (t);
35488 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
35489 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
35490 OMP_CLAUSE_DECL (u) = v;
35491 OMP_CLAUSE_CHAIN (u) = list;
35492 list = u;
35493 }
35494
35495 return list;
35496 }
35497
35498 /* OpenACC 2.5:
35499 auto
35500 finalize
35501 independent
35502 nohost
35503 seq */
35504
35505 static tree
35506 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
35507 tree list)
35508 {
35509 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
35510
35511 tree c = build_omp_clause (loc, code);
35512 OMP_CLAUSE_CHAIN (c) = list;
35513
35514 return c;
35515 }
35516
35517 /* OpenACC:
35518 num_gangs ( expression )
35519 num_workers ( expression )
35520 vector_length ( expression ) */
35521
35522 static tree
35523 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
35524 const char *str, tree list)
35525 {
35526 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35527
35528 matching_parens parens;
35529 if (!parens.require_open (parser))
35530 return list;
35531
35532 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
35533
35534 if (t == error_mark_node
35535 || !parens.require_close (parser))
35536 {
35537 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35538 /*or_comma=*/false,
35539 /*consume_paren=*/true);
35540 return list;
35541 }
35542
35543 check_no_duplicate_clause (list, code, str, loc);
35544
35545 tree c = build_omp_clause (loc, code);
35546 OMP_CLAUSE_OPERAND (c, 0) = t;
35547 OMP_CLAUSE_CHAIN (c) = list;
35548 return c;
35549 }
35550
35551 /* OpenACC:
35552
35553 gang [( gang-arg-list )]
35554 worker [( [num:] int-expr )]
35555 vector [( [length:] int-expr )]
35556
35557 where gang-arg is one of:
35558
35559 [num:] int-expr
35560 static: size-expr
35561
35562 and size-expr may be:
35563
35564 *
35565 int-expr
35566 */
35567
35568 static tree
35569 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
35570 omp_clause_code kind,
35571 const char *str, tree list)
35572 {
35573 const char *id = "num";
35574 cp_lexer *lexer = parser->lexer;
35575 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
35576
35577 if (kind == OMP_CLAUSE_VECTOR)
35578 id = "length";
35579
35580 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
35581 {
35582 matching_parens parens;
35583 parens.consume_open (parser);
35584
35585 do
35586 {
35587 cp_token *next = cp_lexer_peek_token (lexer);
35588 int idx = 0;
35589
35590 /* Gang static argument. */
35591 if (kind == OMP_CLAUSE_GANG
35592 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
35593 {
35594 cp_lexer_consume_token (lexer);
35595
35596 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35597 goto cleanup_error;
35598
35599 idx = 1;
35600 if (ops[idx] != NULL)
35601 {
35602 cp_parser_error (parser, "too many %<static%> arguments");
35603 goto cleanup_error;
35604 }
35605
35606 /* Check for the '*' argument. */
35607 if (cp_lexer_next_token_is (lexer, CPP_MULT)
35608 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
35609 || cp_lexer_nth_token_is (parser->lexer, 2,
35610 CPP_CLOSE_PAREN)))
35611 {
35612 cp_lexer_consume_token (lexer);
35613 ops[idx] = integer_minus_one_node;
35614
35615 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
35616 {
35617 cp_lexer_consume_token (lexer);
35618 continue;
35619 }
35620 else break;
35621 }
35622 }
35623 /* Worker num: argument and vector length: arguments. */
35624 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
35625 && id_equal (next->u.value, id)
35626 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
35627 {
35628 cp_lexer_consume_token (lexer); /* id */
35629 cp_lexer_consume_token (lexer); /* ':' */
35630 }
35631
35632 /* Now collect the actual argument. */
35633 if (ops[idx] != NULL_TREE)
35634 {
35635 cp_parser_error (parser, "unexpected argument");
35636 goto cleanup_error;
35637 }
35638
35639 tree expr = cp_parser_assignment_expression (parser, NULL, false,
35640 false);
35641 if (expr == error_mark_node)
35642 goto cleanup_error;
35643
35644 mark_exp_read (expr);
35645 ops[idx] = expr;
35646
35647 if (kind == OMP_CLAUSE_GANG
35648 && cp_lexer_next_token_is (lexer, CPP_COMMA))
35649 {
35650 cp_lexer_consume_token (lexer);
35651 continue;
35652 }
35653 break;
35654 }
35655 while (1);
35656
35657 if (!parens.require_close (parser))
35658 goto cleanup_error;
35659 }
35660
35661 check_no_duplicate_clause (list, kind, str, loc);
35662
35663 c = build_omp_clause (loc, kind);
35664
35665 if (ops[1])
35666 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
35667
35668 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
35669 OMP_CLAUSE_CHAIN (c) = list;
35670
35671 return c;
35672
35673 cleanup_error:
35674 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
35675 return list;
35676 }
35677
35678 /* OpenACC 2.0:
35679 tile ( size-expr-list ) */
35680
35681 static tree
35682 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
35683 {
35684 tree c, expr = error_mark_node;
35685 tree tile = NULL_TREE;
35686
35687 /* Collapse and tile are mutually exclusive. (The spec doesn't say
35688 so, but the spec authors never considered such a case and have
35689 differing opinions on what it might mean, including 'not
35690 allowed'.) */
35691 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
35692 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
35693 clause_loc);
35694
35695 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35696 return list;
35697
35698 do
35699 {
35700 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
35701 return list;
35702
35703 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
35704 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
35705 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
35706 {
35707 cp_lexer_consume_token (parser->lexer);
35708 expr = integer_zero_node;
35709 }
35710 else
35711 expr = cp_parser_constant_expression (parser);
35712
35713 tile = tree_cons (NULL_TREE, expr, tile);
35714 }
35715 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
35716
35717 /* Consume the trailing ')'. */
35718 cp_lexer_consume_token (parser->lexer);
35719
35720 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
35721 tile = nreverse (tile);
35722 OMP_CLAUSE_TILE_LIST (c) = tile;
35723 OMP_CLAUSE_CHAIN (c) = list;
35724 return c;
35725 }
35726
35727 /* OpenACC 2.0
35728 Parse wait clause or directive parameters. */
35729
35730 static tree
35731 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
35732 {
35733 vec<tree, va_gc> *args;
35734 tree t, args_tree;
35735
35736 args = cp_parser_parenthesized_expression_list (parser, non_attr,
35737 /*cast_p=*/false,
35738 /*allow_expansion_p=*/true,
35739 /*non_constant_p=*/NULL);
35740
35741 if (args == NULL || args->length () == 0)
35742 {
35743 if (args != NULL)
35744 {
35745 cp_parser_error (parser, "expected integer expression list");
35746 release_tree_vector (args);
35747 }
35748 return list;
35749 }
35750
35751 args_tree = build_tree_list_vec (args);
35752
35753 release_tree_vector (args);
35754
35755 for (t = args_tree; t; t = TREE_CHAIN (t))
35756 {
35757 tree targ = TREE_VALUE (t);
35758
35759 if (targ != error_mark_node)
35760 {
35761 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
35762 error ("%<wait%> expression must be integral");
35763 else
35764 {
35765 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
35766
35767 targ = mark_rvalue_use (targ);
35768 OMP_CLAUSE_DECL (c) = targ;
35769 OMP_CLAUSE_CHAIN (c) = list;
35770 list = c;
35771 }
35772 }
35773 }
35774
35775 return list;
35776 }
35777
35778 /* OpenACC:
35779 wait [( int-expr-list )] */
35780
35781 static tree
35782 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
35783 {
35784 location_t location = cp_lexer_peek_token (parser->lexer)->location;
35785
35786 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35787 list = cp_parser_oacc_wait_list (parser, location, list);
35788 else
35789 {
35790 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
35791
35792 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
35793 OMP_CLAUSE_CHAIN (c) = list;
35794 list = c;
35795 }
35796
35797 return list;
35798 }
35799
35800 /* OpenMP 3.0:
35801 collapse ( constant-expression ) */
35802
35803 static tree
35804 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
35805 {
35806 tree c, num;
35807 location_t loc;
35808 HOST_WIDE_INT n;
35809
35810 loc = cp_lexer_peek_token (parser->lexer)->location;
35811 matching_parens parens;
35812 if (!parens.require_open (parser))
35813 return list;
35814
35815 num = cp_parser_constant_expression (parser);
35816
35817 if (!parens.require_close (parser))
35818 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35819 /*or_comma=*/false,
35820 /*consume_paren=*/true);
35821
35822 if (num == error_mark_node)
35823 return list;
35824 num = fold_non_dependent_expr (num);
35825 if (!tree_fits_shwi_p (num)
35826 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
35827 || (n = tree_to_shwi (num)) <= 0
35828 || (int) n != n)
35829 {
35830 error_at (loc, "collapse argument needs positive constant integer expression");
35831 return list;
35832 }
35833
35834 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
35835 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
35836 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
35837 OMP_CLAUSE_CHAIN (c) = list;
35838 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
35839
35840 return c;
35841 }
35842
35843 /* OpenMP 2.5:
35844 default ( none | shared )
35845
35846 OpenACC:
35847 default ( none | present ) */
35848
35849 static tree
35850 cp_parser_omp_clause_default (cp_parser *parser, tree list,
35851 location_t location, bool is_oacc)
35852 {
35853 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
35854 tree c;
35855
35856 matching_parens parens;
35857 if (!parens.require_open (parser))
35858 return list;
35859 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35860 {
35861 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35862 const char *p = IDENTIFIER_POINTER (id);
35863
35864 switch (p[0])
35865 {
35866 case 'n':
35867 if (strcmp ("none", p) != 0)
35868 goto invalid_kind;
35869 kind = OMP_CLAUSE_DEFAULT_NONE;
35870 break;
35871
35872 case 'p':
35873 if (strcmp ("present", p) != 0 || !is_oacc)
35874 goto invalid_kind;
35875 kind = OMP_CLAUSE_DEFAULT_PRESENT;
35876 break;
35877
35878 case 's':
35879 if (strcmp ("shared", p) != 0 || is_oacc)
35880 goto invalid_kind;
35881 kind = OMP_CLAUSE_DEFAULT_SHARED;
35882 break;
35883
35884 default:
35885 goto invalid_kind;
35886 }
35887
35888 cp_lexer_consume_token (parser->lexer);
35889 }
35890 else
35891 {
35892 invalid_kind:
35893 if (is_oacc)
35894 cp_parser_error (parser, "expected %<none%> or %<present%>");
35895 else
35896 cp_parser_error (parser, "expected %<none%> or %<shared%>");
35897 }
35898
35899 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
35900 || !parens.require_close (parser))
35901 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35902 /*or_comma=*/false,
35903 /*consume_paren=*/true);
35904
35905 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
35906 return list;
35907
35908 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
35909 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
35910 OMP_CLAUSE_CHAIN (c) = list;
35911 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
35912
35913 return c;
35914 }
35915
35916 /* OpenMP 3.1:
35917 final ( expression ) */
35918
35919 static tree
35920 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
35921 {
35922 tree t, c;
35923
35924 matching_parens parens;
35925 if (!parens.require_open (parser))
35926 return list;
35927
35928 t = cp_parser_assignment_expression (parser);
35929
35930 if (t == error_mark_node
35931 || !parens.require_close (parser))
35932 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35933 /*or_comma=*/false,
35934 /*consume_paren=*/true);
35935
35936 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
35937
35938 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
35939 OMP_CLAUSE_FINAL_EXPR (c) = t;
35940 OMP_CLAUSE_CHAIN (c) = list;
35941
35942 return c;
35943 }
35944
35945 /* OpenMP 2.5:
35946 if ( expression )
35947
35948 OpenMP 4.5:
35949 if ( directive-name-modifier : expression )
35950
35951 directive-name-modifier:
35952 parallel | task | taskloop | target data | target | target update
35953 | target enter data | target exit data
35954
35955 OpenMP 5.0:
35956 directive-name-modifier:
35957 ... | simd | cancel */
35958
35959 static tree
35960 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
35961 bool is_omp)
35962 {
35963 tree t, c;
35964 enum tree_code if_modifier = ERROR_MARK;
35965
35966 matching_parens parens;
35967 if (!parens.require_open (parser))
35968 return list;
35969
35970 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35971 {
35972 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35973 const char *p = IDENTIFIER_POINTER (id);
35974 int n = 2;
35975
35976 if (strcmp ("cancel", p) == 0)
35977 if_modifier = VOID_CST;
35978 else if (strcmp ("parallel", p) == 0)
35979 if_modifier = OMP_PARALLEL;
35980 else if (strcmp ("simd", p) == 0)
35981 if_modifier = OMP_SIMD;
35982 else if (strcmp ("task", p) == 0)
35983 if_modifier = OMP_TASK;
35984 else if (strcmp ("taskloop", p) == 0)
35985 if_modifier = OMP_TASKLOOP;
35986 else if (strcmp ("target", p) == 0)
35987 {
35988 if_modifier = OMP_TARGET;
35989 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
35990 {
35991 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
35992 p = IDENTIFIER_POINTER (id);
35993 if (strcmp ("data", p) == 0)
35994 if_modifier = OMP_TARGET_DATA;
35995 else if (strcmp ("update", p) == 0)
35996 if_modifier = OMP_TARGET_UPDATE;
35997 else if (strcmp ("enter", p) == 0)
35998 if_modifier = OMP_TARGET_ENTER_DATA;
35999 else if (strcmp ("exit", p) == 0)
36000 if_modifier = OMP_TARGET_EXIT_DATA;
36001 if (if_modifier != OMP_TARGET)
36002 n = 3;
36003 else
36004 {
36005 location_t loc
36006 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
36007 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
36008 "or %<exit%>");
36009 if_modifier = ERROR_MARK;
36010 }
36011 if (if_modifier == OMP_TARGET_ENTER_DATA
36012 || if_modifier == OMP_TARGET_EXIT_DATA)
36013 {
36014 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
36015 {
36016 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
36017 p = IDENTIFIER_POINTER (id);
36018 if (strcmp ("data", p) == 0)
36019 n = 4;
36020 }
36021 if (n != 4)
36022 {
36023 location_t loc
36024 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
36025 error_at (loc, "expected %<data%>");
36026 if_modifier = ERROR_MARK;
36027 }
36028 }
36029 }
36030 }
36031 if (if_modifier != ERROR_MARK)
36032 {
36033 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
36034 {
36035 while (n-- > 0)
36036 cp_lexer_consume_token (parser->lexer);
36037 }
36038 else
36039 {
36040 if (n > 2)
36041 {
36042 location_t loc
36043 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
36044 error_at (loc, "expected %<:%>");
36045 }
36046 if_modifier = ERROR_MARK;
36047 }
36048 }
36049 }
36050
36051 t = cp_parser_assignment_expression (parser);
36052
36053 if (t == error_mark_node
36054 || !parens.require_close (parser))
36055 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36056 /*or_comma=*/false,
36057 /*consume_paren=*/true);
36058
36059 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
36060 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
36061 {
36062 if (if_modifier != ERROR_MARK
36063 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
36064 {
36065 const char *p = NULL;
36066 switch (if_modifier)
36067 {
36068 case VOID_CST: p = "cancel"; break;
36069 case OMP_PARALLEL: p = "parallel"; break;
36070 case OMP_SIMD: p = "simd"; break;
36071 case OMP_TASK: p = "task"; break;
36072 case OMP_TASKLOOP: p = "taskloop"; break;
36073 case OMP_TARGET_DATA: p = "target data"; break;
36074 case OMP_TARGET: p = "target"; break;
36075 case OMP_TARGET_UPDATE: p = "target update"; break;
36076 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
36077 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
36078 default: gcc_unreachable ();
36079 }
36080 error_at (location, "too many %<if%> clauses with %qs modifier",
36081 p);
36082 return list;
36083 }
36084 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
36085 {
36086 if (!is_omp)
36087 error_at (location, "too many %<if%> clauses");
36088 else
36089 error_at (location, "too many %<if%> clauses without modifier");
36090 return list;
36091 }
36092 else if (if_modifier == ERROR_MARK
36093 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
36094 {
36095 error_at (location, "if any %<if%> clause has modifier, then all "
36096 "%<if%> clauses have to use modifier");
36097 return list;
36098 }
36099 }
36100
36101 c = build_omp_clause (location, OMP_CLAUSE_IF);
36102 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
36103 OMP_CLAUSE_IF_EXPR (c) = t;
36104 OMP_CLAUSE_CHAIN (c) = list;
36105
36106 return c;
36107 }
36108
36109 /* OpenMP 3.1:
36110 mergeable */
36111
36112 static tree
36113 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
36114 tree list, location_t location)
36115 {
36116 tree c;
36117
36118 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
36119 location);
36120
36121 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
36122 OMP_CLAUSE_CHAIN (c) = list;
36123 return c;
36124 }
36125
36126 /* OpenMP 2.5:
36127 nowait */
36128
36129 static tree
36130 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
36131 tree list, location_t location)
36132 {
36133 tree c;
36134
36135 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
36136
36137 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
36138 OMP_CLAUSE_CHAIN (c) = list;
36139 return c;
36140 }
36141
36142 /* OpenMP 2.5:
36143 num_threads ( expression ) */
36144
36145 static tree
36146 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
36147 location_t location)
36148 {
36149 tree t, c;
36150
36151 matching_parens parens;
36152 if (!parens.require_open (parser))
36153 return list;
36154
36155 t = cp_parser_assignment_expression (parser);
36156
36157 if (t == error_mark_node
36158 || !parens.require_close (parser))
36159 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36160 /*or_comma=*/false,
36161 /*consume_paren=*/true);
36162
36163 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
36164 "num_threads", location);
36165
36166 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
36167 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
36168 OMP_CLAUSE_CHAIN (c) = list;
36169
36170 return c;
36171 }
36172
36173 /* OpenMP 4.5:
36174 num_tasks ( expression ) */
36175
36176 static tree
36177 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
36178 location_t location)
36179 {
36180 tree t, c;
36181
36182 matching_parens parens;
36183 if (!parens.require_open (parser))
36184 return list;
36185
36186 t = cp_parser_assignment_expression (parser);
36187
36188 if (t == error_mark_node
36189 || !parens.require_close (parser))
36190 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36191 /*or_comma=*/false,
36192 /*consume_paren=*/true);
36193
36194 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
36195 "num_tasks", location);
36196
36197 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
36198 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
36199 OMP_CLAUSE_CHAIN (c) = list;
36200
36201 return c;
36202 }
36203
36204 /* OpenMP 4.5:
36205 grainsize ( expression ) */
36206
36207 static tree
36208 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
36209 location_t location)
36210 {
36211 tree t, c;
36212
36213 matching_parens parens;
36214 if (!parens.require_open (parser))
36215 return list;
36216
36217 t = cp_parser_assignment_expression (parser);
36218
36219 if (t == error_mark_node
36220 || !parens.require_close (parser))
36221 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36222 /*or_comma=*/false,
36223 /*consume_paren=*/true);
36224
36225 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
36226 "grainsize", location);
36227
36228 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
36229 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
36230 OMP_CLAUSE_CHAIN (c) = list;
36231
36232 return c;
36233 }
36234
36235 /* OpenMP 4.5:
36236 priority ( expression ) */
36237
36238 static tree
36239 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
36240 location_t location)
36241 {
36242 tree t, c;
36243
36244 matching_parens parens;
36245 if (!parens.require_open (parser))
36246 return list;
36247
36248 t = cp_parser_assignment_expression (parser);
36249
36250 if (t == error_mark_node
36251 || !parens.require_close (parser))
36252 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36253 /*or_comma=*/false,
36254 /*consume_paren=*/true);
36255
36256 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
36257 "priority", location);
36258
36259 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
36260 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
36261 OMP_CLAUSE_CHAIN (c) = list;
36262
36263 return c;
36264 }
36265
36266 /* OpenMP 4.5:
36267 hint ( expression ) */
36268
36269 static tree
36270 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
36271 {
36272 tree t, c;
36273
36274 matching_parens parens;
36275 if (!parens.require_open (parser))
36276 return list;
36277
36278 t = cp_parser_assignment_expression (parser);
36279
36280 if (t != error_mark_node)
36281 {
36282 t = fold_non_dependent_expr (t);
36283 if (!value_dependent_expression_p (t)
36284 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
36285 || !tree_fits_shwi_p (t)
36286 || tree_int_cst_sgn (t) == -1))
36287 error_at (location, "expected constant integer expression with "
36288 "valid sync-hint value");
36289 }
36290 if (t == error_mark_node
36291 || !parens.require_close (parser))
36292 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36293 /*or_comma=*/false,
36294 /*consume_paren=*/true);
36295 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
36296
36297 c = build_omp_clause (location, OMP_CLAUSE_HINT);
36298 OMP_CLAUSE_HINT_EXPR (c) = t;
36299 OMP_CLAUSE_CHAIN (c) = list;
36300
36301 return c;
36302 }
36303
36304 /* OpenMP 4.5:
36305 defaultmap ( tofrom : scalar )
36306
36307 OpenMP 5.0:
36308 defaultmap ( implicit-behavior [ : variable-category ] ) */
36309
36310 static tree
36311 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
36312 location_t location)
36313 {
36314 tree c, id;
36315 const char *p;
36316 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
36317 enum omp_clause_defaultmap_kind category
36318 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
36319
36320 matching_parens parens;
36321 if (!parens.require_open (parser))
36322 return list;
36323
36324 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
36325 p = "default";
36326 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36327 {
36328 invalid_behavior:
36329 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
36330 "%<tofrom%>, %<firstprivate%>, %<none%> "
36331 "or %<default%>");
36332 goto out_err;
36333 }
36334 else
36335 {
36336 id = cp_lexer_peek_token (parser->lexer)->u.value;
36337 p = IDENTIFIER_POINTER (id);
36338 }
36339
36340 switch (p[0])
36341 {
36342 case 'a':
36343 if (strcmp ("alloc", p) == 0)
36344 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
36345 else
36346 goto invalid_behavior;
36347 break;
36348
36349 case 'd':
36350 if (strcmp ("default", p) == 0)
36351 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
36352 else
36353 goto invalid_behavior;
36354 break;
36355
36356 case 'f':
36357 if (strcmp ("firstprivate", p) == 0)
36358 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
36359 else if (strcmp ("from", p) == 0)
36360 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
36361 else
36362 goto invalid_behavior;
36363 break;
36364
36365 case 'n':
36366 if (strcmp ("none", p) == 0)
36367 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
36368 else
36369 goto invalid_behavior;
36370 break;
36371
36372 case 't':
36373 if (strcmp ("tofrom", p) == 0)
36374 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
36375 else if (strcmp ("to", p) == 0)
36376 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
36377 else
36378 goto invalid_behavior;
36379 break;
36380
36381 default:
36382 goto invalid_behavior;
36383 }
36384 cp_lexer_consume_token (parser->lexer);
36385
36386 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36387 {
36388 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36389 goto out_err;
36390
36391 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36392 {
36393 invalid_category:
36394 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
36395 "%<pointer%>");
36396 goto out_err;
36397 }
36398 id = cp_lexer_peek_token (parser->lexer)->u.value;
36399 p = IDENTIFIER_POINTER (id);
36400
36401 switch (p[0])
36402 {
36403 case 'a':
36404 if (strcmp ("aggregate", p) == 0)
36405 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
36406 else
36407 goto invalid_category;
36408 break;
36409
36410 case 'p':
36411 if (strcmp ("pointer", p) == 0)
36412 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
36413 else
36414 goto invalid_category;
36415 break;
36416
36417 case 's':
36418 if (strcmp ("scalar", p) == 0)
36419 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
36420 else
36421 goto invalid_category;
36422 break;
36423
36424 default:
36425 goto invalid_category;
36426 }
36427
36428 cp_lexer_consume_token (parser->lexer);
36429 }
36430 if (!parens.require_close (parser))
36431 goto out_err;
36432
36433 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
36434 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
36435 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
36436 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
36437 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
36438 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
36439 {
36440 enum omp_clause_defaultmap_kind cat = category;
36441 location_t loc = OMP_CLAUSE_LOCATION (c);
36442 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
36443 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
36444 p = NULL;
36445 switch (cat)
36446 {
36447 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
36448 p = NULL;
36449 break;
36450 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
36451 p = "aggregate";
36452 break;
36453 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
36454 p = "pointer";
36455 break;
36456 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
36457 p = "scalar";
36458 break;
36459 default:
36460 gcc_unreachable ();
36461 }
36462 if (p)
36463 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
36464 p);
36465 else
36466 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
36467 "category");
36468 break;
36469 }
36470
36471 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
36472 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
36473 OMP_CLAUSE_CHAIN (c) = list;
36474 return c;
36475
36476 out_err:
36477 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36478 /*or_comma=*/false,
36479 /*consume_paren=*/true);
36480 return list;
36481 }
36482
36483 /* OpenMP 5.0:
36484 order ( concurrent ) */
36485
36486 static tree
36487 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
36488 {
36489 tree c, id;
36490 const char *p;
36491
36492 matching_parens parens;
36493 if (!parens.require_open (parser))
36494 return list;
36495
36496 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36497 {
36498 cp_parser_error (parser, "expected %<concurrent%>");
36499 goto out_err;
36500 }
36501 else
36502 {
36503 id = cp_lexer_peek_token (parser->lexer)->u.value;
36504 p = IDENTIFIER_POINTER (id);
36505 }
36506 if (strcmp (p, "concurrent") != 0)
36507 {
36508 cp_parser_error (parser, "expected %<concurrent%>");
36509 goto out_err;
36510 }
36511 cp_lexer_consume_token (parser->lexer);
36512 if (!parens.require_close (parser))
36513 goto out_err;
36514
36515 /* check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location); */
36516 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
36517 OMP_CLAUSE_CHAIN (c) = list;
36518 return c;
36519
36520 out_err:
36521 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36522 /*or_comma=*/false,
36523 /*consume_paren=*/true);
36524 return list;
36525 }
36526
36527 /* OpenMP 5.0:
36528 bind ( teams | parallel | thread ) */
36529
36530 static tree
36531 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
36532 location_t location)
36533 {
36534 tree c;
36535 const char *p;
36536 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
36537
36538 matching_parens parens;
36539 if (!parens.require_open (parser))
36540 return list;
36541
36542 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36543 {
36544 invalid:
36545 cp_parser_error (parser,
36546 "expected %<teams%>, %<parallel%> or %<thread%>");
36547 goto out_err;
36548 }
36549 else
36550 {
36551 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36552 p = IDENTIFIER_POINTER (id);
36553 }
36554 if (strcmp (p, "teams") == 0)
36555 kind = OMP_CLAUSE_BIND_TEAMS;
36556 else if (strcmp (p, "parallel") == 0)
36557 kind = OMP_CLAUSE_BIND_PARALLEL;
36558 else if (strcmp (p, "thread") != 0)
36559 goto invalid;
36560 cp_lexer_consume_token (parser->lexer);
36561 if (!parens.require_close (parser))
36562 goto out_err;
36563
36564 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
36565 c = build_omp_clause (location, OMP_CLAUSE_BIND);
36566 OMP_CLAUSE_BIND_KIND (c) = kind;
36567 OMP_CLAUSE_CHAIN (c) = list;
36568 return c;
36569
36570 out_err:
36571 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36572 /*or_comma=*/false,
36573 /*consume_paren=*/true);
36574 return list;
36575 }
36576
36577 /* OpenMP 2.5:
36578 ordered
36579
36580 OpenMP 4.5:
36581 ordered ( constant-expression ) */
36582
36583 static tree
36584 cp_parser_omp_clause_ordered (cp_parser *parser,
36585 tree list, location_t location)
36586 {
36587 tree c, num = NULL_TREE;
36588 HOST_WIDE_INT n;
36589
36590 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
36591 "ordered", location);
36592
36593 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36594 {
36595 matching_parens parens;
36596 parens.consume_open (parser);
36597
36598 num = cp_parser_constant_expression (parser);
36599
36600 if (!parens.require_close (parser))
36601 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36602 /*or_comma=*/false,
36603 /*consume_paren=*/true);
36604
36605 if (num == error_mark_node)
36606 return list;
36607 num = fold_non_dependent_expr (num);
36608 if (!tree_fits_shwi_p (num)
36609 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
36610 || (n = tree_to_shwi (num)) <= 0
36611 || (int) n != n)
36612 {
36613 error_at (location,
36614 "ordered argument needs positive constant integer "
36615 "expression");
36616 return list;
36617 }
36618 }
36619
36620 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
36621 OMP_CLAUSE_ORDERED_EXPR (c) = num;
36622 OMP_CLAUSE_CHAIN (c) = list;
36623 return c;
36624 }
36625
36626 /* OpenMP 2.5:
36627 reduction ( reduction-operator : variable-list )
36628
36629 reduction-operator:
36630 One of: + * - & ^ | && ||
36631
36632 OpenMP 3.1:
36633
36634 reduction-operator:
36635 One of: + * - & ^ | && || min max
36636
36637 OpenMP 4.0:
36638
36639 reduction-operator:
36640 One of: + * - & ^ | && ||
36641 id-expression
36642
36643 OpenMP 5.0:
36644 reduction ( reduction-modifier, reduction-operator : variable-list )
36645 in_reduction ( reduction-operator : variable-list )
36646 task_reduction ( reduction-operator : variable-list ) */
36647
36648 static tree
36649 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
36650 bool is_omp, tree list)
36651 {
36652 enum tree_code code = ERROR_MARK;
36653 tree nlist, c, id = NULL_TREE;
36654 bool task = false;
36655 bool inscan = false;
36656
36657 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36658 return list;
36659
36660 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
36661 {
36662 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
36663 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
36664 {
36665 cp_lexer_consume_token (parser->lexer);
36666 cp_lexer_consume_token (parser->lexer);
36667 }
36668 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
36669 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
36670 {
36671 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36672 const char *p = IDENTIFIER_POINTER (id);
36673 if (strcmp (p, "task") == 0)
36674 task = true;
36675 else if (strcmp (p, "inscan") == 0)
36676 inscan = true;
36677 if (task || inscan)
36678 {
36679 cp_lexer_consume_token (parser->lexer);
36680 cp_lexer_consume_token (parser->lexer);
36681 }
36682 }
36683 }
36684
36685 switch (cp_lexer_peek_token (parser->lexer)->type)
36686 {
36687 case CPP_PLUS: code = PLUS_EXPR; break;
36688 case CPP_MULT: code = MULT_EXPR; break;
36689 case CPP_MINUS: code = MINUS_EXPR; break;
36690 case CPP_AND: code = BIT_AND_EXPR; break;
36691 case CPP_XOR: code = BIT_XOR_EXPR; break;
36692 case CPP_OR: code = BIT_IOR_EXPR; break;
36693 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
36694 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
36695 default: break;
36696 }
36697
36698 if (code != ERROR_MARK)
36699 cp_lexer_consume_token (parser->lexer);
36700 else
36701 {
36702 bool saved_colon_corrects_to_scope_p;
36703 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36704 parser->colon_corrects_to_scope_p = false;
36705 id = cp_parser_id_expression (parser, /*template_p=*/false,
36706 /*check_dependency_p=*/true,
36707 /*template_p=*/NULL,
36708 /*declarator_p=*/false,
36709 /*optional_p=*/false);
36710 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36711 if (identifier_p (id))
36712 {
36713 const char *p = IDENTIFIER_POINTER (id);
36714
36715 if (strcmp (p, "min") == 0)
36716 code = MIN_EXPR;
36717 else if (strcmp (p, "max") == 0)
36718 code = MAX_EXPR;
36719 else if (id == ovl_op_identifier (false, PLUS_EXPR))
36720 code = PLUS_EXPR;
36721 else if (id == ovl_op_identifier (false, MULT_EXPR))
36722 code = MULT_EXPR;
36723 else if (id == ovl_op_identifier (false, MINUS_EXPR))
36724 code = MINUS_EXPR;
36725 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
36726 code = BIT_AND_EXPR;
36727 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
36728 code = BIT_IOR_EXPR;
36729 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
36730 code = BIT_XOR_EXPR;
36731 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
36732 code = TRUTH_ANDIF_EXPR;
36733 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
36734 code = TRUTH_ORIF_EXPR;
36735 id = omp_reduction_id (code, id, NULL_TREE);
36736 tree scope = parser->scope;
36737 if (scope)
36738 id = build_qualified_name (NULL_TREE, scope, id, false);
36739 parser->scope = NULL_TREE;
36740 parser->qualifying_scope = NULL_TREE;
36741 parser->object_scope = NULL_TREE;
36742 }
36743 else
36744 {
36745 error ("invalid reduction-identifier");
36746 resync_fail:
36747 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36748 /*or_comma=*/false,
36749 /*consume_paren=*/true);
36750 return list;
36751 }
36752 }
36753
36754 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36755 goto resync_fail;
36756
36757 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
36758 NULL);
36759 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36760 {
36761 OMP_CLAUSE_REDUCTION_CODE (c) = code;
36762 if (task)
36763 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
36764 else if (inscan)
36765 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
36766 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
36767 }
36768
36769 return nlist;
36770 }
36771
36772 /* OpenMP 2.5:
36773 schedule ( schedule-kind )
36774 schedule ( schedule-kind , expression )
36775
36776 schedule-kind:
36777 static | dynamic | guided | runtime | auto
36778
36779 OpenMP 4.5:
36780 schedule ( schedule-modifier : schedule-kind )
36781 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
36782
36783 schedule-modifier:
36784 simd
36785 monotonic
36786 nonmonotonic */
36787
36788 static tree
36789 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
36790 {
36791 tree c, t;
36792 int modifiers = 0, nmodifiers = 0;
36793
36794 matching_parens parens;
36795 if (!parens.require_open (parser))
36796 return list;
36797
36798 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
36799
36800 location_t comma = UNKNOWN_LOCATION;
36801 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36802 {
36803 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36804 const char *p = IDENTIFIER_POINTER (id);
36805 if (strcmp ("simd", p) == 0)
36806 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
36807 else if (strcmp ("monotonic", p) == 0)
36808 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
36809 else if (strcmp ("nonmonotonic", p) == 0)
36810 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
36811 else
36812 break;
36813 comma = UNKNOWN_LOCATION;
36814 cp_lexer_consume_token (parser->lexer);
36815 if (nmodifiers++ == 0
36816 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36817 {
36818 comma = cp_lexer_peek_token (parser->lexer)->location;
36819 cp_lexer_consume_token (parser->lexer);
36820 }
36821 else
36822 {
36823 cp_parser_require (parser, CPP_COLON, RT_COLON);
36824 break;
36825 }
36826 }
36827 if (comma != UNKNOWN_LOCATION)
36828 error_at (comma, "expected %<:%>");
36829
36830 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36831 {
36832 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36833 const char *p = IDENTIFIER_POINTER (id);
36834
36835 switch (p[0])
36836 {
36837 case 'd':
36838 if (strcmp ("dynamic", p) != 0)
36839 goto invalid_kind;
36840 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
36841 break;
36842
36843 case 'g':
36844 if (strcmp ("guided", p) != 0)
36845 goto invalid_kind;
36846 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
36847 break;
36848
36849 case 'r':
36850 if (strcmp ("runtime", p) != 0)
36851 goto invalid_kind;
36852 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
36853 break;
36854
36855 default:
36856 goto invalid_kind;
36857 }
36858 }
36859 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
36860 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
36861 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
36862 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
36863 else
36864 goto invalid_kind;
36865 cp_lexer_consume_token (parser->lexer);
36866
36867 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
36868 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
36869 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
36870 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
36871 {
36872 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
36873 "specified");
36874 modifiers = 0;
36875 }
36876
36877 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36878 {
36879 cp_token *token;
36880 cp_lexer_consume_token (parser->lexer);
36881
36882 token = cp_lexer_peek_token (parser->lexer);
36883 t = cp_parser_assignment_expression (parser);
36884
36885 if (t == error_mark_node)
36886 goto resync_fail;
36887 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
36888 error_at (token->location, "schedule %<runtime%> does not take "
36889 "a %<chunk_size%> parameter");
36890 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
36891 error_at (token->location, "schedule %<auto%> does not take "
36892 "a %<chunk_size%> parameter");
36893 else
36894 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
36895
36896 if (!parens.require_close (parser))
36897 goto resync_fail;
36898 }
36899 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
36900 goto resync_fail;
36901
36902 OMP_CLAUSE_SCHEDULE_KIND (c)
36903 = (enum omp_clause_schedule_kind)
36904 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
36905
36906 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
36907 OMP_CLAUSE_CHAIN (c) = list;
36908 return c;
36909
36910 invalid_kind:
36911 cp_parser_error (parser, "invalid schedule kind");
36912 resync_fail:
36913 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36914 /*or_comma=*/false,
36915 /*consume_paren=*/true);
36916 return list;
36917 }
36918
36919 /* OpenMP 3.0:
36920 untied */
36921
36922 static tree
36923 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
36924 tree list, location_t location)
36925 {
36926 tree c;
36927
36928 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
36929
36930 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
36931 OMP_CLAUSE_CHAIN (c) = list;
36932 return c;
36933 }
36934
36935 /* OpenMP 4.0:
36936 inbranch
36937 notinbranch */
36938
36939 static tree
36940 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
36941 tree list, location_t location)
36942 {
36943 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
36944 tree c = build_omp_clause (location, code);
36945 OMP_CLAUSE_CHAIN (c) = list;
36946 return c;
36947 }
36948
36949 /* OpenMP 4.0:
36950 parallel
36951 for
36952 sections
36953 taskgroup */
36954
36955 static tree
36956 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
36957 enum omp_clause_code code,
36958 tree list, location_t location)
36959 {
36960 tree c = build_omp_clause (location, code);
36961 OMP_CLAUSE_CHAIN (c) = list;
36962 return c;
36963 }
36964
36965 /* OpenMP 4.5:
36966 nogroup */
36967
36968 static tree
36969 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
36970 tree list, location_t location)
36971 {
36972 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
36973 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
36974 OMP_CLAUSE_CHAIN (c) = list;
36975 return c;
36976 }
36977
36978 /* OpenMP 4.5:
36979 simd
36980 threads */
36981
36982 static tree
36983 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
36984 enum omp_clause_code code,
36985 tree list, location_t location)
36986 {
36987 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
36988 tree c = build_omp_clause (location, code);
36989 OMP_CLAUSE_CHAIN (c) = list;
36990 return c;
36991 }
36992
36993 /* OpenMP 4.0:
36994 num_teams ( expression ) */
36995
36996 static tree
36997 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
36998 location_t location)
36999 {
37000 tree t, c;
37001
37002 matching_parens parens;
37003 if (!parens.require_open (parser))
37004 return list;
37005
37006 t = cp_parser_assignment_expression (parser);
37007
37008 if (t == error_mark_node
37009 || !parens.require_close (parser))
37010 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37011 /*or_comma=*/false,
37012 /*consume_paren=*/true);
37013
37014 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
37015 "num_teams", location);
37016
37017 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
37018 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
37019 OMP_CLAUSE_CHAIN (c) = list;
37020
37021 return c;
37022 }
37023
37024 /* OpenMP 4.0:
37025 thread_limit ( expression ) */
37026
37027 static tree
37028 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
37029 location_t location)
37030 {
37031 tree t, c;
37032
37033 matching_parens parens;
37034 if (!parens.require_open (parser))
37035 return list;
37036
37037 t = cp_parser_assignment_expression (parser);
37038
37039 if (t == error_mark_node
37040 || !parens.require_close (parser))
37041 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37042 /*or_comma=*/false,
37043 /*consume_paren=*/true);
37044
37045 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
37046 "thread_limit", location);
37047
37048 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
37049 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
37050 OMP_CLAUSE_CHAIN (c) = list;
37051
37052 return c;
37053 }
37054
37055 /* OpenMP 4.0:
37056 aligned ( variable-list )
37057 aligned ( variable-list : constant-expression ) */
37058
37059 static tree
37060 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
37061 {
37062 tree nlist, c, alignment = NULL_TREE;
37063 bool colon;
37064
37065 matching_parens parens;
37066 if (!parens.require_open (parser))
37067 return list;
37068
37069 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
37070 &colon);
37071
37072 if (colon)
37073 {
37074 alignment = cp_parser_constant_expression (parser);
37075
37076 if (!parens.require_close (parser))
37077 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37078 /*or_comma=*/false,
37079 /*consume_paren=*/true);
37080
37081 if (alignment == error_mark_node)
37082 alignment = NULL_TREE;
37083 }
37084
37085 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
37086 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
37087
37088 return nlist;
37089 }
37090
37091 /* OpenMP 5.0:
37092 allocate ( variable-list )
37093 allocate ( expression : variable-list ) */
37094
37095 static tree
37096 cp_parser_omp_clause_allocate (cp_parser *parser, tree list)
37097 {
37098 tree nlist, c, allocator = NULL_TREE;
37099 bool colon;
37100
37101 matching_parens parens;
37102 if (!parens.require_open (parser))
37103 return list;
37104
37105 cp_parser_parse_tentatively (parser);
37106 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37107 parser->colon_corrects_to_scope_p = false;
37108 allocator = cp_parser_assignment_expression (parser);
37109 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37110 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
37111 {
37112 cp_parser_parse_definitely (parser);
37113 cp_lexer_consume_token (parser->lexer);
37114 if (allocator == error_mark_node)
37115 allocator = NULL_TREE;
37116 }
37117 else
37118 {
37119 cp_parser_abort_tentative_parse (parser);
37120 allocator = NULL_TREE;
37121 }
37122
37123 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALLOCATE, list,
37124 &colon);
37125
37126 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
37127 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
37128
37129 return nlist;
37130 }
37131
37132 /* OpenMP 2.5:
37133 lastprivate ( variable-list )
37134
37135 OpenMP 5.0:
37136 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
37137
37138 static tree
37139 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
37140 {
37141 bool conditional = false;
37142
37143 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37144 return list;
37145
37146 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37147 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
37148 {
37149 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37150 const char *p = IDENTIFIER_POINTER (id);
37151
37152 if (strcmp ("conditional", p) == 0)
37153 {
37154 conditional = true;
37155 cp_lexer_consume_token (parser->lexer);
37156 cp_lexer_consume_token (parser->lexer);
37157 }
37158 }
37159
37160 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
37161 list, NULL);
37162
37163 if (conditional)
37164 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
37165 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
37166 return nlist;
37167 }
37168
37169 /* OpenMP 4.0:
37170 linear ( variable-list )
37171 linear ( variable-list : expression )
37172
37173 OpenMP 4.5:
37174 linear ( modifier ( variable-list ) )
37175 linear ( modifier ( variable-list ) : expression ) */
37176
37177 static tree
37178 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
37179 bool declare_simd)
37180 {
37181 tree nlist, c, step = integer_one_node;
37182 bool colon;
37183 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
37184
37185 matching_parens parens;
37186 if (!parens.require_open (parser))
37187 return list;
37188
37189 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37190 {
37191 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37192 const char *p = IDENTIFIER_POINTER (id);
37193
37194 if (strcmp ("ref", p) == 0)
37195 kind = OMP_CLAUSE_LINEAR_REF;
37196 else if (strcmp ("val", p) == 0)
37197 kind = OMP_CLAUSE_LINEAR_VAL;
37198 else if (strcmp ("uval", p) == 0)
37199 kind = OMP_CLAUSE_LINEAR_UVAL;
37200 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
37201 cp_lexer_consume_token (parser->lexer);
37202 else
37203 kind = OMP_CLAUSE_LINEAR_DEFAULT;
37204 }
37205
37206 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
37207 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
37208 &colon);
37209 else
37210 {
37211 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
37212 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
37213 if (colon)
37214 cp_parser_require (parser, CPP_COLON, RT_COLON);
37215 else if (!parens.require_close (parser))
37216 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37217 /*or_comma=*/false,
37218 /*consume_paren=*/true);
37219 }
37220
37221 if (colon)
37222 {
37223 step = NULL_TREE;
37224 if (declare_simd
37225 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37226 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
37227 {
37228 cp_token *token = cp_lexer_peek_token (parser->lexer);
37229 cp_parser_parse_tentatively (parser);
37230 step = cp_parser_id_expression (parser, /*template_p=*/false,
37231 /*check_dependency_p=*/true,
37232 /*template_p=*/NULL,
37233 /*declarator_p=*/false,
37234 /*optional_p=*/false);
37235 if (step != error_mark_node)
37236 step = cp_parser_lookup_name_simple (parser, step, token->location);
37237 if (step == error_mark_node)
37238 {
37239 step = NULL_TREE;
37240 cp_parser_abort_tentative_parse (parser);
37241 }
37242 else if (!cp_parser_parse_definitely (parser))
37243 step = NULL_TREE;
37244 }
37245 if (!step)
37246 step = cp_parser_assignment_expression (parser);
37247
37248 if (!parens.require_close (parser))
37249 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37250 /*or_comma=*/false,
37251 /*consume_paren=*/true);
37252
37253 if (step == error_mark_node)
37254 return list;
37255 }
37256
37257 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
37258 {
37259 OMP_CLAUSE_LINEAR_STEP (c) = step;
37260 OMP_CLAUSE_LINEAR_KIND (c) = kind;
37261 }
37262
37263 return nlist;
37264 }
37265
37266 /* OpenMP 4.0:
37267 safelen ( constant-expression ) */
37268
37269 static tree
37270 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
37271 location_t location)
37272 {
37273 tree t, c;
37274
37275 matching_parens parens;
37276 if (!parens.require_open (parser))
37277 return list;
37278
37279 t = cp_parser_constant_expression (parser);
37280
37281 if (t == error_mark_node
37282 || !parens.require_close (parser))
37283 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37284 /*or_comma=*/false,
37285 /*consume_paren=*/true);
37286
37287 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
37288
37289 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
37290 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
37291 OMP_CLAUSE_CHAIN (c) = list;
37292
37293 return c;
37294 }
37295
37296 /* OpenMP 4.0:
37297 simdlen ( constant-expression ) */
37298
37299 static tree
37300 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
37301 location_t location)
37302 {
37303 tree t, c;
37304
37305 matching_parens parens;
37306 if (!parens.require_open (parser))
37307 return list;
37308
37309 t = cp_parser_constant_expression (parser);
37310
37311 if (t == error_mark_node
37312 || !parens.require_close (parser))
37313 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37314 /*or_comma=*/false,
37315 /*consume_paren=*/true);
37316
37317 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
37318
37319 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
37320 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
37321 OMP_CLAUSE_CHAIN (c) = list;
37322
37323 return c;
37324 }
37325
37326 /* OpenMP 4.5:
37327 vec:
37328 identifier [+/- integer]
37329 vec , identifier [+/- integer]
37330 */
37331
37332 static tree
37333 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
37334 tree list)
37335 {
37336 tree vec = NULL;
37337
37338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37339 {
37340 cp_parser_error (parser, "expected identifier");
37341 return list;
37342 }
37343
37344 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37345 {
37346 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
37347 tree t, identifier = cp_parser_identifier (parser);
37348 tree addend = NULL;
37349
37350 if (identifier == error_mark_node)
37351 t = error_mark_node;
37352 else
37353 {
37354 t = cp_parser_lookup_name_simple
37355 (parser, identifier,
37356 cp_lexer_peek_token (parser->lexer)->location);
37357 if (t == error_mark_node)
37358 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
37359 id_loc);
37360 }
37361
37362 bool neg = false;
37363 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
37364 neg = true;
37365 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
37366 {
37367 addend = integer_zero_node;
37368 goto add_to_vector;
37369 }
37370 cp_lexer_consume_token (parser->lexer);
37371
37372 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
37373 {
37374 cp_parser_error (parser, "expected integer");
37375 return list;
37376 }
37377
37378 addend = cp_lexer_peek_token (parser->lexer)->u.value;
37379 if (TREE_CODE (addend) != INTEGER_CST)
37380 {
37381 cp_parser_error (parser, "expected integer");
37382 return list;
37383 }
37384 cp_lexer_consume_token (parser->lexer);
37385
37386 add_to_vector:
37387 if (t != error_mark_node)
37388 {
37389 vec = tree_cons (addend, t, vec);
37390 if (neg)
37391 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
37392 }
37393
37394 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
37395 break;
37396
37397 cp_lexer_consume_token (parser->lexer);
37398 }
37399
37400 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
37401 {
37402 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
37403 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
37404 OMP_CLAUSE_DECL (u) = nreverse (vec);
37405 OMP_CLAUSE_CHAIN (u) = list;
37406 return u;
37407 }
37408 return list;
37409 }
37410
37411 /* OpenMP 5.0:
37412 detach ( event-handle ) */
37413
37414 static tree
37415 cp_parser_omp_clause_detach (cp_parser *parser, tree list)
37416 {
37417 matching_parens parens;
37418
37419 if (!parens.require_open (parser))
37420 return list;
37421
37422 cp_token *token;
37423 tree name, decl;
37424
37425 token = cp_lexer_peek_token (parser->lexer);
37426 name = cp_parser_id_expression (parser, /*template_p=*/false,
37427 /*check_dependency_p=*/true,
37428 /*template_p=*/NULL,
37429 /*declarator_p=*/false,
37430 /*optional_p=*/false);
37431 if (name == error_mark_node)
37432 decl = error_mark_node;
37433 else
37434 {
37435 if (identifier_p (name))
37436 decl = cp_parser_lookup_name_simple (parser, name, token->location);
37437 else
37438 decl = name;
37439 if (decl == error_mark_node)
37440 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
37441 token->location);
37442 }
37443
37444 if (decl == error_mark_node
37445 || !parens.require_close (parser))
37446 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37447 /*or_comma=*/false,
37448 /*consume_paren=*/true);
37449
37450 tree u = build_omp_clause (token->location, OMP_CLAUSE_DETACH);
37451 OMP_CLAUSE_DECL (u) = decl;
37452 OMP_CLAUSE_CHAIN (u) = list;
37453
37454 return u;
37455 }
37456
37457 /* OpenMP 5.0:
37458 iterators ( iterators-definition )
37459
37460 iterators-definition:
37461 iterator-specifier
37462 iterator-specifier , iterators-definition
37463
37464 iterator-specifier:
37465 identifier = range-specification
37466 iterator-type identifier = range-specification
37467
37468 range-specification:
37469 begin : end
37470 begin : end : step */
37471
37472 static tree
37473 cp_parser_omp_iterators (cp_parser *parser)
37474 {
37475 tree ret = NULL_TREE, *last = &ret;
37476 cp_lexer_consume_token (parser->lexer);
37477
37478 matching_parens parens;
37479 if (!parens.require_open (parser))
37480 return error_mark_node;
37481
37482 bool saved_colon_corrects_to_scope_p
37483 = parser->colon_corrects_to_scope_p;
37484 bool saved_colon_doesnt_start_class_def_p
37485 = parser->colon_doesnt_start_class_def_p;
37486
37487 do
37488 {
37489 tree iter_type;
37490 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37491 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
37492 iter_type = integer_type_node;
37493 else
37494 {
37495 const char *saved_message
37496 = parser->type_definition_forbidden_message;
37497 parser->type_definition_forbidden_message
37498 = G_("types may not be defined in iterator type");
37499
37500 iter_type = cp_parser_type_id (parser);
37501
37502 parser->type_definition_forbidden_message = saved_message;
37503 }
37504
37505 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37506 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37507 {
37508 cp_parser_error (parser, "expected identifier");
37509 break;
37510 }
37511
37512 tree id = cp_parser_identifier (parser);
37513 if (id == error_mark_node)
37514 break;
37515
37516 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
37517 break;
37518
37519 parser->colon_corrects_to_scope_p = false;
37520 parser->colon_doesnt_start_class_def_p = true;
37521 tree begin = cp_parser_assignment_expression (parser);
37522
37523 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37524 break;
37525
37526 tree end = cp_parser_assignment_expression (parser);
37527
37528 tree step = integer_one_node;
37529 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
37530 {
37531 cp_lexer_consume_token (parser->lexer);
37532 step = cp_parser_assignment_expression (parser);
37533 }
37534
37535 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
37536 DECL_ARTIFICIAL (iter_var) = 1;
37537 DECL_CONTEXT (iter_var) = current_function_decl;
37538 pushdecl (iter_var);
37539
37540 *last = make_tree_vec (6);
37541 TREE_VEC_ELT (*last, 0) = iter_var;
37542 TREE_VEC_ELT (*last, 1) = begin;
37543 TREE_VEC_ELT (*last, 2) = end;
37544 TREE_VEC_ELT (*last, 3) = step;
37545 last = &TREE_CHAIN (*last);
37546
37547 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37548 {
37549 cp_lexer_consume_token (parser->lexer);
37550 continue;
37551 }
37552 break;
37553 }
37554 while (1);
37555
37556 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37557 parser->colon_doesnt_start_class_def_p
37558 = saved_colon_doesnt_start_class_def_p;
37559
37560 if (!parens.require_close (parser))
37561 cp_parser_skip_to_closing_parenthesis (parser,
37562 /*recovering=*/true,
37563 /*or_comma=*/false,
37564 /*consume_paren=*/true);
37565
37566 return ret ? ret : error_mark_node;
37567 }
37568
37569 /* OpenMP 4.0:
37570 depend ( depend-kind : variable-list )
37571
37572 depend-kind:
37573 in | out | inout
37574
37575 OpenMP 4.5:
37576 depend ( source )
37577
37578 depend ( sink : vec )
37579
37580 OpenMP 5.0:
37581 depend ( depend-modifier , depend-kind: variable-list )
37582
37583 depend-kind:
37584 in | out | inout | mutexinoutset | depobj
37585
37586 depend-modifier:
37587 iterator ( iterators-definition ) */
37588
37589 static tree
37590 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
37591 {
37592 tree nlist, c, iterators = NULL_TREE;
37593 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
37594
37595 matching_parens parens;
37596 if (!parens.require_open (parser))
37597 return list;
37598
37599 do
37600 {
37601 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37602 goto invalid_kind;
37603
37604 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37605 const char *p = IDENTIFIER_POINTER (id);
37606
37607 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
37608 {
37609 begin_scope (sk_omp, NULL);
37610 iterators = cp_parser_omp_iterators (parser);
37611 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
37612 continue;
37613 }
37614 if (strcmp ("in", p) == 0)
37615 kind = OMP_CLAUSE_DEPEND_IN;
37616 else if (strcmp ("inout", p) == 0)
37617 kind = OMP_CLAUSE_DEPEND_INOUT;
37618 else if (strcmp ("mutexinoutset", p) == 0)
37619 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
37620 else if (strcmp ("out", p) == 0)
37621 kind = OMP_CLAUSE_DEPEND_OUT;
37622 else if (strcmp ("depobj", p) == 0)
37623 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
37624 else if (strcmp ("sink", p) == 0)
37625 kind = OMP_CLAUSE_DEPEND_SINK;
37626 else if (strcmp ("source", p) == 0)
37627 kind = OMP_CLAUSE_DEPEND_SOURCE;
37628 else
37629 goto invalid_kind;
37630 break;
37631 }
37632 while (1);
37633
37634 cp_lexer_consume_token (parser->lexer);
37635
37636 if (iterators
37637 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
37638 {
37639 poplevel (0, 1, 0);
37640 error_at (loc, "%<iterator%> modifier incompatible with %qs",
37641 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
37642 iterators = NULL_TREE;
37643 }
37644
37645 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
37646 {
37647 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
37648 OMP_CLAUSE_DEPEND_KIND (c) = kind;
37649 OMP_CLAUSE_DECL (c) = NULL_TREE;
37650 OMP_CLAUSE_CHAIN (c) = list;
37651 if (!parens.require_close (parser))
37652 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37653 /*or_comma=*/false,
37654 /*consume_paren=*/true);
37655 return c;
37656 }
37657
37658 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37659 goto resync_fail;
37660
37661 if (kind == OMP_CLAUSE_DEPEND_SINK)
37662 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
37663 else
37664 {
37665 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
37666 list, NULL);
37667
37668 if (iterators)
37669 {
37670 tree block = poplevel (1, 1, 0);
37671 if (iterators == error_mark_node)
37672 iterators = NULL_TREE;
37673 else
37674 TREE_VEC_ELT (iterators, 5) = block;
37675 }
37676
37677 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
37678 {
37679 OMP_CLAUSE_DEPEND_KIND (c) = kind;
37680 if (iterators)
37681 OMP_CLAUSE_DECL (c)
37682 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
37683 }
37684 }
37685 return nlist;
37686
37687 invalid_kind:
37688 cp_parser_error (parser, "invalid depend kind");
37689 resync_fail:
37690 if (iterators)
37691 poplevel (0, 1, 0);
37692 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37693 /*or_comma=*/false,
37694 /*consume_paren=*/true);
37695 return list;
37696 }
37697
37698 /* OpenMP 4.0:
37699 map ( map-kind : variable-list )
37700 map ( variable-list )
37701
37702 map-kind:
37703 alloc | to | from | tofrom
37704
37705 OpenMP 4.5:
37706 map-kind:
37707 alloc | to | from | tofrom | release | delete
37708
37709 map ( always [,] map-kind: variable-list ) */
37710
37711 static tree
37712 cp_parser_omp_clause_map (cp_parser *parser, tree list)
37713 {
37714 tree nlist, c;
37715 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
37716 bool always = false;
37717
37718 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37719 return list;
37720
37721 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37722 {
37723 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37724 const char *p = IDENTIFIER_POINTER (id);
37725
37726 if (strcmp ("always", p) == 0)
37727 {
37728 int nth = 2;
37729 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
37730 nth++;
37731 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
37732 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
37733 == RID_DELETE))
37734 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
37735 == CPP_COLON))
37736 {
37737 always = true;
37738 cp_lexer_consume_token (parser->lexer);
37739 if (nth == 3)
37740 cp_lexer_consume_token (parser->lexer);
37741 }
37742 }
37743 }
37744
37745 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37746 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
37747 {
37748 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37749 const char *p = IDENTIFIER_POINTER (id);
37750
37751 if (strcmp ("alloc", p) == 0)
37752 kind = GOMP_MAP_ALLOC;
37753 else if (strcmp ("to", p) == 0)
37754 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
37755 else if (strcmp ("from", p) == 0)
37756 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
37757 else if (strcmp ("tofrom", p) == 0)
37758 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
37759 else if (strcmp ("release", p) == 0)
37760 kind = GOMP_MAP_RELEASE;
37761 else
37762 {
37763 cp_parser_error (parser, "invalid map kind");
37764 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37765 /*or_comma=*/false,
37766 /*consume_paren=*/true);
37767 return list;
37768 }
37769 cp_lexer_consume_token (parser->lexer);
37770 cp_lexer_consume_token (parser->lexer);
37771 }
37772 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
37773 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
37774 {
37775 kind = GOMP_MAP_DELETE;
37776 cp_lexer_consume_token (parser->lexer);
37777 cp_lexer_consume_token (parser->lexer);
37778 }
37779
37780 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
37781 NULL);
37782
37783 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
37784 OMP_CLAUSE_SET_MAP_KIND (c, kind);
37785
37786 return nlist;
37787 }
37788
37789 /* OpenMP 4.0:
37790 device ( expression ) */
37791
37792 static tree
37793 cp_parser_omp_clause_device (cp_parser *parser, tree list,
37794 location_t location)
37795 {
37796 tree t, c;
37797
37798 matching_parens parens;
37799 if (!parens.require_open (parser))
37800 return list;
37801
37802 t = cp_parser_assignment_expression (parser);
37803
37804 if (t == error_mark_node
37805 || !parens.require_close (parser))
37806 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37807 /*or_comma=*/false,
37808 /*consume_paren=*/true);
37809
37810 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
37811 "device", location);
37812
37813 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
37814 OMP_CLAUSE_DEVICE_ID (c) = t;
37815 OMP_CLAUSE_CHAIN (c) = list;
37816
37817 return c;
37818 }
37819
37820 /* OpenMP 4.0:
37821 dist_schedule ( static )
37822 dist_schedule ( static , expression ) */
37823
37824 static tree
37825 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
37826 location_t location)
37827 {
37828 tree c, t;
37829
37830 matching_parens parens;
37831 if (!parens.require_open (parser))
37832 return list;
37833
37834 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
37835
37836 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
37837 goto invalid_kind;
37838 cp_lexer_consume_token (parser->lexer);
37839
37840 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37841 {
37842 cp_lexer_consume_token (parser->lexer);
37843
37844 t = cp_parser_assignment_expression (parser);
37845
37846 if (t == error_mark_node)
37847 goto resync_fail;
37848 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
37849
37850 if (!parens.require_close (parser))
37851 goto resync_fail;
37852 }
37853 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
37854 goto resync_fail;
37855
37856 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
37857 "dist_schedule", location); */
37858 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
37859 warning_at (location, 0, "too many %qs clauses", "dist_schedule");
37860 OMP_CLAUSE_CHAIN (c) = list;
37861 return c;
37862
37863 invalid_kind:
37864 cp_parser_error (parser, "invalid dist_schedule kind");
37865 resync_fail:
37866 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37867 /*or_comma=*/false,
37868 /*consume_paren=*/true);
37869 return list;
37870 }
37871
37872 /* OpenMP 4.0:
37873 proc_bind ( proc-bind-kind )
37874
37875 proc-bind-kind:
37876 master | close | spread */
37877
37878 static tree
37879 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
37880 location_t location)
37881 {
37882 tree c;
37883 enum omp_clause_proc_bind_kind kind;
37884
37885 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37886 return list;
37887
37888 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37889 {
37890 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37891 const char *p = IDENTIFIER_POINTER (id);
37892
37893 if (strcmp ("master", p) == 0)
37894 kind = OMP_CLAUSE_PROC_BIND_MASTER;
37895 else if (strcmp ("close", p) == 0)
37896 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
37897 else if (strcmp ("spread", p) == 0)
37898 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
37899 else
37900 goto invalid_kind;
37901 }
37902 else
37903 goto invalid_kind;
37904
37905 cp_lexer_consume_token (parser->lexer);
37906 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
37907 goto resync_fail;
37908
37909 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
37910 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
37911 location);
37912 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
37913 OMP_CLAUSE_CHAIN (c) = list;
37914 return c;
37915
37916 invalid_kind:
37917 cp_parser_error (parser, "invalid depend kind");
37918 resync_fail:
37919 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37920 /*or_comma=*/false,
37921 /*consume_paren=*/true);
37922 return list;
37923 }
37924
37925 /* OpenMP 5.0:
37926 device_type ( host | nohost | any ) */
37927
37928 static tree
37929 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
37930 location_t location)
37931 {
37932 tree c;
37933 enum omp_clause_device_type_kind kind;
37934
37935 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37936 return list;
37937
37938 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37939 {
37940 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37941 const char *p = IDENTIFIER_POINTER (id);
37942
37943 if (strcmp ("host", p) == 0)
37944 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
37945 else if (strcmp ("nohost", p) == 0)
37946 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
37947 else if (strcmp ("any", p) == 0)
37948 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
37949 else
37950 goto invalid_kind;
37951 }
37952 else
37953 goto invalid_kind;
37954
37955 cp_lexer_consume_token (parser->lexer);
37956 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
37957 goto resync_fail;
37958
37959 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
37960 /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
37961 location); */
37962 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
37963 OMP_CLAUSE_CHAIN (c) = list;
37964 return c;
37965
37966 invalid_kind:
37967 cp_parser_error (parser, "invalid depend kind");
37968 resync_fail:
37969 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37970 /*or_comma=*/false,
37971 /*consume_paren=*/true);
37972 return list;
37973 }
37974
37975 /* OpenACC:
37976 async [( int-expr )] */
37977
37978 static tree
37979 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
37980 {
37981 tree c, t;
37982 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37983
37984 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
37985
37986 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37987 {
37988 matching_parens parens;
37989 parens.consume_open (parser);
37990
37991 t = cp_parser_expression (parser);
37992 if (t == error_mark_node
37993 || !parens.require_close (parser))
37994 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37995 /*or_comma=*/false,
37996 /*consume_paren=*/true);
37997 }
37998
37999 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
38000
38001 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
38002 OMP_CLAUSE_ASYNC_EXPR (c) = t;
38003 OMP_CLAUSE_CHAIN (c) = list;
38004 list = c;
38005
38006 return list;
38007 }
38008
38009 /* Parse all OpenACC clauses. The set clauses allowed by the directive
38010 is a bitmask in MASK. Return the list of clauses found. */
38011
38012 static tree
38013 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
38014 const char *where, cp_token *pragma_tok,
38015 bool finish_p = true)
38016 {
38017 tree clauses = NULL;
38018 bool first = true;
38019
38020 /* Don't create location wrapper nodes within OpenACC clauses. */
38021 auto_suppress_location_wrappers sentinel;
38022
38023 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38024 {
38025 location_t here;
38026 pragma_omp_clause c_kind;
38027 omp_clause_code code;
38028 const char *c_name;
38029 tree prev = clauses;
38030
38031 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38032 cp_lexer_consume_token (parser->lexer);
38033
38034 here = cp_lexer_peek_token (parser->lexer)->location;
38035 c_kind = cp_parser_omp_clause_name (parser);
38036
38037 switch (c_kind)
38038 {
38039 case PRAGMA_OACC_CLAUSE_ASYNC:
38040 clauses = cp_parser_oacc_clause_async (parser, clauses);
38041 c_name = "async";
38042 break;
38043 case PRAGMA_OACC_CLAUSE_AUTO:
38044 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
38045 clauses);
38046 c_name = "auto";
38047 break;
38048 case PRAGMA_OACC_CLAUSE_ATTACH:
38049 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38050 c_name = "attach";
38051 break;
38052 case PRAGMA_OACC_CLAUSE_COLLAPSE:
38053 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
38054 c_name = "collapse";
38055 break;
38056 case PRAGMA_OACC_CLAUSE_COPY:
38057 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38058 c_name = "copy";
38059 break;
38060 case PRAGMA_OACC_CLAUSE_COPYIN:
38061 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38062 c_name = "copyin";
38063 break;
38064 case PRAGMA_OACC_CLAUSE_COPYOUT:
38065 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38066 c_name = "copyout";
38067 break;
38068 case PRAGMA_OACC_CLAUSE_CREATE:
38069 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38070 c_name = "create";
38071 break;
38072 case PRAGMA_OACC_CLAUSE_DELETE:
38073 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38074 c_name = "delete";
38075 break;
38076 case PRAGMA_OMP_CLAUSE_DEFAULT:
38077 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
38078 c_name = "default";
38079 break;
38080 case PRAGMA_OACC_CLAUSE_DETACH:
38081 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38082 c_name = "detach";
38083 break;
38084 case PRAGMA_OACC_CLAUSE_DEVICE:
38085 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38086 c_name = "device";
38087 break;
38088 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
38089 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
38090 c_name = "deviceptr";
38091 break;
38092 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
38093 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38094 c_name = "device_resident";
38095 break;
38096 case PRAGMA_OACC_CLAUSE_FINALIZE:
38097 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
38098 clauses);
38099 c_name = "finalize";
38100 break;
38101 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
38102 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38103 clauses);
38104 c_name = "firstprivate";
38105 break;
38106 case PRAGMA_OACC_CLAUSE_GANG:
38107 c_name = "gang";
38108 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
38109 c_name, clauses);
38110 break;
38111 case PRAGMA_OACC_CLAUSE_HOST:
38112 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38113 c_name = "host";
38114 break;
38115 case PRAGMA_OACC_CLAUSE_IF:
38116 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
38117 c_name = "if";
38118 break;
38119 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
38120 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
38121 clauses);
38122 c_name = "if_present";
38123 break;
38124 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
38125 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
38126 clauses);
38127 c_name = "independent";
38128 break;
38129 case PRAGMA_OACC_CLAUSE_LINK:
38130 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38131 c_name = "link";
38132 break;
38133 case PRAGMA_OACC_CLAUSE_NO_CREATE:
38134 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38135 c_name = "no_create";
38136 break;
38137 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
38138 code = OMP_CLAUSE_NUM_GANGS;
38139 c_name = "num_gangs";
38140 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
38141 clauses);
38142 break;
38143 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
38144 c_name = "num_workers";
38145 code = OMP_CLAUSE_NUM_WORKERS;
38146 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
38147 clauses);
38148 break;
38149 case PRAGMA_OACC_CLAUSE_PRESENT:
38150 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
38151 c_name = "present";
38152 break;
38153 case PRAGMA_OACC_CLAUSE_PRIVATE:
38154 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
38155 clauses);
38156 c_name = "private";
38157 break;
38158 case PRAGMA_OACC_CLAUSE_REDUCTION:
38159 clauses
38160 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
38161 false, clauses);
38162 c_name = "reduction";
38163 break;
38164 case PRAGMA_OACC_CLAUSE_SEQ:
38165 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
38166 clauses);
38167 c_name = "seq";
38168 break;
38169 case PRAGMA_OACC_CLAUSE_TILE:
38170 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
38171 c_name = "tile";
38172 break;
38173 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
38174 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
38175 clauses);
38176 c_name = "use_device";
38177 break;
38178 case PRAGMA_OACC_CLAUSE_VECTOR:
38179 c_name = "vector";
38180 clauses = cp_parser_oacc_shape_clause (parser, here,
38181 OMP_CLAUSE_VECTOR,
38182 c_name, clauses);
38183 break;
38184 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
38185 c_name = "vector_length";
38186 code = OMP_CLAUSE_VECTOR_LENGTH;
38187 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
38188 clauses);
38189 break;
38190 case PRAGMA_OACC_CLAUSE_WAIT:
38191 clauses = cp_parser_oacc_clause_wait (parser, clauses);
38192 c_name = "wait";
38193 break;
38194 case PRAGMA_OACC_CLAUSE_WORKER:
38195 c_name = "worker";
38196 clauses = cp_parser_oacc_shape_clause (parser, here,
38197 OMP_CLAUSE_WORKER,
38198 c_name, clauses);
38199 break;
38200 default:
38201 cp_parser_error (parser, "expected %<#pragma acc%> clause");
38202 goto saw_error;
38203 }
38204
38205 first = false;
38206
38207 if (((mask >> c_kind) & 1) == 0)
38208 {
38209 /* Remove the invalid clause(s) from the list to avoid
38210 confusing the rest of the compiler. */
38211 clauses = prev;
38212 error_at (here, "%qs is not valid for %qs", c_name, where);
38213 }
38214 }
38215
38216 saw_error:
38217 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38218
38219 if (finish_p)
38220 return finish_omp_clauses (clauses, C_ORT_ACC);
38221
38222 return clauses;
38223 }
38224
38225 /* Parse all OpenMP clauses. The set clauses allowed by the directive
38226 is a bitmask in MASK. Return the list of clauses found.
38227 FINISH_P set if finish_omp_clauses should be called.
38228 NESTED non-zero if clauses should be terminated by closing paren instead
38229 of end of pragma. If it is 2, additionally commas are required in between
38230 the clauses. */
38231
38232 static tree
38233 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
38234 const char *where, cp_token *pragma_tok,
38235 bool finish_p = true, int nested = 0)
38236 {
38237 tree clauses = NULL;
38238 bool first = true;
38239 cp_token *token = NULL;
38240
38241 /* Don't create location wrapper nodes within OpenMP clauses. */
38242 auto_suppress_location_wrappers sentinel;
38243
38244 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38245 {
38246 pragma_omp_clause c_kind;
38247 const char *c_name;
38248 tree prev = clauses;
38249
38250 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38251 break;
38252
38253 if (!first)
38254 {
38255 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38256 cp_lexer_consume_token (parser->lexer);
38257 else if (nested == 2)
38258 error_at (cp_lexer_peek_token (parser->lexer)->location,
38259 "clauses in %<simd%> trait should be separated "
38260 "by %<,%>");
38261 }
38262
38263 token = cp_lexer_peek_token (parser->lexer);
38264 c_kind = cp_parser_omp_clause_name (parser);
38265
38266 switch (c_kind)
38267 {
38268 case PRAGMA_OMP_CLAUSE_BIND:
38269 clauses = cp_parser_omp_clause_bind (parser, clauses,
38270 token->location);
38271 c_name = "bind";
38272 break;
38273 case PRAGMA_OMP_CLAUSE_COLLAPSE:
38274 clauses = cp_parser_omp_clause_collapse (parser, clauses,
38275 token->location);
38276 c_name = "collapse";
38277 break;
38278 case PRAGMA_OMP_CLAUSE_COPYIN:
38279 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
38280 c_name = "copyin";
38281 break;
38282 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
38283 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
38284 clauses);
38285 c_name = "copyprivate";
38286 break;
38287 case PRAGMA_OMP_CLAUSE_DEFAULT:
38288 clauses = cp_parser_omp_clause_default (parser, clauses,
38289 token->location, false);
38290 c_name = "default";
38291 break;
38292 case PRAGMA_OMP_CLAUSE_FINAL:
38293 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
38294 c_name = "final";
38295 break;
38296 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
38297 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38298 clauses);
38299 c_name = "firstprivate";
38300 break;
38301 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
38302 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
38303 token->location);
38304 c_name = "grainsize";
38305 break;
38306 case PRAGMA_OMP_CLAUSE_HINT:
38307 clauses = cp_parser_omp_clause_hint (parser, clauses,
38308 token->location);
38309 c_name = "hint";
38310 break;
38311 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
38312 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
38313 token->location);
38314 c_name = "defaultmap";
38315 break;
38316 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
38317 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
38318 clauses);
38319 c_name = "use_device_ptr";
38320 break;
38321 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
38322 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
38323 clauses);
38324 c_name = "use_device_addr";
38325 break;
38326 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
38327 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
38328 clauses);
38329 c_name = "is_device_ptr";
38330 break;
38331 case PRAGMA_OMP_CLAUSE_IF:
38332 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
38333 true);
38334 c_name = "if";
38335 break;
38336 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
38337 clauses
38338 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
38339 true, clauses);
38340 c_name = "in_reduction";
38341 break;
38342 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
38343 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
38344 c_name = "lastprivate";
38345 break;
38346 case PRAGMA_OMP_CLAUSE_MERGEABLE:
38347 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
38348 token->location);
38349 c_name = "mergeable";
38350 break;
38351 case PRAGMA_OMP_CLAUSE_NOWAIT:
38352 clauses = cp_parser_omp_clause_nowait (parser, clauses,
38353 token->location);
38354 c_name = "nowait";
38355 break;
38356 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
38357 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
38358 token->location);
38359 c_name = "num_tasks";
38360 break;
38361 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
38362 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
38363 token->location);
38364 c_name = "num_threads";
38365 break;
38366 case PRAGMA_OMP_CLAUSE_ORDER:
38367 clauses = cp_parser_omp_clause_order (parser, clauses,
38368 token->location);
38369 c_name = "order";
38370 break;
38371 case PRAGMA_OMP_CLAUSE_ORDERED:
38372 clauses = cp_parser_omp_clause_ordered (parser, clauses,
38373 token->location);
38374 c_name = "ordered";
38375 break;
38376 case PRAGMA_OMP_CLAUSE_PRIORITY:
38377 clauses = cp_parser_omp_clause_priority (parser, clauses,
38378 token->location);
38379 c_name = "priority";
38380 break;
38381 case PRAGMA_OMP_CLAUSE_PRIVATE:
38382 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
38383 clauses);
38384 c_name = "private";
38385 break;
38386 case PRAGMA_OMP_CLAUSE_REDUCTION:
38387 clauses
38388 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
38389 true, clauses);
38390 c_name = "reduction";
38391 break;
38392 case PRAGMA_OMP_CLAUSE_SCHEDULE:
38393 clauses = cp_parser_omp_clause_schedule (parser, clauses,
38394 token->location);
38395 c_name = "schedule";
38396 break;
38397 case PRAGMA_OMP_CLAUSE_SHARED:
38398 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
38399 clauses);
38400 c_name = "shared";
38401 break;
38402 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
38403 clauses
38404 = cp_parser_omp_clause_reduction (parser,
38405 OMP_CLAUSE_TASK_REDUCTION,
38406 true, clauses);
38407 c_name = "task_reduction";
38408 break;
38409 case PRAGMA_OMP_CLAUSE_UNTIED:
38410 clauses = cp_parser_omp_clause_untied (parser, clauses,
38411 token->location);
38412 c_name = "untied";
38413 break;
38414 case PRAGMA_OMP_CLAUSE_INBRANCH:
38415 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
38416 clauses, token->location);
38417 c_name = "inbranch";
38418 break;
38419 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
38420 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
38421 clauses);
38422 c_name = "nontemporal";
38423 break;
38424 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
38425 clauses = cp_parser_omp_clause_branch (parser,
38426 OMP_CLAUSE_NOTINBRANCH,
38427 clauses, token->location);
38428 c_name = "notinbranch";
38429 break;
38430 case PRAGMA_OMP_CLAUSE_PARALLEL:
38431 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
38432 clauses, token->location);
38433 c_name = "parallel";
38434 if (!first)
38435 {
38436 clause_not_first:
38437 error_at (token->location, "%qs must be the first clause of %qs",
38438 c_name, where);
38439 clauses = prev;
38440 }
38441 break;
38442 case PRAGMA_OMP_CLAUSE_FOR:
38443 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
38444 clauses, token->location);
38445 c_name = "for";
38446 if (!first)
38447 goto clause_not_first;
38448 break;
38449 case PRAGMA_OMP_CLAUSE_SECTIONS:
38450 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
38451 clauses, token->location);
38452 c_name = "sections";
38453 if (!first)
38454 goto clause_not_first;
38455 break;
38456 case PRAGMA_OMP_CLAUSE_TASKGROUP:
38457 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
38458 clauses, token->location);
38459 c_name = "taskgroup";
38460 if (!first)
38461 goto clause_not_first;
38462 break;
38463 case PRAGMA_OMP_CLAUSE_LINK:
38464 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
38465 c_name = "to";
38466 break;
38467 case PRAGMA_OMP_CLAUSE_TO:
38468 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
38469 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
38470 clauses);
38471 else
38472 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
38473 c_name = "to";
38474 break;
38475 case PRAGMA_OMP_CLAUSE_FROM:
38476 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
38477 c_name = "from";
38478 break;
38479 case PRAGMA_OMP_CLAUSE_UNIFORM:
38480 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
38481 clauses);
38482 c_name = "uniform";
38483 break;
38484 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
38485 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
38486 token->location);
38487 c_name = "num_teams";
38488 break;
38489 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
38490 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
38491 token->location);
38492 c_name = "thread_limit";
38493 break;
38494 case PRAGMA_OMP_CLAUSE_ALIGNED:
38495 clauses = cp_parser_omp_clause_aligned (parser, clauses);
38496 c_name = "aligned";
38497 break;
38498 case PRAGMA_OMP_CLAUSE_ALLOCATE:
38499 clauses = cp_parser_omp_clause_allocate (parser, clauses);
38500 c_name = "allocate";
38501 break;
38502 case PRAGMA_OMP_CLAUSE_LINEAR:
38503 {
38504 bool declare_simd = false;
38505 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
38506 declare_simd = true;
38507 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
38508 }
38509 c_name = "linear";
38510 break;
38511 case PRAGMA_OMP_CLAUSE_DEPEND:
38512 clauses = cp_parser_omp_clause_depend (parser, clauses,
38513 token->location);
38514 c_name = "depend";
38515 break;
38516 case PRAGMA_OMP_CLAUSE_DETACH:
38517 clauses = cp_parser_omp_clause_detach (parser, clauses);
38518 c_name = "detach";
38519 break;
38520 case PRAGMA_OMP_CLAUSE_MAP:
38521 clauses = cp_parser_omp_clause_map (parser, clauses);
38522 c_name = "map";
38523 break;
38524 case PRAGMA_OMP_CLAUSE_DEVICE:
38525 clauses = cp_parser_omp_clause_device (parser, clauses,
38526 token->location);
38527 c_name = "device";
38528 break;
38529 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
38530 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
38531 token->location);
38532 c_name = "dist_schedule";
38533 break;
38534 case PRAGMA_OMP_CLAUSE_PROC_BIND:
38535 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
38536 token->location);
38537 c_name = "proc_bind";
38538 break;
38539 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
38540 clauses = cp_parser_omp_clause_device_type (parser, clauses,
38541 token->location);
38542 c_name = "device_type";
38543 break;
38544 case PRAGMA_OMP_CLAUSE_SAFELEN:
38545 clauses = cp_parser_omp_clause_safelen (parser, clauses,
38546 token->location);
38547 c_name = "safelen";
38548 break;
38549 case PRAGMA_OMP_CLAUSE_SIMDLEN:
38550 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
38551 token->location);
38552 c_name = "simdlen";
38553 break;
38554 case PRAGMA_OMP_CLAUSE_NOGROUP:
38555 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
38556 token->location);
38557 c_name = "nogroup";
38558 break;
38559 case PRAGMA_OMP_CLAUSE_THREADS:
38560 clauses
38561 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
38562 clauses, token->location);
38563 c_name = "threads";
38564 break;
38565 case PRAGMA_OMP_CLAUSE_SIMD:
38566 clauses
38567 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
38568 clauses, token->location);
38569 c_name = "simd";
38570 break;
38571 default:
38572 cp_parser_error (parser, "expected %<#pragma omp%> clause");
38573 goto saw_error;
38574 }
38575
38576 first = false;
38577
38578 if (((mask >> c_kind) & 1) == 0)
38579 {
38580 /* Remove the invalid clause(s) from the list to avoid
38581 confusing the rest of the compiler. */
38582 clauses = prev;
38583 error_at (token->location, "%qs is not valid for %qs", c_name, where);
38584 }
38585 }
38586 saw_error:
38587 if (!nested)
38588 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38589 if (finish_p)
38590 {
38591 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
38592 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
38593 else
38594 return finish_omp_clauses (clauses, C_ORT_OMP);
38595 }
38596 return clauses;
38597 }
38598
38599 /* OpenMP 2.5:
38600 structured-block:
38601 statement
38602
38603 In practice, we're also interested in adding the statement to an
38604 outer node. So it is convenient if we work around the fact that
38605 cp_parser_statement calls add_stmt. */
38606
38607 static unsigned
38608 cp_parser_begin_omp_structured_block (cp_parser *parser)
38609 {
38610 unsigned save = parser->in_statement;
38611
38612 /* Only move the values to IN_OMP_BLOCK if they weren't false.
38613 This preserves the "not within loop or switch" style error messages
38614 for nonsense cases like
38615 void foo() {
38616 #pragma omp single
38617 break;
38618 }
38619 */
38620 if (parser->in_statement)
38621 parser->in_statement = IN_OMP_BLOCK;
38622
38623 return save;
38624 }
38625
38626 static void
38627 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
38628 {
38629 parser->in_statement = save;
38630 }
38631
38632 static tree
38633 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
38634 {
38635 tree stmt = begin_omp_structured_block ();
38636 unsigned int save = cp_parser_begin_omp_structured_block (parser);
38637
38638 cp_parser_statement (parser, NULL_TREE, false, if_p);
38639
38640 cp_parser_end_omp_structured_block (parser, save);
38641 return finish_omp_structured_block (stmt);
38642 }
38643
38644 /* OpenMP 5.0:
38645 # pragma omp allocate (list) [allocator(allocator)] */
38646
38647 static void
38648 cp_parser_omp_allocate (cp_parser *parser, cp_token *pragma_tok)
38649 {
38650 tree allocator = NULL_TREE;
38651 location_t loc = pragma_tok->location;
38652 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ALLOCATE, NULL_TREE);
38653
38654 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38655 {
38656 matching_parens parens;
38657 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38658 const char *p = IDENTIFIER_POINTER (id);
38659 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
38660 cp_lexer_consume_token (parser->lexer);
38661 if (strcmp (p, "allocator") != 0)
38662 error_at (cloc, "expected %<allocator%>");
38663 else if (parens.require_open (parser))
38664 {
38665 allocator = cp_parser_assignment_expression (parser);
38666 if (allocator == error_mark_node)
38667 allocator = NULL_TREE;
38668 parens.require_close (parser);
38669 }
38670 }
38671 cp_parser_require_pragma_eol (parser, pragma_tok);
38672
38673 if (allocator)
38674 for (tree c = nl; c != NULL_TREE; c = OMP_CLAUSE_CHAIN (c))
38675 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
38676
38677 sorry_at (loc, "%<#pragma omp allocate%> not yet supported");
38678 }
38679
38680 /* OpenMP 2.5:
38681 # pragma omp atomic new-line
38682 expression-stmt
38683
38684 expression-stmt:
38685 x binop= expr | x++ | ++x | x-- | --x
38686 binop:
38687 +, *, -, /, &, ^, |, <<, >>
38688
38689 where x is an lvalue expression with scalar type.
38690
38691 OpenMP 3.1:
38692 # pragma omp atomic new-line
38693 update-stmt
38694
38695 # pragma omp atomic read new-line
38696 read-stmt
38697
38698 # pragma omp atomic write new-line
38699 write-stmt
38700
38701 # pragma omp atomic update new-line
38702 update-stmt
38703
38704 # pragma omp atomic capture new-line
38705 capture-stmt
38706
38707 # pragma omp atomic capture new-line
38708 capture-block
38709
38710 read-stmt:
38711 v = x
38712 write-stmt:
38713 x = expr
38714 update-stmt:
38715 expression-stmt | x = x binop expr
38716 capture-stmt:
38717 v = expression-stmt
38718 capture-block:
38719 { v = x; update-stmt; } | { update-stmt; v = x; }
38720
38721 OpenMP 4.0:
38722 update-stmt:
38723 expression-stmt | x = x binop expr | x = expr binop x
38724 capture-stmt:
38725 v = update-stmt
38726 capture-block:
38727 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
38728
38729 where x and v are lvalue expressions with scalar type. */
38730
38731 static void
38732 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok, bool openacc)
38733 {
38734 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
38735 tree rhs1 = NULL_TREE, orig_lhs;
38736 location_t loc = pragma_tok->location;
38737 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
38738 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
38739 bool structured_block = false;
38740 bool first = true;
38741 tree clauses = NULL_TREE;
38742
38743 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38744 {
38745 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38746 cp_lexer_consume_token (parser->lexer);
38747
38748 first = false;
38749
38750 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38751 {
38752 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38753 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
38754 const char *p = IDENTIFIER_POINTER (id);
38755 enum tree_code new_code = ERROR_MARK;
38756 enum omp_memory_order new_memory_order
38757 = OMP_MEMORY_ORDER_UNSPECIFIED;
38758
38759 if (!strcmp (p, "read"))
38760 new_code = OMP_ATOMIC_READ;
38761 else if (!strcmp (p, "write"))
38762 new_code = NOP_EXPR;
38763 else if (!strcmp (p, "update"))
38764 new_code = OMP_ATOMIC;
38765 else if (!strcmp (p, "capture"))
38766 new_code = OMP_ATOMIC_CAPTURE_NEW;
38767 else if (openacc)
38768 {
38769 p = NULL;
38770 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
38771 "or %<capture%> clause");
38772 }
38773 else if (!strcmp (p, "seq_cst"))
38774 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
38775 else if (!strcmp (p, "acq_rel"))
38776 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
38777 else if (!strcmp (p, "release"))
38778 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
38779 else if (!strcmp (p, "acquire"))
38780 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
38781 else if (!strcmp (p, "relaxed"))
38782 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
38783 else if (!strcmp (p, "hint"))
38784 {
38785 cp_lexer_consume_token (parser->lexer);
38786 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
38787 continue;
38788 }
38789 else
38790 {
38791 p = NULL;
38792 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
38793 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
38794 "%<release%>, %<relaxed%> or %<hint%> clause");
38795 }
38796 if (p)
38797 {
38798 if (new_code != ERROR_MARK)
38799 {
38800 /* OpenACC permits 'update capture'. */
38801 if (openacc
38802 && code == OMP_ATOMIC
38803 && new_code == OMP_ATOMIC_CAPTURE_NEW)
38804 code = new_code;
38805 else if (code != ERROR_MARK)
38806 error_at (cloc, "too many atomic clauses");
38807 else
38808 code = new_code;
38809 }
38810 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
38811 {
38812 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
38813 error_at (cloc, "too many memory order clauses");
38814 else
38815 memory_order = new_memory_order;
38816 }
38817 cp_lexer_consume_token (parser->lexer);
38818 continue;
38819 }
38820 }
38821 break;
38822 }
38823 cp_parser_require_pragma_eol (parser, pragma_tok);
38824
38825 if (code == ERROR_MARK)
38826 code = OMP_ATOMIC;
38827 if (openacc)
38828 memory_order = OMP_MEMORY_ORDER_RELAXED;
38829 else if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
38830 {
38831 omp_requires_mask
38832 = (enum omp_requires) (omp_requires_mask
38833 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
38834 switch ((enum omp_memory_order)
38835 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
38836 {
38837 case OMP_MEMORY_ORDER_UNSPECIFIED:
38838 case OMP_MEMORY_ORDER_RELAXED:
38839 memory_order = OMP_MEMORY_ORDER_RELAXED;
38840 break;
38841 case OMP_MEMORY_ORDER_SEQ_CST:
38842 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
38843 break;
38844 case OMP_MEMORY_ORDER_ACQ_REL:
38845 switch (code)
38846 {
38847 case OMP_ATOMIC_READ:
38848 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
38849 break;
38850 case NOP_EXPR: /* atomic write */
38851 case OMP_ATOMIC:
38852 memory_order = OMP_MEMORY_ORDER_RELEASE;
38853 break;
38854 default:
38855 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
38856 break;
38857 }
38858 break;
38859 default:
38860 gcc_unreachable ();
38861 }
38862 }
38863 else
38864 switch (code)
38865 {
38866 case OMP_ATOMIC_READ:
38867 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
38868 || memory_order == OMP_MEMORY_ORDER_RELEASE)
38869 {
38870 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
38871 "%<acq_rel%> or %<release%> clauses");
38872 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
38873 }
38874 break;
38875 case NOP_EXPR: /* atomic write */
38876 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
38877 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
38878 {
38879 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
38880 "%<acq_rel%> or %<acquire%> clauses");
38881 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
38882 }
38883 break;
38884 case OMP_ATOMIC:
38885 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
38886 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
38887 {
38888 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
38889 "%<acq_rel%> or %<acquire%> clauses");
38890 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
38891 }
38892 break;
38893 default:
38894 break;
38895 }
38896
38897 switch (code)
38898 {
38899 case OMP_ATOMIC_READ:
38900 case NOP_EXPR: /* atomic write */
38901 v = cp_parser_unary_expression (parser);
38902 if (v == error_mark_node)
38903 goto saw_error;
38904 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
38905 goto saw_error;
38906 if (code == NOP_EXPR)
38907 lhs = cp_parser_expression (parser);
38908 else
38909 lhs = cp_parser_unary_expression (parser);
38910 if (lhs == error_mark_node)
38911 goto saw_error;
38912 if (code == NOP_EXPR)
38913 {
38914 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
38915 opcode. */
38916 code = OMP_ATOMIC;
38917 rhs = lhs;
38918 lhs = v;
38919 v = NULL_TREE;
38920 }
38921 goto done;
38922 case OMP_ATOMIC_CAPTURE_NEW:
38923 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
38924 {
38925 cp_lexer_consume_token (parser->lexer);
38926 structured_block = true;
38927 }
38928 else
38929 {
38930 v = cp_parser_unary_expression (parser);
38931 if (v == error_mark_node)
38932 goto saw_error;
38933 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
38934 goto saw_error;
38935 }
38936 default:
38937 break;
38938 }
38939
38940 restart:
38941 lhs = cp_parser_unary_expression (parser);
38942 orig_lhs = lhs;
38943 switch (TREE_CODE (lhs))
38944 {
38945 case ERROR_MARK:
38946 goto saw_error;
38947
38948 case POSTINCREMENT_EXPR:
38949 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
38950 code = OMP_ATOMIC_CAPTURE_OLD;
38951 /* FALLTHROUGH */
38952 case PREINCREMENT_EXPR:
38953 lhs = TREE_OPERAND (lhs, 0);
38954 opcode = PLUS_EXPR;
38955 rhs = integer_one_node;
38956 break;
38957
38958 case POSTDECREMENT_EXPR:
38959 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
38960 code = OMP_ATOMIC_CAPTURE_OLD;
38961 /* FALLTHROUGH */
38962 case PREDECREMENT_EXPR:
38963 lhs = TREE_OPERAND (lhs, 0);
38964 opcode = MINUS_EXPR;
38965 rhs = integer_one_node;
38966 break;
38967
38968 case COMPOUND_EXPR:
38969 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
38970 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
38971 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
38972 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
38973 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
38974 (TREE_OPERAND (lhs, 1), 0), 0)))
38975 == BOOLEAN_TYPE)
38976 /* Undo effects of boolean_increment for post {in,de}crement. */
38977 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
38978 /* FALLTHRU */
38979 case MODIFY_EXPR:
38980 if (TREE_CODE (lhs) == MODIFY_EXPR
38981 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
38982 {
38983 /* Undo effects of boolean_increment. */
38984 if (integer_onep (TREE_OPERAND (lhs, 1)))
38985 {
38986 /* This is pre or post increment. */
38987 rhs = TREE_OPERAND (lhs, 1);
38988 lhs = TREE_OPERAND (lhs, 0);
38989 opcode = NOP_EXPR;
38990 if (code == OMP_ATOMIC_CAPTURE_NEW
38991 && !structured_block
38992 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
38993 code = OMP_ATOMIC_CAPTURE_OLD;
38994 break;
38995 }
38996 }
38997 /* FALLTHRU */
38998 default:
38999 switch (cp_lexer_peek_token (parser->lexer)->type)
39000 {
39001 case CPP_MULT_EQ:
39002 opcode = MULT_EXPR;
39003 break;
39004 case CPP_DIV_EQ:
39005 opcode = TRUNC_DIV_EXPR;
39006 break;
39007 case CPP_PLUS_EQ:
39008 opcode = PLUS_EXPR;
39009 break;
39010 case CPP_MINUS_EQ:
39011 opcode = MINUS_EXPR;
39012 break;
39013 case CPP_LSHIFT_EQ:
39014 opcode = LSHIFT_EXPR;
39015 break;
39016 case CPP_RSHIFT_EQ:
39017 opcode = RSHIFT_EXPR;
39018 break;
39019 case CPP_AND_EQ:
39020 opcode = BIT_AND_EXPR;
39021 break;
39022 case CPP_OR_EQ:
39023 opcode = BIT_IOR_EXPR;
39024 break;
39025 case CPP_XOR_EQ:
39026 opcode = BIT_XOR_EXPR;
39027 break;
39028 case CPP_EQ:
39029 enum cp_parser_prec oprec;
39030 cp_token *token;
39031 cp_lexer_consume_token (parser->lexer);
39032 cp_parser_parse_tentatively (parser);
39033 rhs1 = cp_parser_simple_cast_expression (parser);
39034 if (rhs1 == error_mark_node)
39035 {
39036 cp_parser_abort_tentative_parse (parser);
39037 cp_parser_simple_cast_expression (parser);
39038 goto saw_error;
39039 }
39040 token = cp_lexer_peek_token (parser->lexer);
39041 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
39042 {
39043 cp_parser_abort_tentative_parse (parser);
39044 cp_parser_parse_tentatively (parser);
39045 rhs = cp_parser_binary_expression (parser, false, true,
39046 PREC_NOT_OPERATOR, NULL);
39047 if (rhs == error_mark_node)
39048 {
39049 cp_parser_abort_tentative_parse (parser);
39050 cp_parser_binary_expression (parser, false, true,
39051 PREC_NOT_OPERATOR, NULL);
39052 goto saw_error;
39053 }
39054 switch (TREE_CODE (rhs))
39055 {
39056 case MULT_EXPR:
39057 case TRUNC_DIV_EXPR:
39058 case RDIV_EXPR:
39059 case PLUS_EXPR:
39060 case MINUS_EXPR:
39061 case LSHIFT_EXPR:
39062 case RSHIFT_EXPR:
39063 case BIT_AND_EXPR:
39064 case BIT_IOR_EXPR:
39065 case BIT_XOR_EXPR:
39066 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
39067 {
39068 if (cp_parser_parse_definitely (parser))
39069 {
39070 opcode = TREE_CODE (rhs);
39071 rhs1 = TREE_OPERAND (rhs, 0);
39072 rhs = TREE_OPERAND (rhs, 1);
39073 goto stmt_done;
39074 }
39075 else
39076 goto saw_error;
39077 }
39078 break;
39079 default:
39080 break;
39081 }
39082 cp_parser_abort_tentative_parse (parser);
39083 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
39084 {
39085 rhs = cp_parser_expression (parser);
39086 if (rhs == error_mark_node)
39087 goto saw_error;
39088 opcode = NOP_EXPR;
39089 rhs1 = NULL_TREE;
39090 goto stmt_done;
39091 }
39092 cp_parser_error (parser,
39093 "invalid form of %<#pragma omp atomic%>");
39094 goto saw_error;
39095 }
39096 if (!cp_parser_parse_definitely (parser))
39097 goto saw_error;
39098 switch (token->type)
39099 {
39100 case CPP_SEMICOLON:
39101 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
39102 {
39103 code = OMP_ATOMIC_CAPTURE_OLD;
39104 v = lhs;
39105 lhs = NULL_TREE;
39106 lhs1 = rhs1;
39107 rhs1 = NULL_TREE;
39108 cp_lexer_consume_token (parser->lexer);
39109 goto restart;
39110 }
39111 else if (structured_block)
39112 {
39113 opcode = NOP_EXPR;
39114 rhs = rhs1;
39115 rhs1 = NULL_TREE;
39116 goto stmt_done;
39117 }
39118 cp_parser_error (parser,
39119 "invalid form of %<#pragma omp atomic%>");
39120 goto saw_error;
39121 case CPP_MULT:
39122 opcode = MULT_EXPR;
39123 break;
39124 case CPP_DIV:
39125 opcode = TRUNC_DIV_EXPR;
39126 break;
39127 case CPP_PLUS:
39128 opcode = PLUS_EXPR;
39129 break;
39130 case CPP_MINUS:
39131 opcode = MINUS_EXPR;
39132 break;
39133 case CPP_LSHIFT:
39134 opcode = LSHIFT_EXPR;
39135 break;
39136 case CPP_RSHIFT:
39137 opcode = RSHIFT_EXPR;
39138 break;
39139 case CPP_AND:
39140 opcode = BIT_AND_EXPR;
39141 break;
39142 case CPP_OR:
39143 opcode = BIT_IOR_EXPR;
39144 break;
39145 case CPP_XOR:
39146 opcode = BIT_XOR_EXPR;
39147 break;
39148 default:
39149 cp_parser_error (parser,
39150 "invalid operator for %<#pragma omp atomic%>");
39151 goto saw_error;
39152 }
39153 oprec = TOKEN_PRECEDENCE (token);
39154 gcc_assert (oprec != PREC_NOT_OPERATOR);
39155 if (commutative_tree_code (opcode))
39156 oprec = (enum cp_parser_prec) (oprec - 1);
39157 cp_lexer_consume_token (parser->lexer);
39158 rhs = cp_parser_binary_expression (parser, false, false,
39159 oprec, NULL);
39160 if (rhs == error_mark_node)
39161 goto saw_error;
39162 goto stmt_done;
39163 /* FALLTHROUGH */
39164 default:
39165 cp_parser_error (parser,
39166 "invalid operator for %<#pragma omp atomic%>");
39167 goto saw_error;
39168 }
39169 cp_lexer_consume_token (parser->lexer);
39170
39171 rhs = cp_parser_expression (parser);
39172 if (rhs == error_mark_node)
39173 goto saw_error;
39174 break;
39175 }
39176 stmt_done:
39177 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
39178 {
39179 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
39180 goto saw_error;
39181 v = cp_parser_unary_expression (parser);
39182 if (v == error_mark_node)
39183 goto saw_error;
39184 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
39185 goto saw_error;
39186 lhs1 = cp_parser_unary_expression (parser);
39187 if (lhs1 == error_mark_node)
39188 goto saw_error;
39189 }
39190 if (structured_block)
39191 {
39192 cp_parser_consume_semicolon_at_end_of_statement (parser);
39193 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
39194 }
39195 done:
39196 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
39197 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
39198 rhs1, clauses, memory_order);
39199 if (!structured_block)
39200 cp_parser_consume_semicolon_at_end_of_statement (parser);
39201 return;
39202
39203 saw_error:
39204 cp_parser_skip_to_end_of_block_or_statement (parser);
39205 if (structured_block)
39206 {
39207 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
39208 cp_lexer_consume_token (parser->lexer);
39209 else if (code == OMP_ATOMIC_CAPTURE_NEW)
39210 {
39211 cp_parser_skip_to_end_of_block_or_statement (parser);
39212 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
39213 cp_lexer_consume_token (parser->lexer);
39214 }
39215 }
39216 }
39217
39218
39219 /* OpenMP 2.5:
39220 # pragma omp barrier new-line */
39221
39222 static void
39223 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
39224 {
39225 cp_parser_require_pragma_eol (parser, pragma_tok);
39226 finish_omp_barrier ();
39227 }
39228
39229 /* OpenMP 2.5:
39230 # pragma omp critical [(name)] new-line
39231 structured-block
39232
39233 OpenMP 4.5:
39234 # pragma omp critical [(name) [hint(expression)]] new-line
39235 structured-block */
39236
39237 #define OMP_CRITICAL_CLAUSE_MASK \
39238 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
39239
39240 static tree
39241 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39242 {
39243 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
39244
39245 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39246 {
39247 matching_parens parens;
39248 parens.consume_open (parser);
39249
39250 name = cp_parser_identifier (parser);
39251
39252 if (name == error_mark_node
39253 || !parens.require_close (parser))
39254 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39255 /*or_comma=*/false,
39256 /*consume_paren=*/true);
39257 if (name == error_mark_node)
39258 name = NULL;
39259
39260 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
39261 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
39262 cp_lexer_consume_token (parser->lexer);
39263 }
39264
39265 clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
39266 "#pragma omp critical", pragma_tok);
39267
39268 stmt = cp_parser_omp_structured_block (parser, if_p);
39269 return c_finish_omp_critical (input_location, stmt, name, clauses);
39270 }
39271
39272 /* OpenMP 5.0:
39273 # pragma omp depobj ( depobj ) depobj-clause new-line
39274
39275 depobj-clause:
39276 depend (dependence-type : locator)
39277 destroy
39278 update (dependence-type)
39279
39280 dependence-type:
39281 in
39282 out
39283 inout
39284 mutexinout */
39285
39286 static void
39287 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
39288 {
39289 location_t loc = pragma_tok->location;
39290 matching_parens parens;
39291 if (!parens.require_open (parser))
39292 {
39293 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39294 return;
39295 }
39296
39297 tree depobj = cp_parser_assignment_expression (parser);
39298
39299 if (!parens.require_close (parser))
39300 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39301 /*or_comma=*/false,
39302 /*consume_paren=*/true);
39303
39304 tree clause = NULL_TREE;
39305 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
39306 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
39307 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39308 {
39309 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39310 const char *p = IDENTIFIER_POINTER (id);
39311
39312 cp_lexer_consume_token (parser->lexer);
39313 if (!strcmp ("depend", p))
39314 {
39315 /* Don't create location wrapper nodes within the depend clause. */
39316 auto_suppress_location_wrappers sentinel;
39317 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
39318 if (clause)
39319 clause = finish_omp_clauses (clause, C_ORT_OMP);
39320 if (!clause)
39321 clause = error_mark_node;
39322 }
39323 else if (!strcmp ("destroy", p))
39324 kind = OMP_CLAUSE_DEPEND_LAST;
39325 else if (!strcmp ("update", p))
39326 {
39327 matching_parens c_parens;
39328 if (c_parens.require_open (parser))
39329 {
39330 location_t c2_loc
39331 = cp_lexer_peek_token (parser->lexer)->location;
39332 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39333 {
39334 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
39335 const char *p2 = IDENTIFIER_POINTER (id2);
39336
39337 cp_lexer_consume_token (parser->lexer);
39338 if (!strcmp ("in", p2))
39339 kind = OMP_CLAUSE_DEPEND_IN;
39340 else if (!strcmp ("out", p2))
39341 kind = OMP_CLAUSE_DEPEND_OUT;
39342 else if (!strcmp ("inout", p2))
39343 kind = OMP_CLAUSE_DEPEND_INOUT;
39344 else if (!strcmp ("mutexinoutset", p2))
39345 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
39346 }
39347 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
39348 {
39349 clause = error_mark_node;
39350 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
39351 "%<mutexinoutset%>");
39352 }
39353 if (!c_parens.require_close (parser))
39354 cp_parser_skip_to_closing_parenthesis (parser,
39355 /*recovering=*/true,
39356 /*or_comma=*/false,
39357 /*consume_paren=*/true);
39358 }
39359 else
39360 clause = error_mark_node;
39361 }
39362 }
39363 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
39364 {
39365 clause = error_mark_node;
39366 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
39367 }
39368 cp_parser_require_pragma_eol (parser, pragma_tok);
39369
39370 finish_omp_depobj (loc, depobj, kind, clause);
39371 }
39372
39373
39374 /* OpenMP 2.5:
39375 # pragma omp flush flush-vars[opt] new-line
39376
39377 flush-vars:
39378 ( variable-list )
39379
39380 OpenMP 5.0:
39381 # pragma omp flush memory-order-clause new-line */
39382
39383 static void
39384 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
39385 {
39386 enum memmodel mo = MEMMODEL_LAST;
39387 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39388 {
39389 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39390 const char *p = IDENTIFIER_POINTER (id);
39391 if (!strcmp (p, "acq_rel"))
39392 mo = MEMMODEL_ACQ_REL;
39393 else if (!strcmp (p, "release"))
39394 mo = MEMMODEL_RELEASE;
39395 else if (!strcmp (p, "acquire"))
39396 mo = MEMMODEL_ACQUIRE;
39397 else
39398 error_at (cp_lexer_peek_token (parser->lexer)->location,
39399 "expected %<acq_rel%>, %<release%> or %<acquire%>");
39400 cp_lexer_consume_token (parser->lexer);
39401 }
39402 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39403 {
39404 if (mo != MEMMODEL_LAST)
39405 error_at (cp_lexer_peek_token (parser->lexer)->location,
39406 "%<flush%> list specified together with memory order "
39407 "clause");
39408 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
39409 }
39410 cp_parser_require_pragma_eol (parser, pragma_tok);
39411
39412 finish_omp_flush (mo);
39413 }
39414
39415 /* Helper function, to parse omp for increment expression. */
39416
39417 static tree
39418 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
39419 {
39420 tree cond = cp_parser_binary_expression (parser, false, true,
39421 PREC_NOT_OPERATOR, NULL);
39422 if (cond == error_mark_node
39423 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
39424 {
39425 cp_parser_skip_to_end_of_statement (parser);
39426 return error_mark_node;
39427 }
39428
39429 switch (TREE_CODE (cond))
39430 {
39431 case GT_EXPR:
39432 case GE_EXPR:
39433 case LT_EXPR:
39434 case LE_EXPR:
39435 break;
39436 case NE_EXPR:
39437 if (code != OACC_LOOP)
39438 break;
39439 gcc_fallthrough ();
39440 default:
39441 return error_mark_node;
39442 }
39443
39444 /* If decl is an iterator, preserve LHS and RHS of the relational
39445 expr until finish_omp_for. */
39446 if (decl
39447 && (type_dependent_expression_p (decl)
39448 || CLASS_TYPE_P (TREE_TYPE (decl))))
39449 return cond;
39450
39451 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
39452 TREE_CODE (cond),
39453 TREE_OPERAND (cond, 0), ERROR_MARK,
39454 TREE_OPERAND (cond, 1), ERROR_MARK,
39455 /*overload=*/NULL, tf_warning_or_error);
39456 }
39457
39458 /* Helper function, to parse omp for increment expression. */
39459
39460 static tree
39461 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
39462 {
39463 cp_token *token = cp_lexer_peek_token (parser->lexer);
39464 enum tree_code op;
39465 tree lhs, rhs;
39466 cp_id_kind idk;
39467 bool decl_first;
39468
39469 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
39470 {
39471 op = (token->type == CPP_PLUS_PLUS
39472 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
39473 cp_lexer_consume_token (parser->lexer);
39474 lhs = cp_parser_simple_cast_expression (parser);
39475 if (lhs != decl
39476 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
39477 return error_mark_node;
39478 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
39479 }
39480
39481 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
39482 if (lhs != decl
39483 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
39484 return error_mark_node;
39485
39486 token = cp_lexer_peek_token (parser->lexer);
39487 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
39488 {
39489 op = (token->type == CPP_PLUS_PLUS
39490 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
39491 cp_lexer_consume_token (parser->lexer);
39492 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
39493 }
39494
39495 op = cp_parser_assignment_operator_opt (parser);
39496 if (op == ERROR_MARK)
39497 return error_mark_node;
39498
39499 if (op != NOP_EXPR)
39500 {
39501 rhs = cp_parser_assignment_expression (parser);
39502 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
39503 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
39504 }
39505
39506 lhs = cp_parser_binary_expression (parser, false, false,
39507 PREC_ADDITIVE_EXPRESSION, NULL);
39508 token = cp_lexer_peek_token (parser->lexer);
39509 decl_first = (lhs == decl
39510 || (processing_template_decl && cp_tree_equal (lhs, decl)));
39511 if (decl_first)
39512 lhs = NULL_TREE;
39513 if (token->type != CPP_PLUS
39514 && token->type != CPP_MINUS)
39515 return error_mark_node;
39516
39517 do
39518 {
39519 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
39520 cp_lexer_consume_token (parser->lexer);
39521 rhs = cp_parser_binary_expression (parser, false, false,
39522 PREC_ADDITIVE_EXPRESSION, NULL);
39523 token = cp_lexer_peek_token (parser->lexer);
39524 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
39525 {
39526 if (lhs == NULL_TREE)
39527 {
39528 if (op == PLUS_EXPR)
39529 lhs = rhs;
39530 else
39531 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
39532 tf_warning_or_error);
39533 }
39534 else
39535 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
39536 ERROR_MARK, NULL, tf_warning_or_error);
39537 }
39538 }
39539 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
39540
39541 if (!decl_first)
39542 {
39543 if ((rhs != decl
39544 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
39545 || op == MINUS_EXPR)
39546 return error_mark_node;
39547 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
39548 }
39549 else
39550 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
39551
39552 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
39553 }
39554
39555 /* Parse the initialization statement of an OpenMP for loop.
39556
39557 Return true if the resulting construct should have an
39558 OMP_CLAUSE_PRIVATE added to it. */
39559
39560 static tree
39561 cp_parser_omp_for_loop_init (cp_parser *parser,
39562 tree &this_pre_body,
39563 releasing_vec &for_block,
39564 tree &init,
39565 tree &orig_init,
39566 tree &decl,
39567 tree &real_decl)
39568 {
39569 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
39570 return NULL_TREE;
39571
39572 tree add_private_clause = NULL_TREE;
39573
39574 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
39575
39576 init-expr:
39577 var = lb
39578 integer-type var = lb
39579 random-access-iterator-type var = lb
39580 pointer-type var = lb
39581 */
39582 cp_decl_specifier_seq type_specifiers;
39583
39584 /* First, try to parse as an initialized declaration. See
39585 cp_parser_condition, from whence the bulk of this is copied. */
39586
39587 cp_parser_parse_tentatively (parser);
39588 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
39589 /*is_declaration=*/true,
39590 /*is_trailing_return=*/false,
39591 &type_specifiers);
39592 if (cp_parser_parse_definitely (parser))
39593 {
39594 /* If parsing a type specifier seq succeeded, then this
39595 MUST be a initialized declaration. */
39596 tree asm_specification, attributes;
39597 cp_declarator *declarator;
39598
39599 declarator = cp_parser_declarator (parser,
39600 CP_PARSER_DECLARATOR_NAMED,
39601 CP_PARSER_FLAGS_NONE,
39602 /*ctor_dtor_or_conv_p=*/NULL,
39603 /*parenthesized_p=*/NULL,
39604 /*member_p=*/false,
39605 /*friend_p=*/false,
39606 /*static_p=*/false);
39607 attributes = cp_parser_attributes_opt (parser);
39608 asm_specification = cp_parser_asm_specification_opt (parser);
39609
39610 if (declarator == cp_error_declarator)
39611 cp_parser_skip_to_end_of_statement (parser);
39612
39613 else
39614 {
39615 tree pushed_scope, auto_node;
39616
39617 decl = start_decl (declarator, &type_specifiers,
39618 SD_INITIALIZED, attributes,
39619 /*prefix_attributes=*/NULL_TREE,
39620 &pushed_scope);
39621
39622 auto_node = type_uses_auto (TREE_TYPE (decl));
39623 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
39624 {
39625 if (cp_lexer_next_token_is (parser->lexer,
39626 CPP_OPEN_PAREN))
39627 error ("parenthesized initialization is not allowed in "
39628 "OpenMP %<for%> loop");
39629 else
39630 /* Trigger an error. */
39631 cp_parser_require (parser, CPP_EQ, RT_EQ);
39632
39633 init = error_mark_node;
39634 cp_parser_skip_to_end_of_statement (parser);
39635 }
39636 else if (CLASS_TYPE_P (TREE_TYPE (decl))
39637 || type_dependent_expression_p (decl)
39638 || auto_node)
39639 {
39640 bool is_direct_init, is_non_constant_init;
39641
39642 init = cp_parser_initializer (parser,
39643 &is_direct_init,
39644 &is_non_constant_init);
39645
39646 if (auto_node)
39647 {
39648 TREE_TYPE (decl)
39649 = do_auto_deduction (TREE_TYPE (decl), init,
39650 auto_node);
39651
39652 if (!CLASS_TYPE_P (TREE_TYPE (decl))
39653 && !type_dependent_expression_p (decl))
39654 goto non_class;
39655 }
39656
39657 cp_finish_decl (decl, init, !is_non_constant_init,
39658 asm_specification,
39659 LOOKUP_ONLYCONVERTING);
39660 orig_init = init;
39661 if (CLASS_TYPE_P (TREE_TYPE (decl)))
39662 {
39663 vec_safe_push (for_block, this_pre_body);
39664 init = NULL_TREE;
39665 }
39666 else
39667 {
39668 init = pop_stmt_list (this_pre_body);
39669 if (init && TREE_CODE (init) == STATEMENT_LIST)
39670 {
39671 tree_stmt_iterator i = tsi_start (init);
39672 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
39673 while (!tsi_end_p (i))
39674 {
39675 tree t = tsi_stmt (i);
39676 if (TREE_CODE (t) == DECL_EXPR
39677 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
39678 {
39679 tsi_delink (&i);
39680 vec_safe_push (for_block, t);
39681 continue;
39682 }
39683 break;
39684 }
39685 if (tsi_one_before_end_p (i))
39686 {
39687 tree t = tsi_stmt (i);
39688 tsi_delink (&i);
39689 free_stmt_list (init);
39690 init = t;
39691 }
39692 }
39693 }
39694 this_pre_body = NULL_TREE;
39695 }
39696 else
39697 {
39698 /* Consume '='. */
39699 cp_lexer_consume_token (parser->lexer);
39700 init = cp_parser_assignment_expression (parser);
39701
39702 non_class:
39703 if (TYPE_REF_P (TREE_TYPE (decl)))
39704 init = error_mark_node;
39705 else
39706 cp_finish_decl (decl, NULL_TREE,
39707 /*init_const_expr_p=*/false,
39708 asm_specification,
39709 LOOKUP_ONLYCONVERTING);
39710 }
39711
39712 if (pushed_scope)
39713 pop_scope (pushed_scope);
39714 }
39715 }
39716 else
39717 {
39718 cp_id_kind idk;
39719 /* If parsing a type specifier sequence failed, then
39720 this MUST be a simple expression. */
39721 cp_parser_parse_tentatively (parser);
39722 decl = cp_parser_primary_expression (parser, false, false,
39723 false, &idk);
39724 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
39725 if (!cp_parser_error_occurred (parser)
39726 && decl
39727 && (TREE_CODE (decl) == COMPONENT_REF
39728 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
39729 {
39730 cp_parser_abort_tentative_parse (parser);
39731 cp_parser_parse_tentatively (parser);
39732 cp_token *token = cp_lexer_peek_token (parser->lexer);
39733 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
39734 /*check_dependency_p=*/true,
39735 /*template_p=*/NULL,
39736 /*declarator_p=*/false,
39737 /*optional_p=*/false);
39738 if (name != error_mark_node
39739 && last_tok == cp_lexer_peek_token (parser->lexer))
39740 {
39741 decl = cp_parser_lookup_name_simple (parser, name,
39742 token->location);
39743 if (TREE_CODE (decl) == FIELD_DECL)
39744 add_private_clause = omp_privatize_field (decl, false);
39745 }
39746 cp_parser_abort_tentative_parse (parser);
39747 cp_parser_parse_tentatively (parser);
39748 decl = cp_parser_primary_expression (parser, false, false,
39749 false, &idk);
39750 }
39751 if (!cp_parser_error_occurred (parser)
39752 && decl
39753 && DECL_P (decl)
39754 && CLASS_TYPE_P (TREE_TYPE (decl)))
39755 {
39756 tree rhs;
39757
39758 cp_parser_parse_definitely (parser);
39759 cp_parser_require (parser, CPP_EQ, RT_EQ);
39760 rhs = cp_parser_assignment_expression (parser);
39761 orig_init = rhs;
39762 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
39763 decl, NOP_EXPR,
39764 rhs,
39765 tf_warning_or_error));
39766 if (!add_private_clause)
39767 add_private_clause = decl;
39768 }
39769 else
39770 {
39771 decl = NULL;
39772 cp_parser_abort_tentative_parse (parser);
39773 init = cp_parser_expression (parser);
39774 if (init)
39775 {
39776 if (TREE_CODE (init) == MODIFY_EXPR
39777 || TREE_CODE (init) == MODOP_EXPR)
39778 real_decl = TREE_OPERAND (init, 0);
39779 }
39780 }
39781 }
39782 return add_private_clause;
39783 }
39784
39785 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
39786
39787 void
39788 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
39789 tree &decl, tree &orig_decl, tree &init,
39790 tree &orig_init, tree &cond, tree &incr)
39791 {
39792 tree begin, end, range_temp_decl = NULL_TREE;
39793 tree iter_type, begin_expr, end_expr;
39794
39795 if (processing_template_decl)
39796 {
39797 if (check_for_bare_parameter_packs (init))
39798 init = error_mark_node;
39799 if (!type_dependent_expression_p (init)
39800 /* do_auto_deduction doesn't mess with template init-lists. */
39801 && !BRACE_ENCLOSED_INITIALIZER_P (init))
39802 {
39803 tree d = decl;
39804 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
39805 {
39806 tree v = DECL_VALUE_EXPR (decl);
39807 if (TREE_CODE (v) == ARRAY_REF
39808 && VAR_P (TREE_OPERAND (v, 0))
39809 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
39810 d = TREE_OPERAND (v, 0);
39811 }
39812 do_range_for_auto_deduction (d, init);
39813 }
39814 cond = global_namespace;
39815 incr = NULL_TREE;
39816 orig_init = init;
39817 if (this_pre_body)
39818 this_pre_body = pop_stmt_list (this_pre_body);
39819 return;
39820 }
39821
39822 init = mark_lvalue_use (init);
39823
39824 if (decl == error_mark_node || init == error_mark_node)
39825 /* If an error happened previously do nothing or else a lot of
39826 unhelpful errors would be issued. */
39827 begin_expr = end_expr = iter_type = error_mark_node;
39828 else
39829 {
39830 tree range_temp;
39831
39832 if (VAR_P (init)
39833 && array_of_runtime_bound_p (TREE_TYPE (init)))
39834 /* Can't bind a reference to an array of runtime bound. */
39835 range_temp = init;
39836 else
39837 {
39838 range_temp = build_range_temp (init);
39839 DECL_NAME (range_temp) = NULL_TREE;
39840 pushdecl (range_temp);
39841 cp_finish_decl (range_temp, init,
39842 /*is_constant_init*/false, NULL_TREE,
39843 LOOKUP_ONLYCONVERTING);
39844 range_temp_decl = range_temp;
39845 range_temp = convert_from_reference (range_temp);
39846 }
39847 iter_type = cp_parser_perform_range_for_lookup (range_temp,
39848 &begin_expr, &end_expr);
39849 }
39850
39851 tree end_iter_type = iter_type;
39852 if (cxx_dialect >= cxx17)
39853 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
39854 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
39855 TREE_USED (end) = 1;
39856 DECL_ARTIFICIAL (end) = 1;
39857 pushdecl (end);
39858 cp_finish_decl (end, end_expr,
39859 /*is_constant_init*/false, NULL_TREE,
39860 LOOKUP_ONLYCONVERTING);
39861
39862 /* The new for initialization statement. */
39863 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
39864 TREE_USED (begin) = 1;
39865 DECL_ARTIFICIAL (begin) = 1;
39866 pushdecl (begin);
39867 orig_init = init;
39868 if (CLASS_TYPE_P (iter_type))
39869 init = NULL_TREE;
39870 else
39871 {
39872 init = begin_expr;
39873 begin_expr = NULL_TREE;
39874 }
39875 cp_finish_decl (begin, begin_expr,
39876 /*is_constant_init*/false, NULL_TREE,
39877 LOOKUP_ONLYCONVERTING);
39878
39879 /* The new for condition. */
39880 if (CLASS_TYPE_P (iter_type))
39881 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
39882 else
39883 cond = build_x_binary_op (input_location, NE_EXPR,
39884 begin, ERROR_MARK,
39885 end, ERROR_MARK,
39886 NULL, tf_warning_or_error);
39887
39888 /* The new increment expression. */
39889 if (CLASS_TYPE_P (iter_type))
39890 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
39891 else
39892 incr = finish_unary_op_expr (input_location,
39893 PREINCREMENT_EXPR, begin,
39894 tf_warning_or_error);
39895
39896 orig_decl = decl;
39897 decl = begin;
39898 if (for_block)
39899 {
39900 vec_safe_push (for_block, this_pre_body);
39901 this_pre_body = NULL_TREE;
39902 }
39903
39904 tree decomp_first_name = NULL_TREE;
39905 unsigned decomp_cnt = 0;
39906 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
39907 {
39908 tree v = DECL_VALUE_EXPR (orig_decl);
39909 if (TREE_CODE (v) == ARRAY_REF
39910 && VAR_P (TREE_OPERAND (v, 0))
39911 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
39912 {
39913 tree d = orig_decl;
39914 orig_decl = TREE_OPERAND (v, 0);
39915 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
39916 decomp_first_name = d;
39917 }
39918 }
39919
39920 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
39921 if (auto_node)
39922 {
39923 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
39924 tf_none);
39925 if (!error_operand_p (t))
39926 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
39927 t, auto_node);
39928 }
39929
39930 tree v = make_tree_vec (decomp_cnt + 3);
39931 TREE_VEC_ELT (v, 0) = range_temp_decl;
39932 TREE_VEC_ELT (v, 1) = end;
39933 TREE_VEC_ELT (v, 2) = orig_decl;
39934 for (unsigned i = 0; i < decomp_cnt; i++)
39935 {
39936 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
39937 decomp_first_name = DECL_CHAIN (decomp_first_name);
39938 }
39939 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
39940 }
39941
39942 /* Helper for cp_parser_omp_for_loop, finalize part of range for
39943 inside of the collapsed body. */
39944
39945 void
39946 cp_finish_omp_range_for (tree orig, tree begin)
39947 {
39948 gcc_assert (TREE_CODE (orig) == TREE_LIST
39949 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
39950 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
39951 tree decomp_first_name = NULL_TREE;
39952 unsigned int decomp_cnt = 0;
39953
39954 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
39955 {
39956 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
39957 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
39958 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
39959 }
39960
39961 /* The declaration is initialized with *__begin inside the loop body. */
39962 cp_finish_decl (decl,
39963 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
39964 tf_warning_or_error),
39965 /*is_constant_init*/false, NULL_TREE,
39966 LOOKUP_ONLYCONVERTING);
39967 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
39968 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
39969 }
39970
39971 /* OpenMP 5.0:
39972
39973 scan-loop-body:
39974 { structured-block scan-directive structured-block } */
39975
39976 static void
39977 cp_parser_omp_scan_loop_body (cp_parser *parser)
39978 {
39979 tree substmt, clauses = NULL_TREE;
39980
39981 matching_braces braces;
39982 if (!braces.require_open (parser))
39983 return;
39984
39985 substmt = cp_parser_omp_structured_block (parser, NULL);
39986 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
39987 add_stmt (substmt);
39988
39989 cp_token *tok = cp_lexer_peek_token (parser->lexer);
39990 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
39991 {
39992 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
39993
39994 cp_lexer_consume_token (parser->lexer);
39995
39996 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39997 {
39998 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39999 const char *p = IDENTIFIER_POINTER (id);
40000 if (strcmp (p, "inclusive") == 0)
40001 clause = OMP_CLAUSE_INCLUSIVE;
40002 else if (strcmp (p, "exclusive") == 0)
40003 clause = OMP_CLAUSE_EXCLUSIVE;
40004 }
40005 if (clause != OMP_CLAUSE_ERROR)
40006 {
40007 cp_lexer_consume_token (parser->lexer);
40008 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
40009 }
40010 else
40011 cp_parser_error (parser, "expected %<inclusive%> or "
40012 "%<exclusive%> clause");
40013
40014 cp_parser_require_pragma_eol (parser, tok);
40015 }
40016 else
40017 error ("expected %<#pragma omp scan%>");
40018
40019 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
40020 substmt = cp_parser_omp_structured_block (parser, NULL);
40021 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
40022 clauses);
40023 add_stmt (substmt);
40024
40025 braces.require_close (parser);
40026 }
40027
40028 /* Parse the restricted form of the for statement allowed by OpenMP. */
40029
40030 static tree
40031 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
40032 tree *cclauses, bool *if_p)
40033 {
40034 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
40035 tree orig_decl;
40036 tree real_decl, initv, condv, incrv, declv, orig_declv;
40037 tree this_pre_body, cl, ordered_cl = NULL_TREE;
40038 location_t loc_first;
40039 bool collapse_err = false;
40040 int i, collapse = 1, ordered = 0, count, nbraces = 0;
40041 releasing_vec for_block;
40042 auto_vec<tree, 4> orig_inits;
40043 bool tiling = false;
40044 bool inscan = false;
40045
40046 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
40047 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
40048 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
40049 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
40050 {
40051 tiling = true;
40052 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
40053 }
40054 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
40055 && OMP_CLAUSE_ORDERED_EXPR (cl))
40056 {
40057 ordered_cl = cl;
40058 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
40059 }
40060 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
40061 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
40062 && (code == OMP_SIMD || code == OMP_FOR))
40063 inscan = true;
40064
40065 if (ordered && ordered < collapse)
40066 {
40067 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
40068 "%<ordered%> clause parameter is less than %<collapse%>");
40069 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
40070 = build_int_cst (NULL_TREE, collapse);
40071 ordered = collapse;
40072 }
40073 if (ordered)
40074 {
40075 for (tree *pc = &clauses; *pc; )
40076 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
40077 {
40078 error_at (OMP_CLAUSE_LOCATION (*pc),
40079 "%<linear%> clause may not be specified together "
40080 "with %<ordered%> clause with a parameter");
40081 *pc = OMP_CLAUSE_CHAIN (*pc);
40082 }
40083 else
40084 pc = &OMP_CLAUSE_CHAIN (*pc);
40085 }
40086
40087 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
40088 count = ordered ? ordered : collapse;
40089
40090 declv = make_tree_vec (count);
40091 initv = make_tree_vec (count);
40092 condv = make_tree_vec (count);
40093 incrv = make_tree_vec (count);
40094 orig_declv = NULL_TREE;
40095
40096 loc_first = cp_lexer_peek_token (parser->lexer)->location;
40097
40098 for (i = 0; i < count; i++)
40099 {
40100 int bracecount = 0;
40101 tree add_private_clause = NULL_TREE;
40102 location_t loc;
40103
40104 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
40105 {
40106 if (!collapse_err)
40107 cp_parser_error (parser, "for statement expected");
40108 return NULL;
40109 }
40110 loc = cp_lexer_consume_token (parser->lexer)->location;
40111
40112 /* Don't create location wrapper nodes within an OpenMP "for"
40113 statement. */
40114 auto_suppress_location_wrappers sentinel;
40115
40116 matching_parens parens;
40117 if (!parens.require_open (parser))
40118 return NULL;
40119
40120 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
40121 this_pre_body = push_stmt_list ();
40122
40123 if (code != OACC_LOOP && cxx_dialect >= cxx11)
40124 {
40125 /* Save tokens so that we can put them back. */
40126 cp_lexer_save_tokens (parser->lexer);
40127
40128 /* Look for ':' that is not nested in () or {}. */
40129 bool is_range_for
40130 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
40131 /*recovering=*/false,
40132 CPP_COLON,
40133 /*consume_paren=*/
40134 false) == -1);
40135
40136 /* Roll back the tokens we skipped. */
40137 cp_lexer_rollback_tokens (parser->lexer);
40138
40139 if (is_range_for)
40140 {
40141 bool saved_colon_corrects_to_scope_p
40142 = parser->colon_corrects_to_scope_p;
40143
40144 /* A colon is used in range-based for. */
40145 parser->colon_corrects_to_scope_p = false;
40146
40147 /* Parse the declaration. */
40148 cp_parser_simple_declaration (parser,
40149 /*function_definition_allowed_p=*/
40150 false, &decl);
40151 parser->colon_corrects_to_scope_p
40152 = saved_colon_corrects_to_scope_p;
40153
40154 cp_parser_require (parser, CPP_COLON, RT_COLON);
40155
40156 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
40157 false, 0, true);
40158
40159 cp_convert_omp_range_for (this_pre_body, for_block, decl,
40160 orig_decl, init, orig_init,
40161 cond, incr);
40162 if (this_pre_body)
40163 {
40164 if (pre_body)
40165 {
40166 tree t = pre_body;
40167 pre_body = push_stmt_list ();
40168 add_stmt (t);
40169 add_stmt (this_pre_body);
40170 pre_body = pop_stmt_list (pre_body);
40171 }
40172 else
40173 pre_body = this_pre_body;
40174 }
40175
40176 if (ordered_cl)
40177 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
40178 "%<ordered%> clause with parameter on "
40179 "range-based %<for%> loop");
40180
40181 goto parse_close_paren;
40182 }
40183 }
40184
40185 add_private_clause
40186 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
40187 init, orig_init, decl, real_decl);
40188
40189 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40190 if (this_pre_body)
40191 {
40192 this_pre_body = pop_stmt_list (this_pre_body);
40193 if (pre_body)
40194 {
40195 tree t = pre_body;
40196 pre_body = push_stmt_list ();
40197 add_stmt (t);
40198 add_stmt (this_pre_body);
40199 pre_body = pop_stmt_list (pre_body);
40200 }
40201 else
40202 pre_body = this_pre_body;
40203 }
40204
40205 if (decl)
40206 real_decl = decl;
40207 if (cclauses != NULL
40208 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
40209 && real_decl != NULL_TREE
40210 && code != OMP_LOOP)
40211 {
40212 tree *c;
40213 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
40214 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
40215 && OMP_CLAUSE_DECL (*c) == real_decl)
40216 {
40217 error_at (loc, "iteration variable %qD"
40218 " should not be firstprivate", real_decl);
40219 *c = OMP_CLAUSE_CHAIN (*c);
40220 }
40221 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
40222 && OMP_CLAUSE_DECL (*c) == real_decl)
40223 {
40224 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
40225 tree l = *c;
40226 *c = OMP_CLAUSE_CHAIN (*c);
40227 if (code == OMP_SIMD)
40228 {
40229 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
40230 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
40231 }
40232 else
40233 {
40234 OMP_CLAUSE_CHAIN (l) = clauses;
40235 clauses = l;
40236 }
40237 add_private_clause = NULL_TREE;
40238 }
40239 else
40240 {
40241 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
40242 && OMP_CLAUSE_DECL (*c) == real_decl)
40243 add_private_clause = NULL_TREE;
40244 c = &OMP_CLAUSE_CHAIN (*c);
40245 }
40246 }
40247
40248 if (add_private_clause)
40249 {
40250 tree c;
40251 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
40252 {
40253 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
40254 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
40255 && OMP_CLAUSE_DECL (c) == decl)
40256 break;
40257 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
40258 && OMP_CLAUSE_DECL (c) == decl)
40259 error_at (loc, "iteration variable %qD "
40260 "should not be firstprivate",
40261 decl);
40262 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
40263 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
40264 && OMP_CLAUSE_DECL (c) == decl)
40265 error_at (loc, "iteration variable %qD should not be reduction",
40266 decl);
40267 }
40268 if (c == NULL)
40269 {
40270 if ((code == OMP_SIMD && collapse != 1) || code == OMP_LOOP)
40271 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
40272 else if (code != OMP_SIMD)
40273 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
40274 else
40275 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
40276 OMP_CLAUSE_DECL (c) = add_private_clause;
40277 c = finish_omp_clauses (c, C_ORT_OMP);
40278 if (c)
40279 {
40280 OMP_CLAUSE_CHAIN (c) = clauses;
40281 clauses = c;
40282 /* For linear, signal that we need to fill up
40283 the so far unknown linear step. */
40284 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
40285 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
40286 }
40287 }
40288 }
40289
40290 cond = NULL;
40291 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
40292 cond = cp_parser_omp_for_cond (parser, decl, code);
40293 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40294
40295 incr = NULL;
40296 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
40297 {
40298 /* If decl is an iterator, preserve the operator on decl
40299 until finish_omp_for. */
40300 if (real_decl
40301 && ((processing_template_decl
40302 && (TREE_TYPE (real_decl) == NULL_TREE
40303 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
40304 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
40305 incr = cp_parser_omp_for_incr (parser, real_decl);
40306 else
40307 incr = cp_parser_expression (parser);
40308 protected_set_expr_location_if_unset (incr, input_location);
40309 }
40310
40311 parse_close_paren:
40312 if (!parens.require_close (parser))
40313 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40314 /*or_comma=*/false,
40315 /*consume_paren=*/true);
40316
40317 TREE_VEC_ELT (declv, i) = decl;
40318 TREE_VEC_ELT (initv, i) = init;
40319 TREE_VEC_ELT (condv, i) = cond;
40320 TREE_VEC_ELT (incrv, i) = incr;
40321 if (orig_init)
40322 {
40323 orig_inits.safe_grow_cleared (i + 1, true);
40324 orig_inits[i] = orig_init;
40325 }
40326 if (orig_decl)
40327 {
40328 if (!orig_declv)
40329 orig_declv = copy_node (declv);
40330 TREE_VEC_ELT (orig_declv, i) = orig_decl;
40331 }
40332 else if (orig_declv)
40333 TREE_VEC_ELT (orig_declv, i) = decl;
40334
40335 if (i == count - 1)
40336 break;
40337
40338 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
40339 in between the collapsed for loops to be still considered perfectly
40340 nested. Hopefully the final version clarifies this.
40341 For now handle (multiple) {'s and empty statements. */
40342 cp_parser_parse_tentatively (parser);
40343 for (;;)
40344 {
40345 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
40346 break;
40347 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
40348 {
40349 cp_lexer_consume_token (parser->lexer);
40350 bracecount++;
40351 }
40352 else if (bracecount
40353 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
40354 cp_lexer_consume_token (parser->lexer);
40355 else
40356 {
40357 loc = cp_lexer_peek_token (parser->lexer)->location;
40358 error_at (loc, "not enough for loops to collapse");
40359 collapse_err = true;
40360 cp_parser_abort_tentative_parse (parser);
40361 declv = NULL_TREE;
40362 break;
40363 }
40364 }
40365
40366 if (declv)
40367 {
40368 cp_parser_parse_definitely (parser);
40369 nbraces += bracecount;
40370 }
40371 }
40372
40373 if (nbraces)
40374 if_p = NULL;
40375
40376 /* Note that we saved the original contents of this flag when we entered
40377 the structured block, and so we don't need to re-save it here. */
40378 parser->in_statement = IN_OMP_FOR;
40379
40380 /* Note that the grammar doesn't call for a structured block here,
40381 though the loop as a whole is a structured block. */
40382 if (orig_declv)
40383 {
40384 body = begin_omp_structured_block ();
40385 for (i = 0; i < count; i++)
40386 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
40387 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
40388 TREE_VEC_ELT (declv, i));
40389 }
40390 else
40391 body = push_stmt_list ();
40392 if (inscan)
40393 cp_parser_omp_scan_loop_body (parser);
40394 else
40395 cp_parser_statement (parser, NULL_TREE, false, if_p);
40396 if (orig_declv)
40397 body = finish_omp_structured_block (body);
40398 else
40399 body = pop_stmt_list (body);
40400
40401 if (declv == NULL_TREE)
40402 ret = NULL_TREE;
40403 else
40404 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
40405 incrv, body, pre_body, &orig_inits, clauses);
40406
40407 while (nbraces)
40408 {
40409 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
40410 {
40411 cp_lexer_consume_token (parser->lexer);
40412 nbraces--;
40413 }
40414 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
40415 cp_lexer_consume_token (parser->lexer);
40416 else
40417 {
40418 if (!collapse_err)
40419 {
40420 error_at (cp_lexer_peek_token (parser->lexer)->location,
40421 "collapsed loops not perfectly nested");
40422 }
40423 collapse_err = true;
40424 cp_parser_statement_seq_opt (parser, NULL);
40425 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
40426 break;
40427 }
40428 }
40429
40430 while (!for_block->is_empty ())
40431 {
40432 tree t = for_block->pop ();
40433 if (TREE_CODE (t) == STATEMENT_LIST)
40434 add_stmt (pop_stmt_list (t));
40435 else
40436 add_stmt (t);
40437 }
40438
40439 return ret;
40440 }
40441
40442 /* Helper function for OpenMP parsing, split clauses and call
40443 finish_omp_clauses on each of the set of clauses afterwards. */
40444
40445 static void
40446 cp_omp_split_clauses (location_t loc, enum tree_code code,
40447 omp_clause_mask mask, tree clauses, tree *cclauses)
40448 {
40449 int i;
40450 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
40451 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
40452 if (cclauses[i])
40453 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
40454 }
40455
40456 /* OpenMP 5.0:
40457 #pragma omp loop loop-clause[optseq] new-line
40458 for-loop */
40459
40460 #define OMP_LOOP_CLAUSE_MASK \
40461 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
40465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
40466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
40467
40468 static tree
40469 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
40470 char *p_name, omp_clause_mask mask, tree *cclauses,
40471 bool *if_p)
40472 {
40473 tree clauses, sb, ret;
40474 unsigned int save;
40475 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40476
40477 strcat (p_name, " loop");
40478 mask |= OMP_LOOP_CLAUSE_MASK;
40479
40480 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40481 cclauses == NULL);
40482 if (cclauses)
40483 {
40484 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
40485 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
40486 }
40487
40488 keep_next_level (true);
40489 sb = begin_omp_structured_block ();
40490 save = cp_parser_begin_omp_structured_block (parser);
40491
40492 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
40493
40494 cp_parser_end_omp_structured_block (parser, save);
40495 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
40496
40497 return ret;
40498 }
40499
40500 /* OpenMP 4.0:
40501 #pragma omp simd simd-clause[optseq] new-line
40502 for-loop */
40503
40504 #define OMP_SIMD_CLAUSE_MASK \
40505 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
40506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
40507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
40508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
40509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
40513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
40515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
40516
40517 static tree
40518 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
40519 char *p_name, omp_clause_mask mask, tree *cclauses,
40520 bool *if_p)
40521 {
40522 tree clauses, sb, ret;
40523 unsigned int save;
40524 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40525
40526 strcat (p_name, " simd");
40527 mask |= OMP_SIMD_CLAUSE_MASK;
40528
40529 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40530 cclauses == NULL);
40531 if (cclauses)
40532 {
40533 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
40534 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
40535 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
40536 OMP_CLAUSE_ORDERED);
40537 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
40538 {
40539 error_at (OMP_CLAUSE_LOCATION (c),
40540 "%<ordered%> clause with parameter may not be specified "
40541 "on %qs construct", p_name);
40542 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
40543 }
40544 }
40545
40546 keep_next_level (true);
40547 sb = begin_omp_structured_block ();
40548 save = cp_parser_begin_omp_structured_block (parser);
40549
40550 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
40551
40552 cp_parser_end_omp_structured_block (parser, save);
40553 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
40554
40555 return ret;
40556 }
40557
40558 /* OpenMP 2.5:
40559 #pragma omp for for-clause[optseq] new-line
40560 for-loop
40561
40562 OpenMP 4.0:
40563 #pragma omp for simd for-simd-clause[optseq] new-line
40564 for-loop */
40565
40566 #define OMP_FOR_CLAUSE_MASK \
40567 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
40571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
40573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
40574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
40575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
40576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
40578
40579 static tree
40580 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
40581 char *p_name, omp_clause_mask mask, tree *cclauses,
40582 bool *if_p)
40583 {
40584 tree clauses, sb, ret;
40585 unsigned int save;
40586 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40587
40588 strcat (p_name, " for");
40589 mask |= OMP_FOR_CLAUSE_MASK;
40590 /* parallel for{, simd} disallows nowait clause, but for
40591 target {teams distribute ,}parallel for{, simd} it should be accepted. */
40592 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
40593 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
40594 /* Composite distribute parallel for{, simd} disallows ordered clause. */
40595 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
40596 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
40597
40598 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40599 {
40600 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40601 const char *p = IDENTIFIER_POINTER (id);
40602
40603 if (strcmp (p, "simd") == 0)
40604 {
40605 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40606 if (cclauses == NULL)
40607 cclauses = cclauses_buf;
40608
40609 cp_lexer_consume_token (parser->lexer);
40610 if (!flag_openmp) /* flag_openmp_simd */
40611 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40612 cclauses, if_p);
40613 sb = begin_omp_structured_block ();
40614 save = cp_parser_begin_omp_structured_block (parser);
40615 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40616 cclauses, if_p);
40617 cp_parser_end_omp_structured_block (parser, save);
40618 tree body = finish_omp_structured_block (sb);
40619 if (ret == NULL)
40620 return ret;
40621 ret = make_node (OMP_FOR);
40622 TREE_TYPE (ret) = void_type_node;
40623 OMP_FOR_BODY (ret) = body;
40624 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
40625 SET_EXPR_LOCATION (ret, loc);
40626 add_stmt (ret);
40627 return ret;
40628 }
40629 }
40630 if (!flag_openmp) /* flag_openmp_simd */
40631 {
40632 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40633 return NULL_TREE;
40634 }
40635
40636 /* Composite distribute parallel for disallows linear clause. */
40637 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
40638 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
40639
40640 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40641 cclauses == NULL);
40642 if (cclauses)
40643 {
40644 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
40645 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
40646 }
40647
40648 keep_next_level (true);
40649 sb = begin_omp_structured_block ();
40650 save = cp_parser_begin_omp_structured_block (parser);
40651
40652 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
40653
40654 cp_parser_end_omp_structured_block (parser, save);
40655 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
40656
40657 return ret;
40658 }
40659
40660 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
40661 omp_clause_mask, tree *, bool *);
40662
40663 /* OpenMP 2.5:
40664 # pragma omp master new-line
40665 structured-block */
40666
40667 static tree
40668 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
40669 char *p_name, omp_clause_mask mask, tree *cclauses,
40670 bool *if_p)
40671 {
40672 tree clauses, sb, ret;
40673 unsigned int save;
40674 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40675
40676 strcat (p_name, " master");
40677
40678 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40679 {
40680 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40681 const char *p = IDENTIFIER_POINTER (id);
40682
40683 if (strcmp (p, "taskloop") == 0)
40684 {
40685 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40686 if (cclauses == NULL)
40687 cclauses = cclauses_buf;
40688
40689 cp_lexer_consume_token (parser->lexer);
40690 if (!flag_openmp) /* flag_openmp_simd */
40691 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
40692 cclauses, if_p);
40693 sb = begin_omp_structured_block ();
40694 save = cp_parser_begin_omp_structured_block (parser);
40695 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
40696 cclauses, if_p);
40697 cp_parser_end_omp_structured_block (parser, save);
40698 tree body = finish_omp_structured_block (sb);
40699 if (ret == NULL)
40700 return ret;
40701 return c_finish_omp_master (loc, body);
40702 }
40703 }
40704 if (!flag_openmp) /* flag_openmp_simd */
40705 {
40706 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40707 return NULL_TREE;
40708 }
40709
40710 if (cclauses)
40711 {
40712 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40713 false);
40714 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
40715 }
40716 else
40717 cp_parser_require_pragma_eol (parser, pragma_tok);
40718
40719 return c_finish_omp_master (loc,
40720 cp_parser_omp_structured_block (parser, if_p));
40721 }
40722
40723 /* OpenMP 2.5:
40724 # pragma omp ordered new-line
40725 structured-block
40726
40727 OpenMP 4.5:
40728 # pragma omp ordered ordered-clauses new-line
40729 structured-block */
40730
40731 #define OMP_ORDERED_CLAUSE_MASK \
40732 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
40733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
40734
40735 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
40736 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
40737
40738 static bool
40739 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
40740 enum pragma_context context, bool *if_p)
40741 {
40742 location_t loc = pragma_tok->location;
40743
40744 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40745 {
40746 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40747 const char *p = IDENTIFIER_POINTER (id);
40748
40749 if (strcmp (p, "depend") == 0)
40750 {
40751 if (!flag_openmp) /* flag_openmp_simd */
40752 {
40753 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40754 return false;
40755 }
40756 if (context == pragma_stmt)
40757 {
40758 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
40759 "%<depend%> clause may only be used in compound "
40760 "statements");
40761 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40762 return false;
40763 }
40764 tree clauses
40765 = cp_parser_omp_all_clauses (parser,
40766 OMP_ORDERED_DEPEND_CLAUSE_MASK,
40767 "#pragma omp ordered", pragma_tok);
40768 c_finish_omp_ordered (loc, clauses, NULL_TREE);
40769 return false;
40770 }
40771 }
40772
40773 tree clauses
40774 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
40775 "#pragma omp ordered", pragma_tok);
40776
40777 if (!flag_openmp /* flag_openmp_simd */
40778 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
40779 return false;
40780
40781 c_finish_omp_ordered (loc, clauses,
40782 cp_parser_omp_structured_block (parser, if_p));
40783 return true;
40784 }
40785
40786 /* OpenMP 2.5:
40787
40788 section-scope:
40789 { section-sequence }
40790
40791 section-sequence:
40792 section-directive[opt] structured-block
40793 section-sequence section-directive structured-block */
40794
40795 static tree
40796 cp_parser_omp_sections_scope (cp_parser *parser)
40797 {
40798 tree stmt, substmt;
40799 bool error_suppress = false;
40800 cp_token *tok;
40801
40802 matching_braces braces;
40803 if (!braces.require_open (parser))
40804 return NULL_TREE;
40805
40806 stmt = push_stmt_list ();
40807
40808 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
40809 != PRAGMA_OMP_SECTION)
40810 {
40811 substmt = cp_parser_omp_structured_block (parser, NULL);
40812 substmt = build1 (OMP_SECTION, void_type_node, substmt);
40813 add_stmt (substmt);
40814 }
40815
40816 while (1)
40817 {
40818 tok = cp_lexer_peek_token (parser->lexer);
40819 if (tok->type == CPP_CLOSE_BRACE)
40820 break;
40821 if (tok->type == CPP_EOF)
40822 break;
40823
40824 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
40825 {
40826 cp_lexer_consume_token (parser->lexer);
40827 cp_parser_require_pragma_eol (parser, tok);
40828 error_suppress = false;
40829 }
40830 else if (!error_suppress)
40831 {
40832 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
40833 error_suppress = true;
40834 }
40835
40836 substmt = cp_parser_omp_structured_block (parser, NULL);
40837 substmt = build1 (OMP_SECTION, void_type_node, substmt);
40838 add_stmt (substmt);
40839 }
40840 braces.require_close (parser);
40841
40842 substmt = pop_stmt_list (stmt);
40843
40844 stmt = make_node (OMP_SECTIONS);
40845 TREE_TYPE (stmt) = void_type_node;
40846 OMP_SECTIONS_BODY (stmt) = substmt;
40847
40848 add_stmt (stmt);
40849 return stmt;
40850 }
40851
40852 /* OpenMP 2.5:
40853 # pragma omp sections sections-clause[optseq] newline
40854 sections-scope */
40855
40856 #define OMP_SECTIONS_CLAUSE_MASK \
40857 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40863
40864 static tree
40865 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
40866 char *p_name, omp_clause_mask mask, tree *cclauses)
40867 {
40868 tree clauses, ret;
40869 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40870
40871 strcat (p_name, " sections");
40872 mask |= OMP_SECTIONS_CLAUSE_MASK;
40873 if (cclauses)
40874 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
40875
40876 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40877 cclauses == NULL);
40878 if (cclauses)
40879 {
40880 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
40881 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
40882 }
40883
40884 ret = cp_parser_omp_sections_scope (parser);
40885 if (ret)
40886 OMP_SECTIONS_CLAUSES (ret) = clauses;
40887
40888 return ret;
40889 }
40890
40891 /* OpenMP 2.5:
40892 # pragma omp parallel parallel-clause[optseq] new-line
40893 structured-block
40894 # pragma omp parallel for parallel-for-clause[optseq] new-line
40895 structured-block
40896 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
40897 structured-block
40898
40899 OpenMP 4.0:
40900 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
40901 structured-block */
40902
40903 #define OMP_PARALLEL_CLAUSE_MASK \
40904 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
40908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
40909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
40910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
40912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
40914
40915 static tree
40916 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
40917 char *p_name, omp_clause_mask mask, tree *cclauses,
40918 bool *if_p)
40919 {
40920 tree stmt, clauses, block;
40921 unsigned int save;
40922 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40923
40924 strcat (p_name, " parallel");
40925 mask |= OMP_PARALLEL_CLAUSE_MASK;
40926 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
40927 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
40928 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
40929 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
40930
40931 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
40932 {
40933 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40934 if (cclauses == NULL)
40935 cclauses = cclauses_buf;
40936
40937 cp_lexer_consume_token (parser->lexer);
40938 if (!flag_openmp) /* flag_openmp_simd */
40939 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
40940 if_p);
40941 block = begin_omp_parallel ();
40942 save = cp_parser_begin_omp_structured_block (parser);
40943 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
40944 if_p);
40945 cp_parser_end_omp_structured_block (parser, save);
40946 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
40947 block);
40948 if (ret == NULL_TREE)
40949 return ret;
40950 OMP_PARALLEL_COMBINED (stmt) = 1;
40951 return stmt;
40952 }
40953 /* When combined with distribute, parallel has to be followed by for.
40954 #pragma omp target parallel is allowed though. */
40955 else if (cclauses
40956 && (mask & (OMP_CLAUSE_MASK_1
40957 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
40958 {
40959 error_at (loc, "expected %<for%> after %qs", p_name);
40960 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40961 return NULL_TREE;
40962 }
40963 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40964 {
40965 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40966 const char *p = IDENTIFIER_POINTER (id);
40967 if (cclauses == NULL && strcmp (p, "master") == 0)
40968 {
40969 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40970 cclauses = cclauses_buf;
40971
40972 cp_lexer_consume_token (parser->lexer);
40973 if (!flag_openmp) /* flag_openmp_simd */
40974 return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
40975 cclauses, if_p);
40976 block = begin_omp_parallel ();
40977 save = cp_parser_begin_omp_structured_block (parser);
40978 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
40979 cclauses, if_p);
40980 cp_parser_end_omp_structured_block (parser, save);
40981 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
40982 block);
40983 if (ret == NULL_TREE)
40984 return ret;
40985 OMP_PARALLEL_COMBINED (stmt) = 1;
40986 return stmt;
40987 }
40988 else if (strcmp (p, "loop") == 0)
40989 {
40990 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40991 if (cclauses == NULL)
40992 cclauses = cclauses_buf;
40993
40994 cp_lexer_consume_token (parser->lexer);
40995 if (!flag_openmp) /* flag_openmp_simd */
40996 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
40997 cclauses, if_p);
40998 block = begin_omp_parallel ();
40999 save = cp_parser_begin_omp_structured_block (parser);
41000 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
41001 cclauses, if_p);
41002 cp_parser_end_omp_structured_block (parser, save);
41003 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
41004 block);
41005 if (ret == NULL_TREE)
41006 return ret;
41007 OMP_PARALLEL_COMBINED (stmt) = 1;
41008 return stmt;
41009 }
41010 else if (!flag_openmp) /* flag_openmp_simd */
41011 {
41012 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41013 return NULL_TREE;
41014 }
41015 else if (cclauses == NULL && strcmp (p, "sections") == 0)
41016 {
41017 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
41018 cclauses = cclauses_buf;
41019
41020 cp_lexer_consume_token (parser->lexer);
41021 block = begin_omp_parallel ();
41022 save = cp_parser_begin_omp_structured_block (parser);
41023 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
41024 cp_parser_end_omp_structured_block (parser, save);
41025 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
41026 block);
41027 OMP_PARALLEL_COMBINED (stmt) = 1;
41028 return stmt;
41029 }
41030 }
41031 else if (!flag_openmp) /* flag_openmp_simd */
41032 {
41033 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41034 return NULL_TREE;
41035 }
41036
41037 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
41038 cclauses == NULL);
41039 if (cclauses)
41040 {
41041 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
41042 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
41043 }
41044
41045 block = begin_omp_parallel ();
41046 save = cp_parser_begin_omp_structured_block (parser);
41047 cp_parser_statement (parser, NULL_TREE, false, if_p);
41048 cp_parser_end_omp_structured_block (parser, save);
41049 stmt = finish_omp_parallel (clauses, block);
41050 return stmt;
41051 }
41052
41053 /* OpenMP 2.5:
41054 # pragma omp single single-clause[optseq] new-line
41055 structured-block */
41056
41057 #define OMP_SINGLE_CLAUSE_MASK \
41058 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
41061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
41063
41064 static tree
41065 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41066 {
41067 tree stmt = make_node (OMP_SINGLE);
41068 TREE_TYPE (stmt) = void_type_node;
41069 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41070
41071 OMP_SINGLE_CLAUSES (stmt)
41072 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
41073 "#pragma omp single", pragma_tok);
41074 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
41075
41076 return add_stmt (stmt);
41077 }
41078
41079 /* OpenMP 3.0:
41080 # pragma omp task task-clause[optseq] new-line
41081 structured-block */
41082
41083 #define OMP_TASK_CLAUSE_MASK \
41084 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
41086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
41087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
41090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
41091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
41092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
41094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
41096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH))
41097
41098 static tree
41099 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41100 {
41101 tree clauses, block;
41102 unsigned int save;
41103
41104 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
41105 "#pragma omp task", pragma_tok);
41106 block = begin_omp_task ();
41107 save = cp_parser_begin_omp_structured_block (parser);
41108 cp_parser_statement (parser, NULL_TREE, false, if_p);
41109 cp_parser_end_omp_structured_block (parser, save);
41110 return finish_omp_task (clauses, block);
41111 }
41112
41113 /* OpenMP 3.0:
41114 # pragma omp taskwait new-line
41115
41116 OpenMP 5.0:
41117 # pragma omp taskwait taskwait-clause[opt] new-line */
41118
41119 #define OMP_TASKWAIT_CLAUSE_MASK \
41120 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
41121
41122 static void
41123 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
41124 {
41125 tree clauses
41126 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
41127 "#pragma omp taskwait", pragma_tok);
41128
41129 if (clauses)
41130 {
41131 tree stmt = make_node (OMP_TASK);
41132 TREE_TYPE (stmt) = void_node;
41133 OMP_TASK_CLAUSES (stmt) = clauses;
41134 OMP_TASK_BODY (stmt) = NULL_TREE;
41135 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41136 add_stmt (stmt);
41137 }
41138 else
41139 finish_omp_taskwait ();
41140 }
41141
41142 /* OpenMP 3.1:
41143 # pragma omp taskyield new-line */
41144
41145 static void
41146 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
41147 {
41148 cp_parser_require_pragma_eol (parser, pragma_tok);
41149 finish_omp_taskyield ();
41150 }
41151
41152 /* OpenMP 4.0:
41153 # pragma omp taskgroup new-line
41154 structured-block
41155
41156 OpenMP 5.0:
41157 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
41158
41159 #define OMP_TASKGROUP_CLAUSE_MASK \
41160 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
41162
41163 static tree
41164 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41165 {
41166 tree clauses
41167 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
41168 "#pragma omp taskgroup", pragma_tok);
41169 return c_finish_omp_taskgroup (input_location,
41170 cp_parser_omp_structured_block (parser,
41171 if_p),
41172 clauses);
41173 }
41174
41175
41176 /* OpenMP 2.5:
41177 # pragma omp threadprivate (variable-list) */
41178
41179 static void
41180 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
41181 {
41182 tree vars;
41183
41184 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
41185 cp_parser_require_pragma_eol (parser, pragma_tok);
41186
41187 finish_omp_threadprivate (vars);
41188 }
41189
41190 /* OpenMP 4.0:
41191 # pragma omp cancel cancel-clause[optseq] new-line */
41192
41193 #define OMP_CANCEL_CLAUSE_MASK \
41194 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
41195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
41196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
41197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
41198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
41199
41200 static void
41201 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
41202 {
41203 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
41204 "#pragma omp cancel", pragma_tok);
41205 finish_omp_cancel (clauses);
41206 }
41207
41208 /* OpenMP 4.0:
41209 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
41210
41211 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
41212 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
41213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
41214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
41215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
41216
41217 static void
41218 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
41219 enum pragma_context context)
41220 {
41221 tree clauses;
41222 bool point_seen = false;
41223
41224 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41225 {
41226 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41227 const char *p = IDENTIFIER_POINTER (id);
41228
41229 if (strcmp (p, "point") == 0)
41230 {
41231 cp_lexer_consume_token (parser->lexer);
41232 point_seen = true;
41233 }
41234 }
41235 if (!point_seen)
41236 {
41237 cp_parser_error (parser, "expected %<point%>");
41238 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41239 return;
41240 }
41241
41242 if (context != pragma_compound)
41243 {
41244 if (context == pragma_stmt)
41245 error_at (pragma_tok->location,
41246 "%<#pragma %s%> may only be used in compound statements",
41247 "omp cancellation point");
41248 else
41249 cp_parser_error (parser, "expected declaration specifiers");
41250 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41251 return;
41252 }
41253
41254 clauses = cp_parser_omp_all_clauses (parser,
41255 OMP_CANCELLATION_POINT_CLAUSE_MASK,
41256 "#pragma omp cancellation point",
41257 pragma_tok);
41258 finish_omp_cancellation_point (clauses);
41259 }
41260
41261 /* OpenMP 4.0:
41262 #pragma omp distribute distribute-clause[optseq] new-line
41263 for-loop */
41264
41265 #define OMP_DISTRIBUTE_CLAUSE_MASK \
41266 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
41269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
41270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
41272
41273 static tree
41274 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
41275 char *p_name, omp_clause_mask mask, tree *cclauses,
41276 bool *if_p)
41277 {
41278 tree clauses, sb, ret;
41279 unsigned int save;
41280 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41281
41282 strcat (p_name, " distribute");
41283 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
41284
41285 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41286 {
41287 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41288 const char *p = IDENTIFIER_POINTER (id);
41289 bool simd = false;
41290 bool parallel = false;
41291
41292 if (strcmp (p, "simd") == 0)
41293 simd = true;
41294 else
41295 parallel = strcmp (p, "parallel") == 0;
41296 if (parallel || simd)
41297 {
41298 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
41299 if (cclauses == NULL)
41300 cclauses = cclauses_buf;
41301 cp_lexer_consume_token (parser->lexer);
41302 if (!flag_openmp) /* flag_openmp_simd */
41303 {
41304 if (simd)
41305 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
41306 cclauses, if_p);
41307 else
41308 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
41309 cclauses, if_p);
41310 }
41311 sb = begin_omp_structured_block ();
41312 save = cp_parser_begin_omp_structured_block (parser);
41313 if (simd)
41314 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
41315 cclauses, if_p);
41316 else
41317 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
41318 cclauses, if_p);
41319 cp_parser_end_omp_structured_block (parser, save);
41320 tree body = finish_omp_structured_block (sb);
41321 if (ret == NULL)
41322 return ret;
41323 ret = make_node (OMP_DISTRIBUTE);
41324 TREE_TYPE (ret) = void_type_node;
41325 OMP_FOR_BODY (ret) = body;
41326 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
41327 SET_EXPR_LOCATION (ret, loc);
41328 add_stmt (ret);
41329 return ret;
41330 }
41331 }
41332 if (!flag_openmp) /* flag_openmp_simd */
41333 {
41334 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41335 return NULL_TREE;
41336 }
41337
41338 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
41339 cclauses == NULL);
41340 if (cclauses)
41341 {
41342 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
41343 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
41344 }
41345
41346 keep_next_level (true);
41347 sb = begin_omp_structured_block ();
41348 save = cp_parser_begin_omp_structured_block (parser);
41349
41350 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
41351
41352 cp_parser_end_omp_structured_block (parser, save);
41353 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
41354
41355 return ret;
41356 }
41357
41358 /* OpenMP 4.0:
41359 # pragma omp teams teams-clause[optseq] new-line
41360 structured-block */
41361
41362 #define OMP_TEAMS_CLAUSE_MASK \
41363 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
41366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
41367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
41368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
41369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
41371
41372 static tree
41373 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
41374 char *p_name, omp_clause_mask mask, tree *cclauses,
41375 bool *if_p)
41376 {
41377 tree clauses, sb, ret;
41378 unsigned int save;
41379 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41380
41381 strcat (p_name, " teams");
41382 mask |= OMP_TEAMS_CLAUSE_MASK;
41383
41384 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41385 {
41386 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41387 const char *p = IDENTIFIER_POINTER (id);
41388 if (strcmp (p, "distribute") == 0)
41389 {
41390 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
41391 if (cclauses == NULL)
41392 cclauses = cclauses_buf;
41393
41394 cp_lexer_consume_token (parser->lexer);
41395 if (!flag_openmp) /* flag_openmp_simd */
41396 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
41397 cclauses, if_p);
41398 keep_next_level (true);
41399 sb = begin_omp_structured_block ();
41400 save = cp_parser_begin_omp_structured_block (parser);
41401 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
41402 cclauses, if_p);
41403 cp_parser_end_omp_structured_block (parser, save);
41404 tree body = finish_omp_structured_block (sb);
41405 if (ret == NULL)
41406 return ret;
41407 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
41408 ret = make_node (OMP_TEAMS);
41409 TREE_TYPE (ret) = void_type_node;
41410 OMP_TEAMS_CLAUSES (ret) = clauses;
41411 OMP_TEAMS_BODY (ret) = body;
41412 OMP_TEAMS_COMBINED (ret) = 1;
41413 SET_EXPR_LOCATION (ret, loc);
41414 return add_stmt (ret);
41415 }
41416 else if (strcmp (p, "loop") == 0)
41417 {
41418 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
41419 if (cclauses == NULL)
41420 cclauses = cclauses_buf;
41421
41422 cp_lexer_consume_token (parser->lexer);
41423 if (!flag_openmp) /* flag_openmp_simd */
41424 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
41425 cclauses, if_p);
41426 keep_next_level (true);
41427 sb = begin_omp_structured_block ();
41428 save = cp_parser_begin_omp_structured_block (parser);
41429 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
41430 cclauses, if_p);
41431 cp_parser_end_omp_structured_block (parser, save);
41432 tree body = finish_omp_structured_block (sb);
41433 if (ret == NULL)
41434 return ret;
41435 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
41436 ret = make_node (OMP_TEAMS);
41437 TREE_TYPE (ret) = void_type_node;
41438 OMP_TEAMS_CLAUSES (ret) = clauses;
41439 OMP_TEAMS_BODY (ret) = body;
41440 OMP_TEAMS_COMBINED (ret) = 1;
41441 SET_EXPR_LOCATION (ret, loc);
41442 return add_stmt (ret);
41443 }
41444 }
41445 if (!flag_openmp) /* flag_openmp_simd */
41446 {
41447 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41448 return NULL_TREE;
41449 }
41450
41451 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
41452 cclauses == NULL);
41453 if (cclauses)
41454 {
41455 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
41456 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
41457 }
41458
41459 tree stmt = make_node (OMP_TEAMS);
41460 TREE_TYPE (stmt) = void_type_node;
41461 OMP_TEAMS_CLAUSES (stmt) = clauses;
41462 keep_next_level (true);
41463 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
41464 SET_EXPR_LOCATION (stmt, loc);
41465
41466 return add_stmt (stmt);
41467 }
41468
41469 /* OpenMP 4.0:
41470 # pragma omp target data target-data-clause[optseq] new-line
41471 structured-block */
41472
41473 #define OMP_TARGET_DATA_CLAUSE_MASK \
41474 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
41476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
41478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
41479
41480 static tree
41481 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41482 {
41483 tree clauses
41484 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
41485 "#pragma omp target data", pragma_tok);
41486 c_omp_adjust_map_clauses (clauses, false);
41487 int map_seen = 0;
41488 for (tree *pc = &clauses; *pc;)
41489 {
41490 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
41491 switch (OMP_CLAUSE_MAP_KIND (*pc))
41492 {
41493 case GOMP_MAP_TO:
41494 case GOMP_MAP_ALWAYS_TO:
41495 case GOMP_MAP_FROM:
41496 case GOMP_MAP_ALWAYS_FROM:
41497 case GOMP_MAP_TOFROM:
41498 case GOMP_MAP_ALWAYS_TOFROM:
41499 case GOMP_MAP_ALLOC:
41500 map_seen = 3;
41501 break;
41502 case GOMP_MAP_FIRSTPRIVATE_POINTER:
41503 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
41504 case GOMP_MAP_ALWAYS_POINTER:
41505 case GOMP_MAP_ATTACH_DETACH:
41506 break;
41507 default:
41508 map_seen |= 1;
41509 error_at (OMP_CLAUSE_LOCATION (*pc),
41510 "%<#pragma omp target data%> with map-type other "
41511 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
41512 "on %<map%> clause");
41513 *pc = OMP_CLAUSE_CHAIN (*pc);
41514 continue;
41515 }
41516 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
41517 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
41518 map_seen = 3;
41519 pc = &OMP_CLAUSE_CHAIN (*pc);
41520 }
41521
41522 if (map_seen != 3)
41523 {
41524 if (map_seen == 0)
41525 error_at (pragma_tok->location,
41526 "%<#pragma omp target data%> must contain at least "
41527 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
41528 "clause");
41529 return NULL_TREE;
41530 }
41531
41532 tree stmt = make_node (OMP_TARGET_DATA);
41533 TREE_TYPE (stmt) = void_type_node;
41534 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
41535
41536 keep_next_level (true);
41537 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
41538
41539 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41540 return add_stmt (stmt);
41541 }
41542
41543 /* OpenMP 4.5:
41544 # pragma omp target enter data target-enter-data-clause[optseq] new-line
41545 structured-block */
41546
41547 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
41548 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
41550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
41553
41554 static tree
41555 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
41556 enum pragma_context context)
41557 {
41558 bool data_seen = false;
41559 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41560 {
41561 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41562 const char *p = IDENTIFIER_POINTER (id);
41563
41564 if (strcmp (p, "data") == 0)
41565 {
41566 cp_lexer_consume_token (parser->lexer);
41567 data_seen = true;
41568 }
41569 }
41570 if (!data_seen)
41571 {
41572 cp_parser_error (parser, "expected %<data%>");
41573 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41574 return NULL_TREE;
41575 }
41576
41577 if (context == pragma_stmt)
41578 {
41579 error_at (pragma_tok->location,
41580 "%<#pragma %s%> may only be used in compound statements",
41581 "omp target enter data");
41582 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41583 return NULL_TREE;
41584 }
41585
41586 tree clauses
41587 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
41588 "#pragma omp target enter data", pragma_tok);
41589 c_omp_adjust_map_clauses (clauses, false);
41590 int map_seen = 0;
41591 for (tree *pc = &clauses; *pc;)
41592 {
41593 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
41594 switch (OMP_CLAUSE_MAP_KIND (*pc))
41595 {
41596 case GOMP_MAP_TO:
41597 case GOMP_MAP_ALWAYS_TO:
41598 case GOMP_MAP_ALLOC:
41599 map_seen = 3;
41600 break;
41601 case GOMP_MAP_FIRSTPRIVATE_POINTER:
41602 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
41603 case GOMP_MAP_ALWAYS_POINTER:
41604 case GOMP_MAP_ATTACH_DETACH:
41605 break;
41606 default:
41607 map_seen |= 1;
41608 error_at (OMP_CLAUSE_LOCATION (*pc),
41609 "%<#pragma omp target enter data%> with map-type other "
41610 "than %<to%> or %<alloc%> on %<map%> clause");
41611 *pc = OMP_CLAUSE_CHAIN (*pc);
41612 continue;
41613 }
41614 pc = &OMP_CLAUSE_CHAIN (*pc);
41615 }
41616
41617 if (map_seen != 3)
41618 {
41619 if (map_seen == 0)
41620 error_at (pragma_tok->location,
41621 "%<#pragma omp target enter data%> must contain at least "
41622 "one %<map%> clause");
41623 return NULL_TREE;
41624 }
41625
41626 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
41627 TREE_TYPE (stmt) = void_type_node;
41628 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
41629 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41630 return add_stmt (stmt);
41631 }
41632
41633 /* OpenMP 4.5:
41634 # pragma omp target exit data target-enter-data-clause[optseq] new-line
41635 structured-block */
41636
41637 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
41638 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
41640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
41643
41644 static tree
41645 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
41646 enum pragma_context context)
41647 {
41648 bool data_seen = false;
41649 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41650 {
41651 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41652 const char *p = IDENTIFIER_POINTER (id);
41653
41654 if (strcmp (p, "data") == 0)
41655 {
41656 cp_lexer_consume_token (parser->lexer);
41657 data_seen = true;
41658 }
41659 }
41660 if (!data_seen)
41661 {
41662 cp_parser_error (parser, "expected %<data%>");
41663 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41664 return NULL_TREE;
41665 }
41666
41667 if (context == pragma_stmt)
41668 {
41669 error_at (pragma_tok->location,
41670 "%<#pragma %s%> may only be used in compound statements",
41671 "omp target exit data");
41672 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41673 return NULL_TREE;
41674 }
41675
41676 tree clauses
41677 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
41678 "#pragma omp target exit data", pragma_tok);
41679 c_omp_adjust_map_clauses (clauses, false);
41680 int map_seen = 0;
41681 for (tree *pc = &clauses; *pc;)
41682 {
41683 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
41684 switch (OMP_CLAUSE_MAP_KIND (*pc))
41685 {
41686 case GOMP_MAP_FROM:
41687 case GOMP_MAP_ALWAYS_FROM:
41688 case GOMP_MAP_RELEASE:
41689 case GOMP_MAP_DELETE:
41690 map_seen = 3;
41691 break;
41692 case GOMP_MAP_FIRSTPRIVATE_POINTER:
41693 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
41694 case GOMP_MAP_ALWAYS_POINTER:
41695 case GOMP_MAP_ATTACH_DETACH:
41696 break;
41697 default:
41698 map_seen |= 1;
41699 error_at (OMP_CLAUSE_LOCATION (*pc),
41700 "%<#pragma omp target exit data%> with map-type other "
41701 "than %<from%>, %<release%> or %<delete%> on %<map%>"
41702 " clause");
41703 *pc = OMP_CLAUSE_CHAIN (*pc);
41704 continue;
41705 }
41706 pc = &OMP_CLAUSE_CHAIN (*pc);
41707 }
41708
41709 if (map_seen != 3)
41710 {
41711 if (map_seen == 0)
41712 error_at (pragma_tok->location,
41713 "%<#pragma omp target exit data%> must contain at least "
41714 "one %<map%> clause");
41715 return NULL_TREE;
41716 }
41717
41718 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
41719 TREE_TYPE (stmt) = void_type_node;
41720 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
41721 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41722 return add_stmt (stmt);
41723 }
41724
41725 /* OpenMP 4.0:
41726 # pragma omp target update target-update-clause[optseq] new-line */
41727
41728 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
41729 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
41730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
41731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
41735
41736 static bool
41737 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
41738 enum pragma_context context)
41739 {
41740 if (context == pragma_stmt)
41741 {
41742 error_at (pragma_tok->location,
41743 "%<#pragma %s%> may only be used in compound statements",
41744 "omp target update");
41745 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41746 return false;
41747 }
41748
41749 tree clauses
41750 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
41751 "#pragma omp target update", pragma_tok);
41752 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
41753 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
41754 {
41755 error_at (pragma_tok->location,
41756 "%<#pragma omp target update%> must contain at least one "
41757 "%<from%> or %<to%> clauses");
41758 return false;
41759 }
41760
41761 tree stmt = make_node (OMP_TARGET_UPDATE);
41762 TREE_TYPE (stmt) = void_type_node;
41763 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
41764 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41765 add_stmt (stmt);
41766 return false;
41767 }
41768
41769 /* OpenMP 4.0:
41770 # pragma omp target target-clause[optseq] new-line
41771 structured-block */
41772
41773 #define OMP_TARGET_CLAUSE_MASK \
41774 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
41776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
41779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
41782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
41784
41785 static bool
41786 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
41787 enum pragma_context context, bool *if_p)
41788 {
41789 tree *pc = NULL, stmt;
41790
41791 if (flag_openmp)
41792 omp_requires_mask
41793 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
41794
41795 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41796 {
41797 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41798 const char *p = IDENTIFIER_POINTER (id);
41799 enum tree_code ccode = ERROR_MARK;
41800
41801 if (strcmp (p, "teams") == 0)
41802 ccode = OMP_TEAMS;
41803 else if (strcmp (p, "parallel") == 0)
41804 ccode = OMP_PARALLEL;
41805 else if (strcmp (p, "simd") == 0)
41806 ccode = OMP_SIMD;
41807 if (ccode != ERROR_MARK)
41808 {
41809 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
41810 char p_name[sizeof ("#pragma omp target teams distribute "
41811 "parallel for simd")];
41812
41813 cp_lexer_consume_token (parser->lexer);
41814 strcpy (p_name, "#pragma omp target");
41815 if (!flag_openmp) /* flag_openmp_simd */
41816 {
41817 tree stmt;
41818 switch (ccode)
41819 {
41820 case OMP_TEAMS:
41821 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
41822 OMP_TARGET_CLAUSE_MASK,
41823 cclauses, if_p);
41824 break;
41825 case OMP_PARALLEL:
41826 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
41827 OMP_TARGET_CLAUSE_MASK,
41828 cclauses, if_p);
41829 break;
41830 case OMP_SIMD:
41831 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
41832 OMP_TARGET_CLAUSE_MASK,
41833 cclauses, if_p);
41834 break;
41835 default:
41836 gcc_unreachable ();
41837 }
41838 return stmt != NULL_TREE;
41839 }
41840 keep_next_level (true);
41841 tree sb = begin_omp_structured_block (), ret;
41842 unsigned save = cp_parser_begin_omp_structured_block (parser);
41843 switch (ccode)
41844 {
41845 case OMP_TEAMS:
41846 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
41847 OMP_TARGET_CLAUSE_MASK, cclauses,
41848 if_p);
41849 break;
41850 case OMP_PARALLEL:
41851 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
41852 OMP_TARGET_CLAUSE_MASK, cclauses,
41853 if_p);
41854 break;
41855 case OMP_SIMD:
41856 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
41857 OMP_TARGET_CLAUSE_MASK, cclauses,
41858 if_p);
41859 break;
41860 default:
41861 gcc_unreachable ();
41862 }
41863 cp_parser_end_omp_structured_block (parser, save);
41864 tree body = finish_omp_structured_block (sb);
41865 if (ret == NULL_TREE)
41866 return false;
41867 if (ccode == OMP_TEAMS && !processing_template_decl)
41868 {
41869 /* For combined target teams, ensure the num_teams and
41870 thread_limit clause expressions are evaluated on the host,
41871 before entering the target construct. */
41872 tree c;
41873 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
41874 c; c = OMP_CLAUSE_CHAIN (c))
41875 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
41876 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
41877 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
41878 {
41879 tree expr = OMP_CLAUSE_OPERAND (c, 0);
41880 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
41881 if (expr == error_mark_node)
41882 continue;
41883 tree tmp = TARGET_EXPR_SLOT (expr);
41884 add_stmt (expr);
41885 OMP_CLAUSE_OPERAND (c, 0) = expr;
41886 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
41887 OMP_CLAUSE_FIRSTPRIVATE);
41888 OMP_CLAUSE_DECL (tc) = tmp;
41889 OMP_CLAUSE_CHAIN (tc)
41890 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
41891 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
41892 }
41893 }
41894 tree stmt = make_node (OMP_TARGET);
41895 TREE_TYPE (stmt) = void_type_node;
41896 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
41897 OMP_TARGET_BODY (stmt) = body;
41898 OMP_TARGET_COMBINED (stmt) = 1;
41899 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41900 add_stmt (stmt);
41901 pc = &OMP_TARGET_CLAUSES (stmt);
41902 goto check_clauses;
41903 }
41904 else if (!flag_openmp) /* flag_openmp_simd */
41905 {
41906 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41907 return false;
41908 }
41909 else if (strcmp (p, "data") == 0)
41910 {
41911 cp_lexer_consume_token (parser->lexer);
41912 cp_parser_omp_target_data (parser, pragma_tok, if_p);
41913 return true;
41914 }
41915 else if (strcmp (p, "enter") == 0)
41916 {
41917 cp_lexer_consume_token (parser->lexer);
41918 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
41919 return false;
41920 }
41921 else if (strcmp (p, "exit") == 0)
41922 {
41923 cp_lexer_consume_token (parser->lexer);
41924 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
41925 return false;
41926 }
41927 else if (strcmp (p, "update") == 0)
41928 {
41929 cp_lexer_consume_token (parser->lexer);
41930 return cp_parser_omp_target_update (parser, pragma_tok, context);
41931 }
41932 }
41933 if (!flag_openmp) /* flag_openmp_simd */
41934 {
41935 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41936 return false;
41937 }
41938
41939 stmt = make_node (OMP_TARGET);
41940 TREE_TYPE (stmt) = void_type_node;
41941
41942 OMP_TARGET_CLAUSES (stmt)
41943 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
41944 "#pragma omp target", pragma_tok);
41945 c_omp_adjust_map_clauses (OMP_TARGET_CLAUSES (stmt), true);
41946
41947 pc = &OMP_TARGET_CLAUSES (stmt);
41948 keep_next_level (true);
41949 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
41950
41951 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41952 add_stmt (stmt);
41953
41954 check_clauses:
41955 while (*pc)
41956 {
41957 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
41958 switch (OMP_CLAUSE_MAP_KIND (*pc))
41959 {
41960 case GOMP_MAP_TO:
41961 case GOMP_MAP_ALWAYS_TO:
41962 case GOMP_MAP_FROM:
41963 case GOMP_MAP_ALWAYS_FROM:
41964 case GOMP_MAP_TOFROM:
41965 case GOMP_MAP_ALWAYS_TOFROM:
41966 case GOMP_MAP_ALLOC:
41967 case GOMP_MAP_FIRSTPRIVATE_POINTER:
41968 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
41969 case GOMP_MAP_ALWAYS_POINTER:
41970 case GOMP_MAP_ATTACH_DETACH:
41971 break;
41972 default:
41973 error_at (OMP_CLAUSE_LOCATION (*pc),
41974 "%<#pragma omp target%> with map-type other "
41975 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
41976 "on %<map%> clause");
41977 *pc = OMP_CLAUSE_CHAIN (*pc);
41978 continue;
41979 }
41980 pc = &OMP_CLAUSE_CHAIN (*pc);
41981 }
41982 return true;
41983 }
41984
41985 /* OpenACC 2.0:
41986 # pragma acc cache (variable-list) new-line
41987 */
41988
41989 static tree
41990 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
41991 {
41992 /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
41993 clauses. */
41994 auto_suppress_location_wrappers sentinel;
41995
41996 tree stmt, clauses;
41997
41998 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
41999 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
42000
42001 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
42002
42003 stmt = make_node (OACC_CACHE);
42004 TREE_TYPE (stmt) = void_type_node;
42005 OACC_CACHE_CLAUSES (stmt) = clauses;
42006 SET_EXPR_LOCATION (stmt, pragma_tok->location);
42007 add_stmt (stmt);
42008
42009 return stmt;
42010 }
42011
42012 /* OpenACC 2.0:
42013 # pragma acc data oacc-data-clause[optseq] new-line
42014 structured-block */
42015
42016 #define OACC_DATA_CLAUSE_MASK \
42017 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
42018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
42019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
42023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
42024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
42026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
42027
42028 static tree
42029 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
42030 {
42031 tree stmt, clauses, block;
42032 unsigned int save;
42033
42034 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
42035 "#pragma acc data", pragma_tok);
42036
42037 block = begin_omp_parallel ();
42038 save = cp_parser_begin_omp_structured_block (parser);
42039 cp_parser_statement (parser, NULL_TREE, false, if_p);
42040 cp_parser_end_omp_structured_block (parser, save);
42041 stmt = finish_oacc_data (clauses, block);
42042 return stmt;
42043 }
42044
42045 /* OpenACC 2.0:
42046 # pragma acc host_data <clauses> new-line
42047 structured-block */
42048
42049 #define OACC_HOST_DATA_CLAUSE_MASK \
42050 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
42051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
42053
42054 static tree
42055 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
42056 {
42057 tree stmt, clauses, block;
42058 unsigned int save;
42059
42060 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
42061 "#pragma acc host_data", pragma_tok);
42062
42063 block = begin_omp_parallel ();
42064 save = cp_parser_begin_omp_structured_block (parser);
42065 cp_parser_statement (parser, NULL_TREE, false, if_p);
42066 cp_parser_end_omp_structured_block (parser, save);
42067 stmt = finish_oacc_host_data (clauses, block);
42068 return stmt;
42069 }
42070
42071 /* OpenACC 2.0:
42072 # pragma acc declare oacc-data-clause[optseq] new-line
42073 */
42074
42075 #define OACC_DECLARE_CLAUSE_MASK \
42076 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
42077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
42081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
42082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
42083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
42084
42085 static tree
42086 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
42087 {
42088 tree clauses, stmt;
42089 bool error = false;
42090 bool found_in_scope = global_bindings_p ();
42091
42092 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
42093 "#pragma acc declare", pragma_tok, true);
42094
42095
42096 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
42097 {
42098 error_at (pragma_tok->location,
42099 "no valid clauses specified in %<#pragma acc declare%>");
42100 return NULL_TREE;
42101 }
42102
42103 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
42104 {
42105 location_t loc = OMP_CLAUSE_LOCATION (t);
42106 tree decl = OMP_CLAUSE_DECL (t);
42107 if (!DECL_P (decl))
42108 {
42109 error_at (loc, "array section in %<#pragma acc declare%>");
42110 error = true;
42111 continue;
42112 }
42113 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
42114 switch (OMP_CLAUSE_MAP_KIND (t))
42115 {
42116 case GOMP_MAP_FIRSTPRIVATE_POINTER:
42117 case GOMP_MAP_ALLOC:
42118 case GOMP_MAP_TO:
42119 case GOMP_MAP_FORCE_DEVICEPTR:
42120 case GOMP_MAP_DEVICE_RESIDENT:
42121 break;
42122
42123 case GOMP_MAP_LINK:
42124 if (!global_bindings_p ()
42125 && (TREE_STATIC (decl)
42126 || !DECL_EXTERNAL (decl)))
42127 {
42128 error_at (loc,
42129 "%qD must be a global variable in "
42130 "%<#pragma acc declare link%>",
42131 decl);
42132 error = true;
42133 continue;
42134 }
42135 break;
42136
42137 default:
42138 if (global_bindings_p ())
42139 {
42140 error_at (loc, "invalid OpenACC clause at file scope");
42141 error = true;
42142 continue;
42143 }
42144 if (DECL_EXTERNAL (decl))
42145 {
42146 error_at (loc,
42147 "invalid use of %<extern%> variable %qD "
42148 "in %<#pragma acc declare%>", decl);
42149 error = true;
42150 continue;
42151 }
42152 else if (TREE_PUBLIC (decl))
42153 {
42154 error_at (loc,
42155 "invalid use of %<global%> variable %qD "
42156 "in %<#pragma acc declare%>", decl);
42157 error = true;
42158 continue;
42159 }
42160 break;
42161 }
42162
42163 if (!found_in_scope)
42164 /* This seems to ignore the existence of cleanup scopes?
42165 What is the meaning for local extern decls? The local
42166 extern is in this scope, but it is referring to a decl that
42167 is namespace scope. */
42168 for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
42169 if (d == decl)
42170 {
42171 found_in_scope = true;
42172 break;
42173 }
42174 if (!found_in_scope)
42175 {
42176 error_at (loc,
42177 "%qD must be a variable declared in the same scope as "
42178 "%<#pragma acc declare%>", decl);
42179 error = true;
42180 continue;
42181 }
42182
42183 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
42184 || lookup_attribute ("omp declare target link",
42185 DECL_ATTRIBUTES (decl)))
42186 {
42187 error_at (loc, "variable %qD used more than once with "
42188 "%<#pragma acc declare%>", decl);
42189 error = true;
42190 continue;
42191 }
42192
42193 if (!error)
42194 {
42195 tree id;
42196
42197 if (DECL_LOCAL_DECL_P (decl))
42198 /* We need to mark the aliased decl, as that is the entity
42199 that is being referred to. This won't work for
42200 dependent variables, but it didn't work for them before
42201 DECL_LOCAL_DECL_P was a thing either. But then
42202 dependent local extern variable decls are as rare as
42203 hen's teeth. */
42204 if (auto alias = DECL_LOCAL_DECL_ALIAS (decl))
42205 decl = alias;
42206
42207 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
42208 id = get_identifier ("omp declare target link");
42209 else
42210 id = get_identifier ("omp declare target");
42211
42212 DECL_ATTRIBUTES (decl)
42213 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
42214 if (current_binding_level->kind == sk_namespace)
42215 {
42216 symtab_node *node = symtab_node::get (decl);
42217 if (node != NULL)
42218 {
42219 node->offloadable = 1;
42220 if (ENABLE_OFFLOADING)
42221 {
42222 g->have_offload = true;
42223 if (is_a <varpool_node *> (node))
42224 vec_safe_push (offload_vars, decl);
42225 }
42226 }
42227 }
42228 }
42229 }
42230
42231 if (error || current_binding_level->kind == sk_namespace)
42232 return NULL_TREE;
42233
42234 stmt = make_node (OACC_DECLARE);
42235 TREE_TYPE (stmt) = void_type_node;
42236 OACC_DECLARE_CLAUSES (stmt) = clauses;
42237 SET_EXPR_LOCATION (stmt, pragma_tok->location);
42238
42239 add_stmt (stmt);
42240
42241 return NULL_TREE;
42242 }
42243
42244 /* OpenACC 2.0:
42245 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
42246
42247 or
42248
42249 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
42250
42251 LOC is the location of the #pragma token.
42252 */
42253
42254 #define OACC_ENTER_DATA_CLAUSE_MASK \
42255 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
42257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
42261
42262 #define OACC_EXIT_DATA_CLAUSE_MASK \
42263 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
42267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
42268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
42269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
42270
42271 static tree
42272 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
42273 bool enter)
42274 {
42275 location_t loc = pragma_tok->location;
42276 tree stmt, clauses;
42277 const char *p = "";
42278
42279 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42280 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
42281
42282 if (strcmp (p, "data") != 0)
42283 {
42284 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
42285 enter ? "enter" : "exit");
42286 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42287 return NULL_TREE;
42288 }
42289
42290 cp_lexer_consume_token (parser->lexer);
42291
42292 if (enter)
42293 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
42294 "#pragma acc enter data", pragma_tok);
42295 else
42296 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
42297 "#pragma acc exit data", pragma_tok);
42298
42299 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
42300 {
42301 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
42302 enter ? "enter" : "exit");
42303 return NULL_TREE;
42304 }
42305
42306 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
42307 TREE_TYPE (stmt) = void_type_node;
42308 OMP_STANDALONE_CLAUSES (stmt) = clauses;
42309 SET_EXPR_LOCATION (stmt, loc);
42310 add_stmt (stmt);
42311 return stmt;
42312 }
42313
42314 /* OpenACC 2.0:
42315 # pragma acc loop oacc-loop-clause[optseq] new-line
42316 structured-block */
42317
42318 #define OACC_LOOP_CLAUSE_MASK \
42319 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
42320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
42321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
42322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
42323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
42324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
42325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
42326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
42327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
42328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
42329
42330 static tree
42331 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
42332 omp_clause_mask mask, tree *cclauses, bool *if_p)
42333 {
42334 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
42335
42336 strcat (p_name, " loop");
42337 mask |= OACC_LOOP_CLAUSE_MASK;
42338
42339 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
42340 cclauses == NULL);
42341 if (cclauses)
42342 {
42343 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
42344 if (*cclauses)
42345 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
42346 if (clauses)
42347 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
42348 }
42349
42350 tree block = begin_omp_structured_block ();
42351 int save = cp_parser_begin_omp_structured_block (parser);
42352 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
42353 cp_parser_end_omp_structured_block (parser, save);
42354 add_stmt (finish_omp_structured_block (block));
42355
42356 return stmt;
42357 }
42358
42359 /* OpenACC 2.0:
42360 # pragma acc kernels oacc-kernels-clause[optseq] new-line
42361 structured-block
42362
42363 or
42364
42365 # pragma acc parallel oacc-parallel-clause[optseq] new-line
42366 structured-block
42367
42368 OpenACC 2.6:
42369
42370 # pragma acc serial oacc-serial-clause[optseq] new-line
42371 */
42372
42373 #define OACC_KERNELS_CLAUSE_MASK \
42374 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
42376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
42377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
42381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
42382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
42384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
42385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
42386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
42387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
42388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
42389
42390 #define OACC_PARALLEL_CLAUSE_MASK \
42391 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
42393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
42394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
42398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
42399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
42400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
42402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
42403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
42404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
42405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
42406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
42407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
42408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
42409
42410 #define OACC_SERIAL_CLAUSE_MASK \
42411 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
42413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
42414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
42418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
42419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
42421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
42422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
42423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
42424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
42425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
42426
42427 static tree
42428 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
42429 char *p_name, bool *if_p)
42430 {
42431 omp_clause_mask mask;
42432 enum tree_code code;
42433 switch (cp_parser_pragma_kind (pragma_tok))
42434 {
42435 case PRAGMA_OACC_KERNELS:
42436 strcat (p_name, " kernels");
42437 mask = OACC_KERNELS_CLAUSE_MASK;
42438 code = OACC_KERNELS;
42439 break;
42440 case PRAGMA_OACC_PARALLEL:
42441 strcat (p_name, " parallel");
42442 mask = OACC_PARALLEL_CLAUSE_MASK;
42443 code = OACC_PARALLEL;
42444 break;
42445 case PRAGMA_OACC_SERIAL:
42446 strcat (p_name, " serial");
42447 mask = OACC_SERIAL_CLAUSE_MASK;
42448 code = OACC_SERIAL;
42449 break;
42450 default:
42451 gcc_unreachable ();
42452 }
42453
42454 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42455 {
42456 const char *p
42457 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
42458 if (strcmp (p, "loop") == 0)
42459 {
42460 cp_lexer_consume_token (parser->lexer);
42461 tree block = begin_omp_parallel ();
42462 tree clauses;
42463 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
42464 &clauses, if_p);
42465 protected_set_expr_location (stmt, pragma_tok->location);
42466 return finish_omp_construct (code, block, clauses);
42467 }
42468 }
42469
42470 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
42471
42472 tree block = begin_omp_parallel ();
42473 unsigned int save = cp_parser_begin_omp_structured_block (parser);
42474 cp_parser_statement (parser, NULL_TREE, false, if_p);
42475 cp_parser_end_omp_structured_block (parser, save);
42476 return finish_omp_construct (code, block, clauses);
42477 }
42478
42479 /* OpenACC 2.0:
42480 # pragma acc update oacc-update-clause[optseq] new-line
42481 */
42482
42483 #define OACC_UPDATE_CLAUSE_MASK \
42484 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
42486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
42487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
42489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
42490
42491 static tree
42492 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
42493 {
42494 tree stmt, clauses;
42495
42496 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
42497 "#pragma acc update", pragma_tok);
42498
42499 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
42500 {
42501 error_at (pragma_tok->location,
42502 "%<#pragma acc update%> must contain at least one "
42503 "%<device%> or %<host%> or %<self%> clause");
42504 return NULL_TREE;
42505 }
42506
42507 stmt = make_node (OACC_UPDATE);
42508 TREE_TYPE (stmt) = void_type_node;
42509 OACC_UPDATE_CLAUSES (stmt) = clauses;
42510 SET_EXPR_LOCATION (stmt, pragma_tok->location);
42511 add_stmt (stmt);
42512 return stmt;
42513 }
42514
42515 /* OpenACC 2.0:
42516 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
42517
42518 LOC is the location of the #pragma token.
42519 */
42520
42521 #define OACC_WAIT_CLAUSE_MASK \
42522 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
42523
42524 static tree
42525 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
42526 {
42527 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
42528 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42529
42530 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
42531 list = cp_parser_oacc_wait_list (parser, loc, list);
42532
42533 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
42534 "#pragma acc wait", pragma_tok);
42535
42536 stmt = c_finish_oacc_wait (loc, list, clauses);
42537 stmt = finish_expr_stmt (stmt);
42538
42539 return stmt;
42540 }
42541
42542 /* OpenMP 4.0:
42543 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
42544
42545 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
42546 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
42547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
42548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
42549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
42550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
42551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
42552
42553 static void
42554 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
42555 enum pragma_context context,
42556 bool variant_p)
42557 {
42558 bool first_p = parser->omp_declare_simd == NULL;
42559 cp_omp_declare_simd_data data;
42560 if (first_p)
42561 {
42562 data.error_seen = false;
42563 data.fndecl_seen = false;
42564 data.variant_p = variant_p;
42565 data.tokens = vNULL;
42566 data.clauses = NULL_TREE;
42567 /* It is safe to take the address of a local variable; it will only be
42568 used while this scope is live. */
42569 parser->omp_declare_simd = &data;
42570 }
42571 else if (parser->omp_declare_simd->variant_p != variant_p)
42572 {
42573 error_at (pragma_tok->location,
42574 "%<#pragma omp declare %s%> followed by "
42575 "%<#pragma omp declare %s%>",
42576 parser->omp_declare_simd->variant_p ? "variant" : "simd",
42577 parser->omp_declare_simd->variant_p ? "simd" : "variant");
42578 parser->omp_declare_simd->error_seen = true;
42579 }
42580
42581 /* Store away all pragma tokens. */
42582 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42583 cp_lexer_consume_token (parser->lexer);
42584 cp_parser_require_pragma_eol (parser, pragma_tok);
42585 struct cp_token_cache *cp
42586 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
42587 parser->omp_declare_simd->tokens.safe_push (cp);
42588
42589 if (first_p)
42590 {
42591 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
42592 cp_parser_pragma (parser, context, NULL);
42593 switch (context)
42594 {
42595 case pragma_external:
42596 cp_parser_declaration (parser, NULL_TREE);
42597 break;
42598 case pragma_member:
42599 cp_parser_member_declaration (parser);
42600 break;
42601 case pragma_objc_icode:
42602 cp_parser_block_declaration (parser, /*statement_p=*/false);
42603 break;
42604 default:
42605 cp_parser_declaration_statement (parser);
42606 break;
42607 }
42608 if (parser->omp_declare_simd
42609 && !parser->omp_declare_simd->error_seen
42610 && !parser->omp_declare_simd->fndecl_seen)
42611 error_at (pragma_tok->location,
42612 "%<#pragma omp declare %s%> not immediately followed by "
42613 "function declaration or definition",
42614 parser->omp_declare_simd->variant_p ? "variant" : "simd");
42615 data.tokens.release ();
42616 parser->omp_declare_simd = NULL;
42617 }
42618 }
42619
42620 static const char *const omp_construct_selectors[] = {
42621 "simd", "target", "teams", "parallel", "for", NULL };
42622 static const char *const omp_device_selectors[] = {
42623 "kind", "isa", "arch", NULL };
42624 static const char *const omp_implementation_selectors[] = {
42625 "vendor", "extension", "atomic_default_mem_order", "unified_address",
42626 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
42627 static const char *const omp_user_selectors[] = {
42628 "condition", NULL };
42629
42630 /* OpenMP 5.0:
42631
42632 trait-selector:
42633 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
42634
42635 trait-score:
42636 score(score-expression) */
42637
42638 static tree
42639 cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
42640 {
42641 tree ret = NULL_TREE;
42642 do
42643 {
42644 tree selector;
42645 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
42646 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42647 selector = cp_lexer_peek_token (parser->lexer)->u.value;
42648 else
42649 {
42650 cp_parser_error (parser, "expected trait selector name");
42651 return error_mark_node;
42652 }
42653
42654 tree properties = NULL_TREE;
42655 const char *const *selectors = NULL;
42656 bool allow_score = true;
42657 bool allow_user = false;
42658 int property_limit = 0;
42659 enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
42660 CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
42661 CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
42662 switch (IDENTIFIER_POINTER (set)[0])
42663 {
42664 case 'c': /* construct */
42665 selectors = omp_construct_selectors;
42666 allow_score = false;
42667 property_limit = 1;
42668 property_kind = CTX_PROPERTY_SIMD;
42669 break;
42670 case 'd': /* device */
42671 selectors = omp_device_selectors;
42672 allow_score = false;
42673 allow_user = true;
42674 property_limit = 3;
42675 property_kind = CTX_PROPERTY_NAME_LIST;
42676 break;
42677 case 'i': /* implementation */
42678 selectors = omp_implementation_selectors;
42679 allow_user = true;
42680 property_limit = 3;
42681 property_kind = CTX_PROPERTY_NAME_LIST;
42682 break;
42683 case 'u': /* user */
42684 selectors = omp_user_selectors;
42685 property_limit = 1;
42686 property_kind = CTX_PROPERTY_EXPR;
42687 break;
42688 default:
42689 gcc_unreachable ();
42690 }
42691 for (int i = 0; ; i++)
42692 {
42693 if (selectors[i] == NULL)
42694 {
42695 if (allow_user)
42696 {
42697 property_kind = CTX_PROPERTY_USER;
42698 break;
42699 }
42700 else
42701 {
42702 error ("selector %qs not allowed for context selector "
42703 "set %qs", IDENTIFIER_POINTER (selector),
42704 IDENTIFIER_POINTER (set));
42705 cp_lexer_consume_token (parser->lexer);
42706 return error_mark_node;
42707 }
42708 }
42709 if (i == property_limit)
42710 property_kind = CTX_PROPERTY_NONE;
42711 if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
42712 break;
42713 }
42714 if (property_kind == CTX_PROPERTY_NAME_LIST
42715 && IDENTIFIER_POINTER (set)[0] == 'i'
42716 && strcmp (IDENTIFIER_POINTER (selector),
42717 "atomic_default_mem_order") == 0)
42718 property_kind = CTX_PROPERTY_ID;
42719
42720 cp_lexer_consume_token (parser->lexer);
42721
42722 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
42723 {
42724 if (property_kind == CTX_PROPERTY_NONE)
42725 {
42726 error ("selector %qs does not accept any properties",
42727 IDENTIFIER_POINTER (selector));
42728 return error_mark_node;
42729 }
42730
42731 matching_parens parens;
42732 parens.consume_open (parser);
42733
42734 cp_token *token = cp_lexer_peek_token (parser->lexer);
42735 if (allow_score
42736 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
42737 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
42738 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
42739 {
42740 cp_lexer_save_tokens (parser->lexer);
42741 cp_lexer_consume_token (parser->lexer);
42742 cp_lexer_consume_token (parser->lexer);
42743 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
42744 true)
42745 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
42746 {
42747 cp_lexer_rollback_tokens (parser->lexer);
42748 cp_lexer_consume_token (parser->lexer);
42749
42750 matching_parens parens2;
42751 parens2.require_open (parser);
42752 tree score = cp_parser_constant_expression (parser);
42753 if (!parens2.require_close (parser))
42754 cp_parser_skip_to_closing_parenthesis (parser, true,
42755 false, true);
42756 cp_parser_require (parser, CPP_COLON, RT_COLON);
42757 if (score != error_mark_node)
42758 {
42759 score = fold_non_dependent_expr (score);
42760 if (value_dependent_expression_p (score))
42761 properties = tree_cons (get_identifier (" score"),
42762 score, properties);
42763 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
42764 || TREE_CODE (score) != INTEGER_CST)
42765 error_at (token->location, "score argument must be "
42766 "constant integer expression");
42767 else if (tree_int_cst_sgn (score) < 0)
42768 error_at (token->location, "score argument must be "
42769 "non-negative");
42770 else
42771 properties = tree_cons (get_identifier (" score"),
42772 score, properties);
42773 }
42774 }
42775 else
42776 cp_lexer_rollback_tokens (parser->lexer);
42777
42778 token = cp_lexer_peek_token (parser->lexer);
42779 }
42780
42781 switch (property_kind)
42782 {
42783 tree t;
42784 case CTX_PROPERTY_USER:
42785 do
42786 {
42787 t = cp_parser_constant_expression (parser);
42788 if (t != error_mark_node)
42789 {
42790 t = fold_non_dependent_expr (t);
42791 if (TREE_CODE (t) == STRING_CST)
42792 properties = tree_cons (NULL_TREE, t, properties);
42793 else if (!value_dependent_expression_p (t)
42794 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
42795 || !tree_fits_shwi_p (t)))
42796 error_at (token->location, "property must be "
42797 "constant integer expression or string "
42798 "literal");
42799 else
42800 properties = tree_cons (NULL_TREE, t, properties);
42801 }
42802 else
42803 return error_mark_node;
42804
42805 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42806 cp_lexer_consume_token (parser->lexer);
42807 else
42808 break;
42809 }
42810 while (1);
42811 break;
42812 case CTX_PROPERTY_ID:
42813 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
42814 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42815 {
42816 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
42817 cp_lexer_consume_token (parser->lexer);
42818 properties = tree_cons (prop, NULL_TREE, properties);
42819 }
42820 else
42821 {
42822 cp_parser_error (parser, "expected identifier");
42823 return error_mark_node;
42824 }
42825 break;
42826 case CTX_PROPERTY_NAME_LIST:
42827 do
42828 {
42829 tree prop = NULL_TREE, value = NULL_TREE;
42830 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
42831 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42832 {
42833 prop = cp_lexer_peek_token (parser->lexer)->u.value;
42834 cp_lexer_consume_token (parser->lexer);
42835 }
42836 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
42837 value = cp_parser_string_literal (parser, false, false);
42838 else
42839 {
42840 cp_parser_error (parser, "expected identifier or "
42841 "string literal");
42842 return error_mark_node;
42843 }
42844
42845 properties = tree_cons (prop, value, properties);
42846
42847 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42848 cp_lexer_consume_token (parser->lexer);
42849 else
42850 break;
42851 }
42852 while (1);
42853 break;
42854 case CTX_PROPERTY_EXPR:
42855 t = cp_parser_constant_expression (parser);
42856 if (t != error_mark_node)
42857 {
42858 t = fold_non_dependent_expr (t);
42859 if (!value_dependent_expression_p (t)
42860 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
42861 || !tree_fits_shwi_p (t)))
42862 error_at (token->location, "property must be "
42863 "constant integer expression");
42864 else
42865 properties = tree_cons (NULL_TREE, t, properties);
42866 }
42867 else
42868 return error_mark_node;
42869 break;
42870 case CTX_PROPERTY_SIMD:
42871 if (!has_parms_p)
42872 {
42873 error_at (token->location, "properties for %<simd%> "
42874 "selector may not be specified in "
42875 "%<metadirective%>");
42876 return error_mark_node;
42877 }
42878 properties
42879 = cp_parser_omp_all_clauses (parser,
42880 OMP_DECLARE_SIMD_CLAUSE_MASK,
42881 "simd", NULL, true, 2);
42882 break;
42883 default:
42884 gcc_unreachable ();
42885 }
42886
42887 if (!parens.require_close (parser))
42888 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
42889
42890 properties = nreverse (properties);
42891 }
42892 else if (property_kind == CTX_PROPERTY_NAME_LIST
42893 || property_kind == CTX_PROPERTY_ID
42894 || property_kind == CTX_PROPERTY_EXPR)
42895 {
42896 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
42897 return error_mark_node;
42898 }
42899
42900 ret = tree_cons (selector, properties, ret);
42901
42902 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42903 cp_lexer_consume_token (parser->lexer);
42904 else
42905 break;
42906 }
42907 while (1);
42908
42909 return nreverse (ret);
42910 }
42911
42912 /* OpenMP 5.0:
42913
42914 trait-set-selector[,trait-set-selector[,...]]
42915
42916 trait-set-selector:
42917 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
42918
42919 trait-set-selector-name:
42920 constructor
42921 device
42922 implementation
42923 user */
42924
42925 static tree
42926 cp_parser_omp_context_selector_specification (cp_parser *parser,
42927 bool has_parms_p)
42928 {
42929 tree ret = NULL_TREE;
42930 do
42931 {
42932 const char *setp = "";
42933 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42934 setp
42935 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
42936 switch (setp[0])
42937 {
42938 case 'c':
42939 if (strcmp (setp, "construct") == 0)
42940 setp = NULL;
42941 break;
42942 case 'd':
42943 if (strcmp (setp, "device") == 0)
42944 setp = NULL;
42945 break;
42946 case 'i':
42947 if (strcmp (setp, "implementation") == 0)
42948 setp = NULL;
42949 break;
42950 case 'u':
42951 if (strcmp (setp, "user") == 0)
42952 setp = NULL;
42953 break;
42954 default:
42955 break;
42956 }
42957 if (setp)
42958 {
42959 cp_parser_error (parser, "expected %<construct%>, %<device%>, "
42960 "%<implementation%> or %<user%>");
42961 return error_mark_node;
42962 }
42963
42964 tree set = cp_lexer_peek_token (parser->lexer)->u.value;
42965 cp_lexer_consume_token (parser->lexer);
42966
42967 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42968 return error_mark_node;
42969
42970 matching_braces braces;
42971 if (!braces.require_open (parser))
42972 return error_mark_node;
42973
42974 tree selectors
42975 = cp_parser_omp_context_selector (parser, set, has_parms_p);
42976 if (selectors == error_mark_node)
42977 {
42978 cp_parser_skip_to_closing_brace (parser);
42979 ret = error_mark_node;
42980 }
42981 else if (ret != error_mark_node)
42982 ret = tree_cons (set, selectors, ret);
42983
42984 braces.require_close (parser);
42985
42986 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42987 cp_lexer_consume_token (parser->lexer);
42988 else
42989 break;
42990 }
42991 while (1);
42992
42993 if (ret == error_mark_node)
42994 return ret;
42995 return nreverse (ret);
42996 }
42997
42998 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
42999 that into "omp declare variant base" attribute. */
43000
43001 static tree
43002 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
43003 tree attrs)
43004 {
43005 matching_parens parens;
43006 if (!parens.require_open (parser))
43007 {
43008 fail:
43009 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43010 return attrs;
43011 }
43012
43013 bool template_p;
43014 cp_id_kind idk = CP_ID_KIND_NONE;
43015 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
43016 cp_expr varid
43017 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
43018 /*check_dependency_p=*/true,
43019 /*template_p=*/&template_p,
43020 /*declarator_p=*/false,
43021 /*optional_p=*/false);
43022 parens.require_close (parser);
43023
43024 tree variant;
43025 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
43026 || TREE_CODE (varid) == TYPE_DECL
43027 || varid == error_mark_node)
43028 variant = varid;
43029 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
43030 variant = NULL_TREE;
43031 else
43032 {
43033 tree ambiguous_decls;
43034 variant = cp_parser_lookup_name (parser, varid, none_type,
43035 template_p, /*is_namespace=*/false,
43036 /*check_dependency=*/true,
43037 &ambiguous_decls,
43038 varid.get_location ());
43039 if (ambiguous_decls)
43040 variant = NULL_TREE;
43041 }
43042 if (variant == NULL_TREE)
43043 variant = error_mark_node;
43044 else if (TREE_CODE (variant) != SCOPE_REF)
43045 {
43046 const char *error_msg;
43047 variant
43048 = finish_id_expression (varid, variant, parser->scope,
43049 &idk, false, true,
43050 &parser->non_integral_constant_expression_p,
43051 template_p, true, false, false, &error_msg,
43052 varid.get_location ());
43053 if (error_msg)
43054 cp_parser_error (parser, error_msg);
43055 }
43056 location_t caret_loc = get_pure_location (varid.get_location ());
43057 location_t start_loc = get_start (varid_token->location);
43058 location_t finish_loc = get_finish (varid.get_location ());
43059 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
43060
43061 const char *clause = "";
43062 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
43063 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43064 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
43065 if (strcmp (clause, "match"))
43066 {
43067 cp_parser_error (parser, "expected %<match%>");
43068 goto fail;
43069 }
43070
43071 cp_lexer_consume_token (parser->lexer);
43072
43073 if (!parens.require_open (parser))
43074 goto fail;
43075
43076 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
43077 if (ctx == error_mark_node)
43078 goto fail;
43079 ctx = c_omp_check_context_selector (match_loc, ctx);
43080 if (ctx != error_mark_node && variant != error_mark_node)
43081 {
43082 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
43083 match_loc);
43084 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
43085 loc_node = tree_cons (match_loc_node,
43086 build_int_cst (integer_type_node, idk),
43087 build_tree_list (loc_node, integer_zero_node));
43088 attrs = tree_cons (get_identifier ("omp declare variant base"),
43089 tree_cons (variant, ctx, loc_node), attrs);
43090 if (processing_template_decl)
43091 ATTR_IS_DEPENDENT (attrs) = 1;
43092 }
43093
43094 parens.require_close (parser);
43095 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43096 return attrs;
43097 }
43098
43099
43100 /* Finalize #pragma omp declare simd clauses after direct declarator has
43101 been parsed, and put that into "omp declare simd" attribute. */
43102
43103 static tree
43104 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
43105 {
43106 struct cp_token_cache *ce;
43107 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
43108 int i;
43109
43110 if (!data->error_seen && data->fndecl_seen)
43111 {
43112 error ("%<#pragma omp declare %s%> not immediately followed by "
43113 "a single function declaration or definition",
43114 data->variant_p ? "variant" : "simd");
43115 data->error_seen = true;
43116 }
43117 if (data->error_seen)
43118 return attrs;
43119
43120 FOR_EACH_VEC_ELT (data->tokens, i, ce)
43121 {
43122 tree c, cl;
43123
43124 cp_parser_push_lexer_for_tokens (parser, ce);
43125 parser->lexer->in_pragma = true;
43126 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
43127 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
43128 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43129 const char *kind = IDENTIFIER_POINTER (id);
43130 cp_lexer_consume_token (parser->lexer);
43131 if (strcmp (kind, "simd") == 0)
43132 {
43133 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
43134 "#pragma omp declare simd",
43135 pragma_tok);
43136 if (cl)
43137 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
43138 c = build_tree_list (get_identifier ("omp declare simd"), cl);
43139 TREE_CHAIN (c) = attrs;
43140 if (processing_template_decl)
43141 ATTR_IS_DEPENDENT (c) = 1;
43142 attrs = c;
43143 }
43144 else
43145 {
43146 gcc_assert (strcmp (kind, "variant") == 0);
43147 attrs = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
43148 }
43149 cp_parser_pop_lexer (parser);
43150 }
43151
43152 data->fndecl_seen = true;
43153 return attrs;
43154 }
43155
43156
43157 /* OpenMP 4.0:
43158 # pragma omp declare target new-line
43159 declarations and definitions
43160 # pragma omp end declare target new-line
43161
43162 OpenMP 4.5:
43163 # pragma omp declare target ( extended-list ) new-line
43164
43165 # pragma omp declare target declare-target-clauses[seq] new-line */
43166
43167 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
43168 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
43169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
43170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
43171
43172 static void
43173 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
43174 {
43175 tree clauses = NULL_TREE;
43176 int device_type = 0;
43177 bool only_device_type = true;
43178 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43179 clauses
43180 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
43181 "#pragma omp declare target", pragma_tok);
43182 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
43183 {
43184 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
43185 clauses);
43186 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
43187 cp_parser_require_pragma_eol (parser, pragma_tok);
43188 }
43189 else
43190 {
43191 cp_parser_require_pragma_eol (parser, pragma_tok);
43192 scope_chain->omp_declare_target_attribute++;
43193 return;
43194 }
43195 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
43196 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
43197 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
43198 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
43199 {
43200 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
43201 continue;
43202 tree t = OMP_CLAUSE_DECL (c), id;
43203 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
43204 tree at2 = lookup_attribute ("omp declare target link",
43205 DECL_ATTRIBUTES (t));
43206 only_device_type = false;
43207 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
43208 {
43209 id = get_identifier ("omp declare target link");
43210 std::swap (at1, at2);
43211 }
43212 else
43213 id = get_identifier ("omp declare target");
43214 if (at2)
43215 {
43216 error_at (OMP_CLAUSE_LOCATION (c),
43217 "%qD specified both in declare target %<link%> and %<to%>"
43218 " clauses", t);
43219 continue;
43220 }
43221 if (!at1)
43222 {
43223 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
43224 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
43225 continue;
43226
43227 symtab_node *node = symtab_node::get (t);
43228 if (node != NULL)
43229 {
43230 node->offloadable = 1;
43231 if (ENABLE_OFFLOADING)
43232 {
43233 g->have_offload = true;
43234 if (is_a <varpool_node *> (node))
43235 vec_safe_push (offload_vars, t);
43236 }
43237 }
43238 }
43239 if (TREE_CODE (t) != FUNCTION_DECL)
43240 continue;
43241 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
43242 {
43243 tree at3 = lookup_attribute ("omp declare target host",
43244 DECL_ATTRIBUTES (t));
43245 if (at3 == NULL_TREE)
43246 {
43247 id = get_identifier ("omp declare target host");
43248 DECL_ATTRIBUTES (t)
43249 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
43250 }
43251 }
43252 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
43253 {
43254 tree at3 = lookup_attribute ("omp declare target nohost",
43255 DECL_ATTRIBUTES (t));
43256 if (at3 == NULL_TREE)
43257 {
43258 id = get_identifier ("omp declare target nohost");
43259 DECL_ATTRIBUTES (t)
43260 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
43261 }
43262 }
43263 }
43264 if (device_type && only_device_type)
43265 warning_at (OMP_CLAUSE_LOCATION (clauses), 0,
43266 "directive with only %<device_type%> clauses ignored");
43267 }
43268
43269 static void
43270 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
43271 {
43272 const char *p = "";
43273 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43274 {
43275 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43276 p = IDENTIFIER_POINTER (id);
43277 }
43278 if (strcmp (p, "declare") == 0)
43279 {
43280 cp_lexer_consume_token (parser->lexer);
43281 p = "";
43282 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43283 {
43284 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43285 p = IDENTIFIER_POINTER (id);
43286 }
43287 if (strcmp (p, "target") == 0)
43288 cp_lexer_consume_token (parser->lexer);
43289 else
43290 {
43291 cp_parser_error (parser, "expected %<target%>");
43292 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43293 return;
43294 }
43295 }
43296 else
43297 {
43298 cp_parser_error (parser, "expected %<declare%>");
43299 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43300 return;
43301 }
43302 cp_parser_require_pragma_eol (parser, pragma_tok);
43303 if (!scope_chain->omp_declare_target_attribute)
43304 error_at (pragma_tok->location,
43305 "%<#pragma omp end declare target%> without corresponding "
43306 "%<#pragma omp declare target%>");
43307 else
43308 scope_chain->omp_declare_target_attribute--;
43309 }
43310
43311 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
43312 expression and optional initializer clause of
43313 #pragma omp declare reduction. We store the expression(s) as
43314 either 3, 6 or 7 special statements inside of the artificial function's
43315 body. The first two statements are DECL_EXPRs for the artificial
43316 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
43317 expression that uses those variables.
43318 If there was any INITIALIZER clause, this is followed by further statements,
43319 the fourth and fifth statements are DECL_EXPRs for the artificial
43320 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
43321 constructor variant (first token after open paren is not omp_priv),
43322 then the sixth statement is a statement with the function call expression
43323 that uses the OMP_PRIV and optionally OMP_ORIG variable.
43324 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
43325 to initialize the OMP_PRIV artificial variable and there is seventh
43326 statement, a DECL_EXPR of the OMP_PRIV statement again. */
43327
43328 static bool
43329 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
43330 {
43331 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
43332 gcc_assert (TYPE_REF_P (type));
43333 type = TREE_TYPE (type);
43334 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
43335 DECL_ARTIFICIAL (omp_out) = 1;
43336 pushdecl (omp_out);
43337 add_decl_expr (omp_out);
43338 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
43339 DECL_ARTIFICIAL (omp_in) = 1;
43340 pushdecl (omp_in);
43341 add_decl_expr (omp_in);
43342 tree combiner;
43343 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
43344
43345 keep_next_level (true);
43346 tree block = begin_omp_structured_block ();
43347 combiner = cp_parser_expression (parser);
43348 finish_expr_stmt (combiner);
43349 block = finish_omp_structured_block (block);
43350 if (processing_template_decl)
43351 block = build_stmt (input_location, EXPR_STMT, block);
43352 add_stmt (block);
43353
43354 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
43355 return false;
43356
43357 const char *p = "";
43358 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43359 {
43360 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43361 p = IDENTIFIER_POINTER (id);
43362 }
43363
43364 if (strcmp (p, "initializer") == 0)
43365 {
43366 cp_lexer_consume_token (parser->lexer);
43367 matching_parens parens;
43368 if (!parens.require_open (parser))
43369 return false;
43370
43371 p = "";
43372 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43373 {
43374 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43375 p = IDENTIFIER_POINTER (id);
43376 }
43377
43378 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
43379 DECL_ARTIFICIAL (omp_priv) = 1;
43380 pushdecl (omp_priv);
43381 add_decl_expr (omp_priv);
43382 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
43383 DECL_ARTIFICIAL (omp_orig) = 1;
43384 pushdecl (omp_orig);
43385 add_decl_expr (omp_orig);
43386
43387 keep_next_level (true);
43388 block = begin_omp_structured_block ();
43389
43390 bool ctor = false;
43391 if (strcmp (p, "omp_priv") == 0)
43392 {
43393 bool is_direct_init, is_non_constant_init;
43394 ctor = true;
43395 cp_lexer_consume_token (parser->lexer);
43396 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
43397 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
43398 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
43399 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
43400 == CPP_CLOSE_PAREN
43401 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
43402 == CPP_CLOSE_PAREN))
43403 {
43404 finish_omp_structured_block (block);
43405 error ("invalid initializer clause");
43406 return false;
43407 }
43408 initializer = cp_parser_initializer (parser, &is_direct_init,
43409 &is_non_constant_init);
43410 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
43411 NULL_TREE, LOOKUP_ONLYCONVERTING);
43412 }
43413 else
43414 {
43415 cp_parser_parse_tentatively (parser);
43416 /* Don't create location wrapper nodes here. */
43417 auto_suppress_location_wrappers sentinel;
43418 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
43419 /*check_dependency_p=*/true,
43420 /*template_p=*/NULL,
43421 /*declarator_p=*/false,
43422 /*optional_p=*/false);
43423 vec<tree, va_gc> *args;
43424 if (fn_name == error_mark_node
43425 || cp_parser_error_occurred (parser)
43426 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
43427 || ((args = cp_parser_parenthesized_expression_list
43428 (parser, non_attr, /*cast_p=*/false,
43429 /*allow_expansion_p=*/true,
43430 /*non_constant_p=*/NULL)),
43431 cp_parser_error_occurred (parser)))
43432 {
43433 finish_omp_structured_block (block);
43434 cp_parser_abort_tentative_parse (parser);
43435 cp_parser_error (parser, "expected id-expression (arguments)");
43436 return false;
43437 }
43438 unsigned int i;
43439 tree arg;
43440 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
43441 if (arg == omp_priv
43442 || (TREE_CODE (arg) == ADDR_EXPR
43443 && TREE_OPERAND (arg, 0) == omp_priv))
43444 break;
43445 cp_parser_abort_tentative_parse (parser);
43446 if (arg == NULL_TREE)
43447 error ("one of the initializer call arguments should be %<omp_priv%>"
43448 " or %<&omp_priv%>");
43449 initializer = cp_parser_postfix_expression (parser, false, false, false,
43450 false, NULL);
43451 finish_expr_stmt (initializer);
43452 }
43453
43454 block = finish_omp_structured_block (block);
43455 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
43456 if (processing_template_decl)
43457 block = build_stmt (input_location, EXPR_STMT, block);
43458 add_stmt (block);
43459
43460 if (ctor)
43461 add_decl_expr (omp_orig);
43462
43463 if (!parens.require_close (parser))
43464 return false;
43465 }
43466
43467 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
43468 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
43469 UNKNOWN_LOCATION);
43470
43471 return true;
43472 }
43473
43474 /* OpenMP 4.0
43475 #pragma omp declare reduction (reduction-id : typename-list : expression) \
43476 initializer-clause[opt] new-line
43477
43478 initializer-clause:
43479 initializer (omp_priv initializer)
43480 initializer (function-name (argument-list)) */
43481
43482 static void
43483 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
43484 enum pragma_context)
43485 {
43486 auto_vec<tree> types;
43487 enum tree_code reduc_code = ERROR_MARK;
43488 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
43489 unsigned int i;
43490 cp_token *first_token;
43491 cp_token_cache *cp;
43492 int errs;
43493 void *p;
43494
43495 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
43496 p = obstack_alloc (&declarator_obstack, 0);
43497
43498 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
43499 goto fail;
43500
43501 switch (cp_lexer_peek_token (parser->lexer)->type)
43502 {
43503 case CPP_PLUS:
43504 reduc_code = PLUS_EXPR;
43505 break;
43506 case CPP_MULT:
43507 reduc_code = MULT_EXPR;
43508 break;
43509 case CPP_MINUS:
43510 reduc_code = MINUS_EXPR;
43511 break;
43512 case CPP_AND:
43513 reduc_code = BIT_AND_EXPR;
43514 break;
43515 case CPP_XOR:
43516 reduc_code = BIT_XOR_EXPR;
43517 break;
43518 case CPP_OR:
43519 reduc_code = BIT_IOR_EXPR;
43520 break;
43521 case CPP_AND_AND:
43522 reduc_code = TRUTH_ANDIF_EXPR;
43523 break;
43524 case CPP_OR_OR:
43525 reduc_code = TRUTH_ORIF_EXPR;
43526 break;
43527 case CPP_NAME:
43528 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
43529 break;
43530 default:
43531 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
43532 "%<|%>, %<&&%>, %<||%> or identifier");
43533 goto fail;
43534 }
43535
43536 if (reduc_code != ERROR_MARK)
43537 cp_lexer_consume_token (parser->lexer);
43538
43539 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
43540 if (reduc_id == error_mark_node)
43541 goto fail;
43542
43543 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
43544 goto fail;
43545
43546 /* Types may not be defined in declare reduction type list. */
43547 const char *saved_message;
43548 saved_message = parser->type_definition_forbidden_message;
43549 parser->type_definition_forbidden_message
43550 = G_("types may not be defined in declare reduction type list");
43551 bool saved_colon_corrects_to_scope_p;
43552 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
43553 parser->colon_corrects_to_scope_p = false;
43554 bool saved_colon_doesnt_start_class_def_p;
43555 saved_colon_doesnt_start_class_def_p
43556 = parser->colon_doesnt_start_class_def_p;
43557 parser->colon_doesnt_start_class_def_p = true;
43558
43559 while (true)
43560 {
43561 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43562 type = cp_parser_type_id (parser);
43563 if (type == error_mark_node)
43564 ;
43565 else if (ARITHMETIC_TYPE_P (type)
43566 && (orig_reduc_id == NULL_TREE
43567 || (TREE_CODE (type) != COMPLEX_TYPE
43568 && (id_equal (orig_reduc_id, "min")
43569 || id_equal (orig_reduc_id, "max")))))
43570 error_at (loc, "predeclared arithmetic type %qT in "
43571 "%<#pragma omp declare reduction%>", type);
43572 else if (FUNC_OR_METHOD_TYPE_P (type)
43573 || TREE_CODE (type) == ARRAY_TYPE)
43574 error_at (loc, "function or array type %qT in "
43575 "%<#pragma omp declare reduction%>", type);
43576 else if (TYPE_REF_P (type))
43577 error_at (loc, "reference type %qT in "
43578 "%<#pragma omp declare reduction%>", type);
43579 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
43580 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
43581 "type %qT in %<#pragma omp declare reduction%>", type);
43582 else
43583 types.safe_push (type);
43584
43585 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43586 cp_lexer_consume_token (parser->lexer);
43587 else
43588 break;
43589 }
43590
43591 /* Restore the saved message. */
43592 parser->type_definition_forbidden_message = saved_message;
43593 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
43594 parser->colon_doesnt_start_class_def_p
43595 = saved_colon_doesnt_start_class_def_p;
43596
43597 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
43598 || types.is_empty ())
43599 {
43600 fail:
43601 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43602 goto done;
43603 }
43604
43605 first_token = cp_lexer_peek_token (parser->lexer);
43606 cp = NULL;
43607 errs = errorcount;
43608 FOR_EACH_VEC_ELT (types, i, type)
43609 {
43610 tree fntype
43611 = build_function_type_list (void_type_node,
43612 cp_build_reference_type (type, false),
43613 NULL_TREE);
43614 tree this_reduc_id = reduc_id;
43615 if (!dependent_type_p (type))
43616 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
43617 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
43618 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
43619 DECL_ARTIFICIAL (fndecl) = 1;
43620 DECL_EXTERNAL (fndecl) = 1;
43621 DECL_DECLARED_INLINE_P (fndecl) = 1;
43622 DECL_IGNORED_P (fndecl) = 1;
43623 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
43624 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
43625 DECL_ATTRIBUTES (fndecl)
43626 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
43627 DECL_ATTRIBUTES (fndecl));
43628 bool block_scope = false;
43629 if (current_function_decl)
43630 {
43631 block_scope = true;
43632 DECL_CONTEXT (fndecl) = current_function_decl;
43633 DECL_LOCAL_DECL_P (fndecl) = true;
43634 }
43635
43636 if (processing_template_decl)
43637 fndecl = push_template_decl (fndecl);
43638
43639 if (block_scope)
43640 {
43641 if (!processing_template_decl)
43642 pushdecl (fndecl);
43643 }
43644 else if (current_class_type)
43645 {
43646 if (cp == NULL)
43647 {
43648 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
43649 cp_lexer_consume_token (parser->lexer);
43650 cp = cp_token_cache_new (first_token,
43651 cp_lexer_peek_nth_token (parser->lexer,
43652 2));
43653 }
43654 DECL_STATIC_FUNCTION_P (fndecl) = 1;
43655 finish_member_declaration (fndecl);
43656 DECL_PENDING_INLINE_INFO (fndecl) = cp;
43657 DECL_PENDING_INLINE_P (fndecl) = 1;
43658 vec_safe_push (unparsed_funs_with_definitions, fndecl);
43659 continue;
43660 }
43661 else
43662 {
43663 DECL_CONTEXT (fndecl) = current_namespace;
43664 tree d = pushdecl (fndecl);
43665 /* We should never meet a matched duplicate decl. */
43666 gcc_checking_assert (d == error_mark_node || d == fndecl);
43667 }
43668
43669 tree block = NULL_TREE;
43670 if (!block_scope)
43671 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
43672 else
43673 block = begin_omp_structured_block ();
43674 if (cp)
43675 {
43676 cp_parser_push_lexer_for_tokens (parser, cp);
43677 parser->lexer->in_pragma = true;
43678 }
43679
43680 bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
43681
43682 if (cp)
43683 cp_parser_pop_lexer (parser);
43684 if (!block_scope)
43685 finish_function (/*inline_p=*/false);
43686 else
43687 {
43688 DECL_CONTEXT (fndecl) = current_function_decl;
43689 if (DECL_TEMPLATE_INFO (fndecl))
43690 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
43691 }
43692 if (!ok)
43693 goto fail;
43694
43695 if (block_scope)
43696 {
43697 block = finish_omp_structured_block (block);
43698 if (TREE_CODE (block) == BIND_EXPR)
43699 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
43700 else if (TREE_CODE (block) == STATEMENT_LIST)
43701 DECL_SAVED_TREE (fndecl) = block;
43702 if (processing_template_decl)
43703 add_decl_expr (fndecl);
43704 }
43705
43706 cp_check_omp_declare_reduction (fndecl);
43707 if (cp == NULL && types.length () > 1)
43708 cp = cp_token_cache_new (first_token,
43709 cp_lexer_peek_nth_token (parser->lexer, 2));
43710 if (errs != errorcount)
43711 break;
43712 }
43713
43714 cp_parser_require_pragma_eol (parser, pragma_tok);
43715
43716 done:
43717 /* Free any declarators allocated. */
43718 obstack_free (&declarator_obstack, p);
43719 }
43720
43721 /* OpenMP 4.0
43722 #pragma omp declare simd declare-simd-clauses[optseq] new-line
43723 #pragma omp declare reduction (reduction-id : typename-list : expression) \
43724 initializer-clause[opt] new-line
43725 #pragma omp declare target new-line
43726
43727 OpenMP 5.0
43728 #pragma omp declare variant (identifier) match (context-selector) */
43729
43730 static bool
43731 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
43732 enum pragma_context context)
43733 {
43734 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43735 {
43736 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43737 const char *p = IDENTIFIER_POINTER (id);
43738
43739 if (strcmp (p, "simd") == 0)
43740 {
43741 cp_lexer_consume_token (parser->lexer);
43742 cp_parser_omp_declare_simd (parser, pragma_tok,
43743 context, false);
43744 return true;
43745 }
43746 if (flag_openmp && strcmp (p, "variant") == 0)
43747 {
43748 cp_lexer_consume_token (parser->lexer);
43749 cp_parser_omp_declare_simd (parser, pragma_tok,
43750 context, true);
43751 return true;
43752 }
43753 cp_ensure_no_omp_declare_simd (parser);
43754 if (strcmp (p, "reduction") == 0)
43755 {
43756 cp_lexer_consume_token (parser->lexer);
43757 cp_parser_omp_declare_reduction (parser, pragma_tok,
43758 context);
43759 return false;
43760 }
43761 if (!flag_openmp) /* flag_openmp_simd */
43762 {
43763 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43764 return false;
43765 }
43766 if (strcmp (p, "target") == 0)
43767 {
43768 cp_lexer_consume_token (parser->lexer);
43769 cp_parser_omp_declare_target (parser, pragma_tok);
43770 return false;
43771 }
43772 }
43773 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
43774 "%<target%> or %<variant%>");
43775 cp_parser_require_pragma_eol (parser, pragma_tok);
43776 return false;
43777 }
43778
43779 /* OpenMP 5.0
43780 #pragma omp requires clauses[optseq] new-line */
43781
43782 static bool
43783 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
43784 {
43785 bool first = true;
43786 enum omp_requires new_req = (enum omp_requires) 0;
43787
43788 location_t loc = pragma_tok->location;
43789 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
43790 {
43791 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43792 cp_lexer_consume_token (parser->lexer);
43793
43794 first = false;
43795
43796 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43797 {
43798 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43799 const char *p = IDENTIFIER_POINTER (id);
43800 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
43801 enum omp_requires this_req = (enum omp_requires) 0;
43802
43803 if (!strcmp (p, "unified_address"))
43804 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
43805 else if (!strcmp (p, "unified_shared_memory"))
43806 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
43807 else if (!strcmp (p, "dynamic_allocators"))
43808 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
43809 else if (!strcmp (p, "reverse_offload"))
43810 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
43811 else if (!strcmp (p, "atomic_default_mem_order"))
43812 {
43813 cp_lexer_consume_token (parser->lexer);
43814
43815 matching_parens parens;
43816 if (parens.require_open (parser))
43817 {
43818 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43819 {
43820 id = cp_lexer_peek_token (parser->lexer)->u.value;
43821 p = IDENTIFIER_POINTER (id);
43822
43823 if (!strcmp (p, "seq_cst"))
43824 this_req
43825 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
43826 else if (!strcmp (p, "relaxed"))
43827 this_req
43828 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
43829 else if (!strcmp (p, "acq_rel"))
43830 this_req
43831 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
43832 }
43833 if (this_req == 0)
43834 {
43835 error_at (cp_lexer_peek_token (parser->lexer)->location,
43836 "expected %<seq_cst%>, %<relaxed%> or "
43837 "%<acq_rel%>");
43838 if (cp_lexer_nth_token_is (parser->lexer, 2,
43839 CPP_CLOSE_PAREN))
43840 cp_lexer_consume_token (parser->lexer);
43841 }
43842 else
43843 cp_lexer_consume_token (parser->lexer);
43844
43845 if (!parens.require_close (parser))
43846 cp_parser_skip_to_closing_parenthesis (parser,
43847 /*recovering=*/true,
43848 /*or_comma=*/false,
43849 /*consume_paren=*/
43850 true);
43851
43852 if (this_req == 0)
43853 {
43854 cp_parser_require_pragma_eol (parser, pragma_tok);
43855 return false;
43856 }
43857 }
43858 p = NULL;
43859 }
43860 else
43861 {
43862 error_at (cloc, "expected %<unified_address%>, "
43863 "%<unified_shared_memory%>, "
43864 "%<dynamic_allocators%>, "
43865 "%<reverse_offload%> "
43866 "or %<atomic_default_mem_order%> clause");
43867 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43868 return false;
43869 }
43870 if (p)
43871 sorry_at (cloc, "%qs clause on %<requires%> directive not "
43872 "supported yet", p);
43873 if (p)
43874 cp_lexer_consume_token (parser->lexer);
43875 if (this_req)
43876 {
43877 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
43878 {
43879 if ((this_req & new_req) != 0)
43880 error_at (cloc, "too many %qs clauses", p);
43881 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
43882 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
43883 error_at (cloc, "%qs clause used lexically after first "
43884 "target construct or offloading API", p);
43885 }
43886 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
43887 {
43888 error_at (cloc, "too many %qs clauses",
43889 "atomic_default_mem_order");
43890 this_req = (enum omp_requires) 0;
43891 }
43892 else if ((omp_requires_mask
43893 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
43894 {
43895 error_at (cloc, "more than one %<atomic_default_mem_order%>"
43896 " clause in a single compilation unit");
43897 this_req
43898 = (enum omp_requires)
43899 (omp_requires_mask
43900 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
43901 }
43902 else if ((omp_requires_mask
43903 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
43904 error_at (cloc, "%<atomic_default_mem_order%> clause used "
43905 "lexically after first %<atomic%> construct "
43906 "without memory order clause");
43907 new_req = (enum omp_requires) (new_req | this_req);
43908 omp_requires_mask
43909 = (enum omp_requires) (omp_requires_mask | this_req);
43910 continue;
43911 }
43912 }
43913 break;
43914 }
43915 cp_parser_require_pragma_eol (parser, pragma_tok);
43916
43917 if (new_req == 0)
43918 error_at (loc, "%<pragma omp requires%> requires at least one clause");
43919 return false;
43920 }
43921
43922
43923 /* OpenMP 4.5:
43924 #pragma omp taskloop taskloop-clause[optseq] new-line
43925 for-loop
43926
43927 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
43928 for-loop */
43929
43930 #define OMP_TASKLOOP_CLAUSE_MASK \
43931 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
43932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
43936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
43937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
43938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
43940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
43942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
43943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
43944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
43945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
43948
43949 static tree
43950 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
43951 char *p_name, omp_clause_mask mask, tree *cclauses,
43952 bool *if_p)
43953 {
43954 tree clauses, sb, ret;
43955 unsigned int save;
43956 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43957
43958 strcat (p_name, " taskloop");
43959 mask |= OMP_TASKLOOP_CLAUSE_MASK;
43960 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
43961 clause. */
43962 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
43963 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
43964
43965 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43966 {
43967 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43968 const char *p = IDENTIFIER_POINTER (id);
43969
43970 if (strcmp (p, "simd") == 0)
43971 {
43972 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43973 if (cclauses == NULL)
43974 cclauses = cclauses_buf;
43975
43976 cp_lexer_consume_token (parser->lexer);
43977 if (!flag_openmp) /* flag_openmp_simd */
43978 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43979 cclauses, if_p);
43980 sb = begin_omp_structured_block ();
43981 save = cp_parser_begin_omp_structured_block (parser);
43982 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43983 cclauses, if_p);
43984 cp_parser_end_omp_structured_block (parser, save);
43985 tree body = finish_omp_structured_block (sb);
43986 if (ret == NULL)
43987 return ret;
43988 ret = make_node (OMP_TASKLOOP);
43989 TREE_TYPE (ret) = void_type_node;
43990 OMP_FOR_BODY (ret) = body;
43991 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
43992 SET_EXPR_LOCATION (ret, loc);
43993 add_stmt (ret);
43994 return ret;
43995 }
43996 }
43997 if (!flag_openmp) /* flag_openmp_simd */
43998 {
43999 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44000 return NULL_TREE;
44001 }
44002
44003 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44004 cclauses == NULL);
44005 if (cclauses)
44006 {
44007 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
44008 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
44009 }
44010
44011 keep_next_level (true);
44012 sb = begin_omp_structured_block ();
44013 save = cp_parser_begin_omp_structured_block (parser);
44014
44015 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
44016 if_p);
44017
44018 cp_parser_end_omp_structured_block (parser, save);
44019 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
44020
44021 return ret;
44022 }
44023
44024
44025 /* OpenACC 2.0:
44026 # pragma acc routine oacc-routine-clause[optseq] new-line
44027 function-definition
44028
44029 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
44030 */
44031
44032 #define OACC_ROUTINE_CLAUSE_MASK \
44033 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
44034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
44035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
44036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
44037
44038
44039 /* Parse the OpenACC routine pragma. This has an optional '( name )'
44040 component, which must resolve to a declared namespace-scope
44041 function. The clauses are either processed directly (for a named
44042 function), or defered until the immediatley following declaration
44043 is parsed. */
44044
44045 static void
44046 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
44047 enum pragma_context context)
44048 {
44049 gcc_checking_assert (context == pragma_external);
44050 /* The checking for "another pragma following this one" in the "no optional
44051 '( name )'" case makes sure that we dont re-enter. */
44052 gcc_checking_assert (parser->oacc_routine == NULL);
44053
44054 cp_oacc_routine_data data;
44055 data.error_seen = false;
44056 data.fndecl_seen = false;
44057 data.tokens = vNULL;
44058 data.clauses = NULL_TREE;
44059 data.loc = pragma_tok->location;
44060 /* It is safe to take the address of a local variable; it will only be
44061 used while this scope is live. */
44062 parser->oacc_routine = &data;
44063
44064 /* Look for optional '( name )'. */
44065 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
44066 {
44067 matching_parens parens;
44068 parens.consume_open (parser); /* '(' */
44069
44070 /* We parse the name as an id-expression. If it resolves to
44071 anything other than a non-overloaded function at namespace
44072 scope, it's an error. */
44073 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
44074 tree name = cp_parser_id_expression (parser,
44075 /*template_keyword_p=*/false,
44076 /*check_dependency_p=*/false,
44077 /*template_p=*/NULL,
44078 /*declarator_p=*/false,
44079 /*optional_p=*/false);
44080 tree decl = (identifier_p (name)
44081 ? cp_parser_lookup_name_simple (parser, name, name_loc)
44082 : name);
44083 if (name != error_mark_node && decl == error_mark_node)
44084 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
44085
44086 if (decl == error_mark_node
44087 || !parens.require_close (parser))
44088 {
44089 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44090 parser->oacc_routine = NULL;
44091 return;
44092 }
44093
44094 data.clauses
44095 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
44096 "#pragma acc routine",
44097 cp_lexer_peek_token (parser->lexer));
44098 /* The clauses are in reverse order; fix that to make later diagnostic
44099 emission easier. */
44100 data.clauses = nreverse (data.clauses);
44101
44102 if (decl && is_overloaded_fn (decl)
44103 && (TREE_CODE (decl) != FUNCTION_DECL
44104 || DECL_FUNCTION_TEMPLATE_P (decl)))
44105 {
44106 error_at (name_loc,
44107 "%<#pragma acc routine%> names a set of overloads");
44108 parser->oacc_routine = NULL;
44109 return;
44110 }
44111
44112 /* Perhaps we should use the same rule as declarations in different
44113 namespaces? */
44114 if (!DECL_NAMESPACE_SCOPE_P (decl))
44115 {
44116 error_at (name_loc,
44117 "%qD does not refer to a namespace scope function", decl);
44118 parser->oacc_routine = NULL;
44119 return;
44120 }
44121
44122 if (TREE_CODE (decl) != FUNCTION_DECL)
44123 {
44124 error_at (name_loc, "%qD does not refer to a function", decl);
44125 parser->oacc_routine = NULL;
44126 return;
44127 }
44128
44129 cp_finalize_oacc_routine (parser, decl, false);
44130 parser->oacc_routine = NULL;
44131 }
44132 else /* No optional '( name )'. */
44133 {
44134 /* Store away all pragma tokens. */
44135 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
44136 cp_lexer_consume_token (parser->lexer);
44137 cp_parser_require_pragma_eol (parser, pragma_tok);
44138 struct cp_token_cache *cp
44139 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
44140 parser->oacc_routine->tokens.safe_push (cp);
44141
44142 /* Emit a helpful diagnostic if there's another pragma following this
44143 one. */
44144 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
44145 {
44146 cp_ensure_no_oacc_routine (parser);
44147 data.tokens.release ();
44148 /* ..., and then just keep going. */
44149 return;
44150 }
44151
44152 /* We only have to consider the pragma_external case here. */
44153 cp_parser_declaration (parser, NULL_TREE);
44154 if (parser->oacc_routine
44155 && !parser->oacc_routine->fndecl_seen)
44156 cp_ensure_no_oacc_routine (parser);
44157 else
44158 parser->oacc_routine = NULL;
44159 data.tokens.release ();
44160 }
44161 }
44162
44163 /* Finalize #pragma acc routine clauses after direct declarator has
44164 been parsed. */
44165
44166 static tree
44167 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
44168 {
44169 struct cp_token_cache *ce;
44170 cp_oacc_routine_data *data = parser->oacc_routine;
44171
44172 if (!data->error_seen && data->fndecl_seen)
44173 {
44174 error_at (data->loc,
44175 "%<#pragma acc routine%> not immediately followed by "
44176 "a single function declaration or definition");
44177 data->error_seen = true;
44178 }
44179 if (data->error_seen)
44180 return attrs;
44181
44182 gcc_checking_assert (data->tokens.length () == 1);
44183 ce = data->tokens[0];
44184
44185 cp_parser_push_lexer_for_tokens (parser, ce);
44186 parser->lexer->in_pragma = true;
44187 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
44188
44189 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
44190 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
44191 parser->oacc_routine->clauses
44192 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
44193 "#pragma acc routine", pragma_tok);
44194 /* The clauses are in reverse order; fix that to make later diagnostic
44195 emission easier. */
44196 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
44197 cp_parser_pop_lexer (parser);
44198 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
44199 fndecl_seen. */
44200
44201 return attrs;
44202 }
44203
44204 /* Apply any saved OpenACC routine clauses to a just-parsed
44205 declaration. */
44206
44207 static void
44208 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
44209 {
44210 if (__builtin_expect (parser->oacc_routine != NULL, 0))
44211 {
44212 /* Keep going if we're in error reporting mode. */
44213 if (parser->oacc_routine->error_seen
44214 || fndecl == error_mark_node)
44215 return;
44216
44217 if (parser->oacc_routine->fndecl_seen)
44218 {
44219 error_at (parser->oacc_routine->loc,
44220 "%<#pragma acc routine%> not immediately followed by"
44221 " a single function declaration or definition");
44222 parser->oacc_routine = NULL;
44223 return;
44224 }
44225 if (TREE_CODE (fndecl) != FUNCTION_DECL)
44226 {
44227 cp_ensure_no_oacc_routine (parser);
44228 return;
44229 }
44230
44231 int compatible
44232 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
44233 parser->oacc_routine->loc,
44234 "#pragma acc routine");
44235 if (compatible < 0)
44236 {
44237 parser->oacc_routine = NULL;
44238 return;
44239 }
44240 if (compatible > 0)
44241 {
44242 }
44243 else
44244 {
44245 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
44246 {
44247 error_at (parser->oacc_routine->loc,
44248 TREE_USED (fndecl)
44249 ? G_("%<#pragma acc routine%> must be applied before"
44250 " use")
44251 : G_("%<#pragma acc routine%> must be applied before"
44252 " definition"));
44253 parser->oacc_routine = NULL;
44254 return;
44255 }
44256
44257 /* Set the routine's level of parallelism. */
44258 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
44259 oacc_replace_fn_attrib (fndecl, dims);
44260
44261 /* Add an "omp declare target" attribute. */
44262 DECL_ATTRIBUTES (fndecl)
44263 = tree_cons (get_identifier ("omp declare target"),
44264 parser->oacc_routine->clauses,
44265 DECL_ATTRIBUTES (fndecl));
44266 }
44267
44268 /* Don't unset parser->oacc_routine here: we may still need it to
44269 diagnose wrong usage. But, remember that we've used this "#pragma acc
44270 routine". */
44271 parser->oacc_routine->fndecl_seen = true;
44272 }
44273 }
44274
44275 /* Main entry point to OpenMP statement pragmas. */
44276
44277 static void
44278 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44279 {
44280 tree stmt;
44281 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
44282 omp_clause_mask mask (0);
44283
44284 switch (cp_parser_pragma_kind (pragma_tok))
44285 {
44286 case PRAGMA_OACC_ATOMIC:
44287 cp_parser_omp_atomic (parser, pragma_tok, true);
44288 return;
44289 case PRAGMA_OACC_CACHE:
44290 stmt = cp_parser_oacc_cache (parser, pragma_tok);
44291 break;
44292 case PRAGMA_OACC_DATA:
44293 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
44294 break;
44295 case PRAGMA_OACC_ENTER_DATA:
44296 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
44297 break;
44298 case PRAGMA_OACC_EXIT_DATA:
44299 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
44300 break;
44301 case PRAGMA_OACC_HOST_DATA:
44302 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
44303 break;
44304 case PRAGMA_OACC_KERNELS:
44305 case PRAGMA_OACC_PARALLEL:
44306 case PRAGMA_OACC_SERIAL:
44307 strcpy (p_name, "#pragma acc");
44308 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
44309 break;
44310 case PRAGMA_OACC_LOOP:
44311 strcpy (p_name, "#pragma acc");
44312 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
44313 if_p);
44314 break;
44315 case PRAGMA_OACC_UPDATE:
44316 stmt = cp_parser_oacc_update (parser, pragma_tok);
44317 break;
44318 case PRAGMA_OACC_WAIT:
44319 stmt = cp_parser_oacc_wait (parser, pragma_tok);
44320 break;
44321 case PRAGMA_OMP_ALLOCATE:
44322 cp_parser_omp_allocate (parser, pragma_tok);
44323 return;
44324 case PRAGMA_OMP_ATOMIC:
44325 cp_parser_omp_atomic (parser, pragma_tok, false);
44326 return;
44327 case PRAGMA_OMP_CRITICAL:
44328 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
44329 break;
44330 case PRAGMA_OMP_DISTRIBUTE:
44331 strcpy (p_name, "#pragma omp");
44332 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
44333 if_p);
44334 break;
44335 case PRAGMA_OMP_FOR:
44336 strcpy (p_name, "#pragma omp");
44337 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
44338 if_p);
44339 break;
44340 case PRAGMA_OMP_LOOP:
44341 strcpy (p_name, "#pragma omp");
44342 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
44343 if_p);
44344 break;
44345 case PRAGMA_OMP_MASTER:
44346 strcpy (p_name, "#pragma omp");
44347 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
44348 if_p);
44349 break;
44350 case PRAGMA_OMP_PARALLEL:
44351 strcpy (p_name, "#pragma omp");
44352 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
44353 if_p);
44354 break;
44355 case PRAGMA_OMP_SECTIONS:
44356 strcpy (p_name, "#pragma omp");
44357 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
44358 break;
44359 case PRAGMA_OMP_SIMD:
44360 strcpy (p_name, "#pragma omp");
44361 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
44362 if_p);
44363 break;
44364 case PRAGMA_OMP_SINGLE:
44365 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
44366 break;
44367 case PRAGMA_OMP_TASK:
44368 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
44369 break;
44370 case PRAGMA_OMP_TASKGROUP:
44371 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
44372 break;
44373 case PRAGMA_OMP_TASKLOOP:
44374 strcpy (p_name, "#pragma omp");
44375 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
44376 if_p);
44377 break;
44378 case PRAGMA_OMP_TEAMS:
44379 strcpy (p_name, "#pragma omp");
44380 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
44381 if_p);
44382 break;
44383 default:
44384 gcc_unreachable ();
44385 }
44386
44387 protected_set_expr_location (stmt, pragma_tok->location);
44388 }
44389 \f
44390 /* Transactional Memory parsing routines. */
44391
44392 /* Parse a transaction attribute.
44393
44394 txn-attribute:
44395 attribute
44396 [ [ identifier ] ]
44397
44398 We use this instead of cp_parser_attributes_opt for transactions to avoid
44399 the pedwarn in C++98 mode. */
44400
44401 static tree
44402 cp_parser_txn_attribute_opt (cp_parser *parser)
44403 {
44404 cp_token *token;
44405 tree attr_name, attr = NULL;
44406
44407 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
44408 return cp_parser_attributes_opt (parser);
44409
44410 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
44411 return NULL_TREE;
44412 cp_lexer_consume_token (parser->lexer);
44413 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
44414 goto error1;
44415
44416 token = cp_lexer_peek_token (parser->lexer);
44417 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
44418 {
44419 token = cp_lexer_consume_token (parser->lexer);
44420
44421 attr_name = (token->type == CPP_KEYWORD
44422 /* For keywords, use the canonical spelling,
44423 not the parsed identifier. */
44424 ? ridpointers[(int) token->keyword]
44425 : token->u.value);
44426 attr = build_tree_list (attr_name, NULL_TREE);
44427 }
44428 else
44429 cp_parser_error (parser, "expected identifier");
44430
44431 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
44432 error1:
44433 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
44434 return attr;
44435 }
44436
44437 /* Parse a __transaction_atomic or __transaction_relaxed statement.
44438
44439 transaction-statement:
44440 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
44441 compound-statement
44442 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
44443 */
44444
44445 static tree
44446 cp_parser_transaction (cp_parser *parser, cp_token *token)
44447 {
44448 unsigned char old_in = parser->in_transaction;
44449 unsigned char this_in = 1, new_in;
44450 enum rid keyword = token->keyword;
44451 tree stmt, attrs, noex;
44452
44453 cp_lexer_consume_token (parser->lexer);
44454
44455 if (keyword == RID_TRANSACTION_RELAXED
44456 || keyword == RID_SYNCHRONIZED)
44457 this_in |= TM_STMT_ATTR_RELAXED;
44458 else
44459 {
44460 attrs = cp_parser_txn_attribute_opt (parser);
44461 if (attrs)
44462 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
44463 }
44464
44465 /* Parse a noexcept specification. */
44466 if (keyword == RID_ATOMIC_NOEXCEPT)
44467 noex = boolean_true_node;
44468 else if (keyword == RID_ATOMIC_CANCEL)
44469 {
44470 /* cancel-and-throw is unimplemented. */
44471 sorry ("%<atomic_cancel%>");
44472 noex = NULL_TREE;
44473 }
44474 else
44475 noex = cp_parser_noexcept_specification_opt (parser,
44476 CP_PARSER_FLAGS_NONE,
44477 /*require_constexpr=*/true,
44478 /*consumed_expr=*/NULL,
44479 /*return_cond=*/true);
44480
44481 /* Keep track if we're in the lexical scope of an outer transaction. */
44482 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
44483
44484 stmt = begin_transaction_stmt (token->location, NULL, this_in);
44485
44486 parser->in_transaction = new_in;
44487 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
44488 parser->in_transaction = old_in;
44489
44490 finish_transaction_stmt (stmt, NULL, this_in, noex);
44491
44492 return stmt;
44493 }
44494
44495 /* Parse a __transaction_atomic or __transaction_relaxed expression.
44496
44497 transaction-expression:
44498 __transaction_atomic txn-noexcept-spec[opt] ( expression )
44499 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
44500 */
44501
44502 static tree
44503 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
44504 {
44505 unsigned char old_in = parser->in_transaction;
44506 unsigned char this_in = 1;
44507 cp_token *token;
44508 tree expr, noex;
44509 bool noex_expr;
44510 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44511
44512 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
44513 || keyword == RID_TRANSACTION_RELAXED);
44514
44515 if (!flag_tm)
44516 error_at (loc,
44517 keyword == RID_TRANSACTION_RELAXED
44518 ? G_("%<__transaction_relaxed%> without transactional memory "
44519 "support enabled")
44520 : G_("%<__transaction_atomic%> without transactional memory "
44521 "support enabled"));
44522
44523 token = cp_parser_require_keyword (parser, keyword,
44524 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
44525 : RT_TRANSACTION_RELAXED));
44526 gcc_assert (token != NULL);
44527
44528 if (keyword == RID_TRANSACTION_RELAXED)
44529 this_in |= TM_STMT_ATTR_RELAXED;
44530
44531 /* Set this early. This might mean that we allow transaction_cancel in
44532 an expression that we find out later actually has to be a constexpr.
44533 However, we expect that cxx_constant_value will be able to deal with
44534 this; also, if the noexcept has no constexpr, then what we parse next
44535 really is a transaction's body. */
44536 parser->in_transaction = this_in;
44537
44538 /* Parse a noexcept specification. */
44539 noex = cp_parser_noexcept_specification_opt (parser,
44540 CP_PARSER_FLAGS_NONE,
44541 /*require_constexpr=*/false,
44542 &noex_expr,
44543 /*return_cond=*/true);
44544
44545 if (!noex || !noex_expr
44546 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
44547 {
44548 matching_parens parens;
44549 parens.require_open (parser);
44550
44551 expr = cp_parser_expression (parser);
44552 expr = finish_parenthesized_expr (expr);
44553
44554 parens.require_close (parser);
44555 }
44556 else
44557 {
44558 /* The only expression that is available got parsed for the noexcept
44559 already. noexcept is true then. */
44560 expr = noex;
44561 noex = boolean_true_node;
44562 }
44563
44564 expr = build_transaction_expr (token->location, expr, this_in, noex);
44565 parser->in_transaction = old_in;
44566
44567 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
44568 return error_mark_node;
44569
44570 return (flag_tm ? expr : error_mark_node);
44571 }
44572
44573 /* Parse a function-transaction-block.
44574
44575 function-transaction-block:
44576 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
44577 function-body
44578 __transaction_atomic txn-attribute[opt] function-try-block
44579 __transaction_relaxed ctor-initializer[opt] function-body
44580 __transaction_relaxed function-try-block
44581 */
44582
44583 static void
44584 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
44585 {
44586 unsigned char old_in = parser->in_transaction;
44587 unsigned char new_in = 1;
44588 tree compound_stmt, stmt, attrs;
44589 cp_token *token;
44590
44591 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
44592 || keyword == RID_TRANSACTION_RELAXED);
44593 token = cp_parser_require_keyword (parser, keyword,
44594 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
44595 : RT_TRANSACTION_RELAXED));
44596 gcc_assert (token != NULL);
44597
44598 if (keyword == RID_TRANSACTION_RELAXED)
44599 new_in |= TM_STMT_ATTR_RELAXED;
44600 else
44601 {
44602 attrs = cp_parser_txn_attribute_opt (parser);
44603 if (attrs)
44604 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
44605 }
44606
44607 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
44608
44609 parser->in_transaction = new_in;
44610
44611 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
44612 cp_parser_function_try_block (parser);
44613 else
44614 cp_parser_ctor_initializer_opt_and_function_body
44615 (parser, /*in_function_try_block=*/false);
44616
44617 parser->in_transaction = old_in;
44618
44619 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
44620 }
44621
44622 /* Parse a __transaction_cancel statement.
44623
44624 cancel-statement:
44625 __transaction_cancel txn-attribute[opt] ;
44626 __transaction_cancel txn-attribute[opt] throw-expression ;
44627
44628 ??? Cancel and throw is not yet implemented. */
44629
44630 static tree
44631 cp_parser_transaction_cancel (cp_parser *parser)
44632 {
44633 cp_token *token;
44634 bool is_outer = false;
44635 tree stmt, attrs;
44636
44637 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
44638 RT_TRANSACTION_CANCEL);
44639 gcc_assert (token != NULL);
44640
44641 attrs = cp_parser_txn_attribute_opt (parser);
44642 if (attrs)
44643 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
44644
44645 /* ??? Parse cancel-and-throw here. */
44646
44647 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
44648
44649 if (!flag_tm)
44650 {
44651 error_at (token->location, "%<__transaction_cancel%> without "
44652 "transactional memory support enabled");
44653 return error_mark_node;
44654 }
44655 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
44656 {
44657 error_at (token->location, "%<__transaction_cancel%> within a "
44658 "%<__transaction_relaxed%>");
44659 return error_mark_node;
44660 }
44661 else if (is_outer)
44662 {
44663 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
44664 && !is_tm_may_cancel_outer (current_function_decl))
44665 {
44666 error_at (token->location, "outer %<__transaction_cancel%> not "
44667 "within outer %<__transaction_atomic%>");
44668 error_at (token->location,
44669 " or a %<transaction_may_cancel_outer%> function");
44670 return error_mark_node;
44671 }
44672 }
44673 else if (parser->in_transaction == 0)
44674 {
44675 error_at (token->location, "%<__transaction_cancel%> not within "
44676 "%<__transaction_atomic%>");
44677 return error_mark_node;
44678 }
44679
44680 stmt = build_tm_abort_call (token->location, is_outer);
44681 add_stmt (stmt);
44682
44683 return stmt;
44684 }
44685 \f
44686 /* The parser. */
44687
44688 static GTY (()) cp_parser *the_parser;
44689
44690 \f
44691 /* Special handling for the first token or line in the file. The first
44692 thing in the file might be #pragma GCC pch_preprocess, which loads a
44693 PCH file, which is a GC collection point. So we need to handle this
44694 first pragma without benefit of an existing lexer structure.
44695
44696 Always returns one token to the caller in *FIRST_TOKEN. This is
44697 either the true first token of the file, or the first token after
44698 the initial pragma. */
44699
44700 static void
44701 cp_parser_initial_pragma (cp_token *first_token)
44702 {
44703 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
44704 return;
44705
44706 cp_lexer_get_preprocessor_token (0, first_token);
44707
44708 tree name = NULL;
44709 if (first_token->type == CPP_STRING)
44710 {
44711 name = first_token->u.value;
44712
44713 cp_lexer_get_preprocessor_token (0, first_token);
44714 }
44715
44716 /* Skip to the end of the pragma. */
44717 if (first_token->type != CPP_PRAGMA_EOL)
44718 {
44719 error_at (first_token->location,
44720 "malformed %<#pragma GCC pch_preprocess%>");
44721 do
44722 cp_lexer_get_preprocessor_token (0, first_token);
44723 while (first_token->type != CPP_PRAGMA_EOL);
44724 }
44725
44726 /* Now actually load the PCH file. */
44727 if (name)
44728 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
44729
44730 /* Read one more token to return to our caller. We have to do this
44731 after reading the PCH file in, since its pointers have to be
44732 live. */
44733 cp_lexer_get_preprocessor_token (0, first_token);
44734 }
44735
44736 /* Parse a pragma GCC ivdep. */
44737
44738 static bool
44739 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
44740 {
44741 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44742 return true;
44743 }
44744
44745 /* Parse a pragma GCC unroll. */
44746
44747 static unsigned short
44748 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
44749 {
44750 location_t location = cp_lexer_peek_token (parser->lexer)->location;
44751 tree expr = cp_parser_constant_expression (parser);
44752 unsigned short unroll;
44753 expr = maybe_constant_value (expr);
44754 HOST_WIDE_INT lunroll = 0;
44755 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
44756 || TREE_CODE (expr) != INTEGER_CST
44757 || (lunroll = tree_to_shwi (expr)) < 0
44758 || lunroll >= USHRT_MAX)
44759 {
44760 error_at (location, "%<#pragma GCC unroll%> requires an"
44761 " assignment-expression that evaluates to a non-negative"
44762 " integral constant less than %u", USHRT_MAX);
44763 unroll = 0;
44764 }
44765 else
44766 {
44767 unroll = (unsigned short)lunroll;
44768 if (unroll == 0)
44769 unroll = 1;
44770 }
44771 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44772 return unroll;
44773 }
44774
44775 /* Normal parsing of a pragma token. Here we can (and must) use the
44776 regular lexer. */
44777
44778 static bool
44779 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
44780 {
44781 cp_token *pragma_tok;
44782 unsigned int id;
44783 tree stmt;
44784 bool ret;
44785
44786 pragma_tok = cp_lexer_consume_token (parser->lexer);
44787 gcc_assert (pragma_tok->type == CPP_PRAGMA);
44788 parser->lexer->in_pragma = true;
44789
44790 id = cp_parser_pragma_kind (pragma_tok);
44791 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
44792 cp_ensure_no_omp_declare_simd (parser);
44793 switch (id)
44794 {
44795 case PRAGMA_GCC_PCH_PREPROCESS:
44796 error_at (pragma_tok->location,
44797 "%<#pragma GCC pch_preprocess%> must be first");
44798 break;
44799
44800 case PRAGMA_OMP_BARRIER:
44801 switch (context)
44802 {
44803 case pragma_compound:
44804 cp_parser_omp_barrier (parser, pragma_tok);
44805 return false;
44806 case pragma_stmt:
44807 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
44808 "used in compound statements", "omp barrier");
44809 break;
44810 default:
44811 goto bad_stmt;
44812 }
44813 break;
44814
44815 case PRAGMA_OMP_DEPOBJ:
44816 switch (context)
44817 {
44818 case pragma_compound:
44819 cp_parser_omp_depobj (parser, pragma_tok);
44820 return false;
44821 case pragma_stmt:
44822 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
44823 "used in compound statements", "omp depobj");
44824 break;
44825 default:
44826 goto bad_stmt;
44827 }
44828 break;
44829
44830 case PRAGMA_OMP_FLUSH:
44831 switch (context)
44832 {
44833 case pragma_compound:
44834 cp_parser_omp_flush (parser, pragma_tok);
44835 return false;
44836 case pragma_stmt:
44837 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
44838 "used in compound statements", "omp flush");
44839 break;
44840 default:
44841 goto bad_stmt;
44842 }
44843 break;
44844
44845 case PRAGMA_OMP_TASKWAIT:
44846 switch (context)
44847 {
44848 case pragma_compound:
44849 cp_parser_omp_taskwait (parser, pragma_tok);
44850 return false;
44851 case pragma_stmt:
44852 error_at (pragma_tok->location,
44853 "%<#pragma %s%> may only be used in compound statements",
44854 "omp taskwait");
44855 break;
44856 default:
44857 goto bad_stmt;
44858 }
44859 break;
44860
44861 case PRAGMA_OMP_TASKYIELD:
44862 switch (context)
44863 {
44864 case pragma_compound:
44865 cp_parser_omp_taskyield (parser, pragma_tok);
44866 return false;
44867 case pragma_stmt:
44868 error_at (pragma_tok->location,
44869 "%<#pragma %s%> may only be used in compound statements",
44870 "omp taskyield");
44871 break;
44872 default:
44873 goto bad_stmt;
44874 }
44875 break;
44876
44877 case PRAGMA_OMP_CANCEL:
44878 switch (context)
44879 {
44880 case pragma_compound:
44881 cp_parser_omp_cancel (parser, pragma_tok);
44882 return false;
44883 case pragma_stmt:
44884 error_at (pragma_tok->location,
44885 "%<#pragma %s%> may only be used in compound statements",
44886 "omp cancel");
44887 break;
44888 default:
44889 goto bad_stmt;
44890 }
44891 break;
44892
44893 case PRAGMA_OMP_CANCELLATION_POINT:
44894 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
44895 return false;
44896
44897 case PRAGMA_OMP_THREADPRIVATE:
44898 cp_parser_omp_threadprivate (parser, pragma_tok);
44899 return false;
44900
44901 case PRAGMA_OMP_DECLARE:
44902 return cp_parser_omp_declare (parser, pragma_tok, context);
44903
44904 case PRAGMA_OACC_DECLARE:
44905 cp_parser_oacc_declare (parser, pragma_tok);
44906 return false;
44907
44908 case PRAGMA_OACC_ENTER_DATA:
44909 if (context == pragma_stmt)
44910 {
44911 error_at (pragma_tok->location,
44912 "%<#pragma %s%> may only be used in compound statements",
44913 "acc enter data");
44914 break;
44915 }
44916 else if (context != pragma_compound)
44917 goto bad_stmt;
44918 cp_parser_omp_construct (parser, pragma_tok, if_p);
44919 return true;
44920
44921 case PRAGMA_OACC_EXIT_DATA:
44922 if (context == pragma_stmt)
44923 {
44924 error_at (pragma_tok->location,
44925 "%<#pragma %s%> may only be used in compound statements",
44926 "acc exit data");
44927 break;
44928 }
44929 else if (context != pragma_compound)
44930 goto bad_stmt;
44931 cp_parser_omp_construct (parser, pragma_tok, if_p);
44932 return true;
44933
44934 case PRAGMA_OACC_ROUTINE:
44935 if (context != pragma_external)
44936 {
44937 error_at (pragma_tok->location,
44938 "%<#pragma acc routine%> must be at file scope");
44939 break;
44940 }
44941 cp_parser_oacc_routine (parser, pragma_tok, context);
44942 return false;
44943
44944 case PRAGMA_OACC_UPDATE:
44945 if (context == pragma_stmt)
44946 {
44947 error_at (pragma_tok->location,
44948 "%<#pragma %s%> may only be used in compound statements",
44949 "acc update");
44950 break;
44951 }
44952 else if (context != pragma_compound)
44953 goto bad_stmt;
44954 cp_parser_omp_construct (parser, pragma_tok, if_p);
44955 return true;
44956
44957 case PRAGMA_OACC_WAIT:
44958 if (context == pragma_stmt)
44959 {
44960 error_at (pragma_tok->location,
44961 "%<#pragma %s%> may only be used in compound statements",
44962 "acc wait");
44963 break;
44964 }
44965 else if (context != pragma_compound)
44966 goto bad_stmt;
44967 cp_parser_omp_construct (parser, pragma_tok, if_p);
44968 return true;
44969 case PRAGMA_OMP_ALLOCATE:
44970 cp_parser_omp_allocate (parser, pragma_tok);
44971 return false;
44972 case PRAGMA_OACC_ATOMIC:
44973 case PRAGMA_OACC_CACHE:
44974 case PRAGMA_OACC_DATA:
44975 case PRAGMA_OACC_HOST_DATA:
44976 case PRAGMA_OACC_KERNELS:
44977 case PRAGMA_OACC_LOOP:
44978 case PRAGMA_OACC_PARALLEL:
44979 case PRAGMA_OACC_SERIAL:
44980 case PRAGMA_OMP_ATOMIC:
44981 case PRAGMA_OMP_CRITICAL:
44982 case PRAGMA_OMP_DISTRIBUTE:
44983 case PRAGMA_OMP_FOR:
44984 case PRAGMA_OMP_LOOP:
44985 case PRAGMA_OMP_MASTER:
44986 case PRAGMA_OMP_PARALLEL:
44987 case PRAGMA_OMP_SECTIONS:
44988 case PRAGMA_OMP_SIMD:
44989 case PRAGMA_OMP_SINGLE:
44990 case PRAGMA_OMP_TASK:
44991 case PRAGMA_OMP_TASKGROUP:
44992 case PRAGMA_OMP_TASKLOOP:
44993 case PRAGMA_OMP_TEAMS:
44994 if (context != pragma_stmt && context != pragma_compound)
44995 goto bad_stmt;
44996 stmt = push_omp_privatization_clauses (false);
44997 cp_parser_omp_construct (parser, pragma_tok, if_p);
44998 pop_omp_privatization_clauses (stmt);
44999 return true;
45000
45001 case PRAGMA_OMP_REQUIRES:
45002 if (context != pragma_external)
45003 {
45004 error_at (pragma_tok->location,
45005 "%<#pragma omp requires%> may only be used at file or "
45006 "namespace scope");
45007 break;
45008 }
45009 return cp_parser_omp_requires (parser, pragma_tok);
45010
45011 case PRAGMA_OMP_ORDERED:
45012 if (context != pragma_stmt && context != pragma_compound)
45013 goto bad_stmt;
45014 stmt = push_omp_privatization_clauses (false);
45015 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
45016 pop_omp_privatization_clauses (stmt);
45017 return ret;
45018
45019 case PRAGMA_OMP_TARGET:
45020 if (context != pragma_stmt && context != pragma_compound)
45021 goto bad_stmt;
45022 stmt = push_omp_privatization_clauses (false);
45023 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
45024 pop_omp_privatization_clauses (stmt);
45025 return ret;
45026
45027 case PRAGMA_OMP_END_DECLARE_TARGET:
45028 cp_parser_omp_end_declare_target (parser, pragma_tok);
45029 return false;
45030
45031 case PRAGMA_OMP_SCAN:
45032 error_at (pragma_tok->location,
45033 "%<#pragma omp scan%> may only be used in "
45034 "a loop construct with %<inscan%> %<reduction%> clause");
45035 break;
45036
45037 case PRAGMA_OMP_SECTION:
45038 error_at (pragma_tok->location,
45039 "%<#pragma omp section%> may only be used in "
45040 "%<#pragma omp sections%> construct");
45041 break;
45042
45043 case PRAGMA_IVDEP:
45044 {
45045 if (context == pragma_external)
45046 {
45047 error_at (pragma_tok->location,
45048 "%<#pragma GCC ivdep%> must be inside a function");
45049 break;
45050 }
45051 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
45052 unsigned short unroll;
45053 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
45054 if (tok->type == CPP_PRAGMA
45055 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
45056 {
45057 tok = cp_lexer_consume_token (parser->lexer);
45058 unroll = cp_parser_pragma_unroll (parser, tok);
45059 tok = cp_lexer_peek_token (the_parser->lexer);
45060 }
45061 else
45062 unroll = 0;
45063 if (tok->type != CPP_KEYWORD
45064 || (tok->keyword != RID_FOR
45065 && tok->keyword != RID_WHILE
45066 && tok->keyword != RID_DO))
45067 {
45068 cp_parser_error (parser, "for, while or do statement expected");
45069 return false;
45070 }
45071 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
45072 return true;
45073 }
45074
45075 case PRAGMA_UNROLL:
45076 {
45077 if (context == pragma_external)
45078 {
45079 error_at (pragma_tok->location,
45080 "%<#pragma GCC unroll%> must be inside a function");
45081 break;
45082 }
45083 const unsigned short unroll
45084 = cp_parser_pragma_unroll (parser, pragma_tok);
45085 bool ivdep;
45086 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
45087 if (tok->type == CPP_PRAGMA
45088 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
45089 {
45090 tok = cp_lexer_consume_token (parser->lexer);
45091 ivdep = cp_parser_pragma_ivdep (parser, tok);
45092 tok = cp_lexer_peek_token (the_parser->lexer);
45093 }
45094 else
45095 ivdep = false;
45096 if (tok->type != CPP_KEYWORD
45097 || (tok->keyword != RID_FOR
45098 && tok->keyword != RID_WHILE
45099 && tok->keyword != RID_DO))
45100 {
45101 cp_parser_error (parser, "for, while or do statement expected");
45102 return false;
45103 }
45104 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
45105 return true;
45106 }
45107
45108 default:
45109 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
45110 c_invoke_pragma_handler (id);
45111 break;
45112
45113 bad_stmt:
45114 cp_parser_error (parser, "expected declaration specifiers");
45115 break;
45116 }
45117
45118 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45119 return false;
45120 }
45121
45122 /* The interface the pragma parsers have to the lexer. */
45123
45124 enum cpp_ttype
45125 pragma_lex (tree *value, location_t *loc)
45126 {
45127 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
45128 enum cpp_ttype ret = tok->type;
45129
45130 *value = tok->u.value;
45131 if (loc)
45132 *loc = tok->location;
45133
45134 if (ret == CPP_PRAGMA_EOL)
45135 ret = CPP_EOF;
45136 else if (ret == CPP_STRING)
45137 *value = cp_parser_string_literal (the_parser, false, false);
45138 else
45139 {
45140 if (ret == CPP_KEYWORD)
45141 ret = CPP_NAME;
45142 cp_lexer_consume_token (the_parser->lexer);
45143 }
45144
45145 return ret;
45146 }
45147
45148 \f
45149 /* External interface. */
45150
45151 /* Parse one entire translation unit. */
45152
45153 void
45154 c_parse_file (void)
45155 {
45156 static bool already_called = false;
45157
45158 if (already_called)
45159 fatal_error (input_location,
45160 "multi-source compilation not implemented for C++");
45161 already_called = true;
45162
45163 /* cp_lexer_new_main is called before doing any GC allocation
45164 because tokenization might load a PCH file. */
45165 cp_lexer *lexer = cp_lexer_new_main ();
45166
45167 the_parser = cp_parser_new (lexer);
45168
45169 cp_parser_translation_unit (the_parser);
45170 class_decl_loc_t::diag_mismatched_tags ();
45171
45172 the_parser = NULL;
45173
45174 finish_translation_unit ();
45175 }
45176
45177 /* Create an identifier for a generic parameter type (a synthesized
45178 template parameter implied by `auto' or a concept identifier). */
45179
45180 static GTY(()) int generic_parm_count;
45181 static tree
45182 make_generic_type_name ()
45183 {
45184 char buf[32];
45185 sprintf (buf, "auto:%d", ++generic_parm_count);
45186 return get_identifier (buf);
45187 }
45188
45189 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
45190 (creating a new template parameter list if necessary). Returns the newly
45191 created template type parm. */
45192
45193 static tree
45194 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
45195 {
45196 /* A requires-clause is not a function and cannot have placeholders. */
45197 if (current_binding_level->kind == sk_block)
45198 {
45199 error ("placeholder type not allowed in this context");
45200 return error_mark_node;
45201 }
45202
45203 gcc_assert (current_binding_level->kind == sk_function_parms);
45204
45205 /* We are either continuing a function template that already contains implicit
45206 template parameters, creating a new fully-implicit function template, or
45207 extending an existing explicit function template with implicit template
45208 parameters. */
45209
45210 cp_binding_level *const entry_scope = current_binding_level;
45211
45212 bool become_template = false;
45213 cp_binding_level *parent_scope = 0;
45214
45215 if (parser->implicit_template_scope)
45216 {
45217 gcc_assert (parser->implicit_template_parms);
45218
45219 current_binding_level = parser->implicit_template_scope;
45220 }
45221 else
45222 {
45223 /* Roll back to the existing template parameter scope (in the case of
45224 extending an explicit function template) or introduce a new template
45225 parameter scope ahead of the function parameter scope (or class scope
45226 in the case of out-of-line member definitions). The function scope is
45227 added back after template parameter synthesis below. */
45228
45229 cp_binding_level *scope = entry_scope;
45230
45231 while (scope->kind == sk_function_parms)
45232 {
45233 parent_scope = scope;
45234 scope = scope->level_chain;
45235 }
45236 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
45237 {
45238 /* If not defining a class, then any class scope is a scope level in
45239 an out-of-line member definition. In this case simply wind back
45240 beyond the first such scope to inject the template parameter list.
45241 Otherwise wind back to the class being defined. The latter can
45242 occur in class member friend declarations such as:
45243
45244 class A {
45245 void foo (auto);
45246 };
45247 class B {
45248 friend void A::foo (auto);
45249 };
45250
45251 The template parameter list synthesized for the friend declaration
45252 must be injected in the scope of 'B'. This can also occur in
45253 erroneous cases such as:
45254
45255 struct A {
45256 struct B {
45257 void foo (auto);
45258 };
45259 void B::foo (auto) {}
45260 };
45261
45262 Here the attempted definition of 'B::foo' within 'A' is ill-formed
45263 but, nevertheless, the template parameter list synthesized for the
45264 declarator should be injected into the scope of 'A' as if the
45265 ill-formed template was specified explicitly. */
45266
45267 while (scope->kind == sk_class && !scope->defining_class_p)
45268 {
45269 parent_scope = scope;
45270 scope = scope->level_chain;
45271 }
45272 }
45273
45274 current_binding_level = scope;
45275
45276 if (scope->kind != sk_template_parms
45277 || !function_being_declared_is_template_p (parser))
45278 {
45279 /* Introduce a new template parameter list for implicit template
45280 parameters. */
45281
45282 become_template = true;
45283
45284 parser->implicit_template_scope
45285 = begin_scope (sk_template_parms, NULL);
45286
45287 ++processing_template_decl;
45288
45289 parser->fully_implicit_function_template_p = true;
45290 ++parser->num_template_parameter_lists;
45291 }
45292 else
45293 {
45294 /* Synthesize implicit template parameters at the end of the explicit
45295 template parameter list. */
45296
45297 gcc_assert (current_template_parms);
45298
45299 parser->implicit_template_scope = scope;
45300
45301 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
45302 parser->implicit_template_parms
45303 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
45304 }
45305 }
45306
45307 /* Synthesize a new template parameter and track the current template
45308 parameter chain with implicit_template_parms. */
45309
45310 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
45311 tree synth_id = make_generic_type_name ();
45312 tree synth_tmpl_parm;
45313 bool non_type = false;
45314
45315 /* Synthesize the type template parameter. */
45316 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
45317 synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
45318
45319 /* Attach the constraint to the parm before processing. */
45320 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
45321 TREE_TYPE (node) = constr;
45322 tree new_parm
45323 = process_template_parm (parser->implicit_template_parms,
45324 input_location,
45325 node,
45326 /*non_type=*/non_type,
45327 /*param_pack=*/false);
45328
45329 /* Mark the synthetic declaration "virtual". This is used when
45330 comparing template-heads to determine if whether an abbreviated
45331 function template is equivalent to an explicit template.
45332
45333 Note that DECL_ARTIFICIAL is used elsewhere for template parameters. */
45334 DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
45335
45336 // Chain the new parameter to the list of implicit parameters.
45337 if (parser->implicit_template_parms)
45338 parser->implicit_template_parms
45339 = TREE_CHAIN (parser->implicit_template_parms);
45340 else
45341 parser->implicit_template_parms = new_parm;
45342
45343 tree new_decl = get_local_decls ();
45344 if (non_type)
45345 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
45346 new_decl = DECL_INITIAL (new_decl);
45347
45348 /* If creating a fully implicit function template, start the new implicit
45349 template parameter list with this synthesized type, otherwise grow the
45350 current template parameter list. */
45351
45352 if (become_template)
45353 {
45354 parent_scope->level_chain = current_binding_level;
45355
45356 tree new_parms = make_tree_vec (1);
45357 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
45358 current_template_parms = tree_cons (size_int (processing_template_decl),
45359 new_parms, current_template_parms);
45360 }
45361 else
45362 {
45363 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
45364 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
45365 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
45366 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
45367 }
45368
45369 /* If the new parameter was constrained, we need to add that to the
45370 constraints in the template parameter list. */
45371 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
45372 {
45373 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
45374 reqs = combine_constraint_expressions (reqs, req);
45375 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
45376 }
45377
45378 current_binding_level = entry_scope;
45379
45380 return new_decl;
45381 }
45382
45383 /* Finish the declaration of a fully implicit function template. Such a
45384 template has no explicit template parameter list so has not been through the
45385 normal template head and tail processing. synthesize_implicit_template_parm
45386 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
45387 provided if the declaration is a class member such that its template
45388 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
45389 form is returned. Otherwise NULL_TREE is returned. */
45390
45391 static tree
45392 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
45393 {
45394 gcc_assert (parser->fully_implicit_function_template_p);
45395
45396 if (member_decl_opt && member_decl_opt != error_mark_node
45397 && DECL_VIRTUAL_P (member_decl_opt))
45398 {
45399 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
45400 "implicit templates may not be %<virtual%>");
45401 DECL_VIRTUAL_P (member_decl_opt) = false;
45402 }
45403
45404 if (member_decl_opt)
45405 member_decl_opt = finish_member_template_decl (member_decl_opt);
45406 end_template_decl ();
45407
45408 parser->fully_implicit_function_template_p = false;
45409 parser->implicit_template_parms = 0;
45410 parser->implicit_template_scope = 0;
45411 --parser->num_template_parameter_lists;
45412
45413 return member_decl_opt;
45414 }
45415
45416 /* Like finish_fully_implicit_template, but to be used in error
45417 recovery, rearranging scopes so that we restore the state we had
45418 before synthesize_implicit_template_parm inserted the implement
45419 template parms scope. */
45420
45421 static void
45422 abort_fully_implicit_template (cp_parser *parser)
45423 {
45424 cp_binding_level *return_to_scope = current_binding_level;
45425
45426 if (parser->implicit_template_scope
45427 && return_to_scope != parser->implicit_template_scope)
45428 {
45429 cp_binding_level *child = return_to_scope;
45430 for (cp_binding_level *scope = child->level_chain;
45431 scope != parser->implicit_template_scope;
45432 scope = child->level_chain)
45433 child = scope;
45434 child->level_chain = parser->implicit_template_scope->level_chain;
45435 parser->implicit_template_scope->level_chain = return_to_scope;
45436 current_binding_level = parser->implicit_template_scope;
45437 }
45438 else
45439 return_to_scope = return_to_scope->level_chain;
45440
45441 finish_fully_implicit_template (parser, NULL);
45442
45443 gcc_assert (current_binding_level == return_to_scope);
45444 }
45445
45446 /* Helper function for diagnostics that have complained about things
45447 being used with 'extern "C"' linkage.
45448
45449 Attempt to issue a note showing where the 'extern "C"' linkage began. */
45450
45451 void
45452 maybe_show_extern_c_location (void)
45453 {
45454 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
45455 inform (the_parser->innermost_linkage_specification_location,
45456 "%<extern \"C\"%> linkage started here");
45457 }
45458
45459 #include "gt-cp-parser.h"