528b41b717096b07590174003b8a9eeb760f500c
[gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2020 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
47 #include "memmodel.h"
48 #include "c-family/known-headers.h"
49
50 \f
51 /* The lexer. */
52
53 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
54 and c-lex.c) and the C++ parser. */
55
56 /* The various kinds of non integral constant we encounter. */
57 enum non_integral_constant {
58 NIC_NONE,
59 /* floating-point literal */
60 NIC_FLOAT,
61 /* %<this%> */
62 NIC_THIS,
63 /* %<__FUNCTION__%> */
64 NIC_FUNC_NAME,
65 /* %<__PRETTY_FUNCTION__%> */
66 NIC_PRETTY_FUNC,
67 /* %<__func__%> */
68 NIC_C99_FUNC,
69 /* "%<va_arg%> */
70 NIC_VA_ARG,
71 /* a cast */
72 NIC_CAST,
73 /* %<typeid%> operator */
74 NIC_TYPEID,
75 /* non-constant compound literals */
76 NIC_NCC,
77 /* a function call */
78 NIC_FUNC_CALL,
79 /* an increment */
80 NIC_INC,
81 /* an decrement */
82 NIC_DEC,
83 /* an array reference */
84 NIC_ARRAY_REF,
85 /* %<->%> */
86 NIC_ARROW,
87 /* %<.%> */
88 NIC_POINT,
89 /* the address of a label */
90 NIC_ADDR_LABEL,
91 /* %<*%> */
92 NIC_STAR,
93 /* %<&%> */
94 NIC_ADDR,
95 /* %<++%> */
96 NIC_PREINCREMENT,
97 /* %<--%> */
98 NIC_PREDECREMENT,
99 /* %<new%> */
100 NIC_NEW,
101 /* %<delete%> */
102 NIC_DEL,
103 /* calls to overloaded operators */
104 NIC_OVERLOADED,
105 /* an assignment */
106 NIC_ASSIGNMENT,
107 /* a comma operator */
108 NIC_COMMA,
109 /* a call to a constructor */
110 NIC_CONSTRUCTOR,
111 /* a transaction expression */
112 NIC_TRANSACTION
113 };
114
115 /* The various kinds of errors about name-lookup failing. */
116 enum name_lookup_error {
117 /* NULL */
118 NLE_NULL,
119 /* is not a type */
120 NLE_TYPE,
121 /* is not a class or namespace */
122 NLE_CXX98,
123 /* is not a class, namespace, or enumeration */
124 NLE_NOT_CXX98
125 };
126
127 /* The various kinds of required token */
128 enum required_token {
129 RT_NONE,
130 RT_SEMICOLON, /* ';' */
131 RT_OPEN_PAREN, /* '(' */
132 RT_CLOSE_BRACE, /* '}' */
133 RT_OPEN_BRACE, /* '{' */
134 RT_CLOSE_SQUARE, /* ']' */
135 RT_OPEN_SQUARE, /* '[' */
136 RT_COMMA, /* ',' */
137 RT_SCOPE, /* '::' */
138 RT_LESS, /* '<' */
139 RT_GREATER, /* '>' */
140 RT_EQ, /* '=' */
141 RT_ELLIPSIS, /* '...' */
142 RT_MULT, /* '*' */
143 RT_COMPL, /* '~' */
144 RT_COLON, /* ':' */
145 RT_COLON_SCOPE, /* ':' or '::' */
146 RT_CLOSE_PAREN, /* ')' */
147 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
148 RT_PRAGMA_EOL, /* end of line */
149 RT_NAME, /* identifier */
150
151 /* The type is CPP_KEYWORD */
152 RT_NEW, /* new */
153 RT_DELETE, /* delete */
154 RT_RETURN, /* return */
155 RT_WHILE, /* while */
156 RT_EXTERN, /* extern */
157 RT_STATIC_ASSERT, /* static_assert */
158 RT_DECLTYPE, /* decltype */
159 RT_OPERATOR, /* operator */
160 RT_CLASS, /* class */
161 RT_TEMPLATE, /* template */
162 RT_NAMESPACE, /* namespace */
163 RT_USING, /* using */
164 RT_ASM, /* asm */
165 RT_TRY, /* try */
166 RT_CATCH, /* catch */
167 RT_THROW, /* throw */
168 RT_AUTO, /* auto */
169 RT_LABEL, /* __label__ */
170 RT_AT_TRY, /* @try */
171 RT_AT_SYNCHRONIZED, /* @synchronized */
172 RT_AT_THROW, /* @throw */
173
174 RT_SELECT, /* selection-statement */
175 RT_ITERATION, /* iteration-statement */
176 RT_JUMP, /* jump-statement */
177 RT_CLASS_KEY, /* class-key */
178 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
179 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
180 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
181 RT_TRANSACTION_CANCEL, /* __transaction_cancel */
182
183 RT_CO_YIELD /* co_yield */
184 };
185
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
188
189 class type_id_in_expr_sentinel
190 {
191 cp_parser *parser;
192 bool saved;
193 public:
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195 : parser (parser),
196 saved (parser->in_type_id_in_expr_p)
197 { parser->in_type_id_in_expr_p = set; }
198 ~type_id_in_expr_sentinel ()
199 { parser->in_type_id_in_expr_p = saved; }
200 };
201
202 /* Prototypes. */
203
204 static cp_lexer *cp_lexer_new_main
205 (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209 (cp_lexer *);
210 static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer *, 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 cp_lexer *lexer;
617
618 c_common_no_more_pch ();
619
620 /* Allocate the memory. */
621 lexer = ggc_cleared_alloc<cp_lexer> ();
622
623 /* Initially we are not debugging. */
624 lexer->debugging_p = false;
625
626 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
627
628 /* Create the buffer. */
629 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
630
631 return lexer;
632 }
633
634
635 /* Create a new main C++ lexer, the lexer that gets tokens from the
636 preprocessor. */
637
638 static cp_lexer *
639 cp_lexer_new_main (void)
640 {
641 cp_lexer *lexer;
642 cp_token token;
643
644 /* It's possible that parsing the first pragma will load a PCH file,
645 which is a GC collection point. So we have to do that before
646 allocating any memory. */
647 cp_parser_initial_pragma (&token);
648
649 lexer = cp_lexer_alloc ();
650
651 /* Put the first token in the buffer. */
652 lexer->buffer->quick_push (token);
653
654 /* Get the remaining tokens from the preprocessor. */
655 while (token.type != CPP_EOF)
656 {
657 cp_lexer_get_preprocessor_token (lexer, &token);
658 vec_safe_push (lexer->buffer, token);
659 }
660
661 lexer->next_token = lexer->buffer->address ();
662 lexer->last_token = lexer->next_token
663 + lexer->buffer->length ()
664 - 1;
665
666 /* Subsequent preprocessor diagnostics should use compiler
667 diagnostic functions to get the compiler source location. */
668 done_lexing = true;
669
670 gcc_assert (!lexer->next_token->purged_p);
671 return lexer;
672 }
673
674 /* Create a new lexer whose token stream is primed with the tokens in
675 CACHE. When these tokens are exhausted, no new tokens will be read. */
676
677 static cp_lexer *
678 cp_lexer_new_from_tokens (cp_token_cache *cache)
679 {
680 cp_token *first = cache->first;
681 cp_token *last = cache->last;
682 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
683
684 /* We do not own the buffer. */
685 lexer->buffer = NULL;
686
687 /* Insert an EOF token. */
688 lexer->saved_type = last->type;
689 lexer->saved_keyword = last->keyword;
690 last->type = CPP_EOF;
691 last->keyword = RID_MAX;
692
693 lexer->next_token = first;
694 lexer->last_token = last;
695
696 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
697
698 /* Initially we are not debugging. */
699 lexer->debugging_p = false;
700
701 gcc_assert (!lexer->next_token->purged_p);
702 return lexer;
703 }
704
705 /* Frees all resources associated with LEXER. */
706
707 static void
708 cp_lexer_destroy (cp_lexer *lexer)
709 {
710 if (lexer->buffer)
711 vec_free (lexer->buffer);
712 else
713 {
714 /* Restore the token we overwrite with EOF. */
715 lexer->last_token->type = lexer->saved_type;
716 lexer->last_token->keyword = lexer->saved_keyword;
717 }
718 lexer->saved_tokens.release ();
719 ggc_free (lexer);
720 }
721
722 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
723 be used. The point of this flag is to help the compiler to fold away calls
724 to cp_lexer_debugging_p within this source file at compile time, when the
725 lexer is not being debugged. */
726
727 #define LEXER_DEBUGGING_ENABLED_P false
728
729 /* Returns nonzero if debugging information should be output. */
730
731 static inline bool
732 cp_lexer_debugging_p (cp_lexer *lexer)
733 {
734 if (!LEXER_DEBUGGING_ENABLED_P)
735 return false;
736
737 return lexer->debugging_p;
738 }
739
740
741 static inline cp_token_position
742 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
743 {
744 return lexer->next_token - previous_p;
745 }
746
747 static inline cp_token *
748 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
749 {
750 return pos;
751 }
752
753 static inline void
754 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
755 {
756 lexer->next_token = cp_lexer_token_at (lexer, pos);
757 }
758
759 static inline cp_token_position
760 cp_lexer_previous_token_position (cp_lexer *lexer)
761 {
762 return cp_lexer_token_position (lexer, true);
763 }
764
765 static inline cp_token *
766 cp_lexer_previous_token (cp_lexer *lexer)
767 {
768 cp_token_position tp = cp_lexer_previous_token_position (lexer);
769
770 /* Skip past purged tokens. */
771 while (tp->purged_p)
772 {
773 gcc_assert (tp != vec_safe_address (lexer->buffer));
774 tp--;
775 }
776
777 return cp_lexer_token_at (lexer, tp);
778 }
779
780 /* Same as above, but return NULL when the lexer doesn't own the token
781 buffer or if the next_token is at the start of the token
782 vector. */
783
784 static cp_token *
785 cp_lexer_safe_previous_token (cp_lexer *lexer)
786 {
787 if (lexer->buffer)
788 if (lexer->next_token != lexer->buffer->address ())
789 return cp_lexer_previous_token (lexer);
790
791 return NULL;
792 }
793
794 /* Overload for make_location, taking the lexer to mean the location of the
795 previous token. */
796
797 static inline location_t
798 make_location (location_t caret, location_t start, cp_lexer *lexer)
799 {
800 cp_token *t = cp_lexer_previous_token (lexer);
801 return make_location (caret, start, t->location);
802 }
803
804 /* nonzero if we are presently saving tokens. */
805
806 static inline int
807 cp_lexer_saving_tokens (const cp_lexer* lexer)
808 {
809 return lexer->saved_tokens.length () != 0;
810 }
811
812 /* Store the next token from the preprocessor in *TOKEN. Return true
813 if we reach EOF. If LEXER is NULL, assume we are handling an
814 initial #pragma pch_preprocess, and thus want the lexer to return
815 processed strings. */
816
817 static void
818 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
819 {
820 static int is_extern_c = 0;
821
822 /* Get a new token from the preprocessor. */
823 token->type
824 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
825 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
826 token->keyword = RID_MAX;
827 token->purged_p = false;
828 token->error_reported = false;
829 token->tree_check_p = false;
830
831 /* On some systems, some header files are surrounded by an
832 implicit extern "C" block. Set a flag in the token if it
833 comes from such a header. */
834 is_extern_c += pending_lang_change;
835 pending_lang_change = 0;
836 token->implicit_extern_c = is_extern_c > 0;
837
838 /* Check to see if this token is a keyword. */
839 if (token->type == CPP_NAME)
840 {
841 if (IDENTIFIER_KEYWORD_P (token->u.value))
842 {
843 /* Mark this token as a keyword. */
844 token->type = CPP_KEYWORD;
845 /* Record which keyword. */
846 token->keyword = C_RID_CODE (token->u.value);
847 }
848 else
849 {
850 if (warn_cxx11_compat
851 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
852 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
853 {
854 /* Warn about the C++0x keyword (but still treat it as
855 an identifier). */
856 warning_at (token->location, OPT_Wc__11_compat,
857 "identifier %qE is a keyword in C++11",
858 token->u.value);
859
860 /* Clear out the C_RID_CODE so we don't warn about this
861 particular identifier-turned-keyword again. */
862 C_SET_RID_CODE (token->u.value, RID_MAX);
863 }
864 if (warn_cxx20_compat
865 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX20
866 && C_RID_CODE (token->u.value) <= RID_LAST_CXX20)
867 {
868 /* Warn about the C++20 keyword (but still treat it as
869 an identifier). */
870 warning_at (token->location, OPT_Wc__20_compat,
871 "identifier %qE is a keyword in C++20",
872 token->u.value);
873
874 /* Clear out the C_RID_CODE so we don't warn about this
875 particular identifier-turned-keyword again. */
876 C_SET_RID_CODE (token->u.value, RID_MAX);
877 }
878
879 token->keyword = RID_MAX;
880 }
881 }
882 else if (token->type == CPP_AT_NAME)
883 {
884 /* This only happens in Objective-C++; it must be a keyword. */
885 token->type = CPP_KEYWORD;
886 switch (C_RID_CODE (token->u.value))
887 {
888 /* Replace 'class' with '@class', 'private' with '@private',
889 etc. This prevents confusion with the C++ keyword
890 'class', and makes the tokens consistent with other
891 Objective-C 'AT' keywords. For example '@class' is
892 reported as RID_AT_CLASS which is consistent with
893 '@synchronized', which is reported as
894 RID_AT_SYNCHRONIZED.
895 */
896 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
897 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
898 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
899 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
900 case RID_THROW: token->keyword = RID_AT_THROW; break;
901 case RID_TRY: token->keyword = RID_AT_TRY; break;
902 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
903 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
904 default: token->keyword = C_RID_CODE (token->u.value);
905 }
906 }
907 }
908
909 /* Update the globals input_location and the input file stack from TOKEN. */
910 static inline void
911 cp_lexer_set_source_position_from_token (cp_token *token)
912 {
913 input_location = token->location;
914 }
915
916 /* Update the globals input_location and the input file stack from LEXER. */
917 static inline void
918 cp_lexer_set_source_position (cp_lexer *lexer)
919 {
920 cp_token *token = cp_lexer_peek_token (lexer);
921 cp_lexer_set_source_position_from_token (token);
922 }
923
924 /* Return a pointer to the next token in the token stream, but do not
925 consume it. */
926
927 static inline cp_token *
928 cp_lexer_peek_token (cp_lexer *lexer)
929 {
930 if (cp_lexer_debugging_p (lexer))
931 {
932 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
933 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
934 putc ('\n', cp_lexer_debug_stream);
935 }
936 return lexer->next_token;
937 }
938
939 /* Return true if the next token has the indicated TYPE. */
940
941 static inline bool
942 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
943 {
944 return cp_lexer_peek_token (lexer)->type == type;
945 }
946
947 /* Return true if the next token does not have the indicated TYPE. */
948
949 static inline bool
950 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
951 {
952 return !cp_lexer_next_token_is (lexer, type);
953 }
954
955 /* Return true if the next token is the indicated KEYWORD. */
956
957 static inline bool
958 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
959 {
960 return cp_lexer_peek_token (lexer)->keyword == keyword;
961 }
962
963 static inline bool
964 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
965 {
966 return cp_lexer_peek_nth_token (lexer, n)->type == type;
967 }
968
969 static inline bool
970 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
971 {
972 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
973 }
974
975 /* Return true if KEYWORD can start a decl-specifier. */
976
977 bool
978 cp_keyword_starts_decl_specifier_p (enum rid keyword)
979 {
980 switch (keyword)
981 {
982 /* auto specifier: storage-class-specifier in C++,
983 simple-type-specifier in C++0x. */
984 case RID_AUTO:
985 /* Storage classes. */
986 case RID_REGISTER:
987 case RID_STATIC:
988 case RID_EXTERN:
989 case RID_MUTABLE:
990 case RID_THREAD:
991 /* Elaborated type specifiers. */
992 case RID_ENUM:
993 case RID_CLASS:
994 case RID_STRUCT:
995 case RID_UNION:
996 case RID_TYPENAME:
997 /* Simple type specifiers. */
998 case RID_CHAR:
999 case RID_CHAR8:
1000 case RID_CHAR16:
1001 case RID_CHAR32:
1002 case RID_WCHAR:
1003 case RID_BOOL:
1004 case RID_SHORT:
1005 case RID_INT:
1006 case RID_LONG:
1007 case RID_SIGNED:
1008 case RID_UNSIGNED:
1009 case RID_FLOAT:
1010 case RID_DOUBLE:
1011 case RID_VOID:
1012 /* GNU extensions. */
1013 case RID_ATTRIBUTE:
1014 case RID_TYPEOF:
1015 /* C++11 extensions. */
1016 case RID_DECLTYPE:
1017 case RID_UNDERLYING_TYPE:
1018 case RID_CONSTEXPR:
1019 /* C++20 extensions. */
1020 case RID_CONSTINIT:
1021 case RID_CONSTEVAL:
1022 return true;
1023
1024 default:
1025 if (keyword >= RID_FIRST_INT_N
1026 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
1027 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
1028 return true;
1029 return false;
1030 }
1031 }
1032
1033 /* Return true if the next token is a keyword for a decl-specifier. */
1034
1035 static bool
1036 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1037 {
1038 cp_token *token;
1039
1040 token = cp_lexer_peek_token (lexer);
1041 return cp_keyword_starts_decl_specifier_p (token->keyword);
1042 }
1043
1044 /* Returns TRUE iff the token T begins a decltype type. */
1045
1046 static bool
1047 token_is_decltype (cp_token *t)
1048 {
1049 return (t->keyword == RID_DECLTYPE
1050 || t->type == CPP_DECLTYPE);
1051 }
1052
1053 /* Returns TRUE iff the next token begins a decltype type. */
1054
1055 static bool
1056 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1057 {
1058 cp_token *t = cp_lexer_peek_token (lexer);
1059 return token_is_decltype (t);
1060 }
1061
1062 /* Called when processing a token with tree_check_value; perform or defer the
1063 associated checks and return the value. */
1064
1065 static tree
1066 saved_checks_value (struct tree_check *check_value)
1067 {
1068 /* Perform any access checks that were deferred. */
1069 vec<deferred_access_check, va_gc> *checks;
1070 deferred_access_check *chk;
1071 checks = check_value->checks;
1072 if (checks)
1073 {
1074 int i;
1075 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1076 perform_or_defer_access_check (chk->binfo,
1077 chk->decl,
1078 chk->diag_decl, tf_warning_or_error);
1079 }
1080 /* Return the stored value. */
1081 return check_value->value;
1082 }
1083
1084 /* Return a pointer to the Nth token in the token stream. If N is 1,
1085 then this is precisely equivalent to cp_lexer_peek_token (except
1086 that it is not inline). One would like to disallow that case, but
1087 there is one case (cp_parser_nth_token_starts_template_id) where
1088 the caller passes a variable for N and it might be 1. */
1089
1090 static cp_token *
1091 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1092 {
1093 cp_token *token;
1094
1095 /* N is 1-based, not zero-based. */
1096 gcc_assert (n > 0);
1097
1098 if (cp_lexer_debugging_p (lexer))
1099 fprintf (cp_lexer_debug_stream,
1100 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1101
1102 --n;
1103 token = lexer->next_token;
1104 while (n && token->type != CPP_EOF)
1105 {
1106 ++token;
1107 if (!token->purged_p)
1108 --n;
1109 }
1110
1111 if (cp_lexer_debugging_p (lexer))
1112 {
1113 cp_lexer_print_token (cp_lexer_debug_stream, token);
1114 putc ('\n', cp_lexer_debug_stream);
1115 }
1116
1117 return token;
1118 }
1119
1120 /* Return the next token, and advance the lexer's next_token pointer
1121 to point to the next non-purged token. */
1122
1123 static cp_token *
1124 cp_lexer_consume_token (cp_lexer* lexer)
1125 {
1126 cp_token *token = lexer->next_token;
1127
1128 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1129
1130 do
1131 {
1132 gcc_assert (token->type != CPP_EOF);
1133 lexer->next_token++;
1134 }
1135 while (lexer->next_token->purged_p);
1136
1137 cp_lexer_set_source_position_from_token (token);
1138
1139 /* Provide debugging output. */
1140 if (cp_lexer_debugging_p (lexer))
1141 {
1142 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1143 cp_lexer_print_token (cp_lexer_debug_stream, token);
1144 putc ('\n', cp_lexer_debug_stream);
1145 }
1146
1147 return token;
1148 }
1149
1150 /* Permanently remove the next token from the token stream, and
1151 advance the next_token pointer to refer to the next non-purged
1152 token. */
1153
1154 static void
1155 cp_lexer_purge_token (cp_lexer *lexer)
1156 {
1157 cp_token *tok = lexer->next_token;
1158
1159 gcc_assert (tok->type != CPP_EOF);
1160 tok->purged_p = true;
1161 tok->location = UNKNOWN_LOCATION;
1162 tok->u.value = NULL_TREE;
1163 tok->keyword = RID_MAX;
1164
1165 do
1166 tok++;
1167 while (tok->purged_p);
1168 lexer->next_token = tok;
1169 }
1170
1171 /* Permanently remove all tokens after TOK, up to, but not
1172 including, the token that will be returned next by
1173 cp_lexer_peek_token. */
1174
1175 static void
1176 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1177 {
1178 cp_token *peek = lexer->next_token;
1179
1180 gcc_assert (tok < peek);
1181
1182 for (tok++; tok != peek; tok++)
1183 {
1184 tok->purged_p = true;
1185 tok->location = UNKNOWN_LOCATION;
1186 tok->u.value = NULL_TREE;
1187 tok->keyword = RID_MAX;
1188 }
1189 }
1190
1191 /* Begin saving tokens. All tokens consumed after this point will be
1192 preserved. */
1193
1194 static void
1195 cp_lexer_save_tokens (cp_lexer* lexer)
1196 {
1197 /* Provide debugging output. */
1198 if (cp_lexer_debugging_p (lexer))
1199 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1200
1201 lexer->saved_tokens.safe_push (lexer->next_token);
1202 }
1203
1204 /* Commit to the portion of the token stream most recently saved. */
1205
1206 static void
1207 cp_lexer_commit_tokens (cp_lexer* lexer)
1208 {
1209 /* Provide debugging output. */
1210 if (cp_lexer_debugging_p (lexer))
1211 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1212
1213 lexer->saved_tokens.pop ();
1214 }
1215
1216 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1217 to the token stream. Stop saving tokens. */
1218
1219 static void
1220 cp_lexer_rollback_tokens (cp_lexer* lexer)
1221 {
1222 /* Provide debugging output. */
1223 if (cp_lexer_debugging_p (lexer))
1224 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1225
1226 lexer->next_token = lexer->saved_tokens.pop ();
1227 }
1228
1229 /* RAII wrapper around the above functions, with sanity checking. Creating
1230 a variable saves tokens, which are committed when the variable is
1231 destroyed unless they are explicitly rolled back by calling the rollback
1232 member function. */
1233
1234 struct saved_token_sentinel
1235 {
1236 cp_lexer *lexer;
1237 unsigned len;
1238 bool commit;
1239 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1240 {
1241 len = lexer->saved_tokens.length ();
1242 cp_lexer_save_tokens (lexer);
1243 }
1244 void rollback ()
1245 {
1246 cp_lexer_rollback_tokens (lexer);
1247 commit = false;
1248 }
1249 ~saved_token_sentinel()
1250 {
1251 if (commit)
1252 cp_lexer_commit_tokens (lexer);
1253 gcc_assert (lexer->saved_tokens.length () == len);
1254 }
1255 };
1256
1257 /* Print a representation of the TOKEN on the STREAM. */
1258
1259 static void
1260 cp_lexer_print_token (FILE * stream, cp_token *token)
1261 {
1262 /* We don't use cpp_type2name here because the parser defines
1263 a few tokens of its own. */
1264 static const char *const token_names[] = {
1265 /* cpplib-defined token types */
1266 #define OP(e, s) #e,
1267 #define TK(e, s) #e,
1268 TTYPE_TABLE
1269 #undef OP
1270 #undef TK
1271 /* C++ parser token types - see "Manifest constants", above. */
1272 "KEYWORD",
1273 "TEMPLATE_ID",
1274 "NESTED_NAME_SPECIFIER",
1275 };
1276
1277 /* For some tokens, print the associated data. */
1278 switch (token->type)
1279 {
1280 case CPP_KEYWORD:
1281 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1282 For example, `struct' is mapped to an INTEGER_CST. */
1283 if (!identifier_p (token->u.value))
1284 break;
1285 /* fall through */
1286 case CPP_NAME:
1287 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1288 break;
1289
1290 case CPP_STRING:
1291 case CPP_STRING16:
1292 case CPP_STRING32:
1293 case CPP_WSTRING:
1294 case CPP_UTF8STRING:
1295 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1296 break;
1297
1298 case CPP_NUMBER:
1299 print_generic_expr (stream, token->u.value);
1300 break;
1301
1302 default:
1303 /* If we have a name for the token, print it out. Otherwise, we
1304 simply give the numeric code. */
1305 if (token->type < ARRAY_SIZE(token_names))
1306 fputs (token_names[token->type], stream);
1307 else
1308 fprintf (stream, "[%d]", token->type);
1309 break;
1310 }
1311 }
1312
1313 DEBUG_FUNCTION void
1314 debug (cp_token &ref)
1315 {
1316 cp_lexer_print_token (stderr, &ref);
1317 fprintf (stderr, "\n");
1318 }
1319
1320 DEBUG_FUNCTION void
1321 debug (cp_token *ptr)
1322 {
1323 if (ptr)
1324 debug (*ptr);
1325 else
1326 fprintf (stderr, "<nil>\n");
1327 }
1328
1329
1330 /* Start emitting debugging information. */
1331
1332 static void
1333 cp_lexer_start_debugging (cp_lexer* lexer)
1334 {
1335 if (!LEXER_DEBUGGING_ENABLED_P)
1336 fatal_error (input_location,
1337 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1338
1339 lexer->debugging_p = true;
1340 cp_lexer_debug_stream = stderr;
1341 }
1342
1343 /* Stop emitting debugging information. */
1344
1345 static void
1346 cp_lexer_stop_debugging (cp_lexer* lexer)
1347 {
1348 if (!LEXER_DEBUGGING_ENABLED_P)
1349 fatal_error (input_location,
1350 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1351
1352 lexer->debugging_p = false;
1353 cp_lexer_debug_stream = NULL;
1354 }
1355
1356 /* Create a new cp_token_cache, representing a range of tokens. */
1357
1358 static cp_token_cache *
1359 cp_token_cache_new (cp_token *first, cp_token *last)
1360 {
1361 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1362 cache->first = first;
1363 cache->last = last;
1364 return cache;
1365 }
1366
1367 /* Diagnose if #pragma omp declare simd isn't followed immediately
1368 by function declaration or definition. */
1369
1370 static inline void
1371 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1372 {
1373 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1374 {
1375 error ("%<#pragma omp declare %s%> not immediately followed by "
1376 "function declaration or definition",
1377 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1378 parser->omp_declare_simd = NULL;
1379 }
1380 }
1381
1382 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1383 and put that into "omp declare simd" attribute. */
1384
1385 static inline void
1386 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1387 {
1388 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1389 {
1390 if (fndecl == error_mark_node)
1391 {
1392 parser->omp_declare_simd = NULL;
1393 return;
1394 }
1395 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1396 {
1397 cp_ensure_no_omp_declare_simd (parser);
1398 return;
1399 }
1400 }
1401 }
1402
1403 /* Diagnose if #pragma acc routine isn't followed immediately by function
1404 declaration or definition. */
1405
1406 static inline void
1407 cp_ensure_no_oacc_routine (cp_parser *parser)
1408 {
1409 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1410 {
1411 error_at (parser->oacc_routine->loc,
1412 "%<#pragma acc routine%> not immediately followed by "
1413 "function declaration or definition");
1414 parser->oacc_routine = NULL;
1415 }
1416 }
1417 \f
1418 /* Decl-specifiers. */
1419
1420 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1421
1422 static void
1423 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1424 {
1425 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1426 }
1427
1428 /* Declarators. */
1429
1430 /* Nothing other than the parser should be creating declarators;
1431 declarators are a semi-syntactic representation of C++ entities.
1432 Other parts of the front end that need to create entities (like
1433 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1434
1435 static cp_declarator *make_call_declarator
1436 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1437 static cp_declarator *make_array_declarator
1438 (cp_declarator *, tree);
1439 static cp_declarator *make_pointer_declarator
1440 (cp_cv_quals, cp_declarator *, tree);
1441 static cp_declarator *make_reference_declarator
1442 (cp_cv_quals, cp_declarator *, bool, tree);
1443 static cp_declarator *make_ptrmem_declarator
1444 (cp_cv_quals, tree, cp_declarator *, tree);
1445
1446 /* An erroneous declarator. */
1447 static cp_declarator *cp_error_declarator;
1448
1449 /* The obstack on which declarators and related data structures are
1450 allocated. */
1451 static struct obstack declarator_obstack;
1452
1453 /* Alloc BYTES from the declarator memory pool. */
1454
1455 static inline void *
1456 alloc_declarator (size_t bytes)
1457 {
1458 return obstack_alloc (&declarator_obstack, bytes);
1459 }
1460
1461 /* Allocate a declarator of the indicated KIND. Clear fields that are
1462 common to all declarators. */
1463
1464 static cp_declarator *
1465 make_declarator (cp_declarator_kind kind)
1466 {
1467 cp_declarator *declarator;
1468
1469 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1470 declarator->kind = kind;
1471 declarator->parenthesized = UNKNOWN_LOCATION;
1472 declarator->attributes = NULL_TREE;
1473 declarator->std_attributes = NULL_TREE;
1474 declarator->declarator = NULL;
1475 declarator->parameter_pack_p = false;
1476 declarator->id_loc = UNKNOWN_LOCATION;
1477
1478 return declarator;
1479 }
1480
1481 /* Make a declarator for a generalized identifier. If
1482 QUALIFYING_SCOPE is non-NULL, the identifier is
1483 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1484 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1485 is, if any. */
1486
1487 static cp_declarator *
1488 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1489 special_function_kind sfk, location_t id_location)
1490 {
1491 cp_declarator *declarator;
1492
1493 /* It is valid to write:
1494
1495 class C { void f(); };
1496 typedef C D;
1497 void D::f();
1498
1499 The standard is not clear about whether `typedef const C D' is
1500 legal; as of 2002-09-15 the committee is considering that
1501 question. EDG 3.0 allows that syntax. Therefore, we do as
1502 well. */
1503 if (qualifying_scope && TYPE_P (qualifying_scope))
1504 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1505
1506 gcc_assert (identifier_p (unqualified_name)
1507 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1508 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1509
1510 declarator = make_declarator (cdk_id);
1511 declarator->u.id.qualifying_scope = qualifying_scope;
1512 declarator->u.id.unqualified_name = unqualified_name;
1513 declarator->u.id.sfk = sfk;
1514 declarator->id_loc = id_location;
1515
1516 return declarator;
1517 }
1518
1519 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1520 of modifiers such as const or volatile to apply to the pointer
1521 type, represented as identifiers. ATTRIBUTES represent the attributes that
1522 appertain to the pointer or reference. */
1523
1524 cp_declarator *
1525 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1526 tree attributes)
1527 {
1528 cp_declarator *declarator;
1529
1530 declarator = make_declarator (cdk_pointer);
1531 declarator->declarator = target;
1532 declarator->u.pointer.qualifiers = cv_qualifiers;
1533 declarator->u.pointer.class_type = NULL_TREE;
1534 if (target)
1535 {
1536 declarator->id_loc = target->id_loc;
1537 declarator->parameter_pack_p = target->parameter_pack_p;
1538 target->parameter_pack_p = false;
1539 }
1540 else
1541 declarator->parameter_pack_p = false;
1542
1543 declarator->std_attributes = attributes;
1544
1545 return declarator;
1546 }
1547
1548 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1549 represent the attributes that appertain to the pointer or
1550 reference. */
1551
1552 cp_declarator *
1553 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1554 bool rvalue_ref, tree attributes)
1555 {
1556 cp_declarator *declarator;
1557
1558 declarator = make_declarator (cdk_reference);
1559 declarator->declarator = target;
1560 declarator->u.reference.qualifiers = cv_qualifiers;
1561 declarator->u.reference.rvalue_ref = rvalue_ref;
1562 if (target)
1563 {
1564 declarator->id_loc = target->id_loc;
1565 declarator->parameter_pack_p = target->parameter_pack_p;
1566 target->parameter_pack_p = false;
1567 }
1568 else
1569 declarator->parameter_pack_p = false;
1570
1571 declarator->std_attributes = attributes;
1572
1573 return declarator;
1574 }
1575
1576 /* Like make_pointer_declarator -- but for a pointer to a non-static
1577 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1578 appertain to the pointer or reference. */
1579
1580 cp_declarator *
1581 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1582 cp_declarator *pointee,
1583 tree attributes)
1584 {
1585 cp_declarator *declarator;
1586
1587 declarator = make_declarator (cdk_ptrmem);
1588 declarator->declarator = pointee;
1589 declarator->u.pointer.qualifiers = cv_qualifiers;
1590 declarator->u.pointer.class_type = class_type;
1591
1592 if (pointee)
1593 {
1594 declarator->parameter_pack_p = pointee->parameter_pack_p;
1595 pointee->parameter_pack_p = false;
1596 }
1597 else
1598 declarator->parameter_pack_p = false;
1599
1600 declarator->std_attributes = attributes;
1601
1602 return declarator;
1603 }
1604
1605 /* Make a declarator for the function given by TARGET, with the
1606 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1607 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1608 indicates what exceptions can be thrown. */
1609
1610 cp_declarator *
1611 make_call_declarator (cp_declarator *target,
1612 tree parms,
1613 cp_cv_quals cv_qualifiers,
1614 cp_virt_specifiers virt_specifiers,
1615 cp_ref_qualifier ref_qualifier,
1616 tree tx_qualifier,
1617 tree exception_specification,
1618 tree late_return_type,
1619 tree requires_clause)
1620 {
1621 cp_declarator *declarator;
1622
1623 declarator = make_declarator (cdk_function);
1624 declarator->declarator = target;
1625 declarator->u.function.parameters = parms;
1626 declarator->u.function.qualifiers = cv_qualifiers;
1627 declarator->u.function.virt_specifiers = virt_specifiers;
1628 declarator->u.function.ref_qualifier = ref_qualifier;
1629 declarator->u.function.tx_qualifier = tx_qualifier;
1630 declarator->u.function.exception_specification = exception_specification;
1631 declarator->u.function.late_return_type = late_return_type;
1632 declarator->u.function.requires_clause = requires_clause;
1633 if (target)
1634 {
1635 declarator->id_loc = target->id_loc;
1636 declarator->parameter_pack_p = target->parameter_pack_p;
1637 target->parameter_pack_p = false;
1638 }
1639 else
1640 declarator->parameter_pack_p = false;
1641
1642 return declarator;
1643 }
1644
1645 /* Make a declarator for an array of BOUNDS elements, each of which is
1646 defined by ELEMENT. */
1647
1648 cp_declarator *
1649 make_array_declarator (cp_declarator *element, tree bounds)
1650 {
1651 cp_declarator *declarator;
1652
1653 declarator = make_declarator (cdk_array);
1654 declarator->declarator = element;
1655 declarator->u.array.bounds = bounds;
1656 if (element)
1657 {
1658 declarator->id_loc = element->id_loc;
1659 declarator->parameter_pack_p = element->parameter_pack_p;
1660 element->parameter_pack_p = false;
1661 }
1662 else
1663 declarator->parameter_pack_p = false;
1664
1665 return declarator;
1666 }
1667
1668 /* Determine whether the declarator we've seen so far can be a
1669 parameter pack, when followed by an ellipsis. */
1670 static bool
1671 declarator_can_be_parameter_pack (cp_declarator *declarator)
1672 {
1673 if (declarator && declarator->parameter_pack_p)
1674 /* We already saw an ellipsis. */
1675 return false;
1676
1677 /* Search for a declarator name, or any other declarator that goes
1678 after the point where the ellipsis could appear in a parameter
1679 pack. If we find any of these, then this declarator cannot be
1680 made into a parameter pack. */
1681 bool found = false;
1682 while (declarator && !found)
1683 {
1684 switch ((int)declarator->kind)
1685 {
1686 case cdk_id:
1687 case cdk_array:
1688 case cdk_decomp:
1689 found = true;
1690 break;
1691
1692 case cdk_error:
1693 return true;
1694
1695 default:
1696 declarator = declarator->declarator;
1697 break;
1698 }
1699 }
1700
1701 return !found;
1702 }
1703
1704 cp_parameter_declarator *no_parameters;
1705
1706 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1707 DECLARATOR and DEFAULT_ARGUMENT. */
1708
1709 cp_parameter_declarator *
1710 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1711 cp_declarator *declarator,
1712 tree default_argument,
1713 location_t loc,
1714 bool template_parameter_pack_p = false)
1715 {
1716 cp_parameter_declarator *parameter;
1717
1718 parameter = ((cp_parameter_declarator *)
1719 alloc_declarator (sizeof (cp_parameter_declarator)));
1720 parameter->next = NULL;
1721 if (decl_specifiers)
1722 parameter->decl_specifiers = *decl_specifiers;
1723 else
1724 clear_decl_specs (&parameter->decl_specifiers);
1725 parameter->declarator = declarator;
1726 parameter->default_argument = default_argument;
1727 parameter->template_parameter_pack_p = template_parameter_pack_p;
1728 parameter->loc = loc;
1729
1730 return parameter;
1731 }
1732
1733 /* Returns true iff DECLARATOR is a declaration for a function. */
1734
1735 static bool
1736 function_declarator_p (const cp_declarator *declarator)
1737 {
1738 while (declarator)
1739 {
1740 if (declarator->kind == cdk_function
1741 && declarator->declarator->kind == cdk_id)
1742 return true;
1743 if (declarator->kind == cdk_id
1744 || declarator->kind == cdk_decomp
1745 || declarator->kind == cdk_error)
1746 return false;
1747 declarator = declarator->declarator;
1748 }
1749 return false;
1750 }
1751
1752 /* The parser. */
1753
1754 /* Overview
1755 --------
1756
1757 A cp_parser parses the token stream as specified by the C++
1758 grammar. Its job is purely parsing, not semantic analysis. For
1759 example, the parser breaks the token stream into declarators,
1760 expressions, statements, and other similar syntactic constructs.
1761 It does not check that the types of the expressions on either side
1762 of an assignment-statement are compatible, or that a function is
1763 not declared with a parameter of type `void'.
1764
1765 The parser invokes routines elsewhere in the compiler to perform
1766 semantic analysis and to build up the abstract syntax tree for the
1767 code processed.
1768
1769 The parser (and the template instantiation code, which is, in a
1770 way, a close relative of parsing) are the only parts of the
1771 compiler that should be calling push_scope and pop_scope, or
1772 related functions. The parser (and template instantiation code)
1773 keeps track of what scope is presently active; everything else
1774 should simply honor that. (The code that generates static
1775 initializers may also need to set the scope, in order to check
1776 access control correctly when emitting the initializers.)
1777
1778 Methodology
1779 -----------
1780
1781 The parser is of the standard recursive-descent variety. Upcoming
1782 tokens in the token stream are examined in order to determine which
1783 production to use when parsing a non-terminal. Some C++ constructs
1784 require arbitrary look ahead to disambiguate. For example, it is
1785 impossible, in the general case, to tell whether a statement is an
1786 expression or declaration without scanning the entire statement.
1787 Therefore, the parser is capable of "parsing tentatively." When the
1788 parser is not sure what construct comes next, it enters this mode.
1789 Then, while we attempt to parse the construct, the parser queues up
1790 error messages, rather than issuing them immediately, and saves the
1791 tokens it consumes. If the construct is parsed successfully, the
1792 parser "commits", i.e., it issues any queued error messages and
1793 the tokens that were being preserved are permanently discarded.
1794 If, however, the construct is not parsed successfully, the parser
1795 rolls back its state completely so that it can resume parsing using
1796 a different alternative.
1797
1798 Future Improvements
1799 -------------------
1800
1801 The performance of the parser could probably be improved substantially.
1802 We could often eliminate the need to parse tentatively by looking ahead
1803 a little bit. In some places, this approach might not entirely eliminate
1804 the need to parse tentatively, but it might still speed up the average
1805 case. */
1806
1807 /* Flags that are passed to some parsing functions. These values can
1808 be bitwise-ored together. */
1809
1810 enum
1811 {
1812 /* No flags. */
1813 CP_PARSER_FLAGS_NONE = 0x0,
1814 /* The construct is optional. If it is not present, then no error
1815 should be issued. */
1816 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1817 /* When parsing a type-specifier, treat user-defined type-names
1818 as non-type identifiers. */
1819 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1820 /* When parsing a type-specifier, do not try to parse a class-specifier
1821 or enum-specifier. */
1822 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1823 /* When parsing a decl-specifier-seq, only allow type-specifier or
1824 constexpr. */
1825 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1826 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
1827 for C++20 consteval. */
1828 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1829 /* When parsing a decl-specifier-seq, allow missing typename. */
1830 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
1831 /* When parsing of the noexcept-specifier should be delayed. */
1832 CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
1833 /* When parsing a consteval declarator. */
1834 CP_PARSER_FLAGS_CONSTEVAL = 0x80
1835 };
1836
1837 /* This type is used for parameters and variables which hold
1838 combinations of the above flags. */
1839 typedef int cp_parser_flags;
1840
1841 /* The different kinds of declarators we want to parse. */
1842
1843 enum cp_parser_declarator_kind
1844 {
1845 /* We want an abstract declarator. */
1846 CP_PARSER_DECLARATOR_ABSTRACT,
1847 /* We want a named declarator. */
1848 CP_PARSER_DECLARATOR_NAMED,
1849 /* We don't mind, but the name must be an unqualified-id. */
1850 CP_PARSER_DECLARATOR_EITHER
1851 };
1852
1853 /* The precedence values used to parse binary expressions. The minimum value
1854 of PREC must be 1, because zero is reserved to quickly discriminate
1855 binary operators from other tokens. */
1856
1857 enum cp_parser_prec
1858 {
1859 PREC_NOT_OPERATOR,
1860 PREC_LOGICAL_OR_EXPRESSION,
1861 PREC_LOGICAL_AND_EXPRESSION,
1862 PREC_INCLUSIVE_OR_EXPRESSION,
1863 PREC_EXCLUSIVE_OR_EXPRESSION,
1864 PREC_AND_EXPRESSION,
1865 PREC_EQUALITY_EXPRESSION,
1866 PREC_RELATIONAL_EXPRESSION,
1867 PREC_SPACESHIP_EXPRESSION,
1868 PREC_SHIFT_EXPRESSION,
1869 PREC_ADDITIVE_EXPRESSION,
1870 PREC_MULTIPLICATIVE_EXPRESSION,
1871 PREC_PM_EXPRESSION,
1872 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1873 };
1874
1875 /* A mapping from a token type to a corresponding tree node type, with a
1876 precedence value. */
1877
1878 struct cp_parser_binary_operations_map_node
1879 {
1880 /* The token type. */
1881 enum cpp_ttype token_type;
1882 /* The corresponding tree code. */
1883 enum tree_code tree_type;
1884 /* The precedence of this operator. */
1885 enum cp_parser_prec prec;
1886 };
1887
1888 struct cp_parser_expression_stack_entry
1889 {
1890 /* Left hand side of the binary operation we are currently
1891 parsing. */
1892 cp_expr lhs;
1893 /* Original tree code for left hand side, if it was a binary
1894 expression itself (used for -Wparentheses). */
1895 enum tree_code lhs_type;
1896 /* Tree code for the binary operation we are parsing. */
1897 enum tree_code tree_type;
1898 /* Precedence of the binary operation we are parsing. */
1899 enum cp_parser_prec prec;
1900 /* Location of the binary operation we are parsing. */
1901 location_t loc;
1902 };
1903
1904 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1905 entries because precedence levels on the stack are monotonically
1906 increasing. */
1907 typedef struct cp_parser_expression_stack_entry
1908 cp_parser_expression_stack[NUM_PREC_VALUES];
1909
1910 /* Prototypes. */
1911
1912 /* Constructors and destructors. */
1913
1914 static cp_parser_context *cp_parser_context_new
1915 (cp_parser_context *);
1916
1917 /* Class variables. */
1918
1919 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1920
1921 /* The operator-precedence table used by cp_parser_binary_expression.
1922 Transformed into an associative array (binops_by_token) by
1923 cp_parser_new. */
1924
1925 static const cp_parser_binary_operations_map_node binops[] = {
1926 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1927 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1928
1929 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1930 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1931 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1932
1933 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1934 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1935
1936 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1937 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1938
1939 { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
1940
1941 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1942 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1943 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1944 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1945
1946 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1947 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1948
1949 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1950
1951 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1952
1953 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1954
1955 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1956
1957 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1958 };
1959
1960 /* The same as binops, but initialized by cp_parser_new so that
1961 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1962 for speed. */
1963 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1964
1965 /* Constructors and destructors. */
1966
1967 /* Construct a new context. The context below this one on the stack
1968 is given by NEXT. */
1969
1970 static cp_parser_context *
1971 cp_parser_context_new (cp_parser_context* next)
1972 {
1973 cp_parser_context *context;
1974
1975 /* Allocate the storage. */
1976 if (cp_parser_context_free_list != NULL)
1977 {
1978 /* Pull the first entry from the free list. */
1979 context = cp_parser_context_free_list;
1980 cp_parser_context_free_list = context->next;
1981 memset (context, 0, sizeof (*context));
1982 }
1983 else
1984 context = ggc_cleared_alloc<cp_parser_context> ();
1985
1986 /* No errors have occurred yet in this context. */
1987 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1988 /* If this is not the bottommost context, copy information that we
1989 need from the previous context. */
1990 if (next)
1991 {
1992 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1993 expression, then we are parsing one in this context, too. */
1994 context->object_type = next->object_type;
1995 /* Thread the stack. */
1996 context->next = next;
1997 }
1998
1999 return context;
2000 }
2001
2002 /* Managing the unparsed function queues. */
2003
2004 #define unparsed_funs_with_default_args \
2005 parser->unparsed_queues->last ().funs_with_default_args
2006 #define unparsed_funs_with_definitions \
2007 parser->unparsed_queues->last ().funs_with_definitions
2008 #define unparsed_nsdmis \
2009 parser->unparsed_queues->last ().nsdmis
2010 #define unparsed_noexcepts \
2011 parser->unparsed_queues->last ().noexcepts
2012
2013 static void
2014 push_unparsed_function_queues (cp_parser *parser)
2015 {
2016 cp_unparsed_functions_entry e = { NULL, make_tree_vector (), NULL, NULL };
2017 vec_safe_push (parser->unparsed_queues, e);
2018 }
2019
2020 static void
2021 pop_unparsed_function_queues (cp_parser *parser)
2022 {
2023 release_tree_vector (unparsed_funs_with_definitions);
2024 parser->unparsed_queues->pop ();
2025 }
2026
2027 /* Prototypes. */
2028
2029 /* Constructors and destructors. */
2030
2031 static cp_parser *cp_parser_new
2032 (void);
2033
2034 /* Routines to parse various constructs.
2035
2036 Those that return `tree' will return the error_mark_node (rather
2037 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2038 Sometimes, they will return an ordinary node if error-recovery was
2039 attempted, even though a parse error occurred. So, to check
2040 whether or not a parse error occurred, you should always use
2041 cp_parser_error_occurred. If the construct is optional (indicated
2042 either by an `_opt' in the name of the function that does the
2043 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2044 the construct is not present. */
2045
2046 /* Lexical conventions [gram.lex] */
2047
2048 static cp_expr cp_parser_identifier
2049 (cp_parser *);
2050 static cp_expr cp_parser_string_literal
2051 (cp_parser *, bool, bool, bool);
2052 static cp_expr cp_parser_userdef_char_literal
2053 (cp_parser *);
2054 static tree cp_parser_userdef_string_literal
2055 (tree);
2056 static cp_expr cp_parser_userdef_numeric_literal
2057 (cp_parser *);
2058
2059 /* Basic concepts [gram.basic] */
2060
2061 static void cp_parser_translation_unit (cp_parser *);
2062
2063 /* Expressions [gram.expr] */
2064
2065 static cp_expr cp_parser_primary_expression
2066 (cp_parser *, bool, bool, bool, cp_id_kind *);
2067 static cp_expr cp_parser_id_expression
2068 (cp_parser *, bool, bool, bool *, bool, bool);
2069 static cp_expr cp_parser_unqualified_id
2070 (cp_parser *, bool, bool, bool, bool);
2071 static tree cp_parser_nested_name_specifier_opt
2072 (cp_parser *, bool, bool, bool, bool, bool = false);
2073 static tree cp_parser_nested_name_specifier
2074 (cp_parser *, bool, bool, bool, bool);
2075 static tree cp_parser_qualifying_entity
2076 (cp_parser *, bool, bool, bool, bool, bool);
2077 static cp_expr cp_parser_postfix_expression
2078 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2079 static tree cp_parser_postfix_open_square_expression
2080 (cp_parser *, tree, bool, bool);
2081 static tree cp_parser_postfix_dot_deref_expression
2082 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2083 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2084 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2085 bool = false);
2086 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2087 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2088 static void cp_parser_pseudo_destructor_name
2089 (cp_parser *, tree, tree *, tree *);
2090 static cp_expr cp_parser_unary_expression
2091 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2092 static enum tree_code cp_parser_unary_operator
2093 (cp_token *);
2094 static tree cp_parser_has_attribute_expression
2095 (cp_parser *);
2096 static tree cp_parser_new_expression
2097 (cp_parser *);
2098 static vec<tree, va_gc> *cp_parser_new_placement
2099 (cp_parser *);
2100 static tree cp_parser_new_type_id
2101 (cp_parser *, tree *);
2102 static cp_declarator *cp_parser_new_declarator_opt
2103 (cp_parser *);
2104 static cp_declarator *cp_parser_direct_new_declarator
2105 (cp_parser *);
2106 static vec<tree, va_gc> *cp_parser_new_initializer
2107 (cp_parser *);
2108 static tree cp_parser_delete_expression
2109 (cp_parser *);
2110 static cp_expr cp_parser_cast_expression
2111 (cp_parser *, bool, bool, bool, cp_id_kind *);
2112 static cp_expr cp_parser_binary_expression
2113 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2114 static tree cp_parser_question_colon_clause
2115 (cp_parser *, cp_expr);
2116 static cp_expr cp_parser_assignment_expression
2117 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2118 static enum tree_code cp_parser_assignment_operator_opt
2119 (cp_parser *);
2120 static cp_expr cp_parser_expression
2121 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2122 static cp_expr cp_parser_constant_expression
2123 (cp_parser *, bool = false, bool * = NULL, bool = false);
2124 static cp_expr cp_parser_builtin_offsetof
2125 (cp_parser *);
2126 static cp_expr cp_parser_lambda_expression
2127 (cp_parser *);
2128 static void cp_parser_lambda_introducer
2129 (cp_parser *, tree);
2130 static bool cp_parser_lambda_declarator_opt
2131 (cp_parser *, tree);
2132 static void cp_parser_lambda_body
2133 (cp_parser *, tree);
2134
2135 /* Statements [gram.stmt.stmt] */
2136
2137 static void cp_parser_statement
2138 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2139 static void cp_parser_label_for_labeled_statement
2140 (cp_parser *, tree);
2141 static tree cp_parser_expression_statement
2142 (cp_parser *, tree);
2143 static tree cp_parser_compound_statement
2144 (cp_parser *, tree, int, bool);
2145 static void cp_parser_statement_seq_opt
2146 (cp_parser *, tree);
2147 static tree cp_parser_selection_statement
2148 (cp_parser *, bool *, vec<tree> *);
2149 static tree cp_parser_condition
2150 (cp_parser *);
2151 static tree cp_parser_iteration_statement
2152 (cp_parser *, bool *, bool, unsigned short);
2153 static bool cp_parser_init_statement
2154 (cp_parser *, tree *decl);
2155 static tree cp_parser_for
2156 (cp_parser *, bool, unsigned short);
2157 static tree cp_parser_c_for
2158 (cp_parser *, tree, tree, bool, unsigned short);
2159 static tree cp_parser_range_for
2160 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2161 static void do_range_for_auto_deduction
2162 (tree, tree);
2163 static tree cp_parser_perform_range_for_lookup
2164 (tree, tree *, tree *);
2165 static tree cp_parser_range_for_member_function
2166 (tree, tree);
2167 static tree cp_parser_jump_statement
2168 (cp_parser *);
2169 static void cp_parser_declaration_statement
2170 (cp_parser *);
2171
2172 static tree cp_parser_implicitly_scoped_statement
2173 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2174 static void cp_parser_already_scoped_statement
2175 (cp_parser *, bool *, const token_indent_info &);
2176
2177 /* Declarations [gram.dcl.dcl] */
2178
2179 static void cp_parser_declaration_seq_opt
2180 (cp_parser *);
2181 static void cp_parser_declaration
2182 (cp_parser *);
2183 static void cp_parser_toplevel_declaration
2184 (cp_parser *);
2185 static void cp_parser_block_declaration
2186 (cp_parser *, bool);
2187 static void cp_parser_simple_declaration
2188 (cp_parser *, bool, tree *);
2189 static void cp_parser_decl_specifier_seq
2190 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2191 static tree cp_parser_storage_class_specifier_opt
2192 (cp_parser *);
2193 static tree cp_parser_function_specifier_opt
2194 (cp_parser *, cp_decl_specifier_seq *);
2195 static tree cp_parser_type_specifier
2196 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2197 int *, bool *);
2198 static tree cp_parser_simple_type_specifier
2199 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2200 static tree cp_parser_placeholder_type_specifier
2201 (cp_parser *, location_t, tree, bool);
2202 static tree cp_parser_type_name
2203 (cp_parser *, bool);
2204 static tree cp_parser_nonclass_name
2205 (cp_parser* parser);
2206 static tree cp_parser_elaborated_type_specifier
2207 (cp_parser *, bool, bool);
2208 static tree cp_parser_enum_specifier
2209 (cp_parser *);
2210 static void cp_parser_enumerator_list
2211 (cp_parser *, tree);
2212 static void cp_parser_enumerator_definition
2213 (cp_parser *, tree);
2214 static tree cp_parser_namespace_name
2215 (cp_parser *);
2216 static void cp_parser_namespace_definition
2217 (cp_parser *);
2218 static void cp_parser_namespace_body
2219 (cp_parser *);
2220 static tree cp_parser_qualified_namespace_specifier
2221 (cp_parser *);
2222 static void cp_parser_namespace_alias_definition
2223 (cp_parser *);
2224 static bool cp_parser_using_declaration
2225 (cp_parser *, bool);
2226 static void cp_parser_using_directive
2227 (cp_parser *);
2228 static tree cp_parser_alias_declaration
2229 (cp_parser *);
2230 static void cp_parser_asm_definition
2231 (cp_parser *);
2232 static void cp_parser_linkage_specification
2233 (cp_parser *);
2234 static void cp_parser_static_assert
2235 (cp_parser *, bool);
2236 static tree cp_parser_decltype
2237 (cp_parser *);
2238 static tree cp_parser_decomposition_declaration
2239 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2240
2241 /* Declarators [gram.dcl.decl] */
2242
2243 static tree cp_parser_init_declarator
2244 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2245 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2246 location_t *, tree *);
2247 static cp_declarator *cp_parser_declarator
2248 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2249 bool, bool, bool);
2250 static cp_declarator *cp_parser_direct_declarator
2251 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2252 bool);
2253 static enum tree_code cp_parser_ptr_operator
2254 (cp_parser *, tree *, cp_cv_quals *, tree *);
2255 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2256 (cp_parser *);
2257 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2258 (cp_parser *);
2259 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2260 (cp_parser *);
2261 static tree cp_parser_tx_qualifier_opt
2262 (cp_parser *);
2263 static tree cp_parser_late_return_type_opt
2264 (cp_parser *, cp_declarator *, tree &);
2265 static tree cp_parser_declarator_id
2266 (cp_parser *, bool);
2267 static tree cp_parser_type_id
2268 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2269 static tree cp_parser_template_type_arg
2270 (cp_parser *);
2271 static tree cp_parser_trailing_type_id (cp_parser *);
2272 static tree cp_parser_type_id_1
2273 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2274 static void cp_parser_type_specifier_seq
2275 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2276 static tree cp_parser_parameter_declaration_clause
2277 (cp_parser *, cp_parser_flags);
2278 static tree cp_parser_parameter_declaration_list
2279 (cp_parser *, cp_parser_flags);
2280 static cp_parameter_declarator *cp_parser_parameter_declaration
2281 (cp_parser *, cp_parser_flags, bool, bool *);
2282 static tree cp_parser_default_argument
2283 (cp_parser *, bool);
2284 static void cp_parser_function_body
2285 (cp_parser *, bool);
2286 static tree cp_parser_initializer
2287 (cp_parser *, bool *, bool *, bool = false);
2288 static cp_expr cp_parser_initializer_clause
2289 (cp_parser *, bool *);
2290 static cp_expr cp_parser_braced_list
2291 (cp_parser*, bool*);
2292 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2293 (cp_parser *, bool *, bool *);
2294
2295 static void cp_parser_ctor_initializer_opt_and_function_body
2296 (cp_parser *, bool);
2297
2298 static tree cp_parser_late_parsing_omp_declare_simd
2299 (cp_parser *, tree);
2300
2301 static tree cp_parser_late_parsing_oacc_routine
2302 (cp_parser *, tree);
2303
2304 static tree synthesize_implicit_template_parm
2305 (cp_parser *, tree);
2306 static tree finish_fully_implicit_template
2307 (cp_parser *, tree);
2308 static void abort_fully_implicit_template
2309 (cp_parser *);
2310
2311 /* Classes [gram.class] */
2312
2313 static tree cp_parser_class_name
2314 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2315 static tree cp_parser_class_specifier
2316 (cp_parser *);
2317 static tree cp_parser_class_head
2318 (cp_parser *, bool *);
2319 static enum tag_types cp_parser_class_key
2320 (cp_parser *);
2321 static void cp_parser_type_parameter_key
2322 (cp_parser* parser);
2323 static void cp_parser_member_specification_opt
2324 (cp_parser *);
2325 static void cp_parser_member_declaration
2326 (cp_parser *);
2327 static tree cp_parser_pure_specifier
2328 (cp_parser *);
2329 static tree cp_parser_constant_initializer
2330 (cp_parser *);
2331
2332 /* Derived classes [gram.class.derived] */
2333
2334 static tree cp_parser_base_clause
2335 (cp_parser *);
2336 static tree cp_parser_base_specifier
2337 (cp_parser *);
2338
2339 /* Special member functions [gram.special] */
2340
2341 static tree cp_parser_conversion_function_id
2342 (cp_parser *);
2343 static tree cp_parser_conversion_type_id
2344 (cp_parser *);
2345 static cp_declarator *cp_parser_conversion_declarator_opt
2346 (cp_parser *);
2347 static void cp_parser_ctor_initializer_opt
2348 (cp_parser *);
2349 static void cp_parser_mem_initializer_list
2350 (cp_parser *);
2351 static tree cp_parser_mem_initializer
2352 (cp_parser *);
2353 static tree cp_parser_mem_initializer_id
2354 (cp_parser *);
2355
2356 /* Overloading [gram.over] */
2357
2358 static cp_expr cp_parser_operator_function_id
2359 (cp_parser *);
2360 static cp_expr cp_parser_operator
2361 (cp_parser *, location_t);
2362
2363 /* Templates [gram.temp] */
2364
2365 static void cp_parser_template_declaration
2366 (cp_parser *, bool);
2367 static tree cp_parser_template_parameter_list
2368 (cp_parser *);
2369 static tree cp_parser_template_parameter
2370 (cp_parser *, bool *, bool *);
2371 static tree cp_parser_type_parameter
2372 (cp_parser *, bool *);
2373 static tree cp_parser_template_id
2374 (cp_parser *, bool, bool, enum tag_types, bool);
2375 static tree cp_parser_template_id_expr
2376 (cp_parser *, bool, bool, bool);
2377 static tree cp_parser_template_name
2378 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2379 static tree cp_parser_template_argument_list
2380 (cp_parser *);
2381 static tree cp_parser_template_argument
2382 (cp_parser *);
2383 static void cp_parser_explicit_instantiation
2384 (cp_parser *);
2385 static void cp_parser_explicit_specialization
2386 (cp_parser *);
2387
2388 /* Exception handling [gram.except] */
2389
2390 static tree cp_parser_try_block
2391 (cp_parser *);
2392 static void cp_parser_function_try_block
2393 (cp_parser *);
2394 static void cp_parser_handler_seq
2395 (cp_parser *);
2396 static void cp_parser_handler
2397 (cp_parser *);
2398 static tree cp_parser_exception_declaration
2399 (cp_parser *);
2400 static tree cp_parser_throw_expression
2401 (cp_parser *);
2402 static tree cp_parser_exception_specification_opt
2403 (cp_parser *, cp_parser_flags);
2404 static tree cp_parser_type_id_list
2405 (cp_parser *);
2406 static tree cp_parser_noexcept_specification_opt
2407 (cp_parser *, cp_parser_flags, bool, bool *, bool);
2408
2409 /* GNU Extensions */
2410
2411 static tree cp_parser_asm_specification_opt
2412 (cp_parser *);
2413 static tree cp_parser_asm_operand_list
2414 (cp_parser *);
2415 static tree cp_parser_asm_clobber_list
2416 (cp_parser *);
2417 static tree cp_parser_asm_label_list
2418 (cp_parser *);
2419 static bool cp_next_tokens_can_be_attribute_p
2420 (cp_parser *);
2421 static bool cp_next_tokens_can_be_gnu_attribute_p
2422 (cp_parser *);
2423 static bool cp_next_tokens_can_be_std_attribute_p
2424 (cp_parser *);
2425 static bool cp_nth_tokens_can_be_std_attribute_p
2426 (cp_parser *, size_t);
2427 static bool cp_nth_tokens_can_be_gnu_attribute_p
2428 (cp_parser *, size_t);
2429 static bool cp_nth_tokens_can_be_attribute_p
2430 (cp_parser *, size_t);
2431 static tree cp_parser_attributes_opt
2432 (cp_parser *);
2433 static tree cp_parser_gnu_attributes_opt
2434 (cp_parser *);
2435 static tree cp_parser_gnu_attribute_list
2436 (cp_parser *, bool = false);
2437 static tree cp_parser_std_attribute
2438 (cp_parser *, tree);
2439 static tree cp_parser_std_attribute_spec
2440 (cp_parser *);
2441 static tree cp_parser_std_attribute_spec_seq
2442 (cp_parser *);
2443 static size_t cp_parser_skip_attributes_opt
2444 (cp_parser *, size_t);
2445 static bool cp_parser_extension_opt
2446 (cp_parser *, int *);
2447 static void cp_parser_label_declaration
2448 (cp_parser *);
2449
2450 /* Concept Extensions */
2451
2452 static tree cp_parser_concept_definition
2453 (cp_parser *);
2454 static tree cp_parser_constraint_expression
2455 (cp_parser *);
2456 static tree cp_parser_requires_clause_opt
2457 (cp_parser *, bool);
2458 static tree cp_parser_requires_expression
2459 (cp_parser *);
2460 static tree cp_parser_requirement_parameter_list
2461 (cp_parser *);
2462 static tree cp_parser_requirement_body
2463 (cp_parser *);
2464 static tree cp_parser_requirement_seq
2465 (cp_parser *);
2466 static tree cp_parser_requirement
2467 (cp_parser *);
2468 static tree cp_parser_simple_requirement
2469 (cp_parser *);
2470 static tree cp_parser_compound_requirement
2471 (cp_parser *);
2472 static tree cp_parser_type_requirement
2473 (cp_parser *);
2474 static tree cp_parser_nested_requirement
2475 (cp_parser *);
2476
2477 /* Transactional Memory Extensions */
2478
2479 static tree cp_parser_transaction
2480 (cp_parser *, cp_token *);
2481 static tree cp_parser_transaction_expression
2482 (cp_parser *, enum rid);
2483 static void cp_parser_function_transaction
2484 (cp_parser *, enum rid);
2485 static tree cp_parser_transaction_cancel
2486 (cp_parser *);
2487
2488 /* Coroutine extensions. */
2489
2490 static tree cp_parser_yield_expression
2491 (cp_parser *);
2492
2493
2494 enum pragma_context {
2495 pragma_external,
2496 pragma_member,
2497 pragma_objc_icode,
2498 pragma_stmt,
2499 pragma_compound
2500 };
2501 static bool cp_parser_pragma
2502 (cp_parser *, enum pragma_context, bool *);
2503
2504 /* Objective-C++ Productions */
2505
2506 static tree cp_parser_objc_message_receiver
2507 (cp_parser *);
2508 static tree cp_parser_objc_message_args
2509 (cp_parser *);
2510 static tree cp_parser_objc_message_expression
2511 (cp_parser *);
2512 static cp_expr cp_parser_objc_encode_expression
2513 (cp_parser *);
2514 static tree cp_parser_objc_defs_expression
2515 (cp_parser *);
2516 static tree cp_parser_objc_protocol_expression
2517 (cp_parser *);
2518 static tree cp_parser_objc_selector_expression
2519 (cp_parser *);
2520 static cp_expr cp_parser_objc_expression
2521 (cp_parser *);
2522 static bool cp_parser_objc_selector_p
2523 (enum cpp_ttype);
2524 static tree cp_parser_objc_selector
2525 (cp_parser *);
2526 static tree cp_parser_objc_protocol_refs_opt
2527 (cp_parser *);
2528 static void cp_parser_objc_declaration
2529 (cp_parser *, tree);
2530 static tree cp_parser_objc_statement
2531 (cp_parser *);
2532 static bool cp_parser_objc_valid_prefix_attributes
2533 (cp_parser *, tree *);
2534 static void cp_parser_objc_at_property_declaration
2535 (cp_parser *) ;
2536 static void cp_parser_objc_at_synthesize_declaration
2537 (cp_parser *) ;
2538 static void cp_parser_objc_at_dynamic_declaration
2539 (cp_parser *) ;
2540 static tree cp_parser_objc_struct_declaration
2541 (cp_parser *) ;
2542
2543 /* Utility Routines */
2544
2545 static cp_expr cp_parser_lookup_name
2546 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2547 static tree cp_parser_lookup_name_simple
2548 (cp_parser *, tree, location_t);
2549 static tree cp_parser_maybe_treat_template_as_class
2550 (tree, bool);
2551 static bool cp_parser_check_declarator_template_parameters
2552 (cp_parser *, cp_declarator *, location_t);
2553 static bool cp_parser_check_template_parameters
2554 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2555 static cp_expr cp_parser_simple_cast_expression
2556 (cp_parser *);
2557 static tree cp_parser_global_scope_opt
2558 (cp_parser *, bool);
2559 static bool cp_parser_constructor_declarator_p
2560 (cp_parser *, cp_parser_flags, bool);
2561 static tree cp_parser_function_definition_from_specifiers_and_declarator
2562 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2563 static tree cp_parser_function_definition_after_declarator
2564 (cp_parser *, bool);
2565 static bool cp_parser_template_declaration_after_export
2566 (cp_parser *, bool);
2567 static void cp_parser_perform_template_parameter_access_checks
2568 (vec<deferred_access_check, va_gc> *);
2569 static tree cp_parser_single_declaration
2570 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2571 static cp_expr cp_parser_functional_cast
2572 (cp_parser *, tree);
2573 static tree cp_parser_save_member_function_body
2574 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2575 static tree cp_parser_save_nsdmi
2576 (cp_parser *);
2577 static tree cp_parser_enclosed_template_argument_list
2578 (cp_parser *);
2579 static void cp_parser_save_default_args
2580 (cp_parser *, tree);
2581 static void cp_parser_late_parsing_for_member
2582 (cp_parser *, tree);
2583 static tree cp_parser_late_parse_one_default_arg
2584 (cp_parser *, tree, tree, tree);
2585 static void cp_parser_late_parsing_nsdmi
2586 (cp_parser *, tree);
2587 static void cp_parser_late_parsing_default_args
2588 (cp_parser *, tree);
2589 static tree cp_parser_sizeof_operand
2590 (cp_parser *, enum rid);
2591 static cp_expr cp_parser_trait_expr
2592 (cp_parser *, enum rid);
2593 static bool cp_parser_declares_only_class_p
2594 (cp_parser *);
2595 static void cp_parser_set_storage_class
2596 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2597 static void cp_parser_set_decl_spec_type
2598 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2599 static void set_and_check_decl_spec_loc
2600 (cp_decl_specifier_seq *decl_specs,
2601 cp_decl_spec ds, cp_token *);
2602 static bool cp_parser_friend_p
2603 (const cp_decl_specifier_seq *);
2604 static void cp_parser_required_error
2605 (cp_parser *, required_token, bool, location_t);
2606 static cp_token *cp_parser_require
2607 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2608 static cp_token *cp_parser_require_keyword
2609 (cp_parser *, enum rid, required_token);
2610 static bool cp_parser_token_starts_function_definition_p
2611 (cp_token *);
2612 static bool cp_parser_next_token_starts_class_definition_p
2613 (cp_parser *);
2614 static bool cp_parser_next_token_ends_template_argument_p
2615 (cp_parser *);
2616 static bool cp_parser_nth_token_starts_template_argument_list_p
2617 (cp_parser *, size_t);
2618 static enum tag_types cp_parser_token_is_class_key
2619 (cp_token *);
2620 static enum tag_types cp_parser_token_is_type_parameter_key
2621 (cp_token *);
2622 static void cp_parser_maybe_warn_enum_key (cp_parser *, location_t, tree, rid);
2623 static void cp_parser_check_class_key
2624 (cp_parser *, location_t, enum tag_types, tree type, bool, bool);
2625 static void cp_parser_check_access_in_redeclaration
2626 (tree type, location_t location);
2627 static bool cp_parser_optional_template_keyword
2628 (cp_parser *);
2629 static void cp_parser_pre_parsed_nested_name_specifier
2630 (cp_parser *);
2631 static bool cp_parser_cache_group
2632 (cp_parser *, enum cpp_ttype, unsigned);
2633 static tree cp_parser_cache_defarg
2634 (cp_parser *parser, bool nsdmi);
2635 static void cp_parser_parse_tentatively
2636 (cp_parser *);
2637 static void cp_parser_commit_to_tentative_parse
2638 (cp_parser *);
2639 static void cp_parser_commit_to_topmost_tentative_parse
2640 (cp_parser *);
2641 static void cp_parser_abort_tentative_parse
2642 (cp_parser *);
2643 static bool cp_parser_parse_definitely
2644 (cp_parser *);
2645 static inline bool cp_parser_parsing_tentatively
2646 (cp_parser *);
2647 static bool cp_parser_uncommitted_to_tentative_parse_p
2648 (cp_parser *);
2649 static void cp_parser_error
2650 (cp_parser *, const char *);
2651 static void cp_parser_name_lookup_error
2652 (cp_parser *, tree, tree, name_lookup_error, location_t);
2653 static bool cp_parser_simulate_error
2654 (cp_parser *);
2655 static bool cp_parser_check_type_definition
2656 (cp_parser *);
2657 static void cp_parser_check_for_definition_in_return_type
2658 (cp_declarator *, tree, location_t type_location);
2659 static void cp_parser_check_for_invalid_template_id
2660 (cp_parser *, tree, enum tag_types, location_t location);
2661 static bool cp_parser_non_integral_constant_expression
2662 (cp_parser *, non_integral_constant);
2663 static void cp_parser_diagnose_invalid_type_name
2664 (cp_parser *, tree, location_t);
2665 static bool cp_parser_parse_and_diagnose_invalid_type_name
2666 (cp_parser *);
2667 static int cp_parser_skip_to_closing_parenthesis
2668 (cp_parser *, bool, bool, bool);
2669 static void cp_parser_skip_to_end_of_statement
2670 (cp_parser *);
2671 static void cp_parser_consume_semicolon_at_end_of_statement
2672 (cp_parser *);
2673 static void cp_parser_skip_to_end_of_block_or_statement
2674 (cp_parser *);
2675 static bool cp_parser_skip_to_closing_brace
2676 (cp_parser *);
2677 static void cp_parser_skip_to_end_of_template_parameter_list
2678 (cp_parser *);
2679 static void cp_parser_skip_to_pragma_eol
2680 (cp_parser*, cp_token *);
2681 static bool cp_parser_error_occurred
2682 (cp_parser *);
2683 static bool cp_parser_allow_gnu_extensions_p
2684 (cp_parser *);
2685 static bool cp_parser_is_pure_string_literal
2686 (cp_token *);
2687 static bool cp_parser_is_string_literal
2688 (cp_token *);
2689 static bool cp_parser_is_keyword
2690 (cp_token *, enum rid);
2691 static tree cp_parser_make_typename_type
2692 (cp_parser *, tree, location_t location);
2693 static cp_declarator * cp_parser_make_indirect_declarator
2694 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2695 static bool cp_parser_compound_literal_p
2696 (cp_parser *);
2697 static bool cp_parser_array_designator_p
2698 (cp_parser *);
2699 static bool cp_parser_init_statement_p
2700 (cp_parser *);
2701 static bool cp_parser_skip_to_closing_square_bracket
2702 (cp_parser *);
2703 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
2704
2705 // -------------------------------------------------------------------------- //
2706 // Unevaluated Operand Guard
2707 //
2708 // Implementation of an RAII helper for unevaluated operand parsing.
2709 cp_unevaluated::cp_unevaluated ()
2710 {
2711 ++cp_unevaluated_operand;
2712 ++c_inhibit_evaluation_warnings;
2713 }
2714
2715 cp_unevaluated::~cp_unevaluated ()
2716 {
2717 --c_inhibit_evaluation_warnings;
2718 --cp_unevaluated_operand;
2719 }
2720
2721 // -------------------------------------------------------------------------- //
2722 // Tentative Parsing
2723
2724 /* Returns nonzero if we are parsing tentatively. */
2725
2726 static inline bool
2727 cp_parser_parsing_tentatively (cp_parser* parser)
2728 {
2729 return parser->context->next != NULL;
2730 }
2731
2732 /* Returns nonzero if TOKEN is a string literal. */
2733
2734 static bool
2735 cp_parser_is_pure_string_literal (cp_token* token)
2736 {
2737 return (token->type == CPP_STRING ||
2738 token->type == CPP_STRING16 ||
2739 token->type == CPP_STRING32 ||
2740 token->type == CPP_WSTRING ||
2741 token->type == CPP_UTF8STRING);
2742 }
2743
2744 /* Returns nonzero if TOKEN is a string literal
2745 of a user-defined string literal. */
2746
2747 static bool
2748 cp_parser_is_string_literal (cp_token* token)
2749 {
2750 return (cp_parser_is_pure_string_literal (token) ||
2751 token->type == CPP_STRING_USERDEF ||
2752 token->type == CPP_STRING16_USERDEF ||
2753 token->type == CPP_STRING32_USERDEF ||
2754 token->type == CPP_WSTRING_USERDEF ||
2755 token->type == CPP_UTF8STRING_USERDEF);
2756 }
2757
2758 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2759
2760 static bool
2761 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2762 {
2763 return token->keyword == keyword;
2764 }
2765
2766 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2767 PRAGMA_NONE. */
2768
2769 static enum pragma_kind
2770 cp_parser_pragma_kind (cp_token *token)
2771 {
2772 if (token->type != CPP_PRAGMA)
2773 return PRAGMA_NONE;
2774 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2775 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2776 }
2777
2778 /* Helper function for cp_parser_error.
2779 Having peeked a token of kind TOK1_KIND that might signify
2780 a conflict marker, peek successor tokens to determine
2781 if we actually do have a conflict marker.
2782 Specifically, we consider a run of 7 '<', '=' or '>' characters
2783 at the start of a line as a conflict marker.
2784 These come through the lexer as three pairs and a single,
2785 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2786 If it returns true, *OUT_LOC is written to with the location/range
2787 of the marker. */
2788
2789 static bool
2790 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2791 location_t *out_loc)
2792 {
2793 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2794 if (token2->type != tok1_kind)
2795 return false;
2796 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2797 if (token3->type != tok1_kind)
2798 return false;
2799 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2800 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2801 return false;
2802
2803 /* It must be at the start of the line. */
2804 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2805 if (LOCATION_COLUMN (start_loc) != 1)
2806 return false;
2807
2808 /* We have a conflict marker. Construct a location of the form:
2809 <<<<<<<
2810 ^~~~~~~
2811 with start == caret, finishing at the end of the marker. */
2812 location_t finish_loc = get_finish (token4->location);
2813 *out_loc = make_location (start_loc, start_loc, finish_loc);
2814
2815 return true;
2816 }
2817
2818 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2819 RT_CLOSE_PAREN. */
2820
2821 static const char *
2822 get_matching_symbol (required_token token_desc)
2823 {
2824 switch (token_desc)
2825 {
2826 default:
2827 gcc_unreachable ();
2828 return "";
2829 case RT_CLOSE_BRACE:
2830 return "{";
2831 case RT_CLOSE_PAREN:
2832 return "(";
2833 }
2834 }
2835
2836 /* Attempt to convert TOKEN_DESC from a required_token to an
2837 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2838
2839 static enum cpp_ttype
2840 get_required_cpp_ttype (required_token token_desc)
2841 {
2842 switch (token_desc)
2843 {
2844 case RT_SEMICOLON:
2845 return CPP_SEMICOLON;
2846 case RT_OPEN_PAREN:
2847 return CPP_OPEN_PAREN;
2848 case RT_CLOSE_BRACE:
2849 return CPP_CLOSE_BRACE;
2850 case RT_OPEN_BRACE:
2851 return CPP_OPEN_BRACE;
2852 case RT_CLOSE_SQUARE:
2853 return CPP_CLOSE_SQUARE;
2854 case RT_OPEN_SQUARE:
2855 return CPP_OPEN_SQUARE;
2856 case RT_COMMA:
2857 return CPP_COMMA;
2858 case RT_COLON:
2859 return CPP_COLON;
2860 case RT_CLOSE_PAREN:
2861 return CPP_CLOSE_PAREN;
2862
2863 default:
2864 /* Use CPP_EOF as a "no completions possible" code. */
2865 return CPP_EOF;
2866 }
2867 }
2868
2869
2870 /* Subroutine of cp_parser_error and cp_parser_required_error.
2871
2872 Issue a diagnostic of the form
2873 FILE:LINE: MESSAGE before TOKEN
2874 where TOKEN is the next token in the input stream. MESSAGE
2875 (specified by the caller) is usually of the form "expected
2876 OTHER-TOKEN".
2877
2878 This bypasses the check for tentative passing, and potentially
2879 adds material needed by cp_parser_required_error.
2880
2881 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2882 suggesting insertion of the missing token.
2883
2884 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2885 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2886 location. */
2887
2888 static void
2889 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2890 required_token missing_token_desc,
2891 location_t matching_location)
2892 {
2893 cp_token *token = cp_lexer_peek_token (parser->lexer);
2894 /* This diagnostic makes more sense if it is tagged to the line
2895 of the token we just peeked at. */
2896 cp_lexer_set_source_position_from_token (token);
2897
2898 if (token->type == CPP_PRAGMA)
2899 {
2900 error_at (token->location,
2901 "%<#pragma%> is not allowed here");
2902 cp_parser_skip_to_pragma_eol (parser, token);
2903 return;
2904 }
2905
2906 /* If this is actually a conflict marker, report it as such. */
2907 if (token->type == CPP_LSHIFT
2908 || token->type == CPP_RSHIFT
2909 || token->type == CPP_EQ_EQ)
2910 {
2911 location_t loc;
2912 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2913 {
2914 error_at (loc, "version control conflict marker in file");
2915 expanded_location token_exploc = expand_location (token->location);
2916 /* Consume tokens until the end of the source line. */
2917 for (;;)
2918 {
2919 cp_lexer_consume_token (parser->lexer);
2920 cp_token *next = cp_lexer_peek_token (parser->lexer);
2921 if (next->type == CPP_EOF)
2922 break;
2923 if (next->location == UNKNOWN_LOCATION
2924 || loc == UNKNOWN_LOCATION)
2925 break;
2926
2927 expanded_location next_exploc = expand_location (next->location);
2928 if (next_exploc.file != token_exploc.file)
2929 break;
2930 if (next_exploc.line != token_exploc.line)
2931 break;
2932 }
2933 return;
2934 }
2935 }
2936
2937 auto_diagnostic_group d;
2938 gcc_rich_location richloc (input_location);
2939
2940 bool added_matching_location = false;
2941
2942 if (missing_token_desc != RT_NONE)
2943 {
2944 /* Potentially supply a fix-it hint, suggesting to add the
2945 missing token immediately after the *previous* token.
2946 This may move the primary location within richloc. */
2947 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2948 location_t prev_token_loc
2949 = cp_lexer_previous_token (parser->lexer)->location;
2950 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2951
2952 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2953 Attempt to consolidate diagnostics by printing it as a
2954 secondary range within the main diagnostic. */
2955 if (matching_location != UNKNOWN_LOCATION)
2956 added_matching_location
2957 = richloc.add_location_if_nearby (matching_location);
2958 }
2959
2960 /* If we were parsing a string-literal and there is an unknown name
2961 token right after, then check to see if that could also have been
2962 a literal string by checking the name against a list of known
2963 standard string literal constants defined in header files. If
2964 there is one, then add that as an hint to the error message. */
2965 name_hint h;
2966 cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer);
2967 if (prev_token && cp_parser_is_string_literal (prev_token)
2968 && token->type == CPP_NAME)
2969 {
2970 tree name = token->u.value;
2971 const char *token_name = IDENTIFIER_POINTER (name);
2972 const char *header_hint
2973 = get_cp_stdlib_header_for_string_macro_name (token_name);
2974 if (header_hint != NULL)
2975 h = name_hint (NULL, new suggest_missing_header (token->location,
2976 token_name,
2977 header_hint));
2978 }
2979
2980 /* Actually emit the error. */
2981 c_parse_error (gmsgid,
2982 /* Because c_parser_error does not understand
2983 CPP_KEYWORD, keywords are treated like
2984 identifiers. */
2985 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2986 token->u.value, token->flags, &richloc);
2987
2988 if (missing_token_desc != RT_NONE)
2989 {
2990 /* If we weren't able to consolidate matching_location, then
2991 print it as a secondary diagnostic. */
2992 if (matching_location != UNKNOWN_LOCATION
2993 && !added_matching_location)
2994 inform (matching_location, "to match this %qs",
2995 get_matching_symbol (missing_token_desc));
2996 }
2997 }
2998
2999 /* If not parsing tentatively, issue a diagnostic of the form
3000 FILE:LINE: MESSAGE before TOKEN
3001 where TOKEN is the next token in the input stream. MESSAGE
3002 (specified by the caller) is usually of the form "expected
3003 OTHER-TOKEN". */
3004
3005 static void
3006 cp_parser_error (cp_parser* parser, const char* gmsgid)
3007 {
3008 if (!cp_parser_simulate_error (parser))
3009 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
3010 }
3011
3012 /* Issue an error about name-lookup failing. NAME is the
3013 IDENTIFIER_NODE DECL is the result of
3014 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3015 the thing that we hoped to find. */
3016
3017 static void
3018 cp_parser_name_lookup_error (cp_parser* parser,
3019 tree name,
3020 tree decl,
3021 name_lookup_error desired,
3022 location_t location)
3023 {
3024 /* If name lookup completely failed, tell the user that NAME was not
3025 declared. */
3026 if (decl == error_mark_node)
3027 {
3028 if (parser->scope && parser->scope != global_namespace)
3029 error_at (location, "%<%E::%E%> has not been declared",
3030 parser->scope, name);
3031 else if (parser->scope == global_namespace)
3032 error_at (location, "%<::%E%> has not been declared", name);
3033 else if (parser->object_scope
3034 && !CLASS_TYPE_P (parser->object_scope))
3035 error_at (location, "request for member %qE in non-class type %qT",
3036 name, parser->object_scope);
3037 else if (parser->object_scope)
3038 error_at (location, "%<%T::%E%> has not been declared",
3039 parser->object_scope, name);
3040 else
3041 error_at (location, "%qE has not been declared", name);
3042 }
3043 else if (parser->scope && parser->scope != global_namespace)
3044 {
3045 switch (desired)
3046 {
3047 case NLE_TYPE:
3048 error_at (location, "%<%E::%E%> is not a type",
3049 parser->scope, name);
3050 break;
3051 case NLE_CXX98:
3052 error_at (location, "%<%E::%E%> is not a class or namespace",
3053 parser->scope, name);
3054 break;
3055 case NLE_NOT_CXX98:
3056 error_at (location,
3057 "%<%E::%E%> is not a class, namespace, or enumeration",
3058 parser->scope, name);
3059 break;
3060 default:
3061 gcc_unreachable ();
3062
3063 }
3064 }
3065 else if (parser->scope == global_namespace)
3066 {
3067 switch (desired)
3068 {
3069 case NLE_TYPE:
3070 error_at (location, "%<::%E%> is not a type", name);
3071 break;
3072 case NLE_CXX98:
3073 error_at (location, "%<::%E%> is not a class or namespace", name);
3074 break;
3075 case NLE_NOT_CXX98:
3076 error_at (location,
3077 "%<::%E%> is not a class, namespace, or enumeration",
3078 name);
3079 break;
3080 default:
3081 gcc_unreachable ();
3082 }
3083 }
3084 else
3085 {
3086 switch (desired)
3087 {
3088 case NLE_TYPE:
3089 error_at (location, "%qE is not a type", name);
3090 break;
3091 case NLE_CXX98:
3092 error_at (location, "%qE is not a class or namespace", name);
3093 break;
3094 case NLE_NOT_CXX98:
3095 error_at (location,
3096 "%qE is not a class, namespace, or enumeration", name);
3097 break;
3098 default:
3099 gcc_unreachable ();
3100 }
3101 }
3102 }
3103
3104 /* If we are parsing tentatively, remember that an error has occurred
3105 during this tentative parse. Returns true if the error was
3106 simulated; false if a message should be issued by the caller. */
3107
3108 static bool
3109 cp_parser_simulate_error (cp_parser* parser)
3110 {
3111 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3112 {
3113 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3114 return true;
3115 }
3116 return false;
3117 }
3118
3119 /* This function is called when a type is defined. If type
3120 definitions are forbidden at this point, an error message is
3121 issued. */
3122
3123 static bool
3124 cp_parser_check_type_definition (cp_parser* parser)
3125 {
3126 /* If types are forbidden here, issue a message. */
3127 if (parser->type_definition_forbidden_message)
3128 {
3129 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3130 or %qs in the message need to be interpreted. */
3131 error (parser->type_definition_forbidden_message,
3132 parser->type_definition_forbidden_message_arg);
3133 return false;
3134 }
3135 return true;
3136 }
3137
3138 /* This function is called when the DECLARATOR is processed. The TYPE
3139 was a type defined in the decl-specifiers. If it is invalid to
3140 define a type in the decl-specifiers for DECLARATOR, an error is
3141 issued. TYPE_LOCATION is the location of TYPE and is used
3142 for error reporting. */
3143
3144 static void
3145 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3146 tree type, location_t type_location)
3147 {
3148 /* [dcl.fct] forbids type definitions in return types.
3149 Unfortunately, it's not easy to know whether or not we are
3150 processing a return type until after the fact. */
3151 while (declarator
3152 && (declarator->kind == cdk_pointer
3153 || declarator->kind == cdk_reference
3154 || declarator->kind == cdk_ptrmem))
3155 declarator = declarator->declarator;
3156 if (declarator
3157 && declarator->kind == cdk_function)
3158 {
3159 error_at (type_location,
3160 "new types may not be defined in a return type");
3161 inform (type_location,
3162 "(perhaps a semicolon is missing after the definition of %qT)",
3163 type);
3164 }
3165 }
3166
3167 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3168 "<" in any valid C++ program. If the next token is indeed "<",
3169 issue a message warning the user about what appears to be an
3170 invalid attempt to form a template-id. LOCATION is the location
3171 of the type-specifier (TYPE) */
3172
3173 static void
3174 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3175 tree type,
3176 enum tag_types tag_type,
3177 location_t location)
3178 {
3179 cp_token_position start = 0;
3180
3181 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3182 {
3183 if (TREE_CODE (type) == TYPE_DECL)
3184 type = TREE_TYPE (type);
3185 if (TYPE_P (type) && !template_placeholder_p (type))
3186 error_at (location, "%qT is not a template", type);
3187 else if (identifier_p (type))
3188 {
3189 if (tag_type != none_type)
3190 error_at (location, "%qE is not a class template", type);
3191 else
3192 error_at (location, "%qE is not a template", type);
3193 }
3194 else
3195 error_at (location, "invalid template-id");
3196 /* Remember the location of the invalid "<". */
3197 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3198 start = cp_lexer_token_position (parser->lexer, true);
3199 /* Consume the "<". */
3200 cp_lexer_consume_token (parser->lexer);
3201 /* Parse the template arguments. */
3202 cp_parser_enclosed_template_argument_list (parser);
3203 /* Permanently remove the invalid template arguments so that
3204 this error message is not issued again. */
3205 if (start)
3206 cp_lexer_purge_tokens_after (parser->lexer, start);
3207 }
3208 }
3209
3210 /* If parsing an integral constant-expression, issue an error message
3211 about the fact that THING appeared and return true. Otherwise,
3212 return false. In either case, set
3213 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3214
3215 static bool
3216 cp_parser_non_integral_constant_expression (cp_parser *parser,
3217 non_integral_constant thing)
3218 {
3219 parser->non_integral_constant_expression_p = true;
3220 if (parser->integral_constant_expression_p)
3221 {
3222 if (!parser->allow_non_integral_constant_expression_p)
3223 {
3224 const char *msg = NULL;
3225 switch (thing)
3226 {
3227 case NIC_FLOAT:
3228 pedwarn (input_location, OPT_Wpedantic,
3229 "ISO C++ forbids using a floating-point literal "
3230 "in a constant-expression");
3231 return true;
3232 case NIC_CAST:
3233 error ("a cast to a type other than an integral or "
3234 "enumeration type cannot appear in a "
3235 "constant-expression");
3236 return true;
3237 case NIC_TYPEID:
3238 error ("%<typeid%> operator "
3239 "cannot appear in a constant-expression");
3240 return true;
3241 case NIC_NCC:
3242 error ("non-constant compound literals "
3243 "cannot appear in a constant-expression");
3244 return true;
3245 case NIC_FUNC_CALL:
3246 error ("a function call "
3247 "cannot appear in a constant-expression");
3248 return true;
3249 case NIC_INC:
3250 error ("an increment "
3251 "cannot appear in a constant-expression");
3252 return true;
3253 case NIC_DEC:
3254 error ("an decrement "
3255 "cannot appear in a constant-expression");
3256 return true;
3257 case NIC_ARRAY_REF:
3258 error ("an array reference "
3259 "cannot appear in a constant-expression");
3260 return true;
3261 case NIC_ADDR_LABEL:
3262 error ("the address of a label "
3263 "cannot appear in a constant-expression");
3264 return true;
3265 case NIC_OVERLOADED:
3266 error ("calls to overloaded operators "
3267 "cannot appear in a constant-expression");
3268 return true;
3269 case NIC_ASSIGNMENT:
3270 error ("an assignment cannot appear in a constant-expression");
3271 return true;
3272 case NIC_COMMA:
3273 error ("a comma operator "
3274 "cannot appear in a constant-expression");
3275 return true;
3276 case NIC_CONSTRUCTOR:
3277 error ("a call to a constructor "
3278 "cannot appear in a constant-expression");
3279 return true;
3280 case NIC_TRANSACTION:
3281 error ("a transaction expression "
3282 "cannot appear in a constant-expression");
3283 return true;
3284 case NIC_THIS:
3285 msg = "this";
3286 break;
3287 case NIC_FUNC_NAME:
3288 msg = "__FUNCTION__";
3289 break;
3290 case NIC_PRETTY_FUNC:
3291 msg = "__PRETTY_FUNCTION__";
3292 break;
3293 case NIC_C99_FUNC:
3294 msg = "__func__";
3295 break;
3296 case NIC_VA_ARG:
3297 msg = "va_arg";
3298 break;
3299 case NIC_ARROW:
3300 msg = "->";
3301 break;
3302 case NIC_POINT:
3303 msg = ".";
3304 break;
3305 case NIC_STAR:
3306 msg = "*";
3307 break;
3308 case NIC_ADDR:
3309 msg = "&";
3310 break;
3311 case NIC_PREINCREMENT:
3312 msg = "++";
3313 break;
3314 case NIC_PREDECREMENT:
3315 msg = "--";
3316 break;
3317 case NIC_NEW:
3318 msg = "new";
3319 break;
3320 case NIC_DEL:
3321 msg = "delete";
3322 break;
3323 default:
3324 gcc_unreachable ();
3325 }
3326 if (msg)
3327 error ("%qs cannot appear in a constant-expression", msg);
3328 return true;
3329 }
3330 }
3331 return false;
3332 }
3333
3334 /* Emit a diagnostic for an invalid type name. This function commits
3335 to the current active tentative parse, if any. (Otherwise, the
3336 problematic construct might be encountered again later, resulting
3337 in duplicate error messages.) LOCATION is the location of ID. */
3338
3339 static void
3340 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3341 location_t location)
3342 {
3343 tree decl, ambiguous_decls;
3344 cp_parser_commit_to_tentative_parse (parser);
3345 /* Try to lookup the identifier. */
3346 decl = cp_parser_lookup_name (parser, id, none_type,
3347 /*is_template=*/false,
3348 /*is_namespace=*/false,
3349 /*check_dependency=*/true,
3350 &ambiguous_decls, location);
3351 if (ambiguous_decls)
3352 /* If the lookup was ambiguous, an error will already have
3353 been issued. */
3354 return;
3355 /* If the lookup found a template-name, it means that the user forgot
3356 to specify an argument list. Emit a useful error message. */
3357 if (DECL_TYPE_TEMPLATE_P (decl))
3358 {
3359 auto_diagnostic_group d;
3360 error_at (location,
3361 "invalid use of template-name %qE without an argument list",
3362 decl);
3363 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3364 inform (location, "class template argument deduction is only available "
3365 "with %<-std=c++17%> or %<-std=gnu++17%>");
3366 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3367 }
3368 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3369 error_at (location, "invalid use of destructor %qD as a type", id);
3370 else if (TREE_CODE (decl) == TYPE_DECL)
3371 /* Something like 'unsigned A a;' */
3372 error_at (location, "invalid combination of multiple type-specifiers");
3373 else if (!parser->scope)
3374 {
3375 /* Issue an error message. */
3376 auto_diagnostic_group d;
3377 name_hint hint;
3378 if (TREE_CODE (id) == IDENTIFIER_NODE)
3379 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3380 if (const char *suggestion = hint.suggestion ())
3381 {
3382 gcc_rich_location richloc (location);
3383 richloc.add_fixit_replace (suggestion);
3384 error_at (&richloc,
3385 "%qE does not name a type; did you mean %qs?",
3386 id, suggestion);
3387 }
3388 else
3389 error_at (location, "%qE does not name a type", id);
3390 /* If we're in a template class, it's possible that the user was
3391 referring to a type from a base class. For example:
3392
3393 template <typename T> struct A { typedef T X; };
3394 template <typename T> struct B : public A<T> { X x; };
3395
3396 The user should have said "typename A<T>::X". */
3397 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3398 inform (location, "C++11 %<constexpr%> only available with "
3399 "%<-std=c++11%> or %<-std=gnu++11%>");
3400 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3401 inform (location, "C++11 %<noexcept%> only available with "
3402 "%<-std=c++11%> or %<-std=gnu++11%>");
3403 else if (cxx_dialect < cxx11
3404 && TREE_CODE (id) == IDENTIFIER_NODE
3405 && id_equal (id, "thread_local"))
3406 inform (location, "C++11 %<thread_local%> only available with "
3407 "%<-std=c++11%> or %<-std=gnu++11%>");
3408 else if (cxx_dialect < cxx20 && id == ridpointers[(int)RID_CONSTINIT])
3409 inform (location, "C++20 %<constinit%> only available with "
3410 "%<-std=c++20%> or %<-std=gnu++20%>");
3411 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3412 inform (location, "%<concept%> only available with %<-std=c++20%> or "
3413 "%<-fconcepts%>");
3414 else if (!flag_concepts && id == ridpointers[(int)RID_REQUIRES])
3415 inform (location, "%<requires%> only available with %<-std=c++20%> or "
3416 "%<-fconcepts%>");
3417 else if (processing_template_decl && current_class_type
3418 && TYPE_BINFO (current_class_type))
3419 {
3420 for (tree b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3421 b; b = TREE_CHAIN (b))
3422 {
3423 tree base_type = BINFO_TYPE (b);
3424 if (CLASS_TYPE_P (base_type)
3425 && dependent_type_p (base_type))
3426 {
3427 /* Go from a particular instantiation of the
3428 template (which will have an empty TYPE_FIELDs),
3429 to the main version. */
3430 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3431 for (tree field = TYPE_FIELDS (base_type);
3432 field; field = DECL_CHAIN (field))
3433 if (TREE_CODE (field) == TYPE_DECL
3434 && DECL_NAME (field) == id)
3435 {
3436 inform (location,
3437 "(perhaps %<typename %T::%E%> was intended)",
3438 BINFO_TYPE (b), id);
3439 goto found;
3440 }
3441 }
3442 }
3443 found:;
3444 }
3445 }
3446 /* Here we diagnose qualified-ids where the scope is actually correct,
3447 but the identifier does not resolve to a valid type name. */
3448 else if (parser->scope != error_mark_node)
3449 {
3450 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3451 {
3452 auto_diagnostic_group d;
3453 name_hint hint;
3454 if (decl == error_mark_node)
3455 hint = suggest_alternative_in_explicit_scope (location, id,
3456 parser->scope);
3457 const char *suggestion = hint.suggestion ();
3458 gcc_rich_location richloc (location_of (id));
3459 if (suggestion)
3460 richloc.add_fixit_replace (suggestion);
3461 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3462 {
3463 if (suggestion)
3464 error_at (&richloc,
3465 "%qE in namespace %qE does not name a template"
3466 " type; did you mean %qs?",
3467 id, parser->scope, suggestion);
3468 else
3469 error_at (&richloc,
3470 "%qE in namespace %qE does not name a template type",
3471 id, parser->scope);
3472 }
3473 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3474 {
3475 if (suggestion)
3476 error_at (&richloc,
3477 "%qE in namespace %qE does not name a template"
3478 " type; did you mean %qs?",
3479 TREE_OPERAND (id, 0), parser->scope, suggestion);
3480 else
3481 error_at (&richloc,
3482 "%qE in namespace %qE does not name a template"
3483 " type",
3484 TREE_OPERAND (id, 0), parser->scope);
3485 }
3486 else
3487 {
3488 if (suggestion)
3489 error_at (&richloc,
3490 "%qE in namespace %qE does not name a type"
3491 "; did you mean %qs?",
3492 id, parser->scope, suggestion);
3493 else
3494 error_at (&richloc,
3495 "%qE in namespace %qE does not name a type",
3496 id, parser->scope);
3497 }
3498 if (DECL_P (decl))
3499 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3500 }
3501 else if (CLASS_TYPE_P (parser->scope)
3502 && constructor_name_p (id, parser->scope))
3503 {
3504 /* A<T>::A<T>() */
3505 auto_diagnostic_group d;
3506 error_at (location, "%<%T::%E%> names the constructor, not"
3507 " the type", parser->scope, id);
3508 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3509 error_at (location, "and %qT has no template constructors",
3510 parser->scope);
3511 }
3512 else if (TYPE_P (parser->scope)
3513 && dependent_scope_p (parser->scope))
3514 {
3515 gcc_rich_location richloc (location);
3516 richloc.add_fixit_insert_before ("typename ");
3517 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3518 error_at (&richloc,
3519 "need %<typename%> before %<%T::%D::%E%> because "
3520 "%<%T::%D%> is a dependent scope",
3521 TYPE_CONTEXT (parser->scope),
3522 TYPENAME_TYPE_FULLNAME (parser->scope),
3523 id,
3524 TYPE_CONTEXT (parser->scope),
3525 TYPENAME_TYPE_FULLNAME (parser->scope));
3526 else
3527 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3528 "%qT is a dependent scope",
3529 parser->scope, id, parser->scope);
3530 }
3531 else if (TYPE_P (parser->scope))
3532 {
3533 auto_diagnostic_group d;
3534 if (!COMPLETE_TYPE_P (parser->scope))
3535 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3536 parser->scope);
3537 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3538 error_at (location_of (id),
3539 "%qE in %q#T does not name a template type",
3540 id, parser->scope);
3541 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3542 error_at (location_of (id),
3543 "%qE in %q#T does not name a template type",
3544 TREE_OPERAND (id, 0), parser->scope);
3545 else
3546 error_at (location_of (id),
3547 "%qE in %q#T does not name a type",
3548 id, parser->scope);
3549 if (DECL_P (decl))
3550 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3551 }
3552 else
3553 gcc_unreachable ();
3554 }
3555 }
3556
3557 /* Check for a common situation where a type-name should be present,
3558 but is not, and issue a sensible error message. Returns true if an
3559 invalid type-name was detected.
3560
3561 The situation handled by this function are variable declarations of the
3562 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3563 Usually, `ID' should name a type, but if we got here it means that it
3564 does not. We try to emit the best possible error message depending on
3565 how exactly the id-expression looks like. */
3566
3567 static bool
3568 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3569 {
3570 tree id;
3571 cp_token *token = cp_lexer_peek_token (parser->lexer);
3572
3573 /* Avoid duplicate error about ambiguous lookup. */
3574 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3575 {
3576 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3577 if (next->type == CPP_NAME && next->error_reported)
3578 goto out;
3579 }
3580
3581 cp_parser_parse_tentatively (parser);
3582 id = cp_parser_id_expression (parser,
3583 /*template_keyword_p=*/false,
3584 /*check_dependency_p=*/true,
3585 /*template_p=*/NULL,
3586 /*declarator_p=*/false,
3587 /*optional_p=*/false);
3588 /* If the next token is a (, this is a function with no explicit return
3589 type, i.e. constructor, destructor or conversion op. */
3590 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3591 || TREE_CODE (id) == TYPE_DECL)
3592 {
3593 cp_parser_abort_tentative_parse (parser);
3594 return false;
3595 }
3596 if (!cp_parser_parse_definitely (parser))
3597 return false;
3598
3599 /* Emit a diagnostic for the invalid type. */
3600 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3601 out:
3602 /* If we aren't in the middle of a declarator (i.e. in a
3603 parameter-declaration-clause), skip to the end of the declaration;
3604 there's no point in trying to process it. */
3605 if (!parser->in_declarator_p)
3606 cp_parser_skip_to_end_of_block_or_statement (parser);
3607 return true;
3608 }
3609
3610 /* Consume tokens up to, and including, the next non-nested closing `)'.
3611 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3612 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3613 found an unnested token of that type. */
3614
3615 static int
3616 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3617 bool recovering,
3618 cpp_ttype or_ttype,
3619 bool consume_paren)
3620 {
3621 unsigned paren_depth = 0;
3622 unsigned brace_depth = 0;
3623 unsigned square_depth = 0;
3624 unsigned condop_depth = 0;
3625
3626 if (recovering && or_ttype == CPP_EOF
3627 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3628 return 0;
3629
3630 while (true)
3631 {
3632 cp_token * token = cp_lexer_peek_token (parser->lexer);
3633
3634 /* Have we found what we're looking for before the closing paren? */
3635 if (token->type == or_ttype && or_ttype != CPP_EOF
3636 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3637 return -1;
3638
3639 switch (token->type)
3640 {
3641 case CPP_PRAGMA_EOL:
3642 if (!parser->lexer->in_pragma)
3643 break;
3644 /* FALLTHRU */
3645 case CPP_EOF:
3646 /* If we've run out of tokens, then there is no closing `)'. */
3647 return 0;
3648
3649 /* This is good for lambda expression capture-lists. */
3650 case CPP_OPEN_SQUARE:
3651 ++square_depth;
3652 break;
3653 case CPP_CLOSE_SQUARE:
3654 if (!square_depth--)
3655 return 0;
3656 break;
3657
3658 case CPP_SEMICOLON:
3659 /* This matches the processing in skip_to_end_of_statement. */
3660 if (!brace_depth)
3661 return 0;
3662 break;
3663
3664 case CPP_OPEN_BRACE:
3665 ++brace_depth;
3666 break;
3667 case CPP_CLOSE_BRACE:
3668 if (!brace_depth--)
3669 return 0;
3670 break;
3671
3672 case CPP_OPEN_PAREN:
3673 if (!brace_depth)
3674 ++paren_depth;
3675 break;
3676
3677 case CPP_CLOSE_PAREN:
3678 if (!brace_depth && !paren_depth--)
3679 {
3680 if (consume_paren)
3681 cp_lexer_consume_token (parser->lexer);
3682 return 1;
3683 }
3684 break;
3685
3686 case CPP_QUERY:
3687 if (!brace_depth && !paren_depth && !square_depth)
3688 ++condop_depth;
3689 break;
3690
3691 case CPP_COLON:
3692 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3693 condop_depth--;
3694 break;
3695
3696 default:
3697 break;
3698 }
3699
3700 /* Consume the token. */
3701 cp_lexer_consume_token (parser->lexer);
3702 }
3703 }
3704
3705 /* Consume tokens up to, and including, the next non-nested closing `)'.
3706 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3707 are doing error recovery. Returns -1 if OR_COMMA is true and we
3708 found an unnested token of that type. */
3709
3710 static int
3711 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3712 bool recovering,
3713 bool or_comma,
3714 bool consume_paren)
3715 {
3716 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3717 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3718 ttype, consume_paren);
3719 }
3720
3721 /* Consume tokens until we reach the end of the current statement.
3722 Normally, that will be just before consuming a `;'. However, if a
3723 non-nested `}' comes first, then we stop before consuming that. */
3724
3725 static void
3726 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3727 {
3728 unsigned nesting_depth = 0;
3729
3730 /* Unwind generic function template scope if necessary. */
3731 if (parser->fully_implicit_function_template_p)
3732 abort_fully_implicit_template (parser);
3733
3734 while (true)
3735 {
3736 cp_token *token = cp_lexer_peek_token (parser->lexer);
3737
3738 switch (token->type)
3739 {
3740 case CPP_PRAGMA_EOL:
3741 if (!parser->lexer->in_pragma)
3742 break;
3743 /* FALLTHRU */
3744 case CPP_EOF:
3745 /* If we've run out of tokens, stop. */
3746 return;
3747
3748 case CPP_SEMICOLON:
3749 /* If the next token is a `;', we have reached the end of the
3750 statement. */
3751 if (!nesting_depth)
3752 return;
3753 break;
3754
3755 case CPP_CLOSE_BRACE:
3756 /* If this is a non-nested '}', stop before consuming it.
3757 That way, when confronted with something like:
3758
3759 { 3 + }
3760
3761 we stop before consuming the closing '}', even though we
3762 have not yet reached a `;'. */
3763 if (nesting_depth == 0)
3764 return;
3765
3766 /* If it is the closing '}' for a block that we have
3767 scanned, stop -- but only after consuming the token.
3768 That way given:
3769
3770 void f g () { ... }
3771 typedef int I;
3772
3773 we will stop after the body of the erroneously declared
3774 function, but before consuming the following `typedef'
3775 declaration. */
3776 if (--nesting_depth == 0)
3777 {
3778 cp_lexer_consume_token (parser->lexer);
3779 return;
3780 }
3781 break;
3782
3783 case CPP_OPEN_BRACE:
3784 ++nesting_depth;
3785 break;
3786
3787 default:
3788 break;
3789 }
3790
3791 /* Consume the token. */
3792 cp_lexer_consume_token (parser->lexer);
3793 }
3794 }
3795
3796 /* This function is called at the end of a statement or declaration.
3797 If the next token is a semicolon, it is consumed; otherwise, error
3798 recovery is attempted. */
3799
3800 static void
3801 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3802 {
3803 /* Look for the trailing `;'. */
3804 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3805 {
3806 /* If there is additional (erroneous) input, skip to the end of
3807 the statement. */
3808 cp_parser_skip_to_end_of_statement (parser);
3809 /* If the next token is now a `;', consume it. */
3810 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3811 cp_lexer_consume_token (parser->lexer);
3812 }
3813 }
3814
3815 /* Skip tokens until we have consumed an entire block, or until we
3816 have consumed a non-nested `;'. */
3817
3818 static void
3819 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3820 {
3821 int nesting_depth = 0;
3822
3823 /* Unwind generic function template scope if necessary. */
3824 if (parser->fully_implicit_function_template_p)
3825 abort_fully_implicit_template (parser);
3826
3827 while (nesting_depth >= 0)
3828 {
3829 cp_token *token = cp_lexer_peek_token (parser->lexer);
3830
3831 switch (token->type)
3832 {
3833 case CPP_PRAGMA_EOL:
3834 if (!parser->lexer->in_pragma)
3835 break;
3836 /* FALLTHRU */
3837 case CPP_EOF:
3838 /* If we've run out of tokens, stop. */
3839 return;
3840
3841 case CPP_SEMICOLON:
3842 /* Stop if this is an unnested ';'. */
3843 if (!nesting_depth)
3844 nesting_depth = -1;
3845 break;
3846
3847 case CPP_CLOSE_BRACE:
3848 /* Stop if this is an unnested '}', or closes the outermost
3849 nesting level. */
3850 nesting_depth--;
3851 if (nesting_depth < 0)
3852 return;
3853 if (!nesting_depth)
3854 nesting_depth = -1;
3855 break;
3856
3857 case CPP_OPEN_BRACE:
3858 /* Nest. */
3859 nesting_depth++;
3860 break;
3861
3862 default:
3863 break;
3864 }
3865
3866 /* Consume the token. */
3867 cp_lexer_consume_token (parser->lexer);
3868 }
3869 }
3870
3871 /* Skip tokens until a non-nested closing curly brace is the next
3872 token, or there are no more tokens. Return true in the first case,
3873 false otherwise. */
3874
3875 static bool
3876 cp_parser_skip_to_closing_brace (cp_parser *parser)
3877 {
3878 unsigned nesting_depth = 0;
3879
3880 while (true)
3881 {
3882 cp_token *token = cp_lexer_peek_token (parser->lexer);
3883
3884 switch (token->type)
3885 {
3886 case CPP_PRAGMA_EOL:
3887 if (!parser->lexer->in_pragma)
3888 break;
3889 /* FALLTHRU */
3890 case CPP_EOF:
3891 /* If we've run out of tokens, stop. */
3892 return false;
3893
3894 case CPP_CLOSE_BRACE:
3895 /* If the next token is a non-nested `}', then we have reached
3896 the end of the current block. */
3897 if (nesting_depth-- == 0)
3898 return true;
3899 break;
3900
3901 case CPP_OPEN_BRACE:
3902 /* If it the next token is a `{', then we are entering a new
3903 block. Consume the entire block. */
3904 ++nesting_depth;
3905 break;
3906
3907 default:
3908 break;
3909 }
3910
3911 /* Consume the token. */
3912 cp_lexer_consume_token (parser->lexer);
3913 }
3914 }
3915
3916 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3917 parameter is the PRAGMA token, allowing us to purge the entire pragma
3918 sequence. */
3919
3920 static void
3921 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3922 {
3923 cp_token *token;
3924
3925 parser->lexer->in_pragma = false;
3926
3927 do
3928 token = cp_lexer_consume_token (parser->lexer);
3929 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3930
3931 /* Ensure that the pragma is not parsed again. */
3932 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3933 }
3934
3935 /* Require pragma end of line, resyncing with it as necessary. The
3936 arguments are as for cp_parser_skip_to_pragma_eol. */
3937
3938 static void
3939 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3940 {
3941 parser->lexer->in_pragma = false;
3942 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3943 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3944 }
3945
3946 /* This is a simple wrapper around make_typename_type. When the id is
3947 an unresolved identifier node, we can provide a superior diagnostic
3948 using cp_parser_diagnose_invalid_type_name. */
3949
3950 static tree
3951 cp_parser_make_typename_type (cp_parser *parser, tree id,
3952 location_t id_location)
3953 {
3954 tree result;
3955 if (identifier_p (id))
3956 {
3957 result = make_typename_type (parser->scope, id, typename_type,
3958 /*complain=*/tf_none);
3959 if (result == error_mark_node)
3960 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3961 return result;
3962 }
3963 return make_typename_type (parser->scope, id, typename_type, tf_error);
3964 }
3965
3966 /* This is a wrapper around the
3967 make_{pointer,ptrmem,reference}_declarator functions that decides
3968 which one to call based on the CODE and CLASS_TYPE arguments. The
3969 CODE argument should be one of the values returned by
3970 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3971 appertain to the pointer or reference. */
3972
3973 static cp_declarator *
3974 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3975 cp_cv_quals cv_qualifiers,
3976 cp_declarator *target,
3977 tree attributes)
3978 {
3979 if (code == ERROR_MARK || target == cp_error_declarator)
3980 return cp_error_declarator;
3981
3982 if (code == INDIRECT_REF)
3983 if (class_type == NULL_TREE)
3984 return make_pointer_declarator (cv_qualifiers, target, attributes);
3985 else
3986 return make_ptrmem_declarator (cv_qualifiers, class_type,
3987 target, attributes);
3988 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3989 return make_reference_declarator (cv_qualifiers, target,
3990 false, attributes);
3991 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3992 return make_reference_declarator (cv_qualifiers, target,
3993 true, attributes);
3994 gcc_unreachable ();
3995 }
3996
3997 /* Create a new C++ parser. */
3998
3999 static cp_parser *
4000 cp_parser_new (void)
4001 {
4002 cp_parser *parser;
4003 cp_lexer *lexer;
4004 unsigned i;
4005
4006 /* cp_lexer_new_main is called before doing GC allocation because
4007 cp_lexer_new_main might load a PCH file. */
4008 lexer = cp_lexer_new_main ();
4009
4010 /* Initialize the binops_by_token so that we can get the tree
4011 directly from the token. */
4012 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
4013 binops_by_token[binops[i].token_type] = binops[i];
4014
4015 parser = ggc_cleared_alloc<cp_parser> ();
4016 parser->lexer = lexer;
4017 parser->context = cp_parser_context_new (NULL);
4018
4019 /* For now, we always accept GNU extensions. */
4020 parser->allow_gnu_extensions_p = 1;
4021
4022 /* The `>' token is a greater-than operator, not the end of a
4023 template-id. */
4024 parser->greater_than_is_operator_p = true;
4025
4026 parser->default_arg_ok_p = true;
4027
4028 /* We are not parsing a constant-expression. */
4029 parser->integral_constant_expression_p = false;
4030 parser->allow_non_integral_constant_expression_p = false;
4031 parser->non_integral_constant_expression_p = false;
4032
4033 /* Local variable names are not forbidden. */
4034 parser->local_variables_forbidden_p = 0;
4035
4036 /* We are not processing an `extern "C"' declaration. */
4037 parser->in_unbraced_linkage_specification_p = false;
4038
4039 /* We are not processing a declarator. */
4040 parser->in_declarator_p = false;
4041
4042 /* We are not processing a template-argument-list. */
4043 parser->in_template_argument_list_p = false;
4044
4045 /* We are not in an iteration statement. */
4046 parser->in_statement = 0;
4047
4048 /* We are not in a switch statement. */
4049 parser->in_switch_statement_p = false;
4050
4051 /* We are not parsing a type-id inside an expression. */
4052 parser->in_type_id_in_expr_p = false;
4053
4054 /* String literals should be translated to the execution character set. */
4055 parser->translate_strings_p = true;
4056
4057 /* We are not parsing a function body. */
4058 parser->in_function_body = false;
4059
4060 /* We can correct until told otherwise. */
4061 parser->colon_corrects_to_scope_p = true;
4062
4063 /* The unparsed function queue is empty. */
4064 push_unparsed_function_queues (parser);
4065
4066 /* There are no classes being defined. */
4067 parser->num_classes_being_defined = 0;
4068
4069 /* No template parameters apply. */
4070 parser->num_template_parameter_lists = 0;
4071
4072 /* Special parsing data structures. */
4073 parser->omp_declare_simd = NULL;
4074 parser->oacc_routine = NULL;
4075
4076 /* Not declaring an implicit function template. */
4077 parser->auto_is_implicit_function_template_parm_p = false;
4078 parser->fully_implicit_function_template_p = false;
4079 parser->implicit_template_parms = 0;
4080 parser->implicit_template_scope = 0;
4081
4082 /* Allow constrained-type-specifiers. */
4083 parser->prevent_constrained_type_specifiers = 0;
4084
4085 /* We haven't yet seen an 'extern "C"'. */
4086 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4087
4088 return parser;
4089 }
4090
4091 /* Create a cp_lexer structure which will emit the tokens in CACHE
4092 and push it onto the parser's lexer stack. This is used for delayed
4093 parsing of in-class method bodies and default arguments, and should
4094 not be confused with tentative parsing. */
4095 static void
4096 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4097 {
4098 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4099 lexer->next = parser->lexer;
4100 parser->lexer = lexer;
4101
4102 /* Move the current source position to that of the first token in the
4103 new lexer. */
4104 cp_lexer_set_source_position_from_token (lexer->next_token);
4105 }
4106
4107 /* Pop the top lexer off the parser stack. This is never used for the
4108 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4109 static void
4110 cp_parser_pop_lexer (cp_parser *parser)
4111 {
4112 cp_lexer *lexer = parser->lexer;
4113 parser->lexer = lexer->next;
4114 cp_lexer_destroy (lexer);
4115
4116 /* Put the current source position back where it was before this
4117 lexer was pushed. */
4118 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4119 }
4120
4121 /* Lexical conventions [gram.lex] */
4122
4123 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4124 identifier. */
4125
4126 static cp_expr
4127 cp_parser_identifier (cp_parser* parser)
4128 {
4129 cp_token *token;
4130
4131 /* Look for the identifier. */
4132 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4133 /* Return the value. */
4134 if (token)
4135 return cp_expr (token->u.value, token->location);
4136 else
4137 return error_mark_node;
4138 }
4139
4140 /* Parse a sequence of adjacent string constants. Returns a
4141 TREE_STRING representing the combined, nul-terminated string
4142 constant. If TRANSLATE is true, translate the string to the
4143 execution character set. If WIDE_OK is true, a wide string is
4144 invalid here.
4145
4146 C++98 [lex.string] says that if a narrow string literal token is
4147 adjacent to a wide string literal token, the behavior is undefined.
4148 However, C99 6.4.5p4 says that this results in a wide string literal.
4149 We follow C99 here, for consistency with the C front end.
4150
4151 This code is largely lifted from lex_string() in c-lex.c.
4152
4153 FUTURE: ObjC++ will need to handle @-strings here. */
4154 static cp_expr
4155 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4156 bool lookup_udlit = true)
4157 {
4158 tree value;
4159 size_t count;
4160 struct obstack str_ob;
4161 struct obstack loc_ob;
4162 cpp_string str, istr, *strs;
4163 cp_token *tok;
4164 enum cpp_ttype type, curr_type;
4165 int have_suffix_p = 0;
4166 tree string_tree;
4167 tree suffix_id = NULL_TREE;
4168 bool curr_tok_is_userdef_p = false;
4169
4170 tok = cp_lexer_peek_token (parser->lexer);
4171 if (!cp_parser_is_string_literal (tok))
4172 {
4173 cp_parser_error (parser, "expected string-literal");
4174 return error_mark_node;
4175 }
4176
4177 location_t loc = tok->location;
4178
4179 if (cpp_userdef_string_p (tok->type))
4180 {
4181 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4182 curr_type = cpp_userdef_string_remove_type (tok->type);
4183 curr_tok_is_userdef_p = true;
4184 }
4185 else
4186 {
4187 string_tree = tok->u.value;
4188 curr_type = tok->type;
4189 }
4190 type = curr_type;
4191
4192 /* Try to avoid the overhead of creating and destroying an obstack
4193 for the common case of just one string. */
4194 if (!cp_parser_is_string_literal
4195 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4196 {
4197 cp_lexer_consume_token (parser->lexer);
4198
4199 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4200 str.len = TREE_STRING_LENGTH (string_tree);
4201 count = 1;
4202
4203 if (curr_tok_is_userdef_p)
4204 {
4205 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4206 have_suffix_p = 1;
4207 curr_type = cpp_userdef_string_remove_type (tok->type);
4208 }
4209 else
4210 curr_type = tok->type;
4211
4212 strs = &str;
4213 }
4214 else
4215 {
4216 location_t last_tok_loc = tok->location;
4217 gcc_obstack_init (&str_ob);
4218 gcc_obstack_init (&loc_ob);
4219 count = 0;
4220
4221 do
4222 {
4223 cp_lexer_consume_token (parser->lexer);
4224 count++;
4225 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4226 str.len = TREE_STRING_LENGTH (string_tree);
4227
4228 if (curr_tok_is_userdef_p)
4229 {
4230 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4231 if (have_suffix_p == 0)
4232 {
4233 suffix_id = curr_suffix_id;
4234 have_suffix_p = 1;
4235 }
4236 else if (have_suffix_p == 1
4237 && curr_suffix_id != suffix_id)
4238 {
4239 error ("inconsistent user-defined literal suffixes"
4240 " %qD and %qD in string literal",
4241 suffix_id, curr_suffix_id);
4242 have_suffix_p = -1;
4243 }
4244 curr_type = cpp_userdef_string_remove_type (tok->type);
4245 }
4246 else
4247 curr_type = tok->type;
4248
4249 if (type != curr_type)
4250 {
4251 if (type == CPP_STRING)
4252 type = curr_type;
4253 else if (curr_type != CPP_STRING)
4254 {
4255 rich_location rich_loc (line_table, tok->location);
4256 rich_loc.add_range (last_tok_loc);
4257 error_at (&rich_loc,
4258 "unsupported non-standard concatenation "
4259 "of string literals");
4260 }
4261 }
4262
4263 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4264 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4265
4266 last_tok_loc = tok->location;
4267
4268 tok = cp_lexer_peek_token (parser->lexer);
4269 if (cpp_userdef_string_p (tok->type))
4270 {
4271 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4272 curr_type = cpp_userdef_string_remove_type (tok->type);
4273 curr_tok_is_userdef_p = true;
4274 }
4275 else
4276 {
4277 string_tree = tok->u.value;
4278 curr_type = tok->type;
4279 curr_tok_is_userdef_p = false;
4280 }
4281 }
4282 while (cp_parser_is_string_literal (tok));
4283
4284 /* A string literal built by concatenation has its caret=start at
4285 the start of the initial string, and its finish at the finish of
4286 the final string literal. */
4287 loc = make_location (loc, loc, get_finish (last_tok_loc));
4288
4289 strs = (cpp_string *) obstack_finish (&str_ob);
4290 }
4291
4292 if (type != CPP_STRING && !wide_ok)
4293 {
4294 cp_parser_error (parser, "a wide string is invalid in this context");
4295 type = CPP_STRING;
4296 }
4297
4298 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4299 (parse_in, strs, count, &istr, type))
4300 {
4301 value = build_string (istr.len, (const char *)istr.text);
4302 free (CONST_CAST (unsigned char *, istr.text));
4303 if (count > 1)
4304 {
4305 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4306 gcc_assert (g_string_concat_db);
4307 g_string_concat_db->record_string_concatenation (count, locs);
4308 }
4309
4310 switch (type)
4311 {
4312 default:
4313 case CPP_STRING:
4314 TREE_TYPE (value) = char_array_type_node;
4315 break;
4316 case CPP_UTF8STRING:
4317 if (flag_char8_t)
4318 TREE_TYPE (value) = char8_array_type_node;
4319 else
4320 TREE_TYPE (value) = char_array_type_node;
4321 break;
4322 case CPP_STRING16:
4323 TREE_TYPE (value) = char16_array_type_node;
4324 break;
4325 case CPP_STRING32:
4326 TREE_TYPE (value) = char32_array_type_node;
4327 break;
4328 case CPP_WSTRING:
4329 TREE_TYPE (value) = wchar_array_type_node;
4330 break;
4331 }
4332
4333 value = fix_string_type (value);
4334
4335 if (have_suffix_p)
4336 {
4337 tree literal = build_userdef_literal (suffix_id, value,
4338 OT_NONE, NULL_TREE);
4339 if (lookup_udlit)
4340 value = cp_parser_userdef_string_literal (literal);
4341 else
4342 value = literal;
4343 }
4344 }
4345 else
4346 /* cpp_interpret_string has issued an error. */
4347 value = error_mark_node;
4348
4349 if (count > 1)
4350 {
4351 obstack_free (&str_ob, 0);
4352 obstack_free (&loc_ob, 0);
4353 }
4354
4355 return cp_expr (value, loc);
4356 }
4357
4358 /* Look up a literal operator with the name and the exact arguments. */
4359
4360 static tree
4361 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4362 {
4363 tree decl = lookup_name (name);
4364 if (!decl || !is_overloaded_fn (decl))
4365 return error_mark_node;
4366
4367 for (lkp_iterator iter (decl); iter; ++iter)
4368 {
4369 tree fn = *iter;
4370
4371 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4372 {
4373 unsigned int ix;
4374 bool found = true;
4375
4376 for (ix = 0;
4377 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4378 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4379 {
4380 tree tparm = TREE_VALUE (parmtypes);
4381 tree targ = TREE_TYPE ((*args)[ix]);
4382 bool ptr = TYPE_PTR_P (tparm);
4383 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4384 if ((ptr || arr || !same_type_p (tparm, targ))
4385 && (!ptr || !arr
4386 || !same_type_p (TREE_TYPE (tparm),
4387 TREE_TYPE (targ))))
4388 found = false;
4389 }
4390
4391 if (found
4392 && ix == vec_safe_length (args)
4393 /* May be this should be sufficient_parms_p instead,
4394 depending on how exactly should user-defined literals
4395 work in presence of default arguments on the literal
4396 operator parameters. */
4397 && parmtypes == void_list_node)
4398 return decl;
4399 }
4400 }
4401
4402 return error_mark_node;
4403 }
4404
4405 /* Parse a user-defined char constant. Returns a call to a user-defined
4406 literal operator taking the character as an argument. */
4407
4408 static cp_expr
4409 cp_parser_userdef_char_literal (cp_parser *parser)
4410 {
4411 cp_token *token = cp_lexer_consume_token (parser->lexer);
4412 tree literal = token->u.value;
4413 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4414 tree value = USERDEF_LITERAL_VALUE (literal);
4415 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4416 tree decl, result;
4417
4418 /* Build up a call to the user-defined operator */
4419 /* Lookup the name we got back from the id-expression. */
4420 releasing_vec args;
4421 vec_safe_push (args, value);
4422 decl = lookup_literal_operator (name, args);
4423 if (!decl || decl == error_mark_node)
4424 {
4425 error ("unable to find character literal operator %qD with %qT argument",
4426 name, TREE_TYPE (value));
4427 return error_mark_node;
4428 }
4429 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4430 return result;
4431 }
4432
4433 /* A subroutine of cp_parser_userdef_numeric_literal to
4434 create a char... template parameter pack from a string node. */
4435
4436 static tree
4437 make_char_string_pack (tree value)
4438 {
4439 tree charvec;
4440 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4441 const char *str = TREE_STRING_POINTER (value);
4442 int i, len = TREE_STRING_LENGTH (value) - 1;
4443 tree argvec = make_tree_vec (1);
4444
4445 /* Fill in CHARVEC with all of the parameters. */
4446 charvec = make_tree_vec (len);
4447 for (i = 0; i < len; ++i)
4448 {
4449 unsigned char s[3] = { '\'', str[i], '\'' };
4450 cpp_string in = { 3, s };
4451 cpp_string out = { 0, 0 };
4452 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4453 return NULL_TREE;
4454 gcc_assert (out.len == 2);
4455 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4456 out.text[0]);
4457 }
4458
4459 /* Build the argument packs. */
4460 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4461
4462 TREE_VEC_ELT (argvec, 0) = argpack;
4463
4464 return argvec;
4465 }
4466
4467 /* A subroutine of cp_parser_userdef_numeric_literal to
4468 create a char... template parameter pack from a string node. */
4469
4470 static tree
4471 make_string_pack (tree value)
4472 {
4473 tree charvec;
4474 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4475 const unsigned char *str
4476 = (const unsigned char *) TREE_STRING_POINTER (value);
4477 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4478 int len = TREE_STRING_LENGTH (value) / sz - 1;
4479 tree argvec = make_tree_vec (2);
4480
4481 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4482 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4483
4484 /* First template parm is character type. */
4485 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4486
4487 /* Fill in CHARVEC with all of the parameters. */
4488 charvec = make_tree_vec (len);
4489 for (int i = 0; i < len; ++i)
4490 TREE_VEC_ELT (charvec, i)
4491 = double_int_to_tree (str_char_type_node,
4492 double_int::from_buffer (str + i * sz, sz));
4493
4494 /* Build the argument packs. */
4495 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4496
4497 TREE_VEC_ELT (argvec, 1) = argpack;
4498
4499 return argvec;
4500 }
4501
4502 /* Parse a user-defined numeric constant. returns a call to a user-defined
4503 literal operator. */
4504
4505 static cp_expr
4506 cp_parser_userdef_numeric_literal (cp_parser *parser)
4507 {
4508 cp_token *token = cp_lexer_consume_token (parser->lexer);
4509 tree literal = token->u.value;
4510 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4511 tree value = USERDEF_LITERAL_VALUE (literal);
4512 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4513 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4514 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4515 tree decl, result;
4516
4517 /* Look for a literal operator taking the exact type of numeric argument
4518 as the literal value. */
4519 releasing_vec args;
4520 vec_safe_push (args, value);
4521 decl = lookup_literal_operator (name, args);
4522 if (decl && decl != error_mark_node)
4523 {
4524 result = finish_call_expr (decl, &args, false, true,
4525 tf_warning_or_error);
4526
4527 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4528 {
4529 warning_at (token->location, OPT_Woverflow,
4530 "integer literal exceeds range of %qT type",
4531 long_long_unsigned_type_node);
4532 }
4533 else
4534 {
4535 if (overflow > 0)
4536 warning_at (token->location, OPT_Woverflow,
4537 "floating literal exceeds range of %qT type",
4538 long_double_type_node);
4539 else if (overflow < 0)
4540 warning_at (token->location, OPT_Woverflow,
4541 "floating literal truncated to zero");
4542 }
4543
4544 return result;
4545 }
4546
4547 /* If the numeric argument didn't work, look for a raw literal
4548 operator taking a const char* argument consisting of the number
4549 in string format. */
4550 args->truncate (0);
4551 vec_safe_push (args, num_string);
4552 decl = lookup_literal_operator (name, args);
4553 if (decl && decl != error_mark_node)
4554 {
4555 result = finish_call_expr (decl, &args, false, true,
4556 tf_warning_or_error);
4557 return result;
4558 }
4559
4560 /* If the raw literal didn't work, look for a non-type template
4561 function with parameter pack char.... Call the function with
4562 template parameter characters representing the number. */
4563 args->truncate (0);
4564 decl = lookup_literal_operator (name, args);
4565 if (decl && decl != error_mark_node)
4566 {
4567 tree tmpl_args = make_char_string_pack (num_string);
4568 if (tmpl_args == NULL_TREE)
4569 {
4570 error ("failed to translate literal to execution character set %qT",
4571 num_string);
4572 return error_mark_node;
4573 }
4574 decl = lookup_template_function (decl, tmpl_args);
4575 result = finish_call_expr (decl, &args, false, true,
4576 tf_warning_or_error);
4577 return result;
4578 }
4579
4580 /* In C++14 the standard library defines complex number suffixes that
4581 conflict with GNU extensions. Prefer them if <complex> is #included. */
4582 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4583 bool i14 = (cxx_dialect > cxx11
4584 && (id_equal (suffix_id, "i")
4585 || id_equal (suffix_id, "if")
4586 || id_equal (suffix_id, "il")));
4587 diagnostic_t kind = DK_ERROR;
4588 int opt = 0;
4589
4590 if (i14 && ext)
4591 {
4592 tree cxlit = lookup_qualified_name (std_node, "complex_literals",
4593 0, false);
4594 if (cxlit == error_mark_node)
4595 {
4596 /* No <complex>, so pedwarn and use GNU semantics. */
4597 kind = DK_PEDWARN;
4598 opt = OPT_Wpedantic;
4599 }
4600 }
4601
4602 bool complained
4603 = emit_diagnostic (kind, input_location, opt,
4604 "unable to find numeric literal operator %qD", name);
4605
4606 if (!complained)
4607 /* Don't inform either. */;
4608 else if (i14)
4609 {
4610 inform (token->location, "add %<using namespace std::complex_literals%> "
4611 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4612 "suffixes");
4613 if (ext)
4614 inform (token->location, "or use %<j%> instead of %<i%> for the "
4615 "GNU built-in suffix");
4616 }
4617 else if (!ext)
4618 inform (token->location, "use %<-fext-numeric-literals%> "
4619 "to enable more built-in suffixes");
4620
4621 if (kind == DK_ERROR)
4622 value = error_mark_node;
4623 else
4624 {
4625 /* Use the built-in semantics. */
4626 tree type;
4627 if (id_equal (suffix_id, "i"))
4628 {
4629 if (TREE_CODE (value) == INTEGER_CST)
4630 type = integer_type_node;
4631 else
4632 type = double_type_node;
4633 }
4634 else if (id_equal (suffix_id, "if"))
4635 type = float_type_node;
4636 else /* if (id_equal (suffix_id, "il")) */
4637 type = long_double_type_node;
4638
4639 value = build_complex (build_complex_type (type),
4640 fold_convert (type, integer_zero_node),
4641 fold_convert (type, value));
4642 }
4643
4644 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4645 /* Avoid repeated diagnostics. */
4646 token->u.value = value;
4647 return value;
4648 }
4649
4650 /* Parse a user-defined string constant. Returns a call to a user-defined
4651 literal operator taking a character pointer and the length of the string
4652 as arguments. */
4653
4654 static tree
4655 cp_parser_userdef_string_literal (tree literal)
4656 {
4657 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4658 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4659 tree value = USERDEF_LITERAL_VALUE (literal);
4660 int len = TREE_STRING_LENGTH (value)
4661 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4662 tree decl;
4663
4664 /* Build up a call to the user-defined operator. */
4665 /* Lookup the name we got back from the id-expression. */
4666 releasing_vec args;
4667 vec_safe_push (args, value);
4668 vec_safe_push (args, build_int_cst (size_type_node, len));
4669 decl = lookup_literal_operator (name, args);
4670
4671 if (decl && decl != error_mark_node)
4672 return finish_call_expr (decl, &args, false, true,
4673 tf_warning_or_error);
4674
4675 /* Look for a suitable template function, either (C++20) with a single
4676 parameter of class type, or (N3599) with typename parameter CharT and
4677 parameter pack CharT... */
4678 args->truncate (0);
4679 decl = lookup_literal_operator (name, args);
4680 if (decl && decl != error_mark_node)
4681 {
4682 /* Use resolve_nondeduced_context to try to choose one form of template
4683 or the other. */
4684 tree tmpl_args = make_tree_vec (1);
4685 TREE_VEC_ELT (tmpl_args, 0) = value;
4686 decl = lookup_template_function (decl, tmpl_args);
4687 tree res = resolve_nondeduced_context (decl, tf_none);
4688 if (DECL_P (res))
4689 decl = res;
4690 else
4691 {
4692 TREE_OPERAND (decl, 1) = make_string_pack (value);
4693 res = resolve_nondeduced_context (decl, tf_none);
4694 if (DECL_P (res))
4695 decl = res;
4696 }
4697 if (!DECL_P (decl) && cxx_dialect > cxx17)
4698 TREE_OPERAND (decl, 1) = tmpl_args;
4699 return finish_call_expr (decl, &args, false, true,
4700 tf_warning_or_error);
4701 }
4702
4703 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4704 name, TREE_TYPE (value), size_type_node);
4705 return error_mark_node;
4706 }
4707
4708
4709 /* Basic concepts [gram.basic] */
4710
4711 /* Parse a translation-unit.
4712
4713 translation-unit:
4714 declaration-seq [opt] */
4715
4716 static void
4717 cp_parser_translation_unit (cp_parser* parser)
4718 {
4719 gcc_checking_assert (!cp_error_declarator);
4720
4721 /* Create the declarator obstack. */
4722 gcc_obstack_init (&declarator_obstack);
4723 /* Create the error declarator. */
4724 cp_error_declarator = make_declarator (cdk_error);
4725 /* Create the empty parameter list. */
4726 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4727 UNKNOWN_LOCATION);
4728 /* Remember where the base of the declarator obstack lies. */
4729 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4730
4731 bool implicit_extern_c = false;
4732
4733 for (;;)
4734 {
4735 cp_token *token = cp_lexer_peek_token (parser->lexer);
4736
4737 /* If we're entering or exiting a region that's implicitly
4738 extern "C", modify the lang context appropriately. */
4739 if (implicit_extern_c
4740 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4741 {
4742 implicit_extern_c = !implicit_extern_c;
4743 if (implicit_extern_c)
4744 push_lang_context (lang_name_c);
4745 else
4746 pop_lang_context ();
4747 }
4748
4749 if (token->type == CPP_EOF)
4750 break;
4751
4752 if (token->type == CPP_CLOSE_BRACE)
4753 {
4754 cp_parser_error (parser, "expected declaration");
4755 cp_lexer_consume_token (parser->lexer);
4756 /* If the next token is now a `;', consume it. */
4757 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4758 cp_lexer_consume_token (parser->lexer);
4759 }
4760 else
4761 cp_parser_toplevel_declaration (parser);
4762 }
4763
4764 /* Get rid of the token array; we don't need it any more. */
4765 cp_lexer_destroy (parser->lexer);
4766 parser->lexer = NULL;
4767
4768 /* The EOF should have reset this. */
4769 gcc_checking_assert (!implicit_extern_c);
4770
4771 /* Make sure the declarator obstack was fully cleaned up. */
4772 gcc_assert (obstack_next_free (&declarator_obstack)
4773 == declarator_obstack_base);
4774 }
4775
4776 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4777 decltype context. */
4778
4779 static inline tsubst_flags_t
4780 complain_flags (bool decltype_p)
4781 {
4782 tsubst_flags_t complain = tf_warning_or_error;
4783 if (decltype_p)
4784 complain |= tf_decltype;
4785 return complain;
4786 }
4787
4788 /* We're about to parse a collection of statements. If we're currently
4789 parsing tentatively, set up a firewall so that any nested
4790 cp_parser_commit_to_tentative_parse won't affect the current context. */
4791
4792 static cp_token_position
4793 cp_parser_start_tentative_firewall (cp_parser *parser)
4794 {
4795 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4796 return 0;
4797
4798 cp_parser_parse_tentatively (parser);
4799 cp_parser_commit_to_topmost_tentative_parse (parser);
4800 return cp_lexer_token_position (parser->lexer, false);
4801 }
4802
4803 /* We've finished parsing the collection of statements. Wrap up the
4804 firewall and replace the relevant tokens with the parsed form. */
4805
4806 static void
4807 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4808 tree expr)
4809 {
4810 if (!start)
4811 return;
4812
4813 /* Finish the firewall level. */
4814 cp_parser_parse_definitely (parser);
4815 /* And remember the result of the parse for when we try again. */
4816 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4817 token->type = CPP_PREPARSED_EXPR;
4818 token->u.value = expr;
4819 token->keyword = RID_MAX;
4820 cp_lexer_purge_tokens_after (parser->lexer, start);
4821 }
4822
4823 /* Like the above functions, but let the user modify the tokens. Used by
4824 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4825 later parses, so it makes sense to localize the effects of
4826 cp_parser_commit_to_tentative_parse. */
4827
4828 struct tentative_firewall
4829 {
4830 cp_parser *parser;
4831 bool set;
4832
4833 tentative_firewall (cp_parser *p): parser(p)
4834 {
4835 /* If we're currently parsing tentatively, start a committed level as a
4836 firewall and then an inner tentative parse. */
4837 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4838 {
4839 cp_parser_parse_tentatively (parser);
4840 cp_parser_commit_to_topmost_tentative_parse (parser);
4841 cp_parser_parse_tentatively (parser);
4842 }
4843 }
4844
4845 ~tentative_firewall()
4846 {
4847 if (set)
4848 {
4849 /* Finish the inner tentative parse and the firewall, propagating any
4850 uncommitted error state to the outer tentative parse. */
4851 bool err = cp_parser_error_occurred (parser);
4852 cp_parser_parse_definitely (parser);
4853 cp_parser_parse_definitely (parser);
4854 if (err)
4855 cp_parser_simulate_error (parser);
4856 }
4857 }
4858 };
4859
4860 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4861 This class is for tracking such a matching pair of symbols.
4862 In particular, it tracks the location of the first token,
4863 so that if the second token is missing, we can highlight the
4864 location of the first token when notifying the user about the
4865 problem. */
4866
4867 template <typename traits_t>
4868 class token_pair
4869 {
4870 public:
4871 /* token_pair's ctor. */
4872 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4873
4874 /* If the next token is the opening symbol for this pair, consume it and
4875 return true.
4876 Otherwise, issue an error and return false.
4877 In either case, record the location of the opening token. */
4878
4879 bool require_open (cp_parser *parser)
4880 {
4881 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4882 return cp_parser_require (parser, traits_t::open_token_type,
4883 traits_t::required_token_open);
4884 }
4885
4886 /* Consume the next token from PARSER, recording its location as
4887 that of the opening token within the pair. */
4888
4889 cp_token * consume_open (cp_parser *parser)
4890 {
4891 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4892 gcc_assert (tok->type == traits_t::open_token_type);
4893 m_open_loc = tok->location;
4894 return tok;
4895 }
4896
4897 /* If the next token is the closing symbol for this pair, consume it
4898 and return it.
4899 Otherwise, issue an error, highlighting the location of the
4900 corresponding opening token, and return NULL. */
4901
4902 cp_token *require_close (cp_parser *parser) const
4903 {
4904 return cp_parser_require (parser, traits_t::close_token_type,
4905 traits_t::required_token_close,
4906 m_open_loc);
4907 }
4908
4909 location_t open_location () const { return m_open_loc; }
4910
4911 private:
4912 location_t m_open_loc;
4913 };
4914
4915 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4916
4917 struct matching_paren_traits
4918 {
4919 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4920 static const enum required_token required_token_open = RT_OPEN_PAREN;
4921 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4922 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4923 };
4924
4925 /* "matching_parens" is a token_pair<T> class for tracking matching
4926 pairs of parentheses. */
4927
4928 typedef token_pair<matching_paren_traits> matching_parens;
4929
4930 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4931
4932 struct matching_brace_traits
4933 {
4934 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4935 static const enum required_token required_token_open = RT_OPEN_BRACE;
4936 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4937 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4938 };
4939
4940 /* "matching_braces" is a token_pair<T> class for tracking matching
4941 pairs of braces. */
4942
4943 typedef token_pair<matching_brace_traits> matching_braces;
4944
4945
4946 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4947 enclosing parentheses. */
4948
4949 static cp_expr
4950 cp_parser_statement_expr (cp_parser *parser)
4951 {
4952 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4953
4954 /* Consume the '('. */
4955 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4956 matching_parens parens;
4957 parens.consume_open (parser);
4958 /* Start the statement-expression. */
4959 tree expr = begin_stmt_expr ();
4960 /* Parse the compound-statement. */
4961 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4962 /* Finish up. */
4963 expr = finish_stmt_expr (expr, false);
4964 /* Consume the ')'. */
4965 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4966 if (!parens.require_close (parser))
4967 cp_parser_skip_to_end_of_statement (parser);
4968
4969 cp_parser_end_tentative_firewall (parser, start, expr);
4970 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4971 return cp_expr (expr, combined_loc);
4972 }
4973
4974 /* Expressions [gram.expr] */
4975
4976 /* Parse a fold-operator.
4977
4978 fold-operator:
4979 - * / % ^ & | = < > << >>
4980 = -= *= /= %= ^= &= |= <<= >>=
4981 == != <= >= && || , .* ->*
4982
4983 This returns the tree code corresponding to the matched operator
4984 as an int. When the current token matches a compound assignment
4985 operator, the resulting tree code is the negative value of the
4986 non-assignment operator. */
4987
4988 static int
4989 cp_parser_fold_operator (cp_token *token)
4990 {
4991 switch (token->type)
4992 {
4993 case CPP_PLUS: return PLUS_EXPR;
4994 case CPP_MINUS: return MINUS_EXPR;
4995 case CPP_MULT: return MULT_EXPR;
4996 case CPP_DIV: return TRUNC_DIV_EXPR;
4997 case CPP_MOD: return TRUNC_MOD_EXPR;
4998 case CPP_XOR: return BIT_XOR_EXPR;
4999 case CPP_AND: return BIT_AND_EXPR;
5000 case CPP_OR: return BIT_IOR_EXPR;
5001 case CPP_LSHIFT: return LSHIFT_EXPR;
5002 case CPP_RSHIFT: return RSHIFT_EXPR;
5003
5004 case CPP_EQ: return -NOP_EXPR;
5005 case CPP_PLUS_EQ: return -PLUS_EXPR;
5006 case CPP_MINUS_EQ: return -MINUS_EXPR;
5007 case CPP_MULT_EQ: return -MULT_EXPR;
5008 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
5009 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
5010 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
5011 case CPP_AND_EQ: return -BIT_AND_EXPR;
5012 case CPP_OR_EQ: return -BIT_IOR_EXPR;
5013 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
5014 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
5015
5016 case CPP_EQ_EQ: return EQ_EXPR;
5017 case CPP_NOT_EQ: return NE_EXPR;
5018 case CPP_LESS: return LT_EXPR;
5019 case CPP_GREATER: return GT_EXPR;
5020 case CPP_LESS_EQ: return LE_EXPR;
5021 case CPP_GREATER_EQ: return GE_EXPR;
5022
5023 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
5024 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
5025
5026 case CPP_COMMA: return COMPOUND_EXPR;
5027
5028 case CPP_DOT_STAR: return DOTSTAR_EXPR;
5029 case CPP_DEREF_STAR: return MEMBER_REF;
5030
5031 default: return ERROR_MARK;
5032 }
5033 }
5034
5035 /* Returns true if CODE indicates a binary expression, which is not allowed in
5036 the LHS of a fold-expression. More codes will need to be added to use this
5037 function in other contexts. */
5038
5039 static bool
5040 is_binary_op (tree_code code)
5041 {
5042 switch (code)
5043 {
5044 case PLUS_EXPR:
5045 case POINTER_PLUS_EXPR:
5046 case MINUS_EXPR:
5047 case MULT_EXPR:
5048 case TRUNC_DIV_EXPR:
5049 case TRUNC_MOD_EXPR:
5050 case BIT_XOR_EXPR:
5051 case BIT_AND_EXPR:
5052 case BIT_IOR_EXPR:
5053 case LSHIFT_EXPR:
5054 case RSHIFT_EXPR:
5055
5056 case MODOP_EXPR:
5057
5058 case EQ_EXPR:
5059 case NE_EXPR:
5060 case LE_EXPR:
5061 case GE_EXPR:
5062 case LT_EXPR:
5063 case GT_EXPR:
5064
5065 case TRUTH_ANDIF_EXPR:
5066 case TRUTH_ORIF_EXPR:
5067
5068 case COMPOUND_EXPR:
5069
5070 case DOTSTAR_EXPR:
5071 case MEMBER_REF:
5072 return true;
5073
5074 default:
5075 return false;
5076 }
5077 }
5078
5079 /* If the next token is a suitable fold operator, consume it and return as
5080 the function above. */
5081
5082 static int
5083 cp_parser_fold_operator (cp_parser *parser)
5084 {
5085 cp_token* token = cp_lexer_peek_token (parser->lexer);
5086 int code = cp_parser_fold_operator (token);
5087 if (code != ERROR_MARK)
5088 cp_lexer_consume_token (parser->lexer);
5089 return code;
5090 }
5091
5092 /* Parse a fold-expression.
5093
5094 fold-expression:
5095 ( ... folding-operator cast-expression)
5096 ( cast-expression folding-operator ... )
5097 ( cast-expression folding operator ... folding-operator cast-expression)
5098
5099 Note that the '(' and ')' are matched in primary expression. */
5100
5101 static cp_expr
5102 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5103 {
5104 cp_id_kind pidk;
5105
5106 // Left fold.
5107 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5108 {
5109 cp_lexer_consume_token (parser->lexer);
5110 int op = cp_parser_fold_operator (parser);
5111 if (op == ERROR_MARK)
5112 {
5113 cp_parser_error (parser, "expected binary operator");
5114 return error_mark_node;
5115 }
5116
5117 tree expr = cp_parser_cast_expression (parser, false, false,
5118 false, &pidk);
5119 if (expr == error_mark_node)
5120 return error_mark_node;
5121 return finish_left_unary_fold_expr (expr, op);
5122 }
5123
5124 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5125 int op = cp_parser_fold_operator (parser);
5126 if (op == ERROR_MARK)
5127 {
5128 cp_parser_error (parser, "expected binary operator");
5129 return error_mark_node;
5130 }
5131
5132 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5133 {
5134 cp_parser_error (parser, "expected ...");
5135 return error_mark_node;
5136 }
5137 cp_lexer_consume_token (parser->lexer);
5138
5139 /* The operands of a fold-expression are cast-expressions, so binary or
5140 conditional expressions are not allowed. We check this here to avoid
5141 tentative parsing. */
5142 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5143 /* OK, the expression was parenthesized. */;
5144 else if (is_binary_op (TREE_CODE (expr1)))
5145 error_at (location_of (expr1),
5146 "binary expression in operand of fold-expression");
5147 else if (TREE_CODE (expr1) == COND_EXPR
5148 || (REFERENCE_REF_P (expr1)
5149 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5150 error_at (location_of (expr1),
5151 "conditional expression in operand of fold-expression");
5152
5153 // Right fold.
5154 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5155 return finish_right_unary_fold_expr (expr1, op);
5156
5157 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5158 {
5159 cp_parser_error (parser, "mismatched operator in fold-expression");
5160 return error_mark_node;
5161 }
5162 cp_lexer_consume_token (parser->lexer);
5163
5164 // Binary left or right fold.
5165 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5166 if (expr2 == error_mark_node)
5167 return error_mark_node;
5168 return finish_binary_fold_expr (expr1, expr2, op);
5169 }
5170
5171 /* Parse a primary-expression.
5172
5173 primary-expression:
5174 literal
5175 this
5176 ( expression )
5177 id-expression
5178 lambda-expression (C++11)
5179
5180 GNU Extensions:
5181
5182 primary-expression:
5183 ( compound-statement )
5184 __builtin_va_arg ( assignment-expression , type-id )
5185 __builtin_offsetof ( type-id , offsetof-expression )
5186
5187 C++ Extensions:
5188 __has_nothrow_assign ( type-id )
5189 __has_nothrow_constructor ( type-id )
5190 __has_nothrow_copy ( type-id )
5191 __has_trivial_assign ( type-id )
5192 __has_trivial_constructor ( type-id )
5193 __has_trivial_copy ( type-id )
5194 __has_trivial_destructor ( type-id )
5195 __has_virtual_destructor ( type-id )
5196 __is_abstract ( type-id )
5197 __is_base_of ( type-id , type-id )
5198 __is_class ( type-id )
5199 __is_empty ( type-id )
5200 __is_enum ( type-id )
5201 __is_final ( type-id )
5202 __is_literal_type ( type-id )
5203 __is_pod ( type-id )
5204 __is_polymorphic ( type-id )
5205 __is_std_layout ( type-id )
5206 __is_trivial ( type-id )
5207 __is_union ( type-id )
5208
5209 Objective-C++ Extension:
5210
5211 primary-expression:
5212 objc-expression
5213
5214 literal:
5215 __null
5216
5217 ADDRESS_P is true iff this expression was immediately preceded by
5218 "&" and therefore might denote a pointer-to-member. CAST_P is true
5219 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5220 true iff this expression is a template argument.
5221
5222 Returns a representation of the expression. Upon return, *IDK
5223 indicates what kind of id-expression (if any) was present. */
5224
5225 static cp_expr
5226 cp_parser_primary_expression (cp_parser *parser,
5227 bool address_p,
5228 bool cast_p,
5229 bool template_arg_p,
5230 bool decltype_p,
5231 cp_id_kind *idk)
5232 {
5233 cp_token *token = NULL;
5234
5235 /* Assume the primary expression is not an id-expression. */
5236 *idk = CP_ID_KIND_NONE;
5237
5238 /* Peek at the next token. */
5239 token = cp_lexer_peek_token (parser->lexer);
5240 switch ((int) token->type)
5241 {
5242 /* literal:
5243 integer-literal
5244 character-literal
5245 floating-literal
5246 string-literal
5247 boolean-literal
5248 pointer-literal
5249 user-defined-literal */
5250 case CPP_CHAR:
5251 case CPP_CHAR16:
5252 case CPP_CHAR32:
5253 case CPP_WCHAR:
5254 case CPP_UTF8CHAR:
5255 case CPP_NUMBER:
5256 case CPP_PREPARSED_EXPR:
5257 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5258 return cp_parser_userdef_numeric_literal (parser);
5259 token = cp_lexer_consume_token (parser->lexer);
5260 if (TREE_CODE (token->u.value) == FIXED_CST)
5261 {
5262 error_at (token->location,
5263 "fixed-point types not supported in C++");
5264 return error_mark_node;
5265 }
5266 /* Floating-point literals are only allowed in an integral
5267 constant expression if they are cast to an integral or
5268 enumeration type. */
5269 if (TREE_CODE (token->u.value) == REAL_CST
5270 && parser->integral_constant_expression_p
5271 && pedantic)
5272 {
5273 /* CAST_P will be set even in invalid code like "int(2.7 +
5274 ...)". Therefore, we have to check that the next token
5275 is sure to end the cast. */
5276 if (cast_p)
5277 {
5278 cp_token *next_token;
5279
5280 next_token = cp_lexer_peek_token (parser->lexer);
5281 if (/* The comma at the end of an
5282 enumerator-definition. */
5283 next_token->type != CPP_COMMA
5284 /* The curly brace at the end of an enum-specifier. */
5285 && next_token->type != CPP_CLOSE_BRACE
5286 /* The end of a statement. */
5287 && next_token->type != CPP_SEMICOLON
5288 /* The end of the cast-expression. */
5289 && next_token->type != CPP_CLOSE_PAREN
5290 /* The end of an array bound. */
5291 && next_token->type != CPP_CLOSE_SQUARE
5292 /* The closing ">" in a template-argument-list. */
5293 && (next_token->type != CPP_GREATER
5294 || parser->greater_than_is_operator_p)
5295 /* C++0x only: A ">>" treated like two ">" tokens,
5296 in a template-argument-list. */
5297 && (next_token->type != CPP_RSHIFT
5298 || (cxx_dialect == cxx98)
5299 || parser->greater_than_is_operator_p))
5300 cast_p = false;
5301 }
5302
5303 /* If we are within a cast, then the constraint that the
5304 cast is to an integral or enumeration type will be
5305 checked at that point. If we are not within a cast, then
5306 this code is invalid. */
5307 if (!cast_p)
5308 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5309 }
5310 return (cp_expr (token->u.value, token->location)
5311 .maybe_add_location_wrapper ());
5312
5313 case CPP_CHAR_USERDEF:
5314 case CPP_CHAR16_USERDEF:
5315 case CPP_CHAR32_USERDEF:
5316 case CPP_WCHAR_USERDEF:
5317 case CPP_UTF8CHAR_USERDEF:
5318 return cp_parser_userdef_char_literal (parser);
5319
5320 case CPP_STRING:
5321 case CPP_STRING16:
5322 case CPP_STRING32:
5323 case CPP_WSTRING:
5324 case CPP_UTF8STRING:
5325 case CPP_STRING_USERDEF:
5326 case CPP_STRING16_USERDEF:
5327 case CPP_STRING32_USERDEF:
5328 case CPP_WSTRING_USERDEF:
5329 case CPP_UTF8STRING_USERDEF:
5330 /* ??? Should wide strings be allowed when parser->translate_strings_p
5331 is false (i.e. in attributes)? If not, we can kill the third
5332 argument to cp_parser_string_literal. */
5333 return (cp_parser_string_literal (parser,
5334 parser->translate_strings_p,
5335 true)
5336 .maybe_add_location_wrapper ());
5337
5338 case CPP_OPEN_PAREN:
5339 /* If we see `( { ' then we are looking at the beginning of
5340 a GNU statement-expression. */
5341 if (cp_parser_allow_gnu_extensions_p (parser)
5342 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5343 {
5344 /* Statement-expressions are not allowed by the standard. */
5345 pedwarn (token->location, OPT_Wpedantic,
5346 "ISO C++ forbids braced-groups within expressions");
5347
5348 /* And they're not allowed outside of a function-body; you
5349 cannot, for example, write:
5350
5351 int i = ({ int j = 3; j + 1; });
5352
5353 at class or namespace scope. */
5354 if (!parser->in_function_body
5355 || parser->in_template_argument_list_p)
5356 {
5357 error_at (token->location,
5358 "statement-expressions are not allowed outside "
5359 "functions nor in template-argument lists");
5360 cp_parser_skip_to_end_of_block_or_statement (parser);
5361 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5362 cp_lexer_consume_token (parser->lexer);
5363 return error_mark_node;
5364 }
5365 else
5366 return cp_parser_statement_expr (parser);
5367 }
5368 /* Otherwise it's a normal parenthesized expression. */
5369 {
5370 cp_expr expr;
5371 bool saved_greater_than_is_operator_p;
5372
5373 location_t open_paren_loc = token->location;
5374
5375 /* Consume the `('. */
5376 matching_parens parens;
5377 parens.consume_open (parser);
5378 /* Within a parenthesized expression, a `>' token is always
5379 the greater-than operator. */
5380 saved_greater_than_is_operator_p
5381 = parser->greater_than_is_operator_p;
5382 parser->greater_than_is_operator_p = true;
5383
5384 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5385 /* Left fold expression. */
5386 expr = NULL_TREE;
5387 else
5388 /* Parse the parenthesized expression. */
5389 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5390
5391 token = cp_lexer_peek_token (parser->lexer);
5392 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5393 {
5394 expr = cp_parser_fold_expression (parser, expr);
5395 if (expr != error_mark_node
5396 && cxx_dialect < cxx17)
5397 pedwarn (input_location, 0, "fold-expressions only available "
5398 "with %<-std=c++17%> or %<-std=gnu++17%>");
5399 }
5400 else
5401 /* Let the front end know that this expression was
5402 enclosed in parentheses. This matters in case, for
5403 example, the expression is of the form `A::B', since
5404 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5405 not. */
5406 expr = finish_parenthesized_expr (expr);
5407
5408 /* DR 705: Wrapping an unqualified name in parentheses
5409 suppresses arg-dependent lookup. We want to pass back
5410 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5411 (c++/37862), but none of the others. */
5412 if (*idk != CP_ID_KIND_QUALIFIED)
5413 *idk = CP_ID_KIND_NONE;
5414
5415 /* The `>' token might be the end of a template-id or
5416 template-parameter-list now. */
5417 parser->greater_than_is_operator_p
5418 = saved_greater_than_is_operator_p;
5419
5420 /* Consume the `)'. */
5421 token = cp_lexer_peek_token (parser->lexer);
5422 location_t close_paren_loc = token->location;
5423 expr.set_range (open_paren_loc, close_paren_loc);
5424 if (!parens.require_close (parser)
5425 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5426 cp_parser_skip_to_end_of_statement (parser);
5427
5428 return expr;
5429 }
5430
5431 case CPP_OPEN_SQUARE:
5432 {
5433 if (c_dialect_objc ())
5434 {
5435 /* We might have an Objective-C++ message. */
5436 cp_parser_parse_tentatively (parser);
5437 tree msg = cp_parser_objc_message_expression (parser);
5438 /* If that works out, we're done ... */
5439 if (cp_parser_parse_definitely (parser))
5440 return msg;
5441 /* ... else, fall though to see if it's a lambda. */
5442 }
5443 cp_expr lam = cp_parser_lambda_expression (parser);
5444 /* Don't warn about a failed tentative parse. */
5445 if (cp_parser_error_occurred (parser))
5446 return error_mark_node;
5447 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5448 return lam;
5449 }
5450
5451 case CPP_OBJC_STRING:
5452 if (c_dialect_objc ())
5453 /* We have an Objective-C++ string literal. */
5454 return cp_parser_objc_expression (parser);
5455 cp_parser_error (parser, "expected primary-expression");
5456 return error_mark_node;
5457
5458 case CPP_KEYWORD:
5459 switch (token->keyword)
5460 {
5461 /* These two are the boolean literals. */
5462 case RID_TRUE:
5463 cp_lexer_consume_token (parser->lexer);
5464 return cp_expr (boolean_true_node, token->location);
5465 case RID_FALSE:
5466 cp_lexer_consume_token (parser->lexer);
5467 return cp_expr (boolean_false_node, token->location);
5468
5469 /* The `__null' literal. */
5470 case RID_NULL:
5471 cp_lexer_consume_token (parser->lexer);
5472 return cp_expr (null_node, token->location);
5473
5474 /* The `nullptr' literal. */
5475 case RID_NULLPTR:
5476 cp_lexer_consume_token (parser->lexer);
5477 return cp_expr (nullptr_node, token->location);
5478
5479 /* Recognize the `this' keyword. */
5480 case RID_THIS:
5481 cp_lexer_consume_token (parser->lexer);
5482 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5483 {
5484 error_at (token->location,
5485 "%<this%> may not be used in this context");
5486 return error_mark_node;
5487 }
5488 /* Pointers cannot appear in constant-expressions. */
5489 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5490 return error_mark_node;
5491 return cp_expr (finish_this_expr (), token->location);
5492
5493 /* The `operator' keyword can be the beginning of an
5494 id-expression. */
5495 case RID_OPERATOR:
5496 goto id_expression;
5497
5498 case RID_FUNCTION_NAME:
5499 case RID_PRETTY_FUNCTION_NAME:
5500 case RID_C99_FUNCTION_NAME:
5501 {
5502 non_integral_constant name;
5503
5504 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5505 __func__ are the names of variables -- but they are
5506 treated specially. Therefore, they are handled here,
5507 rather than relying on the generic id-expression logic
5508 below. Grammatically, these names are id-expressions.
5509
5510 Consume the token. */
5511 token = cp_lexer_consume_token (parser->lexer);
5512
5513 switch (token->keyword)
5514 {
5515 case RID_FUNCTION_NAME:
5516 name = NIC_FUNC_NAME;
5517 break;
5518 case RID_PRETTY_FUNCTION_NAME:
5519 name = NIC_PRETTY_FUNC;
5520 break;
5521 case RID_C99_FUNCTION_NAME:
5522 name = NIC_C99_FUNC;
5523 break;
5524 default:
5525 gcc_unreachable ();
5526 }
5527
5528 if (cp_parser_non_integral_constant_expression (parser, name))
5529 return error_mark_node;
5530
5531 /* Look up the name. */
5532 return finish_fname (token->u.value);
5533 }
5534
5535 case RID_VA_ARG:
5536 {
5537 tree expression;
5538 tree type;
5539 location_t type_location;
5540 location_t start_loc
5541 = cp_lexer_peek_token (parser->lexer)->location;
5542 /* The `__builtin_va_arg' construct is used to handle
5543 `va_arg'. Consume the `__builtin_va_arg' token. */
5544 cp_lexer_consume_token (parser->lexer);
5545 /* Look for the opening `('. */
5546 matching_parens parens;
5547 parens.require_open (parser);
5548 /* Now, parse the assignment-expression. */
5549 expression = cp_parser_assignment_expression (parser);
5550 /* Look for the `,'. */
5551 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5552 type_location = cp_lexer_peek_token (parser->lexer)->location;
5553 /* Parse the type-id. */
5554 {
5555 type_id_in_expr_sentinel s (parser);
5556 type = cp_parser_type_id (parser);
5557 }
5558 /* Look for the closing `)'. */
5559 location_t finish_loc
5560 = cp_lexer_peek_token (parser->lexer)->location;
5561 parens.require_close (parser);
5562 /* Using `va_arg' in a constant-expression is not
5563 allowed. */
5564 if (cp_parser_non_integral_constant_expression (parser,
5565 NIC_VA_ARG))
5566 return error_mark_node;
5567 /* Construct a location of the form:
5568 __builtin_va_arg (v, int)
5569 ~~~~~~~~~~~~~~~~~~~~~^~~~
5570 with the caret at the type, ranging from the start of the
5571 "__builtin_va_arg" token to the close paren. */
5572 location_t combined_loc
5573 = make_location (type_location, start_loc, finish_loc);
5574 return build_x_va_arg (combined_loc, expression, type);
5575 }
5576
5577 case RID_OFFSETOF:
5578 return cp_parser_builtin_offsetof (parser);
5579
5580 case RID_HAS_NOTHROW_ASSIGN:
5581 case RID_HAS_NOTHROW_CONSTRUCTOR:
5582 case RID_HAS_NOTHROW_COPY:
5583 case RID_HAS_TRIVIAL_ASSIGN:
5584 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5585 case RID_HAS_TRIVIAL_COPY:
5586 case RID_HAS_TRIVIAL_DESTRUCTOR:
5587 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5588 case RID_HAS_VIRTUAL_DESTRUCTOR:
5589 case RID_IS_ABSTRACT:
5590 case RID_IS_AGGREGATE:
5591 case RID_IS_BASE_OF:
5592 case RID_IS_CLASS:
5593 case RID_IS_EMPTY:
5594 case RID_IS_ENUM:
5595 case RID_IS_FINAL:
5596 case RID_IS_LITERAL_TYPE:
5597 case RID_IS_POD:
5598 case RID_IS_POLYMORPHIC:
5599 case RID_IS_SAME_AS:
5600 case RID_IS_STD_LAYOUT:
5601 case RID_IS_TRIVIAL:
5602 case RID_IS_TRIVIALLY_ASSIGNABLE:
5603 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5604 case RID_IS_TRIVIALLY_COPYABLE:
5605 case RID_IS_UNION:
5606 case RID_IS_ASSIGNABLE:
5607 case RID_IS_CONSTRUCTIBLE:
5608 return cp_parser_trait_expr (parser, token->keyword);
5609
5610 // C++ concepts
5611 case RID_REQUIRES:
5612 return cp_parser_requires_expression (parser);
5613
5614 /* Objective-C++ expressions. */
5615 case RID_AT_ENCODE:
5616 case RID_AT_PROTOCOL:
5617 case RID_AT_SELECTOR:
5618 return cp_parser_objc_expression (parser);
5619
5620 case RID_TEMPLATE:
5621 if (parser->in_function_body
5622 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5623 == CPP_LESS))
5624 {
5625 error_at (token->location,
5626 "a template declaration cannot appear at block scope");
5627 cp_parser_skip_to_end_of_block_or_statement (parser);
5628 return error_mark_node;
5629 }
5630 /* FALLTHRU */
5631 default:
5632 cp_parser_error (parser, "expected primary-expression");
5633 return error_mark_node;
5634 }
5635
5636 /* An id-expression can start with either an identifier, a
5637 `::' as the beginning of a qualified-id, or the "operator"
5638 keyword. */
5639 case CPP_NAME:
5640 case CPP_SCOPE:
5641 case CPP_TEMPLATE_ID:
5642 case CPP_NESTED_NAME_SPECIFIER:
5643 {
5644 id_expression:
5645 cp_expr id_expression;
5646 cp_expr decl;
5647 const char *error_msg;
5648 bool template_p;
5649 bool done;
5650 cp_token *id_expr_token;
5651
5652 /* Parse the id-expression. */
5653 id_expression
5654 = cp_parser_id_expression (parser,
5655 /*template_keyword_p=*/false,
5656 /*check_dependency_p=*/true,
5657 &template_p,
5658 /*declarator_p=*/false,
5659 /*optional_p=*/false);
5660 if (id_expression == error_mark_node)
5661 return error_mark_node;
5662 id_expr_token = token;
5663 token = cp_lexer_peek_token (parser->lexer);
5664 done = (token->type != CPP_OPEN_SQUARE
5665 && token->type != CPP_OPEN_PAREN
5666 && token->type != CPP_DOT
5667 && token->type != CPP_DEREF
5668 && token->type != CPP_PLUS_PLUS
5669 && token->type != CPP_MINUS_MINUS);
5670 /* If we have a template-id, then no further lookup is
5671 required. If the template-id was for a template-class, we
5672 will sometimes have a TYPE_DECL at this point. */
5673 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5674 || TREE_CODE (id_expression) == TYPE_DECL)
5675 decl = id_expression;
5676 /* Look up the name. */
5677 else
5678 {
5679 tree ambiguous_decls;
5680
5681 /* If we already know that this lookup is ambiguous, then
5682 we've already issued an error message; there's no reason
5683 to check again. */
5684 if (id_expr_token->type == CPP_NAME
5685 && id_expr_token->error_reported)
5686 {
5687 cp_parser_simulate_error (parser);
5688 return error_mark_node;
5689 }
5690
5691 decl = cp_parser_lookup_name (parser, id_expression,
5692 none_type,
5693 template_p,
5694 /*is_namespace=*/false,
5695 /*check_dependency=*/true,
5696 &ambiguous_decls,
5697 id_expression.get_location ());
5698 /* If the lookup was ambiguous, an error will already have
5699 been issued. */
5700 if (ambiguous_decls)
5701 return error_mark_node;
5702
5703 /* In Objective-C++, we may have an Objective-C 2.0
5704 dot-syntax for classes here. */
5705 if (c_dialect_objc ()
5706 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5707 && TREE_CODE (decl) == TYPE_DECL
5708 && objc_is_class_name (decl))
5709 {
5710 tree component;
5711 cp_lexer_consume_token (parser->lexer);
5712 component = cp_parser_identifier (parser);
5713 if (component == error_mark_node)
5714 return error_mark_node;
5715
5716 tree result = objc_build_class_component_ref (id_expression,
5717 component);
5718 /* Build a location of the form:
5719 expr.component
5720 ~~~~~^~~~~~~~~
5721 with caret at the start of the component name (at
5722 input_location), ranging from the start of the id_expression
5723 to the end of the component name. */
5724 location_t combined_loc
5725 = make_location (input_location, id_expression.get_start (),
5726 get_finish (input_location));
5727 protected_set_expr_location (result, combined_loc);
5728 return result;
5729 }
5730
5731 /* In Objective-C++, an instance variable (ivar) may be preferred
5732 to whatever cp_parser_lookup_name() found.
5733 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5734 rest of c-family, we have to do a little extra work to preserve
5735 any location information in cp_expr "decl". Given that
5736 objc_lookup_ivar is implemented in "c-family" and "objc", we
5737 have a trip through the pure "tree" type, rather than cp_expr.
5738 Naively copying it back to "decl" would implicitly give the
5739 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5740 store an EXPR_LOCATION. Hence we only update "decl" (and
5741 hence its location_t) if we get back a different tree node. */
5742 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5743 id_expression);
5744 if (decl_tree != decl.get_value ())
5745 decl = cp_expr (decl_tree);
5746
5747 /* If name lookup gives us a SCOPE_REF, then the
5748 qualifying scope was dependent. */
5749 if (TREE_CODE (decl) == SCOPE_REF)
5750 {
5751 /* At this point, we do not know if DECL is a valid
5752 integral constant expression. We assume that it is
5753 in fact such an expression, so that code like:
5754
5755 template <int N> struct A {
5756 int a[B<N>::i];
5757 };
5758
5759 is accepted. At template-instantiation time, we
5760 will check that B<N>::i is actually a constant. */
5761 return decl;
5762 }
5763 /* Check to see if DECL is a local variable in a context
5764 where that is forbidden. */
5765 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
5766 && local_variable_p (decl))
5767 {
5768 error_at (id_expression.get_location (),
5769 "local variable %qD may not appear in this context",
5770 decl.get_value ());
5771 return error_mark_node;
5772 }
5773 }
5774
5775 decl = (finish_id_expression
5776 (id_expression, decl, parser->scope,
5777 idk,
5778 parser->integral_constant_expression_p,
5779 parser->allow_non_integral_constant_expression_p,
5780 &parser->non_integral_constant_expression_p,
5781 template_p, done, address_p,
5782 template_arg_p,
5783 &error_msg,
5784 id_expression.get_location ()));
5785 if (error_msg)
5786 cp_parser_error (parser, error_msg);
5787 /* Build a location for an id-expression of the form:
5788 ::ns::id
5789 ~~~~~~^~
5790 or:
5791 id
5792 ^~
5793 i.e. from the start of the first token to the end of the final
5794 token, with the caret at the start of the unqualified-id. */
5795 location_t caret_loc = get_pure_location (id_expression.get_location ());
5796 location_t start_loc = get_start (id_expr_token->location);
5797 location_t finish_loc = get_finish (id_expression.get_location ());
5798 location_t combined_loc
5799 = make_location (caret_loc, start_loc, finish_loc);
5800
5801 decl.set_location (combined_loc);
5802 return decl;
5803 }
5804
5805 /* Anything else is an error. */
5806 default:
5807 cp_parser_error (parser, "expected primary-expression");
5808 return error_mark_node;
5809 }
5810 }
5811
5812 static inline cp_expr
5813 cp_parser_primary_expression (cp_parser *parser,
5814 bool address_p,
5815 bool cast_p,
5816 bool template_arg_p,
5817 cp_id_kind *idk)
5818 {
5819 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5820 /*decltype*/false, idk);
5821 }
5822
5823 /* Parse an id-expression.
5824
5825 id-expression:
5826 unqualified-id
5827 qualified-id
5828
5829 qualified-id:
5830 :: [opt] nested-name-specifier template [opt] unqualified-id
5831 :: identifier
5832 :: operator-function-id
5833 :: template-id
5834
5835 Return a representation of the unqualified portion of the
5836 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5837 a `::' or nested-name-specifier.
5838
5839 Often, if the id-expression was a qualified-id, the caller will
5840 want to make a SCOPE_REF to represent the qualified-id. This
5841 function does not do this in order to avoid wastefully creating
5842 SCOPE_REFs when they are not required.
5843
5844 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5845 `template' keyword.
5846
5847 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5848 uninstantiated templates.
5849
5850 If *TEMPLATE_P is non-NULL, it is set to true iff the
5851 `template' keyword is used to explicitly indicate that the entity
5852 named is a template.
5853
5854 If DECLARATOR_P is true, the id-expression is appearing as part of
5855 a declarator, rather than as part of an expression. */
5856
5857 static cp_expr
5858 cp_parser_id_expression (cp_parser *parser,
5859 bool template_keyword_p,
5860 bool check_dependency_p,
5861 bool *template_p,
5862 bool declarator_p,
5863 bool optional_p)
5864 {
5865 bool global_scope_p;
5866 bool nested_name_specifier_p;
5867
5868 /* Assume the `template' keyword was not used. */
5869 if (template_p)
5870 *template_p = template_keyword_p;
5871
5872 /* Look for the optional `::' operator. */
5873 global_scope_p
5874 = (!template_keyword_p
5875 && (cp_parser_global_scope_opt (parser,
5876 /*current_scope_valid_p=*/false)
5877 != NULL_TREE));
5878
5879 /* Look for the optional nested-name-specifier. */
5880 nested_name_specifier_p
5881 = (cp_parser_nested_name_specifier_opt (parser,
5882 /*typename_keyword_p=*/false,
5883 check_dependency_p,
5884 /*type_p=*/false,
5885 declarator_p,
5886 template_keyword_p)
5887 != NULL_TREE);
5888
5889 /* If there is a nested-name-specifier, then we are looking at
5890 the first qualified-id production. */
5891 if (nested_name_specifier_p)
5892 {
5893 tree saved_scope;
5894 tree saved_object_scope;
5895 tree saved_qualifying_scope;
5896 cp_expr unqualified_id;
5897 bool is_template;
5898
5899 /* See if the next token is the `template' keyword. */
5900 if (!template_p)
5901 template_p = &is_template;
5902 *template_p = cp_parser_optional_template_keyword (parser);
5903 /* Name lookup we do during the processing of the
5904 unqualified-id might obliterate SCOPE. */
5905 saved_scope = parser->scope;
5906 saved_object_scope = parser->object_scope;
5907 saved_qualifying_scope = parser->qualifying_scope;
5908 /* Process the final unqualified-id. */
5909 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5910 check_dependency_p,
5911 declarator_p,
5912 /*optional_p=*/false);
5913 /* Restore the SAVED_SCOPE for our caller. */
5914 parser->scope = saved_scope;
5915 parser->object_scope = saved_object_scope;
5916 parser->qualifying_scope = saved_qualifying_scope;
5917
5918 return unqualified_id;
5919 }
5920 /* Otherwise, if we are in global scope, then we are looking at one
5921 of the other qualified-id productions. */
5922 else if (global_scope_p)
5923 {
5924 cp_token *token;
5925 tree id;
5926
5927 /* Peek at the next token. */
5928 token = cp_lexer_peek_token (parser->lexer);
5929
5930 /* If it's an identifier, and the next token is not a "<", then
5931 we can avoid the template-id case. This is an optimization
5932 for this common case. */
5933 if (token->type == CPP_NAME
5934 && !cp_parser_nth_token_starts_template_argument_list_p
5935 (parser, 2))
5936 return cp_parser_identifier (parser);
5937
5938 cp_parser_parse_tentatively (parser);
5939 /* Try a template-id. */
5940 id = cp_parser_template_id_expr (parser,
5941 /*template_keyword_p=*/false,
5942 /*check_dependency_p=*/true,
5943 declarator_p);
5944 /* If that worked, we're done. */
5945 if (cp_parser_parse_definitely (parser))
5946 return id;
5947
5948 /* Peek at the next token. (Changes in the token buffer may
5949 have invalidated the pointer obtained above.) */
5950 token = cp_lexer_peek_token (parser->lexer);
5951
5952 switch (token->type)
5953 {
5954 case CPP_NAME:
5955 return cp_parser_identifier (parser);
5956
5957 case CPP_KEYWORD:
5958 if (token->keyword == RID_OPERATOR)
5959 return cp_parser_operator_function_id (parser);
5960 /* Fall through. */
5961
5962 default:
5963 cp_parser_error (parser, "expected id-expression");
5964 return error_mark_node;
5965 }
5966 }
5967 else
5968 return cp_parser_unqualified_id (parser, template_keyword_p,
5969 /*check_dependency_p=*/true,
5970 declarator_p,
5971 optional_p);
5972 }
5973
5974 /* Parse an unqualified-id.
5975
5976 unqualified-id:
5977 identifier
5978 operator-function-id
5979 conversion-function-id
5980 ~ class-name
5981 template-id
5982
5983 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5984 keyword, in a construct like `A::template ...'.
5985
5986 Returns a representation of unqualified-id. For the `identifier'
5987 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5988 production a BIT_NOT_EXPR is returned; the operand of the
5989 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5990 other productions, see the documentation accompanying the
5991 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5992 names are looked up in uninstantiated templates. If DECLARATOR_P
5993 is true, the unqualified-id is appearing as part of a declarator,
5994 rather than as part of an expression. */
5995
5996 static cp_expr
5997 cp_parser_unqualified_id (cp_parser* parser,
5998 bool template_keyword_p,
5999 bool check_dependency_p,
6000 bool declarator_p,
6001 bool optional_p)
6002 {
6003 cp_token *token;
6004
6005 /* Peek at the next token. */
6006 token = cp_lexer_peek_token (parser->lexer);
6007
6008 switch ((int) token->type)
6009 {
6010 case CPP_NAME:
6011 {
6012 tree id;
6013
6014 /* We don't know yet whether or not this will be a
6015 template-id. */
6016 cp_parser_parse_tentatively (parser);
6017 /* Try a template-id. */
6018 id = cp_parser_template_id_expr (parser, template_keyword_p,
6019 check_dependency_p,
6020 declarator_p);
6021 /* If it worked, we're done. */
6022 if (cp_parser_parse_definitely (parser))
6023 return id;
6024 /* Otherwise, it's an ordinary identifier. */
6025 return cp_parser_identifier (parser);
6026 }
6027
6028 case CPP_TEMPLATE_ID:
6029 return cp_parser_template_id_expr (parser, template_keyword_p,
6030 check_dependency_p,
6031 declarator_p);
6032
6033 case CPP_COMPL:
6034 {
6035 tree type_decl;
6036 tree qualifying_scope;
6037 tree object_scope;
6038 tree scope;
6039 bool done;
6040 location_t tilde_loc = token->location;
6041
6042 /* Consume the `~' token. */
6043 cp_lexer_consume_token (parser->lexer);
6044 /* Parse the class-name. The standard, as written, seems to
6045 say that:
6046
6047 template <typename T> struct S { ~S (); };
6048 template <typename T> S<T>::~S() {}
6049
6050 is invalid, since `~' must be followed by a class-name, but
6051 `S<T>' is dependent, and so not known to be a class.
6052 That's not right; we need to look in uninstantiated
6053 templates. A further complication arises from:
6054
6055 template <typename T> void f(T t) {
6056 t.T::~T();
6057 }
6058
6059 Here, it is not possible to look up `T' in the scope of `T'
6060 itself. We must look in both the current scope, and the
6061 scope of the containing complete expression.
6062
6063 Yet another issue is:
6064
6065 struct S {
6066 int S;
6067 ~S();
6068 };
6069
6070 S::~S() {}
6071
6072 The standard does not seem to say that the `S' in `~S'
6073 should refer to the type `S' and not the data member
6074 `S::S'. */
6075
6076 /* DR 244 says that we look up the name after the "~" in the
6077 same scope as we looked up the qualifying name. That idea
6078 isn't fully worked out; it's more complicated than that. */
6079 scope = parser->scope;
6080 object_scope = parser->object_scope;
6081 qualifying_scope = parser->qualifying_scope;
6082
6083 /* Check for invalid scopes. */
6084 if (scope == error_mark_node)
6085 {
6086 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6087 cp_lexer_consume_token (parser->lexer);
6088 return error_mark_node;
6089 }
6090 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6091 {
6092 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6093 error_at (token->location,
6094 "scope %qT before %<~%> is not a class-name",
6095 scope);
6096 cp_parser_simulate_error (parser);
6097 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6098 cp_lexer_consume_token (parser->lexer);
6099 return error_mark_node;
6100 }
6101 if (template_keyword_p)
6102 {
6103 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6104 error_at (tilde_loc, "%<template%> keyword not permitted in "
6105 "destructor name");
6106 cp_parser_simulate_error (parser);
6107 return error_mark_node;
6108 }
6109
6110 gcc_assert (!scope || TYPE_P (scope));
6111
6112 token = cp_lexer_peek_token (parser->lexer);
6113
6114 /* Create a location with caret == start at the tilde,
6115 finishing at the end of the peeked token, e.g:
6116 ~token
6117 ^~~~~~. */
6118 location_t loc
6119 = make_location (tilde_loc, tilde_loc, token->location);
6120
6121 /* If the name is of the form "X::~X" it's OK even if X is a
6122 typedef. */
6123
6124 if (scope
6125 && token->type == CPP_NAME
6126 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6127 != CPP_LESS)
6128 && (token->u.value == TYPE_IDENTIFIER (scope)
6129 || (CLASS_TYPE_P (scope)
6130 && constructor_name_p (token->u.value, scope))))
6131 {
6132 cp_lexer_consume_token (parser->lexer);
6133 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6134 }
6135
6136 /* ~auto means the destructor of whatever the object is. */
6137 if (cp_parser_is_keyword (token, RID_AUTO))
6138 {
6139 if (cxx_dialect < cxx14)
6140 pedwarn (loc, 0,
6141 "%<~auto%> only available with "
6142 "%<-std=c++14%> or %<-std=gnu++14%>");
6143 cp_lexer_consume_token (parser->lexer);
6144 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6145 }
6146
6147 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6148 declarator-id of a constructor or destructor. */
6149 if (token->type == CPP_TEMPLATE_ID && cxx_dialect >= cxx20)
6150 {
6151 if (!cp_parser_simulate_error (parser))
6152 error_at (tilde_loc, "template-id not allowed for destructor");
6153 return error_mark_node;
6154 }
6155
6156 /* If there was an explicit qualification (S::~T), first look
6157 in the scope given by the qualification (i.e., S).
6158
6159 Note: in the calls to cp_parser_class_name below we pass
6160 typename_type so that lookup finds the injected-class-name
6161 rather than the constructor. */
6162 done = false;
6163 type_decl = NULL_TREE;
6164 if (scope)
6165 {
6166 cp_parser_parse_tentatively (parser);
6167 type_decl = cp_parser_class_name (parser,
6168 /*typename_keyword_p=*/false,
6169 /*template_keyword_p=*/false,
6170 typename_type,
6171 /*check_dependency=*/false,
6172 /*class_head_p=*/false,
6173 declarator_p);
6174 if (cp_parser_parse_definitely (parser))
6175 done = true;
6176 }
6177 /* In "N::S::~S", look in "N" as well. */
6178 if (!done && scope && qualifying_scope)
6179 {
6180 cp_parser_parse_tentatively (parser);
6181 parser->scope = qualifying_scope;
6182 parser->object_scope = NULL_TREE;
6183 parser->qualifying_scope = NULL_TREE;
6184 type_decl
6185 = cp_parser_class_name (parser,
6186 /*typename_keyword_p=*/false,
6187 /*template_keyword_p=*/false,
6188 typename_type,
6189 /*check_dependency=*/false,
6190 /*class_head_p=*/false,
6191 declarator_p);
6192 if (cp_parser_parse_definitely (parser))
6193 done = true;
6194 }
6195 /* In "p->S::~T", look in the scope given by "*p" as well. */
6196 else if (!done && object_scope)
6197 {
6198 cp_parser_parse_tentatively (parser);
6199 parser->scope = object_scope;
6200 parser->object_scope = NULL_TREE;
6201 parser->qualifying_scope = NULL_TREE;
6202 type_decl
6203 = cp_parser_class_name (parser,
6204 /*typename_keyword_p=*/false,
6205 /*template_keyword_p=*/false,
6206 typename_type,
6207 /*check_dependency=*/false,
6208 /*class_head_p=*/false,
6209 declarator_p);
6210 if (cp_parser_parse_definitely (parser))
6211 done = true;
6212 }
6213 /* Look in the surrounding context. */
6214 if (!done)
6215 {
6216 parser->scope = NULL_TREE;
6217 parser->object_scope = NULL_TREE;
6218 parser->qualifying_scope = NULL_TREE;
6219 if (processing_template_decl)
6220 cp_parser_parse_tentatively (parser);
6221 type_decl
6222 = cp_parser_class_name (parser,
6223 /*typename_keyword_p=*/false,
6224 /*template_keyword_p=*/false,
6225 typename_type,
6226 /*check_dependency=*/false,
6227 /*class_head_p=*/false,
6228 declarator_p);
6229 if (processing_template_decl
6230 && ! cp_parser_parse_definitely (parser))
6231 {
6232 /* We couldn't find a type with this name. If we're parsing
6233 tentatively, fail and try something else. */
6234 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6235 {
6236 cp_parser_simulate_error (parser);
6237 return error_mark_node;
6238 }
6239 /* Otherwise, accept it and check for a match at instantiation
6240 time. */
6241 type_decl = cp_parser_identifier (parser);
6242 if (type_decl != error_mark_node)
6243 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6244 return type_decl;
6245 }
6246 }
6247 /* If an error occurred, assume that the name of the
6248 destructor is the same as the name of the qualifying
6249 class. That allows us to keep parsing after running
6250 into ill-formed destructor names. */
6251 if (type_decl == error_mark_node && scope)
6252 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6253 else if (type_decl == error_mark_node)
6254 return error_mark_node;
6255
6256 /* Check that destructor name and scope match. */
6257 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6258 {
6259 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6260 error_at (loc,
6261 "declaration of %<~%T%> as member of %qT",
6262 type_decl, scope);
6263 cp_parser_simulate_error (parser);
6264 return error_mark_node;
6265 }
6266
6267 /* [class.dtor]
6268
6269 A typedef-name that names a class shall not be used as the
6270 identifier in the declarator for a destructor declaration. */
6271 if (declarator_p
6272 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6273 && !DECL_SELF_REFERENCE_P (type_decl)
6274 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6275 error_at (loc,
6276 "typedef-name %qD used as destructor declarator",
6277 type_decl);
6278
6279 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6280 }
6281
6282 case CPP_KEYWORD:
6283 if (token->keyword == RID_OPERATOR)
6284 {
6285 cp_expr id;
6286
6287 /* This could be a template-id, so we try that first. */
6288 cp_parser_parse_tentatively (parser);
6289 /* Try a template-id. */
6290 id = cp_parser_template_id_expr (parser, template_keyword_p,
6291 /*check_dependency_p=*/true,
6292 declarator_p);
6293 /* If that worked, we're done. */
6294 if (cp_parser_parse_definitely (parser))
6295 return id;
6296 /* We still don't know whether we're looking at an
6297 operator-function-id or a conversion-function-id. */
6298 cp_parser_parse_tentatively (parser);
6299 /* Try an operator-function-id. */
6300 id = cp_parser_operator_function_id (parser);
6301 /* If that didn't work, try a conversion-function-id. */
6302 if (!cp_parser_parse_definitely (parser))
6303 id = cp_parser_conversion_function_id (parser);
6304
6305 return id;
6306 }
6307 /* Fall through. */
6308
6309 default:
6310 if (optional_p)
6311 return NULL_TREE;
6312 cp_parser_error (parser, "expected unqualified-id");
6313 return error_mark_node;
6314 }
6315 }
6316
6317 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6318 be a template-id or the name shall refer to a class template or an
6319 alias template. */
6320
6321 static void
6322 check_template_keyword_in_nested_name_spec (tree name)
6323 {
6324 if (CLASS_TYPE_P (name)
6325 && ((CLASSTYPE_USE_TEMPLATE (name)
6326 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name)))
6327 || CLASSTYPE_IS_TEMPLATE (name)))
6328 return;
6329
6330 if (TREE_CODE (name) == TYPENAME_TYPE
6331 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR)
6332 return;
6333 /* Alias templates are also OK. */
6334 else if (alias_template_specialization_p (name, nt_opaque))
6335 return;
6336
6337 permerror (input_location, TYPE_P (name)
6338 ? G_("%qT is not a template")
6339 : G_("%qD is not a template"),
6340 name);
6341 }
6342
6343 /* Parse an (optional) nested-name-specifier.
6344
6345 nested-name-specifier: [C++98]
6346 class-or-namespace-name :: nested-name-specifier [opt]
6347 class-or-namespace-name :: template nested-name-specifier [opt]
6348
6349 nested-name-specifier: [C++0x]
6350 type-name ::
6351 namespace-name ::
6352 nested-name-specifier identifier ::
6353 nested-name-specifier template [opt] simple-template-id ::
6354
6355 PARSER->SCOPE should be set appropriately before this function is
6356 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6357 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6358 in name lookups.
6359
6360 Sets PARSER->SCOPE to the class (TYPE) or namespace
6361 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6362 it unchanged if there is no nested-name-specifier. Returns the new
6363 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6364
6365 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6366 part of a declaration and/or decl-specifier. */
6367
6368 static tree
6369 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6370 bool typename_keyword_p,
6371 bool check_dependency_p,
6372 bool type_p,
6373 bool is_declaration,
6374 bool template_keyword_p /* = false */)
6375 {
6376 bool success = false;
6377 cp_token_position start = 0;
6378 cp_token *token;
6379
6380 /* Remember where the nested-name-specifier starts. */
6381 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6382 && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6383 {
6384 start = cp_lexer_token_position (parser->lexer, false);
6385 push_deferring_access_checks (dk_deferred);
6386 }
6387
6388 while (true)
6389 {
6390 tree new_scope;
6391 tree old_scope;
6392 tree saved_qualifying_scope;
6393
6394 /* Spot cases that cannot be the beginning of a
6395 nested-name-specifier. */
6396 token = cp_lexer_peek_token (parser->lexer);
6397
6398 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6399 the already parsed nested-name-specifier. */
6400 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6401 {
6402 /* Grab the nested-name-specifier and continue the loop. */
6403 cp_parser_pre_parsed_nested_name_specifier (parser);
6404 /* If we originally encountered this nested-name-specifier
6405 with IS_DECLARATION set to false, we will not have
6406 resolved TYPENAME_TYPEs, so we must do so here. */
6407 if (is_declaration
6408 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6409 {
6410 new_scope = resolve_typename_type (parser->scope,
6411 /*only_current_p=*/false);
6412 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6413 parser->scope = new_scope;
6414 }
6415 success = true;
6416 continue;
6417 }
6418
6419 /* Spot cases that cannot be the beginning of a
6420 nested-name-specifier. On the second and subsequent times
6421 through the loop, we look for the `template' keyword. */
6422 if (success && token->keyword == RID_TEMPLATE)
6423 ;
6424 /* A template-id can start a nested-name-specifier. */
6425 else if (token->type == CPP_TEMPLATE_ID)
6426 ;
6427 /* DR 743: decltype can be used in a nested-name-specifier. */
6428 else if (token_is_decltype (token))
6429 ;
6430 else
6431 {
6432 /* If the next token is not an identifier, then it is
6433 definitely not a type-name or namespace-name. */
6434 if (token->type != CPP_NAME)
6435 break;
6436 /* If the following token is neither a `<' (to begin a
6437 template-id), nor a `::', then we are not looking at a
6438 nested-name-specifier. */
6439 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6440
6441 if (token->type == CPP_COLON
6442 && parser->colon_corrects_to_scope_p
6443 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6444 {
6445 gcc_rich_location richloc (token->location);
6446 richloc.add_fixit_replace ("::");
6447 error_at (&richloc,
6448 "found %<:%> in nested-name-specifier, "
6449 "expected %<::%>");
6450 token->type = CPP_SCOPE;
6451 }
6452
6453 if (token->type != CPP_SCOPE
6454 && !cp_parser_nth_token_starts_template_argument_list_p
6455 (parser, 2))
6456 break;
6457 }
6458
6459 /* The nested-name-specifier is optional, so we parse
6460 tentatively. */
6461 cp_parser_parse_tentatively (parser);
6462
6463 /* Look for the optional `template' keyword, if this isn't the
6464 first time through the loop. */
6465 if (success)
6466 {
6467 template_keyword_p = cp_parser_optional_template_keyword (parser);
6468 /* DR1710: "In a qualified-id used as the name in
6469 a typename-specifier, elaborated-type-specifier, using-declaration,
6470 or class-or-decltype, an optional keyword template appearing at
6471 the top level is ignored." */
6472 if (!template_keyword_p
6473 && typename_keyword_p
6474 && cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
6475 template_keyword_p = true;
6476 }
6477
6478 /* Save the old scope since the name lookup we are about to do
6479 might destroy it. */
6480 old_scope = parser->scope;
6481 saved_qualifying_scope = parser->qualifying_scope;
6482 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6483 look up names in "X<T>::I" in order to determine that "Y" is
6484 a template. So, if we have a typename at this point, we make
6485 an effort to look through it. */
6486 if (is_declaration
6487 && !typename_keyword_p
6488 && parser->scope
6489 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6490 parser->scope = resolve_typename_type (parser->scope,
6491 /*only_current_p=*/false);
6492 /* Parse the qualifying entity. */
6493 new_scope
6494 = cp_parser_qualifying_entity (parser,
6495 typename_keyword_p,
6496 template_keyword_p,
6497 check_dependency_p,
6498 type_p,
6499 is_declaration);
6500 /* Look for the `::' token. */
6501 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6502
6503 /* If we found what we wanted, we keep going; otherwise, we're
6504 done. */
6505 if (!cp_parser_parse_definitely (parser))
6506 {
6507 bool error_p = false;
6508
6509 /* Restore the OLD_SCOPE since it was valid before the
6510 failed attempt at finding the last
6511 class-or-namespace-name. */
6512 parser->scope = old_scope;
6513 parser->qualifying_scope = saved_qualifying_scope;
6514
6515 /* If the next token is a decltype, and the one after that is a
6516 `::', then the decltype has failed to resolve to a class or
6517 enumeration type. Give this error even when parsing
6518 tentatively since it can't possibly be valid--and we're going
6519 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6520 won't get another chance.*/
6521 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6522 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6523 == CPP_SCOPE))
6524 {
6525 token = cp_lexer_consume_token (parser->lexer);
6526 tree dtype = token->u.tree_check_value->value;
6527 if (dtype != error_mark_node)
6528 error_at (token->location, "%<decltype%> evaluates to %qT, "
6529 "which is not a class or enumeration type",
6530 dtype);
6531 parser->scope = error_mark_node;
6532 error_p = true;
6533 /* As below. */
6534 success = true;
6535 cp_lexer_consume_token (parser->lexer);
6536 }
6537
6538 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6539 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6540 {
6541 /* If we have a non-type template-id followed by ::, it can't
6542 possibly be valid. */
6543 token = cp_lexer_peek_token (parser->lexer);
6544 tree tid = token->u.tree_check_value->value;
6545 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6546 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6547 {
6548 tree tmpl = NULL_TREE;
6549 if (is_overloaded_fn (tid))
6550 {
6551 tree fns = get_fns (tid);
6552 if (OVL_SINGLE_P (fns))
6553 tmpl = OVL_FIRST (fns);
6554 if (function_concept_p (fns))
6555 error_at (token->location, "concept-id %qD "
6556 "in nested-name-specifier", tid);
6557 else
6558 error_at (token->location, "function template-id "
6559 "%qD in nested-name-specifier", tid);
6560 }
6561 else
6562 {
6563 tmpl = TREE_OPERAND (tid, 0);
6564 if (variable_concept_p (tmpl)
6565 || standard_concept_p (tmpl))
6566 error_at (token->location, "concept-id %qD "
6567 "in nested-name-specifier", tid);
6568 else
6569 {
6570 /* Variable template. */
6571 gcc_assert (variable_template_p (tmpl));
6572 error_at (token->location, "variable template-id "
6573 "%qD in nested-name-specifier", tid);
6574 }
6575 }
6576 if (tmpl)
6577 inform (DECL_SOURCE_LOCATION (tmpl),
6578 "%qD declared here", tmpl);
6579
6580 parser->scope = error_mark_node;
6581 error_p = true;
6582 /* As below. */
6583 success = true;
6584 cp_lexer_consume_token (parser->lexer);
6585 cp_lexer_consume_token (parser->lexer);
6586 }
6587 }
6588
6589 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6590 break;
6591 /* If the next token is an identifier, and the one after
6592 that is a `::', then any valid interpretation would have
6593 found a class-or-namespace-name. */
6594 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6595 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6596 == CPP_SCOPE)
6597 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6598 != CPP_COMPL))
6599 {
6600 token = cp_lexer_consume_token (parser->lexer);
6601 if (!error_p)
6602 {
6603 if (!token->error_reported)
6604 {
6605 tree decl;
6606 tree ambiguous_decls;
6607
6608 decl = cp_parser_lookup_name (parser, token->u.value,
6609 none_type,
6610 /*is_template=*/false,
6611 /*is_namespace=*/false,
6612 /*check_dependency=*/true,
6613 &ambiguous_decls,
6614 token->location);
6615 if (TREE_CODE (decl) == TEMPLATE_DECL)
6616 error_at (token->location,
6617 "%qD used without template arguments",
6618 decl);
6619 else if (ambiguous_decls)
6620 {
6621 // cp_parser_lookup_name has the same diagnostic,
6622 // thus make sure to emit it at most once.
6623 if (cp_parser_uncommitted_to_tentative_parse_p
6624 (parser))
6625 {
6626 error_at (token->location,
6627 "reference to %qD is ambiguous",
6628 token->u.value);
6629 print_candidates (ambiguous_decls);
6630 }
6631 decl = error_mark_node;
6632 }
6633 else
6634 {
6635 if (cxx_dialect != cxx98)
6636 cp_parser_name_lookup_error
6637 (parser, token->u.value, decl, NLE_NOT_CXX98,
6638 token->location);
6639 else
6640 cp_parser_name_lookup_error
6641 (parser, token->u.value, decl, NLE_CXX98,
6642 token->location);
6643 }
6644 }
6645 parser->scope = error_mark_node;
6646 error_p = true;
6647 /* Treat this as a successful nested-name-specifier
6648 due to:
6649
6650 [basic.lookup.qual]
6651
6652 If the name found is not a class-name (clause
6653 _class_) or namespace-name (_namespace.def_), the
6654 program is ill-formed. */
6655 success = true;
6656 }
6657 cp_lexer_consume_token (parser->lexer);
6658 }
6659 break;
6660 }
6661 /* We've found one valid nested-name-specifier. */
6662 success = true;
6663 /* Name lookup always gives us a DECL. */
6664 if (TREE_CODE (new_scope) == TYPE_DECL)
6665 new_scope = TREE_TYPE (new_scope);
6666 /* Uses of "template" must be followed by actual templates. */
6667 if (template_keyword_p)
6668 check_template_keyword_in_nested_name_spec (new_scope);
6669 /* If it is a class scope, try to complete it; we are about to
6670 be looking up names inside the class. */
6671 if (TYPE_P (new_scope)
6672 /* Since checking types for dependency can be expensive,
6673 avoid doing it if the type is already complete. */
6674 && !COMPLETE_TYPE_P (new_scope)
6675 /* Do not try to complete dependent types. */
6676 && !dependent_type_p (new_scope))
6677 {
6678 new_scope = complete_type (new_scope);
6679 /* If it is a typedef to current class, use the current
6680 class instead, as the typedef won't have any names inside
6681 it yet. */
6682 if (!COMPLETE_TYPE_P (new_scope)
6683 && currently_open_class (new_scope))
6684 new_scope = TYPE_MAIN_VARIANT (new_scope);
6685 }
6686 /* Make sure we look in the right scope the next time through
6687 the loop. */
6688 parser->scope = new_scope;
6689 }
6690
6691 /* If parsing tentatively, replace the sequence of tokens that makes
6692 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6693 token. That way, should we re-parse the token stream, we will
6694 not have to repeat the effort required to do the parse, nor will
6695 we issue duplicate error messages. */
6696 if (success && start)
6697 {
6698 cp_token *token;
6699
6700 token = cp_lexer_token_at (parser->lexer, start);
6701 /* Reset the contents of the START token. */
6702 token->type = CPP_NESTED_NAME_SPECIFIER;
6703 /* Retrieve any deferred checks. Do not pop this access checks yet
6704 so the memory will not be reclaimed during token replacing below. */
6705 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6706 token->tree_check_p = true;
6707 token->u.tree_check_value->value = parser->scope;
6708 token->u.tree_check_value->checks = get_deferred_access_checks ();
6709 token->u.tree_check_value->qualifying_scope =
6710 parser->qualifying_scope;
6711 token->keyword = RID_MAX;
6712
6713 /* Purge all subsequent tokens. */
6714 cp_lexer_purge_tokens_after (parser->lexer, start);
6715 }
6716
6717 if (start)
6718 pop_to_parent_deferring_access_checks ();
6719
6720 return success ? parser->scope : NULL_TREE;
6721 }
6722
6723 /* Parse a nested-name-specifier. See
6724 cp_parser_nested_name_specifier_opt for details. This function
6725 behaves identically, except that it will an issue an error if no
6726 nested-name-specifier is present. */
6727
6728 static tree
6729 cp_parser_nested_name_specifier (cp_parser *parser,
6730 bool typename_keyword_p,
6731 bool check_dependency_p,
6732 bool type_p,
6733 bool is_declaration)
6734 {
6735 tree scope;
6736
6737 /* Look for the nested-name-specifier. */
6738 scope = cp_parser_nested_name_specifier_opt (parser,
6739 typename_keyword_p,
6740 check_dependency_p,
6741 type_p,
6742 is_declaration);
6743 /* If it was not present, issue an error message. */
6744 if (!scope)
6745 {
6746 cp_parser_error (parser, "expected nested-name-specifier");
6747 parser->scope = NULL_TREE;
6748 }
6749
6750 return scope;
6751 }
6752
6753 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6754 this is either a class-name or a namespace-name (which corresponds
6755 to the class-or-namespace-name production in the grammar). For
6756 C++0x, it can also be a type-name that refers to an enumeration
6757 type or a simple-template-id.
6758
6759 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6760 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6761 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6762 TYPE_P is TRUE iff the next name should be taken as a class-name,
6763 even the same name is declared to be another entity in the same
6764 scope.
6765
6766 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6767 specified by the class-or-namespace-name. If neither is found the
6768 ERROR_MARK_NODE is returned. */
6769
6770 static tree
6771 cp_parser_qualifying_entity (cp_parser *parser,
6772 bool typename_keyword_p,
6773 bool template_keyword_p,
6774 bool check_dependency_p,
6775 bool type_p,
6776 bool is_declaration)
6777 {
6778 tree saved_scope;
6779 tree saved_qualifying_scope;
6780 tree saved_object_scope;
6781 tree scope;
6782 bool only_class_p;
6783 bool successful_parse_p;
6784
6785 /* DR 743: decltype can appear in a nested-name-specifier. */
6786 if (cp_lexer_next_token_is_decltype (parser->lexer))
6787 {
6788 scope = cp_parser_decltype (parser);
6789 if (TREE_CODE (scope) != ENUMERAL_TYPE
6790 && !MAYBE_CLASS_TYPE_P (scope))
6791 {
6792 cp_parser_simulate_error (parser);
6793 return error_mark_node;
6794 }
6795 if (TYPE_NAME (scope))
6796 scope = TYPE_NAME (scope);
6797 return scope;
6798 }
6799
6800 /* Before we try to parse the class-name, we must save away the
6801 current PARSER->SCOPE since cp_parser_class_name will destroy
6802 it. */
6803 saved_scope = parser->scope;
6804 saved_qualifying_scope = parser->qualifying_scope;
6805 saved_object_scope = parser->object_scope;
6806 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6807 there is no need to look for a namespace-name. */
6808 only_class_p = template_keyword_p
6809 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6810 if (!only_class_p)
6811 cp_parser_parse_tentatively (parser);
6812 scope = cp_parser_class_name (parser,
6813 typename_keyword_p,
6814 template_keyword_p,
6815 type_p ? class_type : none_type,
6816 check_dependency_p,
6817 /*class_head_p=*/false,
6818 is_declaration,
6819 /*enum_ok=*/cxx_dialect > cxx98);
6820 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6821 /* If that didn't work, try for a namespace-name. */
6822 if (!only_class_p && !successful_parse_p)
6823 {
6824 /* Restore the saved scope. */
6825 parser->scope = saved_scope;
6826 parser->qualifying_scope = saved_qualifying_scope;
6827 parser->object_scope = saved_object_scope;
6828 /* If we are not looking at an identifier followed by the scope
6829 resolution operator, then this is not part of a
6830 nested-name-specifier. (Note that this function is only used
6831 to parse the components of a nested-name-specifier.) */
6832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6833 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6834 return error_mark_node;
6835 scope = cp_parser_namespace_name (parser);
6836 }
6837
6838 return scope;
6839 }
6840
6841 /* Return true if we are looking at a compound-literal, false otherwise. */
6842
6843 static bool
6844 cp_parser_compound_literal_p (cp_parser *parser)
6845 {
6846 cp_lexer_save_tokens (parser->lexer);
6847
6848 /* Skip tokens until the next token is a closing parenthesis.
6849 If we find the closing `)', and the next token is a `{', then
6850 we are looking at a compound-literal. */
6851 bool compound_literal_p
6852 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6853 /*consume_paren=*/true)
6854 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6855
6856 /* Roll back the tokens we skipped. */
6857 cp_lexer_rollback_tokens (parser->lexer);
6858
6859 return compound_literal_p;
6860 }
6861
6862 /* Return true if EXPR is the integer constant zero or a complex constant
6863 of zero, without any folding, but ignoring location wrappers. */
6864
6865 bool
6866 literal_integer_zerop (const_tree expr)
6867 {
6868 return (location_wrapper_p (expr)
6869 && integer_zerop (TREE_OPERAND (expr, 0)));
6870 }
6871
6872 /* Parse a postfix-expression.
6873
6874 postfix-expression:
6875 primary-expression
6876 postfix-expression [ expression ]
6877 postfix-expression ( expression-list [opt] )
6878 simple-type-specifier ( expression-list [opt] )
6879 typename :: [opt] nested-name-specifier identifier
6880 ( expression-list [opt] )
6881 typename :: [opt] nested-name-specifier template [opt] template-id
6882 ( expression-list [opt] )
6883 postfix-expression . template [opt] id-expression
6884 postfix-expression -> template [opt] id-expression
6885 postfix-expression . pseudo-destructor-name
6886 postfix-expression -> pseudo-destructor-name
6887 postfix-expression ++
6888 postfix-expression --
6889 dynamic_cast < type-id > ( expression )
6890 static_cast < type-id > ( expression )
6891 reinterpret_cast < type-id > ( expression )
6892 const_cast < type-id > ( expression )
6893 typeid ( expression )
6894 typeid ( type-id )
6895
6896 GNU Extension:
6897
6898 postfix-expression:
6899 ( type-id ) { initializer-list , [opt] }
6900
6901 This extension is a GNU version of the C99 compound-literal
6902 construct. (The C99 grammar uses `type-name' instead of `type-id',
6903 but they are essentially the same concept.)
6904
6905 If ADDRESS_P is true, the postfix expression is the operand of the
6906 `&' operator. CAST_P is true if this expression is the target of a
6907 cast.
6908
6909 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6910 class member access expressions [expr.ref].
6911
6912 Returns a representation of the expression. */
6913
6914 static cp_expr
6915 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6916 bool member_access_only_p, bool decltype_p,
6917 cp_id_kind * pidk_return)
6918 {
6919 cp_token *token;
6920 location_t loc;
6921 enum rid keyword;
6922 cp_id_kind idk = CP_ID_KIND_NONE;
6923 cp_expr postfix_expression = NULL_TREE;
6924 bool is_member_access = false;
6925
6926 /* Peek at the next token. */
6927 token = cp_lexer_peek_token (parser->lexer);
6928 loc = token->location;
6929 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6930
6931 /* Some of the productions are determined by keywords. */
6932 keyword = token->keyword;
6933 switch (keyword)
6934 {
6935 case RID_DYNCAST:
6936 case RID_STATCAST:
6937 case RID_REINTCAST:
6938 case RID_CONSTCAST:
6939 {
6940 tree type;
6941 cp_expr expression;
6942 const char *saved_message;
6943 bool saved_in_type_id_in_expr_p;
6944
6945 /* All of these can be handled in the same way from the point
6946 of view of parsing. Begin by consuming the token
6947 identifying the cast. */
6948 cp_lexer_consume_token (parser->lexer);
6949
6950 /* New types cannot be defined in the cast. */
6951 saved_message = parser->type_definition_forbidden_message;
6952 parser->type_definition_forbidden_message
6953 = G_("types may not be defined in casts");
6954
6955 /* Look for the opening `<'. */
6956 cp_parser_require (parser, CPP_LESS, RT_LESS);
6957 /* Parse the type to which we are casting. */
6958 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6959 parser->in_type_id_in_expr_p = true;
6960 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
6961 NULL);
6962 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6963 /* Look for the closing `>'. */
6964 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6965 /* Restore the old message. */
6966 parser->type_definition_forbidden_message = saved_message;
6967
6968 bool saved_greater_than_is_operator_p
6969 = parser->greater_than_is_operator_p;
6970 parser->greater_than_is_operator_p = true;
6971
6972 /* And the expression which is being cast. */
6973 matching_parens parens;
6974 parens.require_open (parser);
6975 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6976 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6977 RT_CLOSE_PAREN);
6978 location_t end_loc = close_paren ?
6979 close_paren->location : UNKNOWN_LOCATION;
6980
6981 parser->greater_than_is_operator_p
6982 = saved_greater_than_is_operator_p;
6983
6984 /* Only type conversions to integral or enumeration types
6985 can be used in constant-expressions. */
6986 if (!cast_valid_in_integral_constant_expression_p (type)
6987 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6988 {
6989 postfix_expression = error_mark_node;
6990 break;
6991 }
6992
6993 /* Construct a location e.g. :
6994 reinterpret_cast <int *> (expr)
6995 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6996 ranging from the start of the "*_cast" token to the final closing
6997 paren, with the caret at the start. */
6998 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6999
7000 switch (keyword)
7001 {
7002 case RID_DYNCAST:
7003 postfix_expression
7004 = build_dynamic_cast (cp_cast_loc, type, expression,
7005 tf_warning_or_error);
7006 break;
7007 case RID_STATCAST:
7008 postfix_expression
7009 = build_static_cast (cp_cast_loc, type, expression,
7010 tf_warning_or_error);
7011 break;
7012 case RID_REINTCAST:
7013 postfix_expression
7014 = build_reinterpret_cast (cp_cast_loc, type, expression,
7015 tf_warning_or_error);
7016 break;
7017 case RID_CONSTCAST:
7018 postfix_expression
7019 = build_const_cast (cp_cast_loc, type, expression,
7020 tf_warning_or_error);
7021 break;
7022 default:
7023 gcc_unreachable ();
7024 }
7025 }
7026 break;
7027
7028 case RID_TYPEID:
7029 {
7030 tree type;
7031 const char *saved_message;
7032 bool saved_in_type_id_in_expr_p;
7033
7034 /* Consume the `typeid' token. */
7035 cp_lexer_consume_token (parser->lexer);
7036 /* Look for the `(' token. */
7037 matching_parens parens;
7038 parens.require_open (parser);
7039 /* Types cannot be defined in a `typeid' expression. */
7040 saved_message = parser->type_definition_forbidden_message;
7041 parser->type_definition_forbidden_message
7042 = G_("types may not be defined in a %<typeid%> expression");
7043 /* We can't be sure yet whether we're looking at a type-id or an
7044 expression. */
7045 cp_parser_parse_tentatively (parser);
7046 /* Try a type-id first. */
7047 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7048 parser->in_type_id_in_expr_p = true;
7049 type = cp_parser_type_id (parser);
7050 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7051 /* Look for the `)' token. Otherwise, we can't be sure that
7052 we're not looking at an expression: consider `typeid (int
7053 (3))', for example. */
7054 cp_token *close_paren = parens.require_close (parser);
7055 /* If all went well, simply lookup the type-id. */
7056 if (cp_parser_parse_definitely (parser))
7057 postfix_expression = get_typeid (type, tf_warning_or_error);
7058 /* Otherwise, fall back to the expression variant. */
7059 else
7060 {
7061 tree expression;
7062
7063 /* Look for an expression. */
7064 expression = cp_parser_expression (parser, & idk);
7065 /* Compute its typeid. */
7066 postfix_expression = build_typeid (expression, tf_warning_or_error);
7067 /* Look for the `)' token. */
7068 close_paren = parens.require_close (parser);
7069 }
7070 /* Restore the saved message. */
7071 parser->type_definition_forbidden_message = saved_message;
7072 /* `typeid' may not appear in an integral constant expression. */
7073 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
7074 postfix_expression = error_mark_node;
7075
7076 /* Construct a location e.g. :
7077 typeid (expr)
7078 ^~~~~~~~~~~~~
7079 ranging from the start of the "typeid" token to the final closing
7080 paren, with the caret at the start. */
7081 if (close_paren)
7082 {
7083 location_t typeid_loc
7084 = make_location (start_loc, start_loc, close_paren->location);
7085 postfix_expression.set_location (typeid_loc);
7086 postfix_expression.maybe_add_location_wrapper ();
7087 }
7088 }
7089 break;
7090
7091 case RID_TYPENAME:
7092 {
7093 tree type;
7094 /* The syntax permitted here is the same permitted for an
7095 elaborated-type-specifier. */
7096 ++parser->prevent_constrained_type_specifiers;
7097 type = cp_parser_elaborated_type_specifier (parser,
7098 /*is_friend=*/false,
7099 /*is_declaration=*/false);
7100 --parser->prevent_constrained_type_specifiers;
7101 postfix_expression = cp_parser_functional_cast (parser, type);
7102 }
7103 break;
7104
7105 case RID_ADDRESSOF:
7106 case RID_BUILTIN_SHUFFLE:
7107 case RID_BUILTIN_LAUNDER:
7108 {
7109 vec<tree, va_gc> *vec;
7110 unsigned int i;
7111 tree p;
7112
7113 cp_lexer_consume_token (parser->lexer);
7114 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7115 /*cast_p=*/false, /*allow_expansion_p=*/true,
7116 /*non_constant_p=*/NULL);
7117 if (vec == NULL)
7118 {
7119 postfix_expression = error_mark_node;
7120 break;
7121 }
7122
7123 FOR_EACH_VEC_ELT (*vec, i, p)
7124 mark_exp_read (p);
7125
7126 switch (keyword)
7127 {
7128 case RID_ADDRESSOF:
7129 if (vec->length () == 1)
7130 postfix_expression
7131 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7132 else
7133 {
7134 error_at (loc, "wrong number of arguments to "
7135 "%<__builtin_addressof%>");
7136 postfix_expression = error_mark_node;
7137 }
7138 break;
7139
7140 case RID_BUILTIN_LAUNDER:
7141 if (vec->length () == 1)
7142 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7143 tf_warning_or_error);
7144 else
7145 {
7146 error_at (loc, "wrong number of arguments to "
7147 "%<__builtin_launder%>");
7148 postfix_expression = error_mark_node;
7149 }
7150 break;
7151
7152 case RID_BUILTIN_SHUFFLE:
7153 if (vec->length () == 2)
7154 postfix_expression
7155 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7156 (*vec)[1], tf_warning_or_error);
7157 else if (vec->length () == 3)
7158 postfix_expression
7159 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7160 (*vec)[2], tf_warning_or_error);
7161 else
7162 {
7163 error_at (loc, "wrong number of arguments to "
7164 "%<__builtin_shuffle%>");
7165 postfix_expression = error_mark_node;
7166 }
7167 break;
7168
7169 default:
7170 gcc_unreachable ();
7171 }
7172 break;
7173 }
7174
7175 case RID_BUILTIN_CONVERTVECTOR:
7176 {
7177 tree expression;
7178 tree type;
7179 /* Consume the `__builtin_convertvector' token. */
7180 cp_lexer_consume_token (parser->lexer);
7181 /* Look for the opening `('. */
7182 matching_parens parens;
7183 parens.require_open (parser);
7184 /* Now, parse the assignment-expression. */
7185 expression = cp_parser_assignment_expression (parser);
7186 /* Look for the `,'. */
7187 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7188 location_t type_location
7189 = cp_lexer_peek_token (parser->lexer)->location;
7190 /* Parse the type-id. */
7191 {
7192 type_id_in_expr_sentinel s (parser);
7193 type = cp_parser_type_id (parser);
7194 }
7195 /* Look for the closing `)'. */
7196 parens.require_close (parser);
7197 return cp_build_vec_convert (expression, type_location, type,
7198 tf_warning_or_error);
7199 }
7200
7201 default:
7202 {
7203 tree type;
7204
7205 /* If the next thing is a simple-type-specifier, we may be
7206 looking at a functional cast. We could also be looking at
7207 an id-expression. So, we try the functional cast, and if
7208 that doesn't work we fall back to the primary-expression. */
7209 cp_parser_parse_tentatively (parser);
7210 /* Look for the simple-type-specifier. */
7211 ++parser->prevent_constrained_type_specifiers;
7212 type = cp_parser_simple_type_specifier (parser,
7213 /*decl_specs=*/NULL,
7214 CP_PARSER_FLAGS_NONE);
7215 --parser->prevent_constrained_type_specifiers;
7216 /* Parse the cast itself. */
7217 if (!cp_parser_error_occurred (parser))
7218 postfix_expression
7219 = cp_parser_functional_cast (parser, type);
7220 /* If that worked, we're done. */
7221 if (cp_parser_parse_definitely (parser))
7222 break;
7223
7224 /* If the functional-cast didn't work out, try a
7225 compound-literal. */
7226 if (cp_parser_allow_gnu_extensions_p (parser)
7227 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7228 {
7229 cp_expr initializer = NULL_TREE;
7230
7231 cp_parser_parse_tentatively (parser);
7232
7233 matching_parens parens;
7234 parens.consume_open (parser);
7235
7236 /* Avoid calling cp_parser_type_id pointlessly, see comment
7237 in cp_parser_cast_expression about c++/29234. */
7238 if (!cp_parser_compound_literal_p (parser))
7239 cp_parser_simulate_error (parser);
7240 else
7241 {
7242 /* Parse the type. */
7243 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7244 parser->in_type_id_in_expr_p = true;
7245 type = cp_parser_type_id (parser);
7246 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7247 parens.require_close (parser);
7248 }
7249
7250 /* If things aren't going well, there's no need to
7251 keep going. */
7252 if (!cp_parser_error_occurred (parser))
7253 {
7254 bool non_constant_p;
7255 /* Parse the brace-enclosed initializer list. */
7256 initializer = cp_parser_braced_list (parser,
7257 &non_constant_p);
7258 }
7259 /* If that worked, we're definitely looking at a
7260 compound-literal expression. */
7261 if (cp_parser_parse_definitely (parser))
7262 {
7263 /* Warn the user that a compound literal is not
7264 allowed in standard C++. */
7265 pedwarn (input_location, OPT_Wpedantic,
7266 "ISO C++ forbids compound-literals");
7267 /* For simplicity, we disallow compound literals in
7268 constant-expressions. We could
7269 allow compound literals of integer type, whose
7270 initializer was a constant, in constant
7271 expressions. Permitting that usage, as a further
7272 extension, would not change the meaning of any
7273 currently accepted programs. (Of course, as
7274 compound literals are not part of ISO C++, the
7275 standard has nothing to say.) */
7276 if (cp_parser_non_integral_constant_expression (parser,
7277 NIC_NCC))
7278 {
7279 postfix_expression = error_mark_node;
7280 break;
7281 }
7282 /* Form the representation of the compound-literal. */
7283 postfix_expression
7284 = finish_compound_literal (type, initializer,
7285 tf_warning_or_error, fcl_c99);
7286 postfix_expression.set_location (initializer.get_location ());
7287 break;
7288 }
7289 }
7290
7291 /* It must be a primary-expression. */
7292 postfix_expression
7293 = cp_parser_primary_expression (parser, address_p, cast_p,
7294 /*template_arg_p=*/false,
7295 decltype_p,
7296 &idk);
7297 }
7298 break;
7299 }
7300
7301 /* Note that we don't need to worry about calling build_cplus_new on a
7302 class-valued CALL_EXPR in decltype when it isn't the end of the
7303 postfix-expression; unary_complex_lvalue will take care of that for
7304 all these cases. */
7305
7306 /* Keep looping until the postfix-expression is complete. */
7307 while (true)
7308 {
7309 if (idk == CP_ID_KIND_UNQUALIFIED
7310 && identifier_p (postfix_expression)
7311 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7312 /* It is not a Koenig lookup function call. */
7313 postfix_expression
7314 = unqualified_name_lookup_error (postfix_expression);
7315
7316 /* Peek at the next token. */
7317 token = cp_lexer_peek_token (parser->lexer);
7318
7319 switch (token->type)
7320 {
7321 case CPP_OPEN_SQUARE:
7322 if (cp_next_tokens_can_be_std_attribute_p (parser))
7323 {
7324 cp_parser_error (parser,
7325 "two consecutive %<[%> shall "
7326 "only introduce an attribute");
7327 return error_mark_node;
7328 }
7329 postfix_expression
7330 = cp_parser_postfix_open_square_expression (parser,
7331 postfix_expression,
7332 false,
7333 decltype_p);
7334 postfix_expression.set_range (start_loc,
7335 postfix_expression.get_location ());
7336
7337 idk = CP_ID_KIND_NONE;
7338 is_member_access = false;
7339 break;
7340
7341 case CPP_OPEN_PAREN:
7342 /* postfix-expression ( expression-list [opt] ) */
7343 {
7344 bool koenig_p;
7345 bool is_builtin_constant_p;
7346 bool saved_integral_constant_expression_p = false;
7347 bool saved_non_integral_constant_expression_p = false;
7348 tsubst_flags_t complain = complain_flags (decltype_p);
7349 vec<tree, va_gc> *args;
7350 location_t close_paren_loc = UNKNOWN_LOCATION;
7351
7352 is_member_access = false;
7353
7354 tree stripped_expression
7355 = tree_strip_any_location_wrapper (postfix_expression);
7356 is_builtin_constant_p
7357 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7358 if (is_builtin_constant_p)
7359 {
7360 /* The whole point of __builtin_constant_p is to allow
7361 non-constant expressions to appear as arguments. */
7362 saved_integral_constant_expression_p
7363 = parser->integral_constant_expression_p;
7364 saved_non_integral_constant_expression_p
7365 = parser->non_integral_constant_expression_p;
7366 parser->integral_constant_expression_p = false;
7367 }
7368 args = (cp_parser_parenthesized_expression_list
7369 (parser, non_attr,
7370 /*cast_p=*/false, /*allow_expansion_p=*/true,
7371 /*non_constant_p=*/NULL,
7372 /*close_paren_loc=*/&close_paren_loc,
7373 /*wrap_locations_p=*/true));
7374 if (is_builtin_constant_p)
7375 {
7376 parser->integral_constant_expression_p
7377 = saved_integral_constant_expression_p;
7378 parser->non_integral_constant_expression_p
7379 = saved_non_integral_constant_expression_p;
7380 }
7381
7382 if (args == NULL)
7383 {
7384 postfix_expression = error_mark_node;
7385 break;
7386 }
7387
7388 /* Function calls are not permitted in
7389 constant-expressions. */
7390 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7391 && cp_parser_non_integral_constant_expression (parser,
7392 NIC_FUNC_CALL))
7393 {
7394 postfix_expression = error_mark_node;
7395 release_tree_vector (args);
7396 break;
7397 }
7398
7399 koenig_p = false;
7400 if (idk == CP_ID_KIND_UNQUALIFIED
7401 || idk == CP_ID_KIND_TEMPLATE_ID)
7402 {
7403 if (identifier_p (postfix_expression)
7404 /* In C++20, we may need to perform ADL for a template
7405 name. */
7406 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7407 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7408 {
7409 if (!args->is_empty ())
7410 {
7411 koenig_p = true;
7412 if (!any_type_dependent_arguments_p (args))
7413 postfix_expression
7414 = perform_koenig_lookup (postfix_expression, args,
7415 complain);
7416 }
7417 else
7418 postfix_expression
7419 = unqualified_fn_lookup_error (postfix_expression);
7420 }
7421 /* We do not perform argument-dependent lookup if
7422 normal lookup finds a non-function, in accordance
7423 with the expected resolution of DR 218. */
7424 else if (!args->is_empty ()
7425 && is_overloaded_fn (postfix_expression))
7426 {
7427 /* Do not do argument dependent lookup if regular
7428 lookup finds a member function or a block-scope
7429 function declaration. [basic.lookup.argdep]/3 */
7430 bool do_adl_p = true;
7431 tree fns = get_fns (postfix_expression);
7432 for (lkp_iterator iter (fns); iter; ++iter)
7433 {
7434 tree fn = STRIP_TEMPLATE (*iter);
7435 if ((TREE_CODE (fn) == USING_DECL
7436 && DECL_DEPENDENT_P (fn))
7437 || DECL_FUNCTION_MEMBER_P (fn)
7438 || DECL_LOCAL_FUNCTION_P (fn))
7439 {
7440 do_adl_p = false;
7441 break;
7442 }
7443 }
7444
7445 if (do_adl_p)
7446 {
7447 koenig_p = true;
7448 if (!any_type_dependent_arguments_p (args))
7449 postfix_expression
7450 = perform_koenig_lookup (postfix_expression, args,
7451 complain);
7452 }
7453 }
7454 }
7455
7456 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7457 {
7458 tree instance = TREE_OPERAND (postfix_expression, 0);
7459 tree fn = TREE_OPERAND (postfix_expression, 1);
7460
7461 if (processing_template_decl
7462 && (type_dependent_object_expression_p (instance)
7463 || (!BASELINK_P (fn)
7464 && TREE_CODE (fn) != FIELD_DECL)
7465 || type_dependent_expression_p (fn)
7466 || any_type_dependent_arguments_p (args)))
7467 {
7468 maybe_generic_this_capture (instance, fn);
7469 postfix_expression
7470 = build_min_nt_call_vec (postfix_expression, args);
7471 }
7472 else if (BASELINK_P (fn))
7473 {
7474 postfix_expression
7475 = (build_new_method_call
7476 (instance, fn, &args, NULL_TREE,
7477 (idk == CP_ID_KIND_QUALIFIED
7478 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7479 : LOOKUP_NORMAL),
7480 /*fn_p=*/NULL,
7481 complain));
7482 }
7483 else
7484 postfix_expression
7485 = finish_call_expr (postfix_expression, &args,
7486 /*disallow_virtual=*/false,
7487 /*koenig_p=*/false,
7488 complain);
7489 }
7490 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7491 || TREE_CODE (postfix_expression) == MEMBER_REF
7492 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7493 postfix_expression = (build_offset_ref_call_from_tree
7494 (postfix_expression, &args,
7495 complain));
7496 else if (idk == CP_ID_KIND_QUALIFIED)
7497 /* A call to a static class member, or a namespace-scope
7498 function. */
7499 postfix_expression
7500 = finish_call_expr (postfix_expression, &args,
7501 /*disallow_virtual=*/true,
7502 koenig_p,
7503 complain);
7504 else
7505 /* All other function calls. */
7506 postfix_expression
7507 = finish_call_expr (postfix_expression, &args,
7508 /*disallow_virtual=*/false,
7509 koenig_p,
7510 complain);
7511
7512 if (close_paren_loc != UNKNOWN_LOCATION)
7513 {
7514 location_t combined_loc = make_location (token->location,
7515 start_loc,
7516 close_paren_loc);
7517 postfix_expression.set_location (combined_loc);
7518 }
7519
7520 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7521 idk = CP_ID_KIND_NONE;
7522
7523 release_tree_vector (args);
7524 }
7525 break;
7526
7527 case CPP_DOT:
7528 case CPP_DEREF:
7529 /* postfix-expression . template [opt] id-expression
7530 postfix-expression . pseudo-destructor-name
7531 postfix-expression -> template [opt] id-expression
7532 postfix-expression -> pseudo-destructor-name */
7533
7534 /* Consume the `.' or `->' operator. */
7535 cp_lexer_consume_token (parser->lexer);
7536
7537 postfix_expression
7538 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7539 postfix_expression,
7540 false, &idk, loc);
7541
7542 is_member_access = true;
7543 break;
7544
7545 case CPP_PLUS_PLUS:
7546 /* postfix-expression ++ */
7547 /* Consume the `++' token. */
7548 cp_lexer_consume_token (parser->lexer);
7549 /* Generate a representation for the complete expression. */
7550 postfix_expression
7551 = finish_increment_expr (postfix_expression,
7552 POSTINCREMENT_EXPR);
7553 /* Increments may not appear in constant-expressions. */
7554 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7555 postfix_expression = error_mark_node;
7556 idk = CP_ID_KIND_NONE;
7557 is_member_access = false;
7558 break;
7559
7560 case CPP_MINUS_MINUS:
7561 /* postfix-expression -- */
7562 /* Consume the `--' token. */
7563 cp_lexer_consume_token (parser->lexer);
7564 /* Generate a representation for the complete expression. */
7565 postfix_expression
7566 = finish_increment_expr (postfix_expression,
7567 POSTDECREMENT_EXPR);
7568 /* Decrements may not appear in constant-expressions. */
7569 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7570 postfix_expression = error_mark_node;
7571 idk = CP_ID_KIND_NONE;
7572 is_member_access = false;
7573 break;
7574
7575 default:
7576 if (pidk_return != NULL)
7577 * pidk_return = idk;
7578 if (member_access_only_p)
7579 return is_member_access
7580 ? postfix_expression
7581 : cp_expr (error_mark_node);
7582 else
7583 return postfix_expression;
7584 }
7585 }
7586
7587 /* We should never get here. */
7588 gcc_unreachable ();
7589 return error_mark_node;
7590 }
7591
7592 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7593 by cp_parser_builtin_offsetof. We're looking for
7594
7595 postfix-expression [ expression ]
7596 postfix-expression [ braced-init-list ] (C++11)
7597
7598 FOR_OFFSETOF is set if we're being called in that context, which
7599 changes how we deal with integer constant expressions. */
7600
7601 static tree
7602 cp_parser_postfix_open_square_expression (cp_parser *parser,
7603 tree postfix_expression,
7604 bool for_offsetof,
7605 bool decltype_p)
7606 {
7607 tree index = NULL_TREE;
7608 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7609 bool saved_greater_than_is_operator_p;
7610
7611 /* Consume the `[' token. */
7612 cp_lexer_consume_token (parser->lexer);
7613
7614 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7615 parser->greater_than_is_operator_p = true;
7616
7617 /* Parse the index expression. */
7618 /* ??? For offsetof, there is a question of what to allow here. If
7619 offsetof is not being used in an integral constant expression context,
7620 then we *could* get the right answer by computing the value at runtime.
7621 If we are in an integral constant expression context, then we might
7622 could accept any constant expression; hard to say without analysis.
7623 Rather than open the barn door too wide right away, allow only integer
7624 constant expressions here. */
7625 if (for_offsetof)
7626 index = cp_parser_constant_expression (parser);
7627 else
7628 {
7629 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7630 {
7631 bool expr_nonconst_p;
7632 cp_lexer_set_source_position (parser->lexer);
7633 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7634 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7635 }
7636 else
7637 index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
7638 /*decltype_p=*/false,
7639 /*warn_comma_p=*/warn_comma_subscript);
7640 }
7641
7642 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7643
7644 /* Look for the closing `]'. */
7645 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7646
7647 /* Build the ARRAY_REF. */
7648 postfix_expression = grok_array_decl (loc, postfix_expression,
7649 index, decltype_p);
7650
7651 /* When not doing offsetof, array references are not permitted in
7652 constant-expressions. */
7653 if (!for_offsetof
7654 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7655 postfix_expression = error_mark_node;
7656
7657 return postfix_expression;
7658 }
7659
7660 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7661 dereference of incomplete type, returns true if error_mark_node should
7662 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7663 and *DEPENDENT_P. */
7664
7665 bool
7666 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7667 bool *dependent_p)
7668 {
7669 /* In a template, be permissive by treating an object expression
7670 of incomplete type as dependent (after a pedwarn). */
7671 diagnostic_t kind = (processing_template_decl
7672 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7673
7674 switch (TREE_CODE (*postfix_expression))
7675 {
7676 case CAST_EXPR:
7677 case REINTERPRET_CAST_EXPR:
7678 case CONST_CAST_EXPR:
7679 case STATIC_CAST_EXPR:
7680 case DYNAMIC_CAST_EXPR:
7681 case IMPLICIT_CONV_EXPR:
7682 case VIEW_CONVERT_EXPR:
7683 case NON_LVALUE_EXPR:
7684 kind = DK_ERROR;
7685 break;
7686 case OVERLOAD:
7687 /* Don't emit any diagnostic for OVERLOADs. */
7688 kind = DK_IGNORED;
7689 break;
7690 default:
7691 /* Avoid clobbering e.g. DECLs. */
7692 if (!EXPR_P (*postfix_expression))
7693 kind = DK_ERROR;
7694 break;
7695 }
7696
7697 if (kind == DK_IGNORED)
7698 return false;
7699
7700 location_t exploc = location_of (*postfix_expression);
7701 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7702 if (!MAYBE_CLASS_TYPE_P (*scope))
7703 return true;
7704 if (kind == DK_ERROR)
7705 *scope = *postfix_expression = error_mark_node;
7706 else if (processing_template_decl)
7707 {
7708 *dependent_p = true;
7709 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7710 }
7711 return false;
7712 }
7713
7714 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7715 by cp_parser_builtin_offsetof. We're looking for
7716
7717 postfix-expression . template [opt] id-expression
7718 postfix-expression . pseudo-destructor-name
7719 postfix-expression -> template [opt] id-expression
7720 postfix-expression -> pseudo-destructor-name
7721
7722 FOR_OFFSETOF is set if we're being called in that context. That sorta
7723 limits what of the above we'll actually accept, but nevermind.
7724 TOKEN_TYPE is the "." or "->" token, which will already have been
7725 removed from the stream. */
7726
7727 static tree
7728 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7729 enum cpp_ttype token_type,
7730 cp_expr postfix_expression,
7731 bool for_offsetof, cp_id_kind *idk,
7732 location_t location)
7733 {
7734 tree name;
7735 bool dependent_p;
7736 bool pseudo_destructor_p;
7737 tree scope = NULL_TREE;
7738 location_t start_loc = postfix_expression.get_start ();
7739
7740 /* If this is a `->' operator, dereference the pointer. */
7741 if (token_type == CPP_DEREF)
7742 postfix_expression = build_x_arrow (location, postfix_expression,
7743 tf_warning_or_error);
7744 /* Check to see whether or not the expression is type-dependent and
7745 not the current instantiation. */
7746 dependent_p = type_dependent_object_expression_p (postfix_expression);
7747 /* The identifier following the `->' or `.' is not qualified. */
7748 parser->scope = NULL_TREE;
7749 parser->qualifying_scope = NULL_TREE;
7750 parser->object_scope = NULL_TREE;
7751 *idk = CP_ID_KIND_NONE;
7752
7753 /* Enter the scope corresponding to the type of the object
7754 given by the POSTFIX_EXPRESSION. */
7755 if (!dependent_p)
7756 {
7757 scope = TREE_TYPE (postfix_expression);
7758 /* According to the standard, no expression should ever have
7759 reference type. Unfortunately, we do not currently match
7760 the standard in this respect in that our internal representation
7761 of an expression may have reference type even when the standard
7762 says it does not. Therefore, we have to manually obtain the
7763 underlying type here. */
7764 scope = non_reference (scope);
7765 /* The type of the POSTFIX_EXPRESSION must be complete. */
7766 /* Unlike the object expression in other contexts, *this is not
7767 required to be of complete type for purposes of class member
7768 access (5.2.5) outside the member function body. */
7769 if (postfix_expression != current_class_ref
7770 && scope != error_mark_node
7771 && !currently_open_class (scope))
7772 {
7773 scope = complete_type (scope);
7774 if (!COMPLETE_TYPE_P (scope)
7775 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7776 &dependent_p))
7777 return error_mark_node;
7778 }
7779
7780 if (!dependent_p)
7781 {
7782 /* Let the name lookup machinery know that we are processing a
7783 class member access expression. */
7784 parser->context->object_type = scope;
7785 /* If something went wrong, we want to be able to discern that case,
7786 as opposed to the case where there was no SCOPE due to the type
7787 of expression being dependent. */
7788 if (!scope)
7789 scope = error_mark_node;
7790 /* If the SCOPE was erroneous, make the various semantic analysis
7791 functions exit quickly -- and without issuing additional error
7792 messages. */
7793 if (scope == error_mark_node)
7794 postfix_expression = error_mark_node;
7795 }
7796 }
7797
7798 if (dependent_p)
7799 {
7800 tree type = TREE_TYPE (postfix_expression);
7801 /* If we don't have a (type-dependent) object of class type, use
7802 typeof to figure out the type of the object. */
7803 if (type == NULL_TREE)
7804 type = finish_typeof (postfix_expression);
7805 parser->context->object_type = type;
7806 }
7807
7808 /* Assume this expression is not a pseudo-destructor access. */
7809 pseudo_destructor_p = false;
7810
7811 /* If the SCOPE is a scalar type, then, if this is a valid program,
7812 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7813 is type dependent, it can be pseudo-destructor-name or something else.
7814 Try to parse it as pseudo-destructor-name first. */
7815 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7816 {
7817 tree s;
7818 tree type;
7819
7820 cp_parser_parse_tentatively (parser);
7821 /* Parse the pseudo-destructor-name. */
7822 s = NULL_TREE;
7823 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7824 &s, &type);
7825 if (dependent_p
7826 && (cp_parser_error_occurred (parser)
7827 || !SCALAR_TYPE_P (type)))
7828 cp_parser_abort_tentative_parse (parser);
7829 else if (cp_parser_parse_definitely (parser))
7830 {
7831 pseudo_destructor_p = true;
7832 postfix_expression
7833 = finish_pseudo_destructor_expr (postfix_expression,
7834 s, type, location);
7835 }
7836 }
7837
7838 if (!pseudo_destructor_p)
7839 {
7840 /* If the SCOPE is not a scalar type, we are looking at an
7841 ordinary class member access expression, rather than a
7842 pseudo-destructor-name. */
7843 bool template_p;
7844 cp_token *token = cp_lexer_peek_token (parser->lexer);
7845 /* Parse the id-expression. */
7846 name = (cp_parser_id_expression
7847 (parser,
7848 cp_parser_optional_template_keyword (parser),
7849 /*check_dependency_p=*/true,
7850 &template_p,
7851 /*declarator_p=*/false,
7852 /*optional_p=*/false));
7853 /* In general, build a SCOPE_REF if the member name is qualified.
7854 However, if the name was not dependent and has already been
7855 resolved; there is no need to build the SCOPE_REF. For example;
7856
7857 struct X { void f(); };
7858 template <typename T> void f(T* t) { t->X::f(); }
7859
7860 Even though "t" is dependent, "X::f" is not and has been resolved
7861 to a BASELINK; there is no need to include scope information. */
7862
7863 /* But we do need to remember that there was an explicit scope for
7864 virtual function calls. */
7865 if (parser->scope)
7866 *idk = CP_ID_KIND_QUALIFIED;
7867
7868 /* If the name is a template-id that names a type, we will get a
7869 TYPE_DECL here. That is invalid code. */
7870 if (TREE_CODE (name) == TYPE_DECL)
7871 {
7872 error_at (token->location, "invalid use of %qD", name);
7873 postfix_expression = error_mark_node;
7874 }
7875 else
7876 {
7877 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7878 {
7879 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7880 {
7881 error_at (token->location, "%<%D::%D%> is not a class member",
7882 parser->scope, name);
7883 postfix_expression = error_mark_node;
7884 }
7885 else
7886 name = build_qualified_name (/*type=*/NULL_TREE,
7887 parser->scope,
7888 name,
7889 template_p);
7890 parser->scope = NULL_TREE;
7891 parser->qualifying_scope = NULL_TREE;
7892 parser->object_scope = NULL_TREE;
7893 }
7894 if (parser->scope && name && BASELINK_P (name))
7895 adjust_result_of_qualified_name_lookup
7896 (name, parser->scope, scope);
7897 postfix_expression
7898 = finish_class_member_access_expr (postfix_expression, name,
7899 template_p,
7900 tf_warning_or_error);
7901 /* Build a location e.g.:
7902 ptr->access_expr
7903 ~~~^~~~~~~~~~~~~
7904 where the caret is at the deref token, ranging from
7905 the start of postfix_expression to the end of the access expr. */
7906 location_t combined_loc
7907 = make_location (input_location, start_loc, parser->lexer);
7908 protected_set_expr_location (postfix_expression, combined_loc);
7909 }
7910 }
7911
7912 /* We no longer need to look up names in the scope of the object on
7913 the left-hand side of the `.' or `->' operator. */
7914 parser->context->object_type = NULL_TREE;
7915
7916 /* Outside of offsetof, these operators may not appear in
7917 constant-expressions. */
7918 if (!for_offsetof
7919 && (cp_parser_non_integral_constant_expression
7920 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7921 postfix_expression = error_mark_node;
7922
7923 return postfix_expression;
7924 }
7925
7926 /* Parse a parenthesized expression-list.
7927
7928 expression-list:
7929 assignment-expression
7930 expression-list, assignment-expression
7931
7932 attribute-list:
7933 expression-list
7934 identifier
7935 identifier, expression-list
7936
7937 CAST_P is true if this expression is the target of a cast.
7938
7939 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7940 argument pack.
7941
7942 WRAP_LOCATIONS_P is true if expressions within this list for which
7943 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7944 their source locations.
7945
7946 Returns a vector of trees. Each element is a representation of an
7947 assignment-expression. NULL is returned if the ( and or ) are
7948 missing. An empty, but allocated, vector is returned on no
7949 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7950 if we are parsing an attribute list for an attribute that wants a
7951 plain identifier argument, normal_attr for an attribute that wants
7952 an expression, or non_attr if we aren't parsing an attribute list. If
7953 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7954 not all of the expressions in the list were constant.
7955 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7956 will be written to with the location of the closing parenthesis. If
7957 an error occurs, it may or may not be written to. */
7958
7959 static vec<tree, va_gc> *
7960 cp_parser_parenthesized_expression_list (cp_parser* parser,
7961 int is_attribute_list,
7962 bool cast_p,
7963 bool allow_expansion_p,
7964 bool *non_constant_p,
7965 location_t *close_paren_loc,
7966 bool wrap_locations_p)
7967 {
7968 vec<tree, va_gc> *expression_list;
7969 bool fold_expr_p = is_attribute_list != non_attr;
7970 tree identifier = NULL_TREE;
7971 bool saved_greater_than_is_operator_p;
7972
7973 /* Assume all the expressions will be constant. */
7974 if (non_constant_p)
7975 *non_constant_p = false;
7976
7977 matching_parens parens;
7978 if (!parens.require_open (parser))
7979 return NULL;
7980
7981 expression_list = make_tree_vector ();
7982
7983 /* Within a parenthesized expression, a `>' token is always
7984 the greater-than operator. */
7985 saved_greater_than_is_operator_p
7986 = parser->greater_than_is_operator_p;
7987 parser->greater_than_is_operator_p = true;
7988
7989 cp_expr expr (NULL_TREE);
7990
7991 /* Consume expressions until there are no more. */
7992 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7993 while (true)
7994 {
7995 /* At the beginning of attribute lists, check to see if the
7996 next token is an identifier. */
7997 if (is_attribute_list == id_attr
7998 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7999 {
8000 cp_token *token;
8001
8002 /* Consume the identifier. */
8003 token = cp_lexer_consume_token (parser->lexer);
8004 /* Save the identifier. */
8005 identifier = token->u.value;
8006 }
8007 else
8008 {
8009 bool expr_non_constant_p;
8010
8011 /* Parse the next assignment-expression. */
8012 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8013 {
8014 /* A braced-init-list. */
8015 cp_lexer_set_source_position (parser->lexer);
8016 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8017 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8018 if (non_constant_p && expr_non_constant_p)
8019 *non_constant_p = true;
8020 }
8021 else if (non_constant_p)
8022 {
8023 expr = (cp_parser_constant_expression
8024 (parser, /*allow_non_constant_p=*/true,
8025 &expr_non_constant_p));
8026 if (expr_non_constant_p)
8027 *non_constant_p = true;
8028 }
8029 else
8030 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
8031 cast_p);
8032
8033 if (fold_expr_p)
8034 expr = instantiate_non_dependent_expr (expr);
8035
8036 /* If we have an ellipsis, then this is an expression
8037 expansion. */
8038 if (allow_expansion_p
8039 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8040 {
8041 /* Consume the `...'. */
8042 cp_lexer_consume_token (parser->lexer);
8043
8044 /* Build the argument pack. */
8045 expr = make_pack_expansion (expr);
8046 }
8047
8048 if (wrap_locations_p)
8049 expr.maybe_add_location_wrapper ();
8050
8051 /* Add it to the list. We add error_mark_node
8052 expressions to the list, so that we can still tell if
8053 the correct form for a parenthesized expression-list
8054 is found. That gives better errors. */
8055 vec_safe_push (expression_list, expr.get_value ());
8056
8057 if (expr == error_mark_node)
8058 goto skip_comma;
8059 }
8060
8061 /* After the first item, attribute lists look the same as
8062 expression lists. */
8063 is_attribute_list = non_attr;
8064
8065 get_comma:;
8066 /* If the next token isn't a `,', then we are done. */
8067 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8068 break;
8069
8070 /* Otherwise, consume the `,' and keep going. */
8071 cp_lexer_consume_token (parser->lexer);
8072 }
8073
8074 if (close_paren_loc)
8075 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
8076
8077 if (!parens.require_close (parser))
8078 {
8079 int ending;
8080
8081 skip_comma:;
8082 /* We try and resync to an unnested comma, as that will give the
8083 user better diagnostics. */
8084 ending = cp_parser_skip_to_closing_parenthesis (parser,
8085 /*recovering=*/true,
8086 /*or_comma=*/true,
8087 /*consume_paren=*/true);
8088 if (ending < 0)
8089 goto get_comma;
8090 if (!ending)
8091 {
8092 parser->greater_than_is_operator_p
8093 = saved_greater_than_is_operator_p;
8094 return NULL;
8095 }
8096 }
8097
8098 parser->greater_than_is_operator_p
8099 = saved_greater_than_is_operator_p;
8100
8101 if (identifier)
8102 vec_safe_insert (expression_list, 0, identifier);
8103
8104 return expression_list;
8105 }
8106
8107 /* Parse a pseudo-destructor-name.
8108
8109 pseudo-destructor-name:
8110 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8111 :: [opt] nested-name-specifier template template-id :: ~ type-name
8112 :: [opt] nested-name-specifier [opt] ~ type-name
8113
8114 If either of the first two productions is used, sets *SCOPE to the
8115 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8116 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8117 or ERROR_MARK_NODE if the parse fails. */
8118
8119 static void
8120 cp_parser_pseudo_destructor_name (cp_parser* parser,
8121 tree object,
8122 tree* scope,
8123 tree* type)
8124 {
8125 bool nested_name_specifier_p;
8126
8127 /* Handle ~auto. */
8128 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8129 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8130 && !type_dependent_expression_p (object))
8131 {
8132 if (cxx_dialect < cxx14)
8133 pedwarn (input_location, 0,
8134 "%<~auto%> only available with "
8135 "%<-std=c++14%> or %<-std=gnu++14%>");
8136 cp_lexer_consume_token (parser->lexer);
8137 cp_lexer_consume_token (parser->lexer);
8138 *scope = NULL_TREE;
8139 *type = TREE_TYPE (object);
8140 return;
8141 }
8142
8143 /* Assume that things will not work out. */
8144 *type = error_mark_node;
8145
8146 /* Look for the optional `::' operator. */
8147 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8148 /* Look for the optional nested-name-specifier. */
8149 nested_name_specifier_p
8150 = (cp_parser_nested_name_specifier_opt (parser,
8151 /*typename_keyword_p=*/false,
8152 /*check_dependency_p=*/true,
8153 /*type_p=*/false,
8154 /*is_declaration=*/false)
8155 != NULL_TREE);
8156 /* Now, if we saw a nested-name-specifier, we might be doing the
8157 second production. */
8158 if (nested_name_specifier_p
8159 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8160 {
8161 /* Consume the `template' keyword. */
8162 cp_lexer_consume_token (parser->lexer);
8163 /* Parse the template-id. */
8164 cp_parser_template_id (parser,
8165 /*template_keyword_p=*/true,
8166 /*check_dependency_p=*/false,
8167 class_type,
8168 /*is_declaration=*/true);
8169 /* Look for the `::' token. */
8170 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8171 }
8172 /* If the next token is not a `~', then there might be some
8173 additional qualification. */
8174 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8175 {
8176 /* At this point, we're looking for "type-name :: ~". The type-name
8177 must not be a class-name, since this is a pseudo-destructor. So,
8178 it must be either an enum-name, or a typedef-name -- both of which
8179 are just identifiers. So, we peek ahead to check that the "::"
8180 and "~" tokens are present; if they are not, then we can avoid
8181 calling type_name. */
8182 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8183 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8184 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8185 {
8186 cp_parser_error (parser, "non-scalar type");
8187 return;
8188 }
8189
8190 /* Look for the type-name. */
8191 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8192 if (*scope == error_mark_node)
8193 return;
8194
8195 /* Look for the `::' token. */
8196 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8197 }
8198 else
8199 *scope = NULL_TREE;
8200
8201 /* Look for the `~'. */
8202 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8203
8204 /* Once we see the ~, this has to be a pseudo-destructor. */
8205 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8206 cp_parser_commit_to_topmost_tentative_parse (parser);
8207
8208 /* Look for the type-name again. We are not responsible for
8209 checking that it matches the first type-name. */
8210 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8211 }
8212
8213 /* Parse a unary-expression.
8214
8215 unary-expression:
8216 postfix-expression
8217 ++ cast-expression
8218 -- cast-expression
8219 await-expression
8220 unary-operator cast-expression
8221 sizeof unary-expression
8222 sizeof ( type-id )
8223 alignof ( type-id ) [C++0x]
8224 new-expression
8225 delete-expression
8226
8227 GNU Extensions:
8228
8229 unary-expression:
8230 __extension__ cast-expression
8231 __alignof__ unary-expression
8232 __alignof__ ( type-id )
8233 alignof unary-expression [C++0x]
8234 __real__ cast-expression
8235 __imag__ cast-expression
8236 && identifier
8237 sizeof ( type-id ) { initializer-list , [opt] }
8238 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8239 __alignof__ ( type-id ) { initializer-list , [opt] }
8240
8241 ADDRESS_P is true iff the unary-expression is appearing as the
8242 operand of the `&' operator. CAST_P is true if this expression is
8243 the target of a cast.
8244
8245 Returns a representation of the expression. */
8246
8247 static cp_expr
8248 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8249 bool address_p, bool cast_p, bool decltype_p)
8250 {
8251 cp_token *token;
8252 enum tree_code unary_operator;
8253
8254 /* Peek at the next token. */
8255 token = cp_lexer_peek_token (parser->lexer);
8256 /* Some keywords give away the kind of expression. */
8257 if (token->type == CPP_KEYWORD)
8258 {
8259 enum rid keyword = token->keyword;
8260
8261 switch (keyword)
8262 {
8263 case RID_ALIGNOF:
8264 case RID_SIZEOF:
8265 {
8266 tree operand, ret;
8267 enum tree_code op;
8268 location_t start_loc = token->location;
8269
8270 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8271 bool std_alignof = id_equal (token->u.value, "alignof");
8272
8273 /* Consume the token. */
8274 cp_lexer_consume_token (parser->lexer);
8275 /* Parse the operand. */
8276 operand = cp_parser_sizeof_operand (parser, keyword);
8277
8278 /* Construct a location e.g. :
8279 alignof (expr)
8280 ^~~~~~~~~~~~~~
8281 with start == caret at the start of the "alignof"/"sizeof"
8282 token, with the endpoint at the final closing paren. */
8283 location_t compound_loc
8284 = make_location (start_loc, start_loc, parser->lexer);
8285
8286 if (TYPE_P (operand))
8287 ret = cxx_sizeof_or_alignof_type (compound_loc, operand, op,
8288 std_alignof, true);
8289 else
8290 {
8291 /* ISO C++ defines alignof only with types, not with
8292 expressions. So pedwarn if alignof is used with a non-
8293 type expression. However, __alignof__ is ok. */
8294 if (std_alignof)
8295 pedwarn (token->location, OPT_Wpedantic,
8296 "ISO C++ does not allow %<alignof%> "
8297 "with a non-type");
8298
8299 ret = cxx_sizeof_or_alignof_expr (compound_loc,
8300 operand, op, true);
8301 }
8302 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8303 SIZEOF_EXPR with the original operand. */
8304 if (op == SIZEOF_EXPR && ret != error_mark_node)
8305 {
8306 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8307 {
8308 if (!processing_template_decl && TYPE_P (operand))
8309 {
8310 ret = build_min (SIZEOF_EXPR, size_type_node,
8311 build1 (NOP_EXPR, operand,
8312 error_mark_node));
8313 SIZEOF_EXPR_TYPE_P (ret) = 1;
8314 }
8315 else
8316 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8317 TREE_SIDE_EFFECTS (ret) = 0;
8318 TREE_READONLY (ret) = 1;
8319 SET_EXPR_LOCATION (ret, compound_loc);
8320 }
8321 }
8322
8323 cp_expr ret_expr (ret, compound_loc);
8324 ret_expr = ret_expr.maybe_add_location_wrapper ();
8325 return ret_expr;
8326 }
8327
8328 case RID_BUILTIN_HAS_ATTRIBUTE:
8329 return cp_parser_has_attribute_expression (parser);
8330
8331 case RID_NEW:
8332 return cp_parser_new_expression (parser);
8333
8334 case RID_DELETE:
8335 return cp_parser_delete_expression (parser);
8336
8337 case RID_EXTENSION:
8338 {
8339 /* The saved value of the PEDANTIC flag. */
8340 int saved_pedantic;
8341 tree expr;
8342
8343 /* Save away the PEDANTIC flag. */
8344 cp_parser_extension_opt (parser, &saved_pedantic);
8345 /* Parse the cast-expression. */
8346 expr = cp_parser_simple_cast_expression (parser);
8347 /* Restore the PEDANTIC flag. */
8348 pedantic = saved_pedantic;
8349
8350 return expr;
8351 }
8352
8353 case RID_REALPART:
8354 case RID_IMAGPART:
8355 {
8356 tree expression;
8357
8358 /* Consume the `__real__' or `__imag__' token. */
8359 cp_lexer_consume_token (parser->lexer);
8360 /* Parse the cast-expression. */
8361 expression = cp_parser_simple_cast_expression (parser);
8362 /* Create the complete representation. */
8363 return build_x_unary_op (token->location,
8364 (keyword == RID_REALPART
8365 ? REALPART_EXPR : IMAGPART_EXPR),
8366 expression,
8367 tf_warning_or_error);
8368 }
8369 break;
8370
8371 case RID_TRANSACTION_ATOMIC:
8372 case RID_TRANSACTION_RELAXED:
8373 return cp_parser_transaction_expression (parser, keyword);
8374
8375 case RID_NOEXCEPT:
8376 {
8377 tree expr;
8378 const char *saved_message;
8379 bool saved_integral_constant_expression_p;
8380 bool saved_non_integral_constant_expression_p;
8381 bool saved_greater_than_is_operator_p;
8382
8383 location_t start_loc = token->location;
8384
8385 cp_lexer_consume_token (parser->lexer);
8386 matching_parens parens;
8387 parens.require_open (parser);
8388
8389 saved_message = parser->type_definition_forbidden_message;
8390 parser->type_definition_forbidden_message
8391 = G_("types may not be defined in %<noexcept%> expressions");
8392
8393 saved_integral_constant_expression_p
8394 = parser->integral_constant_expression_p;
8395 saved_non_integral_constant_expression_p
8396 = parser->non_integral_constant_expression_p;
8397 parser->integral_constant_expression_p = false;
8398
8399 saved_greater_than_is_operator_p
8400 = parser->greater_than_is_operator_p;
8401 parser->greater_than_is_operator_p = true;
8402
8403 ++cp_unevaluated_operand;
8404 ++c_inhibit_evaluation_warnings;
8405 ++cp_noexcept_operand;
8406 expr = cp_parser_expression (parser);
8407 --cp_noexcept_operand;
8408 --c_inhibit_evaluation_warnings;
8409 --cp_unevaluated_operand;
8410
8411 parser->greater_than_is_operator_p
8412 = saved_greater_than_is_operator_p;
8413
8414 parser->integral_constant_expression_p
8415 = saved_integral_constant_expression_p;
8416 parser->non_integral_constant_expression_p
8417 = saved_non_integral_constant_expression_p;
8418
8419 parser->type_definition_forbidden_message = saved_message;
8420
8421 parens.require_close (parser);
8422
8423 /* Construct a location of the form:
8424 noexcept (expr)
8425 ^~~~~~~~~~~~~~~
8426 with start == caret, finishing at the close-paren. */
8427 location_t noexcept_loc
8428 = make_location (start_loc, start_loc, parser->lexer);
8429
8430 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8431 noexcept_loc);
8432 }
8433
8434 case RID_CO_AWAIT:
8435 {
8436 tree expr;
8437 location_t kw_loc = token->location;
8438
8439 /* Consume the `co_await' token. */
8440 cp_lexer_consume_token (parser->lexer);
8441 /* Parse its cast-expression. */
8442 expr = cp_parser_simple_cast_expression (parser);
8443 if (expr == error_mark_node)
8444 return error_mark_node;
8445
8446 /* Handle [expr.await]. */
8447 return cp_expr (finish_co_await_expr (kw_loc, expr));
8448 }
8449
8450 default:
8451 break;
8452 }
8453 }
8454
8455 /* Look for the `:: new' and `:: delete', which also signal the
8456 beginning of a new-expression, or delete-expression,
8457 respectively. If the next token is `::', then it might be one of
8458 these. */
8459 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8460 {
8461 enum rid keyword;
8462
8463 /* See if the token after the `::' is one of the keywords in
8464 which we're interested. */
8465 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8466 /* If it's `new', we have a new-expression. */
8467 if (keyword == RID_NEW)
8468 return cp_parser_new_expression (parser);
8469 /* Similarly, for `delete'. */
8470 else if (keyword == RID_DELETE)
8471 return cp_parser_delete_expression (parser);
8472 }
8473
8474 /* Look for a unary operator. */
8475 unary_operator = cp_parser_unary_operator (token);
8476 /* The `++' and `--' operators can be handled similarly, even though
8477 they are not technically unary-operators in the grammar. */
8478 if (unary_operator == ERROR_MARK)
8479 {
8480 if (token->type == CPP_PLUS_PLUS)
8481 unary_operator = PREINCREMENT_EXPR;
8482 else if (token->type == CPP_MINUS_MINUS)
8483 unary_operator = PREDECREMENT_EXPR;
8484 /* Handle the GNU address-of-label extension. */
8485 else if (cp_parser_allow_gnu_extensions_p (parser)
8486 && token->type == CPP_AND_AND)
8487 {
8488 tree identifier;
8489 tree expression;
8490 location_t start_loc = token->location;
8491
8492 /* Consume the '&&' token. */
8493 cp_lexer_consume_token (parser->lexer);
8494 /* Look for the identifier. */
8495 identifier = cp_parser_identifier (parser);
8496 /* Construct a location of the form:
8497 &&label
8498 ^~~~~~~
8499 with caret==start at the "&&", finish at the end of the label. */
8500 location_t combined_loc
8501 = make_location (start_loc, start_loc, parser->lexer);
8502 /* Create an expression representing the address. */
8503 expression = finish_label_address_expr (identifier, combined_loc);
8504 if (cp_parser_non_integral_constant_expression (parser,
8505 NIC_ADDR_LABEL))
8506 expression = error_mark_node;
8507 return expression;
8508 }
8509 }
8510 if (unary_operator != ERROR_MARK)
8511 {
8512 cp_expr cast_expression;
8513 cp_expr expression = error_mark_node;
8514 non_integral_constant non_constant_p = NIC_NONE;
8515 location_t loc = token->location;
8516 tsubst_flags_t complain = complain_flags (decltype_p);
8517
8518 /* Consume the operator token. */
8519 token = cp_lexer_consume_token (parser->lexer);
8520 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8521
8522 /* Parse the cast-expression. */
8523 cast_expression
8524 = cp_parser_cast_expression (parser,
8525 unary_operator == ADDR_EXPR,
8526 /*cast_p=*/false,
8527 /*decltype*/false,
8528 pidk);
8529
8530 /* Make a location:
8531 OP_TOKEN CAST_EXPRESSION
8532 ^~~~~~~~~~~~~~~~~~~~~~~~~
8533 with start==caret at the operator token, and
8534 extending to the end of the cast_expression. */
8535 loc = make_location (loc, loc, cast_expression.get_finish ());
8536
8537 /* Now, build an appropriate representation. */
8538 switch (unary_operator)
8539 {
8540 case INDIRECT_REF:
8541 non_constant_p = NIC_STAR;
8542 expression = build_x_indirect_ref (loc, cast_expression,
8543 RO_UNARY_STAR,
8544 complain);
8545 /* TODO: build_x_indirect_ref does not always honor the
8546 location, so ensure it is set. */
8547 expression.set_location (loc);
8548 break;
8549
8550 case ADDR_EXPR:
8551 non_constant_p = NIC_ADDR;
8552 /* Fall through. */
8553 case BIT_NOT_EXPR:
8554 expression = build_x_unary_op (loc, unary_operator,
8555 cast_expression,
8556 complain);
8557 /* TODO: build_x_unary_op does not always honor the location,
8558 so ensure it is set. */
8559 expression.set_location (loc);
8560 break;
8561
8562 case PREINCREMENT_EXPR:
8563 case PREDECREMENT_EXPR:
8564 non_constant_p = unary_operator == PREINCREMENT_EXPR
8565 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8566 /* Fall through. */
8567 case NEGATE_EXPR:
8568 /* Immediately fold negation of a constant, unless the constant is 0
8569 (since -0 == 0) or it would overflow. */
8570 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8571 {
8572 tree stripped_expr
8573 = tree_strip_any_location_wrapper (cast_expression);
8574 if (CONSTANT_CLASS_P (stripped_expr)
8575 && !integer_zerop (stripped_expr)
8576 && !TREE_OVERFLOW (stripped_expr))
8577 {
8578 tree folded = fold_build1 (unary_operator,
8579 TREE_TYPE (stripped_expr),
8580 stripped_expr);
8581 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8582 {
8583 expression = maybe_wrap_with_location (folded, loc);
8584 break;
8585 }
8586 }
8587 }
8588 /* Fall through. */
8589 case UNARY_PLUS_EXPR:
8590 case TRUTH_NOT_EXPR:
8591 expression = finish_unary_op_expr (loc, unary_operator,
8592 cast_expression, complain);
8593 break;
8594
8595 default:
8596 gcc_unreachable ();
8597 }
8598
8599 if (non_constant_p != NIC_NONE
8600 && cp_parser_non_integral_constant_expression (parser,
8601 non_constant_p))
8602 expression = error_mark_node;
8603
8604 return expression;
8605 }
8606
8607 return cp_parser_postfix_expression (parser, address_p, cast_p,
8608 /*member_access_only_p=*/false,
8609 decltype_p,
8610 pidk);
8611 }
8612
8613 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8614 unary-operator, the corresponding tree code is returned. */
8615
8616 static enum tree_code
8617 cp_parser_unary_operator (cp_token* token)
8618 {
8619 switch (token->type)
8620 {
8621 case CPP_MULT:
8622 return INDIRECT_REF;
8623
8624 case CPP_AND:
8625 return ADDR_EXPR;
8626
8627 case CPP_PLUS:
8628 return UNARY_PLUS_EXPR;
8629
8630 case CPP_MINUS:
8631 return NEGATE_EXPR;
8632
8633 case CPP_NOT:
8634 return TRUTH_NOT_EXPR;
8635
8636 case CPP_COMPL:
8637 return BIT_NOT_EXPR;
8638
8639 default:
8640 return ERROR_MARK;
8641 }
8642 }
8643
8644 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8645 Returns a representation of the expression. */
8646
8647 static tree
8648 cp_parser_has_attribute_expression (cp_parser *parser)
8649 {
8650 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8651
8652 /* Consume the __builtin_has_attribute token. */
8653 cp_lexer_consume_token (parser->lexer);
8654
8655 matching_parens parens;
8656 if (!parens.require_open (parser))
8657 return error_mark_node;
8658
8659 /* Types cannot be defined in a `sizeof' expression. Save away the
8660 old message. */
8661 const char *saved_message = parser->type_definition_forbidden_message;
8662 const char *saved_message_arg
8663 = parser->type_definition_forbidden_message_arg;
8664 parser->type_definition_forbidden_message
8665 = G_("types may not be defined in %qs expressions");
8666 parser->type_definition_forbidden_message_arg
8667 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
8668
8669 /* The restrictions on constant-expressions do not apply inside
8670 sizeof expressions. */
8671 bool saved_integral_constant_expression_p
8672 = parser->integral_constant_expression_p;
8673 bool saved_non_integral_constant_expression_p
8674 = parser->non_integral_constant_expression_p;
8675 parser->integral_constant_expression_p = false;
8676
8677 /* Do not actually evaluate the expression. */
8678 ++cp_unevaluated_operand;
8679 ++c_inhibit_evaluation_warnings;
8680
8681 tree oper = NULL_TREE;
8682
8683 /* We can't be sure yet whether we're looking at a type-id or an
8684 expression. */
8685 cp_parser_parse_tentatively (parser);
8686
8687 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8688 parser->in_type_id_in_expr_p = true;
8689 /* Look for the type-id. */
8690 oper = cp_parser_type_id (parser);
8691 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8692
8693 cp_parser_parse_definitely (parser);
8694
8695 /* If the type-id production did not work out, then we must be
8696 looking at an expression. */
8697 if (!oper || oper == error_mark_node)
8698 oper = cp_parser_assignment_expression (parser);
8699
8700 STRIP_ANY_LOCATION_WRAPPER (oper);
8701
8702 /* Go back to evaluating expressions. */
8703 --cp_unevaluated_operand;
8704 --c_inhibit_evaluation_warnings;
8705
8706 /* And restore the old one. */
8707 parser->type_definition_forbidden_message = saved_message;
8708 parser->type_definition_forbidden_message_arg = saved_message_arg;
8709 parser->integral_constant_expression_p
8710 = saved_integral_constant_expression_p;
8711 parser->non_integral_constant_expression_p
8712 = saved_non_integral_constant_expression_p;
8713
8714 /* Consume the comma if it's there. */
8715 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
8716 {
8717 cp_parser_skip_to_closing_parenthesis (parser, false, false,
8718 /*consume_paren=*/true);
8719 return error_mark_node;
8720 }
8721
8722 /* Parse the attribute specification. */
8723 bool ret = false;
8724 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
8725 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
8726 {
8727 if (oper == error_mark_node)
8728 /* Nothing. */;
8729 else if (type_dependent_expression_p (oper))
8730 sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
8731 "not supported yet");
8732 else
8733 {
8734 /* Fold constant expressions used in attributes first. */
8735 cp_check_const_attributes (attr);
8736
8737 /* Finally, see if OPER has been declared with ATTR. */
8738 ret = has_attribute (atloc, oper, attr, default_conversion);
8739 }
8740
8741 parens.require_close (parser);
8742 }
8743 else
8744 {
8745 error_at (atloc, "expected identifier");
8746 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8747 }
8748
8749 /* Construct a location e.g. :
8750 __builtin_has_attribute (oper, attr)
8751 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8752 with start == caret at the start of the built-in token,
8753 and with the endpoint at the final closing paren. */
8754 location_t compound_loc
8755 = make_location (start_loc, start_loc, parser->lexer);
8756
8757 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
8758 ret_expr.set_location (compound_loc);
8759 ret_expr = ret_expr.maybe_add_location_wrapper ();
8760 return ret_expr;
8761 }
8762
8763 /* Parse a new-expression.
8764
8765 new-expression:
8766 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8767 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8768
8769 Returns a representation of the expression. */
8770
8771 static tree
8772 cp_parser_new_expression (cp_parser* parser)
8773 {
8774 bool global_scope_p;
8775 vec<tree, va_gc> *placement;
8776 tree type;
8777 vec<tree, va_gc> *initializer;
8778 tree nelts = NULL_TREE;
8779 tree ret;
8780
8781 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8782
8783 /* Look for the optional `::' operator. */
8784 global_scope_p
8785 = (cp_parser_global_scope_opt (parser,
8786 /*current_scope_valid_p=*/false)
8787 != NULL_TREE);
8788 /* Look for the `new' operator. */
8789 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8790 /* There's no easy way to tell a new-placement from the
8791 `( type-id )' construct. */
8792 cp_parser_parse_tentatively (parser);
8793 /* Look for a new-placement. */
8794 placement = cp_parser_new_placement (parser);
8795 /* If that didn't work out, there's no new-placement. */
8796 if (!cp_parser_parse_definitely (parser))
8797 {
8798 if (placement != NULL)
8799 release_tree_vector (placement);
8800 placement = NULL;
8801 }
8802
8803 /* If the next token is a `(', then we have a parenthesized
8804 type-id. */
8805 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8806 {
8807 cp_token *token;
8808 const char *saved_message = parser->type_definition_forbidden_message;
8809
8810 /* Consume the `('. */
8811 matching_parens parens;
8812 parens.consume_open (parser);
8813
8814 /* Parse the type-id. */
8815 parser->type_definition_forbidden_message
8816 = G_("types may not be defined in a new-expression");
8817 {
8818 type_id_in_expr_sentinel s (parser);
8819 type = cp_parser_type_id (parser);
8820 }
8821 parser->type_definition_forbidden_message = saved_message;
8822
8823 /* Look for the closing `)'. */
8824 parens.require_close (parser);
8825 token = cp_lexer_peek_token (parser->lexer);
8826 /* There should not be a direct-new-declarator in this production,
8827 but GCC used to allowed this, so we check and emit a sensible error
8828 message for this case. */
8829 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8830 {
8831 error_at (token->location,
8832 "array bound forbidden after parenthesized type-id");
8833 inform (token->location,
8834 "try removing the parentheses around the type-id");
8835 cp_parser_direct_new_declarator (parser);
8836 }
8837 }
8838 /* Otherwise, there must be a new-type-id. */
8839 else
8840 type = cp_parser_new_type_id (parser, &nelts);
8841
8842 /* If the next token is a `(' or '{', then we have a new-initializer. */
8843 cp_token *token = cp_lexer_peek_token (parser->lexer);
8844 if (token->type == CPP_OPEN_PAREN
8845 || token->type == CPP_OPEN_BRACE)
8846 initializer = cp_parser_new_initializer (parser);
8847 else
8848 initializer = NULL;
8849
8850 /* A new-expression may not appear in an integral constant
8851 expression. */
8852 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8853 ret = error_mark_node;
8854 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8855 of a new-type-id or type-id of a new-expression, the new-expression shall
8856 contain a new-initializer of the form ( assignment-expression )".
8857 Additionally, consistently with the spirit of DR 1467, we want to accept
8858 'new auto { 2 }' too. */
8859 else if ((ret = type_uses_auto (type))
8860 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8861 && (vec_safe_length (initializer) != 1
8862 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8863 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8864 {
8865 error_at (token->location,
8866 "initialization of new-expression for type %<auto%> "
8867 "requires exactly one element");
8868 ret = error_mark_node;
8869 }
8870 else
8871 {
8872 /* Construct a location e.g.:
8873 ptr = new int[100]
8874 ^~~~~~~~~~~~
8875 with caret == start at the start of the "new" token, and the end
8876 at the end of the final token we consumed. */
8877 location_t combined_loc = make_location (start_loc, start_loc,
8878 parser->lexer);
8879 /* Create a representation of the new-expression. */
8880 ret = build_new (combined_loc, &placement, type, nelts, &initializer,
8881 global_scope_p, tf_warning_or_error);
8882 }
8883
8884 if (placement != NULL)
8885 release_tree_vector (placement);
8886 if (initializer != NULL)
8887 release_tree_vector (initializer);
8888
8889 return ret;
8890 }
8891
8892 /* Parse a new-placement.
8893
8894 new-placement:
8895 ( expression-list )
8896
8897 Returns the same representation as for an expression-list. */
8898
8899 static vec<tree, va_gc> *
8900 cp_parser_new_placement (cp_parser* parser)
8901 {
8902 vec<tree, va_gc> *expression_list;
8903
8904 /* Parse the expression-list. */
8905 expression_list = (cp_parser_parenthesized_expression_list
8906 (parser, non_attr, /*cast_p=*/false,
8907 /*allow_expansion_p=*/true,
8908 /*non_constant_p=*/NULL));
8909
8910 if (expression_list && expression_list->is_empty ())
8911 error ("expected expression-list or type-id");
8912
8913 return expression_list;
8914 }
8915
8916 /* Parse a new-type-id.
8917
8918 new-type-id:
8919 type-specifier-seq new-declarator [opt]
8920
8921 Returns the TYPE allocated. If the new-type-id indicates an array
8922 type, *NELTS is set to the number of elements in the last array
8923 bound; the TYPE will not include the last array bound. */
8924
8925 static tree
8926 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8927 {
8928 cp_decl_specifier_seq type_specifier_seq;
8929 cp_declarator *new_declarator;
8930 cp_declarator *declarator;
8931 cp_declarator *outer_declarator;
8932 const char *saved_message;
8933
8934 /* The type-specifier sequence must not contain type definitions.
8935 (It cannot contain declarations of new types either, but if they
8936 are not definitions we will catch that because they are not
8937 complete.) */
8938 saved_message = parser->type_definition_forbidden_message;
8939 parser->type_definition_forbidden_message
8940 = G_("types may not be defined in a new-type-id");
8941 /* Parse the type-specifier-seq. */
8942 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
8943 /*is_declaration=*/false,
8944 /*is_trailing_return=*/false,
8945 &type_specifier_seq);
8946 /* Restore the old message. */
8947 parser->type_definition_forbidden_message = saved_message;
8948
8949 if (type_specifier_seq.type == error_mark_node)
8950 return error_mark_node;
8951
8952 /* Parse the new-declarator. */
8953 new_declarator = cp_parser_new_declarator_opt (parser);
8954
8955 /* Determine the number of elements in the last array dimension, if
8956 any. */
8957 *nelts = NULL_TREE;
8958 /* Skip down to the last array dimension. */
8959 declarator = new_declarator;
8960 outer_declarator = NULL;
8961 while (declarator && (declarator->kind == cdk_pointer
8962 || declarator->kind == cdk_ptrmem))
8963 {
8964 outer_declarator = declarator;
8965 declarator = declarator->declarator;
8966 }
8967 while (declarator
8968 && declarator->kind == cdk_array
8969 && declarator->declarator
8970 && declarator->declarator->kind == cdk_array)
8971 {
8972 outer_declarator = declarator;
8973 declarator = declarator->declarator;
8974 }
8975
8976 if (declarator && declarator->kind == cdk_array)
8977 {
8978 *nelts = declarator->u.array.bounds;
8979 if (*nelts == error_mark_node)
8980 *nelts = integer_one_node;
8981
8982 if (outer_declarator)
8983 outer_declarator->declarator = declarator->declarator;
8984 else
8985 new_declarator = NULL;
8986 }
8987
8988 return groktypename (&type_specifier_seq, new_declarator, false);
8989 }
8990
8991 /* Parse an (optional) new-declarator.
8992
8993 new-declarator:
8994 ptr-operator new-declarator [opt]
8995 direct-new-declarator
8996
8997 Returns the declarator. */
8998
8999 static cp_declarator *
9000 cp_parser_new_declarator_opt (cp_parser* parser)
9001 {
9002 enum tree_code code;
9003 tree type, std_attributes = NULL_TREE;
9004 cp_cv_quals cv_quals;
9005
9006 /* We don't know if there's a ptr-operator next, or not. */
9007 cp_parser_parse_tentatively (parser);
9008 /* Look for a ptr-operator. */
9009 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
9010 /* If that worked, look for more new-declarators. */
9011 if (cp_parser_parse_definitely (parser))
9012 {
9013 cp_declarator *declarator;
9014
9015 /* Parse another optional declarator. */
9016 declarator = cp_parser_new_declarator_opt (parser);
9017
9018 declarator = cp_parser_make_indirect_declarator
9019 (code, type, cv_quals, declarator, std_attributes);
9020
9021 return declarator;
9022 }
9023
9024 /* If the next token is a `[', there is a direct-new-declarator. */
9025 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9026 return cp_parser_direct_new_declarator (parser);
9027
9028 return NULL;
9029 }
9030
9031 /* Parse a direct-new-declarator.
9032
9033 direct-new-declarator:
9034 [ expression ]
9035 direct-new-declarator [constant-expression]
9036
9037 */
9038
9039 static cp_declarator *
9040 cp_parser_direct_new_declarator (cp_parser* parser)
9041 {
9042 cp_declarator *declarator = NULL;
9043
9044 while (true)
9045 {
9046 tree expression;
9047 cp_token *token;
9048
9049 /* Look for the opening `['. */
9050 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9051
9052 token = cp_lexer_peek_token (parser->lexer);
9053 expression = cp_parser_expression (parser);
9054 /* The standard requires that the expression have integral
9055 type. DR 74 adds enumeration types. We believe that the
9056 real intent is that these expressions be handled like the
9057 expression in a `switch' condition, which also allows
9058 classes with a single conversion to integral or
9059 enumeration type. */
9060 if (!processing_template_decl)
9061 {
9062 expression
9063 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
9064 expression,
9065 /*complain=*/true);
9066 if (!expression)
9067 {
9068 error_at (token->location,
9069 "expression in new-declarator must have integral "
9070 "or enumeration type");
9071 expression = error_mark_node;
9072 }
9073 }
9074
9075 /* Look for the closing `]'. */
9076 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9077
9078 /* Add this bound to the declarator. */
9079 declarator = make_array_declarator (declarator, expression);
9080
9081 /* If the next token is not a `[', then there are no more
9082 bounds. */
9083 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
9084 break;
9085 }
9086
9087 return declarator;
9088 }
9089
9090 /* Parse a new-initializer.
9091
9092 new-initializer:
9093 ( expression-list [opt] )
9094 braced-init-list
9095
9096 Returns a representation of the expression-list. */
9097
9098 static vec<tree, va_gc> *
9099 cp_parser_new_initializer (cp_parser* parser)
9100 {
9101 vec<tree, va_gc> *expression_list;
9102
9103 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9104 {
9105 tree t;
9106 bool expr_non_constant_p;
9107 cp_lexer_set_source_position (parser->lexer);
9108 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9109 t = cp_parser_braced_list (parser, &expr_non_constant_p);
9110 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
9111 expression_list = make_tree_vector_single (t);
9112 }
9113 else
9114 expression_list = (cp_parser_parenthesized_expression_list
9115 (parser, non_attr, /*cast_p=*/false,
9116 /*allow_expansion_p=*/true,
9117 /*non_constant_p=*/NULL));
9118
9119 return expression_list;
9120 }
9121
9122 /* Parse a delete-expression.
9123
9124 delete-expression:
9125 :: [opt] delete cast-expression
9126 :: [opt] delete [ ] cast-expression
9127
9128 Returns a representation of the expression. */
9129
9130 static tree
9131 cp_parser_delete_expression (cp_parser* parser)
9132 {
9133 bool global_scope_p;
9134 bool array_p;
9135 tree expression;
9136 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9137
9138 /* Look for the optional `::' operator. */
9139 global_scope_p
9140 = (cp_parser_global_scope_opt (parser,
9141 /*current_scope_valid_p=*/false)
9142 != NULL_TREE);
9143 /* Look for the `delete' keyword. */
9144 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9145 /* See if the array syntax is in use. */
9146 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9147 {
9148 /* Consume the `[' token. */
9149 cp_lexer_consume_token (parser->lexer);
9150 /* Look for the `]' token. */
9151 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9152 /* Remember that this is the `[]' construct. */
9153 array_p = true;
9154 }
9155 else
9156 array_p = false;
9157
9158 /* Parse the cast-expression. */
9159 expression = cp_parser_simple_cast_expression (parser);
9160
9161 /* A delete-expression may not appear in an integral constant
9162 expression. */
9163 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9164 return error_mark_node;
9165
9166 /* Construct a location e.g.:
9167 delete [ ] ptr
9168 ^~~~~~~~~~~~~~
9169 with caret == start at the start of the "delete" token, and
9170 the end at the end of the final token we consumed. */
9171 location_t combined_loc = make_location (start_loc, start_loc,
9172 parser->lexer);
9173 expression = delete_sanity (combined_loc, expression, NULL_TREE, array_p,
9174 global_scope_p, tf_warning_or_error);
9175
9176 return expression;
9177 }
9178
9179 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9180 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9181 0 otherwise. */
9182
9183 static int
9184 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9185 {
9186 cp_token *token = cp_lexer_peek_token (parser->lexer);
9187 switch (token->type)
9188 {
9189 case CPP_COMMA:
9190 case CPP_SEMICOLON:
9191 case CPP_QUERY:
9192 case CPP_COLON:
9193 case CPP_CLOSE_SQUARE:
9194 case CPP_CLOSE_PAREN:
9195 case CPP_CLOSE_BRACE:
9196 case CPP_OPEN_BRACE:
9197 case CPP_DOT:
9198 case CPP_DOT_STAR:
9199 case CPP_DEREF:
9200 case CPP_DEREF_STAR:
9201 case CPP_DIV:
9202 case CPP_MOD:
9203 case CPP_LSHIFT:
9204 case CPP_RSHIFT:
9205 case CPP_LESS:
9206 case CPP_GREATER:
9207 case CPP_LESS_EQ:
9208 case CPP_GREATER_EQ:
9209 case CPP_EQ_EQ:
9210 case CPP_NOT_EQ:
9211 case CPP_EQ:
9212 case CPP_MULT_EQ:
9213 case CPP_DIV_EQ:
9214 case CPP_MOD_EQ:
9215 case CPP_PLUS_EQ:
9216 case CPP_MINUS_EQ:
9217 case CPP_RSHIFT_EQ:
9218 case CPP_LSHIFT_EQ:
9219 case CPP_AND_EQ:
9220 case CPP_XOR_EQ:
9221 case CPP_OR_EQ:
9222 case CPP_XOR:
9223 case CPP_OR:
9224 case CPP_OR_OR:
9225 case CPP_EOF:
9226 case CPP_ELLIPSIS:
9227 return 0;
9228
9229 case CPP_OPEN_PAREN:
9230 /* In ((type ()) () the last () isn't a valid cast-expression,
9231 so the whole must be parsed as postfix-expression. */
9232 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9233 != CPP_CLOSE_PAREN;
9234
9235 case CPP_OPEN_SQUARE:
9236 /* '[' may start a primary-expression in obj-c++ and in C++11,
9237 as a lambda-expression, eg, '(void)[]{}'. */
9238 if (cxx_dialect >= cxx11)
9239 return -1;
9240 return c_dialect_objc ();
9241
9242 case CPP_PLUS_PLUS:
9243 case CPP_MINUS_MINUS:
9244 /* '++' and '--' may or may not start a cast-expression:
9245
9246 struct T { void operator++(int); };
9247 void f() { (T())++; }
9248
9249 vs
9250
9251 int a;
9252 (int)++a; */
9253 return -1;
9254
9255 default:
9256 return 1;
9257 }
9258 }
9259
9260 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9261 in the order: const_cast, static_cast, reinterpret_cast.
9262
9263 Don't suggest dynamic_cast.
9264
9265 Return the first legal cast kind found, or NULL otherwise. */
9266
9267 static const char *
9268 get_cast_suggestion (tree dst_type, tree orig_expr)
9269 {
9270 tree trial;
9271
9272 /* Reuse the parser logic by attempting to build the various kinds of
9273 cast, with "complain" disabled.
9274 Identify the first such cast that is valid. */
9275
9276 /* Don't attempt to run such logic within template processing. */
9277 if (processing_template_decl)
9278 return NULL;
9279
9280 /* First try const_cast. */
9281 trial = build_const_cast (input_location, dst_type, orig_expr, tf_none);
9282 if (trial != error_mark_node)
9283 return "const_cast";
9284
9285 /* If that fails, try static_cast. */
9286 trial = build_static_cast (input_location, dst_type, orig_expr, tf_none);
9287 if (trial != error_mark_node)
9288 return "static_cast";
9289
9290 /* Finally, try reinterpret_cast. */
9291 trial = build_reinterpret_cast (input_location, dst_type, orig_expr,
9292 tf_none);
9293 if (trial != error_mark_node)
9294 return "reinterpret_cast";
9295
9296 /* No such cast possible. */
9297 return NULL;
9298 }
9299
9300 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9301 suggesting how to convert a C-style cast of the form:
9302
9303 (DST_TYPE)ORIG_EXPR
9304
9305 to a C++-style cast.
9306
9307 The primary range of RICHLOC is asssumed to be that of the original
9308 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9309 of the parens in the C-style cast. */
9310
9311 static void
9312 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9313 location_t close_paren_loc, tree orig_expr,
9314 tree dst_type)
9315 {
9316 /* This function is non-trivial, so bail out now if the warning isn't
9317 going to be emitted. */
9318 if (!warn_old_style_cast)
9319 return;
9320
9321 /* Try to find a legal C++ cast, trying them in order:
9322 const_cast, static_cast, reinterpret_cast. */
9323 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9324 if (!cast_suggestion)
9325 return;
9326
9327 /* Replace the open paren with "CAST_SUGGESTION<". */
9328 pretty_printer pp;
9329 pp_printf (&pp, "%s<", cast_suggestion);
9330 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9331
9332 /* Replace the close paren with "> (". */
9333 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9334
9335 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9336 rich_loc->add_fixit_insert_after (")");
9337 }
9338
9339
9340 /* Parse a cast-expression.
9341
9342 cast-expression:
9343 unary-expression
9344 ( type-id ) cast-expression
9345
9346 ADDRESS_P is true iff the unary-expression is appearing as the
9347 operand of the `&' operator. CAST_P is true if this expression is
9348 the target of a cast.
9349
9350 Returns a representation of the expression. */
9351
9352 static cp_expr
9353 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9354 bool decltype_p, cp_id_kind * pidk)
9355 {
9356 /* If it's a `(', then we might be looking at a cast. */
9357 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9358 {
9359 tree type = NULL_TREE;
9360 cp_expr expr (NULL_TREE);
9361 int cast_expression = 0;
9362 const char *saved_message;
9363
9364 /* There's no way to know yet whether or not this is a cast.
9365 For example, `(int (3))' is a unary-expression, while `(int)
9366 3' is a cast. So, we resort to parsing tentatively. */
9367 cp_parser_parse_tentatively (parser);
9368 /* Types may not be defined in a cast. */
9369 saved_message = parser->type_definition_forbidden_message;
9370 parser->type_definition_forbidden_message
9371 = G_("types may not be defined in casts");
9372 /* Consume the `('. */
9373 matching_parens parens;
9374 cp_token *open_paren = parens.consume_open (parser);
9375 location_t open_paren_loc = open_paren->location;
9376 location_t close_paren_loc = UNKNOWN_LOCATION;
9377
9378 /* A very tricky bit is that `(struct S) { 3 }' is a
9379 compound-literal (which we permit in C++ as an extension).
9380 But, that construct is not a cast-expression -- it is a
9381 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9382 is legal; if the compound-literal were a cast-expression,
9383 you'd need an extra set of parentheses.) But, if we parse
9384 the type-id, and it happens to be a class-specifier, then we
9385 will commit to the parse at that point, because we cannot
9386 undo the action that is done when creating a new class. So,
9387 then we cannot back up and do a postfix-expression.
9388
9389 Another tricky case is the following (c++/29234):
9390
9391 struct S { void operator () (); };
9392
9393 void foo ()
9394 {
9395 ( S()() );
9396 }
9397
9398 As a type-id we parse the parenthesized S()() as a function
9399 returning a function, groktypename complains and we cannot
9400 back up in this case either.
9401
9402 Therefore, we scan ahead to the closing `)', and check to see
9403 if the tokens after the `)' can start a cast-expression. Otherwise
9404 we are dealing with an unary-expression, a postfix-expression
9405 or something else.
9406
9407 Yet another tricky case, in C++11, is the following (c++/54891):
9408
9409 (void)[]{};
9410
9411 The issue is that usually, besides the case of lambda-expressions,
9412 the parenthesized type-id cannot be followed by '[', and, eg, we
9413 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9414 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9415 we don't commit, we try a cast-expression, then an unary-expression.
9416
9417 Save tokens so that we can put them back. */
9418 cp_lexer_save_tokens (parser->lexer);
9419
9420 /* We may be looking at a cast-expression. */
9421 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9422 /*consume_paren=*/true))
9423 cast_expression
9424 = cp_parser_tokens_start_cast_expression (parser);
9425
9426 /* Roll back the tokens we skipped. */
9427 cp_lexer_rollback_tokens (parser->lexer);
9428 /* If we aren't looking at a cast-expression, simulate an error so
9429 that the call to cp_parser_error_occurred below returns true. */
9430 if (!cast_expression)
9431 cp_parser_simulate_error (parser);
9432 else
9433 {
9434 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9435 parser->in_type_id_in_expr_p = true;
9436 /* Look for the type-id. */
9437 type = cp_parser_type_id (parser);
9438 /* Look for the closing `)'. */
9439 cp_token *close_paren = parens.require_close (parser);
9440 if (close_paren)
9441 close_paren_loc = close_paren->location;
9442 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9443 }
9444
9445 /* Restore the saved message. */
9446 parser->type_definition_forbidden_message = saved_message;
9447
9448 /* At this point this can only be either a cast or a
9449 parenthesized ctor such as `(T ())' that looks like a cast to
9450 function returning T. */
9451 if (!cp_parser_error_occurred (parser))
9452 {
9453 /* Only commit if the cast-expression doesn't start with
9454 '++', '--', or '[' in C++11. */
9455 if (cast_expression > 0)
9456 cp_parser_commit_to_topmost_tentative_parse (parser);
9457
9458 expr = cp_parser_cast_expression (parser,
9459 /*address_p=*/false,
9460 /*cast_p=*/true,
9461 /*decltype_p=*/false,
9462 pidk);
9463
9464 if (cp_parser_parse_definitely (parser))
9465 {
9466 /* Warn about old-style casts, if so requested. */
9467 if (warn_old_style_cast
9468 && !in_system_header_at (input_location)
9469 && !VOID_TYPE_P (type)
9470 && current_lang_name != lang_name_c)
9471 {
9472 gcc_rich_location rich_loc (input_location);
9473 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9474 expr, type);
9475 warning_at (&rich_loc, OPT_Wold_style_cast,
9476 "use of old-style cast to %q#T", type);
9477 }
9478
9479 /* Only type conversions to integral or enumeration types
9480 can be used in constant-expressions. */
9481 if (!cast_valid_in_integral_constant_expression_p (type)
9482 && cp_parser_non_integral_constant_expression (parser,
9483 NIC_CAST))
9484 return error_mark_node;
9485
9486 /* Perform the cast. */
9487 /* Make a location:
9488 (TYPE) EXPR
9489 ^~~~~~~~~~~
9490 with start==caret at the open paren, extending to the
9491 end of "expr". */
9492 location_t cast_loc = make_location (open_paren_loc,
9493 open_paren_loc,
9494 expr.get_finish ());
9495 expr = build_c_cast (cast_loc, type, expr);
9496 return expr;
9497 }
9498 }
9499 else
9500 cp_parser_abort_tentative_parse (parser);
9501 }
9502
9503 /* If we get here, then it's not a cast, so it must be a
9504 unary-expression. */
9505 return cp_parser_unary_expression (parser, pidk, address_p,
9506 cast_p, decltype_p);
9507 }
9508
9509 /* Parse a binary expression of the general form:
9510
9511 pm-expression:
9512 cast-expression
9513 pm-expression .* cast-expression
9514 pm-expression ->* cast-expression
9515
9516 multiplicative-expression:
9517 pm-expression
9518 multiplicative-expression * pm-expression
9519 multiplicative-expression / pm-expression
9520 multiplicative-expression % pm-expression
9521
9522 additive-expression:
9523 multiplicative-expression
9524 additive-expression + multiplicative-expression
9525 additive-expression - multiplicative-expression
9526
9527 shift-expression:
9528 additive-expression
9529 shift-expression << additive-expression
9530 shift-expression >> additive-expression
9531
9532 relational-expression:
9533 shift-expression
9534 relational-expression < shift-expression
9535 relational-expression > shift-expression
9536 relational-expression <= shift-expression
9537 relational-expression >= shift-expression
9538
9539 GNU Extension:
9540
9541 relational-expression:
9542 relational-expression <? shift-expression
9543 relational-expression >? shift-expression
9544
9545 equality-expression:
9546 relational-expression
9547 equality-expression == relational-expression
9548 equality-expression != relational-expression
9549
9550 and-expression:
9551 equality-expression
9552 and-expression & equality-expression
9553
9554 exclusive-or-expression:
9555 and-expression
9556 exclusive-or-expression ^ and-expression
9557
9558 inclusive-or-expression:
9559 exclusive-or-expression
9560 inclusive-or-expression | exclusive-or-expression
9561
9562 logical-and-expression:
9563 inclusive-or-expression
9564 logical-and-expression && inclusive-or-expression
9565
9566 logical-or-expression:
9567 logical-and-expression
9568 logical-or-expression || logical-and-expression
9569
9570 All these are implemented with a single function like:
9571
9572 binary-expression:
9573 simple-cast-expression
9574 binary-expression <token> binary-expression
9575
9576 CAST_P is true if this expression is the target of a cast.
9577
9578 The binops_by_token map is used to get the tree codes for each <token> type.
9579 binary-expressions are associated according to a precedence table. */
9580
9581 #define TOKEN_PRECEDENCE(token) \
9582 (((token->type == CPP_GREATER \
9583 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9584 && !parser->greater_than_is_operator_p) \
9585 ? PREC_NOT_OPERATOR \
9586 : binops_by_token[token->type].prec)
9587
9588 static cp_expr
9589 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9590 bool no_toplevel_fold_p,
9591 bool decltype_p,
9592 enum cp_parser_prec prec,
9593 cp_id_kind * pidk)
9594 {
9595 cp_parser_expression_stack stack;
9596 cp_parser_expression_stack_entry *sp = &stack[0];
9597 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
9598 cp_parser_expression_stack_entry current;
9599 cp_expr rhs;
9600 cp_token *token;
9601 enum tree_code rhs_type;
9602 enum cp_parser_prec new_prec, lookahead_prec;
9603 tree overload;
9604
9605 /* Parse the first expression. */
9606 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9607 ? TRUTH_NOT_EXPR : ERROR_MARK);
9608 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9609 cast_p, decltype_p, pidk);
9610 current.prec = prec;
9611
9612 if (cp_parser_error_occurred (parser))
9613 return error_mark_node;
9614
9615 for (;;)
9616 {
9617 /* Get an operator token. */
9618 token = cp_lexer_peek_token (parser->lexer);
9619
9620 if (warn_cxx11_compat
9621 && token->type == CPP_RSHIFT
9622 && !parser->greater_than_is_operator_p)
9623 {
9624 if (warning_at (token->location, OPT_Wc__11_compat,
9625 "%<>>%> operator is treated"
9626 " as two right angle brackets in C++11"))
9627 inform (token->location,
9628 "suggest parentheses around %<>>%> expression");
9629 }
9630
9631 new_prec = TOKEN_PRECEDENCE (token);
9632 if (new_prec != PREC_NOT_OPERATOR
9633 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9634 /* This is a fold-expression; handle it later. */
9635 new_prec = PREC_NOT_OPERATOR;
9636
9637 /* Popping an entry off the stack means we completed a subexpression:
9638 - either we found a token which is not an operator (`>' where it is not
9639 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9640 will happen repeatedly;
9641 - or, we found an operator which has lower priority. This is the case
9642 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9643 parsing `3 * 4'. */
9644 if (new_prec <= current.prec)
9645 {
9646 if (sp == stack)
9647 break;
9648 else
9649 goto pop;
9650 }
9651
9652 get_rhs:
9653 current.tree_type = binops_by_token[token->type].tree_type;
9654 current.loc = token->location;
9655
9656 /* We used the operator token. */
9657 cp_lexer_consume_token (parser->lexer);
9658
9659 /* For "false && x" or "true || x", x will never be executed;
9660 disable warnings while evaluating it. */
9661 if ((current.tree_type == TRUTH_ANDIF_EXPR
9662 && cp_fully_fold (current.lhs) == truthvalue_false_node)
9663 || (current.tree_type == TRUTH_ORIF_EXPR
9664 && cp_fully_fold (current.lhs) == truthvalue_true_node))
9665 {
9666 disable_warnings_sp = sp;
9667 ++c_inhibit_evaluation_warnings;
9668 }
9669
9670 /* Extract another operand. It may be the RHS of this expression
9671 or the LHS of a new, higher priority expression. */
9672 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9673 ? TRUTH_NOT_EXPR : ERROR_MARK);
9674 rhs = cp_parser_simple_cast_expression (parser);
9675
9676 /* Get another operator token. Look up its precedence to avoid
9677 building a useless (immediately popped) stack entry for common
9678 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9679 token = cp_lexer_peek_token (parser->lexer);
9680 lookahead_prec = TOKEN_PRECEDENCE (token);
9681 if (lookahead_prec != PREC_NOT_OPERATOR
9682 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9683 lookahead_prec = PREC_NOT_OPERATOR;
9684 if (lookahead_prec > new_prec)
9685 {
9686 /* ... and prepare to parse the RHS of the new, higher priority
9687 expression. Since precedence levels on the stack are
9688 monotonically increasing, we do not have to care about
9689 stack overflows. */
9690 *sp = current;
9691 ++sp;
9692 current.lhs = rhs;
9693 current.lhs_type = rhs_type;
9694 current.prec = new_prec;
9695 new_prec = lookahead_prec;
9696 goto get_rhs;
9697
9698 pop:
9699 lookahead_prec = new_prec;
9700 /* If the stack is not empty, we have parsed into LHS the right side
9701 (`4' in the example above) of an expression we had suspended.
9702 We can use the information on the stack to recover the LHS (`3')
9703 from the stack together with the tree code (`MULT_EXPR'), and
9704 the precedence of the higher level subexpression
9705 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9706 which will be used to actually build the additive expression. */
9707 rhs = current.lhs;
9708 rhs_type = current.lhs_type;
9709 --sp;
9710 current = *sp;
9711 }
9712
9713 /* Undo the disabling of warnings done above. */
9714 if (sp == disable_warnings_sp)
9715 {
9716 disable_warnings_sp = NULL;
9717 --c_inhibit_evaluation_warnings;
9718 }
9719
9720 if (warn_logical_not_paren
9721 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9722 && current.lhs_type == TRUTH_NOT_EXPR
9723 /* Avoid warning for !!x == y. */
9724 && (TREE_CODE (current.lhs) != NE_EXPR
9725 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9726 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9727 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9728 /* Avoid warning for !b == y where b is boolean. */
9729 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9730 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9731 != BOOLEAN_TYPE))))
9732 /* Avoid warning for !!b == y where b is boolean. */
9733 && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
9734 || TREE_TYPE (current.lhs) == NULL_TREE
9735 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9736 warn_logical_not_parentheses (current.loc, current.tree_type,
9737 current.lhs, maybe_constant_value (rhs));
9738
9739 overload = NULL;
9740
9741 location_t combined_loc = make_location (current.loc,
9742 current.lhs.get_start (),
9743 rhs.get_finish ());
9744
9745 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9746 ERROR_MARK for everything that is not a binary expression.
9747 This makes warn_about_parentheses miss some warnings that
9748 involve unary operators. For unary expressions we should
9749 pass the correct tree_code unless the unary expression was
9750 surrounded by parentheses.
9751 */
9752 if (no_toplevel_fold_p
9753 && lookahead_prec <= current.prec
9754 && sp == stack)
9755 {
9756 if (current.lhs == error_mark_node || rhs == error_mark_node)
9757 current.lhs = error_mark_node;
9758 else
9759 {
9760 current.lhs.maybe_add_location_wrapper ();
9761 rhs.maybe_add_location_wrapper ();
9762 current.lhs
9763 = build_min (current.tree_type,
9764 TREE_CODE_CLASS (current.tree_type)
9765 == tcc_comparison
9766 ? boolean_type_node : TREE_TYPE (current.lhs),
9767 current.lhs.get_value (), rhs.get_value ());
9768 SET_EXPR_LOCATION (current.lhs, combined_loc);
9769 }
9770 }
9771 else
9772 {
9773 op_location_t op_loc (current.loc, combined_loc);
9774 current.lhs = build_x_binary_op (op_loc, current.tree_type,
9775 current.lhs, current.lhs_type,
9776 rhs, rhs_type, &overload,
9777 complain_flags (decltype_p));
9778 /* TODO: build_x_binary_op doesn't always honor the location. */
9779 current.lhs.set_location (combined_loc);
9780 }
9781 current.lhs_type = current.tree_type;
9782
9783 /* If the binary operator required the use of an overloaded operator,
9784 then this expression cannot be an integral constant-expression.
9785 An overloaded operator can be used even if both operands are
9786 otherwise permissible in an integral constant-expression if at
9787 least one of the operands is of enumeration type. */
9788
9789 if (overload
9790 && cp_parser_non_integral_constant_expression (parser,
9791 NIC_OVERLOADED))
9792 return error_mark_node;
9793 }
9794
9795 return current.lhs;
9796 }
9797
9798 static cp_expr
9799 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9800 bool no_toplevel_fold_p,
9801 enum cp_parser_prec prec,
9802 cp_id_kind * pidk)
9803 {
9804 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9805 /*decltype*/false, prec, pidk);
9806 }
9807
9808 /* Parse the `? expression : assignment-expression' part of a
9809 conditional-expression. The LOGICAL_OR_EXPR is the
9810 logical-or-expression that started the conditional-expression.
9811 Returns a representation of the entire conditional-expression.
9812
9813 This routine is used by cp_parser_assignment_expression.
9814
9815 ? expression : assignment-expression
9816
9817 GNU Extensions:
9818
9819 ? : assignment-expression */
9820
9821 static tree
9822 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9823 {
9824 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9825 cp_expr assignment_expr;
9826 struct cp_token *token;
9827 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9828
9829 /* Consume the `?' token. */
9830 cp_lexer_consume_token (parser->lexer);
9831 token = cp_lexer_peek_token (parser->lexer);
9832 if (cp_parser_allow_gnu_extensions_p (parser)
9833 && token->type == CPP_COLON)
9834 {
9835 pedwarn (token->location, OPT_Wpedantic,
9836 "ISO C++ does not allow %<?:%> with omitted middle operand");
9837 /* Implicit true clause. */
9838 expr = NULL_TREE;
9839 c_inhibit_evaluation_warnings +=
9840 folded_logical_or_expr == truthvalue_true_node;
9841 warn_for_omitted_condop (token->location, logical_or_expr);
9842 }
9843 else
9844 {
9845 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9846 parser->colon_corrects_to_scope_p = false;
9847 /* Parse the expression. */
9848 c_inhibit_evaluation_warnings +=
9849 folded_logical_or_expr == truthvalue_false_node;
9850 expr = cp_parser_expression (parser);
9851 c_inhibit_evaluation_warnings +=
9852 ((folded_logical_or_expr == truthvalue_true_node)
9853 - (folded_logical_or_expr == truthvalue_false_node));
9854 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9855 }
9856
9857 /* The next token should be a `:'. */
9858 cp_parser_require (parser, CPP_COLON, RT_COLON);
9859 /* Parse the assignment-expression. */
9860 assignment_expr = cp_parser_assignment_expression (parser);
9861 c_inhibit_evaluation_warnings -=
9862 folded_logical_or_expr == truthvalue_true_node;
9863
9864 /* Make a location:
9865 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9866 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9867 with the caret at the "?", ranging from the start of
9868 the logical_or_expr to the end of the assignment_expr. */
9869 loc = make_location (loc,
9870 logical_or_expr.get_start (),
9871 assignment_expr.get_finish ());
9872
9873 /* Build the conditional-expression. */
9874 return build_x_conditional_expr (loc, logical_or_expr,
9875 expr,
9876 assignment_expr,
9877 tf_warning_or_error);
9878 }
9879
9880 /* Parse an assignment-expression.
9881
9882 assignment-expression:
9883 conditional-expression
9884 logical-or-expression assignment-operator assignment_expression
9885 throw-expression
9886 yield-expression
9887
9888 CAST_P is true if this expression is the target of a cast.
9889 DECLTYPE_P is true if this expression is the operand of decltype.
9890
9891 Returns a representation for the expression. */
9892
9893 static cp_expr
9894 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9895 bool cast_p, bool decltype_p)
9896 {
9897 cp_expr expr;
9898
9899 /* If the next token is the `throw' keyword, then we're looking at
9900 a throw-expression. */
9901 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9902 expr = cp_parser_throw_expression (parser);
9903 /* If the next token is the `co_yield' keyword, then we're looking at
9904 a yield-expression. */
9905 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CO_YIELD))
9906 expr = cp_parser_yield_expression (parser);
9907 /* Otherwise, it must be that we are looking at a
9908 logical-or-expression. */
9909 else
9910 {
9911 /* Parse the binary expressions (logical-or-expression). */
9912 expr = cp_parser_binary_expression (parser, cast_p, false,
9913 decltype_p,
9914 PREC_NOT_OPERATOR, pidk);
9915 /* If the next token is a `?' then we're actually looking at a
9916 conditional-expression. */
9917 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9918 return cp_parser_question_colon_clause (parser, expr);
9919 else
9920 {
9921 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9922
9923 /* If it's an assignment-operator, we're using the second
9924 production. */
9925 enum tree_code assignment_operator
9926 = cp_parser_assignment_operator_opt (parser);
9927 if (assignment_operator != ERROR_MARK)
9928 {
9929 bool non_constant_p;
9930
9931 /* Parse the right-hand side of the assignment. */
9932 cp_expr rhs = cp_parser_initializer_clause (parser,
9933 &non_constant_p);
9934
9935 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9936 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9937
9938 /* An assignment may not appear in a
9939 constant-expression. */
9940 if (cp_parser_non_integral_constant_expression (parser,
9941 NIC_ASSIGNMENT))
9942 return error_mark_node;
9943 /* Build the assignment expression. Its default
9944 location:
9945 LHS = RHS
9946 ~~~~^~~~~
9947 is the location of the '=' token as the
9948 caret, ranging from the start of the lhs to the
9949 end of the rhs. */
9950 loc = make_location (loc,
9951 expr.get_start (),
9952 rhs.get_finish ());
9953 expr = build_x_modify_expr (loc, expr,
9954 assignment_operator,
9955 rhs,
9956 complain_flags (decltype_p));
9957 /* TODO: build_x_modify_expr doesn't honor the location,
9958 so we must set it here. */
9959 expr.set_location (loc);
9960 }
9961 }
9962 }
9963
9964 return expr;
9965 }
9966
9967 /* Parse an (optional) assignment-operator.
9968
9969 assignment-operator: one of
9970 = *= /= %= += -= >>= <<= &= ^= |=
9971
9972 GNU Extension:
9973
9974 assignment-operator: one of
9975 <?= >?=
9976
9977 If the next token is an assignment operator, the corresponding tree
9978 code is returned, and the token is consumed. For example, for
9979 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9980 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9981 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9982 operator, ERROR_MARK is returned. */
9983
9984 static enum tree_code
9985 cp_parser_assignment_operator_opt (cp_parser* parser)
9986 {
9987 enum tree_code op;
9988 cp_token *token;
9989
9990 /* Peek at the next token. */
9991 token = cp_lexer_peek_token (parser->lexer);
9992
9993 switch (token->type)
9994 {
9995 case CPP_EQ:
9996 op = NOP_EXPR;
9997 break;
9998
9999 case CPP_MULT_EQ:
10000 op = MULT_EXPR;
10001 break;
10002
10003 case CPP_DIV_EQ:
10004 op = TRUNC_DIV_EXPR;
10005 break;
10006
10007 case CPP_MOD_EQ:
10008 op = TRUNC_MOD_EXPR;
10009 break;
10010
10011 case CPP_PLUS_EQ:
10012 op = PLUS_EXPR;
10013 break;
10014
10015 case CPP_MINUS_EQ:
10016 op = MINUS_EXPR;
10017 break;
10018
10019 case CPP_RSHIFT_EQ:
10020 op = RSHIFT_EXPR;
10021 break;
10022
10023 case CPP_LSHIFT_EQ:
10024 op = LSHIFT_EXPR;
10025 break;
10026
10027 case CPP_AND_EQ:
10028 op = BIT_AND_EXPR;
10029 break;
10030
10031 case CPP_XOR_EQ:
10032 op = BIT_XOR_EXPR;
10033 break;
10034
10035 case CPP_OR_EQ:
10036 op = BIT_IOR_EXPR;
10037 break;
10038
10039 default:
10040 /* Nothing else is an assignment operator. */
10041 op = ERROR_MARK;
10042 }
10043
10044 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10045 if (op != ERROR_MARK
10046 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10047 op = ERROR_MARK;
10048
10049 /* If it was an assignment operator, consume it. */
10050 if (op != ERROR_MARK)
10051 cp_lexer_consume_token (parser->lexer);
10052
10053 return op;
10054 }
10055
10056 /* Parse an expression.
10057
10058 expression:
10059 assignment-expression
10060 expression , assignment-expression
10061
10062 CAST_P is true if this expression is the target of a cast.
10063 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10064 except possibly parenthesized or on the RHS of a comma (N3276).
10065 WARN_COMMA_P is true if a comma should be diagnosed.
10066
10067 Returns a representation of the expression. */
10068
10069 static cp_expr
10070 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
10071 bool cast_p, bool decltype_p, bool warn_comma_p)
10072 {
10073 cp_expr expression = NULL_TREE;
10074 location_t loc = UNKNOWN_LOCATION;
10075
10076 while (true)
10077 {
10078 cp_expr assignment_expression;
10079
10080 /* Parse the next assignment-expression. */
10081 assignment_expression
10082 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
10083
10084 /* We don't create a temporary for a call that is the immediate operand
10085 of decltype or on the RHS of a comma. But when we see a comma, we
10086 need to create a temporary for a call on the LHS. */
10087 if (decltype_p && !processing_template_decl
10088 && TREE_CODE (assignment_expression) == CALL_EXPR
10089 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
10090 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10091 assignment_expression
10092 = build_cplus_new (TREE_TYPE (assignment_expression),
10093 assignment_expression, tf_warning_or_error);
10094
10095 /* If this is the first assignment-expression, we can just
10096 save it away. */
10097 if (!expression)
10098 expression = assignment_expression;
10099 else
10100 {
10101 /* Create a location with caret at the comma, ranging
10102 from the start of the LHS to the end of the RHS. */
10103 loc = make_location (loc,
10104 expression.get_start (),
10105 assignment_expression.get_finish ());
10106 expression = build_x_compound_expr (loc, expression,
10107 assignment_expression,
10108 complain_flags (decltype_p));
10109 expression.set_location (loc);
10110 }
10111 /* If the next token is not a comma, or we're in a fold-expression, then
10112 we are done with the expression. */
10113 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
10114 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10115 break;
10116 /* Consume the `,'. */
10117 loc = cp_lexer_peek_token (parser->lexer)->location;
10118 if (warn_comma_p)
10119 {
10120 /* [depr.comma.subscript]: A comma expression appearing as
10121 the expr-or-braced-init-list of a subscripting expression
10122 is deprecated. A parenthesized comma expression is not
10123 deprecated. */
10124 warning_at (loc, OPT_Wcomma_subscript,
10125 "top-level comma expression in array subscript "
10126 "is deprecated");
10127 warn_comma_p = false;
10128 }
10129 cp_lexer_consume_token (parser->lexer);
10130 /* A comma operator cannot appear in a constant-expression. */
10131 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
10132 expression = error_mark_node;
10133 }
10134
10135 return expression;
10136 }
10137
10138 /* Parse a constant-expression.
10139
10140 constant-expression:
10141 conditional-expression
10142
10143 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10144 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10145 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10146 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
10147 only parse a conditional-expression, otherwise parse an
10148 assignment-expression. See below for rationale. */
10149
10150 static cp_expr
10151 cp_parser_constant_expression (cp_parser* parser,
10152 bool allow_non_constant_p,
10153 bool *non_constant_p,
10154 bool strict_p)
10155 {
10156 bool saved_integral_constant_expression_p;
10157 bool saved_allow_non_integral_constant_expression_p;
10158 bool saved_non_integral_constant_expression_p;
10159 cp_expr expression;
10160
10161 /* It might seem that we could simply parse the
10162 conditional-expression, and then check to see if it were
10163 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10164 one that the compiler can figure out is constant, possibly after
10165 doing some simplifications or optimizations. The standard has a
10166 precise definition of constant-expression, and we must honor
10167 that, even though it is somewhat more restrictive.
10168
10169 For example:
10170
10171 int i[(2, 3)];
10172
10173 is not a legal declaration, because `(2, 3)' is not a
10174 constant-expression. The `,' operator is forbidden in a
10175 constant-expression. However, GCC's constant-folding machinery
10176 will fold this operation to an INTEGER_CST for `3'. */
10177
10178 /* Save the old settings. */
10179 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10180 saved_allow_non_integral_constant_expression_p
10181 = parser->allow_non_integral_constant_expression_p;
10182 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10183 /* We are now parsing a constant-expression. */
10184 parser->integral_constant_expression_p = true;
10185 parser->allow_non_integral_constant_expression_p
10186 = (allow_non_constant_p || cxx_dialect >= cxx11);
10187 parser->non_integral_constant_expression_p = false;
10188 /* Although the grammar says "conditional-expression", when not STRICT_P,
10189 we parse an "assignment-expression", which also permits
10190 "throw-expression" and the use of assignment operators. In the case
10191 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10192 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10193 actually essential that we look for an assignment-expression.
10194 For example, cp_parser_initializer_clauses uses this function to
10195 determine whether a particular assignment-expression is in fact
10196 constant. */
10197 if (strict_p)
10198 {
10199 /* Parse the binary expressions (logical-or-expression). */
10200 expression = cp_parser_binary_expression (parser, false, false, false,
10201 PREC_NOT_OPERATOR, NULL);
10202 /* If the next token is a `?' then we're actually looking at
10203 a conditional-expression; otherwise we're done. */
10204 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10205 expression = cp_parser_question_colon_clause (parser, expression);
10206 }
10207 else
10208 expression = cp_parser_assignment_expression (parser);
10209 /* Restore the old settings. */
10210 parser->integral_constant_expression_p
10211 = saved_integral_constant_expression_p;
10212 parser->allow_non_integral_constant_expression_p
10213 = saved_allow_non_integral_constant_expression_p;
10214 if (cxx_dialect >= cxx11)
10215 {
10216 /* Require an rvalue constant expression here; that's what our
10217 callers expect. Reference constant expressions are handled
10218 separately in e.g. cp_parser_template_argument. */
10219 tree decay = expression;
10220 if (TREE_TYPE (expression)
10221 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10222 decay = build_address (expression);
10223 bool is_const = is_rvalue_constant_expression (decay);
10224 parser->non_integral_constant_expression_p = !is_const;
10225 if (!is_const && !allow_non_constant_p)
10226 require_rvalue_constant_expression (decay);
10227 }
10228 if (allow_non_constant_p)
10229 *non_constant_p = parser->non_integral_constant_expression_p;
10230 parser->non_integral_constant_expression_p
10231 = saved_non_integral_constant_expression_p;
10232
10233 return expression;
10234 }
10235
10236 /* Parse __builtin_offsetof.
10237
10238 offsetof-expression:
10239 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10240
10241 offsetof-member-designator:
10242 id-expression
10243 | offsetof-member-designator "." id-expression
10244 | offsetof-member-designator "[" expression "]"
10245 | offsetof-member-designator "->" id-expression */
10246
10247 static cp_expr
10248 cp_parser_builtin_offsetof (cp_parser *parser)
10249 {
10250 int save_ice_p, save_non_ice_p;
10251 tree type;
10252 cp_expr expr;
10253 cp_id_kind dummy;
10254 cp_token *token;
10255 location_t finish_loc;
10256
10257 /* We're about to accept non-integral-constant things, but will
10258 definitely yield an integral constant expression. Save and
10259 restore these values around our local parsing. */
10260 save_ice_p = parser->integral_constant_expression_p;
10261 save_non_ice_p = parser->non_integral_constant_expression_p;
10262
10263 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10264
10265 /* Consume the "__builtin_offsetof" token. */
10266 cp_lexer_consume_token (parser->lexer);
10267 /* Consume the opening `('. */
10268 matching_parens parens;
10269 parens.require_open (parser);
10270 /* Parse the type-id. */
10271 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10272 {
10273 const char *saved_message = parser->type_definition_forbidden_message;
10274 parser->type_definition_forbidden_message
10275 = G_("types may not be defined within %<__builtin_offsetof%>");
10276 type = cp_parser_type_id (parser);
10277 parser->type_definition_forbidden_message = saved_message;
10278 }
10279 /* Look for the `,'. */
10280 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10281 token = cp_lexer_peek_token (parser->lexer);
10282
10283 /* Build the (type *)null that begins the traditional offsetof macro. */
10284 tree object_ptr
10285 = build_static_cast (input_location, build_pointer_type (type),
10286 null_pointer_node, tf_warning_or_error);
10287
10288 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10289 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10290 true, &dummy, token->location);
10291 while (true)
10292 {
10293 token = cp_lexer_peek_token (parser->lexer);
10294 switch (token->type)
10295 {
10296 case CPP_OPEN_SQUARE:
10297 /* offsetof-member-designator "[" expression "]" */
10298 expr = cp_parser_postfix_open_square_expression (parser, expr,
10299 true, false);
10300 break;
10301
10302 case CPP_DEREF:
10303 /* offsetof-member-designator "->" identifier */
10304 expr = grok_array_decl (token->location, expr,
10305 integer_zero_node, false);
10306 /* FALLTHRU */
10307
10308 case CPP_DOT:
10309 /* offsetof-member-designator "." identifier */
10310 cp_lexer_consume_token (parser->lexer);
10311 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10312 expr, true, &dummy,
10313 token->location);
10314 break;
10315
10316 case CPP_CLOSE_PAREN:
10317 /* Consume the ")" token. */
10318 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10319 cp_lexer_consume_token (parser->lexer);
10320 goto success;
10321
10322 default:
10323 /* Error. We know the following require will fail, but
10324 that gives the proper error message. */
10325 parens.require_close (parser);
10326 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10327 expr = error_mark_node;
10328 goto failure;
10329 }
10330 }
10331
10332 success:
10333 /* Make a location of the form:
10334 __builtin_offsetof (struct s, f)
10335 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10336 with caret at the type-id, ranging from the start of the
10337 "_builtin_offsetof" token to the close paren. */
10338 loc = make_location (loc, start_loc, finish_loc);
10339 /* The result will be an INTEGER_CST, so we need to explicitly
10340 preserve the location. */
10341 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10342
10343 failure:
10344 parser->integral_constant_expression_p = save_ice_p;
10345 parser->non_integral_constant_expression_p = save_non_ice_p;
10346
10347 expr = expr.maybe_add_location_wrapper ();
10348 return expr;
10349 }
10350
10351 /* Parse a trait expression.
10352
10353 Returns a representation of the expression, the underlying type
10354 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10355
10356 static cp_expr
10357 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10358 {
10359 cp_trait_kind kind;
10360 tree type1, type2 = NULL_TREE;
10361 bool binary = false;
10362 bool variadic = false;
10363
10364 switch (keyword)
10365 {
10366 case RID_HAS_NOTHROW_ASSIGN:
10367 kind = CPTK_HAS_NOTHROW_ASSIGN;
10368 break;
10369 case RID_HAS_NOTHROW_CONSTRUCTOR:
10370 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10371 break;
10372 case RID_HAS_NOTHROW_COPY:
10373 kind = CPTK_HAS_NOTHROW_COPY;
10374 break;
10375 case RID_HAS_TRIVIAL_ASSIGN:
10376 kind = CPTK_HAS_TRIVIAL_ASSIGN;
10377 break;
10378 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10379 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10380 break;
10381 case RID_HAS_TRIVIAL_COPY:
10382 kind = CPTK_HAS_TRIVIAL_COPY;
10383 break;
10384 case RID_HAS_TRIVIAL_DESTRUCTOR:
10385 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10386 break;
10387 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10388 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10389 break;
10390 case RID_HAS_VIRTUAL_DESTRUCTOR:
10391 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10392 break;
10393 case RID_IS_ABSTRACT:
10394 kind = CPTK_IS_ABSTRACT;
10395 break;
10396 case RID_IS_AGGREGATE:
10397 kind = CPTK_IS_AGGREGATE;
10398 break;
10399 case RID_IS_BASE_OF:
10400 kind = CPTK_IS_BASE_OF;
10401 binary = true;
10402 break;
10403 case RID_IS_CLASS:
10404 kind = CPTK_IS_CLASS;
10405 break;
10406 case RID_IS_EMPTY:
10407 kind = CPTK_IS_EMPTY;
10408 break;
10409 case RID_IS_ENUM:
10410 kind = CPTK_IS_ENUM;
10411 break;
10412 case RID_IS_FINAL:
10413 kind = CPTK_IS_FINAL;
10414 break;
10415 case RID_IS_LITERAL_TYPE:
10416 kind = CPTK_IS_LITERAL_TYPE;
10417 break;
10418 case RID_IS_POD:
10419 kind = CPTK_IS_POD;
10420 break;
10421 case RID_IS_POLYMORPHIC:
10422 kind = CPTK_IS_POLYMORPHIC;
10423 break;
10424 case RID_IS_SAME_AS:
10425 kind = CPTK_IS_SAME_AS;
10426 binary = true;
10427 break;
10428 case RID_IS_STD_LAYOUT:
10429 kind = CPTK_IS_STD_LAYOUT;
10430 break;
10431 case RID_IS_TRIVIAL:
10432 kind = CPTK_IS_TRIVIAL;
10433 break;
10434 case RID_IS_TRIVIALLY_ASSIGNABLE:
10435 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10436 binary = true;
10437 break;
10438 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10439 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10440 variadic = true;
10441 break;
10442 case RID_IS_TRIVIALLY_COPYABLE:
10443 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10444 break;
10445 case RID_IS_UNION:
10446 kind = CPTK_IS_UNION;
10447 break;
10448 case RID_UNDERLYING_TYPE:
10449 kind = CPTK_UNDERLYING_TYPE;
10450 break;
10451 case RID_BASES:
10452 kind = CPTK_BASES;
10453 break;
10454 case RID_DIRECT_BASES:
10455 kind = CPTK_DIRECT_BASES;
10456 break;
10457 case RID_IS_ASSIGNABLE:
10458 kind = CPTK_IS_ASSIGNABLE;
10459 binary = true;
10460 break;
10461 case RID_IS_CONSTRUCTIBLE:
10462 kind = CPTK_IS_CONSTRUCTIBLE;
10463 variadic = true;
10464 break;
10465 default:
10466 gcc_unreachable ();
10467 }
10468
10469 /* Get location of initial token. */
10470 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10471
10472 /* Consume the token. */
10473 cp_lexer_consume_token (parser->lexer);
10474
10475 matching_parens parens;
10476 parens.require_open (parser);
10477
10478 {
10479 type_id_in_expr_sentinel s (parser);
10480 type1 = cp_parser_type_id (parser);
10481 }
10482
10483 if (type1 == error_mark_node)
10484 return error_mark_node;
10485
10486 if (binary)
10487 {
10488 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10489
10490 {
10491 type_id_in_expr_sentinel s (parser);
10492 type2 = cp_parser_type_id (parser);
10493 }
10494
10495 if (type2 == error_mark_node)
10496 return error_mark_node;
10497 }
10498 else if (variadic)
10499 {
10500 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10501 {
10502 cp_lexer_consume_token (parser->lexer);
10503 tree elt = cp_parser_type_id (parser);
10504 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10505 {
10506 cp_lexer_consume_token (parser->lexer);
10507 elt = make_pack_expansion (elt);
10508 }
10509 if (elt == error_mark_node)
10510 return error_mark_node;
10511 type2 = tree_cons (NULL_TREE, elt, type2);
10512 }
10513 }
10514
10515 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10516 parens.require_close (parser);
10517
10518 /* Construct a location of the form:
10519 __is_trivially_copyable(_Tp)
10520 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10521 with start == caret, finishing at the close-paren. */
10522 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10523
10524 /* Complete the trait expression, which may mean either processing
10525 the trait expr now or saving it for template instantiation. */
10526 switch (kind)
10527 {
10528 case CPTK_UNDERLYING_TYPE:
10529 return cp_expr (finish_underlying_type (type1), trait_loc);
10530 case CPTK_BASES:
10531 return cp_expr (finish_bases (type1, false), trait_loc);
10532 case CPTK_DIRECT_BASES:
10533 return cp_expr (finish_bases (type1, true), trait_loc);
10534 default:
10535 return finish_trait_expr (trait_loc, kind, type1, type2);
10536 }
10537 }
10538
10539 /* Parse a lambda expression.
10540
10541 lambda-expression:
10542 lambda-introducer lambda-declarator [opt] compound-statement
10543
10544 Returns a representation of the expression. */
10545
10546 static cp_expr
10547 cp_parser_lambda_expression (cp_parser* parser)
10548 {
10549 tree lambda_expr = build_lambda_expr ();
10550 tree type;
10551 bool ok = true;
10552 cp_token *token = cp_lexer_peek_token (parser->lexer);
10553 cp_token_position start = 0;
10554
10555 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10556
10557 if (cxx_dialect >= cxx20)
10558 /* C++20 allows lambdas in unevaluated context. */;
10559 else if (cp_unevaluated_operand)
10560 {
10561 if (!token->error_reported)
10562 {
10563 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10564 "lambda-expression in unevaluated context"
10565 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
10566 token->error_reported = true;
10567 }
10568 ok = false;
10569 }
10570 else if (parser->in_template_argument_list_p || processing_template_parmlist)
10571 {
10572 if (!token->error_reported)
10573 {
10574 error_at (token->location, "lambda-expression in template-argument"
10575 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
10576 token->error_reported = true;
10577 }
10578 ok = false;
10579 }
10580
10581 /* We may be in the middle of deferred access check. Disable
10582 it now. */
10583 push_deferring_access_checks (dk_no_deferred);
10584
10585 cp_parser_lambda_introducer (parser, lambda_expr);
10586 if (cp_parser_error_occurred (parser))
10587 return error_mark_node;
10588
10589 type = begin_lambda_type (lambda_expr);
10590 if (type == error_mark_node)
10591 return error_mark_node;
10592
10593 record_lambda_scope (lambda_expr);
10594
10595 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10596 determine_visibility (TYPE_NAME (type));
10597
10598 /* Now that we've started the type, add the capture fields for any
10599 explicit captures. */
10600 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10601
10602 {
10603 /* Inside the class, surrounding template-parameter-lists do not apply. */
10604 unsigned int saved_num_template_parameter_lists
10605 = parser->num_template_parameter_lists;
10606 unsigned char in_statement = parser->in_statement;
10607 bool in_switch_statement_p = parser->in_switch_statement_p;
10608 bool fully_implicit_function_template_p
10609 = parser->fully_implicit_function_template_p;
10610 tree implicit_template_parms = parser->implicit_template_parms;
10611 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10612 bool auto_is_implicit_function_template_parm_p
10613 = parser->auto_is_implicit_function_template_parm_p;
10614
10615 parser->num_template_parameter_lists = 0;
10616 parser->in_statement = 0;
10617 parser->in_switch_statement_p = false;
10618 parser->fully_implicit_function_template_p = false;
10619 parser->implicit_template_parms = 0;
10620 parser->implicit_template_scope = 0;
10621 parser->auto_is_implicit_function_template_parm_p = false;
10622
10623 /* The body of a lambda in a discarded statement is not discarded. */
10624 bool discarded = in_discarded_stmt;
10625 in_discarded_stmt = 0;
10626
10627 /* By virtue of defining a local class, a lambda expression has access to
10628 the private variables of enclosing classes. */
10629
10630 if (cp_parser_start_tentative_firewall (parser))
10631 start = token;
10632
10633 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10634
10635 if (ok && cp_parser_error_occurred (parser))
10636 ok = false;
10637
10638 if (ok)
10639 {
10640 cp_parser_lambda_body (parser, lambda_expr);
10641 }
10642 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10643 {
10644 if (cp_parser_skip_to_closing_brace (parser))
10645 cp_lexer_consume_token (parser->lexer);
10646 }
10647
10648 /* The capture list was built up in reverse order; fix that now. */
10649 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10650 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10651
10652 if (ok)
10653 maybe_add_lambda_conv_op (type);
10654
10655 finish_struct (type, /*attributes=*/NULL_TREE);
10656
10657 in_discarded_stmt = discarded;
10658
10659 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10660 parser->in_statement = in_statement;
10661 parser->in_switch_statement_p = in_switch_statement_p;
10662 parser->fully_implicit_function_template_p
10663 = fully_implicit_function_template_p;
10664 parser->implicit_template_parms = implicit_template_parms;
10665 parser->implicit_template_scope = implicit_template_scope;
10666 parser->auto_is_implicit_function_template_parm_p
10667 = auto_is_implicit_function_template_parm_p;
10668 }
10669
10670 /* This field is only used during parsing of the lambda. */
10671 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10672
10673 /* This lambda shouldn't have any proxies left at this point. */
10674 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10675 /* And now that we're done, push proxies for an enclosing lambda. */
10676 insert_pending_capture_proxies ();
10677
10678 /* Update the lambda expression to a range. */
10679 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10680 token->location,
10681 parser->lexer);
10682
10683 if (ok)
10684 lambda_expr = build_lambda_object (lambda_expr);
10685 else
10686 lambda_expr = error_mark_node;
10687
10688 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10689
10690 pop_deferring_access_checks ();
10691
10692 return lambda_expr;
10693 }
10694
10695 /* Parse the beginning of a lambda expression.
10696
10697 lambda-introducer:
10698 [ lambda-capture [opt] ]
10699
10700 LAMBDA_EXPR is the current representation of the lambda expression. */
10701
10702 static void
10703 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10704 {
10705 /* Need commas after the first capture. */
10706 bool first = true;
10707
10708 /* Eat the leading `['. */
10709 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10710
10711 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10712 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10713 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS)
10714 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
10715 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10716 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10717 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10718 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10719
10720 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10721 {
10722 cp_lexer_consume_token (parser->lexer);
10723 first = false;
10724
10725 if (!(at_function_scope_p () || parsing_nsdmi ()))
10726 error ("non-local lambda expression cannot have a capture-default");
10727 }
10728
10729 hash_set<tree, true> ids;
10730 tree first_capture_id = NULL_TREE;
10731 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10732 {
10733 cp_token* capture_token;
10734 tree capture_id;
10735 tree capture_init_expr;
10736 cp_id_kind idk = CP_ID_KIND_NONE;
10737 bool explicit_init_p = false;
10738
10739 enum capture_kind_type
10740 {
10741 BY_COPY,
10742 BY_REFERENCE
10743 };
10744 enum capture_kind_type capture_kind = BY_COPY;
10745
10746 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10747 {
10748 error ("expected end of capture-list");
10749 return;
10750 }
10751
10752 if (first)
10753 first = false;
10754 else
10755 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10756
10757 /* Possibly capture `this'. */
10758 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10759 {
10760 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10761 if (cxx_dialect < cxx20
10762 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10763 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10764 "with by-copy capture default");
10765 cp_lexer_consume_token (parser->lexer);
10766 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10767 pedwarn (input_location, 0,
10768 "already captured %qD in lambda expression",
10769 this_identifier);
10770 else
10771 add_capture (lambda_expr, /*id=*/this_identifier,
10772 /*initializer=*/finish_this_expr (),
10773 /*by_reference_p=*/true, explicit_init_p);
10774 continue;
10775 }
10776
10777 /* Possibly capture `*this'. */
10778 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10779 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10780 {
10781 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10782 if (cxx_dialect < cxx17)
10783 pedwarn (loc, 0, "%<*this%> capture only available with "
10784 "%<-std=c++17%> or %<-std=gnu++17%>");
10785 cp_lexer_consume_token (parser->lexer);
10786 cp_lexer_consume_token (parser->lexer);
10787 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10788 pedwarn (input_location, 0,
10789 "already captured %qD in lambda expression",
10790 this_identifier);
10791 else
10792 add_capture (lambda_expr, /*id=*/this_identifier,
10793 /*initializer=*/finish_this_expr (),
10794 /*by_reference_p=*/false, explicit_init_p);
10795 continue;
10796 }
10797
10798 /* But reject `&this'. */
10799 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10800 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10801 {
10802 error_at (cp_lexer_peek_token (parser->lexer)->location,
10803 "%<this%> cannot be captured by reference");
10804 cp_lexer_consume_token (parser->lexer);
10805 cp_lexer_consume_token (parser->lexer);
10806 continue;
10807 }
10808
10809 /* Remember whether we want to capture as a reference or not. */
10810 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10811 {
10812 capture_kind = BY_REFERENCE;
10813 cp_lexer_consume_token (parser->lexer);
10814 }
10815
10816 bool init_pack_expansion = false;
10817 location_t ellipsis_loc = UNKNOWN_LOCATION;
10818 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10819 {
10820 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
10821 if (cxx_dialect < cxx20)
10822 pedwarn (ellipsis_loc, 0, "pack init-capture only available with "
10823 "%<-std=c++20%> or %<-std=gnu++20%>");
10824 cp_lexer_consume_token (parser->lexer);
10825 init_pack_expansion = true;
10826 }
10827
10828 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
10829 if (init_pack_expansion && capture_kind != BY_REFERENCE
10830 && cp_lexer_next_token_is (parser->lexer, CPP_AND))
10831 {
10832 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
10833 0, "%<&%> should come before %<...%>");
10834 capture_kind = BY_REFERENCE;
10835 cp_lexer_consume_token (parser->lexer);
10836 }
10837
10838 /* Get the identifier. */
10839 capture_token = cp_lexer_peek_token (parser->lexer);
10840 capture_id = cp_parser_identifier (parser);
10841
10842 if (capture_id == error_mark_node)
10843 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10844 delimiters, but I modified this to stop on unnested ']' as well. It
10845 was already changed to stop on unnested '}', so the
10846 "closing_parenthesis" name is no more misleading with my change. */
10847 {
10848 cp_parser_skip_to_closing_parenthesis (parser,
10849 /*recovering=*/true,
10850 /*or_comma=*/true,
10851 /*consume_paren=*/true);
10852 break;
10853 }
10854
10855 /* Find the initializer for this capture. */
10856 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10857 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10858 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10859 {
10860 bool direct, non_constant;
10861 /* An explicit initializer exists. */
10862 if (cxx_dialect < cxx14)
10863 pedwarn (input_location, 0,
10864 "lambda capture initializers "
10865 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
10866 capture_init_expr = cp_parser_initializer (parser, &direct,
10867 &non_constant, true);
10868 explicit_init_p = true;
10869 if (capture_init_expr == NULL_TREE)
10870 {
10871 error ("empty initializer for lambda init-capture");
10872 capture_init_expr = error_mark_node;
10873 }
10874 if (init_pack_expansion)
10875 capture_init_expr = make_pack_expansion (capture_init_expr);
10876 }
10877 else
10878 {
10879 const char* error_msg;
10880
10881 /* Turn the identifier into an id-expression. */
10882 capture_init_expr
10883 = cp_parser_lookup_name_simple (parser, capture_id,
10884 capture_token->location);
10885
10886 if (capture_init_expr == error_mark_node)
10887 {
10888 unqualified_name_lookup_error (capture_id);
10889 continue;
10890 }
10891 else if (!VAR_P (capture_init_expr)
10892 && TREE_CODE (capture_init_expr) != PARM_DECL)
10893 {
10894 error_at (capture_token->location,
10895 "capture of non-variable %qE",
10896 capture_init_expr);
10897 if (DECL_P (capture_init_expr))
10898 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10899 "%q#D declared here", capture_init_expr);
10900 continue;
10901 }
10902 if (VAR_P (capture_init_expr)
10903 && decl_storage_duration (capture_init_expr) != dk_auto)
10904 {
10905 if (pedwarn (capture_token->location, 0, "capture of variable "
10906 "%qD with non-automatic storage duration",
10907 capture_init_expr))
10908 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10909 "%q#D declared here", capture_init_expr);
10910 continue;
10911 }
10912
10913 capture_init_expr
10914 = finish_id_expression
10915 (capture_id,
10916 capture_init_expr,
10917 parser->scope,
10918 &idk,
10919 /*integral_constant_expression_p=*/false,
10920 /*allow_non_integral_constant_expression_p=*/false,
10921 /*non_integral_constant_expression_p=*/NULL,
10922 /*template_p=*/false,
10923 /*done=*/true,
10924 /*address_p=*/false,
10925 /*template_arg_p=*/false,
10926 &error_msg,
10927 capture_token->location);
10928
10929 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10930 {
10931 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10932 cp_lexer_consume_token (parser->lexer);
10933 capture_init_expr = make_pack_expansion (capture_init_expr);
10934 if (init_pack_expansion)
10935 {
10936 /* If what follows is an initializer, the second '...' is
10937 invalid. But for cases like [...xs...], the first one
10938 is invalid. */
10939 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10940 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10941 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10942 ellipsis_loc = loc;
10943 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
10944 continue;
10945 }
10946 }
10947 }
10948
10949 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10950 && !explicit_init_p)
10951 {
10952 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10953 && capture_kind == BY_COPY)
10954 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10955 "of %qD redundant with by-copy capture default",
10956 capture_id);
10957 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10958 && capture_kind == BY_REFERENCE)
10959 pedwarn (capture_token->location, 0, "explicit by-reference "
10960 "capture of %qD redundant with by-reference capture "
10961 "default", capture_id);
10962 }
10963
10964 /* Check for duplicates.
10965 Optimize for the zero or one explicit captures cases and only create
10966 the hash_set after adding second capture. */
10967 bool found = false;
10968 if (!ids.is_empty ())
10969 found = ids.add (capture_id);
10970 else if (first_capture_id == NULL_TREE)
10971 first_capture_id = capture_id;
10972 else if (capture_id == first_capture_id)
10973 found = true;
10974 else
10975 {
10976 ids.add (first_capture_id);
10977 ids.add (capture_id);
10978 }
10979 if (found)
10980 pedwarn (input_location, 0,
10981 "already captured %qD in lambda expression", capture_id);
10982 else
10983 add_capture (lambda_expr, capture_id, capture_init_expr,
10984 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10985 explicit_init_p);
10986
10987 /* If there is any qualification still in effect, clear it
10988 now; we will be starting fresh with the next capture. */
10989 parser->scope = NULL_TREE;
10990 parser->qualifying_scope = NULL_TREE;
10991 parser->object_scope = NULL_TREE;
10992 }
10993
10994 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10995 }
10996
10997 /* Parse the (optional) middle of a lambda expression.
10998
10999 lambda-declarator:
11000 < template-parameter-list [opt] >
11001 requires-clause [opt]
11002 ( parameter-declaration-clause [opt] )
11003 attribute-specifier [opt]
11004 decl-specifier-seq [opt]
11005 exception-specification [opt]
11006 lambda-return-type-clause [opt]
11007 requires-clause [opt]
11008
11009 LAMBDA_EXPR is the current representation of the lambda expression. */
11010
11011 static bool
11012 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
11013 {
11014 /* 5.1.1.4 of the standard says:
11015 If a lambda-expression does not include a lambda-declarator, it is as if
11016 the lambda-declarator were ().
11017 This means an empty parameter list, no attributes, and no exception
11018 specification. */
11019 tree param_list = void_list_node;
11020 tree std_attrs = NULL_TREE;
11021 tree gnu_attrs = NULL_TREE;
11022 tree exception_spec = NULL_TREE;
11023 tree template_param_list = NULL_TREE;
11024 tree tx_qual = NULL_TREE;
11025 tree return_type = NULL_TREE;
11026 tree trailing_requires_clause = NULL_TREE;
11027 cp_decl_specifier_seq lambda_specs;
11028 clear_decl_specs (&lambda_specs);
11029 /* A lambda op() is const unless explicitly 'mutable'. */
11030 cp_cv_quals quals = TYPE_QUAL_CONST;
11031
11032 /* The template-parameter-list is optional, but must begin with
11033 an opening angle if present. */
11034 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
11035 {
11036 if (cxx_dialect < cxx14)
11037 pedwarn (parser->lexer->next_token->location, 0,
11038 "lambda templates are only available with "
11039 "%<-std=c++14%> or %<-std=gnu++14%>");
11040 else if (cxx_dialect < cxx20)
11041 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
11042 "lambda templates are only available with "
11043 "%<-std=c++20%> or %<-std=gnu++20%>");
11044
11045 cp_lexer_consume_token (parser->lexer);
11046
11047 template_param_list = cp_parser_template_parameter_list (parser);
11048 cp_parser_skip_to_end_of_template_parameter_list (parser);
11049
11050 /* We may have a constrained generic lambda; parse the requires-clause
11051 immediately after the template-parameter-list and combine with any
11052 shorthand constraints present. */
11053 tree dreqs = cp_parser_requires_clause_opt (parser, true);
11054 if (flag_concepts)
11055 {
11056 tree reqs = get_shorthand_constraints (current_template_parms);
11057 if (dreqs)
11058 reqs = combine_constraint_expressions (reqs, dreqs);
11059 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
11060 }
11061
11062 /* We just processed one more parameter list. */
11063 ++parser->num_template_parameter_lists;
11064 }
11065
11066 /* Committee discussion supports allowing attributes here. */
11067 lambda_specs.attributes = cp_parser_attributes_opt (parser);
11068
11069 /* The parameter-declaration-clause is optional (unless
11070 template-parameter-list was given), but must begin with an
11071 opening parenthesis if present. */
11072 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11073 {
11074 bool is_consteval = false;
11075 /* For C++20, before parsing the parameter list check if there is
11076 a consteval specifier in the corresponding decl-specifier-seq. */
11077 if (cxx_dialect >= cxx20)
11078 {
11079 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1);
11080 cp_lexer_nth_token_is (parser->lexer, n, CPP_KEYWORD); n++)
11081 {
11082 if (cp_lexer_peek_nth_token (parser->lexer, n)->keyword
11083 == RID_CONSTEVAL)
11084 {
11085 is_consteval = true;
11086 break;
11087 }
11088 }
11089 }
11090
11091 matching_parens parens;
11092 parens.consume_open (parser);
11093
11094 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
11095
11096 if (is_consteval)
11097 current_binding_level->immediate_fn_ctx_p = true;
11098
11099 /* Parse parameters. */
11100 param_list
11101 = cp_parser_parameter_declaration_clause
11102 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
11103
11104 /* Default arguments shall not be specified in the
11105 parameter-declaration-clause of a lambda-declarator. */
11106 if (cxx_dialect < cxx14)
11107 for (tree t = param_list; t; t = TREE_CHAIN (t))
11108 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
11109 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
11110 "default argument specified for lambda parameter");
11111
11112 parens.require_close (parser);
11113
11114 /* In the decl-specifier-seq of the lambda-declarator, each
11115 decl-specifier shall either be mutable or constexpr. */
11116 int declares_class_or_enum;
11117 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
11118 && !cp_next_tokens_can_be_gnu_attribute_p (parser))
11119 cp_parser_decl_specifier_seq (parser,
11120 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
11121 &lambda_specs, &declares_class_or_enum);
11122 if (lambda_specs.storage_class == sc_mutable)
11123 {
11124 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
11125 quals = TYPE_UNQUALIFIED;
11126 if (lambda_specs.conflicting_specifiers_p)
11127 error_at (lambda_specs.locations[ds_storage_class],
11128 "duplicate %<mutable%>");
11129 }
11130
11131 tx_qual = cp_parser_tx_qualifier_opt (parser);
11132
11133 /* Parse optional exception specification. */
11134 exception_spec
11135 = cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE);
11136
11137 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11138
11139 /* Parse optional trailing return type. */
11140 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
11141 {
11142 cp_lexer_consume_token (parser->lexer);
11143 return_type = cp_parser_trailing_type_id (parser);
11144 }
11145
11146 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11147 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
11148
11149 /* Parse optional trailing requires clause. */
11150 trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
11151
11152 /* The function parameters must be in scope all the way until after the
11153 trailing-return-type in case of decltype. */
11154 pop_bindings_and_leave_scope ();
11155 }
11156 else if (template_param_list != NULL_TREE) // generate diagnostic
11157 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11158
11159 /* Create the function call operator.
11160
11161 Messing with declarators like this is no uglier than building up the
11162 FUNCTION_DECL by hand, and this is less likely to get out of sync with
11163 other code. */
11164 {
11165 cp_decl_specifier_seq return_type_specs;
11166 cp_declarator* declarator;
11167 tree fco;
11168 void *p;
11169
11170 clear_decl_specs (&return_type_specs);
11171 return_type_specs.type = make_auto ();
11172
11173 if (lambda_specs.locations[ds_constexpr])
11174 {
11175 if (cxx_dialect >= cxx17)
11176 return_type_specs.locations[ds_constexpr]
11177 = lambda_specs.locations[ds_constexpr];
11178 else
11179 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
11180 "lambda only available with %<-std=c++17%> or "
11181 "%<-std=gnu++17%>");
11182 }
11183 if (lambda_specs.locations[ds_consteval])
11184 return_type_specs.locations[ds_consteval]
11185 = lambda_specs.locations[ds_consteval];
11186
11187 p = obstack_alloc (&declarator_obstack, 0);
11188
11189 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
11190 LAMBDA_EXPR_LOCATION (lambda_expr));
11191
11192 declarator = make_call_declarator (declarator, param_list, quals,
11193 VIRT_SPEC_UNSPECIFIED,
11194 REF_QUAL_NONE,
11195 tx_qual,
11196 exception_spec,
11197 return_type,
11198 trailing_requires_clause);
11199 declarator->std_attributes = std_attrs;
11200
11201 fco = grokmethod (&return_type_specs,
11202 declarator,
11203 chainon (gnu_attrs, lambda_specs.attributes));
11204 if (fco != error_mark_node)
11205 {
11206 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
11207 DECL_ARTIFICIAL (fco) = 1;
11208 /* Give the object parameter a different name. */
11209 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
11210 DECL_SET_LAMBDA_FUNCTION (fco, true);
11211 }
11212 if (template_param_list)
11213 {
11214 fco = finish_member_template_decl (fco);
11215 finish_template_decl (template_param_list);
11216 --parser->num_template_parameter_lists;
11217 }
11218 else if (parser->fully_implicit_function_template_p)
11219 fco = finish_fully_implicit_template (parser, fco);
11220
11221 finish_member_declaration (fco);
11222
11223 obstack_free (&declarator_obstack, p);
11224
11225 return (fco != error_mark_node);
11226 }
11227 }
11228
11229 /* Parse the body of a lambda expression, which is simply
11230
11231 compound-statement
11232
11233 but which requires special handling.
11234 LAMBDA_EXPR is the current representation of the lambda expression. */
11235
11236 static void
11237 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
11238 {
11239 bool nested = (current_function_decl != NULL_TREE);
11240 unsigned char local_variables_forbidden_p
11241 = parser->local_variables_forbidden_p;
11242 bool in_function_body = parser->in_function_body;
11243
11244 /* The body of a lambda-expression is not a subexpression of the enclosing
11245 expression. */
11246 cp_evaluated ev;
11247
11248 if (nested)
11249 push_function_context ();
11250 else
11251 /* Still increment function_depth so that we don't GC in the
11252 middle of an expression. */
11253 ++function_depth;
11254
11255 vec<tree> omp_privatization_save;
11256 save_omp_privatization_clauses (omp_privatization_save);
11257 /* Clear this in case we're in the middle of a default argument. */
11258 parser->local_variables_forbidden_p = 0;
11259 parser->in_function_body = true;
11260
11261 {
11262 local_specialization_stack s (lss_copy);
11263 tree fco = lambda_function (lambda_expr);
11264 tree body = start_lambda_function (fco, lambda_expr);
11265
11266 /* Originally C++11 required us to peek for 'return expr'; and
11267 process it specially here to deduce the return type. N3638
11268 removed the need for that. */
11269 cp_parser_function_body (parser, false);
11270
11271 finish_lambda_function (body);
11272 }
11273
11274 restore_omp_privatization_clauses (omp_privatization_save);
11275 parser->local_variables_forbidden_p = local_variables_forbidden_p;
11276 parser->in_function_body = in_function_body;
11277 if (nested)
11278 pop_function_context();
11279 else
11280 --function_depth;
11281 }
11282
11283 /* Statements [gram.stmt.stmt] */
11284
11285 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11286
11287 static void
11288 add_debug_begin_stmt (location_t loc)
11289 {
11290 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11291 return;
11292 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11293 /* A concept is never expanded normally. */
11294 return;
11295
11296 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11297 SET_EXPR_LOCATION (stmt, loc);
11298 add_stmt (stmt);
11299 }
11300
11301 /* Parse a statement.
11302
11303 statement:
11304 labeled-statement
11305 expression-statement
11306 compound-statement
11307 selection-statement
11308 iteration-statement
11309 jump-statement
11310 declaration-statement
11311 try-block
11312
11313 C++11:
11314
11315 statement:
11316 labeled-statement
11317 attribute-specifier-seq (opt) expression-statement
11318 attribute-specifier-seq (opt) compound-statement
11319 attribute-specifier-seq (opt) selection-statement
11320 attribute-specifier-seq (opt) iteration-statement
11321 attribute-specifier-seq (opt) jump-statement
11322 declaration-statement
11323 attribute-specifier-seq (opt) try-block
11324
11325 init-statement:
11326 expression-statement
11327 simple-declaration
11328
11329 TM Extension:
11330
11331 statement:
11332 atomic-statement
11333
11334 IN_COMPOUND is true when the statement is nested inside a
11335 cp_parser_compound_statement; this matters for certain pragmas.
11336
11337 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11338 is a (possibly labeled) if statement which is not enclosed in braces
11339 and has an else clause. This is used to implement -Wparentheses.
11340
11341 CHAIN is a vector of if-else-if conditions. */
11342
11343 static void
11344 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
11345 bool in_compound, bool *if_p, vec<tree> *chain,
11346 location_t *loc_after_labels)
11347 {
11348 tree statement, std_attrs = NULL_TREE;
11349 cp_token *token;
11350 location_t statement_location, attrs_loc;
11351
11352 restart:
11353 if (if_p != NULL)
11354 *if_p = false;
11355 /* There is no statement yet. */
11356 statement = NULL_TREE;
11357
11358 saved_token_sentinel saved_tokens (parser->lexer);
11359 attrs_loc = cp_lexer_peek_token (parser->lexer)->location;
11360 if (c_dialect_objc ())
11361 /* In obj-c++, seeing '[[' might be the either the beginning of
11362 c++11 attributes, or a nested objc-message-expression. So
11363 let's parse the c++11 attributes tentatively. */
11364 cp_parser_parse_tentatively (parser);
11365 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11366 if (std_attrs)
11367 attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
11368 if (c_dialect_objc ())
11369 {
11370 if (!cp_parser_parse_definitely (parser))
11371 std_attrs = NULL_TREE;
11372 }
11373
11374 /* Peek at the next token. */
11375 token = cp_lexer_peek_token (parser->lexer);
11376 /* Remember the location of the first token in the statement. */
11377 cp_token *statement_token = token;
11378 statement_location = token->location;
11379 add_debug_begin_stmt (statement_location);
11380 /* If this is a keyword, then that will often determine what kind of
11381 statement we have. */
11382 if (token->type == CPP_KEYWORD)
11383 {
11384 enum rid keyword = token->keyword;
11385
11386 switch (keyword)
11387 {
11388 case RID_CASE:
11389 case RID_DEFAULT:
11390 /* Looks like a labeled-statement with a case label.
11391 Parse the label, and then use tail recursion to parse
11392 the statement. */
11393 cp_parser_label_for_labeled_statement (parser, std_attrs);
11394 in_compound = false;
11395 goto restart;
11396
11397 case RID_IF:
11398 case RID_SWITCH:
11399 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11400 statement = cp_parser_selection_statement (parser, if_p, chain);
11401 break;
11402
11403 case RID_WHILE:
11404 case RID_DO:
11405 case RID_FOR:
11406 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11407 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
11408 break;
11409
11410 case RID_BREAK:
11411 case RID_CONTINUE:
11412 case RID_RETURN:
11413 case RID_CO_RETURN:
11414 case RID_GOTO:
11415 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11416 statement = cp_parser_jump_statement (parser);
11417 break;
11418
11419 /* Objective-C++ exception-handling constructs. */
11420 case RID_AT_TRY:
11421 case RID_AT_CATCH:
11422 case RID_AT_FINALLY:
11423 case RID_AT_SYNCHRONIZED:
11424 case RID_AT_THROW:
11425 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11426 statement = cp_parser_objc_statement (parser);
11427 break;
11428
11429 case RID_TRY:
11430 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11431 statement = cp_parser_try_block (parser);
11432 break;
11433
11434 case RID_NAMESPACE:
11435 /* This must be a namespace alias definition. */
11436 if (std_attrs != NULL_TREE)
11437 {
11438 /* Attributes should be parsed as part of the
11439 declaration, so let's un-parse them. */
11440 saved_tokens.rollback();
11441 std_attrs = NULL_TREE;
11442 }
11443 cp_parser_declaration_statement (parser);
11444 return;
11445
11446 case RID_TRANSACTION_ATOMIC:
11447 case RID_TRANSACTION_RELAXED:
11448 case RID_SYNCHRONIZED:
11449 case RID_ATOMIC_NOEXCEPT:
11450 case RID_ATOMIC_CANCEL:
11451 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11452 statement = cp_parser_transaction (parser, token);
11453 break;
11454 case RID_TRANSACTION_CANCEL:
11455 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11456 statement = cp_parser_transaction_cancel (parser);
11457 break;
11458
11459 default:
11460 /* It might be a keyword like `int' that can start a
11461 declaration-statement. */
11462 break;
11463 }
11464 }
11465 else if (token->type == CPP_NAME)
11466 {
11467 /* If the next token is a `:', then we are looking at a
11468 labeled-statement. */
11469 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11470 if (token->type == CPP_COLON)
11471 {
11472 /* Looks like a labeled-statement with an ordinary label.
11473 Parse the label, and then use tail recursion to parse
11474 the statement. */
11475
11476 cp_parser_label_for_labeled_statement (parser, std_attrs);
11477 in_compound = false;
11478 goto restart;
11479 }
11480 }
11481 /* Anything that starts with a `{' must be a compound-statement. */
11482 else if (token->type == CPP_OPEN_BRACE)
11483 {
11484 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11485 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11486 }
11487 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11488 a statement all its own. */
11489 else if (token->type == CPP_PRAGMA)
11490 {
11491 /* Only certain OpenMP pragmas are attached to statements, and thus
11492 are considered statements themselves. All others are not. In
11493 the context of a compound, accept the pragma as a "statement" and
11494 return so that we can check for a close brace. Otherwise we
11495 require a real statement and must go back and read one. */
11496 if (in_compound)
11497 cp_parser_pragma (parser, pragma_compound, if_p);
11498 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11499 goto restart;
11500 return;
11501 }
11502 else if (token->type == CPP_EOF)
11503 {
11504 cp_parser_error (parser, "expected statement");
11505 return;
11506 }
11507
11508 /* Everything else must be a declaration-statement or an
11509 expression-statement. Try for the declaration-statement
11510 first, unless we are looking at a `;', in which case we know that
11511 we have an expression-statement. */
11512 if (!statement)
11513 {
11514 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11515 {
11516 if (std_attrs != NULL_TREE)
11517 /* Attributes should be parsed as part of the declaration,
11518 so let's un-parse them. */
11519 saved_tokens.rollback();
11520
11521 cp_parser_parse_tentatively (parser);
11522 /* Try to parse the declaration-statement. */
11523 cp_parser_declaration_statement (parser);
11524 /* If that worked, we're done. */
11525 if (cp_parser_parse_definitely (parser))
11526 return;
11527 /* It didn't work, restore the post-attribute position. */
11528 if (std_attrs)
11529 cp_lexer_set_token_position (parser->lexer, statement_token);
11530 }
11531 /* All preceding labels have been parsed at this point. */
11532 if (loc_after_labels != NULL)
11533 *loc_after_labels = statement_location;
11534
11535 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11536
11537 /* Look for an expression-statement instead. */
11538 statement = cp_parser_expression_statement (parser, in_statement_expr);
11539
11540 /* Handle [[fallthrough]];. */
11541 if (attribute_fallthrough_p (std_attrs))
11542 {
11543 /* The next token after the fallthrough attribute is ';'. */
11544 if (statement == NULL_TREE)
11545 {
11546 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11547 statement = build_call_expr_internal_loc (statement_location,
11548 IFN_FALLTHROUGH,
11549 void_type_node, 0);
11550 finish_expr_stmt (statement);
11551 }
11552 else
11553 warning_at (statement_location, OPT_Wattributes,
11554 "%<fallthrough%> attribute not followed by %<;%>");
11555 std_attrs = NULL_TREE;
11556 }
11557 }
11558
11559 /* Set the line number for the statement. */
11560 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11561 SET_EXPR_LOCATION (statement, statement_location);
11562
11563 /* Allow "[[fallthrough]];", but warn otherwise. */
11564 if (std_attrs != NULL_TREE)
11565 warning_at (attrs_loc,
11566 OPT_Wattributes,
11567 "attributes at the beginning of statement are ignored");
11568 }
11569
11570 /* Append ATTR to attribute list ATTRS. */
11571
11572 static tree
11573 attr_chainon (tree attrs, tree attr)
11574 {
11575 if (attrs == error_mark_node)
11576 return error_mark_node;
11577 if (attr == error_mark_node)
11578 return error_mark_node;
11579 return chainon (attrs, attr);
11580 }
11581
11582 /* Parse the label for a labeled-statement, i.e.
11583
11584 identifier :
11585 case constant-expression :
11586 default :
11587
11588 GNU Extension:
11589 case constant-expression ... constant-expression : statement
11590
11591 When a label is parsed without errors, the label is added to the
11592 parse tree by the finish_* functions, so this function doesn't
11593 have to return the label. */
11594
11595 static void
11596 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11597 {
11598 cp_token *token;
11599 tree label = NULL_TREE;
11600 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11601
11602 /* The next token should be an identifier. */
11603 token = cp_lexer_peek_token (parser->lexer);
11604 if (token->type != CPP_NAME
11605 && token->type != CPP_KEYWORD)
11606 {
11607 cp_parser_error (parser, "expected labeled-statement");
11608 return;
11609 }
11610
11611 /* Remember whether this case or a user-defined label is allowed to fall
11612 through to. */
11613 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11614
11615 parser->colon_corrects_to_scope_p = false;
11616 switch (token->keyword)
11617 {
11618 case RID_CASE:
11619 {
11620 tree expr, expr_hi;
11621 cp_token *ellipsis;
11622
11623 /* Consume the `case' token. */
11624 cp_lexer_consume_token (parser->lexer);
11625 /* Parse the constant-expression. */
11626 expr = cp_parser_constant_expression (parser);
11627 if (check_for_bare_parameter_packs (expr))
11628 expr = error_mark_node;
11629
11630 ellipsis = cp_lexer_peek_token (parser->lexer);
11631 if (ellipsis->type == CPP_ELLIPSIS)
11632 {
11633 /* Consume the `...' token. */
11634 cp_lexer_consume_token (parser->lexer);
11635 expr_hi = cp_parser_constant_expression (parser);
11636 if (check_for_bare_parameter_packs (expr_hi))
11637 expr_hi = error_mark_node;
11638
11639 /* We don't need to emit warnings here, as the common code
11640 will do this for us. */
11641 }
11642 else
11643 expr_hi = NULL_TREE;
11644
11645 if (parser->in_switch_statement_p)
11646 {
11647 tree l = finish_case_label (token->location, expr, expr_hi);
11648 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11649 {
11650 label = CASE_LABEL (l);
11651 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11652 }
11653 }
11654 else
11655 error_at (token->location,
11656 "case label %qE not within a switch statement",
11657 expr);
11658 }
11659 break;
11660
11661 case RID_DEFAULT:
11662 /* Consume the `default' token. */
11663 cp_lexer_consume_token (parser->lexer);
11664
11665 if (parser->in_switch_statement_p)
11666 {
11667 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11668 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11669 {
11670 label = CASE_LABEL (l);
11671 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11672 }
11673 }
11674 else
11675 error_at (token->location, "case label not within a switch statement");
11676 break;
11677
11678 default:
11679 /* Anything else must be an ordinary label. */
11680 label = finish_label_stmt (cp_parser_identifier (parser));
11681 if (label && TREE_CODE (label) == LABEL_DECL)
11682 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11683 break;
11684 }
11685
11686 /* Require the `:' token. */
11687 cp_parser_require (parser, CPP_COLON, RT_COLON);
11688
11689 /* An ordinary label may optionally be followed by attributes.
11690 However, this is only permitted if the attributes are then
11691 followed by a semicolon. This is because, for backward
11692 compatibility, when parsing
11693 lab: __attribute__ ((unused)) int i;
11694 we want the attribute to attach to "i", not "lab". */
11695 if (label != NULL_TREE
11696 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11697 {
11698 tree attrs;
11699 cp_parser_parse_tentatively (parser);
11700 attrs = cp_parser_gnu_attributes_opt (parser);
11701 if (attrs == NULL_TREE
11702 /* And fallthrough always binds to the expression-statement. */
11703 || attribute_fallthrough_p (attrs)
11704 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11705 cp_parser_abort_tentative_parse (parser);
11706 else if (!cp_parser_parse_definitely (parser))
11707 ;
11708 else
11709 attributes = attr_chainon (attributes, attrs);
11710 }
11711
11712 if (attributes != NULL_TREE)
11713 cplus_decl_attributes (&label, attributes, 0);
11714
11715 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11716 }
11717
11718 /* Parse an expression-statement.
11719
11720 expression-statement:
11721 expression [opt] ;
11722
11723 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11724 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11725 indicates whether this expression-statement is part of an
11726 expression statement. */
11727
11728 static tree
11729 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11730 {
11731 tree statement = NULL_TREE;
11732 cp_token *token = cp_lexer_peek_token (parser->lexer);
11733 location_t loc = token->location;
11734
11735 /* There might be attribute fallthrough. */
11736 tree attr = cp_parser_gnu_attributes_opt (parser);
11737
11738 /* If the next token is a ';', then there is no expression
11739 statement. */
11740 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11741 {
11742 statement = cp_parser_expression (parser);
11743 if (statement == error_mark_node
11744 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11745 {
11746 cp_parser_skip_to_end_of_block_or_statement (parser);
11747 return error_mark_node;
11748 }
11749 }
11750
11751 /* Handle [[fallthrough]];. */
11752 if (attribute_fallthrough_p (attr))
11753 {
11754 /* The next token after the fallthrough attribute is ';'. */
11755 if (statement == NULL_TREE)
11756 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11757 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11758 void_type_node, 0);
11759 else
11760 warning_at (loc, OPT_Wattributes,
11761 "%<fallthrough%> attribute not followed by %<;%>");
11762 attr = NULL_TREE;
11763 }
11764
11765 /* Allow "[[fallthrough]];", but warn otherwise. */
11766 if (attr != NULL_TREE)
11767 warning_at (loc, OPT_Wattributes,
11768 "attributes at the beginning of statement are ignored");
11769
11770 /* Give a helpful message for "A<T>::type t;" and the like. */
11771 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11772 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11773 {
11774 if (TREE_CODE (statement) == SCOPE_REF)
11775 error_at (token->location, "need %<typename%> before %qE because "
11776 "%qT is a dependent scope",
11777 statement, TREE_OPERAND (statement, 0));
11778 else if (is_overloaded_fn (statement)
11779 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11780 {
11781 /* A::A a; */
11782 tree fn = get_first_fn (statement);
11783 error_at (token->location,
11784 "%<%T::%D%> names the constructor, not the type",
11785 DECL_CONTEXT (fn), DECL_NAME (fn));
11786 }
11787 }
11788
11789 /* Consume the final `;'. */
11790 cp_parser_consume_semicolon_at_end_of_statement (parser);
11791
11792 if (in_statement_expr
11793 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11794 /* This is the final expression statement of a statement
11795 expression. */
11796 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11797 else if (statement)
11798 statement = finish_expr_stmt (statement);
11799
11800 return statement;
11801 }
11802
11803 /* Parse a compound-statement.
11804
11805 compound-statement:
11806 { statement-seq [opt] }
11807
11808 GNU extension:
11809
11810 compound-statement:
11811 { label-declaration-seq [opt] statement-seq [opt] }
11812
11813 label-declaration-seq:
11814 label-declaration
11815 label-declaration-seq label-declaration
11816
11817 Returns a tree representing the statement. */
11818
11819 static tree
11820 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11821 int bcs_flags, bool function_body)
11822 {
11823 tree compound_stmt;
11824 matching_braces braces;
11825
11826 /* Consume the `{'. */
11827 if (!braces.require_open (parser))
11828 return error_mark_node;
11829 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11830 && !function_body && cxx_dialect < cxx14)
11831 pedwarn (input_location, OPT_Wpedantic,
11832 "compound-statement in %<constexpr%> function");
11833 /* Begin the compound-statement. */
11834 compound_stmt = begin_compound_stmt (bcs_flags);
11835 /* If the next keyword is `__label__' we have a label declaration. */
11836 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11837 cp_parser_label_declaration (parser);
11838 /* Parse an (optional) statement-seq. */
11839 cp_parser_statement_seq_opt (parser, in_statement_expr);
11840
11841 if (function_body)
11842 maybe_splice_retval_cleanup (compound_stmt);
11843
11844 /* Finish the compound-statement. */
11845 finish_compound_stmt (compound_stmt);
11846 /* Consume the `}'. */
11847 braces.require_close (parser);
11848
11849 return compound_stmt;
11850 }
11851
11852 /* Parse an (optional) statement-seq.
11853
11854 statement-seq:
11855 statement
11856 statement-seq [opt] statement */
11857
11858 static void
11859 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11860 {
11861 /* Scan statements until there aren't any more. */
11862 while (true)
11863 {
11864 cp_token *token = cp_lexer_peek_token (parser->lexer);
11865
11866 /* If we are looking at a `}', then we have run out of
11867 statements; the same is true if we have reached the end
11868 of file, or have stumbled upon a stray '@end'. */
11869 if (token->type == CPP_CLOSE_BRACE
11870 || token->type == CPP_EOF
11871 || token->type == CPP_PRAGMA_EOL
11872 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11873 break;
11874
11875 /* If we are in a compound statement and find 'else' then
11876 something went wrong. */
11877 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11878 {
11879 if (parser->in_statement & IN_IF_STMT)
11880 break;
11881 else
11882 {
11883 token = cp_lexer_consume_token (parser->lexer);
11884 error_at (token->location, "%<else%> without a previous %<if%>");
11885 }
11886 }
11887
11888 /* Parse the statement. */
11889 cp_parser_statement (parser, in_statement_expr, true, NULL);
11890 }
11891 }
11892
11893 /* Return true if this is the C++20 version of range-based-for with
11894 init-statement. */
11895
11896 static bool
11897 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11898 {
11899 bool r = false;
11900
11901 /* Save tokens so that we can put them back. */
11902 cp_lexer_save_tokens (parser->lexer);
11903
11904 /* There has to be an unnested ; followed by an unnested :. */
11905 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11906 /*recovering=*/false,
11907 CPP_SEMICOLON,
11908 /*consume_paren=*/false) != -1)
11909 goto out;
11910
11911 /* We found the semicolon, eat it now. */
11912 cp_lexer_consume_token (parser->lexer);
11913
11914 /* Now look for ':' that is not nested in () or {}. */
11915 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11916 /*recovering=*/false,
11917 CPP_COLON,
11918 /*consume_paren=*/false) == -1);
11919
11920 out:
11921 /* Roll back the tokens we skipped. */
11922 cp_lexer_rollback_tokens (parser->lexer);
11923
11924 return r;
11925 }
11926
11927 /* Return true if we're looking at (init; cond), false otherwise. */
11928
11929 static bool
11930 cp_parser_init_statement_p (cp_parser *parser)
11931 {
11932 /* Save tokens so that we can put them back. */
11933 cp_lexer_save_tokens (parser->lexer);
11934
11935 /* Look for ';' that is not nested in () or {}. */
11936 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11937 /*recovering=*/false,
11938 CPP_SEMICOLON,
11939 /*consume_paren=*/false);
11940
11941 /* Roll back the tokens we skipped. */
11942 cp_lexer_rollback_tokens (parser->lexer);
11943
11944 return ret == -1;
11945 }
11946
11947 /* Parse a selection-statement.
11948
11949 selection-statement:
11950 if ( init-statement [opt] condition ) statement
11951 if ( init-statement [opt] condition ) statement else statement
11952 switch ( init-statement [opt] condition ) statement
11953
11954 Returns the new IF_STMT or SWITCH_STMT.
11955
11956 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11957 is a (possibly labeled) if statement which is not enclosed in
11958 braces and has an else clause. This is used to implement
11959 -Wparentheses.
11960
11961 CHAIN is a vector of if-else-if conditions. This is used to implement
11962 -Wduplicated-cond. */
11963
11964 static tree
11965 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11966 vec<tree> *chain)
11967 {
11968 cp_token *token;
11969 enum rid keyword;
11970 token_indent_info guard_tinfo;
11971
11972 if (if_p != NULL)
11973 *if_p = false;
11974
11975 /* Peek at the next token. */
11976 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11977 guard_tinfo = get_token_indent_info (token);
11978
11979 /* See what kind of keyword it is. */
11980 keyword = token->keyword;
11981 switch (keyword)
11982 {
11983 case RID_IF:
11984 case RID_SWITCH:
11985 {
11986 tree statement;
11987 tree condition;
11988
11989 bool cx = false;
11990 if (keyword == RID_IF
11991 && cp_lexer_next_token_is_keyword (parser->lexer,
11992 RID_CONSTEXPR))
11993 {
11994 cx = true;
11995 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11996 if (cxx_dialect < cxx17)
11997 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11998 "with %<-std=c++17%> or %<-std=gnu++17%>");
11999 }
12000
12001 /* Look for the `('. */
12002 matching_parens parens;
12003 if (!parens.require_open (parser))
12004 {
12005 cp_parser_skip_to_end_of_statement (parser);
12006 return error_mark_node;
12007 }
12008
12009 /* Begin the selection-statement. */
12010 if (keyword == RID_IF)
12011 {
12012 statement = begin_if_stmt ();
12013 IF_STMT_CONSTEXPR_P (statement) = cx;
12014 }
12015 else
12016 statement = begin_switch_stmt ();
12017
12018 /* Parse the optional init-statement. */
12019 if (cp_parser_init_statement_p (parser))
12020 {
12021 tree decl;
12022 if (cxx_dialect < cxx17)
12023 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12024 "init-statement in selection statements only available "
12025 "with %<-std=c++17%> or %<-std=gnu++17%>");
12026 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12027 {
12028 /* A non-empty init-statement can have arbitrary side
12029 effects. */
12030 delete chain;
12031 chain = NULL;
12032 }
12033 cp_parser_init_statement (parser, &decl);
12034 }
12035
12036 /* Parse the condition. */
12037 condition = cp_parser_condition (parser);
12038 /* Look for the `)'. */
12039 if (!parens.require_close (parser))
12040 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12041 /*consume_paren=*/true);
12042
12043 if (keyword == RID_IF)
12044 {
12045 bool nested_if;
12046 unsigned char in_statement;
12047
12048 /* Add the condition. */
12049 condition = finish_if_stmt_cond (condition, statement);
12050
12051 if (warn_duplicated_cond)
12052 warn_duplicated_cond_add_or_warn (token->location, condition,
12053 &chain);
12054
12055 /* Parse the then-clause. */
12056 in_statement = parser->in_statement;
12057 parser->in_statement |= IN_IF_STMT;
12058
12059 /* Outside a template, the non-selected branch of a constexpr
12060 if is a 'discarded statement', i.e. unevaluated. */
12061 bool was_discarded = in_discarded_stmt;
12062 bool discard_then = (cx && !processing_template_decl
12063 && integer_zerop (condition));
12064 if (discard_then)
12065 {
12066 in_discarded_stmt = true;
12067 ++c_inhibit_evaluation_warnings;
12068 }
12069
12070 cp_parser_implicitly_scoped_statement (parser, &nested_if,
12071 guard_tinfo);
12072
12073 parser->in_statement = in_statement;
12074
12075 finish_then_clause (statement);
12076
12077 if (discard_then)
12078 {
12079 THEN_CLAUSE (statement) = NULL_TREE;
12080 in_discarded_stmt = was_discarded;
12081 --c_inhibit_evaluation_warnings;
12082 }
12083
12084 /* If the next token is `else', parse the else-clause. */
12085 if (cp_lexer_next_token_is_keyword (parser->lexer,
12086 RID_ELSE))
12087 {
12088 bool discard_else = (cx && !processing_template_decl
12089 && integer_nonzerop (condition));
12090 if (discard_else)
12091 {
12092 in_discarded_stmt = true;
12093 ++c_inhibit_evaluation_warnings;
12094 }
12095
12096 guard_tinfo
12097 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12098 /* Consume the `else' keyword. */
12099 cp_lexer_consume_token (parser->lexer);
12100 if (warn_duplicated_cond)
12101 {
12102 if (cp_lexer_next_token_is_keyword (parser->lexer,
12103 RID_IF)
12104 && chain == NULL)
12105 {
12106 /* We've got "if (COND) else if (COND2)". Start
12107 the condition chain and add COND as the first
12108 element. */
12109 chain = new vec<tree> ();
12110 if (!CONSTANT_CLASS_P (condition)
12111 && !TREE_SIDE_EFFECTS (condition))
12112 {
12113 /* Wrap it in a NOP_EXPR so that we can set the
12114 location of the condition. */
12115 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
12116 condition);
12117 SET_EXPR_LOCATION (e, token->location);
12118 chain->safe_push (e);
12119 }
12120 }
12121 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
12122 RID_IF))
12123 {
12124 /* This is if-else without subsequent if. Zap the
12125 condition chain; we would have already warned at
12126 this point. */
12127 delete chain;
12128 chain = NULL;
12129 }
12130 }
12131 begin_else_clause (statement);
12132 /* Parse the else-clause. */
12133 cp_parser_implicitly_scoped_statement (parser, NULL,
12134 guard_tinfo, chain);
12135
12136 finish_else_clause (statement);
12137
12138 /* If we are currently parsing a then-clause, then
12139 IF_P will not be NULL. We set it to true to
12140 indicate that this if statement has an else clause.
12141 This may trigger the Wparentheses warning below
12142 when we get back up to the parent if statement. */
12143 if (if_p != NULL)
12144 *if_p = true;
12145
12146 if (discard_else)
12147 {
12148 ELSE_CLAUSE (statement) = NULL_TREE;
12149 in_discarded_stmt = was_discarded;
12150 --c_inhibit_evaluation_warnings;
12151 }
12152 }
12153 else
12154 {
12155 /* This if statement does not have an else clause. If
12156 NESTED_IF is true, then the then-clause has an if
12157 statement which does have an else clause. We warn
12158 about the potential ambiguity. */
12159 if (nested_if)
12160 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
12161 "suggest explicit braces to avoid ambiguous"
12162 " %<else%>");
12163 if (warn_duplicated_cond)
12164 {
12165 /* We don't need the condition chain anymore. */
12166 delete chain;
12167 chain = NULL;
12168 }
12169 }
12170
12171 /* Now we're all done with the if-statement. */
12172 finish_if_stmt (statement);
12173 }
12174 else
12175 {
12176 bool in_switch_statement_p;
12177 unsigned char in_statement;
12178
12179 /* Add the condition. */
12180 finish_switch_cond (condition, statement);
12181
12182 /* Parse the body of the switch-statement. */
12183 in_switch_statement_p = parser->in_switch_statement_p;
12184 in_statement = parser->in_statement;
12185 parser->in_switch_statement_p = true;
12186 parser->in_statement |= IN_SWITCH_STMT;
12187 cp_parser_implicitly_scoped_statement (parser, if_p,
12188 guard_tinfo);
12189 parser->in_switch_statement_p = in_switch_statement_p;
12190 parser->in_statement = in_statement;
12191
12192 /* Now we're all done with the switch-statement. */
12193 finish_switch_stmt (statement);
12194 }
12195
12196 return statement;
12197 }
12198 break;
12199
12200 default:
12201 cp_parser_error (parser, "expected selection-statement");
12202 return error_mark_node;
12203 }
12204 }
12205
12206 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
12207 If we have seen at least one decl-specifier, and the next token is not
12208 a parenthesis (after "int (" we might be looking at a functional cast)
12209 neither we are dealing with a concept-check expression then we must be
12210 looking at a declaration. */
12211
12212 static void
12213 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
12214 cp_decl_specifier_seq *decl_specs)
12215 {
12216 if (decl_specs->any_specifiers_p
12217 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12218 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12219 && !cp_parser_error_occurred (parser)
12220 && !(decl_specs->type
12221 && TREE_CODE (decl_specs->type) == TYPE_DECL
12222 && is_constrained_auto (TREE_TYPE (decl_specs->type))))
12223 cp_parser_commit_to_tentative_parse (parser);
12224 }
12225
12226 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
12227 The declarator shall not specify a function or an array. Returns
12228 TRUE if the declarator is valid, FALSE otherwise. */
12229
12230 static bool
12231 cp_parser_check_condition_declarator (cp_parser* parser,
12232 cp_declarator *declarator,
12233 location_t loc)
12234 {
12235 if (declarator == cp_error_declarator
12236 || function_declarator_p (declarator)
12237 || declarator->kind == cdk_array)
12238 {
12239 if (declarator == cp_error_declarator)
12240 /* Already complained. */;
12241 else if (declarator->kind == cdk_array)
12242 error_at (loc, "condition declares an array");
12243 else
12244 error_at (loc, "condition declares a function");
12245 if (parser->fully_implicit_function_template_p)
12246 abort_fully_implicit_template (parser);
12247 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
12248 /*or_comma=*/false,
12249 /*consume_paren=*/false);
12250 return false;
12251 }
12252 else
12253 return true;
12254 }
12255
12256 /* Parse a condition.
12257
12258 condition:
12259 expression
12260 type-specifier-seq declarator = initializer-clause
12261 type-specifier-seq declarator braced-init-list
12262
12263 GNU Extension:
12264
12265 condition:
12266 type-specifier-seq declarator asm-specification [opt]
12267 attributes [opt] = assignment-expression
12268
12269 Returns the expression that should be tested. */
12270
12271 static tree
12272 cp_parser_condition (cp_parser* parser)
12273 {
12274 cp_decl_specifier_seq type_specifiers;
12275 const char *saved_message;
12276 int declares_class_or_enum;
12277
12278 /* Try the declaration first. */
12279 cp_parser_parse_tentatively (parser);
12280 /* New types are not allowed in the type-specifier-seq for a
12281 condition. */
12282 saved_message = parser->type_definition_forbidden_message;
12283 parser->type_definition_forbidden_message
12284 = G_("types may not be defined in conditions");
12285 /* Parse the type-specifier-seq. */
12286 cp_parser_decl_specifier_seq (parser,
12287 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
12288 &type_specifiers,
12289 &declares_class_or_enum);
12290 /* Restore the saved message. */
12291 parser->type_definition_forbidden_message = saved_message;
12292
12293 /* Gather the attributes that were provided with the
12294 decl-specifiers. */
12295 tree prefix_attributes = type_specifiers.attributes;
12296
12297 cp_parser_maybe_commit_to_declaration (parser, &type_specifiers);
12298
12299 /* If all is well, we might be looking at a declaration. */
12300 if (!cp_parser_error_occurred (parser))
12301 {
12302 tree decl;
12303 tree asm_specification;
12304 tree attributes;
12305 cp_declarator *declarator;
12306 tree initializer = NULL_TREE;
12307 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12308
12309 /* Parse the declarator. */
12310 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12311 CP_PARSER_FLAGS_NONE,
12312 /*ctor_dtor_or_conv_p=*/NULL,
12313 /*parenthesized_p=*/NULL,
12314 /*member_p=*/false,
12315 /*friend_p=*/false,
12316 /*static_p=*/false);
12317 /* Parse the attributes. */
12318 attributes = cp_parser_attributes_opt (parser);
12319 /* Parse the asm-specification. */
12320 asm_specification = cp_parser_asm_specification_opt (parser);
12321 /* If the next token is not an `=' or '{', then we might still be
12322 looking at an expression. For example:
12323
12324 if (A(a).x)
12325
12326 looks like a decl-specifier-seq and a declarator -- but then
12327 there is no `=', so this is an expression. */
12328 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12329 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12330 cp_parser_simulate_error (parser);
12331
12332 /* If we did see an `=' or '{', then we are looking at a declaration
12333 for sure. */
12334 if (cp_parser_parse_definitely (parser))
12335 {
12336 tree pushed_scope;
12337 bool non_constant_p = false;
12338 int flags = LOOKUP_ONLYCONVERTING;
12339
12340 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
12341 return error_mark_node;
12342
12343 /* Create the declaration. */
12344 decl = start_decl (declarator, &type_specifiers,
12345 /*initialized_p=*/true,
12346 attributes, prefix_attributes,
12347 &pushed_scope);
12348
12349 /* Parse the initializer. */
12350 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12351 {
12352 initializer = cp_parser_braced_list (parser, &non_constant_p);
12353 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
12354 flags = 0;
12355 }
12356 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12357 {
12358 /* Consume the `='. */
12359 cp_lexer_consume_token (parser->lexer);
12360 initializer = cp_parser_initializer_clause (parser,
12361 &non_constant_p);
12362 }
12363 else
12364 {
12365 cp_parser_error (parser, "expected initializer");
12366 initializer = error_mark_node;
12367 }
12368 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
12369 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12370
12371 /* Process the initializer. */
12372 cp_finish_decl (decl,
12373 initializer, !non_constant_p,
12374 asm_specification,
12375 flags);
12376
12377 if (pushed_scope)
12378 pop_scope (pushed_scope);
12379
12380 return convert_from_reference (decl);
12381 }
12382 }
12383 /* If we didn't even get past the declarator successfully, we are
12384 definitely not looking at a declaration. */
12385 else
12386 cp_parser_abort_tentative_parse (parser);
12387
12388 /* Otherwise, we are looking at an expression. */
12389 return cp_parser_expression (parser);
12390 }
12391
12392 /* Parses a for-statement or range-for-statement until the closing ')',
12393 not included. */
12394
12395 static tree
12396 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
12397 {
12398 tree init, scope, decl;
12399 bool is_range_for;
12400
12401 /* Begin the for-statement. */
12402 scope = begin_for_scope (&init);
12403
12404 /* Parse the initialization. */
12405 is_range_for = cp_parser_init_statement (parser, &decl);
12406
12407 if (is_range_for)
12408 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
12409 false);
12410 else
12411 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
12412 }
12413
12414 static tree
12415 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
12416 unsigned short unroll)
12417 {
12418 /* Normal for loop */
12419 tree condition = NULL_TREE;
12420 tree expression = NULL_TREE;
12421 tree stmt;
12422
12423 stmt = begin_for_stmt (scope, init);
12424 /* The init-statement has already been parsed in
12425 cp_parser_init_statement, so no work is needed here. */
12426 finish_init_stmt (stmt);
12427
12428 /* If there's a condition, process it. */
12429 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12430 condition = cp_parser_condition (parser);
12431 else if (ivdep)
12432 {
12433 cp_parser_error (parser, "missing loop condition in loop with "
12434 "%<GCC ivdep%> pragma");
12435 condition = error_mark_node;
12436 }
12437 else if (unroll)
12438 {
12439 cp_parser_error (parser, "missing loop condition in loop with "
12440 "%<GCC unroll%> pragma");
12441 condition = error_mark_node;
12442 }
12443 finish_for_cond (condition, stmt, ivdep, unroll);
12444 /* Look for the `;'. */
12445 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12446
12447 /* If there's an expression, process it. */
12448 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
12449 expression = cp_parser_expression (parser);
12450 finish_for_expr (expression, stmt);
12451
12452 return stmt;
12453 }
12454
12455 /* Tries to parse a range-based for-statement:
12456
12457 range-based-for:
12458 decl-specifier-seq declarator : expression
12459
12460 The decl-specifier-seq declarator and the `:' are already parsed by
12461 cp_parser_init_statement. If processing_template_decl it returns a
12462 newly created RANGE_FOR_STMT; if not, it is converted to a
12463 regular FOR_STMT. */
12464
12465 static tree
12466 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
12467 bool ivdep, unsigned short unroll, bool is_omp)
12468 {
12469 tree stmt, range_expr;
12470 auto_vec <cxx_binding *, 16> bindings;
12471 auto_vec <tree, 16> names;
12472 tree decomp_first_name = NULL_TREE;
12473 unsigned int decomp_cnt = 0;
12474
12475 /* Get the range declaration momentarily out of the way so that
12476 the range expression doesn't clash with it. */
12477 if (range_decl != error_mark_node)
12478 {
12479 if (DECL_HAS_VALUE_EXPR_P (range_decl))
12480 {
12481 tree v = DECL_VALUE_EXPR (range_decl);
12482 /* For decomposition declaration get all of the corresponding
12483 declarations out of the way. */
12484 if (TREE_CODE (v) == ARRAY_REF
12485 && VAR_P (TREE_OPERAND (v, 0))
12486 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
12487 {
12488 tree d = range_decl;
12489 range_decl = TREE_OPERAND (v, 0);
12490 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
12491 decomp_first_name = d;
12492 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
12493 {
12494 tree name = DECL_NAME (d);
12495 names.safe_push (name);
12496 bindings.safe_push (IDENTIFIER_BINDING (name));
12497 IDENTIFIER_BINDING (name)
12498 = IDENTIFIER_BINDING (name)->previous;
12499 }
12500 }
12501 }
12502 if (names.is_empty ())
12503 {
12504 tree name = DECL_NAME (range_decl);
12505 names.safe_push (name);
12506 bindings.safe_push (IDENTIFIER_BINDING (name));
12507 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
12508 }
12509 }
12510
12511 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12512 {
12513 bool expr_non_constant_p;
12514 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12515 }
12516 else
12517 range_expr = cp_parser_expression (parser);
12518
12519 /* Put the range declaration(s) back into scope. */
12520 for (unsigned int i = 0; i < names.length (); i++)
12521 {
12522 cxx_binding *binding = bindings[i];
12523 binding->previous = IDENTIFIER_BINDING (names[i]);
12524 IDENTIFIER_BINDING (names[i]) = binding;
12525 }
12526
12527 /* finish_omp_for has its own code for the following, so just
12528 return the range_expr instead. */
12529 if (is_omp)
12530 return range_expr;
12531
12532 /* If in template, STMT is converted to a normal for-statement
12533 at instantiation. If not, it is done just ahead. */
12534 if (processing_template_decl)
12535 {
12536 if (check_for_bare_parameter_packs (range_expr))
12537 range_expr = error_mark_node;
12538 stmt = begin_range_for_stmt (scope, init);
12539 if (ivdep)
12540 RANGE_FOR_IVDEP (stmt) = 1;
12541 if (unroll)
12542 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12543 finish_range_for_decl (stmt, range_decl, range_expr);
12544 if (!type_dependent_expression_p (range_expr)
12545 /* do_auto_deduction doesn't mess with template init-lists. */
12546 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12547 do_range_for_auto_deduction (range_decl, range_expr);
12548 }
12549 else
12550 {
12551 stmt = begin_for_stmt (scope, init);
12552 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12553 decomp_first_name, decomp_cnt, ivdep,
12554 unroll);
12555 }
12556 return stmt;
12557 }
12558
12559 /* Subroutine of cp_convert_range_for: given the initializer expression,
12560 builds up the range temporary. */
12561
12562 static tree
12563 build_range_temp (tree range_expr)
12564 {
12565 tree range_type, range_temp;
12566
12567 /* Find out the type deduced by the declaration
12568 `auto &&__range = range_expr'. */
12569 range_type = cp_build_reference_type (make_auto (), true);
12570 range_type = do_auto_deduction (range_type, range_expr,
12571 type_uses_auto (range_type));
12572
12573 /* Create the __range variable. */
12574 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12575 range_type);
12576 TREE_USED (range_temp) = 1;
12577 DECL_ARTIFICIAL (range_temp) = 1;
12578
12579 return range_temp;
12580 }
12581
12582 /* Used by cp_parser_range_for in template context: we aren't going to
12583 do a full conversion yet, but we still need to resolve auto in the
12584 type of the for-range-declaration if present. This is basically
12585 a shortcut version of cp_convert_range_for. */
12586
12587 static void
12588 do_range_for_auto_deduction (tree decl, tree range_expr)
12589 {
12590 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12591 if (auto_node)
12592 {
12593 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12594 range_temp = convert_from_reference (build_range_temp (range_expr));
12595 iter_type = (cp_parser_perform_range_for_lookup
12596 (range_temp, &begin_dummy, &end_dummy));
12597 if (iter_type)
12598 {
12599 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12600 iter_type);
12601 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12602 RO_UNARY_STAR,
12603 tf_warning_or_error);
12604 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12605 iter_decl, auto_node);
12606 }
12607 }
12608 }
12609
12610 /* Converts a range-based for-statement into a normal
12611 for-statement, as per the definition.
12612
12613 for (RANGE_DECL : RANGE_EXPR)
12614 BLOCK
12615
12616 should be equivalent to:
12617
12618 {
12619 auto &&__range = RANGE_EXPR;
12620 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12621 __begin != __end;
12622 ++__begin)
12623 {
12624 RANGE_DECL = *__begin;
12625 BLOCK
12626 }
12627 }
12628
12629 If RANGE_EXPR is an array:
12630 BEGIN_EXPR = __range
12631 END_EXPR = __range + ARRAY_SIZE(__range)
12632 Else if RANGE_EXPR has a member 'begin' or 'end':
12633 BEGIN_EXPR = __range.begin()
12634 END_EXPR = __range.end()
12635 Else:
12636 BEGIN_EXPR = begin(__range)
12637 END_EXPR = end(__range);
12638
12639 If __range has a member 'begin' but not 'end', or vice versa, we must
12640 still use the second alternative (it will surely fail, however).
12641 When calling begin()/end() in the third alternative we must use
12642 argument dependent lookup, but always considering 'std' as an associated
12643 namespace. */
12644
12645 tree
12646 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12647 tree decomp_first_name, unsigned int decomp_cnt,
12648 bool ivdep, unsigned short unroll)
12649 {
12650 tree begin, end;
12651 tree iter_type, begin_expr, end_expr;
12652 tree condition, expression;
12653
12654 range_expr = mark_lvalue_use (range_expr);
12655
12656 if (range_decl == error_mark_node || range_expr == error_mark_node)
12657 /* If an error happened previously do nothing or else a lot of
12658 unhelpful errors would be issued. */
12659 begin_expr = end_expr = iter_type = error_mark_node;
12660 else
12661 {
12662 tree range_temp;
12663
12664 if (VAR_P (range_expr)
12665 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12666 /* Can't bind a reference to an array of runtime bound. */
12667 range_temp = range_expr;
12668 else
12669 {
12670 range_temp = build_range_temp (range_expr);
12671 pushdecl (range_temp);
12672 cp_finish_decl (range_temp, range_expr,
12673 /*is_constant_init*/false, NULL_TREE,
12674 LOOKUP_ONLYCONVERTING);
12675 range_temp = convert_from_reference (range_temp);
12676 }
12677 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12678 &begin_expr, &end_expr);
12679 }
12680
12681 /* The new for initialization statement. */
12682 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12683 iter_type);
12684 TREE_USED (begin) = 1;
12685 DECL_ARTIFICIAL (begin) = 1;
12686 pushdecl (begin);
12687 cp_finish_decl (begin, begin_expr,
12688 /*is_constant_init*/false, NULL_TREE,
12689 LOOKUP_ONLYCONVERTING);
12690
12691 if (cxx_dialect >= cxx17)
12692 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12693 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12694 TREE_USED (end) = 1;
12695 DECL_ARTIFICIAL (end) = 1;
12696 pushdecl (end);
12697 cp_finish_decl (end, end_expr,
12698 /*is_constant_init*/false, NULL_TREE,
12699 LOOKUP_ONLYCONVERTING);
12700
12701 finish_init_stmt (statement);
12702
12703 /* The new for condition. */
12704 condition = build_x_binary_op (input_location, NE_EXPR,
12705 begin, ERROR_MARK,
12706 end, ERROR_MARK,
12707 NULL, tf_warning_or_error);
12708 finish_for_cond (condition, statement, ivdep, unroll);
12709
12710 /* The new increment expression. */
12711 expression = finish_unary_op_expr (input_location,
12712 PREINCREMENT_EXPR, begin,
12713 tf_warning_or_error);
12714 finish_for_expr (expression, statement);
12715
12716 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12717 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12718
12719 /* The declaration is initialized with *__begin inside the loop body. */
12720 cp_finish_decl (range_decl,
12721 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12722 tf_warning_or_error),
12723 /*is_constant_init*/false, NULL_TREE,
12724 LOOKUP_ONLYCONVERTING);
12725 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12726 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12727
12728 return statement;
12729 }
12730
12731 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12732 We need to solve both at the same time because the method used
12733 depends on the existence of members begin or end.
12734 Returns the type deduced for the iterator expression. */
12735
12736 static tree
12737 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12738 {
12739 if (error_operand_p (range))
12740 {
12741 *begin = *end = error_mark_node;
12742 return error_mark_node;
12743 }
12744
12745 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12746 {
12747 error ("range-based %<for%> expression of type %qT "
12748 "has incomplete type", TREE_TYPE (range));
12749 *begin = *end = error_mark_node;
12750 return error_mark_node;
12751 }
12752 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12753 {
12754 /* If RANGE is an array, we will use pointer arithmetic. */
12755 *begin = decay_conversion (range, tf_warning_or_error);
12756 *end = build_binary_op (input_location, PLUS_EXPR,
12757 range,
12758 array_type_nelts_top (TREE_TYPE (range)),
12759 false);
12760 return TREE_TYPE (*begin);
12761 }
12762 else
12763 {
12764 /* If it is not an array, we must do a bit of magic. */
12765 tree id_begin, id_end;
12766 tree member_begin, member_end;
12767
12768 *begin = *end = error_mark_node;
12769
12770 id_begin = get_identifier ("begin");
12771 id_end = get_identifier ("end");
12772 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12773 /*protect=*/2, /*want_type=*/false,
12774 tf_warning_or_error);
12775 member_end = lookup_member (TREE_TYPE (range), id_end,
12776 /*protect=*/2, /*want_type=*/false,
12777 tf_warning_or_error);
12778
12779 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12780 {
12781 /* Use the member functions. */
12782 *begin = cp_parser_range_for_member_function (range, id_begin);
12783 *end = cp_parser_range_for_member_function (range, id_end);
12784 }
12785 else
12786 {
12787 /* Use global functions with ADL. */
12788 releasing_vec vec;
12789
12790 vec_safe_push (vec, range);
12791
12792 member_begin = perform_koenig_lookup (id_begin, vec,
12793 tf_warning_or_error);
12794 *begin = finish_call_expr (member_begin, &vec, false, true,
12795 tf_warning_or_error);
12796 member_end = perform_koenig_lookup (id_end, vec,
12797 tf_warning_or_error);
12798 *end = finish_call_expr (member_end, &vec, false, true,
12799 tf_warning_or_error);
12800 }
12801
12802 /* Last common checks. */
12803 if (*begin == error_mark_node || *end == error_mark_node)
12804 {
12805 /* If one of the expressions is an error do no more checks. */
12806 *begin = *end = error_mark_node;
12807 return error_mark_node;
12808 }
12809 else if (type_dependent_expression_p (*begin)
12810 || type_dependent_expression_p (*end))
12811 /* Can happen, when, eg, in a template context, Koenig lookup
12812 can't resolve begin/end (c++/58503). */
12813 return NULL_TREE;
12814 else
12815 {
12816 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12817 /* The unqualified type of the __begin and __end temporaries should
12818 be the same, as required by the multiple auto declaration. */
12819 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12820 {
12821 if (cxx_dialect >= cxx17
12822 && (build_x_binary_op (input_location, NE_EXPR,
12823 *begin, ERROR_MARK,
12824 *end, ERROR_MARK,
12825 NULL, tf_none)
12826 != error_mark_node))
12827 /* P0184R0 allows __begin and __end to have different types,
12828 but make sure they are comparable so we can give a better
12829 diagnostic. */;
12830 else
12831 error ("inconsistent begin/end types in range-based %<for%> "
12832 "statement: %qT and %qT",
12833 TREE_TYPE (*begin), TREE_TYPE (*end));
12834 }
12835 return iter_type;
12836 }
12837 }
12838 }
12839
12840 /* Helper function for cp_parser_perform_range_for_lookup.
12841 Builds a tree for RANGE.IDENTIFIER(). */
12842
12843 static tree
12844 cp_parser_range_for_member_function (tree range, tree identifier)
12845 {
12846 tree member, res;
12847
12848 member = finish_class_member_access_expr (range, identifier,
12849 false, tf_warning_or_error);
12850 if (member == error_mark_node)
12851 return error_mark_node;
12852
12853 releasing_vec vec;
12854 res = finish_call_expr (member, &vec,
12855 /*disallow_virtual=*/false,
12856 /*koenig_p=*/false,
12857 tf_warning_or_error);
12858 return res;
12859 }
12860
12861 /* Parse an iteration-statement.
12862
12863 iteration-statement:
12864 while ( condition ) statement
12865 do statement while ( expression ) ;
12866 for ( init-statement condition [opt] ; expression [opt] )
12867 statement
12868
12869 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12870
12871 static tree
12872 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12873 unsigned short unroll)
12874 {
12875 cp_token *token;
12876 enum rid keyword;
12877 tree statement;
12878 unsigned char in_statement;
12879 token_indent_info guard_tinfo;
12880
12881 /* Peek at the next token. */
12882 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12883 if (!token)
12884 return error_mark_node;
12885
12886 guard_tinfo = get_token_indent_info (token);
12887
12888 /* Remember whether or not we are already within an iteration
12889 statement. */
12890 in_statement = parser->in_statement;
12891
12892 /* See what kind of keyword it is. */
12893 keyword = token->keyword;
12894 switch (keyword)
12895 {
12896 case RID_WHILE:
12897 {
12898 tree condition;
12899
12900 /* Begin the while-statement. */
12901 statement = begin_while_stmt ();
12902 /* Look for the `('. */
12903 matching_parens parens;
12904 parens.require_open (parser);
12905 /* Parse the condition. */
12906 condition = cp_parser_condition (parser);
12907 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12908 /* Look for the `)'. */
12909 parens.require_close (parser);
12910 /* Parse the dependent statement. */
12911 parser->in_statement = IN_ITERATION_STMT;
12912 bool prev = note_iteration_stmt_body_start ();
12913 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12914 note_iteration_stmt_body_end (prev);
12915 parser->in_statement = in_statement;
12916 /* We're done with the while-statement. */
12917 finish_while_stmt (statement);
12918 }
12919 break;
12920
12921 case RID_DO:
12922 {
12923 tree expression;
12924
12925 /* Begin the do-statement. */
12926 statement = begin_do_stmt ();
12927 /* Parse the body of the do-statement. */
12928 parser->in_statement = IN_ITERATION_STMT;
12929 bool prev = note_iteration_stmt_body_start ();
12930 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12931 note_iteration_stmt_body_end (prev);
12932 parser->in_statement = in_statement;
12933 finish_do_body (statement);
12934 /* Look for the `while' keyword. */
12935 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12936 /* Look for the `('. */
12937 matching_parens parens;
12938 parens.require_open (parser);
12939 /* Parse the expression. */
12940 expression = cp_parser_expression (parser);
12941 /* We're done with the do-statement. */
12942 finish_do_stmt (expression, statement, ivdep, unroll);
12943 /* Look for the `)'. */
12944 parens.require_close (parser);
12945 /* Look for the `;'. */
12946 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12947 }
12948 break;
12949
12950 case RID_FOR:
12951 {
12952 /* Look for the `('. */
12953 matching_parens parens;
12954 parens.require_open (parser);
12955
12956 statement = cp_parser_for (parser, ivdep, unroll);
12957
12958 /* Look for the `)'. */
12959 parens.require_close (parser);
12960
12961 /* Parse the body of the for-statement. */
12962 parser->in_statement = IN_ITERATION_STMT;
12963 bool prev = note_iteration_stmt_body_start ();
12964 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12965 note_iteration_stmt_body_end (prev);
12966 parser->in_statement = in_statement;
12967
12968 /* We're done with the for-statement. */
12969 finish_for_stmt (statement);
12970 }
12971 break;
12972
12973 default:
12974 cp_parser_error (parser, "expected iteration-statement");
12975 statement = error_mark_node;
12976 break;
12977 }
12978
12979 return statement;
12980 }
12981
12982 /* Parse a init-statement or the declarator of a range-based-for.
12983 Returns true if a range-based-for declaration is seen.
12984
12985 init-statement:
12986 expression-statement
12987 simple-declaration */
12988
12989 static bool
12990 cp_parser_init_statement (cp_parser *parser, tree *decl)
12991 {
12992 /* If the next token is a `;', then we have an empty
12993 expression-statement. Grammatically, this is also a
12994 simple-declaration, but an invalid one, because it does not
12995 declare anything. Therefore, if we did not handle this case
12996 specially, we would issue an error message about an invalid
12997 declaration. */
12998 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12999 {
13000 bool is_range_for = false;
13001 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13002
13003 /* Try to parse the init-statement. */
13004 if (cp_parser_range_based_for_with_init_p (parser))
13005 {
13006 tree dummy;
13007 cp_parser_parse_tentatively (parser);
13008 /* Parse the declaration. */
13009 cp_parser_simple_declaration (parser,
13010 /*function_definition_allowed_p=*/false,
13011 &dummy);
13012 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13013 if (!cp_parser_parse_definitely (parser))
13014 /* That didn't work, try to parse it as an expression-statement. */
13015 cp_parser_expression_statement (parser, NULL_TREE);
13016
13017 if (cxx_dialect < cxx20)
13018 {
13019 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
13020 "range-based %<for%> loops with initializer only "
13021 "available with %<-std=c++20%> or %<-std=gnu++20%>");
13022 *decl = error_mark_node;
13023 }
13024 }
13025
13026 /* A colon is used in range-based for. */
13027 parser->colon_corrects_to_scope_p = false;
13028
13029 /* We're going to speculatively look for a declaration, falling back
13030 to an expression, if necessary. */
13031 cp_parser_parse_tentatively (parser);
13032 /* Parse the declaration. */
13033 cp_parser_simple_declaration (parser,
13034 /*function_definition_allowed_p=*/false,
13035 decl);
13036 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13037 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13038 {
13039 /* It is a range-for, consume the ':'. */
13040 cp_lexer_consume_token (parser->lexer);
13041 is_range_for = true;
13042 if (cxx_dialect < cxx11)
13043 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
13044 "range-based %<for%> loops only available with "
13045 "%<-std=c++11%> or %<-std=gnu++11%>");
13046 }
13047 else
13048 /* The ';' is not consumed yet because we told
13049 cp_parser_simple_declaration not to. */
13050 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13051
13052 if (cp_parser_parse_definitely (parser))
13053 return is_range_for;
13054 /* If the tentative parse failed, then we shall need to look for an
13055 expression-statement. */
13056 }
13057 /* If we are here, it is an expression-statement. */
13058 cp_parser_expression_statement (parser, NULL_TREE);
13059 return false;
13060 }
13061
13062 /* Parse a jump-statement.
13063
13064 jump-statement:
13065 break ;
13066 continue ;
13067 return expression [opt] ;
13068 return braced-init-list ;
13069 coroutine-return-statement;
13070 goto identifier ;
13071
13072 GNU extension:
13073
13074 jump-statement:
13075 goto * expression ;
13076
13077 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
13078
13079 static tree
13080 cp_parser_jump_statement (cp_parser* parser)
13081 {
13082 tree statement = error_mark_node;
13083 cp_token *token;
13084 enum rid keyword;
13085 unsigned char in_statement;
13086
13087 /* Peek at the next token. */
13088 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
13089 if (!token)
13090 return error_mark_node;
13091
13092 /* See what kind of keyword it is. */
13093 keyword = token->keyword;
13094 switch (keyword)
13095 {
13096 case RID_BREAK:
13097 in_statement = parser->in_statement & ~IN_IF_STMT;
13098 switch (in_statement)
13099 {
13100 case 0:
13101 error_at (token->location, "break statement not within loop or switch");
13102 break;
13103 default:
13104 gcc_assert ((in_statement & IN_SWITCH_STMT)
13105 || in_statement == IN_ITERATION_STMT);
13106 statement = finish_break_stmt ();
13107 if (in_statement == IN_ITERATION_STMT)
13108 break_maybe_infinite_loop ();
13109 break;
13110 case IN_OMP_BLOCK:
13111 error_at (token->location, "invalid exit from OpenMP structured block");
13112 break;
13113 case IN_OMP_FOR:
13114 error_at (token->location, "break statement used with OpenMP for loop");
13115 break;
13116 }
13117 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13118 break;
13119
13120 case RID_CONTINUE:
13121 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
13122 {
13123 case 0:
13124 error_at (token->location, "continue statement not within a loop");
13125 break;
13126 /* Fall through. */
13127 case IN_ITERATION_STMT:
13128 case IN_OMP_FOR:
13129 statement = finish_continue_stmt ();
13130 break;
13131 case IN_OMP_BLOCK:
13132 error_at (token->location, "invalid exit from OpenMP structured block");
13133 break;
13134 default:
13135 gcc_unreachable ();
13136 }
13137 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13138 break;
13139
13140 case RID_CO_RETURN:
13141 case RID_RETURN:
13142 {
13143 tree expr;
13144 bool expr_non_constant_p;
13145
13146 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13147 {
13148 cp_lexer_set_source_position (parser->lexer);
13149 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13150 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
13151 }
13152 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13153 expr = cp_parser_expression (parser);
13154 else
13155 /* If the next token is a `;', then there is no
13156 expression. */
13157 expr = NULL_TREE;
13158 /* Build the return-statement, check co-return first, since type
13159 deduction is not valid there. */
13160 if (keyword == RID_CO_RETURN)
13161 statement = finish_co_return_stmt (token->location, expr);
13162 else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
13163 /* Don't deduce from a discarded return statement. */;
13164 else
13165 statement = finish_return_stmt (expr);
13166 /* Look for the final `;'. */
13167 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13168 }
13169 break;
13170
13171 case RID_GOTO:
13172 if (parser->in_function_body
13173 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
13174 {
13175 error ("%<goto%> in %<constexpr%> function");
13176 cp_function_chain->invalid_constexpr = true;
13177 }
13178
13179 /* Create the goto-statement. */
13180 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
13181 {
13182 /* Issue a warning about this use of a GNU extension. */
13183 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
13184 /* Consume the '*' token. */
13185 cp_lexer_consume_token (parser->lexer);
13186 /* Parse the dependent expression. */
13187 finish_goto_stmt (cp_parser_expression (parser));
13188 }
13189 else
13190 finish_goto_stmt (cp_parser_identifier (parser));
13191 /* Look for the final `;'. */
13192 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13193 break;
13194
13195 default:
13196 cp_parser_error (parser, "expected jump-statement");
13197 break;
13198 }
13199
13200 return statement;
13201 }
13202
13203 /* Parse a declaration-statement.
13204
13205 declaration-statement:
13206 block-declaration */
13207
13208 static void
13209 cp_parser_declaration_statement (cp_parser* parser)
13210 {
13211 void *p;
13212
13213 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13214 p = obstack_alloc (&declarator_obstack, 0);
13215
13216 /* Parse the block-declaration. */
13217 cp_parser_block_declaration (parser, /*statement_p=*/true);
13218
13219 /* Free any declarators allocated. */
13220 obstack_free (&declarator_obstack, p);
13221 }
13222
13223 /* Some dependent statements (like `if (cond) statement'), are
13224 implicitly in their own scope. In other words, if the statement is
13225 a single statement (as opposed to a compound-statement), it is
13226 none-the-less treated as if it were enclosed in braces. Any
13227 declarations appearing in the dependent statement are out of scope
13228 after control passes that point. This function parses a statement,
13229 but ensures that is in its own scope, even if it is not a
13230 compound-statement.
13231
13232 If IF_P is not NULL, *IF_P is set to indicate whether the statement
13233 is a (possibly labeled) if statement which is not enclosed in
13234 braces and has an else clause. This is used to implement
13235 -Wparentheses.
13236
13237 CHAIN is a vector of if-else-if conditions. This is used to implement
13238 -Wduplicated-cond.
13239
13240 Returns the new statement. */
13241
13242 static tree
13243 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
13244 const token_indent_info &guard_tinfo,
13245 vec<tree> *chain)
13246 {
13247 tree statement;
13248 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
13249 location_t body_loc_after_labels = UNKNOWN_LOCATION;
13250 token_indent_info body_tinfo
13251 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13252
13253 if (if_p != NULL)
13254 *if_p = false;
13255
13256 /* Mark if () ; with a special NOP_EXPR. */
13257 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13258 {
13259 cp_lexer_consume_token (parser->lexer);
13260 statement = add_stmt (build_empty_stmt (body_loc));
13261
13262 if (guard_tinfo.keyword == RID_IF
13263 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
13264 warning_at (body_loc, OPT_Wempty_body,
13265 "suggest braces around empty body in an %<if%> statement");
13266 else if (guard_tinfo.keyword == RID_ELSE)
13267 warning_at (body_loc, OPT_Wempty_body,
13268 "suggest braces around empty body in an %<else%> statement");
13269 }
13270 /* if a compound is opened, we simply parse the statement directly. */
13271 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13272 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
13273 /* If the token is not a `{', then we must take special action. */
13274 else
13275 {
13276 /* Create a compound-statement. */
13277 statement = begin_compound_stmt (0);
13278 /* Parse the dependent-statement. */
13279 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
13280 &body_loc_after_labels);
13281 /* Finish the dummy compound-statement. */
13282 finish_compound_stmt (statement);
13283 }
13284
13285 token_indent_info next_tinfo
13286 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13287 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13288
13289 if (body_loc_after_labels != UNKNOWN_LOCATION
13290 && next_tinfo.type != CPP_SEMICOLON)
13291 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
13292 guard_tinfo.location, guard_tinfo.keyword);
13293
13294 /* Return the statement. */
13295 return statement;
13296 }
13297
13298 /* For some dependent statements (like `while (cond) statement'), we
13299 have already created a scope. Therefore, even if the dependent
13300 statement is a compound-statement, we do not want to create another
13301 scope. */
13302
13303 static void
13304 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
13305 const token_indent_info &guard_tinfo)
13306 {
13307 /* If the token is a `{', then we must take special action. */
13308 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13309 {
13310 token_indent_info body_tinfo
13311 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13312 location_t loc_after_labels = UNKNOWN_LOCATION;
13313
13314 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
13315 &loc_after_labels);
13316 token_indent_info next_tinfo
13317 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13318 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13319
13320 if (loc_after_labels != UNKNOWN_LOCATION
13321 && next_tinfo.type != CPP_SEMICOLON)
13322 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
13323 guard_tinfo.location,
13324 guard_tinfo.keyword);
13325 }
13326 else
13327 {
13328 /* Avoid calling cp_parser_compound_statement, so that we
13329 don't create a new scope. Do everything else by hand. */
13330 matching_braces braces;
13331 braces.require_open (parser);
13332 /* If the next keyword is `__label__' we have a label declaration. */
13333 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13334 cp_parser_label_declaration (parser);
13335 /* Parse an (optional) statement-seq. */
13336 cp_parser_statement_seq_opt (parser, NULL_TREE);
13337 braces.require_close (parser);
13338 }
13339 }
13340
13341 /* Declarations [gram.dcl.dcl] */
13342
13343 /* Parse an optional declaration-sequence.
13344
13345 declaration-seq:
13346 declaration
13347 declaration-seq declaration */
13348
13349 static void
13350 cp_parser_declaration_seq_opt (cp_parser* parser)
13351 {
13352 while (true)
13353 {
13354 cp_token *token = cp_lexer_peek_token (parser->lexer);
13355
13356 if (token->type == CPP_CLOSE_BRACE
13357 || token->type == CPP_EOF)
13358 break;
13359 else
13360 cp_parser_toplevel_declaration (parser);
13361 }
13362 }
13363
13364 /* Parse a declaration.
13365
13366 declaration:
13367 block-declaration
13368 function-definition
13369 template-declaration
13370 explicit-instantiation
13371 explicit-specialization
13372 linkage-specification
13373 namespace-definition
13374
13375 C++17:
13376 deduction-guide
13377
13378 GNU extension:
13379
13380 declaration:
13381 __extension__ declaration */
13382
13383 static void
13384 cp_parser_declaration (cp_parser* parser)
13385 {
13386 cp_token token1;
13387 cp_token token2;
13388 int saved_pedantic;
13389 void *p;
13390 tree attributes = NULL_TREE;
13391
13392 /* Check for the `__extension__' keyword. */
13393 if (cp_parser_extension_opt (parser, &saved_pedantic))
13394 {
13395 /* Parse the qualified declaration. */
13396 cp_parser_declaration (parser);
13397 /* Restore the PEDANTIC flag. */
13398 pedantic = saved_pedantic;
13399
13400 return;
13401 }
13402
13403 /* Try to figure out what kind of declaration is present. */
13404 token1 = *cp_lexer_peek_token (parser->lexer);
13405
13406 if (token1.type != CPP_EOF)
13407 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
13408 else
13409 {
13410 token2.type = CPP_EOF;
13411 token2.keyword = RID_MAX;
13412 }
13413
13414 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13415 p = obstack_alloc (&declarator_obstack, 0);
13416
13417 /* If the next token is `extern' and the following token is a string
13418 literal, then we have a linkage specification. */
13419 if (token1.keyword == RID_EXTERN
13420 && cp_parser_is_pure_string_literal (&token2))
13421 cp_parser_linkage_specification (parser);
13422 /* If the next token is `template', then we have either a template
13423 declaration, an explicit instantiation, or an explicit
13424 specialization. */
13425 else if (token1.keyword == RID_TEMPLATE)
13426 {
13427 /* `template <>' indicates a template specialization. */
13428 if (token2.type == CPP_LESS
13429 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13430 cp_parser_explicit_specialization (parser);
13431 /* `template <' indicates a template declaration. */
13432 else if (token2.type == CPP_LESS)
13433 cp_parser_template_declaration (parser, /*member_p=*/false);
13434 /* Anything else must be an explicit instantiation. */
13435 else
13436 cp_parser_explicit_instantiation (parser);
13437 }
13438 /* If the next token is `export', then we have a template
13439 declaration. */
13440 else if (token1.keyword == RID_EXPORT)
13441 cp_parser_template_declaration (parser, /*member_p=*/false);
13442 /* If the next token is `extern', 'static' or 'inline' and the one
13443 after that is `template', we have a GNU extended explicit
13444 instantiation directive. */
13445 else if (cp_parser_allow_gnu_extensions_p (parser)
13446 && (token1.keyword == RID_EXTERN
13447 || token1.keyword == RID_STATIC
13448 || token1.keyword == RID_INLINE)
13449 && token2.keyword == RID_TEMPLATE)
13450 cp_parser_explicit_instantiation (parser);
13451 /* If the next token is `namespace', check for a named or unnamed
13452 namespace definition. */
13453 else if (token1.keyword == RID_NAMESPACE
13454 && (/* A named namespace definition. */
13455 (token2.type == CPP_NAME
13456 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
13457 != CPP_EQ))
13458 || (token2.type == CPP_OPEN_SQUARE
13459 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
13460 == CPP_OPEN_SQUARE)
13461 /* An unnamed namespace definition. */
13462 || token2.type == CPP_OPEN_BRACE
13463 || token2.keyword == RID_ATTRIBUTE))
13464 cp_parser_namespace_definition (parser);
13465 /* An inline (associated) namespace definition. */
13466 else if (token1.keyword == RID_INLINE
13467 && token2.keyword == RID_NAMESPACE)
13468 cp_parser_namespace_definition (parser);
13469 /* Objective-C++ declaration/definition. */
13470 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
13471 cp_parser_objc_declaration (parser, NULL_TREE);
13472 else if (c_dialect_objc ()
13473 && token1.keyword == RID_ATTRIBUTE
13474 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
13475 cp_parser_objc_declaration (parser, attributes);
13476 /* At this point we may have a template declared by a concept
13477 introduction. */
13478 else if (flag_concepts
13479 && cp_parser_template_declaration_after_export (parser,
13480 /*member_p=*/false))
13481 /* We did. */;
13482 else
13483 /* Try to parse a block-declaration, or a function-definition. */
13484 cp_parser_block_declaration (parser, /*statement_p=*/false);
13485
13486 /* Free any declarators allocated. */
13487 obstack_free (&declarator_obstack, p);
13488 }
13489
13490 /* Parse a namespace-scope declaration. */
13491
13492 static void
13493 cp_parser_toplevel_declaration (cp_parser* parser)
13494 {
13495 cp_token *token = cp_lexer_peek_token (parser->lexer);
13496
13497 if (token->type == CPP_PRAGMA)
13498 /* A top-level declaration can consist solely of a #pragma. A
13499 nested declaration cannot, so this is done here and not in
13500 cp_parser_declaration. (A #pragma at block scope is
13501 handled in cp_parser_statement.) */
13502 cp_parser_pragma (parser, pragma_external, NULL);
13503 else if (token->type == CPP_SEMICOLON)
13504 {
13505 cp_lexer_consume_token (parser->lexer);
13506 /* A declaration consisting of a single semicolon is invalid
13507 * before C++11. Allow it unless we're being pedantic. */
13508 if (cxx_dialect < cxx11)
13509 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
13510 }
13511 else
13512 /* Parse the declaration itself. */
13513 cp_parser_declaration (parser);
13514 }
13515
13516 /* Parse a block-declaration.
13517
13518 block-declaration:
13519 simple-declaration
13520 asm-definition
13521 namespace-alias-definition
13522 using-declaration
13523 using-directive
13524
13525 GNU Extension:
13526
13527 block-declaration:
13528 __extension__ block-declaration
13529
13530 C++0x Extension:
13531
13532 block-declaration:
13533 static_assert-declaration
13534
13535 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13536 part of a declaration-statement. */
13537
13538 static void
13539 cp_parser_block_declaration (cp_parser *parser,
13540 bool statement_p)
13541 {
13542 cp_token *token1;
13543 int saved_pedantic;
13544
13545 /* Check for the `__extension__' keyword. */
13546 if (cp_parser_extension_opt (parser, &saved_pedantic))
13547 {
13548 /* Parse the qualified declaration. */
13549 cp_parser_block_declaration (parser, statement_p);
13550 /* Restore the PEDANTIC flag. */
13551 pedantic = saved_pedantic;
13552
13553 return;
13554 }
13555
13556 /* Peek at the next token to figure out which kind of declaration is
13557 present. */
13558 token1 = cp_lexer_peek_token (parser->lexer);
13559
13560 /* If the next keyword is `asm', we have an asm-definition. */
13561 if (token1->keyword == RID_ASM)
13562 {
13563 if (statement_p)
13564 cp_parser_commit_to_tentative_parse (parser);
13565 cp_parser_asm_definition (parser);
13566 }
13567 /* If the next keyword is `namespace', we have a
13568 namespace-alias-definition. */
13569 else if (token1->keyword == RID_NAMESPACE)
13570 cp_parser_namespace_alias_definition (parser);
13571 /* If the next keyword is `using', we have a
13572 using-declaration, a using-directive, or an alias-declaration. */
13573 else if (token1->keyword == RID_USING)
13574 {
13575 cp_token *token2;
13576
13577 if (statement_p)
13578 cp_parser_commit_to_tentative_parse (parser);
13579 /* If the token after `using' is `namespace', then we have a
13580 using-directive. */
13581 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13582 if (token2->keyword == RID_NAMESPACE)
13583 cp_parser_using_directive (parser);
13584 /* If the second token after 'using' is '=', then we have an
13585 alias-declaration. */
13586 else if (cxx_dialect >= cxx11
13587 && token2->type == CPP_NAME
13588 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13589 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13590 cp_parser_alias_declaration (parser);
13591 /* Otherwise, it's a using-declaration. */
13592 else
13593 cp_parser_using_declaration (parser,
13594 /*access_declaration_p=*/false);
13595 }
13596 /* If the next keyword is `__label__' we have a misplaced label
13597 declaration. */
13598 else if (token1->keyword == RID_LABEL)
13599 {
13600 cp_lexer_consume_token (parser->lexer);
13601 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13602 cp_parser_skip_to_end_of_statement (parser);
13603 /* If the next token is now a `;', consume it. */
13604 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13605 cp_lexer_consume_token (parser->lexer);
13606 }
13607 /* If the next token is `static_assert' we have a static assertion. */
13608 else if (token1->keyword == RID_STATIC_ASSERT)
13609 cp_parser_static_assert (parser, /*member_p=*/false);
13610 /* Anything else must be a simple-declaration. */
13611 else
13612 cp_parser_simple_declaration (parser, !statement_p,
13613 /*maybe_range_for_decl*/NULL);
13614 }
13615
13616 /* Parse a simple-declaration.
13617
13618 simple-declaration:
13619 decl-specifier-seq [opt] init-declarator-list [opt] ;
13620 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13621 brace-or-equal-initializer ;
13622
13623 init-declarator-list:
13624 init-declarator
13625 init-declarator-list , init-declarator
13626
13627 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13628 function-definition as a simple-declaration.
13629
13630 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13631 parsed declaration if it is an uninitialized single declarator not followed
13632 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13633 if present, will not be consumed. */
13634
13635 static void
13636 cp_parser_simple_declaration (cp_parser* parser,
13637 bool function_definition_allowed_p,
13638 tree *maybe_range_for_decl)
13639 {
13640 cp_decl_specifier_seq decl_specifiers;
13641 int declares_class_or_enum;
13642 bool saw_declarator;
13643 location_t comma_loc = UNKNOWN_LOCATION;
13644 location_t init_loc = UNKNOWN_LOCATION;
13645
13646 if (maybe_range_for_decl)
13647 *maybe_range_for_decl = NULL_TREE;
13648
13649 /* Defer access checks until we know what is being declared; the
13650 checks for names appearing in the decl-specifier-seq should be
13651 done as if we were in the scope of the thing being declared. */
13652 push_deferring_access_checks (dk_deferred);
13653
13654 /* Parse the decl-specifier-seq. We have to keep track of whether
13655 or not the decl-specifier-seq declares a named class or
13656 enumeration type, since that is the only case in which the
13657 init-declarator-list is allowed to be empty.
13658
13659 [dcl.dcl]
13660
13661 In a simple-declaration, the optional init-declarator-list can be
13662 omitted only when declaring a class or enumeration, that is when
13663 the decl-specifier-seq contains either a class-specifier, an
13664 elaborated-type-specifier, or an enum-specifier. */
13665 cp_parser_decl_specifier_seq (parser,
13666 CP_PARSER_FLAGS_OPTIONAL,
13667 &decl_specifiers,
13668 &declares_class_or_enum);
13669 /* We no longer need to defer access checks. */
13670 stop_deferring_access_checks ();
13671
13672 /* In a block scope, a valid declaration must always have a
13673 decl-specifier-seq. By not trying to parse declarators, we can
13674 resolve the declaration/expression ambiguity more quickly. */
13675 if (!function_definition_allowed_p
13676 && !decl_specifiers.any_specifiers_p)
13677 {
13678 cp_parser_error (parser, "expected declaration");
13679 goto done;
13680 }
13681
13682 /* If the next two tokens are both identifiers, the code is
13683 erroneous. The usual cause of this situation is code like:
13684
13685 T t;
13686
13687 where "T" should name a type -- but does not. */
13688 if (!decl_specifiers.any_type_specifiers_p
13689 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13690 {
13691 /* If parsing tentatively, we should commit; we really are
13692 looking at a declaration. */
13693 cp_parser_commit_to_tentative_parse (parser);
13694 /* Give up. */
13695 goto done;
13696 }
13697
13698 cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
13699
13700 /* Look for C++17 decomposition declaration. */
13701 for (size_t n = 1; ; n++)
13702 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13703 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13704 continue;
13705 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13706 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13707 && decl_specifiers.any_specifiers_p)
13708 {
13709 tree decl
13710 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13711 maybe_range_for_decl,
13712 &init_loc);
13713
13714 /* The next token should be either a `,' or a `;'. */
13715 cp_token *token = cp_lexer_peek_token (parser->lexer);
13716 /* If it's a `;', we are done. */
13717 if (token->type == CPP_SEMICOLON)
13718 goto finish;
13719 else if (maybe_range_for_decl)
13720 {
13721 if (*maybe_range_for_decl == NULL_TREE)
13722 *maybe_range_for_decl = error_mark_node;
13723 goto finish;
13724 }
13725 /* Anything else is an error. */
13726 else
13727 {
13728 /* If we have already issued an error message we don't need
13729 to issue another one. */
13730 if ((decl != error_mark_node
13731 && DECL_INITIAL (decl) != error_mark_node)
13732 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13733 cp_parser_error (parser, "expected %<;%>");
13734 /* Skip tokens until we reach the end of the statement. */
13735 cp_parser_skip_to_end_of_statement (parser);
13736 /* If the next token is now a `;', consume it. */
13737 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13738 cp_lexer_consume_token (parser->lexer);
13739 goto done;
13740 }
13741 }
13742 else
13743 break;
13744
13745 tree last_type;
13746 bool auto_specifier_p;
13747 /* NULL_TREE if both variable and function declaration are allowed,
13748 error_mark_node if function declaration are not allowed and
13749 a FUNCTION_DECL that should be diagnosed if it is followed by
13750 variable declarations. */
13751 tree auto_function_declaration;
13752
13753 last_type = NULL_TREE;
13754 auto_specifier_p
13755 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13756 auto_function_declaration = NULL_TREE;
13757
13758 /* Keep going until we hit the `;' at the end of the simple
13759 declaration. */
13760 saw_declarator = false;
13761 while (cp_lexer_next_token_is_not (parser->lexer,
13762 CPP_SEMICOLON))
13763 {
13764 cp_token *token;
13765 bool function_definition_p;
13766 tree decl;
13767 tree auto_result = NULL_TREE;
13768
13769 if (saw_declarator)
13770 {
13771 /* If we are processing next declarator, comma is expected */
13772 token = cp_lexer_peek_token (parser->lexer);
13773 gcc_assert (token->type == CPP_COMMA);
13774 cp_lexer_consume_token (parser->lexer);
13775 if (maybe_range_for_decl)
13776 {
13777 *maybe_range_for_decl = error_mark_node;
13778 if (comma_loc == UNKNOWN_LOCATION)
13779 comma_loc = token->location;
13780 }
13781 }
13782 else
13783 saw_declarator = true;
13784
13785 /* Parse the init-declarator. */
13786 decl = cp_parser_init_declarator (parser,
13787 CP_PARSER_FLAGS_NONE,
13788 &decl_specifiers,
13789 /*checks=*/NULL,
13790 function_definition_allowed_p,
13791 /*member_p=*/false,
13792 declares_class_or_enum,
13793 &function_definition_p,
13794 maybe_range_for_decl,
13795 &init_loc,
13796 &auto_result);
13797 /* If an error occurred while parsing tentatively, exit quickly.
13798 (That usually happens when in the body of a function; each
13799 statement is treated as a declaration-statement until proven
13800 otherwise.) */
13801 if (cp_parser_error_occurred (parser))
13802 goto done;
13803
13804 if (auto_specifier_p && cxx_dialect >= cxx14)
13805 {
13806 /* If the init-declarator-list contains more than one
13807 init-declarator, they shall all form declarations of
13808 variables. */
13809 if (auto_function_declaration == NULL_TREE)
13810 auto_function_declaration
13811 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13812 else if (TREE_CODE (decl) == FUNCTION_DECL
13813 || auto_function_declaration != error_mark_node)
13814 {
13815 error_at (decl_specifiers.locations[ds_type_spec],
13816 "non-variable %qD in declaration with more than one "
13817 "declarator with placeholder type",
13818 TREE_CODE (decl) == FUNCTION_DECL
13819 ? decl : auto_function_declaration);
13820 auto_function_declaration = error_mark_node;
13821 }
13822 }
13823
13824 if (auto_result
13825 && (!processing_template_decl || !type_uses_auto (auto_result)))
13826 {
13827 if (last_type
13828 && last_type != error_mark_node
13829 && !same_type_p (auto_result, last_type))
13830 {
13831 /* If the list of declarators contains more than one declarator,
13832 the type of each declared variable is determined as described
13833 above. If the type deduced for the template parameter U is not
13834 the same in each deduction, the program is ill-formed. */
13835 error_at (decl_specifiers.locations[ds_type_spec],
13836 "inconsistent deduction for %qT: %qT and then %qT",
13837 decl_specifiers.type, last_type, auto_result);
13838 last_type = error_mark_node;
13839 }
13840 else
13841 last_type = auto_result;
13842 }
13843
13844 /* Handle function definitions specially. */
13845 if (function_definition_p)
13846 {
13847 /* If the next token is a `,', then we are probably
13848 processing something like:
13849
13850 void f() {}, *p;
13851
13852 which is erroneous. */
13853 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13854 {
13855 cp_token *token = cp_lexer_peek_token (parser->lexer);
13856 error_at (token->location,
13857 "mixing"
13858 " declarations and function-definitions is forbidden");
13859 }
13860 /* Otherwise, we're done with the list of declarators. */
13861 else
13862 {
13863 pop_deferring_access_checks ();
13864 return;
13865 }
13866 }
13867 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13868 *maybe_range_for_decl = decl;
13869 /* The next token should be either a `,' or a `;'. */
13870 token = cp_lexer_peek_token (parser->lexer);
13871 /* If it's a `,', there are more declarators to come. */
13872 if (token->type == CPP_COMMA)
13873 /* will be consumed next time around */;
13874 /* If it's a `;', we are done. */
13875 else if (token->type == CPP_SEMICOLON)
13876 break;
13877 else if (maybe_range_for_decl)
13878 {
13879 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13880 permerror (decl_specifiers.locations[ds_type_spec],
13881 "types may not be defined in a for-range-declaration");
13882 break;
13883 }
13884 /* Anything else is an error. */
13885 else
13886 {
13887 /* If we have already issued an error message we don't need
13888 to issue another one. */
13889 if ((decl != error_mark_node
13890 && DECL_INITIAL (decl) != error_mark_node)
13891 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13892 cp_parser_error (parser, "expected %<,%> or %<;%>");
13893 /* Skip tokens until we reach the end of the statement. */
13894 cp_parser_skip_to_end_of_statement (parser);
13895 /* If the next token is now a `;', consume it. */
13896 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13897 cp_lexer_consume_token (parser->lexer);
13898 goto done;
13899 }
13900 /* After the first time around, a function-definition is not
13901 allowed -- even if it was OK at first. For example:
13902
13903 int i, f() {}
13904
13905 is not valid. */
13906 function_definition_allowed_p = false;
13907 }
13908
13909 /* Issue an error message if no declarators are present, and the
13910 decl-specifier-seq does not itself declare a class or
13911 enumeration: [dcl.dcl]/3. */
13912 if (!saw_declarator)
13913 {
13914 if (cp_parser_declares_only_class_p (parser))
13915 {
13916 if (!declares_class_or_enum
13917 && decl_specifiers.type
13918 && OVERLOAD_TYPE_P (decl_specifiers.type))
13919 /* Ensure an error is issued anyway when finish_decltype_type,
13920 called via cp_parser_decl_specifier_seq, returns a class or
13921 an enumeration (c++/51786). */
13922 decl_specifiers.type = NULL_TREE;
13923 shadow_tag (&decl_specifiers);
13924 }
13925 /* Perform any deferred access checks. */
13926 perform_deferred_access_checks (tf_warning_or_error);
13927 }
13928
13929 /* Consume the `;'. */
13930 finish:
13931 if (!maybe_range_for_decl)
13932 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13933 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13934 {
13935 if (init_loc != UNKNOWN_LOCATION)
13936 error_at (init_loc, "initializer in range-based %<for%> loop");
13937 if (comma_loc != UNKNOWN_LOCATION)
13938 error_at (comma_loc,
13939 "multiple declarations in range-based %<for%> loop");
13940 }
13941
13942 done:
13943 pop_deferring_access_checks ();
13944 }
13945
13946 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13947 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13948 initializer ; */
13949
13950 static tree
13951 cp_parser_decomposition_declaration (cp_parser *parser,
13952 cp_decl_specifier_seq *decl_specifiers,
13953 tree *maybe_range_for_decl,
13954 location_t *init_loc)
13955 {
13956 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13957 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13958 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13959
13960 /* Parse the identifier-list. */
13961 auto_vec<cp_expr, 10> v;
13962 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13963 while (true)
13964 {
13965 cp_expr e = cp_parser_identifier (parser);
13966 if (e.get_value () == error_mark_node)
13967 break;
13968 v.safe_push (e);
13969 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13970 break;
13971 cp_lexer_consume_token (parser->lexer);
13972 }
13973
13974 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13975 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13976 {
13977 end_loc = UNKNOWN_LOCATION;
13978 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13979 false);
13980 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13981 cp_lexer_consume_token (parser->lexer);
13982 else
13983 {
13984 cp_parser_skip_to_end_of_statement (parser);
13985 return error_mark_node;
13986 }
13987 }
13988
13989 if (cxx_dialect < cxx17)
13990 pedwarn (loc, 0, "structured bindings only available with "
13991 "%<-std=c++17%> or %<-std=gnu++17%>");
13992
13993 tree pushed_scope;
13994 cp_declarator *declarator = make_declarator (cdk_decomp);
13995 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13996 declarator->id_loc = loc;
13997 if (ref_qual != REF_QUAL_NONE)
13998 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13999 ref_qual == REF_QUAL_RVALUE,
14000 NULL_TREE);
14001 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
14002 NULL_TREE, decl_specifiers->attributes,
14003 &pushed_scope);
14004 tree orig_decl = decl;
14005
14006 unsigned int i;
14007 cp_expr e;
14008 cp_decl_specifier_seq decl_specs;
14009 clear_decl_specs (&decl_specs);
14010 decl_specs.type = make_auto ();
14011 tree prev = decl;
14012 FOR_EACH_VEC_ELT (v, i, e)
14013 {
14014 if (i == 0)
14015 declarator = make_id_declarator (NULL_TREE, e.get_value (),
14016 sfk_none, e.get_location ());
14017 else
14018 {
14019 declarator->u.id.unqualified_name = e.get_value ();
14020 declarator->id_loc = e.get_location ();
14021 }
14022 tree elt_pushed_scope;
14023 tree decl2 = start_decl (declarator, &decl_specs, SD_DECOMPOSITION,
14024 NULL_TREE, NULL_TREE, &elt_pushed_scope);
14025 if (decl2 == error_mark_node)
14026 decl = error_mark_node;
14027 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
14028 {
14029 /* Ensure we've diagnosed redeclaration if we aren't creating
14030 a new VAR_DECL. */
14031 gcc_assert (errorcount);
14032 decl = error_mark_node;
14033 }
14034 else
14035 prev = decl2;
14036 if (elt_pushed_scope)
14037 pop_scope (elt_pushed_scope);
14038 }
14039
14040 if (v.is_empty ())
14041 {
14042 error_at (loc, "empty structured binding declaration");
14043 decl = error_mark_node;
14044 }
14045
14046 if (maybe_range_for_decl == NULL
14047 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14048 {
14049 bool non_constant_p = false, is_direct_init = false;
14050 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
14051 tree initializer = cp_parser_initializer (parser, &is_direct_init,
14052 &non_constant_p);
14053 if (initializer == NULL_TREE
14054 || (TREE_CODE (initializer) == TREE_LIST
14055 && TREE_CHAIN (initializer))
14056 || (is_direct_init
14057 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
14058 && CONSTRUCTOR_NELTS (initializer) != 1))
14059 {
14060 error_at (loc, "invalid initializer for structured binding "
14061 "declaration");
14062 initializer = error_mark_node;
14063 }
14064
14065 if (decl != error_mark_node)
14066 {
14067 int flags = (decl_spec_seq_has_spec_p (decl_specifiers, ds_constinit)
14068 ? LOOKUP_CONSTINIT : 0);
14069 cp_maybe_mangle_decomp (decl, prev, v.length ());
14070 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
14071 (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT)
14072 | flags);
14073 cp_finish_decomp (decl, prev, v.length ());
14074 }
14075 }
14076 else if (decl != error_mark_node)
14077 {
14078 *maybe_range_for_decl = prev;
14079 /* Ensure DECL_VALUE_EXPR is created for all the decls but
14080 the underlying DECL. */
14081 cp_finish_decomp (decl, prev, v.length ());
14082 }
14083
14084 if (pushed_scope)
14085 pop_scope (pushed_scope);
14086
14087 if (decl == error_mark_node && DECL_P (orig_decl))
14088 {
14089 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
14090 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
14091 }
14092
14093 return decl;
14094 }
14095
14096 /* Parse a decl-specifier-seq.
14097
14098 decl-specifier-seq:
14099 decl-specifier-seq [opt] decl-specifier
14100 decl-specifier attribute-specifier-seq [opt] (C++11)
14101
14102 decl-specifier:
14103 storage-class-specifier
14104 type-specifier
14105 function-specifier
14106 friend
14107 typedef
14108
14109 GNU Extension:
14110
14111 decl-specifier:
14112 attributes
14113
14114 Concepts Extension:
14115
14116 decl-specifier:
14117 concept
14118
14119 Set *DECL_SPECS to a representation of the decl-specifier-seq.
14120
14121 The parser flags FLAGS is used to control type-specifier parsing.
14122
14123 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
14124 flags:
14125
14126 1: one of the decl-specifiers is an elaborated-type-specifier
14127 (i.e., a type declaration)
14128 2: one of the decl-specifiers is an enum-specifier or a
14129 class-specifier (i.e., a type definition)
14130
14131 */
14132
14133 static void
14134 cp_parser_decl_specifier_seq (cp_parser* parser,
14135 cp_parser_flags flags,
14136 cp_decl_specifier_seq *decl_specs,
14137 int* declares_class_or_enum)
14138 {
14139 bool constructor_possible_p = !parser->in_declarator_p;
14140 bool found_decl_spec = false;
14141 cp_token *start_token = NULL;
14142 cp_decl_spec ds;
14143
14144 /* Clear DECL_SPECS. */
14145 clear_decl_specs (decl_specs);
14146
14147 /* Assume no class or enumeration type is declared. */
14148 *declares_class_or_enum = 0;
14149
14150 /* Keep reading specifiers until there are no more to read. */
14151 while (true)
14152 {
14153 bool constructor_p;
14154 cp_token *token;
14155 ds = ds_last;
14156
14157 /* Peek at the next token. */
14158 token = cp_lexer_peek_token (parser->lexer);
14159
14160 /* Save the first token of the decl spec list for error
14161 reporting. */
14162 if (!start_token)
14163 start_token = token;
14164 /* Handle attributes. */
14165 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
14166 && cp_next_tokens_can_be_attribute_p (parser))
14167 {
14168 /* Parse the attributes. */
14169 tree attrs = cp_parser_attributes_opt (parser);
14170
14171 /* In a sequence of declaration specifiers, c++11 attributes
14172 appertain to the type that precede them. In that case
14173 [dcl.spec]/1 says:
14174
14175 The attribute-specifier-seq affects the type only for
14176 the declaration it appears in, not other declarations
14177 involving the same type.
14178
14179 But for now let's force the user to position the
14180 attribute either at the beginning of the declaration or
14181 after the declarator-id, which would clearly mean that it
14182 applies to the declarator. */
14183 if (cxx11_attribute_p (attrs))
14184 {
14185 if (!found_decl_spec)
14186 /* The c++11 attribute is at the beginning of the
14187 declaration. It appertains to the entity being
14188 declared. */;
14189 else
14190 {
14191 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
14192 {
14193 /* This is an attribute following a
14194 class-specifier. */
14195 if (decl_specs->type_definition_p)
14196 warn_misplaced_attr_for_class_type (token->location,
14197 decl_specs->type);
14198 attrs = NULL_TREE;
14199 }
14200 else
14201 {
14202 decl_specs->std_attributes
14203 = attr_chainon (decl_specs->std_attributes, attrs);
14204 if (decl_specs->locations[ds_std_attribute] == 0)
14205 decl_specs->locations[ds_std_attribute] = token->location;
14206 }
14207 continue;
14208 }
14209 }
14210
14211 decl_specs->attributes
14212 = attr_chainon (decl_specs->attributes, attrs);
14213 if (decl_specs->locations[ds_attribute] == 0)
14214 decl_specs->locations[ds_attribute] = token->location;
14215 continue;
14216 }
14217 /* Assume we will find a decl-specifier keyword. */
14218 found_decl_spec = true;
14219 /* If the next token is an appropriate keyword, we can simply
14220 add it to the list. */
14221 switch (token->keyword)
14222 {
14223 /* decl-specifier:
14224 friend
14225 constexpr
14226 constinit */
14227 case RID_FRIEND:
14228 if (!at_class_scope_p ())
14229 {
14230 gcc_rich_location richloc (token->location);
14231 richloc.add_fixit_remove ();
14232 error_at (&richloc, "%<friend%> used outside of class");
14233 cp_lexer_purge_token (parser->lexer);
14234 }
14235 else
14236 {
14237 ds = ds_friend;
14238 /* Consume the token. */
14239 cp_lexer_consume_token (parser->lexer);
14240 }
14241 break;
14242
14243 case RID_CONSTEXPR:
14244 ds = ds_constexpr;
14245 cp_lexer_consume_token (parser->lexer);
14246 break;
14247
14248 case RID_CONSTINIT:
14249 ds = ds_constinit;
14250 cp_lexer_consume_token (parser->lexer);
14251 break;
14252
14253 case RID_CONSTEVAL:
14254 ds = ds_consteval;
14255 cp_lexer_consume_token (parser->lexer);
14256 break;
14257
14258 case RID_CONCEPT:
14259 ds = ds_concept;
14260 cp_lexer_consume_token (parser->lexer);
14261
14262 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14263 break;
14264
14265 /* Warn for concept as a decl-specifier. We'll rewrite these as
14266 concept declarations later. */
14267 if (!flag_concepts_ts)
14268 {
14269 cp_token *next = cp_lexer_peek_token (parser->lexer);
14270 if (next->keyword == RID_BOOL)
14271 pedwarn (next->location, 0, "the %<bool%> keyword is not "
14272 "allowed in a C++20 concept definition");
14273 else
14274 pedwarn (token->location, 0, "C++20 concept definition syntax "
14275 "is %<concept <name> = <expr>%>");
14276 }
14277
14278 /* In C++20 a concept definition is just 'concept name = expr;'
14279 Support that syntax as a TS extension by pretending we've seen
14280 the 'bool' specifier. */
14281 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14282 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
14283 && !decl_specs->any_type_specifiers_p)
14284 {
14285 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
14286 token, /*type_definition*/false);
14287 decl_specs->any_type_specifiers_p = true;
14288 }
14289 break;
14290
14291 /* function-specifier:
14292 inline
14293 virtual
14294 explicit */
14295 case RID_INLINE:
14296 case RID_VIRTUAL:
14297 case RID_EXPLICIT:
14298 cp_parser_function_specifier_opt (parser, decl_specs);
14299 break;
14300
14301 /* decl-specifier:
14302 typedef */
14303 case RID_TYPEDEF:
14304 ds = ds_typedef;
14305 /* Consume the token. */
14306 cp_lexer_consume_token (parser->lexer);
14307
14308 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14309 break;
14310
14311 /* A constructor declarator cannot appear in a typedef. */
14312 constructor_possible_p = false;
14313 /* The "typedef" keyword can only occur in a declaration; we
14314 may as well commit at this point. */
14315 cp_parser_commit_to_tentative_parse (parser);
14316
14317 if (decl_specs->storage_class != sc_none)
14318 decl_specs->conflicting_specifiers_p = true;
14319 break;
14320
14321 /* storage-class-specifier:
14322 auto
14323 register
14324 static
14325 extern
14326 mutable
14327
14328 GNU Extension:
14329 thread */
14330 case RID_AUTO:
14331 if (cxx_dialect == cxx98)
14332 {
14333 /* Consume the token. */
14334 cp_lexer_consume_token (parser->lexer);
14335
14336 /* Complain about `auto' as a storage specifier, if
14337 we're complaining about C++0x compatibility. */
14338 gcc_rich_location richloc (token->location);
14339 richloc.add_fixit_remove ();
14340 warning_at (&richloc, OPT_Wc__11_compat,
14341 "%<auto%> changes meaning in C++11; "
14342 "please remove it");
14343
14344 /* Set the storage class anyway. */
14345 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
14346 token);
14347 }
14348 else
14349 /* C++0x auto type-specifier. */
14350 found_decl_spec = false;
14351 break;
14352
14353 case RID_REGISTER:
14354 case RID_STATIC:
14355 case RID_EXTERN:
14356 case RID_MUTABLE:
14357 /* Consume the token. */
14358 cp_lexer_consume_token (parser->lexer);
14359 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
14360 token);
14361 break;
14362 case RID_THREAD:
14363 /* Consume the token. */
14364 ds = ds_thread;
14365 cp_lexer_consume_token (parser->lexer);
14366 break;
14367
14368 default:
14369 /* We did not yet find a decl-specifier yet. */
14370 found_decl_spec = false;
14371 break;
14372 }
14373
14374 if (found_decl_spec
14375 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
14376 && token->keyword != RID_CONSTEXPR)
14377 error ("%<decl-specifier%> invalid in condition");
14378
14379 if (found_decl_spec
14380 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14381 && token->keyword != RID_MUTABLE
14382 && token->keyword != RID_CONSTEXPR
14383 && token->keyword != RID_CONSTEVAL)
14384 error_at (token->location, "%qD invalid in lambda",
14385 ridpointers[token->keyword]);
14386
14387 if (ds != ds_last)
14388 set_and_check_decl_spec_loc (decl_specs, ds, token);
14389
14390 /* Constructors are a special case. The `S' in `S()' is not a
14391 decl-specifier; it is the beginning of the declarator. */
14392 constructor_p
14393 = (!found_decl_spec
14394 && constructor_possible_p
14395 && (cp_parser_constructor_declarator_p
14396 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
14397 ds_friend))));
14398
14399 /* If we don't have a DECL_SPEC yet, then we must be looking at
14400 a type-specifier. */
14401 if (!found_decl_spec && !constructor_p)
14402 {
14403 int decl_spec_declares_class_or_enum;
14404 bool is_cv_qualifier;
14405 tree type_spec;
14406
14407 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14408 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14409
14410 type_spec
14411 = cp_parser_type_specifier (parser, flags,
14412 decl_specs,
14413 /*is_declaration=*/true,
14414 &decl_spec_declares_class_or_enum,
14415 &is_cv_qualifier);
14416 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
14417
14418 /* If this type-specifier referenced a user-defined type
14419 (a typedef, class-name, etc.), then we can't allow any
14420 more such type-specifiers henceforth.
14421
14422 [dcl.spec]
14423
14424 The longest sequence of decl-specifiers that could
14425 possibly be a type name is taken as the
14426 decl-specifier-seq of a declaration. The sequence shall
14427 be self-consistent as described below.
14428
14429 [dcl.type]
14430
14431 As a general rule, at most one type-specifier is allowed
14432 in the complete decl-specifier-seq of a declaration. The
14433 only exceptions are the following:
14434
14435 -- const or volatile can be combined with any other
14436 type-specifier.
14437
14438 -- signed or unsigned can be combined with char, long,
14439 short, or int.
14440
14441 -- ..
14442
14443 Example:
14444
14445 typedef char* Pc;
14446 void g (const int Pc);
14447
14448 Here, Pc is *not* part of the decl-specifier seq; it's
14449 the declarator. Therefore, once we see a type-specifier
14450 (other than a cv-qualifier), we forbid any additional
14451 user-defined types. We *do* still allow things like `int
14452 int' to be considered a decl-specifier-seq, and issue the
14453 error message later. */
14454 if (type_spec && !is_cv_qualifier)
14455 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14456 /* A constructor declarator cannot follow a type-specifier. */
14457 if (type_spec)
14458 {
14459 constructor_possible_p = false;
14460 found_decl_spec = true;
14461 if (!is_cv_qualifier)
14462 decl_specs->any_type_specifiers_p = true;
14463
14464 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
14465 error_at (token->location, "type-specifier invalid in lambda");
14466 }
14467 }
14468
14469 /* If we still do not have a DECL_SPEC, then there are no more
14470 decl-specifiers. */
14471 if (!found_decl_spec)
14472 break;
14473
14474 decl_specs->any_specifiers_p = true;
14475 /* After we see one decl-specifier, further decl-specifiers are
14476 always optional. */
14477 flags |= CP_PARSER_FLAGS_OPTIONAL;
14478 }
14479
14480 /* Don't allow a friend specifier with a class definition. */
14481 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
14482 && (*declares_class_or_enum & 2))
14483 error_at (decl_specs->locations[ds_friend],
14484 "class definition may not be declared a friend");
14485 }
14486
14487 /* Parse an (optional) storage-class-specifier.
14488
14489 storage-class-specifier:
14490 auto
14491 register
14492 static
14493 extern
14494 mutable
14495
14496 GNU Extension:
14497
14498 storage-class-specifier:
14499 thread
14500
14501 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14502
14503 static tree
14504 cp_parser_storage_class_specifier_opt (cp_parser* parser)
14505 {
14506 switch (cp_lexer_peek_token (parser->lexer)->keyword)
14507 {
14508 case RID_AUTO:
14509 if (cxx_dialect != cxx98)
14510 return NULL_TREE;
14511 /* Fall through for C++98. */
14512 gcc_fallthrough ();
14513
14514 case RID_REGISTER:
14515 case RID_STATIC:
14516 case RID_EXTERN:
14517 case RID_MUTABLE:
14518 case RID_THREAD:
14519 /* Consume the token. */
14520 return cp_lexer_consume_token (parser->lexer)->u.value;
14521
14522 default:
14523 return NULL_TREE;
14524 }
14525 }
14526
14527 /* Parse an (optional) function-specifier.
14528
14529 function-specifier:
14530 inline
14531 virtual
14532 explicit
14533
14534 C++20 Extension:
14535 explicit(constant-expression)
14536
14537 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14538 Updates DECL_SPECS, if it is non-NULL. */
14539
14540 static tree
14541 cp_parser_function_specifier_opt (cp_parser* parser,
14542 cp_decl_specifier_seq *decl_specs)
14543 {
14544 cp_token *token = cp_lexer_peek_token (parser->lexer);
14545 switch (token->keyword)
14546 {
14547 case RID_INLINE:
14548 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
14549 break;
14550
14551 case RID_VIRTUAL:
14552 /* 14.5.2.3 [temp.mem]
14553
14554 A member function template shall not be virtual. */
14555 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14556 && current_class_type)
14557 error_at (token->location, "templates may not be %<virtual%>");
14558 else
14559 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
14560 break;
14561
14562 case RID_EXPLICIT:
14563 {
14564 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
14565 /* If we see '(', it's C++20 explicit(bool). */
14566 tree expr;
14567 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14568 {
14569 matching_parens parens;
14570 parens.consume_open (parser);
14571
14572 /* New types are not allowed in an explicit-specifier. */
14573 const char *saved_message
14574 = parser->type_definition_forbidden_message;
14575 parser->type_definition_forbidden_message
14576 = G_("types may not be defined in explicit-specifier");
14577
14578 if (cxx_dialect < cxx20)
14579 pedwarn (token->location, 0,
14580 "%<explicit(bool)%> only available with %<-std=c++20%> "
14581 "or %<-std=gnu++20%>");
14582
14583 /* Parse the constant-expression. */
14584 expr = cp_parser_constant_expression (parser);
14585
14586 /* Restore the saved message. */
14587 parser->type_definition_forbidden_message = saved_message;
14588 parens.require_close (parser);
14589 }
14590 else
14591 /* The explicit-specifier explicit without a constant-expression is
14592 equivalent to the explicit-specifier explicit(true). */
14593 expr = boolean_true_node;
14594
14595 /* [dcl.fct.spec]
14596 "the constant-expression, if supplied, shall be a contextually
14597 converted constant expression of type bool." */
14598 expr = build_explicit_specifier (expr, tf_warning_or_error);
14599 /* We could evaluate it -- mark the decl as appropriate. */
14600 if (expr == boolean_true_node)
14601 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14602 else if (expr == boolean_false_node)
14603 /* Don't mark the decl as explicit. */;
14604 else if (decl_specs)
14605 /* The expression was value-dependent. Remember it so that we can
14606 substitute it later. */
14607 decl_specs->explicit_specifier = expr;
14608 return id;
14609 }
14610
14611 default:
14612 return NULL_TREE;
14613 }
14614
14615 /* Consume the token. */
14616 return cp_lexer_consume_token (parser->lexer)->u.value;
14617 }
14618
14619 /* Parse a linkage-specification.
14620
14621 linkage-specification:
14622 extern string-literal { declaration-seq [opt] }
14623 extern string-literal declaration */
14624
14625 static void
14626 cp_parser_linkage_specification (cp_parser* parser)
14627 {
14628 tree linkage;
14629
14630 /* Look for the `extern' keyword. */
14631 cp_token *extern_token
14632 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14633
14634 /* Look for the string-literal. */
14635 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14636 linkage = cp_parser_string_literal (parser, false, false);
14637
14638 /* Transform the literal into an identifier. If the literal is a
14639 wide-character string, or contains embedded NULs, then we can't
14640 handle it as the user wants. */
14641 if (strlen (TREE_STRING_POINTER (linkage))
14642 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14643 {
14644 cp_parser_error (parser, "invalid linkage-specification");
14645 /* Assume C++ linkage. */
14646 linkage = lang_name_cplusplus;
14647 }
14648 else
14649 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14650
14651 /* We're now using the new linkage. */
14652 push_lang_context (linkage);
14653
14654 /* Preserve the location of the innermost linkage specification,
14655 tracking the locations of nested specifications via a local. */
14656 location_t saved_location
14657 = parser->innermost_linkage_specification_location;
14658 /* Construct a location ranging from the start of the "extern" to
14659 the end of the string-literal, with the caret at the start, e.g.:
14660 extern "C" {
14661 ^~~~~~~~~~
14662 */
14663 parser->innermost_linkage_specification_location
14664 = make_location (extern_token->location,
14665 extern_token->location,
14666 get_finish (string_token->location));
14667
14668 /* If the next token is a `{', then we're using the first
14669 production. */
14670 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14671 {
14672 cp_ensure_no_omp_declare_simd (parser);
14673 cp_ensure_no_oacc_routine (parser);
14674
14675 /* Consume the `{' token. */
14676 matching_braces braces;
14677 braces.consume_open (parser);
14678 /* Parse the declarations. */
14679 cp_parser_declaration_seq_opt (parser);
14680 /* Look for the closing `}'. */
14681 braces.require_close (parser);
14682 }
14683 /* Otherwise, there's just one declaration. */
14684 else
14685 {
14686 bool saved_in_unbraced_linkage_specification_p;
14687
14688 saved_in_unbraced_linkage_specification_p
14689 = parser->in_unbraced_linkage_specification_p;
14690 parser->in_unbraced_linkage_specification_p = true;
14691 cp_parser_declaration (parser);
14692 parser->in_unbraced_linkage_specification_p
14693 = saved_in_unbraced_linkage_specification_p;
14694 }
14695
14696 /* We're done with the linkage-specification. */
14697 pop_lang_context ();
14698
14699 /* Restore location of parent linkage specification, if any. */
14700 parser->innermost_linkage_specification_location = saved_location;
14701 }
14702
14703 /* Parse a static_assert-declaration.
14704
14705 static_assert-declaration:
14706 static_assert ( constant-expression , string-literal ) ;
14707 static_assert ( constant-expression ) ; (C++17)
14708
14709 If MEMBER_P, this static_assert is a class member. */
14710
14711 static void
14712 cp_parser_static_assert(cp_parser *parser, bool member_p)
14713 {
14714 cp_expr condition;
14715 location_t token_loc;
14716 tree message;
14717 bool dummy;
14718
14719 /* Peek at the `static_assert' token so we can keep track of exactly
14720 where the static assertion started. */
14721 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14722
14723 /* Look for the `static_assert' keyword. */
14724 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14725 RT_STATIC_ASSERT))
14726 return;
14727
14728 /* We know we are in a static assertion; commit to any tentative
14729 parse. */
14730 if (cp_parser_parsing_tentatively (parser))
14731 cp_parser_commit_to_tentative_parse (parser);
14732
14733 /* Parse the `(' starting the static assertion condition. */
14734 matching_parens parens;
14735 parens.require_open (parser);
14736
14737 /* Parse the constant-expression. Allow a non-constant expression
14738 here in order to give better diagnostics in finish_static_assert. */
14739 condition =
14740 cp_parser_constant_expression (parser,
14741 /*allow_non_constant_p=*/true,
14742 /*non_constant_p=*/&dummy);
14743
14744 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14745 {
14746 if (cxx_dialect < cxx17)
14747 pedwarn (input_location, OPT_Wpedantic,
14748 "%<static_assert%> without a message "
14749 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
14750 /* Eat the ')' */
14751 cp_lexer_consume_token (parser->lexer);
14752 message = build_string (1, "");
14753 TREE_TYPE (message) = char_array_type_node;
14754 fix_string_type (message);
14755 }
14756 else
14757 {
14758 /* Parse the separating `,'. */
14759 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14760
14761 /* Parse the string-literal message. */
14762 message = cp_parser_string_literal (parser,
14763 /*translate=*/false,
14764 /*wide_ok=*/true);
14765
14766 /* A `)' completes the static assertion. */
14767 if (!parens.require_close (parser))
14768 cp_parser_skip_to_closing_parenthesis (parser,
14769 /*recovering=*/true,
14770 /*or_comma=*/false,
14771 /*consume_paren=*/true);
14772 }
14773
14774 /* A semicolon terminates the declaration. */
14775 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14776
14777 /* Get the location for the static assertion. Use that of the
14778 condition if available, otherwise, use that of the "static_assert"
14779 token. */
14780 location_t assert_loc = condition.get_location ();
14781 if (assert_loc == UNKNOWN_LOCATION)
14782 assert_loc = token_loc;
14783
14784 /* Complete the static assertion, which may mean either processing
14785 the static assert now or saving it for template instantiation. */
14786 finish_static_assert (condition, message, assert_loc, member_p);
14787 }
14788
14789 /* Parse the expression in decltype ( expression ). */
14790
14791 static tree
14792 cp_parser_decltype_expr (cp_parser *parser,
14793 bool &id_expression_or_member_access_p)
14794 {
14795 cp_token *id_expr_start_token;
14796 tree expr;
14797
14798 /* First, try parsing an id-expression. */
14799 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14800 cp_parser_parse_tentatively (parser);
14801 expr = cp_parser_id_expression (parser,
14802 /*template_keyword_p=*/false,
14803 /*check_dependency_p=*/true,
14804 /*template_p=*/NULL,
14805 /*declarator_p=*/false,
14806 /*optional_p=*/false);
14807
14808 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14809 {
14810 bool non_integral_constant_expression_p = false;
14811 tree id_expression = expr;
14812 cp_id_kind idk;
14813 const char *error_msg;
14814
14815 if (identifier_p (expr))
14816 /* Lookup the name we got back from the id-expression. */
14817 expr = cp_parser_lookup_name_simple (parser, expr,
14818 id_expr_start_token->location);
14819
14820 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14821 /* A template without args is not a complete id-expression. */
14822 expr = error_mark_node;
14823
14824 if (expr
14825 && expr != error_mark_node
14826 && TREE_CODE (expr) != TYPE_DECL
14827 && (TREE_CODE (expr) != BIT_NOT_EXPR
14828 || !TYPE_P (TREE_OPERAND (expr, 0)))
14829 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14830 {
14831 /* Complete lookup of the id-expression. */
14832 expr = (finish_id_expression
14833 (id_expression, expr, parser->scope, &idk,
14834 /*integral_constant_expression_p=*/false,
14835 /*allow_non_integral_constant_expression_p=*/true,
14836 &non_integral_constant_expression_p,
14837 /*template_p=*/false,
14838 /*done=*/true,
14839 /*address_p=*/false,
14840 /*template_arg_p=*/false,
14841 &error_msg,
14842 id_expr_start_token->location));
14843
14844 if (expr == error_mark_node)
14845 /* We found an id-expression, but it was something that we
14846 should not have found. This is an error, not something
14847 we can recover from, so note that we found an
14848 id-expression and we'll recover as gracefully as
14849 possible. */
14850 id_expression_or_member_access_p = true;
14851 }
14852
14853 if (expr
14854 && expr != error_mark_node
14855 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14856 /* We have an id-expression. */
14857 id_expression_or_member_access_p = true;
14858 }
14859
14860 if (!id_expression_or_member_access_p)
14861 {
14862 /* Abort the id-expression parse. */
14863 cp_parser_abort_tentative_parse (parser);
14864
14865 /* Parsing tentatively, again. */
14866 cp_parser_parse_tentatively (parser);
14867
14868 /* Parse a class member access. */
14869 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14870 /*cast_p=*/false, /*decltype*/true,
14871 /*member_access_only_p=*/true, NULL);
14872
14873 if (expr
14874 && expr != error_mark_node
14875 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14876 /* We have an id-expression. */
14877 id_expression_or_member_access_p = true;
14878 }
14879
14880 if (id_expression_or_member_access_p)
14881 /* We have parsed the complete id-expression or member access. */
14882 cp_parser_parse_definitely (parser);
14883 else
14884 {
14885 /* Abort our attempt to parse an id-expression or member access
14886 expression. */
14887 cp_parser_abort_tentative_parse (parser);
14888
14889 /* Parse a full expression. */
14890 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14891 /*decltype_p=*/true);
14892 }
14893
14894 return expr;
14895 }
14896
14897 /* Parse a `decltype' type. Returns the type.
14898
14899 decltype-specifier:
14900 decltype ( expression )
14901 C++14:
14902 decltype ( auto ) */
14903
14904 static tree
14905 cp_parser_decltype (cp_parser *parser)
14906 {
14907 bool id_expression_or_member_access_p = false;
14908 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14909
14910 if (start_token->type == CPP_DECLTYPE)
14911 {
14912 /* Already parsed. */
14913 cp_lexer_consume_token (parser->lexer);
14914 return saved_checks_value (start_token->u.tree_check_value);
14915 }
14916
14917 /* Look for the `decltype' token. */
14918 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14919 return error_mark_node;
14920
14921 /* Parse the opening `('. */
14922 matching_parens parens;
14923 if (!parens.require_open (parser))
14924 return error_mark_node;
14925
14926 /* Since we're going to preserve any side-effects from this parse, set up a
14927 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14928 in the expression. */
14929 tentative_firewall firewall (parser);
14930
14931 /* If in_declarator_p, a reparse as an expression might succeed (60361).
14932 Otherwise, commit now for better diagnostics. */
14933 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
14934 && !parser->in_declarator_p)
14935 cp_parser_commit_to_topmost_tentative_parse (parser);
14936
14937 push_deferring_access_checks (dk_deferred);
14938
14939 tree expr = NULL_TREE;
14940
14941 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)
14942 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
14943 {
14944 /* decltype (auto) */
14945 cp_lexer_consume_token (parser->lexer);
14946 if (cxx_dialect < cxx14)
14947 {
14948 error_at (start_token->location,
14949 "%<decltype(auto)%> type specifier only available with "
14950 "%<-std=c++14%> or %<-std=gnu++14%>");
14951 expr = error_mark_node;
14952 }
14953 }
14954 else
14955 {
14956 /* decltype (expression) */
14957
14958 /* Types cannot be defined in a `decltype' expression. Save away the
14959 old message and set the new one. */
14960 const char *saved_message = parser->type_definition_forbidden_message;
14961 parser->type_definition_forbidden_message
14962 = G_("types may not be defined in %<decltype%> expressions");
14963
14964 /* The restrictions on constant-expressions do not apply inside
14965 decltype expressions. */
14966 bool saved_integral_constant_expression_p
14967 = parser->integral_constant_expression_p;
14968 bool saved_non_integral_constant_expression_p
14969 = parser->non_integral_constant_expression_p;
14970 parser->integral_constant_expression_p = false;
14971
14972 /* Within a parenthesized expression, a `>' token is always
14973 the greater-than operator. */
14974 bool saved_greater_than_is_operator_p
14975 = parser->greater_than_is_operator_p;
14976 parser->greater_than_is_operator_p = true;
14977
14978 /* Do not actually evaluate the expression. */
14979 ++cp_unevaluated_operand;
14980
14981 /* Do not warn about problems with the expression. */
14982 ++c_inhibit_evaluation_warnings;
14983
14984 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14985 STRIP_ANY_LOCATION_WRAPPER (expr);
14986
14987 /* Go back to evaluating expressions. */
14988 --cp_unevaluated_operand;
14989 --c_inhibit_evaluation_warnings;
14990
14991 /* The `>' token might be the end of a template-id or
14992 template-parameter-list now. */
14993 parser->greater_than_is_operator_p
14994 = saved_greater_than_is_operator_p;
14995
14996 /* Restore the old message and the integral constant expression
14997 flags. */
14998 parser->type_definition_forbidden_message = saved_message;
14999 parser->integral_constant_expression_p
15000 = saved_integral_constant_expression_p;
15001 parser->non_integral_constant_expression_p
15002 = saved_non_integral_constant_expression_p;
15003 }
15004
15005 /* Parse to the closing `)'. */
15006 if (expr == error_mark_node || !parens.require_close (parser))
15007 {
15008 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15009 /*consume_paren=*/true);
15010 expr = error_mark_node;
15011 }
15012
15013 /* If we got a parse error while tentative, bail out now. */
15014 if (cp_parser_error_occurred (parser))
15015 {
15016 pop_deferring_access_checks ();
15017 return error_mark_node;
15018 }
15019
15020 if (!expr)
15021 /* Build auto. */
15022 expr = make_decltype_auto ();
15023 else
15024 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
15025 tf_warning_or_error);
15026
15027 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
15028 it again. */
15029 start_token->type = CPP_DECLTYPE;
15030 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15031 start_token->tree_check_p = true;
15032 start_token->u.tree_check_value->value = expr;
15033 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
15034 start_token->keyword = RID_MAX;
15035
15036 location_t loc = start_token->location;
15037 loc = make_location (loc, loc, parser->lexer);
15038 start_token->location = loc;
15039
15040 cp_lexer_purge_tokens_after (parser->lexer, start_token);
15041
15042 pop_to_parent_deferring_access_checks ();
15043
15044 return expr;
15045 }
15046
15047 /* Special member functions [gram.special] */
15048
15049 /* Parse a conversion-function-id.
15050
15051 conversion-function-id:
15052 operator conversion-type-id
15053
15054 Returns an IDENTIFIER_NODE representing the operator. */
15055
15056 static tree
15057 cp_parser_conversion_function_id (cp_parser* parser)
15058 {
15059 tree type;
15060 tree saved_scope;
15061 tree saved_qualifying_scope;
15062 tree saved_object_scope;
15063 tree pushed_scope = NULL_TREE;
15064
15065 /* Look for the `operator' token. */
15066 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15067 return error_mark_node;
15068 /* When we parse the conversion-type-id, the current scope will be
15069 reset. However, we need that information in able to look up the
15070 conversion function later, so we save it here. */
15071 saved_scope = parser->scope;
15072 saved_qualifying_scope = parser->qualifying_scope;
15073 saved_object_scope = parser->object_scope;
15074 /* We must enter the scope of the class so that the names of
15075 entities declared within the class are available in the
15076 conversion-type-id. For example, consider:
15077
15078 struct S {
15079 typedef int I;
15080 operator I();
15081 };
15082
15083 S::operator I() { ... }
15084
15085 In order to see that `I' is a type-name in the definition, we
15086 must be in the scope of `S'. */
15087 if (saved_scope)
15088 pushed_scope = push_scope (saved_scope);
15089 /* Parse the conversion-type-id. */
15090 type = cp_parser_conversion_type_id (parser);
15091 /* Leave the scope of the class, if any. */
15092 if (pushed_scope)
15093 pop_scope (pushed_scope);
15094 /* Restore the saved scope. */
15095 parser->scope = saved_scope;
15096 parser->qualifying_scope = saved_qualifying_scope;
15097 parser->object_scope = saved_object_scope;
15098 /* If the TYPE is invalid, indicate failure. */
15099 if (type == error_mark_node)
15100 return error_mark_node;
15101 return make_conv_op_name (type);
15102 }
15103
15104 /* Parse a conversion-type-id:
15105
15106 conversion-type-id:
15107 type-specifier-seq conversion-declarator [opt]
15108
15109 Returns the TYPE specified. */
15110
15111 static tree
15112 cp_parser_conversion_type_id (cp_parser* parser)
15113 {
15114 tree attributes;
15115 cp_decl_specifier_seq type_specifiers;
15116 cp_declarator *declarator;
15117 tree type_specified;
15118 const char *saved_message;
15119
15120 /* Parse the attributes. */
15121 attributes = cp_parser_attributes_opt (parser);
15122
15123 saved_message = parser->type_definition_forbidden_message;
15124 parser->type_definition_forbidden_message
15125 = G_("types may not be defined in a conversion-type-id");
15126
15127 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
15128 optional in conversion-type-id. */
15129 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15130 /*is_declaration=*/false,
15131 /*is_trailing_return=*/false,
15132 &type_specifiers);
15133
15134 parser->type_definition_forbidden_message = saved_message;
15135
15136 /* If that didn't work, stop. */
15137 if (type_specifiers.type == error_mark_node)
15138 return error_mark_node;
15139 /* Parse the conversion-declarator. */
15140 declarator = cp_parser_conversion_declarator_opt (parser);
15141
15142 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
15143 /*initialized=*/0, &attributes);
15144 if (attributes)
15145 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
15146
15147 /* Don't give this error when parsing tentatively. This happens to
15148 work because we always parse this definitively once. */
15149 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
15150 && type_uses_auto (type_specified))
15151 {
15152 if (cxx_dialect < cxx14)
15153 {
15154 error ("invalid use of %<auto%> in conversion operator");
15155 return error_mark_node;
15156 }
15157 else if (template_parm_scope_p ())
15158 warning (0, "use of %<auto%> in member template "
15159 "conversion operator can never be deduced");
15160 }
15161
15162 return type_specified;
15163 }
15164
15165 /* Parse an (optional) conversion-declarator.
15166
15167 conversion-declarator:
15168 ptr-operator conversion-declarator [opt]
15169
15170 */
15171
15172 static cp_declarator *
15173 cp_parser_conversion_declarator_opt (cp_parser* parser)
15174 {
15175 enum tree_code code;
15176 tree class_type, std_attributes = NULL_TREE;
15177 cp_cv_quals cv_quals;
15178
15179 /* We don't know if there's a ptr-operator next, or not. */
15180 cp_parser_parse_tentatively (parser);
15181 /* Try the ptr-operator. */
15182 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
15183 &std_attributes);
15184 /* If it worked, look for more conversion-declarators. */
15185 if (cp_parser_parse_definitely (parser))
15186 {
15187 cp_declarator *declarator;
15188
15189 /* Parse another optional declarator. */
15190 declarator = cp_parser_conversion_declarator_opt (parser);
15191
15192 declarator = cp_parser_make_indirect_declarator
15193 (code, class_type, cv_quals, declarator, std_attributes);
15194
15195 return declarator;
15196 }
15197
15198 return NULL;
15199 }
15200
15201 /* Parse an (optional) ctor-initializer.
15202
15203 ctor-initializer:
15204 : mem-initializer-list */
15205
15206 static void
15207 cp_parser_ctor_initializer_opt (cp_parser* parser)
15208 {
15209 /* If the next token is not a `:', then there is no
15210 ctor-initializer. */
15211 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
15212 {
15213 /* Do default initialization of any bases and members. */
15214 if (DECL_CONSTRUCTOR_P (current_function_decl))
15215 finish_mem_initializers (NULL_TREE);
15216 return;
15217 }
15218
15219 /* Consume the `:' token. */
15220 cp_lexer_consume_token (parser->lexer);
15221 /* And the mem-initializer-list. */
15222 cp_parser_mem_initializer_list (parser);
15223 }
15224
15225 /* Parse a mem-initializer-list.
15226
15227 mem-initializer-list:
15228 mem-initializer ... [opt]
15229 mem-initializer ... [opt] , mem-initializer-list */
15230
15231 static void
15232 cp_parser_mem_initializer_list (cp_parser* parser)
15233 {
15234 tree mem_initializer_list = NULL_TREE;
15235 tree target_ctor = error_mark_node;
15236 cp_token *token = cp_lexer_peek_token (parser->lexer);
15237
15238 /* Let the semantic analysis code know that we are starting the
15239 mem-initializer-list. */
15240 if (!DECL_CONSTRUCTOR_P (current_function_decl))
15241 error_at (token->location,
15242 "only constructors take member initializers");
15243
15244 /* Loop through the list. */
15245 while (true)
15246 {
15247 tree mem_initializer;
15248
15249 token = cp_lexer_peek_token (parser->lexer);
15250 /* Parse the mem-initializer. */
15251 mem_initializer = cp_parser_mem_initializer (parser);
15252 /* If the next token is a `...', we're expanding member initializers. */
15253 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15254 if (ellipsis
15255 || (mem_initializer != error_mark_node
15256 && check_for_bare_parameter_packs (TREE_PURPOSE
15257 (mem_initializer))))
15258 {
15259 /* Consume the `...'. */
15260 if (ellipsis)
15261 cp_lexer_consume_token (parser->lexer);
15262
15263 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
15264 can be expanded but members cannot. */
15265 if (mem_initializer != error_mark_node
15266 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
15267 {
15268 error_at (token->location,
15269 "cannot expand initializer for member %qD",
15270 TREE_PURPOSE (mem_initializer));
15271 mem_initializer = error_mark_node;
15272 }
15273
15274 /* Construct the pack expansion type. */
15275 if (mem_initializer != error_mark_node)
15276 mem_initializer = make_pack_expansion (mem_initializer);
15277 }
15278 if (target_ctor != error_mark_node
15279 && mem_initializer != error_mark_node)
15280 {
15281 error ("mem-initializer for %qD follows constructor delegation",
15282 TREE_PURPOSE (mem_initializer));
15283 mem_initializer = error_mark_node;
15284 }
15285 /* Look for a target constructor. */
15286 if (mem_initializer != error_mark_node
15287 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
15288 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
15289 {
15290 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
15291 if (mem_initializer_list)
15292 {
15293 error ("constructor delegation follows mem-initializer for %qD",
15294 TREE_PURPOSE (mem_initializer_list));
15295 mem_initializer = error_mark_node;
15296 }
15297 target_ctor = mem_initializer;
15298 }
15299 /* Add it to the list, unless it was erroneous. */
15300 if (mem_initializer != error_mark_node)
15301 {
15302 TREE_CHAIN (mem_initializer) = mem_initializer_list;
15303 mem_initializer_list = mem_initializer;
15304 }
15305 /* If the next token is not a `,', we're done. */
15306 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15307 break;
15308 /* Consume the `,' token. */
15309 cp_lexer_consume_token (parser->lexer);
15310 }
15311
15312 /* Perform semantic analysis. */
15313 if (DECL_CONSTRUCTOR_P (current_function_decl))
15314 finish_mem_initializers (mem_initializer_list);
15315 }
15316
15317 /* Parse a mem-initializer.
15318
15319 mem-initializer:
15320 mem-initializer-id ( expression-list [opt] )
15321 mem-initializer-id braced-init-list
15322
15323 GNU extension:
15324
15325 mem-initializer:
15326 ( expression-list [opt] )
15327
15328 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
15329 class) or FIELD_DECL (for a non-static data member) to initialize;
15330 the TREE_VALUE is the expression-list. An empty initialization
15331 list is represented by void_list_node. */
15332
15333 static tree
15334 cp_parser_mem_initializer (cp_parser* parser)
15335 {
15336 tree mem_initializer_id;
15337 tree expression_list;
15338 tree member;
15339 cp_token *token = cp_lexer_peek_token (parser->lexer);
15340
15341 /* Find out what is being initialized. */
15342 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15343 {
15344 permerror (token->location,
15345 "anachronistic old-style base class initializer");
15346 mem_initializer_id = NULL_TREE;
15347 }
15348 else
15349 {
15350 mem_initializer_id = cp_parser_mem_initializer_id (parser);
15351 if (mem_initializer_id == error_mark_node)
15352 return mem_initializer_id;
15353 }
15354 member = expand_member_init (mem_initializer_id);
15355 if (member && !DECL_P (member))
15356 in_base_initializer = 1;
15357
15358 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15359 {
15360 bool expr_non_constant_p;
15361 cp_lexer_set_source_position (parser->lexer);
15362 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15363 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
15364 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
15365 expression_list = build_tree_list (NULL_TREE, expression_list);
15366 }
15367 else
15368 {
15369 vec<tree, va_gc> *vec;
15370 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15371 /*cast_p=*/false,
15372 /*allow_expansion_p=*/true,
15373 /*non_constant_p=*/NULL,
15374 /*close_paren_loc=*/NULL,
15375 /*wrap_locations_p=*/true);
15376 if (vec == NULL)
15377 return error_mark_node;
15378 expression_list = build_tree_list_vec (vec);
15379 release_tree_vector (vec);
15380 }
15381
15382 if (expression_list == error_mark_node)
15383 return error_mark_node;
15384 if (!expression_list)
15385 expression_list = void_type_node;
15386
15387 in_base_initializer = 0;
15388
15389 return member ? build_tree_list (member, expression_list) : error_mark_node;
15390 }
15391
15392 /* Parse a mem-initializer-id.
15393
15394 mem-initializer-id:
15395 :: [opt] nested-name-specifier [opt] class-name
15396 decltype-specifier (C++11)
15397 identifier
15398
15399 Returns a TYPE indicating the class to be initialized for the first
15400 production (and the second in C++11). Returns an IDENTIFIER_NODE
15401 indicating the data member to be initialized for the last production. */
15402
15403 static tree
15404 cp_parser_mem_initializer_id (cp_parser* parser)
15405 {
15406 bool global_scope_p;
15407 bool nested_name_specifier_p;
15408 bool template_p = false;
15409 tree id;
15410
15411 cp_token *token = cp_lexer_peek_token (parser->lexer);
15412
15413 /* `typename' is not allowed in this context ([temp.res]). */
15414 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15415 {
15416 error_at (token->location,
15417 "keyword %<typename%> not allowed in this context (a qualified "
15418 "member initializer is implicitly a type)");
15419 cp_lexer_consume_token (parser->lexer);
15420 }
15421 /* Look for the optional `::' operator. */
15422 global_scope_p
15423 = (cp_parser_global_scope_opt (parser,
15424 /*current_scope_valid_p=*/false)
15425 != NULL_TREE);
15426 /* Look for the optional nested-name-specifier. The simplest way to
15427 implement:
15428
15429 [temp.res]
15430
15431 The keyword `typename' is not permitted in a base-specifier or
15432 mem-initializer; in these contexts a qualified name that
15433 depends on a template-parameter is implicitly assumed to be a
15434 type name.
15435
15436 is to assume that we have seen the `typename' keyword at this
15437 point. */
15438 nested_name_specifier_p
15439 = (cp_parser_nested_name_specifier_opt (parser,
15440 /*typename_keyword_p=*/true,
15441 /*check_dependency_p=*/true,
15442 /*type_p=*/true,
15443 /*is_declaration=*/true)
15444 != NULL_TREE);
15445 if (nested_name_specifier_p)
15446 template_p = cp_parser_optional_template_keyword (parser);
15447 /* If there is a `::' operator or a nested-name-specifier, then we
15448 are definitely looking for a class-name. */
15449 if (global_scope_p || nested_name_specifier_p)
15450 return cp_parser_class_name (parser,
15451 /*typename_keyword_p=*/true,
15452 /*template_keyword_p=*/template_p,
15453 typename_type,
15454 /*check_dependency_p=*/true,
15455 /*class_head_p=*/false,
15456 /*is_declaration=*/true);
15457 /* Otherwise, we could also be looking for an ordinary identifier. */
15458 cp_parser_parse_tentatively (parser);
15459 if (cp_lexer_next_token_is_decltype (parser->lexer))
15460 /* Try a decltype-specifier. */
15461 id = cp_parser_decltype (parser);
15462 else
15463 /* Otherwise, try a class-name. */
15464 id = cp_parser_class_name (parser,
15465 /*typename_keyword_p=*/true,
15466 /*template_keyword_p=*/false,
15467 none_type,
15468 /*check_dependency_p=*/true,
15469 /*class_head_p=*/false,
15470 /*is_declaration=*/true);
15471 /* If we found one, we're done. */
15472 if (cp_parser_parse_definitely (parser))
15473 return id;
15474 /* Otherwise, look for an ordinary identifier. */
15475 return cp_parser_identifier (parser);
15476 }
15477
15478 /* Overloading [gram.over] */
15479
15480 /* Parse an operator-function-id.
15481
15482 operator-function-id:
15483 operator operator
15484
15485 Returns an IDENTIFIER_NODE for the operator which is a
15486 human-readable spelling of the identifier, e.g., `operator +'. */
15487
15488 static cp_expr
15489 cp_parser_operator_function_id (cp_parser* parser)
15490 {
15491 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
15492 /* Look for the `operator' keyword. */
15493 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15494 return error_mark_node;
15495 /* And then the name of the operator itself. */
15496 return cp_parser_operator (parser, start_loc);
15497 }
15498
15499 /* Return an identifier node for a user-defined literal operator.
15500 The suffix identifier is chained to the operator name identifier. */
15501
15502 tree
15503 cp_literal_operator_id (const char* name)
15504 {
15505 tree identifier;
15506 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
15507 + strlen (name) + 10);
15508 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
15509 identifier = get_identifier (buffer);
15510 XDELETEVEC (buffer);
15511
15512 return identifier;
15513 }
15514
15515 /* Parse an operator.
15516
15517 operator:
15518 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15519 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15520 || ++ -- , ->* -> () []
15521
15522 GNU Extensions:
15523
15524 operator:
15525 <? >? <?= >?=
15526
15527 Returns an IDENTIFIER_NODE for the operator which is a
15528 human-readable spelling of the identifier, e.g., `operator +'. */
15529
15530 static cp_expr
15531 cp_parser_operator (cp_parser* parser, location_t start_loc)
15532 {
15533 tree id = NULL_TREE;
15534 cp_token *token;
15535 bool utf8 = false;
15536
15537 /* Peek at the next token. */
15538 token = cp_lexer_peek_token (parser->lexer);
15539
15540 location_t end_loc = token->location;
15541
15542 /* Figure out which operator we have. */
15543 enum tree_code op = ERROR_MARK;
15544 bool assop = false;
15545 bool consumed = false;
15546 switch (token->type)
15547 {
15548 case CPP_KEYWORD:
15549 {
15550 /* The keyword should be either `new', `delete' or `co_await'. */
15551 if (token->keyword == RID_NEW)
15552 op = NEW_EXPR;
15553 else if (token->keyword == RID_DELETE)
15554 op = DELETE_EXPR;
15555 else if (token->keyword == RID_CO_AWAIT)
15556 op = CO_AWAIT_EXPR;
15557 else
15558 break;
15559
15560 /* Consume the `new', `delete' or co_await token. */
15561 end_loc = cp_lexer_consume_token (parser->lexer)->location;
15562
15563 /* Peek at the next token. */
15564 token = cp_lexer_peek_token (parser->lexer);
15565 /* If it's a `[' token then this is the array variant of the
15566 operator. */
15567 if (token->type == CPP_OPEN_SQUARE
15568 && op != CO_AWAIT_EXPR)
15569 {
15570 /* Consume the `[' token. */
15571 cp_lexer_consume_token (parser->lexer);
15572 /* Look for the `]' token. */
15573 if (cp_token *close_token
15574 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15575 end_loc = close_token->location;
15576 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
15577 }
15578 consumed = true;
15579 break;
15580 }
15581
15582 case CPP_PLUS:
15583 op = PLUS_EXPR;
15584 break;
15585
15586 case CPP_MINUS:
15587 op = MINUS_EXPR;
15588 break;
15589
15590 case CPP_MULT:
15591 op = MULT_EXPR;
15592 break;
15593
15594 case CPP_DIV:
15595 op = TRUNC_DIV_EXPR;
15596 break;
15597
15598 case CPP_MOD:
15599 op = TRUNC_MOD_EXPR;
15600 break;
15601
15602 case CPP_XOR:
15603 op = BIT_XOR_EXPR;
15604 break;
15605
15606 case CPP_AND:
15607 op = BIT_AND_EXPR;
15608 break;
15609
15610 case CPP_OR:
15611 op = BIT_IOR_EXPR;
15612 break;
15613
15614 case CPP_COMPL:
15615 op = BIT_NOT_EXPR;
15616 break;
15617
15618 case CPP_NOT:
15619 op = TRUTH_NOT_EXPR;
15620 break;
15621
15622 case CPP_EQ:
15623 assop = true;
15624 op = NOP_EXPR;
15625 break;
15626
15627 case CPP_LESS:
15628 op = LT_EXPR;
15629 break;
15630
15631 case CPP_GREATER:
15632 op = GT_EXPR;
15633 break;
15634
15635 case CPP_PLUS_EQ:
15636 assop = true;
15637 op = PLUS_EXPR;
15638 break;
15639
15640 case CPP_MINUS_EQ:
15641 assop = true;
15642 op = MINUS_EXPR;
15643 break;
15644
15645 case CPP_MULT_EQ:
15646 assop = true;
15647 op = MULT_EXPR;
15648 break;
15649
15650 case CPP_DIV_EQ:
15651 assop = true;
15652 op = TRUNC_DIV_EXPR;
15653 break;
15654
15655 case CPP_MOD_EQ:
15656 assop = true;
15657 op = TRUNC_MOD_EXPR;
15658 break;
15659
15660 case CPP_XOR_EQ:
15661 assop = true;
15662 op = BIT_XOR_EXPR;
15663 break;
15664
15665 case CPP_AND_EQ:
15666 assop = true;
15667 op = BIT_AND_EXPR;
15668 break;
15669
15670 case CPP_OR_EQ:
15671 assop = true;
15672 op = BIT_IOR_EXPR;
15673 break;
15674
15675 case CPP_LSHIFT:
15676 op = LSHIFT_EXPR;
15677 break;
15678
15679 case CPP_RSHIFT:
15680 op = RSHIFT_EXPR;
15681 break;
15682
15683 case CPP_LSHIFT_EQ:
15684 assop = true;
15685 op = LSHIFT_EXPR;
15686 break;
15687
15688 case CPP_RSHIFT_EQ:
15689 assop = true;
15690 op = RSHIFT_EXPR;
15691 break;
15692
15693 case CPP_EQ_EQ:
15694 op = EQ_EXPR;
15695 break;
15696
15697 case CPP_NOT_EQ:
15698 op = NE_EXPR;
15699 break;
15700
15701 case CPP_LESS_EQ:
15702 op = LE_EXPR;
15703 break;
15704
15705 case CPP_GREATER_EQ:
15706 op = GE_EXPR;
15707 break;
15708
15709 case CPP_SPACESHIP:
15710 op = SPACESHIP_EXPR;
15711 break;
15712
15713 case CPP_AND_AND:
15714 op = TRUTH_ANDIF_EXPR;
15715 break;
15716
15717 case CPP_OR_OR:
15718 op = TRUTH_ORIF_EXPR;
15719 break;
15720
15721 case CPP_PLUS_PLUS:
15722 op = POSTINCREMENT_EXPR;
15723 break;
15724
15725 case CPP_MINUS_MINUS:
15726 op = PREDECREMENT_EXPR;
15727 break;
15728
15729 case CPP_COMMA:
15730 op = COMPOUND_EXPR;
15731 break;
15732
15733 case CPP_DEREF_STAR:
15734 op = MEMBER_REF;
15735 break;
15736
15737 case CPP_DEREF:
15738 op = COMPONENT_REF;
15739 break;
15740
15741 case CPP_QUERY:
15742 op = COND_EXPR;
15743 /* Consume the `?'. */
15744 cp_lexer_consume_token (parser->lexer);
15745 /* Look for the matching `:'. */
15746 cp_parser_require (parser, CPP_COLON, RT_COLON);
15747 consumed = true;
15748 break;
15749
15750 case CPP_OPEN_PAREN:
15751 {
15752 /* Consume the `('. */
15753 matching_parens parens;
15754 parens.consume_open (parser);
15755 /* Look for the matching `)'. */
15756 token = parens.require_close (parser);
15757 if (token)
15758 end_loc = token->location;
15759 op = CALL_EXPR;
15760 consumed = true;
15761 break;
15762 }
15763
15764 case CPP_OPEN_SQUARE:
15765 /* Consume the `['. */
15766 cp_lexer_consume_token (parser->lexer);
15767 /* Look for the matching `]'. */
15768 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15769 if (token)
15770 end_loc = token->location;
15771 op = ARRAY_REF;
15772 consumed = true;
15773 break;
15774
15775 case CPP_UTF8STRING:
15776 case CPP_UTF8STRING_USERDEF:
15777 utf8 = true;
15778 /* FALLTHRU */
15779 case CPP_STRING:
15780 case CPP_WSTRING:
15781 case CPP_STRING16:
15782 case CPP_STRING32:
15783 case CPP_STRING_USERDEF:
15784 case CPP_WSTRING_USERDEF:
15785 case CPP_STRING16_USERDEF:
15786 case CPP_STRING32_USERDEF:
15787 {
15788 cp_expr str;
15789 tree string_tree;
15790 int sz, len;
15791
15792 if (cxx_dialect == cxx98)
15793 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15794
15795 /* Consume the string. */
15796 str = cp_parser_string_literal (parser, /*translate=*/true,
15797 /*wide_ok=*/true, /*lookup_udlit=*/false);
15798 if (str == error_mark_node)
15799 return error_mark_node;
15800 else if (TREE_CODE (str) == USERDEF_LITERAL)
15801 {
15802 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
15803 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
15804 end_loc = str.get_location ();
15805 }
15806 else
15807 {
15808 string_tree = str;
15809 /* Look for the suffix identifier. */
15810 token = cp_lexer_peek_token (parser->lexer);
15811 if (token->type == CPP_NAME)
15812 {
15813 id = cp_parser_identifier (parser);
15814 end_loc = token->location;
15815 }
15816 else if (token->type == CPP_KEYWORD)
15817 {
15818 error ("unexpected keyword;"
15819 " remove space between quotes and suffix identifier");
15820 return error_mark_node;
15821 }
15822 else
15823 {
15824 error ("expected suffix identifier");
15825 return error_mark_node;
15826 }
15827 }
15828 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15829 (TREE_TYPE (TREE_TYPE (string_tree))));
15830 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15831 if (len != 0)
15832 {
15833 error ("expected empty string after %<operator%> keyword");
15834 return error_mark_node;
15835 }
15836 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15837 != char_type_node)
15838 {
15839 error ("invalid encoding prefix in literal operator");
15840 return error_mark_node;
15841 }
15842 if (id != error_mark_node)
15843 {
15844 const char *name = IDENTIFIER_POINTER (id);
15845 id = cp_literal_operator_id (name);
15846 }
15847 /* Generate a location of the form:
15848 "" _suffix_identifier
15849 ^~~~~~~~~~~~~~~~~~~~~
15850 with caret == start at the start token, finish at the end of the
15851 suffix identifier. */
15852 location_t combined_loc
15853 = make_location (start_loc, start_loc, parser->lexer);
15854 return cp_expr (id, combined_loc);
15855 }
15856
15857 default:
15858 /* Anything else is an error. */
15859 break;
15860 }
15861
15862 /* If we have selected an identifier, we need to consume the
15863 operator token. */
15864 if (op != ERROR_MARK)
15865 {
15866 id = ovl_op_identifier (assop, op);
15867 if (!consumed)
15868 cp_lexer_consume_token (parser->lexer);
15869 }
15870 /* Otherwise, no valid operator name was present. */
15871 else
15872 {
15873 cp_parser_error (parser, "expected operator");
15874 id = error_mark_node;
15875 }
15876
15877 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15878 return cp_expr (id, start_loc);
15879 }
15880
15881 /* Parse a template-declaration.
15882
15883 template-declaration:
15884 export [opt] template < template-parameter-list > declaration
15885
15886 If MEMBER_P is TRUE, this template-declaration occurs within a
15887 class-specifier.
15888
15889 The grammar rule given by the standard isn't correct. What
15890 is really meant is:
15891
15892 template-declaration:
15893 export [opt] template-parameter-list-seq
15894 decl-specifier-seq [opt] init-declarator [opt] ;
15895 export [opt] template-parameter-list-seq
15896 function-definition
15897
15898 template-parameter-list-seq:
15899 template-parameter-list-seq [opt]
15900 template < template-parameter-list >
15901
15902 Concept Extensions:
15903
15904 template-parameter-list-seq:
15905 template < template-parameter-list > requires-clause [opt]
15906
15907 requires-clause:
15908 requires logical-or-expression */
15909
15910 static void
15911 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15912 {
15913 /* Check for `export'. */
15914 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15915 {
15916 /* Consume the `export' token. */
15917 cp_lexer_consume_token (parser->lexer);
15918 /* Warn that we do not support `export'. */
15919 warning (0, "keyword %<export%> not implemented, and will be ignored");
15920 }
15921
15922 cp_parser_template_declaration_after_export (parser, member_p);
15923 }
15924
15925 /* Parse a template-parameter-list.
15926
15927 template-parameter-list:
15928 template-parameter
15929 template-parameter-list , template-parameter
15930
15931 Returns a TREE_LIST. Each node represents a template parameter.
15932 The nodes are connected via their TREE_CHAINs. */
15933
15934 static tree
15935 cp_parser_template_parameter_list (cp_parser* parser)
15936 {
15937 tree parameter_list = NULL_TREE;
15938
15939 /* Don't create wrapper nodes within a template-parameter-list,
15940 since we don't want to have different types based on the
15941 spelling location of constants and decls within them. */
15942 auto_suppress_location_wrappers sentinel;
15943
15944 begin_template_parm_list ();
15945
15946 /* The loop below parses the template parms. We first need to know
15947 the total number of template parms to be able to compute proper
15948 canonical types of each dependent type. So after the loop, when
15949 we know the total number of template parms,
15950 end_template_parm_list computes the proper canonical types and
15951 fixes up the dependent types accordingly. */
15952 while (true)
15953 {
15954 tree parameter;
15955 bool is_non_type;
15956 bool is_parameter_pack;
15957 location_t parm_loc;
15958
15959 /* Parse the template-parameter. */
15960 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15961 parameter = cp_parser_template_parameter (parser,
15962 &is_non_type,
15963 &is_parameter_pack);
15964 /* Add it to the list. */
15965 if (parameter != error_mark_node)
15966 parameter_list = process_template_parm (parameter_list,
15967 parm_loc,
15968 parameter,
15969 is_non_type,
15970 is_parameter_pack);
15971 else
15972 {
15973 tree err_parm = build_tree_list (parameter, parameter);
15974 parameter_list = chainon (parameter_list, err_parm);
15975 }
15976
15977 /* If the next token is not a `,', we're done. */
15978 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15979 break;
15980 /* Otherwise, consume the `,' token. */
15981 cp_lexer_consume_token (parser->lexer);
15982 }
15983
15984 return end_template_parm_list (parameter_list);
15985 }
15986
15987 /* Parse a introduction-list.
15988
15989 introduction-list:
15990 introduced-parameter
15991 introduction-list , introduced-parameter
15992
15993 introduced-parameter:
15994 ...[opt] identifier
15995
15996 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15997 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15998 WILDCARD_DECL will also have DECL_NAME set and token location in
15999 DECL_SOURCE_LOCATION. */
16000
16001 static tree
16002 cp_parser_introduction_list (cp_parser *parser)
16003 {
16004 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
16005
16006 while (true)
16007 {
16008 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
16009 if (is_pack)
16010 cp_lexer_consume_token (parser->lexer);
16011
16012 tree identifier = cp_parser_identifier (parser);
16013 if (identifier == error_mark_node)
16014 break;
16015
16016 /* Build placeholder. */
16017 tree parm = build_nt (WILDCARD_DECL);
16018 DECL_SOURCE_LOCATION (parm)
16019 = cp_lexer_peek_token (parser->lexer)->location;
16020 DECL_NAME (parm) = identifier;
16021 WILDCARD_PACK_P (parm) = is_pack;
16022 vec_safe_push (introduction_vec, parm);
16023
16024 /* If the next token is not a `,', we're done. */
16025 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16026 break;
16027 /* Otherwise, consume the `,' token. */
16028 cp_lexer_consume_token (parser->lexer);
16029 }
16030
16031 /* Convert the vec into a TREE_VEC. */
16032 tree introduction_list = make_tree_vec (introduction_vec->length ());
16033 unsigned int n;
16034 tree parm;
16035 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
16036 TREE_VEC_ELT (introduction_list, n) = parm;
16037
16038 release_tree_vector (introduction_vec);
16039 return introduction_list;
16040 }
16041
16042 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
16043 is an abstract declarator. */
16044
16045 static inline cp_declarator*
16046 get_id_declarator (cp_declarator *declarator)
16047 {
16048 cp_declarator *d = declarator;
16049 while (d && d->kind != cdk_id)
16050 d = d->declarator;
16051 return d;
16052 }
16053
16054 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
16055 is an abstract declarator. */
16056
16057 static inline tree
16058 get_unqualified_id (cp_declarator *declarator)
16059 {
16060 declarator = get_id_declarator (declarator);
16061 if (declarator)
16062 return declarator->u.id.unqualified_name;
16063 else
16064 return NULL_TREE;
16065 }
16066
16067 /* Returns true if TYPE would declare a constrained constrained-parameter. */
16068
16069 static inline bool
16070 is_constrained_parameter (tree type)
16071 {
16072 return (type
16073 && TREE_CODE (type) == TYPE_DECL
16074 && CONSTRAINED_PARM_CONCEPT (type)
16075 && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
16076 }
16077
16078 /* Returns true if PARM declares a constrained-parameter. */
16079
16080 static inline bool
16081 is_constrained_parameter (cp_parameter_declarator *parm)
16082 {
16083 return is_constrained_parameter (parm->decl_specifiers.type);
16084 }
16085
16086 /* Check that the type parameter is only a declarator-id, and that its
16087 type is not cv-qualified. */
16088
16089 bool
16090 cp_parser_check_constrained_type_parm (cp_parser *parser,
16091 cp_parameter_declarator *parm)
16092 {
16093 if (!parm->declarator)
16094 return true;
16095
16096 if (parm->declarator->kind != cdk_id)
16097 {
16098 cp_parser_error (parser, "invalid constrained type parameter");
16099 return false;
16100 }
16101
16102 /* Don't allow cv-qualified type parameters. */
16103 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
16104 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
16105 {
16106 cp_parser_error (parser, "cv-qualified type parameter");
16107 return false;
16108 }
16109
16110 return true;
16111 }
16112
16113 /* Finish parsing/processing a template type parameter and checking
16114 various restrictions. */
16115
16116 static inline tree
16117 cp_parser_constrained_type_template_parm (cp_parser *parser,
16118 tree id,
16119 cp_parameter_declarator* parmdecl)
16120 {
16121 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
16122 return finish_template_type_parm (class_type_node, id);
16123 else
16124 return error_mark_node;
16125 }
16126
16127 static tree
16128 finish_constrained_template_template_parm (tree proto, tree id)
16129 {
16130 /* FIXME: This should probably be copied, and we may need to adjust
16131 the template parameter depths. */
16132 tree saved_parms = current_template_parms;
16133 begin_template_parm_list ();
16134 current_template_parms = DECL_TEMPLATE_PARMS (proto);
16135 end_template_parm_list ();
16136
16137 tree parm = finish_template_template_parm (class_type_node, id);
16138 current_template_parms = saved_parms;
16139
16140 return parm;
16141 }
16142
16143 /* Finish parsing/processing a template template parameter by borrowing
16144 the template parameter list from the prototype parameter. */
16145
16146 static tree
16147 cp_parser_constrained_template_template_parm (cp_parser *parser,
16148 tree proto,
16149 tree id,
16150 cp_parameter_declarator *parmdecl)
16151 {
16152 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
16153 return error_mark_node;
16154 return finish_constrained_template_template_parm (proto, id);
16155 }
16156
16157 /* Create a new non-type template parameter from the given PARM
16158 declarator. */
16159
16160 static tree
16161 cp_parser_constrained_non_type_template_parm (bool *is_non_type,
16162 cp_parameter_declarator *parm)
16163 {
16164 *is_non_type = true;
16165 cp_declarator *decl = parm->declarator;
16166 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
16167 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
16168 return grokdeclarator (decl, specs, TPARM, 0, NULL);
16169 }
16170
16171 /* Build a constrained template parameter based on the PARMDECL
16172 declarator. The type of PARMDECL is the constrained type, which
16173 refers to the prototype template parameter that ultimately
16174 specifies the type of the declared parameter. */
16175
16176 static tree
16177 finish_constrained_parameter (cp_parser *parser,
16178 cp_parameter_declarator *parmdecl,
16179 bool *is_non_type)
16180 {
16181 tree decl = parmdecl->decl_specifiers.type;
16182 tree id = get_unqualified_id (parmdecl->declarator);
16183 tree def = parmdecl->default_argument;
16184 tree proto = DECL_INITIAL (decl);
16185
16186 /* Build the parameter. Return an error if the declarator was invalid. */
16187 tree parm;
16188 if (TREE_CODE (proto) == TYPE_DECL)
16189 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
16190 else if (TREE_CODE (proto) == TEMPLATE_DECL)
16191 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
16192 parmdecl);
16193 else
16194 parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
16195 if (parm == error_mark_node)
16196 return error_mark_node;
16197
16198 /* Finish the parameter decl and create a node attaching the
16199 default argument and constraint. */
16200 parm = build_tree_list (def, parm);
16201 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
16202
16203 return parm;
16204 }
16205
16206 /* Returns true if the parsed type actually represents the declaration
16207 of a type template-parameter. */
16208
16209 static bool
16210 declares_constrained_type_template_parameter (tree type)
16211 {
16212 return (is_constrained_parameter (type)
16213 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
16214 }
16215
16216 /* Returns true if the parsed type actually represents the declaration of
16217 a template template-parameter. */
16218
16219 static bool
16220 declares_constrained_template_template_parameter (tree type)
16221 {
16222 return (is_constrained_parameter (type)
16223 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
16224 }
16225
16226 /* Parse a default argument for a type template-parameter.
16227 Note that diagnostics are handled in cp_parser_template_parameter. */
16228
16229 static tree
16230 cp_parser_default_type_template_argument (cp_parser *parser)
16231 {
16232 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
16233
16234 /* Consume the `=' token. */
16235 cp_lexer_consume_token (parser->lexer);
16236
16237 cp_token *token = cp_lexer_peek_token (parser->lexer);
16238
16239 /* Parse the default-argument. */
16240 push_deferring_access_checks (dk_no_deferred);
16241 tree default_argument = cp_parser_type_id (parser,
16242 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16243 NULL);
16244 pop_deferring_access_checks ();
16245
16246 if (flag_concepts && type_uses_auto (default_argument))
16247 {
16248 error_at (token->location,
16249 "invalid use of %<auto%> in default template argument");
16250 return error_mark_node;
16251 }
16252
16253 return default_argument;
16254 }
16255
16256 /* Parse a default argument for a template template-parameter. */
16257
16258 static tree
16259 cp_parser_default_template_template_argument (cp_parser *parser)
16260 {
16261 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
16262
16263 bool is_template;
16264
16265 /* Consume the `='. */
16266 cp_lexer_consume_token (parser->lexer);
16267 /* Parse the id-expression. */
16268 push_deferring_access_checks (dk_no_deferred);
16269 /* save token before parsing the id-expression, for error
16270 reporting */
16271 const cp_token* token = cp_lexer_peek_token (parser->lexer);
16272 tree default_argument
16273 = cp_parser_id_expression (parser,
16274 /*template_keyword_p=*/false,
16275 /*check_dependency_p=*/true,
16276 /*template_p=*/&is_template,
16277 /*declarator_p=*/false,
16278 /*optional_p=*/false);
16279 if (TREE_CODE (default_argument) == TYPE_DECL)
16280 /* If the id-expression was a template-id that refers to
16281 a template-class, we already have the declaration here,
16282 so no further lookup is needed. */
16283 ;
16284 else
16285 /* Look up the name. */
16286 default_argument
16287 = cp_parser_lookup_name (parser, default_argument,
16288 none_type,
16289 /*is_template=*/is_template,
16290 /*is_namespace=*/false,
16291 /*check_dependency=*/true,
16292 /*ambiguous_decls=*/NULL,
16293 token->location);
16294 /* See if the default argument is valid. */
16295 default_argument = check_template_template_default_arg (default_argument);
16296 pop_deferring_access_checks ();
16297 return default_argument;
16298 }
16299
16300 /* Parse a template-parameter.
16301
16302 template-parameter:
16303 type-parameter
16304 parameter-declaration
16305
16306 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
16307 the parameter. The TREE_PURPOSE is the default value, if any.
16308 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
16309 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
16310 set to true iff this parameter is a parameter pack. */
16311
16312 static tree
16313 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
16314 bool *is_parameter_pack)
16315 {
16316 cp_token *token;
16317 cp_parameter_declarator *parameter_declarator;
16318 tree parm;
16319
16320 /* Assume it is a type parameter or a template parameter. */
16321 *is_non_type = false;
16322 /* Assume it not a parameter pack. */
16323 *is_parameter_pack = false;
16324 /* Peek at the next token. */
16325 token = cp_lexer_peek_token (parser->lexer);
16326 /* If it is `template', we have a type-parameter. */
16327 if (token->keyword == RID_TEMPLATE)
16328 return cp_parser_type_parameter (parser, is_parameter_pack);
16329 /* If it is `class' or `typename' we do not know yet whether it is a
16330 type parameter or a non-type parameter. Consider:
16331
16332 template <typename T, typename T::X X> ...
16333
16334 or:
16335
16336 template <class C, class D*> ...
16337
16338 Here, the first parameter is a type parameter, and the second is
16339 a non-type parameter. We can tell by looking at the token after
16340 the identifier -- if it is a `,', `=', or `>' then we have a type
16341 parameter. */
16342 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
16343 {
16344 /* Peek at the token after `class' or `typename'. */
16345 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16346 /* If it's an ellipsis, we have a template type parameter
16347 pack. */
16348 if (token->type == CPP_ELLIPSIS)
16349 return cp_parser_type_parameter (parser, is_parameter_pack);
16350 /* If it's an identifier, skip it. */
16351 if (token->type == CPP_NAME)
16352 token = cp_lexer_peek_nth_token (parser->lexer, 3);
16353 /* Now, see if the token looks like the end of a template
16354 parameter. */
16355 if (token->type == CPP_COMMA
16356 || token->type == CPP_EQ
16357 || token->type == CPP_GREATER)
16358 return cp_parser_type_parameter (parser, is_parameter_pack);
16359 }
16360
16361 /* Otherwise, it is a non-type parameter or a constrained parameter.
16362
16363 [temp.param]
16364
16365 When parsing a default template-argument for a non-type
16366 template-parameter, the first non-nested `>' is taken as the end
16367 of the template parameter-list rather than a greater-than
16368 operator. */
16369 parameter_declarator
16370 = cp_parser_parameter_declaration (parser,
16371 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16372 /*template_parm_p=*/true,
16373 /*parenthesized_p=*/NULL);
16374
16375 if (!parameter_declarator)
16376 return error_mark_node;
16377
16378 /* If the parameter declaration is marked as a parameter pack, set
16379 *IS_PARAMETER_PACK to notify the caller. */
16380 if (parameter_declarator->template_parameter_pack_p)
16381 *is_parameter_pack = true;
16382
16383 if (parameter_declarator->default_argument)
16384 {
16385 /* Can happen in some cases of erroneous input (c++/34892). */
16386 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16387 /* Consume the `...' for better error recovery. */
16388 cp_lexer_consume_token (parser->lexer);
16389 }
16390
16391 /* The parameter may have been constrained type parameter. */
16392 if (is_constrained_parameter (parameter_declarator))
16393 return finish_constrained_parameter (parser,
16394 parameter_declarator,
16395 is_non_type);
16396
16397 // Now we're sure that the parameter is a non-type parameter.
16398 *is_non_type = true;
16399
16400 parm = grokdeclarator (parameter_declarator->declarator,
16401 &parameter_declarator->decl_specifiers,
16402 TPARM, /*initialized=*/0,
16403 /*attrlist=*/NULL);
16404 if (parm == error_mark_node)
16405 return error_mark_node;
16406
16407 return build_tree_list (parameter_declarator->default_argument, parm);
16408 }
16409
16410 /* Parse a type-parameter.
16411
16412 type-parameter:
16413 class identifier [opt]
16414 class identifier [opt] = type-id
16415 typename identifier [opt]
16416 typename identifier [opt] = type-id
16417 template < template-parameter-list > class identifier [opt]
16418 template < template-parameter-list > class identifier [opt]
16419 = id-expression
16420
16421 GNU Extension (variadic templates):
16422
16423 type-parameter:
16424 class ... identifier [opt]
16425 typename ... identifier [opt]
16426
16427 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16428 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16429 the declaration of the parameter.
16430
16431 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16432
16433 static tree
16434 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
16435 {
16436 cp_token *token;
16437 tree parameter;
16438
16439 /* Look for a keyword to tell us what kind of parameter this is. */
16440 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
16441 if (!token)
16442 return error_mark_node;
16443
16444 switch (token->keyword)
16445 {
16446 case RID_CLASS:
16447 case RID_TYPENAME:
16448 {
16449 tree identifier;
16450 tree default_argument;
16451
16452 /* If the next token is an ellipsis, we have a template
16453 argument pack. */
16454 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16455 {
16456 /* Consume the `...' token. */
16457 cp_lexer_consume_token (parser->lexer);
16458 maybe_warn_variadic_templates ();
16459
16460 *is_parameter_pack = true;
16461 }
16462
16463 /* If the next token is an identifier, then it names the
16464 parameter. */
16465 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16466 identifier = cp_parser_identifier (parser);
16467 else
16468 identifier = NULL_TREE;
16469
16470 /* Create the parameter. */
16471 parameter = finish_template_type_parm (class_type_node, identifier);
16472
16473 /* If the next token is an `=', we have a default argument. */
16474 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16475 {
16476 default_argument
16477 = cp_parser_default_type_template_argument (parser);
16478
16479 /* Template parameter packs cannot have default
16480 arguments. */
16481 if (*is_parameter_pack)
16482 {
16483 if (identifier)
16484 error_at (token->location,
16485 "template parameter pack %qD cannot have a "
16486 "default argument", identifier);
16487 else
16488 error_at (token->location,
16489 "template parameter packs cannot have "
16490 "default arguments");
16491 default_argument = NULL_TREE;
16492 }
16493 else if (check_for_bare_parameter_packs (default_argument))
16494 default_argument = error_mark_node;
16495 }
16496 else
16497 default_argument = NULL_TREE;
16498
16499 /* Create the combined representation of the parameter and the
16500 default argument. */
16501 parameter = build_tree_list (default_argument, parameter);
16502 }
16503 break;
16504
16505 case RID_TEMPLATE:
16506 {
16507 tree identifier;
16508 tree default_argument;
16509
16510 /* Look for the `<'. */
16511 cp_parser_require (parser, CPP_LESS, RT_LESS);
16512 /* Parse the template-parameter-list. */
16513 cp_parser_template_parameter_list (parser);
16514 /* Look for the `>'. */
16515 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16516
16517 /* If template requirements are present, parse them. */
16518 if (flag_concepts)
16519 {
16520 tree reqs = get_shorthand_constraints (current_template_parms);
16521 if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
16522 reqs = combine_constraint_expressions (reqs, dreqs);
16523 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
16524 }
16525
16526 /* Look for the `class' or 'typename' keywords. */
16527 cp_parser_type_parameter_key (parser);
16528 /* If the next token is an ellipsis, we have a template
16529 argument pack. */
16530 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16531 {
16532 /* Consume the `...' token. */
16533 cp_lexer_consume_token (parser->lexer);
16534 maybe_warn_variadic_templates ();
16535
16536 *is_parameter_pack = true;
16537 }
16538 /* If the next token is an `=', then there is a
16539 default-argument. If the next token is a `>', we are at
16540 the end of the parameter-list. If the next token is a `,',
16541 then we are at the end of this parameter. */
16542 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16543 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
16544 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16545 {
16546 identifier = cp_parser_identifier (parser);
16547 /* Treat invalid names as if the parameter were nameless. */
16548 if (identifier == error_mark_node)
16549 identifier = NULL_TREE;
16550 }
16551 else
16552 identifier = NULL_TREE;
16553
16554 /* Create the template parameter. */
16555 parameter = finish_template_template_parm (class_type_node,
16556 identifier);
16557
16558 /* If the next token is an `=', then there is a
16559 default-argument. */
16560 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16561 {
16562 default_argument
16563 = cp_parser_default_template_template_argument (parser);
16564
16565 /* Template parameter packs cannot have default
16566 arguments. */
16567 if (*is_parameter_pack)
16568 {
16569 if (identifier)
16570 error_at (token->location,
16571 "template parameter pack %qD cannot "
16572 "have a default argument",
16573 identifier);
16574 else
16575 error_at (token->location, "template parameter packs cannot "
16576 "have default arguments");
16577 default_argument = NULL_TREE;
16578 }
16579 }
16580 else
16581 default_argument = NULL_TREE;
16582
16583 /* Create the combined representation of the parameter and the
16584 default argument. */
16585 parameter = build_tree_list (default_argument, parameter);
16586 }
16587 break;
16588
16589 default:
16590 gcc_unreachable ();
16591 break;
16592 }
16593
16594 return parameter;
16595 }
16596
16597 /* Parse a template-id.
16598
16599 template-id:
16600 template-name < template-argument-list [opt] >
16601
16602 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16603 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16604 returned. Otherwise, if the template-name names a function, or set
16605 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16606 names a class, returns a TYPE_DECL for the specialization.
16607
16608 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16609 uninstantiated templates. */
16610
16611 static tree
16612 cp_parser_template_id (cp_parser *parser,
16613 bool template_keyword_p,
16614 bool check_dependency_p,
16615 enum tag_types tag_type,
16616 bool is_declaration)
16617 {
16618 tree templ;
16619 tree arguments;
16620 tree template_id;
16621 cp_token_position start_of_id = 0;
16622 cp_token *next_token = NULL, *next_token_2 = NULL;
16623 bool is_identifier;
16624
16625 /* If the next token corresponds to a template-id, there is no need
16626 to reparse it. */
16627 cp_token *token = cp_lexer_peek_token (parser->lexer);
16628
16629 if (token->type == CPP_TEMPLATE_ID)
16630 {
16631 cp_lexer_consume_token (parser->lexer);
16632 return saved_checks_value (token->u.tree_check_value);
16633 }
16634
16635 /* Avoid performing name lookup if there is no possibility of
16636 finding a template-id. */
16637 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16638 || (token->type == CPP_NAME
16639 && !cp_parser_nth_token_starts_template_argument_list_p
16640 (parser, 2)))
16641 {
16642 cp_parser_error (parser, "expected template-id");
16643 return error_mark_node;
16644 }
16645
16646 /* Remember where the template-id starts. */
16647 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16648 start_of_id = cp_lexer_token_position (parser->lexer, false);
16649
16650 push_deferring_access_checks (dk_deferred);
16651
16652 /* Parse the template-name. */
16653 is_identifier = false;
16654 templ = cp_parser_template_name (parser, template_keyword_p,
16655 check_dependency_p,
16656 is_declaration,
16657 tag_type,
16658 &is_identifier);
16659
16660 /* Push any access checks inside the firewall we're about to create. */
16661 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
16662 pop_deferring_access_checks ();
16663 if (templ == error_mark_node || is_identifier)
16664 return templ;
16665
16666 /* Since we're going to preserve any side-effects from this parse, set up a
16667 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16668 in the template arguments. */
16669 tentative_firewall firewall (parser);
16670 reopen_deferring_access_checks (checks);
16671
16672 /* If we find the sequence `[:' after a template-name, it's probably
16673 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16674 parse correctly the argument list. */
16675 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16676 == CPP_OPEN_SQUARE)
16677 && next_token->flags & DIGRAPH
16678 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16679 == CPP_COLON)
16680 && !(next_token_2->flags & PREV_WHITE))
16681 {
16682 cp_parser_parse_tentatively (parser);
16683 /* Change `:' into `::'. */
16684 next_token_2->type = CPP_SCOPE;
16685 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16686 CPP_LESS. */
16687 cp_lexer_consume_token (parser->lexer);
16688
16689 /* Parse the arguments. */
16690 arguments = cp_parser_enclosed_template_argument_list (parser);
16691 if (!cp_parser_parse_definitely (parser))
16692 {
16693 /* If we couldn't parse an argument list, then we revert our changes
16694 and return simply an error. Maybe this is not a template-id
16695 after all. */
16696 next_token_2->type = CPP_COLON;
16697 cp_parser_error (parser, "expected %<<%>");
16698 pop_deferring_access_checks ();
16699 return error_mark_node;
16700 }
16701 /* Otherwise, emit an error about the invalid digraph, but continue
16702 parsing because we got our argument list. */
16703 if (permerror (next_token->location,
16704 "%<<::%> cannot begin a template-argument list"))
16705 {
16706 static bool hint = false;
16707 inform (next_token->location,
16708 "%<<:%> is an alternate spelling for %<[%>."
16709 " Insert whitespace between %<<%> and %<::%>");
16710 if (!hint && !flag_permissive)
16711 {
16712 inform (next_token->location, "(if you use %<-fpermissive%> "
16713 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16714 "accept your code)");
16715 hint = true;
16716 }
16717 }
16718 }
16719 else
16720 {
16721 /* Look for the `<' that starts the template-argument-list. */
16722 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16723 {
16724 pop_deferring_access_checks ();
16725 return error_mark_node;
16726 }
16727 /* Parse the arguments. */
16728 arguments = cp_parser_enclosed_template_argument_list (parser);
16729
16730 if ((cxx_dialect > cxx17)
16731 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16732 && !template_keyword_p
16733 && (cp_parser_error_occurred (parser)
16734 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16735 {
16736 /* This didn't go well. */
16737 if (TREE_CODE (templ) == FUNCTION_DECL)
16738 {
16739 /* C++20 says that "function-name < a;" is now ill-formed. */
16740 if (cp_parser_error_occurred (parser))
16741 {
16742 error_at (token->location, "invalid template-argument-list");
16743 inform (token->location, "function name as the left hand "
16744 "operand of %<<%> is ill-formed in C++20; wrap the "
16745 "function name in %<()%>");
16746 }
16747 else
16748 /* We expect "f<targs>" to be followed by "(args)". */
16749 error_at (cp_lexer_peek_token (parser->lexer)->location,
16750 "expected %<(%> after template-argument-list");
16751 if (start_of_id)
16752 /* Purge all subsequent tokens. */
16753 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16754 }
16755 else
16756 cp_parser_simulate_error (parser);
16757 pop_deferring_access_checks ();
16758 return error_mark_node;
16759 }
16760 }
16761
16762 /* Set the location to be of the form:
16763 template-name < template-argument-list [opt] >
16764 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16765 with caret == start at the start of the template-name,
16766 ranging until the closing '>'. */
16767 location_t combined_loc
16768 = make_location (token->location, token->location, parser->lexer);
16769
16770 /* Check for concepts autos where they don't belong. We could
16771 identify types in some cases of identifier TEMPL, looking ahead
16772 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16773 types. We reject them in functions, but if what we have is an
16774 identifier, even with none_type we can't conclude it's NOT a
16775 type, we have to wait for template substitution. */
16776 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16777 template_id = error_mark_node;
16778 /* Build a representation of the specialization. */
16779 else if (identifier_p (templ))
16780 template_id = build_min_nt_loc (combined_loc,
16781 TEMPLATE_ID_EXPR,
16782 templ, arguments);
16783 else if (DECL_TYPE_TEMPLATE_P (templ)
16784 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16785 {
16786 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16787 template (rather than some instantiation thereof) only if
16788 is not nested within some other construct. For example, in
16789 "template <typename T> void f(T) { A<T>::", A<T> is just an
16790 instantiation of A. */
16791 bool entering_scope
16792 = (template_parm_scope_p ()
16793 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16794 template_id
16795 = finish_template_type (templ, arguments, entering_scope);
16796 }
16797 else if (concept_definition_p (templ))
16798 {
16799 /* The caller will decide whether this is a concept check or type
16800 constraint. */
16801 template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
16802 boolean_type_node, templ, arguments);
16803 }
16804 else if (variable_template_p (templ))
16805 {
16806 template_id = lookup_template_variable (templ, arguments);
16807 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16808 SET_EXPR_LOCATION (template_id, combined_loc);
16809 }
16810 else
16811 {
16812 /* If it's not a class-template or a template-template, it should be
16813 a function-template. */
16814 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
16815
16816 template_id = lookup_template_function (templ, arguments);
16817 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16818 SET_EXPR_LOCATION (template_id, combined_loc);
16819 }
16820
16821 /* If parsing tentatively, replace the sequence of tokens that makes
16822 up the template-id with a CPP_TEMPLATE_ID token. That way,
16823 should we re-parse the token stream, we will not have to repeat
16824 the effort required to do the parse, nor will we issue duplicate
16825 error messages about problems during instantiation of the
16826 template. */
16827 if (start_of_id
16828 /* Don't do this if we had a parse error in a declarator; re-parsing
16829 might succeed if a name changes meaning (60361). */
16830 && !(cp_parser_error_occurred (parser)
16831 && cp_parser_parsing_tentatively (parser)
16832 && parser->in_declarator_p))
16833 {
16834 /* Reset the contents of the START_OF_ID token. */
16835 token->type = CPP_TEMPLATE_ID;
16836 token->location = combined_loc;
16837
16838 /* Retrieve any deferred checks. Do not pop this access checks yet
16839 so the memory will not be reclaimed during token replacing below. */
16840 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16841 token->tree_check_p = true;
16842 token->u.tree_check_value->value = template_id;
16843 token->u.tree_check_value->checks = get_deferred_access_checks ();
16844 token->keyword = RID_MAX;
16845
16846 /* Purge all subsequent tokens. */
16847 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16848
16849 /* ??? Can we actually assume that, if template_id ==
16850 error_mark_node, we will have issued a diagnostic to the
16851 user, as opposed to simply marking the tentative parse as
16852 failed? */
16853 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16854 error_at (token->location, "parse error in template argument list");
16855 }
16856
16857 pop_to_parent_deferring_access_checks ();
16858 return template_id;
16859 }
16860
16861 /* Like cp_parser_template_id, called in non-type context. */
16862
16863 static tree
16864 cp_parser_template_id_expr (cp_parser *parser,
16865 bool template_keyword_p,
16866 bool check_dependency_p,
16867 bool is_declaration)
16868 {
16869 tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
16870 none_type, is_declaration);
16871 if (TREE_CODE (x) == TEMPLATE_ID_EXPR
16872 && concept_check_p (x))
16873 /* We didn't check the arguments in cp_parser_template_id; do that now. */
16874 return build_concept_id (x);
16875 return x;
16876 }
16877
16878 /* Parse a template-name.
16879
16880 template-name:
16881 identifier
16882
16883 The standard should actually say:
16884
16885 template-name:
16886 identifier
16887 operator-function-id
16888
16889 A defect report has been filed about this issue.
16890
16891 A conversion-function-id cannot be a template name because they cannot
16892 be part of a template-id. In fact, looking at this code:
16893
16894 a.operator K<int>()
16895
16896 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16897 It is impossible to call a templated conversion-function-id with an
16898 explicit argument list, since the only allowed template parameter is
16899 the type to which it is converting.
16900
16901 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16902 `template' keyword, in a construction like:
16903
16904 T::template f<3>()
16905
16906 In that case `f' is taken to be a template-name, even though there
16907 is no way of knowing for sure.
16908
16909 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16910 name refers to a set of overloaded functions, at least one of which
16911 is a template, or an IDENTIFIER_NODE with the name of the template,
16912 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16913 names are looked up inside uninstantiated templates. */
16914
16915 static tree
16916 cp_parser_template_name (cp_parser* parser,
16917 bool template_keyword_p,
16918 bool check_dependency_p,
16919 bool is_declaration,
16920 enum tag_types tag_type,
16921 bool *is_identifier)
16922 {
16923 tree identifier;
16924 tree decl;
16925 cp_token *token = cp_lexer_peek_token (parser->lexer);
16926
16927 /* If the next token is `operator', then we have either an
16928 operator-function-id or a conversion-function-id. */
16929 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16930 {
16931 /* We don't know whether we're looking at an
16932 operator-function-id or a conversion-function-id. */
16933 cp_parser_parse_tentatively (parser);
16934 /* Try an operator-function-id. */
16935 identifier = cp_parser_operator_function_id (parser);
16936 /* If that didn't work, try a conversion-function-id. */
16937 if (!cp_parser_parse_definitely (parser))
16938 {
16939 cp_parser_error (parser, "expected template-name");
16940 return error_mark_node;
16941 }
16942 }
16943 /* Look for the identifier. */
16944 else
16945 identifier = cp_parser_identifier (parser);
16946
16947 /* If we didn't find an identifier, we don't have a template-id. */
16948 if (identifier == error_mark_node)
16949 return error_mark_node;
16950
16951 /* If the name immediately followed the `template' keyword, then it
16952 is a template-name. However, if the next token is not `<', then
16953 we do not treat it as a template-name, since it is not being used
16954 as part of a template-id. This enables us to handle constructs
16955 like:
16956
16957 template <typename T> struct S { S(); };
16958 template <typename T> S<T>::S();
16959
16960 correctly. We would treat `S' as a template -- if it were `S<T>'
16961 -- but we do not if there is no `<'. */
16962
16963 if (processing_template_decl
16964 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16965 {
16966 /* In a declaration, in a dependent context, we pretend that the
16967 "template" keyword was present in order to improve error
16968 recovery. For example, given:
16969
16970 template <typename T> void f(T::X<int>);
16971
16972 we want to treat "X<int>" as a template-id. */
16973 if (is_declaration
16974 && !template_keyword_p
16975 && parser->scope && TYPE_P (parser->scope)
16976 && check_dependency_p
16977 && dependent_scope_p (parser->scope)
16978 /* Do not do this for dtors (or ctors), since they never
16979 need the template keyword before their name. */
16980 && !constructor_name_p (identifier, parser->scope))
16981 {
16982 cp_token_position start = 0;
16983
16984 /* Explain what went wrong. */
16985 error_at (token->location, "non-template %qD used as template",
16986 identifier);
16987 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16988 parser->scope, identifier);
16989 /* If parsing tentatively, find the location of the "<" token. */
16990 if (cp_parser_simulate_error (parser))
16991 start = cp_lexer_token_position (parser->lexer, true);
16992 /* Parse the template arguments so that we can issue error
16993 messages about them. */
16994 cp_lexer_consume_token (parser->lexer);
16995 cp_parser_enclosed_template_argument_list (parser);
16996 /* Skip tokens until we find a good place from which to
16997 continue parsing. */
16998 cp_parser_skip_to_closing_parenthesis (parser,
16999 /*recovering=*/true,
17000 /*or_comma=*/true,
17001 /*consume_paren=*/false);
17002 /* If parsing tentatively, permanently remove the
17003 template argument list. That will prevent duplicate
17004 error messages from being issued about the missing
17005 "template" keyword. */
17006 if (start)
17007 cp_lexer_purge_tokens_after (parser->lexer, start);
17008 if (is_identifier)
17009 *is_identifier = true;
17010 parser->context->object_type = NULL_TREE;
17011 return identifier;
17012 }
17013
17014 /* If the "template" keyword is present, then there is generally
17015 no point in doing name-lookup, so we just return IDENTIFIER.
17016 But, if the qualifying scope is non-dependent then we can
17017 (and must) do name-lookup normally. */
17018 if (template_keyword_p)
17019 {
17020 tree scope = (parser->scope ? parser->scope
17021 : parser->context->object_type);
17022 if (scope && TYPE_P (scope)
17023 && (!CLASS_TYPE_P (scope)
17024 || (check_dependency_p && dependent_type_p (scope))))
17025 {
17026 /* We're optimizing away the call to cp_parser_lookup_name, but
17027 we still need to do this. */
17028 parser->context->object_type = NULL_TREE;
17029 return identifier;
17030 }
17031 }
17032 }
17033
17034 /* cp_parser_lookup_name clears OBJECT_TYPE. */
17035 const bool scoped_p = ((parser->scope ? parser->scope
17036 : parser->context->object_type) != NULL_TREE);
17037
17038 /* Look up the name. */
17039 decl = cp_parser_lookup_name (parser, identifier,
17040 tag_type,
17041 /*is_template=*/true,
17042 /*is_namespace=*/false,
17043 check_dependency_p,
17044 /*ambiguous_decls=*/NULL,
17045 token->location);
17046
17047 decl = strip_using_decl (decl);
17048
17049 /* If DECL is a template, then the name was a template-name. */
17050 if (TREE_CODE (decl) == TEMPLATE_DECL)
17051 {
17052 if (TREE_DEPRECATED (decl)
17053 && deprecated_state != DEPRECATED_SUPPRESS)
17054 {
17055 tree d = DECL_TEMPLATE_RESULT (decl);
17056 tree attr;
17057 if (TREE_CODE (d) == TYPE_DECL)
17058 attr = lookup_attribute ("deprecated",
17059 TYPE_ATTRIBUTES (TREE_TYPE (d)));
17060 else
17061 attr = lookup_attribute ("deprecated",
17062 DECL_ATTRIBUTES (d));
17063 warn_deprecated_use (decl, attr);
17064 }
17065 }
17066 else
17067 {
17068 /* The standard does not explicitly indicate whether a name that
17069 names a set of overloaded declarations, some of which are
17070 templates, is a template-name. However, such a name should
17071 be a template-name; otherwise, there is no way to form a
17072 template-id for the overloaded templates. */
17073 bool found = false;
17074
17075 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
17076 !found && iter; ++iter)
17077 if (TREE_CODE (*iter) == TEMPLATE_DECL)
17078 found = true;
17079
17080 if (!found
17081 && (cxx_dialect > cxx17)
17082 && !scoped_p
17083 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
17084 && tag_type == none_type)
17085 {
17086 /* [temp.names] says "A name is also considered to refer to a template
17087 if it is an unqualified-id followed by a < and name lookup finds
17088 either one or more functions or finds nothing." */
17089
17090 /* The "more functions" case. Just use the OVERLOAD as normally.
17091 We don't use is_overloaded_fn here to avoid considering
17092 BASELINKs. */
17093 if (TREE_CODE (decl) == OVERLOAD
17094 /* Name lookup found one function. */
17095 || TREE_CODE (decl) == FUNCTION_DECL)
17096 found = true;
17097 /* Name lookup found nothing. */
17098 else if (decl == error_mark_node)
17099 return identifier;
17100 }
17101
17102 if (!found)
17103 {
17104 /* The name does not name a template. */
17105 cp_parser_error (parser, "expected template-name");
17106 return error_mark_node;
17107 }
17108 }
17109
17110 return decl;
17111 }
17112
17113 /* Parse a template-argument-list.
17114
17115 template-argument-list:
17116 template-argument ... [opt]
17117 template-argument-list , template-argument ... [opt]
17118
17119 Returns a TREE_VEC containing the arguments. */
17120
17121 static tree
17122 cp_parser_template_argument_list (cp_parser* parser)
17123 {
17124 tree fixed_args[10];
17125 unsigned n_args = 0;
17126 unsigned alloced = 10;
17127 tree *arg_ary = fixed_args;
17128 tree vec;
17129 bool saved_in_template_argument_list_p;
17130 bool saved_ice_p;
17131 bool saved_non_ice_p;
17132
17133 /* Don't create location wrapper nodes within a template-argument-list. */
17134 auto_suppress_location_wrappers sentinel;
17135
17136 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
17137 parser->in_template_argument_list_p = true;
17138 /* Even if the template-id appears in an integral
17139 constant-expression, the contents of the argument list do
17140 not. */
17141 saved_ice_p = parser->integral_constant_expression_p;
17142 parser->integral_constant_expression_p = false;
17143 saved_non_ice_p = parser->non_integral_constant_expression_p;
17144 parser->non_integral_constant_expression_p = false;
17145
17146 /* Parse the arguments. */
17147 do
17148 {
17149 tree argument;
17150
17151 if (n_args)
17152 /* Consume the comma. */
17153 cp_lexer_consume_token (parser->lexer);
17154
17155 /* Parse the template-argument. */
17156 argument = cp_parser_template_argument (parser);
17157
17158 /* If the next token is an ellipsis, we're expanding a template
17159 argument pack. */
17160 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17161 {
17162 if (argument == error_mark_node)
17163 {
17164 cp_token *token = cp_lexer_peek_token (parser->lexer);
17165 error_at (token->location,
17166 "expected parameter pack before %<...%>");
17167 }
17168 /* Consume the `...' token. */
17169 cp_lexer_consume_token (parser->lexer);
17170
17171 /* Make the argument into a TYPE_PACK_EXPANSION or
17172 EXPR_PACK_EXPANSION. */
17173 argument = make_pack_expansion (argument);
17174 }
17175
17176 if (n_args == alloced)
17177 {
17178 alloced *= 2;
17179
17180 if (arg_ary == fixed_args)
17181 {
17182 arg_ary = XNEWVEC (tree, alloced);
17183 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
17184 }
17185 else
17186 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
17187 }
17188 arg_ary[n_args++] = argument;
17189 }
17190 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17191
17192 vec = make_tree_vec (n_args);
17193
17194 while (n_args--)
17195 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
17196
17197 if (arg_ary != fixed_args)
17198 free (arg_ary);
17199 parser->non_integral_constant_expression_p = saved_non_ice_p;
17200 parser->integral_constant_expression_p = saved_ice_p;
17201 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
17202 if (CHECKING_P)
17203 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
17204 return vec;
17205 }
17206
17207 /* Parse a template-argument.
17208
17209 template-argument:
17210 assignment-expression
17211 type-id
17212 id-expression
17213
17214 The representation is that of an assignment-expression, type-id, or
17215 id-expression -- except that the qualified id-expression is
17216 evaluated, so that the value returned is either a DECL or an
17217 OVERLOAD.
17218
17219 Although the standard says "assignment-expression", it forbids
17220 throw-expressions or assignments in the template argument.
17221 Therefore, we use "conditional-expression" instead. */
17222
17223 static tree
17224 cp_parser_template_argument (cp_parser* parser)
17225 {
17226 tree argument;
17227 bool template_p;
17228 bool address_p;
17229 bool maybe_type_id = false;
17230 cp_token *token = NULL, *argument_start_token = NULL;
17231 location_t loc = 0;
17232 cp_id_kind idk;
17233
17234 /* There's really no way to know what we're looking at, so we just
17235 try each alternative in order.
17236
17237 [temp.arg]
17238
17239 In a template-argument, an ambiguity between a type-id and an
17240 expression is resolved to a type-id, regardless of the form of
17241 the corresponding template-parameter.
17242
17243 Therefore, we try a type-id first. */
17244 cp_parser_parse_tentatively (parser);
17245 argument = cp_parser_template_type_arg (parser);
17246 /* If there was no error parsing the type-id but the next token is a
17247 '>>', our behavior depends on which dialect of C++ we're
17248 parsing. In C++98, we probably found a typo for '> >'. But there
17249 are type-id which are also valid expressions. For instance:
17250
17251 struct X { int operator >> (int); };
17252 template <int V> struct Foo {};
17253 Foo<X () >> 5> r;
17254
17255 Here 'X()' is a valid type-id of a function type, but the user just
17256 wanted to write the expression "X() >> 5". Thus, we remember that we
17257 found a valid type-id, but we still try to parse the argument as an
17258 expression to see what happens.
17259
17260 In C++0x, the '>>' will be considered two separate '>'
17261 tokens. */
17262 if (!cp_parser_error_occurred (parser)
17263 && cxx_dialect == cxx98
17264 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17265 {
17266 maybe_type_id = true;
17267 cp_parser_abort_tentative_parse (parser);
17268 }
17269 else
17270 {
17271 /* If the next token isn't a `,' or a `>', then this argument wasn't
17272 really finished. This means that the argument is not a valid
17273 type-id. */
17274 if (!cp_parser_next_token_ends_template_argument_p (parser))
17275 cp_parser_error (parser, "expected template-argument");
17276 /* If that worked, we're done. */
17277 if (cp_parser_parse_definitely (parser))
17278 return argument;
17279 }
17280 /* We're still not sure what the argument will be. */
17281 cp_parser_parse_tentatively (parser);
17282 /* Try a template. */
17283 argument_start_token = cp_lexer_peek_token (parser->lexer);
17284 argument = cp_parser_id_expression (parser,
17285 /*template_keyword_p=*/false,
17286 /*check_dependency_p=*/true,
17287 &template_p,
17288 /*declarator_p=*/false,
17289 /*optional_p=*/false);
17290 /* If the next token isn't a `,' or a `>', then this argument wasn't
17291 really finished. */
17292 if (!cp_parser_next_token_ends_template_argument_p (parser))
17293 cp_parser_error (parser, "expected template-argument");
17294 if (!cp_parser_error_occurred (parser))
17295 {
17296 /* Figure out what is being referred to. If the id-expression
17297 was for a class template specialization, then we will have a
17298 TYPE_DECL at this point. There is no need to do name lookup
17299 at this point in that case. */
17300 if (TREE_CODE (argument) != TYPE_DECL)
17301 argument = cp_parser_lookup_name (parser, argument,
17302 none_type,
17303 /*is_template=*/template_p,
17304 /*is_namespace=*/false,
17305 /*check_dependency=*/true,
17306 /*ambiguous_decls=*/NULL,
17307 argument_start_token->location);
17308 if (TREE_CODE (argument) != TEMPLATE_DECL
17309 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
17310 cp_parser_error (parser, "expected template-name");
17311 }
17312 if (cp_parser_parse_definitely (parser))
17313 {
17314 if (TREE_DEPRECATED (argument))
17315 warn_deprecated_use (argument, NULL_TREE);
17316 return argument;
17317 }
17318 /* It must be a non-type argument. In C++17 any constant-expression is
17319 allowed. */
17320 if (cxx_dialect > cxx14)
17321 goto general_expr;
17322
17323 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
17324
17325 -- an integral constant-expression of integral or enumeration
17326 type; or
17327
17328 -- the name of a non-type template-parameter; or
17329
17330 -- the name of an object or function with external linkage...
17331
17332 -- the address of an object or function with external linkage...
17333
17334 -- a pointer to member... */
17335 /* Look for a non-type template parameter. */
17336 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17337 {
17338 cp_parser_parse_tentatively (parser);
17339 argument = cp_parser_primary_expression (parser,
17340 /*address_p=*/false,
17341 /*cast_p=*/false,
17342 /*template_arg_p=*/true,
17343 &idk);
17344 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
17345 || !cp_parser_next_token_ends_template_argument_p (parser))
17346 cp_parser_simulate_error (parser);
17347 if (cp_parser_parse_definitely (parser))
17348 return argument;
17349 }
17350
17351 /* If the next token is "&", the argument must be the address of an
17352 object or function with external linkage. */
17353 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
17354 if (address_p)
17355 {
17356 loc = cp_lexer_peek_token (parser->lexer)->location;
17357 cp_lexer_consume_token (parser->lexer);
17358 }
17359 /* See if we might have an id-expression. */
17360 token = cp_lexer_peek_token (parser->lexer);
17361 if (token->type == CPP_NAME
17362 || token->keyword == RID_OPERATOR
17363 || token->type == CPP_SCOPE
17364 || token->type == CPP_TEMPLATE_ID
17365 || token->type == CPP_NESTED_NAME_SPECIFIER)
17366 {
17367 cp_parser_parse_tentatively (parser);
17368 argument = cp_parser_primary_expression (parser,
17369 address_p,
17370 /*cast_p=*/false,
17371 /*template_arg_p=*/true,
17372 &idk);
17373 if (cp_parser_error_occurred (parser)
17374 || !cp_parser_next_token_ends_template_argument_p (parser))
17375 cp_parser_abort_tentative_parse (parser);
17376 else
17377 {
17378 tree probe;
17379
17380 if (INDIRECT_REF_P (argument))
17381 {
17382 /* Strip the dereference temporarily. */
17383 gcc_assert (REFERENCE_REF_P (argument));
17384 argument = TREE_OPERAND (argument, 0);
17385 }
17386
17387 /* If we're in a template, we represent a qualified-id referring
17388 to a static data member as a SCOPE_REF even if the scope isn't
17389 dependent so that we can check access control later. */
17390 probe = argument;
17391 if (TREE_CODE (probe) == SCOPE_REF)
17392 probe = TREE_OPERAND (probe, 1);
17393 if (VAR_P (probe))
17394 {
17395 /* A variable without external linkage might still be a
17396 valid constant-expression, so no error is issued here
17397 if the external-linkage check fails. */
17398 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
17399 cp_parser_simulate_error (parser);
17400 }
17401 else if (is_overloaded_fn (argument))
17402 /* All overloaded functions are allowed; if the external
17403 linkage test does not pass, an error will be issued
17404 later. */
17405 ;
17406 else if (address_p
17407 && (TREE_CODE (argument) == OFFSET_REF
17408 || TREE_CODE (argument) == SCOPE_REF))
17409 /* A pointer-to-member. */
17410 ;
17411 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
17412 ;
17413 else
17414 cp_parser_simulate_error (parser);
17415
17416 if (cp_parser_parse_definitely (parser))
17417 {
17418 if (address_p)
17419 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
17420 tf_warning_or_error);
17421 else
17422 argument = convert_from_reference (argument);
17423 return argument;
17424 }
17425 }
17426 }
17427 /* If the argument started with "&", there are no other valid
17428 alternatives at this point. */
17429 if (address_p)
17430 {
17431 cp_parser_error (parser, "invalid non-type template argument");
17432 return error_mark_node;
17433 }
17434
17435 general_expr:
17436 /* If the argument wasn't successfully parsed as a type-id followed
17437 by '>>', the argument can only be a constant expression now.
17438 Otherwise, we try parsing the constant-expression tentatively,
17439 because the argument could really be a type-id. */
17440 if (maybe_type_id)
17441 cp_parser_parse_tentatively (parser);
17442
17443 if (cxx_dialect <= cxx14)
17444 argument = cp_parser_constant_expression (parser);
17445 else
17446 {
17447 /* In C++20, we can encounter a braced-init-list. */
17448 if (cxx_dialect >= cxx20
17449 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17450 {
17451 bool expr_non_constant_p;
17452 return cp_parser_braced_list (parser, &expr_non_constant_p);
17453 }
17454
17455 /* With C++17 generalized non-type template arguments we need to handle
17456 lvalue constant expressions, too. */
17457 argument = cp_parser_assignment_expression (parser);
17458 require_potential_constant_expression (argument);
17459 }
17460
17461 if (!maybe_type_id)
17462 return argument;
17463 if (!cp_parser_next_token_ends_template_argument_p (parser))
17464 cp_parser_error (parser, "expected template-argument");
17465 if (cp_parser_parse_definitely (parser))
17466 return argument;
17467 /* We did our best to parse the argument as a non type-id, but that
17468 was the only alternative that matched (albeit with a '>' after
17469 it). We can assume it's just a typo from the user, and a
17470 diagnostic will then be issued. */
17471 return cp_parser_template_type_arg (parser);
17472 }
17473
17474 /* Parse an explicit-instantiation.
17475
17476 explicit-instantiation:
17477 template declaration
17478
17479 Although the standard says `declaration', what it really means is:
17480
17481 explicit-instantiation:
17482 template decl-specifier-seq [opt] declarator [opt] ;
17483
17484 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17485 supposed to be allowed. A defect report has been filed about this
17486 issue.
17487
17488 GNU Extension:
17489
17490 explicit-instantiation:
17491 storage-class-specifier template
17492 decl-specifier-seq [opt] declarator [opt] ;
17493 function-specifier template
17494 decl-specifier-seq [opt] declarator [opt] ; */
17495
17496 static void
17497 cp_parser_explicit_instantiation (cp_parser* parser)
17498 {
17499 int declares_class_or_enum;
17500 cp_decl_specifier_seq decl_specifiers;
17501 tree extension_specifier = NULL_TREE;
17502
17503 timevar_push (TV_TEMPLATE_INST);
17504
17505 /* Look for an (optional) storage-class-specifier or
17506 function-specifier. */
17507 if (cp_parser_allow_gnu_extensions_p (parser))
17508 {
17509 extension_specifier
17510 = cp_parser_storage_class_specifier_opt (parser);
17511 if (!extension_specifier)
17512 extension_specifier
17513 = cp_parser_function_specifier_opt (parser,
17514 /*decl_specs=*/NULL);
17515 }
17516
17517 /* Look for the `template' keyword. */
17518 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17519 /* Let the front end know that we are processing an explicit
17520 instantiation. */
17521 begin_explicit_instantiation ();
17522 /* [temp.explicit] says that we are supposed to ignore access
17523 control while processing explicit instantiation directives. */
17524 push_deferring_access_checks (dk_no_check);
17525 /* Parse a decl-specifier-seq. */
17526 cp_parser_decl_specifier_seq (parser,
17527 CP_PARSER_FLAGS_OPTIONAL,
17528 &decl_specifiers,
17529 &declares_class_or_enum);
17530 /* If there was exactly one decl-specifier, and it declared a class,
17531 and there's no declarator, then we have an explicit type
17532 instantiation. */
17533 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
17534 {
17535 tree type;
17536
17537 type = check_tag_decl (&decl_specifiers,
17538 /*explicit_type_instantiation_p=*/true);
17539 /* Turn access control back on for names used during
17540 template instantiation. */
17541 pop_deferring_access_checks ();
17542 if (type)
17543 do_type_instantiation (type, extension_specifier,
17544 /*complain=*/tf_error);
17545 }
17546 else
17547 {
17548 cp_declarator *declarator;
17549 tree decl;
17550
17551 /* Parse the declarator. */
17552 declarator
17553 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17554 CP_PARSER_FLAGS_NONE,
17555 /*ctor_dtor_or_conv_p=*/NULL,
17556 /*parenthesized_p=*/NULL,
17557 /*member_p=*/false,
17558 /*friend_p=*/false,
17559 /*static_p=*/false);
17560 if (declares_class_or_enum & 2)
17561 cp_parser_check_for_definition_in_return_type (declarator,
17562 decl_specifiers.type,
17563 decl_specifiers.locations[ds_type_spec]);
17564 if (declarator != cp_error_declarator)
17565 {
17566 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
17567 permerror (decl_specifiers.locations[ds_inline],
17568 "explicit instantiation shall not use"
17569 " %<inline%> specifier");
17570 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
17571 permerror (decl_specifiers.locations[ds_constexpr],
17572 "explicit instantiation shall not use"
17573 " %<constexpr%> specifier");
17574 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
17575 permerror (decl_specifiers.locations[ds_consteval],
17576 "explicit instantiation shall not use"
17577 " %<consteval%> specifier");
17578
17579 decl = grokdeclarator (declarator, &decl_specifiers,
17580 NORMAL, 0, &decl_specifiers.attributes);
17581 /* Turn access control back on for names used during
17582 template instantiation. */
17583 pop_deferring_access_checks ();
17584 /* Do the explicit instantiation. */
17585 do_decl_instantiation (decl, extension_specifier);
17586 }
17587 else
17588 {
17589 pop_deferring_access_checks ();
17590 /* Skip the body of the explicit instantiation. */
17591 cp_parser_skip_to_end_of_statement (parser);
17592 }
17593 }
17594 /* We're done with the instantiation. */
17595 end_explicit_instantiation ();
17596
17597 cp_parser_consume_semicolon_at_end_of_statement (parser);
17598
17599 timevar_pop (TV_TEMPLATE_INST);
17600 }
17601
17602 /* Parse an explicit-specialization.
17603
17604 explicit-specialization:
17605 template < > declaration
17606
17607 Although the standard says `declaration', what it really means is:
17608
17609 explicit-specialization:
17610 template <> decl-specifier [opt] init-declarator [opt] ;
17611 template <> function-definition
17612 template <> explicit-specialization
17613 template <> template-declaration */
17614
17615 static void
17616 cp_parser_explicit_specialization (cp_parser* parser)
17617 {
17618 bool need_lang_pop;
17619 cp_token *token = cp_lexer_peek_token (parser->lexer);
17620
17621 /* Look for the `template' keyword. */
17622 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17623 /* Look for the `<'. */
17624 cp_parser_require (parser, CPP_LESS, RT_LESS);
17625 /* Look for the `>'. */
17626 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17627 /* We have processed another parameter list. */
17628 ++parser->num_template_parameter_lists;
17629 /* [temp]
17630
17631 A template ... explicit specialization ... shall not have C
17632 linkage. */
17633 if (current_lang_name == lang_name_c)
17634 {
17635 error_at (token->location, "template specialization with C linkage");
17636 maybe_show_extern_c_location ();
17637 /* Give it C++ linkage to avoid confusing other parts of the
17638 front end. */
17639 push_lang_context (lang_name_cplusplus);
17640 need_lang_pop = true;
17641 }
17642 else
17643 need_lang_pop = false;
17644 /* Let the front end know that we are beginning a specialization. */
17645 if (!begin_specialization ())
17646 {
17647 end_specialization ();
17648 return;
17649 }
17650
17651 /* If the next keyword is `template', we need to figure out whether
17652 or not we're looking a template-declaration. */
17653 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17654 {
17655 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17656 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
17657 cp_parser_template_declaration_after_export (parser,
17658 /*member_p=*/false);
17659 else
17660 cp_parser_explicit_specialization (parser);
17661 }
17662 else
17663 /* Parse the dependent declaration. */
17664 cp_parser_single_declaration (parser,
17665 /*checks=*/NULL,
17666 /*member_p=*/false,
17667 /*explicit_specialization_p=*/true,
17668 /*friend_p=*/NULL);
17669 /* We're done with the specialization. */
17670 end_specialization ();
17671 /* For the erroneous case of a template with C linkage, we pushed an
17672 implicit C++ linkage scope; exit that scope now. */
17673 if (need_lang_pop)
17674 pop_lang_context ();
17675 /* We're done with this parameter list. */
17676 --parser->num_template_parameter_lists;
17677 }
17678
17679 /* Parse a type-specifier.
17680
17681 type-specifier:
17682 simple-type-specifier
17683 class-specifier
17684 enum-specifier
17685 elaborated-type-specifier
17686 cv-qualifier
17687
17688 GNU Extension:
17689
17690 type-specifier:
17691 __complex__
17692
17693 Returns a representation of the type-specifier. For a
17694 class-specifier, enum-specifier, or elaborated-type-specifier, a
17695 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17696
17697 The parser flags FLAGS is used to control type-specifier parsing.
17698
17699 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17700 in a decl-specifier-seq.
17701
17702 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17703 class-specifier, enum-specifier, or elaborated-type-specifier, then
17704 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17705 if a type is declared; 2 if it is defined. Otherwise, it is set to
17706 zero.
17707
17708 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17709 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17710 is set to FALSE. */
17711
17712 static tree
17713 cp_parser_type_specifier (cp_parser* parser,
17714 cp_parser_flags flags,
17715 cp_decl_specifier_seq *decl_specs,
17716 bool is_declaration,
17717 int* declares_class_or_enum,
17718 bool* is_cv_qualifier)
17719 {
17720 tree type_spec = NULL_TREE;
17721 cp_token *token;
17722 enum rid keyword;
17723 cp_decl_spec ds = ds_last;
17724
17725 /* Assume this type-specifier does not declare a new type. */
17726 if (declares_class_or_enum)
17727 *declares_class_or_enum = 0;
17728 /* And that it does not specify a cv-qualifier. */
17729 if (is_cv_qualifier)
17730 *is_cv_qualifier = false;
17731 /* Peek at the next token. */
17732 token = cp_lexer_peek_token (parser->lexer);
17733
17734 /* If we're looking at a keyword, we can use that to guide the
17735 production we choose. */
17736 keyword = token->keyword;
17737 switch (keyword)
17738 {
17739 case RID_ENUM:
17740 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17741 goto elaborated_type_specifier;
17742
17743 /* Look for the enum-specifier. */
17744 type_spec = cp_parser_enum_specifier (parser);
17745 /* If that worked, we're done. */
17746 if (type_spec)
17747 {
17748 if (declares_class_or_enum)
17749 *declares_class_or_enum = 2;
17750 if (decl_specs)
17751 cp_parser_set_decl_spec_type (decl_specs,
17752 type_spec,
17753 token,
17754 /*type_definition_p=*/true);
17755 return type_spec;
17756 }
17757 else
17758 goto elaborated_type_specifier;
17759
17760 /* Any of these indicate either a class-specifier, or an
17761 elaborated-type-specifier. */
17762 case RID_CLASS:
17763 case RID_STRUCT:
17764 case RID_UNION:
17765 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17766 goto elaborated_type_specifier;
17767
17768 /* Parse tentatively so that we can back up if we don't find a
17769 class-specifier. */
17770 cp_parser_parse_tentatively (parser);
17771 /* Look for the class-specifier. */
17772 type_spec = cp_parser_class_specifier (parser);
17773 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17774 /* If that worked, we're done. */
17775 if (cp_parser_parse_definitely (parser))
17776 {
17777 if (declares_class_or_enum)
17778 *declares_class_or_enum = 2;
17779 if (decl_specs)
17780 cp_parser_set_decl_spec_type (decl_specs,
17781 type_spec,
17782 token,
17783 /*type_definition_p=*/true);
17784 return type_spec;
17785 }
17786
17787 /* Fall through. */
17788 elaborated_type_specifier:
17789 /* We're declaring (not defining) a class or enum. */
17790 if (declares_class_or_enum)
17791 *declares_class_or_enum = 1;
17792
17793 /* Fall through. */
17794 case RID_TYPENAME:
17795 /* Look for an elaborated-type-specifier. */
17796 type_spec
17797 = (cp_parser_elaborated_type_specifier
17798 (parser,
17799 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17800 is_declaration));
17801 if (decl_specs)
17802 cp_parser_set_decl_spec_type (decl_specs,
17803 type_spec,
17804 token,
17805 /*type_definition_p=*/false);
17806 return type_spec;
17807
17808 case RID_CONST:
17809 ds = ds_const;
17810 if (is_cv_qualifier)
17811 *is_cv_qualifier = true;
17812 break;
17813
17814 case RID_VOLATILE:
17815 ds = ds_volatile;
17816 if (is_cv_qualifier)
17817 *is_cv_qualifier = true;
17818 break;
17819
17820 case RID_RESTRICT:
17821 ds = ds_restrict;
17822 if (is_cv_qualifier)
17823 *is_cv_qualifier = true;
17824 break;
17825
17826 case RID_COMPLEX:
17827 /* The `__complex__' keyword is a GNU extension. */
17828 ds = ds_complex;
17829 break;
17830
17831 default:
17832 break;
17833 }
17834
17835 /* Handle simple keywords. */
17836 if (ds != ds_last)
17837 {
17838 if (decl_specs)
17839 {
17840 set_and_check_decl_spec_loc (decl_specs, ds, token);
17841 decl_specs->any_specifiers_p = true;
17842 }
17843 return cp_lexer_consume_token (parser->lexer)->u.value;
17844 }
17845
17846 /* If we do not already have a type-specifier, assume we are looking
17847 at a simple-type-specifier. */
17848 type_spec = cp_parser_simple_type_specifier (parser,
17849 decl_specs,
17850 flags);
17851
17852 /* If we didn't find a type-specifier, and a type-specifier was not
17853 optional in this context, issue an error message. */
17854 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17855 {
17856 cp_parser_error (parser, "expected type specifier");
17857 return error_mark_node;
17858 }
17859
17860 return type_spec;
17861 }
17862
17863 /* Parse a simple-type-specifier.
17864
17865 simple-type-specifier:
17866 :: [opt] nested-name-specifier [opt] type-name
17867 :: [opt] nested-name-specifier template template-id
17868 char
17869 wchar_t
17870 bool
17871 short
17872 int
17873 long
17874 signed
17875 unsigned
17876 float
17877 double
17878 void
17879
17880 C++11 Extension:
17881
17882 simple-type-specifier:
17883 auto
17884 decltype ( expression )
17885 char16_t
17886 char32_t
17887 __underlying_type ( type-id )
17888
17889 C++17 extension:
17890
17891 nested-name-specifier(opt) template-name
17892
17893 GNU Extension:
17894
17895 simple-type-specifier:
17896 __int128
17897 __typeof__ unary-expression
17898 __typeof__ ( type-id )
17899 __typeof__ ( type-id ) { initializer-list , [opt] }
17900
17901 Concepts Extension:
17902
17903 simple-type-specifier:
17904 constrained-type-specifier
17905
17906 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17907 appropriately updated. */
17908
17909 static tree
17910 cp_parser_simple_type_specifier (cp_parser* parser,
17911 cp_decl_specifier_seq *decl_specs,
17912 cp_parser_flags flags)
17913 {
17914 tree type = NULL_TREE;
17915 cp_token *token;
17916 int idx;
17917
17918 /* Peek at the next token. */
17919 token = cp_lexer_peek_token (parser->lexer);
17920
17921 /* If we're looking at a keyword, things are easy. */
17922 switch (token->keyword)
17923 {
17924 case RID_CHAR:
17925 if (decl_specs)
17926 decl_specs->explicit_char_p = true;
17927 type = char_type_node;
17928 break;
17929 case RID_CHAR8:
17930 type = char8_type_node;
17931 break;
17932 case RID_CHAR16:
17933 type = char16_type_node;
17934 break;
17935 case RID_CHAR32:
17936 type = char32_type_node;
17937 break;
17938 case RID_WCHAR:
17939 type = wchar_type_node;
17940 break;
17941 case RID_BOOL:
17942 type = boolean_type_node;
17943 break;
17944 case RID_SHORT:
17945 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17946 type = short_integer_type_node;
17947 break;
17948 case RID_INT:
17949 if (decl_specs)
17950 decl_specs->explicit_int_p = true;
17951 type = integer_type_node;
17952 break;
17953 case RID_INT_N_0:
17954 case RID_INT_N_1:
17955 case RID_INT_N_2:
17956 case RID_INT_N_3:
17957 idx = token->keyword - RID_INT_N_0;
17958 if (! int_n_enabled_p [idx])
17959 break;
17960 if (decl_specs)
17961 {
17962 decl_specs->explicit_intN_p = true;
17963 decl_specs->int_n_idx = idx;
17964 /* Check if the alternate "__intN__" form has been used instead of
17965 "__intN". */
17966 if (strncmp (IDENTIFIER_POINTER (token->u.value)
17967 + (IDENTIFIER_LENGTH (token->u.value) - 2),
17968 "__", 2) == 0)
17969 decl_specs->int_n_alt = true;
17970 }
17971 type = int_n_trees [idx].signed_type;
17972 break;
17973 case RID_LONG:
17974 if (decl_specs)
17975 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17976 type = long_integer_type_node;
17977 break;
17978 case RID_SIGNED:
17979 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17980 type = integer_type_node;
17981 break;
17982 case RID_UNSIGNED:
17983 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17984 type = unsigned_type_node;
17985 break;
17986 case RID_FLOAT:
17987 type = float_type_node;
17988 break;
17989 case RID_DOUBLE:
17990 type = double_type_node;
17991 break;
17992 case RID_VOID:
17993 type = void_type_node;
17994 break;
17995
17996 case RID_AUTO:
17997 maybe_warn_cpp0x (CPP0X_AUTO);
17998 if (parser->auto_is_implicit_function_template_parm_p)
17999 {
18000 /* The 'auto' might be the placeholder return type for a function decl
18001 with trailing return type. */
18002 bool have_trailing_return_fn_decl = false;
18003
18004 cp_parser_parse_tentatively (parser);
18005 cp_lexer_consume_token (parser->lexer);
18006 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
18007 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
18008 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18009 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
18010 {
18011 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18012 {
18013 cp_lexer_consume_token (parser->lexer);
18014 cp_parser_skip_to_closing_parenthesis (parser,
18015 /*recovering*/false,
18016 /*or_comma*/false,
18017 /*consume_paren*/true);
18018 continue;
18019 }
18020
18021 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
18022 {
18023 have_trailing_return_fn_decl = true;
18024 break;
18025 }
18026
18027 cp_lexer_consume_token (parser->lexer);
18028 }
18029 cp_parser_abort_tentative_parse (parser);
18030
18031 if (have_trailing_return_fn_decl)
18032 {
18033 type = make_auto ();
18034 break;
18035 }
18036
18037 if (cxx_dialect >= cxx14)
18038 {
18039 type = synthesize_implicit_template_parm (parser, NULL_TREE);
18040 type = TREE_TYPE (type);
18041 }
18042 else
18043 type = error_mark_node;
18044
18045 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
18046 {
18047 if (cxx_dialect < cxx14)
18048 error_at (token->location,
18049 "use of %<auto%> in lambda parameter declaration "
18050 "only available with "
18051 "%<-std=c++14%> or %<-std=gnu++14%>");
18052 }
18053 else if (cxx_dialect < cxx14)
18054 error_at (token->location,
18055 "use of %<auto%> in parameter declaration "
18056 "only available with "
18057 "%<-std=c++14%> or %<-std=gnu++14%>");
18058 else if (!flag_concepts)
18059 pedwarn (token->location, 0,
18060 "use of %<auto%> in parameter declaration "
18061 "only available with %<-fconcepts-ts%>");
18062 }
18063 else
18064 type = make_auto ();
18065 break;
18066
18067 case RID_DECLTYPE:
18068 /* Since DR 743, decltype can either be a simple-type-specifier by
18069 itself or begin a nested-name-specifier. Parsing it will replace
18070 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
18071 handling below decide what to do. */
18072 cp_parser_decltype (parser);
18073 cp_lexer_set_token_position (parser->lexer, token);
18074 break;
18075
18076 case RID_TYPEOF:
18077 /* Consume the `typeof' token. */
18078 cp_lexer_consume_token (parser->lexer);
18079 /* Parse the operand to `typeof'. */
18080 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
18081 /* If it is not already a TYPE, take its type. */
18082 if (!TYPE_P (type))
18083 type = finish_typeof (type);
18084
18085 if (decl_specs)
18086 cp_parser_set_decl_spec_type (decl_specs, type,
18087 token,
18088 /*type_definition_p=*/false);
18089
18090 return type;
18091
18092 case RID_UNDERLYING_TYPE:
18093 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
18094 if (decl_specs)
18095 cp_parser_set_decl_spec_type (decl_specs, type,
18096 token,
18097 /*type_definition_p=*/false);
18098
18099 return type;
18100
18101 case RID_BASES:
18102 case RID_DIRECT_BASES:
18103 type = cp_parser_trait_expr (parser, token->keyword);
18104 if (decl_specs)
18105 cp_parser_set_decl_spec_type (decl_specs, type,
18106 token,
18107 /*type_definition_p=*/false);
18108 return type;
18109 default:
18110 break;
18111 }
18112
18113 /* If token is an already-parsed decltype not followed by ::,
18114 it's a simple-type-specifier. */
18115 if (token->type == CPP_DECLTYPE
18116 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
18117 {
18118 type = saved_checks_value (token->u.tree_check_value);
18119 if (decl_specs)
18120 {
18121 cp_parser_set_decl_spec_type (decl_specs, type,
18122 token,
18123 /*type_definition_p=*/false);
18124 /* Remember that we are handling a decltype in order to
18125 implement the resolution of DR 1510 when the argument
18126 isn't instantiation dependent. */
18127 decl_specs->decltype_p = true;
18128 }
18129 cp_lexer_consume_token (parser->lexer);
18130 return type;
18131 }
18132
18133 /* If the type-specifier was for a built-in type, we're done. */
18134 if (type)
18135 {
18136 /* Record the type. */
18137 if (decl_specs
18138 && (token->keyword != RID_SIGNED
18139 && token->keyword != RID_UNSIGNED
18140 && token->keyword != RID_SHORT
18141 && token->keyword != RID_LONG))
18142 cp_parser_set_decl_spec_type (decl_specs,
18143 type,
18144 token,
18145 /*type_definition_p=*/false);
18146 if (decl_specs)
18147 decl_specs->any_specifiers_p = true;
18148
18149 /* Consume the token. */
18150 cp_lexer_consume_token (parser->lexer);
18151
18152 if (type == error_mark_node)
18153 return error_mark_node;
18154
18155 /* There is no valid C++ program where a non-template type is
18156 followed by a "<". That usually indicates that the user thought
18157 that the type was a template. */
18158 cp_parser_check_for_invalid_template_id (parser, type, none_type,
18159 token->location);
18160
18161 return TYPE_NAME (type);
18162 }
18163
18164 /* The type-specifier must be a user-defined type. */
18165 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
18166 {
18167 bool qualified_p;
18168 bool global_p;
18169 const bool typename_p = (cxx_dialect >= cxx20
18170 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
18171
18172 /* Don't gobble tokens or issue error messages if this is an
18173 optional type-specifier. */
18174 if (flags & CP_PARSER_FLAGS_OPTIONAL)
18175 cp_parser_parse_tentatively (parser);
18176
18177 /* Remember current tentative parsing state -- if we know we need
18178 a type, we can give better diagnostics here. */
18179 bool tent = cp_parser_parsing_tentatively (parser);
18180
18181 token = cp_lexer_peek_token (parser->lexer);
18182
18183 /* Look for the optional `::' operator. */
18184 global_p
18185 = (cp_parser_global_scope_opt (parser,
18186 /*current_scope_valid_p=*/false)
18187 != NULL_TREE);
18188 /* Look for the nested-name specifier. */
18189 qualified_p
18190 = (cp_parser_nested_name_specifier_opt (parser,
18191 /*typename_keyword_p=*/false,
18192 /*check_dependency_p=*/true,
18193 /*type_p=*/false,
18194 /*is_declaration=*/false)
18195 != NULL_TREE);
18196 /* If we have seen a nested-name-specifier, and the next token
18197 is `template', then we are using the template-id production. */
18198 if (parser->scope
18199 && cp_parser_optional_template_keyword (parser))
18200 {
18201 /* Look for the template-id. */
18202 type = cp_parser_template_id (parser,
18203 /*template_keyword_p=*/true,
18204 /*check_dependency_p=*/true,
18205 none_type,
18206 /*is_declaration=*/false);
18207 /* If the template-id did not name a type, we are out of
18208 luck. */
18209 if (TREE_CODE (type) != TYPE_DECL)
18210 {
18211 /* ...unless we pretend we have seen 'typename'. */
18212 if (typename_p)
18213 type = cp_parser_make_typename_type (parser, type,
18214 token->location);
18215 else
18216 {
18217 cp_parser_error (parser, "expected template-id for type");
18218 type = error_mark_node;
18219 }
18220 }
18221 }
18222 /* DR 1812: A < following a qualified-id in a typename-specifier
18223 could safely be assumed to begin a template argument list, so
18224 the template keyword should be optional. */
18225 else if (parser->scope
18226 && qualified_p
18227 && typename_p
18228 && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
18229 {
18230 cp_parser_parse_tentatively (parser);
18231
18232 type = cp_parser_template_id (parser,
18233 /*template_keyword_p=*/true,
18234 /*check_dependency_p=*/true,
18235 none_type,
18236 /*is_declaration=*/false);
18237 /* This is handled below, so back off. */
18238 if (type && concept_check_p (type))
18239 cp_parser_simulate_error (parser);
18240
18241 if (!cp_parser_parse_definitely (parser))
18242 type = NULL_TREE;
18243 else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
18244 type = make_typename_type (parser->scope, type, typename_type,
18245 /*complain=*/tf_error);
18246 else if (TREE_CODE (type) != TYPE_DECL)
18247 type = NULL_TREE;
18248 }
18249
18250 /* Otherwise, look for a type-name. */
18251 if (!type)
18252 {
18253 if (cxx_dialect >= cxx17)
18254 cp_parser_parse_tentatively (parser);
18255
18256 type = cp_parser_type_name (parser, (qualified_p && typename_p));
18257
18258 if (cxx_dialect >= cxx17 && !cp_parser_parse_definitely (parser))
18259 type = NULL_TREE;
18260 }
18261
18262 if (!type && flag_concepts && decl_specs)
18263 {
18264 /* Try for a type-constraint with template arguments. We check
18265 decl_specs here to avoid trying this for a functional cast. */
18266
18267 cp_parser_parse_tentatively (parser);
18268
18269 type = cp_parser_template_id (parser,
18270 /*template_keyword_p=*/false,
18271 /*check_dependency_p=*/true,
18272 none_type,
18273 /*is_declaration=*/false);
18274 if (type && concept_check_p (type))
18275 {
18276 location_t loc = EXPR_LOCATION (type);
18277 type = cp_parser_placeholder_type_specifier (parser, loc,
18278 type, tent);
18279 if (tent && type == error_mark_node)
18280 /* Perhaps it's a concept-check expression. */
18281 cp_parser_simulate_error (parser);
18282 }
18283 else
18284 cp_parser_simulate_error (parser);
18285
18286 if (!cp_parser_parse_definitely (parser))
18287 type = NULL_TREE;
18288 }
18289
18290 if (!type && cxx_dialect >= cxx17)
18291 {
18292 /* Try class template argument deduction or type-constraint without
18293 template arguments. */
18294 tree name = cp_parser_identifier (parser);
18295 if (name && TREE_CODE (name) == IDENTIFIER_NODE
18296 && parser->scope != error_mark_node)
18297 {
18298 location_t loc
18299 = cp_lexer_previous_token (parser->lexer)->location;
18300 tree tmpl = cp_parser_lookup_name (parser, name,
18301 none_type,
18302 /*is_template=*/false,
18303 /*is_namespace=*/false,
18304 /*check_dependency=*/true,
18305 /*ambiguous_decls=*/NULL,
18306 token->location);
18307 if (tmpl && tmpl != error_mark_node
18308 && ctad_template_p (tmpl))
18309 type = make_template_placeholder (tmpl);
18310 else if (flag_concepts && tmpl && concept_definition_p (tmpl))
18311 type = cp_parser_placeholder_type_specifier (parser, loc,
18312 tmpl, tent);
18313 else
18314 {
18315 type = error_mark_node;
18316 if (!cp_parser_simulate_error (parser))
18317 cp_parser_name_lookup_error (parser, name, tmpl,
18318 NLE_TYPE, token->location);
18319 }
18320 }
18321 else
18322 type = error_mark_node;
18323 }
18324
18325 /* If it didn't work out, we don't have a TYPE. */
18326 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
18327 && !cp_parser_parse_definitely (parser))
18328 type = NULL_TREE;
18329
18330 /* Keep track of all name-lookups performed in class scopes. */
18331 if (type
18332 && !global_p
18333 && !qualified_p
18334 && TREE_CODE (type) == TYPE_DECL
18335 && identifier_p (DECL_NAME (type)))
18336 maybe_note_name_used_in_class (DECL_NAME (type), type);
18337
18338 if (type && decl_specs)
18339 cp_parser_set_decl_spec_type (decl_specs, type,
18340 token,
18341 /*type_definition_p=*/false);
18342 }
18343
18344 /* If we didn't get a type-name, issue an error message. */
18345 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
18346 {
18347 cp_parser_error (parser, "expected type-name");
18348 return error_mark_node;
18349 }
18350
18351 if (type && type != error_mark_node)
18352 {
18353 /* See if TYPE is an Objective-C type, and if so, parse and
18354 accept any protocol references following it. Do this before
18355 the cp_parser_check_for_invalid_template_id() call, because
18356 Objective-C types can be followed by '<...>' which would
18357 enclose protocol names rather than template arguments, and so
18358 everything is fine. */
18359 if (c_dialect_objc () && !parser->scope
18360 && (objc_is_id (type) || objc_is_class_name (type)))
18361 {
18362 tree protos = cp_parser_objc_protocol_refs_opt (parser);
18363 tree qual_type = objc_get_protocol_qualified_type (type, protos);
18364
18365 /* Clobber the "unqualified" type previously entered into
18366 DECL_SPECS with the new, improved protocol-qualified version. */
18367 if (decl_specs)
18368 decl_specs->type = qual_type;
18369
18370 return qual_type;
18371 }
18372
18373 /* There is no valid C++ program where a non-template type is
18374 followed by a "<". That usually indicates that the user
18375 thought that the type was a template. */
18376 cp_parser_check_for_invalid_template_id (parser, type,
18377 none_type,
18378 token->location);
18379 }
18380
18381 return type;
18382 }
18383
18384 /* Parse the remainder of a placholder-type-specifier.
18385
18386 placeholder-type-specifier:
18387 type-constraint_opt auto
18388 type-constraint_opt decltype(auto)
18389
18390 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
18391 and passed as TMPL. This function converts TMPL to an actual type-constraint,
18392 parses the placeholder type, and performs some contextual syntactic analysis.
18393
18394 LOC provides the location of the template name.
18395
18396 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
18397 don't give an error if TMPL isn't a valid type-constraint, as the template-id
18398 might actually be a concept-check,
18399
18400 Note that the Concepts TS allows the auto or decltype(auto) to be
18401 omitted in a constrained-type-specifier. */
18402
18403 tree
18404 cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
18405 tree tmpl, bool tentative)
18406 {
18407 if (tmpl == error_mark_node)
18408 return error_mark_node;
18409
18410 tree orig_tmpl = tmpl;
18411
18412 /* Get the arguments as written for subsequent analysis. */
18413 tree args = NULL_TREE;
18414 if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
18415 {
18416 args = TREE_OPERAND (tmpl, 1);
18417 tmpl = TREE_OPERAND (tmpl, 0);
18418 }
18419 if (args == NULL_TREE)
18420 /* A concept-name with no arguments can't be an expression. */
18421 tentative = false;
18422
18423 tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
18424
18425 /* Get the concept and prototype parameter for the constraint. */
18426 tree_pair info = finish_type_constraints (tmpl, args, complain);
18427 tree con = info.first;
18428 tree proto = info.second;
18429 if (con == error_mark_node)
18430 return error_mark_node;
18431
18432 /* As per the standard, require auto or decltype(auto), except in some
18433 cases (template parameter lists, -fconcepts-ts enabled). */
18434 cp_token *placeholder = NULL, *close_paren = NULL;
18435 if (cxx_dialect >= cxx20)
18436 {
18437 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
18438 placeholder = cp_lexer_consume_token (parser->lexer);
18439 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
18440 {
18441 placeholder = cp_lexer_consume_token (parser->lexer);
18442 matching_parens parens;
18443 parens.require_open (parser);
18444 cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
18445 close_paren = parens.require_close (parser);
18446 }
18447 }
18448
18449 /* A type constraint constrains a contextually determined type or type
18450 parameter pack. However, the Concepts TS does allow concepts
18451 to introduce non-type and template template parameters. */
18452 if (TREE_CODE (proto) != TYPE_DECL)
18453 {
18454 if (!flag_concepts_ts
18455 || !processing_template_parmlist)
18456 {
18457 error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
18458 inform (DECL_SOURCE_LOCATION (con), "concept defined here");
18459 return error_mark_node;
18460 }
18461 }
18462
18463 /* In a template parameter list, a type-parameter can be introduced
18464 by type-constraints alone. */
18465 if (processing_template_parmlist && !placeholder)
18466 return build_constrained_parameter (con, proto, args);
18467
18468 /* Diagnose issues placeholder issues. */
18469 if (!flag_concepts_ts
18470 && !parser->in_result_type_constraint_p
18471 && !placeholder)
18472 {
18473 if (tentative)
18474 /* Perhaps it's a concept-check expression (c++/91073). */
18475 return error_mark_node;
18476
18477 tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
18478 tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
18479 error_at (input_location,
18480 "expected %<auto%> or %<decltype(auto)%> after %qE", expr);
18481 /* Fall through. This is an error of omission. */
18482 }
18483 else if (parser->in_result_type_constraint_p && placeholder)
18484 {
18485 /* A trailing return type only allows type-constraints. */
18486 error_at (input_location,
18487 "unexpected placeholder in constrained result type");
18488 }
18489
18490 /* In a parameter-declaration-clause, a placeholder-type-specifier
18491 results in an invented template parameter. */
18492 if (parser->auto_is_implicit_function_template_parm_p)
18493 {
18494 if (close_paren)
18495 {
18496 location_t loc = make_location (placeholder->location,
18497 placeholder->location,
18498 close_paren->location);
18499 error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
18500 return error_mark_node;
18501 }
18502 tree parm = build_constrained_parameter (con, proto, args);
18503 return synthesize_implicit_template_parm (parser, parm);
18504 }
18505
18506 /* Determine if the type should be deduced using template argument
18507 deduction or decltype deduction. Note that the latter is always
18508 used for type-constraints in trailing return types. */
18509 bool decltype_p = placeholder
18510 ? placeholder->keyword == RID_DECLTYPE
18511 : parser->in_result_type_constraint_p;
18512
18513 /* Otherwise, this is the type of a variable or return type. */
18514 if (decltype_p)
18515 return make_constrained_decltype_auto (con, args);
18516 else
18517 return make_constrained_auto (con, args);
18518 }
18519
18520 /* Parse a type-name.
18521
18522 type-name:
18523 class-name
18524 enum-name
18525 typedef-name
18526 simple-template-id [in c++0x]
18527
18528 enum-name:
18529 identifier
18530
18531 typedef-name:
18532 identifier
18533
18534 Concepts:
18535
18536 type-name:
18537 concept-name
18538 partial-concept-id
18539
18540 concept-name:
18541 identifier
18542
18543 Returns a TYPE_DECL for the type. */
18544
18545 static tree
18546 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
18547 {
18548 tree type_decl;
18549
18550 /* We can't know yet whether it is a class-name or not. */
18551 cp_parser_parse_tentatively (parser);
18552 /* Try a class-name. */
18553 type_decl = cp_parser_class_name (parser,
18554 typename_keyword_p,
18555 /*template_keyword_p=*/false,
18556 none_type,
18557 /*check_dependency_p=*/true,
18558 /*class_head_p=*/false,
18559 /*is_declaration=*/false);
18560 /* If it's not a class-name, keep looking. */
18561 if (!cp_parser_parse_definitely (parser))
18562 {
18563 if (cxx_dialect < cxx11)
18564 /* It must be a typedef-name or an enum-name. */
18565 return cp_parser_nonclass_name (parser);
18566
18567 cp_parser_parse_tentatively (parser);
18568 /* It is either a simple-template-id representing an
18569 instantiation of an alias template... */
18570 type_decl = cp_parser_template_id (parser,
18571 /*template_keyword_p=*/false,
18572 /*check_dependency_p=*/true,
18573 none_type,
18574 /*is_declaration=*/false);
18575 /* Note that this must be an instantiation of an alias template
18576 because [temp.names]/6 says:
18577
18578 A template-id that names an alias template specialization
18579 is a type-name.
18580
18581 Whereas [temp.names]/7 says:
18582
18583 A simple-template-id that names a class template
18584 specialization is a class-name.
18585
18586 With concepts, this could also be a partial-concept-id that
18587 declares a non-type template parameter. */
18588 if (type_decl != NULL_TREE
18589 && TREE_CODE (type_decl) == TYPE_DECL
18590 && TYPE_DECL_ALIAS_P (type_decl))
18591 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
18592 else
18593 cp_parser_simulate_error (parser);
18594
18595 if (!cp_parser_parse_definitely (parser))
18596 /* ... Or a typedef-name or an enum-name. */
18597 return cp_parser_nonclass_name (parser);
18598 }
18599
18600 return type_decl;
18601 }
18602
18603 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18604 or a concept-name.
18605
18606 enum-name:
18607 identifier
18608
18609 typedef-name:
18610 identifier
18611
18612 concept-name:
18613 identifier
18614
18615 Returns a TYPE_DECL for the type. */
18616
18617 static tree
18618 cp_parser_nonclass_name (cp_parser* parser)
18619 {
18620 tree type_decl;
18621 tree identifier;
18622
18623 cp_token *token = cp_lexer_peek_token (parser->lexer);
18624 identifier = cp_parser_identifier (parser);
18625 if (identifier == error_mark_node)
18626 return error_mark_node;
18627
18628 /* Look up the type-name. */
18629 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
18630
18631 type_decl = strip_using_decl (type_decl);
18632
18633 if (TREE_CODE (type_decl) != TYPE_DECL
18634 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
18635 {
18636 /* See if this is an Objective-C type. */
18637 tree protos = cp_parser_objc_protocol_refs_opt (parser);
18638 tree type = objc_get_protocol_qualified_type (identifier, protos);
18639 if (type)
18640 type_decl = TYPE_NAME (type);
18641 }
18642
18643 /* Issue an error if we did not find a type-name. */
18644 if (TREE_CODE (type_decl) != TYPE_DECL
18645 /* In Objective-C, we have the complication that class names are
18646 normally type names and start declarations (eg, the
18647 "NSObject" in "NSObject *object;"), but can be used in an
18648 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18649 is an expression. So, a classname followed by a dot is not a
18650 valid type-name. */
18651 || (objc_is_class_name (TREE_TYPE (type_decl))
18652 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
18653 {
18654 if (!cp_parser_simulate_error (parser))
18655 cp_parser_name_lookup_error (parser, identifier, type_decl,
18656 NLE_TYPE, token->location);
18657 return error_mark_node;
18658 }
18659 /* Remember that the name was used in the definition of the
18660 current class so that we can check later to see if the
18661 meaning would have been different after the class was
18662 entirely defined. */
18663 else if (type_decl != error_mark_node
18664 && !parser->scope)
18665 maybe_note_name_used_in_class (identifier, type_decl);
18666
18667 return type_decl;
18668 }
18669
18670 /* Parse an elaborated-type-specifier. Note that the grammar given
18671 here incorporates the resolution to DR68.
18672
18673 elaborated-type-specifier:
18674 class-key :: [opt] nested-name-specifier [opt] identifier
18675 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18676 enum-key :: [opt] nested-name-specifier [opt] identifier
18677 typename :: [opt] nested-name-specifier identifier
18678 typename :: [opt] nested-name-specifier template [opt]
18679 template-id
18680
18681 GNU extension:
18682
18683 elaborated-type-specifier:
18684 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18685 class-key attributes :: [opt] nested-name-specifier [opt]
18686 template [opt] template-id
18687 enum attributes :: [opt] nested-name-specifier [opt] identifier
18688
18689 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18690 declared `friend'. If IS_DECLARATION is TRUE, then this
18691 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18692 something is being declared.
18693
18694 Returns the TYPE specified. */
18695
18696 static tree
18697 cp_parser_elaborated_type_specifier (cp_parser* parser,
18698 bool is_friend,
18699 bool is_declaration)
18700 {
18701 enum tag_types tag_type;
18702 tree identifier;
18703 tree type = NULL_TREE;
18704 tree attributes = NULL_TREE;
18705 tree globalscope;
18706 cp_token *token = NULL;
18707
18708 /* For class and enum types the location of the class-key or enum-key. */
18709 location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
18710 /* For a scoped enum, the 'class' or 'struct' keyword id. */
18711 rid scoped_key = RID_MAX;
18712
18713 /* See if we're looking at the `enum' keyword. */
18714 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
18715 {
18716 /* Consume the `enum' token. */
18717 cp_lexer_consume_token (parser->lexer);
18718 /* Remember that it's an enumeration type. */
18719 tag_type = enum_type;
18720 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18721 enums) is used here. */
18722 cp_token *token = cp_lexer_peek_token (parser->lexer);
18723 if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
18724 || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
18725 {
18726 location_t loc = token->location;
18727 gcc_rich_location richloc (loc);
18728 richloc.add_range (input_location);
18729 richloc.add_fixit_remove ();
18730 pedwarn (&richloc, 0, "elaborated-type-specifier for "
18731 "a scoped enum must not use the %qD keyword",
18732 token->u.value);
18733 /* Consume the `struct' or `class' and parse it anyway. */
18734 cp_lexer_consume_token (parser->lexer);
18735 /* Create a combined location for the whole scoped-enum-key. */
18736 key_loc = make_location (key_loc, key_loc, loc);
18737 }
18738 else
18739 scoped_key = RID_MAX;
18740
18741 /* Parse the attributes. */
18742 attributes = cp_parser_attributes_opt (parser);
18743 }
18744 /* Or, it might be `typename'. */
18745 else if (cp_lexer_next_token_is_keyword (parser->lexer,
18746 RID_TYPENAME))
18747 {
18748 /* Consume the `typename' token. */
18749 cp_lexer_consume_token (parser->lexer);
18750 /* Remember that it's a `typename' type. */
18751 tag_type = typename_type;
18752 }
18753 /* Otherwise it must be a class-key. */
18754 else
18755 {
18756 key_loc = cp_lexer_peek_token (parser->lexer)->location;
18757 tag_type = cp_parser_class_key (parser);
18758 if (tag_type == none_type)
18759 return error_mark_node;
18760 /* Parse the attributes. */
18761 attributes = cp_parser_attributes_opt (parser);
18762 }
18763
18764 /* Look for the `::' operator. */
18765 globalscope = cp_parser_global_scope_opt (parser,
18766 /*current_scope_valid_p=*/false);
18767 /* Look for the nested-name-specifier. */
18768 tree nested_name_specifier;
18769 if (tag_type == typename_type && !globalscope)
18770 {
18771 nested_name_specifier
18772 = cp_parser_nested_name_specifier (parser,
18773 /*typename_keyword_p=*/true,
18774 /*check_dependency_p=*/true,
18775 /*type_p=*/true,
18776 is_declaration);
18777 if (!nested_name_specifier)
18778 return error_mark_node;
18779 }
18780 else
18781 /* Even though `typename' is not present, the proposed resolution
18782 to Core Issue 180 says that in `class A<T>::B', `B' should be
18783 considered a type-name, even if `A<T>' is dependent. */
18784 nested_name_specifier
18785 = cp_parser_nested_name_specifier_opt (parser,
18786 /*typename_keyword_p=*/true,
18787 /*check_dependency_p=*/true,
18788 /*type_p=*/true,
18789 is_declaration);
18790 /* For everything but enumeration types, consider a template-id.
18791 For an enumeration type, consider only a plain identifier. */
18792 if (tag_type != enum_type)
18793 {
18794 bool template_p = false;
18795 tree decl;
18796
18797 /* Allow the `template' keyword. */
18798 template_p = cp_parser_optional_template_keyword (parser);
18799 /* If we didn't see `template', we don't know if there's a
18800 template-id or not. */
18801 if (!template_p)
18802 cp_parser_parse_tentatively (parser);
18803 /* The `template' keyword must follow a nested-name-specifier. */
18804 else if (!nested_name_specifier)
18805 {
18806 cp_parser_error (parser, "%<template%> must follow a nested-"
18807 "name-specifier");
18808 return error_mark_node;
18809 }
18810
18811 /* Parse the template-id. */
18812 token = cp_lexer_peek_token (parser->lexer);
18813 decl = cp_parser_template_id (parser, template_p,
18814 /*check_dependency_p=*/true,
18815 tag_type,
18816 is_declaration);
18817 /* If we didn't find a template-id, look for an ordinary
18818 identifier. */
18819 if (!template_p && !cp_parser_parse_definitely (parser))
18820 ;
18821 /* We can get here when cp_parser_template_id, called by
18822 cp_parser_class_name with tag_type == none_type, succeeds
18823 and caches a BASELINK. Then, when called again here,
18824 instead of failing and returning an error_mark_node
18825 returns it (see template/typename17.C in C++11).
18826 ??? Could we diagnose this earlier? */
18827 else if (tag_type == typename_type && BASELINK_P (decl))
18828 {
18829 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18830 type = error_mark_node;
18831 }
18832 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18833 in effect, then we must assume that, upon instantiation, the
18834 template will correspond to a class. */
18835 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18836 && tag_type == typename_type)
18837 type = make_typename_type (parser->scope, decl,
18838 typename_type,
18839 /*complain=*/tf_error);
18840 /* If the `typename' keyword is in effect and DECL is not a type
18841 decl, then type is non existent. */
18842 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18843 ;
18844 else if (TREE_CODE (decl) == TYPE_DECL)
18845 {
18846 type = check_elaborated_type_specifier (tag_type, decl,
18847 /*allow_template_p=*/true);
18848
18849 /* If the next token is a semicolon, this must be a specialization,
18850 instantiation, or friend declaration. Check the scope while we
18851 still know whether or not we had a nested-name-specifier. */
18852 if (type != error_mark_node
18853 && !nested_name_specifier && !is_friend
18854 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18855 check_unqualified_spec_or_inst (type, token->location);
18856 }
18857 else if (decl == error_mark_node)
18858 type = error_mark_node;
18859 }
18860
18861 if (!type)
18862 {
18863 token = cp_lexer_peek_token (parser->lexer);
18864 identifier = cp_parser_identifier (parser);
18865
18866 if (identifier == error_mark_node)
18867 {
18868 parser->scope = NULL_TREE;
18869 return error_mark_node;
18870 }
18871
18872 /* For a `typename', we needn't call xref_tag. */
18873 if (tag_type == typename_type
18874 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18875 return cp_parser_make_typename_type (parser, identifier,
18876 token->location);
18877
18878 /* Template parameter lists apply only if we are not within a
18879 function parameter list. */
18880 bool template_parm_lists_apply
18881 = parser->num_template_parameter_lists;
18882 if (template_parm_lists_apply)
18883 for (cp_binding_level *s = current_binding_level;
18884 s && s->kind != sk_template_parms;
18885 s = s->level_chain)
18886 if (s->kind == sk_function_parms)
18887 template_parm_lists_apply = false;
18888
18889 /* Look up a qualified name in the usual way. */
18890 if (parser->scope)
18891 {
18892 tree decl;
18893 tree ambiguous_decls;
18894
18895 decl = cp_parser_lookup_name (parser, identifier,
18896 tag_type,
18897 /*is_template=*/false,
18898 /*is_namespace=*/false,
18899 /*check_dependency=*/true,
18900 &ambiguous_decls,
18901 token->location);
18902
18903 /* If the lookup was ambiguous, an error will already have been
18904 issued. */
18905 if (ambiguous_decls)
18906 return error_mark_node;
18907
18908 /* If we are parsing friend declaration, DECL may be a
18909 TEMPLATE_DECL tree node here. However, we need to check
18910 whether this TEMPLATE_DECL results in valid code. Consider
18911 the following example:
18912
18913 namespace N {
18914 template <class T> class C {};
18915 }
18916 class X {
18917 template <class T> friend class N::C; // #1, valid code
18918 };
18919 template <class T> class Y {
18920 friend class N::C; // #2, invalid code
18921 };
18922
18923 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18924 name lookup of `N::C'. We see that friend declaration must
18925 be template for the code to be valid. Note that
18926 processing_template_decl does not work here since it is
18927 always 1 for the above two cases. */
18928
18929 decl = (cp_parser_maybe_treat_template_as_class
18930 (decl, /*tag_name_p=*/is_friend
18931 && template_parm_lists_apply));
18932
18933 if (TREE_CODE (decl) != TYPE_DECL)
18934 {
18935 cp_parser_diagnose_invalid_type_name (parser,
18936 identifier,
18937 token->location);
18938 return error_mark_node;
18939 }
18940
18941 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18942 {
18943 bool allow_template = (template_parm_lists_apply
18944 || DECL_SELF_REFERENCE_P (decl));
18945 type = check_elaborated_type_specifier (tag_type, decl,
18946 allow_template);
18947
18948 if (type == error_mark_node)
18949 return error_mark_node;
18950 }
18951
18952 /* Forward declarations of nested types, such as
18953
18954 class C1::C2;
18955 class C1::C2::C3;
18956
18957 are invalid unless all components preceding the final '::'
18958 are complete. If all enclosing types are complete, these
18959 declarations become merely pointless.
18960
18961 Invalid forward declarations of nested types are errors
18962 caught elsewhere in parsing. Those that are pointless arrive
18963 here. */
18964
18965 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18966 && !is_friend && is_declaration
18967 && !processing_explicit_instantiation)
18968 warning (0, "declaration %qD does not declare anything", decl);
18969
18970 type = TREE_TYPE (decl);
18971 }
18972 else
18973 {
18974 /* An elaborated-type-specifier sometimes introduces a new type and
18975 sometimes names an existing type. Normally, the rule is that it
18976 introduces a new type only if there is not an existing type of
18977 the same name already in scope. For example, given:
18978
18979 struct S {};
18980 void f() { struct S s; }
18981
18982 the `struct S' in the body of `f' is the same `struct S' as in
18983 the global scope; the existing definition is used. However, if
18984 there were no global declaration, this would introduce a new
18985 local class named `S'.
18986
18987 An exception to this rule applies to the following code:
18988
18989 namespace N { struct S; }
18990
18991 Here, the elaborated-type-specifier names a new type
18992 unconditionally; even if there is already an `S' in the
18993 containing scope this declaration names a new type.
18994 This exception only applies if the elaborated-type-specifier
18995 forms the complete declaration:
18996
18997 [class.name]
18998
18999 A declaration consisting solely of `class-key identifier ;' is
19000 either a redeclaration of the name in the current scope or a
19001 forward declaration of the identifier as a class name. It
19002 introduces the name into the current scope.
19003
19004 We are in this situation precisely when the next token is a `;'.
19005
19006 An exception to the exception is that a `friend' declaration does
19007 *not* name a new type; i.e., given:
19008
19009 struct S { friend struct T; };
19010
19011 `T' is not a new type in the scope of `S'.
19012
19013 Also, `new struct S' or `sizeof (struct S)' never results in the
19014 definition of a new type; a new type can only be declared in a
19015 declaration context. */
19016
19017 tag_scope ts;
19018 bool template_p;
19019
19020 if (is_friend)
19021 /* Friends have special name lookup rules. */
19022 ts = ts_within_enclosing_non_class;
19023 else if (is_declaration
19024 && cp_lexer_next_token_is (parser->lexer,
19025 CPP_SEMICOLON))
19026 /* This is a `class-key identifier ;' */
19027 ts = ts_current;
19028 else
19029 ts = ts_global;
19030
19031 template_p =
19032 (template_parm_lists_apply
19033 && (cp_parser_next_token_starts_class_definition_p (parser)
19034 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
19035 /* An unqualified name was used to reference this type, so
19036 there were no qualifying templates. */
19037 if (template_parm_lists_apply
19038 && !cp_parser_check_template_parameters (parser,
19039 /*num_templates=*/0,
19040 /*template_id*/false,
19041 token->location,
19042 /*declarator=*/NULL))
19043 return error_mark_node;
19044 type = xref_tag (tag_type, identifier, ts, template_p);
19045 }
19046 }
19047
19048 if (type == error_mark_node)
19049 return error_mark_node;
19050
19051 /* Allow attributes on forward declarations of classes. */
19052 if (attributes)
19053 {
19054 if (TREE_CODE (type) == TYPENAME_TYPE)
19055 warning (OPT_Wattributes,
19056 "attributes ignored on uninstantiated type");
19057 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
19058 && ! processing_explicit_instantiation)
19059 warning (OPT_Wattributes,
19060 "attributes ignored on template instantiation");
19061 else if (is_declaration && cp_parser_declares_only_class_p (parser))
19062 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
19063 else
19064 warning (OPT_Wattributes,
19065 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
19066 }
19067
19068 if (tag_type == enum_type)
19069 cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
19070 else
19071 {
19072 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
19073 for alias definition. */
19074 bool decl_class = (is_declaration
19075 && cp_parser_declares_only_class_p (parser));
19076 cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
19077 decl_class);
19078
19079 /* Indicate whether this class was declared as a `class' or as a
19080 `struct'. */
19081 if (CLASS_TYPE_P (type) && !currently_open_class (type))
19082 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
19083 }
19084
19085 /* A "<" cannot follow an elaborated type specifier. If that
19086 happens, the user was probably trying to form a template-id. */
19087 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
19088 token->location);
19089
19090 return type;
19091 }
19092
19093 /* Parse an enum-specifier.
19094
19095 enum-specifier:
19096 enum-head { enumerator-list [opt] }
19097 enum-head { enumerator-list , } [C++0x]
19098
19099 enum-head:
19100 enum-key identifier [opt] enum-base [opt]
19101 enum-key nested-name-specifier identifier enum-base [opt]
19102
19103 enum-key:
19104 enum
19105 enum class [C++0x]
19106 enum struct [C++0x]
19107
19108 enum-base: [C++0x]
19109 : type-specifier-seq
19110
19111 opaque-enum-specifier:
19112 enum-key identifier enum-base [opt] ;
19113
19114 GNU Extensions:
19115 enum-key attributes[opt] identifier [opt] enum-base [opt]
19116 { enumerator-list [opt] }attributes[opt]
19117 enum-key attributes[opt] identifier [opt] enum-base [opt]
19118 { enumerator-list, }attributes[opt] [C++0x]
19119
19120 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
19121 if the token stream isn't an enum-specifier after all. */
19122
19123 static tree
19124 cp_parser_enum_specifier (cp_parser* parser)
19125 {
19126 tree identifier;
19127 tree type = NULL_TREE;
19128 tree prev_scope;
19129 tree nested_name_specifier = NULL_TREE;
19130 tree attributes;
19131 bool scoped_enum_p = false;
19132 bool has_underlying_type = false;
19133 bool nested_being_defined = false;
19134 bool new_value_list = false;
19135 bool is_new_type = false;
19136 bool is_unnamed = false;
19137 tree underlying_type = NULL_TREE;
19138 cp_token *type_start_token = NULL;
19139 temp_override<bool> cleanup (parser->colon_corrects_to_scope_p, false);
19140
19141 /* Parse tentatively so that we can back up if we don't find a
19142 enum-specifier. */
19143 cp_parser_parse_tentatively (parser);
19144
19145 /* Caller guarantees that the current token is 'enum', an identifier
19146 possibly follows, and the token after that is an opening brace.
19147 If we don't have an identifier, fabricate an anonymous name for
19148 the enumeration being defined. */
19149 cp_lexer_consume_token (parser->lexer);
19150
19151 /* Parse the "class" or "struct", which indicates a scoped
19152 enumeration type in C++0x. */
19153 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
19154 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
19155 {
19156 if (cxx_dialect < cxx11)
19157 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
19158
19159 /* Consume the `struct' or `class' token. */
19160 cp_lexer_consume_token (parser->lexer);
19161
19162 scoped_enum_p = true;
19163 }
19164
19165 attributes = cp_parser_attributes_opt (parser);
19166
19167 /* Clear the qualification. */
19168 parser->scope = NULL_TREE;
19169 parser->qualifying_scope = NULL_TREE;
19170 parser->object_scope = NULL_TREE;
19171
19172 /* Figure out in what scope the declaration is being placed. */
19173 prev_scope = current_scope ();
19174
19175 type_start_token = cp_lexer_peek_token (parser->lexer);
19176
19177 push_deferring_access_checks (dk_no_check);
19178 nested_name_specifier
19179 = cp_parser_nested_name_specifier_opt (parser,
19180 /*typename_keyword_p=*/true,
19181 /*check_dependency_p=*/false,
19182 /*type_p=*/false,
19183 /*is_declaration=*/false);
19184
19185 if (nested_name_specifier)
19186 {
19187 tree name;
19188
19189 identifier = cp_parser_identifier (parser);
19190 name = cp_parser_lookup_name (parser, identifier,
19191 enum_type,
19192 /*is_template=*/false,
19193 /*is_namespace=*/false,
19194 /*check_dependency=*/true,
19195 /*ambiguous_decls=*/NULL,
19196 input_location);
19197 if (name && name != error_mark_node)
19198 {
19199 type = TREE_TYPE (name);
19200 if (TREE_CODE (type) == TYPENAME_TYPE)
19201 {
19202 /* Are template enums allowed in ISO? */
19203 if (template_parm_scope_p ())
19204 pedwarn (type_start_token->location, OPT_Wpedantic,
19205 "%qD is an enumeration template", name);
19206 /* ignore a typename reference, for it will be solved by name
19207 in start_enum. */
19208 type = NULL_TREE;
19209 }
19210 }
19211 else if (nested_name_specifier == error_mark_node)
19212 /* We already issued an error. */;
19213 else
19214 {
19215 error_at (type_start_token->location,
19216 "%qD does not name an enumeration in %qT",
19217 identifier, nested_name_specifier);
19218 nested_name_specifier = error_mark_node;
19219 }
19220 }
19221 else
19222 {
19223 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19224 identifier = cp_parser_identifier (parser);
19225 else
19226 {
19227 identifier = make_anon_name ();
19228 is_unnamed = true;
19229 if (scoped_enum_p)
19230 error_at (type_start_token->location,
19231 "unnamed scoped enum is not allowed");
19232 }
19233 }
19234 pop_deferring_access_checks ();
19235
19236 /* Check for the `:' that denotes a specified underlying type in C++0x.
19237 Note that a ':' could also indicate a bitfield width, however. */
19238 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19239 {
19240 cp_decl_specifier_seq type_specifiers;
19241
19242 /* Consume the `:'. */
19243 cp_lexer_consume_token (parser->lexer);
19244
19245 /* Parse the type-specifier-seq. */
19246 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
19247 /*is_declaration=*/false,
19248 /*is_trailing_return=*/false,
19249 &type_specifiers);
19250
19251 /* At this point this is surely not elaborated type specifier. */
19252 if (!cp_parser_parse_definitely (parser))
19253 return NULL_TREE;
19254
19255 if (cxx_dialect < cxx11)
19256 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
19257
19258 has_underlying_type = true;
19259
19260 /* If that didn't work, stop. */
19261 if (type_specifiers.type != error_mark_node)
19262 {
19263 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
19264 /*initialized=*/0, NULL);
19265 if (underlying_type == error_mark_node
19266 || check_for_bare_parameter_packs (underlying_type))
19267 underlying_type = NULL_TREE;
19268 }
19269 }
19270
19271 /* Look for the `{' but don't consume it yet. */
19272 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19273 {
19274 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
19275 {
19276 if (has_underlying_type)
19277 cp_parser_commit_to_tentative_parse (parser);
19278 cp_parser_error (parser, "expected %<{%>");
19279 if (has_underlying_type)
19280 return error_mark_node;
19281 }
19282 /* An opaque-enum-specifier must have a ';' here. */
19283 if ((scoped_enum_p || underlying_type)
19284 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19285 {
19286 if (has_underlying_type)
19287 cp_parser_commit_to_tentative_parse (parser);
19288 cp_parser_error (parser, "expected %<;%> or %<{%>");
19289 if (has_underlying_type)
19290 return error_mark_node;
19291 }
19292 }
19293
19294 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
19295 return NULL_TREE;
19296
19297 if (nested_name_specifier)
19298 {
19299 if (CLASS_TYPE_P (nested_name_specifier))
19300 {
19301 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
19302 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
19303 push_scope (nested_name_specifier);
19304 }
19305 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
19306 push_nested_namespace (nested_name_specifier);
19307 }
19308
19309 /* Issue an error message if type-definitions are forbidden here. */
19310 if (!cp_parser_check_type_definition (parser))
19311 type = error_mark_node;
19312 else
19313 /* Create the new type. We do this before consuming the opening
19314 brace so the enum will be recorded as being on the line of its
19315 tag (or the 'enum' keyword, if there is no tag). */
19316 type = start_enum (identifier, type, underlying_type,
19317 attributes, scoped_enum_p, &is_new_type);
19318
19319 /* If the next token is not '{' it is an opaque-enum-specifier or an
19320 elaborated-type-specifier. */
19321 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19322 {
19323 timevar_push (TV_PARSE_ENUM);
19324 if (nested_name_specifier
19325 && nested_name_specifier != error_mark_node)
19326 {
19327 /* The following catches invalid code such as:
19328 enum class S<int>::E { A, B, C }; */
19329 if (!processing_specialization
19330 && CLASS_TYPE_P (nested_name_specifier)
19331 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
19332 error_at (type_start_token->location, "cannot add an enumerator "
19333 "list to a template instantiation");
19334
19335 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
19336 {
19337 error_at (type_start_token->location,
19338 "%<%T::%E%> has not been declared",
19339 TYPE_CONTEXT (nested_name_specifier),
19340 nested_name_specifier);
19341 type = error_mark_node;
19342 }
19343 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
19344 && !CLASS_TYPE_P (nested_name_specifier))
19345 {
19346 error_at (type_start_token->location, "nested name specifier "
19347 "%qT for enum declaration does not name a class "
19348 "or namespace", nested_name_specifier);
19349 type = error_mark_node;
19350 }
19351 /* If that scope does not contain the scope in which the
19352 class was originally declared, the program is invalid. */
19353 else if (prev_scope && !is_ancestor (prev_scope,
19354 nested_name_specifier))
19355 {
19356 if (at_namespace_scope_p ())
19357 error_at (type_start_token->location,
19358 "declaration of %qD in namespace %qD which does not "
19359 "enclose %qD",
19360 type, prev_scope, nested_name_specifier);
19361 else
19362 error_at (type_start_token->location,
19363 "declaration of %qD in %qD which does not "
19364 "enclose %qD",
19365 type, prev_scope, nested_name_specifier);
19366 type = error_mark_node;
19367 }
19368 /* If that scope is the scope where the declaration is being placed
19369 the program is invalid. */
19370 else if (CLASS_TYPE_P (nested_name_specifier)
19371 && CLASS_TYPE_P (prev_scope)
19372 && same_type_p (nested_name_specifier, prev_scope))
19373 {
19374 permerror (type_start_token->location,
19375 "extra qualification not allowed");
19376 nested_name_specifier = NULL_TREE;
19377 }
19378 }
19379
19380 if (scoped_enum_p)
19381 begin_scope (sk_scoped_enum, type);
19382
19383 /* Consume the opening brace. */
19384 matching_braces braces;
19385 braces.consume_open (parser);
19386
19387 if (type == error_mark_node)
19388 ; /* Nothing to add */
19389 else if (OPAQUE_ENUM_P (type)
19390 || (cxx_dialect > cxx98 && processing_specialization))
19391 {
19392 new_value_list = true;
19393 SET_OPAQUE_ENUM_P (type, false);
19394 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
19395 }
19396 else
19397 {
19398 error_at (type_start_token->location,
19399 "multiple definition of %q#T", type);
19400 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
19401 "previous definition here");
19402 type = error_mark_node;
19403 }
19404
19405 if (type == error_mark_node)
19406 cp_parser_skip_to_end_of_block_or_statement (parser);
19407 /* If the next token is not '}', then there are some enumerators. */
19408 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19409 {
19410 if (is_unnamed && !scoped_enum_p)
19411 pedwarn (type_start_token->location, OPT_Wpedantic,
19412 "ISO C++ forbids empty unnamed enum");
19413 }
19414 else
19415 cp_parser_enumerator_list (parser, type);
19416
19417 /* Consume the final '}'. */
19418 braces.require_close (parser);
19419
19420 if (scoped_enum_p)
19421 finish_scope ();
19422 timevar_pop (TV_PARSE_ENUM);
19423 }
19424 else
19425 {
19426 /* If a ';' follows, then it is an opaque-enum-specifier
19427 and additional restrictions apply. */
19428 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19429 {
19430 if (is_unnamed)
19431 error_at (type_start_token->location,
19432 "opaque-enum-specifier without name");
19433 else if (nested_name_specifier)
19434 error_at (type_start_token->location,
19435 "opaque-enum-specifier must use a simple identifier");
19436 }
19437 }
19438
19439 /* Look for trailing attributes to apply to this enumeration, and
19440 apply them if appropriate. */
19441 if (cp_parser_allow_gnu_extensions_p (parser))
19442 {
19443 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
19444 cplus_decl_attributes (&type,
19445 trailing_attr,
19446 (int) ATTR_FLAG_TYPE_IN_PLACE);
19447 }
19448
19449 /* Finish up the enumeration. */
19450 if (type != error_mark_node)
19451 {
19452 if (new_value_list)
19453 finish_enum_value_list (type);
19454 if (is_new_type)
19455 finish_enum (type);
19456 }
19457
19458 if (nested_name_specifier)
19459 {
19460 if (CLASS_TYPE_P (nested_name_specifier))
19461 {
19462 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
19463 pop_scope (nested_name_specifier);
19464 }
19465 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
19466 pop_nested_namespace (nested_name_specifier);
19467 }
19468 return type;
19469 }
19470
19471 /* Parse an enumerator-list. The enumerators all have the indicated
19472 TYPE.
19473
19474 enumerator-list:
19475 enumerator-definition
19476 enumerator-list , enumerator-definition */
19477
19478 static void
19479 cp_parser_enumerator_list (cp_parser* parser, tree type)
19480 {
19481 while (true)
19482 {
19483 /* Parse an enumerator-definition. */
19484 cp_parser_enumerator_definition (parser, type);
19485
19486 /* If the next token is not a ',', we've reached the end of
19487 the list. */
19488 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19489 break;
19490 /* Otherwise, consume the `,' and keep going. */
19491 cp_lexer_consume_token (parser->lexer);
19492 /* If the next token is a `}', there is a trailing comma. */
19493 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19494 {
19495 if (cxx_dialect < cxx11)
19496 pedwarn (input_location, OPT_Wpedantic,
19497 "comma at end of enumerator list");
19498 break;
19499 }
19500 }
19501 }
19502
19503 /* Parse an enumerator-definition. The enumerator has the indicated
19504 TYPE.
19505
19506 enumerator-definition:
19507 enumerator
19508 enumerator = constant-expression
19509
19510 enumerator:
19511 identifier
19512
19513 GNU Extensions:
19514
19515 enumerator-definition:
19516 enumerator attributes [opt]
19517 enumerator attributes [opt] = constant-expression */
19518
19519 static void
19520 cp_parser_enumerator_definition (cp_parser* parser, tree type)
19521 {
19522 tree identifier;
19523 tree value;
19524 location_t loc;
19525
19526 /* Save the input location because we are interested in the location
19527 of the identifier and not the location of the explicit value. */
19528 loc = cp_lexer_peek_token (parser->lexer)->location;
19529
19530 /* Look for the identifier. */
19531 identifier = cp_parser_identifier (parser);
19532 if (identifier == error_mark_node)
19533 return;
19534
19535 /* Parse any specified attributes. */
19536 tree attrs = cp_parser_attributes_opt (parser);
19537
19538 /* If the next token is an '=', then there is an explicit value. */
19539 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19540 {
19541 /* Consume the `=' token. */
19542 cp_lexer_consume_token (parser->lexer);
19543 /* Parse the value. */
19544 value = cp_parser_constant_expression (parser);
19545 }
19546 else
19547 value = NULL_TREE;
19548
19549 /* If we are processing a template, make sure the initializer of the
19550 enumerator doesn't contain any bare template parameter pack. */
19551 if (check_for_bare_parameter_packs (value))
19552 value = error_mark_node;
19553
19554 /* Create the enumerator. */
19555 build_enumerator (identifier, value, type, attrs, loc);
19556 }
19557
19558 /* Parse a namespace-name.
19559
19560 namespace-name:
19561 original-namespace-name
19562 namespace-alias
19563
19564 Returns the NAMESPACE_DECL for the namespace. */
19565
19566 static tree
19567 cp_parser_namespace_name (cp_parser* parser)
19568 {
19569 tree identifier;
19570 tree namespace_decl;
19571
19572 cp_token *token = cp_lexer_peek_token (parser->lexer);
19573
19574 /* Get the name of the namespace. */
19575 identifier = cp_parser_identifier (parser);
19576 if (identifier == error_mark_node)
19577 return error_mark_node;
19578
19579 /* Look up the identifier in the currently active scope. Look only
19580 for namespaces, due to:
19581
19582 [basic.lookup.udir]
19583
19584 When looking up a namespace-name in a using-directive or alias
19585 definition, only namespace names are considered.
19586
19587 And:
19588
19589 [basic.lookup.qual]
19590
19591 During the lookup of a name preceding the :: scope resolution
19592 operator, object, function, and enumerator names are ignored.
19593
19594 (Note that cp_parser_qualifying_entity only calls this
19595 function if the token after the name is the scope resolution
19596 operator.) */
19597 namespace_decl = cp_parser_lookup_name (parser, identifier,
19598 none_type,
19599 /*is_template=*/false,
19600 /*is_namespace=*/true,
19601 /*check_dependency=*/true,
19602 /*ambiguous_decls=*/NULL,
19603 token->location);
19604 /* If it's not a namespace, issue an error. */
19605 if (namespace_decl == error_mark_node
19606 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
19607 {
19608 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19609 {
19610 auto_diagnostic_group d;
19611 name_hint hint;
19612 if (namespace_decl == error_mark_node
19613 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
19614 hint = suggest_alternative_in_explicit_scope (token->location,
19615 identifier,
19616 parser->scope);
19617 if (const char *suggestion = hint.suggestion ())
19618 {
19619 gcc_rich_location richloc (token->location);
19620 richloc.add_fixit_replace (suggestion);
19621 error_at (&richloc,
19622 "%qD is not a namespace-name; did you mean %qs?",
19623 identifier, suggestion);
19624 }
19625 else
19626 error_at (token->location, "%qD is not a namespace-name",
19627 identifier);
19628 }
19629 else
19630 cp_parser_error (parser, "expected namespace-name");
19631 namespace_decl = error_mark_node;
19632 }
19633
19634 return namespace_decl;
19635 }
19636
19637 /* Parse a namespace-definition.
19638
19639 namespace-definition:
19640 named-namespace-definition
19641 unnamed-namespace-definition
19642
19643 named-namespace-definition:
19644 original-namespace-definition
19645 extension-namespace-definition
19646
19647 original-namespace-definition:
19648 namespace identifier { namespace-body }
19649
19650 extension-namespace-definition:
19651 namespace original-namespace-name { namespace-body }
19652
19653 unnamed-namespace-definition:
19654 namespace { namespace-body } */
19655
19656 static void
19657 cp_parser_namespace_definition (cp_parser* parser)
19658 {
19659 tree identifier;
19660 int nested_definition_count = 0;
19661
19662 cp_ensure_no_omp_declare_simd (parser);
19663 cp_ensure_no_oacc_routine (parser);
19664
19665 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
19666 const bool topmost_inline_p = is_inline;
19667
19668 if (is_inline)
19669 {
19670 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
19671 cp_lexer_consume_token (parser->lexer);
19672 }
19673
19674 /* Look for the `namespace' keyword. */
19675 cp_token* token
19676 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19677
19678 /* Parse any specified attributes before the identifier. */
19679 tree attribs = cp_parser_attributes_opt (parser);
19680
19681 for (;;)
19682 {
19683 identifier = NULL_TREE;
19684
19685 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
19686 RID_INLINE);
19687 if (nested_inline_p && nested_definition_count != 0)
19688 {
19689 if (cxx_dialect < cxx20)
19690 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
19691 OPT_Wpedantic, "nested inline namespace definitions only "
19692 "available with %<-std=c++20%> or %<-std=gnu++20%>");
19693 cp_lexer_consume_token (parser->lexer);
19694 }
19695
19696 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19697 {
19698 identifier = cp_parser_identifier (parser);
19699
19700 if (cp_next_tokens_can_be_std_attribute_p (parser))
19701 pedwarn (input_location, OPT_Wpedantic,
19702 "standard attributes on namespaces must precede "
19703 "the namespace name");
19704
19705 /* Parse any attributes specified after the identifier. */
19706 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
19707 }
19708
19709 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19710 {
19711 /* Don't forget that the innermost namespace might have been
19712 marked as inline. Use |= because we cannot overwrite
19713 IS_INLINE in case the outermost namespace is inline, but
19714 there are no nested inlines. */
19715 is_inline |= nested_inline_p;
19716 break;
19717 }
19718
19719 if (!nested_definition_count && cxx_dialect < cxx17)
19720 pedwarn (input_location, OPT_Wpedantic,
19721 "nested namespace definitions only available with "
19722 "%<-std=c++17%> or %<-std=gnu++17%>");
19723
19724 /* Nested namespace names can create new namespaces (unlike
19725 other qualified-ids). */
19726 if (int count = (identifier
19727 ? push_namespace (identifier, nested_inline_p)
19728 : 0))
19729 nested_definition_count += count;
19730 else
19731 cp_parser_error (parser, "nested namespace name required");
19732 cp_lexer_consume_token (parser->lexer);
19733 }
19734
19735 if (nested_definition_count && !identifier)
19736 cp_parser_error (parser, "namespace name required");
19737
19738 if (nested_definition_count && attribs)
19739 error_at (token->location,
19740 "a nested namespace definition cannot have attributes");
19741 if (nested_definition_count && topmost_inline_p)
19742 error_at (token->location,
19743 "a nested namespace definition cannot be inline");
19744
19745 /* Start the namespace. */
19746 nested_definition_count += push_namespace (identifier, is_inline);
19747
19748 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
19749
19750 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
19751
19752 /* Look for the `{' to validate starting the namespace. */
19753 matching_braces braces;
19754 if (braces.require_open (parser))
19755 {
19756 /* Parse the body of the namespace. */
19757 cp_parser_namespace_body (parser);
19758
19759 /* Look for the final `}'. */
19760 braces.require_close (parser);
19761 }
19762
19763 if (has_visibility)
19764 pop_visibility (1);
19765
19766 /* Pop the nested namespace definitions. */
19767 while (nested_definition_count--)
19768 pop_namespace ();
19769 }
19770
19771 /* Parse a namespace-body.
19772
19773 namespace-body:
19774 declaration-seq [opt] */
19775
19776 static void
19777 cp_parser_namespace_body (cp_parser* parser)
19778 {
19779 cp_parser_declaration_seq_opt (parser);
19780 }
19781
19782 /* Parse a namespace-alias-definition.
19783
19784 namespace-alias-definition:
19785 namespace identifier = qualified-namespace-specifier ; */
19786
19787 static void
19788 cp_parser_namespace_alias_definition (cp_parser* parser)
19789 {
19790 tree identifier;
19791 tree namespace_specifier;
19792
19793 cp_token *token = cp_lexer_peek_token (parser->lexer);
19794
19795 /* Look for the `namespace' keyword. */
19796 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19797 /* Look for the identifier. */
19798 identifier = cp_parser_identifier (parser);
19799 if (identifier == error_mark_node)
19800 return;
19801 /* Look for the `=' token. */
19802 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
19803 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19804 {
19805 error_at (token->location, "%<namespace%> definition is not allowed here");
19806 /* Skip the definition. */
19807 cp_lexer_consume_token (parser->lexer);
19808 if (cp_parser_skip_to_closing_brace (parser))
19809 cp_lexer_consume_token (parser->lexer);
19810 return;
19811 }
19812 cp_parser_require (parser, CPP_EQ, RT_EQ);
19813 /* Look for the qualified-namespace-specifier. */
19814 namespace_specifier
19815 = cp_parser_qualified_namespace_specifier (parser);
19816 cp_warn_deprecated_use_scopes (namespace_specifier);
19817 /* Look for the `;' token. */
19818 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19819
19820 /* Register the alias in the symbol table. */
19821 do_namespace_alias (identifier, namespace_specifier);
19822 }
19823
19824 /* Parse a qualified-namespace-specifier.
19825
19826 qualified-namespace-specifier:
19827 :: [opt] nested-name-specifier [opt] namespace-name
19828
19829 Returns a NAMESPACE_DECL corresponding to the specified
19830 namespace. */
19831
19832 static tree
19833 cp_parser_qualified_namespace_specifier (cp_parser* parser)
19834 {
19835 /* Look for the optional `::'. */
19836 cp_parser_global_scope_opt (parser,
19837 /*current_scope_valid_p=*/false);
19838
19839 /* Look for the optional nested-name-specifier. */
19840 cp_parser_nested_name_specifier_opt (parser,
19841 /*typename_keyword_p=*/false,
19842 /*check_dependency_p=*/true,
19843 /*type_p=*/false,
19844 /*is_declaration=*/true);
19845
19846 return cp_parser_namespace_name (parser);
19847 }
19848
19849 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19850 access declaration.
19851
19852 using-declaration:
19853 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19854 using :: unqualified-id ;
19855
19856 access-declaration:
19857 qualified-id ;
19858
19859 */
19860
19861 static bool
19862 cp_parser_using_declaration (cp_parser* parser,
19863 bool access_declaration_p)
19864 {
19865 cp_token *token;
19866 bool typename_p = false;
19867 bool global_scope_p;
19868 tree decl;
19869 tree identifier;
19870 tree qscope;
19871 int oldcount = errorcount;
19872 cp_token *diag_token = NULL;
19873
19874 if (access_declaration_p)
19875 {
19876 diag_token = cp_lexer_peek_token (parser->lexer);
19877 cp_parser_parse_tentatively (parser);
19878 }
19879 else
19880 {
19881 /* Look for the `using' keyword. */
19882 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19883
19884 again:
19885 /* Peek at the next token. */
19886 token = cp_lexer_peek_token (parser->lexer);
19887 /* See if it's `typename'. */
19888 if (token->keyword == RID_TYPENAME)
19889 {
19890 /* Remember that we've seen it. */
19891 typename_p = true;
19892 /* Consume the `typename' token. */
19893 cp_lexer_consume_token (parser->lexer);
19894 }
19895 }
19896
19897 /* Look for the optional global scope qualification. */
19898 global_scope_p
19899 = (cp_parser_global_scope_opt (parser,
19900 /*current_scope_valid_p=*/false)
19901 != NULL_TREE);
19902
19903 /* If we saw `typename', or didn't see `::', then there must be a
19904 nested-name-specifier present. */
19905 if (typename_p || !global_scope_p)
19906 {
19907 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19908 /*check_dependency_p=*/true,
19909 /*type_p=*/false,
19910 /*is_declaration=*/true);
19911 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19912 {
19913 cp_parser_skip_to_end_of_block_or_statement (parser);
19914 return false;
19915 }
19916 }
19917 /* Otherwise, we could be in either of the two productions. In that
19918 case, treat the nested-name-specifier as optional. */
19919 else
19920 qscope = cp_parser_nested_name_specifier_opt (parser,
19921 /*typename_keyword_p=*/false,
19922 /*check_dependency_p=*/true,
19923 /*type_p=*/false,
19924 /*is_declaration=*/true);
19925 if (!qscope)
19926 qscope = global_namespace;
19927 else if (UNSCOPED_ENUM_P (qscope)
19928 && !TYPE_FUNCTION_SCOPE_P (qscope))
19929 qscope = CP_TYPE_CONTEXT (qscope);
19930
19931 cp_warn_deprecated_use_scopes (qscope);
19932
19933 if (access_declaration_p && cp_parser_error_occurred (parser))
19934 /* Something has already gone wrong; there's no need to parse
19935 further. Since an error has occurred, the return value of
19936 cp_parser_parse_definitely will be false, as required. */
19937 return cp_parser_parse_definitely (parser);
19938
19939 token = cp_lexer_peek_token (parser->lexer);
19940 /* Parse the unqualified-id. */
19941 identifier = cp_parser_unqualified_id (parser,
19942 /*template_keyword_p=*/false,
19943 /*check_dependency_p=*/true,
19944 /*declarator_p=*/true,
19945 /*optional_p=*/false);
19946
19947 if (access_declaration_p)
19948 {
19949 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19950 cp_parser_simulate_error (parser);
19951 if (!cp_parser_parse_definitely (parser))
19952 return false;
19953 }
19954 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19955 {
19956 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19957 if (cxx_dialect < cxx17)
19958 pedwarn (ell->location, 0,
19959 "pack expansion in using-declaration only available "
19960 "with %<-std=c++17%> or %<-std=gnu++17%>");
19961 qscope = make_pack_expansion (qscope);
19962 }
19963
19964 /* The function we call to handle a using-declaration is different
19965 depending on what scope we are in. */
19966 if (qscope == error_mark_node || identifier == error_mark_node)
19967 ;
19968 else if (!identifier_p (identifier)
19969 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19970 /* [namespace.udecl]
19971
19972 A using declaration shall not name a template-id. */
19973 error_at (token->location,
19974 "a template-id may not appear in a using-declaration");
19975 else
19976 {
19977 if (at_class_scope_p ())
19978 {
19979 /* Create the USING_DECL. */
19980 decl = do_class_using_decl (qscope, identifier);
19981
19982 if (decl && typename_p)
19983 USING_DECL_TYPENAME_P (decl) = 1;
19984
19985 if (check_for_bare_parameter_packs (decl))
19986 {
19987 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19988 return false;
19989 }
19990 else
19991 /* Add it to the list of members in this class. */
19992 finish_member_declaration (decl);
19993 }
19994 else
19995 finish_nonmember_using_decl (qscope, identifier);
19996 }
19997
19998 if (!access_declaration_p
19999 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20000 {
20001 cp_token *comma = cp_lexer_consume_token (parser->lexer);
20002 if (cxx_dialect < cxx17)
20003 pedwarn (comma->location, 0,
20004 "comma-separated list in using-declaration only available "
20005 "with %<-std=c++17%> or %<-std=gnu++17%>");
20006 goto again;
20007 }
20008
20009 /* Look for the final `;'. */
20010 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20011
20012 if (access_declaration_p && errorcount == oldcount)
20013 warning_at (diag_token->location, OPT_Wdeprecated,
20014 "access declarations are deprecated "
20015 "in favour of using-declarations; "
20016 "suggestion: add the %<using%> keyword");
20017
20018 return true;
20019 }
20020
20021 /* Parse an alias-declaration.
20022
20023 alias-declaration:
20024 using identifier attribute-specifier-seq [opt] = type-id */
20025
20026 static tree
20027 cp_parser_alias_declaration (cp_parser* parser)
20028 {
20029 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
20030 location_t id_location, type_location;
20031 cp_declarator *declarator;
20032 cp_decl_specifier_seq decl_specs;
20033 bool member_p;
20034 const char *saved_message = NULL;
20035
20036 /* Look for the `using' keyword. */
20037 cp_token *using_token
20038 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
20039 if (using_token == NULL)
20040 return error_mark_node;
20041
20042 id_location = cp_lexer_peek_token (parser->lexer)->location;
20043 id = cp_parser_identifier (parser);
20044 if (id == error_mark_node)
20045 return error_mark_node;
20046
20047 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
20048 attributes = cp_parser_attributes_opt (parser);
20049 if (attributes == error_mark_node)
20050 return error_mark_node;
20051
20052 cp_parser_require (parser, CPP_EQ, RT_EQ);
20053
20054 if (cp_parser_error_occurred (parser))
20055 return error_mark_node;
20056
20057 cp_parser_commit_to_tentative_parse (parser);
20058
20059 /* Now we are going to parse the type-id of the declaration. */
20060
20061 /*
20062 [dcl.type]/3 says:
20063
20064 "A type-specifier-seq shall not define a class or enumeration
20065 unless it appears in the type-id of an alias-declaration (7.1.3) that
20066 is not the declaration of a template-declaration."
20067
20068 In other words, if we currently are in an alias template, the
20069 type-id should not define a type.
20070
20071 So let's set parser->type_definition_forbidden_message in that
20072 case; cp_parser_check_type_definition (called by
20073 cp_parser_class_specifier) will then emit an error if a type is
20074 defined in the type-id. */
20075 if (parser->num_template_parameter_lists)
20076 {
20077 saved_message = parser->type_definition_forbidden_message;
20078 parser->type_definition_forbidden_message =
20079 G_("types may not be defined in alias template declarations");
20080 }
20081
20082 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
20083 &type_location);
20084
20085 /* Restore the error message if need be. */
20086 if (parser->num_template_parameter_lists)
20087 parser->type_definition_forbidden_message = saved_message;
20088
20089 if (type == error_mark_node
20090 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
20091 {
20092 cp_parser_skip_to_end_of_block_or_statement (parser);
20093 return error_mark_node;
20094 }
20095
20096 /* A typedef-name can also be introduced by an alias-declaration. The
20097 identifier following the using keyword becomes a typedef-name. It has
20098 the same semantics as if it were introduced by the typedef
20099 specifier. In particular, it does not define a new type and it shall
20100 not appear in the type-id. */
20101
20102 clear_decl_specs (&decl_specs);
20103 decl_specs.type = type;
20104 if (attributes != NULL_TREE)
20105 {
20106 decl_specs.attributes = attributes;
20107 set_and_check_decl_spec_loc (&decl_specs,
20108 ds_attribute,
20109 attrs_token);
20110 }
20111 set_and_check_decl_spec_loc (&decl_specs,
20112 ds_typedef,
20113 using_token);
20114 set_and_check_decl_spec_loc (&decl_specs,
20115 ds_alias,
20116 using_token);
20117 decl_specs.locations[ds_type_spec] = type_location;
20118
20119 if (parser->num_template_parameter_lists
20120 && !cp_parser_check_template_parameters (parser,
20121 /*num_templates=*/0,
20122 /*template_id*/false,
20123 id_location,
20124 /*declarator=*/NULL))
20125 return error_mark_node;
20126
20127 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
20128
20129 member_p = at_class_scope_p ();
20130 if (member_p)
20131 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
20132 NULL_TREE, attributes);
20133 else
20134 decl = start_decl (declarator, &decl_specs, 0,
20135 attributes, NULL_TREE, &pushed_scope);
20136 if (decl == error_mark_node)
20137 return decl;
20138
20139 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
20140
20141 if (pushed_scope)
20142 pop_scope (pushed_scope);
20143
20144 /* If decl is a template, return its TEMPLATE_DECL so that it gets
20145 added into the symbol table; otherwise, return the TYPE_DECL. */
20146 if (DECL_LANG_SPECIFIC (decl)
20147 && DECL_TEMPLATE_INFO (decl)
20148 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
20149 {
20150 decl = DECL_TI_TEMPLATE (decl);
20151 if (member_p)
20152 check_member_template (decl);
20153 }
20154
20155 return decl;
20156 }
20157
20158 /* Parse a using-directive.
20159
20160 using-directive:
20161 using namespace :: [opt] nested-name-specifier [opt]
20162 namespace-name ; */
20163
20164 static void
20165 cp_parser_using_directive (cp_parser* parser)
20166 {
20167 tree namespace_decl;
20168 tree attribs;
20169
20170 /* Look for the `using' keyword. */
20171 cp_parser_require_keyword (parser, RID_USING, RT_USING);
20172 /* And the `namespace' keyword. */
20173 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
20174 /* Look for the optional `::' operator. */
20175 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
20176 /* And the optional nested-name-specifier. */
20177 cp_parser_nested_name_specifier_opt (parser,
20178 /*typename_keyword_p=*/false,
20179 /*check_dependency_p=*/true,
20180 /*type_p=*/false,
20181 /*is_declaration=*/true);
20182 /* Get the namespace being used. */
20183 namespace_decl = cp_parser_namespace_name (parser);
20184 cp_warn_deprecated_use_scopes (namespace_decl);
20185 /* And any specified attributes. */
20186 attribs = cp_parser_attributes_opt (parser);
20187
20188 /* Update the symbol table. */
20189 finish_using_directive (namespace_decl, attribs);
20190
20191 /* Look for the final `;'. */
20192 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20193 }
20194
20195 /* Parse an asm-definition.
20196
20197 asm-qualifier:
20198 volatile
20199 inline
20200 goto
20201
20202 asm-qualifier-list:
20203 asm-qualifier
20204 asm-qualifier-list asm-qualifier
20205
20206 asm-definition:
20207 asm ( string-literal ) ;
20208
20209 GNU Extension:
20210
20211 asm-definition:
20212 asm asm-qualifier-list [opt] ( string-literal ) ;
20213 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
20214 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
20215 : asm-operand-list [opt] ) ;
20216 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
20217 : asm-operand-list [opt]
20218 : asm-clobber-list [opt] ) ;
20219 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
20220 : asm-clobber-list [opt]
20221 : asm-goto-list ) ;
20222
20223 The form with asm-goto-list is valid if and only if the asm-qualifier-list
20224 contains goto, and is the only allowed form in that case. No duplicates are
20225 allowed in an asm-qualifier-list. */
20226
20227 static void
20228 cp_parser_asm_definition (cp_parser* parser)
20229 {
20230 tree string;
20231 tree outputs = NULL_TREE;
20232 tree inputs = NULL_TREE;
20233 tree clobbers = NULL_TREE;
20234 tree labels = NULL_TREE;
20235 tree asm_stmt;
20236 bool extended_p = false;
20237 bool invalid_inputs_p = false;
20238 bool invalid_outputs_p = false;
20239 required_token missing = RT_NONE;
20240 location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
20241
20242 /* Look for the `asm' keyword. */
20243 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
20244
20245 /* In C++20, unevaluated inline assembly is permitted in constexpr
20246 functions. */
20247 if (parser->in_function_body
20248 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
20249 && (cxx_dialect < cxx20))
20250 pedwarn (asm_loc, 0, "%<asm%> in %<constexpr%> function only available "
20251 "with %<-std=c++20%> or %<-std=gnu++20%>");
20252
20253 /* Handle the asm-qualifier-list. */
20254 location_t volatile_loc = UNKNOWN_LOCATION;
20255 location_t inline_loc = UNKNOWN_LOCATION;
20256 location_t goto_loc = UNKNOWN_LOCATION;
20257 location_t first_loc = UNKNOWN_LOCATION;
20258
20259 if (cp_parser_allow_gnu_extensions_p (parser))
20260 for (;;)
20261 {
20262 cp_token *token = cp_lexer_peek_token (parser->lexer);
20263 location_t loc = token->location;
20264 switch (cp_lexer_peek_token (parser->lexer)->keyword)
20265 {
20266 case RID_VOLATILE:
20267 if (volatile_loc)
20268 {
20269 error_at (loc, "duplicate %<asm%> qualifier %qT",
20270 token->u.value);
20271 inform (volatile_loc, "first seen here");
20272 }
20273 else
20274 {
20275 if (!parser->in_function_body)
20276 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
20277 "outside of function body", token->u.value);
20278 volatile_loc = loc;
20279 }
20280 cp_lexer_consume_token (parser->lexer);
20281 continue;
20282
20283 case RID_INLINE:
20284 if (inline_loc)
20285 {
20286 error_at (loc, "duplicate %<asm%> qualifier %qT",
20287 token->u.value);
20288 inform (inline_loc, "first seen here");
20289 }
20290 else
20291 inline_loc = loc;
20292 if (!first_loc)
20293 first_loc = loc;
20294 cp_lexer_consume_token (parser->lexer);
20295 continue;
20296
20297 case RID_GOTO:
20298 if (goto_loc)
20299 {
20300 error_at (loc, "duplicate %<asm%> qualifier %qT",
20301 token->u.value);
20302 inform (goto_loc, "first seen here");
20303 }
20304 else
20305 goto_loc = loc;
20306 if (!first_loc)
20307 first_loc = loc;
20308 cp_lexer_consume_token (parser->lexer);
20309 continue;
20310
20311 case RID_CONST:
20312 case RID_RESTRICT:
20313 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
20314 cp_lexer_consume_token (parser->lexer);
20315 continue;
20316
20317 default:
20318 break;
20319 }
20320 break;
20321 }
20322
20323 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
20324 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
20325 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
20326
20327 if (!parser->in_function_body && (inline_p || goto_p))
20328 {
20329 error_at (first_loc, "%<asm%> qualifier outside of function body");
20330 inline_p = goto_p = false;
20331 }
20332
20333 /* Look for the opening `('. */
20334 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20335 return;
20336 /* Look for the string. */
20337 string = cp_parser_string_literal (parser, false, false);
20338 if (string == error_mark_node)
20339 {
20340 cp_parser_skip_to_closing_parenthesis (parser, true, false,
20341 /*consume_paren=*/true);
20342 return;
20343 }
20344
20345 /* If we're allowing GNU extensions, check for the extended assembly
20346 syntax. Unfortunately, the `:' tokens need not be separated by
20347 a space in C, and so, for compatibility, we tolerate that here
20348 too. Doing that means that we have to treat the `::' operator as
20349 two `:' tokens. */
20350 if (cp_parser_allow_gnu_extensions_p (parser)
20351 && parser->in_function_body
20352 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
20353 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
20354 {
20355 bool inputs_p = false;
20356 bool clobbers_p = false;
20357 bool labels_p = false;
20358
20359 /* The extended syntax was used. */
20360 extended_p = true;
20361
20362 /* Look for outputs. */
20363 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20364 {
20365 /* Consume the `:'. */
20366 cp_lexer_consume_token (parser->lexer);
20367 /* Parse the output-operands. */
20368 if (cp_lexer_next_token_is_not (parser->lexer,
20369 CPP_COLON)
20370 && cp_lexer_next_token_is_not (parser->lexer,
20371 CPP_SCOPE)
20372 && cp_lexer_next_token_is_not (parser->lexer,
20373 CPP_CLOSE_PAREN)
20374 && !goto_p)
20375 {
20376 outputs = cp_parser_asm_operand_list (parser);
20377 if (outputs == error_mark_node)
20378 invalid_outputs_p = true;
20379 }
20380 }
20381 /* If the next token is `::', there are no outputs, and the
20382 next token is the beginning of the inputs. */
20383 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20384 /* The inputs are coming next. */
20385 inputs_p = true;
20386
20387 /* Look for inputs. */
20388 if (inputs_p
20389 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20390 {
20391 /* Consume the `:' or `::'. */
20392 cp_lexer_consume_token (parser->lexer);
20393 /* Parse the output-operands. */
20394 if (cp_lexer_next_token_is_not (parser->lexer,
20395 CPP_COLON)
20396 && cp_lexer_next_token_is_not (parser->lexer,
20397 CPP_SCOPE)
20398 && cp_lexer_next_token_is_not (parser->lexer,
20399 CPP_CLOSE_PAREN))
20400 {
20401 inputs = cp_parser_asm_operand_list (parser);
20402 if (inputs == error_mark_node)
20403 invalid_inputs_p = true;
20404 }
20405 }
20406 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20407 /* The clobbers are coming next. */
20408 clobbers_p = true;
20409
20410 /* Look for clobbers. */
20411 if (clobbers_p
20412 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20413 {
20414 clobbers_p = true;
20415 /* Consume the `:' or `::'. */
20416 cp_lexer_consume_token (parser->lexer);
20417 /* Parse the clobbers. */
20418 if (cp_lexer_next_token_is_not (parser->lexer,
20419 CPP_COLON)
20420 && cp_lexer_next_token_is_not (parser->lexer,
20421 CPP_CLOSE_PAREN))
20422 clobbers = cp_parser_asm_clobber_list (parser);
20423 }
20424 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20425 /* The labels are coming next. */
20426 labels_p = true;
20427
20428 /* Look for labels. */
20429 if (labels_p
20430 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
20431 {
20432 labels_p = true;
20433 /* Consume the `:' or `::'. */
20434 cp_lexer_consume_token (parser->lexer);
20435 /* Parse the labels. */
20436 labels = cp_parser_asm_label_list (parser);
20437 }
20438
20439 if (goto_p && !labels_p)
20440 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
20441 }
20442 else if (goto_p)
20443 missing = RT_COLON_SCOPE;
20444
20445 /* Look for the closing `)'. */
20446 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
20447 missing ? missing : RT_CLOSE_PAREN))
20448 cp_parser_skip_to_closing_parenthesis (parser, true, false,
20449 /*consume_paren=*/true);
20450 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20451
20452 if (!invalid_inputs_p && !invalid_outputs_p)
20453 {
20454 /* Create the ASM_EXPR. */
20455 if (parser->in_function_body)
20456 {
20457 asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
20458 inputs, clobbers, labels, inline_p);
20459 /* If the extended syntax was not used, mark the ASM_EXPR. */
20460 if (!extended_p)
20461 {
20462 tree temp = asm_stmt;
20463 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
20464 temp = TREE_OPERAND (temp, 0);
20465
20466 ASM_INPUT_P (temp) = 1;
20467 }
20468 }
20469 else
20470 symtab->finalize_toplevel_asm (string);
20471 }
20472 }
20473
20474 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
20475 type that comes from the decl-specifier-seq. */
20476
20477 static tree
20478 strip_declarator_types (tree type, cp_declarator *declarator)
20479 {
20480 for (cp_declarator *d = declarator; d;)
20481 switch (d->kind)
20482 {
20483 case cdk_id:
20484 case cdk_decomp:
20485 case cdk_error:
20486 d = NULL;
20487 break;
20488
20489 default:
20490 if (TYPE_PTRMEMFUNC_P (type))
20491 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
20492 type = TREE_TYPE (type);
20493 d = d->declarator;
20494 break;
20495 }
20496
20497 return type;
20498 }
20499
20500 /* Declarators [gram.dcl.decl] */
20501
20502 /* Parse an init-declarator.
20503
20504 init-declarator:
20505 declarator initializer [opt]
20506
20507 GNU Extension:
20508
20509 init-declarator:
20510 declarator asm-specification [opt] attributes [opt] initializer [opt]
20511
20512 function-definition:
20513 decl-specifier-seq [opt] declarator ctor-initializer [opt]
20514 function-body
20515 decl-specifier-seq [opt] declarator function-try-block
20516
20517 GNU Extension:
20518
20519 function-definition:
20520 __extension__ function-definition
20521
20522 TM Extension:
20523
20524 function-definition:
20525 decl-specifier-seq [opt] declarator function-transaction-block
20526
20527 The parser flags FLAGS is used to control type-specifier parsing.
20528
20529 The DECL_SPECIFIERS apply to this declarator. Returns a
20530 representation of the entity declared. If MEMBER_P is TRUE, then
20531 this declarator appears in a class scope. The new DECL created by
20532 this declarator is returned.
20533
20534 The CHECKS are access checks that should be performed once we know
20535 what entity is being declared (and, therefore, what classes have
20536 befriended it).
20537
20538 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20539 for a function-definition here as well. If the declarator is a
20540 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20541 be TRUE upon return. By that point, the function-definition will
20542 have been completely parsed.
20543
20544 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20545 is FALSE.
20546
20547 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20548 parsed declaration if it is an uninitialized single declarator not followed
20549 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20550 if present, will not be consumed. If returned, this declarator will be
20551 created with SD_INITIALIZED but will not call cp_finish_decl.
20552
20553 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20554 and there is an initializer, the pointed location_t is set to the
20555 location of the '=' or `(', or '{' in C++11 token introducing the
20556 initializer. */
20557
20558 static tree
20559 cp_parser_init_declarator (cp_parser* parser,
20560 cp_parser_flags flags,
20561 cp_decl_specifier_seq *decl_specifiers,
20562 vec<deferred_access_check, va_gc> *checks,
20563 bool function_definition_allowed_p,
20564 bool member_p,
20565 int declares_class_or_enum,
20566 bool* function_definition_p,
20567 tree* maybe_range_for_decl,
20568 location_t* init_loc,
20569 tree* auto_result)
20570 {
20571 cp_token *token = NULL, *asm_spec_start_token = NULL,
20572 *attributes_start_token = NULL;
20573 cp_declarator *declarator;
20574 tree prefix_attributes;
20575 tree attributes = NULL;
20576 tree asm_specification;
20577 tree initializer;
20578 tree decl = NULL_TREE;
20579 tree scope;
20580 int is_initialized;
20581 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20582 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20583 "(...)". */
20584 enum cpp_ttype initialization_kind;
20585 bool is_direct_init = false;
20586 bool is_non_constant_init;
20587 int ctor_dtor_or_conv_p;
20588 bool friend_p = cp_parser_friend_p (decl_specifiers);
20589 tree pushed_scope = NULL_TREE;
20590 bool range_for_decl_p = false;
20591 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20592 location_t tmp_init_loc = UNKNOWN_LOCATION;
20593
20594 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
20595 flags |= CP_PARSER_FLAGS_CONSTEVAL;
20596
20597 /* Gather the attributes that were provided with the
20598 decl-specifiers. */
20599 prefix_attributes = decl_specifiers->attributes;
20600
20601 /* Assume that this is not the declarator for a function
20602 definition. */
20603 if (function_definition_p)
20604 *function_definition_p = false;
20605
20606 /* Default arguments are only permitted for function parameters. */
20607 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
20608 parser->default_arg_ok_p = false;
20609
20610 /* Defer access checks while parsing the declarator; we cannot know
20611 what names are accessible until we know what is being
20612 declared. */
20613 resume_deferring_access_checks ();
20614
20615 token = cp_lexer_peek_token (parser->lexer);
20616
20617 /* Parse the declarator. */
20618 declarator
20619 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20620 flags, &ctor_dtor_or_conv_p,
20621 /*parenthesized_p=*/NULL,
20622 member_p, friend_p, /*static_p=*/false);
20623 /* Gather up the deferred checks. */
20624 stop_deferring_access_checks ();
20625
20626 parser->default_arg_ok_p = saved_default_arg_ok_p;
20627
20628 /* If the DECLARATOR was erroneous, there's no need to go
20629 further. */
20630 if (declarator == cp_error_declarator)
20631 return error_mark_node;
20632
20633 /* Check that the number of template-parameter-lists is OK. */
20634 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
20635 token->location))
20636 return error_mark_node;
20637
20638 if (declares_class_or_enum & 2)
20639 cp_parser_check_for_definition_in_return_type (declarator,
20640 decl_specifiers->type,
20641 decl_specifiers->locations[ds_type_spec]);
20642
20643 /* Figure out what scope the entity declared by the DECLARATOR is
20644 located in. `grokdeclarator' sometimes changes the scope, so
20645 we compute it now. */
20646 scope = get_scope_of_declarator (declarator);
20647
20648 /* Perform any lookups in the declared type which were thought to be
20649 dependent, but are not in the scope of the declarator. */
20650 decl_specifiers->type
20651 = maybe_update_decl_type (decl_specifiers->type, scope);
20652
20653 /* If we're allowing GNU extensions, look for an
20654 asm-specification. */
20655 if (cp_parser_allow_gnu_extensions_p (parser))
20656 {
20657 /* Look for an asm-specification. */
20658 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
20659 asm_specification = cp_parser_asm_specification_opt (parser);
20660 }
20661 else
20662 asm_specification = NULL_TREE;
20663
20664 /* Look for attributes. */
20665 attributes_start_token = cp_lexer_peek_token (parser->lexer);
20666 attributes = cp_parser_attributes_opt (parser);
20667
20668 /* Peek at the next token. */
20669 token = cp_lexer_peek_token (parser->lexer);
20670
20671 bool bogus_implicit_tmpl = false;
20672
20673 if (function_declarator_p (declarator))
20674 {
20675 /* Handle C++17 deduction guides. */
20676 if (!decl_specifiers->type
20677 && ctor_dtor_or_conv_p <= 0
20678 && cxx_dialect >= cxx17)
20679 {
20680 cp_declarator *id = get_id_declarator (declarator);
20681 tree name = id->u.id.unqualified_name;
20682 parser->scope = id->u.id.qualifying_scope;
20683 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
20684 if (tmpl
20685 && (DECL_CLASS_TEMPLATE_P (tmpl)
20686 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
20687 {
20688 id->u.id.unqualified_name = dguide_name (tmpl);
20689 id->u.id.sfk = sfk_deduction_guide;
20690 ctor_dtor_or_conv_p = 1;
20691 }
20692 }
20693
20694 /* Check to see if the token indicates the start of a
20695 function-definition. */
20696 if (cp_parser_token_starts_function_definition_p (token))
20697 {
20698 if (!function_definition_allowed_p)
20699 {
20700 /* If a function-definition should not appear here, issue an
20701 error message. */
20702 cp_parser_error (parser,
20703 "a function-definition is not allowed here");
20704 return error_mark_node;
20705 }
20706
20707 location_t func_brace_location
20708 = cp_lexer_peek_token (parser->lexer)->location;
20709
20710 /* Neither attributes nor an asm-specification are allowed
20711 on a function-definition. */
20712 if (asm_specification)
20713 error_at (asm_spec_start_token->location,
20714 "an %<asm%> specification is not allowed "
20715 "on a function-definition");
20716 if (attributes)
20717 error_at (attributes_start_token->location,
20718 "attributes are not allowed "
20719 "on a function-definition");
20720 /* This is a function-definition. */
20721 *function_definition_p = true;
20722
20723 /* Parse the function definition. */
20724 if (member_p)
20725 decl = cp_parser_save_member_function_body (parser,
20726 decl_specifiers,
20727 declarator,
20728 prefix_attributes);
20729 else
20730 decl =
20731 (cp_parser_function_definition_from_specifiers_and_declarator
20732 (parser, decl_specifiers, prefix_attributes, declarator));
20733
20734 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
20735 {
20736 /* This is where the prologue starts... */
20737 DECL_STRUCT_FUNCTION (decl)->function_start_locus
20738 = func_brace_location;
20739 }
20740
20741 return decl;
20742 }
20743 }
20744 else if (parser->fully_implicit_function_template_p)
20745 {
20746 /* A non-template declaration involving a function parameter list
20747 containing an implicit template parameter will be made into a
20748 template. If the resulting declaration is not going to be an
20749 actual function then finish the template scope here to prevent it.
20750 An error message will be issued once we have a decl to talk about.
20751
20752 FIXME probably we should do type deduction rather than create an
20753 implicit template, but the standard currently doesn't allow it. */
20754 bogus_implicit_tmpl = true;
20755 finish_fully_implicit_template (parser, NULL_TREE);
20756 }
20757
20758 /* [dcl.dcl]
20759
20760 Only in function declarations for constructors, destructors, type
20761 conversions, and deduction guides can the decl-specifier-seq be omitted.
20762
20763 We explicitly postpone this check past the point where we handle
20764 function-definitions because we tolerate function-definitions
20765 that are missing their return types in some modes. */
20766 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
20767 {
20768 cp_parser_error (parser,
20769 "expected constructor, destructor, or type conversion");
20770 return error_mark_node;
20771 }
20772
20773 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20774 if (token->type == CPP_EQ
20775 || token->type == CPP_OPEN_PAREN
20776 || token->type == CPP_OPEN_BRACE)
20777 {
20778 is_initialized = SD_INITIALIZED;
20779 initialization_kind = token->type;
20780 if (maybe_range_for_decl)
20781 *maybe_range_for_decl = error_mark_node;
20782 tmp_init_loc = token->location;
20783 if (init_loc && *init_loc == UNKNOWN_LOCATION)
20784 *init_loc = tmp_init_loc;
20785
20786 if (token->type == CPP_EQ
20787 && function_declarator_p (declarator))
20788 {
20789 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
20790 if (t2->keyword == RID_DEFAULT)
20791 is_initialized = SD_DEFAULTED;
20792 else if (t2->keyword == RID_DELETE)
20793 is_initialized = SD_DELETED;
20794 }
20795 }
20796 else
20797 {
20798 /* If the init-declarator isn't initialized and isn't followed by a
20799 `,' or `;', it's not a valid init-declarator. */
20800 if (token->type != CPP_COMMA
20801 && token->type != CPP_SEMICOLON)
20802 {
20803 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
20804 range_for_decl_p = true;
20805 else
20806 {
20807 if (!maybe_range_for_decl)
20808 cp_parser_error (parser, "expected initializer");
20809 return error_mark_node;
20810 }
20811 }
20812 is_initialized = SD_UNINITIALIZED;
20813 initialization_kind = CPP_EOF;
20814 }
20815
20816 /* Because start_decl has side-effects, we should only call it if we
20817 know we're going ahead. By this point, we know that we cannot
20818 possibly be looking at any other construct. */
20819 cp_parser_commit_to_tentative_parse (parser);
20820
20821 /* Enter the newly declared entry in the symbol table. If we're
20822 processing a declaration in a class-specifier, we wait until
20823 after processing the initializer. */
20824 if (!member_p)
20825 {
20826 if (parser->in_unbraced_linkage_specification_p)
20827 decl_specifiers->storage_class = sc_extern;
20828 decl = start_decl (declarator, decl_specifiers,
20829 range_for_decl_p? SD_INITIALIZED : is_initialized,
20830 attributes, prefix_attributes, &pushed_scope);
20831 cp_finalize_omp_declare_simd (parser, decl);
20832 cp_finalize_oacc_routine (parser, decl, false);
20833 /* Adjust location of decl if declarator->id_loc is more appropriate:
20834 set, and decl wasn't merged with another decl, in which case its
20835 location would be different from input_location, and more accurate. */
20836 if (DECL_P (decl)
20837 && declarator->id_loc != UNKNOWN_LOCATION
20838 && DECL_SOURCE_LOCATION (decl) == input_location)
20839 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
20840 }
20841 else if (scope)
20842 /* Enter the SCOPE. That way unqualified names appearing in the
20843 initializer will be looked up in SCOPE. */
20844 pushed_scope = push_scope (scope);
20845
20846 /* Perform deferred access control checks, now that we know in which
20847 SCOPE the declared entity resides. */
20848 if (!member_p && decl)
20849 {
20850 tree saved_current_function_decl = NULL_TREE;
20851
20852 /* If the entity being declared is a function, pretend that we
20853 are in its scope. If it is a `friend', it may have access to
20854 things that would not otherwise be accessible. */
20855 if (TREE_CODE (decl) == FUNCTION_DECL)
20856 {
20857 saved_current_function_decl = current_function_decl;
20858 current_function_decl = decl;
20859 }
20860
20861 /* Perform access checks for template parameters. */
20862 cp_parser_perform_template_parameter_access_checks (checks);
20863
20864 /* Perform the access control checks for the declarator and the
20865 decl-specifiers. */
20866 perform_deferred_access_checks (tf_warning_or_error);
20867
20868 /* Restore the saved value. */
20869 if (TREE_CODE (decl) == FUNCTION_DECL)
20870 current_function_decl = saved_current_function_decl;
20871 }
20872
20873 /* Parse the initializer. */
20874 initializer = NULL_TREE;
20875 is_direct_init = false;
20876 is_non_constant_init = true;
20877 if (is_initialized)
20878 {
20879 if (function_declarator_p (declarator))
20880 {
20881 if (initialization_kind == CPP_EQ)
20882 initializer = cp_parser_pure_specifier (parser);
20883 else
20884 {
20885 /* If the declaration was erroneous, we don't really
20886 know what the user intended, so just silently
20887 consume the initializer. */
20888 if (decl != error_mark_node)
20889 error_at (tmp_init_loc, "initializer provided for function");
20890 cp_parser_skip_to_closing_parenthesis (parser,
20891 /*recovering=*/true,
20892 /*or_comma=*/false,
20893 /*consume_paren=*/true);
20894 }
20895 }
20896 else
20897 {
20898 /* We want to record the extra mangling scope for in-class
20899 initializers of class members and initializers of static
20900 data member templates and namespace-scope initializers.
20901 The former involves deferring parsing of the initializer
20902 until end of class as with default arguments. So right
20903 here we only handle the latter two. */
20904 bool has_lambda_scope = false;
20905
20906 if (decl != error_mark_node
20907 && !member_p
20908 && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
20909 has_lambda_scope = true;
20910
20911 if (has_lambda_scope)
20912 start_lambda_scope (decl);
20913 initializer = cp_parser_initializer (parser,
20914 &is_direct_init,
20915 &is_non_constant_init);
20916 if (has_lambda_scope)
20917 finish_lambda_scope ();
20918 if (initializer == error_mark_node)
20919 cp_parser_skip_to_end_of_statement (parser);
20920 }
20921 }
20922
20923 /* The old parser allows attributes to appear after a parenthesized
20924 initializer. Mark Mitchell proposed removing this functionality
20925 on the GCC mailing lists on 2002-08-13. This parser accepts the
20926 attributes -- but ignores them. Made a permerror in GCC 8. */
20927 if (cp_parser_allow_gnu_extensions_p (parser)
20928 && initialization_kind == CPP_OPEN_PAREN
20929 && cp_parser_attributes_opt (parser)
20930 && permerror (input_location,
20931 "attributes after parenthesized initializer ignored"))
20932 {
20933 static bool hint;
20934 if (flag_permissive && !hint)
20935 {
20936 hint = true;
20937 inform (input_location,
20938 "this flexibility is deprecated and will be removed");
20939 }
20940 }
20941
20942 /* And now complain about a non-function implicit template. */
20943 if (bogus_implicit_tmpl && decl != error_mark_node)
20944 error_at (DECL_SOURCE_LOCATION (decl),
20945 "non-function %qD declared as implicit template", decl);
20946
20947 /* For an in-class declaration, use `grokfield' to create the
20948 declaration. */
20949 if (member_p)
20950 {
20951 if (pushed_scope)
20952 {
20953 pop_scope (pushed_scope);
20954 pushed_scope = NULL_TREE;
20955 }
20956 decl = grokfield (declarator, decl_specifiers,
20957 initializer, !is_non_constant_init,
20958 /*asmspec=*/NULL_TREE,
20959 attr_chainon (attributes, prefix_attributes));
20960 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20961 cp_parser_save_default_args (parser, decl);
20962 cp_finalize_omp_declare_simd (parser, decl);
20963 cp_finalize_oacc_routine (parser, decl, false);
20964 }
20965
20966 /* Finish processing the declaration. But, skip member
20967 declarations. */
20968 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20969 {
20970 int cf = (decl_spec_seq_has_spec_p (decl_specifiers, ds_constinit)
20971 ? LOOKUP_CONSTINIT : 0);
20972 cp_finish_decl (decl,
20973 initializer, !is_non_constant_init,
20974 asm_specification,
20975 /* If the initializer is in parentheses, then this is
20976 a direct-initialization, which means that an
20977 `explicit' constructor is OK. Otherwise, an
20978 `explicit' constructor cannot be used. */
20979 ((is_direct_init || !is_initialized)
20980 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT) | cf);
20981 }
20982 else if ((cxx_dialect != cxx98) && friend_p
20983 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20984 /* Core issue #226 (C++0x only): A default template-argument
20985 shall not be specified in a friend class template
20986 declaration. */
20987 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20988 /*is_partial=*/false, /*is_friend_decl=*/1);
20989
20990 if (!friend_p && pushed_scope)
20991 pop_scope (pushed_scope);
20992
20993 if (function_declarator_p (declarator)
20994 && parser->fully_implicit_function_template_p)
20995 {
20996 if (member_p)
20997 decl = finish_fully_implicit_template (parser, decl);
20998 else
20999 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
21000 }
21001
21002 if (auto_result && is_initialized && decl_specifiers->type
21003 && type_uses_auto (decl_specifiers->type))
21004 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
21005
21006 return decl;
21007 }
21008
21009 /* Parse a declarator.
21010
21011 declarator:
21012 direct-declarator
21013 ptr-operator declarator
21014
21015 abstract-declarator:
21016 ptr-operator abstract-declarator [opt]
21017 direct-abstract-declarator
21018
21019 GNU Extensions:
21020
21021 declarator:
21022 attributes [opt] direct-declarator
21023 attributes [opt] ptr-operator declarator
21024
21025 abstract-declarator:
21026 attributes [opt] ptr-operator abstract-declarator [opt]
21027 attributes [opt] direct-abstract-declarator
21028
21029 The parser flags FLAGS is used to control type-specifier parsing.
21030
21031 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
21032 detect constructors, destructors, deduction guides, or conversion operators.
21033 It is set to -1 if the declarator is a name, and +1 if it is a
21034 function. Otherwise it is set to zero. Usually you just want to
21035 test for >0, but internally the negative value is used.
21036
21037 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
21038 a decl-specifier-seq unless it declares a constructor, destructor,
21039 or conversion. It might seem that we could check this condition in
21040 semantic analysis, rather than parsing, but that makes it difficult
21041 to handle something like `f()'. We want to notice that there are
21042 no decl-specifiers, and therefore realize that this is an
21043 expression, not a declaration.)
21044
21045 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
21046 the declarator is a direct-declarator of the form "(...)".
21047
21048 MEMBER_P is true iff this declarator is a member-declarator.
21049
21050 FRIEND_P is true iff this declarator is a friend.
21051
21052 STATIC_P is true iff the keyword static was seen. */
21053
21054 static cp_declarator *
21055 cp_parser_declarator (cp_parser* parser,
21056 cp_parser_declarator_kind dcl_kind,
21057 cp_parser_flags flags,
21058 int* ctor_dtor_or_conv_p,
21059 bool* parenthesized_p,
21060 bool member_p, bool friend_p, bool static_p)
21061 {
21062 cp_declarator *declarator;
21063 enum tree_code code;
21064 cp_cv_quals cv_quals;
21065 tree class_type;
21066 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
21067
21068 /* Assume this is not a constructor, destructor, or type-conversion
21069 operator. */
21070 if (ctor_dtor_or_conv_p)
21071 *ctor_dtor_or_conv_p = 0;
21072
21073 if (cp_parser_allow_gnu_extensions_p (parser))
21074 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
21075
21076 /* Check for the ptr-operator production. */
21077 cp_parser_parse_tentatively (parser);
21078 /* Parse the ptr-operator. */
21079 code = cp_parser_ptr_operator (parser,
21080 &class_type,
21081 &cv_quals,
21082 &std_attributes);
21083
21084 /* If that worked, then we have a ptr-operator. */
21085 if (cp_parser_parse_definitely (parser))
21086 {
21087 /* If a ptr-operator was found, then this declarator was not
21088 parenthesized. */
21089 if (parenthesized_p)
21090 *parenthesized_p = true;
21091 /* The dependent declarator is optional if we are parsing an
21092 abstract-declarator. */
21093 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
21094 cp_parser_parse_tentatively (parser);
21095
21096 /* Parse the dependent declarator. */
21097 declarator = cp_parser_declarator (parser, dcl_kind,
21098 CP_PARSER_FLAGS_NONE,
21099 /*ctor_dtor_or_conv_p=*/NULL,
21100 /*parenthesized_p=*/NULL,
21101 /*member_p=*/false,
21102 friend_p, /*static_p=*/false);
21103
21104 /* If we are parsing an abstract-declarator, we must handle the
21105 case where the dependent declarator is absent. */
21106 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
21107 && !cp_parser_parse_definitely (parser))
21108 declarator = NULL;
21109
21110 declarator = cp_parser_make_indirect_declarator
21111 (code, class_type, cv_quals, declarator, std_attributes);
21112 }
21113 /* Everything else is a direct-declarator. */
21114 else
21115 {
21116 if (parenthesized_p)
21117 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
21118 CPP_OPEN_PAREN);
21119 declarator = cp_parser_direct_declarator (parser, dcl_kind,
21120 flags, ctor_dtor_or_conv_p,
21121 member_p, friend_p, static_p);
21122 }
21123
21124 if (gnu_attributes && declarator && declarator != cp_error_declarator)
21125 declarator->attributes = gnu_attributes;
21126 return declarator;
21127 }
21128
21129 /* Parse a direct-declarator or direct-abstract-declarator.
21130
21131 direct-declarator:
21132 declarator-id
21133 direct-declarator ( parameter-declaration-clause )
21134 cv-qualifier-seq [opt]
21135 ref-qualifier [opt]
21136 exception-specification [opt]
21137 direct-declarator [ constant-expression [opt] ]
21138 ( declarator )
21139
21140 direct-abstract-declarator:
21141 direct-abstract-declarator [opt]
21142 ( parameter-declaration-clause )
21143 cv-qualifier-seq [opt]
21144 ref-qualifier [opt]
21145 exception-specification [opt]
21146 direct-abstract-declarator [opt] [ constant-expression [opt] ]
21147 ( abstract-declarator )
21148
21149 Returns a representation of the declarator. DCL_KIND is
21150 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
21151 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
21152 we are parsing a direct-declarator. It is
21153 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
21154 of ambiguity we prefer an abstract declarator, as per
21155 [dcl.ambig.res].
21156 The parser flags FLAGS is used to control type-specifier parsing.
21157 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
21158 as for cp_parser_declarator. */
21159
21160 static cp_declarator *
21161 cp_parser_direct_declarator (cp_parser* parser,
21162 cp_parser_declarator_kind dcl_kind,
21163 cp_parser_flags flags,
21164 int* ctor_dtor_or_conv_p,
21165 bool member_p, bool friend_p, bool static_p)
21166 {
21167 cp_token *token;
21168 cp_declarator *declarator = NULL;
21169 tree scope = NULL_TREE;
21170 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21171 bool saved_in_declarator_p = parser->in_declarator_p;
21172 bool first = true;
21173 tree pushed_scope = NULL_TREE;
21174 cp_token *open_paren = NULL, *close_paren = NULL;
21175
21176 while (true)
21177 {
21178 /* Peek at the next token. */
21179 token = cp_lexer_peek_token (parser->lexer);
21180 if (token->type == CPP_OPEN_PAREN)
21181 {
21182 /* This is either a parameter-declaration-clause, or a
21183 parenthesized declarator. When we know we are parsing a
21184 named declarator, it must be a parenthesized declarator
21185 if FIRST is true. For instance, `(int)' is a
21186 parameter-declaration-clause, with an omitted
21187 direct-abstract-declarator. But `((*))', is a
21188 parenthesized abstract declarator. Finally, when T is a
21189 template parameter `(T)' is a
21190 parameter-declaration-clause, and not a parenthesized
21191 named declarator.
21192
21193 We first try and parse a parameter-declaration-clause,
21194 and then try a nested declarator (if FIRST is true).
21195
21196 It is not an error for it not to be a
21197 parameter-declaration-clause, even when FIRST is
21198 false. Consider,
21199
21200 int i (int);
21201 int i (3);
21202
21203 The first is the declaration of a function while the
21204 second is the definition of a variable, including its
21205 initializer.
21206
21207 Having seen only the parenthesis, we cannot know which of
21208 these two alternatives should be selected. Even more
21209 complex are examples like:
21210
21211 int i (int (a));
21212 int i (int (3));
21213
21214 The former is a function-declaration; the latter is a
21215 variable initialization.
21216
21217 Thus again, we try a parameter-declaration-clause, and if
21218 that fails, we back out and return. */
21219
21220 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
21221 {
21222 tree params;
21223 bool is_declarator = false;
21224
21225 open_paren = NULL;
21226
21227 /* In a member-declarator, the only valid interpretation
21228 of a parenthesis is the start of a
21229 parameter-declaration-clause. (It is invalid to
21230 initialize a static data member with a parenthesized
21231 initializer; only the "=" form of initialization is
21232 permitted.) */
21233 if (!member_p)
21234 cp_parser_parse_tentatively (parser);
21235
21236 /* Consume the `('. */
21237 matching_parens parens;
21238 parens.consume_open (parser);
21239 if (first)
21240 {
21241 /* If this is going to be an abstract declarator, we're
21242 in a declarator and we can't have default args. */
21243 parser->default_arg_ok_p = false;
21244 parser->in_declarator_p = true;
21245 }
21246
21247 begin_scope (sk_function_parms, NULL_TREE);
21248
21249 /* Signal we are in the immediate function context. */
21250 if (flags & CP_PARSER_FLAGS_CONSTEVAL)
21251 current_binding_level->immediate_fn_ctx_p = true;
21252
21253 /* Parse the parameter-declaration-clause. */
21254 params
21255 = cp_parser_parameter_declaration_clause (parser, flags);
21256
21257 /* Consume the `)'. */
21258 parens.require_close (parser);
21259
21260 /* If all went well, parse the cv-qualifier-seq,
21261 ref-qualifier and the exception-specification. */
21262 if (member_p || cp_parser_parse_definitely (parser))
21263 {
21264 cp_cv_quals cv_quals;
21265 cp_virt_specifiers virt_specifiers;
21266 cp_ref_qualifier ref_qual;
21267 tree exception_specification;
21268 tree late_return;
21269 tree attrs;
21270 bool memfn = (member_p || (pushed_scope
21271 && CLASS_TYPE_P (pushed_scope)));
21272 unsigned char local_variables_forbidden_p
21273 = parser->local_variables_forbidden_p;
21274 /* 'this' is not allowed in static member functions. */
21275 if (static_p || friend_p)
21276 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
21277
21278 is_declarator = true;
21279
21280 if (ctor_dtor_or_conv_p)
21281 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
21282 first = false;
21283
21284 /* Parse the cv-qualifier-seq. */
21285 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21286 /* Parse the ref-qualifier. */
21287 ref_qual = cp_parser_ref_qualifier_opt (parser);
21288 /* Parse the tx-qualifier. */
21289 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
21290
21291 tree save_ccp = current_class_ptr;
21292 tree save_ccr = current_class_ref;
21293 if (memfn)
21294 /* DR 1207: 'this' is in scope after the cv-quals. */
21295 inject_this_parameter (current_class_type, cv_quals);
21296
21297 /* If it turned out that this is e.g. a pointer to a
21298 function, we don't want to delay noexcept parsing. */
21299 if (declarator == NULL || declarator->kind != cdk_id)
21300 flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
21301
21302 /* Parse the exception-specification. */
21303 exception_specification
21304 = cp_parser_exception_specification_opt (parser,
21305 flags);
21306
21307 attrs = cp_parser_std_attribute_spec_seq (parser);
21308
21309 /* In here, we handle cases where attribute is used after
21310 the function declaration. For example:
21311 void func (int x) __attribute__((vector(..))); */
21312 tree gnu_attrs = NULL_TREE;
21313 tree requires_clause = NULL_TREE;
21314 late_return = (cp_parser_late_return_type_opt
21315 (parser, declarator, requires_clause));
21316
21317 /* Parse the virt-specifier-seq. */
21318 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21319
21320 /* Create the function-declarator. */
21321 declarator = make_call_declarator (declarator,
21322 params,
21323 cv_quals,
21324 virt_specifiers,
21325 ref_qual,
21326 tx_qual,
21327 exception_specification,
21328 late_return,
21329 requires_clause);
21330 declarator->std_attributes = attrs;
21331 declarator->attributes = gnu_attrs;
21332 /* Any subsequent parameter lists are to do with
21333 return type, so are not those of the declared
21334 function. */
21335 parser->default_arg_ok_p = false;
21336
21337 current_class_ptr = save_ccp;
21338 current_class_ref = save_ccr;
21339
21340 /* Restore the state of local_variables_forbidden_p. */
21341 parser->local_variables_forbidden_p
21342 = local_variables_forbidden_p;
21343 }
21344
21345 /* Remove the function parms from scope. */
21346 pop_bindings_and_leave_scope ();
21347
21348 if (is_declarator)
21349 /* Repeat the main loop. */
21350 continue;
21351 }
21352
21353 /* If this is the first, we can try a parenthesized
21354 declarator. */
21355 if (first)
21356 {
21357 bool saved_in_type_id_in_expr_p;
21358
21359 parser->default_arg_ok_p = saved_default_arg_ok_p;
21360 parser->in_declarator_p = saved_in_declarator_p;
21361
21362 open_paren = token;
21363 /* Consume the `('. */
21364 matching_parens parens;
21365 parens.consume_open (parser);
21366 /* Parse the nested declarator. */
21367 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21368 parser->in_type_id_in_expr_p = true;
21369 declarator
21370 = cp_parser_declarator (parser, dcl_kind, flags,
21371 ctor_dtor_or_conv_p,
21372 /*parenthesized_p=*/NULL,
21373 member_p, friend_p,
21374 /*static_p=*/false);
21375 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21376 first = false;
21377 /* Expect a `)'. */
21378 close_paren = cp_lexer_peek_token (parser->lexer);
21379 if (!parens.require_close (parser))
21380 declarator = cp_error_declarator;
21381 if (declarator == cp_error_declarator)
21382 break;
21383
21384 goto handle_declarator;
21385 }
21386 /* Otherwise, we must be done. */
21387 else
21388 break;
21389 }
21390 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
21391 && token->type == CPP_OPEN_SQUARE
21392 && !cp_next_tokens_can_be_attribute_p (parser))
21393 {
21394 /* Parse an array-declarator. */
21395 tree bounds, attrs;
21396
21397 if (ctor_dtor_or_conv_p)
21398 *ctor_dtor_or_conv_p = 0;
21399
21400 open_paren = NULL;
21401 first = false;
21402 parser->default_arg_ok_p = false;
21403 parser->in_declarator_p = true;
21404 /* Consume the `['. */
21405 cp_lexer_consume_token (parser->lexer);
21406 /* Peek at the next token. */
21407 token = cp_lexer_peek_token (parser->lexer);
21408 /* If the next token is `]', then there is no
21409 constant-expression. */
21410 if (token->type != CPP_CLOSE_SQUARE)
21411 {
21412 bool non_constant_p;
21413 bounds
21414 = cp_parser_constant_expression (parser,
21415 /*allow_non_constant=*/true,
21416 &non_constant_p);
21417 if (!non_constant_p)
21418 /* OK */;
21419 else if (error_operand_p (bounds))
21420 /* Already gave an error. */;
21421 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21422 /* Let compute_array_index_type diagnose this. */;
21423 else if (!parser->in_function_body
21424 || current_binding_level->kind == sk_function_parms)
21425 {
21426 /* Normally, the array bound must be an integral constant
21427 expression. However, as an extension, we allow VLAs
21428 in function scopes as long as they aren't part of a
21429 parameter declaration. */
21430 cp_parser_error (parser,
21431 "array bound is not an integer constant");
21432 bounds = error_mark_node;
21433 }
21434 else if (processing_template_decl
21435 && !type_dependent_expression_p (bounds))
21436 {
21437 /* Remember this wasn't a constant-expression. */
21438 bounds = build_nop (TREE_TYPE (bounds), bounds);
21439 TREE_SIDE_EFFECTS (bounds) = 1;
21440 }
21441 }
21442 else
21443 bounds = NULL_TREE;
21444 /* Look for the closing `]'. */
21445 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21446 {
21447 declarator = cp_error_declarator;
21448 break;
21449 }
21450
21451 attrs = cp_parser_std_attribute_spec_seq (parser);
21452 declarator = make_array_declarator (declarator, bounds);
21453 declarator->std_attributes = attrs;
21454 }
21455 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
21456 {
21457 {
21458 tree qualifying_scope;
21459 tree unqualified_name;
21460 tree attrs;
21461 special_function_kind sfk;
21462 bool abstract_ok;
21463 bool pack_expansion_p = false;
21464 cp_token *declarator_id_start_token;
21465
21466 /* Parse a declarator-id */
21467 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
21468 if (abstract_ok)
21469 {
21470 cp_parser_parse_tentatively (parser);
21471
21472 /* If we see an ellipsis, we should be looking at a
21473 parameter pack. */
21474 if (token->type == CPP_ELLIPSIS)
21475 {
21476 /* Consume the `...' */
21477 cp_lexer_consume_token (parser->lexer);
21478
21479 pack_expansion_p = true;
21480 }
21481 }
21482
21483 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
21484 unqualified_name
21485 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
21486 qualifying_scope = parser->scope;
21487 if (abstract_ok)
21488 {
21489 bool okay = false;
21490
21491 if (!unqualified_name && pack_expansion_p)
21492 {
21493 /* Check whether an error occurred. */
21494 okay = !cp_parser_error_occurred (parser);
21495
21496 /* We already consumed the ellipsis to mark a
21497 parameter pack, but we have no way to report it,
21498 so abort the tentative parse. We will be exiting
21499 immediately anyway. */
21500 cp_parser_abort_tentative_parse (parser);
21501 }
21502 else
21503 okay = cp_parser_parse_definitely (parser);
21504
21505 if (!okay)
21506 unqualified_name = error_mark_node;
21507 else if (unqualified_name
21508 && (qualifying_scope
21509 || (!identifier_p (unqualified_name))))
21510 {
21511 cp_parser_error (parser, "expected unqualified-id");
21512 unqualified_name = error_mark_node;
21513 }
21514 }
21515
21516 if (!unqualified_name)
21517 return NULL;
21518 if (unqualified_name == error_mark_node)
21519 {
21520 declarator = cp_error_declarator;
21521 pack_expansion_p = false;
21522 declarator->parameter_pack_p = false;
21523 break;
21524 }
21525
21526 attrs = cp_parser_std_attribute_spec_seq (parser);
21527
21528 if (qualifying_scope && at_namespace_scope_p ()
21529 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
21530 {
21531 /* In the declaration of a member of a template class
21532 outside of the class itself, the SCOPE will sometimes
21533 be a TYPENAME_TYPE. For example, given:
21534
21535 template <typename T>
21536 int S<T>::R::i = 3;
21537
21538 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
21539 this context, we must resolve S<T>::R to an ordinary
21540 type, rather than a typename type.
21541
21542 The reason we normally avoid resolving TYPENAME_TYPEs
21543 is that a specialization of `S' might render
21544 `S<T>::R' not a type. However, if `S' is
21545 specialized, then this `i' will not be used, so there
21546 is no harm in resolving the types here. */
21547 tree type;
21548
21549 /* Resolve the TYPENAME_TYPE. */
21550 type = resolve_typename_type (qualifying_scope,
21551 /*only_current_p=*/false);
21552 /* If that failed, the declarator is invalid. */
21553 if (TREE_CODE (type) == TYPENAME_TYPE)
21554 {
21555 if (typedef_variant_p (type))
21556 error_at (declarator_id_start_token->location,
21557 "cannot define member of dependent typedef "
21558 "%qT", type);
21559 else
21560 error_at (declarator_id_start_token->location,
21561 "%<%T::%E%> is not a type",
21562 TYPE_CONTEXT (qualifying_scope),
21563 TYPE_IDENTIFIER (qualifying_scope));
21564 }
21565 qualifying_scope = type;
21566 }
21567
21568 sfk = sfk_none;
21569
21570 if (unqualified_name)
21571 {
21572 tree class_type;
21573
21574 if (qualifying_scope
21575 && CLASS_TYPE_P (qualifying_scope))
21576 class_type = qualifying_scope;
21577 else
21578 class_type = current_class_type;
21579
21580 if (TREE_CODE (unqualified_name) == TYPE_DECL)
21581 {
21582 tree name_type = TREE_TYPE (unqualified_name);
21583
21584 if (!class_type || !same_type_p (name_type, class_type))
21585 {
21586 /* We do not attempt to print the declarator
21587 here because we do not have enough
21588 information about its original syntactic
21589 form. */
21590 cp_parser_error (parser, "invalid declarator");
21591 declarator = cp_error_declarator;
21592 break;
21593 }
21594 else if (qualifying_scope
21595 && CLASSTYPE_USE_TEMPLATE (name_type))
21596 {
21597 error_at (declarator_id_start_token->location,
21598 "invalid use of constructor as a template");
21599 inform (declarator_id_start_token->location,
21600 "use %<%T::%D%> instead of %<%T::%D%> to "
21601 "name the constructor in a qualified name",
21602 class_type,
21603 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
21604 class_type, name_type);
21605 declarator = cp_error_declarator;
21606 break;
21607 }
21608 unqualified_name = constructor_name (class_type);
21609 }
21610
21611 if (class_type)
21612 {
21613 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
21614 sfk = sfk_destructor;
21615 else if (identifier_p (unqualified_name)
21616 && IDENTIFIER_CONV_OP_P (unqualified_name))
21617 sfk = sfk_conversion;
21618 else if (/* There's no way to declare a constructor
21619 for an unnamed type, even if the type
21620 got a name for linkage purposes. */
21621 !TYPE_WAS_UNNAMED (class_type)
21622 /* Handle correctly (c++/19200):
21623
21624 struct S {
21625 struct T{};
21626 friend void S(T);
21627 };
21628
21629 and also:
21630
21631 namespace N {
21632 void S();
21633 }
21634
21635 struct S {
21636 friend void N::S();
21637 }; */
21638 && (!friend_p || class_type == qualifying_scope)
21639 && constructor_name_p (unqualified_name,
21640 class_type))
21641 sfk = sfk_constructor;
21642 else if (is_overloaded_fn (unqualified_name)
21643 && DECL_CONSTRUCTOR_P (get_first_fn
21644 (unqualified_name)))
21645 sfk = sfk_constructor;
21646
21647 if (ctor_dtor_or_conv_p && sfk != sfk_none)
21648 *ctor_dtor_or_conv_p = -1;
21649 }
21650 }
21651 declarator = make_id_declarator (qualifying_scope,
21652 unqualified_name,
21653 sfk, token->location);
21654 declarator->std_attributes = attrs;
21655 declarator->parameter_pack_p = pack_expansion_p;
21656
21657 if (pack_expansion_p)
21658 maybe_warn_variadic_templates ();
21659
21660 /* We're looking for this case in [temp.res]:
21661 A qualified-id is assumed to name a type if [...]
21662 - it is a decl-specifier of the decl-specifier-seq of a
21663 parameter-declaration in a declarator of a function or
21664 function template declaration, ... */
21665 if (cxx_dialect >= cxx20
21666 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
21667 && declarator->kind == cdk_id
21668 && !at_class_scope_p ()
21669 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21670 {
21671 /* ...whose declarator-id is qualified. If it isn't, never
21672 assume the parameters to refer to types. */
21673 if (qualifying_scope == NULL_TREE)
21674 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21675 else
21676 {
21677 /* Now we have something like
21678 template <typename T> int C::x(S::p);
21679 which can be a function template declaration or a
21680 variable template definition. If name lookup for
21681 the declarator-id C::x finds one or more function
21682 templates, assume S::p to name a type. Otherwise,
21683 don't. */
21684 tree decl
21685 = cp_parser_lookup_name_simple (parser, unqualified_name,
21686 token->location);
21687 if (!is_overloaded_fn (decl)
21688 /* Allow
21689 template<typename T>
21690 A<T>::A(T::type) { } */
21691 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
21692 && constructor_name_p (unqualified_name,
21693 qualifying_scope)))
21694 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21695 }
21696 }
21697 }
21698
21699 handle_declarator:;
21700 scope = get_scope_of_declarator (declarator);
21701 if (scope)
21702 {
21703 /* Any names that appear after the declarator-id for a
21704 member are looked up in the containing scope. */
21705 if (at_function_scope_p ())
21706 {
21707 /* But declarations with qualified-ids can't appear in a
21708 function. */
21709 cp_parser_error (parser, "qualified-id in declaration");
21710 declarator = cp_error_declarator;
21711 break;
21712 }
21713 pushed_scope = push_scope (scope);
21714 }
21715 parser->in_declarator_p = true;
21716 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
21717 || (declarator && declarator->kind == cdk_id))
21718 /* Default args are only allowed on function
21719 declarations. */
21720 parser->default_arg_ok_p = saved_default_arg_ok_p;
21721 else
21722 parser->default_arg_ok_p = false;
21723
21724 first = false;
21725 }
21726 /* We're done. */
21727 else
21728 break;
21729 }
21730
21731 /* For an abstract declarator, we might wind up with nothing at this
21732 point. That's an error; the declarator is not optional. */
21733 if (!declarator)
21734 cp_parser_error (parser, "expected declarator");
21735 else if (open_paren)
21736 {
21737 /* Record overly parenthesized declarator so we can give a
21738 diagnostic about confusing decl/expr disambiguation. */
21739 if (declarator->kind == cdk_array)
21740 {
21741 /* If the open and close parens are on different lines, this
21742 is probably a formatting thing, so ignore. */
21743 expanded_location open = expand_location (open_paren->location);
21744 expanded_location close = expand_location (close_paren->location);
21745 if (open.line != close.line || open.file != close.file)
21746 open_paren = NULL;
21747 }
21748 if (open_paren)
21749 declarator->parenthesized = open_paren->location;
21750 }
21751
21752 /* If we entered a scope, we must exit it now. */
21753 if (pushed_scope)
21754 pop_scope (pushed_scope);
21755
21756 parser->default_arg_ok_p = saved_default_arg_ok_p;
21757 parser->in_declarator_p = saved_in_declarator_p;
21758
21759 return declarator;
21760 }
21761
21762 /* Parse a ptr-operator.
21763
21764 ptr-operator:
21765 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21766 * cv-qualifier-seq [opt]
21767 &
21768 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21769 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21770
21771 GNU Extension:
21772
21773 ptr-operator:
21774 & cv-qualifier-seq [opt]
21775
21776 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21777 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21778 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21779 filled in with the TYPE containing the member. *CV_QUALS is
21780 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21781 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21782 Note that the tree codes returned by this function have nothing
21783 to do with the types of trees that will be eventually be created
21784 to represent the pointer or reference type being parsed. They are
21785 just constants with suggestive names. */
21786 static enum tree_code
21787 cp_parser_ptr_operator (cp_parser* parser,
21788 tree* type,
21789 cp_cv_quals *cv_quals,
21790 tree *attributes)
21791 {
21792 enum tree_code code = ERROR_MARK;
21793 cp_token *token;
21794 tree attrs = NULL_TREE;
21795
21796 /* Assume that it's not a pointer-to-member. */
21797 *type = NULL_TREE;
21798 /* And that there are no cv-qualifiers. */
21799 *cv_quals = TYPE_UNQUALIFIED;
21800
21801 /* Peek at the next token. */
21802 token = cp_lexer_peek_token (parser->lexer);
21803
21804 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21805 if (token->type == CPP_MULT)
21806 code = INDIRECT_REF;
21807 else if (token->type == CPP_AND)
21808 code = ADDR_EXPR;
21809 else if ((cxx_dialect != cxx98) &&
21810 token->type == CPP_AND_AND) /* C++0x only */
21811 code = NON_LVALUE_EXPR;
21812
21813 if (code != ERROR_MARK)
21814 {
21815 /* Consume the `*', `&' or `&&'. */
21816 cp_lexer_consume_token (parser->lexer);
21817
21818 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21819 `&', if we are allowing GNU extensions. (The only qualifier
21820 that can legally appear after `&' is `restrict', but that is
21821 enforced during semantic analysis. */
21822 if (code == INDIRECT_REF
21823 || cp_parser_allow_gnu_extensions_p (parser))
21824 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21825
21826 attrs = cp_parser_std_attribute_spec_seq (parser);
21827 if (attributes != NULL)
21828 *attributes = attrs;
21829 }
21830 else
21831 {
21832 /* Try the pointer-to-member case. */
21833 cp_parser_parse_tentatively (parser);
21834 /* Look for the optional `::' operator. */
21835 cp_parser_global_scope_opt (parser,
21836 /*current_scope_valid_p=*/false);
21837 /* Look for the nested-name specifier. */
21838 token = cp_lexer_peek_token (parser->lexer);
21839 cp_parser_nested_name_specifier (parser,
21840 /*typename_keyword_p=*/false,
21841 /*check_dependency_p=*/true,
21842 /*type_p=*/false,
21843 /*is_declaration=*/false);
21844 /* If we found it, and the next token is a `*', then we are
21845 indeed looking at a pointer-to-member operator. */
21846 if (!cp_parser_error_occurred (parser)
21847 && cp_parser_require (parser, CPP_MULT, RT_MULT))
21848 {
21849 /* Indicate that the `*' operator was used. */
21850 code = INDIRECT_REF;
21851
21852 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
21853 error_at (token->location, "%qD is a namespace", parser->scope);
21854 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
21855 error_at (token->location, "cannot form pointer to member of "
21856 "non-class %q#T", parser->scope);
21857 else
21858 {
21859 /* The type of which the member is a member is given by the
21860 current SCOPE. */
21861 *type = parser->scope;
21862 /* The next name will not be qualified. */
21863 parser->scope = NULL_TREE;
21864 parser->qualifying_scope = NULL_TREE;
21865 parser->object_scope = NULL_TREE;
21866 /* Look for optional c++11 attributes. */
21867 attrs = cp_parser_std_attribute_spec_seq (parser);
21868 if (attributes != NULL)
21869 *attributes = attrs;
21870 /* Look for the optional cv-qualifier-seq. */
21871 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21872 }
21873 }
21874 /* If that didn't work we don't have a ptr-operator. */
21875 if (!cp_parser_parse_definitely (parser))
21876 cp_parser_error (parser, "expected ptr-operator");
21877 }
21878
21879 return code;
21880 }
21881
21882 /* Parse an (optional) cv-qualifier-seq.
21883
21884 cv-qualifier-seq:
21885 cv-qualifier cv-qualifier-seq [opt]
21886
21887 cv-qualifier:
21888 const
21889 volatile
21890
21891 GNU Extension:
21892
21893 cv-qualifier:
21894 __restrict__
21895
21896 Returns a bitmask representing the cv-qualifiers. */
21897
21898 static cp_cv_quals
21899 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
21900 {
21901 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
21902
21903 while (true)
21904 {
21905 cp_token *token;
21906 cp_cv_quals cv_qualifier;
21907
21908 /* Peek at the next token. */
21909 token = cp_lexer_peek_token (parser->lexer);
21910 /* See if it's a cv-qualifier. */
21911 switch (token->keyword)
21912 {
21913 case RID_CONST:
21914 cv_qualifier = TYPE_QUAL_CONST;
21915 break;
21916
21917 case RID_VOLATILE:
21918 cv_qualifier = TYPE_QUAL_VOLATILE;
21919 break;
21920
21921 case RID_RESTRICT:
21922 cv_qualifier = TYPE_QUAL_RESTRICT;
21923 break;
21924
21925 default:
21926 cv_qualifier = TYPE_UNQUALIFIED;
21927 break;
21928 }
21929
21930 if (!cv_qualifier)
21931 break;
21932
21933 if (cv_quals & cv_qualifier)
21934 {
21935 gcc_rich_location richloc (token->location);
21936 richloc.add_fixit_remove ();
21937 error_at (&richloc, "duplicate cv-qualifier");
21938 cp_lexer_purge_token (parser->lexer);
21939 }
21940 else
21941 {
21942 cp_lexer_consume_token (parser->lexer);
21943 cv_quals |= cv_qualifier;
21944 }
21945 }
21946
21947 return cv_quals;
21948 }
21949
21950 /* Parse an (optional) ref-qualifier
21951
21952 ref-qualifier:
21953 &
21954 &&
21955
21956 Returns cp_ref_qualifier representing ref-qualifier. */
21957
21958 static cp_ref_qualifier
21959 cp_parser_ref_qualifier_opt (cp_parser* parser)
21960 {
21961 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
21962
21963 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21964 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
21965 return ref_qual;
21966
21967 while (true)
21968 {
21969 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
21970 cp_token *token = cp_lexer_peek_token (parser->lexer);
21971
21972 switch (token->type)
21973 {
21974 case CPP_AND:
21975 curr_ref_qual = REF_QUAL_LVALUE;
21976 break;
21977
21978 case CPP_AND_AND:
21979 curr_ref_qual = REF_QUAL_RVALUE;
21980 break;
21981
21982 default:
21983 curr_ref_qual = REF_QUAL_NONE;
21984 break;
21985 }
21986
21987 if (!curr_ref_qual)
21988 break;
21989 else if (ref_qual)
21990 {
21991 error_at (token->location, "multiple ref-qualifiers");
21992 cp_lexer_purge_token (parser->lexer);
21993 }
21994 else
21995 {
21996 ref_qual = curr_ref_qual;
21997 cp_lexer_consume_token (parser->lexer);
21998 }
21999 }
22000
22001 return ref_qual;
22002 }
22003
22004 /* Parse an optional tx-qualifier.
22005
22006 tx-qualifier:
22007 transaction_safe
22008 transaction_safe_dynamic */
22009
22010 static tree
22011 cp_parser_tx_qualifier_opt (cp_parser *parser)
22012 {
22013 cp_token *token = cp_lexer_peek_token (parser->lexer);
22014 if (token->type == CPP_NAME)
22015 {
22016 tree name = token->u.value;
22017 const char *p = IDENTIFIER_POINTER (name);
22018 const int len = strlen ("transaction_safe");
22019 if (!strncmp (p, "transaction_safe", len))
22020 {
22021 p += len;
22022 if (*p == '\0'
22023 || !strcmp (p, "_dynamic"))
22024 {
22025 cp_lexer_consume_token (parser->lexer);
22026 if (!flag_tm)
22027 {
22028 error ("%qE requires %<-fgnu-tm%>", name);
22029 return NULL_TREE;
22030 }
22031 else
22032 return name;
22033 }
22034 }
22035 }
22036 return NULL_TREE;
22037 }
22038
22039 /* Parse an (optional) virt-specifier-seq.
22040
22041 virt-specifier-seq:
22042 virt-specifier virt-specifier-seq [opt]
22043
22044 virt-specifier:
22045 override
22046 final
22047
22048 Returns a bitmask representing the virt-specifiers. */
22049
22050 static cp_virt_specifiers
22051 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
22052 {
22053 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22054
22055 while (true)
22056 {
22057 cp_token *token;
22058 cp_virt_specifiers virt_specifier;
22059
22060 /* Peek at the next token. */
22061 token = cp_lexer_peek_token (parser->lexer);
22062 /* See if it's a virt-specifier-qualifier. */
22063 if (token->type != CPP_NAME)
22064 break;
22065 if (id_equal (token->u.value, "override"))
22066 {
22067 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
22068 virt_specifier = VIRT_SPEC_OVERRIDE;
22069 }
22070 else if (id_equal (token->u.value, "final"))
22071 {
22072 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
22073 virt_specifier = VIRT_SPEC_FINAL;
22074 }
22075 else if (id_equal (token->u.value, "__final"))
22076 {
22077 virt_specifier = VIRT_SPEC_FINAL;
22078 }
22079 else
22080 break;
22081
22082 if (virt_specifiers & virt_specifier)
22083 {
22084 gcc_rich_location richloc (token->location);
22085 richloc.add_fixit_remove ();
22086 error_at (&richloc, "duplicate virt-specifier");
22087 cp_lexer_purge_token (parser->lexer);
22088 }
22089 else
22090 {
22091 cp_lexer_consume_token (parser->lexer);
22092 virt_specifiers |= virt_specifier;
22093 }
22094 }
22095 return virt_specifiers;
22096 }
22097
22098 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
22099 is in scope even though it isn't real. */
22100
22101 void
22102 inject_this_parameter (tree ctype, cp_cv_quals quals)
22103 {
22104 tree this_parm;
22105
22106 if (current_class_ptr)
22107 {
22108 /* We don't clear this between NSDMIs. Is it already what we want? */
22109 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
22110 if (DECL_P (current_class_ptr)
22111 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
22112 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
22113 && cp_type_quals (type) == quals)
22114 return;
22115 }
22116
22117 this_parm = build_this_parm (NULL_TREE, ctype, quals);
22118 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
22119 current_class_ptr = NULL_TREE;
22120 current_class_ref
22121 = cp_build_fold_indirect_ref (this_parm);
22122 current_class_ptr = this_parm;
22123 }
22124
22125 /* Return true iff our current scope is a non-static data member
22126 initializer. */
22127
22128 bool
22129 parsing_nsdmi (void)
22130 {
22131 /* We recognize NSDMI context by the context-less 'this' pointer set up
22132 by the function above. */
22133 if (current_class_ptr
22134 && TREE_CODE (current_class_ptr) == PARM_DECL
22135 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
22136 return true;
22137 return false;
22138 }
22139
22140 /* Parse a late-specified return type, if any. This is not a separate
22141 non-terminal, but part of a function declarator, which looks like
22142
22143 -> trailing-type-specifier-seq abstract-declarator(opt)
22144
22145 Returns the type indicated by the type-id.
22146
22147 In addition to this, parse any queued up #pragma omp declare simd
22148 clauses, and #pragma acc routine clauses.
22149
22150 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
22151 function. */
22152
22153 static tree
22154 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
22155 tree& requires_clause)
22156 {
22157 cp_token *token;
22158 tree type = NULL_TREE;
22159 bool declare_simd_p = (parser->omp_declare_simd
22160 && declarator
22161 && declarator->kind == cdk_id);
22162
22163 bool oacc_routine_p = (parser->oacc_routine
22164 && declarator
22165 && declarator->kind == cdk_id);
22166
22167 /* Peek at the next token. */
22168 token = cp_lexer_peek_token (parser->lexer);
22169 /* A late-specified return type is indicated by an initial '->'. */
22170 if (token->type != CPP_DEREF
22171 && token->keyword != RID_REQUIRES
22172 && !(token->type == CPP_NAME
22173 && token->u.value == ridpointers[RID_REQUIRES])
22174 && !(declare_simd_p || oacc_routine_p))
22175 return NULL_TREE;
22176
22177 if (token->type == CPP_DEREF)
22178 {
22179 /* Consume the ->. */
22180 cp_lexer_consume_token (parser->lexer);
22181
22182 type = cp_parser_trailing_type_id (parser);
22183 }
22184
22185 /* Function declarations may be followed by a trailing
22186 requires-clause. */
22187 requires_clause = cp_parser_requires_clause_opt (parser, false);
22188
22189 if (declare_simd_p)
22190 declarator->attributes
22191 = cp_parser_late_parsing_omp_declare_simd (parser,
22192 declarator->attributes);
22193 if (oacc_routine_p)
22194 declarator->attributes
22195 = cp_parser_late_parsing_oacc_routine (parser,
22196 declarator->attributes);
22197
22198 return type;
22199 }
22200
22201 /* Parse a declarator-id.
22202
22203 declarator-id:
22204 id-expression
22205 :: [opt] nested-name-specifier [opt] type-name
22206
22207 In the `id-expression' case, the value returned is as for
22208 cp_parser_id_expression if the id-expression was an unqualified-id.
22209 If the id-expression was a qualified-id, then a SCOPE_REF is
22210 returned. The first operand is the scope (either a NAMESPACE_DECL
22211 or TREE_TYPE), but the second is still just a representation of an
22212 unqualified-id. */
22213
22214 static tree
22215 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
22216 {
22217 tree id;
22218 /* The expression must be an id-expression. Assume that qualified
22219 names are the names of types so that:
22220
22221 template <class T>
22222 int S<T>::R::i = 3;
22223
22224 will work; we must treat `S<T>::R' as the name of a type.
22225 Similarly, assume that qualified names are templates, where
22226 required, so that:
22227
22228 template <class T>
22229 int S<T>::R<T>::i = 3;
22230
22231 will work, too. */
22232 id = cp_parser_id_expression (parser,
22233 /*template_keyword_p=*/false,
22234 /*check_dependency_p=*/false,
22235 /*template_p=*/NULL,
22236 /*declarator_p=*/true,
22237 optional_p);
22238 if (id && BASELINK_P (id))
22239 id = BASELINK_FUNCTIONS (id);
22240 return id;
22241 }
22242
22243 /* Parse a type-id.
22244
22245 type-id:
22246 type-specifier-seq abstract-declarator [opt]
22247
22248 The parser flags FLAGS is used to control type-specifier parsing.
22249
22250 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
22251
22252 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
22253 i.e. we've just seen "->".
22254
22255 Returns the TYPE specified. */
22256
22257 static tree
22258 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
22259 bool is_template_arg, bool is_trailing_return,
22260 location_t *type_location)
22261 {
22262 cp_decl_specifier_seq type_specifier_seq;
22263 cp_declarator *abstract_declarator;
22264
22265 /* Parse the type-specifier-seq. */
22266 cp_parser_type_specifier_seq (parser, flags,
22267 /*is_declaration=*/false,
22268 is_trailing_return,
22269 &type_specifier_seq);
22270 if (type_location)
22271 *type_location = type_specifier_seq.locations[ds_type_spec];
22272
22273 if (is_template_arg && type_specifier_seq.type
22274 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
22275 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
22276 /* A bare template name as a template argument is a template template
22277 argument, not a placeholder, so fail parsing it as a type argument. */
22278 {
22279 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
22280 cp_parser_simulate_error (parser);
22281 return error_mark_node;
22282 }
22283 if (type_specifier_seq.type == error_mark_node)
22284 return error_mark_node;
22285
22286 /* There might or might not be an abstract declarator. */
22287 cp_parser_parse_tentatively (parser);
22288 /* Look for the declarator. */
22289 abstract_declarator
22290 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
22291 CP_PARSER_FLAGS_NONE, NULL,
22292 /*parenthesized_p=*/NULL,
22293 /*member_p=*/false,
22294 /*friend_p=*/false,
22295 /*static_p=*/false);
22296 /* Check to see if there really was a declarator. */
22297 if (!cp_parser_parse_definitely (parser))
22298 abstract_declarator = NULL;
22299
22300 if (type_specifier_seq.type
22301 /* The concepts TS allows 'auto' as a type-id. */
22302 && (!flag_concepts || parser->in_type_id_in_expr_p)
22303 /* None of the valid uses of 'auto' in C++14 involve the type-id
22304 nonterminal, but it is valid in a trailing-return-type. */
22305 && !(cxx_dialect >= cxx14 && is_trailing_return))
22306 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
22307 {
22308 /* A type-id with type 'auto' is only ok if the abstract declarator
22309 is a function declarator with a late-specified return type.
22310
22311 A type-id with 'auto' is also valid in a trailing-return-type
22312 in a compound-requirement. */
22313 if (abstract_declarator
22314 && abstract_declarator->kind == cdk_function
22315 && abstract_declarator->u.function.late_return_type)
22316 /* OK */;
22317 else if (parser->in_result_type_constraint_p)
22318 /* OK */;
22319 else
22320 {
22321 location_t loc = type_specifier_seq.locations[ds_type_spec];
22322 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
22323 {
22324 error_at (loc, "missing template arguments after %qT",
22325 auto_node);
22326 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
22327 tmpl);
22328 }
22329 else
22330 error_at (loc, "invalid use of %qT", auto_node);
22331 return error_mark_node;
22332 }
22333 }
22334
22335 return groktypename (&type_specifier_seq, abstract_declarator,
22336 is_template_arg);
22337 }
22338
22339 /* Wrapper for cp_parser_type_id_1. */
22340
22341 static tree
22342 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
22343 location_t *type_location)
22344 {
22345 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
22346 }
22347
22348 /* Wrapper for cp_parser_type_id_1. */
22349
22350 static tree
22351 cp_parser_template_type_arg (cp_parser *parser)
22352 {
22353 tree r;
22354 const char *saved_message = parser->type_definition_forbidden_message;
22355 parser->type_definition_forbidden_message
22356 = G_("types may not be defined in template arguments");
22357 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
22358 parser->type_definition_forbidden_message = saved_message;
22359 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
22360 {
22361 error ("invalid use of %<auto%> in template argument");
22362 r = error_mark_node;
22363 }
22364 return r;
22365 }
22366
22367 /* Wrapper for cp_parser_type_id_1. */
22368
22369 static tree
22370 cp_parser_trailing_type_id (cp_parser *parser)
22371 {
22372 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
22373 false, true, NULL);
22374 }
22375
22376 /* Parse a type-specifier-seq.
22377
22378 type-specifier-seq:
22379 type-specifier type-specifier-seq [opt]
22380
22381 GNU extension:
22382
22383 type-specifier-seq:
22384 attributes type-specifier-seq [opt]
22385
22386 The parser flags FLAGS is used to control type-specifier parsing.
22387
22388 If IS_DECLARATION is true, we are at the start of a "condition" or
22389 exception-declaration, so we might be followed by a declarator-id.
22390
22391 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
22392 i.e. we've just seen "->".
22393
22394 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
22395
22396 static void
22397 cp_parser_type_specifier_seq (cp_parser* parser,
22398 cp_parser_flags flags,
22399 bool is_declaration,
22400 bool is_trailing_return,
22401 cp_decl_specifier_seq *type_specifier_seq)
22402 {
22403 bool seen_type_specifier = false;
22404 cp_token *start_token = NULL;
22405
22406 /* Clear the TYPE_SPECIFIER_SEQ. */
22407 clear_decl_specs (type_specifier_seq);
22408
22409 flags |= CP_PARSER_FLAGS_OPTIONAL;
22410 /* In the context of a trailing return type, enum E { } is an
22411 elaborated-type-specifier followed by a function-body, not an
22412 enum-specifier. */
22413 if (is_trailing_return)
22414 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
22415
22416 /* Parse the type-specifiers and attributes. */
22417 while (true)
22418 {
22419 tree type_specifier;
22420 bool is_cv_qualifier;
22421
22422 /* Check for attributes first. */
22423 if (cp_next_tokens_can_be_attribute_p (parser))
22424 {
22425 /* GNU attributes at the end of a declaration apply to the
22426 declaration as a whole, not to the trailing return type. So look
22427 ahead to see if these attributes are at the end. */
22428 if (seen_type_specifier && is_trailing_return
22429 && cp_next_tokens_can_be_gnu_attribute_p (parser))
22430 {
22431 size_t n = cp_parser_skip_attributes_opt (parser, 1);
22432 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
22433 if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
22434 || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
22435 break;
22436 }
22437 type_specifier_seq->attributes
22438 = attr_chainon (type_specifier_seq->attributes,
22439 cp_parser_attributes_opt (parser));
22440 continue;
22441 }
22442
22443 /* record the token of the beginning of the type specifier seq,
22444 for error reporting purposes*/
22445 if (!start_token)
22446 start_token = cp_lexer_peek_token (parser->lexer);
22447
22448 /* Look for the type-specifier. */
22449 type_specifier = cp_parser_type_specifier (parser,
22450 flags,
22451 type_specifier_seq,
22452 /*is_declaration=*/false,
22453 NULL,
22454 &is_cv_qualifier);
22455 if (!type_specifier)
22456 {
22457 /* If the first type-specifier could not be found, this is not a
22458 type-specifier-seq at all. */
22459 if (!seen_type_specifier)
22460 {
22461 /* Set in_declarator_p to avoid skipping to the semicolon. */
22462 int in_decl = parser->in_declarator_p;
22463 parser->in_declarator_p = true;
22464
22465 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
22466 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
22467 cp_parser_error (parser, "expected type-specifier");
22468
22469 parser->in_declarator_p = in_decl;
22470
22471 type_specifier_seq->type = error_mark_node;
22472 return;
22473 }
22474 /* If subsequent type-specifiers could not be found, the
22475 type-specifier-seq is complete. */
22476 break;
22477 }
22478
22479 seen_type_specifier = true;
22480 /* The standard says that a condition can be:
22481
22482 type-specifier-seq declarator = assignment-expression
22483
22484 However, given:
22485
22486 struct S {};
22487 if (int S = ...)
22488
22489 we should treat the "S" as a declarator, not as a
22490 type-specifier. The standard doesn't say that explicitly for
22491 type-specifier-seq, but it does say that for
22492 decl-specifier-seq in an ordinary declaration. Perhaps it
22493 would be clearer just to allow a decl-specifier-seq here, and
22494 then add a semantic restriction that if any decl-specifiers
22495 that are not type-specifiers appear, the program is invalid. */
22496 if (is_declaration && !is_cv_qualifier)
22497 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
22498 }
22499 }
22500
22501 /* Return whether the function currently being declared has an associated
22502 template parameter list. */
22503
22504 static bool
22505 function_being_declared_is_template_p (cp_parser* parser)
22506 {
22507 if (!current_template_parms || processing_template_parmlist)
22508 return false;
22509
22510 if (parser->implicit_template_scope)
22511 return true;
22512
22513 if (at_class_scope_p ()
22514 && TYPE_BEING_DEFINED (current_class_type))
22515 return parser->num_template_parameter_lists != 0;
22516
22517 return ((int) parser->num_template_parameter_lists > template_class_depth
22518 (current_class_type));
22519 }
22520
22521 /* Parse a parameter-declaration-clause.
22522
22523 parameter-declaration-clause:
22524 parameter-declaration-list [opt] ... [opt]
22525 parameter-declaration-list , ...
22526
22527 The parser flags FLAGS is used to control type-specifier parsing.
22528
22529 Returns a representation for the parameter declarations. A return
22530 value of NULL indicates a parameter-declaration-clause consisting
22531 only of an ellipsis. */
22532
22533 static tree
22534 cp_parser_parameter_declaration_clause (cp_parser* parser,
22535 cp_parser_flags flags)
22536 {
22537 tree parameters;
22538 cp_token *token;
22539 bool ellipsis_p;
22540
22541 temp_override<bool> cleanup
22542 (parser->auto_is_implicit_function_template_parm_p);
22543
22544 if (!processing_specialization
22545 && !processing_template_parmlist
22546 && !processing_explicit_instantiation
22547 /* default_arg_ok_p tracks whether this is a parameter-clause for an
22548 actual function or a random abstract declarator. */
22549 && parser->default_arg_ok_p)
22550 if (!current_function_decl
22551 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
22552 parser->auto_is_implicit_function_template_parm_p = true;
22553
22554 /* Peek at the next token. */
22555 token = cp_lexer_peek_token (parser->lexer);
22556 /* Check for trivial parameter-declaration-clauses. */
22557 if (token->type == CPP_ELLIPSIS)
22558 {
22559 /* Consume the `...' token. */
22560 cp_lexer_consume_token (parser->lexer);
22561 return NULL_TREE;
22562 }
22563 else if (token->type == CPP_CLOSE_PAREN)
22564 /* There are no parameters. */
22565 return void_list_node;
22566 /* Check for `(void)', too, which is a special case. */
22567 else if (token->keyword == RID_VOID
22568 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22569 == CPP_CLOSE_PAREN))
22570 {
22571 /* Consume the `void' token. */
22572 cp_lexer_consume_token (parser->lexer);
22573 /* There are no parameters. */
22574 return void_list_node;
22575 }
22576
22577 /* Parse the parameter-declaration-list. */
22578 parameters = cp_parser_parameter_declaration_list (parser, flags);
22579 /* If a parse error occurred while parsing the
22580 parameter-declaration-list, then the entire
22581 parameter-declaration-clause is erroneous. */
22582 if (parameters == error_mark_node)
22583 return NULL_TREE;
22584
22585 /* Peek at the next token. */
22586 token = cp_lexer_peek_token (parser->lexer);
22587 /* If it's a `,', the clause should terminate with an ellipsis. */
22588 if (token->type == CPP_COMMA)
22589 {
22590 /* Consume the `,'. */
22591 cp_lexer_consume_token (parser->lexer);
22592 /* Expect an ellipsis. */
22593 ellipsis_p
22594 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
22595 }
22596 /* It might also be `...' if the optional trailing `,' was
22597 omitted. */
22598 else if (token->type == CPP_ELLIPSIS)
22599 {
22600 /* Consume the `...' token. */
22601 cp_lexer_consume_token (parser->lexer);
22602 /* And remember that we saw it. */
22603 ellipsis_p = true;
22604 }
22605 else
22606 ellipsis_p = false;
22607
22608 /* Finish the parameter list. */
22609 if (!ellipsis_p)
22610 parameters = chainon (parameters, void_list_node);
22611
22612 return parameters;
22613 }
22614
22615 /* Parse a parameter-declaration-list.
22616
22617 parameter-declaration-list:
22618 parameter-declaration
22619 parameter-declaration-list , parameter-declaration
22620
22621 The parser flags FLAGS is used to control type-specifier parsing.
22622
22623 Returns a representation of the parameter-declaration-list, as for
22624 cp_parser_parameter_declaration_clause. However, the
22625 `void_list_node' is never appended to the list. */
22626
22627 static tree
22628 cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
22629 {
22630 tree parameters = NULL_TREE;
22631 tree *tail = &parameters;
22632 bool saved_in_unbraced_linkage_specification_p;
22633 int index = 0;
22634
22635 /* The special considerations that apply to a function within an
22636 unbraced linkage specifications do not apply to the parameters
22637 to the function. */
22638 saved_in_unbraced_linkage_specification_p
22639 = parser->in_unbraced_linkage_specification_p;
22640 parser->in_unbraced_linkage_specification_p = false;
22641
22642 /* Look for more parameters. */
22643 while (true)
22644 {
22645 cp_parameter_declarator *parameter;
22646 tree decl = error_mark_node;
22647 bool parenthesized_p = false;
22648
22649 /* Parse the parameter. */
22650 parameter
22651 = cp_parser_parameter_declaration (parser, flags,
22652 /*template_parm_p=*/false,
22653 &parenthesized_p);
22654
22655 /* We don't know yet if the enclosing context is deprecated, so wait
22656 and warn in grokparms if appropriate. */
22657 deprecated_state = DEPRECATED_SUPPRESS;
22658
22659 if (parameter)
22660 {
22661 decl = grokdeclarator (parameter->declarator,
22662 &parameter->decl_specifiers,
22663 PARM,
22664 parameter->default_argument != NULL_TREE,
22665 &parameter->decl_specifiers.attributes);
22666 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
22667 DECL_SOURCE_LOCATION (decl) = parameter->loc;
22668 }
22669
22670 deprecated_state = DEPRECATED_NORMAL;
22671
22672 /* If a parse error occurred parsing the parameter declaration,
22673 then the entire parameter-declaration-list is erroneous. */
22674 if (decl == error_mark_node)
22675 {
22676 parameters = error_mark_node;
22677 break;
22678 }
22679
22680 if (parameter->decl_specifiers.attributes)
22681 cplus_decl_attributes (&decl,
22682 parameter->decl_specifiers.attributes,
22683 0);
22684 if (DECL_NAME (decl))
22685 decl = pushdecl (decl);
22686
22687 if (decl != error_mark_node)
22688 {
22689 retrofit_lang_decl (decl);
22690 DECL_PARM_INDEX (decl) = ++index;
22691 DECL_PARM_LEVEL (decl) = function_parm_depth ();
22692 }
22693
22694 /* Add the new parameter to the list. */
22695 *tail = build_tree_list (parameter->default_argument, decl);
22696 tail = &TREE_CHAIN (*tail);
22697
22698 /* Peek at the next token. */
22699 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
22700 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
22701 /* These are for Objective-C++ */
22702 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22703 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22704 /* The parameter-declaration-list is complete. */
22705 break;
22706 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22707 {
22708 cp_token *token;
22709
22710 /* Peek at the next token. */
22711 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22712 /* If it's an ellipsis, then the list is complete. */
22713 if (token->type == CPP_ELLIPSIS)
22714 break;
22715 /* Otherwise, there must be more parameters. Consume the
22716 `,'. */
22717 cp_lexer_consume_token (parser->lexer);
22718 /* When parsing something like:
22719
22720 int i(float f, double d)
22721
22722 we can tell after seeing the declaration for "f" that we
22723 are not looking at an initialization of a variable "i",
22724 but rather at the declaration of a function "i".
22725
22726 Due to the fact that the parsing of template arguments
22727 (as specified to a template-id) requires backtracking we
22728 cannot use this technique when inside a template argument
22729 list. */
22730 if (!parser->in_template_argument_list_p
22731 && !parser->in_type_id_in_expr_p
22732 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22733 /* However, a parameter-declaration of the form
22734 "float(f)" (which is a valid declaration of a
22735 parameter "f") can also be interpreted as an
22736 expression (the conversion of "f" to "float"). */
22737 && !parenthesized_p)
22738 cp_parser_commit_to_tentative_parse (parser);
22739 }
22740 else
22741 {
22742 cp_parser_error (parser, "expected %<,%> or %<...%>");
22743 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
22744 cp_parser_skip_to_closing_parenthesis (parser,
22745 /*recovering=*/true,
22746 /*or_comma=*/false,
22747 /*consume_paren=*/false);
22748 break;
22749 }
22750 }
22751
22752 parser->in_unbraced_linkage_specification_p
22753 = saved_in_unbraced_linkage_specification_p;
22754
22755 /* Reset implicit_template_scope if we are about to leave the function
22756 parameter list that introduced it. Note that for out-of-line member
22757 definitions, there will be one or more class scopes before we get to
22758 the template parameter scope. */
22759
22760 if (cp_binding_level *its = parser->implicit_template_scope)
22761 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
22762 {
22763 while (maybe_its->kind == sk_class)
22764 maybe_its = maybe_its->level_chain;
22765 if (maybe_its == its)
22766 {
22767 parser->implicit_template_parms = 0;
22768 parser->implicit_template_scope = 0;
22769 }
22770 }
22771
22772 return parameters;
22773 }
22774
22775 /* Parse a parameter declaration.
22776
22777 parameter-declaration:
22778 decl-specifier-seq ... [opt] declarator
22779 decl-specifier-seq declarator = assignment-expression
22780 decl-specifier-seq ... [opt] abstract-declarator [opt]
22781 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22782
22783 The parser flags FLAGS is used to control type-specifier parsing.
22784
22785 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22786 declares a template parameter. (In that case, a non-nested `>'
22787 token encountered during the parsing of the assignment-expression
22788 is not interpreted as a greater-than operator.)
22789
22790 Returns a representation of the parameter, or NULL if an error
22791 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22792 true iff the declarator is of the form "(p)". */
22793
22794 static cp_parameter_declarator *
22795 cp_parser_parameter_declaration (cp_parser *parser,
22796 cp_parser_flags flags,
22797 bool template_parm_p,
22798 bool *parenthesized_p)
22799 {
22800 int declares_class_or_enum;
22801 cp_decl_specifier_seq decl_specifiers;
22802 cp_declarator *declarator;
22803 tree default_argument;
22804 cp_token *token = NULL, *declarator_token_start = NULL;
22805 const char *saved_message;
22806 bool template_parameter_pack_p = false;
22807
22808 /* In a template parameter, `>' is not an operator.
22809
22810 [temp.param]
22811
22812 When parsing a default template-argument for a non-type
22813 template-parameter, the first non-nested `>' is taken as the end
22814 of the template parameter-list rather than a greater-than
22815 operator. */
22816
22817 /* Type definitions may not appear in parameter types. */
22818 saved_message = parser->type_definition_forbidden_message;
22819 parser->type_definition_forbidden_message
22820 = G_("types may not be defined in parameter types");
22821
22822 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
22823 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22824 (current_template_parms)) : 0);
22825
22826 /* Parse the declaration-specifiers. */
22827 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22828 cp_parser_decl_specifier_seq (parser,
22829 flags,
22830 &decl_specifiers,
22831 &declares_class_or_enum);
22832
22833 /* Complain about missing 'typename' or other invalid type names. */
22834 if (!decl_specifiers.any_type_specifiers_p
22835 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22836 decl_specifiers.type = error_mark_node;
22837
22838 /* If an error occurred, there's no reason to attempt to parse the
22839 rest of the declaration. */
22840 if (cp_parser_error_occurred (parser))
22841 {
22842 parser->type_definition_forbidden_message = saved_message;
22843 return NULL;
22844 }
22845
22846 /* Peek at the next token. */
22847 token = cp_lexer_peek_token (parser->lexer);
22848
22849 /* If the next token is a `)', `,', `=', `>', or `...', then there
22850 is no declarator. However, when variadic templates are enabled,
22851 there may be a declarator following `...'. */
22852 if (token->type == CPP_CLOSE_PAREN
22853 || token->type == CPP_COMMA
22854 || token->type == CPP_EQ
22855 || token->type == CPP_GREATER)
22856 {
22857 declarator = NULL;
22858 if (parenthesized_p)
22859 *parenthesized_p = false;
22860 }
22861 /* Otherwise, there should be a declarator. */
22862 else
22863 {
22864 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22865 parser->default_arg_ok_p = false;
22866
22867 /* After seeing a decl-specifier-seq, if the next token is not a
22868 "(", there is no possibility that the code is a valid
22869 expression. Therefore, if parsing tentatively, we commit at
22870 this point. */
22871 if (!parser->in_template_argument_list_p
22872 /* In an expression context, having seen:
22873
22874 (int((char ...
22875
22876 we cannot be sure whether we are looking at a
22877 function-type (taking a "char" as a parameter) or a cast
22878 of some object of type "char" to "int". */
22879 && !parser->in_type_id_in_expr_p
22880 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22881 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22882 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
22883 cp_parser_commit_to_tentative_parse (parser);
22884 /* Parse the declarator. */
22885 declarator_token_start = token;
22886 declarator = cp_parser_declarator (parser,
22887 CP_PARSER_DECLARATOR_EITHER,
22888 CP_PARSER_FLAGS_NONE,
22889 /*ctor_dtor_or_conv_p=*/NULL,
22890 parenthesized_p,
22891 /*member_p=*/false,
22892 /*friend_p=*/false,
22893 /*static_p=*/false);
22894 parser->default_arg_ok_p = saved_default_arg_ok_p;
22895 /* After the declarator, allow more attributes. */
22896 decl_specifiers.attributes
22897 = attr_chainon (decl_specifiers.attributes,
22898 cp_parser_attributes_opt (parser));
22899
22900 /* If the declarator is a template parameter pack, remember that and
22901 clear the flag in the declarator itself so we don't get errors
22902 from grokdeclarator. */
22903 if (template_parm_p && declarator && declarator->parameter_pack_p)
22904 {
22905 declarator->parameter_pack_p = false;
22906 template_parameter_pack_p = true;
22907 }
22908 }
22909
22910 /* If the next token is an ellipsis, and we have not seen a declarator
22911 name, and if either the type of the declarator contains parameter
22912 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22913 for, eg, abbreviated integral type names), then we actually have a
22914 parameter pack expansion expression. Otherwise, leave the ellipsis
22915 for a C-style variadic function. */
22916 token = cp_lexer_peek_token (parser->lexer);
22917
22918 /* If a function parameter pack was specified and an implicit template
22919 parameter was introduced during cp_parser_parameter_declaration,
22920 change any implicit parameters introduced into packs. */
22921 if (parser->implicit_template_parms
22922 && ((token->type == CPP_ELLIPSIS
22923 && declarator_can_be_parameter_pack (declarator))
22924 || (declarator && declarator->parameter_pack_p)))
22925 {
22926 int latest_template_parm_idx = TREE_VEC_LENGTH
22927 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
22928
22929 if (latest_template_parm_idx != template_parm_idx)
22930 decl_specifiers.type = convert_generic_types_to_packs
22931 (decl_specifiers.type,
22932 template_parm_idx, latest_template_parm_idx);
22933 }
22934
22935 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22936 {
22937 tree type = decl_specifiers.type;
22938
22939 if (type && DECL_P (type))
22940 type = TREE_TYPE (type);
22941
22942 if (((type
22943 && TREE_CODE (type) != TYPE_PACK_EXPANSION
22944 && (template_parm_p || uses_parameter_packs (type)))
22945 || (!type && template_parm_p))
22946 && declarator_can_be_parameter_pack (declarator))
22947 {
22948 /* Consume the `...'. */
22949 cp_lexer_consume_token (parser->lexer);
22950 maybe_warn_variadic_templates ();
22951
22952 /* Build a pack expansion type */
22953 if (template_parm_p)
22954 template_parameter_pack_p = true;
22955 else if (declarator)
22956 declarator->parameter_pack_p = true;
22957 else
22958 decl_specifiers.type = make_pack_expansion (type);
22959 }
22960 }
22961
22962 /* The restriction on defining new types applies only to the type
22963 of the parameter, not to the default argument. */
22964 parser->type_definition_forbidden_message = saved_message;
22965
22966 /* If the next token is `=', then process a default argument. */
22967 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22968 {
22969 tree type = decl_specifiers.type;
22970 token = cp_lexer_peek_token (parser->lexer);
22971 /* If we are defining a class, then the tokens that make up the
22972 default argument must be saved and processed later. */
22973 if (!template_parm_p && at_class_scope_p ()
22974 && TYPE_BEING_DEFINED (current_class_type)
22975 && !LAMBDA_TYPE_P (current_class_type))
22976 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
22977
22978 /* A constrained-type-specifier may declare a type
22979 template-parameter. */
22980 else if (declares_constrained_type_template_parameter (type))
22981 default_argument
22982 = cp_parser_default_type_template_argument (parser);
22983
22984 /* A constrained-type-specifier may declare a
22985 template-template-parameter. */
22986 else if (declares_constrained_template_template_parameter (type))
22987 default_argument
22988 = cp_parser_default_template_template_argument (parser);
22989
22990 /* Outside of a class definition, we can just parse the
22991 assignment-expression. */
22992 else
22993 default_argument
22994 = cp_parser_default_argument (parser, template_parm_p);
22995
22996 if (!parser->default_arg_ok_p)
22997 {
22998 permerror (token->location,
22999 "default arguments are only "
23000 "permitted for function parameters");
23001 }
23002 else if ((declarator && declarator->parameter_pack_p)
23003 || template_parameter_pack_p
23004 || (decl_specifiers.type
23005 && PACK_EXPANSION_P (decl_specifiers.type)))
23006 {
23007 /* Find the name of the parameter pack. */
23008 cp_declarator *id_declarator = declarator;
23009 while (id_declarator && id_declarator->kind != cdk_id)
23010 id_declarator = id_declarator->declarator;
23011
23012 if (id_declarator && id_declarator->kind == cdk_id)
23013 error_at (declarator_token_start->location,
23014 template_parm_p
23015 ? G_("template parameter pack %qD "
23016 "cannot have a default argument")
23017 : G_("parameter pack %qD cannot have "
23018 "a default argument"),
23019 id_declarator->u.id.unqualified_name);
23020 else
23021 error_at (declarator_token_start->location,
23022 template_parm_p
23023 ? G_("template parameter pack cannot have "
23024 "a default argument")
23025 : G_("parameter pack cannot have a "
23026 "default argument"));
23027
23028 default_argument = NULL_TREE;
23029 }
23030 }
23031 else
23032 default_argument = NULL_TREE;
23033
23034 if (default_argument)
23035 STRIP_ANY_LOCATION_WRAPPER (default_argument);
23036
23037 /* Generate a location for the parameter, ranging from the start of the
23038 initial token to the end of the final token (using input_location for
23039 the latter, set up by cp_lexer_set_source_position_from_token when
23040 consuming tokens).
23041
23042 If we have a identifier, then use it for the caret location, e.g.
23043
23044 extern int callee (int one, int (*two)(int, int), float three);
23045 ~~~~~~^~~~~~~~~~~~~~
23046
23047 otherwise, reuse the start location for the caret location e.g.:
23048
23049 extern int callee (int one, int (*)(int, int), float three);
23050 ^~~~~~~~~~~~~~~~~
23051
23052 */
23053 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
23054 ? declarator->id_loc
23055 : decl_spec_token_start->location);
23056 location_t param_loc = make_location (caret_loc,
23057 decl_spec_token_start->location,
23058 input_location);
23059
23060 return make_parameter_declarator (&decl_specifiers,
23061 declarator,
23062 default_argument,
23063 param_loc,
23064 template_parameter_pack_p);
23065 }
23066
23067 /* Parse a default argument and return it.
23068
23069 TEMPLATE_PARM_P is true if this is a default argument for a
23070 non-type template parameter. */
23071 static tree
23072 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
23073 {
23074 tree default_argument = NULL_TREE;
23075 bool saved_greater_than_is_operator_p;
23076 unsigned char saved_local_variables_forbidden_p;
23077 bool non_constant_p, is_direct_init;
23078
23079 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
23080 set correctly. */
23081 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
23082 parser->greater_than_is_operator_p = !template_parm_p;
23083 /* Local variable names (and the `this' keyword) may not
23084 appear in a default argument. */
23085 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23086 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
23087 /* Parse the assignment-expression. */
23088 if (template_parm_p)
23089 push_deferring_access_checks (dk_no_deferred);
23090 tree saved_class_ptr = NULL_TREE;
23091 tree saved_class_ref = NULL_TREE;
23092 /* The "this" pointer is not valid in a default argument. */
23093 if (cfun)
23094 {
23095 saved_class_ptr = current_class_ptr;
23096 cp_function_chain->x_current_class_ptr = NULL_TREE;
23097 saved_class_ref = current_class_ref;
23098 cp_function_chain->x_current_class_ref = NULL_TREE;
23099 }
23100 default_argument
23101 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
23102 /* Restore the "this" pointer. */
23103 if (cfun)
23104 {
23105 cp_function_chain->x_current_class_ptr = saved_class_ptr;
23106 cp_function_chain->x_current_class_ref = saved_class_ref;
23107 }
23108 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
23109 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23110 if (template_parm_p)
23111 pop_deferring_access_checks ();
23112 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
23113 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
23114
23115 return default_argument;
23116 }
23117
23118 /* Parse a function-body.
23119
23120 function-body:
23121 compound_statement */
23122
23123 static void
23124 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
23125 {
23126 cp_parser_compound_statement (parser, NULL, (in_function_try_block
23127 ? BCS_TRY_BLOCK : BCS_NORMAL),
23128 true);
23129 }
23130
23131 /* Parse a ctor-initializer-opt followed by a function-body. Return
23132 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
23133 is true we are parsing a function-try-block. */
23134
23135 static void
23136 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
23137 bool in_function_try_block)
23138 {
23139 tree body, list;
23140 const bool check_body_p
23141 = (DECL_CONSTRUCTOR_P (current_function_decl)
23142 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
23143 tree last = NULL;
23144
23145 if (in_function_try_block
23146 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
23147 && cxx_dialect < cxx20)
23148 {
23149 if (DECL_CONSTRUCTOR_P (current_function_decl))
23150 pedwarn (input_location, 0,
23151 "function-try-block body of %<constexpr%> constructor only "
23152 "available with %<-std=c++20%> or %<-std=gnu++20%>");
23153 else
23154 pedwarn (input_location, 0,
23155 "function-try-block body of %<constexpr%> function only "
23156 "available with %<-std=c++20%> or %<-std=gnu++20%>");
23157 }
23158
23159 /* Begin the function body. */
23160 body = begin_function_body ();
23161 /* Parse the optional ctor-initializer. */
23162 cp_parser_ctor_initializer_opt (parser);
23163
23164 /* If we're parsing a constexpr constructor definition, we need
23165 to check that the constructor body is indeed empty. However,
23166 before we get to cp_parser_function_body lot of junk has been
23167 generated, so we can't just check that we have an empty block.
23168 Rather we take a snapshot of the outermost block, and check whether
23169 cp_parser_function_body changed its state. */
23170 if (check_body_p)
23171 {
23172 list = cur_stmt_list;
23173 if (STATEMENT_LIST_TAIL (list))
23174 last = STATEMENT_LIST_TAIL (list)->stmt;
23175 }
23176 /* Parse the function-body. */
23177 cp_parser_function_body (parser, in_function_try_block);
23178 if (check_body_p)
23179 check_constexpr_ctor_body (last, list, /*complain=*/true);
23180 /* Finish the function body. */
23181 finish_function_body (body);
23182 }
23183
23184 /* Parse an initializer.
23185
23186 initializer:
23187 = initializer-clause
23188 ( expression-list )
23189
23190 Returns an expression representing the initializer. If no
23191 initializer is present, NULL_TREE is returned.
23192
23193 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
23194 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
23195 set to TRUE if there is no initializer present. If there is an
23196 initializer, and it is not a constant-expression, *NON_CONSTANT_P
23197 is set to true; otherwise it is set to false. */
23198
23199 static tree
23200 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
23201 bool* non_constant_p, bool subexpression_p)
23202 {
23203 cp_token *token;
23204 tree init;
23205
23206 /* Peek at the next token. */
23207 token = cp_lexer_peek_token (parser->lexer);
23208
23209 /* Let our caller know whether or not this initializer was
23210 parenthesized. */
23211 *is_direct_init = (token->type != CPP_EQ);
23212 /* Assume that the initializer is constant. */
23213 *non_constant_p = false;
23214
23215 if (token->type == CPP_EQ)
23216 {
23217 /* Consume the `='. */
23218 cp_lexer_consume_token (parser->lexer);
23219 /* Parse the initializer-clause. */
23220 init = cp_parser_initializer_clause (parser, non_constant_p);
23221 }
23222 else if (token->type == CPP_OPEN_PAREN)
23223 {
23224 vec<tree, va_gc> *vec;
23225 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23226 /*cast_p=*/false,
23227 /*allow_expansion_p=*/true,
23228 non_constant_p);
23229 if (vec == NULL)
23230 return error_mark_node;
23231 init = build_tree_list_vec (vec);
23232 release_tree_vector (vec);
23233 }
23234 else if (token->type == CPP_OPEN_BRACE)
23235 {
23236 cp_lexer_set_source_position (parser->lexer);
23237 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23238 init = cp_parser_braced_list (parser, non_constant_p);
23239 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
23240 }
23241 else
23242 {
23243 /* Anything else is an error. */
23244 cp_parser_error (parser, "expected initializer");
23245 init = error_mark_node;
23246 }
23247
23248 if (!subexpression_p && check_for_bare_parameter_packs (init))
23249 init = error_mark_node;
23250
23251 return init;
23252 }
23253
23254 /* Parse an initializer-clause.
23255
23256 initializer-clause:
23257 assignment-expression
23258 braced-init-list
23259
23260 Returns an expression representing the initializer.
23261
23262 If the `assignment-expression' production is used the value
23263 returned is simply a representation for the expression.
23264
23265 Otherwise, calls cp_parser_braced_list. */
23266
23267 static cp_expr
23268 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
23269 {
23270 cp_expr initializer;
23271
23272 /* Assume the expression is constant. */
23273 *non_constant_p = false;
23274
23275 /* If it is not a `{', then we are looking at an
23276 assignment-expression. */
23277 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23278 {
23279 initializer
23280 = cp_parser_constant_expression (parser,
23281 /*allow_non_constant_p=*/true,
23282 non_constant_p);
23283 }
23284 else
23285 initializer = cp_parser_braced_list (parser, non_constant_p);
23286
23287 return initializer;
23288 }
23289
23290 /* Parse a brace-enclosed initializer list.
23291
23292 braced-init-list:
23293 { initializer-list , [opt] }
23294 { designated-initializer-list , [opt] }
23295 { }
23296
23297 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
23298 the elements of the initializer-list (or NULL, if the last
23299 production is used). The TREE_TYPE for the CONSTRUCTOR will be
23300 NULL_TREE. There is no way to detect whether or not the optional
23301 trailing `,' was provided. NON_CONSTANT_P is as for
23302 cp_parser_initializer. */
23303
23304 static cp_expr
23305 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
23306 {
23307 tree initializer;
23308 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
23309
23310 /* Consume the `{' token. */
23311 matching_braces braces;
23312 braces.require_open (parser);
23313 /* Create a CONSTRUCTOR to represent the braced-initializer. */
23314 initializer = make_node (CONSTRUCTOR);
23315 /* If it's not a `}', then there is a non-trivial initializer. */
23316 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
23317 {
23318 bool designated;
23319 /* Parse the initializer list. */
23320 CONSTRUCTOR_ELTS (initializer)
23321 = cp_parser_initializer_list (parser, non_constant_p, &designated);
23322 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
23323 /* A trailing `,' token is allowed. */
23324 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23325 cp_lexer_consume_token (parser->lexer);
23326 }
23327 else
23328 *non_constant_p = false;
23329 /* Now, there should be a trailing `}'. */
23330 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
23331 braces.require_close (parser);
23332 TREE_TYPE (initializer) = init_list_type_node;
23333
23334 cp_expr result (initializer);
23335 /* Build a location of the form:
23336 { ... }
23337 ^~~~~~~
23338 with caret==start at the open brace, finish at the close brace. */
23339 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
23340 result.set_location (combined_loc);
23341 return result;
23342 }
23343
23344 /* Consume tokens up to, and including, the next non-nested closing `]'.
23345 Returns true iff we found a closing `]'. */
23346
23347 static bool
23348 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
23349 {
23350 unsigned square_depth = 0;
23351
23352 while (true)
23353 {
23354 cp_token * token = cp_lexer_peek_token (parser->lexer);
23355
23356 switch (token->type)
23357 {
23358 case CPP_PRAGMA_EOL:
23359 if (!parser->lexer->in_pragma)
23360 break;
23361 /* FALLTHRU */
23362 case CPP_EOF:
23363 /* If we've run out of tokens, then there is no closing `]'. */
23364 return false;
23365
23366 case CPP_OPEN_SQUARE:
23367 ++square_depth;
23368 break;
23369
23370 case CPP_CLOSE_SQUARE:
23371 if (!square_depth--)
23372 {
23373 cp_lexer_consume_token (parser->lexer);
23374 return true;
23375 }
23376 break;
23377
23378 default:
23379 break;
23380 }
23381
23382 /* Consume the token. */
23383 cp_lexer_consume_token (parser->lexer);
23384 }
23385 }
23386
23387 /* Return true if we are looking at an array-designator, false otherwise. */
23388
23389 static bool
23390 cp_parser_array_designator_p (cp_parser *parser)
23391 {
23392 /* Consume the `['. */
23393 cp_lexer_consume_token (parser->lexer);
23394
23395 cp_lexer_save_tokens (parser->lexer);
23396
23397 /* Skip tokens until the next token is a closing square bracket.
23398 If we find the closing `]', and the next token is a `=', then
23399 we are looking at an array designator. */
23400 bool array_designator_p
23401 = (cp_parser_skip_to_closing_square_bracket (parser)
23402 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
23403
23404 /* Roll back the tokens we skipped. */
23405 cp_lexer_rollback_tokens (parser->lexer);
23406
23407 return array_designator_p;
23408 }
23409
23410 /* Parse an initializer-list.
23411
23412 initializer-list:
23413 initializer-clause ... [opt]
23414 initializer-list , initializer-clause ... [opt]
23415
23416 C++20 Extension:
23417
23418 designated-initializer-list:
23419 designated-initializer-clause
23420 designated-initializer-list , designated-initializer-clause
23421
23422 designated-initializer-clause:
23423 designator brace-or-equal-initializer
23424
23425 designator:
23426 . identifier
23427
23428 GNU Extension:
23429
23430 initializer-list:
23431 designation initializer-clause ...[opt]
23432 initializer-list , designation initializer-clause ...[opt]
23433
23434 designation:
23435 . identifier =
23436 identifier :
23437 [ constant-expression ] =
23438
23439 Returns a vec of constructor_elt. The VALUE of each elt is an expression
23440 for the initializer. If the INDEX of the elt is non-NULL, it is the
23441 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
23442 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
23443 are any designators. */
23444
23445 static vec<constructor_elt, va_gc> *
23446 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
23447 bool *designated)
23448 {
23449 vec<constructor_elt, va_gc> *v = NULL;
23450 bool first_p = true;
23451 tree first_designator = NULL_TREE;
23452
23453 /* Assume all of the expressions are constant. */
23454 *non_constant_p = false;
23455
23456 unsigned nelts = 0;
23457 int suppress = suppress_location_wrappers;
23458
23459 /* Parse the rest of the list. */
23460 while (true)
23461 {
23462 cp_token *token;
23463 tree designator;
23464 tree initializer;
23465 bool clause_non_constant_p;
23466 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23467
23468 /* Handle the C++20 syntax, '. id ='. */
23469 if ((cxx_dialect >= cxx20
23470 || cp_parser_allow_gnu_extensions_p (parser))
23471 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
23472 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
23473 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
23474 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
23475 == CPP_OPEN_BRACE)))
23476 {
23477 if (cxx_dialect < cxx20)
23478 pedwarn (loc, OPT_Wpedantic,
23479 "C++ designated initializers only available with "
23480 "%<-std=c++20%> or %<-std=gnu++20%>");
23481 /* Consume the `.'. */
23482 cp_lexer_consume_token (parser->lexer);
23483 /* Consume the identifier. */
23484 designator = cp_lexer_consume_token (parser->lexer)->u.value;
23485 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23486 /* Consume the `='. */
23487 cp_lexer_consume_token (parser->lexer);
23488 }
23489 /* Also, if the next token is an identifier and the following one is a
23490 colon, we are looking at the GNU designated-initializer
23491 syntax. */
23492 else if (cp_parser_allow_gnu_extensions_p (parser)
23493 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
23494 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23495 == CPP_COLON))
23496 {
23497 /* Warn the user that they are using an extension. */
23498 pedwarn (loc, OPT_Wpedantic,
23499 "ISO C++ does not allow GNU designated initializers");
23500 /* Consume the identifier. */
23501 designator = cp_lexer_consume_token (parser->lexer)->u.value;
23502 /* Consume the `:'. */
23503 cp_lexer_consume_token (parser->lexer);
23504 }
23505 /* Also handle C99 array designators, '[ const ] ='. */
23506 else if (cp_parser_allow_gnu_extensions_p (parser)
23507 && !c_dialect_objc ()
23508 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23509 {
23510 /* In C++11, [ could start a lambda-introducer. */
23511 bool non_const = false;
23512
23513 cp_parser_parse_tentatively (parser);
23514
23515 if (!cp_parser_array_designator_p (parser))
23516 {
23517 cp_parser_simulate_error (parser);
23518 designator = NULL_TREE;
23519 }
23520 else
23521 {
23522 designator = cp_parser_constant_expression (parser, true,
23523 &non_const);
23524 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23525 cp_parser_require (parser, CPP_EQ, RT_EQ);
23526 }
23527
23528 if (!cp_parser_parse_definitely (parser))
23529 designator = NULL_TREE;
23530 else if (non_const
23531 && (!require_potential_rvalue_constant_expression
23532 (designator)))
23533 designator = NULL_TREE;
23534 if (designator)
23535 /* Warn the user that they are using an extension. */
23536 pedwarn (loc, OPT_Wpedantic,
23537 "ISO C++ does not allow C99 designated initializers");
23538 }
23539 else
23540 designator = NULL_TREE;
23541
23542 if (first_p)
23543 {
23544 first_designator = designator;
23545 first_p = false;
23546 }
23547 else if (cxx_dialect >= cxx20
23548 && first_designator != error_mark_node
23549 && (!first_designator != !designator))
23550 {
23551 error_at (loc, "either all initializer clauses should be designated "
23552 "or none of them should be");
23553 first_designator = error_mark_node;
23554 }
23555 else if (cxx_dialect < cxx20 && !first_designator)
23556 first_designator = designator;
23557
23558 /* Parse the initializer. */
23559 initializer = cp_parser_initializer_clause (parser,
23560 &clause_non_constant_p);
23561 /* If any clause is non-constant, so is the entire initializer. */
23562 if (clause_non_constant_p)
23563 *non_constant_p = true;
23564
23565 /* If we have an ellipsis, this is an initializer pack
23566 expansion. */
23567 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23568 {
23569 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23570
23571 /* Consume the `...'. */
23572 cp_lexer_consume_token (parser->lexer);
23573
23574 if (designator && cxx_dialect >= cxx20)
23575 error_at (loc,
23576 "%<...%> not allowed in designated initializer list");
23577
23578 /* Turn the initializer into an initializer expansion. */
23579 initializer = make_pack_expansion (initializer);
23580 }
23581
23582 /* Add it to the vector. */
23583 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
23584
23585 /* If the next token is not a comma, we have reached the end of
23586 the list. */
23587 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23588 break;
23589
23590 /* Peek at the next token. */
23591 token = cp_lexer_peek_nth_token (parser->lexer, 2);
23592 /* If the next token is a `}', then we're still done. An
23593 initializer-clause can have a trailing `,' after the
23594 initializer-list and before the closing `}'. */
23595 if (token->type == CPP_CLOSE_BRACE)
23596 break;
23597
23598 /* Suppress location wrappers in a long initializer to save memory
23599 (14179). The cutoff is chosen arbitrarily. */
23600 const unsigned loc_max = 256;
23601 unsigned incr = 1;
23602 if (TREE_CODE (initializer) == CONSTRUCTOR)
23603 /* Look one level down because it's easy. Looking deeper would require
23604 passing down a nelts pointer, and I don't think multi-level massive
23605 initializers are common enough to justify this. */
23606 incr = CONSTRUCTOR_NELTS (initializer);
23607 nelts += incr;
23608 if (nelts >= loc_max && (nelts - incr) < loc_max)
23609 ++suppress_location_wrappers;
23610
23611 /* Consume the `,' token. */
23612 cp_lexer_consume_token (parser->lexer);
23613 }
23614
23615 /* The same identifier shall not appear in multiple designators
23616 of a designated-initializer-list. */
23617 if (first_designator)
23618 {
23619 unsigned int i;
23620 tree designator, val;
23621 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23622 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23623 {
23624 if (IDENTIFIER_MARKED (designator))
23625 {
23626 error_at (cp_expr_loc_or_input_loc (val),
23627 "%<.%s%> designator used multiple times in "
23628 "the same initializer list",
23629 IDENTIFIER_POINTER (designator));
23630 (*v)[i].index = error_mark_node;
23631 }
23632 else
23633 IDENTIFIER_MARKED (designator) = 1;
23634 }
23635 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23636 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23637 IDENTIFIER_MARKED (designator) = 0;
23638 }
23639
23640 suppress_location_wrappers = suppress;
23641
23642 *designated = first_designator != NULL_TREE;
23643 return v;
23644 }
23645
23646 /* Classes [gram.class] */
23647
23648 /* Parse a class-name.
23649
23650 class-name:
23651 identifier
23652 template-id
23653
23654 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23655 to indicate that names looked up in dependent types should be
23656 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23657 keyword has been used to indicate that the name that appears next
23658 is a template. TAG_TYPE indicates the explicit tag given before
23659 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23660 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23661 is the class being defined in a class-head. If ENUM_OK is TRUE,
23662 enum-names are also accepted.
23663
23664 Returns the TYPE_DECL representing the class. */
23665
23666 static tree
23667 cp_parser_class_name (cp_parser *parser,
23668 bool typename_keyword_p,
23669 bool template_keyword_p,
23670 enum tag_types tag_type,
23671 bool check_dependency_p,
23672 bool class_head_p,
23673 bool is_declaration,
23674 bool enum_ok)
23675 {
23676 tree decl;
23677 tree scope;
23678 bool typename_p;
23679 cp_token *token;
23680 tree identifier = NULL_TREE;
23681
23682 /* All class-names start with an identifier. */
23683 token = cp_lexer_peek_token (parser->lexer);
23684 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
23685 {
23686 cp_parser_error (parser, "expected class-name");
23687 return error_mark_node;
23688 }
23689
23690 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23691 to a template-id, so we save it here. Consider object scope too,
23692 so that make_typename_type below can use it (cp_parser_template_name
23693 considers object scope also). This may happen with code like
23694
23695 p->template A<T>::a()
23696
23697 where we first want to look up A<T>::a in the class of the object
23698 expression, as per [basic.lookup.classref]. */
23699 scope = parser->scope ? parser->scope : parser->context->object_type;
23700 if (scope == error_mark_node)
23701 return error_mark_node;
23702
23703 /* Any name names a type if we're following the `typename' keyword
23704 in a qualified name where the enclosing scope is type-dependent. */
23705 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
23706 && dependent_type_p (scope));
23707 /* Handle the common case (an identifier, but not a template-id)
23708 efficiently. */
23709 if (token->type == CPP_NAME
23710 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
23711 {
23712 cp_token *identifier_token;
23713 bool ambiguous_p;
23714
23715 /* Look for the identifier. */
23716 identifier_token = cp_lexer_peek_token (parser->lexer);
23717 ambiguous_p = identifier_token->error_reported;
23718 identifier = cp_parser_identifier (parser);
23719 /* If the next token isn't an identifier, we are certainly not
23720 looking at a class-name. */
23721 if (identifier == error_mark_node)
23722 decl = error_mark_node;
23723 /* If we know this is a type-name, there's no need to look it
23724 up. */
23725 else if (typename_p)
23726 decl = identifier;
23727 else
23728 {
23729 tree ambiguous_decls;
23730 /* If we already know that this lookup is ambiguous, then
23731 we've already issued an error message; there's no reason
23732 to check again. */
23733 if (ambiguous_p)
23734 {
23735 cp_parser_simulate_error (parser);
23736 return error_mark_node;
23737 }
23738 /* If the next token is a `::', then the name must be a type
23739 name.
23740
23741 [basic.lookup.qual]
23742
23743 During the lookup for a name preceding the :: scope
23744 resolution operator, object, function, and enumerator
23745 names are ignored. */
23746 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23747 tag_type = scope_type;
23748 /* Look up the name. */
23749 decl = cp_parser_lookup_name (parser, identifier,
23750 tag_type,
23751 /*is_template=*/false,
23752 /*is_namespace=*/false,
23753 check_dependency_p,
23754 &ambiguous_decls,
23755 identifier_token->location);
23756 if (ambiguous_decls)
23757 {
23758 if (cp_parser_parsing_tentatively (parser))
23759 cp_parser_simulate_error (parser);
23760 return error_mark_node;
23761 }
23762 }
23763 }
23764 else
23765 {
23766 /* Try a template-id. */
23767 decl = cp_parser_template_id (parser, template_keyword_p,
23768 check_dependency_p,
23769 tag_type,
23770 is_declaration);
23771 if (decl == error_mark_node)
23772 return error_mark_node;
23773 }
23774
23775 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
23776
23777 /* If this is a typename, create a TYPENAME_TYPE. */
23778 if (typename_p
23779 && decl != error_mark_node
23780 && !is_overloaded_fn (decl))
23781 {
23782 decl = make_typename_type (scope, decl, typename_type,
23783 /*complain=*/tf_error);
23784 if (decl != error_mark_node)
23785 decl = TYPE_NAME (decl);
23786 }
23787
23788 decl = strip_using_decl (decl);
23789
23790 /* Check to see that it is really the name of a class. */
23791 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
23792 && identifier_p (TREE_OPERAND (decl, 0))
23793 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23794 /* Situations like this:
23795
23796 template <typename T> struct A {
23797 typename T::template X<int>::I i;
23798 };
23799
23800 are problematic. Is `T::template X<int>' a class-name? The
23801 standard does not seem to be definitive, but there is no other
23802 valid interpretation of the following `::'. Therefore, those
23803 names are considered class-names. */
23804 {
23805 decl = make_typename_type (scope, decl, tag_type, tf_error);
23806 if (decl != error_mark_node)
23807 decl = TYPE_NAME (decl);
23808 }
23809 else if (TREE_CODE (decl) != TYPE_DECL
23810 || TREE_TYPE (decl) == error_mark_node
23811 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
23812 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
23813 /* In Objective-C 2.0, a classname followed by '.' starts a
23814 dot-syntax expression, and it's not a type-name. */
23815 || (c_dialect_objc ()
23816 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
23817 && objc_is_class_name (decl)))
23818 decl = error_mark_node;
23819
23820 if (decl == error_mark_node)
23821 cp_parser_error (parser, "expected class-name");
23822 else if (identifier && !parser->scope)
23823 maybe_note_name_used_in_class (identifier, decl);
23824
23825 return decl;
23826 }
23827
23828 /* Make sure that any member-function parameters are in scope.
23829 For instance, a function's noexcept-specifier can use the function's
23830 parameters:
23831
23832 struct S {
23833 void fn (int p) noexcept(noexcept(p));
23834 };
23835
23836 so we need to make sure name lookup can find them. This is used
23837 when we delay parsing of the noexcept-specifier. */
23838
23839 static void
23840 inject_parm_decls (tree decl)
23841 {
23842 begin_scope (sk_function_parms, decl);
23843 tree args = DECL_ARGUMENTS (decl);
23844
23845 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
23846
23847 if (args && is_this_parameter (args))
23848 {
23849 gcc_checking_assert (current_class_ptr == NULL_TREE);
23850 current_class_ptr = NULL_TREE;
23851 current_class_ref = cp_build_fold_indirect_ref (args);
23852 current_class_ptr = args;
23853 }
23854 }
23855
23856 /* Undo the effects of inject_parm_decls. */
23857
23858 static void
23859 pop_injected_parms (void)
23860 {
23861 pop_bindings_and_leave_scope ();
23862 current_class_ptr = current_class_ref = NULL_TREE;
23863 }
23864
23865 /* Parse a class-specifier.
23866
23867 class-specifier:
23868 class-head { member-specification [opt] }
23869
23870 Returns the TREE_TYPE representing the class. */
23871
23872 static tree
23873 cp_parser_class_specifier_1 (cp_parser* parser)
23874 {
23875 tree type;
23876 tree attributes = NULL_TREE;
23877 bool nested_name_specifier_p;
23878 unsigned saved_num_template_parameter_lists;
23879 bool saved_in_function_body;
23880 unsigned char in_statement;
23881 bool in_switch_statement_p;
23882 bool saved_in_unbraced_linkage_specification_p;
23883 tree old_scope = NULL_TREE;
23884 tree scope = NULL_TREE;
23885 cp_token *closing_brace;
23886
23887 push_deferring_access_checks (dk_no_deferred);
23888
23889 /* Parse the class-head. */
23890 type = cp_parser_class_head (parser,
23891 &nested_name_specifier_p);
23892 /* If the class-head was a semantic disaster, skip the entire body
23893 of the class. */
23894 if (!type)
23895 {
23896 cp_parser_skip_to_end_of_block_or_statement (parser);
23897 pop_deferring_access_checks ();
23898 return error_mark_node;
23899 }
23900
23901 /* Look for the `{'. */
23902 matching_braces braces;
23903 if (!braces.require_open (parser))
23904 {
23905 pop_deferring_access_checks ();
23906 return error_mark_node;
23907 }
23908
23909 cp_ensure_no_omp_declare_simd (parser);
23910 cp_ensure_no_oacc_routine (parser);
23911
23912 /* Issue an error message if type-definitions are forbidden here. */
23913 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
23914 /* Remember that we are defining one more class. */
23915 ++parser->num_classes_being_defined;
23916 /* Inside the class, surrounding template-parameter-lists do not
23917 apply. */
23918 saved_num_template_parameter_lists
23919 = parser->num_template_parameter_lists;
23920 parser->num_template_parameter_lists = 0;
23921 /* We are not in a function body. */
23922 saved_in_function_body = parser->in_function_body;
23923 parser->in_function_body = false;
23924 /* Or in a loop. */
23925 in_statement = parser->in_statement;
23926 parser->in_statement = 0;
23927 /* Or in a switch. */
23928 in_switch_statement_p = parser->in_switch_statement_p;
23929 parser->in_switch_statement_p = false;
23930 /* We are not immediately inside an extern "lang" block. */
23931 saved_in_unbraced_linkage_specification_p
23932 = parser->in_unbraced_linkage_specification_p;
23933 parser->in_unbraced_linkage_specification_p = false;
23934
23935 // Associate constraints with the type.
23936 if (flag_concepts)
23937 type = associate_classtype_constraints (type);
23938
23939 /* Start the class. */
23940 if (nested_name_specifier_p)
23941 {
23942 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
23943 /* SCOPE must be a scope nested inside current scope. */
23944 if (is_nested_namespace (current_namespace,
23945 decl_namespace_context (scope)))
23946 old_scope = push_inner_scope (scope);
23947 else
23948 nested_name_specifier_p = false;
23949 }
23950 type = begin_class_definition (type);
23951
23952 if (type == error_mark_node)
23953 /* If the type is erroneous, skip the entire body of the class. */
23954 cp_parser_skip_to_closing_brace (parser);
23955 else
23956 /* Parse the member-specification. */
23957 cp_parser_member_specification_opt (parser);
23958
23959 /* Look for the trailing `}'. */
23960 closing_brace = braces.require_close (parser);
23961 /* Look for trailing attributes to apply to this class. */
23962 if (cp_parser_allow_gnu_extensions_p (parser))
23963 attributes = cp_parser_gnu_attributes_opt (parser);
23964 if (type != error_mark_node)
23965 type = finish_struct (type, attributes);
23966 if (nested_name_specifier_p)
23967 pop_inner_scope (old_scope, scope);
23968
23969 /* We've finished a type definition. Check for the common syntax
23970 error of forgetting a semicolon after the definition. We need to
23971 be careful, as we can't just check for not-a-semicolon and be done
23972 with it; the user might have typed:
23973
23974 class X { } c = ...;
23975 class X { } *p = ...;
23976
23977 and so forth. Instead, enumerate all the possible tokens that
23978 might follow this production; if we don't see one of them, then
23979 complain and silently insert the semicolon. */
23980 {
23981 cp_token *token = cp_lexer_peek_token (parser->lexer);
23982 bool want_semicolon = true;
23983
23984 if (cp_next_tokens_can_be_std_attribute_p (parser))
23985 /* Don't try to parse c++11 attributes here. As per the
23986 grammar, that should be a task for
23987 cp_parser_decl_specifier_seq. */
23988 want_semicolon = false;
23989
23990 switch (token->type)
23991 {
23992 case CPP_NAME:
23993 case CPP_SEMICOLON:
23994 case CPP_MULT:
23995 case CPP_AND:
23996 case CPP_OPEN_PAREN:
23997 case CPP_CLOSE_PAREN:
23998 case CPP_COMMA:
23999 want_semicolon = false;
24000 break;
24001
24002 /* While it's legal for type qualifiers and storage class
24003 specifiers to follow type definitions in the grammar, only
24004 compiler testsuites contain code like that. Assume that if
24005 we see such code, then what we're really seeing is a case
24006 like:
24007
24008 class X { }
24009 const <type> var = ...;
24010
24011 or
24012
24013 class Y { }
24014 static <type> func (...) ...
24015
24016 i.e. the qualifier or specifier applies to the next
24017 declaration. To do so, however, we need to look ahead one
24018 more token to see if *that* token is a type specifier.
24019
24020 This code could be improved to handle:
24021
24022 class Z { }
24023 static const <type> var = ...; */
24024 case CPP_KEYWORD:
24025 if (keyword_is_decl_specifier (token->keyword))
24026 {
24027 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
24028
24029 /* Handling user-defined types here would be nice, but very
24030 tricky. */
24031 want_semicolon
24032 = (lookahead->type == CPP_KEYWORD
24033 && keyword_begins_type_specifier (lookahead->keyword));
24034 }
24035 break;
24036 default:
24037 break;
24038 }
24039
24040 /* If we don't have a type, then something is very wrong and we
24041 shouldn't try to do anything clever. Likewise for not seeing the
24042 closing brace. */
24043 if (closing_brace && TYPE_P (type) && want_semicolon)
24044 {
24045 /* Locate the closing brace. */
24046 cp_token_position prev
24047 = cp_lexer_previous_token_position (parser->lexer);
24048 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
24049 location_t loc = prev_token->location;
24050
24051 /* We want to suggest insertion of a ';' immediately *after* the
24052 closing brace, so, if we can, offset the location by 1 column. */
24053 location_t next_loc = loc;
24054 if (!linemap_location_from_macro_expansion_p (line_table, loc))
24055 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
24056
24057 rich_location richloc (line_table, next_loc);
24058
24059 /* If we successfully offset the location, suggest the fix-it. */
24060 if (next_loc != loc)
24061 richloc.add_fixit_insert_before (next_loc, ";");
24062
24063 if (CLASSTYPE_DECLARED_CLASS (type))
24064 error_at (&richloc,
24065 "expected %<;%> after class definition");
24066 else if (TREE_CODE (type) == RECORD_TYPE)
24067 error_at (&richloc,
24068 "expected %<;%> after struct definition");
24069 else if (TREE_CODE (type) == UNION_TYPE)
24070 error_at (&richloc,
24071 "expected %<;%> after union definition");
24072 else
24073 gcc_unreachable ();
24074
24075 /* Unget one token and smash it to look as though we encountered
24076 a semicolon in the input stream. */
24077 cp_lexer_set_token_position (parser->lexer, prev);
24078 token = cp_lexer_peek_token (parser->lexer);
24079 token->type = CPP_SEMICOLON;
24080 token->keyword = RID_MAX;
24081 }
24082 }
24083
24084 /* If this class is not itself within the scope of another class,
24085 then we need to parse the bodies of all of the queued function
24086 definitions. Note that the queued functions defined in a class
24087 are not always processed immediately following the
24088 class-specifier for that class. Consider:
24089
24090 struct A {
24091 struct B { void f() { sizeof (A); } };
24092 };
24093
24094 If `f' were processed before the processing of `A' were
24095 completed, there would be no way to compute the size of `A'.
24096 Note that the nesting we are interested in here is lexical --
24097 not the semantic nesting given by TYPE_CONTEXT. In particular,
24098 for:
24099
24100 struct A { struct B; };
24101 struct A::B { void f() { } };
24102
24103 there is no need to delay the parsing of `A::B::f'. */
24104 if (--parser->num_classes_being_defined == 0)
24105 {
24106 tree decl;
24107 tree class_type = NULL_TREE;
24108 tree pushed_scope = NULL_TREE;
24109 unsigned ix;
24110 cp_default_arg_entry *e;
24111 tree save_ccp, save_ccr;
24112
24113 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
24114 {
24115 /* Skip default arguments, NSDMIs, etc, in order to improve
24116 error recovery (c++/71169, c++/71832). */
24117 vec_safe_truncate (unparsed_funs_with_default_args, 0);
24118 vec_safe_truncate (unparsed_nsdmis, 0);
24119 vec_safe_truncate (unparsed_funs_with_definitions, 0);
24120 }
24121
24122 /* In a first pass, parse default arguments to the functions.
24123 Then, in a second pass, parse the bodies of the functions.
24124 This two-phased approach handles cases like:
24125
24126 struct S {
24127 void f() { g(); }
24128 void g(int i = 3);
24129 };
24130
24131 */
24132 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
24133 {
24134 decl = e->decl;
24135 /* If there are default arguments that have not yet been processed,
24136 take care of them now. */
24137 if (class_type != e->class_type)
24138 {
24139 if (pushed_scope)
24140 pop_scope (pushed_scope);
24141 class_type = e->class_type;
24142 pushed_scope = push_scope (class_type);
24143 }
24144 /* Make sure that any template parameters are in scope. */
24145 maybe_begin_member_template_processing (decl);
24146 /* Parse the default argument expressions. */
24147 cp_parser_late_parsing_default_args (parser, decl);
24148 /* Remove any template parameters from the symbol table. */
24149 maybe_end_member_template_processing ();
24150 }
24151 vec_safe_truncate (unparsed_funs_with_default_args, 0);
24152 /* Now parse any NSDMIs. */
24153 save_ccp = current_class_ptr;
24154 save_ccr = current_class_ref;
24155 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
24156 {
24157 if (class_type != DECL_CONTEXT (decl))
24158 {
24159 if (pushed_scope)
24160 pop_scope (pushed_scope);
24161 class_type = DECL_CONTEXT (decl);
24162 pushed_scope = push_scope (class_type);
24163 }
24164 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
24165 cp_parser_late_parsing_nsdmi (parser, decl);
24166 }
24167 vec_safe_truncate (unparsed_nsdmis, 0);
24168 current_class_ptr = save_ccp;
24169 current_class_ref = save_ccr;
24170 if (pushed_scope)
24171 pop_scope (pushed_scope);
24172
24173 /* If there are noexcept-specifiers that have not yet been processed,
24174 take care of them now. */
24175 class_type = NULL_TREE;
24176 pushed_scope = NULL_TREE;
24177 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
24178 {
24179 tree ctx = DECL_CONTEXT (decl);
24180 if (class_type != ctx)
24181 {
24182 if (pushed_scope)
24183 pop_scope (pushed_scope);
24184 class_type = ctx;
24185 pushed_scope = push_scope (class_type);
24186 }
24187
24188 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
24189 spec = TREE_PURPOSE (spec);
24190
24191 /* Make sure that any template parameters are in scope. */
24192 maybe_begin_member_template_processing (decl);
24193
24194 /* Make sure that any member-function parameters are in scope. */
24195 inject_parm_decls (decl);
24196
24197 /* 'this' is not allowed in static member functions. */
24198 unsigned char local_variables_forbidden_p
24199 = parser->local_variables_forbidden_p;
24200 if (DECL_THIS_STATIC (decl))
24201 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
24202
24203 /* Now we can parse the noexcept-specifier. */
24204 spec = cp_parser_late_noexcept_specifier (parser, spec);
24205
24206 if (spec != error_mark_node)
24207 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
24208
24209 /* Restore the state of local_variables_forbidden_p. */
24210 parser->local_variables_forbidden_p = local_variables_forbidden_p;
24211
24212 /* The finish_struct call above performed various override checking,
24213 but it skipped unparsed noexcept-specifier operands. Now that we
24214 have resolved them, check again. */
24215 noexcept_override_late_checks (type, decl);
24216
24217 /* Remove any member-function parameters from the symbol table. */
24218 pop_injected_parms ();
24219
24220 /* Remove any template parameters from the symbol table. */
24221 maybe_end_member_template_processing ();
24222 }
24223 vec_safe_truncate (unparsed_noexcepts, 0);
24224 if (pushed_scope)
24225 pop_scope (pushed_scope);
24226
24227 /* Now parse the body of the functions. */
24228 if (flag_openmp)
24229 {
24230 /* OpenMP UDRs need to be parsed before all other functions. */
24231 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
24232 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
24233 cp_parser_late_parsing_for_member (parser, decl);
24234 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
24235 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
24236 cp_parser_late_parsing_for_member (parser, decl);
24237 }
24238 else
24239 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
24240 cp_parser_late_parsing_for_member (parser, decl);
24241 vec_safe_truncate (unparsed_funs_with_definitions, 0);
24242 }
24243
24244 /* Put back any saved access checks. */
24245 pop_deferring_access_checks ();
24246
24247 /* Restore saved state. */
24248 parser->in_switch_statement_p = in_switch_statement_p;
24249 parser->in_statement = in_statement;
24250 parser->in_function_body = saved_in_function_body;
24251 parser->num_template_parameter_lists
24252 = saved_num_template_parameter_lists;
24253 parser->in_unbraced_linkage_specification_p
24254 = saved_in_unbraced_linkage_specification_p;
24255
24256 return type;
24257 }
24258
24259 static tree
24260 cp_parser_class_specifier (cp_parser* parser)
24261 {
24262 tree ret;
24263 timevar_push (TV_PARSE_STRUCT);
24264 ret = cp_parser_class_specifier_1 (parser);
24265 timevar_pop (TV_PARSE_STRUCT);
24266 return ret;
24267 }
24268
24269 /* Parse a class-head.
24270
24271 class-head:
24272 class-key identifier [opt] base-clause [opt]
24273 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
24274 class-key nested-name-specifier [opt] template-id
24275 base-clause [opt]
24276
24277 class-virt-specifier:
24278 final
24279
24280 GNU Extensions:
24281 class-key attributes identifier [opt] base-clause [opt]
24282 class-key attributes nested-name-specifier identifier base-clause [opt]
24283 class-key attributes nested-name-specifier [opt] template-id
24284 base-clause [opt]
24285
24286 Upon return BASES is initialized to the list of base classes (or
24287 NULL, if there are none) in the same form returned by
24288 cp_parser_base_clause.
24289
24290 Returns the TYPE of the indicated class. Sets
24291 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
24292 involving a nested-name-specifier was used, and FALSE otherwise.
24293
24294 Returns error_mark_node if this is not a class-head.
24295
24296 Returns NULL_TREE if the class-head is syntactically valid, but
24297 semantically invalid in a way that means we should skip the entire
24298 body of the class. */
24299
24300 static tree
24301 cp_parser_class_head (cp_parser* parser,
24302 bool* nested_name_specifier_p)
24303 {
24304 tree nested_name_specifier;
24305 enum tag_types class_key;
24306 tree id = NULL_TREE;
24307 tree type = NULL_TREE;
24308 tree attributes;
24309 tree bases;
24310 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
24311 bool template_id_p = false;
24312 bool qualified_p = false;
24313 bool invalid_nested_name_p = false;
24314 bool invalid_explicit_specialization_p = false;
24315 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24316 tree pushed_scope = NULL_TREE;
24317 unsigned num_templates;
24318 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
24319 /* Assume no nested-name-specifier will be present. */
24320 *nested_name_specifier_p = false;
24321 /* Assume no template parameter lists will be used in defining the
24322 type. */
24323 num_templates = 0;
24324 parser->colon_corrects_to_scope_p = false;
24325
24326 /* Look for the class-key. */
24327 class_key = cp_parser_class_key (parser);
24328 if (class_key == none_type)
24329 return error_mark_node;
24330
24331 location_t class_head_start_location = input_location;
24332
24333 /* Parse the attributes. */
24334 attributes = cp_parser_attributes_opt (parser);
24335
24336 /* If the next token is `::', that is invalid -- but sometimes
24337 people do try to write:
24338
24339 struct ::S {};
24340
24341 Handle this gracefully by accepting the extra qualifier, and then
24342 issuing an error about it later if this really is a
24343 class-head. If it turns out just to be an elaborated type
24344 specifier, remain silent. */
24345 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
24346 qualified_p = true;
24347
24348 push_deferring_access_checks (dk_no_check);
24349
24350 /* Determine the name of the class. Begin by looking for an
24351 optional nested-name-specifier. */
24352 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
24353 nested_name_specifier
24354 = cp_parser_nested_name_specifier_opt (parser,
24355 /*typename_keyword_p=*/false,
24356 /*check_dependency_p=*/false,
24357 /*type_p=*/true,
24358 /*is_declaration=*/false);
24359 /* If there was a nested-name-specifier, then there *must* be an
24360 identifier. */
24361
24362 cp_token *bad_template_keyword = NULL;
24363
24364 if (nested_name_specifier)
24365 {
24366 type_start_token = cp_lexer_peek_token (parser->lexer);
24367 /* Although the grammar says `identifier', it really means
24368 `class-name' or `template-name'. You are only allowed to
24369 define a class that has already been declared with this
24370 syntax.
24371
24372 The proposed resolution for Core Issue 180 says that wherever
24373 you see `class T::X' you should treat `X' as a type-name.
24374
24375 It is OK to define an inaccessible class; for example:
24376
24377 class A { class B; };
24378 class A::B {};
24379
24380 We do not know if we will see a class-name, or a
24381 template-name. We look for a class-name first, in case the
24382 class-name is a template-id; if we looked for the
24383 template-name first we would stop after the template-name. */
24384 cp_parser_parse_tentatively (parser);
24385 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24386 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
24387 type = cp_parser_class_name (parser,
24388 /*typename_keyword_p=*/false,
24389 /*template_keyword_p=*/false,
24390 class_type,
24391 /*check_dependency_p=*/false,
24392 /*class_head_p=*/true,
24393 /*is_declaration=*/false);
24394 /* If that didn't work, ignore the nested-name-specifier. */
24395 if (!cp_parser_parse_definitely (parser))
24396 {
24397 invalid_nested_name_p = true;
24398 type_start_token = cp_lexer_peek_token (parser->lexer);
24399 id = cp_parser_identifier (parser);
24400 if (id == error_mark_node)
24401 id = NULL_TREE;
24402 }
24403 /* If we could not find a corresponding TYPE, treat this
24404 declaration like an unqualified declaration. */
24405 if (type == error_mark_node)
24406 nested_name_specifier = NULL_TREE;
24407 /* Otherwise, count the number of templates used in TYPE and its
24408 containing scopes. */
24409 else
24410 num_templates = num_template_headers_for_class (TREE_TYPE (type));
24411 }
24412 /* Otherwise, the identifier is optional. */
24413 else
24414 {
24415 /* We don't know whether what comes next is a template-id,
24416 an identifier, or nothing at all. */
24417 cp_parser_parse_tentatively (parser);
24418 /* Check for a template-id. */
24419 type_start_token = cp_lexer_peek_token (parser->lexer);
24420 id = cp_parser_template_id (parser,
24421 /*template_keyword_p=*/false,
24422 /*check_dependency_p=*/true,
24423 class_key,
24424 /*is_declaration=*/true);
24425 /* If that didn't work, it could still be an identifier. */
24426 if (!cp_parser_parse_definitely (parser))
24427 {
24428 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24429 {
24430 type_start_token = cp_lexer_peek_token (parser->lexer);
24431 id = cp_parser_identifier (parser);
24432 }
24433 else
24434 id = NULL_TREE;
24435 }
24436 else
24437 {
24438 template_id_p = true;
24439 ++num_templates;
24440 }
24441 }
24442
24443 pop_deferring_access_checks ();
24444
24445 if (id)
24446 {
24447 cp_parser_check_for_invalid_template_id (parser, id,
24448 class_key,
24449 type_start_token->location);
24450 }
24451 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
24452
24453 /* If it's not a `:' or a `{' then we can't really be looking at a
24454 class-head, since a class-head only appears as part of a
24455 class-specifier. We have to detect this situation before calling
24456 xref_tag, since that has irreversible side-effects. */
24457 if (!cp_parser_next_token_starts_class_definition_p (parser))
24458 {
24459 cp_parser_error (parser, "expected %<{%> or %<:%>");
24460 type = error_mark_node;
24461 goto out;
24462 }
24463
24464 /* At this point, we're going ahead with the class-specifier, even
24465 if some other problem occurs. */
24466 cp_parser_commit_to_tentative_parse (parser);
24467 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
24468 {
24469 cp_parser_error (parser,
24470 "cannot specify %<override%> for a class");
24471 type = error_mark_node;
24472 goto out;
24473 }
24474 /* Issue the error about the overly-qualified name now. */
24475 if (qualified_p)
24476 {
24477 cp_parser_error (parser,
24478 "global qualification of class name is invalid");
24479 type = error_mark_node;
24480 goto out;
24481 }
24482 else if (invalid_nested_name_p)
24483 {
24484 cp_parser_error (parser,
24485 "qualified name does not name a class");
24486 type = error_mark_node;
24487 goto out;
24488 }
24489 else if (nested_name_specifier)
24490 {
24491 tree scope;
24492
24493 if (bad_template_keyword)
24494 /* [temp.names]: in a qualified-id formed by a class-head-name, the
24495 keyword template shall not appear at the top level. */
24496 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
24497 "keyword %<template%> not allowed in class-head-name");
24498
24499 /* Reject typedef-names in class heads. */
24500 if (!DECL_IMPLICIT_TYPEDEF_P (type))
24501 {
24502 error_at (type_start_token->location,
24503 "invalid class name in declaration of %qD",
24504 type);
24505 type = NULL_TREE;
24506 goto done;
24507 }
24508
24509 /* Figure out in what scope the declaration is being placed. */
24510 scope = current_scope ();
24511 /* If that scope does not contain the scope in which the
24512 class was originally declared, the program is invalid. */
24513 if (scope && !is_ancestor (scope, nested_name_specifier))
24514 {
24515 if (at_namespace_scope_p ())
24516 error_at (type_start_token->location,
24517 "declaration of %qD in namespace %qD which does not "
24518 "enclose %qD",
24519 type, scope, nested_name_specifier);
24520 else
24521 error_at (type_start_token->location,
24522 "declaration of %qD in %qD which does not enclose %qD",
24523 type, scope, nested_name_specifier);
24524 type = NULL_TREE;
24525 goto done;
24526 }
24527 /* [dcl.meaning]
24528
24529 A declarator-id shall not be qualified except for the
24530 definition of a ... nested class outside of its class
24531 ... [or] the definition or explicit instantiation of a
24532 class member of a namespace outside of its namespace. */
24533 if (scope == nested_name_specifier)
24534 permerror (nested_name_specifier_token_start->location,
24535 "extra qualification not allowed");
24536 }
24537 /* An explicit-specialization must be preceded by "template <>". If
24538 it is not, try to recover gracefully. */
24539 if (at_namespace_scope_p ()
24540 && parser->num_template_parameter_lists == 0
24541 && !processing_template_parmlist
24542 && template_id_p)
24543 {
24544 /* Build a location of this form:
24545 struct typename <ARGS>
24546 ^~~~~~~~~~~~~~~~~~~~~~
24547 with caret==start at the start token, and
24548 finishing at the end of the type. */
24549 location_t reported_loc
24550 = make_location (class_head_start_location,
24551 class_head_start_location,
24552 get_finish (type_start_token->location));
24553 rich_location richloc (line_table, reported_loc);
24554 richloc.add_fixit_insert_before (class_head_start_location,
24555 "template <> ");
24556 error_at (&richloc,
24557 "an explicit specialization must be preceded by"
24558 " %<template <>%>");
24559 invalid_explicit_specialization_p = true;
24560 /* Take the same action that would have been taken by
24561 cp_parser_explicit_specialization. */
24562 ++parser->num_template_parameter_lists;
24563 begin_specialization ();
24564 }
24565 /* There must be no "return" statements between this point and the
24566 end of this function; set "type "to the correct return value and
24567 use "goto done;" to return. */
24568 /* Make sure that the right number of template parameters were
24569 present. */
24570 if (!cp_parser_check_template_parameters (parser, num_templates,
24571 template_id_p,
24572 type_start_token->location,
24573 /*declarator=*/NULL))
24574 {
24575 /* If something went wrong, there is no point in even trying to
24576 process the class-definition. */
24577 type = NULL_TREE;
24578 goto done;
24579 }
24580
24581 /* Look up the type. */
24582 if (template_id_p)
24583 {
24584 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
24585 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
24586 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
24587 {
24588 error_at (type_start_token->location,
24589 "function template %qD redeclared as a class template", id);
24590 type = error_mark_node;
24591 }
24592 else
24593 {
24594 type = TREE_TYPE (id);
24595 type = maybe_process_partial_specialization (type);
24596
24597 /* Check the scope while we still know whether or not we had a
24598 nested-name-specifier. */
24599 if (type != error_mark_node)
24600 check_unqualified_spec_or_inst (type, type_start_token->location);
24601 }
24602 if (nested_name_specifier)
24603 pushed_scope = push_scope (nested_name_specifier);
24604 }
24605 else if (nested_name_specifier)
24606 {
24607 tree class_type;
24608
24609 /* Given:
24610
24611 template <typename T> struct S { struct T };
24612 template <typename T> struct S<T>::T { };
24613
24614 we will get a TYPENAME_TYPE when processing the definition of
24615 `S::T'. We need to resolve it to the actual type before we
24616 try to define it. */
24617 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
24618 {
24619 class_type = resolve_typename_type (TREE_TYPE (type),
24620 /*only_current_p=*/false);
24621 if (TREE_CODE (class_type) != TYPENAME_TYPE)
24622 type = TYPE_NAME (class_type);
24623 else
24624 {
24625 cp_parser_error (parser, "could not resolve typename type");
24626 type = error_mark_node;
24627 }
24628 }
24629
24630 if (maybe_process_partial_specialization (TREE_TYPE (type))
24631 == error_mark_node)
24632 {
24633 type = NULL_TREE;
24634 goto done;
24635 }
24636
24637 class_type = current_class_type;
24638 /* Enter the scope indicated by the nested-name-specifier. */
24639 pushed_scope = push_scope (nested_name_specifier);
24640 /* Get the canonical version of this type. */
24641 type = TYPE_MAIN_DECL (TREE_TYPE (type));
24642 /* Call push_template_decl if it seems like we should be defining a
24643 template either from the template headers or the type we're
24644 defining, so that we diagnose both extra and missing headers. */
24645 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
24646 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
24647 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
24648 {
24649 type = push_template_decl (type);
24650 if (type == error_mark_node)
24651 {
24652 type = NULL_TREE;
24653 goto done;
24654 }
24655 }
24656
24657 type = TREE_TYPE (type);
24658 *nested_name_specifier_p = true;
24659 }
24660 else /* The name is not a nested name. */
24661 {
24662 /* If the class was unnamed, create a dummy name. */
24663 if (!id)
24664 id = make_anon_name ();
24665 tag_scope tag_scope = (parser->in_type_id_in_expr_p
24666 ? ts_within_enclosing_non_class
24667 : ts_current);
24668 type = xref_tag (class_key, id, tag_scope,
24669 parser->num_template_parameter_lists);
24670 }
24671
24672 /* Diagnose class/struct/union mismatches. */
24673 cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
24674 true, true);
24675
24676 /* Indicate whether this class was declared as a `class' or as a
24677 `struct'. */
24678 if (TREE_CODE (type) == RECORD_TYPE)
24679 CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
24680
24681 /* If this type was already complete, and we see another definition,
24682 that's an error. Likewise if the type is already being defined:
24683 this can happen, eg, when it's defined from within an expression
24684 (c++/84605). */
24685 if (type != error_mark_node
24686 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
24687 {
24688 error_at (type_start_token->location, "redefinition of %q#T",
24689 type);
24690 inform (location_of (type), "previous definition of %q#T",
24691 type);
24692 type = NULL_TREE;
24693 goto done;
24694 }
24695 else if (type == error_mark_node)
24696 type = NULL_TREE;
24697
24698 if (type)
24699 {
24700 /* Apply attributes now, before any use of the class as a template
24701 argument in its base list. */
24702 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
24703 fixup_attribute_variants (type);
24704 }
24705
24706 /* We will have entered the scope containing the class; the names of
24707 base classes should be looked up in that context. For example:
24708
24709 struct A { struct B {}; struct C; };
24710 struct A::C : B {};
24711
24712 is valid. */
24713
24714 /* Get the list of base-classes, if there is one. */
24715 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24716 {
24717 /* PR59482: enter the class scope so that base-specifiers are looked
24718 up correctly. */
24719 if (type)
24720 pushclass (type);
24721 bases = cp_parser_base_clause (parser);
24722 /* PR59482: get out of the previously pushed class scope so that the
24723 subsequent pops pop the right thing. */
24724 if (type)
24725 popclass ();
24726 }
24727 else
24728 bases = NULL_TREE;
24729
24730 /* If we're really defining a class, process the base classes.
24731 If they're invalid, fail. */
24732 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24733 xref_basetypes (type, bases);
24734
24735 done:
24736 /* Leave the scope given by the nested-name-specifier. We will
24737 enter the class scope itself while processing the members. */
24738 if (pushed_scope)
24739 pop_scope (pushed_scope);
24740
24741 if (invalid_explicit_specialization_p)
24742 {
24743 end_specialization ();
24744 --parser->num_template_parameter_lists;
24745 }
24746
24747 if (type)
24748 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
24749 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
24750 CLASSTYPE_FINAL (type) = 1;
24751 out:
24752 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24753 return type;
24754 }
24755
24756 /* Parse a class-key.
24757
24758 class-key:
24759 class
24760 struct
24761 union
24762
24763 Returns the kind of class-key specified, or none_type to indicate
24764 error. */
24765
24766 static enum tag_types
24767 cp_parser_class_key (cp_parser* parser)
24768 {
24769 cp_token *token;
24770 enum tag_types tag_type;
24771
24772 /* Look for the class-key. */
24773 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
24774 if (!token)
24775 return none_type;
24776
24777 /* Check to see if the TOKEN is a class-key. */
24778 tag_type = cp_parser_token_is_class_key (token);
24779 if (!tag_type)
24780 cp_parser_error (parser, "expected class-key");
24781 return tag_type;
24782 }
24783
24784 /* Parse a type-parameter-key.
24785
24786 type-parameter-key:
24787 class
24788 typename
24789 */
24790
24791 static void
24792 cp_parser_type_parameter_key (cp_parser* parser)
24793 {
24794 /* Look for the type-parameter-key. */
24795 enum tag_types tag_type = none_type;
24796 cp_token *token = cp_lexer_peek_token (parser->lexer);
24797 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
24798 {
24799 cp_lexer_consume_token (parser->lexer);
24800 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
24801 /* typename is not allowed in a template template parameter
24802 by the standard until C++17. */
24803 pedwarn (token->location, OPT_Wpedantic,
24804 "ISO C++ forbids typename key in template template parameter;"
24805 " use %<-std=c++17%> or %<-std=gnu++17%>");
24806 }
24807 else
24808 cp_parser_error (parser, "expected %<class%> or %<typename%>");
24809
24810 return;
24811 }
24812
24813 /* Parse an (optional) member-specification.
24814
24815 member-specification:
24816 member-declaration member-specification [opt]
24817 access-specifier : member-specification [opt] */
24818
24819 static void
24820 cp_parser_member_specification_opt (cp_parser* parser)
24821 {
24822 while (true)
24823 {
24824 cp_token *token;
24825 enum rid keyword;
24826
24827 /* Peek at the next token. */
24828 token = cp_lexer_peek_token (parser->lexer);
24829 /* If it's a `}', or EOF then we've seen all the members. */
24830 if (token->type == CPP_CLOSE_BRACE
24831 || token->type == CPP_EOF
24832 || token->type == CPP_PRAGMA_EOL)
24833 break;
24834
24835 /* See if this token is a keyword. */
24836 keyword = token->keyword;
24837 switch (keyword)
24838 {
24839 case RID_PUBLIC:
24840 case RID_PROTECTED:
24841 case RID_PRIVATE:
24842 /* Consume the access-specifier. */
24843 cp_lexer_consume_token (parser->lexer);
24844 /* Remember which access-specifier is active. */
24845 current_access_specifier = token->u.value;
24846 /* Look for the `:'. */
24847 cp_parser_require (parser, CPP_COLON, RT_COLON);
24848 break;
24849
24850 default:
24851 /* Accept #pragmas at class scope. */
24852 if (token->type == CPP_PRAGMA)
24853 {
24854 cp_parser_pragma (parser, pragma_member, NULL);
24855 break;
24856 }
24857
24858 /* Otherwise, the next construction must be a
24859 member-declaration. */
24860 cp_parser_member_declaration (parser);
24861 }
24862 }
24863 }
24864
24865 /* Parse a member-declaration.
24866
24867 member-declaration:
24868 decl-specifier-seq [opt] member-declarator-list [opt] ;
24869 function-definition ; [opt]
24870 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24871 using-declaration
24872 template-declaration
24873 alias-declaration
24874
24875 member-declarator-list:
24876 member-declarator
24877 member-declarator-list , member-declarator
24878
24879 member-declarator:
24880 declarator pure-specifier [opt]
24881 declarator constant-initializer [opt]
24882 identifier [opt] : constant-expression
24883
24884 GNU Extensions:
24885
24886 member-declaration:
24887 __extension__ member-declaration
24888
24889 member-declarator:
24890 declarator attributes [opt] pure-specifier [opt]
24891 declarator attributes [opt] constant-initializer [opt]
24892 identifier [opt] attributes [opt] : constant-expression
24893
24894 C++0x Extensions:
24895
24896 member-declaration:
24897 static_assert-declaration */
24898
24899 static void
24900 cp_parser_member_declaration (cp_parser* parser)
24901 {
24902 cp_decl_specifier_seq decl_specifiers;
24903 tree prefix_attributes;
24904 tree decl;
24905 int declares_class_or_enum;
24906 bool friend_p;
24907 cp_token *token = NULL;
24908 cp_token *decl_spec_token_start = NULL;
24909 cp_token *initializer_token_start = NULL;
24910 int saved_pedantic;
24911 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24912
24913 /* Check for the `__extension__' keyword. */
24914 if (cp_parser_extension_opt (parser, &saved_pedantic))
24915 {
24916 /* Recurse. */
24917 cp_parser_member_declaration (parser);
24918 /* Restore the old value of the PEDANTIC flag. */
24919 pedantic = saved_pedantic;
24920
24921 return;
24922 }
24923
24924 /* Check for a template-declaration. */
24925 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24926 {
24927 /* An explicit specialization here is an error condition, and we
24928 expect the specialization handler to detect and report this. */
24929 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
24930 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
24931 cp_parser_explicit_specialization (parser);
24932 else
24933 cp_parser_template_declaration (parser, /*member_p=*/true);
24934
24935 return;
24936 }
24937 /* Check for a template introduction. */
24938 else if (cp_parser_template_declaration_after_export (parser, true))
24939 return;
24940
24941 /* Check for a using-declaration. */
24942 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24943 {
24944 if (cxx_dialect < cxx11)
24945 {
24946 /* Parse the using-declaration. */
24947 cp_parser_using_declaration (parser,
24948 /*access_declaration_p=*/false);
24949 return;
24950 }
24951 else
24952 {
24953 tree decl;
24954 bool alias_decl_expected;
24955 cp_parser_parse_tentatively (parser);
24956 decl = cp_parser_alias_declaration (parser);
24957 /* Note that if we actually see the '=' token after the
24958 identifier, cp_parser_alias_declaration commits the
24959 tentative parse. In that case, we really expect an
24960 alias-declaration. Otherwise, we expect a using
24961 declaration. */
24962 alias_decl_expected =
24963 !cp_parser_uncommitted_to_tentative_parse_p (parser);
24964 cp_parser_parse_definitely (parser);
24965
24966 if (alias_decl_expected)
24967 finish_member_declaration (decl);
24968 else
24969 cp_parser_using_declaration (parser,
24970 /*access_declaration_p=*/false);
24971 return;
24972 }
24973 }
24974
24975 /* Check for @defs. */
24976 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
24977 {
24978 tree ivar, member;
24979 tree ivar_chains = cp_parser_objc_defs_expression (parser);
24980 ivar = ivar_chains;
24981 while (ivar)
24982 {
24983 member = ivar;
24984 ivar = TREE_CHAIN (member);
24985 TREE_CHAIN (member) = NULL_TREE;
24986 finish_member_declaration (member);
24987 }
24988 return;
24989 }
24990
24991 /* If the next token is `static_assert' we have a static assertion. */
24992 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
24993 {
24994 cp_parser_static_assert (parser, /*member_p=*/true);
24995 return;
24996 }
24997
24998 parser->colon_corrects_to_scope_p = false;
24999
25000 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
25001 goto out;
25002
25003 /* Parse the decl-specifier-seq. */
25004 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25005 cp_parser_decl_specifier_seq (parser,
25006 (CP_PARSER_FLAGS_OPTIONAL
25007 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
25008 &decl_specifiers,
25009 &declares_class_or_enum);
25010 /* Check for an invalid type-name. */
25011 if (!decl_specifiers.any_type_specifiers_p
25012 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25013 goto out;
25014 /* If there is no declarator, then the decl-specifier-seq should
25015 specify a type. */
25016 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25017 {
25018 /* If there was no decl-specifier-seq, and the next token is a
25019 `;', then we have something like:
25020
25021 struct S { ; };
25022
25023 [class.mem]
25024
25025 Each member-declaration shall declare at least one member
25026 name of the class. */
25027 if (!decl_specifiers.any_specifiers_p)
25028 {
25029 cp_token *token = cp_lexer_peek_token (parser->lexer);
25030 if (!in_system_header_at (token->location))
25031 {
25032 gcc_rich_location richloc (token->location);
25033 richloc.add_fixit_remove ();
25034 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
25035 }
25036 }
25037 else
25038 {
25039 tree type;
25040
25041 /* See if this declaration is a friend. */
25042 friend_p = cp_parser_friend_p (&decl_specifiers);
25043 /* If there were decl-specifiers, check to see if there was
25044 a class-declaration. */
25045 type = check_tag_decl (&decl_specifiers,
25046 /*explicit_type_instantiation_p=*/false);
25047 /* Nested classes have already been added to the class, but
25048 a `friend' needs to be explicitly registered. */
25049 if (friend_p)
25050 {
25051 /* If the `friend' keyword was present, the friend must
25052 be introduced with a class-key. */
25053 if (!declares_class_or_enum && cxx_dialect < cxx11)
25054 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
25055 "in C++03 a class-key must be used "
25056 "when declaring a friend");
25057 /* In this case:
25058
25059 template <typename T> struct A {
25060 friend struct A<T>::B;
25061 };
25062
25063 A<T>::B will be represented by a TYPENAME_TYPE, and
25064 therefore not recognized by check_tag_decl. */
25065 if (!type)
25066 {
25067 type = decl_specifiers.type;
25068 if (type && TREE_CODE (type) == TYPE_DECL)
25069 type = TREE_TYPE (type);
25070 }
25071 if (!type || !TYPE_P (type))
25072 error_at (decl_spec_token_start->location,
25073 "friend declaration does not name a class or "
25074 "function");
25075 else
25076 make_friend_class (current_class_type, type,
25077 /*complain=*/true);
25078 }
25079 /* If there is no TYPE, an error message will already have
25080 been issued. */
25081 else if (!type || type == error_mark_node)
25082 ;
25083 /* An anonymous aggregate has to be handled specially; such
25084 a declaration really declares a data member (with a
25085 particular type), as opposed to a nested class. */
25086 else if (ANON_AGGR_TYPE_P (type))
25087 {
25088 /* C++11 9.5/6. */
25089 if (decl_specifiers.storage_class != sc_none)
25090 error_at (decl_spec_token_start->location,
25091 "a storage class on an anonymous aggregate "
25092 "in class scope is not allowed");
25093
25094 /* Remove constructors and such from TYPE, now that we
25095 know it is an anonymous aggregate. */
25096 fixup_anonymous_aggr (type);
25097 /* And make the corresponding data member. */
25098 decl = build_decl (decl_spec_token_start->location,
25099 FIELD_DECL, NULL_TREE, type);
25100 /* Add it to the class. */
25101 finish_member_declaration (decl);
25102 }
25103 else
25104 cp_parser_check_access_in_redeclaration
25105 (TYPE_NAME (type),
25106 decl_spec_token_start->location);
25107 }
25108 }
25109 else
25110 {
25111 bool assume_semicolon = false;
25112
25113 /* Clear attributes from the decl_specifiers but keep them
25114 around as prefix attributes that apply them to the entity
25115 being declared. */
25116 prefix_attributes = decl_specifiers.attributes;
25117 decl_specifiers.attributes = NULL_TREE;
25118
25119 /* See if these declarations will be friends. */
25120 friend_p = cp_parser_friend_p (&decl_specifiers);
25121
25122 /* Keep going until we hit the `;' at the end of the
25123 declaration. */
25124 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25125 {
25126 tree attributes = NULL_TREE;
25127 tree first_attribute;
25128 tree initializer;
25129 bool named_bitfld = false;
25130
25131 /* Peek at the next token. */
25132 token = cp_lexer_peek_token (parser->lexer);
25133
25134 /* The following code wants to know early if it is a bit-field
25135 or some other declaration. Attributes can appear before
25136 the `:' token. Skip over them without consuming any tokens
25137 to peek if they are followed by `:'. */
25138 if (cp_next_tokens_can_be_attribute_p (parser)
25139 || (token->type == CPP_NAME
25140 && cp_nth_tokens_can_be_attribute_p (parser, 2)
25141 && (named_bitfld = true)))
25142 {
25143 size_t n
25144 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
25145 token = cp_lexer_peek_nth_token (parser->lexer, n);
25146 }
25147
25148 /* Check for a bitfield declaration. */
25149 if (token->type == CPP_COLON
25150 || (token->type == CPP_NAME
25151 && token == cp_lexer_peek_token (parser->lexer)
25152 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
25153 && (named_bitfld = true)))
25154 {
25155 tree identifier;
25156 tree width;
25157 tree late_attributes = NULL_TREE;
25158 location_t id_location
25159 = cp_lexer_peek_token (parser->lexer)->location;
25160
25161 if (named_bitfld)
25162 identifier = cp_parser_identifier (parser);
25163 else
25164 identifier = NULL_TREE;
25165
25166 /* Look for attributes that apply to the bitfield. */
25167 attributes = cp_parser_attributes_opt (parser);
25168
25169 /* Consume the `:' token. */
25170 cp_lexer_consume_token (parser->lexer);
25171
25172 /* Get the width of the bitfield. */
25173 width = cp_parser_constant_expression (parser, false, NULL,
25174 cxx_dialect >= cxx11);
25175
25176 /* In C++20 and as extension for C++11 and above we allow
25177 default member initializers for bit-fields. */
25178 initializer = NULL_TREE;
25179 if (cxx_dialect >= cxx11
25180 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
25181 || cp_lexer_next_token_is (parser->lexer,
25182 CPP_OPEN_BRACE)))
25183 {
25184 location_t loc
25185 = cp_lexer_peek_token (parser->lexer)->location;
25186 if (cxx_dialect < cxx20
25187 && identifier != NULL_TREE)
25188 pedwarn (loc, 0,
25189 "default member initializers for bit-fields "
25190 "only available with %<-std=c++20%> or "
25191 "%<-std=gnu++20%>");
25192
25193 initializer = cp_parser_save_nsdmi (parser);
25194 if (identifier == NULL_TREE)
25195 {
25196 error_at (loc, "default member initializer for "
25197 "unnamed bit-field");
25198 initializer = NULL_TREE;
25199 }
25200 }
25201 else
25202 {
25203 /* Look for attributes that apply to the bitfield after
25204 the `:' token and width. This is where GCC used to
25205 parse attributes in the past, pedwarn if there is
25206 a std attribute. */
25207 if (cp_next_tokens_can_be_std_attribute_p (parser))
25208 pedwarn (input_location, OPT_Wpedantic,
25209 "ISO C++ allows bit-field attributes only "
25210 "before the %<:%> token");
25211
25212 late_attributes = cp_parser_attributes_opt (parser);
25213 }
25214
25215 attributes = attr_chainon (attributes, late_attributes);
25216
25217 /* Remember which attributes are prefix attributes and
25218 which are not. */
25219 first_attribute = attributes;
25220 /* Combine the attributes. */
25221 attributes = attr_chainon (prefix_attributes, attributes);
25222
25223 /* Create the bitfield declaration. */
25224 decl = grokbitfield (identifier
25225 ? make_id_declarator (NULL_TREE,
25226 identifier,
25227 sfk_none,
25228 id_location)
25229 : NULL,
25230 &decl_specifiers,
25231 width, initializer,
25232 attributes);
25233 }
25234 else
25235 {
25236 cp_declarator *declarator;
25237 tree asm_specification;
25238 int ctor_dtor_or_conv_p;
25239 bool static_p = (decl_specifiers.storage_class == sc_static);
25240 cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
25241 if (!friend_p
25242 && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25243 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
25244
25245 /* Parse the declarator. */
25246 declarator
25247 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25248 flags,
25249 &ctor_dtor_or_conv_p,
25250 /*parenthesized_p=*/NULL,
25251 /*member_p=*/true,
25252 friend_p, static_p);
25253
25254 /* If something went wrong parsing the declarator, make sure
25255 that we at least consume some tokens. */
25256 if (declarator == cp_error_declarator)
25257 {
25258 /* Skip to the end of the statement. */
25259 cp_parser_skip_to_end_of_statement (parser);
25260 /* If the next token is not a semicolon, that is
25261 probably because we just skipped over the body of
25262 a function. So, we consume a semicolon if
25263 present, but do not issue an error message if it
25264 is not present. */
25265 if (cp_lexer_next_token_is (parser->lexer,
25266 CPP_SEMICOLON))
25267 cp_lexer_consume_token (parser->lexer);
25268 goto out;
25269 }
25270
25271 if (declares_class_or_enum & 2)
25272 cp_parser_check_for_definition_in_return_type
25273 (declarator, decl_specifiers.type,
25274 decl_specifiers.locations[ds_type_spec]);
25275
25276 /* Look for an asm-specification. */
25277 asm_specification = cp_parser_asm_specification_opt (parser);
25278 /* Look for attributes that apply to the declaration. */
25279 attributes = cp_parser_attributes_opt (parser);
25280 /* Remember which attributes are prefix attributes and
25281 which are not. */
25282 first_attribute = attributes;
25283 /* Combine the attributes. */
25284 attributes = attr_chainon (prefix_attributes, attributes);
25285
25286 /* If it's an `=', then we have a constant-initializer or a
25287 pure-specifier. It is not correct to parse the
25288 initializer before registering the member declaration
25289 since the member declaration should be in scope while
25290 its initializer is processed. However, the rest of the
25291 front end does not yet provide an interface that allows
25292 us to handle this correctly. */
25293 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25294 {
25295 /* In [class.mem]:
25296
25297 A pure-specifier shall be used only in the declaration of
25298 a virtual function.
25299
25300 A member-declarator can contain a constant-initializer
25301 only if it declares a static member of integral or
25302 enumeration type.
25303
25304 Therefore, if the DECLARATOR is for a function, we look
25305 for a pure-specifier; otherwise, we look for a
25306 constant-initializer. When we call `grokfield', it will
25307 perform more stringent semantics checks. */
25308 initializer_token_start = cp_lexer_peek_token (parser->lexer);
25309 if (function_declarator_p (declarator)
25310 || (decl_specifiers.type
25311 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
25312 && declarator->kind == cdk_id
25313 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
25314 == FUNCTION_TYPE)))
25315 initializer = cp_parser_pure_specifier (parser);
25316 else if (decl_specifiers.storage_class != sc_static)
25317 initializer = cp_parser_save_nsdmi (parser);
25318 else if (cxx_dialect >= cxx11)
25319 {
25320 bool nonconst;
25321 /* Don't require a constant rvalue in C++11, since we
25322 might want a reference constant. We'll enforce
25323 constancy later. */
25324 cp_lexer_consume_token (parser->lexer);
25325 /* Parse the initializer. */
25326 initializer = cp_parser_initializer_clause (parser,
25327 &nonconst);
25328 }
25329 else
25330 /* Parse the initializer. */
25331 initializer = cp_parser_constant_initializer (parser);
25332 }
25333 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
25334 && !function_declarator_p (declarator))
25335 {
25336 bool x;
25337 if (decl_specifiers.storage_class != sc_static)
25338 initializer = cp_parser_save_nsdmi (parser);
25339 else
25340 initializer = cp_parser_initializer (parser, &x, &x);
25341 }
25342 /* Detect invalid bit-field cases such as
25343
25344 int *p : 4;
25345 int &&r : 3;
25346
25347 and similar. */
25348 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
25349 /* If there were no type specifiers, it was a
25350 constructor. */
25351 && decl_specifiers.any_type_specifiers_p)
25352 {
25353 /* This is called for a decent diagnostic only. */
25354 tree d = grokdeclarator (declarator, &decl_specifiers,
25355 BITFIELD, /*initialized=*/false,
25356 &attributes);
25357 if (!error_operand_p (d))
25358 error_at (DECL_SOURCE_LOCATION (d),
25359 "bit-field %qD has non-integral type %qT",
25360 d, TREE_TYPE (d));
25361 cp_parser_skip_to_end_of_statement (parser);
25362 /* Avoid "extra ;" pedwarns. */
25363 if (cp_lexer_next_token_is (parser->lexer,
25364 CPP_SEMICOLON))
25365 cp_lexer_consume_token (parser->lexer);
25366 goto out;
25367 }
25368 /* Otherwise, there is no initializer. */
25369 else
25370 initializer = NULL_TREE;
25371
25372 /* See if we are probably looking at a function
25373 definition. We are certainly not looking at a
25374 member-declarator. Calling `grokfield' has
25375 side-effects, so we must not do it unless we are sure
25376 that we are looking at a member-declarator. */
25377 if (cp_parser_token_starts_function_definition_p
25378 (cp_lexer_peek_token (parser->lexer)))
25379 {
25380 /* The grammar does not allow a pure-specifier to be
25381 used when a member function is defined. (It is
25382 possible that this fact is an oversight in the
25383 standard, since a pure function may be defined
25384 outside of the class-specifier. */
25385 if (initializer && initializer_token_start)
25386 error_at (initializer_token_start->location,
25387 "pure-specifier on function-definition");
25388 decl = cp_parser_save_member_function_body (parser,
25389 &decl_specifiers,
25390 declarator,
25391 attributes);
25392 if (parser->fully_implicit_function_template_p)
25393 decl = finish_fully_implicit_template (parser, decl);
25394 /* If the member was not a friend, declare it here. */
25395 if (!friend_p)
25396 finish_member_declaration (decl);
25397 /* Peek at the next token. */
25398 token = cp_lexer_peek_token (parser->lexer);
25399 /* If the next token is a semicolon, consume it. */
25400 if (token->type == CPP_SEMICOLON)
25401 {
25402 location_t semicolon_loc
25403 = cp_lexer_consume_token (parser->lexer)->location;
25404 gcc_rich_location richloc (semicolon_loc);
25405 richloc.add_fixit_remove ();
25406 warning_at (&richloc, OPT_Wextra_semi,
25407 "extra %<;%> after in-class "
25408 "function definition");
25409 }
25410 goto out;
25411 }
25412 else
25413 if (declarator->kind == cdk_function)
25414 declarator->id_loc = token->location;
25415 /* Create the declaration. */
25416 decl = grokfield (declarator, &decl_specifiers,
25417 initializer, /*init_const_expr_p=*/true,
25418 asm_specification, attributes);
25419 if (parser->fully_implicit_function_template_p)
25420 {
25421 if (friend_p)
25422 finish_fully_implicit_template (parser, 0);
25423 else
25424 decl = finish_fully_implicit_template (parser, decl);
25425 }
25426 }
25427
25428 cp_finalize_omp_declare_simd (parser, decl);
25429 cp_finalize_oacc_routine (parser, decl, false);
25430
25431 /* Reset PREFIX_ATTRIBUTES. */
25432 if (attributes != error_mark_node)
25433 {
25434 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25435 attributes = TREE_CHAIN (attributes);
25436 if (attributes)
25437 TREE_CHAIN (attributes) = NULL_TREE;
25438 }
25439
25440 /* If there is any qualification still in effect, clear it
25441 now; we will be starting fresh with the next declarator. */
25442 parser->scope = NULL_TREE;
25443 parser->qualifying_scope = NULL_TREE;
25444 parser->object_scope = NULL_TREE;
25445 /* If it's a `,', then there are more declarators. */
25446 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25447 {
25448 cp_lexer_consume_token (parser->lexer);
25449 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25450 {
25451 cp_token *token = cp_lexer_previous_token (parser->lexer);
25452 gcc_rich_location richloc (token->location);
25453 richloc.add_fixit_remove ();
25454 error_at (&richloc, "stray %<,%> at end of "
25455 "member declaration");
25456 }
25457 }
25458 /* If the next token isn't a `;', then we have a parse error. */
25459 else if (cp_lexer_next_token_is_not (parser->lexer,
25460 CPP_SEMICOLON))
25461 {
25462 /* The next token might be a ways away from where the
25463 actual semicolon is missing. Find the previous token
25464 and use that for our error position. */
25465 cp_token *token = cp_lexer_previous_token (parser->lexer);
25466 gcc_rich_location richloc (token->location);
25467 richloc.add_fixit_insert_after (";");
25468 error_at (&richloc, "expected %<;%> at end of "
25469 "member declaration");
25470
25471 /* Assume that the user meant to provide a semicolon. If
25472 we were to cp_parser_skip_to_end_of_statement, we might
25473 skip to a semicolon inside a member function definition
25474 and issue nonsensical error messages. */
25475 assume_semicolon = true;
25476 }
25477
25478 if (decl)
25479 {
25480 /* Add DECL to the list of members. */
25481 if (!friend_p
25482 /* Explicitly include, eg, NSDMIs, for better error
25483 recovery (c++/58650). */
25484 || !DECL_DECLARES_FUNCTION_P (decl))
25485 finish_member_declaration (decl);
25486
25487 if (TREE_CODE (decl) == FUNCTION_DECL)
25488 cp_parser_save_default_args (parser, decl);
25489 else if (TREE_CODE (decl) == FIELD_DECL
25490 && DECL_INITIAL (decl))
25491 /* Add DECL to the queue of NSDMI to be parsed later. */
25492 vec_safe_push (unparsed_nsdmis, decl);
25493 }
25494
25495 if (assume_semicolon)
25496 goto out;
25497 }
25498 }
25499
25500 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25501 out:
25502 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
25503 }
25504
25505 /* Parse a pure-specifier.
25506
25507 pure-specifier:
25508 = 0
25509
25510 Returns INTEGER_ZERO_NODE if a pure specifier is found.
25511 Otherwise, ERROR_MARK_NODE is returned. */
25512
25513 static tree
25514 cp_parser_pure_specifier (cp_parser* parser)
25515 {
25516 cp_token *token;
25517
25518 /* Look for the `=' token. */
25519 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25520 return error_mark_node;
25521 /* Look for the `0' token. */
25522 token = cp_lexer_peek_token (parser->lexer);
25523
25524 if (token->type == CPP_EOF
25525 || token->type == CPP_PRAGMA_EOL)
25526 return error_mark_node;
25527
25528 cp_lexer_consume_token (parser->lexer);
25529
25530 /* Accept = default or = delete in c++0x mode. */
25531 if (token->keyword == RID_DEFAULT
25532 || token->keyword == RID_DELETE)
25533 {
25534 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
25535 return token->u.value;
25536 }
25537
25538 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
25539 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
25540 {
25541 cp_parser_error (parser,
25542 "invalid pure specifier (only %<= 0%> is allowed)");
25543 cp_parser_skip_to_end_of_statement (parser);
25544 return error_mark_node;
25545 }
25546 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
25547 {
25548 error_at (token->location, "templates may not be %<virtual%>");
25549 return error_mark_node;
25550 }
25551
25552 return integer_zero_node;
25553 }
25554
25555 /* Parse a constant-initializer.
25556
25557 constant-initializer:
25558 = constant-expression
25559
25560 Returns a representation of the constant-expression. */
25561
25562 static tree
25563 cp_parser_constant_initializer (cp_parser* parser)
25564 {
25565 /* Look for the `=' token. */
25566 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25567 return error_mark_node;
25568
25569 /* It is invalid to write:
25570
25571 struct S { static const int i = { 7 }; };
25572
25573 */
25574 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25575 {
25576 cp_parser_error (parser,
25577 "a brace-enclosed initializer is not allowed here");
25578 /* Consume the opening brace. */
25579 matching_braces braces;
25580 braces.consume_open (parser);
25581 /* Skip the initializer. */
25582 cp_parser_skip_to_closing_brace (parser);
25583 /* Look for the trailing `}'. */
25584 braces.require_close (parser);
25585
25586 return error_mark_node;
25587 }
25588
25589 return cp_parser_constant_expression (parser);
25590 }
25591
25592 /* Derived classes [gram.class.derived] */
25593
25594 /* Parse a base-clause.
25595
25596 base-clause:
25597 : base-specifier-list
25598
25599 base-specifier-list:
25600 base-specifier ... [opt]
25601 base-specifier-list , base-specifier ... [opt]
25602
25603 Returns a TREE_LIST representing the base-classes, in the order in
25604 which they were declared. The representation of each node is as
25605 described by cp_parser_base_specifier.
25606
25607 In the case that no bases are specified, this function will return
25608 NULL_TREE, not ERROR_MARK_NODE. */
25609
25610 static tree
25611 cp_parser_base_clause (cp_parser* parser)
25612 {
25613 tree bases = NULL_TREE;
25614
25615 /* Look for the `:' that begins the list. */
25616 cp_parser_require (parser, CPP_COLON, RT_COLON);
25617
25618 /* Scan the base-specifier-list. */
25619 while (true)
25620 {
25621 cp_token *token;
25622 tree base;
25623 bool pack_expansion_p = false;
25624
25625 /* Look for the base-specifier. */
25626 base = cp_parser_base_specifier (parser);
25627 /* Look for the (optional) ellipsis. */
25628 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25629 {
25630 /* Consume the `...'. */
25631 cp_lexer_consume_token (parser->lexer);
25632
25633 pack_expansion_p = true;
25634 }
25635
25636 /* Add BASE to the front of the list. */
25637 if (base && base != error_mark_node)
25638 {
25639 if (pack_expansion_p)
25640 /* Make this a pack expansion type. */
25641 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
25642
25643 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
25644 {
25645 TREE_CHAIN (base) = bases;
25646 bases = base;
25647 }
25648 }
25649 /* Peek at the next token. */
25650 token = cp_lexer_peek_token (parser->lexer);
25651 /* If it's not a comma, then the list is complete. */
25652 if (token->type != CPP_COMMA)
25653 break;
25654 /* Consume the `,'. */
25655 cp_lexer_consume_token (parser->lexer);
25656 }
25657
25658 /* PARSER->SCOPE may still be non-NULL at this point, if the last
25659 base class had a qualified name. However, the next name that
25660 appears is certainly not qualified. */
25661 parser->scope = NULL_TREE;
25662 parser->qualifying_scope = NULL_TREE;
25663 parser->object_scope = NULL_TREE;
25664
25665 return nreverse (bases);
25666 }
25667
25668 /* Parse a base-specifier.
25669
25670 base-specifier:
25671 :: [opt] nested-name-specifier [opt] class-name
25672 virtual access-specifier [opt] :: [opt] nested-name-specifier
25673 [opt] class-name
25674 access-specifier virtual [opt] :: [opt] nested-name-specifier
25675 [opt] class-name
25676
25677 Returns a TREE_LIST. The TREE_PURPOSE will be one of
25678 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
25679 indicate the specifiers provided. The TREE_VALUE will be a TYPE
25680 (or the ERROR_MARK_NODE) indicating the type that was specified. */
25681
25682 static tree
25683 cp_parser_base_specifier (cp_parser* parser)
25684 {
25685 cp_token *token;
25686 bool done = false;
25687 bool virtual_p = false;
25688 bool duplicate_virtual_error_issued_p = false;
25689 bool duplicate_access_error_issued_p = false;
25690 bool class_scope_p, template_p;
25691 tree access = access_default_node;
25692 tree type;
25693
25694 /* Process the optional `virtual' and `access-specifier'. */
25695 while (!done)
25696 {
25697 /* Peek at the next token. */
25698 token = cp_lexer_peek_token (parser->lexer);
25699 /* Process `virtual'. */
25700 switch (token->keyword)
25701 {
25702 case RID_VIRTUAL:
25703 /* If `virtual' appears more than once, issue an error. */
25704 if (virtual_p && !duplicate_virtual_error_issued_p)
25705 {
25706 cp_parser_error (parser,
25707 "%<virtual%> specified more than once in base-specifier");
25708 duplicate_virtual_error_issued_p = true;
25709 }
25710
25711 virtual_p = true;
25712
25713 /* Consume the `virtual' token. */
25714 cp_lexer_consume_token (parser->lexer);
25715
25716 break;
25717
25718 case RID_PUBLIC:
25719 case RID_PROTECTED:
25720 case RID_PRIVATE:
25721 /* If more than one access specifier appears, issue an
25722 error. */
25723 if (access != access_default_node
25724 && !duplicate_access_error_issued_p)
25725 {
25726 cp_parser_error (parser,
25727 "more than one access specifier in base-specifier");
25728 duplicate_access_error_issued_p = true;
25729 }
25730
25731 access = ridpointers[(int) token->keyword];
25732
25733 /* Consume the access-specifier. */
25734 cp_lexer_consume_token (parser->lexer);
25735
25736 break;
25737
25738 default:
25739 done = true;
25740 break;
25741 }
25742 }
25743 /* It is not uncommon to see programs mechanically, erroneously, use
25744 the 'typename' keyword to denote (dependent) qualified types
25745 as base classes. */
25746 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25747 {
25748 token = cp_lexer_peek_token (parser->lexer);
25749 if (!processing_template_decl)
25750 error_at (token->location,
25751 "keyword %<typename%> not allowed outside of templates");
25752 else
25753 error_at (token->location,
25754 "keyword %<typename%> not allowed in this context "
25755 "(the base class is implicitly a type)");
25756 cp_lexer_consume_token (parser->lexer);
25757 }
25758
25759 /* Look for the optional `::' operator. */
25760 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
25761 /* Look for the nested-name-specifier. The simplest way to
25762 implement:
25763
25764 [temp.res]
25765
25766 The keyword `typename' is not permitted in a base-specifier or
25767 mem-initializer; in these contexts a qualified name that
25768 depends on a template-parameter is implicitly assumed to be a
25769 type name.
25770
25771 is to pretend that we have seen the `typename' keyword at this
25772 point. */
25773 cp_parser_nested_name_specifier_opt (parser,
25774 /*typename_keyword_p=*/true,
25775 /*check_dependency_p=*/true,
25776 /*type_p=*/true,
25777 /*is_declaration=*/true);
25778 /* If the base class is given by a qualified name, assume that names
25779 we see are type names or templates, as appropriate. */
25780 class_scope_p = (parser->scope && TYPE_P (parser->scope));
25781 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
25782
25783 if (!parser->scope
25784 && cp_lexer_next_token_is_decltype (parser->lexer))
25785 /* DR 950 allows decltype as a base-specifier. */
25786 type = cp_parser_decltype (parser);
25787 else
25788 {
25789 /* Otherwise, look for the class-name. */
25790 type = cp_parser_class_name (parser,
25791 class_scope_p,
25792 template_p,
25793 typename_type,
25794 /*check_dependency_p=*/true,
25795 /*class_head_p=*/false,
25796 /*is_declaration=*/true);
25797 type = TREE_TYPE (type);
25798 }
25799
25800 if (type == error_mark_node)
25801 return error_mark_node;
25802
25803 return finish_base_specifier (type, access, virtual_p);
25804 }
25805
25806 /* Exception handling [gram.exception] */
25807
25808 /* Save the tokens that make up the noexcept-specifier for a member-function.
25809 Returns a DEFERRED_PARSE. */
25810
25811 static tree
25812 cp_parser_save_noexcept (cp_parser *parser)
25813 {
25814 cp_token *first = parser->lexer->next_token;
25815 /* We want everything up to, including, the final ')'. */
25816 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
25817 cp_token *last = parser->lexer->next_token;
25818
25819 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
25820 to carry the information we will need. */
25821 tree expr = make_node (DEFERRED_PARSE);
25822 /* Save away the noexcept-specifier; we will process it when the
25823 class is complete. */
25824 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
25825 expr = build_tree_list (expr, NULL_TREE);
25826 return expr;
25827 }
25828
25829 /* Used for late processing of noexcept-specifiers of member-functions.
25830 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
25831 we saved for later; parse it now. DECL is the declaration of the
25832 member function. */
25833
25834 static tree
25835 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
25836 {
25837 /* Make sure we've gotten something that hasn't been parsed yet. */
25838 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
25839
25840 push_unparsed_function_queues (parser);
25841
25842 /* Push the saved tokens for the noexcept-specifier onto the parser's
25843 lexer stack. */
25844 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
25845 cp_parser_push_lexer_for_tokens (parser, tokens);
25846
25847 /* Parse the cached noexcept-specifier. */
25848 tree parsed_arg
25849 = cp_parser_noexcept_specification_opt (parser,
25850 CP_PARSER_FLAGS_NONE,
25851 /*require_constexpr=*/true,
25852 /*consumed_expr=*/NULL,
25853 /*return_cond=*/false);
25854
25855 /* Revert to the main lexer. */
25856 cp_parser_pop_lexer (parser);
25857
25858 /* Restore the queue. */
25859 pop_unparsed_function_queues (parser);
25860
25861 /* And we're done. */
25862 return parsed_arg;
25863 }
25864
25865 /* Perform late checking of overriding function with respect to their
25866 noexcept-specifiers. TYPE is the class and FNDECL is the function
25867 that potentially overrides some virtual function with the same
25868 signature. */
25869
25870 static void
25871 noexcept_override_late_checks (tree type, tree fndecl)
25872 {
25873 tree binfo = TYPE_BINFO (type);
25874 tree base_binfo;
25875
25876 if (DECL_STATIC_FUNCTION_P (fndecl))
25877 return;
25878
25879 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
25880 {
25881 tree basetype = BINFO_TYPE (base_binfo);
25882
25883 if (!TYPE_POLYMORPHIC_P (basetype))
25884 continue;
25885
25886 tree fn = look_for_overrides_here (basetype, fndecl);
25887 if (fn)
25888 maybe_check_overriding_exception_spec (fndecl, fn);
25889 }
25890 }
25891
25892 /* Parse an (optional) noexcept-specification.
25893
25894 noexcept-specification:
25895 noexcept ( constant-expression ) [opt]
25896
25897 If no noexcept-specification is present, returns NULL_TREE.
25898 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25899 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25900 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25901 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25902 in which case a boolean condition is returned instead. The parser flags
25903 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
25904 the (member) function is `const'. */
25905
25906 static tree
25907 cp_parser_noexcept_specification_opt (cp_parser* parser,
25908 cp_parser_flags flags,
25909 bool require_constexpr,
25910 bool* consumed_expr,
25911 bool return_cond)
25912 {
25913 cp_token *token;
25914 const char *saved_message;
25915
25916 /* Peek at the next token. */
25917 token = cp_lexer_peek_token (parser->lexer);
25918
25919 /* Is it a noexcept-specification? */
25920 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
25921 {
25922 tree expr;
25923
25924 /* [class.mem]/6 says that a noexcept-specifer (within the
25925 member-specification of the class) is a complete-class context of
25926 a class. So, if the noexcept-specifier has the optional expression,
25927 just save the tokens, and reparse this after we're done with the
25928 class. */
25929 const bool literal_p
25930 = ((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
25931 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
25932 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN));
25933
25934 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
25935 /* No need to delay parsing for a number literal or true/false. */
25936 && !literal_p
25937 && at_class_scope_p ()
25938 /* We don't delay parsing for friend member functions,
25939 alias-declarations, and typedefs, even though the standard seems
25940 to require it. */
25941 && (flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
25942 && TYPE_BEING_DEFINED (current_class_type)
25943 && !LAMBDA_TYPE_P (current_class_type))
25944 return cp_parser_save_noexcept (parser);
25945
25946 cp_lexer_consume_token (parser->lexer);
25947
25948 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
25949 {
25950 matching_parens parens;
25951 parens.consume_open (parser);
25952
25953 if (require_constexpr)
25954 {
25955 /* Types may not be defined in an exception-specification. */
25956 saved_message = parser->type_definition_forbidden_message;
25957 parser->type_definition_forbidden_message
25958 = G_("types may not be defined in an exception-specification");
25959
25960 bool non_constant_p;
25961 expr
25962 = cp_parser_constant_expression (parser,
25963 /*allow_non_constant=*/true,
25964 &non_constant_p);
25965 if (non_constant_p
25966 && !require_potential_rvalue_constant_expression (expr))
25967 {
25968 expr = NULL_TREE;
25969 return_cond = true;
25970 }
25971
25972 /* Restore the saved message. */
25973 parser->type_definition_forbidden_message = saved_message;
25974 }
25975 else
25976 {
25977 expr = cp_parser_expression (parser);
25978 *consumed_expr = true;
25979 }
25980
25981 parens.require_close (parser);
25982 }
25983 else
25984 {
25985 expr = boolean_true_node;
25986 if (!require_constexpr)
25987 *consumed_expr = false;
25988 }
25989
25990 /* We cannot build a noexcept-spec right away because this will check
25991 that expr is a constexpr. */
25992 if (!return_cond)
25993 return build_noexcept_spec (expr, tf_warning_or_error);
25994 else
25995 return expr;
25996 }
25997 else
25998 return NULL_TREE;
25999 }
26000
26001 /* Parse an (optional) exception-specification.
26002
26003 exception-specification:
26004 throw ( type-id-list [opt] )
26005
26006 Returns a TREE_LIST representing the exception-specification. The
26007 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
26008 control parsing. QUALS are qualifiers indicating whether the (member)
26009 function is `const'. */
26010
26011 static tree
26012 cp_parser_exception_specification_opt (cp_parser* parser,
26013 cp_parser_flags flags)
26014 {
26015 cp_token *token;
26016 tree type_id_list;
26017 const char *saved_message;
26018
26019 /* Peek at the next token. */
26020 token = cp_lexer_peek_token (parser->lexer);
26021
26022 /* Is it a noexcept-specification? */
26023 type_id_list
26024 = cp_parser_noexcept_specification_opt (parser, flags,
26025 /*require_constexpr=*/true,
26026 /*consumed_expr=*/NULL,
26027 /*return_cond=*/false);
26028 if (type_id_list != NULL_TREE)
26029 return type_id_list;
26030
26031 /* If it's not `throw', then there's no exception-specification. */
26032 if (!cp_parser_is_keyword (token, RID_THROW))
26033 return NULL_TREE;
26034
26035 location_t loc = token->location;
26036
26037 /* Consume the `throw'. */
26038 cp_lexer_consume_token (parser->lexer);
26039
26040 /* Look for the `('. */
26041 matching_parens parens;
26042 parens.require_open (parser);
26043
26044 /* Peek at the next token. */
26045 token = cp_lexer_peek_token (parser->lexer);
26046 /* If it's not a `)', then there is a type-id-list. */
26047 if (token->type != CPP_CLOSE_PAREN)
26048 {
26049 /* Types may not be defined in an exception-specification. */
26050 saved_message = parser->type_definition_forbidden_message;
26051 parser->type_definition_forbidden_message
26052 = G_("types may not be defined in an exception-specification");
26053 /* Parse the type-id-list. */
26054 type_id_list = cp_parser_type_id_list (parser);
26055 /* Restore the saved message. */
26056 parser->type_definition_forbidden_message = saved_message;
26057
26058 if (cxx_dialect >= cxx17)
26059 {
26060 error_at (loc, "ISO C++17 does not allow dynamic exception "
26061 "specifications");
26062 type_id_list = NULL_TREE;
26063 }
26064 else if (cxx_dialect >= cxx11)
26065 warning_at (loc, OPT_Wdeprecated,
26066 "dynamic exception specifications are deprecated in "
26067 "C++11");
26068 }
26069 /* In C++17, throw() is equivalent to noexcept (true). throw()
26070 is deprecated in C++11 and above as well, but is still widely used,
26071 so don't warn about it yet. */
26072 else if (cxx_dialect >= cxx17)
26073 type_id_list = noexcept_true_spec;
26074 else
26075 type_id_list = empty_except_spec;
26076
26077 /* Look for the `)'. */
26078 parens.require_close (parser);
26079
26080 return type_id_list;
26081 }
26082
26083 /* Parse an (optional) type-id-list.
26084
26085 type-id-list:
26086 type-id ... [opt]
26087 type-id-list , type-id ... [opt]
26088
26089 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
26090 in the order that the types were presented. */
26091
26092 static tree
26093 cp_parser_type_id_list (cp_parser* parser)
26094 {
26095 tree types = NULL_TREE;
26096
26097 while (true)
26098 {
26099 cp_token *token;
26100 tree type;
26101
26102 token = cp_lexer_peek_token (parser->lexer);
26103
26104 /* Get the next type-id. */
26105 type = cp_parser_type_id (parser);
26106 /* Check for invalid 'auto'. */
26107 if (flag_concepts && type_uses_auto (type))
26108 {
26109 error_at (token->location,
26110 "invalid use of %<auto%> in exception-specification");
26111 type = error_mark_node;
26112 }
26113 /* Parse the optional ellipsis. */
26114 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26115 {
26116 /* Consume the `...'. */
26117 cp_lexer_consume_token (parser->lexer);
26118
26119 /* Turn the type into a pack expansion expression. */
26120 type = make_pack_expansion (type);
26121 }
26122 /* Add it to the list. */
26123 types = add_exception_specifier (types, type, /*complain=*/1);
26124 /* Peek at the next token. */
26125 token = cp_lexer_peek_token (parser->lexer);
26126 /* If it is not a `,', we are done. */
26127 if (token->type != CPP_COMMA)
26128 break;
26129 /* Consume the `,'. */
26130 cp_lexer_consume_token (parser->lexer);
26131 }
26132
26133 return nreverse (types);
26134 }
26135
26136 /* Parse a try-block.
26137
26138 try-block:
26139 try compound-statement handler-seq */
26140
26141 static tree
26142 cp_parser_try_block (cp_parser* parser)
26143 {
26144 tree try_block;
26145
26146 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
26147 if (parser->in_function_body
26148 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
26149 && cxx_dialect < cxx20)
26150 pedwarn (input_location, 0,
26151 "%<try%> in %<constexpr%> function only "
26152 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26153
26154 try_block = begin_try_block ();
26155 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
26156 finish_try_block (try_block);
26157 cp_parser_handler_seq (parser);
26158 finish_handler_sequence (try_block);
26159
26160 return try_block;
26161 }
26162
26163 /* Parse a function-try-block.
26164
26165 function-try-block:
26166 try ctor-initializer [opt] function-body handler-seq */
26167
26168 static void
26169 cp_parser_function_try_block (cp_parser* parser)
26170 {
26171 tree compound_stmt;
26172 tree try_block;
26173
26174 /* Look for the `try' keyword. */
26175 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
26176 return;
26177 /* Let the rest of the front end know where we are. */
26178 try_block = begin_function_try_block (&compound_stmt);
26179 /* Parse the function-body. */
26180 cp_parser_ctor_initializer_opt_and_function_body
26181 (parser, /*in_function_try_block=*/true);
26182 /* We're done with the `try' part. */
26183 finish_function_try_block (try_block);
26184 /* Parse the handlers. */
26185 cp_parser_handler_seq (parser);
26186 /* We're done with the handlers. */
26187 finish_function_handler_sequence (try_block, compound_stmt);
26188 }
26189
26190 /* Parse a handler-seq.
26191
26192 handler-seq:
26193 handler handler-seq [opt] */
26194
26195 static void
26196 cp_parser_handler_seq (cp_parser* parser)
26197 {
26198 while (true)
26199 {
26200 cp_token *token;
26201
26202 /* Parse the handler. */
26203 cp_parser_handler (parser);
26204 /* Peek at the next token. */
26205 token = cp_lexer_peek_token (parser->lexer);
26206 /* If it's not `catch' then there are no more handlers. */
26207 if (!cp_parser_is_keyword (token, RID_CATCH))
26208 break;
26209 }
26210 }
26211
26212 /* Parse a handler.
26213
26214 handler:
26215 catch ( exception-declaration ) compound-statement */
26216
26217 static void
26218 cp_parser_handler (cp_parser* parser)
26219 {
26220 tree handler;
26221 tree declaration;
26222
26223 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
26224 handler = begin_handler ();
26225 matching_parens parens;
26226 parens.require_open (parser);
26227 declaration = cp_parser_exception_declaration (parser);
26228 finish_handler_parms (declaration, handler);
26229 parens.require_close (parser);
26230 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
26231 finish_handler (handler);
26232 }
26233
26234 /* Parse an exception-declaration.
26235
26236 exception-declaration:
26237 type-specifier-seq declarator
26238 type-specifier-seq abstract-declarator
26239 type-specifier-seq
26240 ...
26241
26242 Returns a VAR_DECL for the declaration, or NULL_TREE if the
26243 ellipsis variant is used. */
26244
26245 static tree
26246 cp_parser_exception_declaration (cp_parser* parser)
26247 {
26248 cp_decl_specifier_seq type_specifiers;
26249 cp_declarator *declarator;
26250 const char *saved_message;
26251
26252 /* If it's an ellipsis, it's easy to handle. */
26253 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26254 {
26255 /* Consume the `...' token. */
26256 cp_lexer_consume_token (parser->lexer);
26257 return NULL_TREE;
26258 }
26259
26260 /* Types may not be defined in exception-declarations. */
26261 saved_message = parser->type_definition_forbidden_message;
26262 parser->type_definition_forbidden_message
26263 = G_("types may not be defined in exception-declarations");
26264
26265 /* Parse the type-specifier-seq. */
26266 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
26267 /*is_declaration=*/true,
26268 /*is_trailing_return=*/false,
26269 &type_specifiers);
26270 /* If it's a `)', then there is no declarator. */
26271 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26272 declarator = NULL;
26273 else
26274 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
26275 CP_PARSER_FLAGS_NONE,
26276 /*ctor_dtor_or_conv_p=*/NULL,
26277 /*parenthesized_p=*/NULL,
26278 /*member_p=*/false,
26279 /*friend_p=*/false,
26280 /*static_p=*/false);
26281
26282 /* Restore the saved message. */
26283 parser->type_definition_forbidden_message = saved_message;
26284
26285 if (!type_specifiers.any_specifiers_p)
26286 return error_mark_node;
26287
26288 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
26289 }
26290
26291 /* Parse a throw-expression.
26292
26293 throw-expression:
26294 throw assignment-expression [opt]
26295
26296 Returns a THROW_EXPR representing the throw-expression. */
26297
26298 static tree
26299 cp_parser_throw_expression (cp_parser* parser)
26300 {
26301 tree expression;
26302 cp_token* token;
26303 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
26304
26305 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
26306 token = cp_lexer_peek_token (parser->lexer);
26307 /* Figure out whether or not there is an assignment-expression
26308 following the "throw" keyword. */
26309 if (token->type == CPP_COMMA
26310 || token->type == CPP_SEMICOLON
26311 || token->type == CPP_CLOSE_PAREN
26312 || token->type == CPP_CLOSE_SQUARE
26313 || token->type == CPP_CLOSE_BRACE
26314 || token->type == CPP_COLON)
26315 expression = NULL_TREE;
26316 else
26317 expression = cp_parser_assignment_expression (parser);
26318
26319 /* Construct a location e.g.:
26320 throw x
26321 ^~~~~~~
26322 with caret == start at the start of the "throw" token, and
26323 the end at the end of the final token we consumed. */
26324 location_t combined_loc = make_location (start_loc, start_loc,
26325 parser->lexer);
26326 expression = build_throw (combined_loc, expression);
26327
26328 return expression;
26329 }
26330
26331 /* Parse a yield-expression.
26332
26333 yield-expression:
26334 co_yield assignment-expression
26335 co_yield braced-init-list
26336
26337 Returns a CO_YIELD_EXPR representing the yield-expression. */
26338
26339 static tree
26340 cp_parser_yield_expression (cp_parser* parser)
26341 {
26342 tree expr;
26343
26344 cp_token *token = cp_lexer_peek_token (parser->lexer);
26345 location_t kw_loc = token->location; /* Save for later. */
26346
26347 cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
26348
26349 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26350 {
26351 bool expr_non_constant_p;
26352 cp_lexer_set_source_position (parser->lexer);
26353 /* ??? : probably a moot point? */
26354 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26355 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
26356 }
26357 else
26358 expr = cp_parser_assignment_expression (parser);
26359
26360 if (expr == error_mark_node)
26361 return expr;
26362
26363 return finish_co_yield_expr (kw_loc, expr);
26364 }
26365
26366 /* GNU Extensions */
26367
26368 /* Parse an (optional) asm-specification.
26369
26370 asm-specification:
26371 asm ( string-literal )
26372
26373 If the asm-specification is present, returns a STRING_CST
26374 corresponding to the string-literal. Otherwise, returns
26375 NULL_TREE. */
26376
26377 static tree
26378 cp_parser_asm_specification_opt (cp_parser* parser)
26379 {
26380 cp_token *token;
26381 tree asm_specification;
26382
26383 /* Peek at the next token. */
26384 token = cp_lexer_peek_token (parser->lexer);
26385 /* If the next token isn't the `asm' keyword, then there's no
26386 asm-specification. */
26387 if (!cp_parser_is_keyword (token, RID_ASM))
26388 return NULL_TREE;
26389
26390 /* Consume the `asm' token. */
26391 cp_lexer_consume_token (parser->lexer);
26392 /* Look for the `('. */
26393 matching_parens parens;
26394 parens.require_open (parser);
26395
26396 /* Look for the string-literal. */
26397 asm_specification = cp_parser_string_literal (parser, false, false);
26398
26399 /* Look for the `)'. */
26400 parens.require_close (parser);
26401
26402 return asm_specification;
26403 }
26404
26405 /* Parse an asm-operand-list.
26406
26407 asm-operand-list:
26408 asm-operand
26409 asm-operand-list , asm-operand
26410
26411 asm-operand:
26412 string-literal ( expression )
26413 [ string-literal ] string-literal ( expression )
26414
26415 Returns a TREE_LIST representing the operands. The TREE_VALUE of
26416 each node is the expression. The TREE_PURPOSE is itself a
26417 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
26418 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
26419 is a STRING_CST for the string literal before the parenthesis. Returns
26420 ERROR_MARK_NODE if any of the operands are invalid. */
26421
26422 static tree
26423 cp_parser_asm_operand_list (cp_parser* parser)
26424 {
26425 tree asm_operands = NULL_TREE;
26426 bool invalid_operands = false;
26427
26428 while (true)
26429 {
26430 tree string_literal;
26431 tree expression;
26432 tree name;
26433
26434 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
26435 {
26436 /* Consume the `[' token. */
26437 cp_lexer_consume_token (parser->lexer);
26438 /* Read the operand name. */
26439 name = cp_parser_identifier (parser);
26440 if (name != error_mark_node)
26441 name = build_string (IDENTIFIER_LENGTH (name),
26442 IDENTIFIER_POINTER (name));
26443 /* Look for the closing `]'. */
26444 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26445 }
26446 else
26447 name = NULL_TREE;
26448 /* Look for the string-literal. */
26449 string_literal = cp_parser_string_literal (parser, false, false);
26450
26451 /* Look for the `('. */
26452 matching_parens parens;
26453 parens.require_open (parser);
26454 /* Parse the expression. */
26455 expression = cp_parser_expression (parser);
26456 /* Look for the `)'. */
26457 parens.require_close (parser);
26458
26459 if (name == error_mark_node
26460 || string_literal == error_mark_node
26461 || expression == error_mark_node)
26462 invalid_operands = true;
26463
26464 /* Add this operand to the list. */
26465 asm_operands = tree_cons (build_tree_list (name, string_literal),
26466 expression,
26467 asm_operands);
26468 /* If the next token is not a `,', there are no more
26469 operands. */
26470 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26471 break;
26472 /* Consume the `,'. */
26473 cp_lexer_consume_token (parser->lexer);
26474 }
26475
26476 return invalid_operands ? error_mark_node : nreverse (asm_operands);
26477 }
26478
26479 /* Parse an asm-clobber-list.
26480
26481 asm-clobber-list:
26482 string-literal
26483 asm-clobber-list , string-literal
26484
26485 Returns a TREE_LIST, indicating the clobbers in the order that they
26486 appeared. The TREE_VALUE of each node is a STRING_CST. */
26487
26488 static tree
26489 cp_parser_asm_clobber_list (cp_parser* parser)
26490 {
26491 tree clobbers = NULL_TREE;
26492
26493 while (true)
26494 {
26495 tree string_literal;
26496
26497 /* Look for the string literal. */
26498 string_literal = cp_parser_string_literal (parser, false, false);
26499 /* Add it to the list. */
26500 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
26501 /* If the next token is not a `,', then the list is
26502 complete. */
26503 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26504 break;
26505 /* Consume the `,' token. */
26506 cp_lexer_consume_token (parser->lexer);
26507 }
26508
26509 return clobbers;
26510 }
26511
26512 /* Parse an asm-label-list.
26513
26514 asm-label-list:
26515 identifier
26516 asm-label-list , identifier
26517
26518 Returns a TREE_LIST, indicating the labels in the order that they
26519 appeared. The TREE_VALUE of each node is a label. */
26520
26521 static tree
26522 cp_parser_asm_label_list (cp_parser* parser)
26523 {
26524 tree labels = NULL_TREE;
26525
26526 while (true)
26527 {
26528 tree identifier, label, name;
26529
26530 /* Look for the identifier. */
26531 identifier = cp_parser_identifier (parser);
26532 if (!error_operand_p (identifier))
26533 {
26534 label = lookup_label (identifier);
26535 if (TREE_CODE (label) == LABEL_DECL)
26536 {
26537 TREE_USED (label) = 1;
26538 check_goto (label);
26539 name = build_string (IDENTIFIER_LENGTH (identifier),
26540 IDENTIFIER_POINTER (identifier));
26541 labels = tree_cons (name, label, labels);
26542 }
26543 }
26544 /* If the next token is not a `,', then the list is
26545 complete. */
26546 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26547 break;
26548 /* Consume the `,' token. */
26549 cp_lexer_consume_token (parser->lexer);
26550 }
26551
26552 return nreverse (labels);
26553 }
26554
26555 /* Return TRUE iff the next tokens in the stream are possibly the
26556 beginning of a GNU extension attribute. */
26557
26558 static bool
26559 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
26560 {
26561 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
26562 }
26563
26564 /* Return TRUE iff the next tokens in the stream are possibly the
26565 beginning of a standard C++-11 attribute specifier. */
26566
26567 static bool
26568 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
26569 {
26570 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
26571 }
26572
26573 /* Return TRUE iff the next Nth tokens in the stream are possibly the
26574 beginning of a standard C++-11 attribute specifier. */
26575
26576 static bool
26577 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
26578 {
26579 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
26580
26581 return (cxx_dialect >= cxx11
26582 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
26583 || (token->type == CPP_OPEN_SQUARE
26584 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
26585 && token->type == CPP_OPEN_SQUARE)));
26586 }
26587
26588 /* Return TRUE iff the next Nth tokens in the stream are possibly the
26589 beginning of a GNU extension attribute. */
26590
26591 static bool
26592 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
26593 {
26594 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
26595
26596 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
26597 }
26598
26599 /* Return true iff the next tokens can be the beginning of either a
26600 GNU attribute list, or a standard C++11 attribute sequence. */
26601
26602 static bool
26603 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
26604 {
26605 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
26606 || cp_next_tokens_can_be_std_attribute_p (parser));
26607 }
26608
26609 /* Return true iff the next Nth tokens can be the beginning of either
26610 a GNU attribute list, or a standard C++11 attribute sequence. */
26611
26612 static bool
26613 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
26614 {
26615 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
26616 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
26617 }
26618
26619 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
26620 of GNU attributes, or return NULL. */
26621
26622 static tree
26623 cp_parser_attributes_opt (cp_parser *parser)
26624 {
26625 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
26626 return cp_parser_gnu_attributes_opt (parser);
26627 return cp_parser_std_attribute_spec_seq (parser);
26628 }
26629
26630 /* Parse an (optional) series of attributes.
26631
26632 attributes:
26633 attributes attribute
26634
26635 attribute:
26636 __attribute__ (( attribute-list [opt] ))
26637
26638 The return value is as for cp_parser_gnu_attribute_list. */
26639
26640 static tree
26641 cp_parser_gnu_attributes_opt (cp_parser* parser)
26642 {
26643 tree attributes = NULL_TREE;
26644
26645 temp_override<bool> cleanup
26646 (parser->auto_is_implicit_function_template_parm_p, false);
26647
26648 while (true)
26649 {
26650 cp_token *token;
26651 tree attribute_list;
26652 bool ok = true;
26653
26654 /* Peek at the next token. */
26655 token = cp_lexer_peek_token (parser->lexer);
26656 /* If it's not `__attribute__', then we're done. */
26657 if (token->keyword != RID_ATTRIBUTE)
26658 break;
26659
26660 /* Consume the `__attribute__' keyword. */
26661 cp_lexer_consume_token (parser->lexer);
26662 /* Look for the two `(' tokens. */
26663 matching_parens outer_parens;
26664 if (!outer_parens.require_open (parser))
26665 ok = false;
26666 matching_parens inner_parens;
26667 if (!inner_parens.require_open (parser))
26668 ok = false;
26669
26670 /* Peek at the next token. */
26671 token = cp_lexer_peek_token (parser->lexer);
26672 if (token->type != CPP_CLOSE_PAREN)
26673 /* Parse the attribute-list. */
26674 attribute_list = cp_parser_gnu_attribute_list (parser);
26675 else
26676 /* If the next token is a `)', then there is no attribute
26677 list. */
26678 attribute_list = NULL;
26679
26680 /* Look for the two `)' tokens. */
26681 if (!inner_parens.require_close (parser))
26682 ok = false;
26683 if (!outer_parens.require_close (parser))
26684 ok = false;
26685 if (!ok)
26686 cp_parser_skip_to_end_of_statement (parser);
26687
26688 /* Add these new attributes to the list. */
26689 attributes = attr_chainon (attributes, attribute_list);
26690 }
26691
26692 return attributes;
26693 }
26694
26695 /* Parse a GNU attribute-list.
26696
26697 attribute-list:
26698 attribute
26699 attribute-list , attribute
26700
26701 attribute:
26702 identifier
26703 identifier ( identifier )
26704 identifier ( identifier , expression-list )
26705 identifier ( expression-list )
26706
26707 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
26708 to an attribute. The TREE_PURPOSE of each node is the identifier
26709 indicating which attribute is in use. The TREE_VALUE represents
26710 the arguments, if any. */
26711
26712 static tree
26713 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
26714 {
26715 tree attribute_list = NULL_TREE;
26716 bool save_translate_strings_p = parser->translate_strings_p;
26717
26718 /* Don't create wrapper nodes within attributes: the
26719 handlers don't know how to handle them. */
26720 auto_suppress_location_wrappers sentinel;
26721
26722 parser->translate_strings_p = false;
26723 while (true)
26724 {
26725 cp_token *token;
26726 tree identifier;
26727 tree attribute;
26728
26729 /* Look for the identifier. We also allow keywords here; for
26730 example `__attribute__ ((const))' is legal. */
26731 token = cp_lexer_peek_token (parser->lexer);
26732 if (token->type == CPP_NAME
26733 || token->type == CPP_KEYWORD)
26734 {
26735 tree arguments = NULL_TREE;
26736
26737 /* Consume the token, but save it since we need it for the
26738 SIMD enabled function parsing. */
26739 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
26740
26741 /* Save away the identifier that indicates which attribute
26742 this is. */
26743 identifier = (token->type == CPP_KEYWORD)
26744 /* For keywords, use the canonical spelling, not the
26745 parsed identifier. */
26746 ? ridpointers[(int) token->keyword]
26747 : id_token->u.value;
26748
26749 identifier = canonicalize_attr_name (identifier);
26750 attribute = build_tree_list (identifier, NULL_TREE);
26751
26752 /* Peek at the next token. */
26753 token = cp_lexer_peek_token (parser->lexer);
26754 /* If it's an `(', then parse the attribute arguments. */
26755 if (token->type == CPP_OPEN_PAREN)
26756 {
26757 vec<tree, va_gc> *vec;
26758 int attr_flag = (attribute_takes_identifier_p (identifier)
26759 ? id_attr : normal_attr);
26760 vec = cp_parser_parenthesized_expression_list
26761 (parser, attr_flag, /*cast_p=*/false,
26762 /*allow_expansion_p=*/false,
26763 /*non_constant_p=*/NULL);
26764 if (vec == NULL)
26765 arguments = error_mark_node;
26766 else
26767 {
26768 arguments = build_tree_list_vec (vec);
26769 release_tree_vector (vec);
26770 }
26771 /* Save the arguments away. */
26772 TREE_VALUE (attribute) = arguments;
26773 }
26774
26775 if (arguments != error_mark_node)
26776 {
26777 /* Add this attribute to the list. */
26778 TREE_CHAIN (attribute) = attribute_list;
26779 attribute_list = attribute;
26780 }
26781
26782 token = cp_lexer_peek_token (parser->lexer);
26783 }
26784 /* Unless EXACTLY_ONE is set look for more attributes.
26785 If the next token isn't a `,', we're done. */
26786 if (exactly_one || token->type != CPP_COMMA)
26787 break;
26788
26789 /* Consume the comma and keep going. */
26790 cp_lexer_consume_token (parser->lexer);
26791 }
26792 parser->translate_strings_p = save_translate_strings_p;
26793
26794 /* We built up the list in reverse order. */
26795 return nreverse (attribute_list);
26796 }
26797
26798 /* Parse a standard C++11 attribute.
26799
26800 The returned representation is a TREE_LIST which TREE_PURPOSE is
26801 the scoped name of the attribute, and the TREE_VALUE is its
26802 arguments list.
26803
26804 Note that the scoped name of the attribute is itself a TREE_LIST
26805 which TREE_PURPOSE is the namespace of the attribute, and
26806 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
26807 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
26808 and which TREE_PURPOSE is directly the attribute name.
26809
26810 Clients of the attribute code should use get_attribute_namespace
26811 and get_attribute_name to get the actual namespace and name of
26812 attributes, regardless of their being GNU or C++11 attributes.
26813
26814 attribute:
26815 attribute-token attribute-argument-clause [opt]
26816
26817 attribute-token:
26818 identifier
26819 attribute-scoped-token
26820
26821 attribute-scoped-token:
26822 attribute-namespace :: identifier
26823
26824 attribute-namespace:
26825 identifier
26826
26827 attribute-argument-clause:
26828 ( balanced-token-seq )
26829
26830 balanced-token-seq:
26831 balanced-token [opt]
26832 balanced-token-seq balanced-token
26833
26834 balanced-token:
26835 ( balanced-token-seq )
26836 [ balanced-token-seq ]
26837 { balanced-token-seq }. */
26838
26839 static tree
26840 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
26841 {
26842 tree attribute, attr_id = NULL_TREE, arguments;
26843 cp_token *token;
26844
26845 temp_override<bool> cleanup
26846 (parser->auto_is_implicit_function_template_parm_p, false);
26847
26848 /* First, parse name of the attribute, a.k.a attribute-token. */
26849
26850 token = cp_lexer_peek_token (parser->lexer);
26851 if (token->type == CPP_NAME)
26852 attr_id = token->u.value;
26853 else if (token->type == CPP_KEYWORD)
26854 attr_id = ridpointers[(int) token->keyword];
26855 else if (token->flags & NAMED_OP)
26856 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26857
26858 if (attr_id == NULL_TREE)
26859 return NULL_TREE;
26860
26861 cp_lexer_consume_token (parser->lexer);
26862
26863 token = cp_lexer_peek_token (parser->lexer);
26864 if (token->type == CPP_SCOPE)
26865 {
26866 /* We are seeing a scoped attribute token. */
26867
26868 cp_lexer_consume_token (parser->lexer);
26869 if (attr_ns)
26870 error_at (token->location, "attribute using prefix used together "
26871 "with scoped attribute token");
26872 attr_ns = attr_id;
26873
26874 token = cp_lexer_peek_token (parser->lexer);
26875 if (token->type == CPP_NAME)
26876 attr_id = token->u.value;
26877 else if (token->type == CPP_KEYWORD)
26878 attr_id = ridpointers[(int) token->keyword];
26879 else if (token->flags & NAMED_OP)
26880 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26881 else
26882 {
26883 error_at (token->location,
26884 "expected an identifier for the attribute name");
26885 return error_mark_node;
26886 }
26887 cp_lexer_consume_token (parser->lexer);
26888
26889 attr_ns = canonicalize_attr_name (attr_ns);
26890 attr_id = canonicalize_attr_name (attr_id);
26891 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26892 NULL_TREE);
26893 token = cp_lexer_peek_token (parser->lexer);
26894 }
26895 else if (attr_ns)
26896 {
26897 attr_ns = canonicalize_attr_name (attr_ns);
26898 attr_id = canonicalize_attr_name (attr_id);
26899 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26900 NULL_TREE);
26901 }
26902 else
26903 {
26904 attr_id = canonicalize_attr_name (attr_id);
26905 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
26906 NULL_TREE);
26907 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
26908 but no longer: we have to be able to tell [[noreturn]] and
26909 __attribute__((noreturn)) apart. */
26910 /* C++14 deprecated attribute is equivalent to GNU's. */
26911 if (is_attribute_p ("deprecated", attr_id))
26912 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26913 /* C++17 fallthrough attribute is equivalent to GNU's. */
26914 else if (is_attribute_p ("fallthrough", attr_id))
26915 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26916 /* Transactional Memory TS optimize_for_synchronized attribute is
26917 equivalent to GNU transaction_callable. */
26918 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
26919 TREE_PURPOSE (attribute)
26920 = get_identifier ("transaction_callable");
26921 /* Transactional Memory attributes are GNU attributes. */
26922 else if (tm_attr_to_mask (attr_id))
26923 TREE_PURPOSE (attribute) = attr_id;
26924 }
26925
26926 /* Now parse the optional argument clause of the attribute. */
26927
26928 if (token->type != CPP_OPEN_PAREN)
26929 return attribute;
26930
26931 {
26932 vec<tree, va_gc> *vec;
26933 int attr_flag = normal_attr;
26934
26935 /* Maybe we don't expect to see any arguments for this attribute. */
26936 const attribute_spec *as
26937 = lookup_attribute_spec (TREE_PURPOSE (attribute));
26938 if (as && as->max_length == 0)
26939 {
26940 error_at (token->location, "%qE attribute does not take any arguments",
26941 attr_id);
26942 cp_parser_skip_to_closing_parenthesis (parser,
26943 /*recovering=*/true,
26944 /*or_comma=*/false,
26945 /*consume_paren=*/true);
26946 return error_mark_node;
26947 }
26948
26949 if (attr_ns == gnu_identifier
26950 && attribute_takes_identifier_p (attr_id))
26951 /* A GNU attribute that takes an identifier in parameter. */
26952 attr_flag = id_attr;
26953
26954 if (as == NULL)
26955 {
26956 /* For unknown attributes, just skip balanced tokens instead of
26957 trying to parse the arguments. */
26958 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
26959 cp_lexer_consume_token (parser->lexer);
26960 return attribute;
26961 }
26962
26963 vec = cp_parser_parenthesized_expression_list
26964 (parser, attr_flag, /*cast_p=*/false,
26965 /*allow_expansion_p=*/true,
26966 /*non_constant_p=*/NULL);
26967 if (vec == NULL)
26968 arguments = error_mark_node;
26969 else
26970 {
26971 if (vec->is_empty ())
26972 /* e.g. [[attr()]]. */
26973 error_at (token->location, "parentheses must be omitted if "
26974 "%qE attribute argument list is empty",
26975 attr_id);
26976 arguments = build_tree_list_vec (vec);
26977 release_tree_vector (vec);
26978 }
26979
26980 if (arguments == error_mark_node)
26981 attribute = error_mark_node;
26982 else
26983 TREE_VALUE (attribute) = arguments;
26984 }
26985
26986 return attribute;
26987 }
26988
26989 /* Check that the attribute ATTRIBUTE appears at most once in the
26990 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3),
26991 nodiscard, and deprecated (7.6.5). Note that
26992 carries_dependency (7.6.4) isn't implemented yet in GCC. */
26993
26994 static void
26995 cp_parser_check_std_attribute (tree attributes, tree attribute)
26996 {
26997 if (attributes)
26998 {
26999 tree name = get_attribute_name (attribute);
27000 if (is_attribute_p ("noreturn", name)
27001 && lookup_attribute ("noreturn", attributes))
27002 error ("attribute %<noreturn%> can appear at most once "
27003 "in an attribute-list");
27004 else if (is_attribute_p ("deprecated", name)
27005 && lookup_attribute ("deprecated", attributes))
27006 error ("attribute %<deprecated%> can appear at most once "
27007 "in an attribute-list");
27008 else if (is_attribute_p ("nodiscard", name)
27009 && lookup_attribute ("nodiscard", attributes))
27010 error ("attribute %<nodiscard%> can appear at most once "
27011 "in an attribute-list");
27012 }
27013 }
27014
27015 /* Parse a list of standard C++-11 attributes.
27016
27017 attribute-list:
27018 attribute [opt]
27019 attribute-list , attribute[opt]
27020 attribute ...
27021 attribute-list , attribute ...
27022 */
27023
27024 static tree
27025 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
27026 {
27027 tree attributes = NULL_TREE, attribute = NULL_TREE;
27028 cp_token *token = NULL;
27029
27030 while (true)
27031 {
27032 attribute = cp_parser_std_attribute (parser, attr_ns);
27033 if (attribute == error_mark_node)
27034 break;
27035 if (attribute != NULL_TREE)
27036 {
27037 cp_parser_check_std_attribute (attributes, attribute);
27038 TREE_CHAIN (attribute) = attributes;
27039 attributes = attribute;
27040 }
27041 token = cp_lexer_peek_token (parser->lexer);
27042 if (token->type == CPP_ELLIPSIS)
27043 {
27044 cp_lexer_consume_token (parser->lexer);
27045 if (attribute == NULL_TREE)
27046 error_at (token->location,
27047 "expected attribute before %<...%>");
27048 else
27049 {
27050 tree pack = make_pack_expansion (TREE_VALUE (attribute));
27051 if (pack == error_mark_node)
27052 return error_mark_node;
27053 TREE_VALUE (attribute) = pack;
27054 }
27055 token = cp_lexer_peek_token (parser->lexer);
27056 }
27057 if (token->type != CPP_COMMA)
27058 break;
27059 cp_lexer_consume_token (parser->lexer);
27060 }
27061 attributes = nreverse (attributes);
27062 return attributes;
27063 }
27064
27065 /* Parse a standard C++-11 attribute specifier.
27066
27067 attribute-specifier:
27068 [ [ attribute-using-prefix [opt] attribute-list ] ]
27069 alignment-specifier
27070
27071 attribute-using-prefix:
27072 using attribute-namespace :
27073
27074 alignment-specifier:
27075 alignas ( type-id ... [opt] )
27076 alignas ( alignment-expression ... [opt] ). */
27077
27078 static tree
27079 cp_parser_std_attribute_spec (cp_parser *parser)
27080 {
27081 tree attributes = NULL_TREE;
27082 cp_token *token = cp_lexer_peek_token (parser->lexer);
27083
27084 if (token->type == CPP_OPEN_SQUARE
27085 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
27086 {
27087 tree attr_ns = NULL_TREE;
27088
27089 cp_lexer_consume_token (parser->lexer);
27090 cp_lexer_consume_token (parser->lexer);
27091
27092 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27093 {
27094 token = cp_lexer_peek_nth_token (parser->lexer, 2);
27095 if (token->type == CPP_NAME)
27096 attr_ns = token->u.value;
27097 else if (token->type == CPP_KEYWORD)
27098 attr_ns = ridpointers[(int) token->keyword];
27099 else if (token->flags & NAMED_OP)
27100 attr_ns = get_identifier (cpp_type2name (token->type,
27101 token->flags));
27102 if (attr_ns
27103 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
27104 {
27105 if (cxx_dialect < cxx17)
27106 pedwarn (input_location, 0,
27107 "attribute using prefix only available "
27108 "with %<-std=c++17%> or %<-std=gnu++17%>");
27109
27110 cp_lexer_consume_token (parser->lexer);
27111 cp_lexer_consume_token (parser->lexer);
27112 cp_lexer_consume_token (parser->lexer);
27113 }
27114 else
27115 attr_ns = NULL_TREE;
27116 }
27117
27118 attributes = cp_parser_std_attribute_list (parser, attr_ns);
27119
27120 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
27121 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
27122 cp_parser_skip_to_end_of_statement (parser);
27123 else
27124 /* Warn about parsing c++11 attribute in non-c++11 mode, only
27125 when we are sure that we have actually parsed them. */
27126 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
27127 }
27128 else
27129 {
27130 tree alignas_expr;
27131
27132 /* Look for an alignment-specifier. */
27133
27134 token = cp_lexer_peek_token (parser->lexer);
27135
27136 if (token->type != CPP_KEYWORD
27137 || token->keyword != RID_ALIGNAS)
27138 return NULL_TREE;
27139
27140 cp_lexer_consume_token (parser->lexer);
27141 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
27142
27143 matching_parens parens;
27144 if (!parens.require_open (parser))
27145 return error_mark_node;
27146
27147 cp_parser_parse_tentatively (parser);
27148 alignas_expr = cp_parser_type_id (parser);
27149
27150 if (!cp_parser_parse_definitely (parser))
27151 {
27152 alignas_expr = cp_parser_assignment_expression (parser);
27153 if (alignas_expr == error_mark_node)
27154 cp_parser_skip_to_end_of_statement (parser);
27155 if (alignas_expr == NULL_TREE
27156 || alignas_expr == error_mark_node)
27157 return alignas_expr;
27158 }
27159
27160 alignas_expr = cxx_alignas_expr (alignas_expr);
27161 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
27162
27163 /* Handle alignas (pack...). */
27164 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27165 {
27166 cp_lexer_consume_token (parser->lexer);
27167 alignas_expr = make_pack_expansion (alignas_expr);
27168 }
27169
27170 /* Something went wrong, so don't build the attribute. */
27171 if (alignas_expr == error_mark_node)
27172 return error_mark_node;
27173
27174 /* Missing ')' means the code cannot possibly be valid; go ahead
27175 and commit to make sure we issue a hard error. */
27176 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
27177 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27178 cp_parser_commit_to_tentative_parse (parser);
27179
27180 if (!parens.require_close (parser))
27181 return error_mark_node;
27182
27183 /* Build the C++-11 representation of an 'aligned'
27184 attribute. */
27185 attributes
27186 = build_tree_list (build_tree_list (gnu_identifier,
27187 aligned_identifier), alignas_expr);
27188 }
27189
27190 return attributes;
27191 }
27192
27193 /* Parse a standard C++-11 attribute-specifier-seq.
27194
27195 attribute-specifier-seq:
27196 attribute-specifier-seq [opt] attribute-specifier
27197 */
27198
27199 static tree
27200 cp_parser_std_attribute_spec_seq (cp_parser *parser)
27201 {
27202 tree attr_specs = NULL_TREE;
27203 tree attr_last = NULL_TREE;
27204
27205 /* Don't create wrapper nodes within attributes: the
27206 handlers don't know how to handle them. */
27207 auto_suppress_location_wrappers sentinel;
27208
27209 while (true)
27210 {
27211 tree attr_spec = cp_parser_std_attribute_spec (parser);
27212 if (attr_spec == NULL_TREE)
27213 break;
27214 if (attr_spec == error_mark_node)
27215 return error_mark_node;
27216
27217 if (attr_last)
27218 TREE_CHAIN (attr_last) = attr_spec;
27219 else
27220 attr_specs = attr_last = attr_spec;
27221 attr_last = tree_last (attr_last);
27222 }
27223
27224 return attr_specs;
27225 }
27226
27227 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
27228 return index of the first token after balanced-token, or N on failure. */
27229
27230 static size_t
27231 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
27232 {
27233 size_t orig_n = n;
27234 int nparens = 0, nbraces = 0, nsquares = 0;
27235 do
27236 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
27237 {
27238 case CPP_PRAGMA_EOL:
27239 if (!parser->lexer->in_pragma)
27240 break;
27241 /* FALLTHRU */
27242 case CPP_EOF:
27243 /* Ran out of tokens. */
27244 return orig_n;
27245 case CPP_OPEN_PAREN:
27246 ++nparens;
27247 break;
27248 case CPP_OPEN_BRACE:
27249 ++nbraces;
27250 break;
27251 case CPP_OPEN_SQUARE:
27252 ++nsquares;
27253 break;
27254 case CPP_CLOSE_PAREN:
27255 --nparens;
27256 break;
27257 case CPP_CLOSE_BRACE:
27258 --nbraces;
27259 break;
27260 case CPP_CLOSE_SQUARE:
27261 --nsquares;
27262 break;
27263 default:
27264 break;
27265 }
27266 while (nparens || nbraces || nsquares);
27267 return n;
27268 }
27269
27270 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
27271 return index of the first token after the GNU attribute tokens, or N on
27272 failure. */
27273
27274 static size_t
27275 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
27276 {
27277 while (true)
27278 {
27279 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
27280 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
27281 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
27282 break;
27283
27284 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
27285 if (n2 == n + 2)
27286 break;
27287 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
27288 break;
27289 n = n2 + 1;
27290 }
27291 return n;
27292 }
27293
27294 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
27295 next token), return index of the first token after the standard C++11
27296 attribute tokens, or N on failure. */
27297
27298 static size_t
27299 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
27300 {
27301 while (true)
27302 {
27303 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
27304 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
27305 {
27306 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
27307 if (n2 == n + 1)
27308 break;
27309 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
27310 break;
27311 n = n2 + 1;
27312 }
27313 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
27314 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
27315 {
27316 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
27317 if (n2 == n + 1)
27318 break;
27319 n = n2;
27320 }
27321 else
27322 break;
27323 }
27324 return n;
27325 }
27326
27327 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
27328 as the next token), return index of the first token after the attribute
27329 tokens, or N on failure. */
27330
27331 static size_t
27332 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
27333 {
27334 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
27335 return cp_parser_skip_gnu_attributes_opt (parser, n);
27336 return cp_parser_skip_std_attribute_spec_seq (parser, n);
27337 }
27338
27339 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
27340 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
27341 current value of the PEDANTIC flag, regardless of whether or not
27342 the `__extension__' keyword is present. The caller is responsible
27343 for restoring the value of the PEDANTIC flag. */
27344
27345 static bool
27346 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
27347 {
27348 /* Save the old value of the PEDANTIC flag. */
27349 *saved_pedantic = pedantic;
27350
27351 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
27352 {
27353 /* Consume the `__extension__' token. */
27354 cp_lexer_consume_token (parser->lexer);
27355 /* We're not being pedantic while the `__extension__' keyword is
27356 in effect. */
27357 pedantic = 0;
27358
27359 return true;
27360 }
27361
27362 return false;
27363 }
27364
27365 /* Parse a label declaration.
27366
27367 label-declaration:
27368 __label__ label-declarator-seq ;
27369
27370 label-declarator-seq:
27371 identifier , label-declarator-seq
27372 identifier */
27373
27374 static void
27375 cp_parser_label_declaration (cp_parser* parser)
27376 {
27377 /* Look for the `__label__' keyword. */
27378 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
27379
27380 while (true)
27381 {
27382 tree identifier;
27383
27384 /* Look for an identifier. */
27385 identifier = cp_parser_identifier (parser);
27386 /* If we failed, stop. */
27387 if (identifier == error_mark_node)
27388 break;
27389 /* Declare it as a label. */
27390 finish_label_decl (identifier);
27391 /* If the next token is a `;', stop. */
27392 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27393 break;
27394 /* Look for the `,' separating the label declarations. */
27395 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
27396 }
27397
27398 /* Look for the final `;'. */
27399 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27400 }
27401
27402 // -------------------------------------------------------------------------- //
27403 // Concept definitions
27404
27405 static tree
27406 cp_parser_concept_definition (cp_parser *parser)
27407 {
27408 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
27409 cp_lexer_consume_token (parser->lexer);
27410
27411 cp_expr id = cp_parser_identifier (parser);
27412 if (id == error_mark_node)
27413 {
27414 cp_parser_skip_to_end_of_statement (parser);
27415 cp_parser_consume_semicolon_at_end_of_statement (parser);
27416 return NULL_TREE;
27417 }
27418
27419 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27420 {
27421 cp_parser_skip_to_end_of_statement (parser);
27422 cp_parser_consume_semicolon_at_end_of_statement (parser);
27423 return error_mark_node;
27424 }
27425
27426 processing_constraint_expression_sentinel parsing_constraint;
27427 tree init = cp_parser_constraint_expression (parser);
27428 if (init == error_mark_node)
27429 cp_parser_skip_to_end_of_statement (parser);
27430
27431 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
27432 but continue as if it were. */
27433 cp_parser_consume_semicolon_at_end_of_statement (parser);
27434
27435 return finish_concept_definition (id, init);
27436 }
27437
27438 // -------------------------------------------------------------------------- //
27439 // Requires Clause
27440
27441 /* Diagnose an expression that should appear in ()'s within a requires-clause
27442 and suggest where to place those parentheses. */
27443
27444 static void
27445 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
27446 {
27447 error_at (loc, "expression must be enclosed in parentheses");
27448 }
27449
27450 static void
27451 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
27452 {
27453 gcc_rich_location richloc (loc);
27454 richloc.add_fixit_insert_before ("(");
27455 richloc.add_fixit_insert_after (")");
27456 error_at (&richloc, "expression must be enclosed in parentheses");
27457 }
27458
27459 /* Characterizes the likely kind of expression intended by a mis-written
27460 primary constraint. */
27461 enum primary_constraint_error
27462 {
27463 pce_ok,
27464 pce_maybe_operator,
27465 pce_maybe_postfix
27466 };
27467
27468 /* Returns true if the token(s) following a primary-expression in a
27469 constraint-logical-* expression would require parentheses. */
27470
27471 static primary_constraint_error
27472 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
27473 {
27474 cp_token *token = cp_lexer_peek_token (parser->lexer);
27475 switch (token->type)
27476 {
27477 default:
27478 return pce_ok;
27479
27480 case CPP_EQ:
27481 {
27482 /* An equal sign may be part of the definition of a function,
27483 and not an assignment operator, when parsing the expression
27484 for a trailing requires-clause. For example:
27485
27486 template<typename T>
27487 struct S {
27488 S() requires C<T> = default;
27489 };
27490
27491 Don't try to reparse this a binary operator. */
27492 if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
27493 || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
27494 return pce_ok;
27495
27496 gcc_fallthrough ();
27497 }
27498
27499 /* Arithmetic operators. */
27500 case CPP_PLUS:
27501 case CPP_MINUS:
27502 case CPP_MULT:
27503 case CPP_DIV:
27504 case CPP_MOD:
27505 /* Bitwise operators. */
27506 case CPP_AND:
27507 case CPP_OR:
27508 case CPP_XOR:
27509 case CPP_RSHIFT:
27510 case CPP_LSHIFT:
27511 /* Relational operators. */
27512 case CPP_EQ_EQ:
27513 case CPP_NOT_EQ:
27514 case CPP_LESS:
27515 case CPP_GREATER:
27516 case CPP_LESS_EQ:
27517 case CPP_GREATER_EQ:
27518 case CPP_SPACESHIP:
27519 /* Pointer-to-member. */
27520 case CPP_DOT_STAR:
27521 case CPP_DEREF_STAR:
27522 /* Assignment operators. */
27523 case CPP_PLUS_EQ:
27524 case CPP_MINUS_EQ:
27525 case CPP_MULT_EQ:
27526 case CPP_DIV_EQ:
27527 case CPP_MOD_EQ:
27528 case CPP_AND_EQ:
27529 case CPP_OR_EQ:
27530 case CPP_XOR_EQ:
27531 case CPP_RSHIFT_EQ:
27532 case CPP_LSHIFT_EQ:
27533 /* Conditional operator */
27534 case CPP_QUERY:
27535 /* Unenclosed binary or conditional operator. */
27536 return pce_maybe_operator;
27537
27538 case CPP_OPEN_PAREN:
27539 {
27540 /* A primary constraint that precedes the parameter-list of a
27541 lambda expression is followed by an open paren.
27542
27543 []<typename T> requires C (T a, T b) { ... }
27544
27545 Don't try to re-parse this as a postfix expression. */
27546 if (lambda_p)
27547 return pce_ok;
27548
27549 gcc_fallthrough ();
27550 }
27551 case CPP_OPEN_SQUARE:
27552 {
27553 /* A primary-constraint-expression followed by a '[[' is not a
27554 postfix expression. */
27555 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
27556 return pce_ok;
27557
27558 gcc_fallthrough ();
27559 }
27560 case CPP_PLUS_PLUS:
27561 case CPP_MINUS_MINUS:
27562 case CPP_DOT:
27563 case CPP_DEREF:
27564 /* Unenclosed postfix operator. */
27565 return pce_maybe_postfix;
27566 }
27567 }
27568
27569 /* Returns true if the next token begins a unary expression, preceded by
27570 an operator or keyword. */
27571
27572 static bool
27573 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
27574 {
27575 cp_token *token = cp_lexer_peek_token (parser->lexer);
27576 switch (token->type)
27577 {
27578 case CPP_NOT:
27579 case CPP_PLUS:
27580 case CPP_MINUS:
27581 case CPP_MULT:
27582 case CPP_COMPL:
27583 case CPP_PLUS_PLUS:
27584 case CPP_MINUS_MINUS:
27585 return true;
27586
27587 case CPP_KEYWORD:
27588 {
27589 switch (token->keyword)
27590 {
27591 case RID_STATCAST:
27592 case RID_DYNCAST:
27593 case RID_REINTCAST:
27594 case RID_CONSTCAST:
27595 case RID_TYPEID:
27596 case RID_SIZEOF:
27597 case RID_ALIGNOF:
27598 case RID_NOEXCEPT:
27599 case RID_NEW:
27600 case RID_DELETE:
27601 case RID_THROW:
27602 return true;
27603
27604 default:
27605 break;
27606 }
27607 }
27608
27609 default:
27610 break;
27611 }
27612
27613 return false;
27614 }
27615
27616 /* Parse a primary expression within a constraint. */
27617
27618 static cp_expr
27619 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
27620 {
27621 /* If this looks like a unary expression, parse it as such, but diagnose
27622 it as ill-formed; it requires parens. */
27623 if (cp_parser_unary_constraint_requires_parens (parser))
27624 {
27625 cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
27626 cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
27627 return e;
27628 }
27629
27630 cp_lexer_save_tokens (parser->lexer);
27631 cp_id_kind idk;
27632 location_t loc = input_location;
27633 cp_expr expr = cp_parser_primary_expression (parser,
27634 /*address_p=*/false,
27635 /*cast_p=*/false,
27636 /*template_arg_p=*/false,
27637 &idk);
27638 expr.maybe_add_location_wrapper ();
27639
27640 primary_constraint_error pce = pce_ok;
27641 if (expr != error_mark_node)
27642 {
27643 /* The primary-expression could be part of an unenclosed non-logical
27644 compound expression. */
27645 pce = cp_parser_constraint_requires_parens (parser, lambda_p);
27646 }
27647 if (pce == pce_ok)
27648 {
27649 cp_lexer_commit_tokens (parser->lexer);
27650 return finish_constraint_primary_expr (expr);
27651 }
27652
27653 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
27654 error, but return the expression as if it were valid. */
27655 cp_lexer_rollback_tokens (parser->lexer);
27656 cp_parser_parse_tentatively (parser);
27657 if (pce == pce_maybe_operator)
27658 expr = cp_parser_assignment_expression (parser, NULL, false, false);
27659 else
27660 expr = cp_parser_simple_cast_expression (parser);
27661 if (cp_parser_parse_definitely (parser))
27662 {
27663 cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
27664 return expr;
27665 }
27666
27667 /* Otherwise, something has gone very wrong, and we can't generate a more
27668 meaningful diagnostic or recover. */
27669 cp_parser_diagnose_ungrouped_constraint_plain (loc);
27670 return error_mark_node;
27671 }
27672
27673 /* Parse a constraint-logical-and-expression.
27674
27675 constraint-logical-and-expression:
27676 primary-expression
27677 constraint-logical-and-expression '&&' primary-expression */
27678
27679 static cp_expr
27680 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
27681 {
27682 cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
27683 while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
27684 {
27685 cp_token *op = cp_lexer_consume_token (parser->lexer);
27686 tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
27687 lhs = finish_constraint_and_expr (op->location, lhs, rhs);
27688 }
27689 return lhs;
27690 }
27691
27692 /* Parse a constraint-logical-or-expression.
27693
27694 constraint-logical-or-expression:
27695 constraint-logical-and-expression
27696 constraint-logical-or-expression '||' constraint-logical-and-expression */
27697
27698 static cp_expr
27699 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
27700 {
27701 cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
27702 while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
27703 {
27704 cp_token *op = cp_lexer_consume_token (parser->lexer);
27705 cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
27706 lhs = finish_constraint_or_expr (op->location, lhs, rhs);
27707 }
27708 return lhs;
27709 }
27710
27711 /* Parse the expression after a requires-clause. This has a different grammar
27712 than that in the concepts TS. */
27713
27714 static tree
27715 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
27716 {
27717 processing_constraint_expression_sentinel parsing_constraint;
27718 temp_override<int> ovr (processing_template_decl);
27719 if (!processing_template_decl)
27720 /* Adjust processing_template_decl so that we always obtain template
27721 trees here. We don't do the usual ++processing_template_decl
27722 because that would skew the template parameter depth of a lambda
27723 within if we're already inside a template. */
27724 processing_template_decl = 1;
27725 cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
27726 if (check_for_bare_parameter_packs (expr))
27727 expr = error_mark_node;
27728 return expr;
27729 }
27730
27731 /* Parse a expression after a requires clause.
27732
27733 constraint-expression:
27734 logical-or-expression
27735
27736 The required logical-or-expression must be a constant expression. Note
27737 that we don't check that the expression is constepxr here. We defer until
27738 we analyze constraints and then, we only check atomic constraints. */
27739
27740 static tree
27741 cp_parser_constraint_expression (cp_parser *parser)
27742 {
27743 processing_constraint_expression_sentinel parsing_constraint;
27744 temp_override<int> ovr (processing_template_decl);
27745 if (!processing_template_decl)
27746 /* As in cp_parser_requires_clause_expression. */
27747 processing_template_decl = 1;
27748 cp_expr expr = cp_parser_binary_expression (parser, false, true,
27749 PREC_NOT_OPERATOR, NULL);
27750 if (check_for_bare_parameter_packs (expr))
27751 expr = error_mark_node;
27752 expr.maybe_add_location_wrapper ();
27753 return expr;
27754 }
27755
27756 /* Optionally parse a requires clause:
27757
27758 requires-clause:
27759 `requires` constraint-logical-or-expression.
27760 [ConceptsTS]
27761 `requires constraint-expression.
27762
27763 LAMBDA_P is true when the requires-clause is parsed before the
27764 parameter-list of a lambda-declarator. */
27765
27766 static tree
27767 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
27768 {
27769 cp_token *tok = cp_lexer_peek_token (parser->lexer);
27770 if (tok->keyword != RID_REQUIRES)
27771 {
27772 if (!flag_concepts && tok->type == CPP_NAME
27773 && tok->u.value == ridpointers[RID_REQUIRES])
27774 {
27775 error_at (cp_lexer_peek_token (parser->lexer)->location,
27776 "%<requires%> only available with "
27777 "%<-std=c++20%> or %<-fconcepts%>");
27778 /* Parse and discard the requires-clause. */
27779 cp_lexer_consume_token (parser->lexer);
27780 cp_parser_constraint_expression (parser);
27781 }
27782 return NULL_TREE;
27783 }
27784
27785 cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
27786 if (tok2->type == CPP_OPEN_BRACE)
27787 {
27788 /* An opening brace following the start of a requires-clause is
27789 ill-formed; the user likely forgot the second `requires' that
27790 would start a requires-expression. */
27791 gcc_rich_location richloc (tok2->location);
27792 richloc.add_fixit_insert_after (tok->location, " requires");
27793 error_at (&richloc, "missing additional %<requires%> to start "
27794 "a requires-expression");
27795 /* Don't consume the `requires', so that it's reused as the start of a
27796 requires-expression. */
27797 }
27798 else
27799 cp_lexer_consume_token (parser->lexer);
27800
27801 if (!flag_concepts_ts)
27802 return cp_parser_requires_clause_expression (parser, lambda_p);
27803 else
27804 return cp_parser_constraint_expression (parser);
27805 }
27806
27807 /*---------------------------------------------------------------------------
27808 Requires expressions
27809 ---------------------------------------------------------------------------*/
27810
27811 /* Parse a requires expression
27812
27813 requirement-expression:
27814 'requires' requirement-parameter-list [opt] requirement-body */
27815
27816 static tree
27817 cp_parser_requires_expression (cp_parser *parser)
27818 {
27819 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
27820 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
27821
27822 /* Avoid committing to outer tentative parse. */
27823 tentative_firewall firewall (parser);
27824
27825 /* This is definitely a requires-expression. */
27826 cp_parser_commit_to_tentative_parse (parser);
27827
27828 tree parms, reqs;
27829 {
27830 /* Local parameters are delared as variables within the scope
27831 of the expression. They are not visible past the end of
27832 the expression. Expressions within the requires-expression
27833 are unevaluated. */
27834 struct scope_sentinel
27835 {
27836 scope_sentinel ()
27837 {
27838 ++cp_unevaluated_operand;
27839 begin_scope (sk_block, NULL_TREE);
27840 }
27841
27842 ~scope_sentinel ()
27843 {
27844 pop_bindings_and_leave_scope ();
27845 --cp_unevaluated_operand;
27846 }
27847 } s;
27848
27849 /* Parse the optional parameter list. */
27850 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27851 {
27852 parms = cp_parser_requirement_parameter_list (parser);
27853 if (parms == error_mark_node)
27854 return error_mark_node;
27855 }
27856 else
27857 parms = NULL_TREE;
27858
27859 /* Parse the requirement body. */
27860 temp_override<int> ovr (processing_template_decl);
27861 if (!processing_template_decl)
27862 /* As in cp_parser_requires_clause_expression. */
27863 processing_template_decl = 1;
27864 reqs = cp_parser_requirement_body (parser);
27865 if (reqs == error_mark_node)
27866 return error_mark_node;
27867 }
27868
27869 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
27870 the parm chain. */
27871 grokparms (parms, &parms);
27872 loc = make_location (loc, loc, parser->lexer);
27873 tree expr = finish_requires_expr (loc, parms, reqs);
27874 if (!processing_template_decl)
27875 {
27876 /* Perform semantic processing now to diagnose any invalid types and
27877 expressions. */
27878 int saved_errorcount = errorcount;
27879 tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
27880 if (errorcount > saved_errorcount)
27881 return error_mark_node;
27882 }
27883 return expr;
27884 }
27885
27886 /* Parse a parameterized requirement.
27887
27888 requirement-parameter-list:
27889 '(' parameter-declaration-clause ')' */
27890
27891 static tree
27892 cp_parser_requirement_parameter_list (cp_parser *parser)
27893 {
27894 matching_parens parens;
27895 if (!parens.require_open (parser))
27896 return error_mark_node;
27897
27898 tree parms = (cp_parser_parameter_declaration_clause
27899 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
27900
27901 if (!parens.require_close (parser))
27902 return error_mark_node;
27903
27904 return parms;
27905 }
27906
27907 /* Parse the body of a requirement.
27908
27909 requirement-body:
27910 '{' requirement-list '}' */
27911 static tree
27912 cp_parser_requirement_body (cp_parser *parser)
27913 {
27914 matching_braces braces;
27915 if (!braces.require_open (parser))
27916 return error_mark_node;
27917
27918 tree reqs = cp_parser_requirement_seq (parser);
27919
27920 if (!braces.require_close (parser))
27921 return error_mark_node;
27922
27923 return reqs;
27924 }
27925
27926 /* Parse a sequence of requirements.
27927
27928 requirement-seq:
27929 requirement
27930 requirement-seq requirement */
27931
27932 static tree
27933 cp_parser_requirement_seq (cp_parser *parser)
27934 {
27935 tree result = NULL_TREE;
27936 do
27937 {
27938 tree req = cp_parser_requirement (parser);
27939 if (req != error_mark_node)
27940 result = tree_cons (NULL_TREE, req, result);
27941 } while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE));
27942
27943 /* If there are no valid requirements, this is not a valid expression. */
27944 if (!result)
27945 return error_mark_node;
27946
27947 /* Reverse the order of requirements so they are analyzed in order. */
27948 return nreverse (result);
27949 }
27950
27951 /* Parse a syntactic requirement or type requirement.
27952
27953 requirement:
27954 simple-requirement
27955 compound-requirement
27956 type-requirement
27957 nested-requirement */
27958
27959 static tree
27960 cp_parser_requirement (cp_parser *parser)
27961 {
27962 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27963 return cp_parser_compound_requirement (parser);
27964 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
27965 return cp_parser_type_requirement (parser);
27966 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
27967 return cp_parser_nested_requirement (parser);
27968 else
27969 return cp_parser_simple_requirement (parser);
27970 }
27971
27972 /* Parse a simple requirement.
27973
27974 simple-requirement:
27975 expression ';' */
27976
27977 static tree
27978 cp_parser_simple_requirement (cp_parser *parser)
27979 {
27980 location_t start = cp_lexer_peek_token (parser->lexer)->location;
27981 cp_expr expr = cp_parser_expression (parser, NULL, false, false);
27982 if (expr == error_mark_node)
27983 cp_parser_skip_to_end_of_statement (parser);
27984
27985 cp_parser_consume_semicolon_at_end_of_statement (parser);
27986
27987 if (!expr || expr == error_mark_node)
27988 return error_mark_node;
27989
27990 /* Sometimes we don't get locations, so use the cached token location
27991 as a reasonable approximation. */
27992 if (expr.get_location() == UNKNOWN_LOCATION)
27993 expr.set_location (start);
27994
27995 return finish_simple_requirement (expr.get_location (), expr);
27996 }
27997
27998 /* Parse a type requirement
27999
28000 type-requirement
28001 nested-name-specifier [opt] required-type-name ';'
28002
28003 required-type-name:
28004 type-name
28005 'template' [opt] simple-template-id */
28006
28007 static tree
28008 cp_parser_type_requirement (cp_parser *parser)
28009 {
28010 cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
28011 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28012
28013 // Save the scope before parsing name specifiers.
28014 tree saved_scope = parser->scope;
28015 tree saved_object_scope = parser->object_scope;
28016 tree saved_qualifying_scope = parser->qualifying_scope;
28017 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
28018 cp_parser_nested_name_specifier_opt (parser,
28019 /*typename_keyword_p=*/true,
28020 /*check_dependency_p=*/false,
28021 /*type_p=*/true,
28022 /*is_declaration=*/false);
28023
28024 tree type;
28025 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28026 {
28027 cp_lexer_consume_token (parser->lexer);
28028 type = cp_parser_template_id (parser,
28029 /*template_keyword_p=*/true,
28030 /*check_dependency=*/false,
28031 /*tag_type=*/none_type,
28032 /*is_declaration=*/false);
28033 type = make_typename_type (parser->scope, type, typename_type,
28034 /*complain=*/tf_error);
28035 }
28036 else
28037 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
28038
28039 if (TREE_CODE (type) == TYPE_DECL)
28040 type = TREE_TYPE (type);
28041
28042 parser->scope = saved_scope;
28043 parser->object_scope = saved_object_scope;
28044 parser->qualifying_scope = saved_qualifying_scope;
28045
28046 if (type == error_mark_node)
28047 cp_parser_skip_to_end_of_statement (parser);
28048
28049 cp_parser_consume_semicolon_at_end_of_statement (parser);
28050
28051 if (type == error_mark_node)
28052 return error_mark_node;
28053
28054 loc = make_location (loc, start_tok->location, parser->lexer);
28055 return finish_type_requirement (loc, type);
28056 }
28057
28058 /* Parse a compound requirement
28059
28060 compound-requirement:
28061 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
28062
28063 static tree
28064 cp_parser_compound_requirement (cp_parser *parser)
28065 {
28066 /* Parse an expression enclosed in '{ }'s. */
28067 matching_braces braces;
28068 if (!braces.require_open (parser))
28069 return error_mark_node;
28070
28071 cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
28072
28073 tree expr = cp_parser_expression (parser, NULL, false, false);
28074 if (expr == error_mark_node)
28075 cp_parser_skip_to_closing_brace (parser);
28076
28077 if (!braces.require_close (parser))
28078 {
28079 cp_parser_skip_to_end_of_statement (parser);
28080 cp_parser_consume_semicolon_at_end_of_statement (parser);
28081 return error_mark_node;
28082 }
28083
28084 /* If the expression was invalid, skip the remainder of the requirement. */
28085 if (!expr || expr == error_mark_node)
28086 {
28087 cp_parser_skip_to_end_of_statement (parser);
28088 cp_parser_consume_semicolon_at_end_of_statement (parser);
28089 return error_mark_node;
28090 }
28091
28092 /* Parse the optional noexcept. */
28093 bool noexcept_p = false;
28094 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
28095 {
28096 cp_lexer_consume_token (parser->lexer);
28097 noexcept_p = true;
28098 }
28099
28100 /* Parse the optional trailing return type. */
28101 tree type = NULL_TREE;
28102 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
28103 {
28104 cp_lexer_consume_token (parser->lexer);
28105 cp_token *tok = cp_lexer_peek_token (parser->lexer);
28106
28107 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
28108 parser->in_result_type_constraint_p = true;
28109 /* C++20 allows either a type-id or a type-constraint. Parsing
28110 a type-id will subsume the parsing for a type-constraint but
28111 allow for more syntactic forms (e.g., const C<T>*). */
28112 type = cp_parser_trailing_type_id (parser);
28113 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
28114 if (type == error_mark_node)
28115 return error_mark_node;
28116
28117 location_t type_loc = make_location (tok->location, tok->location,
28118 parser->lexer);
28119
28120 /* Check that we haven't written something like 'const C<T>*'. */
28121 if (type_uses_auto (type))
28122 {
28123 if (!is_auto (type))
28124 {
28125 error_at (type_loc,
28126 "result type is not a plain type-constraint");
28127 cp_parser_consume_semicolon_at_end_of_statement (parser);
28128 return error_mark_node;
28129 }
28130 }
28131 else if (!flag_concepts_ts)
28132 /* P1452R2 removed the trailing-return-type option. */
28133 error_at (type_loc,
28134 "return-type-requirement is not a type-constraint");
28135 }
28136
28137 location_t loc = make_location (expr_token->location,
28138 braces.open_location (),
28139 parser->lexer);
28140
28141 cp_parser_consume_semicolon_at_end_of_statement (parser);
28142
28143 if (expr == error_mark_node || type == error_mark_node)
28144 return error_mark_node;
28145
28146 return finish_compound_requirement (loc, expr, type, noexcept_p);
28147 }
28148
28149 /* Parse a nested requirement. This is the same as a requires clause.
28150
28151 nested-requirement:
28152 requires-clause */
28153
28154 static tree
28155 cp_parser_nested_requirement (cp_parser *parser)
28156 {
28157 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
28158 cp_token *tok = cp_lexer_consume_token (parser->lexer);
28159 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28160 tree req = cp_parser_constraint_expression (parser);
28161 if (req == error_mark_node)
28162 cp_parser_skip_to_end_of_statement (parser);
28163 loc = make_location (loc, tok->location, parser->lexer);
28164 cp_parser_consume_semicolon_at_end_of_statement (parser);
28165 if (req == error_mark_node)
28166 return error_mark_node;
28167 return finish_nested_requirement (loc, req);
28168 }
28169
28170 /* Support Functions */
28171
28172 /* Return the appropriate prefer_type argument for lookup_name_real based on
28173 tag_type and template_mem_access. */
28174
28175 static inline int
28176 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
28177 {
28178 /* DR 141: When looking in the current enclosing context for a template-name
28179 after -> or ., only consider class templates. */
28180 if (template_mem_access)
28181 return 2;
28182 switch (tag_type)
28183 {
28184 case none_type: return 0; // No preference.
28185 case scope_type: return 1; // Type or namespace.
28186 default: return 2; // Type only.
28187 }
28188 }
28189
28190 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
28191 NAME should have one of the representations used for an
28192 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
28193 is returned. If PARSER->SCOPE is a dependent type, then a
28194 SCOPE_REF is returned.
28195
28196 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
28197 returned; the name was already resolved when the TEMPLATE_ID_EXPR
28198 was formed. Abstractly, such entities should not be passed to this
28199 function, because they do not need to be looked up, but it is
28200 simpler to check for this special case here, rather than at the
28201 call-sites.
28202
28203 In cases not explicitly covered above, this function returns a
28204 DECL, OVERLOAD, or baselink representing the result of the lookup.
28205 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
28206 is returned.
28207
28208 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
28209 (e.g., "struct") that was used. In that case bindings that do not
28210 refer to types are ignored.
28211
28212 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
28213 ignored.
28214
28215 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
28216 are ignored.
28217
28218 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
28219 types.
28220
28221 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
28222 TREE_LIST of candidates if name-lookup results in an ambiguity, and
28223 NULL_TREE otherwise. */
28224
28225 static cp_expr
28226 cp_parser_lookup_name (cp_parser *parser, tree name,
28227 enum tag_types tag_type,
28228 bool is_template,
28229 bool is_namespace,
28230 bool check_dependency,
28231 tree *ambiguous_decls,
28232 location_t name_location)
28233 {
28234 tree decl;
28235 tree object_type = parser->context->object_type;
28236
28237 /* Assume that the lookup will be unambiguous. */
28238 if (ambiguous_decls)
28239 *ambiguous_decls = NULL_TREE;
28240
28241 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
28242 no longer valid. Note that if we are parsing tentatively, and
28243 the parse fails, OBJECT_TYPE will be automatically restored. */
28244 parser->context->object_type = NULL_TREE;
28245
28246 if (name == error_mark_node)
28247 return error_mark_node;
28248
28249 /* A template-id has already been resolved; there is no lookup to
28250 do. */
28251 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
28252 return name;
28253 if (BASELINK_P (name))
28254 {
28255 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
28256 == TEMPLATE_ID_EXPR);
28257 return name;
28258 }
28259
28260 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
28261 it should already have been checked to make sure that the name
28262 used matches the type being destroyed. */
28263 if (TREE_CODE (name) == BIT_NOT_EXPR)
28264 {
28265 tree type;
28266
28267 /* Figure out to which type this destructor applies. */
28268 if (parser->scope)
28269 type = parser->scope;
28270 else if (object_type)
28271 type = object_type;
28272 else
28273 type = current_class_type;
28274 /* If that's not a class type, there is no destructor. */
28275 if (!type || !CLASS_TYPE_P (type))
28276 return error_mark_node;
28277
28278 /* In a non-static member function, check implicit this->. */
28279 if (current_class_ref)
28280 return lookup_destructor (current_class_ref, parser->scope, name,
28281 tf_warning_or_error);
28282
28283 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
28284 lazily_declare_fn (sfk_destructor, type);
28285
28286 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
28287 return dtor;
28288
28289 return error_mark_node;
28290 }
28291
28292 /* By this point, the NAME should be an ordinary identifier. If
28293 the id-expression was a qualified name, the qualifying scope is
28294 stored in PARSER->SCOPE at this point. */
28295 gcc_assert (identifier_p (name));
28296
28297 /* Perform the lookup. */
28298 if (parser->scope)
28299 {
28300 bool dependent_p;
28301
28302 if (parser->scope == error_mark_node)
28303 return error_mark_node;
28304
28305 /* If the SCOPE is dependent, the lookup must be deferred until
28306 the template is instantiated -- unless we are explicitly
28307 looking up names in uninstantiated templates. Even then, we
28308 cannot look up the name if the scope is not a class type; it
28309 might, for example, be a template type parameter. */
28310 dependent_p = (TYPE_P (parser->scope)
28311 && dependent_scope_p (parser->scope));
28312 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
28313 && dependent_p)
28314 /* Defer lookup. */
28315 decl = error_mark_node;
28316 else
28317 {
28318 tree pushed_scope = NULL_TREE;
28319
28320 /* If PARSER->SCOPE is a dependent type, then it must be a
28321 class type, and we must not be checking dependencies;
28322 otherwise, we would have processed this lookup above. So
28323 that PARSER->SCOPE is not considered a dependent base by
28324 lookup_member, we must enter the scope here. */
28325 if (dependent_p)
28326 pushed_scope = push_scope (parser->scope);
28327
28328 /* If the PARSER->SCOPE is a template specialization, it
28329 may be instantiated during name lookup. In that case,
28330 errors may be issued. Even if we rollback the current
28331 tentative parse, those errors are valid. */
28332 decl = lookup_qualified_name (parser->scope, name,
28333 prefer_type_arg (tag_type),
28334 /*complain=*/true);
28335
28336 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
28337 lookup result and the nested-name-specifier nominates a class C:
28338 * if the name specified after the nested-name-specifier, when
28339 looked up in C, is the injected-class-name of C (Clause 9), or
28340 * if the name specified after the nested-name-specifier is the
28341 same as the identifier or the simple-template-id's template-
28342 name in the last component of the nested-name-specifier,
28343 the name is instead considered to name the constructor of
28344 class C. [ Note: for example, the constructor is not an
28345 acceptable lookup result in an elaborated-type-specifier so
28346 the constructor would not be used in place of the
28347 injected-class-name. --end note ] Such a constructor name
28348 shall be used only in the declarator-id of a declaration that
28349 names a constructor or in a using-declaration. */
28350 if (tag_type == none_type
28351 && DECL_SELF_REFERENCE_P (decl)
28352 && same_type_p (DECL_CONTEXT (decl), parser->scope))
28353 decl = lookup_qualified_name (parser->scope, ctor_identifier,
28354 prefer_type_arg (tag_type),
28355 /*complain=*/true);
28356
28357 /* If we have a single function from a using decl, pull it out. */
28358 if (TREE_CODE (decl) == OVERLOAD
28359 && !really_overloaded_fn (decl))
28360 decl = OVL_FUNCTION (decl);
28361
28362 if (pushed_scope)
28363 pop_scope (pushed_scope);
28364 }
28365
28366 /* If the scope is a dependent type and either we deferred lookup or
28367 we did lookup but didn't find the name, rememeber the name. */
28368 if (decl == error_mark_node && TYPE_P (parser->scope)
28369 && dependent_type_p (parser->scope))
28370 {
28371 if (tag_type)
28372 {
28373 tree type;
28374
28375 /* The resolution to Core Issue 180 says that `struct
28376 A::B' should be considered a type-name, even if `A'
28377 is dependent. */
28378 type = make_typename_type (parser->scope, name, tag_type,
28379 /*complain=*/tf_error);
28380 if (type != error_mark_node)
28381 decl = TYPE_NAME (type);
28382 }
28383 else if (is_template
28384 && (cp_parser_next_token_ends_template_argument_p (parser)
28385 || cp_lexer_next_token_is (parser->lexer,
28386 CPP_CLOSE_PAREN)))
28387 decl = make_unbound_class_template (parser->scope,
28388 name, NULL_TREE,
28389 /*complain=*/tf_error);
28390 else
28391 decl = build_qualified_name (/*type=*/NULL_TREE,
28392 parser->scope, name,
28393 is_template);
28394 }
28395 parser->qualifying_scope = parser->scope;
28396 parser->object_scope = NULL_TREE;
28397 }
28398 else if (object_type)
28399 {
28400 /* Look up the name in the scope of the OBJECT_TYPE, unless the
28401 OBJECT_TYPE is not a class. */
28402 if (CLASS_TYPE_P (object_type))
28403 /* If the OBJECT_TYPE is a template specialization, it may
28404 be instantiated during name lookup. In that case, errors
28405 may be issued. Even if we rollback the current tentative
28406 parse, those errors are valid. */
28407 decl = lookup_member (object_type,
28408 name,
28409 /*protect=*/0,
28410 prefer_type_arg (tag_type),
28411 tf_warning_or_error);
28412 else
28413 decl = NULL_TREE;
28414
28415 if (!decl)
28416 /* Look it up in the enclosing context. DR 141: When looking for a
28417 template-name after -> or ., only consider class templates. */
28418 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
28419 /*nonclass=*/0,
28420 /*block_p=*/true, is_namespace, 0);
28421 parser->object_scope = object_type;
28422 parser->qualifying_scope = NULL_TREE;
28423 }
28424 else
28425 {
28426 decl = lookup_name_real (name, prefer_type_arg (tag_type),
28427 /*nonclass=*/0,
28428 /*block_p=*/true, is_namespace, 0);
28429 parser->qualifying_scope = NULL_TREE;
28430 parser->object_scope = NULL_TREE;
28431 }
28432
28433 /* If the lookup failed, let our caller know. */
28434 if (!decl || decl == error_mark_node)
28435 return error_mark_node;
28436
28437 /* Pull out the template from an injected-class-name (or multiple). */
28438 if (is_template)
28439 decl = maybe_get_template_decl_from_type_decl (decl);
28440
28441 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
28442 if (TREE_CODE (decl) == TREE_LIST)
28443 {
28444 if (ambiguous_decls)
28445 *ambiguous_decls = decl;
28446 /* The error message we have to print is too complicated for
28447 cp_parser_error, so we incorporate its actions directly. */
28448 if (!cp_parser_simulate_error (parser))
28449 {
28450 error_at (name_location, "reference to %qD is ambiguous",
28451 name);
28452 print_candidates (decl);
28453 }
28454 return error_mark_node;
28455 }
28456
28457 gcc_assert (DECL_P (decl)
28458 || TREE_CODE (decl) == OVERLOAD
28459 || TREE_CODE (decl) == SCOPE_REF
28460 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
28461 || BASELINK_P (decl));
28462
28463 /* If we have resolved the name of a member declaration, check to
28464 see if the declaration is accessible. When the name resolves to
28465 set of overloaded functions, accessibility is checked when
28466 overload resolution is done.
28467
28468 During an explicit instantiation, access is not checked at all,
28469 as per [temp.explicit]. */
28470 if (DECL_P (decl))
28471 check_accessibility_of_qualified_id (decl, object_type, parser->scope,
28472 tf_warning_or_error);
28473
28474 maybe_record_typedef_use (decl);
28475
28476 return cp_expr (decl, name_location);
28477 }
28478
28479 /* Like cp_parser_lookup_name, but for use in the typical case where
28480 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
28481 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
28482
28483 static tree
28484 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
28485 {
28486 return cp_parser_lookup_name (parser, name,
28487 none_type,
28488 /*is_template=*/false,
28489 /*is_namespace=*/false,
28490 /*check_dependency=*/true,
28491 /*ambiguous_decls=*/NULL,
28492 location);
28493 }
28494
28495 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
28496 the current context, return the TYPE_DECL. If TAG_NAME_P is
28497 true, the DECL indicates the class being defined in a class-head,
28498 or declared in an elaborated-type-specifier.
28499
28500 Otherwise, return DECL. */
28501
28502 static tree
28503 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
28504 {
28505 /* If the TEMPLATE_DECL is being declared as part of a class-head,
28506 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
28507
28508 struct A {
28509 template <typename T> struct B;
28510 };
28511
28512 template <typename T> struct A::B {};
28513
28514 Similarly, in an elaborated-type-specifier:
28515
28516 namespace N { struct X{}; }
28517
28518 struct A {
28519 template <typename T> friend struct N::X;
28520 };
28521
28522 However, if the DECL refers to a class type, and we are in
28523 the scope of the class, then the name lookup automatically
28524 finds the TYPE_DECL created by build_self_reference rather
28525 than a TEMPLATE_DECL. For example, in:
28526
28527 template <class T> struct S {
28528 S s;
28529 };
28530
28531 there is no need to handle such case. */
28532
28533 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
28534 return DECL_TEMPLATE_RESULT (decl);
28535
28536 return decl;
28537 }
28538
28539 /* If too many, or too few, template-parameter lists apply to the
28540 declarator, issue an error message. Returns TRUE if all went well,
28541 and FALSE otherwise. */
28542
28543 static bool
28544 cp_parser_check_declarator_template_parameters (cp_parser* parser,
28545 cp_declarator *declarator,
28546 location_t declarator_location)
28547 {
28548 switch (declarator->kind)
28549 {
28550 case cdk_id:
28551 {
28552 unsigned num_templates = 0;
28553 tree scope = declarator->u.id.qualifying_scope;
28554 bool template_id_p = false;
28555
28556 if (scope)
28557 num_templates = num_template_headers_for_class (scope);
28558 else if (TREE_CODE (declarator->u.id.unqualified_name)
28559 == TEMPLATE_ID_EXPR)
28560 {
28561 /* If the DECLARATOR has the form `X<y>' then it uses one
28562 additional level of template parameters. */
28563 ++num_templates;
28564 template_id_p = true;
28565 }
28566
28567 return cp_parser_check_template_parameters
28568 (parser, num_templates, template_id_p, declarator_location,
28569 declarator);
28570 }
28571
28572 case cdk_function:
28573 case cdk_array:
28574 case cdk_pointer:
28575 case cdk_reference:
28576 case cdk_ptrmem:
28577 return (cp_parser_check_declarator_template_parameters
28578 (parser, declarator->declarator, declarator_location));
28579
28580 case cdk_decomp:
28581 case cdk_error:
28582 return true;
28583
28584 default:
28585 gcc_unreachable ();
28586 }
28587 return false;
28588 }
28589
28590 /* NUM_TEMPLATES were used in the current declaration. If that is
28591 invalid, return FALSE and issue an error messages. Otherwise,
28592 return TRUE. If DECLARATOR is non-NULL, then we are checking a
28593 declarator and we can print more accurate diagnostics. */
28594
28595 static bool
28596 cp_parser_check_template_parameters (cp_parser* parser,
28597 unsigned num_templates,
28598 bool template_id_p,
28599 location_t location,
28600 cp_declarator *declarator)
28601 {
28602 /* If there are the same number of template classes and parameter
28603 lists, that's OK. */
28604 if (parser->num_template_parameter_lists == num_templates)
28605 return true;
28606 /* If there are more, but only one more, and the name ends in an identifier,
28607 then we are declaring a primary template. That's OK too. */
28608 if (!template_id_p
28609 && parser->num_template_parameter_lists == num_templates + 1)
28610 return true;
28611
28612 if (cp_parser_simulate_error (parser))
28613 return false;
28614
28615 /* If there are more template classes than parameter lists, we have
28616 something like:
28617
28618 template <class T> void S<T>::R<T>::f (); */
28619 if (parser->num_template_parameter_lists < num_templates)
28620 {
28621 if (declarator && !current_function_decl)
28622 error_at (location, "specializing member %<%T::%E%> "
28623 "requires %<template<>%> syntax",
28624 declarator->u.id.qualifying_scope,
28625 declarator->u.id.unqualified_name);
28626 else if (declarator)
28627 error_at (location, "invalid declaration of %<%T::%E%>",
28628 declarator->u.id.qualifying_scope,
28629 declarator->u.id.unqualified_name);
28630 else
28631 error_at (location, "too few template-parameter-lists");
28632 return false;
28633 }
28634 /* Otherwise, there are too many template parameter lists. We have
28635 something like:
28636
28637 template <class T> template <class U> void S::f(); */
28638 error_at (location, "too many template-parameter-lists");
28639 return false;
28640 }
28641
28642 /* Parse an optional `::' token indicating that the following name is
28643 from the global namespace. If so, PARSER->SCOPE is set to the
28644 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
28645 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
28646 Returns the new value of PARSER->SCOPE, if the `::' token is
28647 present, and NULL_TREE otherwise. */
28648
28649 static tree
28650 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
28651 {
28652 cp_token *token;
28653
28654 /* Peek at the next token. */
28655 token = cp_lexer_peek_token (parser->lexer);
28656 /* If we're looking at a `::' token then we're starting from the
28657 global namespace, not our current location. */
28658 if (token->type == CPP_SCOPE)
28659 {
28660 /* Consume the `::' token. */
28661 cp_lexer_consume_token (parser->lexer);
28662 /* Set the SCOPE so that we know where to start the lookup. */
28663 parser->scope = global_namespace;
28664 parser->qualifying_scope = global_namespace;
28665 parser->object_scope = NULL_TREE;
28666
28667 return parser->scope;
28668 }
28669 else if (!current_scope_valid_p)
28670 {
28671 parser->scope = NULL_TREE;
28672 parser->qualifying_scope = NULL_TREE;
28673 parser->object_scope = NULL_TREE;
28674 }
28675
28676 return NULL_TREE;
28677 }
28678
28679 /* Returns TRUE if the upcoming token sequence is the start of a
28680 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
28681 declarator is preceded by the `friend' specifier. The parser flags FLAGS
28682 is used to control type-specifier parsing. */
28683
28684 static bool
28685 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
28686 bool friend_p)
28687 {
28688 bool constructor_p;
28689 bool outside_class_specifier_p;
28690 tree nested_name_specifier;
28691 cp_token *next_token;
28692
28693 /* The common case is that this is not a constructor declarator, so
28694 try to avoid doing lots of work if at all possible. It's not
28695 valid declare a constructor at function scope. */
28696 if (parser->in_function_body)
28697 return false;
28698 /* And only certain tokens can begin a constructor declarator. */
28699 next_token = cp_lexer_peek_token (parser->lexer);
28700 if (next_token->type != CPP_NAME
28701 && next_token->type != CPP_SCOPE
28702 && next_token->type != CPP_NESTED_NAME_SPECIFIER
28703 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
28704 declarator-id of a constructor or destructor. */
28705 && (next_token->type != CPP_TEMPLATE_ID || cxx_dialect >= cxx20))
28706 return false;
28707
28708 /* Parse tentatively; we are going to roll back all of the tokens
28709 consumed here. */
28710 cp_parser_parse_tentatively (parser);
28711 /* Assume that we are looking at a constructor declarator. */
28712 constructor_p = true;
28713
28714 /* Look for the optional `::' operator. */
28715 cp_parser_global_scope_opt (parser,
28716 /*current_scope_valid_p=*/false);
28717 /* Look for the nested-name-specifier. */
28718 nested_name_specifier
28719 = (cp_parser_nested_name_specifier_opt (parser,
28720 /*typename_keyword_p=*/false,
28721 /*check_dependency_p=*/false,
28722 /*type_p=*/false,
28723 /*is_declaration=*/false));
28724
28725 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
28726 if (nested_name_specifier
28727 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
28728 {
28729 tree s = resolve_typename_type (nested_name_specifier,
28730 /*only_current_p=*/false);
28731 if (TREE_CODE (s) != TYPENAME_TYPE)
28732 nested_name_specifier = s;
28733 }
28734
28735 outside_class_specifier_p = (!at_class_scope_p ()
28736 || !TYPE_BEING_DEFINED (current_class_type)
28737 || friend_p);
28738
28739 /* Outside of a class-specifier, there must be a
28740 nested-name-specifier. Except in C++17 mode, where we
28741 might be declaring a guiding declaration. */
28742 if (!nested_name_specifier && outside_class_specifier_p
28743 && cxx_dialect < cxx17)
28744 constructor_p = false;
28745 else if (nested_name_specifier == error_mark_node)
28746 constructor_p = false;
28747
28748 /* If we have a class scope, this is easy; DR 147 says that S::S always
28749 names the constructor, and no other qualified name could. */
28750 if (constructor_p && nested_name_specifier
28751 && CLASS_TYPE_P (nested_name_specifier))
28752 {
28753 tree id = cp_parser_unqualified_id (parser,
28754 /*template_keyword_p=*/false,
28755 /*check_dependency_p=*/false,
28756 /*declarator_p=*/true,
28757 /*optional_p=*/false);
28758 if (is_overloaded_fn (id))
28759 id = DECL_NAME (get_first_fn (id));
28760 if (!constructor_name_p (id, nested_name_specifier))
28761 constructor_p = false;
28762 }
28763 /* If we still think that this might be a constructor-declarator,
28764 look for a class-name. */
28765 else if (constructor_p)
28766 {
28767 /* If we have:
28768
28769 template <typename T> struct S {
28770 S();
28771 };
28772
28773 we must recognize that the nested `S' names a class. */
28774 if (cxx_dialect >= cxx17)
28775 cp_parser_parse_tentatively (parser);
28776
28777 tree type_decl;
28778 type_decl = cp_parser_class_name (parser,
28779 /*typename_keyword_p=*/false,
28780 /*template_keyword_p=*/false,
28781 none_type,
28782 /*check_dependency_p=*/false,
28783 /*class_head_p=*/false,
28784 /*is_declaration=*/false);
28785
28786 if (cxx_dialect >= cxx17
28787 && !cp_parser_parse_definitely (parser))
28788 {
28789 type_decl = NULL_TREE;
28790 tree tmpl = cp_parser_template_name (parser,
28791 /*template_keyword*/false,
28792 /*check_dependency_p*/false,
28793 /*is_declaration*/false,
28794 none_type,
28795 /*is_identifier*/NULL);
28796 if (DECL_CLASS_TEMPLATE_P (tmpl)
28797 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28798 /* It's a deduction guide, return true. */;
28799 else
28800 cp_parser_simulate_error (parser);
28801 }
28802
28803 /* If there was no class-name, then this is not a constructor.
28804 Otherwise, if we are in a class-specifier and we aren't
28805 handling a friend declaration, check that its type matches
28806 current_class_type (c++/38313). Note: error_mark_node
28807 is left alone for error recovery purposes. */
28808 constructor_p = (!cp_parser_error_occurred (parser)
28809 && (outside_class_specifier_p
28810 || type_decl == NULL_TREE
28811 || type_decl == error_mark_node
28812 || same_type_p (current_class_type,
28813 TREE_TYPE (type_decl))));
28814
28815 /* If we're still considering a constructor, we have to see a `(',
28816 to begin the parameter-declaration-clause, followed by either a
28817 `)', an `...', or a decl-specifier. We need to check for a
28818 type-specifier to avoid being fooled into thinking that:
28819
28820 S (f) (int);
28821
28822 is a constructor. (It is actually a function named `f' that
28823 takes one parameter (of type `int') and returns a value of type
28824 `S'. */
28825 if (constructor_p
28826 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28827 constructor_p = false;
28828
28829 if (constructor_p
28830 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
28831 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
28832 /* A parameter declaration begins with a decl-specifier,
28833 which is either the "attribute" keyword, a storage class
28834 specifier, or (usually) a type-specifier. */
28835 && (!cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
28836 /* GNU attributes can actually appear both at the start of
28837 a parameter and parenthesized declarator.
28838 S (__attribute__((unused)) int);
28839 is a constructor, but
28840 S (__attribute__((unused)) foo) (int);
28841 is a function declaration. */
28842 || (cp_parser_allow_gnu_extensions_p (parser)
28843 && cp_next_tokens_can_be_gnu_attribute_p (parser)))
28844 /* A parameter declaration can also begin with [[attribute]]. */
28845 && !cp_next_tokens_can_be_std_attribute_p (parser))
28846 {
28847 tree type;
28848 tree pushed_scope = NULL_TREE;
28849 unsigned saved_num_template_parameter_lists;
28850
28851 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
28852 {
28853 unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
28854 while (--n)
28855 cp_lexer_consume_token (parser->lexer);
28856 }
28857
28858 /* Names appearing in the type-specifier should be looked up
28859 in the scope of the class. */
28860 if (current_class_type)
28861 type = NULL_TREE;
28862 else if (type_decl)
28863 {
28864 type = TREE_TYPE (type_decl);
28865 if (TREE_CODE (type) == TYPENAME_TYPE)
28866 {
28867 type = resolve_typename_type (type,
28868 /*only_current_p=*/false);
28869 if (TREE_CODE (type) == TYPENAME_TYPE)
28870 {
28871 cp_parser_abort_tentative_parse (parser);
28872 return false;
28873 }
28874 }
28875 pushed_scope = push_scope (type);
28876 }
28877
28878 /* Inside the constructor parameter list, surrounding
28879 template-parameter-lists do not apply. */
28880 saved_num_template_parameter_lists
28881 = parser->num_template_parameter_lists;
28882 parser->num_template_parameter_lists = 0;
28883
28884 /* Look for the type-specifier. It's not optional, but its typename
28885 might be. Unless this is a friend declaration; we don't want to
28886 treat
28887
28888 friend S (T::fn)(int);
28889
28890 as a constructor, but with P0634, we might assume a type when
28891 looking for the type-specifier. It is actually a function named
28892 `T::fn' that takes one parameter (of type `int') and returns a
28893 value of type `S'. Constructors can be friends, but they must
28894 use a qualified name.
28895
28896 Parse with an empty set of declaration specifiers since we're
28897 trying to match a decl-specifier-seq of the first parameter.
28898 This must be non-null so that cp_parser_simple_type_specifier
28899 will recognize a constrained placeholder type such as:
28900 'C<int> auto' where C is a type concept. */
28901 cp_decl_specifier_seq ctor_specs;
28902 clear_decl_specs (&ctor_specs);
28903 cp_parser_type_specifier (parser,
28904 (friend_p ? CP_PARSER_FLAGS_NONE
28905 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
28906 /*decl_specs=*/&ctor_specs,
28907 /*is_declarator=*/true,
28908 /*declares_class_or_enum=*/NULL,
28909 /*is_cv_qualifier=*/NULL);
28910
28911 parser->num_template_parameter_lists
28912 = saved_num_template_parameter_lists;
28913
28914 /* Leave the scope of the class. */
28915 if (pushed_scope)
28916 pop_scope (pushed_scope);
28917
28918 constructor_p = !cp_parser_error_occurred (parser);
28919 }
28920 }
28921
28922 /* We did not really want to consume any tokens. */
28923 cp_parser_abort_tentative_parse (parser);
28924
28925 return constructor_p;
28926 }
28927
28928 /* Parse the definition of the function given by the DECL_SPECIFIERS,
28929 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
28930 they must be performed once we are in the scope of the function.
28931
28932 Returns the function defined. */
28933
28934 static tree
28935 cp_parser_function_definition_from_specifiers_and_declarator
28936 (cp_parser* parser,
28937 cp_decl_specifier_seq *decl_specifiers,
28938 tree attributes,
28939 const cp_declarator *declarator)
28940 {
28941 tree fn;
28942 bool success_p;
28943
28944 /* Begin the function-definition. */
28945 success_p = start_function (decl_specifiers, declarator, attributes);
28946
28947 /* The things we're about to see are not directly qualified by any
28948 template headers we've seen thus far. */
28949 reset_specialization ();
28950
28951 /* If there were names looked up in the decl-specifier-seq that we
28952 did not check, check them now. We must wait until we are in the
28953 scope of the function to perform the checks, since the function
28954 might be a friend. */
28955 perform_deferred_access_checks (tf_warning_or_error);
28956
28957 if (success_p)
28958 {
28959 cp_finalize_omp_declare_simd (parser, current_function_decl);
28960 parser->omp_declare_simd = NULL;
28961 cp_finalize_oacc_routine (parser, current_function_decl, true);
28962 parser->oacc_routine = NULL;
28963 }
28964
28965 if (!success_p)
28966 {
28967 /* Skip the entire function. */
28968 cp_parser_skip_to_end_of_block_or_statement (parser);
28969 fn = error_mark_node;
28970 }
28971 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
28972 {
28973 /* Seen already, skip it. An error message has already been output. */
28974 cp_parser_skip_to_end_of_block_or_statement (parser);
28975 fn = current_function_decl;
28976 current_function_decl = NULL_TREE;
28977 /* If this is a function from a class, pop the nested class. */
28978 if (current_class_name)
28979 pop_nested_class ();
28980 }
28981 else
28982 {
28983 timevar_id_t tv;
28984 if (DECL_DECLARED_INLINE_P (current_function_decl))
28985 tv = TV_PARSE_INLINE;
28986 else
28987 tv = TV_PARSE_FUNC;
28988 timevar_push (tv);
28989 fn = cp_parser_function_definition_after_declarator (parser,
28990 /*inline_p=*/false);
28991 timevar_pop (tv);
28992 }
28993
28994 return fn;
28995 }
28996
28997 /* Parse the part of a function-definition that follows the
28998 declarator. INLINE_P is TRUE iff this function is an inline
28999 function defined within a class-specifier.
29000
29001 Returns the function defined. */
29002
29003 static tree
29004 cp_parser_function_definition_after_declarator (cp_parser* parser,
29005 bool inline_p)
29006 {
29007 tree fn;
29008 bool saved_in_unbraced_linkage_specification_p;
29009 bool saved_in_function_body;
29010 unsigned saved_num_template_parameter_lists;
29011 cp_token *token;
29012 bool fully_implicit_function_template_p
29013 = parser->fully_implicit_function_template_p;
29014 parser->fully_implicit_function_template_p = false;
29015 tree implicit_template_parms
29016 = parser->implicit_template_parms;
29017 parser->implicit_template_parms = 0;
29018 cp_binding_level* implicit_template_scope
29019 = parser->implicit_template_scope;
29020 parser->implicit_template_scope = 0;
29021
29022 saved_in_function_body = parser->in_function_body;
29023 parser->in_function_body = true;
29024 /* If the next token is `return', then the code may be trying to
29025 make use of the "named return value" extension that G++ used to
29026 support. */
29027 token = cp_lexer_peek_token (parser->lexer);
29028 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
29029 {
29030 /* Consume the `return' keyword. */
29031 cp_lexer_consume_token (parser->lexer);
29032 /* Look for the identifier that indicates what value is to be
29033 returned. */
29034 cp_parser_identifier (parser);
29035 /* Issue an error message. */
29036 error_at (token->location,
29037 "named return values are no longer supported");
29038 /* Skip tokens until we reach the start of the function body. */
29039 while (true)
29040 {
29041 cp_token *token = cp_lexer_peek_token (parser->lexer);
29042 if (token->type == CPP_OPEN_BRACE
29043 || token->type == CPP_EOF
29044 || token->type == CPP_PRAGMA_EOL)
29045 break;
29046 cp_lexer_consume_token (parser->lexer);
29047 }
29048 }
29049 /* The `extern' in `extern "C" void f () { ... }' does not apply to
29050 anything declared inside `f'. */
29051 saved_in_unbraced_linkage_specification_p
29052 = parser->in_unbraced_linkage_specification_p;
29053 parser->in_unbraced_linkage_specification_p = false;
29054 /* Inside the function, surrounding template-parameter-lists do not
29055 apply. */
29056 saved_num_template_parameter_lists
29057 = parser->num_template_parameter_lists;
29058 parser->num_template_parameter_lists = 0;
29059
29060 /* If the next token is `try', `__transaction_atomic', or
29061 `__transaction_relaxed`, then we are looking at either function-try-block
29062 or function-transaction-block. Note that all of these include the
29063 function-body. */
29064 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
29065 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
29066 else if (cp_lexer_next_token_is_keyword (parser->lexer,
29067 RID_TRANSACTION_RELAXED))
29068 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
29069 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
29070 cp_parser_function_try_block (parser);
29071 else
29072 cp_parser_ctor_initializer_opt_and_function_body
29073 (parser, /*in_function_try_block=*/false);
29074
29075 /* Finish the function. */
29076 fn = finish_function (inline_p);
29077 /* Generate code for it, if necessary. */
29078 expand_or_defer_fn (fn);
29079 /* Restore the saved values. */
29080 parser->in_unbraced_linkage_specification_p
29081 = saved_in_unbraced_linkage_specification_p;
29082 parser->num_template_parameter_lists
29083 = saved_num_template_parameter_lists;
29084 parser->in_function_body = saved_in_function_body;
29085
29086 parser->fully_implicit_function_template_p
29087 = fully_implicit_function_template_p;
29088 parser->implicit_template_parms
29089 = implicit_template_parms;
29090 parser->implicit_template_scope
29091 = implicit_template_scope;
29092
29093 if (parser->fully_implicit_function_template_p)
29094 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
29095
29096 return fn;
29097 }
29098
29099 /* Parse a template-declaration body (following argument list). */
29100
29101 static void
29102 cp_parser_template_declaration_after_parameters (cp_parser* parser,
29103 tree parameter_list,
29104 bool member_p)
29105 {
29106 tree decl = NULL_TREE;
29107 bool friend_p = false;
29108
29109 /* We just processed one more parameter list. */
29110 ++parser->num_template_parameter_lists;
29111
29112 /* Get the deferred access checks from the parameter list. These
29113 will be checked once we know what is being declared, as for a
29114 member template the checks must be performed in the scope of the
29115 class containing the member. */
29116 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
29117
29118 /* Tentatively parse for a new template parameter list, which can either be
29119 the template keyword or a template introduction. */
29120 if (cp_parser_template_declaration_after_export (parser, member_p))
29121 /* OK */;
29122 else if (cxx_dialect >= cxx11
29123 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
29124 decl = cp_parser_alias_declaration (parser);
29125 else if (cxx_dialect >= cxx20 /* Implies flag_concept. */
29126 && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
29127 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_BOOL))
29128 /* Allow 'concept bool' to be handled as per the TS. */
29129 decl = cp_parser_concept_definition (parser);
29130 else
29131 {
29132 cp_token *token = cp_lexer_peek_token (parser->lexer);
29133 decl = cp_parser_single_declaration (parser,
29134 checks,
29135 member_p,
29136 /*explicit_specialization_p=*/false,
29137 &friend_p);
29138
29139 /* If this is a member template declaration, let the front
29140 end know. */
29141 if (member_p && !friend_p && decl)
29142 {
29143 if (TREE_CODE (decl) == TYPE_DECL)
29144 cp_parser_check_access_in_redeclaration (decl, token->location);
29145
29146 decl = finish_member_template_decl (decl);
29147 }
29148 else if (friend_p && decl
29149 && DECL_DECLARES_TYPE_P (decl))
29150 make_friend_class (current_class_type, TREE_TYPE (decl),
29151 /*complain=*/true);
29152 }
29153 /* We are done with the current parameter list. */
29154 --parser->num_template_parameter_lists;
29155
29156 pop_deferring_access_checks ();
29157
29158 /* Finish up. */
29159 finish_template_decl (parameter_list);
29160
29161 /* Check the template arguments for a literal operator template. */
29162 if (decl
29163 && DECL_DECLARES_FUNCTION_P (decl)
29164 && UDLIT_OPER_P (DECL_NAME (decl)))
29165 {
29166 bool ok = true;
29167 if (parameter_list == NULL_TREE)
29168 ok = false;
29169 else
29170 {
29171 int num_parms = TREE_VEC_LENGTH (parameter_list);
29172 if (num_parms == 1)
29173 {
29174 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
29175 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
29176 if (TREE_CODE (parm) != PARM_DECL)
29177 ok = false;
29178 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
29179 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
29180 /* OK, C++20 string literal operator template. We don't need
29181 to warn in lower dialects here because we will have already
29182 warned about the template parameter. */;
29183 else if (TREE_TYPE (parm) != char_type_node
29184 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
29185 ok = false;
29186 }
29187 else if (num_parms == 2 && cxx_dialect >= cxx14)
29188 {
29189 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
29190 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
29191 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
29192 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
29193 if (TREE_CODE (parm) != PARM_DECL
29194 || TREE_TYPE (parm) != TREE_TYPE (type)
29195 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
29196 ok = false;
29197 else
29198 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
29199 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
29200 "ISO C++ did not adopt string literal operator templa"
29201 "tes taking an argument pack of characters");
29202 }
29203 else
29204 ok = false;
29205 }
29206 if (!ok)
29207 {
29208 if (cxx_dialect > cxx17)
29209 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
29210 "template %qD has invalid parameter list; expected "
29211 "non-type template parameter pack %<<char...>%> or "
29212 "single non-type parameter of class type",
29213 decl);
29214 else
29215 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
29216 "template %qD has invalid parameter list; expected "
29217 "non-type template parameter pack %<<char...>%>",
29218 decl);
29219 }
29220 }
29221
29222 /* Register member declarations. */
29223 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
29224 finish_member_declaration (decl);
29225 /* If DECL is a function template, we must return to parse it later.
29226 (Even though there is no definition, there might be default
29227 arguments that need handling.) */
29228 if (member_p && decl
29229 && DECL_DECLARES_FUNCTION_P (decl))
29230 vec_safe_push (unparsed_funs_with_definitions, decl);
29231 }
29232
29233 /* Parse a template introduction header for a template-declaration. Returns
29234 false if tentative parse fails. */
29235
29236 static bool
29237 cp_parser_template_introduction (cp_parser* parser, bool member_p)
29238 {
29239 cp_parser_parse_tentatively (parser);
29240
29241 tree saved_scope = parser->scope;
29242 tree saved_object_scope = parser->object_scope;
29243 tree saved_qualifying_scope = parser->qualifying_scope;
29244
29245 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
29246
29247 /* Look for the optional `::' operator. */
29248 cp_parser_global_scope_opt (parser,
29249 /*current_scope_valid_p=*/false);
29250 /* Look for the nested-name-specifier. */
29251 cp_parser_nested_name_specifier_opt (parser,
29252 /*typename_keyword_p=*/false,
29253 /*check_dependency_p=*/true,
29254 /*type_p=*/false,
29255 /*is_declaration=*/false);
29256
29257 cp_token *token = cp_lexer_peek_token (parser->lexer);
29258 tree concept_name = cp_parser_identifier (parser);
29259
29260 /* Look up the concept for which we will be matching
29261 template parameters. */
29262 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
29263 token->location);
29264 parser->scope = saved_scope;
29265 parser->object_scope = saved_object_scope;
29266 parser->qualifying_scope = saved_qualifying_scope;
29267
29268 if (concept_name == error_mark_node
29269 || (seen_error () && !concept_definition_p (tmpl_decl)))
29270 cp_parser_simulate_error (parser);
29271
29272 /* Look for opening brace for introduction. */
29273 matching_braces braces;
29274 braces.require_open (parser);
29275 location_t open_loc = input_location;
29276
29277 if (!cp_parser_parse_definitely (parser))
29278 return false;
29279
29280 push_deferring_access_checks (dk_deferred);
29281
29282 /* Build vector of placeholder parameters and grab
29283 matching identifiers. */
29284 tree introduction_list = cp_parser_introduction_list (parser);
29285
29286 /* Look for closing brace for introduction. */
29287 if (!braces.require_close (parser))
29288 return true;
29289
29290 /* The introduction-list shall not be empty. */
29291 int nargs = TREE_VEC_LENGTH (introduction_list);
29292 if (nargs == 0)
29293 {
29294 /* In cp_parser_introduction_list we have already issued an error. */
29295 return true;
29296 }
29297
29298 if (tmpl_decl == error_mark_node)
29299 {
29300 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
29301 token->location);
29302 return true;
29303 }
29304
29305 /* Build and associate the constraint. */
29306 location_t introduction_loc = make_location (open_loc,
29307 start_token->location,
29308 parser->lexer);
29309 tree parms = finish_template_introduction (tmpl_decl,
29310 introduction_list,
29311 introduction_loc);
29312 if (parms && parms != error_mark_node)
29313 {
29314 if (!flag_concepts_ts)
29315 pedwarn (introduction_loc, 0, "template-introductions"
29316 " are not part of C++20 concepts [-fconcepts-ts]");
29317
29318 cp_parser_template_declaration_after_parameters (parser, parms,
29319 member_p);
29320 return true;
29321 }
29322
29323 if (parms == NULL_TREE)
29324 error_at (token->location, "no matching concept for template-introduction");
29325
29326 return true;
29327 }
29328
29329 /* Parse a normal template-declaration following the template keyword. */
29330
29331 static void
29332 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
29333 {
29334 tree parameter_list;
29335 bool need_lang_pop;
29336 location_t location = input_location;
29337
29338 /* Look for the `<' token. */
29339 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
29340 return;
29341 if (at_class_scope_p () && current_function_decl)
29342 {
29343 /* 14.5.2.2 [temp.mem]
29344
29345 A local class shall not have member templates. */
29346 error_at (location,
29347 "invalid declaration of member template in local class");
29348 cp_parser_skip_to_end_of_block_or_statement (parser);
29349 return;
29350 }
29351 /* [temp]
29352
29353 A template ... shall not have C linkage. */
29354 if (current_lang_name == lang_name_c)
29355 {
29356 error_at (location, "template with C linkage");
29357 maybe_show_extern_c_location ();
29358 /* Give it C++ linkage to avoid confusing other parts of the
29359 front end. */
29360 push_lang_context (lang_name_cplusplus);
29361 need_lang_pop = true;
29362 }
29363 else
29364 need_lang_pop = false;
29365
29366 /* We cannot perform access checks on the template parameter
29367 declarations until we know what is being declared, just as we
29368 cannot check the decl-specifier list. */
29369 push_deferring_access_checks (dk_deferred);
29370
29371 /* If the next token is `>', then we have an invalid
29372 specialization. Rather than complain about an invalid template
29373 parameter, issue an error message here. */
29374 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
29375 {
29376 cp_parser_error (parser, "invalid explicit specialization");
29377 begin_specialization ();
29378 parameter_list = NULL_TREE;
29379 }
29380 else
29381 {
29382 /* Parse the template parameters. */
29383 parameter_list = cp_parser_template_parameter_list (parser);
29384 }
29385
29386 /* Look for the `>'. */
29387 cp_parser_skip_to_end_of_template_parameter_list (parser);
29388
29389 /* Manage template requirements */
29390 if (flag_concepts)
29391 {
29392 tree reqs = get_shorthand_constraints (current_template_parms);
29393 if (tree treqs = cp_parser_requires_clause_opt (parser, false))
29394 reqs = combine_constraint_expressions (reqs, treqs);
29395 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
29396 }
29397
29398 cp_parser_template_declaration_after_parameters (parser, parameter_list,
29399 member_p);
29400
29401 /* For the erroneous case of a template with C linkage, we pushed an
29402 implicit C++ linkage scope; exit that scope now. */
29403 if (need_lang_pop)
29404 pop_lang_context ();
29405 }
29406
29407 /* Parse a template-declaration, assuming that the `export' (and
29408 `extern') keywords, if present, has already been scanned. MEMBER_P
29409 is as for cp_parser_template_declaration. */
29410
29411 static bool
29412 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
29413 {
29414 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29415 {
29416 cp_lexer_consume_token (parser->lexer);
29417 cp_parser_explicit_template_declaration (parser, member_p);
29418 return true;
29419 }
29420 else if (flag_concepts)
29421 return cp_parser_template_introduction (parser, member_p);
29422
29423 return false;
29424 }
29425
29426 /* Perform the deferred access checks from a template-parameter-list.
29427 CHECKS is a TREE_LIST of access checks, as returned by
29428 get_deferred_access_checks. */
29429
29430 static void
29431 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
29432 {
29433 ++processing_template_parmlist;
29434 perform_access_checks (checks, tf_warning_or_error);
29435 --processing_template_parmlist;
29436 }
29437
29438 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
29439 `function-definition' sequence that follows a template header.
29440 If MEMBER_P is true, this declaration appears in a class scope.
29441
29442 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
29443 *FRIEND_P is set to TRUE iff the declaration is a friend. */
29444
29445 static tree
29446 cp_parser_single_declaration (cp_parser* parser,
29447 vec<deferred_access_check, va_gc> *checks,
29448 bool member_p,
29449 bool explicit_specialization_p,
29450 bool* friend_p)
29451 {
29452 int declares_class_or_enum;
29453 tree decl = NULL_TREE;
29454 cp_decl_specifier_seq decl_specifiers;
29455 bool function_definition_p = false;
29456 cp_token *decl_spec_token_start;
29457
29458 /* This function is only used when processing a template
29459 declaration. */
29460 gcc_assert (innermost_scope_kind () == sk_template_parms
29461 || innermost_scope_kind () == sk_template_spec);
29462
29463 /* Defer access checks until we know what is being declared. */
29464 push_deferring_access_checks (dk_deferred);
29465
29466 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
29467 alternative. */
29468 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
29469 cp_parser_decl_specifier_seq (parser,
29470 (CP_PARSER_FLAGS_OPTIONAL
29471 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
29472 &decl_specifiers,
29473 &declares_class_or_enum);
29474 if (friend_p)
29475 *friend_p = cp_parser_friend_p (&decl_specifiers);
29476
29477 /* There are no template typedefs. */
29478 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
29479 {
29480 error_at (decl_spec_token_start->location,
29481 "template declaration of %<typedef%>");
29482 decl = error_mark_node;
29483 }
29484
29485 /* Gather up the access checks that occurred the
29486 decl-specifier-seq. */
29487 stop_deferring_access_checks ();
29488
29489 /* Check for the declaration of a template class. */
29490 if (declares_class_or_enum)
29491 {
29492 if (cp_parser_declares_only_class_p (parser)
29493 || (declares_class_or_enum & 2))
29494 {
29495 /* If this is a declaration, but not a definition, associate
29496 any constraints with the type declaration. Constraints
29497 are associated with definitions in cp_parser_class_specifier. */
29498 if (declares_class_or_enum == 1)
29499 associate_classtype_constraints (decl_specifiers.type);
29500
29501 decl = shadow_tag (&decl_specifiers);
29502
29503 /* In this case:
29504
29505 struct C {
29506 friend template <typename T> struct A<T>::B;
29507 };
29508
29509 A<T>::B will be represented by a TYPENAME_TYPE, and
29510 therefore not recognized by shadow_tag. */
29511 if (friend_p && *friend_p
29512 && !decl
29513 && decl_specifiers.type
29514 && TYPE_P (decl_specifiers.type))
29515 decl = decl_specifiers.type;
29516
29517 if (decl && decl != error_mark_node)
29518 decl = TYPE_NAME (decl);
29519 else
29520 decl = error_mark_node;
29521
29522 /* Perform access checks for template parameters. */
29523 cp_parser_perform_template_parameter_access_checks (checks);
29524
29525 /* Give a helpful diagnostic for
29526 template <class T> struct A { } a;
29527 if we aren't already recovering from an error. */
29528 if (!cp_parser_declares_only_class_p (parser)
29529 && !seen_error ())
29530 {
29531 error_at (cp_lexer_peek_token (parser->lexer)->location,
29532 "a class template declaration must not declare "
29533 "anything else");
29534 cp_parser_skip_to_end_of_block_or_statement (parser);
29535 goto out;
29536 }
29537 }
29538 }
29539
29540 /* Complain about missing 'typename' or other invalid type names. */
29541 if (!decl_specifiers.any_type_specifiers_p
29542 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
29543 {
29544 /* cp_parser_parse_and_diagnose_invalid_type_name calls
29545 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
29546 the rest of this declaration. */
29547 decl = error_mark_node;
29548 goto out;
29549 }
29550
29551 /* If it's not a template class, try for a template function. If
29552 the next token is a `;', then this declaration does not declare
29553 anything. But, if there were errors in the decl-specifiers, then
29554 the error might well have come from an attempted class-specifier.
29555 In that case, there's no need to warn about a missing declarator. */
29556 if (!decl
29557 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
29558 || decl_specifiers.type != error_mark_node))
29559 {
29560 decl = cp_parser_init_declarator (parser,
29561 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
29562 &decl_specifiers,
29563 checks,
29564 /*function_definition_allowed_p=*/true,
29565 member_p,
29566 declares_class_or_enum,
29567 &function_definition_p,
29568 NULL, NULL, NULL);
29569
29570 /* 7.1.1-1 [dcl.stc]
29571
29572 A storage-class-specifier shall not be specified in an explicit
29573 specialization... */
29574 if (decl
29575 && explicit_specialization_p
29576 && decl_specifiers.storage_class != sc_none)
29577 {
29578 error_at (decl_spec_token_start->location,
29579 "explicit template specialization cannot have a storage class");
29580 decl = error_mark_node;
29581 }
29582
29583 if (decl && VAR_P (decl))
29584 check_template_variable (decl);
29585 }
29586
29587 /* Look for a trailing `;' after the declaration. */
29588 if (!function_definition_p
29589 && (decl == error_mark_node
29590 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
29591 cp_parser_skip_to_end_of_block_or_statement (parser);
29592
29593 out:
29594 pop_deferring_access_checks ();
29595
29596 /* Clear any current qualification; whatever comes next is the start
29597 of something new. */
29598 parser->scope = NULL_TREE;
29599 parser->qualifying_scope = NULL_TREE;
29600 parser->object_scope = NULL_TREE;
29601
29602 return decl;
29603 }
29604
29605 /* Parse a cast-expression that is not the operand of a unary "&". */
29606
29607 static cp_expr
29608 cp_parser_simple_cast_expression (cp_parser *parser)
29609 {
29610 return cp_parser_cast_expression (parser, /*address_p=*/false,
29611 /*cast_p=*/false, /*decltype*/false, NULL);
29612 }
29613
29614 /* Parse a functional cast to TYPE. Returns an expression
29615 representing the cast. */
29616
29617 static cp_expr
29618 cp_parser_functional_cast (cp_parser* parser, tree type)
29619 {
29620 vec<tree, va_gc> *vec;
29621 tree expression_list;
29622 cp_expr cast;
29623 bool nonconst_p;
29624
29625 location_t start_loc = input_location;
29626
29627 if (!type)
29628 type = error_mark_node;
29629
29630 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29631 {
29632 cp_lexer_set_source_position (parser->lexer);
29633 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
29634 expression_list = cp_parser_braced_list (parser, &nonconst_p);
29635 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
29636 if (TREE_CODE (type) == TYPE_DECL)
29637 type = TREE_TYPE (type);
29638
29639 cast = finish_compound_literal (type, expression_list,
29640 tf_warning_or_error, fcl_functional);
29641 /* Create a location of the form:
29642 type_name{i, f}
29643 ^~~~~~~~~~~~~~~
29644 with caret == start at the start of the type name,
29645 finishing at the closing brace. */
29646 location_t combined_loc = make_location (start_loc, start_loc,
29647 parser->lexer);
29648 cast.set_location (combined_loc);
29649 return cast;
29650 }
29651
29652
29653 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
29654 /*cast_p=*/true,
29655 /*allow_expansion_p=*/true,
29656 /*non_constant_p=*/NULL);
29657 if (vec == NULL)
29658 expression_list = error_mark_node;
29659 else
29660 {
29661 expression_list = build_tree_list_vec (vec);
29662 release_tree_vector (vec);
29663 }
29664
29665 /* Create a location of the form:
29666 float(i)
29667 ^~~~~~~~
29668 with caret == start at the start of the type name,
29669 finishing at the closing paren. */
29670 location_t combined_loc = make_location (start_loc, start_loc,
29671 parser->lexer);
29672 cast = build_functional_cast (combined_loc, type, expression_list,
29673 tf_warning_or_error);
29674
29675 /* [expr.const]/1: In an integral constant expression "only type
29676 conversions to integral or enumeration type can be used". */
29677 if (TREE_CODE (type) == TYPE_DECL)
29678 type = TREE_TYPE (type);
29679 if (cast != error_mark_node
29680 && !cast_valid_in_integral_constant_expression_p (type)
29681 && cp_parser_non_integral_constant_expression (parser,
29682 NIC_CONSTRUCTOR))
29683 return error_mark_node;
29684
29685 return cast;
29686 }
29687
29688 /* Save the tokens that make up the body of a member function defined
29689 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
29690 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
29691 specifiers applied to the declaration. Returns the FUNCTION_DECL
29692 for the member function. */
29693
29694 static tree
29695 cp_parser_save_member_function_body (cp_parser* parser,
29696 cp_decl_specifier_seq *decl_specifiers,
29697 cp_declarator *declarator,
29698 tree attributes)
29699 {
29700 cp_token *first;
29701 cp_token *last;
29702 tree fn;
29703 bool function_try_block = false;
29704
29705 /* Create the FUNCTION_DECL. */
29706 fn = grokmethod (decl_specifiers, declarator, attributes);
29707 cp_finalize_omp_declare_simd (parser, fn);
29708 cp_finalize_oacc_routine (parser, fn, true);
29709 /* If something went badly wrong, bail out now. */
29710 if (fn == error_mark_node)
29711 {
29712 /* If there's a function-body, skip it. */
29713 if (cp_parser_token_starts_function_definition_p
29714 (cp_lexer_peek_token (parser->lexer)))
29715 cp_parser_skip_to_end_of_block_or_statement (parser);
29716 return error_mark_node;
29717 }
29718
29719 /* Remember it, if there are default args to post process. */
29720 cp_parser_save_default_args (parser, fn);
29721
29722 /* Save away the tokens that make up the body of the
29723 function. */
29724 first = parser->lexer->next_token;
29725
29726 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
29727 cp_lexer_consume_token (parser->lexer);
29728 else if (cp_lexer_next_token_is_keyword (parser->lexer,
29729 RID_TRANSACTION_ATOMIC))
29730 {
29731 cp_lexer_consume_token (parser->lexer);
29732 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
29733 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
29734 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
29735 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
29736 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
29737 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
29738 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
29739 {
29740 cp_lexer_consume_token (parser->lexer);
29741 cp_lexer_consume_token (parser->lexer);
29742 cp_lexer_consume_token (parser->lexer);
29743 cp_lexer_consume_token (parser->lexer);
29744 cp_lexer_consume_token (parser->lexer);
29745 }
29746 else
29747 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
29748 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
29749 {
29750 cp_lexer_consume_token (parser->lexer);
29751 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
29752 break;
29753 }
29754 }
29755
29756 /* Handle function try blocks. */
29757 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
29758 {
29759 cp_lexer_consume_token (parser->lexer);
29760 function_try_block = true;
29761 }
29762 /* We can have braced-init-list mem-initializers before the fn body. */
29763 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29764 {
29765 cp_lexer_consume_token (parser->lexer);
29766 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
29767 {
29768 /* cache_group will stop after an un-nested { } pair, too. */
29769 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
29770 break;
29771
29772 /* variadic mem-inits have ... after the ')'. */
29773 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29774 cp_lexer_consume_token (parser->lexer);
29775 }
29776 }
29777 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29778 /* Handle function try blocks. */
29779 if (function_try_block)
29780 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
29781 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29782 last = parser->lexer->next_token;
29783
29784 /* Save away the inline definition; we will process it when the
29785 class is complete. */
29786 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
29787 DECL_PENDING_INLINE_P (fn) = 1;
29788
29789 /* We need to know that this was defined in the class, so that
29790 friend templates are handled correctly. */
29791 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
29792
29793 /* Add FN to the queue of functions to be parsed later. */
29794 vec_safe_push (unparsed_funs_with_definitions, fn);
29795
29796 return fn;
29797 }
29798
29799 /* Save the tokens that make up the in-class initializer for a non-static
29800 data member. Returns a DEFERRED_PARSE. */
29801
29802 static tree
29803 cp_parser_save_nsdmi (cp_parser* parser)
29804 {
29805 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
29806 }
29807
29808 /* Parse a template-argument-list, as well as the trailing ">" (but
29809 not the opening "<"). See cp_parser_template_argument_list for the
29810 return value. */
29811
29812 static tree
29813 cp_parser_enclosed_template_argument_list (cp_parser* parser)
29814 {
29815 tree arguments;
29816 tree saved_scope;
29817 tree saved_qualifying_scope;
29818 tree saved_object_scope;
29819 bool saved_greater_than_is_operator_p;
29820
29821 /* [temp.names]
29822
29823 When parsing a template-id, the first non-nested `>' is taken as
29824 the end of the template-argument-list rather than a greater-than
29825 operator. */
29826 saved_greater_than_is_operator_p
29827 = parser->greater_than_is_operator_p;
29828 parser->greater_than_is_operator_p = false;
29829 /* Parsing the argument list may modify SCOPE, so we save it
29830 here. */
29831 saved_scope = parser->scope;
29832 saved_qualifying_scope = parser->qualifying_scope;
29833 saved_object_scope = parser->object_scope;
29834 /* We need to evaluate the template arguments, even though this
29835 template-id may be nested within a "sizeof". */
29836 cp_evaluated ev;
29837 /* Parse the template-argument-list itself. */
29838 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
29839 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
29840 arguments = NULL_TREE;
29841 else
29842 arguments = cp_parser_template_argument_list (parser);
29843 /* Look for the `>' that ends the template-argument-list. If we find
29844 a '>>' instead, it's probably just a typo. */
29845 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
29846 {
29847 if (cxx_dialect != cxx98)
29848 {
29849 /* In C++0x, a `>>' in a template argument list or cast
29850 expression is considered to be two separate `>'
29851 tokens. So, change the current token to a `>', but don't
29852 consume it: it will be consumed later when the outer
29853 template argument list (or cast expression) is parsed.
29854 Note that this replacement of `>' for `>>' is necessary
29855 even if we are parsing tentatively: in the tentative
29856 case, after calling
29857 cp_parser_enclosed_template_argument_list we will always
29858 throw away all of the template arguments and the first
29859 closing `>', either because the template argument list
29860 was erroneous or because we are replacing those tokens
29861 with a CPP_TEMPLATE_ID token. The second `>' (which will
29862 not have been thrown away) is needed either to close an
29863 outer template argument list or to complete a new-style
29864 cast. */
29865 cp_token *token = cp_lexer_peek_token (parser->lexer);
29866 token->type = CPP_GREATER;
29867 }
29868 else if (!saved_greater_than_is_operator_p)
29869 {
29870 /* If we're in a nested template argument list, the '>>' has
29871 to be a typo for '> >'. We emit the error message, but we
29872 continue parsing and we push a '>' as next token, so that
29873 the argument list will be parsed correctly. Note that the
29874 global source location is still on the token before the
29875 '>>', so we need to say explicitly where we want it. */
29876 cp_token *token = cp_lexer_peek_token (parser->lexer);
29877 gcc_rich_location richloc (token->location);
29878 richloc.add_fixit_replace ("> >");
29879 error_at (&richloc, "%<>>%> should be %<> >%> "
29880 "within a nested template argument list");
29881
29882 token->type = CPP_GREATER;
29883 }
29884 else
29885 {
29886 /* If this is not a nested template argument list, the '>>'
29887 is a typo for '>'. Emit an error message and continue.
29888 Same deal about the token location, but here we can get it
29889 right by consuming the '>>' before issuing the diagnostic. */
29890 cp_token *token = cp_lexer_consume_token (parser->lexer);
29891 error_at (token->location,
29892 "spurious %<>>%>, use %<>%> to terminate "
29893 "a template argument list");
29894 }
29895 }
29896 else
29897 cp_parser_skip_to_end_of_template_parameter_list (parser);
29898 /* The `>' token might be a greater-than operator again now. */
29899 parser->greater_than_is_operator_p
29900 = saved_greater_than_is_operator_p;
29901 /* Restore the SAVED_SCOPE. */
29902 parser->scope = saved_scope;
29903 parser->qualifying_scope = saved_qualifying_scope;
29904 parser->object_scope = saved_object_scope;
29905
29906 return arguments;
29907 }
29908
29909 /* MEMBER_FUNCTION is a member function, or a friend. If default
29910 arguments, or the body of the function have not yet been parsed,
29911 parse them now. */
29912
29913 static void
29914 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
29915 {
29916 timevar_push (TV_PARSE_INMETH);
29917 /* If this member is a template, get the underlying
29918 FUNCTION_DECL. */
29919 if (DECL_FUNCTION_TEMPLATE_P (member_function))
29920 member_function = DECL_TEMPLATE_RESULT (member_function);
29921
29922 /* There should not be any class definitions in progress at this
29923 point; the bodies of members are only parsed outside of all class
29924 definitions. */
29925 gcc_assert (parser->num_classes_being_defined == 0);
29926 /* While we're parsing the member functions we might encounter more
29927 classes. We want to handle them right away, but we don't want
29928 them getting mixed up with functions that are currently in the
29929 queue. */
29930 push_unparsed_function_queues (parser);
29931
29932 /* Make sure that any template parameters are in scope. */
29933 maybe_begin_member_template_processing (member_function);
29934
29935 /* If the body of the function has not yet been parsed, parse it
29936 now. */
29937 if (DECL_PENDING_INLINE_P (member_function))
29938 {
29939 tree function_scope;
29940 cp_token_cache *tokens;
29941
29942 /* The function is no longer pending; we are processing it. */
29943 tokens = DECL_PENDING_INLINE_INFO (member_function);
29944 DECL_PENDING_INLINE_INFO (member_function) = NULL;
29945 DECL_PENDING_INLINE_P (member_function) = 0;
29946
29947 /* If this is a local class, enter the scope of the containing
29948 function. */
29949 function_scope = current_function_decl;
29950 if (function_scope)
29951 push_function_context ();
29952
29953 /* Push the body of the function onto the lexer stack. */
29954 cp_parser_push_lexer_for_tokens (parser, tokens);
29955
29956 /* Let the front end know that we going to be defining this
29957 function. */
29958 start_preparsed_function (member_function, NULL_TREE,
29959 SF_PRE_PARSED | SF_INCLASS_INLINE);
29960
29961 /* Don't do access checking if it is a templated function. */
29962 if (processing_template_decl)
29963 push_deferring_access_checks (dk_no_check);
29964
29965 /* #pragma omp declare reduction needs special parsing. */
29966 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
29967 {
29968 parser->lexer->in_pragma = true;
29969 cp_parser_omp_declare_reduction_exprs (member_function, parser);
29970 finish_function (/*inline_p=*/true);
29971 cp_check_omp_declare_reduction (member_function);
29972 }
29973 else
29974 /* Now, parse the body of the function. */
29975 cp_parser_function_definition_after_declarator (parser,
29976 /*inline_p=*/true);
29977
29978 if (processing_template_decl)
29979 pop_deferring_access_checks ();
29980
29981 /* Leave the scope of the containing function. */
29982 if (function_scope)
29983 pop_function_context ();
29984 cp_parser_pop_lexer (parser);
29985 }
29986
29987 /* Remove any template parameters from the symbol table. */
29988 maybe_end_member_template_processing ();
29989
29990 /* Restore the queue. */
29991 pop_unparsed_function_queues (parser);
29992 timevar_pop (TV_PARSE_INMETH);
29993 }
29994
29995 /* If DECL contains any default args, remember it on the unparsed
29996 functions queue. */
29997
29998 static void
29999 cp_parser_save_default_args (cp_parser* parser, tree decl)
30000 {
30001 tree probe;
30002
30003 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
30004 probe;
30005 probe = TREE_CHAIN (probe))
30006 if (TREE_PURPOSE (probe))
30007 {
30008 cp_default_arg_entry entry = {current_class_type, decl};
30009 vec_safe_push (unparsed_funs_with_default_args, entry);
30010 break;
30011 }
30012
30013 /* Remember if there is a noexcept-specifier to post process. */
30014 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
30015 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
30016 vec_safe_push (unparsed_noexcepts, decl);
30017 }
30018
30019 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
30020 which is either a FIELD_DECL or PARM_DECL. Parse it and return
30021 the result. For a PARM_DECL, PARMTYPE is the corresponding type
30022 from the parameter-type-list. */
30023
30024 static tree
30025 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
30026 tree default_arg, tree parmtype)
30027 {
30028 cp_token_cache *tokens;
30029 tree parsed_arg;
30030 bool dummy;
30031
30032 if (default_arg == error_mark_node)
30033 return error_mark_node;
30034
30035 /* Push the saved tokens for the default argument onto the parser's
30036 lexer stack. */
30037 tokens = DEFPARSE_TOKENS (default_arg);
30038 cp_parser_push_lexer_for_tokens (parser, tokens);
30039
30040 start_lambda_scope (decl);
30041
30042 /* Parse the default argument. */
30043 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
30044 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
30045 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
30046
30047 finish_lambda_scope ();
30048
30049 if (parsed_arg == error_mark_node)
30050 cp_parser_skip_to_end_of_statement (parser);
30051
30052 if (!processing_template_decl)
30053 {
30054 /* In a non-template class, check conversions now. In a template,
30055 we'll wait and instantiate these as needed. */
30056 if (TREE_CODE (decl) == PARM_DECL)
30057 parsed_arg = check_default_argument (parmtype, parsed_arg,
30058 tf_warning_or_error);
30059 else if (maybe_reject_flexarray_init (decl, parsed_arg))
30060 parsed_arg = error_mark_node;
30061 else
30062 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
30063 }
30064
30065 /* If the token stream has not been completely used up, then
30066 there was extra junk after the end of the default
30067 argument. */
30068 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30069 {
30070 if (TREE_CODE (decl) == PARM_DECL)
30071 cp_parser_error (parser, "expected %<,%>");
30072 else
30073 cp_parser_error (parser, "expected %<;%>");
30074 }
30075
30076 /* Revert to the main lexer. */
30077 cp_parser_pop_lexer (parser);
30078
30079 return parsed_arg;
30080 }
30081
30082 /* FIELD is a non-static data member with an initializer which we saved for
30083 later; parse it now. */
30084
30085 static void
30086 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
30087 {
30088 tree def;
30089
30090 maybe_begin_member_template_processing (field);
30091
30092 push_unparsed_function_queues (parser);
30093 def = cp_parser_late_parse_one_default_arg (parser, field,
30094 DECL_INITIAL (field),
30095 NULL_TREE);
30096 pop_unparsed_function_queues (parser);
30097
30098 maybe_end_member_template_processing ();
30099
30100 DECL_INITIAL (field) = def;
30101 }
30102
30103 /* FN is a FUNCTION_DECL which may contains a parameter with an
30104 unparsed DEFERRED_PARSE. Parse the default args now. This function
30105 assumes that the current scope is the scope in which the default
30106 argument should be processed. */
30107
30108 static void
30109 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
30110 {
30111 unsigned char saved_local_variables_forbidden_p;
30112 tree parm, parmdecl;
30113
30114 /* While we're parsing the default args, we might (due to the
30115 statement expression extension) encounter more classes. We want
30116 to handle them right away, but we don't want them getting mixed
30117 up with default args that are currently in the queue. */
30118 push_unparsed_function_queues (parser);
30119
30120 /* Local variable names (and the `this' keyword) may not appear
30121 in a default argument. */
30122 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
30123 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
30124
30125 push_defarg_context (fn);
30126
30127 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
30128 parmdecl = DECL_ARGUMENTS (fn);
30129 parm && parm != void_list_node;
30130 parm = TREE_CHAIN (parm),
30131 parmdecl = DECL_CHAIN (parmdecl))
30132 {
30133 tree default_arg = TREE_PURPOSE (parm);
30134 tree parsed_arg;
30135 vec<tree, va_gc> *insts;
30136 tree copy;
30137 unsigned ix;
30138
30139 if (!default_arg)
30140 continue;
30141
30142 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
30143 /* This can happen for a friend declaration for a function
30144 already declared with default arguments. */
30145 continue;
30146
30147 parsed_arg
30148 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
30149 default_arg,
30150 TREE_VALUE (parm));
30151 TREE_PURPOSE (parm) = parsed_arg;
30152
30153 /* Update any instantiations we've already created. */
30154 for (insts = DEFPARSE_INSTANTIATIONS (default_arg), ix = 0;
30155 vec_safe_iterate (insts, ix, &copy); ix++)
30156 TREE_PURPOSE (copy) = parsed_arg;
30157 }
30158
30159 pop_defarg_context ();
30160
30161 /* Make sure no default arg is missing. */
30162 check_default_args (fn);
30163
30164 /* Restore the state of local_variables_forbidden_p. */
30165 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
30166
30167 /* Restore the queue. */
30168 pop_unparsed_function_queues (parser);
30169 }
30170
30171 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
30172
30173 sizeof ... ( identifier )
30174
30175 where the 'sizeof' token has already been consumed. */
30176
30177 static tree
30178 cp_parser_sizeof_pack (cp_parser *parser)
30179 {
30180 /* Consume the `...'. */
30181 cp_lexer_consume_token (parser->lexer);
30182 maybe_warn_variadic_templates ();
30183
30184 matching_parens parens;
30185 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
30186 if (paren)
30187 parens.consume_open (parser);
30188 else
30189 permerror (cp_lexer_peek_token (parser->lexer)->location,
30190 "%<sizeof...%> argument must be surrounded by parentheses");
30191
30192 cp_token *token = cp_lexer_peek_token (parser->lexer);
30193 tree name = cp_parser_identifier (parser);
30194 if (name == error_mark_node)
30195 return error_mark_node;
30196 /* The name is not qualified. */
30197 parser->scope = NULL_TREE;
30198 parser->qualifying_scope = NULL_TREE;
30199 parser->object_scope = NULL_TREE;
30200 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
30201 if (expr == error_mark_node)
30202 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
30203 token->location);
30204 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
30205 expr = TREE_TYPE (expr);
30206 else if (TREE_CODE (expr) == CONST_DECL)
30207 expr = DECL_INITIAL (expr);
30208 expr = make_pack_expansion (expr);
30209 PACK_EXPANSION_SIZEOF_P (expr) = true;
30210
30211 if (paren)
30212 parens.require_close (parser);
30213
30214 return expr;
30215 }
30216
30217 /* Parse the operand of `sizeof' (or a similar operator). Returns
30218 either a TYPE or an expression, depending on the form of the
30219 input. The KEYWORD indicates which kind of expression we have
30220 encountered. */
30221
30222 static tree
30223 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
30224 {
30225 tree expr = NULL_TREE;
30226 const char *saved_message;
30227 const char *saved_message_arg;
30228 bool saved_integral_constant_expression_p;
30229 bool saved_non_integral_constant_expression_p;
30230
30231 /* If it's a `...', then we are computing the length of a parameter
30232 pack. */
30233 if (keyword == RID_SIZEOF
30234 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30235 return cp_parser_sizeof_pack (parser);
30236
30237 /* Types cannot be defined in a `sizeof' expression. Save away the
30238 old message. */
30239 saved_message = parser->type_definition_forbidden_message;
30240 saved_message_arg = parser->type_definition_forbidden_message_arg;
30241 parser->type_definition_forbidden_message
30242 = G_("types may not be defined in %qs expressions");
30243 parser->type_definition_forbidden_message_arg
30244 = IDENTIFIER_POINTER (ridpointers[keyword]);
30245
30246 /* The restrictions on constant-expressions do not apply inside
30247 sizeof expressions. */
30248 saved_integral_constant_expression_p
30249 = parser->integral_constant_expression_p;
30250 saved_non_integral_constant_expression_p
30251 = parser->non_integral_constant_expression_p;
30252 parser->integral_constant_expression_p = false;
30253
30254 /* Do not actually evaluate the expression. */
30255 ++cp_unevaluated_operand;
30256 ++c_inhibit_evaluation_warnings;
30257 /* If it's a `(', then we might be looking at the type-id
30258 construction. */
30259 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30260 {
30261 tree type = NULL_TREE;
30262
30263 tentative_firewall firewall (parser);
30264
30265 /* We can't be sure yet whether we're looking at a type-id or an
30266 expression. */
30267 cp_parser_parse_tentatively (parser);
30268
30269 matching_parens parens;
30270 parens.consume_open (parser);
30271
30272 /* Note: as a GNU Extension, compound literals are considered
30273 postfix-expressions as they are in C99, so they are valid
30274 arguments to sizeof. See comment in cp_parser_cast_expression
30275 for details. */
30276 if (cp_parser_compound_literal_p (parser))
30277 cp_parser_simulate_error (parser);
30278 else
30279 {
30280 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
30281 parser->in_type_id_in_expr_p = true;
30282 /* Look for the type-id. */
30283 type = cp_parser_type_id (parser);
30284 /* Look for the closing `)'. */
30285 parens.require_close (parser);
30286 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
30287 }
30288
30289 /* If all went well, then we're done. */
30290 if (cp_parser_parse_definitely (parser))
30291 expr = type;
30292 else
30293 {
30294 /* Commit to the tentative_firewall so we get syntax errors. */
30295 cp_parser_commit_to_tentative_parse (parser);
30296
30297 expr = cp_parser_unary_expression (parser);
30298 }
30299 }
30300 else
30301 expr = cp_parser_unary_expression (parser);
30302
30303 /* Go back to evaluating expressions. */
30304 --cp_unevaluated_operand;
30305 --c_inhibit_evaluation_warnings;
30306
30307 /* And restore the old one. */
30308 parser->type_definition_forbidden_message = saved_message;
30309 parser->type_definition_forbidden_message_arg = saved_message_arg;
30310 parser->integral_constant_expression_p
30311 = saved_integral_constant_expression_p;
30312 parser->non_integral_constant_expression_p
30313 = saved_non_integral_constant_expression_p;
30314
30315 return expr;
30316 }
30317
30318 /* If the current declaration has no declarator, return true. */
30319
30320 static bool
30321 cp_parser_declares_only_class_p (cp_parser *parser)
30322 {
30323 /* If the next token is a `;' or a `,' then there is no
30324 declarator. */
30325 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30326 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
30327 }
30328
30329 /* Update the DECL_SPECS to reflect the storage class indicated by
30330 KEYWORD. */
30331
30332 static void
30333 cp_parser_set_storage_class (cp_parser *parser,
30334 cp_decl_specifier_seq *decl_specs,
30335 enum rid keyword,
30336 cp_token *token)
30337 {
30338 cp_storage_class storage_class;
30339
30340 if (parser->in_unbraced_linkage_specification_p)
30341 {
30342 error_at (token->location, "invalid use of %qD in linkage specification",
30343 ridpointers[keyword]);
30344 return;
30345 }
30346 else if (decl_specs->storage_class != sc_none)
30347 {
30348 decl_specs->conflicting_specifiers_p = true;
30349 return;
30350 }
30351
30352 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
30353 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
30354 && decl_specs->gnu_thread_keyword_p)
30355 {
30356 pedwarn (decl_specs->locations[ds_thread], 0,
30357 "%<__thread%> before %qD", ridpointers[keyword]);
30358 }
30359
30360 switch (keyword)
30361 {
30362 case RID_AUTO:
30363 storage_class = sc_auto;
30364 break;
30365 case RID_REGISTER:
30366 storage_class = sc_register;
30367 break;
30368 case RID_STATIC:
30369 storage_class = sc_static;
30370 break;
30371 case RID_EXTERN:
30372 storage_class = sc_extern;
30373 break;
30374 case RID_MUTABLE:
30375 storage_class = sc_mutable;
30376 break;
30377 default:
30378 gcc_unreachable ();
30379 }
30380 decl_specs->storage_class = storage_class;
30381 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
30382
30383 /* A storage class specifier cannot be applied alongside a typedef
30384 specifier. If there is a typedef specifier present then set
30385 conflicting_specifiers_p which will trigger an error later
30386 on in grokdeclarator. */
30387 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
30388 decl_specs->conflicting_specifiers_p = true;
30389 }
30390
30391 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
30392 is true, the type is a class or enum definition. */
30393
30394 static void
30395 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
30396 tree type_spec,
30397 cp_token *token,
30398 bool type_definition_p)
30399 {
30400 decl_specs->any_specifiers_p = true;
30401
30402 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
30403 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
30404 this is what happened. In system headers, we ignore these
30405 declarations so that G++ can work with system headers that are not
30406 C++-safe. */
30407 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
30408 && !type_definition_p
30409 && (type_spec == boolean_type_node
30410 || type_spec == char8_type_node
30411 || type_spec == char16_type_node
30412 || type_spec == char32_type_node
30413 || type_spec == wchar_type_node)
30414 && (decl_specs->type
30415 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
30416 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
30417 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
30418 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
30419 {
30420 decl_specs->redefined_builtin_type = type_spec;
30421 set_and_check_decl_spec_loc (decl_specs,
30422 ds_redefined_builtin_type_spec,
30423 token);
30424 if (!decl_specs->type)
30425 {
30426 decl_specs->type = type_spec;
30427 decl_specs->type_definition_p = false;
30428 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
30429 }
30430 }
30431 else if (decl_specs->type)
30432 decl_specs->multiple_types_p = true;
30433 else
30434 {
30435 decl_specs->type = type_spec;
30436 decl_specs->type_definition_p = type_definition_p;
30437 decl_specs->redefined_builtin_type = NULL_TREE;
30438 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
30439 }
30440 }
30441
30442 /* True iff TOKEN is the GNU keyword __thread. */
30443
30444 static bool
30445 token_is__thread (cp_token *token)
30446 {
30447 gcc_assert (token->keyword == RID_THREAD);
30448 return id_equal (token->u.value, "__thread");
30449 }
30450
30451 /* Set the location for a declarator specifier and check if it is
30452 duplicated.
30453
30454 DECL_SPECS is the sequence of declarator specifiers onto which to
30455 set the location.
30456
30457 DS is the single declarator specifier to set which location is to
30458 be set onto the existing sequence of declarators.
30459
30460 LOCATION is the location for the declarator specifier to
30461 consider. */
30462
30463 static void
30464 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
30465 cp_decl_spec ds, cp_token *token)
30466 {
30467 gcc_assert (ds < ds_last);
30468
30469 if (decl_specs == NULL)
30470 return;
30471
30472 location_t location = token->location;
30473
30474 if (decl_specs->locations[ds] == 0)
30475 {
30476 decl_specs->locations[ds] = location;
30477 if (ds == ds_thread)
30478 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
30479 }
30480 else
30481 {
30482 if (ds == ds_long)
30483 {
30484 if (decl_specs->locations[ds_long_long] != 0)
30485 error_at (location,
30486 "%<long long long%> is too long for GCC");
30487 else
30488 {
30489 decl_specs->locations[ds_long_long] = location;
30490 pedwarn_cxx98 (location,
30491 OPT_Wlong_long,
30492 "ISO C++ 1998 does not support %<long long%>");
30493 }
30494 }
30495 else if (ds == ds_thread)
30496 {
30497 bool gnu = token_is__thread (token);
30498 gcc_rich_location richloc (location);
30499 if (gnu != decl_specs->gnu_thread_keyword_p)
30500 {
30501 richloc.add_range (decl_specs->locations[ds_thread]);
30502 error_at (&richloc,
30503 "both %<__thread%> and %<thread_local%> specified");
30504 }
30505 else
30506 {
30507 richloc.add_fixit_remove ();
30508 error_at (&richloc, "duplicate %qD", token->u.value);
30509 }
30510 }
30511 else
30512 {
30513 static const char *const decl_spec_names[] = {
30514 "signed",
30515 "unsigned",
30516 "short",
30517 "long",
30518 "const",
30519 "volatile",
30520 "restrict",
30521 "inline",
30522 "virtual",
30523 "explicit",
30524 "friend",
30525 "typedef",
30526 "using",
30527 "constexpr",
30528 "__complex",
30529 "constinit",
30530 "consteval"
30531 };
30532 gcc_rich_location richloc (location);
30533 richloc.add_fixit_remove ();
30534 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
30535 }
30536 }
30537 }
30538
30539 /* Return true iff the declarator specifier DS is present in the
30540 sequence of declarator specifiers DECL_SPECS. */
30541
30542 bool
30543 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
30544 cp_decl_spec ds)
30545 {
30546 gcc_assert (ds < ds_last);
30547
30548 if (decl_specs == NULL)
30549 return false;
30550
30551 return decl_specs->locations[ds] != 0;
30552 }
30553
30554 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
30555 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
30556
30557 static bool
30558 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
30559 {
30560 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
30561 }
30562
30563 /* Issue an error message indicating that TOKEN_DESC was expected.
30564 If KEYWORD is true, it indicated this function is called by
30565 cp_parser_require_keword and the required token can only be
30566 a indicated keyword.
30567
30568 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
30569 within any error as the location of an "opening" token matching
30570 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
30571 RT_CLOSE_PAREN). */
30572
30573 static void
30574 cp_parser_required_error (cp_parser *parser,
30575 required_token token_desc,
30576 bool keyword,
30577 location_t matching_location)
30578 {
30579 if (cp_parser_simulate_error (parser))
30580 return;
30581
30582 const char *gmsgid = NULL;
30583 switch (token_desc)
30584 {
30585 case RT_NEW:
30586 gmsgid = G_("expected %<new%>");
30587 break;
30588 case RT_DELETE:
30589 gmsgid = G_("expected %<delete%>");
30590 break;
30591 case RT_RETURN:
30592 gmsgid = G_("expected %<return%>");
30593 break;
30594 case RT_WHILE:
30595 gmsgid = G_("expected %<while%>");
30596 break;
30597 case RT_EXTERN:
30598 gmsgid = G_("expected %<extern%>");
30599 break;
30600 case RT_STATIC_ASSERT:
30601 gmsgid = G_("expected %<static_assert%>");
30602 break;
30603 case RT_DECLTYPE:
30604 gmsgid = G_("expected %<decltype%>");
30605 break;
30606 case RT_OPERATOR:
30607 gmsgid = G_("expected %<operator%>");
30608 break;
30609 case RT_CLASS:
30610 gmsgid = G_("expected %<class%>");
30611 break;
30612 case RT_TEMPLATE:
30613 gmsgid = G_("expected %<template%>");
30614 break;
30615 case RT_NAMESPACE:
30616 gmsgid = G_("expected %<namespace%>");
30617 break;
30618 case RT_USING:
30619 gmsgid = G_("expected %<using%>");
30620 break;
30621 case RT_ASM:
30622 gmsgid = G_("expected %<asm%>");
30623 break;
30624 case RT_TRY:
30625 gmsgid = G_("expected %<try%>");
30626 break;
30627 case RT_CATCH:
30628 gmsgid = G_("expected %<catch%>");
30629 break;
30630 case RT_THROW:
30631 gmsgid = G_("expected %<throw%>");
30632 break;
30633 case RT_AUTO:
30634 gmsgid = G_("expected %<auto%>");
30635 break;
30636 case RT_LABEL:
30637 gmsgid = G_("expected %<__label__%>");
30638 break;
30639 case RT_AT_TRY:
30640 gmsgid = G_("expected %<@try%>");
30641 break;
30642 case RT_AT_SYNCHRONIZED:
30643 gmsgid = G_("expected %<@synchronized%>");
30644 break;
30645 case RT_AT_THROW:
30646 gmsgid = G_("expected %<@throw%>");
30647 break;
30648 case RT_TRANSACTION_ATOMIC:
30649 gmsgid = G_("expected %<__transaction_atomic%>");
30650 break;
30651 case RT_TRANSACTION_RELAXED:
30652 gmsgid = G_("expected %<__transaction_relaxed%>");
30653 break;
30654 case RT_CO_YIELD:
30655 gmsgid = G_("expected %<co_yield%>");
30656 break;
30657 default:
30658 break;
30659 }
30660
30661 if (!gmsgid && !keyword)
30662 {
30663 switch (token_desc)
30664 {
30665 case RT_SEMICOLON:
30666 gmsgid = G_("expected %<;%>");
30667 break;
30668 case RT_OPEN_PAREN:
30669 gmsgid = G_("expected %<(%>");
30670 break;
30671 case RT_CLOSE_BRACE:
30672 gmsgid = G_("expected %<}%>");
30673 break;
30674 case RT_OPEN_BRACE:
30675 gmsgid = G_("expected %<{%>");
30676 break;
30677 case RT_CLOSE_SQUARE:
30678 gmsgid = G_("expected %<]%>");
30679 break;
30680 case RT_OPEN_SQUARE:
30681 gmsgid = G_("expected %<[%>");
30682 break;
30683 case RT_COMMA:
30684 gmsgid = G_("expected %<,%>");
30685 break;
30686 case RT_SCOPE:
30687 gmsgid = G_("expected %<::%>");
30688 break;
30689 case RT_LESS:
30690 gmsgid = G_("expected %<<%>");
30691 break;
30692 case RT_GREATER:
30693 gmsgid = G_("expected %<>%>");
30694 break;
30695 case RT_EQ:
30696 gmsgid = G_("expected %<=%>");
30697 break;
30698 case RT_ELLIPSIS:
30699 gmsgid = G_("expected %<...%>");
30700 break;
30701 case RT_MULT:
30702 gmsgid = G_("expected %<*%>");
30703 break;
30704 case RT_COMPL:
30705 gmsgid = G_("expected %<~%>");
30706 break;
30707 case RT_COLON:
30708 gmsgid = G_("expected %<:%>");
30709 break;
30710 case RT_COLON_SCOPE:
30711 gmsgid = G_("expected %<:%> or %<::%>");
30712 break;
30713 case RT_CLOSE_PAREN:
30714 gmsgid = G_("expected %<)%>");
30715 break;
30716 case RT_COMMA_CLOSE_PAREN:
30717 gmsgid = G_("expected %<,%> or %<)%>");
30718 break;
30719 case RT_PRAGMA_EOL:
30720 gmsgid = G_("expected end of line");
30721 break;
30722 case RT_NAME:
30723 gmsgid = G_("expected identifier");
30724 break;
30725 case RT_SELECT:
30726 gmsgid = G_("expected selection-statement");
30727 break;
30728 case RT_ITERATION:
30729 gmsgid = G_("expected iteration-statement");
30730 break;
30731 case RT_JUMP:
30732 gmsgid = G_("expected jump-statement");
30733 break;
30734 case RT_CLASS_KEY:
30735 gmsgid = G_("expected class-key");
30736 break;
30737 case RT_CLASS_TYPENAME_TEMPLATE:
30738 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
30739 break;
30740 default:
30741 gcc_unreachable ();
30742 }
30743 }
30744
30745 if (gmsgid)
30746 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
30747 }
30748
30749
30750 /* If the next token is of the indicated TYPE, consume it. Otherwise,
30751 issue an error message indicating that TOKEN_DESC was expected.
30752
30753 Returns the token consumed, if the token had the appropriate type.
30754 Otherwise, returns NULL.
30755
30756 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
30757 within any error as the location of an "opening" token matching
30758 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
30759 RT_CLOSE_PAREN). */
30760
30761 static cp_token *
30762 cp_parser_require (cp_parser* parser,
30763 enum cpp_ttype type,
30764 required_token token_desc,
30765 location_t matching_location)
30766 {
30767 if (cp_lexer_next_token_is (parser->lexer, type))
30768 return cp_lexer_consume_token (parser->lexer);
30769 else
30770 {
30771 /* Output the MESSAGE -- unless we're parsing tentatively. */
30772 if (!cp_parser_simulate_error (parser))
30773 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
30774 matching_location);
30775 return NULL;
30776 }
30777 }
30778
30779 /* An error message is produced if the next token is not '>'.
30780 All further tokens are skipped until the desired token is
30781 found or '{', '}', ';' or an unbalanced ')' or ']'. */
30782
30783 static void
30784 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
30785 {
30786 /* Current level of '< ... >'. */
30787 unsigned level = 0;
30788 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
30789 unsigned nesting_depth = 0;
30790
30791 /* Are we ready, yet? If not, issue error message. */
30792 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
30793 return;
30794
30795 /* Skip tokens until the desired token is found. */
30796 while (true)
30797 {
30798 /* Peek at the next token. */
30799 switch (cp_lexer_peek_token (parser->lexer)->type)
30800 {
30801 case CPP_LESS:
30802 if (!nesting_depth)
30803 ++level;
30804 break;
30805
30806 case CPP_RSHIFT:
30807 if (cxx_dialect == cxx98)
30808 /* C++0x views the `>>' operator as two `>' tokens, but
30809 C++98 does not. */
30810 break;
30811 else if (!nesting_depth && level-- == 0)
30812 {
30813 /* We've hit a `>>' where the first `>' closes the
30814 template argument list, and the second `>' is
30815 spurious. Just consume the `>>' and stop; we've
30816 already produced at least one error. */
30817 cp_lexer_consume_token (parser->lexer);
30818 return;
30819 }
30820 /* Fall through for C++0x, so we handle the second `>' in
30821 the `>>'. */
30822 gcc_fallthrough ();
30823
30824 case CPP_GREATER:
30825 if (!nesting_depth && level-- == 0)
30826 {
30827 /* We've reached the token we want, consume it and stop. */
30828 cp_lexer_consume_token (parser->lexer);
30829 return;
30830 }
30831 break;
30832
30833 case CPP_OPEN_PAREN:
30834 case CPP_OPEN_SQUARE:
30835 ++nesting_depth;
30836 break;
30837
30838 case CPP_CLOSE_PAREN:
30839 case CPP_CLOSE_SQUARE:
30840 if (nesting_depth-- == 0)
30841 return;
30842 break;
30843
30844 case CPP_EOF:
30845 case CPP_PRAGMA_EOL:
30846 case CPP_SEMICOLON:
30847 case CPP_OPEN_BRACE:
30848 case CPP_CLOSE_BRACE:
30849 /* The '>' was probably forgotten, don't look further. */
30850 return;
30851
30852 default:
30853 break;
30854 }
30855
30856 /* Consume this token. */
30857 cp_lexer_consume_token (parser->lexer);
30858 }
30859 }
30860
30861 /* If the next token is the indicated keyword, consume it. Otherwise,
30862 issue an error message indicating that TOKEN_DESC was expected.
30863
30864 Returns the token consumed, if the token had the appropriate type.
30865 Otherwise, returns NULL. */
30866
30867 static cp_token *
30868 cp_parser_require_keyword (cp_parser* parser,
30869 enum rid keyword,
30870 required_token token_desc)
30871 {
30872 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
30873
30874 if (token && token->keyword != keyword)
30875 {
30876 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
30877 UNKNOWN_LOCATION);
30878 return NULL;
30879 }
30880
30881 return token;
30882 }
30883
30884 /* Returns TRUE iff TOKEN is a token that can begin the body of a
30885 function-definition. */
30886
30887 static bool
30888 cp_parser_token_starts_function_definition_p (cp_token* token)
30889 {
30890 return (/* An ordinary function-body begins with an `{'. */
30891 token->type == CPP_OPEN_BRACE
30892 /* A ctor-initializer begins with a `:'. */
30893 || token->type == CPP_COLON
30894 /* A function-try-block begins with `try'. */
30895 || token->keyword == RID_TRY
30896 /* A function-transaction-block begins with `__transaction_atomic'
30897 or `__transaction_relaxed'. */
30898 || token->keyword == RID_TRANSACTION_ATOMIC
30899 || token->keyword == RID_TRANSACTION_RELAXED
30900 /* The named return value extension begins with `return'. */
30901 || token->keyword == RID_RETURN);
30902 }
30903
30904 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
30905 definition. */
30906
30907 static bool
30908 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
30909 {
30910 cp_token *token;
30911
30912 token = cp_lexer_peek_token (parser->lexer);
30913 return (token->type == CPP_OPEN_BRACE
30914 || (token->type == CPP_COLON
30915 && !parser->colon_doesnt_start_class_def_p));
30916 }
30917
30918 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
30919 C++0x) ending a template-argument. */
30920
30921 static bool
30922 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
30923 {
30924 cp_token *token;
30925
30926 token = cp_lexer_peek_token (parser->lexer);
30927 return (token->type == CPP_COMMA
30928 || token->type == CPP_GREATER
30929 || token->type == CPP_ELLIPSIS
30930 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
30931 }
30932
30933 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
30934 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
30935
30936 static bool
30937 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
30938 size_t n)
30939 {
30940 cp_token *token;
30941
30942 token = cp_lexer_peek_nth_token (parser->lexer, n);
30943 if (token->type == CPP_LESS)
30944 return true;
30945 /* Check for the sequence `<::' in the original code. It would be lexed as
30946 `[:', where `[' is a digraph, and there is no whitespace before
30947 `:'. */
30948 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
30949 {
30950 cp_token *token2;
30951 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
30952 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
30953 return true;
30954 }
30955 return false;
30956 }
30957
30958 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
30959 or none_type otherwise. */
30960
30961 static enum tag_types
30962 cp_parser_token_is_class_key (cp_token* token)
30963 {
30964 switch (token->keyword)
30965 {
30966 case RID_CLASS:
30967 return class_type;
30968 case RID_STRUCT:
30969 return record_type;
30970 case RID_UNION:
30971 return union_type;
30972
30973 default:
30974 return none_type;
30975 }
30976 }
30977
30978 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
30979 or none_type otherwise or if the token is null. */
30980
30981 static enum tag_types
30982 cp_parser_token_is_type_parameter_key (cp_token* token)
30983 {
30984 if (!token)
30985 return none_type;
30986
30987 switch (token->keyword)
30988 {
30989 case RID_CLASS:
30990 return class_type;
30991 case RID_TYPENAME:
30992 return typename_type;
30993
30994 default:
30995 return none_type;
30996 }
30997 }
30998
30999 /* Diagnose redundant enum-keys. */
31000
31001 static void
31002 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
31003 tree type, rid scoped_key)
31004 {
31005 if (!warn_redundant_tags)
31006 return;
31007
31008 tree type_decl = TYPE_MAIN_DECL (type);
31009 tree name = DECL_NAME (type_decl);
31010 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
31011 push_deferring_access_checks (dk_no_check);
31012 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
31013 pop_deferring_access_checks ();
31014
31015 /* The enum-key is redundant for uses of the TYPE that are not
31016 declarations and for which name lookup returns just the type
31017 itself. */
31018 if (decl != type_decl)
31019 return;
31020
31021 if (scoped_key != RID_CLASS
31022 && scoped_key != RID_STRUCT
31023 && current_lang_name != lang_name_cplusplus
31024 && current_namespace == global_namespace)
31025 {
31026 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
31027 enum tag in shared C/C++ code in files (such as headers) included
31028 in the main source file. */
31029 const line_map_ordinary *map = NULL;
31030 linemap_resolve_location (line_table, key_loc,
31031 LRK_MACRO_DEFINITION_LOCATION,
31032 &map);
31033 if (!MAIN_FILE_P (map))
31034 return;
31035 }
31036
31037 gcc_rich_location richloc (key_loc);
31038 richloc.add_fixit_remove (key_loc);
31039 warning_at (&richloc, OPT_Wredundant_tags,
31040 "redundant enum-key %<enum%s%> in reference to %q#T",
31041 (scoped_key == RID_CLASS ? " class"
31042 : scoped_key == RID_STRUCT ? " struct" : ""), type);
31043 }
31044
31045 /* Describes the set of declarations of a struct, class, or class template
31046 or its specializations. Used for -Wmismatched-tags. */
31047
31048 class class_decl_loc_t
31049 {
31050 public:
31051
31052 class_decl_loc_t ()
31053 : locvec (), idxdef (), def_class_key ()
31054 {
31055 locvec.create (4);
31056 }
31057
31058 /* Constructs an object for a single declaration of a class with
31059 CLASS_KEY at the current location in the current function (or
31060 at another scope). KEY_REDUNDANT is true if the class-key may
31061 be omitted in the current context without an ambiguity with
31062 another symbol with the same name.
31063 DEF_P is true for a class declaration that is a definition.
31064 CURLOC is the associated location. */
31065 class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
31066 location_t curloc = input_location)
31067 : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
31068 {
31069 locvec.create (4);
31070 class_key_loc_t ckl (current_function_decl, curloc, class_key,
31071 key_redundant);
31072 locvec.quick_push (ckl);
31073 }
31074
31075 /* Copy, assign, and destroy the object. Necessary because LOCVEC
31076 isn't safely copyable and assignable and doesn't release storage
31077 on its own. */
31078 class_decl_loc_t (const class_decl_loc_t &rhs)
31079 : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
31080 def_class_key (rhs.def_class_key)
31081 { }
31082
31083 class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
31084 {
31085 if (this == &rhs)
31086 return *this;
31087 locvec.release ();
31088 locvec = rhs.locvec.copy ();
31089 idxdef = rhs.idxdef;
31090 def_class_key = rhs.def_class_key;
31091 return *this;
31092 }
31093
31094 ~class_decl_loc_t ()
31095 {
31096 locvec.release ();
31097 }
31098
31099 /* Issues -Wmismatched-tags for a single class. */
31100 void diag_mismatched_tags (tree);
31101
31102 /* Issues -Wmismatched-tags for all classes. */
31103 static void diag_mismatched_tags ();
31104
31105 /* Adds TYPE_DECL to the collection of class decls and diagnoses
31106 redundant tags (if -Wredundant-tags is enabled). */
31107 static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
31108
31109 /* Either adds this decl to the collection of class decls
31110 or diagnoses it, whichever is appropriate. */
31111 void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
31112
31113 private:
31114
31115 tree function (unsigned i) const
31116 {
31117 return locvec[i].func;
31118 }
31119
31120 location_t location (unsigned i) const
31121 {
31122 return locvec[i].loc;
31123 }
31124
31125 bool key_redundant (unsigned i) const
31126 {
31127 return locvec[i].key_redundant;
31128 }
31129
31130 tag_types class_key (unsigned i) const
31131 {
31132 return locvec[i].class_key;
31133 }
31134
31135 /* True if a definition for the class has been seen. */
31136 bool def_p () const
31137 {
31138 return idxdef < locvec.length ();
31139 }
31140
31141 /* The location of a single mention of a class type with the given
31142 class-key. */
31143 struct class_key_loc_t
31144 {
31145 class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
31146 : func (func), loc (loc), class_key (key), key_redundant (redundant)
31147 { }
31148
31149 /* The function the type is mentioned in. */
31150 tree func;
31151 /* The exact location. */
31152 location_t loc;
31153 /* The class-key used in the mention of the type. */
31154 tag_types class_key;
31155 /* True when the class-key could be omitted at this location
31156 without an ambiguity with another symbol of the same name. */
31157 bool key_redundant;
31158 };
31159 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
31160 vec <class_key_loc_t> locvec;
31161 /* LOCVEC index of the definition or UINT_MAX if none exists. */
31162 unsigned idxdef;
31163 /* The class-key the class was last declared with or none_type when
31164 it has been declared with a mismatched key. */
31165 tag_types def_class_key;
31166
31167 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
31168 description above. */
31169 typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
31170 static class_to_loc_map_t class2loc;
31171 };
31172
31173 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
31174
31175 /* Issue an error message if the CLASS_KEY does not match the TYPE.
31176 DEF_P is expected to be set for a definition of class TYPE. DECL_P
31177 is set for a declaration of class TYPE and clear for a reference to
31178 it that is not a declaration of it. */
31179
31180 static void
31181 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
31182 tag_types class_key, tree type, bool def_p,
31183 bool decl_p)
31184 {
31185 if (type == error_mark_node)
31186 return;
31187
31188 bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
31189 if (seen_as_union != (class_key == union_type))
31190 {
31191 if (permerror (input_location, "%qs tag used in naming %q#T",
31192 class_key == union_type ? "union"
31193 : class_key == record_type ? "struct" : "class",
31194 type))
31195 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
31196 "%q#T was previously declared here", type);
31197 return;
31198 }
31199
31200 if (!warn_mismatched_tags && !warn_redundant_tags)
31201 return;
31202
31203 /* Only consider the true class-keys below and ignore typename_type,
31204 etc. that are not C++ class-keys. */
31205 if (class_key != class_type
31206 && class_key != record_type
31207 && class_key != union_type)
31208 return;
31209
31210 class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
31211 }
31212
31213 /* Returns the template or specialization of one to which the RECORD_TYPE
31214 TYPE corresponds. */
31215
31216 static tree
31217 specialization_of (tree type)
31218 {
31219 tree ret = type;
31220
31221 /* Determine the template or its partial specialization to which TYPE
31222 corresponds. */
31223 if (tree spec = most_specialized_partial_spec (type, tf_none))
31224 if (spec != error_mark_node)
31225 ret = TREE_TYPE (TREE_VALUE (spec));
31226
31227 if (ret == type)
31228 ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
31229
31230 return TYPE_MAIN_DECL (ret);
31231 }
31232
31233
31234 /* Adds the class TYPE to the collection of class decls and diagnoses
31235 redundant tags (if -Wredundant-tags is enabled).
31236 DEF_P is expected to be set for a definition of class TYPE. DECL_P
31237 is set for a (likely, based on syntactic context) declaration of class
31238 TYPE and clear for a reference to it that is not a declaration of it. */
31239
31240 void
31241 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
31242 tag_types class_key, tree type, bool def_p, bool decl_p)
31243 {
31244 tree type_decl = TYPE_MAIN_DECL (type);
31245 tree name = DECL_NAME (type_decl);
31246 /* Look up the NAME to see if it unambiguously refers to the TYPE
31247 and set KEY_REDUNDANT if so. */
31248 push_deferring_access_checks (dk_no_check);
31249 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
31250 pop_deferring_access_checks ();
31251
31252 /* The class-key is redundant for uses of the CLASS_TYPE that are
31253 neither definitions of it nor declarations, and for which name
31254 lookup returns just the type itself. */
31255 bool key_redundant = (!def_p && !decl_p
31256 && (decl == type_decl
31257 || TREE_CODE (decl) == TEMPLATE_DECL
31258 || TYPE_BEING_DEFINED (type)));
31259
31260 if (key_redundant
31261 && class_key != class_type
31262 && current_lang_name != lang_name_cplusplus
31263 && current_namespace == global_namespace)
31264 {
31265 /* Avoid issuing the diagnostic for apparently redundant struct
31266 and union class-keys in shared C/C++ code in files (such as
31267 headers) included in the main source file. */
31268 const line_map_ordinary *map = NULL;
31269 linemap_resolve_location (line_table, key_loc,
31270 LRK_MACRO_DEFINITION_LOCATION,
31271 &map);
31272 if (!MAIN_FILE_P (map))
31273 key_redundant = false;
31274 }
31275
31276 /* Set if a declaration of TYPE has previously been seen or if it must
31277 exist in a precompiled header. */
31278 bool exist;
31279 class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
31280 if (!exist)
31281 {
31282 tree type = TREE_TYPE (type_decl);
31283 if (def_p || !COMPLETE_TYPE_P (type))
31284 {
31285 /* TYPE_DECL is the first declaration or definition of the type
31286 (outside precompiled headers -- see below). Just create
31287 a new entry for it and return unless it's a declaration
31288 involving a template that may need to be diagnosed by
31289 -Wredundant-tags. */
31290 *rdl = class_decl_loc_t (class_key, false, def_p);
31291 if (TREE_CODE (decl) != TEMPLATE_DECL)
31292 return;
31293 }
31294 else
31295 {
31296 /* TYPE was previously defined in some unknown precompiled hdeader.
31297 Simply add a record of its definition at an unknown location and
31298 proceed below to add a reference to it at the current location.
31299 (Declarations in precompiled headers that are not definitions
31300 are ignored.) */
31301 tag_types def_key
31302 = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
31303 location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
31304 *rdl = class_decl_loc_t (def_key, false, true, def_loc);
31305 exist = true;
31306 }
31307 }
31308
31309 /* A prior declaration of TYPE_DECL has been seen. */
31310
31311 if (key_redundant)
31312 {
31313 gcc_rich_location richloc (key_loc);
31314 richloc.add_fixit_remove (key_loc);
31315 warning_at (&richloc, OPT_Wredundant_tags,
31316 "redundant class-key %qs in reference to %q#T",
31317 class_key == union_type ? "union"
31318 : class_key == record_type ? "struct" : "class",
31319 type);
31320 }
31321
31322 if (!exist)
31323 /* Do nothing if this is the first declaration of the type. */
31324 return;
31325
31326 if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
31327 /* Do nothing if the class-key in this declaration matches
31328 the definition. */
31329 return;
31330
31331 rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
31332 def_p);
31333 }
31334
31335 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
31336 of class decls or diagnoses it, whichever is appropriate. */
31337
31338 void
31339 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
31340 tag_types class_key,
31341 bool redundant,
31342 bool def_p)
31343 {
31344 /* Reset the CLASS_KEY associated with this type on mismatch.
31345 This is an optimization that lets the diagnostic code skip
31346 over classes that use the same class-key in all declarations. */
31347 if (def_class_key != class_key)
31348 def_class_key = none_type;
31349
31350 /* Set IDXDEF to the index of the vector corresponding to
31351 the definition. */
31352 if (def_p)
31353 idxdef = locvec.length ();
31354
31355 /* Append a record of this declaration to the vector. */
31356 class_key_loc_t ckl (current_function_decl, input_location, class_key,
31357 redundant);
31358 locvec.safe_push (ckl);
31359
31360 if (idxdef == UINT_MAX)
31361 return;
31362
31363 /* As a space optimization diagnose declarations of a class
31364 whose definition has been seen and purge the LOCVEC of
31365 all entries except the definition. */
31366 diag_mismatched_tags (type_decl);
31367 if (idxdef)
31368 {
31369 class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
31370 locvec.release ();
31371 locvec.reserve (2);
31372 locvec.safe_push (ent);
31373 idxdef = 0;
31374 }
31375 else
31376 /* Pop the entry pushed above for this declaration. */
31377 locvec.pop ();
31378 }
31379
31380 /* Issues -Wmismatched-tags for a single class. */
31381
31382 void
31383 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
31384 {
31385 if (!warn_mismatched_tags)
31386 return;
31387
31388 /* Number of uses of the class. */
31389 const unsigned ndecls = locvec.length ();
31390
31391 /* The class (or template) declaration guiding the decisions about
31392 the diagnostic. For ordinary classes it's the same as THIS. For
31393 uses of instantiations of templates other than their declarations
31394 it points to the record for the declaration of the corresponding
31395 primary template or partial specialization. */
31396 class_decl_loc_t *cdlguide = this;
31397
31398 tree type = TREE_TYPE (type_decl);
31399 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type))
31400 {
31401 /* For implicit instantiations of a primary template look up
31402 the primary or partial specialization and use it as
31403 the expected class-key rather than using the class-key of
31404 the first reference to the instantiation. The primary must
31405 be (and inevitably is) at index zero. */
31406 tree spec = specialization_of (type);
31407 cdlguide = class2loc.get (spec);
31408 gcc_assert (cdlguide != NULL);
31409 }
31410 else
31411 {
31412 /* Skip declarations that consistently use the same class-key. */
31413 if (def_class_key != none_type)
31414 return;
31415 }
31416
31417 /* Set if a definition for the class has been seen. */
31418 const bool def_p = cdlguide->def_p ();
31419
31420 /* The index of the declaration whose class-key this declaration
31421 is expected to match. It's either the class-key of the class
31422 definition if one exists or the first declaration otherwise. */
31423 const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
31424
31425 /* The class-key the class is expected to be declared with: it's
31426 either the key used in its definition or the first declaration
31427 if no definition has been provided.
31428 For implicit instantiations of a primary template it's
31429 the class-key used to declare the primary with. The primary
31430 must be at index zero. */
31431 const tag_types xpect_key = cdlguide->class_key (idxguide);
31432
31433 unsigned idx = 0;
31434 /* Advance IDX to the first declaration that either is not
31435 a definition or that doesn't match the first declaration
31436 if no definition is provided. */
31437 while (class_key (idx) == xpect_key)
31438 if (++idx == ndecls)
31439 return;
31440
31441 /* Save the current function before changing it below. */
31442 tree save_func = current_function_decl;
31443 /* Set the function declaration to print in diagnostic context. */
31444 current_function_decl = function (idx);
31445
31446 const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
31447 const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
31448
31449 location_t loc = location (idx);
31450 bool key_redundant_p = key_redundant (idx);
31451 auto_diagnostic_group d;
31452 /* Issue a warning for the first mismatched declaration.
31453 Avoid using "%#qT" since the class-key for the same type will
31454 be the same regardless of which one was used in the declaraion. */
31455 if (warning_at (loc, OPT_Wmismatched_tags,
31456 "%qT declared with a mismatched class-key %qs",
31457 type_decl, xmatchkstr))
31458 {
31459 /* Suggest how to avoid the warning for each instance since
31460 the guidance may be different depending on context. */
31461 inform (loc,
31462 (key_redundant_p
31463 ? G_("remove the class-key or replace it with %qs")
31464 : G_("replace the class-key with %qs")),
31465 xpectkstr);
31466
31467 /* Also point to the first declaration or definition that guided
31468 the decision to issue the warning above. */
31469 inform (cdlguide->location (idxguide),
31470 (def_p
31471 ? G_("%qT defined as %qs here")
31472 : G_("%qT first declared as %qs here")),
31473 type_decl, xpectkstr);
31474 }
31475
31476 /* Issue warnings for the remaining inconsistent declarations. */
31477 for (unsigned i = idx + 1; i != ndecls; ++i)
31478 {
31479 tag_types clskey = class_key (i);
31480 /* Skip over the declarations that match either the definition
31481 if one was provided or the first declaration. */
31482 if (clskey == xpect_key)
31483 continue;
31484
31485 loc = location (i);
31486 key_redundant_p = key_redundant (i);
31487 /* Set the function declaration to print in diagnostic context. */
31488 current_function_decl = function (i);
31489 if (warning_at (loc, OPT_Wmismatched_tags,
31490 "%qT declared with a mismatched class-key %qs",
31491 type_decl, xmatchkstr))
31492 /* Suggest how to avoid the warning for each instance since
31493 the guidance may be different depending on context. */
31494 inform (loc,
31495 (key_redundant_p
31496 ? G_("remove the class-key or replace it with %qs")
31497 : G_("replace the class-key with %qs")),
31498 xpectkstr);
31499 }
31500
31501 /* Restore the current function in case it was replaced above. */
31502 current_function_decl = save_func;
31503 }
31504
31505 /* Issues -Wmismatched-tags for all classes. Called at the end
31506 of processing a translation unit, after declarations of all class
31507 types and their uses have been recorded. */
31508
31509 void
31510 class_decl_loc_t::diag_mismatched_tags ()
31511 {
31512 /* CLASS2LOC should be empty if both -Wmismatched-tags and
31513 -Wredundant-tags are disabled. */
31514 gcc_assert (warn_mismatched_tags
31515 || warn_redundant_tags
31516 || class2loc.is_empty ());
31517
31518 /* Save the current function before changing on return. It should
31519 be null at this point. */
31520 temp_override<tree> cleanup (current_function_decl);
31521
31522 if (warn_mismatched_tags)
31523 {
31524 /* Iterate over the collected class/struct/template declarations. */
31525 typedef class_to_loc_map_t::iterator iter_t;
31526 for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
31527 {
31528 tree type_decl = (*it).first;
31529 class_decl_loc_t &recloc = (*it).second;
31530 recloc.diag_mismatched_tags (type_decl);
31531 }
31532 }
31533
31534 class2loc.empty ();
31535 }
31536
31537 /* Issue an error message if DECL is redeclared with different
31538 access than its original declaration [class.access.spec/3].
31539 This applies to nested classes, nested class templates and
31540 enumerations [class.mem/1]. */
31541
31542 static void
31543 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
31544 {
31545 if (!decl
31546 || (!CLASS_TYPE_P (TREE_TYPE (decl))
31547 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
31548 return;
31549
31550 if ((TREE_PRIVATE (decl)
31551 != (current_access_specifier == access_private_node))
31552 || (TREE_PROTECTED (decl)
31553 != (current_access_specifier == access_protected_node)))
31554 error_at (location, "%qD redeclared with different access", decl);
31555 }
31556
31557 /* Look for the `template' keyword, as a syntactic disambiguator.
31558 Return TRUE iff it is present, in which case it will be
31559 consumed. */
31560
31561 static bool
31562 cp_parser_optional_template_keyword (cp_parser *parser)
31563 {
31564 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
31565 {
31566 /* In C++98 the `template' keyword can only be used within templates;
31567 outside templates the parser can always figure out what is a
31568 template and what is not. In C++11, per the resolution of DR 468,
31569 `template' is allowed in cases where it is not strictly necessary. */
31570 if (!processing_template_decl
31571 && pedantic && cxx_dialect == cxx98)
31572 {
31573 cp_token *token = cp_lexer_peek_token (parser->lexer);
31574 pedwarn (token->location, OPT_Wpedantic,
31575 "in C++98 %<template%> (as a disambiguator) is only "
31576 "allowed within templates");
31577 /* If this part of the token stream is rescanned, the same
31578 error message would be generated. So, we purge the token
31579 from the stream. */
31580 cp_lexer_purge_token (parser->lexer);
31581 return false;
31582 }
31583 else
31584 {
31585 /* Consume the `template' keyword. */
31586 cp_lexer_consume_token (parser->lexer);
31587 return true;
31588 }
31589 }
31590 return false;
31591 }
31592
31593 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
31594 set PARSER->SCOPE, and perform other related actions. */
31595
31596 static void
31597 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
31598 {
31599 struct tree_check *check_value;
31600
31601 /* Get the stored value. */
31602 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
31603 /* Set the scope from the stored value. */
31604 parser->scope = saved_checks_value (check_value);
31605 parser->qualifying_scope = check_value->qualifying_scope;
31606 parser->object_scope = NULL_TREE;
31607 }
31608
31609 /* Consume tokens up through a non-nested END token. Returns TRUE if we
31610 encounter the end of a block before what we were looking for. */
31611
31612 static bool
31613 cp_parser_cache_group (cp_parser *parser,
31614 enum cpp_ttype end,
31615 unsigned depth)
31616 {
31617 while (true)
31618 {
31619 cp_token *token = cp_lexer_peek_token (parser->lexer);
31620
31621 /* Abort a parenthesized expression if we encounter a semicolon. */
31622 if ((end == CPP_CLOSE_PAREN || depth == 0)
31623 && token->type == CPP_SEMICOLON)
31624 return true;
31625 /* If we've reached the end of the file, stop. */
31626 if (token->type == CPP_EOF
31627 || (end != CPP_PRAGMA_EOL
31628 && token->type == CPP_PRAGMA_EOL))
31629 return true;
31630 if (token->type == CPP_CLOSE_BRACE && depth == 0)
31631 /* We've hit the end of an enclosing block, so there's been some
31632 kind of syntax error. */
31633 return true;
31634
31635 /* Consume the token. */
31636 cp_lexer_consume_token (parser->lexer);
31637 /* See if it starts a new group. */
31638 if (token->type == CPP_OPEN_BRACE)
31639 {
31640 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
31641 /* In theory this should probably check end == '}', but
31642 cp_parser_save_member_function_body needs it to exit
31643 after either '}' or ')' when called with ')'. */
31644 if (depth == 0)
31645 return false;
31646 }
31647 else if (token->type == CPP_OPEN_PAREN)
31648 {
31649 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
31650 if (depth == 0 && end == CPP_CLOSE_PAREN)
31651 return false;
31652 }
31653 else if (token->type == CPP_PRAGMA)
31654 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
31655 else if (token->type == end)
31656 return false;
31657 }
31658 }
31659
31660 /* Like above, for caching a default argument or NSDMI. Both of these are
31661 terminated by a non-nested comma, but it can be unclear whether or not a
31662 comma is nested in a template argument list unless we do more parsing.
31663 In order to handle this ambiguity, when we encounter a ',' after a '<'
31664 we try to parse what follows as a parameter-declaration-list (in the
31665 case of a default argument) or a member-declarator (in the case of an
31666 NSDMI). If that succeeds, then we stop caching. */
31667
31668 static tree
31669 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
31670 {
31671 unsigned depth = 0;
31672 int maybe_template_id = 0;
31673 cp_token *first_token;
31674 cp_token *token;
31675 tree default_argument;
31676
31677 /* Add tokens until we have processed the entire default
31678 argument. We add the range [first_token, token). */
31679 first_token = cp_lexer_peek_token (parser->lexer);
31680 if (first_token->type == CPP_OPEN_BRACE)
31681 {
31682 /* For list-initialization, this is straightforward. */
31683 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
31684 token = cp_lexer_peek_token (parser->lexer);
31685 }
31686 else while (true)
31687 {
31688 bool done = false;
31689
31690 /* Peek at the next token. */
31691 token = cp_lexer_peek_token (parser->lexer);
31692 /* What we do depends on what token we have. */
31693 switch (token->type)
31694 {
31695 /* In valid code, a default argument must be
31696 immediately followed by a `,' `)', or `...'. */
31697 case CPP_COMMA:
31698 if (depth == 0 && maybe_template_id)
31699 {
31700 /* If we've seen a '<', we might be in a
31701 template-argument-list. Until Core issue 325 is
31702 resolved, we don't know how this situation ought
31703 to be handled, so try to DTRT. We check whether
31704 what comes after the comma is a valid parameter
31705 declaration list. If it is, then the comma ends
31706 the default argument; otherwise the default
31707 argument continues. */
31708 bool error = false;
31709 cp_token *peek;
31710
31711 /* Set ITALP so cp_parser_parameter_declaration_list
31712 doesn't decide to commit to this parse. */
31713 bool saved_italp = parser->in_template_argument_list_p;
31714 parser->in_template_argument_list_p = true;
31715
31716 cp_parser_parse_tentatively (parser);
31717
31718 if (nsdmi)
31719 {
31720 /* Parse declarators until we reach a non-comma or
31721 somthing that cannot be an initializer.
31722 Just checking whether we're looking at a single
31723 declarator is insufficient. Consider:
31724 int var = tuple<T,U>::x;
31725 The template parameter 'U' looks exactly like a
31726 declarator. */
31727 do
31728 {
31729 int ctor_dtor_or_conv_p;
31730 cp_lexer_consume_token (parser->lexer);
31731 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31732 CP_PARSER_FLAGS_NONE,
31733 &ctor_dtor_or_conv_p,
31734 /*parenthesized_p=*/NULL,
31735 /*member_p=*/true,
31736 /*friend_p=*/false,
31737 /*static_p=*/false);
31738 peek = cp_lexer_peek_token (parser->lexer);
31739 if (cp_parser_error_occurred (parser))
31740 break;
31741 }
31742 while (peek->type == CPP_COMMA);
31743 /* If we met an '=' or ';' then the original comma
31744 was the end of the NSDMI. Otherwise assume
31745 we're still in the NSDMI. */
31746 error = (peek->type != CPP_EQ
31747 && peek->type != CPP_SEMICOLON);
31748 }
31749 else
31750 {
31751 cp_lexer_consume_token (parser->lexer);
31752 begin_scope (sk_function_parms, NULL_TREE);
31753 tree t = cp_parser_parameter_declaration_list
31754 (parser, CP_PARSER_FLAGS_NONE);
31755 if (t == error_mark_node)
31756 error = true;
31757 pop_bindings_and_leave_scope ();
31758 }
31759 if (!cp_parser_error_occurred (parser) && !error)
31760 done = true;
31761 cp_parser_abort_tentative_parse (parser);
31762
31763 parser->in_template_argument_list_p = saved_italp;
31764 break;
31765 }
31766 /* FALLTHRU */
31767 case CPP_CLOSE_PAREN:
31768 case CPP_ELLIPSIS:
31769 /* If we run into a non-nested `;', `}', or `]',
31770 then the code is invalid -- but the default
31771 argument is certainly over. */
31772 case CPP_SEMICOLON:
31773 case CPP_CLOSE_BRACE:
31774 case CPP_CLOSE_SQUARE:
31775 if (depth == 0
31776 /* Handle correctly int n = sizeof ... ( p ); */
31777 && token->type != CPP_ELLIPSIS)
31778 done = true;
31779 /* Update DEPTH, if necessary. */
31780 else if (token->type == CPP_CLOSE_PAREN
31781 || token->type == CPP_CLOSE_BRACE
31782 || token->type == CPP_CLOSE_SQUARE)
31783 --depth;
31784 break;
31785
31786 case CPP_OPEN_PAREN:
31787 case CPP_OPEN_SQUARE:
31788 case CPP_OPEN_BRACE:
31789 ++depth;
31790 break;
31791
31792 case CPP_LESS:
31793 if (depth == 0)
31794 /* This might be the comparison operator, or it might
31795 start a template argument list. */
31796 ++maybe_template_id;
31797 break;
31798
31799 case CPP_RSHIFT:
31800 if (cxx_dialect == cxx98)
31801 break;
31802 /* Fall through for C++0x, which treats the `>>'
31803 operator like two `>' tokens in certain
31804 cases. */
31805 gcc_fallthrough ();
31806
31807 case CPP_GREATER:
31808 if (depth == 0)
31809 {
31810 /* This might be an operator, or it might close a
31811 template argument list. But if a previous '<'
31812 started a template argument list, this will have
31813 closed it, so we can't be in one anymore. */
31814 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
31815 if (maybe_template_id < 0)
31816 maybe_template_id = 0;
31817 }
31818 break;
31819
31820 /* If we run out of tokens, issue an error message. */
31821 case CPP_EOF:
31822 case CPP_PRAGMA_EOL:
31823 error_at (token->location, "file ends in default argument");
31824 return error_mark_node;
31825
31826 case CPP_NAME:
31827 case CPP_SCOPE:
31828 /* In these cases, we should look for template-ids.
31829 For example, if the default argument is
31830 `X<int, double>()', we need to do name lookup to
31831 figure out whether or not `X' is a template; if
31832 so, the `,' does not end the default argument.
31833
31834 That is not yet done. */
31835 break;
31836
31837 default:
31838 break;
31839 }
31840
31841 /* If we've reached the end, stop. */
31842 if (done)
31843 break;
31844
31845 /* Add the token to the token block. */
31846 token = cp_lexer_consume_token (parser->lexer);
31847 }
31848
31849 /* Create a DEFERRED_PARSE to represent the unparsed default
31850 argument. */
31851 default_argument = make_node (DEFERRED_PARSE);
31852 DEFPARSE_TOKENS (default_argument)
31853 = cp_token_cache_new (first_token, token);
31854 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
31855
31856 return default_argument;
31857 }
31858
31859 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
31860
31861 location_t
31862 defparse_location (tree default_argument)
31863 {
31864 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
31865 location_t start = tokens->first->location;
31866 location_t end = tokens->last->location;
31867 return make_location (start, start, end);
31868 }
31869
31870 /* Begin parsing tentatively. We always save tokens while parsing
31871 tentatively so that if the tentative parsing fails we can restore the
31872 tokens. */
31873
31874 static void
31875 cp_parser_parse_tentatively (cp_parser* parser)
31876 {
31877 /* Enter a new parsing context. */
31878 parser->context = cp_parser_context_new (parser->context);
31879 /* Begin saving tokens. */
31880 cp_lexer_save_tokens (parser->lexer);
31881 /* In order to avoid repetitive access control error messages,
31882 access checks are queued up until we are no longer parsing
31883 tentatively. */
31884 push_deferring_access_checks (dk_deferred);
31885 }
31886
31887 /* Commit to the currently active tentative parse. */
31888
31889 static void
31890 cp_parser_commit_to_tentative_parse (cp_parser* parser)
31891 {
31892 cp_parser_context *context;
31893 cp_lexer *lexer;
31894
31895 /* Mark all of the levels as committed. */
31896 lexer = parser->lexer;
31897 for (context = parser->context; context->next; context = context->next)
31898 {
31899 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
31900 break;
31901 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
31902 while (!cp_lexer_saving_tokens (lexer))
31903 lexer = lexer->next;
31904 cp_lexer_commit_tokens (lexer);
31905 }
31906 }
31907
31908 /* Commit to the topmost currently active tentative parse.
31909
31910 Note that this function shouldn't be called when there are
31911 irreversible side-effects while in a tentative state. For
31912 example, we shouldn't create a permanent entry in the symbol
31913 table, or issue an error message that might not apply if the
31914 tentative parse is aborted. */
31915
31916 static void
31917 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
31918 {
31919 cp_parser_context *context = parser->context;
31920 cp_lexer *lexer = parser->lexer;
31921
31922 if (context)
31923 {
31924 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
31925 return;
31926 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
31927
31928 while (!cp_lexer_saving_tokens (lexer))
31929 lexer = lexer->next;
31930 cp_lexer_commit_tokens (lexer);
31931 }
31932 }
31933
31934 /* Abort the currently active tentative parse. All consumed tokens
31935 will be rolled back, and no diagnostics will be issued. */
31936
31937 static void
31938 cp_parser_abort_tentative_parse (cp_parser* parser)
31939 {
31940 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
31941 || errorcount > 0);
31942 cp_parser_simulate_error (parser);
31943 /* Now, pretend that we want to see if the construct was
31944 successfully parsed. */
31945 cp_parser_parse_definitely (parser);
31946 }
31947
31948 /* Stop parsing tentatively. If a parse error has occurred, restore the
31949 token stream. Otherwise, commit to the tokens we have consumed.
31950 Returns true if no error occurred; false otherwise. */
31951
31952 static bool
31953 cp_parser_parse_definitely (cp_parser* parser)
31954 {
31955 bool error_occurred;
31956 cp_parser_context *context;
31957
31958 /* Remember whether or not an error occurred, since we are about to
31959 destroy that information. */
31960 error_occurred = cp_parser_error_occurred (parser);
31961 /* Remove the topmost context from the stack. */
31962 context = parser->context;
31963 parser->context = context->next;
31964 /* If no parse errors occurred, commit to the tentative parse. */
31965 if (!error_occurred)
31966 {
31967 /* Commit to the tokens read tentatively, unless that was
31968 already done. */
31969 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
31970 cp_lexer_commit_tokens (parser->lexer);
31971
31972 pop_to_parent_deferring_access_checks ();
31973 }
31974 /* Otherwise, if errors occurred, roll back our state so that things
31975 are just as they were before we began the tentative parse. */
31976 else
31977 {
31978 cp_lexer_rollback_tokens (parser->lexer);
31979 pop_deferring_access_checks ();
31980 }
31981 /* Add the context to the front of the free list. */
31982 context->next = cp_parser_context_free_list;
31983 cp_parser_context_free_list = context;
31984
31985 return !error_occurred;
31986 }
31987
31988 /* Returns true if we are parsing tentatively and are not committed to
31989 this tentative parse. */
31990
31991 static bool
31992 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
31993 {
31994 return (cp_parser_parsing_tentatively (parser)
31995 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
31996 }
31997
31998 /* Returns nonzero iff an error has occurred during the most recent
31999 tentative parse. */
32000
32001 static bool
32002 cp_parser_error_occurred (cp_parser* parser)
32003 {
32004 return (cp_parser_parsing_tentatively (parser)
32005 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
32006 }
32007
32008 /* Returns nonzero if GNU extensions are allowed. */
32009
32010 static bool
32011 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
32012 {
32013 return parser->allow_gnu_extensions_p;
32014 }
32015 \f
32016 /* Objective-C++ Productions */
32017
32018
32019 /* Parse an Objective-C expression, which feeds into a primary-expression
32020 above.
32021
32022 objc-expression:
32023 objc-message-expression
32024 objc-string-literal
32025 objc-encode-expression
32026 objc-protocol-expression
32027 objc-selector-expression
32028
32029 Returns a tree representation of the expression. */
32030
32031 static cp_expr
32032 cp_parser_objc_expression (cp_parser* parser)
32033 {
32034 /* Try to figure out what kind of declaration is present. */
32035 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
32036
32037 switch (kwd->type)
32038 {
32039 case CPP_OPEN_SQUARE:
32040 return cp_parser_objc_message_expression (parser);
32041
32042 case CPP_OBJC_STRING:
32043 kwd = cp_lexer_consume_token (parser->lexer);
32044 return objc_build_string_object (kwd->u.value);
32045
32046 case CPP_KEYWORD:
32047 switch (kwd->keyword)
32048 {
32049 case RID_AT_ENCODE:
32050 return cp_parser_objc_encode_expression (parser);
32051
32052 case RID_AT_PROTOCOL:
32053 return cp_parser_objc_protocol_expression (parser);
32054
32055 case RID_AT_SELECTOR:
32056 return cp_parser_objc_selector_expression (parser);
32057
32058 default:
32059 break;
32060 }
32061 /* FALLTHRU */
32062 default:
32063 error_at (kwd->location,
32064 "misplaced %<@%D%> Objective-C++ construct",
32065 kwd->u.value);
32066 cp_parser_skip_to_end_of_block_or_statement (parser);
32067 }
32068
32069 return error_mark_node;
32070 }
32071
32072 /* Parse an Objective-C message expression.
32073
32074 objc-message-expression:
32075 [ objc-message-receiver objc-message-args ]
32076
32077 Returns a representation of an Objective-C message. */
32078
32079 static tree
32080 cp_parser_objc_message_expression (cp_parser* parser)
32081 {
32082 tree receiver, messageargs;
32083
32084 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
32085 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
32086 receiver = cp_parser_objc_message_receiver (parser);
32087 messageargs = cp_parser_objc_message_args (parser);
32088 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
32089 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32090
32091 tree result = objc_build_message_expr (receiver, messageargs);
32092
32093 /* Construct a location e.g.
32094 [self func1:5]
32095 ^~~~~~~~~~~~~~
32096 ranging from the '[' to the ']', with the caret at the start. */
32097 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
32098 protected_set_expr_location (result, combined_loc);
32099
32100 return result;
32101 }
32102
32103 /* Parse an objc-message-receiver.
32104
32105 objc-message-receiver:
32106 expression
32107 simple-type-specifier
32108
32109 Returns a representation of the type or expression. */
32110
32111 static tree
32112 cp_parser_objc_message_receiver (cp_parser* parser)
32113 {
32114 tree rcv;
32115
32116 /* An Objective-C message receiver may be either (1) a type
32117 or (2) an expression. */
32118 cp_parser_parse_tentatively (parser);
32119 rcv = cp_parser_expression (parser);
32120
32121 /* If that worked out, fine. */
32122 if (cp_parser_parse_definitely (parser))
32123 return rcv;
32124
32125 cp_parser_parse_tentatively (parser);
32126 rcv = cp_parser_simple_type_specifier (parser,
32127 /*decl_specs=*/NULL,
32128 CP_PARSER_FLAGS_NONE);
32129
32130 if (cp_parser_parse_definitely (parser))
32131 return objc_get_class_reference (rcv);
32132
32133 cp_parser_error (parser, "objective-c++ message receiver expected");
32134 return error_mark_node;
32135 }
32136
32137 /* Parse the arguments and selectors comprising an Objective-C message.
32138
32139 objc-message-args:
32140 objc-selector
32141 objc-selector-args
32142 objc-selector-args , objc-comma-args
32143
32144 objc-selector-args:
32145 objc-selector [opt] : assignment-expression
32146 objc-selector-args objc-selector [opt] : assignment-expression
32147
32148 objc-comma-args:
32149 assignment-expression
32150 objc-comma-args , assignment-expression
32151
32152 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
32153 selector arguments and TREE_VALUE containing a list of comma
32154 arguments. */
32155
32156 static tree
32157 cp_parser_objc_message_args (cp_parser* parser)
32158 {
32159 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
32160 bool maybe_unary_selector_p = true;
32161 cp_token *token = cp_lexer_peek_token (parser->lexer);
32162
32163 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
32164 {
32165 tree selector = NULL_TREE, arg;
32166
32167 if (token->type != CPP_COLON)
32168 selector = cp_parser_objc_selector (parser);
32169
32170 /* Detect if we have a unary selector. */
32171 if (maybe_unary_selector_p
32172 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
32173 return build_tree_list (selector, NULL_TREE);
32174
32175 maybe_unary_selector_p = false;
32176 cp_parser_require (parser, CPP_COLON, RT_COLON);
32177 arg = cp_parser_assignment_expression (parser);
32178
32179 sel_args
32180 = chainon (sel_args,
32181 build_tree_list (selector, arg));
32182
32183 token = cp_lexer_peek_token (parser->lexer);
32184 }
32185
32186 /* Handle non-selector arguments, if any. */
32187 while (token->type == CPP_COMMA)
32188 {
32189 tree arg;
32190
32191 cp_lexer_consume_token (parser->lexer);
32192 arg = cp_parser_assignment_expression (parser);
32193
32194 addl_args
32195 = chainon (addl_args,
32196 build_tree_list (NULL_TREE, arg));
32197
32198 token = cp_lexer_peek_token (parser->lexer);
32199 }
32200
32201 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
32202 {
32203 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
32204 return build_tree_list (error_mark_node, error_mark_node);
32205 }
32206
32207 return build_tree_list (sel_args, addl_args);
32208 }
32209
32210 /* Parse an Objective-C encode expression.
32211
32212 objc-encode-expression:
32213 @encode objc-typename
32214
32215 Returns an encoded representation of the type argument. */
32216
32217 static cp_expr
32218 cp_parser_objc_encode_expression (cp_parser* parser)
32219 {
32220 tree type;
32221 cp_token *token;
32222 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
32223
32224 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
32225 matching_parens parens;
32226 parens.require_open (parser);
32227 token = cp_lexer_peek_token (parser->lexer);
32228 type = complete_type (cp_parser_type_id (parser));
32229 parens.require_close (parser);
32230
32231 if (!type)
32232 {
32233 error_at (token->location,
32234 "%<@encode%> must specify a type as an argument");
32235 return error_mark_node;
32236 }
32237
32238 /* This happens if we find @encode(T) (where T is a template
32239 typename or something dependent on a template typename) when
32240 parsing a template. In that case, we can't compile it
32241 immediately, but we rather create an AT_ENCODE_EXPR which will
32242 need to be instantiated when the template is used.
32243 */
32244 if (dependent_type_p (type))
32245 {
32246 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
32247 TREE_READONLY (value) = 1;
32248 return value;
32249 }
32250
32251
32252 /* Build a location of the form:
32253 @encode(int)
32254 ^~~~~~~~~~~~
32255 with caret==start at the @ token, finishing at the close paren. */
32256 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
32257
32258 return cp_expr (objc_build_encode_expr (type), combined_loc);
32259 }
32260
32261 /* Parse an Objective-C @defs expression. */
32262
32263 static tree
32264 cp_parser_objc_defs_expression (cp_parser *parser)
32265 {
32266 tree name;
32267
32268 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
32269 matching_parens parens;
32270 parens.require_open (parser);
32271 name = cp_parser_identifier (parser);
32272 parens.require_close (parser);
32273
32274 return objc_get_class_ivars (name);
32275 }
32276
32277 /* Parse an Objective-C protocol expression.
32278
32279 objc-protocol-expression:
32280 @protocol ( identifier )
32281
32282 Returns a representation of the protocol expression. */
32283
32284 static tree
32285 cp_parser_objc_protocol_expression (cp_parser* parser)
32286 {
32287 tree proto;
32288 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
32289
32290 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
32291 matching_parens parens;
32292 parens.require_open (parser);
32293 proto = cp_parser_identifier (parser);
32294 parens.require_close (parser);
32295
32296 /* Build a location of the form:
32297 @protocol(prot)
32298 ^~~~~~~~~~~~~~~
32299 with caret==start at the @ token, finishing at the close paren. */
32300 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
32301 tree result = objc_build_protocol_expr (proto);
32302 protected_set_expr_location (result, combined_loc);
32303 return result;
32304 }
32305
32306 /* Parse an Objective-C selector expression.
32307
32308 objc-selector-expression:
32309 @selector ( objc-method-signature )
32310
32311 objc-method-signature:
32312 objc-selector
32313 objc-selector-seq
32314
32315 objc-selector-seq:
32316 objc-selector :
32317 objc-selector-seq objc-selector :
32318
32319 Returns a representation of the method selector. */
32320
32321 static tree
32322 cp_parser_objc_selector_expression (cp_parser* parser)
32323 {
32324 tree sel_seq = NULL_TREE;
32325 bool maybe_unary_selector_p = true;
32326 cp_token *token;
32327 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32328
32329 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
32330 matching_parens parens;
32331 parens.require_open (parser);
32332 token = cp_lexer_peek_token (parser->lexer);
32333
32334 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
32335 || token->type == CPP_SCOPE)
32336 {
32337 tree selector = NULL_TREE;
32338
32339 if (token->type != CPP_COLON
32340 || token->type == CPP_SCOPE)
32341 selector = cp_parser_objc_selector (parser);
32342
32343 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
32344 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
32345 {
32346 /* Detect if we have a unary selector. */
32347 if (maybe_unary_selector_p)
32348 {
32349 sel_seq = selector;
32350 goto finish_selector;
32351 }
32352 else
32353 {
32354 cp_parser_error (parser, "expected %<:%>");
32355 }
32356 }
32357 maybe_unary_selector_p = false;
32358 token = cp_lexer_consume_token (parser->lexer);
32359
32360 if (token->type == CPP_SCOPE)
32361 {
32362 sel_seq
32363 = chainon (sel_seq,
32364 build_tree_list (selector, NULL_TREE));
32365 sel_seq
32366 = chainon (sel_seq,
32367 build_tree_list (NULL_TREE, NULL_TREE));
32368 }
32369 else
32370 sel_seq
32371 = chainon (sel_seq,
32372 build_tree_list (selector, NULL_TREE));
32373
32374 token = cp_lexer_peek_token (parser->lexer);
32375 }
32376
32377 finish_selector:
32378 parens.require_close (parser);
32379
32380
32381 /* Build a location of the form:
32382 @selector(func)
32383 ^~~~~~~~~~~~~~~
32384 with caret==start at the @ token, finishing at the close paren. */
32385 location_t combined_loc = make_location (loc, loc, parser->lexer);
32386 tree result = objc_build_selector_expr (combined_loc, sel_seq);
32387 /* TODO: objc_build_selector_expr doesn't always honor the location. */
32388 protected_set_expr_location (result, combined_loc);
32389 return result;
32390 }
32391
32392 /* Parse a list of identifiers.
32393
32394 objc-identifier-list:
32395 identifier
32396 objc-identifier-list , identifier
32397
32398 Returns a TREE_LIST of identifier nodes. */
32399
32400 static tree
32401 cp_parser_objc_identifier_list (cp_parser* parser)
32402 {
32403 tree identifier;
32404 tree list;
32405 cp_token *sep;
32406
32407 identifier = cp_parser_identifier (parser);
32408 if (identifier == error_mark_node)
32409 return error_mark_node;
32410
32411 list = build_tree_list (NULL_TREE, identifier);
32412 sep = cp_lexer_peek_token (parser->lexer);
32413
32414 while (sep->type == CPP_COMMA)
32415 {
32416 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
32417 identifier = cp_parser_identifier (parser);
32418 if (identifier == error_mark_node)
32419 return list;
32420
32421 list = chainon (list, build_tree_list (NULL_TREE,
32422 identifier));
32423 sep = cp_lexer_peek_token (parser->lexer);
32424 }
32425
32426 return list;
32427 }
32428
32429 /* Parse an Objective-C alias declaration.
32430
32431 objc-alias-declaration:
32432 @compatibility_alias identifier identifier ;
32433
32434 This function registers the alias mapping with the Objective-C front end.
32435 It returns nothing. */
32436
32437 static void
32438 cp_parser_objc_alias_declaration (cp_parser* parser)
32439 {
32440 tree alias, orig;
32441
32442 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
32443 alias = cp_parser_identifier (parser);
32444 orig = cp_parser_identifier (parser);
32445 objc_declare_alias (alias, orig);
32446 cp_parser_consume_semicolon_at_end_of_statement (parser);
32447 }
32448
32449 /* Parse an Objective-C class forward-declaration.
32450
32451 objc-class-declaration:
32452 @class objc-identifier-list ;
32453
32454 The function registers the forward declarations with the Objective-C
32455 front end. It returns nothing. */
32456
32457 static void
32458 cp_parser_objc_class_declaration (cp_parser* parser)
32459 {
32460 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
32461 while (true)
32462 {
32463 tree id;
32464
32465 id = cp_parser_identifier (parser);
32466 if (id == error_mark_node)
32467 break;
32468
32469 objc_declare_class (id);
32470
32471 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32472 cp_lexer_consume_token (parser->lexer);
32473 else
32474 break;
32475 }
32476 cp_parser_consume_semicolon_at_end_of_statement (parser);
32477 }
32478
32479 /* Parse a list of Objective-C protocol references.
32480
32481 objc-protocol-refs-opt:
32482 objc-protocol-refs [opt]
32483
32484 objc-protocol-refs:
32485 < objc-identifier-list >
32486
32487 Returns a TREE_LIST of identifiers, if any. */
32488
32489 static tree
32490 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
32491 {
32492 tree protorefs = NULL_TREE;
32493
32494 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
32495 {
32496 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
32497 protorefs = cp_parser_objc_identifier_list (parser);
32498 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
32499 }
32500
32501 return protorefs;
32502 }
32503
32504 /* Parse a Objective-C visibility specification. */
32505
32506 static void
32507 cp_parser_objc_visibility_spec (cp_parser* parser)
32508 {
32509 cp_token *vis = cp_lexer_peek_token (parser->lexer);
32510
32511 switch (vis->keyword)
32512 {
32513 case RID_AT_PRIVATE:
32514 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
32515 break;
32516 case RID_AT_PROTECTED:
32517 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
32518 break;
32519 case RID_AT_PUBLIC:
32520 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
32521 break;
32522 case RID_AT_PACKAGE:
32523 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
32524 break;
32525 default:
32526 return;
32527 }
32528
32529 /* Eat '@private'/'@protected'/'@public'. */
32530 cp_lexer_consume_token (parser->lexer);
32531 }
32532
32533 /* Parse an Objective-C method type. Return 'true' if it is a class
32534 (+) method, and 'false' if it is an instance (-) method. */
32535
32536 static inline bool
32537 cp_parser_objc_method_type (cp_parser* parser)
32538 {
32539 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
32540 return true;
32541 else
32542 return false;
32543 }
32544
32545 /* Parse an Objective-C protocol qualifier. */
32546
32547 static tree
32548 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
32549 {
32550 tree quals = NULL_TREE, node;
32551 cp_token *token = cp_lexer_peek_token (parser->lexer);
32552
32553 node = token->u.value;
32554
32555 while (node && identifier_p (node)
32556 && (node == ridpointers [(int) RID_IN]
32557 || node == ridpointers [(int) RID_OUT]
32558 || node == ridpointers [(int) RID_INOUT]
32559 || node == ridpointers [(int) RID_BYCOPY]
32560 || node == ridpointers [(int) RID_BYREF]
32561 || node == ridpointers [(int) RID_ONEWAY]))
32562 {
32563 quals = tree_cons (NULL_TREE, node, quals);
32564 cp_lexer_consume_token (parser->lexer);
32565 token = cp_lexer_peek_token (parser->lexer);
32566 node = token->u.value;
32567 }
32568
32569 return quals;
32570 }
32571
32572 /* Parse an Objective-C typename. */
32573
32574 static tree
32575 cp_parser_objc_typename (cp_parser* parser)
32576 {
32577 tree type_name = NULL_TREE;
32578
32579 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32580 {
32581 tree proto_quals, cp_type = NULL_TREE;
32582
32583 matching_parens parens;
32584 parens.consume_open (parser); /* Eat '('. */
32585 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
32586
32587 /* An ObjC type name may consist of just protocol qualifiers, in which
32588 case the type shall default to 'id'. */
32589 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
32590 {
32591 cp_type = cp_parser_type_id (parser);
32592
32593 /* If the type could not be parsed, an error has already
32594 been produced. For error recovery, behave as if it had
32595 not been specified, which will use the default type
32596 'id'. */
32597 if (cp_type == error_mark_node)
32598 {
32599 cp_type = NULL_TREE;
32600 /* We need to skip to the closing parenthesis as
32601 cp_parser_type_id() does not seem to do it for
32602 us. */
32603 cp_parser_skip_to_closing_parenthesis (parser,
32604 /*recovering=*/true,
32605 /*or_comma=*/false,
32606 /*consume_paren=*/false);
32607 }
32608 }
32609
32610 parens.require_close (parser);
32611 type_name = build_tree_list (proto_quals, cp_type);
32612 }
32613
32614 return type_name;
32615 }
32616
32617 /* Check to see if TYPE refers to an Objective-C selector name. */
32618
32619 static bool
32620 cp_parser_objc_selector_p (enum cpp_ttype type)
32621 {
32622 return (type == CPP_NAME || type == CPP_KEYWORD
32623 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
32624 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
32625 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
32626 || type == CPP_XOR || type == CPP_XOR_EQ);
32627 }
32628
32629 /* Parse an Objective-C selector. */
32630
32631 static tree
32632 cp_parser_objc_selector (cp_parser* parser)
32633 {
32634 cp_token *token = cp_lexer_consume_token (parser->lexer);
32635
32636 if (!cp_parser_objc_selector_p (token->type))
32637 {
32638 error_at (token->location, "invalid Objective-C++ selector name");
32639 return error_mark_node;
32640 }
32641
32642 /* C++ operator names are allowed to appear in ObjC selectors. */
32643 switch (token->type)
32644 {
32645 case CPP_AND_AND: return get_identifier ("and");
32646 case CPP_AND_EQ: return get_identifier ("and_eq");
32647 case CPP_AND: return get_identifier ("bitand");
32648 case CPP_OR: return get_identifier ("bitor");
32649 case CPP_COMPL: return get_identifier ("compl");
32650 case CPP_NOT: return get_identifier ("not");
32651 case CPP_NOT_EQ: return get_identifier ("not_eq");
32652 case CPP_OR_OR: return get_identifier ("or");
32653 case CPP_OR_EQ: return get_identifier ("or_eq");
32654 case CPP_XOR: return get_identifier ("xor");
32655 case CPP_XOR_EQ: return get_identifier ("xor_eq");
32656 default: return token->u.value;
32657 }
32658 }
32659
32660 /* Parse an Objective-C params list. */
32661
32662 static tree
32663 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
32664 {
32665 tree params = NULL_TREE;
32666 bool maybe_unary_selector_p = true;
32667 cp_token *token = cp_lexer_peek_token (parser->lexer);
32668
32669 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
32670 {
32671 tree selector = NULL_TREE, type_name, identifier;
32672 tree parm_attr = NULL_TREE;
32673
32674 if (token->keyword == RID_ATTRIBUTE)
32675 break;
32676
32677 if (token->type != CPP_COLON)
32678 selector = cp_parser_objc_selector (parser);
32679
32680 /* Detect if we have a unary selector. */
32681 if (maybe_unary_selector_p
32682 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
32683 {
32684 params = selector; /* Might be followed by attributes. */
32685 break;
32686 }
32687
32688 maybe_unary_selector_p = false;
32689 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32690 {
32691 /* Something went quite wrong. There should be a colon
32692 here, but there is not. Stop parsing parameters. */
32693 break;
32694 }
32695 type_name = cp_parser_objc_typename (parser);
32696 /* New ObjC allows attributes on parameters too. */
32697 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
32698 parm_attr = cp_parser_attributes_opt (parser);
32699 identifier = cp_parser_identifier (parser);
32700
32701 params
32702 = chainon (params,
32703 objc_build_keyword_decl (selector,
32704 type_name,
32705 identifier,
32706 parm_attr));
32707
32708 token = cp_lexer_peek_token (parser->lexer);
32709 }
32710
32711 if (params == NULL_TREE)
32712 {
32713 cp_parser_error (parser, "objective-c++ method declaration is expected");
32714 return error_mark_node;
32715 }
32716
32717 /* We allow tail attributes for the method. */
32718 if (token->keyword == RID_ATTRIBUTE)
32719 {
32720 *attributes = cp_parser_attributes_opt (parser);
32721 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
32722 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32723 return params;
32724 cp_parser_error (parser,
32725 "method attributes must be specified at the end");
32726 return error_mark_node;
32727 }
32728
32729 if (params == NULL_TREE)
32730 {
32731 cp_parser_error (parser, "objective-c++ method declaration is expected");
32732 return error_mark_node;
32733 }
32734 return params;
32735 }
32736
32737 /* Parse the non-keyword Objective-C params. */
32738
32739 static tree
32740 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
32741 tree* attributes)
32742 {
32743 tree params = make_node (TREE_LIST);
32744 cp_token *token = cp_lexer_peek_token (parser->lexer);
32745 *ellipsisp = false; /* Initially, assume no ellipsis. */
32746
32747 while (token->type == CPP_COMMA)
32748 {
32749 cp_parameter_declarator *parmdecl;
32750 tree parm;
32751
32752 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
32753 token = cp_lexer_peek_token (parser->lexer);
32754
32755 if (token->type == CPP_ELLIPSIS)
32756 {
32757 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
32758 *ellipsisp = true;
32759 token = cp_lexer_peek_token (parser->lexer);
32760 break;
32761 }
32762
32763 /* TODO: parse attributes for tail parameters. */
32764 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
32765 false, NULL);
32766 parm = grokdeclarator (parmdecl->declarator,
32767 &parmdecl->decl_specifiers,
32768 PARM, /*initialized=*/0,
32769 /*attrlist=*/NULL);
32770
32771 chainon (params, build_tree_list (NULL_TREE, parm));
32772 token = cp_lexer_peek_token (parser->lexer);
32773 }
32774
32775 /* We allow tail attributes for the method. */
32776 if (token->keyword == RID_ATTRIBUTE)
32777 {
32778 if (*attributes == NULL_TREE)
32779 {
32780 *attributes = cp_parser_attributes_opt (parser);
32781 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
32782 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32783 return params;
32784 }
32785 else
32786 /* We have an error, but parse the attributes, so that we can
32787 carry on. */
32788 *attributes = cp_parser_attributes_opt (parser);
32789
32790 cp_parser_error (parser,
32791 "method attributes must be specified at the end");
32792 return error_mark_node;
32793 }
32794
32795 return params;
32796 }
32797
32798 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
32799
32800 static void
32801 cp_parser_objc_interstitial_code (cp_parser* parser)
32802 {
32803 cp_token *token = cp_lexer_peek_token (parser->lexer);
32804
32805 /* If the next token is `extern' and the following token is a string
32806 literal, then we have a linkage specification. */
32807 if (token->keyword == RID_EXTERN
32808 && cp_parser_is_pure_string_literal
32809 (cp_lexer_peek_nth_token (parser->lexer, 2)))
32810 cp_parser_linkage_specification (parser);
32811 /* Handle #pragma, if any. */
32812 else if (token->type == CPP_PRAGMA)
32813 cp_parser_pragma (parser, pragma_objc_icode, NULL);
32814 /* Allow stray semicolons. */
32815 else if (token->type == CPP_SEMICOLON)
32816 cp_lexer_consume_token (parser->lexer);
32817 /* Mark methods as optional or required, when building protocols. */
32818 else if (token->keyword == RID_AT_OPTIONAL)
32819 {
32820 cp_lexer_consume_token (parser->lexer);
32821 objc_set_method_opt (true);
32822 }
32823 else if (token->keyword == RID_AT_REQUIRED)
32824 {
32825 cp_lexer_consume_token (parser->lexer);
32826 objc_set_method_opt (false);
32827 }
32828 else if (token->keyword == RID_NAMESPACE)
32829 cp_parser_namespace_definition (parser);
32830 /* Other stray characters must generate errors. */
32831 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
32832 {
32833 cp_lexer_consume_token (parser->lexer);
32834 error ("stray %qs between Objective-C++ methods",
32835 token->type == CPP_OPEN_BRACE ? "{" : "}");
32836 }
32837 /* Finally, try to parse a block-declaration, or a function-definition. */
32838 else
32839 cp_parser_block_declaration (parser, /*statement_p=*/false);
32840 }
32841
32842 /* Parse a method signature. */
32843
32844 static tree
32845 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
32846 {
32847 tree rettype, kwdparms, optparms;
32848 bool ellipsis = false;
32849 bool is_class_method;
32850
32851 is_class_method = cp_parser_objc_method_type (parser);
32852 rettype = cp_parser_objc_typename (parser);
32853 *attributes = NULL_TREE;
32854 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
32855 if (kwdparms == error_mark_node)
32856 return error_mark_node;
32857 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
32858 if (optparms == error_mark_node)
32859 return error_mark_node;
32860
32861 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
32862 }
32863
32864 static bool
32865 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
32866 {
32867 tree tattr;
32868 cp_lexer_save_tokens (parser->lexer);
32869 tattr = cp_parser_attributes_opt (parser);
32870 gcc_assert (tattr) ;
32871
32872 /* If the attributes are followed by a method introducer, this is not allowed.
32873 Dump the attributes and flag the situation. */
32874 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
32875 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32876 return true;
32877
32878 /* Otherwise, the attributes introduce some interstitial code, possibly so
32879 rewind to allow that check. */
32880 cp_lexer_rollback_tokens (parser->lexer);
32881 return false;
32882 }
32883
32884 /* Parse an Objective-C method prototype list. */
32885
32886 static void
32887 cp_parser_objc_method_prototype_list (cp_parser* parser)
32888 {
32889 cp_token *token = cp_lexer_peek_token (parser->lexer);
32890
32891 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
32892 {
32893 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
32894 {
32895 tree attributes, sig;
32896 bool is_class_method;
32897 if (token->type == CPP_PLUS)
32898 is_class_method = true;
32899 else
32900 is_class_method = false;
32901 sig = cp_parser_objc_method_signature (parser, &attributes);
32902 if (sig == error_mark_node)
32903 {
32904 cp_parser_skip_to_end_of_block_or_statement (parser);
32905 token = cp_lexer_peek_token (parser->lexer);
32906 continue;
32907 }
32908 objc_add_method_declaration (is_class_method, sig, attributes);
32909 cp_parser_consume_semicolon_at_end_of_statement (parser);
32910 }
32911 else if (token->keyword == RID_AT_PROPERTY)
32912 cp_parser_objc_at_property_declaration (parser);
32913 else if (token->keyword == RID_ATTRIBUTE
32914 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
32915 warning_at (cp_lexer_peek_token (parser->lexer)->location,
32916 OPT_Wattributes,
32917 "prefix attributes are ignored for methods");
32918 else
32919 /* Allow for interspersed non-ObjC++ code. */
32920 cp_parser_objc_interstitial_code (parser);
32921
32922 token = cp_lexer_peek_token (parser->lexer);
32923 }
32924
32925 if (token->type != CPP_EOF)
32926 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
32927 else
32928 cp_parser_error (parser, "expected %<@end%>");
32929
32930 objc_finish_interface ();
32931 }
32932
32933 /* Parse an Objective-C method definition list. */
32934
32935 static void
32936 cp_parser_objc_method_definition_list (cp_parser* parser)
32937 {
32938 cp_token *token = cp_lexer_peek_token (parser->lexer);
32939
32940 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
32941 {
32942 tree meth;
32943
32944 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
32945 {
32946 cp_token *ptk;
32947 tree sig, attribute;
32948 bool is_class_method;
32949 if (token->type == CPP_PLUS)
32950 is_class_method = true;
32951 else
32952 is_class_method = false;
32953 push_deferring_access_checks (dk_deferred);
32954 sig = cp_parser_objc_method_signature (parser, &attribute);
32955 if (sig == error_mark_node)
32956 {
32957 cp_parser_skip_to_end_of_block_or_statement (parser);
32958 token = cp_lexer_peek_token (parser->lexer);
32959 continue;
32960 }
32961 objc_start_method_definition (is_class_method, sig, attribute,
32962 NULL_TREE);
32963
32964 /* For historical reasons, we accept an optional semicolon. */
32965 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
32966 cp_lexer_consume_token (parser->lexer);
32967
32968 ptk = cp_lexer_peek_token (parser->lexer);
32969 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
32970 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
32971 {
32972 perform_deferred_access_checks (tf_warning_or_error);
32973 stop_deferring_access_checks ();
32974 meth = cp_parser_function_definition_after_declarator (parser,
32975 false);
32976 pop_deferring_access_checks ();
32977 objc_finish_method_definition (meth);
32978 }
32979 }
32980 /* The following case will be removed once @synthesize is
32981 completely implemented. */
32982 else if (token->keyword == RID_AT_PROPERTY)
32983 cp_parser_objc_at_property_declaration (parser);
32984 else if (token->keyword == RID_AT_SYNTHESIZE)
32985 cp_parser_objc_at_synthesize_declaration (parser);
32986 else if (token->keyword == RID_AT_DYNAMIC)
32987 cp_parser_objc_at_dynamic_declaration (parser);
32988 else if (token->keyword == RID_ATTRIBUTE
32989 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
32990 warning_at (token->location, OPT_Wattributes,
32991 "prefix attributes are ignored for methods");
32992 else
32993 /* Allow for interspersed non-ObjC++ code. */
32994 cp_parser_objc_interstitial_code (parser);
32995
32996 token = cp_lexer_peek_token (parser->lexer);
32997 }
32998
32999 if (token->type != CPP_EOF)
33000 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
33001 else
33002 cp_parser_error (parser, "expected %<@end%>");
33003
33004 objc_finish_implementation ();
33005 }
33006
33007 /* Parse Objective-C ivars. */
33008
33009 static void
33010 cp_parser_objc_class_ivars (cp_parser* parser)
33011 {
33012 cp_token *token = cp_lexer_peek_token (parser->lexer);
33013
33014 if (token->type != CPP_OPEN_BRACE)
33015 return; /* No ivars specified. */
33016
33017 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
33018 token = cp_lexer_peek_token (parser->lexer);
33019
33020 while (token->type != CPP_CLOSE_BRACE
33021 && token->keyword != RID_AT_END && token->type != CPP_EOF)
33022 {
33023 cp_decl_specifier_seq declspecs;
33024 int decl_class_or_enum_p;
33025 tree prefix_attributes;
33026
33027 cp_parser_objc_visibility_spec (parser);
33028
33029 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33030 break;
33031
33032 cp_parser_decl_specifier_seq (parser,
33033 CP_PARSER_FLAGS_OPTIONAL,
33034 &declspecs,
33035 &decl_class_or_enum_p);
33036
33037 /* auto, register, static, extern, mutable. */
33038 if (declspecs.storage_class != sc_none)
33039 {
33040 cp_parser_error (parser, "invalid type for instance variable");
33041 declspecs.storage_class = sc_none;
33042 }
33043
33044 /* thread_local. */
33045 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
33046 {
33047 cp_parser_error (parser, "invalid type for instance variable");
33048 declspecs.locations[ds_thread] = 0;
33049 }
33050
33051 /* typedef. */
33052 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
33053 {
33054 cp_parser_error (parser, "invalid type for instance variable");
33055 declspecs.locations[ds_typedef] = 0;
33056 }
33057
33058 prefix_attributes = declspecs.attributes;
33059 declspecs.attributes = NULL_TREE;
33060
33061 /* Keep going until we hit the `;' at the end of the
33062 declaration. */
33063 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33064 {
33065 tree width = NULL_TREE, attributes, first_attribute, decl;
33066 cp_declarator *declarator = NULL;
33067 int ctor_dtor_or_conv_p;
33068
33069 /* Check for a (possibly unnamed) bitfield declaration. */
33070 token = cp_lexer_peek_token (parser->lexer);
33071 if (token->type == CPP_COLON)
33072 goto eat_colon;
33073
33074 if (token->type == CPP_NAME
33075 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
33076 == CPP_COLON))
33077 {
33078 /* Get the name of the bitfield. */
33079 declarator = make_id_declarator (NULL_TREE,
33080 cp_parser_identifier (parser),
33081 sfk_none, token->location);
33082
33083 eat_colon:
33084 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
33085 /* Get the width of the bitfield. */
33086 width
33087 = cp_parser_constant_expression (parser);
33088 }
33089 else
33090 {
33091 /* Parse the declarator. */
33092 declarator
33093 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
33094 CP_PARSER_FLAGS_NONE,
33095 &ctor_dtor_or_conv_p,
33096 /*parenthesized_p=*/NULL,
33097 /*member_p=*/false,
33098 /*friend_p=*/false,
33099 /*static_p=*/false);
33100 }
33101
33102 /* Look for attributes that apply to the ivar. */
33103 attributes = cp_parser_attributes_opt (parser);
33104 /* Remember which attributes are prefix attributes and
33105 which are not. */
33106 first_attribute = attributes;
33107 /* Combine the attributes. */
33108 attributes = attr_chainon (prefix_attributes, attributes);
33109
33110 if (width)
33111 /* Create the bitfield declaration. */
33112 decl = grokbitfield (declarator, &declspecs,
33113 width, NULL_TREE, attributes);
33114 else
33115 decl = grokfield (declarator, &declspecs,
33116 NULL_TREE, /*init_const_expr_p=*/false,
33117 NULL_TREE, attributes);
33118
33119 /* Add the instance variable. */
33120 if (decl != error_mark_node && decl != NULL_TREE)
33121 objc_add_instance_variable (decl);
33122
33123 /* Reset PREFIX_ATTRIBUTES. */
33124 if (attributes != error_mark_node)
33125 {
33126 while (attributes && TREE_CHAIN (attributes) != first_attribute)
33127 attributes = TREE_CHAIN (attributes);
33128 if (attributes)
33129 TREE_CHAIN (attributes) = NULL_TREE;
33130 }
33131
33132 token = cp_lexer_peek_token (parser->lexer);
33133
33134 if (token->type == CPP_COMMA)
33135 {
33136 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
33137 continue;
33138 }
33139 break;
33140 }
33141
33142 cp_parser_consume_semicolon_at_end_of_statement (parser);
33143 token = cp_lexer_peek_token (parser->lexer);
33144 }
33145
33146 if (token->keyword == RID_AT_END)
33147 cp_parser_error (parser, "expected %<}%>");
33148
33149 /* Do not consume the RID_AT_END, so it will be read again as terminating
33150 the @interface of @implementation. */
33151 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
33152 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
33153
33154 /* For historical reasons, we accept an optional semicolon. */
33155 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33156 cp_lexer_consume_token (parser->lexer);
33157 }
33158
33159 /* Parse an Objective-C protocol declaration. */
33160
33161 static void
33162 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
33163 {
33164 tree proto, protorefs;
33165 cp_token *tok;
33166
33167 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
33168 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33169 {
33170 tok = cp_lexer_peek_token (parser->lexer);
33171 error_at (tok->location, "identifier expected after %<@protocol%>");
33172 cp_parser_consume_semicolon_at_end_of_statement (parser);
33173 return;
33174 }
33175
33176 /* See if we have a forward declaration or a definition. */
33177 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
33178
33179 /* Try a forward declaration first. */
33180 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
33181 {
33182 while (true)
33183 {
33184 tree id;
33185
33186 id = cp_parser_identifier (parser);
33187 if (id == error_mark_node)
33188 break;
33189
33190 objc_declare_protocol (id, attributes);
33191
33192 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33193 cp_lexer_consume_token (parser->lexer);
33194 else
33195 break;
33196 }
33197 cp_parser_consume_semicolon_at_end_of_statement (parser);
33198 }
33199
33200 /* Ok, we got a full-fledged definition (or at least should). */
33201 else
33202 {
33203 proto = cp_parser_identifier (parser);
33204 protorefs = cp_parser_objc_protocol_refs_opt (parser);
33205 objc_start_protocol (proto, protorefs, attributes);
33206 cp_parser_objc_method_prototype_list (parser);
33207 }
33208 }
33209
33210 /* Parse an Objective-C superclass or category. */
33211
33212 static void
33213 cp_parser_objc_superclass_or_category (cp_parser *parser,
33214 bool iface_p,
33215 tree *super,
33216 tree *categ, bool *is_class_extension)
33217 {
33218 cp_token *next = cp_lexer_peek_token (parser->lexer);
33219
33220 *super = *categ = NULL_TREE;
33221 *is_class_extension = false;
33222 if (next->type == CPP_COLON)
33223 {
33224 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
33225 *super = cp_parser_identifier (parser);
33226 }
33227 else if (next->type == CPP_OPEN_PAREN)
33228 {
33229 matching_parens parens;
33230 parens.consume_open (parser); /* Eat '('. */
33231
33232 /* If there is no category name, and this is an @interface, we
33233 have a class extension. */
33234 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33235 {
33236 *categ = NULL_TREE;
33237 *is_class_extension = true;
33238 }
33239 else
33240 *categ = cp_parser_identifier (parser);
33241
33242 parens.require_close (parser);
33243 }
33244 }
33245
33246 /* Parse an Objective-C class interface. */
33247
33248 static void
33249 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
33250 {
33251 tree name, super, categ, protos;
33252 bool is_class_extension;
33253
33254 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
33255 name = cp_parser_identifier (parser);
33256 if (name == error_mark_node)
33257 {
33258 /* It's hard to recover because even if valid @interface stuff
33259 is to follow, we can't compile it (or validate it) if we
33260 don't even know which class it refers to. Let's assume this
33261 was a stray '@interface' token in the stream and skip it.
33262 */
33263 return;
33264 }
33265 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
33266 &is_class_extension);
33267 protos = cp_parser_objc_protocol_refs_opt (parser);
33268
33269 /* We have either a class or a category on our hands. */
33270 if (categ || is_class_extension)
33271 objc_start_category_interface (name, categ, protos, attributes);
33272 else
33273 {
33274 objc_start_class_interface (name, super, protos, attributes);
33275 /* Handle instance variable declarations, if any. */
33276 cp_parser_objc_class_ivars (parser);
33277 objc_continue_interface ();
33278 }
33279
33280 cp_parser_objc_method_prototype_list (parser);
33281 }
33282
33283 /* Parse an Objective-C class implementation. */
33284
33285 static void
33286 cp_parser_objc_class_implementation (cp_parser* parser)
33287 {
33288 tree name, super, categ;
33289 bool is_class_extension;
33290
33291 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
33292 name = cp_parser_identifier (parser);
33293 if (name == error_mark_node)
33294 {
33295 /* It's hard to recover because even if valid @implementation
33296 stuff is to follow, we can't compile it (or validate it) if
33297 we don't even know which class it refers to. Let's assume
33298 this was a stray '@implementation' token in the stream and
33299 skip it.
33300 */
33301 return;
33302 }
33303 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
33304 &is_class_extension);
33305
33306 /* We have either a class or a category on our hands. */
33307 if (categ)
33308 objc_start_category_implementation (name, categ);
33309 else
33310 {
33311 objc_start_class_implementation (name, super);
33312 /* Handle instance variable declarations, if any. */
33313 cp_parser_objc_class_ivars (parser);
33314 objc_continue_implementation ();
33315 }
33316
33317 cp_parser_objc_method_definition_list (parser);
33318 }
33319
33320 /* Consume the @end token and finish off the implementation. */
33321
33322 static void
33323 cp_parser_objc_end_implementation (cp_parser* parser)
33324 {
33325 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
33326 objc_finish_implementation ();
33327 }
33328
33329 /* Parse an Objective-C declaration. */
33330
33331 static void
33332 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
33333 {
33334 /* Try to figure out what kind of declaration is present. */
33335 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
33336
33337 if (attributes)
33338 switch (kwd->keyword)
33339 {
33340 case RID_AT_ALIAS:
33341 case RID_AT_CLASS:
33342 case RID_AT_END:
33343 error_at (kwd->location, "attributes may not be specified before"
33344 " the %<@%D%> Objective-C++ keyword",
33345 kwd->u.value);
33346 attributes = NULL;
33347 break;
33348 case RID_AT_IMPLEMENTATION:
33349 warning_at (kwd->location, OPT_Wattributes,
33350 "prefix attributes are ignored before %<@%D%>",
33351 kwd->u.value);
33352 attributes = NULL;
33353 default:
33354 break;
33355 }
33356
33357 switch (kwd->keyword)
33358 {
33359 case RID_AT_ALIAS:
33360 cp_parser_objc_alias_declaration (parser);
33361 break;
33362 case RID_AT_CLASS:
33363 cp_parser_objc_class_declaration (parser);
33364 break;
33365 case RID_AT_PROTOCOL:
33366 cp_parser_objc_protocol_declaration (parser, attributes);
33367 break;
33368 case RID_AT_INTERFACE:
33369 cp_parser_objc_class_interface (parser, attributes);
33370 break;
33371 case RID_AT_IMPLEMENTATION:
33372 cp_parser_objc_class_implementation (parser);
33373 break;
33374 case RID_AT_END:
33375 cp_parser_objc_end_implementation (parser);
33376 break;
33377 default:
33378 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
33379 kwd->u.value);
33380 cp_parser_skip_to_end_of_block_or_statement (parser);
33381 }
33382 }
33383
33384 /* Parse an Objective-C try-catch-finally statement.
33385
33386 objc-try-catch-finally-stmt:
33387 @try compound-statement objc-catch-clause-seq [opt]
33388 objc-finally-clause [opt]
33389
33390 objc-catch-clause-seq:
33391 objc-catch-clause objc-catch-clause-seq [opt]
33392
33393 objc-catch-clause:
33394 @catch ( objc-exception-declaration ) compound-statement
33395
33396 objc-finally-clause:
33397 @finally compound-statement
33398
33399 objc-exception-declaration:
33400 parameter-declaration
33401 '...'
33402
33403 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
33404
33405 Returns NULL_TREE.
33406
33407 PS: This function is identical to c_parser_objc_try_catch_finally_statement
33408 for C. Keep them in sync. */
33409
33410 static tree
33411 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
33412 {
33413 location_t location;
33414 tree stmt;
33415
33416 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
33417 location = cp_lexer_peek_token (parser->lexer)->location;
33418 objc_maybe_warn_exceptions (location);
33419 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
33420 node, lest it get absorbed into the surrounding block. */
33421 stmt = push_stmt_list ();
33422 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
33423 objc_begin_try_stmt (location, pop_stmt_list (stmt));
33424
33425 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
33426 {
33427 cp_parameter_declarator *parm;
33428 tree parameter_declaration = error_mark_node;
33429 bool seen_open_paren = false;
33430 matching_parens parens;
33431
33432 cp_lexer_consume_token (parser->lexer);
33433 if (parens.require_open (parser))
33434 seen_open_paren = true;
33435 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
33436 {
33437 /* We have "@catch (...)" (where the '...' are literally
33438 what is in the code). Skip the '...'.
33439 parameter_declaration is set to NULL_TREE, and
33440 objc_being_catch_clauses() knows that that means
33441 '...'. */
33442 cp_lexer_consume_token (parser->lexer);
33443 parameter_declaration = NULL_TREE;
33444 }
33445 else
33446 {
33447 /* We have "@catch (NSException *exception)" or something
33448 like that. Parse the parameter declaration. */
33449 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
33450 false, NULL);
33451 if (parm == NULL)
33452 parameter_declaration = error_mark_node;
33453 else
33454 parameter_declaration = grokdeclarator (parm->declarator,
33455 &parm->decl_specifiers,
33456 PARM, /*initialized=*/0,
33457 /*attrlist=*/NULL);
33458 }
33459 if (seen_open_paren)
33460 parens.require_close (parser);
33461 else
33462 {
33463 /* If there was no open parenthesis, we are recovering from
33464 an error, and we are trying to figure out what mistake
33465 the user has made. */
33466
33467 /* If there is an immediate closing parenthesis, the user
33468 probably forgot the opening one (ie, they typed "@catch
33469 NSException *e)". Parse the closing parenthesis and keep
33470 going. */
33471 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33472 cp_lexer_consume_token (parser->lexer);
33473
33474 /* If these is no immediate closing parenthesis, the user
33475 probably doesn't know that parenthesis are required at
33476 all (ie, they typed "@catch NSException *e"). So, just
33477 forget about the closing parenthesis and keep going. */
33478 }
33479 objc_begin_catch_clause (parameter_declaration);
33480 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
33481 objc_finish_catch_clause ();
33482 }
33483 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
33484 {
33485 cp_lexer_consume_token (parser->lexer);
33486 location = cp_lexer_peek_token (parser->lexer)->location;
33487 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
33488 node, lest it get absorbed into the surrounding block. */
33489 stmt = push_stmt_list ();
33490 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
33491 objc_build_finally_clause (location, pop_stmt_list (stmt));
33492 }
33493
33494 return objc_finish_try_stmt ();
33495 }
33496
33497 /* Parse an Objective-C synchronized statement.
33498
33499 objc-synchronized-stmt:
33500 @synchronized ( expression ) compound-statement
33501
33502 Returns NULL_TREE. */
33503
33504 static tree
33505 cp_parser_objc_synchronized_statement (cp_parser *parser)
33506 {
33507 location_t location;
33508 tree lock, stmt;
33509
33510 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
33511
33512 location = cp_lexer_peek_token (parser->lexer)->location;
33513 objc_maybe_warn_exceptions (location);
33514 matching_parens parens;
33515 parens.require_open (parser);
33516 lock = cp_parser_expression (parser);
33517 parens.require_close (parser);
33518
33519 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
33520 node, lest it get absorbed into the surrounding block. */
33521 stmt = push_stmt_list ();
33522 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
33523
33524 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
33525 }
33526
33527 /* Parse an Objective-C throw statement.
33528
33529 objc-throw-stmt:
33530 @throw assignment-expression [opt] ;
33531
33532 Returns a constructed '@throw' statement. */
33533
33534 static tree
33535 cp_parser_objc_throw_statement (cp_parser *parser)
33536 {
33537 tree expr = NULL_TREE;
33538 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33539
33540 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
33541
33542 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33543 expr = cp_parser_expression (parser);
33544
33545 cp_parser_consume_semicolon_at_end_of_statement (parser);
33546
33547 return objc_build_throw_stmt (loc, expr);
33548 }
33549
33550 /* Parse an Objective-C statement. */
33551
33552 static tree
33553 cp_parser_objc_statement (cp_parser * parser)
33554 {
33555 /* Try to figure out what kind of declaration is present. */
33556 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
33557
33558 switch (kwd->keyword)
33559 {
33560 case RID_AT_TRY:
33561 return cp_parser_objc_try_catch_finally_statement (parser);
33562 case RID_AT_SYNCHRONIZED:
33563 return cp_parser_objc_synchronized_statement (parser);
33564 case RID_AT_THROW:
33565 return cp_parser_objc_throw_statement (parser);
33566 default:
33567 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
33568 kwd->u.value);
33569 cp_parser_skip_to_end_of_block_or_statement (parser);
33570 }
33571
33572 return error_mark_node;
33573 }
33574
33575 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
33576 look ahead to see if an objc keyword follows the attributes. This
33577 is to detect the use of prefix attributes on ObjC @interface and
33578 @protocol. */
33579
33580 static bool
33581 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
33582 {
33583 cp_lexer_save_tokens (parser->lexer);
33584 *attrib = cp_parser_attributes_opt (parser);
33585 gcc_assert (*attrib);
33586 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
33587 {
33588 cp_lexer_commit_tokens (parser->lexer);
33589 return true;
33590 }
33591 cp_lexer_rollback_tokens (parser->lexer);
33592 return false;
33593 }
33594
33595 /* This routine is a minimal replacement for
33596 c_parser_struct_declaration () used when parsing the list of
33597 types/names or ObjC++ properties. For example, when parsing the
33598 code
33599
33600 @property (readonly) int a, b, c;
33601
33602 this function is responsible for parsing "int a, int b, int c" and
33603 returning the declarations as CHAIN of DECLs.
33604
33605 TODO: Share this code with cp_parser_objc_class_ivars. It's very
33606 similar parsing. */
33607 static tree
33608 cp_parser_objc_struct_declaration (cp_parser *parser)
33609 {
33610 tree decls = NULL_TREE;
33611 cp_decl_specifier_seq declspecs;
33612 int decl_class_or_enum_p;
33613 tree prefix_attributes;
33614
33615 cp_parser_decl_specifier_seq (parser,
33616 CP_PARSER_FLAGS_NONE,
33617 &declspecs,
33618 &decl_class_or_enum_p);
33619
33620 if (declspecs.type == error_mark_node)
33621 return error_mark_node;
33622
33623 /* auto, register, static, extern, mutable. */
33624 if (declspecs.storage_class != sc_none)
33625 {
33626 cp_parser_error (parser, "invalid type for property");
33627 declspecs.storage_class = sc_none;
33628 }
33629
33630 /* thread_local. */
33631 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
33632 {
33633 cp_parser_error (parser, "invalid type for property");
33634 declspecs.locations[ds_thread] = 0;
33635 }
33636
33637 /* typedef. */
33638 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
33639 {
33640 cp_parser_error (parser, "invalid type for property");
33641 declspecs.locations[ds_typedef] = 0;
33642 }
33643
33644 prefix_attributes = declspecs.attributes;
33645 declspecs.attributes = NULL_TREE;
33646
33647 /* Keep going until we hit the `;' at the end of the declaration. */
33648 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33649 {
33650 tree attributes, first_attribute, decl;
33651 cp_declarator *declarator;
33652 cp_token *token;
33653
33654 /* Parse the declarator. */
33655 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
33656 CP_PARSER_FLAGS_NONE,
33657 NULL, NULL, false, false, false);
33658
33659 /* Look for attributes that apply to the ivar. */
33660 attributes = cp_parser_attributes_opt (parser);
33661 /* Remember which attributes are prefix attributes and
33662 which are not. */
33663 first_attribute = attributes;
33664 /* Combine the attributes. */
33665 attributes = attr_chainon (prefix_attributes, attributes);
33666
33667 decl = grokfield (declarator, &declspecs,
33668 NULL_TREE, /*init_const_expr_p=*/false,
33669 NULL_TREE, attributes);
33670
33671 if (decl == error_mark_node || decl == NULL_TREE)
33672 return error_mark_node;
33673
33674 /* Reset PREFIX_ATTRIBUTES. */
33675 if (attributes != error_mark_node)
33676 {
33677 while (attributes && TREE_CHAIN (attributes) != first_attribute)
33678 attributes = TREE_CHAIN (attributes);
33679 if (attributes)
33680 TREE_CHAIN (attributes) = NULL_TREE;
33681 }
33682
33683 DECL_CHAIN (decl) = decls;
33684 decls = decl;
33685
33686 token = cp_lexer_peek_token (parser->lexer);
33687 if (token->type == CPP_COMMA)
33688 {
33689 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
33690 continue;
33691 }
33692 else
33693 break;
33694 }
33695 return decls;
33696 }
33697
33698 /* Parse an Objective-C @property declaration. The syntax is:
33699
33700 objc-property-declaration:
33701 '@property' objc-property-attributes[opt] struct-declaration ;
33702
33703 objc-property-attributes:
33704 '(' objc-property-attribute-list ')'
33705
33706 objc-property-attribute-list:
33707 objc-property-attribute
33708 objc-property-attribute-list, objc-property-attribute
33709
33710 objc-property-attribute
33711 'getter' = identifier
33712 'setter' = identifier
33713 'readonly'
33714 'readwrite'
33715 'assign'
33716 'retain'
33717 'copy'
33718 'nonatomic'
33719
33720 For example:
33721 @property NSString *name;
33722 @property (readonly) id object;
33723 @property (retain, nonatomic, getter=getTheName) id name;
33724 @property int a, b, c;
33725
33726 PS: This function is identical to
33727 c_parser_objc_at_property_declaration for C. Keep them in sync. */
33728 static void
33729 cp_parser_objc_at_property_declaration (cp_parser *parser)
33730 {
33731 /* The following variables hold the attributes of the properties as
33732 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
33733 seen. When we see an attribute, we set them to 'true' (if they
33734 are boolean properties) or to the identifier (if they have an
33735 argument, ie, for getter and setter). Note that here we only
33736 parse the list of attributes, check the syntax and accumulate the
33737 attributes that we find. objc_add_property_declaration() will
33738 then process the information. */
33739 bool property_assign = false;
33740 bool property_copy = false;
33741 tree property_getter_ident = NULL_TREE;
33742 bool property_nonatomic = false;
33743 bool property_readonly = false;
33744 bool property_readwrite = false;
33745 bool property_retain = false;
33746 tree property_setter_ident = NULL_TREE;
33747
33748 /* 'properties' is the list of properties that we read. Usually a
33749 single one, but maybe more (eg, in "@property int a, b, c;" there
33750 are three). */
33751 tree properties;
33752 location_t loc;
33753
33754 loc = cp_lexer_peek_token (parser->lexer)->location;
33755
33756 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
33757
33758 /* Parse the optional attribute list... */
33759 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33760 {
33761 /* Eat the '('. */
33762 matching_parens parens;
33763 parens.consume_open (parser);
33764
33765 while (true)
33766 {
33767 bool syntax_error = false;
33768 cp_token *token = cp_lexer_peek_token (parser->lexer);
33769 enum rid keyword;
33770
33771 if (token->type != CPP_NAME)
33772 {
33773 cp_parser_error (parser, "expected identifier");
33774 break;
33775 }
33776 keyword = C_RID_CODE (token->u.value);
33777 cp_lexer_consume_token (parser->lexer);
33778 switch (keyword)
33779 {
33780 case RID_ASSIGN: property_assign = true; break;
33781 case RID_COPY: property_copy = true; break;
33782 case RID_NONATOMIC: property_nonatomic = true; break;
33783 case RID_READONLY: property_readonly = true; break;
33784 case RID_READWRITE: property_readwrite = true; break;
33785 case RID_RETAIN: property_retain = true; break;
33786
33787 case RID_GETTER:
33788 case RID_SETTER:
33789 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
33790 {
33791 if (keyword == RID_GETTER)
33792 cp_parser_error (parser,
33793 "missing %<=%> (after %<getter%> attribute)");
33794 else
33795 cp_parser_error (parser,
33796 "missing %<=%> (after %<setter%> attribute)");
33797 syntax_error = true;
33798 break;
33799 }
33800 cp_lexer_consume_token (parser->lexer); /* eat the = */
33801 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
33802 {
33803 cp_parser_error (parser, "expected identifier");
33804 syntax_error = true;
33805 break;
33806 }
33807 if (keyword == RID_SETTER)
33808 {
33809 if (property_setter_ident != NULL_TREE)
33810 {
33811 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
33812 cp_lexer_consume_token (parser->lexer);
33813 }
33814 else
33815 property_setter_ident = cp_parser_objc_selector (parser);
33816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
33817 cp_parser_error (parser, "setter name must terminate with %<:%>");
33818 else
33819 cp_lexer_consume_token (parser->lexer);
33820 }
33821 else
33822 {
33823 if (property_getter_ident != NULL_TREE)
33824 {
33825 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
33826 cp_lexer_consume_token (parser->lexer);
33827 }
33828 else
33829 property_getter_ident = cp_parser_objc_selector (parser);
33830 }
33831 break;
33832 default:
33833 cp_parser_error (parser, "unknown property attribute");
33834 syntax_error = true;
33835 break;
33836 }
33837
33838 if (syntax_error)
33839 break;
33840
33841 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33842 cp_lexer_consume_token (parser->lexer);
33843 else
33844 break;
33845 }
33846
33847 /* FIXME: "@property (setter, assign);" will generate a spurious
33848 "error: expected ‘)’ before ‘,’ token". This is because
33849 cp_parser_require, unlike the C counterpart, will produce an
33850 error even if we are in error recovery. */
33851 if (!parens.require_close (parser))
33852 {
33853 cp_parser_skip_to_closing_parenthesis (parser,
33854 /*recovering=*/true,
33855 /*or_comma=*/false,
33856 /*consume_paren=*/true);
33857 }
33858 }
33859
33860 /* ... and the property declaration(s). */
33861 properties = cp_parser_objc_struct_declaration (parser);
33862
33863 if (properties == error_mark_node)
33864 {
33865 cp_parser_skip_to_end_of_statement (parser);
33866 /* If the next token is now a `;', consume it. */
33867 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33868 cp_lexer_consume_token (parser->lexer);
33869 return;
33870 }
33871
33872 if (properties == NULL_TREE)
33873 cp_parser_error (parser, "expected identifier");
33874 else
33875 {
33876 /* Comma-separated properties are chained together in
33877 reverse order; add them one by one. */
33878 properties = nreverse (properties);
33879
33880 for (; properties; properties = TREE_CHAIN (properties))
33881 objc_add_property_declaration (loc, copy_node (properties),
33882 property_readonly, property_readwrite,
33883 property_assign, property_retain,
33884 property_copy, property_nonatomic,
33885 property_getter_ident, property_setter_ident);
33886 }
33887
33888 cp_parser_consume_semicolon_at_end_of_statement (parser);
33889 }
33890
33891 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
33892
33893 objc-synthesize-declaration:
33894 @synthesize objc-synthesize-identifier-list ;
33895
33896 objc-synthesize-identifier-list:
33897 objc-synthesize-identifier
33898 objc-synthesize-identifier-list, objc-synthesize-identifier
33899
33900 objc-synthesize-identifier
33901 identifier
33902 identifier = identifier
33903
33904 For example:
33905 @synthesize MyProperty;
33906 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
33907
33908 PS: This function is identical to c_parser_objc_at_synthesize_declaration
33909 for C. Keep them in sync.
33910 */
33911 static void
33912 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
33913 {
33914 tree list = NULL_TREE;
33915 location_t loc;
33916 loc = cp_lexer_peek_token (parser->lexer)->location;
33917
33918 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
33919 while (true)
33920 {
33921 tree property, ivar;
33922 property = cp_parser_identifier (parser);
33923 if (property == error_mark_node)
33924 {
33925 cp_parser_consume_semicolon_at_end_of_statement (parser);
33926 return;
33927 }
33928 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
33929 {
33930 cp_lexer_consume_token (parser->lexer);
33931 ivar = cp_parser_identifier (parser);
33932 if (ivar == error_mark_node)
33933 {
33934 cp_parser_consume_semicolon_at_end_of_statement (parser);
33935 return;
33936 }
33937 }
33938 else
33939 ivar = NULL_TREE;
33940 list = chainon (list, build_tree_list (ivar, property));
33941 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33942 cp_lexer_consume_token (parser->lexer);
33943 else
33944 break;
33945 }
33946 cp_parser_consume_semicolon_at_end_of_statement (parser);
33947 objc_add_synthesize_declaration (loc, list);
33948 }
33949
33950 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
33951
33952 objc-dynamic-declaration:
33953 @dynamic identifier-list ;
33954
33955 For example:
33956 @dynamic MyProperty;
33957 @dynamic MyProperty, AnotherProperty;
33958
33959 PS: This function is identical to c_parser_objc_at_dynamic_declaration
33960 for C. Keep them in sync.
33961 */
33962 static void
33963 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
33964 {
33965 tree list = NULL_TREE;
33966 location_t loc;
33967 loc = cp_lexer_peek_token (parser->lexer)->location;
33968
33969 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
33970 while (true)
33971 {
33972 tree property;
33973 property = cp_parser_identifier (parser);
33974 if (property == error_mark_node)
33975 {
33976 cp_parser_consume_semicolon_at_end_of_statement (parser);
33977 return;
33978 }
33979 list = chainon (list, build_tree_list (NULL, property));
33980 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33981 cp_lexer_consume_token (parser->lexer);
33982 else
33983 break;
33984 }
33985 cp_parser_consume_semicolon_at_end_of_statement (parser);
33986 objc_add_dynamic_declaration (loc, list);
33987 }
33988
33989 \f
33990 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
33991
33992 /* Returns name of the next clause.
33993 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
33994 the token is not consumed. Otherwise appropriate pragma_omp_clause is
33995 returned and the token is consumed. */
33996
33997 static pragma_omp_clause
33998 cp_parser_omp_clause_name (cp_parser *parser)
33999 {
34000 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
34001
34002 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
34003 result = PRAGMA_OACC_CLAUSE_AUTO;
34004 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
34005 result = PRAGMA_OMP_CLAUSE_IF;
34006 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
34007 result = PRAGMA_OMP_CLAUSE_DEFAULT;
34008 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
34009 result = PRAGMA_OACC_CLAUSE_DELETE;
34010 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
34011 result = PRAGMA_OMP_CLAUSE_PRIVATE;
34012 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34013 result = PRAGMA_OMP_CLAUSE_FOR;
34014 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34015 {
34016 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34017 const char *p = IDENTIFIER_POINTER (id);
34018
34019 switch (p[0])
34020 {
34021 case 'a':
34022 if (!strcmp ("aligned", p))
34023 result = PRAGMA_OMP_CLAUSE_ALIGNED;
34024 else if (!strcmp ("async", p))
34025 result = PRAGMA_OACC_CLAUSE_ASYNC;
34026 else if (!strcmp ("attach", p))
34027 result = PRAGMA_OACC_CLAUSE_ATTACH;
34028 break;
34029 case 'b':
34030 if (!strcmp ("bind", p))
34031 result = PRAGMA_OMP_CLAUSE_BIND;
34032 break;
34033 case 'c':
34034 if (!strcmp ("collapse", p))
34035 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
34036 else if (!strcmp ("copy", p))
34037 result = PRAGMA_OACC_CLAUSE_COPY;
34038 else if (!strcmp ("copyin", p))
34039 result = PRAGMA_OMP_CLAUSE_COPYIN;
34040 else if (!strcmp ("copyout", p))
34041 result = PRAGMA_OACC_CLAUSE_COPYOUT;
34042 else if (!strcmp ("copyprivate", p))
34043 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
34044 else if (!strcmp ("create", p))
34045 result = PRAGMA_OACC_CLAUSE_CREATE;
34046 break;
34047 case 'd':
34048 if (!strcmp ("defaultmap", p))
34049 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
34050 else if (!strcmp ("depend", p))
34051 result = PRAGMA_OMP_CLAUSE_DEPEND;
34052 else if (!strcmp ("detach", p))
34053 result = PRAGMA_OACC_CLAUSE_DETACH;
34054 else if (!strcmp ("device", p))
34055 result = PRAGMA_OMP_CLAUSE_DEVICE;
34056 else if (!strcmp ("deviceptr", p))
34057 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
34058 else if (!strcmp ("device_resident", p))
34059 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
34060 else if (!strcmp ("device_type", p))
34061 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
34062 else if (!strcmp ("dist_schedule", p))
34063 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
34064 break;
34065 case 'f':
34066 if (!strcmp ("final", p))
34067 result = PRAGMA_OMP_CLAUSE_FINAL;
34068 else if (!strcmp ("finalize", p))
34069 result = PRAGMA_OACC_CLAUSE_FINALIZE;
34070 else if (!strcmp ("firstprivate", p))
34071 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
34072 else if (!strcmp ("from", p))
34073 result = PRAGMA_OMP_CLAUSE_FROM;
34074 break;
34075 case 'g':
34076 if (!strcmp ("gang", p))
34077 result = PRAGMA_OACC_CLAUSE_GANG;
34078 else if (!strcmp ("grainsize", p))
34079 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
34080 break;
34081 case 'h':
34082 if (!strcmp ("hint", p))
34083 result = PRAGMA_OMP_CLAUSE_HINT;
34084 else if (!strcmp ("host", p))
34085 result = PRAGMA_OACC_CLAUSE_HOST;
34086 break;
34087 case 'i':
34088 if (!strcmp ("if_present", p))
34089 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
34090 else if (!strcmp ("in_reduction", p))
34091 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
34092 else if (!strcmp ("inbranch", p))
34093 result = PRAGMA_OMP_CLAUSE_INBRANCH;
34094 else if (!strcmp ("independent", p))
34095 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
34096 else if (!strcmp ("is_device_ptr", p))
34097 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
34098 break;
34099 case 'l':
34100 if (!strcmp ("lastprivate", p))
34101 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
34102 else if (!strcmp ("linear", p))
34103 result = PRAGMA_OMP_CLAUSE_LINEAR;
34104 else if (!strcmp ("link", p))
34105 result = PRAGMA_OMP_CLAUSE_LINK;
34106 break;
34107 case 'm':
34108 if (!strcmp ("map", p))
34109 result = PRAGMA_OMP_CLAUSE_MAP;
34110 else if (!strcmp ("mergeable", p))
34111 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
34112 break;
34113 case 'n':
34114 if (!strcmp ("no_create", p))
34115 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
34116 else if (!strcmp ("nogroup", p))
34117 result = PRAGMA_OMP_CLAUSE_NOGROUP;
34118 else if (!strcmp ("nontemporal", p))
34119 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
34120 else if (!strcmp ("notinbranch", p))
34121 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
34122 else if (!strcmp ("nowait", p))
34123 result = PRAGMA_OMP_CLAUSE_NOWAIT;
34124 else if (!strcmp ("num_gangs", p))
34125 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
34126 else if (!strcmp ("num_tasks", p))
34127 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
34128 else if (!strcmp ("num_teams", p))
34129 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
34130 else if (!strcmp ("num_threads", p))
34131 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
34132 else if (!strcmp ("num_workers", p))
34133 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
34134 break;
34135 case 'o':
34136 if (!strcmp ("ordered", p))
34137 result = PRAGMA_OMP_CLAUSE_ORDERED;
34138 else if (!strcmp ("order", p))
34139 result = PRAGMA_OMP_CLAUSE_ORDER;
34140 break;
34141 case 'p':
34142 if (!strcmp ("parallel", p))
34143 result = PRAGMA_OMP_CLAUSE_PARALLEL;
34144 else if (!strcmp ("present", p))
34145 result = PRAGMA_OACC_CLAUSE_PRESENT;
34146 else if (!strcmp ("present_or_copy", p)
34147 || !strcmp ("pcopy", p))
34148 result = PRAGMA_OACC_CLAUSE_COPY;
34149 else if (!strcmp ("present_or_copyin", p)
34150 || !strcmp ("pcopyin", p))
34151 result = PRAGMA_OACC_CLAUSE_COPYIN;
34152 else if (!strcmp ("present_or_copyout", p)
34153 || !strcmp ("pcopyout", p))
34154 result = PRAGMA_OACC_CLAUSE_COPYOUT;
34155 else if (!strcmp ("present_or_create", p)
34156 || !strcmp ("pcreate", p))
34157 result = PRAGMA_OACC_CLAUSE_CREATE;
34158 else if (!strcmp ("priority", p))
34159 result = PRAGMA_OMP_CLAUSE_PRIORITY;
34160 else if (!strcmp ("proc_bind", p))
34161 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
34162 break;
34163 case 'r':
34164 if (!strcmp ("reduction", p))
34165 result = PRAGMA_OMP_CLAUSE_REDUCTION;
34166 break;
34167 case 's':
34168 if (!strcmp ("safelen", p))
34169 result = PRAGMA_OMP_CLAUSE_SAFELEN;
34170 else if (!strcmp ("schedule", p))
34171 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
34172 else if (!strcmp ("sections", p))
34173 result = PRAGMA_OMP_CLAUSE_SECTIONS;
34174 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
34175 result = PRAGMA_OACC_CLAUSE_HOST;
34176 else if (!strcmp ("seq", p))
34177 result = PRAGMA_OACC_CLAUSE_SEQ;
34178 else if (!strcmp ("shared", p))
34179 result = PRAGMA_OMP_CLAUSE_SHARED;
34180 else if (!strcmp ("simd", p))
34181 result = PRAGMA_OMP_CLAUSE_SIMD;
34182 else if (!strcmp ("simdlen", p))
34183 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
34184 break;
34185 case 't':
34186 if (!strcmp ("task_reduction", p))
34187 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
34188 else if (!strcmp ("taskgroup", p))
34189 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
34190 else if (!strcmp ("thread_limit", p))
34191 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
34192 else if (!strcmp ("threads", p))
34193 result = PRAGMA_OMP_CLAUSE_THREADS;
34194 else if (!strcmp ("tile", p))
34195 result = PRAGMA_OACC_CLAUSE_TILE;
34196 else if (!strcmp ("to", p))
34197 result = PRAGMA_OMP_CLAUSE_TO;
34198 break;
34199 case 'u':
34200 if (!strcmp ("uniform", p))
34201 result = PRAGMA_OMP_CLAUSE_UNIFORM;
34202 else if (!strcmp ("untied", p))
34203 result = PRAGMA_OMP_CLAUSE_UNTIED;
34204 else if (!strcmp ("use_device", p))
34205 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
34206 else if (!strcmp ("use_device_addr", p))
34207 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
34208 else if (!strcmp ("use_device_ptr", p))
34209 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
34210 break;
34211 case 'v':
34212 if (!strcmp ("vector", p))
34213 result = PRAGMA_OACC_CLAUSE_VECTOR;
34214 else if (!strcmp ("vector_length", p))
34215 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
34216 break;
34217 case 'w':
34218 if (!strcmp ("wait", p))
34219 result = PRAGMA_OACC_CLAUSE_WAIT;
34220 else if (!strcmp ("worker", p))
34221 result = PRAGMA_OACC_CLAUSE_WORKER;
34222 break;
34223 }
34224 }
34225
34226 if (result != PRAGMA_OMP_CLAUSE_NONE)
34227 cp_lexer_consume_token (parser->lexer);
34228
34229 return result;
34230 }
34231
34232 /* Validate that a clause of the given type does not already exist. */
34233
34234 static void
34235 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
34236 const char *name, location_t location)
34237 {
34238 if (omp_find_clause (clauses, code))
34239 error_at (location, "too many %qs clauses", name);
34240 }
34241
34242 /* OpenMP 2.5:
34243 variable-list:
34244 identifier
34245 variable-list , identifier
34246
34247 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
34248 colon). An opening parenthesis will have been consumed by the caller.
34249
34250 If KIND is nonzero, create the appropriate node and install the decl
34251 in OMP_CLAUSE_DECL and add the node to the head of the list.
34252
34253 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
34254 return the list created.
34255
34256 COLON can be NULL if only closing parenthesis should end the list,
34257 or pointer to bool which will receive false if the list is terminated
34258 by closing parenthesis or true if the list is terminated by colon.
34259
34260 The optional ALLOW_DEREF argument is true if list items can use the deref
34261 (->) operator. */
34262
34263 static tree
34264 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
34265 tree list, bool *colon,
34266 bool allow_deref = false)
34267 {
34268 cp_token *token;
34269 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
34270 if (colon)
34271 {
34272 parser->colon_corrects_to_scope_p = false;
34273 *colon = false;
34274 }
34275 while (1)
34276 {
34277 tree name, decl;
34278
34279 if (kind == OMP_CLAUSE_DEPEND)
34280 cp_parser_parse_tentatively (parser);
34281 token = cp_lexer_peek_token (parser->lexer);
34282 if (kind != 0
34283 && current_class_ptr
34284 && cp_parser_is_keyword (token, RID_THIS))
34285 {
34286 decl = finish_this_expr ();
34287 if (TREE_CODE (decl) == NON_LVALUE_EXPR
34288 || CONVERT_EXPR_P (decl))
34289 decl = TREE_OPERAND (decl, 0);
34290 cp_lexer_consume_token (parser->lexer);
34291 }
34292 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
34293 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
34294 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
34295 {
34296 cp_id_kind idk;
34297 decl = cp_parser_primary_expression (parser, false, false, false,
34298 &idk);
34299 }
34300 else
34301 {
34302 name = cp_parser_id_expression (parser, /*template_p=*/false,
34303 /*check_dependency_p=*/true,
34304 /*template_p=*/NULL,
34305 /*declarator_p=*/false,
34306 /*optional_p=*/false);
34307 if (name == error_mark_node)
34308 {
34309 if (kind == OMP_CLAUSE_DEPEND
34310 && cp_parser_simulate_error (parser))
34311 goto depend_lvalue;
34312 goto skip_comma;
34313 }
34314
34315 if (identifier_p (name))
34316 decl = cp_parser_lookup_name_simple (parser, name, token->location);
34317 else
34318 decl = name;
34319 if (decl == error_mark_node)
34320 {
34321 if (kind == OMP_CLAUSE_DEPEND
34322 && cp_parser_simulate_error (parser))
34323 goto depend_lvalue;
34324 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
34325 token->location);
34326 }
34327 }
34328 if (outer_automatic_var_p (decl))
34329 decl = process_outer_var_ref (decl, tf_warning_or_error);
34330 if (decl == error_mark_node)
34331 ;
34332 else if (kind != 0)
34333 {
34334 switch (kind)
34335 {
34336 case OMP_CLAUSE__CACHE_:
34337 /* The OpenACC cache directive explicitly only allows "array
34338 elements or subarrays". */
34339 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
34340 {
34341 error_at (token->location, "expected %<[%>");
34342 decl = error_mark_node;
34343 break;
34344 }
34345 /* FALLTHROUGH. */
34346 case OMP_CLAUSE_MAP:
34347 case OMP_CLAUSE_FROM:
34348 case OMP_CLAUSE_TO:
34349 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
34350 || (allow_deref
34351 && cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
34352 {
34353 cpp_ttype ttype
34354 = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
34355 ? CPP_DOT : CPP_DEREF;
34356 location_t loc
34357 = cp_lexer_peek_token (parser->lexer)->location;
34358 cp_id_kind idk = CP_ID_KIND_NONE;
34359 cp_lexer_consume_token (parser->lexer);
34360 decl = convert_from_reference (decl);
34361 decl
34362 = cp_parser_postfix_dot_deref_expression (parser, ttype,
34363 decl, false,
34364 &idk, loc);
34365 }
34366 /* FALLTHROUGH. */
34367 case OMP_CLAUSE_DEPEND:
34368 case OMP_CLAUSE_REDUCTION:
34369 case OMP_CLAUSE_IN_REDUCTION:
34370 case OMP_CLAUSE_TASK_REDUCTION:
34371 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
34372 {
34373 tree low_bound = NULL_TREE, length = NULL_TREE;
34374
34375 parser->colon_corrects_to_scope_p = false;
34376 cp_lexer_consume_token (parser->lexer);
34377 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34378 low_bound = cp_parser_expression (parser);
34379 if (!colon)
34380 parser->colon_corrects_to_scope_p
34381 = saved_colon_corrects_to_scope_p;
34382 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
34383 length = integer_one_node;
34384 else
34385 {
34386 /* Look for `:'. */
34387 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34388 {
34389 if (kind == OMP_CLAUSE_DEPEND
34390 && cp_parser_simulate_error (parser))
34391 goto depend_lvalue;
34392 goto skip_comma;
34393 }
34394 if (kind == OMP_CLAUSE_DEPEND)
34395 cp_parser_commit_to_tentative_parse (parser);
34396 if (!cp_lexer_next_token_is (parser->lexer,
34397 CPP_CLOSE_SQUARE))
34398 length = cp_parser_expression (parser);
34399 }
34400 /* Look for the closing `]'. */
34401 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
34402 RT_CLOSE_SQUARE))
34403 {
34404 if (kind == OMP_CLAUSE_DEPEND
34405 && cp_parser_simulate_error (parser))
34406 goto depend_lvalue;
34407 goto skip_comma;
34408 }
34409
34410 decl = tree_cons (low_bound, length, decl);
34411 }
34412 break;
34413 default:
34414 break;
34415 }
34416
34417 if (kind == OMP_CLAUSE_DEPEND)
34418 {
34419 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
34420 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
34421 && cp_parser_simulate_error (parser))
34422 {
34423 depend_lvalue:
34424 cp_parser_abort_tentative_parse (parser);
34425 decl = cp_parser_assignment_expression (parser, NULL,
34426 false, false);
34427 }
34428 else
34429 cp_parser_parse_definitely (parser);
34430 }
34431
34432 tree u = build_omp_clause (token->location, kind);
34433 OMP_CLAUSE_DECL (u) = decl;
34434 OMP_CLAUSE_CHAIN (u) = list;
34435 list = u;
34436 }
34437 else
34438 list = tree_cons (decl, NULL_TREE, list);
34439
34440 get_comma:
34441 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
34442 break;
34443 cp_lexer_consume_token (parser->lexer);
34444 }
34445
34446 if (colon)
34447 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34448
34449 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34450 {
34451 *colon = true;
34452 cp_parser_require (parser, CPP_COLON, RT_COLON);
34453 return list;
34454 }
34455
34456 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34457 {
34458 int ending;
34459
34460 /* Try to resync to an unnested comma. Copied from
34461 cp_parser_parenthesized_expression_list. */
34462 skip_comma:
34463 if (colon)
34464 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34465 ending = cp_parser_skip_to_closing_parenthesis (parser,
34466 /*recovering=*/true,
34467 /*or_comma=*/true,
34468 /*consume_paren=*/true);
34469 if (ending < 0)
34470 goto get_comma;
34471 }
34472
34473 return list;
34474 }
34475
34476 /* Similarly, but expect leading and trailing parenthesis. This is a very
34477 common case for omp clauses. */
34478
34479 static tree
34480 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
34481 bool allow_deref = false)
34482 {
34483 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34484 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
34485 allow_deref);
34486 return list;
34487 }
34488
34489 /* OpenACC 2.0:
34490 copy ( variable-list )
34491 copyin ( variable-list )
34492 copyout ( variable-list )
34493 create ( variable-list )
34494 delete ( variable-list )
34495 present ( variable-list )
34496
34497 OpenACC 2.6:
34498 no_create ( variable-list )
34499 attach ( variable-list )
34500 detach ( variable-list ) */
34501
34502 static tree
34503 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
34504 tree list)
34505 {
34506 enum gomp_map_kind kind;
34507 switch (c_kind)
34508 {
34509 case PRAGMA_OACC_CLAUSE_ATTACH:
34510 kind = GOMP_MAP_ATTACH;
34511 break;
34512 case PRAGMA_OACC_CLAUSE_COPY:
34513 kind = GOMP_MAP_TOFROM;
34514 break;
34515 case PRAGMA_OACC_CLAUSE_COPYIN:
34516 kind = GOMP_MAP_TO;
34517 break;
34518 case PRAGMA_OACC_CLAUSE_COPYOUT:
34519 kind = GOMP_MAP_FROM;
34520 break;
34521 case PRAGMA_OACC_CLAUSE_CREATE:
34522 kind = GOMP_MAP_ALLOC;
34523 break;
34524 case PRAGMA_OACC_CLAUSE_DELETE:
34525 kind = GOMP_MAP_RELEASE;
34526 break;
34527 case PRAGMA_OACC_CLAUSE_DETACH:
34528 kind = GOMP_MAP_DETACH;
34529 break;
34530 case PRAGMA_OACC_CLAUSE_DEVICE:
34531 kind = GOMP_MAP_FORCE_TO;
34532 break;
34533 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
34534 kind = GOMP_MAP_DEVICE_RESIDENT;
34535 break;
34536 case PRAGMA_OACC_CLAUSE_HOST:
34537 kind = GOMP_MAP_FORCE_FROM;
34538 break;
34539 case PRAGMA_OACC_CLAUSE_LINK:
34540 kind = GOMP_MAP_LINK;
34541 break;
34542 case PRAGMA_OACC_CLAUSE_NO_CREATE:
34543 kind = GOMP_MAP_IF_PRESENT;
34544 break;
34545 case PRAGMA_OACC_CLAUSE_PRESENT:
34546 kind = GOMP_MAP_FORCE_PRESENT;
34547 break;
34548 default:
34549 gcc_unreachable ();
34550 }
34551 tree nl, c;
34552 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, true);
34553
34554 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
34555 OMP_CLAUSE_SET_MAP_KIND (c, kind);
34556
34557 return nl;
34558 }
34559
34560 /* OpenACC 2.0:
34561 deviceptr ( variable-list ) */
34562
34563 static tree
34564 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
34565 {
34566 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34567 tree vars, t;
34568
34569 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
34570 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
34571 variable-list must only allow for pointer variables. */
34572 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34573 for (t = vars; t; t = TREE_CHAIN (t))
34574 {
34575 tree v = TREE_PURPOSE (t);
34576 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
34577 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
34578 OMP_CLAUSE_DECL (u) = v;
34579 OMP_CLAUSE_CHAIN (u) = list;
34580 list = u;
34581 }
34582
34583 return list;
34584 }
34585
34586 /* OpenACC 2.5:
34587 auto
34588 finalize
34589 independent
34590 nohost
34591 seq */
34592
34593 static tree
34594 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
34595 tree list)
34596 {
34597 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
34598
34599 tree c = build_omp_clause (loc, code);
34600 OMP_CLAUSE_CHAIN (c) = list;
34601
34602 return c;
34603 }
34604
34605 /* OpenACC:
34606 num_gangs ( expression )
34607 num_workers ( expression )
34608 vector_length ( expression ) */
34609
34610 static tree
34611 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
34612 const char *str, tree list)
34613 {
34614 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34615
34616 matching_parens parens;
34617 if (!parens.require_open (parser))
34618 return list;
34619
34620 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
34621
34622 if (t == error_mark_node
34623 || !parens.require_close (parser))
34624 {
34625 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34626 /*or_comma=*/false,
34627 /*consume_paren=*/true);
34628 return list;
34629 }
34630
34631 check_no_duplicate_clause (list, code, str, loc);
34632
34633 tree c = build_omp_clause (loc, code);
34634 OMP_CLAUSE_OPERAND (c, 0) = t;
34635 OMP_CLAUSE_CHAIN (c) = list;
34636 return c;
34637 }
34638
34639 /* OpenACC:
34640
34641 gang [( gang-arg-list )]
34642 worker [( [num:] int-expr )]
34643 vector [( [length:] int-expr )]
34644
34645 where gang-arg is one of:
34646
34647 [num:] int-expr
34648 static: size-expr
34649
34650 and size-expr may be:
34651
34652 *
34653 int-expr
34654 */
34655
34656 static tree
34657 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
34658 omp_clause_code kind,
34659 const char *str, tree list)
34660 {
34661 const char *id = "num";
34662 cp_lexer *lexer = parser->lexer;
34663 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
34664
34665 if (kind == OMP_CLAUSE_VECTOR)
34666 id = "length";
34667
34668 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
34669 {
34670 matching_parens parens;
34671 parens.consume_open (parser);
34672
34673 do
34674 {
34675 cp_token *next = cp_lexer_peek_token (lexer);
34676 int idx = 0;
34677
34678 /* Gang static argument. */
34679 if (kind == OMP_CLAUSE_GANG
34680 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
34681 {
34682 cp_lexer_consume_token (lexer);
34683
34684 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34685 goto cleanup_error;
34686
34687 idx = 1;
34688 if (ops[idx] != NULL)
34689 {
34690 cp_parser_error (parser, "too many %<static%> arguments");
34691 goto cleanup_error;
34692 }
34693
34694 /* Check for the '*' argument. */
34695 if (cp_lexer_next_token_is (lexer, CPP_MULT)
34696 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
34697 || cp_lexer_nth_token_is (parser->lexer, 2,
34698 CPP_CLOSE_PAREN)))
34699 {
34700 cp_lexer_consume_token (lexer);
34701 ops[idx] = integer_minus_one_node;
34702
34703 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
34704 {
34705 cp_lexer_consume_token (lexer);
34706 continue;
34707 }
34708 else break;
34709 }
34710 }
34711 /* Worker num: argument and vector length: arguments. */
34712 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
34713 && id_equal (next->u.value, id)
34714 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
34715 {
34716 cp_lexer_consume_token (lexer); /* id */
34717 cp_lexer_consume_token (lexer); /* ':' */
34718 }
34719
34720 /* Now collect the actual argument. */
34721 if (ops[idx] != NULL_TREE)
34722 {
34723 cp_parser_error (parser, "unexpected argument");
34724 goto cleanup_error;
34725 }
34726
34727 tree expr = cp_parser_assignment_expression (parser, NULL, false,
34728 false);
34729 if (expr == error_mark_node)
34730 goto cleanup_error;
34731
34732 mark_exp_read (expr);
34733 ops[idx] = expr;
34734
34735 if (kind == OMP_CLAUSE_GANG
34736 && cp_lexer_next_token_is (lexer, CPP_COMMA))
34737 {
34738 cp_lexer_consume_token (lexer);
34739 continue;
34740 }
34741 break;
34742 }
34743 while (1);
34744
34745 if (!parens.require_close (parser))
34746 goto cleanup_error;
34747 }
34748
34749 check_no_duplicate_clause (list, kind, str, loc);
34750
34751 c = build_omp_clause (loc, kind);
34752
34753 if (ops[1])
34754 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
34755
34756 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
34757 OMP_CLAUSE_CHAIN (c) = list;
34758
34759 return c;
34760
34761 cleanup_error:
34762 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
34763 return list;
34764 }
34765
34766 /* OpenACC 2.0:
34767 tile ( size-expr-list ) */
34768
34769 static tree
34770 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
34771 {
34772 tree c, expr = error_mark_node;
34773 tree tile = NULL_TREE;
34774
34775 /* Collapse and tile are mutually exclusive. (The spec doesn't say
34776 so, but the spec authors never considered such a case and have
34777 differing opinions on what it might mean, including 'not
34778 allowed'.) */
34779 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
34780 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
34781 clause_loc);
34782
34783 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34784 return list;
34785
34786 do
34787 {
34788 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
34789 return list;
34790
34791 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
34792 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
34793 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
34794 {
34795 cp_lexer_consume_token (parser->lexer);
34796 expr = integer_zero_node;
34797 }
34798 else
34799 expr = cp_parser_constant_expression (parser);
34800
34801 tile = tree_cons (NULL_TREE, expr, tile);
34802 }
34803 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
34804
34805 /* Consume the trailing ')'. */
34806 cp_lexer_consume_token (parser->lexer);
34807
34808 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
34809 tile = nreverse (tile);
34810 OMP_CLAUSE_TILE_LIST (c) = tile;
34811 OMP_CLAUSE_CHAIN (c) = list;
34812 return c;
34813 }
34814
34815 /* OpenACC 2.0
34816 Parse wait clause or directive parameters. */
34817
34818 static tree
34819 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
34820 {
34821 vec<tree, va_gc> *args;
34822 tree t, args_tree;
34823
34824 args = cp_parser_parenthesized_expression_list (parser, non_attr,
34825 /*cast_p=*/false,
34826 /*allow_expansion_p=*/true,
34827 /*non_constant_p=*/NULL);
34828
34829 if (args == NULL || args->length () == 0)
34830 {
34831 if (args != NULL)
34832 {
34833 cp_parser_error (parser, "expected integer expression list");
34834 release_tree_vector (args);
34835 }
34836 return list;
34837 }
34838
34839 args_tree = build_tree_list_vec (args);
34840
34841 release_tree_vector (args);
34842
34843 for (t = args_tree; t; t = TREE_CHAIN (t))
34844 {
34845 tree targ = TREE_VALUE (t);
34846
34847 if (targ != error_mark_node)
34848 {
34849 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
34850 error ("%<wait%> expression must be integral");
34851 else
34852 {
34853 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
34854
34855 targ = mark_rvalue_use (targ);
34856 OMP_CLAUSE_DECL (c) = targ;
34857 OMP_CLAUSE_CHAIN (c) = list;
34858 list = c;
34859 }
34860 }
34861 }
34862
34863 return list;
34864 }
34865
34866 /* OpenACC:
34867 wait [( int-expr-list )] */
34868
34869 static tree
34870 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
34871 {
34872 location_t location = cp_lexer_peek_token (parser->lexer)->location;
34873
34874 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34875 list = cp_parser_oacc_wait_list (parser, location, list);
34876 else
34877 {
34878 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
34879
34880 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
34881 OMP_CLAUSE_CHAIN (c) = list;
34882 list = c;
34883 }
34884
34885 return list;
34886 }
34887
34888 /* OpenMP 3.0:
34889 collapse ( constant-expression ) */
34890
34891 static tree
34892 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
34893 {
34894 tree c, num;
34895 location_t loc;
34896 HOST_WIDE_INT n;
34897
34898 loc = cp_lexer_peek_token (parser->lexer)->location;
34899 matching_parens parens;
34900 if (!parens.require_open (parser))
34901 return list;
34902
34903 num = cp_parser_constant_expression (parser);
34904
34905 if (!parens.require_close (parser))
34906 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34907 /*or_comma=*/false,
34908 /*consume_paren=*/true);
34909
34910 if (num == error_mark_node)
34911 return list;
34912 num = fold_non_dependent_expr (num);
34913 if (!tree_fits_shwi_p (num)
34914 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
34915 || (n = tree_to_shwi (num)) <= 0
34916 || (int) n != n)
34917 {
34918 error_at (loc, "collapse argument needs positive constant integer expression");
34919 return list;
34920 }
34921
34922 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
34923 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
34924 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
34925 OMP_CLAUSE_CHAIN (c) = list;
34926 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
34927
34928 return c;
34929 }
34930
34931 /* OpenMP 2.5:
34932 default ( none | shared )
34933
34934 OpenACC:
34935 default ( none | present ) */
34936
34937 static tree
34938 cp_parser_omp_clause_default (cp_parser *parser, tree list,
34939 location_t location, bool is_oacc)
34940 {
34941 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
34942 tree c;
34943
34944 matching_parens parens;
34945 if (!parens.require_open (parser))
34946 return list;
34947 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34948 {
34949 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34950 const char *p = IDENTIFIER_POINTER (id);
34951
34952 switch (p[0])
34953 {
34954 case 'n':
34955 if (strcmp ("none", p) != 0)
34956 goto invalid_kind;
34957 kind = OMP_CLAUSE_DEFAULT_NONE;
34958 break;
34959
34960 case 'p':
34961 if (strcmp ("present", p) != 0 || !is_oacc)
34962 goto invalid_kind;
34963 kind = OMP_CLAUSE_DEFAULT_PRESENT;
34964 break;
34965
34966 case 's':
34967 if (strcmp ("shared", p) != 0 || is_oacc)
34968 goto invalid_kind;
34969 kind = OMP_CLAUSE_DEFAULT_SHARED;
34970 break;
34971
34972 default:
34973 goto invalid_kind;
34974 }
34975
34976 cp_lexer_consume_token (parser->lexer);
34977 }
34978 else
34979 {
34980 invalid_kind:
34981 if (is_oacc)
34982 cp_parser_error (parser, "expected %<none%> or %<present%>");
34983 else
34984 cp_parser_error (parser, "expected %<none%> or %<shared%>");
34985 }
34986
34987 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
34988 || !parens.require_close (parser))
34989 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34990 /*or_comma=*/false,
34991 /*consume_paren=*/true);
34992
34993 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
34994 return list;
34995
34996 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
34997 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
34998 OMP_CLAUSE_CHAIN (c) = list;
34999 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
35000
35001 return c;
35002 }
35003
35004 /* OpenMP 3.1:
35005 final ( expression ) */
35006
35007 static tree
35008 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
35009 {
35010 tree t, c;
35011
35012 matching_parens parens;
35013 if (!parens.require_open (parser))
35014 return list;
35015
35016 t = cp_parser_assignment_expression (parser);
35017
35018 if (t == error_mark_node
35019 || !parens.require_close (parser))
35020 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35021 /*or_comma=*/false,
35022 /*consume_paren=*/true);
35023
35024 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
35025
35026 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
35027 OMP_CLAUSE_FINAL_EXPR (c) = t;
35028 OMP_CLAUSE_CHAIN (c) = list;
35029
35030 return c;
35031 }
35032
35033 /* OpenMP 2.5:
35034 if ( expression )
35035
35036 OpenMP 4.5:
35037 if ( directive-name-modifier : expression )
35038
35039 directive-name-modifier:
35040 parallel | task | taskloop | target data | target | target update
35041 | target enter data | target exit data
35042
35043 OpenMP 5.0:
35044 directive-name-modifier:
35045 ... | simd | cancel */
35046
35047 static tree
35048 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
35049 bool is_omp)
35050 {
35051 tree t, c;
35052 enum tree_code if_modifier = ERROR_MARK;
35053
35054 matching_parens parens;
35055 if (!parens.require_open (parser))
35056 return list;
35057
35058 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35059 {
35060 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35061 const char *p = IDENTIFIER_POINTER (id);
35062 int n = 2;
35063
35064 if (strcmp ("cancel", p) == 0)
35065 if_modifier = VOID_CST;
35066 else if (strcmp ("parallel", p) == 0)
35067 if_modifier = OMP_PARALLEL;
35068 else if (strcmp ("simd", p) == 0)
35069 if_modifier = OMP_SIMD;
35070 else if (strcmp ("task", p) == 0)
35071 if_modifier = OMP_TASK;
35072 else if (strcmp ("taskloop", p) == 0)
35073 if_modifier = OMP_TASKLOOP;
35074 else if (strcmp ("target", p) == 0)
35075 {
35076 if_modifier = OMP_TARGET;
35077 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
35078 {
35079 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
35080 p = IDENTIFIER_POINTER (id);
35081 if (strcmp ("data", p) == 0)
35082 if_modifier = OMP_TARGET_DATA;
35083 else if (strcmp ("update", p) == 0)
35084 if_modifier = OMP_TARGET_UPDATE;
35085 else if (strcmp ("enter", p) == 0)
35086 if_modifier = OMP_TARGET_ENTER_DATA;
35087 else if (strcmp ("exit", p) == 0)
35088 if_modifier = OMP_TARGET_EXIT_DATA;
35089 if (if_modifier != OMP_TARGET)
35090 n = 3;
35091 else
35092 {
35093 location_t loc
35094 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
35095 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
35096 "or %<exit%>");
35097 if_modifier = ERROR_MARK;
35098 }
35099 if (if_modifier == OMP_TARGET_ENTER_DATA
35100 || if_modifier == OMP_TARGET_EXIT_DATA)
35101 {
35102 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
35103 {
35104 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
35105 p = IDENTIFIER_POINTER (id);
35106 if (strcmp ("data", p) == 0)
35107 n = 4;
35108 }
35109 if (n != 4)
35110 {
35111 location_t loc
35112 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
35113 error_at (loc, "expected %<data%>");
35114 if_modifier = ERROR_MARK;
35115 }
35116 }
35117 }
35118 }
35119 if (if_modifier != ERROR_MARK)
35120 {
35121 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
35122 {
35123 while (n-- > 0)
35124 cp_lexer_consume_token (parser->lexer);
35125 }
35126 else
35127 {
35128 if (n > 2)
35129 {
35130 location_t loc
35131 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
35132 error_at (loc, "expected %<:%>");
35133 }
35134 if_modifier = ERROR_MARK;
35135 }
35136 }
35137 }
35138
35139 t = cp_parser_assignment_expression (parser);
35140
35141 if (t == error_mark_node
35142 || !parens.require_close (parser))
35143 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35144 /*or_comma=*/false,
35145 /*consume_paren=*/true);
35146
35147 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
35148 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
35149 {
35150 if (if_modifier != ERROR_MARK
35151 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
35152 {
35153 const char *p = NULL;
35154 switch (if_modifier)
35155 {
35156 case VOID_CST: p = "cancel"; break;
35157 case OMP_PARALLEL: p = "parallel"; break;
35158 case OMP_SIMD: p = "simd"; break;
35159 case OMP_TASK: p = "task"; break;
35160 case OMP_TASKLOOP: p = "taskloop"; break;
35161 case OMP_TARGET_DATA: p = "target data"; break;
35162 case OMP_TARGET: p = "target"; break;
35163 case OMP_TARGET_UPDATE: p = "target update"; break;
35164 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
35165 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
35166 default: gcc_unreachable ();
35167 }
35168 error_at (location, "too many %<if%> clauses with %qs modifier",
35169 p);
35170 return list;
35171 }
35172 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
35173 {
35174 if (!is_omp)
35175 error_at (location, "too many %<if%> clauses");
35176 else
35177 error_at (location, "too many %<if%> clauses without modifier");
35178 return list;
35179 }
35180 else if (if_modifier == ERROR_MARK
35181 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
35182 {
35183 error_at (location, "if any %<if%> clause has modifier, then all "
35184 "%<if%> clauses have to use modifier");
35185 return list;
35186 }
35187 }
35188
35189 c = build_omp_clause (location, OMP_CLAUSE_IF);
35190 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
35191 OMP_CLAUSE_IF_EXPR (c) = t;
35192 OMP_CLAUSE_CHAIN (c) = list;
35193
35194 return c;
35195 }
35196
35197 /* OpenMP 3.1:
35198 mergeable */
35199
35200 static tree
35201 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
35202 tree list, location_t location)
35203 {
35204 tree c;
35205
35206 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
35207 location);
35208
35209 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
35210 OMP_CLAUSE_CHAIN (c) = list;
35211 return c;
35212 }
35213
35214 /* OpenMP 2.5:
35215 nowait */
35216
35217 static tree
35218 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
35219 tree list, location_t location)
35220 {
35221 tree c;
35222
35223 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
35224
35225 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
35226 OMP_CLAUSE_CHAIN (c) = list;
35227 return c;
35228 }
35229
35230 /* OpenMP 2.5:
35231 num_threads ( expression ) */
35232
35233 static tree
35234 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
35235 location_t location)
35236 {
35237 tree t, c;
35238
35239 matching_parens parens;
35240 if (!parens.require_open (parser))
35241 return list;
35242
35243 t = cp_parser_assignment_expression (parser);
35244
35245 if (t == error_mark_node
35246 || !parens.require_close (parser))
35247 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35248 /*or_comma=*/false,
35249 /*consume_paren=*/true);
35250
35251 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
35252 "num_threads", location);
35253
35254 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
35255 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
35256 OMP_CLAUSE_CHAIN (c) = list;
35257
35258 return c;
35259 }
35260
35261 /* OpenMP 4.5:
35262 num_tasks ( expression ) */
35263
35264 static tree
35265 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
35266 location_t location)
35267 {
35268 tree t, c;
35269
35270 matching_parens parens;
35271 if (!parens.require_open (parser))
35272 return list;
35273
35274 t = cp_parser_assignment_expression (parser);
35275
35276 if (t == error_mark_node
35277 || !parens.require_close (parser))
35278 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35279 /*or_comma=*/false,
35280 /*consume_paren=*/true);
35281
35282 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
35283 "num_tasks", location);
35284
35285 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
35286 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
35287 OMP_CLAUSE_CHAIN (c) = list;
35288
35289 return c;
35290 }
35291
35292 /* OpenMP 4.5:
35293 grainsize ( expression ) */
35294
35295 static tree
35296 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
35297 location_t location)
35298 {
35299 tree t, c;
35300
35301 matching_parens parens;
35302 if (!parens.require_open (parser))
35303 return list;
35304
35305 t = cp_parser_assignment_expression (parser);
35306
35307 if (t == error_mark_node
35308 || !parens.require_close (parser))
35309 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35310 /*or_comma=*/false,
35311 /*consume_paren=*/true);
35312
35313 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
35314 "grainsize", location);
35315
35316 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
35317 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
35318 OMP_CLAUSE_CHAIN (c) = list;
35319
35320 return c;
35321 }
35322
35323 /* OpenMP 4.5:
35324 priority ( expression ) */
35325
35326 static tree
35327 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
35328 location_t location)
35329 {
35330 tree t, c;
35331
35332 matching_parens parens;
35333 if (!parens.require_open (parser))
35334 return list;
35335
35336 t = cp_parser_assignment_expression (parser);
35337
35338 if (t == error_mark_node
35339 || !parens.require_close (parser))
35340 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35341 /*or_comma=*/false,
35342 /*consume_paren=*/true);
35343
35344 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
35345 "priority", location);
35346
35347 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
35348 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
35349 OMP_CLAUSE_CHAIN (c) = list;
35350
35351 return c;
35352 }
35353
35354 /* OpenMP 4.5:
35355 hint ( expression ) */
35356
35357 static tree
35358 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
35359 {
35360 tree t, c;
35361
35362 matching_parens parens;
35363 if (!parens.require_open (parser))
35364 return list;
35365
35366 t = cp_parser_assignment_expression (parser);
35367
35368 if (t == error_mark_node
35369 || !parens.require_close (parser))
35370 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35371 /*or_comma=*/false,
35372 /*consume_paren=*/true);
35373
35374 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
35375
35376 c = build_omp_clause (location, OMP_CLAUSE_HINT);
35377 OMP_CLAUSE_HINT_EXPR (c) = t;
35378 OMP_CLAUSE_CHAIN (c) = list;
35379
35380 return c;
35381 }
35382
35383 /* OpenMP 4.5:
35384 defaultmap ( tofrom : scalar )
35385
35386 OpenMP 5.0:
35387 defaultmap ( implicit-behavior [ : variable-category ] ) */
35388
35389 static tree
35390 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
35391 location_t location)
35392 {
35393 tree c, id;
35394 const char *p;
35395 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
35396 enum omp_clause_defaultmap_kind category
35397 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
35398
35399 matching_parens parens;
35400 if (!parens.require_open (parser))
35401 return list;
35402
35403 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
35404 p = "default";
35405 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35406 {
35407 invalid_behavior:
35408 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
35409 "%<tofrom%>, %<firstprivate%>, %<none%> "
35410 "or %<default%>");
35411 goto out_err;
35412 }
35413 else
35414 {
35415 id = cp_lexer_peek_token (parser->lexer)->u.value;
35416 p = IDENTIFIER_POINTER (id);
35417 }
35418
35419 switch (p[0])
35420 {
35421 case 'a':
35422 if (strcmp ("alloc", p) == 0)
35423 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
35424 else
35425 goto invalid_behavior;
35426 break;
35427
35428 case 'd':
35429 if (strcmp ("default", p) == 0)
35430 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
35431 else
35432 goto invalid_behavior;
35433 break;
35434
35435 case 'f':
35436 if (strcmp ("firstprivate", p) == 0)
35437 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
35438 else if (strcmp ("from", p) == 0)
35439 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
35440 else
35441 goto invalid_behavior;
35442 break;
35443
35444 case 'n':
35445 if (strcmp ("none", p) == 0)
35446 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
35447 else
35448 goto invalid_behavior;
35449 break;
35450
35451 case 't':
35452 if (strcmp ("tofrom", p) == 0)
35453 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
35454 else if (strcmp ("to", p) == 0)
35455 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
35456 else
35457 goto invalid_behavior;
35458 break;
35459
35460 default:
35461 goto invalid_behavior;
35462 }
35463 cp_lexer_consume_token (parser->lexer);
35464
35465 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
35466 {
35467 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35468 goto out_err;
35469
35470 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35471 {
35472 invalid_category:
35473 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
35474 "%<pointer%>");
35475 goto out_err;
35476 }
35477 id = cp_lexer_peek_token (parser->lexer)->u.value;
35478 p = IDENTIFIER_POINTER (id);
35479
35480 switch (p[0])
35481 {
35482 case 'a':
35483 if (strcmp ("aggregate", p) == 0)
35484 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
35485 else
35486 goto invalid_category;
35487 break;
35488
35489 case 'p':
35490 if (strcmp ("pointer", p) == 0)
35491 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
35492 else
35493 goto invalid_category;
35494 break;
35495
35496 case 's':
35497 if (strcmp ("scalar", p) == 0)
35498 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
35499 else
35500 goto invalid_category;
35501 break;
35502
35503 default:
35504 goto invalid_category;
35505 }
35506
35507 cp_lexer_consume_token (parser->lexer);
35508 }
35509 if (!parens.require_close (parser))
35510 goto out_err;
35511
35512 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
35513 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
35514 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
35515 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
35516 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
35517 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
35518 {
35519 enum omp_clause_defaultmap_kind cat = category;
35520 location_t loc = OMP_CLAUSE_LOCATION (c);
35521 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
35522 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
35523 p = NULL;
35524 switch (cat)
35525 {
35526 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
35527 p = NULL;
35528 break;
35529 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
35530 p = "aggregate";
35531 break;
35532 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
35533 p = "pointer";
35534 break;
35535 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
35536 p = "scalar";
35537 break;
35538 default:
35539 gcc_unreachable ();
35540 }
35541 if (p)
35542 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
35543 p);
35544 else
35545 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
35546 "category");
35547 break;
35548 }
35549
35550 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
35551 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
35552 OMP_CLAUSE_CHAIN (c) = list;
35553 return c;
35554
35555 out_err:
35556 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35557 /*or_comma=*/false,
35558 /*consume_paren=*/true);
35559 return list;
35560 }
35561
35562 /* OpenMP 5.0:
35563 order ( concurrent ) */
35564
35565 static tree
35566 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
35567 {
35568 tree c, id;
35569 const char *p;
35570
35571 matching_parens parens;
35572 if (!parens.require_open (parser))
35573 return list;
35574
35575 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35576 {
35577 cp_parser_error (parser, "expected %<concurrent%>");
35578 goto out_err;
35579 }
35580 else
35581 {
35582 id = cp_lexer_peek_token (parser->lexer)->u.value;
35583 p = IDENTIFIER_POINTER (id);
35584 }
35585 if (strcmp (p, "concurrent") != 0)
35586 {
35587 cp_parser_error (parser, "expected %<concurrent%>");
35588 goto out_err;
35589 }
35590 cp_lexer_consume_token (parser->lexer);
35591 if (!parens.require_close (parser))
35592 goto out_err;
35593
35594 /* check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location); */
35595 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
35596 OMP_CLAUSE_CHAIN (c) = list;
35597 return c;
35598
35599 out_err:
35600 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35601 /*or_comma=*/false,
35602 /*consume_paren=*/true);
35603 return list;
35604 }
35605
35606 /* OpenMP 5.0:
35607 bind ( teams | parallel | thread ) */
35608
35609 static tree
35610 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
35611 location_t location)
35612 {
35613 tree c;
35614 const char *p;
35615 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
35616
35617 matching_parens parens;
35618 if (!parens.require_open (parser))
35619 return list;
35620
35621 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35622 {
35623 invalid:
35624 cp_parser_error (parser,
35625 "expected %<teams%>, %<parallel%> or %<thread%>");
35626 goto out_err;
35627 }
35628 else
35629 {
35630 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35631 p = IDENTIFIER_POINTER (id);
35632 }
35633 if (strcmp (p, "teams") == 0)
35634 kind = OMP_CLAUSE_BIND_TEAMS;
35635 else if (strcmp (p, "parallel") == 0)
35636 kind = OMP_CLAUSE_BIND_PARALLEL;
35637 else if (strcmp (p, "thread") != 0)
35638 goto invalid;
35639 cp_lexer_consume_token (parser->lexer);
35640 if (!parens.require_close (parser))
35641 goto out_err;
35642
35643 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
35644 c = build_omp_clause (location, OMP_CLAUSE_BIND);
35645 OMP_CLAUSE_BIND_KIND (c) = kind;
35646 OMP_CLAUSE_CHAIN (c) = list;
35647 return c;
35648
35649 out_err:
35650 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35651 /*or_comma=*/false,
35652 /*consume_paren=*/true);
35653 return list;
35654 }
35655
35656 /* OpenMP 2.5:
35657 ordered
35658
35659 OpenMP 4.5:
35660 ordered ( constant-expression ) */
35661
35662 static tree
35663 cp_parser_omp_clause_ordered (cp_parser *parser,
35664 tree list, location_t location)
35665 {
35666 tree c, num = NULL_TREE;
35667 HOST_WIDE_INT n;
35668
35669 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
35670 "ordered", location);
35671
35672 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35673 {
35674 matching_parens parens;
35675 parens.consume_open (parser);
35676
35677 num = cp_parser_constant_expression (parser);
35678
35679 if (!parens.require_close (parser))
35680 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35681 /*or_comma=*/false,
35682 /*consume_paren=*/true);
35683
35684 if (num == error_mark_node)
35685 return list;
35686 num = fold_non_dependent_expr (num);
35687 if (!tree_fits_shwi_p (num)
35688 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
35689 || (n = tree_to_shwi (num)) <= 0
35690 || (int) n != n)
35691 {
35692 error_at (location,
35693 "ordered argument needs positive constant integer "
35694 "expression");
35695 return list;
35696 }
35697 }
35698
35699 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
35700 OMP_CLAUSE_ORDERED_EXPR (c) = num;
35701 OMP_CLAUSE_CHAIN (c) = list;
35702 return c;
35703 }
35704
35705 /* OpenMP 2.5:
35706 reduction ( reduction-operator : variable-list )
35707
35708 reduction-operator:
35709 One of: + * - & ^ | && ||
35710
35711 OpenMP 3.1:
35712
35713 reduction-operator:
35714 One of: + * - & ^ | && || min max
35715
35716 OpenMP 4.0:
35717
35718 reduction-operator:
35719 One of: + * - & ^ | && ||
35720 id-expression
35721
35722 OpenMP 5.0:
35723 reduction ( reduction-modifier, reduction-operator : variable-list )
35724 in_reduction ( reduction-operator : variable-list )
35725 task_reduction ( reduction-operator : variable-list ) */
35726
35727 static tree
35728 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
35729 bool is_omp, tree list)
35730 {
35731 enum tree_code code = ERROR_MARK;
35732 tree nlist, c, id = NULL_TREE;
35733 bool task = false;
35734 bool inscan = false;
35735
35736 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35737 return list;
35738
35739 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
35740 {
35741 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
35742 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
35743 {
35744 cp_lexer_consume_token (parser->lexer);
35745 cp_lexer_consume_token (parser->lexer);
35746 }
35747 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
35748 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
35749 {
35750 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35751 const char *p = IDENTIFIER_POINTER (id);
35752 if (strcmp (p, "task") == 0)
35753 task = true;
35754 else if (strcmp (p, "inscan") == 0)
35755 inscan = true;
35756 if (task || inscan)
35757 {
35758 cp_lexer_consume_token (parser->lexer);
35759 cp_lexer_consume_token (parser->lexer);
35760 }
35761 }
35762 }
35763
35764 switch (cp_lexer_peek_token (parser->lexer)->type)
35765 {
35766 case CPP_PLUS: code = PLUS_EXPR; break;
35767 case CPP_MULT: code = MULT_EXPR; break;
35768 case CPP_MINUS: code = MINUS_EXPR; break;
35769 case CPP_AND: code = BIT_AND_EXPR; break;
35770 case CPP_XOR: code = BIT_XOR_EXPR; break;
35771 case CPP_OR: code = BIT_IOR_EXPR; break;
35772 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
35773 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
35774 default: break;
35775 }
35776
35777 if (code != ERROR_MARK)
35778 cp_lexer_consume_token (parser->lexer);
35779 else
35780 {
35781 bool saved_colon_corrects_to_scope_p;
35782 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
35783 parser->colon_corrects_to_scope_p = false;
35784 id = cp_parser_id_expression (parser, /*template_p=*/false,
35785 /*check_dependency_p=*/true,
35786 /*template_p=*/NULL,
35787 /*declarator_p=*/false,
35788 /*optional_p=*/false);
35789 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
35790 if (identifier_p (id))
35791 {
35792 const char *p = IDENTIFIER_POINTER (id);
35793
35794 if (strcmp (p, "min") == 0)
35795 code = MIN_EXPR;
35796 else if (strcmp (p, "max") == 0)
35797 code = MAX_EXPR;
35798 else if (id == ovl_op_identifier (false, PLUS_EXPR))
35799 code = PLUS_EXPR;
35800 else if (id == ovl_op_identifier (false, MULT_EXPR))
35801 code = MULT_EXPR;
35802 else if (id == ovl_op_identifier (false, MINUS_EXPR))
35803 code = MINUS_EXPR;
35804 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
35805 code = BIT_AND_EXPR;
35806 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
35807 code = BIT_IOR_EXPR;
35808 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
35809 code = BIT_XOR_EXPR;
35810 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
35811 code = TRUTH_ANDIF_EXPR;
35812 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
35813 code = TRUTH_ORIF_EXPR;
35814 id = omp_reduction_id (code, id, NULL_TREE);
35815 tree scope = parser->scope;
35816 if (scope)
35817 id = build_qualified_name (NULL_TREE, scope, id, false);
35818 parser->scope = NULL_TREE;
35819 parser->qualifying_scope = NULL_TREE;
35820 parser->object_scope = NULL_TREE;
35821 }
35822 else
35823 {
35824 error ("invalid reduction-identifier");
35825 resync_fail:
35826 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35827 /*or_comma=*/false,
35828 /*consume_paren=*/true);
35829 return list;
35830 }
35831 }
35832
35833 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35834 goto resync_fail;
35835
35836 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
35837 NULL);
35838 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
35839 {
35840 OMP_CLAUSE_REDUCTION_CODE (c) = code;
35841 if (task)
35842 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
35843 else if (inscan)
35844 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
35845 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
35846 }
35847
35848 return nlist;
35849 }
35850
35851 /* OpenMP 2.5:
35852 schedule ( schedule-kind )
35853 schedule ( schedule-kind , expression )
35854
35855 schedule-kind:
35856 static | dynamic | guided | runtime | auto
35857
35858 OpenMP 4.5:
35859 schedule ( schedule-modifier : schedule-kind )
35860 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
35861
35862 schedule-modifier:
35863 simd
35864 monotonic
35865 nonmonotonic */
35866
35867 static tree
35868 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
35869 {
35870 tree c, t;
35871 int modifiers = 0, nmodifiers = 0;
35872
35873 matching_parens parens;
35874 if (!parens.require_open (parser))
35875 return list;
35876
35877 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
35878
35879 location_t comma = UNKNOWN_LOCATION;
35880 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35881 {
35882 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35883 const char *p = IDENTIFIER_POINTER (id);
35884 if (strcmp ("simd", p) == 0)
35885 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
35886 else if (strcmp ("monotonic", p) == 0)
35887 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
35888 else if (strcmp ("nonmonotonic", p) == 0)
35889 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
35890 else
35891 break;
35892 comma = UNKNOWN_LOCATION;
35893 cp_lexer_consume_token (parser->lexer);
35894 if (nmodifiers++ == 0
35895 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35896 {
35897 comma = cp_lexer_peek_token (parser->lexer)->location;
35898 cp_lexer_consume_token (parser->lexer);
35899 }
35900 else
35901 {
35902 cp_parser_require (parser, CPP_COLON, RT_COLON);
35903 break;
35904 }
35905 }
35906 if (comma != UNKNOWN_LOCATION)
35907 error_at (comma, "expected %<:%>");
35908
35909 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35910 {
35911 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35912 const char *p = IDENTIFIER_POINTER (id);
35913
35914 switch (p[0])
35915 {
35916 case 'd':
35917 if (strcmp ("dynamic", p) != 0)
35918 goto invalid_kind;
35919 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
35920 break;
35921
35922 case 'g':
35923 if (strcmp ("guided", p) != 0)
35924 goto invalid_kind;
35925 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
35926 break;
35927
35928 case 'r':
35929 if (strcmp ("runtime", p) != 0)
35930 goto invalid_kind;
35931 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
35932 break;
35933
35934 default:
35935 goto invalid_kind;
35936 }
35937 }
35938 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
35939 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
35940 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
35941 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
35942 else
35943 goto invalid_kind;
35944 cp_lexer_consume_token (parser->lexer);
35945
35946 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
35947 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
35948 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
35949 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
35950 {
35951 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
35952 "specified");
35953 modifiers = 0;
35954 }
35955
35956 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35957 {
35958 cp_token *token;
35959 cp_lexer_consume_token (parser->lexer);
35960
35961 token = cp_lexer_peek_token (parser->lexer);
35962 t = cp_parser_assignment_expression (parser);
35963
35964 if (t == error_mark_node)
35965 goto resync_fail;
35966 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
35967 error_at (token->location, "schedule %<runtime%> does not take "
35968 "a %<chunk_size%> parameter");
35969 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
35970 error_at (token->location, "schedule %<auto%> does not take "
35971 "a %<chunk_size%> parameter");
35972 else
35973 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
35974
35975 if (!parens.require_close (parser))
35976 goto resync_fail;
35977 }
35978 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
35979 goto resync_fail;
35980
35981 OMP_CLAUSE_SCHEDULE_KIND (c)
35982 = (enum omp_clause_schedule_kind)
35983 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
35984
35985 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
35986 OMP_CLAUSE_CHAIN (c) = list;
35987 return c;
35988
35989 invalid_kind:
35990 cp_parser_error (parser, "invalid schedule kind");
35991 resync_fail:
35992 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35993 /*or_comma=*/false,
35994 /*consume_paren=*/true);
35995 return list;
35996 }
35997
35998 /* OpenMP 3.0:
35999 untied */
36000
36001 static tree
36002 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
36003 tree list, location_t location)
36004 {
36005 tree c;
36006
36007 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
36008
36009 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
36010 OMP_CLAUSE_CHAIN (c) = list;
36011 return c;
36012 }
36013
36014 /* OpenMP 4.0:
36015 inbranch
36016 notinbranch */
36017
36018 static tree
36019 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
36020 tree list, location_t location)
36021 {
36022 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
36023 tree c = build_omp_clause (location, code);
36024 OMP_CLAUSE_CHAIN (c) = list;
36025 return c;
36026 }
36027
36028 /* OpenMP 4.0:
36029 parallel
36030 for
36031 sections
36032 taskgroup */
36033
36034 static tree
36035 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
36036 enum omp_clause_code code,
36037 tree list, location_t location)
36038 {
36039 tree c = build_omp_clause (location, code);
36040 OMP_CLAUSE_CHAIN (c) = list;
36041 return c;
36042 }
36043
36044 /* OpenMP 4.5:
36045 nogroup */
36046
36047 static tree
36048 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
36049 tree list, location_t location)
36050 {
36051 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
36052 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
36053 OMP_CLAUSE_CHAIN (c) = list;
36054 return c;
36055 }
36056
36057 /* OpenMP 4.5:
36058 simd
36059 threads */
36060
36061 static tree
36062 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
36063 enum omp_clause_code code,
36064 tree list, location_t location)
36065 {
36066 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
36067 tree c = build_omp_clause (location, code);
36068 OMP_CLAUSE_CHAIN (c) = list;
36069 return c;
36070 }
36071
36072 /* OpenMP 4.0:
36073 num_teams ( expression ) */
36074
36075 static tree
36076 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
36077 location_t location)
36078 {
36079 tree t, c;
36080
36081 matching_parens parens;
36082 if (!parens.require_open (parser))
36083 return list;
36084
36085 t = cp_parser_assignment_expression (parser);
36086
36087 if (t == error_mark_node
36088 || !parens.require_close (parser))
36089 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36090 /*or_comma=*/false,
36091 /*consume_paren=*/true);
36092
36093 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
36094 "num_teams", location);
36095
36096 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
36097 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
36098 OMP_CLAUSE_CHAIN (c) = list;
36099
36100 return c;
36101 }
36102
36103 /* OpenMP 4.0:
36104 thread_limit ( expression ) */
36105
36106 static tree
36107 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
36108 location_t location)
36109 {
36110 tree t, c;
36111
36112 matching_parens parens;
36113 if (!parens.require_open (parser))
36114 return list;
36115
36116 t = cp_parser_assignment_expression (parser);
36117
36118 if (t == error_mark_node
36119 || !parens.require_close (parser))
36120 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36121 /*or_comma=*/false,
36122 /*consume_paren=*/true);
36123
36124 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
36125 "thread_limit", location);
36126
36127 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
36128 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
36129 OMP_CLAUSE_CHAIN (c) = list;
36130
36131 return c;
36132 }
36133
36134 /* OpenMP 4.0:
36135 aligned ( variable-list )
36136 aligned ( variable-list : constant-expression ) */
36137
36138 static tree
36139 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
36140 {
36141 tree nlist, c, alignment = NULL_TREE;
36142 bool colon;
36143
36144 matching_parens parens;
36145 if (!parens.require_open (parser))
36146 return list;
36147
36148 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
36149 &colon);
36150
36151 if (colon)
36152 {
36153 alignment = cp_parser_constant_expression (parser);
36154
36155 if (!parens.require_close (parser))
36156 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36157 /*or_comma=*/false,
36158 /*consume_paren=*/true);
36159
36160 if (alignment == error_mark_node)
36161 alignment = NULL_TREE;
36162 }
36163
36164 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36165 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
36166
36167 return nlist;
36168 }
36169
36170 /* OpenMP 2.5:
36171 lastprivate ( variable-list )
36172
36173 OpenMP 5.0:
36174 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
36175
36176 static tree
36177 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
36178 {
36179 bool conditional = false;
36180
36181 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36182 return list;
36183
36184 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
36185 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
36186 {
36187 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36188 const char *p = IDENTIFIER_POINTER (id);
36189
36190 if (strcmp ("conditional", p) == 0)
36191 {
36192 conditional = true;
36193 cp_lexer_consume_token (parser->lexer);
36194 cp_lexer_consume_token (parser->lexer);
36195 }
36196 }
36197
36198 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
36199 list, NULL);
36200
36201 if (conditional)
36202 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36203 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
36204 return nlist;
36205 }
36206
36207 /* OpenMP 4.0:
36208 linear ( variable-list )
36209 linear ( variable-list : expression )
36210
36211 OpenMP 4.5:
36212 linear ( modifier ( variable-list ) )
36213 linear ( modifier ( variable-list ) : expression ) */
36214
36215 static tree
36216 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
36217 bool declare_simd)
36218 {
36219 tree nlist, c, step = integer_one_node;
36220 bool colon;
36221 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
36222
36223 matching_parens parens;
36224 if (!parens.require_open (parser))
36225 return list;
36226
36227 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36228 {
36229 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36230 const char *p = IDENTIFIER_POINTER (id);
36231
36232 if (strcmp ("ref", p) == 0)
36233 kind = OMP_CLAUSE_LINEAR_REF;
36234 else if (strcmp ("val", p) == 0)
36235 kind = OMP_CLAUSE_LINEAR_VAL;
36236 else if (strcmp ("uval", p) == 0)
36237 kind = OMP_CLAUSE_LINEAR_UVAL;
36238 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
36239 cp_lexer_consume_token (parser->lexer);
36240 else
36241 kind = OMP_CLAUSE_LINEAR_DEFAULT;
36242 }
36243
36244 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
36245 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
36246 &colon);
36247 else
36248 {
36249 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
36250 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
36251 if (colon)
36252 cp_parser_require (parser, CPP_COLON, RT_COLON);
36253 else if (!parens.require_close (parser))
36254 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36255 /*or_comma=*/false,
36256 /*consume_paren=*/true);
36257 }
36258
36259 if (colon)
36260 {
36261 step = NULL_TREE;
36262 if (declare_simd
36263 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
36264 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
36265 {
36266 cp_token *token = cp_lexer_peek_token (parser->lexer);
36267 cp_parser_parse_tentatively (parser);
36268 step = cp_parser_id_expression (parser, /*template_p=*/false,
36269 /*check_dependency_p=*/true,
36270 /*template_p=*/NULL,
36271 /*declarator_p=*/false,
36272 /*optional_p=*/false);
36273 if (step != error_mark_node)
36274 step = cp_parser_lookup_name_simple (parser, step, token->location);
36275 if (step == error_mark_node)
36276 {
36277 step = NULL_TREE;
36278 cp_parser_abort_tentative_parse (parser);
36279 }
36280 else if (!cp_parser_parse_definitely (parser))
36281 step = NULL_TREE;
36282 }
36283 if (!step)
36284 step = cp_parser_assignment_expression (parser);
36285
36286 if (!parens.require_close (parser))
36287 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36288 /*or_comma=*/false,
36289 /*consume_paren=*/true);
36290
36291 if (step == error_mark_node)
36292 return list;
36293 }
36294
36295 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36296 {
36297 OMP_CLAUSE_LINEAR_STEP (c) = step;
36298 OMP_CLAUSE_LINEAR_KIND (c) = kind;
36299 }
36300
36301 return nlist;
36302 }
36303
36304 /* OpenMP 4.0:
36305 safelen ( constant-expression ) */
36306
36307 static tree
36308 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
36309 location_t location)
36310 {
36311 tree t, c;
36312
36313 matching_parens parens;
36314 if (!parens.require_open (parser))
36315 return list;
36316
36317 t = cp_parser_constant_expression (parser);
36318
36319 if (t == error_mark_node
36320 || !parens.require_close (parser))
36321 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36322 /*or_comma=*/false,
36323 /*consume_paren=*/true);
36324
36325 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
36326
36327 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
36328 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
36329 OMP_CLAUSE_CHAIN (c) = list;
36330
36331 return c;
36332 }
36333
36334 /* OpenMP 4.0:
36335 simdlen ( constant-expression ) */
36336
36337 static tree
36338 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
36339 location_t location)
36340 {
36341 tree t, c;
36342
36343 matching_parens parens;
36344 if (!parens.require_open (parser))
36345 return list;
36346
36347 t = cp_parser_constant_expression (parser);
36348
36349 if (t == error_mark_node
36350 || !parens.require_close (parser))
36351 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36352 /*or_comma=*/false,
36353 /*consume_paren=*/true);
36354
36355 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
36356
36357 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
36358 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
36359 OMP_CLAUSE_CHAIN (c) = list;
36360
36361 return c;
36362 }
36363
36364 /* OpenMP 4.5:
36365 vec:
36366 identifier [+/- integer]
36367 vec , identifier [+/- integer]
36368 */
36369
36370 static tree
36371 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
36372 tree list)
36373 {
36374 tree vec = NULL;
36375
36376 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36377 {
36378 cp_parser_error (parser, "expected identifier");
36379 return list;
36380 }
36381
36382 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36383 {
36384 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
36385 tree t, identifier = cp_parser_identifier (parser);
36386 tree addend = NULL;
36387
36388 if (identifier == error_mark_node)
36389 t = error_mark_node;
36390 else
36391 {
36392 t = cp_parser_lookup_name_simple
36393 (parser, identifier,
36394 cp_lexer_peek_token (parser->lexer)->location);
36395 if (t == error_mark_node)
36396 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
36397 id_loc);
36398 }
36399
36400 bool neg = false;
36401 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
36402 neg = true;
36403 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
36404 {
36405 addend = integer_zero_node;
36406 goto add_to_vector;
36407 }
36408 cp_lexer_consume_token (parser->lexer);
36409
36410 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
36411 {
36412 cp_parser_error (parser, "expected integer");
36413 return list;
36414 }
36415
36416 addend = cp_lexer_peek_token (parser->lexer)->u.value;
36417 if (TREE_CODE (addend) != INTEGER_CST)
36418 {
36419 cp_parser_error (parser, "expected integer");
36420 return list;
36421 }
36422 cp_lexer_consume_token (parser->lexer);
36423
36424 add_to_vector:
36425 if (t != error_mark_node)
36426 {
36427 vec = tree_cons (addend, t, vec);
36428 if (neg)
36429 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
36430 }
36431
36432 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
36433 break;
36434
36435 cp_lexer_consume_token (parser->lexer);
36436 }
36437
36438 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
36439 {
36440 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
36441 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
36442 OMP_CLAUSE_DECL (u) = nreverse (vec);
36443 OMP_CLAUSE_CHAIN (u) = list;
36444 return u;
36445 }
36446 return list;
36447 }
36448
36449 /* OpenMP 5.0:
36450 iterators ( iterators-definition )
36451
36452 iterators-definition:
36453 iterator-specifier
36454 iterator-specifier , iterators-definition
36455
36456 iterator-specifier:
36457 identifier = range-specification
36458 iterator-type identifier = range-specification
36459
36460 range-specification:
36461 begin : end
36462 begin : end : step */
36463
36464 static tree
36465 cp_parser_omp_iterators (cp_parser *parser)
36466 {
36467 tree ret = NULL_TREE, *last = &ret;
36468 cp_lexer_consume_token (parser->lexer);
36469
36470 matching_parens parens;
36471 if (!parens.require_open (parser))
36472 return error_mark_node;
36473
36474 bool saved_colon_corrects_to_scope_p
36475 = parser->colon_corrects_to_scope_p;
36476 bool saved_colon_doesnt_start_class_def_p
36477 = parser->colon_doesnt_start_class_def_p;
36478
36479 do
36480 {
36481 tree iter_type;
36482 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
36483 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
36484 iter_type = integer_type_node;
36485 else
36486 {
36487 const char *saved_message
36488 = parser->type_definition_forbidden_message;
36489 parser->type_definition_forbidden_message
36490 = G_("types may not be defined in iterator type");
36491
36492 iter_type = cp_parser_type_id (parser);
36493
36494 parser->type_definition_forbidden_message = saved_message;
36495 }
36496
36497 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36498 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36499 {
36500 cp_parser_error (parser, "expected identifier");
36501 break;
36502 }
36503
36504 tree id = cp_parser_identifier (parser);
36505 if (id == error_mark_node)
36506 break;
36507
36508 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
36509 break;
36510
36511 parser->colon_corrects_to_scope_p = false;
36512 parser->colon_doesnt_start_class_def_p = true;
36513 tree begin = cp_parser_assignment_expression (parser);
36514
36515 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36516 break;
36517
36518 tree end = cp_parser_assignment_expression (parser);
36519
36520 tree step = integer_one_node;
36521 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
36522 {
36523 cp_lexer_consume_token (parser->lexer);
36524 step = cp_parser_assignment_expression (parser);
36525 }
36526
36527 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
36528 DECL_ARTIFICIAL (iter_var) = 1;
36529 DECL_CONTEXT (iter_var) = current_function_decl;
36530 pushdecl (iter_var);
36531
36532 *last = make_tree_vec (6);
36533 TREE_VEC_ELT (*last, 0) = iter_var;
36534 TREE_VEC_ELT (*last, 1) = begin;
36535 TREE_VEC_ELT (*last, 2) = end;
36536 TREE_VEC_ELT (*last, 3) = step;
36537 last = &TREE_CHAIN (*last);
36538
36539 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36540 {
36541 cp_lexer_consume_token (parser->lexer);
36542 continue;
36543 }
36544 break;
36545 }
36546 while (1);
36547
36548 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36549 parser->colon_doesnt_start_class_def_p
36550 = saved_colon_doesnt_start_class_def_p;
36551
36552 if (!parens.require_close (parser))
36553 cp_parser_skip_to_closing_parenthesis (parser,
36554 /*recovering=*/true,
36555 /*or_comma=*/false,
36556 /*consume_paren=*/true);
36557
36558 return ret ? ret : error_mark_node;
36559 }
36560
36561 /* OpenMP 4.0:
36562 depend ( depend-kind : variable-list )
36563
36564 depend-kind:
36565 in | out | inout
36566
36567 OpenMP 4.5:
36568 depend ( source )
36569
36570 depend ( sink : vec )
36571
36572 OpenMP 5.0:
36573 depend ( depend-modifier , depend-kind: variable-list )
36574
36575 depend-kind:
36576 in | out | inout | mutexinoutset | depobj
36577
36578 depend-modifier:
36579 iterator ( iterators-definition ) */
36580
36581 static tree
36582 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
36583 {
36584 tree nlist, c, iterators = NULL_TREE;
36585 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
36586
36587 matching_parens parens;
36588 if (!parens.require_open (parser))
36589 return list;
36590
36591 do
36592 {
36593 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36594 goto invalid_kind;
36595
36596 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36597 const char *p = IDENTIFIER_POINTER (id);
36598
36599 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
36600 {
36601 begin_scope (sk_omp, NULL);
36602 iterators = cp_parser_omp_iterators (parser);
36603 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
36604 continue;
36605 }
36606 if (strcmp ("in", p) == 0)
36607 kind = OMP_CLAUSE_DEPEND_IN;
36608 else if (strcmp ("inout", p) == 0)
36609 kind = OMP_CLAUSE_DEPEND_INOUT;
36610 else if (strcmp ("mutexinoutset", p) == 0)
36611 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
36612 else if (strcmp ("out", p) == 0)
36613 kind = OMP_CLAUSE_DEPEND_OUT;
36614 else if (strcmp ("depobj", p) == 0)
36615 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
36616 else if (strcmp ("sink", p) == 0)
36617 kind = OMP_CLAUSE_DEPEND_SINK;
36618 else if (strcmp ("source", p) == 0)
36619 kind = OMP_CLAUSE_DEPEND_SOURCE;
36620 else
36621 goto invalid_kind;
36622 break;
36623 }
36624 while (1);
36625
36626 cp_lexer_consume_token (parser->lexer);
36627
36628 if (iterators
36629 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
36630 {
36631 poplevel (0, 1, 0);
36632 error_at (loc, "%<iterator%> modifier incompatible with %qs",
36633 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
36634 iterators = NULL_TREE;
36635 }
36636
36637 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
36638 {
36639 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
36640 OMP_CLAUSE_DEPEND_KIND (c) = kind;
36641 OMP_CLAUSE_DECL (c) = NULL_TREE;
36642 OMP_CLAUSE_CHAIN (c) = list;
36643 if (!parens.require_close (parser))
36644 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36645 /*or_comma=*/false,
36646 /*consume_paren=*/true);
36647 return c;
36648 }
36649
36650 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36651 goto resync_fail;
36652
36653 if (kind == OMP_CLAUSE_DEPEND_SINK)
36654 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
36655 else
36656 {
36657 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
36658 list, NULL);
36659
36660 if (iterators)
36661 {
36662 tree block = poplevel (1, 1, 0);
36663 if (iterators == error_mark_node)
36664 iterators = NULL_TREE;
36665 else
36666 TREE_VEC_ELT (iterators, 5) = block;
36667 }
36668
36669 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36670 {
36671 OMP_CLAUSE_DEPEND_KIND (c) = kind;
36672 if (iterators)
36673 OMP_CLAUSE_DECL (c)
36674 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
36675 }
36676 }
36677 return nlist;
36678
36679 invalid_kind:
36680 cp_parser_error (parser, "invalid depend kind");
36681 resync_fail:
36682 if (iterators)
36683 poplevel (0, 1, 0);
36684 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36685 /*or_comma=*/false,
36686 /*consume_paren=*/true);
36687 return list;
36688 }
36689
36690 /* OpenMP 4.0:
36691 map ( map-kind : variable-list )
36692 map ( variable-list )
36693
36694 map-kind:
36695 alloc | to | from | tofrom
36696
36697 OpenMP 4.5:
36698 map-kind:
36699 alloc | to | from | tofrom | release | delete
36700
36701 map ( always [,] map-kind: variable-list ) */
36702
36703 static tree
36704 cp_parser_omp_clause_map (cp_parser *parser, tree list)
36705 {
36706 tree nlist, c;
36707 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
36708 bool always = false;
36709
36710 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36711 return list;
36712
36713 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36714 {
36715 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36716 const char *p = IDENTIFIER_POINTER (id);
36717
36718 if (strcmp ("always", p) == 0)
36719 {
36720 int nth = 2;
36721 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
36722 nth++;
36723 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
36724 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
36725 == RID_DELETE))
36726 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
36727 == CPP_COLON))
36728 {
36729 always = true;
36730 cp_lexer_consume_token (parser->lexer);
36731 if (nth == 3)
36732 cp_lexer_consume_token (parser->lexer);
36733 }
36734 }
36735 }
36736
36737 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
36738 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
36739 {
36740 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36741 const char *p = IDENTIFIER_POINTER (id);
36742
36743 if (strcmp ("alloc", p) == 0)
36744 kind = GOMP_MAP_ALLOC;
36745 else if (strcmp ("to", p) == 0)
36746 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
36747 else if (strcmp ("from", p) == 0)
36748 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
36749 else if (strcmp ("tofrom", p) == 0)
36750 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
36751 else if (strcmp ("release", p) == 0)
36752 kind = GOMP_MAP_RELEASE;
36753 else
36754 {
36755 cp_parser_error (parser, "invalid map kind");
36756 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36757 /*or_comma=*/false,
36758 /*consume_paren=*/true);
36759 return list;
36760 }
36761 cp_lexer_consume_token (parser->lexer);
36762 cp_lexer_consume_token (parser->lexer);
36763 }
36764 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
36765 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
36766 {
36767 kind = GOMP_MAP_DELETE;
36768 cp_lexer_consume_token (parser->lexer);
36769 cp_lexer_consume_token (parser->lexer);
36770 }
36771
36772 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
36773 NULL);
36774
36775 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
36776 OMP_CLAUSE_SET_MAP_KIND (c, kind);
36777
36778 return nlist;
36779 }
36780
36781 /* OpenMP 4.0:
36782 device ( expression ) */
36783
36784 static tree
36785 cp_parser_omp_clause_device (cp_parser *parser, tree list,
36786 location_t location)
36787 {
36788 tree t, c;
36789
36790 matching_parens parens;
36791 if (!parens.require_open (parser))
36792 return list;
36793
36794 t = cp_parser_assignment_expression (parser);
36795
36796 if (t == error_mark_node
36797 || !parens.require_close (parser))
36798 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36799 /*or_comma=*/false,
36800 /*consume_paren=*/true);
36801
36802 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
36803 "device", location);
36804
36805 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
36806 OMP_CLAUSE_DEVICE_ID (c) = t;
36807 OMP_CLAUSE_CHAIN (c) = list;
36808
36809 return c;
36810 }
36811
36812 /* OpenMP 4.0:
36813 dist_schedule ( static )
36814 dist_schedule ( static , expression ) */
36815
36816 static tree
36817 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
36818 location_t location)
36819 {
36820 tree c, t;
36821
36822 matching_parens parens;
36823 if (!parens.require_open (parser))
36824 return list;
36825
36826 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
36827
36828 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
36829 goto invalid_kind;
36830 cp_lexer_consume_token (parser->lexer);
36831
36832 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36833 {
36834 cp_lexer_consume_token (parser->lexer);
36835
36836 t = cp_parser_assignment_expression (parser);
36837
36838 if (t == error_mark_node)
36839 goto resync_fail;
36840 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
36841
36842 if (!parens.require_close (parser))
36843 goto resync_fail;
36844 }
36845 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
36846 goto resync_fail;
36847
36848 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
36849 "dist_schedule", location); */
36850 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
36851 warning_at (location, 0, "too many %qs clauses", "dist_schedule");
36852 OMP_CLAUSE_CHAIN (c) = list;
36853 return c;
36854
36855 invalid_kind:
36856 cp_parser_error (parser, "invalid dist_schedule kind");
36857 resync_fail:
36858 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36859 /*or_comma=*/false,
36860 /*consume_paren=*/true);
36861 return list;
36862 }
36863
36864 /* OpenMP 4.0:
36865 proc_bind ( proc-bind-kind )
36866
36867 proc-bind-kind:
36868 master | close | spread */
36869
36870 static tree
36871 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
36872 location_t location)
36873 {
36874 tree c;
36875 enum omp_clause_proc_bind_kind kind;
36876
36877 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36878 return list;
36879
36880 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36881 {
36882 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36883 const char *p = IDENTIFIER_POINTER (id);
36884
36885 if (strcmp ("master", p) == 0)
36886 kind = OMP_CLAUSE_PROC_BIND_MASTER;
36887 else if (strcmp ("close", p) == 0)
36888 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
36889 else if (strcmp ("spread", p) == 0)
36890 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
36891 else
36892 goto invalid_kind;
36893 }
36894 else
36895 goto invalid_kind;
36896
36897 cp_lexer_consume_token (parser->lexer);
36898 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
36899 goto resync_fail;
36900
36901 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
36902 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
36903 location);
36904 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
36905 OMP_CLAUSE_CHAIN (c) = list;
36906 return c;
36907
36908 invalid_kind:
36909 cp_parser_error (parser, "invalid depend kind");
36910 resync_fail:
36911 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36912 /*or_comma=*/false,
36913 /*consume_paren=*/true);
36914 return list;
36915 }
36916
36917 /* OpenMP 5.0:
36918 device_type ( host | nohost | any ) */
36919
36920 static tree
36921 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
36922 location_t location)
36923 {
36924 tree c;
36925 enum omp_clause_device_type_kind kind;
36926
36927 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36928 return list;
36929
36930 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36931 {
36932 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36933 const char *p = IDENTIFIER_POINTER (id);
36934
36935 if (strcmp ("host", p) == 0)
36936 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
36937 else if (strcmp ("nohost", p) == 0)
36938 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
36939 else if (strcmp ("any", p) == 0)
36940 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
36941 else
36942 goto invalid_kind;
36943 }
36944 else
36945 goto invalid_kind;
36946
36947 cp_lexer_consume_token (parser->lexer);
36948 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
36949 goto resync_fail;
36950
36951 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
36952 /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
36953 location); */
36954 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
36955 OMP_CLAUSE_CHAIN (c) = list;
36956 return c;
36957
36958 invalid_kind:
36959 cp_parser_error (parser, "invalid depend kind");
36960 resync_fail:
36961 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36962 /*or_comma=*/false,
36963 /*consume_paren=*/true);
36964 return list;
36965 }
36966
36967 /* OpenACC:
36968 async [( int-expr )] */
36969
36970 static tree
36971 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
36972 {
36973 tree c, t;
36974 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36975
36976 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
36977
36978 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36979 {
36980 matching_parens parens;
36981 parens.consume_open (parser);
36982
36983 t = cp_parser_expression (parser);
36984 if (t == error_mark_node
36985 || !parens.require_close (parser))
36986 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36987 /*or_comma=*/false,
36988 /*consume_paren=*/true);
36989 }
36990
36991 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
36992
36993 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
36994 OMP_CLAUSE_ASYNC_EXPR (c) = t;
36995 OMP_CLAUSE_CHAIN (c) = list;
36996 list = c;
36997
36998 return list;
36999 }
37000
37001 /* Parse all OpenACC clauses. The set clauses allowed by the directive
37002 is a bitmask in MASK. Return the list of clauses found. */
37003
37004 static tree
37005 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
37006 const char *where, cp_token *pragma_tok,
37007 bool finish_p = true)
37008 {
37009 tree clauses = NULL;
37010 bool first = true;
37011
37012 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37013 {
37014 location_t here;
37015 pragma_omp_clause c_kind;
37016 omp_clause_code code;
37017 const char *c_name;
37018 tree prev = clauses;
37019
37020 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37021 cp_lexer_consume_token (parser->lexer);
37022
37023 here = cp_lexer_peek_token (parser->lexer)->location;
37024 c_kind = cp_parser_omp_clause_name (parser);
37025
37026 switch (c_kind)
37027 {
37028 case PRAGMA_OACC_CLAUSE_ASYNC:
37029 clauses = cp_parser_oacc_clause_async (parser, clauses);
37030 c_name = "async";
37031 break;
37032 case PRAGMA_OACC_CLAUSE_AUTO:
37033 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
37034 clauses);
37035 c_name = "auto";
37036 break;
37037 case PRAGMA_OACC_CLAUSE_ATTACH:
37038 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37039 c_name = "attach";
37040 break;
37041 case PRAGMA_OACC_CLAUSE_COLLAPSE:
37042 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
37043 c_name = "collapse";
37044 break;
37045 case PRAGMA_OACC_CLAUSE_COPY:
37046 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37047 c_name = "copy";
37048 break;
37049 case PRAGMA_OACC_CLAUSE_COPYIN:
37050 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37051 c_name = "copyin";
37052 break;
37053 case PRAGMA_OACC_CLAUSE_COPYOUT:
37054 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37055 c_name = "copyout";
37056 break;
37057 case PRAGMA_OACC_CLAUSE_CREATE:
37058 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37059 c_name = "create";
37060 break;
37061 case PRAGMA_OACC_CLAUSE_DELETE:
37062 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37063 c_name = "delete";
37064 break;
37065 case PRAGMA_OMP_CLAUSE_DEFAULT:
37066 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
37067 c_name = "default";
37068 break;
37069 case PRAGMA_OACC_CLAUSE_DETACH:
37070 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37071 c_name = "detach";
37072 break;
37073 case PRAGMA_OACC_CLAUSE_DEVICE:
37074 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37075 c_name = "device";
37076 break;
37077 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
37078 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
37079 c_name = "deviceptr";
37080 break;
37081 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
37082 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37083 c_name = "device_resident";
37084 break;
37085 case PRAGMA_OACC_CLAUSE_FINALIZE:
37086 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
37087 clauses);
37088 c_name = "finalize";
37089 break;
37090 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
37091 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37092 clauses);
37093 c_name = "firstprivate";
37094 break;
37095 case PRAGMA_OACC_CLAUSE_GANG:
37096 c_name = "gang";
37097 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
37098 c_name, clauses);
37099 break;
37100 case PRAGMA_OACC_CLAUSE_HOST:
37101 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37102 c_name = "host";
37103 break;
37104 case PRAGMA_OACC_CLAUSE_IF:
37105 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
37106 c_name = "if";
37107 break;
37108 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
37109 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
37110 clauses);
37111 c_name = "if_present";
37112 break;
37113 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
37114 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
37115 clauses);
37116 c_name = "independent";
37117 break;
37118 case PRAGMA_OACC_CLAUSE_LINK:
37119 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37120 c_name = "link";
37121 break;
37122 case PRAGMA_OACC_CLAUSE_NO_CREATE:
37123 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37124 c_name = "no_create";
37125 break;
37126 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
37127 code = OMP_CLAUSE_NUM_GANGS;
37128 c_name = "num_gangs";
37129 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
37130 clauses);
37131 break;
37132 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
37133 c_name = "num_workers";
37134 code = OMP_CLAUSE_NUM_WORKERS;
37135 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
37136 clauses);
37137 break;
37138 case PRAGMA_OACC_CLAUSE_PRESENT:
37139 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
37140 c_name = "present";
37141 break;
37142 case PRAGMA_OACC_CLAUSE_PRIVATE:
37143 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
37144 clauses);
37145 c_name = "private";
37146 break;
37147 case PRAGMA_OACC_CLAUSE_REDUCTION:
37148 clauses
37149 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
37150 false, clauses);
37151 c_name = "reduction";
37152 break;
37153 case PRAGMA_OACC_CLAUSE_SEQ:
37154 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
37155 clauses);
37156 c_name = "seq";
37157 break;
37158 case PRAGMA_OACC_CLAUSE_TILE:
37159 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
37160 c_name = "tile";
37161 break;
37162 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
37163 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
37164 clauses);
37165 c_name = "use_device";
37166 break;
37167 case PRAGMA_OACC_CLAUSE_VECTOR:
37168 c_name = "vector";
37169 clauses = cp_parser_oacc_shape_clause (parser, here,
37170 OMP_CLAUSE_VECTOR,
37171 c_name, clauses);
37172 break;
37173 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
37174 c_name = "vector_length";
37175 code = OMP_CLAUSE_VECTOR_LENGTH;
37176 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
37177 clauses);
37178 break;
37179 case PRAGMA_OACC_CLAUSE_WAIT:
37180 clauses = cp_parser_oacc_clause_wait (parser, clauses);
37181 c_name = "wait";
37182 break;
37183 case PRAGMA_OACC_CLAUSE_WORKER:
37184 c_name = "worker";
37185 clauses = cp_parser_oacc_shape_clause (parser, here,
37186 OMP_CLAUSE_WORKER,
37187 c_name, clauses);
37188 break;
37189 default:
37190 cp_parser_error (parser, "expected %<#pragma acc%> clause");
37191 goto saw_error;
37192 }
37193
37194 first = false;
37195
37196 if (((mask >> c_kind) & 1) == 0)
37197 {
37198 /* Remove the invalid clause(s) from the list to avoid
37199 confusing the rest of the compiler. */
37200 clauses = prev;
37201 error_at (here, "%qs is not valid for %qs", c_name, where);
37202 }
37203 }
37204
37205 saw_error:
37206 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37207
37208 if (finish_p)
37209 return finish_omp_clauses (clauses, C_ORT_ACC);
37210
37211 return clauses;
37212 }
37213
37214 /* Parse all OpenMP clauses. The set clauses allowed by the directive
37215 is a bitmask in MASK. Return the list of clauses found.
37216 FINISH_P set if finish_omp_clauses should be called.
37217 NESTED non-zero if clauses should be terminated by closing paren instead
37218 of end of pragma. If it is 2, additionally commas are required in between
37219 the clauses. */
37220
37221 static tree
37222 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
37223 const char *where, cp_token *pragma_tok,
37224 bool finish_p = true, int nested = 0)
37225 {
37226 tree clauses = NULL;
37227 bool first = true;
37228 cp_token *token = NULL;
37229
37230 /* Don't create location wrapper nodes within OpenMP clauses. */
37231 auto_suppress_location_wrappers sentinel;
37232
37233 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37234 {
37235 pragma_omp_clause c_kind;
37236 const char *c_name;
37237 tree prev = clauses;
37238
37239 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37240 break;
37241
37242 if (!first)
37243 {
37244 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37245 cp_lexer_consume_token (parser->lexer);
37246 else if (nested == 2)
37247 error_at (cp_lexer_peek_token (parser->lexer)->location,
37248 "clauses in %<simd%> trait should be separated "
37249 "by %<,%>");
37250 }
37251
37252 token = cp_lexer_peek_token (parser->lexer);
37253 c_kind = cp_parser_omp_clause_name (parser);
37254
37255 switch (c_kind)
37256 {
37257 case PRAGMA_OMP_CLAUSE_BIND:
37258 clauses = cp_parser_omp_clause_bind (parser, clauses,
37259 token->location);
37260 c_name = "bind";
37261 break;
37262 case PRAGMA_OMP_CLAUSE_COLLAPSE:
37263 clauses = cp_parser_omp_clause_collapse (parser, clauses,
37264 token->location);
37265 c_name = "collapse";
37266 break;
37267 case PRAGMA_OMP_CLAUSE_COPYIN:
37268 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
37269 c_name = "copyin";
37270 break;
37271 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
37272 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
37273 clauses);
37274 c_name = "copyprivate";
37275 break;
37276 case PRAGMA_OMP_CLAUSE_DEFAULT:
37277 clauses = cp_parser_omp_clause_default (parser, clauses,
37278 token->location, false);
37279 c_name = "default";
37280 break;
37281 case PRAGMA_OMP_CLAUSE_FINAL:
37282 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
37283 c_name = "final";
37284 break;
37285 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
37286 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37287 clauses);
37288 c_name = "firstprivate";
37289 break;
37290 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
37291 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
37292 token->location);
37293 c_name = "grainsize";
37294 break;
37295 case PRAGMA_OMP_CLAUSE_HINT:
37296 clauses = cp_parser_omp_clause_hint (parser, clauses,
37297 token->location);
37298 c_name = "hint";
37299 break;
37300 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
37301 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
37302 token->location);
37303 c_name = "defaultmap";
37304 break;
37305 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
37306 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
37307 clauses);
37308 c_name = "use_device_ptr";
37309 break;
37310 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
37311 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
37312 clauses);
37313 c_name = "use_device_addr";
37314 break;
37315 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
37316 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
37317 clauses);
37318 c_name = "is_device_ptr";
37319 break;
37320 case PRAGMA_OMP_CLAUSE_IF:
37321 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
37322 true);
37323 c_name = "if";
37324 break;
37325 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
37326 clauses
37327 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
37328 true, clauses);
37329 c_name = "in_reduction";
37330 break;
37331 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
37332 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
37333 c_name = "lastprivate";
37334 break;
37335 case PRAGMA_OMP_CLAUSE_MERGEABLE:
37336 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
37337 token->location);
37338 c_name = "mergeable";
37339 break;
37340 case PRAGMA_OMP_CLAUSE_NOWAIT:
37341 clauses = cp_parser_omp_clause_nowait (parser, clauses,
37342 token->location);
37343 c_name = "nowait";
37344 break;
37345 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
37346 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
37347 token->location);
37348 c_name = "num_tasks";
37349 break;
37350 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
37351 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
37352 token->location);
37353 c_name = "num_threads";
37354 break;
37355 case PRAGMA_OMP_CLAUSE_ORDER:
37356 clauses = cp_parser_omp_clause_order (parser, clauses,
37357 token->location);
37358 c_name = "order";
37359 break;
37360 case PRAGMA_OMP_CLAUSE_ORDERED:
37361 clauses = cp_parser_omp_clause_ordered (parser, clauses,
37362 token->location);
37363 c_name = "ordered";
37364 break;
37365 case PRAGMA_OMP_CLAUSE_PRIORITY:
37366 clauses = cp_parser_omp_clause_priority (parser, clauses,
37367 token->location);
37368 c_name = "priority";
37369 break;
37370 case PRAGMA_OMP_CLAUSE_PRIVATE:
37371 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
37372 clauses);
37373 c_name = "private";
37374 break;
37375 case PRAGMA_OMP_CLAUSE_REDUCTION:
37376 clauses
37377 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
37378 true, clauses);
37379 c_name = "reduction";
37380 break;
37381 case PRAGMA_OMP_CLAUSE_SCHEDULE:
37382 clauses = cp_parser_omp_clause_schedule (parser, clauses,
37383 token->location);
37384 c_name = "schedule";
37385 break;
37386 case PRAGMA_OMP_CLAUSE_SHARED:
37387 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
37388 clauses);
37389 c_name = "shared";
37390 break;
37391 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
37392 clauses
37393 = cp_parser_omp_clause_reduction (parser,
37394 OMP_CLAUSE_TASK_REDUCTION,
37395 true, clauses);
37396 c_name = "task_reduction";
37397 break;
37398 case PRAGMA_OMP_CLAUSE_UNTIED:
37399 clauses = cp_parser_omp_clause_untied (parser, clauses,
37400 token->location);
37401 c_name = "untied";
37402 break;
37403 case PRAGMA_OMP_CLAUSE_INBRANCH:
37404 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
37405 clauses, token->location);
37406 c_name = "inbranch";
37407 break;
37408 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
37409 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
37410 clauses);
37411 c_name = "nontemporal";
37412 break;
37413 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
37414 clauses = cp_parser_omp_clause_branch (parser,
37415 OMP_CLAUSE_NOTINBRANCH,
37416 clauses, token->location);
37417 c_name = "notinbranch";
37418 break;
37419 case PRAGMA_OMP_CLAUSE_PARALLEL:
37420 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
37421 clauses, token->location);
37422 c_name = "parallel";
37423 if (!first)
37424 {
37425 clause_not_first:
37426 error_at (token->location, "%qs must be the first clause of %qs",
37427 c_name, where);
37428 clauses = prev;
37429 }
37430 break;
37431 case PRAGMA_OMP_CLAUSE_FOR:
37432 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
37433 clauses, token->location);
37434 c_name = "for";
37435 if (!first)
37436 goto clause_not_first;
37437 break;
37438 case PRAGMA_OMP_CLAUSE_SECTIONS:
37439 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
37440 clauses, token->location);
37441 c_name = "sections";
37442 if (!first)
37443 goto clause_not_first;
37444 break;
37445 case PRAGMA_OMP_CLAUSE_TASKGROUP:
37446 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
37447 clauses, token->location);
37448 c_name = "taskgroup";
37449 if (!first)
37450 goto clause_not_first;
37451 break;
37452 case PRAGMA_OMP_CLAUSE_LINK:
37453 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
37454 c_name = "to";
37455 break;
37456 case PRAGMA_OMP_CLAUSE_TO:
37457 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
37458 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37459 clauses);
37460 else
37461 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
37462 c_name = "to";
37463 break;
37464 case PRAGMA_OMP_CLAUSE_FROM:
37465 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
37466 c_name = "from";
37467 break;
37468 case PRAGMA_OMP_CLAUSE_UNIFORM:
37469 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
37470 clauses);
37471 c_name = "uniform";
37472 break;
37473 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
37474 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
37475 token->location);
37476 c_name = "num_teams";
37477 break;
37478 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
37479 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
37480 token->location);
37481 c_name = "thread_limit";
37482 break;
37483 case PRAGMA_OMP_CLAUSE_ALIGNED:
37484 clauses = cp_parser_omp_clause_aligned (parser, clauses);
37485 c_name = "aligned";
37486 break;
37487 case PRAGMA_OMP_CLAUSE_LINEAR:
37488 {
37489 bool declare_simd = false;
37490 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
37491 declare_simd = true;
37492 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
37493 }
37494 c_name = "linear";
37495 break;
37496 case PRAGMA_OMP_CLAUSE_DEPEND:
37497 clauses = cp_parser_omp_clause_depend (parser, clauses,
37498 token->location);
37499 c_name = "depend";
37500 break;
37501 case PRAGMA_OMP_CLAUSE_MAP:
37502 clauses = cp_parser_omp_clause_map (parser, clauses);
37503 c_name = "map";
37504 break;
37505 case PRAGMA_OMP_CLAUSE_DEVICE:
37506 clauses = cp_parser_omp_clause_device (parser, clauses,
37507 token->location);
37508 c_name = "device";
37509 break;
37510 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
37511 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
37512 token->location);
37513 c_name = "dist_schedule";
37514 break;
37515 case PRAGMA_OMP_CLAUSE_PROC_BIND:
37516 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
37517 token->location);
37518 c_name = "proc_bind";
37519 break;
37520 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
37521 clauses = cp_parser_omp_clause_device_type (parser, clauses,
37522 token->location);
37523 c_name = "device_type";
37524 break;
37525 case PRAGMA_OMP_CLAUSE_SAFELEN:
37526 clauses = cp_parser_omp_clause_safelen (parser, clauses,
37527 token->location);
37528 c_name = "safelen";
37529 break;
37530 case PRAGMA_OMP_CLAUSE_SIMDLEN:
37531 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
37532 token->location);
37533 c_name = "simdlen";
37534 break;
37535 case PRAGMA_OMP_CLAUSE_NOGROUP:
37536 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
37537 token->location);
37538 c_name = "nogroup";
37539 break;
37540 case PRAGMA_OMP_CLAUSE_THREADS:
37541 clauses
37542 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
37543 clauses, token->location);
37544 c_name = "threads";
37545 break;
37546 case PRAGMA_OMP_CLAUSE_SIMD:
37547 clauses
37548 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
37549 clauses, token->location);
37550 c_name = "simd";
37551 break;
37552 default:
37553 cp_parser_error (parser, "expected %<#pragma omp%> clause");
37554 goto saw_error;
37555 }
37556
37557 first = false;
37558
37559 if (((mask >> c_kind) & 1) == 0)
37560 {
37561 /* Remove the invalid clause(s) from the list to avoid
37562 confusing the rest of the compiler. */
37563 clauses = prev;
37564 error_at (token->location, "%qs is not valid for %qs", c_name, where);
37565 }
37566 }
37567 saw_error:
37568 if (!nested)
37569 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37570 if (finish_p)
37571 {
37572 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
37573 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
37574 else
37575 return finish_omp_clauses (clauses, C_ORT_OMP);
37576 }
37577 return clauses;
37578 }
37579
37580 /* OpenMP 2.5:
37581 structured-block:
37582 statement
37583
37584 In practice, we're also interested in adding the statement to an
37585 outer node. So it is convenient if we work around the fact that
37586 cp_parser_statement calls add_stmt. */
37587
37588 static unsigned
37589 cp_parser_begin_omp_structured_block (cp_parser *parser)
37590 {
37591 unsigned save = parser->in_statement;
37592
37593 /* Only move the values to IN_OMP_BLOCK if they weren't false.
37594 This preserves the "not within loop or switch" style error messages
37595 for nonsense cases like
37596 void foo() {
37597 #pragma omp single
37598 break;
37599 }
37600 */
37601 if (parser->in_statement)
37602 parser->in_statement = IN_OMP_BLOCK;
37603
37604 return save;
37605 }
37606
37607 static void
37608 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
37609 {
37610 parser->in_statement = save;
37611 }
37612
37613 static tree
37614 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
37615 {
37616 tree stmt = begin_omp_structured_block ();
37617 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37618
37619 cp_parser_statement (parser, NULL_TREE, false, if_p);
37620
37621 cp_parser_end_omp_structured_block (parser, save);
37622 return finish_omp_structured_block (stmt);
37623 }
37624
37625 /* OpenMP 2.5:
37626 # pragma omp atomic new-line
37627 expression-stmt
37628
37629 expression-stmt:
37630 x binop= expr | x++ | ++x | x-- | --x
37631 binop:
37632 +, *, -, /, &, ^, |, <<, >>
37633
37634 where x is an lvalue expression with scalar type.
37635
37636 OpenMP 3.1:
37637 # pragma omp atomic new-line
37638 update-stmt
37639
37640 # pragma omp atomic read new-line
37641 read-stmt
37642
37643 # pragma omp atomic write new-line
37644 write-stmt
37645
37646 # pragma omp atomic update new-line
37647 update-stmt
37648
37649 # pragma omp atomic capture new-line
37650 capture-stmt
37651
37652 # pragma omp atomic capture new-line
37653 capture-block
37654
37655 read-stmt:
37656 v = x
37657 write-stmt:
37658 x = expr
37659 update-stmt:
37660 expression-stmt | x = x binop expr
37661 capture-stmt:
37662 v = expression-stmt
37663 capture-block:
37664 { v = x; update-stmt; } | { update-stmt; v = x; }
37665
37666 OpenMP 4.0:
37667 update-stmt:
37668 expression-stmt | x = x binop expr | x = expr binop x
37669 capture-stmt:
37670 v = update-stmt
37671 capture-block:
37672 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
37673
37674 where x and v are lvalue expressions with scalar type. */
37675
37676 static void
37677 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
37678 {
37679 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
37680 tree rhs1 = NULL_TREE, orig_lhs;
37681 location_t loc = pragma_tok->location;
37682 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
37683 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
37684 bool structured_block = false;
37685 bool first = true;
37686 tree clauses = NULL_TREE;
37687
37688 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37689 {
37690 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37691 cp_lexer_consume_token (parser->lexer);
37692
37693 first = false;
37694
37695 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37696 {
37697 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37698 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
37699 const char *p = IDENTIFIER_POINTER (id);
37700 enum tree_code new_code = ERROR_MARK;
37701 enum omp_memory_order new_memory_order
37702 = OMP_MEMORY_ORDER_UNSPECIFIED;
37703
37704 if (!strcmp (p, "read"))
37705 new_code = OMP_ATOMIC_READ;
37706 else if (!strcmp (p, "write"))
37707 new_code = NOP_EXPR;
37708 else if (!strcmp (p, "update"))
37709 new_code = OMP_ATOMIC;
37710 else if (!strcmp (p, "capture"))
37711 new_code = OMP_ATOMIC_CAPTURE_NEW;
37712 else if (!strcmp (p, "seq_cst"))
37713 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
37714 else if (!strcmp (p, "acq_rel"))
37715 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
37716 else if (!strcmp (p, "release"))
37717 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
37718 else if (!strcmp (p, "acquire"))
37719 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
37720 else if (!strcmp (p, "relaxed"))
37721 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
37722 else if (!strcmp (p, "hint"))
37723 {
37724 cp_lexer_consume_token (parser->lexer);
37725 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
37726 continue;
37727 }
37728 else
37729 {
37730 p = NULL;
37731 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
37732 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
37733 "%<release%>, %<relaxed%> or %<hint%> clause");
37734 }
37735 if (p)
37736 {
37737 if (new_code != ERROR_MARK)
37738 {
37739 if (code != ERROR_MARK)
37740 error_at (cloc, "too many atomic clauses");
37741 else
37742 code = new_code;
37743 }
37744 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
37745 {
37746 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
37747 error_at (cloc, "too many memory order clauses");
37748 else
37749 memory_order = new_memory_order;
37750 }
37751 cp_lexer_consume_token (parser->lexer);
37752 continue;
37753 }
37754 }
37755 break;
37756 }
37757 cp_parser_require_pragma_eol (parser, pragma_tok);
37758
37759 if (code == ERROR_MARK)
37760 code = OMP_ATOMIC;
37761 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
37762 {
37763 omp_requires_mask
37764 = (enum omp_requires) (omp_requires_mask
37765 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
37766 switch ((enum omp_memory_order)
37767 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
37768 {
37769 case OMP_MEMORY_ORDER_UNSPECIFIED:
37770 case OMP_MEMORY_ORDER_RELAXED:
37771 memory_order = OMP_MEMORY_ORDER_RELAXED;
37772 break;
37773 case OMP_MEMORY_ORDER_SEQ_CST:
37774 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
37775 break;
37776 case OMP_MEMORY_ORDER_ACQ_REL:
37777 switch (code)
37778 {
37779 case OMP_ATOMIC_READ:
37780 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
37781 break;
37782 case NOP_EXPR: /* atomic write */
37783 case OMP_ATOMIC:
37784 memory_order = OMP_MEMORY_ORDER_RELEASE;
37785 break;
37786 default:
37787 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
37788 break;
37789 }
37790 break;
37791 default:
37792 gcc_unreachable ();
37793 }
37794 }
37795 else
37796 switch (code)
37797 {
37798 case OMP_ATOMIC_READ:
37799 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
37800 || memory_order == OMP_MEMORY_ORDER_RELEASE)
37801 {
37802 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
37803 "%<acq_rel%> or %<release%> clauses");
37804 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
37805 }
37806 break;
37807 case NOP_EXPR: /* atomic write */
37808 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
37809 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
37810 {
37811 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
37812 "%<acq_rel%> or %<acquire%> clauses");
37813 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
37814 }
37815 break;
37816 case OMP_ATOMIC:
37817 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
37818 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
37819 {
37820 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
37821 "%<acq_rel%> or %<acquire%> clauses");
37822 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
37823 }
37824 break;
37825 default:
37826 break;
37827 }
37828
37829 switch (code)
37830 {
37831 case OMP_ATOMIC_READ:
37832 case NOP_EXPR: /* atomic write */
37833 v = cp_parser_unary_expression (parser);
37834 if (v == error_mark_node)
37835 goto saw_error;
37836 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
37837 goto saw_error;
37838 if (code == NOP_EXPR)
37839 lhs = cp_parser_expression (parser);
37840 else
37841 lhs = cp_parser_unary_expression (parser);
37842 if (lhs == error_mark_node)
37843 goto saw_error;
37844 if (code == NOP_EXPR)
37845 {
37846 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
37847 opcode. */
37848 code = OMP_ATOMIC;
37849 rhs = lhs;
37850 lhs = v;
37851 v = NULL_TREE;
37852 }
37853 goto done;
37854 case OMP_ATOMIC_CAPTURE_NEW:
37855 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
37856 {
37857 cp_lexer_consume_token (parser->lexer);
37858 structured_block = true;
37859 }
37860 else
37861 {
37862 v = cp_parser_unary_expression (parser);
37863 if (v == error_mark_node)
37864 goto saw_error;
37865 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
37866 goto saw_error;
37867 }
37868 default:
37869 break;
37870 }
37871
37872 restart:
37873 lhs = cp_parser_unary_expression (parser);
37874 orig_lhs = lhs;
37875 switch (TREE_CODE (lhs))
37876 {
37877 case ERROR_MARK:
37878 goto saw_error;
37879
37880 case POSTINCREMENT_EXPR:
37881 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
37882 code = OMP_ATOMIC_CAPTURE_OLD;
37883 /* FALLTHROUGH */
37884 case PREINCREMENT_EXPR:
37885 lhs = TREE_OPERAND (lhs, 0);
37886 opcode = PLUS_EXPR;
37887 rhs = integer_one_node;
37888 break;
37889
37890 case POSTDECREMENT_EXPR:
37891 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
37892 code = OMP_ATOMIC_CAPTURE_OLD;
37893 /* FALLTHROUGH */
37894 case PREDECREMENT_EXPR:
37895 lhs = TREE_OPERAND (lhs, 0);
37896 opcode = MINUS_EXPR;
37897 rhs = integer_one_node;
37898 break;
37899
37900 case COMPOUND_EXPR:
37901 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
37902 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
37903 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
37904 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
37905 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
37906 (TREE_OPERAND (lhs, 1), 0), 0)))
37907 == BOOLEAN_TYPE)
37908 /* Undo effects of boolean_increment for post {in,de}crement. */
37909 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
37910 /* FALLTHRU */
37911 case MODIFY_EXPR:
37912 if (TREE_CODE (lhs) == MODIFY_EXPR
37913 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
37914 {
37915 /* Undo effects of boolean_increment. */
37916 if (integer_onep (TREE_OPERAND (lhs, 1)))
37917 {
37918 /* This is pre or post increment. */
37919 rhs = TREE_OPERAND (lhs, 1);
37920 lhs = TREE_OPERAND (lhs, 0);
37921 opcode = NOP_EXPR;
37922 if (code == OMP_ATOMIC_CAPTURE_NEW
37923 && !structured_block
37924 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
37925 code = OMP_ATOMIC_CAPTURE_OLD;
37926 break;
37927 }
37928 }
37929 /* FALLTHRU */
37930 default:
37931 switch (cp_lexer_peek_token (parser->lexer)->type)
37932 {
37933 case CPP_MULT_EQ:
37934 opcode = MULT_EXPR;
37935 break;
37936 case CPP_DIV_EQ:
37937 opcode = TRUNC_DIV_EXPR;
37938 break;
37939 case CPP_PLUS_EQ:
37940 opcode = PLUS_EXPR;
37941 break;
37942 case CPP_MINUS_EQ:
37943 opcode = MINUS_EXPR;
37944 break;
37945 case CPP_LSHIFT_EQ:
37946 opcode = LSHIFT_EXPR;
37947 break;
37948 case CPP_RSHIFT_EQ:
37949 opcode = RSHIFT_EXPR;
37950 break;
37951 case CPP_AND_EQ:
37952 opcode = BIT_AND_EXPR;
37953 break;
37954 case CPP_OR_EQ:
37955 opcode = BIT_IOR_EXPR;
37956 break;
37957 case CPP_XOR_EQ:
37958 opcode = BIT_XOR_EXPR;
37959 break;
37960 case CPP_EQ:
37961 enum cp_parser_prec oprec;
37962 cp_token *token;
37963 cp_lexer_consume_token (parser->lexer);
37964 cp_parser_parse_tentatively (parser);
37965 rhs1 = cp_parser_simple_cast_expression (parser);
37966 if (rhs1 == error_mark_node)
37967 {
37968 cp_parser_abort_tentative_parse (parser);
37969 cp_parser_simple_cast_expression (parser);
37970 goto saw_error;
37971 }
37972 token = cp_lexer_peek_token (parser->lexer);
37973 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
37974 {
37975 cp_parser_abort_tentative_parse (parser);
37976 cp_parser_parse_tentatively (parser);
37977 rhs = cp_parser_binary_expression (parser, false, true,
37978 PREC_NOT_OPERATOR, NULL);
37979 if (rhs == error_mark_node)
37980 {
37981 cp_parser_abort_tentative_parse (parser);
37982 cp_parser_binary_expression (parser, false, true,
37983 PREC_NOT_OPERATOR, NULL);
37984 goto saw_error;
37985 }
37986 switch (TREE_CODE (rhs))
37987 {
37988 case MULT_EXPR:
37989 case TRUNC_DIV_EXPR:
37990 case RDIV_EXPR:
37991 case PLUS_EXPR:
37992 case MINUS_EXPR:
37993 case LSHIFT_EXPR:
37994 case RSHIFT_EXPR:
37995 case BIT_AND_EXPR:
37996 case BIT_IOR_EXPR:
37997 case BIT_XOR_EXPR:
37998 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
37999 {
38000 if (cp_parser_parse_definitely (parser))
38001 {
38002 opcode = TREE_CODE (rhs);
38003 rhs1 = TREE_OPERAND (rhs, 0);
38004 rhs = TREE_OPERAND (rhs, 1);
38005 goto stmt_done;
38006 }
38007 else
38008 goto saw_error;
38009 }
38010 break;
38011 default:
38012 break;
38013 }
38014 cp_parser_abort_tentative_parse (parser);
38015 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
38016 {
38017 rhs = cp_parser_expression (parser);
38018 if (rhs == error_mark_node)
38019 goto saw_error;
38020 opcode = NOP_EXPR;
38021 rhs1 = NULL_TREE;
38022 goto stmt_done;
38023 }
38024 cp_parser_error (parser,
38025 "invalid form of %<#pragma omp atomic%>");
38026 goto saw_error;
38027 }
38028 if (!cp_parser_parse_definitely (parser))
38029 goto saw_error;
38030 switch (token->type)
38031 {
38032 case CPP_SEMICOLON:
38033 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
38034 {
38035 code = OMP_ATOMIC_CAPTURE_OLD;
38036 v = lhs;
38037 lhs = NULL_TREE;
38038 lhs1 = rhs1;
38039 rhs1 = NULL_TREE;
38040 cp_lexer_consume_token (parser->lexer);
38041 goto restart;
38042 }
38043 else if (structured_block)
38044 {
38045 opcode = NOP_EXPR;
38046 rhs = rhs1;
38047 rhs1 = NULL_TREE;
38048 goto stmt_done;
38049 }
38050 cp_parser_error (parser,
38051 "invalid form of %<#pragma omp atomic%>");
38052 goto saw_error;
38053 case CPP_MULT:
38054 opcode = MULT_EXPR;
38055 break;
38056 case CPP_DIV:
38057 opcode = TRUNC_DIV_EXPR;
38058 break;
38059 case CPP_PLUS:
38060 opcode = PLUS_EXPR;
38061 break;
38062 case CPP_MINUS:
38063 opcode = MINUS_EXPR;
38064 break;
38065 case CPP_LSHIFT:
38066 opcode = LSHIFT_EXPR;
38067 break;
38068 case CPP_RSHIFT:
38069 opcode = RSHIFT_EXPR;
38070 break;
38071 case CPP_AND:
38072 opcode = BIT_AND_EXPR;
38073 break;
38074 case CPP_OR:
38075 opcode = BIT_IOR_EXPR;
38076 break;
38077 case CPP_XOR:
38078 opcode = BIT_XOR_EXPR;
38079 break;
38080 default:
38081 cp_parser_error (parser,
38082 "invalid operator for %<#pragma omp atomic%>");
38083 goto saw_error;
38084 }
38085 oprec = TOKEN_PRECEDENCE (token);
38086 gcc_assert (oprec != PREC_NOT_OPERATOR);
38087 if (commutative_tree_code (opcode))
38088 oprec = (enum cp_parser_prec) (oprec - 1);
38089 cp_lexer_consume_token (parser->lexer);
38090 rhs = cp_parser_binary_expression (parser, false, false,
38091 oprec, NULL);
38092 if (rhs == error_mark_node)
38093 goto saw_error;
38094 goto stmt_done;
38095 /* FALLTHROUGH */
38096 default:
38097 cp_parser_error (parser,
38098 "invalid operator for %<#pragma omp atomic%>");
38099 goto saw_error;
38100 }
38101 cp_lexer_consume_token (parser->lexer);
38102
38103 rhs = cp_parser_expression (parser);
38104 if (rhs == error_mark_node)
38105 goto saw_error;
38106 break;
38107 }
38108 stmt_done:
38109 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
38110 {
38111 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
38112 goto saw_error;
38113 v = cp_parser_unary_expression (parser);
38114 if (v == error_mark_node)
38115 goto saw_error;
38116 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
38117 goto saw_error;
38118 lhs1 = cp_parser_unary_expression (parser);
38119 if (lhs1 == error_mark_node)
38120 goto saw_error;
38121 }
38122 if (structured_block)
38123 {
38124 cp_parser_consume_semicolon_at_end_of_statement (parser);
38125 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
38126 }
38127 done:
38128 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
38129 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
38130 rhs1, clauses, memory_order);
38131 if (!structured_block)
38132 cp_parser_consume_semicolon_at_end_of_statement (parser);
38133 return;
38134
38135 saw_error:
38136 cp_parser_skip_to_end_of_block_or_statement (parser);
38137 if (structured_block)
38138 {
38139 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
38140 cp_lexer_consume_token (parser->lexer);
38141 else if (code == OMP_ATOMIC_CAPTURE_NEW)
38142 {
38143 cp_parser_skip_to_end_of_block_or_statement (parser);
38144 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
38145 cp_lexer_consume_token (parser->lexer);
38146 }
38147 }
38148 }
38149
38150
38151 /* OpenMP 2.5:
38152 # pragma omp barrier new-line */
38153
38154 static void
38155 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
38156 {
38157 cp_parser_require_pragma_eol (parser, pragma_tok);
38158 finish_omp_barrier ();
38159 }
38160
38161 /* OpenMP 2.5:
38162 # pragma omp critical [(name)] new-line
38163 structured-block
38164
38165 OpenMP 4.5:
38166 # pragma omp critical [(name) [hint(expression)]] new-line
38167 structured-block */
38168
38169 #define OMP_CRITICAL_CLAUSE_MASK \
38170 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
38171
38172 static tree
38173 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38174 {
38175 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
38176
38177 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38178 {
38179 matching_parens parens;
38180 parens.consume_open (parser);
38181
38182 name = cp_parser_identifier (parser);
38183
38184 if (name == error_mark_node
38185 || !parens.require_close (parser))
38186 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38187 /*or_comma=*/false,
38188 /*consume_paren=*/true);
38189 if (name == error_mark_node)
38190 name = NULL;
38191
38192 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
38193 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
38194 cp_lexer_consume_token (parser->lexer);
38195
38196 clauses = cp_parser_omp_all_clauses (parser,
38197 OMP_CRITICAL_CLAUSE_MASK,
38198 "#pragma omp critical", pragma_tok);
38199 }
38200 else
38201 cp_parser_require_pragma_eol (parser, pragma_tok);
38202
38203 stmt = cp_parser_omp_structured_block (parser, if_p);
38204 return c_finish_omp_critical (input_location, stmt, name, clauses);
38205 }
38206
38207 /* OpenMP 5.0:
38208 # pragma omp depobj ( depobj ) depobj-clause new-line
38209
38210 depobj-clause:
38211 depend (dependence-type : locator)
38212 destroy
38213 update (dependence-type)
38214
38215 dependence-type:
38216 in
38217 out
38218 inout
38219 mutexinout */
38220
38221 static void
38222 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
38223 {
38224 location_t loc = pragma_tok->location;
38225 matching_parens parens;
38226 if (!parens.require_open (parser))
38227 {
38228 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38229 return;
38230 }
38231
38232 tree depobj = cp_parser_assignment_expression (parser);
38233
38234 if (!parens.require_close (parser))
38235 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38236 /*or_comma=*/false,
38237 /*consume_paren=*/true);
38238
38239 tree clause = NULL_TREE;
38240 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
38241 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
38242 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38243 {
38244 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38245 const char *p = IDENTIFIER_POINTER (id);
38246
38247 cp_lexer_consume_token (parser->lexer);
38248 if (!strcmp ("depend", p))
38249 {
38250 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
38251 if (clause)
38252 clause = finish_omp_clauses (clause, C_ORT_OMP);
38253 if (!clause)
38254 clause = error_mark_node;
38255 }
38256 else if (!strcmp ("destroy", p))
38257 kind = OMP_CLAUSE_DEPEND_LAST;
38258 else if (!strcmp ("update", p))
38259 {
38260 matching_parens c_parens;
38261 if (c_parens.require_open (parser))
38262 {
38263 location_t c2_loc
38264 = cp_lexer_peek_token (parser->lexer)->location;
38265 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38266 {
38267 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
38268 const char *p2 = IDENTIFIER_POINTER (id2);
38269
38270 cp_lexer_consume_token (parser->lexer);
38271 if (!strcmp ("in", p2))
38272 kind = OMP_CLAUSE_DEPEND_IN;
38273 else if (!strcmp ("out", p2))
38274 kind = OMP_CLAUSE_DEPEND_OUT;
38275 else if (!strcmp ("inout", p2))
38276 kind = OMP_CLAUSE_DEPEND_INOUT;
38277 else if (!strcmp ("mutexinoutset", p2))
38278 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
38279 }
38280 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
38281 {
38282 clause = error_mark_node;
38283 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
38284 "%<mutexinoutset%>");
38285 }
38286 if (!c_parens.require_close (parser))
38287 cp_parser_skip_to_closing_parenthesis (parser,
38288 /*recovering=*/true,
38289 /*or_comma=*/false,
38290 /*consume_paren=*/true);
38291 }
38292 else
38293 clause = error_mark_node;
38294 }
38295 }
38296 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
38297 {
38298 clause = error_mark_node;
38299 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
38300 }
38301 cp_parser_require_pragma_eol (parser, pragma_tok);
38302
38303 finish_omp_depobj (loc, depobj, kind, clause);
38304 }
38305
38306
38307 /* OpenMP 2.5:
38308 # pragma omp flush flush-vars[opt] new-line
38309
38310 flush-vars:
38311 ( variable-list )
38312
38313 OpenMP 5.0:
38314 # pragma omp flush memory-order-clause new-line */
38315
38316 static void
38317 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
38318 {
38319 enum memmodel mo = MEMMODEL_LAST;
38320 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38321 {
38322 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38323 const char *p = IDENTIFIER_POINTER (id);
38324 if (!strcmp (p, "acq_rel"))
38325 mo = MEMMODEL_ACQ_REL;
38326 else if (!strcmp (p, "release"))
38327 mo = MEMMODEL_RELEASE;
38328 else if (!strcmp (p, "acquire"))
38329 mo = MEMMODEL_ACQUIRE;
38330 else
38331 error_at (cp_lexer_peek_token (parser->lexer)->location,
38332 "expected %<acq_rel%>, %<release%> or %<acquire%>");
38333 cp_lexer_consume_token (parser->lexer);
38334 }
38335 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38336 {
38337 if (mo != MEMMODEL_LAST)
38338 error_at (cp_lexer_peek_token (parser->lexer)->location,
38339 "%<flush%> list specified together with memory order "
38340 "clause");
38341 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
38342 }
38343 cp_parser_require_pragma_eol (parser, pragma_tok);
38344
38345 finish_omp_flush (mo);
38346 }
38347
38348 /* Helper function, to parse omp for increment expression. */
38349
38350 static tree
38351 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
38352 {
38353 tree cond = cp_parser_binary_expression (parser, false, true,
38354 PREC_NOT_OPERATOR, NULL);
38355 if (cond == error_mark_node
38356 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
38357 {
38358 cp_parser_skip_to_end_of_statement (parser);
38359 return error_mark_node;
38360 }
38361
38362 switch (TREE_CODE (cond))
38363 {
38364 case GT_EXPR:
38365 case GE_EXPR:
38366 case LT_EXPR:
38367 case LE_EXPR:
38368 break;
38369 case NE_EXPR:
38370 if (code != OACC_LOOP)
38371 break;
38372 gcc_fallthrough ();
38373 default:
38374 return error_mark_node;
38375 }
38376
38377 /* If decl is an iterator, preserve LHS and RHS of the relational
38378 expr until finish_omp_for. */
38379 if (decl
38380 && (type_dependent_expression_p (decl)
38381 || CLASS_TYPE_P (TREE_TYPE (decl))))
38382 return cond;
38383
38384 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
38385 TREE_CODE (cond),
38386 TREE_OPERAND (cond, 0), ERROR_MARK,
38387 TREE_OPERAND (cond, 1), ERROR_MARK,
38388 /*overload=*/NULL, tf_warning_or_error);
38389 }
38390
38391 /* Helper function, to parse omp for increment expression. */
38392
38393 static tree
38394 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
38395 {
38396 cp_token *token = cp_lexer_peek_token (parser->lexer);
38397 enum tree_code op;
38398 tree lhs, rhs;
38399 cp_id_kind idk;
38400 bool decl_first;
38401
38402 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
38403 {
38404 op = (token->type == CPP_PLUS_PLUS
38405 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
38406 cp_lexer_consume_token (parser->lexer);
38407 lhs = cp_parser_simple_cast_expression (parser);
38408 if (lhs != decl
38409 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
38410 return error_mark_node;
38411 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
38412 }
38413
38414 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
38415 if (lhs != decl
38416 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
38417 return error_mark_node;
38418
38419 token = cp_lexer_peek_token (parser->lexer);
38420 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
38421 {
38422 op = (token->type == CPP_PLUS_PLUS
38423 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
38424 cp_lexer_consume_token (parser->lexer);
38425 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
38426 }
38427
38428 op = cp_parser_assignment_operator_opt (parser);
38429 if (op == ERROR_MARK)
38430 return error_mark_node;
38431
38432 if (op != NOP_EXPR)
38433 {
38434 rhs = cp_parser_assignment_expression (parser);
38435 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
38436 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
38437 }
38438
38439 lhs = cp_parser_binary_expression (parser, false, false,
38440 PREC_ADDITIVE_EXPRESSION, NULL);
38441 token = cp_lexer_peek_token (parser->lexer);
38442 decl_first = (lhs == decl
38443 || (processing_template_decl && cp_tree_equal (lhs, decl)));
38444 if (decl_first)
38445 lhs = NULL_TREE;
38446 if (token->type != CPP_PLUS
38447 && token->type != CPP_MINUS)
38448 return error_mark_node;
38449
38450 do
38451 {
38452 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
38453 cp_lexer_consume_token (parser->lexer);
38454 rhs = cp_parser_binary_expression (parser, false, false,
38455 PREC_ADDITIVE_EXPRESSION, NULL);
38456 token = cp_lexer_peek_token (parser->lexer);
38457 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
38458 {
38459 if (lhs == NULL_TREE)
38460 {
38461 if (op == PLUS_EXPR)
38462 lhs = rhs;
38463 else
38464 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
38465 tf_warning_or_error);
38466 }
38467 else
38468 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
38469 ERROR_MARK, NULL, tf_warning_or_error);
38470 }
38471 }
38472 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
38473
38474 if (!decl_first)
38475 {
38476 if ((rhs != decl
38477 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
38478 || op == MINUS_EXPR)
38479 return error_mark_node;
38480 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
38481 }
38482 else
38483 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
38484
38485 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
38486 }
38487
38488 /* Parse the initialization statement of an OpenMP for loop.
38489
38490 Return true if the resulting construct should have an
38491 OMP_CLAUSE_PRIVATE added to it. */
38492
38493 static tree
38494 cp_parser_omp_for_loop_init (cp_parser *parser,
38495 tree &this_pre_body,
38496 releasing_vec &for_block,
38497 tree &init,
38498 tree &orig_init,
38499 tree &decl,
38500 tree &real_decl)
38501 {
38502 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
38503 return NULL_TREE;
38504
38505 tree add_private_clause = NULL_TREE;
38506
38507 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
38508
38509 init-expr:
38510 var = lb
38511 integer-type var = lb
38512 random-access-iterator-type var = lb
38513 pointer-type var = lb
38514 */
38515 cp_decl_specifier_seq type_specifiers;
38516
38517 /* First, try to parse as an initialized declaration. See
38518 cp_parser_condition, from whence the bulk of this is copied. */
38519
38520 cp_parser_parse_tentatively (parser);
38521 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
38522 /*is_declaration=*/true,
38523 /*is_trailing_return=*/false,
38524 &type_specifiers);
38525 if (cp_parser_parse_definitely (parser))
38526 {
38527 /* If parsing a type specifier seq succeeded, then this
38528 MUST be a initialized declaration. */
38529 tree asm_specification, attributes;
38530 cp_declarator *declarator;
38531
38532 declarator = cp_parser_declarator (parser,
38533 CP_PARSER_DECLARATOR_NAMED,
38534 CP_PARSER_FLAGS_NONE,
38535 /*ctor_dtor_or_conv_p=*/NULL,
38536 /*parenthesized_p=*/NULL,
38537 /*member_p=*/false,
38538 /*friend_p=*/false,
38539 /*static_p=*/false);
38540 attributes = cp_parser_attributes_opt (parser);
38541 asm_specification = cp_parser_asm_specification_opt (parser);
38542
38543 if (declarator == cp_error_declarator)
38544 cp_parser_skip_to_end_of_statement (parser);
38545
38546 else
38547 {
38548 tree pushed_scope, auto_node;
38549
38550 decl = start_decl (declarator, &type_specifiers,
38551 SD_INITIALIZED, attributes,
38552 /*prefix_attributes=*/NULL_TREE,
38553 &pushed_scope);
38554
38555 auto_node = type_uses_auto (TREE_TYPE (decl));
38556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
38557 {
38558 if (cp_lexer_next_token_is (parser->lexer,
38559 CPP_OPEN_PAREN))
38560 error ("parenthesized initialization is not allowed in "
38561 "OpenMP %<for%> loop");
38562 else
38563 /* Trigger an error. */
38564 cp_parser_require (parser, CPP_EQ, RT_EQ);
38565
38566 init = error_mark_node;
38567 cp_parser_skip_to_end_of_statement (parser);
38568 }
38569 else if (CLASS_TYPE_P (TREE_TYPE (decl))
38570 || type_dependent_expression_p (decl)
38571 || auto_node)
38572 {
38573 bool is_direct_init, is_non_constant_init;
38574
38575 init = cp_parser_initializer (parser,
38576 &is_direct_init,
38577 &is_non_constant_init);
38578
38579 if (auto_node)
38580 {
38581 TREE_TYPE (decl)
38582 = do_auto_deduction (TREE_TYPE (decl), init,
38583 auto_node);
38584
38585 if (!CLASS_TYPE_P (TREE_TYPE (decl))
38586 && !type_dependent_expression_p (decl))
38587 goto non_class;
38588 }
38589
38590 cp_finish_decl (decl, init, !is_non_constant_init,
38591 asm_specification,
38592 LOOKUP_ONLYCONVERTING);
38593 orig_init = init;
38594 if (CLASS_TYPE_P (TREE_TYPE (decl)))
38595 {
38596 vec_safe_push (for_block, this_pre_body);
38597 init = NULL_TREE;
38598 }
38599 else
38600 {
38601 init = pop_stmt_list (this_pre_body);
38602 if (init && TREE_CODE (init) == STATEMENT_LIST)
38603 {
38604 tree_stmt_iterator i = tsi_start (init);
38605 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
38606 while (!tsi_end_p (i))
38607 {
38608 tree t = tsi_stmt (i);
38609 if (TREE_CODE (t) == DECL_EXPR
38610 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
38611 {
38612 tsi_delink (&i);
38613 vec_safe_push (for_block, t);
38614 continue;
38615 }
38616 break;
38617 }
38618 if (tsi_one_before_end_p (i))
38619 {
38620 tree t = tsi_stmt (i);
38621 tsi_delink (&i);
38622 free_stmt_list (init);
38623 init = t;
38624 }
38625 }
38626 }
38627 this_pre_body = NULL_TREE;
38628 }
38629 else
38630 {
38631 /* Consume '='. */
38632 cp_lexer_consume_token (parser->lexer);
38633 init = cp_parser_assignment_expression (parser);
38634
38635 non_class:
38636 if (TYPE_REF_P (TREE_TYPE (decl)))
38637 init = error_mark_node;
38638 else
38639 cp_finish_decl (decl, NULL_TREE,
38640 /*init_const_expr_p=*/false,
38641 asm_specification,
38642 LOOKUP_ONLYCONVERTING);
38643 }
38644
38645 if (pushed_scope)
38646 pop_scope (pushed_scope);
38647 }
38648 }
38649 else
38650 {
38651 cp_id_kind idk;
38652 /* If parsing a type specifier sequence failed, then
38653 this MUST be a simple expression. */
38654 cp_parser_parse_tentatively (parser);
38655 decl = cp_parser_primary_expression (parser, false, false,
38656 false, &idk);
38657 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
38658 if (!cp_parser_error_occurred (parser)
38659 && decl
38660 && (TREE_CODE (decl) == COMPONENT_REF
38661 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
38662 {
38663 cp_parser_abort_tentative_parse (parser);
38664 cp_parser_parse_tentatively (parser);
38665 cp_token *token = cp_lexer_peek_token (parser->lexer);
38666 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
38667 /*check_dependency_p=*/true,
38668 /*template_p=*/NULL,
38669 /*declarator_p=*/false,
38670 /*optional_p=*/false);
38671 if (name != error_mark_node
38672 && last_tok == cp_lexer_peek_token (parser->lexer))
38673 {
38674 decl = cp_parser_lookup_name_simple (parser, name,
38675 token->location);
38676 if (TREE_CODE (decl) == FIELD_DECL)
38677 add_private_clause = omp_privatize_field (decl, false);
38678 }
38679 cp_parser_abort_tentative_parse (parser);
38680 cp_parser_parse_tentatively (parser);
38681 decl = cp_parser_primary_expression (parser, false, false,
38682 false, &idk);
38683 }
38684 if (!cp_parser_error_occurred (parser)
38685 && decl
38686 && DECL_P (decl)
38687 && CLASS_TYPE_P (TREE_TYPE (decl)))
38688 {
38689 tree rhs;
38690
38691 cp_parser_parse_definitely (parser);
38692 cp_parser_require (parser, CPP_EQ, RT_EQ);
38693 rhs = cp_parser_assignment_expression (parser);
38694 orig_init = rhs;
38695 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
38696 decl, NOP_EXPR,
38697 rhs,
38698 tf_warning_or_error));
38699 if (!add_private_clause)
38700 add_private_clause = decl;
38701 }
38702 else
38703 {
38704 decl = NULL;
38705 cp_parser_abort_tentative_parse (parser);
38706 init = cp_parser_expression (parser);
38707 if (init)
38708 {
38709 if (TREE_CODE (init) == MODIFY_EXPR
38710 || TREE_CODE (init) == MODOP_EXPR)
38711 real_decl = TREE_OPERAND (init, 0);
38712 }
38713 }
38714 }
38715 return add_private_clause;
38716 }
38717
38718 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
38719
38720 void
38721 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
38722 tree &decl, tree &orig_decl, tree &init,
38723 tree &orig_init, tree &cond, tree &incr)
38724 {
38725 tree begin, end, range_temp_decl = NULL_TREE;
38726 tree iter_type, begin_expr, end_expr;
38727
38728 if (processing_template_decl)
38729 {
38730 if (check_for_bare_parameter_packs (init))
38731 init = error_mark_node;
38732 if (!type_dependent_expression_p (init)
38733 /* do_auto_deduction doesn't mess with template init-lists. */
38734 && !BRACE_ENCLOSED_INITIALIZER_P (init))
38735 {
38736 tree d = decl;
38737 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
38738 {
38739 tree v = DECL_VALUE_EXPR (decl);
38740 if (TREE_CODE (v) == ARRAY_REF
38741 && VAR_P (TREE_OPERAND (v, 0))
38742 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
38743 d = TREE_OPERAND (v, 0);
38744 }
38745 do_range_for_auto_deduction (d, init);
38746 }
38747 cond = global_namespace;
38748 incr = NULL_TREE;
38749 orig_init = init;
38750 if (this_pre_body)
38751 this_pre_body = pop_stmt_list (this_pre_body);
38752 return;
38753 }
38754
38755 init = mark_lvalue_use (init);
38756
38757 if (decl == error_mark_node || init == error_mark_node)
38758 /* If an error happened previously do nothing or else a lot of
38759 unhelpful errors would be issued. */
38760 begin_expr = end_expr = iter_type = error_mark_node;
38761 else
38762 {
38763 tree range_temp;
38764
38765 if (VAR_P (init)
38766 && array_of_runtime_bound_p (TREE_TYPE (init)))
38767 /* Can't bind a reference to an array of runtime bound. */
38768 range_temp = init;
38769 else
38770 {
38771 range_temp = build_range_temp (init);
38772 DECL_NAME (range_temp) = NULL_TREE;
38773 pushdecl (range_temp);
38774 cp_finish_decl (range_temp, init,
38775 /*is_constant_init*/false, NULL_TREE,
38776 LOOKUP_ONLYCONVERTING);
38777 range_temp_decl = range_temp;
38778 range_temp = convert_from_reference (range_temp);
38779 }
38780 iter_type = cp_parser_perform_range_for_lookup (range_temp,
38781 &begin_expr, &end_expr);
38782 }
38783
38784 tree end_iter_type = iter_type;
38785 if (cxx_dialect >= cxx17)
38786 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
38787 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
38788 TREE_USED (end) = 1;
38789 DECL_ARTIFICIAL (end) = 1;
38790 pushdecl (end);
38791 cp_finish_decl (end, end_expr,
38792 /*is_constant_init*/false, NULL_TREE,
38793 LOOKUP_ONLYCONVERTING);
38794
38795 /* The new for initialization statement. */
38796 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
38797 TREE_USED (begin) = 1;
38798 DECL_ARTIFICIAL (begin) = 1;
38799 pushdecl (begin);
38800 orig_init = init;
38801 if (CLASS_TYPE_P (iter_type))
38802 init = NULL_TREE;
38803 else
38804 {
38805 init = begin_expr;
38806 begin_expr = NULL_TREE;
38807 }
38808 cp_finish_decl (begin, begin_expr,
38809 /*is_constant_init*/false, NULL_TREE,
38810 LOOKUP_ONLYCONVERTING);
38811
38812 /* The new for condition. */
38813 if (CLASS_TYPE_P (iter_type))
38814 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
38815 else
38816 cond = build_x_binary_op (input_location, NE_EXPR,
38817 begin, ERROR_MARK,
38818 end, ERROR_MARK,
38819 NULL, tf_warning_or_error);
38820
38821 /* The new increment expression. */
38822 if (CLASS_TYPE_P (iter_type))
38823 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
38824 else
38825 incr = finish_unary_op_expr (input_location,
38826 PREINCREMENT_EXPR, begin,
38827 tf_warning_or_error);
38828
38829 orig_decl = decl;
38830 decl = begin;
38831 if (for_block)
38832 {
38833 vec_safe_push (for_block, this_pre_body);
38834 this_pre_body = NULL_TREE;
38835 }
38836
38837 tree decomp_first_name = NULL_TREE;
38838 unsigned decomp_cnt = 0;
38839 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
38840 {
38841 tree v = DECL_VALUE_EXPR (orig_decl);
38842 if (TREE_CODE (v) == ARRAY_REF
38843 && VAR_P (TREE_OPERAND (v, 0))
38844 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
38845 {
38846 tree d = orig_decl;
38847 orig_decl = TREE_OPERAND (v, 0);
38848 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
38849 decomp_first_name = d;
38850 }
38851 }
38852
38853 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
38854 if (auto_node)
38855 {
38856 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
38857 tf_none);
38858 if (!error_operand_p (t))
38859 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
38860 t, auto_node);
38861 }
38862
38863 tree v = make_tree_vec (decomp_cnt + 3);
38864 TREE_VEC_ELT (v, 0) = range_temp_decl;
38865 TREE_VEC_ELT (v, 1) = end;
38866 TREE_VEC_ELT (v, 2) = orig_decl;
38867 for (unsigned i = 0; i < decomp_cnt; i++)
38868 {
38869 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
38870 decomp_first_name = DECL_CHAIN (decomp_first_name);
38871 }
38872 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
38873 }
38874
38875 /* Helper for cp_parser_omp_for_loop, finalize part of range for
38876 inside of the collapsed body. */
38877
38878 void
38879 cp_finish_omp_range_for (tree orig, tree begin)
38880 {
38881 gcc_assert (TREE_CODE (orig) == TREE_LIST
38882 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
38883 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
38884 tree decomp_first_name = NULL_TREE;
38885 unsigned int decomp_cnt = 0;
38886
38887 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
38888 {
38889 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
38890 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
38891 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
38892 }
38893
38894 /* The declaration is initialized with *__begin inside the loop body. */
38895 cp_finish_decl (decl,
38896 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
38897 tf_warning_or_error),
38898 /*is_constant_init*/false, NULL_TREE,
38899 LOOKUP_ONLYCONVERTING);
38900 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
38901 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
38902 }
38903
38904 /* OpenMP 5.0:
38905
38906 scan-loop-body:
38907 { structured-block scan-directive structured-block } */
38908
38909 static void
38910 cp_parser_omp_scan_loop_body (cp_parser *parser)
38911 {
38912 tree substmt, clauses = NULL_TREE;
38913
38914 matching_braces braces;
38915 if (!braces.require_open (parser))
38916 return;
38917
38918 substmt = cp_parser_omp_structured_block (parser, NULL);
38919 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
38920 add_stmt (substmt);
38921
38922 cp_token *tok = cp_lexer_peek_token (parser->lexer);
38923 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
38924 {
38925 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
38926
38927 cp_lexer_consume_token (parser->lexer);
38928
38929 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38930 {
38931 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38932 const char *p = IDENTIFIER_POINTER (id);
38933 if (strcmp (p, "inclusive") == 0)
38934 clause = OMP_CLAUSE_INCLUSIVE;
38935 else if (strcmp (p, "exclusive") == 0)
38936 clause = OMP_CLAUSE_EXCLUSIVE;
38937 }
38938 if (clause != OMP_CLAUSE_ERROR)
38939 {
38940 cp_lexer_consume_token (parser->lexer);
38941 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
38942 }
38943 else
38944 cp_parser_error (parser, "expected %<inclusive%> or "
38945 "%<exclusive%> clause");
38946
38947 cp_parser_require_pragma_eol (parser, tok);
38948 }
38949 else
38950 error ("expected %<#pragma omp scan%>");
38951
38952 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
38953 substmt = cp_parser_omp_structured_block (parser, NULL);
38954 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
38955 clauses);
38956 add_stmt (substmt);
38957
38958 braces.require_close (parser);
38959 }
38960
38961 /* Parse the restricted form of the for statement allowed by OpenMP. */
38962
38963 static tree
38964 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
38965 tree *cclauses, bool *if_p)
38966 {
38967 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
38968 tree orig_decl;
38969 tree real_decl, initv, condv, incrv, declv, orig_declv;
38970 tree this_pre_body, cl, ordered_cl = NULL_TREE;
38971 location_t loc_first;
38972 bool collapse_err = false;
38973 int i, collapse = 1, ordered = 0, count, nbraces = 0;
38974 releasing_vec for_block;
38975 auto_vec<tree, 4> orig_inits;
38976 bool tiling = false;
38977 bool inscan = false;
38978
38979 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
38980 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
38981 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
38982 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
38983 {
38984 tiling = true;
38985 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
38986 }
38987 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
38988 && OMP_CLAUSE_ORDERED_EXPR (cl))
38989 {
38990 ordered_cl = cl;
38991 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
38992 }
38993 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
38994 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
38995 && (code == OMP_SIMD || code == OMP_FOR))
38996 inscan = true;
38997
38998 if (ordered && ordered < collapse)
38999 {
39000 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
39001 "%<ordered%> clause parameter is less than %<collapse%>");
39002 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
39003 = build_int_cst (NULL_TREE, collapse);
39004 ordered = collapse;
39005 }
39006 if (ordered)
39007 {
39008 for (tree *pc = &clauses; *pc; )
39009 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
39010 {
39011 error_at (OMP_CLAUSE_LOCATION (*pc),
39012 "%<linear%> clause may not be specified together "
39013 "with %<ordered%> clause with a parameter");
39014 *pc = OMP_CLAUSE_CHAIN (*pc);
39015 }
39016 else
39017 pc = &OMP_CLAUSE_CHAIN (*pc);
39018 }
39019
39020 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
39021 count = ordered ? ordered : collapse;
39022
39023 declv = make_tree_vec (count);
39024 initv = make_tree_vec (count);
39025 condv = make_tree_vec (count);
39026 incrv = make_tree_vec (count);
39027 orig_declv = NULL_TREE;
39028
39029 loc_first = cp_lexer_peek_token (parser->lexer)->location;
39030
39031 for (i = 0; i < count; i++)
39032 {
39033 int bracecount = 0;
39034 tree add_private_clause = NULL_TREE;
39035 location_t loc;
39036
39037 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
39038 {
39039 if (!collapse_err)
39040 cp_parser_error (parser, "for statement expected");
39041 return NULL;
39042 }
39043 loc = cp_lexer_consume_token (parser->lexer)->location;
39044
39045 /* Don't create location wrapper nodes within an OpenMP "for"
39046 statement. */
39047 auto_suppress_location_wrappers sentinel;
39048
39049 matching_parens parens;
39050 if (!parens.require_open (parser))
39051 return NULL;
39052
39053 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
39054 this_pre_body = push_stmt_list ();
39055
39056 if (code != OACC_LOOP && cxx_dialect >= cxx11)
39057 {
39058 /* Save tokens so that we can put them back. */
39059 cp_lexer_save_tokens (parser->lexer);
39060
39061 /* Look for ':' that is not nested in () or {}. */
39062 bool is_range_for
39063 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
39064 /*recovering=*/false,
39065 CPP_COLON,
39066 /*consume_paren=*/
39067 false) == -1);
39068
39069 /* Roll back the tokens we skipped. */
39070 cp_lexer_rollback_tokens (parser->lexer);
39071
39072 if (is_range_for)
39073 {
39074 bool saved_colon_corrects_to_scope_p
39075 = parser->colon_corrects_to_scope_p;
39076
39077 /* A colon is used in range-based for. */
39078 parser->colon_corrects_to_scope_p = false;
39079
39080 /* Parse the declaration. */
39081 cp_parser_simple_declaration (parser,
39082 /*function_definition_allowed_p=*/
39083 false, &decl);
39084 parser->colon_corrects_to_scope_p
39085 = saved_colon_corrects_to_scope_p;
39086
39087 cp_parser_require (parser, CPP_COLON, RT_COLON);
39088
39089 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
39090 false, 0, true);
39091
39092 cp_convert_omp_range_for (this_pre_body, for_block, decl,
39093 orig_decl, init, orig_init,
39094 cond, incr);
39095 if (this_pre_body)
39096 {
39097 if (pre_body)
39098 {
39099 tree t = pre_body;
39100 pre_body = push_stmt_list ();
39101 add_stmt (t);
39102 add_stmt (this_pre_body);
39103 pre_body = pop_stmt_list (pre_body);
39104 }
39105 else
39106 pre_body = this_pre_body;
39107 }
39108
39109 if (ordered_cl)
39110 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
39111 "%<ordered%> clause with parameter on "
39112 "range-based %<for%> loop");
39113
39114 goto parse_close_paren;
39115 }
39116 }
39117
39118 add_private_clause
39119 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
39120 init, orig_init, decl, real_decl);
39121
39122 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
39123 if (this_pre_body)
39124 {
39125 this_pre_body = pop_stmt_list (this_pre_body);
39126 if (pre_body)
39127 {
39128 tree t = pre_body;
39129 pre_body = push_stmt_list ();
39130 add_stmt (t);
39131 add_stmt (this_pre_body);
39132 pre_body = pop_stmt_list (pre_body);
39133 }
39134 else
39135 pre_body = this_pre_body;
39136 }
39137
39138 if (decl)
39139 real_decl = decl;
39140 if (cclauses != NULL
39141 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
39142 && real_decl != NULL_TREE
39143 && code != OMP_LOOP)
39144 {
39145 tree *c;
39146 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
39147 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
39148 && OMP_CLAUSE_DECL (*c) == real_decl)
39149 {
39150 error_at (loc, "iteration variable %qD"
39151 " should not be firstprivate", real_decl);
39152 *c = OMP_CLAUSE_CHAIN (*c);
39153 }
39154 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
39155 && OMP_CLAUSE_DECL (*c) == real_decl)
39156 {
39157 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
39158 tree l = *c;
39159 *c = OMP_CLAUSE_CHAIN (*c);
39160 if (code == OMP_SIMD)
39161 {
39162 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
39163 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
39164 }
39165 else
39166 {
39167 OMP_CLAUSE_CHAIN (l) = clauses;
39168 clauses = l;
39169 }
39170 add_private_clause = NULL_TREE;
39171 }
39172 else
39173 {
39174 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
39175 && OMP_CLAUSE_DECL (*c) == real_decl)
39176 add_private_clause = NULL_TREE;
39177 c = &OMP_CLAUSE_CHAIN (*c);
39178 }
39179 }
39180
39181 if (add_private_clause)
39182 {
39183 tree c;
39184 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
39185 {
39186 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
39187 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
39188 && OMP_CLAUSE_DECL (c) == decl)
39189 break;
39190 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
39191 && OMP_CLAUSE_DECL (c) == decl)
39192 error_at (loc, "iteration variable %qD "
39193 "should not be firstprivate",
39194 decl);
39195 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
39196 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
39197 && OMP_CLAUSE_DECL (c) == decl)
39198 error_at (loc, "iteration variable %qD should not be reduction",
39199 decl);
39200 }
39201 if (c == NULL)
39202 {
39203 if ((code == OMP_SIMD && collapse != 1) || code == OMP_LOOP)
39204 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
39205 else if (code != OMP_SIMD)
39206 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
39207 else
39208 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
39209 OMP_CLAUSE_DECL (c) = add_private_clause;
39210 c = finish_omp_clauses (c, C_ORT_OMP);
39211 if (c)
39212 {
39213 OMP_CLAUSE_CHAIN (c) = clauses;
39214 clauses = c;
39215 /* For linear, signal that we need to fill up
39216 the so far unknown linear step. */
39217 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
39218 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
39219 }
39220 }
39221 }
39222
39223 cond = NULL;
39224 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
39225 cond = cp_parser_omp_for_cond (parser, decl, code);
39226 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
39227
39228 incr = NULL;
39229 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
39230 {
39231 /* If decl is an iterator, preserve the operator on decl
39232 until finish_omp_for. */
39233 if (real_decl
39234 && ((processing_template_decl
39235 && (TREE_TYPE (real_decl) == NULL_TREE
39236 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
39237 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
39238 incr = cp_parser_omp_for_incr (parser, real_decl);
39239 else
39240 incr = cp_parser_expression (parser);
39241 protected_set_expr_location_if_unset (incr, input_location);
39242 }
39243
39244 parse_close_paren:
39245 if (!parens.require_close (parser))
39246 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39247 /*or_comma=*/false,
39248 /*consume_paren=*/true);
39249
39250 TREE_VEC_ELT (declv, i) = decl;
39251 TREE_VEC_ELT (initv, i) = init;
39252 TREE_VEC_ELT (condv, i) = cond;
39253 TREE_VEC_ELT (incrv, i) = incr;
39254 if (orig_init)
39255 {
39256 orig_inits.safe_grow_cleared (i + 1);
39257 orig_inits[i] = orig_init;
39258 }
39259 if (orig_decl)
39260 {
39261 if (!orig_declv)
39262 orig_declv = copy_node (declv);
39263 TREE_VEC_ELT (orig_declv, i) = orig_decl;
39264 }
39265 else if (orig_declv)
39266 TREE_VEC_ELT (orig_declv, i) = decl;
39267
39268 if (i == count - 1)
39269 break;
39270
39271 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
39272 in between the collapsed for loops to be still considered perfectly
39273 nested. Hopefully the final version clarifies this.
39274 For now handle (multiple) {'s and empty statements. */
39275 cp_parser_parse_tentatively (parser);
39276 for (;;)
39277 {
39278 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
39279 break;
39280 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
39281 {
39282 cp_lexer_consume_token (parser->lexer);
39283 bracecount++;
39284 }
39285 else if (bracecount
39286 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
39287 cp_lexer_consume_token (parser->lexer);
39288 else
39289 {
39290 loc = cp_lexer_peek_token (parser->lexer)->location;
39291 error_at (loc, "not enough for loops to collapse");
39292 collapse_err = true;
39293 cp_parser_abort_tentative_parse (parser);
39294 declv = NULL_TREE;
39295 break;
39296 }
39297 }
39298
39299 if (declv)
39300 {
39301 cp_parser_parse_definitely (parser);
39302 nbraces += bracecount;
39303 }
39304 }
39305
39306 if (nbraces)
39307 if_p = NULL;
39308
39309 /* Note that we saved the original contents of this flag when we entered
39310 the structured block, and so we don't need to re-save it here. */
39311 parser->in_statement = IN_OMP_FOR;
39312
39313 /* Note that the grammar doesn't call for a structured block here,
39314 though the loop as a whole is a structured block. */
39315 if (orig_declv)
39316 {
39317 body = begin_omp_structured_block ();
39318 for (i = 0; i < count; i++)
39319 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
39320 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
39321 TREE_VEC_ELT (declv, i));
39322 }
39323 else
39324 body = push_stmt_list ();
39325 if (inscan)
39326 cp_parser_omp_scan_loop_body (parser);
39327 else
39328 cp_parser_statement (parser, NULL_TREE, false, if_p);
39329 if (orig_declv)
39330 body = finish_omp_structured_block (body);
39331 else
39332 body = pop_stmt_list (body);
39333
39334 if (declv == NULL_TREE)
39335 ret = NULL_TREE;
39336 else
39337 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
39338 incrv, body, pre_body, &orig_inits, clauses);
39339
39340 while (nbraces)
39341 {
39342 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
39343 {
39344 cp_lexer_consume_token (parser->lexer);
39345 nbraces--;
39346 }
39347 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
39348 cp_lexer_consume_token (parser->lexer);
39349 else
39350 {
39351 if (!collapse_err)
39352 {
39353 error_at (cp_lexer_peek_token (parser->lexer)->location,
39354 "collapsed loops not perfectly nested");
39355 }
39356 collapse_err = true;
39357 cp_parser_statement_seq_opt (parser, NULL);
39358 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
39359 break;
39360 }
39361 }
39362
39363 while (!for_block->is_empty ())
39364 {
39365 tree t = for_block->pop ();
39366 if (TREE_CODE (t) == STATEMENT_LIST)
39367 add_stmt (pop_stmt_list (t));
39368 else
39369 add_stmt (t);
39370 }
39371
39372 return ret;
39373 }
39374
39375 /* Helper function for OpenMP parsing, split clauses and call
39376 finish_omp_clauses on each of the set of clauses afterwards. */
39377
39378 static void
39379 cp_omp_split_clauses (location_t loc, enum tree_code code,
39380 omp_clause_mask mask, tree clauses, tree *cclauses)
39381 {
39382 int i;
39383 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
39384 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
39385 if (cclauses[i])
39386 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
39387 }
39388
39389 /* OpenMP 5.0:
39390 #pragma omp loop loop-clause[optseq] new-line
39391 for-loop */
39392
39393 #define OMP_LOOP_CLAUSE_MASK \
39394 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
39399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
39400
39401 static tree
39402 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
39403 char *p_name, omp_clause_mask mask, tree *cclauses,
39404 bool *if_p)
39405 {
39406 tree clauses, sb, ret;
39407 unsigned int save;
39408 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39409
39410 strcat (p_name, " loop");
39411 mask |= OMP_LOOP_CLAUSE_MASK;
39412
39413 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39414 cclauses == NULL);
39415 if (cclauses)
39416 {
39417 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
39418 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
39419 }
39420
39421 keep_next_level (true);
39422 sb = begin_omp_structured_block ();
39423 save = cp_parser_begin_omp_structured_block (parser);
39424
39425 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
39426
39427 cp_parser_end_omp_structured_block (parser, save);
39428 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39429
39430 return ret;
39431 }
39432
39433 /* OpenMP 4.0:
39434 #pragma omp simd simd-clause[optseq] new-line
39435 for-loop */
39436
39437 #define OMP_SIMD_CLAUSE_MASK \
39438 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
39439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
39440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
39442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
39448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
39449
39450 static tree
39451 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
39452 char *p_name, omp_clause_mask mask, tree *cclauses,
39453 bool *if_p)
39454 {
39455 tree clauses, sb, ret;
39456 unsigned int save;
39457 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39458
39459 strcat (p_name, " simd");
39460 mask |= OMP_SIMD_CLAUSE_MASK;
39461
39462 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39463 cclauses == NULL);
39464 if (cclauses)
39465 {
39466 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
39467 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
39468 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
39469 OMP_CLAUSE_ORDERED);
39470 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
39471 {
39472 error_at (OMP_CLAUSE_LOCATION (c),
39473 "%<ordered%> clause with parameter may not be specified "
39474 "on %qs construct", p_name);
39475 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
39476 }
39477 }
39478
39479 keep_next_level (true);
39480 sb = begin_omp_structured_block ();
39481 save = cp_parser_begin_omp_structured_block (parser);
39482
39483 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
39484
39485 cp_parser_end_omp_structured_block (parser, save);
39486 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39487
39488 return ret;
39489 }
39490
39491 /* OpenMP 2.5:
39492 #pragma omp for for-clause[optseq] new-line
39493 for-loop
39494
39495 OpenMP 4.0:
39496 #pragma omp for simd for-simd-clause[optseq] new-line
39497 for-loop */
39498
39499 #define OMP_FOR_CLAUSE_MASK \
39500 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
39506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
39507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
39508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
39510
39511 static tree
39512 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
39513 char *p_name, omp_clause_mask mask, tree *cclauses,
39514 bool *if_p)
39515 {
39516 tree clauses, sb, ret;
39517 unsigned int save;
39518 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39519
39520 strcat (p_name, " for");
39521 mask |= OMP_FOR_CLAUSE_MASK;
39522 /* parallel for{, simd} disallows nowait clause, but for
39523 target {teams distribute ,}parallel for{, simd} it should be accepted. */
39524 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
39525 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
39526 /* Composite distribute parallel for{, simd} disallows ordered clause. */
39527 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
39528 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
39529
39530 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39531 {
39532 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39533 const char *p = IDENTIFIER_POINTER (id);
39534
39535 if (strcmp (p, "simd") == 0)
39536 {
39537 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39538 if (cclauses == NULL)
39539 cclauses = cclauses_buf;
39540
39541 cp_lexer_consume_token (parser->lexer);
39542 if (!flag_openmp) /* flag_openmp_simd */
39543 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39544 cclauses, if_p);
39545 sb = begin_omp_structured_block ();
39546 save = cp_parser_begin_omp_structured_block (parser);
39547 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39548 cclauses, if_p);
39549 cp_parser_end_omp_structured_block (parser, save);
39550 tree body = finish_omp_structured_block (sb);
39551 if (ret == NULL)
39552 return ret;
39553 ret = make_node (OMP_FOR);
39554 TREE_TYPE (ret) = void_type_node;
39555 OMP_FOR_BODY (ret) = body;
39556 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
39557 SET_EXPR_LOCATION (ret, loc);
39558 add_stmt (ret);
39559 return ret;
39560 }
39561 }
39562 if (!flag_openmp) /* flag_openmp_simd */
39563 {
39564 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39565 return NULL_TREE;
39566 }
39567
39568 /* Composite distribute parallel for disallows linear clause. */
39569 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
39570 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
39571
39572 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39573 cclauses == NULL);
39574 if (cclauses)
39575 {
39576 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
39577 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
39578 }
39579
39580 keep_next_level (true);
39581 sb = begin_omp_structured_block ();
39582 save = cp_parser_begin_omp_structured_block (parser);
39583
39584 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
39585
39586 cp_parser_end_omp_structured_block (parser, save);
39587 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39588
39589 return ret;
39590 }
39591
39592 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
39593 omp_clause_mask, tree *, bool *);
39594
39595 /* OpenMP 2.5:
39596 # pragma omp master new-line
39597 structured-block */
39598
39599 static tree
39600 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
39601 char *p_name, omp_clause_mask mask, tree *cclauses,
39602 bool *if_p)
39603 {
39604 tree clauses, sb, ret;
39605 unsigned int save;
39606 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39607
39608 strcat (p_name, " master");
39609
39610 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39611 {
39612 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39613 const char *p = IDENTIFIER_POINTER (id);
39614
39615 if (strcmp (p, "taskloop") == 0)
39616 {
39617 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39618 if (cclauses == NULL)
39619 cclauses = cclauses_buf;
39620
39621 cp_lexer_consume_token (parser->lexer);
39622 if (!flag_openmp) /* flag_openmp_simd */
39623 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
39624 cclauses, if_p);
39625 sb = begin_omp_structured_block ();
39626 save = cp_parser_begin_omp_structured_block (parser);
39627 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
39628 cclauses, if_p);
39629 cp_parser_end_omp_structured_block (parser, save);
39630 tree body = finish_omp_structured_block (sb);
39631 if (ret == NULL)
39632 return ret;
39633 return c_finish_omp_master (loc, body);
39634 }
39635 }
39636 if (!flag_openmp) /* flag_openmp_simd */
39637 {
39638 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39639 return NULL_TREE;
39640 }
39641
39642 if (cclauses)
39643 {
39644 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39645 false);
39646 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
39647 }
39648 else
39649 cp_parser_require_pragma_eol (parser, pragma_tok);
39650
39651 return c_finish_omp_master (loc,
39652 cp_parser_omp_structured_block (parser, if_p));
39653 }
39654
39655 /* OpenMP 2.5:
39656 # pragma omp ordered new-line
39657 structured-block
39658
39659 OpenMP 4.5:
39660 # pragma omp ordered ordered-clauses new-line
39661 structured-block */
39662
39663 #define OMP_ORDERED_CLAUSE_MASK \
39664 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
39665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
39666
39667 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
39668 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
39669
39670 static bool
39671 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
39672 enum pragma_context context, bool *if_p)
39673 {
39674 location_t loc = pragma_tok->location;
39675
39676 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39677 {
39678 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39679 const char *p = IDENTIFIER_POINTER (id);
39680
39681 if (strcmp (p, "depend") == 0)
39682 {
39683 if (!flag_openmp) /* flag_openmp_simd */
39684 {
39685 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39686 return false;
39687 }
39688 if (context == pragma_stmt)
39689 {
39690 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
39691 "%<depend%> clause may only be used in compound "
39692 "statements");
39693 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39694 return false;
39695 }
39696 tree clauses
39697 = cp_parser_omp_all_clauses (parser,
39698 OMP_ORDERED_DEPEND_CLAUSE_MASK,
39699 "#pragma omp ordered", pragma_tok);
39700 c_finish_omp_ordered (loc, clauses, NULL_TREE);
39701 return false;
39702 }
39703 }
39704
39705 tree clauses
39706 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
39707 "#pragma omp ordered", pragma_tok);
39708
39709 if (!flag_openmp /* flag_openmp_simd */
39710 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
39711 return false;
39712
39713 c_finish_omp_ordered (loc, clauses,
39714 cp_parser_omp_structured_block (parser, if_p));
39715 return true;
39716 }
39717
39718 /* OpenMP 2.5:
39719
39720 section-scope:
39721 { section-sequence }
39722
39723 section-sequence:
39724 section-directive[opt] structured-block
39725 section-sequence section-directive structured-block */
39726
39727 static tree
39728 cp_parser_omp_sections_scope (cp_parser *parser)
39729 {
39730 tree stmt, substmt;
39731 bool error_suppress = false;
39732 cp_token *tok;
39733
39734 matching_braces braces;
39735 if (!braces.require_open (parser))
39736 return NULL_TREE;
39737
39738 stmt = push_stmt_list ();
39739
39740 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
39741 != PRAGMA_OMP_SECTION)
39742 {
39743 substmt = cp_parser_omp_structured_block (parser, NULL);
39744 substmt = build1 (OMP_SECTION, void_type_node, substmt);
39745 add_stmt (substmt);
39746 }
39747
39748 while (1)
39749 {
39750 tok = cp_lexer_peek_token (parser->lexer);
39751 if (tok->type == CPP_CLOSE_BRACE)
39752 break;
39753 if (tok->type == CPP_EOF)
39754 break;
39755
39756 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
39757 {
39758 cp_lexer_consume_token (parser->lexer);
39759 cp_parser_require_pragma_eol (parser, tok);
39760 error_suppress = false;
39761 }
39762 else if (!error_suppress)
39763 {
39764 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
39765 error_suppress = true;
39766 }
39767
39768 substmt = cp_parser_omp_structured_block (parser, NULL);
39769 substmt = build1 (OMP_SECTION, void_type_node, substmt);
39770 add_stmt (substmt);
39771 }
39772 braces.require_close (parser);
39773
39774 substmt = pop_stmt_list (stmt);
39775
39776 stmt = make_node (OMP_SECTIONS);
39777 TREE_TYPE (stmt) = void_type_node;
39778 OMP_SECTIONS_BODY (stmt) = substmt;
39779
39780 add_stmt (stmt);
39781 return stmt;
39782 }
39783
39784 /* OpenMP 2.5:
39785 # pragma omp sections sections-clause[optseq] newline
39786 sections-scope */
39787
39788 #define OMP_SECTIONS_CLAUSE_MASK \
39789 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
39794
39795 static tree
39796 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
39797 char *p_name, omp_clause_mask mask, tree *cclauses)
39798 {
39799 tree clauses, ret;
39800 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39801
39802 strcat (p_name, " sections");
39803 mask |= OMP_SECTIONS_CLAUSE_MASK;
39804 if (cclauses)
39805 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
39806
39807 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39808 cclauses == NULL);
39809 if (cclauses)
39810 {
39811 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
39812 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
39813 }
39814
39815 ret = cp_parser_omp_sections_scope (parser);
39816 if (ret)
39817 OMP_SECTIONS_CLAUSES (ret) = clauses;
39818
39819 return ret;
39820 }
39821
39822 /* OpenMP 2.5:
39823 # pragma omp parallel parallel-clause[optseq] new-line
39824 structured-block
39825 # pragma omp parallel for parallel-for-clause[optseq] new-line
39826 structured-block
39827 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
39828 structured-block
39829
39830 OpenMP 4.0:
39831 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
39832 structured-block */
39833
39834 #define OMP_PARALLEL_CLAUSE_MASK \
39835 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
39841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
39843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
39844
39845 static tree
39846 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
39847 char *p_name, omp_clause_mask mask, tree *cclauses,
39848 bool *if_p)
39849 {
39850 tree stmt, clauses, block;
39851 unsigned int save;
39852 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39853
39854 strcat (p_name, " parallel");
39855 mask |= OMP_PARALLEL_CLAUSE_MASK;
39856 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
39857 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
39858 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
39859 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
39860
39861 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
39862 {
39863 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39864 if (cclauses == NULL)
39865 cclauses = cclauses_buf;
39866
39867 cp_lexer_consume_token (parser->lexer);
39868 if (!flag_openmp) /* flag_openmp_simd */
39869 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
39870 if_p);
39871 block = begin_omp_parallel ();
39872 save = cp_parser_begin_omp_structured_block (parser);
39873 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
39874 if_p);
39875 cp_parser_end_omp_structured_block (parser, save);
39876 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
39877 block);
39878 if (ret == NULL_TREE)
39879 return ret;
39880 OMP_PARALLEL_COMBINED (stmt) = 1;
39881 return stmt;
39882 }
39883 /* When combined with distribute, parallel has to be followed by for.
39884 #pragma omp target parallel is allowed though. */
39885 else if (cclauses
39886 && (mask & (OMP_CLAUSE_MASK_1
39887 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
39888 {
39889 error_at (loc, "expected %<for%> after %qs", p_name);
39890 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39891 return NULL_TREE;
39892 }
39893 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39894 {
39895 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39896 const char *p = IDENTIFIER_POINTER (id);
39897 if (cclauses == NULL && strcmp (p, "master") == 0)
39898 {
39899 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39900 cclauses = cclauses_buf;
39901
39902 cp_lexer_consume_token (parser->lexer);
39903 block = begin_omp_parallel ();
39904 save = cp_parser_begin_omp_structured_block (parser);
39905 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
39906 cclauses, if_p);
39907 cp_parser_end_omp_structured_block (parser, save);
39908 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
39909 block);
39910 if (ret == NULL_TREE)
39911 return ret;
39912 OMP_PARALLEL_COMBINED (stmt) = 1;
39913 return stmt;
39914 }
39915 else if (strcmp (p, "loop") == 0)
39916 {
39917 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39918 if (cclauses == NULL)
39919 cclauses = cclauses_buf;
39920
39921 cp_lexer_consume_token (parser->lexer);
39922 if (!flag_openmp) /* flag_openmp_simd */
39923 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
39924 cclauses, if_p);
39925 block = begin_omp_parallel ();
39926 save = cp_parser_begin_omp_structured_block (parser);
39927 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
39928 cclauses, if_p);
39929 cp_parser_end_omp_structured_block (parser, save);
39930 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
39931 block);
39932 if (ret == NULL_TREE)
39933 return ret;
39934 OMP_PARALLEL_COMBINED (stmt) = 1;
39935 return stmt;
39936 }
39937 else if (!flag_openmp) /* flag_openmp_simd */
39938 {
39939 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39940 return NULL_TREE;
39941 }
39942 else if (cclauses == NULL && strcmp (p, "sections") == 0)
39943 {
39944 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39945 cclauses = cclauses_buf;
39946
39947 cp_lexer_consume_token (parser->lexer);
39948 block = begin_omp_parallel ();
39949 save = cp_parser_begin_omp_structured_block (parser);
39950 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
39951 cp_parser_end_omp_structured_block (parser, save);
39952 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
39953 block);
39954 OMP_PARALLEL_COMBINED (stmt) = 1;
39955 return stmt;
39956 }
39957 }
39958 else if (!flag_openmp) /* flag_openmp_simd */
39959 {
39960 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39961 return NULL_TREE;
39962 }
39963
39964 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39965 cclauses == NULL);
39966 if (cclauses)
39967 {
39968 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
39969 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
39970 }
39971
39972 block = begin_omp_parallel ();
39973 save = cp_parser_begin_omp_structured_block (parser);
39974 cp_parser_statement (parser, NULL_TREE, false, if_p);
39975 cp_parser_end_omp_structured_block (parser, save);
39976 stmt = finish_omp_parallel (clauses, block);
39977 return stmt;
39978 }
39979
39980 /* OpenMP 2.5:
39981 # pragma omp single single-clause[optseq] new-line
39982 structured-block */
39983
39984 #define OMP_SINGLE_CLAUSE_MASK \
39985 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
39988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
39989
39990 static tree
39991 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39992 {
39993 tree stmt = make_node (OMP_SINGLE);
39994 TREE_TYPE (stmt) = void_type_node;
39995 SET_EXPR_LOCATION (stmt, pragma_tok->location);
39996
39997 OMP_SINGLE_CLAUSES (stmt)
39998 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
39999 "#pragma omp single", pragma_tok);
40000 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
40001
40002 return add_stmt (stmt);
40003 }
40004
40005 /* OpenMP 3.0:
40006 # pragma omp task task-clause[optseq] new-line
40007 structured-block */
40008
40009 #define OMP_TASK_CLAUSE_MASK \
40010 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
40012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
40013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
40016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
40017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
40018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
40020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
40021
40022 static tree
40023 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40024 {
40025 tree clauses, block;
40026 unsigned int save;
40027
40028 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
40029 "#pragma omp task", pragma_tok);
40030 block = begin_omp_task ();
40031 save = cp_parser_begin_omp_structured_block (parser);
40032 cp_parser_statement (parser, NULL_TREE, false, if_p);
40033 cp_parser_end_omp_structured_block (parser, save);
40034 return finish_omp_task (clauses, block);
40035 }
40036
40037 /* OpenMP 3.0:
40038 # pragma omp taskwait new-line
40039
40040 OpenMP 5.0:
40041 # pragma omp taskwait taskwait-clause[opt] new-line */
40042
40043 #define OMP_TASKWAIT_CLAUSE_MASK \
40044 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
40045
40046 static void
40047 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
40048 {
40049 tree clauses
40050 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
40051 "#pragma omp taskwait", pragma_tok);
40052
40053 if (clauses)
40054 {
40055 tree stmt = make_node (OMP_TASK);
40056 TREE_TYPE (stmt) = void_node;
40057 OMP_TASK_CLAUSES (stmt) = clauses;
40058 OMP_TASK_BODY (stmt) = NULL_TREE;
40059 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40060 add_stmt (stmt);
40061 }
40062 else
40063 finish_omp_taskwait ();
40064 }
40065
40066 /* OpenMP 3.1:
40067 # pragma omp taskyield new-line */
40068
40069 static void
40070 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
40071 {
40072 cp_parser_require_pragma_eol (parser, pragma_tok);
40073 finish_omp_taskyield ();
40074 }
40075
40076 /* OpenMP 4.0:
40077 # pragma omp taskgroup new-line
40078 structured-block
40079
40080 OpenMP 5.0:
40081 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
40082
40083 #define OMP_TASKGROUP_CLAUSE_MASK \
40084 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
40085
40086 static tree
40087 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40088 {
40089 tree clauses
40090 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
40091 "#pragma omp taskgroup", pragma_tok);
40092 return c_finish_omp_taskgroup (input_location,
40093 cp_parser_omp_structured_block (parser,
40094 if_p),
40095 clauses);
40096 }
40097
40098
40099 /* OpenMP 2.5:
40100 # pragma omp threadprivate (variable-list) */
40101
40102 static void
40103 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
40104 {
40105 tree vars;
40106
40107 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
40108 cp_parser_require_pragma_eol (parser, pragma_tok);
40109
40110 finish_omp_threadprivate (vars);
40111 }
40112
40113 /* OpenMP 4.0:
40114 # pragma omp cancel cancel-clause[optseq] new-line */
40115
40116 #define OMP_CANCEL_CLAUSE_MASK \
40117 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
40118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
40119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
40120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
40121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
40122
40123 static void
40124 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
40125 {
40126 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
40127 "#pragma omp cancel", pragma_tok);
40128 finish_omp_cancel (clauses);
40129 }
40130
40131 /* OpenMP 4.0:
40132 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
40133
40134 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
40135 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
40136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
40137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
40138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
40139
40140 static void
40141 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
40142 enum pragma_context context)
40143 {
40144 tree clauses;
40145 bool point_seen = false;
40146
40147 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40148 {
40149 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40150 const char *p = IDENTIFIER_POINTER (id);
40151
40152 if (strcmp (p, "point") == 0)
40153 {
40154 cp_lexer_consume_token (parser->lexer);
40155 point_seen = true;
40156 }
40157 }
40158 if (!point_seen)
40159 {
40160 cp_parser_error (parser, "expected %<point%>");
40161 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40162 return;
40163 }
40164
40165 if (context != pragma_compound)
40166 {
40167 if (context == pragma_stmt)
40168 error_at (pragma_tok->location,
40169 "%<#pragma %s%> may only be used in compound statements",
40170 "omp cancellation point");
40171 else
40172 cp_parser_error (parser, "expected declaration specifiers");
40173 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40174 return;
40175 }
40176
40177 clauses = cp_parser_omp_all_clauses (parser,
40178 OMP_CANCELLATION_POINT_CLAUSE_MASK,
40179 "#pragma omp cancellation point",
40180 pragma_tok);
40181 finish_omp_cancellation_point (clauses);
40182 }
40183
40184 /* OpenMP 4.0:
40185 #pragma omp distribute distribute-clause[optseq] new-line
40186 for-loop */
40187
40188 #define OMP_DISTRIBUTE_CLAUSE_MASK \
40189 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
40193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
40194
40195 static tree
40196 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
40197 char *p_name, omp_clause_mask mask, tree *cclauses,
40198 bool *if_p)
40199 {
40200 tree clauses, sb, ret;
40201 unsigned int save;
40202 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40203
40204 strcat (p_name, " distribute");
40205 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
40206
40207 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40208 {
40209 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40210 const char *p = IDENTIFIER_POINTER (id);
40211 bool simd = false;
40212 bool parallel = false;
40213
40214 if (strcmp (p, "simd") == 0)
40215 simd = true;
40216 else
40217 parallel = strcmp (p, "parallel") == 0;
40218 if (parallel || simd)
40219 {
40220 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40221 if (cclauses == NULL)
40222 cclauses = cclauses_buf;
40223 cp_lexer_consume_token (parser->lexer);
40224 if (!flag_openmp) /* flag_openmp_simd */
40225 {
40226 if (simd)
40227 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40228 cclauses, if_p);
40229 else
40230 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
40231 cclauses, if_p);
40232 }
40233 sb = begin_omp_structured_block ();
40234 save = cp_parser_begin_omp_structured_block (parser);
40235 if (simd)
40236 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40237 cclauses, if_p);
40238 else
40239 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
40240 cclauses, if_p);
40241 cp_parser_end_omp_structured_block (parser, save);
40242 tree body = finish_omp_structured_block (sb);
40243 if (ret == NULL)
40244 return ret;
40245 ret = make_node (OMP_DISTRIBUTE);
40246 TREE_TYPE (ret) = void_type_node;
40247 OMP_FOR_BODY (ret) = body;
40248 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
40249 SET_EXPR_LOCATION (ret, loc);
40250 add_stmt (ret);
40251 return ret;
40252 }
40253 }
40254 if (!flag_openmp) /* flag_openmp_simd */
40255 {
40256 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40257 return NULL_TREE;
40258 }
40259
40260 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40261 cclauses == NULL);
40262 if (cclauses)
40263 {
40264 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
40265 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
40266 }
40267
40268 keep_next_level (true);
40269 sb = begin_omp_structured_block ();
40270 save = cp_parser_begin_omp_structured_block (parser);
40271
40272 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
40273
40274 cp_parser_end_omp_structured_block (parser, save);
40275 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
40276
40277 return ret;
40278 }
40279
40280 /* OpenMP 4.0:
40281 # pragma omp teams teams-clause[optseq] new-line
40282 structured-block */
40283
40284 #define OMP_TEAMS_CLAUSE_MASK \
40285 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
40288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
40290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
40291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
40292
40293 static tree
40294 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
40295 char *p_name, omp_clause_mask mask, tree *cclauses,
40296 bool *if_p)
40297 {
40298 tree clauses, sb, ret;
40299 unsigned int save;
40300 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40301
40302 strcat (p_name, " teams");
40303 mask |= OMP_TEAMS_CLAUSE_MASK;
40304
40305 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40306 {
40307 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40308 const char *p = IDENTIFIER_POINTER (id);
40309 if (strcmp (p, "distribute") == 0)
40310 {
40311 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40312 if (cclauses == NULL)
40313 cclauses = cclauses_buf;
40314
40315 cp_lexer_consume_token (parser->lexer);
40316 if (!flag_openmp) /* flag_openmp_simd */
40317 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
40318 cclauses, if_p);
40319 keep_next_level (true);
40320 sb = begin_omp_structured_block ();
40321 save = cp_parser_begin_omp_structured_block (parser);
40322 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
40323 cclauses, if_p);
40324 cp_parser_end_omp_structured_block (parser, save);
40325 tree body = finish_omp_structured_block (sb);
40326 if (ret == NULL)
40327 return ret;
40328 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
40329 ret = make_node (OMP_TEAMS);
40330 TREE_TYPE (ret) = void_type_node;
40331 OMP_TEAMS_CLAUSES (ret) = clauses;
40332 OMP_TEAMS_BODY (ret) = body;
40333 OMP_TEAMS_COMBINED (ret) = 1;
40334 SET_EXPR_LOCATION (ret, loc);
40335 return add_stmt (ret);
40336 }
40337 else if (strcmp (p, "loop") == 0)
40338 {
40339 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40340 if (cclauses == NULL)
40341 cclauses = cclauses_buf;
40342
40343 cp_lexer_consume_token (parser->lexer);
40344 if (!flag_openmp) /* flag_openmp_simd */
40345 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
40346 cclauses, if_p);
40347 keep_next_level (true);
40348 sb = begin_omp_structured_block ();
40349 save = cp_parser_begin_omp_structured_block (parser);
40350 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
40351 cclauses, if_p);
40352 cp_parser_end_omp_structured_block (parser, save);
40353 tree body = finish_omp_structured_block (sb);
40354 if (ret == NULL)
40355 return ret;
40356 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
40357 ret = make_node (OMP_TEAMS);
40358 TREE_TYPE (ret) = void_type_node;
40359 OMP_TEAMS_CLAUSES (ret) = clauses;
40360 OMP_TEAMS_BODY (ret) = body;
40361 OMP_TEAMS_COMBINED (ret) = 1;
40362 SET_EXPR_LOCATION (ret, loc);
40363 return add_stmt (ret);
40364 }
40365 }
40366 if (!flag_openmp) /* flag_openmp_simd */
40367 {
40368 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40369 return NULL_TREE;
40370 }
40371
40372 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40373 cclauses == NULL);
40374 if (cclauses)
40375 {
40376 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
40377 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
40378 }
40379
40380 tree stmt = make_node (OMP_TEAMS);
40381 TREE_TYPE (stmt) = void_type_node;
40382 OMP_TEAMS_CLAUSES (stmt) = clauses;
40383 keep_next_level (true);
40384 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
40385 SET_EXPR_LOCATION (stmt, loc);
40386
40387 return add_stmt (stmt);
40388 }
40389
40390 /* OpenMP 4.0:
40391 # pragma omp target data target-data-clause[optseq] new-line
40392 structured-block */
40393
40394 #define OMP_TARGET_DATA_CLAUSE_MASK \
40395 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
40397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
40399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
40400
40401 static tree
40402 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40403 {
40404 tree clauses
40405 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
40406 "#pragma omp target data", pragma_tok);
40407 int map_seen = 0;
40408 for (tree *pc = &clauses; *pc;)
40409 {
40410 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
40411 switch (OMP_CLAUSE_MAP_KIND (*pc))
40412 {
40413 case GOMP_MAP_TO:
40414 case GOMP_MAP_ALWAYS_TO:
40415 case GOMP_MAP_FROM:
40416 case GOMP_MAP_ALWAYS_FROM:
40417 case GOMP_MAP_TOFROM:
40418 case GOMP_MAP_ALWAYS_TOFROM:
40419 case GOMP_MAP_ALLOC:
40420 map_seen = 3;
40421 break;
40422 case GOMP_MAP_FIRSTPRIVATE_POINTER:
40423 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
40424 case GOMP_MAP_ALWAYS_POINTER:
40425 break;
40426 default:
40427 map_seen |= 1;
40428 error_at (OMP_CLAUSE_LOCATION (*pc),
40429 "%<#pragma omp target data%> with map-type other "
40430 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
40431 "on %<map%> clause");
40432 *pc = OMP_CLAUSE_CHAIN (*pc);
40433 continue;
40434 }
40435 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
40436 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
40437 map_seen = 3;
40438 pc = &OMP_CLAUSE_CHAIN (*pc);
40439 }
40440
40441 if (map_seen != 3)
40442 {
40443 if (map_seen == 0)
40444 error_at (pragma_tok->location,
40445 "%<#pragma omp target data%> must contain at least "
40446 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
40447 "clause");
40448 return NULL_TREE;
40449 }
40450
40451 tree stmt = make_node (OMP_TARGET_DATA);
40452 TREE_TYPE (stmt) = void_type_node;
40453 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
40454
40455 keep_next_level (true);
40456 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
40457
40458 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40459 return add_stmt (stmt);
40460 }
40461
40462 /* OpenMP 4.5:
40463 # pragma omp target enter data target-enter-data-clause[optseq] new-line
40464 structured-block */
40465
40466 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
40467 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
40469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40472
40473 static tree
40474 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
40475 enum pragma_context context)
40476 {
40477 bool data_seen = false;
40478 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40479 {
40480 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40481 const char *p = IDENTIFIER_POINTER (id);
40482
40483 if (strcmp (p, "data") == 0)
40484 {
40485 cp_lexer_consume_token (parser->lexer);
40486 data_seen = true;
40487 }
40488 }
40489 if (!data_seen)
40490 {
40491 cp_parser_error (parser, "expected %<data%>");
40492 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40493 return NULL_TREE;
40494 }
40495
40496 if (context == pragma_stmt)
40497 {
40498 error_at (pragma_tok->location,
40499 "%<#pragma %s%> may only be used in compound statements",
40500 "omp target enter data");
40501 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40502 return NULL_TREE;
40503 }
40504
40505 tree clauses
40506 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
40507 "#pragma omp target enter data", pragma_tok);
40508 int map_seen = 0;
40509 for (tree *pc = &clauses; *pc;)
40510 {
40511 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
40512 switch (OMP_CLAUSE_MAP_KIND (*pc))
40513 {
40514 case GOMP_MAP_TO:
40515 case GOMP_MAP_ALWAYS_TO:
40516 case GOMP_MAP_ALLOC:
40517 map_seen = 3;
40518 break;
40519 case GOMP_MAP_FIRSTPRIVATE_POINTER:
40520 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
40521 case GOMP_MAP_ALWAYS_POINTER:
40522 break;
40523 default:
40524 map_seen |= 1;
40525 error_at (OMP_CLAUSE_LOCATION (*pc),
40526 "%<#pragma omp target enter data%> with map-type other "
40527 "than %<to%> or %<alloc%> on %<map%> clause");
40528 *pc = OMP_CLAUSE_CHAIN (*pc);
40529 continue;
40530 }
40531 pc = &OMP_CLAUSE_CHAIN (*pc);
40532 }
40533
40534 if (map_seen != 3)
40535 {
40536 if (map_seen == 0)
40537 error_at (pragma_tok->location,
40538 "%<#pragma omp target enter data%> must contain at least "
40539 "one %<map%> clause");
40540 return NULL_TREE;
40541 }
40542
40543 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
40544 TREE_TYPE (stmt) = void_type_node;
40545 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
40546 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40547 return add_stmt (stmt);
40548 }
40549
40550 /* OpenMP 4.5:
40551 # pragma omp target exit data target-enter-data-clause[optseq] new-line
40552 structured-block */
40553
40554 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
40555 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
40557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40560
40561 static tree
40562 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
40563 enum pragma_context context)
40564 {
40565 bool data_seen = false;
40566 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40567 {
40568 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40569 const char *p = IDENTIFIER_POINTER (id);
40570
40571 if (strcmp (p, "data") == 0)
40572 {
40573 cp_lexer_consume_token (parser->lexer);
40574 data_seen = true;
40575 }
40576 }
40577 if (!data_seen)
40578 {
40579 cp_parser_error (parser, "expected %<data%>");
40580 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40581 return NULL_TREE;
40582 }
40583
40584 if (context == pragma_stmt)
40585 {
40586 error_at (pragma_tok->location,
40587 "%<#pragma %s%> may only be used in compound statements",
40588 "omp target exit data");
40589 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40590 return NULL_TREE;
40591 }
40592
40593 tree clauses
40594 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
40595 "#pragma omp target exit data", pragma_tok);
40596 int map_seen = 0;
40597 for (tree *pc = &clauses; *pc;)
40598 {
40599 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
40600 switch (OMP_CLAUSE_MAP_KIND (*pc))
40601 {
40602 case GOMP_MAP_FROM:
40603 case GOMP_MAP_ALWAYS_FROM:
40604 case GOMP_MAP_RELEASE:
40605 case GOMP_MAP_DELETE:
40606 map_seen = 3;
40607 break;
40608 case GOMP_MAP_FIRSTPRIVATE_POINTER:
40609 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
40610 case GOMP_MAP_ALWAYS_POINTER:
40611 break;
40612 default:
40613 map_seen |= 1;
40614 error_at (OMP_CLAUSE_LOCATION (*pc),
40615 "%<#pragma omp target exit data%> with map-type other "
40616 "than %<from%>, %<release%> or %<delete%> on %<map%>"
40617 " clause");
40618 *pc = OMP_CLAUSE_CHAIN (*pc);
40619 continue;
40620 }
40621 pc = &OMP_CLAUSE_CHAIN (*pc);
40622 }
40623
40624 if (map_seen != 3)
40625 {
40626 if (map_seen == 0)
40627 error_at (pragma_tok->location,
40628 "%<#pragma omp target exit data%> must contain at least "
40629 "one %<map%> clause");
40630 return NULL_TREE;
40631 }
40632
40633 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
40634 TREE_TYPE (stmt) = void_type_node;
40635 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
40636 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40637 return add_stmt (stmt);
40638 }
40639
40640 /* OpenMP 4.0:
40641 # pragma omp target update target-update-clause[optseq] new-line */
40642
40643 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
40644 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
40645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
40646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40650
40651 static bool
40652 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
40653 enum pragma_context context)
40654 {
40655 if (context == pragma_stmt)
40656 {
40657 error_at (pragma_tok->location,
40658 "%<#pragma %s%> may only be used in compound statements",
40659 "omp target update");
40660 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40661 return false;
40662 }
40663
40664 tree clauses
40665 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
40666 "#pragma omp target update", pragma_tok);
40667 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
40668 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
40669 {
40670 error_at (pragma_tok->location,
40671 "%<#pragma omp target update%> must contain at least one "
40672 "%<from%> or %<to%> clauses");
40673 return false;
40674 }
40675
40676 tree stmt = make_node (OMP_TARGET_UPDATE);
40677 TREE_TYPE (stmt) = void_type_node;
40678 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
40679 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40680 add_stmt (stmt);
40681 return false;
40682 }
40683
40684 /* OpenMP 4.0:
40685 # pragma omp target target-clause[optseq] new-line
40686 structured-block */
40687
40688 #define OMP_TARGET_CLAUSE_MASK \
40689 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
40691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
40694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
40697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
40698
40699 static bool
40700 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
40701 enum pragma_context context, bool *if_p)
40702 {
40703 tree *pc = NULL, stmt;
40704
40705 if (flag_openmp)
40706 omp_requires_mask
40707 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
40708
40709 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40710 {
40711 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40712 const char *p = IDENTIFIER_POINTER (id);
40713 enum tree_code ccode = ERROR_MARK;
40714
40715 if (strcmp (p, "teams") == 0)
40716 ccode = OMP_TEAMS;
40717 else if (strcmp (p, "parallel") == 0)
40718 ccode = OMP_PARALLEL;
40719 else if (strcmp (p, "simd") == 0)
40720 ccode = OMP_SIMD;
40721 if (ccode != ERROR_MARK)
40722 {
40723 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
40724 char p_name[sizeof ("#pragma omp target teams distribute "
40725 "parallel for simd")];
40726
40727 cp_lexer_consume_token (parser->lexer);
40728 strcpy (p_name, "#pragma omp target");
40729 if (!flag_openmp) /* flag_openmp_simd */
40730 {
40731 tree stmt;
40732 switch (ccode)
40733 {
40734 case OMP_TEAMS:
40735 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
40736 OMP_TARGET_CLAUSE_MASK,
40737 cclauses, if_p);
40738 break;
40739 case OMP_PARALLEL:
40740 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
40741 OMP_TARGET_CLAUSE_MASK,
40742 cclauses, if_p);
40743 break;
40744 case OMP_SIMD:
40745 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
40746 OMP_TARGET_CLAUSE_MASK,
40747 cclauses, if_p);
40748 break;
40749 default:
40750 gcc_unreachable ();
40751 }
40752 return stmt != NULL_TREE;
40753 }
40754 keep_next_level (true);
40755 tree sb = begin_omp_structured_block (), ret;
40756 unsigned save = cp_parser_begin_omp_structured_block (parser);
40757 switch (ccode)
40758 {
40759 case OMP_TEAMS:
40760 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
40761 OMP_TARGET_CLAUSE_MASK, cclauses,
40762 if_p);
40763 break;
40764 case OMP_PARALLEL:
40765 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
40766 OMP_TARGET_CLAUSE_MASK, cclauses,
40767 if_p);
40768 break;
40769 case OMP_SIMD:
40770 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
40771 OMP_TARGET_CLAUSE_MASK, cclauses,
40772 if_p);
40773 break;
40774 default:
40775 gcc_unreachable ();
40776 }
40777 cp_parser_end_omp_structured_block (parser, save);
40778 tree body = finish_omp_structured_block (sb);
40779 if (ret == NULL_TREE)
40780 return false;
40781 if (ccode == OMP_TEAMS && !processing_template_decl)
40782 {
40783 /* For combined target teams, ensure the num_teams and
40784 thread_limit clause expressions are evaluated on the host,
40785 before entering the target construct. */
40786 tree c;
40787 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
40788 c; c = OMP_CLAUSE_CHAIN (c))
40789 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
40790 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
40791 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
40792 {
40793 tree expr = OMP_CLAUSE_OPERAND (c, 0);
40794 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
40795 if (expr == error_mark_node)
40796 continue;
40797 tree tmp = TARGET_EXPR_SLOT (expr);
40798 add_stmt (expr);
40799 OMP_CLAUSE_OPERAND (c, 0) = expr;
40800 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
40801 OMP_CLAUSE_FIRSTPRIVATE);
40802 OMP_CLAUSE_DECL (tc) = tmp;
40803 OMP_CLAUSE_CHAIN (tc)
40804 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
40805 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
40806 }
40807 }
40808 tree stmt = make_node (OMP_TARGET);
40809 TREE_TYPE (stmt) = void_type_node;
40810 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
40811 OMP_TARGET_BODY (stmt) = body;
40812 OMP_TARGET_COMBINED (stmt) = 1;
40813 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40814 add_stmt (stmt);
40815 pc = &OMP_TARGET_CLAUSES (stmt);
40816 goto check_clauses;
40817 }
40818 else if (!flag_openmp) /* flag_openmp_simd */
40819 {
40820 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40821 return false;
40822 }
40823 else if (strcmp (p, "data") == 0)
40824 {
40825 cp_lexer_consume_token (parser->lexer);
40826 cp_parser_omp_target_data (parser, pragma_tok, if_p);
40827 return true;
40828 }
40829 else if (strcmp (p, "enter") == 0)
40830 {
40831 cp_lexer_consume_token (parser->lexer);
40832 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
40833 return false;
40834 }
40835 else if (strcmp (p, "exit") == 0)
40836 {
40837 cp_lexer_consume_token (parser->lexer);
40838 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
40839 return false;
40840 }
40841 else if (strcmp (p, "update") == 0)
40842 {
40843 cp_lexer_consume_token (parser->lexer);
40844 return cp_parser_omp_target_update (parser, pragma_tok, context);
40845 }
40846 }
40847 if (!flag_openmp) /* flag_openmp_simd */
40848 {
40849 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40850 return false;
40851 }
40852
40853 stmt = make_node (OMP_TARGET);
40854 TREE_TYPE (stmt) = void_type_node;
40855
40856 OMP_TARGET_CLAUSES (stmt)
40857 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
40858 "#pragma omp target", pragma_tok);
40859 pc = &OMP_TARGET_CLAUSES (stmt);
40860 keep_next_level (true);
40861 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
40862
40863 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40864 add_stmt (stmt);
40865
40866 check_clauses:
40867 while (*pc)
40868 {
40869 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
40870 switch (OMP_CLAUSE_MAP_KIND (*pc))
40871 {
40872 case GOMP_MAP_TO:
40873 case GOMP_MAP_ALWAYS_TO:
40874 case GOMP_MAP_FROM:
40875 case GOMP_MAP_ALWAYS_FROM:
40876 case GOMP_MAP_TOFROM:
40877 case GOMP_MAP_ALWAYS_TOFROM:
40878 case GOMP_MAP_ALLOC:
40879 case GOMP_MAP_FIRSTPRIVATE_POINTER:
40880 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
40881 case GOMP_MAP_ALWAYS_POINTER:
40882 break;
40883 default:
40884 error_at (OMP_CLAUSE_LOCATION (*pc),
40885 "%<#pragma omp target%> with map-type other "
40886 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
40887 "on %<map%> clause");
40888 *pc = OMP_CLAUSE_CHAIN (*pc);
40889 continue;
40890 }
40891 pc = &OMP_CLAUSE_CHAIN (*pc);
40892 }
40893 return true;
40894 }
40895
40896 /* OpenACC 2.0:
40897 # pragma acc cache (variable-list) new-line
40898 */
40899
40900 static tree
40901 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
40902 {
40903 tree stmt, clauses;
40904
40905 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
40906 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
40907
40908 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
40909
40910 stmt = make_node (OACC_CACHE);
40911 TREE_TYPE (stmt) = void_type_node;
40912 OACC_CACHE_CLAUSES (stmt) = clauses;
40913 SET_EXPR_LOCATION (stmt, pragma_tok->location);
40914 add_stmt (stmt);
40915
40916 return stmt;
40917 }
40918
40919 /* OpenACC 2.0:
40920 # pragma acc data oacc-data-clause[optseq] new-line
40921 structured-block */
40922
40923 #define OACC_DATA_CLAUSE_MASK \
40924 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
40925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
40926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
40927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
40928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
40929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
40930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
40931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
40933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
40934
40935 static tree
40936 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40937 {
40938 tree stmt, clauses, block;
40939 unsigned int save;
40940
40941 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
40942 "#pragma acc data", pragma_tok);
40943
40944 block = begin_omp_parallel ();
40945 save = cp_parser_begin_omp_structured_block (parser);
40946 cp_parser_statement (parser, NULL_TREE, false, if_p);
40947 cp_parser_end_omp_structured_block (parser, save);
40948 stmt = finish_oacc_data (clauses, block);
40949 return stmt;
40950 }
40951
40952 /* OpenACC 2.0:
40953 # pragma acc host_data <clauses> new-line
40954 structured-block */
40955
40956 #define OACC_HOST_DATA_CLAUSE_MASK \
40957 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
40958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
40960
40961 static tree
40962 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40963 {
40964 tree stmt, clauses, block;
40965 unsigned int save;
40966
40967 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
40968 "#pragma acc host_data", pragma_tok);
40969
40970 block = begin_omp_parallel ();
40971 save = cp_parser_begin_omp_structured_block (parser);
40972 cp_parser_statement (parser, NULL_TREE, false, if_p);
40973 cp_parser_end_omp_structured_block (parser, save);
40974 stmt = finish_oacc_host_data (clauses, block);
40975 return stmt;
40976 }
40977
40978 /* OpenACC 2.0:
40979 # pragma acc declare oacc-data-clause[optseq] new-line
40980 */
40981
40982 #define OACC_DECLARE_CLAUSE_MASK \
40983 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
40984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
40985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
40986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
40987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
40988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
40989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
40990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
40991
40992 static tree
40993 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
40994 {
40995 tree clauses, stmt;
40996 bool error = false;
40997 bool found_in_scope = global_bindings_p ();
40998
40999 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
41000 "#pragma acc declare", pragma_tok, true);
41001
41002
41003 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
41004 {
41005 error_at (pragma_tok->location,
41006 "no valid clauses specified in %<#pragma acc declare%>");
41007 return NULL_TREE;
41008 }
41009
41010 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
41011 {
41012 location_t loc = OMP_CLAUSE_LOCATION (t);
41013 tree decl = OMP_CLAUSE_DECL (t);
41014 if (!DECL_P (decl))
41015 {
41016 error_at (loc, "array section in %<#pragma acc declare%>");
41017 error = true;
41018 continue;
41019 }
41020 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
41021 switch (OMP_CLAUSE_MAP_KIND (t))
41022 {
41023 case GOMP_MAP_FIRSTPRIVATE_POINTER:
41024 case GOMP_MAP_ALLOC:
41025 case GOMP_MAP_TO:
41026 case GOMP_MAP_FORCE_DEVICEPTR:
41027 case GOMP_MAP_DEVICE_RESIDENT:
41028 break;
41029
41030 case GOMP_MAP_LINK:
41031 if (!global_bindings_p ()
41032 && (TREE_STATIC (decl)
41033 || !DECL_EXTERNAL (decl)))
41034 {
41035 error_at (loc,
41036 "%qD must be a global variable in "
41037 "%<#pragma acc declare link%>",
41038 decl);
41039 error = true;
41040 continue;
41041 }
41042 break;
41043
41044 default:
41045 if (global_bindings_p ())
41046 {
41047 error_at (loc, "invalid OpenACC clause at file scope");
41048 error = true;
41049 continue;
41050 }
41051 if (DECL_EXTERNAL (decl))
41052 {
41053 error_at (loc,
41054 "invalid use of %<extern%> variable %qD "
41055 "in %<#pragma acc declare%>", decl);
41056 error = true;
41057 continue;
41058 }
41059 else if (TREE_PUBLIC (decl))
41060 {
41061 error_at (loc,
41062 "invalid use of %<global%> variable %qD "
41063 "in %<#pragma acc declare%>", decl);
41064 error = true;
41065 continue;
41066 }
41067 break;
41068 }
41069
41070 if (!found_in_scope)
41071 for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
41072 if (d == decl)
41073 {
41074 found_in_scope = true;
41075 break;
41076 }
41077 if (!found_in_scope)
41078 {
41079 error_at (loc,
41080 "%qD must be a variable declared in the same scope as "
41081 "%<#pragma acc declare%>", decl);
41082 error = true;
41083 continue;
41084 }
41085
41086 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
41087 || lookup_attribute ("omp declare target link",
41088 DECL_ATTRIBUTES (decl)))
41089 {
41090 error_at (loc, "variable %qD used more than once with "
41091 "%<#pragma acc declare%>", decl);
41092 error = true;
41093 continue;
41094 }
41095
41096 if (!error)
41097 {
41098 tree id;
41099
41100 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
41101 id = get_identifier ("omp declare target link");
41102 else
41103 id = get_identifier ("omp declare target");
41104
41105 DECL_ATTRIBUTES (decl)
41106 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
41107 if (current_binding_level->kind == sk_namespace)
41108 {
41109 symtab_node *node = symtab_node::get (decl);
41110 if (node != NULL)
41111 {
41112 node->offloadable = 1;
41113 if (ENABLE_OFFLOADING)
41114 {
41115 g->have_offload = true;
41116 if (is_a <varpool_node *> (node))
41117 vec_safe_push (offload_vars, decl);
41118 }
41119 }
41120 }
41121 }
41122 }
41123
41124 if (error || current_binding_level->kind == sk_namespace)
41125 return NULL_TREE;
41126
41127 stmt = make_node (OACC_DECLARE);
41128 TREE_TYPE (stmt) = void_type_node;
41129 OACC_DECLARE_CLAUSES (stmt) = clauses;
41130 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41131
41132 add_stmt (stmt);
41133
41134 return NULL_TREE;
41135 }
41136
41137 /* OpenACC 2.0:
41138 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
41139
41140 or
41141
41142 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
41143
41144 LOC is the location of the #pragma token.
41145 */
41146
41147 #define OACC_ENTER_DATA_CLAUSE_MASK \
41148 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41154
41155 #define OACC_EXIT_DATA_CLAUSE_MASK \
41156 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
41160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
41161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
41162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41163
41164 static tree
41165 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
41166 bool enter)
41167 {
41168 location_t loc = pragma_tok->location;
41169 tree stmt, clauses;
41170 const char *p = "";
41171
41172 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41173 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
41174
41175 if (strcmp (p, "data") != 0)
41176 {
41177 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
41178 enter ? "enter" : "exit");
41179 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41180 return NULL_TREE;
41181 }
41182
41183 cp_lexer_consume_token (parser->lexer);
41184
41185 if (enter)
41186 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
41187 "#pragma acc enter data", pragma_tok);
41188 else
41189 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
41190 "#pragma acc exit data", pragma_tok);
41191
41192 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
41193 {
41194 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
41195 enter ? "enter" : "exit");
41196 return NULL_TREE;
41197 }
41198
41199 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
41200 TREE_TYPE (stmt) = void_type_node;
41201 OMP_STANDALONE_CLAUSES (stmt) = clauses;
41202 SET_EXPR_LOCATION (stmt, loc);
41203 add_stmt (stmt);
41204 return stmt;
41205 }
41206
41207 /* OpenACC 2.0:
41208 # pragma acc loop oacc-loop-clause[optseq] new-line
41209 structured-block */
41210
41211 #define OACC_LOOP_CLAUSE_MASK \
41212 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
41213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
41214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
41215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
41216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
41217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
41218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
41219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
41220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
41221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
41222
41223 static tree
41224 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
41225 omp_clause_mask mask, tree *cclauses, bool *if_p)
41226 {
41227 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
41228
41229 strcat (p_name, " loop");
41230 mask |= OACC_LOOP_CLAUSE_MASK;
41231
41232 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
41233 cclauses == NULL);
41234 if (cclauses)
41235 {
41236 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
41237 if (*cclauses)
41238 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
41239 if (clauses)
41240 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
41241 }
41242
41243 tree block = begin_omp_structured_block ();
41244 int save = cp_parser_begin_omp_structured_block (parser);
41245 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
41246 cp_parser_end_omp_structured_block (parser, save);
41247 add_stmt (finish_omp_structured_block (block));
41248
41249 return stmt;
41250 }
41251
41252 /* OpenACC 2.0:
41253 # pragma acc kernels oacc-kernels-clause[optseq] new-line
41254 structured-block
41255
41256 or
41257
41258 # pragma acc parallel oacc-parallel-clause[optseq] new-line
41259 structured-block
41260
41261 OpenACC 2.6:
41262
41263 # pragma acc serial oacc-serial-clause[optseq] new-line
41264 */
41265
41266 #define OACC_KERNELS_CLAUSE_MASK \
41267 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
41270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
41274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
41275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
41277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
41278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
41279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
41280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
41281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41282
41283 #define OACC_PARALLEL_CLAUSE_MASK \
41284 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
41287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
41291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
41292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
41293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
41295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
41296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
41297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
41298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
41299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
41300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
41301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41302
41303 #define OACC_SERIAL_CLAUSE_MASK \
41304 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
41307 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
41311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
41312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
41314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
41315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
41316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
41317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
41318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41319
41320 static tree
41321 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
41322 char *p_name, bool *if_p)
41323 {
41324 omp_clause_mask mask;
41325 enum tree_code code;
41326 switch (cp_parser_pragma_kind (pragma_tok))
41327 {
41328 case PRAGMA_OACC_KERNELS:
41329 strcat (p_name, " kernels");
41330 mask = OACC_KERNELS_CLAUSE_MASK;
41331 code = OACC_KERNELS;
41332 break;
41333 case PRAGMA_OACC_PARALLEL:
41334 strcat (p_name, " parallel");
41335 mask = OACC_PARALLEL_CLAUSE_MASK;
41336 code = OACC_PARALLEL;
41337 break;
41338 case PRAGMA_OACC_SERIAL:
41339 strcat (p_name, " serial");
41340 mask = OACC_SERIAL_CLAUSE_MASK;
41341 code = OACC_SERIAL;
41342 break;
41343 default:
41344 gcc_unreachable ();
41345 }
41346
41347 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41348 {
41349 const char *p
41350 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
41351 if (strcmp (p, "loop") == 0)
41352 {
41353 cp_lexer_consume_token (parser->lexer);
41354 tree block = begin_omp_parallel ();
41355 tree clauses;
41356 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
41357 &clauses, if_p);
41358 protected_set_expr_location (stmt, pragma_tok->location);
41359 return finish_omp_construct (code, block, clauses);
41360 }
41361 }
41362
41363 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
41364
41365 tree block = begin_omp_parallel ();
41366 unsigned int save = cp_parser_begin_omp_structured_block (parser);
41367 cp_parser_statement (parser, NULL_TREE, false, if_p);
41368 cp_parser_end_omp_structured_block (parser, save);
41369 return finish_omp_construct (code, block, clauses);
41370 }
41371
41372 /* OpenACC 2.0:
41373 # pragma acc update oacc-update-clause[optseq] new-line
41374 */
41375
41376 #define OACC_UPDATE_CLAUSE_MASK \
41377 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
41379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
41380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
41382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
41383
41384 static tree
41385 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
41386 {
41387 tree stmt, clauses;
41388
41389 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
41390 "#pragma acc update", pragma_tok);
41391
41392 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
41393 {
41394 error_at (pragma_tok->location,
41395 "%<#pragma acc update%> must contain at least one "
41396 "%<device%> or %<host%> or %<self%> clause");
41397 return NULL_TREE;
41398 }
41399
41400 stmt = make_node (OACC_UPDATE);
41401 TREE_TYPE (stmt) = void_type_node;
41402 OACC_UPDATE_CLAUSES (stmt) = clauses;
41403 SET_EXPR_LOCATION (stmt, pragma_tok->location);
41404 add_stmt (stmt);
41405 return stmt;
41406 }
41407
41408 /* OpenACC 2.0:
41409 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
41410
41411 LOC is the location of the #pragma token.
41412 */
41413
41414 #define OACC_WAIT_CLAUSE_MASK \
41415 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
41416
41417 static tree
41418 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
41419 {
41420 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
41421 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41422
41423 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
41424 list = cp_parser_oacc_wait_list (parser, loc, list);
41425
41426 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
41427 "#pragma acc wait", pragma_tok);
41428
41429 stmt = c_finish_oacc_wait (loc, list, clauses);
41430 stmt = finish_expr_stmt (stmt);
41431
41432 return stmt;
41433 }
41434
41435 /* OpenMP 4.0:
41436 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
41437
41438 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
41439 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
41440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
41441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
41442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
41443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
41444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
41445
41446 static void
41447 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
41448 enum pragma_context context,
41449 bool variant_p)
41450 {
41451 bool first_p = parser->omp_declare_simd == NULL;
41452 cp_omp_declare_simd_data data;
41453 if (first_p)
41454 {
41455 data.error_seen = false;
41456 data.fndecl_seen = false;
41457 data.variant_p = variant_p;
41458 data.tokens = vNULL;
41459 data.clauses = NULL_TREE;
41460 /* It is safe to take the address of a local variable; it will only be
41461 used while this scope is live. */
41462 parser->omp_declare_simd = &data;
41463 }
41464 else if (parser->omp_declare_simd->variant_p != variant_p)
41465 {
41466 error_at (pragma_tok->location,
41467 "%<#pragma omp declare %s%> followed by "
41468 "%<#pragma omp declare %s%>",
41469 parser->omp_declare_simd->variant_p ? "variant" : "simd",
41470 parser->omp_declare_simd->variant_p ? "simd" : "variant");
41471 parser->omp_declare_simd->error_seen = true;
41472 }
41473
41474 /* Store away all pragma tokens. */
41475 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
41476 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
41477 cp_lexer_consume_token (parser->lexer);
41478 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41479 parser->omp_declare_simd->error_seen = true;
41480 cp_parser_require_pragma_eol (parser, pragma_tok);
41481 struct cp_token_cache *cp
41482 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
41483 parser->omp_declare_simd->tokens.safe_push (cp);
41484
41485 if (first_p)
41486 {
41487 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
41488 cp_parser_pragma (parser, context, NULL);
41489 switch (context)
41490 {
41491 case pragma_external:
41492 cp_parser_declaration (parser);
41493 break;
41494 case pragma_member:
41495 cp_parser_member_declaration (parser);
41496 break;
41497 case pragma_objc_icode:
41498 cp_parser_block_declaration (parser, /*statement_p=*/false);
41499 break;
41500 default:
41501 cp_parser_declaration_statement (parser);
41502 break;
41503 }
41504 if (parser->omp_declare_simd
41505 && !parser->omp_declare_simd->error_seen
41506 && !parser->omp_declare_simd->fndecl_seen)
41507 error_at (pragma_tok->location,
41508 "%<#pragma omp declare %s%> not immediately followed by "
41509 "function declaration or definition",
41510 parser->omp_declare_simd->variant_p ? "variant" : "simd");
41511 data.tokens.release ();
41512 parser->omp_declare_simd = NULL;
41513 }
41514 }
41515
41516 static const char *const omp_construct_selectors[] = {
41517 "simd", "target", "teams", "parallel", "for", NULL };
41518 static const char *const omp_device_selectors[] = {
41519 "kind", "isa", "arch", NULL };
41520 static const char *const omp_implementation_selectors[] = {
41521 "vendor", "extension", "atomic_default_mem_order", "unified_address",
41522 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
41523 static const char *const omp_user_selectors[] = {
41524 "condition", NULL };
41525
41526 /* OpenMP 5.0:
41527
41528 trait-selector:
41529 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
41530
41531 trait-score:
41532 score(score-expression) */
41533
41534 static tree
41535 cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
41536 {
41537 tree ret = NULL_TREE;
41538 do
41539 {
41540 tree selector;
41541 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
41542 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41543 selector = cp_lexer_peek_token (parser->lexer)->u.value;
41544 else
41545 {
41546 cp_parser_error (parser, "expected trait selector name");
41547 return error_mark_node;
41548 }
41549
41550 tree properties = NULL_TREE;
41551 const char *const *selectors = NULL;
41552 bool allow_score = true;
41553 bool allow_user = false;
41554 int property_limit = 0;
41555 enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
41556 CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
41557 CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
41558 switch (IDENTIFIER_POINTER (set)[0])
41559 {
41560 case 'c': /* construct */
41561 selectors = omp_construct_selectors;
41562 allow_score = false;
41563 property_limit = 1;
41564 property_kind = CTX_PROPERTY_SIMD;
41565 break;
41566 case 'd': /* device */
41567 selectors = omp_device_selectors;
41568 allow_score = false;
41569 allow_user = true;
41570 property_limit = 3;
41571 property_kind = CTX_PROPERTY_NAME_LIST;
41572 break;
41573 case 'i': /* implementation */
41574 selectors = omp_implementation_selectors;
41575 allow_user = true;
41576 property_limit = 3;
41577 property_kind = CTX_PROPERTY_NAME_LIST;
41578 break;
41579 case 'u': /* user */
41580 selectors = omp_user_selectors;
41581 property_limit = 1;
41582 property_kind = CTX_PROPERTY_EXPR;
41583 break;
41584 default:
41585 gcc_unreachable ();
41586 }
41587 for (int i = 0; ; i++)
41588 {
41589 if (selectors[i] == NULL)
41590 {
41591 if (allow_user)
41592 {
41593 property_kind = CTX_PROPERTY_USER;
41594 break;
41595 }
41596 else
41597 {
41598 error ("selector %qs not allowed for context selector "
41599 "set %qs", IDENTIFIER_POINTER (selector),
41600 IDENTIFIER_POINTER (set));
41601 cp_lexer_consume_token (parser->lexer);
41602 return error_mark_node;
41603 }
41604 }
41605 if (i == property_limit)
41606 property_kind = CTX_PROPERTY_NONE;
41607 if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
41608 break;
41609 }
41610 if (property_kind == CTX_PROPERTY_NAME_LIST
41611 && IDENTIFIER_POINTER (set)[0] == 'i'
41612 && strcmp (IDENTIFIER_POINTER (selector),
41613 "atomic_default_mem_order") == 0)
41614 property_kind = CTX_PROPERTY_ID;
41615
41616 cp_lexer_consume_token (parser->lexer);
41617
41618 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
41619 {
41620 if (property_kind == CTX_PROPERTY_NONE)
41621 {
41622 error ("selector %qs does not accept any properties",
41623 IDENTIFIER_POINTER (selector));
41624 return error_mark_node;
41625 }
41626
41627 matching_parens parens;
41628 parens.consume_open (parser);
41629
41630 cp_token *token = cp_lexer_peek_token (parser->lexer);
41631 if (allow_score
41632 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
41633 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
41634 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
41635 {
41636 cp_lexer_save_tokens (parser->lexer);
41637 cp_lexer_consume_token (parser->lexer);
41638 cp_lexer_consume_token (parser->lexer);
41639 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
41640 true)
41641 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
41642 {
41643 cp_lexer_rollback_tokens (parser->lexer);
41644 cp_lexer_consume_token (parser->lexer);
41645
41646 matching_parens parens2;
41647 parens2.require_open (parser);
41648 tree score = cp_parser_constant_expression (parser);
41649 if (!parens2.require_close (parser))
41650 cp_parser_skip_to_closing_parenthesis (parser, true,
41651 false, true);
41652 cp_parser_require (parser, CPP_COLON, RT_COLON);
41653 if (score != error_mark_node)
41654 {
41655 score = fold_non_dependent_expr (score);
41656 if (value_dependent_expression_p (score))
41657 properties = tree_cons (get_identifier (" score"),
41658 score, properties);
41659 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
41660 || TREE_CODE (score) != INTEGER_CST)
41661 error_at (token->location, "score argument must be "
41662 "constant integer expression");
41663 else if (tree_int_cst_sgn (score) < 0)
41664 error_at (token->location, "score argument must be "
41665 "non-negative");
41666 else
41667 properties = tree_cons (get_identifier (" score"),
41668 score, properties);
41669 }
41670 }
41671 else
41672 cp_lexer_rollback_tokens (parser->lexer);
41673
41674 token = cp_lexer_peek_token (parser->lexer);
41675 }
41676
41677 switch (property_kind)
41678 {
41679 tree t;
41680 case CTX_PROPERTY_USER:
41681 do
41682 {
41683 t = cp_parser_constant_expression (parser);
41684 if (t != error_mark_node)
41685 {
41686 t = fold_non_dependent_expr (t);
41687 if (TREE_CODE (t) == STRING_CST)
41688 properties = tree_cons (NULL_TREE, t, properties);
41689 else if (!value_dependent_expression_p (t)
41690 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
41691 || !tree_fits_shwi_p (t)))
41692 error_at (token->location, "property must be "
41693 "constant integer expression or string "
41694 "literal");
41695 else
41696 properties = tree_cons (NULL_TREE, t, properties);
41697 }
41698 else
41699 return error_mark_node;
41700
41701 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41702 cp_lexer_consume_token (parser->lexer);
41703 else
41704 break;
41705 }
41706 while (1);
41707 break;
41708 case CTX_PROPERTY_ID:
41709 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
41710 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41711 {
41712 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
41713 cp_lexer_consume_token (parser->lexer);
41714 properties = tree_cons (prop, NULL_TREE, properties);
41715 }
41716 else
41717 {
41718 cp_parser_error (parser, "expected identifier");
41719 return error_mark_node;
41720 }
41721 break;
41722 case CTX_PROPERTY_NAME_LIST:
41723 do
41724 {
41725 tree prop = NULL_TREE, value = NULL_TREE;
41726 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
41727 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41728 {
41729 prop = cp_lexer_peek_token (parser->lexer)->u.value;
41730 cp_lexer_consume_token (parser->lexer);
41731 }
41732 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
41733 value = cp_parser_string_literal (parser, false, false);
41734 else
41735 {
41736 cp_parser_error (parser, "expected identifier or "
41737 "string literal");
41738 return error_mark_node;
41739 }
41740
41741 properties = tree_cons (prop, value, properties);
41742
41743 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41744 cp_lexer_consume_token (parser->lexer);
41745 else
41746 break;
41747 }
41748 while (1);
41749 break;
41750 case CTX_PROPERTY_EXPR:
41751 t = cp_parser_constant_expression (parser);
41752 if (t != error_mark_node)
41753 {
41754 t = fold_non_dependent_expr (t);
41755 if (!value_dependent_expression_p (t)
41756 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
41757 || !tree_fits_shwi_p (t)))
41758 error_at (token->location, "property must be "
41759 "constant integer expression");
41760 else
41761 properties = tree_cons (NULL_TREE, t, properties);
41762 }
41763 else
41764 return error_mark_node;
41765 break;
41766 case CTX_PROPERTY_SIMD:
41767 if (!has_parms_p)
41768 {
41769 error_at (token->location, "properties for %<simd%> "
41770 "selector may not be specified in "
41771 "%<metadirective%>");
41772 return error_mark_node;
41773 }
41774 properties
41775 = cp_parser_omp_all_clauses (parser,
41776 OMP_DECLARE_SIMD_CLAUSE_MASK,
41777 "simd", NULL, true, 2);
41778 break;
41779 default:
41780 gcc_unreachable ();
41781 }
41782
41783 if (!parens.require_close (parser))
41784 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
41785
41786 properties = nreverse (properties);
41787 }
41788 else if (property_kind == CTX_PROPERTY_NAME_LIST
41789 || property_kind == CTX_PROPERTY_ID
41790 || property_kind == CTX_PROPERTY_EXPR)
41791 {
41792 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
41793 return error_mark_node;
41794 }
41795
41796 ret = tree_cons (selector, properties, ret);
41797
41798 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41799 cp_lexer_consume_token (parser->lexer);
41800 else
41801 break;
41802 }
41803 while (1);
41804
41805 return nreverse (ret);
41806 }
41807
41808 /* OpenMP 5.0:
41809
41810 trait-set-selector[,trait-set-selector[,...]]
41811
41812 trait-set-selector:
41813 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
41814
41815 trait-set-selector-name:
41816 constructor
41817 device
41818 implementation
41819 user */
41820
41821 static tree
41822 cp_parser_omp_context_selector_specification (cp_parser *parser,
41823 bool has_parms_p)
41824 {
41825 tree ret = NULL_TREE;
41826 do
41827 {
41828 const char *setp = "";
41829 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41830 setp
41831 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
41832 switch (setp[0])
41833 {
41834 case 'c':
41835 if (strcmp (setp, "construct") == 0)
41836 setp = NULL;
41837 break;
41838 case 'd':
41839 if (strcmp (setp, "device") == 0)
41840 setp = NULL;
41841 break;
41842 case 'i':
41843 if (strcmp (setp, "implementation") == 0)
41844 setp = NULL;
41845 break;
41846 case 'u':
41847 if (strcmp (setp, "user") == 0)
41848 setp = NULL;
41849 break;
41850 default:
41851 break;
41852 }
41853 if (setp)
41854 {
41855 cp_parser_error (parser, "expected %<construct%>, %<device%>, "
41856 "%<implementation%> or %<user%>");
41857 return error_mark_node;
41858 }
41859
41860 tree set = cp_lexer_peek_token (parser->lexer)->u.value;
41861 cp_lexer_consume_token (parser->lexer);
41862
41863 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41864 return error_mark_node;
41865
41866 matching_braces braces;
41867 if (!braces.require_open (parser))
41868 return error_mark_node;
41869
41870 tree selectors
41871 = cp_parser_omp_context_selector (parser, set, has_parms_p);
41872 if (selectors == error_mark_node)
41873 {
41874 cp_parser_skip_to_closing_brace (parser);
41875 ret = error_mark_node;
41876 }
41877 else if (ret != error_mark_node)
41878 ret = tree_cons (set, selectors, ret);
41879
41880 braces.require_close (parser);
41881
41882 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41883 cp_lexer_consume_token (parser->lexer);
41884 else
41885 break;
41886 }
41887 while (1);
41888
41889 if (ret == error_mark_node)
41890 return ret;
41891 return nreverse (ret);
41892 }
41893
41894 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
41895 that into "omp declare variant base" attribute. */
41896
41897 static tree
41898 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
41899 tree attrs)
41900 {
41901 matching_parens parens;
41902 if (!parens.require_open (parser))
41903 {
41904 fail:
41905 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41906 return attrs;
41907 }
41908
41909 bool template_p;
41910 cp_id_kind idk = CP_ID_KIND_NONE;
41911 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
41912 cp_expr varid
41913 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
41914 /*check_dependency_p=*/true,
41915 /*template_p=*/&template_p,
41916 /*declarator_p=*/false,
41917 /*optional_p=*/false);
41918 parens.require_close (parser);
41919
41920 tree variant;
41921 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
41922 || TREE_CODE (varid) == TYPE_DECL
41923 || varid == error_mark_node)
41924 variant = varid;
41925 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
41926 variant = NULL_TREE;
41927 else
41928 {
41929 tree ambiguous_decls;
41930 variant = cp_parser_lookup_name (parser, varid, none_type,
41931 template_p, /*is_namespace=*/false,
41932 /*check_dependency=*/true,
41933 &ambiguous_decls,
41934 varid.get_location ());
41935 if (ambiguous_decls)
41936 variant = NULL_TREE;
41937 }
41938 if (variant == NULL_TREE)
41939 variant = error_mark_node;
41940 else if (TREE_CODE (variant) != SCOPE_REF)
41941 {
41942 const char *error_msg;
41943 variant
41944 = finish_id_expression (varid, variant, parser->scope,
41945 &idk, false, true,
41946 &parser->non_integral_constant_expression_p,
41947 template_p, true, false, false, &error_msg,
41948 varid.get_location ());
41949 if (error_msg)
41950 cp_parser_error (parser, error_msg);
41951 }
41952 location_t caret_loc = get_pure_location (varid.get_location ());
41953 location_t start_loc = get_start (varid_token->location);
41954 location_t finish_loc = get_finish (varid.get_location ());
41955 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
41956
41957 const char *clause = "";
41958 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
41959 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41960 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
41961 if (strcmp (clause, "match"))
41962 {
41963 cp_parser_error (parser, "expected %<match%>");
41964 goto fail;
41965 }
41966
41967 cp_lexer_consume_token (parser->lexer);
41968
41969 if (!parens.require_open (parser))
41970 goto fail;
41971
41972 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
41973 if (ctx == error_mark_node)
41974 goto fail;
41975 ctx = c_omp_check_context_selector (match_loc, ctx);
41976 if (ctx != error_mark_node && variant != error_mark_node)
41977 {
41978 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
41979 match_loc);
41980 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
41981 loc_node = tree_cons (match_loc_node,
41982 build_int_cst (integer_type_node, idk),
41983 build_tree_list (loc_node, integer_zero_node));
41984 attrs = tree_cons (get_identifier ("omp declare variant base"),
41985 tree_cons (variant, ctx, loc_node), attrs);
41986 if (processing_template_decl)
41987 ATTR_IS_DEPENDENT (attrs) = 1;
41988 }
41989
41990 parens.require_close (parser);
41991 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41992 return attrs;
41993 }
41994
41995
41996 /* Finalize #pragma omp declare simd clauses after direct declarator has
41997 been parsed, and put that into "omp declare simd" attribute. */
41998
41999 static tree
42000 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
42001 {
42002 struct cp_token_cache *ce;
42003 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
42004 int i;
42005
42006 if (!data->error_seen && data->fndecl_seen)
42007 {
42008 error ("%<#pragma omp declare %s%> not immediately followed by "
42009 "a single function declaration or definition",
42010 data->variant_p ? "variant" : "simd");
42011 data->error_seen = true;
42012 }
42013 if (data->error_seen)
42014 return attrs;
42015
42016 FOR_EACH_VEC_ELT (data->tokens, i, ce)
42017 {
42018 tree c, cl;
42019
42020 cp_parser_push_lexer_for_tokens (parser, ce);
42021 parser->lexer->in_pragma = true;
42022 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
42023 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
42024 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42025 const char *kind = IDENTIFIER_POINTER (id);
42026 cp_lexer_consume_token (parser->lexer);
42027 if (strcmp (kind, "simd") == 0)
42028 {
42029 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
42030 "#pragma omp declare simd",
42031 pragma_tok);
42032 if (cl)
42033 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
42034 c = build_tree_list (get_identifier ("omp declare simd"), cl);
42035 TREE_CHAIN (c) = attrs;
42036 if (processing_template_decl)
42037 ATTR_IS_DEPENDENT (c) = 1;
42038 attrs = c;
42039 }
42040 else
42041 {
42042 gcc_assert (strcmp (kind, "variant") == 0);
42043 attrs = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
42044 }
42045 cp_parser_pop_lexer (parser);
42046 }
42047
42048 data->fndecl_seen = true;
42049 return attrs;
42050 }
42051
42052
42053 /* OpenMP 4.0:
42054 # pragma omp declare target new-line
42055 declarations and definitions
42056 # pragma omp end declare target new-line
42057
42058 OpenMP 4.5:
42059 # pragma omp declare target ( extended-list ) new-line
42060
42061 # pragma omp declare target declare-target-clauses[seq] new-line */
42062
42063 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
42064 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
42065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
42066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
42067
42068 static void
42069 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
42070 {
42071 tree clauses = NULL_TREE;
42072 int device_type = 0;
42073 bool only_device_type = true;
42074 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42075 clauses
42076 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
42077 "#pragma omp declare target", pragma_tok);
42078 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
42079 {
42080 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
42081 clauses);
42082 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
42083 cp_parser_require_pragma_eol (parser, pragma_tok);
42084 }
42085 else
42086 {
42087 cp_parser_require_pragma_eol (parser, pragma_tok);
42088 scope_chain->omp_declare_target_attribute++;
42089 return;
42090 }
42091 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
42092 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
42093 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
42094 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
42095 {
42096 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
42097 continue;
42098 tree t = OMP_CLAUSE_DECL (c), id;
42099 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
42100 tree at2 = lookup_attribute ("omp declare target link",
42101 DECL_ATTRIBUTES (t));
42102 only_device_type = false;
42103 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
42104 {
42105 id = get_identifier ("omp declare target link");
42106 std::swap (at1, at2);
42107 }
42108 else
42109 id = get_identifier ("omp declare target");
42110 if (at2)
42111 {
42112 error_at (OMP_CLAUSE_LOCATION (c),
42113 "%qD specified both in declare target %<link%> and %<to%>"
42114 " clauses", t);
42115 continue;
42116 }
42117 if (!at1)
42118 {
42119 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
42120 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
42121 continue;
42122
42123 symtab_node *node = symtab_node::get (t);
42124 if (node != NULL)
42125 {
42126 node->offloadable = 1;
42127 if (ENABLE_OFFLOADING)
42128 {
42129 g->have_offload = true;
42130 if (is_a <varpool_node *> (node))
42131 vec_safe_push (offload_vars, t);
42132 }
42133 }
42134 }
42135 if (TREE_CODE (t) != FUNCTION_DECL)
42136 continue;
42137 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
42138 {
42139 tree at3 = lookup_attribute ("omp declare target host",
42140 DECL_ATTRIBUTES (t));
42141 if (at3 == NULL_TREE)
42142 {
42143 id = get_identifier ("omp declare target host");
42144 DECL_ATTRIBUTES (t)
42145 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
42146 }
42147 }
42148 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
42149 {
42150 tree at3 = lookup_attribute ("omp declare target nohost",
42151 DECL_ATTRIBUTES (t));
42152 if (at3 == NULL_TREE)
42153 {
42154 id = get_identifier ("omp declare target nohost");
42155 DECL_ATTRIBUTES (t)
42156 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
42157 }
42158 }
42159 }
42160 if (device_type && only_device_type)
42161 warning_at (OMP_CLAUSE_LOCATION (clauses), 0,
42162 "directive with only %<device_type%> clauses ignored");
42163 }
42164
42165 static void
42166 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
42167 {
42168 const char *p = "";
42169 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42170 {
42171 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42172 p = IDENTIFIER_POINTER (id);
42173 }
42174 if (strcmp (p, "declare") == 0)
42175 {
42176 cp_lexer_consume_token (parser->lexer);
42177 p = "";
42178 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42179 {
42180 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42181 p = IDENTIFIER_POINTER (id);
42182 }
42183 if (strcmp (p, "target") == 0)
42184 cp_lexer_consume_token (parser->lexer);
42185 else
42186 {
42187 cp_parser_error (parser, "expected %<target%>");
42188 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42189 return;
42190 }
42191 }
42192 else
42193 {
42194 cp_parser_error (parser, "expected %<declare%>");
42195 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42196 return;
42197 }
42198 cp_parser_require_pragma_eol (parser, pragma_tok);
42199 if (!scope_chain->omp_declare_target_attribute)
42200 error_at (pragma_tok->location,
42201 "%<#pragma omp end declare target%> without corresponding "
42202 "%<#pragma omp declare target%>");
42203 else
42204 scope_chain->omp_declare_target_attribute--;
42205 }
42206
42207 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
42208 expression and optional initializer clause of
42209 #pragma omp declare reduction. We store the expression(s) as
42210 either 3, 6 or 7 special statements inside of the artificial function's
42211 body. The first two statements are DECL_EXPRs for the artificial
42212 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
42213 expression that uses those variables.
42214 If there was any INITIALIZER clause, this is followed by further statements,
42215 the fourth and fifth statements are DECL_EXPRs for the artificial
42216 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
42217 constructor variant (first token after open paren is not omp_priv),
42218 then the sixth statement is a statement with the function call expression
42219 that uses the OMP_PRIV and optionally OMP_ORIG variable.
42220 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
42221 to initialize the OMP_PRIV artificial variable and there is seventh
42222 statement, a DECL_EXPR of the OMP_PRIV statement again. */
42223
42224 static bool
42225 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
42226 {
42227 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
42228 gcc_assert (TYPE_REF_P (type));
42229 type = TREE_TYPE (type);
42230 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
42231 DECL_ARTIFICIAL (omp_out) = 1;
42232 pushdecl (omp_out);
42233 add_decl_expr (omp_out);
42234 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
42235 DECL_ARTIFICIAL (omp_in) = 1;
42236 pushdecl (omp_in);
42237 add_decl_expr (omp_in);
42238 tree combiner;
42239 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
42240
42241 keep_next_level (true);
42242 tree block = begin_omp_structured_block ();
42243 combiner = cp_parser_expression (parser);
42244 finish_expr_stmt (combiner);
42245 block = finish_omp_structured_block (block);
42246 if (processing_template_decl)
42247 block = build_stmt (input_location, EXPR_STMT, block);
42248 add_stmt (block);
42249
42250 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
42251 return false;
42252
42253 const char *p = "";
42254 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42255 {
42256 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42257 p = IDENTIFIER_POINTER (id);
42258 }
42259
42260 if (strcmp (p, "initializer") == 0)
42261 {
42262 cp_lexer_consume_token (parser->lexer);
42263 matching_parens parens;
42264 if (!parens.require_open (parser))
42265 return false;
42266
42267 p = "";
42268 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42269 {
42270 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42271 p = IDENTIFIER_POINTER (id);
42272 }
42273
42274 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
42275 DECL_ARTIFICIAL (omp_priv) = 1;
42276 pushdecl (omp_priv);
42277 add_decl_expr (omp_priv);
42278 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
42279 DECL_ARTIFICIAL (omp_orig) = 1;
42280 pushdecl (omp_orig);
42281 add_decl_expr (omp_orig);
42282
42283 keep_next_level (true);
42284 block = begin_omp_structured_block ();
42285
42286 bool ctor = false;
42287 if (strcmp (p, "omp_priv") == 0)
42288 {
42289 bool is_direct_init, is_non_constant_init;
42290 ctor = true;
42291 cp_lexer_consume_token (parser->lexer);
42292 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
42293 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
42294 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
42295 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
42296 == CPP_CLOSE_PAREN
42297 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
42298 == CPP_CLOSE_PAREN))
42299 {
42300 finish_omp_structured_block (block);
42301 error ("invalid initializer clause");
42302 return false;
42303 }
42304 initializer = cp_parser_initializer (parser, &is_direct_init,
42305 &is_non_constant_init);
42306 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
42307 NULL_TREE, LOOKUP_ONLYCONVERTING);
42308 }
42309 else
42310 {
42311 cp_parser_parse_tentatively (parser);
42312 /* Don't create location wrapper nodes here. */
42313 auto_suppress_location_wrappers sentinel;
42314 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
42315 /*check_dependency_p=*/true,
42316 /*template_p=*/NULL,
42317 /*declarator_p=*/false,
42318 /*optional_p=*/false);
42319 vec<tree, va_gc> *args;
42320 if (fn_name == error_mark_node
42321 || cp_parser_error_occurred (parser)
42322 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
42323 || ((args = cp_parser_parenthesized_expression_list
42324 (parser, non_attr, /*cast_p=*/false,
42325 /*allow_expansion_p=*/true,
42326 /*non_constant_p=*/NULL)),
42327 cp_parser_error_occurred (parser)))
42328 {
42329 finish_omp_structured_block (block);
42330 cp_parser_abort_tentative_parse (parser);
42331 cp_parser_error (parser, "expected id-expression (arguments)");
42332 return false;
42333 }
42334 unsigned int i;
42335 tree arg;
42336 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
42337 if (arg == omp_priv
42338 || (TREE_CODE (arg) == ADDR_EXPR
42339 && TREE_OPERAND (arg, 0) == omp_priv))
42340 break;
42341 cp_parser_abort_tentative_parse (parser);
42342 if (arg == NULL_TREE)
42343 error ("one of the initializer call arguments should be %<omp_priv%>"
42344 " or %<&omp_priv%>");
42345 initializer = cp_parser_postfix_expression (parser, false, false, false,
42346 false, NULL);
42347 finish_expr_stmt (initializer);
42348 }
42349
42350 block = finish_omp_structured_block (block);
42351 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
42352 if (processing_template_decl)
42353 block = build_stmt (input_location, EXPR_STMT, block);
42354 add_stmt (block);
42355
42356 if (ctor)
42357 add_decl_expr (omp_orig);
42358
42359 if (!parens.require_close (parser))
42360 return false;
42361 }
42362
42363 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
42364 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
42365 UNKNOWN_LOCATION);
42366
42367 return true;
42368 }
42369
42370 /* OpenMP 4.0
42371 #pragma omp declare reduction (reduction-id : typename-list : expression) \
42372 initializer-clause[opt] new-line
42373
42374 initializer-clause:
42375 initializer (omp_priv initializer)
42376 initializer (function-name (argument-list)) */
42377
42378 static void
42379 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
42380 enum pragma_context)
42381 {
42382 auto_vec<tree> types;
42383 enum tree_code reduc_code = ERROR_MARK;
42384 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
42385 unsigned int i;
42386 cp_token *first_token;
42387 cp_token_cache *cp;
42388 int errs;
42389 void *p;
42390
42391 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
42392 p = obstack_alloc (&declarator_obstack, 0);
42393
42394 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
42395 goto fail;
42396
42397 switch (cp_lexer_peek_token (parser->lexer)->type)
42398 {
42399 case CPP_PLUS:
42400 reduc_code = PLUS_EXPR;
42401 break;
42402 case CPP_MULT:
42403 reduc_code = MULT_EXPR;
42404 break;
42405 case CPP_MINUS:
42406 reduc_code = MINUS_EXPR;
42407 break;
42408 case CPP_AND:
42409 reduc_code = BIT_AND_EXPR;
42410 break;
42411 case CPP_XOR:
42412 reduc_code = BIT_XOR_EXPR;
42413 break;
42414 case CPP_OR:
42415 reduc_code = BIT_IOR_EXPR;
42416 break;
42417 case CPP_AND_AND:
42418 reduc_code = TRUTH_ANDIF_EXPR;
42419 break;
42420 case CPP_OR_OR:
42421 reduc_code = TRUTH_ORIF_EXPR;
42422 break;
42423 case CPP_NAME:
42424 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
42425 break;
42426 default:
42427 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
42428 "%<|%>, %<&&%>, %<||%> or identifier");
42429 goto fail;
42430 }
42431
42432 if (reduc_code != ERROR_MARK)
42433 cp_lexer_consume_token (parser->lexer);
42434
42435 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
42436 if (reduc_id == error_mark_node)
42437 goto fail;
42438
42439 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
42440 goto fail;
42441
42442 /* Types may not be defined in declare reduction type list. */
42443 const char *saved_message;
42444 saved_message = parser->type_definition_forbidden_message;
42445 parser->type_definition_forbidden_message
42446 = G_("types may not be defined in declare reduction type list");
42447 bool saved_colon_corrects_to_scope_p;
42448 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
42449 parser->colon_corrects_to_scope_p = false;
42450 bool saved_colon_doesnt_start_class_def_p;
42451 saved_colon_doesnt_start_class_def_p
42452 = parser->colon_doesnt_start_class_def_p;
42453 parser->colon_doesnt_start_class_def_p = true;
42454
42455 while (true)
42456 {
42457 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42458 type = cp_parser_type_id (parser);
42459 if (type == error_mark_node)
42460 ;
42461 else if (ARITHMETIC_TYPE_P (type)
42462 && (orig_reduc_id == NULL_TREE
42463 || (TREE_CODE (type) != COMPLEX_TYPE
42464 && (id_equal (orig_reduc_id, "min")
42465 || id_equal (orig_reduc_id, "max")))))
42466 error_at (loc, "predeclared arithmetic type %qT in "
42467 "%<#pragma omp declare reduction%>", type);
42468 else if (FUNC_OR_METHOD_TYPE_P (type)
42469 || TREE_CODE (type) == ARRAY_TYPE)
42470 error_at (loc, "function or array type %qT in "
42471 "%<#pragma omp declare reduction%>", type);
42472 else if (TYPE_REF_P (type))
42473 error_at (loc, "reference type %qT in "
42474 "%<#pragma omp declare reduction%>", type);
42475 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
42476 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
42477 "type %qT in %<#pragma omp declare reduction%>", type);
42478 else
42479 types.safe_push (type);
42480
42481 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42482 cp_lexer_consume_token (parser->lexer);
42483 else
42484 break;
42485 }
42486
42487 /* Restore the saved message. */
42488 parser->type_definition_forbidden_message = saved_message;
42489 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
42490 parser->colon_doesnt_start_class_def_p
42491 = saved_colon_doesnt_start_class_def_p;
42492
42493 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
42494 || types.is_empty ())
42495 {
42496 fail:
42497 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42498 goto done;
42499 }
42500
42501 first_token = cp_lexer_peek_token (parser->lexer);
42502 cp = NULL;
42503 errs = errorcount;
42504 FOR_EACH_VEC_ELT (types, i, type)
42505 {
42506 tree fntype
42507 = build_function_type_list (void_type_node,
42508 cp_build_reference_type (type, false),
42509 NULL_TREE);
42510 tree this_reduc_id = reduc_id;
42511 if (!dependent_type_p (type))
42512 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
42513 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
42514 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
42515 DECL_ARTIFICIAL (fndecl) = 1;
42516 DECL_EXTERNAL (fndecl) = 1;
42517 DECL_DECLARED_INLINE_P (fndecl) = 1;
42518 DECL_IGNORED_P (fndecl) = 1;
42519 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
42520 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
42521 DECL_ATTRIBUTES (fndecl)
42522 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
42523 DECL_ATTRIBUTES (fndecl));
42524 if (processing_template_decl)
42525 fndecl = push_template_decl (fndecl);
42526 bool block_scope = false;
42527 tree block = NULL_TREE;
42528 if (current_function_decl)
42529 {
42530 block_scope = true;
42531 DECL_CONTEXT (fndecl) = global_namespace;
42532 if (!processing_template_decl)
42533 pushdecl (fndecl);
42534 }
42535 else if (current_class_type)
42536 {
42537 if (cp == NULL)
42538 {
42539 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
42540 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
42541 cp_lexer_consume_token (parser->lexer);
42542 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42543 goto fail;
42544 cp = cp_token_cache_new (first_token,
42545 cp_lexer_peek_nth_token (parser->lexer,
42546 2));
42547 }
42548 DECL_STATIC_FUNCTION_P (fndecl) = 1;
42549 finish_member_declaration (fndecl);
42550 DECL_PENDING_INLINE_INFO (fndecl) = cp;
42551 DECL_PENDING_INLINE_P (fndecl) = 1;
42552 vec_safe_push (unparsed_funs_with_definitions, fndecl);
42553 continue;
42554 }
42555 else
42556 {
42557 DECL_CONTEXT (fndecl) = current_namespace;
42558 pushdecl (fndecl);
42559 }
42560 if (!block_scope)
42561 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
42562 else
42563 block = begin_omp_structured_block ();
42564 if (cp)
42565 {
42566 cp_parser_push_lexer_for_tokens (parser, cp);
42567 parser->lexer->in_pragma = true;
42568 }
42569 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
42570 {
42571 if (!block_scope)
42572 finish_function (/*inline_p=*/false);
42573 else
42574 DECL_CONTEXT (fndecl) = current_function_decl;
42575 if (cp)
42576 cp_parser_pop_lexer (parser);
42577 goto fail;
42578 }
42579 if (cp)
42580 cp_parser_pop_lexer (parser);
42581 if (!block_scope)
42582 finish_function (/*inline_p=*/false);
42583 else
42584 {
42585 DECL_CONTEXT (fndecl) = current_function_decl;
42586 block = finish_omp_structured_block (block);
42587 if (TREE_CODE (block) == BIND_EXPR)
42588 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
42589 else if (TREE_CODE (block) == STATEMENT_LIST)
42590 DECL_SAVED_TREE (fndecl) = block;
42591 if (processing_template_decl)
42592 add_decl_expr (fndecl);
42593 }
42594 cp_check_omp_declare_reduction (fndecl);
42595 if (cp == NULL && types.length () > 1)
42596 cp = cp_token_cache_new (first_token,
42597 cp_lexer_peek_nth_token (parser->lexer, 2));
42598 if (errs != errorcount)
42599 break;
42600 }
42601
42602 cp_parser_require_pragma_eol (parser, pragma_tok);
42603
42604 done:
42605 /* Free any declarators allocated. */
42606 obstack_free (&declarator_obstack, p);
42607 }
42608
42609 /* OpenMP 4.0
42610 #pragma omp declare simd declare-simd-clauses[optseq] new-line
42611 #pragma omp declare reduction (reduction-id : typename-list : expression) \
42612 initializer-clause[opt] new-line
42613 #pragma omp declare target new-line
42614
42615 OpenMP 5.0
42616 #pragma omp declare variant (identifier) match (context-selector) */
42617
42618 static bool
42619 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
42620 enum pragma_context context)
42621 {
42622 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42623 {
42624 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42625 const char *p = IDENTIFIER_POINTER (id);
42626
42627 if (strcmp (p, "simd") == 0)
42628 {
42629 cp_lexer_consume_token (parser->lexer);
42630 cp_parser_omp_declare_simd (parser, pragma_tok,
42631 context, false);
42632 return true;
42633 }
42634 if (flag_openmp && strcmp (p, "variant") == 0)
42635 {
42636 cp_lexer_consume_token (parser->lexer);
42637 cp_parser_omp_declare_simd (parser, pragma_tok,
42638 context, true);
42639 return true;
42640 }
42641 cp_ensure_no_omp_declare_simd (parser);
42642 if (strcmp (p, "reduction") == 0)
42643 {
42644 cp_lexer_consume_token (parser->lexer);
42645 cp_parser_omp_declare_reduction (parser, pragma_tok,
42646 context);
42647 return false;
42648 }
42649 if (!flag_openmp) /* flag_openmp_simd */
42650 {
42651 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42652 return false;
42653 }
42654 if (strcmp (p, "target") == 0)
42655 {
42656 cp_lexer_consume_token (parser->lexer);
42657 cp_parser_omp_declare_target (parser, pragma_tok);
42658 return false;
42659 }
42660 }
42661 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
42662 "%<target%> or %<variant%>");
42663 cp_parser_require_pragma_eol (parser, pragma_tok);
42664 return false;
42665 }
42666
42667 /* OpenMP 5.0
42668 #pragma omp requires clauses[optseq] new-line */
42669
42670 static bool
42671 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
42672 {
42673 bool first = true;
42674 enum omp_requires new_req = (enum omp_requires) 0;
42675
42676 location_t loc = pragma_tok->location;
42677 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42678 {
42679 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42680 cp_lexer_consume_token (parser->lexer);
42681
42682 first = false;
42683
42684 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42685 {
42686 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42687 const char *p = IDENTIFIER_POINTER (id);
42688 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
42689 enum omp_requires this_req = (enum omp_requires) 0;
42690
42691 if (!strcmp (p, "unified_address"))
42692 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
42693 else if (!strcmp (p, "unified_shared_memory"))
42694 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
42695 else if (!strcmp (p, "dynamic_allocators"))
42696 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
42697 else if (!strcmp (p, "reverse_offload"))
42698 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
42699 else if (!strcmp (p, "atomic_default_mem_order"))
42700 {
42701 cp_lexer_consume_token (parser->lexer);
42702
42703 matching_parens parens;
42704 if (parens.require_open (parser))
42705 {
42706 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42707 {
42708 id = cp_lexer_peek_token (parser->lexer)->u.value;
42709 p = IDENTIFIER_POINTER (id);
42710
42711 if (!strcmp (p, "seq_cst"))
42712 this_req
42713 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
42714 else if (!strcmp (p, "relaxed"))
42715 this_req
42716 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
42717 else if (!strcmp (p, "acq_rel"))
42718 this_req
42719 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
42720 }
42721 if (this_req == 0)
42722 {
42723 error_at (cp_lexer_peek_token (parser->lexer)->location,
42724 "expected %<seq_cst%>, %<relaxed%> or "
42725 "%<acq_rel%>");
42726 if (cp_lexer_nth_token_is (parser->lexer, 2,
42727 CPP_CLOSE_PAREN))
42728 cp_lexer_consume_token (parser->lexer);
42729 }
42730 else
42731 cp_lexer_consume_token (parser->lexer);
42732
42733 if (!parens.require_close (parser))
42734 cp_parser_skip_to_closing_parenthesis (parser,
42735 /*recovering=*/true,
42736 /*or_comma=*/false,
42737 /*consume_paren=*/
42738 true);
42739
42740 if (this_req == 0)
42741 {
42742 cp_parser_require_pragma_eol (parser, pragma_tok);
42743 return false;
42744 }
42745 }
42746 p = NULL;
42747 }
42748 else
42749 {
42750 error_at (cloc, "expected %<unified_address%>, "
42751 "%<unified_shared_memory%>, "
42752 "%<dynamic_allocators%>, "
42753 "%<reverse_offload%> "
42754 "or %<atomic_default_mem_order%> clause");
42755 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42756 return false;
42757 }
42758 if (p)
42759 sorry_at (cloc, "%qs clause on %<requires%> directive not "
42760 "supported yet", p);
42761 if (p)
42762 cp_lexer_consume_token (parser->lexer);
42763 if (this_req)
42764 {
42765 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
42766 {
42767 if ((this_req & new_req) != 0)
42768 error_at (cloc, "too many %qs clauses", p);
42769 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
42770 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
42771 error_at (cloc, "%qs clause used lexically after first "
42772 "target construct or offloading API", p);
42773 }
42774 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
42775 {
42776 error_at (cloc, "too many %qs clauses",
42777 "atomic_default_mem_order");
42778 this_req = (enum omp_requires) 0;
42779 }
42780 else if ((omp_requires_mask
42781 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
42782 {
42783 error_at (cloc, "more than one %<atomic_default_mem_order%>"
42784 " clause in a single compilation unit");
42785 this_req
42786 = (enum omp_requires)
42787 (omp_requires_mask
42788 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
42789 }
42790 else if ((omp_requires_mask
42791 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
42792 error_at (cloc, "%<atomic_default_mem_order%> clause used "
42793 "lexically after first %<atomic%> construct "
42794 "without memory order clause");
42795 new_req = (enum omp_requires) (new_req | this_req);
42796 omp_requires_mask
42797 = (enum omp_requires) (omp_requires_mask | this_req);
42798 continue;
42799 }
42800 }
42801 break;
42802 }
42803 cp_parser_require_pragma_eol (parser, pragma_tok);
42804
42805 if (new_req == 0)
42806 error_at (loc, "%<pragma omp requires%> requires at least one clause");
42807 return false;
42808 }
42809
42810
42811 /* OpenMP 4.5:
42812 #pragma omp taskloop taskloop-clause[optseq] new-line
42813 for-loop
42814
42815 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
42816 for-loop */
42817
42818 #define OMP_TASKLOOP_CLAUSE_MASK \
42819 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
42820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
42821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
42822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
42823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
42824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
42825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
42826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
42827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
42828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
42829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
42830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
42831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
42832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
42833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
42834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
42835
42836 static tree
42837 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
42838 char *p_name, omp_clause_mask mask, tree *cclauses,
42839 bool *if_p)
42840 {
42841 tree clauses, sb, ret;
42842 unsigned int save;
42843 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42844
42845 strcat (p_name, " taskloop");
42846 mask |= OMP_TASKLOOP_CLAUSE_MASK;
42847 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
42848 clause. */
42849 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
42850 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
42851
42852 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42853 {
42854 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42855 const char *p = IDENTIFIER_POINTER (id);
42856
42857 if (strcmp (p, "simd") == 0)
42858 {
42859 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
42860 if (cclauses == NULL)
42861 cclauses = cclauses_buf;
42862
42863 cp_lexer_consume_token (parser->lexer);
42864 if (!flag_openmp) /* flag_openmp_simd */
42865 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
42866 cclauses, if_p);
42867 sb = begin_omp_structured_block ();
42868 save = cp_parser_begin_omp_structured_block (parser);
42869 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
42870 cclauses, if_p);
42871 cp_parser_end_omp_structured_block (parser, save);
42872 tree body = finish_omp_structured_block (sb);
42873 if (ret == NULL)
42874 return ret;
42875 ret = make_node (OMP_TASKLOOP);
42876 TREE_TYPE (ret) = void_type_node;
42877 OMP_FOR_BODY (ret) = body;
42878 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
42879 SET_EXPR_LOCATION (ret, loc);
42880 add_stmt (ret);
42881 return ret;
42882 }
42883 }
42884 if (!flag_openmp) /* flag_openmp_simd */
42885 {
42886 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42887 return NULL_TREE;
42888 }
42889
42890 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
42891 cclauses == NULL);
42892 if (cclauses)
42893 {
42894 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
42895 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
42896 }
42897
42898 keep_next_level (true);
42899 sb = begin_omp_structured_block ();
42900 save = cp_parser_begin_omp_structured_block (parser);
42901
42902 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
42903 if_p);
42904
42905 cp_parser_end_omp_structured_block (parser, save);
42906 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
42907
42908 return ret;
42909 }
42910
42911
42912 /* OpenACC 2.0:
42913 # pragma acc routine oacc-routine-clause[optseq] new-line
42914 function-definition
42915
42916 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
42917 */
42918
42919 #define OACC_ROUTINE_CLAUSE_MASK \
42920 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
42921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
42922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
42923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
42924
42925
42926 /* Parse the OpenACC routine pragma. This has an optional '( name )'
42927 component, which must resolve to a declared namespace-scope
42928 function. The clauses are either processed directly (for a named
42929 function), or defered until the immediatley following declaration
42930 is parsed. */
42931
42932 static void
42933 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
42934 enum pragma_context context)
42935 {
42936 gcc_checking_assert (context == pragma_external);
42937 /* The checking for "another pragma following this one" in the "no optional
42938 '( name )'" case makes sure that we dont re-enter. */
42939 gcc_checking_assert (parser->oacc_routine == NULL);
42940
42941 cp_oacc_routine_data data;
42942 data.error_seen = false;
42943 data.fndecl_seen = false;
42944 data.tokens = vNULL;
42945 data.clauses = NULL_TREE;
42946 data.loc = pragma_tok->location;
42947 /* It is safe to take the address of a local variable; it will only be
42948 used while this scope is live. */
42949 parser->oacc_routine = &data;
42950
42951 /* Look for optional '( name )'. */
42952 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
42953 {
42954 matching_parens parens;
42955 parens.consume_open (parser); /* '(' */
42956
42957 /* We parse the name as an id-expression. If it resolves to
42958 anything other than a non-overloaded function at namespace
42959 scope, it's an error. */
42960 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
42961 tree name = cp_parser_id_expression (parser,
42962 /*template_keyword_p=*/false,
42963 /*check_dependency_p=*/false,
42964 /*template_p=*/NULL,
42965 /*declarator_p=*/false,
42966 /*optional_p=*/false);
42967 tree decl = (identifier_p (name)
42968 ? cp_parser_lookup_name_simple (parser, name, name_loc)
42969 : name);
42970 if (name != error_mark_node && decl == error_mark_node)
42971 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
42972
42973 if (decl == error_mark_node
42974 || !parens.require_close (parser))
42975 {
42976 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42977 parser->oacc_routine = NULL;
42978 return;
42979 }
42980
42981 data.clauses
42982 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
42983 "#pragma acc routine",
42984 cp_lexer_peek_token (parser->lexer));
42985 /* The clauses are in reverse order; fix that to make later diagnostic
42986 emission easier. */
42987 data.clauses = nreverse (data.clauses);
42988
42989 if (decl && is_overloaded_fn (decl)
42990 && (TREE_CODE (decl) != FUNCTION_DECL
42991 || DECL_FUNCTION_TEMPLATE_P (decl)))
42992 {
42993 error_at (name_loc,
42994 "%<#pragma acc routine%> names a set of overloads");
42995 parser->oacc_routine = NULL;
42996 return;
42997 }
42998
42999 /* Perhaps we should use the same rule as declarations in different
43000 namespaces? */
43001 if (!DECL_NAMESPACE_SCOPE_P (decl))
43002 {
43003 error_at (name_loc,
43004 "%qD does not refer to a namespace scope function", decl);
43005 parser->oacc_routine = NULL;
43006 return;
43007 }
43008
43009 if (TREE_CODE (decl) != FUNCTION_DECL)
43010 {
43011 error_at (name_loc, "%qD does not refer to a function", decl);
43012 parser->oacc_routine = NULL;
43013 return;
43014 }
43015
43016 cp_finalize_oacc_routine (parser, decl, false);
43017 parser->oacc_routine = NULL;
43018 }
43019 else /* No optional '( name )'. */
43020 {
43021 /* Store away all pragma tokens. */
43022 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
43023 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
43024 cp_lexer_consume_token (parser->lexer);
43025 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
43026 parser->oacc_routine->error_seen = true;
43027 cp_parser_require_pragma_eol (parser, pragma_tok);
43028 struct cp_token_cache *cp
43029 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
43030 parser->oacc_routine->tokens.safe_push (cp);
43031
43032 /* Emit a helpful diagnostic if there's another pragma following this
43033 one. */
43034 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
43035 {
43036 cp_ensure_no_oacc_routine (parser);
43037 data.tokens.release ();
43038 /* ..., and then just keep going. */
43039 return;
43040 }
43041
43042 /* We only have to consider the pragma_external case here. */
43043 cp_parser_declaration (parser);
43044 if (parser->oacc_routine
43045 && !parser->oacc_routine->fndecl_seen)
43046 cp_ensure_no_oacc_routine (parser);
43047 else
43048 parser->oacc_routine = NULL;
43049 data.tokens.release ();
43050 }
43051 }
43052
43053 /* Finalize #pragma acc routine clauses after direct declarator has
43054 been parsed. */
43055
43056 static tree
43057 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
43058 {
43059 struct cp_token_cache *ce;
43060 cp_oacc_routine_data *data = parser->oacc_routine;
43061
43062 if (!data->error_seen && data->fndecl_seen)
43063 {
43064 error_at (data->loc,
43065 "%<#pragma acc routine%> not immediately followed by "
43066 "a single function declaration or definition");
43067 data->error_seen = true;
43068 }
43069 if (data->error_seen)
43070 return attrs;
43071
43072 gcc_checking_assert (data->tokens.length () == 1);
43073 ce = data->tokens[0];
43074
43075 cp_parser_push_lexer_for_tokens (parser, ce);
43076 parser->lexer->in_pragma = true;
43077 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
43078
43079 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
43080 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
43081 parser->oacc_routine->clauses
43082 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
43083 "#pragma acc routine", pragma_tok);
43084 /* The clauses are in reverse order; fix that to make later diagnostic
43085 emission easier. */
43086 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
43087 cp_parser_pop_lexer (parser);
43088 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
43089 fndecl_seen. */
43090
43091 return attrs;
43092 }
43093
43094 /* Apply any saved OpenACC routine clauses to a just-parsed
43095 declaration. */
43096
43097 static void
43098 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
43099 {
43100 if (__builtin_expect (parser->oacc_routine != NULL, 0))
43101 {
43102 /* Keep going if we're in error reporting mode. */
43103 if (parser->oacc_routine->error_seen
43104 || fndecl == error_mark_node)
43105 return;
43106
43107 if (parser->oacc_routine->fndecl_seen)
43108 {
43109 error_at (parser->oacc_routine->loc,
43110 "%<#pragma acc routine%> not immediately followed by"
43111 " a single function declaration or definition");
43112 parser->oacc_routine = NULL;
43113 return;
43114 }
43115 if (TREE_CODE (fndecl) != FUNCTION_DECL)
43116 {
43117 cp_ensure_no_oacc_routine (parser);
43118 return;
43119 }
43120
43121 int compatible
43122 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
43123 parser->oacc_routine->loc,
43124 "#pragma acc routine");
43125 if (compatible < 0)
43126 {
43127 parser->oacc_routine = NULL;
43128 return;
43129 }
43130 if (compatible > 0)
43131 {
43132 }
43133 else
43134 {
43135 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
43136 {
43137 error_at (parser->oacc_routine->loc,
43138 TREE_USED (fndecl)
43139 ? G_("%<#pragma acc routine%> must be applied before"
43140 " use")
43141 : G_("%<#pragma acc routine%> must be applied before"
43142 " definition"));
43143 parser->oacc_routine = NULL;
43144 return;
43145 }
43146
43147 /* Set the routine's level of parallelism. */
43148 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
43149 oacc_replace_fn_attrib (fndecl, dims);
43150
43151 /* Add an "omp declare target" attribute. */
43152 DECL_ATTRIBUTES (fndecl)
43153 = tree_cons (get_identifier ("omp declare target"),
43154 parser->oacc_routine->clauses,
43155 DECL_ATTRIBUTES (fndecl));
43156 }
43157
43158 /* Don't unset parser->oacc_routine here: we may still need it to
43159 diagnose wrong usage. But, remember that we've used this "#pragma acc
43160 routine". */
43161 parser->oacc_routine->fndecl_seen = true;
43162 }
43163 }
43164
43165 /* Main entry point to OpenMP statement pragmas. */
43166
43167 static void
43168 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43169 {
43170 tree stmt;
43171 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
43172 omp_clause_mask mask (0);
43173
43174 switch (cp_parser_pragma_kind (pragma_tok))
43175 {
43176 case PRAGMA_OACC_ATOMIC:
43177 cp_parser_omp_atomic (parser, pragma_tok);
43178 return;
43179 case PRAGMA_OACC_CACHE:
43180 stmt = cp_parser_oacc_cache (parser, pragma_tok);
43181 break;
43182 case PRAGMA_OACC_DATA:
43183 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
43184 break;
43185 case PRAGMA_OACC_ENTER_DATA:
43186 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
43187 break;
43188 case PRAGMA_OACC_EXIT_DATA:
43189 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
43190 break;
43191 case PRAGMA_OACC_HOST_DATA:
43192 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
43193 break;
43194 case PRAGMA_OACC_KERNELS:
43195 case PRAGMA_OACC_PARALLEL:
43196 case PRAGMA_OACC_SERIAL:
43197 strcpy (p_name, "#pragma acc");
43198 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
43199 break;
43200 case PRAGMA_OACC_LOOP:
43201 strcpy (p_name, "#pragma acc");
43202 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
43203 if_p);
43204 break;
43205 case PRAGMA_OACC_UPDATE:
43206 stmt = cp_parser_oacc_update (parser, pragma_tok);
43207 break;
43208 case PRAGMA_OACC_WAIT:
43209 stmt = cp_parser_oacc_wait (parser, pragma_tok);
43210 break;
43211 case PRAGMA_OMP_ATOMIC:
43212 cp_parser_omp_atomic (parser, pragma_tok);
43213 return;
43214 case PRAGMA_OMP_CRITICAL:
43215 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
43216 break;
43217 case PRAGMA_OMP_DISTRIBUTE:
43218 strcpy (p_name, "#pragma omp");
43219 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
43220 if_p);
43221 break;
43222 case PRAGMA_OMP_FOR:
43223 strcpy (p_name, "#pragma omp");
43224 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
43225 if_p);
43226 break;
43227 case PRAGMA_OMP_LOOP:
43228 strcpy (p_name, "#pragma omp");
43229 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
43230 if_p);
43231 break;
43232 case PRAGMA_OMP_MASTER:
43233 strcpy (p_name, "#pragma omp");
43234 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
43235 if_p);
43236 break;
43237 case PRAGMA_OMP_PARALLEL:
43238 strcpy (p_name, "#pragma omp");
43239 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
43240 if_p);
43241 break;
43242 case PRAGMA_OMP_SECTIONS:
43243 strcpy (p_name, "#pragma omp");
43244 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
43245 break;
43246 case PRAGMA_OMP_SIMD:
43247 strcpy (p_name, "#pragma omp");
43248 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
43249 if_p);
43250 break;
43251 case PRAGMA_OMP_SINGLE:
43252 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
43253 break;
43254 case PRAGMA_OMP_TASK:
43255 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
43256 break;
43257 case PRAGMA_OMP_TASKGROUP:
43258 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
43259 break;
43260 case PRAGMA_OMP_TASKLOOP:
43261 strcpy (p_name, "#pragma omp");
43262 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
43263 if_p);
43264 break;
43265 case PRAGMA_OMP_TEAMS:
43266 strcpy (p_name, "#pragma omp");
43267 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
43268 if_p);
43269 break;
43270 default:
43271 gcc_unreachable ();
43272 }
43273
43274 protected_set_expr_location (stmt, pragma_tok->location);
43275 }
43276 \f
43277 /* Transactional Memory parsing routines. */
43278
43279 /* Parse a transaction attribute.
43280
43281 txn-attribute:
43282 attribute
43283 [ [ identifier ] ]
43284
43285 We use this instead of cp_parser_attributes_opt for transactions to avoid
43286 the pedwarn in C++98 mode. */
43287
43288 static tree
43289 cp_parser_txn_attribute_opt (cp_parser *parser)
43290 {
43291 cp_token *token;
43292 tree attr_name, attr = NULL;
43293
43294 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
43295 return cp_parser_attributes_opt (parser);
43296
43297 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
43298 return NULL_TREE;
43299 cp_lexer_consume_token (parser->lexer);
43300 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
43301 goto error1;
43302
43303 token = cp_lexer_peek_token (parser->lexer);
43304 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
43305 {
43306 token = cp_lexer_consume_token (parser->lexer);
43307
43308 attr_name = (token->type == CPP_KEYWORD
43309 /* For keywords, use the canonical spelling,
43310 not the parsed identifier. */
43311 ? ridpointers[(int) token->keyword]
43312 : token->u.value);
43313 attr = build_tree_list (attr_name, NULL_TREE);
43314 }
43315 else
43316 cp_parser_error (parser, "expected identifier");
43317
43318 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
43319 error1:
43320 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
43321 return attr;
43322 }
43323
43324 /* Parse a __transaction_atomic or __transaction_relaxed statement.
43325
43326 transaction-statement:
43327 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
43328 compound-statement
43329 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
43330 */
43331
43332 static tree
43333 cp_parser_transaction (cp_parser *parser, cp_token *token)
43334 {
43335 unsigned char old_in = parser->in_transaction;
43336 unsigned char this_in = 1, new_in;
43337 enum rid keyword = token->keyword;
43338 tree stmt, attrs, noex;
43339
43340 cp_lexer_consume_token (parser->lexer);
43341
43342 if (keyword == RID_TRANSACTION_RELAXED
43343 || keyword == RID_SYNCHRONIZED)
43344 this_in |= TM_STMT_ATTR_RELAXED;
43345 else
43346 {
43347 attrs = cp_parser_txn_attribute_opt (parser);
43348 if (attrs)
43349 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
43350 }
43351
43352 /* Parse a noexcept specification. */
43353 if (keyword == RID_ATOMIC_NOEXCEPT)
43354 noex = boolean_true_node;
43355 else if (keyword == RID_ATOMIC_CANCEL)
43356 {
43357 /* cancel-and-throw is unimplemented. */
43358 sorry ("%<atomic_cancel%>");
43359 noex = NULL_TREE;
43360 }
43361 else
43362 noex = cp_parser_noexcept_specification_opt (parser,
43363 CP_PARSER_FLAGS_NONE,
43364 /*require_constexpr=*/true,
43365 /*consumed_expr=*/NULL,
43366 /*return_cond=*/true);
43367
43368 /* Keep track if we're in the lexical scope of an outer transaction. */
43369 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
43370
43371 stmt = begin_transaction_stmt (token->location, NULL, this_in);
43372
43373 parser->in_transaction = new_in;
43374 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
43375 parser->in_transaction = old_in;
43376
43377 finish_transaction_stmt (stmt, NULL, this_in, noex);
43378
43379 return stmt;
43380 }
43381
43382 /* Parse a __transaction_atomic or __transaction_relaxed expression.
43383
43384 transaction-expression:
43385 __transaction_atomic txn-noexcept-spec[opt] ( expression )
43386 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
43387 */
43388
43389 static tree
43390 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
43391 {
43392 unsigned char old_in = parser->in_transaction;
43393 unsigned char this_in = 1;
43394 cp_token *token;
43395 tree expr, noex;
43396 bool noex_expr;
43397 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43398
43399 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
43400 || keyword == RID_TRANSACTION_RELAXED);
43401
43402 if (!flag_tm)
43403 error_at (loc,
43404 keyword == RID_TRANSACTION_RELAXED
43405 ? G_("%<__transaction_relaxed%> without transactional memory "
43406 "support enabled")
43407 : G_("%<__transaction_atomic%> without transactional memory "
43408 "support enabled"));
43409
43410 token = cp_parser_require_keyword (parser, keyword,
43411 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
43412 : RT_TRANSACTION_RELAXED));
43413 gcc_assert (token != NULL);
43414
43415 if (keyword == RID_TRANSACTION_RELAXED)
43416 this_in |= TM_STMT_ATTR_RELAXED;
43417
43418 /* Set this early. This might mean that we allow transaction_cancel in
43419 an expression that we find out later actually has to be a constexpr.
43420 However, we expect that cxx_constant_value will be able to deal with
43421 this; also, if the noexcept has no constexpr, then what we parse next
43422 really is a transaction's body. */
43423 parser->in_transaction = this_in;
43424
43425 /* Parse a noexcept specification. */
43426 noex = cp_parser_noexcept_specification_opt (parser,
43427 CP_PARSER_FLAGS_NONE,
43428 /*require_constexpr=*/false,
43429 &noex_expr,
43430 /*return_cond=*/true);
43431
43432 if (!noex || !noex_expr
43433 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
43434 {
43435 matching_parens parens;
43436 parens.require_open (parser);
43437
43438 expr = cp_parser_expression (parser);
43439 expr = finish_parenthesized_expr (expr);
43440
43441 parens.require_close (parser);
43442 }
43443 else
43444 {
43445 /* The only expression that is available got parsed for the noexcept
43446 already. noexcept is true then. */
43447 expr = noex;
43448 noex = boolean_true_node;
43449 }
43450
43451 expr = build_transaction_expr (token->location, expr, this_in, noex);
43452 parser->in_transaction = old_in;
43453
43454 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
43455 return error_mark_node;
43456
43457 return (flag_tm ? expr : error_mark_node);
43458 }
43459
43460 /* Parse a function-transaction-block.
43461
43462 function-transaction-block:
43463 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
43464 function-body
43465 __transaction_atomic txn-attribute[opt] function-try-block
43466 __transaction_relaxed ctor-initializer[opt] function-body
43467 __transaction_relaxed function-try-block
43468 */
43469
43470 static void
43471 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
43472 {
43473 unsigned char old_in = parser->in_transaction;
43474 unsigned char new_in = 1;
43475 tree compound_stmt, stmt, attrs;
43476 cp_token *token;
43477
43478 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
43479 || keyword == RID_TRANSACTION_RELAXED);
43480 token = cp_parser_require_keyword (parser, keyword,
43481 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
43482 : RT_TRANSACTION_RELAXED));
43483 gcc_assert (token != NULL);
43484
43485 if (keyword == RID_TRANSACTION_RELAXED)
43486 new_in |= TM_STMT_ATTR_RELAXED;
43487 else
43488 {
43489 attrs = cp_parser_txn_attribute_opt (parser);
43490 if (attrs)
43491 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
43492 }
43493
43494 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
43495
43496 parser->in_transaction = new_in;
43497
43498 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
43499 cp_parser_function_try_block (parser);
43500 else
43501 cp_parser_ctor_initializer_opt_and_function_body
43502 (parser, /*in_function_try_block=*/false);
43503
43504 parser->in_transaction = old_in;
43505
43506 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
43507 }
43508
43509 /* Parse a __transaction_cancel statement.
43510
43511 cancel-statement:
43512 __transaction_cancel txn-attribute[opt] ;
43513 __transaction_cancel txn-attribute[opt] throw-expression ;
43514
43515 ??? Cancel and throw is not yet implemented. */
43516
43517 static tree
43518 cp_parser_transaction_cancel (cp_parser *parser)
43519 {
43520 cp_token *token;
43521 bool is_outer = false;
43522 tree stmt, attrs;
43523
43524 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
43525 RT_TRANSACTION_CANCEL);
43526 gcc_assert (token != NULL);
43527
43528 attrs = cp_parser_txn_attribute_opt (parser);
43529 if (attrs)
43530 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
43531
43532 /* ??? Parse cancel-and-throw here. */
43533
43534 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
43535
43536 if (!flag_tm)
43537 {
43538 error_at (token->location, "%<__transaction_cancel%> without "
43539 "transactional memory support enabled");
43540 return error_mark_node;
43541 }
43542 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
43543 {
43544 error_at (token->location, "%<__transaction_cancel%> within a "
43545 "%<__transaction_relaxed%>");
43546 return error_mark_node;
43547 }
43548 else if (is_outer)
43549 {
43550 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
43551 && !is_tm_may_cancel_outer (current_function_decl))
43552 {
43553 error_at (token->location, "outer %<__transaction_cancel%> not "
43554 "within outer %<__transaction_atomic%>");
43555 error_at (token->location,
43556 " or a %<transaction_may_cancel_outer%> function");
43557 return error_mark_node;
43558 }
43559 }
43560 else if (parser->in_transaction == 0)
43561 {
43562 error_at (token->location, "%<__transaction_cancel%> not within "
43563 "%<__transaction_atomic%>");
43564 return error_mark_node;
43565 }
43566
43567 stmt = build_tm_abort_call (token->location, is_outer);
43568 add_stmt (stmt);
43569
43570 return stmt;
43571 }
43572 \f
43573 /* The parser. */
43574
43575 static GTY (()) cp_parser *the_parser;
43576
43577 \f
43578 /* Special handling for the first token or line in the file. The first
43579 thing in the file might be #pragma GCC pch_preprocess, which loads a
43580 PCH file, which is a GC collection point. So we need to handle this
43581 first pragma without benefit of an existing lexer structure.
43582
43583 Always returns one token to the caller in *FIRST_TOKEN. This is
43584 either the true first token of the file, or the first token after
43585 the initial pragma. */
43586
43587 static void
43588 cp_parser_initial_pragma (cp_token *first_token)
43589 {
43590 tree name = NULL;
43591
43592 cp_lexer_get_preprocessor_token (NULL, first_token);
43593 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
43594 {
43595 c_common_no_more_pch ();
43596 return;
43597 }
43598
43599 cp_lexer_get_preprocessor_token (NULL, first_token);
43600 if (first_token->type == CPP_STRING)
43601 {
43602 name = first_token->u.value;
43603
43604 cp_lexer_get_preprocessor_token (NULL, first_token);
43605 if (first_token->type != CPP_PRAGMA_EOL)
43606 error_at (first_token->location,
43607 "junk at end of %<#pragma GCC pch_preprocess%>");
43608 }
43609 else
43610 error_at (first_token->location, "expected string literal");
43611
43612 /* Skip to the end of the pragma. */
43613 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
43614 cp_lexer_get_preprocessor_token (NULL, first_token);
43615
43616 /* Now actually load the PCH file. */
43617 if (name)
43618 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
43619
43620 /* Read one more token to return to our caller. We have to do this
43621 after reading the PCH file in, since its pointers have to be
43622 live. */
43623 cp_lexer_get_preprocessor_token (NULL, first_token);
43624 }
43625
43626 /* Parse a pragma GCC ivdep. */
43627
43628 static bool
43629 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
43630 {
43631 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43632 return true;
43633 }
43634
43635 /* Parse a pragma GCC unroll. */
43636
43637 static unsigned short
43638 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
43639 {
43640 location_t location = cp_lexer_peek_token (parser->lexer)->location;
43641 tree expr = cp_parser_constant_expression (parser);
43642 unsigned short unroll;
43643 expr = maybe_constant_value (expr);
43644 HOST_WIDE_INT lunroll = 0;
43645 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
43646 || TREE_CODE (expr) != INTEGER_CST
43647 || (lunroll = tree_to_shwi (expr)) < 0
43648 || lunroll >= USHRT_MAX)
43649 {
43650 error_at (location, "%<#pragma GCC unroll%> requires an"
43651 " assignment-expression that evaluates to a non-negative"
43652 " integral constant less than %u", USHRT_MAX);
43653 unroll = 0;
43654 }
43655 else
43656 {
43657 unroll = (unsigned short)lunroll;
43658 if (unroll == 0)
43659 unroll = 1;
43660 }
43661 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43662 return unroll;
43663 }
43664
43665 /* Normal parsing of a pragma token. Here we can (and must) use the
43666 regular lexer. */
43667
43668 static bool
43669 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
43670 {
43671 cp_token *pragma_tok;
43672 unsigned int id;
43673 tree stmt;
43674 bool ret;
43675
43676 pragma_tok = cp_lexer_consume_token (parser->lexer);
43677 gcc_assert (pragma_tok->type == CPP_PRAGMA);
43678 parser->lexer->in_pragma = true;
43679
43680 id = cp_parser_pragma_kind (pragma_tok);
43681 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
43682 cp_ensure_no_omp_declare_simd (parser);
43683 switch (id)
43684 {
43685 case PRAGMA_GCC_PCH_PREPROCESS:
43686 error_at (pragma_tok->location,
43687 "%<#pragma GCC pch_preprocess%> must be first");
43688 break;
43689
43690 case PRAGMA_OMP_BARRIER:
43691 switch (context)
43692 {
43693 case pragma_compound:
43694 cp_parser_omp_barrier (parser, pragma_tok);
43695 return false;
43696 case pragma_stmt:
43697 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
43698 "used in compound statements", "omp barrier");
43699 break;
43700 default:
43701 goto bad_stmt;
43702 }
43703 break;
43704
43705 case PRAGMA_OMP_DEPOBJ:
43706 switch (context)
43707 {
43708 case pragma_compound:
43709 cp_parser_omp_depobj (parser, pragma_tok);
43710 return false;
43711 case pragma_stmt:
43712 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
43713 "used in compound statements", "omp depobj");
43714 break;
43715 default:
43716 goto bad_stmt;
43717 }
43718 break;
43719
43720 case PRAGMA_OMP_FLUSH:
43721 switch (context)
43722 {
43723 case pragma_compound:
43724 cp_parser_omp_flush (parser, pragma_tok);
43725 return false;
43726 case pragma_stmt:
43727 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
43728 "used in compound statements", "omp flush");
43729 break;
43730 default:
43731 goto bad_stmt;
43732 }
43733 break;
43734
43735 case PRAGMA_OMP_TASKWAIT:
43736 switch (context)
43737 {
43738 case pragma_compound:
43739 cp_parser_omp_taskwait (parser, pragma_tok);
43740 return false;
43741 case pragma_stmt:
43742 error_at (pragma_tok->location,
43743 "%<#pragma %s%> may only be used in compound statements",
43744 "omp taskwait");
43745 break;
43746 default:
43747 goto bad_stmt;
43748 }
43749 break;
43750
43751 case PRAGMA_OMP_TASKYIELD:
43752 switch (context)
43753 {
43754 case pragma_compound:
43755 cp_parser_omp_taskyield (parser, pragma_tok);
43756 return false;
43757 case pragma_stmt:
43758 error_at (pragma_tok->location,
43759 "%<#pragma %s%> may only be used in compound statements",
43760 "omp taskyield");
43761 break;
43762 default:
43763 goto bad_stmt;
43764 }
43765 break;
43766
43767 case PRAGMA_OMP_CANCEL:
43768 switch (context)
43769 {
43770 case pragma_compound:
43771 cp_parser_omp_cancel (parser, pragma_tok);
43772 return false;
43773 case pragma_stmt:
43774 error_at (pragma_tok->location,
43775 "%<#pragma %s%> may only be used in compound statements",
43776 "omp cancel");
43777 break;
43778 default:
43779 goto bad_stmt;
43780 }
43781 break;
43782
43783 case PRAGMA_OMP_CANCELLATION_POINT:
43784 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
43785 return false;
43786
43787 case PRAGMA_OMP_THREADPRIVATE:
43788 cp_parser_omp_threadprivate (parser, pragma_tok);
43789 return false;
43790
43791 case PRAGMA_OMP_DECLARE:
43792 return cp_parser_omp_declare (parser, pragma_tok, context);
43793
43794 case PRAGMA_OACC_DECLARE:
43795 cp_parser_oacc_declare (parser, pragma_tok);
43796 return false;
43797
43798 case PRAGMA_OACC_ENTER_DATA:
43799 if (context == pragma_stmt)
43800 {
43801 error_at (pragma_tok->location,
43802 "%<#pragma %s%> may only be used in compound statements",
43803 "acc enter data");
43804 break;
43805 }
43806 else if (context != pragma_compound)
43807 goto bad_stmt;
43808 cp_parser_omp_construct (parser, pragma_tok, if_p);
43809 return true;
43810
43811 case PRAGMA_OACC_EXIT_DATA:
43812 if (context == pragma_stmt)
43813 {
43814 error_at (pragma_tok->location,
43815 "%<#pragma %s%> may only be used in compound statements",
43816 "acc exit data");
43817 break;
43818 }
43819 else if (context != pragma_compound)
43820 goto bad_stmt;
43821 cp_parser_omp_construct (parser, pragma_tok, if_p);
43822 return true;
43823
43824 case PRAGMA_OACC_ROUTINE:
43825 if (context != pragma_external)
43826 {
43827 error_at (pragma_tok->location,
43828 "%<#pragma acc routine%> must be at file scope");
43829 break;
43830 }
43831 cp_parser_oacc_routine (parser, pragma_tok, context);
43832 return false;
43833
43834 case PRAGMA_OACC_UPDATE:
43835 if (context == pragma_stmt)
43836 {
43837 error_at (pragma_tok->location,
43838 "%<#pragma %s%> may only be used in compound statements",
43839 "acc update");
43840 break;
43841 }
43842 else if (context != pragma_compound)
43843 goto bad_stmt;
43844 cp_parser_omp_construct (parser, pragma_tok, if_p);
43845 return true;
43846
43847 case PRAGMA_OACC_WAIT:
43848 if (context == pragma_stmt)
43849 {
43850 error_at (pragma_tok->location,
43851 "%<#pragma %s%> may only be used in compound statements",
43852 "acc wait");
43853 break;
43854 }
43855 else if (context != pragma_compound)
43856 goto bad_stmt;
43857 cp_parser_omp_construct (parser, pragma_tok, if_p);
43858 return true;
43859
43860 case PRAGMA_OACC_ATOMIC:
43861 case PRAGMA_OACC_CACHE:
43862 case PRAGMA_OACC_DATA:
43863 case PRAGMA_OACC_HOST_DATA:
43864 case PRAGMA_OACC_KERNELS:
43865 case PRAGMA_OACC_LOOP:
43866 case PRAGMA_OACC_PARALLEL:
43867 case PRAGMA_OACC_SERIAL:
43868 case PRAGMA_OMP_ATOMIC:
43869 case PRAGMA_OMP_CRITICAL:
43870 case PRAGMA_OMP_DISTRIBUTE:
43871 case PRAGMA_OMP_FOR:
43872 case PRAGMA_OMP_LOOP:
43873 case PRAGMA_OMP_MASTER:
43874 case PRAGMA_OMP_PARALLEL:
43875 case PRAGMA_OMP_SECTIONS:
43876 case PRAGMA_OMP_SIMD:
43877 case PRAGMA_OMP_SINGLE:
43878 case PRAGMA_OMP_TASK:
43879 case PRAGMA_OMP_TASKGROUP:
43880 case PRAGMA_OMP_TASKLOOP:
43881 case PRAGMA_OMP_TEAMS:
43882 if (context != pragma_stmt && context != pragma_compound)
43883 goto bad_stmt;
43884 stmt = push_omp_privatization_clauses (false);
43885 cp_parser_omp_construct (parser, pragma_tok, if_p);
43886 pop_omp_privatization_clauses (stmt);
43887 return true;
43888
43889 case PRAGMA_OMP_REQUIRES:
43890 if (context != pragma_external)
43891 {
43892 error_at (pragma_tok->location,
43893 "%<#pragma omp requires%> may only be used at file or "
43894 "namespace scope");
43895 break;
43896 }
43897 return cp_parser_omp_requires (parser, pragma_tok);
43898
43899 case PRAGMA_OMP_ORDERED:
43900 if (context != pragma_stmt && context != pragma_compound)
43901 goto bad_stmt;
43902 stmt = push_omp_privatization_clauses (false);
43903 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
43904 pop_omp_privatization_clauses (stmt);
43905 return ret;
43906
43907 case PRAGMA_OMP_TARGET:
43908 if (context != pragma_stmt && context != pragma_compound)
43909 goto bad_stmt;
43910 stmt = push_omp_privatization_clauses (false);
43911 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
43912 pop_omp_privatization_clauses (stmt);
43913 return ret;
43914
43915 case PRAGMA_OMP_END_DECLARE_TARGET:
43916 cp_parser_omp_end_declare_target (parser, pragma_tok);
43917 return false;
43918
43919 case PRAGMA_OMP_SCAN:
43920 error_at (pragma_tok->location,
43921 "%<#pragma omp scan%> may only be used in "
43922 "a loop construct with %<inscan%> %<reduction%> clause");
43923 break;
43924
43925 case PRAGMA_OMP_SECTION:
43926 error_at (pragma_tok->location,
43927 "%<#pragma omp section%> may only be used in "
43928 "%<#pragma omp sections%> construct");
43929 break;
43930
43931 case PRAGMA_IVDEP:
43932 {
43933 if (context == pragma_external)
43934 {
43935 error_at (pragma_tok->location,
43936 "%<#pragma GCC ivdep%> must be inside a function");
43937 break;
43938 }
43939 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
43940 unsigned short unroll;
43941 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
43942 if (tok->type == CPP_PRAGMA
43943 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
43944 {
43945 tok = cp_lexer_consume_token (parser->lexer);
43946 unroll = cp_parser_pragma_unroll (parser, tok);
43947 tok = cp_lexer_peek_token (the_parser->lexer);
43948 }
43949 else
43950 unroll = 0;
43951 if (tok->type != CPP_KEYWORD
43952 || (tok->keyword != RID_FOR
43953 && tok->keyword != RID_WHILE
43954 && tok->keyword != RID_DO))
43955 {
43956 cp_parser_error (parser, "for, while or do statement expected");
43957 return false;
43958 }
43959 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
43960 return true;
43961 }
43962
43963 case PRAGMA_UNROLL:
43964 {
43965 if (context == pragma_external)
43966 {
43967 error_at (pragma_tok->location,
43968 "%<#pragma GCC unroll%> must be inside a function");
43969 break;
43970 }
43971 const unsigned short unroll
43972 = cp_parser_pragma_unroll (parser, pragma_tok);
43973 bool ivdep;
43974 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
43975 if (tok->type == CPP_PRAGMA
43976 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
43977 {
43978 tok = cp_lexer_consume_token (parser->lexer);
43979 ivdep = cp_parser_pragma_ivdep (parser, tok);
43980 tok = cp_lexer_peek_token (the_parser->lexer);
43981 }
43982 else
43983 ivdep = false;
43984 if (tok->type != CPP_KEYWORD
43985 || (tok->keyword != RID_FOR
43986 && tok->keyword != RID_WHILE
43987 && tok->keyword != RID_DO))
43988 {
43989 cp_parser_error (parser, "for, while or do statement expected");
43990 return false;
43991 }
43992 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
43993 return true;
43994 }
43995
43996 default:
43997 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
43998 c_invoke_pragma_handler (id);
43999 break;
44000
44001 bad_stmt:
44002 cp_parser_error (parser, "expected declaration specifiers");
44003 break;
44004 }
44005
44006 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44007 return false;
44008 }
44009
44010 /* The interface the pragma parsers have to the lexer. */
44011
44012 enum cpp_ttype
44013 pragma_lex (tree *value, location_t *loc)
44014 {
44015 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
44016 enum cpp_ttype ret = tok->type;
44017
44018 *value = tok->u.value;
44019 if (loc)
44020 *loc = tok->location;
44021
44022 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
44023 ret = CPP_EOF;
44024 else if (ret == CPP_STRING)
44025 *value = cp_parser_string_literal (the_parser, false, false);
44026 else
44027 {
44028 if (ret == CPP_KEYWORD)
44029 ret = CPP_NAME;
44030 cp_lexer_consume_token (the_parser->lexer);
44031 }
44032
44033 return ret;
44034 }
44035
44036 \f
44037 /* External interface. */
44038
44039 /* Parse one entire translation unit. */
44040
44041 void
44042 c_parse_file (void)
44043 {
44044 static bool already_called = false;
44045
44046 if (already_called)
44047 fatal_error (input_location,
44048 "inter-module optimizations not implemented for C++");
44049 already_called = true;
44050
44051 the_parser = cp_parser_new ();
44052 push_deferring_access_checks (flag_access_control
44053 ? dk_no_deferred : dk_no_check);
44054 cp_parser_translation_unit (the_parser);
44055 class_decl_loc_t::diag_mismatched_tags ();
44056
44057 the_parser = NULL;
44058
44059 finish_translation_unit ();
44060 }
44061
44062 /* Create an identifier for a generic parameter type (a synthesized
44063 template parameter implied by `auto' or a concept identifier). */
44064
44065 static GTY(()) int generic_parm_count;
44066 static tree
44067 make_generic_type_name ()
44068 {
44069 char buf[32];
44070 sprintf (buf, "auto:%d", ++generic_parm_count);
44071 return get_identifier (buf);
44072 }
44073
44074 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
44075 (creating a new template parameter list if necessary). Returns the newly
44076 created template type parm. */
44077
44078 static tree
44079 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
44080 {
44081 /* A requires-clause is not a function and cannot have placeholders. */
44082 if (current_binding_level->kind == sk_block)
44083 {
44084 error ("placeholder type not allowed in this context");
44085 return error_mark_node;
44086 }
44087
44088 gcc_assert (current_binding_level->kind == sk_function_parms);
44089
44090 /* We are either continuing a function template that already contains implicit
44091 template parameters, creating a new fully-implicit function template, or
44092 extending an existing explicit function template with implicit template
44093 parameters. */
44094
44095 cp_binding_level *const entry_scope = current_binding_level;
44096
44097 bool become_template = false;
44098 cp_binding_level *parent_scope = 0;
44099
44100 if (parser->implicit_template_scope)
44101 {
44102 gcc_assert (parser->implicit_template_parms);
44103
44104 current_binding_level = parser->implicit_template_scope;
44105 }
44106 else
44107 {
44108 /* Roll back to the existing template parameter scope (in the case of
44109 extending an explicit function template) or introduce a new template
44110 parameter scope ahead of the function parameter scope (or class scope
44111 in the case of out-of-line member definitions). The function scope is
44112 added back after template parameter synthesis below. */
44113
44114 cp_binding_level *scope = entry_scope;
44115
44116 while (scope->kind == sk_function_parms)
44117 {
44118 parent_scope = scope;
44119 scope = scope->level_chain;
44120 }
44121 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
44122 {
44123 /* If not defining a class, then any class scope is a scope level in
44124 an out-of-line member definition. In this case simply wind back
44125 beyond the first such scope to inject the template parameter list.
44126 Otherwise wind back to the class being defined. The latter can
44127 occur in class member friend declarations such as:
44128
44129 class A {
44130 void foo (auto);
44131 };
44132 class B {
44133 friend void A::foo (auto);
44134 };
44135
44136 The template parameter list synthesized for the friend declaration
44137 must be injected in the scope of 'B'. This can also occur in
44138 erroneous cases such as:
44139
44140 struct A {
44141 struct B {
44142 void foo (auto);
44143 };
44144 void B::foo (auto) {}
44145 };
44146
44147 Here the attempted definition of 'B::foo' within 'A' is ill-formed
44148 but, nevertheless, the template parameter list synthesized for the
44149 declarator should be injected into the scope of 'A' as if the
44150 ill-formed template was specified explicitly. */
44151
44152 while (scope->kind == sk_class && !scope->defining_class_p)
44153 {
44154 parent_scope = scope;
44155 scope = scope->level_chain;
44156 }
44157 }
44158
44159 current_binding_level = scope;
44160
44161 if (scope->kind != sk_template_parms
44162 || !function_being_declared_is_template_p (parser))
44163 {
44164 /* Introduce a new template parameter list for implicit template
44165 parameters. */
44166
44167 become_template = true;
44168
44169 parser->implicit_template_scope
44170 = begin_scope (sk_template_parms, NULL);
44171
44172 ++processing_template_decl;
44173
44174 parser->fully_implicit_function_template_p = true;
44175 ++parser->num_template_parameter_lists;
44176 }
44177 else
44178 {
44179 /* Synthesize implicit template parameters at the end of the explicit
44180 template parameter list. */
44181
44182 gcc_assert (current_template_parms);
44183
44184 parser->implicit_template_scope = scope;
44185
44186 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
44187 parser->implicit_template_parms
44188 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
44189 }
44190 }
44191
44192 /* Synthesize a new template parameter and track the current template
44193 parameter chain with implicit_template_parms. */
44194
44195 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
44196 tree synth_id = make_generic_type_name ();
44197 tree synth_tmpl_parm;
44198 bool non_type = false;
44199
44200 /* Synthesize the type template parameter. */
44201 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
44202 synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
44203
44204 /* Attach the constraint to the parm before processing. */
44205 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
44206 TREE_TYPE (node) = constr;
44207 tree new_parm
44208 = process_template_parm (parser->implicit_template_parms,
44209 input_location,
44210 node,
44211 /*non_type=*/non_type,
44212 /*param_pack=*/false);
44213
44214 /* Mark the synthetic declaration "virtual". This is used when
44215 comparing template-heads to determine if whether an abbreviated
44216 function template is equivalent to an explicit template.
44217
44218 Note that DECL_ARTIFICIAL is used elsewhere for template parameters. */
44219 DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
44220
44221 // Chain the new parameter to the list of implicit parameters.
44222 if (parser->implicit_template_parms)
44223 parser->implicit_template_parms
44224 = TREE_CHAIN (parser->implicit_template_parms);
44225 else
44226 parser->implicit_template_parms = new_parm;
44227
44228 tree new_decl = get_local_decls ();
44229 if (non_type)
44230 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
44231 new_decl = DECL_INITIAL (new_decl);
44232
44233 /* If creating a fully implicit function template, start the new implicit
44234 template parameter list with this synthesized type, otherwise grow the
44235 current template parameter list. */
44236
44237 if (become_template)
44238 {
44239 parent_scope->level_chain = current_binding_level;
44240
44241 tree new_parms = make_tree_vec (1);
44242 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
44243 current_template_parms = tree_cons (size_int (processing_template_decl),
44244 new_parms, current_template_parms);
44245 }
44246 else
44247 {
44248 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
44249 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
44250 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
44251 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
44252 }
44253
44254 /* If the new parameter was constrained, we need to add that to the
44255 constraints in the template parameter list. */
44256 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
44257 {
44258 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
44259 reqs = combine_constraint_expressions (reqs, req);
44260 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
44261 }
44262
44263 current_binding_level = entry_scope;
44264
44265 return new_decl;
44266 }
44267
44268 /* Finish the declaration of a fully implicit function template. Such a
44269 template has no explicit template parameter list so has not been through the
44270 normal template head and tail processing. synthesize_implicit_template_parm
44271 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
44272 provided if the declaration is a class member such that its template
44273 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
44274 form is returned. Otherwise NULL_TREE is returned. */
44275
44276 static tree
44277 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
44278 {
44279 gcc_assert (parser->fully_implicit_function_template_p);
44280
44281 if (member_decl_opt && member_decl_opt != error_mark_node
44282 && DECL_VIRTUAL_P (member_decl_opt))
44283 {
44284 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
44285 "implicit templates may not be %<virtual%>");
44286 DECL_VIRTUAL_P (member_decl_opt) = false;
44287 }
44288
44289 if (member_decl_opt)
44290 member_decl_opt = finish_member_template_decl (member_decl_opt);
44291 end_template_decl ();
44292
44293 parser->fully_implicit_function_template_p = false;
44294 parser->implicit_template_parms = 0;
44295 parser->implicit_template_scope = 0;
44296 --parser->num_template_parameter_lists;
44297
44298 return member_decl_opt;
44299 }
44300
44301 /* Like finish_fully_implicit_template, but to be used in error
44302 recovery, rearranging scopes so that we restore the state we had
44303 before synthesize_implicit_template_parm inserted the implement
44304 template parms scope. */
44305
44306 static void
44307 abort_fully_implicit_template (cp_parser *parser)
44308 {
44309 cp_binding_level *return_to_scope = current_binding_level;
44310
44311 if (parser->implicit_template_scope
44312 && return_to_scope != parser->implicit_template_scope)
44313 {
44314 cp_binding_level *child = return_to_scope;
44315 for (cp_binding_level *scope = child->level_chain;
44316 scope != parser->implicit_template_scope;
44317 scope = child->level_chain)
44318 child = scope;
44319 child->level_chain = parser->implicit_template_scope->level_chain;
44320 parser->implicit_template_scope->level_chain = return_to_scope;
44321 current_binding_level = parser->implicit_template_scope;
44322 }
44323 else
44324 return_to_scope = return_to_scope->level_chain;
44325
44326 finish_fully_implicit_template (parser, NULL);
44327
44328 gcc_assert (current_binding_level == return_to_scope);
44329 }
44330
44331 /* Helper function for diagnostics that have complained about things
44332 being used with 'extern "C"' linkage.
44333
44334 Attempt to issue a note showing where the 'extern "C"' linkage began. */
44335
44336 void
44337 maybe_show_extern_c_location (void)
44338 {
44339 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
44340 inform (the_parser->innermost_linkage_specification_location,
44341 "%<extern \"C\"%> linkage started here");
44342 }
44343
44344 #include "gt-cp-parser.h"